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