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

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: Changing code entry from class to function. 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 #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 namespace app_host_installer {
113
114 using content::BrowserThread;
115
116 AppHostInstallerImpl::AppHostInstallerImpl() {}
117
118 // Private destructor.
119 AppHostInstallerImpl::~AppHostInstallerImpl() {}
120
121 // (1) is checked here. If true, proceeds to (2), else signals success.
122 void AppHostInstallerImpl::InstallAppHostIfNecessary(
123 const Extension& extension,
124 const base::Callback<void(bool)>& completion_callback) {
125 if (!BrowserThread::GetCurrentThreadIdentifier(&caller_thread_id_)) {
126 NOTREACHED();
127 delete this;
128 return;
129 }
130
131 completion_callback_ = completion_callback;
132 if (extension.is_platform_app())
133 InstallAppHostIfNecessaryInternal();
134 else
135 Finish(true);
136 }
137
138 // (2) is checked here. If false, signals success, else installs App Host.
139 void AppHostInstallerImpl::InstallAppHostIfNecessaryInternal() {
140 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) {
141 // Redo on FILE thread.
142 if (!BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, base::Bind(
143 &AppHostInstallerImpl::InstallAppHostIfNecessaryInternal,
144 base::Unretained(this)))) {
145 Finish(false);
146 }
147 return;
148 }
149
150 if (chrome_launcher_support::IsAppHostPresent())
151 Finish(true);
152 else
153 InstallAppHost();
154 }
155
156 void AppHostInstallerImpl::InstallAppHost() {
157 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
158 DCHECK(!process_.IsValid());
159 if (!LaunchQuickEnableAppHost(&process_)) {
160 Finish(false);
161 } else {
162 DCHECK(process_.IsValid());
163 DCHECK(!delegate_.get());
164 watcher_.StopWatching();
165 delegate_.reset(new QuickEnableWatcher(
166 base::Bind(&AppHostInstallerImpl::Finish, base::Unretained(this))));
167 watcher_.StartWatching(process_, delegate_.get());
168 }
169 }
170
171 void AppHostInstallerImpl::Finish(bool success) {
172 if (!BrowserThread::CurrentlyOn(caller_thread_id_)) {
173 // Redo on caller thread.
174 if (!BrowserThread::PostTask(caller_thread_id_, FROM_HERE, base::Bind(
175 &AppHostInstallerImpl::Finish, base::Unretained(this), success))) {
176 // This could happen in Shutdown....
177 delete this;
178 }
179 return;
180 }
181
182 completion_callback_.Run(success);
183 delete this;
184 }
185
186 } // namespace app_host_installer
187
188 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698