blob: 9f425f57445b17dc110c3530babf849ffaea4c11 [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
avia2f4804a2015-12-24 23:11:137#include <stddef.h>
8
[email protected]654512b2010-09-01 02:09:429#include <algorithm>
[email protected]695b5712012-12-06 23:55:2810#include <iterator>
[email protected]97d2f1d2011-01-15 00:41:0811#include <set>
[email protected]654512b2010-09-01 02:09:4212
[email protected]62433d32011-10-12 22:33:1213#include "base/command_line.h"
skyostilf221b7de2015-06-11 20:36:3214#include "base/location.h"
rkaplowfca0f4dc52015-03-14 21:40:5715#include "base/metrics/histogram_macros.h"
skyostilf221b7de2015-06-11 20:36:3216#include "base/single_thread_task_runner.h"
[email protected]3ea1b182013-02-08 22:38:4117#include "base/strings/string_number_conversions.h"
[email protected]00e7bef2013-06-10 20:35:1718#include "base/strings/stringprintf.h"
[email protected]112158af2013-06-07 23:46:1819#include "base/strings/utf_string_conversions.h"
[email protected]7f8f24f2012-11-15 19:40:1420#include "base/threading/sequenced_worker_pool.h"
[email protected]34b99632011-01-01 01:01:0621#include "base/threading/thread_restrictions.h"
gabb15e19072016-05-11 20:45:4122#include "base/threading/thread_task_runner_handle.h"
[email protected]41a17c52013-06-28 00:27:5323#include "base/time/time.h"
rkaplowe65c2ff2015-02-14 16:29:5424#include "base/trace_event/trace_event.h"
avia2f4804a2015-12-24 23:11:1325#include "build/build_config.h"
[email protected]15730c42009-09-03 00:03:2026#include "chrome/browser/browser_process.h"
[email protected]9ea0cd32013-07-12 01:50:3627#include "chrome/browser/chrome_notification_types.h"
mukai87a8d402014-09-15 20:15:0928#include "chrome/browser/content_settings/content_settings_internal_extension_provider.h"
sunil.ratnu21b896c2014-11-24 10:09:2429#include "chrome/browser/extensions/api/content_settings/content_settings_custom_extension_provider.h"
mukai87a8d402014-09-15 20:15:0930#include "chrome/browser/extensions/api/content_settings/content_settings_service.h"
ryanackley48bedbd2015-01-27 23:12:1431#include "chrome/browser/extensions/app_data_migrator.h"
[email protected]d8c8f25f2011-11-02 18:18:0132#include "chrome/browser/extensions/component_loader.h"
[email protected]13e062e2014-08-09 10:21:5533#include "chrome/browser/extensions/crx_installer.h"
[email protected]d9ede582012-08-14 19:21:3834#include "chrome/browser/extensions/data_deleter.h"
rdevlin.cronin2813c1b2014-08-26 23:51:0835#include "chrome/browser/extensions/extension_action_storage_manager.h"
[email protected]5a145e82014-05-29 22:19:0736#include "chrome/browser/extensions/extension_assets_manager.h"
[email protected]62f051c2012-03-29 17:04:4437#include "chrome/browser/extensions/extension_disabled_ui.h"
[email protected]373daf972014-04-10 01:50:4438#include "chrome/browser/extensions/extension_error_controller.h"
[email protected]19eb80152011-02-26 00:28:4339#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]f8aefb132013-10-30 09:29:5240#include "chrome/browser/extensions/extension_sync_service.h"
[email protected]617342a42013-12-18 23:34:0341#include "chrome/browser/extensions/extension_util.h"
[email protected]2894a512014-06-26 19:03:5642#include "chrome/browser/extensions/external_install_manager.h"
[email protected]5df038b2012-07-16 19:03:2743#include "chrome/browser/extensions/external_provider_impl.h"
[email protected]ffd2f79e2013-11-14 00:11:4644#include "chrome/browser/extensions/install_verifier.h"
[email protected]d8c8f25f2011-11-02 18:18:0145#include "chrome/browser/extensions/installed_loader.h"
[email protected]f3d3b382014-03-14 21:19:2846#include "chrome/browser/extensions/pending_extension_manager.h"
[email protected]13e062e2014-08-09 10:21:5547#include "chrome/browser/extensions/permissions_updater.h"
[email protected]90878c52014-04-04 18:21:0248#include "chrome/browser/extensions/shared_module_service.h"
[email protected]13e062e2014-08-09 10:21:5549#include "chrome/browser/extensions/unpacked_installer.h"
rockotdb5192992014-09-16 21:27:0250#include "chrome/browser/extensions/updater/chrome_extension_downloader_factory.h"
[email protected]13e062e2014-08-09 10:21:5551#include "chrome/browser/extensions/updater/extension_updater.h"
rockotdb5192992014-09-16 21:27:0252#include "chrome/browser/google/google_brand.h"
[email protected]8ecad5e2010-12-02 21:18:3353#include "chrome/browser/profiles/profile.h"
dbeam8d57a012015-05-21 07:12:2054#include "chrome/browser/search/thumbnail_source.h"
[email protected]b5a507b22013-11-08 20:41:5755#include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
[email protected]c8d407e2011-04-28 21:27:1756#include "chrome/browser/ui/webui/favicon_source.h"
[email protected]b07e606e2012-09-15 20:16:1557#include "chrome/browser/ui/webui/theme_source.h"
[email protected]e2eb43112009-05-29 21:19:5458#include "chrome/common/chrome_switches.h"
[email protected]f6431be82013-09-07 02:53:4559#include "chrome/common/crash_keys.h"
[email protected]9f4e4f082013-06-21 07:11:1960#include "chrome/common/extensions/extension_constants.h"
[email protected]a57209872009-05-04 22:53:1461#include "chrome/common/url_constants.h"
mukai8eaec822014-10-25 17:53:1662#include "components/content_settings/core/browser/host_content_settings_map.h"
[email protected]fdd28372014-08-21 02:27:2663#include "components/crx_file/id_util.h"
[email protected]dccba4f82014-05-29 00:52:5664#include "content/public/browser/devtools_agent_host.h"
[email protected]05aad2da2011-10-28 10:12:3765#include "content/public/browser/notification_service.h"
[email protected]f3b1a082011-11-18 00:34:3066#include "content/public/browser/render_process_host.h"
[email protected]399583b2012-12-11 09:33:4267#include "content/public/browser/storage_partition.h"
treib926ee2d2015-08-06 10:55:4268#include "extensions/browser/app_sorting.h"
[email protected]34423532013-11-21 18:13:1069#include "extensions/browser/event_router.h"
[email protected]22401dc2014-03-21 01:38:5770#include "extensions/browser/extension_host.h"
[email protected]bb1bc9b32013-12-21 03:09:1471#include "extensions/browser/extension_registry.h"
[email protected]59b0e602014-01-30 00:41:2472#include "extensions/browser/extension_system.h"
rockotffa65b12014-10-14 17:19:0673#include "extensions/browser/extensions_browser_client.h"
lazyboye8634172016-01-28 00:10:4874#include "extensions/browser/external_install_info.h"
[email protected]4a1d9c0d2014-06-13 12:50:1175#include "extensions/browser/install_flag.h"
rdevlin.cronin5e510e802016-07-26 15:09:2076#include "extensions/browser/renderer_startup_helper.h"
[email protected]45f5b7d2014-01-22 23:47:1377#include "extensions/browser/runtime_data.h"
[email protected]e43c61f2014-07-20 21:46:3478#include "extensions/browser/uninstall_reason.h"
[email protected]a9aa62b312013-11-29 05:35:0679#include "extensions/browser/update_observer.h"
rockotffa65b12014-10-14 17:19:0680#include "extensions/browser/updater/extension_cache.h"
rockot05f40a922014-10-16 19:40:2581#include "extensions/browser/updater/extension_downloader.h"
[email protected]fb820c02014-03-13 15:07:0882#include "extensions/common/extension_messages.h"
rockot90659852014-09-18 19:31:5283#include "extensions/common/extension_urls.h"
[email protected]5ef835a2013-11-08 20:42:5784#include "extensions/common/feature_switch.h"
rdevlin.cronin41227532016-07-13 21:24:3485#include "extensions/common/features/feature_channel.h"
[email protected]85df9d12014-04-15 17:02:1486#include "extensions/common/file_util.h"
[email protected]0c3c9732013-09-16 08:53:4187#include "extensions/common/manifest_constants.h"
[email protected]558878cc82013-11-09 01:25:5188#include "extensions/common/manifest_handlers/background_info.h"
rockotd5546142014-10-15 00:29:0889#include "extensions/common/manifest_url_handlers.h"
[email protected]dccba4f82014-05-29 00:52:5690#include "extensions/common/one_shot_event.h"
treib2e0517f92015-05-07 23:15:3691#include "extensions/common/permissions/api_permission.h"
[email protected]c41003472013-10-19 15:37:2592#include "extensions/common/permissions/permission_message_provider.h"
[email protected]e4452d32013-11-15 23:07:4193#include "extensions/common/permissions/permissions_data.h"
[email protected]79a60642012-10-20 21:03:1894
treib8ecc1eb52015-03-04 18:29:0695#if defined(ENABLE_SUPERVISED_USERS)
96#include "chrome/browser/supervised_user/supervised_user_service.h"
97#include "chrome/browser/supervised_user/supervised_user_service_factory.h"
98#endif
99
[email protected]eed367e2011-04-12 03:43:31100#if defined(OS_CHROMEOS)
[email protected]88e8ec9152013-01-17 04:05:18101#include "chrome/browser/chromeos/extensions/install_limiter.h"
pilgrime92c5fcd2014-09-10 23:31:23102#include "storage/browser/fileapi/file_system_backend.h"
103#include "storage/browser/fileapi/file_system_context.h"
[email protected]eed367e2011-04-12 03:43:31104#endif
105
[email protected]55eb70e762012-02-20 17:38:39106using content::BrowserContext;
[email protected]631bb742011-11-02 11:29:39107using content::BrowserThread;
[email protected]98f66112012-12-25 12:59:36108using content::DevToolsAgentHost;
treib2e0517f92015-05-07 23:15:36109using extensions::APIPermission;
deepak.m14ba69e62015-11-17 05:42:12110using extensions::AppSorting;
[email protected]bf3d9df2012-07-24 23:20:27111using extensions::CrxInstaller;
[email protected]1c321ee52012-05-21 03:02:34112using extensions::Extension;
113using extensions::ExtensionIdSet;
114using extensions::ExtensionInfo;
[email protected]599539802014-01-07 23:06:00115using extensions::ExtensionRegistry;
[email protected]289c44b2013-12-17 03:26:57116using extensions::ExtensionSet;
lazyboye8634172016-01-28 00:10:48117using extensions::ExternalInstallInfoFile;
118using extensions::ExternalInstallInfoUpdateUrl;
119using extensions::ExternalProviderInterface;
[email protected]215a7be2012-10-22 19:53:42120using extensions::FeatureSwitch;
[email protected]ffd2f79e2013-11-14 00:11:46121using extensions::InstallVerifier;
[email protected]0d54b682013-11-05 14:15:36122using extensions::ManagementPolicy;
[email protected]1d5e58b2013-01-31 08:41:40123using extensions::Manifest;
treib2e0517f92015-05-07 23:15:36124using extensions::PermissionID;
125using extensions::PermissionIDSet;
[email protected]c2e66e12012-06-27 06:27:06126using extensions::PermissionSet;
[email protected]9f4e4f082013-06-21 07:11:19127using extensions::SharedModuleInfo;
[email protected]90878c52014-04-04 18:21:02128using extensions::SharedModuleService;
[email protected]e410b5f2012-12-14 14:02:24129using extensions::UnloadedExtensionInfo;
[email protected]5ef47ec2010-01-28 05:58:05130
[email protected]b6ab96d2009-08-20 18:58:19131namespace {
132
[email protected]0db124b02012-11-07 04:55:05133// Wait this many seconds after an extensions becomes idle before updating it.
[email protected]ddc6a122014-06-27 04:52:32134const int kUpdateIdleDelay = 5;
[email protected]0db124b02012-11-07 04:55:05135
[email protected]c6d474f82009-12-16 21:11:06136} // namespace
[email protected]b6ab96d2009-08-20 18:58:19137
[email protected]eaa7dd182010-12-14 11:09:00138// ExtensionService.
[email protected]6014d672008-12-05 00:38:25139
[email protected]8e4560b62011-01-14 10:09:14140void ExtensionService::CheckExternalUninstall(const std::string& id) {
[email protected]a29a517a2011-01-21 21:11:12141 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20142
[email protected]7a4c6852010-09-16 03:44:22143 // Check if the providers know about this extension.
achuith2f5578b2016-02-26 21:57:13144 for (const auto& provider : external_extension_providers_) {
145 DCHECK(provider->IsReady());
146 if (provider->HasExtension(id))
[email protected]0a60a2e2010-10-25 16:15:21147 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22148 }
149
[email protected]0f48fca2011-05-19 18:46:35150 // We get the list of external extensions to check from preferences.
151 // It is possible that an extension has preferences but is not loaded.
152 // For example, an extension that requires experimental permissions
153 // will not be loaded if the experimental command line flag is not used.
154 // In this case, do not uninstall.
[email protected]31bb5ee62012-09-12 22:58:40155 if (!GetInstalledExtension(id)) {
[email protected]0f48fca2011-05-19 18:46:35156 // We can't call UninstallExtension with an unloaded/invalid
157 // extension ID.
158 LOG(WARNING) << "Attempted uninstallation of unloaded/invalid extension "
159 << "with id: " << id;
160 return;
161 }
[email protected]42d58f62014-07-31 01:32:45162 UninstallExtension(id,
163 extensions::UNINSTALL_REASON_ORPHANED_EXTERNAL_EXTENSION,
achuith2f5578b2016-02-26 21:57:13164 base::Bind(&base::DoNothing), nullptr);
[email protected]7a4c6852010-09-16 03:44:22165}
166
[email protected]7f8f24f2012-11-15 19:40:14167void ExtensionService::SetFileTaskRunnerForTesting(
dcheng605ef8d2014-08-28 18:29:44168 const scoped_refptr<base::SequencedTaskRunner>& task_runner) {
[email protected]7f8f24f2012-11-15 19:40:14169 file_task_runner_ = task_runner;
170}
171
[email protected]8e4560b62011-01-14 10:09:14172void ExtensionService::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22173 external_extension_providers_.clear();
174}
175
[email protected]8e4560b62011-01-14 10:09:14176void ExtensionService::AddProviderForTesting(
lazyboye8634172016-01-28 00:10:48177 ExternalProviderInterface* test_provider) {
[email protected]a29a517a2011-01-21 21:11:12178 CHECK(test_provider);
[email protected]0a60a2e2010-10-25 16:15:21179 external_extension_providers_.push_back(
lazyboye8634172016-01-28 00:10:48180 linked_ptr<ExternalProviderInterface>(test_provider));
[email protected]7a4c6852010-09-16 03:44:22181}
182
[email protected]8f959f522014-08-06 06:26:28183void ExtensionService::BlacklistExtensionForTest(
184 const std::string& extension_id) {
mlerman6a37b6a42014-11-26 22:10:53185 ExtensionIdSet blacklisted;
[email protected]8f959f522014-08-06 06:26:28186 ExtensionIdSet unchanged;
mlerman6a37b6a42014-11-26 22:10:53187 blacklisted.insert(extension_id);
188 UpdateBlacklistedExtensions(blacklisted, unchanged);
[email protected]8f959f522014-08-06 06:26:28189}
190
[email protected]9060d8b02012-01-13 02:14:30191bool ExtensionService::OnExternalExtensionUpdateUrlFound(
lazyboye8634172016-01-28 00:10:48192 const ExternalInstallInfoUpdateUrl& info,
193 bool is_initial_load) {
[email protected]ab22ba42011-01-14 16:36:38194 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
lazyboye8634172016-01-28 00:10:48195 CHECK(crx_file::id_util::IdIsValid(info.extension_id));
[email protected]a8af9fdb2010-10-28 21:52:20196
lazyboye8634172016-01-28 00:10:48197 if (Manifest::IsExternalLocation(info.download_location)) {
[email protected]044e86992014-01-24 22:59:11198 // All extensions that are not user specific can be cached.
lazyboye8634172016-01-28 00:10:48199 extensions::ExtensionsBrowserClient::Get()
200 ->GetExtensionCache()
201 ->AllowCaching(info.extension_id);
[email protected]044e86992014-01-24 22:59:11202 }
203
lazyboye8634172016-01-28 00:10:48204 const Extension* extension = GetExtensionById(info.extension_id, true);
[email protected]8a87a5332011-08-11 17:54:59205 if (extension) {
206 // Already installed. Skip this install if the current location has
lazyboye8634172016-01-28 00:10:48207 // higher priority than |info.download_location|.
[email protected]1d5e58b2013-01-31 08:41:40208 Manifest::Location current = extension->location();
lazyboye8634172016-01-28 00:10:48209 if (current ==
210 Manifest::GetHigherPriorityLocation(current, info.download_location)) {
[email protected]9060d8b02012-01-13 02:14:30211 return false;
lazyboye8634172016-01-28 00:10:48212 }
[email protected]8a87a5332011-08-11 17:54:59213 // Otherwise, overwrite the current installation.
[email protected]7a4c6852010-09-16 03:44:22214 }
[email protected]9060d8b02012-01-13 02:14:30215
lazyboye8634172016-01-28 00:10:48216 // Add |info.extension_id| to the set of pending extensions. If it can not
217 // be added, then there is already a pending record from a higher-priority
218 // install source. In this case, signal that this extension will not be
[email protected]9060d8b02012-01-13 02:14:30219 // installed by returning false.
220 if (!pending_extension_manager()->AddFromExternalUpdateUrl(
lazyboye8634172016-01-28 00:10:48221 info.extension_id, info.install_parameter, *info.update_url,
222 info.download_location, info.creation_flags,
223 info.mark_acknowledged)) {
[email protected]9060d8b02012-01-13 02:14:30224 return false;
[email protected]31bb5ee62012-09-12 22:58:40225 }
[email protected]9060d8b02012-01-13 02:14:30226
lazyboye8634172016-01-28 00:10:48227 if (is_initial_load)
228 update_once_all_providers_are_ready_ = true;
[email protected]9060d8b02012-01-13 02:14:30229 return true;
[email protected]7a4c6852010-09-16 03:44:22230}
231
lazyboye8634172016-01-28 00:10:48232void ExtensionService::OnExternalProviderUpdateComplete(
233 const ExternalProviderInterface* provider,
234 const ScopedVector<ExternalInstallInfoUpdateUrl>& update_url_extensions,
235 const ScopedVector<ExternalInstallInfoFile>& file_extensions,
236 const std::set<std::string>& removed_extensions) {
237 // Update pending_extension_manager() with the new extensions first.
vmpstrae72b082016-07-25 21:55:47238 for (auto* extension : update_url_extensions)
lazyboye8634172016-01-28 00:10:48239 OnExternalExtensionUpdateUrlFound(*extension, false);
vmpstrae72b082016-07-25 21:55:47240 for (auto* extension : file_extensions)
lazyboye8634172016-01-28 00:10:48241 OnExternalExtensionFileFound(*extension);
242
243#if DCHECK_IS_ON()
244 for (const std::string& id : removed_extensions) {
vmpstrae72b082016-07-25 21:55:47245 for (auto* extension : update_url_extensions)
lazyboye8634172016-01-28 00:10:48246 DCHECK_NE(id, extension->extension_id);
vmpstrae72b082016-07-25 21:55:47247 for (auto* extension : file_extensions)
lazyboye8634172016-01-28 00:10:48248 DCHECK_NE(id, extension->extension_id);
249 }
250#endif
251
252 // Then uninstall before running |updater_|.
253 for (const std::string& id : removed_extensions)
254 CheckExternalUninstall(id);
255
256 if (!update_url_extensions.empty() && updater_) {
257 // Empty params will cause pending extensions to be updated.
258 extensions::ExtensionUpdater::CheckParams empty_params;
259 updater_->CheckNow(empty_params);
260 }
261
262 error_controller_->ShowErrorIfNeeded();
263 external_install_manager_->UpdateExternalExtensionAlert();
264}
265
[email protected]6aeac8342010-10-01 20:21:18266// static
[email protected]cc2f55c2014-07-08 02:19:04267// This function is used to uninstall an extension via sync. The LOG statements
268// within this function are used to inform the user if the uninstall cannot be
269// done.
[email protected]eaa7dd182010-12-14 11:09:00270bool ExtensionService::UninstallExtensionHelper(
271 ExtensionService* extensions_service,
[email protected]cc2f55c2014-07-08 02:19:04272 const std::string& extension_id,
[email protected]e43c61f2014-07-20 21:46:34273 extensions::UninstallReason reason) {
[email protected]d6ebc9792011-04-07 18:18:33274 // We can't call UninstallExtension with an invalid extension ID.
[email protected]31bb5ee62012-09-12 22:58:40275 if (!extensions_service->GetInstalledExtension(extension_id)) {
[email protected]6aeac8342010-10-01 20:21:18276 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]95da88c42011-03-31 10:07:33277 << "id: " << extension_id;
278 return false;
[email protected]6aeac8342010-10-01 20:21:18279 }
280
[email protected]d6ebc9792011-04-07 18:18:33281 // The following call to UninstallExtension will not allow an uninstall of a
282 // policy-controlled extension.
[email protected]439f1e32013-12-09 20:09:09283 base::string16 error;
[email protected]42d58f62014-07-31 01:32:45284 if (!extensions_service->UninstallExtension(
285 extension_id, reason, base::Bind(&base::DoNothing), &error)) {
[email protected]d6ebc9792011-04-07 18:18:33286 LOG(WARNING) << "Cannot uninstall extension with id " << extension_id
287 << ": " << error;
288 return false;
289 }
[email protected]95da88c42011-03-31 10:07:33290
[email protected]6aeac8342010-10-01 20:21:18291 return true;
292}
293
[email protected]eaa7dd182010-12-14 11:09:00294ExtensionService::ExtensionService(Profile* profile,
avi3ef9ec9e2014-12-22 22:50:17295 const base::CommandLine* command_line,
[email protected]650b2d52013-02-10 03:41:45296 const base::FilePath& install_directory,
[email protected]45759612012-07-10 17:21:23297 extensions::ExtensionPrefs* extension_prefs,
[email protected]fdd679b2012-11-15 20:49:39298 extensions::Blacklist* blacklist,
[email protected]0436b102011-04-15 18:30:03299 bool autoupdate_enabled,
[email protected]4a10006a2013-05-17 23:18:35300 bool extensions_enabled,
301 extensions::OneShotEvent* ready)
[email protected]fdd679b2012-11-15 20:49:39302 : extensions::Blacklist::Observer(blacklist),
303 profile_(profile),
[email protected]bd306722012-07-11 20:43:59304 system_(extensions::ExtensionSystem::Get(profile)),
[email protected]73c47932010-12-06 18:13:43305 extension_prefs_(extension_prefs),
[email protected]695b5712012-12-06 23:55:28306 blacklist_(blacklist),
[email protected]5fdfa562013-12-27 17:43:59307 registry_(extensions::ExtensionRegistry::Get(profile)),
[email protected]6c9bedf2014-05-21 03:55:51308 pending_extension_manager_(profile),
[email protected]a9b00ac2009-06-25 21:03:23309 install_directory_(install_directory),
[email protected]0436b102011-04-15 18:30:03310 extensions_enabled_(extensions_enabled),
[email protected]4a10006a2013-05-17 23:18:35311 ready_(ready),
ryanackley48bedbd2015-01-27 23:12:14312 shared_module_service_(new extensions::SharedModuleService(profile_)),
rdevlin.cronin5e510e802016-07-26 15:09:20313 renderer_helper_(
314 extensions::RendererStartupHelperFactory::GetForBrowserContext(
315 profile_)),
ryanackley48bedbd2015-01-27 23:12:14316 app_data_migrator_(new extensions::AppDataMigrator(profile_, registry_)) {
[email protected]a29a517a2011-01-21 21:11:12317 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
rkaplowdd66a1342015-03-05 00:31:49318 TRACE_EVENT0("browser,startup", "ExtensionService::ExtensionService::ctor");
[email protected]a8af9fdb2010-10-28 21:52:20319
[email protected]36a784c2009-06-23 06:21:08320 // Figure out if extension installation should be enabled.
[email protected]367d9b172013-12-03 00:31:02321 if (extensions::ExtensionsBrowserClient::Get()->AreExtensionsDisabled(
322 *command_line, profile))
[email protected]6d60703b2009-08-29 01:29:23323 extensions_enabled_ = false;
[email protected]36a784c2009-06-23 06:21:08324
[email protected]3c4abc82012-10-22 22:25:54325 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING,
326 content::NotificationService::AllBrowserContextsAndSources());
[email protected]adf5a102014-07-31 12:44:06327 registrar_.Add(this,
328 extensions::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07329 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27330 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07331 content::NotificationService::AllBrowserContextsAndSources());
[email protected]92dd8d92013-02-26 00:41:08332 registrar_.Add(this, chrome::NOTIFICATION_UPGRADE_RECOMMENDED,
333 content::NotificationService::AllBrowserContextsAndSources());
[email protected]ebe07772014-05-22 04:16:06334 registrar_.Add(this,
335 chrome::NOTIFICATION_PROFILE_DESTRUCTION_STARTED,
336 content::Source<Profile>(profile_));
binjin1569c9b2014-09-05 13:33:18337
338 extensions::ExtensionManagementFactory::GetForBrowserContext(profile_)
339 ->AddObserver(this);
[email protected]4814b512009-11-07 00:12:29340
rkaplowdd66a1342015-03-05 00:31:49341 // Set up the ExtensionUpdater.
[email protected]93fd78f42009-07-10 16:43:17342 if (autoupdate_enabled) {
[email protected]501105b2013-09-26 05:42:02343 int update_frequency = extensions::kDefaultUpdateFrequencySeconds;
[email protected]93fd78f42009-07-10 16:43:17344 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25345 base::StringToInt(command_line->GetSwitchValueASCII(
346 switches::kExtensionsUpdateFrequency),
347 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17348 }
[email protected]044e86992014-01-24 22:59:11349 updater_.reset(new extensions::ExtensionUpdater(
350 this,
351 extension_prefs,
352 profile->GetPrefs(),
353 profile,
354 update_frequency,
rockotffa65b12014-10-14 17:19:06355 extensions::ExtensionsBrowserClient::Get()->GetExtensionCache(),
rockotdb5192992014-09-16 21:27:02356 base::Bind(ChromeExtensionDownloaderFactory::CreateForProfile,
357 profile)));
[email protected]93fd78f42009-07-10 16:43:17358 }
359
[email protected]25ae0152011-11-18 14:40:02360 component_loader_.reset(
361 new extensions::ComponentLoader(this,
362 profile->GetPrefs(),
[email protected]55e16cd2013-12-18 04:36:08363 g_browser_process->local_state(),
364 profile));
[email protected]8e4560b62011-01-14 10:09:14365
[email protected]0436b102011-04-15 18:30:03366 if (extensions_enabled_) {
[email protected]ae4c37e2012-12-21 01:16:25367 extensions::ExternalProviderImpl::CreateExternalProviders(
368 this, profile_, &external_extension_providers_);
[email protected]873531342011-03-09 12:16:05369 }
[email protected]b671760b2010-07-15 21:13:47370
[email protected]74474042013-11-21 12:03:54371 // Set this as the ExtensionService for app sorting to ensure it causes syncs
372 // if required.
[email protected]b3aa7182013-04-25 04:45:23373 is_first_run_ = !extension_prefs_->SetAlertSystemFirstRun();
374
[email protected]373daf972014-04-10 01:50:44375 error_controller_.reset(
376 new extensions::ExtensionErrorController(profile_, is_first_run_));
[email protected]374ceb6f2014-07-02 19:25:34377 external_install_manager_.reset(
378 new extensions::ExternalInstallManager(profile_, is_first_run_));
[email protected]373daf972014-04-10 01:50:44379
[email protected]c77f2352012-08-08 22:07:58380 extension_action_storage_manager_.reset(
381 new extensions::ExtensionActionStorageManager(profile_));
382
[email protected]cb0e50312011-05-09 15:03:07383 // How long is the path to the Extensions directory?
384 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ExtensionRootPathLength",
drbasicf0d1b262016-08-23 06:10:42385 install_directory_.value().length(), 1, 500, 100);
[email protected]6014d672008-12-05 00:38:25386}
387
[email protected]3f213ad2012-07-26 23:39:41388extensions::PendingExtensionManager*
389 ExtensionService::pending_extension_manager() {
[email protected]b2907fd2011-03-25 16:43:37390 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45391}
392
[email protected]eaa7dd182010-12-14 11:09:00393ExtensionService::~ExtensionService() {
[email protected]c8d407e2011-04-28 21:27:17394 // No need to unload extensions here because they are profile-scoped, and the
395 // profile is in the process of being deleted.
achuith2f5578b2016-02-26 21:57:13396 for (const auto& provider : external_extension_providers_)
[email protected]8e4560b62011-01-14 10:09:14397 provider->ServiceShutdown();
[email protected]6014d672008-12-05 00:38:25398}
399
[email protected]037228a2012-10-05 01:36:16400void ExtensionService::Shutdown() {
binjin1569c9b2014-09-05 13:33:18401 extensions::ExtensionManagementFactory::GetInstance()
402 ->GetForBrowserContext(profile())
403 ->RemoveObserver(this);
[email protected]037228a2012-10-05 01:36:16404}
405
[email protected]b2907fd2011-03-25 16:43:37406const Extension* ExtensionService::GetExtensionById(
407 const std::string& id, bool include_disabled) const {
[email protected]599539802014-01-07 23:06:00408 int include_mask = ExtensionRegistry::ENABLED;
[email protected]695b5712012-12-06 23:55:28409 if (include_disabled) {
mlerman6a37b6a42014-11-26 22:10:53410 // Include blacklisted and blocked extensions here because there are
411 // hundreds of callers of this function, and many might assume that this
412 // includes those that have been disabled due to blacklisting or blocking.
[email protected]599539802014-01-07 23:06:00413 include_mask |= ExtensionRegistry::DISABLED |
mlerman6a37b6a42014-11-26 22:10:53414 ExtensionRegistry::BLACKLISTED | ExtensionRegistry::BLOCKED;
[email protected]695b5712012-12-06 23:55:28415 }
[email protected]599539802014-01-07 23:06:00416 return registry_->GetExtensionById(id, include_mask);
[email protected]695b5712012-12-06 23:55:28417}
418
[email protected]eaa7dd182010-12-14 11:09:00419void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12420 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
rkaplowe65c2ff2015-02-14 16:29:54421 TRACE_EVENT0("browser,startup", "ExtensionService::Init");
rkaplowa8fd8d32015-02-25 21:27:56422 SCOPED_UMA_HISTOGRAM_TIMER("Extensions.ExtensionServiceInitTime");
[email protected]3350d712013-11-18 09:32:24423
[email protected]4a10006a2013-05-17 23:18:35424 DCHECK(!is_ready()); // Can't redo init.
[email protected]bb1bc9b32013-12-21 03:09:14425 DCHECK_EQ(registry_->enabled_extensions().size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32426
benwells22ab8ee2015-08-21 02:55:08427 // LoadAllExtensions() calls OnLoadedInstalledExtensions().
428 component_loader_->LoadAll();
429 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]9f4e4f082013-06-21 07:11:19430
benwells22ab8ee2015-08-21 02:55:08431 EnabledReloadableExtensions();
432 MaybeFinishShutdownDelayed();
433 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25434
benwells22ab8ee2015-08-21 02:55:08435 // TODO(erikkay): this should probably be deferred to a future point
436 // rather than running immediately at startup.
437 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57438
benwells22ab8ee2015-08-21 02:55:08439 LoadGreylistFromPrefs();
rkaplowa8fd8d32015-02-25 21:27:56440}
[email protected]3350d712013-11-18 09:32:24441
rkaplowa8fd8d32015-02-25 21:27:56442void ExtensionService::EnabledReloadableExtensions() {
443 TRACE_EVENT0("browser,startup",
444 "ExtensionService::EnabledReloadableExtensions");
445
446 std::vector<std::string> extensions_to_enable;
achuith2f5578b2016-02-26 21:57:13447 for (const auto& e : registry_->disabled_extensions()) {
rkaplowa8fd8d32015-02-25 21:27:56448 if (extension_prefs_->GetDisableReasons(e->id()) ==
achuith2f5578b2016-02-26 21:57:13449 Extension::DISABLE_RELOAD)
rkaplowa8fd8d32015-02-25 21:27:56450 extensions_to_enable.push_back(e->id());
rkaplowa8fd8d32015-02-25 21:27:56451 }
452 for (const std::string& extension : extensions_to_enable) {
453 EnableExtension(extension);
454 }
455}
456
457void ExtensionService::MaybeFinishShutdownDelayed() {
458 TRACE_EVENT0("browser,startup",
459 "ExtensionService::MaybeFinishShutdownDelayed");
460
dchengc963c7142016-04-08 03:55:22461 std::unique_ptr<extensions::ExtensionPrefs::ExtensionsInfo> delayed_info(
rkaplowa8fd8d32015-02-25 21:27:56462 extension_prefs_->GetAllDelayedInstallInfo());
463 for (size_t i = 0; i < delayed_info->size(); ++i) {
464 ExtensionInfo* info = delayed_info->at(i).get();
achuith2f5578b2016-02-26 21:57:13465 scoped_refptr<const Extension> extension(nullptr);
rkaplowa8fd8d32015-02-25 21:27:56466 if (info->extension_manifest) {
467 std::string error;
468 extension = Extension::Create(
469 info->extension_path, info->extension_location,
470 *info->extension_manifest,
471 extension_prefs_->GetDelayedInstallCreationFlags(info->extension_id),
472 info->extension_id, &error);
473 if (extension.get())
474 delayed_installs_.Insert(extension);
475 }
476 }
477 MaybeFinishDelayedInstallations();
dchengc963c7142016-04-08 03:55:22478 std::unique_ptr<extensions::ExtensionPrefs::ExtensionsInfo> delayed_info2(
rkaplowa8fd8d32015-02-25 21:27:56479 extension_prefs_->GetAllDelayedInstallInfo());
480 UMA_HISTOGRAM_COUNTS_100("Extensions.UpdateOnLoad",
481 delayed_info2->size() - delayed_info->size());
[email protected]6014d672008-12-05 00:38:25482}
483
[email protected]2d19eb6e2014-01-27 17:30:00484void ExtensionService::LoadGreylistFromPrefs() {
rkaplowa8fd8d32015-02-25 21:27:56485 TRACE_EVENT0("browser,startup", "ExtensionService::LoadGreylistFromPrefs");
486
dchengc963c7142016-04-08 03:55:22487 std::unique_ptr<ExtensionSet> all_extensions =
[email protected]f47f7172014-03-19 19:27:10488 registry_->GenerateInstalledExtensionsSet();
[email protected]2d19eb6e2014-01-27 17:30:00489
achuith2f5578b2016-02-26 21:57:13490 for (const auto& extension : *all_extensions) {
491 const extensions::BlacklistState state =
492 extension_prefs_->GetExtensionBlacklistState(extension->id());
[email protected]2d19eb6e2014-01-27 17:30:00493 if (state == extensions::BLACKLISTED_SECURITY_VULNERABILITY ||
494 state == extensions::BLACKLISTED_POTENTIALLY_UNWANTED ||
495 state == extensions::BLACKLISTED_CWS_POLICY_VIOLATION)
achuith2f5578b2016-02-26 21:57:13496 greylist_.Insert(extension);
[email protected]2d19eb6e2014-01-27 17:30:00497 }
498}
499
ginkage553af3202015-02-04 12:39:09500bool ExtensionService::UpdateExtension(const extensions::CRXFileInfo& file,
[email protected]044e86992014-01-24 22:59:11501 bool file_ownership_passed,
[email protected]31bb5ee62012-09-12 22:58:40502 CrxInstaller** out_crx_installer) {
[email protected]a29a517a2011-01-21 21:11:12503 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]3c4abc82012-10-22 22:25:54504 if (browser_terminating_) {
505 LOG(WARNING) << "Skipping UpdateExtension due to browser shutdown";
506 // Leak the temp file at extension_path. We don't want to add to the disk
507 // I/O burden at shutdown, we can't rely on the I/O completing anyway, and
508 // the file is in the OS temp directory which should be cleaned up for us.
509 return false;
510 }
[email protected]a8af9fdb2010-10-28 21:52:20511
ginkage553af3202015-02-04 12:39:09512 const std::string& id = file.extension_id;
513
[email protected]3f213ad2012-07-26 23:39:41514 const extensions::PendingExtensionInfo* pending_extension_info =
[email protected]51a3bf8b2012-06-08 22:53:06515 pending_extension_manager()->GetById(id);
[email protected]8ef78fd2010-08-19 17:14:32516
[email protected]695b5712012-12-06 23:55:28517 const Extension* extension = GetInstalledExtension(id);
[email protected]51a3bf8b2012-06-08 22:53:06518 if (!pending_extension_info && !extension) {
[email protected]aa142702010-03-26 01:26:33519 LOG(WARNING) << "Will not update extension " << id
520 << " because it is not installed or pending";
521 // Delete extension_path since we're not creating a CrxInstaller
522 // that would do it for us.
asargentd50b18c2016-04-21 01:17:16523 if (file_ownership_passed &&
524 !GetFileTaskRunner()->PostTask(
[email protected]7f8f24f2012-11-15 19:40:14525 FROM_HERE,
ginkage553af3202015-02-04 12:39:09526 base::Bind(&extensions::file_util::DeleteFile, file.path, false)))
[email protected]14908b72011-04-20 06:54:36527 NOTREACHED();
[email protected]420a0ec2011-06-01 01:07:03528
529 return false;
[email protected]e957fe52009-06-23 16:51:05530 }
531
treibe960e282015-09-11 10:38:08532 scoped_refptr<CrxInstaller> installer(CrxInstaller::CreateSilent(this));
[email protected]6dfbbf82010-03-12 23:09:16533 installer->set_expected_id(id);
ginkage47e603e2015-02-27 08:42:41534 installer->set_expected_hash(file.expected_hash);
[email protected]464213a2013-10-15 01:06:48535 int creation_flags = Extension::NO_FLAGS;
[email protected]51a3bf8b2012-06-08 22:53:06536 if (pending_extension_info) {
537 installer->set_install_source(pending_extension_info->install_source());
pkotwicz31eb1e8ec2014-10-08 15:26:46538 installer->set_allow_silent_install(true);
treibe960e282015-09-11 10:38:08539 // If the extension came in disabled due to a permission increase, then
540 // don't grant it all the permissions. crbug.com/484214
541 bool has_permissions_increase =
542 extensions::ExtensionPrefs::Get(profile_)->HasDisableReason(
543 id, Extension::DISABLE_PERMISSIONS_INCREASE);
544 const base::Version& expected_version = pending_extension_info->version();
545 if (has_permissions_increase ||
546 pending_extension_info->remote_install() ||
547 !expected_version.IsValid()) {
[email protected]21db9ef2014-05-16 02:06:27548 installer->set_grant_permissions(false);
treibe960e282015-09-11 10:38:08549 } else {
550 installer->set_expected_version(expected_version,
551 false /* fail_install_if_unexpected */);
552 }
mamir0128d5a2016-07-15 20:55:48553 creation_flags = pending_extension_info->creation_flags();
[email protected]464213a2013-10-15 01:06:48554 if (pending_extension_info->mark_acknowledged())
[email protected]374ceb6f2014-07-02 19:25:34555 external_install_manager_->AcknowledgeExternalExtension(id);
[email protected]51a3bf8b2012-06-08 22:53:06556 } else if (extension) {
[email protected]5eb375e92010-11-26 07:50:41557 installer->set_install_source(extension->location());
[email protected]51a3bf8b2012-06-08 22:53:06558 }
[email protected]7d8b7072012-04-07 01:07:46559 // If the extension was installed from or has migrated to the webstore, or
[email protected]fcb2c2c2012-10-17 21:08:45560 // its auto-update URL is from the webstore, treat it as a webstore install.
561 // Note that we ignore some older extensions with blank auto-update URLs
562 // because we are mostly concerned with restrictions on NaCl extensions,
563 // which are newer.
[email protected]75764512011-12-19 19:54:28564 if ((extension && extension->from_webstore()) ||
[email protected]4a5fe3e22013-06-04 07:06:38565 (extension && extensions::ManifestURL::UpdatesFromGallery(extension)) ||
[email protected]963d13c2012-09-29 17:13:35566 (!extension && extension_urls::IsWebstoreUpdateUrl(
[email protected]51a3bf8b2012-06-08 22:53:06567 pending_extension_info->update_url()))) {
[email protected]a12ce8b22012-01-17 18:40:53568 creation_flags |= Extension::FROM_WEBSTORE;
569 }
570
571 // Bookmark apps being updated is kind of a contradiction, but that's because
572 // we mark the default apps as bookmark apps, and they're hosted in the web
573 // store, thus they can get updated. See http://crbug.com/101605 for more
574 // details.
575 if (extension && extension->from_bookmark())
576 creation_flags |= Extension::FROM_BOOKMARK;
577
[email protected]e33bbc22012-08-27 22:05:46578 if (extension && extension->was_installed_by_default())
579 creation_flags |= Extension::WAS_INSTALLED_BY_DEFAULT;
580
[email protected]acc3c7c22014-03-19 06:23:39581 if (extension && extension->was_installed_by_oem())
582 creation_flags |= Extension::WAS_INSTALLED_BY_OEM;
583
benwells1dd4acd2015-12-09 02:20:24584 if (extension)
[email protected]c30bda262014-06-19 04:10:13585 installer->set_do_not_sync(extension_prefs_->DoNotSync(id));
[email protected]ab6c7be42014-01-16 02:05:54586
[email protected]a12ce8b22012-01-17 18:40:53587 installer->set_creation_flags(creation_flags);
588
[email protected]044e86992014-01-24 22:59:11589 installer->set_delete_source(file_ownership_passed);
[email protected]cb0e50312011-05-09 15:03:07590 installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
ginkage553af3202015-02-04 12:39:09591 installer->InstallCrxFile(file);
[email protected]420a0ec2011-06-01 01:07:03592
593 if (out_crx_installer)
[email protected]dc24976f2013-06-02 21:15:09594 *out_crx_installer = installer.get();
[email protected]420a0ec2011-06-01 01:07:03595
596 return true;
[email protected]e957fe52009-06-23 16:51:05597}
598
[email protected]bca4b832014-07-17 20:22:34599void ExtensionService::ReloadExtensionImpl(
[email protected]757d60a2014-05-23 00:11:44600 // "transient" because the process of reloading may cause the reference
601 // to become invalid. Instead, use |extension_id|, a copy.
[email protected]bca4b832014-07-17 20:22:34602 const std::string& transient_extension_id,
603 bool be_noisy) {
[email protected]a29a517a2011-01-21 21:11:12604 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]2a947202013-03-06 04:58:05605
606 // If the extension is already reloading, don't reload again.
[email protected]757d60a2014-05-23 00:11:44607 if (extension_prefs_->GetDisableReasons(transient_extension_id) &
[email protected]2a947202013-03-06 04:58:05608 Extension::DISABLE_RELOAD) {
609 return;
610 }
611
mlerman6a37b6a42014-11-26 22:10:53612 // Ignore attempts to reload a blacklisted or blocked extension. Sometimes
613 // this can happen in a convoluted reload sequence triggered by the
614 // termination of a blacklisted or blocked extension and a naive attempt to
615 // reload it. For an example see http://crbug.com/373842.
616 if (registry_->blacklisted_extensions().Contains(transient_extension_id) ||
617 registry_->blocked_extensions().Contains(transient_extension_id)) {
[email protected]757d60a2014-05-23 00:11:44618 return;
mlerman6a37b6a42014-11-26 22:10:53619 }
[email protected]757d60a2014-05-23 00:11:44620
[email protected]650b2d52013-02-10 03:41:45621 base::FilePath path;
[email protected]757d60a2014-05-23 00:11:44622
623 std::string extension_id = transient_extension_id;
624 const Extension* transient_current_extension =
625 GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40626
[email protected]f17dbd42010-08-16 23:21:10627 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]757d60a2014-05-23 00:11:44628 if (transient_current_extension) {
[email protected]4814b512009-11-07 00:12:29629 // If the extension has an inspector open for its background page, detach
630 // the inspector and hang onto a cookie for it, so that we can reattach
631 // later.
[email protected]31d8f5f22012-04-02 15:22:08632 // TODO(yoz): this is not incognito-safe!
reillyg0ea3fa902014-10-28 15:30:23633 extensions::ProcessManager* manager =
634 extensions::ProcessManager::Get(profile_);
[email protected]3a1dc572012-07-31 22:25:13635 extensions::ExtensionHost* host =
636 manager->GetBackgroundHostForExtension(extension_id);
[email protected]b3f957e62014-08-08 10:09:02637 if (host && DevToolsAgentHost::HasFor(host->host_contents())) {
[email protected]4814b512009-11-07 00:12:29638 // Look for an open inspector for the background page.
[email protected]04ea1bb2013-07-10 09:26:09639 scoped_refptr<DevToolsAgentHost> agent_host =
[email protected]b3f957e62014-08-08 10:09:02640 DevToolsAgentHost::GetOrCreateFor(host->host_contents());
641 agent_host->DisconnectWebContents();
[email protected]04ea1bb2013-07-10 09:26:09642 orphaned_dev_tools_[extension_id] = agent_host;
[email protected]4814b512009-11-07 00:12:29643 }
644
[email protected]757d60a2014-05-23 00:11:44645 path = transient_current_extension->path();
[email protected]58076192013-07-19 19:43:50646 // BeingUpgraded is set back to false when the extension is added.
rdevlin.cronin2c16c602014-11-21 01:35:49647 system_->runtime_data()->SetBeingUpgraded(transient_current_extension->id(),
[email protected]757d60a2014-05-23 00:11:44648 true);
[email protected]44d62b62012-04-11 00:06:03649 DisableExtension(extension_id, Extension::DISABLE_RELOAD);
[email protected]b914e2952013-04-26 07:10:03650 reloading_extensions_.insert(extension_id);
[email protected]1eb175082010-02-10 09:26:16651 } else {
[email protected]d32c02a2014-06-16 22:58:36652 std::map<std::string, base::FilePath>::const_iterator iter =
653 unloaded_extension_paths_.find(extension_id);
654 if (iter == unloaded_extension_paths_.end()) {
655 return;
656 }
[email protected]1eb175082010-02-10 09:26:16657 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06658 }
659
achuith2f5578b2016-02-26 21:57:13660 transient_current_extension = nullptr;
[email protected]757d60a2014-05-23 00:11:44661
[email protected]9f4e4f082013-06-21 07:11:19662 if (delayed_installs_.Contains(extension_id)) {
[email protected]6f6101832012-11-27 22:10:48663 FinishDelayedInstallation(extension_id);
[email protected]0db124b02012-11-07 04:55:05664 return;
665 }
666
[email protected]43ceb002012-02-10 23:19:15667 // If we're reloading a component extension, use the component extension
668 // loader's reloader.
669 if (component_loader_->Exists(extension_id)) {
670 component_loader_->Reload(extension_id);
671 return;
672 }
673
[email protected]e6090e42010-03-23 22:44:08674 // Check the installed extensions to see if what we're reloading was already
675 // installed.
dchengc963c7142016-04-08 03:55:22676 std::unique_ptr<ExtensionInfo> installed_extension(
[email protected]e6090e42010-03-23 22:44:08677 extension_prefs_->GetInstalledExtensionInfo(extension_id));
678 if (installed_extension.get() &&
679 installed_extension->extension_manifest.get()) {
[email protected]d8c8f25f2011-11-02 18:18:01680 extensions::InstalledLoader(this).Load(*installed_extension, false);
[email protected]e6090e42010-03-23 22:44:08681 } else {
[email protected]d8c8f25f2011-11-02 18:18:01682 // Otherwise, the extension is unpacked (location LOAD).
[email protected]e6090e42010-03-23 22:44:08683 // We should always be able to remember the extension's path. If it's not in
684 // the map, someone failed to update |unloaded_extension_paths_|.
685 CHECK(!path.empty());
[email protected]bca4b832014-07-17 20:22:34686 scoped_refptr<extensions::UnpackedInstaller> unpacked_installer =
687 extensions::UnpackedInstaller::Create(this);
688 unpacked_installer->set_be_noisy_on_failure(be_noisy);
689 unpacked_installer->Load(path);
[email protected]e6090e42010-03-23 22:44:08690 }
[email protected]9cddd4702009-07-27 22:09:40691}
692
[email protected]bca4b832014-07-17 20:22:34693void ExtensionService::ReloadExtension(const std::string& extension_id) {
asargentd50b18c2016-04-21 01:17:16694 ReloadExtensionImpl(extension_id, true); // be_noisy
[email protected]bca4b832014-07-17 20:22:34695}
696
697void ExtensionService::ReloadExtensionWithQuietFailure(
698 const std::string& extension_id) {
asargentd50b18c2016-04-21 01:17:16699 ReloadExtensionImpl(extension_id, false); // be_noisy
[email protected]bca4b832014-07-17 20:22:34700}
701
[email protected]757d60a2014-05-23 00:11:44702bool ExtensionService::UninstallExtension(
703 // "transient" because the process of uninstalling may cause the reference
704 // to become invalid. Instead, use |extenson->id()|.
705 const std::string& transient_extension_id,
[email protected]e43c61f2014-07-20 21:46:34706 extensions::UninstallReason reason,
[email protected]42d58f62014-07-31 01:32:45707 const base::Closure& deletion_done_callback,
[email protected]757d60a2014-05-23 00:11:44708 base::string16* error) {
[email protected]a29a517a2011-01-21 21:11:12709 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20710
[email protected]757d60a2014-05-23 00:11:44711 scoped_refptr<const Extension> extension =
712 GetInstalledExtension(transient_extension_id);
[email protected]631cf822009-05-15 07:01:25713
[email protected]e7afe2452010-08-22 16:19:13714 // Callers should not send us nonexistent extensions.
[email protected]dc24976f2013-06-02 21:15:09715 CHECK(extension.get());
[email protected]9f1087e2009-06-15 17:29:32716
binjincccacef2014-10-13 19:00:20717 ManagementPolicy* by_policy = system_->management_policy();
[email protected]95da88c42011-03-31 10:07:33718 // Policy change which triggers an uninstall will always set
719 // |external_uninstall| to true so this is the only way to uninstall
720 // managed extensions.
[email protected]90878c52014-04-04 18:21:02721 // Shared modules being uninstalled will also set |external_uninstall| to true
722 // so that we can guarantee users don't uninstall a shared module.
723 // (crbug.com/273300)
724 // TODO(rdevlin.cronin): This is probably not right. We should do something
725 // else, like include an enum IS_INTERNAL_UNINSTALL or IS_USER_UNINSTALL so
726 // we don't do this.
[email protected]cc2f55c2014-07-08 02:19:04727 bool external_uninstall =
[email protected]e43c61f2014-07-20 21:46:34728 (reason == extensions::UNINSTALL_REASON_INTERNAL_MANAGEMENT) ||
dtseng9fb3d5b2015-02-23 17:24:17729 (reason == extensions::UNINSTALL_REASON_COMPONENT_REMOVED) ||
rdevlin.cronin5f6b69d2014-09-20 01:23:35730 (reason == extensions::UNINSTALL_REASON_REINSTALL) ||
[email protected]e43c61f2014-07-20 21:46:34731 (reason == extensions::UNINSTALL_REASON_ORPHANED_EXTERNAL_EXTENSION) ||
[email protected]6ef60012014-07-22 19:08:12732 (reason == extensions::UNINSTALL_REASON_ORPHANED_SHARED_MODULE) ||
733 (reason == extensions::UNINSTALL_REASON_SYNC &&
mamir192d7882016-06-22 17:10:16734 extensions::util::WasInstalledByCustodian(extension->id(), profile_));
[email protected]65187152012-06-02 13:14:14735 if (!external_uninstall &&
binjincccacef2014-10-13 19:00:20736 (!by_policy->UserMayModifySettings(extension.get(), error) ||
737 by_policy->MustRemainInstalled(extension.get(), error))) {
[email protected]ad50def52011-10-19 23:17:07738 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:06739 extensions::NOTIFICATION_EXTENSION_UNINSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:53740 content::Source<Profile>(profile_),
[email protected]dc24976f2013-06-02 21:15:09741 content::Details<const Extension>(extension.get()));
[email protected]d6ebc9792011-04-07 18:18:33742 return false;
743 }
[email protected]95da88c42011-03-31 10:07:33744
juncai33e462102015-05-18 20:48:44745 InstallVerifier::Get(GetBrowserContext())->Remove(extension->id());
[email protected]ffd2f79e2013-11-14 00:11:46746
[email protected]9b217652010-10-08 22:04:23747 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08748 extension->GetType(), 100);
treib2e0517f92015-05-07 23:15:36749 RecordPermissionMessagesHistogram(extension.get(), "Uninstall");
[email protected]9b217652010-10-08 22:04:23750
[email protected]831aa212010-03-26 13:55:19751 // Unload before doing more cleanup to ensure that nothing is hanging on to
752 // any of these resources.
[email protected]757d60a2014-05-23 00:11:44753 UnloadExtension(extension->id(), UnloadedExtensionInfo::REASON_UNINSTALL);
asargent96c7ec42016-05-27 02:45:47754 if (registry_->blacklisted_extensions().Contains(extension->id()))
755 registry_->RemoveBlacklisted(extension->id());
[email protected]831aa212010-03-26 13:55:19756
[email protected]9f1087e2009-06-15 17:29:32757 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]12075d12013-02-27 05:38:05758 if (!Manifest::IsUnpackedLocation(extension->location())) {
[email protected]7f8f24f2012-11-15 19:40:14759 if (!GetFileTaskRunner()->PostTask(
760 FROM_HERE,
[email protected]5a145e82014-05-29 22:19:07761 base::Bind(&ExtensionService::UninstallExtensionOnFileThread,
762 extension->id(),
763 profile_,
[email protected]85df9d12014-04-15 17:02:14764 install_directory_,
[email protected]5a145e82014-05-29 22:19:07765 extension->path())))
[email protected]14908b72011-04-20 06:54:36766 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32767 }
768
[email protected]42d58f62014-07-31 01:32:45769 extensions::DataDeleter::StartDeleting(
770 profile_, extension.get(), deletion_done_callback);
[email protected]0d6ec3a72011-09-02 02:09:43771
[email protected]757d60a2014-05-23 00:11:44772 UntrackTerminatedExtension(extension->id());
[email protected]211030342010-09-30 18:41:06773
774 // Notify interested parties that we've uninstalled this extension.
[email protected]e43c61f2014-07-20 21:46:34775 ExtensionRegistry::Get(profile_)
776 ->TriggerOnUninstalled(extension.get(), reason);
[email protected]d6ebc9792011-04-07 18:18:33777
[email protected]757d60a2014-05-23 00:11:44778 delayed_installs_.Remove(extension->id());
[email protected]0db124b02012-11-07 04:55:05779
[email protected]757d60a2014-05-23 00:11:44780 extension_prefs_->OnExtensionUninstalled(
781 extension->id(), extension->location(), external_uninstall);
[email protected]d4eda592013-09-18 03:37:57782
[email protected]333b1de2011-09-12 18:28:50783 // Track the uninstallation.
[email protected]49098f702011-10-13 03:47:18784 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionUninstalled", 1, 2);
785
[email protected]d6ebc9792011-04-07 18:18:33786 return true;
[email protected]c10da4b02010-03-25 14:38:32787}
788
[email protected]5a145e82014-05-29 22:19:07789// static
790void ExtensionService::UninstallExtensionOnFileThread(
791 const std::string& id,
792 Profile* profile,
793 const base::FilePath& install_dir,
794 const base::FilePath& extension_path) {
795 extensions::ExtensionAssetsManager* assets_manager =
796 extensions::ExtensionAssetsManager::GetInstance();
797 assets_manager->UninstallExtension(id, profile, install_dir, extension_path);
798}
799
[email protected]c3cfb012011-04-06 22:07:35800bool ExtensionService::IsExtensionEnabled(
801 const std::string& extension_id) const {
[email protected]bb1bc9b32013-12-21 03:09:14802 if (registry_->enabled_extensions().Contains(extension_id) ||
803 registry_->terminated_extensions().Contains(extension_id)) {
[email protected]36429da2011-07-11 20:25:18804 return true;
[email protected]dc9a74f72012-08-17 18:07:21805 }
[email protected]36429da2011-07-11 20:25:18806
[email protected]bb1bc9b32013-12-21 03:09:14807 if (registry_->disabled_extensions().Contains(extension_id) ||
mlerman6a37b6a42014-11-26 22:10:53808 registry_->blacklisted_extensions().Contains(extension_id) ||
809 registry_->blocked_extensions().Contains(extension_id)) {
[email protected]ad83ca242011-07-29 01:32:25810 return false;
[email protected]695b5712012-12-06 23:55:28811 }
[email protected]ad83ca242011-07-29 01:32:25812
mlerman3690e5be2014-12-01 22:57:44813 // Blocked extensions aren't marked as such in prefs, thus if
814 // |block_extensions_| is true then CanBlockExtension() must be called with an
815 // Extension object. If the |extension_id| is not loaded, assume not enabled.
816 if (block_extensions_) {
817 const Extension* extension = GetInstalledExtension(extension_id);
818 if (!extension || CanBlockExtension(extension))
819 return false;
820 }
mlerman6a37b6a42014-11-26 22:10:53821
[email protected]ad83ca242011-07-29 01:32:25822 // If the extension hasn't been loaded yet, check the prefs for it. Assume
823 // enabled unless otherwise noted.
824 return !extension_prefs_->IsExtensionDisabled(extension_id) &&
[email protected]27e528322014-05-27 20:54:30825 !extension_prefs_->IsExtensionBlacklisted(extension_id) &&
[email protected]f574c402012-12-04 23:20:31826 !extension_prefs_->IsExternalExtensionUninstalled(extension_id);
[email protected]c3cfb012011-04-06 22:07:35827}
828
[email protected]eaa7dd182010-12-14 11:09:00829void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12830 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20831
asargent96c7ec42016-05-27 02:45:47832 if (IsExtensionEnabled(extension_id) ||
833 extension_prefs_->IsExtensionBlacklisted(extension_id))
[email protected]0c6da502009-08-14 22:32:39834 return;
[email protected]bb1bc9b32013-12-21 03:09:14835 const Extension* extension =
836 registry_->disabled_extensions().GetByID(extension_id);
[email protected]0d54b682013-11-05 14:15:36837
838 ManagementPolicy* policy = system_->management_policy();
achuith2f5578b2016-02-26 21:57:13839 if (extension && policy->MustRemainDisabled(extension, nullptr, nullptr)) {
[email protected]0d54b682013-11-05 14:15:36840 UMA_HISTOGRAM_COUNTS_100("Extensions.EnableDeniedByPolicy", 1);
841 return;
842 }
[email protected]0c6da502009-08-14 22:32:39843
treib8a6d9892015-08-26 10:23:19844 extension_prefs_->SetExtensionEnabled(extension_id);
[email protected]1784e83a2009-09-08 21:01:52845
treib8a6d9892015-08-26 10:23:19846 // This can happen if sync enables an extension that is not installed yet.
[email protected]06f92562011-04-29 19:27:31847 if (!extension)
848 return;
849
[email protected]0c6da502009-08-14 22:32:39850 // Move it over to the enabled list.
[email protected]bb1bc9b32013-12-21 03:09:14851 registry_->AddEnabled(make_scoped_refptr(extension));
852 registry_->RemoveDisabled(extension->id());
[email protected]0c6da502009-08-14 22:32:39853
[email protected]62d30f42009-10-01 22:36:06854 NotifyExtensionLoaded(extension);
[email protected]3bdba0d2011-08-23 07:17:30855
[email protected]f74d7f32012-06-19 19:22:51856 // Notify listeners that the extension was enabled.
857 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:06858 extensions::NOTIFICATION_EXTENSION_ENABLED,
[email protected]f74d7f32012-06-19 19:22:51859 content::Source<Profile>(profile_),
860 content::Details<const Extension>(extension));
[email protected]0c6da502009-08-14 22:32:39861}
862
treibc1192322015-05-20 12:56:07863void ExtensionService::DisableExtension(const std::string& extension_id,
864 int disable_reasons) {
[email protected]a29a517a2011-01-21 21:11:12865 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20866
asargent96c7ec42016-05-27 02:45:47867 if (extension_prefs_->IsExtensionBlacklisted(extension_id))
868 return;
869
treib8a6d9892015-08-26 10:23:19870 // The extension may have been disabled already. Just add the disable reasons.
[email protected]8f959f522014-08-06 06:26:28871 if (!IsExtensionEnabled(extension_id)) {
treibc1192322015-05-20 12:56:07872 extension_prefs_->AddDisableReasons(extension_id, disable_reasons);
[email protected]1784e83a2009-09-08 21:01:52873 return;
[email protected]8f959f522014-08-06 06:26:28874 }
[email protected]1784e83a2009-09-08 21:01:52875
[email protected]06f92562011-04-29 19:27:31876 const Extension* extension = GetInstalledExtension(extension_id);
elijahtaylor49c09c5c2016-07-09 01:06:22877
878 // Shared modules cannot be disabled, they are just resources used by other
879 // extensions, and are not user controlled.
880 if (extension && SharedModuleInfo::IsSharedModule(extension))
881 return;
882
achuith2f5578b2016-02-26 21:57:13883 // |extension| can be nullptr if sync disables an extension that is not
[email protected]06f92562011-04-29 19:27:31884 // installed yet.
[email protected]ab894542014-07-03 12:35:39885 // EXTERNAL_COMPONENT extensions are not generally modifiable by users, but
886 // can be uninstalled by the browser if the user sets extension-specific
887 // preferences.
achuith2f5578b2016-02-26 21:57:13888 if (extension && !(disable_reasons & Extension::DISABLE_RELOAD) &&
treibc1192322015-05-20 12:56:07889 !(disable_reasons & Extension::DISABLE_UPDATE_REQUIRED_BY_POLICY) &&
achuith2f5578b2016-02-26 21:57:13890 !system_->management_policy()->UserMayModifySettings(extension,
891 nullptr) &&
[email protected]ab894542014-07-03 12:35:39892 extension->location() != Manifest::EXTERNAL_COMPONENT) {
[email protected]95da88c42011-03-31 10:07:33893 return;
[email protected]65187152012-06-02 13:14:14894 }
[email protected]95da88c42011-03-31 10:07:33895
treib8a6d9892015-08-26 10:23:19896 extension_prefs_->SetExtensionDisabled(extension_id, disable_reasons);
[email protected]1784e83a2009-09-08 21:01:52897
[email protected]599539802014-01-07 23:06:00898 int include_mask =
899 ExtensionRegistry::EVERYTHING & ~ExtensionRegistry::DISABLED;
900 extension = registry_->GetExtensionById(extension_id, include_mask);
[email protected]06f92562011-04-29 19:27:31901 if (!extension)
902 return;
903
[email protected]45f5b7d2014-01-22 23:47:13904 // The extension is either enabled or terminated.
905 DCHECK(registry_->enabled_extensions().Contains(extension->id()) ||
906 registry_->terminated_extensions().Contains(extension->id()));
[email protected]64742de32013-04-22 08:44:34907
[email protected]5c094792012-09-07 19:44:53908 // Move it over to the disabled list. Don't send a second unload notification
909 // for terminated extensions being disabled.
[email protected]bb1bc9b32013-12-21 03:09:14910 registry_->AddDisabled(make_scoped_refptr(extension));
911 if (registry_->enabled_extensions().Contains(extension->id())) {
912 registry_->RemoveEnabled(extension->id());
[email protected]b0af4792013-10-23 09:12:13913 NotifyExtensionUnloaded(extension, UnloadedExtensionInfo::REASON_DISABLE);
[email protected]5c094792012-09-07 19:44:53914 } else {
[email protected]bb1bc9b32013-12-21 03:09:14915 registry_->RemoveTerminated(extension->id());
[email protected]5c094792012-09-07 19:44:53916 }
[email protected]1784e83a2009-09-08 21:01:52917}
918
[email protected]1abf05e2013-07-09 17:04:36919void ExtensionService::DisableUserExtensions(
920 const std::vector<std::string>& except_ids) {
[email protected]e516e4c2013-06-12 17:41:14921 extensions::ManagementPolicy* management_policy =
922 system_->management_policy();
923 extensions::ExtensionList to_disable;
924
achuith2f5578b2016-02-26 21:57:13925 for (const auto& extension : registry_->enabled_extensions()) {
926 if (management_policy->UserMayModifySettings(extension.get(), nullptr))
927 to_disable.push_back(extension);
[email protected]e516e4c2013-06-12 17:41:14928 }
929
achuith2f5578b2016-02-26 21:57:13930 for (const auto& extension : registry_->terminated_extensions()) {
931 if (management_policy->UserMayModifySettings(extension.get(), nullptr))
932 to_disable.push_back(extension);
933 }
934
935 for (const auto& extension : to_disable) {
936 if (extension->was_installed_by_default() &&
[email protected]3082fe32013-08-06 11:12:38937 extension_urls::IsWebstoreUpdateUrl(
achuith2f5578b2016-02-26 21:57:13938 extensions::ManifestURL::GetUpdateURL(extension.get())))
[email protected]3082fe32013-08-06 11:12:38939 continue;
achuith2f5578b2016-02-26 21:57:13940 const std::string& id = extension->id();
[email protected]1abf05e2013-07-09 17:04:36941 if (except_ids.end() == std::find(except_ids.begin(), except_ids.end(), id))
942 DisableExtension(id, extensions::Extension::DISABLE_USER_ACTION);
[email protected]e516e4c2013-06-12 17:41:14943 }
944}
945
mlerman6a37b6a42014-11-26 22:10:53946// Extensions that are not locked, components or forced by policy should be
947// locked. Extensions are no longer considered enabled or disabled. Blacklisted
948// extensions are now considered both blacklisted and locked.
949void ExtensionService::BlockAllExtensions() {
950 if (block_extensions_)
951 return;
952 block_extensions_ = true;
953
954 // Blacklisted extensions are already unloaded, need not be blocked.
dchengc963c7142016-04-08 03:55:22955 std::unique_ptr<ExtensionSet> extensions =
mlerman6a37b6a42014-11-26 22:10:53956 registry_->GenerateInstalledExtensionsSet(ExtensionRegistry::ENABLED |
957 ExtensionRegistry::DISABLED |
958 ExtensionRegistry::TERMINATED);
959
achuith2f5578b2016-02-26 21:57:13960 for (const auto& extension : *extensions) {
mlerman6a37b6a42014-11-26 22:10:53961 const std::string& id = extension->id();
962
963 if (!CanBlockExtension(extension.get()))
964 continue;
965
966 registry_->RemoveEnabled(id);
967 registry_->RemoveDisabled(id);
968 registry_->RemoveTerminated(id);
969
970 registry_->AddBlocked(extension.get());
971 UnloadExtension(id, extensions::UnloadedExtensionInfo::REASON_LOCK_ALL);
972 }
973}
974
975// All locked extensions should revert to being either enabled or disabled
976// as appropriate.
977void ExtensionService::UnblockAllExtensions() {
978 block_extensions_ = false;
dchengc963c7142016-04-08 03:55:22979 std::unique_ptr<ExtensionSet> to_unblock =
mlerman6a37b6a42014-11-26 22:10:53980 registry_->GenerateInstalledExtensionsSet(ExtensionRegistry::BLOCKED);
981
achuith2f5578b2016-02-26 21:57:13982 for (const auto& extension : *to_unblock) {
mlerman6a37b6a42014-11-26 22:10:53983 registry_->RemoveBlocked(extension->id());
984 AddExtension(extension.get());
985 }
986}
987
[email protected]eaa7dd182010-12-14 11:09:00988void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]009633c2013-03-07 22:08:28989 const Extension* extension) {
990 GrantPermissions(extension);
treib2e0517f92015-05-07 23:15:36991 RecordPermissionMessagesHistogram(extension, "ReEnable");
[email protected]8d888c12010-11-30 00:00:25992 EnableExtension(extension->id());
993}
994
[email protected]009633c2013-03-07 22:08:28995void ExtensionService::GrantPermissions(const Extension* extension) {
[email protected]be083862012-09-01 03:53:45996 CHECK(extension);
[email protected]8d42db52014-06-20 21:50:50997 extensions::PermissionsUpdater(profile()).GrantActivePermissions(extension);
[email protected]be083862012-09-01 03:53:45998}
999
[email protected]fe2dd7742011-04-19 22:52:491000// static
1001void ExtensionService::RecordPermissionMessagesHistogram(
[email protected]13c68b62013-05-17 11:29:051002 const Extension* extension, const char* histogram) {
[email protected]de415552013-01-23 04:12:171003 // Since this is called from multiple sources, and since the histogram macros
1004 // use statics, we need to manually lookup the histogram ourselves.
treib2e0517f92015-05-07 23:15:361005 base::HistogramBase* counter = base::LinearHistogram::FactoryGet(
1006 base::StringPrintf("Extensions.Permissions_%s3", histogram),
1007 1,
1008 APIPermission::kEnumBoundary,
1009 APIPermission::kEnumBoundary + 1,
1010 base::HistogramBase::kUmaTargetedHistogramFlag);
1011
1012 base::HistogramBase* counter_has_any = base::BooleanHistogram::FactoryGet(
1013 base::StringPrintf("Extensions.HasPermissions_%s3", histogram),
1014 base::HistogramBase::kUmaTargetedHistogramFlag);
1015
1016 PermissionIDSet permissions =
1017 extensions::PermissionMessageProvider::Get()->GetAllPermissionIDs(
rdevlin.cronine2d0fd02015-09-24 22:35:491018 extension->permissions_data()->active_permissions(),
treib2e0517f92015-05-07 23:15:361019 extension->GetType());
1020 counter_has_any->AddBoolean(!permissions.empty());
1021 for (const PermissionID& id : permissions)
1022 counter->Add(id.id());
[email protected]fe2dd7742011-04-19 22:52:491023}
1024
[email protected]eaa7dd182010-12-14 11:09:001025void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]f9c8c7c72014-07-31 16:42:311026 // The URLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:061027 // was loaded, otherwise a race can arise where a renderer that is created
1028 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:421029 // that the request context doesn't yet know about. The profile is responsible
1030 // for ensuring its URLRequestContexts appropriately discover the loaded
1031 // extension.
rockot494f0072015-07-29 17:58:071032 system_->RegisterExtensionWithRequestContexts(
1033 extension,
1034 base::Bind(&ExtensionService::OnExtensionRegisteredWithRequestContexts,
1035 AsWeakPtr(), make_scoped_refptr(extension)));
[email protected]62d30f42009-10-01 22:36:061036
rdevlin.cronin5e510e802016-07-26 15:09:201037 renderer_helper_->OnExtensionLoaded(*extension);
[email protected]77a6970c2011-04-23 16:58:561038
[email protected]3442a662012-01-12 08:06:171039 // Tell subsystems that use the EXTENSION_LOADED notification about the new
1040 // extension.
1041 //
1042 // NOTE: It is important that this happen after notifying the renderers about
1043 // the new extensions so that if we navigate to an extension URL in
[email protected]fdc76192014-04-20 21:54:411044 // ExtensionRegistryObserver::OnLoaded or
1045 // NOTIFICATION_EXTENSION_LOADED_DEPRECATED, the
[email protected]dcc47642014-03-26 22:03:491046 // renderer is guaranteed to know about it.
1047 registry_->TriggerOnLoaded(extension);
1048
[email protected]3442a662012-01-12 08:06:171049 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061050 extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
[email protected]3442a662012-01-12 08:06:171051 content::Source<Profile>(profile_),
1052 content::Details<const Extension>(extension));
1053
[email protected]dcc47642014-03-26 22:03:491054 // TODO(kalman): Convert ExtensionSpecialStoragePolicy to a
1055 // BrowserContextKeyedService and use ExtensionRegistryObserver.
[email protected]c8d407e2011-04-28 21:27:171056 profile_->GetExtensionSpecialStoragePolicy()->
rdevlin.croninbc8979a2014-09-30 01:08:501057 GrantRightsForExtension(extension, profile_);
[email protected]c8d407e2011-04-28 21:27:171058
[email protected]dcc47642014-03-26 22:03:491059 // TODO(kalman): This is broken. The crash reporter is process-wide so doesn't
1060 // work properly multi-profile. Besides which, it should be using
1061 // ExtensionRegistryObserver. See http://crbug.com/355029.
[email protected]c8d407e2011-04-28 21:27:171062 UpdateActiveExtensionsInCrashReporter();
1063
[email protected]d695bb12014-06-05 16:16:301064 const extensions::PermissionsData* permissions_data =
[email protected]a6910e72014-06-06 05:04:361065 extension->permissions_data();
[email protected]d695bb12014-06-05 16:16:301066
[email protected]c8d407e2011-04-28 21:27:171067 // If the extension has permission to load chrome://favicon/ resources we need
1068 // to make sure that the FaviconSource is registered with the
1069 // ChromeURLDataManager.
[email protected]d695bb12014-06-05 16:16:301070 if (permissions_data->HasHostPermission(GURL(chrome::kChromeUIFaviconURL))) {
[email protected]c8d407e2011-04-28 21:27:171071 FaviconSource* favicon_source = new FaviconSource(profile_,
1072 FaviconSource::FAVICON);
[email protected]24ea7a12013-01-27 23:54:531073 content::URLDataSource::Add(profile_, favicon_source);
[email protected]c8d407e2011-04-28 21:27:171074 }
[email protected]b07e606e2012-09-15 20:16:151075
[email protected]b07e606e2012-09-15 20:16:151076 // Same for chrome://theme/ resources.
[email protected]d695bb12014-06-05 16:16:301077 if (permissions_data->HasHostPermission(GURL(chrome::kChromeUIThemeURL))) {
[email protected]b07e606e2012-09-15 20:16:151078 ThemeSource* theme_source = new ThemeSource(profile_);
[email protected]24ea7a12013-01-27 23:54:531079 content::URLDataSource::Add(profile_, theme_source);
[email protected]b07e606e2012-09-15 20:16:151080 }
[email protected]b07e606e2012-09-15 20:16:151081
[email protected]5eddc3e2011-10-26 04:33:311082 // Same for chrome://thumb/ resources.
[email protected]d695bb12014-06-05 16:16:301083 if (permissions_data->HasHostPermission(
1084 GURL(chrome::kChromeUIThumbnailURL))) {
[email protected]420e6d92013-09-17 01:48:511085 ThumbnailSource* thumbnail_source = new ThumbnailSource(profile_, false);
[email protected]24ea7a12013-01-27 23:54:531086 content::URLDataSource::Add(profile_, thumbnail_source);
[email protected]5eddc3e2011-10-26 04:33:311087 }
[email protected]62d30f42009-10-01 22:36:061088}
1089
rockot494f0072015-07-29 17:58:071090void ExtensionService::OnExtensionRegisteredWithRequestContexts(
1091 scoped_refptr<const extensions::Extension> extension) {
1092 registry_->AddReady(extension);
1093 if (registry_->enabled_extensions().Contains(extension->id()))
1094 registry_->TriggerOnReady(extension.get());
1095}
1096
[email protected]a9f39a312010-12-23 22:14:271097void ExtensionService::NotifyExtensionUnloaded(
[email protected]814a7bf0f2011-08-13 05:30:591098 const Extension* extension,
[email protected]b0af4792013-10-23 09:12:131099 UnloadedExtensionInfo::Reason reason) {
[email protected]a9f39a312010-12-23 22:14:271100 UnloadedExtensionInfo details(extension, reason);
[email protected]e51232f32014-04-18 20:05:361101
1102 registry_->TriggerOnUnloaded(extension, reason);
1103
[email protected]ad50def52011-10-19 23:17:071104 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061105 extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
[email protected]6c2381d2011-10-19 02:52:531106 content::Source<Profile>(profile_),
1107 content::Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061108
rdevlin.croninc40d39f2016-08-04 23:42:131109 renderer_helper_->OnExtensionUnloaded(*extension);
[email protected]77a6970c2011-04-23 16:58:561110
[email protected]31d8f5f22012-04-02 15:22:081111 system_->UnregisterExtensionWithRequestContexts(extension->id(), reason);
[email protected]dcc47642014-03-26 22:03:491112
1113 // TODO(kalman): Convert ExtensionSpecialStoragePolicy to a
1114 // BrowserContextKeyedService and use ExtensionRegistryObserver.
[email protected]c8d407e2011-04-28 21:27:171115 profile_->GetExtensionSpecialStoragePolicy()->
1116 RevokeRightsForExtension(extension);
1117
[email protected]b777b332011-04-16 04:01:081118#if defined(OS_CHROMEOS)
[email protected]ac66e452013-01-11 09:21:231119 // Revoke external file access for the extension from its file system context.
1120 // It is safe to access the extension's storage partition at this point. The
1121 // storage partition may get destroyed only after the extension gets unloaded.
[email protected]3a746ec2014-03-15 05:30:561122 GURL site =
1123 extensions::util::GetSiteForExtensionId(extension->id(), profile_);
[email protected]cd501a72014-08-22 19:58:311124 storage::FileSystemContext* filesystem_context =
1125 BrowserContext::GetStoragePartitionForSite(profile_, site)
1126 ->GetFileSystemContext();
[email protected]f19bbf62013-07-09 01:22:321127 if (filesystem_context && filesystem_context->external_backend()) {
1128 filesystem_context->external_backend()->
[email protected]c8d407e2011-04-28 21:27:171129 RevokeAccessForExtension(extension->id());
[email protected]62d30f42009-10-01 22:36:061130 }
[email protected]c8d407e2011-04-28 21:27:171131#endif
1132
[email protected]dcc47642014-03-26 22:03:491133 // TODO(kalman): This is broken. The crash reporter is process-wide so doesn't
1134 // work properly multi-profile. Besides which, it should be using
1135 // ExtensionRegistryObserver::OnExtensionLoaded. See http://crbug.com/355029.
[email protected]c8d407e2011-04-28 21:27:171136 UpdateActiveExtensionsInCrashReporter();
[email protected]62d30f42009-10-01 22:36:061137}
1138
[email protected]599539802014-01-07 23:06:001139content::BrowserContext* ExtensionService::GetBrowserContext() const {
1140 // Implemented in the .cc file to avoid adding a profile.h dependency to
1141 // extension_service.h.
1142 return profile_;
1143}
1144
[email protected]25ae0152011-11-18 14:40:021145bool ExtensionService::is_ready() {
[email protected]4a10006a2013-05-17 23:18:351146 return ready_->is_signaled();
[email protected]25ae0152011-11-18 14:40:021147}
1148
[email protected]7f8f24f2012-11-15 19:40:141149base::SequencedTaskRunner* ExtensionService::GetFileTaskRunner() {
[email protected]dc24976f2013-06-02 21:15:091150 if (file_task_runner_.get())
1151 return file_task_runner_.get();
[email protected]7f8f24f2012-11-15 19:40:141152
1153 // We should be able to interrupt any part of extension install process during
1154 // shutdown. SKIP_ON_SHUTDOWN ensures that not started extension install tasks
1155 // will be ignored/deleted while we will block on started tasks.
1156 std::string token("ext_install-");
1157 token.append(profile_->GetPath().AsUTF8Unsafe());
1158 file_task_runner_ = BrowserThread::GetBlockingPool()->
1159 GetSequencedTaskRunnerWithShutdownBehavior(
1160 BrowserThread::GetBlockingPool()->GetNamedSequenceToken(token),
1161 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
[email protected]dc24976f2013-06-02 21:15:091162 return file_task_runner_.get();
[email protected]7f8f24f2012-11-15 19:40:141163}
1164
[email protected]4ee07c62012-08-21 12:40:421165void ExtensionService::CheckManagementPolicy() {
[email protected]0d54b682013-11-05 14:15:361166 std::vector<std::string> to_unload;
1167 std::map<std::string, Extension::DisableReason> to_disable;
binjin8e3d0182014-12-04 16:44:281168 std::vector<std::string> to_enable;
[email protected]695b5712012-12-06 23:55:281169
[email protected]0d54b682013-11-05 14:15:361170 // Loop through the extensions list, finding extensions we need to unload or
1171 // disable.
achuith2f5578b2016-02-26 21:57:131172 for (const auto& extension : registry_->enabled_extensions()) {
binjin8e3d0182014-12-04 16:44:281173 if (!system_->management_policy()->UserMayLoad(extension.get(), nullptr))
[email protected]0d54b682013-11-05 14:15:361174 to_unload.push_back(extension->id());
1175 Extension::DisableReason disable_reason = Extension::DISABLE_NONE;
1176 if (system_->management_policy()->MustRemainDisabled(
binjin8e3d0182014-12-04 16:44:281177 extension.get(), &disable_reason, nullptr))
[email protected]0d54b682013-11-05 14:15:361178 to_disable[extension->id()] = disable_reason;
[email protected]aa96d3a2010-08-21 08:45:251179 }
1180
binjin8e3d0182014-12-04 16:44:281181 extensions::ExtensionManagement* management =
1182 extensions::ExtensionManagementFactory::GetForBrowserContext(profile());
1183
1184 // Loop through the disabled extension list, find extensions to re-enable
1185 // automatically. These extensions are exclusive from the |to_disable| and
1186 // |to_unload| lists constructed above, since disabled_extensions() and
1187 // enabled_extensions() are supposed to be mutually exclusive.
achuith2f5578b2016-02-26 21:57:131188 for (const auto& extension : registry_->disabled_extensions()) {
binjin8e3d0182014-12-04 16:44:281189 // Find all disabled extensions disabled due to minimum version requirement,
1190 // but now satisfying it.
1191 if (management->CheckMinimumVersion(extension.get(), nullptr) &&
1192 extension_prefs_->HasDisableReason(
1193 extension->id(), Extension::DISABLE_UPDATE_REQUIRED_BY_POLICY)) {
1194 // Is DISABLE_UPDATE_REQUIRED_BY_POLICY the *only* reason?
1195 if (extension_prefs_->GetDisableReasons(extension->id()) ==
1196 Extension::DISABLE_UPDATE_REQUIRED_BY_POLICY) {
1197 // We need to enable those disabled *only* due to minimum version
1198 // requirement.
1199 to_enable.push_back(extension->id());
1200 }
1201 extension_prefs_->RemoveDisableReason(
1202 extension->id(), Extension::DISABLE_UPDATE_REQUIRED_BY_POLICY);
1203 }
1204 }
1205
1206 for (const std::string& id : to_unload)
1207 UnloadExtension(id, UnloadedExtensionInfo::REASON_DISABLE);
[email protected]0d54b682013-11-05 14:15:361208
achuith2f5578b2016-02-26 21:57:131209 for (const auto& i : to_disable)
1210 DisableExtension(i.first, i.second);
binjin8e3d0182014-12-04 16:44:281211
1212 // No extension is getting re-enabled here after disabling/unloading
1213 // because to_enable is mutually exclusive to to_disable + to_unload.
1214 for (const std::string& id : to_enable)
1215 EnableExtension(id);
1216
1217 if (updater_.get()) {
1218 // Find all extensions disabled due to minimum version requirement from
1219 // policy (including the ones that got disabled just now), and check
1220 // for update.
1221 extensions::ExtensionUpdater::CheckParams to_recheck;
achuith2f5578b2016-02-26 21:57:131222 for (const auto& extension : registry_->disabled_extensions()) {
binjin8e3d0182014-12-04 16:44:281223 if (extension_prefs_->GetDisableReasons(extension->id()) ==
1224 Extension::DISABLE_UPDATE_REQUIRED_BY_POLICY) {
1225 // The minimum version check is the only thing holding this extension
1226 // back, so check if it can be updated to fix that.
1227 to_recheck.ids.push_back(extension->id());
1228 }
1229 }
1230 if (!to_recheck.ids.empty())
1231 updater_->CheckNow(to_recheck);
1232 }
[email protected]aa96d3a2010-08-21 08:45:251233}
1234
[email protected]31206602011-04-13 23:07:321235void ExtensionService::CheckForUpdatesSoon() {
[email protected]90878c52014-04-04 18:21:021236 // This can legitimately happen in unit tests.
1237 if (!updater_.get())
1238 return;
1239
[email protected]90878c52014-04-04 18:21:021240 updater_->CheckSoon();
[email protected]c3cfb012011-04-06 22:07:351241}
1242
[email protected]8e4560b62011-01-14 10:09:141243// Some extensions will autoupdate themselves externally from Chrome. These
rkaplowa8fd8d32015-02-25 21:27:561244// are typically part of some larger client application package. To support
1245// these, the extension will register its location in the preferences file
[email protected]8e4560b62011-01-14 10:09:141246// (and also, on Windows, in the registry) and this code will periodically
1247// check that location for a .crx file, which it will then install locally if
1248// a new version is available.
rkaplowa8fd8d32015-02-25 21:27:561249// Errors are reported through ExtensionErrorReporter. Success is not
[email protected]8e4560b62011-01-14 10:09:141250// reported.
[email protected]eaa7dd182010-12-14 11:09:001251void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:121252 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
rkaplowa8fd8d32015-02-25 21:27:561253 TRACE_EVENT0("browser,startup", "ExtensionService::CheckForExternalUpdates");
1254 SCOPED_UMA_HISTOGRAM_TIMER("Extensions.CheckForExternalUpdatesTime");
[email protected]8e4560b62011-01-14 10:09:141255
1256 // Note that this installation is intentionally silent (since it didn't
1257 // go through the front-end). Extensions that are registered in this
1258 // way are effectively considered 'pre-bundled', and so implicitly
1259 // trusted. In general, if something has HKLM or filesystem access,
1260 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:121261
[email protected]8e4560b62011-01-14 10:09:141262 // Ask each external extension provider to give us a call back for each
1263 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
achuith2f5578b2016-02-26 21:57:131264 for (const auto& provider : external_extension_providers_)
[email protected]8e4560b62011-01-14 10:09:141265 provider->VisitRegisteredExtension();
[email protected]8e4560b62011-01-14 10:09:141266
[email protected]50067e52011-10-20 23:17:071267 // Do any required work that we would have done after completion of all
1268 // providers.
[email protected]373daf972014-04-10 01:50:441269 if (external_extension_providers_.empty())
[email protected]50067e52011-10-20 23:17:071270 OnAllExternalProvidersReady();
[email protected]9f1087e2009-06-15 17:29:321271}
1272
[email protected]50067e52011-10-20 23:17:071273void ExtensionService::OnExternalProviderReady(
lazyboye8634172016-01-28 00:10:481274 const ExternalProviderInterface* provider) {
[email protected]a29a517a2011-01-21 21:11:121275 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]50067e52011-10-20 23:17:071276 CHECK(provider->IsReady());
[email protected]a29a517a2011-01-21 21:11:121277
1278 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:141279 // if all of them are finished. So we check them first.
[email protected]94fde232012-04-27 10:22:301280 if (AreAllExternalProvidersReady())
1281 OnAllExternalProvidersReady();
1282}
1283
1284bool ExtensionService::AreAllExternalProvidersReady() const {
achuith2f5578b2016-02-26 21:57:131285 for (const auto& provider : external_extension_providers_) {
1286 if (!provider->IsReady())
[email protected]94fde232012-04-27 10:22:301287 return false;
[email protected]8e4560b62011-01-14 10:09:141288 }
[email protected]94fde232012-04-27 10:22:301289 return true;
[email protected]50067e52011-10-20 23:17:071290}
1291
1292void ExtensionService::OnAllExternalProvidersReady() {
1293 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]94fde232012-04-27 10:22:301294 base::TimeDelta elapsed = base::Time::Now() - profile_->GetStartTime();
1295 UMA_HISTOGRAM_TIMES("Extension.ExternalProvidersReadyAfter", elapsed);
[email protected]50067e52011-10-20 23:17:071296
1297 // Install any pending extensions.
[email protected]94fde232012-04-27 10:22:301298 if (update_once_all_providers_are_ready_ && updater()) {
1299 update_once_all_providers_are_ready_ = false;
[email protected]4e9b59d2013-12-18 06:47:141300 extensions::ExtensionUpdater::CheckParams params;
1301 params.callback = external_updates_finished_callback_;
1302 updater()->CheckNow(params);
[email protected]8e4560b62011-01-14 10:09:141303 }
1304
1305 // Uninstall all the unclaimed extensions.
dchengc963c7142016-04-08 03:55:221306 std::unique_ptr<extensions::ExtensionPrefs::ExtensionsInfo> extensions_info(
[email protected]8e4560b62011-01-14 10:09:141307 extension_prefs_->GetInstalledExtensionsInfo());
1308 for (size_t i = 0; i < extensions_info->size(); ++i) {
1309 ExtensionInfo* info = extensions_info->at(i).get();
[email protected]1d5e58b2013-01-31 08:41:401310 if (Manifest::IsExternalLocation(info->extension_location))
[email protected]8e4560b62011-01-14 10:09:141311 CheckExternalUninstall(info->extension_id);
1312 }
[email protected]e5af875f2011-10-10 21:09:141313
[email protected]373daf972014-04-10 01:50:441314 error_controller_->ShowErrorIfNeeded();
[email protected]a9aa5932012-01-25 08:27:401315
[email protected]374ceb6f2014-07-02 19:25:341316 external_install_manager_->UpdateExternalExtensionAlert();
[email protected]e5af875f2011-10-10 21:09:141317}
1318
[email protected]a9f39a312010-12-23 22:14:271319void ExtensionService::UnloadExtension(
1320 const std::string& extension_id,
[email protected]b0af4792013-10-23 09:12:131321 UnloadedExtensionInfo::Reason reason) {
[email protected]27e469a2010-01-11 20:35:091322 // Make sure the extension gets deleted after we return from this function.
[email protected]599539802014-01-07 23:06:001323 int include_mask =
1324 ExtensionRegistry::EVERYTHING & ~ExtensionRegistry::TERMINATED;
[email protected]9adb9692010-10-29 23:14:021325 scoped_refptr<const Extension> extension(
[email protected]599539802014-01-07 23:06:001326 registry_->GetExtensionById(extension_id, include_mask));
[email protected]631cf822009-05-15 07:01:251327
[email protected]fa0c96732010-11-17 00:14:231328 // This method can be called via PostTask, so the extension may have been
1329 // unloaded by the time this runs.
[email protected]dc24976f2013-06-02 21:15:091330 if (!extension.get()) {
[email protected]dd163fb02011-05-04 22:22:171331 // In case the extension may have crashed/uninstalled. Allow the profile to
1332 // clean up its RequestContexts.
[email protected]31d8f5f22012-04-02 15:22:081333 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]fa0c96732010-11-17 00:14:231334 return;
[email protected]dd163fb02011-05-04 22:22:171335 }
[email protected]0c6da502009-08-14 22:32:391336
[email protected]1eb175082010-02-10 09:26:161337 // Keep information about the extension so that we can reload it later
1338 // even if it's not permanently installed.
1339 unloaded_extension_paths_[extension->id()] = extension->path();
1340
[email protected]f17dbd42010-08-16 23:21:101341 // Clean up if the extension is meant to be enabled after a reload.
[email protected]b914e2952013-04-26 07:10:031342 reloading_extensions_.erase(extension->id());
[email protected]f17dbd42010-08-16 23:21:101343
[email protected]bb1bc9b32013-12-21 03:09:141344 if (registry_->disabled_extensions().Contains(extension->id())) {
1345 registry_->RemoveDisabled(extension->id());
[email protected]dd163fb02011-05-04 22:22:171346 // Make sure the profile cleans up its RequestContexts when an already
1347 // disabled extension is unloaded (since they are also tracking the disabled
1348 // extensions).
[email protected]31d8f5f22012-04-02 15:22:081349 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]45f5b7d2014-01-22 23:47:131350 // Don't send the unloaded notification. It was sent when the extension
1351 // was disabled.
[email protected]60a174a2013-08-02 20:29:281352 } else {
[email protected]bb1bc9b32013-12-21 03:09:141353 // Remove the extension from the enabled list.
1354 registry_->RemoveEnabled(extension->id());
[email protected]60a174a2013-08-02 20:29:281355 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]0c6da502009-08-14 22:32:391356 }
1357
[email protected]fb789532013-08-27 02:40:211358 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061359 extensions::NOTIFICATION_EXTENSION_REMOVED,
[email protected]fb789532013-08-27 02:40:211360 content::Source<Profile>(profile_),
1361 content::Details<const Extension>(extension.get()));
[email protected]631cf822009-05-15 07:01:251362}
1363
[email protected]8b1ec202013-09-05 02:09:501364void ExtensionService::RemoveComponentExtension(
1365 const std::string& extension_id) {
1366 scoped_refptr<const Extension> extension(
1367 GetExtensionById(extension_id, false));
[email protected]b0af4792013-10-23 09:12:131368 UnloadExtension(extension_id, UnloadedExtensionInfo::REASON_UNINSTALL);
[email protected]ebe07772014-05-22 04:16:061369 if (extension.get()) {
[email protected]e43c61f2014-07-20 21:46:341370 ExtensionRegistry::Get(profile_)->TriggerOnUninstalled(
dtseng9fb3d5b2015-02-23 17:24:171371 extension.get(), extensions::UNINSTALL_REASON_COMPONENT_REMOVED);
[email protected]ebe07772014-05-22 04:16:061372 }
[email protected]8b1ec202013-09-05 02:09:501373}
1374
[email protected]bb1bc9b32013-12-21 03:09:141375void ExtensionService::UnloadAllExtensionsForTest() {
1376 UnloadAllExtensionsInternal();
[email protected]9f1087e2009-06-15 17:29:321377}
1378
[email protected]bb1bc9b32013-12-21 03:09:141379void ExtensionService::ReloadExtensionsForTest() {
1380 // Calling UnloadAllExtensionsForTest here triggers a false-positive presubmit
1381 // warning about calling test code in production.
1382 UnloadAllExtensionsInternal();
[email protected]eac88332012-12-26 17:57:451383 component_loader_->LoadAll();
[email protected]d8c8f25f2011-11-02 18:18:011384 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]4a10006a2013-05-17 23:18:351385 // Don't call SetReadyAndNotifyListeners() since tests call this multiple
1386 // times.
[email protected]9f1087e2009-06-15 17:29:321387}
1388
[email protected]820d9bd2013-04-03 03:46:031389void ExtensionService::SetReadyAndNotifyListeners() {
rkaplowa8fd8d32015-02-25 21:27:561390 TRACE_EVENT0("browser,startup",
1391 "ExtensionService::SetReadyAndNotifyListeners");
1392 SCOPED_UMA_HISTOGRAM_TIMER(
1393 "Extensions.ExtensionServiceNotifyReadyListenersTime");
1394
[email protected]4a10006a2013-05-17 23:18:351395 ready_->Signal();
[email protected]820d9bd2013-04-03 03:46:031396 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061397 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED,
[email protected]820d9bd2013-04-03 03:46:031398 content::Source<Profile>(profile_),
1399 content::NotificationService::NoDetails());
1400}
1401
[email protected]eaa7dd182010-12-14 11:09:001402void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]3eeddd892013-04-17 17:00:111403 if (updater_)
[email protected]93fd78f42009-07-10 16:43:171404 updater_->Start();
[email protected]fa6a9102010-11-22 15:38:501405
elijahtaylor49c09c5c2016-07-09 01:06:221406 // Enable any Shared Modules that incorrectly got disabled previously.
1407 // This is temporary code to fix incorrect behavior from previous versions of
1408 // Chrome and can be removed after several releases (perhaps M60).
1409 extensions::ExtensionList to_enable;
1410 for (const auto& extension : registry_->disabled_extensions()) {
1411 if (SharedModuleInfo::IsSharedModule(extension.get()))
1412 to_enable.push_back(extension);
1413 }
1414 for (const auto& extension : to_enable) {
1415 EnableExtension(extension->id());
1416 }
1417
[email protected]bc151cf92013-02-12 04:57:261418 OnBlacklistUpdated();
[email protected]e72e8eb82009-06-18 17:21:511419}
1420
[email protected]fcb58a862012-05-01 01:03:151421void ExtensionService::AddExtension(const Extension* extension) {
[email protected]c8d407e2011-04-28 21:27:171422 // TODO(jstritar): We may be able to get rid of this branch by overriding the
1423 // default extension state to DISABLED when the --disable-extensions flag
1424 // is set (http://crbug.com/29067).
1425 if (!extensions_enabled() &&
1426 !extension->is_theme() &&
[email protected]1d5e58b2013-01-31 08:41:401427 extension->location() != Manifest::COMPONENT &&
1428 !Manifest::IsExternalLocation(extension->location())) {
[email protected]fcb58a862012-05-01 01:03:151429 return;
[email protected]330840c2012-08-29 22:21:011430 }
[email protected]c8d407e2011-04-28 21:27:171431
[email protected]b914e2952013-04-26 07:10:031432 bool is_extension_upgrade = false;
treib6e51bca2016-06-15 10:26:031433 bool is_extension_loaded = false;
[email protected]116d40e2013-08-08 17:23:171434 const Extension* old = GetInstalledExtension(extension->id());
1435 if (old) {
treib6e51bca2016-06-15 10:26:031436 is_extension_loaded = true;
[email protected]116d40e2013-08-08 17:23:171437 int version_compare_result =
1438 extension->version()->CompareTo(*(old->version()));
1439 is_extension_upgrade = version_compare_result > 0;
[email protected]b914e2952013-04-26 07:10:031440 // Other than for unpacked extensions, CrxInstaller should have guaranteed
1441 // that we aren't downgrading.
1442 if (!Manifest::IsUnpackedLocation(extension->location()))
[email protected]116d40e2013-08-08 17:23:171443 CHECK_GE(version_compare_result, 0);
[email protected]b914e2952013-04-26 07:10:031444 }
rdevlin.cronin2c16c602014-11-21 01:35:491445 // If the extension was disabled for a reload, then enable it.
1446 bool reloading = reloading_extensions_.erase(extension->id()) > 0;
1447
1448 // Set the upgraded bit; we consider reloads upgrades.
1449 system_->runtime_data()->SetBeingUpgraded(extension->id(),
1450 is_extension_upgrade || reloading);
[email protected]c8d407e2011-04-28 21:27:171451
[email protected]1eb175082010-02-10 09:26:161452 // The extension is now loaded, remove its data from unloaded extension map.
1453 unloaded_extension_paths_.erase(extension->id());
1454
[email protected]bb7f40952011-01-13 00:21:201455 // If a terminated extension is loaded, remove it from the terminated list.
1456 UntrackTerminatedExtension(extension->id());
1457
[email protected]b914e2952013-04-26 07:10:031458 // Check if the extension's privileges have changed and mark the
1459 // extension disabled if necessary.
treib6e51bca2016-06-15 10:26:031460 CheckPermissionsIncrease(extension, is_extension_loaded);
[email protected]b914e2952013-04-26 07:10:031461
treib6e51bca2016-06-15 10:26:031462 if (is_extension_loaded && !reloading) {
[email protected]ca2e82022013-04-29 16:48:411463 // To upgrade an extension in place, unload the old one and then load the
1464 // new one. ReloadExtension disables the extension, which is sufficient.
[email protected]b0af4792013-10-23 09:12:131465 UnloadExtension(extension->id(), UnloadedExtensionInfo::REASON_UPDATE);
[email protected]b914e2952013-04-26 07:10:031466 }
[email protected]0c6da502009-08-14 22:32:391467
[email protected]695b5712012-12-06 23:55:281468 if (extension_prefs_->IsExtensionBlacklisted(extension->id())) {
[email protected]ac875372013-02-28 04:36:091469 // Only prefs is checked for the blacklist. We rely on callers to check the
1470 // blacklist before calling into here, e.g. CrxInstaller checks before
[email protected]9f3c8532013-07-31 19:52:071471 // installation then threads through the install and pending install flow
1472 // of this class, and we check when loading installed extensions.
[email protected]bb1bc9b32013-12-21 03:09:141473 registry_->AddBlacklisted(extension);
mlerman6a37b6a42014-11-26 22:10:531474 } else if (block_extensions_ && CanBlockExtension(extension)) {
1475 registry_->AddBlocked(extension);
[email protected]ca2e82022013-04-29 16:48:411476 } else if (!reloading &&
1477 extension_prefs_->IsExtensionDisabled(extension->id())) {
[email protected]bb1bc9b32013-12-21 03:09:141478 registry_->AddDisabled(extension);
[email protected]44d62b62012-04-11 00:06:031479 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061480 extensions::NOTIFICATION_EXTENSION_UPDATE_DISABLED,
[email protected]44d62b62012-04-11 00:06:031481 content::Source<Profile>(profile_),
1482 content::Details<const Extension>(extension));
[email protected]62f051c2012-03-29 17:04:441483
[email protected]21db9ef2014-05-16 02:06:271484 // Show the extension disabled error if a permissions increase or a remote
1485 // installation is the reason it was disabled, and no other reasons exist.
1486 int reasons = extension_prefs_->GetDisableReasons(extension->id());
1487 const int kReasonMask = Extension::DISABLE_PERMISSIONS_INCREASE |
1488 Extension::DISABLE_REMOTE_INSTALL;
1489 if (reasons & kReasonMask && !(reasons & ~kReasonMask)) {
1490 extensions::AddExtensionDisabledError(
1491 this,
1492 extension,
1493 extension_prefs_->HasDisableReason(
1494 extension->id(), Extension::DISABLE_REMOTE_INSTALL));
[email protected]62f051c2012-03-29 17:04:441495 }
[email protected]ca2e82022013-04-29 16:48:411496 } else if (reloading) {
1497 // Replace the old extension with the new version.
[email protected]bb1bc9b32013-12-21 03:09:141498 CHECK(!registry_->AddDisabled(extension));
[email protected]ca2e82022013-04-29 16:48:411499 EnableExtension(extension->id());
[email protected]695b5712012-12-06 23:55:281500 } else {
1501 // All apps that are displayed in the launcher are ordered by their ordinals
1502 // so we must ensure they have valid ordinals.
1503 if (extension->RequiresSortOrdinal()) {
deepak.m14ba69e62015-11-17 05:42:121504 AppSorting* app_sorting =
1505 extensions::ExtensionSystem::Get(GetBrowserContext())->app_sorting();
1506 app_sorting->SetExtensionVisible(
[email protected]db378322014-07-21 23:19:341507 extension->id(),
benwells1dd4acd2015-12-09 02:20:241508 extension->ShouldDisplayInNewTabPage());
1509 app_sorting->EnsureValidOrdinals(extension->id(),
1510 syncer::StringOrdinal());
[email protected]695b5712012-12-06 23:55:281511 }
1512
[email protected]bb1bc9b32013-12-21 03:09:141513 registry_->AddEnabled(extension);
[email protected]695b5712012-12-06 23:55:281514 NotifyExtensionLoaded(extension);
[email protected]f574c402012-12-04 23:20:311515 }
rdevlin.cronin2c16c602014-11-21 01:35:491516 system_->runtime_data()->SetBeingUpgraded(extension->id(), false);
[email protected]aab98a52009-12-02 03:22:351517}
1518
[email protected]8c484b742012-11-29 06:05:361519void ExtensionService::AddComponentExtension(const Extension* extension) {
1520 const std::string old_version_string(
1521 extension_prefs_->GetVersionString(extension->id()));
pwnallcbd73192016-08-22 18:59:171522 const base::Version old_version(old_version_string);
[email protected]8c484b742012-11-29 06:05:361523
[email protected]0181a7d2013-11-12 01:08:421524 VLOG(1) << "AddComponentExtension " << extension->name();
robpercivaldcd8b102016-01-25 19:39:001525 if (!old_version.IsValid() || old_version != *extension->version()) {
[email protected]8c484b742012-11-29 06:05:361526 VLOG(1) << "Component extension " << extension->name() << " ("
1527 << extension->id() << ") installing/upgrading from '"
1528 << old_version_string << "' to " << extension->version()->GetString();
1529
1530 AddNewOrUpdatedExtension(extension,
[email protected]9ae73962014-08-14 16:53:411531 Extension::ENABLED,
[email protected]4a1d9c0d2014-06-13 12:50:111532 extensions::kInstallFlagNone,
[email protected]d8fd0fd2014-03-24 13:16:061533 syncer::StringOrdinal(),
1534 std::string());
[email protected]8c484b742012-11-29 06:05:361535 return;
1536 }
1537
1538 AddExtension(extension);
1539}
1540
[email protected]b914e2952013-04-26 07:10:031541void ExtensionService::CheckPermissionsIncrease(const Extension* extension,
treib6e51bca2016-06-15 10:26:031542 bool is_extension_loaded) {
[email protected]23a85362014-07-07 23:26:191543 extensions::PermissionsUpdater(profile_).InitializePermissions(extension);
[email protected]902fd7b2011-07-27 18:42:311544
[email protected]8d888c12010-11-30 00:00:251545 // We keep track of all permissions the user has granted each extension.
1546 // This allows extensions to gracefully support backwards compatibility
1547 // by including unknown permissions in their manifests. When the user
1548 // installs the extension, only the recognized permissions are recorded.
1549 // When the unknown permissions become recognized (e.g., through browser
1550 // upgrade), we can prompt the user to accept these new permissions.
1551 // Extensions can also silently upgrade to less permissions, and then
1552 // silently upgrade to a version that adds these permissions back.
1553 //
1554 // For example, pretend that Chrome 10 includes a permission "omnibox"
1555 // for an API that adds suggestions to the omnibox. An extension can
1556 // maintain backwards compatibility while still having "omnibox" in the
1557 // manifest. If a user installs the extension on Chrome 9, the browser
1558 // will record the permissions it recognized, not including "omnibox."
1559 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
1560 // will disable the extension and prompt the user to approve the increase
1561 // in privileges. The extension could then release a new version that
1562 // removes the "omnibox" permission. When the user upgrades, Chrome will
1563 // still remember that "omnibox" had been granted, so that if the
1564 // extension once again includes "omnibox" in an upgrade, the extension
1565 // can upgrade without requiring this user's approval.
[email protected]eb5e4f92012-08-15 23:33:281566 int disable_reasons = extension_prefs_->GetDisableReasons(extension->id());
[email protected]8d888c12010-11-30 00:00:251567
rockot1d4a06262015-02-17 19:39:281568 // Silently grant all active permissions to default apps and apps installed
1569 // in kiosk mode.
[email protected]211a3f32013-05-28 21:48:181570 bool auto_grant_permission =
rockot1d4a06262015-02-17 19:39:281571 extension->was_installed_by_default() ||
[email protected]45928b682013-11-28 08:20:271572 extensions::ExtensionsBrowserClient::Get()->IsRunningInForcedAppMode();
[email protected]211a3f32013-05-28 21:48:181573 if (auto_grant_permission)
[email protected]b914e2952013-04-26 07:10:031574 GrantPermissions(extension);
1575
1576 bool is_privilege_increase = false;
1577 // We only need to compare the granted permissions to the current permissions
rockot1d4a06262015-02-17 19:39:281578 // if the extension has not been auto-granted its permissions above and is
1579 // installed internally.
1580 if (extension->location() == Manifest::INTERNAL && !auto_grant_permission) {
[email protected]8d888c12010-11-30 00:00:251581 // Add all the recognized permissions if the granted permissions list
1582 // hasn't been initialized yet.
dchengc963c7142016-04-08 03:55:221583 std::unique_ptr<const PermissionSet> granted_permissions =
[email protected]902fd7b2011-07-27 18:42:311584 extension_prefs_->GetGrantedPermissions(extension->id());
[email protected]0d3e4a22011-06-23 19:02:521585 CHECK(granted_permissions.get());
[email protected]8d888c12010-11-30 00:00:251586
1587 // Here, we check if an extension's privileges have increased in a manner
1588 // that requires the user's approval. This could occur because the browser
1589 // upgraded and recognized additional privileges, or an extension upgrades
1590 // to a version that requires additional privileges.
[email protected]c41003472013-10-19 15:37:251591 is_privilege_increase =
1592 extensions::PermissionMessageProvider::Get()->IsPrivilegeIncrease(
rdevlin.cronind630c302015-09-30 20:19:331593 *granted_permissions,
rdevlin.cronine2d0fd02015-09-24 22:35:491594 extension->permissions_data()->active_permissions(),
[email protected]076ebeda2014-06-06 21:47:261595 extension->GetType());
treib2c20f9442016-05-18 19:50:281596
1597 // If there was no privilege increase, the extension might still have new
1598 // permissions (which either don't generate a warning message, or whose
1599 // warning messages are suppressed by existing permissions). Grant the new
1600 // permissions.
1601 if (!is_privilege_increase)
1602 GrantPermissions(extension);
[email protected]8d888c12010-11-30 00:00:251603 }
1604
treib6e51bca2016-06-15 10:26:031605 bool previously_disabled =
1606 extension_prefs_->IsExtensionDisabled(extension->id());
1607 // TODO(treib): Is the |is_extension_loaded| check needed here?
1608 if (is_extension_loaded && previously_disabled) {
treib8a6d9892015-08-26 10:23:191609 // Legacy disabled extensions do not have a disable reason. Infer that it
1610 // was likely disabled by the user.
treib6e51bca2016-06-15 10:26:031611 if (disable_reasons == Extension::DISABLE_NONE)
[email protected]7e9e2422012-12-13 19:54:191612 disable_reasons |= Extension::DISABLE_USER_ACTION;
treib8a6d9892015-08-26 10:23:191613
[email protected]7e9e2422012-12-13 19:54:191614 // Extensions that came to us disabled from sync need a similar inference,
1615 // except based on the new version's permissions.
treib3b91e9f2015-11-04 11:29:411616 // TODO(treib,devlin): Since M48, DISABLE_UNKNOWN_FROM_SYNC isn't used
1617 // anymore; this code is still here to migrate any existing old state.
1618 // Remove it after some grace period.
treib6e51bca2016-06-15 10:26:031619 if (disable_reasons & Extension::DEPRECATED_DISABLE_UNKNOWN_FROM_SYNC) {
[email protected]7e9e2422012-12-13 19:54:191620 // Remove the DISABLE_UNKNOWN_FROM_SYNC reason.
treib3b91e9f2015-11-04 11:29:411621 disable_reasons &= ~Extension::DEPRECATED_DISABLE_UNKNOWN_FROM_SYNC;
treib8a6d9892015-08-26 10:23:191622 extension_prefs_->RemoveDisableReason(
treib3b91e9f2015-11-04 11:29:411623 extension->id(), Extension::DEPRECATED_DISABLE_UNKNOWN_FROM_SYNC);
treib8a6d9892015-08-26 10:23:191624 // If there was no privilege increase, it was likely disabled by the user.
[email protected]7e9e2422012-12-13 19:54:191625 if (!is_privilege_increase)
1626 disable_reasons |= Extension::DISABLE_USER_ACTION;
1627 }
[email protected]8d888c12010-11-30 00:00:251628 }
1629
treib6e51bca2016-06-15 10:26:031630 // If the extension is disabled due to a permissions increase, but does in
1631 // fact have all permissions, remove that disable reason.
1632 // TODO(devlin): This was added to fix crbug.com/616474, but it's unclear
1633 // if this behavior should stay forever.
1634 if (disable_reasons & Extension::DISABLE_PERMISSIONS_INCREASE) {
1635 bool reset_permissions_increase = false;
1636 if (!is_privilege_increase) {
1637 reset_permissions_increase = true;
1638 disable_reasons &= ~Extension::DISABLE_PERMISSIONS_INCREASE;
1639 extension_prefs_->RemoveDisableReason(
1640 extension->id(), Extension::DISABLE_PERMISSIONS_INCREASE);
1641 }
1642 UMA_HISTOGRAM_BOOLEAN("Extensions.ResetPermissionsIncrease",
1643 reset_permissions_increase);
1644 }
1645
[email protected]8d888c12010-11-30 00:00:251646 // Extension has changed permissions significantly. Disable it. A
[email protected]21db9ef2014-05-16 02:06:271647 // notification should be sent by the caller. If the extension is already
1648 // disabled because it was installed remotely, don't add another disable
treibaac30ec2015-06-10 09:18:091649 // reason.
1650 if (is_privilege_increase &&
treib8a6d9892015-08-26 10:23:191651 !(disable_reasons & Extension::DISABLE_REMOTE_INSTALL)) {
[email protected]7e9e2422012-12-13 19:54:191652 disable_reasons |= Extension::DISABLE_PERMISSIONS_INCREASE;
treib8a6d9892015-08-26 10:23:191653 if (!extension_prefs_->DidExtensionEscalatePermissions(extension->id()))
treib2e0517f92015-05-07 23:15:361654 RecordPermissionMessagesHistogram(extension, "AutoDisable");
treib8ecc1eb52015-03-04 18:29:061655
1656#if defined(ENABLE_SUPERVISED_USERS)
1657 // If a custodian-installed extension is disabled for a supervised user due
treib3202d592015-07-31 08:33:161658 // to a permissions increase, send a request to the custodian if the
treibf832a992015-03-24 18:09:241659 // supervised user themselves can't re-enable the extension.
treib3202d592015-07-31 08:33:161660 if (extensions::util::IsExtensionSupervised(extension, profile_) &&
treibb6af28cd2015-12-01 11:19:461661 extensions::util::NeedCustodianApprovalForPermissionIncrease(
1662 profile_) &&
treib40d3ad92015-10-20 18:15:421663 !ExtensionSyncService::Get(profile_)->HasPendingReenable(
1664 extension->id(), *extension->version())) {
treib8ecc1eb52015-03-04 18:29:061665 SupervisedUserService* supervised_user_service =
1666 SupervisedUserServiceFactory::GetForProfile(profile_);
treib40d3ad92015-10-20 18:15:421667 supervised_user_service->AddExtensionUpdateRequest(extension->id(),
1668 *extension->version());
treib8ecc1eb52015-03-04 18:29:061669 }
1670#endif
[email protected]b914e2952013-04-26 07:10:031671 }
treib6e51bca2016-06-15 10:26:031672
1673 if (disable_reasons == Extension::DISABLE_NONE)
1674 extension_prefs_->SetExtensionEnabled(extension->id());
1675 else
treib8a6d9892015-08-26 10:23:191676 extension_prefs_->SetExtensionDisabled(extension->id(), disable_reasons);
[email protected]8d888c12010-11-30 00:00:251677}
1678
[email protected]eaa7dd182010-12-14 11:09:001679void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:391680 std::set<std::string> extension_ids;
achuith2f5578b2016-02-26 21:57:131681 for (const auto& extension : registry_->enabled_extensions()) {
[email protected]1d5e58b2013-01-31 08:41:401682 if (!extension->is_theme() && extension->location() != Manifest::COMPONENT)
[email protected]84df8332011-12-06 18:22:461683 extension_ids.insert(extension->id());
[email protected]aab98a52009-12-02 03:22:351684 }
1685
[email protected]dcc47642014-03-26 22:03:491686 // TODO(kalman): This is broken. ExtensionService is per-profile.
1687 // crash_keys::SetActiveExtensions is per-process. See
1688 // http://crbug.com/355029.
[email protected]f6431be82013-09-07 02:53:451689 crash_keys::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:251690}
1691
[email protected]8266d662011-07-12 21:53:261692void ExtensionService::OnExtensionInstalled(
[email protected]36a5c4c2011-12-14 16:34:501693 const Extension* extension,
[email protected]98270432012-09-11 20:51:241694 const syncer::StringOrdinal& page_ordinal,
[email protected]4a1d9c0d2014-06-13 12:50:111695 int install_flags) {
[email protected]a29a517a2011-01-21 21:11:121696 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:201697
[email protected]c3cfb012011-04-06 22:07:351698 const std::string& id = extension->id();
binjin47947f842014-11-18 12:10:241699 int disable_reasons = GetDisableReasonsOnInstalled(extension);
[email protected]d8fd0fd2014-03-24 13:16:061700 std::string install_parameter;
[email protected]96aebe22014-07-16 04:07:511701 const extensions::PendingExtensionInfo* pending_extension_info =
1702 pending_extension_manager()->GetById(id);
1703 if (pending_extension_info) {
[email protected]8f3bcbd2013-06-05 08:42:401704 if (!pending_extension_info->ShouldAllowInstall(extension)) {
treib227b2582015-12-09 09:28:261705 // Hack for crbug.com/558299, see comment on DeleteThemeDoNotUse.
1706 if (extension->is_theme() && pending_extension_info->is_from_sync())
1707 ExtensionSyncService::Get(profile_)->DeleteThemeDoNotUse(*extension);
1708
[email protected]51a3bf8b2012-06-08 22:53:061709 pending_extension_manager()->Remove(id);
[email protected]8ef78fd2010-08-19 17:14:321710
[email protected]31bb5ee62012-09-12 22:58:401711 LOG(WARNING) << "ShouldAllowInstall() returned false for "
1712 << id << " of type " << extension->GetType()
[email protected]65348062013-01-15 07:27:221713 << " and update URL "
1714 << extensions::ManifestURL::GetUpdateURL(extension).spec()
[email protected]31bb5ee62012-09-12 22:58:401715 << "; not installing";
[email protected]ae6cb3022011-04-04 20:11:541716
[email protected]4416c5a2010-06-26 01:28:571717 // Delete the extension directory since we're not going to
1718 // load it.
[email protected]7f8f24f2012-11-15 19:40:141719 if (!GetFileTaskRunner()->PostTask(
1720 FROM_HERE,
[email protected]85df9d12014-04-15 17:02:141721 base::Bind(&extensions::file_util::DeleteFile,
1722 extension->path(),
1723 true))) {
[email protected]14908b72011-04-20 06:54:361724 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:401725 }
[email protected]4416c5a2010-06-26 01:28:571726 return;
1727 }
[email protected]51a3bf8b2012-06-08 22:53:061728
[email protected]d8fd0fd2014-03-24 13:16:061729 install_parameter = pending_extension_info->install_parameter();
[email protected]51a3bf8b2012-06-08 22:53:061730 pending_extension_manager()->Remove(id);
[email protected]4416c5a2010-06-26 01:28:571731 } else {
[email protected]c3cfb012011-04-06 22:07:351732 // We explicitly want to re-enable an uninstalled external
1733 // extension; if we're here, that means the user is manually
1734 // installing the extension.
[email protected]46f3e102014-03-25 01:22:451735 if (extension_prefs_->IsExternalExtensionUninstalled(id)) {
binjin47947f842014-11-18 12:10:241736 disable_reasons = Extension::DISABLE_NONE;
[email protected]6cc7dbae2011-04-29 21:18:331737 }
[email protected]aa142702010-03-26 01:26:331738 }
1739
[email protected]98270432012-09-11 20:51:241740 // Unsupported requirements overrides the management policy.
[email protected]4a1d9c0d2014-06-13 12:50:111741 if (install_flags & extensions::kInstallFlagHasRequirementErrors) {
binjin47947f842014-11-18 12:10:241742 disable_reasons |= Extension::DISABLE_UNSUPPORTED_REQUIREMENT;
binjin8e3d0182014-12-04 16:44:281743 } else {
1744 // Requirement is supported now, remove the corresponding disable reason
1745 // instead.
binjin8e3d0182014-12-04 16:44:281746 disable_reasons &= ~Extension::DISABLE_UNSUPPORTED_REQUIREMENT;
1747 }
1748
1749 // Check if the extension was disabled because of the minimum version
1750 // requirements from enterprise policy, and satisfies it now.
1751 if (extensions::ExtensionManagementFactory::GetForBrowserContext(profile())
1752 ->CheckMinimumVersion(extension, nullptr)) {
1753 // And remove the corresponding disable reason.
binjin8e3d0182014-12-04 16:44:281754 disable_reasons &= ~Extension::DISABLE_UPDATE_REQUIRED_BY_POLICY;
[email protected]98270432012-09-11 20:51:241755 }
1756
[email protected]4a1d9c0d2014-06-13 12:50:111757 if (install_flags & extensions::kInstallFlagIsBlacklistedForMalware) {
[email protected]9f3c8532013-07-31 19:52:071758 // Installation of a blacklisted extension can happen from sync, policy,
1759 // etc, where to maintain consistency we need to install it, just never
1760 // load it (see AddExtension). Usually it should be the job of callers to
treibaac30ec2015-06-10 09:18:091761 // intercept blacklisted extensions earlier (e.g. CrxInstaller, before even
[email protected]9f3c8532013-07-31 19:52:071762 // showing the install dialogue).
[email protected]7c82539c2014-02-19 06:09:171763 extension_prefs_->AcknowledgeBlacklistedExtension(id);
[email protected]9f3c8532013-07-31 19:52:071764 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.SilentInstall",
1765 extension->location(),
1766 Manifest::NUM_LOCATIONS);
1767 }
1768
[email protected]695b5712012-12-06 23:55:281769 if (!GetInstalledExtension(extension->id())) {
[email protected]07533022011-06-27 20:42:551770 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
1771 extension->GetType(), 100);
[email protected]69084dc2012-11-17 07:39:301772 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallSource",
[email protected]1d5e58b2013-01-31 08:41:401773 extension->location(), Manifest::NUM_LOCATIONS);
treib2e0517f92015-05-07 23:15:361774 RecordPermissionMessagesHistogram(extension, "Install");
[email protected]69084dc2012-11-17 07:39:301775 } else {
1776 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateType",
1777 extension->GetType(), 100);
1778 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateSource",
[email protected]1d5e58b2013-01-31 08:41:401779 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]07533022011-06-27 20:42:551780 }
1781
[email protected]399583b2012-12-11 09:33:421782 const Extension::State initial_state =
binjin47947f842014-11-18 12:10:241783 disable_reasons == Extension::DISABLE_NONE ? Extension::ENABLED
1784 : Extension::DISABLED;
treib8a6d9892015-08-26 10:23:191785 if (initial_state == Extension::ENABLED)
1786 extension_prefs_->SetExtensionEnabled(id);
1787 else
1788 extension_prefs_->SetExtensionDisabled(id, disable_reasons);
binjin47947f842014-11-18 12:10:241789
xiyuanf6a4c6a62016-04-19 18:14:541790 extensions::ExtensionPrefs::DelayReason delay_reason;
1791 extensions::InstallGate::Action action = ShouldDelayExtensionInstall(
1792 extension, !!(install_flags & extensions::kInstallFlagInstallImmediately),
1793 &delay_reason);
1794 switch (action) {
1795 case extensions::InstallGate::INSTALL:
1796 AddNewOrUpdatedExtension(extension, initial_state, install_flags,
1797 page_ordinal, install_parameter);
1798 return;
1799 case extensions::InstallGate::DELAY:
1800 extension_prefs_->SetDelayedInstallInfo(extension, initial_state,
1801 install_flags, delay_reason,
1802 page_ordinal, install_parameter);
[email protected]0db124b02012-11-07 04:55:051803
xiyuanf6a4c6a62016-04-19 18:14:541804 // Transfer ownership of |extension|.
[email protected]9f4e4f082013-06-21 07:11:191805 delayed_installs_.Insert(extension);
xiyuanf6a4c6a62016-04-19 18:14:541806
1807 if (delay_reason ==
1808 extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE) {
1809 // Notify observers that app update is available.
1810 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
1811 OnAppUpdateAvailable(extension));
1812 }
1813 return;
1814 case extensions::InstallGate::ABORT:
1815 // Do nothing to abort the install. One such case is the shared module
1816 // service gets IMPORT_STATUS_UNRECOVERABLE status for the pending
1817 // install.
1818 return;
[email protected]399583b2012-12-11 09:33:421819 }
xiyuanf6a4c6a62016-04-19 18:14:541820
1821 NOTREACHED() << "Unknown action for delayed install: " << action;
[email protected]8c484b742012-11-29 06:05:361822}
1823
binjin1569c9b2014-09-05 13:33:181824void ExtensionService::OnExtensionManagementSettingsChanged() {
1825 error_controller_->ShowErrorIfNeeded();
binjine6b58b52014-10-31 01:55:571826
1827 // Revokes blocked permissions from active_permissions for all extensions.
1828 extensions::ExtensionManagement* settings =
1829 extensions::ExtensionManagementFactory::GetForBrowserContext(profile());
1830 CHECK(settings);
dchengc963c7142016-04-08 03:55:221831 std::unique_ptr<ExtensionSet> all_extensions(
binjine6b58b52014-10-31 01:55:571832 registry_->GenerateInstalledExtensionsSet());
achuith2f5578b2016-02-26 21:57:131833 for (const auto& extension : *all_extensions) {
binjine6b58b52014-10-31 01:55:571834 if (!settings->IsPermissionSetAllowed(
binjin685ade82014-11-06 09:53:561835 extension.get(),
rdevlin.cronind630c302015-09-30 20:19:331836 extension->permissions_data()->active_permissions())) {
rdevlin.cronin77cb0ef2015-09-16 17:03:481837 extensions::PermissionsUpdater(profile()).RemovePermissionsUnsafe(
rdevlin.cronind630c302015-09-30 20:19:331838 extension.get(), *settings->GetBlockedPermissions(extension.get()));
binjine6b58b52014-10-31 01:55:571839 }
1840 }
1841
binjin1569c9b2014-09-05 13:33:181842 CheckManagementPolicy();
1843}
1844
[email protected]8c484b742012-11-29 06:05:361845void ExtensionService::AddNewOrUpdatedExtension(
1846 const Extension* extension,
[email protected]399583b2012-12-11 09:33:421847 Extension::State initial_state,
[email protected]4a1d9c0d2014-06-13 12:50:111848 int install_flags,
[email protected]d8fd0fd2014-03-24 13:16:061849 const syncer::StringOrdinal& page_ordinal,
1850 const std::string& install_parameter) {
[email protected]8c484b742012-11-29 06:05:361851 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]4a1d9c0d2014-06-13 12:50:111852 extension_prefs_->OnExtensionInstalled(
1853 extension, initial_state, page_ordinal, install_flags, install_parameter);
[email protected]9e9c1d12013-07-31 01:58:121854 delayed_installs_.Remove(extension->id());
[email protected]913ffca92014-03-27 15:26:161855 if (InstallVerifier::NeedsVerification(*extension))
juncai33e462102015-05-18 20:48:441856 InstallVerifier::Get(GetBrowserContext())->VerifyExtension(extension->id());
ryanackley48bedbd2015-01-27 23:12:141857
1858 const Extension* old = GetInstalledExtension(extension->id());
1859 if (extensions::AppDataMigrator::NeedsMigration(old, extension)) {
1860 app_data_migrator_->DoMigrationAndReply(
vmpstra34d11322016-03-21 20:28:471861 old, extension, base::Bind(&ExtensionService::FinishInstallation,
1862 AsWeakPtr(), base::RetainedRef(extension)));
ryanackley48bedbd2015-01-27 23:12:141863 return;
1864 }
1865
benwells1dd4acd2015-12-09 02:20:241866 FinishInstallation(extension);
[email protected]6f6101832012-11-27 22:10:481867}
1868
1869void ExtensionService::MaybeFinishDelayedInstallation(
1870 const std::string& extension_id) {
[email protected]9f4e4f082013-06-21 07:11:191871 // Check if the extension already got installed.
[email protected]9f4e4f082013-06-21 07:11:191872 const Extension* extension = delayed_installs_.GetByID(extension_id);
xiyuanf6a4c6a62016-04-19 18:14:541873 if (!extension)
1874 return;
1875
1876 extensions::ExtensionPrefs::DelayReason reason;
1877 const extensions::InstallGate::Action action = ShouldDelayExtensionInstall(
1878 extension, false /* install_immediately*/, &reason);
1879 switch (action) {
1880 case extensions::InstallGate::INSTALL:
1881 break;
1882 case extensions::InstallGate::DELAY:
1883 // Bail out and continue to delay the install.
[email protected]9f4e4f082013-06-21 07:11:191884 return;
xiyuanf6a4c6a62016-04-19 18:14:541885 case extensions::InstallGate::ABORT:
1886 delayed_installs_.Remove(extension_id);
1887 // Make sure no version of the extension is actually installed, (i.e.,
1888 // that this delayed install was not an update).
1889 CHECK(!extension_prefs_->GetInstalledExtensionInfo(extension_id).get());
1890 extension_prefs_->DeleteExtensionPrefs(extension_id);
1891 return;
[email protected]9f4e4f082013-06-21 07:11:191892 }
1893
[email protected]6f6101832012-11-27 22:10:481894 FinishDelayedInstallation(extension_id);
1895}
1896
1897void ExtensionService::FinishDelayedInstallation(
1898 const std::string& extension_id) {
1899 scoped_refptr<const Extension> extension(
1900 GetPendingExtensionUpdate(extension_id));
[email protected]dc24976f2013-06-02 21:15:091901 CHECK(extension.get());
[email protected]9f4e4f082013-06-21 07:11:191902 delayed_installs_.Remove(extension_id);
[email protected]6f6101832012-11-27 22:10:481903
[email protected]399583b2012-12-11 09:33:421904 if (!extension_prefs_->FinishDelayedInstallInfo(extension_id))
[email protected]6f6101832012-11-27 22:10:481905 NOTREACHED();
1906
benwells1dd4acd2015-12-09 02:20:241907 FinishInstallation(extension.get());
[email protected]6f6101832012-11-27 22:10:481908}
1909
[email protected]760f743b2014-05-28 13:52:021910void ExtensionService::FinishInstallation(
benwells1dd4acd2015-12-09 02:20:241911 const Extension* extension) {
[email protected]e7554c3f2013-05-29 00:36:561912 const extensions::Extension* existing_extension =
1913 GetInstalledExtension(extension->id());
1914 bool is_update = false;
1915 std::string old_name;
1916 if (existing_extension) {
1917 is_update = true;
1918 old_name = existing_extension->name();
1919 }
[email protected]bc44b5da2014-06-12 14:20:001920 registry_->TriggerOnWillBeInstalled(
benwells1dd4acd2015-12-09 02:20:241921 extension, is_update, old_name);
[email protected]17f07822014-05-22 08:45:151922
[email protected]399583b2012-12-11 09:33:421923 // Unpacked extensions default to allowing file access, but if that has been
1924 // overridden, don't reset the value.
[email protected]1d5e58b2013-01-31 08:41:401925 if (Manifest::ShouldAlwaysAllowFileAccess(extension->location()) &&
[email protected]399583b2012-12-11 09:33:421926 !extension_prefs_->HasAllowFileAccessSetting(extension->id())) {
1927 extension_prefs_->SetAllowFileAccess(extension->id(), true);
1928 }
1929
[email protected]6f6101832012-11-27 22:10:481930 AddExtension(extension);
[email protected]612a1cb12012-10-17 13:18:031931
[email protected]bc44b5da2014-06-12 14:20:001932 // Notify observers that need to know when an installation is complete.
[email protected]38e872532014-07-16 23:27:511933 registry_->TriggerOnInstalled(extension, is_update);
[email protected]bc44b5da2014-06-12 14:20:001934
[email protected]9f4e4f082013-06-21 07:11:191935 // Check extensions that may have been delayed only because this shared module
1936 // was not available.
[email protected]374ceb6f2014-07-02 19:25:341937 if (SharedModuleInfo::IsSharedModule(extension))
[email protected]9f4e4f082013-06-21 07:11:191938 MaybeFinishDelayedInstallations();
[email protected]4a190632009-05-09 01:07:421939}
1940
[email protected]76b65442012-11-17 14:11:481941const Extension* ExtensionService::GetPendingExtensionUpdate(
1942 const std::string& id) const {
[email protected]9f4e4f082013-06-21 07:11:191943 return delayed_installs_.GetByID(id);
[email protected]76b65442012-11-17 14:11:481944}
1945
mukai87a8d402014-09-15 20:15:091946void ExtensionService::RegisterContentSettings(
1947 HostContentSettingsMap* host_content_settings_map) {
rkaplowe65c2ff2015-02-14 16:29:541948 TRACE_EVENT0("browser,startup", "ExtensionService::RegisterContentSettings");
mukaia1aa3512014-10-22 20:14:111949 DCHECK_CURRENTLY_ON(BrowserThread::UI);
mukai87a8d402014-09-15 20:15:091950 host_content_settings_map->RegisterProvider(
1951 HostContentSettingsMap::INTERNAL_EXTENSION_PROVIDER,
dchengc963c7142016-04-08 03:55:221952 std::unique_ptr<content_settings::ObservableProvider>(
reillyga3acbc12014-11-11 23:17:121953 new content_settings::InternalExtensionProvider(profile_)));
mukai87a8d402014-09-15 20:15:091954
1955 host_content_settings_map->RegisterProvider(
1956 HostContentSettingsMap::CUSTOM_EXTENSION_PROVIDER,
dchengc963c7142016-04-08 03:55:221957 std::unique_ptr<content_settings::ObservableProvider>(
mukai87a8d402014-09-15 20:15:091958 new content_settings::CustomExtensionProvider(
dchengc963c7142016-04-08 03:55:221959 extensions::ContentSettingsService::Get(profile_)
1960 ->content_settings_store(),
mukai87a8d402014-09-15 20:15:091961 profile_->GetOriginalProfile() != profile_)));
1962}
1963
xiyuane87def2e2015-06-04 22:50:221964void ExtensionService::TrackTerminatedExtension(
1965 const std::string& extension_id) {
1966 extensions_being_terminated_.erase(extension_id);
1967
1968 const Extension* extension = GetInstalledExtension(extension_id);
1969 if (!extension) {
xiyuane87def2e2015-06-04 22:50:221970 return;
1971 }
1972
[email protected]bb1bc9b32013-12-21 03:09:141973 // No need to check for duplicates; inserting a duplicate is a no-op.
1974 registry_->AddTerminated(make_scoped_refptr(extension));
[email protected]b0af4792013-10-23 09:12:131975 UnloadExtension(extension->id(), UnloadedExtensionInfo::REASON_TERMINATE);
[email protected]bb7f40952011-01-13 00:21:201976}
1977
[email protected]e9d7496e2014-04-18 01:25:461978void ExtensionService::TerminateExtension(const std::string& extension_id) {
xiyuana30a2092015-06-09 23:14:441979 TrackTerminatedExtension(extension_id);
[email protected]e9d7496e2014-04-18 01:25:461980}
1981
[email protected]bb7f40952011-01-13 00:21:201982void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
brettwfce8d192015-08-10 19:07:511983 std::string lowercase_id = base::ToLowerASCII(id);
[email protected]bb1bc9b32013-12-21 03:09:141984 const Extension* extension =
1985 registry_->terminated_extensions().GetByID(lowercase_id);
1986 registry_->RemoveTerminated(lowercase_id);
[email protected]fb789532013-08-27 02:40:211987 if (extension) {
1988 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061989 extensions::NOTIFICATION_EXTENSION_REMOVED,
[email protected]fb789532013-08-27 02:40:211990 content::Source<Profile>(profile_),
1991 content::Details<const Extension>(extension));
1992 }
[email protected]bb7f40952011-01-13 00:21:201993}
1994
[email protected]8001df22011-04-28 19:59:471995const Extension* ExtensionService::GetInstalledExtension(
1996 const std::string& id) const {
[email protected]599539802014-01-07 23:06:001997 return registry_->GetExtensionById(id, ExtensionRegistry::EVERYTHING);
[email protected]0dfe05c2011-02-23 23:03:361998}
1999
[email protected]9060d8b02012-01-13 02:14:302000bool ExtensionService::OnExternalExtensionFileFound(
lazyboye8634172016-01-28 00:10:482001 const ExternalInstallInfoFile& info) {
[email protected]ab22ba42011-01-14 16:36:382002 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
lazyboye8634172016-01-28 00:10:482003 CHECK(crx_file::id_util::IdIsValid(info.extension_id));
2004 if (extension_prefs_->IsExternalExtensionUninstalled(info.extension_id))
[email protected]9060d8b02012-01-13 02:14:302005 return false;
[email protected]a8af9fdb2010-10-28 21:52:202006
[email protected]7577a5c52009-07-30 06:21:582007 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:492008 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:582009 // installed on every startup.
lazyboye8634172016-01-28 00:10:482010 const Extension* existing = GetExtensionById(info.extension_id, true);
[email protected]910f72ce2012-08-24 01:38:352011
[email protected]7577a5c52009-07-30 06:21:582012 if (existing) {
[email protected]910f72ce2012-08-24 01:38:352013 // The default apps will have the location set as INTERNAL. Since older
2014 // default apps are installed as EXTERNAL, we override them. However, if the
2015 // app is already installed as internal, then do the version check.
[email protected]31bb5ee62012-09-12 22:58:402016 // TODO(grv) : Remove after Q1-2013.
[email protected]910f72ce2012-08-24 01:38:352017 bool is_default_apps_migration =
lazyboye8634172016-01-28 00:10:482018 (info.crx_location == Manifest::INTERNAL &&
[email protected]1d5e58b2013-01-31 08:41:402019 Manifest::IsExternalLocation(existing->location()));
[email protected]910f72ce2012-08-24 01:38:352020
2021 if (!is_default_apps_migration) {
lazyboye8634172016-01-28 00:10:482022 DCHECK(info.version.get());
[email protected]910f72ce2012-08-24 01:38:352023
lazyboye8634172016-01-28 00:10:482024 switch (existing->version()->CompareTo(*info.version)) {
[email protected]910f72ce2012-08-24 01:38:352025 case -1: // existing version is older, we should upgrade
2026 break;
2027 case 0: // existing version is same, do nothing
2028 return false;
2029 case 1: // existing version is newer, uh-oh
lazyboye8634172016-01-28 00:10:482030 LOG(WARNING) << "Found external version of extension "
2031 << info.extension_id
[email protected]910f72ce2012-08-24 01:38:352032 << "that is older than current version. Current version "
2033 << "is: " << existing->VersionString() << ". New "
lazyboye8634172016-01-28 00:10:482034 << "version is: " << info.version->GetString()
[email protected]910f72ce2012-08-24 01:38:352035 << ". Keeping current version.";
2036 return false;
2037 }
[email protected]7577a5c52009-07-30 06:21:582038 }
2039 }
2040
[email protected]9060d8b02012-01-13 02:14:302041 // If the extension is already pending, don't start an install.
[email protected]e3987852012-05-04 10:06:302042 if (!pending_extension_manager()->AddFromExternalFile(
lazyboye8634172016-01-28 00:10:482043 info.extension_id, info.crx_location, *info.version,
2044 info.creation_flags, info.mark_acknowledged)) {
[email protected]9060d8b02012-01-13 02:14:302045 return false;
[email protected]e3987852012-05-04 10:06:302046 }
[email protected]9c635f22010-12-02 09:36:362047
[email protected]14908b72011-04-20 06:54:362048 // no client (silent install)
[email protected]f8636f92013-08-09 21:02:372049 scoped_refptr<CrxInstaller> installer(CrxInstaller::CreateSilent(this));
lazyboye8634172016-01-28 00:10:482050 installer->set_install_source(info.crx_location);
2051 installer->set_expected_id(info.extension_id);
2052 installer->set_expected_version(*info.version,
treibe960e282015-09-11 10:38:082053 true /* fail_install_if_unexpected */);
[email protected]cb0e50312011-05-09 15:03:072054 installer->set_install_cause(extension_misc::INSTALL_CAUSE_EXTERNAL_FILE);
lazyboye8634172016-01-28 00:10:482055 installer->set_install_immediately(info.install_immediately);
2056 installer->set_creation_flags(info.creation_flags);
[email protected]88e8ec9152013-01-17 04:05:182057#if defined(OS_CHROMEOS)
lazyboye8634172016-01-28 00:10:482058 extensions::InstallLimiter::Get(profile_)->Add(installer, info.path);
[email protected]88e8ec9152013-01-17 04:05:182059#else
lazyboye8634172016-01-28 00:10:482060 installer->InstallCrx(info.path);
[email protected]88e8ec9152013-01-17 04:05:182061#endif
[email protected]47fc70c2011-12-06 07:29:512062
2063 // Depending on the source, a new external extension might not need a user
2064 // notification on installation. For such extensions, mark them acknowledged
2065 // now to suppress the notification.
lazyboye8634172016-01-28 00:10:482066 if (info.mark_acknowledged)
2067 external_install_manager_->AcknowledgeExternalExtension(info.extension_id);
[email protected]9060d8b02012-01-13 02:14:302068
2069 return true;
[email protected]7577a5c52009-07-30 06:21:582070}
2071
[email protected]eaa7dd182010-12-14 11:09:002072void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]3a1dc572012-07-31 22:25:132073 extensions::ExtensionHost* host) {
[email protected]406027c02010-09-27 08:03:182074 OrphanedDevTools::iterator iter =
[email protected]2d2f6cfc2011-05-06 21:09:332075 orphaned_dev_tools_.find(host->extension_id());
[email protected]406027c02010-09-27 08:03:182076 if (iter == orphaned_dev_tools_.end())
2077 return;
2078
rockota34fed222015-09-25 21:56:312079 // Keepalive count is reset on extension reload. This re-establishes the
2080 // keepalive that was added when the DevTools agent was initially attached.
2081 extensions::ProcessManager::Get(profile_)->IncrementLazyKeepaliveCount(
2082 host->extension());
[email protected]b3f957e62014-08-08 10:09:022083 iter->second->ConnectWebContents(host->host_contents());
[email protected]406027c02010-09-27 08:03:182084 orphaned_dev_tools_.erase(iter);
2085}
2086
[email protected]432115822011-07-10 15:52:272087void ExtensionService::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:532088 const content::NotificationSource& source,
2089 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:272090 switch (type) {
[email protected]3c4abc82012-10-22 22:25:542091 case chrome::NOTIFICATION_APP_TERMINATING:
2092 // Shutdown has started. Don't start any more extension installs.
2093 // (We cannot use ExtensionService::Shutdown() for this because it
2094 // happens too late in browser teardown.)
2095 browser_terminating_ = true;
2096 break;
[email protected]adf5a102014-07-31 12:44:062097 case extensions::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]6c2381d2011-10-19 02:52:532098 if (profile_ !=
2099 content::Source<Profile>(source).ptr()->GetOriginalProfile()) {
[email protected]bc535ee52010-08-31 18:40:322100 break;
[email protected]6c2381d2011-10-19 02:52:532101 }
[email protected]a4ed6282009-12-14 20:51:162102
[email protected]3a1dc572012-07-31 22:25:132103 extensions::ExtensionHost* host =
2104 content::Details<extensions::ExtensionHost>(details).ptr();
[email protected]f128af42010-08-05 18:05:262105
[email protected]3964e572014-02-13 21:57:412106 // If the extension is already being terminated, there is nothing left to
2107 // do.
2108 if (!extensions_being_terminated_.insert(host->extension_id()).second)
2109 break;
2110
[email protected]fa2416f2011-05-03 08:41:202111 // Mark the extension as terminated and Unload it. We want it to
2112 // be in a consistent state: either fully working or not loaded
2113 // at all, but never half-crashed. We do it in a PostTask so
2114 // that other handlers of this notification will still have
2115 // access to the Extension and ExtensionHost.
skyostilf221b7de2015-06-11 20:36:322116 base::ThreadTaskRunnerHandle::Get()->PostTask(
2117 FROM_HERE, base::Bind(&ExtensionService::TrackTerminatedExtension,
2118 AsWeakPtr(), host->extension()->id()));
[email protected]31f77262009-12-02 20:48:532119 break;
2120 }
[email protected]432115822011-07-10 15:52:272121 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
[email protected]f3b1a082011-11-18 00:34:302122 content::RenderProcessHost* process =
2123 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192124 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302125 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192126 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2127 break;
2128
[email protected]fafdc842014-01-17 18:09:082129 extensions::ProcessMap* process_map =
2130 extensions::ProcessMap::Get(profile_);
2131 if (process_map->Contains(process->GetID())) {
[email protected]52b76592013-11-02 17:59:032132 // An extension process was terminated, this might have resulted in an
2133 // app or extension becoming idle.
2134 std::set<std::string> extension_ids =
[email protected]fafdc842014-01-17 18:09:082135 process_map->GetExtensionsInProcess(process->GetID());
elijahtaylor0def4432014-10-06 18:15:112136 // In addition to the extensions listed in the process map, one of those
2137 // extensions could be referencing a shared module which is waiting for
2138 // idle to update. Check all imports of these extensions, too.
2139 std::set<std::string> import_ids;
2140 for (std::set<std::string>::const_iterator it = extension_ids.begin();
2141 it != extension_ids.end();
2142 ++it) {
2143 const Extension* extension = GetExtensionById(*it, true);
2144 if (!extension)
2145 continue;
2146 const std::vector<SharedModuleInfo::ImportInfo>& imports =
2147 SharedModuleInfo::GetImports(extension);
2148 std::vector<SharedModuleInfo::ImportInfo>::const_iterator import_it;
2149 for (import_it = imports.begin(); import_it != imports.end();
2150 import_it++) {
2151 import_ids.insert((*import_it).extension_id);
2152 }
2153 }
2154 extension_ids.insert(import_ids.begin(), import_ids.end());
2155
[email protected]52b76592013-11-02 17:59:032156 for (std::set<std::string>::const_iterator it = extension_ids.begin();
2157 it != extension_ids.end(); ++it) {
2158 if (delayed_installs_.Contains(*it)) {
skyostilf221b7de2015-06-11 20:36:322159 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
[email protected]52b76592013-11-02 17:59:032160 FROM_HERE,
2161 base::Bind(&ExtensionService::MaybeFinishDelayedInstallation,
2162 AsWeakPtr(), *it),
2163 base::TimeDelta::FromSeconds(kUpdateIdleDelay));
2164 }
2165 }
2166 }
2167
[email protected]fafdc842014-01-17 18:09:082168 process_map->RemoveAllFromProcess(process->GetID());
[email protected]6f371442011-11-09 06:45:462169 BrowserThread::PostTask(
[email protected]38427a12013-11-09 17:34:202170 BrowserThread::IO,
2171 FROM_HERE,
2172 base::Bind(&extensions::InfoMap::UnregisterAllExtensionsInProcess,
[email protected]31d8f5f22012-04-02 15:22:082173 system_->info_map(),
[email protected]f3b1a082011-11-18 00:34:302174 process->GetID()));
[email protected]da5683db2011-04-23 17:12:212175 break;
2176 }
[email protected]92dd8d92013-02-26 00:41:082177 case chrome::NOTIFICATION_UPGRADE_RECOMMENDED: {
[email protected]75bdcb872013-03-13 00:41:452178 // Notify observers that chrome update is available.
2179 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
2180 OnChromeUpdateAvailable());
[email protected]92dd8d92013-02-26 00:41:082181 break;
2182 }
[email protected]ebe07772014-05-22 04:16:062183 case chrome::NOTIFICATION_PROFILE_DESTRUCTION_STARTED: {
2184 OnProfileDestructionStarted();
2185 break;
2186 }
[email protected]aa96d3a2010-08-21 08:45:252187
[email protected]4814b512009-11-07 00:12:292188 default:
2189 NOTREACHED() << "Unexpected notification type.";
2190 }
2191}
2192
binjin47947f842014-11-18 12:10:242193int ExtensionService::GetDisableReasonsOnInstalled(const Extension* extension) {
2194 Extension::DisableReason disable_reason;
2195 // Extensions disabled by management policy should always be disabled, even
2196 // if it's force-installed.
2197 if (system_->management_policy()->MustRemainDisabled(
2198 extension, &disable_reason, nullptr)) {
2199 // A specified reason is required to disable the extension.
2200 DCHECK(disable_reason != Extension::DISABLE_NONE);
2201 return disable_reason;
2202 }
2203
[email protected]612a1cb12012-10-17 13:18:032204 // Extensions installed by policy can't be disabled. So even if a previous
2205 // installation disabled the extension, make sure it is now enabled.
binjin47947f842014-11-18 12:10:242206 if (system_->management_policy()->MustRemainEnabled(extension, nullptr))
2207 return Extension::DISABLE_NONE;
[email protected]612a1cb12012-10-17 13:18:032208
binjin47947f842014-11-18 12:10:242209 // An already disabled extension should inherit the disable reasons and
2210 // remain disabled.
2211 if (extension_prefs_->IsExtensionDisabled(extension->id())) {
2212 int disable_reasons = extension_prefs_->GetDisableReasons(extension->id());
2213 // If an extension was disabled without specified reason, presume it's
2214 // disabled by user.
2215 return disable_reasons == Extension::DISABLE_NONE
2216 ? Extension::DISABLE_USER_ACTION
2217 : disable_reasons;
2218 }
[email protected]612a1cb12012-10-17 13:18:032219
[email protected]41070e8d2012-10-24 01:34:362220 if (FeatureSwitch::prompt_for_external_extensions()->IsEnabled()) {
[email protected]612a1cb12012-10-17 13:18:032221 // External extensions are initially disabled. We prompt the user before
[email protected]2c495c42013-01-04 21:49:542222 // enabling them. Hosted apps are excepted because they are not dangerous
2223 // (they need to be launched by the user anyway).
[email protected]1d5e58b2013-01-31 08:41:402224 if (extension->GetType() != Manifest::TYPE_HOSTED_APP &&
2225 Manifest::IsExternalLocation(extension->location()) &&
[email protected]612a1cb12012-10-17 13:18:032226 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id())) {
binjin47947f842014-11-18 12:10:242227 return Extension::DISABLE_EXTERNAL_EXTENSION;
[email protected]612a1cb12012-10-17 13:18:032228 }
2229 }
[email protected]612a1cb12012-10-17 13:18:032230
binjin47947f842014-11-18 12:10:242231 return Extension::DISABLE_NONE;
[email protected]612a1cb12012-10-17 13:18:032232}
[email protected]0db124b02012-11-07 04:55:052233
mlerman6a37b6a42014-11-26 22:10:532234// Helper method to determine if an extension can be blocked.
2235bool ExtensionService::CanBlockExtension(const Extension* extension) const {
mlerman3690e5be2014-12-01 22:57:442236 DCHECK(extension);
mlerman6a37b6a42014-11-26 22:10:532237 return extension->location() != Manifest::COMPONENT &&
2238 extension->location() != Manifest::EXTERNAL_COMPONENT &&
achuith2f5578b2016-02-26 21:57:132239 !system_->management_policy()->MustRemainEnabled(extension, nullptr);
mlerman6a37b6a42014-11-26 22:10:532240}
2241
xiyuanf6a4c6a62016-04-19 18:14:542242extensions::InstallGate::Action ExtensionService::ShouldDelayExtensionInstall(
2243 const extensions::Extension* extension,
2244 bool install_immediately,
2245 extensions::ExtensionPrefs::DelayReason* reason) const {
2246 for (const auto& entry : install_delayer_registry_) {
2247 extensions::InstallGate* const delayer = entry.second;
2248 extensions::InstallGate::Action action =
2249 delayer->ShouldDelay(extension, install_immediately);
2250 if (action != extensions::InstallGate::INSTALL) {
2251 *reason = entry.first;
2252 return action;
2253 }
[email protected]e7aa7b7e2012-11-27 04:51:222254 }
[email protected]fdd679b2012-11-15 20:49:392255
xiyuanf6a4c6a62016-04-19 18:14:542256 return extensions::InstallGate::INSTALL;
[email protected]9f4e4f082013-06-21 07:11:192257}
2258
2259void ExtensionService::MaybeFinishDelayedInstallations() {
2260 std::vector<std::string> to_be_installed;
achuith2f5578b2016-02-26 21:57:132261 for (const auto& extension : delayed_installs_) {
2262 to_be_installed.push_back(extension->id());
[email protected]399583b2012-12-11 09:33:422263 }
achuith2f5578b2016-02-26 21:57:132264 for (const auto& extension_id : to_be_installed) {
2265 MaybeFinishDelayedInstallation(extension_id);
[email protected]399583b2012-12-11 09:33:422266 }
[email protected]399583b2012-12-11 09:33:422267}
2268
[email protected]fdd679b2012-11-15 20:49:392269void ExtensionService::OnBlacklistUpdated() {
[email protected]2d19eb6e2014-01-27 17:30:002270 blacklist_->GetBlacklistedIDs(
[email protected]f47f7172014-03-19 19:27:102271 registry_->GenerateInstalledExtensionsSet()->GetIDs(),
[email protected]3f2a2fa2013-09-24 02:55:252272 base::Bind(&ExtensionService::ManageBlacklist, AsWeakPtr()));
[email protected]695b5712012-12-06 23:55:282273}
2274
[email protected]2d19eb6e2014-01-27 17:30:002275void ExtensionService::ManageBlacklist(
2276 const extensions::Blacklist::BlacklistStateMap& state_map) {
[email protected]54ee8192014-03-29 17:37:242277 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]695b5712012-12-06 23:55:282278
mlerman6a37b6a42014-11-26 22:10:532279 std::set<std::string> blacklisted;
[email protected]2d19eb6e2014-01-27 17:30:002280 ExtensionIdSet greylist;
2281 ExtensionIdSet unchanged;
achuith2f5578b2016-02-26 21:57:132282 for (const auto& it : state_map) {
2283 switch (it.second) {
[email protected]2d19eb6e2014-01-27 17:30:002284 case extensions::NOT_BLACKLISTED:
2285 break;
[email protected]695b5712012-12-06 23:55:282286
[email protected]2d19eb6e2014-01-27 17:30:002287 case extensions::BLACKLISTED_MALWARE:
achuith2f5578b2016-02-26 21:57:132288 blacklisted.insert(it.first);
[email protected]2d19eb6e2014-01-27 17:30:002289 break;
2290
2291 case extensions::BLACKLISTED_SECURITY_VULNERABILITY:
2292 case extensions::BLACKLISTED_CWS_POLICY_VIOLATION:
2293 case extensions::BLACKLISTED_POTENTIALLY_UNWANTED:
achuith2f5578b2016-02-26 21:57:132294 greylist.insert(it.first);
[email protected]2d19eb6e2014-01-27 17:30:002295 break;
2296
2297 case extensions::BLACKLISTED_UNKNOWN:
achuith2f5578b2016-02-26 21:57:132298 unchanged.insert(it.first);
[email protected]2d19eb6e2014-01-27 17:30:002299 break;
2300 }
2301 }
2302
mlerman6a37b6a42014-11-26 22:10:532303 UpdateBlacklistedExtensions(blacklisted, unchanged);
[email protected]2d19eb6e2014-01-27 17:30:002304 UpdateGreylistedExtensions(greylist, unchanged, state_map);
2305
[email protected]373daf972014-04-10 01:50:442306 error_controller_->ShowErrorIfNeeded();
[email protected]2d19eb6e2014-01-27 17:30:002307}
2308
2309namespace {
2310void Partition(const ExtensionIdSet& before,
2311 const ExtensionIdSet& after,
2312 const ExtensionIdSet& unchanged,
2313 ExtensionIdSet* no_longer,
2314 ExtensionIdSet* not_yet) {
2315 *not_yet = base::STLSetDifference<ExtensionIdSet>(after, before);
2316 *no_longer = base::STLSetDifference<ExtensionIdSet>(before, after);
2317 *no_longer = base::STLSetDifference<ExtensionIdSet>(*no_longer, unchanged);
2318}
2319} // namespace
2320
mlerman6a37b6a42014-11-26 22:10:532321void ExtensionService::UpdateBlacklistedExtensions(
2322 const ExtensionIdSet& blacklisted,
[email protected]2d19eb6e2014-01-27 17:30:002323 const ExtensionIdSet& unchanged) {
2324 ExtensionIdSet not_yet_blocked, no_longer_blocked;
mlerman6a37b6a42014-11-26 22:10:532325 Partition(registry_->blacklisted_extensions().GetIDs(), blacklisted,
2326 unchanged, &no_longer_blocked, &not_yet_blocked);
[email protected]2d19eb6e2014-01-27 17:30:002327
2328 for (ExtensionIdSet::iterator it = no_longer_blocked.begin();
2329 it != no_longer_blocked.end(); ++it) {
[email protected]695b5712012-12-06 23:55:282330 scoped_refptr<const Extension> extension =
[email protected]bb1bc9b32013-12-21 03:09:142331 registry_->blacklisted_extensions().GetByID(*it);
[email protected]3f2a2fa2013-09-24 02:55:252332 if (!extension.get()) {
mlerman6a37b6a42014-11-26 22:10:532333 NOTREACHED() << "Extension " << *it << " no longer blacklisted, "
2334 << "but it was never blacklisted.";
[email protected]695b5712012-12-06 23:55:282335 continue;
[email protected]3f2a2fa2013-09-24 02:55:252336 }
[email protected]bb1bc9b32013-12-21 03:09:142337 registry_->RemoveBlacklisted(*it);
atuchin6dc7c442016-07-20 07:04:342338 extension_prefs_->SetExtensionBlacklistState(extension->id(),
2339 extensions::NOT_BLACKLISTED);
[email protected]dc24976f2013-06-02 21:15:092340 AddExtension(extension.get());
[email protected]ac875372013-02-28 04:36:092341 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.UnblacklistInstalled",
[email protected]dc24976f2013-06-02 21:15:092342 extension->location(),
2343 Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:282344 }
2345
[email protected]2d19eb6e2014-01-27 17:30:002346 for (ExtensionIdSet::iterator it = not_yet_blocked.begin();
2347 it != not_yet_blocked.end(); ++it) {
[email protected]695b5712012-12-06 23:55:282348 scoped_refptr<const Extension> extension = GetInstalledExtension(*it);
[email protected]3f2a2fa2013-09-24 02:55:252349 if (!extension.get()) {
2350 NOTREACHED() << "Extension " << *it << " needs to be "
2351 << "blacklisted, but it's not installed.";
[email protected]695b5712012-12-06 23:55:282352 continue;
[email protected]3f2a2fa2013-09-24 02:55:252353 }
[email protected]bb1bc9b32013-12-21 03:09:142354 registry_->AddBlacklisted(extension);
[email protected]2d19eb6e2014-01-27 17:30:002355 extension_prefs_->SetExtensionBlacklistState(
2356 extension->id(), extensions::BLACKLISTED_MALWARE);
[email protected]b0af4792013-10-23 09:12:132357 UnloadExtension(*it, UnloadedExtensionInfo::REASON_BLACKLIST);
[email protected]ac875372013-02-28 04:36:092358 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.BlacklistInstalled",
2359 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:282360 }
[email protected]2d19eb6e2014-01-27 17:30:002361}
[email protected]695b5712012-12-06 23:55:282362
[email protected]2d19eb6e2014-01-27 17:30:002363// TODO(oleg): UMA logging
2364void ExtensionService::UpdateGreylistedExtensions(
2365 const ExtensionIdSet& greylist,
2366 const ExtensionIdSet& unchanged,
2367 const extensions::Blacklist::BlacklistStateMap& state_map) {
2368 ExtensionIdSet not_yet_greylisted, no_longer_greylisted;
2369 Partition(greylist_.GetIDs(),
2370 greylist, unchanged,
2371 &no_longer_greylisted, &not_yet_greylisted);
2372
2373 for (ExtensionIdSet::iterator it = no_longer_greylisted.begin();
2374 it != no_longer_greylisted.end(); ++it) {
2375 scoped_refptr<const Extension> extension = greylist_.GetByID(*it);
2376 if (!extension.get()) {
2377 NOTREACHED() << "Extension " << *it << " no longer greylisted, "
2378 << "but it was not marked as greylisted.";
2379 continue;
2380 }
2381
2382 greylist_.Remove(*it);
2383 extension_prefs_->SetExtensionBlacklistState(extension->id(),
2384 extensions::NOT_BLACKLISTED);
2385 if (extension_prefs_->GetDisableReasons(extension->id()) &
2386 extensions::Extension::DISABLE_GREYLIST)
2387 EnableExtension(*it);
2388 }
2389
2390 for (ExtensionIdSet::iterator it = not_yet_greylisted.begin();
2391 it != not_yet_greylisted.end(); ++it) {
2392 scoped_refptr<const Extension> extension = GetInstalledExtension(*it);
2393 if (!extension.get()) {
2394 NOTREACHED() << "Extension " << *it << " needs to be "
2395 << "disabled, but it's not installed.";
2396 continue;
2397 }
2398 greylist_.Insert(extension);
2399 extension_prefs_->SetExtensionBlacklistState(extension->id(),
2400 state_map.find(*it)->second);
2401 if (registry_->enabled_extensions().Contains(extension->id()))
2402 DisableExtension(*it, extensions::Extension::DISABLE_GREYLIST);
2403 }
[email protected]fdd679b2012-11-15 20:49:392404}
[email protected]75bdcb872013-03-13 00:41:452405
2406void ExtensionService::AddUpdateObserver(extensions::UpdateObserver* observer) {
2407 update_observers_.AddObserver(observer);
2408}
2409
2410void ExtensionService::RemoveUpdateObserver(
2411 extensions::UpdateObserver* observer) {
2412 update_observers_.RemoveObserver(observer);
2413}
[email protected]bb1bc9b32013-12-21 03:09:142414
xiyuanf6a4c6a62016-04-19 18:14:542415void ExtensionService::RegisterInstallGate(
2416 extensions::ExtensionPrefs::DelayReason reason,
2417 extensions::InstallGate* install_delayer) {
2418 DCHECK(install_delayer_registry_.end() ==
2419 install_delayer_registry_.find(reason));
2420 install_delayer_registry_[reason] = install_delayer;
2421}
2422
2423void ExtensionService::UnregisterInstallGate(
2424 extensions::InstallGate* install_delayer) {
2425 for (auto it = install_delayer_registry_.begin();
2426 it != install_delayer_registry_.end(); ++it) {
2427 if (it->second == install_delayer) {
2428 install_delayer_registry_.erase(it);
2429 return;
2430 }
2431 }
2432}
2433
[email protected]bb1bc9b32013-12-21 03:09:142434// Used only by test code.
2435void ExtensionService::UnloadAllExtensionsInternal() {
2436 profile_->GetExtensionSpecialStoragePolicy()->RevokeRightsForAllExtensions();
2437
2438 registry_->ClearAll();
[email protected]45f5b7d2014-01-22 23:47:132439 system_->runtime_data()->ClearAll();
[email protected]bb1bc9b32013-12-21 03:09:142440
2441 // TODO(erikkay) should there be a notification for this? We can't use
2442 // EXTENSION_UNLOADED since that implies that the extension has been disabled
2443 // or uninstalled.
2444}
[email protected]ebe07772014-05-22 04:16:062445
2446void ExtensionService::OnProfileDestructionStarted() {
2447 ExtensionIdSet ids_to_unload = registry_->enabled_extensions().GetIDs();
2448 for (ExtensionIdSet::iterator it = ids_to_unload.begin();
2449 it != ids_to_unload.end();
2450 ++it) {
2451 UnloadExtension(*it, UnloadedExtensionInfo::REASON_PROFILE_SHUTDOWN);
2452 }
2453}