blob: 687b340e1a53c16c2d8bf5ca06cf760277597f5d [file] [log] [blame]
[email protected]098fa7a2013-03-08 22:11:171// Copyright (c) 2013 The Chromium Authors. All rights reserved.
[email protected]6014d672008-12-05 00:38:252// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]eaa7dd182010-12-14 11:09:005#include "chrome/browser/extensions/extension_service.h"
[email protected]6014d672008-12-05 00:38:256
[email protected]654512b2010-09-01 02:09:427#include <algorithm>
[email protected]695b5712012-12-06 23:55:288#include <iterator>
[email protected]97d2f1d2011-01-15 00:41:089#include <set>
[email protected]654512b2010-09-01 02:09:4210
[email protected]62433d32011-10-12 22:33:1211#include "base/command_line.h"
rkaplowfca0f4dc52015-03-14 21:40:5712#include "base/metrics/histogram_macros.h"
yiyaoliu252f915e2015-04-23 21:56:3613#include "base/profiler/scoped_profile.h"
[email protected]3ea1b182013-02-08 22:38:4114#include "base/strings/string_number_conversions.h"
[email protected]00e7bef2013-06-10 20:35:1715#include "base/strings/stringprintf.h"
[email protected]112158af2013-06-07 23:46:1816#include "base/strings/utf_string_conversions.h"
[email protected]7f8f24f2012-11-15 19:40:1417#include "base/threading/sequenced_worker_pool.h"
[email protected]34b99632011-01-01 01:01:0618#include "base/threading/thread_restrictions.h"
[email protected]41a17c52013-06-28 00:27:5319#include "base/time/time.h"
rkaplowe65c2ff2015-02-14 16:29:5420#include "base/trace_event/trace_event.h"
[email protected]15730c42009-09-03 00:03:2021#include "chrome/browser/browser_process.h"
[email protected]9ea0cd32013-07-12 01:50:3622#include "chrome/browser/chrome_notification_types.h"
mukai87a8d402014-09-15 20:15:0923#include "chrome/browser/content_settings/content_settings_internal_extension_provider.h"
sunil.ratnu21b896c2014-11-24 10:09:2424#include "chrome/browser/extensions/api/content_settings/content_settings_custom_extension_provider.h"
mukai87a8d402014-09-15 20:15:0925#include "chrome/browser/extensions/api/content_settings/content_settings_service.h"
ryanackley48bedbd2015-01-27 23:12:1426#include "chrome/browser/extensions/app_data_migrator.h"
[email protected]d8c8f25f2011-11-02 18:18:0127#include "chrome/browser/extensions/component_loader.h"
[email protected]13e062e2014-08-09 10:21:5528#include "chrome/browser/extensions/crx_installer.h"
[email protected]d9ede582012-08-14 19:21:3829#include "chrome/browser/extensions/data_deleter.h"
rdevlin.cronin2813c1b2014-08-26 23:51:0830#include "chrome/browser/extensions/extension_action_storage_manager.h"
[email protected]5a145e82014-05-29 22:19:0731#include "chrome/browser/extensions/extension_assets_manager.h"
[email protected]62f051c2012-03-29 17:04:4432#include "chrome/browser/extensions/extension_disabled_ui.h"
[email protected]373daf972014-04-10 01:50:4433#include "chrome/browser/extensions/extension_error_controller.h"
[email protected]19eb80152011-02-26 00:28:4334#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]f8aefb132013-10-30 09:29:5235#include "chrome/browser/extensions/extension_sync_service.h"
[email protected]617342a42013-12-18 23:34:0336#include "chrome/browser/extensions/extension_util.h"
[email protected]2894a512014-06-26 19:03:5637#include "chrome/browser/extensions/external_install_manager.h"
[email protected]5df038b2012-07-16 19:03:2738#include "chrome/browser/extensions/external_provider_impl.h"
[email protected]ffd2f79e2013-11-14 00:11:4639#include "chrome/browser/extensions/install_verifier.h"
[email protected]d8c8f25f2011-11-02 18:18:0140#include "chrome/browser/extensions/installed_loader.h"
[email protected]f3d3b382014-03-14 21:19:2841#include "chrome/browser/extensions/pending_extension_manager.h"
[email protected]13e062e2014-08-09 10:21:5542#include "chrome/browser/extensions/permissions_updater.h"
[email protected]90878c52014-04-04 18:21:0243#include "chrome/browser/extensions/shared_module_service.h"
[email protected]13e062e2014-08-09 10:21:5544#include "chrome/browser/extensions/unpacked_installer.h"
rockotdb5192992014-09-16 21:27:0245#include "chrome/browser/extensions/updater/chrome_extension_downloader_factory.h"
[email protected]13e062e2014-08-09 10:21:5546#include "chrome/browser/extensions/updater/extension_updater.h"
rockotdb5192992014-09-16 21:27:0247#include "chrome/browser/google/google_brand.h"
[email protected]8ecad5e2010-12-02 21:18:3348#include "chrome/browser/profiles/profile.h"
dbeam8d57a012015-05-21 07:12:2049#include "chrome/browser/search/thumbnail_source.h"
[email protected]b5a507b22013-11-08 20:41:5750#include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
[email protected]c8d407e2011-04-28 21:27:1751#include "chrome/browser/ui/webui/favicon_source.h"
[email protected]b07e606e2012-09-15 20:16:1552#include "chrome/browser/ui/webui/theme_source.h"
[email protected]e2eb43112009-05-29 21:19:5453#include "chrome/common/chrome_switches.h"
[email protected]f6431be82013-09-07 02:53:4554#include "chrome/common/crash_keys.h"
[email protected]9f4e4f082013-06-21 07:11:1955#include "chrome/common/extensions/extension_constants.h"
[email protected]15d4d2d2013-08-09 06:49:0356#include "chrome/common/extensions/features/feature_channel.h"
[email protected]a57209872009-05-04 22:53:1457#include "chrome/common/url_constants.h"
mukai8eaec822014-10-25 17:53:1658#include "components/content_settings/core/browser/host_content_settings_map.h"
[email protected]fdd28372014-08-21 02:27:2659#include "components/crx_file/id_util.h"
[email protected]7a83dd072013-09-03 12:09:1760#include "components/startup_metric_utils/startup_metric_utils.h"
[email protected]dccba4f82014-05-29 00:52:5661#include "content/public/browser/devtools_agent_host.h"
[email protected]05aad2da2011-10-28 10:12:3762#include "content/public/browser/notification_service.h"
[email protected]f3b1a082011-11-18 00:34:3063#include "content/public/browser/render_process_host.h"
[email protected]399583b2012-12-11 09:33:4264#include "content/public/browser/storage_partition.h"
[email protected]34423532013-11-21 18:13:1065#include "extensions/browser/event_router.h"
[email protected]22401dc2014-03-21 01:38:5766#include "extensions/browser/extension_host.h"
[email protected]dccba4f82014-05-29 00:52:5667#include "extensions/browser/extension_prefs.h"
[email protected]bb1bc9b32013-12-21 03:09:1468#include "extensions/browser/extension_registry.h"
[email protected]59b0e602014-01-30 00:41:2469#include "extensions/browser/extension_system.h"
rockotffa65b12014-10-14 17:19:0670#include "extensions/browser/extensions_browser_client.h"
[email protected]4a1d9c0d2014-06-13 12:50:1171#include "extensions/browser/install_flag.h"
[email protected]45f5b7d2014-01-22 23:47:1372#include "extensions/browser/runtime_data.h"
[email protected]e43c61f2014-07-20 21:46:3473#include "extensions/browser/uninstall_reason.h"
[email protected]a9aa62b312013-11-29 05:35:0674#include "extensions/browser/update_observer.h"
rockotffa65b12014-10-14 17:19:0675#include "extensions/browser/updater/extension_cache.h"
rockot05f40a922014-10-16 19:40:2576#include "extensions/browser/updater/extension_downloader.h"
[email protected]fb820c02014-03-13 15:07:0877#include "extensions/common/extension_messages.h"
rockot90659852014-09-18 19:31:5278#include "extensions/common/extension_urls.h"
[email protected]5ef835a2013-11-08 20:42:5779#include "extensions/common/feature_switch.h"
[email protected]85df9d12014-04-15 17:02:1480#include "extensions/common/file_util.h"
[email protected]0c3c9732013-09-16 08:53:4181#include "extensions/common/manifest_constants.h"
[email protected]558878cc82013-11-09 01:25:5182#include "extensions/common/manifest_handlers/background_info.h"
rockotd5546142014-10-15 00:29:0883#include "extensions/common/manifest_url_handlers.h"
[email protected]dccba4f82014-05-29 00:52:5684#include "extensions/common/one_shot_event.h"
treib2e0517f92015-05-07 23:15:3685#include "extensions/common/permissions/api_permission.h"
[email protected]c41003472013-10-19 15:37:2586#include "extensions/common/permissions/permission_message_provider.h"
[email protected]e4452d32013-11-15 23:07:4187#include "extensions/common/permissions/permissions_data.h"
[email protected]79a60642012-10-20 21:03:1888
treib8ecc1eb52015-03-04 18:29:0689#if defined(ENABLE_SUPERVISED_USERS)
90#include "chrome/browser/supervised_user/supervised_user_service.h"
91#include "chrome/browser/supervised_user/supervised_user_service_factory.h"
92#endif
93
[email protected]eed367e2011-04-12 03:43:3194#if defined(OS_CHROMEOS)
[email protected]88e8ec9152013-01-17 04:05:1895#include "chrome/browser/chromeos/extensions/install_limiter.h"
pilgrime92c5fcd2014-09-10 23:31:2396#include "storage/browser/fileapi/file_system_backend.h"
97#include "storage/browser/fileapi/file_system_context.h"
[email protected]eed367e2011-04-12 03:43:3198#endif
99
[email protected]55eb70e762012-02-20 17:38:39100using content::BrowserContext;
[email protected]631bb742011-11-02 11:29:39101using content::BrowserThread;
[email protected]98f66112012-12-25 12:59:36102using content::DevToolsAgentHost;
treib2e0517f92015-05-07 23:15:36103using extensions::APIPermission;
[email protected]bf3d9df2012-07-24 23:20:27104using extensions::CrxInstaller;
[email protected]1c321ee52012-05-21 03:02:34105using extensions::Extension;
106using extensions::ExtensionIdSet;
107using extensions::ExtensionInfo;
[email protected]599539802014-01-07 23:06:00108using extensions::ExtensionRegistry;
[email protected]289c44b2013-12-17 03:26:57109using extensions::ExtensionSet;
[email protected]215a7be2012-10-22 19:53:42110using extensions::FeatureSwitch;
[email protected]ffd2f79e2013-11-14 00:11:46111using extensions::InstallVerifier;
[email protected]0d54b682013-11-05 14:15:36112using extensions::ManagementPolicy;
[email protected]1d5e58b2013-01-31 08:41:40113using extensions::Manifest;
treib2e0517f92015-05-07 23:15:36114using extensions::PermissionID;
115using extensions::PermissionIDSet;
[email protected]c2e66e12012-06-27 06:27:06116using extensions::PermissionMessage;
treibb566ee832015-03-16 10:20:46117using extensions::PermissionMessageIDs;
[email protected]c2e66e12012-06-27 06:27:06118using extensions::PermissionSet;
[email protected]9f4e4f082013-06-21 07:11:19119using extensions::SharedModuleInfo;
[email protected]90878c52014-04-04 18:21:02120using extensions::SharedModuleService;
[email protected]e410b5f2012-12-14 14:02:24121using extensions::UnloadedExtensionInfo;
[email protected]5ef47ec2010-01-28 05:58:05122
[email protected]b6ab96d2009-08-20 18:58:19123namespace {
124
[email protected]0db124b02012-11-07 04:55:05125// Wait this many seconds after an extensions becomes idle before updating it.
[email protected]ddc6a122014-06-27 04:52:32126const int kUpdateIdleDelay = 5;
[email protected]0db124b02012-11-07 04:55:05127
treib8ecc1eb52015-03-04 18:29:06128#if defined(ENABLE_SUPERVISED_USERS)
129// Callback for SupervisedUserService::AddExtensionUpdateRequest.
130void ExtensionUpdateRequestSent(const std::string& id, bool success) {
131 LOG_IF(WARNING, !success) << "Failed sending update request for " << id;
132}
133#endif
134
[email protected]c6d474f82009-12-16 21:11:06135} // namespace
[email protected]b6ab96d2009-08-20 18:58:19136
[email protected]eaa7dd182010-12-14 11:09:00137// ExtensionService.
[email protected]6014d672008-12-05 00:38:25138
[email protected]8e4560b62011-01-14 10:09:14139void ExtensionService::CheckExternalUninstall(const std::string& id) {
[email protected]a29a517a2011-01-21 21:11:12140 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20141
[email protected]7a4c6852010-09-16 03:44:22142 // Check if the providers know about this extension.
[email protected]5df038b2012-07-16 19:03:27143 extensions::ProviderCollection::const_iterator i;
[email protected]0a60a2e2010-10-25 16:15:21144 for (i = external_extension_providers_.begin();
145 i != external_extension_providers_.end(); ++i) {
[email protected]8e4560b62011-01-14 10:09:14146 DCHECK(i->get()->IsReady());
[email protected]0a60a2e2010-10-25 16:15:21147 if (i->get()->HasExtension(id))
148 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22149 }
150
[email protected]0f48fca2011-05-19 18:46:35151 // We get the list of external extensions to check from preferences.
152 // It is possible that an extension has preferences but is not loaded.
153 // For example, an extension that requires experimental permissions
154 // will not be loaded if the experimental command line flag is not used.
155 // In this case, do not uninstall.
[email protected]31bb5ee62012-09-12 22:58:40156 if (!GetInstalledExtension(id)) {
[email protected]0f48fca2011-05-19 18:46:35157 // We can't call UninstallExtension with an unloaded/invalid
158 // extension ID.
159 LOG(WARNING) << "Attempted uninstallation of unloaded/invalid extension "
160 << "with id: " << id;
161 return;
162 }
[email protected]42d58f62014-07-31 01:32:45163 UninstallExtension(id,
164 extensions::UNINSTALL_REASON_ORPHANED_EXTERNAL_EXTENSION,
165 base::Bind(&base::DoNothing),
166 NULL);
[email protected]7a4c6852010-09-16 03:44:22167}
168
[email protected]7f8f24f2012-11-15 19:40:14169void ExtensionService::SetFileTaskRunnerForTesting(
dcheng605ef8d2014-08-28 18:29:44170 const scoped_refptr<base::SequencedTaskRunner>& task_runner) {
[email protected]7f8f24f2012-11-15 19:40:14171 file_task_runner_ = task_runner;
172}
173
[email protected]8e4560b62011-01-14 10:09:14174void ExtensionService::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22175 external_extension_providers_.clear();
176}
177
[email protected]8e4560b62011-01-14 10:09:14178void ExtensionService::AddProviderForTesting(
[email protected]5df038b2012-07-16 19:03:27179 extensions::ExternalProviderInterface* test_provider) {
[email protected]a29a517a2011-01-21 21:11:12180 CHECK(test_provider);
[email protected]0a60a2e2010-10-25 16:15:21181 external_extension_providers_.push_back(
[email protected]5df038b2012-07-16 19:03:27182 linked_ptr<extensions::ExternalProviderInterface>(test_provider));
[email protected]7a4c6852010-09-16 03:44:22183}
184
[email protected]8f959f522014-08-06 06:26:28185void ExtensionService::BlacklistExtensionForTest(
186 const std::string& extension_id) {
mlerman6a37b6a42014-11-26 22:10:53187 ExtensionIdSet blacklisted;
[email protected]8f959f522014-08-06 06:26:28188 ExtensionIdSet unchanged;
mlerman6a37b6a42014-11-26 22:10:53189 blacklisted.insert(extension_id);
190 UpdateBlacklistedExtensions(blacklisted, unchanged);
[email protected]8f959f522014-08-06 06:26:28191}
192
[email protected]9060d8b02012-01-13 02:14:30193bool ExtensionService::OnExternalExtensionUpdateUrlFound(
[email protected]7a4c6852010-09-16 03:44:22194 const std::string& id,
[email protected]d8fd0fd2014-03-24 13:16:06195 const std::string& install_parameter,
[email protected]21a5a672010-11-04 10:47:42196 const GURL& update_url,
[email protected]464213a2013-10-15 01:06:48197 Manifest::Location location,
198 int creation_flags,
199 bool mark_acknowledged) {
[email protected]ab22ba42011-01-14 16:36:38200 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]fdd28372014-08-21 02:27:26201 CHECK(crx_file::id_util::IdIsValid(id));
[email protected]a8af9fdb2010-10-28 21:52:20202
[email protected]044e86992014-01-24 22:59:11203 if (Manifest::IsExternalLocation(location)) {
204 // All extensions that are not user specific can be cached.
rockotffa65b12014-10-14 17:19:06205 extensions::ExtensionsBrowserClient::Get()->GetExtensionCache()
206 ->AllowCaching(id);
[email protected]044e86992014-01-24 22:59:11207 }
208
[email protected]8a87a5332011-08-11 17:54:59209 const Extension* extension = GetExtensionById(id, true);
210 if (extension) {
211 // Already installed. Skip this install if the current location has
212 // higher priority than |location|.
[email protected]1d5e58b2013-01-31 08:41:40213 Manifest::Location current = extension->location();
214 if (current == Manifest::GetHigherPriorityLocation(current, location))
[email protected]9060d8b02012-01-13 02:14:30215 return false;
[email protected]8a87a5332011-08-11 17:54:59216 // Otherwise, overwrite the current installation.
[email protected]7a4c6852010-09-16 03:44:22217 }
[email protected]9060d8b02012-01-13 02:14:30218
219 // Add |id| to the set of pending extensions. If it can not be added,
220 // then there is already a pending record from a higher-priority install
221 // source. In this case, signal that this extension will not be
222 // installed by returning false.
223 if (!pending_extension_manager()->AddFromExternalUpdateUrl(
[email protected]d8fd0fd2014-03-24 13:16:06224 id,
225 install_parameter,
226 update_url,
227 location,
228 creation_flags,
229 mark_acknowledged)) {
[email protected]9060d8b02012-01-13 02:14:30230 return false;
[email protected]31bb5ee62012-09-12 22:58:40231 }
[email protected]9060d8b02012-01-13 02:14:30232
[email protected]94fde232012-04-27 10:22:30233 update_once_all_providers_are_ready_ = true;
[email protected]9060d8b02012-01-13 02:14:30234 return true;
[email protected]7a4c6852010-09-16 03:44:22235}
236
[email protected]6aeac8342010-10-01 20:21:18237// static
[email protected]cc2f55c2014-07-08 02:19:04238// This function is used to uninstall an extension via sync. The LOG statements
239// within this function are used to inform the user if the uninstall cannot be
240// done.
[email protected]eaa7dd182010-12-14 11:09:00241bool ExtensionService::UninstallExtensionHelper(
242 ExtensionService* extensions_service,
[email protected]cc2f55c2014-07-08 02:19:04243 const std::string& extension_id,
[email protected]e43c61f2014-07-20 21:46:34244 extensions::UninstallReason reason) {
[email protected]d6ebc9792011-04-07 18:18:33245 // We can't call UninstallExtension with an invalid extension ID.
[email protected]31bb5ee62012-09-12 22:58:40246 if (!extensions_service->GetInstalledExtension(extension_id)) {
[email protected]6aeac8342010-10-01 20:21:18247 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]95da88c42011-03-31 10:07:33248 << "id: " << extension_id;
249 return false;
[email protected]6aeac8342010-10-01 20:21:18250 }
251
[email protected]d6ebc9792011-04-07 18:18:33252 // The following call to UninstallExtension will not allow an uninstall of a
253 // policy-controlled extension.
[email protected]439f1e32013-12-09 20:09:09254 base::string16 error;
[email protected]42d58f62014-07-31 01:32:45255 if (!extensions_service->UninstallExtension(
256 extension_id, reason, base::Bind(&base::DoNothing), &error)) {
[email protected]d6ebc9792011-04-07 18:18:33257 LOG(WARNING) << "Cannot uninstall extension with id " << extension_id
258 << ": " << error;
259 return false;
260 }
[email protected]95da88c42011-03-31 10:07:33261
[email protected]6aeac8342010-10-01 20:21:18262 return true;
263}
264
[email protected]eaa7dd182010-12-14 11:09:00265ExtensionService::ExtensionService(Profile* profile,
avi3ef9ec9e2014-12-22 22:50:17266 const base::CommandLine* command_line,
[email protected]650b2d52013-02-10 03:41:45267 const base::FilePath& install_directory,
[email protected]45759612012-07-10 17:21:23268 extensions::ExtensionPrefs* extension_prefs,
[email protected]fdd679b2012-11-15 20:49:39269 extensions::Blacklist* blacklist,
[email protected]0436b102011-04-15 18:30:03270 bool autoupdate_enabled,
[email protected]4a10006a2013-05-17 23:18:35271 bool extensions_enabled,
272 extensions::OneShotEvent* ready)
[email protected]fdd679b2012-11-15 20:49:39273 : extensions::Blacklist::Observer(blacklist),
274 profile_(profile),
[email protected]bd306722012-07-11 20:43:59275 system_(extensions::ExtensionSystem::Get(profile)),
[email protected]73c47932010-12-06 18:13:43276 extension_prefs_(extension_prefs),
[email protected]695b5712012-12-06 23:55:28277 blacklist_(blacklist),
[email protected]f8aefb132013-10-30 09:29:52278 extension_sync_service_(NULL),
[email protected]5fdfa562013-12-27 17:43:59279 registry_(extensions::ExtensionRegistry::Get(profile)),
[email protected]6c9bedf2014-05-21 03:55:51280 pending_extension_manager_(profile),
[email protected]a9b00ac2009-06-25 21:03:23281 install_directory_(install_directory),
[email protected]0436b102011-04-15 18:30:03282 extensions_enabled_(extensions_enabled),
[email protected]e81dba32009-06-19 20:19:13283 show_extensions_prompts_(true),
[email protected]fc332ae2012-11-14 20:17:33284 install_updates_when_idle_(true),
[email protected]4a10006a2013-05-17 23:18:35285 ready_(ready),
[email protected]dff1e042012-04-27 10:59:18286 update_once_all_providers_are_ready_(false),
[email protected]3c4abc82012-10-22 22:25:54287 browser_terminating_(false),
[email protected]9f4e4f082013-06-21 07:11:19288 installs_delayed_for_gc_(false),
[email protected]e5f8fffe2014-04-02 00:30:44289 is_first_run_(false),
mlerman6a37b6a42014-11-26 22:10:53290 block_extensions_(false),
ryanackley48bedbd2015-01-27 23:12:14291 shared_module_service_(new extensions::SharedModuleService(profile_)),
292 app_data_migrator_(new extensions::AppDataMigrator(profile_, registry_)) {
[email protected]a29a517a2011-01-21 21:11:12293 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
rkaplowdd66a1342015-03-05 00:31:49294 TRACE_EVENT0("browser,startup", "ExtensionService::ExtensionService::ctor");
[email protected]a8af9fdb2010-10-28 21:52:20295
[email protected]36a784c2009-06-23 06:21:08296 // Figure out if extension installation should be enabled.
[email protected]367d9b172013-12-03 00:31:02297 if (extensions::ExtensionsBrowserClient::Get()->AreExtensionsDisabled(
298 *command_line, profile))
[email protected]6d60703b2009-08-29 01:29:23299 extensions_enabled_ = false;
[email protected]36a784c2009-06-23 06:21:08300
[email protected]3c4abc82012-10-22 22:25:54301 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING,
302 content::NotificationService::AllBrowserContextsAndSources());
[email protected]adf5a102014-07-31 12:44:06303 registrar_.Add(this,
304 extensions::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07305 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27306 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07307 content::NotificationService::AllBrowserContextsAndSources());
[email protected]92dd8d92013-02-26 00:41:08308 registrar_.Add(this, chrome::NOTIFICATION_UPGRADE_RECOMMENDED,
309 content::NotificationService::AllBrowserContextsAndSources());
[email protected]ebe07772014-05-22 04:16:06310 registrar_.Add(this,
311 chrome::NOTIFICATION_PROFILE_DESTRUCTION_STARTED,
312 content::Source<Profile>(profile_));
binjin1569c9b2014-09-05 13:33:18313
314 extensions::ExtensionManagementFactory::GetForBrowserContext(profile_)
315 ->AddObserver(this);
[email protected]4814b512009-11-07 00:12:29316
rkaplowdd66a1342015-03-05 00:31:49317 // Set up the ExtensionUpdater.
[email protected]93fd78f42009-07-10 16:43:17318 if (autoupdate_enabled) {
[email protected]501105b2013-09-26 05:42:02319 int update_frequency = extensions::kDefaultUpdateFrequencySeconds;
[email protected]93fd78f42009-07-10 16:43:17320 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25321 base::StringToInt(command_line->GetSwitchValueASCII(
322 switches::kExtensionsUpdateFrequency),
323 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17324 }
[email protected]044e86992014-01-24 22:59:11325 updater_.reset(new extensions::ExtensionUpdater(
326 this,
327 extension_prefs,
328 profile->GetPrefs(),
329 profile,
330 update_frequency,
rockotffa65b12014-10-14 17:19:06331 extensions::ExtensionsBrowserClient::Get()->GetExtensionCache(),
rockotdb5192992014-09-16 21:27:02332 base::Bind(ChromeExtensionDownloaderFactory::CreateForProfile,
333 profile)));
[email protected]93fd78f42009-07-10 16:43:17334 }
335
[email protected]25ae0152011-11-18 14:40:02336 component_loader_.reset(
337 new extensions::ComponentLoader(this,
338 profile->GetPrefs(),
[email protected]55e16cd2013-12-18 04:36:08339 g_browser_process->local_state(),
340 profile));
[email protected]8e4560b62011-01-14 10:09:14341
[email protected]0436b102011-04-15 18:30:03342 if (extensions_enabled_) {
[email protected]ae4c37e2012-12-21 01:16:25343 extensions::ExternalProviderImpl::CreateExternalProviders(
344 this, profile_, &external_extension_providers_);
[email protected]873531342011-03-09 12:16:05345 }
[email protected]b671760b2010-07-15 21:13:47346
[email protected]74474042013-11-21 12:03:54347 // Set this as the ExtensionService for app sorting to ensure it causes syncs
348 // if required.
[email protected]b3aa7182013-04-25 04:45:23349 is_first_run_ = !extension_prefs_->SetAlertSystemFirstRun();
350
[email protected]373daf972014-04-10 01:50:44351 error_controller_.reset(
352 new extensions::ExtensionErrorController(profile_, is_first_run_));
[email protected]374ceb6f2014-07-02 19:25:34353 external_install_manager_.reset(
354 new extensions::ExternalInstallManager(profile_, is_first_run_));
[email protected]373daf972014-04-10 01:50:44355
[email protected]c77f2352012-08-08 22:07:58356 extension_action_storage_manager_.reset(
357 new extensions::ExtensionActionStorageManager(profile_));
358
[email protected]cb0e50312011-05-09 15:03:07359 // How long is the path to the Extensions directory?
360 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ExtensionRootPathLength",
361 install_directory_.value().length(), 0, 500, 100);
[email protected]6014d672008-12-05 00:38:25362}
363
[email protected]3f213ad2012-07-26 23:39:41364extensions::PendingExtensionManager*
365 ExtensionService::pending_extension_manager() {
[email protected]b2907fd2011-03-25 16:43:37366 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45367}
368
[email protected]eaa7dd182010-12-14 11:09:00369ExtensionService::~ExtensionService() {
[email protected]c8d407e2011-04-28 21:27:17370 // No need to unload extensions here because they are profile-scoped, and the
371 // profile is in the process of being deleted.
[email protected]94b4ffc2011-02-04 05:55:17372
[email protected]5df038b2012-07-16 19:03:27373 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:14374 for (i = external_extension_providers_.begin();
375 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:27376 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:14377 provider->ServiceShutdown();
378 }
[email protected]6014d672008-12-05 00:38:25379}
380
[email protected]037228a2012-10-05 01:36:16381void ExtensionService::Shutdown() {
binjin1569c9b2014-09-05 13:33:18382 extensions::ExtensionManagementFactory::GetInstance()
383 ->GetForBrowserContext(profile())
384 ->RemoveObserver(this);
[email protected]4c9201c42013-08-16 04:56:21385 system_->management_policy()->UnregisterProvider(
386 shared_module_policy_provider_.get());
[email protected]037228a2012-10-05 01:36:16387}
388
[email protected]b2907fd2011-03-25 16:43:37389const Extension* ExtensionService::GetExtensionById(
390 const std::string& id, bool include_disabled) const {
[email protected]599539802014-01-07 23:06:00391 int include_mask = ExtensionRegistry::ENABLED;
[email protected]695b5712012-12-06 23:55:28392 if (include_disabled) {
mlerman6a37b6a42014-11-26 22:10:53393 // Include blacklisted and blocked extensions here because there are
394 // hundreds of callers of this function, and many might assume that this
395 // includes those that have been disabled due to blacklisting or blocking.
[email protected]599539802014-01-07 23:06:00396 include_mask |= ExtensionRegistry::DISABLED |
mlerman6a37b6a42014-11-26 22:10:53397 ExtensionRegistry::BLACKLISTED | ExtensionRegistry::BLOCKED;
[email protected]695b5712012-12-06 23:55:28398 }
[email protected]599539802014-01-07 23:06:00399 return registry_->GetExtensionById(id, include_mask);
[email protected]695b5712012-12-06 23:55:28400}
401
[email protected]eaa7dd182010-12-14 11:09:00402void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12403 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
rkaplowe65c2ff2015-02-14 16:29:54404 TRACE_EVENT0("browser,startup", "ExtensionService::Init");
yiyaoliu252f915e2015-04-23 21:56:36405 TRACK_SCOPED_REGION("Startup", "ExtensionService::Init");
rkaplowa8fd8d32015-02-25 21:27:56406 SCOPED_UMA_HISTOGRAM_TIMER("Extensions.ExtensionServiceInitTime");
[email protected]3350d712013-11-18 09:32:24407
[email protected]4a10006a2013-05-17 23:18:35408 DCHECK(!is_ready()); // Can't redo init.
[email protected]bb1bc9b32013-12-21 03:09:14409 DCHECK_EQ(registry_->enabled_extensions().size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32410
avi3ef9ec9e2014-12-22 22:50:17411 const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
rdevlin.cronin462b2fb2014-10-02 18:32:52412 if (cmd_line->HasSwitch(switches::kInstallEphemeralAppFromWebstore)) {
[email protected]820d9bd2013-04-03 03:46:03413 // The sole purpose of this launch is to install a new extension from CWS
414 // and immediately terminate: loading already installed extensions is
415 // unnecessary and may interfere with the inline install dialog (e.g. if an
416 // extension listens to onStartup and opens a window).
417 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25418 } else {
[email protected]47b60072013-06-20 10:35:56419 // LoadAllExtensions() calls OnLoadedInstalledExtensions().
420 component_loader_->LoadAll();
421 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]9f4e4f082013-06-21 07:11:19422
rkaplowa8fd8d32015-02-25 21:27:56423 EnabledReloadableExtensions();
424 MaybeFinishShutdownDelayed();
[email protected]47b60072013-06-20 10:35:56425 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25426
rkaplowa8fd8d32015-02-25 21:27:56427 // TODO(erikkay): this should probably be deferred to a future point
[email protected]47b60072013-06-20 10:35:56428 // rather than running immediately at startup.
429 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57430
[email protected]2d19eb6e2014-01-27 17:30:00431 LoadGreylistFromPrefs();
[email protected]399583b2012-12-11 09:33:42432 }
rkaplowa8fd8d32015-02-25 21:27:56433}
[email protected]3350d712013-11-18 09:32:24434
rkaplowa8fd8d32015-02-25 21:27:56435void ExtensionService::EnabledReloadableExtensions() {
436 TRACE_EVENT0("browser,startup",
437 "ExtensionService::EnabledReloadableExtensions");
438
439 std::vector<std::string> extensions_to_enable;
440 const ExtensionSet& disabled_extensions = registry_->disabled_extensions();
441 for (ExtensionSet::const_iterator iter = disabled_extensions.begin();
442 iter != disabled_extensions.end(); ++iter) {
443 const Extension* e = iter->get();
444 if (extension_prefs_->GetDisableReasons(e->id()) ==
445 Extension::DISABLE_RELOAD) {
446 extensions_to_enable.push_back(e->id());
447 }
448 }
449 for (const std::string& extension : extensions_to_enable) {
450 EnableExtension(extension);
451 }
452}
453
454void ExtensionService::MaybeFinishShutdownDelayed() {
455 TRACE_EVENT0("browser,startup",
456 "ExtensionService::MaybeFinishShutdownDelayed");
457
458 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> delayed_info(
459 extension_prefs_->GetAllDelayedInstallInfo());
460 for (size_t i = 0; i < delayed_info->size(); ++i) {
461 ExtensionInfo* info = delayed_info->at(i).get();
462 scoped_refptr<const Extension> extension(NULL);
463 if (info->extension_manifest) {
464 std::string error;
465 extension = Extension::Create(
466 info->extension_path, info->extension_location,
467 *info->extension_manifest,
468 extension_prefs_->GetDelayedInstallCreationFlags(info->extension_id),
469 info->extension_id, &error);
470 if (extension.get())
471 delayed_installs_.Insert(extension);
472 }
473 }
474 MaybeFinishDelayedInstallations();
475 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> delayed_info2(
476 extension_prefs_->GetAllDelayedInstallInfo());
477 UMA_HISTOGRAM_COUNTS_100("Extensions.UpdateOnLoad",
478 delayed_info2->size() - delayed_info->size());
[email protected]6014d672008-12-05 00:38:25479}
480
[email protected]2d19eb6e2014-01-27 17:30:00481void ExtensionService::LoadGreylistFromPrefs() {
rkaplowa8fd8d32015-02-25 21:27:56482 TRACE_EVENT0("browser,startup", "ExtensionService::LoadGreylistFromPrefs");
483
[email protected]f47f7172014-03-19 19:27:10484 scoped_ptr<ExtensionSet> all_extensions =
485 registry_->GenerateInstalledExtensionsSet();
[email protected]2d19eb6e2014-01-27 17:30:00486
487 for (ExtensionSet::const_iterator it = all_extensions->begin();
488 it != all_extensions->end(); ++it) {
489 extensions::BlacklistState state =
490 extension_prefs_->GetExtensionBlacklistState((*it)->id());
491 if (state == extensions::BLACKLISTED_SECURITY_VULNERABILITY ||
492 state == extensions::BLACKLISTED_POTENTIALLY_UNWANTED ||
493 state == extensions::BLACKLISTED_CWS_POLICY_VIOLATION)
494 greylist_.Insert(*it);
495 }
496}
497
ginkage553af3202015-02-04 12:39:09498bool ExtensionService::UpdateExtension(const extensions::CRXFileInfo& file,
[email protected]044e86992014-01-24 22:59:11499 bool file_ownership_passed,
[email protected]31bb5ee62012-09-12 22:58:40500 CrxInstaller** out_crx_installer) {
[email protected]a29a517a2011-01-21 21:11:12501 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]3c4abc82012-10-22 22:25:54502 if (browser_terminating_) {
503 LOG(WARNING) << "Skipping UpdateExtension due to browser shutdown";
504 // Leak the temp file at extension_path. We don't want to add to the disk
505 // I/O burden at shutdown, we can't rely on the I/O completing anyway, and
506 // the file is in the OS temp directory which should be cleaned up for us.
507 return false;
508 }
[email protected]a8af9fdb2010-10-28 21:52:20509
ginkage553af3202015-02-04 12:39:09510 const std::string& id = file.extension_id;
511
[email protected]3f213ad2012-07-26 23:39:41512 const extensions::PendingExtensionInfo* pending_extension_info =
[email protected]51a3bf8b2012-06-08 22:53:06513 pending_extension_manager()->GetById(id);
[email protected]8ef78fd2010-08-19 17:14:32514
[email protected]695b5712012-12-06 23:55:28515 const Extension* extension = GetInstalledExtension(id);
[email protected]51a3bf8b2012-06-08 22:53:06516 if (!pending_extension_info && !extension) {
[email protected]aa142702010-03-26 01:26:33517 LOG(WARNING) << "Will not update extension " << id
518 << " because it is not installed or pending";
519 // Delete extension_path since we're not creating a CrxInstaller
520 // that would do it for us.
[email protected]7f8f24f2012-11-15 19:40:14521 if (!GetFileTaskRunner()->PostTask(
522 FROM_HERE,
ginkage553af3202015-02-04 12:39:09523 base::Bind(&extensions::file_util::DeleteFile, file.path, false)))
[email protected]14908b72011-04-20 06:54:36524 NOTREACHED();
[email protected]420a0ec2011-06-01 01:07:03525
526 return false;
[email protected]e957fe52009-06-23 16:51:05527 }
528
[email protected]f8636f92013-08-09 21:02:37529 scoped_refptr<CrxInstaller> installer(
pkotwicz31eb1e8ec2014-10-08 15:26:46530 CrxInstaller::Create(this, scoped_ptr<ExtensionInstallPrompt>()));
[email protected]6dfbbf82010-03-12 23:09:16531 installer->set_expected_id(id);
ginkage47e603e2015-02-27 08:42:41532 installer->set_expected_hash(file.expected_hash);
[email protected]464213a2013-10-15 01:06:48533 int creation_flags = Extension::NO_FLAGS;
[email protected]51a3bf8b2012-06-08 22:53:06534 if (pending_extension_info) {
535 installer->set_install_source(pending_extension_info->install_source());
pkotwicz31eb1e8ec2014-10-08 15:26:46536 installer->set_allow_silent_install(true);
[email protected]21db9ef2014-05-16 02:06:27537 if (pending_extension_info->remote_install())
538 installer->set_grant_permissions(false);
[email protected]464213a2013-10-15 01:06:48539 creation_flags = pending_extension_info->creation_flags();
540 if (pending_extension_info->mark_acknowledged())
[email protected]374ceb6f2014-07-02 19:25:34541 external_install_manager_->AcknowledgeExternalExtension(id);
treib69a7e012015-05-21 09:37:57542
543 // If the extension came in disabled due to a permission increase, then
544 // don't grant it all the permissions. crbug.com/484214
545 if (extensions::ExtensionPrefs::Get(profile_)->HasDisableReason(
546 id, Extension::DISABLE_PERMISSIONS_INCREASE)) {
547 installer->set_grant_permissions(false);
548 }
[email protected]51a3bf8b2012-06-08 22:53:06549 } else if (extension) {
[email protected]5eb375e92010-11-26 07:50:41550 installer->set_install_source(extension->location());
[email protected]51a3bf8b2012-06-08 22:53:06551 }
[email protected]7d8b7072012-04-07 01:07:46552 // If the extension was installed from or has migrated to the webstore, or
[email protected]fcb2c2c2012-10-17 21:08:45553 // its auto-update URL is from the webstore, treat it as a webstore install.
554 // Note that we ignore some older extensions with blank auto-update URLs
555 // because we are mostly concerned with restrictions on NaCl extensions,
556 // which are newer.
[email protected]75764512011-12-19 19:54:28557 if ((extension && extension->from_webstore()) ||
[email protected]4a5fe3e22013-06-04 07:06:38558 (extension && extensions::ManifestURL::UpdatesFromGallery(extension)) ||
[email protected]963d13c2012-09-29 17:13:35559 (!extension && extension_urls::IsWebstoreUpdateUrl(
[email protected]51a3bf8b2012-06-08 22:53:06560 pending_extension_info->update_url()))) {
[email protected]a12ce8b22012-01-17 18:40:53561 creation_flags |= Extension::FROM_WEBSTORE;
562 }
563
564 // Bookmark apps being updated is kind of a contradiction, but that's because
565 // we mark the default apps as bookmark apps, and they're hosted in the web
566 // store, thus they can get updated. See http://crbug.com/101605 for more
567 // details.
568 if (extension && extension->from_bookmark())
569 creation_flags |= Extension::FROM_BOOKMARK;
570
[email protected]e33bbc22012-08-27 22:05:46571 if (extension && extension->was_installed_by_default())
572 creation_flags |= Extension::WAS_INSTALLED_BY_DEFAULT;
573
[email protected]acc3c7c22014-03-19 06:23:39574 if (extension && extension->was_installed_by_oem())
575 creation_flags |= Extension::WAS_INSTALLED_BY_OEM;
576
[email protected]6338fa32014-07-16 21:41:59577 if (extension && extension->was_installed_by_custodian())
578 creation_flags |= Extension::WAS_INSTALLED_BY_CUSTODIAN;
579
[email protected]658eae52014-06-14 20:28:05580 if (extension) {
[email protected]411f8ae2014-05-22 11:12:23581 installer->set_is_ephemeral(extension_prefs_->IsEphemeralApp(id));
[email protected]c30bda262014-06-19 04:10:13582 installer->set_do_not_sync(extension_prefs_->DoNotSync(id));
[email protected]658eae52014-06-14 20:28:05583 }
[email protected]ab6c7be42014-01-16 02:05:54584
[email protected]a12ce8b22012-01-17 18:40:53585 installer->set_creation_flags(creation_flags);
586
[email protected]044e86992014-01-24 22:59:11587 installer->set_delete_source(file_ownership_passed);
[email protected]cb0e50312011-05-09 15:03:07588 installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
ginkage553af3202015-02-04 12:39:09589 installer->InstallCrxFile(file);
[email protected]420a0ec2011-06-01 01:07:03590
591 if (out_crx_installer)
[email protected]dc24976f2013-06-02 21:15:09592 *out_crx_installer = installer.get();
[email protected]420a0ec2011-06-01 01:07:03593
594 return true;
[email protected]e957fe52009-06-23 16:51:05595}
596
[email protected]bca4b832014-07-17 20:22:34597void ExtensionService::ReloadExtensionImpl(
[email protected]757d60a2014-05-23 00:11:44598 // "transient" because the process of reloading may cause the reference
599 // to become invalid. Instead, use |extension_id|, a copy.
[email protected]bca4b832014-07-17 20:22:34600 const std::string& transient_extension_id,
601 bool be_noisy) {
[email protected]a29a517a2011-01-21 21:11:12602 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]2a947202013-03-06 04:58:05603
604 // If the extension is already reloading, don't reload again.
[email protected]757d60a2014-05-23 00:11:44605 if (extension_prefs_->GetDisableReasons(transient_extension_id) &
[email protected]2a947202013-03-06 04:58:05606 Extension::DISABLE_RELOAD) {
607 return;
608 }
609
mlerman6a37b6a42014-11-26 22:10:53610 // Ignore attempts to reload a blacklisted or blocked extension. Sometimes
611 // this can happen in a convoluted reload sequence triggered by the
612 // termination of a blacklisted or blocked extension and a naive attempt to
613 // reload it. For an example see http://crbug.com/373842.
614 if (registry_->blacklisted_extensions().Contains(transient_extension_id) ||
615 registry_->blocked_extensions().Contains(transient_extension_id)) {
[email protected]757d60a2014-05-23 00:11:44616 return;
mlerman6a37b6a42014-11-26 22:10:53617 }
[email protected]757d60a2014-05-23 00:11:44618
[email protected]650b2d52013-02-10 03:41:45619 base::FilePath path;
[email protected]757d60a2014-05-23 00:11:44620
621 std::string extension_id = transient_extension_id;
622 const Extension* transient_current_extension =
623 GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40624
[email protected]f17dbd42010-08-16 23:21:10625 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]757d60a2014-05-23 00:11:44626 if (transient_current_extension) {
[email protected]4814b512009-11-07 00:12:29627 // If the extension has an inspector open for its background page, detach
628 // the inspector and hang onto a cookie for it, so that we can reattach
629 // later.
[email protected]31d8f5f22012-04-02 15:22:08630 // TODO(yoz): this is not incognito-safe!
reillyg0ea3fa902014-10-28 15:30:23631 extensions::ProcessManager* manager =
632 extensions::ProcessManager::Get(profile_);
[email protected]3a1dc572012-07-31 22:25:13633 extensions::ExtensionHost* host =
634 manager->GetBackgroundHostForExtension(extension_id);
[email protected]b3f957e62014-08-08 10:09:02635 if (host && DevToolsAgentHost::HasFor(host->host_contents())) {
[email protected]4814b512009-11-07 00:12:29636 // Look for an open inspector for the background page.
[email protected]04ea1bb2013-07-10 09:26:09637 scoped_refptr<DevToolsAgentHost> agent_host =
[email protected]b3f957e62014-08-08 10:09:02638 DevToolsAgentHost::GetOrCreateFor(host->host_contents());
639 agent_host->DisconnectWebContents();
[email protected]04ea1bb2013-07-10 09:26:09640 orphaned_dev_tools_[extension_id] = agent_host;
[email protected]4814b512009-11-07 00:12:29641 }
642
[email protected]757d60a2014-05-23 00:11:44643 path = transient_current_extension->path();
[email protected]58076192013-07-19 19:43:50644 // BeingUpgraded is set back to false when the extension is added.
rdevlin.cronin2c16c602014-11-21 01:35:49645 system_->runtime_data()->SetBeingUpgraded(transient_current_extension->id(),
[email protected]757d60a2014-05-23 00:11:44646 true);
[email protected]44d62b62012-04-11 00:06:03647 DisableExtension(extension_id, Extension::DISABLE_RELOAD);
[email protected]b914e2952013-04-26 07:10:03648 reloading_extensions_.insert(extension_id);
[email protected]1eb175082010-02-10 09:26:16649 } else {
[email protected]d32c02a2014-06-16 22:58:36650 std::map<std::string, base::FilePath>::const_iterator iter =
651 unloaded_extension_paths_.find(extension_id);
652 if (iter == unloaded_extension_paths_.end()) {
653 return;
654 }
[email protected]1eb175082010-02-10 09:26:16655 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06656 }
657
[email protected]757d60a2014-05-23 00:11:44658 transient_current_extension = NULL;
659
[email protected]9f4e4f082013-06-21 07:11:19660 if (delayed_installs_.Contains(extension_id)) {
[email protected]6f6101832012-11-27 22:10:48661 FinishDelayedInstallation(extension_id);
[email protected]0db124b02012-11-07 04:55:05662 return;
663 }
664
[email protected]43ceb002012-02-10 23:19:15665 // If we're reloading a component extension, use the component extension
666 // loader's reloader.
667 if (component_loader_->Exists(extension_id)) {
668 component_loader_->Reload(extension_id);
669 return;
670 }
671
[email protected]e6090e42010-03-23 22:44:08672 // Check the installed extensions to see if what we're reloading was already
673 // installed.
674 scoped_ptr<ExtensionInfo> installed_extension(
675 extension_prefs_->GetInstalledExtensionInfo(extension_id));
676 if (installed_extension.get() &&
677 installed_extension->extension_manifest.get()) {
[email protected]d8c8f25f2011-11-02 18:18:01678 extensions::InstalledLoader(this).Load(*installed_extension, false);
[email protected]e6090e42010-03-23 22:44:08679 } else {
[email protected]d8c8f25f2011-11-02 18:18:01680 // Otherwise, the extension is unpacked (location LOAD).
[email protected]e6090e42010-03-23 22:44:08681 // We should always be able to remember the extension's path. If it's not in
682 // the map, someone failed to update |unloaded_extension_paths_|.
683 CHECK(!path.empty());
[email protected]bca4b832014-07-17 20:22:34684 scoped_refptr<extensions::UnpackedInstaller> unpacked_installer =
685 extensions::UnpackedInstaller::Create(this);
686 unpacked_installer->set_be_noisy_on_failure(be_noisy);
687 unpacked_installer->Load(path);
[email protected]e6090e42010-03-23 22:44:08688 }
[email protected]9cddd4702009-07-27 22:09:40689}
690
[email protected]bca4b832014-07-17 20:22:34691void ExtensionService::ReloadExtension(const std::string& extension_id) {
692 ReloadExtensionImpl(extension_id, true); // be_noisy
693}
694
695void ExtensionService::ReloadExtensionWithQuietFailure(
696 const std::string& extension_id) {
697 ReloadExtensionImpl(extension_id, false); // be_noisy
698}
699
[email protected]757d60a2014-05-23 00:11:44700bool ExtensionService::UninstallExtension(
701 // "transient" because the process of uninstalling may cause the reference
702 // to become invalid. Instead, use |extenson->id()|.
703 const std::string& transient_extension_id,
[email protected]e43c61f2014-07-20 21:46:34704 extensions::UninstallReason reason,
[email protected]42d58f62014-07-31 01:32:45705 const base::Closure& deletion_done_callback,
[email protected]757d60a2014-05-23 00:11:44706 base::string16* error) {
[email protected]a29a517a2011-01-21 21:11:12707 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20708
[email protected]757d60a2014-05-23 00:11:44709 scoped_refptr<const Extension> extension =
710 GetInstalledExtension(transient_extension_id);
[email protected]631cf822009-05-15 07:01:25711
[email protected]e7afe2452010-08-22 16:19:13712 // Callers should not send us nonexistent extensions.
[email protected]dc24976f2013-06-02 21:15:09713 CHECK(extension.get());
[email protected]9f1087e2009-06-15 17:29:32714
binjincccacef2014-10-13 19:00:20715 ManagementPolicy* by_policy = system_->management_policy();
[email protected]95da88c42011-03-31 10:07:33716 // Policy change which triggers an uninstall will always set
717 // |external_uninstall| to true so this is the only way to uninstall
718 // managed extensions.
[email protected]90878c52014-04-04 18:21:02719 // Shared modules being uninstalled will also set |external_uninstall| to true
720 // so that we can guarantee users don't uninstall a shared module.
721 // (crbug.com/273300)
722 // TODO(rdevlin.cronin): This is probably not right. We should do something
723 // else, like include an enum IS_INTERNAL_UNINSTALL or IS_USER_UNINSTALL so
724 // we don't do this.
[email protected]cc2f55c2014-07-08 02:19:04725 bool external_uninstall =
[email protected]e43c61f2014-07-20 21:46:34726 (reason == extensions::UNINSTALL_REASON_INTERNAL_MANAGEMENT) ||
dtseng9fb3d5b2015-02-23 17:24:17727 (reason == extensions::UNINSTALL_REASON_COMPONENT_REMOVED) ||
rdevlin.cronin5f6b69d2014-09-20 01:23:35728 (reason == extensions::UNINSTALL_REASON_REINSTALL) ||
[email protected]e43c61f2014-07-20 21:46:34729 (reason == extensions::UNINSTALL_REASON_ORPHANED_EXTERNAL_EXTENSION) ||
[email protected]6ef60012014-07-22 19:08:12730 (reason == extensions::UNINSTALL_REASON_ORPHANED_SHARED_MODULE) ||
731 (reason == extensions::UNINSTALL_REASON_SYNC &&
dtseng9fb3d5b2015-02-23 17:24:17732 extension->was_installed_by_custodian());
[email protected]65187152012-06-02 13:14:14733 if (!external_uninstall &&
binjincccacef2014-10-13 19:00:20734 (!by_policy->UserMayModifySettings(extension.get(), error) ||
735 by_policy->MustRemainInstalled(extension.get(), error))) {
[email protected]ad50def52011-10-19 23:17:07736 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:06737 extensions::NOTIFICATION_EXTENSION_UNINSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:53738 content::Source<Profile>(profile_),
[email protected]dc24976f2013-06-02 21:15:09739 content::Details<const Extension>(extension.get()));
[email protected]d6ebc9792011-04-07 18:18:33740 return false;
741 }
[email protected]95da88c42011-03-31 10:07:33742
[email protected]65f173552012-06-28 22:43:58743 syncer::SyncChange sync_change;
rdevlin.cronin5f6b69d2014-09-20 01:23:35744 // Don't sync the uninstall if we're going to reinstall the extension
745 // momentarily.
746 if (extension_sync_service_ &&
747 reason != extensions::UNINSTALL_REASON_REINSTALL) {
[email protected]f8aefb132013-10-30 09:29:52748 sync_change = extension_sync_service_->PrepareToSyncUninstallExtension(
749 extension.get(), is_ready());
[email protected]3bdba0d2011-08-23 07:17:30750 }
751
juncai33e462102015-05-18 20:48:44752 InstallVerifier::Get(GetBrowserContext())->Remove(extension->id());
[email protected]ffd2f79e2013-11-14 00:11:46753
[email protected]9b217652010-10-08 22:04:23754 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08755 extension->GetType(), 100);
treib2e0517f92015-05-07 23:15:36756 RecordPermissionMessagesHistogram(extension.get(), "Uninstall");
[email protected]9b217652010-10-08 22:04:23757
[email protected]831aa212010-03-26 13:55:19758 // Unload before doing more cleanup to ensure that nothing is hanging on to
759 // any of these resources.
[email protected]757d60a2014-05-23 00:11:44760 UnloadExtension(extension->id(), UnloadedExtensionInfo::REASON_UNINSTALL);
[email protected]831aa212010-03-26 13:55:19761
[email protected]9f1087e2009-06-15 17:29:32762 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]12075d12013-02-27 05:38:05763 if (!Manifest::IsUnpackedLocation(extension->location())) {
[email protected]7f8f24f2012-11-15 19:40:14764 if (!GetFileTaskRunner()->PostTask(
765 FROM_HERE,
[email protected]5a145e82014-05-29 22:19:07766 base::Bind(&ExtensionService::UninstallExtensionOnFileThread,
767 extension->id(),
768 profile_,
[email protected]85df9d12014-04-15 17:02:14769 install_directory_,
[email protected]5a145e82014-05-29 22:19:07770 extension->path())))
[email protected]14908b72011-04-20 06:54:36771 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32772 }
773
[email protected]42d58f62014-07-31 01:32:45774 extensions::DataDeleter::StartDeleting(
775 profile_, extension.get(), deletion_done_callback);
[email protected]0d6ec3a72011-09-02 02:09:43776
[email protected]757d60a2014-05-23 00:11:44777 UntrackTerminatedExtension(extension->id());
[email protected]211030342010-09-30 18:41:06778
779 // Notify interested parties that we've uninstalled this extension.
[email protected]e43c61f2014-07-20 21:46:34780 ExtensionRegistry::Get(profile_)
781 ->TriggerOnUninstalled(extension.get(), reason);
[email protected]d6ebc9792011-04-07 18:18:33782
rdevlin.cronin5f6b69d2014-09-20 01:23:35783 if (sync_change.IsValid()) {
[email protected]757d60a2014-05-23 00:11:44784 extension_sync_service_->ProcessSyncUninstallExtension(extension->id(),
[email protected]f8aefb132013-10-30 09:29:52785 sync_change);
[email protected]3bdba0d2011-08-23 07:17:30786 }
787
[email protected]757d60a2014-05-23 00:11:44788 delayed_installs_.Remove(extension->id());
[email protected]0db124b02012-11-07 04:55:05789
[email protected]757d60a2014-05-23 00:11:44790 extension_prefs_->OnExtensionUninstalled(
791 extension->id(), extension->location(), external_uninstall);
[email protected]d4eda592013-09-18 03:37:57792
[email protected]333b1de2011-09-12 18:28:50793 // Track the uninstallation.
[email protected]49098f702011-10-13 03:47:18794 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionUninstalled", 1, 2);
795
[email protected]d6ebc9792011-04-07 18:18:33796 return true;
[email protected]c10da4b02010-03-25 14:38:32797}
798
[email protected]5a145e82014-05-29 22:19:07799// static
800void ExtensionService::UninstallExtensionOnFileThread(
801 const std::string& id,
802 Profile* profile,
803 const base::FilePath& install_dir,
804 const base::FilePath& extension_path) {
805 extensions::ExtensionAssetsManager* assets_manager =
806 extensions::ExtensionAssetsManager::GetInstance();
807 assets_manager->UninstallExtension(id, profile, install_dir, extension_path);
808}
809
[email protected]c3cfb012011-04-06 22:07:35810bool ExtensionService::IsExtensionEnabled(
811 const std::string& extension_id) const {
[email protected]bb1bc9b32013-12-21 03:09:14812 if (registry_->enabled_extensions().Contains(extension_id) ||
813 registry_->terminated_extensions().Contains(extension_id)) {
[email protected]36429da2011-07-11 20:25:18814 return true;
[email protected]dc9a74f72012-08-17 18:07:21815 }
[email protected]36429da2011-07-11 20:25:18816
[email protected]bb1bc9b32013-12-21 03:09:14817 if (registry_->disabled_extensions().Contains(extension_id) ||
mlerman6a37b6a42014-11-26 22:10:53818 registry_->blacklisted_extensions().Contains(extension_id) ||
819 registry_->blocked_extensions().Contains(extension_id)) {
[email protected]ad83ca242011-07-29 01:32:25820 return false;
[email protected]695b5712012-12-06 23:55:28821 }
[email protected]ad83ca242011-07-29 01:32:25822
mlerman3690e5be2014-12-01 22:57:44823 // Blocked extensions aren't marked as such in prefs, thus if
824 // |block_extensions_| is true then CanBlockExtension() must be called with an
825 // Extension object. If the |extension_id| is not loaded, assume not enabled.
826 if (block_extensions_) {
827 const Extension* extension = GetInstalledExtension(extension_id);
828 if (!extension || CanBlockExtension(extension))
829 return false;
830 }
mlerman6a37b6a42014-11-26 22:10:53831
[email protected]ad83ca242011-07-29 01:32:25832 // If the extension hasn't been loaded yet, check the prefs for it. Assume
833 // enabled unless otherwise noted.
834 return !extension_prefs_->IsExtensionDisabled(extension_id) &&
[email protected]27e528322014-05-27 20:54:30835 !extension_prefs_->IsExtensionBlacklisted(extension_id) &&
[email protected]f574c402012-12-04 23:20:31836 !extension_prefs_->IsExternalExtensionUninstalled(extension_id);
[email protected]c3cfb012011-04-06 22:07:35837}
838
[email protected]eaa7dd182010-12-14 11:09:00839void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12840 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20841
[email protected]06f92562011-04-29 19:27:31842 if (IsExtensionEnabled(extension_id))
[email protected]0c6da502009-08-14 22:32:39843 return;
[email protected]bb1bc9b32013-12-21 03:09:14844 const Extension* extension =
845 registry_->disabled_extensions().GetByID(extension_id);
[email protected]0d54b682013-11-05 14:15:36846
847 ManagementPolicy* policy = system_->management_policy();
848 if (extension && policy->MustRemainDisabled(extension, NULL, NULL)) {
849 UMA_HISTOGRAM_COUNTS_100("Extensions.EnableDeniedByPolicy", 1);
850 return;
851 }
[email protected]0c6da502009-08-14 22:32:39852
[email protected]b3317ad2011-04-28 23:46:00853 extension_prefs_->SetExtensionState(extension_id, Extension::ENABLED);
[email protected]eb5e4f92012-08-15 23:33:28854 extension_prefs_->ClearDisableReasons(extension_id);
[email protected]1784e83a2009-09-08 21:01:52855
[email protected]06f92562011-04-29 19:27:31856 // This can happen if sync enables an extension that is not
857 // installed yet.
858 if (!extension)
859 return;
860
[email protected]0c6da502009-08-14 22:32:39861 // Move it over to the enabled list.
[email protected]bb1bc9b32013-12-21 03:09:14862 registry_->AddEnabled(make_scoped_refptr(extension));
863 registry_->RemoveDisabled(extension->id());
[email protected]0c6da502009-08-14 22:32:39864
[email protected]62d30f42009-10-01 22:36:06865 NotifyExtensionLoaded(extension);
[email protected]3bdba0d2011-08-23 07:17:30866
[email protected]f74d7f32012-06-19 19:22:51867 // Notify listeners that the extension was enabled.
868 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:06869 extensions::NOTIFICATION_EXTENSION_ENABLED,
[email protected]f74d7f32012-06-19 19:22:51870 content::Source<Profile>(profile_),
871 content::Details<const Extension>(extension));
872
[email protected]f8aefb132013-10-30 09:29:52873 if (extension_sync_service_)
874 extension_sync_service_->SyncEnableExtension(*extension);
[email protected]0c6da502009-08-14 22:32:39875}
876
treibc1192322015-05-20 12:56:07877void ExtensionService::DisableExtension(const std::string& extension_id,
878 int disable_reasons) {
[email protected]a29a517a2011-01-21 21:11:12879 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20880
[email protected]8f959f522014-08-06 06:26:28881 // The extension may have been disabled already. Just add a disable reason.
882 if (!IsExtensionEnabled(extension_id)) {
treibc1192322015-05-20 12:56:07883 extension_prefs_->AddDisableReasons(extension_id, disable_reasons);
[email protected]1784e83a2009-09-08 21:01:52884 return;
[email protected]8f959f522014-08-06 06:26:28885 }
[email protected]1784e83a2009-09-08 21:01:52886
[email protected]06f92562011-04-29 19:27:31887 const Extension* extension = GetInstalledExtension(extension_id);
888 // |extension| can be NULL if sync disables an extension that is not
889 // installed yet.
[email protected]ab894542014-07-03 12:35:39890 // EXTERNAL_COMPONENT extensions are not generally modifiable by users, but
891 // can be uninstalled by the browser if the user sets extension-specific
892 // preferences.
[email protected]65187152012-06-02 13:14:14893 if (extension &&
treibc1192322015-05-20 12:56:07894 !(disable_reasons & Extension::DISABLE_RELOAD) &&
895 !(disable_reasons & Extension::DISABLE_UPDATE_REQUIRED_BY_POLICY) &&
[email protected]ab894542014-07-03 12:35:39896 !system_->management_policy()->UserMayModifySettings(extension, NULL) &&
897 extension->location() != Manifest::EXTERNAL_COMPONENT) {
[email protected]95da88c42011-03-31 10:07:33898 return;
[email protected]65187152012-06-02 13:14:14899 }
[email protected]95da88c42011-03-31 10:07:33900
[email protected]b3317ad2011-04-28 23:46:00901 extension_prefs_->SetExtensionState(extension_id, Extension::DISABLED);
treibc1192322015-05-20 12:56:07902 extension_prefs_->AddDisableReasons(extension_id, disable_reasons);
[email protected]1784e83a2009-09-08 21:01:52903
[email protected]599539802014-01-07 23:06:00904 int include_mask =
905 ExtensionRegistry::EVERYTHING & ~ExtensionRegistry::DISABLED;
906 extension = registry_->GetExtensionById(extension_id, include_mask);
[email protected]06f92562011-04-29 19:27:31907 if (!extension)
908 return;
909
[email protected]45f5b7d2014-01-22 23:47:13910 // The extension is either enabled or terminated.
911 DCHECK(registry_->enabled_extensions().Contains(extension->id()) ||
912 registry_->terminated_extensions().Contains(extension->id()));
[email protected]64742de32013-04-22 08:44:34913
[email protected]5c094792012-09-07 19:44:53914 // Move it over to the disabled list. Don't send a second unload notification
915 // for terminated extensions being disabled.
[email protected]bb1bc9b32013-12-21 03:09:14916 registry_->AddDisabled(make_scoped_refptr(extension));
917 if (registry_->enabled_extensions().Contains(extension->id())) {
918 registry_->RemoveEnabled(extension->id());
[email protected]b0af4792013-10-23 09:12:13919 NotifyExtensionUnloaded(extension, UnloadedExtensionInfo::REASON_DISABLE);
[email protected]5c094792012-09-07 19:44:53920 } else {
[email protected]bb1bc9b32013-12-21 03:09:14921 registry_->RemoveTerminated(extension->id());
[email protected]5c094792012-09-07 19:44:53922 }
[email protected]3bdba0d2011-08-23 07:17:30923
[email protected]f8aefb132013-10-30 09:29:52924 if (extension_sync_service_)
925 extension_sync_service_->SyncDisableExtension(*extension);
[email protected]1784e83a2009-09-08 21:01:52926}
927
[email protected]1abf05e2013-07-09 17:04:36928void ExtensionService::DisableUserExtensions(
929 const std::vector<std::string>& except_ids) {
[email protected]e516e4c2013-06-12 17:41:14930 extensions::ManagementPolicy* management_policy =
931 system_->management_policy();
932 extensions::ExtensionList to_disable;
933
[email protected]bb1bc9b32013-12-21 03:09:14934 const ExtensionSet& enabled_set = registry_->enabled_extensions();
935 for (ExtensionSet::const_iterator extension = enabled_set.begin();
936 extension != enabled_set.end(); ++extension) {
[email protected]ab894542014-07-03 12:35:39937 if (management_policy->UserMayModifySettings(extension->get(), NULL))
[email protected]e516e4c2013-06-12 17:41:14938 to_disable.push_back(*extension);
939 }
[email protected]bb1bc9b32013-12-21 03:09:14940 const ExtensionSet& terminated_set = registry_->terminated_extensions();
941 for (ExtensionSet::const_iterator extension = terminated_set.begin();
942 extension != terminated_set.end(); ++extension) {
[email protected]ab894542014-07-03 12:35:39943 if (management_policy->UserMayModifySettings(extension->get(), NULL))
[email protected]e516e4c2013-06-12 17:41:14944 to_disable.push_back(*extension);
945 }
946
947 for (extensions::ExtensionList::const_iterator extension = to_disable.begin();
948 extension != to_disable.end(); ++extension) {
[email protected]3082fe32013-08-06 11:12:38949 if ((*extension)->was_installed_by_default() &&
950 extension_urls::IsWebstoreUpdateUrl(
dchengc7047942014-08-26 05:05:31951 extensions::ManifestURL::GetUpdateURL(extension->get())))
[email protected]3082fe32013-08-06 11:12:38952 continue;
[email protected]1abf05e2013-07-09 17:04:36953 const std::string& id = (*extension)->id();
954 if (except_ids.end() == std::find(except_ids.begin(), except_ids.end(), id))
955 DisableExtension(id, extensions::Extension::DISABLE_USER_ACTION);
[email protected]e516e4c2013-06-12 17:41:14956 }
957}
958
mlerman6a37b6a42014-11-26 22:10:53959// Extensions that are not locked, components or forced by policy should be
960// locked. Extensions are no longer considered enabled or disabled. Blacklisted
961// extensions are now considered both blacklisted and locked.
962void ExtensionService::BlockAllExtensions() {
963 if (block_extensions_)
964 return;
965 block_extensions_ = true;
966
967 // Blacklisted extensions are already unloaded, need not be blocked.
968 scoped_ptr<ExtensionSet> extensions =
969 registry_->GenerateInstalledExtensionsSet(ExtensionRegistry::ENABLED |
970 ExtensionRegistry::DISABLED |
971 ExtensionRegistry::TERMINATED);
972
973 for (const scoped_refptr<const Extension>& extension : *extensions) {
974 const std::string& id = extension->id();
975
976 if (!CanBlockExtension(extension.get()))
977 continue;
978
979 registry_->RemoveEnabled(id);
980 registry_->RemoveDisabled(id);
981 registry_->RemoveTerminated(id);
982
983 registry_->AddBlocked(extension.get());
984 UnloadExtension(id, extensions::UnloadedExtensionInfo::REASON_LOCK_ALL);
985 }
986}
987
988// All locked extensions should revert to being either enabled or disabled
989// as appropriate.
990void ExtensionService::UnblockAllExtensions() {
991 block_extensions_ = false;
992 scoped_ptr<ExtensionSet> to_unblock =
993 registry_->GenerateInstalledExtensionsSet(ExtensionRegistry::BLOCKED);
994
995 for (const scoped_refptr<const Extension>& extension : *to_unblock) {
996 registry_->RemoveBlocked(extension->id());
997 AddExtension(extension.get());
998 }
999}
1000
[email protected]eaa7dd182010-12-14 11:09:001001void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]009633c2013-03-07 22:08:281002 const Extension* extension) {
1003 GrantPermissions(extension);
treib2e0517f92015-05-07 23:15:361004 RecordPermissionMessagesHistogram(extension, "ReEnable");
[email protected]8d888c12010-11-30 00:00:251005 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
1006 EnableExtension(extension->id());
1007}
1008
[email protected]009633c2013-03-07 22:08:281009void ExtensionService::GrantPermissions(const Extension* extension) {
[email protected]be083862012-09-01 03:53:451010 CHECK(extension);
[email protected]8d42db52014-06-20 21:50:501011 extensions::PermissionsUpdater(profile()).GrantActivePermissions(extension);
[email protected]be083862012-09-01 03:53:451012}
1013
[email protected]fe2dd7742011-04-19 22:52:491014// static
1015void ExtensionService::RecordPermissionMessagesHistogram(
[email protected]13c68b62013-05-17 11:29:051016 const Extension* extension, const char* histogram) {
[email protected]de415552013-01-23 04:12:171017 // Since this is called from multiple sources, and since the histogram macros
1018 // use statics, we need to manually lookup the histogram ourselves.
treib2e0517f92015-05-07 23:15:361019 base::HistogramBase* legacy_counter = base::LinearHistogram::FactoryGet(
1020 base::StringPrintf("Extensions.Permissions_%s2", histogram),
[email protected]fe2dd7742011-04-19 22:52:491021 1,
[email protected]c2e66e12012-06-27 06:27:061022 PermissionMessage::kEnumBoundary,
1023 PermissionMessage::kEnumBoundary + 1,
[email protected]de415552013-01-23 04:12:171024 base::HistogramBase::kUmaTargetedHistogramFlag);
[email protected]fe2dd7742011-04-19 22:52:491025
treib2e0517f92015-05-07 23:15:361026 // TODO(treib): Remove the legacy "2" histograms. See crbug.com/484102.
1027 PermissionMessageIDs legacy_permissions =
treibb566ee832015-03-16 10:20:461028 extension->permissions_data()->GetLegacyPermissionMessageIDs();
treib2e0517f92015-05-07 23:15:361029 if (legacy_permissions.empty()) {
1030 legacy_counter->Add(PermissionMessage::kNone);
[email protected]fe2dd7742011-04-19 22:52:491031 } else {
treib2e0517f92015-05-07 23:15:361032 for (PermissionMessage::ID id : legacy_permissions)
1033 legacy_counter->Add(id);
[email protected]fe2dd7742011-04-19 22:52:491034 }
treib2e0517f92015-05-07 23:15:361035
1036 base::HistogramBase* counter = base::LinearHistogram::FactoryGet(
1037 base::StringPrintf("Extensions.Permissions_%s3", histogram),
1038 1,
1039 APIPermission::kEnumBoundary,
1040 APIPermission::kEnumBoundary + 1,
1041 base::HistogramBase::kUmaTargetedHistogramFlag);
1042
1043 base::HistogramBase* counter_has_any = base::BooleanHistogram::FactoryGet(
1044 base::StringPrintf("Extensions.HasPermissions_%s3", histogram),
1045 base::HistogramBase::kUmaTargetedHistogramFlag);
1046
1047 PermissionIDSet permissions =
1048 extensions::PermissionMessageProvider::Get()->GetAllPermissionIDs(
1049 extension->permissions_data()->active_permissions().get(),
1050 extension->GetType());
1051 counter_has_any->AddBoolean(!permissions.empty());
1052 for (const PermissionID& id : permissions)
1053 counter->Add(id.id());
[email protected]fe2dd7742011-04-19 22:52:491054}
1055
[email protected]eaa7dd182010-12-14 11:09:001056void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]f9c8c7c72014-07-31 16:42:311057 // The URLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:061058 // was loaded, otherwise a race can arise where a renderer that is created
1059 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:421060 // that the request context doesn't yet know about. The profile is responsible
1061 // for ensuring its URLRequestContexts appropriately discover the loaded
1062 // extension.
[email protected]31d8f5f22012-04-02 15:22:081063 system_->RegisterExtensionWithRequestContexts(extension);
[email protected]62d30f42009-10-01 22:36:061064
[email protected]d5949312012-12-03 22:13:301065 // Tell renderers about the new extension, unless it's a theme (renderers
1066 // don't need to know about themes).
[email protected]a58f599c2012-12-01 03:08:191067 if (!extension->is_theme()) {
[email protected]19647262011-12-16 09:57:491068 for (content::RenderProcessHost::iterator i(
1069 content::RenderProcessHost::AllHostsIterator());
1070 !i.IsAtEnd(); i.Advance()) {
1071 content::RenderProcessHost* host = i.GetCurrentValue();
1072 Profile* host_profile =
1073 Profile::FromBrowserContext(host->GetBrowserContext());
1074 if (host_profile->GetOriginalProfile() ==
1075 profile_->GetOriginalProfile()) {
rdevlin.croninc491dbf2015-02-07 00:31:551076 // We don't need to include tab permisisons here, since the extension
1077 // was just loaded.
[email protected]19647262011-12-16 09:57:491078 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions(
rdevlin.croninc491dbf2015-02-07 00:31:551079 1, ExtensionMsg_Loaded_Params(extension,
1080 false /* no tab permissions */));
[email protected]19647262011-12-16 09:57:491081 host->Send(
1082 new ExtensionMsg_Loaded(loaded_extensions));
1083 }
[email protected]c8d407e2011-04-28 21:27:171084 }
[email protected]77a6970c2011-04-23 16:58:561085 }
1086
[email protected]3442a662012-01-12 08:06:171087 // Tell subsystems that use the EXTENSION_LOADED notification about the new
1088 // extension.
1089 //
1090 // NOTE: It is important that this happen after notifying the renderers about
1091 // the new extensions so that if we navigate to an extension URL in
[email protected]fdc76192014-04-20 21:54:411092 // ExtensionRegistryObserver::OnLoaded or
1093 // NOTIFICATION_EXTENSION_LOADED_DEPRECATED, the
[email protected]dcc47642014-03-26 22:03:491094 // renderer is guaranteed to know about it.
1095 registry_->TriggerOnLoaded(extension);
1096
[email protected]3442a662012-01-12 08:06:171097 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061098 extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
[email protected]3442a662012-01-12 08:06:171099 content::Source<Profile>(profile_),
1100 content::Details<const Extension>(extension));
1101
[email protected]dcc47642014-03-26 22:03:491102 // TODO(kalman): Convert ExtensionSpecialStoragePolicy to a
1103 // BrowserContextKeyedService and use ExtensionRegistryObserver.
[email protected]c8d407e2011-04-28 21:27:171104 profile_->GetExtensionSpecialStoragePolicy()->
rdevlin.croninbc8979a2014-09-30 01:08:501105 GrantRightsForExtension(extension, profile_);
[email protected]c8d407e2011-04-28 21:27:171106
[email protected]dcc47642014-03-26 22:03:491107 // TODO(kalman): This is broken. The crash reporter is process-wide so doesn't
1108 // work properly multi-profile. Besides which, it should be using
1109 // ExtensionRegistryObserver. See http://crbug.com/355029.
[email protected]c8d407e2011-04-28 21:27:171110 UpdateActiveExtensionsInCrashReporter();
1111
[email protected]d695bb12014-06-05 16:16:301112 const extensions::PermissionsData* permissions_data =
[email protected]a6910e72014-06-06 05:04:361113 extension->permissions_data();
[email protected]d695bb12014-06-05 16:16:301114
[email protected]c8d407e2011-04-28 21:27:171115 // If the extension has permission to load chrome://favicon/ resources we need
1116 // to make sure that the FaviconSource is registered with the
1117 // ChromeURLDataManager.
[email protected]d695bb12014-06-05 16:16:301118 if (permissions_data->HasHostPermission(GURL(chrome::kChromeUIFaviconURL))) {
[email protected]c8d407e2011-04-28 21:27:171119 FaviconSource* favicon_source = new FaviconSource(profile_,
1120 FaviconSource::FAVICON);
[email protected]24ea7a12013-01-27 23:54:531121 content::URLDataSource::Add(profile_, favicon_source);
[email protected]c8d407e2011-04-28 21:27:171122 }
[email protected]b07e606e2012-09-15 20:16:151123
[email protected]b07e606e2012-09-15 20:16:151124 // Same for chrome://theme/ resources.
[email protected]d695bb12014-06-05 16:16:301125 if (permissions_data->HasHostPermission(GURL(chrome::kChromeUIThemeURL))) {
[email protected]b07e606e2012-09-15 20:16:151126 ThemeSource* theme_source = new ThemeSource(profile_);
[email protected]24ea7a12013-01-27 23:54:531127 content::URLDataSource::Add(profile_, theme_source);
[email protected]b07e606e2012-09-15 20:16:151128 }
[email protected]b07e606e2012-09-15 20:16:151129
[email protected]5eddc3e2011-10-26 04:33:311130 // Same for chrome://thumb/ resources.
[email protected]d695bb12014-06-05 16:16:301131 if (permissions_data->HasHostPermission(
1132 GURL(chrome::kChromeUIThumbnailURL))) {
[email protected]420e6d92013-09-17 01:48:511133 ThumbnailSource* thumbnail_source = new ThumbnailSource(profile_, false);
[email protected]24ea7a12013-01-27 23:54:531134 content::URLDataSource::Add(profile_, thumbnail_source);
[email protected]5eddc3e2011-10-26 04:33:311135 }
[email protected]62d30f42009-10-01 22:36:061136}
1137
[email protected]a9f39a312010-12-23 22:14:271138void ExtensionService::NotifyExtensionUnloaded(
[email protected]814a7bf0f2011-08-13 05:30:591139 const Extension* extension,
[email protected]b0af4792013-10-23 09:12:131140 UnloadedExtensionInfo::Reason reason) {
[email protected]a9f39a312010-12-23 22:14:271141 UnloadedExtensionInfo details(extension, reason);
[email protected]e51232f32014-04-18 20:05:361142
1143 registry_->TriggerOnUnloaded(extension, reason);
1144
[email protected]ad50def52011-10-19 23:17:071145 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061146 extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
[email protected]6c2381d2011-10-19 02:52:531147 content::Source<Profile>(profile_),
1148 content::Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061149
[email protected]f3b1a082011-11-18 00:34:301150 for (content::RenderProcessHost::iterator i(
1151 content::RenderProcessHost::AllHostsIterator());
[email protected]77a6970c2011-04-23 16:58:561152 !i.IsAtEnd(); i.Advance()) {
[email protected]f3b1a082011-11-18 00:34:301153 content::RenderProcessHost* host = i.GetCurrentValue();
[email protected]9b62ecf2011-07-27 20:23:081154 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:301155 Profile::FromBrowserContext(host->GetBrowserContext());
[email protected]9b62ecf2011-07-27 20:23:081156 if (host_profile->GetOriginalProfile() == profile_->GetOriginalProfile())
[email protected]c8d407e2011-04-28 21:27:171157 host->Send(new ExtensionMsg_Unloaded(extension->id()));
[email protected]77a6970c2011-04-23 16:58:561158 }
1159
[email protected]31d8f5f22012-04-02 15:22:081160 system_->UnregisterExtensionWithRequestContexts(extension->id(), reason);
[email protected]dcc47642014-03-26 22:03:491161
1162 // TODO(kalman): Convert ExtensionSpecialStoragePolicy to a
1163 // BrowserContextKeyedService and use ExtensionRegistryObserver.
[email protected]c8d407e2011-04-28 21:27:171164 profile_->GetExtensionSpecialStoragePolicy()->
1165 RevokeRightsForExtension(extension);
1166
[email protected]b777b332011-04-16 04:01:081167#if defined(OS_CHROMEOS)
[email protected]ac66e452013-01-11 09:21:231168 // Revoke external file access for the extension from its file system context.
1169 // It is safe to access the extension's storage partition at this point. The
1170 // storage partition may get destroyed only after the extension gets unloaded.
[email protected]3a746ec2014-03-15 05:30:561171 GURL site =
1172 extensions::util::GetSiteForExtensionId(extension->id(), profile_);
[email protected]cd501a72014-08-22 19:58:311173 storage::FileSystemContext* filesystem_context =
1174 BrowserContext::GetStoragePartitionForSite(profile_, site)
1175 ->GetFileSystemContext();
[email protected]f19bbf62013-07-09 01:22:321176 if (filesystem_context && filesystem_context->external_backend()) {
1177 filesystem_context->external_backend()->
[email protected]c8d407e2011-04-28 21:27:171178 RevokeAccessForExtension(extension->id());
[email protected]62d30f42009-10-01 22:36:061179 }
[email protected]c8d407e2011-04-28 21:27:171180#endif
1181
[email protected]dcc47642014-03-26 22:03:491182 // TODO(kalman): This is broken. The crash reporter is process-wide so doesn't
1183 // work properly multi-profile. Besides which, it should be using
1184 // ExtensionRegistryObserver::OnExtensionLoaded. See http://crbug.com/355029.
[email protected]c8d407e2011-04-28 21:27:171185 UpdateActiveExtensionsInCrashReporter();
[email protected]62d30f42009-10-01 22:36:061186}
1187
[email protected]599539802014-01-07 23:06:001188content::BrowserContext* ExtensionService::GetBrowserContext() const {
1189 // Implemented in the .cc file to avoid adding a profile.h dependency to
1190 // extension_service.h.
1191 return profile_;
1192}
1193
[email protected]25ae0152011-11-18 14:40:021194bool ExtensionService::is_ready() {
[email protected]4a10006a2013-05-17 23:18:351195 return ready_->is_signaled();
[email protected]25ae0152011-11-18 14:40:021196}
1197
[email protected]7f8f24f2012-11-15 19:40:141198base::SequencedTaskRunner* ExtensionService::GetFileTaskRunner() {
[email protected]dc24976f2013-06-02 21:15:091199 if (file_task_runner_.get())
1200 return file_task_runner_.get();
[email protected]7f8f24f2012-11-15 19:40:141201
1202 // We should be able to interrupt any part of extension install process during
1203 // shutdown. SKIP_ON_SHUTDOWN ensures that not started extension install tasks
1204 // will be ignored/deleted while we will block on started tasks.
1205 std::string token("ext_install-");
1206 token.append(profile_->GetPath().AsUTF8Unsafe());
1207 file_task_runner_ = BrowserThread::GetBlockingPool()->
1208 GetSequencedTaskRunnerWithShutdownBehavior(
1209 BrowserThread::GetBlockingPool()->GetNamedSequenceToken(token),
1210 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
[email protected]dc24976f2013-06-02 21:15:091211 return file_task_runner_.get();
[email protected]7f8f24f2012-11-15 19:40:141212}
1213
[email protected]4ee07c62012-08-21 12:40:421214void ExtensionService::CheckManagementPolicy() {
[email protected]0d54b682013-11-05 14:15:361215 std::vector<std::string> to_unload;
1216 std::map<std::string, Extension::DisableReason> to_disable;
binjin8e3d0182014-12-04 16:44:281217 std::vector<std::string> to_enable;
[email protected]695b5712012-12-06 23:55:281218
[email protected]0d54b682013-11-05 14:15:361219 // Loop through the extensions list, finding extensions we need to unload or
1220 // disable.
binjin8e3d0182014-12-04 16:44:281221 for (scoped_refptr<const Extension> extension :
1222 registry_->enabled_extensions()) {
1223 if (!system_->management_policy()->UserMayLoad(extension.get(), nullptr))
[email protected]0d54b682013-11-05 14:15:361224 to_unload.push_back(extension->id());
1225 Extension::DisableReason disable_reason = Extension::DISABLE_NONE;
1226 if (system_->management_policy()->MustRemainDisabled(
binjin8e3d0182014-12-04 16:44:281227 extension.get(), &disable_reason, nullptr))
[email protected]0d54b682013-11-05 14:15:361228 to_disable[extension->id()] = disable_reason;
[email protected]aa96d3a2010-08-21 08:45:251229 }
1230
binjin8e3d0182014-12-04 16:44:281231 extensions::ExtensionManagement* management =
1232 extensions::ExtensionManagementFactory::GetForBrowserContext(profile());
1233
1234 // Loop through the disabled extension list, find extensions to re-enable
1235 // automatically. These extensions are exclusive from the |to_disable| and
1236 // |to_unload| lists constructed above, since disabled_extensions() and
1237 // enabled_extensions() are supposed to be mutually exclusive.
1238 for (scoped_refptr<const Extension> extension :
1239 registry_->disabled_extensions()) {
1240 // Find all disabled extensions disabled due to minimum version requirement,
1241 // but now satisfying it.
1242 if (management->CheckMinimumVersion(extension.get(), nullptr) &&
1243 extension_prefs_->HasDisableReason(
1244 extension->id(), Extension::DISABLE_UPDATE_REQUIRED_BY_POLICY)) {
1245 // Is DISABLE_UPDATE_REQUIRED_BY_POLICY the *only* reason?
1246 if (extension_prefs_->GetDisableReasons(extension->id()) ==
1247 Extension::DISABLE_UPDATE_REQUIRED_BY_POLICY) {
1248 // We need to enable those disabled *only* due to minimum version
1249 // requirement.
1250 to_enable.push_back(extension->id());
1251 }
1252 extension_prefs_->RemoveDisableReason(
1253 extension->id(), Extension::DISABLE_UPDATE_REQUIRED_BY_POLICY);
1254 }
1255 }
1256
1257 for (const std::string& id : to_unload)
1258 UnloadExtension(id, UnloadedExtensionInfo::REASON_DISABLE);
[email protected]0d54b682013-11-05 14:15:361259
1260 for (std::map<std::string, Extension::DisableReason>::const_iterator i =
1261 to_disable.begin(); i != to_disable.end(); ++i)
1262 DisableExtension(i->first, i->second);
binjin8e3d0182014-12-04 16:44:281263
1264 // No extension is getting re-enabled here after disabling/unloading
1265 // because to_enable is mutually exclusive to to_disable + to_unload.
1266 for (const std::string& id : to_enable)
1267 EnableExtension(id);
1268
1269 if (updater_.get()) {
1270 // Find all extensions disabled due to minimum version requirement from
1271 // policy (including the ones that got disabled just now), and check
1272 // for update.
1273 extensions::ExtensionUpdater::CheckParams to_recheck;
1274 for (scoped_refptr<const Extension> extension :
1275 registry_->disabled_extensions()) {
1276 if (extension_prefs_->GetDisableReasons(extension->id()) ==
1277 Extension::DISABLE_UPDATE_REQUIRED_BY_POLICY) {
1278 // The minimum version check is the only thing holding this extension
1279 // back, so check if it can be updated to fix that.
1280 to_recheck.ids.push_back(extension->id());
1281 }
1282 }
1283 if (!to_recheck.ids.empty())
1284 updater_->CheckNow(to_recheck);
1285 }
[email protected]aa96d3a2010-08-21 08:45:251286}
1287
[email protected]31206602011-04-13 23:07:321288void ExtensionService::CheckForUpdatesSoon() {
[email protected]90878c52014-04-04 18:21:021289 // This can legitimately happen in unit tests.
1290 if (!updater_.get())
1291 return;
1292
1293 if (AreAllExternalProvidersReady()) {
1294 updater_->CheckSoon();
[email protected]c3cfb012011-04-06 22:07:351295 } else {
[email protected]90878c52014-04-04 18:21:021296 // Sync can start updating before all the external providers are ready
1297 // during startup. Start the update as soon as those providers are ready,
1298 // but not before.
1299 update_once_all_providers_are_ready_ = true;
[email protected]c3cfb012011-04-06 22:07:351300 }
1301}
1302
[email protected]8e4560b62011-01-14 10:09:141303// Some extensions will autoupdate themselves externally from Chrome. These
rkaplowa8fd8d32015-02-25 21:27:561304// are typically part of some larger client application package. To support
1305// these, the extension will register its location in the preferences file
[email protected]8e4560b62011-01-14 10:09:141306// (and also, on Windows, in the registry) and this code will periodically
1307// check that location for a .crx file, which it will then install locally if
1308// a new version is available.
rkaplowa8fd8d32015-02-25 21:27:561309// Errors are reported through ExtensionErrorReporter. Success is not
[email protected]8e4560b62011-01-14 10:09:141310// reported.
[email protected]eaa7dd182010-12-14 11:09:001311void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:121312 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
rkaplowa8fd8d32015-02-25 21:27:561313 TRACE_EVENT0("browser,startup", "ExtensionService::CheckForExternalUpdates");
1314 SCOPED_UMA_HISTOGRAM_TIMER("Extensions.CheckForExternalUpdatesTime");
[email protected]8e4560b62011-01-14 10:09:141315
1316 // Note that this installation is intentionally silent (since it didn't
1317 // go through the front-end). Extensions that are registered in this
1318 // way are effectively considered 'pre-bundled', and so implicitly
1319 // trusted. In general, if something has HKLM or filesystem access,
1320 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:121321
[email protected]8e4560b62011-01-14 10:09:141322 // Ask each external extension provider to give us a call back for each
1323 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
[email protected]5df038b2012-07-16 19:03:271324 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141325 for (i = external_extension_providers_.begin();
1326 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:271327 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:141328 provider->VisitRegisteredExtension();
1329 }
1330
[email protected]50067e52011-10-20 23:17:071331 // Do any required work that we would have done after completion of all
1332 // providers.
[email protected]373daf972014-04-10 01:50:441333 if (external_extension_providers_.empty())
[email protected]50067e52011-10-20 23:17:071334 OnAllExternalProvidersReady();
[email protected]9f1087e2009-06-15 17:29:321335}
1336
[email protected]50067e52011-10-20 23:17:071337void ExtensionService::OnExternalProviderReady(
[email protected]5df038b2012-07-16 19:03:271338 const extensions::ExternalProviderInterface* provider) {
[email protected]a29a517a2011-01-21 21:11:121339 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]50067e52011-10-20 23:17:071340 CHECK(provider->IsReady());
[email protected]a29a517a2011-01-21 21:11:121341
1342 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:141343 // if all of them are finished. So we check them first.
[email protected]94fde232012-04-27 10:22:301344 if (AreAllExternalProvidersReady())
1345 OnAllExternalProvidersReady();
1346}
1347
1348bool ExtensionService::AreAllExternalProvidersReady() const {
[email protected]5df038b2012-07-16 19:03:271349 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141350 for (i = external_extension_providers_.begin();
1351 i != external_extension_providers_.end(); ++i) {
[email protected]94fde232012-04-27 10:22:301352 if (!i->get()->IsReady())
1353 return false;
[email protected]8e4560b62011-01-14 10:09:141354 }
[email protected]94fde232012-04-27 10:22:301355 return true;
[email protected]50067e52011-10-20 23:17:071356}
1357
1358void ExtensionService::OnAllExternalProvidersReady() {
1359 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]94fde232012-04-27 10:22:301360 base::TimeDelta elapsed = base::Time::Now() - profile_->GetStartTime();
1361 UMA_HISTOGRAM_TIMES("Extension.ExternalProvidersReadyAfter", elapsed);
[email protected]50067e52011-10-20 23:17:071362
1363 // Install any pending extensions.
[email protected]94fde232012-04-27 10:22:301364 if (update_once_all_providers_are_ready_ && updater()) {
1365 update_once_all_providers_are_ready_ = false;
[email protected]4e9b59d2013-12-18 06:47:141366 extensions::ExtensionUpdater::CheckParams params;
1367 params.callback = external_updates_finished_callback_;
1368 updater()->CheckNow(params);
[email protected]8e4560b62011-01-14 10:09:141369 }
1370
1371 // Uninstall all the unclaimed extensions.
[email protected]45759612012-07-10 17:21:231372 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> extensions_info(
[email protected]8e4560b62011-01-14 10:09:141373 extension_prefs_->GetInstalledExtensionsInfo());
1374 for (size_t i = 0; i < extensions_info->size(); ++i) {
1375 ExtensionInfo* info = extensions_info->at(i).get();
[email protected]1d5e58b2013-01-31 08:41:401376 if (Manifest::IsExternalLocation(info->extension_location))
[email protected]8e4560b62011-01-14 10:09:141377 CheckExternalUninstall(info->extension_id);
1378 }
[email protected]e5af875f2011-10-10 21:09:141379
[email protected]373daf972014-04-10 01:50:441380 error_controller_->ShowErrorIfNeeded();
[email protected]a9aa5932012-01-25 08:27:401381
[email protected]374ceb6f2014-07-02 19:25:341382 external_install_manager_->UpdateExternalExtensionAlert();
[email protected]e5af875f2011-10-10 21:09:141383}
1384
[email protected]a9f39a312010-12-23 22:14:271385void ExtensionService::UnloadExtension(
1386 const std::string& extension_id,
[email protected]b0af4792013-10-23 09:12:131387 UnloadedExtensionInfo::Reason reason) {
[email protected]27e469a2010-01-11 20:35:091388 // Make sure the extension gets deleted after we return from this function.
[email protected]599539802014-01-07 23:06:001389 int include_mask =
1390 ExtensionRegistry::EVERYTHING & ~ExtensionRegistry::TERMINATED;
[email protected]9adb9692010-10-29 23:14:021391 scoped_refptr<const Extension> extension(
[email protected]599539802014-01-07 23:06:001392 registry_->GetExtensionById(extension_id, include_mask));
[email protected]631cf822009-05-15 07:01:251393
[email protected]fa0c96732010-11-17 00:14:231394 // This method can be called via PostTask, so the extension may have been
1395 // unloaded by the time this runs.
[email protected]dc24976f2013-06-02 21:15:091396 if (!extension.get()) {
[email protected]dd163fb02011-05-04 22:22:171397 // In case the extension may have crashed/uninstalled. Allow the profile to
1398 // clean up its RequestContexts.
[email protected]31d8f5f22012-04-02 15:22:081399 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]fa0c96732010-11-17 00:14:231400 return;
[email protected]dd163fb02011-05-04 22:22:171401 }
[email protected]0c6da502009-08-14 22:32:391402
[email protected]1eb175082010-02-10 09:26:161403 // Keep information about the extension so that we can reload it later
1404 // even if it's not permanently installed.
1405 unloaded_extension_paths_[extension->id()] = extension->path();
1406
[email protected]f17dbd42010-08-16 23:21:101407 // Clean up if the extension is meant to be enabled after a reload.
[email protected]b914e2952013-04-26 07:10:031408 reloading_extensions_.erase(extension->id());
[email protected]f17dbd42010-08-16 23:21:101409
[email protected]bb1bc9b32013-12-21 03:09:141410 if (registry_->disabled_extensions().Contains(extension->id())) {
1411 registry_->RemoveDisabled(extension->id());
[email protected]dd163fb02011-05-04 22:22:171412 // Make sure the profile cleans up its RequestContexts when an already
1413 // disabled extension is unloaded (since they are also tracking the disabled
1414 // extensions).
[email protected]31d8f5f22012-04-02 15:22:081415 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]45f5b7d2014-01-22 23:47:131416 // Don't send the unloaded notification. It was sent when the extension
1417 // was disabled.
[email protected]60a174a2013-08-02 20:29:281418 } else {
[email protected]bb1bc9b32013-12-21 03:09:141419 // Remove the extension from the enabled list.
1420 registry_->RemoveEnabled(extension->id());
[email protected]60a174a2013-08-02 20:29:281421 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]0c6da502009-08-14 22:32:391422 }
1423
[email protected]fb789532013-08-27 02:40:211424 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061425 extensions::NOTIFICATION_EXTENSION_REMOVED,
[email protected]fb789532013-08-27 02:40:211426 content::Source<Profile>(profile_),
1427 content::Details<const Extension>(extension.get()));
[email protected]631cf822009-05-15 07:01:251428}
1429
[email protected]8b1ec202013-09-05 02:09:501430void ExtensionService::RemoveComponentExtension(
1431 const std::string& extension_id) {
1432 scoped_refptr<const Extension> extension(
1433 GetExtensionById(extension_id, false));
[email protected]b0af4792013-10-23 09:12:131434 UnloadExtension(extension_id, UnloadedExtensionInfo::REASON_UNINSTALL);
[email protected]ebe07772014-05-22 04:16:061435 if (extension.get()) {
[email protected]e43c61f2014-07-20 21:46:341436 ExtensionRegistry::Get(profile_)->TriggerOnUninstalled(
dtseng9fb3d5b2015-02-23 17:24:171437 extension.get(), extensions::UNINSTALL_REASON_COMPONENT_REMOVED);
[email protected]ebe07772014-05-22 04:16:061438 }
[email protected]8b1ec202013-09-05 02:09:501439}
1440
[email protected]bb1bc9b32013-12-21 03:09:141441void ExtensionService::UnloadAllExtensionsForTest() {
1442 UnloadAllExtensionsInternal();
[email protected]9f1087e2009-06-15 17:29:321443}
1444
[email protected]bb1bc9b32013-12-21 03:09:141445void ExtensionService::ReloadExtensionsForTest() {
1446 // Calling UnloadAllExtensionsForTest here triggers a false-positive presubmit
1447 // warning about calling test code in production.
1448 UnloadAllExtensionsInternal();
[email protected]eac88332012-12-26 17:57:451449 component_loader_->LoadAll();
[email protected]d8c8f25f2011-11-02 18:18:011450 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]4a10006a2013-05-17 23:18:351451 // Don't call SetReadyAndNotifyListeners() since tests call this multiple
1452 // times.
[email protected]9f1087e2009-06-15 17:29:321453}
1454
[email protected]820d9bd2013-04-03 03:46:031455void ExtensionService::SetReadyAndNotifyListeners() {
rkaplowa8fd8d32015-02-25 21:27:561456 TRACE_EVENT0("browser,startup",
1457 "ExtensionService::SetReadyAndNotifyListeners");
yiyaoliu252f915e2015-04-23 21:56:361458 TRACK_SCOPED_REGION(
1459 "Startup", "ExtensionService::SetReadyAndNotifyListeners");
rkaplowa8fd8d32015-02-25 21:27:561460 SCOPED_UMA_HISTOGRAM_TIMER(
1461 "Extensions.ExtensionServiceNotifyReadyListenersTime");
1462
[email protected]4a10006a2013-05-17 23:18:351463 ready_->Signal();
[email protected]820d9bd2013-04-03 03:46:031464 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061465 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED,
[email protected]820d9bd2013-04-03 03:46:031466 content::Source<Profile>(profile_),
1467 content::NotificationService::NoDetails());
[email protected]820d9bd2013-04-03 03:46:031468}
1469
[email protected]eaa7dd182010-12-14 11:09:001470void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]3eeddd892013-04-17 17:00:111471 if (updater_)
[email protected]93fd78f42009-07-10 16:43:171472 updater_->Start();
[email protected]fa6a9102010-11-22 15:38:501473
[email protected]bc151cf92013-02-12 04:57:261474 OnBlacklistUpdated();
[email protected]e72e8eb82009-06-18 17:21:511475}
1476
[email protected]fcb58a862012-05-01 01:03:151477void ExtensionService::AddExtension(const Extension* extension) {
[email protected]c8d407e2011-04-28 21:27:171478 // TODO(jstritar): We may be able to get rid of this branch by overriding the
1479 // default extension state to DISABLED when the --disable-extensions flag
1480 // is set (http://crbug.com/29067).
1481 if (!extensions_enabled() &&
1482 !extension->is_theme() &&
[email protected]1d5e58b2013-01-31 08:41:401483 extension->location() != Manifest::COMPONENT &&
1484 !Manifest::IsExternalLocation(extension->location())) {
[email protected]fcb58a862012-05-01 01:03:151485 return;
[email protected]330840c2012-08-29 22:21:011486 }
[email protected]c8d407e2011-04-28 21:27:171487
[email protected]b914e2952013-04-26 07:10:031488 bool is_extension_upgrade = false;
[email protected]116d40e2013-08-08 17:23:171489 bool is_extension_installed = false;
1490 const Extension* old = GetInstalledExtension(extension->id());
1491 if (old) {
1492 is_extension_installed = true;
1493 int version_compare_result =
1494 extension->version()->CompareTo(*(old->version()));
1495 is_extension_upgrade = version_compare_result > 0;
[email protected]b914e2952013-04-26 07:10:031496 // Other than for unpacked extensions, CrxInstaller should have guaranteed
1497 // that we aren't downgrading.
1498 if (!Manifest::IsUnpackedLocation(extension->location()))
[email protected]116d40e2013-08-08 17:23:171499 CHECK_GE(version_compare_result, 0);
[email protected]b914e2952013-04-26 07:10:031500 }
rdevlin.cronin2c16c602014-11-21 01:35:491501 // If the extension was disabled for a reload, then enable it.
1502 bool reloading = reloading_extensions_.erase(extension->id()) > 0;
1503
1504 // Set the upgraded bit; we consider reloads upgrades.
1505 system_->runtime_data()->SetBeingUpgraded(extension->id(),
1506 is_extension_upgrade || reloading);
[email protected]c8d407e2011-04-28 21:27:171507
[email protected]1eb175082010-02-10 09:26:161508 // The extension is now loaded, remove its data from unloaded extension map.
1509 unloaded_extension_paths_.erase(extension->id());
1510
[email protected]bb7f40952011-01-13 00:21:201511 // If a terminated extension is loaded, remove it from the terminated list.
1512 UntrackTerminatedExtension(extension->id());
1513
[email protected]b914e2952013-04-26 07:10:031514 // Check if the extension's privileges have changed and mark the
1515 // extension disabled if necessary.
[email protected]116d40e2013-08-08 17:23:171516 CheckPermissionsIncrease(extension, is_extension_installed);
[email protected]b914e2952013-04-26 07:10:031517
[email protected]116d40e2013-08-08 17:23:171518 if (is_extension_installed && !reloading) {
[email protected]ca2e82022013-04-29 16:48:411519 // To upgrade an extension in place, unload the old one and then load the
1520 // new one. ReloadExtension disables the extension, which is sufficient.
[email protected]b0af4792013-10-23 09:12:131521 UnloadExtension(extension->id(), UnloadedExtensionInfo::REASON_UPDATE);
[email protected]b914e2952013-04-26 07:10:031522 }
[email protected]0c6da502009-08-14 22:32:391523
[email protected]695b5712012-12-06 23:55:281524 if (extension_prefs_->IsExtensionBlacklisted(extension->id())) {
[email protected]ac875372013-02-28 04:36:091525 // Only prefs is checked for the blacklist. We rely on callers to check the
1526 // blacklist before calling into here, e.g. CrxInstaller checks before
[email protected]9f3c8532013-07-31 19:52:071527 // installation then threads through the install and pending install flow
1528 // of this class, and we check when loading installed extensions.
[email protected]bb1bc9b32013-12-21 03:09:141529 registry_->AddBlacklisted(extension);
mlerman6a37b6a42014-11-26 22:10:531530 } else if (block_extensions_ && CanBlockExtension(extension)) {
1531 registry_->AddBlocked(extension);
[email protected]ca2e82022013-04-29 16:48:411532 } else if (!reloading &&
1533 extension_prefs_->IsExtensionDisabled(extension->id())) {
[email protected]bb1bc9b32013-12-21 03:09:141534 registry_->AddDisabled(extension);
[email protected]f8aefb132013-10-30 09:29:521535 if (extension_sync_service_)
1536 extension_sync_service_->SyncExtensionChangeIfNeeded(*extension);
[email protected]44d62b62012-04-11 00:06:031537 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061538 extensions::NOTIFICATION_EXTENSION_UPDATE_DISABLED,
[email protected]44d62b62012-04-11 00:06:031539 content::Source<Profile>(profile_),
1540 content::Details<const Extension>(extension));
[email protected]62f051c2012-03-29 17:04:441541
[email protected]21db9ef2014-05-16 02:06:271542 // Show the extension disabled error if a permissions increase or a remote
1543 // installation is the reason it was disabled, and no other reasons exist.
1544 int reasons = extension_prefs_->GetDisableReasons(extension->id());
1545 const int kReasonMask = Extension::DISABLE_PERMISSIONS_INCREASE |
1546 Extension::DISABLE_REMOTE_INSTALL;
1547 if (reasons & kReasonMask && !(reasons & ~kReasonMask)) {
1548 extensions::AddExtensionDisabledError(
1549 this,
1550 extension,
1551 extension_prefs_->HasDisableReason(
1552 extension->id(), Extension::DISABLE_REMOTE_INSTALL));
[email protected]62f051c2012-03-29 17:04:441553 }
[email protected]ca2e82022013-04-29 16:48:411554 } else if (reloading) {
1555 // Replace the old extension with the new version.
[email protected]bb1bc9b32013-12-21 03:09:141556 CHECK(!registry_->AddDisabled(extension));
[email protected]ca2e82022013-04-29 16:48:411557 EnableExtension(extension->id());
[email protected]695b5712012-12-06 23:55:281558 } else {
1559 // All apps that are displayed in the launcher are ordered by their ordinals
1560 // so we must ensure they have valid ordinals.
1561 if (extension->RequiresSortOrdinal()) {
[email protected]db378322014-07-21 23:19:341562 extension_prefs_->app_sorting()->SetExtensionVisible(
1563 extension->id(),
1564 extension->ShouldDisplayInNewTabPage() &&
1565 !extension_prefs_->IsEphemeralApp(extension->id()));
1566 if (!extension_prefs_->IsEphemeralApp(extension->id())) {
1567 extension_prefs_->app_sorting()->EnsureValidOrdinals(
1568 extension->id(), syncer::StringOrdinal());
[email protected]bd36b652013-02-27 02:13:251569 }
[email protected]695b5712012-12-06 23:55:281570 }
1571
[email protected]bb1bc9b32013-12-21 03:09:141572 registry_->AddEnabled(extension);
[email protected]f8aefb132013-10-30 09:29:521573 if (extension_sync_service_)
1574 extension_sync_service_->SyncExtensionChangeIfNeeded(*extension);
[email protected]695b5712012-12-06 23:55:281575 NotifyExtensionLoaded(extension);
[email protected]f574c402012-12-04 23:20:311576 }
rdevlin.cronin2c16c602014-11-21 01:35:491577 system_->runtime_data()->SetBeingUpgraded(extension->id(), false);
[email protected]aab98a52009-12-02 03:22:351578}
1579
[email protected]8c484b742012-11-29 06:05:361580void ExtensionService::AddComponentExtension(const Extension* extension) {
1581 const std::string old_version_string(
1582 extension_prefs_->GetVersionString(extension->id()));
[email protected]c5e4a2222014-01-03 16:06:131583 const Version old_version(old_version_string);
[email protected]8c484b742012-11-29 06:05:361584
[email protected]0181a7d2013-11-12 01:08:421585 VLOG(1) << "AddComponentExtension " << extension->name();
[email protected]8c484b742012-11-29 06:05:361586 if (!old_version.IsValid() || !old_version.Equals(*extension->version())) {
1587 VLOG(1) << "Component extension " << extension->name() << " ("
1588 << extension->id() << ") installing/upgrading from '"
1589 << old_version_string << "' to " << extension->version()->GetString();
1590
1591 AddNewOrUpdatedExtension(extension,
[email protected]9ae73962014-08-14 16:53:411592 Extension::ENABLED,
[email protected]4a1d9c0d2014-06-13 12:50:111593 extensions::kInstallFlagNone,
[email protected]d8fd0fd2014-03-24 13:16:061594 syncer::StringOrdinal(),
1595 std::string());
[email protected]8c484b742012-11-29 06:05:361596 return;
1597 }
1598
1599 AddExtension(extension);
1600}
1601
[email protected]b914e2952013-04-26 07:10:031602void ExtensionService::CheckPermissionsIncrease(const Extension* extension,
[email protected]116d40e2013-08-08 17:23:171603 bool is_extension_installed) {
[email protected]23a85362014-07-07 23:26:191604 extensions::PermissionsUpdater(profile_).InitializePermissions(extension);
[email protected]902fd7b2011-07-27 18:42:311605
[email protected]8d888c12010-11-30 00:00:251606 // We keep track of all permissions the user has granted each extension.
1607 // This allows extensions to gracefully support backwards compatibility
1608 // by including unknown permissions in their manifests. When the user
1609 // installs the extension, only the recognized permissions are recorded.
1610 // When the unknown permissions become recognized (e.g., through browser
1611 // upgrade), we can prompt the user to accept these new permissions.
1612 // Extensions can also silently upgrade to less permissions, and then
1613 // silently upgrade to a version that adds these permissions back.
1614 //
1615 // For example, pretend that Chrome 10 includes a permission "omnibox"
1616 // for an API that adds suggestions to the omnibox. An extension can
1617 // maintain backwards compatibility while still having "omnibox" in the
1618 // manifest. If a user installs the extension on Chrome 9, the browser
1619 // will record the permissions it recognized, not including "omnibox."
1620 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
1621 // will disable the extension and prompt the user to approve the increase
1622 // in privileges. The extension could then release a new version that
1623 // removes the "omnibox" permission. When the user upgrades, Chrome will
1624 // still remember that "omnibox" had been granted, so that if the
1625 // extension once again includes "omnibox" in an upgrade, the extension
1626 // can upgrade without requiring this user's approval.
[email protected]eb5e4f92012-08-15 23:33:281627 int disable_reasons = extension_prefs_->GetDisableReasons(extension->id());
[email protected]8d888c12010-11-30 00:00:251628
rockot1d4a06262015-02-17 19:39:281629 // Silently grant all active permissions to default apps and apps installed
1630 // in kiosk mode.
[email protected]211a3f32013-05-28 21:48:181631 bool auto_grant_permission =
rockot1d4a06262015-02-17 19:39:281632 extension->was_installed_by_default() ||
[email protected]45928b682013-11-28 08:20:271633 extensions::ExtensionsBrowserClient::Get()->IsRunningInForcedAppMode();
[email protected]211a3f32013-05-28 21:48:181634 if (auto_grant_permission)
[email protected]b914e2952013-04-26 07:10:031635 GrantPermissions(extension);
1636
1637 bool is_privilege_increase = false;
1638 // We only need to compare the granted permissions to the current permissions
rockot1d4a06262015-02-17 19:39:281639 // if the extension has not been auto-granted its permissions above and is
1640 // installed internally.
1641 if (extension->location() == Manifest::INTERNAL && !auto_grant_permission) {
[email protected]8d888c12010-11-30 00:00:251642 // Add all the recognized permissions if the granted permissions list
1643 // hasn't been initialized yet.
[email protected]c2e66e12012-06-27 06:27:061644 scoped_refptr<PermissionSet> granted_permissions =
[email protected]902fd7b2011-07-27 18:42:311645 extension_prefs_->GetGrantedPermissions(extension->id());
[email protected]0d3e4a22011-06-23 19:02:521646 CHECK(granted_permissions.get());
[email protected]8d888c12010-11-30 00:00:251647
1648 // Here, we check if an extension's privileges have increased in a manner
1649 // that requires the user's approval. This could occur because the browser
1650 // upgraded and recognized additional privileges, or an extension upgrades
1651 // to a version that requires additional privileges.
[email protected]c41003472013-10-19 15:37:251652 is_privilege_increase =
1653 extensions::PermissionMessageProvider::Get()->IsPrivilegeIncrease(
dchengc7047942014-08-26 05:05:311654 granted_permissions.get(),
[email protected]076ebeda2014-06-06 21:47:261655 extension->permissions_data()->active_permissions().get(),
1656 extension->GetType());
[email protected]8d888c12010-11-30 00:00:251657 }
1658
[email protected]116d40e2013-08-08 17:23:171659 if (is_extension_installed) {
[email protected]44d62b62012-04-11 00:06:031660 // If the extension was already disabled, suppress any alerts for becoming
1661 // disabled on permissions increase.
[email protected]b914e2952013-04-26 07:10:031662 bool previously_disabled =
1663 extension_prefs_->IsExtensionDisabled(extension->id());
[email protected]7e9e2422012-12-13 19:54:191664 // Legacy disabled extensions do not have a disable reason. Infer that if
1665 // there was no permission increase, it was likely disabled by the user.
1666 if (previously_disabled && disable_reasons == Extension::DISABLE_NONE &&
[email protected]b914e2952013-04-26 07:10:031667 !extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
[email protected]7e9e2422012-12-13 19:54:191668 disable_reasons |= Extension::DISABLE_USER_ACTION;
[email protected]44d62b62012-04-11 00:06:031669 }
[email protected]7e9e2422012-12-13 19:54:191670 // Extensions that came to us disabled from sync need a similar inference,
1671 // except based on the new version's permissions.
1672 if (previously_disabled &&
1673 disable_reasons == Extension::DISABLE_UNKNOWN_FROM_SYNC) {
1674 // Remove the DISABLE_UNKNOWN_FROM_SYNC reason.
1675 extension_prefs_->ClearDisableReasons(extension->id());
1676 if (!is_privilege_increase)
1677 disable_reasons |= Extension::DISABLE_USER_ACTION;
1678 }
[email protected]b914e2952013-04-26 07:10:031679 disable_reasons &= ~Extension::DISABLE_UNKNOWN_FROM_SYNC;
[email protected]8d888c12010-11-30 00:00:251680 }
1681
1682 // Extension has changed permissions significantly. Disable it. A
[email protected]21db9ef2014-05-16 02:06:271683 // notification should be sent by the caller. If the extension is already
1684 // disabled because it was installed remotely, don't add another disable
1685 // reason, but instead always set the "did escalate permissions" flag, to
1686 // ensure enabling it will always show a warning.
1687 if (disable_reasons == Extension::DISABLE_REMOTE_INSTALL) {
1688 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
1689 } else if (is_privilege_increase) {
[email protected]7e9e2422012-12-13 19:54:191690 disable_reasons |= Extension::DISABLE_PERMISSIONS_INCREASE;
[email protected]fe2dd7742011-04-19 22:52:491691 if (!extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
treib2e0517f92015-05-07 23:15:361692 RecordPermissionMessagesHistogram(extension, "AutoDisable");
[email protected]fe2dd7742011-04-19 22:52:491693 }
[email protected]195c1582013-10-29 18:42:231694 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
[email protected]8d888c12010-11-30 00:00:251695 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
treib8ecc1eb52015-03-04 18:29:061696
1697#if defined(ENABLE_SUPERVISED_USERS)
1698 // If a custodian-installed extension is disabled for a supervised user due
1699 // to a permissions increase, send a request to the custodian, since the
treibf832a992015-03-24 18:09:241700 // supervised user themselves can't re-enable the extension.
treib8ecc1eb52015-03-04 18:29:061701 if (extensions::util::IsExtensionSupervised(extension, profile_)) {
1702 SupervisedUserService* supervised_user_service =
1703 SupervisedUserServiceFactory::GetForProfile(profile_);
1704 supervised_user_service->AddExtensionUpdateRequest(
treibf832a992015-03-24 18:09:241705 extension->id(), *extension->version(),
treib8ecc1eb52015-03-04 18:29:061706 base::Bind(ExtensionUpdateRequestSent, extension->id()));
1707 }
1708#endif
[email protected]b914e2952013-04-26 07:10:031709 }
1710 if (disable_reasons != Extension::DISABLE_NONE) {
[email protected]eb5e4f92012-08-15 23:33:281711 extension_prefs_->AddDisableReason(
1712 extension->id(),
1713 static_cast<Extension::DisableReason>(disable_reasons));
[email protected]8d888c12010-11-30 00:00:251714 }
1715}
1716
[email protected]eaa7dd182010-12-14 11:09:001717void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:391718 std::set<std::string> extension_ids;
[email protected]bb1bc9b32013-12-21 03:09:141719 const ExtensionSet& extensions = registry_->enabled_extensions();
1720 for (ExtensionSet::const_iterator iter = extensions.begin();
1721 iter != extensions.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361722 const Extension* extension = iter->get();
[email protected]1d5e58b2013-01-31 08:41:401723 if (!extension->is_theme() && extension->location() != Manifest::COMPONENT)
[email protected]84df8332011-12-06 18:22:461724 extension_ids.insert(extension->id());
[email protected]aab98a52009-12-02 03:22:351725 }
1726
[email protected]dcc47642014-03-26 22:03:491727 // TODO(kalman): This is broken. ExtensionService is per-profile.
1728 // crash_keys::SetActiveExtensions is per-process. See
1729 // http://crbug.com/355029.
[email protected]f6431be82013-09-07 02:53:451730 crash_keys::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:251731}
1732
[email protected]8266d662011-07-12 21:53:261733void ExtensionService::OnExtensionInstalled(
[email protected]36a5c4c2011-12-14 16:34:501734 const Extension* extension,
[email protected]98270432012-09-11 20:51:241735 const syncer::StringOrdinal& page_ordinal,
[email protected]4a1d9c0d2014-06-13 12:50:111736 int install_flags) {
[email protected]a29a517a2011-01-21 21:11:121737 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:201738
[email protected]c3cfb012011-04-06 22:07:351739 const std::string& id = extension->id();
binjin47947f842014-11-18 12:10:241740 int disable_reasons = GetDisableReasonsOnInstalled(extension);
[email protected]d8fd0fd2014-03-24 13:16:061741 std::string install_parameter;
[email protected]96aebe22014-07-16 04:07:511742 const extensions::PendingExtensionInfo* pending_extension_info =
1743 pending_extension_manager()->GetById(id);
1744 if (pending_extension_info) {
[email protected]8f3bcbd2013-06-05 08:42:401745 if (!pending_extension_info->ShouldAllowInstall(extension)) {
[email protected]51a3bf8b2012-06-08 22:53:061746 pending_extension_manager()->Remove(id);
[email protected]8ef78fd2010-08-19 17:14:321747
[email protected]31bb5ee62012-09-12 22:58:401748 LOG(WARNING) << "ShouldAllowInstall() returned false for "
1749 << id << " of type " << extension->GetType()
[email protected]65348062013-01-15 07:27:221750 << " and update URL "
1751 << extensions::ManifestURL::GetUpdateURL(extension).spec()
[email protected]31bb5ee62012-09-12 22:58:401752 << "; not installing";
[email protected]ae6cb3022011-04-04 20:11:541753
[email protected]4416c5a2010-06-26 01:28:571754 // Delete the extension directory since we're not going to
1755 // load it.
[email protected]7f8f24f2012-11-15 19:40:141756 if (!GetFileTaskRunner()->PostTask(
1757 FROM_HERE,
[email protected]85df9d12014-04-15 17:02:141758 base::Bind(&extensions::file_util::DeleteFile,
1759 extension->path(),
1760 true))) {
[email protected]14908b72011-04-20 06:54:361761 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:401762 }
[email protected]4416c5a2010-06-26 01:28:571763 return;
1764 }
[email protected]51a3bf8b2012-06-08 22:53:061765
[email protected]d8fd0fd2014-03-24 13:16:061766 install_parameter = pending_extension_info->install_parameter();
[email protected]51a3bf8b2012-06-08 22:53:061767 pending_extension_manager()->Remove(id);
[email protected]4416c5a2010-06-26 01:28:571768 } else {
[email protected]c3cfb012011-04-06 22:07:351769 // We explicitly want to re-enable an uninstalled external
1770 // extension; if we're here, that means the user is manually
1771 // installing the extension.
[email protected]46f3e102014-03-25 01:22:451772 if (extension_prefs_->IsExternalExtensionUninstalled(id)) {
binjin47947f842014-11-18 12:10:241773 disable_reasons = Extension::DISABLE_NONE;
[email protected]6cc7dbae2011-04-29 21:18:331774 }
[email protected]aa142702010-03-26 01:26:331775 }
1776
[email protected]98270432012-09-11 20:51:241777 // Unsupported requirements overrides the management policy.
[email protected]4a1d9c0d2014-06-13 12:50:111778 if (install_flags & extensions::kInstallFlagHasRequirementErrors) {
binjin47947f842014-11-18 12:10:241779 disable_reasons |= Extension::DISABLE_UNSUPPORTED_REQUIREMENT;
binjin8e3d0182014-12-04 16:44:281780 } else {
1781 // Requirement is supported now, remove the corresponding disable reason
1782 // instead.
1783 extension_prefs_->RemoveDisableReason(
1784 id, Extension::DISABLE_UNSUPPORTED_REQUIREMENT);
1785 disable_reasons &= ~Extension::DISABLE_UNSUPPORTED_REQUIREMENT;
1786 }
1787
1788 // Check if the extension was disabled because of the minimum version
1789 // requirements from enterprise policy, and satisfies it now.
1790 if (extensions::ExtensionManagementFactory::GetForBrowserContext(profile())
1791 ->CheckMinimumVersion(extension, nullptr)) {
1792 // And remove the corresponding disable reason.
1793 extension_prefs_->RemoveDisableReason(
1794 id, Extension::DISABLE_UPDATE_REQUIRED_BY_POLICY);
1795 disable_reasons &= ~Extension::DISABLE_UPDATE_REQUIRED_BY_POLICY;
[email protected]98270432012-09-11 20:51:241796 }
1797
[email protected]4a1d9c0d2014-06-13 12:50:111798 if (install_flags & extensions::kInstallFlagIsBlacklistedForMalware) {
[email protected]9f3c8532013-07-31 19:52:071799 // Installation of a blacklisted extension can happen from sync, policy,
1800 // etc, where to maintain consistency we need to install it, just never
1801 // load it (see AddExtension). Usually it should be the job of callers to
1802 // incercept blacklisted extension earlier (e.g. CrxInstaller, before even
1803 // showing the install dialogue).
[email protected]7c82539c2014-02-19 06:09:171804 extension_prefs_->AcknowledgeBlacklistedExtension(id);
[email protected]9f3c8532013-07-31 19:52:071805 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.SilentInstall",
1806 extension->location(),
1807 Manifest::NUM_LOCATIONS);
1808 }
1809
[email protected]695b5712012-12-06 23:55:281810 if (!GetInstalledExtension(extension->id())) {
[email protected]07533022011-06-27 20:42:551811 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
1812 extension->GetType(), 100);
[email protected]69084dc2012-11-17 07:39:301813 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallSource",
[email protected]1d5e58b2013-01-31 08:41:401814 extension->location(), Manifest::NUM_LOCATIONS);
treib2e0517f92015-05-07 23:15:361815 RecordPermissionMessagesHistogram(extension, "Install");
[email protected]69084dc2012-11-17 07:39:301816 } else {
1817 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateType",
1818 extension->GetType(), 100);
1819 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateSource",
[email protected]1d5e58b2013-01-31 08:41:401820 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]8f959f522014-08-06 06:26:281821
1822 // A fully installed app cannot be demoted to an ephemeral app.
1823 if ((install_flags & extensions::kInstallFlagIsEphemeral) &&
1824 !extension_prefs_->IsEphemeralApp(id)) {
1825 install_flags &= ~static_cast<int>(extensions::kInstallFlagIsEphemeral);
1826 }
[email protected]07533022011-06-27 20:42:551827 }
1828
binjin47947f842014-11-18 12:10:241829 if (disable_reasons)
deepak.m1f5c30272015-04-16 03:30:431830 extension_prefs_->AddDisableReason(id,
1831 static_cast<Extension::DisableReason>(disable_reasons));
binjin47947f842014-11-18 12:10:241832
[email protected]399583b2012-12-11 09:33:421833 const Extension::State initial_state =
binjin47947f842014-11-18 12:10:241834 disable_reasons == Extension::DISABLE_NONE ? Extension::ENABLED
1835 : Extension::DISABLED;
1836
[email protected]4a1d9c0d2014-06-13 12:50:111837 if (ShouldDelayExtensionUpdate(
1838 id,
1839 !!(install_flags & extensions::kInstallFlagInstallImmediately))) {
[email protected]9f3c8532013-07-31 19:52:071840 extension_prefs_->SetDelayedInstallInfo(
1841 extension,
1842 initial_state,
[email protected]4a1d9c0d2014-06-13 12:50:111843 install_flags,
[email protected]9f3c8532013-07-31 19:52:071844 extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE,
[email protected]d8fd0fd2014-03-24 13:16:061845 page_ordinal,
1846 install_parameter);
[email protected]0db124b02012-11-07 04:55:051847
1848 // Transfer ownership of |extension|.
[email protected]9f4e4f082013-06-21 07:11:191849 delayed_installs_.Insert(extension);
[email protected]0db124b02012-11-07 04:55:051850
[email protected]75bdcb872013-03-13 00:41:451851 // Notify observers that app update is available.
1852 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
[email protected]a9aa62b312013-11-29 05:35:061853 OnAppUpdateAvailable(extension));
[email protected]0db124b02012-11-07 04:55:051854 return;
1855 }
1856
[email protected]90878c52014-04-04 18:21:021857 extensions::SharedModuleService::ImportStatus status =
1858 shared_module_service_->SatisfyImports(extension);
[email protected]e5f8fffe2014-04-02 00:30:441859 if (installs_delayed_for_gc_) {
[email protected]9f3c8532013-07-31 19:52:071860 extension_prefs_->SetDelayedInstallInfo(
1861 extension,
1862 initial_state,
[email protected]4a1d9c0d2014-06-13 12:50:111863 install_flags,
[email protected]9f3c8532013-07-31 19:52:071864 extensions::ExtensionPrefs::DELAY_REASON_GC,
[email protected]d8fd0fd2014-03-24 13:16:061865 page_ordinal,
1866 install_parameter);
[email protected]399583b2012-12-11 09:33:421867 delayed_installs_.Insert(extension);
[email protected]90878c52014-04-04 18:21:021868 } else if (status != SharedModuleService::IMPORT_STATUS_OK) {
1869 if (status == SharedModuleService::IMPORT_STATUS_UNSATISFIED) {
[email protected]9f3c8532013-07-31 19:52:071870 extension_prefs_->SetDelayedInstallInfo(
1871 extension,
1872 initial_state,
[email protected]4a1d9c0d2014-06-13 12:50:111873 install_flags,
[email protected]9f4e4f082013-06-21 07:11:191874 extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IMPORTS,
[email protected]d8fd0fd2014-03-24 13:16:061875 page_ordinal,
1876 install_parameter);
[email protected]9f4e4f082013-06-21 07:11:191877 delayed_installs_.Insert(extension);
1878 }
[email protected]399583b2012-12-11 09:33:421879 } else {
[email protected]9f3c8532013-07-31 19:52:071880 AddNewOrUpdatedExtension(extension,
1881 initial_state,
[email protected]4a1d9c0d2014-06-13 12:50:111882 install_flags,
[email protected]d8fd0fd2014-03-24 13:16:061883 page_ordinal,
1884 install_parameter);
[email protected]399583b2012-12-11 09:33:421885 }
[email protected]8c484b742012-11-29 06:05:361886}
1887
binjin1569c9b2014-09-05 13:33:181888void ExtensionService::OnExtensionManagementSettingsChanged() {
1889 error_controller_->ShowErrorIfNeeded();
binjine6b58b52014-10-31 01:55:571890
1891 // Revokes blocked permissions from active_permissions for all extensions.
1892 extensions::ExtensionManagement* settings =
1893 extensions::ExtensionManagementFactory::GetForBrowserContext(profile());
1894 CHECK(settings);
1895 scoped_ptr<ExtensionSet> all_extensions(
1896 registry_->GenerateInstalledExtensionsSet());
1897 for (const auto& extension : *all_extensions.get()) {
1898 if (!settings->IsPermissionSetAllowed(
binjin685ade82014-11-06 09:53:561899 extension.get(),
binjine6b58b52014-10-31 01:55:571900 extension->permissions_data()->active_permissions())) {
1901 extensions::PermissionsUpdater(profile()).RemovePermissions(
1902 extension.get(),
binjin685ade82014-11-06 09:53:561903 settings->GetBlockedPermissions(extension.get()).get());
binjine6b58b52014-10-31 01:55:571904 }
1905 }
1906
binjin1569c9b2014-09-05 13:33:181907 CheckManagementPolicy();
1908}
1909
[email protected]8c484b742012-11-29 06:05:361910void ExtensionService::AddNewOrUpdatedExtension(
1911 const Extension* extension,
[email protected]399583b2012-12-11 09:33:421912 Extension::State initial_state,
[email protected]4a1d9c0d2014-06-13 12:50:111913 int install_flags,
[email protected]d8fd0fd2014-03-24 13:16:061914 const syncer::StringOrdinal& page_ordinal,
1915 const std::string& install_parameter) {
[email protected]8c484b742012-11-29 06:05:361916 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]760f743b2014-05-28 13:52:021917 bool was_ephemeral = extension_prefs_->IsEphemeralApp(extension->id());
[email protected]4a1d9c0d2014-06-13 12:50:111918 extension_prefs_->OnExtensionInstalled(
1919 extension, initial_state, page_ordinal, install_flags, install_parameter);
[email protected]9e9c1d12013-07-31 01:58:121920 delayed_installs_.Remove(extension->id());
[email protected]913ffca92014-03-27 15:26:161921 if (InstallVerifier::NeedsVerification(*extension))
juncai33e462102015-05-18 20:48:441922 InstallVerifier::Get(GetBrowserContext())->VerifyExtension(extension->id());
ryanackley48bedbd2015-01-27 23:12:141923
1924 const Extension* old = GetInstalledExtension(extension->id());
1925 if (extensions::AppDataMigrator::NeedsMigration(old, extension)) {
1926 app_data_migrator_->DoMigrationAndReply(
1927 old, extension,
1928 base::Bind(&ExtensionService::FinishInstallation, AsWeakPtr(),
1929 make_scoped_refptr(extension), was_ephemeral));
1930 return;
1931 }
1932
[email protected]760f743b2014-05-28 13:52:021933 FinishInstallation(extension, was_ephemeral);
[email protected]6f6101832012-11-27 22:10:481934}
1935
1936void ExtensionService::MaybeFinishDelayedInstallation(
1937 const std::string& extension_id) {
[email protected]9f4e4f082013-06-21 07:11:191938 // Check if the extension already got installed.
1939 if (!delayed_installs_.Contains(extension_id))
[email protected]6f6101832012-11-27 22:10:481940 return;
[email protected]9f4e4f082013-06-21 07:11:191941 extensions::ExtensionPrefs::DelayReason reason =
1942 extension_prefs_->GetDelayedInstallReason(extension_id);
1943
1944 // Check if the extension is idle. DELAY_REASON_NONE is used for older
1945 // preferences files that will not have set this field but it was previously
1946 // only used for idle updates.
1947 if ((reason == extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE ||
1948 reason == extensions::ExtensionPrefs::DELAY_REASON_NONE) &&
[email protected]1d5cf4142014-01-24 18:25:221949 is_ready() && !extensions::util::IsExtensionIdle(extension_id, profile_))
[email protected]6f6101832012-11-27 22:10:481950 return;
1951
[email protected]9f4e4f082013-06-21 07:11:191952 const Extension* extension = delayed_installs_.GetByID(extension_id);
1953 if (reason == extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IMPORTS) {
[email protected]90878c52014-04-04 18:21:021954 extensions::SharedModuleService::ImportStatus status =
1955 shared_module_service_->SatisfyImports(extension);
1956 if (status != SharedModuleService::IMPORT_STATUS_OK) {
1957 if (status == SharedModuleService::IMPORT_STATUS_UNRECOVERABLE) {
[email protected]9f4e4f082013-06-21 07:11:191958 delayed_installs_.Remove(extension_id);
1959 // Make sure no version of the extension is actually installed, (i.e.,
1960 // that this delayed install was not an update).
1961 CHECK(!extension_prefs_->GetInstalledExtensionInfo(extension_id).get());
1962 extension_prefs_->DeleteExtensionPrefs(extension_id);
1963 }
1964 return;
1965 }
1966 }
1967
[email protected]6f6101832012-11-27 22:10:481968 FinishDelayedInstallation(extension_id);
1969}
1970
1971void ExtensionService::FinishDelayedInstallation(
1972 const std::string& extension_id) {
1973 scoped_refptr<const Extension> extension(
1974 GetPendingExtensionUpdate(extension_id));
[email protected]dc24976f2013-06-02 21:15:091975 CHECK(extension.get());
[email protected]9f4e4f082013-06-21 07:11:191976 delayed_installs_.Remove(extension_id);
[email protected]6f6101832012-11-27 22:10:481977
[email protected]760f743b2014-05-28 13:52:021978 bool was_ephemeral = extension_prefs_->IsEphemeralApp(extension->id());
[email protected]399583b2012-12-11 09:33:421979 if (!extension_prefs_->FinishDelayedInstallInfo(extension_id))
[email protected]6f6101832012-11-27 22:10:481980 NOTREACHED();
1981
[email protected]760f743b2014-05-28 13:52:021982 FinishInstallation(extension.get(), was_ephemeral);
[email protected]6f6101832012-11-27 22:10:481983}
1984
[email protected]760f743b2014-05-28 13:52:021985void ExtensionService::FinishInstallation(
1986 const Extension* extension, bool was_ephemeral) {
[email protected]e7554c3f2013-05-29 00:36:561987 const extensions::Extension* existing_extension =
1988 GetInstalledExtension(extension->id());
1989 bool is_update = false;
1990 std::string old_name;
1991 if (existing_extension) {
1992 is_update = true;
1993 old_name = existing_extension->name();
1994 }
[email protected]760f743b2014-05-28 13:52:021995 bool from_ephemeral =
1996 was_ephemeral && !extension_prefs_->IsEphemeralApp(extension->id());
1997 extensions::InstalledExtensionInfo details(
1998 extension, is_update, from_ephemeral, old_name);
[email protected]fcb58a862012-05-01 01:03:151999 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:062000 extensions::NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED,
[email protected]fcb58a862012-05-01 01:03:152001 content::Source<Profile>(profile_),
[email protected]41bb80bd2013-05-03 10:56:022002 content::Details<const extensions::InstalledExtensionInfo>(&details));
[email protected]fcb58a862012-05-01 01:03:152003
[email protected]bc44b5da2014-06-12 14:20:002004 registry_->TriggerOnWillBeInstalled(
[email protected]760f743b2014-05-28 13:52:022005 extension, is_update, from_ephemeral, old_name);
[email protected]17f07822014-05-22 08:45:152006
[email protected]399583b2012-12-11 09:33:422007 // Unpacked extensions default to allowing file access, but if that has been
2008 // overridden, don't reset the value.
[email protected]1d5e58b2013-01-31 08:41:402009 if (Manifest::ShouldAlwaysAllowFileAccess(extension->location()) &&
[email protected]399583b2012-12-11 09:33:422010 !extension_prefs_->HasAllowFileAccessSetting(extension->id())) {
2011 extension_prefs_->SetAllowFileAccess(extension->id(), true);
2012 }
2013
[email protected]6f6101832012-11-27 22:10:482014 AddExtension(extension);
[email protected]612a1cb12012-10-17 13:18:032015
[email protected]bc44b5da2014-06-12 14:20:002016 // Notify observers that need to know when an installation is complete.
[email protected]38e872532014-07-16 23:27:512017 registry_->TriggerOnInstalled(extension, is_update);
[email protected]bc44b5da2014-06-12 14:20:002018
[email protected]9f4e4f082013-06-21 07:11:192019 // Check extensions that may have been delayed only because this shared module
2020 // was not available.
[email protected]374ceb6f2014-07-02 19:25:342021 if (SharedModuleInfo::IsSharedModule(extension))
[email protected]9f4e4f082013-06-21 07:11:192022 MaybeFinishDelayedInstallations();
[email protected]4a190632009-05-09 01:07:422023}
2024
[email protected]760f743b2014-05-28 13:52:022025void ExtensionService::PromoteEphemeralApp(
2026 const extensions::Extension* extension, bool is_from_sync) {
2027 DCHECK(GetInstalledExtension(extension->id()) &&
2028 extension_prefs_->IsEphemeralApp(extension->id()));
2029
[email protected]db378322014-07-21 23:19:342030 if (extension->RequiresSortOrdinal()) {
2031 extension_prefs_->app_sorting()->SetExtensionVisible(
2032 extension->id(), extension->ShouldDisplayInNewTabPage());
2033
2034 if (!is_from_sync) {
[email protected]760f743b2014-05-28 13:52:022035 // Reset the sort ordinals of the app to ensure it is added to the default
2036 // position, like newly installed apps would.
2037 extension_prefs_->app_sorting()->ClearOrdinals(extension->id());
[email protected]760f743b2014-05-28 13:52:022038 }
2039
[email protected]db378322014-07-21 23:19:342040 extension_prefs_->app_sorting()->EnsureValidOrdinals(
2041 extension->id(), syncer::StringOrdinal());
2042 }
2043
[email protected]760f743b2014-05-28 13:52:022044 // Remove the ephemeral flags from the preferences.
2045 extension_prefs_->OnEphemeralAppPromoted(extension->id());
2046
2047 // Fire install-related events to allow observers to handle the promotion
2048 // of the ephemeral app.
2049 extensions::InstalledExtensionInfo details(
2050 extension,
2051 true /* is update */,
2052 true /* from ephemeral */,
2053 extension->name() /* old name */);
2054 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:062055 extensions::NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED,
[email protected]760f743b2014-05-28 13:52:022056 content::Source<Profile>(profile_),
2057 content::Details<const extensions::InstalledExtensionInfo>(&details));
2058
2059 registry_->TriggerOnWillBeInstalled(
2060 extension,
2061 true /* is update */,
2062 true /* from ephemeral */,
2063 extension->name() /* old name */);
2064
2065 if (registry_->enabled_extensions().Contains(extension->id())) {
[email protected]8f959f522014-08-06 06:26:282066 // If the app is already enabled and loaded, fire the load events to allow
2067 // observers to handle the promotion of the ephemeral app.
[email protected]760f743b2014-05-28 13:52:022068 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:062069 extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
[email protected]760f743b2014-05-28 13:52:022070 content::Source<Profile>(profile_),
2071 content::Details<const Extension>(extension));
2072
2073 registry_->TriggerOnLoaded(extension);
[email protected]8f959f522014-08-06 06:26:282074 } else {
2075 // Cached ephemeral apps may be updated and disabled due to permissions
2076 // increase. The app can be enabled (as long as no other disable reasons
2077 // exist) as the install was user-acknowledged.
2078 int disable_mask = Extension::DISABLE_NONE;
2079 if (!is_from_sync)
2080 disable_mask |= Extension::DISABLE_PERMISSIONS_INCREASE;
2081
2082 int other_disable_reasons =
2083 extension_prefs_->GetDisableReasons(extension->id()) & ~disable_mask;
2084 if (!other_disable_reasons) {
2085 if (extension_prefs_->DidExtensionEscalatePermissions(extension->id()))
2086 GrantPermissionsAndEnableExtension(extension);
2087 else
2088 EnableExtension(extension->id());
2089 }
[email protected]760f743b2014-05-28 13:52:022090 }
2091
[email protected]38e872532014-07-16 23:27:512092 registry_->TriggerOnInstalled(extension, true);
[email protected]bc44b5da2014-06-12 14:20:002093
[email protected]760f743b2014-05-28 13:52:022094 if (!is_from_sync && extension_sync_service_)
2095 extension_sync_service_->SyncExtensionChangeIfNeeded(*extension);
2096}
2097
[email protected]76b65442012-11-17 14:11:482098const Extension* ExtensionService::GetPendingExtensionUpdate(
2099 const std::string& id) const {
[email protected]9f4e4f082013-06-21 07:11:192100 return delayed_installs_.GetByID(id);
[email protected]76b65442012-11-17 14:11:482101}
2102
mukai87a8d402014-09-15 20:15:092103void ExtensionService::RegisterContentSettings(
2104 HostContentSettingsMap* host_content_settings_map) {
rkaplowe65c2ff2015-02-14 16:29:542105 TRACE_EVENT0("browser,startup", "ExtensionService::RegisterContentSettings");
mukaia1aa3512014-10-22 20:14:112106 DCHECK_CURRENTLY_ON(BrowserThread::UI);
mukai87a8d402014-09-15 20:15:092107 host_content_settings_map->RegisterProvider(
2108 HostContentSettingsMap::INTERNAL_EXTENSION_PROVIDER,
2109 scoped_ptr<content_settings::ObservableProvider>(
reillyga3acbc12014-11-11 23:17:122110 new content_settings::InternalExtensionProvider(profile_)));
mukai87a8d402014-09-15 20:15:092111
2112 host_content_settings_map->RegisterProvider(
2113 HostContentSettingsMap::CUSTOM_EXTENSION_PROVIDER,
2114 scoped_ptr<content_settings::ObservableProvider>(
2115 new content_settings::CustomExtensionProvider(
2116 extensions::ContentSettingsService::Get(
2117 profile_)->content_settings_store(),
2118 profile_->GetOriginalProfile() != profile_)));
2119}
2120
xiyuane87def2e2015-06-04 22:50:222121void ExtensionService::TrackTerminatedExtension(
2122 const std::string& extension_id) {
2123 extensions_being_terminated_.erase(extension_id);
2124
2125 const Extension* extension = GetInstalledExtension(extension_id);
2126 if (!extension) {
2127 LOG(WARNING) << "Terminated extension is already removed.";
2128 return;
2129 }
2130
[email protected]bb1bc9b32013-12-21 03:09:142131 // No need to check for duplicates; inserting a duplicate is a no-op.
2132 registry_->AddTerminated(make_scoped_refptr(extension));
[email protected]b0af4792013-10-23 09:12:132133 UnloadExtension(extension->id(), UnloadedExtensionInfo::REASON_TERMINATE);
[email protected]bb7f40952011-01-13 00:21:202134}
2135
[email protected]e9d7496e2014-04-18 01:25:462136void ExtensionService::TerminateExtension(const std::string& extension_id) {
2137 const Extension* extension = GetInstalledExtension(extension_id);
xiyuane87def2e2015-06-04 22:50:222138 TrackTerminatedExtension(extension->id());
[email protected]e9d7496e2014-04-18 01:25:462139}
2140
[email protected]bb7f40952011-01-13 00:21:202141void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
[email protected]cb1f4ac2014-08-07 16:55:422142 std::string lowercase_id = base::StringToLowerASCII(id);
[email protected]bb1bc9b32013-12-21 03:09:142143 const Extension* extension =
2144 registry_->terminated_extensions().GetByID(lowercase_id);
2145 registry_->RemoveTerminated(lowercase_id);
[email protected]fb789532013-08-27 02:40:212146 if (extension) {
2147 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:062148 extensions::NOTIFICATION_EXTENSION_REMOVED,
[email protected]fb789532013-08-27 02:40:212149 content::Source<Profile>(profile_),
2150 content::Details<const Extension>(extension));
2151 }
[email protected]bb7f40952011-01-13 00:21:202152}
2153
[email protected]8001df22011-04-28 19:59:472154const Extension* ExtensionService::GetInstalledExtension(
2155 const std::string& id) const {
[email protected]599539802014-01-07 23:06:002156 return registry_->GetExtensionById(id, ExtensionRegistry::EVERYTHING);
[email protected]0dfe05c2011-02-23 23:03:362157}
2158
[email protected]9060d8b02012-01-13 02:14:302159bool ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:322160 const std::string& id,
[email protected]c5e4a2222014-01-03 16:06:132161 const Version* version,
[email protected]650b2d52013-02-10 03:41:452162 const base::FilePath& path,
[email protected]1d5e58b2013-01-31 08:41:402163 Manifest::Location location,
[email protected]47fc70c2011-12-06 07:29:512164 int creation_flags,
xiyuan4d82f7b62015-03-04 02:29:222165 bool mark_acknowledged,
2166 bool install_immediately) {
[email protected]ab22ba42011-01-14 16:36:382167 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]fdd28372014-08-21 02:27:262168 CHECK(crx_file::id_util::IdIsValid(id));
[email protected]79c833b52011-04-05 18:31:012169 if (extension_prefs_->IsExternalExtensionUninstalled(id))
[email protected]9060d8b02012-01-13 02:14:302170 return false;
[email protected]a8af9fdb2010-10-28 21:52:202171
[email protected]7577a5c52009-07-30 06:21:582172 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:492173 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:582174 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:022175 const Extension* existing = GetExtensionById(id, true);
[email protected]910f72ce2012-08-24 01:38:352176
[email protected]7577a5c52009-07-30 06:21:582177 if (existing) {
[email protected]910f72ce2012-08-24 01:38:352178 // The default apps will have the location set as INTERNAL. Since older
2179 // default apps are installed as EXTERNAL, we override them. However, if the
2180 // app is already installed as internal, then do the version check.
[email protected]31bb5ee62012-09-12 22:58:402181 // TODO(grv) : Remove after Q1-2013.
[email protected]910f72ce2012-08-24 01:38:352182 bool is_default_apps_migration =
[email protected]1d5e58b2013-01-31 08:41:402183 (location == Manifest::INTERNAL &&
2184 Manifest::IsExternalLocation(existing->location()));
[email protected]910f72ce2012-08-24 01:38:352185
2186 if (!is_default_apps_migration) {
2187 DCHECK(version);
2188
2189 switch (existing->version()->CompareTo(*version)) {
2190 case -1: // existing version is older, we should upgrade
2191 break;
2192 case 0: // existing version is same, do nothing
2193 return false;
2194 case 1: // existing version is newer, uh-oh
2195 LOG(WARNING) << "Found external version of extension " << id
2196 << "that is older than current version. Current version "
2197 << "is: " << existing->VersionString() << ". New "
2198 << "version is: " << version->GetString()
2199 << ". Keeping current version.";
2200 return false;
2201 }
[email protected]7577a5c52009-07-30 06:21:582202 }
2203 }
2204
[email protected]9060d8b02012-01-13 02:14:302205 // If the extension is already pending, don't start an install.
[email protected]e3987852012-05-04 10:06:302206 if (!pending_extension_manager()->AddFromExternalFile(
[email protected]464213a2013-10-15 01:06:482207 id, location, *version, creation_flags, mark_acknowledged)) {
[email protected]9060d8b02012-01-13 02:14:302208 return false;
[email protected]e3987852012-05-04 10:06:302209 }
[email protected]9c635f22010-12-02 09:36:362210
[email protected]14908b72011-04-20 06:54:362211 // no client (silent install)
[email protected]f8636f92013-08-09 21:02:372212 scoped_refptr<CrxInstaller> installer(CrxInstaller::CreateSilent(this));
[email protected]6dfbbf82010-03-12 23:09:162213 installer->set_install_source(location);
2214 installer->set_expected_id(id);
[email protected]cb0e50312011-05-09 15:03:072215 installer->set_expected_version(*version);
2216 installer->set_install_cause(extension_misc::INSTALL_CAUSE_EXTERNAL_FILE);
xiyuan4d82f7b62015-03-04 02:29:222217 installer->set_install_immediately(install_immediately);
[email protected]1bf73cc32011-10-26 22:38:312218 installer->set_creation_flags(creation_flags);
[email protected]88e8ec9152013-01-17 04:05:182219#if defined(OS_CHROMEOS)
2220 extensions::InstallLimiter::Get(profile_)->Add(installer, path);
2221#else
[email protected]6dfbbf82010-03-12 23:09:162222 installer->InstallCrx(path);
[email protected]88e8ec9152013-01-17 04:05:182223#endif
[email protected]47fc70c2011-12-06 07:29:512224
2225 // Depending on the source, a new external extension might not need a user
2226 // notification on installation. For such extensions, mark them acknowledged
2227 // now to suppress the notification.
2228 if (mark_acknowledged)
[email protected]374ceb6f2014-07-02 19:25:342229 external_install_manager_->AcknowledgeExternalExtension(id);
[email protected]9060d8b02012-01-13 02:14:302230
2231 return true;
[email protected]7577a5c52009-07-30 06:21:582232}
2233
[email protected]eaa7dd182010-12-14 11:09:002234void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]3a1dc572012-07-31 22:25:132235 extensions::ExtensionHost* host) {
[email protected]406027c02010-09-27 08:03:182236 OrphanedDevTools::iterator iter =
[email protected]2d2f6cfc2011-05-06 21:09:332237 orphaned_dev_tools_.find(host->extension_id());
[email protected]406027c02010-09-27 08:03:182238 if (iter == orphaned_dev_tools_.end())
2239 return;
2240
[email protected]b3f957e62014-08-08 10:09:022241 iter->second->ConnectWebContents(host->host_contents());
[email protected]406027c02010-09-27 08:03:182242 orphaned_dev_tools_.erase(iter);
2243}
2244
[email protected]432115822011-07-10 15:52:272245void ExtensionService::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:532246 const content::NotificationSource& source,
2247 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:272248 switch (type) {
[email protected]3c4abc82012-10-22 22:25:542249 case chrome::NOTIFICATION_APP_TERMINATING:
2250 // Shutdown has started. Don't start any more extension installs.
2251 // (We cannot use ExtensionService::Shutdown() for this because it
2252 // happens too late in browser teardown.)
2253 browser_terminating_ = true;
2254 break;
[email protected]adf5a102014-07-31 12:44:062255 case extensions::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]6c2381d2011-10-19 02:52:532256 if (profile_ !=
2257 content::Source<Profile>(source).ptr()->GetOriginalProfile()) {
[email protected]bc535ee52010-08-31 18:40:322258 break;
[email protected]6c2381d2011-10-19 02:52:532259 }
[email protected]a4ed6282009-12-14 20:51:162260
[email protected]3a1dc572012-07-31 22:25:132261 extensions::ExtensionHost* host =
2262 content::Details<extensions::ExtensionHost>(details).ptr();
[email protected]f128af42010-08-05 18:05:262263
[email protected]3964e572014-02-13 21:57:412264 // If the extension is already being terminated, there is nothing left to
2265 // do.
2266 if (!extensions_being_terminated_.insert(host->extension_id()).second)
2267 break;
2268
[email protected]fa2416f2011-05-03 08:41:202269 // Mark the extension as terminated and Unload it. We want it to
2270 // be in a consistent state: either fully working or not loaded
2271 // at all, but never half-crashed. We do it in a PostTask so
2272 // that other handlers of this notification will still have
2273 // access to the Extension and ExtensionHost.
[email protected]b3a25092013-05-28 22:08:162274 base::MessageLoop::current()->PostTask(
[email protected]14908b72011-04-20 06:54:362275 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362276 base::Bind(
[email protected]fa2416f2011-05-03 08:41:202277 &ExtensionService::TrackTerminatedExtension,
[email protected]d8c8f25f2011-11-02 18:18:012278 AsWeakPtr(),
xiyuane87def2e2015-06-04 22:50:222279 host->extension()->id()));
[email protected]31f77262009-12-02 20:48:532280 break;
2281 }
[email protected]432115822011-07-10 15:52:272282 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
[email protected]f3b1a082011-11-18 00:34:302283 content::RenderProcessHost* process =
2284 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192285 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302286 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192287 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2288 break;
2289
[email protected]fafdc842014-01-17 18:09:082290 extensions::ProcessMap* process_map =
2291 extensions::ProcessMap::Get(profile_);
2292 if (process_map->Contains(process->GetID())) {
[email protected]52b76592013-11-02 17:59:032293 // An extension process was terminated, this might have resulted in an
2294 // app or extension becoming idle.
2295 std::set<std::string> extension_ids =
[email protected]fafdc842014-01-17 18:09:082296 process_map->GetExtensionsInProcess(process->GetID());
elijahtaylor0def4432014-10-06 18:15:112297 // In addition to the extensions listed in the process map, one of those
2298 // extensions could be referencing a shared module which is waiting for
2299 // idle to update. Check all imports of these extensions, too.
2300 std::set<std::string> import_ids;
2301 for (std::set<std::string>::const_iterator it = extension_ids.begin();
2302 it != extension_ids.end();
2303 ++it) {
2304 const Extension* extension = GetExtensionById(*it, true);
2305 if (!extension)
2306 continue;
2307 const std::vector<SharedModuleInfo::ImportInfo>& imports =
2308 SharedModuleInfo::GetImports(extension);
2309 std::vector<SharedModuleInfo::ImportInfo>::const_iterator import_it;
2310 for (import_it = imports.begin(); import_it != imports.end();
2311 import_it++) {
2312 import_ids.insert((*import_it).extension_id);
2313 }
2314 }
2315 extension_ids.insert(import_ids.begin(), import_ids.end());
2316
[email protected]52b76592013-11-02 17:59:032317 for (std::set<std::string>::const_iterator it = extension_ids.begin();
2318 it != extension_ids.end(); ++it) {
2319 if (delayed_installs_.Contains(*it)) {
2320 base::MessageLoop::current()->PostDelayedTask(
2321 FROM_HERE,
2322 base::Bind(&ExtensionService::MaybeFinishDelayedInstallation,
2323 AsWeakPtr(), *it),
2324 base::TimeDelta::FromSeconds(kUpdateIdleDelay));
2325 }
2326 }
2327 }
2328
[email protected]fafdc842014-01-17 18:09:082329 process_map->RemoveAllFromProcess(process->GetID());
[email protected]6f371442011-11-09 06:45:462330 BrowserThread::PostTask(
[email protected]38427a12013-11-09 17:34:202331 BrowserThread::IO,
2332 FROM_HERE,
2333 base::Bind(&extensions::InfoMap::UnregisterAllExtensionsInProcess,
[email protected]31d8f5f22012-04-02 15:22:082334 system_->info_map(),
[email protected]f3b1a082011-11-18 00:34:302335 process->GetID()));
[email protected]da5683db2011-04-23 17:12:212336 break;
2337 }
[email protected]92dd8d92013-02-26 00:41:082338 case chrome::NOTIFICATION_UPGRADE_RECOMMENDED: {
[email protected]75bdcb872013-03-13 00:41:452339 // Notify observers that chrome update is available.
2340 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
2341 OnChromeUpdateAvailable());
[email protected]92dd8d92013-02-26 00:41:082342 break;
2343 }
[email protected]ebe07772014-05-22 04:16:062344 case chrome::NOTIFICATION_PROFILE_DESTRUCTION_STARTED: {
2345 OnProfileDestructionStarted();
2346 break;
2347 }
[email protected]aa96d3a2010-08-21 08:45:252348
[email protected]4814b512009-11-07 00:12:292349 default:
2350 NOTREACHED() << "Unexpected notification type.";
2351 }
2352}
2353
binjin47947f842014-11-18 12:10:242354int ExtensionService::GetDisableReasonsOnInstalled(const Extension* extension) {
2355 Extension::DisableReason disable_reason;
2356 // Extensions disabled by management policy should always be disabled, even
2357 // if it's force-installed.
2358 if (system_->management_policy()->MustRemainDisabled(
2359 extension, &disable_reason, nullptr)) {
2360 // A specified reason is required to disable the extension.
2361 DCHECK(disable_reason != Extension::DISABLE_NONE);
2362 return disable_reason;
2363 }
2364
[email protected]612a1cb12012-10-17 13:18:032365 // Extensions installed by policy can't be disabled. So even if a previous
2366 // installation disabled the extension, make sure it is now enabled.
binjin47947f842014-11-18 12:10:242367 if (system_->management_policy()->MustRemainEnabled(extension, nullptr))
2368 return Extension::DISABLE_NONE;
[email protected]612a1cb12012-10-17 13:18:032369
binjin47947f842014-11-18 12:10:242370 // An already disabled extension should inherit the disable reasons and
2371 // remain disabled.
2372 if (extension_prefs_->IsExtensionDisabled(extension->id())) {
2373 int disable_reasons = extension_prefs_->GetDisableReasons(extension->id());
2374 // If an extension was disabled without specified reason, presume it's
2375 // disabled by user.
2376 return disable_reasons == Extension::DISABLE_NONE
2377 ? Extension::DISABLE_USER_ACTION
2378 : disable_reasons;
2379 }
[email protected]612a1cb12012-10-17 13:18:032380
[email protected]41070e8d2012-10-24 01:34:362381 if (FeatureSwitch::prompt_for_external_extensions()->IsEnabled()) {
[email protected]612a1cb12012-10-17 13:18:032382 // External extensions are initially disabled. We prompt the user before
[email protected]2c495c42013-01-04 21:49:542383 // enabling them. Hosted apps are excepted because they are not dangerous
2384 // (they need to be launched by the user anyway).
[email protected]1d5e58b2013-01-31 08:41:402385 if (extension->GetType() != Manifest::TYPE_HOSTED_APP &&
2386 Manifest::IsExternalLocation(extension->location()) &&
[email protected]612a1cb12012-10-17 13:18:032387 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id())) {
binjin47947f842014-11-18 12:10:242388 return Extension::DISABLE_EXTERNAL_EXTENSION;
[email protected]612a1cb12012-10-17 13:18:032389 }
2390 }
[email protected]612a1cb12012-10-17 13:18:032391
binjin47947f842014-11-18 12:10:242392 return Extension::DISABLE_NONE;
[email protected]612a1cb12012-10-17 13:18:032393}
[email protected]0db124b02012-11-07 04:55:052394
mlerman6a37b6a42014-11-26 22:10:532395// Helper method to determine if an extension can be blocked.
2396bool ExtensionService::CanBlockExtension(const Extension* extension) const {
mlerman3690e5be2014-12-01 22:57:442397 DCHECK(extension);
mlerman6a37b6a42014-11-26 22:10:532398 return extension->location() != Manifest::COMPONENT &&
2399 extension->location() != Manifest::EXTERNAL_COMPONENT &&
2400 !system_->management_policy()->MustRemainEnabled(extension, NULL);
2401}
2402
[email protected]e7aa7b7e2012-11-27 04:51:222403bool ExtensionService::ShouldDelayExtensionUpdate(
2404 const std::string& extension_id,
[email protected]4a1d9c0d2014-06-13 12:50:112405 bool install_immediately) const {
[email protected]e7aa7b7e2012-11-27 04:51:222406 const char kOnUpdateAvailableEvent[] = "runtime.onUpdateAvailable";
2407
2408 // If delayed updates are globally disabled, or just for this extension,
2409 // don't delay.
[email protected]4a1d9c0d2014-06-13 12:50:112410 if (!install_updates_when_idle_ || install_immediately)
[email protected]e7aa7b7e2012-11-27 04:51:222411 return false;
2412
[email protected]695b5712012-12-06 23:55:282413 const Extension* old = GetInstalledExtension(extension_id);
[email protected]e7aa7b7e2012-11-27 04:51:222414 // If there is no old extension, this is not an update, so don't delay.
2415 if (!old)
2416 return false;
2417
[email protected]9367eabc2013-03-01 01:29:292418 if (extensions::BackgroundInfo::HasPersistentBackgroundPage(old)) {
[email protected]e7aa7b7e2012-11-27 04:51:222419 // Delay installation if the extension listens for the onUpdateAvailable
2420 // event.
juncaicf523332015-06-04 00:14:042421 return extensions::EventRouter::Get(profile_)
2422 ->ExtensionHasEventListener(extension_id, kOnUpdateAvailableEvent);
[email protected]e7aa7b7e2012-11-27 04:51:222423 } else {
2424 // Delay installation if the extension is not idle.
[email protected]1d5cf4142014-01-24 18:25:222425 return !extensions::util::IsExtensionIdle(extension_id, profile_);
[email protected]e7aa7b7e2012-11-27 04:51:222426 }
[email protected]0db124b02012-11-07 04:55:052427}
[email protected]fdd679b2012-11-15 20:49:392428
[email protected]e5f8fffe2014-04-02 00:30:442429void ExtensionService::OnGarbageCollectIsolatedStorageStart() {
2430 DCHECK(!installs_delayed_for_gc_);
2431 installs_delayed_for_gc_ = true;
[email protected]399583b2012-12-11 09:33:422432}
2433
2434void ExtensionService::OnGarbageCollectIsolatedStorageFinished() {
[email protected]e5f8fffe2014-04-02 00:30:442435 DCHECK(installs_delayed_for_gc_);
2436 installs_delayed_for_gc_ = false;
[email protected]9f4e4f082013-06-21 07:11:192437 MaybeFinishDelayedInstallations();
2438}
2439
2440void ExtensionService::MaybeFinishDelayedInstallations() {
2441 std::vector<std::string> to_be_installed;
[email protected]399583b2012-12-11 09:33:422442 for (ExtensionSet::const_iterator it = delayed_installs_.begin();
2443 it != delayed_installs_.end();
2444 ++it) {
[email protected]9f4e4f082013-06-21 07:11:192445 to_be_installed.push_back((*it)->id());
[email protected]399583b2012-12-11 09:33:422446 }
[email protected]9f4e4f082013-06-21 07:11:192447 for (std::vector<std::string>::const_iterator it = to_be_installed.begin();
2448 it != to_be_installed.end();
[email protected]399583b2012-12-11 09:33:422449 ++it) {
[email protected]9f4e4f082013-06-21 07:11:192450 MaybeFinishDelayedInstallation(*it);
[email protected]399583b2012-12-11 09:33:422451 }
[email protected]399583b2012-12-11 09:33:422452}
2453
[email protected]fdd679b2012-11-15 20:49:392454void ExtensionService::OnBlacklistUpdated() {
[email protected]2d19eb6e2014-01-27 17:30:002455 blacklist_->GetBlacklistedIDs(
[email protected]f47f7172014-03-19 19:27:102456 registry_->GenerateInstalledExtensionsSet()->GetIDs(),
[email protected]3f2a2fa2013-09-24 02:55:252457 base::Bind(&ExtensionService::ManageBlacklist, AsWeakPtr()));
[email protected]695b5712012-12-06 23:55:282458}
2459
[email protected]2d19eb6e2014-01-27 17:30:002460void ExtensionService::ManageBlacklist(
2461 const extensions::Blacklist::BlacklistStateMap& state_map) {
[email protected]54ee8192014-03-29 17:37:242462 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]695b5712012-12-06 23:55:282463
mlerman6a37b6a42014-11-26 22:10:532464 std::set<std::string> blacklisted;
[email protected]2d19eb6e2014-01-27 17:30:002465 ExtensionIdSet greylist;
2466 ExtensionIdSet unchanged;
2467 for (extensions::Blacklist::BlacklistStateMap::const_iterator it =
2468 state_map.begin();
2469 it != state_map.end();
2470 ++it) {
2471 switch (it->second) {
2472 case extensions::NOT_BLACKLISTED:
2473 break;
[email protected]695b5712012-12-06 23:55:282474
[email protected]2d19eb6e2014-01-27 17:30:002475 case extensions::BLACKLISTED_MALWARE:
mlerman6a37b6a42014-11-26 22:10:532476 blacklisted.insert(it->first);
[email protected]2d19eb6e2014-01-27 17:30:002477 break;
2478
2479 case extensions::BLACKLISTED_SECURITY_VULNERABILITY:
2480 case extensions::BLACKLISTED_CWS_POLICY_VIOLATION:
2481 case extensions::BLACKLISTED_POTENTIALLY_UNWANTED:
2482 greylist.insert(it->first);
2483 break;
2484
2485 case extensions::BLACKLISTED_UNKNOWN:
2486 unchanged.insert(it->first);
2487 break;
2488 }
2489 }
2490
mlerman6a37b6a42014-11-26 22:10:532491 UpdateBlacklistedExtensions(blacklisted, unchanged);
[email protected]2d19eb6e2014-01-27 17:30:002492 UpdateGreylistedExtensions(greylist, unchanged, state_map);
2493
[email protected]373daf972014-04-10 01:50:442494 error_controller_->ShowErrorIfNeeded();
[email protected]2d19eb6e2014-01-27 17:30:002495}
2496
2497namespace {
2498void Partition(const ExtensionIdSet& before,
2499 const ExtensionIdSet& after,
2500 const ExtensionIdSet& unchanged,
2501 ExtensionIdSet* no_longer,
2502 ExtensionIdSet* not_yet) {
2503 *not_yet = base::STLSetDifference<ExtensionIdSet>(after, before);
2504 *no_longer = base::STLSetDifference<ExtensionIdSet>(before, after);
2505 *no_longer = base::STLSetDifference<ExtensionIdSet>(*no_longer, unchanged);
2506}
2507} // namespace
2508
mlerman6a37b6a42014-11-26 22:10:532509void ExtensionService::UpdateBlacklistedExtensions(
2510 const ExtensionIdSet& blacklisted,
[email protected]2d19eb6e2014-01-27 17:30:002511 const ExtensionIdSet& unchanged) {
2512 ExtensionIdSet not_yet_blocked, no_longer_blocked;
mlerman6a37b6a42014-11-26 22:10:532513 Partition(registry_->blacklisted_extensions().GetIDs(), blacklisted,
2514 unchanged, &no_longer_blocked, &not_yet_blocked);
[email protected]2d19eb6e2014-01-27 17:30:002515
2516 for (ExtensionIdSet::iterator it = no_longer_blocked.begin();
2517 it != no_longer_blocked.end(); ++it) {
[email protected]695b5712012-12-06 23:55:282518 scoped_refptr<const Extension> extension =
[email protected]bb1bc9b32013-12-21 03:09:142519 registry_->blacklisted_extensions().GetByID(*it);
[email protected]3f2a2fa2013-09-24 02:55:252520 if (!extension.get()) {
mlerman6a37b6a42014-11-26 22:10:532521 NOTREACHED() << "Extension " << *it << " no longer blacklisted, "
2522 << "but it was never blacklisted.";
[email protected]695b5712012-12-06 23:55:282523 continue;
[email protected]3f2a2fa2013-09-24 02:55:252524 }
[email protected]bb1bc9b32013-12-21 03:09:142525 registry_->RemoveBlacklisted(*it);
[email protected]3f2a2fa2013-09-24 02:55:252526 extension_prefs_->SetExtensionBlacklisted(extension->id(), false);
[email protected]dc24976f2013-06-02 21:15:092527 AddExtension(extension.get());
[email protected]ac875372013-02-28 04:36:092528 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.UnblacklistInstalled",
[email protected]dc24976f2013-06-02 21:15:092529 extension->location(),
2530 Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:282531 }
2532
[email protected]2d19eb6e2014-01-27 17:30:002533 for (ExtensionIdSet::iterator it = not_yet_blocked.begin();
2534 it != not_yet_blocked.end(); ++it) {
[email protected]695b5712012-12-06 23:55:282535 scoped_refptr<const Extension> extension = GetInstalledExtension(*it);
[email protected]3f2a2fa2013-09-24 02:55:252536 if (!extension.get()) {
2537 NOTREACHED() << "Extension " << *it << " needs to be "
2538 << "blacklisted, but it's not installed.";
[email protected]695b5712012-12-06 23:55:282539 continue;
[email protected]3f2a2fa2013-09-24 02:55:252540 }
[email protected]bb1bc9b32013-12-21 03:09:142541 registry_->AddBlacklisted(extension);
[email protected]2d19eb6e2014-01-27 17:30:002542 extension_prefs_->SetExtensionBlacklistState(
2543 extension->id(), extensions::BLACKLISTED_MALWARE);
[email protected]b0af4792013-10-23 09:12:132544 UnloadExtension(*it, UnloadedExtensionInfo::REASON_BLACKLIST);
[email protected]ac875372013-02-28 04:36:092545 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.BlacklistInstalled",
2546 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:282547 }
[email protected]2d19eb6e2014-01-27 17:30:002548}
[email protected]695b5712012-12-06 23:55:282549
[email protected]2d19eb6e2014-01-27 17:30:002550// TODO(oleg): UMA logging
2551void ExtensionService::UpdateGreylistedExtensions(
2552 const ExtensionIdSet& greylist,
2553 const ExtensionIdSet& unchanged,
2554 const extensions::Blacklist::BlacklistStateMap& state_map) {
2555 ExtensionIdSet not_yet_greylisted, no_longer_greylisted;
2556 Partition(greylist_.GetIDs(),
2557 greylist, unchanged,
2558 &no_longer_greylisted, &not_yet_greylisted);
2559
2560 for (ExtensionIdSet::iterator it = no_longer_greylisted.begin();
2561 it != no_longer_greylisted.end(); ++it) {
2562 scoped_refptr<const Extension> extension = greylist_.GetByID(*it);
2563 if (!extension.get()) {
2564 NOTREACHED() << "Extension " << *it << " no longer greylisted, "
2565 << "but it was not marked as greylisted.";
2566 continue;
2567 }
2568
2569 greylist_.Remove(*it);
2570 extension_prefs_->SetExtensionBlacklistState(extension->id(),
2571 extensions::NOT_BLACKLISTED);
2572 if (extension_prefs_->GetDisableReasons(extension->id()) &
2573 extensions::Extension::DISABLE_GREYLIST)
2574 EnableExtension(*it);
2575 }
2576
2577 for (ExtensionIdSet::iterator it = not_yet_greylisted.begin();
2578 it != not_yet_greylisted.end(); ++it) {
2579 scoped_refptr<const Extension> extension = GetInstalledExtension(*it);
2580 if (!extension.get()) {
2581 NOTREACHED() << "Extension " << *it << " needs to be "
2582 << "disabled, but it's not installed.";
2583 continue;
2584 }
2585 greylist_.Insert(extension);
2586 extension_prefs_->SetExtensionBlacklistState(extension->id(),
2587 state_map.find(*it)->second);
2588 if (registry_->enabled_extensions().Contains(extension->id()))
2589 DisableExtension(*it, extensions::Extension::DISABLE_GREYLIST);
2590 }
[email protected]fdd679b2012-11-15 20:49:392591}
[email protected]75bdcb872013-03-13 00:41:452592
2593void ExtensionService::AddUpdateObserver(extensions::UpdateObserver* observer) {
2594 update_observers_.AddObserver(observer);
2595}
2596
2597void ExtensionService::RemoveUpdateObserver(
2598 extensions::UpdateObserver* observer) {
2599 update_observers_.RemoveObserver(observer);
2600}
[email protected]bb1bc9b32013-12-21 03:09:142601
2602// Used only by test code.
2603void ExtensionService::UnloadAllExtensionsInternal() {
2604 profile_->GetExtensionSpecialStoragePolicy()->RevokeRightsForAllExtensions();
2605
2606 registry_->ClearAll();
[email protected]45f5b7d2014-01-22 23:47:132607 system_->runtime_data()->ClearAll();
[email protected]bb1bc9b32013-12-21 03:09:142608
2609 // TODO(erikkay) should there be a notification for this? We can't use
2610 // EXTENSION_UNLOADED since that implies that the extension has been disabled
2611 // or uninstalled.
2612}
[email protected]ebe07772014-05-22 04:16:062613
2614void ExtensionService::OnProfileDestructionStarted() {
2615 ExtensionIdSet ids_to_unload = registry_->enabled_extensions().GetIDs();
2616 for (ExtensionIdSet::iterator it = ids_to_unload.begin();
2617 it != ids_to_unload.end();
2618 ++it) {
2619 UnloadExtension(*it, UnloadedExtensionInfo::REASON_PROFILE_SHUTDOWN);
2620 }
2621}