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