blob: 8c1759bfb0d5dd4d5df4368a5ba02d70d57b6222 [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]24b538a2010-02-27 01:22:4411#include "base/basictypes.h"
[email protected]05aad2da2011-10-28 10:12:3712#include "base/bind.h"
[email protected]e5af875f2011-10-10 21:09:1413#include "base/callback.h"
[email protected]62433d32011-10-12 22:33:1214#include "base/command_line.h"
[email protected]6014d672008-12-05 00:38:2515#include "base/file_util.h"
[email protected]14908b72011-04-20 06:54:3616#include "base/logging.h"
[email protected]835d7c82010-10-14 04:38:3817#include "base/metrics/histogram.h"
[email protected]3853a4c2013-02-11 17:15:5718#include "base/prefs/pref_service.h"
[email protected]7286e3fc2011-07-19 22:13:2419#include "base/stl_util.h"
[email protected]3ea1b182013-02-08 22:38:4120#include "base/strings/string_number_conversions.h"
[email protected]00e7bef2013-06-10 20:35:1721#include "base/strings/string_util.h"
22#include "base/strings/stringprintf.h"
[email protected]112158af2013-06-07 23:46:1823#include "base/strings/utf_string_conversions.h"
[email protected]7f8f24f2012-11-15 19:40:1424#include "base/threading/sequenced_worker_pool.h"
[email protected]34b99632011-01-01 01:01:0625#include "base/threading/thread_restrictions.h"
[email protected]41a17c52013-06-28 00:27:5326#include "base/time/time.h"
[email protected]cc655912009-01-29 23:19:1927#include "base/values.h"
[email protected]aa142702010-03-26 01:26:3328#include "base/version.h"
[email protected]15730c42009-09-03 00:03:2029#include "chrome/browser/browser_process.h"
[email protected]9ea0cd32013-07-12 01:50:3630#include "chrome/browser/chrome_notification_types.h"
[email protected]ad445762013-01-23 00:47:4431#include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
[email protected]4636c832013-01-11 02:10:1132#include "chrome/browser/extensions/api/storage/settings_frontend.h"
[email protected]d8c8f25f2011-11-02 18:18:0133#include "chrome/browser/extensions/component_loader.h"
[email protected]e0785902011-05-19 23:34:1734#include "chrome/browser/extensions/crx_installer.h"
[email protected]d9ede582012-08-14 19:21:3835#include "chrome/browser/extensions/data_deleter.h"
[email protected]62f051c2012-03-29 17:04:4436#include "chrome/browser/extensions/extension_disabled_ui.h"
[email protected]14a000d2010-04-29 21:44:2437#include "chrome/browser/extensions/extension_error_reporter.h"
[email protected]89226982012-07-16 20:09:1838#include "chrome/browser/extensions/extension_error_ui.h"
[email protected]b1748b1d82009-11-30 20:32:5639#include "chrome/browser/extensions/extension_host.h"
[email protected]00b38242012-07-18 18:43:2240#include "chrome/browser/extensions/extension_install_ui.h"
[email protected]19eb80152011-02-26 00:28:4341#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]f8aefb132013-10-30 09:29:5242#include "chrome/browser/extensions/extension_sync_service.h"
[email protected]31d8f5f22012-04-02 15:22:0843#include "chrome/browser/extensions/extension_system.h"
[email protected]617342a42013-12-18 23:34:0344#include "chrome/browser/extensions/extension_util.h"
[email protected]612a1cb12012-10-17 13:18:0345#include "chrome/browser/extensions/external_install_ui.h"
[email protected]5df038b2012-07-16 19:03:2746#include "chrome/browser/extensions/external_provider_impl.h"
[email protected]ffd2f79e2013-11-14 00:11:4647#include "chrome/browser/extensions/install_verifier.h"
[email protected]d8c8f25f2011-11-02 18:18:0148#include "chrome/browser/extensions/installed_loader.h"
[email protected]c333e792012-01-06 16:57:3949#include "chrome/browser/extensions/permissions_updater.h"
[email protected]d8c8f25f2011-11-02 18:18:0150#include "chrome/browser/extensions/unpacked_installer.h"
[email protected]42a08162012-03-16 18:09:1151#include "chrome/browser/extensions/updater/extension_updater.h"
[email protected]8ecad5e2010-12-02 21:18:3352#include "chrome/browser/profiles/profile.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]5eddc3e2011-10-26 04:33:3155#include "chrome/browser/ui/webui/ntp/thumbnail_source.h"
[email protected]b07e606e2012-09-15 20:16:1556#include "chrome/browser/ui/webui/theme_source.h"
[email protected]e2eb43112009-05-29 21:19:5457#include "chrome/common/chrome_switches.h"
[email protected]f6431be82013-09-07 02:53:4558#include "chrome/common/crash_keys.h"
[email protected]9f4e4f082013-06-21 07:11:1959#include "chrome/common/extensions/extension_constants.h"
[email protected]7c927b62010-02-24 09:54:1360#include "chrome/common/extensions/extension_file_util.h"
[email protected]77a6970c2011-04-23 16:58:5661#include "chrome/common/extensions/extension_messages.h"
[email protected]15d4d2d2013-08-09 06:49:0362#include "chrome/common/extensions/features/feature_channel.h"
[email protected]561e33d52013-04-03 06:58:4363#include "chrome/common/extensions/manifest_handlers/app_isolation_info.h"
[email protected]65348062013-01-15 07:27:2264#include "chrome/common/extensions/manifest_url_handler.h"
[email protected]25b34332009-06-05 21:53:1965#include "chrome/common/pref_names.h"
[email protected]a57209872009-05-04 22:53:1466#include "chrome/common/url_constants.h"
[email protected]7a83dd072013-09-03 12:09:1767#include "components/startup_metric_utils/startup_metric_utils.h"
[email protected]c38831a12011-10-28 12:44:4968#include "content/public/browser/browser_thread.h"
[email protected]98f66112012-12-25 12:59:3669#include "content/public/browser/devtools_agent_host.h"
[email protected]05aad2da2011-10-28 10:12:3770#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1671#include "content/public/browser/notification_types.h"
[email protected]f3b1a082011-11-18 00:34:3072#include "content/public/browser/render_process_host.h"
[email protected]399583b2012-12-11 09:33:4273#include "content/public/browser/site_instance.h"
74#include "content/public/browser/storage_partition.h"
[email protected]24ea7a12013-01-27 23:54:5375#include "content/public/browser/url_data_source.h"
[email protected]74474042013-11-21 12:03:5476#include "extensions/browser/app_sorting.h"
[email protected]34423532013-11-21 18:13:1077#include "extensions/browser/event_router.h"
[email protected]bb1bc9b32013-12-21 03:09:1478#include "extensions/browser/extension_registry.h"
[email protected]45928b682013-11-28 08:20:2779#include "extensions/browser/extensions_browser_client.h"
[email protected]301116c62013-11-26 10:37:4580#include "extensions/browser/external_provider_interface.h"
81#include "extensions/browser/management_policy.h"
[email protected]b52f8ca2013-11-28 08:25:2282#include "extensions/browser/pending_extension_manager.h"
[email protected]234fc5ff2014-01-16 23:32:2883#include "extensions/browser/pref_names.h"
[email protected]98b6d942013-11-10 00:34:0784#include "extensions/browser/process_manager.h"
[email protected]fafdc842014-01-17 18:09:0885#include "extensions/browser/process_map.h"
[email protected]45f5b7d2014-01-22 23:47:1386#include "extensions/browser/runtime_data.h"
[email protected]a9aa62b312013-11-29 05:35:0687#include "extensions/browser/update_observer.h"
[email protected]836e2982013-05-16 08:07:4288#include "extensions/common/constants.h"
[email protected]e9f541a2012-11-19 21:52:3189#include "extensions/common/error_utils.h"
[email protected]e4452d32013-11-15 23:07:4190#include "extensions/common/extension.h"
[email protected]c81f5d32013-10-26 10:38:4291#include "extensions/common/extensions_client.h"
[email protected]5ef835a2013-11-08 20:42:5792#include "extensions/common/feature_switch.h"
[email protected]d42c11152013-08-22 19:36:3293#include "extensions/common/manifest.h"
[email protected]0c3c9732013-09-16 08:53:4194#include "extensions/common/manifest_constants.h"
[email protected]558878cc82013-11-09 01:25:5195#include "extensions/common/manifest_handlers/background_info.h"
[email protected]1f7de252013-11-06 22:02:0096#include "extensions/common/manifest_handlers/incognito_info.h"
[email protected]301116c62013-11-26 10:37:4597#include "extensions/common/manifest_handlers/shared_module_info.h"
[email protected]c41003472013-10-19 15:37:2598#include "extensions/common/permissions/permission_message_provider.h"
[email protected]e4452d32013-11-15 23:07:4199#include "extensions/common/permissions/permissions_data.h"
[email protected]65187152012-06-02 13:14:14100#include "grit/generated_resources.h"
[email protected]be28b5f42012-07-20 11:31:25101#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
[email protected]cd67ed52013-10-15 01:22:13102#include "ui/base/webui/web_ui_util.h"
[email protected]a6483d22013-07-03 22:11:00103#include "url/gurl.h"
[email protected]08b1f75f2013-05-22 22:02:38104#include "webkit/browser/database/database_tracker.h"
105#include "webkit/browser/database/database_util.h"
[email protected]79a60642012-10-20 21:03:18106
[email protected]eed367e2011-04-12 03:43:31107#if defined(OS_CHROMEOS)
[email protected]88e8ec9152013-01-17 04:05:18108#include "chrome/browser/chromeos/extensions/install_limiter.h"
[email protected]f19bbf62013-07-09 01:22:32109#include "webkit/browser/fileapi/file_system_backend.h"
[email protected]c6f9203a2013-05-28 02:08:07110#include "webkit/browser/fileapi/file_system_context.h"
[email protected]eed367e2011-04-12 03:43:31111#endif
112
[email protected]55eb70e762012-02-20 17:38:39113using content::BrowserContext;
[email protected]631bb742011-11-02 11:29:39114using content::BrowserThread;
[email protected]98f66112012-12-25 12:59:36115using content::DevToolsAgentHost;
[email protected]bf3d9df2012-07-24 23:20:27116using extensions::CrxInstaller;
[email protected]1c321ee52012-05-21 03:02:34117using extensions::Extension;
118using extensions::ExtensionIdSet;
119using extensions::ExtensionInfo;
[email protected]599539802014-01-07 23:06:00120using extensions::ExtensionRegistry;
[email protected]289c44b2013-12-17 03:26:57121using extensions::ExtensionSet;
[email protected]215a7be2012-10-22 19:53:42122using extensions::FeatureSwitch;
[email protected]ffd2f79e2013-11-14 00:11:46123using extensions::InstallVerifier;
[email protected]0d54b682013-11-05 14:15:36124using extensions::ManagementPolicy;
[email protected]1d5e58b2013-01-31 08:41:40125using extensions::Manifest;
[email protected]c2e66e12012-06-27 06:27:06126using extensions::PermissionMessage;
127using extensions::PermissionMessages;
128using extensions::PermissionSet;
[email protected]9f4e4f082013-06-21 07:11:19129using extensions::SharedModuleInfo;
[email protected]e410b5f2012-12-14 14:02:24130using extensions::UnloadedExtensionInfo;
[email protected]5ef47ec2010-01-28 05:58:05131
[email protected]0c3c9732013-09-16 08:53:41132namespace errors = extensions::manifest_errors;
[email protected]c6d474f82009-12-16 21:11:06133
[email protected]b6ab96d2009-08-20 18:58:19134namespace {
135
[email protected]d96eb512012-11-01 23:44:08136// Histogram values for logging events related to externally installed
137// extensions.
138enum ExternalExtensionEvent {
139 EXTERNAL_EXTENSION_INSTALLED = 0,
140 EXTERNAL_EXTENSION_IGNORED,
141 EXTERNAL_EXTENSION_REENABLED,
142 EXTERNAL_EXTENSION_UNINSTALLED,
143 EXTERNAL_EXTENSION_BUCKET_BOUNDARY,
144};
145
[email protected]612a1cb12012-10-17 13:18:03146// Prompt the user this many times before considering an extension acknowledged.
147static const int kMaxExtensionAcknowledgePromptCount = 3;
148
[email protected]0db124b02012-11-07 04:55:05149// Wait this many seconds after an extensions becomes idle before updating it.
150static const int kUpdateIdleDelay = 5;
151
[email protected]9bd9a6862012-11-29 09:24:22152// Wait this many seconds before trying to garbage collect extensions again.
153static const int kGarbageCollectRetryDelay = 30;
154
[email protected]36fc54f2013-06-14 02:49:34155// Wait this many seconds after startup to see if there are any extensions
156// which can be garbage collected.
157static const int kGarbageCollectStartupDelay = 30;
158
[email protected]9f4e4f082013-06-21 07:11:19159static bool IsSharedModule(const Extension* extension) {
160 return SharedModuleInfo::IsSharedModule(extension);
161}
162
[email protected]4c9201c42013-08-16 04:56:21163static bool IsCWSSharedModule(const Extension* extension) {
164 return extension->from_webstore() && IsSharedModule(extension);
165}
166
167class SharedModuleProvider : public extensions::ManagementPolicy::Provider {
168 public:
169 SharedModuleProvider() {}
170 virtual ~SharedModuleProvider() {}
171
172 virtual std::string GetDebugPolicyProviderName() const OVERRIDE {
173 return "SharedModuleProvider";
174 }
175
176 virtual bool UserMayModifySettings(const Extension* extension,
[email protected]439f1e32013-12-09 20:09:09177 base::string16* error) const OVERRIDE {
[email protected]4c9201c42013-08-16 04:56:21178 return !IsCWSSharedModule(extension);
179 }
180
181 virtual bool MustRemainEnabled(const Extension* extension,
[email protected]439f1e32013-12-09 20:09:09182 base::string16* error) const OVERRIDE {
[email protected]4c9201c42013-08-16 04:56:21183 return IsCWSSharedModule(extension);
184 }
185
186 private:
187 DISALLOW_COPY_AND_ASSIGN(SharedModuleProvider);
188};
189
190
[email protected]c6d474f82009-12-16 21:11:06191} // namespace
[email protected]b6ab96d2009-08-20 18:58:19192
[email protected]eaa7dd182010-12-14 11:09:00193// ExtensionService.
[email protected]6014d672008-12-05 00:38:25194
[email protected]8e4560b62011-01-14 10:09:14195void ExtensionService::CheckExternalUninstall(const std::string& id) {
[email protected]a29a517a2011-01-21 21:11:12196 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20197
[email protected]7a4c6852010-09-16 03:44:22198 // Check if the providers know about this extension.
[email protected]5df038b2012-07-16 19:03:27199 extensions::ProviderCollection::const_iterator i;
[email protected]0a60a2e2010-10-25 16:15:21200 for (i = external_extension_providers_.begin();
201 i != external_extension_providers_.end(); ++i) {
[email protected]8e4560b62011-01-14 10:09:14202 DCHECK(i->get()->IsReady());
[email protected]0a60a2e2010-10-25 16:15:21203 if (i->get()->HasExtension(id))
204 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22205 }
206
[email protected]0f48fca2011-05-19 18:46:35207 // We get the list of external extensions to check from preferences.
208 // It is possible that an extension has preferences but is not loaded.
209 // For example, an extension that requires experimental permissions
210 // will not be loaded if the experimental command line flag is not used.
211 // In this case, do not uninstall.
[email protected]31bb5ee62012-09-12 22:58:40212 if (!GetInstalledExtension(id)) {
[email protected]0f48fca2011-05-19 18:46:35213 // We can't call UninstallExtension with an unloaded/invalid
214 // extension ID.
215 LOG(WARNING) << "Attempted uninstallation of unloaded/invalid extension "
216 << "with id: " << id;
217 return;
218 }
[email protected]d6ebc9792011-04-07 18:18:33219 UninstallExtension(id, true, NULL);
[email protected]7a4c6852010-09-16 03:44:22220}
221
[email protected]7f8f24f2012-11-15 19:40:14222void ExtensionService::SetFileTaskRunnerForTesting(
223 base::SequencedTaskRunner* task_runner) {
224 file_task_runner_ = task_runner;
225}
226
[email protected]8e4560b62011-01-14 10:09:14227void ExtensionService::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22228 external_extension_providers_.clear();
229}
230
[email protected]8e4560b62011-01-14 10:09:14231void ExtensionService::AddProviderForTesting(
[email protected]5df038b2012-07-16 19:03:27232 extensions::ExternalProviderInterface* test_provider) {
[email protected]a29a517a2011-01-21 21:11:12233 CHECK(test_provider);
[email protected]0a60a2e2010-10-25 16:15:21234 external_extension_providers_.push_back(
[email protected]5df038b2012-07-16 19:03:27235 linked_ptr<extensions::ExternalProviderInterface>(test_provider));
[email protected]7a4c6852010-09-16 03:44:22236}
237
[email protected]9060d8b02012-01-13 02:14:30238bool ExtensionService::OnExternalExtensionUpdateUrlFound(
[email protected]7a4c6852010-09-16 03:44:22239 const std::string& id,
[email protected]21a5a672010-11-04 10:47:42240 const GURL& update_url,
[email protected]464213a2013-10-15 01:06:48241 Manifest::Location location,
242 int creation_flags,
243 bool mark_acknowledged) {
[email protected]ab22ba42011-01-14 16:36:38244 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
245 CHECK(Extension::IdIsValid(id));
[email protected]a8af9fdb2010-10-28 21:52:20246
[email protected]8a87a5332011-08-11 17:54:59247 const Extension* extension = GetExtensionById(id, true);
248 if (extension) {
249 // Already installed. Skip this install if the current location has
250 // higher priority than |location|.
[email protected]1d5e58b2013-01-31 08:41:40251 Manifest::Location current = extension->location();
252 if (current == Manifest::GetHigherPriorityLocation(current, location))
[email protected]9060d8b02012-01-13 02:14:30253 return false;
[email protected]8a87a5332011-08-11 17:54:59254 // Otherwise, overwrite the current installation.
[email protected]7a4c6852010-09-16 03:44:22255 }
[email protected]9060d8b02012-01-13 02:14:30256
257 // Add |id| to the set of pending extensions. If it can not be added,
258 // then there is already a pending record from a higher-priority install
259 // source. In this case, signal that this extension will not be
260 // installed by returning false.
261 if (!pending_extension_manager()->AddFromExternalUpdateUrl(
[email protected]464213a2013-10-15 01:06:48262 id, update_url, location, creation_flags, mark_acknowledged)) {
[email protected]9060d8b02012-01-13 02:14:30263 return false;
[email protected]31bb5ee62012-09-12 22:58:40264 }
[email protected]9060d8b02012-01-13 02:14:30265
[email protected]94fde232012-04-27 10:22:30266 update_once_all_providers_are_ready_ = true;
[email protected]9060d8b02012-01-13 02:14:30267 return true;
[email protected]7a4c6852010-09-16 03:44:22268}
269
[email protected]400c4392013-12-13 14:41:58270const Extension* ExtensionService::GetInstalledExtensionByUrl(
271 const GURL& url) const {
[email protected]bb1bc9b32013-12-21 03:09:14272 return registry_->enabled_extensions().GetExtensionOrAppByURL(url);
[email protected]400c4392013-12-13 14:41:58273}
274
[email protected]dc9a74f72012-08-17 18:07:21275const Extension* ExtensionService::GetInstalledApp(const GURL& url) const {
[email protected]400c4392013-12-13 14:41:58276 const Extension* extension = GetInstalledExtensionByUrl(url);
[email protected]dc9a74f72012-08-17 18:07:21277 return (extension && extension->is_app()) ? extension : NULL;
[email protected]d9696672011-03-15 22:45:09278}
279
[email protected]dc9a74f72012-08-17 18:07:21280bool ExtensionService::IsInstalledApp(const GURL& url) const {
[email protected]d9696672011-03-15 22:45:09281 return !!GetInstalledApp(url);
[email protected]ffb204f22010-12-05 23:20:27282}
283
[email protected]6aeac8342010-10-01 20:21:18284// static
[email protected]d6ebc9792011-04-07 18:18:33285// This function is used to implement the command-line switch
[email protected]3bdba0d2011-08-23 07:17:30286// --uninstall-extension, and to uninstall an extension via sync. The LOG
287// statements within this function are used to inform the user if the uninstall
288// cannot be done.
[email protected]eaa7dd182010-12-14 11:09:00289bool ExtensionService::UninstallExtensionHelper(
290 ExtensionService* extensions_service,
[email protected]6aeac8342010-10-01 20:21:18291 const std::string& extension_id) {
[email protected]d6ebc9792011-04-07 18:18:33292 // We can't call UninstallExtension with an invalid extension ID.
[email protected]31bb5ee62012-09-12 22:58:40293 if (!extensions_service->GetInstalledExtension(extension_id)) {
[email protected]6aeac8342010-10-01 20:21:18294 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]95da88c42011-03-31 10:07:33295 << "id: " << extension_id;
296 return false;
[email protected]6aeac8342010-10-01 20:21:18297 }
298
[email protected]d6ebc9792011-04-07 18:18:33299 // The following call to UninstallExtension will not allow an uninstall of a
300 // policy-controlled extension.
[email protected]439f1e32013-12-09 20:09:09301 base::string16 error;
[email protected]d6ebc9792011-04-07 18:18:33302 if (!extensions_service->UninstallExtension(extension_id, false, &error)) {
303 LOG(WARNING) << "Cannot uninstall extension with id " << extension_id
304 << ": " << error;
305 return false;
306 }
[email protected]95da88c42011-03-31 10:07:33307
[email protected]6aeac8342010-10-01 20:21:18308 return true;
309}
310
[email protected]eaa7dd182010-12-14 11:09:00311ExtensionService::ExtensionService(Profile* profile,
[email protected]f0841cd2011-01-19 15:07:24312 const CommandLine* command_line,
[email protected]650b2d52013-02-10 03:41:45313 const base::FilePath& install_directory,
[email protected]45759612012-07-10 17:21:23314 extensions::ExtensionPrefs* extension_prefs,
[email protected]fdd679b2012-11-15 20:49:39315 extensions::Blacklist* blacklist,
[email protected]0436b102011-04-15 18:30:03316 bool autoupdate_enabled,
[email protected]4a10006a2013-05-17 23:18:35317 bool extensions_enabled,
318 extensions::OneShotEvent* ready)
[email protected]fdd679b2012-11-15 20:49:39319 : extensions::Blacklist::Observer(blacklist),
320 profile_(profile),
[email protected]bd306722012-07-11 20:43:59321 system_(extensions::ExtensionSystem::Get(profile)),
[email protected]73c47932010-12-06 18:13:43322 extension_prefs_(extension_prefs),
[email protected]695b5712012-12-06 23:55:28323 blacklist_(blacklist),
[email protected]cccdf0aa2011-11-11 03:43:38324 settings_frontend_(extensions::SettingsFrontend::Create(profile)),
[email protected]f8aefb132013-10-30 09:29:52325 extension_sync_service_(NULL),
[email protected]5fdfa562013-12-27 17:43:59326 registry_(extensions::ExtensionRegistry::Get(profile)),
[email protected]9c009092013-05-01 03:14:09327 pending_extension_manager_(*this),
[email protected]a9b00ac2009-06-25 21:03:23328 install_directory_(install_directory),
[email protected]0436b102011-04-15 18:30:03329 extensions_enabled_(extensions_enabled),
[email protected]e81dba32009-06-19 20:19:13330 show_extensions_prompts_(true),
[email protected]fc332ae2012-11-14 20:17:33331 install_updates_when_idle_(true),
[email protected]4a10006a2013-05-17 23:18:35332 ready_(ready),
[email protected]dff1e042012-04-27 10:59:18333 update_once_all_providers_are_ready_(false),
[email protected]3c4abc82012-10-22 22:25:54334 browser_terminating_(false),
[email protected]9f4e4f082013-06-21 07:11:19335 installs_delayed_for_gc_(false),
[email protected]f8aefb132013-10-30 09:29:52336 is_first_run_(false) {
[email protected]c04fd3dd2013-09-05 08:20:10337#if defined(OS_CHROMEOS)
338 disable_garbage_collection_ = false;
339#endif
[email protected]a29a517a2011-01-21 21:11:12340 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20341
[email protected]36a784c2009-06-23 06:21:08342 // Figure out if extension installation should be enabled.
[email protected]367d9b172013-12-03 00:31:02343 if (extensions::ExtensionsBrowserClient::Get()->AreExtensionsDisabled(
344 *command_line, profile))
[email protected]6d60703b2009-08-29 01:29:23345 extensions_enabled_ = false;
[email protected]36a784c2009-06-23 06:21:08346
[email protected]3c4abc82012-10-22 22:25:54347 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING,
348 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27349 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07350 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27351 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED,
[email protected]ad50def52011-10-19 23:17:07352 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27353 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07354 content::NotificationService::AllBrowserContextsAndSources());
[email protected]92dd8d92013-02-26 00:41:08355 registrar_.Add(this, chrome::NOTIFICATION_UPGRADE_RECOMMENDED,
356 content::NotificationService::AllBrowserContextsAndSources());
[email protected]2fb7dc982010-09-29 12:24:28357 pref_change_registrar_.Init(profile->GetPrefs());
[email protected]90bb38d2012-11-14 18:36:03358 base::Closure callback =
359 base::Bind(&ExtensionService::OnExtensionInstallPrefChanged,
360 base::Unretained(this));
[email protected]234fc5ff2014-01-16 23:32:28361 pref_change_registrar_.Add(extensions::pref_names::kInstallAllowList,
362 callback);
363 pref_change_registrar_.Add(extensions::pref_names::kInstallDenyList,
364 callback);
365 pref_change_registrar_.Add(extensions::pref_names::kAllowedTypes, callback);
[email protected]4814b512009-11-07 00:12:29366
[email protected]93fd78f42009-07-10 16:43:17367 // Set up the ExtensionUpdater
368 if (autoupdate_enabled) {
[email protected]501105b2013-09-26 05:42:02369 int update_frequency = extensions::kDefaultUpdateFrequencySeconds;
[email protected]93fd78f42009-07-10 16:43:17370 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25371 base::StringToInt(command_line->GetSwitchValueASCII(
372 switches::kExtensionsUpdateFrequency),
373 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17374 }
[email protected]42a08162012-03-16 18:09:11375 updater_.reset(new extensions::ExtensionUpdater(this,
376 extension_prefs,
377 profile->GetPrefs(),
378 profile,
379 update_frequency));
[email protected]93fd78f42009-07-10 16:43:17380 }
381
[email protected]25ae0152011-11-18 14:40:02382 component_loader_.reset(
383 new extensions::ComponentLoader(this,
384 profile->GetPrefs(),
[email protected]55e16cd2013-12-18 04:36:08385 g_browser_process->local_state(),
386 profile));
[email protected]8e4560b62011-01-14 10:09:14387
[email protected]0436b102011-04-15 18:30:03388 if (extensions_enabled_) {
[email protected]ae4c37e2012-12-21 01:16:25389 extensions::ExternalProviderImpl::CreateExternalProviders(
390 this, profile_, &external_extension_providers_);
[email protected]873531342011-03-09 12:16:05391 }
[email protected]b671760b2010-07-15 21:13:47392
[email protected]74474042013-11-21 12:03:54393 // Set this as the ExtensionService for app sorting to ensure it causes syncs
394 // if required.
[email protected]b3aa7182013-04-25 04:45:23395 is_first_run_ = !extension_prefs_->SetAlertSystemFirstRun();
396
[email protected]9ce11d22012-08-08 23:20:13397#if defined(ENABLE_EXTENSIONS)
[email protected]c77f2352012-08-08 22:07:58398 extension_action_storage_manager_.reset(
399 new extensions::ExtensionActionStorageManager(profile_));
[email protected]9ce11d22012-08-08 23:20:13400#endif
[email protected]c77f2352012-08-08 22:07:58401
[email protected]4c9201c42013-08-16 04:56:21402 shared_module_policy_provider_.reset(new SharedModuleProvider);
403
[email protected]cb0e50312011-05-09 15:03:07404 // How long is the path to the Extensions directory?
405 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ExtensionRootPathLength",
406 install_directory_.value().length(), 0, 500, 100);
[email protected]6014d672008-12-05 00:38:25407}
408
[email protected]84df8332011-12-06 18:22:46409const ExtensionSet* ExtensionService::extensions() const {
[email protected]bb1bc9b32013-12-21 03:09:14410 return &registry_->enabled_extensions();
[email protected]ddb1e5a2010-12-13 20:10:45411}
412
[email protected]84df8332011-12-06 18:22:46413const ExtensionSet* ExtensionService::disabled_extensions() const {
[email protected]bb1bc9b32013-12-21 03:09:14414 return &registry_->disabled_extensions();
[email protected]ddb1e5a2010-12-13 20:10:45415}
416
[email protected]9e9c1d12013-07-31 01:58:12417const ExtensionSet* ExtensionService::delayed_installs() const {
418 return &delayed_installs_;
419}
420
[email protected]620b5a62013-11-18 04:21:08421scoped_ptr<ExtensionSet>
[email protected]695b5712012-12-06 23:55:28422 ExtensionService::GenerateInstalledExtensionsSet() const {
423 scoped_ptr<ExtensionSet> installed_extensions(new ExtensionSet());
[email protected]bb1bc9b32013-12-21 03:09:14424 installed_extensions->InsertAll(registry_->enabled_extensions());
425 installed_extensions->InsertAll(registry_->disabled_extensions());
426 installed_extensions->InsertAll(registry_->terminated_extensions());
427 installed_extensions->InsertAll(registry_->blacklisted_extensions());
428 return installed_extensions.Pass();
[email protected]7f4308d2012-01-18 07:43:01429}
430
[email protected]3f213ad2012-07-26 23:39:41431extensions::PendingExtensionManager*
432 ExtensionService::pending_extension_manager() {
[email protected]b2907fd2011-03-25 16:43:37433 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45434}
435
[email protected]eaa7dd182010-12-14 11:09:00436ExtensionService::~ExtensionService() {
[email protected]c8d407e2011-04-28 21:27:17437 // No need to unload extensions here because they are profile-scoped, and the
438 // profile is in the process of being deleted.
[email protected]94b4ffc2011-02-04 05:55:17439
[email protected]5df038b2012-07-16 19:03:27440 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:14441 for (i = external_extension_providers_.begin();
442 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:27443 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:14444 provider->ServiceShutdown();
445 }
[email protected]6014d672008-12-05 00:38:25446}
447
[email protected]037228a2012-10-05 01:36:16448void ExtensionService::Shutdown() {
[email protected]4c9201c42013-08-16 04:56:21449 system_->management_policy()->UnregisterProvider(
450 shared_module_policy_provider_.get());
[email protected]037228a2012-10-05 01:36:16451}
452
[email protected]b2907fd2011-03-25 16:43:37453const Extension* ExtensionService::GetExtensionById(
454 const std::string& id, bool include_disabled) const {
[email protected]599539802014-01-07 23:06:00455 int include_mask = ExtensionRegistry::ENABLED;
[email protected]695b5712012-12-06 23:55:28456 if (include_disabled) {
457 // Include blacklisted extensions here because there are hundreds of
458 // callers of this function, and many might assume that this includes those
459 // that have been disabled due to blacklisting.
[email protected]599539802014-01-07 23:06:00460 include_mask |= ExtensionRegistry::DISABLED |
461 ExtensionRegistry::BLACKLISTED;
[email protected]695b5712012-12-06 23:55:28462 }
[email protected]599539802014-01-07 23:06:00463 return registry_->GetExtensionById(id, include_mask);
[email protected]695b5712012-12-06 23:55:28464}
465
[email protected]399583b2012-12-11 09:33:42466GURL ExtensionService::GetSiteForExtensionId(const std::string& extension_id) {
467 return content::SiteInstance::GetSiteForURL(
468 profile_,
469 Extension::GetBaseURLFromExtensionId(extension_id));
470}
471
[email protected]eaa7dd182010-12-14 11:09:00472void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12473 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20474
[email protected]3350d712013-11-18 09:32:24475 base::Time begin_time = base::Time::Now();
476
[email protected]4a10006a2013-05-17 23:18:35477 DCHECK(!is_ready()); // Can't redo init.
[email protected]bb1bc9b32013-12-21 03:09:14478 DCHECK_EQ(registry_->enabled_extensions().size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32479
[email protected]820d9bd2013-04-03 03:46:03480 const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
[email protected]820d9bd2013-04-03 03:46:03481 if (cmd_line->HasSwitch(switches::kInstallFromWebstore) ||
482 cmd_line->HasSwitch(switches::kLimitedInstallFromWebstore)) {
483 // The sole purpose of this launch is to install a new extension from CWS
484 // and immediately terminate: loading already installed extensions is
485 // unnecessary and may interfere with the inline install dialog (e.g. if an
486 // extension listens to onStartup and opens a window).
487 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25488 } else {
[email protected]47b60072013-06-20 10:35:56489 // LoadAllExtensions() calls OnLoadedInstalledExtensions().
490 component_loader_->LoadAll();
491 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]9f4e4f082013-06-21 07:11:19492
[email protected]59ee99d2013-10-11 15:46:16493 ReconcileKnownDisabled();
494
[email protected]2edd57852013-08-14 22:07:40495 // Attempt to re-enable extensions whose only disable reason is reloading.
496 std::vector<std::string> extensions_to_enable;
[email protected]bb1bc9b32013-12-21 03:09:14497 const ExtensionSet& disabled_extensions = registry_->disabled_extensions();
498 for (ExtensionSet::const_iterator iter = disabled_extensions.begin();
499 iter != disabled_extensions.end(); ++iter) {
[email protected]2edd57852013-08-14 22:07:40500 const Extension* e = iter->get();
501 if (extension_prefs_->GetDisableReasons(e->id()) ==
502 Extension::DISABLE_RELOAD) {
503 extensions_to_enable.push_back(e->id());
504 }
505 }
506 for (std::vector<std::string>::iterator it = extensions_to_enable.begin();
507 it != extensions_to_enable.end(); ++it) {
508 EnableExtension(*it);
509 }
510
[email protected]9f4e4f082013-06-21 07:11:19511 // Finish install (if possible) of extensions that were still delayed while
512 // the browser was shut down.
513 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> delayed_info(
514 extension_prefs_->GetAllDelayedInstallInfo());
515 for (size_t i = 0; i < delayed_info->size(); ++i) {
516 ExtensionInfo* info = delayed_info->at(i).get();
517 scoped_refptr<const Extension> extension(NULL);
518 if (info->extension_manifest) {
519 std::string error;
520 extension = Extension::Create(
521 info->extension_path,
522 info->extension_location,
523 *info->extension_manifest,
524 extension_prefs_->GetDelayedInstallCreationFlags(
525 info->extension_id),
526 info->extension_id,
527 &error);
528 if (extension.get())
529 delayed_installs_.Insert(extension);
530 }
531 }
532 MaybeFinishDelayedInstallations();
533
534 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> delayed_info2(
535 extension_prefs_->GetAllDelayedInstallInfo());
536 UMA_HISTOGRAM_COUNTS_100("Extensions.UpdateOnLoad",
537 delayed_info2->size() - delayed_info->size());
538
[email protected]47b60072013-06-20 10:35:56539 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25540
[email protected]47b60072013-06-20 10:35:56541 // TODO(erikkay) this should probably be deferred to a future point
542 // rather than running immediately at startup.
543 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57544
[email protected]68e5cc72013-12-17 02:41:53545 InstallVerifier* verifier =
546 extensions::ExtensionSystem::Get(profile_)->install_verifier();
547 if (verifier->NeedsBootstrap())
548 VerifyAllExtensions();
[email protected]47b60072013-06-20 10:35:56549 base::MessageLoop::current()->PostDelayedTask(
550 FROM_HERE,
551 base::Bind(&ExtensionService::GarbageCollectExtensions, AsWeakPtr()),
552 base::TimeDelta::FromSeconds(kGarbageCollectStartupDelay));
[email protected]215a7be2012-10-22 19:53:42553
[email protected]820d9bd2013-04-03 03:46:03554 if (extension_prefs_->NeedsStorageGarbageCollection()) {
555 GarbageCollectIsolatedStorage();
556 extension_prefs_->SetNeedsStorageGarbageCollection(false);
557 }
[email protected]4c9201c42013-08-16 04:56:21558 system_->management_policy()->RegisterProvider(
559 shared_module_policy_provider_.get());
[email protected]399583b2012-12-11 09:33:42560 }
[email protected]3350d712013-11-18 09:32:24561
562 UMA_HISTOGRAM_TIMES("Extensions.ExtensionServiceInitTime",
563 base::Time::Now() - begin_time);
[email protected]6014d672008-12-05 00:38:25564}
565
[email protected]68e5cc72013-12-17 02:41:53566void ExtensionService::VerifyAllExtensions() {
567 ExtensionIdSet to_add;
568 scoped_ptr<ExtensionSet> all_extensions = GenerateInstalledExtensionsSet();
569
570 for (ExtensionSet::const_iterator i = all_extensions->begin();
571 i != all_extensions->end(); ++i) {
572 const Extension& extension = **i;
573
[email protected]88622932014-01-16 17:59:12574 if (InstallVerifier::NeedsVerification(extension))
[email protected]68e5cc72013-12-17 02:41:53575 to_add.insert(extension.id());
576 }
577 extensions::ExtensionSystem::Get(profile_)->install_verifier()->AddMany(
578 to_add, base::Bind(&ExtensionService::FinishVerifyAllExtensions,
579 AsWeakPtr()));
580}
581
582void ExtensionService::FinishVerifyAllExtensions(bool success) {
583 if (success) {
584 // Check to see if any currently unverified extensions became verified.
585 InstallVerifier* verifier =
586 extensions::ExtensionSystem::Get(profile_)->install_verifier();
[email protected]bb1bc9b32013-12-21 03:09:14587 const ExtensionSet& disabled_extensions = registry_->disabled_extensions();
588 for (ExtensionSet::const_iterator i = disabled_extensions.begin();
589 i != disabled_extensions.end(); ++i) {
[email protected]68e5cc72013-12-17 02:41:53590 const Extension& extension = **i;
591 int disable_reasons = extension_prefs_->GetDisableReasons(extension.id());
592 if (disable_reasons & Extension::DISABLE_NOT_VERIFIED &&
593 !verifier->MustRemainDisabled(&extension, NULL, NULL)) {
594 extension_prefs_->RemoveDisableReason(extension.id(),
595 Extension::DISABLE_NOT_VERIFIED);
596 // Notify interested observers (eg the extensions settings page) by
597 // sending an UNLOADED notification.
598 //
599 // TODO(asargent) - this is a slight hack because it's already
600 // disabled; the right solution might be to add a separate listener
601 // interface for DisableReason's changing. http://crbug.com/328916
602 UnloadedExtensionInfo details(&extension,
603 UnloadedExtensionInfo::REASON_DISABLE);
604 content::NotificationService::current()->Notify(
605 chrome::NOTIFICATION_EXTENSION_UNLOADED,
606 content::Source<Profile>(profile_),
607 content::Details<UnloadedExtensionInfo>(&details));
608 }
609 }
610 // Might disable some extensions.
611 CheckManagementPolicy();
612 }
613}
614
[email protected]31bb5ee62012-09-12 22:58:40615bool ExtensionService::UpdateExtension(const std::string& id,
[email protected]650b2d52013-02-10 03:41:45616 const base::FilePath& extension_path,
[email protected]31bb5ee62012-09-12 22:58:40617 const GURL& download_url,
618 CrxInstaller** out_crx_installer) {
[email protected]a29a517a2011-01-21 21:11:12619 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]3c4abc82012-10-22 22:25:54620 if (browser_terminating_) {
621 LOG(WARNING) << "Skipping UpdateExtension due to browser shutdown";
622 // Leak the temp file at extension_path. We don't want to add to the disk
623 // I/O burden at shutdown, we can't rely on the I/O completing anyway, and
624 // the file is in the OS temp directory which should be cleaned up for us.
625 return false;
626 }
[email protected]a8af9fdb2010-10-28 21:52:20627
[email protected]3f213ad2012-07-26 23:39:41628 const extensions::PendingExtensionInfo* pending_extension_info =
[email protected]51a3bf8b2012-06-08 22:53:06629 pending_extension_manager()->GetById(id);
[email protected]8ef78fd2010-08-19 17:14:32630
[email protected]695b5712012-12-06 23:55:28631 const Extension* extension = GetInstalledExtension(id);
[email protected]51a3bf8b2012-06-08 22:53:06632 if (!pending_extension_info && !extension) {
[email protected]aa142702010-03-26 01:26:33633 LOG(WARNING) << "Will not update extension " << id
634 << " because it is not installed or pending";
635 // Delete extension_path since we're not creating a CrxInstaller
636 // that would do it for us.
[email protected]7f8f24f2012-11-15 19:40:14637 if (!GetFileTaskRunner()->PostTask(
638 FROM_HERE,
[email protected]53612e82011-10-18 18:00:36639 base::Bind(
[email protected]7296f2762011-11-21 19:23:44640 &extension_file_util::DeleteFile, extension_path, false)))
[email protected]14908b72011-04-20 06:54:36641 NOTREACHED();
[email protected]420a0ec2011-06-01 01:07:03642
643 return false;
[email protected]e957fe52009-06-23 16:51:05644 }
645
[email protected]aa142702010-03-26 01:26:33646 // We want a silent install only for non-pending extensions and
647 // pending extensions that have install_silently set.
[email protected]f8636f92013-08-09 21:02:37648 scoped_ptr<ExtensionInstallPrompt> client;
[email protected]91e51d612012-10-21 23:03:05649 if (pending_extension_info && !pending_extension_info->install_silently())
[email protected]f8636f92013-08-09 21:02:37650 client.reset(ExtensionInstallUI::CreateInstallPromptWithProfile(profile_));
[email protected]aa142702010-03-26 01:26:33651
[email protected]f8636f92013-08-09 21:02:37652 scoped_refptr<CrxInstaller> installer(
653 CrxInstaller::Create(this, client.Pass()));
[email protected]6dfbbf82010-03-12 23:09:16654 installer->set_expected_id(id);
[email protected]464213a2013-10-15 01:06:48655 int creation_flags = Extension::NO_FLAGS;
[email protected]51a3bf8b2012-06-08 22:53:06656 if (pending_extension_info) {
657 installer->set_install_source(pending_extension_info->install_source());
658 if (pending_extension_info->install_silently())
659 installer->set_allow_silent_install(true);
[email protected]464213a2013-10-15 01:06:48660 creation_flags = pending_extension_info->creation_flags();
661 if (pending_extension_info->mark_acknowledged())
662 AcknowledgeExternalExtension(id);
[email protected]51a3bf8b2012-06-08 22:53:06663 } else if (extension) {
[email protected]5eb375e92010-11-26 07:50:41664 installer->set_install_source(extension->location());
[email protected]51a3bf8b2012-06-08 22:53:06665 }
[email protected]7d8b7072012-04-07 01:07:46666 // If the extension was installed from or has migrated to the webstore, or
[email protected]fcb2c2c2012-10-17 21:08:45667 // its auto-update URL is from the webstore, treat it as a webstore install.
668 // Note that we ignore some older extensions with blank auto-update URLs
669 // because we are mostly concerned with restrictions on NaCl extensions,
670 // which are newer.
[email protected]75764512011-12-19 19:54:28671 if ((extension && extension->from_webstore()) ||
[email protected]4a5fe3e22013-06-04 07:06:38672 (extension && extensions::ManifestURL::UpdatesFromGallery(extension)) ||
[email protected]963d13c2012-09-29 17:13:35673 (!extension && extension_urls::IsWebstoreUpdateUrl(
[email protected]51a3bf8b2012-06-08 22:53:06674 pending_extension_info->update_url()))) {
[email protected]a12ce8b22012-01-17 18:40:53675 creation_flags |= Extension::FROM_WEBSTORE;
676 }
677
678 // Bookmark apps being updated is kind of a contradiction, but that's because
679 // we mark the default apps as bookmark apps, and they're hosted in the web
680 // store, thus they can get updated. See http://crbug.com/101605 for more
681 // details.
682 if (extension && extension->from_bookmark())
683 creation_flags |= Extension::FROM_BOOKMARK;
684
[email protected]e33bbc22012-08-27 22:05:46685 if (extension && extension->was_installed_by_default())
686 creation_flags |= Extension::WAS_INSTALLED_BY_DEFAULT;
687
[email protected]ab6c7be42014-01-16 02:05:54688 if (extension && extension->is_ephemeral())
689 creation_flags |= Extension::IS_EPHEMERAL;
690
[email protected]a12ce8b22012-01-17 18:40:53691 installer->set_creation_flags(creation_flags);
692
[email protected]6dfbbf82010-03-12 23:09:16693 installer->set_delete_source(true);
[email protected]655b2b1a2011-10-13 17:13:06694 installer->set_download_url(download_url);
[email protected]cb0e50312011-05-09 15:03:07695 installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
[email protected]6dfbbf82010-03-12 23:09:16696 installer->InstallCrx(extension_path);
[email protected]420a0ec2011-06-01 01:07:03697
698 if (out_crx_installer)
[email protected]dc24976f2013-06-02 21:15:09699 *out_crx_installer = installer.get();
[email protected]420a0ec2011-06-01 01:07:03700
701 return true;
[email protected]e957fe52009-06-23 16:51:05702}
703
[email protected]e178ad92013-06-28 02:29:25704void ExtensionService::ReloadExtension(const std::string extension_id) {
[email protected]a29a517a2011-01-21 21:11:12705 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]2a947202013-03-06 04:58:05706
707 // If the extension is already reloading, don't reload again.
708 if (extension_prefs_->GetDisableReasons(extension_id) &
709 Extension::DISABLE_RELOAD) {
710 return;
711 }
712
[email protected]650b2d52013-02-10 03:41:45713 base::FilePath path;
[email protected]9adb9692010-10-29 23:14:02714 const Extension* current_extension = GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40715
[email protected]f17dbd42010-08-16 23:21:10716 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]b65272f2009-08-31 15:47:06717 if (current_extension) {
[email protected]4814b512009-11-07 00:12:29718 // If the extension has an inspector open for its background page, detach
719 // the inspector and hang onto a cookie for it, so that we can reattach
720 // later.
[email protected]31d8f5f22012-04-02 15:22:08721 // TODO(yoz): this is not incognito-safe!
[email protected]98b6d942013-11-10 00:34:07722 extensions::ProcessManager* manager = system_->process_manager();
[email protected]3a1dc572012-07-31 22:25:13723 extensions::ExtensionHost* host =
724 manager->GetBackgroundHostForExtension(extension_id);
[email protected]98f66112012-12-25 12:59:36725 if (host && DevToolsAgentHost::HasFor(host->render_view_host())) {
[email protected]4814b512009-11-07 00:12:29726 // Look for an open inspector for the background page.
[email protected]04ea1bb2013-07-10 09:26:09727 scoped_refptr<DevToolsAgentHost> agent_host =
728 DevToolsAgentHost::GetOrCreateFor(host->render_view_host());
729 agent_host->DisconnectRenderViewHost();
730 orphaned_dev_tools_[extension_id] = agent_host;
[email protected]4814b512009-11-07 00:12:29731 }
732
[email protected]b65272f2009-08-31 15:47:06733 path = current_extension->path();
[email protected]58076192013-07-19 19:43:50734 // BeingUpgraded is set back to false when the extension is added.
[email protected]45f5b7d2014-01-22 23:47:13735 system_->runtime_data()->SetBeingUpgraded(current_extension, true);
[email protected]44d62b62012-04-11 00:06:03736 DisableExtension(extension_id, Extension::DISABLE_RELOAD);
[email protected]b914e2952013-04-26 07:10:03737 reloading_extensions_.insert(extension_id);
[email protected]1eb175082010-02-10 09:26:16738 } else {
739 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06740 }
741
[email protected]9f4e4f082013-06-21 07:11:19742 if (delayed_installs_.Contains(extension_id)) {
[email protected]6f6101832012-11-27 22:10:48743 FinishDelayedInstallation(extension_id);
[email protected]0db124b02012-11-07 04:55:05744 return;
745 }
746
[email protected]43ceb002012-02-10 23:19:15747 // If we're reloading a component extension, use the component extension
748 // loader's reloader.
749 if (component_loader_->Exists(extension_id)) {
[email protected]e178ad92013-06-28 02:29:25750 SetBeingReloaded(extension_id, true);
[email protected]43ceb002012-02-10 23:19:15751 component_loader_->Reload(extension_id);
[email protected]e178ad92013-06-28 02:29:25752 SetBeingReloaded(extension_id, false);
[email protected]43ceb002012-02-10 23:19:15753 return;
754 }
755
[email protected]e6090e42010-03-23 22:44:08756 // Check the installed extensions to see if what we're reloading was already
757 // installed.
[email protected]e178ad92013-06-28 02:29:25758 SetBeingReloaded(extension_id, true);
[email protected]e6090e42010-03-23 22:44:08759 scoped_ptr<ExtensionInfo> installed_extension(
760 extension_prefs_->GetInstalledExtensionInfo(extension_id));
761 if (installed_extension.get() &&
762 installed_extension->extension_manifest.get()) {
[email protected]d8c8f25f2011-11-02 18:18:01763 extensions::InstalledLoader(this).Load(*installed_extension, false);
[email protected]e6090e42010-03-23 22:44:08764 } else {
[email protected]d8c8f25f2011-11-02 18:18:01765 // Otherwise, the extension is unpacked (location LOAD).
[email protected]e6090e42010-03-23 22:44:08766 // We should always be able to remember the extension's path. If it's not in
767 // the map, someone failed to update |unloaded_extension_paths_|.
768 CHECK(!path.empty());
[email protected]d8c8f25f2011-11-02 18:18:01769 extensions::UnpackedInstaller::Create(this)->Load(path);
[email protected]e6090e42010-03-23 22:44:08770 }
[email protected]e178ad92013-06-28 02:29:25771 // When reloading is done, mark this extension as done reloading.
772 SetBeingReloaded(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40773}
774
[email protected]fa2416f2011-05-03 08:41:20775bool ExtensionService::UninstallExtension(
[email protected]a12c706e2011-12-01 00:58:08776 std::string extension_id,
[email protected]fa2416f2011-05-03 08:41:20777 bool external_uninstall,
[email protected]439f1e32013-12-09 20:09:09778 base::string16* error) {
[email protected]a29a517a2011-01-21 21:11:12779 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20780
[email protected]0d6ec3a72011-09-02 02:09:43781 scoped_refptr<const Extension> extension(GetInstalledExtension(extension_id));
[email protected]631cf822009-05-15 07:01:25782
[email protected]e7afe2452010-08-22 16:19:13783 // Callers should not send us nonexistent extensions.
[email protected]dc24976f2013-06-02 21:15:09784 CHECK(extension.get());
[email protected]9f1087e2009-06-15 17:29:32785
[email protected]95da88c42011-03-31 10:07:33786 // Policy change which triggers an uninstall will always set
787 // |external_uninstall| to true so this is the only way to uninstall
788 // managed extensions.
[email protected]65187152012-06-02 13:14:14789 if (!external_uninstall &&
790 !system_->management_policy()->UserMayModifySettings(
791 extension.get(), error)) {
[email protected]ad50def52011-10-19 23:17:07792 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27793 chrome::NOTIFICATION_EXTENSION_UNINSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:53794 content::Source<Profile>(profile_),
[email protected]dc24976f2013-06-02 21:15:09795 content::Details<const Extension>(extension.get()));
[email protected]d6ebc9792011-04-07 18:18:33796 return false;
797 }
[email protected]95da88c42011-03-31 10:07:33798
[email protected]65f173552012-06-28 22:43:58799 syncer::SyncChange sync_change;
[email protected]f8aefb132013-10-30 09:29:52800 if (extension_sync_service_) {
801 sync_change = extension_sync_service_->PrepareToSyncUninstallExtension(
802 extension.get(), is_ready());
[email protected]3bdba0d2011-08-23 07:17:30803 }
804
[email protected]ffd2f79e2013-11-14 00:11:46805 extensions::ExtensionSystem::Get(profile_)->install_verifier()->Remove(
806 extension->id());
807
[email protected]dc24976f2013-06-02 21:15:09808 if (IsUnacknowledgedExternalExtension(extension.get())) {
[email protected]d96eb512012-11-01 23:44:08809 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
810 EXTERNAL_EXTENSION_UNINSTALLED,
811 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:02812 if (extensions::ManifestURL::UpdatesFromGallery(extension.get())) {
813 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventWebstore",
814 EXTERNAL_EXTENSION_UNINSTALLED,
815 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
816 } else {
817 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventNonWebstore",
818 EXTERNAL_EXTENSION_UNINSTALLED,
819 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
820 }
[email protected]d96eb512012-11-01 23:44:08821 }
[email protected]9b217652010-10-08 22:04:23822 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08823 extension->GetType(), 100);
[email protected]dc24976f2013-06-02 21:15:09824 RecordPermissionMessagesHistogram(extension.get(),
825 "Extensions.Permissions_Uninstall");
[email protected]9b217652010-10-08 22:04:23826
[email protected]831aa212010-03-26 13:55:19827 // Unload before doing more cleanup to ensure that nothing is hanging on to
828 // any of these resources.
[email protected]b0af4792013-10-23 09:12:13829 UnloadExtension(extension_id, UnloadedExtensionInfo::REASON_UNINSTALL);
[email protected]831aa212010-03-26 13:55:19830
[email protected]9f1087e2009-06-15 17:29:32831 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]12075d12013-02-27 05:38:05832 if (!Manifest::IsUnpackedLocation(extension->location())) {
[email protected]7f8f24f2012-11-15 19:40:14833 if (!GetFileTaskRunner()->PostTask(
834 FROM_HERE,
[email protected]53612e82011-10-18 18:00:36835 base::Bind(
[email protected]14908b72011-04-20 06:54:36836 &extension_file_util::UninstallExtension,
837 install_directory_,
[email protected]fa2416f2011-05-03 08:41:20838 extension_id)))
[email protected]14908b72011-04-20 06:54:36839 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32840 }
841
[email protected]d656595b2014-01-09 14:09:35842 extensions::DataDeleter::StartDeleting(profile_, extension.get());
[email protected]0d6ec3a72011-09-02 02:09:43843
[email protected]0dfe05c2011-02-23 23:03:36844 UntrackTerminatedExtension(extension_id);
[email protected]211030342010-09-30 18:41:06845
846 // Notify interested parties that we've uninstalled this extension.
[email protected]ad50def52011-10-19 23:17:07847 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27848 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
[email protected]6c2381d2011-10-19 02:52:53849 content::Source<Profile>(profile_),
[email protected]dc24976f2013-06-02 21:15:09850 content::Details<const Extension>(extension.get()));
[email protected]d6ebc9792011-04-07 18:18:33851
[email protected]f8aefb132013-10-30 09:29:52852 if (extension_sync_service_) {
853 extension_sync_service_->ProcessSyncUninstallExtension(extension_id,
854 sync_change);
[email protected]3bdba0d2011-08-23 07:17:30855 }
856
[email protected]399583b2012-12-11 09:33:42857 delayed_installs_.Remove(extension_id);
[email protected]0db124b02012-11-07 04:55:05858
[email protected]198b5902013-06-27 10:36:11859 PruneSharedModulesOnUninstall(extension.get());
[email protected]9f4e4f082013-06-21 07:11:19860
[email protected]d4eda592013-09-18 03:37:57861 extension_prefs_->OnExtensionUninstalled(extension_id, extension->location(),
862 external_uninstall);
863
[email protected]333b1de2011-09-12 18:28:50864 // Track the uninstallation.
[email protected]49098f702011-10-13 03:47:18865 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionUninstalled", 1, 2);
866
[email protected]d6ebc9792011-04-07 18:18:33867 return true;
[email protected]c10da4b02010-03-25 14:38:32868}
869
[email protected]c3cfb012011-04-06 22:07:35870bool ExtensionService::IsExtensionEnabled(
871 const std::string& extension_id) const {
[email protected]bb1bc9b32013-12-21 03:09:14872 if (registry_->enabled_extensions().Contains(extension_id) ||
873 registry_->terminated_extensions().Contains(extension_id)) {
[email protected]36429da2011-07-11 20:25:18874 return true;
[email protected]dc9a74f72012-08-17 18:07:21875 }
[email protected]36429da2011-07-11 20:25:18876
[email protected]bb1bc9b32013-12-21 03:09:14877 if (registry_->disabled_extensions().Contains(extension_id) ||
878 registry_->blacklisted_extensions().Contains(extension_id)) {
[email protected]ad83ca242011-07-29 01:32:25879 return false;
[email protected]695b5712012-12-06 23:55:28880 }
[email protected]ad83ca242011-07-29 01:32:25881
882 // If the extension hasn't been loaded yet, check the prefs for it. Assume
883 // enabled unless otherwise noted.
884 return !extension_prefs_->IsExtensionDisabled(extension_id) &&
[email protected]f574c402012-12-04 23:20:31885 !extension_prefs_->IsExternalExtensionUninstalled(extension_id);
[email protected]c3cfb012011-04-06 22:07:35886}
887
888bool ExtensionService::IsExternalExtensionUninstalled(
889 const std::string& extension_id) const {
890 return extension_prefs_->IsExternalExtensionUninstalled(extension_id);
891}
892
[email protected]eaa7dd182010-12-14 11:09:00893void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12894 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20895
[email protected]06f92562011-04-29 19:27:31896 if (IsExtensionEnabled(extension_id))
[email protected]0c6da502009-08-14 22:32:39897 return;
[email protected]bb1bc9b32013-12-21 03:09:14898 const Extension* extension =
899 registry_->disabled_extensions().GetByID(extension_id);
[email protected]0d54b682013-11-05 14:15:36900
901 ManagementPolicy* policy = system_->management_policy();
902 if (extension && policy->MustRemainDisabled(extension, NULL, NULL)) {
903 UMA_HISTOGRAM_COUNTS_100("Extensions.EnableDeniedByPolicy", 1);
904 return;
905 }
[email protected]0c6da502009-08-14 22:32:39906
[email protected]b3317ad2011-04-28 23:46:00907 extension_prefs_->SetExtensionState(extension_id, Extension::ENABLED);
[email protected]eb5e4f92012-08-15 23:33:28908 extension_prefs_->ClearDisableReasons(extension_id);
[email protected]1784e83a2009-09-08 21:01:52909
[email protected]06f92562011-04-29 19:27:31910 // This can happen if sync enables an extension that is not
911 // installed yet.
912 if (!extension)
913 return;
914
[email protected]d96eb512012-11-01 23:44:08915 if (IsUnacknowledgedExternalExtension(extension)) {
916 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
917 EXTERNAL_EXTENSION_REENABLED,
918 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:02919 if (extensions::ManifestURL::UpdatesFromGallery(extension)) {
920 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventWebstore",
921 EXTERNAL_EXTENSION_REENABLED,
922 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
923 } else {
924 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventNonWebstore",
925 EXTERNAL_EXTENSION_REENABLED,
926 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
927 }
[email protected]612a1cb12012-10-17 13:18:03928 AcknowledgeExternalExtension(extension->id());
[email protected]d96eb512012-11-01 23:44:08929 }
[email protected]612a1cb12012-10-17 13:18:03930
[email protected]0c6da502009-08-14 22:32:39931 // Move it over to the enabled list.
[email protected]bb1bc9b32013-12-21 03:09:14932 registry_->AddEnabled(make_scoped_refptr(extension));
933 registry_->RemoveDisabled(extension->id());
[email protected]0c6da502009-08-14 22:32:39934
[email protected]62d30f42009-10-01 22:36:06935 NotifyExtensionLoaded(extension);
[email protected]3bdba0d2011-08-23 07:17:30936
[email protected]f74d7f32012-06-19 19:22:51937 // Notify listeners that the extension was enabled.
938 content::NotificationService::current()->Notify(
939 chrome::NOTIFICATION_EXTENSION_ENABLED,
940 content::Source<Profile>(profile_),
941 content::Details<const Extension>(extension));
942
[email protected]f8aefb132013-10-30 09:29:52943 if (extension_sync_service_)
944 extension_sync_service_->SyncEnableExtension(*extension);
[email protected]0c6da502009-08-14 22:32:39945}
946
[email protected]44d62b62012-04-11 00:06:03947void ExtensionService::DisableExtension(
948 const std::string& extension_id,
949 Extension::DisableReason disable_reason) {
[email protected]a29a517a2011-01-21 21:11:12950 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20951
[email protected]b2ba9962009-12-10 20:10:15952 // The extension may have been disabled already.
[email protected]06f92562011-04-29 19:27:31953 if (!IsExtensionEnabled(extension_id))
[email protected]1784e83a2009-09-08 21:01:52954 return;
[email protected]1784e83a2009-09-08 21:01:52955
[email protected]06f92562011-04-29 19:27:31956 const Extension* extension = GetInstalledExtension(extension_id);
957 // |extension| can be NULL if sync disables an extension that is not
958 // installed yet.
[email protected]65187152012-06-02 13:14:14959 if (extension &&
[email protected]505ccdc2013-06-20 20:15:35960 disable_reason != Extension::DISABLE_RELOAD &&
[email protected]65187152012-06-02 13:14:14961 !system_->management_policy()->UserMayModifySettings(extension, NULL)) {
[email protected]95da88c42011-03-31 10:07:33962 return;
[email protected]65187152012-06-02 13:14:14963 }
[email protected]95da88c42011-03-31 10:07:33964
[email protected]b3317ad2011-04-28 23:46:00965 extension_prefs_->SetExtensionState(extension_id, Extension::DISABLED);
[email protected]eb5e4f92012-08-15 23:33:28966 extension_prefs_->AddDisableReason(extension_id, disable_reason);
[email protected]1784e83a2009-09-08 21:01:52967
[email protected]599539802014-01-07 23:06:00968 int include_mask =
969 ExtensionRegistry::EVERYTHING & ~ExtensionRegistry::DISABLED;
970 extension = registry_->GetExtensionById(extension_id, include_mask);
[email protected]06f92562011-04-29 19:27:31971 if (!extension)
972 return;
973
[email protected]45f5b7d2014-01-22 23:47:13974 // The extension is either enabled or terminated.
975 DCHECK(registry_->enabled_extensions().Contains(extension->id()) ||
976 registry_->terminated_extensions().Contains(extension->id()));
[email protected]64742de32013-04-22 08:44:34977
[email protected]5c094792012-09-07 19:44:53978 // Move it over to the disabled list. Don't send a second unload notification
979 // for terminated extensions being disabled.
[email protected]bb1bc9b32013-12-21 03:09:14980 registry_->AddDisabled(make_scoped_refptr(extension));
981 if (registry_->enabled_extensions().Contains(extension->id())) {
982 registry_->RemoveEnabled(extension->id());
[email protected]b0af4792013-10-23 09:12:13983 NotifyExtensionUnloaded(extension, UnloadedExtensionInfo::REASON_DISABLE);
[email protected]5c094792012-09-07 19:44:53984 } else {
[email protected]bb1bc9b32013-12-21 03:09:14985 registry_->RemoveTerminated(extension->id());
[email protected]5c094792012-09-07 19:44:53986 }
[email protected]3bdba0d2011-08-23 07:17:30987
[email protected]f8aefb132013-10-30 09:29:52988 if (extension_sync_service_)
989 extension_sync_service_->SyncDisableExtension(*extension);
[email protected]1784e83a2009-09-08 21:01:52990}
991
[email protected]1abf05e2013-07-09 17:04:36992void ExtensionService::DisableUserExtensions(
993 const std::vector<std::string>& except_ids) {
[email protected]e516e4c2013-06-12 17:41:14994 extensions::ManagementPolicy* management_policy =
995 system_->management_policy();
996 extensions::ExtensionList to_disable;
997
[email protected]bb1bc9b32013-12-21 03:09:14998 const ExtensionSet& enabled_set = registry_->enabled_extensions();
999 for (ExtensionSet::const_iterator extension = enabled_set.begin();
1000 extension != enabled_set.end(); ++extension) {
[email protected]198b5902013-06-27 10:36:111001 if (management_policy->UserMayModifySettings(extension->get(), NULL))
[email protected]e516e4c2013-06-12 17:41:141002 to_disable.push_back(*extension);
1003 }
[email protected]bb1bc9b32013-12-21 03:09:141004 const ExtensionSet& terminated_set = registry_->terminated_extensions();
1005 for (ExtensionSet::const_iterator extension = terminated_set.begin();
1006 extension != terminated_set.end(); ++extension) {
[email protected]198b5902013-06-27 10:36:111007 if (management_policy->UserMayModifySettings(extension->get(), NULL))
[email protected]e516e4c2013-06-12 17:41:141008 to_disable.push_back(*extension);
1009 }
1010
1011 for (extensions::ExtensionList::const_iterator extension = to_disable.begin();
1012 extension != to_disable.end(); ++extension) {
[email protected]3082fe32013-08-06 11:12:381013 if ((*extension)->was_installed_by_default() &&
1014 extension_urls::IsWebstoreUpdateUrl(
1015 extensions::ManifestURL::GetUpdateURL(*extension)))
1016 continue;
[email protected]1abf05e2013-07-09 17:04:361017 const std::string& id = (*extension)->id();
1018 if (except_ids.end() == std::find(except_ids.begin(), except_ids.end(), id))
1019 DisableExtension(id, extensions::Extension::DISABLE_USER_ACTION);
[email protected]e516e4c2013-06-12 17:41:141020 }
1021}
1022
[email protected]eaa7dd182010-12-14 11:09:001023void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]009633c2013-03-07 22:08:281024 const Extension* extension) {
1025 GrantPermissions(extension);
[email protected]fe2dd7742011-04-19 22:52:491026 RecordPermissionMessagesHistogram(
1027 extension, "Extensions.Permissions_ReEnable");
[email protected]8d888c12010-11-30 00:00:251028 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
1029 EnableExtension(extension->id());
1030}
1031
[email protected]009633c2013-03-07 22:08:281032void ExtensionService::GrantPermissions(const Extension* extension) {
[email protected]be083862012-09-01 03:53:451033 CHECK(extension);
1034 extensions::PermissionsUpdater perms_updater(profile());
[email protected]009633c2013-03-07 22:08:281035 perms_updater.GrantActivePermissions(extension);
[email protected]be083862012-09-01 03:53:451036}
1037
[email protected]fe2dd7742011-04-19 22:52:491038// static
1039void ExtensionService::RecordPermissionMessagesHistogram(
[email protected]13c68b62013-05-17 11:29:051040 const Extension* extension, const char* histogram) {
[email protected]de415552013-01-23 04:12:171041 // Since this is called from multiple sources, and since the histogram macros
1042 // use statics, we need to manually lookup the histogram ourselves.
1043 base::HistogramBase* counter = base::LinearHistogram::FactoryGet(
[email protected]fe2dd7742011-04-19 22:52:491044 histogram,
1045 1,
[email protected]c2e66e12012-06-27 06:27:061046 PermissionMessage::kEnumBoundary,
1047 PermissionMessage::kEnumBoundary + 1,
[email protected]de415552013-01-23 04:12:171048 base::HistogramBase::kUmaTargetedHistogramFlag);
[email protected]fe2dd7742011-04-19 22:52:491049
[email protected]13c68b62013-05-17 11:29:051050 PermissionMessages permissions =
1051 extensions::PermissionsData::GetPermissionMessages(extension);
[email protected]fe2dd7742011-04-19 22:52:491052 if (permissions.empty()) {
[email protected]c2e66e12012-06-27 06:27:061053 counter->Add(PermissionMessage::kNone);
[email protected]fe2dd7742011-04-19 22:52:491054 } else {
[email protected]c2e66e12012-06-27 06:27:061055 for (PermissionMessages::iterator it = permissions.begin();
[email protected]0d3e4a22011-06-23 19:02:521056 it != permissions.end(); ++it)
1057 counter->Add(it->id());
[email protected]fe2dd7742011-04-19 22:52:491058 }
1059}
1060
[email protected]eaa7dd182010-12-14 11:09:001061void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]57a777f72010-03-31 01:09:421062 // The ChromeURLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:061063 // was loaded, otherwise a race can arise where a renderer that is created
1064 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:421065 // that the request context doesn't yet know about. The profile is responsible
1066 // for ensuring its URLRequestContexts appropriately discover the loaded
1067 // extension.
[email protected]31d8f5f22012-04-02 15:22:081068 system_->RegisterExtensionWithRequestContexts(extension);
[email protected]62d30f42009-10-01 22:36:061069
[email protected]d5949312012-12-03 22:13:301070 // Tell renderers about the new extension, unless it's a theme (renderers
1071 // don't need to know about themes).
[email protected]a58f599c2012-12-01 03:08:191072 if (!extension->is_theme()) {
[email protected]19647262011-12-16 09:57:491073 for (content::RenderProcessHost::iterator i(
1074 content::RenderProcessHost::AllHostsIterator());
1075 !i.IsAtEnd(); i.Advance()) {
1076 content::RenderProcessHost* host = i.GetCurrentValue();
1077 Profile* host_profile =
1078 Profile::FromBrowserContext(host->GetBrowserContext());
1079 if (host_profile->GetOriginalProfile() ==
1080 profile_->GetOriginalProfile()) {
1081 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions(
1082 1, ExtensionMsg_Loaded_Params(extension));
1083 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
1094 // NOTIFICATION_EXTENSION_LOADED, the renderer is guaranteed to know about it.
1095 content::NotificationService::current()->Notify(
1096 chrome::NOTIFICATION_EXTENSION_LOADED,
1097 content::Source<Profile>(profile_),
1098 content::Details<const Extension>(extension));
1099
[email protected]c8d407e2011-04-28 21:27:171100 // Tell a random-ass collection of other subsystems about the new extension.
1101 // TODO(aa): What should we do with all this goop? Can it move into the
1102 // relevant objects via EXTENSION_LOADED?
1103
1104 profile_->GetExtensionSpecialStoragePolicy()->
1105 GrantRightsForExtension(extension);
1106
1107 UpdateActiveExtensionsInCrashReporter();
1108
[email protected]c8d407e2011-04-28 21:27:171109 // If the extension has permission to load chrome://favicon/ resources we need
1110 // to make sure that the FaviconSource is registered with the
1111 // ChromeURLDataManager.
[email protected]13c68b62013-05-17 11:29:051112 if (extensions::PermissionsData::HasHostPermission(
1113 extension, GURL(chrome::kChromeUIFaviconURL))) {
[email protected]c8d407e2011-04-28 21:27:171114 FaviconSource* favicon_source = new FaviconSource(profile_,
1115 FaviconSource::FAVICON);
[email protected]24ea7a12013-01-27 23:54:531116 content::URLDataSource::Add(profile_, favicon_source);
[email protected]c8d407e2011-04-28 21:27:171117 }
[email protected]b07e606e2012-09-15 20:16:151118
1119#if !defined(OS_ANDROID)
1120 // Same for chrome://theme/ resources.
[email protected]13c68b62013-05-17 11:29:051121 if (extensions::PermissionsData::HasHostPermission(
1122 extension, GURL(chrome::kChromeUIThemeURL))) {
[email protected]b07e606e2012-09-15 20:16:151123 ThemeSource* theme_source = new ThemeSource(profile_);
[email protected]24ea7a12013-01-27 23:54:531124 content::URLDataSource::Add(profile_, theme_source);
[email protected]b07e606e2012-09-15 20:16:151125 }
1126#endif
1127
[email protected]5eddc3e2011-10-26 04:33:311128 // Same for chrome://thumb/ resources.
[email protected]13c68b62013-05-17 11:29:051129 if (extensions::PermissionsData::HasHostPermission(
1130 extension, GURL(chrome::kChromeUIThumbnailURL))) {
[email protected]420e6d92013-09-17 01:48:511131 ThumbnailSource* thumbnail_source = new ThumbnailSource(profile_, false);
[email protected]24ea7a12013-01-27 23:54:531132 content::URLDataSource::Add(profile_, thumbnail_source);
[email protected]5eddc3e2011-10-26 04:33:311133 }
[email protected]62d30f42009-10-01 22:36:061134}
1135
[email protected]a9f39a312010-12-23 22:14:271136void ExtensionService::NotifyExtensionUnloaded(
[email protected]814a7bf0f2011-08-13 05:30:591137 const Extension* extension,
[email protected]b0af4792013-10-23 09:12:131138 UnloadedExtensionInfo::Reason reason) {
[email protected]45f5b7d2014-01-22 23:47:131139 registry_->TriggerOnUnloaded(extension);
1140
[email protected]a9f39a312010-12-23 22:14:271141 UnloadedExtensionInfo details(extension, reason);
[email protected]ad50def52011-10-19 23:17:071142 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271143 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]6c2381d2011-10-19 02:52:531144 content::Source<Profile>(profile_),
1145 content::Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061146
[email protected]f3b1a082011-11-18 00:34:301147 for (content::RenderProcessHost::iterator i(
1148 content::RenderProcessHost::AllHostsIterator());
[email protected]77a6970c2011-04-23 16:58:561149 !i.IsAtEnd(); i.Advance()) {
[email protected]f3b1a082011-11-18 00:34:301150 content::RenderProcessHost* host = i.GetCurrentValue();
[email protected]9b62ecf2011-07-27 20:23:081151 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:301152 Profile::FromBrowserContext(host->GetBrowserContext());
[email protected]9b62ecf2011-07-27 20:23:081153 if (host_profile->GetOriginalProfile() == profile_->GetOriginalProfile())
[email protected]c8d407e2011-04-28 21:27:171154 host->Send(new ExtensionMsg_Unloaded(extension->id()));
[email protected]77a6970c2011-04-23 16:58:561155 }
1156
[email protected]31d8f5f22012-04-02 15:22:081157 system_->UnregisterExtensionWithRequestContexts(extension->id(), reason);
[email protected]c8d407e2011-04-28 21:27:171158 profile_->GetExtensionSpecialStoragePolicy()->
1159 RevokeRightsForExtension(extension);
1160
[email protected]b777b332011-04-16 04:01:081161#if defined(OS_CHROMEOS)
[email protected]ac66e452013-01-11 09:21:231162 // Revoke external file access for the extension from its file system context.
1163 // It is safe to access the extension's storage partition at this point. The
1164 // storage partition may get destroyed only after the extension gets unloaded.
1165 GURL site = extensions::ExtensionSystem::Get(profile_)->extension_service()->
1166 GetSiteForExtensionId(extension->id());
[email protected]10eb28162012-09-18 03:04:091167 fileapi::FileSystemContext* filesystem_context =
[email protected]ac66e452013-01-11 09:21:231168 BrowserContext::GetStoragePartitionForSite(profile_, site)->
[email protected]10eb28162012-09-18 03:04:091169 GetFileSystemContext();
[email protected]f19bbf62013-07-09 01:22:321170 if (filesystem_context && filesystem_context->external_backend()) {
1171 filesystem_context->external_backend()->
[email protected]c8d407e2011-04-28 21:27:171172 RevokeAccessForExtension(extension->id());
[email protected]62d30f42009-10-01 22:36:061173 }
[email protected]c8d407e2011-04-28 21:27:171174#endif
1175
1176 UpdateActiveExtensionsInCrashReporter();
[email protected]62d30f42009-10-01 22:36:061177}
1178
[email protected]0a071a32011-02-08 00:18:241179Profile* ExtensionService::profile() {
1180 return profile_;
1181}
1182
[email protected]599539802014-01-07 23:06:001183content::BrowserContext* ExtensionService::GetBrowserContext() const {
1184 // Implemented in the .cc file to avoid adding a profile.h dependency to
1185 // extension_service.h.
1186 return profile_;
1187}
1188
[email protected]45759612012-07-10 17:21:231189extensions::ExtensionPrefs* ExtensionService::extension_prefs() {
[email protected]ddb1e5a2010-12-13 20:10:451190 return extension_prefs_;
1191}
1192
[email protected]a7ff4b72013-10-17 20:56:021193const extensions::ExtensionPrefs* ExtensionService::extension_prefs() const {
1194 return extension_prefs_;
1195}
1196
[email protected]0d9a2202011-11-09 13:48:411197extensions::SettingsFrontend* ExtensionService::settings_frontend() {
[email protected]cccdf0aa2011-11-11 03:43:381198 return settings_frontend_.get();
[email protected]b7f853e282011-08-10 09:24:201199}
1200
[email protected]e2d720aa2012-05-17 00:11:351201extensions::ContentSettingsStore* ExtensionService::GetContentSettingsStore() {
[email protected]b790b072011-05-20 09:46:441202 return extension_prefs()->content_settings_store();
1203}
1204
[email protected]25ae0152011-11-18 14:40:021205bool ExtensionService::is_ready() {
[email protected]4a10006a2013-05-17 23:18:351206 return ready_->is_signaled();
[email protected]25ae0152011-11-18 14:40:021207}
1208
[email protected]7f8f24f2012-11-15 19:40:141209base::SequencedTaskRunner* ExtensionService::GetFileTaskRunner() {
[email protected]dc24976f2013-06-02 21:15:091210 if (file_task_runner_.get())
1211 return file_task_runner_.get();
[email protected]7f8f24f2012-11-15 19:40:141212
1213 // We should be able to interrupt any part of extension install process during
1214 // shutdown. SKIP_ON_SHUTDOWN ensures that not started extension install tasks
1215 // will be ignored/deleted while we will block on started tasks.
1216 std::string token("ext_install-");
1217 token.append(profile_->GetPath().AsUTF8Unsafe());
1218 file_task_runner_ = BrowserThread::GetBlockingPool()->
1219 GetSequencedTaskRunnerWithShutdownBehavior(
1220 BrowserThread::GetBlockingPool()->GetNamedSequenceToken(token),
1221 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
[email protected]dc24976f2013-06-02 21:15:091222 return file_task_runner_.get();
[email protected]7f8f24f2012-11-15 19:40:141223}
1224
[email protected]42a08162012-03-16 18:09:111225extensions::ExtensionUpdater* ExtensionService::updater() {
[email protected]2859946f2011-04-04 18:18:061226 return updater_.get();
1227}
1228
[email protected]4ee07c62012-08-21 12:40:421229void ExtensionService::CheckManagementPolicy() {
[email protected]0d54b682013-11-05 14:15:361230 std::vector<std::string> to_unload;
1231 std::map<std::string, Extension::DisableReason> to_disable;
[email protected]695b5712012-12-06 23:55:281232
[email protected]0d54b682013-11-05 14:15:361233 // Loop through the extensions list, finding extensions we need to unload or
1234 // disable.
[email protected]bb1bc9b32013-12-21 03:09:141235 const ExtensionSet& extensions = registry_->enabled_extensions();
1236 for (ExtensionSet::const_iterator iter = extensions.begin();
1237 iter != extensions.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361238 const Extension* extension = (iter->get());
[email protected]4ee07c62012-08-21 12:40:421239 if (!system_->management_policy()->UserMayLoad(extension, NULL))
[email protected]0d54b682013-11-05 14:15:361240 to_unload.push_back(extension->id());
1241 Extension::DisableReason disable_reason = Extension::DISABLE_NONE;
1242 if (system_->management_policy()->MustRemainDisabled(
1243 extension, &disable_reason, NULL))
1244 to_disable[extension->id()] = disable_reason;
[email protected]aa96d3a2010-08-21 08:45:251245 }
1246
[email protected]0d54b682013-11-05 14:15:361247 for (size_t i = 0; i < to_unload.size(); ++i)
1248 UnloadExtension(to_unload[i], UnloadedExtensionInfo::REASON_DISABLE);
1249
1250 for (std::map<std::string, Extension::DisableReason>::const_iterator i =
1251 to_disable.begin(); i != to_disable.end(); ++i)
1252 DisableExtension(i->first, i->second);
[email protected]aa96d3a2010-08-21 08:45:251253}
1254
[email protected]31206602011-04-13 23:07:321255void ExtensionService::CheckForUpdatesSoon() {
[email protected]c3cfb012011-04-06 22:07:351256 if (updater()) {
[email protected]94fde232012-04-27 10:22:301257 if (AreAllExternalProvidersReady()) {
1258 updater()->CheckSoon();
1259 } else {
1260 // Sync can start updating before all the external providers are ready
1261 // during startup. Start the update as soon as those providers are ready,
1262 // but not before.
1263 update_once_all_providers_are_ready_ = true;
1264 }
[email protected]c3cfb012011-04-06 22:07:351265 } else {
[email protected]31206602011-04-13 23:07:321266 LOG(WARNING) << "CheckForUpdatesSoon() called with auto-update turned off";
[email protected]c3cfb012011-04-06 22:07:351267 }
1268}
1269
[email protected]168389f2011-12-20 17:12:481270void ExtensionService::OnExtensionMoved(
1271 const std::string& moved_extension_id,
1272 const std::string& predecessor_extension_id,
1273 const std::string& successor_extension_id) {
[email protected]74474042013-11-21 12:03:541274 extension_prefs_->app_sorting()->OnExtensionMoved(
[email protected]3569b502012-01-12 20:08:231275 moved_extension_id,
1276 predecessor_extension_id,
1277 successor_extension_id);
[email protected]168389f2011-12-20 17:12:481278
1279 const Extension* extension = GetInstalledExtension(moved_extension_id);
[email protected]f8aefb132013-10-30 09:29:521280 if (extension_sync_service_ && extension) {
1281 extension_sync_service_->SyncExtensionChangeIfNeeded(*extension);
1282 }
[email protected]168389f2011-12-20 17:12:481283}
1284
[email protected]8e4560b62011-01-14 10:09:141285// Some extensions will autoupdate themselves externally from Chrome. These
1286// are typically part of some larger client application package. To support
1287// these, the extension will register its location in the the preferences file
1288// (and also, on Windows, in the registry) and this code will periodically
1289// check that location for a .crx file, which it will then install locally if
1290// a new version is available.
1291// Errors are reported through ExtensionErrorReporter. Succcess is not
1292// reported.
[email protected]eaa7dd182010-12-14 11:09:001293void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:121294 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]8e4560b62011-01-14 10:09:141295
1296 // Note that this installation is intentionally silent (since it didn't
1297 // go through the front-end). Extensions that are registered in this
1298 // way are effectively considered 'pre-bundled', and so implicitly
1299 // trusted. In general, if something has HKLM or filesystem access,
1300 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:121301
[email protected]8e4560b62011-01-14 10:09:141302 // Ask each external extension provider to give us a call back for each
1303 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
[email protected]5df038b2012-07-16 19:03:271304 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141305 for (i = external_extension_providers_.begin();
1306 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:271307 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:141308 provider->VisitRegisteredExtension();
1309 }
1310
[email protected]50067e52011-10-20 23:17:071311 // Do any required work that we would have done after completion of all
1312 // providers.
1313 if (external_extension_providers_.empty()) {
1314 OnAllExternalProvidersReady();
1315 }
[email protected]9f1087e2009-06-15 17:29:321316}
1317
[email protected]50067e52011-10-20 23:17:071318void ExtensionService::OnExternalProviderReady(
[email protected]5df038b2012-07-16 19:03:271319 const extensions::ExternalProviderInterface* provider) {
[email protected]a29a517a2011-01-21 21:11:121320 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]50067e52011-10-20 23:17:071321 CHECK(provider->IsReady());
[email protected]a29a517a2011-01-21 21:11:121322
1323 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:141324 // if all of them are finished. So we check them first.
[email protected]94fde232012-04-27 10:22:301325 if (AreAllExternalProvidersReady())
1326 OnAllExternalProvidersReady();
1327}
1328
1329bool ExtensionService::AreAllExternalProvidersReady() const {
[email protected]5df038b2012-07-16 19:03:271330 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141331 for (i = external_extension_providers_.begin();
1332 i != external_extension_providers_.end(); ++i) {
[email protected]94fde232012-04-27 10:22:301333 if (!i->get()->IsReady())
1334 return false;
[email protected]8e4560b62011-01-14 10:09:141335 }
[email protected]94fde232012-04-27 10:22:301336 return true;
[email protected]50067e52011-10-20 23:17:071337}
1338
1339void ExtensionService::OnAllExternalProvidersReady() {
1340 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]94fde232012-04-27 10:22:301341 base::TimeDelta elapsed = base::Time::Now() - profile_->GetStartTime();
1342 UMA_HISTOGRAM_TIMES("Extension.ExternalProvidersReadyAfter", elapsed);
[email protected]50067e52011-10-20 23:17:071343
1344 // Install any pending extensions.
[email protected]94fde232012-04-27 10:22:301345 if (update_once_all_providers_are_ready_ && updater()) {
1346 update_once_all_providers_are_ready_ = false;
[email protected]4e9b59d2013-12-18 06:47:141347 extensions::ExtensionUpdater::CheckParams params;
1348 params.callback = external_updates_finished_callback_;
1349 updater()->CheckNow(params);
[email protected]8e4560b62011-01-14 10:09:141350 }
1351
1352 // Uninstall all the unclaimed extensions.
[email protected]45759612012-07-10 17:21:231353 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> extensions_info(
[email protected]8e4560b62011-01-14 10:09:141354 extension_prefs_->GetInstalledExtensionsInfo());
1355 for (size_t i = 0; i < extensions_info->size(); ++i) {
1356 ExtensionInfo* info = extensions_info->at(i).get();
[email protected]1d5e58b2013-01-31 08:41:401357 if (Manifest::IsExternalLocation(info->extension_location))
[email protected]8e4560b62011-01-14 10:09:141358 CheckExternalUninstall(info->extension_id);
1359 }
[email protected]e5af875f2011-10-10 21:09:141360 IdentifyAlertableExtensions();
[email protected]e5af875f2011-10-10 21:09:141361}
1362
1363void ExtensionService::IdentifyAlertableExtensions() {
1364 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1365
[email protected]07c9f2f42012-02-29 18:45:221366 // Build up the lists of extensions that require acknowledgment. If this is
1367 // the first time, grandfather extensions that would have caused
1368 // notification.
[email protected]89226982012-07-16 20:09:181369 extension_error_ui_.reset(ExtensionErrorUI::Create(this));
[email protected]e5af875f2011-10-10 21:09:141370
[email protected]a9aa5932012-01-25 08:27:401371 bool did_show_alert = false;
[email protected]89226982012-07-16 20:09:181372 if (PopulateExtensionErrorUI(extension_error_ui_.get())) {
[email protected]460c6712013-04-24 07:20:011373 if (!is_first_run_) {
[email protected]a9aa5932012-01-25 08:27:401374 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]89226982012-07-16 20:09:181375 did_show_alert = extension_error_ui_->ShowErrorInBubbleView();
[email protected]e5af875f2011-10-10 21:09:141376 } else {
1377 // First run. Just acknowledge all the extensions, silently, by
1378 // shortcutting the display of the UI and going straight to the
1379 // callback for pressing the Accept button.
[email protected]a9aa5932012-01-25 08:27:401380 HandleExtensionAlertAccept();
[email protected]e5af875f2011-10-10 21:09:141381 }
1382 }
[email protected]a9aa5932012-01-25 08:27:401383
[email protected]612a1cb12012-10-17 13:18:031384 UpdateExternalExtensionAlert();
1385
[email protected]a9aa5932012-01-25 08:27:401386 if (!did_show_alert)
[email protected]89226982012-07-16 20:09:181387 extension_error_ui_.reset();
[email protected]e5af875f2011-10-10 21:09:141388}
1389
[email protected]89226982012-07-16 20:09:181390bool ExtensionService::PopulateExtensionErrorUI(
1391 ExtensionErrorUI* extension_error_ui) {
[email protected]07c9f2f42012-02-29 18:45:221392 bool needs_alert = false;
[email protected]695b5712012-12-06 23:55:281393
1394 // Extensions that are blacklisted.
[email protected]bb1bc9b32013-12-21 03:09:141395 const ExtensionSet& blacklisted_set = registry_->blacklisted_extensions();
1396 for (ExtensionSet::const_iterator it = blacklisted_set.begin();
1397 it != blacklisted_set.end(); ++it) {
[email protected]695b5712012-12-06 23:55:281398 std::string id = (*it)->id();
1399 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(id)) {
1400 extension_error_ui->AddBlacklistedExtension(id);
1401 needs_alert = true;
1402 }
1403 }
1404
[email protected]bb1bc9b32013-12-21 03:09:141405 const ExtensionSet& enabled_set = registry_->enabled_extensions();
1406 for (ExtensionSet::const_iterator iter = enabled_set.begin();
1407 iter != enabled_set.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361408 const Extension* e = iter->get();
[email protected]695b5712012-12-06 23:55:281409
[email protected]870f5572013-12-19 12:30:151410 // Skip for extensions that have pending updates. They will be checked again
1411 // once the pending update is finished.
1412 if (pending_extension_manager()->IsIdPending(e->id()))
1413 continue;
1414
[email protected]695b5712012-12-06 23:55:281415 // Extensions disabled by policy. Note: this no longer includes blacklisted
1416 // extensions, though we still show the same UI.
[email protected]fdd679b2012-11-15 20:49:391417 if (!system_->management_policy()->UserMayLoad(e, NULL)) {
[email protected]07c9f2f42012-02-29 18:45:221418 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(e->id())) {
[email protected]89226982012-07-16 20:09:181419 extension_error_ui->AddBlacklistedExtension(e->id());
[email protected]07c9f2f42012-02-29 18:45:221420 needs_alert = true;
1421 }
1422 }
[email protected]07c9f2f42012-02-29 18:45:221423 }
[email protected]695b5712012-12-06 23:55:281424
[email protected]07c9f2f42012-02-29 18:45:221425 return needs_alert;
1426}
1427
[email protected]a9aa5932012-01-25 08:27:401428void ExtensionService::HandleExtensionAlertClosed() {
[email protected]612a1cb12012-10-17 13:18:031429 const ExtensionIdSet* extension_ids =
1430 extension_error_ui_->get_blacklisted_extension_ids();
[email protected]e5af875f2011-10-10 21:09:141431 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1432 iter != extension_ids->end(); ++iter) {
1433 extension_prefs_->AcknowledgeBlacklistedExtension(*iter);
1434 }
[email protected]879a6092013-03-06 04:12:501435 extension_error_ui_.reset();
1436}
1437
1438void ExtensionService::HandleExtensionAlertAccept() {
[email protected]73a85e012013-04-04 10:45:301439 extension_error_ui_->Close();
[email protected]e5af875f2011-10-10 21:09:141440}
1441
[email protected]47fc70c2011-12-06 07:29:511442void ExtensionService::AcknowledgeExternalExtension(const std::string& id) {
1443 extension_prefs_->AcknowledgeExternalExtension(id);
[email protected]612a1cb12012-10-17 13:18:031444 UpdateExternalExtensionAlert();
[email protected]47fc70c2011-12-06 07:29:511445}
1446
[email protected]d96eb512012-11-01 23:44:081447bool ExtensionService::IsUnacknowledgedExternalExtension(
1448 const Extension* extension) {
[email protected]b82d18da2012-11-09 00:11:321449 if (!FeatureSwitch::prompt_for_external_extensions()->IsEnabled())
1450 return false;
1451
[email protected]1d5e58b2013-01-31 08:41:401452 return (Manifest::IsExternalLocation(extension->location()) &&
[email protected]d96eb512012-11-01 23:44:081453 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id()) &&
1454 !(extension_prefs_->GetDisableReasons(extension->id()) &
1455 Extension::DISABLE_SIDELOAD_WIPEOUT));
1456}
1457
[email protected]59ee99d2013-10-11 15:46:161458void ExtensionService::ReconcileKnownDisabled() {
[email protected]60a8bc962013-12-03 03:17:271459 ExtensionIdSet known_disabled_ids;
1460 if (!extension_prefs_->GetKnownDisabled(&known_disabled_ids)) {
[email protected]bb1bc9b32013-12-21 03:09:141461 extension_prefs_->SetKnownDisabled(
1462 registry_->disabled_extensions().GetIDs());
[email protected]018a5682013-11-20 02:41:211463 UMA_HISTOGRAM_BOOLEAN("Extensions.KnownDisabledInitialized", true);
1464 return;
[email protected]59ee99d2013-10-11 15:46:161465 }
[email protected]195c1582013-10-29 18:42:231466
[email protected]bb1bc9b32013-12-21 03:09:141467 // Both |known_disabled_ids| and |extensions| are ordered (by definition
[email protected]018a5682013-11-20 02:41:211468 // of std::map and std::set). Iterate forward over both sets in parallel
1469 // to find matching IDs and disable the corresponding extensions.
[email protected]bb1bc9b32013-12-21 03:09:141470 const ExtensionSet& enabled_set = registry_->enabled_extensions();
1471 ExtensionSet::const_iterator extensions_it = enabled_set.begin();
[email protected]018a5682013-11-20 02:41:211472 ExtensionIdSet::const_iterator known_disabled_ids_it =
1473 known_disabled_ids.begin();
1474 int known_disabled_count = 0;
[email protected]bb1bc9b32013-12-21 03:09:141475 while (extensions_it != enabled_set.end() &&
[email protected]018a5682013-11-20 02:41:211476 known_disabled_ids_it != known_disabled_ids.end()) {
1477 const std::string& extension_id = extensions_it->get()->id();
1478 const int comparison = extension_id.compare(*known_disabled_ids_it);
1479 if (comparison < 0) {
1480 ++extensions_it;
1481 } else if (comparison > 0) {
1482 ++known_disabled_ids_it;
1483 } else {
1484 ++known_disabled_count;
1485 // Advance |extensions_it| immediately as it will be invalidated upon
1486 // disabling the extension it points to.
1487 ++extensions_it;
1488 ++known_disabled_ids_it;
1489 DisableExtension(extension_id, Extension::DISABLE_KNOWN_DISABLED);
1490 }
1491 }
1492 UMA_HISTOGRAM_COUNTS_100("Extensions.KnownDisabledReDisabled",
1493 known_disabled_count);
1494
[email protected]195c1582013-10-29 18:42:231495 // Update the list of known disabled to reflect every change to
1496 // |disabled_extensions_| from this point forward.
[email protected]bb1bc9b32013-12-21 03:09:141497 registry_->SetDisabledModificationCallback(
[email protected]195c1582013-10-29 18:42:231498 base::Bind(&extensions::ExtensionPrefs::SetKnownDisabled,
1499 base::Unretained(extension_prefs_)));
[email protected]59ee99d2013-10-11 15:46:161500}
1501
[email protected]89226982012-07-16 20:09:181502void ExtensionService::HandleExtensionAlertDetails() {
1503 extension_error_ui_->ShowExtensions();
[email protected]73a85e012013-04-04 10:45:301504 // ShowExtensions may cause the error UI to close synchronously, e.g. if it
1505 // causes a navigation.
1506 if (extension_error_ui_)
1507 extension_error_ui_->Close();
[email protected]6c751e72010-11-23 10:11:101508}
1509
[email protected]612a1cb12012-10-17 13:18:031510void ExtensionService::UpdateExternalExtensionAlert() {
[email protected]41070e8d2012-10-24 01:34:361511 if (!FeatureSwitch::prompt_for_external_extensions()->IsEnabled())
[email protected]612a1cb12012-10-17 13:18:031512 return;
1513
1514 const Extension* extension = NULL;
[email protected]bb1bc9b32013-12-21 03:09:141515 const ExtensionSet& disabled_extensions = registry_->disabled_extensions();
1516 for (ExtensionSet::const_iterator iter = disabled_extensions.begin();
1517 iter != disabled_extensions.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361518 const Extension* e = iter->get();
[email protected]d96eb512012-11-01 23:44:081519 if (IsUnacknowledgedExternalExtension(e)) {
1520 extension = e;
1521 break;
[email protected]612a1cb12012-10-17 13:18:031522 }
1523 }
1524
1525 if (extension) {
[email protected]d96eb512012-11-01 23:44:081526 if (!extensions::HasExternalInstallError(this)) {
1527 if (extension_prefs_->IncrementAcknowledgePromptCount(extension->id()) >
[email protected]612a1cb12012-10-17 13:18:031528 kMaxExtensionAcknowledgePromptCount) {
[email protected]d96eb512012-11-01 23:44:081529 // Stop prompting for this extension, and check if there's another
1530 // one that needs prompting.
[email protected]612a1cb12012-10-17 13:18:031531 extension_prefs_->AcknowledgeExternalExtension(extension->id());
[email protected]d96eb512012-11-01 23:44:081532 UpdateExternalExtensionAlert();
1533 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
1534 EXTERNAL_EXTENSION_IGNORED,
1535 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:021536 if (extensions::ManifestURL::UpdatesFromGallery(extension)) {
1537 UMA_HISTOGRAM_ENUMERATION(
1538 "Extensions.ExternalExtensionEventWebstore",
1539 EXTERNAL_EXTENSION_IGNORED,
1540 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
1541 } else {
1542 UMA_HISTOGRAM_ENUMERATION(
1543 "Extensions.ExternalExtensionEventNonWebstore",
1544 EXTERNAL_EXTENSION_IGNORED,
1545 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
1546 }
[email protected]d96eb512012-11-01 23:44:081547 return;
[email protected]612a1cb12012-10-17 13:18:031548 }
[email protected]b3aa7182013-04-25 04:45:231549 if (is_first_run_)
1550 extension_prefs_->SetExternalInstallFirstRun(extension->id());
1551 // first_run is true if the extension was installed during a first run
1552 // (even if it's post-first run now).
1553 bool first_run = extension_prefs_->IsExternalInstallFirstRun(
1554 extension->id());
1555 extensions::AddExternalInstallError(this, extension, first_run);
[email protected]612a1cb12012-10-17 13:18:031556 }
1557 } else {
1558 extensions::RemoveExternalInstallError(this);
1559 }
[email protected]612a1cb12012-10-17 13:18:031560}
1561
[email protected]a9f39a312010-12-23 22:14:271562void ExtensionService::UnloadExtension(
1563 const std::string& extension_id,
[email protected]b0af4792013-10-23 09:12:131564 UnloadedExtensionInfo::Reason reason) {
[email protected]27e469a2010-01-11 20:35:091565 // Make sure the extension gets deleted after we return from this function.
[email protected]599539802014-01-07 23:06:001566 int include_mask =
1567 ExtensionRegistry::EVERYTHING & ~ExtensionRegistry::TERMINATED;
[email protected]9adb9692010-10-29 23:14:021568 scoped_refptr<const Extension> extension(
[email protected]599539802014-01-07 23:06:001569 registry_->GetExtensionById(extension_id, include_mask));
[email protected]631cf822009-05-15 07:01:251570
[email protected]fa0c96732010-11-17 00:14:231571 // This method can be called via PostTask, so the extension may have been
1572 // unloaded by the time this runs.
[email protected]dc24976f2013-06-02 21:15:091573 if (!extension.get()) {
[email protected]dd163fb02011-05-04 22:22:171574 // In case the extension may have crashed/uninstalled. Allow the profile to
1575 // clean up its RequestContexts.
[email protected]31d8f5f22012-04-02 15:22:081576 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]fa0c96732010-11-17 00:14:231577 return;
[email protected]dd163fb02011-05-04 22:22:171578 }
[email protected]0c6da502009-08-14 22:32:391579
[email protected]1eb175082010-02-10 09:26:161580 // Keep information about the extension so that we can reload it later
1581 // even if it's not permanently installed.
1582 unloaded_extension_paths_[extension->id()] = extension->path();
1583
[email protected]f17dbd42010-08-16 23:21:101584 // Clean up if the extension is meant to be enabled after a reload.
[email protected]b914e2952013-04-26 07:10:031585 reloading_extensions_.erase(extension->id());
[email protected]f17dbd42010-08-16 23:21:101586
[email protected]bb1bc9b32013-12-21 03:09:141587 if (registry_->disabled_extensions().Contains(extension->id())) {
1588 registry_->RemoveDisabled(extension->id());
[email protected]dd163fb02011-05-04 22:22:171589 // Make sure the profile cleans up its RequestContexts when an already
1590 // disabled extension is unloaded (since they are also tracking the disabled
1591 // extensions).
[email protected]31d8f5f22012-04-02 15:22:081592 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]45f5b7d2014-01-22 23:47:131593 // Don't send the unloaded notification. It was sent when the extension
1594 // was disabled.
[email protected]60a174a2013-08-02 20:29:281595 } else {
[email protected]bb1bc9b32013-12-21 03:09:141596 // Remove the extension from the enabled list.
1597 registry_->RemoveEnabled(extension->id());
[email protected]60a174a2013-08-02 20:29:281598 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]0c6da502009-08-14 22:32:391599 }
1600
[email protected]fb789532013-08-27 02:40:211601 content::NotificationService::current()->Notify(
1602 chrome::NOTIFICATION_EXTENSION_REMOVED,
1603 content::Source<Profile>(profile_),
1604 content::Details<const Extension>(extension.get()));
[email protected]631cf822009-05-15 07:01:251605}
1606
[email protected]8b1ec202013-09-05 02:09:501607void ExtensionService::RemoveComponentExtension(
1608 const std::string& extension_id) {
1609 scoped_refptr<const Extension> extension(
1610 GetExtensionById(extension_id, false));
[email protected]b0af4792013-10-23 09:12:131611 UnloadExtension(extension_id, UnloadedExtensionInfo::REASON_UNINSTALL);
[email protected]8b1ec202013-09-05 02:09:501612 content::NotificationService::current()->Notify(
1613 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
1614 content::Source<Profile>(profile_),
1615 content::Details<const Extension>(extension.get()));
1616}
1617
[email protected]bb1bc9b32013-12-21 03:09:141618void ExtensionService::UnloadAllExtensionsForTest() {
1619 UnloadAllExtensionsInternal();
[email protected]9f1087e2009-06-15 17:29:321620}
1621
[email protected]bb1bc9b32013-12-21 03:09:141622void ExtensionService::ReloadExtensionsForTest() {
1623 // Calling UnloadAllExtensionsForTest here triggers a false-positive presubmit
1624 // warning about calling test code in production.
1625 UnloadAllExtensionsInternal();
[email protected]eac88332012-12-26 17:57:451626 component_loader_->LoadAll();
[email protected]d8c8f25f2011-11-02 18:18:011627 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]4a10006a2013-05-17 23:18:351628 // Don't call SetReadyAndNotifyListeners() since tests call this multiple
1629 // times.
[email protected]9f1087e2009-06-15 17:29:321630}
1631
[email protected]eaa7dd182010-12-14 11:09:001632void ExtensionService::GarbageCollectExtensions() {
[email protected]c04fd3dd2013-09-05 08:20:101633#if defined(OS_CHROMEOS)
1634 if (disable_garbage_collection_)
1635 return;
1636#endif
1637
[email protected]2f8757c32012-06-19 19:17:471638 if (extension_prefs_->pref_service()->ReadOnly())
1639 return;
1640
[email protected]e8107c12014-01-14 15:38:011641 bool clean_temp_dir = true;
1642
[email protected]9bd9a6862012-11-29 09:24:221643 if (pending_extension_manager()->HasPendingExtensions()) {
[email protected]e8107c12014-01-14 15:38:011644 // Don't garbage collect temp dir while there are pending installations,
1645 // which may be using the temporary installation directory. Try to garbage
1646 // collect again later.
1647 clean_temp_dir = false;
[email protected]b3a25092013-05-28 22:08:161648 base::MessageLoop::current()->PostDelayedTask(
[email protected]9bd9a6862012-11-29 09:24:221649 FROM_HERE,
1650 base::Bind(&ExtensionService::GarbageCollectExtensions, AsWeakPtr()),
1651 base::TimeDelta::FromSeconds(kGarbageCollectRetryDelay));
[email protected]9bd9a6862012-11-29 09:24:221652 }
1653
[email protected]45759612012-07-10 17:21:231654 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> info(
[email protected]2f8757c32012-06-19 19:17:471655 extension_prefs_->GetInstalledExtensionsInfo());
1656
[email protected]650b2d52013-02-10 03:41:451657 std::multimap<std::string, base::FilePath> extension_paths;
[email protected]2f8757c32012-06-19 19:17:471658 for (size_t i = 0; i < info->size(); ++i)
[email protected]0db124b02012-11-07 04:55:051659 extension_paths.insert(std::make_pair(info->at(i)->extension_id,
1660 info->at(i)->extension_path));
1661
[email protected]399583b2012-12-11 09:33:421662 info = extension_prefs_->GetAllDelayedInstallInfo();
[email protected]0db124b02012-11-07 04:55:051663 for (size_t i = 0; i < info->size(); ++i)
1664 extension_paths.insert(std::make_pair(info->at(i)->extension_id,
1665 info->at(i)->extension_path));
[email protected]2f8757c32012-06-19 19:17:471666
[email protected]7f8f24f2012-11-15 19:40:141667 if (!GetFileTaskRunner()->PostTask(
1668 FROM_HERE,
[email protected]2f8757c32012-06-19 19:17:471669 base::Bind(
1670 &extension_file_util::GarbageCollectExtensions,
1671 install_directory_,
[email protected]e8107c12014-01-14 15:38:011672 extension_paths,
1673 clean_temp_dir))) {
[email protected]2f8757c32012-06-19 19:17:471674 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:401675 }
[email protected]3cf4f0992009-02-03 23:00:301676}
1677
[email protected]820d9bd2013-04-03 03:46:031678void ExtensionService::SetReadyAndNotifyListeners() {
[email protected]4a10006a2013-05-17 23:18:351679 ready_->Signal();
[email protected]820d9bd2013-04-03 03:46:031680 content::NotificationService::current()->Notify(
1681 chrome::NOTIFICATION_EXTENSIONS_READY,
1682 content::Source<Profile>(profile_),
1683 content::NotificationService::NoDetails());
1684}
1685
[email protected]eaa7dd182010-12-14 11:09:001686void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]3eeddd892013-04-17 17:00:111687 if (updater_)
[email protected]93fd78f42009-07-10 16:43:171688 updater_->Start();
[email protected]fa6a9102010-11-22 15:38:501689
[email protected]bc151cf92013-02-12 04:57:261690 OnBlacklistUpdated();
[email protected]e72e8eb82009-06-18 17:21:511691}
1692
[email protected]fcb58a862012-05-01 01:03:151693void ExtensionService::AddExtension(const Extension* extension) {
[email protected]c8d407e2011-04-28 21:27:171694 // TODO(jstritar): We may be able to get rid of this branch by overriding the
1695 // default extension state to DISABLED when the --disable-extensions flag
1696 // is set (http://crbug.com/29067).
1697 if (!extensions_enabled() &&
1698 !extension->is_theme() &&
[email protected]1d5e58b2013-01-31 08:41:401699 extension->location() != Manifest::COMPONENT &&
1700 !Manifest::IsExternalLocation(extension->location())) {
[email protected]fcb58a862012-05-01 01:03:151701 return;
[email protected]330840c2012-08-29 22:21:011702 }
[email protected]c8d407e2011-04-28 21:27:171703
[email protected]b914e2952013-04-26 07:10:031704 bool is_extension_upgrade = false;
[email protected]116d40e2013-08-08 17:23:171705 bool is_extension_installed = false;
1706 const Extension* old = GetInstalledExtension(extension->id());
1707 if (old) {
1708 is_extension_installed = true;
1709 int version_compare_result =
1710 extension->version()->CompareTo(*(old->version()));
1711 is_extension_upgrade = version_compare_result > 0;
[email protected]b914e2952013-04-26 07:10:031712 // Other than for unpacked extensions, CrxInstaller should have guaranteed
1713 // that we aren't downgrading.
1714 if (!Manifest::IsUnpackedLocation(extension->location()))
[email protected]116d40e2013-08-08 17:23:171715 CHECK_GE(version_compare_result, 0);
[email protected]b914e2952013-04-26 07:10:031716 }
[email protected]45f5b7d2014-01-22 23:47:131717 system_->runtime_data()->SetBeingUpgraded(extension, is_extension_upgrade);
[email protected]c8d407e2011-04-28 21:27:171718
[email protected]1eb175082010-02-10 09:26:161719 // The extension is now loaded, remove its data from unloaded extension map.
1720 unloaded_extension_paths_.erase(extension->id());
1721
[email protected]bb7f40952011-01-13 00:21:201722 // If a terminated extension is loaded, remove it from the terminated list.
1723 UntrackTerminatedExtension(extension->id());
1724
[email protected]f17dbd42010-08-16 23:21:101725 // If the extension was disabled for a reload, then enable it.
[email protected]ca2e82022013-04-29 16:48:411726 bool reloading = reloading_extensions_.erase(extension->id()) > 0;
[email protected]f17dbd42010-08-16 23:21:101727
[email protected]b914e2952013-04-26 07:10:031728 // Check if the extension's privileges have changed and mark the
1729 // extension disabled if necessary.
[email protected]116d40e2013-08-08 17:23:171730 CheckPermissionsIncrease(extension, is_extension_installed);
[email protected]b914e2952013-04-26 07:10:031731
[email protected]116d40e2013-08-08 17:23:171732 if (is_extension_installed && !reloading) {
[email protected]ca2e82022013-04-29 16:48:411733 // To upgrade an extension in place, unload the old one and then load the
1734 // new one. ReloadExtension disables the extension, which is sufficient.
[email protected]b0af4792013-10-23 09:12:131735 UnloadExtension(extension->id(), UnloadedExtensionInfo::REASON_UPDATE);
[email protected]b914e2952013-04-26 07:10:031736 }
[email protected]0c6da502009-08-14 22:32:391737
[email protected]695b5712012-12-06 23:55:281738 if (extension_prefs_->IsExtensionBlacklisted(extension->id())) {
[email protected]ac875372013-02-28 04:36:091739 // Only prefs is checked for the blacklist. We rely on callers to check the
1740 // blacklist before calling into here, e.g. CrxInstaller checks before
[email protected]9f3c8532013-07-31 19:52:071741 // installation then threads through the install and pending install flow
1742 // of this class, and we check when loading installed extensions.
[email protected]bb1bc9b32013-12-21 03:09:141743 registry_->AddBlacklisted(extension);
[email protected]ca2e82022013-04-29 16:48:411744 } else if (!reloading &&
1745 extension_prefs_->IsExtensionDisabled(extension->id())) {
[email protected]bb1bc9b32013-12-21 03:09:141746 registry_->AddDisabled(extension);
[email protected]f8aefb132013-10-30 09:29:521747 if (extension_sync_service_)
1748 extension_sync_service_->SyncExtensionChangeIfNeeded(*extension);
[email protected]44d62b62012-04-11 00:06:031749 content::NotificationService::current()->Notify(
1750 chrome::NOTIFICATION_EXTENSION_UPDATE_DISABLED,
1751 content::Source<Profile>(profile_),
1752 content::Details<const Extension>(extension));
[email protected]62f051c2012-03-29 17:04:441753
[email protected]7e9e2422012-12-13 19:54:191754 // Show the extension disabled error if a permissions increase was the
1755 // only reason it was disabled.
1756 if (extension_prefs_->GetDisableReasons(extension->id()) ==
[email protected]44d62b62012-04-11 00:06:031757 Extension::DISABLE_PERMISSIONS_INCREASE) {
[email protected]62f051c2012-03-29 17:04:441758 extensions::AddExtensionDisabledError(this, extension);
1759 }
[email protected]ca2e82022013-04-29 16:48:411760 } else if (reloading) {
1761 // Replace the old extension with the new version.
[email protected]bb1bc9b32013-12-21 03:09:141762 CHECK(!registry_->AddDisabled(extension));
[email protected]ca2e82022013-04-29 16:48:411763 EnableExtension(extension->id());
[email protected]695b5712012-12-06 23:55:281764 } else {
1765 // All apps that are displayed in the launcher are ordered by their ordinals
1766 // so we must ensure they have valid ordinals.
1767 if (extension->RequiresSortOrdinal()) {
[email protected]bd36b652013-02-27 02:13:251768 if (!extension->ShouldDisplayInNewTabPage()) {
[email protected]74474042013-11-21 12:03:541769 extension_prefs_->app_sorting()->MarkExtensionAsHidden(extension->id());
[email protected]bd36b652013-02-27 02:13:251770 }
[email protected]74474042013-11-21 12:03:541771 extension_prefs_->app_sorting()->EnsureValidOrdinals(
[email protected]695b5712012-12-06 23:55:281772 extension->id(), syncer::StringOrdinal());
1773 }
1774
[email protected]bb1bc9b32013-12-21 03:09:141775 registry_->AddEnabled(extension);
[email protected]f8aefb132013-10-30 09:29:521776 if (extension_sync_service_)
1777 extension_sync_service_->SyncExtensionChangeIfNeeded(*extension);
[email protected]695b5712012-12-06 23:55:281778 NotifyExtensionLoaded(extension);
[email protected]f574c402012-12-04 23:20:311779 }
[email protected]45f5b7d2014-01-22 23:47:131780 system_->runtime_data()->SetBeingUpgraded(extension, false);
[email protected]aab98a52009-12-02 03:22:351781}
1782
[email protected]8c484b742012-11-29 06:05:361783void ExtensionService::AddComponentExtension(const Extension* extension) {
1784 const std::string old_version_string(
1785 extension_prefs_->GetVersionString(extension->id()));
[email protected]c5e4a2222014-01-03 16:06:131786 const Version old_version(old_version_string);
[email protected]8c484b742012-11-29 06:05:361787
[email protected]0181a7d2013-11-12 01:08:421788 VLOG(1) << "AddComponentExtension " << extension->name();
[email protected]8c484b742012-11-29 06:05:361789 if (!old_version.IsValid() || !old_version.Equals(*extension->version())) {
1790 VLOG(1) << "Component extension " << extension->name() << " ("
1791 << extension->id() << ") installing/upgrading from '"
1792 << old_version_string << "' to " << extension->version()->GetString();
1793
1794 AddNewOrUpdatedExtension(extension,
[email protected]399583b2012-12-11 09:33:421795 Extension::ENABLED_COMPONENT,
[email protected]8e289f0b2013-12-17 17:49:071796 extensions::NOT_BLACKLISTED,
[email protected]399583b2012-12-11 09:33:421797 syncer::StringOrdinal());
[email protected]8c484b742012-11-29 06:05:361798 return;
1799 }
1800
1801 AddExtension(extension);
1802}
1803
[email protected]b914e2952013-04-26 07:10:031804void ExtensionService::UpdateActivePermissions(const Extension* extension) {
[email protected]902fd7b2011-07-27 18:42:311805 // If the extension has used the optional permissions API, it will have a
1806 // custom set of active permissions defined in the extension prefs. Here,
1807 // we update the extension's active permissions based on the prefs.
[email protected]c2e66e12012-06-27 06:27:061808 scoped_refptr<PermissionSet> active_permissions =
[email protected]902fd7b2011-07-27 18:42:311809 extension_prefs()->GetActivePermissions(extension->id());
1810
[email protected]dc24976f2013-06-02 21:15:091811 if (active_permissions.get()) {
[email protected]902fd7b2011-07-27 18:42:311812 // We restrict the active permissions to be within the bounds defined in the
1813 // extension's manifest.
1814 // a) active permissions must be a subset of optional + default permissions
1815 // b) active permissions must contains all default permissions
[email protected]c2e66e12012-06-27 06:27:061816 scoped_refptr<PermissionSet> total_permissions =
1817 PermissionSet::CreateUnion(
[email protected]2cb5e302013-05-09 05:00:061818 extensions::PermissionsData::GetRequiredPermissions(extension),
1819 extensions::PermissionsData::GetOptionalPermissions(extension));
[email protected]902fd7b2011-07-27 18:42:311820
1821 // Make sure the active permissions contain no more than optional + default.
[email protected]c2e66e12012-06-27 06:27:061822 scoped_refptr<PermissionSet> adjusted_active =
1823 PermissionSet::CreateIntersection(
[email protected]902fd7b2011-07-27 18:42:311824 total_permissions.get(), active_permissions.get());
1825
1826 // Make sure the active permissions contain the default permissions.
[email protected]c2e66e12012-06-27 06:27:061827 adjusted_active = PermissionSet::CreateUnion(
[email protected]2cb5e302013-05-09 05:00:061828 extensions::PermissionsData::GetRequiredPermissions(extension),
1829 adjusted_active.get());
[email protected]902fd7b2011-07-27 18:42:311830
[email protected]c333e792012-01-06 16:57:391831 extensions::PermissionsUpdater perms_updater(profile());
[email protected]dc24976f2013-06-02 21:15:091832 perms_updater.UpdateActivePermissions(extension, adjusted_active.get());
[email protected]902fd7b2011-07-27 18:42:311833 }
[email protected]b914e2952013-04-26 07:10:031834}
1835
1836void ExtensionService::CheckPermissionsIncrease(const Extension* extension,
[email protected]116d40e2013-08-08 17:23:171837 bool is_extension_installed) {
[email protected]b914e2952013-04-26 07:10:031838 UpdateActivePermissions(extension);
[email protected]902fd7b2011-07-27 18:42:311839
[email protected]8d888c12010-11-30 00:00:251840 // We keep track of all permissions the user has granted each extension.
1841 // This allows extensions to gracefully support backwards compatibility
1842 // by including unknown permissions in their manifests. When the user
1843 // installs the extension, only the recognized permissions are recorded.
1844 // When the unknown permissions become recognized (e.g., through browser
1845 // upgrade), we can prompt the user to accept these new permissions.
1846 // Extensions can also silently upgrade to less permissions, and then
1847 // silently upgrade to a version that adds these permissions back.
1848 //
1849 // For example, pretend that Chrome 10 includes a permission "omnibox"
1850 // for an API that adds suggestions to the omnibox. An extension can
1851 // maintain backwards compatibility while still having "omnibox" in the
1852 // manifest. If a user installs the extension on Chrome 9, the browser
1853 // will record the permissions it recognized, not including "omnibox."
1854 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
1855 // will disable the extension and prompt the user to approve the increase
1856 // in privileges. The extension could then release a new version that
1857 // removes the "omnibox" permission. When the user upgrades, Chrome will
1858 // still remember that "omnibox" had been granted, so that if the
1859 // extension once again includes "omnibox" in an upgrade, the extension
1860 // can upgrade without requiring this user's approval.
[email protected]eb5e4f92012-08-15 23:33:281861 int disable_reasons = extension_prefs_->GetDisableReasons(extension->id());
[email protected]8d888c12010-11-30 00:00:251862
[email protected]211a3f32013-05-28 21:48:181863 bool auto_grant_permission =
[email protected]1b563df2013-10-30 16:49:361864 (!is_extension_installed && extension->was_installed_by_default()) ||
[email protected]45928b682013-11-28 08:20:271865 extensions::ExtensionsBrowserClient::Get()->IsRunningInForcedAppMode();
[email protected]b914e2952013-04-26 07:10:031866 // Silently grant all active permissions to default apps only on install.
1867 // After install they should behave like other apps.
[email protected]211a3f32013-05-28 21:48:181868 // Silently grant all active permissions to apps install in kiosk mode on both
1869 // install and update.
1870 if (auto_grant_permission)
[email protected]b914e2952013-04-26 07:10:031871 GrantPermissions(extension);
1872
1873 bool is_privilege_increase = false;
1874 // We only need to compare the granted permissions to the current permissions
1875 // if the extension is not allowed to silently increase its permissions.
[email protected]13c68b62013-05-17 11:29:051876 if (!extensions::PermissionsData::CanSilentlyIncreasePermissions(extension) &&
[email protected]211a3f32013-05-28 21:48:181877 !auto_grant_permission) {
[email protected]8d888c12010-11-30 00:00:251878 // Add all the recognized permissions if the granted permissions list
1879 // hasn't been initialized yet.
[email protected]c2e66e12012-06-27 06:27:061880 scoped_refptr<PermissionSet> granted_permissions =
[email protected]902fd7b2011-07-27 18:42:311881 extension_prefs_->GetGrantedPermissions(extension->id());
[email protected]0d3e4a22011-06-23 19:02:521882 CHECK(granted_permissions.get());
[email protected]8d888c12010-11-30 00:00:251883
1884 // Here, we check if an extension's privileges have increased in a manner
1885 // that requires the user's approval. This could occur because the browser
1886 // upgraded and recognized additional privileges, or an extension upgrades
1887 // to a version that requires additional privileges.
[email protected]c41003472013-10-19 15:37:251888 is_privilege_increase =
1889 extensions::PermissionMessageProvider::Get()->IsPrivilegeIncrease(
1890 granted_permissions,
1891 extension->GetActivePermissions().get(),
1892 extension->GetType());
[email protected]8d888c12010-11-30 00:00:251893 }
1894
[email protected]116d40e2013-08-08 17:23:171895 if (is_extension_installed) {
[email protected]44d62b62012-04-11 00:06:031896 // If the extension was already disabled, suppress any alerts for becoming
1897 // disabled on permissions increase.
[email protected]b914e2952013-04-26 07:10:031898 bool previously_disabled =
1899 extension_prefs_->IsExtensionDisabled(extension->id());
[email protected]7e9e2422012-12-13 19:54:191900 // Legacy disabled extensions do not have a disable reason. Infer that if
1901 // there was no permission increase, it was likely disabled by the user.
1902 if (previously_disabled && disable_reasons == Extension::DISABLE_NONE &&
[email protected]b914e2952013-04-26 07:10:031903 !extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
[email protected]7e9e2422012-12-13 19:54:191904 disable_reasons |= Extension::DISABLE_USER_ACTION;
[email protected]44d62b62012-04-11 00:06:031905 }
[email protected]7e9e2422012-12-13 19:54:191906 // Extensions that came to us disabled from sync need a similar inference,
1907 // except based on the new version's permissions.
1908 if (previously_disabled &&
1909 disable_reasons == Extension::DISABLE_UNKNOWN_FROM_SYNC) {
1910 // Remove the DISABLE_UNKNOWN_FROM_SYNC reason.
1911 extension_prefs_->ClearDisableReasons(extension->id());
1912 if (!is_privilege_increase)
1913 disable_reasons |= Extension::DISABLE_USER_ACTION;
1914 }
[email protected]b914e2952013-04-26 07:10:031915 disable_reasons &= ~Extension::DISABLE_UNKNOWN_FROM_SYNC;
[email protected]8d888c12010-11-30 00:00:251916 }
1917
1918 // Extension has changed permissions significantly. Disable it. A
1919 // notification should be sent by the caller.
1920 if (is_privilege_increase) {
[email protected]7e9e2422012-12-13 19:54:191921 disable_reasons |= Extension::DISABLE_PERMISSIONS_INCREASE;
[email protected]fe2dd7742011-04-19 22:52:491922 if (!extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
1923 RecordPermissionMessagesHistogram(
1924 extension, "Extensions.Permissions_AutoDisable");
1925 }
[email protected]195c1582013-10-29 18:42:231926 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
[email protected]8d888c12010-11-30 00:00:251927 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
[email protected]b914e2952013-04-26 07:10:031928 }
1929 if (disable_reasons != Extension::DISABLE_NONE) {
[email protected]eb5e4f92012-08-15 23:33:281930 extension_prefs_->AddDisableReason(
1931 extension->id(),
1932 static_cast<Extension::DisableReason>(disable_reasons));
[email protected]8d888c12010-11-30 00:00:251933 }
1934}
1935
[email protected]eaa7dd182010-12-14 11:09:001936void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:391937 std::set<std::string> extension_ids;
[email protected]bb1bc9b32013-12-21 03:09:141938 const ExtensionSet& extensions = registry_->enabled_extensions();
1939 for (ExtensionSet::const_iterator iter = extensions.begin();
1940 iter != extensions.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361941 const Extension* extension = iter->get();
[email protected]1d5e58b2013-01-31 08:41:401942 if (!extension->is_theme() && extension->location() != Manifest::COMPONENT)
[email protected]84df8332011-12-06 18:22:461943 extension_ids.insert(extension->id());
[email protected]aab98a52009-12-02 03:22:351944 }
1945
[email protected]f6431be82013-09-07 02:53:451946 crash_keys::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:251947}
1948
[email protected]669b2372013-10-17 15:04:581949ExtensionService::ImportStatus ExtensionService::CheckImports(
1950 const extensions::Extension* extension,
1951 std::list<SharedModuleInfo::ImportInfo>* missing_modules,
1952 std::list<SharedModuleInfo::ImportInfo>* outdated_modules) {
1953 DCHECK(extension);
1954 DCHECK(missing_modules && missing_modules->empty());
1955 DCHECK(outdated_modules && outdated_modules->empty());
[email protected]9f4e4f082013-06-21 07:11:191956 ImportStatus status = IMPORT_STATUS_OK;
[email protected]9f4e4f082013-06-21 07:11:191957 if (SharedModuleInfo::ImportsModules(extension)) {
1958 const std::vector<SharedModuleInfo::ImportInfo>& imports =
1959 SharedModuleInfo::GetImports(extension);
1960 std::vector<SharedModuleInfo::ImportInfo>::const_iterator i;
1961 for (i = imports.begin(); i != imports.end(); ++i) {
[email protected]c5e4a2222014-01-03 16:06:131962 Version version_required(i->minimum_version);
[email protected]9f4e4f082013-06-21 07:11:191963 const Extension* imported_module =
1964 GetExtensionById(i->extension_id, true);
1965 if (!imported_module) {
1966 if (extension->from_webstore()) {
1967 status = IMPORT_STATUS_UNSATISFIED;
[email protected]669b2372013-10-17 15:04:581968 missing_modules->push_back(*i);
[email protected]9f4e4f082013-06-21 07:11:191969 } else {
1970 return IMPORT_STATUS_UNRECOVERABLE;
1971 }
1972 } else if (!SharedModuleInfo::IsSharedModule(imported_module)) {
1973 return IMPORT_STATUS_UNRECOVERABLE;
1974 } else if (version_required.IsValid() &&
1975 imported_module->version()->CompareTo(version_required) < 0) {
1976 if (imported_module->from_webstore()) {
[email protected]669b2372013-10-17 15:04:581977 outdated_modules->push_back(*i);
[email protected]9f4e4f082013-06-21 07:11:191978 status = IMPORT_STATUS_UNSATISFIED;
1979 } else {
1980 return IMPORT_STATUS_UNRECOVERABLE;
1981 }
1982 }
1983 }
1984 }
[email protected]669b2372013-10-17 15:04:581985 return status;
1986}
1987
1988ExtensionService::ImportStatus ExtensionService::SatisfyImports(
1989 const Extension* extension) {
1990 std::list<SharedModuleInfo::ImportInfo> noinstalled;
1991 std::list<SharedModuleInfo::ImportInfo> outdated;
1992 ImportStatus status = CheckImports(extension, &noinstalled, &outdated);
1993 if (status == IMPORT_STATUS_UNRECOVERABLE)
1994 return status;
[email protected]9f4e4f082013-06-21 07:11:191995 if (status == IMPORT_STATUS_UNSATISFIED) {
[email protected]669b2372013-10-17 15:04:581996 std::list<SharedModuleInfo::ImportInfo>::const_iterator iter;
1997 for (iter = noinstalled.begin(); iter != noinstalled.end(); ++iter) {
[email protected]9f4e4f082013-06-21 07:11:191998 pending_extension_manager()->AddFromExtensionImport(
[email protected]669b2372013-10-17 15:04:581999 iter->extension_id,
[email protected]9f4e4f082013-06-21 07:11:192000 extension_urls::GetWebstoreUpdateUrl(),
2001 IsSharedModule);
2002 }
2003 CheckForUpdatesSoon();
2004 }
2005 return status;
2006}
2007
2008scoped_ptr<const ExtensionSet>
2009 ExtensionService::GetDependentExtensions(const Extension* extension) {
2010 scoped_ptr<ExtensionSet> dependents(new ExtensionSet());
2011 scoped_ptr<ExtensionSet> set_to_check(new ExtensionSet());
2012 if (SharedModuleInfo::IsSharedModule(extension)) {
[email protected]bb1bc9b32013-12-21 03:09:142013 set_to_check->InsertAll(registry_->disabled_extensions());
[email protected]9f4e4f082013-06-21 07:11:192014 set_to_check->InsertAll(delayed_installs_);
[email protected]bb1bc9b32013-12-21 03:09:142015 set_to_check->InsertAll(registry_->enabled_extensions());
[email protected]9f4e4f082013-06-21 07:11:192016 for (ExtensionSet::const_iterator iter = set_to_check->begin();
2017 iter != set_to_check->end(); ++iter) {
[email protected]198b5902013-06-27 10:36:112018 if (SharedModuleInfo::ImportsExtensionById(iter->get(),
2019 extension->id())) {
[email protected]9f4e4f082013-06-21 07:11:192020 dependents->Insert(*iter);
2021 }
2022 }
2023 }
2024 return dependents.PassAs<const ExtensionSet>();
2025}
2026
2027void ExtensionService::PruneSharedModulesOnUninstall(
2028 const Extension* extension) {
2029 if (SharedModuleInfo::ImportsModules(extension)) {
2030 const std::vector<SharedModuleInfo::ImportInfo>& imports =
2031 SharedModuleInfo::GetImports(extension);
2032 std::vector<SharedModuleInfo::ImportInfo>::const_iterator i;
2033 for (i = imports.begin(); i != imports.end(); ++i) {
2034 const Extension* imported_module =
2035 GetExtensionById(i->extension_id, true);
2036 if (imported_module && imported_module->from_webstore()) {
2037 scoped_ptr<const ExtensionSet> dependents =
2038 GetDependentExtensions(imported_module);
2039 if (dependents->size() == 0) {
[email protected]4c9201c42013-08-16 04:56:212040 UninstallExtension(i->extension_id, true, NULL);
[email protected]9f4e4f082013-06-21 07:11:192041 }
2042 }
2043 }
2044 }
2045}
2046
[email protected]8266d662011-07-12 21:53:262047void ExtensionService::OnExtensionInstalled(
[email protected]36a5c4c2011-12-14 16:34:502048 const Extension* extension,
[email protected]98270432012-09-11 20:51:242049 const syncer::StringOrdinal& page_ordinal,
[email protected]0db124b02012-11-07 04:55:052050 bool has_requirement_errors,
[email protected]8e289f0b2013-12-17 17:49:072051 extensions::BlacklistState blacklist_state,
[email protected]0db124b02012-11-07 04:55:052052 bool wait_for_idle) {
[email protected]a29a517a2011-01-21 21:11:122053 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:202054
[email protected]c3cfb012011-04-06 22:07:352055 const std::string& id = extension->id();
[email protected]612a1cb12012-10-17 13:18:032056 bool initial_enable = ShouldEnableOnInstall(extension);
[email protected]3f213ad2012-07-26 23:39:412057 const extensions::PendingExtensionInfo* pending_extension_info = NULL;
[email protected]51a3bf8b2012-06-08 22:53:062058 if ((pending_extension_info = pending_extension_manager()->GetById(id))) {
[email protected]8f3bcbd2013-06-05 08:42:402059 if (!pending_extension_info->ShouldAllowInstall(extension)) {
[email protected]51a3bf8b2012-06-08 22:53:062060 pending_extension_manager()->Remove(id);
[email protected]8ef78fd2010-08-19 17:14:322061
[email protected]31bb5ee62012-09-12 22:58:402062 LOG(WARNING) << "ShouldAllowInstall() returned false for "
2063 << id << " of type " << extension->GetType()
[email protected]65348062013-01-15 07:27:222064 << " and update URL "
2065 << extensions::ManifestURL::GetUpdateURL(extension).spec()
[email protected]31bb5ee62012-09-12 22:58:402066 << "; not installing";
[email protected]ae6cb3022011-04-04 20:11:542067
[email protected]4416c5a2010-06-26 01:28:572068 // Delete the extension directory since we're not going to
2069 // load it.
[email protected]7f8f24f2012-11-15 19:40:142070 if (!GetFileTaskRunner()->PostTask(
2071 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362072 base::Bind(&extension_file_util::DeleteFile,
[email protected]31bb5ee62012-09-12 22:58:402073 extension->path(), true))) {
[email protected]14908b72011-04-20 06:54:362074 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:402075 }
[email protected]4416c5a2010-06-26 01:28:572076 return;
2077 }
[email protected]51a3bf8b2012-06-08 22:53:062078
2079 pending_extension_manager()->Remove(id);
[email protected]4416c5a2010-06-26 01:28:572080 } else {
[email protected]c3cfb012011-04-06 22:07:352081 // We explicitly want to re-enable an uninstalled external
2082 // extension; if we're here, that means the user is manually
2083 // installing the extension.
[email protected]6cc7dbae2011-04-29 21:18:332084 if (IsExternalExtensionUninstalled(id)) {
2085 initial_enable = true;
2086 }
[email protected]aa142702010-03-26 01:26:332087 }
2088
[email protected]98270432012-09-11 20:51:242089 // Unsupported requirements overrides the management policy.
2090 if (has_requirement_errors) {
2091 initial_enable = false;
2092 extension_prefs_->AddDisableReason(
2093 id, Extension::DISABLE_UNSUPPORTED_REQUIREMENT);
2094 // If the extension was disabled because of unsupported requirements but
2095 // now supports all requirements after an update and there are not other
2096 // disable reasons, enable it.
2097 } else if (extension_prefs_->GetDisableReasons(id) ==
2098 Extension::DISABLE_UNSUPPORTED_REQUIREMENT) {
2099 initial_enable = true;
2100 extension_prefs_->ClearDisableReasons(id);
2101 }
2102
[email protected]8e289f0b2013-12-17 17:49:072103 if (blacklist_state == extensions::BLACKLISTED_MALWARE) {
[email protected]9f3c8532013-07-31 19:52:072104 // Installation of a blacklisted extension can happen from sync, policy,
2105 // etc, where to maintain consistency we need to install it, just never
2106 // load it (see AddExtension). Usually it should be the job of callers to
2107 // incercept blacklisted extension earlier (e.g. CrxInstaller, before even
2108 // showing the install dialogue).
2109 extension_prefs()->AcknowledgeBlacklistedExtension(id);
2110 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.SilentInstall",
2111 extension->location(),
2112 Manifest::NUM_LOCATIONS);
2113 }
2114
[email protected]695b5712012-12-06 23:55:282115 if (!GetInstalledExtension(extension->id())) {
[email protected]07533022011-06-27 20:42:552116 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
2117 extension->GetType(), 100);
[email protected]69084dc2012-11-17 07:39:302118 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallSource",
[email protected]1d5e58b2013-01-31 08:41:402119 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]07533022011-06-27 20:42:552120 RecordPermissionMessagesHistogram(
2121 extension, "Extensions.Permissions_Install");
[email protected]69084dc2012-11-17 07:39:302122 } else {
2123 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateType",
2124 extension->GetType(), 100);
2125 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateSource",
[email protected]1d5e58b2013-01-31 08:41:402126 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]07533022011-06-27 20:42:552127 }
2128
[email protected]a39921b42012-02-28 03:42:542129 // Certain extension locations are specific enough that we can
2130 // auto-acknowledge any extension that came from one of them.
[email protected]9bb691632013-09-26 18:50:122131 if (Manifest::IsPolicyLocation(extension->location()))
[email protected]a39921b42012-02-28 03:42:542132 AcknowledgeExternalExtension(extension->id());
[email protected]399583b2012-12-11 09:33:422133 const Extension::State initial_state =
2134 initial_enable ? Extension::ENABLED : Extension::DISABLED;
[email protected]0f9e6262013-11-05 23:49:192135 const bool blacklisted_for_malware =
[email protected]8e289f0b2013-12-17 17:49:072136 blacklist_state == extensions::BLACKLISTED_MALWARE;
[email protected]e7aa7b7e2012-11-27 04:51:222137 if (ShouldDelayExtensionUpdate(id, wait_for_idle)) {
[email protected]9f3c8532013-07-31 19:52:072138 extension_prefs_->SetDelayedInstallInfo(
2139 extension,
2140 initial_state,
[email protected]0f9e6262013-11-05 23:49:192141 blacklisted_for_malware,
[email protected]9f3c8532013-07-31 19:52:072142 extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE,
2143 page_ordinal);
[email protected]0db124b02012-11-07 04:55:052144
2145 // Transfer ownership of |extension|.
[email protected]9f4e4f082013-06-21 07:11:192146 delayed_installs_.Insert(extension);
[email protected]0db124b02012-11-07 04:55:052147
[email protected]75bdcb872013-03-13 00:41:452148 // Notify observers that app update is available.
2149 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
[email protected]a9aa62b312013-11-29 05:35:062150 OnAppUpdateAvailable(extension));
[email protected]0db124b02012-11-07 04:55:052151 return;
2152 }
2153
[email protected]9f4e4f082013-06-21 07:11:192154 ImportStatus status = SatisfyImports(extension);
2155 if (installs_delayed_for_gc()) {
[email protected]9f3c8532013-07-31 19:52:072156 extension_prefs_->SetDelayedInstallInfo(
2157 extension,
2158 initial_state,
[email protected]0f9e6262013-11-05 23:49:192159 blacklisted_for_malware,
[email protected]9f3c8532013-07-31 19:52:072160 extensions::ExtensionPrefs::DELAY_REASON_GC,
2161 page_ordinal);
[email protected]399583b2012-12-11 09:33:422162 delayed_installs_.Insert(extension);
[email protected]9f4e4f082013-06-21 07:11:192163 } else if (status != IMPORT_STATUS_OK) {
2164 if (status == IMPORT_STATUS_UNSATISFIED) {
[email protected]9f3c8532013-07-31 19:52:072165 extension_prefs_->SetDelayedInstallInfo(
2166 extension,
2167 initial_state,
[email protected]0f9e6262013-11-05 23:49:192168 blacklisted_for_malware,
[email protected]9f4e4f082013-06-21 07:11:192169 extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IMPORTS,
2170 page_ordinal);
2171 delayed_installs_.Insert(extension);
2172 }
[email protected]399583b2012-12-11 09:33:422173 } else {
[email protected]9f3c8532013-07-31 19:52:072174 AddNewOrUpdatedExtension(extension,
2175 initial_state,
2176 blacklist_state,
2177 page_ordinal);
[email protected]399583b2012-12-11 09:33:422178 }
[email protected]8c484b742012-11-29 06:05:362179}
2180
2181void ExtensionService::AddNewOrUpdatedExtension(
2182 const Extension* extension,
[email protected]399583b2012-12-11 09:33:422183 Extension::State initial_state,
[email protected]8e289f0b2013-12-17 17:49:072184 extensions::BlacklistState blacklist_state,
[email protected]399583b2012-12-11 09:33:422185 const syncer::StringOrdinal& page_ordinal) {
[email protected]8c484b742012-11-29 06:05:362186 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]0f9e6262013-11-05 23:49:192187 const bool blacklisted_for_malware =
[email protected]8e289f0b2013-12-17 17:49:072188 blacklist_state == extensions::BLACKLISTED_MALWARE;
[email protected]9f3c8532013-07-31 19:52:072189 extension_prefs_->OnExtensionInstalled(extension,
2190 initial_state,
[email protected]0f9e6262013-11-05 23:49:192191 blacklisted_for_malware,
[email protected]9f3c8532013-07-31 19:52:072192 page_ordinal);
[email protected]9e9c1d12013-07-31 01:58:122193 delayed_installs_.Remove(extension->id());
[email protected]88622932014-01-16 17:59:122194 if (InstallVerifier::NeedsVerification(*extension)) {
[email protected]ffd2f79e2013-11-14 00:11:462195 extensions::ExtensionSystem::Get(profile_)->install_verifier()->Add(
2196 extension->id(), InstallVerifier::AddResultCallback());
2197 }
[email protected]6f6101832012-11-27 22:10:482198 FinishInstallation(extension);
2199}
2200
2201void ExtensionService::MaybeFinishDelayedInstallation(
2202 const std::string& extension_id) {
[email protected]9f4e4f082013-06-21 07:11:192203 // Check if the extension already got installed.
2204 if (!delayed_installs_.Contains(extension_id))
[email protected]6f6101832012-11-27 22:10:482205 return;
[email protected]9f4e4f082013-06-21 07:11:192206 extensions::ExtensionPrefs::DelayReason reason =
2207 extension_prefs_->GetDelayedInstallReason(extension_id);
2208
2209 // Check if the extension is idle. DELAY_REASON_NONE is used for older
2210 // preferences files that will not have set this field but it was previously
2211 // only used for idle updates.
2212 if ((reason == extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE ||
2213 reason == extensions::ExtensionPrefs::DELAY_REASON_NONE) &&
[email protected]617342a42013-12-18 23:34:032214 is_ready() && !extension_util::IsExtensionIdle(extension_id, system_))
[email protected]6f6101832012-11-27 22:10:482215 return;
2216
[email protected]9f4e4f082013-06-21 07:11:192217 const Extension* extension = delayed_installs_.GetByID(extension_id);
2218 if (reason == extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IMPORTS) {
2219 ImportStatus status = SatisfyImports(extension);
2220 if (status != IMPORT_STATUS_OK) {
2221 if (status == IMPORT_STATUS_UNRECOVERABLE) {
2222 delayed_installs_.Remove(extension_id);
2223 // Make sure no version of the extension is actually installed, (i.e.,
2224 // that this delayed install was not an update).
2225 CHECK(!extension_prefs_->GetInstalledExtensionInfo(extension_id).get());
2226 extension_prefs_->DeleteExtensionPrefs(extension_id);
2227 }
2228 return;
2229 }
2230 }
2231
[email protected]6f6101832012-11-27 22:10:482232 FinishDelayedInstallation(extension_id);
2233}
2234
2235void ExtensionService::FinishDelayedInstallation(
2236 const std::string& extension_id) {
2237 scoped_refptr<const Extension> extension(
2238 GetPendingExtensionUpdate(extension_id));
[email protected]dc24976f2013-06-02 21:15:092239 CHECK(extension.get());
[email protected]9f4e4f082013-06-21 07:11:192240 delayed_installs_.Remove(extension_id);
[email protected]6f6101832012-11-27 22:10:482241
[email protected]399583b2012-12-11 09:33:422242 if (!extension_prefs_->FinishDelayedInstallInfo(extension_id))
[email protected]6f6101832012-11-27 22:10:482243 NOTREACHED();
2244
[email protected]dc24976f2013-06-02 21:15:092245 FinishInstallation(extension.get());
[email protected]6f6101832012-11-27 22:10:482246}
2247
2248void ExtensionService::FinishInstallation(const Extension* extension) {
[email protected]e7554c3f2013-05-29 00:36:562249 const extensions::Extension* existing_extension =
2250 GetInstalledExtension(extension->id());
2251 bool is_update = false;
2252 std::string old_name;
2253 if (existing_extension) {
2254 is_update = true;
2255 old_name = existing_extension->name();
2256 }
2257 extensions::InstalledExtensionInfo details(extension, is_update, old_name);
[email protected]fcb58a862012-05-01 01:03:152258 content::NotificationService::current()->Notify(
2259 chrome::NOTIFICATION_EXTENSION_INSTALLED,
2260 content::Source<Profile>(profile_),
[email protected]41bb80bd2013-05-03 10:56:022261 content::Details<const extensions::InstalledExtensionInfo>(&details));
[email protected]fcb58a862012-05-01 01:03:152262
[email protected]d96eb512012-11-01 23:44:082263 bool unacknowledged_external = IsUnacknowledgedExternalExtension(extension);
[email protected]41070e8d2012-10-24 01:34:362264
[email protected]399583b2012-12-11 09:33:422265 // Unpacked extensions default to allowing file access, but if that has been
2266 // overridden, don't reset the value.
[email protected]1d5e58b2013-01-31 08:41:402267 if (Manifest::ShouldAlwaysAllowFileAccess(extension->location()) &&
[email protected]399583b2012-12-11 09:33:422268 !extension_prefs_->HasAllowFileAccessSetting(extension->id())) {
2269 extension_prefs_->SetAllowFileAccess(extension->id(), true);
2270 }
2271
[email protected]6f6101832012-11-27 22:10:482272 AddExtension(extension);
[email protected]612a1cb12012-10-17 13:18:032273
2274 // If this is a new external extension that was disabled, alert the user
[email protected]d96eb512012-11-01 23:44:082275 // so he can reenable it. We do this last so that it has already been
2276 // added to our list of extensions.
[email protected]3e3fafbb2013-08-17 13:33:162277 if (unacknowledged_external && !is_update) {
[email protected]612a1cb12012-10-17 13:18:032278 UpdateExternalExtensionAlert();
[email protected]d96eb512012-11-01 23:44:082279 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
2280 EXTERNAL_EXTENSION_INSTALLED,
2281 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:022282 if (extensions::ManifestURL::UpdatesFromGallery(extension)) {
2283 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventWebstore",
2284 EXTERNAL_EXTENSION_INSTALLED,
2285 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
2286 } else {
2287 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventNonWebstore",
2288 EXTERNAL_EXTENSION_INSTALLED,
2289 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
2290 }
[email protected]d96eb512012-11-01 23:44:082291 }
[email protected]9f4e4f082013-06-21 07:11:192292
2293 // Check extensions that may have been delayed only because this shared module
2294 // was not available.
2295 if (SharedModuleInfo::IsSharedModule(extension)) {
2296 MaybeFinishDelayedInstallations();
2297 }
[email protected]4a190632009-05-09 01:07:422298}
2299
[email protected]76b65442012-11-17 14:11:482300const Extension* ExtensionService::GetPendingExtensionUpdate(
2301 const std::string& id) const {
[email protected]9f4e4f082013-06-21 07:11:192302 return delayed_installs_.GetByID(id);
[email protected]76b65442012-11-17 14:11:482303}
2304
[email protected]bb7f40952011-01-13 00:21:202305void ExtensionService::TrackTerminatedExtension(const Extension* extension) {
[email protected]bb1bc9b32013-12-21 03:09:142306 // No need to check for duplicates; inserting a duplicate is a no-op.
2307 registry_->AddTerminated(make_scoped_refptr(extension));
[email protected]b0af4792013-10-23 09:12:132308 UnloadExtension(extension->id(), UnloadedExtensionInfo::REASON_TERMINATE);
[email protected]bb7f40952011-01-13 00:21:202309}
2310
2311void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
[email protected]fa2416f2011-05-03 08:41:202312 std::string lowercase_id = StringToLowerASCII(id);
[email protected]bb1bc9b32013-12-21 03:09:142313 const Extension* extension =
2314 registry_->terminated_extensions().GetByID(lowercase_id);
2315 registry_->RemoveTerminated(lowercase_id);
[email protected]fb789532013-08-27 02:40:212316 if (extension) {
2317 content::NotificationService::current()->Notify(
2318 chrome::NOTIFICATION_EXTENSION_REMOVED,
2319 content::Source<Profile>(profile_),
2320 content::Details<const Extension>(extension));
2321 }
[email protected]bb7f40952011-01-13 00:21:202322}
2323
[email protected]0dfe05c2011-02-23 23:03:362324const Extension* ExtensionService::GetTerminatedExtension(
[email protected]8001df22011-04-28 19:59:472325 const std::string& id) const {
[email protected]599539802014-01-07 23:06:002326 return registry_->GetExtensionById(id, ExtensionRegistry::TERMINATED);
[email protected]8001df22011-04-28 19:59:472327}
2328
2329const Extension* ExtensionService::GetInstalledExtension(
2330 const std::string& id) const {
[email protected]599539802014-01-07 23:06:002331 return registry_->GetExtensionById(id, ExtensionRegistry::EVERYTHING);
[email protected]0dfe05c2011-02-23 23:03:362332}
2333
[email protected]eaa7dd182010-12-14 11:09:002334bool ExtensionService::ExtensionBindingsAllowed(const GURL& url) {
[email protected]615d88f2011-12-13 01:47:442335 // Allow bindings for all packaged extensions and component hosted apps.
[email protected]bb1bc9b32013-12-21 03:09:142336 const Extension* extension =
2337 registry_->enabled_extensions().GetExtensionOrAppByURL(url);
[email protected]615d88f2011-12-13 01:47:442338 return extension && (!extension->is_hosted_app() ||
[email protected]1d5e58b2013-01-31 08:41:402339 extension->location() == Manifest::COMPONENT);
[email protected]6d2e60bd2010-06-03 22:37:392340}
2341
[email protected]4d007b312012-10-17 03:00:482342bool ExtensionService::ShouldBlockUrlInBrowserTab(GURL* url) {
[email protected]bb1bc9b32013-12-21 03:09:142343 const Extension* extension =
2344 registry_->enabled_extensions().GetExtensionOrAppByURL(*url);
[email protected]4d007b312012-10-17 03:00:482345 if (extension && extension->is_platform_app()) {
2346 *url = GURL(chrome::kExtensionInvalidRequestURL);
2347 return true;
2348 }
2349
2350 return false;
2351}
2352
[email protected]9060d8b02012-01-13 02:14:302353bool ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:322354 const std::string& id,
[email protected]c5e4a2222014-01-03 16:06:132355 const Version* version,
[email protected]650b2d52013-02-10 03:41:452356 const base::FilePath& path,
[email protected]1d5e58b2013-01-31 08:41:402357 Manifest::Location location,
[email protected]47fc70c2011-12-06 07:29:512358 int creation_flags,
2359 bool mark_acknowledged) {
[email protected]ab22ba42011-01-14 16:36:382360 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2361 CHECK(Extension::IdIsValid(id));
[email protected]79c833b52011-04-05 18:31:012362 if (extension_prefs_->IsExternalExtensionUninstalled(id))
[email protected]9060d8b02012-01-13 02:14:302363 return false;
[email protected]a8af9fdb2010-10-28 21:52:202364
[email protected]7577a5c52009-07-30 06:21:582365 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:492366 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:582367 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:022368 const Extension* existing = GetExtensionById(id, true);
[email protected]910f72ce2012-08-24 01:38:352369
[email protected]7577a5c52009-07-30 06:21:582370 if (existing) {
[email protected]910f72ce2012-08-24 01:38:352371 // The default apps will have the location set as INTERNAL. Since older
2372 // default apps are installed as EXTERNAL, we override them. However, if the
2373 // app is already installed as internal, then do the version check.
[email protected]31bb5ee62012-09-12 22:58:402374 // TODO(grv) : Remove after Q1-2013.
[email protected]910f72ce2012-08-24 01:38:352375 bool is_default_apps_migration =
[email protected]1d5e58b2013-01-31 08:41:402376 (location == Manifest::INTERNAL &&
2377 Manifest::IsExternalLocation(existing->location()));
[email protected]910f72ce2012-08-24 01:38:352378
2379 if (!is_default_apps_migration) {
2380 DCHECK(version);
2381
2382 switch (existing->version()->CompareTo(*version)) {
2383 case -1: // existing version is older, we should upgrade
2384 break;
2385 case 0: // existing version is same, do nothing
2386 return false;
2387 case 1: // existing version is newer, uh-oh
2388 LOG(WARNING) << "Found external version of extension " << id
2389 << "that is older than current version. Current version "
2390 << "is: " << existing->VersionString() << ". New "
2391 << "version is: " << version->GetString()
2392 << ". Keeping current version.";
2393 return false;
2394 }
[email protected]7577a5c52009-07-30 06:21:582395 }
2396 }
2397
[email protected]9060d8b02012-01-13 02:14:302398 // If the extension is already pending, don't start an install.
[email protected]e3987852012-05-04 10:06:302399 if (!pending_extension_manager()->AddFromExternalFile(
[email protected]464213a2013-10-15 01:06:482400 id, location, *version, creation_flags, mark_acknowledged)) {
[email protected]9060d8b02012-01-13 02:14:302401 return false;
[email protected]e3987852012-05-04 10:06:302402 }
[email protected]9c635f22010-12-02 09:36:362403
[email protected]14908b72011-04-20 06:54:362404 // no client (silent install)
[email protected]f8636f92013-08-09 21:02:372405 scoped_refptr<CrxInstaller> installer(CrxInstaller::CreateSilent(this));
[email protected]6dfbbf82010-03-12 23:09:162406 installer->set_install_source(location);
2407 installer->set_expected_id(id);
[email protected]cb0e50312011-05-09 15:03:072408 installer->set_expected_version(*version);
2409 installer->set_install_cause(extension_misc::INSTALL_CAUSE_EXTERNAL_FILE);
[email protected]1bf73cc32011-10-26 22:38:312410 installer->set_creation_flags(creation_flags);
[email protected]88e8ec9152013-01-17 04:05:182411#if defined(OS_CHROMEOS)
2412 extensions::InstallLimiter::Get(profile_)->Add(installer, path);
2413#else
[email protected]6dfbbf82010-03-12 23:09:162414 installer->InstallCrx(path);
[email protected]88e8ec9152013-01-17 04:05:182415#endif
[email protected]47fc70c2011-12-06 07:29:512416
2417 // Depending on the source, a new external extension might not need a user
2418 // notification on installation. For such extensions, mark them acknowledged
2419 // now to suppress the notification.
2420 if (mark_acknowledged)
2421 AcknowledgeExternalExtension(id);
[email protected]9060d8b02012-01-13 02:14:302422
2423 return true;
[email protected]7577a5c52009-07-30 06:21:582424}
2425
[email protected]023b3d12013-12-23 18:46:492426scoped_ptr<base::DictionaryValue> ExtensionService::GetExtensionInfo(
[email protected]b5a507b22013-11-08 20:41:572427 const std::string& extension_id) const {
[email protected]023b3d12013-12-23 18:46:492428 scoped_ptr<base::DictionaryValue> dictionary(new base::DictionaryValue);
[email protected]bb1bc9b32013-12-21 03:09:142429 const extensions::Extension* extension =
2430 registry_->enabled_extensions().GetByID(extension_id);
[email protected]b5a507b22013-11-08 20:41:572431 if (extension) {
2432 GURL icon = extensions::ExtensionIconSource::GetIconURL(
2433 extension, extension_misc::EXTENSION_ICON_SMALLISH,
2434 ExtensionIconSet::MATCH_BIGGER, false, NULL);
2435 dictionary->SetString("id", extension_id);
2436 dictionary->SetString("name", extension->name());
2437 dictionary->SetString("icon", icon.spec());
2438 }
2439 return dictionary.Pass();
2440}
2441
[email protected]eaa7dd182010-12-14 11:09:002442void ExtensionService::ReportExtensionLoadError(
[email protected]650b2d52013-02-10 03:41:452443 const base::FilePath& extension_path,
[email protected]d11c8e92009-10-20 23:26:402444 const std::string &error,
[email protected]d11c8e92009-10-20 23:26:402445 bool be_noisy) {
[email protected]d8c8f25f2011-11-02 18:18:012446 content::NotificationService::current()->Notify(
2447 chrome::NOTIFICATION_EXTENSION_LOAD_ERROR,
2448 content::Source<Profile>(profile_),
2449 content::Details<const std::string>(&error));
[email protected]d11c8e92009-10-20 23:26:402450
[email protected]04338722013-12-24 23:18:052451 std::string path_str = base::UTF16ToUTF8(extension_path.LossyDisplayName());
2452 base::string16 message = base::UTF8ToUTF16(base::StringPrintf(
[email protected]18d4b6c2010-09-21 03:21:042453 "Could not load extension from '%s'. %s",
[email protected]fc670822011-12-17 09:33:492454 path_str.c_str(), error.c_str()));
[email protected]d11c8e92009-10-20 23:26:402455 ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy);
2456}
2457
[email protected]eaa7dd182010-12-14 11:09:002458void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]3a1dc572012-07-31 22:25:132459 extensions::ExtensionHost* host) {
[email protected]406027c02010-09-27 08:03:182460 OrphanedDevTools::iterator iter =
[email protected]2d2f6cfc2011-05-06 21:09:332461 orphaned_dev_tools_.find(host->extension_id());
[email protected]406027c02010-09-27 08:03:182462 if (iter == orphaned_dev_tools_.end())
2463 return;
2464
[email protected]04ea1bb2013-07-10 09:26:092465 iter->second->ConnectRenderViewHost(host->render_view_host());
[email protected]406027c02010-09-27 08:03:182466 orphaned_dev_tools_.erase(iter);
2467}
2468
[email protected]432115822011-07-10 15:52:272469void ExtensionService::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:532470 const content::NotificationSource& source,
2471 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:272472 switch (type) {
[email protected]3c4abc82012-10-22 22:25:542473 case chrome::NOTIFICATION_APP_TERMINATING:
2474 // Shutdown has started. Don't start any more extension installs.
2475 // (We cannot use ExtensionService::Shutdown() for this because it
2476 // happens too late in browser teardown.)
2477 browser_terminating_ = true;
2478 break;
[email protected]432115822011-07-10 15:52:272479 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]6c2381d2011-10-19 02:52:532480 if (profile_ !=
2481 content::Source<Profile>(source).ptr()->GetOriginalProfile()) {
[email protected]bc535ee52010-08-31 18:40:322482 break;
[email protected]6c2381d2011-10-19 02:52:532483 }
[email protected]a4ed6282009-12-14 20:51:162484
[email protected]3a1dc572012-07-31 22:25:132485 extensions::ExtensionHost* host =
2486 content::Details<extensions::ExtensionHost>(details).ptr();
[email protected]f128af42010-08-05 18:05:262487
[email protected]fa2416f2011-05-03 08:41:202488 // Mark the extension as terminated and Unload it. We want it to
2489 // be in a consistent state: either fully working or not loaded
2490 // at all, but never half-crashed. We do it in a PostTask so
2491 // that other handlers of this notification will still have
2492 // access to the Extension and ExtensionHost.
[email protected]b3a25092013-05-28 22:08:162493 base::MessageLoop::current()->PostTask(
[email protected]14908b72011-04-20 06:54:362494 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362495 base::Bind(
[email protected]fa2416f2011-05-03 08:41:202496 &ExtensionService::TrackTerminatedExtension,
[email protected]d8c8f25f2011-11-02 18:18:012497 AsWeakPtr(),
[email protected]fa2416f2011-05-03 08:41:202498 host->extension()));
[email protected]31f77262009-12-02 20:48:532499 break;
2500 }
[email protected]432115822011-07-10 15:52:272501 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: {
[email protected]b32c76f2014-01-16 21:23:472502 // TODO(jamescook): Extract this block of code to src/extensions so it
2503 // can be shared with app_shell.
[email protected]f3b1a082011-11-18 00:34:302504 content::RenderProcessHost* process =
2505 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192506 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302507 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192508 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2509 break;
2510
[email protected]15397b42012-05-16 23:56:062511 // Extensions need to know the channel for API restrictions.
2512 process->Send(new ExtensionMsg_SetChannel(
[email protected]15d4d2d2013-08-09 06:49:032513 extensions::GetCurrentChannel()));
[email protected]15397b42012-05-16 23:56:062514
[email protected]4941fe92013-06-13 23:21:012515 // Platform apps need to know the system font.
2516 scoped_ptr<base::DictionaryValue> fonts(new base::DictionaryValue);
2517 webui::SetFontAndTextDirection(fonts.get());
2518 std::string font_family, font_size;
2519 fonts->GetString("fontfamily", &font_family);
2520 fonts->GetString("fontsize", &font_size);
2521 process->Send(new ExtensionMsg_SetSystemFont(
2522 font_family, font_size));
2523
[email protected]77a6970c2011-04-23 16:58:562524 // Valid extension function names, used to setup bindings in renderer.
2525 std::vector<std::string> function_names;
2526 ExtensionFunctionDispatcher::GetAllFunctionNames(&function_names);
2527 process->Send(new ExtensionMsg_SetFunctionNames(function_names));
[email protected]31f77262009-12-02 20:48:532528
[email protected]77a6970c2011-04-23 16:58:562529 // Scripting whitelist. This is modified by tests and must be communicated
2530 // to renderers.
2531 process->Send(new ExtensionMsg_SetScriptingWhitelist(
[email protected]c81f5d32013-10-26 10:38:422532 extensions::ExtensionsClient::Get()->GetScriptingWhitelist()));
[email protected]77a6970c2011-04-23 16:58:562533
2534 // Loaded extensions.
[email protected]9776e82e2011-11-15 02:17:532535 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions;
[email protected]bb1bc9b32013-12-21 03:09:142536 const ExtensionSet& extensions = registry_->enabled_extensions();
2537 for (ExtensionSet::const_iterator iter = extensions.begin();
2538 iter != extensions.end(); ++iter) {
[email protected]19647262011-12-16 09:57:492539 // Renderers don't need to know about themes.
2540 if (!(*iter)->is_theme())
[email protected]cadac622013-06-11 16:46:362541 loaded_extensions.push_back(ExtensionMsg_Loaded_Params(iter->get()));
[email protected]77a6970c2011-04-23 16:58:562542 }
[email protected]9776e82e2011-11-15 02:17:532543 process->Send(new ExtensionMsg_Loaded(loaded_extensions));
[email protected]77a6970c2011-04-23 16:58:562544 break;
2545 }
[email protected]432115822011-07-10 15:52:272546 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
[email protected]f3b1a082011-11-18 00:34:302547 content::RenderProcessHost* process =
2548 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192549 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302550 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192551 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2552 break;
2553
[email protected]fafdc842014-01-17 18:09:082554 extensions::ProcessMap* process_map =
2555 extensions::ProcessMap::Get(profile_);
2556 if (process_map->Contains(process->GetID())) {
[email protected]52b76592013-11-02 17:59:032557 // An extension process was terminated, this might have resulted in an
2558 // app or extension becoming idle.
2559 std::set<std::string> extension_ids =
[email protected]fafdc842014-01-17 18:09:082560 process_map->GetExtensionsInProcess(process->GetID());
[email protected]52b76592013-11-02 17:59:032561 for (std::set<std::string>::const_iterator it = extension_ids.begin();
2562 it != extension_ids.end(); ++it) {
2563 if (delayed_installs_.Contains(*it)) {
2564 base::MessageLoop::current()->PostDelayedTask(
2565 FROM_HERE,
2566 base::Bind(&ExtensionService::MaybeFinishDelayedInstallation,
2567 AsWeakPtr(), *it),
2568 base::TimeDelta::FromSeconds(kUpdateIdleDelay));
2569 }
2570 }
2571 }
2572
[email protected]fafdc842014-01-17 18:09:082573 process_map->RemoveAllFromProcess(process->GetID());
[email protected]6f371442011-11-09 06:45:462574 BrowserThread::PostTask(
[email protected]38427a12013-11-09 17:34:202575 BrowserThread::IO,
2576 FROM_HERE,
2577 base::Bind(&extensions::InfoMap::UnregisterAllExtensionsInProcess,
[email protected]31d8f5f22012-04-02 15:22:082578 system_->info_map(),
[email protected]f3b1a082011-11-18 00:34:302579 process->GetID()));
[email protected]da5683db2011-04-23 17:12:212580 break;
2581 }
[email protected]92dd8d92013-02-26 00:41:082582 case chrome::NOTIFICATION_UPGRADE_RECOMMENDED: {
[email protected]75bdcb872013-03-13 00:41:452583 // Notify observers that chrome update is available.
2584 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
2585 OnChromeUpdateAvailable());
[email protected]92dd8d92013-02-26 00:41:082586 break;
2587 }
[email protected]aa96d3a2010-08-21 08:45:252588
[email protected]4814b512009-11-07 00:12:292589 default:
2590 NOTREACHED() << "Unexpected notification type.";
2591 }
2592}
2593
[email protected]90bb38d2012-11-14 18:36:032594void ExtensionService::OnExtensionInstallPrefChanged() {
[email protected]a6a7ced2012-11-01 17:24:182595 IdentifyAlertableExtensions();
2596 CheckManagementPolicy();
2597}
2598
[email protected]eaa7dd182010-12-14 11:09:002599bool ExtensionService::HasApps() const {
[email protected]ec5b50d2010-10-09 16:35:182600 return !GetAppIds().empty();
2601}
[email protected]377011d2010-07-20 04:18:502602
[email protected]eaa7dd182010-12-14 11:09:002603ExtensionIdSet ExtensionService::GetAppIds() const {
[email protected]ec5b50d2010-10-09 16:35:182604 ExtensionIdSet result;
[email protected]bb1bc9b32013-12-21 03:09:142605 const ExtensionSet& extensions = registry_->enabled_extensions();
2606 for (ExtensionSet::const_iterator it = extensions.begin();
2607 it != extensions.end(); ++it) {
[email protected]1d5e58b2013-01-31 08:41:402608 if ((*it)->is_app() && (*it)->location() != Manifest::COMPONENT)
[email protected]ec5b50d2010-10-09 16:35:182609 result.insert((*it)->id());
[email protected]377011d2010-07-20 04:18:502610 }
2611
[email protected]ec5b50d2010-10-09 16:35:182612 return result;
[email protected]377011d2010-07-20 04:18:502613}
[email protected]d7e9a862010-11-03 21:57:492614
[email protected]e178ad92013-06-28 02:29:252615bool ExtensionService::IsBeingReloaded(
2616 const std::string& extension_id) const {
2617 return ContainsKey(extensions_being_reloaded_, extension_id);
2618}
2619
2620void ExtensionService::SetBeingReloaded(const std::string& extension_id,
[email protected]5e7015c2013-07-23 23:29:432621 bool isBeingReloaded) {
2622 if (isBeingReloaded)
[email protected]e178ad92013-06-28 02:29:252623 extensions_being_reloaded_.insert(extension_id);
[email protected]5e7015c2013-07-23 23:29:432624 else
[email protected]e178ad92013-06-28 02:29:252625 extensions_being_reloaded_.erase(extension_id);
[email protected]e178ad92013-06-28 02:29:252626}
2627
[email protected]612a1cb12012-10-17 13:18:032628bool ExtensionService::ShouldEnableOnInstall(const Extension* extension) {
2629 // Extensions installed by policy can't be disabled. So even if a previous
2630 // installation disabled the extension, make sure it is now enabled.
2631 if (system_->management_policy()->MustRemainEnabled(extension, NULL))
2632 return true;
2633
2634 if (extension_prefs_->IsExtensionDisabled(extension->id()))
2635 return false;
2636
[email protected]41070e8d2012-10-24 01:34:362637 if (FeatureSwitch::prompt_for_external_extensions()->IsEnabled()) {
[email protected]612a1cb12012-10-17 13:18:032638 // External extensions are initially disabled. We prompt the user before
[email protected]2c495c42013-01-04 21:49:542639 // enabling them. Hosted apps are excepted because they are not dangerous
2640 // (they need to be launched by the user anyway).
[email protected]1d5e58b2013-01-31 08:41:402641 if (extension->GetType() != Manifest::TYPE_HOSTED_APP &&
2642 Manifest::IsExternalLocation(extension->location()) &&
[email protected]612a1cb12012-10-17 13:18:032643 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id())) {
2644 return false;
2645 }
2646 }
[email protected]612a1cb12012-10-17 13:18:032647
2648 return true;
2649}
[email protected]0db124b02012-11-07 04:55:052650
[email protected]e7aa7b7e2012-11-27 04:51:222651bool ExtensionService::ShouldDelayExtensionUpdate(
2652 const std::string& extension_id,
2653 bool wait_for_idle) const {
2654 const char kOnUpdateAvailableEvent[] = "runtime.onUpdateAvailable";
2655
2656 // If delayed updates are globally disabled, or just for this extension,
2657 // don't delay.
2658 if (!install_updates_when_idle_ || !wait_for_idle)
2659 return false;
2660
[email protected]695b5712012-12-06 23:55:282661 const Extension* old = GetInstalledExtension(extension_id);
[email protected]e7aa7b7e2012-11-27 04:51:222662 // If there is no old extension, this is not an update, so don't delay.
2663 if (!old)
2664 return false;
2665
[email protected]9367eabc2013-03-01 01:29:292666 if (extensions::BackgroundInfo::HasPersistentBackgroundPage(old)) {
[email protected]e7aa7b7e2012-11-27 04:51:222667 // Delay installation if the extension listens for the onUpdateAvailable
2668 // event.
2669 return system_->event_router()->ExtensionHasEventListener(
2670 extension_id, kOnUpdateAvailableEvent);
2671 } else {
2672 // Delay installation if the extension is not idle.
[email protected]617342a42013-12-18 23:34:032673 return !extension_util::IsExtensionIdle(extension_id, system_);
[email protected]e7aa7b7e2012-11-27 04:51:222674 }
[email protected]0db124b02012-11-07 04:55:052675}
[email protected]fdd679b2012-11-15 20:49:392676
[email protected]399583b2012-12-11 09:33:422677void ExtensionService::GarbageCollectIsolatedStorage() {
[email protected]650b2d52013-02-10 03:41:452678 scoped_ptr<base::hash_set<base::FilePath> > active_paths(
2679 new base::hash_set<base::FilePath>());
[email protected]bb1bc9b32013-12-21 03:09:142680 const ExtensionSet& extensions = registry_->enabled_extensions();
2681 for (ExtensionSet::const_iterator it = extensions.begin();
2682 it != extensions.end(); ++it) {
[email protected]cadac622013-06-11 16:46:362683 if (extensions::AppIsolationInfo::HasIsolatedStorage(it->get())) {
2684 active_paths->insert(BrowserContext::GetStoragePartitionForSite(
2685 profile_, GetSiteForExtensionId((*it)->id()))->GetPath());
[email protected]399583b2012-12-11 09:33:422686 }
2687 }
2688
[email protected]9f4e4f082013-06-21 07:11:192689 DCHECK(!installs_delayed_for_gc());
2690 set_installs_delayed_for_gc(true);
[email protected]399583b2012-12-11 09:33:422691 BrowserContext::GarbageCollectStoragePartitions(
2692 profile_, active_paths.Pass(),
2693 base::Bind(&ExtensionService::OnGarbageCollectIsolatedStorageFinished,
2694 AsWeakPtr()));
2695}
2696
2697void ExtensionService::OnGarbageCollectIsolatedStorageFinished() {
[email protected]9f4e4f082013-06-21 07:11:192698 set_installs_delayed_for_gc(false);
2699 MaybeFinishDelayedInstallations();
2700}
2701
2702void ExtensionService::MaybeFinishDelayedInstallations() {
2703 std::vector<std::string> to_be_installed;
[email protected]399583b2012-12-11 09:33:422704 for (ExtensionSet::const_iterator it = delayed_installs_.begin();
2705 it != delayed_installs_.end();
2706 ++it) {
[email protected]9f4e4f082013-06-21 07:11:192707 to_be_installed.push_back((*it)->id());
[email protected]399583b2012-12-11 09:33:422708 }
[email protected]9f4e4f082013-06-21 07:11:192709 for (std::vector<std::string>::const_iterator it = to_be_installed.begin();
2710 it != to_be_installed.end();
[email protected]399583b2012-12-11 09:33:422711 ++it) {
[email protected]9f4e4f082013-06-21 07:11:192712 MaybeFinishDelayedInstallation(*it);
[email protected]399583b2012-12-11 09:33:422713 }
[email protected]399583b2012-12-11 09:33:422714}
2715
[email protected]fdd679b2012-11-15 20:49:392716void ExtensionService::OnBlacklistUpdated() {
[email protected]48a359342013-10-30 00:22:002717 blacklist_->GetMalwareIDs(
[email protected]695b5712012-12-06 23:55:282718 GenerateInstalledExtensionsSet()->GetIDs(),
[email protected]3f2a2fa2013-09-24 02:55:252719 base::Bind(&ExtensionService::ManageBlacklist, AsWeakPtr()));
[email protected]695b5712012-12-06 23:55:282720}
2721
[email protected]3f2a2fa2013-09-24 02:55:252722void ExtensionService::ManageBlacklist(const std::set<std::string>& updated) {
[email protected]695b5712012-12-06 23:55:282723 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2724
[email protected]bb1bc9b32013-12-21 03:09:142725 std::set<std::string> before = registry_->blacklisted_extensions().GetIDs();
[email protected]16bf7ba72013-08-23 11:52:542726 std::set<std::string> no_longer_blacklisted =
[email protected]3f2a2fa2013-09-24 02:55:252727 base::STLSetDifference<std::set<std::string> >(before, updated);
[email protected]16bf7ba72013-08-23 11:52:542728 std::set<std::string> not_yet_blacklisted =
[email protected]3f2a2fa2013-09-24 02:55:252729 base::STLSetDifference<std::set<std::string> >(updated, before);
[email protected]695b5712012-12-06 23:55:282730
2731 for (std::set<std::string>::iterator it = no_longer_blacklisted.begin();
2732 it != no_longer_blacklisted.end(); ++it) {
2733 scoped_refptr<const Extension> extension =
[email protected]bb1bc9b32013-12-21 03:09:142734 registry_->blacklisted_extensions().GetByID(*it);
[email protected]3f2a2fa2013-09-24 02:55:252735 if (!extension.get()) {
2736 NOTREACHED() << "Extension " << *it << " no longer blacklisted, "
2737 << "but it was never blacklisted.";
[email protected]695b5712012-12-06 23:55:282738 continue;
[email protected]3f2a2fa2013-09-24 02:55:252739 }
[email protected]bb1bc9b32013-12-21 03:09:142740 registry_->RemoveBlacklisted(*it);
[email protected]3f2a2fa2013-09-24 02:55:252741 extension_prefs_->SetExtensionBlacklisted(extension->id(), false);
[email protected]dc24976f2013-06-02 21:15:092742 AddExtension(extension.get());
[email protected]ac875372013-02-28 04:36:092743 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.UnblacklistInstalled",
[email protected]dc24976f2013-06-02 21:15:092744 extension->location(),
2745 Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:282746 }
2747
2748 for (std::set<std::string>::iterator it = not_yet_blacklisted.begin();
2749 it != not_yet_blacklisted.end(); ++it) {
2750 scoped_refptr<const Extension> extension = GetInstalledExtension(*it);
[email protected]3f2a2fa2013-09-24 02:55:252751 if (!extension.get()) {
2752 NOTREACHED() << "Extension " << *it << " needs to be "
2753 << "blacklisted, but it's not installed.";
[email protected]695b5712012-12-06 23:55:282754 continue;
[email protected]3f2a2fa2013-09-24 02:55:252755 }
[email protected]bb1bc9b32013-12-21 03:09:142756 registry_->AddBlacklisted(extension);
[email protected]3f2a2fa2013-09-24 02:55:252757 extension_prefs_->SetExtensionBlacklisted(extension->id(), true);
[email protected]b0af4792013-10-23 09:12:132758 UnloadExtension(*it, UnloadedExtensionInfo::REASON_BLACKLIST);
[email protected]ac875372013-02-28 04:36:092759 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.BlacklistInstalled",
2760 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:282761 }
2762
2763 IdentifyAlertableExtensions();
[email protected]fdd679b2012-11-15 20:49:392764}
[email protected]75bdcb872013-03-13 00:41:452765
2766void ExtensionService::AddUpdateObserver(extensions::UpdateObserver* observer) {
2767 update_observers_.AddObserver(observer);
2768}
2769
2770void ExtensionService::RemoveUpdateObserver(
2771 extensions::UpdateObserver* observer) {
2772 update_observers_.RemoveObserver(observer);
2773}
[email protected]bb1bc9b32013-12-21 03:09:142774
2775// Used only by test code.
2776void ExtensionService::UnloadAllExtensionsInternal() {
2777 profile_->GetExtensionSpecialStoragePolicy()->RevokeRightsForAllExtensions();
2778
2779 registry_->ClearAll();
[email protected]45f5b7d2014-01-22 23:47:132780 system_->runtime_data()->ClearAll();
[email protected]bb1bc9b32013-12-21 03:09:142781
2782 // TODO(erikkay) should there be a notification for this? We can't use
2783 // EXTENSION_UNLOADED since that implies that the extension has been disabled
2784 // or uninstalled.
2785}