Index: chrome/browser/extensions/app_host_installer.cc |
diff --git a/chrome/browser/extensions/app_host_installer.cc b/chrome/browser/extensions/app_host_installer.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..4bc8648c6ab756c089860884a45d6209c9ea6bc7 |
--- /dev/null |
+++ b/chrome/browser/extensions/app_host_installer.cc |
@@ -0,0 +1,267 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/extensions/app_host_installer.h" |
+ |
+#include "base/bind.h" |
+#include "base/callback.h" |
+#include "base/compiler_specific.h" |
+#include "chrome/common/extensions/extension.h" |
+ |
+#if defined(OS_WIN) |
+#include <windows.h> |
+#include "base/logging.h" |
+#include "base/process_util.h" |
+#include "base/string16.h" |
+#include "base/win/object_watcher.h" |
+#include "base/win/registry.h" |
+#include "chrome/installer/launcher_support/chrome_launcher_support.h" |
+#include "content/public/browser/browser_thread.h" |
+#endif |
+ |
+namespace { |
+ |
+#if defined(OS_WIN) |
+ |
+// TODO(huangs) Refactor the constants: http://crbug.com/148538 |
+const wchar_t kGoogleRegClientsKey[] = L"Software\\Google\\Update\\Clients"; |
+ |
+// Copied from chrome_appid.cc. |
+const wchar_t kBinariesAppGuid[] = L"{4DC8B4CA-1BDA-483e-B5FA-D3C12E15B62D}"; |
+ |
+// Copied from google_update_constants.cc |
+const wchar_t kRegCommandLineField[] = L"CommandLine"; |
+const wchar_t kRegCommandsKey[] = L"Commands"; |
+ |
+// Copied from util_constants.cc. |
+const wchar_t kCmdQuickEnableApplicationHost[] = |
+ L"quick-enable-application-host"; |
+ |
+class QuickEnableWatcher : public base::win::ObjectWatcher::Delegate { |
+ public: |
+ QuickEnableWatcher(const base::Callback<void(bool)>& callback) |
+ : callback_(callback) {} |
+ |
+ // base::win::ObjectWatcher::Delegate implementation. |
+ virtual void OnObjectSignaled(HANDLE object) OVERRIDE { |
+ int exit_code = 0; |
+ base::TerminationStatus status( |
+ base::GetTerminationStatus(object, &exit_code)); |
+ if (status == base::TERMINATION_STATUS_NORMAL_TERMINATION) { |
+ callback_.Run(true); |
+ } else { |
+ LOG(ERROR) << "App Host install failed, status = " << status |
+ << ", exit code = " << exit_code; |
+ callback_.Run(false); |
+ } |
+ callback_.Reset(); |
+ } |
+ |
+ private: |
+ base::Callback<void(bool)> callback_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(QuickEnableWatcher); |
+}; |
+ |
+// Reads the path to app_host.exe from the value "UninstallString" within the |
+// App Host's "ClientState" registry key. Returns an empty string if the path |
+// does not exist or cannot be read. |
+string16 GetQuickEnableAppHostCommand(bool system_level) { |
+ HKEY root_key = system_level ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; |
+ string16 subkey(kGoogleRegClientsKey); |
+ subkey.append(1, L'\\').append(kBinariesAppGuid) |
+ .append(1, L'\\').append(kRegCommandsKey) |
+ .append(1, L'\\').append(kCmdQuickEnableApplicationHost); |
+ base::win::RegKey reg_key; |
+ string16 cmd; |
+ if (reg_key.Open(root_key, subkey.c_str(), |
+ KEY_QUERY_VALUE) == ERROR_SUCCESS) { |
+ // If read is unsuccessful, |cmd| remains empty. |
+ reg_key.ReadValue(kRegCommandLineField, &cmd); |
+ } |
+ return cmd; |
+} |
+ |
+// Launches the Google Update command to quick-enable App Host. |
+// Returns true if the command is launched. |
+bool LaunchQuickEnableAppHost(base::win::ScopedHandle* process) { |
+ DCHECK(!process->IsValid()); |
+ bool success = false; |
+ |
+ string16 cmd_str(GetQuickEnableAppHostCommand(true)); |
+ if (cmd_str.empty()) { // Try user-level if absent from system-level. |
+ cmd_str = GetQuickEnableAppHostCommand(false); |
+ } |
+ if (!cmd_str.empty()) { |
+ VLOG(1) << "Quick-enabling application host: " << cmd_str; |
+ if (!base::LaunchProcess(cmd_str, base::LaunchOptions(), |
+ process->Receive())) { |
+ LOG(ERROR) << "Failed to quick-enable application host."; |
+ } |
+ success = process->IsValid(); |
+ } |
+ return success; |
+} |
+ |
+#endif |
+ |
+} // namespace |
+ |
+namespace extensions { |
+ |
+#if defined(OS_WIN) |
+ |
+using content::BrowserThread; |
+ |
+// App Host installation is necessary if |
+// (1) |extension| is platform app, and |
+// (2) app_host_exe is not installed. |
+// Complexity arises because (1) can be checked from the caller thread, |
+// but (2) must be checked in the FILE thread. |
+// App Host installation is also launched in the FILE thread as an |
+// asynchronous process. Once installation completes, QuickEnableWatcher |
+// is notified (on the FILE thread). This in turn notifies the caller thread |
+// which then proceed with the next step specified by |final_step|. |
+class AppHostInstallerImpl { |
erikwright (departed)
2012/10/05 02:21:56
Move AppHostInstallerImpl to app_host_installer_im
huangs
2012/10/05 19:46:08
Done.
|
+ public: |
+ AppHostInstallerImpl(); |
+ |
+ // Implementation of AppHostInstaller::InstallAppHostIfNecessaryThenCall(). |
+ void InstallAppHostIfNecessaryThenCall( |
erikwright (departed)
2012/10/05 02:21:56
Rename function and parameter.
"Implementation of
huangs
2012/10/05 19:46:08
Done.
|
+ const Extension& extension, |
+ const base::Callback<void(bool)>& final_step); |
+ |
+ private: |
+ |
erikwright (departed)
2012/10/05 02:21:56
remove blank line
huangs
2012/10/05 19:46:08
Done.
|
+ // Runs on the FILE thread. Continues InstallAppHostIfNecessaryThenCall(). |
+ void InstallAppHostIfNecessaryOnFileThreadThenCall( |
erikwright (departed)
2012/10/05 02:21:56
rename as elsewhere.
huangs
2012/10/05 19:46:08
Done.
|
+ const base::Callback<void(bool)>& final_step); |
+ |
+ // Runs on the FILE thread. Installs App Host, then calls |
+ // |wrapped_final_step|. |
+ void InstallAppHost(const base::Callback<void(bool)>& wrapped_final_step); |
+ |
+ // Called from the FILE thread, to return to caller thread |
+ // and call NotifyOnOriginalThread(), passing |final_step| and |result|. |
+ void PostResultToCallerThread(BrowserThread::ID thread_id, |
erikwright (departed)
2012/10/05 02:21:56
This method and the next one could actually be fre
huangs
2012/10/05 19:46:08
Yes, but now it needs to be move elsewhere, which
|
+ const base::Callback<void(bool)>& final_step, |
erikwright (departed)
2012/10/05 02:21:56
final_step -> original_completion_callback
huangs
2012/10/05 19:46:08
Moot in the new flow, since there is only one comp
|
+ bool result); |
+ |
+ // Callback on the caller thread, to call |final_step| and pass |result|. |
+ void NotifyOnOriginalThread(const base::Callback<void(bool)>& final_step, |
+ bool result); |
+ |
+ base::win::ScopedHandle process_; |
+ base::win::ObjectWatcher watcher_; |
+ scoped_ptr<base::win::ObjectWatcher::Delegate> delegate_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(AppHostInstallerImpl); |
+}; |
+ |
+AppHostInstallerImpl::AppHostInstallerImpl() {} |
+ |
+// (1) is checked here. If true, goes to FILE thread to proceed with (2), |
+// else called |final_step| with true. |
+void AppHostInstallerImpl::InstallAppHostIfNecessaryThenCall( |
+ const Extension& extension, |
+ const base::Callback<void(bool)>& final_step) { |
+ if (extension.is_platform_app()) { |
+ BrowserThread::ID caller_thread_id; |
+ if (!BrowserThread::GetCurrentThreadIdentifier(&caller_thread_id)) { |
+ NOTREACHED(); |
+ } else { |
+ // Wrapping |final_step| with caller thread and the callback. |
+ base::Callback<void(bool)> wrapped_final_step( |
+ base::Bind(&AppHostInstallerImpl::PostResultToCallerThread, |
erikwright (departed)
2012/10/05 02:21:56
Need a comment to explain why this callback can ne
huangs
2012/10/05 19:46:08
Moot, since this is unused in the new flow.
|
+ base::Unretained(this), caller_thread_id, final_step)); |
+ // Continue check (2) in FILE thread. |
+ BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, base::Bind( |
+ &AppHostInstallerImpl::InstallAppHostIfNecessaryOnFileThreadThenCall, |
+ base::Unretained(this), wrapped_final_step)); |
+ } |
+ } else { |
+ final_step.Run(true); |
+ } |
+} |
+ |
+// (2) is checked here. If false, calls |wrapped_final_step| and finish, |
+// else install App Host. |
+void AppHostInstallerImpl::InstallAppHostIfNecessaryOnFileThreadThenCall( |
+ const base::Callback<void(bool)>& wrapped_final_step) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
+ if (chrome_launcher_support::IsAppHostPresent()) |
+ wrapped_final_step.Run(true); |
+ else |
+ InstallAppHost(wrapped_final_step); |
+} |
+ |
+// Main installation step, calls |wrapped_final_step| after. |
+void AppHostInstallerImpl::InstallAppHost( |
+ const base::Callback<void(bool)>& wrapped_final_step) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
+ DCHECK(!process_.IsValid()); |
+ if (!LaunchQuickEnableAppHost(&process_)) { |
+ wrapped_final_step.Run(false); |
+ } else { |
+ DCHECK(process_.IsValid()); |
+ DCHECK(!delegate_.get()); |
+ watcher_.StopWatching(); |
+ delegate_.reset(new QuickEnableWatcher(wrapped_final_step)); |
+ watcher_.StartWatching(process_, delegate_.get()); |
+ } |
+} |
+ |
+// Destination of |wrapped_final_step|, invokes NotifyOnOriginalThread() |
+// in the caller thread, passing |final_step| and |result|. |
+void AppHostInstallerImpl::PostResultToCallerThread( |
+ BrowserThread::ID thread_id, |
+ const base::Callback<void(bool)>& final_step, |
+ bool result) { |
+ BrowserThread::PostTask(thread_id, FROM_HERE, |
+ base::Bind(&AppHostInstallerImpl::NotifyOnOriginalThread, |
+ base::Unretained(this), final_step, result)); |
+} |
+ |
+// Calls |final_step|, and passes |result| to complete flow. |
+void AppHostInstallerImpl::NotifyOnOriginalThread( |
+ const base::Callback<void(bool)>& final_step, |
+ bool result) { |
+ final_step.Run(result); |
+} |
+ |
+#else |
+ |
+// Stub implementation for OS without App Host. |
+class AppHostInstallerImpl { |
erikwright (departed)
2012/10/05 02:21:56
No impl required in the else case - see below...
huangs
2012/10/05 19:46:08
Done.
|
+ public: |
+ AppHostInstallerImpl::AppHostInstallerImpl(); |
+ |
+ void AppHostInstallerImpl::InstallAppHostIfNecessaryThenCall( |
+ const Extension& extension, |
+ const base::Callback<void(bool)>& final_step); |
+}; |
+ |
+AppHostInstallerImpl::AppHostInstallerImpl() {} |
+ |
+void AppHostInstallerImpl::InstallAppHostIfNecessaryThenCall( |
+ const Extension& extension, |
+ const base::Callback<void(bool)>& final_step) { |
+ final_step.Run(true); |
+} |
+ |
+#endif |
+ |
+AppHostInstaller::AppHostInstaller() : impl_(new AppHostInstallerImpl()) {} |
erikwright (departed)
2012/10/05 02:21:56
#if win, new AppHostInstallerImpl
#else 0
huangs
2012/10/05 19:46:08
Done.
|
+ |
+AppHostInstaller::~AppHostInstaller() { |
+ delete impl_; |
erikwright (departed)
2012/10/05 02:21:56
if impl_
delete impl_
huangs
2012/10/05 19:46:08
Done.
|
+} |
+ |
+void AppHostInstaller::InstallAppHostIfNecessaryThenCall( |
+ const Extension& extension, |
+ const base::Callback<void(bool)>& final_step) { |
+ impl_->InstallAppHostIfNecessaryThenCall(extension, final_step); |
erikwright (departed)
2012/10/05 02:21:56
if impl_
impl_->...
else
final_step.Run(true)
huangs
2012/10/05 19:46:08
Done, but need to use #if define(OS_WIN), since we
|
+} |
+ |
+} // namespace extensions |