1 // Copyright (c) 2013 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/extension_service.h"
11 #include "base/command_line.h"
12 #include "base/metrics/histogram.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/stringprintf.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "base/threading/sequenced_worker_pool.h"
17 #include "base/threading/thread_restrictions.h"
18 #include "base/time/time.h"
19 #include "chrome/browser/browser_process.h"
20 #include "chrome/browser/chrome_notification_types.h"
21 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
22 #include "chrome/browser/extensions/component_loader.h"
23 #include "chrome/browser/extensions/crx_installer.h"
24 #include "chrome/browser/extensions/data_deleter.h"
25 #include "chrome/browser/extensions/extension_assets_manager.h"
26 #include "chrome/browser/extensions/extension_disabled_ui.h"
27 #include "chrome/browser/extensions/extension_error_controller.h"
28 #include "chrome/browser/extensions/extension_install_ui.h"
29 #include "chrome/browser/extensions/extension_special_storage_policy.h"
30 #include "chrome/browser/extensions/extension_sync_service.h"
31 #include "chrome/browser/extensions/extension_util.h"
32 #include "chrome/browser/extensions/external_install_manager.h"
33 #include "chrome/browser/extensions/external_provider_impl.h"
34 #include "chrome/browser/extensions/install_verifier.h"
35 #include "chrome/browser/extensions/installed_loader.h"
36 #include "chrome/browser/extensions/pending_extension_manager.h"
37 #include "chrome/browser/extensions/permissions_updater.h"
38 #include "chrome/browser/extensions/shared_module_service.h"
39 #include "chrome/browser/extensions/unpacked_installer.h"
40 #include "chrome/browser/extensions/updater/extension_cache.h"
41 #include "chrome/browser/extensions/updater/extension_downloader.h"
42 #include "chrome/browser/extensions/updater/extension_updater.h"
43 #include "chrome/browser/profiles/profile.h"
44 #include "chrome/browser/signin/profile_identity_provider.h"
45 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
46 #include "chrome/browser/signin/signin_manager_factory.h"
47 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
48 #include "chrome/browser/ui/webui/favicon_source.h"
49 #include "chrome/browser/ui/webui/ntp/thumbnail_source.h"
50 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
51 #include "chrome/browser/ui/webui/theme_source.h"
52 #include "chrome/common/chrome_switches.h"
53 #include "chrome/common/crash_keys.h"
54 #include "chrome/common/extensions/extension_constants.h"
55 #include "chrome/common/extensions/features/feature_channel.h"
56 #include "chrome/common/extensions/manifest_url_handler.h"
57 #include "chrome/common/pref_names.h"
58 #include "chrome/common/url_constants.h"
59 #include "components/signin/core/browser/signin_manager.h"
60 #include "components/startup_metric_utils/startup_metric_utils.h"
61 #include "content/public/browser/devtools_agent_host.h"
62 #include "content/public/browser/notification_service.h"
63 #include "content/public/browser/render_process_host.h"
64 #include "content/public/browser/storage_partition.h"
65 #include "extensions/browser/event_router.h"
66 #include "extensions/browser/extension_host.h"
67 #include "extensions/browser/extension_prefs.h"
68 #include "extensions/browser/extension_registry.h"
69 #include "extensions/browser/extension_system.h"
70 #include "extensions/browser/install_flag.h"
71 #include "extensions/browser/pref_names.h"
72 #include "extensions/browser/runtime_data.h"
73 #include "extensions/browser/uninstall_reason.h"
74 #include "extensions/browser/update_observer.h"
75 #include "extensions/common/extension_messages.h"
76 #include "extensions/common/feature_switch.h"
77 #include "extensions/common/file_util.h"
78 #include "extensions/common/manifest_constants.h"
79 #include "extensions/common/manifest_handlers/background_info.h"
80 #include "extensions/common/one_shot_event.h"
81 #include "extensions/common/permissions/permission_message_provider.h"
82 #include "extensions/common/permissions/permissions_data.h"
84 #if defined(OS_CHROMEOS)
85 #include "chrome/browser/chromeos/extensions/install_limiter.h"
86 #include "webkit/browser/fileapi/file_system_backend.h"
87 #include "webkit/browser/fileapi/file_system_context.h"
90 using content::BrowserContext;
91 using content::BrowserThread;
92 using content::DevToolsAgentHost;
93 using extensions::CrxInstaller;
94 using extensions::Extension;
95 using extensions::ExtensionDownloader;
96 using extensions::ExtensionDownloaderDelegate;
97 using extensions::ExtensionIdSet;
98 using extensions::ExtensionInfo;
99 using extensions::ExtensionRegistry;
100 using extensions::ExtensionSet;
101 using extensions::FeatureSwitch;
102 using extensions::InstallVerifier;
103 using extensions::ManagementPolicy;
104 using extensions::Manifest;
105 using extensions::PermissionMessage;
106 using extensions::PermissionMessages;
107 using extensions::PermissionSet;
108 using extensions::SharedModuleInfo;
109 using extensions::SharedModuleService;
110 using extensions::UnloadedExtensionInfo;
112 namespace errors = extensions::manifest_errors;
116 // Wait this many seconds after an extensions becomes idle before updating it.
117 const int kUpdateIdleDelay = 5;
119 #if defined(ENABLE_EXTENSIONS)
120 scoped_ptr<IdentityProvider> CreateWebstoreIdentityProvider(Profile* profile) {
121 return make_scoped_ptr<IdentityProvider>(new ProfileIdentityProvider(
122 SigninManagerFactory::GetForProfile(profile),
123 ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
124 LoginUIServiceFactory::GetForProfile(profile)));
126 #endif // defined(ENABLE_EXTENSIONS)
132 void ExtensionService::CheckExternalUninstall(const std::string& id) {
133 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
135 // Check if the providers know about this extension.
136 extensions::ProviderCollection::const_iterator i;
137 for (i = external_extension_providers_.begin();
138 i != external_extension_providers_.end(); ++i) {
139 DCHECK(i->get()->IsReady());
140 if (i->get()->HasExtension(id))
141 return; // Yup, known extension, don't uninstall.
144 // We get the list of external extensions to check from preferences.
145 // It is possible that an extension has preferences but is not loaded.
146 // For example, an extension that requires experimental permissions
147 // will not be loaded if the experimental command line flag is not used.
148 // In this case, do not uninstall.
149 if (!GetInstalledExtension(id)) {
150 // We can't call UninstallExtension with an unloaded/invalid
152 LOG(WARNING) << "Attempted uninstallation of unloaded/invalid extension "
153 << "with id: " << id;
156 UninstallExtension(id,
157 extensions::UNINSTALL_REASON_ORPHANED_EXTERNAL_EXTENSION,
158 base::Bind(&base::DoNothing),
162 void ExtensionService::SetFileTaskRunnerForTesting(
163 base::SequencedTaskRunner* task_runner) {
164 file_task_runner_ = task_runner;
167 void ExtensionService::ClearProvidersForTesting() {
168 external_extension_providers_.clear();
171 void ExtensionService::AddProviderForTesting(
172 extensions::ExternalProviderInterface* test_provider) {
173 CHECK(test_provider);
174 external_extension_providers_.push_back(
175 linked_ptr<extensions::ExternalProviderInterface>(test_provider));
178 void ExtensionService::BlacklistExtensionForTest(
179 const std::string& extension_id) {
180 ExtensionIdSet blocked;
181 ExtensionIdSet unchanged;
182 blocked.insert(extension_id);
183 UpdateBlockedExtensions(blocked, unchanged);
186 bool ExtensionService::OnExternalExtensionUpdateUrlFound(
187 const std::string& id,
188 const std::string& install_parameter,
189 const GURL& update_url,
190 Manifest::Location location,
192 bool mark_acknowledged) {
193 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
194 CHECK(Extension::IdIsValid(id));
196 if (Manifest::IsExternalLocation(location)) {
197 // All extensions that are not user specific can be cached.
198 extensions::ExtensionCache::GetInstance()->AllowCaching(id);
201 const Extension* extension = GetExtensionById(id, true);
203 // Already installed. Skip this install if the current location has
204 // higher priority than |location|.
205 Manifest::Location current = extension->location();
206 if (current == Manifest::GetHigherPriorityLocation(current, location))
208 // Otherwise, overwrite the current installation.
211 // Add |id| to the set of pending extensions. If it can not be added,
212 // then there is already a pending record from a higher-priority install
213 // source. In this case, signal that this extension will not be
214 // installed by returning false.
215 if (!pending_extension_manager()->AddFromExternalUpdateUrl(
221 mark_acknowledged)) {
225 update_once_all_providers_are_ready_ = true;
230 // This function is used to uninstall an extension via sync. The LOG statements
231 // within this function are used to inform the user if the uninstall cannot be
233 bool ExtensionService::UninstallExtensionHelper(
234 ExtensionService* extensions_service,
235 const std::string& extension_id,
236 extensions::UninstallReason reason) {
237 // We can't call UninstallExtension with an invalid extension ID.
238 if (!extensions_service->GetInstalledExtension(extension_id)) {
239 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
240 << "id: " << extension_id;
244 // The following call to UninstallExtension will not allow an uninstall of a
245 // policy-controlled extension.
246 base::string16 error;
247 if (!extensions_service->UninstallExtension(
248 extension_id, reason, base::Bind(&base::DoNothing), &error)) {
249 LOG(WARNING) << "Cannot uninstall extension with id " << extension_id
257 ExtensionService::ExtensionService(Profile* profile,
258 const CommandLine* command_line,
259 const base::FilePath& install_directory,
260 extensions::ExtensionPrefs* extension_prefs,
261 extensions::Blacklist* blacklist,
262 bool autoupdate_enabled,
263 bool extensions_enabled,
264 extensions::OneShotEvent* ready)
265 : extensions::Blacklist::Observer(blacklist),
267 system_(extensions::ExtensionSystem::Get(profile)),
268 extension_prefs_(extension_prefs),
269 blacklist_(blacklist),
270 extension_sync_service_(NULL),
271 registry_(extensions::ExtensionRegistry::Get(profile)),
272 pending_extension_manager_(profile),
273 install_directory_(install_directory),
274 extensions_enabled_(extensions_enabled),
275 show_extensions_prompts_(true),
276 install_updates_when_idle_(true),
278 update_once_all_providers_are_ready_(false),
279 browser_terminating_(false),
280 installs_delayed_for_gc_(false),
281 is_first_run_(false),
282 shared_module_service_(new extensions::SharedModuleService(profile_)) {
283 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
285 // Figure out if extension installation should be enabled.
286 if (extensions::ExtensionsBrowserClient::Get()->AreExtensionsDisabled(
287 *command_line, profile))
288 extensions_enabled_ = false;
290 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING,
291 content::NotificationService::AllBrowserContextsAndSources());
293 extensions::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
294 content::NotificationService::AllBrowserContextsAndSources());
295 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
296 content::NotificationService::AllBrowserContextsAndSources());
297 registrar_.Add(this, chrome::NOTIFICATION_UPGRADE_RECOMMENDED,
298 content::NotificationService::AllBrowserContextsAndSources());
300 chrome::NOTIFICATION_PROFILE_DESTRUCTION_STARTED,
301 content::Source<Profile>(profile_));
302 pref_change_registrar_.Init(profile->GetPrefs());
303 base::Closure callback =
304 base::Bind(&ExtensionService::OnExtensionInstallPrefChanged,
305 base::Unretained(this));
306 pref_change_registrar_.Add(extensions::pref_names::kInstallAllowList,
308 pref_change_registrar_.Add(extensions::pref_names::kInstallDenyList,
310 pref_change_registrar_.Add(extensions::pref_names::kAllowedTypes, callback);
312 // Set up the ExtensionUpdater
313 if (autoupdate_enabled) {
314 int update_frequency = extensions::kDefaultUpdateFrequencySeconds;
315 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
316 base::StringToInt(command_line->GetSwitchValueASCII(
317 switches::kExtensionsUpdateFrequency),
320 updater_.reset(new extensions::ExtensionUpdater(
326 extensions::ExtensionCache::GetInstance(),
327 base::Bind(&ExtensionService::CreateExtensionDownloader,
328 base::Unretained(this))));
331 component_loader_.reset(
332 new extensions::ComponentLoader(this,
334 g_browser_process->local_state(),
337 if (extensions_enabled_) {
338 extensions::ExternalProviderImpl::CreateExternalProviders(
339 this, profile_, &external_extension_providers_);
342 // Set this as the ExtensionService for app sorting to ensure it causes syncs
344 is_first_run_ = !extension_prefs_->SetAlertSystemFirstRun();
346 error_controller_.reset(
347 new extensions::ExtensionErrorController(profile_, is_first_run_));
348 external_install_manager_.reset(
349 new extensions::ExternalInstallManager(profile_, is_first_run_));
351 extension_action_storage_manager_.reset(
352 new extensions::ExtensionActionStorageManager(profile_));
354 // How long is the path to the Extensions directory?
355 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ExtensionRootPathLength",
356 install_directory_.value().length(), 0, 500, 100);
359 const ExtensionSet* ExtensionService::extensions() const {
360 return ®istry_->enabled_extensions();
363 extensions::PendingExtensionManager*
364 ExtensionService::pending_extension_manager() {
365 return &pending_extension_manager_;
368 ExtensionService::~ExtensionService() {
369 // No need to unload extensions here because they are profile-scoped, and the
370 // profile is in the process of being deleted.
372 extensions::ProviderCollection::const_iterator i;
373 for (i = external_extension_providers_.begin();
374 i != external_extension_providers_.end(); ++i) {
375 extensions::ExternalProviderInterface* provider = i->get();
376 provider->ServiceShutdown();
380 void ExtensionService::Shutdown() {
381 system_->management_policy()->UnregisterProvider(
382 shared_module_policy_provider_.get());
385 const Extension* ExtensionService::GetExtensionById(
386 const std::string& id, bool include_disabled) const {
387 int include_mask = ExtensionRegistry::ENABLED;
388 if (include_disabled) {
389 // Include blacklisted extensions here because there are hundreds of
390 // callers of this function, and many might assume that this includes those
391 // that have been disabled due to blacklisting.
392 include_mask |= ExtensionRegistry::DISABLED |
393 ExtensionRegistry::BLACKLISTED;
395 return registry_->GetExtensionById(id, include_mask);
398 void ExtensionService::Init() {
399 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
401 base::Time begin_time = base::Time::Now();
403 DCHECK(!is_ready()); // Can't redo init.
404 DCHECK_EQ(registry_->enabled_extensions().size(), 0u);
406 const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
407 if (cmd_line->HasSwitch(switches::kInstallFromWebstore) ||
408 cmd_line->HasSwitch(switches::kLimitedInstallFromWebstore)) {
409 // The sole purpose of this launch is to install a new extension from CWS
410 // and immediately terminate: loading already installed extensions is
411 // unnecessary and may interfere with the inline install dialog (e.g. if an
412 // extension listens to onStartup and opens a window).
413 SetReadyAndNotifyListeners();
415 // LoadAllExtensions() calls OnLoadedInstalledExtensions().
416 component_loader_->LoadAll();
417 extensions::InstalledLoader(this).LoadAllExtensions();
419 // Attempt to re-enable extensions whose only disable reason is reloading.
420 std::vector<std::string> extensions_to_enable;
421 const ExtensionSet& disabled_extensions = registry_->disabled_extensions();
422 for (ExtensionSet::const_iterator iter = disabled_extensions.begin();
423 iter != disabled_extensions.end(); ++iter) {
424 const Extension* e = iter->get();
425 if (extension_prefs_->GetDisableReasons(e->id()) ==
426 Extension::DISABLE_RELOAD) {
427 extensions_to_enable.push_back(e->id());
430 for (std::vector<std::string>::iterator it = extensions_to_enable.begin();
431 it != extensions_to_enable.end(); ++it) {
432 EnableExtension(*it);
435 // Finish install (if possible) of extensions that were still delayed while
436 // the browser was shut down.
437 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> delayed_info(
438 extension_prefs_->GetAllDelayedInstallInfo());
439 for (size_t i = 0; i < delayed_info->size(); ++i) {
440 ExtensionInfo* info = delayed_info->at(i).get();
441 scoped_refptr<const Extension> extension(NULL);
442 if (info->extension_manifest) {
444 extension = Extension::Create(
445 info->extension_path,
446 info->extension_location,
447 *info->extension_manifest,
448 extension_prefs_->GetDelayedInstallCreationFlags(
453 delayed_installs_.Insert(extension);
456 MaybeFinishDelayedInstallations();
458 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> delayed_info2(
459 extension_prefs_->GetAllDelayedInstallInfo());
460 UMA_HISTOGRAM_COUNTS_100("Extensions.UpdateOnLoad",
461 delayed_info2->size() - delayed_info->size());
463 SetReadyAndNotifyListeners();
465 // TODO(erikkay) this should probably be deferred to a future point
466 // rather than running immediately at startup.
467 CheckForExternalUpdates();
469 LoadGreylistFromPrefs();
472 UMA_HISTOGRAM_TIMES("Extensions.ExtensionServiceInitTime",
473 base::Time::Now() - begin_time);
476 void ExtensionService::LoadGreylistFromPrefs() {
477 scoped_ptr<ExtensionSet> all_extensions =
478 registry_->GenerateInstalledExtensionsSet();
480 for (ExtensionSet::const_iterator it = all_extensions->begin();
481 it != all_extensions->end(); ++it) {
482 extensions::BlacklistState state =
483 extension_prefs_->GetExtensionBlacklistState((*it)->id());
484 if (state == extensions::BLACKLISTED_SECURITY_VULNERABILITY ||
485 state == extensions::BLACKLISTED_POTENTIALLY_UNWANTED ||
486 state == extensions::BLACKLISTED_CWS_POLICY_VIOLATION)
487 greylist_.Insert(*it);
491 bool ExtensionService::UpdateExtension(const std::string& id,
492 const base::FilePath& extension_path,
493 bool file_ownership_passed,
494 CrxInstaller** out_crx_installer) {
495 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
496 if (browser_terminating_) {
497 LOG(WARNING) << "Skipping UpdateExtension due to browser shutdown";
498 // Leak the temp file at extension_path. We don't want to add to the disk
499 // I/O burden at shutdown, we can't rely on the I/O completing anyway, and
500 // the file is in the OS temp directory which should be cleaned up for us.
504 const extensions::PendingExtensionInfo* pending_extension_info =
505 pending_extension_manager()->GetById(id);
507 const Extension* extension = GetInstalledExtension(id);
508 if (!pending_extension_info && !extension) {
509 LOG(WARNING) << "Will not update extension " << id
510 << " because it is not installed or pending";
511 // Delete extension_path since we're not creating a CrxInstaller
512 // that would do it for us.
513 if (!GetFileTaskRunner()->PostTask(
516 &extensions::file_util::DeleteFile, extension_path, false)))
522 // We want a silent install only for non-pending extensions and
523 // pending extensions that have install_silently set.
524 scoped_ptr<ExtensionInstallPrompt> client;
525 if (pending_extension_info && !pending_extension_info->install_silently())
526 client.reset(ExtensionInstallUI::CreateInstallPromptWithProfile(profile_));
528 scoped_refptr<CrxInstaller> installer(
529 CrxInstaller::Create(this, client.Pass()));
530 installer->set_expected_id(id);
531 int creation_flags = Extension::NO_FLAGS;
532 if (pending_extension_info) {
533 installer->set_install_source(pending_extension_info->install_source());
534 if (pending_extension_info->install_silently())
535 installer->set_allow_silent_install(true);
536 if (pending_extension_info->remote_install())
537 installer->set_grant_permissions(false);
538 creation_flags = pending_extension_info->creation_flags();
539 if (pending_extension_info->mark_acknowledged())
540 external_install_manager_->AcknowledgeExternalExtension(id);
541 } else if (extension) {
542 installer->set_install_source(extension->location());
544 // If the extension was installed from or has migrated to the webstore, or
545 // its auto-update URL is from the webstore, treat it as a webstore install.
546 // Note that we ignore some older extensions with blank auto-update URLs
547 // because we are mostly concerned with restrictions on NaCl extensions,
549 if ((extension && extension->from_webstore()) ||
550 (extension && extensions::ManifestURL::UpdatesFromGallery(extension)) ||
551 (!extension && extension_urls::IsWebstoreUpdateUrl(
552 pending_extension_info->update_url()))) {
553 creation_flags |= Extension::FROM_WEBSTORE;
556 // Bookmark apps being updated is kind of a contradiction, but that's because
557 // we mark the default apps as bookmark apps, and they're hosted in the web
558 // store, thus they can get updated. See http://crbug.com/101605 for more
560 if (extension && extension->from_bookmark())
561 creation_flags |= Extension::FROM_BOOKMARK;
563 if (extension && extension->was_installed_by_default())
564 creation_flags |= Extension::WAS_INSTALLED_BY_DEFAULT;
566 if (extension && extension->was_installed_by_oem())
567 creation_flags |= Extension::WAS_INSTALLED_BY_OEM;
569 if (extension && extension->was_installed_by_custodian())
570 creation_flags |= Extension::WAS_INSTALLED_BY_CUSTODIAN;
573 installer->set_is_ephemeral(extension_prefs_->IsEphemeralApp(id));
574 installer->set_do_not_sync(extension_prefs_->DoNotSync(id));
577 installer->set_creation_flags(creation_flags);
579 installer->set_delete_source(file_ownership_passed);
580 installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
581 installer->InstallCrx(extension_path);
583 if (out_crx_installer)
584 *out_crx_installer = installer.get();
589 scoped_ptr<ExtensionDownloader> ExtensionService::CreateExtensionDownloader(
590 ExtensionDownloaderDelegate* delegate) {
591 scoped_ptr<ExtensionDownloader> downloader;
592 #if defined(ENABLE_EXTENSIONS)
593 scoped_ptr<IdentityProvider> identity_provider =
594 CreateWebstoreIdentityProvider(profile_);
595 downloader.reset(new ExtensionDownloader(
597 profile_->GetRequestContext()));
598 downloader->SetWebstoreIdentityProvider(identity_provider.Pass());
600 return downloader.Pass();
603 void ExtensionService::ReloadExtensionImpl(
604 // "transient" because the process of reloading may cause the reference
605 // to become invalid. Instead, use |extension_id|, a copy.
606 const std::string& transient_extension_id,
608 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
610 // If the extension is already reloading, don't reload again.
611 if (extension_prefs_->GetDisableReasons(transient_extension_id) &
612 Extension::DISABLE_RELOAD) {
616 // Ignore attempts to reload a blacklisted extension. Sometimes this can
617 // happen in a convoluted reload sequence triggered by the termination of a
618 // blacklisted extension and a naive attempt to reload it. For an example see
619 // http://crbug.com/373842.
620 if (registry_->blacklisted_extensions().Contains(transient_extension_id))
625 std::string extension_id = transient_extension_id;
626 const Extension* transient_current_extension =
627 GetExtensionById(extension_id, false);
629 // Disable the extension if it's loaded. It might not be loaded if it crashed.
630 if (transient_current_extension) {
631 // If the extension has an inspector open for its background page, detach
632 // the inspector and hang onto a cookie for it, so that we can reattach
634 // TODO(yoz): this is not incognito-safe!
635 extensions::ProcessManager* manager = system_->process_manager();
636 extensions::ExtensionHost* host =
637 manager->GetBackgroundHostForExtension(extension_id);
638 if (host && DevToolsAgentHost::HasFor(host->host_contents())) {
639 // Look for an open inspector for the background page.
640 scoped_refptr<DevToolsAgentHost> agent_host =
641 DevToolsAgentHost::GetOrCreateFor(host->host_contents());
642 agent_host->DisconnectWebContents();
643 orphaned_dev_tools_[extension_id] = agent_host;
646 path = transient_current_extension->path();
647 // BeingUpgraded is set back to false when the extension is added.
648 system_->runtime_data()->SetBeingUpgraded(transient_current_extension,
650 DisableExtension(extension_id, Extension::DISABLE_RELOAD);
651 reloading_extensions_.insert(extension_id);
653 std::map<std::string, base::FilePath>::const_iterator iter =
654 unloaded_extension_paths_.find(extension_id);
655 if (iter == unloaded_extension_paths_.end()) {
658 path = unloaded_extension_paths_[extension_id];
661 transient_current_extension = NULL;
663 if (delayed_installs_.Contains(extension_id)) {
664 FinishDelayedInstallation(extension_id);
668 // If we're reloading a component extension, use the component extension
669 // loader's reloader.
670 if (component_loader_->Exists(extension_id)) {
671 component_loader_->Reload(extension_id);
675 // Check the installed extensions to see if what we're reloading was already
677 scoped_ptr<ExtensionInfo> installed_extension(
678 extension_prefs_->GetInstalledExtensionInfo(extension_id));
679 if (installed_extension.get() &&
680 installed_extension->extension_manifest.get()) {
681 extensions::InstalledLoader(this).Load(*installed_extension, false);
683 // Otherwise, the extension is unpacked (location LOAD).
684 // We should always be able to remember the extension's path. If it's not in
685 // the map, someone failed to update |unloaded_extension_paths_|.
686 CHECK(!path.empty());
687 scoped_refptr<extensions::UnpackedInstaller> unpacked_installer =
688 extensions::UnpackedInstaller::Create(this);
689 unpacked_installer->set_be_noisy_on_failure(be_noisy);
690 unpacked_installer->Load(path);
694 void ExtensionService::ReloadExtension(const std::string& extension_id) {
695 ReloadExtensionImpl(extension_id, true); // be_noisy
698 void ExtensionService::ReloadExtensionWithQuietFailure(
699 const std::string& extension_id) {
700 ReloadExtensionImpl(extension_id, false); // be_noisy
703 bool ExtensionService::UninstallExtension(
704 // "transient" because the process of uninstalling may cause the reference
705 // to become invalid. Instead, use |extenson->id()|.
706 const std::string& transient_extension_id,
707 extensions::UninstallReason reason,
708 const base::Closure& deletion_done_callback,
709 base::string16* error) {
710 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
712 scoped_refptr<const Extension> extension =
713 GetInstalledExtension(transient_extension_id);
715 // Callers should not send us nonexistent extensions.
716 CHECK(extension.get());
718 // Policy change which triggers an uninstall will always set
719 // |external_uninstall| to true so this is the only way to uninstall
720 // managed extensions.
721 // Shared modules being uninstalled will also set |external_uninstall| to true
722 // so that we can guarantee users don't uninstall a shared module.
723 // (crbug.com/273300)
724 // TODO(rdevlin.cronin): This is probably not right. We should do something
725 // else, like include an enum IS_INTERNAL_UNINSTALL or IS_USER_UNINSTALL so
727 bool external_uninstall =
728 (reason == extensions::UNINSTALL_REASON_INTERNAL_MANAGEMENT) ||
729 (reason == extensions::UNINSTALL_REASON_ORPHANED_EXTERNAL_EXTENSION) ||
730 (reason == extensions::UNINSTALL_REASON_ORPHANED_SHARED_MODULE) ||
731 (reason == extensions::UNINSTALL_REASON_SYNC &&
732 extension->was_installed_by_custodian());
733 if (!external_uninstall &&
734 !system_->management_policy()->UserMayModifySettings(
735 extension.get(), error)) {
736 content::NotificationService::current()->Notify(
737 extensions::NOTIFICATION_EXTENSION_UNINSTALL_NOT_ALLOWED,
738 content::Source<Profile>(profile_),
739 content::Details<const Extension>(extension.get()));
743 syncer::SyncChange sync_change;
744 if (extension_sync_service_) {
745 sync_change = extension_sync_service_->PrepareToSyncUninstallExtension(
746 extension.get(), is_ready());
749 system_->install_verifier()->Remove(extension->id());
751 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
752 extension->GetType(), 100);
753 RecordPermissionMessagesHistogram(extension.get(),
754 "Extensions.Permissions_Uninstall2");
756 // Unload before doing more cleanup to ensure that nothing is hanging on to
757 // any of these resources.
758 UnloadExtension(extension->id(), UnloadedExtensionInfo::REASON_UNINSTALL);
760 // Tell the backend to start deleting installed extensions on the file thread.
761 if (!Manifest::IsUnpackedLocation(extension->location())) {
762 if (!GetFileTaskRunner()->PostTask(
764 base::Bind(&ExtensionService::UninstallExtensionOnFileThread,
772 extensions::DataDeleter::StartDeleting(
773 profile_, extension.get(), deletion_done_callback);
775 UntrackTerminatedExtension(extension->id());
777 // Notify interested parties that we've uninstalled this extension.
778 content::NotificationService::current()->Notify(
779 extensions::NOTIFICATION_EXTENSION_UNINSTALLED_DEPRECATED,
780 content::Source<Profile>(profile_),
781 content::Details<const Extension>(extension.get()));
782 ExtensionRegistry::Get(profile_)
783 ->TriggerOnUninstalled(extension.get(), reason);
785 if (extension_sync_service_) {
786 extension_sync_service_->ProcessSyncUninstallExtension(extension->id(),
790 delayed_installs_.Remove(extension->id());
792 extension_prefs_->OnExtensionUninstalled(
793 extension->id(), extension->location(), external_uninstall);
795 // Track the uninstallation.
796 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionUninstalled", 1, 2);
802 void ExtensionService::UninstallExtensionOnFileThread(
803 const std::string& id,
805 const base::FilePath& install_dir,
806 const base::FilePath& extension_path) {
807 extensions::ExtensionAssetsManager* assets_manager =
808 extensions::ExtensionAssetsManager::GetInstance();
809 assets_manager->UninstallExtension(id, profile, install_dir, extension_path);
812 bool ExtensionService::IsExtensionEnabled(
813 const std::string& extension_id) const {
814 if (registry_->enabled_extensions().Contains(extension_id) ||
815 registry_->terminated_extensions().Contains(extension_id)) {
819 if (registry_->disabled_extensions().Contains(extension_id) ||
820 registry_->blacklisted_extensions().Contains(extension_id)) {
824 // If the extension hasn't been loaded yet, check the prefs for it. Assume
825 // enabled unless otherwise noted.
826 return !extension_prefs_->IsExtensionDisabled(extension_id) &&
827 !extension_prefs_->IsExtensionBlacklisted(extension_id) &&
828 !extension_prefs_->IsExternalExtensionUninstalled(extension_id);
831 void ExtensionService::EnableExtension(const std::string& extension_id) {
832 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
834 if (IsExtensionEnabled(extension_id))
836 const Extension* extension =
837 registry_->disabled_extensions().GetByID(extension_id);
839 ManagementPolicy* policy = system_->management_policy();
840 if (extension && policy->MustRemainDisabled(extension, NULL, NULL)) {
841 UMA_HISTOGRAM_COUNTS_100("Extensions.EnableDeniedByPolicy", 1);
845 extension_prefs_->SetExtensionState(extension_id, Extension::ENABLED);
846 extension_prefs_->ClearDisableReasons(extension_id);
848 // This can happen if sync enables an extension that is not
853 // Move it over to the enabled list.
854 registry_->AddEnabled(make_scoped_refptr(extension));
855 registry_->RemoveDisabled(extension->id());
857 NotifyExtensionLoaded(extension);
859 // Notify listeners that the extension was enabled.
860 content::NotificationService::current()->Notify(
861 extensions::NOTIFICATION_EXTENSION_ENABLED,
862 content::Source<Profile>(profile_),
863 content::Details<const Extension>(extension));
865 if (extension_sync_service_)
866 extension_sync_service_->SyncEnableExtension(*extension);
869 void ExtensionService::DisableExtension(
870 const std::string& extension_id,
871 Extension::DisableReason disable_reason) {
872 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
874 // The extension may have been disabled already. Just add a disable reason.
875 if (!IsExtensionEnabled(extension_id)) {
876 extension_prefs_->AddDisableReason(extension_id, disable_reason);
880 const Extension* extension = GetInstalledExtension(extension_id);
881 // |extension| can be NULL if sync disables an extension that is not
883 // EXTERNAL_COMPONENT extensions are not generally modifiable by users, but
884 // can be uninstalled by the browser if the user sets extension-specific
887 disable_reason != Extension::DISABLE_RELOAD &&
888 !system_->management_policy()->UserMayModifySettings(extension, NULL) &&
889 extension->location() != Manifest::EXTERNAL_COMPONENT) {
893 extension_prefs_->SetExtensionState(extension_id, Extension::DISABLED);
894 extension_prefs_->AddDisableReason(extension_id, disable_reason);
897 ExtensionRegistry::EVERYTHING & ~ExtensionRegistry::DISABLED;
898 extension = registry_->GetExtensionById(extension_id, include_mask);
902 // The extension is either enabled or terminated.
903 DCHECK(registry_->enabled_extensions().Contains(extension->id()) ||
904 registry_->terminated_extensions().Contains(extension->id()));
906 // Move it over to the disabled list. Don't send a second unload notification
907 // for terminated extensions being disabled.
908 registry_->AddDisabled(make_scoped_refptr(extension));
909 if (registry_->enabled_extensions().Contains(extension->id())) {
910 registry_->RemoveEnabled(extension->id());
911 NotifyExtensionUnloaded(extension, UnloadedExtensionInfo::REASON_DISABLE);
913 registry_->RemoveTerminated(extension->id());
916 if (extension_sync_service_)
917 extension_sync_service_->SyncDisableExtension(*extension);
920 void ExtensionService::DisableUserExtensions(
921 const std::vector<std::string>& except_ids) {
922 extensions::ManagementPolicy* management_policy =
923 system_->management_policy();
924 extensions::ExtensionList to_disable;
926 const ExtensionSet& enabled_set = registry_->enabled_extensions();
927 for (ExtensionSet::const_iterator extension = enabled_set.begin();
928 extension != enabled_set.end(); ++extension) {
929 if (management_policy->UserMayModifySettings(extension->get(), NULL))
930 to_disable.push_back(*extension);
932 const ExtensionSet& terminated_set = registry_->terminated_extensions();
933 for (ExtensionSet::const_iterator extension = terminated_set.begin();
934 extension != terminated_set.end(); ++extension) {
935 if (management_policy->UserMayModifySettings(extension->get(), NULL))
936 to_disable.push_back(*extension);
939 for (extensions::ExtensionList::const_iterator extension = to_disable.begin();
940 extension != to_disable.end(); ++extension) {
941 if ((*extension)->was_installed_by_default() &&
942 extension_urls::IsWebstoreUpdateUrl(
943 extensions::ManifestURL::GetUpdateURL(*extension)))
945 const std::string& id = (*extension)->id();
946 if (except_ids.end() == std::find(except_ids.begin(), except_ids.end(), id))
947 DisableExtension(id, extensions::Extension::DISABLE_USER_ACTION);
951 void ExtensionService::GrantPermissionsAndEnableExtension(
952 const Extension* extension) {
953 GrantPermissions(extension);
954 RecordPermissionMessagesHistogram(extension,
955 "Extensions.Permissions_ReEnable2");
956 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
957 EnableExtension(extension->id());
960 void ExtensionService::GrantPermissions(const Extension* extension) {
962 extensions::PermissionsUpdater(profile()).GrantActivePermissions(extension);
966 void ExtensionService::RecordPermissionMessagesHistogram(
967 const Extension* extension, const char* histogram) {
968 // Since this is called from multiple sources, and since the histogram macros
969 // use statics, we need to manually lookup the histogram ourselves.
970 base::HistogramBase* counter = base::LinearHistogram::FactoryGet(
973 PermissionMessage::kEnumBoundary,
974 PermissionMessage::kEnumBoundary + 1,
975 base::HistogramBase::kUmaTargetedHistogramFlag);
977 PermissionMessages permissions =
978 extension->permissions_data()->GetPermissionMessages();
979 if (permissions.empty()) {
980 counter->Add(PermissionMessage::kNone);
982 for (PermissionMessages::iterator it = permissions.begin();
983 it != permissions.end(); ++it)
984 counter->Add(it->id());
988 void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
989 // The URLRequestContexts need to be first to know that the extension
990 // was loaded, otherwise a race can arise where a renderer that is created
991 // for the extension may try to load an extension URL with an extension id
992 // that the request context doesn't yet know about. The profile is responsible
993 // for ensuring its URLRequestContexts appropriately discover the loaded
995 system_->RegisterExtensionWithRequestContexts(extension);
997 // Tell renderers about the new extension, unless it's a theme (renderers
998 // don't need to know about themes).
999 if (!extension->is_theme()) {
1000 for (content::RenderProcessHost::iterator i(
1001 content::RenderProcessHost::AllHostsIterator());
1002 !i.IsAtEnd(); i.Advance()) {
1003 content::RenderProcessHost* host = i.GetCurrentValue();
1004 Profile* host_profile =
1005 Profile::FromBrowserContext(host->GetBrowserContext());
1006 if (host_profile->GetOriginalProfile() ==
1007 profile_->GetOriginalProfile()) {
1008 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions(
1009 1, ExtensionMsg_Loaded_Params(extension));
1011 new ExtensionMsg_Loaded(loaded_extensions));
1016 // Tell subsystems that use the EXTENSION_LOADED notification about the new
1019 // NOTE: It is important that this happen after notifying the renderers about
1020 // the new extensions so that if we navigate to an extension URL in
1021 // ExtensionRegistryObserver::OnLoaded or
1022 // NOTIFICATION_EXTENSION_LOADED_DEPRECATED, the
1023 // renderer is guaranteed to know about it.
1024 registry_->TriggerOnLoaded(extension);
1026 content::NotificationService::current()->Notify(
1027 extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
1028 content::Source<Profile>(profile_),
1029 content::Details<const Extension>(extension));
1031 // TODO(kalman): Convert ExtensionSpecialStoragePolicy to a
1032 // BrowserContextKeyedService and use ExtensionRegistryObserver.
1033 profile_->GetExtensionSpecialStoragePolicy()->
1034 GrantRightsForExtension(extension);
1036 // TODO(kalman): This is broken. The crash reporter is process-wide so doesn't
1037 // work properly multi-profile. Besides which, it should be using
1038 // ExtensionRegistryObserver. See http://crbug.com/355029.
1039 UpdateActiveExtensionsInCrashReporter();
1041 const extensions::PermissionsData* permissions_data =
1042 extension->permissions_data();
1044 // If the extension has permission to load chrome://favicon/ resources we need
1045 // to make sure that the FaviconSource is registered with the
1046 // ChromeURLDataManager.
1047 if (permissions_data->HasHostPermission(GURL(chrome::kChromeUIFaviconURL))) {
1048 FaviconSource* favicon_source = new FaviconSource(profile_,
1049 FaviconSource::FAVICON);
1050 content::URLDataSource::Add(profile_, favicon_source);
1053 // Same for chrome://theme/ resources.
1054 if (permissions_data->HasHostPermission(GURL(chrome::kChromeUIThemeURL))) {
1055 ThemeSource* theme_source = new ThemeSource(profile_);
1056 content::URLDataSource::Add(profile_, theme_source);
1059 // Same for chrome://thumb/ resources.
1060 if (permissions_data->HasHostPermission(
1061 GURL(chrome::kChromeUIThumbnailURL))) {
1062 ThumbnailSource* thumbnail_source = new ThumbnailSource(profile_, false);
1063 content::URLDataSource::Add(profile_, thumbnail_source);
1067 void ExtensionService::NotifyExtensionUnloaded(
1068 const Extension* extension,
1069 UnloadedExtensionInfo::Reason reason) {
1070 UnloadedExtensionInfo details(extension, reason);
1072 registry_->TriggerOnUnloaded(extension, reason);
1074 content::NotificationService::current()->Notify(
1075 extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
1076 content::Source<Profile>(profile_),
1077 content::Details<UnloadedExtensionInfo>(&details));
1079 for (content::RenderProcessHost::iterator i(
1080 content::RenderProcessHost::AllHostsIterator());
1081 !i.IsAtEnd(); i.Advance()) {
1082 content::RenderProcessHost* host = i.GetCurrentValue();
1083 Profile* host_profile =
1084 Profile::FromBrowserContext(host->GetBrowserContext());
1085 if (host_profile->GetOriginalProfile() == profile_->GetOriginalProfile())
1086 host->Send(new ExtensionMsg_Unloaded(extension->id()));
1089 system_->UnregisterExtensionWithRequestContexts(extension->id(), reason);
1091 // TODO(kalman): Convert ExtensionSpecialStoragePolicy to a
1092 // BrowserContextKeyedService and use ExtensionRegistryObserver.
1093 profile_->GetExtensionSpecialStoragePolicy()->
1094 RevokeRightsForExtension(extension);
1096 #if defined(OS_CHROMEOS)
1097 // Revoke external file access for the extension from its file system context.
1098 // It is safe to access the extension's storage partition at this point. The
1099 // storage partition may get destroyed only after the extension gets unloaded.
1101 extensions::util::GetSiteForExtensionId(extension->id(), profile_);
1102 fileapi::FileSystemContext* filesystem_context =
1103 BrowserContext::GetStoragePartitionForSite(profile_, site)->
1104 GetFileSystemContext();
1105 if (filesystem_context && filesystem_context->external_backend()) {
1106 filesystem_context->external_backend()->
1107 RevokeAccessForExtension(extension->id());
1111 // TODO(kalman): This is broken. The crash reporter is process-wide so doesn't
1112 // work properly multi-profile. Besides which, it should be using
1113 // ExtensionRegistryObserver::OnExtensionLoaded. See http://crbug.com/355029.
1114 UpdateActiveExtensionsInCrashReporter();
1117 content::BrowserContext* ExtensionService::GetBrowserContext() const {
1118 // Implemented in the .cc file to avoid adding a profile.h dependency to
1119 // extension_service.h.
1123 bool ExtensionService::is_ready() {
1124 return ready_->is_signaled();
1127 base::SequencedTaskRunner* ExtensionService::GetFileTaskRunner() {
1128 if (file_task_runner_.get())
1129 return file_task_runner_.get();
1131 // We should be able to interrupt any part of extension install process during
1132 // shutdown. SKIP_ON_SHUTDOWN ensures that not started extension install tasks
1133 // will be ignored/deleted while we will block on started tasks.
1134 std::string token("ext_install-");
1135 token.append(profile_->GetPath().AsUTF8Unsafe());
1136 file_task_runner_ = BrowserThread::GetBlockingPool()->
1137 GetSequencedTaskRunnerWithShutdownBehavior(
1138 BrowserThread::GetBlockingPool()->GetNamedSequenceToken(token),
1139 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
1140 return file_task_runner_.get();
1143 void ExtensionService::CheckManagementPolicy() {
1144 std::vector<std::string> to_unload;
1145 std::map<std::string, Extension::DisableReason> to_disable;
1147 // Loop through the extensions list, finding extensions we need to unload or
1149 const ExtensionSet& extensions = registry_->enabled_extensions();
1150 for (ExtensionSet::const_iterator iter = extensions.begin();
1151 iter != extensions.end(); ++iter) {
1152 const Extension* extension = (iter->get());
1153 if (!system_->management_policy()->UserMayLoad(extension, NULL))
1154 to_unload.push_back(extension->id());
1155 Extension::DisableReason disable_reason = Extension::DISABLE_NONE;
1156 if (system_->management_policy()->MustRemainDisabled(
1157 extension, &disable_reason, NULL))
1158 to_disable[extension->id()] = disable_reason;
1161 for (size_t i = 0; i < to_unload.size(); ++i)
1162 UnloadExtension(to_unload[i], UnloadedExtensionInfo::REASON_DISABLE);
1164 for (std::map<std::string, Extension::DisableReason>::const_iterator i =
1165 to_disable.begin(); i != to_disable.end(); ++i)
1166 DisableExtension(i->first, i->second);
1169 void ExtensionService::CheckForUpdatesSoon() {
1170 // This can legitimately happen in unit tests.
1171 if (!updater_.get())
1174 if (AreAllExternalProvidersReady()) {
1175 updater_->CheckSoon();
1177 // Sync can start updating before all the external providers are ready
1178 // during startup. Start the update as soon as those providers are ready,
1180 update_once_all_providers_are_ready_ = true;
1184 // Some extensions will autoupdate themselves externally from Chrome. These
1185 // are typically part of some larger client application package. To support
1186 // these, the extension will register its location in the the preferences file
1187 // (and also, on Windows, in the registry) and this code will periodically
1188 // check that location for a .crx file, which it will then install locally if
1189 // a new version is available.
1190 // Errors are reported through ExtensionErrorReporter. Succcess is not
1192 void ExtensionService::CheckForExternalUpdates() {
1193 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1195 // Note that this installation is intentionally silent (since it didn't
1196 // go through the front-end). Extensions that are registered in this
1197 // way are effectively considered 'pre-bundled', and so implicitly
1198 // trusted. In general, if something has HKLM or filesystem access,
1199 // they could install an extension manually themselves anyway.
1201 // Ask each external extension provider to give us a call back for each
1202 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
1203 extensions::ProviderCollection::const_iterator i;
1204 for (i = external_extension_providers_.begin();
1205 i != external_extension_providers_.end(); ++i) {
1206 extensions::ExternalProviderInterface* provider = i->get();
1207 provider->VisitRegisteredExtension();
1210 // Do any required work that we would have done after completion of all
1212 if (external_extension_providers_.empty())
1213 OnAllExternalProvidersReady();
1216 void ExtensionService::OnExternalProviderReady(
1217 const extensions::ExternalProviderInterface* provider) {
1218 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1219 CHECK(provider->IsReady());
1221 // An external provider has finished loading. We only take action
1222 // if all of them are finished. So we check them first.
1223 if (AreAllExternalProvidersReady())
1224 OnAllExternalProvidersReady();
1227 bool ExtensionService::AreAllExternalProvidersReady() const {
1228 extensions::ProviderCollection::const_iterator i;
1229 for (i = external_extension_providers_.begin();
1230 i != external_extension_providers_.end(); ++i) {
1231 if (!i->get()->IsReady())
1237 void ExtensionService::OnAllExternalProvidersReady() {
1238 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1239 base::TimeDelta elapsed = base::Time::Now() - profile_->GetStartTime();
1240 UMA_HISTOGRAM_TIMES("Extension.ExternalProvidersReadyAfter", elapsed);
1242 // Install any pending extensions.
1243 if (update_once_all_providers_are_ready_ && updater()) {
1244 update_once_all_providers_are_ready_ = false;
1245 extensions::ExtensionUpdater::CheckParams params;
1246 params.callback = external_updates_finished_callback_;
1247 updater()->CheckNow(params);
1250 // Uninstall all the unclaimed extensions.
1251 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> extensions_info(
1252 extension_prefs_->GetInstalledExtensionsInfo());
1253 for (size_t i = 0; i < extensions_info->size(); ++i) {
1254 ExtensionInfo* info = extensions_info->at(i).get();
1255 if (Manifest::IsExternalLocation(info->extension_location))
1256 CheckExternalUninstall(info->extension_id);
1259 error_controller_->ShowErrorIfNeeded();
1261 external_install_manager_->UpdateExternalExtensionAlert();
1264 void ExtensionService::UnloadExtension(
1265 const std::string& extension_id,
1266 UnloadedExtensionInfo::Reason reason) {
1267 // Make sure the extension gets deleted after we return from this function.
1269 ExtensionRegistry::EVERYTHING & ~ExtensionRegistry::TERMINATED;
1270 scoped_refptr<const Extension> extension(
1271 registry_->GetExtensionById(extension_id, include_mask));
1273 // This method can be called via PostTask, so the extension may have been
1274 // unloaded by the time this runs.
1275 if (!extension.get()) {
1276 // In case the extension may have crashed/uninstalled. Allow the profile to
1277 // clean up its RequestContexts.
1278 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
1282 // Keep information about the extension so that we can reload it later
1283 // even if it's not permanently installed.
1284 unloaded_extension_paths_[extension->id()] = extension->path();
1286 // Clean up if the extension is meant to be enabled after a reload.
1287 reloading_extensions_.erase(extension->id());
1289 if (registry_->disabled_extensions().Contains(extension->id())) {
1290 registry_->RemoveDisabled(extension->id());
1291 // Make sure the profile cleans up its RequestContexts when an already
1292 // disabled extension is unloaded (since they are also tracking the disabled
1294 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
1295 // Don't send the unloaded notification. It was sent when the extension
1298 // Remove the extension from the enabled list.
1299 registry_->RemoveEnabled(extension->id());
1300 NotifyExtensionUnloaded(extension.get(), reason);
1303 content::NotificationService::current()->Notify(
1304 extensions::NOTIFICATION_EXTENSION_REMOVED,
1305 content::Source<Profile>(profile_),
1306 content::Details<const Extension>(extension.get()));
1309 void ExtensionService::RemoveComponentExtension(
1310 const std::string& extension_id) {
1311 scoped_refptr<const Extension> extension(
1312 GetExtensionById(extension_id, false));
1313 UnloadExtension(extension_id, UnloadedExtensionInfo::REASON_UNINSTALL);
1314 if (extension.get()) {
1315 content::NotificationService::current()->Notify(
1316 extensions::NOTIFICATION_EXTENSION_UNINSTALLED_DEPRECATED,
1317 content::Source<Profile>(profile_),
1318 content::Details<const Extension>(extension.get()));
1319 ExtensionRegistry::Get(profile_)->TriggerOnUninstalled(
1320 extension.get(), extensions::UNINSTALL_REASON_INTERNAL_MANAGEMENT);
1324 void ExtensionService::UnloadAllExtensionsForTest() {
1325 UnloadAllExtensionsInternal();
1328 void ExtensionService::ReloadExtensionsForTest() {
1329 // Calling UnloadAllExtensionsForTest here triggers a false-positive presubmit
1330 // warning about calling test code in production.
1331 UnloadAllExtensionsInternal();
1332 component_loader_->LoadAll();
1333 extensions::InstalledLoader(this).LoadAllExtensions();
1334 // Don't call SetReadyAndNotifyListeners() since tests call this multiple
1338 void ExtensionService::SetReadyAndNotifyListeners() {
1340 content::NotificationService::current()->Notify(
1341 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED,
1342 content::Source<Profile>(profile_),
1343 content::NotificationService::NoDetails());
1346 void ExtensionService::OnLoadedInstalledExtensions() {
1350 OnBlacklistUpdated();
1353 void ExtensionService::AddExtension(const Extension* extension) {
1354 // TODO(jstritar): We may be able to get rid of this branch by overriding the
1355 // default extension state to DISABLED when the --disable-extensions flag
1356 // is set (http://crbug.com/29067).
1357 if (!extensions_enabled() &&
1358 !extension->is_theme() &&
1359 extension->location() != Manifest::COMPONENT &&
1360 !Manifest::IsExternalLocation(extension->location())) {
1364 bool is_extension_upgrade = false;
1365 bool is_extension_installed = false;
1366 const Extension* old = GetInstalledExtension(extension->id());
1368 is_extension_installed = true;
1369 int version_compare_result =
1370 extension->version()->CompareTo(*(old->version()));
1371 is_extension_upgrade = version_compare_result > 0;
1372 // Other than for unpacked extensions, CrxInstaller should have guaranteed
1373 // that we aren't downgrading.
1374 if (!Manifest::IsUnpackedLocation(extension->location()))
1375 CHECK_GE(version_compare_result, 0);
1377 system_->runtime_data()->SetBeingUpgraded(extension, is_extension_upgrade);
1379 // The extension is now loaded, remove its data from unloaded extension map.
1380 unloaded_extension_paths_.erase(extension->id());
1382 // If a terminated extension is loaded, remove it from the terminated list.
1383 UntrackTerminatedExtension(extension->id());
1385 // If the extension was disabled for a reload, then enable it.
1386 bool reloading = reloading_extensions_.erase(extension->id()) > 0;
1388 // Check if the extension's privileges have changed and mark the
1389 // extension disabled if necessary.
1390 CheckPermissionsIncrease(extension, is_extension_installed);
1392 if (is_extension_installed && !reloading) {
1393 // To upgrade an extension in place, unload the old one and then load the
1394 // new one. ReloadExtension disables the extension, which is sufficient.
1395 UnloadExtension(extension->id(), UnloadedExtensionInfo::REASON_UPDATE);
1398 if (extension_prefs_->IsExtensionBlacklisted(extension->id())) {
1399 // Only prefs is checked for the blacklist. We rely on callers to check the
1400 // blacklist before calling into here, e.g. CrxInstaller checks before
1401 // installation then threads through the install and pending install flow
1402 // of this class, and we check when loading installed extensions.
1403 registry_->AddBlacklisted(extension);
1404 } else if (!reloading &&
1405 extension_prefs_->IsExtensionDisabled(extension->id())) {
1406 registry_->AddDisabled(extension);
1407 if (extension_sync_service_)
1408 extension_sync_service_->SyncExtensionChangeIfNeeded(*extension);
1409 content::NotificationService::current()->Notify(
1410 extensions::NOTIFICATION_EXTENSION_UPDATE_DISABLED,
1411 content::Source<Profile>(profile_),
1412 content::Details<const Extension>(extension));
1414 // Show the extension disabled error if a permissions increase or a remote
1415 // installation is the reason it was disabled, and no other reasons exist.
1416 int reasons = extension_prefs_->GetDisableReasons(extension->id());
1417 const int kReasonMask = Extension::DISABLE_PERMISSIONS_INCREASE |
1418 Extension::DISABLE_REMOTE_INSTALL;
1419 if (reasons & kReasonMask && !(reasons & ~kReasonMask)) {
1420 extensions::AddExtensionDisabledError(
1423 extension_prefs_->HasDisableReason(
1424 extension->id(), Extension::DISABLE_REMOTE_INSTALL));
1426 } else if (reloading) {
1427 // Replace the old extension with the new version.
1428 CHECK(!registry_->AddDisabled(extension));
1429 EnableExtension(extension->id());
1431 // All apps that are displayed in the launcher are ordered by their ordinals
1432 // so we must ensure they have valid ordinals.
1433 if (extension->RequiresSortOrdinal()) {
1434 extension_prefs_->app_sorting()->SetExtensionVisible(
1436 extension->ShouldDisplayInNewTabPage() &&
1437 !extension_prefs_->IsEphemeralApp(extension->id()));
1438 if (!extension_prefs_->IsEphemeralApp(extension->id())) {
1439 extension_prefs_->app_sorting()->EnsureValidOrdinals(
1440 extension->id(), syncer::StringOrdinal());
1444 registry_->AddEnabled(extension);
1445 if (extension_sync_service_)
1446 extension_sync_service_->SyncExtensionChangeIfNeeded(*extension);
1447 NotifyExtensionLoaded(extension);
1449 system_->runtime_data()->SetBeingUpgraded(extension, false);
1452 void ExtensionService::AddComponentExtension(const Extension* extension) {
1453 const std::string old_version_string(
1454 extension_prefs_->GetVersionString(extension->id()));
1455 const Version old_version(old_version_string);
1457 VLOG(1) << "AddComponentExtension " << extension->name();
1458 if (!old_version.IsValid() || !old_version.Equals(*extension->version())) {
1459 VLOG(1) << "Component extension " << extension->name() << " ("
1460 << extension->id() << ") installing/upgrading from '"
1461 << old_version_string << "' to " << extension->version()->GetString();
1463 AddNewOrUpdatedExtension(extension,
1465 extensions::kInstallFlagNone,
1466 syncer::StringOrdinal(),
1471 AddExtension(extension);
1474 void ExtensionService::CheckPermissionsIncrease(const Extension* extension,
1475 bool is_extension_installed) {
1476 extensions::PermissionsUpdater(profile_).InitializePermissions(extension);
1478 // We keep track of all permissions the user has granted each extension.
1479 // This allows extensions to gracefully support backwards compatibility
1480 // by including unknown permissions in their manifests. When the user
1481 // installs the extension, only the recognized permissions are recorded.
1482 // When the unknown permissions become recognized (e.g., through browser
1483 // upgrade), we can prompt the user to accept these new permissions.
1484 // Extensions can also silently upgrade to less permissions, and then
1485 // silently upgrade to a version that adds these permissions back.
1487 // For example, pretend that Chrome 10 includes a permission "omnibox"
1488 // for an API that adds suggestions to the omnibox. An extension can
1489 // maintain backwards compatibility while still having "omnibox" in the
1490 // manifest. If a user installs the extension on Chrome 9, the browser
1491 // will record the permissions it recognized, not including "omnibox."
1492 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
1493 // will disable the extension and prompt the user to approve the increase
1494 // in privileges. The extension could then release a new version that
1495 // removes the "omnibox" permission. When the user upgrades, Chrome will
1496 // still remember that "omnibox" had been granted, so that if the
1497 // extension once again includes "omnibox" in an upgrade, the extension
1498 // can upgrade without requiring this user's approval.
1499 int disable_reasons = extension_prefs_->GetDisableReasons(extension->id());
1501 bool auto_grant_permission =
1502 (!is_extension_installed && extension->was_installed_by_default()) ||
1503 extensions::ExtensionsBrowserClient::Get()->IsRunningInForcedAppMode();
1504 // Silently grant all active permissions to default apps only on install.
1505 // After install they should behave like other apps.
1506 // Silently grant all active permissions to apps install in kiosk mode on both
1507 // install and update.
1508 if (auto_grant_permission)
1509 GrantPermissions(extension);
1511 bool is_privilege_increase = false;
1512 // We only need to compare the granted permissions to the current permissions
1513 // if the extension is not allowed to silently increase its permissions.
1514 if (!extensions::PermissionsData::CanSilentlyIncreasePermissions(extension) &&
1515 !auto_grant_permission) {
1516 // Add all the recognized permissions if the granted permissions list
1517 // hasn't been initialized yet.
1518 scoped_refptr<PermissionSet> granted_permissions =
1519 extension_prefs_->GetGrantedPermissions(extension->id());
1520 CHECK(granted_permissions.get());
1522 // Here, we check if an extension's privileges have increased in a manner
1523 // that requires the user's approval. This could occur because the browser
1524 // upgraded and recognized additional privileges, or an extension upgrades
1525 // to a version that requires additional privileges.
1526 is_privilege_increase =
1527 extensions::PermissionMessageProvider::Get()->IsPrivilegeIncrease(
1528 granted_permissions,
1529 extension->permissions_data()->active_permissions().get(),
1530 extension->GetType());
1533 if (is_extension_installed) {
1534 // If the extension was already disabled, suppress any alerts for becoming
1535 // disabled on permissions increase.
1536 bool previously_disabled =
1537 extension_prefs_->IsExtensionDisabled(extension->id());
1538 // Legacy disabled extensions do not have a disable reason. Infer that if
1539 // there was no permission increase, it was likely disabled by the user.
1540 if (previously_disabled && disable_reasons == Extension::DISABLE_NONE &&
1541 !extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
1542 disable_reasons |= Extension::DISABLE_USER_ACTION;
1544 // Extensions that came to us disabled from sync need a similar inference,
1545 // except based on the new version's permissions.
1546 if (previously_disabled &&
1547 disable_reasons == Extension::DISABLE_UNKNOWN_FROM_SYNC) {
1548 // Remove the DISABLE_UNKNOWN_FROM_SYNC reason.
1549 extension_prefs_->ClearDisableReasons(extension->id());
1550 if (!is_privilege_increase)
1551 disable_reasons |= Extension::DISABLE_USER_ACTION;
1553 disable_reasons &= ~Extension::DISABLE_UNKNOWN_FROM_SYNC;
1556 // Extension has changed permissions significantly. Disable it. A
1557 // notification should be sent by the caller. If the extension is already
1558 // disabled because it was installed remotely, don't add another disable
1559 // reason, but instead always set the "did escalate permissions" flag, to
1560 // ensure enabling it will always show a warning.
1561 if (disable_reasons == Extension::DISABLE_REMOTE_INSTALL) {
1562 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
1563 } else if (is_privilege_increase) {
1564 disable_reasons |= Extension::DISABLE_PERMISSIONS_INCREASE;
1565 if (!extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
1566 RecordPermissionMessagesHistogram(extension,
1567 "Extensions.Permissions_AutoDisable2");
1569 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
1570 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
1572 if (disable_reasons != Extension::DISABLE_NONE) {
1573 extension_prefs_->AddDisableReason(
1575 static_cast<Extension::DisableReason>(disable_reasons));
1579 void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
1580 std::set<std::string> extension_ids;
1581 const ExtensionSet& extensions = registry_->enabled_extensions();
1582 for (ExtensionSet::const_iterator iter = extensions.begin();
1583 iter != extensions.end(); ++iter) {
1584 const Extension* extension = iter->get();
1585 if (!extension->is_theme() && extension->location() != Manifest::COMPONENT)
1586 extension_ids.insert(extension->id());
1589 // TODO(kalman): This is broken. ExtensionService is per-profile.
1590 // crash_keys::SetActiveExtensions is per-process. See
1591 // http://crbug.com/355029.
1592 crash_keys::SetActiveExtensions(extension_ids);
1595 void ExtensionService::OnExtensionInstalled(
1596 const Extension* extension,
1597 const syncer::StringOrdinal& page_ordinal,
1598 int install_flags) {
1599 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1601 const std::string& id = extension->id();
1602 bool initial_enable = ShouldEnableOnInstall(extension);
1603 std::string install_parameter;
1604 const extensions::PendingExtensionInfo* pending_extension_info =
1605 pending_extension_manager()->GetById(id);
1606 if (pending_extension_info) {
1607 if (!pending_extension_info->ShouldAllowInstall(extension)) {
1608 pending_extension_manager()->Remove(id);
1610 LOG(WARNING) << "ShouldAllowInstall() returned false for "
1611 << id << " of type " << extension->GetType()
1612 << " and update URL "
1613 << extensions::ManifestURL::GetUpdateURL(extension).spec()
1614 << "; not installing";
1616 // Delete the extension directory since we're not going to
1618 if (!GetFileTaskRunner()->PostTask(
1620 base::Bind(&extensions::file_util::DeleteFile,
1628 install_parameter = pending_extension_info->install_parameter();
1629 pending_extension_manager()->Remove(id);
1631 // We explicitly want to re-enable an uninstalled external
1632 // extension; if we're here, that means the user is manually
1633 // installing the extension.
1634 if (extension_prefs_->IsExternalExtensionUninstalled(id)) {
1635 initial_enable = true;
1639 // Unsupported requirements overrides the management policy.
1640 if (install_flags & extensions::kInstallFlagHasRequirementErrors) {
1641 initial_enable = false;
1642 extension_prefs_->AddDisableReason(
1643 id, Extension::DISABLE_UNSUPPORTED_REQUIREMENT);
1644 // If the extension was disabled because of unsupported requirements but
1645 // now supports all requirements after an update and there are not other
1646 // disable reasons, enable it.
1647 } else if (extension_prefs_->GetDisableReasons(id) ==
1648 Extension::DISABLE_UNSUPPORTED_REQUIREMENT) {
1649 initial_enable = true;
1650 extension_prefs_->ClearDisableReasons(id);
1653 if (install_flags & extensions::kInstallFlagIsBlacklistedForMalware) {
1654 // Installation of a blacklisted extension can happen from sync, policy,
1655 // etc, where to maintain consistency we need to install it, just never
1656 // load it (see AddExtension). Usually it should be the job of callers to
1657 // incercept blacklisted extension earlier (e.g. CrxInstaller, before even
1658 // showing the install dialogue).
1659 extension_prefs_->AcknowledgeBlacklistedExtension(id);
1660 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.SilentInstall",
1661 extension->location(),
1662 Manifest::NUM_LOCATIONS);
1665 if (!GetInstalledExtension(extension->id())) {
1666 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
1667 extension->GetType(), 100);
1668 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallSource",
1669 extension->location(), Manifest::NUM_LOCATIONS);
1670 RecordPermissionMessagesHistogram(extension,
1671 "Extensions.Permissions_Install2");
1673 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateType",
1674 extension->GetType(), 100);
1675 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateSource",
1676 extension->location(), Manifest::NUM_LOCATIONS);
1678 // A fully installed app cannot be demoted to an ephemeral app.
1679 if ((install_flags & extensions::kInstallFlagIsEphemeral) &&
1680 !extension_prefs_->IsEphemeralApp(id)) {
1681 install_flags &= ~static_cast<int>(extensions::kInstallFlagIsEphemeral);
1685 const Extension::State initial_state =
1686 initial_enable ? Extension::ENABLED : Extension::DISABLED;
1687 if (ShouldDelayExtensionUpdate(
1689 !!(install_flags & extensions::kInstallFlagInstallImmediately))) {
1690 extension_prefs_->SetDelayedInstallInfo(
1694 extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE,
1698 // Transfer ownership of |extension|.
1699 delayed_installs_.Insert(extension);
1701 // Notify observers that app update is available.
1702 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
1703 OnAppUpdateAvailable(extension));
1707 extensions::SharedModuleService::ImportStatus status =
1708 shared_module_service_->SatisfyImports(extension);
1709 if (installs_delayed_for_gc_) {
1710 extension_prefs_->SetDelayedInstallInfo(
1714 extensions::ExtensionPrefs::DELAY_REASON_GC,
1717 delayed_installs_.Insert(extension);
1718 } else if (status != SharedModuleService::IMPORT_STATUS_OK) {
1719 if (status == SharedModuleService::IMPORT_STATUS_UNSATISFIED) {
1720 extension_prefs_->SetDelayedInstallInfo(
1724 extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IMPORTS,
1727 delayed_installs_.Insert(extension);
1730 AddNewOrUpdatedExtension(extension,
1738 void ExtensionService::AddNewOrUpdatedExtension(
1739 const Extension* extension,
1740 Extension::State initial_state,
1742 const syncer::StringOrdinal& page_ordinal,
1743 const std::string& install_parameter) {
1744 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1745 bool was_ephemeral = extension_prefs_->IsEphemeralApp(extension->id());
1746 extension_prefs_->OnExtensionInstalled(
1747 extension, initial_state, page_ordinal, install_flags, install_parameter);
1748 delayed_installs_.Remove(extension->id());
1749 if (InstallVerifier::NeedsVerification(*extension))
1750 system_->install_verifier()->VerifyExtension(extension->id());
1751 FinishInstallation(extension, was_ephemeral);
1754 void ExtensionService::MaybeFinishDelayedInstallation(
1755 const std::string& extension_id) {
1756 // Check if the extension already got installed.
1757 if (!delayed_installs_.Contains(extension_id))
1759 extensions::ExtensionPrefs::DelayReason reason =
1760 extension_prefs_->GetDelayedInstallReason(extension_id);
1762 // Check if the extension is idle. DELAY_REASON_NONE is used for older
1763 // preferences files that will not have set this field but it was previously
1764 // only used for idle updates.
1765 if ((reason == extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE ||
1766 reason == extensions::ExtensionPrefs::DELAY_REASON_NONE) &&
1767 is_ready() && !extensions::util::IsExtensionIdle(extension_id, profile_))
1770 const Extension* extension = delayed_installs_.GetByID(extension_id);
1771 if (reason == extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IMPORTS) {
1772 extensions::SharedModuleService::ImportStatus status =
1773 shared_module_service_->SatisfyImports(extension);
1774 if (status != SharedModuleService::IMPORT_STATUS_OK) {
1775 if (status == SharedModuleService::IMPORT_STATUS_UNRECOVERABLE) {
1776 delayed_installs_.Remove(extension_id);
1777 // Make sure no version of the extension is actually installed, (i.e.,
1778 // that this delayed install was not an update).
1779 CHECK(!extension_prefs_->GetInstalledExtensionInfo(extension_id).get());
1780 extension_prefs_->DeleteExtensionPrefs(extension_id);
1786 FinishDelayedInstallation(extension_id);
1789 void ExtensionService::FinishDelayedInstallation(
1790 const std::string& extension_id) {
1791 scoped_refptr<const Extension> extension(
1792 GetPendingExtensionUpdate(extension_id));
1793 CHECK(extension.get());
1794 delayed_installs_.Remove(extension_id);
1796 bool was_ephemeral = extension_prefs_->IsEphemeralApp(extension->id());
1797 if (!extension_prefs_->FinishDelayedInstallInfo(extension_id))
1800 FinishInstallation(extension.get(), was_ephemeral);
1803 void ExtensionService::FinishInstallation(
1804 const Extension* extension, bool was_ephemeral) {
1805 const extensions::Extension* existing_extension =
1806 GetInstalledExtension(extension->id());
1807 bool is_update = false;
1808 std::string old_name;
1809 if (existing_extension) {
1811 old_name = existing_extension->name();
1813 bool from_ephemeral =
1814 was_ephemeral && !extension_prefs_->IsEphemeralApp(extension->id());
1815 extensions::InstalledExtensionInfo details(
1816 extension, is_update, from_ephemeral, old_name);
1817 content::NotificationService::current()->Notify(
1818 extensions::NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED,
1819 content::Source<Profile>(profile_),
1820 content::Details<const extensions::InstalledExtensionInfo>(&details));
1822 registry_->TriggerOnWillBeInstalled(
1823 extension, is_update, from_ephemeral, old_name);
1825 // Unpacked extensions default to allowing file access, but if that has been
1826 // overridden, don't reset the value.
1827 if (Manifest::ShouldAlwaysAllowFileAccess(extension->location()) &&
1828 !extension_prefs_->HasAllowFileAccessSetting(extension->id())) {
1829 extension_prefs_->SetAllowFileAccess(extension->id(), true);
1832 AddExtension(extension);
1834 // Notify observers that need to know when an installation is complete.
1835 registry_->TriggerOnInstalled(extension, is_update);
1837 // Check extensions that may have been delayed only because this shared module
1838 // was not available.
1839 if (SharedModuleInfo::IsSharedModule(extension))
1840 MaybeFinishDelayedInstallations();
1843 void ExtensionService::PromoteEphemeralApp(
1844 const extensions::Extension* extension, bool is_from_sync) {
1845 DCHECK(GetInstalledExtension(extension->id()) &&
1846 extension_prefs_->IsEphemeralApp(extension->id()));
1848 if (extension->RequiresSortOrdinal()) {
1849 extension_prefs_->app_sorting()->SetExtensionVisible(
1850 extension->id(), extension->ShouldDisplayInNewTabPage());
1852 if (!is_from_sync) {
1853 // Reset the sort ordinals of the app to ensure it is added to the default
1854 // position, like newly installed apps would.
1855 extension_prefs_->app_sorting()->ClearOrdinals(extension->id());
1858 extension_prefs_->app_sorting()->EnsureValidOrdinals(
1859 extension->id(), syncer::StringOrdinal());
1862 // Remove the ephemeral flags from the preferences.
1863 extension_prefs_->OnEphemeralAppPromoted(extension->id());
1865 // Fire install-related events to allow observers to handle the promotion
1866 // of the ephemeral app.
1867 extensions::InstalledExtensionInfo details(
1869 true /* is update */,
1870 true /* from ephemeral */,
1871 extension->name() /* old name */);
1872 content::NotificationService::current()->Notify(
1873 extensions::NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED,
1874 content::Source<Profile>(profile_),
1875 content::Details<const extensions::InstalledExtensionInfo>(&details));
1877 registry_->TriggerOnWillBeInstalled(
1879 true /* is update */,
1880 true /* from ephemeral */,
1881 extension->name() /* old name */);
1883 if (registry_->enabled_extensions().Contains(extension->id())) {
1884 // If the app is already enabled and loaded, fire the load events to allow
1885 // observers to handle the promotion of the ephemeral app.
1886 content::NotificationService::current()->Notify(
1887 extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
1888 content::Source<Profile>(profile_),
1889 content::Details<const Extension>(extension));
1891 registry_->TriggerOnLoaded(extension);
1893 // Cached ephemeral apps may be updated and disabled due to permissions
1894 // increase. The app can be enabled (as long as no other disable reasons
1895 // exist) as the install was user-acknowledged.
1896 int disable_mask = Extension::DISABLE_NONE;
1898 disable_mask |= Extension::DISABLE_PERMISSIONS_INCREASE;
1900 int other_disable_reasons =
1901 extension_prefs_->GetDisableReasons(extension->id()) & ~disable_mask;
1902 if (!other_disable_reasons) {
1903 if (extension_prefs_->DidExtensionEscalatePermissions(extension->id()))
1904 GrantPermissionsAndEnableExtension(extension);
1906 EnableExtension(extension->id());
1910 registry_->TriggerOnInstalled(extension, true);
1912 if (!is_from_sync && extension_sync_service_)
1913 extension_sync_service_->SyncExtensionChangeIfNeeded(*extension);
1916 const Extension* ExtensionService::GetPendingExtensionUpdate(
1917 const std::string& id) const {
1918 return delayed_installs_.GetByID(id);
1921 void ExtensionService::TrackTerminatedExtension(const Extension* extension) {
1922 // No need to check for duplicates; inserting a duplicate is a no-op.
1923 registry_->AddTerminated(make_scoped_refptr(extension));
1924 extensions_being_terminated_.erase(extension->id());
1925 UnloadExtension(extension->id(), UnloadedExtensionInfo::REASON_TERMINATE);
1928 void ExtensionService::TerminateExtension(const std::string& extension_id) {
1929 const Extension* extension = GetInstalledExtension(extension_id);
1930 TrackTerminatedExtension(extension);
1933 void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
1934 std::string lowercase_id = base::StringToLowerASCII(id);
1935 const Extension* extension =
1936 registry_->terminated_extensions().GetByID(lowercase_id);
1937 registry_->RemoveTerminated(lowercase_id);
1939 content::NotificationService::current()->Notify(
1940 extensions::NOTIFICATION_EXTENSION_REMOVED,
1941 content::Source<Profile>(profile_),
1942 content::Details<const Extension>(extension));
1946 const Extension* ExtensionService::GetInstalledExtension(
1947 const std::string& id) const {
1948 return registry_->GetExtensionById(id, ExtensionRegistry::EVERYTHING);
1951 bool ExtensionService::OnExternalExtensionFileFound(
1952 const std::string& id,
1953 const Version* version,
1954 const base::FilePath& path,
1955 Manifest::Location location,
1957 bool mark_acknowledged) {
1958 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1959 CHECK(Extension::IdIsValid(id));
1960 if (extension_prefs_->IsExternalExtensionUninstalled(id))
1963 // Before even bothering to unpack, check and see if we already have this
1964 // version. This is important because these extensions are going to get
1965 // installed on every startup.
1966 const Extension* existing = GetExtensionById(id, true);
1969 // The default apps will have the location set as INTERNAL. Since older
1970 // default apps are installed as EXTERNAL, we override them. However, if the
1971 // app is already installed as internal, then do the version check.
1972 // TODO(grv) : Remove after Q1-2013.
1973 bool is_default_apps_migration =
1974 (location == Manifest::INTERNAL &&
1975 Manifest::IsExternalLocation(existing->location()));
1977 if (!is_default_apps_migration) {
1980 switch (existing->version()->CompareTo(*version)) {
1981 case -1: // existing version is older, we should upgrade
1983 case 0: // existing version is same, do nothing
1985 case 1: // existing version is newer, uh-oh
1986 LOG(WARNING) << "Found external version of extension " << id
1987 << "that is older than current version. Current version "
1988 << "is: " << existing->VersionString() << ". New "
1989 << "version is: " << version->GetString()
1990 << ". Keeping current version.";
1996 // If the extension is already pending, don't start an install.
1997 if (!pending_extension_manager()->AddFromExternalFile(
1998 id, location, *version, creation_flags, mark_acknowledged)) {
2002 // no client (silent install)
2003 scoped_refptr<CrxInstaller> installer(CrxInstaller::CreateSilent(this));
2004 installer->set_install_source(location);
2005 installer->set_expected_id(id);
2006 installer->set_expected_version(*version);
2007 installer->set_install_cause(extension_misc::INSTALL_CAUSE_EXTERNAL_FILE);
2008 installer->set_creation_flags(creation_flags);
2009 #if defined(OS_CHROMEOS)
2010 extensions::InstallLimiter::Get(profile_)->Add(installer, path);
2012 installer->InstallCrx(path);
2015 // Depending on the source, a new external extension might not need a user
2016 // notification on installation. For such extensions, mark them acknowledged
2017 // now to suppress the notification.
2018 if (mark_acknowledged)
2019 external_install_manager_->AcknowledgeExternalExtension(id);
2024 void ExtensionService::DidCreateRenderViewForBackgroundPage(
2025 extensions::ExtensionHost* host) {
2026 OrphanedDevTools::iterator iter =
2027 orphaned_dev_tools_.find(host->extension_id());
2028 if (iter == orphaned_dev_tools_.end())
2031 iter->second->ConnectWebContents(host->host_contents());
2032 orphaned_dev_tools_.erase(iter);
2035 void ExtensionService::Observe(int type,
2036 const content::NotificationSource& source,
2037 const content::NotificationDetails& details) {
2039 case chrome::NOTIFICATION_APP_TERMINATING:
2040 // Shutdown has started. Don't start any more extension installs.
2041 // (We cannot use ExtensionService::Shutdown() for this because it
2042 // happens too late in browser teardown.)
2043 browser_terminating_ = true;
2045 case extensions::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
2047 content::Source<Profile>(source).ptr()->GetOriginalProfile()) {
2051 extensions::ExtensionHost* host =
2052 content::Details<extensions::ExtensionHost>(details).ptr();
2054 // If the extension is already being terminated, there is nothing left to
2056 if (!extensions_being_terminated_.insert(host->extension_id()).second)
2059 // Mark the extension as terminated and Unload it. We want it to
2060 // be in a consistent state: either fully working or not loaded
2061 // at all, but never half-crashed. We do it in a PostTask so
2062 // that other handlers of this notification will still have
2063 // access to the Extension and ExtensionHost.
2064 base::MessageLoop::current()->PostTask(
2067 &ExtensionService::TrackTerminatedExtension,
2069 host->extension()));
2072 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
2073 content::RenderProcessHost* process =
2074 content::Source<content::RenderProcessHost>(source).ptr();
2075 Profile* host_profile =
2076 Profile::FromBrowserContext(process->GetBrowserContext());
2077 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2080 extensions::ProcessMap* process_map =
2081 extensions::ProcessMap::Get(profile_);
2082 if (process_map->Contains(process->GetID())) {
2083 // An extension process was terminated, this might have resulted in an
2084 // app or extension becoming idle.
2085 std::set<std::string> extension_ids =
2086 process_map->GetExtensionsInProcess(process->GetID());
2087 for (std::set<std::string>::const_iterator it = extension_ids.begin();
2088 it != extension_ids.end(); ++it) {
2089 if (delayed_installs_.Contains(*it)) {
2090 base::MessageLoop::current()->PostDelayedTask(
2092 base::Bind(&ExtensionService::MaybeFinishDelayedInstallation,
2094 base::TimeDelta::FromSeconds(kUpdateIdleDelay));
2099 process_map->RemoveAllFromProcess(process->GetID());
2100 BrowserThread::PostTask(
2103 base::Bind(&extensions::InfoMap::UnregisterAllExtensionsInProcess,
2104 system_->info_map(),
2108 case chrome::NOTIFICATION_UPGRADE_RECOMMENDED: {
2109 // Notify observers that chrome update is available.
2110 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
2111 OnChromeUpdateAvailable());
2114 case chrome::NOTIFICATION_PROFILE_DESTRUCTION_STARTED: {
2115 OnProfileDestructionStarted();
2120 NOTREACHED() << "Unexpected notification type.";
2124 void ExtensionService::OnExtensionInstallPrefChanged() {
2125 error_controller_->ShowErrorIfNeeded();
2126 CheckManagementPolicy();
2129 bool ExtensionService::ShouldEnableOnInstall(const Extension* extension) {
2130 // Extensions installed by policy can't be disabled. So even if a previous
2131 // installation disabled the extension, make sure it is now enabled.
2132 if (system_->management_policy()->MustRemainEnabled(extension, NULL))
2135 if (extension_prefs_->IsExtensionDisabled(extension->id()))
2138 if (FeatureSwitch::prompt_for_external_extensions()->IsEnabled()) {
2139 // External extensions are initially disabled. We prompt the user before
2140 // enabling them. Hosted apps are excepted because they are not dangerous
2141 // (they need to be launched by the user anyway).
2142 if (extension->GetType() != Manifest::TYPE_HOSTED_APP &&
2143 Manifest::IsExternalLocation(extension->location()) &&
2144 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id())) {
2152 bool ExtensionService::ShouldDelayExtensionUpdate(
2153 const std::string& extension_id,
2154 bool install_immediately) const {
2155 const char kOnUpdateAvailableEvent[] = "runtime.onUpdateAvailable";
2157 // If delayed updates are globally disabled, or just for this extension,
2159 if (!install_updates_when_idle_ || install_immediately)
2162 const Extension* old = GetInstalledExtension(extension_id);
2163 // If there is no old extension, this is not an update, so don't delay.
2167 if (extensions::BackgroundInfo::HasPersistentBackgroundPage(old)) {
2168 // Delay installation if the extension listens for the onUpdateAvailable
2170 return system_->event_router()->ExtensionHasEventListener(
2171 extension_id, kOnUpdateAvailableEvent);
2173 // Delay installation if the extension is not idle.
2174 return !extensions::util::IsExtensionIdle(extension_id, profile_);
2178 void ExtensionService::OnGarbageCollectIsolatedStorageStart() {
2179 DCHECK(!installs_delayed_for_gc_);
2180 installs_delayed_for_gc_ = true;
2183 void ExtensionService::OnGarbageCollectIsolatedStorageFinished() {
2184 DCHECK(installs_delayed_for_gc_);
2185 installs_delayed_for_gc_ = false;
2186 MaybeFinishDelayedInstallations();
2189 void ExtensionService::MaybeFinishDelayedInstallations() {
2190 std::vector<std::string> to_be_installed;
2191 for (ExtensionSet::const_iterator it = delayed_installs_.begin();
2192 it != delayed_installs_.end();
2194 to_be_installed.push_back((*it)->id());
2196 for (std::vector<std::string>::const_iterator it = to_be_installed.begin();
2197 it != to_be_installed.end();
2199 MaybeFinishDelayedInstallation(*it);
2203 void ExtensionService::OnBlacklistUpdated() {
2204 blacklist_->GetBlacklistedIDs(
2205 registry_->GenerateInstalledExtensionsSet()->GetIDs(),
2206 base::Bind(&ExtensionService::ManageBlacklist, AsWeakPtr()));
2209 void ExtensionService::ManageBlacklist(
2210 const extensions::Blacklist::BlacklistStateMap& state_map) {
2211 DCHECK_CURRENTLY_ON(BrowserThread::UI);
2213 std::set<std::string> blocked;
2214 ExtensionIdSet greylist;
2215 ExtensionIdSet unchanged;
2216 for (extensions::Blacklist::BlacklistStateMap::const_iterator it =
2218 it != state_map.end();
2220 switch (it->second) {
2221 case extensions::NOT_BLACKLISTED:
2224 case extensions::BLACKLISTED_MALWARE:
2225 blocked.insert(it->first);
2228 case extensions::BLACKLISTED_SECURITY_VULNERABILITY:
2229 case extensions::BLACKLISTED_CWS_POLICY_VIOLATION:
2230 case extensions::BLACKLISTED_POTENTIALLY_UNWANTED:
2231 greylist.insert(it->first);
2234 case extensions::BLACKLISTED_UNKNOWN:
2235 unchanged.insert(it->first);
2240 UpdateBlockedExtensions(blocked, unchanged);
2241 UpdateGreylistedExtensions(greylist, unchanged, state_map);
2243 error_controller_->ShowErrorIfNeeded();
2247 void Partition(const ExtensionIdSet& before,
2248 const ExtensionIdSet& after,
2249 const ExtensionIdSet& unchanged,
2250 ExtensionIdSet* no_longer,
2251 ExtensionIdSet* not_yet) {
2252 *not_yet = base::STLSetDifference<ExtensionIdSet>(after, before);
2253 *no_longer = base::STLSetDifference<ExtensionIdSet>(before, after);
2254 *no_longer = base::STLSetDifference<ExtensionIdSet>(*no_longer, unchanged);
2258 void ExtensionService::UpdateBlockedExtensions(
2259 const ExtensionIdSet& blocked,
2260 const ExtensionIdSet& unchanged) {
2261 ExtensionIdSet not_yet_blocked, no_longer_blocked;
2262 Partition(registry_->blacklisted_extensions().GetIDs(),
2264 &no_longer_blocked, ¬_yet_blocked);
2266 for (ExtensionIdSet::iterator it = no_longer_blocked.begin();
2267 it != no_longer_blocked.end(); ++it) {
2268 scoped_refptr<const Extension> extension =
2269 registry_->blacklisted_extensions().GetByID(*it);
2270 if (!extension.get()) {
2271 NOTREACHED() << "Extension " << *it << " no longer blocked, "
2272 << "but it was never blocked.";
2275 registry_->RemoveBlacklisted(*it);
2276 extension_prefs_->SetExtensionBlacklisted(extension->id(), false);
2277 AddExtension(extension.get());
2278 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.UnblacklistInstalled",
2279 extension->location(),
2280 Manifest::NUM_LOCATIONS);
2283 for (ExtensionIdSet::iterator it = not_yet_blocked.begin();
2284 it != not_yet_blocked.end(); ++it) {
2285 scoped_refptr<const Extension> extension = GetInstalledExtension(*it);
2286 if (!extension.get()) {
2287 NOTREACHED() << "Extension " << *it << " needs to be "
2288 << "blacklisted, but it's not installed.";
2291 registry_->AddBlacklisted(extension);
2292 extension_prefs_->SetExtensionBlacklistState(
2293 extension->id(), extensions::BLACKLISTED_MALWARE);
2294 UnloadExtension(*it, UnloadedExtensionInfo::REASON_BLACKLIST);
2295 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.BlacklistInstalled",
2296 extension->location(), Manifest::NUM_LOCATIONS);
2300 // TODO(oleg): UMA logging
2301 void ExtensionService::UpdateGreylistedExtensions(
2302 const ExtensionIdSet& greylist,
2303 const ExtensionIdSet& unchanged,
2304 const extensions::Blacklist::BlacklistStateMap& state_map) {
2305 ExtensionIdSet not_yet_greylisted, no_longer_greylisted;
2306 Partition(greylist_.GetIDs(),
2307 greylist, unchanged,
2308 &no_longer_greylisted, ¬_yet_greylisted);
2310 for (ExtensionIdSet::iterator it = no_longer_greylisted.begin();
2311 it != no_longer_greylisted.end(); ++it) {
2312 scoped_refptr<const Extension> extension = greylist_.GetByID(*it);
2313 if (!extension.get()) {
2314 NOTREACHED() << "Extension " << *it << " no longer greylisted, "
2315 << "but it was not marked as greylisted.";
2319 greylist_.Remove(*it);
2320 extension_prefs_->SetExtensionBlacklistState(extension->id(),
2321 extensions::NOT_BLACKLISTED);
2322 if (extension_prefs_->GetDisableReasons(extension->id()) &
2323 extensions::Extension::DISABLE_GREYLIST)
2324 EnableExtension(*it);
2327 for (ExtensionIdSet::iterator it = not_yet_greylisted.begin();
2328 it != not_yet_greylisted.end(); ++it) {
2329 scoped_refptr<const Extension> extension = GetInstalledExtension(*it);
2330 if (!extension.get()) {
2331 NOTREACHED() << "Extension " << *it << " needs to be "
2332 << "disabled, but it's not installed.";
2335 greylist_.Insert(extension);
2336 extension_prefs_->SetExtensionBlacklistState(extension->id(),
2337 state_map.find(*it)->second);
2338 if (registry_->enabled_extensions().Contains(extension->id()))
2339 DisableExtension(*it, extensions::Extension::DISABLE_GREYLIST);
2343 void ExtensionService::AddUpdateObserver(extensions::UpdateObserver* observer) {
2344 update_observers_.AddObserver(observer);
2347 void ExtensionService::RemoveUpdateObserver(
2348 extensions::UpdateObserver* observer) {
2349 update_observers_.RemoveObserver(observer);
2352 // Used only by test code.
2353 void ExtensionService::UnloadAllExtensionsInternal() {
2354 profile_->GetExtensionSpecialStoragePolicy()->RevokeRightsForAllExtensions();
2356 registry_->ClearAll();
2357 system_->runtime_data()->ClearAll();
2359 // TODO(erikkay) should there be a notification for this? We can't use
2360 // EXTENSION_UNLOADED since that implies that the extension has been disabled
2364 void ExtensionService::OnProfileDestructionStarted() {
2365 ExtensionIdSet ids_to_unload = registry_->enabled_extensions().GetIDs();
2366 for (ExtensionIdSet::iterator it = ids_to_unload.begin();
2367 it != ids_to_unload.end();
2369 UnloadExtension(*it, UnloadedExtensionInfo::REASON_PROFILE_SHUTDOWN);