Chromium Code Reviews| 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..04270da822c23eb3919805caa613fc7c962c0326 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/app_host_installer_impl_win.cc |
| @@ -0,0 +1,201 @@ |
| +// 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/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"; |
| + |
| +// QuickEnableDelegate handles the completion event of App Host installation |
| +// via the quick-enable-application host command. At construction, the |
| +// class is given |callback_| that takes a bool parameter. |
| +// Upon completion, |callback_| is invoked, and is passed a boolean to |
| +// indicate success or failure of installation. |
| +class QuickEnableDelegate : public base::win::ObjectWatcher::Delegate { |
| + public: |
| + QuickEnableDelegate(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(QuickEnableDelegate); |
| +}; |
| + |
| +// 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 { |
| + |
| +namespace app_host_installer { |
| + |
| +using content::BrowserThread; |
| + |
| +// static function. |
|
erikwright (departed)
2012/10/09 21:51:52
just "// static"
huangs
2012/10/09 22:44:26
Done.
|
| +void AppHostInstallerImpl::EnsureAppHostInstalled( |
| + const base::Callback<void(bool)>& completion_callback) { |
| + BrowserThread::ID caller_thread_id; |
| + if (!BrowserThread::GetCurrentThreadIdentifier(&caller_thread_id)) { |
| + NOTREACHED(); |
| + return; |
| + } |
| + |
| + AppHostInstallerImpl *impl = |
|
erikwright (departed)
2012/10/09 21:51:52
the assignment to a local variable is unnecessary.
huangs
2012/10/09 22:44:26
Done. Needed (new ...)->... to make compiler happ
|
| + new AppHostInstallerImpl(completion_callback, caller_thread_id); |
| + impl->EnsureAppHostInstalledInternal(); |
| + // impl will delete itself. |
|
erikwright (departed)
2012/10/09 21:51:52
// AppHostInstalerImpl will delete itself
new AppH
huangs
2012/10/09 22:44:26
Done.
|
| +} |
| + |
| +// AppHostInstallerImpl checks the presence of app_host.exe, and launches |
|
erikwright (departed)
2012/10/09 21:51:52
This comment should probably be at the top of this
huangs
2012/10/09 22:44:26
Done.
|
| +// the installer if missing. The check must be performed on the FILE thread. |
| +// The installation is also launched on the FILE thread as an asynchronous |
| +// process. Once installation completes, QuickEnableWatcher is notified. |
| +// Finish() is called in the end, which returns to the caller thread and |
| +// calls |completion_callback|, followed by self-destruction. |
| + |
| +// Private constructor. |
| +AppHostInstallerImpl::AppHostInstallerImpl( |
|
erikwright (departed)
2012/10/09 21:51:52
"// Private constructor." is not necessary.
huangs
2012/10/09 22:44:26
Done.
|
| + const base::Callback<void(bool)>& completion_callback, |
| + BrowserThread::ID caller_thread_id) |
| + : completion_callback_(completion_callback), |
| + caller_thread_id_(caller_thread_id) {} |
| + |
| +// Private destructor. |
|
erikwright (departed)
2012/10/09 21:51:52
not necessary.
huangs
2012/10/09 22:44:26
Done.
|
| +AppHostInstallerImpl::~AppHostInstallerImpl() {} |
| + |
| +// Checks the system state on the FILE thread. Will trigger InstallAppHost() |
|
erikwright (departed)
2012/10/09 21:51:52
Unless you have implementation details to add beyo
huangs
2012/10/09 22:44:26
Done.
|
| +// if App Host is not installed. |
| +void AppHostInstallerImpl::EnsureAppHostInstalledInternal() { |
| + if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) { |
| + // Redo on FILE thread. |
| + if (!BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| + base::Bind(&AppHostInstallerImpl::EnsureAppHostInstalledInternal, |
| + base::Unretained(this)))) { |
| + Finish(false); |
| + } |
| + return; |
| + } |
| + |
| + if (chrome_launcher_support::IsAppHostPresent()) |
| + Finish(true); |
| + else |
| + InstallAppHost(); |
| +} |
| + |
| +// Asynchronously triggers the App Host installation. Will call "Finish" upon |
|
erikwright (departed)
2012/10/09 21:51:52
Ditto.
huangs
2012/10/09 22:44:26
Done. Moved the comment to the .h file.
|
| +// completion (successful or otherwise). |
| +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 QuickEnableDelegate( |
| + base::Bind(&AppHostInstallerImpl::Finish, base::Unretained(this)))); |
| + watcher_.StartWatching(process_, delegate_.get()); |
| + } |
| +} |
| + |
| +// Passes |success| to |completion_callback| on the original caller thread. |
|
erikwright (departed)
2012/10/09 21:51:52
Ditto.
huangs
2012/10/09 22:44:26
Done.
|
| +// Deletes the AppHostInstallerImpl. |
| +void AppHostInstallerImpl::Finish(bool success) { |
| + if (!BrowserThread::CurrentlyOn(caller_thread_id_)) { |
| + // Redo on caller thread. |
| + if (!BrowserThread::PostTask(caller_thread_id_, FROM_HERE, base::Bind( |
| + &AppHostInstallerImpl::Finish, base::Unretained(this), success))) { |
| + // This could happen in Shutdown.... |
| + delete this; |
| + } |
| + return; |
| + } |
| + |
| + completion_callback_.Run(success); |
| + delete this; |
| +} |
| + |
| +} // namespace app_host_installer |
| + |
| +} // namespace extensions |