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/extensions/crx_installer.h" | 5 #include "chrome/browser/extensions/crx_installer.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <set> | 8 #include <set> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
12 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
13 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
14 #include "base/metrics/histogram.h" | 14 #include "base/metrics/histogram.h" |
15 #include "base/path_service.h" | 15 #include "base/path_service.h" |
16 #include "base/sequenced_task_runner.h" | 16 #include "base/sequenced_task_runner.h" |
17 #include "base/string_util.h" | 17 #include "base/string_util.h" |
18 #include "base/stringprintf.h" | 18 #include "base/stringprintf.h" |
| 19 #include "base/threading/sequenced_worker_pool.h" |
19 #include "base/threading/thread_restrictions.h" | 20 #include "base/threading/thread_restrictions.h" |
20 #include "base/time.h" | 21 #include "base/time.h" |
21 #include "base/threading/sequenced_worker_pool.h" | |
22 #include "base/utf_string_conversions.h" | 22 #include "base/utf_string_conversions.h" |
23 #include "base/version.h" | 23 #include "base/version.h" |
24 #include "chrome/browser/browser_process.h" | 24 #include "chrome/browser/browser_process.h" |
25 #include "chrome/browser/extensions/convert_user_script.h" | 25 #include "chrome/browser/extensions/convert_user_script.h" |
26 #include "chrome/browser/extensions/convert_web_app.h" | 26 #include "chrome/browser/extensions/convert_web_app.h" |
27 #include "chrome/browser/extensions/extension_error_reporter.h" | 27 #include "chrome/browser/extensions/extension_error_reporter.h" |
28 #include "chrome/browser/extensions/extension_install_ui.h" | 28 #include "chrome/browser/extensions/extension_install_ui.h" |
29 #include "chrome/browser/extensions/extension_service.h" | 29 #include "chrome/browser/extensions/extension_service.h" |
30 #include "chrome/browser/extensions/extension_system.h" | 30 #include "chrome/browser/extensions/extension_system.h" |
31 #include "chrome/browser/extensions/management_policy.h" | 31 #include "chrome/browser/extensions/management_policy.h" |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 install_source_(Manifest::INTERNAL), | 92 install_source_(Manifest::INTERNAL), |
93 approved_(false), | 93 approved_(false), |
94 extensions_enabled_(frontend_weak->extensions_enabled()), | 94 extensions_enabled_(frontend_weak->extensions_enabled()), |
95 delete_source_(false), | 95 delete_source_(false), |
96 create_app_shortcut_(false), | 96 create_app_shortcut_(false), |
97 frontend_weak_(frontend_weak), | 97 frontend_weak_(frontend_weak), |
98 profile_(frontend_weak->profile()), | 98 profile_(frontend_weak->profile()), |
99 client_(client), | 99 client_(client), |
100 apps_require_extension_mime_type_(false), | 100 apps_require_extension_mime_type_(false), |
101 allow_silent_install_(false), | 101 allow_silent_install_(false), |
| 102 bypass_blacklist_for_test_(false), |
102 install_cause_(extension_misc::INSTALL_CAUSE_UNSET), | 103 install_cause_(extension_misc::INSTALL_CAUSE_UNSET), |
103 creation_flags_(Extension::NO_FLAGS), | 104 creation_flags_(Extension::NO_FLAGS), |
104 off_store_install_allow_reason_(OffStoreInstallDisallowed), | 105 off_store_install_allow_reason_(OffStoreInstallDisallowed), |
105 did_handle_successfully_(true), | 106 did_handle_successfully_(true), |
106 record_oauth2_grant_(false), | 107 record_oauth2_grant_(false), |
107 error_on_unsupported_requirements_(false), | 108 error_on_unsupported_requirements_(false), |
108 requirements_checker_(new extensions::RequirementsChecker()), | 109 requirements_checker_(new extensions::RequirementsChecker()), |
109 has_requirement_errors_(false), | 110 has_requirement_errors_(false), |
110 install_wait_for_idle_(true) { | 111 install_wait_for_idle_(true) { |
111 installer_task_runner_ = frontend_weak->GetFileTaskRunner(); | 112 installer_task_runner_ = frontend_weak->GetFileTaskRunner(); |
(...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
479 ExtensionService::RecordPermissionMessagesHistogram( | 480 ExtensionService::RecordPermissionMessagesHistogram( |
480 extension_, histogram_name.c_str()); | 481 extension_, histogram_name.c_str()); |
481 | 482 |
482 // Kill the theme loading bubble. | 483 // Kill the theme loading bubble. |
483 content::NotificationService* service = | 484 content::NotificationService* service = |
484 content::NotificationService::current(); | 485 content::NotificationService::current(); |
485 service->Notify(chrome::NOTIFICATION_NO_THEME_DETECTED, | 486 service->Notify(chrome::NOTIFICATION_NO_THEME_DETECTED, |
486 content::Source<CrxInstaller>(this), | 487 content::Source<CrxInstaller>(this), |
487 content::NotificationService::NoDetails()); | 488 content::NotificationService::NoDetails()); |
488 | 489 |
489 NotifyCrxInstallComplete(NULL); | 490 NotifyCrxInstallComplete(false); |
490 | 491 |
491 Release(); // balanced in ConfirmInstall(). | 492 Release(); // balanced in ConfirmInstall(). |
492 | 493 |
493 // We're done. Since we don't post any more tasks to ourself, our ref count | 494 // We're done. Since we don't post any more tasks to ourself, our ref count |
494 // should go to zero and we die. The destructor will clean up the temp dir. | 495 // should go to zero and we die. The destructor will clean up the temp dir. |
495 } | 496 } |
496 | 497 |
497 void CrxInstaller::CompleteInstall() { | 498 void CrxInstaller::CompleteInstall() { |
498 DCHECK(installer_task_runner_->RunsTasksOnCurrentThread()); | 499 DCHECK(installer_task_runner_->RunsTasksOnCurrentThread()); |
499 | 500 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
540 std::string error; | 541 std::string error; |
541 extension_ = extension_file_util::LoadExtension( | 542 extension_ = extension_file_util::LoadExtension( |
542 version_dir, | 543 version_dir, |
543 install_source_, | 544 install_source_, |
544 extension_->creation_flags() | Extension::REQUIRE_KEY, | 545 extension_->creation_flags() | Extension::REQUIRE_KEY, |
545 &error); | 546 &error); |
546 | 547 |
547 if (extension_) { | 548 if (extension_) { |
548 ReportSuccessFromFileThread(); | 549 ReportSuccessFromFileThread(); |
549 } else { | 550 } else { |
550 LOG(ERROR) << error << " " << extension_id << " " << download_url_.spec(); | 551 LOG(ERROR) << error << " " << extension_id << " " << download_url_; |
551 ReportFailureFromFileThread(CrxInstallerError(UTF8ToUTF16(error))); | 552 ReportFailureFromFileThread(CrxInstallerError(UTF8ToUTF16(error))); |
552 } | 553 } |
553 | 554 |
554 } | 555 } |
555 | 556 |
556 void CrxInstaller::ReportFailureFromFileThread(const CrxInstallerError& error) { | 557 void CrxInstaller::ReportFailureFromFileThread(const CrxInstallerError& error) { |
557 DCHECK(installer_task_runner_->RunsTasksOnCurrentThread()); | 558 DCHECK(installer_task_runner_->RunsTasksOnCurrentThread()); |
558 if (!BrowserThread::PostTask( | 559 if (!BrowserThread::PostTask( |
559 BrowserThread::UI, FROM_HERE, | 560 BrowserThread::UI, FROM_HERE, |
560 base::Bind(&CrxInstaller::ReportFailureFromUIThread, this, error))) { | 561 base::Bind(&CrxInstaller::ReportFailureFromUIThread, this, error))) { |
(...skipping 14 matching lines...) Expand all Loading... |
575 // see errors get reported via this interface. | 576 // see errors get reported via this interface. |
576 // | 577 // |
577 // TODO(aa): Need to go through unit tests and clean them up too, probably get | 578 // TODO(aa): Need to go through unit tests and clean them up too, probably get |
578 // rid of this line. | 579 // rid of this line. |
579 ExtensionErrorReporter::GetInstance()->ReportError( | 580 ExtensionErrorReporter::GetInstance()->ReportError( |
580 error.message(), false); // quiet | 581 error.message(), false); // quiet |
581 | 582 |
582 if (client_) | 583 if (client_) |
583 client_->OnInstallFailure(error); | 584 client_->OnInstallFailure(error); |
584 | 585 |
585 NotifyCrxInstallComplete(NULL); | 586 NotifyCrxInstallComplete(false); |
586 | 587 |
587 // Delete temporary files. | 588 // Delete temporary files. |
588 CleanupTempFiles(); | 589 CleanupTempFiles(); |
589 } | 590 } |
590 | 591 |
591 void CrxInstaller::ReportSuccessFromFileThread() { | 592 void CrxInstaller::ReportSuccessFromFileThread() { |
592 DCHECK(installer_task_runner_->RunsTasksOnCurrentThread()); | 593 DCHECK(installer_task_runner_->RunsTasksOnCurrentThread()); |
593 | 594 |
594 // Tracking number of extensions installed by users | 595 // Tracking number of extensions installed by users |
595 if (install_cause() == extension_misc::INSTALL_CAUSE_USER_DOWNLOAD) | 596 if (install_cause() == extension_misc::INSTALL_CAUSE_USER_DOWNLOAD) |
(...skipping 23 matching lines...) Expand all Loading... |
619 record_oauth2_grant_ = client_->record_oauth2_grant(); | 620 record_oauth2_grant_ = client_->record_oauth2_grant(); |
620 | 621 |
621 // We update the extension's granted permissions if the user already approved | 622 // We update the extension's granted permissions if the user already approved |
622 // the install (client_ is non NULL), or we are allowed to install this | 623 // the install (client_ is non NULL), or we are allowed to install this |
623 // silently. | 624 // silently. |
624 if (client_ || allow_silent_install_) { | 625 if (client_ || allow_silent_install_) { |
625 PermissionsUpdater perms_updater(profile()); | 626 PermissionsUpdater perms_updater(profile()); |
626 perms_updater.GrantActivePermissions(extension_, record_oauth2_grant_); | 627 perms_updater.GrantActivePermissions(extension_, record_oauth2_grant_); |
627 } | 628 } |
628 | 629 |
629 // Tell the frontend about the installation and hand off ownership of | 630 // Install the extension if it's not blacklisted, but notify either way. |
630 // extension_ to it. | 631 base::Closure on_success = |
631 frontend_weak_->OnExtensionInstalled(extension_, | 632 base::Bind(&ExtensionService::OnExtensionInstalled, |
632 page_ordinal_, | 633 frontend_weak_, |
633 has_requirement_errors_, | 634 extension_, |
634 install_wait_for_idle_); | 635 page_ordinal_, |
635 | 636 has_requirement_errors_, |
636 NotifyCrxInstallComplete(extension_.get()); | 637 install_wait_for_idle_); |
637 | 638 if (bypass_blacklist_for_test_) { |
638 extension_ = NULL; | 639 HandleIsBlacklistedResponse(on_success, false); |
639 | 640 } else { |
640 // We're done. We don't post any more tasks to ourselves so we are deleted | 641 ExtensionSystem::Get(profile_)->blacklist()->IsBlacklisted( |
641 // soon. | 642 extension_->id(), |
| 643 base::Bind(&CrxInstaller::HandleIsBlacklistedResponse, |
| 644 this, |
| 645 on_success)); |
| 646 } |
642 } | 647 } |
643 | 648 |
644 void CrxInstaller::NotifyCrxInstallComplete(const Extension* extension) { | 649 void CrxInstaller::HandleIsBlacklistedResponse( |
| 650 const base::Closure& on_success, |
| 651 bool is_blacklisted) { |
| 652 if (is_blacklisted) { |
| 653 string16 error = |
| 654 l10n_util::GetStringFUTF16(IDS_EXTENSION_IS_BLACKLISTED, |
| 655 UTF8ToUTF16(extension_->name())); |
| 656 make_scoped_ptr(ExtensionInstallUI::Create(profile()))->OnInstallFailure( |
| 657 extensions::CrxInstallerError(error)); |
| 658 // Show error via reporter to make tests happy. |
| 659 ExtensionErrorReporter::GetInstance()->ReportError(error, false); // quiet |
| 660 } else { |
| 661 on_success.Run(); |
| 662 } |
| 663 NotifyCrxInstallComplete(!is_blacklisted); |
| 664 } |
| 665 |
| 666 void CrxInstaller::NotifyCrxInstallComplete(bool success) { |
645 // Some users (such as the download shelf) need to know when a | 667 // Some users (such as the download shelf) need to know when a |
646 // CRXInstaller is done. Listening for the EXTENSION_* events | 668 // CRXInstaller is done. Listening for the EXTENSION_* events |
647 // is problematic because they don't know anything about the | 669 // is problematic because they don't know anything about the |
648 // extension before it is unpacked, so they cannot filter based | 670 // extension before it is unpacked, so they cannot filter based |
649 // on the extension. | 671 // on the extension. |
650 content::NotificationService::current()->Notify( | 672 content::NotificationService::current()->Notify( |
651 chrome::NOTIFICATION_CRX_INSTALLER_DONE, | 673 chrome::NOTIFICATION_CRX_INSTALLER_DONE, |
652 content::Source<CrxInstaller>(this), | 674 content::Source<CrxInstaller>(this), |
653 content::Details<const Extension>(extension)); | 675 content::Details<const Extension>(success ? extension_.get() : NULL)); |
| 676 |
| 677 // We're done. We don't post any more tasks to ourselves so we are deleted |
| 678 // soon. |
| 679 extension_ = NULL; |
654 } | 680 } |
655 | 681 |
656 void CrxInstaller::CleanupTempFiles() { | 682 void CrxInstaller::CleanupTempFiles() { |
657 if (!installer_task_runner_->RunsTasksOnCurrentThread()) { | 683 if (!installer_task_runner_->RunsTasksOnCurrentThread()) { |
658 if (!installer_task_runner_->PostTask( | 684 if (!installer_task_runner_->PostTask( |
659 FROM_HERE, | 685 FROM_HERE, |
660 base::Bind(&CrxInstaller::CleanupTempFiles, this))) { | 686 base::Bind(&CrxInstaller::CleanupTempFiles, this))) { |
661 NOTREACHED(); | 687 NOTREACHED(); |
662 } | 688 } |
663 return; | 689 return; |
664 } | 690 } |
665 | 691 |
666 // Delete the temp directory and crx file as necessary. | 692 // Delete the temp directory and crx file as necessary. |
667 if (!temp_dir_.value().empty()) { | 693 if (!temp_dir_.value().empty()) { |
668 extension_file_util::DeleteFile(temp_dir_, true); | 694 extension_file_util::DeleteFile(temp_dir_, true); |
669 temp_dir_ = base::FilePath(); | 695 temp_dir_ = base::FilePath(); |
670 } | 696 } |
671 | 697 |
672 if (delete_source_ && !source_file_.value().empty()) { | 698 if (delete_source_ && !source_file_.value().empty()) { |
673 extension_file_util::DeleteFile(source_file_, false); | 699 extension_file_util::DeleteFile(source_file_, false); |
674 source_file_ = base::FilePath(); | 700 source_file_ = base::FilePath(); |
675 } | 701 } |
676 } | 702 } |
677 | 703 |
678 } // namespace extensions | 704 } // namespace extensions |
OLD | NEW |