12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/extensions/extension_service.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <algorithm>
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <iterator>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <set>
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/command_line.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/metrics/histogram.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/strings/string_number_conversions.h"
14868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/stringprintf.h"
15868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/threading/sequenced_worker_pool.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/threading/thread_restrictions.h"
18eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/time/time.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/browser_process.h"
207dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "chrome/browser/chrome_notification_types.h"
211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/browser/content_settings/content_settings_custom_extension_provider.h"
221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/browser/content_settings/content_settings_internal_extension_provider.h"
231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/browser/content_settings/host_content_settings_map.h"
241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/browser/extensions/api/content_settings/content_settings_service.h"
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/extensions/component_loader.h"
266e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "chrome/browser/extensions/crx_installer.h"
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/extensions/data_deleter.h"
281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/browser/extensions/extension_action_storage_manager.h"
29cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/browser/extensions/extension_assets_manager.h"
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/extensions/extension_disabled_ui.h"
31c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "chrome/browser/extensions/extension_error_controller.h"
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/extensions/extension_install_ui.h"
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/extensions/extension_special_storage_policy.h"
341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chrome/browser/extensions/extension_sync_service.h"
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/extensions/extension_util.h"
36116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chrome/browser/extensions/external_install_manager.h"
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/extensions/external_provider_impl.h"
38f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "chrome/browser/extensions/install_verifier.h"
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/extensions/installed_loader.h"
40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "chrome/browser/extensions/pending_extension_manager.h"
416e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "chrome/browser/extensions/permissions_updater.h"
42c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "chrome/browser/extensions/shared_module_service.h"
436e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "chrome/browser/extensions/unpacked_installer.h"
441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/browser/extensions/updater/chrome_extension_downloader_factory.h"
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/extensions/updater/extension_cache.h"
466e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "chrome/browser/extensions/updater/extension_downloader.h"
476e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "chrome/browser/extensions/updater/extension_updater.h"
481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/browser/google/google_brand.h"
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/profiles/profile.h"
50f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/webui/favicon_source.h"
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/webui/ntp/thumbnail_source.h"
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/webui/theme_source.h"
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/chrome_switches.h"
5558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "chrome/common/crash_keys.h"
56eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "chrome/common/extensions/extension_constants.h"
57ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "chrome/common/extensions/features/feature_channel.h"
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/common/extensions/manifest_url_handler.h"
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/url_constants.h"
6003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "components/crx_file/id_util.h"
6158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "components/startup_metric_utils/startup_metric_utils.h"
62cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "content/public/browser/devtools_agent_host.h"
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/notification_service.h"
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/render_process_host.h"
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/storage_partition.h"
66f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "extensions/browser/event_router.h"
6723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)#include "extensions/browser/extension_host.h"
68cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "extensions/browser/extension_prefs.h"
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "extensions/browser/extension_registry.h"
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "extensions/browser/extension_system.h"
71f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "extensions/browser/install_flag.h"
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "extensions/browser/runtime_data.h"
735f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "extensions/browser/uninstall_reason.h"
74a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "extensions/browser/update_observer.h"
75a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "extensions/common/extension_messages.h"
761320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "extensions/common/extension_urls.h"
77f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "extensions/common/feature_switch.h"
78a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#include "extensions/common/file_util.h"
79d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "extensions/common/manifest_constants.h"
80f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "extensions/common/manifest_handlers/background_info.h"
81cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "extensions/common/one_shot_event.h"
828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "extensions/common/permissions/permission_message_provider.h"
83f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "extensions/common/permissions/permissions_data.h"
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_CHROMEOS)
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/chromeos/extensions/install_limiter.h"
871320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/browser/fileapi/file_system_backend.h"
881320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/browser/fileapi/file_system_context.h"
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::BrowserContext;
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::BrowserThread;
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::DevToolsAgentHost;
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using extensions::CrxInstaller;
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using extensions::Extension;
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using extensions::ExtensionIdSet;
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using extensions::ExtensionInfo;
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)using extensions::ExtensionRegistry;
995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)using extensions::ExtensionSet;
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using extensions::FeatureSwitch;
101f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)using extensions::InstallVerifier;
102f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)using extensions::ManagementPolicy;
1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)using extensions::Manifest;
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using extensions::PermissionMessage;
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using extensions::PermissionMessages;
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using extensions::PermissionSet;
107eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochusing extensions::SharedModuleInfo;
108c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochusing extensions::SharedModuleService;
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)using extensions::UnloadedExtensionInfo;
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
111d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)namespace errors = extensions::manifest_errors;
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Wait this many seconds after an extensions becomes idle before updating it.
116116680a4aac90f2aa7413d9095a592090648e557Ben Murdochconst int kUpdateIdleDelay = 5;
1173551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace
1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ExtensionService.
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ExtensionService::CheckExternalUninstall(const std::string& id) {
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Check if the providers know about this extension.
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  extensions::ProviderCollection::const_iterator i;
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (i = external_extension_providers_.begin();
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       i != external_extension_providers_.end(); ++i) {
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DCHECK(i->get()->IsReady());
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (i->get()->HasExtension(id))
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return;  // Yup, known extension, don't uninstall.
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We get the list of external extensions to check from preferences.
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // It is possible that an extension has preferences but is not loaded.
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // For example, an extension that requires experimental permissions
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // will not be loaded if the experimental command line flag is not used.
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // In this case, do not uninstall.
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!GetInstalledExtension(id)) {
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // We can't call UninstallExtension with an unloaded/invalid
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // extension ID.
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(WARNING) << "Attempted uninstallation of unloaded/invalid extension "
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 << "with id: " << id;
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1465f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  UninstallExtension(id,
1475f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                     extensions::UNINSTALL_REASON_ORPHANED_EXTERNAL_EXTENSION,
1485f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                     base::Bind(&base::DoNothing),
1495f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                     NULL);
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void ExtensionService::SetFileTaskRunnerForTesting(
1531320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    const scoped_refptr<base::SequencedTaskRunner>& task_runner) {
1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  file_task_runner_ = task_runner;
1552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ExtensionService::ClearProvidersForTesting() {
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  external_extension_providers_.clear();
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ExtensionService::AddProviderForTesting(
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    extensions::ExternalProviderInterface* test_provider) {
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK(test_provider);
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  external_extension_providers_.push_back(
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      linked_ptr<extensions::ExternalProviderInterface>(test_provider));
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1685f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void ExtensionService::BlacklistExtensionForTest(
1695f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    const std::string& extension_id) {
1705f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  ExtensionIdSet blocked;
1715f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  ExtensionIdSet unchanged;
1725f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  blocked.insert(extension_id);
1735f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  UpdateBlockedExtensions(blocked, unchanged);
1745f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
1755f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool ExtensionService::OnExternalExtensionUpdateUrlFound(
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& id,
178effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    const std::string& install_parameter,
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const GURL& update_url,
1804e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    Manifest::Location location,
1814e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    int creation_flags,
1824e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    bool mark_acknowledged) {
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
18403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  CHECK(crx_file::id_util::IdIsValid(id));
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (Manifest::IsExternalLocation(location)) {
1875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // All extensions that are not user specific can be cached.
1885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    extensions::ExtensionCache::GetInstance()->AllowCaching(id);
1895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
1905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const Extension* extension = GetExtensionById(id, true);
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (extension) {
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Already installed. Skip this install if the current location has
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // higher priority than |location|.
1952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Manifest::Location current = extension->location();
1962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (current == Manifest::GetHigherPriorityLocation(current, location))
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return false;
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Otherwise, overwrite the current installation.
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Add |id| to the set of pending extensions.  If it can not be added,
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // then there is already a pending record from a higher-priority install
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // source.  In this case, signal that this extension will not be
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // installed by returning false.
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!pending_extension_manager()->AddFromExternalUpdateUrl(
206effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch          id,
207effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch          install_parameter,
208effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch          update_url,
209effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch          location,
210effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch          creation_flags,
211effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch          mark_acknowledged)) {
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  update_once_all_providers_are_ready_ = true;
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
220116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// This function is used to uninstall an extension via sync.  The LOG statements
221116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// within this function are used to inform the user if the uninstall cannot be
222116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// done.
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool ExtensionService::UninstallExtensionHelper(
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ExtensionService* extensions_service,
225116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const std::string& extension_id,
2265f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    extensions::UninstallReason reason) {
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We can't call UninstallExtension with an invalid extension ID.
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!extensions_service->GetInstalledExtension(extension_id)) {
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 << "id: " << extension_id;
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The following call to UninstallExtension will not allow an uninstall of a
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // policy-controlled extension.
236a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 error;
2375f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  if (!extensions_service->UninstallExtension(
2385f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)          extension_id, reason, base::Bind(&base::DoNothing), &error)) {
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(WARNING) << "Cannot uninstall extension with id " << extension_id
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 << ": " << error;
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ExtensionService::ExtensionService(Profile* profile,
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   const CommandLine* command_line,
2492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                   const base::FilePath& install_directory,
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   extensions::ExtensionPrefs* extension_prefs,
2512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                   extensions::Blacklist* blacklist,
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   bool autoupdate_enabled,
25390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                   bool extensions_enabled,
25490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                   extensions::OneShotEvent* ready)
2552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    : extensions::Blacklist::Observer(blacklist),
2562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      profile_(profile),
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      system_(extensions::ExtensionSystem::Get(profile)),
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      extension_prefs_(extension_prefs),
2592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      blacklist_(blacklist),
2601e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      extension_sync_service_(NULL),
2615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      registry_(extensions::ExtensionRegistry::Get(profile)),
262cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      pending_extension_manager_(profile),
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      install_directory_(install_directory),
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      extensions_enabled_(extensions_enabled),
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      show_extensions_prompts_(true),
2662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      install_updates_when_idle_(true),
26790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      ready_(ready),
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      update_once_all_providers_are_ready_(false),
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      browser_terminating_(false),
270eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      installs_delayed_for_gc_(false),
271e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      is_first_run_(false),
272c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      shared_module_service_(new extensions::SharedModuleService(profile_)) {
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Figure out if extension installation should be enabled.
276a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (extensions::ExtensionsBrowserClient::Get()->AreExtensionsDisabled(
277a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          *command_line, profile))
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    extensions_enabled_ = false;
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING,
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 content::NotificationService::AllBrowserContextsAndSources());
2825f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  registrar_.Add(this,
2835f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                 extensions::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 content::NotificationService::AllBrowserContextsAndSources());
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 content::NotificationService::AllBrowserContextsAndSources());
2872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  registrar_.Add(this, chrome::NOTIFICATION_UPGRADE_RECOMMENDED,
2882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 content::NotificationService::AllBrowserContextsAndSources());
289cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  registrar_.Add(this,
290cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                 chrome::NOTIFICATION_PROFILE_DESTRUCTION_STARTED,
291cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                 content::Source<Profile>(profile_));
2921320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
2931320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  extensions::ExtensionManagementFactory::GetForBrowserContext(profile_)
2941320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      ->AddObserver(this);
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Set up the ExtensionUpdater
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (autoupdate_enabled) {
29868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    int update_frequency = extensions::kDefaultUpdateFrequencySeconds;
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      base::StringToInt(command_line->GetSwitchValueASCII(
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          switches::kExtensionsUpdateFrequency),
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          &update_frequency);
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
3045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    updater_.reset(new extensions::ExtensionUpdater(
3055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        this,
3065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        extension_prefs,
3075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        profile->GetPrefs(),
3085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        profile,
3095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        update_frequency,
3106e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        extensions::ExtensionCache::GetInstance(),
3111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        base::Bind(ChromeExtensionDownloaderFactory::CreateForProfile,
3121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                   profile)));
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  component_loader_.reset(
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      new extensions::ComponentLoader(this,
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                      profile->GetPrefs(),
3185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                      g_browser_process->local_state(),
3195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                      profile));
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (extensions_enabled_) {
3222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    extensions::ExternalProviderImpl::CreateExternalProviders(
3232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        this, profile_, &external_extension_providers_);
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
326f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Set this as the ExtensionService for app sorting to ensure it causes syncs
327f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // if required.
328c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  is_first_run_ = !extension_prefs_->SetAlertSystemFirstRun();
329c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
330c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  error_controller_.reset(
331c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      new extensions::ExtensionErrorController(profile_, is_first_run_));
332116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  external_install_manager_.reset(
333116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      new extensions::ExternalInstallManager(profile_, is_first_run_));
334c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  extension_action_storage_manager_.reset(
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      new extensions::ExtensionActionStorageManager(profile_));
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // How long is the path to the Extensions directory?
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ExtensionRootPathLength",
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              install_directory_.value().length(), 0, 500, 100);
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const ExtensionSet* ExtensionService::extensions() const {
3445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return &registry_->enabled_extensions();
3452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extensions::PendingExtensionManager*
3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ExtensionService::pending_extension_manager() {
3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return &pending_extension_manager_;
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ExtensionService::~ExtensionService() {
3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // No need to unload extensions here because they are profile-scoped, and the
3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // profile is in the process of being deleted.
3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  extensions::ProviderCollection::const_iterator i;
3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (i = external_extension_providers_.begin();
3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       i != external_extension_providers_.end(); ++i) {
3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    extensions::ExternalProviderInterface* provider = i->get();
3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    provider->ServiceShutdown();
3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ExtensionService::Shutdown() {
3651320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  extensions::ExtensionManagementFactory::GetInstance()
3661320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      ->GetForBrowserContext(profile())
3671320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      ->RemoveObserver(this);
3683551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  system_->management_policy()->UnregisterProvider(
3693551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      shared_module_policy_provider_.get());
3705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const Extension* ExtensionService::GetExtensionById(
3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& id, bool include_disabled) const {
3745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  int include_mask = ExtensionRegistry::ENABLED;
3752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (include_disabled) {
3762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Include blacklisted extensions here because there are hundreds of
3772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // callers of this function, and many might assume that this includes those
3782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // that have been disabled due to blacklisting.
3795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    include_mask |= ExtensionRegistry::DISABLED |
3805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                    ExtensionRegistry::BLACKLISTED;
3812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
3825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return registry_->GetExtensionById(id, include_mask);
3832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ExtensionService::Init() {
3865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
3875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
388f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  base::Time begin_time = base::Time::Now();
389f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
39090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(!is_ready());  // Can't redo init.
3915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DCHECK_EQ(registry_->enabled_extensions().size(), 0u);
3925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
393c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
39434680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)  if (cmd_line->HasSwitch(switches::kInstallEphemeralAppFromWebstore)) {
395c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // The sole purpose of this launch is to install a new extension from CWS
396c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // and immediately terminate: loading already installed extensions is
397c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // unnecessary and may interfere with the inline install dialog (e.g. if an
398c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // extension listens to onStartup and opens a window).
399c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    SetReadyAndNotifyListeners();
4002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } else {
401eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    // LoadAllExtensions() calls OnLoadedInstalledExtensions().
402eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    component_loader_->LoadAll();
403eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    extensions::InstalledLoader(this).LoadAllExtensions();
404eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
4053551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    // Attempt to re-enable extensions whose only disable reason is reloading.
4063551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    std::vector<std::string> extensions_to_enable;
4075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const ExtensionSet& disabled_extensions = registry_->disabled_extensions();
4085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    for (ExtensionSet::const_iterator iter = disabled_extensions.begin();
4095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        iter != disabled_extensions.end(); ++iter) {
4103551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      const Extension* e = iter->get();
4113551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      if (extension_prefs_->GetDisableReasons(e->id()) ==
4123551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)          Extension::DISABLE_RELOAD) {
4133551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        extensions_to_enable.push_back(e->id());
4143551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      }
4153551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    }
4163551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    for (std::vector<std::string>::iterator it = extensions_to_enable.begin();
4173551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)         it != extensions_to_enable.end(); ++it) {
4183551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      EnableExtension(*it);
4193551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    }
4203551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
421eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    // Finish install (if possible) of extensions that were still delayed while
422eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    // the browser was shut down.
423eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> delayed_info(
424eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        extension_prefs_->GetAllDelayedInstallInfo());
425eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    for (size_t i = 0; i < delayed_info->size(); ++i) {
426eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      ExtensionInfo* info = delayed_info->at(i).get();
427eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      scoped_refptr<const Extension> extension(NULL);
428eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      if (info->extension_manifest) {
429eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        std::string error;
430eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        extension = Extension::Create(
431eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch            info->extension_path,
432eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch            info->extension_location,
433eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch            *info->extension_manifest,
434eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch            extension_prefs_->GetDelayedInstallCreationFlags(
435eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                info->extension_id),
436eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch            info->extension_id,
437eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch            &error);
438eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        if (extension.get())
439eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          delayed_installs_.Insert(extension);
440eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      }
441eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
442eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    MaybeFinishDelayedInstallations();
4432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
444eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> delayed_info2(
445eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        extension_prefs_->GetAllDelayedInstallInfo());
446eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    UMA_HISTOGRAM_COUNTS_100("Extensions.UpdateOnLoad",
447eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                             delayed_info2->size() - delayed_info->size());
4485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
449eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    SetReadyAndNotifyListeners();
450eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
451eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    // TODO(erikkay) this should probably be deferred to a future point
452eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    // rather than running immediately at startup.
453eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    CheckForExternalUpdates();
454eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
4555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    LoadGreylistFromPrefs();
4565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
457f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
458f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  UMA_HISTOGRAM_TIMES("Extensions.ExtensionServiceInitTime",
459f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                      base::Time::Now() - begin_time);
4605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void ExtensionService::LoadGreylistFromPrefs() {
46323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  scoped_ptr<ExtensionSet> all_extensions =
46423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      registry_->GenerateInstalledExtensionsSet();
4655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  for (ExtensionSet::const_iterator it = all_extensions->begin();
4675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)       it != all_extensions->end(); ++it) {
4685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    extensions::BlacklistState state =
4695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        extension_prefs_->GetExtensionBlacklistState((*it)->id());
4705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (state == extensions::BLACKLISTED_SECURITY_VULNERABILITY ||
4715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        state == extensions::BLACKLISTED_POTENTIALLY_UNWANTED ||
4725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        state == extensions::BLACKLISTED_CWS_POLICY_VIOLATION)
4735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      greylist_.Insert(*it);
4745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
4755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
4765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool ExtensionService::UpdateExtension(const std::string& id,
4782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                       const base::FilePath& extension_path,
4795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                       bool file_ownership_passed,
4805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                       CrxInstaller** out_crx_installer) {
4815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
4825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (browser_terminating_) {
4835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(WARNING) << "Skipping UpdateExtension due to browser shutdown";
4845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Leak the temp file at extension_path. We don't want to add to the disk
4855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // I/O burden at shutdown, we can't rely on the I/O completing anyway, and
4865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // the file is in the OS temp directory which should be cleaned up for us.
4875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
4885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const extensions::PendingExtensionInfo* pending_extension_info =
4915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      pending_extension_manager()->GetById(id);
4925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const Extension* extension = GetInstalledExtension(id);
4945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!pending_extension_info && !extension) {
4955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(WARNING) << "Will not update extension " << id
4965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 << " because it is not installed or pending";
4975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Delete extension_path since we're not creating a CrxInstaller
4985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // that would do it for us.
4992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!GetFileTaskRunner()->PostTask(
5002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            FROM_HERE,
5015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            base::Bind(
502a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                &extensions::file_util::DeleteFile, extension_path, false)))
5035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NOTREACHED();
5045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
5065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We want a silent install only for non-pending extensions and
5095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // pending extensions that have install_silently set.
510ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  scoped_ptr<ExtensionInstallPrompt> client;
5115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (pending_extension_info && !pending_extension_info->install_silently())
512ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    client.reset(ExtensionInstallUI::CreateInstallPromptWithProfile(profile_));
5135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
514ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  scoped_refptr<CrxInstaller> installer(
515ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      CrxInstaller::Create(this, client.Pass()));
5165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  installer->set_expected_id(id);
5174e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  int creation_flags = Extension::NO_FLAGS;
5185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (pending_extension_info) {
5195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    installer->set_install_source(pending_extension_info->install_source());
5205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (pending_extension_info->install_silently())
5215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      installer->set_allow_silent_install(true);
522cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    if (pending_extension_info->remote_install())
523cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      installer->set_grant_permissions(false);
5244e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    creation_flags = pending_extension_info->creation_flags();
5254e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    if (pending_extension_info->mark_acknowledged())
526116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      external_install_manager_->AcknowledgeExternalExtension(id);
5275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else if (extension) {
5285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    installer->set_install_source(extension->location());
5295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If the extension was installed from or has migrated to the webstore, or
5315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // its auto-update URL is from the webstore, treat it as a webstore install.
5325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Note that we ignore some older extensions with blank auto-update URLs
5335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // because we are mostly concerned with restrictions on NaCl extensions,
5345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // which are newer.
5355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if ((extension && extension->from_webstore()) ||
536868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      (extension && extensions::ManifestURL::UpdatesFromGallery(extension)) ||
5375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      (!extension && extension_urls::IsWebstoreUpdateUrl(
5385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)           pending_extension_info->update_url()))) {
5395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    creation_flags |= Extension::FROM_WEBSTORE;
5405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Bookmark apps being updated is kind of a contradiction, but that's because
5435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // we mark the default apps as bookmark apps, and they're hosted in the web
5445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // store, thus they can get updated. See http://crbug.com/101605 for more
5455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // details.
5465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (extension && extension->from_bookmark())
5475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    creation_flags |= Extension::FROM_BOOKMARK;
5485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (extension && extension->was_installed_by_default())
5505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    creation_flags |= Extension::WAS_INSTALLED_BY_DEFAULT;
5515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  if (extension && extension->was_installed_by_oem())
55323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    creation_flags |= Extension::WAS_INSTALLED_BY_OEM;
55423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
555116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (extension && extension->was_installed_by_custodian())
556116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    creation_flags |= Extension::WAS_INSTALLED_BY_CUSTODIAN;
557116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
558f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (extension) {
559cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    installer->set_is_ephemeral(extension_prefs_->IsEphemeralApp(id));
5606d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    installer->set_do_not_sync(extension_prefs_->DoNotSync(id));
561f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  }
5625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  installer->set_creation_flags(creation_flags);
5645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  installer->set_delete_source(file_ownership_passed);
5665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
5675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  installer->InstallCrx(extension_path);
5685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (out_crx_installer)
570868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    *out_crx_installer = installer.get();
5715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
5736e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
5746e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
575116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid ExtensionService::ReloadExtensionImpl(
576cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    // "transient" because the process of reloading may cause the reference
577cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    // to become invalid. Instead, use |extension_id|, a copy.
578116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const std::string& transient_extension_id,
579116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    bool be_noisy) {
5805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
5812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If the extension is already reloading, don't reload again.
583cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (extension_prefs_->GetDisableReasons(transient_extension_id) &
5842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      Extension::DISABLE_RELOAD) {
5852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
5862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
5872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
588cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Ignore attempts to reload a blacklisted extension. Sometimes this can
589cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // happen in a convoluted reload sequence triggered by the termination of a
590cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // blacklisted extension and a naive attempt to reload it. For an example see
591cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // http://crbug.com/373842.
592cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (registry_->blacklisted_extensions().Contains(transient_extension_id))
593cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return;
594cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
5952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath path;
596cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
597cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  std::string extension_id = transient_extension_id;
598cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  const Extension* transient_current_extension =
599cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      GetExtensionById(extension_id, false);
6005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Disable the extension if it's loaded. It might not be loaded if it crashed.
602cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (transient_current_extension) {
6035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // If the extension has an inspector open for its background page, detach
6045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // the inspector and hang onto a cookie for it, so that we can reattach
6055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // later.
6065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // TODO(yoz): this is not incognito-safe!
607f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    extensions::ProcessManager* manager = system_->process_manager();
6085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    extensions::ExtensionHost* host =
6095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        manager->GetBackgroundHostForExtension(extension_id);
6106e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    if (host && DevToolsAgentHost::HasFor(host->host_contents())) {
6115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Look for an open inspector for the background page.
6127dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      scoped_refptr<DevToolsAgentHost> agent_host =
6136e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          DevToolsAgentHost::GetOrCreateFor(host->host_contents());
6146e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      agent_host->DisconnectWebContents();
6157dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      orphaned_dev_tools_[extension_id] = agent_host;
6165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
6175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
618cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    path = transient_current_extension->path();
619ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    // BeingUpgraded is set back to false when the extension is added.
620cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    system_->runtime_data()->SetBeingUpgraded(transient_current_extension,
621cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                              true);
6225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DisableExtension(extension_id, Extension::DISABLE_RELOAD);
623c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    reloading_extensions_.insert(extension_id);
6245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
625f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    std::map<std::string, base::FilePath>::const_iterator iter =
626f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        unloaded_extension_paths_.find(extension_id);
627f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    if (iter == unloaded_extension_paths_.end()) {
628f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      return;
629f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    }
6305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    path = unloaded_extension_paths_[extension_id];
6315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
633cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  transient_current_extension = NULL;
634cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
635eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (delayed_installs_.Contains(extension_id)) {
6362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    FinishDelayedInstallation(extension_id);
6375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
6385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If we're reloading a component extension, use the component extension
6415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // loader's reloader.
6425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (component_loader_->Exists(extension_id)) {
6435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    component_loader_->Reload(extension_id);
6445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
6455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Check the installed extensions to see if what we're reloading was already
6485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // installed.
6495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<ExtensionInfo> installed_extension(
6505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      extension_prefs_->GetInstalledExtensionInfo(extension_id));
6515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (installed_extension.get() &&
6525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      installed_extension->extension_manifest.get()) {
6535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    extensions::InstalledLoader(this).Load(*installed_extension, false);
6545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
6555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Otherwise, the extension is unpacked (location LOAD).
6565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // We should always be able to remember the extension's path. If it's not in
6575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // the map, someone failed to update |unloaded_extension_paths_|.
6585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CHECK(!path.empty());
659116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    scoped_refptr<extensions::UnpackedInstaller> unpacked_installer =
660116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        extensions::UnpackedInstaller::Create(this);
661116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    unpacked_installer->set_be_noisy_on_failure(be_noisy);
662116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    unpacked_installer->Load(path);
6635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
664116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
665116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
666116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid ExtensionService::ReloadExtension(const std::string& extension_id) {
667116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ReloadExtensionImpl(extension_id, true); // be_noisy
668116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
669116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
670116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid ExtensionService::ReloadExtensionWithQuietFailure(
671116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const std::string& extension_id) {
672116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ReloadExtensionImpl(extension_id, false); // be_noisy
6735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
675cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)bool ExtensionService::UninstallExtension(
676cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    // "transient" because the process of uninstalling may cause the reference
677cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    // to become invalid. Instead, use |extenson->id()|.
678cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const std::string& transient_extension_id,
6795f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    extensions::UninstallReason reason,
6805f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    const base::Closure& deletion_done_callback,
681cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    base::string16* error) {
6825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
6835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
684cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  scoped_refptr<const Extension> extension =
685cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      GetInstalledExtension(transient_extension_id);
6865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Callers should not send us nonexistent extensions.
688868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  CHECK(extension.get());
6895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Policy change which triggers an uninstall will always set
6915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |external_uninstall| to true so this is the only way to uninstall
6925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // managed extensions.
693c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Shared modules being uninstalled will also set |external_uninstall| to true
694c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // so that we can guarantee users don't uninstall a shared module.
695c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // (crbug.com/273300)
696c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // TODO(rdevlin.cronin): This is probably not right. We should do something
697c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // else, like include an enum IS_INTERNAL_UNINSTALL or IS_USER_UNINSTALL so
698c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // we don't do this.
699116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  bool external_uninstall =
7005f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      (reason == extensions::UNINSTALL_REASON_INTERNAL_MANAGEMENT) ||
7011320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      (reason == extensions::UNINSTALL_REASON_REINSTALL) ||
7025f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      (reason == extensions::UNINSTALL_REASON_ORPHANED_EXTERNAL_EXTENSION) ||
7035f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      (reason == extensions::UNINSTALL_REASON_ORPHANED_SHARED_MODULE) ||
7045f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      (reason == extensions::UNINSTALL_REASON_SYNC &&
7055f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)           extension->was_installed_by_custodian());
7065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!external_uninstall &&
7075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      !system_->management_policy()->UserMayModifySettings(
7085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        extension.get(), error)) {
7095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    content::NotificationService::current()->Notify(
7105f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        extensions::NOTIFICATION_EXTENSION_UNINSTALL_NOT_ALLOWED,
7115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        content::Source<Profile>(profile_),
712868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        content::Details<const Extension>(extension.get()));
7135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
7145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
7155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  syncer::SyncChange sync_change;
7171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Don't sync the uninstall if we're going to reinstall the extension
7181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // momentarily.
7191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (extension_sync_service_ &&
7201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      reason != extensions::UNINSTALL_REASON_REINSTALL) {
7211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)     sync_change = extension_sync_service_->PrepareToSyncUninstallExtension(
7221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        extension.get(), is_ready());
7235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
7245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
725a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  system_->install_verifier()->Remove(extension->id());
726f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
7275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
7285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                            extension->GetType(), 100);
729868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  RecordPermissionMessagesHistogram(extension.get(),
73046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                                    "Extensions.Permissions_Uninstall2");
7315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Unload before doing more cleanup to ensure that nothing is hanging on to
7335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // any of these resources.
734cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  UnloadExtension(extension->id(), UnloadedExtensionInfo::REASON_UNINSTALL);
7355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Tell the backend to start deleting installed extensions on the file thread.
7372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!Manifest::IsUnpackedLocation(extension->location())) {
7382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!GetFileTaskRunner()->PostTask(
7392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            FROM_HERE,
740cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            base::Bind(&ExtensionService::UninstallExtensionOnFileThread,
741cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                       extension->id(),
742cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                       profile_,
743a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                       install_directory_,
744cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                       extension->path())))
7455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NOTREACHED();
7465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
7475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7485f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  extensions::DataDeleter::StartDeleting(
7495f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      profile_, extension.get(), deletion_done_callback);
7505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
751cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  UntrackTerminatedExtension(extension->id());
7525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Notify interested parties that we've uninstalled this extension.
7545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  content::NotificationService::current()->Notify(
7555f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      extensions::NOTIFICATION_EXTENSION_UNINSTALLED_DEPRECATED,
7565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      content::Source<Profile>(profile_),
757868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      content::Details<const Extension>(extension.get()));
7585f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  ExtensionRegistry::Get(profile_)
7595f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      ->TriggerOnUninstalled(extension.get(), reason);
7605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (sync_change.IsValid()) {
762cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    extension_sync_service_->ProcessSyncUninstallExtension(extension->id(),
7631e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                                           sync_change);
7645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
7655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
766cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  delayed_installs_.Remove(extension->id());
7675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
768cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  extension_prefs_->OnExtensionUninstalled(
769cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      extension->id(), extension->location(), external_uninstall);
770d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
7715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Track the uninstallation.
7725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionUninstalled", 1, 2);
7735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
7755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
777cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// static
778cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void ExtensionService::UninstallExtensionOnFileThread(
779cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const std::string& id,
780cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    Profile* profile,
781cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const base::FilePath& install_dir,
782cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const base::FilePath& extension_path) {
783cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  extensions::ExtensionAssetsManager* assets_manager =
784cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      extensions::ExtensionAssetsManager::GetInstance();
785cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  assets_manager->UninstallExtension(id, profile, install_dir, extension_path);
786cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
787cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
7885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool ExtensionService::IsExtensionEnabled(
7895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& extension_id) const {
7905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (registry_->enabled_extensions().Contains(extension_id) ||
7915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      registry_->terminated_extensions().Contains(extension_id)) {
7925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return true;
7935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
7945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (registry_->disabled_extensions().Contains(extension_id) ||
7965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      registry_->blacklisted_extensions().Contains(extension_id)) {
7975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
7982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
7995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If the extension hasn't been loaded yet, check the prefs for it. Assume
8015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // enabled unless otherwise noted.
8025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return !extension_prefs_->IsExtensionDisabled(extension_id) &&
803cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)         !extension_prefs_->IsExtensionBlacklisted(extension_id) &&
8042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         !extension_prefs_->IsExternalExtensionUninstalled(extension_id);
8055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ExtensionService::EnableExtension(const std::string& extension_id) {
8085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
8095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (IsExtensionEnabled(extension_id))
8115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
8125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const Extension* extension =
8135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      registry_->disabled_extensions().GetByID(extension_id);
814f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
815f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  ManagementPolicy* policy = system_->management_policy();
816f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (extension && policy->MustRemainDisabled(extension, NULL, NULL)) {
817f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    UMA_HISTOGRAM_COUNTS_100("Extensions.EnableDeniedByPolicy", 1);
818f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return;
819f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
8205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  extension_prefs_->SetExtensionState(extension_id, Extension::ENABLED);
8225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  extension_prefs_->ClearDisableReasons(extension_id);
8235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This can happen if sync enables an extension that is not
8255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // installed yet.
8265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!extension)
8275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
8285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Move it over to the enabled list.
8305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  registry_->AddEnabled(make_scoped_refptr(extension));
8315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  registry_->RemoveDisabled(extension->id());
8325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NotifyExtensionLoaded(extension);
8345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Notify listeners that the extension was enabled.
8365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  content::NotificationService::current()->Notify(
8375f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      extensions::NOTIFICATION_EXTENSION_ENABLED,
8385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      content::Source<Profile>(profile_),
8395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      content::Details<const Extension>(extension));
8405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8411e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (extension_sync_service_)
8421e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    extension_sync_service_->SyncEnableExtension(*extension);
8435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ExtensionService::DisableExtension(
8465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& extension_id,
8475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Extension::DisableReason disable_reason) {
8485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
8495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8505f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // The extension may have been disabled already. Just add a disable reason.
8515f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  if (!IsExtensionEnabled(extension_id)) {
8525f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    extension_prefs_->AddDisableReason(extension_id, disable_reason);
8535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
8545f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  }
8555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const Extension* extension = GetInstalledExtension(extension_id);
8575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |extension| can be NULL if sync disables an extension that is not
8585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // installed yet.
859116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // EXTERNAL_COMPONENT extensions are not generally modifiable by users, but
860116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // can be uninstalled by the browser if the user sets extension-specific
861116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // preferences.
8625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (extension &&
863eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      disable_reason != Extension::DISABLE_RELOAD &&
864116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      !system_->management_policy()->UserMayModifySettings(extension, NULL) &&
865116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      extension->location() != Manifest::EXTERNAL_COMPONENT) {
8665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
8675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
8685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  extension_prefs_->SetExtensionState(extension_id, Extension::DISABLED);
8705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  extension_prefs_->AddDisableReason(extension_id, disable_reason);
8715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  int include_mask =
8735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ExtensionRegistry::EVERYTHING & ~ExtensionRegistry::DISABLED;
8745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  extension = registry_->GetExtensionById(extension_id, include_mask);
8755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!extension)
8765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
8775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // The extension is either enabled or terminated.
8795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DCHECK(registry_->enabled_extensions().Contains(extension->id()) ||
8805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)         registry_->terminated_extensions().Contains(extension->id()));
881c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
8825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Move it over to the disabled list. Don't send a second unload notification
8835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // for terminated extensions being disabled.
8845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  registry_->AddDisabled(make_scoped_refptr(extension));
8855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (registry_->enabled_extensions().Contains(extension->id())) {
8865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    registry_->RemoveEnabled(extension->id());
8871e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    NotifyExtensionUnloaded(extension, UnloadedExtensionInfo::REASON_DISABLE);
8885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
8895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    registry_->RemoveTerminated(extension->id());
8905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
8915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8921e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (extension_sync_service_)
8931e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    extension_sync_service_->SyncDisableExtension(*extension);
8945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8967dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochvoid ExtensionService::DisableUserExtensions(
8977dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    const std::vector<std::string>& except_ids) {
8987d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  extensions::ManagementPolicy* management_policy =
8997d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      system_->management_policy();
9007d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  extensions::ExtensionList to_disable;
9017d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
9025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const ExtensionSet& enabled_set = registry_->enabled_extensions();
9035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  for (ExtensionSet::const_iterator extension = enabled_set.begin();
9045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      extension != enabled_set.end(); ++extension) {
905116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    if (management_policy->UserMayModifySettings(extension->get(), NULL))
9067d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      to_disable.push_back(*extension);
9077d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
9085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const ExtensionSet& terminated_set = registry_->terminated_extensions();
9095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  for (ExtensionSet::const_iterator extension = terminated_set.begin();
9105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      extension != terminated_set.end(); ++extension) {
911116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    if (management_policy->UserMayModifySettings(extension->get(), NULL))
9127d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      to_disable.push_back(*extension);
9137d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
9147d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
9157d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  for (extensions::ExtensionList::const_iterator extension = to_disable.begin();
9167d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      extension != to_disable.end(); ++extension) {
9173240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch    if ((*extension)->was_installed_by_default() &&
9183240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch        extension_urls::IsWebstoreUpdateUrl(
9191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci            extensions::ManifestURL::GetUpdateURL(extension->get())))
9203240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch      continue;
9217dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    const std::string& id = (*extension)->id();
9227dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    if (except_ids.end() == std::find(except_ids.begin(), except_ids.end(), id))
9237dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      DisableExtension(id, extensions::Extension::DISABLE_USER_ACTION);
9247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
9257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
9267d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
9275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ExtensionService::GrantPermissionsAndEnableExtension(
9282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const Extension* extension) {
9292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  GrantPermissions(extension);
93046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  RecordPermissionMessagesHistogram(extension,
93146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                                    "Extensions.Permissions_ReEnable2");
9325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
9335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EnableExtension(extension->id());
9345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
9355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void ExtensionService::GrantPermissions(const Extension* extension) {
9375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK(extension);
9386d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  extensions::PermissionsUpdater(profile()).GrantActivePermissions(extension);
9395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
9405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
9425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ExtensionService::RecordPermissionMessagesHistogram(
94390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const Extension* extension, const char* histogram) {
9442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Since this is called from multiple sources, and since the histogram macros
9452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // use statics, we need to manually lookup the histogram ourselves.
9462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::HistogramBase* counter = base::LinearHistogram::FactoryGet(
9475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      histogram,
9485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      1,
9495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      PermissionMessage::kEnumBoundary,
9505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      PermissionMessage::kEnumBoundary + 1,
9512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::HistogramBase::kUmaTargetedHistogramFlag);
9525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
95390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  PermissionMessages permissions =
95446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      extension->permissions_data()->GetPermissionMessages();
9555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (permissions.empty()) {
9565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    counter->Add(PermissionMessage::kNone);
9575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
9585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    for (PermissionMessages::iterator it = permissions.begin();
9595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         it != permissions.end(); ++it)
960