blob: 80f893b0b614e4791c576bdce1a559122c2c7672 [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]aa142702010-03-26 01:26:3327#include "base/version.h"
[email protected]15730c42009-09-03 00:03:2028#include "chrome/browser/browser_process.h"
[email protected]9ea0cd32013-07-12 01:50:3629#include "chrome/browser/chrome_notification_types.h"
[email protected]ad445762013-01-23 00:47:4430#include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
[email protected]d8c8f25f2011-11-02 18:18:0131#include "chrome/browser/extensions/component_loader.h"
[email protected]e0785902011-05-19 23:34:1732#include "chrome/browser/extensions/crx_installer.h"
[email protected]d9ede582012-08-14 19:21:3833#include "chrome/browser/extensions/data_deleter.h"
[email protected]62f051c2012-03-29 17:04:4434#include "chrome/browser/extensions/extension_disabled_ui.h"
[email protected]14a000d2010-04-29 21:44:2435#include "chrome/browser/extensions/extension_error_reporter.h"
[email protected]89226982012-07-16 20:09:1836#include "chrome/browser/extensions/extension_error_ui.h"
[email protected]b1748b1d82009-11-30 20:32:5637#include "chrome/browser/extensions/extension_host.h"
[email protected]00b38242012-07-18 18:43:2238#include "chrome/browser/extensions/extension_install_ui.h"
[email protected]19eb80152011-02-26 00:28:4339#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]f8aefb132013-10-30 09:29:5240#include "chrome/browser/extensions/extension_sync_service.h"
[email protected]617342a42013-12-18 23:34:0341#include "chrome/browser/extensions/extension_util.h"
[email protected]612a1cb12012-10-17 13:18:0342#include "chrome/browser/extensions/external_install_ui.h"
[email protected]5df038b2012-07-16 19:03:2743#include "chrome/browser/extensions/external_provider_impl.h"
[email protected]ffd2f79e2013-11-14 00:11:4644#include "chrome/browser/extensions/install_verifier.h"
[email protected]d8c8f25f2011-11-02 18:18:0145#include "chrome/browser/extensions/installed_loader.h"
[email protected]f3d3b382014-03-14 21:19:2846#include "chrome/browser/extensions/pending_extension_manager.h"
[email protected]c333e792012-01-06 16:57:3947#include "chrome/browser/extensions/permissions_updater.h"
[email protected]d8c8f25f2011-11-02 18:18:0148#include "chrome/browser/extensions/unpacked_installer.h"
[email protected]044e86992014-01-24 22:59:1149#include "chrome/browser/extensions/updater/extension_cache.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]15d4d2d2013-08-09 06:49:0360#include "chrome/common/extensions/features/feature_channel.h"
[email protected]561e33d52013-04-03 06:58:4361#include "chrome/common/extensions/manifest_handlers/app_isolation_info.h"
[email protected]65348062013-01-15 07:27:2262#include "chrome/common/extensions/manifest_url_handler.h"
[email protected]25b34332009-06-05 21:53:1963#include "chrome/common/pref_names.h"
[email protected]a57209872009-05-04 22:53:1464#include "chrome/common/url_constants.h"
[email protected]7a83dd072013-09-03 12:09:1765#include "components/startup_metric_utils/startup_metric_utils.h"
[email protected]c38831a12011-10-28 12:44:4966#include "content/public/browser/browser_thread.h"
[email protected]98f66112012-12-25 12:59:3667#include "content/public/browser/devtools_agent_host.h"
[email protected]05aad2da2011-10-28 10:12:3768#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1669#include "content/public/browser/notification_types.h"
[email protected]f3b1a082011-11-18 00:34:3070#include "content/public/browser/render_process_host.h"
[email protected]399583b2012-12-11 09:33:4271#include "content/public/browser/storage_partition.h"
[email protected]24ea7a12013-01-27 23:54:5372#include "content/public/browser/url_data_source.h"
[email protected]74474042013-11-21 12:03:5473#include "extensions/browser/app_sorting.h"
[email protected]34423532013-11-21 18:13:1074#include "extensions/browser/event_router.h"
[email protected]bb1bc9b32013-12-21 03:09:1475#include "extensions/browser/extension_registry.h"
[email protected]59b0e602014-01-30 00:41:2476#include "extensions/browser/extension_system.h"
[email protected]45928b682013-11-28 08:20:2777#include "extensions/browser/extensions_browser_client.h"
[email protected]301116c62013-11-26 10:37:4578#include "extensions/browser/external_provider_interface.h"
79#include "extensions/browser/management_policy.h"
[email protected]234fc5ff2014-01-16 23:32:2880#include "extensions/browser/pref_names.h"
[email protected]98b6d942013-11-10 00:34:0781#include "extensions/browser/process_manager.h"
[email protected]fafdc842014-01-17 18:09:0882#include "extensions/browser/process_map.h"
[email protected]45f5b7d2014-01-22 23:47:1383#include "extensions/browser/runtime_data.h"
[email protected]a9aa62b312013-11-29 05:35:0684#include "extensions/browser/update_observer.h"
[email protected]836e2982013-05-16 08:07:4285#include "extensions/common/constants.h"
[email protected]e9f541a2012-11-19 21:52:3186#include "extensions/common/error_utils.h"
[email protected]e4452d32013-11-15 23:07:4187#include "extensions/common/extension.h"
[email protected]fb820c02014-03-13 15:07:0888#include "extensions/common/extension_messages.h"
[email protected]c81f5d32013-10-26 10:38:4289#include "extensions/common/extensions_client.h"
[email protected]5ef835a2013-11-08 20:42:5790#include "extensions/common/feature_switch.h"
[email protected]d42c11152013-08-22 19:36:3291#include "extensions/common/manifest.h"
[email protected]0c3c9732013-09-16 08:53:4192#include "extensions/common/manifest_constants.h"
[email protected]558878cc82013-11-09 01:25:5193#include "extensions/common/manifest_handlers/background_info.h"
[email protected]1f7de252013-11-06 22:02:0094#include "extensions/common/manifest_handlers/incognito_info.h"
[email protected]301116c62013-11-26 10:37:4595#include "extensions/common/manifest_handlers/shared_module_info.h"
[email protected]c41003472013-10-19 15:37:2596#include "extensions/common/permissions/permission_message_provider.h"
[email protected]e4452d32013-11-15 23:07:4197#include "extensions/common/permissions/permissions_data.h"
[email protected]65187152012-06-02 13:14:1498#include "grit/generated_resources.h"
[email protected]be28b5f42012-07-20 11:31:2599#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
[email protected]cd67ed52013-10-15 01:22:13100#include "ui/base/webui/web_ui_util.h"
[email protected]a6483d22013-07-03 22:11:00101#include "url/gurl.h"
[email protected]08b1f75f2013-05-22 22:02:38102#include "webkit/browser/database/database_tracker.h"
103#include "webkit/browser/database/database_util.h"
[email protected]79a60642012-10-20 21:03:18104
[email protected]eed367e2011-04-12 03:43:31105#if defined(OS_CHROMEOS)
[email protected]88e8ec9152013-01-17 04:05:18106#include "chrome/browser/chromeos/extensions/install_limiter.h"
[email protected]f19bbf62013-07-09 01:22:32107#include "webkit/browser/fileapi/file_system_backend.h"
[email protected]c6f9203a2013-05-28 02:08:07108#include "webkit/browser/fileapi/file_system_context.h"
[email protected]eed367e2011-04-12 03:43:31109#endif
110
[email protected]55eb70e762012-02-20 17:38:39111using content::BrowserContext;
[email protected]631bb742011-11-02 11:29:39112using content::BrowserThread;
[email protected]98f66112012-12-25 12:59:36113using content::DevToolsAgentHost;
[email protected]bf3d9df2012-07-24 23:20:27114using extensions::CrxInstaller;
[email protected]1c321ee52012-05-21 03:02:34115using extensions::Extension;
116using extensions::ExtensionIdSet;
117using extensions::ExtensionInfo;
[email protected]599539802014-01-07 23:06:00118using extensions::ExtensionRegistry;
[email protected]289c44b2013-12-17 03:26:57119using extensions::ExtensionSet;
[email protected]215a7be2012-10-22 19:53:42120using extensions::FeatureSwitch;
[email protected]ffd2f79e2013-11-14 00:11:46121using extensions::InstallVerifier;
[email protected]0d54b682013-11-05 14:15:36122using extensions::ManagementPolicy;
[email protected]1d5e58b2013-01-31 08:41:40123using extensions::Manifest;
[email protected]c2e66e12012-06-27 06:27:06124using extensions::PermissionMessage;
125using extensions::PermissionMessages;
126using extensions::PermissionSet;
[email protected]9f4e4f082013-06-21 07:11:19127using extensions::SharedModuleInfo;
[email protected]e410b5f2012-12-14 14:02:24128using extensions::UnloadedExtensionInfo;
[email protected]5ef47ec2010-01-28 05:58:05129
[email protected]0c3c9732013-09-16 08:53:41130namespace errors = extensions::manifest_errors;
[email protected]c6d474f82009-12-16 21:11:06131
[email protected]b6ab96d2009-08-20 18:58:19132namespace {
133
[email protected]d96eb512012-11-01 23:44:08134// Histogram values for logging events related to externally installed
135// extensions.
136enum ExternalExtensionEvent {
137 EXTERNAL_EXTENSION_INSTALLED = 0,
138 EXTERNAL_EXTENSION_IGNORED,
139 EXTERNAL_EXTENSION_REENABLED,
140 EXTERNAL_EXTENSION_UNINSTALLED,
141 EXTERNAL_EXTENSION_BUCKET_BOUNDARY,
142};
143
[email protected]612a1cb12012-10-17 13:18:03144// Prompt the user this many times before considering an extension acknowledged.
145static const int kMaxExtensionAcknowledgePromptCount = 3;
146
[email protected]0db124b02012-11-07 04:55:05147// Wait this many seconds after an extensions becomes idle before updating it.
148static const int kUpdateIdleDelay = 5;
149
[email protected]9bd9a6862012-11-29 09:24:22150// Wait this many seconds before trying to garbage collect extensions again.
151static const int kGarbageCollectRetryDelay = 30;
152
[email protected]36fc54f2013-06-14 02:49:34153// Wait this many seconds after startup to see if there are any extensions
154// which can be garbage collected.
155static const int kGarbageCollectStartupDelay = 30;
156
[email protected]9f4e4f082013-06-21 07:11:19157static bool IsSharedModule(const Extension* extension) {
158 return SharedModuleInfo::IsSharedModule(extension);
159}
160
[email protected]4c9201c42013-08-16 04:56:21161static bool IsCWSSharedModule(const Extension* extension) {
162 return extension->from_webstore() && IsSharedModule(extension);
163}
164
165class SharedModuleProvider : public extensions::ManagementPolicy::Provider {
166 public:
167 SharedModuleProvider() {}
168 virtual ~SharedModuleProvider() {}
169
170 virtual std::string GetDebugPolicyProviderName() const OVERRIDE {
171 return "SharedModuleProvider";
172 }
173
174 virtual bool UserMayModifySettings(const Extension* extension,
[email protected]439f1e32013-12-09 20:09:09175 base::string16* error) const OVERRIDE {
[email protected]4c9201c42013-08-16 04:56:21176 return !IsCWSSharedModule(extension);
177 }
178
179 virtual bool MustRemainEnabled(const Extension* extension,
[email protected]439f1e32013-12-09 20:09:09180 base::string16* error) const OVERRIDE {
[email protected]4c9201c42013-08-16 04:56:21181 return IsCWSSharedModule(extension);
182 }
183
184 private:
185 DISALLOW_COPY_AND_ASSIGN(SharedModuleProvider);
186};
187
[email protected]74564482014-01-30 13:55:42188enum VerifyAllSuccess {
189 VERIFY_ALL_BOOTSTRAP_SUCCESS = 0,
190 VERIFY_ALL_BOOTSTRAP_FAILURE,
191 VERIFY_ALL_NON_BOOTSTRAP_SUCCESS,
192 VERIFY_ALL_NON_BOOTSTRAP_FAILURE,
193
194 // Used in histograms. Do not remove/reorder any entries above, and the below
195 // MAX entry should always come last.
196
197 VERIFY_ALL_SUCCESS_MAX
198};
199
200void LogVerifyAllSuccessHistogram(bool bootstrap, bool success) {
201 VerifyAllSuccess result;
202 if (bootstrap && success)
203 result = VERIFY_ALL_BOOTSTRAP_SUCCESS;
204 else if (bootstrap && !success)
205 result = VERIFY_ALL_BOOTSTRAP_FAILURE;
206 else if (!bootstrap && success)
207 result = VERIFY_ALL_NON_BOOTSTRAP_SUCCESS;
208 else
209 result = VERIFY_ALL_NON_BOOTSTRAP_FAILURE;
210
211 UMA_HISTOGRAM_ENUMERATION("ExtensionService.VerifyAllSuccess",
212 result, VERIFY_ALL_SUCCESS_MAX);
213}
214
215void LogAddVerifiedSuccess(bool success) {
216 UMA_HISTOGRAM_BOOLEAN("ExtensionService.AddVerified", success);
217}
[email protected]4c9201c42013-08-16 04:56:21218
[email protected]c6d474f82009-12-16 21:11:06219} // namespace
[email protected]b6ab96d2009-08-20 18:58:19220
[email protected]eaa7dd182010-12-14 11:09:00221// ExtensionService.
[email protected]6014d672008-12-05 00:38:25222
[email protected]8e4560b62011-01-14 10:09:14223void ExtensionService::CheckExternalUninstall(const std::string& id) {
[email protected]a29a517a2011-01-21 21:11:12224 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20225
[email protected]7a4c6852010-09-16 03:44:22226 // Check if the providers know about this extension.
[email protected]5df038b2012-07-16 19:03:27227 extensions::ProviderCollection::const_iterator i;
[email protected]0a60a2e2010-10-25 16:15:21228 for (i = external_extension_providers_.begin();
229 i != external_extension_providers_.end(); ++i) {
[email protected]8e4560b62011-01-14 10:09:14230 DCHECK(i->get()->IsReady());
[email protected]0a60a2e2010-10-25 16:15:21231 if (i->get()->HasExtension(id))
232 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22233 }
234
[email protected]0f48fca2011-05-19 18:46:35235 // We get the list of external extensions to check from preferences.
236 // It is possible that an extension has preferences but is not loaded.
237 // For example, an extension that requires experimental permissions
238 // will not be loaded if the experimental command line flag is not used.
239 // In this case, do not uninstall.
[email protected]31bb5ee62012-09-12 22:58:40240 if (!GetInstalledExtension(id)) {
[email protected]0f48fca2011-05-19 18:46:35241 // We can't call UninstallExtension with an unloaded/invalid
242 // extension ID.
243 LOG(WARNING) << "Attempted uninstallation of unloaded/invalid extension "
244 << "with id: " << id;
245 return;
246 }
[email protected]d6ebc9792011-04-07 18:18:33247 UninstallExtension(id, true, NULL);
[email protected]7a4c6852010-09-16 03:44:22248}
249
[email protected]7f8f24f2012-11-15 19:40:14250void ExtensionService::SetFileTaskRunnerForTesting(
251 base::SequencedTaskRunner* task_runner) {
252 file_task_runner_ = task_runner;
253}
254
[email protected]8e4560b62011-01-14 10:09:14255void ExtensionService::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22256 external_extension_providers_.clear();
257}
258
[email protected]8e4560b62011-01-14 10:09:14259void ExtensionService::AddProviderForTesting(
[email protected]5df038b2012-07-16 19:03:27260 extensions::ExternalProviderInterface* test_provider) {
[email protected]a29a517a2011-01-21 21:11:12261 CHECK(test_provider);
[email protected]0a60a2e2010-10-25 16:15:21262 external_extension_providers_.push_back(
[email protected]5df038b2012-07-16 19:03:27263 linked_ptr<extensions::ExternalProviderInterface>(test_provider));
[email protected]7a4c6852010-09-16 03:44:22264}
265
[email protected]9060d8b02012-01-13 02:14:30266bool ExtensionService::OnExternalExtensionUpdateUrlFound(
[email protected]7a4c6852010-09-16 03:44:22267 const std::string& id,
[email protected]21a5a672010-11-04 10:47:42268 const GURL& update_url,
[email protected]464213a2013-10-15 01:06:48269 Manifest::Location location,
270 int creation_flags,
271 bool mark_acknowledged) {
[email protected]ab22ba42011-01-14 16:36:38272 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
273 CHECK(Extension::IdIsValid(id));
[email protected]a8af9fdb2010-10-28 21:52:20274
[email protected]044e86992014-01-24 22:59:11275 if (Manifest::IsExternalLocation(location)) {
276 // All extensions that are not user specific can be cached.
277 extensions::ExtensionCache::GetInstance()->AllowCaching(id);
278 }
279
[email protected]8a87a5332011-08-11 17:54:59280 const Extension* extension = GetExtensionById(id, true);
281 if (extension) {
282 // Already installed. Skip this install if the current location has
283 // higher priority than |location|.
[email protected]1d5e58b2013-01-31 08:41:40284 Manifest::Location current = extension->location();
285 if (current == Manifest::GetHigherPriorityLocation(current, location))
[email protected]9060d8b02012-01-13 02:14:30286 return false;
[email protected]8a87a5332011-08-11 17:54:59287 // Otherwise, overwrite the current installation.
[email protected]7a4c6852010-09-16 03:44:22288 }
[email protected]9060d8b02012-01-13 02:14:30289
290 // Add |id| to the set of pending extensions. If it can not be added,
291 // then there is already a pending record from a higher-priority install
292 // source. In this case, signal that this extension will not be
293 // installed by returning false.
294 if (!pending_extension_manager()->AddFromExternalUpdateUrl(
[email protected]464213a2013-10-15 01:06:48295 id, update_url, location, creation_flags, mark_acknowledged)) {
[email protected]9060d8b02012-01-13 02:14:30296 return false;
[email protected]31bb5ee62012-09-12 22:58:40297 }
[email protected]9060d8b02012-01-13 02:14:30298
[email protected]94fde232012-04-27 10:22:30299 update_once_all_providers_are_ready_ = true;
[email protected]9060d8b02012-01-13 02:14:30300 return true;
[email protected]7a4c6852010-09-16 03:44:22301}
302
[email protected]400c4392013-12-13 14:41:58303const Extension* ExtensionService::GetInstalledExtensionByUrl(
304 const GURL& url) const {
[email protected]bb1bc9b32013-12-21 03:09:14305 return registry_->enabled_extensions().GetExtensionOrAppByURL(url);
[email protected]400c4392013-12-13 14:41:58306}
307
[email protected]dc9a74f72012-08-17 18:07:21308const Extension* ExtensionService::GetInstalledApp(const GURL& url) const {
[email protected]400c4392013-12-13 14:41:58309 const Extension* extension = GetInstalledExtensionByUrl(url);
[email protected]dc9a74f72012-08-17 18:07:21310 return (extension && extension->is_app()) ? extension : NULL;
[email protected]d9696672011-03-15 22:45:09311}
312
[email protected]dc9a74f72012-08-17 18:07:21313bool ExtensionService::IsInstalledApp(const GURL& url) const {
[email protected]d9696672011-03-15 22:45:09314 return !!GetInstalledApp(url);
[email protected]ffb204f22010-12-05 23:20:27315}
316
[email protected]6aeac8342010-10-01 20:21:18317// static
[email protected]d6ebc9792011-04-07 18:18:33318// This function is used to implement the command-line switch
[email protected]3bdba0d2011-08-23 07:17:30319// --uninstall-extension, and to uninstall an extension via sync. The LOG
320// statements within this function are used to inform the user if the uninstall
321// cannot be done.
[email protected]eaa7dd182010-12-14 11:09:00322bool ExtensionService::UninstallExtensionHelper(
323 ExtensionService* extensions_service,
[email protected]6aeac8342010-10-01 20:21:18324 const std::string& extension_id) {
[email protected]d6ebc9792011-04-07 18:18:33325 // We can't call UninstallExtension with an invalid extension ID.
[email protected]31bb5ee62012-09-12 22:58:40326 if (!extensions_service->GetInstalledExtension(extension_id)) {
[email protected]6aeac8342010-10-01 20:21:18327 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]95da88c42011-03-31 10:07:33328 << "id: " << extension_id;
329 return false;
[email protected]6aeac8342010-10-01 20:21:18330 }
331
[email protected]d6ebc9792011-04-07 18:18:33332 // The following call to UninstallExtension will not allow an uninstall of a
333 // policy-controlled extension.
[email protected]439f1e32013-12-09 20:09:09334 base::string16 error;
[email protected]d6ebc9792011-04-07 18:18:33335 if (!extensions_service->UninstallExtension(extension_id, false, &error)) {
336 LOG(WARNING) << "Cannot uninstall extension with id " << extension_id
337 << ": " << error;
338 return false;
339 }
[email protected]95da88c42011-03-31 10:07:33340
[email protected]6aeac8342010-10-01 20:21:18341 return true;
342}
343
[email protected]eaa7dd182010-12-14 11:09:00344ExtensionService::ExtensionService(Profile* profile,
[email protected]f0841cd2011-01-19 15:07:24345 const CommandLine* command_line,
[email protected]650b2d52013-02-10 03:41:45346 const base::FilePath& install_directory,
[email protected]45759612012-07-10 17:21:23347 extensions::ExtensionPrefs* extension_prefs,
[email protected]fdd679b2012-11-15 20:49:39348 extensions::Blacklist* blacklist,
[email protected]0436b102011-04-15 18:30:03349 bool autoupdate_enabled,
[email protected]4a10006a2013-05-17 23:18:35350 bool extensions_enabled,
351 extensions::OneShotEvent* ready)
[email protected]fdd679b2012-11-15 20:49:39352 : extensions::Blacklist::Observer(blacklist),
353 profile_(profile),
[email protected]bd306722012-07-11 20:43:59354 system_(extensions::ExtensionSystem::Get(profile)),
[email protected]73c47932010-12-06 18:13:43355 extension_prefs_(extension_prefs),
[email protected]695b5712012-12-06 23:55:28356 blacklist_(blacklist),
[email protected]f8aefb132013-10-30 09:29:52357 extension_sync_service_(NULL),
[email protected]5fdfa562013-12-27 17:43:59358 registry_(extensions::ExtensionRegistry::Get(profile)),
[email protected]9c009092013-05-01 03:14:09359 pending_extension_manager_(*this),
[email protected]a9b00ac2009-06-25 21:03:23360 install_directory_(install_directory),
[email protected]0436b102011-04-15 18:30:03361 extensions_enabled_(extensions_enabled),
[email protected]e81dba32009-06-19 20:19:13362 show_extensions_prompts_(true),
[email protected]fc332ae2012-11-14 20:17:33363 install_updates_when_idle_(true),
[email protected]4a10006a2013-05-17 23:18:35364 ready_(ready),
[email protected]dff1e042012-04-27 10:59:18365 update_once_all_providers_are_ready_(false),
[email protected]3c4abc82012-10-22 22:25:54366 browser_terminating_(false),
[email protected]9f4e4f082013-06-21 07:11:19367 installs_delayed_for_gc_(false),
[email protected]f8aefb132013-10-30 09:29:52368 is_first_run_(false) {
[email protected]c04fd3dd2013-09-05 08:20:10369#if defined(OS_CHROMEOS)
370 disable_garbage_collection_ = false;
371#endif
[email protected]a29a517a2011-01-21 21:11:12372 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20373
[email protected]36a784c2009-06-23 06:21:08374 // Figure out if extension installation should be enabled.
[email protected]367d9b172013-12-03 00:31:02375 if (extensions::ExtensionsBrowserClient::Get()->AreExtensionsDisabled(
376 *command_line, profile))
[email protected]6d60703b2009-08-29 01:29:23377 extensions_enabled_ = false;
[email protected]36a784c2009-06-23 06:21:08378
[email protected]3c4abc82012-10-22 22:25:54379 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING,
380 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27381 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07382 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27383 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07384 content::NotificationService::AllBrowserContextsAndSources());
[email protected]92dd8d92013-02-26 00:41:08385 registrar_.Add(this, chrome::NOTIFICATION_UPGRADE_RECOMMENDED,
386 content::NotificationService::AllBrowserContextsAndSources());
[email protected]2fb7dc982010-09-29 12:24:28387 pref_change_registrar_.Init(profile->GetPrefs());
[email protected]90bb38d2012-11-14 18:36:03388 base::Closure callback =
389 base::Bind(&ExtensionService::OnExtensionInstallPrefChanged,
390 base::Unretained(this));
[email protected]234fc5ff2014-01-16 23:32:28391 pref_change_registrar_.Add(extensions::pref_names::kInstallAllowList,
392 callback);
393 pref_change_registrar_.Add(extensions::pref_names::kInstallDenyList,
394 callback);
395 pref_change_registrar_.Add(extensions::pref_names::kAllowedTypes, callback);
[email protected]4814b512009-11-07 00:12:29396
[email protected]93fd78f42009-07-10 16:43:17397 // Set up the ExtensionUpdater
398 if (autoupdate_enabled) {
[email protected]501105b2013-09-26 05:42:02399 int update_frequency = extensions::kDefaultUpdateFrequencySeconds;
[email protected]93fd78f42009-07-10 16:43:17400 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25401 base::StringToInt(command_line->GetSwitchValueASCII(
402 switches::kExtensionsUpdateFrequency),
403 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17404 }
[email protected]044e86992014-01-24 22:59:11405 updater_.reset(new extensions::ExtensionUpdater(
406 this,
407 extension_prefs,
408 profile->GetPrefs(),
409 profile,
410 update_frequency,
411 extensions::ExtensionCache::GetInstance()));
[email protected]93fd78f42009-07-10 16:43:17412 }
413
[email protected]25ae0152011-11-18 14:40:02414 component_loader_.reset(
415 new extensions::ComponentLoader(this,
416 profile->GetPrefs(),
[email protected]55e16cd2013-12-18 04:36:08417 g_browser_process->local_state(),
418 profile));
[email protected]8e4560b62011-01-14 10:09:14419
[email protected]0436b102011-04-15 18:30:03420 if (extensions_enabled_) {
[email protected]ae4c37e2012-12-21 01:16:25421 extensions::ExternalProviderImpl::CreateExternalProviders(
422 this, profile_, &external_extension_providers_);
[email protected]873531342011-03-09 12:16:05423 }
[email protected]b671760b2010-07-15 21:13:47424
[email protected]74474042013-11-21 12:03:54425 // Set this as the ExtensionService for app sorting to ensure it causes syncs
426 // if required.
[email protected]b3aa7182013-04-25 04:45:23427 is_first_run_ = !extension_prefs_->SetAlertSystemFirstRun();
428
[email protected]9ce11d22012-08-08 23:20:13429#if defined(ENABLE_EXTENSIONS)
[email protected]c77f2352012-08-08 22:07:58430 extension_action_storage_manager_.reset(
431 new extensions::ExtensionActionStorageManager(profile_));
[email protected]9ce11d22012-08-08 23:20:13432#endif
[email protected]c77f2352012-08-08 22:07:58433
[email protected]4c9201c42013-08-16 04:56:21434 shared_module_policy_provider_.reset(new SharedModuleProvider);
435
[email protected]cb0e50312011-05-09 15:03:07436 // How long is the path to the Extensions directory?
437 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ExtensionRootPathLength",
438 install_directory_.value().length(), 0, 500, 100);
[email protected]6014d672008-12-05 00:38:25439}
440
[email protected]84df8332011-12-06 18:22:46441const ExtensionSet* ExtensionService::extensions() const {
[email protected]bb1bc9b32013-12-21 03:09:14442 return &registry_->enabled_extensions();
[email protected]ddb1e5a2010-12-13 20:10:45443}
444
[email protected]9e9c1d12013-07-31 01:58:12445const ExtensionSet* ExtensionService::delayed_installs() const {
446 return &delayed_installs_;
447}
448
[email protected]3f213ad2012-07-26 23:39:41449extensions::PendingExtensionManager*
450 ExtensionService::pending_extension_manager() {
[email protected]b2907fd2011-03-25 16:43:37451 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45452}
453
[email protected]eaa7dd182010-12-14 11:09:00454ExtensionService::~ExtensionService() {
[email protected]c8d407e2011-04-28 21:27:17455 // No need to unload extensions here because they are profile-scoped, and the
456 // profile is in the process of being deleted.
[email protected]94b4ffc2011-02-04 05:55:17457
[email protected]5df038b2012-07-16 19:03:27458 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:14459 for (i = external_extension_providers_.begin();
460 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:27461 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:14462 provider->ServiceShutdown();
463 }
[email protected]6014d672008-12-05 00:38:25464}
465
[email protected]037228a2012-10-05 01:36:16466void ExtensionService::Shutdown() {
[email protected]4c9201c42013-08-16 04:56:21467 system_->management_policy()->UnregisterProvider(
468 shared_module_policy_provider_.get());
[email protected]037228a2012-10-05 01:36:16469}
470
[email protected]b2907fd2011-03-25 16:43:37471const Extension* ExtensionService::GetExtensionById(
472 const std::string& id, bool include_disabled) const {
[email protected]599539802014-01-07 23:06:00473 int include_mask = ExtensionRegistry::ENABLED;
[email protected]695b5712012-12-06 23:55:28474 if (include_disabled) {
475 // Include blacklisted extensions here because there are hundreds of
476 // callers of this function, and many might assume that this includes those
477 // that have been disabled due to blacklisting.
[email protected]599539802014-01-07 23:06:00478 include_mask |= ExtensionRegistry::DISABLED |
479 ExtensionRegistry::BLACKLISTED;
[email protected]695b5712012-12-06 23:55:28480 }
[email protected]599539802014-01-07 23:06:00481 return registry_->GetExtensionById(id, include_mask);
[email protected]695b5712012-12-06 23:55:28482}
483
[email protected]eaa7dd182010-12-14 11:09:00484void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12485 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20486
[email protected]3350d712013-11-18 09:32:24487 base::Time begin_time = base::Time::Now();
488
[email protected]4a10006a2013-05-17 23:18:35489 DCHECK(!is_ready()); // Can't redo init.
[email protected]bb1bc9b32013-12-21 03:09:14490 DCHECK_EQ(registry_->enabled_extensions().size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32491
[email protected]820d9bd2013-04-03 03:46:03492 const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
[email protected]820d9bd2013-04-03 03:46:03493 if (cmd_line->HasSwitch(switches::kInstallFromWebstore) ||
494 cmd_line->HasSwitch(switches::kLimitedInstallFromWebstore)) {
495 // The sole purpose of this launch is to install a new extension from CWS
496 // and immediately terminate: loading already installed extensions is
497 // unnecessary and may interfere with the inline install dialog (e.g. if an
498 // extension listens to onStartup and opens a window).
499 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25500 } else {
[email protected]47b60072013-06-20 10:35:56501 // LoadAllExtensions() calls OnLoadedInstalledExtensions().
502 component_loader_->LoadAll();
503 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]9f4e4f082013-06-21 07:11:19504
[email protected]59ee99d2013-10-11 15:46:16505 ReconcileKnownDisabled();
506
[email protected]2edd57852013-08-14 22:07:40507 // Attempt to re-enable extensions whose only disable reason is reloading.
508 std::vector<std::string> extensions_to_enable;
[email protected]bb1bc9b32013-12-21 03:09:14509 const ExtensionSet& disabled_extensions = registry_->disabled_extensions();
510 for (ExtensionSet::const_iterator iter = disabled_extensions.begin();
511 iter != disabled_extensions.end(); ++iter) {
[email protected]2edd57852013-08-14 22:07:40512 const Extension* e = iter->get();
513 if (extension_prefs_->GetDisableReasons(e->id()) ==
514 Extension::DISABLE_RELOAD) {
515 extensions_to_enable.push_back(e->id());
516 }
517 }
518 for (std::vector<std::string>::iterator it = extensions_to_enable.begin();
519 it != extensions_to_enable.end(); ++it) {
520 EnableExtension(*it);
521 }
522
[email protected]9f4e4f082013-06-21 07:11:19523 // Finish install (if possible) of extensions that were still delayed while
524 // the browser was shut down.
525 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> delayed_info(
526 extension_prefs_->GetAllDelayedInstallInfo());
527 for (size_t i = 0; i < delayed_info->size(); ++i) {
528 ExtensionInfo* info = delayed_info->at(i).get();
529 scoped_refptr<const Extension> extension(NULL);
530 if (info->extension_manifest) {
531 std::string error;
532 extension = Extension::Create(
533 info->extension_path,
534 info->extension_location,
535 *info->extension_manifest,
536 extension_prefs_->GetDelayedInstallCreationFlags(
537 info->extension_id),
538 info->extension_id,
539 &error);
540 if (extension.get())
541 delayed_installs_.Insert(extension);
542 }
543 }
544 MaybeFinishDelayedInstallations();
545
546 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> delayed_info2(
547 extension_prefs_->GetAllDelayedInstallInfo());
548 UMA_HISTOGRAM_COUNTS_100("Extensions.UpdateOnLoad",
549 delayed_info2->size() - delayed_info->size());
550
[email protected]47b60072013-06-20 10:35:56551 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25552
[email protected]47b60072013-06-20 10:35:56553 // TODO(erikkay) this should probably be deferred to a future point
554 // rather than running immediately at startup.
555 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57556
[email protected]33dc0c62014-02-13 00:00:38557 MaybeBootstrapVerifier();
[email protected]47b60072013-06-20 10:35:56558 base::MessageLoop::current()->PostDelayedTask(
559 FROM_HERE,
560 base::Bind(&ExtensionService::GarbageCollectExtensions, AsWeakPtr()),
561 base::TimeDelta::FromSeconds(kGarbageCollectStartupDelay));
[email protected]215a7be2012-10-22 19:53:42562
[email protected]820d9bd2013-04-03 03:46:03563 if (extension_prefs_->NeedsStorageGarbageCollection()) {
564 GarbageCollectIsolatedStorage();
565 extension_prefs_->SetNeedsStorageGarbageCollection(false);
566 }
[email protected]4c9201c42013-08-16 04:56:21567 system_->management_policy()->RegisterProvider(
568 shared_module_policy_provider_.get());
[email protected]2d19eb6e2014-01-27 17:30:00569
570 LoadGreylistFromPrefs();
[email protected]399583b2012-12-11 09:33:42571 }
[email protected]3350d712013-11-18 09:32:24572
573 UMA_HISTOGRAM_TIMES("Extensions.ExtensionServiceInitTime",
574 base::Time::Now() - begin_time);
[email protected]6014d672008-12-05 00:38:25575}
576
[email protected]2d19eb6e2014-01-27 17:30:00577void ExtensionService::LoadGreylistFromPrefs() {
[email protected]f47f7172014-03-19 19:27:10578 scoped_ptr<ExtensionSet> all_extensions =
579 registry_->GenerateInstalledExtensionsSet();
[email protected]2d19eb6e2014-01-27 17:30:00580
581 for (ExtensionSet::const_iterator it = all_extensions->begin();
582 it != all_extensions->end(); ++it) {
583 extensions::BlacklistState state =
584 extension_prefs_->GetExtensionBlacklistState((*it)->id());
585 if (state == extensions::BLACKLISTED_SECURITY_VULNERABILITY ||
586 state == extensions::BLACKLISTED_POTENTIALLY_UNWANTED ||
587 state == extensions::BLACKLISTED_CWS_POLICY_VIOLATION)
588 greylist_.Insert(*it);
589 }
590}
591
[email protected]33dc0c62014-02-13 00:00:38592void ExtensionService::MaybeBootstrapVerifier() {
[email protected]d820bc52014-03-13 05:35:23593 InstallVerifier* verifier = system_->install_verifier();
[email protected]33dc0c62014-02-13 00:00:38594 bool do_bootstrap = false;
595
596 if (verifier->NeedsBootstrap()) {
597 do_bootstrap = true;
598 } else {
[email protected]d9d44ec2014-02-21 22:44:07599 scoped_ptr<extensions::ExtensionSet> extensions =
[email protected]f47f7172014-03-19 19:27:10600 registry_->GenerateInstalledExtensionsSet();
[email protected]d9d44ec2014-02-21 22:44:07601 for (extensions::ExtensionSet::const_iterator i = extensions->begin();
602 i != extensions->end();
[email protected]33dc0c62014-02-13 00:00:38603 ++i) {
604 const Extension& extension = **i;
[email protected]8abef232014-03-07 08:54:37605
[email protected]33dc0c62014-02-13 00:00:38606 if (verifier->NeedsVerification(extension) &&
[email protected]8abef232014-03-07 08:54:37607 !verifier->IsKnownId(extension.id())) {
[email protected]33dc0c62014-02-13 00:00:38608 do_bootstrap = true;
609 break;
610 }
611 }
612 }
613 if (do_bootstrap)
614 VerifyAllExtensions(true); // bootstrap=true.
615}
616
[email protected]74564482014-01-30 13:55:42617void ExtensionService::VerifyAllExtensions(bool bootstrap) {
[email protected]68e5cc72013-12-17 02:41:53618 ExtensionIdSet to_add;
[email protected]f47f7172014-03-19 19:27:10619 scoped_ptr<ExtensionSet> all_extensions =
620 registry_->GenerateInstalledExtensionsSet();
[email protected]68e5cc72013-12-17 02:41:53621
622 for (ExtensionSet::const_iterator i = all_extensions->begin();
623 i != all_extensions->end(); ++i) {
624 const Extension& extension = **i;
625
[email protected]88622932014-01-16 17:59:12626 if (InstallVerifier::NeedsVerification(extension))
[email protected]68e5cc72013-12-17 02:41:53627 to_add.insert(extension.id());
628 }
[email protected]d820bc52014-03-13 05:35:23629 system_->install_verifier()->AddMany(
630 to_add,
631 base::Bind(&ExtensionService::FinishVerifyAllExtensions,
632 AsWeakPtr(),
633 bootstrap));
[email protected]68e5cc72013-12-17 02:41:53634}
635
[email protected]74564482014-01-30 13:55:42636void ExtensionService::FinishVerifyAllExtensions(bool bootstrap, bool success) {
637 LogVerifyAllSuccessHistogram(bootstrap, success);
[email protected]68e5cc72013-12-17 02:41:53638 if (success) {
639 // Check to see if any currently unverified extensions became verified.
[email protected]d820bc52014-03-13 05:35:23640 InstallVerifier* verifier = system_->install_verifier();
[email protected]bb1bc9b32013-12-21 03:09:14641 const ExtensionSet& disabled_extensions = registry_->disabled_extensions();
642 for (ExtensionSet::const_iterator i = disabled_extensions.begin();
643 i != disabled_extensions.end(); ++i) {
[email protected]68e5cc72013-12-17 02:41:53644 const Extension& extension = **i;
645 int disable_reasons = extension_prefs_->GetDisableReasons(extension.id());
646 if (disable_reasons & Extension::DISABLE_NOT_VERIFIED &&
647 !verifier->MustRemainDisabled(&extension, NULL, NULL)) {
648 extension_prefs_->RemoveDisableReason(extension.id(),
649 Extension::DISABLE_NOT_VERIFIED);
650 // Notify interested observers (eg the extensions settings page) by
651 // sending an UNLOADED notification.
652 //
653 // TODO(asargent) - this is a slight hack because it's already
654 // disabled; the right solution might be to add a separate listener
655 // interface for DisableReason's changing. http://crbug.com/328916
656 UnloadedExtensionInfo details(&extension,
657 UnloadedExtensionInfo::REASON_DISABLE);
658 content::NotificationService::current()->Notify(
[email protected]ec870532014-03-19 20:24:05659 chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
[email protected]68e5cc72013-12-17 02:41:53660 content::Source<Profile>(profile_),
661 content::Details<UnloadedExtensionInfo>(&details));
662 }
663 }
664 // Might disable some extensions.
665 CheckManagementPolicy();
666 }
667}
668
[email protected]31bb5ee62012-09-12 22:58:40669bool ExtensionService::UpdateExtension(const std::string& id,
[email protected]650b2d52013-02-10 03:41:45670 const base::FilePath& extension_path,
[email protected]044e86992014-01-24 22:59:11671 bool file_ownership_passed,
[email protected]31bb5ee62012-09-12 22:58:40672 const GURL& download_url,
673 CrxInstaller** out_crx_installer) {
[email protected]a29a517a2011-01-21 21:11:12674 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]3c4abc82012-10-22 22:25:54675 if (browser_terminating_) {
676 LOG(WARNING) << "Skipping UpdateExtension due to browser shutdown";
677 // Leak the temp file at extension_path. We don't want to add to the disk
678 // I/O burden at shutdown, we can't rely on the I/O completing anyway, and
679 // the file is in the OS temp directory which should be cleaned up for us.
680 return false;
681 }
[email protected]a8af9fdb2010-10-28 21:52:20682
[email protected]3f213ad2012-07-26 23:39:41683 const extensions::PendingExtensionInfo* pending_extension_info =
[email protected]51a3bf8b2012-06-08 22:53:06684 pending_extension_manager()->GetById(id);
[email protected]8ef78fd2010-08-19 17:14:32685
[email protected]695b5712012-12-06 23:55:28686 const Extension* extension = GetInstalledExtension(id);
[email protected]51a3bf8b2012-06-08 22:53:06687 if (!pending_extension_info && !extension) {
[email protected]aa142702010-03-26 01:26:33688 LOG(WARNING) << "Will not update extension " << id
689 << " because it is not installed or pending";
690 // Delete extension_path since we're not creating a CrxInstaller
691 // that would do it for us.
[email protected]7f8f24f2012-11-15 19:40:14692 if (!GetFileTaskRunner()->PostTask(
693 FROM_HERE,
[email protected]53612e82011-10-18 18:00:36694 base::Bind(
[email protected]7296f2762011-11-21 19:23:44695 &extension_file_util::DeleteFile, extension_path, false)))
[email protected]14908b72011-04-20 06:54:36696 NOTREACHED();
[email protected]420a0ec2011-06-01 01:07:03697
698 return false;
[email protected]e957fe52009-06-23 16:51:05699 }
700
[email protected]aa142702010-03-26 01:26:33701 // We want a silent install only for non-pending extensions and
702 // pending extensions that have install_silently set.
[email protected]f8636f92013-08-09 21:02:37703 scoped_ptr<ExtensionInstallPrompt> client;
[email protected]91e51d612012-10-21 23:03:05704 if (pending_extension_info && !pending_extension_info->install_silently())
[email protected]f8636f92013-08-09 21:02:37705 client.reset(ExtensionInstallUI::CreateInstallPromptWithProfile(profile_));
[email protected]aa142702010-03-26 01:26:33706
[email protected]f8636f92013-08-09 21:02:37707 scoped_refptr<CrxInstaller> installer(
708 CrxInstaller::Create(this, client.Pass()));
[email protected]6dfbbf82010-03-12 23:09:16709 installer->set_expected_id(id);
[email protected]464213a2013-10-15 01:06:48710 int creation_flags = Extension::NO_FLAGS;
[email protected]51a3bf8b2012-06-08 22:53:06711 if (pending_extension_info) {
712 installer->set_install_source(pending_extension_info->install_source());
713 if (pending_extension_info->install_silently())
714 installer->set_allow_silent_install(true);
[email protected]464213a2013-10-15 01:06:48715 creation_flags = pending_extension_info->creation_flags();
716 if (pending_extension_info->mark_acknowledged())
717 AcknowledgeExternalExtension(id);
[email protected]51a3bf8b2012-06-08 22:53:06718 } else if (extension) {
[email protected]5eb375e92010-11-26 07:50:41719 installer->set_install_source(extension->location());
[email protected]51a3bf8b2012-06-08 22:53:06720 }
[email protected]7d8b7072012-04-07 01:07:46721 // If the extension was installed from or has migrated to the webstore, or
[email protected]fcb2c2c2012-10-17 21:08:45722 // its auto-update URL is from the webstore, treat it as a webstore install.
723 // Note that we ignore some older extensions with blank auto-update URLs
724 // because we are mostly concerned with restrictions on NaCl extensions,
725 // which are newer.
[email protected]75764512011-12-19 19:54:28726 if ((extension && extension->from_webstore()) ||
[email protected]4a5fe3e22013-06-04 07:06:38727 (extension && extensions::ManifestURL::UpdatesFromGallery(extension)) ||
[email protected]963d13c2012-09-29 17:13:35728 (!extension && extension_urls::IsWebstoreUpdateUrl(
[email protected]51a3bf8b2012-06-08 22:53:06729 pending_extension_info->update_url()))) {
[email protected]a12ce8b22012-01-17 18:40:53730 creation_flags |= Extension::FROM_WEBSTORE;
731 }
732
733 // Bookmark apps being updated is kind of a contradiction, but that's because
734 // we mark the default apps as bookmark apps, and they're hosted in the web
735 // store, thus they can get updated. See http://crbug.com/101605 for more
736 // details.
737 if (extension && extension->from_bookmark())
738 creation_flags |= Extension::FROM_BOOKMARK;
739
[email protected]e33bbc22012-08-27 22:05:46740 if (extension && extension->was_installed_by_default())
741 creation_flags |= Extension::WAS_INSTALLED_BY_DEFAULT;
742
[email protected]acc3c7c22014-03-19 06:23:39743 if (extension && extension->was_installed_by_oem())
744 creation_flags |= Extension::WAS_INSTALLED_BY_OEM;
745
[email protected]ab6c7be42014-01-16 02:05:54746 if (extension && extension->is_ephemeral())
747 creation_flags |= Extension::IS_EPHEMERAL;
748
[email protected]a12ce8b22012-01-17 18:40:53749 installer->set_creation_flags(creation_flags);
750
[email protected]044e86992014-01-24 22:59:11751 installer->set_delete_source(file_ownership_passed);
[email protected]655b2b1a2011-10-13 17:13:06752 installer->set_download_url(download_url);
[email protected]cb0e50312011-05-09 15:03:07753 installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
[email protected]6dfbbf82010-03-12 23:09:16754 installer->InstallCrx(extension_path);
[email protected]420a0ec2011-06-01 01:07:03755
756 if (out_crx_installer)
[email protected]dc24976f2013-06-02 21:15:09757 *out_crx_installer = installer.get();
[email protected]420a0ec2011-06-01 01:07:03758
759 return true;
[email protected]e957fe52009-06-23 16:51:05760}
761
[email protected]e178ad92013-06-28 02:29:25762void ExtensionService::ReloadExtension(const std::string extension_id) {
[email protected]a29a517a2011-01-21 21:11:12763 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]2a947202013-03-06 04:58:05764
765 // If the extension is already reloading, don't reload again.
766 if (extension_prefs_->GetDisableReasons(extension_id) &
767 Extension::DISABLE_RELOAD) {
768 return;
769 }
770
[email protected]650b2d52013-02-10 03:41:45771 base::FilePath path;
[email protected]9adb9692010-10-29 23:14:02772 const Extension* current_extension = GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40773
[email protected]f17dbd42010-08-16 23:21:10774 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]b65272f2009-08-31 15:47:06775 if (current_extension) {
[email protected]4814b512009-11-07 00:12:29776 // If the extension has an inspector open for its background page, detach
777 // the inspector and hang onto a cookie for it, so that we can reattach
778 // later.
[email protected]31d8f5f22012-04-02 15:22:08779 // TODO(yoz): this is not incognito-safe!
[email protected]98b6d942013-11-10 00:34:07780 extensions::ProcessManager* manager = system_->process_manager();
[email protected]3a1dc572012-07-31 22:25:13781 extensions::ExtensionHost* host =
782 manager->GetBackgroundHostForExtension(extension_id);
[email protected]98f66112012-12-25 12:59:36783 if (host && DevToolsAgentHost::HasFor(host->render_view_host())) {
[email protected]4814b512009-11-07 00:12:29784 // Look for an open inspector for the background page.
[email protected]04ea1bb2013-07-10 09:26:09785 scoped_refptr<DevToolsAgentHost> agent_host =
786 DevToolsAgentHost::GetOrCreateFor(host->render_view_host());
787 agent_host->DisconnectRenderViewHost();
788 orphaned_dev_tools_[extension_id] = agent_host;
[email protected]4814b512009-11-07 00:12:29789 }
790
[email protected]b65272f2009-08-31 15:47:06791 path = current_extension->path();
[email protected]58076192013-07-19 19:43:50792 // BeingUpgraded is set back to false when the extension is added.
[email protected]45f5b7d2014-01-22 23:47:13793 system_->runtime_data()->SetBeingUpgraded(current_extension, true);
[email protected]44d62b62012-04-11 00:06:03794 DisableExtension(extension_id, Extension::DISABLE_RELOAD);
[email protected]b914e2952013-04-26 07:10:03795 reloading_extensions_.insert(extension_id);
[email protected]1eb175082010-02-10 09:26:16796 } else {
797 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06798 }
799
[email protected]9f4e4f082013-06-21 07:11:19800 if (delayed_installs_.Contains(extension_id)) {
[email protected]6f6101832012-11-27 22:10:48801 FinishDelayedInstallation(extension_id);
[email protected]0db124b02012-11-07 04:55:05802 return;
803 }
804
[email protected]43ceb002012-02-10 23:19:15805 // If we're reloading a component extension, use the component extension
806 // loader's reloader.
807 if (component_loader_->Exists(extension_id)) {
[email protected]e178ad92013-06-28 02:29:25808 SetBeingReloaded(extension_id, true);
[email protected]43ceb002012-02-10 23:19:15809 component_loader_->Reload(extension_id);
[email protected]e178ad92013-06-28 02:29:25810 SetBeingReloaded(extension_id, false);
[email protected]43ceb002012-02-10 23:19:15811 return;
812 }
813
[email protected]e6090e42010-03-23 22:44:08814 // Check the installed extensions to see if what we're reloading was already
815 // installed.
[email protected]e178ad92013-06-28 02:29:25816 SetBeingReloaded(extension_id, true);
[email protected]e6090e42010-03-23 22:44:08817 scoped_ptr<ExtensionInfo> installed_extension(
818 extension_prefs_->GetInstalledExtensionInfo(extension_id));
819 if (installed_extension.get() &&
820 installed_extension->extension_manifest.get()) {
[email protected]d8c8f25f2011-11-02 18:18:01821 extensions::InstalledLoader(this).Load(*installed_extension, false);
[email protected]e6090e42010-03-23 22:44:08822 } else {
[email protected]d8c8f25f2011-11-02 18:18:01823 // Otherwise, the extension is unpacked (location LOAD).
[email protected]e6090e42010-03-23 22:44:08824 // We should always be able to remember the extension's path. If it's not in
825 // the map, someone failed to update |unloaded_extension_paths_|.
826 CHECK(!path.empty());
[email protected]d8c8f25f2011-11-02 18:18:01827 extensions::UnpackedInstaller::Create(this)->Load(path);
[email protected]e6090e42010-03-23 22:44:08828 }
[email protected]e178ad92013-06-28 02:29:25829 // When reloading is done, mark this extension as done reloading.
830 SetBeingReloaded(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40831}
832
[email protected]fa2416f2011-05-03 08:41:20833bool ExtensionService::UninstallExtension(
[email protected]a12c706e2011-12-01 00:58:08834 std::string extension_id,
[email protected]fa2416f2011-05-03 08:41:20835 bool external_uninstall,
[email protected]439f1e32013-12-09 20:09:09836 base::string16* error) {
[email protected]a29a517a2011-01-21 21:11:12837 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20838
[email protected]0d6ec3a72011-09-02 02:09:43839 scoped_refptr<const Extension> extension(GetInstalledExtension(extension_id));
[email protected]631cf822009-05-15 07:01:25840
[email protected]e7afe2452010-08-22 16:19:13841 // Callers should not send us nonexistent extensions.
[email protected]dc24976f2013-06-02 21:15:09842 CHECK(extension.get());
[email protected]9f1087e2009-06-15 17:29:32843
[email protected]95da88c42011-03-31 10:07:33844 // Policy change which triggers an uninstall will always set
845 // |external_uninstall| to true so this is the only way to uninstall
846 // managed extensions.
[email protected]65187152012-06-02 13:14:14847 if (!external_uninstall &&
848 !system_->management_policy()->UserMayModifySettings(
849 extension.get(), error)) {
[email protected]ad50def52011-10-19 23:17:07850 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27851 chrome::NOTIFICATION_EXTENSION_UNINSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:53852 content::Source<Profile>(profile_),
[email protected]dc24976f2013-06-02 21:15:09853 content::Details<const Extension>(extension.get()));
[email protected]d6ebc9792011-04-07 18:18:33854 return false;
855 }
[email protected]95da88c42011-03-31 10:07:33856
[email protected]65f173552012-06-28 22:43:58857 syncer::SyncChange sync_change;
[email protected]f8aefb132013-10-30 09:29:52858 if (extension_sync_service_) {
859 sync_change = extension_sync_service_->PrepareToSyncUninstallExtension(
860 extension.get(), is_ready());
[email protected]3bdba0d2011-08-23 07:17:30861 }
862
[email protected]d820bc52014-03-13 05:35:23863 system_->install_verifier()->Remove(extension->id());
[email protected]ffd2f79e2013-11-14 00:11:46864
[email protected]dc24976f2013-06-02 21:15:09865 if (IsUnacknowledgedExternalExtension(extension.get())) {
[email protected]d96eb512012-11-01 23:44:08866 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
867 EXTERNAL_EXTENSION_UNINSTALLED,
868 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:02869 if (extensions::ManifestURL::UpdatesFromGallery(extension.get())) {
870 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventWebstore",
871 EXTERNAL_EXTENSION_UNINSTALLED,
872 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
873 } else {
874 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventNonWebstore",
875 EXTERNAL_EXTENSION_UNINSTALLED,
876 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
877 }
[email protected]d96eb512012-11-01 23:44:08878 }
[email protected]9b217652010-10-08 22:04:23879 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08880 extension->GetType(), 100);
[email protected]dc24976f2013-06-02 21:15:09881 RecordPermissionMessagesHistogram(extension.get(),
882 "Extensions.Permissions_Uninstall");
[email protected]9b217652010-10-08 22:04:23883
[email protected]831aa212010-03-26 13:55:19884 // Unload before doing more cleanup to ensure that nothing is hanging on to
885 // any of these resources.
[email protected]b0af4792013-10-23 09:12:13886 UnloadExtension(extension_id, UnloadedExtensionInfo::REASON_UNINSTALL);
[email protected]831aa212010-03-26 13:55:19887
[email protected]9f1087e2009-06-15 17:29:32888 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]12075d12013-02-27 05:38:05889 if (!Manifest::IsUnpackedLocation(extension->location())) {
[email protected]7f8f24f2012-11-15 19:40:14890 if (!GetFileTaskRunner()->PostTask(
891 FROM_HERE,
[email protected]53612e82011-10-18 18:00:36892 base::Bind(
[email protected]14908b72011-04-20 06:54:36893 &extension_file_util::UninstallExtension,
894 install_directory_,
[email protected]fa2416f2011-05-03 08:41:20895 extension_id)))
[email protected]14908b72011-04-20 06:54:36896 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32897 }
898
[email protected]d656595b2014-01-09 14:09:35899 extensions::DataDeleter::StartDeleting(profile_, extension.get());
[email protected]0d6ec3a72011-09-02 02:09:43900
[email protected]0dfe05c2011-02-23 23:03:36901 UntrackTerminatedExtension(extension_id);
[email protected]211030342010-09-30 18:41:06902
903 // Notify interested parties that we've uninstalled this extension.
[email protected]ad50def52011-10-19 23:17:07904 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27905 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
[email protected]6c2381d2011-10-19 02:52:53906 content::Source<Profile>(profile_),
[email protected]dc24976f2013-06-02 21:15:09907 content::Details<const Extension>(extension.get()));
[email protected]d6ebc9792011-04-07 18:18:33908
[email protected]f8aefb132013-10-30 09:29:52909 if (extension_sync_service_) {
910 extension_sync_service_->ProcessSyncUninstallExtension(extension_id,
911 sync_change);
[email protected]3bdba0d2011-08-23 07:17:30912 }
913
[email protected]399583b2012-12-11 09:33:42914 delayed_installs_.Remove(extension_id);
[email protected]0db124b02012-11-07 04:55:05915
[email protected]198b5902013-06-27 10:36:11916 PruneSharedModulesOnUninstall(extension.get());
[email protected]9f4e4f082013-06-21 07:11:19917
[email protected]d4eda592013-09-18 03:37:57918 extension_prefs_->OnExtensionUninstalled(extension_id, extension->location(),
919 external_uninstall);
920
[email protected]333b1de2011-09-12 18:28:50921 // Track the uninstallation.
[email protected]49098f702011-10-13 03:47:18922 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionUninstalled", 1, 2);
923
[email protected]d6ebc9792011-04-07 18:18:33924 return true;
[email protected]c10da4b02010-03-25 14:38:32925}
926
[email protected]c3cfb012011-04-06 22:07:35927bool ExtensionService::IsExtensionEnabled(
928 const std::string& extension_id) const {
[email protected]bb1bc9b32013-12-21 03:09:14929 if (registry_->enabled_extensions().Contains(extension_id) ||
930 registry_->terminated_extensions().Contains(extension_id)) {
[email protected]36429da2011-07-11 20:25:18931 return true;
[email protected]dc9a74f72012-08-17 18:07:21932 }
[email protected]36429da2011-07-11 20:25:18933
[email protected]bb1bc9b32013-12-21 03:09:14934 if (registry_->disabled_extensions().Contains(extension_id) ||
935 registry_->blacklisted_extensions().Contains(extension_id)) {
[email protected]ad83ca242011-07-29 01:32:25936 return false;
[email protected]695b5712012-12-06 23:55:28937 }
[email protected]ad83ca242011-07-29 01:32:25938
939 // If the extension hasn't been loaded yet, check the prefs for it. Assume
940 // enabled unless otherwise noted.
941 return !extension_prefs_->IsExtensionDisabled(extension_id) &&
[email protected]f574c402012-12-04 23:20:31942 !extension_prefs_->IsExternalExtensionUninstalled(extension_id);
[email protected]c3cfb012011-04-06 22:07:35943}
944
945bool ExtensionService::IsExternalExtensionUninstalled(
946 const std::string& extension_id) const {
947 return extension_prefs_->IsExternalExtensionUninstalled(extension_id);
948}
949
[email protected]eaa7dd182010-12-14 11:09:00950void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12951 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20952
[email protected]06f92562011-04-29 19:27:31953 if (IsExtensionEnabled(extension_id))
[email protected]0c6da502009-08-14 22:32:39954 return;
[email protected]bb1bc9b32013-12-21 03:09:14955 const Extension* extension =
956 registry_->disabled_extensions().GetByID(extension_id);
[email protected]0d54b682013-11-05 14:15:36957
958 ManagementPolicy* policy = system_->management_policy();
959 if (extension && policy->MustRemainDisabled(extension, NULL, NULL)) {
960 UMA_HISTOGRAM_COUNTS_100("Extensions.EnableDeniedByPolicy", 1);
961 return;
962 }
[email protected]0c6da502009-08-14 22:32:39963
[email protected]b3317ad2011-04-28 23:46:00964 extension_prefs_->SetExtensionState(extension_id, Extension::ENABLED);
[email protected]eb5e4f92012-08-15 23:33:28965 extension_prefs_->ClearDisableReasons(extension_id);
[email protected]1784e83a2009-09-08 21:01:52966
[email protected]06f92562011-04-29 19:27:31967 // This can happen if sync enables an extension that is not
968 // installed yet.
969 if (!extension)
970 return;
971
[email protected]d96eb512012-11-01 23:44:08972 if (IsUnacknowledgedExternalExtension(extension)) {
973 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
974 EXTERNAL_EXTENSION_REENABLED,
975 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:02976 if (extensions::ManifestURL::UpdatesFromGallery(extension)) {
977 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventWebstore",
978 EXTERNAL_EXTENSION_REENABLED,
979 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
980 } else {
981 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventNonWebstore",
982 EXTERNAL_EXTENSION_REENABLED,
983 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
984 }
[email protected]612a1cb12012-10-17 13:18:03985 AcknowledgeExternalExtension(extension->id());
[email protected]d96eb512012-11-01 23:44:08986 }
[email protected]612a1cb12012-10-17 13:18:03987
[email protected]0c6da502009-08-14 22:32:39988 // Move it over to the enabled list.
[email protected]bb1bc9b32013-12-21 03:09:14989 registry_->AddEnabled(make_scoped_refptr(extension));
990 registry_->RemoveDisabled(extension->id());
[email protected]0c6da502009-08-14 22:32:39991
[email protected]62d30f42009-10-01 22:36:06992 NotifyExtensionLoaded(extension);
[email protected]3bdba0d2011-08-23 07:17:30993
[email protected]f74d7f32012-06-19 19:22:51994 // Notify listeners that the extension was enabled.
995 content::NotificationService::current()->Notify(
996 chrome::NOTIFICATION_EXTENSION_ENABLED,
997 content::Source<Profile>(profile_),
998 content::Details<const Extension>(extension));
999
[email protected]f8aefb132013-10-30 09:29:521000 if (extension_sync_service_)
1001 extension_sync_service_->SyncEnableExtension(*extension);
[email protected]0c6da502009-08-14 22:32:391002}
1003
[email protected]44d62b62012-04-11 00:06:031004void ExtensionService::DisableExtension(
1005 const std::string& extension_id,
1006 Extension::DisableReason disable_reason) {
[email protected]a29a517a2011-01-21 21:11:121007 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:201008
[email protected]b2ba9962009-12-10 20:10:151009 // The extension may have been disabled already.
[email protected]06f92562011-04-29 19:27:311010 if (!IsExtensionEnabled(extension_id))
[email protected]1784e83a2009-09-08 21:01:521011 return;
[email protected]1784e83a2009-09-08 21:01:521012
[email protected]06f92562011-04-29 19:27:311013 const Extension* extension = GetInstalledExtension(extension_id);
1014 // |extension| can be NULL if sync disables an extension that is not
1015 // installed yet.
[email protected]65187152012-06-02 13:14:141016 if (extension &&
[email protected]505ccdc2013-06-20 20:15:351017 disable_reason != Extension::DISABLE_RELOAD &&
[email protected]65187152012-06-02 13:14:141018 !system_->management_policy()->UserMayModifySettings(extension, NULL)) {
[email protected]95da88c42011-03-31 10:07:331019 return;
[email protected]65187152012-06-02 13:14:141020 }
[email protected]95da88c42011-03-31 10:07:331021
[email protected]b3317ad2011-04-28 23:46:001022 extension_prefs_->SetExtensionState(extension_id, Extension::DISABLED);
[email protected]eb5e4f92012-08-15 23:33:281023 extension_prefs_->AddDisableReason(extension_id, disable_reason);
[email protected]1784e83a2009-09-08 21:01:521024
[email protected]599539802014-01-07 23:06:001025 int include_mask =
1026 ExtensionRegistry::EVERYTHING & ~ExtensionRegistry::DISABLED;
1027 extension = registry_->GetExtensionById(extension_id, include_mask);
[email protected]06f92562011-04-29 19:27:311028 if (!extension)
1029 return;
1030
[email protected]45f5b7d2014-01-22 23:47:131031 // The extension is either enabled or terminated.
1032 DCHECK(registry_->enabled_extensions().Contains(extension->id()) ||
1033 registry_->terminated_extensions().Contains(extension->id()));
[email protected]64742de32013-04-22 08:44:341034
[email protected]5c094792012-09-07 19:44:531035 // Move it over to the disabled list. Don't send a second unload notification
1036 // for terminated extensions being disabled.
[email protected]bb1bc9b32013-12-21 03:09:141037 registry_->AddDisabled(make_scoped_refptr(extension));
1038 if (registry_->enabled_extensions().Contains(extension->id())) {
1039 registry_->RemoveEnabled(extension->id());
[email protected]b0af4792013-10-23 09:12:131040 NotifyExtensionUnloaded(extension, UnloadedExtensionInfo::REASON_DISABLE);
[email protected]5c094792012-09-07 19:44:531041 } else {
[email protected]bb1bc9b32013-12-21 03:09:141042 registry_->RemoveTerminated(extension->id());
[email protected]5c094792012-09-07 19:44:531043 }
[email protected]3bdba0d2011-08-23 07:17:301044
[email protected]f8aefb132013-10-30 09:29:521045 if (extension_sync_service_)
1046 extension_sync_service_->SyncDisableExtension(*extension);
[email protected]1784e83a2009-09-08 21:01:521047}
1048
[email protected]1abf05e2013-07-09 17:04:361049void ExtensionService::DisableUserExtensions(
1050 const std::vector<std::string>& except_ids) {
[email protected]e516e4c2013-06-12 17:41:141051 extensions::ManagementPolicy* management_policy =
1052 system_->management_policy();
1053 extensions::ExtensionList to_disable;
1054
[email protected]bb1bc9b32013-12-21 03:09:141055 const ExtensionSet& enabled_set = registry_->enabled_extensions();
1056 for (ExtensionSet::const_iterator extension = enabled_set.begin();
1057 extension != enabled_set.end(); ++extension) {
[email protected]198b5902013-06-27 10:36:111058 if (management_policy->UserMayModifySettings(extension->get(), NULL))
[email protected]e516e4c2013-06-12 17:41:141059 to_disable.push_back(*extension);
1060 }
[email protected]bb1bc9b32013-12-21 03:09:141061 const ExtensionSet& terminated_set = registry_->terminated_extensions();
1062 for (ExtensionSet::const_iterator extension = terminated_set.begin();
1063 extension != terminated_set.end(); ++extension) {
[email protected]198b5902013-06-27 10:36:111064 if (management_policy->UserMayModifySettings(extension->get(), NULL))
[email protected]e516e4c2013-06-12 17:41:141065 to_disable.push_back(*extension);
1066 }
1067
1068 for (extensions::ExtensionList::const_iterator extension = to_disable.begin();
1069 extension != to_disable.end(); ++extension) {
[email protected]3082fe32013-08-06 11:12:381070 if ((*extension)->was_installed_by_default() &&
1071 extension_urls::IsWebstoreUpdateUrl(
1072 extensions::ManifestURL::GetUpdateURL(*extension)))
1073 continue;
[email protected]1abf05e2013-07-09 17:04:361074 const std::string& id = (*extension)->id();
1075 if (except_ids.end() == std::find(except_ids.begin(), except_ids.end(), id))
1076 DisableExtension(id, extensions::Extension::DISABLE_USER_ACTION);
[email protected]e516e4c2013-06-12 17:41:141077 }
1078}
1079
[email protected]eaa7dd182010-12-14 11:09:001080void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]009633c2013-03-07 22:08:281081 const Extension* extension) {
1082 GrantPermissions(extension);
[email protected]fe2dd7742011-04-19 22:52:491083 RecordPermissionMessagesHistogram(
1084 extension, "Extensions.Permissions_ReEnable");
[email protected]8d888c12010-11-30 00:00:251085 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
1086 EnableExtension(extension->id());
1087}
1088
[email protected]009633c2013-03-07 22:08:281089void ExtensionService::GrantPermissions(const Extension* extension) {
[email protected]be083862012-09-01 03:53:451090 CHECK(extension);
1091 extensions::PermissionsUpdater perms_updater(profile());
[email protected]009633c2013-03-07 22:08:281092 perms_updater.GrantActivePermissions(extension);
[email protected]be083862012-09-01 03:53:451093}
1094
[email protected]fe2dd7742011-04-19 22:52:491095// static
1096void ExtensionService::RecordPermissionMessagesHistogram(
[email protected]13c68b62013-05-17 11:29:051097 const Extension* extension, const char* histogram) {
[email protected]de415552013-01-23 04:12:171098 // Since this is called from multiple sources, and since the histogram macros
1099 // use statics, we need to manually lookup the histogram ourselves.
1100 base::HistogramBase* counter = base::LinearHistogram::FactoryGet(
[email protected]fe2dd7742011-04-19 22:52:491101 histogram,
1102 1,
[email protected]c2e66e12012-06-27 06:27:061103 PermissionMessage::kEnumBoundary,
1104 PermissionMessage::kEnumBoundary + 1,
[email protected]de415552013-01-23 04:12:171105 base::HistogramBase::kUmaTargetedHistogramFlag);
[email protected]fe2dd7742011-04-19 22:52:491106
[email protected]13c68b62013-05-17 11:29:051107 PermissionMessages permissions =
1108 extensions::PermissionsData::GetPermissionMessages(extension);
[email protected]fe2dd7742011-04-19 22:52:491109 if (permissions.empty()) {
[email protected]c2e66e12012-06-27 06:27:061110 counter->Add(PermissionMessage::kNone);
[email protected]fe2dd7742011-04-19 22:52:491111 } else {
[email protected]c2e66e12012-06-27 06:27:061112 for (PermissionMessages::iterator it = permissions.begin();
[email protected]0d3e4a22011-06-23 19:02:521113 it != permissions.end(); ++it)
1114 counter->Add(it->id());
[email protected]fe2dd7742011-04-19 22:52:491115 }
1116}
1117
[email protected]eaa7dd182010-12-14 11:09:001118void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]57a777f72010-03-31 01:09:421119 // The ChromeURLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:061120 // was loaded, otherwise a race can arise where a renderer that is created
1121 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:421122 // that the request context doesn't yet know about. The profile is responsible
1123 // for ensuring its URLRequestContexts appropriately discover the loaded
1124 // extension.
[email protected]31d8f5f22012-04-02 15:22:081125 system_->RegisterExtensionWithRequestContexts(extension);
[email protected]62d30f42009-10-01 22:36:061126
[email protected]d5949312012-12-03 22:13:301127 // Tell renderers about the new extension, unless it's a theme (renderers
1128 // don't need to know about themes).
[email protected]a58f599c2012-12-01 03:08:191129 if (!extension->is_theme()) {
[email protected]19647262011-12-16 09:57:491130 for (content::RenderProcessHost::iterator i(
1131 content::RenderProcessHost::AllHostsIterator());
1132 !i.IsAtEnd(); i.Advance()) {
1133 content::RenderProcessHost* host = i.GetCurrentValue();
1134 Profile* host_profile =
1135 Profile::FromBrowserContext(host->GetBrowserContext());
1136 if (host_profile->GetOriginalProfile() ==
1137 profile_->GetOriginalProfile()) {
1138 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions(
1139 1, ExtensionMsg_Loaded_Params(extension));
1140 host->Send(
1141 new ExtensionMsg_Loaded(loaded_extensions));
1142 }
[email protected]c8d407e2011-04-28 21:27:171143 }
[email protected]77a6970c2011-04-23 16:58:561144 }
1145
[email protected]3442a662012-01-12 08:06:171146 // Tell subsystems that use the EXTENSION_LOADED notification about the new
1147 // extension.
1148 //
1149 // NOTE: It is important that this happen after notifying the renderers about
1150 // the new extensions so that if we navigate to an extension URL in
1151 // NOTIFICATION_EXTENSION_LOADED, the renderer is guaranteed to know about it.
1152 content::NotificationService::current()->Notify(
1153 chrome::NOTIFICATION_EXTENSION_LOADED,
1154 content::Source<Profile>(profile_),
1155 content::Details<const Extension>(extension));
1156
[email protected]c8d407e2011-04-28 21:27:171157 // Tell a random-ass collection of other subsystems about the new extension.
1158 // TODO(aa): What should we do with all this goop? Can it move into the
1159 // relevant objects via EXTENSION_LOADED?
1160
1161 profile_->GetExtensionSpecialStoragePolicy()->
1162 GrantRightsForExtension(extension);
1163
1164 UpdateActiveExtensionsInCrashReporter();
1165
[email protected]c8d407e2011-04-28 21:27:171166 // If the extension has permission to load chrome://favicon/ resources we need
1167 // to make sure that the FaviconSource is registered with the
1168 // ChromeURLDataManager.
[email protected]13c68b62013-05-17 11:29:051169 if (extensions::PermissionsData::HasHostPermission(
1170 extension, GURL(chrome::kChromeUIFaviconURL))) {
[email protected]c8d407e2011-04-28 21:27:171171 FaviconSource* favicon_source = new FaviconSource(profile_,
1172 FaviconSource::FAVICON);
[email protected]24ea7a12013-01-27 23:54:531173 content::URLDataSource::Add(profile_, favicon_source);
[email protected]c8d407e2011-04-28 21:27:171174 }
[email protected]b07e606e2012-09-15 20:16:151175
1176#if !defined(OS_ANDROID)
1177 // Same for chrome://theme/ resources.
[email protected]13c68b62013-05-17 11:29:051178 if (extensions::PermissionsData::HasHostPermission(
1179 extension, GURL(chrome::kChromeUIThemeURL))) {
[email protected]b07e606e2012-09-15 20:16:151180 ThemeSource* theme_source = new ThemeSource(profile_);
[email protected]24ea7a12013-01-27 23:54:531181 content::URLDataSource::Add(profile_, theme_source);
[email protected]b07e606e2012-09-15 20:16:151182 }
1183#endif
1184
[email protected]5eddc3e2011-10-26 04:33:311185 // Same for chrome://thumb/ resources.
[email protected]13c68b62013-05-17 11:29:051186 if (extensions::PermissionsData::HasHostPermission(
1187 extension, GURL(chrome::kChromeUIThumbnailURL))) {
[email protected]420e6d92013-09-17 01:48:511188 ThumbnailSource* thumbnail_source = new ThumbnailSource(profile_, false);
[email protected]24ea7a12013-01-27 23:54:531189 content::URLDataSource::Add(profile_, thumbnail_source);
[email protected]5eddc3e2011-10-26 04:33:311190 }
[email protected]62d30f42009-10-01 22:36:061191}
1192
[email protected]a9f39a312010-12-23 22:14:271193void ExtensionService::NotifyExtensionUnloaded(
[email protected]814a7bf0f2011-08-13 05:30:591194 const Extension* extension,
[email protected]b0af4792013-10-23 09:12:131195 UnloadedExtensionInfo::Reason reason) {
[email protected]45f5b7d2014-01-22 23:47:131196 registry_->TriggerOnUnloaded(extension);
1197
[email protected]a9f39a312010-12-23 22:14:271198 UnloadedExtensionInfo details(extension, reason);
[email protected]ad50def52011-10-19 23:17:071199 content::NotificationService::current()->Notify(
[email protected]ec870532014-03-19 20:24:051200 chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
[email protected]6c2381d2011-10-19 02:52:531201 content::Source<Profile>(profile_),
1202 content::Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061203
[email protected]f3b1a082011-11-18 00:34:301204 for (content::RenderProcessHost::iterator i(
1205 content::RenderProcessHost::AllHostsIterator());
[email protected]77a6970c2011-04-23 16:58:561206 !i.IsAtEnd(); i.Advance()) {
[email protected]f3b1a082011-11-18 00:34:301207 content::RenderProcessHost* host = i.GetCurrentValue();
[email protected]9b62ecf2011-07-27 20:23:081208 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:301209 Profile::FromBrowserContext(host->GetBrowserContext());
[email protected]9b62ecf2011-07-27 20:23:081210 if (host_profile->GetOriginalProfile() == profile_->GetOriginalProfile())
[email protected]c8d407e2011-04-28 21:27:171211 host->Send(new ExtensionMsg_Unloaded(extension->id()));
[email protected]77a6970c2011-04-23 16:58:561212 }
1213
[email protected]31d8f5f22012-04-02 15:22:081214 system_->UnregisterExtensionWithRequestContexts(extension->id(), reason);
[email protected]c8d407e2011-04-28 21:27:171215 profile_->GetExtensionSpecialStoragePolicy()->
1216 RevokeRightsForExtension(extension);
1217
[email protected]b777b332011-04-16 04:01:081218#if defined(OS_CHROMEOS)
[email protected]ac66e452013-01-11 09:21:231219 // Revoke external file access for the extension from its file system context.
1220 // It is safe to access the extension's storage partition at this point. The
1221 // storage partition may get destroyed only after the extension gets unloaded.
[email protected]3a746ec2014-03-15 05:30:561222 GURL site =
1223 extensions::util::GetSiteForExtensionId(extension->id(), profile_);
[email protected]10eb28162012-09-18 03:04:091224 fileapi::FileSystemContext* filesystem_context =
[email protected]ac66e452013-01-11 09:21:231225 BrowserContext::GetStoragePartitionForSite(profile_, site)->
[email protected]10eb28162012-09-18 03:04:091226 GetFileSystemContext();
[email protected]f19bbf62013-07-09 01:22:321227 if (filesystem_context && filesystem_context->external_backend()) {
1228 filesystem_context->external_backend()->
[email protected]c8d407e2011-04-28 21:27:171229 RevokeAccessForExtension(extension->id());
[email protected]62d30f42009-10-01 22:36:061230 }
[email protected]c8d407e2011-04-28 21:27:171231#endif
1232
1233 UpdateActiveExtensionsInCrashReporter();
[email protected]62d30f42009-10-01 22:36:061234}
1235
[email protected]0a071a32011-02-08 00:18:241236Profile* ExtensionService::profile() {
1237 return profile_;
1238}
1239
[email protected]599539802014-01-07 23:06:001240content::BrowserContext* ExtensionService::GetBrowserContext() const {
1241 // Implemented in the .cc file to avoid adding a profile.h dependency to
1242 // extension_service.h.
1243 return profile_;
1244}
1245
[email protected]25ae0152011-11-18 14:40:021246bool ExtensionService::is_ready() {
[email protected]4a10006a2013-05-17 23:18:351247 return ready_->is_signaled();
[email protected]25ae0152011-11-18 14:40:021248}
1249
[email protected]7f8f24f2012-11-15 19:40:141250base::SequencedTaskRunner* ExtensionService::GetFileTaskRunner() {
[email protected]dc24976f2013-06-02 21:15:091251 if (file_task_runner_.get())
1252 return file_task_runner_.get();
[email protected]7f8f24f2012-11-15 19:40:141253
1254 // We should be able to interrupt any part of extension install process during
1255 // shutdown. SKIP_ON_SHUTDOWN ensures that not started extension install tasks
1256 // will be ignored/deleted while we will block on started tasks.
1257 std::string token("ext_install-");
1258 token.append(profile_->GetPath().AsUTF8Unsafe());
1259 file_task_runner_ = BrowserThread::GetBlockingPool()->
1260 GetSequencedTaskRunnerWithShutdownBehavior(
1261 BrowserThread::GetBlockingPool()->GetNamedSequenceToken(token),
1262 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
[email protected]dc24976f2013-06-02 21:15:091263 return file_task_runner_.get();
[email protected]7f8f24f2012-11-15 19:40:141264}
1265
[email protected]42a08162012-03-16 18:09:111266extensions::ExtensionUpdater* ExtensionService::updater() {
[email protected]2859946f2011-04-04 18:18:061267 return updater_.get();
1268}
1269
[email protected]4ee07c62012-08-21 12:40:421270void ExtensionService::CheckManagementPolicy() {
[email protected]0d54b682013-11-05 14:15:361271 std::vector<std::string> to_unload;
1272 std::map<std::string, Extension::DisableReason> to_disable;
[email protected]695b5712012-12-06 23:55:281273
[email protected]0d54b682013-11-05 14:15:361274 // Loop through the extensions list, finding extensions we need to unload or
1275 // disable.
[email protected]bb1bc9b32013-12-21 03:09:141276 const ExtensionSet& extensions = registry_->enabled_extensions();
1277 for (ExtensionSet::const_iterator iter = extensions.begin();
1278 iter != extensions.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361279 const Extension* extension = (iter->get());
[email protected]4ee07c62012-08-21 12:40:421280 if (!system_->management_policy()->UserMayLoad(extension, NULL))
[email protected]0d54b682013-11-05 14:15:361281 to_unload.push_back(extension->id());
1282 Extension::DisableReason disable_reason = Extension::DISABLE_NONE;
1283 if (system_->management_policy()->MustRemainDisabled(
1284 extension, &disable_reason, NULL))
1285 to_disable[extension->id()] = disable_reason;
[email protected]aa96d3a2010-08-21 08:45:251286 }
1287
[email protected]0d54b682013-11-05 14:15:361288 for (size_t i = 0; i < to_unload.size(); ++i)
1289 UnloadExtension(to_unload[i], UnloadedExtensionInfo::REASON_DISABLE);
1290
1291 for (std::map<std::string, Extension::DisableReason>::const_iterator i =
1292 to_disable.begin(); i != to_disable.end(); ++i)
1293 DisableExtension(i->first, i->second);
[email protected]aa96d3a2010-08-21 08:45:251294}
1295
[email protected]31206602011-04-13 23:07:321296void ExtensionService::CheckForUpdatesSoon() {
[email protected]c3cfb012011-04-06 22:07:351297 if (updater()) {
[email protected]94fde232012-04-27 10:22:301298 if (AreAllExternalProvidersReady()) {
1299 updater()->CheckSoon();
1300 } else {
1301 // Sync can start updating before all the external providers are ready
1302 // during startup. Start the update as soon as those providers are ready,
1303 // but not before.
1304 update_once_all_providers_are_ready_ = true;
1305 }
[email protected]c3cfb012011-04-06 22:07:351306 } else {
[email protected]31206602011-04-13 23:07:321307 LOG(WARNING) << "CheckForUpdatesSoon() called with auto-update turned off";
[email protected]c3cfb012011-04-06 22:07:351308 }
1309}
1310
[email protected]168389f2011-12-20 17:12:481311void ExtensionService::OnExtensionMoved(
1312 const std::string& moved_extension_id,
1313 const std::string& predecessor_extension_id,
1314 const std::string& successor_extension_id) {
[email protected]74474042013-11-21 12:03:541315 extension_prefs_->app_sorting()->OnExtensionMoved(
[email protected]3569b502012-01-12 20:08:231316 moved_extension_id,
1317 predecessor_extension_id,
1318 successor_extension_id);
[email protected]168389f2011-12-20 17:12:481319
1320 const Extension* extension = GetInstalledExtension(moved_extension_id);
[email protected]f8aefb132013-10-30 09:29:521321 if (extension_sync_service_ && extension) {
1322 extension_sync_service_->SyncExtensionChangeIfNeeded(*extension);
1323 }
[email protected]168389f2011-12-20 17:12:481324}
1325
[email protected]8e4560b62011-01-14 10:09:141326// Some extensions will autoupdate themselves externally from Chrome. These
1327// are typically part of some larger client application package. To support
1328// these, the extension will register its location in the the preferences file
1329// (and also, on Windows, in the registry) and this code will periodically
1330// check that location for a .crx file, which it will then install locally if
1331// a new version is available.
1332// Errors are reported through ExtensionErrorReporter. Succcess is not
1333// reported.
[email protected]eaa7dd182010-12-14 11:09:001334void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:121335 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]8e4560b62011-01-14 10:09:141336
1337 // Note that this installation is intentionally silent (since it didn't
1338 // go through the front-end). Extensions that are registered in this
1339 // way are effectively considered 'pre-bundled', and so implicitly
1340 // trusted. In general, if something has HKLM or filesystem access,
1341 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:121342
[email protected]8e4560b62011-01-14 10:09:141343 // Ask each external extension provider to give us a call back for each
1344 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
[email protected]5df038b2012-07-16 19:03:271345 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141346 for (i = external_extension_providers_.begin();
1347 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:271348 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:141349 provider->VisitRegisteredExtension();
1350 }
1351
[email protected]50067e52011-10-20 23:17:071352 // Do any required work that we would have done after completion of all
1353 // providers.
1354 if (external_extension_providers_.empty()) {
1355 OnAllExternalProvidersReady();
1356 }
[email protected]9f1087e2009-06-15 17:29:321357}
1358
[email protected]50067e52011-10-20 23:17:071359void ExtensionService::OnExternalProviderReady(
[email protected]5df038b2012-07-16 19:03:271360 const extensions::ExternalProviderInterface* provider) {
[email protected]a29a517a2011-01-21 21:11:121361 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]50067e52011-10-20 23:17:071362 CHECK(provider->IsReady());
[email protected]a29a517a2011-01-21 21:11:121363
1364 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:141365 // if all of them are finished. So we check them first.
[email protected]94fde232012-04-27 10:22:301366 if (AreAllExternalProvidersReady())
1367 OnAllExternalProvidersReady();
1368}
1369
1370bool ExtensionService::AreAllExternalProvidersReady() const {
[email protected]5df038b2012-07-16 19:03:271371 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141372 for (i = external_extension_providers_.begin();
1373 i != external_extension_providers_.end(); ++i) {
[email protected]94fde232012-04-27 10:22:301374 if (!i->get()->IsReady())
1375 return false;
[email protected]8e4560b62011-01-14 10:09:141376 }
[email protected]94fde232012-04-27 10:22:301377 return true;
[email protected]50067e52011-10-20 23:17:071378}
1379
1380void ExtensionService::OnAllExternalProvidersReady() {
1381 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]94fde232012-04-27 10:22:301382 base::TimeDelta elapsed = base::Time::Now() - profile_->GetStartTime();
1383 UMA_HISTOGRAM_TIMES("Extension.ExternalProvidersReadyAfter", elapsed);
[email protected]50067e52011-10-20 23:17:071384
1385 // Install any pending extensions.
[email protected]94fde232012-04-27 10:22:301386 if (update_once_all_providers_are_ready_ && updater()) {
1387 update_once_all_providers_are_ready_ = false;
[email protected]4e9b59d2013-12-18 06:47:141388 extensions::ExtensionUpdater::CheckParams params;
1389 params.callback = external_updates_finished_callback_;
1390 updater()->CheckNow(params);
[email protected]8e4560b62011-01-14 10:09:141391 }
1392
1393 // Uninstall all the unclaimed extensions.
[email protected]45759612012-07-10 17:21:231394 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> extensions_info(
[email protected]8e4560b62011-01-14 10:09:141395 extension_prefs_->GetInstalledExtensionsInfo());
1396 for (size_t i = 0; i < extensions_info->size(); ++i) {
1397 ExtensionInfo* info = extensions_info->at(i).get();
[email protected]1d5e58b2013-01-31 08:41:401398 if (Manifest::IsExternalLocation(info->extension_location))
[email protected]8e4560b62011-01-14 10:09:141399 CheckExternalUninstall(info->extension_id);
1400 }
[email protected]e5af875f2011-10-10 21:09:141401 IdentifyAlertableExtensions();
[email protected]e5af875f2011-10-10 21:09:141402}
1403
1404void ExtensionService::IdentifyAlertableExtensions() {
1405 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1406
[email protected]07c9f2f42012-02-29 18:45:221407 // Build up the lists of extensions that require acknowledgment. If this is
1408 // the first time, grandfather extensions that would have caused
1409 // notification.
[email protected]89226982012-07-16 20:09:181410 extension_error_ui_.reset(ExtensionErrorUI::Create(this));
[email protected]e5af875f2011-10-10 21:09:141411
[email protected]a9aa5932012-01-25 08:27:401412 bool did_show_alert = false;
[email protected]89226982012-07-16 20:09:181413 if (PopulateExtensionErrorUI(extension_error_ui_.get())) {
[email protected]460c6712013-04-24 07:20:011414 if (!is_first_run_) {
[email protected]a9aa5932012-01-25 08:27:401415 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]89226982012-07-16 20:09:181416 did_show_alert = extension_error_ui_->ShowErrorInBubbleView();
[email protected]e5af875f2011-10-10 21:09:141417 } else {
1418 // First run. Just acknowledge all the extensions, silently, by
1419 // shortcutting the display of the UI and going straight to the
1420 // callback for pressing the Accept button.
[email protected]a9aa5932012-01-25 08:27:401421 HandleExtensionAlertAccept();
[email protected]e5af875f2011-10-10 21:09:141422 }
1423 }
[email protected]a9aa5932012-01-25 08:27:401424
[email protected]612a1cb12012-10-17 13:18:031425 UpdateExternalExtensionAlert();
1426
[email protected]a9aa5932012-01-25 08:27:401427 if (!did_show_alert)
[email protected]89226982012-07-16 20:09:181428 extension_error_ui_.reset();
[email protected]e5af875f2011-10-10 21:09:141429}
1430
[email protected]89226982012-07-16 20:09:181431bool ExtensionService::PopulateExtensionErrorUI(
1432 ExtensionErrorUI* extension_error_ui) {
[email protected]07c9f2f42012-02-29 18:45:221433 bool needs_alert = false;
[email protected]695b5712012-12-06 23:55:281434
1435 // Extensions that are blacklisted.
[email protected]bb1bc9b32013-12-21 03:09:141436 const ExtensionSet& blacklisted_set = registry_->blacklisted_extensions();
1437 for (ExtensionSet::const_iterator it = blacklisted_set.begin();
1438 it != blacklisted_set.end(); ++it) {
[email protected]695b5712012-12-06 23:55:281439 std::string id = (*it)->id();
1440 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(id)) {
1441 extension_error_ui->AddBlacklistedExtension(id);
1442 needs_alert = true;
1443 }
1444 }
1445
[email protected]bb1bc9b32013-12-21 03:09:141446 const ExtensionSet& enabled_set = registry_->enabled_extensions();
1447 for (ExtensionSet::const_iterator iter = enabled_set.begin();
1448 iter != enabled_set.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361449 const Extension* e = iter->get();
[email protected]695b5712012-12-06 23:55:281450
[email protected]870f5572013-12-19 12:30:151451 // Skip for extensions that have pending updates. They will be checked again
1452 // once the pending update is finished.
1453 if (pending_extension_manager()->IsIdPending(e->id()))
1454 continue;
1455
[email protected]695b5712012-12-06 23:55:281456 // Extensions disabled by policy. Note: this no longer includes blacklisted
1457 // extensions, though we still show the same UI.
[email protected]fdd679b2012-11-15 20:49:391458 if (!system_->management_policy()->UserMayLoad(e, NULL)) {
[email protected]07c9f2f42012-02-29 18:45:221459 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(e->id())) {
[email protected]89226982012-07-16 20:09:181460 extension_error_ui->AddBlacklistedExtension(e->id());
[email protected]07c9f2f42012-02-29 18:45:221461 needs_alert = true;
1462 }
1463 }
[email protected]07c9f2f42012-02-29 18:45:221464 }
[email protected]695b5712012-12-06 23:55:281465
[email protected]07c9f2f42012-02-29 18:45:221466 return needs_alert;
1467}
1468
[email protected]a9aa5932012-01-25 08:27:401469void ExtensionService::HandleExtensionAlertClosed() {
[email protected]612a1cb12012-10-17 13:18:031470 const ExtensionIdSet* extension_ids =
1471 extension_error_ui_->get_blacklisted_extension_ids();
[email protected]e5af875f2011-10-10 21:09:141472 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1473 iter != extension_ids->end(); ++iter) {
1474 extension_prefs_->AcknowledgeBlacklistedExtension(*iter);
1475 }
[email protected]879a6092013-03-06 04:12:501476 extension_error_ui_.reset();
1477}
1478
1479void ExtensionService::HandleExtensionAlertAccept() {
[email protected]73a85e012013-04-04 10:45:301480 extension_error_ui_->Close();
[email protected]e5af875f2011-10-10 21:09:141481}
1482
[email protected]47fc70c2011-12-06 07:29:511483void ExtensionService::AcknowledgeExternalExtension(const std::string& id) {
1484 extension_prefs_->AcknowledgeExternalExtension(id);
[email protected]612a1cb12012-10-17 13:18:031485 UpdateExternalExtensionAlert();
[email protected]47fc70c2011-12-06 07:29:511486}
1487
[email protected]d96eb512012-11-01 23:44:081488bool ExtensionService::IsUnacknowledgedExternalExtension(
1489 const Extension* extension) {
[email protected]b82d18da2012-11-09 00:11:321490 if (!FeatureSwitch::prompt_for_external_extensions()->IsEnabled())
1491 return false;
1492
[email protected]1d5e58b2013-01-31 08:41:401493 return (Manifest::IsExternalLocation(extension->location()) &&
[email protected]d96eb512012-11-01 23:44:081494 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id()) &&
1495 !(extension_prefs_->GetDisableReasons(extension->id()) &
1496 Extension::DISABLE_SIDELOAD_WIPEOUT));
1497}
1498
[email protected]59ee99d2013-10-11 15:46:161499void ExtensionService::ReconcileKnownDisabled() {
[email protected]60a8bc962013-12-03 03:17:271500 ExtensionIdSet known_disabled_ids;
1501 if (!extension_prefs_->GetKnownDisabled(&known_disabled_ids)) {
[email protected]bb1bc9b32013-12-21 03:09:141502 extension_prefs_->SetKnownDisabled(
1503 registry_->disabled_extensions().GetIDs());
[email protected]018a5682013-11-20 02:41:211504 UMA_HISTOGRAM_BOOLEAN("Extensions.KnownDisabledInitialized", true);
1505 return;
[email protected]59ee99d2013-10-11 15:46:161506 }
[email protected]195c1582013-10-29 18:42:231507
[email protected]bb1bc9b32013-12-21 03:09:141508 // Both |known_disabled_ids| and |extensions| are ordered (by definition
[email protected]018a5682013-11-20 02:41:211509 // of std::map and std::set). Iterate forward over both sets in parallel
1510 // to find matching IDs and disable the corresponding extensions.
[email protected]bb1bc9b32013-12-21 03:09:141511 const ExtensionSet& enabled_set = registry_->enabled_extensions();
1512 ExtensionSet::const_iterator extensions_it = enabled_set.begin();
[email protected]018a5682013-11-20 02:41:211513 ExtensionIdSet::const_iterator known_disabled_ids_it =
1514 known_disabled_ids.begin();
1515 int known_disabled_count = 0;
[email protected]bb1bc9b32013-12-21 03:09:141516 while (extensions_it != enabled_set.end() &&
[email protected]018a5682013-11-20 02:41:211517 known_disabled_ids_it != known_disabled_ids.end()) {
1518 const std::string& extension_id = extensions_it->get()->id();
1519 const int comparison = extension_id.compare(*known_disabled_ids_it);
1520 if (comparison < 0) {
1521 ++extensions_it;
1522 } else if (comparison > 0) {
1523 ++known_disabled_ids_it;
1524 } else {
1525 ++known_disabled_count;
1526 // Advance |extensions_it| immediately as it will be invalidated upon
1527 // disabling the extension it points to.
1528 ++extensions_it;
1529 ++known_disabled_ids_it;
1530 DisableExtension(extension_id, Extension::DISABLE_KNOWN_DISABLED);
1531 }
1532 }
1533 UMA_HISTOGRAM_COUNTS_100("Extensions.KnownDisabledReDisabled",
1534 known_disabled_count);
1535
[email protected]195c1582013-10-29 18:42:231536 // Update the list of known disabled to reflect every change to
1537 // |disabled_extensions_| from this point forward.
[email protected]bb1bc9b32013-12-21 03:09:141538 registry_->SetDisabledModificationCallback(
[email protected]195c1582013-10-29 18:42:231539 base::Bind(&extensions::ExtensionPrefs::SetKnownDisabled,
1540 base::Unretained(extension_prefs_)));
[email protected]59ee99d2013-10-11 15:46:161541}
1542
[email protected]89226982012-07-16 20:09:181543void ExtensionService::HandleExtensionAlertDetails() {
1544 extension_error_ui_->ShowExtensions();
[email protected]73a85e012013-04-04 10:45:301545 // ShowExtensions may cause the error UI to close synchronously, e.g. if it
1546 // causes a navigation.
1547 if (extension_error_ui_)
1548 extension_error_ui_->Close();
[email protected]6c751e72010-11-23 10:11:101549}
1550
[email protected]612a1cb12012-10-17 13:18:031551void ExtensionService::UpdateExternalExtensionAlert() {
[email protected]41070e8d2012-10-24 01:34:361552 if (!FeatureSwitch::prompt_for_external_extensions()->IsEnabled())
[email protected]612a1cb12012-10-17 13:18:031553 return;
1554
1555 const Extension* extension = NULL;
[email protected]bb1bc9b32013-12-21 03:09:141556 const ExtensionSet& disabled_extensions = registry_->disabled_extensions();
1557 for (ExtensionSet::const_iterator iter = disabled_extensions.begin();
1558 iter != disabled_extensions.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361559 const Extension* e = iter->get();
[email protected]d96eb512012-11-01 23:44:081560 if (IsUnacknowledgedExternalExtension(e)) {
1561 extension = e;
1562 break;
[email protected]612a1cb12012-10-17 13:18:031563 }
1564 }
1565
1566 if (extension) {
[email protected]d96eb512012-11-01 23:44:081567 if (!extensions::HasExternalInstallError(this)) {
1568 if (extension_prefs_->IncrementAcknowledgePromptCount(extension->id()) >
[email protected]612a1cb12012-10-17 13:18:031569 kMaxExtensionAcknowledgePromptCount) {
[email protected]d96eb512012-11-01 23:44:081570 // Stop prompting for this extension, and check if there's another
1571 // one that needs prompting.
[email protected]612a1cb12012-10-17 13:18:031572 extension_prefs_->AcknowledgeExternalExtension(extension->id());
[email protected]d96eb512012-11-01 23:44:081573 UpdateExternalExtensionAlert();
1574 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
1575 EXTERNAL_EXTENSION_IGNORED,
1576 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:021577 if (extensions::ManifestURL::UpdatesFromGallery(extension)) {
1578 UMA_HISTOGRAM_ENUMERATION(
1579 "Extensions.ExternalExtensionEventWebstore",
1580 EXTERNAL_EXTENSION_IGNORED,
1581 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
1582 } else {
1583 UMA_HISTOGRAM_ENUMERATION(
1584 "Extensions.ExternalExtensionEventNonWebstore",
1585 EXTERNAL_EXTENSION_IGNORED,
1586 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
1587 }
[email protected]d96eb512012-11-01 23:44:081588 return;
[email protected]612a1cb12012-10-17 13:18:031589 }
[email protected]b3aa7182013-04-25 04:45:231590 if (is_first_run_)
1591 extension_prefs_->SetExternalInstallFirstRun(extension->id());
1592 // first_run is true if the extension was installed during a first run
1593 // (even if it's post-first run now).
1594 bool first_run = extension_prefs_->IsExternalInstallFirstRun(
1595 extension->id());
1596 extensions::AddExternalInstallError(this, extension, first_run);
[email protected]612a1cb12012-10-17 13:18:031597 }
1598 } else {
1599 extensions::RemoveExternalInstallError(this);
1600 }
[email protected]612a1cb12012-10-17 13:18:031601}
1602
[email protected]a9f39a312010-12-23 22:14:271603void ExtensionService::UnloadExtension(
1604 const std::string& extension_id,
[email protected]b0af4792013-10-23 09:12:131605 UnloadedExtensionInfo::Reason reason) {
[email protected]27e469a2010-01-11 20:35:091606 // Make sure the extension gets deleted after we return from this function.
[email protected]599539802014-01-07 23:06:001607 int include_mask =
1608 ExtensionRegistry::EVERYTHING & ~ExtensionRegistry::TERMINATED;
[email protected]9adb9692010-10-29 23:14:021609 scoped_refptr<const Extension> extension(
[email protected]599539802014-01-07 23:06:001610 registry_->GetExtensionById(extension_id, include_mask));
[email protected]631cf822009-05-15 07:01:251611
[email protected]fa0c96732010-11-17 00:14:231612 // This method can be called via PostTask, so the extension may have been
1613 // unloaded by the time this runs.
[email protected]dc24976f2013-06-02 21:15:091614 if (!extension.get()) {
[email protected]dd163fb02011-05-04 22:22:171615 // In case the extension may have crashed/uninstalled. Allow the profile to
1616 // clean up its RequestContexts.
[email protected]31d8f5f22012-04-02 15:22:081617 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]fa0c96732010-11-17 00:14:231618 return;
[email protected]dd163fb02011-05-04 22:22:171619 }
[email protected]0c6da502009-08-14 22:32:391620
[email protected]1eb175082010-02-10 09:26:161621 // Keep information about the extension so that we can reload it later
1622 // even if it's not permanently installed.
1623 unloaded_extension_paths_[extension->id()] = extension->path();
1624
[email protected]f17dbd42010-08-16 23:21:101625 // Clean up if the extension is meant to be enabled after a reload.
[email protected]b914e2952013-04-26 07:10:031626 reloading_extensions_.erase(extension->id());
[email protected]f17dbd42010-08-16 23:21:101627
[email protected]bb1bc9b32013-12-21 03:09:141628 if (registry_->disabled_extensions().Contains(extension->id())) {
1629 registry_->RemoveDisabled(extension->id());
[email protected]dd163fb02011-05-04 22:22:171630 // Make sure the profile cleans up its RequestContexts when an already
1631 // disabled extension is unloaded (since they are also tracking the disabled
1632 // extensions).
[email protected]31d8f5f22012-04-02 15:22:081633 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]45f5b7d2014-01-22 23:47:131634 // Don't send the unloaded notification. It was sent when the extension
1635 // was disabled.
[email protected]60a174a2013-08-02 20:29:281636 } else {
[email protected]bb1bc9b32013-12-21 03:09:141637 // Remove the extension from the enabled list.
1638 registry_->RemoveEnabled(extension->id());
[email protected]60a174a2013-08-02 20:29:281639 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]0c6da502009-08-14 22:32:391640 }
1641
[email protected]fb789532013-08-27 02:40:211642 content::NotificationService::current()->Notify(
1643 chrome::NOTIFICATION_EXTENSION_REMOVED,
1644 content::Source<Profile>(profile_),
1645 content::Details<const Extension>(extension.get()));
[email protected]631cf822009-05-15 07:01:251646}
1647
[email protected]8b1ec202013-09-05 02:09:501648void ExtensionService::RemoveComponentExtension(
1649 const std::string& extension_id) {
1650 scoped_refptr<const Extension> extension(
1651 GetExtensionById(extension_id, false));
[email protected]b0af4792013-10-23 09:12:131652 UnloadExtension(extension_id, UnloadedExtensionInfo::REASON_UNINSTALL);
[email protected]8b1ec202013-09-05 02:09:501653 content::NotificationService::current()->Notify(
1654 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
1655 content::Source<Profile>(profile_),
1656 content::Details<const Extension>(extension.get()));
1657}
1658
[email protected]bb1bc9b32013-12-21 03:09:141659void ExtensionService::UnloadAllExtensionsForTest() {
1660 UnloadAllExtensionsInternal();
[email protected]9f1087e2009-06-15 17:29:321661}
1662
[email protected]bb1bc9b32013-12-21 03:09:141663void ExtensionService::ReloadExtensionsForTest() {
1664 // Calling UnloadAllExtensionsForTest here triggers a false-positive presubmit
1665 // warning about calling test code in production.
1666 UnloadAllExtensionsInternal();
[email protected]eac88332012-12-26 17:57:451667 component_loader_->LoadAll();
[email protected]d8c8f25f2011-11-02 18:18:011668 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]4a10006a2013-05-17 23:18:351669 // Don't call SetReadyAndNotifyListeners() since tests call this multiple
1670 // times.
[email protected]9f1087e2009-06-15 17:29:321671}
1672
[email protected]eaa7dd182010-12-14 11:09:001673void ExtensionService::GarbageCollectExtensions() {
[email protected]c04fd3dd2013-09-05 08:20:101674#if defined(OS_CHROMEOS)
1675 if (disable_garbage_collection_)
1676 return;
1677#endif
1678
[email protected]2f8757c32012-06-19 19:17:471679 if (extension_prefs_->pref_service()->ReadOnly())
1680 return;
1681
[email protected]e8107c12014-01-14 15:38:011682 bool clean_temp_dir = true;
1683
[email protected]9bd9a6862012-11-29 09:24:221684 if (pending_extension_manager()->HasPendingExtensions()) {
[email protected]e8107c12014-01-14 15:38:011685 // Don't garbage collect temp dir while there are pending installations,
1686 // which may be using the temporary installation directory. Try to garbage
1687 // collect again later.
1688 clean_temp_dir = false;
[email protected]b3a25092013-05-28 22:08:161689 base::MessageLoop::current()->PostDelayedTask(
[email protected]9bd9a6862012-11-29 09:24:221690 FROM_HERE,
1691 base::Bind(&ExtensionService::GarbageCollectExtensions, AsWeakPtr()),
1692 base::TimeDelta::FromSeconds(kGarbageCollectRetryDelay));
[email protected]9bd9a6862012-11-29 09:24:221693 }
1694
[email protected]45759612012-07-10 17:21:231695 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> info(
[email protected]2f8757c32012-06-19 19:17:471696 extension_prefs_->GetInstalledExtensionsInfo());
1697
[email protected]650b2d52013-02-10 03:41:451698 std::multimap<std::string, base::FilePath> extension_paths;
[email protected]2f8757c32012-06-19 19:17:471699 for (size_t i = 0; i < info->size(); ++i)
[email protected]0db124b02012-11-07 04:55:051700 extension_paths.insert(std::make_pair(info->at(i)->extension_id,
1701 info->at(i)->extension_path));
1702
[email protected]399583b2012-12-11 09:33:421703 info = extension_prefs_->GetAllDelayedInstallInfo();
[email protected]0db124b02012-11-07 04:55:051704 for (size_t i = 0; i < info->size(); ++i)
1705 extension_paths.insert(std::make_pair(info->at(i)->extension_id,
1706 info->at(i)->extension_path));
[email protected]2f8757c32012-06-19 19:17:471707
[email protected]7f8f24f2012-11-15 19:40:141708 if (!GetFileTaskRunner()->PostTask(
1709 FROM_HERE,
[email protected]2f8757c32012-06-19 19:17:471710 base::Bind(
1711 &extension_file_util::GarbageCollectExtensions,
1712 install_directory_,
[email protected]e8107c12014-01-14 15:38:011713 extension_paths,
1714 clean_temp_dir))) {
[email protected]2f8757c32012-06-19 19:17:471715 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:401716 }
[email protected]3cf4f0992009-02-03 23:00:301717}
1718
[email protected]820d9bd2013-04-03 03:46:031719void ExtensionService::SetReadyAndNotifyListeners() {
[email protected]4a10006a2013-05-17 23:18:351720 ready_->Signal();
[email protected]820d9bd2013-04-03 03:46:031721 content::NotificationService::current()->Notify(
1722 chrome::NOTIFICATION_EXTENSIONS_READY,
1723 content::Source<Profile>(profile_),
1724 content::NotificationService::NoDetails());
1725}
1726
[email protected]eaa7dd182010-12-14 11:09:001727void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]3eeddd892013-04-17 17:00:111728 if (updater_)
[email protected]93fd78f42009-07-10 16:43:171729 updater_->Start();
[email protected]fa6a9102010-11-22 15:38:501730
[email protected]bc151cf92013-02-12 04:57:261731 OnBlacklistUpdated();
[email protected]e72e8eb82009-06-18 17:21:511732}
1733
[email protected]fcb58a862012-05-01 01:03:151734void ExtensionService::AddExtension(const Extension* extension) {
[email protected]c8d407e2011-04-28 21:27:171735 // TODO(jstritar): We may be able to get rid of this branch by overriding the
1736 // default extension state to DISABLED when the --disable-extensions flag
1737 // is set (http://crbug.com/29067).
1738 if (!extensions_enabled() &&
1739 !extension->is_theme() &&
[email protected]1d5e58b2013-01-31 08:41:401740 extension->location() != Manifest::COMPONENT &&
1741 !Manifest::IsExternalLocation(extension->location())) {
[email protected]fcb58a862012-05-01 01:03:151742 return;
[email protected]330840c2012-08-29 22:21:011743 }
[email protected]c8d407e2011-04-28 21:27:171744
[email protected]b914e2952013-04-26 07:10:031745 bool is_extension_upgrade = false;
[email protected]116d40e2013-08-08 17:23:171746 bool is_extension_installed = false;
1747 const Extension* old = GetInstalledExtension(extension->id());
1748 if (old) {
1749 is_extension_installed = true;
1750 int version_compare_result =
1751 extension->version()->CompareTo(*(old->version()));
1752 is_extension_upgrade = version_compare_result > 0;
[email protected]b914e2952013-04-26 07:10:031753 // Other than for unpacked extensions, CrxInstaller should have guaranteed
1754 // that we aren't downgrading.
1755 if (!Manifest::IsUnpackedLocation(extension->location()))
[email protected]116d40e2013-08-08 17:23:171756 CHECK_GE(version_compare_result, 0);
[email protected]b914e2952013-04-26 07:10:031757 }
[email protected]45f5b7d2014-01-22 23:47:131758 system_->runtime_data()->SetBeingUpgraded(extension, is_extension_upgrade);
[email protected]c8d407e2011-04-28 21:27:171759
[email protected]1eb175082010-02-10 09:26:161760 // The extension is now loaded, remove its data from unloaded extension map.
1761 unloaded_extension_paths_.erase(extension->id());
1762
[email protected]bb7f40952011-01-13 00:21:201763 // If a terminated extension is loaded, remove it from the terminated list.
1764 UntrackTerminatedExtension(extension->id());
1765
[email protected]f17dbd42010-08-16 23:21:101766 // If the extension was disabled for a reload, then enable it.
[email protected]ca2e82022013-04-29 16:48:411767 bool reloading = reloading_extensions_.erase(extension->id()) > 0;
[email protected]f17dbd42010-08-16 23:21:101768
[email protected]b914e2952013-04-26 07:10:031769 // Check if the extension's privileges have changed and mark the
1770 // extension disabled if necessary.
[email protected]116d40e2013-08-08 17:23:171771 CheckPermissionsIncrease(extension, is_extension_installed);
[email protected]b914e2952013-04-26 07:10:031772
[email protected]116d40e2013-08-08 17:23:171773 if (is_extension_installed && !reloading) {
[email protected]ca2e82022013-04-29 16:48:411774 // To upgrade an extension in place, unload the old one and then load the
1775 // new one. ReloadExtension disables the extension, which is sufficient.
[email protected]b0af4792013-10-23 09:12:131776 UnloadExtension(extension->id(), UnloadedExtensionInfo::REASON_UPDATE);
[email protected]b914e2952013-04-26 07:10:031777 }
[email protected]0c6da502009-08-14 22:32:391778
[email protected]695b5712012-12-06 23:55:281779 if (extension_prefs_->IsExtensionBlacklisted(extension->id())) {
[email protected]ac875372013-02-28 04:36:091780 // Only prefs is checked for the blacklist. We rely on callers to check the
1781 // blacklist before calling into here, e.g. CrxInstaller checks before
[email protected]9f3c8532013-07-31 19:52:071782 // installation then threads through the install and pending install flow
1783 // of this class, and we check when loading installed extensions.
[email protected]bb1bc9b32013-12-21 03:09:141784 registry_->AddBlacklisted(extension);
[email protected]ca2e82022013-04-29 16:48:411785 } else if (!reloading &&
1786 extension_prefs_->IsExtensionDisabled(extension->id())) {
[email protected]bb1bc9b32013-12-21 03:09:141787 registry_->AddDisabled(extension);
[email protected]f8aefb132013-10-30 09:29:521788 if (extension_sync_service_)
1789 extension_sync_service_->SyncExtensionChangeIfNeeded(*extension);
[email protected]44d62b62012-04-11 00:06:031790 content::NotificationService::current()->Notify(
1791 chrome::NOTIFICATION_EXTENSION_UPDATE_DISABLED,
1792 content::Source<Profile>(profile_),
1793 content::Details<const Extension>(extension));
[email protected]62f051c2012-03-29 17:04:441794
[email protected]7e9e2422012-12-13 19:54:191795 // Show the extension disabled error if a permissions increase was the
1796 // only reason it was disabled.
1797 if (extension_prefs_->GetDisableReasons(extension->id()) ==
[email protected]44d62b62012-04-11 00:06:031798 Extension::DISABLE_PERMISSIONS_INCREASE) {
[email protected]62f051c2012-03-29 17:04:441799 extensions::AddExtensionDisabledError(this, extension);
1800 }
[email protected]ca2e82022013-04-29 16:48:411801 } else if (reloading) {
1802 // Replace the old extension with the new version.
[email protected]bb1bc9b32013-12-21 03:09:141803 CHECK(!registry_->AddDisabled(extension));
[email protected]ca2e82022013-04-29 16:48:411804 EnableExtension(extension->id());
[email protected]695b5712012-12-06 23:55:281805 } else {
1806 // All apps that are displayed in the launcher are ordered by their ordinals
1807 // so we must ensure they have valid ordinals.
1808 if (extension->RequiresSortOrdinal()) {
[email protected]bd36b652013-02-27 02:13:251809 if (!extension->ShouldDisplayInNewTabPage()) {
[email protected]74474042013-11-21 12:03:541810 extension_prefs_->app_sorting()->MarkExtensionAsHidden(extension->id());
[email protected]bd36b652013-02-27 02:13:251811 }
[email protected]74474042013-11-21 12:03:541812 extension_prefs_->app_sorting()->EnsureValidOrdinals(
[email protected]695b5712012-12-06 23:55:281813 extension->id(), syncer::StringOrdinal());
1814 }
1815
[email protected]bb1bc9b32013-12-21 03:09:141816 registry_->AddEnabled(extension);
[email protected]f8aefb132013-10-30 09:29:521817 if (extension_sync_service_)
1818 extension_sync_service_->SyncExtensionChangeIfNeeded(*extension);
[email protected]695b5712012-12-06 23:55:281819 NotifyExtensionLoaded(extension);
[email protected]f574c402012-12-04 23:20:311820 }
[email protected]45f5b7d2014-01-22 23:47:131821 system_->runtime_data()->SetBeingUpgraded(extension, false);
[email protected]aab98a52009-12-02 03:22:351822}
1823
[email protected]8c484b742012-11-29 06:05:361824void ExtensionService::AddComponentExtension(const Extension* extension) {
1825 const std::string old_version_string(
1826 extension_prefs_->GetVersionString(extension->id()));
[email protected]c5e4a2222014-01-03 16:06:131827 const Version old_version(old_version_string);
[email protected]8c484b742012-11-29 06:05:361828
[email protected]0181a7d2013-11-12 01:08:421829 VLOG(1) << "AddComponentExtension " << extension->name();
[email protected]8c484b742012-11-29 06:05:361830 if (!old_version.IsValid() || !old_version.Equals(*extension->version())) {
1831 VLOG(1) << "Component extension " << extension->name() << " ("
1832 << extension->id() << ") installing/upgrading from '"
1833 << old_version_string << "' to " << extension->version()->GetString();
1834
1835 AddNewOrUpdatedExtension(extension,
[email protected]399583b2012-12-11 09:33:421836 Extension::ENABLED_COMPONENT,
[email protected]8e289f0b2013-12-17 17:49:071837 extensions::NOT_BLACKLISTED,
[email protected]399583b2012-12-11 09:33:421838 syncer::StringOrdinal());
[email protected]8c484b742012-11-29 06:05:361839 return;
1840 }
1841
1842 AddExtension(extension);
1843}
1844
[email protected]b914e2952013-04-26 07:10:031845void ExtensionService::UpdateActivePermissions(const Extension* extension) {
[email protected]902fd7b2011-07-27 18:42:311846 // If the extension has used the optional permissions API, it will have a
1847 // custom set of active permissions defined in the extension prefs. Here,
1848 // we update the extension's active permissions based on the prefs.
[email protected]c2e66e12012-06-27 06:27:061849 scoped_refptr<PermissionSet> active_permissions =
[email protected]7c82539c2014-02-19 06:09:171850 extension_prefs_->GetActivePermissions(extension->id());
[email protected]902fd7b2011-07-27 18:42:311851
[email protected]dc24976f2013-06-02 21:15:091852 if (active_permissions.get()) {
[email protected]902fd7b2011-07-27 18:42:311853 // We restrict the active permissions to be within the bounds defined in the
1854 // extension's manifest.
1855 // a) active permissions must be a subset of optional + default permissions
1856 // b) active permissions must contains all default permissions
[email protected]c2e66e12012-06-27 06:27:061857 scoped_refptr<PermissionSet> total_permissions =
1858 PermissionSet::CreateUnion(
[email protected]2cb5e302013-05-09 05:00:061859 extensions::PermissionsData::GetRequiredPermissions(extension),
1860 extensions::PermissionsData::GetOptionalPermissions(extension));
[email protected]902fd7b2011-07-27 18:42:311861
1862 // Make sure the active permissions contain no more than optional + default.
[email protected]c2e66e12012-06-27 06:27:061863 scoped_refptr<PermissionSet> adjusted_active =
1864 PermissionSet::CreateIntersection(
[email protected]902fd7b2011-07-27 18:42:311865 total_permissions.get(), active_permissions.get());
1866
1867 // Make sure the active permissions contain the default permissions.
[email protected]c2e66e12012-06-27 06:27:061868 adjusted_active = PermissionSet::CreateUnion(
[email protected]2cb5e302013-05-09 05:00:061869 extensions::PermissionsData::GetRequiredPermissions(extension),
1870 adjusted_active.get());
[email protected]902fd7b2011-07-27 18:42:311871
[email protected]c333e792012-01-06 16:57:391872 extensions::PermissionsUpdater perms_updater(profile());
[email protected]dc24976f2013-06-02 21:15:091873 perms_updater.UpdateActivePermissions(extension, adjusted_active.get());
[email protected]902fd7b2011-07-27 18:42:311874 }
[email protected]b914e2952013-04-26 07:10:031875}
1876
1877void ExtensionService::CheckPermissionsIncrease(const Extension* extension,
[email protected]116d40e2013-08-08 17:23:171878 bool is_extension_installed) {
[email protected]b914e2952013-04-26 07:10:031879 UpdateActivePermissions(extension);
[email protected]902fd7b2011-07-27 18:42:311880
[email protected]8d888c12010-11-30 00:00:251881 // We keep track of all permissions the user has granted each extension.
1882 // This allows extensions to gracefully support backwards compatibility
1883 // by including unknown permissions in their manifests. When the user
1884 // installs the extension, only the recognized permissions are recorded.
1885 // When the unknown permissions become recognized (e.g., through browser
1886 // upgrade), we can prompt the user to accept these new permissions.
1887 // Extensions can also silently upgrade to less permissions, and then
1888 // silently upgrade to a version that adds these permissions back.
1889 //
1890 // For example, pretend that Chrome 10 includes a permission "omnibox"
1891 // for an API that adds suggestions to the omnibox. An extension can
1892 // maintain backwards compatibility while still having "omnibox" in the
1893 // manifest. If a user installs the extension on Chrome 9, the browser
1894 // will record the permissions it recognized, not including "omnibox."
1895 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
1896 // will disable the extension and prompt the user to approve the increase
1897 // in privileges. The extension could then release a new version that
1898 // removes the "omnibox" permission. When the user upgrades, Chrome will
1899 // still remember that "omnibox" had been granted, so that if the
1900 // extension once again includes "omnibox" in an upgrade, the extension
1901 // can upgrade without requiring this user's approval.
[email protected]eb5e4f92012-08-15 23:33:281902 int disable_reasons = extension_prefs_->GetDisableReasons(extension->id());
[email protected]8d888c12010-11-30 00:00:251903
[email protected]211a3f32013-05-28 21:48:181904 bool auto_grant_permission =
[email protected]1b563df2013-10-30 16:49:361905 (!is_extension_installed && extension->was_installed_by_default()) ||
[email protected]45928b682013-11-28 08:20:271906 extensions::ExtensionsBrowserClient::Get()->IsRunningInForcedAppMode();
[email protected]b914e2952013-04-26 07:10:031907 // Silently grant all active permissions to default apps only on install.
1908 // After install they should behave like other apps.
[email protected]211a3f32013-05-28 21:48:181909 // Silently grant all active permissions to apps install in kiosk mode on both
1910 // install and update.
1911 if (auto_grant_permission)
[email protected]b914e2952013-04-26 07:10:031912 GrantPermissions(extension);
1913
1914 bool is_privilege_increase = false;
1915 // We only need to compare the granted permissions to the current permissions
1916 // if the extension is not allowed to silently increase its permissions.
[email protected]13c68b62013-05-17 11:29:051917 if (!extensions::PermissionsData::CanSilentlyIncreasePermissions(extension) &&
[email protected]211a3f32013-05-28 21:48:181918 !auto_grant_permission) {
[email protected]8d888c12010-11-30 00:00:251919 // Add all the recognized permissions if the granted permissions list
1920 // hasn't been initialized yet.
[email protected]c2e66e12012-06-27 06:27:061921 scoped_refptr<PermissionSet> granted_permissions =
[email protected]902fd7b2011-07-27 18:42:311922 extension_prefs_->GetGrantedPermissions(extension->id());
[email protected]0d3e4a22011-06-23 19:02:521923 CHECK(granted_permissions.get());
[email protected]8d888c12010-11-30 00:00:251924
1925 // Here, we check if an extension's privileges have increased in a manner
1926 // that requires the user's approval. This could occur because the browser
1927 // upgraded and recognized additional privileges, or an extension upgrades
1928 // to a version that requires additional privileges.
[email protected]c41003472013-10-19 15:37:251929 is_privilege_increase =
1930 extensions::PermissionMessageProvider::Get()->IsPrivilegeIncrease(
1931 granted_permissions,
1932 extension->GetActivePermissions().get(),
1933 extension->GetType());
[email protected]8d888c12010-11-30 00:00:251934 }
1935
[email protected]116d40e2013-08-08 17:23:171936 if (is_extension_installed) {
[email protected]44d62b62012-04-11 00:06:031937 // If the extension was already disabled, suppress any alerts for becoming
1938 // disabled on permissions increase.
[email protected]b914e2952013-04-26 07:10:031939 bool previously_disabled =
1940 extension_prefs_->IsExtensionDisabled(extension->id());
[email protected]7e9e2422012-12-13 19:54:191941 // Legacy disabled extensions do not have a disable reason. Infer that if
1942 // there was no permission increase, it was likely disabled by the user.
1943 if (previously_disabled && disable_reasons == Extension::DISABLE_NONE &&
[email protected]b914e2952013-04-26 07:10:031944 !extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
[email protected]7e9e2422012-12-13 19:54:191945 disable_reasons |= Extension::DISABLE_USER_ACTION;
[email protected]44d62b62012-04-11 00:06:031946 }
[email protected]7e9e2422012-12-13 19:54:191947 // Extensions that came to us disabled from sync need a similar inference,
1948 // except based on the new version's permissions.
1949 if (previously_disabled &&
1950 disable_reasons == Extension::DISABLE_UNKNOWN_FROM_SYNC) {
1951 // Remove the DISABLE_UNKNOWN_FROM_SYNC reason.
1952 extension_prefs_->ClearDisableReasons(extension->id());
1953 if (!is_privilege_increase)
1954 disable_reasons |= Extension::DISABLE_USER_ACTION;
1955 }
[email protected]b914e2952013-04-26 07:10:031956 disable_reasons &= ~Extension::DISABLE_UNKNOWN_FROM_SYNC;
[email protected]8d888c12010-11-30 00:00:251957 }
1958
1959 // Extension has changed permissions significantly. Disable it. A
1960 // notification should be sent by the caller.
1961 if (is_privilege_increase) {
[email protected]7e9e2422012-12-13 19:54:191962 disable_reasons |= Extension::DISABLE_PERMISSIONS_INCREASE;
[email protected]fe2dd7742011-04-19 22:52:491963 if (!extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
1964 RecordPermissionMessagesHistogram(
1965 extension, "Extensions.Permissions_AutoDisable");
1966 }
[email protected]195c1582013-10-29 18:42:231967 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
[email protected]8d888c12010-11-30 00:00:251968 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
[email protected]b914e2952013-04-26 07:10:031969 }
1970 if (disable_reasons != Extension::DISABLE_NONE) {
[email protected]eb5e4f92012-08-15 23:33:281971 extension_prefs_->AddDisableReason(
1972 extension->id(),
1973 static_cast<Extension::DisableReason>(disable_reasons));
[email protected]8d888c12010-11-30 00:00:251974 }
1975}
1976
[email protected]eaa7dd182010-12-14 11:09:001977void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:391978 std::set<std::string> extension_ids;
[email protected]bb1bc9b32013-12-21 03:09:141979 const ExtensionSet& extensions = registry_->enabled_extensions();
1980 for (ExtensionSet::const_iterator iter = extensions.begin();
1981 iter != extensions.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361982 const Extension* extension = iter->get();
[email protected]1d5e58b2013-01-31 08:41:401983 if (!extension->is_theme() && extension->location() != Manifest::COMPONENT)
[email protected]84df8332011-12-06 18:22:461984 extension_ids.insert(extension->id());
[email protected]aab98a52009-12-02 03:22:351985 }
1986
[email protected]f6431be82013-09-07 02:53:451987 crash_keys::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:251988}
1989
[email protected]669b2372013-10-17 15:04:581990ExtensionService::ImportStatus ExtensionService::CheckImports(
1991 const extensions::Extension* extension,
1992 std::list<SharedModuleInfo::ImportInfo>* missing_modules,
1993 std::list<SharedModuleInfo::ImportInfo>* outdated_modules) {
1994 DCHECK(extension);
1995 DCHECK(missing_modules && missing_modules->empty());
1996 DCHECK(outdated_modules && outdated_modules->empty());
[email protected]9f4e4f082013-06-21 07:11:191997 ImportStatus status = IMPORT_STATUS_OK;
[email protected]9f4e4f082013-06-21 07:11:191998 if (SharedModuleInfo::ImportsModules(extension)) {
1999 const std::vector<SharedModuleInfo::ImportInfo>& imports =
2000 SharedModuleInfo::GetImports(extension);
2001 std::vector<SharedModuleInfo::ImportInfo>::const_iterator i;
2002 for (i = imports.begin(); i != imports.end(); ++i) {
[email protected]c5e4a2222014-01-03 16:06:132003 Version version_required(i->minimum_version);
[email protected]9f4e4f082013-06-21 07:11:192004 const Extension* imported_module =
2005 GetExtensionById(i->extension_id, true);
2006 if (!imported_module) {
2007 if (extension->from_webstore()) {
2008 status = IMPORT_STATUS_UNSATISFIED;
[email protected]669b2372013-10-17 15:04:582009 missing_modules->push_back(*i);
[email protected]9f4e4f082013-06-21 07:11:192010 } else {
2011 return IMPORT_STATUS_UNRECOVERABLE;
2012 }
2013 } else if (!SharedModuleInfo::IsSharedModule(imported_module)) {
2014 return IMPORT_STATUS_UNRECOVERABLE;
2015 } else if (version_required.IsValid() &&
2016 imported_module->version()->CompareTo(version_required) < 0) {
2017 if (imported_module->from_webstore()) {
[email protected]669b2372013-10-17 15:04:582018 outdated_modules->push_back(*i);
[email protected]9f4e4f082013-06-21 07:11:192019 status = IMPORT_STATUS_UNSATISFIED;
2020 } else {
2021 return IMPORT_STATUS_UNRECOVERABLE;
2022 }
2023 }
2024 }
2025 }
[email protected]669b2372013-10-17 15:04:582026 return status;
2027}
2028
2029ExtensionService::ImportStatus ExtensionService::SatisfyImports(
2030 const Extension* extension) {
2031 std::list<SharedModuleInfo::ImportInfo> noinstalled;
2032 std::list<SharedModuleInfo::ImportInfo> outdated;
2033 ImportStatus status = CheckImports(extension, &noinstalled, &outdated);
2034 if (status == IMPORT_STATUS_UNRECOVERABLE)
2035 return status;
[email protected]9f4e4f082013-06-21 07:11:192036 if (status == IMPORT_STATUS_UNSATISFIED) {
[email protected]669b2372013-10-17 15:04:582037 std::list<SharedModuleInfo::ImportInfo>::const_iterator iter;
2038 for (iter = noinstalled.begin(); iter != noinstalled.end(); ++iter) {
[email protected]9f4e4f082013-06-21 07:11:192039 pending_extension_manager()->AddFromExtensionImport(
[email protected]669b2372013-10-17 15:04:582040 iter->extension_id,
[email protected]9f4e4f082013-06-21 07:11:192041 extension_urls::GetWebstoreUpdateUrl(),
2042 IsSharedModule);
2043 }
2044 CheckForUpdatesSoon();
2045 }
2046 return status;
2047}
2048
2049scoped_ptr<const ExtensionSet>
2050 ExtensionService::GetDependentExtensions(const Extension* extension) {
2051 scoped_ptr<ExtensionSet> dependents(new ExtensionSet());
2052 scoped_ptr<ExtensionSet> set_to_check(new ExtensionSet());
2053 if (SharedModuleInfo::IsSharedModule(extension)) {
[email protected]bb1bc9b32013-12-21 03:09:142054 set_to_check->InsertAll(registry_->disabled_extensions());
[email protected]9f4e4f082013-06-21 07:11:192055 set_to_check->InsertAll(delayed_installs_);
[email protected]bb1bc9b32013-12-21 03:09:142056 set_to_check->InsertAll(registry_->enabled_extensions());
[email protected]9f4e4f082013-06-21 07:11:192057 for (ExtensionSet::const_iterator iter = set_to_check->begin();
2058 iter != set_to_check->end(); ++iter) {
[email protected]198b5902013-06-27 10:36:112059 if (SharedModuleInfo::ImportsExtensionById(iter->get(),
2060 extension->id())) {
[email protected]9f4e4f082013-06-21 07:11:192061 dependents->Insert(*iter);
2062 }
2063 }
2064 }
2065 return dependents.PassAs<const ExtensionSet>();
2066}
2067
2068void ExtensionService::PruneSharedModulesOnUninstall(
2069 const Extension* extension) {
2070 if (SharedModuleInfo::ImportsModules(extension)) {
2071 const std::vector<SharedModuleInfo::ImportInfo>& imports =
2072 SharedModuleInfo::GetImports(extension);
2073 std::vector<SharedModuleInfo::ImportInfo>::const_iterator i;
2074 for (i = imports.begin(); i != imports.end(); ++i) {
2075 const Extension* imported_module =
2076 GetExtensionById(i->extension_id, true);
2077 if (imported_module && imported_module->from_webstore()) {
2078 scoped_ptr<const ExtensionSet> dependents =
2079 GetDependentExtensions(imported_module);
2080 if (dependents->size() == 0) {
[email protected]4c9201c42013-08-16 04:56:212081 UninstallExtension(i->extension_id, true, NULL);
[email protected]9f4e4f082013-06-21 07:11:192082 }
2083 }
2084 }
2085 }
2086}
2087
[email protected]8266d662011-07-12 21:53:262088void ExtensionService::OnExtensionInstalled(
[email protected]36a5c4c2011-12-14 16:34:502089 const Extension* extension,
[email protected]98270432012-09-11 20:51:242090 const syncer::StringOrdinal& page_ordinal,
[email protected]0db124b02012-11-07 04:55:052091 bool has_requirement_errors,
[email protected]8e289f0b2013-12-17 17:49:072092 extensions::BlacklistState blacklist_state,
[email protected]0db124b02012-11-07 04:55:052093 bool wait_for_idle) {
[email protected]a29a517a2011-01-21 21:11:122094 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:202095
[email protected]c3cfb012011-04-06 22:07:352096 const std::string& id = extension->id();
[email protected]612a1cb12012-10-17 13:18:032097 bool initial_enable = ShouldEnableOnInstall(extension);
[email protected]3f213ad2012-07-26 23:39:412098 const extensions::PendingExtensionInfo* pending_extension_info = NULL;
[email protected]51a3bf8b2012-06-08 22:53:062099 if ((pending_extension_info = pending_extension_manager()->GetById(id))) {
[email protected]8f3bcbd2013-06-05 08:42:402100 if (!pending_extension_info->ShouldAllowInstall(extension)) {
[email protected]51a3bf8b2012-06-08 22:53:062101 pending_extension_manager()->Remove(id);
[email protected]8ef78fd2010-08-19 17:14:322102
[email protected]31bb5ee62012-09-12 22:58:402103 LOG(WARNING) << "ShouldAllowInstall() returned false for "
2104 << id << " of type " << extension->GetType()
[email protected]65348062013-01-15 07:27:222105 << " and update URL "
2106 << extensions::ManifestURL::GetUpdateURL(extension).spec()
[email protected]31bb5ee62012-09-12 22:58:402107 << "; not installing";
[email protected]ae6cb3022011-04-04 20:11:542108
[email protected]4416c5a2010-06-26 01:28:572109 // Delete the extension directory since we're not going to
2110 // load it.
[email protected]7f8f24f2012-11-15 19:40:142111 if (!GetFileTaskRunner()->PostTask(
2112 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362113 base::Bind(&extension_file_util::DeleteFile,
[email protected]31bb5ee62012-09-12 22:58:402114 extension->path(), true))) {
[email protected]14908b72011-04-20 06:54:362115 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:402116 }
[email protected]4416c5a2010-06-26 01:28:572117 return;
2118 }
[email protected]51a3bf8b2012-06-08 22:53:062119
2120 pending_extension_manager()->Remove(id);
[email protected]4416c5a2010-06-26 01:28:572121 } else {
[email protected]c3cfb012011-04-06 22:07:352122 // We explicitly want to re-enable an uninstalled external
2123 // extension; if we're here, that means the user is manually
2124 // installing the extension.
[email protected]6cc7dbae2011-04-29 21:18:332125 if (IsExternalExtensionUninstalled(id)) {
2126 initial_enable = true;
2127 }
[email protected]aa142702010-03-26 01:26:332128 }
2129
[email protected]98270432012-09-11 20:51:242130 // Unsupported requirements overrides the management policy.
2131 if (has_requirement_errors) {
2132 initial_enable = false;
2133 extension_prefs_->AddDisableReason(
2134 id, Extension::DISABLE_UNSUPPORTED_REQUIREMENT);
2135 // If the extension was disabled because of unsupported requirements but
2136 // now supports all requirements after an update and there are not other
2137 // disable reasons, enable it.
2138 } else if (extension_prefs_->GetDisableReasons(id) ==
2139 Extension::DISABLE_UNSUPPORTED_REQUIREMENT) {
2140 initial_enable = true;
2141 extension_prefs_->ClearDisableReasons(id);
2142 }
2143
[email protected]8e289f0b2013-12-17 17:49:072144 if (blacklist_state == extensions::BLACKLISTED_MALWARE) {
[email protected]9f3c8532013-07-31 19:52:072145 // Installation of a blacklisted extension can happen from sync, policy,
2146 // etc, where to maintain consistency we need to install it, just never
2147 // load it (see AddExtension). Usually it should be the job of callers to
2148 // incercept blacklisted extension earlier (e.g. CrxInstaller, before even
2149 // showing the install dialogue).
[email protected]7c82539c2014-02-19 06:09:172150 extension_prefs_->AcknowledgeBlacklistedExtension(id);
[email protected]9f3c8532013-07-31 19:52:072151 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.SilentInstall",
2152 extension->location(),
2153 Manifest::NUM_LOCATIONS);
2154 }
2155
[email protected]695b5712012-12-06 23:55:282156 if (!GetInstalledExtension(extension->id())) {
[email protected]07533022011-06-27 20:42:552157 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
2158 extension->GetType(), 100);
[email protected]69084dc2012-11-17 07:39:302159 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallSource",
[email protected]1d5e58b2013-01-31 08:41:402160 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]07533022011-06-27 20:42:552161 RecordPermissionMessagesHistogram(
2162 extension, "Extensions.Permissions_Install");
[email protected]69084dc2012-11-17 07:39:302163 } else {
2164 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateType",
2165 extension->GetType(), 100);
2166 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateSource",
[email protected]1d5e58b2013-01-31 08:41:402167 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]07533022011-06-27 20:42:552168 }
2169
[email protected]a39921b42012-02-28 03:42:542170 // Certain extension locations are specific enough that we can
2171 // auto-acknowledge any extension that came from one of them.
[email protected]4eef4352014-02-12 02:21:362172 if (Manifest::IsPolicyLocation(extension->location()) ||
2173 extension->location() == Manifest::EXTERNAL_COMPONENT)
[email protected]a39921b42012-02-28 03:42:542174 AcknowledgeExternalExtension(extension->id());
[email protected]399583b2012-12-11 09:33:422175 const Extension::State initial_state =
2176 initial_enable ? Extension::ENABLED : Extension::DISABLED;
[email protected]0f9e6262013-11-05 23:49:192177 const bool blacklisted_for_malware =
[email protected]8e289f0b2013-12-17 17:49:072178 blacklist_state == extensions::BLACKLISTED_MALWARE;
[email protected]e7aa7b7e2012-11-27 04:51:222179 if (ShouldDelayExtensionUpdate(id, wait_for_idle)) {
[email protected]9f3c8532013-07-31 19:52:072180 extension_prefs_->SetDelayedInstallInfo(
2181 extension,
2182 initial_state,
[email protected]0f9e6262013-11-05 23:49:192183 blacklisted_for_malware,
[email protected]9f3c8532013-07-31 19:52:072184 extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE,
2185 page_ordinal);
[email protected]0db124b02012-11-07 04:55:052186
2187 // Transfer ownership of |extension|.
[email protected]9f4e4f082013-06-21 07:11:192188 delayed_installs_.Insert(extension);
[email protected]0db124b02012-11-07 04:55:052189
[email protected]75bdcb872013-03-13 00:41:452190 // Notify observers that app update is available.
2191 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
[email protected]a9aa62b312013-11-29 05:35:062192 OnAppUpdateAvailable(extension));
[email protected]0db124b02012-11-07 04:55:052193 return;
2194 }
2195
[email protected]9f4e4f082013-06-21 07:11:192196 ImportStatus status = SatisfyImports(extension);
2197 if (installs_delayed_for_gc()) {
[email protected]9f3c8532013-07-31 19:52:072198 extension_prefs_->SetDelayedInstallInfo(
2199 extension,
2200 initial_state,
[email protected]0f9e6262013-11-05 23:49:192201 blacklisted_for_malware,
[email protected]9f3c8532013-07-31 19:52:072202 extensions::ExtensionPrefs::DELAY_REASON_GC,
2203 page_ordinal);
[email protected]399583b2012-12-11 09:33:422204 delayed_installs_.Insert(extension);
[email protected]9f4e4f082013-06-21 07:11:192205 } else if (status != IMPORT_STATUS_OK) {
2206 if (status == IMPORT_STATUS_UNSATISFIED) {
[email protected]9f3c8532013-07-31 19:52:072207 extension_prefs_->SetDelayedInstallInfo(
2208 extension,
2209 initial_state,
[email protected]0f9e6262013-11-05 23:49:192210 blacklisted_for_malware,
[email protected]9f4e4f082013-06-21 07:11:192211 extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IMPORTS,
2212 page_ordinal);
2213 delayed_installs_.Insert(extension);
2214 }
[email protected]399583b2012-12-11 09:33:422215 } else {
[email protected]9f3c8532013-07-31 19:52:072216 AddNewOrUpdatedExtension(extension,
2217 initial_state,
2218 blacklist_state,
2219 page_ordinal);
[email protected]399583b2012-12-11 09:33:422220 }
[email protected]8c484b742012-11-29 06:05:362221}
2222
2223void ExtensionService::AddNewOrUpdatedExtension(
2224 const Extension* extension,
[email protected]399583b2012-12-11 09:33:422225 Extension::State initial_state,
[email protected]8e289f0b2013-12-17 17:49:072226 extensions::BlacklistState blacklist_state,
[email protected]399583b2012-12-11 09:33:422227 const syncer::StringOrdinal& page_ordinal) {
[email protected]8c484b742012-11-29 06:05:362228 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]0f9e6262013-11-05 23:49:192229 const bool blacklisted_for_malware =
[email protected]8e289f0b2013-12-17 17:49:072230 blacklist_state == extensions::BLACKLISTED_MALWARE;
[email protected]9f3c8532013-07-31 19:52:072231 extension_prefs_->OnExtensionInstalled(extension,
2232 initial_state,
[email protected]0f9e6262013-11-05 23:49:192233 blacklisted_for_malware,
[email protected]9f3c8532013-07-31 19:52:072234 page_ordinal);
[email protected]9e9c1d12013-07-31 01:58:122235 delayed_installs_.Remove(extension->id());
[email protected]88622932014-01-16 17:59:122236 if (InstallVerifier::NeedsVerification(*extension)) {
[email protected]d820bc52014-03-13 05:35:232237 system_->install_verifier()->Add(extension->id(),
2238 base::Bind(LogAddVerifiedSuccess));
[email protected]ffd2f79e2013-11-14 00:11:462239 }
[email protected]6f6101832012-11-27 22:10:482240 FinishInstallation(extension);
2241}
2242
2243void ExtensionService::MaybeFinishDelayedInstallation(
2244 const std::string& extension_id) {
[email protected]9f4e4f082013-06-21 07:11:192245 // Check if the extension already got installed.
2246 if (!delayed_installs_.Contains(extension_id))
[email protected]6f6101832012-11-27 22:10:482247 return;
[email protected]9f4e4f082013-06-21 07:11:192248 extensions::ExtensionPrefs::DelayReason reason =
2249 extension_prefs_->GetDelayedInstallReason(extension_id);
2250
2251 // Check if the extension is idle. DELAY_REASON_NONE is used for older
2252 // preferences files that will not have set this field but it was previously
2253 // only used for idle updates.
2254 if ((reason == extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE ||
2255 reason == extensions::ExtensionPrefs::DELAY_REASON_NONE) &&
[email protected]1d5cf4142014-01-24 18:25:222256 is_ready() && !extensions::util::IsExtensionIdle(extension_id, profile_))
[email protected]6f6101832012-11-27 22:10:482257 return;
2258
[email protected]9f4e4f082013-06-21 07:11:192259 const Extension* extension = delayed_installs_.GetByID(extension_id);
2260 if (reason == extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IMPORTS) {
2261 ImportStatus status = SatisfyImports(extension);
2262 if (status != IMPORT_STATUS_OK) {
2263 if (status == IMPORT_STATUS_UNRECOVERABLE) {
2264 delayed_installs_.Remove(extension_id);
2265 // Make sure no version of the extension is actually installed, (i.e.,
2266 // that this delayed install was not an update).
2267 CHECK(!extension_prefs_->GetInstalledExtensionInfo(extension_id).get());
2268 extension_prefs_->DeleteExtensionPrefs(extension_id);
2269 }
2270 return;
2271 }
2272 }
2273
[email protected]6f6101832012-11-27 22:10:482274 FinishDelayedInstallation(extension_id);
2275}
2276
2277void ExtensionService::FinishDelayedInstallation(
2278 const std::string& extension_id) {
2279 scoped_refptr<const Extension> extension(
2280 GetPendingExtensionUpdate(extension_id));
[email protected]dc24976f2013-06-02 21:15:092281 CHECK(extension.get());
[email protected]9f4e4f082013-06-21 07:11:192282 delayed_installs_.Remove(extension_id);
[email protected]6f6101832012-11-27 22:10:482283
[email protected]399583b2012-12-11 09:33:422284 if (!extension_prefs_->FinishDelayedInstallInfo(extension_id))
[email protected]6f6101832012-11-27 22:10:482285 NOTREACHED();
2286
[email protected]dc24976f2013-06-02 21:15:092287 FinishInstallation(extension.get());
[email protected]6f6101832012-11-27 22:10:482288}
2289
2290void ExtensionService::FinishInstallation(const Extension* extension) {
[email protected]e7554c3f2013-05-29 00:36:562291 const extensions::Extension* existing_extension =
2292 GetInstalledExtension(extension->id());
2293 bool is_update = false;
2294 std::string old_name;
2295 if (existing_extension) {
2296 is_update = true;
2297 old_name = existing_extension->name();
2298 }
2299 extensions::InstalledExtensionInfo details(extension, is_update, old_name);
[email protected]fcb58a862012-05-01 01:03:152300 content::NotificationService::current()->Notify(
2301 chrome::NOTIFICATION_EXTENSION_INSTALLED,
2302 content::Source<Profile>(profile_),
[email protected]41bb80bd2013-05-03 10:56:022303 content::Details<const extensions::InstalledExtensionInfo>(&details));
[email protected]fcb58a862012-05-01 01:03:152304
[email protected]d96eb512012-11-01 23:44:082305 bool unacknowledged_external = IsUnacknowledgedExternalExtension(extension);
[email protected]41070e8d2012-10-24 01:34:362306
[email protected]399583b2012-12-11 09:33:422307 // Unpacked extensions default to allowing file access, but if that has been
2308 // overridden, don't reset the value.
[email protected]1d5e58b2013-01-31 08:41:402309 if (Manifest::ShouldAlwaysAllowFileAccess(extension->location()) &&
[email protected]399583b2012-12-11 09:33:422310 !extension_prefs_->HasAllowFileAccessSetting(extension->id())) {
2311 extension_prefs_->SetAllowFileAccess(extension->id(), true);
2312 }
2313
[email protected]6f6101832012-11-27 22:10:482314 AddExtension(extension);
[email protected]612a1cb12012-10-17 13:18:032315
2316 // If this is a new external extension that was disabled, alert the user
[email protected]d96eb512012-11-01 23:44:082317 // so he can reenable it. We do this last so that it has already been
2318 // added to our list of extensions.
[email protected]3e3fafbb2013-08-17 13:33:162319 if (unacknowledged_external && !is_update) {
[email protected]612a1cb12012-10-17 13:18:032320 UpdateExternalExtensionAlert();
[email protected]d96eb512012-11-01 23:44:082321 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
2322 EXTERNAL_EXTENSION_INSTALLED,
2323 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:022324 if (extensions::ManifestURL::UpdatesFromGallery(extension)) {
2325 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventWebstore",
2326 EXTERNAL_EXTENSION_INSTALLED,
2327 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
2328 } else {
2329 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventNonWebstore",
2330 EXTERNAL_EXTENSION_INSTALLED,
2331 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
2332 }
[email protected]d96eb512012-11-01 23:44:082333 }
[email protected]9f4e4f082013-06-21 07:11:192334
2335 // Check extensions that may have been delayed only because this shared module
2336 // was not available.
2337 if (SharedModuleInfo::IsSharedModule(extension)) {
2338 MaybeFinishDelayedInstallations();
2339 }
[email protected]4a190632009-05-09 01:07:422340}
2341
[email protected]76b65442012-11-17 14:11:482342const Extension* ExtensionService::GetPendingExtensionUpdate(
2343 const std::string& id) const {
[email protected]9f4e4f082013-06-21 07:11:192344 return delayed_installs_.GetByID(id);
[email protected]76b65442012-11-17 14:11:482345}
2346
[email protected]bb7f40952011-01-13 00:21:202347void ExtensionService::TrackTerminatedExtension(const Extension* extension) {
[email protected]bb1bc9b32013-12-21 03:09:142348 // No need to check for duplicates; inserting a duplicate is a no-op.
2349 registry_->AddTerminated(make_scoped_refptr(extension));
[email protected]3964e572014-02-13 21:57:412350 extensions_being_terminated_.erase(extension->id());
[email protected]b0af4792013-10-23 09:12:132351 UnloadExtension(extension->id(), UnloadedExtensionInfo::REASON_TERMINATE);
[email protected]bb7f40952011-01-13 00:21:202352}
2353
2354void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
[email protected]fa2416f2011-05-03 08:41:202355 std::string lowercase_id = StringToLowerASCII(id);
[email protected]bb1bc9b32013-12-21 03:09:142356 const Extension* extension =
2357 registry_->terminated_extensions().GetByID(lowercase_id);
2358 registry_->RemoveTerminated(lowercase_id);
[email protected]fb789532013-08-27 02:40:212359 if (extension) {
2360 content::NotificationService::current()->Notify(
2361 chrome::NOTIFICATION_EXTENSION_REMOVED,
2362 content::Source<Profile>(profile_),
2363 content::Details<const Extension>(extension));
2364 }
[email protected]bb7f40952011-01-13 00:21:202365}
2366
[email protected]0dfe05c2011-02-23 23:03:362367const Extension* ExtensionService::GetTerminatedExtension(
[email protected]8001df22011-04-28 19:59:472368 const std::string& id) const {
[email protected]599539802014-01-07 23:06:002369 return registry_->GetExtensionById(id, ExtensionRegistry::TERMINATED);
[email protected]8001df22011-04-28 19:59:472370}
2371
2372const Extension* ExtensionService::GetInstalledExtension(
2373 const std::string& id) const {
[email protected]599539802014-01-07 23:06:002374 return registry_->GetExtensionById(id, ExtensionRegistry::EVERYTHING);
[email protected]0dfe05c2011-02-23 23:03:362375}
2376
[email protected]eaa7dd182010-12-14 11:09:002377bool ExtensionService::ExtensionBindingsAllowed(const GURL& url) {
[email protected]615d88f2011-12-13 01:47:442378 // Allow bindings for all packaged extensions and component hosted apps.
[email protected]bb1bc9b32013-12-21 03:09:142379 const Extension* extension =
2380 registry_->enabled_extensions().GetExtensionOrAppByURL(url);
[email protected]615d88f2011-12-13 01:47:442381 return extension && (!extension->is_hosted_app() ||
[email protected]1d5e58b2013-01-31 08:41:402382 extension->location() == Manifest::COMPONENT);
[email protected]6d2e60bd2010-06-03 22:37:392383}
2384
[email protected]9060d8b02012-01-13 02:14:302385bool ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:322386 const std::string& id,
[email protected]c5e4a2222014-01-03 16:06:132387 const Version* version,
[email protected]650b2d52013-02-10 03:41:452388 const base::FilePath& path,
[email protected]1d5e58b2013-01-31 08:41:402389 Manifest::Location location,
[email protected]47fc70c2011-12-06 07:29:512390 int creation_flags,
2391 bool mark_acknowledged) {
[email protected]ab22ba42011-01-14 16:36:382392 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2393 CHECK(Extension::IdIsValid(id));
[email protected]79c833b52011-04-05 18:31:012394 if (extension_prefs_->IsExternalExtensionUninstalled(id))
[email protected]9060d8b02012-01-13 02:14:302395 return false;
[email protected]a8af9fdb2010-10-28 21:52:202396
[email protected]7577a5c52009-07-30 06:21:582397 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:492398 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:582399 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:022400 const Extension* existing = GetExtensionById(id, true);
[email protected]910f72ce2012-08-24 01:38:352401
[email protected]7577a5c52009-07-30 06:21:582402 if (existing) {
[email protected]910f72ce2012-08-24 01:38:352403 // The default apps will have the location set as INTERNAL. Since older
2404 // default apps are installed as EXTERNAL, we override them. However, if the
2405 // app is already installed as internal, then do the version check.
[email protected]31bb5ee62012-09-12 22:58:402406 // TODO(grv) : Remove after Q1-2013.
[email protected]910f72ce2012-08-24 01:38:352407 bool is_default_apps_migration =
[email protected]1d5e58b2013-01-31 08:41:402408 (location == Manifest::INTERNAL &&
2409 Manifest::IsExternalLocation(existing->location()));
[email protected]910f72ce2012-08-24 01:38:352410
2411 if (!is_default_apps_migration) {
2412 DCHECK(version);
2413
2414 switch (existing->version()->CompareTo(*version)) {
2415 case -1: // existing version is older, we should upgrade
2416 break;
2417 case 0: // existing version is same, do nothing
2418 return false;
2419 case 1: // existing version is newer, uh-oh
2420 LOG(WARNING) << "Found external version of extension " << id
2421 << "that is older than current version. Current version "
2422 << "is: " << existing->VersionString() << ". New "
2423 << "version is: " << version->GetString()
2424 << ". Keeping current version.";
2425 return false;
2426 }
[email protected]7577a5c52009-07-30 06:21:582427 }
2428 }
2429
[email protected]9060d8b02012-01-13 02:14:302430 // If the extension is already pending, don't start an install.
[email protected]e3987852012-05-04 10:06:302431 if (!pending_extension_manager()->AddFromExternalFile(
[email protected]464213a2013-10-15 01:06:482432 id, location, *version, creation_flags, mark_acknowledged)) {
[email protected]9060d8b02012-01-13 02:14:302433 return false;
[email protected]e3987852012-05-04 10:06:302434 }
[email protected]9c635f22010-12-02 09:36:362435
[email protected]14908b72011-04-20 06:54:362436 // no client (silent install)
[email protected]f8636f92013-08-09 21:02:372437 scoped_refptr<CrxInstaller> installer(CrxInstaller::CreateSilent(this));
[email protected]6dfbbf82010-03-12 23:09:162438 installer->set_install_source(location);
2439 installer->set_expected_id(id);
[email protected]cb0e50312011-05-09 15:03:072440 installer->set_expected_version(*version);
2441 installer->set_install_cause(extension_misc::INSTALL_CAUSE_EXTERNAL_FILE);
[email protected]1bf73cc32011-10-26 22:38:312442 installer->set_creation_flags(creation_flags);
[email protected]88e8ec9152013-01-17 04:05:182443#if defined(OS_CHROMEOS)
2444 extensions::InstallLimiter::Get(profile_)->Add(installer, path);
2445#else
[email protected]6dfbbf82010-03-12 23:09:162446 installer->InstallCrx(path);
[email protected]88e8ec9152013-01-17 04:05:182447#endif
[email protected]47fc70c2011-12-06 07:29:512448
2449 // Depending on the source, a new external extension might not need a user
2450 // notification on installation. For such extensions, mark them acknowledged
2451 // now to suppress the notification.
2452 if (mark_acknowledged)
2453 AcknowledgeExternalExtension(id);
[email protected]9060d8b02012-01-13 02:14:302454
2455 return true;
[email protected]7577a5c52009-07-30 06:21:582456}
2457
[email protected]eaa7dd182010-12-14 11:09:002458void ExtensionService::ReportExtensionLoadError(
[email protected]650b2d52013-02-10 03:41:452459 const base::FilePath& extension_path,
[email protected]d11c8e92009-10-20 23:26:402460 const std::string &error,
[email protected]d11c8e92009-10-20 23:26:402461 bool be_noisy) {
[email protected]d8c8f25f2011-11-02 18:18:012462 content::NotificationService::current()->Notify(
2463 chrome::NOTIFICATION_EXTENSION_LOAD_ERROR,
2464 content::Source<Profile>(profile_),
2465 content::Details<const std::string>(&error));
[email protected]d11c8e92009-10-20 23:26:402466
[email protected]04338722013-12-24 23:18:052467 std::string path_str = base::UTF16ToUTF8(extension_path.LossyDisplayName());
2468 base::string16 message = base::UTF8ToUTF16(base::StringPrintf(
[email protected]18d4b6c2010-09-21 03:21:042469 "Could not load extension from '%s'. %s",
[email protected]fc670822011-12-17 09:33:492470 path_str.c_str(), error.c_str()));
[email protected]d11c8e92009-10-20 23:26:402471 ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy);
2472}
2473
[email protected]eaa7dd182010-12-14 11:09:002474void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]3a1dc572012-07-31 22:25:132475 extensions::ExtensionHost* host) {
[email protected]406027c02010-09-27 08:03:182476 OrphanedDevTools::iterator iter =
[email protected]2d2f6cfc2011-05-06 21:09:332477 orphaned_dev_tools_.find(host->extension_id());
[email protected]406027c02010-09-27 08:03:182478 if (iter == orphaned_dev_tools_.end())
2479 return;
2480
[email protected]04ea1bb2013-07-10 09:26:092481 iter->second->ConnectRenderViewHost(host->render_view_host());
[email protected]406027c02010-09-27 08:03:182482 orphaned_dev_tools_.erase(iter);
2483}
2484
[email protected]432115822011-07-10 15:52:272485void ExtensionService::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:532486 const content::NotificationSource& source,
2487 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:272488 switch (type) {
[email protected]3c4abc82012-10-22 22:25:542489 case chrome::NOTIFICATION_APP_TERMINATING:
2490 // Shutdown has started. Don't start any more extension installs.
2491 // (We cannot use ExtensionService::Shutdown() for this because it
2492 // happens too late in browser teardown.)
2493 browser_terminating_ = true;
2494 break;
[email protected]432115822011-07-10 15:52:272495 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]6c2381d2011-10-19 02:52:532496 if (profile_ !=
2497 content::Source<Profile>(source).ptr()->GetOriginalProfile()) {
[email protected]bc535ee52010-08-31 18:40:322498 break;
[email protected]6c2381d2011-10-19 02:52:532499 }
[email protected]a4ed6282009-12-14 20:51:162500
[email protected]3a1dc572012-07-31 22:25:132501 extensions::ExtensionHost* host =
2502 content::Details<extensions::ExtensionHost>(details).ptr();
[email protected]f128af42010-08-05 18:05:262503
[email protected]3964e572014-02-13 21:57:412504 // If the extension is already being terminated, there is nothing left to
2505 // do.
2506 if (!extensions_being_terminated_.insert(host->extension_id()).second)
2507 break;
2508
[email protected]fa2416f2011-05-03 08:41:202509 // Mark the extension as terminated and Unload it. We want it to
2510 // be in a consistent state: either fully working or not loaded
2511 // at all, but never half-crashed. We do it in a PostTask so
2512 // that other handlers of this notification will still have
2513 // access to the Extension and ExtensionHost.
[email protected]b3a25092013-05-28 22:08:162514 base::MessageLoop::current()->PostTask(
[email protected]14908b72011-04-20 06:54:362515 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362516 base::Bind(
[email protected]fa2416f2011-05-03 08:41:202517 &ExtensionService::TrackTerminatedExtension,
[email protected]d8c8f25f2011-11-02 18:18:012518 AsWeakPtr(),
[email protected]fa2416f2011-05-03 08:41:202519 host->extension()));
[email protected]31f77262009-12-02 20:48:532520 break;
2521 }
[email protected]432115822011-07-10 15:52:272522 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
[email protected]f3b1a082011-11-18 00:34:302523 content::RenderProcessHost* process =
2524 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192525 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302526 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192527 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2528 break;
2529
[email protected]fafdc842014-01-17 18:09:082530 extensions::ProcessMap* process_map =
2531 extensions::ProcessMap::Get(profile_);
2532 if (process_map->Contains(process->GetID())) {
[email protected]52b76592013-11-02 17:59:032533 // An extension process was terminated, this might have resulted in an
2534 // app or extension becoming idle.
2535 std::set<std::string> extension_ids =
[email protected]fafdc842014-01-17 18:09:082536 process_map->GetExtensionsInProcess(process->GetID());
[email protected]52b76592013-11-02 17:59:032537 for (std::set<std::string>::const_iterator it = extension_ids.begin();
2538 it != extension_ids.end(); ++it) {
2539 if (delayed_installs_.Contains(*it)) {
2540 base::MessageLoop::current()->PostDelayedTask(
2541 FROM_HERE,
2542 base::Bind(&ExtensionService::MaybeFinishDelayedInstallation,
2543 AsWeakPtr(), *it),
2544 base::TimeDelta::FromSeconds(kUpdateIdleDelay));
2545 }
2546 }
2547 }
2548
[email protected]fafdc842014-01-17 18:09:082549 process_map->RemoveAllFromProcess(process->GetID());
[email protected]6f371442011-11-09 06:45:462550 BrowserThread::PostTask(
[email protected]38427a12013-11-09 17:34:202551 BrowserThread::IO,
2552 FROM_HERE,
2553 base::Bind(&extensions::InfoMap::UnregisterAllExtensionsInProcess,
[email protected]31d8f5f22012-04-02 15:22:082554 system_->info_map(),
[email protected]f3b1a082011-11-18 00:34:302555 process->GetID()));
[email protected]da5683db2011-04-23 17:12:212556 break;
2557 }
[email protected]92dd8d92013-02-26 00:41:082558 case chrome::NOTIFICATION_UPGRADE_RECOMMENDED: {
[email protected]75bdcb872013-03-13 00:41:452559 // Notify observers that chrome update is available.
2560 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
2561 OnChromeUpdateAvailable());
[email protected]92dd8d92013-02-26 00:41:082562 break;
2563 }
[email protected]aa96d3a2010-08-21 08:45:252564
[email protected]4814b512009-11-07 00:12:292565 default:
2566 NOTREACHED() << "Unexpected notification type.";
2567 }
2568}
2569
[email protected]90bb38d2012-11-14 18:36:032570void ExtensionService::OnExtensionInstallPrefChanged() {
[email protected]a6a7ced2012-11-01 17:24:182571 IdentifyAlertableExtensions();
2572 CheckManagementPolicy();
2573}
2574
[email protected]eaa7dd182010-12-14 11:09:002575bool ExtensionService::HasApps() const {
[email protected]ec5b50d2010-10-09 16:35:182576 return !GetAppIds().empty();
2577}
[email protected]377011d2010-07-20 04:18:502578
[email protected]eaa7dd182010-12-14 11:09:002579ExtensionIdSet ExtensionService::GetAppIds() const {
[email protected]ec5b50d2010-10-09 16:35:182580 ExtensionIdSet result;
[email protected]bb1bc9b32013-12-21 03:09:142581 const ExtensionSet& extensions = registry_->enabled_extensions();
2582 for (ExtensionSet::const_iterator it = extensions.begin();
2583 it != extensions.end(); ++it) {
[email protected]1d5e58b2013-01-31 08:41:402584 if ((*it)->is_app() && (*it)->location() != Manifest::COMPONENT)
[email protected]ec5b50d2010-10-09 16:35:182585 result.insert((*it)->id());
[email protected]377011d2010-07-20 04:18:502586 }
2587
[email protected]ec5b50d2010-10-09 16:35:182588 return result;
[email protected]377011d2010-07-20 04:18:502589}
[email protected]d7e9a862010-11-03 21:57:492590
[email protected]e178ad92013-06-28 02:29:252591bool ExtensionService::IsBeingReloaded(
2592 const std::string& extension_id) const {
2593 return ContainsKey(extensions_being_reloaded_, extension_id);
2594}
2595
2596void ExtensionService::SetBeingReloaded(const std::string& extension_id,
[email protected]5e7015c2013-07-23 23:29:432597 bool isBeingReloaded) {
2598 if (isBeingReloaded)
[email protected]e178ad92013-06-28 02:29:252599 extensions_being_reloaded_.insert(extension_id);
[email protected]5e7015c2013-07-23 23:29:432600 else
[email protected]e178ad92013-06-28 02:29:252601 extensions_being_reloaded_.erase(extension_id);
[email protected]e178ad92013-06-28 02:29:252602}
2603
[email protected]612a1cb12012-10-17 13:18:032604bool ExtensionService::ShouldEnableOnInstall(const Extension* extension) {
2605 // Extensions installed by policy can't be disabled. So even if a previous
2606 // installation disabled the extension, make sure it is now enabled.
2607 if (system_->management_policy()->MustRemainEnabled(extension, NULL))
2608 return true;
2609
2610 if (extension_prefs_->IsExtensionDisabled(extension->id()))
2611 return false;
2612
[email protected]41070e8d2012-10-24 01:34:362613 if (FeatureSwitch::prompt_for_external_extensions()->IsEnabled()) {
[email protected]612a1cb12012-10-17 13:18:032614 // External extensions are initially disabled. We prompt the user before
[email protected]2c495c42013-01-04 21:49:542615 // enabling them. Hosted apps are excepted because they are not dangerous
2616 // (they need to be launched by the user anyway).
[email protected]1d5e58b2013-01-31 08:41:402617 if (extension->GetType() != Manifest::TYPE_HOSTED_APP &&
2618 Manifest::IsExternalLocation(extension->location()) &&
[email protected]612a1cb12012-10-17 13:18:032619 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id())) {
2620 return false;
2621 }
2622 }
[email protected]612a1cb12012-10-17 13:18:032623
2624 return true;
2625}
[email protected]0db124b02012-11-07 04:55:052626
[email protected]e7aa7b7e2012-11-27 04:51:222627bool ExtensionService::ShouldDelayExtensionUpdate(
2628 const std::string& extension_id,
2629 bool wait_for_idle) const {
2630 const char kOnUpdateAvailableEvent[] = "runtime.onUpdateAvailable";
2631
2632 // If delayed updates are globally disabled, or just for this extension,
2633 // don't delay.
2634 if (!install_updates_when_idle_ || !wait_for_idle)
2635 return false;
2636
[email protected]695b5712012-12-06 23:55:282637 const Extension* old = GetInstalledExtension(extension_id);
[email protected]e7aa7b7e2012-11-27 04:51:222638 // If there is no old extension, this is not an update, so don't delay.
2639 if (!old)
2640 return false;
2641
[email protected]9367eabc2013-03-01 01:29:292642 if (extensions::BackgroundInfo::HasPersistentBackgroundPage(old)) {
[email protected]e7aa7b7e2012-11-27 04:51:222643 // Delay installation if the extension listens for the onUpdateAvailable
2644 // event.
2645 return system_->event_router()->ExtensionHasEventListener(
2646 extension_id, kOnUpdateAvailableEvent);
2647 } else {
2648 // Delay installation if the extension is not idle.
[email protected]1d5cf4142014-01-24 18:25:222649 return !extensions::util::IsExtensionIdle(extension_id, profile_);
[email protected]e7aa7b7e2012-11-27 04:51:222650 }
[email protected]0db124b02012-11-07 04:55:052651}
[email protected]fdd679b2012-11-15 20:49:392652
[email protected]399583b2012-12-11 09:33:422653void ExtensionService::GarbageCollectIsolatedStorage() {
[email protected]650b2d52013-02-10 03:41:452654 scoped_ptr<base::hash_set<base::FilePath> > active_paths(
2655 new base::hash_set<base::FilePath>());
[email protected]bb1bc9b32013-12-21 03:09:142656 const ExtensionSet& extensions = registry_->enabled_extensions();
2657 for (ExtensionSet::const_iterator it = extensions.begin();
2658 it != extensions.end(); ++it) {
[email protected]cadac622013-06-11 16:46:362659 if (extensions::AppIsolationInfo::HasIsolatedStorage(it->get())) {
2660 active_paths->insert(BrowserContext::GetStoragePartitionForSite(
[email protected]3a746ec2014-03-15 05:30:562661 profile_,
2662 extensions::util::GetSiteForExtensionId(
2663 (*it)->id(), profile()))->GetPath());
[email protected]399583b2012-12-11 09:33:422664 }
2665 }
2666
[email protected]9f4e4f082013-06-21 07:11:192667 DCHECK(!installs_delayed_for_gc());
2668 set_installs_delayed_for_gc(true);
[email protected]399583b2012-12-11 09:33:422669 BrowserContext::GarbageCollectStoragePartitions(
2670 profile_, active_paths.Pass(),
2671 base::Bind(&ExtensionService::OnGarbageCollectIsolatedStorageFinished,
2672 AsWeakPtr()));
2673}
2674
2675void ExtensionService::OnGarbageCollectIsolatedStorageFinished() {
[email protected]9f4e4f082013-06-21 07:11:192676 set_installs_delayed_for_gc(false);
2677 MaybeFinishDelayedInstallations();
2678}
2679
2680void ExtensionService::MaybeFinishDelayedInstallations() {
2681 std::vector<std::string> to_be_installed;
[email protected]399583b2012-12-11 09:33:422682 for (ExtensionSet::const_iterator it = delayed_installs_.begin();
2683 it != delayed_installs_.end();
2684 ++it) {
[email protected]9f4e4f082013-06-21 07:11:192685 to_be_installed.push_back((*it)->id());
[email protected]399583b2012-12-11 09:33:422686 }
[email protected]9f4e4f082013-06-21 07:11:192687 for (std::vector<std::string>::const_iterator it = to_be_installed.begin();
2688 it != to_be_installed.end();
[email protected]399583b2012-12-11 09:33:422689 ++it) {
[email protected]9f4e4f082013-06-21 07:11:192690 MaybeFinishDelayedInstallation(*it);
[email protected]399583b2012-12-11 09:33:422691 }
[email protected]399583b2012-12-11 09:33:422692}
2693
[email protected]fdd679b2012-11-15 20:49:392694void ExtensionService::OnBlacklistUpdated() {
[email protected]2d19eb6e2014-01-27 17:30:002695 blacklist_->GetBlacklistedIDs(
[email protected]f47f7172014-03-19 19:27:102696 registry_->GenerateInstalledExtensionsSet()->GetIDs(),
[email protected]3f2a2fa2013-09-24 02:55:252697 base::Bind(&ExtensionService::ManageBlacklist, AsWeakPtr()));
[email protected]695b5712012-12-06 23:55:282698}
2699
[email protected]2d19eb6e2014-01-27 17:30:002700void ExtensionService::ManageBlacklist(
2701 const extensions::Blacklist::BlacklistStateMap& state_map) {
[email protected]695b5712012-12-06 23:55:282702 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2703
[email protected]2d19eb6e2014-01-27 17:30:002704 std::set<std::string> blocked;
2705 ExtensionIdSet greylist;
2706 ExtensionIdSet unchanged;
2707 for (extensions::Blacklist::BlacklistStateMap::const_iterator it =
2708 state_map.begin();
2709 it != state_map.end();
2710 ++it) {
2711 switch (it->second) {
2712 case extensions::NOT_BLACKLISTED:
2713 break;
[email protected]695b5712012-12-06 23:55:282714
[email protected]2d19eb6e2014-01-27 17:30:002715 case extensions::BLACKLISTED_MALWARE:
2716 blocked.insert(it->first);
2717 break;
2718
2719 case extensions::BLACKLISTED_SECURITY_VULNERABILITY:
2720 case extensions::BLACKLISTED_CWS_POLICY_VIOLATION:
2721 case extensions::BLACKLISTED_POTENTIALLY_UNWANTED:
2722 greylist.insert(it->first);
2723 break;
2724
2725 case extensions::BLACKLISTED_UNKNOWN:
2726 unchanged.insert(it->first);
2727 break;
2728 }
2729 }
2730
2731 UpdateBlockedExtensions(blocked, unchanged);
2732 UpdateGreylistedExtensions(greylist, unchanged, state_map);
2733
2734 IdentifyAlertableExtensions();
2735}
2736
2737namespace {
2738void Partition(const ExtensionIdSet& before,
2739 const ExtensionIdSet& after,
2740 const ExtensionIdSet& unchanged,
2741 ExtensionIdSet* no_longer,
2742 ExtensionIdSet* not_yet) {
2743 *not_yet = base::STLSetDifference<ExtensionIdSet>(after, before);
2744 *no_longer = base::STLSetDifference<ExtensionIdSet>(before, after);
2745 *no_longer = base::STLSetDifference<ExtensionIdSet>(*no_longer, unchanged);
2746}
2747} // namespace
2748
2749void ExtensionService::UpdateBlockedExtensions(
2750 const ExtensionIdSet& blocked,
2751 const ExtensionIdSet& unchanged) {
2752 ExtensionIdSet not_yet_blocked, no_longer_blocked;
2753 Partition(registry_->blacklisted_extensions().GetIDs(),
2754 blocked, unchanged,
2755 &no_longer_blocked, &not_yet_blocked);
2756
2757 for (ExtensionIdSet::iterator it = no_longer_blocked.begin();
2758 it != no_longer_blocked.end(); ++it) {
[email protected]695b5712012-12-06 23:55:282759 scoped_refptr<const Extension> extension =
[email protected]bb1bc9b32013-12-21 03:09:142760 registry_->blacklisted_extensions().GetByID(*it);
[email protected]3f2a2fa2013-09-24 02:55:252761 if (!extension.get()) {
[email protected]2d19eb6e2014-01-27 17:30:002762 NOTREACHED() << "Extension " << *it << " no longer blocked, "
2763 << "but it was never blocked.";
[email protected]695b5712012-12-06 23:55:282764 continue;
[email protected]3f2a2fa2013-09-24 02:55:252765 }
[email protected]bb1bc9b32013-12-21 03:09:142766 registry_->RemoveBlacklisted(*it);
[email protected]3f2a2fa2013-09-24 02:55:252767 extension_prefs_->SetExtensionBlacklisted(extension->id(), false);
[email protected]dc24976f2013-06-02 21:15:092768 AddExtension(extension.get());
[email protected]ac875372013-02-28 04:36:092769 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.UnblacklistInstalled",
[email protected]dc24976f2013-06-02 21:15:092770 extension->location(),
2771 Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:282772 }
2773
[email protected]2d19eb6e2014-01-27 17:30:002774 for (ExtensionIdSet::iterator it = not_yet_blocked.begin();
2775 it != not_yet_blocked.end(); ++it) {
[email protected]695b5712012-12-06 23:55:282776 scoped_refptr<const Extension> extension = GetInstalledExtension(*it);
[email protected]3f2a2fa2013-09-24 02:55:252777 if (!extension.get()) {
2778 NOTREACHED() << "Extension " << *it << " needs to be "
2779 << "blacklisted, but it's not installed.";
[email protected]695b5712012-12-06 23:55:282780 continue;
[email protected]3f2a2fa2013-09-24 02:55:252781 }
[email protected]bb1bc9b32013-12-21 03:09:142782 registry_->AddBlacklisted(extension);
[email protected]2d19eb6e2014-01-27 17:30:002783 extension_prefs_->SetExtensionBlacklistState(
2784 extension->id(), extensions::BLACKLISTED_MALWARE);
[email protected]b0af4792013-10-23 09:12:132785 UnloadExtension(*it, UnloadedExtensionInfo::REASON_BLACKLIST);
[email protected]ac875372013-02-28 04:36:092786 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.BlacklistInstalled",
2787 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:282788 }
[email protected]2d19eb6e2014-01-27 17:30:002789}
[email protected]695b5712012-12-06 23:55:282790
[email protected]2d19eb6e2014-01-27 17:30:002791// TODO(oleg): UMA logging
2792void ExtensionService::UpdateGreylistedExtensions(
2793 const ExtensionIdSet& greylist,
2794 const ExtensionIdSet& unchanged,
2795 const extensions::Blacklist::BlacklistStateMap& state_map) {
2796 ExtensionIdSet not_yet_greylisted, no_longer_greylisted;
2797 Partition(greylist_.GetIDs(),
2798 greylist, unchanged,
2799 &no_longer_greylisted, &not_yet_greylisted);
2800
2801 for (ExtensionIdSet::iterator it = no_longer_greylisted.begin();
2802 it != no_longer_greylisted.end(); ++it) {
2803 scoped_refptr<const Extension> extension = greylist_.GetByID(*it);
2804 if (!extension.get()) {
2805 NOTREACHED() << "Extension " << *it << " no longer greylisted, "
2806 << "but it was not marked as greylisted.";
2807 continue;
2808 }
2809
2810 greylist_.Remove(*it);
2811 extension_prefs_->SetExtensionBlacklistState(extension->id(),
2812 extensions::NOT_BLACKLISTED);
2813 if (extension_prefs_->GetDisableReasons(extension->id()) &
2814 extensions::Extension::DISABLE_GREYLIST)
2815 EnableExtension(*it);
2816 }
2817
2818 for (ExtensionIdSet::iterator it = not_yet_greylisted.begin();
2819 it != not_yet_greylisted.end(); ++it) {
2820 scoped_refptr<const Extension> extension = GetInstalledExtension(*it);
2821 if (!extension.get()) {
2822 NOTREACHED() << "Extension " << *it << " needs to be "
2823 << "disabled, but it's not installed.";
2824 continue;
2825 }
2826 greylist_.Insert(extension);
2827 extension_prefs_->SetExtensionBlacklistState(extension->id(),
2828 state_map.find(*it)->second);
2829 if (registry_->enabled_extensions().Contains(extension->id()))
2830 DisableExtension(*it, extensions::Extension::DISABLE_GREYLIST);
2831 }
[email protected]fdd679b2012-11-15 20:49:392832}
[email protected]75bdcb872013-03-13 00:41:452833
2834void ExtensionService::AddUpdateObserver(extensions::UpdateObserver* observer) {
2835 update_observers_.AddObserver(observer);
2836}
2837
2838void ExtensionService::RemoveUpdateObserver(
2839 extensions::UpdateObserver* observer) {
2840 update_observers_.RemoveObserver(observer);
2841}
[email protected]bb1bc9b32013-12-21 03:09:142842
2843// Used only by test code.
2844void ExtensionService::UnloadAllExtensionsInternal() {
2845 profile_->GetExtensionSpecialStoragePolicy()->RevokeRightsForAllExtensions();
2846
2847 registry_->ClearAll();
[email protected]45f5b7d2014-01-22 23:47:132848 system_->runtime_data()->ClearAll();
[email protected]bb1bc9b32013-12-21 03:09:142849
2850 // TODO(erikkay) should there be a notification for this? We can't use
2851 // EXTENSION_UNLOADED since that implies that the extension has been disabled
2852 // or uninstalled.
2853}