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..93e1cebf1174bd7a1dffd0d44484a021a76257dd |
--- /dev/null |
+++ b/chrome/browser/extensions/app_host_installer.cc |
@@ -0,0 +1,144 @@ |
+// 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/logging.h" |
+#include "base/process_util.h" |
+#include "base/string16.h" |
+#include "chrome/common/extensions/extension.h" |
benwells
2012/10/04 07:53:01
Nit: Move includes like string16, logging, process
huangs
2012/10/04 22:56:42
Done.
|
+#include "content/public/browser/browser_thread.h" |
+ |
+#if defined(OS_WIN) |
+#include <windows.h> |
+#include "base/win/object_watcher.h" |
+#include "base/win/registry.h" |
+#include "chrome/installer/launcher_support/chrome_launcher_support.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) { |
benwells
2012/10/04 07:53:01
Nit: {} (on one line)
huangs
2012/10/04 22:56:42
Done.
|
+ } |
+ |
+ // base::win::ObjectWatcher::Delegate implementation. |
+ void OnObjectSignaled(HANDLE object) { |
erikwright (departed)
2012/10/04 01:28:16
virtual ... OVERRIDE
#include compiler_specific
huangs
2012/10/04 22:56:42
Done.
|
+ int exit_code = 0; |
+ base::TerminationStatus status( |
benwells
2012/10/04 07:53:01
I'm not at all familiar withe ObjectWatcher. What
huangs
2012/10/04 22:56:42
If process crashes, then it will return. If it ru
|
+ 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. |
erikwright (departed)
2012/10/04 01:28:16
"returns an empty string if the path does not exis
huangs
2012/10/04 22:56:42
Done.
|
+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 { |
+ |
+AppHostInstaller::AppHostInstaller() { |
benwells
2012/10/04 07:53:01
Nit: {}
huangs
2012/10/04 22:56:42
Done.
|
+} |
+ |
+// static |
+bool AppHostInstaller::IsAppHostInstallRequired(const Extension& extension) { |
+#if defined(OS_WIN) |
+ return extension.is_platform_app() && |
+ !chrome_launcher_support::IsAppHostPresent(); |
+#else |
+ return false; |
+#endif |
+} |
+ |
+void AppHostInstaller::InstallAppHost( |
+ const base::Callback<void(bool)>& callback) { |
+#if defined(OS_WIN) |
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
+ DCHECK(!process_.IsValid()); |
+ if (FAILED(LaunchQuickEnableAppHost(&process_))) { |
erikwright (departed)
2012/10/04 01:28:16
FAILED is for HRESULTS.
if (!Launch...()) {...}
huangs
2012/10/04 22:56:42
Done.
|
+ callback.Run(false); |
+ } else { |
+ DCHECK(process_.IsValid()); |
+ DCHECK(!delegate_.get()); |
+ delegate_.reset(new QuickEnableWatcher(callback)); |
erikwright (departed)
2012/10/04 01:28:16
Although I think it's technically OK, I'd like to
huangs
2012/10/04 22:56:42
Done.
|
+ watcher_.StartWatching(process_, delegate_.get()); |
+ } |
+#else |
+ NOTREACHED() << "Attempt to install the App Host on an unsupported platform."; |
+ callback.Run(false); |
+#endif |
+} |
+ |
+} // namespace extensions |