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 // AppHostInstaller checks the presence of app_host.exe, and launches |
| 6 // the installer if missing. The check must be performed on the FILE thread. |
| 7 // The installation is also launched on the FILE thread as an asynchronous |
| 8 // process. Once installation completes, QuickEnableWatcher is notified. |
| 9 // AppHostInstaller::FinishOnCallerThread() is called in the end, |
| 10 // which notifies the caller via a completion callback on the original |
| 11 // calling thread, and then destroys the AppHostInstaller instance. |
| 12 |
| 13 #include "chrome/browser/extensions/app_host_installer_win.h" |
| 14 |
| 15 #include <windows.h> |
| 16 #include "base/bind.h" |
| 17 #include "base/callback.h" |
| 18 #include "base/logging.h" |
| 19 #include "base/process_util.h" |
| 20 #include "base/string16.h" |
| 21 #include "base/win/registry.h" |
| 22 #include "chrome/installer/launcher_support/chrome_launcher_support.h" |
| 23 |
| 24 namespace { |
| 25 |
| 26 using extensions::OnAppHostInstallationCompleteCallback; |
| 27 |
| 28 // TODO(huangs) Refactor the constants: http://crbug.com/148538 |
| 29 const wchar_t kGoogleRegClientsKey[] = L"Software\\Google\\Update\\Clients"; |
| 30 |
| 31 // Copied from chrome_appid.cc. |
| 32 const wchar_t kBinariesAppGuid[] = L"{4DC8B4CA-1BDA-483e-B5FA-D3C12E15B62D}"; |
| 33 |
| 34 // Copied from google_update_constants.cc |
| 35 const wchar_t kRegCommandLineField[] = L"CommandLine"; |
| 36 const wchar_t kRegCommandsKey[] = L"Commands"; |
| 37 |
| 38 // Copied from util_constants.cc. |
| 39 const wchar_t kCmdQuickEnableApplicationHost[] = |
| 40 L"quick-enable-application-host"; |
| 41 |
| 42 // QuickEnableDelegate handles the completion event of App Host installation |
| 43 // via the quick-enable-application host command. At construction, the |
| 44 // class is given |callback_| that takes a bool parameter. |
| 45 // Upon completion, |callback_| is invoked, and is passed a boolean to |
| 46 // indicate success or failure of installation. |
| 47 class QuickEnableDelegate : public base::win::ObjectWatcher::Delegate { |
| 48 public: |
| 49 explicit QuickEnableDelegate( |
| 50 const OnAppHostInstallationCompleteCallback& callback); |
| 51 |
| 52 ~QuickEnableDelegate(); |
| 53 |
| 54 // base::win::ObjectWatcher::Delegate implementation. |
| 55 virtual void OnObjectSignaled(HANDLE object) OVERRIDE; |
| 56 |
| 57 private: |
| 58 OnAppHostInstallationCompleteCallback callback_; |
| 59 |
| 60 DISALLOW_COPY_AND_ASSIGN(QuickEnableDelegate); |
| 61 }; |
| 62 |
| 63 QuickEnableDelegate::QuickEnableDelegate( |
| 64 const OnAppHostInstallationCompleteCallback& callback) |
| 65 : callback_(callback) {} |
| 66 |
| 67 QuickEnableDelegate::~QuickEnableDelegate() {} |
| 68 |
| 69 void QuickEnableDelegate::OnObjectSignaled(HANDLE object) { |
| 70 int exit_code = 0; |
| 71 base::TerminationStatus status( |
| 72 base::GetTerminationStatus(object, &exit_code)); |
| 73 if (status == base::TERMINATION_STATUS_NORMAL_TERMINATION) { |
| 74 callback_.Run(true); |
| 75 } else { |
| 76 LOG(ERROR) << "App Host install failed, status = " << status |
| 77 << ", exit code = " << exit_code; |
| 78 callback_.Run(false); |
| 79 } |
| 80 callback_.Reset(); |
| 81 } |
| 82 |
| 83 // Reads the path to app_host.exe from the value "UninstallString" within the |
| 84 // App Host's "ClientState" registry key. Returns an empty string if the path |
| 85 // does not exist or cannot be read. |
| 86 string16 GetQuickEnableAppHostCommand(bool system_level) { |
| 87 HKEY root_key = system_level ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; |
| 88 string16 subkey(kGoogleRegClientsKey); |
| 89 subkey.append(1, L'\\').append(kBinariesAppGuid) |
| 90 .append(1, L'\\').append(kRegCommandsKey) |
| 91 .append(1, L'\\').append(kCmdQuickEnableApplicationHost); |
| 92 base::win::RegKey reg_key; |
| 93 string16 cmd; |
| 94 if (reg_key.Open(root_key, subkey.c_str(), |
| 95 KEY_QUERY_VALUE) == ERROR_SUCCESS) { |
| 96 // If read is unsuccessful, |cmd| remains empty. |
| 97 reg_key.ReadValue(kRegCommandLineField, &cmd); |
| 98 } |
| 99 return cmd; |
| 100 } |
| 101 |
| 102 // Launches the Google Update command to quick-enable App Host. |
| 103 // Returns true if the command is launched. |
| 104 bool LaunchQuickEnableAppHost(base::win::ScopedHandle* process) { |
| 105 DCHECK(!process->IsValid()); |
| 106 bool success = false; |
| 107 |
| 108 string16 cmd_str(GetQuickEnableAppHostCommand(true)); |
| 109 if (cmd_str.empty()) // Try user-level if absent from system-level. |
| 110 cmd_str = GetQuickEnableAppHostCommand(false); |
| 111 if (!cmd_str.empty()) { |
| 112 VLOG(1) << "Quick-enabling application host: " << cmd_str; |
| 113 if (!base::LaunchProcess(cmd_str, base::LaunchOptions(), |
| 114 process->Receive())) { |
| 115 LOG(ERROR) << "Failed to quick-enable application host."; |
| 116 } |
| 117 success = process->IsValid(); |
| 118 } |
| 119 return success; |
| 120 } |
| 121 |
| 122 } // namespace |
| 123 |
| 124 namespace extensions { |
| 125 |
| 126 using content::BrowserThread; |
| 127 |
| 128 // static |
| 129 void AppHostInstaller::EnsureAppHostInstalled( |
| 130 const OnAppHostInstallationCompleteCallback& completion_callback) { |
| 131 BrowserThread::ID caller_thread_id; |
| 132 if (!BrowserThread::GetCurrentThreadIdentifier(&caller_thread_id)) { |
| 133 NOTREACHED(); |
| 134 return; |
| 135 } |
| 136 |
| 137 // AppHostInstaler will delete itself |
| 138 (new AppHostInstaller(completion_callback, caller_thread_id))-> |
| 139 EnsureAppHostInstalledInternal(); |
| 140 } |
| 141 |
| 142 AppHostInstaller::AppHostInstaller( |
| 143 const OnAppHostInstallationCompleteCallback& completion_callback, |
| 144 BrowserThread::ID caller_thread_id) |
| 145 : completion_callback_(completion_callback), |
| 146 caller_thread_id_(caller_thread_id) {} |
| 147 |
| 148 AppHostInstaller::~AppHostInstaller() {} |
| 149 |
| 150 void AppHostInstaller::EnsureAppHostInstalledInternal() { |
| 151 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) { |
| 152 // Redo on FILE thread. |
| 153 if (!BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 154 base::Bind(&AppHostInstaller::EnsureAppHostInstalledInternal, |
| 155 base::Unretained(this)))) { |
| 156 FinishOnCallerThread(false); |
| 157 } |
| 158 return; |
| 159 } |
| 160 |
| 161 if (chrome_launcher_support::IsAppHostPresent()) |
| 162 FinishOnCallerThread(true); |
| 163 else |
| 164 InstallAppHostOnFileThread(); |
| 165 } |
| 166 |
| 167 void AppHostInstaller::InstallAppHostOnFileThread() { |
| 168 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 169 DCHECK(!process_.IsValid()); |
| 170 if (!LaunchQuickEnableAppHost(&process_)) { |
| 171 FinishOnCallerThread(false); |
| 172 } else { |
| 173 DCHECK(process_.IsValid()); |
| 174 DCHECK(!delegate_.get()); |
| 175 watcher_.StopWatching(); |
| 176 delegate_.reset(new QuickEnableDelegate( |
| 177 base::Bind(&AppHostInstaller::FinishOnCallerThread, |
| 178 base::Unretained(this)))); |
| 179 watcher_.StartWatching(process_, delegate_.get()); |
| 180 } |
| 181 } |
| 182 |
| 183 void AppHostInstaller::FinishOnCallerThread(bool success) { |
| 184 if (!BrowserThread::CurrentlyOn(caller_thread_id_)) { |
| 185 // Redo on caller thread. |
| 186 if (!BrowserThread::PostTask(caller_thread_id_, FROM_HERE, |
| 187 base::Bind(&AppHostInstaller::FinishOnCallerThread, |
| 188 base::Unretained(this), |
| 189 success))) { |
| 190 // This could happen in Shutdown.... |
| 191 delete this; |
| 192 } |
| 193 return; |
| 194 } |
| 195 |
| 196 completion_callback_.Run(success); |
| 197 delete this; |
| 198 } |
| 199 |
| 200 } // namespace extensions |
OLD | NEW |