Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1353)

Unified Diff: chrome/browser/extensions/app_host_installer_impl_win.cc

Issue 11054006: Make application shortcuts point to app_host.exe, install App Host during app installation. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Extracting app_host_installer_impl_win.*; proposing simplified flow to switch between threads for A… Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/app_host_installer_impl_win.cc
diff --git a/chrome/browser/extensions/app_host_installer_impl_win.cc b/chrome/browser/extensions/app_host_installer_impl_win.cc
new file mode 100644
index 0000000000000000000000000000000000000000..cb0ad6888e55e8cb991a36d94b4185ed864bdadc
--- /dev/null
+++ b/chrome/browser/extensions/app_host_installer_impl_win.cc
@@ -0,0 +1,165 @@
+// 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_impl_win.h"
+
+#include <windows.h>
+#include "base/basictypes.h"
+#include "base/bind.h"
+#include "base/callback.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/browser/extensions/app_host_installer.h"
+#include "chrome/common/extensions/extension.h"
+#include "chrome/installer/launcher_support/chrome_launcher_support.h"
+#include "content/public/browser/browser_thread.h"
+
+namespace {
+
+// 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 {
erikwright (departed) 2012/10/05 21:18:23 Add class comments.
huangs 2012/10/07 01:42:25 Done.
+ 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;
+}
+
+} // namespace
+
+namespace extensions {
+
+using content::BrowserThread;
+
+AppHostInstallerImpl::AppHostInstallerImpl() {}
+
+// (1) is checked here. If true, proceeds to (2), else signals success.
+void AppHostInstallerImpl::InstallAppHostIfNecessary(
+ const Extension& extension,
+ const base::Callback<void(bool)>& completion_callback) {
+ if (!BrowserThread::GetCurrentThreadIdentifier(&caller_thread_id_)) {
erikwright (departed) 2012/10/05 21:18:23 DCHECK completion_callback_.IsNull()
huangs 2012/10/07 01:42:25 I don't understand this (callback.h has no IsNull(
+ NOTREACHED();
+ return;
+ }
+ completion_callback_ = completion_callback;
+
+ if (extension.is_platform_app())
+ InstallAppHostIfNecessaryInternal();
+ else
+ Finish(true);
+}
+
+// (2) is checked here. If false, signals success, else installs App Host.
+void AppHostInstallerImpl::InstallAppHostIfNecessaryInternal() {
+ if (BrowserThread::CurrentlyOn(BrowserThread::FILE)) {
+ if (chrome_launcher_support::IsAppHostPresent())
+ Finish(true);
+ else
+ InstallAppHost();
+ } else { // Redo on FILE thread.
+ BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, base::Bind(
+ &AppHostInstallerImpl::InstallAppHostIfNecessaryInternal,
+ base::Unretained(this)));
erikwright (departed) 2012/10/05 21:18:23 Can't the impl potentially be deleted before this
huangs 2012/10/07 01:42:25 I'm letting AppHostInstallerImpl manage its own li
+ }
+}
+
+void AppHostInstallerImpl::InstallAppHost() {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
+ DCHECK(!process_.IsValid());
+ if (!LaunchQuickEnableAppHost(&process_)) {
+ Finish(false);
+ } else {
+ DCHECK(process_.IsValid());
+ DCHECK(!delegate_.get());
+ watcher_.StopWatching();
+ delegate_.reset(new QuickEnableWatcher(
+ base::Bind(&AppHostInstallerImpl::Finish, base::Unretained(this))));
erikwright (departed) 2012/10/05 21:18:23 Here I think the Unretained is probably safe, but
huangs 2012/10/07 01:42:25 If AppHostInstallerImpl deletes itself, then this
+ watcher_.StartWatching(process_, delegate_.get());
+ }
+}
+
+void AppHostInstallerImpl::Finish(bool success) {
+ if (BrowserThread::CurrentlyOn(caller_thread_id_)) {
+ completion_callback_.Run(success);
+ } else { // Redo on caller thread.
+ BrowserThread::PostTask(caller_thread_id_, FROM_HERE, base::Bind(
+ &AppHostInstallerImpl::Finish, base::Unretained(this), success));
erikwright (departed) 2012/10/05 21:18:23 Same concern here about Unretained.
huangs 2012/10/07 01:42:25 Ditto.
+ }
+}
+
+} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698