OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/extensions/app_host_installer_impl_win.h" |
| 6 |
| 7 #include <windows.h> |
| 8 #include "base/basictypes.h" |
| 9 #include "base/bind.h" |
| 10 #include "base/callback.h" |
| 11 #include "base/logging.h" |
| 12 #include "base/process_util.h" |
| 13 #include "base/string16.h" |
| 14 #include "base/win/object_watcher.h" |
| 15 #include "base/win/registry.h" |
| 16 #include "chrome/browser/extensions/app_host_installer.h" |
| 17 #include "chrome/common/extensions/extension.h" |
| 18 #include "chrome/installer/launcher_support/chrome_launcher_support.h" |
| 19 #include "content/public/browser/browser_thread.h" |
| 20 |
| 21 namespace { |
| 22 |
| 23 // TODO(huangs) Refactor the constants: http://crbug.com/148538 |
| 24 const wchar_t kGoogleRegClientsKey[] = L"Software\\Google\\Update\\Clients"; |
| 25 |
| 26 // Copied from chrome_appid.cc. |
| 27 const wchar_t kBinariesAppGuid[] = L"{4DC8B4CA-1BDA-483e-B5FA-D3C12E15B62D}"; |
| 28 |
| 29 // Copied from google_update_constants.cc |
| 30 const wchar_t kRegCommandLineField[] = L"CommandLine"; |
| 31 const wchar_t kRegCommandsKey[] = L"Commands"; |
| 32 |
| 33 // Copied from util_constants.cc. |
| 34 const wchar_t kCmdQuickEnableApplicationHost[] = |
| 35 L"quick-enable-application-host"; |
| 36 |
| 37 // QuickEnableWatcher watches the completion event of App Host installation |
| 38 // via the quick-enable-application host command. At construction, the |
| 39 // class is given |callback_| that takes a bool parameter. |
| 40 // Upon completion, |callback_| is invoked, and is passed a boolean to |
| 41 // indicate success or failure of installation. |
| 42 class QuickEnableWatcher : public base::win::ObjectWatcher::Delegate { |
| 43 public: |
| 44 QuickEnableWatcher(const base::Callback<void(bool)>& callback) |
| 45 : callback_(callback) {} |
| 46 |
| 47 // base::win::ObjectWatcher::Delegate implementation. |
| 48 virtual void OnObjectSignaled(HANDLE object) OVERRIDE { |
| 49 int exit_code = 0; |
| 50 base::TerminationStatus status( |
| 51 base::GetTerminationStatus(object, &exit_code)); |
| 52 if (status == base::TERMINATION_STATUS_NORMAL_TERMINATION) { |
| 53 callback_.Run(true); |
| 54 } else { |
| 55 LOG(ERROR) << "App Host install failed, status = " << status |
| 56 << ", exit code = " << exit_code; |
| 57 callback_.Run(false); |
| 58 } |
| 59 callback_.Reset(); |
| 60 } |
| 61 |
| 62 private: |
| 63 base::Callback<void(bool)> callback_; |
| 64 |
| 65 DISALLOW_COPY_AND_ASSIGN(QuickEnableWatcher); |
| 66 }; |
| 67 |
| 68 // Reads the path to app_host.exe from the value "UninstallString" within the |
| 69 // App Host's "ClientState" registry key. Returns an empty string if the path |
| 70 // does not exist or cannot be read. |
| 71 string16 GetQuickEnableAppHostCommand(bool system_level) { |
| 72 HKEY root_key = system_level ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; |
| 73 string16 subkey(kGoogleRegClientsKey); |
| 74 subkey.append(1, L'\\').append(kBinariesAppGuid) |
| 75 .append(1, L'\\').append(kRegCommandsKey) |
| 76 .append(1, L'\\').append(kCmdQuickEnableApplicationHost); |
| 77 base::win::RegKey reg_key; |
| 78 string16 cmd; |
| 79 if (reg_key.Open(root_key, subkey.c_str(), |
| 80 KEY_QUERY_VALUE) == ERROR_SUCCESS) { |
| 81 // If read is unsuccessful, |cmd| remains empty. |
| 82 reg_key.ReadValue(kRegCommandLineField, &cmd); |
| 83 } |
| 84 return cmd; |
| 85 } |
| 86 |
| 87 // Launches the Google Update command to quick-enable App Host. |
| 88 // Returns true if the command is launched. |
| 89 bool LaunchQuickEnableAppHost(base::win::ScopedHandle* process) { |
| 90 DCHECK(!process->IsValid()); |
| 91 bool success = false; |
| 92 |
| 93 string16 cmd_str(GetQuickEnableAppHostCommand(true)); |
| 94 if (cmd_str.empty()) { // Try user-level if absent from system-level. |
| 95 cmd_str = GetQuickEnableAppHostCommand(false); |
| 96 } |
| 97 if (!cmd_str.empty()) { |
| 98 VLOG(1) << "Quick-enabling application host: " << cmd_str; |
| 99 if (!base::LaunchProcess(cmd_str, base::LaunchOptions(), |
| 100 process->Receive())) { |
| 101 LOG(ERROR) << "Failed to quick-enable application host."; |
| 102 } |
| 103 success = process->IsValid(); |
| 104 } |
| 105 return success; |
| 106 } |
| 107 |
| 108 } // namespace |
| 109 |
| 110 namespace extensions { |
| 111 |
| 112 using content::BrowserThread; |
| 113 |
| 114 AppHostInstallerImpl::AppHostInstallerImpl() {} |
| 115 |
| 116 // Private destructor. |
| 117 AppHostInstallerImpl::~AppHostInstallerImpl() {} |
| 118 |
| 119 // (1) is checked here. If true, proceeds to (2), else signals success. |
| 120 void AppHostInstallerImpl::InstallAppHostIfNecessary( |
| 121 const Extension& extension, |
| 122 const base::Callback<void(bool)>& completion_callback) { |
| 123 if (!BrowserThread::GetCurrentThreadIdentifier(&caller_thread_id_)) { |
| 124 NOTREACHED(); |
| 125 delete this; |
| 126 return; |
| 127 } |
| 128 completion_callback_ = completion_callback; |
| 129 |
| 130 if (extension.is_platform_app()) |
| 131 InstallAppHostIfNecessaryInternal(); |
| 132 else |
| 133 Finish(true); |
| 134 } |
| 135 |
| 136 // (2) is checked here. If false, signals success, else installs App Host. |
| 137 void AppHostInstallerImpl::InstallAppHostIfNecessaryInternal() { |
| 138 if (BrowserThread::CurrentlyOn(BrowserThread::FILE)) { |
| 139 if (chrome_launcher_support::IsAppHostPresent()) |
| 140 Finish(true); |
| 141 else |
| 142 InstallAppHost(); |
| 143 } else { // Redo on FILE thread. |
| 144 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, base::Bind( |
| 145 &AppHostInstallerImpl::InstallAppHostIfNecessaryInternal, |
| 146 base::Unretained(this))); |
| 147 } |
| 148 } |
| 149 |
| 150 void AppHostInstallerImpl::InstallAppHost() { |
| 151 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 152 DCHECK(!process_.IsValid()); |
| 153 if (!LaunchQuickEnableAppHost(&process_)) { |
| 154 Finish(false); |
| 155 } else { |
| 156 DCHECK(process_.IsValid()); |
| 157 DCHECK(!delegate_.get()); |
| 158 watcher_.StopWatching(); |
| 159 delegate_.reset(new QuickEnableWatcher( |
| 160 base::Bind(&AppHostInstallerImpl::Finish, base::Unretained(this)))); |
| 161 watcher_.StartWatching(process_, delegate_.get()); |
| 162 } |
| 163 } |
| 164 |
| 165 void AppHostInstallerImpl::Finish(bool success) { |
| 166 if (BrowserThread::CurrentlyOn(caller_thread_id_)) { |
| 167 completion_callback_.Run(success); |
| 168 delete this; |
| 169 } else { // Redo on caller thread. |
| 170 BrowserThread::PostTask(caller_thread_id_, FROM_HERE, base::Bind( |
| 171 &AppHostInstallerImpl::Finish, base::Unretained(this), success)); |
| 172 } |
| 173 } |
| 174 |
| 175 } // namespace extensions |
OLD | NEW |