OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/component_updater/pnacl/pnacl_component_installer.h" | 5 #include "chrome/browser/component_updater/pnacl/pnacl_component_installer.h" |
6 | 6 |
7 #include "base/base_paths.h" | 7 #include "base/base_paths.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/callback.h" |
9 #include "base/command_line.h" | 10 #include "base/command_line.h" |
10 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
11 #include "base/file_util.h" | 12 #include "base/file_util.h" |
12 #include "base/files/file_enumerator.h" | 13 #include "base/files/file_enumerator.h" |
13 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
14 #include "base/json/json_file_value_serializer.h" | 15 #include "base/json/json_file_value_serializer.h" |
15 #include "base/logging.h" | 16 #include "base/logging.h" |
16 #include "base/path_service.h" | 17 #include "base/path_service.h" |
17 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
18 #include "base/values.h" | 19 #include "base/values.h" |
19 #include "base/version.h" | 20 #include "base/version.h" |
20 #include "base/win/windows_version.h" | 21 #include "base/win/windows_version.h" |
21 #include "build/build_config.h" | 22 #include "build/build_config.h" |
22 #include "chrome/browser/browser_process.h" | 23 #include "chrome/browser/browser_process.h" |
23 #include "chrome/browser/component_updater/component_updater_service.h" | 24 #include "chrome/browser/component_updater/component_updater_service.h" |
24 #include "chrome/browser/profiles/profile.h" | 25 #include "chrome/browser/profiles/profile.h" |
25 #include "chrome/browser/profiles/profile_manager.h" | 26 #include "chrome/browser/profiles/profile_manager.h" |
26 #include "chrome/common/chrome_paths.h" | 27 #include "chrome/common/chrome_paths.h" |
27 #include "chrome/common/chrome_switches.h" | 28 #include "chrome/common/chrome_switches.h" |
28 #include "chrome/common/omaha_query_params/omaha_query_params.h" | 29 #include "chrome/common/omaha_query_params/omaha_query_params.h" |
29 #include "content/public/browser/browser_thread.h" | 30 #include "content/public/browser/browser_thread.h" |
30 | 31 |
31 using chrome::OmahaQueryParams; | 32 using chrome::OmahaQueryParams; |
32 using content::BrowserThread; | 33 using content::BrowserThread; |
33 | 34 |
34 namespace { | 35 namespace { |
35 | 36 |
36 // If PNaCl isn't installed yet, but a user is running chrome with | |
37 // --enable-pnacl, this is the amount of time to wait before starting | |
38 // a background install. | |
39 const int kInitialDelaySeconds = 10; | |
40 | |
41 // Name of the Pnacl component specified in the manifest. | 37 // Name of the Pnacl component specified in the manifest. |
42 const char kPnaclManifestName[] = "PNaCl Translator"; | 38 const char kPnaclManifestName[] = "PNaCl Translator"; |
43 | 39 |
44 // Sanitize characters from Pnacl Arch value so that they can be used | 40 // Sanitize characters from Pnacl Arch value so that they can be used |
45 // in path names. This should only be characters in the set: [a-z0-9_]. | 41 // in path names. This should only be characters in the set: [a-z0-9_]. |
46 // Keep in sync with chrome/browser/nacl_host/nacl_file_host. | 42 // Keep in sync with chrome/browser/nacl_host/nacl_file_host. |
47 std::string SanitizeForPath(const std::string& input) { | 43 std::string SanitizeForPath(const std::string& input) { |
48 std::string result; | 44 std::string result; |
49 ReplaceChars(input, "-", "_", &result); | 45 ReplaceChars(input, "-", "_", &result); |
50 return result; | 46 return result; |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 << arch << " vs " << OmahaQueryParams::getNaclArch() << ")"; | 174 << arch << " vs " << OmahaQueryParams::getNaclArch() << ")"; |
179 return false; | 175 return false; |
180 } | 176 } |
181 | 177 |
182 *version_out = version; | 178 *version_out = version; |
183 return true; | 179 return true; |
184 } | 180 } |
185 | 181 |
186 PnaclComponentInstaller::PnaclComponentInstaller() | 182 PnaclComponentInstaller::PnaclComponentInstaller() |
187 : per_user_(false), | 183 : per_user_(false), |
188 cus_(NULL) { | 184 cus_(NULL), |
| 185 callback_nums_(0) { |
189 #if defined(OS_CHROMEOS) | 186 #if defined(OS_CHROMEOS) |
190 per_user_ = true; | 187 per_user_ = true; |
191 #endif | 188 #endif |
| 189 updater_observer_.reset(new PnaclUpdaterObserver(this)); |
192 } | 190 } |
193 | 191 |
194 PnaclComponentInstaller::~PnaclComponentInstaller() { | 192 PnaclComponentInstaller::~PnaclComponentInstaller() { |
195 } | 193 } |
196 | 194 |
197 void PnaclComponentInstaller::OnUpdateError(int error) { | 195 void PnaclComponentInstaller::OnUpdateError(int error) { |
198 NOTREACHED() << "Pnacl update error: " << error; | 196 NOTREACHED() << "Pnacl update error: " << error; |
199 } | 197 } |
200 | 198 |
201 // Pnacl components have the version encoded in the path itself: | 199 // Pnacl components have the version encoded in the path itself: |
(...skipping 23 matching lines...) Expand all Loading... |
225 // On chromeos, we want to find the --login-profile=<foo> dir. | 223 // On chromeos, we want to find the --login-profile=<foo> dir. |
226 // Even though the path does vary between users, the content | 224 // Even though the path does vary between users, the content |
227 // changes when logging out and logging in. | 225 // changes when logging out and logging in. |
228 ProfileManager* pm = g_browser_process->profile_manager(); | 226 ProfileManager* pm = g_browser_process->profile_manager(); |
229 current_profile_path_ = pm->user_data_dir().Append( | 227 current_profile_path_ = pm->user_data_dir().Append( |
230 pm->GetInitialProfileDir()); | 228 pm->GetInitialProfileDir()); |
231 } | 229 } |
232 | 230 |
233 bool PnaclComponentInstaller::Install(const base::DictionaryValue& manifest, | 231 bool PnaclComponentInstaller::Install(const base::DictionaryValue& manifest, |
234 const base::FilePath& unpack_path) { | 232 const base::FilePath& unpack_path) { |
| 233 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
235 scoped_ptr<base::DictionaryValue> pnacl_manifest( | 234 scoped_ptr<base::DictionaryValue> pnacl_manifest( |
236 ReadPnaclManifest(unpack_path)); | 235 ReadPnaclManifest(unpack_path)); |
237 if (pnacl_manifest == NULL) { | 236 if (pnacl_manifest == NULL) { |
238 LOG(WARNING) << "Failed to read pnacl manifest."; | 237 LOG(WARNING) << "Failed to read pnacl manifest."; |
| 238 NotifyInstallError(); |
239 return false; | 239 return false; |
240 } | 240 } |
241 | 241 |
242 Version version; | 242 Version version; |
243 if (!CheckPnaclComponentManifest(manifest, *pnacl_manifest, &version)) { | 243 if (!CheckPnaclComponentManifest(manifest, *pnacl_manifest, &version)) { |
244 LOG(WARNING) << "CheckPnaclComponentManifest failed, not installing."; | 244 LOG(WARNING) << "CheckPnaclComponentManifest failed, not installing."; |
| 245 NotifyInstallError(); |
245 return false; | 246 return false; |
246 } | 247 } |
247 | 248 |
248 // Don't install if the current version is actually newer. | 249 // Don't install if the current version is actually newer. |
249 if (current_version().CompareTo(version) > 0) | 250 if (current_version().CompareTo(version) > 0) { |
| 251 NotifyInstallError(); |
250 return false; | 252 return false; |
| 253 } |
251 | 254 |
252 // Passed the basic tests. Time to install it. | 255 // Passed the basic tests. Time to install it. |
253 base::FilePath path = GetPnaclBaseDirectory().AppendASCII( | 256 base::FilePath path = GetPnaclBaseDirectory().AppendASCII( |
254 version.GetString()); | 257 version.GetString()); |
255 if (file_util::PathExists(path)) { | 258 if (file_util::PathExists(path)) { |
256 LOG(WARNING) << "Target path already exists, not installing."; | 259 LOG(WARNING) << "Target path already exists, not installing."; |
| 260 NotifyInstallError(); |
257 return false; | 261 return false; |
258 } | 262 } |
259 if (!file_util::Move(unpack_path, path)) { | 263 if (!file_util::Move(unpack_path, path)) { |
260 LOG(WARNING) << "Move failed, not installing."; | 264 LOG(WARNING) << "Move failed, not installing."; |
| 265 NotifyInstallError(); |
261 return false; | 266 return false; |
262 } | 267 } |
263 | 268 |
264 // Installation is done. Now tell the rest of chrome (just the path service | 269 // Installation is done. Now tell the rest of chrome. |
265 // for now). TODO(jvoung): we need notifications if someone surfed to a | 270 // - The path service. |
266 // Pnacl webpage and Pnacl was just installed at this time. They should | 271 // - Callbacks that requested an update. |
267 // then be able to reload the page and retry (or something). | |
268 // See: http://code.google.com/p/chromium/issues/detail?id=107438 | |
269 set_current_version(version); | 272 set_current_version(version); |
270 | 273 NotifyInstallSuccess(); |
271 OverrideDirPnaclComponent(path); | 274 OverrideDirPnaclComponent(path); |
272 return true; | 275 return true; |
273 } | 276 } |
274 | 277 |
275 namespace { | 278 void PnaclComponentInstaller::AddInstallCallback( |
| 279 const InstallCallback& cb) { |
| 280 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 281 int num = ++callback_nums_; |
| 282 install_callbacks_.push_back(std::make_pair(cb, num)); |
| 283 } |
276 | 284 |
277 void DoCheckForUpdate(ComponentUpdateService* cus, | 285 void PnaclComponentInstaller::CancelCallback(int num) { |
278 const CrxComponent& pnacl) { | 286 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
279 if (cus->CheckForUpdateSoon(pnacl) != ComponentUpdateService::kOk) { | 287 for (std::list<std::pair<InstallCallback, int> >::iterator |
280 LOG(WARNING) << "Pnacl check for update failed."; | 288 i = install_callbacks_.begin(), |
| 289 e = install_callbacks_.end(); i != e; ++i) { |
| 290 if (i->second == num) { |
| 291 i->first.Run(false); |
| 292 install_callbacks_.erase(i); |
| 293 return; |
| 294 } |
281 } | 295 } |
282 } | 296 } |
283 | 297 |
284 // Finally, do the registration with the right version number. | 298 void PnaclComponentInstaller::NotifyAllWithResult(bool status) { |
| 299 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 300 while (!install_callbacks_.empty()) { |
| 301 install_callbacks_.front().first.Run(status); |
| 302 install_callbacks_.pop_front(); |
| 303 } |
| 304 } |
| 305 |
| 306 void PnaclComponentInstaller::NotifyInstallError() { |
| 307 if (!install_callbacks_.empty()) { |
| 308 BrowserThread::PostTask( |
| 309 BrowserThread::UI, FROM_HERE, |
| 310 base::Bind(&PnaclComponentInstaller::NotifyAllWithResult, |
| 311 // Unretained because installer lives until process shutdown. |
| 312 base::Unretained(this), false)); |
| 313 } |
| 314 } |
| 315 |
| 316 void PnaclComponentInstaller::NotifyInstallSuccess() { |
| 317 if (!install_callbacks_.empty()) { |
| 318 BrowserThread::PostTask( |
| 319 BrowserThread::UI, FROM_HERE, |
| 320 base::Bind(&PnaclComponentInstaller::NotifyAllWithResult, |
| 321 // Unretained because installer lives until process shutdown. |
| 322 base::Unretained(this), true)); |
| 323 } |
| 324 } |
| 325 |
| 326 namespace { |
| 327 |
285 void FinishPnaclUpdateRegistration(const Version& current_version, | 328 void FinishPnaclUpdateRegistration(const Version& current_version, |
286 PnaclComponentInstaller* pci) { | 329 PnaclComponentInstaller* pci) { |
287 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 330 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
288 CrxComponent pnacl_component; | 331 CrxComponent pnacl_component; |
289 pnacl_component.version = current_version; | 332 pnacl_component.version = current_version; |
290 pnacl_component.name = "pnacl"; | 333 pnacl_component.name = "pnacl"; |
291 pnacl_component.installer = pci; | 334 pnacl_component.installer = pci; |
292 pci->set_current_version(current_version); | 335 pci->set_current_version(current_version); |
293 SetPnaclHash(&pnacl_component); | 336 SetPnaclHash(&pnacl_component); |
294 | 337 |
295 ComponentUpdateService::Status status = | 338 ComponentUpdateService::Status status = |
296 pci->cus()->RegisterComponent(pnacl_component); | 339 pci->cus()->RegisterComponent(pnacl_component); |
297 if (status != ComponentUpdateService::kOk | 340 if (status != ComponentUpdateService::kOk |
298 && status != ComponentUpdateService::kReplaced) { | 341 && status != ComponentUpdateService::kReplaced) { |
299 NOTREACHED() << "Pnacl component registration failed."; | 342 NOTREACHED() << "Pnacl component registration failed."; |
300 } | 343 } |
301 | |
302 // If PNaCl is not yet installed but it is requested by --enable-pnacl, | |
303 // we want it to be available "soon", so kick off an update check | |
304 // earlier than usual. | |
305 Version null_version(kNullVersion); | |
306 if (pci->current_version().Equals(null_version)) { | |
307 BrowserThread::PostDelayedTask( | |
308 BrowserThread::UI, FROM_HERE, | |
309 base::Bind(DoCheckForUpdate, pci->cus(), pnacl_component), | |
310 base::TimeDelta::FromSeconds(kInitialDelaySeconds)); | |
311 } | |
312 } | 344 } |
313 | 345 |
314 // Check if there is an existing version on disk first to know when | 346 // Check if there is an existing version on disk first to know when |
315 // a hosted version is actually newer. | 347 // a hosted version is actually newer. |
316 void StartPnaclUpdateRegistration(PnaclComponentInstaller* pci) { | 348 void StartPnaclUpdateRegistration(PnaclComponentInstaller* pci) { |
317 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 349 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
318 base::FilePath path = pci->GetPnaclBaseDirectory(); | 350 base::FilePath path = pci->GetPnaclBaseDirectory(); |
319 if (!file_util::PathExists(path)) { | 351 if (!file_util::PathExists(path)) { |
320 if (!file_util::CreateDirectory(path)) { | 352 if (!file_util::CreateDirectory(path)) { |
321 NOTREACHED() << "Could not create base Pnacl directory."; | 353 NOTREACHED() << "Could not create base Pnacl directory."; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
366 BrowserThread::FILE, FROM_HERE, | 398 BrowserThread::FILE, FROM_HERE, |
367 base::Bind(&StartPnaclUpdateRegistration, pci)); | 399 base::Bind(&StartPnaclUpdateRegistration, pci)); |
368 } | 400 } |
369 | 401 |
370 | 402 |
371 } // namespace | 403 } // namespace |
372 | 404 |
373 void PnaclComponentInstaller::RegisterPnaclComponent( | 405 void PnaclComponentInstaller::RegisterPnaclComponent( |
374 ComponentUpdateService* cus, | 406 ComponentUpdateService* cus, |
375 const CommandLine& command_line) { | 407 const CommandLine& command_line) { |
376 // Only register when given the right flag. This is important since | 408 // Only register when given the right flag, for now. |
377 // we do an early component updater check above (in DoCheckForUpdate). | |
378 if (command_line.HasSwitch(switches::kEnablePnacl)) { | 409 if (command_line.HasSwitch(switches::kEnablePnacl)) { |
379 cus_ = cus; | 410 cus_ = cus; |
380 // If per_user, create a profile observer to watch for logins. | 411 // If per_user, create a profile observer to watch for logins. |
381 // Only do so after cus_ is set to something non-null. | 412 // Only do so after cus_ is set to something non-null. |
382 if (per_user_ && !profile_observer_) { | 413 if (per_user_ && !profile_observer_) { |
383 profile_observer_.reset(new PnaclProfileObserver(this)); | 414 profile_observer_.reset(new PnaclProfileObserver(this)); |
384 } | 415 } |
385 if (per_user_) { | 416 if (per_user_) { |
386 // Figure out profile information, before proceeding to look for files. | 417 // Figure out profile information, before proceeding to look for files. |
387 BrowserThread::PostTask( | 418 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
388 BrowserThread::UI, FROM_HERE, | 419 base::Bind(&GetProfileInformation, this)); |
389 base::Bind(&GetProfileInformation, this)); | |
390 } else { | 420 } else { |
391 BrowserThread::PostTask( | 421 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
392 BrowserThread::FILE, FROM_HERE, | 422 base::Bind(&StartPnaclUpdateRegistration, this)); |
393 base::Bind(&StartPnaclUpdateRegistration, this)); | |
394 } | 423 } |
395 } | 424 } |
396 } | 425 } |
397 | 426 |
398 void PnaclComponentInstaller::ReRegisterPnacl() { | 427 void PnaclComponentInstaller::ReRegisterPnacl() { |
399 // No need to check the commandline flags again here. | 428 // No need to check the commandline flags again here. |
400 // We could only have gotten here after RegisterPnaclComponent | 429 // We could only have gotten here after RegisterPnaclComponent |
401 // found --enable-pnacl, since that is where we create the profile_observer_, | 430 // found --enable-pnacl, since that is where we create the profile_observer_, |
402 // which in turn calls ReRegisterPnacl. | 431 // which in turn calls ReRegisterPnacl. |
403 DCHECK(per_user_); | 432 DCHECK(per_user_); |
404 // Figure out profile information, before proceeding to look for files. | 433 // Figure out profile information, before proceeding to look for files. |
405 BrowserThread::PostTask( | 434 BrowserThread::PostTask( |
406 BrowserThread::UI, FROM_HERE, | 435 BrowserThread::UI, FROM_HERE, |
407 base::Bind(&GetProfileInformation, this)); | 436 base::Bind(&GetProfileInformation, this)); |
408 } | 437 } |
| 438 |
| 439 void RequestFirstInstall(ComponentUpdateService* cus, |
| 440 PnaclComponentInstaller* pci, |
| 441 const base::Callback<void(bool)>& installed) { |
| 442 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 443 Version null_version(kNullVersion); |
| 444 CrxComponent pnacl_component; |
| 445 pci->set_current_version(null_version); |
| 446 pnacl_component.version = null_version; |
| 447 pnacl_component.name = "pnacl"; |
| 448 pnacl_component.installer = pci; |
| 449 SetPnaclHash(&pnacl_component); |
| 450 ComponentUpdateService::Status status = cus->CheckForUpdateSoon( |
| 451 pnacl_component); |
| 452 if (status != ComponentUpdateService::kOk) { |
| 453 installed.Run(false); |
| 454 return; |
| 455 } |
| 456 pci->AddInstallCallback(installed); |
| 457 } |
OLD | NEW |