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