blob: ec57c0a2b724e6706e02157254a6f9309916ff28 [file] [log] [blame]
[email protected]098fa7a2013-03-08 22:11:171// Copyright (c) 2013 The Chromium Authors. All rights reserved.
[email protected]6014d672008-12-05 00:38:252// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]eaa7dd182010-12-14 11:09:005#include "chrome/browser/extensions/extension_service.h"
[email protected]6014d672008-12-05 00:38:256
[email protected]654512b2010-09-01 02:09:427#include <algorithm>
[email protected]695b5712012-12-06 23:55:288#include <iterator>
[email protected]97d2f1d2011-01-15 00:41:089#include <set>
[email protected]654512b2010-09-01 02:09:4210
[email protected]24b538a2010-02-27 01:22:4411#include "base/basictypes.h"
[email protected]05aad2da2011-10-28 10:12:3712#include "base/bind.h"
[email protected]e5af875f2011-10-10 21:09:1413#include "base/callback.h"
[email protected]62433d32011-10-12 22:33:1214#include "base/command_line.h"
[email protected]6014d672008-12-05 00:38:2515#include "base/file_util.h"
[email protected]14908b72011-04-20 06:54:3616#include "base/logging.h"
[email protected]835d7c82010-10-14 04:38:3817#include "base/metrics/histogram.h"
[email protected]3853a4c2013-02-11 17:15:5718#include "base/prefs/pref_service.h"
[email protected]7286e3fc2011-07-19 22:13:2419#include "base/stl_util.h"
[email protected]3ea1b182013-02-08 22:38:4120#include "base/strings/string_number_conversions.h"
[email protected]00e7bef2013-06-10 20:35:1721#include "base/strings/string_util.h"
22#include "base/strings/stringprintf.h"
[email protected]112158af2013-06-07 23:46:1823#include "base/strings/utf_string_conversions.h"
[email protected]7f8f24f2012-11-15 19:40:1424#include "base/threading/sequenced_worker_pool.h"
[email protected]34b99632011-01-01 01:01:0625#include "base/threading/thread_restrictions.h"
[email protected]41a17c52013-06-28 00:27:5326#include "base/time/time.h"
[email protected]cc655912009-01-29 23:19:1927#include "base/values.h"
[email protected]aa142702010-03-26 01:26:3328#include "base/version.h"
[email protected]211a3f32013-05-28 21:48:1829#include "chrome/browser/app_mode/app_mode_utils.h"
[email protected]15730c42009-09-03 00:03:2030#include "chrome/browser/browser_process.h"
[email protected]9ea0cd32013-07-12 01:50:3631#include "chrome/browser/chrome_notification_types.h"
[email protected]70019152012-12-19 11:44:1932#include "chrome/browser/devtools/devtools_window.h"
[email protected]32b36c62012-10-24 05:37:4133#include "chrome/browser/extensions/api/app_runtime/app_runtime_api.h"
[email protected]895a1e52012-05-15 02:50:1234#include "chrome/browser/extensions/api/declarative/rules_registry_service.h"
[email protected]ad445762013-01-23 00:47:4435#include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
[email protected]c39df1a2013-01-08 18:22:0736#include "chrome/browser/extensions/api/profile_keyed_api_factory.h"
[email protected]e9f541a2012-11-19 21:52:3137#include "chrome/browser/extensions/api/runtime/runtime_api.h"
[email protected]4636c832013-01-11 02:10:1138#include "chrome/browser/extensions/api/storage/settings_frontend.h"
[email protected]5db9ada2012-04-11 13:48:2039#include "chrome/browser/extensions/app_sync_data.h"
[email protected]5a38dfd2012-07-23 23:22:1040#include "chrome/browser/extensions/browser_event_router.h"
[email protected]d8c8f25f2011-11-02 18:18:0141#include "chrome/browser/extensions/component_loader.h"
[email protected]e0785902011-05-19 23:34:1742#include "chrome/browser/extensions/crx_installer.h"
[email protected]d9ede582012-08-14 19:21:3843#include "chrome/browser/extensions/data_deleter.h"
[email protected]62f051c2012-03-29 17:04:4444#include "chrome/browser/extensions/extension_disabled_ui.h"
[email protected]14a000d2010-04-29 21:44:2445#include "chrome/browser/extensions/extension_error_reporter.h"
[email protected]89226982012-07-16 20:09:1846#include "chrome/browser/extensions/extension_error_ui.h"
[email protected]b1748b1d82009-11-30 20:32:5647#include "chrome/browser/extensions/extension_host.h"
[email protected]00b38242012-07-18 18:43:2248#include "chrome/browser/extensions/extension_install_ui.h"
[email protected]4814b512009-11-07 00:12:2949#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]3569b502012-01-12 20:08:2350#include "chrome/browser/extensions/extension_sorting.h"
[email protected]19eb80152011-02-26 00:28:4351#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]90310d92011-04-17 07:35:0452#include "chrome/browser/extensions/extension_sync_data.h"
[email protected]31d8f5f22012-04-02 15:22:0853#include "chrome/browser/extensions/extension_system.h"
[email protected]612a1cb12012-10-17 13:18:0354#include "chrome/browser/extensions/external_install_ui.h"
[email protected]5df038b2012-07-16 19:03:2755#include "chrome/browser/extensions/external_provider_impl.h"
56#include "chrome/browser/extensions/external_provider_interface.h"
[email protected]d8c8f25f2011-11-02 18:18:0157#include "chrome/browser/extensions/installed_loader.h"
[email protected]6cafe35a2012-08-27 22:40:5058#include "chrome/browser/extensions/lazy_background_task_queue.h"
[email protected]fdd679b2012-11-15 20:49:3959#include "chrome/browser/extensions/management_policy.h"
[email protected]b2907fd2011-03-25 16:43:3760#include "chrome/browser/extensions/pending_extension_manager.h"
[email protected]c333e792012-01-06 16:57:3961#include "chrome/browser/extensions/permissions_updater.h"
[email protected]6cafe35a2012-08-27 22:40:5062#include "chrome/browser/extensions/platform_app_launcher.h"
[email protected]6cafe35a2012-08-27 22:40:5063#include "chrome/browser/extensions/shell_window_registry.h"
[email protected]d8c8f25f2011-11-02 18:18:0164#include "chrome/browser/extensions/unpacked_installer.h"
[email protected]75bdcb872013-03-13 00:41:4565#include "chrome/browser/extensions/update_observer.h"
[email protected]42a08162012-03-16 18:09:1166#include "chrome/browser/extensions/updater/extension_updater.h"
[email protected]8ecad5e2010-12-02 21:18:3367#include "chrome/browser/profiles/profile.h"
[email protected]cf593af2011-12-30 05:44:3968#include "chrome/browser/profiles/profile_manager.h"
[email protected]d5949312012-12-03 22:13:3069#include "chrome/browser/themes/theme_service.h"
70#include "chrome/browser/themes/theme_service_factory.h"
[email protected]c8d407e2011-04-28 21:27:1771#include "chrome/browser/ui/webui/favicon_source.h"
[email protected]5eddc3e2011-10-26 04:33:3172#include "chrome/browser/ui/webui/ntp/thumbnail_source.h"
[email protected]b07e606e2012-09-15 20:16:1573#include "chrome/browser/ui/webui/theme_source.h"
[email protected]aab98a52009-12-02 03:22:3574#include "chrome/common/child_process_logging.h"
[email protected]e2eb43112009-05-29 21:19:5475#include "chrome/common/chrome_switches.h"
[email protected]15397b42012-05-16 23:56:0676#include "chrome/common/chrome_version_info.h"
[email protected]9367eabc2013-03-01 01:29:2977#include "chrome/common/extensions/background_info.h"
[email protected]5b1a0e22009-05-26 19:00:5878#include "chrome/common/extensions/extension.h"
[email protected]9f4e4f082013-06-21 07:11:1979#include "chrome/common/extensions/extension_constants.h"
[email protected]7c927b62010-02-24 09:54:1380#include "chrome/common/extensions/extension_file_util.h"
[email protected]895a1e52012-05-15 02:50:1281#include "chrome/common/extensions/extension_manifest_constants.h"
[email protected]77a6970c2011-04-23 16:58:5682#include "chrome/common/extensions/extension_messages.h"
[email protected]612a1cb12012-10-17 13:18:0383#include "chrome/common/extensions/feature_switch.h"
[email protected]e9f541a2012-11-19 21:52:3184#include "chrome/common/extensions/features/feature.h"
[email protected]06492ed2013-03-24 22:13:1485#include "chrome/common/extensions/incognito_handler.h"
[email protected]ac875372013-02-28 04:36:0986#include "chrome/common/extensions/manifest.h"
[email protected]561e33d52013-04-03 06:58:4387#include "chrome/common/extensions/manifest_handlers/app_isolation_info.h"
[email protected]6b414c232013-06-05 07:53:3488#include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
[email protected]9f4e4f082013-06-21 07:11:1989#include "chrome/common/extensions/manifest_handlers/shared_module_info.h"
[email protected]65348062013-01-15 07:27:2290#include "chrome/common/extensions/manifest_url_handler.h"
[email protected]2cb5e302013-05-09 05:00:0691#include "chrome/common/extensions/permissions/permissions_data.h"
[email protected]8f3bcbd2013-06-05 08:42:4092#include "chrome/common/extensions/sync_helper.h"
[email protected]25b34332009-06-05 21:53:1993#include "chrome/common/pref_names.h"
[email protected]21f2e2d2013-01-23 22:59:5294#include "chrome/common/startup_metric_utils.h"
[email protected]a57209872009-05-04 22:53:1495#include "chrome/common/url_constants.h"
[email protected]c38831a12011-10-28 12:44:4996#include "content/public/browser/browser_thread.h"
[email protected]98f66112012-12-25 12:59:3697#include "content/public/browser/devtools_agent_host.h"
[email protected]05aad2da2011-10-28 10:12:3798#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1699#include "content/public/browser/notification_types.h"
[email protected]f3b1a082011-11-18 00:34:30100#include "content/public/browser/render_process_host.h"
[email protected]399583b2012-12-11 09:33:42101#include "content/public/browser/site_instance.h"
102#include "content/public/browser/storage_partition.h"
[email protected]24ea7a12013-01-27 23:54:53103#include "content/public/browser/url_data_source.h"
[email protected]836e2982013-05-16 08:07:42104#include "extensions/common/constants.h"
[email protected]e9f541a2012-11-19 21:52:31105#include "extensions/common/error_utils.h"
[email protected]65187152012-06-02 13:14:14106#include "grit/generated_resources.h"
[email protected]be28b5f42012-07-20 11:31:25107#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
[email protected]895a1e52012-05-15 02:50:12108#include "sync/api/sync_change.h"
109#include "sync/api/sync_error_factory.h"
[email protected]4941fe92013-06-13 23:21:01110#include "ui/webui/web_ui_util.h"
[email protected]a6483d22013-07-03 22:11:00111#include "url/gurl.h"
[email protected]08b1f75f2013-05-22 22:02:38112#include "webkit/browser/database/database_tracker.h"
113#include "webkit/browser/database/database_util.h"
[email protected]79a60642012-10-20 21:03:18114
[email protected]eed367e2011-04-12 03:43:31115#if defined(OS_CHROMEOS)
[email protected]88e8ec9152013-01-17 04:05:18116#include "chrome/browser/chromeos/extensions/install_limiter.h"
[email protected]f19bbf62013-07-09 01:22:32117#include "webkit/browser/fileapi/file_system_backend.h"
[email protected]c6f9203a2013-05-28 02:08:07118#include "webkit/browser/fileapi/file_system_context.h"
[email protected]eed367e2011-04-12 03:43:31119#endif
120
[email protected]55eb70e762012-02-20 17:38:39121using content::BrowserContext;
[email protected]631bb742011-11-02 11:29:39122using content::BrowserThread;
[email protected]98f66112012-12-25 12:59:36123using content::DevToolsAgentHost;
[email protected]bf3d9df2012-07-24 23:20:27124using extensions::CrxInstaller;
[email protected]1c321ee52012-05-21 03:02:34125using extensions::Extension;
126using extensions::ExtensionIdSet;
127using extensions::ExtensionInfo;
[email protected]215a7be2012-10-22 19:53:42128using extensions::FeatureSwitch;
[email protected]1d5e58b2013-01-31 08:41:40129using extensions::Manifest;
[email protected]c2e66e12012-06-27 06:27:06130using extensions::PermissionMessage;
131using extensions::PermissionMessages;
132using extensions::PermissionSet;
[email protected]9f4e4f082013-06-21 07:11:19133using extensions::SharedModuleInfo;
[email protected]e410b5f2012-12-14 14:02:24134using extensions::UnloadedExtensionInfo;
[email protected]5ef47ec2010-01-28 05:58:05135
[email protected]c6d474f82009-12-16 21:11:06136namespace errors = extension_manifest_errors;
137
[email protected]b6ab96d2009-08-20 18:58:19138namespace {
139
[email protected]d96eb512012-11-01 23:44:08140// Histogram values for logging events related to externally installed
141// extensions.
142enum ExternalExtensionEvent {
143 EXTERNAL_EXTENSION_INSTALLED = 0,
144 EXTERNAL_EXTENSION_IGNORED,
145 EXTERNAL_EXTENSION_REENABLED,
146 EXTERNAL_EXTENSION_UNINSTALLED,
147 EXTERNAL_EXTENSION_BUCKET_BOUNDARY,
148};
149
[email protected]612a1cb12012-10-17 13:18:03150// Prompt the user this many times before considering an extension acknowledged.
151static const int kMaxExtensionAcknowledgePromptCount = 3;
152
[email protected]0db124b02012-11-07 04:55:05153// Wait this many seconds after an extensions becomes idle before updating it.
154static const int kUpdateIdleDelay = 5;
155
[email protected]9bd9a6862012-11-29 09:24:22156// Wait this many seconds before trying to garbage collect extensions again.
157static const int kGarbageCollectRetryDelay = 30;
158
[email protected]36fc54f2013-06-14 02:49:34159// Wait this many seconds after startup to see if there are any extensions
160// which can be garbage collected.
161static const int kGarbageCollectStartupDelay = 30;
162
[email protected]9f4e4f082013-06-21 07:11:19163static bool IsSharedModule(const Extension* extension) {
164 return SharedModuleInfo::IsSharedModule(extension);
165}
166
[email protected]c6d474f82009-12-16 21:11:06167} // namespace
[email protected]b6ab96d2009-08-20 18:58:19168
[email protected]eaa7dd182010-12-14 11:09:00169ExtensionService::ExtensionRuntimeData::ExtensionRuntimeData()
[email protected]d7e9a862010-11-03 21:57:49170 : background_page_ready(false),
[email protected]f8bf5992011-10-24 22:38:57171 being_upgraded(false),
172 has_used_webrequest(false) {
[email protected]d7e9a862010-11-03 21:57:49173}
174
[email protected]eaa7dd182010-12-14 11:09:00175ExtensionService::ExtensionRuntimeData::~ExtensionRuntimeData() {
[email protected]d7e9a862010-11-03 21:57:49176}
177
[email protected]eaa7dd182010-12-14 11:09:00178// ExtensionService.
[email protected]6014d672008-12-05 00:38:25179
[email protected]31bb5ee62012-09-12 22:58:40180const char ExtensionService::kLocalAppSettingsDirectoryName[] =
[email protected]15d5b472011-11-24 04:17:24181 "Local App Settings";
[email protected]31bb5ee62012-09-12 22:58:40182const char ExtensionService::kLocalExtensionSettingsDirectoryName[] =
[email protected]15d5b472011-11-24 04:17:24183 "Local Extension Settings";
[email protected]31bb5ee62012-09-12 22:58:40184const char ExtensionService::kSyncAppSettingsDirectoryName[] =
[email protected]15d5b472011-11-24 04:17:24185 "Sync App Settings";
[email protected]31bb5ee62012-09-12 22:58:40186const char ExtensionService::kSyncExtensionSettingsDirectoryName[] =
[email protected]15d5b472011-11-24 04:17:24187 "Sync Extension Settings";
[email protected]675bb01012012-12-14 22:03:29188const char ExtensionService::kManagedSettingsDirectoryName[] =
189 "Managed Extension Settings";
[email protected]31bb5ee62012-09-12 22:58:40190const char ExtensionService::kStateStoreName[] = "Extension State";
[email protected]a690e292012-12-19 19:22:49191const char ExtensionService::kRulesStoreName[] = "Extension Rules";
[email protected]494c06e2009-07-25 01:06:42192
[email protected]8e4560b62011-01-14 10:09:14193void ExtensionService::CheckExternalUninstall(const std::string& id) {
[email protected]a29a517a2011-01-21 21:11:12194 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20195
[email protected]7a4c6852010-09-16 03:44:22196 // Check if the providers know about this extension.
[email protected]5df038b2012-07-16 19:03:27197 extensions::ProviderCollection::const_iterator i;
[email protected]0a60a2e2010-10-25 16:15:21198 for (i = external_extension_providers_.begin();
199 i != external_extension_providers_.end(); ++i) {
[email protected]8e4560b62011-01-14 10:09:14200 DCHECK(i->get()->IsReady());
[email protected]0a60a2e2010-10-25 16:15:21201 if (i->get()->HasExtension(id))
202 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22203 }
204
[email protected]0f48fca2011-05-19 18:46:35205 // We get the list of external extensions to check from preferences.
206 // It is possible that an extension has preferences but is not loaded.
207 // For example, an extension that requires experimental permissions
208 // will not be loaded if the experimental command line flag is not used.
209 // In this case, do not uninstall.
[email protected]31bb5ee62012-09-12 22:58:40210 if (!GetInstalledExtension(id)) {
[email protected]0f48fca2011-05-19 18:46:35211 // We can't call UninstallExtension with an unloaded/invalid
212 // extension ID.
213 LOG(WARNING) << "Attempted uninstallation of unloaded/invalid extension "
214 << "with id: " << id;
215 return;
216 }
[email protected]d6ebc9792011-04-07 18:18:33217 UninstallExtension(id, true, NULL);
[email protected]7a4c6852010-09-16 03:44:22218}
219
[email protected]7f8f24f2012-11-15 19:40:14220void ExtensionService::SetFileTaskRunnerForTesting(
221 base::SequencedTaskRunner* task_runner) {
222 file_task_runner_ = task_runner;
223}
224
[email protected]8e4560b62011-01-14 10:09:14225void ExtensionService::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22226 external_extension_providers_.clear();
227}
228
[email protected]8e4560b62011-01-14 10:09:14229void ExtensionService::AddProviderForTesting(
[email protected]5df038b2012-07-16 19:03:27230 extensions::ExternalProviderInterface* test_provider) {
[email protected]a29a517a2011-01-21 21:11:12231 CHECK(test_provider);
[email protected]0a60a2e2010-10-25 16:15:21232 external_extension_providers_.push_back(
[email protected]5df038b2012-07-16 19:03:27233 linked_ptr<extensions::ExternalProviderInterface>(test_provider));
[email protected]7a4c6852010-09-16 03:44:22234}
235
[email protected]9060d8b02012-01-13 02:14:30236bool ExtensionService::OnExternalExtensionUpdateUrlFound(
[email protected]7a4c6852010-09-16 03:44:22237 const std::string& id,
[email protected]21a5a672010-11-04 10:47:42238 const GURL& update_url,
[email protected]1d5e58b2013-01-31 08:41:40239 Manifest::Location location) {
[email protected]ab22ba42011-01-14 16:36:38240 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
241 CHECK(Extension::IdIsValid(id));
[email protected]a8af9fdb2010-10-28 21:52:20242
[email protected]8a87a5332011-08-11 17:54:59243 const Extension* extension = GetExtensionById(id, true);
244 if (extension) {
245 // Already installed. Skip this install if the current location has
246 // higher priority than |location|.
[email protected]1d5e58b2013-01-31 08:41:40247 Manifest::Location current = extension->location();
248 if (current == Manifest::GetHigherPriorityLocation(current, location))
[email protected]9060d8b02012-01-13 02:14:30249 return false;
[email protected]8a87a5332011-08-11 17:54:59250 // Otherwise, overwrite the current installation.
[email protected]7a4c6852010-09-16 03:44:22251 }
[email protected]9060d8b02012-01-13 02:14:30252
253 // Add |id| to the set of pending extensions. If it can not be added,
254 // then there is already a pending record from a higher-priority install
255 // source. In this case, signal that this extension will not be
256 // installed by returning false.
257 if (!pending_extension_manager()->AddFromExternalUpdateUrl(
[email protected]31bb5ee62012-09-12 22:58:40258 id, update_url, location)) {
[email protected]9060d8b02012-01-13 02:14:30259 return false;
[email protected]31bb5ee62012-09-12 22:58:40260 }
[email protected]9060d8b02012-01-13 02:14:30261
[email protected]94fde232012-04-27 10:22:30262 update_once_all_providers_are_ready_ = true;
[email protected]9060d8b02012-01-13 02:14:30263 return true;
[email protected]7a4c6852010-09-16 03:44:22264}
265
[email protected]dc9a74f72012-08-17 18:07:21266const Extension* ExtensionService::GetInstalledApp(const GURL& url) const {
[email protected]be9915fb2013-07-18 09:28:55267 const Extension* extension = extensions_.GetExtensionOrAppByURL(url);
[email protected]dc9a74f72012-08-17 18:07:21268 return (extension && extension->is_app()) ? extension : NULL;
[email protected]d9696672011-03-15 22:45:09269}
270
[email protected]dc9a74f72012-08-17 18:07:21271bool ExtensionService::IsInstalledApp(const GURL& url) const {
[email protected]d9696672011-03-15 22:45:09272 return !!GetInstalledApp(url);
[email protected]ffb204f22010-12-05 23:20:27273}
274
[email protected]404fb002012-10-02 21:06:17275const Extension* ExtensionService::GetIsolatedAppForRenderer(
[email protected]dc9a74f72012-08-17 18:07:21276 int renderer_child_id) const {
[email protected]404fb002012-10-02 21:06:17277 std::set<std::string> extension_ids =
278 process_map_.GetExtensionsInProcess(renderer_child_id);
279 // All apps in one process share the same partition.
280 // It is only possible for the app to have isolated storage
281 // if there is only 1 app in the process.
282 if (extension_ids.size() != 1)
283 return NULL;
284
285 const extensions::Extension* extension =
286 extensions_.GetByID(*(extension_ids.begin()));
[email protected]561e33d52013-04-03 06:58:43287 // We still need to check if the extension has isolated storage,
[email protected]404fb002012-10-02 21:06:17288 // because it's common for there to be one extension in a process
[email protected]561e33d52013-04-03 06:58:43289 // without isolated storage.
290 if (extension &&
291 extensions::AppIsolationInfo::HasIsolatedStorage(extension))
[email protected]404fb002012-10-02 21:06:17292 return extension;
293
294 return NULL;
[email protected]da5683db2011-04-23 17:12:21295}
296
[email protected]6aeac8342010-10-01 20:21:18297// static
[email protected]d6ebc9792011-04-07 18:18:33298// This function is used to implement the command-line switch
[email protected]3bdba0d2011-08-23 07:17:30299// --uninstall-extension, and to uninstall an extension via sync. The LOG
300// statements within this function are used to inform the user if the uninstall
301// cannot be done.
[email protected]eaa7dd182010-12-14 11:09:00302bool ExtensionService::UninstallExtensionHelper(
303 ExtensionService* extensions_service,
[email protected]6aeac8342010-10-01 20:21:18304 const std::string& extension_id) {
[email protected]d6ebc9792011-04-07 18:18:33305 // We can't call UninstallExtension with an invalid extension ID.
[email protected]31bb5ee62012-09-12 22:58:40306 if (!extensions_service->GetInstalledExtension(extension_id)) {
[email protected]6aeac8342010-10-01 20:21:18307 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]95da88c42011-03-31 10:07:33308 << "id: " << extension_id;
309 return false;
[email protected]6aeac8342010-10-01 20:21:18310 }
311
[email protected]d6ebc9792011-04-07 18:18:33312 // The following call to UninstallExtension will not allow an uninstall of a
313 // policy-controlled extension.
[email protected]65187152012-06-02 13:14:14314 string16 error;
[email protected]d6ebc9792011-04-07 18:18:33315 if (!extensions_service->UninstallExtension(extension_id, false, &error)) {
316 LOG(WARNING) << "Cannot uninstall extension with id " << extension_id
317 << ": " << error;
318 return false;
319 }
[email protected]95da88c42011-03-31 10:07:33320
[email protected]6aeac8342010-10-01 20:21:18321 return true;
322}
323
[email protected]eaa7dd182010-12-14 11:09:00324ExtensionService::ExtensionService(Profile* profile,
[email protected]f0841cd2011-01-19 15:07:24325 const CommandLine* command_line,
[email protected]650b2d52013-02-10 03:41:45326 const base::FilePath& install_directory,
[email protected]45759612012-07-10 17:21:23327 extensions::ExtensionPrefs* extension_prefs,
[email protected]fdd679b2012-11-15 20:49:39328 extensions::Blacklist* blacklist,
[email protected]0436b102011-04-15 18:30:03329 bool autoupdate_enabled,
[email protected]4a10006a2013-05-17 23:18:35330 bool extensions_enabled,
331 extensions::OneShotEvent* ready)
[email protected]fdd679b2012-11-15 20:49:39332 : extensions::Blacklist::Observer(blacklist),
333 profile_(profile),
[email protected]bd306722012-07-11 20:43:59334 system_(extensions::ExtensionSystem::Get(profile)),
[email protected]73c47932010-12-06 18:13:43335 extension_prefs_(extension_prefs),
[email protected]695b5712012-12-06 23:55:28336 blacklist_(blacklist),
[email protected]cccdf0aa2011-11-11 03:43:38337 settings_frontend_(extensions::SettingsFrontend::Create(profile)),
[email protected]9c009092013-05-01 03:14:09338 pending_extension_manager_(*this),
[email protected]a9b00ac2009-06-25 21:03:23339 install_directory_(install_directory),
[email protected]0436b102011-04-15 18:30:03340 extensions_enabled_(extensions_enabled),
[email protected]e81dba32009-06-19 20:19:13341 show_extensions_prompts_(true),
[email protected]fc332ae2012-11-14 20:17:33342 install_updates_when_idle_(true),
[email protected]4a10006a2013-05-17 23:18:35343 ready_(ready),
[email protected]9c009092013-05-01 03:14:09344 toolbar_model_(this),
[email protected]8b280302011-10-13 22:22:23345 menu_manager_(profile),
[email protected]7c1490da2011-10-11 18:53:25346 event_routers_initialized_(false),
[email protected]dff1e042012-04-27 10:59:18347 update_once_all_providers_are_ready_(false),
[email protected]3c4abc82012-10-22 22:25:54348 browser_terminating_(false),
[email protected]9f4e4f082013-06-21 07:11:19349 installs_delayed_for_gc_(false),
[email protected]460c6712013-04-24 07:20:01350 is_first_run_(false),
[email protected]9c009092013-05-01 03:14:09351 app_sync_bundle_(this),
352 extension_sync_bundle_(this) {
[email protected]a29a517a2011-01-21 21:11:12353 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20354
[email protected]36a784c2009-06-23 06:21:08355 // Figure out if extension installation should be enabled.
[email protected]31bb5ee62012-09-12 22:58:40356 if (command_line->HasSwitch(switches::kDisableExtensions) ||
357 profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions)) {
[email protected]6d60703b2009-08-29 01:29:23358 extensions_enabled_ = false;
[email protected]6b75ec32009-08-14 06:37:18359 }
[email protected]36a784c2009-06-23 06:21:08360
[email protected]3c4abc82012-10-22 22:25:54361 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING,
362 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27363 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07364 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27365 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED,
[email protected]ad50def52011-10-19 23:17:07366 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27367 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07368 content::NotificationService::AllBrowserContextsAndSources());
[email protected]0db124b02012-11-07 04:55:05369 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
370 content::NotificationService::AllBrowserContextsAndSources());
[email protected]92dd8d92013-02-26 00:41:08371 registrar_.Add(this, chrome::NOTIFICATION_UPGRADE_RECOMMENDED,
372 content::NotificationService::AllBrowserContextsAndSources());
[email protected]2fb7dc982010-09-29 12:24:28373 pref_change_registrar_.Init(profile->GetPrefs());
[email protected]90bb38d2012-11-14 18:36:03374 base::Closure callback =
375 base::Bind(&ExtensionService::OnExtensionInstallPrefChanged,
376 base::Unretained(this));
377 pref_change_registrar_.Add(prefs::kExtensionInstallAllowList, callback);
378 pref_change_registrar_.Add(prefs::kExtensionInstallDenyList, callback);
[email protected]e410b5f2012-12-14 14:02:24379 pref_change_registrar_.Add(prefs::kExtensionAllowedTypes, callback);
[email protected]4814b512009-11-07 00:12:29380
[email protected]93fd78f42009-07-10 16:43:17381 // Set up the ExtensionUpdater
382 if (autoupdate_enabled) {
383 int update_frequency = kDefaultUpdateFrequencySeconds;
384 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25385 base::StringToInt(command_line->GetSwitchValueASCII(
386 switches::kExtensionsUpdateFrequency),
387 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17388 }
[email protected]42a08162012-03-16 18:09:11389 updater_.reset(new extensions::ExtensionUpdater(this,
390 extension_prefs,
391 profile->GetPrefs(),
392 profile,
[email protected]fdd679b2012-11-15 20:49:39393 blacklist,
[email protected]42a08162012-03-16 18:09:11394 update_frequency));
[email protected]93fd78f42009-07-10 16:43:17395 }
396
[email protected]25ae0152011-11-18 14:40:02397 component_loader_.reset(
398 new extensions::ComponentLoader(this,
399 profile->GetPrefs(),
400 g_browser_process->local_state()));
[email protected]8e4560b62011-01-14 10:09:14401
[email protected]0436b102011-04-15 18:30:03402 if (extensions_enabled_) {
[email protected]ae4c37e2012-12-21 01:16:25403 extensions::ExternalProviderImpl::CreateExternalProviders(
404 this, profile_, &external_extension_providers_);
[email protected]873531342011-03-09 12:16:05405 }
[email protected]b671760b2010-07-15 21:13:47406
[email protected]fb82dcd2012-03-21 14:15:46407 // Set this as the ExtensionService for extension sorting to ensure it
408 // cause syncs if required.
409 extension_prefs_->extension_sorting()->SetExtensionService(this);
410
[email protected]b3aa7182013-04-25 04:45:23411 is_first_run_ = !extension_prefs_->SetAlertSystemFirstRun();
412
[email protected]9ce11d22012-08-08 23:20:13413#if defined(ENABLE_EXTENSIONS)
[email protected]c77f2352012-08-08 22:07:58414 extension_action_storage_manager_.reset(
415 new extensions::ExtensionActionStorageManager(profile_));
[email protected]9ce11d22012-08-08 23:20:13416#endif
[email protected]c77f2352012-08-08 22:07:58417
[email protected]cb0e50312011-05-09 15:03:07418 // How long is the path to the Extensions directory?
419 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ExtensionRootPathLength",
420 install_directory_.value().length(), 0, 500, 100);
[email protected]6014d672008-12-05 00:38:25421}
422
[email protected]84df8332011-12-06 18:22:46423const ExtensionSet* ExtensionService::extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45424 return &extensions_;
425}
426
[email protected]84df8332011-12-06 18:22:46427const ExtensionSet* ExtensionService::disabled_extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45428 return &disabled_extensions_;
429}
430
[email protected]84df8332011-12-06 18:22:46431const ExtensionSet* ExtensionService::terminated_extensions() const {
[email protected]bb7f40952011-01-13 00:21:20432 return &terminated_extensions_;
433}
434
[email protected]695b5712012-12-06 23:55:28435const ExtensionSet* ExtensionService::blacklisted_extensions() const {
436 return &blacklisted_extensions_;
437}
438
[email protected]9e9c1d12013-07-31 01:58:12439const ExtensionSet* ExtensionService::delayed_installs() const {
440 return &delayed_installs_;
441}
442
[email protected]695b5712012-12-06 23:55:28443scoped_ptr<const ExtensionSet>
444 ExtensionService::GenerateInstalledExtensionsSet() const {
445 scoped_ptr<ExtensionSet> installed_extensions(new ExtensionSet());
[email protected]7f4308d2012-01-18 07:43:01446 installed_extensions->InsertAll(extensions_);
447 installed_extensions->InsertAll(disabled_extensions_);
448 installed_extensions->InsertAll(terminated_extensions_);
[email protected]695b5712012-12-06 23:55:28449 installed_extensions->InsertAll(blacklisted_extensions_);
450 return installed_extensions.PassAs<const ExtensionSet>();
[email protected]7f4308d2012-01-18 07:43:01451}
452
[email protected]3f213ad2012-07-26 23:39:41453extensions::PendingExtensionManager*
454 ExtensionService::pending_extension_manager() {
[email protected]b2907fd2011-03-25 16:43:37455 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45456}
457
[email protected]eaa7dd182010-12-14 11:09:00458ExtensionService::~ExtensionService() {
[email protected]c8d407e2011-04-28 21:27:17459 // No need to unload extensions here because they are profile-scoped, and the
460 // profile is in the process of being deleted.
[email protected]94b4ffc2011-02-04 05:55:17461
[email protected]5df038b2012-07-16 19:03:27462 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:14463 for (i = external_extension_providers_.begin();
464 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:27465 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:14466 provider->ServiceShutdown();
467 }
[email protected]6014d672008-12-05 00:38:25468}
469
[email protected]6d147912013-07-09 08:02:14470void ExtensionService::SetSyncStartFlare(
471 const syncer::SyncableService::StartSyncFlare& flare) {
472 flare_ = flare;
473}
474
[email protected]eaa7dd182010-12-14 11:09:00475void ExtensionService::InitEventRouters() {
[email protected]b3d62312b12010-10-14 21:10:18476 if (event_routers_initialized_)
477 return;
478
[email protected]6a3cd37e2012-04-17 17:13:34479#if defined(ENABLE_EXTENSIONS)
[email protected]5a38dfd2012-07-23 23:22:10480 browser_event_router_.reset(new extensions::BrowserEventRouter(profile_));
[email protected]6a3cd37e2012-04-17 17:13:34481#endif // defined(ENABLE_EXTENSIONS)
[email protected]b3d62312b12010-10-14 21:10:18482 event_routers_initialized_ = true;
[email protected]c5ae74ab2010-04-15 18:14:37483}
484
[email protected]037228a2012-10-05 01:36:16485void ExtensionService::Shutdown() {
486 // Do nothing for now.
487}
488
[email protected]b2907fd2011-03-25 16:43:37489const Extension* ExtensionService::GetExtensionById(
490 const std::string& id, bool include_disabled) const {
[email protected]81f0d162012-08-15 04:21:47491 int include_mask = INCLUDE_ENABLED;
[email protected]695b5712012-12-06 23:55:28492 if (include_disabled) {
493 // Include blacklisted extensions here because there are hundreds of
494 // callers of this function, and many might assume that this includes those
495 // that have been disabled due to blacklisting.
496 include_mask |= INCLUDE_DISABLED | INCLUDE_BLACKLISTED;
497 }
498 return GetExtensionById(id, include_mask);
499}
500
[email protected]399583b2012-12-11 09:33:42501GURL ExtensionService::GetSiteForExtensionId(const std::string& extension_id) {
502 return content::SiteInstance::GetSiteForURL(
503 profile_,
504 Extension::GetBaseURLFromExtensionId(extension_id));
505}
506
[email protected]695b5712012-12-06 23:55:28507const Extension* ExtensionService::GetExtensionById(
508 const std::string& id, int include_mask) const {
509 std::string lowercase_id = StringToLowerASCII(id);
510 if (include_mask & INCLUDE_ENABLED) {
511 const Extension* extension = extensions_.GetByID(lowercase_id);
512 if (extension)
513 return extension;
514 }
515 if (include_mask & INCLUDE_DISABLED) {
516 const Extension* extension = disabled_extensions_.GetByID(lowercase_id);
517 if (extension)
518 return extension;
519 }
520 if (include_mask & INCLUDE_TERMINATED) {
521 const Extension* extension = terminated_extensions_.GetByID(lowercase_id);
522 if (extension)
523 return extension;
524 }
525 if (include_mask & INCLUDE_BLACKLISTED) {
526 const Extension* extension = blacklisted_extensions_.GetByID(lowercase_id);
527 if (extension)
528 return extension;
529 }
530 return NULL;
[email protected]78994ab02010-12-08 18:06:44531}
532
[email protected]eaa7dd182010-12-14 11:09:00533void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12534 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20535
[email protected]4a10006a2013-05-17 23:18:35536 DCHECK(!is_ready()); // Can't redo init.
[email protected]93fd78f42009-07-10 16:43:17537 DCHECK_EQ(extensions_.size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32538
[email protected]820d9bd2013-04-03 03:46:03539 const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
[email protected]820d9bd2013-04-03 03:46:03540 if (cmd_line->HasSwitch(switches::kInstallFromWebstore) ||
541 cmd_line->HasSwitch(switches::kLimitedInstallFromWebstore)) {
542 // The sole purpose of this launch is to install a new extension from CWS
543 // and immediately terminate: loading already installed extensions is
544 // unnecessary and may interfere with the inline install dialog (e.g. if an
545 // extension listens to onStartup and opens a window).
546 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25547 } else {
[email protected]47b60072013-06-20 10:35:56548 // LoadAllExtensions() calls OnLoadedInstalledExtensions().
549 component_loader_->LoadAll();
550 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]9f4e4f082013-06-21 07:11:19551
552 // Finish install (if possible) of extensions that were still delayed while
553 // the browser was shut down.
554 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> delayed_info(
555 extension_prefs_->GetAllDelayedInstallInfo());
556 for (size_t i = 0; i < delayed_info->size(); ++i) {
557 ExtensionInfo* info = delayed_info->at(i).get();
558 scoped_refptr<const Extension> extension(NULL);
559 if (info->extension_manifest) {
560 std::string error;
561 extension = Extension::Create(
562 info->extension_path,
563 info->extension_location,
564 *info->extension_manifest,
565 extension_prefs_->GetDelayedInstallCreationFlags(
566 info->extension_id),
567 info->extension_id,
568 &error);
569 if (extension.get())
570 delayed_installs_.Insert(extension);
571 }
572 }
573 MaybeFinishDelayedInstallations();
574
575 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> delayed_info2(
576 extension_prefs_->GetAllDelayedInstallInfo());
577 UMA_HISTOGRAM_COUNTS_100("Extensions.UpdateOnLoad",
578 delayed_info2->size() - delayed_info->size());
579
[email protected]47b60072013-06-20 10:35:56580 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25581
[email protected]47b60072013-06-20 10:35:56582 // TODO(erikkay) this should probably be deferred to a future point
583 // rather than running immediately at startup.
584 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57585
[email protected]47b60072013-06-20 10:35:56586 base::MessageLoop::current()->PostDelayedTask(
587 FROM_HERE,
588 base::Bind(&ExtensionService::GarbageCollectExtensions, AsWeakPtr()),
589 base::TimeDelta::FromSeconds(kGarbageCollectStartupDelay));
[email protected]215a7be2012-10-22 19:53:42590
[email protected]820d9bd2013-04-03 03:46:03591 if (extension_prefs_->NeedsStorageGarbageCollection()) {
592 GarbageCollectIsolatedStorage();
593 extension_prefs_->SetNeedsStorageGarbageCollection(false);
594 }
[email protected]399583b2012-12-11 09:33:42595 }
[email protected]6014d672008-12-05 00:38:25596}
597
[email protected]31bb5ee62012-09-12 22:58:40598bool ExtensionService::UpdateExtension(const std::string& id,
[email protected]650b2d52013-02-10 03:41:45599 const base::FilePath& extension_path,
[email protected]31bb5ee62012-09-12 22:58:40600 const GURL& download_url,
601 CrxInstaller** out_crx_installer) {
[email protected]a29a517a2011-01-21 21:11:12602 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]3c4abc82012-10-22 22:25:54603 if (browser_terminating_) {
604 LOG(WARNING) << "Skipping UpdateExtension due to browser shutdown";
605 // Leak the temp file at extension_path. We don't want to add to the disk
606 // I/O burden at shutdown, we can't rely on the I/O completing anyway, and
607 // the file is in the OS temp directory which should be cleaned up for us.
608 return false;
609 }
[email protected]a8af9fdb2010-10-28 21:52:20610
[email protected]3f213ad2012-07-26 23:39:41611 const extensions::PendingExtensionInfo* pending_extension_info =
[email protected]51a3bf8b2012-06-08 22:53:06612 pending_extension_manager()->GetById(id);
[email protected]8ef78fd2010-08-19 17:14:32613
[email protected]695b5712012-12-06 23:55:28614 const Extension* extension = GetInstalledExtension(id);
[email protected]51a3bf8b2012-06-08 22:53:06615 if (!pending_extension_info && !extension) {
[email protected]aa142702010-03-26 01:26:33616 LOG(WARNING) << "Will not update extension " << id
617 << " because it is not installed or pending";
618 // Delete extension_path since we're not creating a CrxInstaller
619 // that would do it for us.
[email protected]7f8f24f2012-11-15 19:40:14620 if (!GetFileTaskRunner()->PostTask(
621 FROM_HERE,
[email protected]53612e82011-10-18 18:00:36622 base::Bind(
[email protected]7296f2762011-11-21 19:23:44623 &extension_file_util::DeleteFile, extension_path, false)))
[email protected]14908b72011-04-20 06:54:36624 NOTREACHED();
[email protected]420a0ec2011-06-01 01:07:03625
626 return false;
[email protected]e957fe52009-06-23 16:51:05627 }
628
[email protected]aa142702010-03-26 01:26:33629 // We want a silent install only for non-pending extensions and
630 // pending extensions that have install_silently set.
[email protected]91e51d612012-10-21 23:03:05631 ExtensionInstallPrompt* client = NULL;
632 if (pending_extension_info && !pending_extension_info->install_silently())
633 client = ExtensionInstallUI::CreateInstallPromptWithProfile(profile_);
[email protected]aa142702010-03-26 01:26:33634
[email protected]d8c8f25f2011-11-02 18:18:01635 scoped_refptr<CrxInstaller> installer(CrxInstaller::Create(this, client));
[email protected]6dfbbf82010-03-12 23:09:16636 installer->set_expected_id(id);
[email protected]51a3bf8b2012-06-08 22:53:06637 if (pending_extension_info) {
638 installer->set_install_source(pending_extension_info->install_source());
639 if (pending_extension_info->install_silently())
640 installer->set_allow_silent_install(true);
641 } else if (extension) {
[email protected]5eb375e92010-11-26 07:50:41642 installer->set_install_source(extension->location());
[email protected]51a3bf8b2012-06-08 22:53:06643 }
[email protected]7d8b7072012-04-07 01:07:46644 // If the extension was installed from or has migrated to the webstore, or
[email protected]fcb2c2c2012-10-17 21:08:45645 // its auto-update URL is from the webstore, treat it as a webstore install.
646 // Note that we ignore some older extensions with blank auto-update URLs
647 // because we are mostly concerned with restrictions on NaCl extensions,
648 // which are newer.
[email protected]a12ce8b22012-01-17 18:40:53649 int creation_flags = Extension::NO_FLAGS;
[email protected]75764512011-12-19 19:54:28650 if ((extension && extension->from_webstore()) ||
[email protected]4a5fe3e22013-06-04 07:06:38651 (extension && extensions::ManifestURL::UpdatesFromGallery(extension)) ||
[email protected]963d13c2012-09-29 17:13:35652 (!extension && extension_urls::IsWebstoreUpdateUrl(
[email protected]51a3bf8b2012-06-08 22:53:06653 pending_extension_info->update_url()))) {
[email protected]a12ce8b22012-01-17 18:40:53654 creation_flags |= Extension::FROM_WEBSTORE;
655 }
656
657 // Bookmark apps being updated is kind of a contradiction, but that's because
658 // we mark the default apps as bookmark apps, and they're hosted in the web
659 // store, thus they can get updated. See http://crbug.com/101605 for more
660 // details.
661 if (extension && extension->from_bookmark())
662 creation_flags |= Extension::FROM_BOOKMARK;
663
[email protected]e33bbc22012-08-27 22:05:46664 if (extension && extension->was_installed_by_default())
665 creation_flags |= Extension::WAS_INSTALLED_BY_DEFAULT;
666
[email protected]a12ce8b22012-01-17 18:40:53667 installer->set_creation_flags(creation_flags);
668
[email protected]6dfbbf82010-03-12 23:09:16669 installer->set_delete_source(true);
[email protected]655b2b1a2011-10-13 17:13:06670 installer->set_download_url(download_url);
[email protected]cb0e50312011-05-09 15:03:07671 installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
[email protected]6dfbbf82010-03-12 23:09:16672 installer->InstallCrx(extension_path);
[email protected]420a0ec2011-06-01 01:07:03673
674 if (out_crx_installer)
[email protected]dc24976f2013-06-02 21:15:09675 *out_crx_installer = installer.get();
[email protected]420a0ec2011-06-01 01:07:03676
677 return true;
[email protected]e957fe52009-06-23 16:51:05678}
679
[email protected]e178ad92013-06-28 02:29:25680void ExtensionService::ReloadExtension(const std::string extension_id) {
[email protected]a29a517a2011-01-21 21:11:12681 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]2a947202013-03-06 04:58:05682
683 // If the extension is already reloading, don't reload again.
684 if (extension_prefs_->GetDisableReasons(extension_id) &
685 Extension::DISABLE_RELOAD) {
686 return;
687 }
688
[email protected]650b2d52013-02-10 03:41:45689 base::FilePath path;
[email protected]9adb9692010-10-29 23:14:02690 const Extension* current_extension = GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40691
[email protected]f17dbd42010-08-16 23:21:10692 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]b65272f2009-08-31 15:47:06693 if (current_extension) {
[email protected]4814b512009-11-07 00:12:29694 // If the extension has an inspector open for its background page, detach
695 // the inspector and hang onto a cookie for it, so that we can reattach
696 // later.
[email protected]31d8f5f22012-04-02 15:22:08697 // TODO(yoz): this is not incognito-safe!
698 ExtensionProcessManager* manager = system_->process_manager();
[email protected]3a1dc572012-07-31 22:25:13699 extensions::ExtensionHost* host =
700 manager->GetBackgroundHostForExtension(extension_id);
[email protected]98f66112012-12-25 12:59:36701 if (host && DevToolsAgentHost::HasFor(host->render_view_host())) {
[email protected]4814b512009-11-07 00:12:29702 // Look for an open inspector for the background page.
[email protected]04ea1bb2013-07-10 09:26:09703 scoped_refptr<DevToolsAgentHost> agent_host =
704 DevToolsAgentHost::GetOrCreateFor(host->render_view_host());
705 agent_host->DisconnectRenderViewHost();
706 orphaned_dev_tools_[extension_id] = agent_host;
[email protected]4814b512009-11-07 00:12:29707 }
708
[email protected]b65272f2009-08-31 15:47:06709 path = current_extension->path();
[email protected]58076192013-07-19 19:43:50710 // BeingUpgraded is set back to false when the extension is added.
711 SetBeingUpgraded(current_extension, true);
[email protected]44d62b62012-04-11 00:06:03712 DisableExtension(extension_id, Extension::DISABLE_RELOAD);
[email protected]b914e2952013-04-26 07:10:03713 reloading_extensions_.insert(extension_id);
[email protected]1eb175082010-02-10 09:26:16714 } else {
715 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06716 }
717
[email protected]9f4e4f082013-06-21 07:11:19718 if (delayed_installs_.Contains(extension_id)) {
[email protected]6f6101832012-11-27 22:10:48719 FinishDelayedInstallation(extension_id);
[email protected]0db124b02012-11-07 04:55:05720 return;
721 }
722
[email protected]43ceb002012-02-10 23:19:15723 // If we're reloading a component extension, use the component extension
724 // loader's reloader.
725 if (component_loader_->Exists(extension_id)) {
[email protected]e178ad92013-06-28 02:29:25726 SetBeingReloaded(extension_id, true);
[email protected]43ceb002012-02-10 23:19:15727 component_loader_->Reload(extension_id);
[email protected]e178ad92013-06-28 02:29:25728 SetBeingReloaded(extension_id, false);
[email protected]43ceb002012-02-10 23:19:15729 return;
730 }
731
[email protected]e6090e42010-03-23 22:44:08732 // Check the installed extensions to see if what we're reloading was already
733 // installed.
[email protected]e178ad92013-06-28 02:29:25734 SetBeingReloaded(extension_id, true);
[email protected]e6090e42010-03-23 22:44:08735 scoped_ptr<ExtensionInfo> installed_extension(
736 extension_prefs_->GetInstalledExtensionInfo(extension_id));
737 if (installed_extension.get() &&
738 installed_extension->extension_manifest.get()) {
[email protected]d8c8f25f2011-11-02 18:18:01739 extensions::InstalledLoader(this).Load(*installed_extension, false);
[email protected]e6090e42010-03-23 22:44:08740 } else {
[email protected]d8c8f25f2011-11-02 18:18:01741 // Otherwise, the extension is unpacked (location LOAD).
[email protected]e6090e42010-03-23 22:44:08742 // We should always be able to remember the extension's path. If it's not in
743 // the map, someone failed to update |unloaded_extension_paths_|.
744 CHECK(!path.empty());
[email protected]d8c8f25f2011-11-02 18:18:01745 extensions::UnpackedInstaller::Create(this)->Load(path);
[email protected]e6090e42010-03-23 22:44:08746 }
[email protected]e178ad92013-06-28 02:29:25747 // When reloading is done, mark this extension as done reloading.
748 SetBeingReloaded(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40749}
750
[email protected]fa2416f2011-05-03 08:41:20751bool ExtensionService::UninstallExtension(
[email protected]a12c706e2011-12-01 00:58:08752 std::string extension_id,
[email protected]fa2416f2011-05-03 08:41:20753 bool external_uninstall,
[email protected]65187152012-06-02 13:14:14754 string16* error) {
[email protected]a29a517a2011-01-21 21:11:12755 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20756
[email protected]0d6ec3a72011-09-02 02:09:43757 scoped_refptr<const Extension> extension(GetInstalledExtension(extension_id));
[email protected]631cf822009-05-15 07:01:25758
[email protected]e7afe2452010-08-22 16:19:13759 // Callers should not send us nonexistent extensions.
[email protected]dc24976f2013-06-02 21:15:09760 CHECK(extension.get());
[email protected]9f1087e2009-06-15 17:29:32761
[email protected]95da88c42011-03-31 10:07:33762 // Policy change which triggers an uninstall will always set
763 // |external_uninstall| to true so this is the only way to uninstall
764 // managed extensions.
[email protected]65187152012-06-02 13:14:14765 if (!external_uninstall &&
766 !system_->management_policy()->UserMayModifySettings(
767 extension.get(), error)) {
[email protected]ad50def52011-10-19 23:17:07768 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27769 chrome::NOTIFICATION_EXTENSION_UNINSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:53770 content::Source<Profile>(profile_),
[email protected]dc24976f2013-06-02 21:15:09771 content::Details<const Extension>(extension.get()));
[email protected]d6ebc9792011-04-07 18:18:33772 return false;
773 }
[email protected]95da88c42011-03-31 10:07:33774
[email protected]3bdba0d2011-08-23 07:17:30775 // Extract the data we need for sync now, but don't actually sync until we've
776 // completed the uninstallation.
[email protected]6d147912013-07-09 08:02:14777 // TODO(tim): If we get here and IsSyncing is false, this will cause
778 // "back from the dead" style bugs, because sync will add-back the extension
779 // that was uninstalled here when MergeDataAndStartSyncing is called.
780 // See crbug.com/256795.
[email protected]65f173552012-06-28 22:43:58781 syncer::SyncChange sync_change;
[email protected]6d147912013-07-09 08:02:14782 if (extensions::sync_helper::IsSyncableApp(extension.get())) {
783 if (app_sync_bundle_.IsSyncing())
784 sync_change = app_sync_bundle_.CreateSyncChangeToDelete(extension.get());
785 else if (is_ready() && !flare_.is_null())
786 flare_.Run(syncer::APPS); // Tell sync to start ASAP.
787 } else if (extensions::sync_helper::IsSyncableExtension(extension.get())) {
788 if (extension_sync_bundle_.IsSyncing())
789 sync_change = extension_sync_bundle_.CreateSyncChangeToDelete(extension);
790 else if (is_ready() && !flare_.is_null())
791 flare_.Run(syncer::EXTENSIONS); // Tell sync to start ASAP.
[email protected]3bdba0d2011-08-23 07:17:30792 }
793
[email protected]dc24976f2013-06-02 21:15:09794 if (IsUnacknowledgedExternalExtension(extension.get())) {
[email protected]d96eb512012-11-01 23:44:08795 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
796 EXTERNAL_EXTENSION_UNINSTALLED,
797 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
798 }
[email protected]9b217652010-10-08 22:04:23799 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08800 extension->GetType(), 100);
[email protected]dc24976f2013-06-02 21:15:09801 RecordPermissionMessagesHistogram(extension.get(),
802 "Extensions.Permissions_Uninstall");
[email protected]9b217652010-10-08 22:04:23803
[email protected]831aa212010-03-26 13:55:19804 // Unload before doing more cleanup to ensure that nothing is hanging on to
805 // any of these resources.
[email protected]814a7bf0f2011-08-13 05:30:59806 UnloadExtension(extension_id, extension_misc::UNLOAD_REASON_UNINSTALL);
[email protected]831aa212010-03-26 13:55:19807
[email protected]0d6ec3a72011-09-02 02:09:43808 extension_prefs_->OnExtensionUninstalled(extension_id, extension->location(),
[email protected]831aa212010-03-26 13:55:19809 external_uninstall);
[email protected]9f1087e2009-06-15 17:29:32810
811 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]12075d12013-02-27 05:38:05812 if (!Manifest::IsUnpackedLocation(extension->location())) {
[email protected]7f8f24f2012-11-15 19:40:14813 if (!GetFileTaskRunner()->PostTask(
814 FROM_HERE,
[email protected]53612e82011-10-18 18:00:36815 base::Bind(
[email protected]14908b72011-04-20 06:54:36816 &extension_file_util::UninstallExtension,
817 install_directory_,
[email protected]fa2416f2011-05-03 08:41:20818 extension_id)))
[email protected]14908b72011-04-20 06:54:36819 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32820 }
821
[email protected]6b414c232013-06-05 07:53:34822 GURL launch_web_url_origin(
[email protected]cadac622013-06-11 16:46:36823 extensions::AppLaunchInfo::GetLaunchWebURL(extension.get()).GetOrigin());
[email protected]561e33d52013-04-03 06:58:43824 bool is_storage_isolated =
[email protected]dc24976f2013-06-02 21:15:09825 extensions::AppIsolationInfo::HasIsolatedStorage(extension.get());
[email protected]0d6ec3a72011-09-02 02:09:43826
[email protected]399583b2012-12-11 09:33:42827 if (is_storage_isolated) {
828 BrowserContext::AsyncObliterateStoragePartition(
829 profile_,
830 GetSiteForExtensionId(extension_id),
831 base::Bind(&ExtensionService::OnNeedsToGarbageCollectIsolatedStorage,
832 AsWeakPtr()));
833 } else {
834 if (extension->is_hosted_app() &&
835 !profile_->GetExtensionSpecialStoragePolicy()->
836 IsStorageProtected(launch_web_url_origin)) {
837 extensions::DataDeleter::StartDeleting(
838 profile_, extension_id, launch_web_url_origin);
839 }
840 extensions::DataDeleter::StartDeleting(profile_, extension_id,
841 extension->url());
[email protected]0d6ec3a72011-09-02 02:09:43842 }
[email protected]0d6ec3a72011-09-02 02:09:43843
[email protected]0dfe05c2011-02-23 23:03:36844 UntrackTerminatedExtension(extension_id);
[email protected]211030342010-09-30 18:41:06845
846 // Notify interested parties that we've uninstalled this extension.
[email protected]ad50def52011-10-19 23:17:07847 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27848 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
[email protected]6c2381d2011-10-19 02:52:53849 content::Source<Profile>(profile_),
[email protected]dc24976f2013-06-02 21:15:09850 content::Details<const Extension>(extension.get()));
[email protected]d6ebc9792011-04-07 18:18:33851
[email protected]5db9ada2012-04-11 13:48:20852 if (app_sync_bundle_.HasExtensionId(extension_id) &&
[email protected]a4a147652012-07-03 23:41:32853 sync_change.sync_data().GetDataType() == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:20854 app_sync_bundle_.ProcessDeletion(extension_id, sync_change);
855 } else if (extension_sync_bundle_.HasExtensionId(extension_id) &&
[email protected]a4a147652012-07-03 23:41:32856 sync_change.sync_data().GetDataType() == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:20857 extension_sync_bundle_.ProcessDeletion(extension_id, sync_change);
[email protected]3bdba0d2011-08-23 07:17:30858 }
859
[email protected]399583b2012-12-11 09:33:42860 delayed_installs_.Remove(extension_id);
[email protected]0db124b02012-11-07 04:55:05861
[email protected]198b5902013-06-27 10:36:11862 PruneSharedModulesOnUninstall(extension.get());
[email protected]9f4e4f082013-06-21 07:11:19863
[email protected]333b1de2011-09-12 18:28:50864 // Track the uninstallation.
[email protected]49098f702011-10-13 03:47:18865 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionUninstalled", 1, 2);
866
[email protected]d6ebc9792011-04-07 18:18:33867 return true;
[email protected]c10da4b02010-03-25 14:38:32868}
869
[email protected]c3cfb012011-04-06 22:07:35870bool ExtensionService::IsExtensionEnabled(
871 const std::string& extension_id) const {
[email protected]84df8332011-12-06 18:22:46872 if (extensions_.Contains(extension_id) ||
[email protected]dc9a74f72012-08-17 18:07:21873 terminated_extensions_.Contains(extension_id)) {
[email protected]36429da2011-07-11 20:25:18874 return true;
[email protected]dc9a74f72012-08-17 18:07:21875 }
[email protected]36429da2011-07-11 20:25:18876
[email protected]695b5712012-12-06 23:55:28877 if (disabled_extensions_.Contains(extension_id) ||
878 blacklisted_extensions_.Contains(extension_id)) {
[email protected]ad83ca242011-07-29 01:32:25879 return false;
[email protected]695b5712012-12-06 23:55:28880 }
[email protected]ad83ca242011-07-29 01:32:25881
882 // If the extension hasn't been loaded yet, check the prefs for it. Assume
883 // enabled unless otherwise noted.
884 return !extension_prefs_->IsExtensionDisabled(extension_id) &&
[email protected]f574c402012-12-04 23:20:31885 !extension_prefs_->IsExternalExtensionUninstalled(extension_id);
[email protected]c3cfb012011-04-06 22:07:35886}
887
888bool ExtensionService::IsExternalExtensionUninstalled(
889 const std::string& extension_id) const {
890 return extension_prefs_->IsExternalExtensionUninstalled(extension_id);
891}
892
[email protected]e2377032013-01-18 17:53:22893bool ExtensionService::IsExtensionEnabledForLauncher(
894 const std::string& extension_id) const {
895 return IsExtensionEnabled(extension_id) &&
896 !GetTerminatedExtension(extension_id);
897}
898
[email protected]eaa7dd182010-12-14 11:09:00899void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12900 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20901
[email protected]06f92562011-04-29 19:27:31902 if (IsExtensionEnabled(extension_id))
[email protected]0c6da502009-08-14 22:32:39903 return;
[email protected]0c6da502009-08-14 22:32:39904
[email protected]b3317ad2011-04-28 23:46:00905 extension_prefs_->SetExtensionState(extension_id, Extension::ENABLED);
[email protected]eb5e4f92012-08-15 23:33:28906 extension_prefs_->ClearDisableReasons(extension_id);
[email protected]1784e83a2009-09-08 21:01:52907
[email protected]695b5712012-12-06 23:55:28908 const Extension* extension = disabled_extensions_.GetByID(extension_id);
[email protected]06f92562011-04-29 19:27:31909 // This can happen if sync enables an extension that is not
910 // installed yet.
911 if (!extension)
912 return;
913
[email protected]d96eb512012-11-01 23:44:08914 if (IsUnacknowledgedExternalExtension(extension)) {
915 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
916 EXTERNAL_EXTENSION_REENABLED,
917 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]612a1cb12012-10-17 13:18:03918 AcknowledgeExternalExtension(extension->id());
[email protected]d96eb512012-11-01 23:44:08919 }
[email protected]612a1cb12012-10-17 13:18:03920
[email protected]0c6da502009-08-14 22:32:39921 // Move it over to the enabled list.
[email protected]84df8332011-12-06 18:22:46922 extensions_.Insert(make_scoped_refptr(extension));
923 disabled_extensions_.Remove(extension->id());
[email protected]0c6da502009-08-14 22:32:39924
[email protected]62d30f42009-10-01 22:36:06925 NotifyExtensionLoaded(extension);
[email protected]3bdba0d2011-08-23 07:17:30926
[email protected]f74d7f32012-06-19 19:22:51927 // Notify listeners that the extension was enabled.
928 content::NotificationService::current()->Notify(
929 chrome::NOTIFICATION_EXTENSION_ENABLED,
930 content::Source<Profile>(profile_),
931 content::Details<const Extension>(extension));
932
[email protected]3bdba0d2011-08-23 07:17:30933 SyncExtensionChangeIfNeeded(*extension);
[email protected]0c6da502009-08-14 22:32:39934}
935
[email protected]44d62b62012-04-11 00:06:03936void ExtensionService::DisableExtension(
937 const std::string& extension_id,
938 Extension::DisableReason disable_reason) {
[email protected]a29a517a2011-01-21 21:11:12939 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20940
[email protected]b2ba9962009-12-10 20:10:15941 // The extension may have been disabled already.
[email protected]06f92562011-04-29 19:27:31942 if (!IsExtensionEnabled(extension_id))
[email protected]1784e83a2009-09-08 21:01:52943 return;
[email protected]1784e83a2009-09-08 21:01:52944
[email protected]06f92562011-04-29 19:27:31945 const Extension* extension = GetInstalledExtension(extension_id);
946 // |extension| can be NULL if sync disables an extension that is not
947 // installed yet.
[email protected]65187152012-06-02 13:14:14948 if (extension &&
[email protected]505ccdc2013-06-20 20:15:35949 disable_reason != Extension::DISABLE_RELOAD &&
[email protected]65187152012-06-02 13:14:14950 !system_->management_policy()->UserMayModifySettings(extension, NULL)) {
[email protected]95da88c42011-03-31 10:07:33951 return;
[email protected]65187152012-06-02 13:14:14952 }
[email protected]95da88c42011-03-31 10:07:33953
[email protected]b3317ad2011-04-28 23:46:00954 extension_prefs_->SetExtensionState(extension_id, Extension::DISABLED);
[email protected]eb5e4f92012-08-15 23:33:28955 extension_prefs_->AddDisableReason(extension_id, disable_reason);
[email protected]1784e83a2009-09-08 21:01:52956
[email protected]695b5712012-12-06 23:55:28957 int include_mask = INCLUDE_EVERYTHING & ~INCLUDE_DISABLED;
958 extension = GetExtensionById(extension_id, include_mask);
[email protected]06f92562011-04-29 19:27:31959 if (!extension)
960 return;
961
[email protected]64742de32013-04-22 08:44:34962 // Reset the background_page_ready flag
963 if (extensions::BackgroundInfo::HasBackgroundPage(extension))
964 extension_runtime_data_[extension->id()].background_page_ready = false;
965
[email protected]5c094792012-09-07 19:44:53966 // Move it over to the disabled list. Don't send a second unload notification
967 // for terminated extensions being disabled.
[email protected]84df8332011-12-06 18:22:46968 disabled_extensions_.Insert(make_scoped_refptr(extension));
[email protected]5c094792012-09-07 19:44:53969 if (extensions_.Contains(extension->id())) {
[email protected]84df8332011-12-06 18:22:46970 extensions_.Remove(extension->id());
[email protected]5c094792012-09-07 19:44:53971 NotifyExtensionUnloaded(extension, extension_misc::UNLOAD_REASON_DISABLE);
972 } else {
[email protected]84df8332011-12-06 18:22:46973 terminated_extensions_.Remove(extension->id());
[email protected]5c094792012-09-07 19:44:53974 }
[email protected]3bdba0d2011-08-23 07:17:30975
976 SyncExtensionChangeIfNeeded(*extension);
[email protected]1784e83a2009-09-08 21:01:52977}
978
[email protected]1abf05e2013-07-09 17:04:36979void ExtensionService::DisableUserExtensions(
980 const std::vector<std::string>& except_ids) {
[email protected]e516e4c2013-06-12 17:41:14981 extensions::ManagementPolicy* management_policy =
982 system_->management_policy();
983 extensions::ExtensionList to_disable;
984
985 for (ExtensionSet::const_iterator extension = extensions_.begin();
986 extension != extensions_.end(); ++extension) {
[email protected]198b5902013-06-27 10:36:11987 if (management_policy->UserMayModifySettings(extension->get(), NULL))
[email protected]e516e4c2013-06-12 17:41:14988 to_disable.push_back(*extension);
989 }
990 for (ExtensionSet::const_iterator extension = terminated_extensions_.begin();
991 extension != terminated_extensions_.end(); ++extension) {
[email protected]198b5902013-06-27 10:36:11992 if (management_policy->UserMayModifySettings(extension->get(), NULL))
[email protected]e516e4c2013-06-12 17:41:14993 to_disable.push_back(*extension);
994 }
995
996 for (extensions::ExtensionList::const_iterator extension = to_disable.begin();
997 extension != to_disable.end(); ++extension) {
[email protected]1abf05e2013-07-09 17:04:36998 const std::string& id = (*extension)->id();
999 if (except_ids.end() == std::find(except_ids.begin(), except_ids.end(), id))
1000 DisableExtension(id, extensions::Extension::DISABLE_USER_ACTION);
[email protected]e516e4c2013-06-12 17:41:141001 }
1002}
1003
[email protected]eaa7dd182010-12-14 11:09:001004void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]009633c2013-03-07 22:08:281005 const Extension* extension) {
1006 GrantPermissions(extension);
[email protected]fe2dd7742011-04-19 22:52:491007 RecordPermissionMessagesHistogram(
1008 extension, "Extensions.Permissions_ReEnable");
[email protected]8d888c12010-11-30 00:00:251009 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
1010 EnableExtension(extension->id());
1011}
1012
[email protected]009633c2013-03-07 22:08:281013void ExtensionService::GrantPermissions(const Extension* extension) {
[email protected]be083862012-09-01 03:53:451014 CHECK(extension);
1015 extensions::PermissionsUpdater perms_updater(profile());
[email protected]009633c2013-03-07 22:08:281016 perms_updater.GrantActivePermissions(extension);
[email protected]be083862012-09-01 03:53:451017}
1018
[email protected]fe2dd7742011-04-19 22:52:491019// static
1020void ExtensionService::RecordPermissionMessagesHistogram(
[email protected]13c68b62013-05-17 11:29:051021 const Extension* extension, const char* histogram) {
[email protected]de415552013-01-23 04:12:171022 // Since this is called from multiple sources, and since the histogram macros
1023 // use statics, we need to manually lookup the histogram ourselves.
1024 base::HistogramBase* counter = base::LinearHistogram::FactoryGet(
[email protected]fe2dd7742011-04-19 22:52:491025 histogram,
1026 1,
[email protected]c2e66e12012-06-27 06:27:061027 PermissionMessage::kEnumBoundary,
1028 PermissionMessage::kEnumBoundary + 1,
[email protected]de415552013-01-23 04:12:171029 base::HistogramBase::kUmaTargetedHistogramFlag);
[email protected]fe2dd7742011-04-19 22:52:491030
[email protected]13c68b62013-05-17 11:29:051031 PermissionMessages permissions =
1032 extensions::PermissionsData::GetPermissionMessages(extension);
[email protected]fe2dd7742011-04-19 22:52:491033 if (permissions.empty()) {
[email protected]c2e66e12012-06-27 06:27:061034 counter->Add(PermissionMessage::kNone);
[email protected]fe2dd7742011-04-19 22:52:491035 } else {
[email protected]c2e66e12012-06-27 06:27:061036 for (PermissionMessages::iterator it = permissions.begin();
[email protected]0d3e4a22011-06-23 19:02:521037 it != permissions.end(); ++it)
1038 counter->Add(it->id());
[email protected]fe2dd7742011-04-19 22:52:491039 }
1040}
1041
[email protected]eaa7dd182010-12-14 11:09:001042void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]57a777f72010-03-31 01:09:421043 // The ChromeURLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:061044 // was loaded, otherwise a race can arise where a renderer that is created
1045 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:421046 // that the request context doesn't yet know about. The profile is responsible
1047 // for ensuring its URLRequestContexts appropriately discover the loaded
1048 // extension.
[email protected]31d8f5f22012-04-02 15:22:081049 system_->RegisterExtensionWithRequestContexts(extension);
[email protected]62d30f42009-10-01 22:36:061050
[email protected]d5949312012-12-03 22:13:301051 // Tell renderers about the new extension, unless it's a theme (renderers
1052 // don't need to know about themes).
[email protected]a58f599c2012-12-01 03:08:191053 if (!extension->is_theme()) {
[email protected]19647262011-12-16 09:57:491054 for (content::RenderProcessHost::iterator i(
1055 content::RenderProcessHost::AllHostsIterator());
1056 !i.IsAtEnd(); i.Advance()) {
1057 content::RenderProcessHost* host = i.GetCurrentValue();
1058 Profile* host_profile =
1059 Profile::FromBrowserContext(host->GetBrowserContext());
1060 if (host_profile->GetOriginalProfile() ==
1061 profile_->GetOriginalProfile()) {
1062 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions(
1063 1, ExtensionMsg_Loaded_Params(extension));
1064 host->Send(
1065 new ExtensionMsg_Loaded(loaded_extensions));
1066 }
[email protected]c8d407e2011-04-28 21:27:171067 }
[email protected]77a6970c2011-04-23 16:58:561068 }
1069
[email protected]3442a662012-01-12 08:06:171070 // Tell subsystems that use the EXTENSION_LOADED notification about the new
1071 // extension.
1072 //
1073 // NOTE: It is important that this happen after notifying the renderers about
1074 // the new extensions so that if we navigate to an extension URL in
1075 // NOTIFICATION_EXTENSION_LOADED, the renderer is guaranteed to know about it.
1076 content::NotificationService::current()->Notify(
1077 chrome::NOTIFICATION_EXTENSION_LOADED,
1078 content::Source<Profile>(profile_),
1079 content::Details<const Extension>(extension));
1080
[email protected]c8d407e2011-04-28 21:27:171081 // Tell a random-ass collection of other subsystems about the new extension.
1082 // TODO(aa): What should we do with all this goop? Can it move into the
1083 // relevant objects via EXTENSION_LOADED?
1084
1085 profile_->GetExtensionSpecialStoragePolicy()->
1086 GrantRightsForExtension(extension);
1087
1088 UpdateActiveExtensionsInCrashReporter();
1089
[email protected]c8d407e2011-04-28 21:27:171090 // If the extension has permission to load chrome://favicon/ resources we need
1091 // to make sure that the FaviconSource is registered with the
1092 // ChromeURLDataManager.
[email protected]13c68b62013-05-17 11:29:051093 if (extensions::PermissionsData::HasHostPermission(
1094 extension, GURL(chrome::kChromeUIFaviconURL))) {
[email protected]c8d407e2011-04-28 21:27:171095 FaviconSource* favicon_source = new FaviconSource(profile_,
1096 FaviconSource::FAVICON);
[email protected]24ea7a12013-01-27 23:54:531097 content::URLDataSource::Add(profile_, favicon_source);
[email protected]c8d407e2011-04-28 21:27:171098 }
[email protected]b07e606e2012-09-15 20:16:151099
1100#if !defined(OS_ANDROID)
1101 // Same for chrome://theme/ resources.
[email protected]13c68b62013-05-17 11:29:051102 if (extensions::PermissionsData::HasHostPermission(
1103 extension, GURL(chrome::kChromeUIThemeURL))) {
[email protected]b07e606e2012-09-15 20:16:151104 ThemeSource* theme_source = new ThemeSource(profile_);
[email protected]24ea7a12013-01-27 23:54:531105 content::URLDataSource::Add(profile_, theme_source);
[email protected]b07e606e2012-09-15 20:16:151106 }
1107#endif
1108
[email protected]5eddc3e2011-10-26 04:33:311109 // Same for chrome://thumb/ resources.
[email protected]13c68b62013-05-17 11:29:051110 if (extensions::PermissionsData::HasHostPermission(
1111 extension, GURL(chrome::kChromeUIThumbnailURL))) {
[email protected]5eddc3e2011-10-26 04:33:311112 ThumbnailSource* thumbnail_source = new ThumbnailSource(profile_);
[email protected]24ea7a12013-01-27 23:54:531113 content::URLDataSource::Add(profile_, thumbnail_source);
[email protected]5eddc3e2011-10-26 04:33:311114 }
[email protected]62d30f42009-10-01 22:36:061115}
1116
[email protected]a9f39a312010-12-23 22:14:271117void ExtensionService::NotifyExtensionUnloaded(
[email protected]814a7bf0f2011-08-13 05:30:591118 const Extension* extension,
1119 extension_misc::UnloadedExtensionReason reason) {
[email protected]a9f39a312010-12-23 22:14:271120 UnloadedExtensionInfo details(extension, reason);
[email protected]ad50def52011-10-19 23:17:071121 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271122 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]6c2381d2011-10-19 02:52:531123 content::Source<Profile>(profile_),
1124 content::Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061125
[email protected]903d70a2012-12-04 13:00:171126#if defined(ENABLE_THEMES)
1127 // If the current theme is being unloaded, tell ThemeService to revert back
1128 // to the default theme.
1129 if (reason != extension_misc::UNLOAD_REASON_UPDATE && extension->is_theme()) {
1130 ThemeService* theme_service = ThemeServiceFactory::GetForProfile(profile_);
1131 if (extension->id() == theme_service->GetThemeID())
1132 theme_service->UseDefaultTheme();
1133 }
1134#endif
1135
[email protected]f3b1a082011-11-18 00:34:301136 for (content::RenderProcessHost::iterator i(
1137 content::RenderProcessHost::AllHostsIterator());
[email protected]77a6970c2011-04-23 16:58:561138 !i.IsAtEnd(); i.Advance()) {
[email protected]f3b1a082011-11-18 00:34:301139 content::RenderProcessHost* host = i.GetCurrentValue();
[email protected]9b62ecf2011-07-27 20:23:081140 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:301141 Profile::FromBrowserContext(host->GetBrowserContext());
[email protected]9b62ecf2011-07-27 20:23:081142 if (host_profile->GetOriginalProfile() == profile_->GetOriginalProfile())
[email protected]c8d407e2011-04-28 21:27:171143 host->Send(new ExtensionMsg_Unloaded(extension->id()));
[email protected]77a6970c2011-04-23 16:58:561144 }
1145
[email protected]31d8f5f22012-04-02 15:22:081146 system_->UnregisterExtensionWithRequestContexts(extension->id(), reason);
[email protected]c8d407e2011-04-28 21:27:171147 profile_->GetExtensionSpecialStoragePolicy()->
1148 RevokeRightsForExtension(extension);
1149
[email protected]b777b332011-04-16 04:01:081150#if defined(OS_CHROMEOS)
[email protected]ac66e452013-01-11 09:21:231151 // Revoke external file access for the extension from its file system context.
1152 // It is safe to access the extension's storage partition at this point. The
1153 // storage partition may get destroyed only after the extension gets unloaded.
1154 GURL site = extensions::ExtensionSystem::Get(profile_)->extension_service()->
1155 GetSiteForExtensionId(extension->id());
[email protected]10eb28162012-09-18 03:04:091156 fileapi::FileSystemContext* filesystem_context =
[email protected]ac66e452013-01-11 09:21:231157 BrowserContext::GetStoragePartitionForSite(profile_, site)->
[email protected]10eb28162012-09-18 03:04:091158 GetFileSystemContext();
[email protected]f19bbf62013-07-09 01:22:321159 if (filesystem_context && filesystem_context->external_backend()) {
1160 filesystem_context->external_backend()->
[email protected]c8d407e2011-04-28 21:27:171161 RevokeAccessForExtension(extension->id());
[email protected]62d30f42009-10-01 22:36:061162 }
[email protected]c8d407e2011-04-28 21:27:171163#endif
1164
1165 UpdateActiveExtensionsInCrashReporter();
[email protected]62d30f42009-10-01 22:36:061166}
1167
[email protected]0a071a32011-02-08 00:18:241168Profile* ExtensionService::profile() {
1169 return profile_;
1170}
1171
[email protected]45759612012-07-10 17:21:231172extensions::ExtensionPrefs* ExtensionService::extension_prefs() {
[email protected]ddb1e5a2010-12-13 20:10:451173 return extension_prefs_;
1174}
1175
[email protected]0d9a2202011-11-09 13:48:411176extensions::SettingsFrontend* ExtensionService::settings_frontend() {
[email protected]cccdf0aa2011-11-11 03:43:381177 return settings_frontend_.get();
[email protected]b7f853e282011-08-10 09:24:201178}
1179
[email protected]e2d720aa2012-05-17 00:11:351180extensions::ContentSettingsStore* ExtensionService::GetContentSettingsStore() {
[email protected]b790b072011-05-20 09:46:441181 return extension_prefs()->content_settings_store();
1182}
1183
[email protected]25ae0152011-11-18 14:40:021184bool ExtensionService::is_ready() {
[email protected]4a10006a2013-05-17 23:18:351185 return ready_->is_signaled();
[email protected]25ae0152011-11-18 14:40:021186}
1187
[email protected]7f8f24f2012-11-15 19:40:141188base::SequencedTaskRunner* ExtensionService::GetFileTaskRunner() {
[email protected]dc24976f2013-06-02 21:15:091189 if (file_task_runner_.get())
1190 return file_task_runner_.get();
[email protected]7f8f24f2012-11-15 19:40:141191
1192 // We should be able to interrupt any part of extension install process during
1193 // shutdown. SKIP_ON_SHUTDOWN ensures that not started extension install tasks
1194 // will be ignored/deleted while we will block on started tasks.
1195 std::string token("ext_install-");
1196 token.append(profile_->GetPath().AsUTF8Unsafe());
1197 file_task_runner_ = BrowserThread::GetBlockingPool()->
1198 GetSequencedTaskRunnerWithShutdownBehavior(
1199 BrowserThread::GetBlockingPool()->GetNamedSequenceToken(token),
1200 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
[email protected]dc24976f2013-06-02 21:15:091201 return file_task_runner_.get();
[email protected]7f8f24f2012-11-15 19:40:141202}
1203
[email protected]42a08162012-03-16 18:09:111204extensions::ExtensionUpdater* ExtensionService::updater() {
[email protected]2859946f2011-04-04 18:18:061205 return updater_.get();
1206}
1207
[email protected]4ee07c62012-08-21 12:40:421208void ExtensionService::CheckManagementPolicy() {
[email protected]aa96d3a2010-08-21 08:45:251209 std::vector<std::string> to_be_removed;
[email protected]695b5712012-12-06 23:55:281210
[email protected]aa96d3a2010-08-21 08:45:251211 // Loop through extensions list, unload installed extensions.
[email protected]84df8332011-12-06 18:22:461212 for (ExtensionSet::const_iterator iter = extensions_.begin();
[email protected]aa96d3a2010-08-21 08:45:251213 iter != extensions_.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361214 const Extension* extension = (iter->get());
[email protected]4ee07c62012-08-21 12:40:421215 if (!system_->management_policy()->UserMayLoad(extension, NULL))
[email protected]aa96d3a2010-08-21 08:45:251216 to_be_removed.push_back(extension->id());
1217 }
1218
1219 // UnloadExtension will change the extensions_ list. So, we should
1220 // call it outside the iterator loop.
[email protected]31bb5ee62012-09-12 22:58:401221 for (size_t i = 0; i < to_be_removed.size(); ++i)
[email protected]814a7bf0f2011-08-13 05:30:591222 UnloadExtension(to_be_removed[i], extension_misc::UNLOAD_REASON_DISABLE);
[email protected]aa96d3a2010-08-21 08:45:251223}
1224
[email protected]31206602011-04-13 23:07:321225void ExtensionService::CheckForUpdatesSoon() {
[email protected]c3cfb012011-04-06 22:07:351226 if (updater()) {
[email protected]94fde232012-04-27 10:22:301227 if (AreAllExternalProvidersReady()) {
1228 updater()->CheckSoon();
1229 } else {
1230 // Sync can start updating before all the external providers are ready
1231 // during startup. Start the update as soon as those providers are ready,
1232 // but not before.
1233 update_once_all_providers_are_ready_ = true;
1234 }
[email protected]c3cfb012011-04-06 22:07:351235 } else {
[email protected]31206602011-04-13 23:07:321236 LOG(WARNING) << "CheckForUpdatesSoon() called with auto-update turned off";
[email protected]c3cfb012011-04-06 22:07:351237 }
1238}
1239
[email protected]3f98fd92012-11-17 15:54:291240syncer::SyncMergeResult ExtensionService::MergeDataAndStartSyncing(
[email protected]a4a147652012-07-03 23:41:321241 syncer::ModelType type,
[email protected]65f173552012-06-28 22:43:581242 const syncer::SyncDataList& initial_sync_data,
1243 scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
1244 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) {
[email protected]5db9ada2012-04-11 13:48:201245 CHECK(sync_processor.get());
[email protected]5d36e052012-04-20 20:12:281246 CHECK(sync_error_factory.get());
[email protected]3bdba0d2011-08-23 07:17:301247
1248 switch (type) {
[email protected]a4a147652012-07-03 23:41:321249 case syncer::EXTENSIONS:
[email protected]5db9ada2012-04-11 13:48:201250 extension_sync_bundle_.SetupSync(sync_processor.release(),
[email protected]5d36e052012-04-20 20:12:281251 sync_error_factory.release(),
[email protected]5db9ada2012-04-11 13:48:201252 initial_sync_data);
[email protected]3bdba0d2011-08-23 07:17:301253 break;
1254
[email protected]a4a147652012-07-03 23:41:321255 case syncer::APPS:
[email protected]5d36e052012-04-20 20:12:281256 app_sync_bundle_.SetupSync(sync_processor.release(),
1257 sync_error_factory.release(),
1258 initial_sync_data);
[email protected]3bdba0d2011-08-23 07:17:301259 break;
1260
1261 default:
1262 LOG(FATAL) << "Got " << type << " ModelType";
1263 }
[email protected]3bdba0d2011-08-23 07:17:301264
[email protected]b1417ab2011-12-15 17:09:111265 // Process local extensions.
1266 // TODO(yoz): Determine whether pending extensions should be considered too.
1267 // See crbug.com/104399.
[email protected]65f173552012-06-28 22:43:581268 syncer::SyncDataList sync_data_list = GetAllSyncData(type);
1269 syncer::SyncChangeList sync_change_list;
1270 for (syncer::SyncDataList::const_iterator i = sync_data_list.begin();
[email protected]3bdba0d2011-08-23 07:17:301271 i != sync_data_list.end();
1272 ++i) {
[email protected]5db9ada2012-04-11 13:48:201273 switch (type) {
[email protected]a4a147652012-07-03 23:41:321274 case syncer::EXTENSIONS:
[email protected]5db9ada2012-04-11 13:48:201275 sync_change_list.push_back(
1276 extension_sync_bundle_.CreateSyncChange(*i));
1277 break;
[email protected]a4a147652012-07-03 23:41:321278 case syncer::APPS:
[email protected]5db9ada2012-04-11 13:48:201279 sync_change_list.push_back(app_sync_bundle_.CreateSyncChange(*i));
1280 break;
1281 default:
1282 LOG(FATAL) << "Got " << type << " ModelType";
[email protected]31bb5ee62012-09-12 22:58:401283 }
[email protected]3bdba0d2011-08-23 07:17:301284 }
[email protected]3bdba0d2011-08-23 07:17:301285
[email protected]5db9ada2012-04-11 13:48:201286
[email protected]a4a147652012-07-03 23:41:321287 if (type == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:201288 extension_sync_bundle_.ProcessSyncChangeList(sync_change_list);
[email protected]a4a147652012-07-03 23:41:321289 } else if (type == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:201290 app_sync_bundle_.ProcessSyncChangeList(sync_change_list);
1291 }
[email protected]0ffd9e52012-01-26 21:44:341292
[email protected]3f98fd92012-11-17 15:54:291293 return syncer::SyncMergeResult(type);
[email protected]3bdba0d2011-08-23 07:17:301294}
1295
[email protected]a4a147652012-07-03 23:41:321296void ExtensionService::StopSyncing(syncer::ModelType type) {
1297 if (type == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:201298 app_sync_bundle_.Reset();
[email protected]a4a147652012-07-03 23:41:321299 } else if (type == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:201300 extension_sync_bundle_.Reset();
1301 }
[email protected]3bdba0d2011-08-23 07:17:301302}
1303
[email protected]65f173552012-06-28 22:43:581304syncer::SyncDataList ExtensionService::GetAllSyncData(
[email protected]a4a147652012-07-03 23:41:321305 syncer::ModelType type) const {
[email protected]31bb5ee62012-09-12 22:58:401306 if (type == syncer::EXTENSIONS)
[email protected]5db9ada2012-04-11 13:48:201307 return extension_sync_bundle_.GetAllSyncData();
[email protected]31bb5ee62012-09-12 22:58:401308 if (type == syncer::APPS)
[email protected]5db9ada2012-04-11 13:48:201309 return app_sync_bundle_.GetAllSyncData();
[email protected]5db9ada2012-04-11 13:48:201310
1311 // We should only get sync data for extensions and apps.
1312 NOTREACHED();
1313
[email protected]65f173552012-06-28 22:43:581314 return syncer::SyncDataList();
[email protected]3bdba0d2011-08-23 07:17:301315}
1316
[email protected]65f173552012-06-28 22:43:581317syncer::SyncError ExtensionService::ProcessSyncChanges(
[email protected]3bdba0d2011-08-23 07:17:301318 const tracked_objects::Location& from_here,
[email protected]65f173552012-06-28 22:43:581319 const syncer::SyncChangeList& change_list) {
1320 for (syncer::SyncChangeList::const_iterator i = change_list.begin();
[email protected]3bdba0d2011-08-23 07:17:301321 i != change_list.end();
1322 ++i) {
[email protected]a4a147652012-07-03 23:41:321323 syncer::ModelType type = i->sync_data().GetDataType();
1324 if (type == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:201325 extension_sync_bundle_.ProcessSyncChange(
1326 extensions::ExtensionSyncData(*i));
[email protected]a4a147652012-07-03 23:41:321327 } else if (type == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:201328 app_sync_bundle_.ProcessSyncChange(extensions::AppSyncData(*i));
1329 }
[email protected]3bdba0d2011-08-23 07:17:301330 }
1331
[email protected]0ffd9e52012-01-26 21:44:341332 extension_prefs()->extension_sorting()->FixNTPOrdinalCollisions();
1333
[email protected]65f173552012-06-28 22:43:581334 return syncer::SyncError();
[email protected]b05fb9ff2011-04-23 00:07:561335}
1336
[email protected]5db9ada2012-04-11 13:48:201337extensions::ExtensionSyncData ExtensionService::GetExtensionSyncData(
1338 const Extension& extension) const {
1339 return extensions::ExtensionSyncData(extension,
1340 IsExtensionEnabled(extension.id()),
1341 IsIncognitoEnabled(extension.id()));
[email protected]b05fb9ff2011-04-23 00:07:561342}
1343
[email protected]5db9ada2012-04-11 13:48:201344extensions::AppSyncData ExtensionService::GetAppSyncData(
1345 const Extension& extension) const {
1346 return extensions::AppSyncData(
1347 extension,
1348 IsExtensionEnabled(extension.id()),
1349 IsIncognitoEnabled(extension.id()),
[email protected]5db9ada2012-04-11 13:48:201350 extension_prefs_->extension_sorting()->GetAppLaunchOrdinal(
1351 extension.id()),
1352 extension_prefs_->extension_sorting()->GetPageOrdinal(extension.id()));
1353}
[email protected]3bdba0d2011-08-23 07:17:301354
[email protected]5db9ada2012-04-11 13:48:201355std::vector<extensions::ExtensionSyncData>
1356 ExtensionService::GetExtensionSyncDataList() const {
1357 std::vector<extensions::ExtensionSyncData> extension_sync_list;
1358 extension_sync_bundle_.GetExtensionSyncDataListHelper(extensions_,
1359 &extension_sync_list);
1360 extension_sync_bundle_.GetExtensionSyncDataListHelper(disabled_extensions_,
1361 &extension_sync_list);
1362 extension_sync_bundle_.GetExtensionSyncDataListHelper(terminated_extensions_,
1363 &extension_sync_list);
1364
1365 std::vector<extensions::ExtensionSyncData> pending_extensions =
1366 extension_sync_bundle_.GetPendingData();
1367 extension_sync_list.insert(extension_sync_list.begin(),
1368 pending_extensions.begin(),
1369 pending_extensions.end());
[email protected]3bdba0d2011-08-23 07:17:301370
1371 return extension_sync_list;
[email protected]b05fb9ff2011-04-23 00:07:561372}
1373
[email protected]5db9ada2012-04-11 13:48:201374std::vector<extensions::AppSyncData> ExtensionService::GetAppSyncDataList()
1375 const {
1376 std::vector<extensions::AppSyncData> app_sync_list;
1377 app_sync_bundle_.GetAppSyncDataListHelper(extensions_, &app_sync_list);
1378 app_sync_bundle_.GetAppSyncDataListHelper(disabled_extensions_,
1379 &app_sync_list);
1380 app_sync_bundle_.GetAppSyncDataListHelper(terminated_extensions_,
1381 &app_sync_list);
1382
1383 std::vector<extensions::AppSyncData> pending_apps =
1384 app_sync_bundle_.GetPendingData();
1385 app_sync_list.insert(app_sync_list.begin(),
1386 pending_apps.begin(),
1387 pending_apps.end());
1388
1389 return app_sync_list;
1390}
1391
1392bool ExtensionService::ProcessExtensionSyncData(
1393 const extensions::ExtensionSyncData& extension_sync_data) {
1394 if (!ProcessExtensionSyncDataHelper(extension_sync_data,
[email protected]a4a147652012-07-03 23:41:321395 syncer::EXTENSIONS)) {
[email protected]5db9ada2012-04-11 13:48:201396 extension_sync_bundle_.AddPendingExtension(extension_sync_data.id(),
1397 extension_sync_data);
1398 CheckForUpdatesSoon();
1399 return false;
1400 }
1401
1402 return true;
1403}
1404
1405bool ExtensionService::ProcessAppSyncData(
1406 const extensions::AppSyncData& app_sync_data) {
1407 const std::string& id = app_sync_data.id();
[email protected]5db9ada2012-04-11 13:48:201408
1409 if (app_sync_data.app_launch_ordinal().IsValid() &&
1410 app_sync_data.page_ordinal().IsValid()) {
1411 extension_prefs_->extension_sorting()->SetAppLaunchOrdinal(
1412 id,
1413 app_sync_data.app_launch_ordinal());
1414 extension_prefs_->extension_sorting()->SetPageOrdinal(
1415 id,
1416 app_sync_data.page_ordinal());
1417 }
1418
[email protected]5db9ada2012-04-11 13:48:201419 if (!ProcessExtensionSyncDataHelper(app_sync_data.extension_sync_data(),
[email protected]a4a147652012-07-03 23:41:321420 syncer::APPS)) {
[email protected]5db9ada2012-04-11 13:48:201421 app_sync_bundle_.AddPendingApp(id, app_sync_data);
1422 CheckForUpdatesSoon();
1423 return false;
1424 }
1425
1426 return true;
1427}
1428
1429bool ExtensionService::IsCorrectSyncType(const Extension& extension,
[email protected]a4a147652012-07-03 23:41:321430 syncer::ModelType type) const {
1431 if (type == syncer::EXTENSIONS &&
[email protected]8f3bcbd2013-06-05 08:42:401432 extensions::sync_helper::IsSyncableExtension(&extension)) {
[email protected]5db9ada2012-04-11 13:48:201433 return true;
[email protected]31bb5ee62012-09-12 22:58:401434 }
[email protected]5db9ada2012-04-11 13:48:201435
[email protected]a4a147652012-07-03 23:41:321436 if (type == syncer::APPS &&
[email protected]8f3bcbd2013-06-05 08:42:401437 extensions::sync_helper::IsSyncableApp(&extension)) {
[email protected]5db9ada2012-04-11 13:48:201438 return true;
[email protected]31bb5ee62012-09-12 22:58:401439 }
[email protected]5db9ada2012-04-11 13:48:201440
1441 return false;
1442}
1443
1444bool ExtensionService::ProcessExtensionSyncDataHelper(
1445 const extensions::ExtensionSyncData& extension_sync_data,
[email protected]a4a147652012-07-03 23:41:321446 syncer::ModelType type) {
[email protected]3bdba0d2011-08-23 07:17:301447 const std::string& id = extension_sync_data.id();
[email protected]96e989b2011-08-30 19:35:061448 const Extension* extension = GetInstalledExtension(id);
1449
1450 // TODO(bolms): we should really handle this better. The particularly bad
1451 // case is where an app becomes an extension or vice versa, and we end up with
1452 // a zombie extension that won't go away.
[email protected]5db9ada2012-04-11 13:48:201453 if (extension && !IsCorrectSyncType(*extension, type))
1454 return true;
[email protected]90310d92011-04-17 07:35:041455
1456 // Handle uninstalls first.
[email protected]3bdba0d2011-08-23 07:17:301457 if (extension_sync_data.uninstalled()) {
[email protected]90310d92011-04-17 07:35:041458 std::string error;
1459 if (!UninstallExtensionHelper(this, id)) {
1460 LOG(WARNING) << "Could not uninstall extension " << id
1461 << " for sync";
1462 }
[email protected]5db9ada2012-04-11 13:48:201463 return true;
[email protected]90310d92011-04-17 07:35:041464 }
1465
[email protected]caf2d442012-05-04 10:00:501466 // Extension from sync was uninstalled by the user as external extensions.
1467 // Honor user choice and skip installation/enabling.
1468 if (IsExternalExtensionUninstalled(id)) {
1469 LOG(WARNING) << "Extension with id " << id
1470 << " from sync was uninstalled as external extension";
1471 return true;
1472 }
1473
[email protected]06f92562011-04-29 19:27:311474 // Set user settings.
[email protected]7e9e2422012-12-13 19:54:191475 // If the extension has been disabled from sync, it may not have
1476 // been installed yet, so we don't know if the disable reason was a
[email protected]b914e2952013-04-26 07:10:031477 // permissions increase. That will be updated once CheckPermissionsIncrease
[email protected]7e9e2422012-12-13 19:54:191478 // is called for it.
[email protected]31bb5ee62012-09-12 22:58:401479 if (extension_sync_data.enabled())
[email protected]06f92562011-04-29 19:27:311480 EnableExtension(id);
[email protected]31bb5ee62012-09-12 22:58:401481 else
[email protected]7e9e2422012-12-13 19:54:191482 DisableExtension(id, Extension::DISABLE_UNKNOWN_FROM_SYNC);
[email protected]81b14cc2011-04-29 00:39:371483
[email protected]406b5a92011-11-08 11:58:261484 // We need to cache some version information here because setting the
1485 // incognito flag invalidates the |extension| pointer (it reloads the
1486 // extension).
[email protected]9060d8b02012-01-13 02:14:301487 bool extension_installed = (extension != NULL);
[email protected]406b5a92011-11-08 11:58:261488 int result = extension ?
1489 extension->version()->CompareTo(extension_sync_data.version()) : 0;
1490 SetIsIncognitoEnabled(id, extension_sync_data.incognito_enabled());
1491 extension = NULL; // No longer safe to use.
1492
1493 if (extension_installed) {
[email protected]06f92562011-04-29 19:27:311494 // If the extension is already installed, check if it's outdated.
[email protected]90310d92011-04-17 07:35:041495 if (result < 0) {
1496 // Extension is outdated.
[email protected]5db9ada2012-04-11 13:48:201497 return false;
[email protected]b020dc12011-12-06 04:35:421498 }
[email protected]06f92562011-04-29 19:27:311499 } else {
[email protected]06f92562011-04-29 19:27:311500 // TODO(akalin): Replace silent update with a list of enabled
1501 // permissions.
[email protected]6cc7dbae2011-04-29 21:18:331502 const bool kInstallSilently = true;
[email protected]5db9ada2012-04-11 13:48:201503
[email protected]a4a147652012-07-03 23:41:321504 CHECK(type == syncer::EXTENSIONS || type == syncer::APPS);
[email protected]8f3bcbd2013-06-05 08:42:401505 extensions::PendingExtensionInfo::ShouldAllowInstallPredicate filter =
1506 (type == syncer::APPS) ? extensions::sync_helper::IsSyncableApp :
1507 extensions::sync_helper::IsSyncableExtension;
[email protected]5db9ada2012-04-11 13:48:201508
[email protected]06f92562011-04-29 19:27:311509 if (!pending_extension_manager()->AddFromSync(
1510 id,
[email protected]3bdba0d2011-08-23 07:17:301511 extension_sync_data.update_url(),
[email protected]5db9ada2012-04-11 13:48:201512 filter,
[email protected]6cc7dbae2011-04-29 21:18:331513 kInstallSilently)) {
[email protected]06f92562011-04-29 19:27:311514 LOG(WARNING) << "Could not add pending extension for " << id;
[email protected]3bdba0d2011-08-23 07:17:301515 // This means that the extension is already pending installation, with a
1516 // non-INTERNAL location. Add to pending_sync_data, even though it will
1517 // never be removed (we'll never install a syncable version of the
1518 // extension), so that GetAllSyncData() continues to send it.
[email protected]06f92562011-04-29 19:27:311519 }
[email protected]3bdba0d2011-08-23 07:17:301520 // Track pending extensions so that we can return them in GetAllSyncData().
[email protected]5db9ada2012-04-11 13:48:201521 return false;
[email protected]90310d92011-04-17 07:35:041522 }
[email protected]5db9ada2012-04-11 13:48:201523
1524 return true;
[email protected]90310d92011-04-17 07:35:041525}
1526
[email protected]c3cfb012011-04-06 22:07:351527bool ExtensionService::IsIncognitoEnabled(
1528 const std::string& extension_id) const {
[email protected]cdc7b1f42012-12-07 19:39:481529 const Extension* extension = GetInstalledExtension(extension_id);
1530 if (extension && !extension->can_be_incognito_enabled())
1531 return false;
[email protected]c3cfb012011-04-06 22:07:351532 // If this is an existing component extension we always allow it to
1533 // work in incognito mode.
[email protected]1d5e58b2013-01-31 08:41:401534 if (extension && extension->location() == Manifest::COMPONENT)
[email protected]cb0ce1e022010-03-10 19:54:411535 return true;
[email protected]fc05749a2013-07-10 06:59:111536 if (extension && extension->force_incognito_enabled())
1537 return true;
[email protected]cb0ce1e022010-03-10 19:54:411538
1539 // Check the prefs.
[email protected]c3cfb012011-04-06 22:07:351540 return extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]db7331a2010-02-25 22:10:501541}
[email protected]55a35692010-02-11 23:25:211542
[email protected]c3cfb012011-04-06 22:07:351543void ExtensionService::SetIsIncognitoEnabled(
1544 const std::string& extension_id, bool enabled) {
[email protected]81b14cc2011-04-29 00:39:371545 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]cdc7b1f42012-12-07 19:39:481546 if (extension && !extension->can_be_incognito_enabled())
1547 return;
[email protected]1d5e58b2013-01-31 08:41:401548 if (extension && extension->location() == Manifest::COMPONENT) {
[email protected]b873cd92012-02-09 21:51:481549 // This shouldn't be called for component extensions unless they are
1550 // syncable.
[email protected]8f3bcbd2013-06-05 08:42:401551 DCHECK(extensions::sync_helper::IsSyncable(extension));
[email protected]b873cd92012-02-09 21:51:481552
1553 // If we are here, make sure the we aren't trying to change the value.
1554 DCHECK_EQ(enabled, IsIncognitoEnabled(extension_id));
1555
[email protected]c3cfb012011-04-06 22:07:351556 return;
1557 }
1558
[email protected]8c6c8c22011-03-09 12:52:341559 // Broadcast unloaded and loaded events to update browser state. Only bother
1560 // if the value changed and the extension is actually enabled, since there is
1561 // no UI otherwise.
[email protected]c3cfb012011-04-06 22:07:351562 bool old_enabled = extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]8c6c8c22011-03-09 12:52:341563 if (enabled == old_enabled)
1564 return;
1565
[email protected]c3cfb012011-04-06 22:07:351566 extension_prefs_->SetIsIncognitoEnabled(extension_id, enabled);
[email protected]81b14cc2011-04-29 00:39:371567
[email protected]ffbf3f12011-12-09 22:10:331568 bool extension_is_enabled = extensions_.Contains(extension_id);
[email protected]bd9892eb2011-11-11 19:41:341569
1570 // When we reload the extension the ID may be invalidated if we've passed it
1571 // by const ref everywhere. Make a copy to be safe.
1572 std::string id = extension_id;
[email protected]406b5a92011-11-08 11:58:261573 if (extension_is_enabled)
[email protected]ffbf3f12011-12-09 22:10:331574 ReloadExtension(id);
[email protected]3bdba0d2011-08-23 07:17:301575
[email protected]406b5a92011-11-08 11:58:261576 // Reloading the extension invalidates the |extension| pointer.
[email protected]bd9892eb2011-11-11 19:41:341577 extension = GetInstalledExtension(id);
[email protected]3bdba0d2011-08-23 07:17:301578 if (extension)
1579 SyncExtensionChangeIfNeeded(*extension);
[email protected]55a35692010-02-11 23:25:211580}
1581
[email protected]dc9a74f72012-08-17 18:07:211582bool ExtensionService::CanCrossIncognito(const Extension* extension) const {
[email protected]2a8f24e2010-11-03 21:37:051583 // We allow the extension to see events and data from another profile iff it
1584 // uses "spanning" behavior and it has incognito access. "split" mode
1585 // extensions only see events for a matching profile.
[email protected]0c41298f2011-09-13 21:17:221586 CHECK(extension);
[email protected]c3cfb012011-04-06 22:07:351587 return IsIncognitoEnabled(extension->id()) &&
[email protected]06492ed2013-03-24 22:13:141588 !extensions::IncognitoInfo::IsSplitMode(extension);
[email protected]2a8f24e2010-11-03 21:37:051589}
1590
[email protected]5ef7b002011-05-12 23:16:201591bool ExtensionService::CanLoadInIncognito(const Extension* extension) const {
1592 if (extension->is_hosted_app())
1593 return true;
1594 // Packaged apps and regular extensions need to be enabled specifically for
1595 // incognito (and split mode should be set).
[email protected]06492ed2013-03-24 22:13:141596 return extensions::IncognitoInfo::IsSplitMode(extension) &&
[email protected]5ef7b002011-05-12 23:16:201597 IsIncognitoEnabled(extension->id());
1598}
1599
[email protected]168389f2011-12-20 17:12:481600void ExtensionService::OnExtensionMoved(
1601 const std::string& moved_extension_id,
1602 const std::string& predecessor_extension_id,
1603 const std::string& successor_extension_id) {
[email protected]3569b502012-01-12 20:08:231604 extension_prefs_->extension_sorting()->OnExtensionMoved(
1605 moved_extension_id,
1606 predecessor_extension_id,
1607 successor_extension_id);
[email protected]168389f2011-12-20 17:12:481608
1609 const Extension* extension = GetInstalledExtension(moved_extension_id);
1610 if (extension)
1611 SyncExtensionChangeIfNeeded(*extension);
1612}
1613
[email protected]dc9a74f72012-08-17 18:07:211614bool ExtensionService::AllowFileAccess(const Extension* extension) const {
[email protected]05c82182010-06-24 17:49:081615 return (CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]334e04a2010-06-24 23:34:441616 switches::kDisableExtensionsFileAccessCheck) ||
[email protected]05c82182010-06-24 17:49:081617 extension_prefs_->AllowFileAccess(extension->id()));
1618}
1619
[email protected]eaa7dd182010-12-14 11:09:001620void ExtensionService::SetAllowFileAccess(const Extension* extension,
[email protected]3aff9ad2011-04-01 20:26:481621 bool allow) {
1622 // Reload to update browser state. Only bother if the value changed and the
1623 // extension is actually enabled, since there is no UI otherwise.
1624 bool old_allow = AllowFileAccess(extension);
1625 if (allow == old_allow)
1626 return;
1627
[email protected]05c82182010-06-24 17:49:081628 extension_prefs_->SetAllowFileAccess(extension->id(), allow);
[email protected]3aff9ad2011-04-01 20:26:481629
[email protected]84df8332011-12-06 18:22:461630 bool extension_is_enabled = extensions_.Contains(extension->id());
[email protected]3aff9ad2011-04-01 20:26:481631 if (extension_is_enabled)
1632 ReloadExtension(extension->id());
[email protected]05c82182010-06-24 17:49:081633}
1634
[email protected]8e4560b62011-01-14 10:09:141635// Some extensions will autoupdate themselves externally from Chrome. These
1636// are typically part of some larger client application package. To support
1637// these, the extension will register its location in the the preferences file
1638// (and also, on Windows, in the registry) and this code will periodically
1639// check that location for a .crx file, which it will then install locally if
1640// a new version is available.
1641// Errors are reported through ExtensionErrorReporter. Succcess is not
1642// reported.
[email protected]eaa7dd182010-12-14 11:09:001643void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:121644 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]8e4560b62011-01-14 10:09:141645
1646 // Note that this installation is intentionally silent (since it didn't
1647 // go through the front-end). Extensions that are registered in this
1648 // way are effectively considered 'pre-bundled', and so implicitly
1649 // trusted. In general, if something has HKLM or filesystem access,
1650 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:121651
[email protected]8e4560b62011-01-14 10:09:141652 // Ask each external extension provider to give us a call back for each
1653 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
[email protected]5df038b2012-07-16 19:03:271654 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141655 for (i = external_extension_providers_.begin();
1656 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:271657 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:141658 provider->VisitRegisteredExtension();
1659 }
1660
[email protected]50067e52011-10-20 23:17:071661 // Do any required work that we would have done after completion of all
1662 // providers.
1663 if (external_extension_providers_.empty()) {
1664 OnAllExternalProvidersReady();
1665 }
[email protected]9f1087e2009-06-15 17:29:321666}
1667
[email protected]50067e52011-10-20 23:17:071668void ExtensionService::OnExternalProviderReady(
[email protected]5df038b2012-07-16 19:03:271669 const extensions::ExternalProviderInterface* provider) {
[email protected]a29a517a2011-01-21 21:11:121670 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]50067e52011-10-20 23:17:071671 CHECK(provider->IsReady());
[email protected]a29a517a2011-01-21 21:11:121672
1673 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:141674 // if all of them are finished. So we check them first.
[email protected]94fde232012-04-27 10:22:301675 if (AreAllExternalProvidersReady())
1676 OnAllExternalProvidersReady();
1677}
1678
1679bool ExtensionService::AreAllExternalProvidersReady() const {
[email protected]5df038b2012-07-16 19:03:271680 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141681 for (i = external_extension_providers_.begin();
1682 i != external_extension_providers_.end(); ++i) {
[email protected]94fde232012-04-27 10:22:301683 if (!i->get()->IsReady())
1684 return false;
[email protected]8e4560b62011-01-14 10:09:141685 }
[email protected]94fde232012-04-27 10:22:301686 return true;
[email protected]50067e52011-10-20 23:17:071687}
1688
1689void ExtensionService::OnAllExternalProvidersReady() {
1690 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]94fde232012-04-27 10:22:301691 base::TimeDelta elapsed = base::Time::Now() - profile_->GetStartTime();
1692 UMA_HISTOGRAM_TIMES("Extension.ExternalProvidersReadyAfter", elapsed);
[email protected]50067e52011-10-20 23:17:071693
1694 // Install any pending extensions.
[email protected]94fde232012-04-27 10:22:301695 if (update_once_all_providers_are_ready_ && updater()) {
1696 update_once_all_providers_are_ready_ = false;
[email protected]42774e42012-11-14 21:01:061697 updater()->CheckNow(extensions::ExtensionUpdater::CheckParams());
[email protected]8e4560b62011-01-14 10:09:141698 }
1699
1700 // Uninstall all the unclaimed extensions.
[email protected]45759612012-07-10 17:21:231701 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> extensions_info(
[email protected]8e4560b62011-01-14 10:09:141702 extension_prefs_->GetInstalledExtensionsInfo());
1703 for (size_t i = 0; i < extensions_info->size(); ++i) {
1704 ExtensionInfo* info = extensions_info->at(i).get();
[email protected]1d5e58b2013-01-31 08:41:401705 if (Manifest::IsExternalLocation(info->extension_location))
[email protected]8e4560b62011-01-14 10:09:141706 CheckExternalUninstall(info->extension_id);
1707 }
[email protected]e5af875f2011-10-10 21:09:141708 IdentifyAlertableExtensions();
[email protected]e5af875f2011-10-10 21:09:141709}
1710
1711void ExtensionService::IdentifyAlertableExtensions() {
1712 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1713
[email protected]07c9f2f42012-02-29 18:45:221714 // Build up the lists of extensions that require acknowledgment. If this is
1715 // the first time, grandfather extensions that would have caused
1716 // notification.
[email protected]89226982012-07-16 20:09:181717 extension_error_ui_.reset(ExtensionErrorUI::Create(this));
[email protected]e5af875f2011-10-10 21:09:141718
[email protected]a9aa5932012-01-25 08:27:401719 bool did_show_alert = false;
[email protected]89226982012-07-16 20:09:181720 if (PopulateExtensionErrorUI(extension_error_ui_.get())) {
[email protected]460c6712013-04-24 07:20:011721 if (!is_first_run_) {
[email protected]a9aa5932012-01-25 08:27:401722 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]89226982012-07-16 20:09:181723 did_show_alert = extension_error_ui_->ShowErrorInBubbleView();
[email protected]e5af875f2011-10-10 21:09:141724 } else {
1725 // First run. Just acknowledge all the extensions, silently, by
1726 // shortcutting the display of the UI and going straight to the
1727 // callback for pressing the Accept button.
[email protected]a9aa5932012-01-25 08:27:401728 HandleExtensionAlertAccept();
[email protected]e5af875f2011-10-10 21:09:141729 }
1730 }
[email protected]a9aa5932012-01-25 08:27:401731
[email protected]612a1cb12012-10-17 13:18:031732 UpdateExternalExtensionAlert();
1733
[email protected]a9aa5932012-01-25 08:27:401734 if (!did_show_alert)
[email protected]89226982012-07-16 20:09:181735 extension_error_ui_.reset();
[email protected]e5af875f2011-10-10 21:09:141736}
1737
[email protected]89226982012-07-16 20:09:181738bool ExtensionService::PopulateExtensionErrorUI(
1739 ExtensionErrorUI* extension_error_ui) {
[email protected]07c9f2f42012-02-29 18:45:221740 bool needs_alert = false;
[email protected]695b5712012-12-06 23:55:281741
1742 // Extensions that are blacklisted.
1743 for (ExtensionSet::const_iterator it = blacklisted_extensions_.begin();
1744 it != blacklisted_extensions_.end(); ++it) {
1745 std::string id = (*it)->id();
1746 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(id)) {
1747 extension_error_ui->AddBlacklistedExtension(id);
1748 needs_alert = true;
1749 }
1750 }
1751
[email protected]07c9f2f42012-02-29 18:45:221752 for (ExtensionSet::const_iterator iter = extensions_.begin();
1753 iter != extensions_.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361754 const Extension* e = iter->get();
[email protected]695b5712012-12-06 23:55:281755
1756 // Extensions disabled by policy. Note: this no longer includes blacklisted
1757 // extensions, though we still show the same UI.
[email protected]fdd679b2012-11-15 20:49:391758 if (!system_->management_policy()->UserMayLoad(e, NULL)) {
[email protected]07c9f2f42012-02-29 18:45:221759 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(e->id())) {
[email protected]89226982012-07-16 20:09:181760 extension_error_ui->AddBlacklistedExtension(e->id());
[email protected]07c9f2f42012-02-29 18:45:221761 needs_alert = true;
1762 }
1763 }
[email protected]07c9f2f42012-02-29 18:45:221764 }
[email protected]695b5712012-12-06 23:55:281765
[email protected]07c9f2f42012-02-29 18:45:221766 return needs_alert;
1767}
1768
[email protected]a9aa5932012-01-25 08:27:401769void ExtensionService::HandleExtensionAlertClosed() {
[email protected]612a1cb12012-10-17 13:18:031770 const ExtensionIdSet* extension_ids =
1771 extension_error_ui_->get_blacklisted_extension_ids();
[email protected]e5af875f2011-10-10 21:09:141772 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1773 iter != extension_ids->end(); ++iter) {
1774 extension_prefs_->AcknowledgeBlacklistedExtension(*iter);
1775 }
[email protected]879a6092013-03-06 04:12:501776 extension_error_ui_.reset();
1777}
1778
1779void ExtensionService::HandleExtensionAlertAccept() {
[email protected]73a85e012013-04-04 10:45:301780 extension_error_ui_->Close();
[email protected]e5af875f2011-10-10 21:09:141781}
1782
[email protected]47fc70c2011-12-06 07:29:511783void ExtensionService::AcknowledgeExternalExtension(const std::string& id) {
1784 extension_prefs_->AcknowledgeExternalExtension(id);
[email protected]612a1cb12012-10-17 13:18:031785 UpdateExternalExtensionAlert();
[email protected]47fc70c2011-12-06 07:29:511786}
1787
[email protected]d96eb512012-11-01 23:44:081788bool ExtensionService::IsUnacknowledgedExternalExtension(
1789 const Extension* extension) {
[email protected]b82d18da2012-11-09 00:11:321790 if (!FeatureSwitch::prompt_for_external_extensions()->IsEnabled())
1791 return false;
1792
[email protected]1d5e58b2013-01-31 08:41:401793 return (Manifest::IsExternalLocation(extension->location()) &&
[email protected]d96eb512012-11-01 23:44:081794 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id()) &&
1795 !(extension_prefs_->GetDisableReasons(extension->id()) &
1796 Extension::DISABLE_SIDELOAD_WIPEOUT));
1797}
1798
[email protected]89226982012-07-16 20:09:181799void ExtensionService::HandleExtensionAlertDetails() {
1800 extension_error_ui_->ShowExtensions();
[email protected]73a85e012013-04-04 10:45:301801 // ShowExtensions may cause the error UI to close synchronously, e.g. if it
1802 // causes a navigation.
1803 if (extension_error_ui_)
1804 extension_error_ui_->Close();
[email protected]6c751e72010-11-23 10:11:101805}
1806
[email protected]612a1cb12012-10-17 13:18:031807void ExtensionService::UpdateExternalExtensionAlert() {
[email protected]41070e8d2012-10-24 01:34:361808 if (!FeatureSwitch::prompt_for_external_extensions()->IsEnabled())
[email protected]612a1cb12012-10-17 13:18:031809 return;
1810
1811 const Extension* extension = NULL;
1812 for (ExtensionSet::const_iterator iter = disabled_extensions_.begin();
1813 iter != disabled_extensions_.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361814 const Extension* e = iter->get();
[email protected]d96eb512012-11-01 23:44:081815 if (IsUnacknowledgedExternalExtension(e)) {
1816 extension = e;
1817 break;
[email protected]612a1cb12012-10-17 13:18:031818 }
1819 }
1820
1821 if (extension) {
[email protected]d96eb512012-11-01 23:44:081822 if (!extensions::HasExternalInstallError(this)) {
1823 if (extension_prefs_->IncrementAcknowledgePromptCount(extension->id()) >
[email protected]612a1cb12012-10-17 13:18:031824 kMaxExtensionAcknowledgePromptCount) {
[email protected]d96eb512012-11-01 23:44:081825 // Stop prompting for this extension, and check if there's another
1826 // one that needs prompting.
[email protected]612a1cb12012-10-17 13:18:031827 extension_prefs_->AcknowledgeExternalExtension(extension->id());
[email protected]d96eb512012-11-01 23:44:081828 UpdateExternalExtensionAlert();
1829 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
1830 EXTERNAL_EXTENSION_IGNORED,
1831 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
1832 return;
[email protected]612a1cb12012-10-17 13:18:031833 }
[email protected]b3aa7182013-04-25 04:45:231834 if (is_first_run_)
1835 extension_prefs_->SetExternalInstallFirstRun(extension->id());
1836 // first_run is true if the extension was installed during a first run
1837 // (even if it's post-first run now).
1838 bool first_run = extension_prefs_->IsExternalInstallFirstRun(
1839 extension->id());
1840 extensions::AddExternalInstallError(this, extension, first_run);
[email protected]612a1cb12012-10-17 13:18:031841 }
1842 } else {
1843 extensions::RemoveExternalInstallError(this);
1844 }
[email protected]612a1cb12012-10-17 13:18:031845}
1846
[email protected]a9f39a312010-12-23 22:14:271847void ExtensionService::UnloadExtension(
1848 const std::string& extension_id,
[email protected]814a7bf0f2011-08-13 05:30:591849 extension_misc::UnloadedExtensionReason reason) {
[email protected]27e469a2010-01-11 20:35:091850 // Make sure the extension gets deleted after we return from this function.
[email protected]695b5712012-12-06 23:55:281851 int include_mask = INCLUDE_EVERYTHING & ~INCLUDE_TERMINATED;
[email protected]9adb9692010-10-29 23:14:021852 scoped_refptr<const Extension> extension(
[email protected]695b5712012-12-06 23:55:281853 GetExtensionById(extension_id, include_mask));
[email protected]631cf822009-05-15 07:01:251854
[email protected]fa0c96732010-11-17 00:14:231855 // This method can be called via PostTask, so the extension may have been
1856 // unloaded by the time this runs.
[email protected]dc24976f2013-06-02 21:15:091857 if (!extension.get()) {
[email protected]dd163fb02011-05-04 22:22:171858 // In case the extension may have crashed/uninstalled. Allow the profile to
1859 // clean up its RequestContexts.
[email protected]31d8f5f22012-04-02 15:22:081860 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]fa0c96732010-11-17 00:14:231861 return;
[email protected]dd163fb02011-05-04 22:22:171862 }
[email protected]0c6da502009-08-14 22:32:391863
[email protected]20c8b942013-05-26 02:03:401864 // If uninstalling let RuntimeEventRouter know.
1865 if (reason == extension_misc::UNLOAD_REASON_UNINSTALL)
1866 extensions::RuntimeEventRouter::OnExtensionUninstalled(
1867 profile_, extension_id);
1868
[email protected]1eb175082010-02-10 09:26:161869 // Keep information about the extension so that we can reload it later
1870 // even if it's not permanently installed.
1871 unloaded_extension_paths_[extension->id()] = extension->path();
1872
[email protected]f17dbd42010-08-16 23:21:101873 // Clean up if the extension is meant to be enabled after a reload.
[email protected]b914e2952013-04-26 07:10:031874 reloading_extensions_.erase(extension->id());
[email protected]f17dbd42010-08-16 23:21:101875
[email protected]d7e9a862010-11-03 21:57:491876 // Clean up runtime data.
1877 extension_runtime_data_.erase(extension_id);
1878
[email protected]060d4972012-07-19 17:22:391879 if (disabled_extensions_.Contains(extension->id())) {
[email protected]082dfa12013-07-29 18:09:331880 UnloadedExtensionInfo details(extension.get(), reason);
1881 details.already_disabled = true;
[email protected]84df8332011-12-06 18:22:461882 disabled_extensions_.Remove(extension->id());
[email protected]082dfa12013-07-29 18:09:331883 content::NotificationService::current()->Notify(
1884 chrome::NOTIFICATION_EXTENSION_UNLOADED,
1885 content::Source<Profile>(profile_),
1886 content::Details<UnloadedExtensionInfo>(&details));
[email protected]dd163fb02011-05-04 22:22:171887 // Make sure the profile cleans up its RequestContexts when an already
1888 // disabled extension is unloaded (since they are also tracking the disabled
1889 // extensions).
[email protected]31d8f5f22012-04-02 15:22:081890 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]0c6da502009-08-14 22:32:391891 return;
1892 }
1893
[email protected]060d4972012-07-19 17:22:391894 // Remove the extension from our list.
[email protected]84df8332011-12-06 18:22:461895 extensions_.Remove(extension->id());
[email protected]631cf822009-05-15 07:01:251896
[email protected]a9f39a312010-12-23 22:14:271897 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]631cf822009-05-15 07:01:251898}
1899
[email protected]eaa7dd182010-12-14 11:09:001900void ExtensionService::UnloadAllExtensions() {
[email protected]31bb5ee62012-09-12 22:58:401901 profile_->GetExtensionSpecialStoragePolicy()->RevokeRightsForAllExtensions();
[email protected]c8d407e2011-04-28 21:27:171902
[email protected]84df8332011-12-06 18:22:461903 extensions_.Clear();
1904 disabled_extensions_.Clear();
1905 terminated_extensions_.Clear();
[email protected]d7e9a862010-11-03 21:57:491906 extension_runtime_data_.clear();
[email protected]cd500f72010-06-25 23:44:321907
[email protected]c6e4a3412009-06-24 15:45:291908 // TODO(erikkay) should there be a notification for this? We can't use
1909 // EXTENSION_UNLOADED since that implies that the extension has been disabled
1910 // or uninstalled, and UnloadAll is just part of shutdown.
[email protected]9f1087e2009-06-15 17:29:321911}
1912
[email protected]eaa7dd182010-12-14 11:09:001913void ExtensionService::ReloadExtensions() {
[email protected]9f1087e2009-06-15 17:29:321914 UnloadAllExtensions();
[email protected]eac88332012-12-26 17:57:451915 component_loader_->LoadAll();
[email protected]d8c8f25f2011-11-02 18:18:011916 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]4a10006a2013-05-17 23:18:351917 // Don't call SetReadyAndNotifyListeners() since tests call this multiple
1918 // times.
[email protected]9f1087e2009-06-15 17:29:321919}
1920
[email protected]eaa7dd182010-12-14 11:09:001921void ExtensionService::GarbageCollectExtensions() {
[email protected]2f8757c32012-06-19 19:17:471922 if (extension_prefs_->pref_service()->ReadOnly())
1923 return;
1924
[email protected]9bd9a6862012-11-29 09:24:221925 if (pending_extension_manager()->HasPendingExtensions()) {
1926 // Don't garbage collect while there are pending installations, which may
1927 // be using the temporary installation directory. Try to garbage collect
1928 // again later.
[email protected]b3a25092013-05-28 22:08:161929 base::MessageLoop::current()->PostDelayedTask(
[email protected]9bd9a6862012-11-29 09:24:221930 FROM_HERE,
1931 base::Bind(&ExtensionService::GarbageCollectExtensions, AsWeakPtr()),
1932 base::TimeDelta::FromSeconds(kGarbageCollectRetryDelay));
1933 return;
1934 }
1935
[email protected]45759612012-07-10 17:21:231936 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> info(
[email protected]2f8757c32012-06-19 19:17:471937 extension_prefs_->GetInstalledExtensionsInfo());
1938
[email protected]650b2d52013-02-10 03:41:451939 std::multimap<std::string, base::FilePath> extension_paths;
[email protected]2f8757c32012-06-19 19:17:471940 for (size_t i = 0; i < info->size(); ++i)
[email protected]0db124b02012-11-07 04:55:051941 extension_paths.insert(std::make_pair(info->at(i)->extension_id,
1942 info->at(i)->extension_path));
1943
[email protected]399583b2012-12-11 09:33:421944 info = extension_prefs_->GetAllDelayedInstallInfo();
[email protected]0db124b02012-11-07 04:55:051945 for (size_t i = 0; i < info->size(); ++i)
1946 extension_paths.insert(std::make_pair(info->at(i)->extension_id,
1947 info->at(i)->extension_path));
[email protected]2f8757c32012-06-19 19:17:471948
[email protected]7f8f24f2012-11-15 19:40:141949 if (!GetFileTaskRunner()->PostTask(
1950 FROM_HERE,
[email protected]2f8757c32012-06-19 19:17:471951 base::Bind(
1952 &extension_file_util::GarbageCollectExtensions,
1953 install_directory_,
[email protected]31bb5ee62012-09-12 22:58:401954 extension_paths))) {
[email protected]2f8757c32012-06-19 19:17:471955 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:401956 }
[email protected]f6ccd652010-11-17 00:48:341957
[email protected]f766a9d2012-07-11 01:57:441958#if defined(ENABLE_THEMES)
[email protected]f6ccd652010-11-17 00:48:341959 // Also garbage-collect themes. We check |profile_| to be
1960 // defensive; in the future, we may call GarbageCollectExtensions()
1961 // from somewhere other than Init() (e.g., in a timer).
1962 if (profile_) {
[email protected]d5949312012-12-03 22:13:301963 ThemeServiceFactory::GetForProfile(profile_)->RemoveUnusedThemes();
[email protected]f6ccd652010-11-17 00:48:341964 }
[email protected]f766a9d2012-07-11 01:57:441965#endif
[email protected]3cf4f0992009-02-03 23:00:301966}
1967
[email protected]fb82dcd2012-03-21 14:15:461968void ExtensionService::SyncExtensionChangeIfNeeded(const Extension& extension) {
[email protected]6d147912013-07-09 08:02:141969 if (extensions::sync_helper::IsSyncableApp(&extension)) {
1970 if (app_sync_bundle_.IsSyncing())
1971 app_sync_bundle_.SyncChangeIfNeeded(extension);
1972 else if (is_ready() && !flare_.is_null())
1973 flare_.Run(syncer::APPS);
1974 } else if (extensions::sync_helper::IsSyncableExtension(&extension)) {
1975 if (extension_sync_bundle_.IsSyncing())
1976 extension_sync_bundle_.SyncChangeIfNeeded(extension);
1977 else if (is_ready() && !flare_.is_null())
1978 flare_.Run(syncer::EXTENSIONS);
[email protected]fb82dcd2012-03-21 14:15:461979 }
1980}
1981
[email protected]820d9bd2013-04-03 03:46:031982void ExtensionService::SetReadyAndNotifyListeners() {
[email protected]4a10006a2013-05-17 23:18:351983 ready_->Signal();
[email protected]820d9bd2013-04-03 03:46:031984 content::NotificationService::current()->Notify(
1985 chrome::NOTIFICATION_EXTENSIONS_READY,
1986 content::Source<Profile>(profile_),
1987 content::NotificationService::NoDetails());
1988}
1989
[email protected]eaa7dd182010-12-14 11:09:001990void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]3eeddd892013-04-17 17:00:111991 if (updater_)
[email protected]93fd78f42009-07-10 16:43:171992 updater_->Start();
[email protected]fa6a9102010-11-22 15:38:501993
[email protected]bc151cf92013-02-12 04:57:261994 OnBlacklistUpdated();
[email protected]e72e8eb82009-06-18 17:21:511995}
1996
[email protected]fcb58a862012-05-01 01:03:151997void ExtensionService::AddExtension(const Extension* extension) {
[email protected]c8d407e2011-04-28 21:27:171998 // TODO(jstritar): We may be able to get rid of this branch by overriding the
1999 // default extension state to DISABLED when the --disable-extensions flag
2000 // is set (http://crbug.com/29067).
2001 if (!extensions_enabled() &&
2002 !extension->is_theme() &&
[email protected]1d5e58b2013-01-31 08:41:402003 extension->location() != Manifest::COMPONENT &&
2004 !Manifest::IsExternalLocation(extension->location())) {
[email protected]fcb58a862012-05-01 01:03:152005 return;
[email protected]330840c2012-08-29 22:21:012006 }
[email protected]c8d407e2011-04-28 21:27:172007
[email protected]b914e2952013-04-26 07:10:032008 bool is_extension_upgrade = false;
2009 if (const Extension* old = GetInstalledExtension(extension->id())) {
2010 is_extension_upgrade = true;
2011 DCHECK_NE(extension, old);
2012 // Other than for unpacked extensions, CrxInstaller should have guaranteed
2013 // that we aren't downgrading.
2014 if (!Manifest::IsUnpackedLocation(extension->location()))
2015 CHECK_GE(extension->version()->CompareTo(*(old->version())), 0);
2016 }
2017 SetBeingUpgraded(extension, is_extension_upgrade);
[email protected]c8d407e2011-04-28 21:27:172018
[email protected]1eb175082010-02-10 09:26:162019 // The extension is now loaded, remove its data from unloaded extension map.
2020 unloaded_extension_paths_.erase(extension->id());
2021
[email protected]bb7f40952011-01-13 00:21:202022 // If a terminated extension is loaded, remove it from the terminated list.
2023 UntrackTerminatedExtension(extension->id());
2024
[email protected]f17dbd42010-08-16 23:21:102025 // If the extension was disabled for a reload, then enable it.
[email protected]ca2e82022013-04-29 16:48:412026 bool reloading = reloading_extensions_.erase(extension->id()) > 0;
[email protected]f17dbd42010-08-16 23:21:102027
[email protected]b914e2952013-04-26 07:10:032028 // Check if the extension's privileges have changed and mark the
2029 // extension disabled if necessary.
2030 CheckPermissionsIncrease(extension, is_extension_upgrade);
2031
[email protected]ca2e82022013-04-29 16:48:412032 if (is_extension_upgrade && !reloading) {
2033 // To upgrade an extension in place, unload the old one and then load the
2034 // new one. ReloadExtension disables the extension, which is sufficient.
[email protected]b914e2952013-04-26 07:10:032035 UnloadExtension(extension->id(), extension_misc::UNLOAD_REASON_UPDATE);
2036 }
[email protected]0c6da502009-08-14 22:32:392037
[email protected]695b5712012-12-06 23:55:282038 if (extension_prefs_->IsExtensionBlacklisted(extension->id())) {
[email protected]ac875372013-02-28 04:36:092039 // Only prefs is checked for the blacklist. We rely on callers to check the
2040 // blacklist before calling into here, e.g. CrxInstaller checks before
2041 // installation, we check when loading installed extensions.
[email protected]695b5712012-12-06 23:55:282042 blacklisted_extensions_.Insert(extension);
[email protected]ca2e82022013-04-29 16:48:412043 } else if (!reloading &&
2044 extension_prefs_->IsExtensionDisabled(extension->id())) {
[email protected]6f6101832012-11-27 22:10:482045 disabled_extensions_.Insert(extension);
[email protected]3bdba0d2011-08-23 07:17:302046 SyncExtensionChangeIfNeeded(*extension);
[email protected]44d62b62012-04-11 00:06:032047 content::NotificationService::current()->Notify(
2048 chrome::NOTIFICATION_EXTENSION_UPDATE_DISABLED,
2049 content::Source<Profile>(profile_),
2050 content::Details<const Extension>(extension));
[email protected]62f051c2012-03-29 17:04:442051
[email protected]7e9e2422012-12-13 19:54:192052 // Show the extension disabled error if a permissions increase was the
2053 // only reason it was disabled.
2054 if (extension_prefs_->GetDisableReasons(extension->id()) ==
[email protected]44d62b62012-04-11 00:06:032055 Extension::DISABLE_PERMISSIONS_INCREASE) {
[email protected]62f051c2012-03-29 17:04:442056 extensions::AddExtensionDisabledError(this, extension);
2057 }
[email protected]ca2e82022013-04-29 16:48:412058 } else if (reloading) {
2059 // Replace the old extension with the new version.
2060 CHECK(!disabled_extensions_.Insert(extension));
2061 EnableExtension(extension->id());
[email protected]695b5712012-12-06 23:55:282062 } else {
2063 // All apps that are displayed in the launcher are ordered by their ordinals
2064 // so we must ensure they have valid ordinals.
2065 if (extension->RequiresSortOrdinal()) {
[email protected]bd36b652013-02-27 02:13:252066 if (!extension->ShouldDisplayInNewTabPage()) {
2067 extension_prefs_->extension_sorting()->MarkExtensionAsHidden(
2068 extension->id());
2069 }
[email protected]695b5712012-12-06 23:55:282070 extension_prefs_->extension_sorting()->EnsureValidOrdinals(
2071 extension->id(), syncer::StringOrdinal());
2072 }
2073
2074 extensions_.Insert(extension);
2075 SyncExtensionChangeIfNeeded(*extension);
2076 NotifyExtensionLoaded(extension);
[email protected]f574c402012-12-04 23:20:312077 }
[email protected]b914e2952013-04-26 07:10:032078 SetBeingUpgraded(extension, false);
[email protected]aab98a52009-12-02 03:22:352079}
2080
[email protected]8c484b742012-11-29 06:05:362081void ExtensionService::AddComponentExtension(const Extension* extension) {
2082 const std::string old_version_string(
2083 extension_prefs_->GetVersionString(extension->id()));
2084 const Version old_version(old_version_string);
2085
2086 if (!old_version.IsValid() || !old_version.Equals(*extension->version())) {
2087 VLOG(1) << "Component extension " << extension->name() << " ("
2088 << extension->id() << ") installing/upgrading from '"
2089 << old_version_string << "' to " << extension->version()->GetString();
2090
2091 AddNewOrUpdatedExtension(extension,
[email protected]399583b2012-12-11 09:33:422092 Extension::ENABLED_COMPONENT,
2093 syncer::StringOrdinal());
[email protected]8c484b742012-11-29 06:05:362094 return;
2095 }
2096
2097 AddExtension(extension);
2098}
2099
[email protected]b914e2952013-04-26 07:10:032100void ExtensionService::UpdateActivePermissions(const Extension* extension) {
[email protected]902fd7b2011-07-27 18:42:312101 // If the extension has used the optional permissions API, it will have a
2102 // custom set of active permissions defined in the extension prefs. Here,
2103 // we update the extension's active permissions based on the prefs.
[email protected]c2e66e12012-06-27 06:27:062104 scoped_refptr<PermissionSet> active_permissions =
[email protected]902fd7b2011-07-27 18:42:312105 extension_prefs()->GetActivePermissions(extension->id());
2106
[email protected]dc24976f2013-06-02 21:15:092107 if (active_permissions.get()) {
[email protected]902fd7b2011-07-27 18:42:312108 // We restrict the active permissions to be within the bounds defined in the
2109 // extension's manifest.
2110 // a) active permissions must be a subset of optional + default permissions
2111 // b) active permissions must contains all default permissions
[email protected]c2e66e12012-06-27 06:27:062112 scoped_refptr<PermissionSet> total_permissions =
2113 PermissionSet::CreateUnion(
[email protected]2cb5e302013-05-09 05:00:062114 extensions::PermissionsData::GetRequiredPermissions(extension),
2115 extensions::PermissionsData::GetOptionalPermissions(extension));
[email protected]902fd7b2011-07-27 18:42:312116
2117 // Make sure the active permissions contain no more than optional + default.
[email protected]c2e66e12012-06-27 06:27:062118 scoped_refptr<PermissionSet> adjusted_active =
2119 PermissionSet::CreateIntersection(
[email protected]902fd7b2011-07-27 18:42:312120 total_permissions.get(), active_permissions.get());
2121
2122 // Make sure the active permissions contain the default permissions.
[email protected]c2e66e12012-06-27 06:27:062123 adjusted_active = PermissionSet::CreateUnion(
[email protected]2cb5e302013-05-09 05:00:062124 extensions::PermissionsData::GetRequiredPermissions(extension),
2125 adjusted_active.get());
[email protected]902fd7b2011-07-27 18:42:312126
[email protected]c333e792012-01-06 16:57:392127 extensions::PermissionsUpdater perms_updater(profile());
[email protected]dc24976f2013-06-02 21:15:092128 perms_updater.UpdateActivePermissions(extension, adjusted_active.get());
[email protected]902fd7b2011-07-27 18:42:312129 }
[email protected]b914e2952013-04-26 07:10:032130}
2131
2132void ExtensionService::CheckPermissionsIncrease(const Extension* extension,
2133 bool is_extension_upgrade) {
2134 UpdateActivePermissions(extension);
[email protected]902fd7b2011-07-27 18:42:312135
[email protected]8d888c12010-11-30 00:00:252136 // We keep track of all permissions the user has granted each extension.
2137 // This allows extensions to gracefully support backwards compatibility
2138 // by including unknown permissions in their manifests. When the user
2139 // installs the extension, only the recognized permissions are recorded.
2140 // When the unknown permissions become recognized (e.g., through browser
2141 // upgrade), we can prompt the user to accept these new permissions.
2142 // Extensions can also silently upgrade to less permissions, and then
2143 // silently upgrade to a version that adds these permissions back.
2144 //
2145 // For example, pretend that Chrome 10 includes a permission "omnibox"
2146 // for an API that adds suggestions to the omnibox. An extension can
2147 // maintain backwards compatibility while still having "omnibox" in the
2148 // manifest. If a user installs the extension on Chrome 9, the browser
2149 // will record the permissions it recognized, not including "omnibox."
2150 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
2151 // will disable the extension and prompt the user to approve the increase
2152 // in privileges. The extension could then release a new version that
2153 // removes the "omnibox" permission. When the user upgrades, Chrome will
2154 // still remember that "omnibox" had been granted, so that if the
2155 // extension once again includes "omnibox" in an upgrade, the extension
2156 // can upgrade without requiring this user's approval.
[email protected]eb5e4f92012-08-15 23:33:282157 int disable_reasons = extension_prefs_->GetDisableReasons(extension->id());
[email protected]8d888c12010-11-30 00:00:252158
[email protected]211a3f32013-05-28 21:48:182159 bool auto_grant_permission =
2160 (!is_extension_upgrade && extension->was_installed_by_default()) ||
2161 chrome::IsRunningInForcedAppMode();
[email protected]b914e2952013-04-26 07:10:032162 // Silently grant all active permissions to default apps only on install.
2163 // After install they should behave like other apps.
[email protected]211a3f32013-05-28 21:48:182164 // Silently grant all active permissions to apps install in kiosk mode on both
2165 // install and update.
2166 if (auto_grant_permission)
[email protected]b914e2952013-04-26 07:10:032167 GrantPermissions(extension);
2168
2169 bool is_privilege_increase = false;
2170 // We only need to compare the granted permissions to the current permissions
2171 // if the extension is not allowed to silently increase its permissions.
[email protected]13c68b62013-05-17 11:29:052172 if (!extensions::PermissionsData::CanSilentlyIncreasePermissions(extension) &&
[email protected]211a3f32013-05-28 21:48:182173 !auto_grant_permission) {
[email protected]8d888c12010-11-30 00:00:252174 // Add all the recognized permissions if the granted permissions list
2175 // hasn't been initialized yet.
[email protected]c2e66e12012-06-27 06:27:062176 scoped_refptr<PermissionSet> granted_permissions =
[email protected]902fd7b2011-07-27 18:42:312177 extension_prefs_->GetGrantedPermissions(extension->id());
[email protected]0d3e4a22011-06-23 19:02:522178 CHECK(granted_permissions.get());
[email protected]8d888c12010-11-30 00:00:252179
2180 // Here, we check if an extension's privileges have increased in a manner
2181 // that requires the user's approval. This could occur because the browser
2182 // upgraded and recognized additional privileges, or an extension upgrades
2183 // to a version that requires additional privileges.
[email protected]cadac622013-06-11 16:46:362184 is_privilege_increase = granted_permissions->HasLessPrivilegesThan(
[email protected]a0a73052013-07-10 01:42:432185 extension->GetActivePermissions().get(), extension->GetType());
[email protected]8d888c12010-11-30 00:00:252186 }
2187
2188 if (is_extension_upgrade) {
[email protected]44d62b62012-04-11 00:06:032189 // If the extension was already disabled, suppress any alerts for becoming
2190 // disabled on permissions increase.
[email protected]b914e2952013-04-26 07:10:032191 bool previously_disabled =
2192 extension_prefs_->IsExtensionDisabled(extension->id());
[email protected]7e9e2422012-12-13 19:54:192193 // Legacy disabled extensions do not have a disable reason. Infer that if
2194 // there was no permission increase, it was likely disabled by the user.
2195 if (previously_disabled && disable_reasons == Extension::DISABLE_NONE &&
[email protected]b914e2952013-04-26 07:10:032196 !extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
[email protected]7e9e2422012-12-13 19:54:192197 disable_reasons |= Extension::DISABLE_USER_ACTION;
[email protected]44d62b62012-04-11 00:06:032198 }
[email protected]7e9e2422012-12-13 19:54:192199 // Extensions that came to us disabled from sync need a similar inference,
2200 // except based on the new version's permissions.
2201 if (previously_disabled &&
2202 disable_reasons == Extension::DISABLE_UNKNOWN_FROM_SYNC) {
2203 // Remove the DISABLE_UNKNOWN_FROM_SYNC reason.
2204 extension_prefs_->ClearDisableReasons(extension->id());
2205 if (!is_privilege_increase)
2206 disable_reasons |= Extension::DISABLE_USER_ACTION;
2207 }
[email protected]b914e2952013-04-26 07:10:032208 disable_reasons &= ~Extension::DISABLE_UNKNOWN_FROM_SYNC;
[email protected]8d888c12010-11-30 00:00:252209 }
2210
2211 // Extension has changed permissions significantly. Disable it. A
2212 // notification should be sent by the caller.
2213 if (is_privilege_increase) {
[email protected]7e9e2422012-12-13 19:54:192214 disable_reasons |= Extension::DISABLE_PERMISSIONS_INCREASE;
[email protected]fe2dd7742011-04-19 22:52:492215 if (!extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
2216 RecordPermissionMessagesHistogram(
2217 extension, "Extensions.Permissions_AutoDisable");
2218 }
[email protected]b3317ad2011-04-28 23:46:002219 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
[email protected]8d888c12010-11-30 00:00:252220 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
[email protected]b914e2952013-04-26 07:10:032221 }
2222 if (disable_reasons != Extension::DISABLE_NONE) {
[email protected]eb5e4f92012-08-15 23:33:282223 extension_prefs_->AddDisableReason(
2224 extension->id(),
2225 static_cast<Extension::DisableReason>(disable_reasons));
[email protected]8d888c12010-11-30 00:00:252226 }
2227}
2228
[email protected]eaa7dd182010-12-14 11:09:002229void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:392230 std::set<std::string> extension_ids;
[email protected]84df8332011-12-06 18:22:462231 for (ExtensionSet::const_iterator iter = extensions_.begin();
2232 iter != extensions_.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:362233 const Extension* extension = iter->get();
[email protected]1d5e58b2013-01-31 08:41:402234 if (!extension->is_theme() && extension->location() != Manifest::COMPONENT)
[email protected]84df8332011-12-06 18:22:462235 extension_ids.insert(extension->id());
[email protected]aab98a52009-12-02 03:22:352236 }
2237
2238 child_process_logging::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:252239}
2240
[email protected]9f4e4f082013-06-21 07:11:192241ExtensionService::ImportStatus ExtensionService::SatisfyImports(
2242 const Extension* extension) {
2243 ImportStatus status = IMPORT_STATUS_OK;
2244 std::vector<std::string> pending;
2245 // TODO(elijahtaylor): Message the user if there is a failure that is
2246 // unrecoverable.
2247 if (SharedModuleInfo::ImportsModules(extension)) {
2248 const std::vector<SharedModuleInfo::ImportInfo>& imports =
2249 SharedModuleInfo::GetImports(extension);
2250 std::vector<SharedModuleInfo::ImportInfo>::const_iterator i;
2251 for (i = imports.begin(); i != imports.end(); ++i) {
2252 Version version_required(i->minimum_version);
2253 const Extension* imported_module =
2254 GetExtensionById(i->extension_id, true);
2255 if (!imported_module) {
2256 if (extension->from_webstore()) {
2257 status = IMPORT_STATUS_UNSATISFIED;
2258 pending.push_back(i->extension_id);
2259 } else {
2260 return IMPORT_STATUS_UNRECOVERABLE;
2261 }
2262 } else if (!SharedModuleInfo::IsSharedModule(imported_module)) {
2263 return IMPORT_STATUS_UNRECOVERABLE;
2264 } else if (version_required.IsValid() &&
2265 imported_module->version()->CompareTo(version_required) < 0) {
2266 if (imported_module->from_webstore()) {
2267 status = IMPORT_STATUS_UNSATISFIED;
2268 } else {
2269 return IMPORT_STATUS_UNRECOVERABLE;
2270 }
2271 }
2272 }
2273 }
2274 if (status == IMPORT_STATUS_UNSATISFIED) {
2275 for (std::vector<std::string>::const_iterator iter = pending.begin();
2276 iter != pending.end();
2277 ++iter) {
2278 pending_extension_manager()->AddFromExtensionImport(
2279 *iter,
2280 extension_urls::GetWebstoreUpdateUrl(),
2281 IsSharedModule);
2282 }
2283 CheckForUpdatesSoon();
2284 }
2285 return status;
2286}
2287
2288scoped_ptr<const ExtensionSet>
2289 ExtensionService::GetDependentExtensions(const Extension* extension) {
2290 scoped_ptr<ExtensionSet> dependents(new ExtensionSet());
2291 scoped_ptr<ExtensionSet> set_to_check(new ExtensionSet());
2292 if (SharedModuleInfo::IsSharedModule(extension)) {
2293 set_to_check->InsertAll(disabled_extensions_);
2294 set_to_check->InsertAll(delayed_installs_);
2295 set_to_check->InsertAll(extensions_);
2296 for (ExtensionSet::const_iterator iter = set_to_check->begin();
2297 iter != set_to_check->end(); ++iter) {
[email protected]198b5902013-06-27 10:36:112298 if (SharedModuleInfo::ImportsExtensionById(iter->get(),
2299 extension->id())) {
[email protected]9f4e4f082013-06-21 07:11:192300 dependents->Insert(*iter);
2301 }
2302 }
2303 }
2304 return dependents.PassAs<const ExtensionSet>();
2305}
2306
2307void ExtensionService::PruneSharedModulesOnUninstall(
2308 const Extension* extension) {
2309 if (SharedModuleInfo::ImportsModules(extension)) {
2310 const std::vector<SharedModuleInfo::ImportInfo>& imports =
2311 SharedModuleInfo::GetImports(extension);
2312 std::vector<SharedModuleInfo::ImportInfo>::const_iterator i;
2313 for (i = imports.begin(); i != imports.end(); ++i) {
2314 const Extension* imported_module =
2315 GetExtensionById(i->extension_id, true);
2316 if (imported_module && imported_module->from_webstore()) {
2317 scoped_ptr<const ExtensionSet> dependents =
2318 GetDependentExtensions(imported_module);
2319 if (dependents->size() == 0) {
2320 UninstallExtension(i->extension_id, false, NULL);
2321 }
2322 }
2323 }
2324 }
2325}
2326
[email protected]8266d662011-07-12 21:53:262327void ExtensionService::OnExtensionInstalled(
[email protected]36a5c4c2011-12-14 16:34:502328 const Extension* extension,
[email protected]98270432012-09-11 20:51:242329 const syncer::StringOrdinal& page_ordinal,
[email protected]0db124b02012-11-07 04:55:052330 bool has_requirement_errors,
2331 bool wait_for_idle) {
[email protected]a29a517a2011-01-21 21:11:122332 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:202333
[email protected]c3cfb012011-04-06 22:07:352334 const std::string& id = extension->id();
[email protected]612a1cb12012-10-17 13:18:032335 bool initial_enable = ShouldEnableOnInstall(extension);
[email protected]3f213ad2012-07-26 23:39:412336 const extensions::PendingExtensionInfo* pending_extension_info = NULL;
[email protected]51a3bf8b2012-06-08 22:53:062337 if ((pending_extension_info = pending_extension_manager()->GetById(id))) {
[email protected]8f3bcbd2013-06-05 08:42:402338 if (!pending_extension_info->ShouldAllowInstall(extension)) {
[email protected]51a3bf8b2012-06-08 22:53:062339 pending_extension_manager()->Remove(id);
[email protected]8ef78fd2010-08-19 17:14:322340
[email protected]31bb5ee62012-09-12 22:58:402341 LOG(WARNING) << "ShouldAllowInstall() returned false for "
2342 << id << " of type " << extension->GetType()
[email protected]65348062013-01-15 07:27:222343 << " and update URL "
2344 << extensions::ManifestURL::GetUpdateURL(extension).spec()
[email protected]31bb5ee62012-09-12 22:58:402345 << "; not installing";
[email protected]ae6cb3022011-04-04 20:11:542346
[email protected]ad50def52011-10-19 23:17:072347 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272348 chrome::NOTIFICATION_EXTENSION_INSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:532349 content::Source<Profile>(profile_),
2350 content::Details<const Extension>(extension));
[email protected]ae6cb3022011-04-04 20:11:542351
[email protected]4416c5a2010-06-26 01:28:572352 // Delete the extension directory since we're not going to
2353 // load it.
[email protected]7f8f24f2012-11-15 19:40:142354 if (!GetFileTaskRunner()->PostTask(
2355 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362356 base::Bind(&extension_file_util::DeleteFile,
[email protected]31bb5ee62012-09-12 22:58:402357 extension->path(), true))) {
[email protected]14908b72011-04-20 06:54:362358 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:402359 }
[email protected]4416c5a2010-06-26 01:28:572360 return;
2361 }
[email protected]51a3bf8b2012-06-08 22:53:062362
2363 pending_extension_manager()->Remove(id);
[email protected]4416c5a2010-06-26 01:28:572364 } else {
[email protected]c3cfb012011-04-06 22:07:352365 // We explicitly want to re-enable an uninstalled external
2366 // extension; if we're here, that means the user is manually
2367 // installing the extension.
[email protected]6cc7dbae2011-04-29 21:18:332368 if (IsExternalExtensionUninstalled(id)) {
2369 initial_enable = true;
2370 }
[email protected]aa142702010-03-26 01:26:332371 }
2372
[email protected]98270432012-09-11 20:51:242373 // Unsupported requirements overrides the management policy.
2374 if (has_requirement_errors) {
2375 initial_enable = false;
2376 extension_prefs_->AddDisableReason(
2377 id, Extension::DISABLE_UNSUPPORTED_REQUIREMENT);
2378 // If the extension was disabled because of unsupported requirements but
2379 // now supports all requirements after an update and there are not other
2380 // disable reasons, enable it.
2381 } else if (extension_prefs_->GetDisableReasons(id) ==
2382 Extension::DISABLE_UNSUPPORTED_REQUIREMENT) {
2383 initial_enable = true;
2384 extension_prefs_->ClearDisableReasons(id);
2385 }
2386
[email protected]695b5712012-12-06 23:55:282387 if (!GetInstalledExtension(extension->id())) {
[email protected]07533022011-06-27 20:42:552388 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
2389 extension->GetType(), 100);
[email protected]69084dc2012-11-17 07:39:302390 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallSource",
[email protected]1d5e58b2013-01-31 08:41:402391 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]07533022011-06-27 20:42:552392 RecordPermissionMessagesHistogram(
2393 extension, "Extensions.Permissions_Install");
[email protected]69084dc2012-11-17 07:39:302394 } else {
2395 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateType",
2396 extension->GetType(), 100);
2397 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateSource",
[email protected]1d5e58b2013-01-31 08:41:402398 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]07533022011-06-27 20:42:552399 }
2400
[email protected]a39921b42012-02-28 03:42:542401 // Certain extension locations are specific enough that we can
2402 // auto-acknowledge any extension that came from one of them.
[email protected]1d5e58b2013-01-31 08:41:402403 if (extension->location() == Manifest::EXTERNAL_POLICY_DOWNLOAD)
[email protected]a39921b42012-02-28 03:42:542404 AcknowledgeExternalExtension(extension->id());
[email protected]399583b2012-12-11 09:33:422405 const Extension::State initial_state =
2406 initial_enable ? Extension::ENABLED : Extension::DISABLED;
[email protected]e7aa7b7e2012-11-27 04:51:222407 if (ShouldDelayExtensionUpdate(id, wait_for_idle)) {
[email protected]399583b2012-12-11 09:33:422408 extension_prefs_->SetDelayedInstallInfo(extension, initial_state,
[email protected]9f4e4f082013-06-21 07:11:192409 extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE, page_ordinal);
[email protected]0db124b02012-11-07 04:55:052410
2411 // Transfer ownership of |extension|.
[email protected]9f4e4f082013-06-21 07:11:192412 delayed_installs_.Insert(extension);
[email protected]0db124b02012-11-07 04:55:052413
2414 // Notify extension of available update.
2415 extensions::RuntimeEventRouter::DispatchOnUpdateAvailableEvent(
2416 profile_, id, extension->manifest()->value());
[email protected]75bdcb872013-03-13 00:41:452417
2418 // Notify observers that app update is available.
2419 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
2420 OnAppUpdateAvailable(extension->id()));
[email protected]0db124b02012-11-07 04:55:052421 return;
2422 }
2423
[email protected]9f4e4f082013-06-21 07:11:192424 ImportStatus status = SatisfyImports(extension);
2425 if (installs_delayed_for_gc()) {
[email protected]399583b2012-12-11 09:33:422426 extension_prefs_->SetDelayedInstallInfo(extension, initial_state,
[email protected]9f4e4f082013-06-21 07:11:192427 extensions::ExtensionPrefs::DELAY_REASON_GC, page_ordinal);
[email protected]399583b2012-12-11 09:33:422428 delayed_installs_.Insert(extension);
[email protected]9f4e4f082013-06-21 07:11:192429 } else if (status != IMPORT_STATUS_OK) {
2430 if (status == IMPORT_STATUS_UNSATISFIED) {
2431 extension_prefs_->SetDelayedInstallInfo(extension, initial_state,
2432 extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IMPORTS,
2433 page_ordinal);
2434 delayed_installs_.Insert(extension);
2435 }
[email protected]399583b2012-12-11 09:33:422436 } else {
2437 AddNewOrUpdatedExtension(extension, initial_state, page_ordinal);
2438 }
[email protected]8c484b742012-11-29 06:05:362439}
2440
2441void ExtensionService::AddNewOrUpdatedExtension(
2442 const Extension* extension,
[email protected]399583b2012-12-11 09:33:422443 Extension::State initial_state,
2444 const syncer::StringOrdinal& page_ordinal) {
[email protected]8c484b742012-11-29 06:05:362445 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2446
[email protected]4416c5a2010-06-26 01:28:572447 extension_prefs_->OnExtensionInstalled(
[email protected]8266d662011-07-12 21:53:262448 extension,
[email protected]8c484b742012-11-29 06:05:362449 initial_state,
[email protected]36a5c4c2011-12-14 16:34:502450 page_ordinal);
[email protected]25b34332009-06-05 21:53:192451
[email protected]9e9c1d12013-07-31 01:58:122452 delayed_installs_.Remove(extension->id());
2453
[email protected]6f6101832012-11-27 22:10:482454 FinishInstallation(extension);
2455}
2456
2457void ExtensionService::MaybeFinishDelayedInstallation(
2458 const std::string& extension_id) {
[email protected]9f4e4f082013-06-21 07:11:192459 // Check if the extension already got installed.
2460 if (!delayed_installs_.Contains(extension_id))
[email protected]6f6101832012-11-27 22:10:482461 return;
[email protected]9f4e4f082013-06-21 07:11:192462 extensions::ExtensionPrefs::DelayReason reason =
2463 extension_prefs_->GetDelayedInstallReason(extension_id);
2464
2465 // Check if the extension is idle. DELAY_REASON_NONE is used for older
2466 // preferences files that will not have set this field but it was previously
2467 // only used for idle updates.
2468 if ((reason == extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE ||
2469 reason == extensions::ExtensionPrefs::DELAY_REASON_NONE) &&
2470 is_ready() && !IsExtensionIdle(extension_id))
[email protected]6f6101832012-11-27 22:10:482471 return;
2472
[email protected]9f4e4f082013-06-21 07:11:192473 const Extension* extension = delayed_installs_.GetByID(extension_id);
2474 if (reason == extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IMPORTS) {
2475 ImportStatus status = SatisfyImports(extension);
2476 if (status != IMPORT_STATUS_OK) {
2477 if (status == IMPORT_STATUS_UNRECOVERABLE) {
2478 delayed_installs_.Remove(extension_id);
2479 // Make sure no version of the extension is actually installed, (i.e.,
2480 // that this delayed install was not an update).
2481 CHECK(!extension_prefs_->GetInstalledExtensionInfo(extension_id).get());
2482 extension_prefs_->DeleteExtensionPrefs(extension_id);
2483 }
2484 return;
2485 }
2486 }
2487
[email protected]6f6101832012-11-27 22:10:482488 FinishDelayedInstallation(extension_id);
2489}
2490
2491void ExtensionService::FinishDelayedInstallation(
2492 const std::string& extension_id) {
2493 scoped_refptr<const Extension> extension(
2494 GetPendingExtensionUpdate(extension_id));
[email protected]dc24976f2013-06-02 21:15:092495 CHECK(extension.get());
[email protected]9f4e4f082013-06-21 07:11:192496 delayed_installs_.Remove(extension_id);
[email protected]6f6101832012-11-27 22:10:482497
[email protected]399583b2012-12-11 09:33:422498 if (!extension_prefs_->FinishDelayedInstallInfo(extension_id))
[email protected]6f6101832012-11-27 22:10:482499 NOTREACHED();
2500
[email protected]dc24976f2013-06-02 21:15:092501 FinishInstallation(extension.get());
[email protected]6f6101832012-11-27 22:10:482502}
2503
2504void ExtensionService::FinishInstallation(const Extension* extension) {
[email protected]e7554c3f2013-05-29 00:36:562505 const extensions::Extension* existing_extension =
2506 GetInstalledExtension(extension->id());
2507 bool is_update = false;
2508 std::string old_name;
2509 if (existing_extension) {
2510 is_update = true;
2511 old_name = existing_extension->name();
2512 }
2513 extensions::InstalledExtensionInfo details(extension, is_update, old_name);
[email protected]fcb58a862012-05-01 01:03:152514 content::NotificationService::current()->Notify(
2515 chrome::NOTIFICATION_EXTENSION_INSTALLED,
2516 content::Source<Profile>(profile_),
[email protected]41bb80bd2013-05-03 10:56:022517 content::Details<const extensions::InstalledExtensionInfo>(&details));
[email protected]fcb58a862012-05-01 01:03:152518
[email protected]d96eb512012-11-01 23:44:082519 bool unacknowledged_external = IsUnacknowledgedExternalExtension(extension);
[email protected]41070e8d2012-10-24 01:34:362520
[email protected]399583b2012-12-11 09:33:422521 // Unpacked extensions default to allowing file access, but if that has been
2522 // overridden, don't reset the value.
[email protected]1d5e58b2013-01-31 08:41:402523 if (Manifest::ShouldAlwaysAllowFileAccess(extension->location()) &&
[email protected]399583b2012-12-11 09:33:422524 !extension_prefs_->HasAllowFileAccessSetting(extension->id())) {
2525 extension_prefs_->SetAllowFileAccess(extension->id(), true);
2526 }
2527
[email protected]6f6101832012-11-27 22:10:482528 AddExtension(extension);
[email protected]612a1cb12012-10-17 13:18:032529
[email protected]a51c9e92012-12-09 09:19:202530#if defined(ENABLE_THEMES)
[email protected]bc151cf92013-02-12 04:57:262531 // We do this here since AddExtension() is always called on browser startup,
2532 // and we only really care about the last theme installed.
[email protected]a51c9e92012-12-09 09:19:202533 // If that ever changes and we have to move this code somewhere
2534 // else, it should be somewhere that's not in the startup path.
2535 if (extension->is_theme() && extensions_.GetByID(extension->id())) {
2536 DCHECK_EQ(extensions_.GetByID(extension->id()), extension);
2537 // Now that the theme extension is visible from outside the
2538 // ExtensionService, notify the ThemeService about the
2539 // newly-installed theme.
2540 ThemeServiceFactory::GetForProfile(profile_)->SetTheme(extension);
2541 }
2542#endif
2543
[email protected]612a1cb12012-10-17 13:18:032544 // If this is a new external extension that was disabled, alert the user
[email protected]d96eb512012-11-01 23:44:082545 // so he can reenable it. We do this last so that it has already been
2546 // added to our list of extensions.
2547 if (unacknowledged_external) {
[email protected]612a1cb12012-10-17 13:18:032548 UpdateExternalExtensionAlert();
[email protected]d96eb512012-11-01 23:44:082549 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
2550 EXTERNAL_EXTENSION_INSTALLED,
2551 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
2552 }
[email protected]9f4e4f082013-06-21 07:11:192553
2554 // Check extensions that may have been delayed only because this shared module
2555 // was not available.
2556 if (SharedModuleInfo::IsSharedModule(extension)) {
2557 MaybeFinishDelayedInstallations();
2558 }
[email protected]4a190632009-05-09 01:07:422559}
2560
[email protected]76b65442012-11-17 14:11:482561const Extension* ExtensionService::GetPendingExtensionUpdate(
2562 const std::string& id) const {
[email protected]9f4e4f082013-06-21 07:11:192563 return delayed_installs_.GetByID(id);
[email protected]76b65442012-11-17 14:11:482564}
2565
[email protected]bb7f40952011-01-13 00:21:202566void ExtensionService::TrackTerminatedExtension(const Extension* extension) {
[email protected]84df8332011-12-06 18:22:462567 if (!terminated_extensions_.Contains(extension->id()))
2568 terminated_extensions_.Insert(make_scoped_refptr(extension));
[email protected]fa2416f2011-05-03 08:41:202569
[email protected]b3f7fe22011-11-11 19:27:562570 UnloadExtension(extension->id(), extension_misc::UNLOAD_REASON_TERMINATE);
[email protected]bb7f40952011-01-13 00:21:202571}
2572
2573void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
[email protected]fa2416f2011-05-03 08:41:202574 std::string lowercase_id = StringToLowerASCII(id);
[email protected]84df8332011-12-06 18:22:462575 terminated_extensions_.Remove(lowercase_id);
[email protected]bb7f40952011-01-13 00:21:202576}
2577
[email protected]0dfe05c2011-02-23 23:03:362578const Extension* ExtensionService::GetTerminatedExtension(
[email protected]8001df22011-04-28 19:59:472579 const std::string& id) const {
[email protected]695b5712012-12-06 23:55:282580 return GetExtensionById(id, INCLUDE_TERMINATED);
[email protected]8001df22011-04-28 19:59:472581}
2582
2583const Extension* ExtensionService::GetInstalledExtension(
2584 const std::string& id) const {
[email protected]695b5712012-12-06 23:55:282585 int include_mask = INCLUDE_ENABLED |
2586 INCLUDE_DISABLED |
2587 INCLUDE_TERMINATED |
2588 INCLUDE_BLACKLISTED;
2589 return GetExtensionById(id, include_mask);
[email protected]0dfe05c2011-02-23 23:03:362590}
2591
[email protected]eaa7dd182010-12-14 11:09:002592bool ExtensionService::ExtensionBindingsAllowed(const GURL& url) {
[email protected]615d88f2011-12-13 01:47:442593 // Allow bindings for all packaged extensions and component hosted apps.
[email protected]be9915fb2013-07-18 09:28:552594 const Extension* extension = extensions_.GetExtensionOrAppByURL(url);
[email protected]615d88f2011-12-13 01:47:442595 return extension && (!extension->is_hosted_app() ||
[email protected]1d5e58b2013-01-31 08:41:402596 extension->location() == Manifest::COMPONENT);
[email protected]6d2e60bd2010-06-03 22:37:392597}
2598
[email protected]4d007b312012-10-17 03:00:482599bool ExtensionService::ShouldBlockUrlInBrowserTab(GURL* url) {
[email protected]be9915fb2013-07-18 09:28:552600 const Extension* extension = extensions_.GetExtensionOrAppByURL(*url);
[email protected]4d007b312012-10-17 03:00:482601 if (extension && extension->is_platform_app()) {
2602 *url = GURL(chrome::kExtensionInvalidRequestURL);
2603 return true;
2604 }
2605
2606 return false;
2607}
2608
[email protected]9060d8b02012-01-13 02:14:302609bool ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:322610 const std::string& id,
[email protected]8e4560b62011-01-14 10:09:142611 const Version* version,
[email protected]650b2d52013-02-10 03:41:452612 const base::FilePath& path,
[email protected]1d5e58b2013-01-31 08:41:402613 Manifest::Location location,
[email protected]47fc70c2011-12-06 07:29:512614 int creation_flags,
2615 bool mark_acknowledged) {
[email protected]ab22ba42011-01-14 16:36:382616 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2617 CHECK(Extension::IdIsValid(id));
[email protected]79c833b52011-04-05 18:31:012618 if (extension_prefs_->IsExternalExtensionUninstalled(id))
[email protected]9060d8b02012-01-13 02:14:302619 return false;
[email protected]a8af9fdb2010-10-28 21:52:202620
[email protected]7577a5c52009-07-30 06:21:582621 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:492622 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:582623 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:022624 const Extension* existing = GetExtensionById(id, true);
[email protected]910f72ce2012-08-24 01:38:352625
[email protected]7577a5c52009-07-30 06:21:582626 if (existing) {
[email protected]910f72ce2012-08-24 01:38:352627 // The default apps will have the location set as INTERNAL. Since older
2628 // default apps are installed as EXTERNAL, we override them. However, if the
2629 // app is already installed as internal, then do the version check.
[email protected]31bb5ee62012-09-12 22:58:402630 // TODO(grv) : Remove after Q1-2013.
[email protected]910f72ce2012-08-24 01:38:352631 bool is_default_apps_migration =
[email protected]1d5e58b2013-01-31 08:41:402632 (location == Manifest::INTERNAL &&
2633 Manifest::IsExternalLocation(existing->location()));
[email protected]910f72ce2012-08-24 01:38:352634
2635 if (!is_default_apps_migration) {
2636 DCHECK(version);
2637
2638 switch (existing->version()->CompareTo(*version)) {
2639 case -1: // existing version is older, we should upgrade
2640 break;
2641 case 0: // existing version is same, do nothing
2642 return false;
2643 case 1: // existing version is newer, uh-oh
2644 LOG(WARNING) << "Found external version of extension " << id
2645 << "that is older than current version. Current version "
2646 << "is: " << existing->VersionString() << ". New "
2647 << "version is: " << version->GetString()
2648 << ". Keeping current version.";
2649 return false;
2650 }
[email protected]7577a5c52009-07-30 06:21:582651 }
2652 }
2653
[email protected]9060d8b02012-01-13 02:14:302654 // If the extension is already pending, don't start an install.
[email protected]e3987852012-05-04 10:06:302655 if (!pending_extension_manager()->AddFromExternalFile(
2656 id, location, *version)) {
[email protected]9060d8b02012-01-13 02:14:302657 return false;
[email protected]e3987852012-05-04 10:06:302658 }
[email protected]9c635f22010-12-02 09:36:362659
[email protected]14908b72011-04-20 06:54:362660 // no client (silent install)
[email protected]d8c8f25f2011-11-02 18:18:012661 scoped_refptr<CrxInstaller> installer(CrxInstaller::Create(this, NULL));
[email protected]6dfbbf82010-03-12 23:09:162662 installer->set_install_source(location);
2663 installer->set_expected_id(id);
[email protected]cb0e50312011-05-09 15:03:072664 installer->set_expected_version(*version);
2665 installer->set_install_cause(extension_misc::INSTALL_CAUSE_EXTERNAL_FILE);
[email protected]1bf73cc32011-10-26 22:38:312666 installer->set_creation_flags(creation_flags);
[email protected]88e8ec9152013-01-17 04:05:182667#if defined(OS_CHROMEOS)
2668 extensions::InstallLimiter::Get(profile_)->Add(installer, path);
2669#else
[email protected]6dfbbf82010-03-12 23:09:162670 installer->InstallCrx(path);
[email protected]88e8ec9152013-01-17 04:05:182671#endif
[email protected]47fc70c2011-12-06 07:29:512672
2673 // Depending on the source, a new external extension might not need a user
2674 // notification on installation. For such extensions, mark them acknowledged
2675 // now to suppress the notification.
2676 if (mark_acknowledged)
2677 AcknowledgeExternalExtension(id);
[email protected]9060d8b02012-01-13 02:14:302678
2679 return true;
[email protected]7577a5c52009-07-30 06:21:582680}
2681
[email protected]eaa7dd182010-12-14 11:09:002682void ExtensionService::ReportExtensionLoadError(
[email protected]650b2d52013-02-10 03:41:452683 const base::FilePath& extension_path,
[email protected]d11c8e92009-10-20 23:26:402684 const std::string &error,
[email protected]d11c8e92009-10-20 23:26:402685 bool be_noisy) {
[email protected]d8c8f25f2011-11-02 18:18:012686 content::NotificationService::current()->Notify(
2687 chrome::NOTIFICATION_EXTENSION_LOAD_ERROR,
2688 content::Source<Profile>(profile_),
2689 content::Details<const std::string>(&error));
[email protected]d11c8e92009-10-20 23:26:402690
[email protected]8a205c02011-02-04 20:41:332691 std::string path_str = UTF16ToUTF8(extension_path.LossyDisplayName());
[email protected]438afb12012-07-05 03:18:212692 string16 message = UTF8ToUTF16(base::StringPrintf(
[email protected]18d4b6c2010-09-21 03:21:042693 "Could not load extension from '%s'. %s",
[email protected]fc670822011-12-17 09:33:492694 path_str.c_str(), error.c_str()));
[email protected]d11c8e92009-10-20 23:26:402695 ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy);
2696}
2697
[email protected]eaa7dd182010-12-14 11:09:002698void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]3a1dc572012-07-31 22:25:132699 extensions::ExtensionHost* host) {
[email protected]406027c02010-09-27 08:03:182700 OrphanedDevTools::iterator iter =
[email protected]2d2f6cfc2011-05-06 21:09:332701 orphaned_dev_tools_.find(host->extension_id());
[email protected]406027c02010-09-27 08:03:182702 if (iter == orphaned_dev_tools_.end())
2703 return;
2704
[email protected]04ea1bb2013-07-10 09:26:092705 iter->second->ConnectRenderViewHost(host->render_view_host());
[email protected]406027c02010-09-27 08:03:182706 orphaned_dev_tools_.erase(iter);
2707}
2708
[email protected]432115822011-07-10 15:52:272709void ExtensionService::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:532710 const content::NotificationSource& source,
2711 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:272712 switch (type) {
[email protected]3c4abc82012-10-22 22:25:542713 case chrome::NOTIFICATION_APP_TERMINATING:
2714 // Shutdown has started. Don't start any more extension installs.
2715 // (We cannot use ExtensionService::Shutdown() for this because it
2716 // happens too late in browser teardown.)
2717 browser_terminating_ = true;
2718 break;
[email protected]432115822011-07-10 15:52:272719 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]6c2381d2011-10-19 02:52:532720 if (profile_ !=
2721 content::Source<Profile>(source).ptr()->GetOriginalProfile()) {
[email protected]bc535ee52010-08-31 18:40:322722 break;
[email protected]6c2381d2011-10-19 02:52:532723 }
[email protected]a4ed6282009-12-14 20:51:162724
[email protected]3a1dc572012-07-31 22:25:132725 extensions::ExtensionHost* host =
2726 content::Details<extensions::ExtensionHost>(details).ptr();
[email protected]f128af42010-08-05 18:05:262727
[email protected]fa2416f2011-05-03 08:41:202728 // Mark the extension as terminated and Unload it. We want it to
2729 // be in a consistent state: either fully working or not loaded
2730 // at all, but never half-crashed. We do it in a PostTask so
2731 // that other handlers of this notification will still have
2732 // access to the Extension and ExtensionHost.
[email protected]b3a25092013-05-28 22:08:162733 base::MessageLoop::current()->PostTask(
[email protected]14908b72011-04-20 06:54:362734 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362735 base::Bind(
[email protected]fa2416f2011-05-03 08:41:202736 &ExtensionService::TrackTerminatedExtension,
[email protected]d8c8f25f2011-11-02 18:18:012737 AsWeakPtr(),
[email protected]fa2416f2011-05-03 08:41:202738 host->extension()));
[email protected]31f77262009-12-02 20:48:532739 break;
2740 }
[email protected]432115822011-07-10 15:52:272741 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: {
[email protected]f3b1a082011-11-18 00:34:302742 content::RenderProcessHost* process =
2743 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192744 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302745 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192746 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2747 break;
2748
[email protected]15397b42012-05-16 23:56:062749 // Extensions need to know the channel for API restrictions.
2750 process->Send(new ExtensionMsg_SetChannel(
[email protected]5243df072012-07-11 21:41:012751 extensions::Feature::GetCurrentChannel()));
[email protected]15397b42012-05-16 23:56:062752
[email protected]4941fe92013-06-13 23:21:012753 // Platform apps need to know the system font.
2754 scoped_ptr<base::DictionaryValue> fonts(new base::DictionaryValue);
2755 webui::SetFontAndTextDirection(fonts.get());
2756 std::string font_family, font_size;
2757 fonts->GetString("fontfamily", &font_family);
2758 fonts->GetString("fontsize", &font_size);
2759 process->Send(new ExtensionMsg_SetSystemFont(
2760 font_family, font_size));
2761
[email protected]77a6970c2011-04-23 16:58:562762 // Valid extension function names, used to setup bindings in renderer.
2763 std::vector<std::string> function_names;
2764 ExtensionFunctionDispatcher::GetAllFunctionNames(&function_names);
2765 process->Send(new ExtensionMsg_SetFunctionNames(function_names));
[email protected]31f77262009-12-02 20:48:532766
[email protected]77a6970c2011-04-23 16:58:562767 // Scripting whitelist. This is modified by tests and must be communicated
2768 // to renderers.
2769 process->Send(new ExtensionMsg_SetScriptingWhitelist(
2770 *Extension::GetScriptingWhitelist()));
2771
2772 // Loaded extensions.
[email protected]9776e82e2011-11-15 02:17:532773 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions;
[email protected]84df8332011-12-06 18:22:462774 for (ExtensionSet::const_iterator iter = extensions_.begin();
2775 iter != extensions_.end(); ++iter) {
[email protected]19647262011-12-16 09:57:492776 // Renderers don't need to know about themes.
2777 if (!(*iter)->is_theme())
[email protected]cadac622013-06-11 16:46:362778 loaded_extensions.push_back(ExtensionMsg_Loaded_Params(iter->get()));
[email protected]77a6970c2011-04-23 16:58:562779 }
[email protected]9776e82e2011-11-15 02:17:532780 process->Send(new ExtensionMsg_Loaded(loaded_extensions));
[email protected]77a6970c2011-04-23 16:58:562781 break;
2782 }
[email protected]432115822011-07-10 15:52:272783 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
[email protected]f3b1a082011-11-18 00:34:302784 content::RenderProcessHost* process =
2785 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192786 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302787 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192788 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2789 break;
2790
[email protected]6bc04fd82011-12-04 02:29:352791 process_map_.RemoveAllFromProcess(process->GetID());
[email protected]6f371442011-11-09 06:45:462792 BrowserThread::PostTask(
2793 BrowserThread::IO, FROM_HERE,
2794 base::Bind(&ExtensionInfoMap::UnregisterAllExtensionsInProcess,
[email protected]31d8f5f22012-04-02 15:22:082795 system_->info_map(),
[email protected]f3b1a082011-11-18 00:34:302796 process->GetID()));
[email protected]da5683db2011-04-23 17:12:212797 break;
2798 }
[email protected]0db124b02012-11-07 04:55:052799 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: {
2800 extensions::ExtensionHost* host =
2801 content::Details<extensions::ExtensionHost>(details).ptr();
[email protected]e7aa7b7e2012-11-27 04:51:222802 std::string extension_id = host->extension_id();
[email protected]9f4e4f082013-06-21 07:11:192803 if (delayed_installs_.Contains(extension_id)) {
[email protected]e7aa7b7e2012-11-27 04:51:222804 // We were waiting for this extension to become idle, it now might have,
2805 // so maybe finish installation.
[email protected]b3a25092013-05-28 22:08:162806 base::MessageLoop::current()->PostDelayedTask(
[email protected]0db124b02012-11-07 04:55:052807 FROM_HERE,
[email protected]6f6101832012-11-27 22:10:482808 base::Bind(&ExtensionService::MaybeFinishDelayedInstallation,
[email protected]0db124b02012-11-07 04:55:052809 AsWeakPtr(), extension_id),
2810 base::TimeDelta::FromSeconds(kUpdateIdleDelay));
[email protected]0db124b02012-11-07 04:55:052811 }
2812 break;
2813 }
[email protected]92dd8d92013-02-26 00:41:082814 case chrome::NOTIFICATION_UPGRADE_RECOMMENDED: {
2815 // Notify extensions that chrome update is available.
2816 extensions::RuntimeEventRouter::DispatchOnBrowserUpdateAvailableEvent(
2817 profile_);
[email protected]75bdcb872013-03-13 00:41:452818
2819 // Notify observers that chrome update is available.
2820 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
2821 OnChromeUpdateAvailable());
[email protected]92dd8d92013-02-26 00:41:082822 break;
2823 }
[email protected]aa96d3a2010-08-21 08:45:252824
[email protected]4814b512009-11-07 00:12:292825 default:
2826 NOTREACHED() << "Unexpected notification type.";
2827 }
2828}
2829
[email protected]90bb38d2012-11-14 18:36:032830void ExtensionService::OnExtensionInstallPrefChanged() {
[email protected]a6a7ced2012-11-01 17:24:182831 IdentifyAlertableExtensions();
2832 CheckManagementPolicy();
2833}
2834
[email protected]eaa7dd182010-12-14 11:09:002835bool ExtensionService::HasApps() const {
[email protected]ec5b50d2010-10-09 16:35:182836 return !GetAppIds().empty();
2837}
[email protected]377011d2010-07-20 04:18:502838
[email protected]eaa7dd182010-12-14 11:09:002839ExtensionIdSet ExtensionService::GetAppIds() const {
[email protected]ec5b50d2010-10-09 16:35:182840 ExtensionIdSet result;
[email protected]84df8332011-12-06 18:22:462841 for (ExtensionSet::const_iterator it = extensions_.begin();
[email protected]377011d2010-07-20 04:18:502842 it != extensions_.end(); ++it) {
[email protected]1d5e58b2013-01-31 08:41:402843 if ((*it)->is_app() && (*it)->location() != Manifest::COMPONENT)
[email protected]ec5b50d2010-10-09 16:35:182844 result.insert((*it)->id());
[email protected]377011d2010-07-20 04:18:502845 }
2846
[email protected]ec5b50d2010-10-09 16:35:182847 return result;
[email protected]377011d2010-07-20 04:18:502848}
[email protected]d7e9a862010-11-03 21:57:492849
[email protected]dc9a74f72012-08-17 18:07:212850bool ExtensionService::IsBackgroundPageReady(const Extension* extension) const {
[email protected]9367eabc2013-03-01 01:29:292851 if (!extensions::BackgroundInfo::HasPersistentBackgroundPage(extension))
[email protected]dc9a74f72012-08-17 18:07:212852 return true;
2853 ExtensionRuntimeDataMap::const_iterator it =
2854 extension_runtime_data_.find(extension->id());
2855 return it == extension_runtime_data_.end() ? false :
2856 it->second.background_page_ready;
[email protected]d7e9a862010-11-03 21:57:492857}
2858
[email protected]eaa7dd182010-12-14 11:09:002859void ExtensionService::SetBackgroundPageReady(const Extension* extension) {
[email protected]9367eabc2013-03-01 01:29:292860 DCHECK(extensions::BackgroundInfo::HasBackgroundPage(extension));
[email protected]d7e9a862010-11-03 21:57:492861 extension_runtime_data_[extension->id()].background_page_ready = true;
[email protected]ad50def52011-10-19 23:17:072862 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272863 chrome::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY,
[email protected]6c2381d2011-10-19 02:52:532864 content::Source<const Extension>(extension),
[email protected]ad50def52011-10-19 23:17:072865 content::NotificationService::NoDetails());
[email protected]d7e9a862010-11-03 21:57:492866}
2867
[email protected]65992d52012-09-19 23:05:312868void ExtensionService::InspectBackgroundPage(const Extension* extension) {
2869 DCHECK(extension);
2870
[email protected]fdd679b2012-11-15 20:49:392871 ExtensionProcessManager* pm = system_->process_manager();
[email protected]65992d52012-09-19 23:05:312872 extensions::LazyBackgroundTaskQueue* queue =
[email protected]fdd679b2012-11-15 20:49:392873 system_->lazy_background_task_queue();
[email protected]65992d52012-09-19 23:05:312874
2875 extensions::ExtensionHost* host =
2876 pm->GetBackgroundHostForExtension(extension->id());
2877 if (host) {
2878 InspectExtensionHost(host);
2879 } else {
2880 queue->AddPendingTask(
2881 profile_, extension->id(),
2882 base::Bind(&ExtensionService::InspectExtensionHost,
2883 base::Unretained(this)));
2884 }
2885}
2886
[email protected]dc9a74f72012-08-17 18:07:212887bool ExtensionService::IsBeingUpgraded(const Extension* extension) const {
2888 ExtensionRuntimeDataMap::const_iterator it =
2889 extension_runtime_data_.find(extension->id());
2890 return it == extension_runtime_data_.end() ? false :
2891 it->second.being_upgraded;
[email protected]d7e9a862010-11-03 21:57:492892}
2893
[email protected]eaa7dd182010-12-14 11:09:002894void ExtensionService::SetBeingUpgraded(const Extension* extension,
[email protected]b914e2952013-04-26 07:10:032895 bool value) {
[email protected]d7e9a862010-11-03 21:57:492896 extension_runtime_data_[extension->id()].being_upgraded = value;
2897}
[email protected]1bead0712010-11-27 17:41:532898
[email protected]e178ad92013-06-28 02:29:252899bool ExtensionService::IsBeingReloaded(
2900 const std::string& extension_id) const {
2901 return ContainsKey(extensions_being_reloaded_, extension_id);
2902}
2903
2904void ExtensionService::SetBeingReloaded(const std::string& extension_id,
[email protected]5e7015c2013-07-23 23:29:432905 bool isBeingReloaded) {
2906 if (isBeingReloaded)
[email protected]e178ad92013-06-28 02:29:252907 extensions_being_reloaded_.insert(extension_id);
[email protected]5e7015c2013-07-23 23:29:432908 else
[email protected]e178ad92013-06-28 02:29:252909 extensions_being_reloaded_.erase(extension_id);
[email protected]e178ad92013-06-28 02:29:252910}
2911
[email protected]dc9a74f72012-08-17 18:07:212912bool ExtensionService::HasUsedWebRequest(const Extension* extension) const {
2913 ExtensionRuntimeDataMap::const_iterator it =
2914 extension_runtime_data_.find(extension->id());
2915 return it == extension_runtime_data_.end() ? false :
2916 it->second.has_used_webrequest;
[email protected]39a5b532011-10-22 01:47:072917}
2918
2919void ExtensionService::SetHasUsedWebRequest(const Extension* extension,
2920 bool value) {
2921 extension_runtime_data_[extension->id()].has_used_webrequest = value;
2922}
2923
[email protected]65992d52012-09-19 23:05:312924void ExtensionService::InspectExtensionHost(
2925 extensions::ExtensionHost* host) {
2926 if (host)
2927 DevToolsWindow::OpenDevToolsWindow(host->render_view_host());
2928}
[email protected]612a1cb12012-10-17 13:18:032929
2930bool ExtensionService::ShouldEnableOnInstall(const Extension* extension) {
2931 // Extensions installed by policy can't be disabled. So even if a previous
2932 // installation disabled the extension, make sure it is now enabled.
2933 if (system_->management_policy()->MustRemainEnabled(extension, NULL))
2934 return true;
2935
2936 if (extension_prefs_->IsExtensionDisabled(extension->id()))
2937 return false;
2938
[email protected]41070e8d2012-10-24 01:34:362939 if (FeatureSwitch::prompt_for_external_extensions()->IsEnabled()) {
[email protected]612a1cb12012-10-17 13:18:032940 // External extensions are initially disabled. We prompt the user before
[email protected]2c495c42013-01-04 21:49:542941 // enabling them. Hosted apps are excepted because they are not dangerous
2942 // (they need to be launched by the user anyway).
[email protected]1d5e58b2013-01-31 08:41:402943 if (extension->GetType() != Manifest::TYPE_HOSTED_APP &&
2944 Manifest::IsExternalLocation(extension->location()) &&
[email protected]612a1cb12012-10-17 13:18:032945 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id())) {
2946 return false;
2947 }
2948 }
[email protected]612a1cb12012-10-17 13:18:032949
2950 return true;
2951}
[email protected]0db124b02012-11-07 04:55:052952
2953bool ExtensionService::IsExtensionIdle(const std::string& extension_id) const {
[email protected]d9a61e12012-11-14 02:43:472954 ExtensionProcessManager* process_manager = system_->process_manager();
[email protected]fc332ae2012-11-14 20:17:332955 DCHECK(process_manager);
[email protected]0db124b02012-11-07 04:55:052956 extensions::ExtensionHost* host =
[email protected]d9a61e12012-11-14 02:43:472957 process_manager->GetBackgroundHostForExtension(extension_id);
[email protected]e7aa7b7e2012-11-27 04:51:222958 if (host)
2959 return false;
2960 return process_manager->GetRenderViewHostsForExtension(extension_id).empty();
2961}
2962
2963bool ExtensionService::ShouldDelayExtensionUpdate(
2964 const std::string& extension_id,
2965 bool wait_for_idle) const {
2966 const char kOnUpdateAvailableEvent[] = "runtime.onUpdateAvailable";
2967
2968 // If delayed updates are globally disabled, or just for this extension,
2969 // don't delay.
2970 if (!install_updates_when_idle_ || !wait_for_idle)
2971 return false;
2972
[email protected]695b5712012-12-06 23:55:282973 const Extension* old = GetInstalledExtension(extension_id);
[email protected]e7aa7b7e2012-11-27 04:51:222974 // If there is no old extension, this is not an update, so don't delay.
2975 if (!old)
2976 return false;
2977
[email protected]9367eabc2013-03-01 01:29:292978 if (extensions::BackgroundInfo::HasPersistentBackgroundPage(old)) {
[email protected]e7aa7b7e2012-11-27 04:51:222979 // Delay installation if the extension listens for the onUpdateAvailable
2980 // event.
2981 return system_->event_router()->ExtensionHasEventListener(
2982 extension_id, kOnUpdateAvailableEvent);
2983 } else {
2984 // Delay installation if the extension is not idle.
2985 return !IsExtensionIdle(extension_id);
2986 }
[email protected]0db124b02012-11-07 04:55:052987}
[email protected]fdd679b2012-11-15 20:49:392988
[email protected]399583b2012-12-11 09:33:422989void ExtensionService::GarbageCollectIsolatedStorage() {
[email protected]650b2d52013-02-10 03:41:452990 scoped_ptr<base::hash_set<base::FilePath> > active_paths(
2991 new base::hash_set<base::FilePath>());
[email protected]399583b2012-12-11 09:33:422992 for (ExtensionSet::const_iterator it = extensions_.begin();
2993 it != extensions_.end(); ++it) {
[email protected]cadac622013-06-11 16:46:362994 if (extensions::AppIsolationInfo::HasIsolatedStorage(it->get())) {
2995 active_paths->insert(BrowserContext::GetStoragePartitionForSite(
2996 profile_, GetSiteForExtensionId((*it)->id()))->GetPath());
[email protected]399583b2012-12-11 09:33:422997 }
2998 }
2999
[email protected]9f4e4f082013-06-21 07:11:193000 DCHECK(!installs_delayed_for_gc());
3001 set_installs_delayed_for_gc(true);
[email protected]399583b2012-12-11 09:33:423002 BrowserContext::GarbageCollectStoragePartitions(
3003 profile_, active_paths.Pass(),
3004 base::Bind(&ExtensionService::OnGarbageCollectIsolatedStorageFinished,
3005 AsWeakPtr()));
3006}
3007
3008void ExtensionService::OnGarbageCollectIsolatedStorageFinished() {
[email protected]9f4e4f082013-06-21 07:11:193009 set_installs_delayed_for_gc(false);
3010 MaybeFinishDelayedInstallations();
3011}
3012
3013void ExtensionService::MaybeFinishDelayedInstallations() {
3014 std::vector<std::string> to_be_installed;
[email protected]399583b2012-12-11 09:33:423015 for (ExtensionSet::const_iterator it = delayed_installs_.begin();
3016 it != delayed_installs_.end();
3017 ++it) {
[email protected]9f4e4f082013-06-21 07:11:193018 to_be_installed.push_back((*it)->id());
[email protected]399583b2012-12-11 09:33:423019 }
[email protected]9f4e4f082013-06-21 07:11:193020 for (std::vector<std::string>::const_iterator it = to_be_installed.begin();
3021 it != to_be_installed.end();
[email protected]399583b2012-12-11 09:33:423022 ++it) {
[email protected]9f4e4f082013-06-21 07:11:193023 MaybeFinishDelayedInstallation(*it);
[email protected]399583b2012-12-11 09:33:423024 }
[email protected]399583b2012-12-11 09:33:423025}
3026
3027void ExtensionService::OnNeedsToGarbageCollectIsolatedStorage() {
3028 extension_prefs_->SetNeedsStorageGarbageCollection(true);
3029}
3030
[email protected]fdd679b2012-11-15 20:49:393031void ExtensionService::OnBlacklistUpdated() {
[email protected]695b5712012-12-06 23:55:283032 blacklist_->GetBlacklistedIDs(
3033 GenerateInstalledExtensionsSet()->GetIDs(),
3034 base::Bind(&ExtensionService::ManageBlacklist,
3035 AsWeakPtr(),
3036 blacklisted_extensions_.GetIDs()));
3037}
3038
3039void ExtensionService::ManageBlacklist(
3040 const std::set<std::string>& old_blacklisted_ids,
3041 const std::set<std::string>& new_blacklisted_ids) {
3042 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
3043
3044 std::set<std::string> no_longer_blacklisted;
3045 std::set_difference(old_blacklisted_ids.begin(), old_blacklisted_ids.end(),
3046 new_blacklisted_ids.begin(), new_blacklisted_ids.end(),
3047 std::inserter(no_longer_blacklisted,
3048 no_longer_blacklisted.begin()));
3049 std::set<std::string> not_yet_blacklisted;
3050 std::set_difference(new_blacklisted_ids.begin(), new_blacklisted_ids.end(),
3051 old_blacklisted_ids.begin(), old_blacklisted_ids.end(),
3052 std::inserter(not_yet_blacklisted,
3053 not_yet_blacklisted.begin()));
3054
3055 for (std::set<std::string>::iterator it = no_longer_blacklisted.begin();
3056 it != no_longer_blacklisted.end(); ++it) {
3057 scoped_refptr<const Extension> extension =
3058 blacklisted_extensions_.GetByID(*it);
[email protected]dc24976f2013-06-02 21:15:093059 DCHECK(extension.get());
3060 if (!extension.get())
[email protected]695b5712012-12-06 23:55:283061 continue;
3062 blacklisted_extensions_.Remove(*it);
[email protected]dc24976f2013-06-02 21:15:093063 AddExtension(extension.get());
[email protected]ac875372013-02-28 04:36:093064 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.UnblacklistInstalled",
[email protected]dc24976f2013-06-02 21:15:093065 extension->location(),
3066 Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:283067 }
3068
3069 for (std::set<std::string>::iterator it = not_yet_blacklisted.begin();
3070 it != not_yet_blacklisted.end(); ++it) {
3071 scoped_refptr<const Extension> extension = GetInstalledExtension(*it);
[email protected]dc24976f2013-06-02 21:15:093072 DCHECK(extension.get());
3073 if (!extension.get())
[email protected]695b5712012-12-06 23:55:283074 continue;
3075 blacklisted_extensions_.Insert(extension);
3076 UnloadExtension(*it, extension_misc::UNLOAD_REASON_BLACKLIST);
[email protected]ac875372013-02-28 04:36:093077 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.BlacklistInstalled",
3078 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:283079 }
3080
3081 IdentifyAlertableExtensions();
[email protected]fdd679b2012-11-15 20:49:393082}
[email protected]75bdcb872013-03-13 00:41:453083
3084void ExtensionService::AddUpdateObserver(extensions::UpdateObserver* observer) {
3085 update_observers_.AddObserver(observer);
3086}
3087
3088void ExtensionService::RemoveUpdateObserver(
3089 extensions::UpdateObserver* observer) {
3090 update_observers_.RemoveObserver(observer);
3091}