blob: fb604df1619d6258e032ed87546943a3a576cd9f [file] [log] [blame]
[email protected]098fa7a2013-03-08 22:11:171// Copyright (c) 2013 The Chromium Authors. All rights reserved.
[email protected]6014d672008-12-05 00:38:252// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]eaa7dd182010-12-14 11:09:005#include "chrome/browser/extensions/extension_service.h"
[email protected]6014d672008-12-05 00:38:256
[email protected]654512b2010-09-01 02:09:427#include <algorithm>
[email protected]695b5712012-12-06 23:55:288#include <iterator>
[email protected]97d2f1d2011-01-15 00:41:089#include <set>
[email protected]654512b2010-09-01 02:09:4210
[email protected]62433d32011-10-12 22:33:1211#include "base/command_line.h"
skyostilf221b7de2015-06-11 20:36:3212#include "base/location.h"
rkaplowfca0f4dc52015-03-14 21:40:5713#include "base/metrics/histogram_macros.h"
yiyaoliu252f915e2015-04-23 21:56:3614#include "base/profiler/scoped_profile.h"
skyostilf221b7de2015-06-11 20:36:3215#include "base/single_thread_task_runner.h"
[email protected]3ea1b182013-02-08 22:38:4116#include "base/strings/string_number_conversions.h"
[email protected]00e7bef2013-06-10 20:35:1717#include "base/strings/stringprintf.h"
[email protected]112158af2013-06-07 23:46:1818#include "base/strings/utf_string_conversions.h"
skyostilf221b7de2015-06-11 20:36:3219#include "base/thread_task_runner_handle.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"
[email protected]41a17c52013-06-28 00:27:5322#include "base/time/time.h"
rkaplowe65c2ff2015-02-14 16:29:5423#include "base/trace_event/trace_event.h"
[email protected]15730c42009-09-03 00:03:2024#include "chrome/browser/browser_process.h"
[email protected]9ea0cd32013-07-12 01:50:3625#include "chrome/browser/chrome_notification_types.h"
mukai87a8d402014-09-15 20:15:0926#include "chrome/browser/content_settings/content_settings_internal_extension_provider.h"
sunil.ratnu21b896c2014-11-24 10:09:2427#include "chrome/browser/extensions/api/content_settings/content_settings_custom_extension_provider.h"
mukai87a8d402014-09-15 20:15:0928#include "chrome/browser/extensions/api/content_settings/content_settings_service.h"
ryanackley48bedbd2015-01-27 23:12:1429#include "chrome/browser/extensions/app_data_migrator.h"
[email protected]d8c8f25f2011-11-02 18:18:0130#include "chrome/browser/extensions/component_loader.h"
[email protected]13e062e2014-08-09 10:21:5531#include "chrome/browser/extensions/crx_installer.h"
[email protected]d9ede582012-08-14 19:21:3832#include "chrome/browser/extensions/data_deleter.h"
rdevlin.cronin2813c1b2014-08-26 23:51:0833#include "chrome/browser/extensions/extension_action_storage_manager.h"
[email protected]5a145e82014-05-29 22:19:0734#include "chrome/browser/extensions/extension_assets_manager.h"
[email protected]62f051c2012-03-29 17:04:4435#include "chrome/browser/extensions/extension_disabled_ui.h"
[email protected]373daf972014-04-10 01:50:4436#include "chrome/browser/extensions/extension_error_controller.h"
[email protected]19eb80152011-02-26 00:28:4337#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]f8aefb132013-10-30 09:29:5238#include "chrome/browser/extensions/extension_sync_service.h"
[email protected]617342a42013-12-18 23:34:0339#include "chrome/browser/extensions/extension_util.h"
[email protected]2894a512014-06-26 19:03:5640#include "chrome/browser/extensions/external_install_manager.h"
[email protected]5df038b2012-07-16 19:03:2741#include "chrome/browser/extensions/external_provider_impl.h"
[email protected]ffd2f79e2013-11-14 00:11:4642#include "chrome/browser/extensions/install_verifier.h"
[email protected]d8c8f25f2011-11-02 18:18:0143#include "chrome/browser/extensions/installed_loader.h"
[email protected]f3d3b382014-03-14 21:19:2844#include "chrome/browser/extensions/pending_extension_manager.h"
[email protected]13e062e2014-08-09 10:21:5545#include "chrome/browser/extensions/permissions_updater.h"
[email protected]90878c52014-04-04 18:21:0246#include "chrome/browser/extensions/shared_module_service.h"
[email protected]13e062e2014-08-09 10:21:5547#include "chrome/browser/extensions/unpacked_installer.h"
rockotdb5192992014-09-16 21:27:0248#include "chrome/browser/extensions/updater/chrome_extension_downloader_factory.h"
[email protected]13e062e2014-08-09 10:21:5549#include "chrome/browser/extensions/updater/extension_updater.h"
rockotdb5192992014-09-16 21:27:0250#include "chrome/browser/google/google_brand.h"
[email protected]8ecad5e2010-12-02 21:18:3351#include "chrome/browser/profiles/profile.h"
dbeam8d57a012015-05-21 07:12:2052#include "chrome/browser/search/thumbnail_source.h"
[email protected]b5a507b22013-11-08 20:41:5753#include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
[email protected]c8d407e2011-04-28 21:27:1754#include "chrome/browser/ui/webui/favicon_source.h"
[email protected]b07e606e2012-09-15 20:16:1555#include "chrome/browser/ui/webui/theme_source.h"
[email protected]e2eb43112009-05-29 21:19:5456#include "chrome/common/chrome_switches.h"
[email protected]f6431be82013-09-07 02:53:4557#include "chrome/common/crash_keys.h"
[email protected]9f4e4f082013-06-21 07:11:1958#include "chrome/common/extensions/extension_constants.h"
[email protected]15d4d2d2013-08-09 06:49:0359#include "chrome/common/extensions/features/feature_channel.h"
[email protected]a57209872009-05-04 22:53:1460#include "chrome/common/url_constants.h"
mukai8eaec822014-10-25 17:53:1661#include "components/content_settings/core/browser/host_content_settings_map.h"
[email protected]fdd28372014-08-21 02:27:2662#include "components/crx_file/id_util.h"
[email protected]7a83dd072013-09-03 12:09:1763#include "components/startup_metric_utils/startup_metric_utils.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"
[email protected]34423532013-11-21 18:13:1068#include "extensions/browser/event_router.h"
[email protected]22401dc2014-03-21 01:38:5769#include "extensions/browser/extension_host.h"
[email protected]dccba4f82014-05-29 00:52:5670#include "extensions/browser/extension_prefs.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"
[email protected]4a1d9c0d2014-06-13 12:50:1174#include "extensions/browser/install_flag.h"
[email protected]45f5b7d2014-01-22 23:47:1375#include "extensions/browser/runtime_data.h"
[email protected]e43c61f2014-07-20 21:46:3476#include "extensions/browser/uninstall_reason.h"
[email protected]a9aa62b312013-11-29 05:35:0677#include "extensions/browser/update_observer.h"
rockotffa65b12014-10-14 17:19:0678#include "extensions/browser/updater/extension_cache.h"
rockot05f40a922014-10-16 19:40:2579#include "extensions/browser/updater/extension_downloader.h"
[email protected]fb820c02014-03-13 15:07:0880#include "extensions/common/extension_messages.h"
rockot90659852014-09-18 19:31:5281#include "extensions/common/extension_urls.h"
[email protected]5ef835a2013-11-08 20:42:5782#include "extensions/common/feature_switch.h"
[email protected]85df9d12014-04-15 17:02:1483#include "extensions/common/file_util.h"
[email protected]0c3c9732013-09-16 08:53:4184#include "extensions/common/manifest_constants.h"
[email protected]558878cc82013-11-09 01:25:5185#include "extensions/common/manifest_handlers/background_info.h"
rockotd5546142014-10-15 00:29:0886#include "extensions/common/manifest_url_handlers.h"
[email protected]dccba4f82014-05-29 00:52:5687#include "extensions/common/one_shot_event.h"
treib2e0517f92015-05-07 23:15:3688#include "extensions/common/permissions/api_permission.h"
[email protected]c41003472013-10-19 15:37:2589#include "extensions/common/permissions/permission_message_provider.h"
[email protected]e4452d32013-11-15 23:07:4190#include "extensions/common/permissions/permissions_data.h"
[email protected]79a60642012-10-20 21:03:1891
treib8ecc1eb52015-03-04 18:29:0692#if defined(ENABLE_SUPERVISED_USERS)
93#include "chrome/browser/supervised_user/supervised_user_service.h"
94#include "chrome/browser/supervised_user/supervised_user_service_factory.h"
95#endif
96
[email protected]eed367e2011-04-12 03:43:3197#if defined(OS_CHROMEOS)
[email protected]88e8ec9152013-01-17 04:05:1898#include "chrome/browser/chromeos/extensions/install_limiter.h"
pilgrime92c5fcd2014-09-10 23:31:2399#include "storage/browser/fileapi/file_system_backend.h"
100#include "storage/browser/fileapi/file_system_context.h"
[email protected]eed367e2011-04-12 03:43:31101#endif
102
[email protected]55eb70e762012-02-20 17:38:39103using content::BrowserContext;
[email protected]631bb742011-11-02 11:29:39104using content::BrowserThread;
[email protected]98f66112012-12-25 12:59:36105using content::DevToolsAgentHost;
treib2e0517f92015-05-07 23:15:36106using extensions::APIPermission;
[email protected]bf3d9df2012-07-24 23:20:27107using extensions::CrxInstaller;
[email protected]1c321ee52012-05-21 03:02:34108using extensions::Extension;
109using extensions::ExtensionIdSet;
110using extensions::ExtensionInfo;
[email protected]599539802014-01-07 23:06:00111using extensions::ExtensionRegistry;
[email protected]289c44b2013-12-17 03:26:57112using extensions::ExtensionSet;
[email protected]215a7be2012-10-22 19:53:42113using extensions::FeatureSwitch;
[email protected]ffd2f79e2013-11-14 00:11:46114using extensions::InstallVerifier;
[email protected]0d54b682013-11-05 14:15:36115using extensions::ManagementPolicy;
[email protected]1d5e58b2013-01-31 08:41:40116using extensions::Manifest;
treib2e0517f92015-05-07 23:15:36117using extensions::PermissionID;
118using extensions::PermissionIDSet;
[email protected]c2e66e12012-06-27 06:27:06119using extensions::PermissionMessage;
treibb566ee832015-03-16 10:20:46120using extensions::PermissionMessageIDs;
[email protected]c2e66e12012-06-27 06:27:06121using extensions::PermissionSet;
[email protected]9f4e4f082013-06-21 07:11:19122using extensions::SharedModuleInfo;
[email protected]90878c52014-04-04 18:21:02123using extensions::SharedModuleService;
[email protected]e410b5f2012-12-14 14:02:24124using extensions::UnloadedExtensionInfo;
[email protected]5ef47ec2010-01-28 05:58:05125
[email protected]b6ab96d2009-08-20 18:58:19126namespace {
127
[email protected]0db124b02012-11-07 04:55:05128// Wait this many seconds after an extensions becomes idle before updating it.
[email protected]ddc6a122014-06-27 04:52:32129const int kUpdateIdleDelay = 5;
[email protected]0db124b02012-11-07 04:55:05130
treib8ecc1eb52015-03-04 18:29:06131#if defined(ENABLE_SUPERVISED_USERS)
132// Callback for SupervisedUserService::AddExtensionUpdateRequest.
133void ExtensionUpdateRequestSent(const std::string& id, bool success) {
134 LOG_IF(WARNING, !success) << "Failed sending update request for " << id;
135}
136#endif
137
[email protected]c6d474f82009-12-16 21:11:06138} // namespace
[email protected]b6ab96d2009-08-20 18:58:19139
[email protected]eaa7dd182010-12-14 11:09:00140// ExtensionService.
[email protected]6014d672008-12-05 00:38:25141
[email protected]8e4560b62011-01-14 10:09:14142void ExtensionService::CheckExternalUninstall(const std::string& id) {
[email protected]a29a517a2011-01-21 21:11:12143 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20144
[email protected]7a4c6852010-09-16 03:44:22145 // Check if the providers know about this extension.
[email protected]5df038b2012-07-16 19:03:27146 extensions::ProviderCollection::const_iterator i;
[email protected]0a60a2e2010-10-25 16:15:21147 for (i = external_extension_providers_.begin();
148 i != external_extension_providers_.end(); ++i) {
[email protected]8e4560b62011-01-14 10:09:14149 DCHECK(i->get()->IsReady());
[email protected]0a60a2e2010-10-25 16:15:21150 if (i->get()->HasExtension(id))
151 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22152 }
153
[email protected]0f48fca2011-05-19 18:46:35154 // We get the list of external extensions to check from preferences.
155 // It is possible that an extension has preferences but is not loaded.
156 // For example, an extension that requires experimental permissions
157 // will not be loaded if the experimental command line flag is not used.
158 // In this case, do not uninstall.
[email protected]31bb5ee62012-09-12 22:58:40159 if (!GetInstalledExtension(id)) {
[email protected]0f48fca2011-05-19 18:46:35160 // We can't call UninstallExtension with an unloaded/invalid
161 // extension ID.
162 LOG(WARNING) << "Attempted uninstallation of unloaded/invalid extension "
163 << "with id: " << id;
164 return;
165 }
[email protected]42d58f62014-07-31 01:32:45166 UninstallExtension(id,
167 extensions::UNINSTALL_REASON_ORPHANED_EXTERNAL_EXTENSION,
168 base::Bind(&base::DoNothing),
169 NULL);
[email protected]7a4c6852010-09-16 03:44:22170}
171
[email protected]7f8f24f2012-11-15 19:40:14172void ExtensionService::SetFileTaskRunnerForTesting(
dcheng605ef8d2014-08-28 18:29:44173 const scoped_refptr<base::SequencedTaskRunner>& task_runner) {
[email protected]7f8f24f2012-11-15 19:40:14174 file_task_runner_ = task_runner;
175}
176
[email protected]8e4560b62011-01-14 10:09:14177void ExtensionService::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22178 external_extension_providers_.clear();
179}
180
[email protected]8e4560b62011-01-14 10:09:14181void ExtensionService::AddProviderForTesting(
[email protected]5df038b2012-07-16 19:03:27182 extensions::ExternalProviderInterface* test_provider) {
[email protected]a29a517a2011-01-21 21:11:12183 CHECK(test_provider);
[email protected]0a60a2e2010-10-25 16:15:21184 external_extension_providers_.push_back(
[email protected]5df038b2012-07-16 19:03:27185 linked_ptr<extensions::ExternalProviderInterface>(test_provider));
[email protected]7a4c6852010-09-16 03:44:22186}
187
[email protected]8f959f522014-08-06 06:26:28188void ExtensionService::BlacklistExtensionForTest(
189 const std::string& extension_id) {
mlerman6a37b6a42014-11-26 22:10:53190 ExtensionIdSet blacklisted;
[email protected]8f959f522014-08-06 06:26:28191 ExtensionIdSet unchanged;
mlerman6a37b6a42014-11-26 22:10:53192 blacklisted.insert(extension_id);
193 UpdateBlacklistedExtensions(blacklisted, unchanged);
[email protected]8f959f522014-08-06 06:26:28194}
195
[email protected]9060d8b02012-01-13 02:14:30196bool ExtensionService::OnExternalExtensionUpdateUrlFound(
[email protected]7a4c6852010-09-16 03:44:22197 const std::string& id,
[email protected]d8fd0fd2014-03-24 13:16:06198 const std::string& install_parameter,
[email protected]21a5a672010-11-04 10:47:42199 const GURL& update_url,
[email protected]464213a2013-10-15 01:06:48200 Manifest::Location location,
201 int creation_flags,
202 bool mark_acknowledged) {
[email protected]ab22ba42011-01-14 16:36:38203 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]fdd28372014-08-21 02:27:26204 CHECK(crx_file::id_util::IdIsValid(id));
[email protected]a8af9fdb2010-10-28 21:52:20205
[email protected]044e86992014-01-24 22:59:11206 if (Manifest::IsExternalLocation(location)) {
207 // All extensions that are not user specific can be cached.
rockotffa65b12014-10-14 17:19:06208 extensions::ExtensionsBrowserClient::Get()->GetExtensionCache()
209 ->AllowCaching(id);
[email protected]044e86992014-01-24 22:59:11210 }
211
[email protected]8a87a5332011-08-11 17:54:59212 const Extension* extension = GetExtensionById(id, true);
213 if (extension) {
214 // Already installed. Skip this install if the current location has
215 // higher priority than |location|.
[email protected]1d5e58b2013-01-31 08:41:40216 Manifest::Location current = extension->location();
217 if (current == Manifest::GetHigherPriorityLocation(current, location))
[email protected]9060d8b02012-01-13 02:14:30218 return false;
[email protected]8a87a5332011-08-11 17:54:59219 // Otherwise, overwrite the current installation.
[email protected]7a4c6852010-09-16 03:44:22220 }
[email protected]9060d8b02012-01-13 02:14:30221
222 // Add |id| to the set of pending extensions. If it can not be added,
223 // then there is already a pending record from a higher-priority install
224 // source. In this case, signal that this extension will not be
225 // installed by returning false.
226 if (!pending_extension_manager()->AddFromExternalUpdateUrl(
[email protected]d8fd0fd2014-03-24 13:16:06227 id,
228 install_parameter,
229 update_url,
230 location,
231 creation_flags,
232 mark_acknowledged)) {
[email protected]9060d8b02012-01-13 02:14:30233 return false;
[email protected]31bb5ee62012-09-12 22:58:40234 }
[email protected]9060d8b02012-01-13 02:14:30235
[email protected]94fde232012-04-27 10:22:30236 update_once_all_providers_are_ready_ = true;
[email protected]9060d8b02012-01-13 02:14:30237 return true;
[email protected]7a4c6852010-09-16 03:44:22238}
239
[email protected]6aeac8342010-10-01 20:21:18240// static
[email protected]cc2f55c2014-07-08 02:19:04241// This function is used to uninstall an extension via sync. The LOG statements
242// within this function are used to inform the user if the uninstall cannot be
243// done.
[email protected]eaa7dd182010-12-14 11:09:00244bool ExtensionService::UninstallExtensionHelper(
245 ExtensionService* extensions_service,
[email protected]cc2f55c2014-07-08 02:19:04246 const std::string& extension_id,
[email protected]e43c61f2014-07-20 21:46:34247 extensions::UninstallReason reason) {
[email protected]d6ebc9792011-04-07 18:18:33248 // We can't call UninstallExtension with an invalid extension ID.
[email protected]31bb5ee62012-09-12 22:58:40249 if (!extensions_service->GetInstalledExtension(extension_id)) {
[email protected]6aeac8342010-10-01 20:21:18250 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]95da88c42011-03-31 10:07:33251 << "id: " << extension_id;
252 return false;
[email protected]6aeac8342010-10-01 20:21:18253 }
254
[email protected]d6ebc9792011-04-07 18:18:33255 // The following call to UninstallExtension will not allow an uninstall of a
256 // policy-controlled extension.
[email protected]439f1e32013-12-09 20:09:09257 base::string16 error;
[email protected]42d58f62014-07-31 01:32:45258 if (!extensions_service->UninstallExtension(
259 extension_id, reason, base::Bind(&base::DoNothing), &error)) {
[email protected]d6ebc9792011-04-07 18:18:33260 LOG(WARNING) << "Cannot uninstall extension with id " << extension_id
261 << ": " << error;
262 return false;
263 }
[email protected]95da88c42011-03-31 10:07:33264
[email protected]6aeac8342010-10-01 20:21:18265 return true;
266}
267
[email protected]eaa7dd182010-12-14 11:09:00268ExtensionService::ExtensionService(Profile* profile,
avi3ef9ec9e2014-12-22 22:50:17269 const base::CommandLine* command_line,
[email protected]650b2d52013-02-10 03:41:45270 const base::FilePath& install_directory,
[email protected]45759612012-07-10 17:21:23271 extensions::ExtensionPrefs* extension_prefs,
[email protected]fdd679b2012-11-15 20:49:39272 extensions::Blacklist* blacklist,
[email protected]0436b102011-04-15 18:30:03273 bool autoupdate_enabled,
[email protected]4a10006a2013-05-17 23:18:35274 bool extensions_enabled,
275 extensions::OneShotEvent* ready)
[email protected]fdd679b2012-11-15 20:49:39276 : extensions::Blacklist::Observer(blacklist),
277 profile_(profile),
[email protected]bd306722012-07-11 20:43:59278 system_(extensions::ExtensionSystem::Get(profile)),
[email protected]73c47932010-12-06 18:13:43279 extension_prefs_(extension_prefs),
[email protected]695b5712012-12-06 23:55:28280 blacklist_(blacklist),
[email protected]f8aefb132013-10-30 09:29:52281 extension_sync_service_(NULL),
[email protected]5fdfa562013-12-27 17:43:59282 registry_(extensions::ExtensionRegistry::Get(profile)),
[email protected]6c9bedf2014-05-21 03:55:51283 pending_extension_manager_(profile),
[email protected]a9b00ac2009-06-25 21:03:23284 install_directory_(install_directory),
[email protected]0436b102011-04-15 18:30:03285 extensions_enabled_(extensions_enabled),
[email protected]e81dba32009-06-19 20:19:13286 show_extensions_prompts_(true),
[email protected]fc332ae2012-11-14 20:17:33287 install_updates_when_idle_(true),
[email protected]4a10006a2013-05-17 23:18:35288 ready_(ready),
[email protected]dff1e042012-04-27 10:59:18289 update_once_all_providers_are_ready_(false),
[email protected]3c4abc82012-10-22 22:25:54290 browser_terminating_(false),
[email protected]9f4e4f082013-06-21 07:11:19291 installs_delayed_for_gc_(false),
[email protected]e5f8fffe2014-04-02 00:30:44292 is_first_run_(false),
mlerman6a37b6a42014-11-26 22:10:53293 block_extensions_(false),
ryanackley48bedbd2015-01-27 23:12:14294 shared_module_service_(new extensions::SharedModuleService(profile_)),
295 app_data_migrator_(new extensions::AppDataMigrator(profile_, registry_)) {
[email protected]a29a517a2011-01-21 21:11:12296 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
rkaplowdd66a1342015-03-05 00:31:49297 TRACE_EVENT0("browser,startup", "ExtensionService::ExtensionService::ctor");
[email protected]a8af9fdb2010-10-28 21:52:20298
[email protected]36a784c2009-06-23 06:21:08299 // Figure out if extension installation should be enabled.
[email protected]367d9b172013-12-03 00:31:02300 if (extensions::ExtensionsBrowserClient::Get()->AreExtensionsDisabled(
301 *command_line, profile))
[email protected]6d60703b2009-08-29 01:29:23302 extensions_enabled_ = false;
[email protected]36a784c2009-06-23 06:21:08303
[email protected]3c4abc82012-10-22 22:25:54304 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING,
305 content::NotificationService::AllBrowserContextsAndSources());
[email protected]adf5a102014-07-31 12:44:06306 registrar_.Add(this,
307 extensions::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07308 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27309 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07310 content::NotificationService::AllBrowserContextsAndSources());
[email protected]92dd8d92013-02-26 00:41:08311 registrar_.Add(this, chrome::NOTIFICATION_UPGRADE_RECOMMENDED,
312 content::NotificationService::AllBrowserContextsAndSources());
[email protected]ebe07772014-05-22 04:16:06313 registrar_.Add(this,
314 chrome::NOTIFICATION_PROFILE_DESTRUCTION_STARTED,
315 content::Source<Profile>(profile_));
binjin1569c9b2014-09-05 13:33:18316
317 extensions::ExtensionManagementFactory::GetForBrowserContext(profile_)
318 ->AddObserver(this);
[email protected]4814b512009-11-07 00:12:29319
rkaplowdd66a1342015-03-05 00:31:49320 // Set up the ExtensionUpdater.
[email protected]93fd78f42009-07-10 16:43:17321 if (autoupdate_enabled) {
[email protected]501105b2013-09-26 05:42:02322 int update_frequency = extensions::kDefaultUpdateFrequencySeconds;
[email protected]93fd78f42009-07-10 16:43:17323 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25324 base::StringToInt(command_line->GetSwitchValueASCII(
325 switches::kExtensionsUpdateFrequency),
326 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17327 }
[email protected]044e86992014-01-24 22:59:11328 updater_.reset(new extensions::ExtensionUpdater(
329 this,
330 extension_prefs,
331 profile->GetPrefs(),
332 profile,
333 update_frequency,
rockotffa65b12014-10-14 17:19:06334 extensions::ExtensionsBrowserClient::Get()->GetExtensionCache(),
rockotdb5192992014-09-16 21:27:02335 base::Bind(ChromeExtensionDownloaderFactory::CreateForProfile,
336 profile)));
[email protected]93fd78f42009-07-10 16:43:17337 }
338
[email protected]25ae0152011-11-18 14:40:02339 component_loader_.reset(
340 new extensions::ComponentLoader(this,
341 profile->GetPrefs(),
[email protected]55e16cd2013-12-18 04:36:08342 g_browser_process->local_state(),
343 profile));
[email protected]8e4560b62011-01-14 10:09:14344
[email protected]0436b102011-04-15 18:30:03345 if (extensions_enabled_) {
[email protected]ae4c37e2012-12-21 01:16:25346 extensions::ExternalProviderImpl::CreateExternalProviders(
347 this, profile_, &external_extension_providers_);
[email protected]873531342011-03-09 12:16:05348 }
[email protected]b671760b2010-07-15 21:13:47349
[email protected]74474042013-11-21 12:03:54350 // Set this as the ExtensionService for app sorting to ensure it causes syncs
351 // if required.
[email protected]b3aa7182013-04-25 04:45:23352 is_first_run_ = !extension_prefs_->SetAlertSystemFirstRun();
353
[email protected]373daf972014-04-10 01:50:44354 error_controller_.reset(
355 new extensions::ExtensionErrorController(profile_, is_first_run_));
[email protected]374ceb6f2014-07-02 19:25:34356 external_install_manager_.reset(
357 new extensions::ExternalInstallManager(profile_, is_first_run_));
[email protected]373daf972014-04-10 01:50:44358
[email protected]c77f2352012-08-08 22:07:58359 extension_action_storage_manager_.reset(
360 new extensions::ExtensionActionStorageManager(profile_));
361
[email protected]cb0e50312011-05-09 15:03:07362 // How long is the path to the Extensions directory?
363 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ExtensionRootPathLength",
364 install_directory_.value().length(), 0, 500, 100);
[email protected]6014d672008-12-05 00:38:25365}
366
[email protected]3f213ad2012-07-26 23:39:41367extensions::PendingExtensionManager*
368 ExtensionService::pending_extension_manager() {
[email protected]b2907fd2011-03-25 16:43:37369 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45370}
371
[email protected]eaa7dd182010-12-14 11:09:00372ExtensionService::~ExtensionService() {
[email protected]c8d407e2011-04-28 21:27:17373 // No need to unload extensions here because they are profile-scoped, and the
374 // profile is in the process of being deleted.
[email protected]94b4ffc2011-02-04 05:55:17375
[email protected]5df038b2012-07-16 19:03:27376 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:14377 for (i = external_extension_providers_.begin();
378 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:27379 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:14380 provider->ServiceShutdown();
381 }
[email protected]6014d672008-12-05 00:38:25382}
383
[email protected]037228a2012-10-05 01:36:16384void ExtensionService::Shutdown() {
binjin1569c9b2014-09-05 13:33:18385 extensions::ExtensionManagementFactory::GetInstance()
386 ->GetForBrowserContext(profile())
387 ->RemoveObserver(this);
[email protected]4c9201c42013-08-16 04:56:21388 system_->management_policy()->UnregisterProvider(
389 shared_module_policy_provider_.get());
[email protected]037228a2012-10-05 01:36:16390}
391
[email protected]b2907fd2011-03-25 16:43:37392const Extension* ExtensionService::GetExtensionById(
393 const std::string& id, bool include_disabled) const {
[email protected]599539802014-01-07 23:06:00394 int include_mask = ExtensionRegistry::ENABLED;
[email protected]695b5712012-12-06 23:55:28395 if (include_disabled) {
mlerman6a37b6a42014-11-26 22:10:53396 // Include blacklisted and blocked extensions here because there are
397 // hundreds of callers of this function, and many might assume that this
398 // includes those that have been disabled due to blacklisting or blocking.
[email protected]599539802014-01-07 23:06:00399 include_mask |= ExtensionRegistry::DISABLED |
mlerman6a37b6a42014-11-26 22:10:53400 ExtensionRegistry::BLACKLISTED | ExtensionRegistry::BLOCKED;
[email protected]695b5712012-12-06 23:55:28401 }
[email protected]599539802014-01-07 23:06:00402 return registry_->GetExtensionById(id, include_mask);
[email protected]695b5712012-12-06 23:55:28403}
404
[email protected]eaa7dd182010-12-14 11:09:00405void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12406 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
rkaplowe65c2ff2015-02-14 16:29:54407 TRACE_EVENT0("browser,startup", "ExtensionService::Init");
yiyaoliu252f915e2015-04-23 21:56:36408 TRACK_SCOPED_REGION("Startup", "ExtensionService::Init");
rkaplowa8fd8d32015-02-25 21:27:56409 SCOPED_UMA_HISTOGRAM_TIMER("Extensions.ExtensionServiceInitTime");
[email protected]3350d712013-11-18 09:32:24410
[email protected]4a10006a2013-05-17 23:18:35411 DCHECK(!is_ready()); // Can't redo init.
[email protected]bb1bc9b32013-12-21 03:09:14412 DCHECK_EQ(registry_->enabled_extensions().size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32413
avi3ef9ec9e2014-12-22 22:50:17414 const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
rdevlin.cronin462b2fb2014-10-02 18:32:52415 if (cmd_line->HasSwitch(switches::kInstallEphemeralAppFromWebstore)) {
[email protected]820d9bd2013-04-03 03:46:03416 // The sole purpose of this launch is to install a new extension from CWS
417 // and immediately terminate: loading already installed extensions is
418 // unnecessary and may interfere with the inline install dialog (e.g. if an
419 // extension listens to onStartup and opens a window).
420 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25421 } else {
[email protected]47b60072013-06-20 10:35:56422 // LoadAllExtensions() calls OnLoadedInstalledExtensions().
423 component_loader_->LoadAll();
424 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]9f4e4f082013-06-21 07:11:19425
rkaplowa8fd8d32015-02-25 21:27:56426 EnabledReloadableExtensions();
427 MaybeFinishShutdownDelayed();
[email protected]47b60072013-06-20 10:35:56428 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25429
rkaplowa8fd8d32015-02-25 21:27:56430 // TODO(erikkay): this should probably be deferred to a future point
[email protected]47b60072013-06-20 10:35:56431 // rather than running immediately at startup.
432 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57433
[email protected]2d19eb6e2014-01-27 17:30:00434 LoadGreylistFromPrefs();
[email protected]399583b2012-12-11 09:33:42435 }
rkaplowa8fd8d32015-02-25 21:27:56436}
[email protected]3350d712013-11-18 09:32:24437
rkaplowa8fd8d32015-02-25 21:27:56438void ExtensionService::EnabledReloadableExtensions() {
439 TRACE_EVENT0("browser,startup",
440 "ExtensionService::EnabledReloadableExtensions");
441
442 std::vector<std::string> extensions_to_enable;
443 const ExtensionSet& disabled_extensions = registry_->disabled_extensions();
444 for (ExtensionSet::const_iterator iter = disabled_extensions.begin();
445 iter != disabled_extensions.end(); ++iter) {
446 const Extension* e = iter->get();
447 if (extension_prefs_->GetDisableReasons(e->id()) ==
448 Extension::DISABLE_RELOAD) {
449 extensions_to_enable.push_back(e->id());
450 }
451 }
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
461 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> delayed_info(
462 extension_prefs_->GetAllDelayedInstallInfo());
463 for (size_t i = 0; i < delayed_info->size(); ++i) {
464 ExtensionInfo* info = delayed_info->at(i).get();
465 scoped_refptr<const Extension> extension(NULL);
466 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();
478 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> delayed_info2(
479 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
[email protected]f47f7172014-03-19 19:27:10487 scoped_ptr<ExtensionSet> all_extensions =
488 registry_->GenerateInstalledExtensionsSet();
[email protected]2d19eb6e2014-01-27 17:30:00489
490 for (ExtensionSet::const_iterator it = all_extensions->begin();
491 it != all_extensions->end(); ++it) {
492 extensions::BlacklistState state =
493 extension_prefs_->GetExtensionBlacklistState((*it)->id());
494 if (state == extensions::BLACKLISTED_SECURITY_VULNERABILITY ||
495 state == extensions::BLACKLISTED_POTENTIALLY_UNWANTED ||
496 state == extensions::BLACKLISTED_CWS_POLICY_VIOLATION)
497 greylist_.Insert(*it);
498 }
499}
500
ginkage553af3202015-02-04 12:39:09501bool ExtensionService::UpdateExtension(const extensions::CRXFileInfo& file,
[email protected]044e86992014-01-24 22:59:11502 bool file_ownership_passed,
[email protected]31bb5ee62012-09-12 22:58:40503 CrxInstaller** out_crx_installer) {
[email protected]a29a517a2011-01-21 21:11:12504 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]3c4abc82012-10-22 22:25:54505 if (browser_terminating_) {
506 LOG(WARNING) << "Skipping UpdateExtension due to browser shutdown";
507 // Leak the temp file at extension_path. We don't want to add to the disk
508 // I/O burden at shutdown, we can't rely on the I/O completing anyway, and
509 // the file is in the OS temp directory which should be cleaned up for us.
510 return false;
511 }
[email protected]a8af9fdb2010-10-28 21:52:20512
ginkage553af3202015-02-04 12:39:09513 const std::string& id = file.extension_id;
514
[email protected]3f213ad2012-07-26 23:39:41515 const extensions::PendingExtensionInfo* pending_extension_info =
[email protected]51a3bf8b2012-06-08 22:53:06516 pending_extension_manager()->GetById(id);
[email protected]8ef78fd2010-08-19 17:14:32517
[email protected]695b5712012-12-06 23:55:28518 const Extension* extension = GetInstalledExtension(id);
[email protected]51a3bf8b2012-06-08 22:53:06519 if (!pending_extension_info && !extension) {
[email protected]aa142702010-03-26 01:26:33520 LOG(WARNING) << "Will not update extension " << id
521 << " because it is not installed or pending";
522 // Delete extension_path since we're not creating a CrxInstaller
523 // that would do it for us.
[email protected]7f8f24f2012-11-15 19:40:14524 if (!GetFileTaskRunner()->PostTask(
525 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
[email protected]f8636f92013-08-09 21:02:37532 scoped_refptr<CrxInstaller> installer(
pkotwicz31eb1e8ec2014-10-08 15:26:46533 CrxInstaller::Create(this, scoped_ptr<ExtensionInstallPrompt>()));
[email protected]6dfbbf82010-03-12 23:09:16534 installer->set_expected_id(id);
ginkage47e603e2015-02-27 08:42:41535 installer->set_expected_hash(file.expected_hash);
[email protected]464213a2013-10-15 01:06:48536 int creation_flags = Extension::NO_FLAGS;
[email protected]51a3bf8b2012-06-08 22:53:06537 if (pending_extension_info) {
538 installer->set_install_source(pending_extension_info->install_source());
pkotwicz31eb1e8ec2014-10-08 15:26:46539 installer->set_allow_silent_install(true);
[email protected]21db9ef2014-05-16 02:06:27540 if (pending_extension_info->remote_install())
541 installer->set_grant_permissions(false);
[email protected]464213a2013-10-15 01:06:48542 creation_flags = pending_extension_info->creation_flags();
543 if (pending_extension_info->mark_acknowledged())
[email protected]374ceb6f2014-07-02 19:25:34544 external_install_manager_->AcknowledgeExternalExtension(id);
treib69a7e012015-05-21 09:37:57545
546 // If the extension came in disabled due to a permission increase, then
547 // don't grant it all the permissions. crbug.com/484214
548 if (extensions::ExtensionPrefs::Get(profile_)->HasDisableReason(
549 id, Extension::DISABLE_PERMISSIONS_INCREASE)) {
550 installer->set_grant_permissions(false);
551 }
[email protected]51a3bf8b2012-06-08 22:53:06552 } else if (extension) {
[email protected]5eb375e92010-11-26 07:50:41553 installer->set_install_source(extension->location());
[email protected]51a3bf8b2012-06-08 22:53:06554 }
[email protected]7d8b7072012-04-07 01:07:46555 // If the extension was installed from or has migrated to the webstore, or
[email protected]fcb2c2c2012-10-17 21:08:45556 // its auto-update URL is from the webstore, treat it as a webstore install.
557 // Note that we ignore some older extensions with blank auto-update URLs
558 // because we are mostly concerned with restrictions on NaCl extensions,
559 // which are newer.
[email protected]75764512011-12-19 19:54:28560 if ((extension && extension->from_webstore()) ||
[email protected]4a5fe3e22013-06-04 07:06:38561 (extension && extensions::ManifestURL::UpdatesFromGallery(extension)) ||
[email protected]963d13c2012-09-29 17:13:35562 (!extension && extension_urls::IsWebstoreUpdateUrl(
[email protected]51a3bf8b2012-06-08 22:53:06563 pending_extension_info->update_url()))) {
[email protected]a12ce8b22012-01-17 18:40:53564 creation_flags |= Extension::FROM_WEBSTORE;
565 }
566
567 // Bookmark apps being updated is kind of a contradiction, but that's because
568 // we mark the default apps as bookmark apps, and they're hosted in the web
569 // store, thus they can get updated. See http://crbug.com/101605 for more
570 // details.
571 if (extension && extension->from_bookmark())
572 creation_flags |= Extension::FROM_BOOKMARK;
573
[email protected]e33bbc22012-08-27 22:05:46574 if (extension && extension->was_installed_by_default())
575 creation_flags |= Extension::WAS_INSTALLED_BY_DEFAULT;
576
[email protected]acc3c7c22014-03-19 06:23:39577 if (extension && extension->was_installed_by_oem())
578 creation_flags |= Extension::WAS_INSTALLED_BY_OEM;
579
[email protected]6338fa32014-07-16 21:41:59580 if (extension && extension->was_installed_by_custodian())
581 creation_flags |= Extension::WAS_INSTALLED_BY_CUSTODIAN;
582
[email protected]658eae52014-06-14 20:28:05583 if (extension) {
[email protected]411f8ae2014-05-22 11:12:23584 installer->set_is_ephemeral(extension_prefs_->IsEphemeralApp(id));
[email protected]c30bda262014-06-19 04:10:13585 installer->set_do_not_sync(extension_prefs_->DoNotSync(id));
[email protected]658eae52014-06-14 20:28:05586 }
[email protected]ab6c7be42014-01-16 02:05:54587
[email protected]a12ce8b22012-01-17 18:40:53588 installer->set_creation_flags(creation_flags);
589
[email protected]044e86992014-01-24 22:59:11590 installer->set_delete_source(file_ownership_passed);
[email protected]cb0e50312011-05-09 15:03:07591 installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
ginkage553af3202015-02-04 12:39:09592 installer->InstallCrxFile(file);
[email protected]420a0ec2011-06-01 01:07:03593
594 if (out_crx_installer)
[email protected]dc24976f2013-06-02 21:15:09595 *out_crx_installer = installer.get();
[email protected]420a0ec2011-06-01 01:07:03596
597 return true;
[email protected]e957fe52009-06-23 16:51:05598}
599
[email protected]bca4b832014-07-17 20:22:34600void ExtensionService::ReloadExtensionImpl(
[email protected]757d60a2014-05-23 00:11:44601 // "transient" because the process of reloading may cause the reference
602 // to become invalid. Instead, use |extension_id|, a copy.
[email protected]bca4b832014-07-17 20:22:34603 const std::string& transient_extension_id,
604 bool be_noisy) {
[email protected]a29a517a2011-01-21 21:11:12605 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]2a947202013-03-06 04:58:05606
607 // If the extension is already reloading, don't reload again.
[email protected]757d60a2014-05-23 00:11:44608 if (extension_prefs_->GetDisableReasons(transient_extension_id) &
[email protected]2a947202013-03-06 04:58:05609 Extension::DISABLE_RELOAD) {
610 return;
611 }
612
mlerman6a37b6a42014-11-26 22:10:53613 // Ignore attempts to reload a blacklisted or blocked extension. Sometimes
614 // this can happen in a convoluted reload sequence triggered by the
615 // termination of a blacklisted or blocked extension and a naive attempt to
616 // reload it. For an example see http://crbug.com/373842.
617 if (registry_->blacklisted_extensions().Contains(transient_extension_id) ||
618 registry_->blocked_extensions().Contains(transient_extension_id)) {
[email protected]757d60a2014-05-23 00:11:44619 return;
mlerman6a37b6a42014-11-26 22:10:53620 }
[email protected]757d60a2014-05-23 00:11:44621
[email protected]650b2d52013-02-10 03:41:45622 base::FilePath path;
[email protected]757d60a2014-05-23 00:11:44623
624 std::string extension_id = transient_extension_id;
625 const Extension* transient_current_extension =
626 GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40627
[email protected]f17dbd42010-08-16 23:21:10628 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]757d60a2014-05-23 00:11:44629 if (transient_current_extension) {
[email protected]4814b512009-11-07 00:12:29630 // If the extension has an inspector open for its background page, detach
631 // the inspector and hang onto a cookie for it, so that we can reattach
632 // later.
[email protected]31d8f5f22012-04-02 15:22:08633 // TODO(yoz): this is not incognito-safe!
reillyg0ea3fa902014-10-28 15:30:23634 extensions::ProcessManager* manager =
635 extensions::ProcessManager::Get(profile_);
[email protected]3a1dc572012-07-31 22:25:13636 extensions::ExtensionHost* host =
637 manager->GetBackgroundHostForExtension(extension_id);
[email protected]b3f957e62014-08-08 10:09:02638 if (host && DevToolsAgentHost::HasFor(host->host_contents())) {
[email protected]4814b512009-11-07 00:12:29639 // Look for an open inspector for the background page.
[email protected]04ea1bb2013-07-10 09:26:09640 scoped_refptr<DevToolsAgentHost> agent_host =
[email protected]b3f957e62014-08-08 10:09:02641 DevToolsAgentHost::GetOrCreateFor(host->host_contents());
642 agent_host->DisconnectWebContents();
[email protected]04ea1bb2013-07-10 09:26:09643 orphaned_dev_tools_[extension_id] = agent_host;
[email protected]4814b512009-11-07 00:12:29644 }
645
[email protected]757d60a2014-05-23 00:11:44646 path = transient_current_extension->path();
[email protected]58076192013-07-19 19:43:50647 // BeingUpgraded is set back to false when the extension is added.
rdevlin.cronin2c16c602014-11-21 01:35:49648 system_->runtime_data()->SetBeingUpgraded(transient_current_extension->id(),
[email protected]757d60a2014-05-23 00:11:44649 true);
[email protected]44d62b62012-04-11 00:06:03650 DisableExtension(extension_id, Extension::DISABLE_RELOAD);
[email protected]b914e2952013-04-26 07:10:03651 reloading_extensions_.insert(extension_id);
[email protected]1eb175082010-02-10 09:26:16652 } else {
[email protected]d32c02a2014-06-16 22:58:36653 std::map<std::string, base::FilePath>::const_iterator iter =
654 unloaded_extension_paths_.find(extension_id);
655 if (iter == unloaded_extension_paths_.end()) {
656 return;
657 }
[email protected]1eb175082010-02-10 09:26:16658 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06659 }
660
[email protected]757d60a2014-05-23 00:11:44661 transient_current_extension = NULL;
662
[email protected]9f4e4f082013-06-21 07:11:19663 if (delayed_installs_.Contains(extension_id)) {
[email protected]6f6101832012-11-27 22:10:48664 FinishDelayedInstallation(extension_id);
[email protected]0db124b02012-11-07 04:55:05665 return;
666 }
667
[email protected]43ceb002012-02-10 23:19:15668 // If we're reloading a component extension, use the component extension
669 // loader's reloader.
670 if (component_loader_->Exists(extension_id)) {
671 component_loader_->Reload(extension_id);
672 return;
673 }
674
[email protected]e6090e42010-03-23 22:44:08675 // Check the installed extensions to see if what we're reloading was already
676 // installed.
677 scoped_ptr<ExtensionInfo> installed_extension(
678 extension_prefs_->GetInstalledExtensionInfo(extension_id));
679 if (installed_extension.get() &&
680 installed_extension->extension_manifest.get()) {
[email protected]d8c8f25f2011-11-02 18:18:01681 extensions::InstalledLoader(this).Load(*installed_extension, false);
[email protected]e6090e42010-03-23 22:44:08682 } else {
[email protected]d8c8f25f2011-11-02 18:18:01683 // Otherwise, the extension is unpacked (location LOAD).
[email protected]e6090e42010-03-23 22:44:08684 // We should always be able to remember the extension's path. If it's not in
685 // the map, someone failed to update |unloaded_extension_paths_|.
686 CHECK(!path.empty());
[email protected]bca4b832014-07-17 20:22:34687 scoped_refptr<extensions::UnpackedInstaller> unpacked_installer =
688 extensions::UnpackedInstaller::Create(this);
689 unpacked_installer->set_be_noisy_on_failure(be_noisy);
690 unpacked_installer->Load(path);
[email protected]e6090e42010-03-23 22:44:08691 }
[email protected]9cddd4702009-07-27 22:09:40692}
693
[email protected]bca4b832014-07-17 20:22:34694void ExtensionService::ReloadExtension(const std::string& extension_id) {
695 ReloadExtensionImpl(extension_id, true); // be_noisy
696}
697
698void ExtensionService::ReloadExtensionWithQuietFailure(
699 const std::string& extension_id) {
700 ReloadExtensionImpl(extension_id, false); // be_noisy
701}
702
[email protected]757d60a2014-05-23 00:11:44703bool ExtensionService::UninstallExtension(
704 // "transient" because the process of uninstalling may cause the reference
705 // to become invalid. Instead, use |extenson->id()|.
706 const std::string& transient_extension_id,
[email protected]e43c61f2014-07-20 21:46:34707 extensions::UninstallReason reason,
[email protected]42d58f62014-07-31 01:32:45708 const base::Closure& deletion_done_callback,
[email protected]757d60a2014-05-23 00:11:44709 base::string16* error) {
[email protected]a29a517a2011-01-21 21:11:12710 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20711
[email protected]757d60a2014-05-23 00:11:44712 scoped_refptr<const Extension> extension =
713 GetInstalledExtension(transient_extension_id);
[email protected]631cf822009-05-15 07:01:25714
[email protected]e7afe2452010-08-22 16:19:13715 // Callers should not send us nonexistent extensions.
[email protected]dc24976f2013-06-02 21:15:09716 CHECK(extension.get());
[email protected]9f1087e2009-06-15 17:29:32717
binjincccacef2014-10-13 19:00:20718 ManagementPolicy* by_policy = system_->management_policy();
[email protected]95da88c42011-03-31 10:07:33719 // Policy change which triggers an uninstall will always set
720 // |external_uninstall| to true so this is the only way to uninstall
721 // managed extensions.
[email protected]90878c52014-04-04 18:21:02722 // Shared modules being uninstalled will also set |external_uninstall| to true
723 // so that we can guarantee users don't uninstall a shared module.
724 // (crbug.com/273300)
725 // TODO(rdevlin.cronin): This is probably not right. We should do something
726 // else, like include an enum IS_INTERNAL_UNINSTALL or IS_USER_UNINSTALL so
727 // we don't do this.
[email protected]cc2f55c2014-07-08 02:19:04728 bool external_uninstall =
[email protected]e43c61f2014-07-20 21:46:34729 (reason == extensions::UNINSTALL_REASON_INTERNAL_MANAGEMENT) ||
dtseng9fb3d5b2015-02-23 17:24:17730 (reason == extensions::UNINSTALL_REASON_COMPONENT_REMOVED) ||
rdevlin.cronin5f6b69d2014-09-20 01:23:35731 (reason == extensions::UNINSTALL_REASON_REINSTALL) ||
[email protected]e43c61f2014-07-20 21:46:34732 (reason == extensions::UNINSTALL_REASON_ORPHANED_EXTERNAL_EXTENSION) ||
[email protected]6ef60012014-07-22 19:08:12733 (reason == extensions::UNINSTALL_REASON_ORPHANED_SHARED_MODULE) ||
734 (reason == extensions::UNINSTALL_REASON_SYNC &&
dtseng9fb3d5b2015-02-23 17:24:17735 extension->was_installed_by_custodian());
[email protected]65187152012-06-02 13:14:14736 if (!external_uninstall &&
binjincccacef2014-10-13 19:00:20737 (!by_policy->UserMayModifySettings(extension.get(), error) ||
738 by_policy->MustRemainInstalled(extension.get(), error))) {
[email protected]ad50def52011-10-19 23:17:07739 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:06740 extensions::NOTIFICATION_EXTENSION_UNINSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:53741 content::Source<Profile>(profile_),
[email protected]dc24976f2013-06-02 21:15:09742 content::Details<const Extension>(extension.get()));
[email protected]d6ebc9792011-04-07 18:18:33743 return false;
744 }
[email protected]95da88c42011-03-31 10:07:33745
[email protected]65f173552012-06-28 22:43:58746 syncer::SyncChange sync_change;
rdevlin.cronin5f6b69d2014-09-20 01:23:35747 // Don't sync the uninstall if we're going to reinstall the extension
748 // momentarily.
749 if (extension_sync_service_ &&
750 reason != extensions::UNINSTALL_REASON_REINSTALL) {
[email protected]f8aefb132013-10-30 09:29:52751 sync_change = extension_sync_service_->PrepareToSyncUninstallExtension(
752 extension.get(), is_ready());
[email protected]3bdba0d2011-08-23 07:17:30753 }
754
juncai33e462102015-05-18 20:48:44755 InstallVerifier::Get(GetBrowserContext())->Remove(extension->id());
[email protected]ffd2f79e2013-11-14 00:11:46756
[email protected]9b217652010-10-08 22:04:23757 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08758 extension->GetType(), 100);
treib2e0517f92015-05-07 23:15:36759 RecordPermissionMessagesHistogram(extension.get(), "Uninstall");
[email protected]9b217652010-10-08 22:04:23760
[email protected]831aa212010-03-26 13:55:19761 // Unload before doing more cleanup to ensure that nothing is hanging on to
762 // any of these resources.
[email protected]757d60a2014-05-23 00:11:44763 UnloadExtension(extension->id(), UnloadedExtensionInfo::REASON_UNINSTALL);
[email protected]831aa212010-03-26 13:55:19764
[email protected]9f1087e2009-06-15 17:29:32765 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]12075d12013-02-27 05:38:05766 if (!Manifest::IsUnpackedLocation(extension->location())) {
[email protected]7f8f24f2012-11-15 19:40:14767 if (!GetFileTaskRunner()->PostTask(
768 FROM_HERE,
[email protected]5a145e82014-05-29 22:19:07769 base::Bind(&ExtensionService::UninstallExtensionOnFileThread,
770 extension->id(),
771 profile_,
[email protected]85df9d12014-04-15 17:02:14772 install_directory_,
[email protected]5a145e82014-05-29 22:19:07773 extension->path())))
[email protected]14908b72011-04-20 06:54:36774 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32775 }
776
[email protected]42d58f62014-07-31 01:32:45777 extensions::DataDeleter::StartDeleting(
778 profile_, extension.get(), deletion_done_callback);
[email protected]0d6ec3a72011-09-02 02:09:43779
[email protected]757d60a2014-05-23 00:11:44780 UntrackTerminatedExtension(extension->id());
[email protected]211030342010-09-30 18:41:06781
782 // Notify interested parties that we've uninstalled this extension.
[email protected]e43c61f2014-07-20 21:46:34783 ExtensionRegistry::Get(profile_)
784 ->TriggerOnUninstalled(extension.get(), reason);
[email protected]d6ebc9792011-04-07 18:18:33785
rdevlin.cronin5f6b69d2014-09-20 01:23:35786 if (sync_change.IsValid()) {
[email protected]757d60a2014-05-23 00:11:44787 extension_sync_service_->ProcessSyncUninstallExtension(extension->id(),
[email protected]f8aefb132013-10-30 09:29:52788 sync_change);
[email protected]3bdba0d2011-08-23 07:17:30789 }
790
[email protected]757d60a2014-05-23 00:11:44791 delayed_installs_.Remove(extension->id());
[email protected]0db124b02012-11-07 04:55:05792
[email protected]757d60a2014-05-23 00:11:44793 extension_prefs_->OnExtensionUninstalled(
794 extension->id(), extension->location(), external_uninstall);
[email protected]d4eda592013-09-18 03:37:57795
[email protected]333b1de2011-09-12 18:28:50796 // Track the uninstallation.
[email protected]49098f702011-10-13 03:47:18797 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionUninstalled", 1, 2);
798
[email protected]d6ebc9792011-04-07 18:18:33799 return true;
[email protected]c10da4b02010-03-25 14:38:32800}
801
[email protected]5a145e82014-05-29 22:19:07802// static
803void ExtensionService::UninstallExtensionOnFileThread(
804 const std::string& id,
805 Profile* profile,
806 const base::FilePath& install_dir,
807 const base::FilePath& extension_path) {
808 extensions::ExtensionAssetsManager* assets_manager =
809 extensions::ExtensionAssetsManager::GetInstance();
810 assets_manager->UninstallExtension(id, profile, install_dir, extension_path);
811}
812
[email protected]c3cfb012011-04-06 22:07:35813bool ExtensionService::IsExtensionEnabled(
814 const std::string& extension_id) const {
[email protected]bb1bc9b32013-12-21 03:09:14815 if (registry_->enabled_extensions().Contains(extension_id) ||
816 registry_->terminated_extensions().Contains(extension_id)) {
[email protected]36429da2011-07-11 20:25:18817 return true;
[email protected]dc9a74f72012-08-17 18:07:21818 }
[email protected]36429da2011-07-11 20:25:18819
[email protected]bb1bc9b32013-12-21 03:09:14820 if (registry_->disabled_extensions().Contains(extension_id) ||
mlerman6a37b6a42014-11-26 22:10:53821 registry_->blacklisted_extensions().Contains(extension_id) ||
822 registry_->blocked_extensions().Contains(extension_id)) {
[email protected]ad83ca242011-07-29 01:32:25823 return false;
[email protected]695b5712012-12-06 23:55:28824 }
[email protected]ad83ca242011-07-29 01:32:25825
mlerman3690e5be2014-12-01 22:57:44826 // Blocked extensions aren't marked as such in prefs, thus if
827 // |block_extensions_| is true then CanBlockExtension() must be called with an
828 // Extension object. If the |extension_id| is not loaded, assume not enabled.
829 if (block_extensions_) {
830 const Extension* extension = GetInstalledExtension(extension_id);
831 if (!extension || CanBlockExtension(extension))
832 return false;
833 }
mlerman6a37b6a42014-11-26 22:10:53834
[email protected]ad83ca242011-07-29 01:32:25835 // If the extension hasn't been loaded yet, check the prefs for it. Assume
836 // enabled unless otherwise noted.
837 return !extension_prefs_->IsExtensionDisabled(extension_id) &&
[email protected]27e528322014-05-27 20:54:30838 !extension_prefs_->IsExtensionBlacklisted(extension_id) &&
[email protected]f574c402012-12-04 23:20:31839 !extension_prefs_->IsExternalExtensionUninstalled(extension_id);
[email protected]c3cfb012011-04-06 22:07:35840}
841
[email protected]eaa7dd182010-12-14 11:09:00842void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12843 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20844
[email protected]06f92562011-04-29 19:27:31845 if (IsExtensionEnabled(extension_id))
[email protected]0c6da502009-08-14 22:32:39846 return;
[email protected]bb1bc9b32013-12-21 03:09:14847 const Extension* extension =
848 registry_->disabled_extensions().GetByID(extension_id);
[email protected]0d54b682013-11-05 14:15:36849
850 ManagementPolicy* policy = system_->management_policy();
851 if (extension && policy->MustRemainDisabled(extension, NULL, NULL)) {
852 UMA_HISTOGRAM_COUNTS_100("Extensions.EnableDeniedByPolicy", 1);
853 return;
854 }
[email protected]0c6da502009-08-14 22:32:39855
[email protected]b3317ad2011-04-28 23:46:00856 extension_prefs_->SetExtensionState(extension_id, Extension::ENABLED);
[email protected]eb5e4f92012-08-15 23:33:28857 extension_prefs_->ClearDisableReasons(extension_id);
[email protected]1784e83a2009-09-08 21:01:52858
[email protected]06f92562011-04-29 19:27:31859 // This can happen if sync enables an extension that is not
860 // installed yet.
861 if (!extension)
862 return;
863
[email protected]0c6da502009-08-14 22:32:39864 // Move it over to the enabled list.
[email protected]bb1bc9b32013-12-21 03:09:14865 registry_->AddEnabled(make_scoped_refptr(extension));
866 registry_->RemoveDisabled(extension->id());
[email protected]0c6da502009-08-14 22:32:39867
[email protected]62d30f42009-10-01 22:36:06868 NotifyExtensionLoaded(extension);
[email protected]3bdba0d2011-08-23 07:17:30869
[email protected]f74d7f32012-06-19 19:22:51870 // Notify listeners that the extension was enabled.
871 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:06872 extensions::NOTIFICATION_EXTENSION_ENABLED,
[email protected]f74d7f32012-06-19 19:22:51873 content::Source<Profile>(profile_),
874 content::Details<const Extension>(extension));
875
[email protected]f8aefb132013-10-30 09:29:52876 if (extension_sync_service_)
877 extension_sync_service_->SyncEnableExtension(*extension);
[email protected]0c6da502009-08-14 22:32:39878}
879
treibc1192322015-05-20 12:56:07880void ExtensionService::DisableExtension(const std::string& extension_id,
881 int disable_reasons) {
[email protected]a29a517a2011-01-21 21:11:12882 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20883
[email protected]8f959f522014-08-06 06:26:28884 // The extension may have been disabled already. Just add a disable reason.
885 if (!IsExtensionEnabled(extension_id)) {
treibc1192322015-05-20 12:56:07886 extension_prefs_->AddDisableReasons(extension_id, disable_reasons);
[email protected]1784e83a2009-09-08 21:01:52887 return;
[email protected]8f959f522014-08-06 06:26:28888 }
[email protected]1784e83a2009-09-08 21:01:52889
[email protected]06f92562011-04-29 19:27:31890 const Extension* extension = GetInstalledExtension(extension_id);
891 // |extension| can be NULL if sync disables an extension that is not
892 // installed yet.
[email protected]ab894542014-07-03 12:35:39893 // EXTERNAL_COMPONENT extensions are not generally modifiable by users, but
894 // can be uninstalled by the browser if the user sets extension-specific
895 // preferences.
[email protected]65187152012-06-02 13:14:14896 if (extension &&
treibc1192322015-05-20 12:56:07897 !(disable_reasons & Extension::DISABLE_RELOAD) &&
898 !(disable_reasons & Extension::DISABLE_UPDATE_REQUIRED_BY_POLICY) &&
[email protected]ab894542014-07-03 12:35:39899 !system_->management_policy()->UserMayModifySettings(extension, NULL) &&
900 extension->location() != Manifest::EXTERNAL_COMPONENT) {
[email protected]95da88c42011-03-31 10:07:33901 return;
[email protected]65187152012-06-02 13:14:14902 }
[email protected]95da88c42011-03-31 10:07:33903
[email protected]b3317ad2011-04-28 23:46:00904 extension_prefs_->SetExtensionState(extension_id, Extension::DISABLED);
treibc1192322015-05-20 12:56:07905 extension_prefs_->AddDisableReasons(extension_id, disable_reasons);
[email protected]1784e83a2009-09-08 21:01:52906
[email protected]599539802014-01-07 23:06:00907 int include_mask =
908 ExtensionRegistry::EVERYTHING & ~ExtensionRegistry::DISABLED;
909 extension = registry_->GetExtensionById(extension_id, include_mask);
[email protected]06f92562011-04-29 19:27:31910 if (!extension)
911 return;
912
[email protected]45f5b7d2014-01-22 23:47:13913 // The extension is either enabled or terminated.
914 DCHECK(registry_->enabled_extensions().Contains(extension->id()) ||
915 registry_->terminated_extensions().Contains(extension->id()));
[email protected]64742de32013-04-22 08:44:34916
[email protected]5c094792012-09-07 19:44:53917 // Move it over to the disabled list. Don't send a second unload notification
918 // for terminated extensions being disabled.
[email protected]bb1bc9b32013-12-21 03:09:14919 registry_->AddDisabled(make_scoped_refptr(extension));
920 if (registry_->enabled_extensions().Contains(extension->id())) {
921 registry_->RemoveEnabled(extension->id());
[email protected]b0af4792013-10-23 09:12:13922 NotifyExtensionUnloaded(extension, UnloadedExtensionInfo::REASON_DISABLE);
[email protected]5c094792012-09-07 19:44:53923 } else {
[email protected]bb1bc9b32013-12-21 03:09:14924 registry_->RemoveTerminated(extension->id());
[email protected]5c094792012-09-07 19:44:53925 }
[email protected]3bdba0d2011-08-23 07:17:30926
[email protected]f8aefb132013-10-30 09:29:52927 if (extension_sync_service_)
928 extension_sync_service_->SyncDisableExtension(*extension);
[email protected]1784e83a2009-09-08 21:01:52929}
930
[email protected]1abf05e2013-07-09 17:04:36931void ExtensionService::DisableUserExtensions(
932 const std::vector<std::string>& except_ids) {
[email protected]e516e4c2013-06-12 17:41:14933 extensions::ManagementPolicy* management_policy =
934 system_->management_policy();
935 extensions::ExtensionList to_disable;
936
[email protected]bb1bc9b32013-12-21 03:09:14937 const ExtensionSet& enabled_set = registry_->enabled_extensions();
938 for (ExtensionSet::const_iterator extension = enabled_set.begin();
939 extension != enabled_set.end(); ++extension) {
[email protected]ab894542014-07-03 12:35:39940 if (management_policy->UserMayModifySettings(extension->get(), NULL))
[email protected]e516e4c2013-06-12 17:41:14941 to_disable.push_back(*extension);
942 }
[email protected]bb1bc9b32013-12-21 03:09:14943 const ExtensionSet& terminated_set = registry_->terminated_extensions();
944 for (ExtensionSet::const_iterator extension = terminated_set.begin();
945 extension != terminated_set.end(); ++extension) {
[email protected]ab894542014-07-03 12:35:39946 if (management_policy->UserMayModifySettings(extension->get(), NULL))
[email protected]e516e4c2013-06-12 17:41:14947 to_disable.push_back(*extension);
948 }
949
950 for (extensions::ExtensionList::const_iterator extension = to_disable.begin();
951 extension != to_disable.end(); ++extension) {
[email protected]3082fe32013-08-06 11:12:38952 if ((*extension)->was_installed_by_default() &&
953 extension_urls::IsWebstoreUpdateUrl(
dchengc7047942014-08-26 05:05:31954 extensions::ManifestURL::GetUpdateURL(extension->get())))
[email protected]3082fe32013-08-06 11:12:38955 continue;
[email protected]1abf05e2013-07-09 17:04:36956 const std::string& id = (*extension)->id();
957 if (except_ids.end() == std::find(except_ids.begin(), except_ids.end(), id))
958 DisableExtension(id, extensions::Extension::DISABLE_USER_ACTION);
[email protected]e516e4c2013-06-12 17:41:14959 }
960}
961
mlerman6a37b6a42014-11-26 22:10:53962// Extensions that are not locked, components or forced by policy should be
963// locked. Extensions are no longer considered enabled or disabled. Blacklisted
964// extensions are now considered both blacklisted and locked.
965void ExtensionService::BlockAllExtensions() {
966 if (block_extensions_)
967 return;
968 block_extensions_ = true;
969
970 // Blacklisted extensions are already unloaded, need not be blocked.
971 scoped_ptr<ExtensionSet> extensions =
972 registry_->GenerateInstalledExtensionsSet(ExtensionRegistry::ENABLED |
973 ExtensionRegistry::DISABLED |
974 ExtensionRegistry::TERMINATED);
975
976 for (const scoped_refptr<const Extension>& extension : *extensions) {
977 const std::string& id = extension->id();
978
979 if (!CanBlockExtension(extension.get()))
980 continue;
981
982 registry_->RemoveEnabled(id);
983 registry_->RemoveDisabled(id);
984 registry_->RemoveTerminated(id);
985
986 registry_->AddBlocked(extension.get());
987 UnloadExtension(id, extensions::UnloadedExtensionInfo::REASON_LOCK_ALL);
988 }
989}
990
991// All locked extensions should revert to being either enabled or disabled
992// as appropriate.
993void ExtensionService::UnblockAllExtensions() {
994 block_extensions_ = false;
995 scoped_ptr<ExtensionSet> to_unblock =
996 registry_->GenerateInstalledExtensionsSet(ExtensionRegistry::BLOCKED);
997
998 for (const scoped_refptr<const Extension>& extension : *to_unblock) {
999 registry_->RemoveBlocked(extension->id());
1000 AddExtension(extension.get());
1001 }
1002}
1003
[email protected]eaa7dd182010-12-14 11:09:001004void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]009633c2013-03-07 22:08:281005 const Extension* extension) {
1006 GrantPermissions(extension);
treib2e0517f92015-05-07 23:15:361007 RecordPermissionMessagesHistogram(extension, "ReEnable");
[email protected]8d888c12010-11-30 00:00:251008 EnableExtension(extension->id());
1009}
1010
[email protected]009633c2013-03-07 22:08:281011void ExtensionService::GrantPermissions(const Extension* extension) {
[email protected]be083862012-09-01 03:53:451012 CHECK(extension);
[email protected]8d42db52014-06-20 21:50:501013 extensions::PermissionsUpdater(profile()).GrantActivePermissions(extension);
[email protected]be083862012-09-01 03:53:451014}
1015
[email protected]fe2dd7742011-04-19 22:52:491016// static
1017void ExtensionService::RecordPermissionMessagesHistogram(
[email protected]13c68b62013-05-17 11:29:051018 const Extension* extension, const char* histogram) {
[email protected]de415552013-01-23 04:12:171019 // Since this is called from multiple sources, and since the histogram macros
1020 // use statics, we need to manually lookup the histogram ourselves.
treib2e0517f92015-05-07 23:15:361021 base::HistogramBase* legacy_counter = base::LinearHistogram::FactoryGet(
1022 base::StringPrintf("Extensions.Permissions_%s2", histogram),
[email protected]fe2dd7742011-04-19 22:52:491023 1,
[email protected]c2e66e12012-06-27 06:27:061024 PermissionMessage::kEnumBoundary,
1025 PermissionMessage::kEnumBoundary + 1,
[email protected]de415552013-01-23 04:12:171026 base::HistogramBase::kUmaTargetedHistogramFlag);
[email protected]fe2dd7742011-04-19 22:52:491027
treib2e0517f92015-05-07 23:15:361028 // TODO(treib): Remove the legacy "2" histograms. See crbug.com/484102.
1029 PermissionMessageIDs legacy_permissions =
treibb566ee832015-03-16 10:20:461030 extension->permissions_data()->GetLegacyPermissionMessageIDs();
treib2e0517f92015-05-07 23:15:361031 if (legacy_permissions.empty()) {
1032 legacy_counter->Add(PermissionMessage::kNone);
[email protected]fe2dd7742011-04-19 22:52:491033 } else {
treib2e0517f92015-05-07 23:15:361034 for (PermissionMessage::ID id : legacy_permissions)
1035 legacy_counter->Add(id);
[email protected]fe2dd7742011-04-19 22:52:491036 }
treib2e0517f92015-05-07 23:15:361037
1038 base::HistogramBase* counter = base::LinearHistogram::FactoryGet(
1039 base::StringPrintf("Extensions.Permissions_%s3", histogram),
1040 1,
1041 APIPermission::kEnumBoundary,
1042 APIPermission::kEnumBoundary + 1,
1043 base::HistogramBase::kUmaTargetedHistogramFlag);
1044
1045 base::HistogramBase* counter_has_any = base::BooleanHistogram::FactoryGet(
1046 base::StringPrintf("Extensions.HasPermissions_%s3", histogram),
1047 base::HistogramBase::kUmaTargetedHistogramFlag);
1048
1049 PermissionIDSet permissions =
1050 extensions::PermissionMessageProvider::Get()->GetAllPermissionIDs(
1051 extension->permissions_data()->active_permissions().get(),
1052 extension->GetType());
1053 counter_has_any->AddBoolean(!permissions.empty());
1054 for (const PermissionID& id : permissions)
1055 counter->Add(id.id());
[email protected]fe2dd7742011-04-19 22:52:491056}
1057
[email protected]eaa7dd182010-12-14 11:09:001058void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]f9c8c7c72014-07-31 16:42:311059 // The URLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:061060 // was loaded, otherwise a race can arise where a renderer that is created
1061 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:421062 // that the request context doesn't yet know about. The profile is responsible
1063 // for ensuring its URLRequestContexts appropriately discover the loaded
1064 // extension.
[email protected]31d8f5f22012-04-02 15:22:081065 system_->RegisterExtensionWithRequestContexts(extension);
[email protected]62d30f42009-10-01 22:36:061066
[email protected]d5949312012-12-03 22:13:301067 // Tell renderers about the new extension, unless it's a theme (renderers
1068 // don't need to know about themes).
[email protected]a58f599c2012-12-01 03:08:191069 if (!extension->is_theme()) {
[email protected]19647262011-12-16 09:57:491070 for (content::RenderProcessHost::iterator i(
1071 content::RenderProcessHost::AllHostsIterator());
1072 !i.IsAtEnd(); i.Advance()) {
1073 content::RenderProcessHost* host = i.GetCurrentValue();
1074 Profile* host_profile =
1075 Profile::FromBrowserContext(host->GetBrowserContext());
1076 if (host_profile->GetOriginalProfile() ==
1077 profile_->GetOriginalProfile()) {
rdevlin.croninc491dbf2015-02-07 00:31:551078 // We don't need to include tab permisisons here, since the extension
1079 // was just loaded.
[email protected]19647262011-12-16 09:57:491080 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions(
rdevlin.croninc491dbf2015-02-07 00:31:551081 1, ExtensionMsg_Loaded_Params(extension,
1082 false /* no tab permissions */));
[email protected]19647262011-12-16 09:57:491083 host->Send(
1084 new ExtensionMsg_Loaded(loaded_extensions));
1085 }
[email protected]c8d407e2011-04-28 21:27:171086 }
[email protected]77a6970c2011-04-23 16:58:561087 }
1088
[email protected]3442a662012-01-12 08:06:171089 // Tell subsystems that use the EXTENSION_LOADED notification about the new
1090 // extension.
1091 //
1092 // NOTE: It is important that this happen after notifying the renderers about
1093 // the new extensions so that if we navigate to an extension URL in
[email protected]fdc76192014-04-20 21:54:411094 // ExtensionRegistryObserver::OnLoaded or
1095 // NOTIFICATION_EXTENSION_LOADED_DEPRECATED, the
[email protected]dcc47642014-03-26 22:03:491096 // renderer is guaranteed to know about it.
1097 registry_->TriggerOnLoaded(extension);
1098
[email protected]3442a662012-01-12 08:06:171099 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061100 extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
[email protected]3442a662012-01-12 08:06:171101 content::Source<Profile>(profile_),
1102 content::Details<const Extension>(extension));
1103
[email protected]dcc47642014-03-26 22:03:491104 // TODO(kalman): Convert ExtensionSpecialStoragePolicy to a
1105 // BrowserContextKeyedService and use ExtensionRegistryObserver.
[email protected]c8d407e2011-04-28 21:27:171106 profile_->GetExtensionSpecialStoragePolicy()->
rdevlin.croninbc8979a2014-09-30 01:08:501107 GrantRightsForExtension(extension, profile_);
[email protected]c8d407e2011-04-28 21:27:171108
[email protected]dcc47642014-03-26 22:03:491109 // TODO(kalman): This is broken. The crash reporter is process-wide so doesn't
1110 // work properly multi-profile. Besides which, it should be using
1111 // ExtensionRegistryObserver. See http://crbug.com/355029.
[email protected]c8d407e2011-04-28 21:27:171112 UpdateActiveExtensionsInCrashReporter();
1113
[email protected]d695bb12014-06-05 16:16:301114 const extensions::PermissionsData* permissions_data =
[email protected]a6910e72014-06-06 05:04:361115 extension->permissions_data();
[email protected]d695bb12014-06-05 16:16:301116
[email protected]c8d407e2011-04-28 21:27:171117 // If the extension has permission to load chrome://favicon/ resources we need
1118 // to make sure that the FaviconSource is registered with the
1119 // ChromeURLDataManager.
[email protected]d695bb12014-06-05 16:16:301120 if (permissions_data->HasHostPermission(GURL(chrome::kChromeUIFaviconURL))) {
[email protected]c8d407e2011-04-28 21:27:171121 FaviconSource* favicon_source = new FaviconSource(profile_,
1122 FaviconSource::FAVICON);
[email protected]24ea7a12013-01-27 23:54:531123 content::URLDataSource::Add(profile_, favicon_source);
[email protected]c8d407e2011-04-28 21:27:171124 }
[email protected]b07e606e2012-09-15 20:16:151125
[email protected]b07e606e2012-09-15 20:16:151126 // Same for chrome://theme/ resources.
[email protected]d695bb12014-06-05 16:16:301127 if (permissions_data->HasHostPermission(GURL(chrome::kChromeUIThemeURL))) {
[email protected]b07e606e2012-09-15 20:16:151128 ThemeSource* theme_source = new ThemeSource(profile_);
[email protected]24ea7a12013-01-27 23:54:531129 content::URLDataSource::Add(profile_, theme_source);
[email protected]b07e606e2012-09-15 20:16:151130 }
[email protected]b07e606e2012-09-15 20:16:151131
[email protected]5eddc3e2011-10-26 04:33:311132 // Same for chrome://thumb/ resources.
[email protected]d695bb12014-06-05 16:16:301133 if (permissions_data->HasHostPermission(
1134 GURL(chrome::kChromeUIThumbnailURL))) {
[email protected]420e6d92013-09-17 01:48:511135 ThumbnailSource* thumbnail_source = new ThumbnailSource(profile_, false);
[email protected]24ea7a12013-01-27 23:54:531136 content::URLDataSource::Add(profile_, thumbnail_source);
[email protected]5eddc3e2011-10-26 04:33:311137 }
[email protected]62d30f42009-10-01 22:36:061138}
1139
[email protected]a9f39a312010-12-23 22:14:271140void ExtensionService::NotifyExtensionUnloaded(
[email protected]814a7bf0f2011-08-13 05:30:591141 const Extension* extension,
[email protected]b0af4792013-10-23 09:12:131142 UnloadedExtensionInfo::Reason reason) {
[email protected]a9f39a312010-12-23 22:14:271143 UnloadedExtensionInfo details(extension, reason);
[email protected]e51232f32014-04-18 20:05:361144
1145 registry_->TriggerOnUnloaded(extension, reason);
1146
[email protected]ad50def52011-10-19 23:17:071147 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061148 extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
[email protected]6c2381d2011-10-19 02:52:531149 content::Source<Profile>(profile_),
1150 content::Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061151
[email protected]f3b1a082011-11-18 00:34:301152 for (content::RenderProcessHost::iterator i(
1153 content::RenderProcessHost::AllHostsIterator());
[email protected]77a6970c2011-04-23 16:58:561154 !i.IsAtEnd(); i.Advance()) {
[email protected]f3b1a082011-11-18 00:34:301155 content::RenderProcessHost* host = i.GetCurrentValue();
[email protected]9b62ecf2011-07-27 20:23:081156 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:301157 Profile::FromBrowserContext(host->GetBrowserContext());
[email protected]9b62ecf2011-07-27 20:23:081158 if (host_profile->GetOriginalProfile() == profile_->GetOriginalProfile())
[email protected]c8d407e2011-04-28 21:27:171159 host->Send(new ExtensionMsg_Unloaded(extension->id()));
[email protected]77a6970c2011-04-23 16:58:561160 }
1161
[email protected]31d8f5f22012-04-02 15:22:081162 system_->UnregisterExtensionWithRequestContexts(extension->id(), reason);
[email protected]dcc47642014-03-26 22:03:491163
1164 // TODO(kalman): Convert ExtensionSpecialStoragePolicy to a
1165 // BrowserContextKeyedService and use ExtensionRegistryObserver.
[email protected]c8d407e2011-04-28 21:27:171166 profile_->GetExtensionSpecialStoragePolicy()->
1167 RevokeRightsForExtension(extension);
1168
[email protected]b777b332011-04-16 04:01:081169#if defined(OS_CHROMEOS)
[email protected]ac66e452013-01-11 09:21:231170 // Revoke external file access for the extension from its file system context.
1171 // It is safe to access the extension's storage partition at this point. The
1172 // storage partition may get destroyed only after the extension gets unloaded.
[email protected]3a746ec2014-03-15 05:30:561173 GURL site =
1174 extensions::util::GetSiteForExtensionId(extension->id(), profile_);
[email protected]cd501a72014-08-22 19:58:311175 storage::FileSystemContext* filesystem_context =
1176 BrowserContext::GetStoragePartitionForSite(profile_, site)
1177 ->GetFileSystemContext();
[email protected]f19bbf62013-07-09 01:22:321178 if (filesystem_context && filesystem_context->external_backend()) {
1179 filesystem_context->external_backend()->
[email protected]c8d407e2011-04-28 21:27:171180 RevokeAccessForExtension(extension->id());
[email protected]62d30f42009-10-01 22:36:061181 }
[email protected]c8d407e2011-04-28 21:27:171182#endif
1183
[email protected]dcc47642014-03-26 22:03:491184 // TODO(kalman): This is broken. The crash reporter is process-wide so doesn't
1185 // work properly multi-profile. Besides which, it should be using
1186 // ExtensionRegistryObserver::OnExtensionLoaded. See http://crbug.com/355029.
[email protected]c8d407e2011-04-28 21:27:171187 UpdateActiveExtensionsInCrashReporter();
[email protected]62d30f42009-10-01 22:36:061188}
1189
[email protected]599539802014-01-07 23:06:001190content::BrowserContext* ExtensionService::GetBrowserContext() const {
1191 // Implemented in the .cc file to avoid adding a profile.h dependency to
1192 // extension_service.h.
1193 return profile_;
1194}
1195
[email protected]25ae0152011-11-18 14:40:021196bool ExtensionService::is_ready() {
[email protected]4a10006a2013-05-17 23:18:351197 return ready_->is_signaled();
[email protected]25ae0152011-11-18 14:40:021198}
1199
[email protected]7f8f24f2012-11-15 19:40:141200base::SequencedTaskRunner* ExtensionService::GetFileTaskRunner() {
[email protected]dc24976f2013-06-02 21:15:091201 if (file_task_runner_.get())
1202 return file_task_runner_.get();
[email protected]7f8f24f2012-11-15 19:40:141203
1204 // We should be able to interrupt any part of extension install process during
1205 // shutdown. SKIP_ON_SHUTDOWN ensures that not started extension install tasks
1206 // will be ignored/deleted while we will block on started tasks.
1207 std::string token("ext_install-");
1208 token.append(profile_->GetPath().AsUTF8Unsafe());
1209 file_task_runner_ = BrowserThread::GetBlockingPool()->
1210 GetSequencedTaskRunnerWithShutdownBehavior(
1211 BrowserThread::GetBlockingPool()->GetNamedSequenceToken(token),
1212 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
[email protected]dc24976f2013-06-02 21:15:091213 return file_task_runner_.get();
[email protected]7f8f24f2012-11-15 19:40:141214}
1215
[email protected]4ee07c62012-08-21 12:40:421216void ExtensionService::CheckManagementPolicy() {
[email protected]0d54b682013-11-05 14:15:361217 std::vector<std::string> to_unload;
1218 std::map<std::string, Extension::DisableReason> to_disable;
binjin8e3d0182014-12-04 16:44:281219 std::vector<std::string> to_enable;
[email protected]695b5712012-12-06 23:55:281220
[email protected]0d54b682013-11-05 14:15:361221 // Loop through the extensions list, finding extensions we need to unload or
1222 // disable.
binjin8e3d0182014-12-04 16:44:281223 for (scoped_refptr<const Extension> extension :
1224 registry_->enabled_extensions()) {
1225 if (!system_->management_policy()->UserMayLoad(extension.get(), nullptr))
[email protected]0d54b682013-11-05 14:15:361226 to_unload.push_back(extension->id());
1227 Extension::DisableReason disable_reason = Extension::DISABLE_NONE;
1228 if (system_->management_policy()->MustRemainDisabled(
binjin8e3d0182014-12-04 16:44:281229 extension.get(), &disable_reason, nullptr))
[email protected]0d54b682013-11-05 14:15:361230 to_disable[extension->id()] = disable_reason;
[email protected]aa96d3a2010-08-21 08:45:251231 }
1232
binjin8e3d0182014-12-04 16:44:281233 extensions::ExtensionManagement* management =
1234 extensions::ExtensionManagementFactory::GetForBrowserContext(profile());
1235
1236 // Loop through the disabled extension list, find extensions to re-enable
1237 // automatically. These extensions are exclusive from the |to_disable| and
1238 // |to_unload| lists constructed above, since disabled_extensions() and
1239 // enabled_extensions() are supposed to be mutually exclusive.
1240 for (scoped_refptr<const Extension> extension :
1241 registry_->disabled_extensions()) {
1242 // Find all disabled extensions disabled due to minimum version requirement,
1243 // but now satisfying it.
1244 if (management->CheckMinimumVersion(extension.get(), nullptr) &&
1245 extension_prefs_->HasDisableReason(
1246 extension->id(), Extension::DISABLE_UPDATE_REQUIRED_BY_POLICY)) {
1247 // Is DISABLE_UPDATE_REQUIRED_BY_POLICY the *only* reason?
1248 if (extension_prefs_->GetDisableReasons(extension->id()) ==
1249 Extension::DISABLE_UPDATE_REQUIRED_BY_POLICY) {
1250 // We need to enable those disabled *only* due to minimum version
1251 // requirement.
1252 to_enable.push_back(extension->id());
1253 }
1254 extension_prefs_->RemoveDisableReason(
1255 extension->id(), Extension::DISABLE_UPDATE_REQUIRED_BY_POLICY);
1256 }
1257 }
1258
1259 for (const std::string& id : to_unload)
1260 UnloadExtension(id, UnloadedExtensionInfo::REASON_DISABLE);
[email protected]0d54b682013-11-05 14:15:361261
1262 for (std::map<std::string, Extension::DisableReason>::const_iterator i =
1263 to_disable.begin(); i != to_disable.end(); ++i)
1264 DisableExtension(i->first, i->second);
binjin8e3d0182014-12-04 16:44:281265
1266 // No extension is getting re-enabled here after disabling/unloading
1267 // because to_enable is mutually exclusive to to_disable + to_unload.
1268 for (const std::string& id : to_enable)
1269 EnableExtension(id);
1270
1271 if (updater_.get()) {
1272 // Find all extensions disabled due to minimum version requirement from
1273 // policy (including the ones that got disabled just now), and check
1274 // for update.
1275 extensions::ExtensionUpdater::CheckParams to_recheck;
1276 for (scoped_refptr<const Extension> extension :
1277 registry_->disabled_extensions()) {
1278 if (extension_prefs_->GetDisableReasons(extension->id()) ==
1279 Extension::DISABLE_UPDATE_REQUIRED_BY_POLICY) {
1280 // The minimum version check is the only thing holding this extension
1281 // back, so check if it can be updated to fix that.
1282 to_recheck.ids.push_back(extension->id());
1283 }
1284 }
1285 if (!to_recheck.ids.empty())
1286 updater_->CheckNow(to_recheck);
1287 }
[email protected]aa96d3a2010-08-21 08:45:251288}
1289
[email protected]31206602011-04-13 23:07:321290void ExtensionService::CheckForUpdatesSoon() {
[email protected]90878c52014-04-04 18:21:021291 // This can legitimately happen in unit tests.
1292 if (!updater_.get())
1293 return;
1294
1295 if (AreAllExternalProvidersReady()) {
1296 updater_->CheckSoon();
[email protected]c3cfb012011-04-06 22:07:351297 } else {
[email protected]90878c52014-04-04 18:21:021298 // Sync can start updating before all the external providers are ready
1299 // during startup. Start the update as soon as those providers are ready,
1300 // but not before.
1301 update_once_all_providers_are_ready_ = true;
[email protected]c3cfb012011-04-06 22:07:351302 }
1303}
1304
[email protected]8e4560b62011-01-14 10:09:141305// Some extensions will autoupdate themselves externally from Chrome. These
rkaplowa8fd8d32015-02-25 21:27:561306// are typically part of some larger client application package. To support
1307// these, the extension will register its location in the preferences file
[email protected]8e4560b62011-01-14 10:09:141308// (and also, on Windows, in the registry) and this code will periodically
1309// check that location for a .crx file, which it will then install locally if
1310// a new version is available.
rkaplowa8fd8d32015-02-25 21:27:561311// Errors are reported through ExtensionErrorReporter. Success is not
[email protected]8e4560b62011-01-14 10:09:141312// reported.
[email protected]eaa7dd182010-12-14 11:09:001313void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:121314 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
rkaplowa8fd8d32015-02-25 21:27:561315 TRACE_EVENT0("browser,startup", "ExtensionService::CheckForExternalUpdates");
1316 SCOPED_UMA_HISTOGRAM_TIMER("Extensions.CheckForExternalUpdatesTime");
[email protected]8e4560b62011-01-14 10:09:141317
1318 // Note that this installation is intentionally silent (since it didn't
1319 // go through the front-end). Extensions that are registered in this
1320 // way are effectively considered 'pre-bundled', and so implicitly
1321 // trusted. In general, if something has HKLM or filesystem access,
1322 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:121323
[email protected]8e4560b62011-01-14 10:09:141324 // Ask each external extension provider to give us a call back for each
1325 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
[email protected]5df038b2012-07-16 19:03:271326 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141327 for (i = external_extension_providers_.begin();
1328 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:271329 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:141330 provider->VisitRegisteredExtension();
1331 }
1332
[email protected]50067e52011-10-20 23:17:071333 // Do any required work that we would have done after completion of all
1334 // providers.
[email protected]373daf972014-04-10 01:50:441335 if (external_extension_providers_.empty())
[email protected]50067e52011-10-20 23:17:071336 OnAllExternalProvidersReady();
[email protected]9f1087e2009-06-15 17:29:321337}
1338
[email protected]50067e52011-10-20 23:17:071339void ExtensionService::OnExternalProviderReady(
[email protected]5df038b2012-07-16 19:03:271340 const extensions::ExternalProviderInterface* provider) {
[email protected]a29a517a2011-01-21 21:11:121341 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]50067e52011-10-20 23:17:071342 CHECK(provider->IsReady());
[email protected]a29a517a2011-01-21 21:11:121343
1344 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:141345 // if all of them are finished. So we check them first.
[email protected]94fde232012-04-27 10:22:301346 if (AreAllExternalProvidersReady())
1347 OnAllExternalProvidersReady();
1348}
1349
1350bool ExtensionService::AreAllExternalProvidersReady() const {
[email protected]5df038b2012-07-16 19:03:271351 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141352 for (i = external_extension_providers_.begin();
1353 i != external_extension_providers_.end(); ++i) {
[email protected]94fde232012-04-27 10:22:301354 if (!i->get()->IsReady())
1355 return false;
[email protected]8e4560b62011-01-14 10:09:141356 }
[email protected]94fde232012-04-27 10:22:301357 return true;
[email protected]50067e52011-10-20 23:17:071358}
1359
1360void ExtensionService::OnAllExternalProvidersReady() {
1361 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]94fde232012-04-27 10:22:301362 base::TimeDelta elapsed = base::Time::Now() - profile_->GetStartTime();
1363 UMA_HISTOGRAM_TIMES("Extension.ExternalProvidersReadyAfter", elapsed);
[email protected]50067e52011-10-20 23:17:071364
1365 // Install any pending extensions.
[email protected]94fde232012-04-27 10:22:301366 if (update_once_all_providers_are_ready_ && updater()) {
1367 update_once_all_providers_are_ready_ = false;
[email protected]4e9b59d2013-12-18 06:47:141368 extensions::ExtensionUpdater::CheckParams params;
1369 params.callback = external_updates_finished_callback_;
1370 updater()->CheckNow(params);
[email protected]8e4560b62011-01-14 10:09:141371 }
1372
1373 // Uninstall all the unclaimed extensions.
[email protected]45759612012-07-10 17:21:231374 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> extensions_info(
[email protected]8e4560b62011-01-14 10:09:141375 extension_prefs_->GetInstalledExtensionsInfo());
1376 for (size_t i = 0; i < extensions_info->size(); ++i) {
1377 ExtensionInfo* info = extensions_info->at(i).get();
[email protected]1d5e58b2013-01-31 08:41:401378 if (Manifest::IsExternalLocation(info->extension_location))
[email protected]8e4560b62011-01-14 10:09:141379 CheckExternalUninstall(info->extension_id);
1380 }
[email protected]e5af875f2011-10-10 21:09:141381
[email protected]373daf972014-04-10 01:50:441382 error_controller_->ShowErrorIfNeeded();
[email protected]a9aa5932012-01-25 08:27:401383
[email protected]374ceb6f2014-07-02 19:25:341384 external_install_manager_->UpdateExternalExtensionAlert();
[email protected]e5af875f2011-10-10 21:09:141385}
1386
[email protected]a9f39a312010-12-23 22:14:271387void ExtensionService::UnloadExtension(
1388 const std::string& extension_id,
[email protected]b0af4792013-10-23 09:12:131389 UnloadedExtensionInfo::Reason reason) {
[email protected]27e469a2010-01-11 20:35:091390 // Make sure the extension gets deleted after we return from this function.
[email protected]599539802014-01-07 23:06:001391 int include_mask =
1392 ExtensionRegistry::EVERYTHING & ~ExtensionRegistry::TERMINATED;
[email protected]9adb9692010-10-29 23:14:021393 scoped_refptr<const Extension> extension(
[email protected]599539802014-01-07 23:06:001394 registry_->GetExtensionById(extension_id, include_mask));
[email protected]631cf822009-05-15 07:01:251395
[email protected]fa0c96732010-11-17 00:14:231396 // This method can be called via PostTask, so the extension may have been
1397 // unloaded by the time this runs.
[email protected]dc24976f2013-06-02 21:15:091398 if (!extension.get()) {
[email protected]dd163fb02011-05-04 22:22:171399 // In case the extension may have crashed/uninstalled. Allow the profile to
1400 // clean up its RequestContexts.
[email protected]31d8f5f22012-04-02 15:22:081401 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]fa0c96732010-11-17 00:14:231402 return;
[email protected]dd163fb02011-05-04 22:22:171403 }
[email protected]0c6da502009-08-14 22:32:391404
[email protected]1eb175082010-02-10 09:26:161405 // Keep information about the extension so that we can reload it later
1406 // even if it's not permanently installed.
1407 unloaded_extension_paths_[extension->id()] = extension->path();
1408
[email protected]f17dbd42010-08-16 23:21:101409 // Clean up if the extension is meant to be enabled after a reload.
[email protected]b914e2952013-04-26 07:10:031410 reloading_extensions_.erase(extension->id());
[email protected]f17dbd42010-08-16 23:21:101411
[email protected]bb1bc9b32013-12-21 03:09:141412 if (registry_->disabled_extensions().Contains(extension->id())) {
1413 registry_->RemoveDisabled(extension->id());
[email protected]dd163fb02011-05-04 22:22:171414 // Make sure the profile cleans up its RequestContexts when an already
1415 // disabled extension is unloaded (since they are also tracking the disabled
1416 // extensions).
[email protected]31d8f5f22012-04-02 15:22:081417 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]45f5b7d2014-01-22 23:47:131418 // Don't send the unloaded notification. It was sent when the extension
1419 // was disabled.
[email protected]60a174a2013-08-02 20:29:281420 } else {
[email protected]bb1bc9b32013-12-21 03:09:141421 // Remove the extension from the enabled list.
1422 registry_->RemoveEnabled(extension->id());
[email protected]60a174a2013-08-02 20:29:281423 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]0c6da502009-08-14 22:32:391424 }
1425
[email protected]fb789532013-08-27 02:40:211426 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061427 extensions::NOTIFICATION_EXTENSION_REMOVED,
[email protected]fb789532013-08-27 02:40:211428 content::Source<Profile>(profile_),
1429 content::Details<const Extension>(extension.get()));
[email protected]631cf822009-05-15 07:01:251430}
1431
[email protected]8b1ec202013-09-05 02:09:501432void ExtensionService::RemoveComponentExtension(
1433 const std::string& extension_id) {
1434 scoped_refptr<const Extension> extension(
1435 GetExtensionById(extension_id, false));
[email protected]b0af4792013-10-23 09:12:131436 UnloadExtension(extension_id, UnloadedExtensionInfo::REASON_UNINSTALL);
[email protected]ebe07772014-05-22 04:16:061437 if (extension.get()) {
[email protected]e43c61f2014-07-20 21:46:341438 ExtensionRegistry::Get(profile_)->TriggerOnUninstalled(
dtseng9fb3d5b2015-02-23 17:24:171439 extension.get(), extensions::UNINSTALL_REASON_COMPONENT_REMOVED);
[email protected]ebe07772014-05-22 04:16:061440 }
[email protected]8b1ec202013-09-05 02:09:501441}
1442
[email protected]bb1bc9b32013-12-21 03:09:141443void ExtensionService::UnloadAllExtensionsForTest() {
1444 UnloadAllExtensionsInternal();
[email protected]9f1087e2009-06-15 17:29:321445}
1446
[email protected]bb1bc9b32013-12-21 03:09:141447void ExtensionService::ReloadExtensionsForTest() {
1448 // Calling UnloadAllExtensionsForTest here triggers a false-positive presubmit
1449 // warning about calling test code in production.
1450 UnloadAllExtensionsInternal();
[email protected]eac88332012-12-26 17:57:451451 component_loader_->LoadAll();
[email protected]d8c8f25f2011-11-02 18:18:011452 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]4a10006a2013-05-17 23:18:351453 // Don't call SetReadyAndNotifyListeners() since tests call this multiple
1454 // times.
[email protected]9f1087e2009-06-15 17:29:321455}
1456
[email protected]820d9bd2013-04-03 03:46:031457void ExtensionService::SetReadyAndNotifyListeners() {
rkaplowa8fd8d32015-02-25 21:27:561458 TRACE_EVENT0("browser,startup",
1459 "ExtensionService::SetReadyAndNotifyListeners");
yiyaoliu252f915e2015-04-23 21:56:361460 TRACK_SCOPED_REGION(
1461 "Startup", "ExtensionService::SetReadyAndNotifyListeners");
rkaplowa8fd8d32015-02-25 21:27:561462 SCOPED_UMA_HISTOGRAM_TIMER(
1463 "Extensions.ExtensionServiceNotifyReadyListenersTime");
1464
[email protected]4a10006a2013-05-17 23:18:351465 ready_->Signal();
[email protected]820d9bd2013-04-03 03:46:031466 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061467 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED,
[email protected]820d9bd2013-04-03 03:46:031468 content::Source<Profile>(profile_),
1469 content::NotificationService::NoDetails());
[email protected]820d9bd2013-04-03 03:46:031470}
1471
[email protected]eaa7dd182010-12-14 11:09:001472void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]3eeddd892013-04-17 17:00:111473 if (updater_)
[email protected]93fd78f42009-07-10 16:43:171474 updater_->Start();
[email protected]fa6a9102010-11-22 15:38:501475
[email protected]bc151cf92013-02-12 04:57:261476 OnBlacklistUpdated();
[email protected]e72e8eb82009-06-18 17:21:511477}
1478
[email protected]fcb58a862012-05-01 01:03:151479void ExtensionService::AddExtension(const Extension* extension) {
[email protected]c8d407e2011-04-28 21:27:171480 // TODO(jstritar): We may be able to get rid of this branch by overriding the
1481 // default extension state to DISABLED when the --disable-extensions flag
1482 // is set (http://crbug.com/29067).
1483 if (!extensions_enabled() &&
1484 !extension->is_theme() &&
[email protected]1d5e58b2013-01-31 08:41:401485 extension->location() != Manifest::COMPONENT &&
1486 !Manifest::IsExternalLocation(extension->location())) {
[email protected]fcb58a862012-05-01 01:03:151487 return;
[email protected]330840c2012-08-29 22:21:011488 }
[email protected]c8d407e2011-04-28 21:27:171489
[email protected]b914e2952013-04-26 07:10:031490 bool is_extension_upgrade = false;
[email protected]116d40e2013-08-08 17:23:171491 bool is_extension_installed = false;
1492 const Extension* old = GetInstalledExtension(extension->id());
1493 if (old) {
1494 is_extension_installed = true;
1495 int version_compare_result =
1496 extension->version()->CompareTo(*(old->version()));
1497 is_extension_upgrade = version_compare_result > 0;
[email protected]b914e2952013-04-26 07:10:031498 // Other than for unpacked extensions, CrxInstaller should have guaranteed
1499 // that we aren't downgrading.
1500 if (!Manifest::IsUnpackedLocation(extension->location()))
[email protected]116d40e2013-08-08 17:23:171501 CHECK_GE(version_compare_result, 0);
[email protected]b914e2952013-04-26 07:10:031502 }
rdevlin.cronin2c16c602014-11-21 01:35:491503 // If the extension was disabled for a reload, then enable it.
1504 bool reloading = reloading_extensions_.erase(extension->id()) > 0;
1505
1506 // Set the upgraded bit; we consider reloads upgrades.
1507 system_->runtime_data()->SetBeingUpgraded(extension->id(),
1508 is_extension_upgrade || reloading);
[email protected]c8d407e2011-04-28 21:27:171509
[email protected]1eb175082010-02-10 09:26:161510 // The extension is now loaded, remove its data from unloaded extension map.
1511 unloaded_extension_paths_.erase(extension->id());
1512
[email protected]bb7f40952011-01-13 00:21:201513 // If a terminated extension is loaded, remove it from the terminated list.
1514 UntrackTerminatedExtension(extension->id());
1515
[email protected]b914e2952013-04-26 07:10:031516 // Check if the extension's privileges have changed and mark the
1517 // extension disabled if necessary.
[email protected]116d40e2013-08-08 17:23:171518 CheckPermissionsIncrease(extension, is_extension_installed);
[email protected]b914e2952013-04-26 07:10:031519
[email protected]116d40e2013-08-08 17:23:171520 if (is_extension_installed && !reloading) {
[email protected]ca2e82022013-04-29 16:48:411521 // To upgrade an extension in place, unload the old one and then load the
1522 // new one. ReloadExtension disables the extension, which is sufficient.
[email protected]b0af4792013-10-23 09:12:131523 UnloadExtension(extension->id(), UnloadedExtensionInfo::REASON_UPDATE);
[email protected]b914e2952013-04-26 07:10:031524 }
[email protected]0c6da502009-08-14 22:32:391525
[email protected]695b5712012-12-06 23:55:281526 if (extension_prefs_->IsExtensionBlacklisted(extension->id())) {
[email protected]ac875372013-02-28 04:36:091527 // Only prefs is checked for the blacklist. We rely on callers to check the
1528 // blacklist before calling into here, e.g. CrxInstaller checks before
[email protected]9f3c8532013-07-31 19:52:071529 // installation then threads through the install and pending install flow
1530 // of this class, and we check when loading installed extensions.
[email protected]bb1bc9b32013-12-21 03:09:141531 registry_->AddBlacklisted(extension);
mlerman6a37b6a42014-11-26 22:10:531532 } else if (block_extensions_ && CanBlockExtension(extension)) {
1533 registry_->AddBlocked(extension);
[email protected]ca2e82022013-04-29 16:48:411534 } else if (!reloading &&
1535 extension_prefs_->IsExtensionDisabled(extension->id())) {
[email protected]bb1bc9b32013-12-21 03:09:141536 registry_->AddDisabled(extension);
[email protected]f8aefb132013-10-30 09:29:521537 if (extension_sync_service_)
1538 extension_sync_service_->SyncExtensionChangeIfNeeded(*extension);
[email protected]44d62b62012-04-11 00:06:031539 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061540 extensions::NOTIFICATION_EXTENSION_UPDATE_DISABLED,
[email protected]44d62b62012-04-11 00:06:031541 content::Source<Profile>(profile_),
1542 content::Details<const Extension>(extension));
[email protected]62f051c2012-03-29 17:04:441543
[email protected]21db9ef2014-05-16 02:06:271544 // Show the extension disabled error if a permissions increase or a remote
1545 // installation is the reason it was disabled, and no other reasons exist.
1546 int reasons = extension_prefs_->GetDisableReasons(extension->id());
1547 const int kReasonMask = Extension::DISABLE_PERMISSIONS_INCREASE |
1548 Extension::DISABLE_REMOTE_INSTALL;
1549 if (reasons & kReasonMask && !(reasons & ~kReasonMask)) {
1550 extensions::AddExtensionDisabledError(
1551 this,
1552 extension,
1553 extension_prefs_->HasDisableReason(
1554 extension->id(), Extension::DISABLE_REMOTE_INSTALL));
[email protected]62f051c2012-03-29 17:04:441555 }
[email protected]ca2e82022013-04-29 16:48:411556 } else if (reloading) {
1557 // Replace the old extension with the new version.
[email protected]bb1bc9b32013-12-21 03:09:141558 CHECK(!registry_->AddDisabled(extension));
[email protected]ca2e82022013-04-29 16:48:411559 EnableExtension(extension->id());
[email protected]695b5712012-12-06 23:55:281560 } else {
1561 // All apps that are displayed in the launcher are ordered by their ordinals
1562 // so we must ensure they have valid ordinals.
1563 if (extension->RequiresSortOrdinal()) {
[email protected]db378322014-07-21 23:19:341564 extension_prefs_->app_sorting()->SetExtensionVisible(
1565 extension->id(),
1566 extension->ShouldDisplayInNewTabPage() &&
1567 !extension_prefs_->IsEphemeralApp(extension->id()));
1568 if (!extension_prefs_->IsEphemeralApp(extension->id())) {
1569 extension_prefs_->app_sorting()->EnsureValidOrdinals(
1570 extension->id(), syncer::StringOrdinal());
[email protected]bd36b652013-02-27 02:13:251571 }
[email protected]695b5712012-12-06 23:55:281572 }
1573
[email protected]bb1bc9b32013-12-21 03:09:141574 registry_->AddEnabled(extension);
[email protected]f8aefb132013-10-30 09:29:521575 if (extension_sync_service_)
1576 extension_sync_service_->SyncExtensionChangeIfNeeded(*extension);
[email protected]695b5712012-12-06 23:55:281577 NotifyExtensionLoaded(extension);
[email protected]f574c402012-12-04 23:20:311578 }
rdevlin.cronin2c16c602014-11-21 01:35:491579 system_->runtime_data()->SetBeingUpgraded(extension->id(), false);
[email protected]aab98a52009-12-02 03:22:351580}
1581
[email protected]8c484b742012-11-29 06:05:361582void ExtensionService::AddComponentExtension(const Extension* extension) {
1583 const std::string old_version_string(
1584 extension_prefs_->GetVersionString(extension->id()));
[email protected]c5e4a2222014-01-03 16:06:131585 const Version old_version(old_version_string);
[email protected]8c484b742012-11-29 06:05:361586
[email protected]0181a7d2013-11-12 01:08:421587 VLOG(1) << "AddComponentExtension " << extension->name();
[email protected]8c484b742012-11-29 06:05:361588 if (!old_version.IsValid() || !old_version.Equals(*extension->version())) {
1589 VLOG(1) << "Component extension " << extension->name() << " ("
1590 << extension->id() << ") installing/upgrading from '"
1591 << old_version_string << "' to " << extension->version()->GetString();
1592
1593 AddNewOrUpdatedExtension(extension,
[email protected]9ae73962014-08-14 16:53:411594 Extension::ENABLED,
[email protected]4a1d9c0d2014-06-13 12:50:111595 extensions::kInstallFlagNone,
[email protected]d8fd0fd2014-03-24 13:16:061596 syncer::StringOrdinal(),
1597 std::string());
[email protected]8c484b742012-11-29 06:05:361598 return;
1599 }
1600
1601 AddExtension(extension);
1602}
1603
[email protected]b914e2952013-04-26 07:10:031604void ExtensionService::CheckPermissionsIncrease(const Extension* extension,
[email protected]116d40e2013-08-08 17:23:171605 bool is_extension_installed) {
[email protected]23a85362014-07-07 23:26:191606 extensions::PermissionsUpdater(profile_).InitializePermissions(extension);
[email protected]902fd7b2011-07-27 18:42:311607
[email protected]8d888c12010-11-30 00:00:251608 // We keep track of all permissions the user has granted each extension.
1609 // This allows extensions to gracefully support backwards compatibility
1610 // by including unknown permissions in their manifests. When the user
1611 // installs the extension, only the recognized permissions are recorded.
1612 // When the unknown permissions become recognized (e.g., through browser
1613 // upgrade), we can prompt the user to accept these new permissions.
1614 // Extensions can also silently upgrade to less permissions, and then
1615 // silently upgrade to a version that adds these permissions back.
1616 //
1617 // For example, pretend that Chrome 10 includes a permission "omnibox"
1618 // for an API that adds suggestions to the omnibox. An extension can
1619 // maintain backwards compatibility while still having "omnibox" in the
1620 // manifest. If a user installs the extension on Chrome 9, the browser
1621 // will record the permissions it recognized, not including "omnibox."
1622 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
1623 // will disable the extension and prompt the user to approve the increase
1624 // in privileges. The extension could then release a new version that
1625 // removes the "omnibox" permission. When the user upgrades, Chrome will
1626 // still remember that "omnibox" had been granted, so that if the
1627 // extension once again includes "omnibox" in an upgrade, the extension
1628 // can upgrade without requiring this user's approval.
[email protected]eb5e4f92012-08-15 23:33:281629 int disable_reasons = extension_prefs_->GetDisableReasons(extension->id());
[email protected]8d888c12010-11-30 00:00:251630
rockot1d4a06262015-02-17 19:39:281631 // Silently grant all active permissions to default apps and apps installed
1632 // in kiosk mode.
[email protected]211a3f32013-05-28 21:48:181633 bool auto_grant_permission =
rockot1d4a06262015-02-17 19:39:281634 extension->was_installed_by_default() ||
[email protected]45928b682013-11-28 08:20:271635 extensions::ExtensionsBrowserClient::Get()->IsRunningInForcedAppMode();
[email protected]211a3f32013-05-28 21:48:181636 if (auto_grant_permission)
[email protected]b914e2952013-04-26 07:10:031637 GrantPermissions(extension);
1638
1639 bool is_privilege_increase = false;
1640 // We only need to compare the granted permissions to the current permissions
rockot1d4a06262015-02-17 19:39:281641 // if the extension has not been auto-granted its permissions above and is
1642 // installed internally.
1643 if (extension->location() == Manifest::INTERNAL && !auto_grant_permission) {
[email protected]8d888c12010-11-30 00:00:251644 // Add all the recognized permissions if the granted permissions list
1645 // hasn't been initialized yet.
[email protected]c2e66e12012-06-27 06:27:061646 scoped_refptr<PermissionSet> granted_permissions =
[email protected]902fd7b2011-07-27 18:42:311647 extension_prefs_->GetGrantedPermissions(extension->id());
[email protected]0d3e4a22011-06-23 19:02:521648 CHECK(granted_permissions.get());
[email protected]8d888c12010-11-30 00:00:251649
1650 // Here, we check if an extension's privileges have increased in a manner
1651 // that requires the user's approval. This could occur because the browser
1652 // upgraded and recognized additional privileges, or an extension upgrades
1653 // to a version that requires additional privileges.
[email protected]c41003472013-10-19 15:37:251654 is_privilege_increase =
1655 extensions::PermissionMessageProvider::Get()->IsPrivilegeIncrease(
dchengc7047942014-08-26 05:05:311656 granted_permissions.get(),
[email protected]076ebeda2014-06-06 21:47:261657 extension->permissions_data()->active_permissions().get(),
1658 extension->GetType());
[email protected]8d888c12010-11-30 00:00:251659 }
1660
[email protected]116d40e2013-08-08 17:23:171661 if (is_extension_installed) {
[email protected]44d62b62012-04-11 00:06:031662 // If the extension was already disabled, suppress any alerts for becoming
1663 // disabled on permissions increase.
[email protected]b914e2952013-04-26 07:10:031664 bool previously_disabled =
1665 extension_prefs_->IsExtensionDisabled(extension->id());
[email protected]7e9e2422012-12-13 19:54:191666 // Legacy disabled extensions do not have a disable reason. Infer that if
1667 // there was no permission increase, it was likely disabled by the user.
1668 if (previously_disabled && disable_reasons == Extension::DISABLE_NONE &&
[email protected]b914e2952013-04-26 07:10:031669 !extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
[email protected]7e9e2422012-12-13 19:54:191670 disable_reasons |= Extension::DISABLE_USER_ACTION;
[email protected]44d62b62012-04-11 00:06:031671 }
[email protected]7e9e2422012-12-13 19:54:191672 // Extensions that came to us disabled from sync need a similar inference,
1673 // except based on the new version's permissions.
1674 if (previously_disabled &&
1675 disable_reasons == Extension::DISABLE_UNKNOWN_FROM_SYNC) {
1676 // Remove the DISABLE_UNKNOWN_FROM_SYNC reason.
1677 extension_prefs_->ClearDisableReasons(extension->id());
1678 if (!is_privilege_increase)
1679 disable_reasons |= Extension::DISABLE_USER_ACTION;
1680 }
[email protected]b914e2952013-04-26 07:10:031681 disable_reasons &= ~Extension::DISABLE_UNKNOWN_FROM_SYNC;
[email protected]8d888c12010-11-30 00:00:251682 }
1683
1684 // Extension has changed permissions significantly. Disable it. A
[email protected]21db9ef2014-05-16 02:06:271685 // notification should be sent by the caller. If the extension is already
1686 // disabled because it was installed remotely, don't add another disable
treibaac30ec2015-06-10 09:18:091687 // reason.
1688 if (is_privilege_increase &&
1689 disable_reasons != Extension::DISABLE_REMOTE_INSTALL) {
[email protected]7e9e2422012-12-13 19:54:191690 disable_reasons |= Extension::DISABLE_PERMISSIONS_INCREASE;
[email protected]fe2dd7742011-04-19 22:52:491691 if (!extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
treib2e0517f92015-05-07 23:15:361692 RecordPermissionMessagesHistogram(extension, "AutoDisable");
[email protected]fe2dd7742011-04-19 22:52:491693 }
[email protected]195c1582013-10-29 18:42:231694 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
treib8ecc1eb52015-03-04 18:29:061695
1696#if defined(ENABLE_SUPERVISED_USERS)
1697 // If a custodian-installed extension is disabled for a supervised user due
1698 // to a permissions increase, send a request to the custodian, since the
treibf832a992015-03-24 18:09:241699 // supervised user themselves can't re-enable the extension.
treib8ecc1eb52015-03-04 18:29:061700 if (extensions::util::IsExtensionSupervised(extension, profile_)) {
1701 SupervisedUserService* supervised_user_service =
1702 SupervisedUserServiceFactory::GetForProfile(profile_);
1703 supervised_user_service->AddExtensionUpdateRequest(
treibf832a992015-03-24 18:09:241704 extension->id(), *extension->version(),
treib8ecc1eb52015-03-04 18:29:061705 base::Bind(ExtensionUpdateRequestSent, extension->id()));
1706 }
1707#endif
[email protected]b914e2952013-04-26 07:10:031708 }
1709 if (disable_reasons != Extension::DISABLE_NONE) {
[email protected]eb5e4f92012-08-15 23:33:281710 extension_prefs_->AddDisableReason(
1711 extension->id(),
1712 static_cast<Extension::DisableReason>(disable_reasons));
[email protected]8d888c12010-11-30 00:00:251713 }
1714}
1715
[email protected]eaa7dd182010-12-14 11:09:001716void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:391717 std::set<std::string> extension_ids;
[email protected]bb1bc9b32013-12-21 03:09:141718 const ExtensionSet& extensions = registry_->enabled_extensions();
1719 for (ExtensionSet::const_iterator iter = extensions.begin();
1720 iter != extensions.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361721 const Extension* extension = iter->get();
[email protected]1d5e58b2013-01-31 08:41:401722 if (!extension->is_theme() && extension->location() != Manifest::COMPONENT)
[email protected]84df8332011-12-06 18:22:461723 extension_ids.insert(extension->id());
[email protected]aab98a52009-12-02 03:22:351724 }
1725
[email protected]dcc47642014-03-26 22:03:491726 // TODO(kalman): This is broken. ExtensionService is per-profile.
1727 // crash_keys::SetActiveExtensions is per-process. See
1728 // http://crbug.com/355029.
[email protected]f6431be82013-09-07 02:53:451729 crash_keys::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:251730}
1731
[email protected]8266d662011-07-12 21:53:261732void ExtensionService::OnExtensionInstalled(
[email protected]36a5c4c2011-12-14 16:34:501733 const Extension* extension,
[email protected]98270432012-09-11 20:51:241734 const syncer::StringOrdinal& page_ordinal,
[email protected]4a1d9c0d2014-06-13 12:50:111735 int install_flags) {
[email protected]a29a517a2011-01-21 21:11:121736 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:201737
[email protected]c3cfb012011-04-06 22:07:351738 const std::string& id = extension->id();
binjin47947f842014-11-18 12:10:241739 int disable_reasons = GetDisableReasonsOnInstalled(extension);
[email protected]d8fd0fd2014-03-24 13:16:061740 std::string install_parameter;
[email protected]96aebe22014-07-16 04:07:511741 const extensions::PendingExtensionInfo* pending_extension_info =
1742 pending_extension_manager()->GetById(id);
1743 if (pending_extension_info) {
[email protected]8f3bcbd2013-06-05 08:42:401744 if (!pending_extension_info->ShouldAllowInstall(extension)) {
[email protected]51a3bf8b2012-06-08 22:53:061745 pending_extension_manager()->Remove(id);
[email protected]8ef78fd2010-08-19 17:14:321746
[email protected]31bb5ee62012-09-12 22:58:401747 LOG(WARNING) << "ShouldAllowInstall() returned false for "
1748 << id << " of type " << extension->GetType()
[email protected]65348062013-01-15 07:27:221749 << " and update URL "
1750 << extensions::ManifestURL::GetUpdateURL(extension).spec()
[email protected]31bb5ee62012-09-12 22:58:401751 << "; not installing";
[email protected]ae6cb3022011-04-04 20:11:541752
[email protected]4416c5a2010-06-26 01:28:571753 // Delete the extension directory since we're not going to
1754 // load it.
[email protected]7f8f24f2012-11-15 19:40:141755 if (!GetFileTaskRunner()->PostTask(
1756 FROM_HERE,
[email protected]85df9d12014-04-15 17:02:141757 base::Bind(&extensions::file_util::DeleteFile,
1758 extension->path(),
1759 true))) {
[email protected]14908b72011-04-20 06:54:361760 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:401761 }
[email protected]4416c5a2010-06-26 01:28:571762 return;
1763 }
[email protected]51a3bf8b2012-06-08 22:53:061764
[email protected]d8fd0fd2014-03-24 13:16:061765 install_parameter = pending_extension_info->install_parameter();
[email protected]51a3bf8b2012-06-08 22:53:061766 pending_extension_manager()->Remove(id);
[email protected]4416c5a2010-06-26 01:28:571767 } else {
[email protected]c3cfb012011-04-06 22:07:351768 // We explicitly want to re-enable an uninstalled external
1769 // extension; if we're here, that means the user is manually
1770 // installing the extension.
[email protected]46f3e102014-03-25 01:22:451771 if (extension_prefs_->IsExternalExtensionUninstalled(id)) {
binjin47947f842014-11-18 12:10:241772 disable_reasons = Extension::DISABLE_NONE;
[email protected]6cc7dbae2011-04-29 21:18:331773 }
[email protected]aa142702010-03-26 01:26:331774 }
1775
[email protected]98270432012-09-11 20:51:241776 // Unsupported requirements overrides the management policy.
[email protected]4a1d9c0d2014-06-13 12:50:111777 if (install_flags & extensions::kInstallFlagHasRequirementErrors) {
binjin47947f842014-11-18 12:10:241778 disable_reasons |= Extension::DISABLE_UNSUPPORTED_REQUIREMENT;
binjin8e3d0182014-12-04 16:44:281779 } else {
1780 // Requirement is supported now, remove the corresponding disable reason
1781 // instead.
1782 extension_prefs_->RemoveDisableReason(
1783 id, Extension::DISABLE_UNSUPPORTED_REQUIREMENT);
1784 disable_reasons &= ~Extension::DISABLE_UNSUPPORTED_REQUIREMENT;
1785 }
1786
1787 // Check if the extension was disabled because of the minimum version
1788 // requirements from enterprise policy, and satisfies it now.
1789 if (extensions::ExtensionManagementFactory::GetForBrowserContext(profile())
1790 ->CheckMinimumVersion(extension, nullptr)) {
1791 // And remove the corresponding disable reason.
1792 extension_prefs_->RemoveDisableReason(
1793 id, Extension::DISABLE_UPDATE_REQUIRED_BY_POLICY);
1794 disable_reasons &= ~Extension::DISABLE_UPDATE_REQUIRED_BY_POLICY;
[email protected]98270432012-09-11 20:51:241795 }
1796
[email protected]4a1d9c0d2014-06-13 12:50:111797 if (install_flags & extensions::kInstallFlagIsBlacklistedForMalware) {
[email protected]9f3c8532013-07-31 19:52:071798 // Installation of a blacklisted extension can happen from sync, policy,
1799 // etc, where to maintain consistency we need to install it, just never
1800 // load it (see AddExtension). Usually it should be the job of callers to
treibaac30ec2015-06-10 09:18:091801 // intercept blacklisted extensions earlier (e.g. CrxInstaller, before even
[email protected]9f3c8532013-07-31 19:52:071802 // showing the install dialogue).
[email protected]7c82539c2014-02-19 06:09:171803 extension_prefs_->AcknowledgeBlacklistedExtension(id);
[email protected]9f3c8532013-07-31 19:52:071804 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.SilentInstall",
1805 extension->location(),
1806 Manifest::NUM_LOCATIONS);
1807 }
1808
[email protected]695b5712012-12-06 23:55:281809 if (!GetInstalledExtension(extension->id())) {
[email protected]07533022011-06-27 20:42:551810 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
1811 extension->GetType(), 100);
[email protected]69084dc2012-11-17 07:39:301812 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallSource",
[email protected]1d5e58b2013-01-31 08:41:401813 extension->location(), Manifest::NUM_LOCATIONS);
treib2e0517f92015-05-07 23:15:361814 RecordPermissionMessagesHistogram(extension, "Install");
[email protected]69084dc2012-11-17 07:39:301815 } else {
1816 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateType",
1817 extension->GetType(), 100);
1818 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateSource",
[email protected]1d5e58b2013-01-31 08:41:401819 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]8f959f522014-08-06 06:26:281820
1821 // A fully installed app cannot be demoted to an ephemeral app.
1822 if ((install_flags & extensions::kInstallFlagIsEphemeral) &&
1823 !extension_prefs_->IsEphemeralApp(id)) {
1824 install_flags &= ~static_cast<int>(extensions::kInstallFlagIsEphemeral);
1825 }
[email protected]07533022011-06-27 20:42:551826 }
1827
binjin47947f842014-11-18 12:10:241828 if (disable_reasons)
treibaac30ec2015-06-10 09:18:091829 extension_prefs_->AddDisableReasons(id, disable_reasons);
binjin47947f842014-11-18 12:10:241830
[email protected]399583b2012-12-11 09:33:421831 const Extension::State initial_state =
binjin47947f842014-11-18 12:10:241832 disable_reasons == Extension::DISABLE_NONE ? Extension::ENABLED
1833 : Extension::DISABLED;
1834
[email protected]4a1d9c0d2014-06-13 12:50:111835 if (ShouldDelayExtensionUpdate(
1836 id,
1837 !!(install_flags & extensions::kInstallFlagInstallImmediately))) {
[email protected]9f3c8532013-07-31 19:52:071838 extension_prefs_->SetDelayedInstallInfo(
1839 extension,
1840 initial_state,
[email protected]4a1d9c0d2014-06-13 12:50:111841 install_flags,
[email protected]9f3c8532013-07-31 19:52:071842 extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE,
[email protected]d8fd0fd2014-03-24 13:16:061843 page_ordinal,
1844 install_parameter);
[email protected]0db124b02012-11-07 04:55:051845
1846 // Transfer ownership of |extension|.
[email protected]9f4e4f082013-06-21 07:11:191847 delayed_installs_.Insert(extension);
[email protected]0db124b02012-11-07 04:55:051848
[email protected]75bdcb872013-03-13 00:41:451849 // Notify observers that app update is available.
1850 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
[email protected]a9aa62b312013-11-29 05:35:061851 OnAppUpdateAvailable(extension));
[email protected]0db124b02012-11-07 04:55:051852 return;
1853 }
1854
[email protected]90878c52014-04-04 18:21:021855 extensions::SharedModuleService::ImportStatus status =
1856 shared_module_service_->SatisfyImports(extension);
[email protected]e5f8fffe2014-04-02 00:30:441857 if (installs_delayed_for_gc_) {
[email protected]9f3c8532013-07-31 19:52:071858 extension_prefs_->SetDelayedInstallInfo(
1859 extension,
1860 initial_state,
[email protected]4a1d9c0d2014-06-13 12:50:111861 install_flags,
[email protected]9f3c8532013-07-31 19:52:071862 extensions::ExtensionPrefs::DELAY_REASON_GC,
[email protected]d8fd0fd2014-03-24 13:16:061863 page_ordinal,
1864 install_parameter);
[email protected]399583b2012-12-11 09:33:421865 delayed_installs_.Insert(extension);
[email protected]90878c52014-04-04 18:21:021866 } else if (status != SharedModuleService::IMPORT_STATUS_OK) {
1867 if (status == SharedModuleService::IMPORT_STATUS_UNSATISFIED) {
[email protected]9f3c8532013-07-31 19:52:071868 extension_prefs_->SetDelayedInstallInfo(
1869 extension,
1870 initial_state,
[email protected]4a1d9c0d2014-06-13 12:50:111871 install_flags,
[email protected]9f4e4f082013-06-21 07:11:191872 extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IMPORTS,
[email protected]d8fd0fd2014-03-24 13:16:061873 page_ordinal,
1874 install_parameter);
[email protected]9f4e4f082013-06-21 07:11:191875 delayed_installs_.Insert(extension);
1876 }
[email protected]399583b2012-12-11 09:33:421877 } else {
[email protected]9f3c8532013-07-31 19:52:071878 AddNewOrUpdatedExtension(extension,
1879 initial_state,
[email protected]4a1d9c0d2014-06-13 12:50:111880 install_flags,
[email protected]d8fd0fd2014-03-24 13:16:061881 page_ordinal,
1882 install_parameter);
[email protected]399583b2012-12-11 09:33:421883 }
[email protected]8c484b742012-11-29 06:05:361884}
1885
binjin1569c9b2014-09-05 13:33:181886void ExtensionService::OnExtensionManagementSettingsChanged() {
1887 error_controller_->ShowErrorIfNeeded();
binjine6b58b52014-10-31 01:55:571888
1889 // Revokes blocked permissions from active_permissions for all extensions.
1890 extensions::ExtensionManagement* settings =
1891 extensions::ExtensionManagementFactory::GetForBrowserContext(profile());
1892 CHECK(settings);
1893 scoped_ptr<ExtensionSet> all_extensions(
1894 registry_->GenerateInstalledExtensionsSet());
1895 for (const auto& extension : *all_extensions.get()) {
1896 if (!settings->IsPermissionSetAllowed(
binjin685ade82014-11-06 09:53:561897 extension.get(),
binjine6b58b52014-10-31 01:55:571898 extension->permissions_data()->active_permissions())) {
1899 extensions::PermissionsUpdater(profile()).RemovePermissions(
1900 extension.get(),
binjin685ade82014-11-06 09:53:561901 settings->GetBlockedPermissions(extension.get()).get());
binjine6b58b52014-10-31 01:55:571902 }
1903 }
1904
binjin1569c9b2014-09-05 13:33:181905 CheckManagementPolicy();
1906}
1907
[email protected]8c484b742012-11-29 06:05:361908void ExtensionService::AddNewOrUpdatedExtension(
1909 const Extension* extension,
[email protected]399583b2012-12-11 09:33:421910 Extension::State initial_state,
[email protected]4a1d9c0d2014-06-13 12:50:111911 int install_flags,
[email protected]d8fd0fd2014-03-24 13:16:061912 const syncer::StringOrdinal& page_ordinal,
1913 const std::string& install_parameter) {
[email protected]8c484b742012-11-29 06:05:361914 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]760f743b2014-05-28 13:52:021915 bool was_ephemeral = extension_prefs_->IsEphemeralApp(extension->id());
[email protected]4a1d9c0d2014-06-13 12:50:111916 extension_prefs_->OnExtensionInstalled(
1917 extension, initial_state, page_ordinal, install_flags, install_parameter);
[email protected]9e9c1d12013-07-31 01:58:121918 delayed_installs_.Remove(extension->id());
[email protected]913ffca92014-03-27 15:26:161919 if (InstallVerifier::NeedsVerification(*extension))
juncai33e462102015-05-18 20:48:441920 InstallVerifier::Get(GetBrowserContext())->VerifyExtension(extension->id());
ryanackley48bedbd2015-01-27 23:12:141921
1922 const Extension* old = GetInstalledExtension(extension->id());
1923 if (extensions::AppDataMigrator::NeedsMigration(old, extension)) {
1924 app_data_migrator_->DoMigrationAndReply(
1925 old, extension,
1926 base::Bind(&ExtensionService::FinishInstallation, AsWeakPtr(),
1927 make_scoped_refptr(extension), was_ephemeral));
1928 return;
1929 }
1930
[email protected]760f743b2014-05-28 13:52:021931 FinishInstallation(extension, was_ephemeral);
[email protected]6f6101832012-11-27 22:10:481932}
1933
1934void ExtensionService::MaybeFinishDelayedInstallation(
1935 const std::string& extension_id) {
[email protected]9f4e4f082013-06-21 07:11:191936 // Check if the extension already got installed.
1937 if (!delayed_installs_.Contains(extension_id))
[email protected]6f6101832012-11-27 22:10:481938 return;
[email protected]9f4e4f082013-06-21 07:11:191939 extensions::ExtensionPrefs::DelayReason reason =
1940 extension_prefs_->GetDelayedInstallReason(extension_id);
1941
1942 // Check if the extension is idle. DELAY_REASON_NONE is used for older
1943 // preferences files that will not have set this field but it was previously
1944 // only used for idle updates.
1945 if ((reason == extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE ||
1946 reason == extensions::ExtensionPrefs::DELAY_REASON_NONE) &&
[email protected]1d5cf4142014-01-24 18:25:221947 is_ready() && !extensions::util::IsExtensionIdle(extension_id, profile_))
[email protected]6f6101832012-11-27 22:10:481948 return;
1949
[email protected]9f4e4f082013-06-21 07:11:191950 const Extension* extension = delayed_installs_.GetByID(extension_id);
1951 if (reason == extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IMPORTS) {
[email protected]90878c52014-04-04 18:21:021952 extensions::SharedModuleService::ImportStatus status =
1953 shared_module_service_->SatisfyImports(extension);
1954 if (status != SharedModuleService::IMPORT_STATUS_OK) {
1955 if (status == SharedModuleService::IMPORT_STATUS_UNRECOVERABLE) {
[email protected]9f4e4f082013-06-21 07:11:191956 delayed_installs_.Remove(extension_id);
1957 // Make sure no version of the extension is actually installed, (i.e.,
1958 // that this delayed install was not an update).
1959 CHECK(!extension_prefs_->GetInstalledExtensionInfo(extension_id).get());
1960 extension_prefs_->DeleteExtensionPrefs(extension_id);
1961 }
1962 return;
1963 }
1964 }
1965
[email protected]6f6101832012-11-27 22:10:481966 FinishDelayedInstallation(extension_id);
1967}
1968
1969void ExtensionService::FinishDelayedInstallation(
1970 const std::string& extension_id) {
1971 scoped_refptr<const Extension> extension(
1972 GetPendingExtensionUpdate(extension_id));
[email protected]dc24976f2013-06-02 21:15:091973 CHECK(extension.get());
[email protected]9f4e4f082013-06-21 07:11:191974 delayed_installs_.Remove(extension_id);
[email protected]6f6101832012-11-27 22:10:481975
[email protected]760f743b2014-05-28 13:52:021976 bool was_ephemeral = extension_prefs_->IsEphemeralApp(extension->id());
[email protected]399583b2012-12-11 09:33:421977 if (!extension_prefs_->FinishDelayedInstallInfo(extension_id))
[email protected]6f6101832012-11-27 22:10:481978 NOTREACHED();
1979
[email protected]760f743b2014-05-28 13:52:021980 FinishInstallation(extension.get(), was_ephemeral);
[email protected]6f6101832012-11-27 22:10:481981}
1982
[email protected]760f743b2014-05-28 13:52:021983void ExtensionService::FinishInstallation(
1984 const Extension* extension, bool was_ephemeral) {
[email protected]e7554c3f2013-05-29 00:36:561985 const extensions::Extension* existing_extension =
1986 GetInstalledExtension(extension->id());
1987 bool is_update = false;
1988 std::string old_name;
1989 if (existing_extension) {
1990 is_update = true;
1991 old_name = existing_extension->name();
1992 }
[email protected]760f743b2014-05-28 13:52:021993 bool from_ephemeral =
1994 was_ephemeral && !extension_prefs_->IsEphemeralApp(extension->id());
1995 extensions::InstalledExtensionInfo details(
1996 extension, is_update, from_ephemeral, old_name);
[email protected]fcb58a862012-05-01 01:03:151997 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061998 extensions::NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED,
[email protected]fcb58a862012-05-01 01:03:151999 content::Source<Profile>(profile_),
[email protected]41bb80bd2013-05-03 10:56:022000 content::Details<const extensions::InstalledExtensionInfo>(&details));
[email protected]fcb58a862012-05-01 01:03:152001
[email protected]bc44b5da2014-06-12 14:20:002002 registry_->TriggerOnWillBeInstalled(
[email protected]760f743b2014-05-28 13:52:022003 extension, is_update, from_ephemeral, old_name);
[email protected]17f07822014-05-22 08:45:152004
[email protected]399583b2012-12-11 09:33:422005 // Unpacked extensions default to allowing file access, but if that has been
2006 // overridden, don't reset the value.
[email protected]1d5e58b2013-01-31 08:41:402007 if (Manifest::ShouldAlwaysAllowFileAccess(extension->location()) &&
[email protected]399583b2012-12-11 09:33:422008 !extension_prefs_->HasAllowFileAccessSetting(extension->id())) {
2009 extension_prefs_->SetAllowFileAccess(extension->id(), true);
2010 }
2011
[email protected]6f6101832012-11-27 22:10:482012 AddExtension(extension);
[email protected]612a1cb12012-10-17 13:18:032013
[email protected]bc44b5da2014-06-12 14:20:002014 // Notify observers that need to know when an installation is complete.
[email protected]38e872532014-07-16 23:27:512015 registry_->TriggerOnInstalled(extension, is_update);
[email protected]bc44b5da2014-06-12 14:20:002016
[email protected]9f4e4f082013-06-21 07:11:192017 // Check extensions that may have been delayed only because this shared module
2018 // was not available.
[email protected]374ceb6f2014-07-02 19:25:342019 if (SharedModuleInfo::IsSharedModule(extension))
[email protected]9f4e4f082013-06-21 07:11:192020 MaybeFinishDelayedInstallations();
[email protected]4a190632009-05-09 01:07:422021}
2022
[email protected]760f743b2014-05-28 13:52:022023void ExtensionService::PromoteEphemeralApp(
2024 const extensions::Extension* extension, bool is_from_sync) {
2025 DCHECK(GetInstalledExtension(extension->id()) &&
2026 extension_prefs_->IsEphemeralApp(extension->id()));
2027
[email protected]db378322014-07-21 23:19:342028 if (extension->RequiresSortOrdinal()) {
2029 extension_prefs_->app_sorting()->SetExtensionVisible(
2030 extension->id(), extension->ShouldDisplayInNewTabPage());
2031
2032 if (!is_from_sync) {
[email protected]760f743b2014-05-28 13:52:022033 // Reset the sort ordinals of the app to ensure it is added to the default
2034 // position, like newly installed apps would.
2035 extension_prefs_->app_sorting()->ClearOrdinals(extension->id());
[email protected]760f743b2014-05-28 13:52:022036 }
2037
[email protected]db378322014-07-21 23:19:342038 extension_prefs_->app_sorting()->EnsureValidOrdinals(
2039 extension->id(), syncer::StringOrdinal());
2040 }
2041
[email protected]760f743b2014-05-28 13:52:022042 // Remove the ephemeral flags from the preferences.
2043 extension_prefs_->OnEphemeralAppPromoted(extension->id());
2044
2045 // Fire install-related events to allow observers to handle the promotion
2046 // of the ephemeral app.
2047 extensions::InstalledExtensionInfo details(
2048 extension,
2049 true /* is update */,
2050 true /* from ephemeral */,
2051 extension->name() /* old name */);
2052 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:062053 extensions::NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED,
[email protected]760f743b2014-05-28 13:52:022054 content::Source<Profile>(profile_),
2055 content::Details<const extensions::InstalledExtensionInfo>(&details));
2056
2057 registry_->TriggerOnWillBeInstalled(
2058 extension,
2059 true /* is update */,
2060 true /* from ephemeral */,
2061 extension->name() /* old name */);
2062
2063 if (registry_->enabled_extensions().Contains(extension->id())) {
[email protected]8f959f522014-08-06 06:26:282064 // If the app is already enabled and loaded, fire the load events to allow
2065 // observers to handle the promotion of the ephemeral app.
[email protected]760f743b2014-05-28 13:52:022066 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:062067 extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
[email protected]760f743b2014-05-28 13:52:022068 content::Source<Profile>(profile_),
2069 content::Details<const Extension>(extension));
2070
2071 registry_->TriggerOnLoaded(extension);
[email protected]8f959f522014-08-06 06:26:282072 } else {
2073 // Cached ephemeral apps may be updated and disabled due to permissions
2074 // increase. The app can be enabled (as long as no other disable reasons
2075 // exist) as the install was user-acknowledged.
2076 int disable_mask = Extension::DISABLE_NONE;
2077 if (!is_from_sync)
2078 disable_mask |= Extension::DISABLE_PERMISSIONS_INCREASE;
2079
2080 int other_disable_reasons =
2081 extension_prefs_->GetDisableReasons(extension->id()) & ~disable_mask;
2082 if (!other_disable_reasons) {
2083 if (extension_prefs_->DidExtensionEscalatePermissions(extension->id()))
2084 GrantPermissionsAndEnableExtension(extension);
2085 else
2086 EnableExtension(extension->id());
2087 }
[email protected]760f743b2014-05-28 13:52:022088 }
2089
[email protected]38e872532014-07-16 23:27:512090 registry_->TriggerOnInstalled(extension, true);
[email protected]bc44b5da2014-06-12 14:20:002091
[email protected]760f743b2014-05-28 13:52:022092 if (!is_from_sync && extension_sync_service_)
2093 extension_sync_service_->SyncExtensionChangeIfNeeded(*extension);
2094}
2095
[email protected]76b65442012-11-17 14:11:482096const Extension* ExtensionService::GetPendingExtensionUpdate(
2097 const std::string& id) const {
[email protected]9f4e4f082013-06-21 07:11:192098 return delayed_installs_.GetByID(id);
[email protected]76b65442012-11-17 14:11:482099}
2100
mukai87a8d402014-09-15 20:15:092101void ExtensionService::RegisterContentSettings(
2102 HostContentSettingsMap* host_content_settings_map) {
rkaplowe65c2ff2015-02-14 16:29:542103 TRACE_EVENT0("browser,startup", "ExtensionService::RegisterContentSettings");
mukaia1aa3512014-10-22 20:14:112104 DCHECK_CURRENTLY_ON(BrowserThread::UI);
mukai87a8d402014-09-15 20:15:092105 host_content_settings_map->RegisterProvider(
2106 HostContentSettingsMap::INTERNAL_EXTENSION_PROVIDER,
2107 scoped_ptr<content_settings::ObservableProvider>(
reillyga3acbc12014-11-11 23:17:122108 new content_settings::InternalExtensionProvider(profile_)));
mukai87a8d402014-09-15 20:15:092109
2110 host_content_settings_map->RegisterProvider(
2111 HostContentSettingsMap::CUSTOM_EXTENSION_PROVIDER,
2112 scoped_ptr<content_settings::ObservableProvider>(
2113 new content_settings::CustomExtensionProvider(
2114 extensions::ContentSettingsService::Get(
2115 profile_)->content_settings_store(),
2116 profile_->GetOriginalProfile() != profile_)));
2117}
2118
xiyuane87def2e2015-06-04 22:50:222119void ExtensionService::TrackTerminatedExtension(
2120 const std::string& extension_id) {
2121 extensions_being_terminated_.erase(extension_id);
2122
2123 const Extension* extension = GetInstalledExtension(extension_id);
2124 if (!extension) {
xiyuane87def2e2015-06-04 22:50:222125 return;
2126 }
2127
[email protected]bb1bc9b32013-12-21 03:09:142128 // No need to check for duplicates; inserting a duplicate is a no-op.
2129 registry_->AddTerminated(make_scoped_refptr(extension));
[email protected]b0af4792013-10-23 09:12:132130 UnloadExtension(extension->id(), UnloadedExtensionInfo::REASON_TERMINATE);
[email protected]bb7f40952011-01-13 00:21:202131}
2132
[email protected]e9d7496e2014-04-18 01:25:462133void ExtensionService::TerminateExtension(const std::string& extension_id) {
xiyuana30a2092015-06-09 23:14:442134 TrackTerminatedExtension(extension_id);
[email protected]e9d7496e2014-04-18 01:25:462135}
2136
[email protected]bb7f40952011-01-13 00:21:202137void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
[email protected]cb1f4ac2014-08-07 16:55:422138 std::string lowercase_id = base::StringToLowerASCII(id);
[email protected]bb1bc9b32013-12-21 03:09:142139 const Extension* extension =
2140 registry_->terminated_extensions().GetByID(lowercase_id);
2141 registry_->RemoveTerminated(lowercase_id);
[email protected]fb789532013-08-27 02:40:212142 if (extension) {
2143 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:062144 extensions::NOTIFICATION_EXTENSION_REMOVED,
[email protected]fb789532013-08-27 02:40:212145 content::Source<Profile>(profile_),
2146 content::Details<const Extension>(extension));
2147 }
[email protected]bb7f40952011-01-13 00:21:202148}
2149
[email protected]8001df22011-04-28 19:59:472150const Extension* ExtensionService::GetInstalledExtension(
2151 const std::string& id) const {
[email protected]599539802014-01-07 23:06:002152 return registry_->GetExtensionById(id, ExtensionRegistry::EVERYTHING);
[email protected]0dfe05c2011-02-23 23:03:362153}
2154
[email protected]9060d8b02012-01-13 02:14:302155bool ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:322156 const std::string& id,
[email protected]c5e4a2222014-01-03 16:06:132157 const Version* version,
[email protected]650b2d52013-02-10 03:41:452158 const base::FilePath& path,
[email protected]1d5e58b2013-01-31 08:41:402159 Manifest::Location location,
[email protected]47fc70c2011-12-06 07:29:512160 int creation_flags,
xiyuan4d82f7b62015-03-04 02:29:222161 bool mark_acknowledged,
2162 bool install_immediately) {
[email protected]ab22ba42011-01-14 16:36:382163 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]fdd28372014-08-21 02:27:262164 CHECK(crx_file::id_util::IdIsValid(id));
[email protected]79c833b52011-04-05 18:31:012165 if (extension_prefs_->IsExternalExtensionUninstalled(id))
[email protected]9060d8b02012-01-13 02:14:302166 return false;
[email protected]a8af9fdb2010-10-28 21:52:202167
[email protected]7577a5c52009-07-30 06:21:582168 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:492169 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:582170 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:022171 const Extension* existing = GetExtensionById(id, true);
[email protected]910f72ce2012-08-24 01:38:352172
[email protected]7577a5c52009-07-30 06:21:582173 if (existing) {
[email protected]910f72ce2012-08-24 01:38:352174 // The default apps will have the location set as INTERNAL. Since older
2175 // default apps are installed as EXTERNAL, we override them. However, if the
2176 // app is already installed as internal, then do the version check.
[email protected]31bb5ee62012-09-12 22:58:402177 // TODO(grv) : Remove after Q1-2013.
[email protected]910f72ce2012-08-24 01:38:352178 bool is_default_apps_migration =
[email protected]1d5e58b2013-01-31 08:41:402179 (location == Manifest::INTERNAL &&
2180 Manifest::IsExternalLocation(existing->location()));
[email protected]910f72ce2012-08-24 01:38:352181
2182 if (!is_default_apps_migration) {
2183 DCHECK(version);
2184
2185 switch (existing->version()->CompareTo(*version)) {
2186 case -1: // existing version is older, we should upgrade
2187 break;
2188 case 0: // existing version is same, do nothing
2189 return false;
2190 case 1: // existing version is newer, uh-oh
2191 LOG(WARNING) << "Found external version of extension " << id
2192 << "that is older than current version. Current version "
2193 << "is: " << existing->VersionString() << ". New "
2194 << "version is: " << version->GetString()
2195 << ". Keeping current version.";
2196 return false;
2197 }
[email protected]7577a5c52009-07-30 06:21:582198 }
2199 }
2200
[email protected]9060d8b02012-01-13 02:14:302201 // If the extension is already pending, don't start an install.
[email protected]e3987852012-05-04 10:06:302202 if (!pending_extension_manager()->AddFromExternalFile(
[email protected]464213a2013-10-15 01:06:482203 id, location, *version, creation_flags, mark_acknowledged)) {
[email protected]9060d8b02012-01-13 02:14:302204 return false;
[email protected]e3987852012-05-04 10:06:302205 }
[email protected]9c635f22010-12-02 09:36:362206
[email protected]14908b72011-04-20 06:54:362207 // no client (silent install)
[email protected]f8636f92013-08-09 21:02:372208 scoped_refptr<CrxInstaller> installer(CrxInstaller::CreateSilent(this));
[email protected]6dfbbf82010-03-12 23:09:162209 installer->set_install_source(location);
2210 installer->set_expected_id(id);
[email protected]cb0e50312011-05-09 15:03:072211 installer->set_expected_version(*version);
2212 installer->set_install_cause(extension_misc::INSTALL_CAUSE_EXTERNAL_FILE);
xiyuan4d82f7b62015-03-04 02:29:222213 installer->set_install_immediately(install_immediately);
[email protected]1bf73cc32011-10-26 22:38:312214 installer->set_creation_flags(creation_flags);
[email protected]88e8ec9152013-01-17 04:05:182215#if defined(OS_CHROMEOS)
2216 extensions::InstallLimiter::Get(profile_)->Add(installer, path);
2217#else
[email protected]6dfbbf82010-03-12 23:09:162218 installer->InstallCrx(path);
[email protected]88e8ec9152013-01-17 04:05:182219#endif
[email protected]47fc70c2011-12-06 07:29:512220
2221 // Depending on the source, a new external extension might not need a user
2222 // notification on installation. For such extensions, mark them acknowledged
2223 // now to suppress the notification.
2224 if (mark_acknowledged)
[email protected]374ceb6f2014-07-02 19:25:342225 external_install_manager_->AcknowledgeExternalExtension(id);
[email protected]9060d8b02012-01-13 02:14:302226
2227 return true;
[email protected]7577a5c52009-07-30 06:21:582228}
2229
[email protected]eaa7dd182010-12-14 11:09:002230void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]3a1dc572012-07-31 22:25:132231 extensions::ExtensionHost* host) {
[email protected]406027c02010-09-27 08:03:182232 OrphanedDevTools::iterator iter =
[email protected]2d2f6cfc2011-05-06 21:09:332233 orphaned_dev_tools_.find(host->extension_id());
[email protected]406027c02010-09-27 08:03:182234 if (iter == orphaned_dev_tools_.end())
2235 return;
2236
[email protected]b3f957e62014-08-08 10:09:022237 iter->second->ConnectWebContents(host->host_contents());
[email protected]406027c02010-09-27 08:03:182238 orphaned_dev_tools_.erase(iter);
2239}
2240
[email protected]432115822011-07-10 15:52:272241void ExtensionService::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:532242 const content::NotificationSource& source,
2243 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:272244 switch (type) {
[email protected]3c4abc82012-10-22 22:25:542245 case chrome::NOTIFICATION_APP_TERMINATING:
2246 // Shutdown has started. Don't start any more extension installs.
2247 // (We cannot use ExtensionService::Shutdown() for this because it
2248 // happens too late in browser teardown.)
2249 browser_terminating_ = true;
2250 break;
[email protected]adf5a102014-07-31 12:44:062251 case extensions::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]6c2381d2011-10-19 02:52:532252 if (profile_ !=
2253 content::Source<Profile>(source).ptr()->GetOriginalProfile()) {
[email protected]bc535ee52010-08-31 18:40:322254 break;
[email protected]6c2381d2011-10-19 02:52:532255 }
[email protected]a4ed6282009-12-14 20:51:162256
[email protected]3a1dc572012-07-31 22:25:132257 extensions::ExtensionHost* host =
2258 content::Details<extensions::ExtensionHost>(details).ptr();
[email protected]f128af42010-08-05 18:05:262259
[email protected]3964e572014-02-13 21:57:412260 // If the extension is already being terminated, there is nothing left to
2261 // do.
2262 if (!extensions_being_terminated_.insert(host->extension_id()).second)
2263 break;
2264
[email protected]fa2416f2011-05-03 08:41:202265 // Mark the extension as terminated and Unload it. We want it to
2266 // be in a consistent state: either fully working or not loaded
2267 // at all, but never half-crashed. We do it in a PostTask so
2268 // that other handlers of this notification will still have
2269 // access to the Extension and ExtensionHost.
skyostilf221b7de2015-06-11 20:36:322270 base::ThreadTaskRunnerHandle::Get()->PostTask(
2271 FROM_HERE, base::Bind(&ExtensionService::TrackTerminatedExtension,
2272 AsWeakPtr(), host->extension()->id()));
[email protected]31f77262009-12-02 20:48:532273 break;
2274 }
[email protected]432115822011-07-10 15:52:272275 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
[email protected]f3b1a082011-11-18 00:34:302276 content::RenderProcessHost* process =
2277 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192278 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302279 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192280 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2281 break;
2282
[email protected]fafdc842014-01-17 18:09:082283 extensions::ProcessMap* process_map =
2284 extensions::ProcessMap::Get(profile_);
2285 if (process_map->Contains(process->GetID())) {
[email protected]52b76592013-11-02 17:59:032286 // An extension process was terminated, this might have resulted in an
2287 // app or extension becoming idle.
2288 std::set<std::string> extension_ids =
[email protected]fafdc842014-01-17 18:09:082289 process_map->GetExtensionsInProcess(process->GetID());
elijahtaylor0def4432014-10-06 18:15:112290 // In addition to the extensions listed in the process map, one of those
2291 // extensions could be referencing a shared module which is waiting for
2292 // idle to update. Check all imports of these extensions, too.
2293 std::set<std::string> import_ids;
2294 for (std::set<std::string>::const_iterator it = extension_ids.begin();
2295 it != extension_ids.end();
2296 ++it) {
2297 const Extension* extension = GetExtensionById(*it, true);
2298 if (!extension)
2299 continue;
2300 const std::vector<SharedModuleInfo::ImportInfo>& imports =
2301 SharedModuleInfo::GetImports(extension);
2302 std::vector<SharedModuleInfo::ImportInfo>::const_iterator import_it;
2303 for (import_it = imports.begin(); import_it != imports.end();
2304 import_it++) {
2305 import_ids.insert((*import_it).extension_id);
2306 }
2307 }
2308 extension_ids.insert(import_ids.begin(), import_ids.end());
2309
[email protected]52b76592013-11-02 17:59:032310 for (std::set<std::string>::const_iterator it = extension_ids.begin();
2311 it != extension_ids.end(); ++it) {
2312 if (delayed_installs_.Contains(*it)) {
skyostilf221b7de2015-06-11 20:36:322313 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
[email protected]52b76592013-11-02 17:59:032314 FROM_HERE,
2315 base::Bind(&ExtensionService::MaybeFinishDelayedInstallation,
2316 AsWeakPtr(), *it),
2317 base::TimeDelta::FromSeconds(kUpdateIdleDelay));
2318 }
2319 }
2320 }
2321
[email protected]fafdc842014-01-17 18:09:082322 process_map->RemoveAllFromProcess(process->GetID());
[email protected]6f371442011-11-09 06:45:462323 BrowserThread::PostTask(
[email protected]38427a12013-11-09 17:34:202324 BrowserThread::IO,
2325 FROM_HERE,
2326 base::Bind(&extensions::InfoMap::UnregisterAllExtensionsInProcess,
[email protected]31d8f5f22012-04-02 15:22:082327 system_->info_map(),
[email protected]f3b1a082011-11-18 00:34:302328 process->GetID()));
[email protected]da5683db2011-04-23 17:12:212329 break;
2330 }
[email protected]92dd8d92013-02-26 00:41:082331 case chrome::NOTIFICATION_UPGRADE_RECOMMENDED: {
[email protected]75bdcb872013-03-13 00:41:452332 // Notify observers that chrome update is available.
2333 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
2334 OnChromeUpdateAvailable());
[email protected]92dd8d92013-02-26 00:41:082335 break;
2336 }
[email protected]ebe07772014-05-22 04:16:062337 case chrome::NOTIFICATION_PROFILE_DESTRUCTION_STARTED: {
2338 OnProfileDestructionStarted();
2339 break;
2340 }
[email protected]aa96d3a2010-08-21 08:45:252341
[email protected]4814b512009-11-07 00:12:292342 default:
2343 NOTREACHED() << "Unexpected notification type.";
2344 }
2345}
2346
binjin47947f842014-11-18 12:10:242347int ExtensionService::GetDisableReasonsOnInstalled(const Extension* extension) {
2348 Extension::DisableReason disable_reason;
2349 // Extensions disabled by management policy should always be disabled, even
2350 // if it's force-installed.
2351 if (system_->management_policy()->MustRemainDisabled(
2352 extension, &disable_reason, nullptr)) {
2353 // A specified reason is required to disable the extension.
2354 DCHECK(disable_reason != Extension::DISABLE_NONE);
2355 return disable_reason;
2356 }
2357
[email protected]612a1cb12012-10-17 13:18:032358 // Extensions installed by policy can't be disabled. So even if a previous
2359 // installation disabled the extension, make sure it is now enabled.
binjin47947f842014-11-18 12:10:242360 if (system_->management_policy()->MustRemainEnabled(extension, nullptr))
2361 return Extension::DISABLE_NONE;
[email protected]612a1cb12012-10-17 13:18:032362
binjin47947f842014-11-18 12:10:242363 // An already disabled extension should inherit the disable reasons and
2364 // remain disabled.
2365 if (extension_prefs_->IsExtensionDisabled(extension->id())) {
2366 int disable_reasons = extension_prefs_->GetDisableReasons(extension->id());
2367 // If an extension was disabled without specified reason, presume it's
2368 // disabled by user.
2369 return disable_reasons == Extension::DISABLE_NONE
2370 ? Extension::DISABLE_USER_ACTION
2371 : disable_reasons;
2372 }
[email protected]612a1cb12012-10-17 13:18:032373
[email protected]41070e8d2012-10-24 01:34:362374 if (FeatureSwitch::prompt_for_external_extensions()->IsEnabled()) {
[email protected]612a1cb12012-10-17 13:18:032375 // External extensions are initially disabled. We prompt the user before
[email protected]2c495c42013-01-04 21:49:542376 // enabling them. Hosted apps are excepted because they are not dangerous
2377 // (they need to be launched by the user anyway).
[email protected]1d5e58b2013-01-31 08:41:402378 if (extension->GetType() != Manifest::TYPE_HOSTED_APP &&
2379 Manifest::IsExternalLocation(extension->location()) &&
[email protected]612a1cb12012-10-17 13:18:032380 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id())) {
binjin47947f842014-11-18 12:10:242381 return Extension::DISABLE_EXTERNAL_EXTENSION;
[email protected]612a1cb12012-10-17 13:18:032382 }
2383 }
[email protected]612a1cb12012-10-17 13:18:032384
binjin47947f842014-11-18 12:10:242385 return Extension::DISABLE_NONE;
[email protected]612a1cb12012-10-17 13:18:032386}
[email protected]0db124b02012-11-07 04:55:052387
mlerman6a37b6a42014-11-26 22:10:532388// Helper method to determine if an extension can be blocked.
2389bool ExtensionService::CanBlockExtension(const Extension* extension) const {
mlerman3690e5be2014-12-01 22:57:442390 DCHECK(extension);
mlerman6a37b6a42014-11-26 22:10:532391 return extension->location() != Manifest::COMPONENT &&
2392 extension->location() != Manifest::EXTERNAL_COMPONENT &&
2393 !system_->management_policy()->MustRemainEnabled(extension, NULL);
2394}
2395
[email protected]e7aa7b7e2012-11-27 04:51:222396bool ExtensionService::ShouldDelayExtensionUpdate(
2397 const std::string& extension_id,
[email protected]4a1d9c0d2014-06-13 12:50:112398 bool install_immediately) const {
[email protected]e7aa7b7e2012-11-27 04:51:222399 const char kOnUpdateAvailableEvent[] = "runtime.onUpdateAvailable";
2400
2401 // If delayed updates are globally disabled, or just for this extension,
2402 // don't delay.
[email protected]4a1d9c0d2014-06-13 12:50:112403 if (!install_updates_when_idle_ || install_immediately)
[email protected]e7aa7b7e2012-11-27 04:51:222404 return false;
2405
[email protected]695b5712012-12-06 23:55:282406 const Extension* old = GetInstalledExtension(extension_id);
[email protected]e7aa7b7e2012-11-27 04:51:222407 // If there is no old extension, this is not an update, so don't delay.
2408 if (!old)
2409 return false;
2410
[email protected]9367eabc2013-03-01 01:29:292411 if (extensions::BackgroundInfo::HasPersistentBackgroundPage(old)) {
[email protected]e7aa7b7e2012-11-27 04:51:222412 // Delay installation if the extension listens for the onUpdateAvailable
2413 // event.
juncaicf523332015-06-04 00:14:042414 return extensions::EventRouter::Get(profile_)
2415 ->ExtensionHasEventListener(extension_id, kOnUpdateAvailableEvent);
[email protected]e7aa7b7e2012-11-27 04:51:222416 } else {
2417 // Delay installation if the extension is not idle.
[email protected]1d5cf4142014-01-24 18:25:222418 return !extensions::util::IsExtensionIdle(extension_id, profile_);
[email protected]e7aa7b7e2012-11-27 04:51:222419 }
[email protected]0db124b02012-11-07 04:55:052420}
[email protected]fdd679b2012-11-15 20:49:392421
[email protected]e5f8fffe2014-04-02 00:30:442422void ExtensionService::OnGarbageCollectIsolatedStorageStart() {
2423 DCHECK(!installs_delayed_for_gc_);
2424 installs_delayed_for_gc_ = true;
[email protected]399583b2012-12-11 09:33:422425}
2426
2427void ExtensionService::OnGarbageCollectIsolatedStorageFinished() {
[email protected]e5f8fffe2014-04-02 00:30:442428 DCHECK(installs_delayed_for_gc_);
2429 installs_delayed_for_gc_ = false;
[email protected]9f4e4f082013-06-21 07:11:192430 MaybeFinishDelayedInstallations();
2431}
2432
2433void ExtensionService::MaybeFinishDelayedInstallations() {
2434 std::vector<std::string> to_be_installed;
[email protected]399583b2012-12-11 09:33:422435 for (ExtensionSet::const_iterator it = delayed_installs_.begin();
2436 it != delayed_installs_.end();
2437 ++it) {
[email protected]9f4e4f082013-06-21 07:11:192438 to_be_installed.push_back((*it)->id());
[email protected]399583b2012-12-11 09:33:422439 }
[email protected]9f4e4f082013-06-21 07:11:192440 for (std::vector<std::string>::const_iterator it = to_be_installed.begin();
2441 it != to_be_installed.end();
[email protected]399583b2012-12-11 09:33:422442 ++it) {
[email protected]9f4e4f082013-06-21 07:11:192443 MaybeFinishDelayedInstallation(*it);
[email protected]399583b2012-12-11 09:33:422444 }
[email protected]399583b2012-12-11 09:33:422445}
2446
[email protected]fdd679b2012-11-15 20:49:392447void ExtensionService::OnBlacklistUpdated() {
[email protected]2d19eb6e2014-01-27 17:30:002448 blacklist_->GetBlacklistedIDs(
[email protected]f47f7172014-03-19 19:27:102449 registry_->GenerateInstalledExtensionsSet()->GetIDs(),
[email protected]3f2a2fa2013-09-24 02:55:252450 base::Bind(&ExtensionService::ManageBlacklist, AsWeakPtr()));
[email protected]695b5712012-12-06 23:55:282451}
2452
[email protected]2d19eb6e2014-01-27 17:30:002453void ExtensionService::ManageBlacklist(
2454 const extensions::Blacklist::BlacklistStateMap& state_map) {
[email protected]54ee8192014-03-29 17:37:242455 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]695b5712012-12-06 23:55:282456
mlerman6a37b6a42014-11-26 22:10:532457 std::set<std::string> blacklisted;
[email protected]2d19eb6e2014-01-27 17:30:002458 ExtensionIdSet greylist;
2459 ExtensionIdSet unchanged;
2460 for (extensions::Blacklist::BlacklistStateMap::const_iterator it =
2461 state_map.begin();
2462 it != state_map.end();
2463 ++it) {
2464 switch (it->second) {
2465 case extensions::NOT_BLACKLISTED:
2466 break;
[email protected]695b5712012-12-06 23:55:282467
[email protected]2d19eb6e2014-01-27 17:30:002468 case extensions::BLACKLISTED_MALWARE:
mlerman6a37b6a42014-11-26 22:10:532469 blacklisted.insert(it->first);
[email protected]2d19eb6e2014-01-27 17:30:002470 break;
2471
2472 case extensions::BLACKLISTED_SECURITY_VULNERABILITY:
2473 case extensions::BLACKLISTED_CWS_POLICY_VIOLATION:
2474 case extensions::BLACKLISTED_POTENTIALLY_UNWANTED:
2475 greylist.insert(it->first);
2476 break;
2477
2478 case extensions::BLACKLISTED_UNKNOWN:
2479 unchanged.insert(it->first);
2480 break;
2481 }
2482 }
2483
mlerman6a37b6a42014-11-26 22:10:532484 UpdateBlacklistedExtensions(blacklisted, unchanged);
[email protected]2d19eb6e2014-01-27 17:30:002485 UpdateGreylistedExtensions(greylist, unchanged, state_map);
2486
[email protected]373daf972014-04-10 01:50:442487 error_controller_->ShowErrorIfNeeded();
[email protected]2d19eb6e2014-01-27 17:30:002488}
2489
2490namespace {
2491void Partition(const ExtensionIdSet& before,
2492 const ExtensionIdSet& after,
2493 const ExtensionIdSet& unchanged,
2494 ExtensionIdSet* no_longer,
2495 ExtensionIdSet* not_yet) {
2496 *not_yet = base::STLSetDifference<ExtensionIdSet>(after, before);
2497 *no_longer = base::STLSetDifference<ExtensionIdSet>(before, after);
2498 *no_longer = base::STLSetDifference<ExtensionIdSet>(*no_longer, unchanged);
2499}
2500} // namespace
2501
mlerman6a37b6a42014-11-26 22:10:532502void ExtensionService::UpdateBlacklistedExtensions(
2503 const ExtensionIdSet& blacklisted,
[email protected]2d19eb6e2014-01-27 17:30:002504 const ExtensionIdSet& unchanged) {
2505 ExtensionIdSet not_yet_blocked, no_longer_blocked;
mlerman6a37b6a42014-11-26 22:10:532506 Partition(registry_->blacklisted_extensions().GetIDs(), blacklisted,
2507 unchanged, &no_longer_blocked, &not_yet_blocked);
[email protected]2d19eb6e2014-01-27 17:30:002508
2509 for (ExtensionIdSet::iterator it = no_longer_blocked.begin();
2510 it != no_longer_blocked.end(); ++it) {
[email protected]695b5712012-12-06 23:55:282511 scoped_refptr<const Extension> extension =
[email protected]bb1bc9b32013-12-21 03:09:142512 registry_->blacklisted_extensions().GetByID(*it);
[email protected]3f2a2fa2013-09-24 02:55:252513 if (!extension.get()) {
mlerman6a37b6a42014-11-26 22:10:532514 NOTREACHED() << "Extension " << *it << " no longer blacklisted, "
2515 << "but it was never blacklisted.";
[email protected]695b5712012-12-06 23:55:282516 continue;
[email protected]3f2a2fa2013-09-24 02:55:252517 }
[email protected]bb1bc9b32013-12-21 03:09:142518 registry_->RemoveBlacklisted(*it);
[email protected]3f2a2fa2013-09-24 02:55:252519 extension_prefs_->SetExtensionBlacklisted(extension->id(), false);
[email protected]dc24976f2013-06-02 21:15:092520 AddExtension(extension.get());
[email protected]ac875372013-02-28 04:36:092521 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.UnblacklistInstalled",
[email protected]dc24976f2013-06-02 21:15:092522 extension->location(),
2523 Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:282524 }
2525
[email protected]2d19eb6e2014-01-27 17:30:002526 for (ExtensionIdSet::iterator it = not_yet_blocked.begin();
2527 it != not_yet_blocked.end(); ++it) {
[email protected]695b5712012-12-06 23:55:282528 scoped_refptr<const Extension> extension = GetInstalledExtension(*it);
[email protected]3f2a2fa2013-09-24 02:55:252529 if (!extension.get()) {
2530 NOTREACHED() << "Extension " << *it << " needs to be "
2531 << "blacklisted, but it's not installed.";
[email protected]695b5712012-12-06 23:55:282532 continue;
[email protected]3f2a2fa2013-09-24 02:55:252533 }
[email protected]bb1bc9b32013-12-21 03:09:142534 registry_->AddBlacklisted(extension);
[email protected]2d19eb6e2014-01-27 17:30:002535 extension_prefs_->SetExtensionBlacklistState(
2536 extension->id(), extensions::BLACKLISTED_MALWARE);
[email protected]b0af4792013-10-23 09:12:132537 UnloadExtension(*it, UnloadedExtensionInfo::REASON_BLACKLIST);
[email protected]ac875372013-02-28 04:36:092538 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.BlacklistInstalled",
2539 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:282540 }
[email protected]2d19eb6e2014-01-27 17:30:002541}
[email protected]695b5712012-12-06 23:55:282542
[email protected]2d19eb6e2014-01-27 17:30:002543// TODO(oleg): UMA logging
2544void ExtensionService::UpdateGreylistedExtensions(
2545 const ExtensionIdSet& greylist,
2546 const ExtensionIdSet& unchanged,
2547 const extensions::Blacklist::BlacklistStateMap& state_map) {
2548 ExtensionIdSet not_yet_greylisted, no_longer_greylisted;
2549 Partition(greylist_.GetIDs(),
2550 greylist, unchanged,
2551 &no_longer_greylisted, &not_yet_greylisted);
2552
2553 for (ExtensionIdSet::iterator it = no_longer_greylisted.begin();
2554 it != no_longer_greylisted.end(); ++it) {
2555 scoped_refptr<const Extension> extension = greylist_.GetByID(*it);
2556 if (!extension.get()) {
2557 NOTREACHED() << "Extension " << *it << " no longer greylisted, "
2558 << "but it was not marked as greylisted.";
2559 continue;
2560 }
2561
2562 greylist_.Remove(*it);
2563 extension_prefs_->SetExtensionBlacklistState(extension->id(),
2564 extensions::NOT_BLACKLISTED);
2565 if (extension_prefs_->GetDisableReasons(extension->id()) &
2566 extensions::Extension::DISABLE_GREYLIST)
2567 EnableExtension(*it);
2568 }
2569
2570 for (ExtensionIdSet::iterator it = not_yet_greylisted.begin();
2571 it != not_yet_greylisted.end(); ++it) {
2572 scoped_refptr<const Extension> extension = GetInstalledExtension(*it);
2573 if (!extension.get()) {
2574 NOTREACHED() << "Extension " << *it << " needs to be "
2575 << "disabled, but it's not installed.";
2576 continue;
2577 }
2578 greylist_.Insert(extension);
2579 extension_prefs_->SetExtensionBlacklistState(extension->id(),
2580 state_map.find(*it)->second);
2581 if (registry_->enabled_extensions().Contains(extension->id()))
2582 DisableExtension(*it, extensions::Extension::DISABLE_GREYLIST);
2583 }
[email protected]fdd679b2012-11-15 20:49:392584}
[email protected]75bdcb872013-03-13 00:41:452585
2586void ExtensionService::AddUpdateObserver(extensions::UpdateObserver* observer) {
2587 update_observers_.AddObserver(observer);
2588}
2589
2590void ExtensionService::RemoveUpdateObserver(
2591 extensions::UpdateObserver* observer) {
2592 update_observers_.RemoveObserver(observer);
2593}
[email protected]bb1bc9b32013-12-21 03:09:142594
2595// Used only by test code.
2596void ExtensionService::UnloadAllExtensionsInternal() {
2597 profile_->GetExtensionSpecialStoragePolicy()->RevokeRightsForAllExtensions();
2598
2599 registry_->ClearAll();
[email protected]45f5b7d2014-01-22 23:47:132600 system_->runtime_data()->ClearAll();
[email protected]bb1bc9b32013-12-21 03:09:142601
2602 // TODO(erikkay) should there be a notification for this? We can't use
2603 // EXTENSION_UNLOADED since that implies that the extension has been disabled
2604 // or uninstalled.
2605}
[email protected]ebe07772014-05-22 04:16:062606
2607void ExtensionService::OnProfileDestructionStarted() {
2608 ExtensionIdSet ids_to_unload = registry_->enabled_extensions().GetIDs();
2609 for (ExtensionIdSet::iterator it = ids_to_unload.begin();
2610 it != ids_to_unload.end();
2611 ++it) {
2612 UnloadExtension(*it, UnloadedExtensionInfo::REASON_PROFILE_SHUTDOWN);
2613 }
2614}