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

Side by Side 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: Stylistic changes: typedef and function renames. 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 unified diff | Download patch
OLDNEW
(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 // AppHostInstallerImpl 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 // AppHostInstallerImpl::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 AppHostInstallerImpl instance.
12
13 #include "chrome/browser/extensions/app_host_installer_impl_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::app_host_installer::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 namespace app_host_installer {
127
128 using content::BrowserThread;
129
130 // static
131 void AppHostInstallerImpl::EnsureAppHostInstalled(
132 const OnAppHostInstallationCompleteCallback& completion_callback) {
133 BrowserThread::ID caller_thread_id;
134 if (!BrowserThread::GetCurrentThreadIdentifier(&caller_thread_id)) {
135 NOTREACHED();
136 return;
137 }
138
139 // AppHostInstalerImpl will delete itself
140 (new AppHostInstallerImpl(completion_callback, caller_thread_id))->
141 EnsureAppHostInstalledInternal();
142 }
143
144 AppHostInstallerImpl::AppHostInstallerImpl(
145 const OnAppHostInstallationCompleteCallback& completion_callback,
146 BrowserThread::ID caller_thread_id)
147 : completion_callback_(completion_callback),
148 caller_thread_id_(caller_thread_id) {}
149
150 AppHostInstallerImpl::~AppHostInstallerImpl() {}
151
152 void AppHostInstallerImpl::EnsureAppHostInstalledInternal() {
153 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) {
154 // Redo on FILE thread.
155 if (!BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
156 base::Bind(&AppHostInstallerImpl::EnsureAppHostInstalledInternal,
157 base::Unretained(this)))) {
158 FinishOnCallerThread(false);
159 }
160 return;
161 }
162
163 if (chrome_launcher_support::IsAppHostPresent())
164 FinishOnCallerThread(true);
165 else
166 InstallAppHostOnFileThread();
167 }
168
169 void AppHostInstallerImpl::InstallAppHostOnFileThread() {
170 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
171 DCHECK(!process_.IsValid());
172 if (!LaunchQuickEnableAppHost(&process_)) {
173 FinishOnCallerThread(false);
174 } else {
175 DCHECK(process_.IsValid());
176 DCHECK(!delegate_.get());
177 watcher_.StopWatching();
178 delegate_.reset(new QuickEnableDelegate(
179 base::Bind(&AppHostInstallerImpl::FinishOnCallerThread,
180 base::Unretained(this))));
181 watcher_.StartWatching(process_, delegate_.get());
182 }
183 }
184
185 void AppHostInstallerImpl::FinishOnCallerThread(bool success) {
186 if (!BrowserThread::CurrentlyOn(caller_thread_id_)) {
187 // Redo on caller thread.
188 if (!BrowserThread::PostTask(caller_thread_id_, FROM_HERE,
189 base::Bind(&AppHostInstallerImpl::FinishOnCallerThread,
190 base::Unretained(this),
191 success))) {
192 // This could happen in Shutdown....
193 delete this;
194 }
195 return;
196 }
197
198 completion_callback_.Run(success);
199 delete this;
200 }
201
202 } // namespace app_host_installer
203
204 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698