blob: dc68f7aaa01395436b9f7f5adc616fd631c5d8a3 [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]00b38242012-07-18 18:43:2237#include "chrome/browser/extensions/extension_install_ui.h"
[email protected]19eb80152011-02-26 00:28:4338#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]f8aefb132013-10-30 09:29:5239#include "chrome/browser/extensions/extension_sync_service.h"
[email protected]617342a42013-12-18 23:34:0340#include "chrome/browser/extensions/extension_util.h"
[email protected]612a1cb12012-10-17 13:18:0341#include "chrome/browser/extensions/external_install_ui.h"
[email protected]5df038b2012-07-16 19:03:2742#include "chrome/browser/extensions/external_provider_impl.h"
[email protected]ffd2f79e2013-11-14 00:11:4643#include "chrome/browser/extensions/install_verifier.h"
[email protected]d8c8f25f2011-11-02 18:18:0144#include "chrome/browser/extensions/installed_loader.h"
[email protected]f3d3b382014-03-14 21:19:2845#include "chrome/browser/extensions/pending_extension_manager.h"
[email protected]c333e792012-01-06 16:57:3946#include "chrome/browser/extensions/permissions_updater.h"
[email protected]d8c8f25f2011-11-02 18:18:0147#include "chrome/browser/extensions/unpacked_installer.h"
[email protected]044e86992014-01-24 22:59:1148#include "chrome/browser/extensions/updater/extension_cache.h"
[email protected]42a08162012-03-16 18:09:1149#include "chrome/browser/extensions/updater/extension_updater.h"
[email protected]8ecad5e2010-12-02 21:18:3350#include "chrome/browser/profiles/profile.h"
[email protected]b5a507b22013-11-08 20:41:5751#include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
[email protected]c8d407e2011-04-28 21:27:1752#include "chrome/browser/ui/webui/favicon_source.h"
[email protected]5eddc3e2011-10-26 04:33:3153#include "chrome/browser/ui/webui/ntp/thumbnail_source.h"
[email protected]b07e606e2012-09-15 20:16:1554#include "chrome/browser/ui/webui/theme_source.h"
[email protected]e2eb43112009-05-29 21:19:5455#include "chrome/common/chrome_switches.h"
[email protected]f6431be82013-09-07 02:53:4556#include "chrome/common/crash_keys.h"
[email protected]9f4e4f082013-06-21 07:11:1957#include "chrome/common/extensions/extension_constants.h"
[email protected]7c927b62010-02-24 09:54:1358#include "chrome/common/extensions/extension_file_util.h"
[email protected]15d4d2d2013-08-09 06:49:0359#include "chrome/common/extensions/features/feature_channel.h"
[email protected]561e33d52013-04-03 06:58:4360#include "chrome/common/extensions/manifest_handlers/app_isolation_info.h"
[email protected]65348062013-01-15 07:27:2261#include "chrome/common/extensions/manifest_url_handler.h"
[email protected]25b34332009-06-05 21:53:1962#include "chrome/common/pref_names.h"
[email protected]a57209872009-05-04 22:53:1463#include "chrome/common/url_constants.h"
[email protected]7a83dd072013-09-03 12:09:1764#include "components/startup_metric_utils/startup_metric_utils.h"
[email protected]c38831a12011-10-28 12:44:4965#include "content/public/browser/browser_thread.h"
[email protected]98f66112012-12-25 12:59:3666#include "content/public/browser/devtools_agent_host.h"
[email protected]05aad2da2011-10-28 10:12:3767#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1668#include "content/public/browser/notification_types.h"
[email protected]f3b1a082011-11-18 00:34:3069#include "content/public/browser/render_process_host.h"
[email protected]399583b2012-12-11 09:33:4270#include "content/public/browser/storage_partition.h"
[email protected]24ea7a12013-01-27 23:54:5371#include "content/public/browser/url_data_source.h"
[email protected]74474042013-11-21 12:03:5472#include "extensions/browser/app_sorting.h"
[email protected]34423532013-11-21 18:13:1073#include "extensions/browser/event_router.h"
[email protected]22401dc2014-03-21 01:38:5774#include "extensions/browser/extension_host.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]d8fd0fd2014-03-24 13:16:06268 const std::string& install_parameter,
[email protected]21a5a672010-11-04 10:47:42269 const GURL& update_url,
[email protected]464213a2013-10-15 01:06:48270 Manifest::Location location,
271 int creation_flags,
272 bool mark_acknowledged) {
[email protected]ab22ba42011-01-14 16:36:38273 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
274 CHECK(Extension::IdIsValid(id));
[email protected]a8af9fdb2010-10-28 21:52:20275
[email protected]044e86992014-01-24 22:59:11276 if (Manifest::IsExternalLocation(location)) {
277 // All extensions that are not user specific can be cached.
278 extensions::ExtensionCache::GetInstance()->AllowCaching(id);
279 }
280
[email protected]8a87a5332011-08-11 17:54:59281 const Extension* extension = GetExtensionById(id, true);
282 if (extension) {
283 // Already installed. Skip this install if the current location has
284 // higher priority than |location|.
[email protected]1d5e58b2013-01-31 08:41:40285 Manifest::Location current = extension->location();
286 if (current == Manifest::GetHigherPriorityLocation(current, location))
[email protected]9060d8b02012-01-13 02:14:30287 return false;
[email protected]8a87a5332011-08-11 17:54:59288 // Otherwise, overwrite the current installation.
[email protected]7a4c6852010-09-16 03:44:22289 }
[email protected]9060d8b02012-01-13 02:14:30290
291 // Add |id| to the set of pending extensions. If it can not be added,
292 // then there is already a pending record from a higher-priority install
293 // source. In this case, signal that this extension will not be
294 // installed by returning false.
295 if (!pending_extension_manager()->AddFromExternalUpdateUrl(
[email protected]d8fd0fd2014-03-24 13:16:06296 id,
297 install_parameter,
298 update_url,
299 location,
300 creation_flags,
301 mark_acknowledged)) {
[email protected]9060d8b02012-01-13 02:14:30302 return false;
[email protected]31bb5ee62012-09-12 22:58:40303 }
[email protected]9060d8b02012-01-13 02:14:30304
[email protected]94fde232012-04-27 10:22:30305 update_once_all_providers_are_ready_ = true;
[email protected]9060d8b02012-01-13 02:14:30306 return true;
[email protected]7a4c6852010-09-16 03:44:22307}
308
[email protected]400c4392013-12-13 14:41:58309const Extension* ExtensionService::GetInstalledExtensionByUrl(
310 const GURL& url) const {
[email protected]bb1bc9b32013-12-21 03:09:14311 return registry_->enabled_extensions().GetExtensionOrAppByURL(url);
[email protected]400c4392013-12-13 14:41:58312}
313
[email protected]dc9a74f72012-08-17 18:07:21314const Extension* ExtensionService::GetInstalledApp(const GURL& url) const {
[email protected]400c4392013-12-13 14:41:58315 const Extension* extension = GetInstalledExtensionByUrl(url);
[email protected]dc9a74f72012-08-17 18:07:21316 return (extension && extension->is_app()) ? extension : NULL;
[email protected]d9696672011-03-15 22:45:09317}
318
[email protected]dc9a74f72012-08-17 18:07:21319bool ExtensionService::IsInstalledApp(const GURL& url) const {
[email protected]d9696672011-03-15 22:45:09320 return !!GetInstalledApp(url);
[email protected]ffb204f22010-12-05 23:20:27321}
322
[email protected]6aeac8342010-10-01 20:21:18323// static
[email protected]d6ebc9792011-04-07 18:18:33324// This function is used to implement the command-line switch
[email protected]3bdba0d2011-08-23 07:17:30325// --uninstall-extension, and to uninstall an extension via sync. The LOG
326// statements within this function are used to inform the user if the uninstall
327// cannot be done.
[email protected]eaa7dd182010-12-14 11:09:00328bool ExtensionService::UninstallExtensionHelper(
329 ExtensionService* extensions_service,
[email protected]6aeac8342010-10-01 20:21:18330 const std::string& extension_id) {
[email protected]d6ebc9792011-04-07 18:18:33331 // We can't call UninstallExtension with an invalid extension ID.
[email protected]31bb5ee62012-09-12 22:58:40332 if (!extensions_service->GetInstalledExtension(extension_id)) {
[email protected]6aeac8342010-10-01 20:21:18333 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]95da88c42011-03-31 10:07:33334 << "id: " << extension_id;
335 return false;
[email protected]6aeac8342010-10-01 20:21:18336 }
337
[email protected]d6ebc9792011-04-07 18:18:33338 // The following call to UninstallExtension will not allow an uninstall of a
339 // policy-controlled extension.
[email protected]439f1e32013-12-09 20:09:09340 base::string16 error;
[email protected]d6ebc9792011-04-07 18:18:33341 if (!extensions_service->UninstallExtension(extension_id, false, &error)) {
342 LOG(WARNING) << "Cannot uninstall extension with id " << extension_id
343 << ": " << error;
344 return false;
345 }
[email protected]95da88c42011-03-31 10:07:33346
[email protected]6aeac8342010-10-01 20:21:18347 return true;
348}
349
[email protected]eaa7dd182010-12-14 11:09:00350ExtensionService::ExtensionService(Profile* profile,
[email protected]f0841cd2011-01-19 15:07:24351 const CommandLine* command_line,
[email protected]650b2d52013-02-10 03:41:45352 const base::FilePath& install_directory,
[email protected]45759612012-07-10 17:21:23353 extensions::ExtensionPrefs* extension_prefs,
[email protected]fdd679b2012-11-15 20:49:39354 extensions::Blacklist* blacklist,
[email protected]0436b102011-04-15 18:30:03355 bool autoupdate_enabled,
[email protected]4a10006a2013-05-17 23:18:35356 bool extensions_enabled,
357 extensions::OneShotEvent* ready)
[email protected]fdd679b2012-11-15 20:49:39358 : extensions::Blacklist::Observer(blacklist),
359 profile_(profile),
[email protected]bd306722012-07-11 20:43:59360 system_(extensions::ExtensionSystem::Get(profile)),
[email protected]73c47932010-12-06 18:13:43361 extension_prefs_(extension_prefs),
[email protected]695b5712012-12-06 23:55:28362 blacklist_(blacklist),
[email protected]f8aefb132013-10-30 09:29:52363 extension_sync_service_(NULL),
[email protected]5fdfa562013-12-27 17:43:59364 registry_(extensions::ExtensionRegistry::Get(profile)),
[email protected]46f3e102014-03-25 01:22:45365 pending_extension_manager_(*this, profile),
[email protected]a9b00ac2009-06-25 21:03:23366 install_directory_(install_directory),
[email protected]0436b102011-04-15 18:30:03367 extensions_enabled_(extensions_enabled),
[email protected]e81dba32009-06-19 20:19:13368 show_extensions_prompts_(true),
[email protected]fc332ae2012-11-14 20:17:33369 install_updates_when_idle_(true),
[email protected]4a10006a2013-05-17 23:18:35370 ready_(ready),
[email protected]dff1e042012-04-27 10:59:18371 update_once_all_providers_are_ready_(false),
[email protected]3c4abc82012-10-22 22:25:54372 browser_terminating_(false),
[email protected]9f4e4f082013-06-21 07:11:19373 installs_delayed_for_gc_(false),
[email protected]f8aefb132013-10-30 09:29:52374 is_first_run_(false) {
[email protected]c04fd3dd2013-09-05 08:20:10375#if defined(OS_CHROMEOS)
376 disable_garbage_collection_ = false;
377#endif
[email protected]a29a517a2011-01-21 21:11:12378 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20379
[email protected]36a784c2009-06-23 06:21:08380 // Figure out if extension installation should be enabled.
[email protected]367d9b172013-12-03 00:31:02381 if (extensions::ExtensionsBrowserClient::Get()->AreExtensionsDisabled(
382 *command_line, profile))
[email protected]6d60703b2009-08-29 01:29:23383 extensions_enabled_ = false;
[email protected]36a784c2009-06-23 06:21:08384
[email protected]3c4abc82012-10-22 22:25:54385 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING,
386 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27387 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07388 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27389 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07390 content::NotificationService::AllBrowserContextsAndSources());
[email protected]92dd8d92013-02-26 00:41:08391 registrar_.Add(this, chrome::NOTIFICATION_UPGRADE_RECOMMENDED,
392 content::NotificationService::AllBrowserContextsAndSources());
[email protected]2fb7dc982010-09-29 12:24:28393 pref_change_registrar_.Init(profile->GetPrefs());
[email protected]90bb38d2012-11-14 18:36:03394 base::Closure callback =
395 base::Bind(&ExtensionService::OnExtensionInstallPrefChanged,
396 base::Unretained(this));
[email protected]234fc5ff2014-01-16 23:32:28397 pref_change_registrar_.Add(extensions::pref_names::kInstallAllowList,
398 callback);
399 pref_change_registrar_.Add(extensions::pref_names::kInstallDenyList,
400 callback);
401 pref_change_registrar_.Add(extensions::pref_names::kAllowedTypes, callback);
[email protected]4814b512009-11-07 00:12:29402
[email protected]93fd78f42009-07-10 16:43:17403 // Set up the ExtensionUpdater
404 if (autoupdate_enabled) {
[email protected]501105b2013-09-26 05:42:02405 int update_frequency = extensions::kDefaultUpdateFrequencySeconds;
[email protected]93fd78f42009-07-10 16:43:17406 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25407 base::StringToInt(command_line->GetSwitchValueASCII(
408 switches::kExtensionsUpdateFrequency),
409 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17410 }
[email protected]044e86992014-01-24 22:59:11411 updater_.reset(new extensions::ExtensionUpdater(
412 this,
413 extension_prefs,
414 profile->GetPrefs(),
415 profile,
416 update_frequency,
417 extensions::ExtensionCache::GetInstance()));
[email protected]93fd78f42009-07-10 16:43:17418 }
419
[email protected]25ae0152011-11-18 14:40:02420 component_loader_.reset(
421 new extensions::ComponentLoader(this,
422 profile->GetPrefs(),
[email protected]55e16cd2013-12-18 04:36:08423 g_browser_process->local_state(),
424 profile));
[email protected]8e4560b62011-01-14 10:09:14425
[email protected]0436b102011-04-15 18:30:03426 if (extensions_enabled_) {
[email protected]ae4c37e2012-12-21 01:16:25427 extensions::ExternalProviderImpl::CreateExternalProviders(
428 this, profile_, &external_extension_providers_);
[email protected]873531342011-03-09 12:16:05429 }
[email protected]b671760b2010-07-15 21:13:47430
[email protected]74474042013-11-21 12:03:54431 // Set this as the ExtensionService for app sorting to ensure it causes syncs
432 // if required.
[email protected]b3aa7182013-04-25 04:45:23433 is_first_run_ = !extension_prefs_->SetAlertSystemFirstRun();
434
[email protected]9ce11d22012-08-08 23:20:13435#if defined(ENABLE_EXTENSIONS)
[email protected]c77f2352012-08-08 22:07:58436 extension_action_storage_manager_.reset(
437 new extensions::ExtensionActionStorageManager(profile_));
[email protected]9ce11d22012-08-08 23:20:13438#endif
[email protected]c77f2352012-08-08 22:07:58439
[email protected]4c9201c42013-08-16 04:56:21440 shared_module_policy_provider_.reset(new SharedModuleProvider);
441
[email protected]cb0e50312011-05-09 15:03:07442 // How long is the path to the Extensions directory?
443 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ExtensionRootPathLength",
444 install_directory_.value().length(), 0, 500, 100);
[email protected]6014d672008-12-05 00:38:25445}
446
[email protected]84df8332011-12-06 18:22:46447const ExtensionSet* ExtensionService::extensions() const {
[email protected]bb1bc9b32013-12-21 03:09:14448 return &registry_->enabled_extensions();
[email protected]ddb1e5a2010-12-13 20:10:45449}
450
[email protected]9e9c1d12013-07-31 01:58:12451const ExtensionSet* ExtensionService::delayed_installs() const {
452 return &delayed_installs_;
453}
454
[email protected]3f213ad2012-07-26 23:39:41455extensions::PendingExtensionManager*
456 ExtensionService::pending_extension_manager() {
[email protected]b2907fd2011-03-25 16:43:37457 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45458}
459
[email protected]eaa7dd182010-12-14 11:09:00460ExtensionService::~ExtensionService() {
[email protected]c8d407e2011-04-28 21:27:17461 // No need to unload extensions here because they are profile-scoped, and the
462 // profile is in the process of being deleted.
[email protected]94b4ffc2011-02-04 05:55:17463
[email protected]5df038b2012-07-16 19:03:27464 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:14465 for (i = external_extension_providers_.begin();
466 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:27467 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:14468 provider->ServiceShutdown();
469 }
[email protected]6014d672008-12-05 00:38:25470}
471
[email protected]037228a2012-10-05 01:36:16472void ExtensionService::Shutdown() {
[email protected]4c9201c42013-08-16 04:56:21473 system_->management_policy()->UnregisterProvider(
474 shared_module_policy_provider_.get());
[email protected]037228a2012-10-05 01:36:16475}
476
[email protected]b2907fd2011-03-25 16:43:37477const Extension* ExtensionService::GetExtensionById(
478 const std::string& id, bool include_disabled) const {
[email protected]599539802014-01-07 23:06:00479 int include_mask = ExtensionRegistry::ENABLED;
[email protected]695b5712012-12-06 23:55:28480 if (include_disabled) {
481 // Include blacklisted extensions here because there are hundreds of
482 // callers of this function, and many might assume that this includes those
483 // that have been disabled due to blacklisting.
[email protected]599539802014-01-07 23:06:00484 include_mask |= ExtensionRegistry::DISABLED |
485 ExtensionRegistry::BLACKLISTED;
[email protected]695b5712012-12-06 23:55:28486 }
[email protected]599539802014-01-07 23:06:00487 return registry_->GetExtensionById(id, include_mask);
[email protected]695b5712012-12-06 23:55:28488}
489
[email protected]eaa7dd182010-12-14 11:09:00490void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12491 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20492
[email protected]3350d712013-11-18 09:32:24493 base::Time begin_time = base::Time::Now();
494
[email protected]4a10006a2013-05-17 23:18:35495 DCHECK(!is_ready()); // Can't redo init.
[email protected]bb1bc9b32013-12-21 03:09:14496 DCHECK_EQ(registry_->enabled_extensions().size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32497
[email protected]820d9bd2013-04-03 03:46:03498 const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
[email protected]820d9bd2013-04-03 03:46:03499 if (cmd_line->HasSwitch(switches::kInstallFromWebstore) ||
500 cmd_line->HasSwitch(switches::kLimitedInstallFromWebstore)) {
501 // The sole purpose of this launch is to install a new extension from CWS
502 // and immediately terminate: loading already installed extensions is
503 // unnecessary and may interfere with the inline install dialog (e.g. if an
504 // extension listens to onStartup and opens a window).
505 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25506 } else {
[email protected]47b60072013-06-20 10:35:56507 // LoadAllExtensions() calls OnLoadedInstalledExtensions().
508 component_loader_->LoadAll();
509 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]9f4e4f082013-06-21 07:11:19510
[email protected]59ee99d2013-10-11 15:46:16511 ReconcileKnownDisabled();
512
[email protected]2edd57852013-08-14 22:07:40513 // Attempt to re-enable extensions whose only disable reason is reloading.
514 std::vector<std::string> extensions_to_enable;
[email protected]bb1bc9b32013-12-21 03:09:14515 const ExtensionSet& disabled_extensions = registry_->disabled_extensions();
516 for (ExtensionSet::const_iterator iter = disabled_extensions.begin();
517 iter != disabled_extensions.end(); ++iter) {
[email protected]2edd57852013-08-14 22:07:40518 const Extension* e = iter->get();
519 if (extension_prefs_->GetDisableReasons(e->id()) ==
520 Extension::DISABLE_RELOAD) {
521 extensions_to_enable.push_back(e->id());
522 }
523 }
524 for (std::vector<std::string>::iterator it = extensions_to_enable.begin();
525 it != extensions_to_enable.end(); ++it) {
526 EnableExtension(*it);
527 }
528
[email protected]9f4e4f082013-06-21 07:11:19529 // Finish install (if possible) of extensions that were still delayed while
530 // the browser was shut down.
531 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> delayed_info(
532 extension_prefs_->GetAllDelayedInstallInfo());
533 for (size_t i = 0; i < delayed_info->size(); ++i) {
534 ExtensionInfo* info = delayed_info->at(i).get();
535 scoped_refptr<const Extension> extension(NULL);
536 if (info->extension_manifest) {
537 std::string error;
538 extension = Extension::Create(
539 info->extension_path,
540 info->extension_location,
541 *info->extension_manifest,
542 extension_prefs_->GetDelayedInstallCreationFlags(
543 info->extension_id),
544 info->extension_id,
545 &error);
546 if (extension.get())
547 delayed_installs_.Insert(extension);
548 }
549 }
550 MaybeFinishDelayedInstallations();
551
552 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> delayed_info2(
553 extension_prefs_->GetAllDelayedInstallInfo());
554 UMA_HISTOGRAM_COUNTS_100("Extensions.UpdateOnLoad",
555 delayed_info2->size() - delayed_info->size());
556
[email protected]47b60072013-06-20 10:35:56557 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25558
[email protected]47b60072013-06-20 10:35:56559 // TODO(erikkay) this should probably be deferred to a future point
560 // rather than running immediately at startup.
561 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57562
[email protected]33dc0c62014-02-13 00:00:38563 MaybeBootstrapVerifier();
[email protected]47b60072013-06-20 10:35:56564 base::MessageLoop::current()->PostDelayedTask(
565 FROM_HERE,
566 base::Bind(&ExtensionService::GarbageCollectExtensions, AsWeakPtr()),
567 base::TimeDelta::FromSeconds(kGarbageCollectStartupDelay));
[email protected]215a7be2012-10-22 19:53:42568
[email protected]820d9bd2013-04-03 03:46:03569 if (extension_prefs_->NeedsStorageGarbageCollection()) {
570 GarbageCollectIsolatedStorage();
571 extension_prefs_->SetNeedsStorageGarbageCollection(false);
572 }
[email protected]4c9201c42013-08-16 04:56:21573 system_->management_policy()->RegisterProvider(
574 shared_module_policy_provider_.get());
[email protected]2d19eb6e2014-01-27 17:30:00575
576 LoadGreylistFromPrefs();
[email protected]399583b2012-12-11 09:33:42577 }
[email protected]3350d712013-11-18 09:32:24578
579 UMA_HISTOGRAM_TIMES("Extensions.ExtensionServiceInitTime",
580 base::Time::Now() - begin_time);
[email protected]6014d672008-12-05 00:38:25581}
582
[email protected]2d19eb6e2014-01-27 17:30:00583void ExtensionService::LoadGreylistFromPrefs() {
[email protected]f47f7172014-03-19 19:27:10584 scoped_ptr<ExtensionSet> all_extensions =
585 registry_->GenerateInstalledExtensionsSet();
[email protected]2d19eb6e2014-01-27 17:30:00586
587 for (ExtensionSet::const_iterator it = all_extensions->begin();
588 it != all_extensions->end(); ++it) {
589 extensions::BlacklistState state =
590 extension_prefs_->GetExtensionBlacklistState((*it)->id());
591 if (state == extensions::BLACKLISTED_SECURITY_VULNERABILITY ||
592 state == extensions::BLACKLISTED_POTENTIALLY_UNWANTED ||
593 state == extensions::BLACKLISTED_CWS_POLICY_VIOLATION)
594 greylist_.Insert(*it);
595 }
596}
597
[email protected]33dc0c62014-02-13 00:00:38598void ExtensionService::MaybeBootstrapVerifier() {
[email protected]d820bc52014-03-13 05:35:23599 InstallVerifier* verifier = system_->install_verifier();
[email protected]33dc0c62014-02-13 00:00:38600 bool do_bootstrap = false;
601
602 if (verifier->NeedsBootstrap()) {
603 do_bootstrap = true;
604 } else {
[email protected]d9d44ec2014-02-21 22:44:07605 scoped_ptr<extensions::ExtensionSet> extensions =
[email protected]f47f7172014-03-19 19:27:10606 registry_->GenerateInstalledExtensionsSet();
[email protected]d9d44ec2014-02-21 22:44:07607 for (extensions::ExtensionSet::const_iterator i = extensions->begin();
608 i != extensions->end();
[email protected]33dc0c62014-02-13 00:00:38609 ++i) {
610 const Extension& extension = **i;
[email protected]8abef232014-03-07 08:54:37611
[email protected]33dc0c62014-02-13 00:00:38612 if (verifier->NeedsVerification(extension) &&
[email protected]8abef232014-03-07 08:54:37613 !verifier->IsKnownId(extension.id())) {
[email protected]33dc0c62014-02-13 00:00:38614 do_bootstrap = true;
615 break;
616 }
617 }
618 }
619 if (do_bootstrap)
620 VerifyAllExtensions(true); // bootstrap=true.
621}
622
[email protected]74564482014-01-30 13:55:42623void ExtensionService::VerifyAllExtensions(bool bootstrap) {
[email protected]68e5cc72013-12-17 02:41:53624 ExtensionIdSet to_add;
[email protected]f47f7172014-03-19 19:27:10625 scoped_ptr<ExtensionSet> all_extensions =
626 registry_->GenerateInstalledExtensionsSet();
[email protected]68e5cc72013-12-17 02:41:53627
628 for (ExtensionSet::const_iterator i = all_extensions->begin();
629 i != all_extensions->end(); ++i) {
630 const Extension& extension = **i;
631
[email protected]88622932014-01-16 17:59:12632 if (InstallVerifier::NeedsVerification(extension))
[email protected]68e5cc72013-12-17 02:41:53633 to_add.insert(extension.id());
634 }
[email protected]d820bc52014-03-13 05:35:23635 system_->install_verifier()->AddMany(
636 to_add,
637 base::Bind(&ExtensionService::FinishVerifyAllExtensions,
638 AsWeakPtr(),
639 bootstrap));
[email protected]68e5cc72013-12-17 02:41:53640}
641
[email protected]74564482014-01-30 13:55:42642void ExtensionService::FinishVerifyAllExtensions(bool bootstrap, bool success) {
643 LogVerifyAllSuccessHistogram(bootstrap, success);
[email protected]68e5cc72013-12-17 02:41:53644 if (success) {
645 // Check to see if any currently unverified extensions became verified.
[email protected]d820bc52014-03-13 05:35:23646 InstallVerifier* verifier = system_->install_verifier();
[email protected]bb1bc9b32013-12-21 03:09:14647 const ExtensionSet& disabled_extensions = registry_->disabled_extensions();
648 for (ExtensionSet::const_iterator i = disabled_extensions.begin();
649 i != disabled_extensions.end(); ++i) {
[email protected]68e5cc72013-12-17 02:41:53650 const Extension& extension = **i;
651 int disable_reasons = extension_prefs_->GetDisableReasons(extension.id());
652 if (disable_reasons & Extension::DISABLE_NOT_VERIFIED &&
653 !verifier->MustRemainDisabled(&extension, NULL, NULL)) {
654 extension_prefs_->RemoveDisableReason(extension.id(),
655 Extension::DISABLE_NOT_VERIFIED);
656 // Notify interested observers (eg the extensions settings page) by
657 // sending an UNLOADED notification.
658 //
659 // TODO(asargent) - this is a slight hack because it's already
660 // disabled; the right solution might be to add a separate listener
661 // interface for DisableReason's changing. http://crbug.com/328916
662 UnloadedExtensionInfo details(&extension,
663 UnloadedExtensionInfo::REASON_DISABLE);
664 content::NotificationService::current()->Notify(
[email protected]ec870532014-03-19 20:24:05665 chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
[email protected]68e5cc72013-12-17 02:41:53666 content::Source<Profile>(profile_),
667 content::Details<UnloadedExtensionInfo>(&details));
668 }
669 }
670 // Might disable some extensions.
671 CheckManagementPolicy();
672 }
673}
674
[email protected]31bb5ee62012-09-12 22:58:40675bool ExtensionService::UpdateExtension(const std::string& id,
[email protected]650b2d52013-02-10 03:41:45676 const base::FilePath& extension_path,
[email protected]044e86992014-01-24 22:59:11677 bool file_ownership_passed,
[email protected]31bb5ee62012-09-12 22:58:40678 const GURL& download_url,
679 CrxInstaller** out_crx_installer) {
[email protected]a29a517a2011-01-21 21:11:12680 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]3c4abc82012-10-22 22:25:54681 if (browser_terminating_) {
682 LOG(WARNING) << "Skipping UpdateExtension due to browser shutdown";
683 // Leak the temp file at extension_path. We don't want to add to the disk
684 // I/O burden at shutdown, we can't rely on the I/O completing anyway, and
685 // the file is in the OS temp directory which should be cleaned up for us.
686 return false;
687 }
[email protected]a8af9fdb2010-10-28 21:52:20688
[email protected]3f213ad2012-07-26 23:39:41689 const extensions::PendingExtensionInfo* pending_extension_info =
[email protected]51a3bf8b2012-06-08 22:53:06690 pending_extension_manager()->GetById(id);
[email protected]8ef78fd2010-08-19 17:14:32691
[email protected]695b5712012-12-06 23:55:28692 const Extension* extension = GetInstalledExtension(id);
[email protected]51a3bf8b2012-06-08 22:53:06693 if (!pending_extension_info && !extension) {
[email protected]aa142702010-03-26 01:26:33694 LOG(WARNING) << "Will not update extension " << id
695 << " because it is not installed or pending";
696 // Delete extension_path since we're not creating a CrxInstaller
697 // that would do it for us.
[email protected]7f8f24f2012-11-15 19:40:14698 if (!GetFileTaskRunner()->PostTask(
699 FROM_HERE,
[email protected]53612e82011-10-18 18:00:36700 base::Bind(
[email protected]7296f2762011-11-21 19:23:44701 &extension_file_util::DeleteFile, extension_path, false)))
[email protected]14908b72011-04-20 06:54:36702 NOTREACHED();
[email protected]420a0ec2011-06-01 01:07:03703
704 return false;
[email protected]e957fe52009-06-23 16:51:05705 }
706
[email protected]aa142702010-03-26 01:26:33707 // We want a silent install only for non-pending extensions and
708 // pending extensions that have install_silently set.
[email protected]f8636f92013-08-09 21:02:37709 scoped_ptr<ExtensionInstallPrompt> client;
[email protected]91e51d612012-10-21 23:03:05710 if (pending_extension_info && !pending_extension_info->install_silently())
[email protected]f8636f92013-08-09 21:02:37711 client.reset(ExtensionInstallUI::CreateInstallPromptWithProfile(profile_));
[email protected]aa142702010-03-26 01:26:33712
[email protected]f8636f92013-08-09 21:02:37713 scoped_refptr<CrxInstaller> installer(
714 CrxInstaller::Create(this, client.Pass()));
[email protected]6dfbbf82010-03-12 23:09:16715 installer->set_expected_id(id);
[email protected]464213a2013-10-15 01:06:48716 int creation_flags = Extension::NO_FLAGS;
[email protected]51a3bf8b2012-06-08 22:53:06717 if (pending_extension_info) {
718 installer->set_install_source(pending_extension_info->install_source());
719 if (pending_extension_info->install_silently())
720 installer->set_allow_silent_install(true);
[email protected]464213a2013-10-15 01:06:48721 creation_flags = pending_extension_info->creation_flags();
722 if (pending_extension_info->mark_acknowledged())
723 AcknowledgeExternalExtension(id);
[email protected]51a3bf8b2012-06-08 22:53:06724 } else if (extension) {
[email protected]5eb375e92010-11-26 07:50:41725 installer->set_install_source(extension->location());
[email protected]51a3bf8b2012-06-08 22:53:06726 }
[email protected]7d8b7072012-04-07 01:07:46727 // If the extension was installed from or has migrated to the webstore, or
[email protected]fcb2c2c2012-10-17 21:08:45728 // its auto-update URL is from the webstore, treat it as a webstore install.
729 // Note that we ignore some older extensions with blank auto-update URLs
730 // because we are mostly concerned with restrictions on NaCl extensions,
731 // which are newer.
[email protected]75764512011-12-19 19:54:28732 if ((extension && extension->from_webstore()) ||
[email protected]4a5fe3e22013-06-04 07:06:38733 (extension && extensions::ManifestURL::UpdatesFromGallery(extension)) ||
[email protected]963d13c2012-09-29 17:13:35734 (!extension && extension_urls::IsWebstoreUpdateUrl(
[email protected]51a3bf8b2012-06-08 22:53:06735 pending_extension_info->update_url()))) {
[email protected]a12ce8b22012-01-17 18:40:53736 creation_flags |= Extension::FROM_WEBSTORE;
737 }
738
739 // Bookmark apps being updated is kind of a contradiction, but that's because
740 // we mark the default apps as bookmark apps, and they're hosted in the web
741 // store, thus they can get updated. See http://crbug.com/101605 for more
742 // details.
743 if (extension && extension->from_bookmark())
744 creation_flags |= Extension::FROM_BOOKMARK;
745
[email protected]e33bbc22012-08-27 22:05:46746 if (extension && extension->was_installed_by_default())
747 creation_flags |= Extension::WAS_INSTALLED_BY_DEFAULT;
748
[email protected]acc3c7c22014-03-19 06:23:39749 if (extension && extension->was_installed_by_oem())
750 creation_flags |= Extension::WAS_INSTALLED_BY_OEM;
751
[email protected]ab6c7be42014-01-16 02:05:54752 if (extension && extension->is_ephemeral())
753 creation_flags |= Extension::IS_EPHEMERAL;
754
[email protected]a12ce8b22012-01-17 18:40:53755 installer->set_creation_flags(creation_flags);
756
[email protected]044e86992014-01-24 22:59:11757 installer->set_delete_source(file_ownership_passed);
[email protected]655b2b1a2011-10-13 17:13:06758 installer->set_download_url(download_url);
[email protected]cb0e50312011-05-09 15:03:07759 installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
[email protected]6dfbbf82010-03-12 23:09:16760 installer->InstallCrx(extension_path);
[email protected]420a0ec2011-06-01 01:07:03761
762 if (out_crx_installer)
[email protected]dc24976f2013-06-02 21:15:09763 *out_crx_installer = installer.get();
[email protected]420a0ec2011-06-01 01:07:03764
765 return true;
[email protected]e957fe52009-06-23 16:51:05766}
767
[email protected]e178ad92013-06-28 02:29:25768void ExtensionService::ReloadExtension(const std::string extension_id) {
[email protected]a29a517a2011-01-21 21:11:12769 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]2a947202013-03-06 04:58:05770
771 // If the extension is already reloading, don't reload again.
772 if (extension_prefs_->GetDisableReasons(extension_id) &
773 Extension::DISABLE_RELOAD) {
774 return;
775 }
776
[email protected]650b2d52013-02-10 03:41:45777 base::FilePath path;
[email protected]9adb9692010-10-29 23:14:02778 const Extension* current_extension = GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40779
[email protected]f17dbd42010-08-16 23:21:10780 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]b65272f2009-08-31 15:47:06781 if (current_extension) {
[email protected]4814b512009-11-07 00:12:29782 // If the extension has an inspector open for its background page, detach
783 // the inspector and hang onto a cookie for it, so that we can reattach
784 // later.
[email protected]31d8f5f22012-04-02 15:22:08785 // TODO(yoz): this is not incognito-safe!
[email protected]98b6d942013-11-10 00:34:07786 extensions::ProcessManager* manager = system_->process_manager();
[email protected]3a1dc572012-07-31 22:25:13787 extensions::ExtensionHost* host =
788 manager->GetBackgroundHostForExtension(extension_id);
[email protected]98f66112012-12-25 12:59:36789 if (host && DevToolsAgentHost::HasFor(host->render_view_host())) {
[email protected]4814b512009-11-07 00:12:29790 // Look for an open inspector for the background page.
[email protected]04ea1bb2013-07-10 09:26:09791 scoped_refptr<DevToolsAgentHost> agent_host =
792 DevToolsAgentHost::GetOrCreateFor(host->render_view_host());
793 agent_host->DisconnectRenderViewHost();
794 orphaned_dev_tools_[extension_id] = agent_host;
[email protected]4814b512009-11-07 00:12:29795 }
796
[email protected]b65272f2009-08-31 15:47:06797 path = current_extension->path();
[email protected]58076192013-07-19 19:43:50798 // BeingUpgraded is set back to false when the extension is added.
[email protected]45f5b7d2014-01-22 23:47:13799 system_->runtime_data()->SetBeingUpgraded(current_extension, true);
[email protected]44d62b62012-04-11 00:06:03800 DisableExtension(extension_id, Extension::DISABLE_RELOAD);
[email protected]b914e2952013-04-26 07:10:03801 reloading_extensions_.insert(extension_id);
[email protected]1eb175082010-02-10 09:26:16802 } else {
803 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06804 }
805
[email protected]9f4e4f082013-06-21 07:11:19806 if (delayed_installs_.Contains(extension_id)) {
[email protected]6f6101832012-11-27 22:10:48807 FinishDelayedInstallation(extension_id);
[email protected]0db124b02012-11-07 04:55:05808 return;
809 }
810
[email protected]43ceb002012-02-10 23:19:15811 // If we're reloading a component extension, use the component extension
812 // loader's reloader.
813 if (component_loader_->Exists(extension_id)) {
[email protected]e178ad92013-06-28 02:29:25814 SetBeingReloaded(extension_id, true);
[email protected]43ceb002012-02-10 23:19:15815 component_loader_->Reload(extension_id);
[email protected]e178ad92013-06-28 02:29:25816 SetBeingReloaded(extension_id, false);
[email protected]43ceb002012-02-10 23:19:15817 return;
818 }
819
[email protected]e6090e42010-03-23 22:44:08820 // Check the installed extensions to see if what we're reloading was already
821 // installed.
[email protected]e178ad92013-06-28 02:29:25822 SetBeingReloaded(extension_id, true);
[email protected]e6090e42010-03-23 22:44:08823 scoped_ptr<ExtensionInfo> installed_extension(
824 extension_prefs_->GetInstalledExtensionInfo(extension_id));
825 if (installed_extension.get() &&
826 installed_extension->extension_manifest.get()) {
[email protected]d8c8f25f2011-11-02 18:18:01827 extensions::InstalledLoader(this).Load(*installed_extension, false);
[email protected]e6090e42010-03-23 22:44:08828 } else {
[email protected]d8c8f25f2011-11-02 18:18:01829 // Otherwise, the extension is unpacked (location LOAD).
[email protected]e6090e42010-03-23 22:44:08830 // We should always be able to remember the extension's path. If it's not in
831 // the map, someone failed to update |unloaded_extension_paths_|.
832 CHECK(!path.empty());
[email protected]d8c8f25f2011-11-02 18:18:01833 extensions::UnpackedInstaller::Create(this)->Load(path);
[email protected]e6090e42010-03-23 22:44:08834 }
[email protected]e178ad92013-06-28 02:29:25835 // When reloading is done, mark this extension as done reloading.
836 SetBeingReloaded(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40837}
838
[email protected]fa2416f2011-05-03 08:41:20839bool ExtensionService::UninstallExtension(
[email protected]a12c706e2011-12-01 00:58:08840 std::string extension_id,
[email protected]fa2416f2011-05-03 08:41:20841 bool external_uninstall,
[email protected]439f1e32013-12-09 20:09:09842 base::string16* error) {
[email protected]a29a517a2011-01-21 21:11:12843 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20844
[email protected]0d6ec3a72011-09-02 02:09:43845 scoped_refptr<const Extension> extension(GetInstalledExtension(extension_id));
[email protected]631cf822009-05-15 07:01:25846
[email protected]e7afe2452010-08-22 16:19:13847 // Callers should not send us nonexistent extensions.
[email protected]dc24976f2013-06-02 21:15:09848 CHECK(extension.get());
[email protected]9f1087e2009-06-15 17:29:32849
[email protected]95da88c42011-03-31 10:07:33850 // Policy change which triggers an uninstall will always set
851 // |external_uninstall| to true so this is the only way to uninstall
852 // managed extensions.
[email protected]65187152012-06-02 13:14:14853 if (!external_uninstall &&
854 !system_->management_policy()->UserMayModifySettings(
855 extension.get(), error)) {
[email protected]ad50def52011-10-19 23:17:07856 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27857 chrome::NOTIFICATION_EXTENSION_UNINSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:53858 content::Source<Profile>(profile_),
[email protected]dc24976f2013-06-02 21:15:09859 content::Details<const Extension>(extension.get()));
[email protected]d6ebc9792011-04-07 18:18:33860 return false;
861 }
[email protected]95da88c42011-03-31 10:07:33862
[email protected]65f173552012-06-28 22:43:58863 syncer::SyncChange sync_change;
[email protected]f8aefb132013-10-30 09:29:52864 if (extension_sync_service_) {
865 sync_change = extension_sync_service_->PrepareToSyncUninstallExtension(
866 extension.get(), is_ready());
[email protected]3bdba0d2011-08-23 07:17:30867 }
868
[email protected]d820bc52014-03-13 05:35:23869 system_->install_verifier()->Remove(extension->id());
[email protected]ffd2f79e2013-11-14 00:11:46870
[email protected]dc24976f2013-06-02 21:15:09871 if (IsUnacknowledgedExternalExtension(extension.get())) {
[email protected]d96eb512012-11-01 23:44:08872 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
873 EXTERNAL_EXTENSION_UNINSTALLED,
874 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:02875 if (extensions::ManifestURL::UpdatesFromGallery(extension.get())) {
876 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventWebstore",
877 EXTERNAL_EXTENSION_UNINSTALLED,
878 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
879 } else {
880 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventNonWebstore",
881 EXTERNAL_EXTENSION_UNINSTALLED,
882 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
883 }
[email protected]d96eb512012-11-01 23:44:08884 }
[email protected]9b217652010-10-08 22:04:23885 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08886 extension->GetType(), 100);
[email protected]dc24976f2013-06-02 21:15:09887 RecordPermissionMessagesHistogram(extension.get(),
888 "Extensions.Permissions_Uninstall");
[email protected]9b217652010-10-08 22:04:23889
[email protected]831aa212010-03-26 13:55:19890 // Unload before doing more cleanup to ensure that nothing is hanging on to
891 // any of these resources.
[email protected]b0af4792013-10-23 09:12:13892 UnloadExtension(extension_id, UnloadedExtensionInfo::REASON_UNINSTALL);
[email protected]831aa212010-03-26 13:55:19893
[email protected]9f1087e2009-06-15 17:29:32894 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]12075d12013-02-27 05:38:05895 if (!Manifest::IsUnpackedLocation(extension->location())) {
[email protected]7f8f24f2012-11-15 19:40:14896 if (!GetFileTaskRunner()->PostTask(
897 FROM_HERE,
[email protected]53612e82011-10-18 18:00:36898 base::Bind(
[email protected]14908b72011-04-20 06:54:36899 &extension_file_util::UninstallExtension,
900 install_directory_,
[email protected]fa2416f2011-05-03 08:41:20901 extension_id)))
[email protected]14908b72011-04-20 06:54:36902 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32903 }
904
[email protected]ca0336342014-03-21 12:58:34905 // Do not remove the data of ephemeral apps. They will be garbage collected by
906 // EphemeralAppService.
907 if (!extension->is_ephemeral())
908 extensions::DataDeleter::StartDeleting(profile_, extension.get());
[email protected]0d6ec3a72011-09-02 02:09:43909
[email protected]0dfe05c2011-02-23 23:03:36910 UntrackTerminatedExtension(extension_id);
[email protected]211030342010-09-30 18:41:06911
912 // Notify interested parties that we've uninstalled this extension.
[email protected]ad50def52011-10-19 23:17:07913 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27914 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
[email protected]6c2381d2011-10-19 02:52:53915 content::Source<Profile>(profile_),
[email protected]dc24976f2013-06-02 21:15:09916 content::Details<const Extension>(extension.get()));
[email protected]d6ebc9792011-04-07 18:18:33917
[email protected]f8aefb132013-10-30 09:29:52918 if (extension_sync_service_) {
919 extension_sync_service_->ProcessSyncUninstallExtension(extension_id,
920 sync_change);
[email protected]3bdba0d2011-08-23 07:17:30921 }
922
[email protected]399583b2012-12-11 09:33:42923 delayed_installs_.Remove(extension_id);
[email protected]0db124b02012-11-07 04:55:05924
[email protected]198b5902013-06-27 10:36:11925 PruneSharedModulesOnUninstall(extension.get());
[email protected]9f4e4f082013-06-21 07:11:19926
[email protected]d4eda592013-09-18 03:37:57927 extension_prefs_->OnExtensionUninstalled(extension_id, extension->location(),
928 external_uninstall);
929
[email protected]333b1de2011-09-12 18:28:50930 // Track the uninstallation.
[email protected]49098f702011-10-13 03:47:18931 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionUninstalled", 1, 2);
932
[email protected]d6ebc9792011-04-07 18:18:33933 return true;
[email protected]c10da4b02010-03-25 14:38:32934}
935
[email protected]c3cfb012011-04-06 22:07:35936bool ExtensionService::IsExtensionEnabled(
937 const std::string& extension_id) const {
[email protected]bb1bc9b32013-12-21 03:09:14938 if (registry_->enabled_extensions().Contains(extension_id) ||
939 registry_->terminated_extensions().Contains(extension_id)) {
[email protected]36429da2011-07-11 20:25:18940 return true;
[email protected]dc9a74f72012-08-17 18:07:21941 }
[email protected]36429da2011-07-11 20:25:18942
[email protected]bb1bc9b32013-12-21 03:09:14943 if (registry_->disabled_extensions().Contains(extension_id) ||
944 registry_->blacklisted_extensions().Contains(extension_id)) {
[email protected]ad83ca242011-07-29 01:32:25945 return false;
[email protected]695b5712012-12-06 23:55:28946 }
[email protected]ad83ca242011-07-29 01:32:25947
948 // If the extension hasn't been loaded yet, check the prefs for it. Assume
949 // enabled unless otherwise noted.
950 return !extension_prefs_->IsExtensionDisabled(extension_id) &&
[email protected]f574c402012-12-04 23:20:31951 !extension_prefs_->IsExternalExtensionUninstalled(extension_id);
[email protected]c3cfb012011-04-06 22:07:35952}
953
[email protected]eaa7dd182010-12-14 11:09:00954void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12955 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20956
[email protected]06f92562011-04-29 19:27:31957 if (IsExtensionEnabled(extension_id))
[email protected]0c6da502009-08-14 22:32:39958 return;
[email protected]bb1bc9b32013-12-21 03:09:14959 const Extension* extension =
960 registry_->disabled_extensions().GetByID(extension_id);
[email protected]0d54b682013-11-05 14:15:36961
962 ManagementPolicy* policy = system_->management_policy();
963 if (extension && policy->MustRemainDisabled(extension, NULL, NULL)) {
964 UMA_HISTOGRAM_COUNTS_100("Extensions.EnableDeniedByPolicy", 1);
965 return;
966 }
[email protected]0c6da502009-08-14 22:32:39967
[email protected]b3317ad2011-04-28 23:46:00968 extension_prefs_->SetExtensionState(extension_id, Extension::ENABLED);
[email protected]eb5e4f92012-08-15 23:33:28969 extension_prefs_->ClearDisableReasons(extension_id);
[email protected]1784e83a2009-09-08 21:01:52970
[email protected]06f92562011-04-29 19:27:31971 // This can happen if sync enables an extension that is not
972 // installed yet.
973 if (!extension)
974 return;
975
[email protected]d96eb512012-11-01 23:44:08976 if (IsUnacknowledgedExternalExtension(extension)) {
977 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
978 EXTERNAL_EXTENSION_REENABLED,
979 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:02980 if (extensions::ManifestURL::UpdatesFromGallery(extension)) {
981 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventWebstore",
982 EXTERNAL_EXTENSION_REENABLED,
983 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
984 } else {
985 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventNonWebstore",
986 EXTERNAL_EXTENSION_REENABLED,
987 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
988 }
[email protected]612a1cb12012-10-17 13:18:03989 AcknowledgeExternalExtension(extension->id());
[email protected]d96eb512012-11-01 23:44:08990 }
[email protected]612a1cb12012-10-17 13:18:03991
[email protected]0c6da502009-08-14 22:32:39992 // Move it over to the enabled list.
[email protected]bb1bc9b32013-12-21 03:09:14993 registry_->AddEnabled(make_scoped_refptr(extension));
994 registry_->RemoveDisabled(extension->id());
[email protected]0c6da502009-08-14 22:32:39995
[email protected]62d30f42009-10-01 22:36:06996 NotifyExtensionLoaded(extension);
[email protected]3bdba0d2011-08-23 07:17:30997
[email protected]f74d7f32012-06-19 19:22:51998 // Notify listeners that the extension was enabled.
999 content::NotificationService::current()->Notify(
1000 chrome::NOTIFICATION_EXTENSION_ENABLED,
1001 content::Source<Profile>(profile_),
1002 content::Details<const Extension>(extension));
1003
[email protected]f8aefb132013-10-30 09:29:521004 if (extension_sync_service_)
1005 extension_sync_service_->SyncEnableExtension(*extension);
[email protected]0c6da502009-08-14 22:32:391006}
1007
[email protected]44d62b62012-04-11 00:06:031008void ExtensionService::DisableExtension(
1009 const std::string& extension_id,
1010 Extension::DisableReason disable_reason) {
[email protected]a29a517a2011-01-21 21:11:121011 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:201012
[email protected]b2ba9962009-12-10 20:10:151013 // The extension may have been disabled already.
[email protected]06f92562011-04-29 19:27:311014 if (!IsExtensionEnabled(extension_id))
[email protected]1784e83a2009-09-08 21:01:521015 return;
[email protected]1784e83a2009-09-08 21:01:521016
[email protected]06f92562011-04-29 19:27:311017 const Extension* extension = GetInstalledExtension(extension_id);
1018 // |extension| can be NULL if sync disables an extension that is not
1019 // installed yet.
[email protected]65187152012-06-02 13:14:141020 if (extension &&
[email protected]505ccdc2013-06-20 20:15:351021 disable_reason != Extension::DISABLE_RELOAD &&
[email protected]65187152012-06-02 13:14:141022 !system_->management_policy()->UserMayModifySettings(extension, NULL)) {
[email protected]95da88c42011-03-31 10:07:331023 return;
[email protected]65187152012-06-02 13:14:141024 }
[email protected]95da88c42011-03-31 10:07:331025
[email protected]b3317ad2011-04-28 23:46:001026 extension_prefs_->SetExtensionState(extension_id, Extension::DISABLED);
[email protected]eb5e4f92012-08-15 23:33:281027 extension_prefs_->AddDisableReason(extension_id, disable_reason);
[email protected]1784e83a2009-09-08 21:01:521028
[email protected]599539802014-01-07 23:06:001029 int include_mask =
1030 ExtensionRegistry::EVERYTHING & ~ExtensionRegistry::DISABLED;
1031 extension = registry_->GetExtensionById(extension_id, include_mask);
[email protected]06f92562011-04-29 19:27:311032 if (!extension)
1033 return;
1034
[email protected]45f5b7d2014-01-22 23:47:131035 // The extension is either enabled or terminated.
1036 DCHECK(registry_->enabled_extensions().Contains(extension->id()) ||
1037 registry_->terminated_extensions().Contains(extension->id()));
[email protected]64742de32013-04-22 08:44:341038
[email protected]5c094792012-09-07 19:44:531039 // Move it over to the disabled list. Don't send a second unload notification
1040 // for terminated extensions being disabled.
[email protected]bb1bc9b32013-12-21 03:09:141041 registry_->AddDisabled(make_scoped_refptr(extension));
1042 if (registry_->enabled_extensions().Contains(extension->id())) {
1043 registry_->RemoveEnabled(extension->id());
[email protected]b0af4792013-10-23 09:12:131044 NotifyExtensionUnloaded(extension, UnloadedExtensionInfo::REASON_DISABLE);
[email protected]5c094792012-09-07 19:44:531045 } else {
[email protected]bb1bc9b32013-12-21 03:09:141046 registry_->RemoveTerminated(extension->id());
[email protected]5c094792012-09-07 19:44:531047 }
[email protected]3bdba0d2011-08-23 07:17:301048
[email protected]f8aefb132013-10-30 09:29:521049 if (extension_sync_service_)
1050 extension_sync_service_->SyncDisableExtension(*extension);
[email protected]1784e83a2009-09-08 21:01:521051}
1052
[email protected]1abf05e2013-07-09 17:04:361053void ExtensionService::DisableUserExtensions(
1054 const std::vector<std::string>& except_ids) {
[email protected]e516e4c2013-06-12 17:41:141055 extensions::ManagementPolicy* management_policy =
1056 system_->management_policy();
1057 extensions::ExtensionList to_disable;
1058
[email protected]75181f62014-03-19 21:41:121059 // TODO(rlp): Clean up this code. crbug.com/353266.
[email protected]bb1bc9b32013-12-21 03:09:141060 const ExtensionSet& enabled_set = registry_->enabled_extensions();
1061 for (ExtensionSet::const_iterator extension = enabled_set.begin();
1062 extension != enabled_set.end(); ++extension) {
[email protected]75181f62014-03-19 21:41:121063 if (management_policy->UserMayModifySettings(extension->get(), NULL) &&
1064 extension->get()->location() != Manifest::EXTERNAL_COMPONENT)
[email protected]e516e4c2013-06-12 17:41:141065 to_disable.push_back(*extension);
1066 }
[email protected]bb1bc9b32013-12-21 03:09:141067 const ExtensionSet& terminated_set = registry_->terminated_extensions();
1068 for (ExtensionSet::const_iterator extension = terminated_set.begin();
1069 extension != terminated_set.end(); ++extension) {
[email protected]75181f62014-03-19 21:41:121070 if (management_policy->UserMayModifySettings(extension->get(), NULL) &&
1071 extension->get()->location() != Manifest::EXTERNAL_COMPONENT)
[email protected]e516e4c2013-06-12 17:41:141072 to_disable.push_back(*extension);
1073 }
1074
1075 for (extensions::ExtensionList::const_iterator extension = to_disable.begin();
1076 extension != to_disable.end(); ++extension) {
[email protected]3082fe32013-08-06 11:12:381077 if ((*extension)->was_installed_by_default() &&
1078 extension_urls::IsWebstoreUpdateUrl(
1079 extensions::ManifestURL::GetUpdateURL(*extension)))
1080 continue;
[email protected]1abf05e2013-07-09 17:04:361081 const std::string& id = (*extension)->id();
1082 if (except_ids.end() == std::find(except_ids.begin(), except_ids.end(), id))
1083 DisableExtension(id, extensions::Extension::DISABLE_USER_ACTION);
[email protected]e516e4c2013-06-12 17:41:141084 }
1085}
1086
[email protected]eaa7dd182010-12-14 11:09:001087void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]009633c2013-03-07 22:08:281088 const Extension* extension) {
1089 GrantPermissions(extension);
[email protected]fe2dd7742011-04-19 22:52:491090 RecordPermissionMessagesHistogram(
1091 extension, "Extensions.Permissions_ReEnable");
[email protected]8d888c12010-11-30 00:00:251092 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
1093 EnableExtension(extension->id());
1094}
1095
[email protected]009633c2013-03-07 22:08:281096void ExtensionService::GrantPermissions(const Extension* extension) {
[email protected]be083862012-09-01 03:53:451097 CHECK(extension);
1098 extensions::PermissionsUpdater perms_updater(profile());
[email protected]009633c2013-03-07 22:08:281099 perms_updater.GrantActivePermissions(extension);
[email protected]be083862012-09-01 03:53:451100}
1101
[email protected]fe2dd7742011-04-19 22:52:491102// static
1103void ExtensionService::RecordPermissionMessagesHistogram(
[email protected]13c68b62013-05-17 11:29:051104 const Extension* extension, const char* histogram) {
[email protected]de415552013-01-23 04:12:171105 // Since this is called from multiple sources, and since the histogram macros
1106 // use statics, we need to manually lookup the histogram ourselves.
1107 base::HistogramBase* counter = base::LinearHistogram::FactoryGet(
[email protected]fe2dd7742011-04-19 22:52:491108 histogram,
1109 1,
[email protected]c2e66e12012-06-27 06:27:061110 PermissionMessage::kEnumBoundary,
1111 PermissionMessage::kEnumBoundary + 1,
[email protected]de415552013-01-23 04:12:171112 base::HistogramBase::kUmaTargetedHistogramFlag);
[email protected]fe2dd7742011-04-19 22:52:491113
[email protected]13c68b62013-05-17 11:29:051114 PermissionMessages permissions =
1115 extensions::PermissionsData::GetPermissionMessages(extension);
[email protected]fe2dd7742011-04-19 22:52:491116 if (permissions.empty()) {
[email protected]c2e66e12012-06-27 06:27:061117 counter->Add(PermissionMessage::kNone);
[email protected]fe2dd7742011-04-19 22:52:491118 } else {
[email protected]c2e66e12012-06-27 06:27:061119 for (PermissionMessages::iterator it = permissions.begin();
[email protected]0d3e4a22011-06-23 19:02:521120 it != permissions.end(); ++it)
1121 counter->Add(it->id());
[email protected]fe2dd7742011-04-19 22:52:491122 }
1123}
1124
[email protected]eaa7dd182010-12-14 11:09:001125void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]57a777f72010-03-31 01:09:421126 // The ChromeURLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:061127 // was loaded, otherwise a race can arise where a renderer that is created
1128 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:421129 // that the request context doesn't yet know about. The profile is responsible
1130 // for ensuring its URLRequestContexts appropriately discover the loaded
1131 // extension.
[email protected]31d8f5f22012-04-02 15:22:081132 system_->RegisterExtensionWithRequestContexts(extension);
[email protected]62d30f42009-10-01 22:36:061133
[email protected]d5949312012-12-03 22:13:301134 // Tell renderers about the new extension, unless it's a theme (renderers
1135 // don't need to know about themes).
[email protected]a58f599c2012-12-01 03:08:191136 if (!extension->is_theme()) {
[email protected]19647262011-12-16 09:57:491137 for (content::RenderProcessHost::iterator i(
1138 content::RenderProcessHost::AllHostsIterator());
1139 !i.IsAtEnd(); i.Advance()) {
1140 content::RenderProcessHost* host = i.GetCurrentValue();
1141 Profile* host_profile =
1142 Profile::FromBrowserContext(host->GetBrowserContext());
1143 if (host_profile->GetOriginalProfile() ==
1144 profile_->GetOriginalProfile()) {
1145 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions(
1146 1, ExtensionMsg_Loaded_Params(extension));
1147 host->Send(
1148 new ExtensionMsg_Loaded(loaded_extensions));
1149 }
[email protected]c8d407e2011-04-28 21:27:171150 }
[email protected]77a6970c2011-04-23 16:58:561151 }
1152
[email protected]3442a662012-01-12 08:06:171153 // Tell subsystems that use the EXTENSION_LOADED notification about the new
1154 // extension.
1155 //
1156 // NOTE: It is important that this happen after notifying the renderers about
1157 // the new extensions so that if we navigate to an extension URL in
1158 // NOTIFICATION_EXTENSION_LOADED, the renderer is guaranteed to know about it.
1159 content::NotificationService::current()->Notify(
1160 chrome::NOTIFICATION_EXTENSION_LOADED,
1161 content::Source<Profile>(profile_),
1162 content::Details<const Extension>(extension));
1163
[email protected]c8d407e2011-04-28 21:27:171164 // Tell a random-ass collection of other subsystems about the new extension.
1165 // TODO(aa): What should we do with all this goop? Can it move into the
1166 // relevant objects via EXTENSION_LOADED?
1167
1168 profile_->GetExtensionSpecialStoragePolicy()->
1169 GrantRightsForExtension(extension);
1170
1171 UpdateActiveExtensionsInCrashReporter();
1172
[email protected]c8d407e2011-04-28 21:27:171173 // If the extension has permission to load chrome://favicon/ resources we need
1174 // to make sure that the FaviconSource is registered with the
1175 // ChromeURLDataManager.
[email protected]13c68b62013-05-17 11:29:051176 if (extensions::PermissionsData::HasHostPermission(
1177 extension, GURL(chrome::kChromeUIFaviconURL))) {
[email protected]c8d407e2011-04-28 21:27:171178 FaviconSource* favicon_source = new FaviconSource(profile_,
1179 FaviconSource::FAVICON);
[email protected]24ea7a12013-01-27 23:54:531180 content::URLDataSource::Add(profile_, favicon_source);
[email protected]c8d407e2011-04-28 21:27:171181 }
[email protected]b07e606e2012-09-15 20:16:151182
1183#if !defined(OS_ANDROID)
1184 // Same for chrome://theme/ resources.
[email protected]13c68b62013-05-17 11:29:051185 if (extensions::PermissionsData::HasHostPermission(
1186 extension, GURL(chrome::kChromeUIThemeURL))) {
[email protected]b07e606e2012-09-15 20:16:151187 ThemeSource* theme_source = new ThemeSource(profile_);
[email protected]24ea7a12013-01-27 23:54:531188 content::URLDataSource::Add(profile_, theme_source);
[email protected]b07e606e2012-09-15 20:16:151189 }
1190#endif
1191
[email protected]5eddc3e2011-10-26 04:33:311192 // Same for chrome://thumb/ resources.
[email protected]13c68b62013-05-17 11:29:051193 if (extensions::PermissionsData::HasHostPermission(
1194 extension, GURL(chrome::kChromeUIThumbnailURL))) {
[email protected]420e6d92013-09-17 01:48:511195 ThumbnailSource* thumbnail_source = new ThumbnailSource(profile_, false);
[email protected]24ea7a12013-01-27 23:54:531196 content::URLDataSource::Add(profile_, thumbnail_source);
[email protected]5eddc3e2011-10-26 04:33:311197 }
[email protected]62d30f42009-10-01 22:36:061198}
1199
[email protected]a9f39a312010-12-23 22:14:271200void ExtensionService::NotifyExtensionUnloaded(
[email protected]814a7bf0f2011-08-13 05:30:591201 const Extension* extension,
[email protected]b0af4792013-10-23 09:12:131202 UnloadedExtensionInfo::Reason reason) {
[email protected]45f5b7d2014-01-22 23:47:131203 registry_->TriggerOnUnloaded(extension);
1204
[email protected]a9f39a312010-12-23 22:14:271205 UnloadedExtensionInfo details(extension, reason);
[email protected]ad50def52011-10-19 23:17:071206 content::NotificationService::current()->Notify(
[email protected]ec870532014-03-19 20:24:051207 chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
[email protected]6c2381d2011-10-19 02:52:531208 content::Source<Profile>(profile_),
1209 content::Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061210
[email protected]f3b1a082011-11-18 00:34:301211 for (content::RenderProcessHost::iterator i(
1212 content::RenderProcessHost::AllHostsIterator());
[email protected]77a6970c2011-04-23 16:58:561213 !i.IsAtEnd(); i.Advance()) {
[email protected]f3b1a082011-11-18 00:34:301214 content::RenderProcessHost* host = i.GetCurrentValue();
[email protected]9b62ecf2011-07-27 20:23:081215 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:301216 Profile::FromBrowserContext(host->GetBrowserContext());
[email protected]9b62ecf2011-07-27 20:23:081217 if (host_profile->GetOriginalProfile() == profile_->GetOriginalProfile())
[email protected]c8d407e2011-04-28 21:27:171218 host->Send(new ExtensionMsg_Unloaded(extension->id()));
[email protected]77a6970c2011-04-23 16:58:561219 }
1220
[email protected]31d8f5f22012-04-02 15:22:081221 system_->UnregisterExtensionWithRequestContexts(extension->id(), reason);
[email protected]c8d407e2011-04-28 21:27:171222 profile_->GetExtensionSpecialStoragePolicy()->
1223 RevokeRightsForExtension(extension);
1224
[email protected]b777b332011-04-16 04:01:081225#if defined(OS_CHROMEOS)
[email protected]ac66e452013-01-11 09:21:231226 // Revoke external file access for the extension from its file system context.
1227 // It is safe to access the extension's storage partition at this point. The
1228 // storage partition may get destroyed only after the extension gets unloaded.
[email protected]3a746ec2014-03-15 05:30:561229 GURL site =
1230 extensions::util::GetSiteForExtensionId(extension->id(), profile_);
[email protected]10eb28162012-09-18 03:04:091231 fileapi::FileSystemContext* filesystem_context =
[email protected]ac66e452013-01-11 09:21:231232 BrowserContext::GetStoragePartitionForSite(profile_, site)->
[email protected]10eb28162012-09-18 03:04:091233 GetFileSystemContext();
[email protected]f19bbf62013-07-09 01:22:321234 if (filesystem_context && filesystem_context->external_backend()) {
1235 filesystem_context->external_backend()->
[email protected]c8d407e2011-04-28 21:27:171236 RevokeAccessForExtension(extension->id());
[email protected]62d30f42009-10-01 22:36:061237 }
[email protected]c8d407e2011-04-28 21:27:171238#endif
1239
1240 UpdateActiveExtensionsInCrashReporter();
[email protected]62d30f42009-10-01 22:36:061241}
1242
[email protected]0a071a32011-02-08 00:18:241243Profile* ExtensionService::profile() {
1244 return profile_;
1245}
1246
[email protected]599539802014-01-07 23:06:001247content::BrowserContext* ExtensionService::GetBrowserContext() const {
1248 // Implemented in the .cc file to avoid adding a profile.h dependency to
1249 // extension_service.h.
1250 return profile_;
1251}
1252
[email protected]25ae0152011-11-18 14:40:021253bool ExtensionService::is_ready() {
[email protected]4a10006a2013-05-17 23:18:351254 return ready_->is_signaled();
[email protected]25ae0152011-11-18 14:40:021255}
1256
[email protected]7f8f24f2012-11-15 19:40:141257base::SequencedTaskRunner* ExtensionService::GetFileTaskRunner() {
[email protected]dc24976f2013-06-02 21:15:091258 if (file_task_runner_.get())
1259 return file_task_runner_.get();
[email protected]7f8f24f2012-11-15 19:40:141260
1261 // We should be able to interrupt any part of extension install process during
1262 // shutdown. SKIP_ON_SHUTDOWN ensures that not started extension install tasks
1263 // will be ignored/deleted while we will block on started tasks.
1264 std::string token("ext_install-");
1265 token.append(profile_->GetPath().AsUTF8Unsafe());
1266 file_task_runner_ = BrowserThread::GetBlockingPool()->
1267 GetSequencedTaskRunnerWithShutdownBehavior(
1268 BrowserThread::GetBlockingPool()->GetNamedSequenceToken(token),
1269 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
[email protected]dc24976f2013-06-02 21:15:091270 return file_task_runner_.get();
[email protected]7f8f24f2012-11-15 19:40:141271}
1272
[email protected]42a08162012-03-16 18:09:111273extensions::ExtensionUpdater* ExtensionService::updater() {
[email protected]2859946f2011-04-04 18:18:061274 return updater_.get();
1275}
1276
[email protected]4ee07c62012-08-21 12:40:421277void ExtensionService::CheckManagementPolicy() {
[email protected]0d54b682013-11-05 14:15:361278 std::vector<std::string> to_unload;
1279 std::map<std::string, Extension::DisableReason> to_disable;
[email protected]695b5712012-12-06 23:55:281280
[email protected]0d54b682013-11-05 14:15:361281 // Loop through the extensions list, finding extensions we need to unload or
1282 // disable.
[email protected]bb1bc9b32013-12-21 03:09:141283 const ExtensionSet& extensions = registry_->enabled_extensions();
1284 for (ExtensionSet::const_iterator iter = extensions.begin();
1285 iter != extensions.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361286 const Extension* extension = (iter->get());
[email protected]4ee07c62012-08-21 12:40:421287 if (!system_->management_policy()->UserMayLoad(extension, NULL))
[email protected]0d54b682013-11-05 14:15:361288 to_unload.push_back(extension->id());
1289 Extension::DisableReason disable_reason = Extension::DISABLE_NONE;
1290 if (system_->management_policy()->MustRemainDisabled(
1291 extension, &disable_reason, NULL))
1292 to_disable[extension->id()] = disable_reason;
[email protected]aa96d3a2010-08-21 08:45:251293 }
1294
[email protected]0d54b682013-11-05 14:15:361295 for (size_t i = 0; i < to_unload.size(); ++i)
1296 UnloadExtension(to_unload[i], UnloadedExtensionInfo::REASON_DISABLE);
1297
1298 for (std::map<std::string, Extension::DisableReason>::const_iterator i =
1299 to_disable.begin(); i != to_disable.end(); ++i)
1300 DisableExtension(i->first, i->second);
[email protected]aa96d3a2010-08-21 08:45:251301}
1302
[email protected]31206602011-04-13 23:07:321303void ExtensionService::CheckForUpdatesSoon() {
[email protected]c3cfb012011-04-06 22:07:351304 if (updater()) {
[email protected]94fde232012-04-27 10:22:301305 if (AreAllExternalProvidersReady()) {
1306 updater()->CheckSoon();
1307 } else {
1308 // Sync can start updating before all the external providers are ready
1309 // during startup. Start the update as soon as those providers are ready,
1310 // but not before.
1311 update_once_all_providers_are_ready_ = true;
1312 }
[email protected]c3cfb012011-04-06 22:07:351313 } else {
[email protected]31206602011-04-13 23:07:321314 LOG(WARNING) << "CheckForUpdatesSoon() called with auto-update turned off";
[email protected]c3cfb012011-04-06 22:07:351315 }
1316}
1317
[email protected]8e4560b62011-01-14 10:09:141318// Some extensions will autoupdate themselves externally from Chrome. These
1319// are typically part of some larger client application package. To support
1320// these, the extension will register its location in the the preferences file
1321// (and also, on Windows, in the registry) and this code will periodically
1322// check that location for a .crx file, which it will then install locally if
1323// a new version is available.
1324// Errors are reported through ExtensionErrorReporter. Succcess is not
1325// reported.
[email protected]eaa7dd182010-12-14 11:09:001326void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:121327 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]8e4560b62011-01-14 10:09:141328
1329 // Note that this installation is intentionally silent (since it didn't
1330 // go through the front-end). Extensions that are registered in this
1331 // way are effectively considered 'pre-bundled', and so implicitly
1332 // trusted. In general, if something has HKLM or filesystem access,
1333 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:121334
[email protected]8e4560b62011-01-14 10:09:141335 // Ask each external extension provider to give us a call back for each
1336 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
[email protected]5df038b2012-07-16 19:03:271337 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141338 for (i = external_extension_providers_.begin();
1339 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:271340 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:141341 provider->VisitRegisteredExtension();
1342 }
1343
[email protected]50067e52011-10-20 23:17:071344 // Do any required work that we would have done after completion of all
1345 // providers.
1346 if (external_extension_providers_.empty()) {
1347 OnAllExternalProvidersReady();
1348 }
[email protected]9f1087e2009-06-15 17:29:321349}
1350
[email protected]50067e52011-10-20 23:17:071351void ExtensionService::OnExternalProviderReady(
[email protected]5df038b2012-07-16 19:03:271352 const extensions::ExternalProviderInterface* provider) {
[email protected]a29a517a2011-01-21 21:11:121353 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]50067e52011-10-20 23:17:071354 CHECK(provider->IsReady());
[email protected]a29a517a2011-01-21 21:11:121355
1356 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:141357 // if all of them are finished. So we check them first.
[email protected]94fde232012-04-27 10:22:301358 if (AreAllExternalProvidersReady())
1359 OnAllExternalProvidersReady();
1360}
1361
1362bool ExtensionService::AreAllExternalProvidersReady() const {
[email protected]5df038b2012-07-16 19:03:271363 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141364 for (i = external_extension_providers_.begin();
1365 i != external_extension_providers_.end(); ++i) {
[email protected]94fde232012-04-27 10:22:301366 if (!i->get()->IsReady())
1367 return false;
[email protected]8e4560b62011-01-14 10:09:141368 }
[email protected]94fde232012-04-27 10:22:301369 return true;
[email protected]50067e52011-10-20 23:17:071370}
1371
1372void ExtensionService::OnAllExternalProvidersReady() {
1373 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]94fde232012-04-27 10:22:301374 base::TimeDelta elapsed = base::Time::Now() - profile_->GetStartTime();
1375 UMA_HISTOGRAM_TIMES("Extension.ExternalProvidersReadyAfter", elapsed);
[email protected]50067e52011-10-20 23:17:071376
1377 // Install any pending extensions.
[email protected]94fde232012-04-27 10:22:301378 if (update_once_all_providers_are_ready_ && updater()) {
1379 update_once_all_providers_are_ready_ = false;
[email protected]4e9b59d2013-12-18 06:47:141380 extensions::ExtensionUpdater::CheckParams params;
1381 params.callback = external_updates_finished_callback_;
1382 updater()->CheckNow(params);
[email protected]8e4560b62011-01-14 10:09:141383 }
1384
1385 // Uninstall all the unclaimed extensions.
[email protected]45759612012-07-10 17:21:231386 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> extensions_info(
[email protected]8e4560b62011-01-14 10:09:141387 extension_prefs_->GetInstalledExtensionsInfo());
1388 for (size_t i = 0; i < extensions_info->size(); ++i) {
1389 ExtensionInfo* info = extensions_info->at(i).get();
[email protected]1d5e58b2013-01-31 08:41:401390 if (Manifest::IsExternalLocation(info->extension_location))
[email protected]8e4560b62011-01-14 10:09:141391 CheckExternalUninstall(info->extension_id);
1392 }
[email protected]e5af875f2011-10-10 21:09:141393 IdentifyAlertableExtensions();
[email protected]e5af875f2011-10-10 21:09:141394}
1395
1396void ExtensionService::IdentifyAlertableExtensions() {
1397 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1398
[email protected]07c9f2f42012-02-29 18:45:221399 // Build up the lists of extensions that require acknowledgment. If this is
1400 // the first time, grandfather extensions that would have caused
1401 // notification.
[email protected]89226982012-07-16 20:09:181402 extension_error_ui_.reset(ExtensionErrorUI::Create(this));
[email protected]e5af875f2011-10-10 21:09:141403
[email protected]a9aa5932012-01-25 08:27:401404 bool did_show_alert = false;
[email protected]89226982012-07-16 20:09:181405 if (PopulateExtensionErrorUI(extension_error_ui_.get())) {
[email protected]460c6712013-04-24 07:20:011406 if (!is_first_run_) {
[email protected]a9aa5932012-01-25 08:27:401407 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]89226982012-07-16 20:09:181408 did_show_alert = extension_error_ui_->ShowErrorInBubbleView();
[email protected]e5af875f2011-10-10 21:09:141409 } else {
1410 // First run. Just acknowledge all the extensions, silently, by
1411 // shortcutting the display of the UI and going straight to the
1412 // callback for pressing the Accept button.
[email protected]a9aa5932012-01-25 08:27:401413 HandleExtensionAlertAccept();
[email protected]e5af875f2011-10-10 21:09:141414 }
1415 }
[email protected]a9aa5932012-01-25 08:27:401416
[email protected]612a1cb12012-10-17 13:18:031417 UpdateExternalExtensionAlert();
1418
[email protected]a9aa5932012-01-25 08:27:401419 if (!did_show_alert)
[email protected]89226982012-07-16 20:09:181420 extension_error_ui_.reset();
[email protected]e5af875f2011-10-10 21:09:141421}
1422
[email protected]89226982012-07-16 20:09:181423bool ExtensionService::PopulateExtensionErrorUI(
1424 ExtensionErrorUI* extension_error_ui) {
[email protected]07c9f2f42012-02-29 18:45:221425 bool needs_alert = false;
[email protected]695b5712012-12-06 23:55:281426
1427 // Extensions that are blacklisted.
[email protected]bb1bc9b32013-12-21 03:09:141428 const ExtensionSet& blacklisted_set = registry_->blacklisted_extensions();
1429 for (ExtensionSet::const_iterator it = blacklisted_set.begin();
1430 it != blacklisted_set.end(); ++it) {
[email protected]695b5712012-12-06 23:55:281431 std::string id = (*it)->id();
1432 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(id)) {
1433 extension_error_ui->AddBlacklistedExtension(id);
1434 needs_alert = true;
1435 }
1436 }
1437
[email protected]bb1bc9b32013-12-21 03:09:141438 const ExtensionSet& enabled_set = registry_->enabled_extensions();
1439 for (ExtensionSet::const_iterator iter = enabled_set.begin();
1440 iter != enabled_set.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361441 const Extension* e = iter->get();
[email protected]695b5712012-12-06 23:55:281442
[email protected]870f5572013-12-19 12:30:151443 // Skip for extensions that have pending updates. They will be checked again
1444 // once the pending update is finished.
1445 if (pending_extension_manager()->IsIdPending(e->id()))
1446 continue;
1447
[email protected]695b5712012-12-06 23:55:281448 // Extensions disabled by policy. Note: this no longer includes blacklisted
1449 // extensions, though we still show the same UI.
[email protected]fdd679b2012-11-15 20:49:391450 if (!system_->management_policy()->UserMayLoad(e, NULL)) {
[email protected]07c9f2f42012-02-29 18:45:221451 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(e->id())) {
[email protected]89226982012-07-16 20:09:181452 extension_error_ui->AddBlacklistedExtension(e->id());
[email protected]07c9f2f42012-02-29 18:45:221453 needs_alert = true;
1454 }
1455 }
[email protected]07c9f2f42012-02-29 18:45:221456 }
[email protected]695b5712012-12-06 23:55:281457
[email protected]07c9f2f42012-02-29 18:45:221458 return needs_alert;
1459}
1460
[email protected]a9aa5932012-01-25 08:27:401461void ExtensionService::HandleExtensionAlertClosed() {
[email protected]612a1cb12012-10-17 13:18:031462 const ExtensionIdSet* extension_ids =
1463 extension_error_ui_->get_blacklisted_extension_ids();
[email protected]e5af875f2011-10-10 21:09:141464 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1465 iter != extension_ids->end(); ++iter) {
1466 extension_prefs_->AcknowledgeBlacklistedExtension(*iter);
1467 }
[email protected]879a6092013-03-06 04:12:501468 extension_error_ui_.reset();
1469}
1470
1471void ExtensionService::HandleExtensionAlertAccept() {
[email protected]73a85e012013-04-04 10:45:301472 extension_error_ui_->Close();
[email protected]e5af875f2011-10-10 21:09:141473}
1474
[email protected]47fc70c2011-12-06 07:29:511475void ExtensionService::AcknowledgeExternalExtension(const std::string& id) {
1476 extension_prefs_->AcknowledgeExternalExtension(id);
[email protected]612a1cb12012-10-17 13:18:031477 UpdateExternalExtensionAlert();
[email protected]47fc70c2011-12-06 07:29:511478}
1479
[email protected]d96eb512012-11-01 23:44:081480bool ExtensionService::IsUnacknowledgedExternalExtension(
1481 const Extension* extension) {
[email protected]b82d18da2012-11-09 00:11:321482 if (!FeatureSwitch::prompt_for_external_extensions()->IsEnabled())
1483 return false;
1484
[email protected]1d5e58b2013-01-31 08:41:401485 return (Manifest::IsExternalLocation(extension->location()) &&
[email protected]d96eb512012-11-01 23:44:081486 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id()) &&
1487 !(extension_prefs_->GetDisableReasons(extension->id()) &
1488 Extension::DISABLE_SIDELOAD_WIPEOUT));
1489}
1490
[email protected]59ee99d2013-10-11 15:46:161491void ExtensionService::ReconcileKnownDisabled() {
[email protected]60a8bc962013-12-03 03:17:271492 ExtensionIdSet known_disabled_ids;
1493 if (!extension_prefs_->GetKnownDisabled(&known_disabled_ids)) {
[email protected]bb1bc9b32013-12-21 03:09:141494 extension_prefs_->SetKnownDisabled(
1495 registry_->disabled_extensions().GetIDs());
[email protected]018a5682013-11-20 02:41:211496 UMA_HISTOGRAM_BOOLEAN("Extensions.KnownDisabledInitialized", true);
1497 return;
[email protected]59ee99d2013-10-11 15:46:161498 }
[email protected]195c1582013-10-29 18:42:231499
[email protected]bb1bc9b32013-12-21 03:09:141500 // Both |known_disabled_ids| and |extensions| are ordered (by definition
[email protected]018a5682013-11-20 02:41:211501 // of std::map and std::set). Iterate forward over both sets in parallel
1502 // to find matching IDs and disable the corresponding extensions.
[email protected]bb1bc9b32013-12-21 03:09:141503 const ExtensionSet& enabled_set = registry_->enabled_extensions();
1504 ExtensionSet::const_iterator extensions_it = enabled_set.begin();
[email protected]018a5682013-11-20 02:41:211505 ExtensionIdSet::const_iterator known_disabled_ids_it =
1506 known_disabled_ids.begin();
1507 int known_disabled_count = 0;
[email protected]bb1bc9b32013-12-21 03:09:141508 while (extensions_it != enabled_set.end() &&
[email protected]018a5682013-11-20 02:41:211509 known_disabled_ids_it != known_disabled_ids.end()) {
1510 const std::string& extension_id = extensions_it->get()->id();
1511 const int comparison = extension_id.compare(*known_disabled_ids_it);
1512 if (comparison < 0) {
1513 ++extensions_it;
1514 } else if (comparison > 0) {
1515 ++known_disabled_ids_it;
1516 } else {
1517 ++known_disabled_count;
1518 // Advance |extensions_it| immediately as it will be invalidated upon
1519 // disabling the extension it points to.
1520 ++extensions_it;
1521 ++known_disabled_ids_it;
1522 DisableExtension(extension_id, Extension::DISABLE_KNOWN_DISABLED);
1523 }
1524 }
1525 UMA_HISTOGRAM_COUNTS_100("Extensions.KnownDisabledReDisabled",
1526 known_disabled_count);
1527
[email protected]195c1582013-10-29 18:42:231528 // Update the list of known disabled to reflect every change to
1529 // |disabled_extensions_| from this point forward.
[email protected]bb1bc9b32013-12-21 03:09:141530 registry_->SetDisabledModificationCallback(
[email protected]195c1582013-10-29 18:42:231531 base::Bind(&extensions::ExtensionPrefs::SetKnownDisabled,
1532 base::Unretained(extension_prefs_)));
[email protected]59ee99d2013-10-11 15:46:161533}
1534
[email protected]89226982012-07-16 20:09:181535void ExtensionService::HandleExtensionAlertDetails() {
1536 extension_error_ui_->ShowExtensions();
[email protected]73a85e012013-04-04 10:45:301537 // ShowExtensions may cause the error UI to close synchronously, e.g. if it
1538 // causes a navigation.
1539 if (extension_error_ui_)
1540 extension_error_ui_->Close();
[email protected]6c751e72010-11-23 10:11:101541}
1542
[email protected]612a1cb12012-10-17 13:18:031543void ExtensionService::UpdateExternalExtensionAlert() {
[email protected]41070e8d2012-10-24 01:34:361544 if (!FeatureSwitch::prompt_for_external_extensions()->IsEnabled())
[email protected]612a1cb12012-10-17 13:18:031545 return;
1546
1547 const Extension* extension = NULL;
[email protected]bb1bc9b32013-12-21 03:09:141548 const ExtensionSet& disabled_extensions = registry_->disabled_extensions();
1549 for (ExtensionSet::const_iterator iter = disabled_extensions.begin();
1550 iter != disabled_extensions.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361551 const Extension* e = iter->get();
[email protected]d96eb512012-11-01 23:44:081552 if (IsUnacknowledgedExternalExtension(e)) {
1553 extension = e;
1554 break;
[email protected]612a1cb12012-10-17 13:18:031555 }
1556 }
1557
1558 if (extension) {
[email protected]d96eb512012-11-01 23:44:081559 if (!extensions::HasExternalInstallError(this)) {
1560 if (extension_prefs_->IncrementAcknowledgePromptCount(extension->id()) >
[email protected]612a1cb12012-10-17 13:18:031561 kMaxExtensionAcknowledgePromptCount) {
[email protected]d96eb512012-11-01 23:44:081562 // Stop prompting for this extension, and check if there's another
1563 // one that needs prompting.
[email protected]612a1cb12012-10-17 13:18:031564 extension_prefs_->AcknowledgeExternalExtension(extension->id());
[email protected]d96eb512012-11-01 23:44:081565 UpdateExternalExtensionAlert();
1566 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
1567 EXTERNAL_EXTENSION_IGNORED,
1568 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:021569 if (extensions::ManifestURL::UpdatesFromGallery(extension)) {
1570 UMA_HISTOGRAM_ENUMERATION(
1571 "Extensions.ExternalExtensionEventWebstore",
1572 EXTERNAL_EXTENSION_IGNORED,
1573 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
1574 } else {
1575 UMA_HISTOGRAM_ENUMERATION(
1576 "Extensions.ExternalExtensionEventNonWebstore",
1577 EXTERNAL_EXTENSION_IGNORED,
1578 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
1579 }
[email protected]d96eb512012-11-01 23:44:081580 return;
[email protected]612a1cb12012-10-17 13:18:031581 }
[email protected]b3aa7182013-04-25 04:45:231582 if (is_first_run_)
1583 extension_prefs_->SetExternalInstallFirstRun(extension->id());
1584 // first_run is true if the extension was installed during a first run
1585 // (even if it's post-first run now).
1586 bool first_run = extension_prefs_->IsExternalInstallFirstRun(
1587 extension->id());
1588 extensions::AddExternalInstallError(this, extension, first_run);
[email protected]612a1cb12012-10-17 13:18:031589 }
1590 } else {
1591 extensions::RemoveExternalInstallError(this);
1592 }
[email protected]612a1cb12012-10-17 13:18:031593}
1594
[email protected]a9f39a312010-12-23 22:14:271595void ExtensionService::UnloadExtension(
1596 const std::string& extension_id,
[email protected]b0af4792013-10-23 09:12:131597 UnloadedExtensionInfo::Reason reason) {
[email protected]27e469a2010-01-11 20:35:091598 // Make sure the extension gets deleted after we return from this function.
[email protected]599539802014-01-07 23:06:001599 int include_mask =
1600 ExtensionRegistry::EVERYTHING & ~ExtensionRegistry::TERMINATED;
[email protected]9adb9692010-10-29 23:14:021601 scoped_refptr<const Extension> extension(
[email protected]599539802014-01-07 23:06:001602 registry_->GetExtensionById(extension_id, include_mask));
[email protected]631cf822009-05-15 07:01:251603
[email protected]fa0c96732010-11-17 00:14:231604 // This method can be called via PostTask, so the extension may have been
1605 // unloaded by the time this runs.
[email protected]dc24976f2013-06-02 21:15:091606 if (!extension.get()) {
[email protected]dd163fb02011-05-04 22:22:171607 // In case the extension may have crashed/uninstalled. Allow the profile to
1608 // clean up its RequestContexts.
[email protected]31d8f5f22012-04-02 15:22:081609 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]fa0c96732010-11-17 00:14:231610 return;
[email protected]dd163fb02011-05-04 22:22:171611 }
[email protected]0c6da502009-08-14 22:32:391612
[email protected]1eb175082010-02-10 09:26:161613 // Keep information about the extension so that we can reload it later
1614 // even if it's not permanently installed.
1615 unloaded_extension_paths_[extension->id()] = extension->path();
1616
[email protected]f17dbd42010-08-16 23:21:101617 // Clean up if the extension is meant to be enabled after a reload.
[email protected]b914e2952013-04-26 07:10:031618 reloading_extensions_.erase(extension->id());
[email protected]f17dbd42010-08-16 23:21:101619
[email protected]bb1bc9b32013-12-21 03:09:141620 if (registry_->disabled_extensions().Contains(extension->id())) {
1621 registry_->RemoveDisabled(extension->id());
[email protected]dd163fb02011-05-04 22:22:171622 // Make sure the profile cleans up its RequestContexts when an already
1623 // disabled extension is unloaded (since they are also tracking the disabled
1624 // extensions).
[email protected]31d8f5f22012-04-02 15:22:081625 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]45f5b7d2014-01-22 23:47:131626 // Don't send the unloaded notification. It was sent when the extension
1627 // was disabled.
[email protected]60a174a2013-08-02 20:29:281628 } else {
[email protected]bb1bc9b32013-12-21 03:09:141629 // Remove the extension from the enabled list.
1630 registry_->RemoveEnabled(extension->id());
[email protected]60a174a2013-08-02 20:29:281631 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]0c6da502009-08-14 22:32:391632 }
1633
[email protected]fb789532013-08-27 02:40:211634 content::NotificationService::current()->Notify(
1635 chrome::NOTIFICATION_EXTENSION_REMOVED,
1636 content::Source<Profile>(profile_),
1637 content::Details<const Extension>(extension.get()));
[email protected]631cf822009-05-15 07:01:251638}
1639
[email protected]8b1ec202013-09-05 02:09:501640void ExtensionService::RemoveComponentExtension(
1641 const std::string& extension_id) {
1642 scoped_refptr<const Extension> extension(
1643 GetExtensionById(extension_id, false));
[email protected]b0af4792013-10-23 09:12:131644 UnloadExtension(extension_id, UnloadedExtensionInfo::REASON_UNINSTALL);
[email protected]8b1ec202013-09-05 02:09:501645 content::NotificationService::current()->Notify(
1646 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
1647 content::Source<Profile>(profile_),
1648 content::Details<const Extension>(extension.get()));
1649}
1650
[email protected]bb1bc9b32013-12-21 03:09:141651void ExtensionService::UnloadAllExtensionsForTest() {
1652 UnloadAllExtensionsInternal();
[email protected]9f1087e2009-06-15 17:29:321653}
1654
[email protected]bb1bc9b32013-12-21 03:09:141655void ExtensionService::ReloadExtensionsForTest() {
1656 // Calling UnloadAllExtensionsForTest here triggers a false-positive presubmit
1657 // warning about calling test code in production.
1658 UnloadAllExtensionsInternal();
[email protected]eac88332012-12-26 17:57:451659 component_loader_->LoadAll();
[email protected]d8c8f25f2011-11-02 18:18:011660 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]4a10006a2013-05-17 23:18:351661 // Don't call SetReadyAndNotifyListeners() since tests call this multiple
1662 // times.
[email protected]9f1087e2009-06-15 17:29:321663}
1664
[email protected]eaa7dd182010-12-14 11:09:001665void ExtensionService::GarbageCollectExtensions() {
[email protected]c04fd3dd2013-09-05 08:20:101666#if defined(OS_CHROMEOS)
1667 if (disable_garbage_collection_)
1668 return;
1669#endif
1670
[email protected]2f8757c32012-06-19 19:17:471671 if (extension_prefs_->pref_service()->ReadOnly())
1672 return;
1673
[email protected]e8107c12014-01-14 15:38:011674 bool clean_temp_dir = true;
1675
[email protected]9bd9a6862012-11-29 09:24:221676 if (pending_extension_manager()->HasPendingExtensions()) {
[email protected]e8107c12014-01-14 15:38:011677 // Don't garbage collect temp dir while there are pending installations,
1678 // which may be using the temporary installation directory. Try to garbage
1679 // collect again later.
1680 clean_temp_dir = false;
[email protected]b3a25092013-05-28 22:08:161681 base::MessageLoop::current()->PostDelayedTask(
[email protected]9bd9a6862012-11-29 09:24:221682 FROM_HERE,
1683 base::Bind(&ExtensionService::GarbageCollectExtensions, AsWeakPtr()),
1684 base::TimeDelta::FromSeconds(kGarbageCollectRetryDelay));
[email protected]9bd9a6862012-11-29 09:24:221685 }
1686
[email protected]45759612012-07-10 17:21:231687 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> info(
[email protected]2f8757c32012-06-19 19:17:471688 extension_prefs_->GetInstalledExtensionsInfo());
1689
[email protected]650b2d52013-02-10 03:41:451690 std::multimap<std::string, base::FilePath> extension_paths;
[email protected]2f8757c32012-06-19 19:17:471691 for (size_t i = 0; i < info->size(); ++i)
[email protected]0db124b02012-11-07 04:55:051692 extension_paths.insert(std::make_pair(info->at(i)->extension_id,
1693 info->at(i)->extension_path));
1694
[email protected]399583b2012-12-11 09:33:421695 info = extension_prefs_->GetAllDelayedInstallInfo();
[email protected]0db124b02012-11-07 04:55:051696 for (size_t i = 0; i < info->size(); ++i)
1697 extension_paths.insert(std::make_pair(info->at(i)->extension_id,
1698 info->at(i)->extension_path));
[email protected]2f8757c32012-06-19 19:17:471699
[email protected]7f8f24f2012-11-15 19:40:141700 if (!GetFileTaskRunner()->PostTask(
1701 FROM_HERE,
[email protected]2f8757c32012-06-19 19:17:471702 base::Bind(
1703 &extension_file_util::GarbageCollectExtensions,
1704 install_directory_,
[email protected]e8107c12014-01-14 15:38:011705 extension_paths,
1706 clean_temp_dir))) {
[email protected]2f8757c32012-06-19 19:17:471707 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:401708 }
[email protected]3cf4f0992009-02-03 23:00:301709}
1710
[email protected]820d9bd2013-04-03 03:46:031711void ExtensionService::SetReadyAndNotifyListeners() {
[email protected]4a10006a2013-05-17 23:18:351712 ready_->Signal();
[email protected]820d9bd2013-04-03 03:46:031713 content::NotificationService::current()->Notify(
1714 chrome::NOTIFICATION_EXTENSIONS_READY,
1715 content::Source<Profile>(profile_),
1716 content::NotificationService::NoDetails());
1717}
1718
[email protected]eaa7dd182010-12-14 11:09:001719void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]3eeddd892013-04-17 17:00:111720 if (updater_)
[email protected]93fd78f42009-07-10 16:43:171721 updater_->Start();
[email protected]fa6a9102010-11-22 15:38:501722
[email protected]bc151cf92013-02-12 04:57:261723 OnBlacklistUpdated();
[email protected]e72e8eb82009-06-18 17:21:511724}
1725
[email protected]fcb58a862012-05-01 01:03:151726void ExtensionService::AddExtension(const Extension* extension) {
[email protected]c8d407e2011-04-28 21:27:171727 // TODO(jstritar): We may be able to get rid of this branch by overriding the
1728 // default extension state to DISABLED when the --disable-extensions flag
1729 // is set (http://crbug.com/29067).
1730 if (!extensions_enabled() &&
1731 !extension->is_theme() &&
[email protected]1d5e58b2013-01-31 08:41:401732 extension->location() != Manifest::COMPONENT &&
1733 !Manifest::IsExternalLocation(extension->location())) {
[email protected]fcb58a862012-05-01 01:03:151734 return;
[email protected]330840c2012-08-29 22:21:011735 }
[email protected]c8d407e2011-04-28 21:27:171736
[email protected]b914e2952013-04-26 07:10:031737 bool is_extension_upgrade = false;
[email protected]116d40e2013-08-08 17:23:171738 bool is_extension_installed = false;
1739 const Extension* old = GetInstalledExtension(extension->id());
1740 if (old) {
1741 is_extension_installed = true;
1742 int version_compare_result =
1743 extension->version()->CompareTo(*(old->version()));
1744 is_extension_upgrade = version_compare_result > 0;
[email protected]b914e2952013-04-26 07:10:031745 // Other than for unpacked extensions, CrxInstaller should have guaranteed
1746 // that we aren't downgrading.
1747 if (!Manifest::IsUnpackedLocation(extension->location()))
[email protected]116d40e2013-08-08 17:23:171748 CHECK_GE(version_compare_result, 0);
[email protected]b914e2952013-04-26 07:10:031749 }
[email protected]45f5b7d2014-01-22 23:47:131750 system_->runtime_data()->SetBeingUpgraded(extension, is_extension_upgrade);
[email protected]c8d407e2011-04-28 21:27:171751
[email protected]1eb175082010-02-10 09:26:161752 // The extension is now loaded, remove its data from unloaded extension map.
1753 unloaded_extension_paths_.erase(extension->id());
1754
[email protected]bb7f40952011-01-13 00:21:201755 // If a terminated extension is loaded, remove it from the terminated list.
1756 UntrackTerminatedExtension(extension->id());
1757
[email protected]f17dbd42010-08-16 23:21:101758 // If the extension was disabled for a reload, then enable it.
[email protected]ca2e82022013-04-29 16:48:411759 bool reloading = reloading_extensions_.erase(extension->id()) > 0;
[email protected]f17dbd42010-08-16 23:21:101760
[email protected]b914e2952013-04-26 07:10:031761 // Check if the extension's privileges have changed and mark the
1762 // extension disabled if necessary.
[email protected]116d40e2013-08-08 17:23:171763 CheckPermissionsIncrease(extension, is_extension_installed);
[email protected]b914e2952013-04-26 07:10:031764
[email protected]116d40e2013-08-08 17:23:171765 if (is_extension_installed && !reloading) {
[email protected]ca2e82022013-04-29 16:48:411766 // To upgrade an extension in place, unload the old one and then load the
1767 // new one. ReloadExtension disables the extension, which is sufficient.
[email protected]b0af4792013-10-23 09:12:131768 UnloadExtension(extension->id(), UnloadedExtensionInfo::REASON_UPDATE);
[email protected]b914e2952013-04-26 07:10:031769 }
[email protected]0c6da502009-08-14 22:32:391770
[email protected]695b5712012-12-06 23:55:281771 if (extension_prefs_->IsExtensionBlacklisted(extension->id())) {
[email protected]ac875372013-02-28 04:36:091772 // Only prefs is checked for the blacklist. We rely on callers to check the
1773 // blacklist before calling into here, e.g. CrxInstaller checks before
[email protected]9f3c8532013-07-31 19:52:071774 // installation then threads through the install and pending install flow
1775 // of this class, and we check when loading installed extensions.
[email protected]bb1bc9b32013-12-21 03:09:141776 registry_->AddBlacklisted(extension);
[email protected]ca2e82022013-04-29 16:48:411777 } else if (!reloading &&
1778 extension_prefs_->IsExtensionDisabled(extension->id())) {
[email protected]bb1bc9b32013-12-21 03:09:141779 registry_->AddDisabled(extension);
[email protected]f8aefb132013-10-30 09:29:521780 if (extension_sync_service_)
1781 extension_sync_service_->SyncExtensionChangeIfNeeded(*extension);
[email protected]44d62b62012-04-11 00:06:031782 content::NotificationService::current()->Notify(
1783 chrome::NOTIFICATION_EXTENSION_UPDATE_DISABLED,
1784 content::Source<Profile>(profile_),
1785 content::Details<const Extension>(extension));
[email protected]62f051c2012-03-29 17:04:441786
[email protected]7e9e2422012-12-13 19:54:191787 // Show the extension disabled error if a permissions increase was the
1788 // only reason it was disabled.
1789 if (extension_prefs_->GetDisableReasons(extension->id()) ==
[email protected]44d62b62012-04-11 00:06:031790 Extension::DISABLE_PERMISSIONS_INCREASE) {
[email protected]62f051c2012-03-29 17:04:441791 extensions::AddExtensionDisabledError(this, extension);
1792 }
[email protected]ca2e82022013-04-29 16:48:411793 } else if (reloading) {
1794 // Replace the old extension with the new version.
[email protected]bb1bc9b32013-12-21 03:09:141795 CHECK(!registry_->AddDisabled(extension));
[email protected]ca2e82022013-04-29 16:48:411796 EnableExtension(extension->id());
[email protected]695b5712012-12-06 23:55:281797 } else {
1798 // All apps that are displayed in the launcher are ordered by their ordinals
1799 // so we must ensure they have valid ordinals.
1800 if (extension->RequiresSortOrdinal()) {
[email protected]bd36b652013-02-27 02:13:251801 if (!extension->ShouldDisplayInNewTabPage()) {
[email protected]74474042013-11-21 12:03:541802 extension_prefs_->app_sorting()->MarkExtensionAsHidden(extension->id());
[email protected]bd36b652013-02-27 02:13:251803 }
[email protected]74474042013-11-21 12:03:541804 extension_prefs_->app_sorting()->EnsureValidOrdinals(
[email protected]695b5712012-12-06 23:55:281805 extension->id(), syncer::StringOrdinal());
1806 }
1807
[email protected]bb1bc9b32013-12-21 03:09:141808 registry_->AddEnabled(extension);
[email protected]f8aefb132013-10-30 09:29:521809 if (extension_sync_service_)
1810 extension_sync_service_->SyncExtensionChangeIfNeeded(*extension);
[email protected]695b5712012-12-06 23:55:281811 NotifyExtensionLoaded(extension);
[email protected]f574c402012-12-04 23:20:311812 }
[email protected]45f5b7d2014-01-22 23:47:131813 system_->runtime_data()->SetBeingUpgraded(extension, false);
[email protected]aab98a52009-12-02 03:22:351814}
1815
[email protected]8c484b742012-11-29 06:05:361816void ExtensionService::AddComponentExtension(const Extension* extension) {
1817 const std::string old_version_string(
1818 extension_prefs_->GetVersionString(extension->id()));
[email protected]c5e4a2222014-01-03 16:06:131819 const Version old_version(old_version_string);
[email protected]8c484b742012-11-29 06:05:361820
[email protected]0181a7d2013-11-12 01:08:421821 VLOG(1) << "AddComponentExtension " << extension->name();
[email protected]8c484b742012-11-29 06:05:361822 if (!old_version.IsValid() || !old_version.Equals(*extension->version())) {
1823 VLOG(1) << "Component extension " << extension->name() << " ("
1824 << extension->id() << ") installing/upgrading from '"
1825 << old_version_string << "' to " << extension->version()->GetString();
1826
1827 AddNewOrUpdatedExtension(extension,
[email protected]399583b2012-12-11 09:33:421828 Extension::ENABLED_COMPONENT,
[email protected]8e289f0b2013-12-17 17:49:071829 extensions::NOT_BLACKLISTED,
[email protected]d8fd0fd2014-03-24 13:16:061830 syncer::StringOrdinal(),
1831 std::string());
[email protected]8c484b742012-11-29 06:05:361832 return;
1833 }
1834
1835 AddExtension(extension);
1836}
1837
[email protected]b914e2952013-04-26 07:10:031838void ExtensionService::UpdateActivePermissions(const Extension* extension) {
[email protected]902fd7b2011-07-27 18:42:311839 // If the extension has used the optional permissions API, it will have a
1840 // custom set of active permissions defined in the extension prefs. Here,
1841 // we update the extension's active permissions based on the prefs.
[email protected]c2e66e12012-06-27 06:27:061842 scoped_refptr<PermissionSet> active_permissions =
[email protected]7c82539c2014-02-19 06:09:171843 extension_prefs_->GetActivePermissions(extension->id());
[email protected]902fd7b2011-07-27 18:42:311844
[email protected]dc24976f2013-06-02 21:15:091845 if (active_permissions.get()) {
[email protected]902fd7b2011-07-27 18:42:311846 // We restrict the active permissions to be within the bounds defined in the
1847 // extension's manifest.
1848 // a) active permissions must be a subset of optional + default permissions
1849 // b) active permissions must contains all default permissions
[email protected]c2e66e12012-06-27 06:27:061850 scoped_refptr<PermissionSet> total_permissions =
1851 PermissionSet::CreateUnion(
[email protected]2cb5e302013-05-09 05:00:061852 extensions::PermissionsData::GetRequiredPermissions(extension),
1853 extensions::PermissionsData::GetOptionalPermissions(extension));
[email protected]902fd7b2011-07-27 18:42:311854
1855 // Make sure the active permissions contain no more than optional + default.
[email protected]c2e66e12012-06-27 06:27:061856 scoped_refptr<PermissionSet> adjusted_active =
1857 PermissionSet::CreateIntersection(
[email protected]902fd7b2011-07-27 18:42:311858 total_permissions.get(), active_permissions.get());
1859
1860 // Make sure the active permissions contain the default permissions.
[email protected]c2e66e12012-06-27 06:27:061861 adjusted_active = PermissionSet::CreateUnion(
[email protected]2cb5e302013-05-09 05:00:061862 extensions::PermissionsData::GetRequiredPermissions(extension),
1863 adjusted_active.get());
[email protected]902fd7b2011-07-27 18:42:311864
[email protected]c333e792012-01-06 16:57:391865 extensions::PermissionsUpdater perms_updater(profile());
[email protected]dc24976f2013-06-02 21:15:091866 perms_updater.UpdateActivePermissions(extension, adjusted_active.get());
[email protected]902fd7b2011-07-27 18:42:311867 }
[email protected]b914e2952013-04-26 07:10:031868}
1869
1870void ExtensionService::CheckPermissionsIncrease(const Extension* extension,
[email protected]116d40e2013-08-08 17:23:171871 bool is_extension_installed) {
[email protected]b914e2952013-04-26 07:10:031872 UpdateActivePermissions(extension);
[email protected]902fd7b2011-07-27 18:42:311873
[email protected]8d888c12010-11-30 00:00:251874 // We keep track of all permissions the user has granted each extension.
1875 // This allows extensions to gracefully support backwards compatibility
1876 // by including unknown permissions in their manifests. When the user
1877 // installs the extension, only the recognized permissions are recorded.
1878 // When the unknown permissions become recognized (e.g., through browser
1879 // upgrade), we can prompt the user to accept these new permissions.
1880 // Extensions can also silently upgrade to less permissions, and then
1881 // silently upgrade to a version that adds these permissions back.
1882 //
1883 // For example, pretend that Chrome 10 includes a permission "omnibox"
1884 // for an API that adds suggestions to the omnibox. An extension can
1885 // maintain backwards compatibility while still having "omnibox" in the
1886 // manifest. If a user installs the extension on Chrome 9, the browser
1887 // will record the permissions it recognized, not including "omnibox."
1888 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
1889 // will disable the extension and prompt the user to approve the increase
1890 // in privileges. The extension could then release a new version that
1891 // removes the "omnibox" permission. When the user upgrades, Chrome will
1892 // still remember that "omnibox" had been granted, so that if the
1893 // extension once again includes "omnibox" in an upgrade, the extension
1894 // can upgrade without requiring this user's approval.
[email protected]eb5e4f92012-08-15 23:33:281895 int disable_reasons = extension_prefs_->GetDisableReasons(extension->id());
[email protected]8d888c12010-11-30 00:00:251896
[email protected]211a3f32013-05-28 21:48:181897 bool auto_grant_permission =
[email protected]1b563df2013-10-30 16:49:361898 (!is_extension_installed && extension->was_installed_by_default()) ||
[email protected]45928b682013-11-28 08:20:271899 extensions::ExtensionsBrowserClient::Get()->IsRunningInForcedAppMode();
[email protected]b914e2952013-04-26 07:10:031900 // Silently grant all active permissions to default apps only on install.
1901 // After install they should behave like other apps.
[email protected]211a3f32013-05-28 21:48:181902 // Silently grant all active permissions to apps install in kiosk mode on both
1903 // install and update.
1904 if (auto_grant_permission)
[email protected]b914e2952013-04-26 07:10:031905 GrantPermissions(extension);
1906
1907 bool is_privilege_increase = false;
1908 // We only need to compare the granted permissions to the current permissions
1909 // if the extension is not allowed to silently increase its permissions.
[email protected]13c68b62013-05-17 11:29:051910 if (!extensions::PermissionsData::CanSilentlyIncreasePermissions(extension) &&
[email protected]211a3f32013-05-28 21:48:181911 !auto_grant_permission) {
[email protected]8d888c12010-11-30 00:00:251912 // Add all the recognized permissions if the granted permissions list
1913 // hasn't been initialized yet.
[email protected]c2e66e12012-06-27 06:27:061914 scoped_refptr<PermissionSet> granted_permissions =
[email protected]902fd7b2011-07-27 18:42:311915 extension_prefs_->GetGrantedPermissions(extension->id());
[email protected]0d3e4a22011-06-23 19:02:521916 CHECK(granted_permissions.get());
[email protected]8d888c12010-11-30 00:00:251917
1918 // Here, we check if an extension's privileges have increased in a manner
1919 // that requires the user's approval. This could occur because the browser
1920 // upgraded and recognized additional privileges, or an extension upgrades
1921 // to a version that requires additional privileges.
[email protected]c41003472013-10-19 15:37:251922 is_privilege_increase =
1923 extensions::PermissionMessageProvider::Get()->IsPrivilegeIncrease(
1924 granted_permissions,
1925 extension->GetActivePermissions().get(),
1926 extension->GetType());
[email protected]8d888c12010-11-30 00:00:251927 }
1928
[email protected]116d40e2013-08-08 17:23:171929 if (is_extension_installed) {
[email protected]44d62b62012-04-11 00:06:031930 // If the extension was already disabled, suppress any alerts for becoming
1931 // disabled on permissions increase.
[email protected]b914e2952013-04-26 07:10:031932 bool previously_disabled =
1933 extension_prefs_->IsExtensionDisabled(extension->id());
[email protected]7e9e2422012-12-13 19:54:191934 // Legacy disabled extensions do not have a disable reason. Infer that if
1935 // there was no permission increase, it was likely disabled by the user.
1936 if (previously_disabled && disable_reasons == Extension::DISABLE_NONE &&
[email protected]b914e2952013-04-26 07:10:031937 !extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
[email protected]7e9e2422012-12-13 19:54:191938 disable_reasons |= Extension::DISABLE_USER_ACTION;
[email protected]44d62b62012-04-11 00:06:031939 }
[email protected]7e9e2422012-12-13 19:54:191940 // Extensions that came to us disabled from sync need a similar inference,
1941 // except based on the new version's permissions.
1942 if (previously_disabled &&
1943 disable_reasons == Extension::DISABLE_UNKNOWN_FROM_SYNC) {
1944 // Remove the DISABLE_UNKNOWN_FROM_SYNC reason.
1945 extension_prefs_->ClearDisableReasons(extension->id());
1946 if (!is_privilege_increase)
1947 disable_reasons |= Extension::DISABLE_USER_ACTION;
1948 }
[email protected]b914e2952013-04-26 07:10:031949 disable_reasons &= ~Extension::DISABLE_UNKNOWN_FROM_SYNC;
[email protected]8d888c12010-11-30 00:00:251950 }
1951
1952 // Extension has changed permissions significantly. Disable it. A
1953 // notification should be sent by the caller.
1954 if (is_privilege_increase) {
[email protected]7e9e2422012-12-13 19:54:191955 disable_reasons |= Extension::DISABLE_PERMISSIONS_INCREASE;
[email protected]fe2dd7742011-04-19 22:52:491956 if (!extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
1957 RecordPermissionMessagesHistogram(
1958 extension, "Extensions.Permissions_AutoDisable");
1959 }
[email protected]195c1582013-10-29 18:42:231960 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
[email protected]8d888c12010-11-30 00:00:251961 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
[email protected]b914e2952013-04-26 07:10:031962 }
1963 if (disable_reasons != Extension::DISABLE_NONE) {
[email protected]eb5e4f92012-08-15 23:33:281964 extension_prefs_->AddDisableReason(
1965 extension->id(),
1966 static_cast<Extension::DisableReason>(disable_reasons));
[email protected]8d888c12010-11-30 00:00:251967 }
1968}
1969
[email protected]eaa7dd182010-12-14 11:09:001970void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:391971 std::set<std::string> extension_ids;
[email protected]bb1bc9b32013-12-21 03:09:141972 const ExtensionSet& extensions = registry_->enabled_extensions();
1973 for (ExtensionSet::const_iterator iter = extensions.begin();
1974 iter != extensions.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361975 const Extension* extension = iter->get();
[email protected]1d5e58b2013-01-31 08:41:401976 if (!extension->is_theme() && extension->location() != Manifest::COMPONENT)
[email protected]84df8332011-12-06 18:22:461977 extension_ids.insert(extension->id());
[email protected]aab98a52009-12-02 03:22:351978 }
1979
[email protected]f6431be82013-09-07 02:53:451980 crash_keys::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:251981}
1982
[email protected]669b2372013-10-17 15:04:581983ExtensionService::ImportStatus ExtensionService::CheckImports(
1984 const extensions::Extension* extension,
1985 std::list<SharedModuleInfo::ImportInfo>* missing_modules,
1986 std::list<SharedModuleInfo::ImportInfo>* outdated_modules) {
1987 DCHECK(extension);
1988 DCHECK(missing_modules && missing_modules->empty());
1989 DCHECK(outdated_modules && outdated_modules->empty());
[email protected]9f4e4f082013-06-21 07:11:191990 ImportStatus status = IMPORT_STATUS_OK;
[email protected]9f4e4f082013-06-21 07:11:191991 if (SharedModuleInfo::ImportsModules(extension)) {
1992 const std::vector<SharedModuleInfo::ImportInfo>& imports =
1993 SharedModuleInfo::GetImports(extension);
1994 std::vector<SharedModuleInfo::ImportInfo>::const_iterator i;
1995 for (i = imports.begin(); i != imports.end(); ++i) {
[email protected]c5e4a2222014-01-03 16:06:131996 Version version_required(i->minimum_version);
[email protected]9f4e4f082013-06-21 07:11:191997 const Extension* imported_module =
1998 GetExtensionById(i->extension_id, true);
1999 if (!imported_module) {
2000 if (extension->from_webstore()) {
2001 status = IMPORT_STATUS_UNSATISFIED;
[email protected]669b2372013-10-17 15:04:582002 missing_modules->push_back(*i);
[email protected]9f4e4f082013-06-21 07:11:192003 } else {
2004 return IMPORT_STATUS_UNRECOVERABLE;
2005 }
2006 } else if (!SharedModuleInfo::IsSharedModule(imported_module)) {
2007 return IMPORT_STATUS_UNRECOVERABLE;
2008 } else if (version_required.IsValid() &&
2009 imported_module->version()->CompareTo(version_required) < 0) {
2010 if (imported_module->from_webstore()) {
[email protected]669b2372013-10-17 15:04:582011 outdated_modules->push_back(*i);
[email protected]9f4e4f082013-06-21 07:11:192012 status = IMPORT_STATUS_UNSATISFIED;
2013 } else {
2014 return IMPORT_STATUS_UNRECOVERABLE;
2015 }
2016 }
2017 }
2018 }
[email protected]669b2372013-10-17 15:04:582019 return status;
2020}
2021
2022ExtensionService::ImportStatus ExtensionService::SatisfyImports(
2023 const Extension* extension) {
2024 std::list<SharedModuleInfo::ImportInfo> noinstalled;
2025 std::list<SharedModuleInfo::ImportInfo> outdated;
2026 ImportStatus status = CheckImports(extension, &noinstalled, &outdated);
2027 if (status == IMPORT_STATUS_UNRECOVERABLE)
2028 return status;
[email protected]9f4e4f082013-06-21 07:11:192029 if (status == IMPORT_STATUS_UNSATISFIED) {
[email protected]669b2372013-10-17 15:04:582030 std::list<SharedModuleInfo::ImportInfo>::const_iterator iter;
2031 for (iter = noinstalled.begin(); iter != noinstalled.end(); ++iter) {
[email protected]9f4e4f082013-06-21 07:11:192032 pending_extension_manager()->AddFromExtensionImport(
[email protected]669b2372013-10-17 15:04:582033 iter->extension_id,
[email protected]9f4e4f082013-06-21 07:11:192034 extension_urls::GetWebstoreUpdateUrl(),
2035 IsSharedModule);
2036 }
2037 CheckForUpdatesSoon();
2038 }
2039 return status;
2040}
2041
2042scoped_ptr<const ExtensionSet>
2043 ExtensionService::GetDependentExtensions(const Extension* extension) {
2044 scoped_ptr<ExtensionSet> dependents(new ExtensionSet());
2045 scoped_ptr<ExtensionSet> set_to_check(new ExtensionSet());
2046 if (SharedModuleInfo::IsSharedModule(extension)) {
[email protected]bb1bc9b32013-12-21 03:09:142047 set_to_check->InsertAll(registry_->disabled_extensions());
[email protected]9f4e4f082013-06-21 07:11:192048 set_to_check->InsertAll(delayed_installs_);
[email protected]bb1bc9b32013-12-21 03:09:142049 set_to_check->InsertAll(registry_->enabled_extensions());
[email protected]9f4e4f082013-06-21 07:11:192050 for (ExtensionSet::const_iterator iter = set_to_check->begin();
2051 iter != set_to_check->end(); ++iter) {
[email protected]198b5902013-06-27 10:36:112052 if (SharedModuleInfo::ImportsExtensionById(iter->get(),
2053 extension->id())) {
[email protected]9f4e4f082013-06-21 07:11:192054 dependents->Insert(*iter);
2055 }
2056 }
2057 }
2058 return dependents.PassAs<const ExtensionSet>();
2059}
2060
2061void ExtensionService::PruneSharedModulesOnUninstall(
2062 const Extension* extension) {
2063 if (SharedModuleInfo::ImportsModules(extension)) {
2064 const std::vector<SharedModuleInfo::ImportInfo>& imports =
2065 SharedModuleInfo::GetImports(extension);
2066 std::vector<SharedModuleInfo::ImportInfo>::const_iterator i;
2067 for (i = imports.begin(); i != imports.end(); ++i) {
2068 const Extension* imported_module =
2069 GetExtensionById(i->extension_id, true);
2070 if (imported_module && imported_module->from_webstore()) {
2071 scoped_ptr<const ExtensionSet> dependents =
2072 GetDependentExtensions(imported_module);
2073 if (dependents->size() == 0) {
[email protected]4c9201c42013-08-16 04:56:212074 UninstallExtension(i->extension_id, true, NULL);
[email protected]9f4e4f082013-06-21 07:11:192075 }
2076 }
2077 }
2078 }
2079}
2080
[email protected]8266d662011-07-12 21:53:262081void ExtensionService::OnExtensionInstalled(
[email protected]36a5c4c2011-12-14 16:34:502082 const Extension* extension,
[email protected]98270432012-09-11 20:51:242083 const syncer::StringOrdinal& page_ordinal,
[email protected]0db124b02012-11-07 04:55:052084 bool has_requirement_errors,
[email protected]8e289f0b2013-12-17 17:49:072085 extensions::BlacklistState blacklist_state,
[email protected]0db124b02012-11-07 04:55:052086 bool wait_for_idle) {
[email protected]a29a517a2011-01-21 21:11:122087 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:202088
[email protected]c3cfb012011-04-06 22:07:352089 const std::string& id = extension->id();
[email protected]612a1cb12012-10-17 13:18:032090 bool initial_enable = ShouldEnableOnInstall(extension);
[email protected]d8fd0fd2014-03-24 13:16:062091 std::string install_parameter;
[email protected]3f213ad2012-07-26 23:39:412092 const extensions::PendingExtensionInfo* pending_extension_info = NULL;
[email protected]51a3bf8b2012-06-08 22:53:062093 if ((pending_extension_info = pending_extension_manager()->GetById(id))) {
[email protected]8f3bcbd2013-06-05 08:42:402094 if (!pending_extension_info->ShouldAllowInstall(extension)) {
[email protected]51a3bf8b2012-06-08 22:53:062095 pending_extension_manager()->Remove(id);
[email protected]8ef78fd2010-08-19 17:14:322096
[email protected]31bb5ee62012-09-12 22:58:402097 LOG(WARNING) << "ShouldAllowInstall() returned false for "
2098 << id << " of type " << extension->GetType()
[email protected]65348062013-01-15 07:27:222099 << " and update URL "
2100 << extensions::ManifestURL::GetUpdateURL(extension).spec()
[email protected]31bb5ee62012-09-12 22:58:402101 << "; not installing";
[email protected]ae6cb3022011-04-04 20:11:542102
[email protected]4416c5a2010-06-26 01:28:572103 // Delete the extension directory since we're not going to
2104 // load it.
[email protected]7f8f24f2012-11-15 19:40:142105 if (!GetFileTaskRunner()->PostTask(
2106 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362107 base::Bind(&extension_file_util::DeleteFile,
[email protected]31bb5ee62012-09-12 22:58:402108 extension->path(), true))) {
[email protected]14908b72011-04-20 06:54:362109 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:402110 }
[email protected]4416c5a2010-06-26 01:28:572111 return;
2112 }
[email protected]51a3bf8b2012-06-08 22:53:062113
[email protected]d8fd0fd2014-03-24 13:16:062114 install_parameter = pending_extension_info->install_parameter();
[email protected]51a3bf8b2012-06-08 22:53:062115 pending_extension_manager()->Remove(id);
[email protected]4416c5a2010-06-26 01:28:572116 } else {
[email protected]c3cfb012011-04-06 22:07:352117 // We explicitly want to re-enable an uninstalled external
2118 // extension; if we're here, that means the user is manually
2119 // installing the extension.
[email protected]46f3e102014-03-25 01:22:452120 if (extension_prefs_->IsExternalExtensionUninstalled(id)) {
[email protected]6cc7dbae2011-04-29 21:18:332121 initial_enable = true;
2122 }
[email protected]aa142702010-03-26 01:26:332123 }
2124
[email protected]98270432012-09-11 20:51:242125 // Unsupported requirements overrides the management policy.
2126 if (has_requirement_errors) {
2127 initial_enable = false;
2128 extension_prefs_->AddDisableReason(
2129 id, Extension::DISABLE_UNSUPPORTED_REQUIREMENT);
2130 // If the extension was disabled because of unsupported requirements but
2131 // now supports all requirements after an update and there are not other
2132 // disable reasons, enable it.
2133 } else if (extension_prefs_->GetDisableReasons(id) ==
2134 Extension::DISABLE_UNSUPPORTED_REQUIREMENT) {
2135 initial_enable = true;
2136 extension_prefs_->ClearDisableReasons(id);
2137 }
2138
[email protected]8e289f0b2013-12-17 17:49:072139 if (blacklist_state == extensions::BLACKLISTED_MALWARE) {
[email protected]9f3c8532013-07-31 19:52:072140 // Installation of a blacklisted extension can happen from sync, policy,
2141 // etc, where to maintain consistency we need to install it, just never
2142 // load it (see AddExtension). Usually it should be the job of callers to
2143 // incercept blacklisted extension earlier (e.g. CrxInstaller, before even
2144 // showing the install dialogue).
[email protected]7c82539c2014-02-19 06:09:172145 extension_prefs_->AcknowledgeBlacklistedExtension(id);
[email protected]9f3c8532013-07-31 19:52:072146 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.SilentInstall",
2147 extension->location(),
2148 Manifest::NUM_LOCATIONS);
2149 }
2150
[email protected]695b5712012-12-06 23:55:282151 if (!GetInstalledExtension(extension->id())) {
[email protected]07533022011-06-27 20:42:552152 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
2153 extension->GetType(), 100);
[email protected]69084dc2012-11-17 07:39:302154 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallSource",
[email protected]1d5e58b2013-01-31 08:41:402155 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]07533022011-06-27 20:42:552156 RecordPermissionMessagesHistogram(
2157 extension, "Extensions.Permissions_Install");
[email protected]69084dc2012-11-17 07:39:302158 } else {
2159 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateType",
2160 extension->GetType(), 100);
2161 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateSource",
[email protected]1d5e58b2013-01-31 08:41:402162 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]07533022011-06-27 20:42:552163 }
2164
[email protected]a39921b42012-02-28 03:42:542165 // Certain extension locations are specific enough that we can
2166 // auto-acknowledge any extension that came from one of them.
[email protected]4eef4352014-02-12 02:21:362167 if (Manifest::IsPolicyLocation(extension->location()) ||
2168 extension->location() == Manifest::EXTERNAL_COMPONENT)
[email protected]a39921b42012-02-28 03:42:542169 AcknowledgeExternalExtension(extension->id());
[email protected]399583b2012-12-11 09:33:422170 const Extension::State initial_state =
2171 initial_enable ? Extension::ENABLED : Extension::DISABLED;
[email protected]0f9e6262013-11-05 23:49:192172 const bool blacklisted_for_malware =
[email protected]8e289f0b2013-12-17 17:49:072173 blacklist_state == extensions::BLACKLISTED_MALWARE;
[email protected]e7aa7b7e2012-11-27 04:51:222174 if (ShouldDelayExtensionUpdate(id, wait_for_idle)) {
[email protected]9f3c8532013-07-31 19:52:072175 extension_prefs_->SetDelayedInstallInfo(
2176 extension,
2177 initial_state,
[email protected]0f9e6262013-11-05 23:49:192178 blacklisted_for_malware,
[email protected]9f3c8532013-07-31 19:52:072179 extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE,
[email protected]d8fd0fd2014-03-24 13:16:062180 page_ordinal,
2181 install_parameter);
[email protected]0db124b02012-11-07 04:55:052182
2183 // Transfer ownership of |extension|.
[email protected]9f4e4f082013-06-21 07:11:192184 delayed_installs_.Insert(extension);
[email protected]0db124b02012-11-07 04:55:052185
[email protected]75bdcb872013-03-13 00:41:452186 // Notify observers that app update is available.
2187 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
[email protected]a9aa62b312013-11-29 05:35:062188 OnAppUpdateAvailable(extension));
[email protected]0db124b02012-11-07 04:55:052189 return;
2190 }
2191
[email protected]9f4e4f082013-06-21 07:11:192192 ImportStatus status = SatisfyImports(extension);
2193 if (installs_delayed_for_gc()) {
[email protected]9f3c8532013-07-31 19:52:072194 extension_prefs_->SetDelayedInstallInfo(
2195 extension,
2196 initial_state,
[email protected]0f9e6262013-11-05 23:49:192197 blacklisted_for_malware,
[email protected]9f3c8532013-07-31 19:52:072198 extensions::ExtensionPrefs::DELAY_REASON_GC,
[email protected]d8fd0fd2014-03-24 13:16:062199 page_ordinal,
2200 install_parameter);
[email protected]399583b2012-12-11 09:33:422201 delayed_installs_.Insert(extension);
[email protected]9f4e4f082013-06-21 07:11:192202 } else if (status != IMPORT_STATUS_OK) {
2203 if (status == IMPORT_STATUS_UNSATISFIED) {
[email protected]9f3c8532013-07-31 19:52:072204 extension_prefs_->SetDelayedInstallInfo(
2205 extension,
2206 initial_state,
[email protected]0f9e6262013-11-05 23:49:192207 blacklisted_for_malware,
[email protected]9f4e4f082013-06-21 07:11:192208 extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IMPORTS,
[email protected]d8fd0fd2014-03-24 13:16:062209 page_ordinal,
2210 install_parameter);
[email protected]9f4e4f082013-06-21 07:11:192211 delayed_installs_.Insert(extension);
2212 }
[email protected]399583b2012-12-11 09:33:422213 } else {
[email protected]9f3c8532013-07-31 19:52:072214 AddNewOrUpdatedExtension(extension,
2215 initial_state,
2216 blacklist_state,
[email protected]d8fd0fd2014-03-24 13:16:062217 page_ordinal,
2218 install_parameter);
[email protected]399583b2012-12-11 09:33:422219 }
[email protected]8c484b742012-11-29 06:05:362220}
2221
2222void ExtensionService::AddNewOrUpdatedExtension(
2223 const Extension* extension,
[email protected]399583b2012-12-11 09:33:422224 Extension::State initial_state,
[email protected]8e289f0b2013-12-17 17:49:072225 extensions::BlacklistState blacklist_state,
[email protected]d8fd0fd2014-03-24 13:16:062226 const syncer::StringOrdinal& page_ordinal,
2227 const std::string& install_parameter) {
[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]d8fd0fd2014-03-24 13:16:062234 page_ordinal,
2235 install_parameter);
[email protected]9e9c1d12013-07-31 01:58:122236 delayed_installs_.Remove(extension->id());
[email protected]88622932014-01-16 17:59:122237 if (InstallVerifier::NeedsVerification(*extension)) {
[email protected]d820bc52014-03-13 05:35:232238 system_->install_verifier()->Add(extension->id(),
2239 base::Bind(LogAddVerifiedSuccess));
[email protected]ffd2f79e2013-11-14 00:11:462240 }
[email protected]6f6101832012-11-27 22:10:482241 FinishInstallation(extension);
2242}
2243
2244void ExtensionService::MaybeFinishDelayedInstallation(
2245 const std::string& extension_id) {
[email protected]9f4e4f082013-06-21 07:11:192246 // Check if the extension already got installed.
2247 if (!delayed_installs_.Contains(extension_id))
[email protected]6f6101832012-11-27 22:10:482248 return;
[email protected]9f4e4f082013-06-21 07:11:192249 extensions::ExtensionPrefs::DelayReason reason =
2250 extension_prefs_->GetDelayedInstallReason(extension_id);
2251
2252 // Check if the extension is idle. DELAY_REASON_NONE is used for older
2253 // preferences files that will not have set this field but it was previously
2254 // only used for idle updates.
2255 if ((reason == extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE ||
2256 reason == extensions::ExtensionPrefs::DELAY_REASON_NONE) &&
[email protected]1d5cf4142014-01-24 18:25:222257 is_ready() && !extensions::util::IsExtensionIdle(extension_id, profile_))
[email protected]6f6101832012-11-27 22:10:482258 return;
2259
[email protected]9f4e4f082013-06-21 07:11:192260 const Extension* extension = delayed_installs_.GetByID(extension_id);
2261 if (reason == extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IMPORTS) {
2262 ImportStatus status = SatisfyImports(extension);
2263 if (status != IMPORT_STATUS_OK) {
2264 if (status == IMPORT_STATUS_UNRECOVERABLE) {
2265 delayed_installs_.Remove(extension_id);
2266 // Make sure no version of the extension is actually installed, (i.e.,
2267 // that this delayed install was not an update).
2268 CHECK(!extension_prefs_->GetInstalledExtensionInfo(extension_id).get());
2269 extension_prefs_->DeleteExtensionPrefs(extension_id);
2270 }
2271 return;
2272 }
2273 }
2274
[email protected]6f6101832012-11-27 22:10:482275 FinishDelayedInstallation(extension_id);
2276}
2277
2278void ExtensionService::FinishDelayedInstallation(
2279 const std::string& extension_id) {
2280 scoped_refptr<const Extension> extension(
2281 GetPendingExtensionUpdate(extension_id));
[email protected]dc24976f2013-06-02 21:15:092282 CHECK(extension.get());
[email protected]9f4e4f082013-06-21 07:11:192283 delayed_installs_.Remove(extension_id);
[email protected]6f6101832012-11-27 22:10:482284
[email protected]399583b2012-12-11 09:33:422285 if (!extension_prefs_->FinishDelayedInstallInfo(extension_id))
[email protected]6f6101832012-11-27 22:10:482286 NOTREACHED();
2287
[email protected]dc24976f2013-06-02 21:15:092288 FinishInstallation(extension.get());
[email protected]6f6101832012-11-27 22:10:482289}
2290
2291void ExtensionService::FinishInstallation(const Extension* extension) {
[email protected]e7554c3f2013-05-29 00:36:562292 const extensions::Extension* existing_extension =
2293 GetInstalledExtension(extension->id());
2294 bool is_update = false;
2295 std::string old_name;
2296 if (existing_extension) {
2297 is_update = true;
2298 old_name = existing_extension->name();
2299 }
2300 extensions::InstalledExtensionInfo details(extension, is_update, old_name);
[email protected]fcb58a862012-05-01 01:03:152301 content::NotificationService::current()->Notify(
2302 chrome::NOTIFICATION_EXTENSION_INSTALLED,
2303 content::Source<Profile>(profile_),
[email protected]41bb80bd2013-05-03 10:56:022304 content::Details<const extensions::InstalledExtensionInfo>(&details));
[email protected]fcb58a862012-05-01 01:03:152305
[email protected]d96eb512012-11-01 23:44:082306 bool unacknowledged_external = IsUnacknowledgedExternalExtension(extension);
[email protected]41070e8d2012-10-24 01:34:362307
[email protected]399583b2012-12-11 09:33:422308 // Unpacked extensions default to allowing file access, but if that has been
2309 // overridden, don't reset the value.
[email protected]1d5e58b2013-01-31 08:41:402310 if (Manifest::ShouldAlwaysAllowFileAccess(extension->location()) &&
[email protected]399583b2012-12-11 09:33:422311 !extension_prefs_->HasAllowFileAccessSetting(extension->id())) {
2312 extension_prefs_->SetAllowFileAccess(extension->id(), true);
2313 }
2314
[email protected]6f6101832012-11-27 22:10:482315 AddExtension(extension);
[email protected]612a1cb12012-10-17 13:18:032316
2317 // If this is a new external extension that was disabled, alert the user
[email protected]d96eb512012-11-01 23:44:082318 // so he can reenable it. We do this last so that it has already been
2319 // added to our list of extensions.
[email protected]3e3fafbb2013-08-17 13:33:162320 if (unacknowledged_external && !is_update) {
[email protected]612a1cb12012-10-17 13:18:032321 UpdateExternalExtensionAlert();
[email protected]d96eb512012-11-01 23:44:082322 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
2323 EXTERNAL_EXTENSION_INSTALLED,
2324 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:022325 if (extensions::ManifestURL::UpdatesFromGallery(extension)) {
2326 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventWebstore",
2327 EXTERNAL_EXTENSION_INSTALLED,
2328 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
2329 } else {
2330 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventNonWebstore",
2331 EXTERNAL_EXTENSION_INSTALLED,
2332 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
2333 }
[email protected]d96eb512012-11-01 23:44:082334 }
[email protected]9f4e4f082013-06-21 07:11:192335
2336 // Check extensions that may have been delayed only because this shared module
2337 // was not available.
2338 if (SharedModuleInfo::IsSharedModule(extension)) {
2339 MaybeFinishDelayedInstallations();
2340 }
[email protected]4a190632009-05-09 01:07:422341}
2342
[email protected]76b65442012-11-17 14:11:482343const Extension* ExtensionService::GetPendingExtensionUpdate(
2344 const std::string& id) const {
[email protected]9f4e4f082013-06-21 07:11:192345 return delayed_installs_.GetByID(id);
[email protected]76b65442012-11-17 14:11:482346}
2347
[email protected]bb7f40952011-01-13 00:21:202348void ExtensionService::TrackTerminatedExtension(const Extension* extension) {
[email protected]bb1bc9b32013-12-21 03:09:142349 // No need to check for duplicates; inserting a duplicate is a no-op.
2350 registry_->AddTerminated(make_scoped_refptr(extension));
[email protected]3964e572014-02-13 21:57:412351 extensions_being_terminated_.erase(extension->id());
[email protected]b0af4792013-10-23 09:12:132352 UnloadExtension(extension->id(), UnloadedExtensionInfo::REASON_TERMINATE);
[email protected]bb7f40952011-01-13 00:21:202353}
2354
2355void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
[email protected]fa2416f2011-05-03 08:41:202356 std::string lowercase_id = StringToLowerASCII(id);
[email protected]bb1bc9b32013-12-21 03:09:142357 const Extension* extension =
2358 registry_->terminated_extensions().GetByID(lowercase_id);
2359 registry_->RemoveTerminated(lowercase_id);
[email protected]fb789532013-08-27 02:40:212360 if (extension) {
2361 content::NotificationService::current()->Notify(
2362 chrome::NOTIFICATION_EXTENSION_REMOVED,
2363 content::Source<Profile>(profile_),
2364 content::Details<const Extension>(extension));
2365 }
[email protected]bb7f40952011-01-13 00:21:202366}
2367
[email protected]0dfe05c2011-02-23 23:03:362368const Extension* ExtensionService::GetTerminatedExtension(
[email protected]8001df22011-04-28 19:59:472369 const std::string& id) const {
[email protected]599539802014-01-07 23:06:002370 return registry_->GetExtensionById(id, ExtensionRegistry::TERMINATED);
[email protected]8001df22011-04-28 19:59:472371}
2372
2373const Extension* ExtensionService::GetInstalledExtension(
2374 const std::string& id) const {
[email protected]599539802014-01-07 23:06:002375 return registry_->GetExtensionById(id, ExtensionRegistry::EVERYTHING);
[email protected]0dfe05c2011-02-23 23:03:362376}
2377
[email protected]eaa7dd182010-12-14 11:09:002378bool ExtensionService::ExtensionBindingsAllowed(const GURL& url) {
[email protected]615d88f2011-12-13 01:47:442379 // Allow bindings for all packaged extensions and component hosted apps.
[email protected]bb1bc9b32013-12-21 03:09:142380 const Extension* extension =
2381 registry_->enabled_extensions().GetExtensionOrAppByURL(url);
[email protected]615d88f2011-12-13 01:47:442382 return extension && (!extension->is_hosted_app() ||
[email protected]1d5e58b2013-01-31 08:41:402383 extension->location() == Manifest::COMPONENT);
[email protected]6d2e60bd2010-06-03 22:37:392384}
2385
[email protected]9060d8b02012-01-13 02:14:302386bool ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:322387 const std::string& id,
[email protected]c5e4a2222014-01-03 16:06:132388 const Version* version,
[email protected]650b2d52013-02-10 03:41:452389 const base::FilePath& path,
[email protected]1d5e58b2013-01-31 08:41:402390 Manifest::Location location,
[email protected]47fc70c2011-12-06 07:29:512391 int creation_flags,
2392 bool mark_acknowledged) {
[email protected]ab22ba42011-01-14 16:36:382393 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2394 CHECK(Extension::IdIsValid(id));
[email protected]79c833b52011-04-05 18:31:012395 if (extension_prefs_->IsExternalExtensionUninstalled(id))
[email protected]9060d8b02012-01-13 02:14:302396 return false;
[email protected]a8af9fdb2010-10-28 21:52:202397
[email protected]7577a5c52009-07-30 06:21:582398 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:492399 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:582400 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:022401 const Extension* existing = GetExtensionById(id, true);
[email protected]910f72ce2012-08-24 01:38:352402
[email protected]7577a5c52009-07-30 06:21:582403 if (existing) {
[email protected]910f72ce2012-08-24 01:38:352404 // The default apps will have the location set as INTERNAL. Since older
2405 // default apps are installed as EXTERNAL, we override them. However, if the
2406 // app is already installed as internal, then do the version check.
[email protected]31bb5ee62012-09-12 22:58:402407 // TODO(grv) : Remove after Q1-2013.
[email protected]910f72ce2012-08-24 01:38:352408 bool is_default_apps_migration =
[email protected]1d5e58b2013-01-31 08:41:402409 (location == Manifest::INTERNAL &&
2410 Manifest::IsExternalLocation(existing->location()));
[email protected]910f72ce2012-08-24 01:38:352411
2412 if (!is_default_apps_migration) {
2413 DCHECK(version);
2414
2415 switch (existing->version()->CompareTo(*version)) {
2416 case -1: // existing version is older, we should upgrade
2417 break;
2418 case 0: // existing version is same, do nothing
2419 return false;
2420 case 1: // existing version is newer, uh-oh
2421 LOG(WARNING) << "Found external version of extension " << id
2422 << "that is older than current version. Current version "
2423 << "is: " << existing->VersionString() << ". New "
2424 << "version is: " << version->GetString()
2425 << ". Keeping current version.";
2426 return false;
2427 }
[email protected]7577a5c52009-07-30 06:21:582428 }
2429 }
2430
[email protected]9060d8b02012-01-13 02:14:302431 // If the extension is already pending, don't start an install.
[email protected]e3987852012-05-04 10:06:302432 if (!pending_extension_manager()->AddFromExternalFile(
[email protected]464213a2013-10-15 01:06:482433 id, location, *version, creation_flags, mark_acknowledged)) {
[email protected]9060d8b02012-01-13 02:14:302434 return false;
[email protected]e3987852012-05-04 10:06:302435 }
[email protected]9c635f22010-12-02 09:36:362436
[email protected]14908b72011-04-20 06:54:362437 // no client (silent install)
[email protected]f8636f92013-08-09 21:02:372438 scoped_refptr<CrxInstaller> installer(CrxInstaller::CreateSilent(this));
[email protected]6dfbbf82010-03-12 23:09:162439 installer->set_install_source(location);
2440 installer->set_expected_id(id);
[email protected]cb0e50312011-05-09 15:03:072441 installer->set_expected_version(*version);
2442 installer->set_install_cause(extension_misc::INSTALL_CAUSE_EXTERNAL_FILE);
[email protected]1bf73cc32011-10-26 22:38:312443 installer->set_creation_flags(creation_flags);
[email protected]88e8ec9152013-01-17 04:05:182444#if defined(OS_CHROMEOS)
2445 extensions::InstallLimiter::Get(profile_)->Add(installer, path);
2446#else
[email protected]6dfbbf82010-03-12 23:09:162447 installer->InstallCrx(path);
[email protected]88e8ec9152013-01-17 04:05:182448#endif
[email protected]47fc70c2011-12-06 07:29:512449
2450 // Depending on the source, a new external extension might not need a user
2451 // notification on installation. For such extensions, mark them acknowledged
2452 // now to suppress the notification.
2453 if (mark_acknowledged)
2454 AcknowledgeExternalExtension(id);
[email protected]9060d8b02012-01-13 02:14:302455
2456 return true;
[email protected]7577a5c52009-07-30 06:21:582457}
2458
[email protected]eaa7dd182010-12-14 11:09:002459void ExtensionService::ReportExtensionLoadError(
[email protected]650b2d52013-02-10 03:41:452460 const base::FilePath& extension_path,
[email protected]d11c8e92009-10-20 23:26:402461 const std::string &error,
[email protected]d11c8e92009-10-20 23:26:402462 bool be_noisy) {
[email protected]d8c8f25f2011-11-02 18:18:012463 content::NotificationService::current()->Notify(
2464 chrome::NOTIFICATION_EXTENSION_LOAD_ERROR,
2465 content::Source<Profile>(profile_),
2466 content::Details<const std::string>(&error));
[email protected]d11c8e92009-10-20 23:26:402467
[email protected]04338722013-12-24 23:18:052468 std::string path_str = base::UTF16ToUTF8(extension_path.LossyDisplayName());
[email protected]179fb952014-03-21 22:04:582469 bool retry = false;
2470 std::string retry_prompt;
2471 if (be_noisy)
2472 retry_prompt = "\n\nWould you like to retry?";
2473
2474 base::string16 message = base::UTF8ToUTF16(
2475 base::StringPrintf("Could not load extension from '%s'. %s%s",
2476 path_str.c_str(),
2477 error.c_str(),
2478 retry_prompt.c_str()));
2479 ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy, &retry);
2480 std::pair<bool, const base::FilePath&> details(retry, extension_path);
2481 content::NotificationService::current()->Notify(
2482 chrome::NOTIFICATION_EXTENSION_LOAD_RETRY,
2483 content::Source<Profile>(profile_),
2484 content::Details<std::pair<bool, const base::FilePath&> >(&details));
[email protected]d11c8e92009-10-20 23:26:402485}
2486
[email protected]eaa7dd182010-12-14 11:09:002487void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]3a1dc572012-07-31 22:25:132488 extensions::ExtensionHost* host) {
[email protected]406027c02010-09-27 08:03:182489 OrphanedDevTools::iterator iter =
[email protected]2d2f6cfc2011-05-06 21:09:332490 orphaned_dev_tools_.find(host->extension_id());
[email protected]406027c02010-09-27 08:03:182491 if (iter == orphaned_dev_tools_.end())
2492 return;
2493
[email protected]04ea1bb2013-07-10 09:26:092494 iter->second->ConnectRenderViewHost(host->render_view_host());
[email protected]406027c02010-09-27 08:03:182495 orphaned_dev_tools_.erase(iter);
2496}
2497
[email protected]432115822011-07-10 15:52:272498void ExtensionService::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:532499 const content::NotificationSource& source,
2500 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:272501 switch (type) {
[email protected]3c4abc82012-10-22 22:25:542502 case chrome::NOTIFICATION_APP_TERMINATING:
2503 // Shutdown has started. Don't start any more extension installs.
2504 // (We cannot use ExtensionService::Shutdown() for this because it
2505 // happens too late in browser teardown.)
2506 browser_terminating_ = true;
2507 break;
[email protected]432115822011-07-10 15:52:272508 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]6c2381d2011-10-19 02:52:532509 if (profile_ !=
2510 content::Source<Profile>(source).ptr()->GetOriginalProfile()) {
[email protected]bc535ee52010-08-31 18:40:322511 break;
[email protected]6c2381d2011-10-19 02:52:532512 }
[email protected]a4ed6282009-12-14 20:51:162513
[email protected]3a1dc572012-07-31 22:25:132514 extensions::ExtensionHost* host =
2515 content::Details<extensions::ExtensionHost>(details).ptr();
[email protected]f128af42010-08-05 18:05:262516
[email protected]3964e572014-02-13 21:57:412517 // If the extension is already being terminated, there is nothing left to
2518 // do.
2519 if (!extensions_being_terminated_.insert(host->extension_id()).second)
2520 break;
2521
[email protected]fa2416f2011-05-03 08:41:202522 // Mark the extension as terminated and Unload it. We want it to
2523 // be in a consistent state: either fully working or not loaded
2524 // at all, but never half-crashed. We do it in a PostTask so
2525 // that other handlers of this notification will still have
2526 // access to the Extension and ExtensionHost.
[email protected]b3a25092013-05-28 22:08:162527 base::MessageLoop::current()->PostTask(
[email protected]14908b72011-04-20 06:54:362528 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362529 base::Bind(
[email protected]fa2416f2011-05-03 08:41:202530 &ExtensionService::TrackTerminatedExtension,
[email protected]d8c8f25f2011-11-02 18:18:012531 AsWeakPtr(),
[email protected]fa2416f2011-05-03 08:41:202532 host->extension()));
[email protected]31f77262009-12-02 20:48:532533 break;
2534 }
[email protected]432115822011-07-10 15:52:272535 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
[email protected]f3b1a082011-11-18 00:34:302536 content::RenderProcessHost* process =
2537 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192538 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302539 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192540 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2541 break;
2542
[email protected]fafdc842014-01-17 18:09:082543 extensions::ProcessMap* process_map =
2544 extensions::ProcessMap::Get(profile_);
2545 if (process_map->Contains(process->GetID())) {
[email protected]52b76592013-11-02 17:59:032546 // An extension process was terminated, this might have resulted in an
2547 // app or extension becoming idle.
2548 std::set<std::string> extension_ids =
[email protected]fafdc842014-01-17 18:09:082549 process_map->GetExtensionsInProcess(process->GetID());
[email protected]52b76592013-11-02 17:59:032550 for (std::set<std::string>::const_iterator it = extension_ids.begin();
2551 it != extension_ids.end(); ++it) {
2552 if (delayed_installs_.Contains(*it)) {
2553 base::MessageLoop::current()->PostDelayedTask(
2554 FROM_HERE,
2555 base::Bind(&ExtensionService::MaybeFinishDelayedInstallation,
2556 AsWeakPtr(), *it),
2557 base::TimeDelta::FromSeconds(kUpdateIdleDelay));
2558 }
2559 }
2560 }
2561
[email protected]fafdc842014-01-17 18:09:082562 process_map->RemoveAllFromProcess(process->GetID());
[email protected]6f371442011-11-09 06:45:462563 BrowserThread::PostTask(
[email protected]38427a12013-11-09 17:34:202564 BrowserThread::IO,
2565 FROM_HERE,
2566 base::Bind(&extensions::InfoMap::UnregisterAllExtensionsInProcess,
[email protected]31d8f5f22012-04-02 15:22:082567 system_->info_map(),
[email protected]f3b1a082011-11-18 00:34:302568 process->GetID()));
[email protected]da5683db2011-04-23 17:12:212569 break;
2570 }
[email protected]92dd8d92013-02-26 00:41:082571 case chrome::NOTIFICATION_UPGRADE_RECOMMENDED: {
[email protected]75bdcb872013-03-13 00:41:452572 // Notify observers that chrome update is available.
2573 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
2574 OnChromeUpdateAvailable());
[email protected]92dd8d92013-02-26 00:41:082575 break;
2576 }
[email protected]aa96d3a2010-08-21 08:45:252577
[email protected]4814b512009-11-07 00:12:292578 default:
2579 NOTREACHED() << "Unexpected notification type.";
2580 }
2581}
2582
[email protected]90bb38d2012-11-14 18:36:032583void ExtensionService::OnExtensionInstallPrefChanged() {
[email protected]a6a7ced2012-11-01 17:24:182584 IdentifyAlertableExtensions();
2585 CheckManagementPolicy();
2586}
2587
[email protected]e178ad92013-06-28 02:29:252588bool ExtensionService::IsBeingReloaded(
2589 const std::string& extension_id) const {
2590 return ContainsKey(extensions_being_reloaded_, extension_id);
2591}
2592
2593void ExtensionService::SetBeingReloaded(const std::string& extension_id,
[email protected]5e7015c2013-07-23 23:29:432594 bool isBeingReloaded) {
2595 if (isBeingReloaded)
[email protected]e178ad92013-06-28 02:29:252596 extensions_being_reloaded_.insert(extension_id);
[email protected]5e7015c2013-07-23 23:29:432597 else
[email protected]e178ad92013-06-28 02:29:252598 extensions_being_reloaded_.erase(extension_id);
[email protected]e178ad92013-06-28 02:29:252599}
2600
[email protected]612a1cb12012-10-17 13:18:032601bool ExtensionService::ShouldEnableOnInstall(const Extension* extension) {
2602 // Extensions installed by policy can't be disabled. So even if a previous
2603 // installation disabled the extension, make sure it is now enabled.
[email protected]75181f62014-03-19 21:41:122604 // TODO(rlp): Clean up the special case for external components as noted
2605 // in crbug.com/353266. For now, EXTERNAL_COMPONENT apps should be
2606 // default enabled on install as before.
2607 if (system_->management_policy()->MustRemainEnabled(extension, NULL) ||
2608 extension->location() == Manifest::EXTERNAL_COMPONENT) {
[email protected]612a1cb12012-10-17 13:18:032609 return true;
[email protected]75181f62014-03-19 21:41:122610 }
[email protected]612a1cb12012-10-17 13:18:032611
2612 if (extension_prefs_->IsExtensionDisabled(extension->id()))
2613 return false;
2614
[email protected]41070e8d2012-10-24 01:34:362615 if (FeatureSwitch::prompt_for_external_extensions()->IsEnabled()) {
[email protected]612a1cb12012-10-17 13:18:032616 // External extensions are initially disabled. We prompt the user before
[email protected]2c495c42013-01-04 21:49:542617 // enabling them. Hosted apps are excepted because they are not dangerous
2618 // (they need to be launched by the user anyway).
[email protected]1d5e58b2013-01-31 08:41:402619 if (extension->GetType() != Manifest::TYPE_HOSTED_APP &&
2620 Manifest::IsExternalLocation(extension->location()) &&
[email protected]612a1cb12012-10-17 13:18:032621 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id())) {
2622 return false;
2623 }
2624 }
[email protected]612a1cb12012-10-17 13:18:032625
2626 return true;
2627}
[email protected]0db124b02012-11-07 04:55:052628
[email protected]e7aa7b7e2012-11-27 04:51:222629bool ExtensionService::ShouldDelayExtensionUpdate(
2630 const std::string& extension_id,
2631 bool wait_for_idle) const {
2632 const char kOnUpdateAvailableEvent[] = "runtime.onUpdateAvailable";
2633
2634 // If delayed updates are globally disabled, or just for this extension,
2635 // don't delay.
2636 if (!install_updates_when_idle_ || !wait_for_idle)
2637 return false;
2638
[email protected]695b5712012-12-06 23:55:282639 const Extension* old = GetInstalledExtension(extension_id);
[email protected]e7aa7b7e2012-11-27 04:51:222640 // If there is no old extension, this is not an update, so don't delay.
2641 if (!old)
2642 return false;
2643
[email protected]9367eabc2013-03-01 01:29:292644 if (extensions::BackgroundInfo::HasPersistentBackgroundPage(old)) {
[email protected]e7aa7b7e2012-11-27 04:51:222645 // Delay installation if the extension listens for the onUpdateAvailable
2646 // event.
2647 return system_->event_router()->ExtensionHasEventListener(
2648 extension_id, kOnUpdateAvailableEvent);
2649 } else {
2650 // Delay installation if the extension is not idle.
[email protected]1d5cf4142014-01-24 18:25:222651 return !extensions::util::IsExtensionIdle(extension_id, profile_);
[email protected]e7aa7b7e2012-11-27 04:51:222652 }
[email protected]0db124b02012-11-07 04:55:052653}
[email protected]fdd679b2012-11-15 20:49:392654
[email protected]399583b2012-12-11 09:33:422655void ExtensionService::GarbageCollectIsolatedStorage() {
[email protected]650b2d52013-02-10 03:41:452656 scoped_ptr<base::hash_set<base::FilePath> > active_paths(
2657 new base::hash_set<base::FilePath>());
[email protected]bb1bc9b32013-12-21 03:09:142658 const ExtensionSet& extensions = registry_->enabled_extensions();
2659 for (ExtensionSet::const_iterator it = extensions.begin();
2660 it != extensions.end(); ++it) {
[email protected]cadac622013-06-11 16:46:362661 if (extensions::AppIsolationInfo::HasIsolatedStorage(it->get())) {
2662 active_paths->insert(BrowserContext::GetStoragePartitionForSite(
[email protected]3a746ec2014-03-15 05:30:562663 profile_,
2664 extensions::util::GetSiteForExtensionId(
2665 (*it)->id(), profile()))->GetPath());
[email protected]399583b2012-12-11 09:33:422666 }
2667 }
2668
[email protected]ca0336342014-03-21 12:58:342669 // The data of ephemeral apps can outlive their cache lifetime. Ensure
2670 // they are not garbage collected.
2671 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> evicted_apps_info(
2672 extension_prefs_->GetEvictedEphemeralAppsInfo());
2673 for (size_t i = 0; i < evicted_apps_info->size(); ++i) {
2674 extensions::ExtensionInfo* info = evicted_apps_info->at(i).get();
2675 if (extensions::util::HasIsolatedStorage(*info)) {
2676 active_paths->insert(BrowserContext::GetStoragePartitionForSite(
2677 profile_,
2678 extensions::util::GetSiteForExtensionId(
2679 info->extension_id, profile()))->GetPath());
2680 }
2681 }
2682
[email protected]9f4e4f082013-06-21 07:11:192683 DCHECK(!installs_delayed_for_gc());
2684 set_installs_delayed_for_gc(true);
[email protected]399583b2012-12-11 09:33:422685 BrowserContext::GarbageCollectStoragePartitions(
2686 profile_, active_paths.Pass(),
2687 base::Bind(&ExtensionService::OnGarbageCollectIsolatedStorageFinished,
2688 AsWeakPtr()));
2689}
2690
2691void ExtensionService::OnGarbageCollectIsolatedStorageFinished() {
[email protected]9f4e4f082013-06-21 07:11:192692 set_installs_delayed_for_gc(false);
2693 MaybeFinishDelayedInstallations();
2694}
2695
2696void ExtensionService::MaybeFinishDelayedInstallations() {
2697 std::vector<std::string> to_be_installed;
[email protected]399583b2012-12-11 09:33:422698 for (ExtensionSet::const_iterator it = delayed_installs_.begin();
2699 it != delayed_installs_.end();
2700 ++it) {
[email protected]9f4e4f082013-06-21 07:11:192701 to_be_installed.push_back((*it)->id());
[email protected]399583b2012-12-11 09:33:422702 }
[email protected]9f4e4f082013-06-21 07:11:192703 for (std::vector<std::string>::const_iterator it = to_be_installed.begin();
2704 it != to_be_installed.end();
[email protected]399583b2012-12-11 09:33:422705 ++it) {
[email protected]9f4e4f082013-06-21 07:11:192706 MaybeFinishDelayedInstallation(*it);
[email protected]399583b2012-12-11 09:33:422707 }
[email protected]399583b2012-12-11 09:33:422708}
2709
[email protected]fdd679b2012-11-15 20:49:392710void ExtensionService::OnBlacklistUpdated() {
[email protected]2d19eb6e2014-01-27 17:30:002711 blacklist_->GetBlacklistedIDs(
[email protected]f47f7172014-03-19 19:27:102712 registry_->GenerateInstalledExtensionsSet()->GetIDs(),
[email protected]3f2a2fa2013-09-24 02:55:252713 base::Bind(&ExtensionService::ManageBlacklist, AsWeakPtr()));
[email protected]695b5712012-12-06 23:55:282714}
2715
[email protected]2d19eb6e2014-01-27 17:30:002716void ExtensionService::ManageBlacklist(
2717 const extensions::Blacklist::BlacklistStateMap& state_map) {
[email protected]695b5712012-12-06 23:55:282718 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2719
[email protected]2d19eb6e2014-01-27 17:30:002720 std::set<std::string> blocked;
2721 ExtensionIdSet greylist;
2722 ExtensionIdSet unchanged;
2723 for (extensions::Blacklist::BlacklistStateMap::const_iterator it =
2724 state_map.begin();
2725 it != state_map.end();
2726 ++it) {
2727 switch (it->second) {
2728 case extensions::NOT_BLACKLISTED:
2729 break;
[email protected]695b5712012-12-06 23:55:282730
[email protected]2d19eb6e2014-01-27 17:30:002731 case extensions::BLACKLISTED_MALWARE:
2732 blocked.insert(it->first);
2733 break;
2734
2735 case extensions::BLACKLISTED_SECURITY_VULNERABILITY:
2736 case extensions::BLACKLISTED_CWS_POLICY_VIOLATION:
2737 case extensions::BLACKLISTED_POTENTIALLY_UNWANTED:
2738 greylist.insert(it->first);
2739 break;
2740
2741 case extensions::BLACKLISTED_UNKNOWN:
2742 unchanged.insert(it->first);
2743 break;
2744 }
2745 }
2746
2747 UpdateBlockedExtensions(blocked, unchanged);
2748 UpdateGreylistedExtensions(greylist, unchanged, state_map);
2749
2750 IdentifyAlertableExtensions();
2751}
2752
2753namespace {
2754void Partition(const ExtensionIdSet& before,
2755 const ExtensionIdSet& after,
2756 const ExtensionIdSet& unchanged,
2757 ExtensionIdSet* no_longer,
2758 ExtensionIdSet* not_yet) {
2759 *not_yet = base::STLSetDifference<ExtensionIdSet>(after, before);
2760 *no_longer = base::STLSetDifference<ExtensionIdSet>(before, after);
2761 *no_longer = base::STLSetDifference<ExtensionIdSet>(*no_longer, unchanged);
2762}
2763} // namespace
2764
2765void ExtensionService::UpdateBlockedExtensions(
2766 const ExtensionIdSet& blocked,
2767 const ExtensionIdSet& unchanged) {
2768 ExtensionIdSet not_yet_blocked, no_longer_blocked;
2769 Partition(registry_->blacklisted_extensions().GetIDs(),
2770 blocked, unchanged,
2771 &no_longer_blocked, &not_yet_blocked);
2772
2773 for (ExtensionIdSet::iterator it = no_longer_blocked.begin();
2774 it != no_longer_blocked.end(); ++it) {
[email protected]695b5712012-12-06 23:55:282775 scoped_refptr<const Extension> extension =
[email protected]bb1bc9b32013-12-21 03:09:142776 registry_->blacklisted_extensions().GetByID(*it);
[email protected]3f2a2fa2013-09-24 02:55:252777 if (!extension.get()) {
[email protected]2d19eb6e2014-01-27 17:30:002778 NOTREACHED() << "Extension " << *it << " no longer blocked, "
2779 << "but it was never blocked.";
[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_->RemoveBlacklisted(*it);
[email protected]3f2a2fa2013-09-24 02:55:252783 extension_prefs_->SetExtensionBlacklisted(extension->id(), false);
[email protected]dc24976f2013-06-02 21:15:092784 AddExtension(extension.get());
[email protected]ac875372013-02-28 04:36:092785 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.UnblacklistInstalled",
[email protected]dc24976f2013-06-02 21:15:092786 extension->location(),
2787 Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:282788 }
2789
[email protected]2d19eb6e2014-01-27 17:30:002790 for (ExtensionIdSet::iterator it = not_yet_blocked.begin();
2791 it != not_yet_blocked.end(); ++it) {
[email protected]695b5712012-12-06 23:55:282792 scoped_refptr<const Extension> extension = GetInstalledExtension(*it);
[email protected]3f2a2fa2013-09-24 02:55:252793 if (!extension.get()) {
2794 NOTREACHED() << "Extension " << *it << " needs to be "
2795 << "blacklisted, but it's not installed.";
[email protected]695b5712012-12-06 23:55:282796 continue;
[email protected]3f2a2fa2013-09-24 02:55:252797 }
[email protected]bb1bc9b32013-12-21 03:09:142798 registry_->AddBlacklisted(extension);
[email protected]2d19eb6e2014-01-27 17:30:002799 extension_prefs_->SetExtensionBlacklistState(
2800 extension->id(), extensions::BLACKLISTED_MALWARE);
[email protected]b0af4792013-10-23 09:12:132801 UnloadExtension(*it, UnloadedExtensionInfo::REASON_BLACKLIST);
[email protected]ac875372013-02-28 04:36:092802 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.BlacklistInstalled",
2803 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:282804 }
[email protected]2d19eb6e2014-01-27 17:30:002805}
[email protected]695b5712012-12-06 23:55:282806
[email protected]2d19eb6e2014-01-27 17:30:002807// TODO(oleg): UMA logging
2808void ExtensionService::UpdateGreylistedExtensions(
2809 const ExtensionIdSet& greylist,
2810 const ExtensionIdSet& unchanged,
2811 const extensions::Blacklist::BlacklistStateMap& state_map) {
2812 ExtensionIdSet not_yet_greylisted, no_longer_greylisted;
2813 Partition(greylist_.GetIDs(),
2814 greylist, unchanged,
2815 &no_longer_greylisted, &not_yet_greylisted);
2816
2817 for (ExtensionIdSet::iterator it = no_longer_greylisted.begin();
2818 it != no_longer_greylisted.end(); ++it) {
2819 scoped_refptr<const Extension> extension = greylist_.GetByID(*it);
2820 if (!extension.get()) {
2821 NOTREACHED() << "Extension " << *it << " no longer greylisted, "
2822 << "but it was not marked as greylisted.";
2823 continue;
2824 }
2825
2826 greylist_.Remove(*it);
2827 extension_prefs_->SetExtensionBlacklistState(extension->id(),
2828 extensions::NOT_BLACKLISTED);
2829 if (extension_prefs_->GetDisableReasons(extension->id()) &
2830 extensions::Extension::DISABLE_GREYLIST)
2831 EnableExtension(*it);
2832 }
2833
2834 for (ExtensionIdSet::iterator it = not_yet_greylisted.begin();
2835 it != not_yet_greylisted.end(); ++it) {
2836 scoped_refptr<const Extension> extension = GetInstalledExtension(*it);
2837 if (!extension.get()) {
2838 NOTREACHED() << "Extension " << *it << " needs to be "
2839 << "disabled, but it's not installed.";
2840 continue;
2841 }
2842 greylist_.Insert(extension);
2843 extension_prefs_->SetExtensionBlacklistState(extension->id(),
2844 state_map.find(*it)->second);
2845 if (registry_->enabled_extensions().Contains(extension->id()))
2846 DisableExtension(*it, extensions::Extension::DISABLE_GREYLIST);
2847 }
[email protected]fdd679b2012-11-15 20:49:392848}
[email protected]75bdcb872013-03-13 00:41:452849
2850void ExtensionService::AddUpdateObserver(extensions::UpdateObserver* observer) {
2851 update_observers_.AddObserver(observer);
2852}
2853
2854void ExtensionService::RemoveUpdateObserver(
2855 extensions::UpdateObserver* observer) {
2856 update_observers_.RemoveObserver(observer);
2857}
[email protected]bb1bc9b32013-12-21 03:09:142858
2859// Used only by test code.
2860void ExtensionService::UnloadAllExtensionsInternal() {
2861 profile_->GetExtensionSpecialStoragePolicy()->RevokeRightsForAllExtensions();
2862
2863 registry_->ClearAll();
[email protected]45f5b7d2014-01-22 23:47:132864 system_->runtime_data()->ClearAll();
[email protected]bb1bc9b32013-12-21 03:09:142865
2866 // TODO(erikkay) should there be a notification for this? We can't use
2867 // EXTENSION_UNLOADED since that implies that the extension has been disabled
2868 // or uninstalled.
2869}