blob: f8245a56f3b091a404eb01a27badbf9469934199 [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]612a1cb12012-10-17 13:18:0344#include "chrome/browser/extensions/external_install_ui.h"
[email protected]5df038b2012-07-16 19:03:2745#include "chrome/browser/extensions/external_provider_impl.h"
[email protected]ffd2f79e2013-11-14 00:11:4646#include "chrome/browser/extensions/install_verifier.h"
[email protected]d8c8f25f2011-11-02 18:18:0147#include "chrome/browser/extensions/installed_loader.h"
[email protected]c333e792012-01-06 16:57:3948#include "chrome/browser/extensions/permissions_updater.h"
[email protected]d8c8f25f2011-11-02 18:18:0149#include "chrome/browser/extensions/unpacked_installer.h"
[email protected]42a08162012-03-16 18:09:1150#include "chrome/browser/extensions/updater/extension_updater.h"
[email protected]8ecad5e2010-12-02 21:18:3351#include "chrome/browser/profiles/profile.h"
[email protected]b5a507b22013-11-08 20:41:5752#include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
[email protected]c8d407e2011-04-28 21:27:1753#include "chrome/browser/ui/webui/favicon_source.h"
[email protected]5eddc3e2011-10-26 04:33:3154#include "chrome/browser/ui/webui/ntp/thumbnail_source.h"
[email protected]b07e606e2012-09-15 20:16:1555#include "chrome/browser/ui/webui/theme_source.h"
[email protected]e2eb43112009-05-29 21:19:5456#include "chrome/common/chrome_switches.h"
[email protected]f6431be82013-09-07 02:53:4557#include "chrome/common/crash_keys.h"
[email protected]9f4e4f082013-06-21 07:11:1958#include "chrome/common/extensions/extension_constants.h"
[email protected]7c927b62010-02-24 09:54:1359#include "chrome/common/extensions/extension_file_util.h"
[email protected]77a6970c2011-04-23 16:58:5660#include "chrome/common/extensions/extension_messages.h"
[email protected]15d4d2d2013-08-09 06:49:0361#include "chrome/common/extensions/features/feature_channel.h"
[email protected]561e33d52013-04-03 06:58:4362#include "chrome/common/extensions/manifest_handlers/app_isolation_info.h"
[email protected]6b414c232013-06-05 07:53:3463#include "chrome/common/extensions/manifest_handlers/app_launch_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]45928b682013-11-28 08:20:2778#include "extensions/browser/extensions_browser_client.h"
[email protected]301116c62013-11-26 10:37:4579#include "extensions/browser/external_provider_interface.h"
80#include "extensions/browser/management_policy.h"
[email protected]b52f8ca2013-11-28 08:25:2281#include "extensions/browser/pending_extension_manager.h"
[email protected]98b6d942013-11-10 00:34:0782#include "extensions/browser/process_manager.h"
[email protected]a9aa62b312013-11-29 05:35:0683#include "extensions/browser/update_observer.h"
[email protected]836e2982013-05-16 08:07:4284#include "extensions/common/constants.h"
[email protected]e9f541a2012-11-19 21:52:3185#include "extensions/common/error_utils.h"
[email protected]e4452d32013-11-15 23:07:4186#include "extensions/common/extension.h"
[email protected]c81f5d32013-10-26 10:38:4287#include "extensions/common/extensions_client.h"
[email protected]5ef835a2013-11-08 20:42:5788#include "extensions/common/feature_switch.h"
[email protected]d42c11152013-08-22 19:36:3289#include "extensions/common/manifest.h"
[email protected]0c3c9732013-09-16 08:53:4190#include "extensions/common/manifest_constants.h"
[email protected]558878cc82013-11-09 01:25:5191#include "extensions/common/manifest_handlers/background_info.h"
[email protected]1f7de252013-11-06 22:02:0092#include "extensions/common/manifest_handlers/incognito_info.h"
[email protected]301116c62013-11-26 10:37:4593#include "extensions/common/manifest_handlers/shared_module_info.h"
[email protected]c41003472013-10-19 15:37:2594#include "extensions/common/permissions/permission_message_provider.h"
[email protected]e4452d32013-11-15 23:07:4195#include "extensions/common/permissions/permissions_data.h"
[email protected]65187152012-06-02 13:14:1496#include "grit/generated_resources.h"
[email protected]be28b5f42012-07-20 11:31:2597#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
[email protected]cd67ed52013-10-15 01:22:1398#include "ui/base/webui/web_ui_util.h"
[email protected]a6483d22013-07-03 22:11:0099#include "url/gurl.h"
[email protected]08b1f75f2013-05-22 22:02:38100#include "webkit/browser/database/database_tracker.h"
101#include "webkit/browser/database/database_util.h"
[email protected]79a60642012-10-20 21:03:18102
[email protected]eed367e2011-04-12 03:43:31103#if defined(OS_CHROMEOS)
[email protected]88e8ec9152013-01-17 04:05:18104#include "chrome/browser/chromeos/extensions/install_limiter.h"
[email protected]f19bbf62013-07-09 01:22:32105#include "webkit/browser/fileapi/file_system_backend.h"
[email protected]c6f9203a2013-05-28 02:08:07106#include "webkit/browser/fileapi/file_system_context.h"
[email protected]eed367e2011-04-12 03:43:31107#endif
108
[email protected]55eb70e762012-02-20 17:38:39109using content::BrowserContext;
[email protected]631bb742011-11-02 11:29:39110using content::BrowserThread;
[email protected]98f66112012-12-25 12:59:36111using content::DevToolsAgentHost;
[email protected]bf3d9df2012-07-24 23:20:27112using extensions::CrxInstaller;
[email protected]1c321ee52012-05-21 03:02:34113using extensions::Extension;
114using extensions::ExtensionIdSet;
115using extensions::ExtensionInfo;
[email protected]215a7be2012-10-22 19:53:42116using extensions::FeatureSwitch;
[email protected]ffd2f79e2013-11-14 00:11:46117using extensions::InstallVerifier;
[email protected]0d54b682013-11-05 14:15:36118using extensions::ManagementPolicy;
[email protected]1d5e58b2013-01-31 08:41:40119using extensions::Manifest;
[email protected]c2e66e12012-06-27 06:27:06120using extensions::PermissionMessage;
121using extensions::PermissionMessages;
122using extensions::PermissionSet;
[email protected]9f4e4f082013-06-21 07:11:19123using extensions::SharedModuleInfo;
[email protected]e410b5f2012-12-14 14:02:24124using extensions::UnloadedExtensionInfo;
[email protected]5ef47ec2010-01-28 05:58:05125
[email protected]0c3c9732013-09-16 08:53:41126namespace errors = extensions::manifest_errors;
[email protected]c6d474f82009-12-16 21:11:06127
[email protected]b6ab96d2009-08-20 18:58:19128namespace {
129
[email protected]d96eb512012-11-01 23:44:08130// Histogram values for logging events related to externally installed
131// extensions.
132enum ExternalExtensionEvent {
133 EXTERNAL_EXTENSION_INSTALLED = 0,
134 EXTERNAL_EXTENSION_IGNORED,
135 EXTERNAL_EXTENSION_REENABLED,
136 EXTERNAL_EXTENSION_UNINSTALLED,
137 EXTERNAL_EXTENSION_BUCKET_BOUNDARY,
138};
139
[email protected]612a1cb12012-10-17 13:18:03140// Prompt the user this many times before considering an extension acknowledged.
141static const int kMaxExtensionAcknowledgePromptCount = 3;
142
[email protected]0db124b02012-11-07 04:55:05143// Wait this many seconds after an extensions becomes idle before updating it.
144static const int kUpdateIdleDelay = 5;
145
[email protected]9bd9a6862012-11-29 09:24:22146// Wait this many seconds before trying to garbage collect extensions again.
147static const int kGarbageCollectRetryDelay = 30;
148
[email protected]36fc54f2013-06-14 02:49:34149// Wait this many seconds after startup to see if there are any extensions
150// which can be garbage collected.
151static const int kGarbageCollectStartupDelay = 30;
152
[email protected]9f4e4f082013-06-21 07:11:19153static bool IsSharedModule(const Extension* extension) {
154 return SharedModuleInfo::IsSharedModule(extension);
155}
156
[email protected]4c9201c42013-08-16 04:56:21157static bool IsCWSSharedModule(const Extension* extension) {
158 return extension->from_webstore() && IsSharedModule(extension);
159}
160
161class SharedModuleProvider : public extensions::ManagementPolicy::Provider {
162 public:
163 SharedModuleProvider() {}
164 virtual ~SharedModuleProvider() {}
165
166 virtual std::string GetDebugPolicyProviderName() const OVERRIDE {
167 return "SharedModuleProvider";
168 }
169
170 virtual bool UserMayModifySettings(const Extension* extension,
171 string16* error) const OVERRIDE {
172 return !IsCWSSharedModule(extension);
173 }
174
175 virtual bool MustRemainEnabled(const Extension* extension,
176 string16* error) const OVERRIDE {
177 return IsCWSSharedModule(extension);
178 }
179
180 private:
181 DISALLOW_COPY_AND_ASSIGN(SharedModuleProvider);
182};
183
184
[email protected]c6d474f82009-12-16 21:11:06185} // namespace
[email protected]b6ab96d2009-08-20 18:58:19186
[email protected]eaa7dd182010-12-14 11:09:00187ExtensionService::ExtensionRuntimeData::ExtensionRuntimeData()
[email protected]d7e9a862010-11-03 21:57:49188 : background_page_ready(false),
[email protected]f8bf5992011-10-24 22:38:57189 being_upgraded(false),
190 has_used_webrequest(false) {
[email protected]d7e9a862010-11-03 21:57:49191}
192
[email protected]eaa7dd182010-12-14 11:09:00193ExtensionService::ExtensionRuntimeData::~ExtensionRuntimeData() {
[email protected]d7e9a862010-11-03 21:57:49194}
195
[email protected]eaa7dd182010-12-14 11:09:00196// ExtensionService.
[email protected]6014d672008-12-05 00:38:25197
[email protected]8e4560b62011-01-14 10:09:14198void ExtensionService::CheckExternalUninstall(const std::string& id) {
[email protected]a29a517a2011-01-21 21:11:12199 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20200
[email protected]7a4c6852010-09-16 03:44:22201 // Check if the providers know about this extension.
[email protected]5df038b2012-07-16 19:03:27202 extensions::ProviderCollection::const_iterator i;
[email protected]0a60a2e2010-10-25 16:15:21203 for (i = external_extension_providers_.begin();
204 i != external_extension_providers_.end(); ++i) {
[email protected]8e4560b62011-01-14 10:09:14205 DCHECK(i->get()->IsReady());
[email protected]0a60a2e2010-10-25 16:15:21206 if (i->get()->HasExtension(id))
207 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22208 }
209
[email protected]0f48fca2011-05-19 18:46:35210 // We get the list of external extensions to check from preferences.
211 // It is possible that an extension has preferences but is not loaded.
212 // For example, an extension that requires experimental permissions
213 // will not be loaded if the experimental command line flag is not used.
214 // In this case, do not uninstall.
[email protected]31bb5ee62012-09-12 22:58:40215 if (!GetInstalledExtension(id)) {
[email protected]0f48fca2011-05-19 18:46:35216 // We can't call UninstallExtension with an unloaded/invalid
217 // extension ID.
218 LOG(WARNING) << "Attempted uninstallation of unloaded/invalid extension "
219 << "with id: " << id;
220 return;
221 }
[email protected]d6ebc9792011-04-07 18:18:33222 UninstallExtension(id, true, NULL);
[email protected]7a4c6852010-09-16 03:44:22223}
224
[email protected]7f8f24f2012-11-15 19:40:14225void ExtensionService::SetFileTaskRunnerForTesting(
226 base::SequencedTaskRunner* task_runner) {
227 file_task_runner_ = task_runner;
228}
229
[email protected]8e4560b62011-01-14 10:09:14230void ExtensionService::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22231 external_extension_providers_.clear();
232}
233
[email protected]8e4560b62011-01-14 10:09:14234void ExtensionService::AddProviderForTesting(
[email protected]5df038b2012-07-16 19:03:27235 extensions::ExternalProviderInterface* test_provider) {
[email protected]a29a517a2011-01-21 21:11:12236 CHECK(test_provider);
[email protected]0a60a2e2010-10-25 16:15:21237 external_extension_providers_.push_back(
[email protected]5df038b2012-07-16 19:03:27238 linked_ptr<extensions::ExternalProviderInterface>(test_provider));
[email protected]7a4c6852010-09-16 03:44:22239}
240
[email protected]9060d8b02012-01-13 02:14:30241bool ExtensionService::OnExternalExtensionUpdateUrlFound(
[email protected]7a4c6852010-09-16 03:44:22242 const std::string& id,
[email protected]21a5a672010-11-04 10:47:42243 const GURL& update_url,
[email protected]464213a2013-10-15 01:06:48244 Manifest::Location location,
245 int creation_flags,
246 bool mark_acknowledged) {
[email protected]ab22ba42011-01-14 16:36:38247 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
248 CHECK(Extension::IdIsValid(id));
[email protected]a8af9fdb2010-10-28 21:52:20249
[email protected]8a87a5332011-08-11 17:54:59250 const Extension* extension = GetExtensionById(id, true);
251 if (extension) {
252 // Already installed. Skip this install if the current location has
253 // higher priority than |location|.
[email protected]1d5e58b2013-01-31 08:41:40254 Manifest::Location current = extension->location();
255 if (current == Manifest::GetHigherPriorityLocation(current, location))
[email protected]9060d8b02012-01-13 02:14:30256 return false;
[email protected]8a87a5332011-08-11 17:54:59257 // Otherwise, overwrite the current installation.
[email protected]7a4c6852010-09-16 03:44:22258 }
[email protected]9060d8b02012-01-13 02:14:30259
260 // Add |id| to the set of pending extensions. If it can not be added,
261 // then there is already a pending record from a higher-priority install
262 // source. In this case, signal that this extension will not be
263 // installed by returning false.
264 if (!pending_extension_manager()->AddFromExternalUpdateUrl(
[email protected]464213a2013-10-15 01:06:48265 id, update_url, location, creation_flags, mark_acknowledged)) {
[email protected]9060d8b02012-01-13 02:14:30266 return false;
[email protected]31bb5ee62012-09-12 22:58:40267 }
[email protected]9060d8b02012-01-13 02:14:30268
[email protected]94fde232012-04-27 10:22:30269 update_once_all_providers_are_ready_ = true;
[email protected]9060d8b02012-01-13 02:14:30270 return true;
[email protected]7a4c6852010-09-16 03:44:22271}
272
[email protected]dc9a74f72012-08-17 18:07:21273const Extension* ExtensionService::GetInstalledApp(const GURL& url) const {
[email protected]be9915fb2013-07-18 09:28:55274 const Extension* extension = extensions_.GetExtensionOrAppByURL(url);
[email protected]dc9a74f72012-08-17 18:07:21275 return (extension && extension->is_app()) ? extension : NULL;
[email protected]d9696672011-03-15 22:45:09276}
277
[email protected]dc9a74f72012-08-17 18:07:21278bool ExtensionService::IsInstalledApp(const GURL& url) const {
[email protected]d9696672011-03-15 22:45:09279 return !!GetInstalledApp(url);
[email protected]ffb204f22010-12-05 23:20:27280}
281
[email protected]6aeac8342010-10-01 20:21:18282// static
[email protected]d6ebc9792011-04-07 18:18:33283// This function is used to implement the command-line switch
[email protected]3bdba0d2011-08-23 07:17:30284// --uninstall-extension, and to uninstall an extension via sync. The LOG
285// statements within this function are used to inform the user if the uninstall
286// cannot be done.
[email protected]eaa7dd182010-12-14 11:09:00287bool ExtensionService::UninstallExtensionHelper(
288 ExtensionService* extensions_service,
[email protected]6aeac8342010-10-01 20:21:18289 const std::string& extension_id) {
[email protected]d6ebc9792011-04-07 18:18:33290 // We can't call UninstallExtension with an invalid extension ID.
[email protected]31bb5ee62012-09-12 22:58:40291 if (!extensions_service->GetInstalledExtension(extension_id)) {
[email protected]6aeac8342010-10-01 20:21:18292 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]95da88c42011-03-31 10:07:33293 << "id: " << extension_id;
294 return false;
[email protected]6aeac8342010-10-01 20:21:18295 }
296
[email protected]d6ebc9792011-04-07 18:18:33297 // The following call to UninstallExtension will not allow an uninstall of a
298 // policy-controlled extension.
[email protected]65187152012-06-02 13:14:14299 string16 error;
[email protected]d6ebc9792011-04-07 18:18:33300 if (!extensions_service->UninstallExtension(extension_id, false, &error)) {
301 LOG(WARNING) << "Cannot uninstall extension with id " << extension_id
302 << ": " << error;
303 return false;
304 }
[email protected]95da88c42011-03-31 10:07:33305
[email protected]6aeac8342010-10-01 20:21:18306 return true;
307}
308
[email protected]eaa7dd182010-12-14 11:09:00309ExtensionService::ExtensionService(Profile* profile,
[email protected]f0841cd2011-01-19 15:07:24310 const CommandLine* command_line,
[email protected]650b2d52013-02-10 03:41:45311 const base::FilePath& install_directory,
[email protected]45759612012-07-10 17:21:23312 extensions::ExtensionPrefs* extension_prefs,
[email protected]fdd679b2012-11-15 20:49:39313 extensions::Blacklist* blacklist,
[email protected]0436b102011-04-15 18:30:03314 bool autoupdate_enabled,
[email protected]4a10006a2013-05-17 23:18:35315 bool extensions_enabled,
316 extensions::OneShotEvent* ready)
[email protected]fdd679b2012-11-15 20:49:39317 : extensions::Blacklist::Observer(blacklist),
318 profile_(profile),
[email protected]bd306722012-07-11 20:43:59319 system_(extensions::ExtensionSystem::Get(profile)),
[email protected]73c47932010-12-06 18:13:43320 extension_prefs_(extension_prefs),
[email protected]695b5712012-12-06 23:55:28321 blacklist_(blacklist),
[email protected]cccdf0aa2011-11-11 03:43:38322 settings_frontend_(extensions::SettingsFrontend::Create(profile)),
[email protected]f8aefb132013-10-30 09:29:52323 extension_sync_service_(NULL),
[email protected]9c009092013-05-01 03:14:09324 pending_extension_manager_(*this),
[email protected]a9b00ac2009-06-25 21:03:23325 install_directory_(install_directory),
[email protected]0436b102011-04-15 18:30:03326 extensions_enabled_(extensions_enabled),
[email protected]e81dba32009-06-19 20:19:13327 show_extensions_prompts_(true),
[email protected]fc332ae2012-11-14 20:17:33328 install_updates_when_idle_(true),
[email protected]4a10006a2013-05-17 23:18:35329 ready_(ready),
[email protected]dff1e042012-04-27 10:59:18330 update_once_all_providers_are_ready_(false),
[email protected]3c4abc82012-10-22 22:25:54331 browser_terminating_(false),
[email protected]9f4e4f082013-06-21 07:11:19332 installs_delayed_for_gc_(false),
[email protected]f8aefb132013-10-30 09:29:52333 is_first_run_(false) {
[email protected]c04fd3dd2013-09-05 08:20:10334#if defined(OS_CHROMEOS)
335 disable_garbage_collection_ = false;
336#endif
[email protected]a29a517a2011-01-21 21:11:12337 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20338
[email protected]36a784c2009-06-23 06:21:08339 // Figure out if extension installation should be enabled.
[email protected]367d9b172013-12-03 00:31:02340 if (extensions::ExtensionsBrowserClient::Get()->AreExtensionsDisabled(
341 *command_line, profile))
[email protected]6d60703b2009-08-29 01:29:23342 extensions_enabled_ = false;
[email protected]36a784c2009-06-23 06:21:08343
[email protected]3c4abc82012-10-22 22:25:54344 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING,
345 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27346 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07347 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27348 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED,
[email protected]ad50def52011-10-19 23:17:07349 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27350 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07351 content::NotificationService::AllBrowserContextsAndSources());
[email protected]92dd8d92013-02-26 00:41:08352 registrar_.Add(this, chrome::NOTIFICATION_UPGRADE_RECOMMENDED,
353 content::NotificationService::AllBrowserContextsAndSources());
[email protected]2fb7dc982010-09-29 12:24:28354 pref_change_registrar_.Init(profile->GetPrefs());
[email protected]90bb38d2012-11-14 18:36:03355 base::Closure callback =
356 base::Bind(&ExtensionService::OnExtensionInstallPrefChanged,
357 base::Unretained(this));
358 pref_change_registrar_.Add(prefs::kExtensionInstallAllowList, callback);
359 pref_change_registrar_.Add(prefs::kExtensionInstallDenyList, callback);
[email protected]e410b5f2012-12-14 14:02:24360 pref_change_registrar_.Add(prefs::kExtensionAllowedTypes, callback);
[email protected]4814b512009-11-07 00:12:29361
[email protected]93fd78f42009-07-10 16:43:17362 // Set up the ExtensionUpdater
363 if (autoupdate_enabled) {
[email protected]501105b2013-09-26 05:42:02364 int update_frequency = extensions::kDefaultUpdateFrequencySeconds;
[email protected]93fd78f42009-07-10 16:43:17365 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25366 base::StringToInt(command_line->GetSwitchValueASCII(
367 switches::kExtensionsUpdateFrequency),
368 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17369 }
[email protected]42a08162012-03-16 18:09:11370 updater_.reset(new extensions::ExtensionUpdater(this,
371 extension_prefs,
372 profile->GetPrefs(),
373 profile,
374 update_frequency));
[email protected]93fd78f42009-07-10 16:43:17375 }
376
[email protected]25ae0152011-11-18 14:40:02377 component_loader_.reset(
378 new extensions::ComponentLoader(this,
379 profile->GetPrefs(),
380 g_browser_process->local_state()));
[email protected]8e4560b62011-01-14 10:09:14381
[email protected]0436b102011-04-15 18:30:03382 if (extensions_enabled_) {
[email protected]ae4c37e2012-12-21 01:16:25383 extensions::ExternalProviderImpl::CreateExternalProviders(
384 this, profile_, &external_extension_providers_);
[email protected]873531342011-03-09 12:16:05385 }
[email protected]b671760b2010-07-15 21:13:47386
[email protected]74474042013-11-21 12:03:54387 // Set this as the ExtensionService for app sorting to ensure it causes syncs
388 // if required.
[email protected]b3aa7182013-04-25 04:45:23389 is_first_run_ = !extension_prefs_->SetAlertSystemFirstRun();
390
[email protected]9ce11d22012-08-08 23:20:13391#if defined(ENABLE_EXTENSIONS)
[email protected]c77f2352012-08-08 22:07:58392 extension_action_storage_manager_.reset(
393 new extensions::ExtensionActionStorageManager(profile_));
[email protected]9ce11d22012-08-08 23:20:13394#endif
[email protected]c77f2352012-08-08 22:07:58395
[email protected]4c9201c42013-08-16 04:56:21396 shared_module_policy_provider_.reset(new SharedModuleProvider);
397
[email protected]cb0e50312011-05-09 15:03:07398 // How long is the path to the Extensions directory?
399 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ExtensionRootPathLength",
400 install_directory_.value().length(), 0, 500, 100);
[email protected]6014d672008-12-05 00:38:25401}
402
[email protected]84df8332011-12-06 18:22:46403const ExtensionSet* ExtensionService::extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45404 return &extensions_;
405}
406
[email protected]84df8332011-12-06 18:22:46407const ExtensionSet* ExtensionService::disabled_extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45408 return &disabled_extensions_;
409}
410
[email protected]84df8332011-12-06 18:22:46411const ExtensionSet* ExtensionService::terminated_extensions() const {
[email protected]bb7f40952011-01-13 00:21:20412 return &terminated_extensions_;
413}
414
[email protected]695b5712012-12-06 23:55:28415const ExtensionSet* ExtensionService::blacklisted_extensions() const {
416 return &blacklisted_extensions_;
417}
418
[email protected]9e9c1d12013-07-31 01:58:12419const ExtensionSet* ExtensionService::delayed_installs() const {
420 return &delayed_installs_;
421}
422
[email protected]620b5a62013-11-18 04:21:08423scoped_ptr<ExtensionSet>
[email protected]695b5712012-12-06 23:55:28424 ExtensionService::GenerateInstalledExtensionsSet() const {
425 scoped_ptr<ExtensionSet> installed_extensions(new ExtensionSet());
[email protected]7f4308d2012-01-18 07:43:01426 installed_extensions->InsertAll(extensions_);
427 installed_extensions->InsertAll(disabled_extensions_);
428 installed_extensions->InsertAll(terminated_extensions_);
[email protected]695b5712012-12-06 23:55:28429 installed_extensions->InsertAll(blacklisted_extensions_);
[email protected]620b5a62013-11-18 04:21:08430 return installed_extensions.PassAs<ExtensionSet>();
[email protected]7f4308d2012-01-18 07:43:01431}
432
[email protected]3f213ad2012-07-26 23:39:41433extensions::PendingExtensionManager*
434 ExtensionService::pending_extension_manager() {
[email protected]b2907fd2011-03-25 16:43:37435 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45436}
437
[email protected]eaa7dd182010-12-14 11:09:00438ExtensionService::~ExtensionService() {
[email protected]c8d407e2011-04-28 21:27:17439 // No need to unload extensions here because they are profile-scoped, and the
440 // profile is in the process of being deleted.
[email protected]94b4ffc2011-02-04 05:55:17441
[email protected]5df038b2012-07-16 19:03:27442 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:14443 for (i = external_extension_providers_.begin();
444 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:27445 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:14446 provider->ServiceShutdown();
447 }
[email protected]6014d672008-12-05 00:38:25448}
449
[email protected]037228a2012-10-05 01:36:16450void ExtensionService::Shutdown() {
[email protected]4c9201c42013-08-16 04:56:21451 system_->management_policy()->UnregisterProvider(
452 shared_module_policy_provider_.get());
[email protected]037228a2012-10-05 01:36:16453}
454
[email protected]b2907fd2011-03-25 16:43:37455const Extension* ExtensionService::GetExtensionById(
456 const std::string& id, bool include_disabled) const {
[email protected]81f0d162012-08-15 04:21:47457 int include_mask = INCLUDE_ENABLED;
[email protected]695b5712012-12-06 23:55:28458 if (include_disabled) {
459 // Include blacklisted extensions here because there are hundreds of
460 // callers of this function, and many might assume that this includes those
461 // that have been disabled due to blacklisting.
[email protected]298cacd2013-11-19 01:47:58462 include_mask |= INCLUDE_DISABLED | INCLUDE_BLACKLISTED | INCLUDE_TERMINATED;
[email protected]695b5712012-12-06 23:55:28463 }
464 return GetExtensionById(id, include_mask);
465}
466
[email protected]399583b2012-12-11 09:33:42467GURL ExtensionService::GetSiteForExtensionId(const std::string& extension_id) {
468 return content::SiteInstance::GetSiteForURL(
469 profile_,
470 Extension::GetBaseURLFromExtensionId(extension_id));
471}
472
[email protected]695b5712012-12-06 23:55:28473const Extension* ExtensionService::GetExtensionById(
474 const std::string& id, int include_mask) const {
475 std::string lowercase_id = StringToLowerASCII(id);
476 if (include_mask & INCLUDE_ENABLED) {
477 const Extension* extension = extensions_.GetByID(lowercase_id);
478 if (extension)
479 return extension;
480 }
481 if (include_mask & INCLUDE_DISABLED) {
482 const Extension* extension = disabled_extensions_.GetByID(lowercase_id);
483 if (extension)
484 return extension;
485 }
486 if (include_mask & INCLUDE_TERMINATED) {
487 const Extension* extension = terminated_extensions_.GetByID(lowercase_id);
488 if (extension)
489 return extension;
490 }
491 if (include_mask & INCLUDE_BLACKLISTED) {
492 const Extension* extension = blacklisted_extensions_.GetByID(lowercase_id);
493 if (extension)
494 return extension;
495 }
496 return NULL;
[email protected]78994ab02010-12-08 18:06:44497}
498
[email protected]eaa7dd182010-12-14 11:09:00499void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12500 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20501
[email protected]3350d712013-11-18 09:32:24502 base::Time begin_time = base::Time::Now();
503
[email protected]4a10006a2013-05-17 23:18:35504 DCHECK(!is_ready()); // Can't redo init.
[email protected]93fd78f42009-07-10 16:43:17505 DCHECK_EQ(extensions_.size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32506
[email protected]820d9bd2013-04-03 03:46:03507 const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
[email protected]820d9bd2013-04-03 03:46:03508 if (cmd_line->HasSwitch(switches::kInstallFromWebstore) ||
509 cmd_line->HasSwitch(switches::kLimitedInstallFromWebstore)) {
510 // The sole purpose of this launch is to install a new extension from CWS
511 // and immediately terminate: loading already installed extensions is
512 // unnecessary and may interfere with the inline install dialog (e.g. if an
513 // extension listens to onStartup and opens a window).
514 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25515 } else {
[email protected]47b60072013-06-20 10:35:56516 // LoadAllExtensions() calls OnLoadedInstalledExtensions().
517 component_loader_->LoadAll();
518 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]9f4e4f082013-06-21 07:11:19519
[email protected]59ee99d2013-10-11 15:46:16520 ReconcileKnownDisabled();
521
[email protected]2edd57852013-08-14 22:07:40522 // Attempt to re-enable extensions whose only disable reason is reloading.
523 std::vector<std::string> extensions_to_enable;
524 for (ExtensionSet::const_iterator iter = disabled_extensions_.begin();
525 iter != disabled_extensions_.end(); ++iter) {
526 const Extension* e = iter->get();
527 if (extension_prefs_->GetDisableReasons(e->id()) ==
528 Extension::DISABLE_RELOAD) {
529 extensions_to_enable.push_back(e->id());
530 }
531 }
532 for (std::vector<std::string>::iterator it = extensions_to_enable.begin();
533 it != extensions_to_enable.end(); ++it) {
534 EnableExtension(*it);
535 }
536
[email protected]9f4e4f082013-06-21 07:11:19537 // Finish install (if possible) of extensions that were still delayed while
538 // the browser was shut down.
539 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> delayed_info(
540 extension_prefs_->GetAllDelayedInstallInfo());
541 for (size_t i = 0; i < delayed_info->size(); ++i) {
542 ExtensionInfo* info = delayed_info->at(i).get();
543 scoped_refptr<const Extension> extension(NULL);
544 if (info->extension_manifest) {
545 std::string error;
546 extension = Extension::Create(
547 info->extension_path,
548 info->extension_location,
549 *info->extension_manifest,
550 extension_prefs_->GetDelayedInstallCreationFlags(
551 info->extension_id),
552 info->extension_id,
553 &error);
554 if (extension.get())
555 delayed_installs_.Insert(extension);
556 }
557 }
558 MaybeFinishDelayedInstallations();
559
560 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> delayed_info2(
561 extension_prefs_->GetAllDelayedInstallInfo());
562 UMA_HISTOGRAM_COUNTS_100("Extensions.UpdateOnLoad",
563 delayed_info2->size() - delayed_info->size());
564
[email protected]47b60072013-06-20 10:35:56565 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25566
[email protected]47b60072013-06-20 10:35:56567 // TODO(erikkay) this should probably be deferred to a future point
568 // rather than running immediately at startup.
569 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57570
[email protected]47b60072013-06-20 10:35:56571 base::MessageLoop::current()->PostDelayedTask(
572 FROM_HERE,
573 base::Bind(&ExtensionService::GarbageCollectExtensions, AsWeakPtr()),
574 base::TimeDelta::FromSeconds(kGarbageCollectStartupDelay));
[email protected]215a7be2012-10-22 19:53:42575
[email protected]820d9bd2013-04-03 03:46:03576 if (extension_prefs_->NeedsStorageGarbageCollection()) {
577 GarbageCollectIsolatedStorage();
578 extension_prefs_->SetNeedsStorageGarbageCollection(false);
579 }
[email protected]4c9201c42013-08-16 04:56:21580 system_->management_policy()->RegisterProvider(
581 shared_module_policy_provider_.get());
[email protected]399583b2012-12-11 09:33:42582 }
[email protected]3350d712013-11-18 09:32:24583
584 UMA_HISTOGRAM_TIMES("Extensions.ExtensionServiceInitTime",
585 base::Time::Now() - begin_time);
[email protected]6014d672008-12-05 00:38:25586}
587
[email protected]31bb5ee62012-09-12 22:58:40588bool ExtensionService::UpdateExtension(const std::string& id,
[email protected]650b2d52013-02-10 03:41:45589 const base::FilePath& extension_path,
[email protected]31bb5ee62012-09-12 22:58:40590 const GURL& download_url,
591 CrxInstaller** out_crx_installer) {
[email protected]a29a517a2011-01-21 21:11:12592 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]3c4abc82012-10-22 22:25:54593 if (browser_terminating_) {
594 LOG(WARNING) << "Skipping UpdateExtension due to browser shutdown";
595 // Leak the temp file at extension_path. We don't want to add to the disk
596 // I/O burden at shutdown, we can't rely on the I/O completing anyway, and
597 // the file is in the OS temp directory which should be cleaned up for us.
598 return false;
599 }
[email protected]a8af9fdb2010-10-28 21:52:20600
[email protected]3f213ad2012-07-26 23:39:41601 const extensions::PendingExtensionInfo* pending_extension_info =
[email protected]51a3bf8b2012-06-08 22:53:06602 pending_extension_manager()->GetById(id);
[email protected]8ef78fd2010-08-19 17:14:32603
[email protected]695b5712012-12-06 23:55:28604 const Extension* extension = GetInstalledExtension(id);
[email protected]51a3bf8b2012-06-08 22:53:06605 if (!pending_extension_info && !extension) {
[email protected]aa142702010-03-26 01:26:33606 LOG(WARNING) << "Will not update extension " << id
607 << " because it is not installed or pending";
608 // Delete extension_path since we're not creating a CrxInstaller
609 // that would do it for us.
[email protected]7f8f24f2012-11-15 19:40:14610 if (!GetFileTaskRunner()->PostTask(
611 FROM_HERE,
[email protected]53612e82011-10-18 18:00:36612 base::Bind(
[email protected]7296f2762011-11-21 19:23:44613 &extension_file_util::DeleteFile, extension_path, false)))
[email protected]14908b72011-04-20 06:54:36614 NOTREACHED();
[email protected]420a0ec2011-06-01 01:07:03615
616 return false;
[email protected]e957fe52009-06-23 16:51:05617 }
618
[email protected]aa142702010-03-26 01:26:33619 // We want a silent install only for non-pending extensions and
620 // pending extensions that have install_silently set.
[email protected]f8636f92013-08-09 21:02:37621 scoped_ptr<ExtensionInstallPrompt> client;
[email protected]91e51d612012-10-21 23:03:05622 if (pending_extension_info && !pending_extension_info->install_silently())
[email protected]f8636f92013-08-09 21:02:37623 client.reset(ExtensionInstallUI::CreateInstallPromptWithProfile(profile_));
[email protected]aa142702010-03-26 01:26:33624
[email protected]f8636f92013-08-09 21:02:37625 scoped_refptr<CrxInstaller> installer(
626 CrxInstaller::Create(this, client.Pass()));
[email protected]6dfbbf82010-03-12 23:09:16627 installer->set_expected_id(id);
[email protected]464213a2013-10-15 01:06:48628 int creation_flags = Extension::NO_FLAGS;
[email protected]51a3bf8b2012-06-08 22:53:06629 if (pending_extension_info) {
630 installer->set_install_source(pending_extension_info->install_source());
631 if (pending_extension_info->install_silently())
632 installer->set_allow_silent_install(true);
[email protected]464213a2013-10-15 01:06:48633 creation_flags = pending_extension_info->creation_flags();
634 if (pending_extension_info->mark_acknowledged())
635 AcknowledgeExternalExtension(id);
[email protected]51a3bf8b2012-06-08 22:53:06636 } else if (extension) {
[email protected]5eb375e92010-11-26 07:50:41637 installer->set_install_source(extension->location());
[email protected]51a3bf8b2012-06-08 22:53:06638 }
[email protected]7d8b7072012-04-07 01:07:46639 // If the extension was installed from or has migrated to the webstore, or
[email protected]fcb2c2c2012-10-17 21:08:45640 // its auto-update URL is from the webstore, treat it as a webstore install.
641 // Note that we ignore some older extensions with blank auto-update URLs
642 // because we are mostly concerned with restrictions on NaCl extensions,
643 // which are newer.
[email protected]75764512011-12-19 19:54:28644 if ((extension && extension->from_webstore()) ||
[email protected]4a5fe3e22013-06-04 07:06:38645 (extension && extensions::ManifestURL::UpdatesFromGallery(extension)) ||
[email protected]963d13c2012-09-29 17:13:35646 (!extension && extension_urls::IsWebstoreUpdateUrl(
[email protected]51a3bf8b2012-06-08 22:53:06647 pending_extension_info->update_url()))) {
[email protected]a12ce8b22012-01-17 18:40:53648 creation_flags |= Extension::FROM_WEBSTORE;
649 }
650
651 // Bookmark apps being updated is kind of a contradiction, but that's because
652 // we mark the default apps as bookmark apps, and they're hosted in the web
653 // store, thus they can get updated. See http://crbug.com/101605 for more
654 // details.
655 if (extension && extension->from_bookmark())
656 creation_flags |= Extension::FROM_BOOKMARK;
657
[email protected]e33bbc22012-08-27 22:05:46658 if (extension && extension->was_installed_by_default())
659 creation_flags |= Extension::WAS_INSTALLED_BY_DEFAULT;
660
[email protected]a12ce8b22012-01-17 18:40:53661 installer->set_creation_flags(creation_flags);
662
[email protected]6dfbbf82010-03-12 23:09:16663 installer->set_delete_source(true);
[email protected]655b2b1a2011-10-13 17:13:06664 installer->set_download_url(download_url);
[email protected]cb0e50312011-05-09 15:03:07665 installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
[email protected]6dfbbf82010-03-12 23:09:16666 installer->InstallCrx(extension_path);
[email protected]420a0ec2011-06-01 01:07:03667
668 if (out_crx_installer)
[email protected]dc24976f2013-06-02 21:15:09669 *out_crx_installer = installer.get();
[email protected]420a0ec2011-06-01 01:07:03670
671 return true;
[email protected]e957fe52009-06-23 16:51:05672}
673
[email protected]e178ad92013-06-28 02:29:25674void ExtensionService::ReloadExtension(const std::string extension_id) {
[email protected]a29a517a2011-01-21 21:11:12675 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]2a947202013-03-06 04:58:05676
677 // If the extension is already reloading, don't reload again.
678 if (extension_prefs_->GetDisableReasons(extension_id) &
679 Extension::DISABLE_RELOAD) {
680 return;
681 }
682
[email protected]650b2d52013-02-10 03:41:45683 base::FilePath path;
[email protected]9adb9692010-10-29 23:14:02684 const Extension* current_extension = GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40685
[email protected]f17dbd42010-08-16 23:21:10686 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]b65272f2009-08-31 15:47:06687 if (current_extension) {
[email protected]4814b512009-11-07 00:12:29688 // If the extension has an inspector open for its background page, detach
689 // the inspector and hang onto a cookie for it, so that we can reattach
690 // later.
[email protected]31d8f5f22012-04-02 15:22:08691 // TODO(yoz): this is not incognito-safe!
[email protected]98b6d942013-11-10 00:34:07692 extensions::ProcessManager* manager = system_->process_manager();
[email protected]3a1dc572012-07-31 22:25:13693 extensions::ExtensionHost* host =
694 manager->GetBackgroundHostForExtension(extension_id);
[email protected]98f66112012-12-25 12:59:36695 if (host && DevToolsAgentHost::HasFor(host->render_view_host())) {
[email protected]4814b512009-11-07 00:12:29696 // Look for an open inspector for the background page.
[email protected]04ea1bb2013-07-10 09:26:09697 scoped_refptr<DevToolsAgentHost> agent_host =
698 DevToolsAgentHost::GetOrCreateFor(host->render_view_host());
699 agent_host->DisconnectRenderViewHost();
700 orphaned_dev_tools_[extension_id] = agent_host;
[email protected]4814b512009-11-07 00:12:29701 }
702
[email protected]b65272f2009-08-31 15:47:06703 path = current_extension->path();
[email protected]58076192013-07-19 19:43:50704 // BeingUpgraded is set back to false when the extension is added.
705 SetBeingUpgraded(current_extension, true);
[email protected]44d62b62012-04-11 00:06:03706 DisableExtension(extension_id, Extension::DISABLE_RELOAD);
[email protected]b914e2952013-04-26 07:10:03707 reloading_extensions_.insert(extension_id);
[email protected]1eb175082010-02-10 09:26:16708 } else {
709 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06710 }
711
[email protected]9f4e4f082013-06-21 07:11:19712 if (delayed_installs_.Contains(extension_id)) {
[email protected]6f6101832012-11-27 22:10:48713 FinishDelayedInstallation(extension_id);
[email protected]0db124b02012-11-07 04:55:05714 return;
715 }
716
[email protected]43ceb002012-02-10 23:19:15717 // If we're reloading a component extension, use the component extension
718 // loader's reloader.
719 if (component_loader_->Exists(extension_id)) {
[email protected]e178ad92013-06-28 02:29:25720 SetBeingReloaded(extension_id, true);
[email protected]43ceb002012-02-10 23:19:15721 component_loader_->Reload(extension_id);
[email protected]e178ad92013-06-28 02:29:25722 SetBeingReloaded(extension_id, false);
[email protected]43ceb002012-02-10 23:19:15723 return;
724 }
725
[email protected]e6090e42010-03-23 22:44:08726 // Check the installed extensions to see if what we're reloading was already
727 // installed.
[email protected]e178ad92013-06-28 02:29:25728 SetBeingReloaded(extension_id, true);
[email protected]e6090e42010-03-23 22:44:08729 scoped_ptr<ExtensionInfo> installed_extension(
730 extension_prefs_->GetInstalledExtensionInfo(extension_id));
731 if (installed_extension.get() &&
732 installed_extension->extension_manifest.get()) {
[email protected]d8c8f25f2011-11-02 18:18:01733 extensions::InstalledLoader(this).Load(*installed_extension, false);
[email protected]e6090e42010-03-23 22:44:08734 } else {
[email protected]d8c8f25f2011-11-02 18:18:01735 // Otherwise, the extension is unpacked (location LOAD).
[email protected]e6090e42010-03-23 22:44:08736 // We should always be able to remember the extension's path. If it's not in
737 // the map, someone failed to update |unloaded_extension_paths_|.
738 CHECK(!path.empty());
[email protected]d8c8f25f2011-11-02 18:18:01739 extensions::UnpackedInstaller::Create(this)->Load(path);
[email protected]e6090e42010-03-23 22:44:08740 }
[email protected]e178ad92013-06-28 02:29:25741 // When reloading is done, mark this extension as done reloading.
742 SetBeingReloaded(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40743}
744
[email protected]fa2416f2011-05-03 08:41:20745bool ExtensionService::UninstallExtension(
[email protected]a12c706e2011-12-01 00:58:08746 std::string extension_id,
[email protected]fa2416f2011-05-03 08:41:20747 bool external_uninstall,
[email protected]65187152012-06-02 13:14:14748 string16* error) {
[email protected]a29a517a2011-01-21 21:11:12749 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20750
[email protected]0d6ec3a72011-09-02 02:09:43751 scoped_refptr<const Extension> extension(GetInstalledExtension(extension_id));
[email protected]631cf822009-05-15 07:01:25752
[email protected]e7afe2452010-08-22 16:19:13753 // Callers should not send us nonexistent extensions.
[email protected]dc24976f2013-06-02 21:15:09754 CHECK(extension.get());
[email protected]9f1087e2009-06-15 17:29:32755
[email protected]95da88c42011-03-31 10:07:33756 // Policy change which triggers an uninstall will always set
757 // |external_uninstall| to true so this is the only way to uninstall
758 // managed extensions.
[email protected]65187152012-06-02 13:14:14759 if (!external_uninstall &&
760 !system_->management_policy()->UserMayModifySettings(
761 extension.get(), error)) {
[email protected]ad50def52011-10-19 23:17:07762 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27763 chrome::NOTIFICATION_EXTENSION_UNINSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:53764 content::Source<Profile>(profile_),
[email protected]dc24976f2013-06-02 21:15:09765 content::Details<const Extension>(extension.get()));
[email protected]d6ebc9792011-04-07 18:18:33766 return false;
767 }
[email protected]95da88c42011-03-31 10:07:33768
[email protected]65f173552012-06-28 22:43:58769 syncer::SyncChange sync_change;
[email protected]f8aefb132013-10-30 09:29:52770 if (extension_sync_service_) {
771 sync_change = extension_sync_service_->PrepareToSyncUninstallExtension(
772 extension.get(), is_ready());
[email protected]3bdba0d2011-08-23 07:17:30773 }
774
[email protected]ffd2f79e2013-11-14 00:11:46775 extensions::ExtensionSystem::Get(profile_)->install_verifier()->Remove(
776 extension->id());
777
[email protected]dc24976f2013-06-02 21:15:09778 if (IsUnacknowledgedExternalExtension(extension.get())) {
[email protected]d96eb512012-11-01 23:44:08779 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
780 EXTERNAL_EXTENSION_UNINSTALLED,
781 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:02782 if (extensions::ManifestURL::UpdatesFromGallery(extension.get())) {
783 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventWebstore",
784 EXTERNAL_EXTENSION_UNINSTALLED,
785 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
786 } else {
787 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventNonWebstore",
788 EXTERNAL_EXTENSION_UNINSTALLED,
789 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
790 }
[email protected]d96eb512012-11-01 23:44:08791 }
[email protected]9b217652010-10-08 22:04:23792 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08793 extension->GetType(), 100);
[email protected]dc24976f2013-06-02 21:15:09794 RecordPermissionMessagesHistogram(extension.get(),
795 "Extensions.Permissions_Uninstall");
[email protected]9b217652010-10-08 22:04:23796
[email protected]831aa212010-03-26 13:55:19797 // Unload before doing more cleanup to ensure that nothing is hanging on to
798 // any of these resources.
[email protected]b0af4792013-10-23 09:12:13799 UnloadExtension(extension_id, UnloadedExtensionInfo::REASON_UNINSTALL);
[email protected]831aa212010-03-26 13:55:19800
[email protected]9f1087e2009-06-15 17:29:32801 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]12075d12013-02-27 05:38:05802 if (!Manifest::IsUnpackedLocation(extension->location())) {
[email protected]7f8f24f2012-11-15 19:40:14803 if (!GetFileTaskRunner()->PostTask(
804 FROM_HERE,
[email protected]53612e82011-10-18 18:00:36805 base::Bind(
[email protected]14908b72011-04-20 06:54:36806 &extension_file_util::UninstallExtension,
807 install_directory_,
[email protected]fa2416f2011-05-03 08:41:20808 extension_id)))
[email protected]14908b72011-04-20 06:54:36809 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32810 }
811
[email protected]6b414c232013-06-05 07:53:34812 GURL launch_web_url_origin(
[email protected]cadac622013-06-11 16:46:36813 extensions::AppLaunchInfo::GetLaunchWebURL(extension.get()).GetOrigin());
[email protected]561e33d52013-04-03 06:58:43814 bool is_storage_isolated =
[email protected]dc24976f2013-06-02 21:15:09815 extensions::AppIsolationInfo::HasIsolatedStorage(extension.get());
[email protected]0d6ec3a72011-09-02 02:09:43816
[email protected]399583b2012-12-11 09:33:42817 if (is_storage_isolated) {
818 BrowserContext::AsyncObliterateStoragePartition(
819 profile_,
820 GetSiteForExtensionId(extension_id),
821 base::Bind(&ExtensionService::OnNeedsToGarbageCollectIsolatedStorage,
822 AsWeakPtr()));
823 } else {
824 if (extension->is_hosted_app() &&
825 !profile_->GetExtensionSpecialStoragePolicy()->
826 IsStorageProtected(launch_web_url_origin)) {
827 extensions::DataDeleter::StartDeleting(
828 profile_, extension_id, launch_web_url_origin);
829 }
830 extensions::DataDeleter::StartDeleting(profile_, extension_id,
831 extension->url());
[email protected]0d6ec3a72011-09-02 02:09:43832 }
[email protected]0d6ec3a72011-09-02 02:09:43833
[email protected]0dfe05c2011-02-23 23:03:36834 UntrackTerminatedExtension(extension_id);
[email protected]211030342010-09-30 18:41:06835
836 // Notify interested parties that we've uninstalled this extension.
[email protected]ad50def52011-10-19 23:17:07837 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27838 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
[email protected]6c2381d2011-10-19 02:52:53839 content::Source<Profile>(profile_),
[email protected]dc24976f2013-06-02 21:15:09840 content::Details<const Extension>(extension.get()));
[email protected]d6ebc9792011-04-07 18:18:33841
[email protected]f8aefb132013-10-30 09:29:52842 if (extension_sync_service_) {
843 extension_sync_service_->ProcessSyncUninstallExtension(extension_id,
844 sync_change);
[email protected]3bdba0d2011-08-23 07:17:30845 }
846
[email protected]399583b2012-12-11 09:33:42847 delayed_installs_.Remove(extension_id);
[email protected]0db124b02012-11-07 04:55:05848
[email protected]198b5902013-06-27 10:36:11849 PruneSharedModulesOnUninstall(extension.get());
[email protected]9f4e4f082013-06-21 07:11:19850
[email protected]d4eda592013-09-18 03:37:57851 extension_prefs_->OnExtensionUninstalled(extension_id, extension->location(),
852 external_uninstall);
853
[email protected]333b1de2011-09-12 18:28:50854 // Track the uninstallation.
[email protected]49098f702011-10-13 03:47:18855 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionUninstalled", 1, 2);
856
[email protected]d6ebc9792011-04-07 18:18:33857 return true;
[email protected]c10da4b02010-03-25 14:38:32858}
859
[email protected]c3cfb012011-04-06 22:07:35860bool ExtensionService::IsExtensionEnabled(
861 const std::string& extension_id) const {
[email protected]84df8332011-12-06 18:22:46862 if (extensions_.Contains(extension_id) ||
[email protected]dc9a74f72012-08-17 18:07:21863 terminated_extensions_.Contains(extension_id)) {
[email protected]36429da2011-07-11 20:25:18864 return true;
[email protected]dc9a74f72012-08-17 18:07:21865 }
[email protected]36429da2011-07-11 20:25:18866
[email protected]695b5712012-12-06 23:55:28867 if (disabled_extensions_.Contains(extension_id) ||
868 blacklisted_extensions_.Contains(extension_id)) {
[email protected]ad83ca242011-07-29 01:32:25869 return false;
[email protected]695b5712012-12-06 23:55:28870 }
[email protected]ad83ca242011-07-29 01:32:25871
872 // If the extension hasn't been loaded yet, check the prefs for it. Assume
873 // enabled unless otherwise noted.
874 return !extension_prefs_->IsExtensionDisabled(extension_id) &&
[email protected]f574c402012-12-04 23:20:31875 !extension_prefs_->IsExternalExtensionUninstalled(extension_id);
[email protected]c3cfb012011-04-06 22:07:35876}
877
878bool ExtensionService::IsExternalExtensionUninstalled(
879 const std::string& extension_id) const {
880 return extension_prefs_->IsExternalExtensionUninstalled(extension_id);
881}
882
[email protected]eaa7dd182010-12-14 11:09:00883void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12884 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20885
[email protected]06f92562011-04-29 19:27:31886 if (IsExtensionEnabled(extension_id))
[email protected]0c6da502009-08-14 22:32:39887 return;
[email protected]0d54b682013-11-05 14:15:36888 const Extension* extension = disabled_extensions_.GetByID(extension_id);
889
890 ManagementPolicy* policy = system_->management_policy();
891 if (extension && policy->MustRemainDisabled(extension, NULL, NULL)) {
892 UMA_HISTOGRAM_COUNTS_100("Extensions.EnableDeniedByPolicy", 1);
893 return;
894 }
[email protected]0c6da502009-08-14 22:32:39895
[email protected]b3317ad2011-04-28 23:46:00896 extension_prefs_->SetExtensionState(extension_id, Extension::ENABLED);
[email protected]eb5e4f92012-08-15 23:33:28897 extension_prefs_->ClearDisableReasons(extension_id);
[email protected]1784e83a2009-09-08 21:01:52898
[email protected]06f92562011-04-29 19:27:31899 // This can happen if sync enables an extension that is not
900 // installed yet.
901 if (!extension)
902 return;
903
[email protected]d96eb512012-11-01 23:44:08904 if (IsUnacknowledgedExternalExtension(extension)) {
905 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
906 EXTERNAL_EXTENSION_REENABLED,
907 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:02908 if (extensions::ManifestURL::UpdatesFromGallery(extension)) {
909 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventWebstore",
910 EXTERNAL_EXTENSION_REENABLED,
911 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
912 } else {
913 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventNonWebstore",
914 EXTERNAL_EXTENSION_REENABLED,
915 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
916 }
[email protected]612a1cb12012-10-17 13:18:03917 AcknowledgeExternalExtension(extension->id());
[email protected]d96eb512012-11-01 23:44:08918 }
[email protected]612a1cb12012-10-17 13:18:03919
[email protected]0c6da502009-08-14 22:32:39920 // Move it over to the enabled list.
[email protected]84df8332011-12-06 18:22:46921 extensions_.Insert(make_scoped_refptr(extension));
[email protected]195c1582013-10-29 18:42:23922 disabled_extensions_.Remove(extension->id());
[email protected]0c6da502009-08-14 22:32:39923
[email protected]62d30f42009-10-01 22:36:06924 NotifyExtensionLoaded(extension);
[email protected]3bdba0d2011-08-23 07:17:30925
[email protected]f74d7f32012-06-19 19:22:51926 // Notify listeners that the extension was enabled.
927 content::NotificationService::current()->Notify(
928 chrome::NOTIFICATION_EXTENSION_ENABLED,
929 content::Source<Profile>(profile_),
930 content::Details<const Extension>(extension));
931
[email protected]f8aefb132013-10-30 09:29:52932 if (extension_sync_service_)
933 extension_sync_service_->SyncEnableExtension(*extension);
[email protected]0c6da502009-08-14 22:32:39934}
935
[email protected]44d62b62012-04-11 00:06:03936void ExtensionService::DisableExtension(
937 const std::string& extension_id,
938 Extension::DisableReason disable_reason) {
[email protected]a29a517a2011-01-21 21:11:12939 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20940
[email protected]b2ba9962009-12-10 20:10:15941 // The extension may have been disabled already.
[email protected]06f92562011-04-29 19:27:31942 if (!IsExtensionEnabled(extension_id))
[email protected]1784e83a2009-09-08 21:01:52943 return;
[email protected]1784e83a2009-09-08 21:01:52944
[email protected]06f92562011-04-29 19:27:31945 const Extension* extension = GetInstalledExtension(extension_id);
946 // |extension| can be NULL if sync disables an extension that is not
947 // installed yet.
[email protected]65187152012-06-02 13:14:14948 if (extension &&
[email protected]505ccdc2013-06-20 20:15:35949 disable_reason != Extension::DISABLE_RELOAD &&
[email protected]65187152012-06-02 13:14:14950 !system_->management_policy()->UserMayModifySettings(extension, NULL)) {
[email protected]95da88c42011-03-31 10:07:33951 return;
[email protected]65187152012-06-02 13:14:14952 }
[email protected]95da88c42011-03-31 10:07:33953
[email protected]b3317ad2011-04-28 23:46:00954 extension_prefs_->SetExtensionState(extension_id, Extension::DISABLED);
[email protected]eb5e4f92012-08-15 23:33:28955 extension_prefs_->AddDisableReason(extension_id, disable_reason);
[email protected]1784e83a2009-09-08 21:01:52956
[email protected]695b5712012-12-06 23:55:28957 int include_mask = INCLUDE_EVERYTHING & ~INCLUDE_DISABLED;
958 extension = GetExtensionById(extension_id, include_mask);
[email protected]06f92562011-04-29 19:27:31959 if (!extension)
960 return;
961
[email protected]64742de32013-04-22 08:44:34962 // Reset the background_page_ready flag
963 if (extensions::BackgroundInfo::HasBackgroundPage(extension))
964 extension_runtime_data_[extension->id()].background_page_ready = false;
965
[email protected]5c094792012-09-07 19:44:53966 // Move it over to the disabled list. Don't send a second unload notification
967 // for terminated extensions being disabled.
[email protected]195c1582013-10-29 18:42:23968 disabled_extensions_.Insert(make_scoped_refptr(extension));
[email protected]5c094792012-09-07 19:44:53969 if (extensions_.Contains(extension->id())) {
[email protected]84df8332011-12-06 18:22:46970 extensions_.Remove(extension->id());
[email protected]b0af4792013-10-23 09:12:13971 NotifyExtensionUnloaded(extension, UnloadedExtensionInfo::REASON_DISABLE);
[email protected]5c094792012-09-07 19:44:53972 } else {
[email protected]84df8332011-12-06 18:22:46973 terminated_extensions_.Remove(extension->id());
[email protected]5c094792012-09-07 19:44:53974 }
[email protected]3bdba0d2011-08-23 07:17:30975
[email protected]f8aefb132013-10-30 09:29:52976 if (extension_sync_service_)
977 extension_sync_service_->SyncDisableExtension(*extension);
[email protected]1784e83a2009-09-08 21:01:52978}
979
[email protected]1abf05e2013-07-09 17:04:36980void ExtensionService::DisableUserExtensions(
981 const std::vector<std::string>& except_ids) {
[email protected]e516e4c2013-06-12 17:41:14982 extensions::ManagementPolicy* management_policy =
983 system_->management_policy();
984 extensions::ExtensionList to_disable;
985
986 for (ExtensionSet::const_iterator extension = extensions_.begin();
987 extension != extensions_.end(); ++extension) {
[email protected]198b5902013-06-27 10:36:11988 if (management_policy->UserMayModifySettings(extension->get(), NULL))
[email protected]e516e4c2013-06-12 17:41:14989 to_disable.push_back(*extension);
990 }
991 for (ExtensionSet::const_iterator extension = terminated_extensions_.begin();
992 extension != terminated_extensions_.end(); ++extension) {
[email protected]198b5902013-06-27 10:36:11993 if (management_policy->UserMayModifySettings(extension->get(), NULL))
[email protected]e516e4c2013-06-12 17:41:14994 to_disable.push_back(*extension);
995 }
996
997 for (extensions::ExtensionList::const_iterator extension = to_disable.begin();
998 extension != to_disable.end(); ++extension) {
[email protected]3082fe32013-08-06 11:12:38999 if ((*extension)->was_installed_by_default() &&
1000 extension_urls::IsWebstoreUpdateUrl(
1001 extensions::ManifestURL::GetUpdateURL(*extension)))
1002 continue;
[email protected]1abf05e2013-07-09 17:04:361003 const std::string& id = (*extension)->id();
1004 if (except_ids.end() == std::find(except_ids.begin(), except_ids.end(), id))
1005 DisableExtension(id, extensions::Extension::DISABLE_USER_ACTION);
[email protected]e516e4c2013-06-12 17:41:141006 }
1007}
1008
[email protected]eaa7dd182010-12-14 11:09:001009void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]009633c2013-03-07 22:08:281010 const Extension* extension) {
1011 GrantPermissions(extension);
[email protected]fe2dd7742011-04-19 22:52:491012 RecordPermissionMessagesHistogram(
1013 extension, "Extensions.Permissions_ReEnable");
[email protected]8d888c12010-11-30 00:00:251014 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
1015 EnableExtension(extension->id());
1016}
1017
[email protected]009633c2013-03-07 22:08:281018void ExtensionService::GrantPermissions(const Extension* extension) {
[email protected]be083862012-09-01 03:53:451019 CHECK(extension);
1020 extensions::PermissionsUpdater perms_updater(profile());
[email protected]009633c2013-03-07 22:08:281021 perms_updater.GrantActivePermissions(extension);
[email protected]be083862012-09-01 03:53:451022}
1023
[email protected]fe2dd7742011-04-19 22:52:491024// static
1025void ExtensionService::RecordPermissionMessagesHistogram(
[email protected]13c68b62013-05-17 11:29:051026 const Extension* extension, const char* histogram) {
[email protected]de415552013-01-23 04:12:171027 // Since this is called from multiple sources, and since the histogram macros
1028 // use statics, we need to manually lookup the histogram ourselves.
1029 base::HistogramBase* counter = base::LinearHistogram::FactoryGet(
[email protected]fe2dd7742011-04-19 22:52:491030 histogram,
1031 1,
[email protected]c2e66e12012-06-27 06:27:061032 PermissionMessage::kEnumBoundary,
1033 PermissionMessage::kEnumBoundary + 1,
[email protected]de415552013-01-23 04:12:171034 base::HistogramBase::kUmaTargetedHistogramFlag);
[email protected]fe2dd7742011-04-19 22:52:491035
[email protected]13c68b62013-05-17 11:29:051036 PermissionMessages permissions =
1037 extensions::PermissionsData::GetPermissionMessages(extension);
[email protected]fe2dd7742011-04-19 22:52:491038 if (permissions.empty()) {
[email protected]c2e66e12012-06-27 06:27:061039 counter->Add(PermissionMessage::kNone);
[email protected]fe2dd7742011-04-19 22:52:491040 } else {
[email protected]c2e66e12012-06-27 06:27:061041 for (PermissionMessages::iterator it = permissions.begin();
[email protected]0d3e4a22011-06-23 19:02:521042 it != permissions.end(); ++it)
1043 counter->Add(it->id());
[email protected]fe2dd7742011-04-19 22:52:491044 }
1045}
1046
[email protected]eaa7dd182010-12-14 11:09:001047void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]57a777f72010-03-31 01:09:421048 // The ChromeURLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:061049 // was loaded, otherwise a race can arise where a renderer that is created
1050 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:421051 // that the request context doesn't yet know about. The profile is responsible
1052 // for ensuring its URLRequestContexts appropriately discover the loaded
1053 // extension.
[email protected]31d8f5f22012-04-02 15:22:081054 system_->RegisterExtensionWithRequestContexts(extension);
[email protected]62d30f42009-10-01 22:36:061055
[email protected]d5949312012-12-03 22:13:301056 // Tell renderers about the new extension, unless it's a theme (renderers
1057 // don't need to know about themes).
[email protected]a58f599c2012-12-01 03:08:191058 if (!extension->is_theme()) {
[email protected]19647262011-12-16 09:57:491059 for (content::RenderProcessHost::iterator i(
1060 content::RenderProcessHost::AllHostsIterator());
1061 !i.IsAtEnd(); i.Advance()) {
1062 content::RenderProcessHost* host = i.GetCurrentValue();
1063 Profile* host_profile =
1064 Profile::FromBrowserContext(host->GetBrowserContext());
1065 if (host_profile->GetOriginalProfile() ==
1066 profile_->GetOriginalProfile()) {
1067 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions(
1068 1, ExtensionMsg_Loaded_Params(extension));
1069 host->Send(
1070 new ExtensionMsg_Loaded(loaded_extensions));
1071 }
[email protected]c8d407e2011-04-28 21:27:171072 }
[email protected]77a6970c2011-04-23 16:58:561073 }
1074
[email protected]3442a662012-01-12 08:06:171075 // Tell subsystems that use the EXTENSION_LOADED notification about the new
1076 // extension.
1077 //
1078 // NOTE: It is important that this happen after notifying the renderers about
1079 // the new extensions so that if we navigate to an extension URL in
1080 // NOTIFICATION_EXTENSION_LOADED, the renderer is guaranteed to know about it.
1081 content::NotificationService::current()->Notify(
1082 chrome::NOTIFICATION_EXTENSION_LOADED,
1083 content::Source<Profile>(profile_),
1084 content::Details<const Extension>(extension));
1085
[email protected]c8d407e2011-04-28 21:27:171086 // Tell a random-ass collection of other subsystems about the new extension.
1087 // TODO(aa): What should we do with all this goop? Can it move into the
1088 // relevant objects via EXTENSION_LOADED?
1089
1090 profile_->GetExtensionSpecialStoragePolicy()->
1091 GrantRightsForExtension(extension);
1092
1093 UpdateActiveExtensionsInCrashReporter();
1094
[email protected]c8d407e2011-04-28 21:27:171095 // If the extension has permission to load chrome://favicon/ resources we need
1096 // to make sure that the FaviconSource is registered with the
1097 // ChromeURLDataManager.
[email protected]13c68b62013-05-17 11:29:051098 if (extensions::PermissionsData::HasHostPermission(
1099 extension, GURL(chrome::kChromeUIFaviconURL))) {
[email protected]c8d407e2011-04-28 21:27:171100 FaviconSource* favicon_source = new FaviconSource(profile_,
1101 FaviconSource::FAVICON);
[email protected]24ea7a12013-01-27 23:54:531102 content::URLDataSource::Add(profile_, favicon_source);
[email protected]c8d407e2011-04-28 21:27:171103 }
[email protected]b07e606e2012-09-15 20:16:151104
1105#if !defined(OS_ANDROID)
1106 // Same for chrome://theme/ resources.
[email protected]13c68b62013-05-17 11:29:051107 if (extensions::PermissionsData::HasHostPermission(
1108 extension, GURL(chrome::kChromeUIThemeURL))) {
[email protected]b07e606e2012-09-15 20:16:151109 ThemeSource* theme_source = new ThemeSource(profile_);
[email protected]24ea7a12013-01-27 23:54:531110 content::URLDataSource::Add(profile_, theme_source);
[email protected]b07e606e2012-09-15 20:16:151111 }
1112#endif
1113
[email protected]5eddc3e2011-10-26 04:33:311114 // Same for chrome://thumb/ resources.
[email protected]13c68b62013-05-17 11:29:051115 if (extensions::PermissionsData::HasHostPermission(
1116 extension, GURL(chrome::kChromeUIThumbnailURL))) {
[email protected]420e6d92013-09-17 01:48:511117 ThumbnailSource* thumbnail_source = new ThumbnailSource(profile_, false);
[email protected]24ea7a12013-01-27 23:54:531118 content::URLDataSource::Add(profile_, thumbnail_source);
[email protected]5eddc3e2011-10-26 04:33:311119 }
[email protected]62d30f42009-10-01 22:36:061120}
1121
[email protected]a9f39a312010-12-23 22:14:271122void ExtensionService::NotifyExtensionUnloaded(
[email protected]814a7bf0f2011-08-13 05:30:591123 const Extension* extension,
[email protected]b0af4792013-10-23 09:12:131124 UnloadedExtensionInfo::Reason reason) {
[email protected]a9f39a312010-12-23 22:14:271125 UnloadedExtensionInfo details(extension, reason);
[email protected]ad50def52011-10-19 23:17:071126 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271127 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]6c2381d2011-10-19 02:52:531128 content::Source<Profile>(profile_),
1129 content::Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061130
[email protected]f3b1a082011-11-18 00:34:301131 for (content::RenderProcessHost::iterator i(
1132 content::RenderProcessHost::AllHostsIterator());
[email protected]77a6970c2011-04-23 16:58:561133 !i.IsAtEnd(); i.Advance()) {
[email protected]f3b1a082011-11-18 00:34:301134 content::RenderProcessHost* host = i.GetCurrentValue();
[email protected]9b62ecf2011-07-27 20:23:081135 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:301136 Profile::FromBrowserContext(host->GetBrowserContext());
[email protected]9b62ecf2011-07-27 20:23:081137 if (host_profile->GetOriginalProfile() == profile_->GetOriginalProfile())
[email protected]c8d407e2011-04-28 21:27:171138 host->Send(new ExtensionMsg_Unloaded(extension->id()));
[email protected]77a6970c2011-04-23 16:58:561139 }
1140
[email protected]31d8f5f22012-04-02 15:22:081141 system_->UnregisterExtensionWithRequestContexts(extension->id(), reason);
[email protected]c8d407e2011-04-28 21:27:171142 profile_->GetExtensionSpecialStoragePolicy()->
1143 RevokeRightsForExtension(extension);
1144
[email protected]b777b332011-04-16 04:01:081145#if defined(OS_CHROMEOS)
[email protected]ac66e452013-01-11 09:21:231146 // Revoke external file access for the extension from its file system context.
1147 // It is safe to access the extension's storage partition at this point. The
1148 // storage partition may get destroyed only after the extension gets unloaded.
1149 GURL site = extensions::ExtensionSystem::Get(profile_)->extension_service()->
1150 GetSiteForExtensionId(extension->id());
[email protected]10eb28162012-09-18 03:04:091151 fileapi::FileSystemContext* filesystem_context =
[email protected]ac66e452013-01-11 09:21:231152 BrowserContext::GetStoragePartitionForSite(profile_, site)->
[email protected]10eb28162012-09-18 03:04:091153 GetFileSystemContext();
[email protected]f19bbf62013-07-09 01:22:321154 if (filesystem_context && filesystem_context->external_backend()) {
1155 filesystem_context->external_backend()->
[email protected]c8d407e2011-04-28 21:27:171156 RevokeAccessForExtension(extension->id());
[email protected]62d30f42009-10-01 22:36:061157 }
[email protected]c8d407e2011-04-28 21:27:171158#endif
1159
1160 UpdateActiveExtensionsInCrashReporter();
[email protected]62d30f42009-10-01 22:36:061161}
1162
[email protected]0a071a32011-02-08 00:18:241163Profile* ExtensionService::profile() {
1164 return profile_;
1165}
1166
[email protected]45759612012-07-10 17:21:231167extensions::ExtensionPrefs* ExtensionService::extension_prefs() {
[email protected]ddb1e5a2010-12-13 20:10:451168 return extension_prefs_;
1169}
1170
[email protected]a7ff4b72013-10-17 20:56:021171const extensions::ExtensionPrefs* ExtensionService::extension_prefs() const {
1172 return extension_prefs_;
1173}
1174
[email protected]0d9a2202011-11-09 13:48:411175extensions::SettingsFrontend* ExtensionService::settings_frontend() {
[email protected]cccdf0aa2011-11-11 03:43:381176 return settings_frontend_.get();
[email protected]b7f853e282011-08-10 09:24:201177}
1178
[email protected]e2d720aa2012-05-17 00:11:351179extensions::ContentSettingsStore* ExtensionService::GetContentSettingsStore() {
[email protected]b790b072011-05-20 09:46:441180 return extension_prefs()->content_settings_store();
1181}
1182
[email protected]25ae0152011-11-18 14:40:021183bool ExtensionService::is_ready() {
[email protected]4a10006a2013-05-17 23:18:351184 return ready_->is_signaled();
[email protected]25ae0152011-11-18 14:40:021185}
1186
[email protected]7f8f24f2012-11-15 19:40:141187base::SequencedTaskRunner* ExtensionService::GetFileTaskRunner() {
[email protected]dc24976f2013-06-02 21:15:091188 if (file_task_runner_.get())
1189 return file_task_runner_.get();
[email protected]7f8f24f2012-11-15 19:40:141190
1191 // We should be able to interrupt any part of extension install process during
1192 // shutdown. SKIP_ON_SHUTDOWN ensures that not started extension install tasks
1193 // will be ignored/deleted while we will block on started tasks.
1194 std::string token("ext_install-");
1195 token.append(profile_->GetPath().AsUTF8Unsafe());
1196 file_task_runner_ = BrowserThread::GetBlockingPool()->
1197 GetSequencedTaskRunnerWithShutdownBehavior(
1198 BrowserThread::GetBlockingPool()->GetNamedSequenceToken(token),
1199 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
[email protected]dc24976f2013-06-02 21:15:091200 return file_task_runner_.get();
[email protected]7f8f24f2012-11-15 19:40:141201}
1202
[email protected]42a08162012-03-16 18:09:111203extensions::ExtensionUpdater* ExtensionService::updater() {
[email protected]2859946f2011-04-04 18:18:061204 return updater_.get();
1205}
1206
[email protected]4ee07c62012-08-21 12:40:421207void ExtensionService::CheckManagementPolicy() {
[email protected]0d54b682013-11-05 14:15:361208 std::vector<std::string> to_unload;
1209 std::map<std::string, Extension::DisableReason> to_disable;
[email protected]695b5712012-12-06 23:55:281210
[email protected]0d54b682013-11-05 14:15:361211 // Loop through the extensions list, finding extensions we need to unload or
1212 // disable.
[email protected]84df8332011-12-06 18:22:461213 for (ExtensionSet::const_iterator iter = extensions_.begin();
[email protected]aa96d3a2010-08-21 08:45:251214 iter != extensions_.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361215 const Extension* extension = (iter->get());
[email protected]4ee07c62012-08-21 12:40:421216 if (!system_->management_policy()->UserMayLoad(extension, NULL))
[email protected]0d54b682013-11-05 14:15:361217 to_unload.push_back(extension->id());
1218 Extension::DisableReason disable_reason = Extension::DISABLE_NONE;
1219 if (system_->management_policy()->MustRemainDisabled(
1220 extension, &disable_reason, NULL))
1221 to_disable[extension->id()] = disable_reason;
[email protected]aa96d3a2010-08-21 08:45:251222 }
1223
[email protected]0d54b682013-11-05 14:15:361224 for (size_t i = 0; i < to_unload.size(); ++i)
1225 UnloadExtension(to_unload[i], UnloadedExtensionInfo::REASON_DISABLE);
1226
1227 for (std::map<std::string, Extension::DisableReason>::const_iterator i =
1228 to_disable.begin(); i != to_disable.end(); ++i)
1229 DisableExtension(i->first, i->second);
[email protected]aa96d3a2010-08-21 08:45:251230}
1231
[email protected]31206602011-04-13 23:07:321232void ExtensionService::CheckForUpdatesSoon() {
[email protected]c3cfb012011-04-06 22:07:351233 if (updater()) {
[email protected]94fde232012-04-27 10:22:301234 if (AreAllExternalProvidersReady()) {
1235 updater()->CheckSoon();
1236 } else {
1237 // Sync can start updating before all the external providers are ready
1238 // during startup. Start the update as soon as those providers are ready,
1239 // but not before.
1240 update_once_all_providers_are_ready_ = true;
1241 }
[email protected]c3cfb012011-04-06 22:07:351242 } else {
[email protected]31206602011-04-13 23:07:321243 LOG(WARNING) << "CheckForUpdatesSoon() called with auto-update turned off";
[email protected]c3cfb012011-04-06 22:07:351244 }
1245}
1246
[email protected]168389f2011-12-20 17:12:481247void ExtensionService::OnExtensionMoved(
1248 const std::string& moved_extension_id,
1249 const std::string& predecessor_extension_id,
1250 const std::string& successor_extension_id) {
[email protected]74474042013-11-21 12:03:541251 extension_prefs_->app_sorting()->OnExtensionMoved(
[email protected]3569b502012-01-12 20:08:231252 moved_extension_id,
1253 predecessor_extension_id,
1254 successor_extension_id);
[email protected]168389f2011-12-20 17:12:481255
1256 const Extension* extension = GetInstalledExtension(moved_extension_id);
[email protected]f8aefb132013-10-30 09:29:521257 if (extension_sync_service_ && extension) {
1258 extension_sync_service_->SyncExtensionChangeIfNeeded(*extension);
1259 }
[email protected]168389f2011-12-20 17:12:481260}
1261
[email protected]8e4560b62011-01-14 10:09:141262// Some extensions will autoupdate themselves externally from Chrome. These
1263// are typically part of some larger client application package. To support
1264// these, the extension will register its location in the the preferences file
1265// (and also, on Windows, in the registry) and this code will periodically
1266// check that location for a .crx file, which it will then install locally if
1267// a new version is available.
1268// Errors are reported through ExtensionErrorReporter. Succcess is not
1269// reported.
[email protected]eaa7dd182010-12-14 11:09:001270void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:121271 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]8e4560b62011-01-14 10:09:141272
1273 // Note that this installation is intentionally silent (since it didn't
1274 // go through the front-end). Extensions that are registered in this
1275 // way are effectively considered 'pre-bundled', and so implicitly
1276 // trusted. In general, if something has HKLM or filesystem access,
1277 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:121278
[email protected]8e4560b62011-01-14 10:09:141279 // Ask each external extension provider to give us a call back for each
1280 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
[email protected]5df038b2012-07-16 19:03:271281 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141282 for (i = external_extension_providers_.begin();
1283 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:271284 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:141285 provider->VisitRegisteredExtension();
1286 }
1287
[email protected]50067e52011-10-20 23:17:071288 // Do any required work that we would have done after completion of all
1289 // providers.
1290 if (external_extension_providers_.empty()) {
1291 OnAllExternalProvidersReady();
1292 }
[email protected]9f1087e2009-06-15 17:29:321293}
1294
[email protected]50067e52011-10-20 23:17:071295void ExtensionService::OnExternalProviderReady(
[email protected]5df038b2012-07-16 19:03:271296 const extensions::ExternalProviderInterface* provider) {
[email protected]a29a517a2011-01-21 21:11:121297 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]50067e52011-10-20 23:17:071298 CHECK(provider->IsReady());
[email protected]a29a517a2011-01-21 21:11:121299
1300 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:141301 // if all of them are finished. So we check them first.
[email protected]94fde232012-04-27 10:22:301302 if (AreAllExternalProvidersReady())
1303 OnAllExternalProvidersReady();
1304}
1305
1306bool ExtensionService::AreAllExternalProvidersReady() const {
[email protected]5df038b2012-07-16 19:03:271307 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141308 for (i = external_extension_providers_.begin();
1309 i != external_extension_providers_.end(); ++i) {
[email protected]94fde232012-04-27 10:22:301310 if (!i->get()->IsReady())
1311 return false;
[email protected]8e4560b62011-01-14 10:09:141312 }
[email protected]94fde232012-04-27 10:22:301313 return true;
[email protected]50067e52011-10-20 23:17:071314}
1315
1316void ExtensionService::OnAllExternalProvidersReady() {
1317 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]94fde232012-04-27 10:22:301318 base::TimeDelta elapsed = base::Time::Now() - profile_->GetStartTime();
1319 UMA_HISTOGRAM_TIMES("Extension.ExternalProvidersReadyAfter", elapsed);
[email protected]50067e52011-10-20 23:17:071320
1321 // Install any pending extensions.
[email protected]94fde232012-04-27 10:22:301322 if (update_once_all_providers_are_ready_ && updater()) {
1323 update_once_all_providers_are_ready_ = false;
[email protected]42774e42012-11-14 21:01:061324 updater()->CheckNow(extensions::ExtensionUpdater::CheckParams());
[email protected]8e4560b62011-01-14 10:09:141325 }
1326
1327 // Uninstall all the unclaimed extensions.
[email protected]45759612012-07-10 17:21:231328 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> extensions_info(
[email protected]8e4560b62011-01-14 10:09:141329 extension_prefs_->GetInstalledExtensionsInfo());
1330 for (size_t i = 0; i < extensions_info->size(); ++i) {
1331 ExtensionInfo* info = extensions_info->at(i).get();
[email protected]1d5e58b2013-01-31 08:41:401332 if (Manifest::IsExternalLocation(info->extension_location))
[email protected]8e4560b62011-01-14 10:09:141333 CheckExternalUninstall(info->extension_id);
1334 }
[email protected]e5af875f2011-10-10 21:09:141335 IdentifyAlertableExtensions();
[email protected]e5af875f2011-10-10 21:09:141336}
1337
1338void ExtensionService::IdentifyAlertableExtensions() {
1339 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1340
[email protected]07c9f2f42012-02-29 18:45:221341 // Build up the lists of extensions that require acknowledgment. If this is
1342 // the first time, grandfather extensions that would have caused
1343 // notification.
[email protected]89226982012-07-16 20:09:181344 extension_error_ui_.reset(ExtensionErrorUI::Create(this));
[email protected]e5af875f2011-10-10 21:09:141345
[email protected]a9aa5932012-01-25 08:27:401346 bool did_show_alert = false;
[email protected]89226982012-07-16 20:09:181347 if (PopulateExtensionErrorUI(extension_error_ui_.get())) {
[email protected]460c6712013-04-24 07:20:011348 if (!is_first_run_) {
[email protected]a9aa5932012-01-25 08:27:401349 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]89226982012-07-16 20:09:181350 did_show_alert = extension_error_ui_->ShowErrorInBubbleView();
[email protected]e5af875f2011-10-10 21:09:141351 } else {
1352 // First run. Just acknowledge all the extensions, silently, by
1353 // shortcutting the display of the UI and going straight to the
1354 // callback for pressing the Accept button.
[email protected]a9aa5932012-01-25 08:27:401355 HandleExtensionAlertAccept();
[email protected]e5af875f2011-10-10 21:09:141356 }
1357 }
[email protected]a9aa5932012-01-25 08:27:401358
[email protected]612a1cb12012-10-17 13:18:031359 UpdateExternalExtensionAlert();
1360
[email protected]a9aa5932012-01-25 08:27:401361 if (!did_show_alert)
[email protected]89226982012-07-16 20:09:181362 extension_error_ui_.reset();
[email protected]e5af875f2011-10-10 21:09:141363}
1364
[email protected]89226982012-07-16 20:09:181365bool ExtensionService::PopulateExtensionErrorUI(
1366 ExtensionErrorUI* extension_error_ui) {
[email protected]07c9f2f42012-02-29 18:45:221367 bool needs_alert = false;
[email protected]695b5712012-12-06 23:55:281368
1369 // Extensions that are blacklisted.
1370 for (ExtensionSet::const_iterator it = blacklisted_extensions_.begin();
1371 it != blacklisted_extensions_.end(); ++it) {
1372 std::string id = (*it)->id();
1373 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(id)) {
1374 extension_error_ui->AddBlacklistedExtension(id);
1375 needs_alert = true;
1376 }
1377 }
1378
[email protected]07c9f2f42012-02-29 18:45:221379 for (ExtensionSet::const_iterator iter = extensions_.begin();
1380 iter != extensions_.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361381 const Extension* e = iter->get();
[email protected]695b5712012-12-06 23:55:281382
1383 // Extensions disabled by policy. Note: this no longer includes blacklisted
1384 // extensions, though we still show the same UI.
[email protected]fdd679b2012-11-15 20:49:391385 if (!system_->management_policy()->UserMayLoad(e, NULL)) {
[email protected]07c9f2f42012-02-29 18:45:221386 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(e->id())) {
[email protected]89226982012-07-16 20:09:181387 extension_error_ui->AddBlacklistedExtension(e->id());
[email protected]07c9f2f42012-02-29 18:45:221388 needs_alert = true;
1389 }
1390 }
[email protected]07c9f2f42012-02-29 18:45:221391 }
[email protected]695b5712012-12-06 23:55:281392
[email protected]07c9f2f42012-02-29 18:45:221393 return needs_alert;
1394}
1395
[email protected]a9aa5932012-01-25 08:27:401396void ExtensionService::HandleExtensionAlertClosed() {
[email protected]612a1cb12012-10-17 13:18:031397 const ExtensionIdSet* extension_ids =
1398 extension_error_ui_->get_blacklisted_extension_ids();
[email protected]e5af875f2011-10-10 21:09:141399 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1400 iter != extension_ids->end(); ++iter) {
1401 extension_prefs_->AcknowledgeBlacklistedExtension(*iter);
1402 }
[email protected]879a6092013-03-06 04:12:501403 extension_error_ui_.reset();
1404}
1405
1406void ExtensionService::HandleExtensionAlertAccept() {
[email protected]73a85e012013-04-04 10:45:301407 extension_error_ui_->Close();
[email protected]e5af875f2011-10-10 21:09:141408}
1409
[email protected]47fc70c2011-12-06 07:29:511410void ExtensionService::AcknowledgeExternalExtension(const std::string& id) {
1411 extension_prefs_->AcknowledgeExternalExtension(id);
[email protected]612a1cb12012-10-17 13:18:031412 UpdateExternalExtensionAlert();
[email protected]47fc70c2011-12-06 07:29:511413}
1414
[email protected]d96eb512012-11-01 23:44:081415bool ExtensionService::IsUnacknowledgedExternalExtension(
1416 const Extension* extension) {
[email protected]b82d18da2012-11-09 00:11:321417 if (!FeatureSwitch::prompt_for_external_extensions()->IsEnabled())
1418 return false;
1419
[email protected]1d5e58b2013-01-31 08:41:401420 return (Manifest::IsExternalLocation(extension->location()) &&
[email protected]d96eb512012-11-01 23:44:081421 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id()) &&
1422 !(extension_prefs_->GetDisableReasons(extension->id()) &
1423 Extension::DISABLE_SIDELOAD_WIPEOUT));
1424}
1425
[email protected]59ee99d2013-10-11 15:46:161426void ExtensionService::ReconcileKnownDisabled() {
[email protected]60a8bc962013-12-03 03:17:271427 ExtensionIdSet known_disabled_ids;
1428 if (!extension_prefs_->GetKnownDisabled(&known_disabled_ids)) {
[email protected]018a5682013-11-20 02:41:211429 extension_prefs_->SetKnownDisabled(disabled_extensions_.GetIDs());
1430 UMA_HISTOGRAM_BOOLEAN("Extensions.KnownDisabledInitialized", true);
1431 return;
[email protected]59ee99d2013-10-11 15:46:161432 }
[email protected]195c1582013-10-29 18:42:231433
[email protected]018a5682013-11-20 02:41:211434 // Both |known_disabled_ids| and |extensions_| are ordered (by definition
1435 // of std::map and std::set). Iterate forward over both sets in parallel
1436 // to find matching IDs and disable the corresponding extensions.
1437 ExtensionSet::const_iterator extensions_it = extensions_.begin();
1438 ExtensionIdSet::const_iterator known_disabled_ids_it =
1439 known_disabled_ids.begin();
1440 int known_disabled_count = 0;
1441 while (extensions_it != extensions_.end() &&
1442 known_disabled_ids_it != known_disabled_ids.end()) {
1443 const std::string& extension_id = extensions_it->get()->id();
1444 const int comparison = extension_id.compare(*known_disabled_ids_it);
1445 if (comparison < 0) {
1446 ++extensions_it;
1447 } else if (comparison > 0) {
1448 ++known_disabled_ids_it;
1449 } else {
1450 ++known_disabled_count;
1451 // Advance |extensions_it| immediately as it will be invalidated upon
1452 // disabling the extension it points to.
1453 ++extensions_it;
1454 ++known_disabled_ids_it;
1455 DisableExtension(extension_id, Extension::DISABLE_KNOWN_DISABLED);
1456 }
1457 }
1458 UMA_HISTOGRAM_COUNTS_100("Extensions.KnownDisabledReDisabled",
1459 known_disabled_count);
1460
[email protected]195c1582013-10-29 18:42:231461 // Update the list of known disabled to reflect every change to
1462 // |disabled_extensions_| from this point forward.
1463 disabled_extensions_.set_modification_callback(
1464 base::Bind(&extensions::ExtensionPrefs::SetKnownDisabled,
1465 base::Unretained(extension_prefs_)));
[email protected]59ee99d2013-10-11 15:46:161466}
1467
[email protected]89226982012-07-16 20:09:181468void ExtensionService::HandleExtensionAlertDetails() {
1469 extension_error_ui_->ShowExtensions();
[email protected]73a85e012013-04-04 10:45:301470 // ShowExtensions may cause the error UI to close synchronously, e.g. if it
1471 // causes a navigation.
1472 if (extension_error_ui_)
1473 extension_error_ui_->Close();
[email protected]6c751e72010-11-23 10:11:101474}
1475
[email protected]612a1cb12012-10-17 13:18:031476void ExtensionService::UpdateExternalExtensionAlert() {
[email protected]41070e8d2012-10-24 01:34:361477 if (!FeatureSwitch::prompt_for_external_extensions()->IsEnabled())
[email protected]612a1cb12012-10-17 13:18:031478 return;
1479
1480 const Extension* extension = NULL;
1481 for (ExtensionSet::const_iterator iter = disabled_extensions_.begin();
1482 iter != disabled_extensions_.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361483 const Extension* e = iter->get();
[email protected]d96eb512012-11-01 23:44:081484 if (IsUnacknowledgedExternalExtension(e)) {
1485 extension = e;
1486 break;
[email protected]612a1cb12012-10-17 13:18:031487 }
1488 }
1489
1490 if (extension) {
[email protected]d96eb512012-11-01 23:44:081491 if (!extensions::HasExternalInstallError(this)) {
1492 if (extension_prefs_->IncrementAcknowledgePromptCount(extension->id()) >
[email protected]612a1cb12012-10-17 13:18:031493 kMaxExtensionAcknowledgePromptCount) {
[email protected]d96eb512012-11-01 23:44:081494 // Stop prompting for this extension, and check if there's another
1495 // one that needs prompting.
[email protected]612a1cb12012-10-17 13:18:031496 extension_prefs_->AcknowledgeExternalExtension(extension->id());
[email protected]d96eb512012-11-01 23:44:081497 UpdateExternalExtensionAlert();
1498 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
1499 EXTERNAL_EXTENSION_IGNORED,
1500 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:021501 if (extensions::ManifestURL::UpdatesFromGallery(extension)) {
1502 UMA_HISTOGRAM_ENUMERATION(
1503 "Extensions.ExternalExtensionEventWebstore",
1504 EXTERNAL_EXTENSION_IGNORED,
1505 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
1506 } else {
1507 UMA_HISTOGRAM_ENUMERATION(
1508 "Extensions.ExternalExtensionEventNonWebstore",
1509 EXTERNAL_EXTENSION_IGNORED,
1510 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
1511 }
[email protected]d96eb512012-11-01 23:44:081512 return;
[email protected]612a1cb12012-10-17 13:18:031513 }
[email protected]b3aa7182013-04-25 04:45:231514 if (is_first_run_)
1515 extension_prefs_->SetExternalInstallFirstRun(extension->id());
1516 // first_run is true if the extension was installed during a first run
1517 // (even if it's post-first run now).
1518 bool first_run = extension_prefs_->IsExternalInstallFirstRun(
1519 extension->id());
1520 extensions::AddExternalInstallError(this, extension, first_run);
[email protected]612a1cb12012-10-17 13:18:031521 }
1522 } else {
1523 extensions::RemoveExternalInstallError(this);
1524 }
[email protected]612a1cb12012-10-17 13:18:031525}
1526
[email protected]a9f39a312010-12-23 22:14:271527void ExtensionService::UnloadExtension(
1528 const std::string& extension_id,
[email protected]b0af4792013-10-23 09:12:131529 UnloadedExtensionInfo::Reason reason) {
[email protected]27e469a2010-01-11 20:35:091530 // Make sure the extension gets deleted after we return from this function.
[email protected]695b5712012-12-06 23:55:281531 int include_mask = INCLUDE_EVERYTHING & ~INCLUDE_TERMINATED;
[email protected]9adb9692010-10-29 23:14:021532 scoped_refptr<const Extension> extension(
[email protected]695b5712012-12-06 23:55:281533 GetExtensionById(extension_id, include_mask));
[email protected]631cf822009-05-15 07:01:251534
[email protected]fa0c96732010-11-17 00:14:231535 // This method can be called via PostTask, so the extension may have been
1536 // unloaded by the time this runs.
[email protected]dc24976f2013-06-02 21:15:091537 if (!extension.get()) {
[email protected]dd163fb02011-05-04 22:22:171538 // In case the extension may have crashed/uninstalled. Allow the profile to
1539 // clean up its RequestContexts.
[email protected]31d8f5f22012-04-02 15:22:081540 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]fa0c96732010-11-17 00:14:231541 return;
[email protected]dd163fb02011-05-04 22:22:171542 }
[email protected]0c6da502009-08-14 22:32:391543
[email protected]1eb175082010-02-10 09:26:161544 // Keep information about the extension so that we can reload it later
1545 // even if it's not permanently installed.
1546 unloaded_extension_paths_[extension->id()] = extension->path();
1547
[email protected]f17dbd42010-08-16 23:21:101548 // Clean up if the extension is meant to be enabled after a reload.
[email protected]b914e2952013-04-26 07:10:031549 reloading_extensions_.erase(extension->id());
[email protected]f17dbd42010-08-16 23:21:101550
[email protected]d7e9a862010-11-03 21:57:491551 // Clean up runtime data.
1552 extension_runtime_data_.erase(extension_id);
1553
[email protected]060d4972012-07-19 17:22:391554 if (disabled_extensions_.Contains(extension->id())) {
[email protected]84df8332011-12-06 18:22:461555 disabled_extensions_.Remove(extension->id());
[email protected]dd163fb02011-05-04 22:22:171556 // Make sure the profile cleans up its RequestContexts when an already
1557 // disabled extension is unloaded (since they are also tracking the disabled
1558 // extensions).
[email protected]31d8f5f22012-04-02 15:22:081559 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]60a174a2013-08-02 20:29:281560 } else {
1561 // Remove the extension from our list.
1562 extensions_.Remove(extension->id());
1563 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]0c6da502009-08-14 22:32:391564 }
1565
[email protected]fb789532013-08-27 02:40:211566 content::NotificationService::current()->Notify(
1567 chrome::NOTIFICATION_EXTENSION_REMOVED,
1568 content::Source<Profile>(profile_),
1569 content::Details<const Extension>(extension.get()));
[email protected]631cf822009-05-15 07:01:251570}
1571
[email protected]8b1ec202013-09-05 02:09:501572void ExtensionService::RemoveComponentExtension(
1573 const std::string& extension_id) {
1574 scoped_refptr<const Extension> extension(
1575 GetExtensionById(extension_id, false));
[email protected]b0af4792013-10-23 09:12:131576 UnloadExtension(extension_id, UnloadedExtensionInfo::REASON_UNINSTALL);
[email protected]8b1ec202013-09-05 02:09:501577 content::NotificationService::current()->Notify(
1578 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
1579 content::Source<Profile>(profile_),
1580 content::Details<const Extension>(extension.get()));
1581}
1582
[email protected]eaa7dd182010-12-14 11:09:001583void ExtensionService::UnloadAllExtensions() {
[email protected]31bb5ee62012-09-12 22:58:401584 profile_->GetExtensionSpecialStoragePolicy()->RevokeRightsForAllExtensions();
[email protected]c8d407e2011-04-28 21:27:171585
[email protected]84df8332011-12-06 18:22:461586 extensions_.Clear();
1587 disabled_extensions_.Clear();
1588 terminated_extensions_.Clear();
[email protected]d7e9a862010-11-03 21:57:491589 extension_runtime_data_.clear();
[email protected]cd500f72010-06-25 23:44:321590
[email protected]c6e4a3412009-06-24 15:45:291591 // TODO(erikkay) should there be a notification for this? We can't use
1592 // EXTENSION_UNLOADED since that implies that the extension has been disabled
1593 // or uninstalled, and UnloadAll is just part of shutdown.
[email protected]9f1087e2009-06-15 17:29:321594}
1595
[email protected]eaa7dd182010-12-14 11:09:001596void ExtensionService::ReloadExtensions() {
[email protected]9f1087e2009-06-15 17:29:321597 UnloadAllExtensions();
[email protected]eac88332012-12-26 17:57:451598 component_loader_->LoadAll();
[email protected]d8c8f25f2011-11-02 18:18:011599 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]4a10006a2013-05-17 23:18:351600 // Don't call SetReadyAndNotifyListeners() since tests call this multiple
1601 // times.
[email protected]9f1087e2009-06-15 17:29:321602}
1603
[email protected]eaa7dd182010-12-14 11:09:001604void ExtensionService::GarbageCollectExtensions() {
[email protected]c04fd3dd2013-09-05 08:20:101605#if defined(OS_CHROMEOS)
1606 if (disable_garbage_collection_)
1607 return;
1608#endif
1609
[email protected]2f8757c32012-06-19 19:17:471610 if (extension_prefs_->pref_service()->ReadOnly())
1611 return;
1612
[email protected]9bd9a6862012-11-29 09:24:221613 if (pending_extension_manager()->HasPendingExtensions()) {
1614 // Don't garbage collect while there are pending installations, which may
1615 // be using the temporary installation directory. Try to garbage collect
1616 // again later.
[email protected]b3a25092013-05-28 22:08:161617 base::MessageLoop::current()->PostDelayedTask(
[email protected]9bd9a6862012-11-29 09:24:221618 FROM_HERE,
1619 base::Bind(&ExtensionService::GarbageCollectExtensions, AsWeakPtr()),
1620 base::TimeDelta::FromSeconds(kGarbageCollectRetryDelay));
1621 return;
1622 }
1623
[email protected]45759612012-07-10 17:21:231624 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> info(
[email protected]2f8757c32012-06-19 19:17:471625 extension_prefs_->GetInstalledExtensionsInfo());
1626
[email protected]650b2d52013-02-10 03:41:451627 std::multimap<std::string, base::FilePath> extension_paths;
[email protected]2f8757c32012-06-19 19:17:471628 for (size_t i = 0; i < info->size(); ++i)
[email protected]0db124b02012-11-07 04:55:051629 extension_paths.insert(std::make_pair(info->at(i)->extension_id,
1630 info->at(i)->extension_path));
1631
[email protected]399583b2012-12-11 09:33:421632 info = extension_prefs_->GetAllDelayedInstallInfo();
[email protected]0db124b02012-11-07 04:55:051633 for (size_t i = 0; i < info->size(); ++i)
1634 extension_paths.insert(std::make_pair(info->at(i)->extension_id,
1635 info->at(i)->extension_path));
[email protected]2f8757c32012-06-19 19:17:471636
[email protected]7f8f24f2012-11-15 19:40:141637 if (!GetFileTaskRunner()->PostTask(
1638 FROM_HERE,
[email protected]2f8757c32012-06-19 19:17:471639 base::Bind(
1640 &extension_file_util::GarbageCollectExtensions,
1641 install_directory_,
[email protected]31bb5ee62012-09-12 22:58:401642 extension_paths))) {
[email protected]2f8757c32012-06-19 19:17:471643 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:401644 }
[email protected]3cf4f0992009-02-03 23:00:301645}
1646
[email protected]820d9bd2013-04-03 03:46:031647void ExtensionService::SetReadyAndNotifyListeners() {
[email protected]4a10006a2013-05-17 23:18:351648 ready_->Signal();
[email protected]820d9bd2013-04-03 03:46:031649 content::NotificationService::current()->Notify(
1650 chrome::NOTIFICATION_EXTENSIONS_READY,
1651 content::Source<Profile>(profile_),
1652 content::NotificationService::NoDetails());
1653}
1654
[email protected]eaa7dd182010-12-14 11:09:001655void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]3eeddd892013-04-17 17:00:111656 if (updater_)
[email protected]93fd78f42009-07-10 16:43:171657 updater_->Start();
[email protected]fa6a9102010-11-22 15:38:501658
[email protected]bc151cf92013-02-12 04:57:261659 OnBlacklistUpdated();
[email protected]e72e8eb82009-06-18 17:21:511660}
1661
[email protected]fcb58a862012-05-01 01:03:151662void ExtensionService::AddExtension(const Extension* extension) {
[email protected]c8d407e2011-04-28 21:27:171663 // TODO(jstritar): We may be able to get rid of this branch by overriding the
1664 // default extension state to DISABLED when the --disable-extensions flag
1665 // is set (http://crbug.com/29067).
1666 if (!extensions_enabled() &&
1667 !extension->is_theme() &&
[email protected]1d5e58b2013-01-31 08:41:401668 extension->location() != Manifest::COMPONENT &&
1669 !Manifest::IsExternalLocation(extension->location())) {
[email protected]fcb58a862012-05-01 01:03:151670 return;
[email protected]330840c2012-08-29 22:21:011671 }
[email protected]c8d407e2011-04-28 21:27:171672
[email protected]b914e2952013-04-26 07:10:031673 bool is_extension_upgrade = false;
[email protected]116d40e2013-08-08 17:23:171674 bool is_extension_installed = false;
1675 const Extension* old = GetInstalledExtension(extension->id());
1676 if (old) {
1677 is_extension_installed = true;
1678 int version_compare_result =
1679 extension->version()->CompareTo(*(old->version()));
1680 is_extension_upgrade = version_compare_result > 0;
[email protected]b914e2952013-04-26 07:10:031681 // Other than for unpacked extensions, CrxInstaller should have guaranteed
1682 // that we aren't downgrading.
1683 if (!Manifest::IsUnpackedLocation(extension->location()))
[email protected]116d40e2013-08-08 17:23:171684 CHECK_GE(version_compare_result, 0);
[email protected]b914e2952013-04-26 07:10:031685 }
1686 SetBeingUpgraded(extension, is_extension_upgrade);
[email protected]c8d407e2011-04-28 21:27:171687
[email protected]1eb175082010-02-10 09:26:161688 // The extension is now loaded, remove its data from unloaded extension map.
1689 unloaded_extension_paths_.erase(extension->id());
1690
[email protected]bb7f40952011-01-13 00:21:201691 // If a terminated extension is loaded, remove it from the terminated list.
1692 UntrackTerminatedExtension(extension->id());
1693
[email protected]f17dbd42010-08-16 23:21:101694 // If the extension was disabled for a reload, then enable it.
[email protected]ca2e82022013-04-29 16:48:411695 bool reloading = reloading_extensions_.erase(extension->id()) > 0;
[email protected]f17dbd42010-08-16 23:21:101696
[email protected]b914e2952013-04-26 07:10:031697 // Check if the extension's privileges have changed and mark the
1698 // extension disabled if necessary.
[email protected]116d40e2013-08-08 17:23:171699 CheckPermissionsIncrease(extension, is_extension_installed);
[email protected]b914e2952013-04-26 07:10:031700
[email protected]116d40e2013-08-08 17:23:171701 if (is_extension_installed && !reloading) {
[email protected]ca2e82022013-04-29 16:48:411702 // To upgrade an extension in place, unload the old one and then load the
1703 // new one. ReloadExtension disables the extension, which is sufficient.
[email protected]b0af4792013-10-23 09:12:131704 UnloadExtension(extension->id(), UnloadedExtensionInfo::REASON_UPDATE);
[email protected]b914e2952013-04-26 07:10:031705 }
[email protected]0c6da502009-08-14 22:32:391706
[email protected]695b5712012-12-06 23:55:281707 if (extension_prefs_->IsExtensionBlacklisted(extension->id())) {
[email protected]ac875372013-02-28 04:36:091708 // Only prefs is checked for the blacklist. We rely on callers to check the
1709 // blacklist before calling into here, e.g. CrxInstaller checks before
[email protected]9f3c8532013-07-31 19:52:071710 // installation then threads through the install and pending install flow
1711 // of this class, and we check when loading installed extensions.
[email protected]695b5712012-12-06 23:55:281712 blacklisted_extensions_.Insert(extension);
[email protected]ca2e82022013-04-29 16:48:411713 } else if (!reloading &&
1714 extension_prefs_->IsExtensionDisabled(extension->id())) {
[email protected]6f6101832012-11-27 22:10:481715 disabled_extensions_.Insert(extension);
[email protected]f8aefb132013-10-30 09:29:521716 if (extension_sync_service_)
1717 extension_sync_service_->SyncExtensionChangeIfNeeded(*extension);
[email protected]44d62b62012-04-11 00:06:031718 content::NotificationService::current()->Notify(
1719 chrome::NOTIFICATION_EXTENSION_UPDATE_DISABLED,
1720 content::Source<Profile>(profile_),
1721 content::Details<const Extension>(extension));
[email protected]62f051c2012-03-29 17:04:441722
[email protected]7e9e2422012-12-13 19:54:191723 // Show the extension disabled error if a permissions increase was the
1724 // only reason it was disabled.
1725 if (extension_prefs_->GetDisableReasons(extension->id()) ==
[email protected]44d62b62012-04-11 00:06:031726 Extension::DISABLE_PERMISSIONS_INCREASE) {
[email protected]62f051c2012-03-29 17:04:441727 extensions::AddExtensionDisabledError(this, extension);
1728 }
[email protected]ca2e82022013-04-29 16:48:411729 } else if (reloading) {
1730 // Replace the old extension with the new version.
1731 CHECK(!disabled_extensions_.Insert(extension));
1732 EnableExtension(extension->id());
[email protected]695b5712012-12-06 23:55:281733 } else {
1734 // All apps that are displayed in the launcher are ordered by their ordinals
1735 // so we must ensure they have valid ordinals.
1736 if (extension->RequiresSortOrdinal()) {
[email protected]bd36b652013-02-27 02:13:251737 if (!extension->ShouldDisplayInNewTabPage()) {
[email protected]74474042013-11-21 12:03:541738 extension_prefs_->app_sorting()->MarkExtensionAsHidden(extension->id());
[email protected]bd36b652013-02-27 02:13:251739 }
[email protected]74474042013-11-21 12:03:541740 extension_prefs_->app_sorting()->EnsureValidOrdinals(
[email protected]695b5712012-12-06 23:55:281741 extension->id(), syncer::StringOrdinal());
1742 }
1743
1744 extensions_.Insert(extension);
[email protected]f8aefb132013-10-30 09:29:521745 if (extension_sync_service_)
1746 extension_sync_service_->SyncExtensionChangeIfNeeded(*extension);
[email protected]695b5712012-12-06 23:55:281747 NotifyExtensionLoaded(extension);
[email protected]f574c402012-12-04 23:20:311748 }
[email protected]b914e2952013-04-26 07:10:031749 SetBeingUpgraded(extension, false);
[email protected]aab98a52009-12-02 03:22:351750}
1751
[email protected]8c484b742012-11-29 06:05:361752void ExtensionService::AddComponentExtension(const Extension* extension) {
1753 const std::string old_version_string(
1754 extension_prefs_->GetVersionString(extension->id()));
1755 const Version old_version(old_version_string);
1756
[email protected]0181a7d2013-11-12 01:08:421757 VLOG(1) << "AddComponentExtension " << extension->name();
[email protected]8c484b742012-11-29 06:05:361758 if (!old_version.IsValid() || !old_version.Equals(*extension->version())) {
1759 VLOG(1) << "Component extension " << extension->name() << " ("
1760 << extension->id() << ") installing/upgrading from '"
1761 << old_version_string << "' to " << extension->version()->GetString();
1762
1763 AddNewOrUpdatedExtension(extension,
[email protected]399583b2012-12-11 09:33:421764 Extension::ENABLED_COMPONENT,
[email protected]9f3c8532013-07-31 19:52:071765 extensions::Blacklist::NOT_BLACKLISTED,
[email protected]399583b2012-12-11 09:33:421766 syncer::StringOrdinal());
[email protected]8c484b742012-11-29 06:05:361767 return;
1768 }
1769
1770 AddExtension(extension);
1771}
1772
[email protected]b914e2952013-04-26 07:10:031773void ExtensionService::UpdateActivePermissions(const Extension* extension) {
[email protected]902fd7b2011-07-27 18:42:311774 // If the extension has used the optional permissions API, it will have a
1775 // custom set of active permissions defined in the extension prefs. Here,
1776 // we update the extension's active permissions based on the prefs.
[email protected]c2e66e12012-06-27 06:27:061777 scoped_refptr<PermissionSet> active_permissions =
[email protected]902fd7b2011-07-27 18:42:311778 extension_prefs()->GetActivePermissions(extension->id());
1779
[email protected]dc24976f2013-06-02 21:15:091780 if (active_permissions.get()) {
[email protected]902fd7b2011-07-27 18:42:311781 // We restrict the active permissions to be within the bounds defined in the
1782 // extension's manifest.
1783 // a) active permissions must be a subset of optional + default permissions
1784 // b) active permissions must contains all default permissions
[email protected]c2e66e12012-06-27 06:27:061785 scoped_refptr<PermissionSet> total_permissions =
1786 PermissionSet::CreateUnion(
[email protected]2cb5e302013-05-09 05:00:061787 extensions::PermissionsData::GetRequiredPermissions(extension),
1788 extensions::PermissionsData::GetOptionalPermissions(extension));
[email protected]902fd7b2011-07-27 18:42:311789
1790 // Make sure the active permissions contain no more than optional + default.
[email protected]c2e66e12012-06-27 06:27:061791 scoped_refptr<PermissionSet> adjusted_active =
1792 PermissionSet::CreateIntersection(
[email protected]902fd7b2011-07-27 18:42:311793 total_permissions.get(), active_permissions.get());
1794
1795 // Make sure the active permissions contain the default permissions.
[email protected]c2e66e12012-06-27 06:27:061796 adjusted_active = PermissionSet::CreateUnion(
[email protected]2cb5e302013-05-09 05:00:061797 extensions::PermissionsData::GetRequiredPermissions(extension),
1798 adjusted_active.get());
[email protected]902fd7b2011-07-27 18:42:311799
[email protected]c333e792012-01-06 16:57:391800 extensions::PermissionsUpdater perms_updater(profile());
[email protected]dc24976f2013-06-02 21:15:091801 perms_updater.UpdateActivePermissions(extension, adjusted_active.get());
[email protected]902fd7b2011-07-27 18:42:311802 }
[email protected]b914e2952013-04-26 07:10:031803}
1804
1805void ExtensionService::CheckPermissionsIncrease(const Extension* extension,
[email protected]116d40e2013-08-08 17:23:171806 bool is_extension_installed) {
[email protected]b914e2952013-04-26 07:10:031807 UpdateActivePermissions(extension);
[email protected]902fd7b2011-07-27 18:42:311808
[email protected]8d888c12010-11-30 00:00:251809 // We keep track of all permissions the user has granted each extension.
1810 // This allows extensions to gracefully support backwards compatibility
1811 // by including unknown permissions in their manifests. When the user
1812 // installs the extension, only the recognized permissions are recorded.
1813 // When the unknown permissions become recognized (e.g., through browser
1814 // upgrade), we can prompt the user to accept these new permissions.
1815 // Extensions can also silently upgrade to less permissions, and then
1816 // silently upgrade to a version that adds these permissions back.
1817 //
1818 // For example, pretend that Chrome 10 includes a permission "omnibox"
1819 // for an API that adds suggestions to the omnibox. An extension can
1820 // maintain backwards compatibility while still having "omnibox" in the
1821 // manifest. If a user installs the extension on Chrome 9, the browser
1822 // will record the permissions it recognized, not including "omnibox."
1823 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
1824 // will disable the extension and prompt the user to approve the increase
1825 // in privileges. The extension could then release a new version that
1826 // removes the "omnibox" permission. When the user upgrades, Chrome will
1827 // still remember that "omnibox" had been granted, so that if the
1828 // extension once again includes "omnibox" in an upgrade, the extension
1829 // can upgrade without requiring this user's approval.
[email protected]eb5e4f92012-08-15 23:33:281830 int disable_reasons = extension_prefs_->GetDisableReasons(extension->id());
[email protected]8d888c12010-11-30 00:00:251831
[email protected]211a3f32013-05-28 21:48:181832 bool auto_grant_permission =
[email protected]1b563df2013-10-30 16:49:361833 (!is_extension_installed && extension->was_installed_by_default()) ||
[email protected]45928b682013-11-28 08:20:271834 extensions::ExtensionsBrowserClient::Get()->IsRunningInForcedAppMode();
[email protected]b914e2952013-04-26 07:10:031835 // Silently grant all active permissions to default apps only on install.
1836 // After install they should behave like other apps.
[email protected]211a3f32013-05-28 21:48:181837 // Silently grant all active permissions to apps install in kiosk mode on both
1838 // install and update.
1839 if (auto_grant_permission)
[email protected]b914e2952013-04-26 07:10:031840 GrantPermissions(extension);
1841
1842 bool is_privilege_increase = false;
1843 // We only need to compare the granted permissions to the current permissions
1844 // if the extension is not allowed to silently increase its permissions.
[email protected]13c68b62013-05-17 11:29:051845 if (!extensions::PermissionsData::CanSilentlyIncreasePermissions(extension) &&
[email protected]211a3f32013-05-28 21:48:181846 !auto_grant_permission) {
[email protected]8d888c12010-11-30 00:00:251847 // Add all the recognized permissions if the granted permissions list
1848 // hasn't been initialized yet.
[email protected]c2e66e12012-06-27 06:27:061849 scoped_refptr<PermissionSet> granted_permissions =
[email protected]902fd7b2011-07-27 18:42:311850 extension_prefs_->GetGrantedPermissions(extension->id());
[email protected]0d3e4a22011-06-23 19:02:521851 CHECK(granted_permissions.get());
[email protected]8d888c12010-11-30 00:00:251852
1853 // Here, we check if an extension's privileges have increased in a manner
1854 // that requires the user's approval. This could occur because the browser
1855 // upgraded and recognized additional privileges, or an extension upgrades
1856 // to a version that requires additional privileges.
[email protected]c41003472013-10-19 15:37:251857 is_privilege_increase =
1858 extensions::PermissionMessageProvider::Get()->IsPrivilegeIncrease(
1859 granted_permissions,
1860 extension->GetActivePermissions().get(),
1861 extension->GetType());
[email protected]8d888c12010-11-30 00:00:251862 }
1863
[email protected]116d40e2013-08-08 17:23:171864 if (is_extension_installed) {
[email protected]44d62b62012-04-11 00:06:031865 // If the extension was already disabled, suppress any alerts for becoming
1866 // disabled on permissions increase.
[email protected]b914e2952013-04-26 07:10:031867 bool previously_disabled =
1868 extension_prefs_->IsExtensionDisabled(extension->id());
[email protected]7e9e2422012-12-13 19:54:191869 // Legacy disabled extensions do not have a disable reason. Infer that if
1870 // there was no permission increase, it was likely disabled by the user.
1871 if (previously_disabled && disable_reasons == Extension::DISABLE_NONE &&
[email protected]b914e2952013-04-26 07:10:031872 !extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
[email protected]7e9e2422012-12-13 19:54:191873 disable_reasons |= Extension::DISABLE_USER_ACTION;
[email protected]44d62b62012-04-11 00:06:031874 }
[email protected]7e9e2422012-12-13 19:54:191875 // Extensions that came to us disabled from sync need a similar inference,
1876 // except based on the new version's permissions.
1877 if (previously_disabled &&
1878 disable_reasons == Extension::DISABLE_UNKNOWN_FROM_SYNC) {
1879 // Remove the DISABLE_UNKNOWN_FROM_SYNC reason.
1880 extension_prefs_->ClearDisableReasons(extension->id());
1881 if (!is_privilege_increase)
1882 disable_reasons |= Extension::DISABLE_USER_ACTION;
1883 }
[email protected]b914e2952013-04-26 07:10:031884 disable_reasons &= ~Extension::DISABLE_UNKNOWN_FROM_SYNC;
[email protected]8d888c12010-11-30 00:00:251885 }
1886
1887 // Extension has changed permissions significantly. Disable it. A
1888 // notification should be sent by the caller.
1889 if (is_privilege_increase) {
[email protected]7e9e2422012-12-13 19:54:191890 disable_reasons |= Extension::DISABLE_PERMISSIONS_INCREASE;
[email protected]fe2dd7742011-04-19 22:52:491891 if (!extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
1892 RecordPermissionMessagesHistogram(
1893 extension, "Extensions.Permissions_AutoDisable");
1894 }
[email protected]195c1582013-10-29 18:42:231895 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
[email protected]8d888c12010-11-30 00:00:251896 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
[email protected]b914e2952013-04-26 07:10:031897 }
1898 if (disable_reasons != Extension::DISABLE_NONE) {
[email protected]eb5e4f92012-08-15 23:33:281899 extension_prefs_->AddDisableReason(
1900 extension->id(),
1901 static_cast<Extension::DisableReason>(disable_reasons));
[email protected]8d888c12010-11-30 00:00:251902 }
1903}
1904
[email protected]eaa7dd182010-12-14 11:09:001905void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:391906 std::set<std::string> extension_ids;
[email protected]84df8332011-12-06 18:22:461907 for (ExtensionSet::const_iterator iter = extensions_.begin();
1908 iter != extensions_.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361909 const Extension* extension = iter->get();
[email protected]1d5e58b2013-01-31 08:41:401910 if (!extension->is_theme() && extension->location() != Manifest::COMPONENT)
[email protected]84df8332011-12-06 18:22:461911 extension_ids.insert(extension->id());
[email protected]aab98a52009-12-02 03:22:351912 }
1913
[email protected]f6431be82013-09-07 02:53:451914 crash_keys::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:251915}
1916
[email protected]669b2372013-10-17 15:04:581917ExtensionService::ImportStatus ExtensionService::CheckImports(
1918 const extensions::Extension* extension,
1919 std::list<SharedModuleInfo::ImportInfo>* missing_modules,
1920 std::list<SharedModuleInfo::ImportInfo>* outdated_modules) {
1921 DCHECK(extension);
1922 DCHECK(missing_modules && missing_modules->empty());
1923 DCHECK(outdated_modules && outdated_modules->empty());
[email protected]9f4e4f082013-06-21 07:11:191924 ImportStatus status = IMPORT_STATUS_OK;
[email protected]9f4e4f082013-06-21 07:11:191925 if (SharedModuleInfo::ImportsModules(extension)) {
1926 const std::vector<SharedModuleInfo::ImportInfo>& imports =
1927 SharedModuleInfo::GetImports(extension);
1928 std::vector<SharedModuleInfo::ImportInfo>::const_iterator i;
1929 for (i = imports.begin(); i != imports.end(); ++i) {
1930 Version version_required(i->minimum_version);
1931 const Extension* imported_module =
1932 GetExtensionById(i->extension_id, true);
1933 if (!imported_module) {
1934 if (extension->from_webstore()) {
1935 status = IMPORT_STATUS_UNSATISFIED;
[email protected]669b2372013-10-17 15:04:581936 missing_modules->push_back(*i);
[email protected]9f4e4f082013-06-21 07:11:191937 } else {
1938 return IMPORT_STATUS_UNRECOVERABLE;
1939 }
1940 } else if (!SharedModuleInfo::IsSharedModule(imported_module)) {
1941 return IMPORT_STATUS_UNRECOVERABLE;
1942 } else if (version_required.IsValid() &&
1943 imported_module->version()->CompareTo(version_required) < 0) {
1944 if (imported_module->from_webstore()) {
[email protected]669b2372013-10-17 15:04:581945 outdated_modules->push_back(*i);
[email protected]9f4e4f082013-06-21 07:11:191946 status = IMPORT_STATUS_UNSATISFIED;
1947 } else {
1948 return IMPORT_STATUS_UNRECOVERABLE;
1949 }
1950 }
1951 }
1952 }
[email protected]669b2372013-10-17 15:04:581953 return status;
1954}
1955
1956ExtensionService::ImportStatus ExtensionService::SatisfyImports(
1957 const Extension* extension) {
1958 std::list<SharedModuleInfo::ImportInfo> noinstalled;
1959 std::list<SharedModuleInfo::ImportInfo> outdated;
1960 ImportStatus status = CheckImports(extension, &noinstalled, &outdated);
1961 if (status == IMPORT_STATUS_UNRECOVERABLE)
1962 return status;
[email protected]9f4e4f082013-06-21 07:11:191963 if (status == IMPORT_STATUS_UNSATISFIED) {
[email protected]669b2372013-10-17 15:04:581964 std::list<SharedModuleInfo::ImportInfo>::const_iterator iter;
1965 for (iter = noinstalled.begin(); iter != noinstalled.end(); ++iter) {
[email protected]9f4e4f082013-06-21 07:11:191966 pending_extension_manager()->AddFromExtensionImport(
[email protected]669b2372013-10-17 15:04:581967 iter->extension_id,
[email protected]9f4e4f082013-06-21 07:11:191968 extension_urls::GetWebstoreUpdateUrl(),
1969 IsSharedModule);
1970 }
1971 CheckForUpdatesSoon();
1972 }
1973 return status;
1974}
1975
1976scoped_ptr<const ExtensionSet>
1977 ExtensionService::GetDependentExtensions(const Extension* extension) {
1978 scoped_ptr<ExtensionSet> dependents(new ExtensionSet());
1979 scoped_ptr<ExtensionSet> set_to_check(new ExtensionSet());
1980 if (SharedModuleInfo::IsSharedModule(extension)) {
1981 set_to_check->InsertAll(disabled_extensions_);
1982 set_to_check->InsertAll(delayed_installs_);
1983 set_to_check->InsertAll(extensions_);
1984 for (ExtensionSet::const_iterator iter = set_to_check->begin();
1985 iter != set_to_check->end(); ++iter) {
[email protected]198b5902013-06-27 10:36:111986 if (SharedModuleInfo::ImportsExtensionById(iter->get(),
1987 extension->id())) {
[email protected]9f4e4f082013-06-21 07:11:191988 dependents->Insert(*iter);
1989 }
1990 }
1991 }
1992 return dependents.PassAs<const ExtensionSet>();
1993}
1994
1995void ExtensionService::PruneSharedModulesOnUninstall(
1996 const Extension* extension) {
1997 if (SharedModuleInfo::ImportsModules(extension)) {
1998 const std::vector<SharedModuleInfo::ImportInfo>& imports =
1999 SharedModuleInfo::GetImports(extension);
2000 std::vector<SharedModuleInfo::ImportInfo>::const_iterator i;
2001 for (i = imports.begin(); i != imports.end(); ++i) {
2002 const Extension* imported_module =
2003 GetExtensionById(i->extension_id, true);
2004 if (imported_module && imported_module->from_webstore()) {
2005 scoped_ptr<const ExtensionSet> dependents =
2006 GetDependentExtensions(imported_module);
2007 if (dependents->size() == 0) {
[email protected]4c9201c42013-08-16 04:56:212008 UninstallExtension(i->extension_id, true, NULL);
[email protected]9f4e4f082013-06-21 07:11:192009 }
2010 }
2011 }
2012 }
2013}
2014
[email protected]8266d662011-07-12 21:53:262015void ExtensionService::OnExtensionInstalled(
[email protected]36a5c4c2011-12-14 16:34:502016 const Extension* extension,
[email protected]98270432012-09-11 20:51:242017 const syncer::StringOrdinal& page_ordinal,
[email protected]0db124b02012-11-07 04:55:052018 bool has_requirement_errors,
[email protected]9f3c8532013-07-31 19:52:072019 extensions::Blacklist::BlacklistState blacklist_state,
[email protected]0db124b02012-11-07 04:55:052020 bool wait_for_idle) {
[email protected]a29a517a2011-01-21 21:11:122021 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:202022
[email protected]c3cfb012011-04-06 22:07:352023 const std::string& id = extension->id();
[email protected]612a1cb12012-10-17 13:18:032024 bool initial_enable = ShouldEnableOnInstall(extension);
[email protected]3f213ad2012-07-26 23:39:412025 const extensions::PendingExtensionInfo* pending_extension_info = NULL;
[email protected]51a3bf8b2012-06-08 22:53:062026 if ((pending_extension_info = pending_extension_manager()->GetById(id))) {
[email protected]8f3bcbd2013-06-05 08:42:402027 if (!pending_extension_info->ShouldAllowInstall(extension)) {
[email protected]51a3bf8b2012-06-08 22:53:062028 pending_extension_manager()->Remove(id);
[email protected]8ef78fd2010-08-19 17:14:322029
[email protected]31bb5ee62012-09-12 22:58:402030 LOG(WARNING) << "ShouldAllowInstall() returned false for "
2031 << id << " of type " << extension->GetType()
[email protected]65348062013-01-15 07:27:222032 << " and update URL "
2033 << extensions::ManifestURL::GetUpdateURL(extension).spec()
[email protected]31bb5ee62012-09-12 22:58:402034 << "; not installing";
[email protected]ae6cb3022011-04-04 20:11:542035
[email protected]4416c5a2010-06-26 01:28:572036 // Delete the extension directory since we're not going to
2037 // load it.
[email protected]7f8f24f2012-11-15 19:40:142038 if (!GetFileTaskRunner()->PostTask(
2039 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362040 base::Bind(&extension_file_util::DeleteFile,
[email protected]31bb5ee62012-09-12 22:58:402041 extension->path(), true))) {
[email protected]14908b72011-04-20 06:54:362042 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:402043 }
[email protected]4416c5a2010-06-26 01:28:572044 return;
2045 }
[email protected]51a3bf8b2012-06-08 22:53:062046
2047 pending_extension_manager()->Remove(id);
[email protected]4416c5a2010-06-26 01:28:572048 } else {
[email protected]c3cfb012011-04-06 22:07:352049 // We explicitly want to re-enable an uninstalled external
2050 // extension; if we're here, that means the user is manually
2051 // installing the extension.
[email protected]6cc7dbae2011-04-29 21:18:332052 if (IsExternalExtensionUninstalled(id)) {
2053 initial_enable = true;
2054 }
[email protected]aa142702010-03-26 01:26:332055 }
2056
[email protected]98270432012-09-11 20:51:242057 // Unsupported requirements overrides the management policy.
2058 if (has_requirement_errors) {
2059 initial_enable = false;
2060 extension_prefs_->AddDisableReason(
2061 id, Extension::DISABLE_UNSUPPORTED_REQUIREMENT);
2062 // If the extension was disabled because of unsupported requirements but
2063 // now supports all requirements after an update and there are not other
2064 // disable reasons, enable it.
2065 } else if (extension_prefs_->GetDisableReasons(id) ==
2066 Extension::DISABLE_UNSUPPORTED_REQUIREMENT) {
2067 initial_enable = true;
2068 extension_prefs_->ClearDisableReasons(id);
2069 }
2070
[email protected]48a359342013-10-30 00:22:002071 if (blacklist_state == extensions::Blacklist::BLACKLISTED_MALWARE) {
[email protected]9f3c8532013-07-31 19:52:072072 // Installation of a blacklisted extension can happen from sync, policy,
2073 // etc, where to maintain consistency we need to install it, just never
2074 // load it (see AddExtension). Usually it should be the job of callers to
2075 // incercept blacklisted extension earlier (e.g. CrxInstaller, before even
2076 // showing the install dialogue).
2077 extension_prefs()->AcknowledgeBlacklistedExtension(id);
2078 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.SilentInstall",
2079 extension->location(),
2080 Manifest::NUM_LOCATIONS);
2081 }
2082
[email protected]695b5712012-12-06 23:55:282083 if (!GetInstalledExtension(extension->id())) {
[email protected]07533022011-06-27 20:42:552084 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
2085 extension->GetType(), 100);
[email protected]69084dc2012-11-17 07:39:302086 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallSource",
[email protected]1d5e58b2013-01-31 08:41:402087 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]07533022011-06-27 20:42:552088 RecordPermissionMessagesHistogram(
2089 extension, "Extensions.Permissions_Install");
[email protected]69084dc2012-11-17 07:39:302090 } else {
2091 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateType",
2092 extension->GetType(), 100);
2093 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateSource",
[email protected]1d5e58b2013-01-31 08:41:402094 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]07533022011-06-27 20:42:552095 }
2096
[email protected]a39921b42012-02-28 03:42:542097 // Certain extension locations are specific enough that we can
2098 // auto-acknowledge any extension that came from one of them.
[email protected]9bb691632013-09-26 18:50:122099 if (Manifest::IsPolicyLocation(extension->location()))
[email protected]a39921b42012-02-28 03:42:542100 AcknowledgeExternalExtension(extension->id());
[email protected]399583b2012-12-11 09:33:422101 const Extension::State initial_state =
2102 initial_enable ? Extension::ENABLED : Extension::DISABLED;
[email protected]0f9e6262013-11-05 23:49:192103 const bool blacklisted_for_malware =
2104 blacklist_state == extensions::Blacklist::BLACKLISTED_MALWARE;
[email protected]e7aa7b7e2012-11-27 04:51:222105 if (ShouldDelayExtensionUpdate(id, wait_for_idle)) {
[email protected]9f3c8532013-07-31 19:52:072106 extension_prefs_->SetDelayedInstallInfo(
2107 extension,
2108 initial_state,
[email protected]0f9e6262013-11-05 23:49:192109 blacklisted_for_malware,
[email protected]9f3c8532013-07-31 19:52:072110 extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE,
2111 page_ordinal);
[email protected]0db124b02012-11-07 04:55:052112
2113 // Transfer ownership of |extension|.
[email protected]9f4e4f082013-06-21 07:11:192114 delayed_installs_.Insert(extension);
[email protected]0db124b02012-11-07 04:55:052115
[email protected]75bdcb872013-03-13 00:41:452116 // Notify observers that app update is available.
2117 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
[email protected]a9aa62b312013-11-29 05:35:062118 OnAppUpdateAvailable(extension));
[email protected]0db124b02012-11-07 04:55:052119 return;
2120 }
2121
[email protected]9f4e4f082013-06-21 07:11:192122 ImportStatus status = SatisfyImports(extension);
2123 if (installs_delayed_for_gc()) {
[email protected]9f3c8532013-07-31 19:52:072124 extension_prefs_->SetDelayedInstallInfo(
2125 extension,
2126 initial_state,
[email protected]0f9e6262013-11-05 23:49:192127 blacklisted_for_malware,
[email protected]9f3c8532013-07-31 19:52:072128 extensions::ExtensionPrefs::DELAY_REASON_GC,
2129 page_ordinal);
[email protected]399583b2012-12-11 09:33:422130 delayed_installs_.Insert(extension);
[email protected]9f4e4f082013-06-21 07:11:192131 } else if (status != IMPORT_STATUS_OK) {
2132 if (status == IMPORT_STATUS_UNSATISFIED) {
[email protected]9f3c8532013-07-31 19:52:072133 extension_prefs_->SetDelayedInstallInfo(
2134 extension,
2135 initial_state,
[email protected]0f9e6262013-11-05 23:49:192136 blacklisted_for_malware,
[email protected]9f4e4f082013-06-21 07:11:192137 extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IMPORTS,
2138 page_ordinal);
2139 delayed_installs_.Insert(extension);
2140 }
[email protected]399583b2012-12-11 09:33:422141 } else {
[email protected]9f3c8532013-07-31 19:52:072142 AddNewOrUpdatedExtension(extension,
2143 initial_state,
2144 blacklist_state,
2145 page_ordinal);
[email protected]399583b2012-12-11 09:33:422146 }
[email protected]8c484b742012-11-29 06:05:362147}
2148
2149void ExtensionService::AddNewOrUpdatedExtension(
2150 const Extension* extension,
[email protected]399583b2012-12-11 09:33:422151 Extension::State initial_state,
[email protected]9f3c8532013-07-31 19:52:072152 extensions::Blacklist::BlacklistState blacklist_state,
[email protected]399583b2012-12-11 09:33:422153 const syncer::StringOrdinal& page_ordinal) {
[email protected]8c484b742012-11-29 06:05:362154 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]0f9e6262013-11-05 23:49:192155 const bool blacklisted_for_malware =
2156 blacklist_state == extensions::Blacklist::BLACKLISTED_MALWARE;
[email protected]9f3c8532013-07-31 19:52:072157 extension_prefs_->OnExtensionInstalled(extension,
2158 initial_state,
[email protected]0f9e6262013-11-05 23:49:192159 blacklisted_for_malware,
[email protected]9f3c8532013-07-31 19:52:072160 page_ordinal);
[email protected]9e9c1d12013-07-31 01:58:122161 delayed_installs_.Remove(extension->id());
[email protected]ffd2f79e2013-11-14 00:11:462162 if (extensions::ManifestURL::UpdatesFromGallery(extension)) {
2163 extensions::ExtensionSystem::Get(profile_)->install_verifier()->Add(
2164 extension->id(), InstallVerifier::AddResultCallback());
2165 }
[email protected]6f6101832012-11-27 22:10:482166 FinishInstallation(extension);
2167}
2168
2169void ExtensionService::MaybeFinishDelayedInstallation(
2170 const std::string& extension_id) {
[email protected]9f4e4f082013-06-21 07:11:192171 // Check if the extension already got installed.
2172 if (!delayed_installs_.Contains(extension_id))
[email protected]6f6101832012-11-27 22:10:482173 return;
[email protected]9f4e4f082013-06-21 07:11:192174 extensions::ExtensionPrefs::DelayReason reason =
2175 extension_prefs_->GetDelayedInstallReason(extension_id);
2176
2177 // Check if the extension is idle. DELAY_REASON_NONE is used for older
2178 // preferences files that will not have set this field but it was previously
2179 // only used for idle updates.
2180 if ((reason == extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE ||
2181 reason == extensions::ExtensionPrefs::DELAY_REASON_NONE) &&
2182 is_ready() && !IsExtensionIdle(extension_id))
[email protected]6f6101832012-11-27 22:10:482183 return;
2184
[email protected]9f4e4f082013-06-21 07:11:192185 const Extension* extension = delayed_installs_.GetByID(extension_id);
2186 if (reason == extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IMPORTS) {
2187 ImportStatus status = SatisfyImports(extension);
2188 if (status != IMPORT_STATUS_OK) {
2189 if (status == IMPORT_STATUS_UNRECOVERABLE) {
2190 delayed_installs_.Remove(extension_id);
2191 // Make sure no version of the extension is actually installed, (i.e.,
2192 // that this delayed install was not an update).
2193 CHECK(!extension_prefs_->GetInstalledExtensionInfo(extension_id).get());
2194 extension_prefs_->DeleteExtensionPrefs(extension_id);
2195 }
2196 return;
2197 }
2198 }
2199
[email protected]6f6101832012-11-27 22:10:482200 FinishDelayedInstallation(extension_id);
2201}
2202
2203void ExtensionService::FinishDelayedInstallation(
2204 const std::string& extension_id) {
2205 scoped_refptr<const Extension> extension(
2206 GetPendingExtensionUpdate(extension_id));
[email protected]dc24976f2013-06-02 21:15:092207 CHECK(extension.get());
[email protected]9f4e4f082013-06-21 07:11:192208 delayed_installs_.Remove(extension_id);
[email protected]6f6101832012-11-27 22:10:482209
[email protected]399583b2012-12-11 09:33:422210 if (!extension_prefs_->FinishDelayedInstallInfo(extension_id))
[email protected]6f6101832012-11-27 22:10:482211 NOTREACHED();
2212
[email protected]dc24976f2013-06-02 21:15:092213 FinishInstallation(extension.get());
[email protected]6f6101832012-11-27 22:10:482214}
2215
2216void ExtensionService::FinishInstallation(const Extension* extension) {
[email protected]e7554c3f2013-05-29 00:36:562217 const extensions::Extension* existing_extension =
2218 GetInstalledExtension(extension->id());
2219 bool is_update = false;
2220 std::string old_name;
2221 if (existing_extension) {
2222 is_update = true;
2223 old_name = existing_extension->name();
2224 }
2225 extensions::InstalledExtensionInfo details(extension, is_update, old_name);
[email protected]fcb58a862012-05-01 01:03:152226 content::NotificationService::current()->Notify(
2227 chrome::NOTIFICATION_EXTENSION_INSTALLED,
2228 content::Source<Profile>(profile_),
[email protected]41bb80bd2013-05-03 10:56:022229 content::Details<const extensions::InstalledExtensionInfo>(&details));
[email protected]fcb58a862012-05-01 01:03:152230
[email protected]d96eb512012-11-01 23:44:082231 bool unacknowledged_external = IsUnacknowledgedExternalExtension(extension);
[email protected]41070e8d2012-10-24 01:34:362232
[email protected]399583b2012-12-11 09:33:422233 // Unpacked extensions default to allowing file access, but if that has been
2234 // overridden, don't reset the value.
[email protected]1d5e58b2013-01-31 08:41:402235 if (Manifest::ShouldAlwaysAllowFileAccess(extension->location()) &&
[email protected]399583b2012-12-11 09:33:422236 !extension_prefs_->HasAllowFileAccessSetting(extension->id())) {
2237 extension_prefs_->SetAllowFileAccess(extension->id(), true);
2238 }
2239
[email protected]6f6101832012-11-27 22:10:482240 AddExtension(extension);
[email protected]612a1cb12012-10-17 13:18:032241
2242 // If this is a new external extension that was disabled, alert the user
[email protected]d96eb512012-11-01 23:44:082243 // so he can reenable it. We do this last so that it has already been
2244 // added to our list of extensions.
[email protected]3e3fafbb2013-08-17 13:33:162245 if (unacknowledged_external && !is_update) {
[email protected]612a1cb12012-10-17 13:18:032246 UpdateExternalExtensionAlert();
[email protected]d96eb512012-11-01 23:44:082247 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
2248 EXTERNAL_EXTENSION_INSTALLED,
2249 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:022250 if (extensions::ManifestURL::UpdatesFromGallery(extension)) {
2251 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventWebstore",
2252 EXTERNAL_EXTENSION_INSTALLED,
2253 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
2254 } else {
2255 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventNonWebstore",
2256 EXTERNAL_EXTENSION_INSTALLED,
2257 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
2258 }
[email protected]d96eb512012-11-01 23:44:082259 }
[email protected]9f4e4f082013-06-21 07:11:192260
2261 // Check extensions that may have been delayed only because this shared module
2262 // was not available.
2263 if (SharedModuleInfo::IsSharedModule(extension)) {
2264 MaybeFinishDelayedInstallations();
2265 }
[email protected]4a190632009-05-09 01:07:422266}
2267
[email protected]76b65442012-11-17 14:11:482268const Extension* ExtensionService::GetPendingExtensionUpdate(
2269 const std::string& id) const {
[email protected]9f4e4f082013-06-21 07:11:192270 return delayed_installs_.GetByID(id);
[email protected]76b65442012-11-17 14:11:482271}
2272
[email protected]bb7f40952011-01-13 00:21:202273void ExtensionService::TrackTerminatedExtension(const Extension* extension) {
[email protected]84df8332011-12-06 18:22:462274 if (!terminated_extensions_.Contains(extension->id()))
2275 terminated_extensions_.Insert(make_scoped_refptr(extension));
[email protected]fa2416f2011-05-03 08:41:202276
[email protected]b0af4792013-10-23 09:12:132277 UnloadExtension(extension->id(), UnloadedExtensionInfo::REASON_TERMINATE);
[email protected]bb7f40952011-01-13 00:21:202278}
2279
2280void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
[email protected]fa2416f2011-05-03 08:41:202281 std::string lowercase_id = StringToLowerASCII(id);
[email protected]60a174a2013-08-02 20:29:282282 const Extension* extension = terminated_extensions_.GetByID(lowercase_id);
[email protected]84df8332011-12-06 18:22:462283 terminated_extensions_.Remove(lowercase_id);
[email protected]fb789532013-08-27 02:40:212284 if (extension) {
2285 content::NotificationService::current()->Notify(
2286 chrome::NOTIFICATION_EXTENSION_REMOVED,
2287 content::Source<Profile>(profile_),
2288 content::Details<const Extension>(extension));
2289 }
[email protected]bb7f40952011-01-13 00:21:202290}
2291
[email protected]0dfe05c2011-02-23 23:03:362292const Extension* ExtensionService::GetTerminatedExtension(
[email protected]8001df22011-04-28 19:59:472293 const std::string& id) const {
[email protected]695b5712012-12-06 23:55:282294 return GetExtensionById(id, INCLUDE_TERMINATED);
[email protected]8001df22011-04-28 19:59:472295}
2296
2297const Extension* ExtensionService::GetInstalledExtension(
2298 const std::string& id) const {
[email protected]695b5712012-12-06 23:55:282299 int include_mask = INCLUDE_ENABLED |
2300 INCLUDE_DISABLED |
2301 INCLUDE_TERMINATED |
2302 INCLUDE_BLACKLISTED;
2303 return GetExtensionById(id, include_mask);
[email protected]0dfe05c2011-02-23 23:03:362304}
2305
[email protected]eaa7dd182010-12-14 11:09:002306bool ExtensionService::ExtensionBindingsAllowed(const GURL& url) {
[email protected]615d88f2011-12-13 01:47:442307 // Allow bindings for all packaged extensions and component hosted apps.
[email protected]be9915fb2013-07-18 09:28:552308 const Extension* extension = extensions_.GetExtensionOrAppByURL(url);
[email protected]615d88f2011-12-13 01:47:442309 return extension && (!extension->is_hosted_app() ||
[email protected]1d5e58b2013-01-31 08:41:402310 extension->location() == Manifest::COMPONENT);
[email protected]6d2e60bd2010-06-03 22:37:392311}
2312
[email protected]4d007b312012-10-17 03:00:482313bool ExtensionService::ShouldBlockUrlInBrowserTab(GURL* url) {
[email protected]be9915fb2013-07-18 09:28:552314 const Extension* extension = extensions_.GetExtensionOrAppByURL(*url);
[email protected]4d007b312012-10-17 03:00:482315 if (extension && extension->is_platform_app()) {
2316 *url = GURL(chrome::kExtensionInvalidRequestURL);
2317 return true;
2318 }
2319
2320 return false;
2321}
2322
[email protected]9060d8b02012-01-13 02:14:302323bool ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:322324 const std::string& id,
[email protected]8e4560b62011-01-14 10:09:142325 const Version* version,
[email protected]650b2d52013-02-10 03:41:452326 const base::FilePath& path,
[email protected]1d5e58b2013-01-31 08:41:402327 Manifest::Location location,
[email protected]47fc70c2011-12-06 07:29:512328 int creation_flags,
2329 bool mark_acknowledged) {
[email protected]ab22ba42011-01-14 16:36:382330 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2331 CHECK(Extension::IdIsValid(id));
[email protected]79c833b52011-04-05 18:31:012332 if (extension_prefs_->IsExternalExtensionUninstalled(id))
[email protected]9060d8b02012-01-13 02:14:302333 return false;
[email protected]a8af9fdb2010-10-28 21:52:202334
[email protected]7577a5c52009-07-30 06:21:582335 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:492336 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:582337 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:022338 const Extension* existing = GetExtensionById(id, true);
[email protected]910f72ce2012-08-24 01:38:352339
[email protected]7577a5c52009-07-30 06:21:582340 if (existing) {
[email protected]910f72ce2012-08-24 01:38:352341 // The default apps will have the location set as INTERNAL. Since older
2342 // default apps are installed as EXTERNAL, we override them. However, if the
2343 // app is already installed as internal, then do the version check.
[email protected]31bb5ee62012-09-12 22:58:402344 // TODO(grv) : Remove after Q1-2013.
[email protected]910f72ce2012-08-24 01:38:352345 bool is_default_apps_migration =
[email protected]1d5e58b2013-01-31 08:41:402346 (location == Manifest::INTERNAL &&
2347 Manifest::IsExternalLocation(existing->location()));
[email protected]910f72ce2012-08-24 01:38:352348
2349 if (!is_default_apps_migration) {
2350 DCHECK(version);
2351
2352 switch (existing->version()->CompareTo(*version)) {
2353 case -1: // existing version is older, we should upgrade
2354 break;
2355 case 0: // existing version is same, do nothing
2356 return false;
2357 case 1: // existing version is newer, uh-oh
2358 LOG(WARNING) << "Found external version of extension " << id
2359 << "that is older than current version. Current version "
2360 << "is: " << existing->VersionString() << ". New "
2361 << "version is: " << version->GetString()
2362 << ". Keeping current version.";
2363 return false;
2364 }
[email protected]7577a5c52009-07-30 06:21:582365 }
2366 }
2367
[email protected]9060d8b02012-01-13 02:14:302368 // If the extension is already pending, don't start an install.
[email protected]e3987852012-05-04 10:06:302369 if (!pending_extension_manager()->AddFromExternalFile(
[email protected]464213a2013-10-15 01:06:482370 id, location, *version, creation_flags, mark_acknowledged)) {
[email protected]9060d8b02012-01-13 02:14:302371 return false;
[email protected]e3987852012-05-04 10:06:302372 }
[email protected]9c635f22010-12-02 09:36:362373
[email protected]14908b72011-04-20 06:54:362374 // no client (silent install)
[email protected]f8636f92013-08-09 21:02:372375 scoped_refptr<CrxInstaller> installer(CrxInstaller::CreateSilent(this));
[email protected]6dfbbf82010-03-12 23:09:162376 installer->set_install_source(location);
2377 installer->set_expected_id(id);
[email protected]cb0e50312011-05-09 15:03:072378 installer->set_expected_version(*version);
2379 installer->set_install_cause(extension_misc::INSTALL_CAUSE_EXTERNAL_FILE);
[email protected]1bf73cc32011-10-26 22:38:312380 installer->set_creation_flags(creation_flags);
[email protected]88e8ec9152013-01-17 04:05:182381#if defined(OS_CHROMEOS)
2382 extensions::InstallLimiter::Get(profile_)->Add(installer, path);
2383#else
[email protected]6dfbbf82010-03-12 23:09:162384 installer->InstallCrx(path);
[email protected]88e8ec9152013-01-17 04:05:182385#endif
[email protected]47fc70c2011-12-06 07:29:512386
2387 // Depending on the source, a new external extension might not need a user
2388 // notification on installation. For such extensions, mark them acknowledged
2389 // now to suppress the notification.
2390 if (mark_acknowledged)
2391 AcknowledgeExternalExtension(id);
[email protected]9060d8b02012-01-13 02:14:302392
2393 return true;
[email protected]7577a5c52009-07-30 06:21:582394}
2395
[email protected]b5a507b22013-11-08 20:41:572396scoped_ptr<DictionaryValue> ExtensionService::GetExtensionInfo(
2397 const std::string& extension_id) const {
2398 scoped_ptr<DictionaryValue> dictionary(new DictionaryValue);
2399 const extensions::Extension* extension = extensions_.GetByID(extension_id);
2400 if (extension) {
2401 GURL icon = extensions::ExtensionIconSource::GetIconURL(
2402 extension, extension_misc::EXTENSION_ICON_SMALLISH,
2403 ExtensionIconSet::MATCH_BIGGER, false, NULL);
2404 dictionary->SetString("id", extension_id);
2405 dictionary->SetString("name", extension->name());
2406 dictionary->SetString("icon", icon.spec());
2407 }
2408 return dictionary.Pass();
2409}
2410
[email protected]eaa7dd182010-12-14 11:09:002411void ExtensionService::ReportExtensionLoadError(
[email protected]650b2d52013-02-10 03:41:452412 const base::FilePath& extension_path,
[email protected]d11c8e92009-10-20 23:26:402413 const std::string &error,
[email protected]d11c8e92009-10-20 23:26:402414 bool be_noisy) {
[email protected]d8c8f25f2011-11-02 18:18:012415 content::NotificationService::current()->Notify(
2416 chrome::NOTIFICATION_EXTENSION_LOAD_ERROR,
2417 content::Source<Profile>(profile_),
2418 content::Details<const std::string>(&error));
[email protected]d11c8e92009-10-20 23:26:402419
[email protected]8a205c02011-02-04 20:41:332420 std::string path_str = UTF16ToUTF8(extension_path.LossyDisplayName());
[email protected]438afb12012-07-05 03:18:212421 string16 message = UTF8ToUTF16(base::StringPrintf(
[email protected]18d4b6c2010-09-21 03:21:042422 "Could not load extension from '%s'. %s",
[email protected]fc670822011-12-17 09:33:492423 path_str.c_str(), error.c_str()));
[email protected]d11c8e92009-10-20 23:26:402424 ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy);
2425}
2426
[email protected]eaa7dd182010-12-14 11:09:002427void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]3a1dc572012-07-31 22:25:132428 extensions::ExtensionHost* host) {
[email protected]406027c02010-09-27 08:03:182429 OrphanedDevTools::iterator iter =
[email protected]2d2f6cfc2011-05-06 21:09:332430 orphaned_dev_tools_.find(host->extension_id());
[email protected]406027c02010-09-27 08:03:182431 if (iter == orphaned_dev_tools_.end())
2432 return;
2433
[email protected]04ea1bb2013-07-10 09:26:092434 iter->second->ConnectRenderViewHost(host->render_view_host());
[email protected]406027c02010-09-27 08:03:182435 orphaned_dev_tools_.erase(iter);
2436}
2437
[email protected]432115822011-07-10 15:52:272438void ExtensionService::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:532439 const content::NotificationSource& source,
2440 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:272441 switch (type) {
[email protected]3c4abc82012-10-22 22:25:542442 case chrome::NOTIFICATION_APP_TERMINATING:
2443 // Shutdown has started. Don't start any more extension installs.
2444 // (We cannot use ExtensionService::Shutdown() for this because it
2445 // happens too late in browser teardown.)
2446 browser_terminating_ = true;
2447 break;
[email protected]432115822011-07-10 15:52:272448 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]6c2381d2011-10-19 02:52:532449 if (profile_ !=
2450 content::Source<Profile>(source).ptr()->GetOriginalProfile()) {
[email protected]bc535ee52010-08-31 18:40:322451 break;
[email protected]6c2381d2011-10-19 02:52:532452 }
[email protected]a4ed6282009-12-14 20:51:162453
[email protected]3a1dc572012-07-31 22:25:132454 extensions::ExtensionHost* host =
2455 content::Details<extensions::ExtensionHost>(details).ptr();
[email protected]f128af42010-08-05 18:05:262456
[email protected]fa2416f2011-05-03 08:41:202457 // Mark the extension as terminated and Unload it. We want it to
2458 // be in a consistent state: either fully working or not loaded
2459 // at all, but never half-crashed. We do it in a PostTask so
2460 // that other handlers of this notification will still have
2461 // access to the Extension and ExtensionHost.
[email protected]b3a25092013-05-28 22:08:162462 base::MessageLoop::current()->PostTask(
[email protected]14908b72011-04-20 06:54:362463 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362464 base::Bind(
[email protected]fa2416f2011-05-03 08:41:202465 &ExtensionService::TrackTerminatedExtension,
[email protected]d8c8f25f2011-11-02 18:18:012466 AsWeakPtr(),
[email protected]fa2416f2011-05-03 08:41:202467 host->extension()));
[email protected]31f77262009-12-02 20:48:532468 break;
2469 }
[email protected]432115822011-07-10 15:52:272470 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: {
[email protected]f3b1a082011-11-18 00:34:302471 content::RenderProcessHost* process =
2472 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192473 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302474 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192475 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2476 break;
2477
[email protected]15397b42012-05-16 23:56:062478 // Extensions need to know the channel for API restrictions.
2479 process->Send(new ExtensionMsg_SetChannel(
[email protected]15d4d2d2013-08-09 06:49:032480 extensions::GetCurrentChannel()));
[email protected]15397b42012-05-16 23:56:062481
[email protected]4941fe92013-06-13 23:21:012482 // Platform apps need to know the system font.
2483 scoped_ptr<base::DictionaryValue> fonts(new base::DictionaryValue);
2484 webui::SetFontAndTextDirection(fonts.get());
2485 std::string font_family, font_size;
2486 fonts->GetString("fontfamily", &font_family);
2487 fonts->GetString("fontsize", &font_size);
2488 process->Send(new ExtensionMsg_SetSystemFont(
2489 font_family, font_size));
2490
[email protected]77a6970c2011-04-23 16:58:562491 // Valid extension function names, used to setup bindings in renderer.
2492 std::vector<std::string> function_names;
2493 ExtensionFunctionDispatcher::GetAllFunctionNames(&function_names);
2494 process->Send(new ExtensionMsg_SetFunctionNames(function_names));
[email protected]31f77262009-12-02 20:48:532495
[email protected]77a6970c2011-04-23 16:58:562496 // Scripting whitelist. This is modified by tests and must be communicated
2497 // to renderers.
2498 process->Send(new ExtensionMsg_SetScriptingWhitelist(
[email protected]c81f5d32013-10-26 10:38:422499 extensions::ExtensionsClient::Get()->GetScriptingWhitelist()));
[email protected]77a6970c2011-04-23 16:58:562500
2501 // Loaded extensions.
[email protected]9776e82e2011-11-15 02:17:532502 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions;
[email protected]84df8332011-12-06 18:22:462503 for (ExtensionSet::const_iterator iter = extensions_.begin();
2504 iter != extensions_.end(); ++iter) {
[email protected]19647262011-12-16 09:57:492505 // Renderers don't need to know about themes.
2506 if (!(*iter)->is_theme())
[email protected]cadac622013-06-11 16:46:362507 loaded_extensions.push_back(ExtensionMsg_Loaded_Params(iter->get()));
[email protected]77a6970c2011-04-23 16:58:562508 }
[email protected]9776e82e2011-11-15 02:17:532509 process->Send(new ExtensionMsg_Loaded(loaded_extensions));
[email protected]77a6970c2011-04-23 16:58:562510 break;
2511 }
[email protected]432115822011-07-10 15:52:272512 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
[email protected]f3b1a082011-11-18 00:34:302513 content::RenderProcessHost* process =
2514 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192515 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302516 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192517 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2518 break;
2519
[email protected]52b76592013-11-02 17:59:032520 if (process_map_.Contains(process->GetID())) {
2521 // An extension process was terminated, this might have resulted in an
2522 // app or extension becoming idle.
2523 std::set<std::string> extension_ids =
2524 process_map_.GetExtensionsInProcess(process->GetID());
2525 for (std::set<std::string>::const_iterator it = extension_ids.begin();
2526 it != extension_ids.end(); ++it) {
2527 if (delayed_installs_.Contains(*it)) {
2528 base::MessageLoop::current()->PostDelayedTask(
2529 FROM_HERE,
2530 base::Bind(&ExtensionService::MaybeFinishDelayedInstallation,
2531 AsWeakPtr(), *it),
2532 base::TimeDelta::FromSeconds(kUpdateIdleDelay));
2533 }
2534 }
2535 }
2536
[email protected]6bc04fd82011-12-04 02:29:352537 process_map_.RemoveAllFromProcess(process->GetID());
[email protected]6f371442011-11-09 06:45:462538 BrowserThread::PostTask(
[email protected]38427a12013-11-09 17:34:202539 BrowserThread::IO,
2540 FROM_HERE,
2541 base::Bind(&extensions::InfoMap::UnregisterAllExtensionsInProcess,
[email protected]31d8f5f22012-04-02 15:22:082542 system_->info_map(),
[email protected]f3b1a082011-11-18 00:34:302543 process->GetID()));
[email protected]da5683db2011-04-23 17:12:212544 break;
2545 }
[email protected]92dd8d92013-02-26 00:41:082546 case chrome::NOTIFICATION_UPGRADE_RECOMMENDED: {
[email protected]75bdcb872013-03-13 00:41:452547 // Notify observers that chrome update is available.
2548 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
2549 OnChromeUpdateAvailable());
[email protected]92dd8d92013-02-26 00:41:082550 break;
2551 }
[email protected]aa96d3a2010-08-21 08:45:252552
[email protected]4814b512009-11-07 00:12:292553 default:
2554 NOTREACHED() << "Unexpected notification type.";
2555 }
2556}
2557
[email protected]90bb38d2012-11-14 18:36:032558void ExtensionService::OnExtensionInstallPrefChanged() {
[email protected]a6a7ced2012-11-01 17:24:182559 IdentifyAlertableExtensions();
2560 CheckManagementPolicy();
2561}
2562
[email protected]eaa7dd182010-12-14 11:09:002563bool ExtensionService::HasApps() const {
[email protected]ec5b50d2010-10-09 16:35:182564 return !GetAppIds().empty();
2565}
[email protected]377011d2010-07-20 04:18:502566
[email protected]eaa7dd182010-12-14 11:09:002567ExtensionIdSet ExtensionService::GetAppIds() const {
[email protected]ec5b50d2010-10-09 16:35:182568 ExtensionIdSet result;
[email protected]84df8332011-12-06 18:22:462569 for (ExtensionSet::const_iterator it = extensions_.begin();
[email protected]377011d2010-07-20 04:18:502570 it != extensions_.end(); ++it) {
[email protected]1d5e58b2013-01-31 08:41:402571 if ((*it)->is_app() && (*it)->location() != Manifest::COMPONENT)
[email protected]ec5b50d2010-10-09 16:35:182572 result.insert((*it)->id());
[email protected]377011d2010-07-20 04:18:502573 }
2574
[email protected]ec5b50d2010-10-09 16:35:182575 return result;
[email protected]377011d2010-07-20 04:18:502576}
[email protected]d7e9a862010-11-03 21:57:492577
[email protected]dc9a74f72012-08-17 18:07:212578bool ExtensionService::IsBackgroundPageReady(const Extension* extension) const {
[email protected]9367eabc2013-03-01 01:29:292579 if (!extensions::BackgroundInfo::HasPersistentBackgroundPage(extension))
[email protected]dc9a74f72012-08-17 18:07:212580 return true;
2581 ExtensionRuntimeDataMap::const_iterator it =
2582 extension_runtime_data_.find(extension->id());
2583 return it == extension_runtime_data_.end() ? false :
2584 it->second.background_page_ready;
[email protected]d7e9a862010-11-03 21:57:492585}
2586
[email protected]eaa7dd182010-12-14 11:09:002587void ExtensionService::SetBackgroundPageReady(const Extension* extension) {
[email protected]9367eabc2013-03-01 01:29:292588 DCHECK(extensions::BackgroundInfo::HasBackgroundPage(extension));
[email protected]d7e9a862010-11-03 21:57:492589 extension_runtime_data_[extension->id()].background_page_ready = true;
[email protected]ad50def52011-10-19 23:17:072590 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272591 chrome::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY,
[email protected]6c2381d2011-10-19 02:52:532592 content::Source<const Extension>(extension),
[email protected]ad50def52011-10-19 23:17:072593 content::NotificationService::NoDetails());
[email protected]d7e9a862010-11-03 21:57:492594}
2595
[email protected]dc9a74f72012-08-17 18:07:212596bool ExtensionService::IsBeingUpgraded(const Extension* extension) const {
2597 ExtensionRuntimeDataMap::const_iterator it =
2598 extension_runtime_data_.find(extension->id());
2599 return it == extension_runtime_data_.end() ? false :
2600 it->second.being_upgraded;
[email protected]d7e9a862010-11-03 21:57:492601}
2602
[email protected]eaa7dd182010-12-14 11:09:002603void ExtensionService::SetBeingUpgraded(const Extension* extension,
[email protected]b914e2952013-04-26 07:10:032604 bool value) {
[email protected]d7e9a862010-11-03 21:57:492605 extension_runtime_data_[extension->id()].being_upgraded = value;
2606}
[email protected]1bead0712010-11-27 17:41:532607
[email protected]e178ad92013-06-28 02:29:252608bool ExtensionService::IsBeingReloaded(
2609 const std::string& extension_id) const {
2610 return ContainsKey(extensions_being_reloaded_, extension_id);
2611}
2612
2613void ExtensionService::SetBeingReloaded(const std::string& extension_id,
[email protected]5e7015c2013-07-23 23:29:432614 bool isBeingReloaded) {
2615 if (isBeingReloaded)
[email protected]e178ad92013-06-28 02:29:252616 extensions_being_reloaded_.insert(extension_id);
[email protected]5e7015c2013-07-23 23:29:432617 else
[email protected]e178ad92013-06-28 02:29:252618 extensions_being_reloaded_.erase(extension_id);
[email protected]e178ad92013-06-28 02:29:252619}
2620
[email protected]dc9a74f72012-08-17 18:07:212621bool ExtensionService::HasUsedWebRequest(const Extension* extension) const {
2622 ExtensionRuntimeDataMap::const_iterator it =
2623 extension_runtime_data_.find(extension->id());
2624 return it == extension_runtime_data_.end() ? false :
2625 it->second.has_used_webrequest;
[email protected]39a5b532011-10-22 01:47:072626}
2627
2628void ExtensionService::SetHasUsedWebRequest(const Extension* extension,
2629 bool value) {
2630 extension_runtime_data_[extension->id()].has_used_webrequest = value;
2631}
2632
[email protected]612a1cb12012-10-17 13:18:032633bool ExtensionService::ShouldEnableOnInstall(const Extension* extension) {
2634 // Extensions installed by policy can't be disabled. So even if a previous
2635 // installation disabled the extension, make sure it is now enabled.
2636 if (system_->management_policy()->MustRemainEnabled(extension, NULL))
2637 return true;
2638
2639 if (extension_prefs_->IsExtensionDisabled(extension->id()))
2640 return false;
2641
[email protected]41070e8d2012-10-24 01:34:362642 if (FeatureSwitch::prompt_for_external_extensions()->IsEnabled()) {
[email protected]612a1cb12012-10-17 13:18:032643 // External extensions are initially disabled. We prompt the user before
[email protected]2c495c42013-01-04 21:49:542644 // enabling them. Hosted apps are excepted because they are not dangerous
2645 // (they need to be launched by the user anyway).
[email protected]1d5e58b2013-01-31 08:41:402646 if (extension->GetType() != Manifest::TYPE_HOSTED_APP &&
2647 Manifest::IsExternalLocation(extension->location()) &&
[email protected]612a1cb12012-10-17 13:18:032648 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id())) {
2649 return false;
2650 }
2651 }
[email protected]612a1cb12012-10-17 13:18:032652
2653 return true;
2654}
[email protected]0db124b02012-11-07 04:55:052655
2656bool ExtensionService::IsExtensionIdle(const std::string& extension_id) const {
[email protected]98b6d942013-11-10 00:34:072657 extensions::ProcessManager* process_manager = system_->process_manager();
[email protected]fc332ae2012-11-14 20:17:332658 DCHECK(process_manager);
[email protected]0db124b02012-11-07 04:55:052659 extensions::ExtensionHost* host =
[email protected]d9a61e12012-11-14 02:43:472660 process_manager->GetBackgroundHostForExtension(extension_id);
[email protected]e7aa7b7e2012-11-27 04:51:222661 if (host)
2662 return false;
[email protected]52b76592013-11-02 17:59:032663
2664 content::SiteInstance* site_instance = process_manager->GetSiteInstanceForURL(
2665 Extension::GetBaseURLFromExtensionId(extension_id));
2666 if (site_instance && site_instance->HasProcess()) {
2667 return false;
2668 }
2669
[email protected]e7aa7b7e2012-11-27 04:51:222670 return process_manager->GetRenderViewHostsForExtension(extension_id).empty();
2671}
2672
2673bool ExtensionService::ShouldDelayExtensionUpdate(
2674 const std::string& extension_id,
2675 bool wait_for_idle) const {
2676 const char kOnUpdateAvailableEvent[] = "runtime.onUpdateAvailable";
2677
2678 // If delayed updates are globally disabled, or just for this extension,
2679 // don't delay.
2680 if (!install_updates_when_idle_ || !wait_for_idle)
2681 return false;
2682
[email protected]695b5712012-12-06 23:55:282683 const Extension* old = GetInstalledExtension(extension_id);
[email protected]e7aa7b7e2012-11-27 04:51:222684 // If there is no old extension, this is not an update, so don't delay.
2685 if (!old)
2686 return false;
2687
[email protected]9367eabc2013-03-01 01:29:292688 if (extensions::BackgroundInfo::HasPersistentBackgroundPage(old)) {
[email protected]e7aa7b7e2012-11-27 04:51:222689 // Delay installation if the extension listens for the onUpdateAvailable
2690 // event.
2691 return system_->event_router()->ExtensionHasEventListener(
2692 extension_id, kOnUpdateAvailableEvent);
2693 } else {
2694 // Delay installation if the extension is not idle.
2695 return !IsExtensionIdle(extension_id);
2696 }
[email protected]0db124b02012-11-07 04:55:052697}
[email protected]fdd679b2012-11-15 20:49:392698
[email protected]399583b2012-12-11 09:33:422699void ExtensionService::GarbageCollectIsolatedStorage() {
[email protected]650b2d52013-02-10 03:41:452700 scoped_ptr<base::hash_set<base::FilePath> > active_paths(
2701 new base::hash_set<base::FilePath>());
[email protected]399583b2012-12-11 09:33:422702 for (ExtensionSet::const_iterator it = extensions_.begin();
2703 it != extensions_.end(); ++it) {
[email protected]cadac622013-06-11 16:46:362704 if (extensions::AppIsolationInfo::HasIsolatedStorage(it->get())) {
2705 active_paths->insert(BrowserContext::GetStoragePartitionForSite(
2706 profile_, GetSiteForExtensionId((*it)->id()))->GetPath());
[email protected]399583b2012-12-11 09:33:422707 }
2708 }
2709
[email protected]9f4e4f082013-06-21 07:11:192710 DCHECK(!installs_delayed_for_gc());
2711 set_installs_delayed_for_gc(true);
[email protected]399583b2012-12-11 09:33:422712 BrowserContext::GarbageCollectStoragePartitions(
2713 profile_, active_paths.Pass(),
2714 base::Bind(&ExtensionService::OnGarbageCollectIsolatedStorageFinished,
2715 AsWeakPtr()));
2716}
2717
2718void ExtensionService::OnGarbageCollectIsolatedStorageFinished() {
[email protected]9f4e4f082013-06-21 07:11:192719 set_installs_delayed_for_gc(false);
2720 MaybeFinishDelayedInstallations();
2721}
2722
2723void ExtensionService::MaybeFinishDelayedInstallations() {
2724 std::vector<std::string> to_be_installed;
[email protected]399583b2012-12-11 09:33:422725 for (ExtensionSet::const_iterator it = delayed_installs_.begin();
2726 it != delayed_installs_.end();
2727 ++it) {
[email protected]9f4e4f082013-06-21 07:11:192728 to_be_installed.push_back((*it)->id());
[email protected]399583b2012-12-11 09:33:422729 }
[email protected]9f4e4f082013-06-21 07:11:192730 for (std::vector<std::string>::const_iterator it = to_be_installed.begin();
2731 it != to_be_installed.end();
[email protected]399583b2012-12-11 09:33:422732 ++it) {
[email protected]9f4e4f082013-06-21 07:11:192733 MaybeFinishDelayedInstallation(*it);
[email protected]399583b2012-12-11 09:33:422734 }
[email protected]399583b2012-12-11 09:33:422735}
2736
2737void ExtensionService::OnNeedsToGarbageCollectIsolatedStorage() {
2738 extension_prefs_->SetNeedsStorageGarbageCollection(true);
2739}
2740
[email protected]fdd679b2012-11-15 20:49:392741void ExtensionService::OnBlacklistUpdated() {
[email protected]48a359342013-10-30 00:22:002742 blacklist_->GetMalwareIDs(
[email protected]695b5712012-12-06 23:55:282743 GenerateInstalledExtensionsSet()->GetIDs(),
[email protected]3f2a2fa2013-09-24 02:55:252744 base::Bind(&ExtensionService::ManageBlacklist, AsWeakPtr()));
[email protected]695b5712012-12-06 23:55:282745}
2746
[email protected]3f2a2fa2013-09-24 02:55:252747void ExtensionService::ManageBlacklist(const std::set<std::string>& updated) {
[email protected]695b5712012-12-06 23:55:282748 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2749
[email protected]3f2a2fa2013-09-24 02:55:252750 std::set<std::string> before = blacklisted_extensions_.GetIDs();
[email protected]16bf7ba72013-08-23 11:52:542751 std::set<std::string> no_longer_blacklisted =
[email protected]3f2a2fa2013-09-24 02:55:252752 base::STLSetDifference<std::set<std::string> >(before, updated);
[email protected]16bf7ba72013-08-23 11:52:542753 std::set<std::string> not_yet_blacklisted =
[email protected]3f2a2fa2013-09-24 02:55:252754 base::STLSetDifference<std::set<std::string> >(updated, before);
[email protected]695b5712012-12-06 23:55:282755
2756 for (std::set<std::string>::iterator it = no_longer_blacklisted.begin();
2757 it != no_longer_blacklisted.end(); ++it) {
2758 scoped_refptr<const Extension> extension =
2759 blacklisted_extensions_.GetByID(*it);
[email protected]3f2a2fa2013-09-24 02:55:252760 if (!extension.get()) {
2761 NOTREACHED() << "Extension " << *it << " no longer blacklisted, "
2762 << "but it was never blacklisted.";
[email protected]695b5712012-12-06 23:55:282763 continue;
[email protected]3f2a2fa2013-09-24 02:55:252764 }
[email protected]695b5712012-12-06 23:55:282765 blacklisted_extensions_.Remove(*it);
[email protected]3f2a2fa2013-09-24 02:55:252766 extension_prefs_->SetExtensionBlacklisted(extension->id(), false);
[email protected]dc24976f2013-06-02 21:15:092767 AddExtension(extension.get());
[email protected]ac875372013-02-28 04:36:092768 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.UnblacklistInstalled",
[email protected]dc24976f2013-06-02 21:15:092769 extension->location(),
2770 Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:282771 }
2772
2773 for (std::set<std::string>::iterator it = not_yet_blacklisted.begin();
2774 it != not_yet_blacklisted.end(); ++it) {
2775 scoped_refptr<const Extension> extension = GetInstalledExtension(*it);
[email protected]3f2a2fa2013-09-24 02:55:252776 if (!extension.get()) {
2777 NOTREACHED() << "Extension " << *it << " needs to be "
2778 << "blacklisted, but it's not installed.";
[email protected]695b5712012-12-06 23:55:282779 continue;
[email protected]3f2a2fa2013-09-24 02:55:252780 }
[email protected]695b5712012-12-06 23:55:282781 blacklisted_extensions_.Insert(extension);
[email protected]3f2a2fa2013-09-24 02:55:252782 extension_prefs_->SetExtensionBlacklisted(extension->id(), true);
[email protected]b0af4792013-10-23 09:12:132783 UnloadExtension(*it, UnloadedExtensionInfo::REASON_BLACKLIST);
[email protected]ac875372013-02-28 04:36:092784 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.BlacklistInstalled",
2785 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:282786 }
2787
2788 IdentifyAlertableExtensions();
[email protected]fdd679b2012-11-15 20:49:392789}
[email protected]75bdcb872013-03-13 00:41:452790
2791void ExtensionService::AddUpdateObserver(extensions::UpdateObserver* observer) {
2792 update_observers_.AddObserver(observer);
2793}
2794
2795void ExtensionService::RemoveUpdateObserver(
2796 extensions::UpdateObserver* observer) {
2797 update_observers_.RemoveObserver(observer);
2798}