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.
5 #include "chrome/browser/extensions/crx_installer.h"
10 #include "base/bind.h"
11 #include "base/files/file_util.h"
12 #include "base/files/scoped_temp_dir.h"
13 #include "base/lazy_instance.h"
14 #include "base/metrics/histogram.h"
15 #include "base/sequenced_task_runner.h"
16 #include "base/strings/string_util.h"
17 #include "base/strings/stringprintf.h"
18 #include "base/strings/utf_string_conversions.h"
19 #include "base/threading/sequenced_worker_pool.h"
20 #include "base/threading/thread_restrictions.h"
21 #include "base/time/time.h"
22 #include "base/version.h"
23 #include "chrome/browser/extensions/convert_user_script.h"
24 #include "chrome/browser/extensions/convert_web_app.h"
25 #include "chrome/browser/extensions/extension_assets_manager.h"
26 #include "chrome/browser/extensions/extension_error_reporter.h"
27 #include "chrome/browser/extensions/extension_service.h"
28 #include "chrome/browser/extensions/install_tracker.h"
29 #include "chrome/browser/extensions/install_tracker_factory.h"
30 #include "chrome/browser/extensions/permissions_updater.h"
31 #include "chrome/browser/extensions/webstore_installer.h"
32 #include "chrome/browser/profiles/profile.h"
33 #include "chrome/browser/web_applications/web_app.h"
34 #include "chrome/common/chrome_paths.h"
35 #include "chrome/common/extensions/extension_constants.h"
36 #include "chrome/grit/generated_resources.h"
37 #include "content/public/browser/browser_thread.h"
38 #include "content/public/browser/notification_service.h"
39 #include "content/public/browser/resource_dispatcher_host.h"
40 #include "content/public/browser/user_metrics.h"
41 #include "extensions/browser/extension_prefs.h"
42 #include "extensions/browser/extension_registry.h"
43 #include "extensions/browser/extension_system.h"
44 #include "extensions/browser/install/crx_install_error.h"
45 #include "extensions/browser/install/extension_install_ui.h"
46 #include "extensions/browser/install_flag.h"
47 #include "extensions/browser/notification_types.h"
48 #include "extensions/common/extension_icon_set.h"
49 #include "extensions/common/feature_switch.h"
50 #include "extensions/common/file_util.h"
51 #include "extensions/common/manifest.h"
52 #include "extensions/common/manifest_handlers/kiosk_mode_info.h"
53 #include "extensions/common/manifest_handlers/shared_module_info.h"
54 #include "extensions/common/manifest_url_handlers.h"
55 #include "extensions/common/permissions/permission_message_provider.h"
56 #include "extensions/common/permissions/permission_set.h"
57 #include "extensions/common/permissions/permissions_data.h"
58 #include "extensions/common/user_script.h"
59 #include "grit/extensions_strings.h"
60 #include "third_party/skia/include/core/SkBitmap.h"
61 #include "ui/base/l10n/l10n_util.h"
63 #if defined(OS_CHROMEOS)
64 #include "components/user_manager/user_manager.h"
67 using base::UserMetricsAction
;
68 using content::BrowserThread
;
69 using extensions::SharedModuleInfo
;
71 namespace extensions
{
75 // Used in histograms; do not change order.
76 enum OffStoreInstallDecision
{
78 OffStoreInstallAllowed
,
79 OffStoreInstallDisallowed
,
80 NumOffStoreInstallDecision
86 scoped_refptr
<CrxInstaller
> CrxInstaller::CreateSilent(
87 ExtensionService
* frontend
) {
88 return new CrxInstaller(frontend
->AsWeakPtr(),
89 scoped_ptr
<ExtensionInstallPrompt
>(),
94 scoped_refptr
<CrxInstaller
> CrxInstaller::Create(
95 ExtensionService
* frontend
,
96 scoped_ptr
<ExtensionInstallPrompt
> client
) {
97 return new CrxInstaller(frontend
->AsWeakPtr(), client
.Pass(), NULL
);
101 scoped_refptr
<CrxInstaller
> CrxInstaller::Create(
102 ExtensionService
* service
,
103 scoped_ptr
<ExtensionInstallPrompt
> client
,
104 const WebstoreInstaller::Approval
* approval
) {
105 return new CrxInstaller(service
->AsWeakPtr(), client
.Pass(), approval
);
108 CrxInstaller::CrxInstaller(base::WeakPtr
<ExtensionService
> service_weak
,
109 scoped_ptr
<ExtensionInstallPrompt
> client
,
110 const WebstoreInstaller::Approval
* approval
)
111 : install_directory_(service_weak
->install_directory()),
112 install_source_(Manifest::INTERNAL
),
114 hash_check_failed_(false),
115 expected_manifest_check_level_(
116 WebstoreInstaller::MANIFEST_CHECK_LEVEL_STRICT
),
117 expected_version_strict_checking_(false),
118 extensions_enabled_(service_weak
->extensions_enabled()),
119 delete_source_(false),
120 create_app_shortcut_(false),
121 service_weak_(service_weak
),
122 // See header file comment on |client_| for why we use a raw pointer here.
123 client_(client
.release()),
124 apps_require_extension_mime_type_(false),
125 allow_silent_install_(false),
126 grant_permissions_(true),
127 install_cause_(extension_misc::INSTALL_CAUSE_UNSET
),
128 creation_flags_(Extension::NO_FLAGS
),
129 off_store_install_allow_reason_(OffStoreInstallDisallowed
),
130 did_handle_successfully_(true),
131 error_on_unsupported_requirements_(false),
132 update_from_settings_page_(false),
133 install_flags_(kInstallFlagNone
),
134 install_checker_(service_weak
->profile()) {
135 installer_task_runner_
= service_weak
->GetFileTaskRunner();
139 CHECK(profile()->IsSameProfile(approval
->profile
));
141 client_
->install_ui()->SetUseAppInstalledBubble(
142 approval
->use_app_installed_bubble
);
143 client_
->install_ui()->SetSkipPostInstallUI(approval
->skip_post_install_ui
);
146 if (approval
->skip_install_dialog
) {
147 // Mark the extension as approved, but save the expected manifest and ID
148 // so we can check that they match the CRX's.
150 expected_manifest_check_level_
= approval
->manifest_check_level
;
151 if (expected_manifest_check_level_
!=
152 WebstoreInstaller::MANIFEST_CHECK_LEVEL_NONE
)
153 expected_manifest_
.reset(approval
->manifest
->DeepCopy());
154 expected_id_
= approval
->extension_id
;
156 if (approval
->minimum_version
.get()) {
157 expected_version_
.reset(new Version(*approval
->minimum_version
));
158 expected_version_strict_checking_
= false;
161 show_dialog_callback_
= approval
->show_dialog_callback
;
162 set_is_ephemeral(approval
->is_ephemeral
);
165 CrxInstaller::~CrxInstaller() {
166 // Make sure the UI is deleted on the ui thread.
168 BrowserThread::DeleteSoon(BrowserThread::UI
, FROM_HERE
, client_
);
173 void CrxInstaller::InstallCrx(const base::FilePath
& source_file
) {
174 InstallCrxFile(CRXFileInfo(source_file
));
177 void CrxInstaller::InstallCrxFile(const CRXFileInfo
& source_file
) {
178 ExtensionService
* service
= service_weak_
.get();
179 if (!service
|| service
->browser_terminating())
182 NotifyCrxInstallBegin();
184 source_file_
= source_file
.path
;
186 scoped_refptr
<SandboxedUnpacker
> unpacker(
187 new SandboxedUnpacker(source_file
,
191 installer_task_runner_
.get(),
194 if (!installer_task_runner_
->PostTask(
196 base::Bind(&SandboxedUnpacker::Start
, unpacker
.get())))
200 void CrxInstaller::InstallUserScript(const base::FilePath
& source_file
,
201 const GURL
& download_url
) {
202 DCHECK(!download_url
.is_empty());
204 NotifyCrxInstallBegin();
206 source_file_
= source_file
;
207 download_url_
= download_url
;
209 if (!installer_task_runner_
->PostTask(
211 base::Bind(&CrxInstaller::ConvertUserScriptOnFileThread
, this)))
215 void CrxInstaller::ConvertUserScriptOnFileThread() {
216 base::string16 error
;
217 scoped_refptr
<Extension
> extension
= ConvertUserScriptToExtension(
218 source_file_
, download_url_
, install_directory_
, &error
);
219 if (!extension
.get()) {
220 ReportFailureFromFileThread(CrxInstallError(error
));
224 OnUnpackSuccess(extension
->path(), extension
->path(), NULL
, extension
.get(),
228 void CrxInstaller::InstallWebApp(const WebApplicationInfo
& web_app
) {
229 NotifyCrxInstallBegin();
231 if (!installer_task_runner_
->PostTask(
233 base::Bind(&CrxInstaller::ConvertWebAppOnFileThread
, this, web_app
)))
237 void CrxInstaller::ConvertWebAppOnFileThread(
238 const WebApplicationInfo
& web_app
) {
239 scoped_refptr
<Extension
> extension(ConvertWebAppToExtension(
240 web_app
, base::Time::Now(), install_directory_
));
241 if (!extension
.get()) {
242 // Validation should have stopped any potential errors before getting here.
243 NOTREACHED() << "Could not convert web app to extension.";
247 // TODO(aa): conversion data gets lost here :(
249 OnUnpackSuccess(extension
->path(), extension
->path(), NULL
, extension
.get(),
253 CrxInstallError
CrxInstaller::AllowInstall(const Extension
* extension
) {
254 DCHECK(installer_task_runner_
->RunsTasksOnCurrentThread());
256 // Make sure the expected ID matches if one was supplied or if we want to
257 // bypass the prompt.
258 if ((approved_
|| !expected_id_
.empty()) &&
259 expected_id_
!= extension
->id()) {
260 return CrxInstallError(l10n_util::GetStringFUTF16(
261 IDS_EXTENSION_INSTALL_UNEXPECTED_ID
, base::ASCIIToUTF16(expected_id_
),
262 base::ASCIIToUTF16(extension
->id())));
265 if (expected_version_
.get()) {
266 if (expected_version_strict_checking_
) {
267 if (!expected_version_
->Equals(*extension
->version())) {
268 return CrxInstallError(l10n_util::GetStringFUTF16(
269 IDS_EXTENSION_INSTALL_UNEXPECTED_VERSION
,
270 base::ASCIIToUTF16(expected_version_
->GetString()),
271 base::ASCIIToUTF16(extension
->version()->GetString())));
274 if (extension
->version()->CompareTo(*expected_version_
) < 0) {
275 return CrxInstallError(l10n_util::GetStringFUTF16(
276 IDS_EXTENSION_INSTALL_UNEXPECTED_VERSION
,
277 base::ASCIIToUTF16(expected_version_
->GetString() + "+"),
278 base::ASCIIToUTF16(extension
->version()->GetString())));
283 // Make sure the manifests match if we want to bypass the prompt.
286 if (expected_manifest_check_level_
==
287 WebstoreInstaller::MANIFEST_CHECK_LEVEL_NONE
) {
288 // To skip manifest checking, the extension must be a shared module
289 // and not request any permissions.
290 if (SharedModuleInfo::IsSharedModule(extension
) &&
291 extension
->permissions_data()->active_permissions()->IsEmpty()) {
295 valid
= expected_manifest_
->Equals(original_manifest_
.get());
296 if (!valid
&& expected_manifest_check_level_
==
297 WebstoreInstaller::MANIFEST_CHECK_LEVEL_LOOSE
) {
299 scoped_refptr
<Extension
> dummy_extension
=
300 Extension::Create(base::FilePath(),
302 *expected_manifest_
->value(),
307 scoped_refptr
<const PermissionSet
> expected_permissions
=
308 dummy_extension
->permissions_data()->active_permissions();
309 valid
= !(PermissionMessageProvider::Get()->IsPrivilegeIncrease(
310 expected_permissions
.get(),
311 extension
->permissions_data()->active_permissions().get(),
312 extension
->GetType()));
318 return CrxInstallError(
319 l10n_util::GetStringUTF16(IDS_EXTENSION_MANIFEST_INVALID
));
322 // The checks below are skipped for themes and external installs.
323 // TODO(pamg): After ManagementPolicy refactoring is complete, remove this
324 // and other uses of install_source_ that are no longer needed now that the
325 // SandboxedUnpacker sets extension->location.
326 if (extension
->is_theme() || Manifest::IsExternalLocation(install_source_
))
327 return CrxInstallError();
329 if (!extensions_enabled_
) {
330 return CrxInstallError(
331 CrxInstallError::ERROR_DECLINED
,
332 l10n_util::GetStringUTF16(IDS_EXTENSION_INSTALL_NOT_ENABLED
));
335 if (install_cause_
== extension_misc::INSTALL_CAUSE_USER_DOWNLOAD
) {
336 if (FeatureSwitch::easy_off_store_install()->IsEnabled()) {
337 const char kHistogramName
[] = "Extensions.OffStoreInstallDecisionEasy";
338 if (is_gallery_install()) {
339 UMA_HISTOGRAM_ENUMERATION(kHistogramName
, OnStoreInstall
,
340 NumOffStoreInstallDecision
);
342 UMA_HISTOGRAM_ENUMERATION(kHistogramName
, OffStoreInstallAllowed
,
343 NumOffStoreInstallDecision
);
346 const char kHistogramName
[] = "Extensions.OffStoreInstallDecisionHard";
347 if (is_gallery_install()) {
348 UMA_HISTOGRAM_ENUMERATION(kHistogramName
, OnStoreInstall
,
349 NumOffStoreInstallDecision
);
350 } else if (off_store_install_allow_reason_
!= OffStoreInstallDisallowed
) {
351 UMA_HISTOGRAM_ENUMERATION(kHistogramName
, OffStoreInstallAllowed
,
352 NumOffStoreInstallDecision
);
353 UMA_HISTOGRAM_ENUMERATION("Extensions.OffStoreInstallAllowReason",
354 off_store_install_allow_reason_
,
355 NumOffStoreInstallAllowReasons
);
357 UMA_HISTOGRAM_ENUMERATION(kHistogramName
, OffStoreInstallDisallowed
,
358 NumOffStoreInstallDecision
);
359 // Don't delete source in this case so that the user can install
360 // manually if they want.
361 delete_source_
= false;
362 did_handle_successfully_
= false;
364 return CrxInstallError(CrxInstallError::ERROR_OFF_STORE
,
365 l10n_util::GetStringUTF16(
366 IDS_EXTENSION_INSTALL_DISALLOWED_ON_SITE
));
371 if (install_checker_
.extension()->is_app()) {
372 // If the app was downloaded, apps_require_extension_mime_type_
373 // will be set. In this case, check that it was served with the
374 // right mime type. Make an exception for file URLs, which come
375 // from the users computer and have no headers.
376 if (!download_url_
.SchemeIsFile() &&
377 apps_require_extension_mime_type_
&&
378 original_mime_type_
!= Extension::kMimeType
) {
379 return CrxInstallError(l10n_util::GetStringFUTF16(
380 IDS_EXTENSION_INSTALL_INCORRECT_APP_CONTENT_TYPE
,
381 base::ASCIIToUTF16(Extension::kMimeType
)));
384 // If the client_ is NULL, then the app is either being installed via
385 // an internal mechanism like sync, external_extensions, or default apps.
386 // In that case, we don't want to enforce things like the install origin.
387 if (!is_gallery_install() && client_
) {
388 // For apps with a gallery update URL, require that they be installed
390 // TODO(erikkay) Apply this rule for paid extensions and themes as well.
391 if (ManifestURL::UpdatesFromGallery(extension
)) {
392 return CrxInstallError(l10n_util::GetStringFUTF16(
393 IDS_EXTENSION_DISALLOW_NON_DOWNLOADED_GALLERY_INSTALLS
,
394 l10n_util::GetStringUTF16(IDS_EXTENSION_WEB_STORE_TITLE
)));
397 // For self-hosted apps, verify that the entire extent is on the same
398 // host (or a subdomain of the host) the download happened from. There's
399 // no way for us to verify that the app controls any other hosts.
400 URLPattern
pattern(UserScript::ValidUserScriptSchemes());
401 pattern
.SetHost(download_url_
.host());
402 pattern
.SetMatchSubdomains(true);
404 URLPatternSet patterns
= install_checker_
.extension()->web_extent();
405 for (URLPatternSet::const_iterator i
= patterns
.begin();
406 i
!= patterns
.end(); ++i
) {
407 if (!pattern
.MatchesHost(i
->host())) {
408 return CrxInstallError(l10n_util::GetStringUTF16(
409 IDS_EXTENSION_INSTALL_INCORRECT_INSTALL_HOST
));
415 return CrxInstallError();
418 void CrxInstaller::OnUnpackFailure(const CrxInstallError
& error
) {
419 DCHECK(installer_task_runner_
->RunsTasksOnCurrentThread());
421 UMA_HISTOGRAM_ENUMERATION("Extensions.UnpackFailureInstallSource",
422 install_source(), Manifest::NUM_LOCATIONS
);
424 UMA_HISTOGRAM_ENUMERATION("Extensions.UnpackFailureInstallCause",
426 extension_misc::NUM_INSTALL_CAUSES
);
428 ReportFailureFromFileThread(error
);
431 void CrxInstaller::OnUnpackSuccess(
432 const base::FilePath
& temp_dir
,
433 const base::FilePath
& extension_dir
,
434 const base::DictionaryValue
* original_manifest
,
435 const Extension
* extension
,
436 const SkBitmap
& install_icon
) {
437 DCHECK(installer_task_runner_
->RunsTasksOnCurrentThread());
439 UMA_HISTOGRAM_ENUMERATION("Extensions.UnpackSuccessInstallSource",
440 install_source(), Manifest::NUM_LOCATIONS
);
443 UMA_HISTOGRAM_ENUMERATION("Extensions.UnpackSuccessInstallCause",
445 extension_misc::NUM_INSTALL_CAUSES
);
447 install_checker_
.set_extension(extension
);
448 temp_dir_
= temp_dir
;
449 if (!install_icon
.empty())
450 install_icon_
.reset(new SkBitmap(install_icon
));
452 if (original_manifest
)
453 original_manifest_
.reset(new Manifest(
454 Manifest::INVALID_LOCATION
,
455 scoped_ptr
<base::DictionaryValue
>(original_manifest
->DeepCopy())));
457 // We don't have to delete the unpack dir explicity since it is a child of
459 unpacked_extension_root_
= extension_dir
;
461 CrxInstallError error
= AllowInstall(extension
);
462 if (error
.type() != CrxInstallError::ERROR_NONE
) {
463 ReportFailureFromFileThread(error
);
467 if (!BrowserThread::PostTask(BrowserThread::UI
,
469 base::Bind(&CrxInstaller::CheckInstall
, this)))
473 void CrxInstaller::CheckInstall() {
474 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
475 ExtensionService
* service
= service_weak_
.get();
476 if (!service
|| service
->browser_terminating())
479 // TODO(crbug.com/420147): Move this code to a utility class to avoid
480 // duplication of SharedModuleService::CheckImports code.
481 if (SharedModuleInfo::ImportsModules(extension())) {
482 const std::vector
<SharedModuleInfo::ImportInfo
>& imports
=
483 SharedModuleInfo::GetImports(extension());
484 std::vector
<SharedModuleInfo::ImportInfo
>::const_iterator i
;
485 for (i
= imports
.begin(); i
!= imports
.end(); ++i
) {
486 Version
version_required(i
->minimum_version
);
487 const Extension
* imported_module
=
488 service
->GetExtensionById(i
->extension_id
, true);
489 if (imported_module
&&
490 !SharedModuleInfo::IsSharedModule(imported_module
)) {
491 ReportFailureFromUIThread(CrxInstallError(
492 CrxInstallError::ERROR_DECLINED
,
493 l10n_util::GetStringFUTF16(
494 IDS_EXTENSION_INSTALL_DEPENDENCY_NOT_SHARED_MODULE
,
495 base::UTF8ToUTF16(imported_module
->name()))));
497 } else if (imported_module
&& (version_required
.IsValid() &&
498 imported_module
->version()->CompareTo(
499 version_required
) < 0)) {
500 ReportFailureFromUIThread(CrxInstallError(
501 CrxInstallError::ERROR_DECLINED
,
502 l10n_util::GetStringFUTF16(
503 IDS_EXTENSION_INSTALL_DEPENDENCY_OLD_VERSION
,
504 base::UTF8ToUTF16(imported_module
->name()),
505 base::ASCIIToUTF16(i
->minimum_version
),
506 base::ASCIIToUTF16(imported_module
->version()->GetString()))));
508 } else if (imported_module
&&
509 !SharedModuleInfo::IsExportAllowedByWhitelist(
510 imported_module
, extension()->id())) {
511 ReportFailureFromUIThread(CrxInstallError(
512 CrxInstallError::ERROR_DECLINED
,
513 l10n_util::GetStringFUTF16(
514 IDS_EXTENSION_INSTALL_DEPENDENCY_NOT_WHITELISTED
,
515 base::UTF8ToUTF16(extension()->name()),
516 base::UTF8ToUTF16(imported_module
->name()))));
522 // Run the policy, requirements and blacklist checks in parallel.
523 install_checker_
.Start(
524 ExtensionInstallChecker::CHECK_ALL
,
525 false /* fail fast */,
526 base::Bind(&CrxInstaller::OnInstallChecksComplete
, this));
529 void CrxInstaller::OnInstallChecksComplete(int failed_checks
) {
530 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
534 // Check for requirement errors.
535 if (!install_checker_
.requirement_errors().empty()) {
536 if (error_on_unsupported_requirements_
) {
537 ReportFailureFromUIThread(
538 CrxInstallError(CrxInstallError::ERROR_DECLINED
,
539 base::UTF8ToUTF16(JoinString(
540 install_checker_
.requirement_errors(), ' '))));
543 install_flags_
|= kInstallFlagHasRequirementErrors
;
546 // Check the blacklist state.
547 if (install_checker_
.blacklist_state() == extensions::BLACKLISTED_MALWARE
) {
548 install_flags_
|= kInstallFlagIsBlacklistedForMalware
;
551 if ((install_checker_
.blacklist_state() == extensions::BLACKLISTED_MALWARE
||
552 install_checker_
.blacklist_state() == extensions::BLACKLISTED_UNKNOWN
) &&
553 !allow_silent_install_
) {
554 // User tried to install a blacklisted extension. Show an error and
555 // refuse to install it.
556 ReportFailureFromUIThread(extensions::CrxInstallError(
557 CrxInstallError::ERROR_DECLINED
,
558 l10n_util::GetStringFUTF16(IDS_EXTENSION_IS_BLACKLISTED
,
559 base::UTF8ToUTF16(extension()->name()))));
560 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.BlockCRX",
561 extension()->location(),
562 Manifest::NUM_LOCATIONS
);
566 // NOTE: extension may still be blacklisted, but we're forced to silently
567 // install it. In this case, ExtensionService::OnExtensionInstalled needs to
570 // Check for policy errors.
571 if (!install_checker_
.policy_error().empty()) {
572 // We don't want to show the error infobar for installs from the WebStore,
573 // because the WebStore already shows an error dialog itself.
574 // Note: |client_| can be NULL in unit_tests!
575 if (extension()->from_webstore() && client_
)
576 client_
->install_ui()->SetSkipPostInstallUI(true);
577 ReportFailureFromUIThread(
578 CrxInstallError(CrxInstallError::ERROR_DECLINED
,
579 base::UTF8ToUTF16(install_checker_
.policy_error())));
586 void CrxInstaller::ConfirmInstall() {
587 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
588 ExtensionService
* service
= service_weak_
.get();
589 if (!service
|| service
->browser_terminating())
592 if (KioskModeInfo::IsKioskOnly(install_checker_
.extension().get())) {
593 bool in_kiosk_mode
= false;
594 #if defined(OS_CHROMEOS)
595 user_manager::UserManager
* user_manager
= user_manager::UserManager::Get();
596 in_kiosk_mode
= user_manager
&& user_manager
->IsLoggedInAsKioskApp();
598 if (!in_kiosk_mode
) {
599 ReportFailureFromUIThread(CrxInstallError(
600 CrxInstallError::ERROR_DECLINED
,
601 l10n_util::GetStringUTF16(IDS_EXTENSION_INSTALL_KIOSK_MODE_ONLY
)));
606 // Check whether this install is initiated from the settings page to
607 // update an existing extension or app.
608 CheckUpdateFromSettingsPage();
610 GURL overlapping_url
;
611 ExtensionRegistry
* registry
= ExtensionRegistry::Get(service
->profile());
612 const Extension
* overlapping_extension
=
613 registry
->enabled_extensions().GetHostedAppByOverlappingWebExtent(
614 extension()->web_extent());
615 if (overlapping_extension
&&
616 overlapping_extension
->id() != extension()->id()) {
617 ReportFailureFromUIThread(CrxInstallError(l10n_util::GetStringFUTF16(
618 IDS_EXTENSION_OVERLAPPING_WEB_EXTENT
,
619 base::UTF8ToUTF16(overlapping_extension
->name()))));
623 current_version_
= ExtensionPrefs::Get(service
->profile())
624 ->GetVersionString(extension()->id());
627 (!allow_silent_install_
|| !approved_
) &&
628 !update_from_settings_page_
) {
629 AddRef(); // Balanced in InstallUIProceed() and InstallUIAbort().
630 client_
->ConfirmInstall(this, extension(), show_dialog_callback_
);
632 if (!installer_task_runner_
->PostTask(
634 base::Bind(&CrxInstaller::CompleteInstall
, this)))
640 void CrxInstaller::InstallUIProceed() {
641 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
643 ExtensionService
* service
= service_weak_
.get();
644 if (!service
|| service
->browser_terminating())
647 // If update_from_settings_page_ boolean is true, this functions is
648 // getting called in response to ExtensionInstallPrompt::ConfirmReEnable()
649 // and if it is false, this function is called in response to
650 // ExtensionInstallPrompt::ConfirmInstall().
651 if (update_from_settings_page_
) {
652 service
->GrantPermissionsAndEnableExtension(extension());
654 if (!installer_task_runner_
->PostTask(
656 base::Bind(&CrxInstaller::CompleteInstall
, this)))
660 Release(); // balanced in ConfirmInstall() or ConfirmReEnable().
663 void CrxInstaller::InstallUIAbort(bool user_initiated
) {
664 // If update_from_settings_page_ boolean is true, this functions is
665 // getting called in response to ExtensionInstallPrompt::ConfirmReEnable()
666 // and if it is false, this function is called in response to
667 // ExtensionInstallPrompt::ConfirmInstall().
668 if (!update_from_settings_page_
) {
669 std::string histogram_name
= user_initiated
670 ? "Extensions.Permissions_InstallCancel2"
671 : "Extensions.Permissions_InstallAbort2";
672 ExtensionService::RecordPermissionMessagesHistogram(
673 extension(), histogram_name
.c_str());
675 NotifyCrxInstallComplete(false);
678 Release(); // balanced in ConfirmInstall() or ConfirmReEnable().
680 // We're done. Since we don't post any more tasks to ourself, our ref count
681 // should go to zero and we die. The destructor will clean up the temp dir.
684 void CrxInstaller::CompleteInstall() {
685 DCHECK(installer_task_runner_
->RunsTasksOnCurrentThread());
687 if (!current_version_
.empty()) {
688 Version
current_version(current_version_
);
689 if (current_version
.CompareTo(*(extension()->version())) > 0) {
690 ReportFailureFromFileThread(CrxInstallError(
691 CrxInstallError::ERROR_DECLINED
,
692 l10n_util::GetStringUTF16(
693 extension()->is_app() ? IDS_APP_CANT_DOWNGRADE_VERSION
694 : IDS_EXTENSION_CANT_DOWNGRADE_VERSION
)));
699 // See how long extension install paths are. This is important on
700 // windows, because file operations may fail if the path to a file
701 // exceeds a small constant. See crbug.com/69693 .
702 UMA_HISTOGRAM_CUSTOM_COUNTS(
703 "Extensions.CrxInstallDirPathLength",
704 install_directory_
.value().length(), 0, 500, 100);
706 ExtensionAssetsManager
* assets_manager
=
707 ExtensionAssetsManager::GetInstance();
708 assets_manager
->InstallExtension(
710 unpacked_extension_root_
,
713 base::Bind(&CrxInstaller::ReloadExtensionAfterInstall
, this));
716 void CrxInstaller::ReloadExtensionAfterInstall(
717 const base::FilePath
& version_dir
) {
718 DCHECK(installer_task_runner_
->RunsTasksOnCurrentThread());
720 if (version_dir
.empty()) {
721 ReportFailureFromFileThread(CrxInstallError(l10n_util::GetStringUTF16(
722 IDS_EXTENSION_MOVE_DIRECTORY_TO_PROFILE_FAILED
)));
726 // This is lame, but we must reload the extension because absolute paths
727 // inside the content scripts are established inside InitFromValue() and we
728 // just moved the extension.
729 // TODO(aa): All paths to resources inside extensions should be created
730 // lazily and based on the Extension's root path at that moment.
731 // TODO(rdevlin.cronin): Continue removing std::string errors and replacing
732 // with base::string16
733 std::string extension_id
= extension()->id();
735 install_checker_
.set_extension(
736 file_util::LoadExtension(
739 extension()->creation_flags() | Extension::REQUIRE_KEY
,
743 ReportSuccessFromFileThread();
745 LOG(ERROR
) << error
<< " " << extension_id
<< " " << download_url_
;
746 ReportFailureFromFileThread(CrxInstallError(base::UTF8ToUTF16(error
)));
750 void CrxInstaller::ReportFailureFromFileThread(const CrxInstallError
& error
) {
751 DCHECK(installer_task_runner_
->RunsTasksOnCurrentThread());
752 if (!BrowserThread::PostTask(
753 BrowserThread::UI
, FROM_HERE
,
754 base::Bind(&CrxInstaller::ReportFailureFromUIThread
, this, error
))) {
759 void CrxInstaller::ReportFailureFromUIThread(const CrxInstallError
& error
) {
760 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
762 if (!service_weak_
.get() || service_weak_
->browser_terminating())
765 content::NotificationService
* service
=
766 content::NotificationService::current();
767 service
->Notify(extensions::NOTIFICATION_EXTENSION_INSTALL_ERROR
,
768 content::Source
<CrxInstaller
>(this),
769 content::Details
<const extensions::CrxInstallError
>(&error
));
771 // This isn't really necessary, it is only used because unit tests expect to
772 // see errors get reported via this interface.
774 // TODO(aa): Need to go through unit tests and clean them up too, probably get
776 ExtensionErrorReporter::GetInstance()->ReportError(
781 client_
->OnInstallFailure(error
);
783 NotifyCrxInstallComplete(false);
785 // Delete temporary files.
789 void CrxInstaller::ReportSuccessFromFileThread() {
790 DCHECK(installer_task_runner_
->RunsTasksOnCurrentThread());
792 // Tracking number of extensions installed by users
793 if (install_cause() == extension_misc::INSTALL_CAUSE_USER_DOWNLOAD
)
794 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionInstalled", 1, 2);
796 if (!BrowserThread::PostTask(
797 BrowserThread::UI
, FROM_HERE
,
798 base::Bind(&CrxInstaller::ReportSuccessFromUIThread
, this)))
801 // Delete temporary files.
805 void CrxInstaller::ReportSuccessFromUIThread() {
806 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
808 if (!service_weak_
.get() || service_weak_
->browser_terminating())
811 if (!update_from_settings_page_
) {
812 // If there is a client, tell the client about installation.
814 client_
->OnInstallSuccess(extension(), install_icon_
.get());
816 // We update the extension's granted permissions if the user already
817 // approved the install (client_ is non NULL), or we are allowed to install
819 if ((client_
|| allow_silent_install_
) && grant_permissions_
) {
820 PermissionsUpdater
perms_updater(profile());
821 perms_updater
.InitializePermissions(extension());
822 perms_updater
.GrantActivePermissions(extension());
826 service_weak_
->OnExtensionInstalled(
827 extension(), page_ordinal_
, install_flags_
);
828 NotifyCrxInstallComplete(true);
831 void CrxInstaller::NotifyCrxInstallBegin() {
832 InstallTrackerFactory::GetForBrowserContext(profile())
833 ->OnBeginCrxInstall(expected_id_
);
836 void CrxInstaller::NotifyCrxInstallComplete(bool success
) {
837 // Some users (such as the download shelf) need to know when a
838 // CRXInstaller is done. Listening for the EXTENSION_* events
839 // is problematic because they don't know anything about the
840 // extension before it is unpacked, so they cannot filter based
842 content::NotificationService::current()->Notify(
843 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
844 content::Source
<CrxInstaller
>(this),
845 content::Details
<const Extension
>(success
? extension() : NULL
));
847 InstallTrackerFactory::GetForBrowserContext(profile())
848 ->OnFinishCrxInstall(success
? extension()->id() : expected_id_
, success
);
854 void CrxInstaller::CleanupTempFiles() {
855 if (!installer_task_runner_
->RunsTasksOnCurrentThread()) {
856 if (!installer_task_runner_
->PostTask(
858 base::Bind(&CrxInstaller::CleanupTempFiles
, this))) {
864 // Delete the temp directory and crx file as necessary.
865 if (!temp_dir_
.value().empty()) {
866 file_util::DeleteFile(temp_dir_
, true);
867 temp_dir_
= base::FilePath();
870 if (delete_source_
&& !source_file_
.value().empty()) {
871 file_util::DeleteFile(source_file_
, false);
872 source_file_
= base::FilePath();
876 void CrxInstaller::CheckUpdateFromSettingsPage() {
877 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
879 ExtensionService
* service
= service_weak_
.get();
880 if (!service
|| service
->browser_terminating())
883 if (off_store_install_allow_reason_
!= OffStoreInstallAllowedFromSettingsPage
)
886 const Extension
* installed_extension
=
887 service
->GetInstalledExtension(extension()->id());
888 if (installed_extension
) {
889 // Previous version of the extension exists.
890 update_from_settings_page_
= true;
891 expected_id_
= installed_extension
->id();
892 install_source_
= installed_extension
->location();
893 install_cause_
= extension_misc::INSTALL_CAUSE_UPDATE
;
897 void CrxInstaller::ConfirmReEnable() {
898 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
900 ExtensionService
* service
= service_weak_
.get();
901 if (!service
|| service
->browser_terminating())
904 if (!update_from_settings_page_
)
907 ExtensionPrefs
* prefs
= ExtensionPrefs::Get(service
->profile());
908 if (!prefs
->DidExtensionEscalatePermissions(extension()->id()))
912 AddRef(); // Balanced in InstallUIProceed() and InstallUIAbort().
913 client_
->ConfirmReEnable(this, extension());
917 } // namespace extensions