blob: 1e3e223a89ed85bf23aff42722887d730bf7c843 [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]cc2c3432009-11-06 17:24:3626#include "base/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]70019152012-12-19 11:44:1931#include "chrome/browser/devtools/devtools_window.h"
[email protected]32b36c62012-10-24 05:37:4132#include "chrome/browser/extensions/api/app_runtime/app_runtime_api.h"
[email protected]895a1e52012-05-15 02:50:1233#include "chrome/browser/extensions/api/declarative/rules_registry_service.h"
[email protected]ad445762013-01-23 00:47:4434#include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
[email protected]c39df1a2013-01-08 18:22:0735#include "chrome/browser/extensions/api/profile_keyed_api_factory.h"
[email protected]e9f541a2012-11-19 21:52:3136#include "chrome/browser/extensions/api/runtime/runtime_api.h"
[email protected]4636c832013-01-11 02:10:1137#include "chrome/browser/extensions/api/storage/settings_frontend.h"
[email protected]5db9ada2012-04-11 13:48:2038#include "chrome/browser/extensions/app_sync_data.h"
[email protected]5a38dfd2012-07-23 23:22:1039#include "chrome/browser/extensions/browser_event_router.h"
[email protected]d8c8f25f2011-11-02 18:18:0140#include "chrome/browser/extensions/component_loader.h"
[email protected]e0785902011-05-19 23:34:1741#include "chrome/browser/extensions/crx_installer.h"
[email protected]d9ede582012-08-14 19:21:3842#include "chrome/browser/extensions/data_deleter.h"
[email protected]62f051c2012-03-29 17:04:4443#include "chrome/browser/extensions/extension_disabled_ui.h"
[email protected]14a000d2010-04-29 21:44:2444#include "chrome/browser/extensions/extension_error_reporter.h"
[email protected]89226982012-07-16 20:09:1845#include "chrome/browser/extensions/extension_error_ui.h"
[email protected]b1748b1d82009-11-30 20:32:5646#include "chrome/browser/extensions/extension_host.h"
[email protected]00b38242012-07-18 18:43:2247#include "chrome/browser/extensions/extension_install_ui.h"
[email protected]4814b512009-11-07 00:12:2948#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]3569b502012-01-12 20:08:2349#include "chrome/browser/extensions/extension_sorting.h"
[email protected]19eb80152011-02-26 00:28:4350#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]90310d92011-04-17 07:35:0451#include "chrome/browser/extensions/extension_sync_data.h"
[email protected]31d8f5f22012-04-02 15:22:0852#include "chrome/browser/extensions/extension_system.h"
[email protected]612a1cb12012-10-17 13:18:0353#include "chrome/browser/extensions/external_install_ui.h"
[email protected]5df038b2012-07-16 19:03:2754#include "chrome/browser/extensions/external_provider_impl.h"
55#include "chrome/browser/extensions/external_provider_interface.h"
[email protected]d8c8f25f2011-11-02 18:18:0156#include "chrome/browser/extensions/installed_loader.h"
[email protected]6cafe35a2012-08-27 22:40:5057#include "chrome/browser/extensions/lazy_background_task_queue.h"
[email protected]fdd679b2012-11-15 20:49:3958#include "chrome/browser/extensions/management_policy.h"
[email protected]b2907fd2011-03-25 16:43:3759#include "chrome/browser/extensions/pending_extension_manager.h"
[email protected]c333e792012-01-06 16:57:3960#include "chrome/browser/extensions/permissions_updater.h"
[email protected]6cafe35a2012-08-27 22:40:5061#include "chrome/browser/extensions/platform_app_launcher.h"
[email protected]6cafe35a2012-08-27 22:40:5062#include "chrome/browser/extensions/shell_window_registry.h"
[email protected]d8c8f25f2011-11-02 18:18:0163#include "chrome/browser/extensions/unpacked_installer.h"
[email protected]75bdcb872013-03-13 00:41:4564#include "chrome/browser/extensions/update_observer.h"
[email protected]42a08162012-03-16 18:09:1165#include "chrome/browser/extensions/updater/extension_updater.h"
[email protected]8ecad5e2010-12-02 21:18:3366#include "chrome/browser/profiles/profile.h"
[email protected]cf593af2011-12-30 05:44:3967#include "chrome/browser/profiles/profile_manager.h"
[email protected]d5949312012-12-03 22:13:3068#include "chrome/browser/themes/theme_service.h"
69#include "chrome/browser/themes/theme_service_factory.h"
[email protected]c8d407e2011-04-28 21:27:1770#include "chrome/browser/ui/webui/favicon_source.h"
[email protected]5eddc3e2011-10-26 04:33:3171#include "chrome/browser/ui/webui/ntp/thumbnail_source.h"
[email protected]b07e606e2012-09-15 20:16:1572#include "chrome/browser/ui/webui/theme_source.h"
[email protected]aab98a52009-12-02 03:22:3573#include "chrome/common/child_process_logging.h"
[email protected]432115822011-07-10 15:52:2774#include "chrome/common/chrome_notification_types.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]7c927b62010-02-24 09:54:1379#include "chrome/common/extensions/extension_file_util.h"
[email protected]895a1e52012-05-15 02:50:1280#include "chrome/common/extensions/extension_manifest_constants.h"
[email protected]77a6970c2011-04-23 16:58:5681#include "chrome/common/extensions/extension_messages.h"
[email protected]612a1cb12012-10-17 13:18:0382#include "chrome/common/extensions/feature_switch.h"
[email protected]e9f541a2012-11-19 21:52:3183#include "chrome/common/extensions/features/feature.h"
[email protected]06492ed2013-03-24 22:13:1484#include "chrome/common/extensions/incognito_handler.h"
[email protected]ac875372013-02-28 04:36:0985#include "chrome/common/extensions/manifest.h"
[email protected]561e33d52013-04-03 06:58:4386#include "chrome/common/extensions/manifest_handlers/app_isolation_info.h"
[email protected]6b414c232013-06-05 07:53:3487#include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
[email protected]65348062013-01-15 07:27:2288#include "chrome/common/extensions/manifest_url_handler.h"
[email protected]2cb5e302013-05-09 05:00:0689#include "chrome/common/extensions/permissions/permissions_data.h"
[email protected]8f3bcbd2013-06-05 08:42:4090#include "chrome/common/extensions/sync_helper.h"
[email protected]25b34332009-06-05 21:53:1991#include "chrome/common/pref_names.h"
[email protected]21f2e2d2013-01-23 22:59:5292#include "chrome/common/startup_metric_utils.h"
[email protected]a57209872009-05-04 22:53:1493#include "chrome/common/url_constants.h"
[email protected]c38831a12011-10-28 12:44:4994#include "content/public/browser/browser_thread.h"
[email protected]98f66112012-12-25 12:59:3695#include "content/public/browser/devtools_agent_host.h"
[email protected]05aad2da2011-10-28 10:12:3796#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1697#include "content/public/browser/notification_types.h"
[email protected]f3b1a082011-11-18 00:34:3098#include "content/public/browser/render_process_host.h"
[email protected]399583b2012-12-11 09:33:4299#include "content/public/browser/site_instance.h"
100#include "content/public/browser/storage_partition.h"
[email protected]24ea7a12013-01-27 23:54:53101#include "content/public/browser/url_data_source.h"
[email protected]836e2982013-05-16 08:07:42102#include "extensions/common/constants.h"
[email protected]e9f541a2012-11-19 21:52:31103#include "extensions/common/error_utils.h"
[email protected]c10da4b02010-03-25 14:38:32104#include "googleurl/src/gurl.h"
[email protected]65187152012-06-02 13:14:14105#include "grit/generated_resources.h"
[email protected]be28b5f42012-07-20 11:31:25106#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
[email protected]895a1e52012-05-15 02:50:12107#include "sync/api/sync_change.h"
108#include "sync/api/sync_error_factory.h"
[email protected]4941fe92013-06-13 23:21:01109#include "ui/webui/web_ui_util.h"
[email protected]08b1f75f2013-05-22 22:02:38110#include "webkit/browser/database/database_tracker.h"
111#include "webkit/browser/database/database_util.h"
[email protected]79a60642012-10-20 21:03:18112
[email protected]eed367e2011-04-12 03:43:31113#if defined(OS_CHROMEOS)
[email protected]88e8ec9152013-01-17 04:05:18114#include "chrome/browser/chromeos/extensions/install_limiter.h"
[email protected]c6f9203a2013-05-28 02:08:07115#include "webkit/browser/fileapi/file_system_context.h"
[email protected]c4298d02013-05-20 05:42:52116#include "webkit/browser/fileapi/file_system_mount_point_provider.h"
[email protected]eed367e2011-04-12 03:43:31117#endif
118
[email protected]55eb70e762012-02-20 17:38:39119using content::BrowserContext;
[email protected]631bb742011-11-02 11:29:39120using content::BrowserThread;
[email protected]98f66112012-12-25 12:59:36121using content::DevToolsAgentHost;
[email protected]bf3d9df2012-07-24 23:20:27122using extensions::CrxInstaller;
[email protected]1c321ee52012-05-21 03:02:34123using extensions::Extension;
124using extensions::ExtensionIdSet;
125using extensions::ExtensionInfo;
[email protected]215a7be2012-10-22 19:53:42126using extensions::FeatureSwitch;
[email protected]1d5e58b2013-01-31 08:41:40127using extensions::Manifest;
[email protected]c2e66e12012-06-27 06:27:06128using extensions::PermissionMessage;
129using extensions::PermissionMessages;
130using extensions::PermissionSet;
[email protected]e410b5f2012-12-14 14:02:24131using extensions::UnloadedExtensionInfo;
[email protected]5ef47ec2010-01-28 05:58:05132
[email protected]c6d474f82009-12-16 21:11:06133namespace errors = extension_manifest_errors;
134
[email protected]b6ab96d2009-08-20 18:58:19135namespace {
136
[email protected]d96eb512012-11-01 23:44:08137// Histogram values for logging events related to externally installed
138// extensions.
139enum ExternalExtensionEvent {
140 EXTERNAL_EXTENSION_INSTALLED = 0,
141 EXTERNAL_EXTENSION_IGNORED,
142 EXTERNAL_EXTENSION_REENABLED,
143 EXTERNAL_EXTENSION_UNINSTALLED,
144 EXTERNAL_EXTENSION_BUCKET_BOUNDARY,
145};
146
[email protected]612a1cb12012-10-17 13:18:03147// Prompt the user this many times before considering an extension acknowledged.
148static const int kMaxExtensionAcknowledgePromptCount = 3;
149
[email protected]0db124b02012-11-07 04:55:05150// Wait this many seconds after an extensions becomes idle before updating it.
151static const int kUpdateIdleDelay = 5;
152
[email protected]9bd9a6862012-11-29 09:24:22153// Wait this many seconds before trying to garbage collect extensions again.
154static const int kGarbageCollectRetryDelay = 30;
155
[email protected]c6d474f82009-12-16 21:11:06156} // namespace
[email protected]b6ab96d2009-08-20 18:58:19157
[email protected]eaa7dd182010-12-14 11:09:00158ExtensionService::ExtensionRuntimeData::ExtensionRuntimeData()
[email protected]d7e9a862010-11-03 21:57:49159 : background_page_ready(false),
[email protected]f8bf5992011-10-24 22:38:57160 being_upgraded(false),
161 has_used_webrequest(false) {
[email protected]d7e9a862010-11-03 21:57:49162}
163
[email protected]eaa7dd182010-12-14 11:09:00164ExtensionService::ExtensionRuntimeData::~ExtensionRuntimeData() {
[email protected]d7e9a862010-11-03 21:57:49165}
166
[email protected]eaa7dd182010-12-14 11:09:00167// ExtensionService.
[email protected]6014d672008-12-05 00:38:25168
[email protected]31bb5ee62012-09-12 22:58:40169const char ExtensionService::kLocalAppSettingsDirectoryName[] =
[email protected]15d5b472011-11-24 04:17:24170 "Local App Settings";
[email protected]31bb5ee62012-09-12 22:58:40171const char ExtensionService::kLocalExtensionSettingsDirectoryName[] =
[email protected]15d5b472011-11-24 04:17:24172 "Local Extension Settings";
[email protected]31bb5ee62012-09-12 22:58:40173const char ExtensionService::kSyncAppSettingsDirectoryName[] =
[email protected]15d5b472011-11-24 04:17:24174 "Sync App Settings";
[email protected]31bb5ee62012-09-12 22:58:40175const char ExtensionService::kSyncExtensionSettingsDirectoryName[] =
[email protected]15d5b472011-11-24 04:17:24176 "Sync Extension Settings";
[email protected]675bb01012012-12-14 22:03:29177const char ExtensionService::kManagedSettingsDirectoryName[] =
178 "Managed Extension Settings";
[email protected]31bb5ee62012-09-12 22:58:40179const char ExtensionService::kStateStoreName[] = "Extension State";
[email protected]a690e292012-12-19 19:22:49180const char ExtensionService::kRulesStoreName[] = "Extension Rules";
[email protected]494c06e2009-07-25 01:06:42181
[email protected]8e4560b62011-01-14 10:09:14182void ExtensionService::CheckExternalUninstall(const std::string& id) {
[email protected]a29a517a2011-01-21 21:11:12183 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20184
[email protected]7a4c6852010-09-16 03:44:22185 // Check if the providers know about this extension.
[email protected]5df038b2012-07-16 19:03:27186 extensions::ProviderCollection::const_iterator i;
[email protected]0a60a2e2010-10-25 16:15:21187 for (i = external_extension_providers_.begin();
188 i != external_extension_providers_.end(); ++i) {
[email protected]8e4560b62011-01-14 10:09:14189 DCHECK(i->get()->IsReady());
[email protected]0a60a2e2010-10-25 16:15:21190 if (i->get()->HasExtension(id))
191 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22192 }
193
[email protected]0f48fca2011-05-19 18:46:35194 // We get the list of external extensions to check from preferences.
195 // It is possible that an extension has preferences but is not loaded.
196 // For example, an extension that requires experimental permissions
197 // will not be loaded if the experimental command line flag is not used.
198 // In this case, do not uninstall.
[email protected]31bb5ee62012-09-12 22:58:40199 if (!GetInstalledExtension(id)) {
[email protected]0f48fca2011-05-19 18:46:35200 // We can't call UninstallExtension with an unloaded/invalid
201 // extension ID.
202 LOG(WARNING) << "Attempted uninstallation of unloaded/invalid extension "
203 << "with id: " << id;
204 return;
205 }
[email protected]d6ebc9792011-04-07 18:18:33206 UninstallExtension(id, true, NULL);
[email protected]7a4c6852010-09-16 03:44:22207}
208
[email protected]7f8f24f2012-11-15 19:40:14209void ExtensionService::SetFileTaskRunnerForTesting(
210 base::SequencedTaskRunner* task_runner) {
211 file_task_runner_ = task_runner;
212}
213
[email protected]8e4560b62011-01-14 10:09:14214void ExtensionService::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22215 external_extension_providers_.clear();
216}
217
[email protected]8e4560b62011-01-14 10:09:14218void ExtensionService::AddProviderForTesting(
[email protected]5df038b2012-07-16 19:03:27219 extensions::ExternalProviderInterface* test_provider) {
[email protected]a29a517a2011-01-21 21:11:12220 CHECK(test_provider);
[email protected]0a60a2e2010-10-25 16:15:21221 external_extension_providers_.push_back(
[email protected]5df038b2012-07-16 19:03:27222 linked_ptr<extensions::ExternalProviderInterface>(test_provider));
[email protected]7a4c6852010-09-16 03:44:22223}
224
[email protected]9060d8b02012-01-13 02:14:30225bool ExtensionService::OnExternalExtensionUpdateUrlFound(
[email protected]7a4c6852010-09-16 03:44:22226 const std::string& id,
[email protected]21a5a672010-11-04 10:47:42227 const GURL& update_url,
[email protected]1d5e58b2013-01-31 08:41:40228 Manifest::Location location) {
[email protected]ab22ba42011-01-14 16:36:38229 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
230 CHECK(Extension::IdIsValid(id));
[email protected]a8af9fdb2010-10-28 21:52:20231
[email protected]8a87a5332011-08-11 17:54:59232 const Extension* extension = GetExtensionById(id, true);
233 if (extension) {
234 // Already installed. Skip this install if the current location has
235 // higher priority than |location|.
[email protected]1d5e58b2013-01-31 08:41:40236 Manifest::Location current = extension->location();
237 if (current == Manifest::GetHigherPriorityLocation(current, location))
[email protected]9060d8b02012-01-13 02:14:30238 return false;
[email protected]8a87a5332011-08-11 17:54:59239 // Otherwise, overwrite the current installation.
[email protected]7a4c6852010-09-16 03:44:22240 }
[email protected]9060d8b02012-01-13 02:14:30241
242 // Add |id| to the set of pending extensions. If it can not be added,
243 // then there is already a pending record from a higher-priority install
244 // source. In this case, signal that this extension will not be
245 // installed by returning false.
246 if (!pending_extension_manager()->AddFromExternalUpdateUrl(
[email protected]31bb5ee62012-09-12 22:58:40247 id, update_url, location)) {
[email protected]9060d8b02012-01-13 02:14:30248 return false;
[email protected]31bb5ee62012-09-12 22:58:40249 }
[email protected]9060d8b02012-01-13 02:14:30250
[email protected]94fde232012-04-27 10:22:30251 update_once_all_providers_are_ready_ = true;
[email protected]9060d8b02012-01-13 02:14:30252 return true;
[email protected]7a4c6852010-09-16 03:44:22253}
254
[email protected]dc9a74f72012-08-17 18:07:21255const Extension* ExtensionService::GetInstalledApp(const GURL& url) const {
[email protected]615d88f2011-12-13 01:47:44256 const Extension* extension = extensions_.GetExtensionOrAppByURL(
257 ExtensionURLInfo(url));
[email protected]dc9a74f72012-08-17 18:07:21258 return (extension && extension->is_app()) ? extension : NULL;
[email protected]d9696672011-03-15 22:45:09259}
260
[email protected]dc9a74f72012-08-17 18:07:21261bool ExtensionService::IsInstalledApp(const GURL& url) const {
[email protected]d9696672011-03-15 22:45:09262 return !!GetInstalledApp(url);
[email protected]ffb204f22010-12-05 23:20:27263}
264
[email protected]404fb002012-10-02 21:06:17265const Extension* ExtensionService::GetIsolatedAppForRenderer(
[email protected]dc9a74f72012-08-17 18:07:21266 int renderer_child_id) const {
[email protected]404fb002012-10-02 21:06:17267 std::set<std::string> extension_ids =
268 process_map_.GetExtensionsInProcess(renderer_child_id);
269 // All apps in one process share the same partition.
270 // It is only possible for the app to have isolated storage
271 // if there is only 1 app in the process.
272 if (extension_ids.size() != 1)
273 return NULL;
274
275 const extensions::Extension* extension =
276 extensions_.GetByID(*(extension_ids.begin()));
[email protected]561e33d52013-04-03 06:58:43277 // We still need to check if the extension has isolated storage,
[email protected]404fb002012-10-02 21:06:17278 // because it's common for there to be one extension in a process
[email protected]561e33d52013-04-03 06:58:43279 // without isolated storage.
280 if (extension &&
281 extensions::AppIsolationInfo::HasIsolatedStorage(extension))
[email protected]404fb002012-10-02 21:06:17282 return extension;
283
284 return NULL;
[email protected]da5683db2011-04-23 17:12:21285}
286
[email protected]6aeac8342010-10-01 20:21:18287// static
[email protected]d6ebc9792011-04-07 18:18:33288// This function is used to implement the command-line switch
[email protected]3bdba0d2011-08-23 07:17:30289// --uninstall-extension, and to uninstall an extension via sync. The LOG
290// statements within this function are used to inform the user if the uninstall
291// cannot be done.
[email protected]eaa7dd182010-12-14 11:09:00292bool ExtensionService::UninstallExtensionHelper(
293 ExtensionService* extensions_service,
[email protected]6aeac8342010-10-01 20:21:18294 const std::string& extension_id) {
[email protected]d6ebc9792011-04-07 18:18:33295 // We can't call UninstallExtension with an invalid extension ID.
[email protected]31bb5ee62012-09-12 22:58:40296 if (!extensions_service->GetInstalledExtension(extension_id)) {
[email protected]6aeac8342010-10-01 20:21:18297 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]95da88c42011-03-31 10:07:33298 << "id: " << extension_id;
299 return false;
[email protected]6aeac8342010-10-01 20:21:18300 }
301
[email protected]d6ebc9792011-04-07 18:18:33302 // The following call to UninstallExtension will not allow an uninstall of a
303 // policy-controlled extension.
[email protected]65187152012-06-02 13:14:14304 string16 error;
[email protected]d6ebc9792011-04-07 18:18:33305 if (!extensions_service->UninstallExtension(extension_id, false, &error)) {
306 LOG(WARNING) << "Cannot uninstall extension with id " << extension_id
307 << ": " << error;
308 return false;
309 }
[email protected]95da88c42011-03-31 10:07:33310
[email protected]6aeac8342010-10-01 20:21:18311 return true;
312}
313
[email protected]eaa7dd182010-12-14 11:09:00314ExtensionService::ExtensionService(Profile* profile,
[email protected]f0841cd2011-01-19 15:07:24315 const CommandLine* command_line,
[email protected]650b2d52013-02-10 03:41:45316 const base::FilePath& install_directory,
[email protected]45759612012-07-10 17:21:23317 extensions::ExtensionPrefs* extension_prefs,
[email protected]fdd679b2012-11-15 20:49:39318 extensions::Blacklist* blacklist,
[email protected]0436b102011-04-15 18:30:03319 bool autoupdate_enabled,
[email protected]4a10006a2013-05-17 23:18:35320 bool extensions_enabled,
321 extensions::OneShotEvent* ready)
[email protected]fdd679b2012-11-15 20:49:39322 : extensions::Blacklist::Observer(blacklist),
323 profile_(profile),
[email protected]bd306722012-07-11 20:43:59324 system_(extensions::ExtensionSystem::Get(profile)),
[email protected]73c47932010-12-06 18:13:43325 extension_prefs_(extension_prefs),
[email protected]695b5712012-12-06 23:55:28326 blacklist_(blacklist),
[email protected]cccdf0aa2011-11-11 03:43:38327 settings_frontend_(extensions::SettingsFrontend::Create(profile)),
[email protected]9c009092013-05-01 03:14:09328 pending_extension_manager_(*this),
[email protected]a9b00ac2009-06-25 21:03:23329 install_directory_(install_directory),
[email protected]0436b102011-04-15 18:30:03330 extensions_enabled_(extensions_enabled),
[email protected]e81dba32009-06-19 20:19:13331 show_extensions_prompts_(true),
[email protected]fc332ae2012-11-14 20:17:33332 install_updates_when_idle_(true),
[email protected]4a10006a2013-05-17 23:18:35333 ready_(ready),
[email protected]9c009092013-05-01 03:14:09334 toolbar_model_(this),
[email protected]8b280302011-10-13 22:22:23335 menu_manager_(profile),
[email protected]7c1490da2011-10-11 18:53:25336 event_routers_initialized_(false),
[email protected]dff1e042012-04-27 10:59:18337 update_once_all_providers_are_ready_(false),
[email protected]3c4abc82012-10-22 22:25:54338 browser_terminating_(false),
[email protected]399583b2012-12-11 09:33:42339 installs_delayed_(false),
[email protected]460c6712013-04-24 07:20:01340 is_first_run_(false),
[email protected]9c009092013-05-01 03:14:09341 app_sync_bundle_(this),
342 extension_sync_bundle_(this) {
[email protected]a29a517a2011-01-21 21:11:12343 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20344
[email protected]36a784c2009-06-23 06:21:08345 // Figure out if extension installation should be enabled.
[email protected]31bb5ee62012-09-12 22:58:40346 if (command_line->HasSwitch(switches::kDisableExtensions) ||
347 profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions)) {
[email protected]6d60703b2009-08-29 01:29:23348 extensions_enabled_ = false;
[email protected]6b75ec32009-08-14 06:37:18349 }
[email protected]36a784c2009-06-23 06:21:08350
[email protected]3c4abc82012-10-22 22:25:54351 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING,
352 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27353 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07354 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27355 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED,
[email protected]ad50def52011-10-19 23:17:07356 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27357 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07358 content::NotificationService::AllBrowserContextsAndSources());
[email protected]0db124b02012-11-07 04:55:05359 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
360 content::NotificationService::AllBrowserContextsAndSources());
[email protected]92dd8d92013-02-26 00:41:08361 registrar_.Add(this, chrome::NOTIFICATION_UPGRADE_RECOMMENDED,
362 content::NotificationService::AllBrowserContextsAndSources());
[email protected]2fb7dc982010-09-29 12:24:28363 pref_change_registrar_.Init(profile->GetPrefs());
[email protected]90bb38d2012-11-14 18:36:03364 base::Closure callback =
365 base::Bind(&ExtensionService::OnExtensionInstallPrefChanged,
366 base::Unretained(this));
367 pref_change_registrar_.Add(prefs::kExtensionInstallAllowList, callback);
368 pref_change_registrar_.Add(prefs::kExtensionInstallDenyList, callback);
[email protected]e410b5f2012-12-14 14:02:24369 pref_change_registrar_.Add(prefs::kExtensionAllowedTypes, callback);
[email protected]4814b512009-11-07 00:12:29370
[email protected]93fd78f42009-07-10 16:43:17371 // Set up the ExtensionUpdater
372 if (autoupdate_enabled) {
373 int update_frequency = kDefaultUpdateFrequencySeconds;
374 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25375 base::StringToInt(command_line->GetSwitchValueASCII(
376 switches::kExtensionsUpdateFrequency),
377 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17378 }
[email protected]42a08162012-03-16 18:09:11379 updater_.reset(new extensions::ExtensionUpdater(this,
380 extension_prefs,
381 profile->GetPrefs(),
382 profile,
[email protected]fdd679b2012-11-15 20:49:39383 blacklist,
[email protected]42a08162012-03-16 18:09:11384 update_frequency));
[email protected]93fd78f42009-07-10 16:43:17385 }
386
[email protected]25ae0152011-11-18 14:40:02387 component_loader_.reset(
388 new extensions::ComponentLoader(this,
389 profile->GetPrefs(),
390 g_browser_process->local_state()));
[email protected]8e4560b62011-01-14 10:09:14391
[email protected]0436b102011-04-15 18:30:03392 if (extensions_enabled_) {
[email protected]ae4c37e2012-12-21 01:16:25393 extensions::ExternalProviderImpl::CreateExternalProviders(
394 this, profile_, &external_extension_providers_);
[email protected]873531342011-03-09 12:16:05395 }
[email protected]b671760b2010-07-15 21:13:47396
[email protected]fb82dcd2012-03-21 14:15:46397 // Set this as the ExtensionService for extension sorting to ensure it
398 // cause syncs if required.
399 extension_prefs_->extension_sorting()->SetExtensionService(this);
400
[email protected]b3aa7182013-04-25 04:45:23401 is_first_run_ = !extension_prefs_->SetAlertSystemFirstRun();
402
[email protected]9ce11d22012-08-08 23:20:13403#if defined(ENABLE_EXTENSIONS)
[email protected]c77f2352012-08-08 22:07:58404 extension_action_storage_manager_.reset(
405 new extensions::ExtensionActionStorageManager(profile_));
[email protected]9ce11d22012-08-08 23:20:13406#endif
[email protected]c77f2352012-08-08 22:07:58407
[email protected]cb0e50312011-05-09 15:03:07408 // How long is the path to the Extensions directory?
409 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ExtensionRootPathLength",
410 install_directory_.value().length(), 0, 500, 100);
[email protected]6014d672008-12-05 00:38:25411}
412
[email protected]84df8332011-12-06 18:22:46413const ExtensionSet* ExtensionService::extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45414 return &extensions_;
415}
416
[email protected]84df8332011-12-06 18:22:46417const ExtensionSet* ExtensionService::disabled_extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45418 return &disabled_extensions_;
419}
420
[email protected]84df8332011-12-06 18:22:46421const ExtensionSet* ExtensionService::terminated_extensions() const {
[email protected]bb7f40952011-01-13 00:21:20422 return &terminated_extensions_;
423}
424
[email protected]695b5712012-12-06 23:55:28425const ExtensionSet* ExtensionService::blacklisted_extensions() const {
426 return &blacklisted_extensions_;
427}
428
429scoped_ptr<const ExtensionSet>
430 ExtensionService::GenerateInstalledExtensionsSet() const {
431 scoped_ptr<ExtensionSet> installed_extensions(new ExtensionSet());
[email protected]7f4308d2012-01-18 07:43:01432 installed_extensions->InsertAll(extensions_);
433 installed_extensions->InsertAll(disabled_extensions_);
434 installed_extensions->InsertAll(terminated_extensions_);
[email protected]695b5712012-12-06 23:55:28435 installed_extensions->InsertAll(blacklisted_extensions_);
436 return installed_extensions.PassAs<const ExtensionSet>();
[email protected]7f4308d2012-01-18 07:43:01437}
438
[email protected]3f213ad2012-07-26 23:39:41439extensions::PendingExtensionManager*
440 ExtensionService::pending_extension_manager() {
[email protected]b2907fd2011-03-25 16:43:37441 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45442}
443
[email protected]eaa7dd182010-12-14 11:09:00444ExtensionService::~ExtensionService() {
[email protected]c8d407e2011-04-28 21:27:17445 // No need to unload extensions here because they are profile-scoped, and the
446 // profile is in the process of being deleted.
[email protected]94b4ffc2011-02-04 05:55:17447
[email protected]5df038b2012-07-16 19:03:27448 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:14449 for (i = external_extension_providers_.begin();
450 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:27451 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:14452 provider->ServiceShutdown();
453 }
[email protected]6014d672008-12-05 00:38:25454}
455
[email protected]d91268022011-08-26 13:17:37456void ExtensionService::InitEventRoutersAfterImport() {
[email protected]cf593af2011-12-30 05:44:39457 RegisterForImportFinished();
458}
459
460void ExtensionService::RegisterForImportFinished() {
461 if (!registrar_.IsRegistered(this, chrome::NOTIFICATION_IMPORT_FINISHED,
462 content::Source<Profile>(profile_))) {
463 registrar_.Add(this, chrome::NOTIFICATION_IMPORT_FINISHED,
464 content::Source<Profile>(profile_));
465 }
466}
467
468void ExtensionService::InitAfterImport() {
[email protected]21f2e2d2013-01-23 22:59:52469 startup_metric_utils::ScopedSlowStartupUMA
470 scoped_timer("Startup.SlowStartupExtensionServiceInitAfterImport");
[email protected]eac88332012-12-26 17:57:45471 component_loader_->LoadAll();
[email protected]ae4c37e2012-12-21 01:16:25472
[email protected]cf593af2011-12-30 05:44:39473 CheckForExternalUpdates();
474
475 GarbageCollectExtensions();
476
477 // Idempotent, so although there is a possible race if the import
478 // process finished sometime in the middle of ProfileImpl::InitExtensions,
479 // it cannot happen twice.
480 InitEventRouters();
[email protected]d91268022011-08-26 13:17:37481}
482
[email protected]eaa7dd182010-12-14 11:09:00483void ExtensionService::InitEventRouters() {
[email protected]b3d62312b12010-10-14 21:10:18484 if (event_routers_initialized_)
485 return;
486
[email protected]6a3cd37e2012-04-17 17:13:34487#if defined(ENABLE_EXTENSIONS)
[email protected]5a38dfd2012-07-23 23:22:10488 browser_event_router_.reset(new extensions::BrowserEventRouter(profile_));
[email protected]6a3cd37e2012-04-17 17:13:34489#endif // defined(ENABLE_EXTENSIONS)
[email protected]b3d62312b12010-10-14 21:10:18490 event_routers_initialized_ = true;
[email protected]c5ae74ab2010-04-15 18:14:37491}
492
[email protected]037228a2012-10-05 01:36:16493void ExtensionService::Shutdown() {
494 // Do nothing for now.
495}
496
[email protected]b2907fd2011-03-25 16:43:37497const Extension* ExtensionService::GetExtensionById(
498 const std::string& id, bool include_disabled) const {
[email protected]81f0d162012-08-15 04:21:47499 int include_mask = INCLUDE_ENABLED;
[email protected]695b5712012-12-06 23:55:28500 if (include_disabled) {
501 // Include blacklisted extensions here because there are hundreds of
502 // callers of this function, and many might assume that this includes those
503 // that have been disabled due to blacklisting.
504 include_mask |= INCLUDE_DISABLED | INCLUDE_BLACKLISTED;
505 }
506 return GetExtensionById(id, include_mask);
507}
508
[email protected]399583b2012-12-11 09:33:42509GURL ExtensionService::GetSiteForExtensionId(const std::string& extension_id) {
510 return content::SiteInstance::GetSiteForURL(
511 profile_,
512 Extension::GetBaseURLFromExtensionId(extension_id));
513}
514
[email protected]695b5712012-12-06 23:55:28515const Extension* ExtensionService::GetExtensionById(
516 const std::string& id, int include_mask) const {
517 std::string lowercase_id = StringToLowerASCII(id);
518 if (include_mask & INCLUDE_ENABLED) {
519 const Extension* extension = extensions_.GetByID(lowercase_id);
520 if (extension)
521 return extension;
522 }
523 if (include_mask & INCLUDE_DISABLED) {
524 const Extension* extension = disabled_extensions_.GetByID(lowercase_id);
525 if (extension)
526 return extension;
527 }
528 if (include_mask & INCLUDE_TERMINATED) {
529 const Extension* extension = terminated_extensions_.GetByID(lowercase_id);
530 if (extension)
531 return extension;
532 }
533 if (include_mask & INCLUDE_BLACKLISTED) {
534 const Extension* extension = blacklisted_extensions_.GetByID(lowercase_id);
535 if (extension)
536 return extension;
537 }
538 return NULL;
[email protected]78994ab02010-12-08 18:06:44539}
540
[email protected]eaa7dd182010-12-14 11:09:00541void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12542 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20543
[email protected]4a10006a2013-05-17 23:18:35544 DCHECK(!is_ready()); // Can't redo init.
[email protected]93fd78f42009-07-10 16:43:17545 DCHECK_EQ(extensions_.size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32546
[email protected]820d9bd2013-04-03 03:46:03547 const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
[email protected]820d9bd2013-04-03 03:46:03548 if (cmd_line->HasSwitch(switches::kInstallFromWebstore) ||
549 cmd_line->HasSwitch(switches::kLimitedInstallFromWebstore)) {
550 // The sole purpose of this launch is to install a new extension from CWS
551 // and immediately terminate: loading already installed extensions is
552 // unnecessary and may interfere with the inline install dialog (e.g. if an
553 // extension listens to onStartup and opens a window).
554 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25555 } else {
[email protected]820d9bd2013-04-03 03:46:03556 // TODO(mek): It might be cleaner to do the FinishDelayedInstallInfo stuff
557 // here instead of in installedloader.
558 if (g_browser_process->profile_manager() &&
559 g_browser_process->profile_manager()->will_import()) {
560 // Do not load any component extensions, since they may conflict with the
561 // import process.
[email protected]ae4c37e2012-12-21 01:16:25562
[email protected]820d9bd2013-04-03 03:46:03563 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]4a10006a2013-05-17 23:18:35564 SetReadyAndNotifyListeners();
[email protected]820d9bd2013-04-03 03:46:03565 RegisterForImportFinished();
566 } else {
[email protected]4a10006a2013-05-17 23:18:35567 // In this case, LoadAllExtensions() calls OnLoadedInstalledExtensions().
[email protected]820d9bd2013-04-03 03:46:03568 component_loader_->LoadAll();
569 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]4a10006a2013-05-17 23:18:35570 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25571
[email protected]820d9bd2013-04-03 03:46:03572 // TODO(erikkay) this should probably be deferred to a future point
573 // rather than running immediately at startup.
574 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57575
[email protected]820d9bd2013-04-03 03:46:03576 // TODO(erikkay) this should probably be deferred as well.
577 GarbageCollectExtensions();
578 }
[email protected]215a7be2012-10-22 19:53:42579
[email protected]820d9bd2013-04-03 03:46:03580 if (extension_prefs_->NeedsStorageGarbageCollection()) {
581 GarbageCollectIsolatedStorage();
582 extension_prefs_->SetNeedsStorageGarbageCollection(false);
583 }
[email protected]399583b2012-12-11 09:33:42584 }
[email protected]6014d672008-12-05 00:38:25585}
586
[email protected]31bb5ee62012-09-12 22:58:40587bool ExtensionService::UpdateExtension(const std::string& id,
[email protected]650b2d52013-02-10 03:41:45588 const base::FilePath& extension_path,
[email protected]31bb5ee62012-09-12 22:58:40589 const GURL& download_url,
590 CrxInstaller** out_crx_installer) {
[email protected]a29a517a2011-01-21 21:11:12591 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]3c4abc82012-10-22 22:25:54592 if (browser_terminating_) {
593 LOG(WARNING) << "Skipping UpdateExtension due to browser shutdown";
594 // Leak the temp file at extension_path. We don't want to add to the disk
595 // I/O burden at shutdown, we can't rely on the I/O completing anyway, and
596 // the file is in the OS temp directory which should be cleaned up for us.
597 return false;
598 }
[email protected]a8af9fdb2010-10-28 21:52:20599
[email protected]3f213ad2012-07-26 23:39:41600 const extensions::PendingExtensionInfo* pending_extension_info =
[email protected]51a3bf8b2012-06-08 22:53:06601 pending_extension_manager()->GetById(id);
[email protected]8ef78fd2010-08-19 17:14:32602
[email protected]695b5712012-12-06 23:55:28603 const Extension* extension = GetInstalledExtension(id);
[email protected]51a3bf8b2012-06-08 22:53:06604 if (!pending_extension_info && !extension) {
[email protected]aa142702010-03-26 01:26:33605 LOG(WARNING) << "Will not update extension " << id
606 << " because it is not installed or pending";
607 // Delete extension_path since we're not creating a CrxInstaller
608 // that would do it for us.
[email protected]7f8f24f2012-11-15 19:40:14609 if (!GetFileTaskRunner()->PostTask(
610 FROM_HERE,
[email protected]53612e82011-10-18 18:00:36611 base::Bind(
[email protected]7296f2762011-11-21 19:23:44612 &extension_file_util::DeleteFile, extension_path, false)))
[email protected]14908b72011-04-20 06:54:36613 NOTREACHED();
[email protected]420a0ec2011-06-01 01:07:03614
615 return false;
[email protected]e957fe52009-06-23 16:51:05616 }
617
[email protected]aa142702010-03-26 01:26:33618 // We want a silent install only for non-pending extensions and
619 // pending extensions that have install_silently set.
[email protected]91e51d612012-10-21 23:03:05620 ExtensionInstallPrompt* client = NULL;
621 if (pending_extension_info && !pending_extension_info->install_silently())
622 client = ExtensionInstallUI::CreateInstallPromptWithProfile(profile_);
[email protected]aa142702010-03-26 01:26:33623
[email protected]d8c8f25f2011-11-02 18:18:01624 scoped_refptr<CrxInstaller> installer(CrxInstaller::Create(this, client));
[email protected]6dfbbf82010-03-12 23:09:16625 installer->set_expected_id(id);
[email protected]51a3bf8b2012-06-08 22:53:06626 if (pending_extension_info) {
627 installer->set_install_source(pending_extension_info->install_source());
628 if (pending_extension_info->install_silently())
629 installer->set_allow_silent_install(true);
630 } else if (extension) {
[email protected]5eb375e92010-11-26 07:50:41631 installer->set_install_source(extension->location());
[email protected]51a3bf8b2012-06-08 22:53:06632 }
[email protected]7d8b7072012-04-07 01:07:46633 // If the extension was installed from or has migrated to the webstore, or
[email protected]fcb2c2c2012-10-17 21:08:45634 // its auto-update URL is from the webstore, treat it as a webstore install.
635 // Note that we ignore some older extensions with blank auto-update URLs
636 // because we are mostly concerned with restrictions on NaCl extensions,
637 // which are newer.
[email protected]a12ce8b22012-01-17 18:40:53638 int creation_flags = Extension::NO_FLAGS;
[email protected]75764512011-12-19 19:54:28639 if ((extension && extension->from_webstore()) ||
[email protected]4a5fe3e22013-06-04 07:06:38640 (extension && extensions::ManifestURL::UpdatesFromGallery(extension)) ||
[email protected]963d13c2012-09-29 17:13:35641 (!extension && extension_urls::IsWebstoreUpdateUrl(
[email protected]51a3bf8b2012-06-08 22:53:06642 pending_extension_info->update_url()))) {
[email protected]a12ce8b22012-01-17 18:40:53643 creation_flags |= Extension::FROM_WEBSTORE;
644 }
645
646 // Bookmark apps being updated is kind of a contradiction, but that's because
647 // we mark the default apps as bookmark apps, and they're hosted in the web
648 // store, thus they can get updated. See http://crbug.com/101605 for more
649 // details.
650 if (extension && extension->from_bookmark())
651 creation_flags |= Extension::FROM_BOOKMARK;
652
[email protected]e33bbc22012-08-27 22:05:46653 if (extension && extension->was_installed_by_default())
654 creation_flags |= Extension::WAS_INSTALLED_BY_DEFAULT;
655
[email protected]a12ce8b22012-01-17 18:40:53656 installer->set_creation_flags(creation_flags);
657
[email protected]6dfbbf82010-03-12 23:09:16658 installer->set_delete_source(true);
[email protected]655b2b1a2011-10-13 17:13:06659 installer->set_download_url(download_url);
[email protected]cb0e50312011-05-09 15:03:07660 installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
[email protected]6dfbbf82010-03-12 23:09:16661 installer->InstallCrx(extension_path);
[email protected]420a0ec2011-06-01 01:07:03662
663 if (out_crx_installer)
[email protected]dc24976f2013-06-02 21:15:09664 *out_crx_installer = installer.get();
[email protected]420a0ec2011-06-01 01:07:03665
666 return true;
[email protected]e957fe52009-06-23 16:51:05667}
668
[email protected]eaa7dd182010-12-14 11:09:00669void ExtensionService::ReloadExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12670 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]2a947202013-03-06 04:58:05671
672 // If the extension is already reloading, don't reload again.
673 if (extension_prefs_->GetDisableReasons(extension_id) &
674 Extension::DISABLE_RELOAD) {
675 return;
676 }
677
[email protected]650b2d52013-02-10 03:41:45678 base::FilePath path;
[email protected]9adb9692010-10-29 23:14:02679 const Extension* current_extension = GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40680
[email protected]f17dbd42010-08-16 23:21:10681 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]b65272f2009-08-31 15:47:06682 if (current_extension) {
[email protected]4814b512009-11-07 00:12:29683 // If the extension has an inspector open for its background page, detach
684 // the inspector and hang onto a cookie for it, so that we can reattach
685 // later.
[email protected]31d8f5f22012-04-02 15:22:08686 // TODO(yoz): this is not incognito-safe!
687 ExtensionProcessManager* manager = system_->process_manager();
[email protected]3a1dc572012-07-31 22:25:13688 extensions::ExtensionHost* host =
689 manager->GetBackgroundHostForExtension(extension_id);
[email protected]98f66112012-12-25 12:59:36690 if (host && DevToolsAgentHost::HasFor(host->render_view_host())) {
[email protected]4814b512009-11-07 00:12:29691 // Look for an open inspector for the background page.
[email protected]ead056352013-03-12 08:47:54692 std::string devtools_cookie = DevToolsAgentHost::DisconnectRenderViewHost(
[email protected]562e20c2012-12-24 18:27:20693 host->render_view_host());
[email protected]ead056352013-03-12 08:47:54694 if (devtools_cookie != std::string())
[email protected]4814b512009-11-07 00:12:29695 orphaned_dev_tools_[extension_id] = devtools_cookie;
696 }
697
[email protected]b65272f2009-08-31 15:47:06698 path = current_extension->path();
[email protected]44d62b62012-04-11 00:06:03699 DisableExtension(extension_id, Extension::DISABLE_RELOAD);
[email protected]b914e2952013-04-26 07:10:03700 reloading_extensions_.insert(extension_id);
[email protected]1eb175082010-02-10 09:26:16701 } else {
702 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06703 }
704
[email protected]399583b2012-12-11 09:33:42705 if (delayed_updates_for_idle_.Contains(extension_id)) {
[email protected]6f6101832012-11-27 22:10:48706 FinishDelayedInstallation(extension_id);
[email protected]0db124b02012-11-07 04:55:05707 return;
708 }
709
[email protected]43ceb002012-02-10 23:19:15710 // If we're reloading a component extension, use the component extension
711 // loader's reloader.
712 if (component_loader_->Exists(extension_id)) {
713 component_loader_->Reload(extension_id);
714 return;
715 }
716
[email protected]e6090e42010-03-23 22:44:08717 // Check the installed extensions to see if what we're reloading was already
718 // installed.
719 scoped_ptr<ExtensionInfo> installed_extension(
720 extension_prefs_->GetInstalledExtensionInfo(extension_id));
721 if (installed_extension.get() &&
722 installed_extension->extension_manifest.get()) {
[email protected]d8c8f25f2011-11-02 18:18:01723 extensions::InstalledLoader(this).Load(*installed_extension, false);
[email protected]e6090e42010-03-23 22:44:08724 } else {
[email protected]d8c8f25f2011-11-02 18:18:01725 // Otherwise, the extension is unpacked (location LOAD).
[email protected]e6090e42010-03-23 22:44:08726 // We should always be able to remember the extension's path. If it's not in
727 // the map, someone failed to update |unloaded_extension_paths_|.
728 CHECK(!path.empty());
[email protected]d8c8f25f2011-11-02 18:18:01729 extensions::UnpackedInstaller::Create(this)->Load(path);
[email protected]e6090e42010-03-23 22:44:08730 }
[email protected]9cddd4702009-07-27 22:09:40731}
732
[email protected]fa2416f2011-05-03 08:41:20733bool ExtensionService::UninstallExtension(
[email protected]a12c706e2011-12-01 00:58:08734 std::string extension_id,
[email protected]fa2416f2011-05-03 08:41:20735 bool external_uninstall,
[email protected]65187152012-06-02 13:14:14736 string16* error) {
[email protected]a29a517a2011-01-21 21:11:12737 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20738
[email protected]0d6ec3a72011-09-02 02:09:43739 scoped_refptr<const Extension> extension(GetInstalledExtension(extension_id));
[email protected]631cf822009-05-15 07:01:25740
[email protected]e7afe2452010-08-22 16:19:13741 // Callers should not send us nonexistent extensions.
[email protected]dc24976f2013-06-02 21:15:09742 CHECK(extension.get());
[email protected]9f1087e2009-06-15 17:29:32743
[email protected]95da88c42011-03-31 10:07:33744 // Policy change which triggers an uninstall will always set
745 // |external_uninstall| to true so this is the only way to uninstall
746 // managed extensions.
[email protected]65187152012-06-02 13:14:14747 if (!external_uninstall &&
748 !system_->management_policy()->UserMayModifySettings(
749 extension.get(), error)) {
[email protected]ad50def52011-10-19 23:17:07750 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27751 chrome::NOTIFICATION_EXTENSION_UNINSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:53752 content::Source<Profile>(profile_),
[email protected]dc24976f2013-06-02 21:15:09753 content::Details<const Extension>(extension.get()));
[email protected]d6ebc9792011-04-07 18:18:33754 return false;
755 }
[email protected]95da88c42011-03-31 10:07:33756
[email protected]3bdba0d2011-08-23 07:17:30757 // Extract the data we need for sync now, but don't actually sync until we've
758 // completed the uninstallation.
[email protected]65f173552012-06-28 22:43:58759 syncer::SyncChange sync_change;
[email protected]dc24976f2013-06-02 21:15:09760 if (app_sync_bundle_.HandlesApp(*extension.get())) {
761 sync_change = app_sync_bundle_.CreateSyncChangeToDelete(extension.get());
762 } else if (extension_sync_bundle_.HandlesExtension(*extension.get())) {
763 sync_change =
764 extension_sync_bundle_.CreateSyncChangeToDelete(extension.get());
[email protected]3bdba0d2011-08-23 07:17:30765 }
766
[email protected]dc24976f2013-06-02 21:15:09767 if (IsUnacknowledgedExternalExtension(extension.get())) {
[email protected]d96eb512012-11-01 23:44:08768 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
769 EXTERNAL_EXTENSION_UNINSTALLED,
770 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
771 }
[email protected]9b217652010-10-08 22:04:23772 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08773 extension->GetType(), 100);
[email protected]dc24976f2013-06-02 21:15:09774 RecordPermissionMessagesHistogram(extension.get(),
775 "Extensions.Permissions_Uninstall");
[email protected]9b217652010-10-08 22:04:23776
[email protected]831aa212010-03-26 13:55:19777 // Unload before doing more cleanup to ensure that nothing is hanging on to
778 // any of these resources.
[email protected]814a7bf0f2011-08-13 05:30:59779 UnloadExtension(extension_id, extension_misc::UNLOAD_REASON_UNINSTALL);
[email protected]831aa212010-03-26 13:55:19780
[email protected]0d6ec3a72011-09-02 02:09:43781 extension_prefs_->OnExtensionUninstalled(extension_id, extension->location(),
[email protected]831aa212010-03-26 13:55:19782 external_uninstall);
[email protected]9f1087e2009-06-15 17:29:32783
784 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]12075d12013-02-27 05:38:05785 if (!Manifest::IsUnpackedLocation(extension->location())) {
[email protected]7f8f24f2012-11-15 19:40:14786 if (!GetFileTaskRunner()->PostTask(
787 FROM_HERE,
[email protected]53612e82011-10-18 18:00:36788 base::Bind(
[email protected]14908b72011-04-20 06:54:36789 &extension_file_util::UninstallExtension,
790 install_directory_,
[email protected]fa2416f2011-05-03 08:41:20791 extension_id)))
[email protected]14908b72011-04-20 06:54:36792 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32793 }
794
[email protected]6b414c232013-06-05 07:53:34795 GURL launch_web_url_origin(
[email protected]cadac622013-06-11 16:46:36796 extensions::AppLaunchInfo::GetLaunchWebURL(extension.get()).GetOrigin());
[email protected]561e33d52013-04-03 06:58:43797 bool is_storage_isolated =
[email protected]dc24976f2013-06-02 21:15:09798 extensions::AppIsolationInfo::HasIsolatedStorage(extension.get());
[email protected]0d6ec3a72011-09-02 02:09:43799
[email protected]399583b2012-12-11 09:33:42800 if (is_storage_isolated) {
801 BrowserContext::AsyncObliterateStoragePartition(
802 profile_,
803 GetSiteForExtensionId(extension_id),
804 base::Bind(&ExtensionService::OnNeedsToGarbageCollectIsolatedStorage,
805 AsWeakPtr()));
806 } else {
807 if (extension->is_hosted_app() &&
808 !profile_->GetExtensionSpecialStoragePolicy()->
809 IsStorageProtected(launch_web_url_origin)) {
810 extensions::DataDeleter::StartDeleting(
811 profile_, extension_id, launch_web_url_origin);
812 }
813 extensions::DataDeleter::StartDeleting(profile_, extension_id,
814 extension->url());
[email protected]0d6ec3a72011-09-02 02:09:43815 }
[email protected]0d6ec3a72011-09-02 02:09:43816
[email protected]0dfe05c2011-02-23 23:03:36817 UntrackTerminatedExtension(extension_id);
[email protected]211030342010-09-30 18:41:06818
819 // Notify interested parties that we've uninstalled this extension.
[email protected]ad50def52011-10-19 23:17:07820 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27821 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
[email protected]6c2381d2011-10-19 02:52:53822 content::Source<Profile>(profile_),
[email protected]dc24976f2013-06-02 21:15:09823 content::Details<const Extension>(extension.get()));
[email protected]d6ebc9792011-04-07 18:18:33824
[email protected]5db9ada2012-04-11 13:48:20825 if (app_sync_bundle_.HasExtensionId(extension_id) &&
[email protected]a4a147652012-07-03 23:41:32826 sync_change.sync_data().GetDataType() == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:20827 app_sync_bundle_.ProcessDeletion(extension_id, sync_change);
828 } else if (extension_sync_bundle_.HasExtensionId(extension_id) &&
[email protected]a4a147652012-07-03 23:41:32829 sync_change.sync_data().GetDataType() == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:20830 extension_sync_bundle_.ProcessDeletion(extension_id, sync_change);
[email protected]3bdba0d2011-08-23 07:17:30831 }
832
[email protected]399583b2012-12-11 09:33:42833 delayed_updates_for_idle_.Remove(extension_id);
834 delayed_installs_.Remove(extension_id);
[email protected]0db124b02012-11-07 04:55:05835
[email protected]333b1de2011-09-12 18:28:50836 // Track the uninstallation.
[email protected]49098f702011-10-13 03:47:18837 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionUninstalled", 1, 2);
838
[email protected]d6ebc9792011-04-07 18:18:33839 return true;
[email protected]c10da4b02010-03-25 14:38:32840}
841
[email protected]c3cfb012011-04-06 22:07:35842bool ExtensionService::IsExtensionEnabled(
843 const std::string& extension_id) const {
[email protected]84df8332011-12-06 18:22:46844 if (extensions_.Contains(extension_id) ||
[email protected]dc9a74f72012-08-17 18:07:21845 terminated_extensions_.Contains(extension_id)) {
[email protected]36429da2011-07-11 20:25:18846 return true;
[email protected]dc9a74f72012-08-17 18:07:21847 }
[email protected]36429da2011-07-11 20:25:18848
[email protected]695b5712012-12-06 23:55:28849 if (disabled_extensions_.Contains(extension_id) ||
850 blacklisted_extensions_.Contains(extension_id)) {
[email protected]ad83ca242011-07-29 01:32:25851 return false;
[email protected]695b5712012-12-06 23:55:28852 }
[email protected]ad83ca242011-07-29 01:32:25853
854 // If the extension hasn't been loaded yet, check the prefs for it. Assume
855 // enabled unless otherwise noted.
856 return !extension_prefs_->IsExtensionDisabled(extension_id) &&
[email protected]f574c402012-12-04 23:20:31857 !extension_prefs_->IsExternalExtensionUninstalled(extension_id);
[email protected]c3cfb012011-04-06 22:07:35858}
859
860bool ExtensionService::IsExternalExtensionUninstalled(
861 const std::string& extension_id) const {
862 return extension_prefs_->IsExternalExtensionUninstalled(extension_id);
863}
864
[email protected]e2377032013-01-18 17:53:22865bool ExtensionService::IsExtensionEnabledForLauncher(
866 const std::string& extension_id) const {
867 return IsExtensionEnabled(extension_id) &&
868 !GetTerminatedExtension(extension_id);
869}
870
[email protected]eaa7dd182010-12-14 11:09:00871void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12872 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20873
[email protected]06f92562011-04-29 19:27:31874 if (IsExtensionEnabled(extension_id))
[email protected]0c6da502009-08-14 22:32:39875 return;
[email protected]0c6da502009-08-14 22:32:39876
[email protected]b3317ad2011-04-28 23:46:00877 extension_prefs_->SetExtensionState(extension_id, Extension::ENABLED);
[email protected]eb5e4f92012-08-15 23:33:28878 extension_prefs_->ClearDisableReasons(extension_id);
[email protected]1784e83a2009-09-08 21:01:52879
[email protected]695b5712012-12-06 23:55:28880 const Extension* extension = disabled_extensions_.GetByID(extension_id);
[email protected]06f92562011-04-29 19:27:31881 // This can happen if sync enables an extension that is not
882 // installed yet.
883 if (!extension)
884 return;
885
[email protected]d96eb512012-11-01 23:44:08886 if (IsUnacknowledgedExternalExtension(extension)) {
887 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
888 EXTERNAL_EXTENSION_REENABLED,
889 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]612a1cb12012-10-17 13:18:03890 AcknowledgeExternalExtension(extension->id());
[email protected]d96eb512012-11-01 23:44:08891 }
[email protected]612a1cb12012-10-17 13:18:03892
[email protected]0c6da502009-08-14 22:32:39893 // Move it over to the enabled list.
[email protected]84df8332011-12-06 18:22:46894 extensions_.Insert(make_scoped_refptr(extension));
895 disabled_extensions_.Remove(extension->id());
[email protected]0c6da502009-08-14 22:32:39896
[email protected]62d30f42009-10-01 22:36:06897 NotifyExtensionLoaded(extension);
[email protected]3bdba0d2011-08-23 07:17:30898
[email protected]f74d7f32012-06-19 19:22:51899 // Notify listeners that the extension was enabled.
900 content::NotificationService::current()->Notify(
901 chrome::NOTIFICATION_EXTENSION_ENABLED,
902 content::Source<Profile>(profile_),
903 content::Details<const Extension>(extension));
904
[email protected]3bdba0d2011-08-23 07:17:30905 SyncExtensionChangeIfNeeded(*extension);
[email protected]0c6da502009-08-14 22:32:39906}
907
[email protected]44d62b62012-04-11 00:06:03908void ExtensionService::DisableExtension(
909 const std::string& extension_id,
910 Extension::DisableReason disable_reason) {
[email protected]a29a517a2011-01-21 21:11:12911 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20912
[email protected]b2ba9962009-12-10 20:10:15913 // The extension may have been disabled already.
[email protected]06f92562011-04-29 19:27:31914 if (!IsExtensionEnabled(extension_id))
[email protected]1784e83a2009-09-08 21:01:52915 return;
[email protected]1784e83a2009-09-08 21:01:52916
[email protected]06f92562011-04-29 19:27:31917 const Extension* extension = GetInstalledExtension(extension_id);
918 // |extension| can be NULL if sync disables an extension that is not
919 // installed yet.
[email protected]65187152012-06-02 13:14:14920 if (extension &&
921 !system_->management_policy()->UserMayModifySettings(extension, NULL)) {
[email protected]95da88c42011-03-31 10:07:33922 return;
[email protected]65187152012-06-02 13:14:14923 }
[email protected]95da88c42011-03-31 10:07:33924
[email protected]b3317ad2011-04-28 23:46:00925 extension_prefs_->SetExtensionState(extension_id, Extension::DISABLED);
[email protected]eb5e4f92012-08-15 23:33:28926 extension_prefs_->AddDisableReason(extension_id, disable_reason);
[email protected]1784e83a2009-09-08 21:01:52927
[email protected]695b5712012-12-06 23:55:28928 int include_mask = INCLUDE_EVERYTHING & ~INCLUDE_DISABLED;
929 extension = GetExtensionById(extension_id, include_mask);
[email protected]06f92562011-04-29 19:27:31930 if (!extension)
931 return;
932
[email protected]64742de32013-04-22 08:44:34933 // Reset the background_page_ready flag
934 if (extensions::BackgroundInfo::HasBackgroundPage(extension))
935 extension_runtime_data_[extension->id()].background_page_ready = false;
936
[email protected]5c094792012-09-07 19:44:53937 // Move it over to the disabled list. Don't send a second unload notification
938 // for terminated extensions being disabled.
[email protected]84df8332011-12-06 18:22:46939 disabled_extensions_.Insert(make_scoped_refptr(extension));
[email protected]5c094792012-09-07 19:44:53940 if (extensions_.Contains(extension->id())) {
[email protected]84df8332011-12-06 18:22:46941 extensions_.Remove(extension->id());
[email protected]5c094792012-09-07 19:44:53942 NotifyExtensionUnloaded(extension, extension_misc::UNLOAD_REASON_DISABLE);
943 } else {
[email protected]84df8332011-12-06 18:22:46944 terminated_extensions_.Remove(extension->id());
[email protected]5c094792012-09-07 19:44:53945 }
[email protected]3bdba0d2011-08-23 07:17:30946
947 SyncExtensionChangeIfNeeded(*extension);
[email protected]1784e83a2009-09-08 21:01:52948}
949
[email protected]e516e4c2013-06-12 17:41:14950void ExtensionService::DisableUserExtensions() {
951 extensions::ManagementPolicy* management_policy =
952 system_->management_policy();
953 extensions::ExtensionList to_disable;
954
955 for (ExtensionSet::const_iterator extension = extensions_.begin();
956 extension != extensions_.end(); ++extension) {
957 if (management_policy->UserMayModifySettings(*extension, NULL))
958 to_disable.push_back(*extension);
959 }
960 for (ExtensionSet::const_iterator extension = terminated_extensions_.begin();
961 extension != terminated_extensions_.end(); ++extension) {
962 if (management_policy->UserMayModifySettings(*extension, NULL))
963 to_disable.push_back(*extension);
964 }
965
966 for (extensions::ExtensionList::const_iterator extension = to_disable.begin();
967 extension != to_disable.end(); ++extension) {
968 DisableExtension((*extension)->id(),
969 extensions::Extension::DISABLE_USER_ACTION);
970 }
971}
972
[email protected]eaa7dd182010-12-14 11:09:00973void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]009633c2013-03-07 22:08:28974 const Extension* extension) {
975 GrantPermissions(extension);
[email protected]fe2dd7742011-04-19 22:52:49976 RecordPermissionMessagesHistogram(
977 extension, "Extensions.Permissions_ReEnable");
[email protected]8d888c12010-11-30 00:00:25978 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
979 EnableExtension(extension->id());
980}
981
[email protected]009633c2013-03-07 22:08:28982void ExtensionService::GrantPermissions(const Extension* extension) {
[email protected]be083862012-09-01 03:53:45983 CHECK(extension);
984 extensions::PermissionsUpdater perms_updater(profile());
[email protected]009633c2013-03-07 22:08:28985 perms_updater.GrantActivePermissions(extension);
[email protected]be083862012-09-01 03:53:45986}
987
[email protected]fe2dd7742011-04-19 22:52:49988// static
989void ExtensionService::RecordPermissionMessagesHistogram(
[email protected]13c68b62013-05-17 11:29:05990 const Extension* extension, const char* histogram) {
[email protected]de415552013-01-23 04:12:17991 // Since this is called from multiple sources, and since the histogram macros
992 // use statics, we need to manually lookup the histogram ourselves.
993 base::HistogramBase* counter = base::LinearHistogram::FactoryGet(
[email protected]fe2dd7742011-04-19 22:52:49994 histogram,
995 1,
[email protected]c2e66e12012-06-27 06:27:06996 PermissionMessage::kEnumBoundary,
997 PermissionMessage::kEnumBoundary + 1,
[email protected]de415552013-01-23 04:12:17998 base::HistogramBase::kUmaTargetedHistogramFlag);
[email protected]fe2dd7742011-04-19 22:52:49999
[email protected]13c68b62013-05-17 11:29:051000 PermissionMessages permissions =
1001 extensions::PermissionsData::GetPermissionMessages(extension);
[email protected]fe2dd7742011-04-19 22:52:491002 if (permissions.empty()) {
[email protected]c2e66e12012-06-27 06:27:061003 counter->Add(PermissionMessage::kNone);
[email protected]fe2dd7742011-04-19 22:52:491004 } else {
[email protected]c2e66e12012-06-27 06:27:061005 for (PermissionMessages::iterator it = permissions.begin();
[email protected]0d3e4a22011-06-23 19:02:521006 it != permissions.end(); ++it)
1007 counter->Add(it->id());
[email protected]fe2dd7742011-04-19 22:52:491008 }
1009}
1010
[email protected]eaa7dd182010-12-14 11:09:001011void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]57a777f72010-03-31 01:09:421012 // The ChromeURLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:061013 // was loaded, otherwise a race can arise where a renderer that is created
1014 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:421015 // that the request context doesn't yet know about. The profile is responsible
1016 // for ensuring its URLRequestContexts appropriately discover the loaded
1017 // extension.
[email protected]31d8f5f22012-04-02 15:22:081018 system_->RegisterExtensionWithRequestContexts(extension);
[email protected]62d30f42009-10-01 22:36:061019
[email protected]d5949312012-12-03 22:13:301020 // Tell renderers about the new extension, unless it's a theme (renderers
1021 // don't need to know about themes).
[email protected]a58f599c2012-12-01 03:08:191022 if (!extension->is_theme()) {
[email protected]19647262011-12-16 09:57:491023 for (content::RenderProcessHost::iterator i(
1024 content::RenderProcessHost::AllHostsIterator());
1025 !i.IsAtEnd(); i.Advance()) {
1026 content::RenderProcessHost* host = i.GetCurrentValue();
1027 Profile* host_profile =
1028 Profile::FromBrowserContext(host->GetBrowserContext());
1029 if (host_profile->GetOriginalProfile() ==
1030 profile_->GetOriginalProfile()) {
1031 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions(
1032 1, ExtensionMsg_Loaded_Params(extension));
1033 host->Send(
1034 new ExtensionMsg_Loaded(loaded_extensions));
1035 }
[email protected]c8d407e2011-04-28 21:27:171036 }
[email protected]77a6970c2011-04-23 16:58:561037 }
1038
[email protected]3442a662012-01-12 08:06:171039 // Tell subsystems that use the EXTENSION_LOADED notification about the new
1040 // extension.
1041 //
1042 // NOTE: It is important that this happen after notifying the renderers about
1043 // the new extensions so that if we navigate to an extension URL in
1044 // NOTIFICATION_EXTENSION_LOADED, the renderer is guaranteed to know about it.
1045 content::NotificationService::current()->Notify(
1046 chrome::NOTIFICATION_EXTENSION_LOADED,
1047 content::Source<Profile>(profile_),
1048 content::Details<const Extension>(extension));
1049
[email protected]c8d407e2011-04-28 21:27:171050 // Tell a random-ass collection of other subsystems about the new extension.
1051 // TODO(aa): What should we do with all this goop? Can it move into the
1052 // relevant objects via EXTENSION_LOADED?
1053
1054 profile_->GetExtensionSpecialStoragePolicy()->
1055 GrantRightsForExtension(extension);
1056
1057 UpdateActiveExtensionsInCrashReporter();
1058
[email protected]c8d407e2011-04-28 21:27:171059 // If the extension has permission to load chrome://favicon/ resources we need
1060 // to make sure that the FaviconSource is registered with the
1061 // ChromeURLDataManager.
[email protected]13c68b62013-05-17 11:29:051062 if (extensions::PermissionsData::HasHostPermission(
1063 extension, GURL(chrome::kChromeUIFaviconURL))) {
[email protected]c8d407e2011-04-28 21:27:171064 FaviconSource* favicon_source = new FaviconSource(profile_,
1065 FaviconSource::FAVICON);
[email protected]24ea7a12013-01-27 23:54:531066 content::URLDataSource::Add(profile_, favicon_source);
[email protected]c8d407e2011-04-28 21:27:171067 }
[email protected]b07e606e2012-09-15 20:16:151068
1069#if !defined(OS_ANDROID)
1070 // Same for chrome://theme/ resources.
[email protected]13c68b62013-05-17 11:29:051071 if (extensions::PermissionsData::HasHostPermission(
1072 extension, GURL(chrome::kChromeUIThemeURL))) {
[email protected]b07e606e2012-09-15 20:16:151073 ThemeSource* theme_source = new ThemeSource(profile_);
[email protected]24ea7a12013-01-27 23:54:531074 content::URLDataSource::Add(profile_, theme_source);
[email protected]b07e606e2012-09-15 20:16:151075 }
1076#endif
1077
[email protected]5eddc3e2011-10-26 04:33:311078 // Same for chrome://thumb/ resources.
[email protected]13c68b62013-05-17 11:29:051079 if (extensions::PermissionsData::HasHostPermission(
1080 extension, GURL(chrome::kChromeUIThumbnailURL))) {
[email protected]5eddc3e2011-10-26 04:33:311081 ThumbnailSource* thumbnail_source = new ThumbnailSource(profile_);
[email protected]24ea7a12013-01-27 23:54:531082 content::URLDataSource::Add(profile_, thumbnail_source);
[email protected]5eddc3e2011-10-26 04:33:311083 }
[email protected]62d30f42009-10-01 22:36:061084}
1085
[email protected]a9f39a312010-12-23 22:14:271086void ExtensionService::NotifyExtensionUnloaded(
[email protected]814a7bf0f2011-08-13 05:30:591087 const Extension* extension,
1088 extension_misc::UnloadedExtensionReason reason) {
[email protected]a9f39a312010-12-23 22:14:271089 UnloadedExtensionInfo details(extension, reason);
[email protected]ad50def52011-10-19 23:17:071090 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271091 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]6c2381d2011-10-19 02:52:531092 content::Source<Profile>(profile_),
1093 content::Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061094
[email protected]903d70a2012-12-04 13:00:171095#if defined(ENABLE_THEMES)
1096 // If the current theme is being unloaded, tell ThemeService to revert back
1097 // to the default theme.
1098 if (reason != extension_misc::UNLOAD_REASON_UPDATE && extension->is_theme()) {
1099 ThemeService* theme_service = ThemeServiceFactory::GetForProfile(profile_);
1100 if (extension->id() == theme_service->GetThemeID())
1101 theme_service->UseDefaultTheme();
1102 }
1103#endif
1104
[email protected]f3b1a082011-11-18 00:34:301105 for (content::RenderProcessHost::iterator i(
1106 content::RenderProcessHost::AllHostsIterator());
[email protected]77a6970c2011-04-23 16:58:561107 !i.IsAtEnd(); i.Advance()) {
[email protected]f3b1a082011-11-18 00:34:301108 content::RenderProcessHost* host = i.GetCurrentValue();
[email protected]9b62ecf2011-07-27 20:23:081109 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:301110 Profile::FromBrowserContext(host->GetBrowserContext());
[email protected]9b62ecf2011-07-27 20:23:081111 if (host_profile->GetOriginalProfile() == profile_->GetOriginalProfile())
[email protected]c8d407e2011-04-28 21:27:171112 host->Send(new ExtensionMsg_Unloaded(extension->id()));
[email protected]77a6970c2011-04-23 16:58:561113 }
1114
[email protected]31d8f5f22012-04-02 15:22:081115 system_->UnregisterExtensionWithRequestContexts(extension->id(), reason);
[email protected]c8d407e2011-04-28 21:27:171116 profile_->GetExtensionSpecialStoragePolicy()->
1117 RevokeRightsForExtension(extension);
1118
[email protected]b777b332011-04-16 04:01:081119#if defined(OS_CHROMEOS)
[email protected]ac66e452013-01-11 09:21:231120 // Revoke external file access for the extension from its file system context.
1121 // It is safe to access the extension's storage partition at this point. The
1122 // storage partition may get destroyed only after the extension gets unloaded.
1123 GURL site = extensions::ExtensionSystem::Get(profile_)->extension_service()->
1124 GetSiteForExtensionId(extension->id());
[email protected]10eb28162012-09-18 03:04:091125 fileapi::FileSystemContext* filesystem_context =
[email protected]ac66e452013-01-11 09:21:231126 BrowserContext::GetStoragePartitionForSite(profile_, site)->
[email protected]10eb28162012-09-18 03:04:091127 GetFileSystemContext();
1128 if (filesystem_context && filesystem_context->external_provider()) {
1129 filesystem_context->external_provider()->
[email protected]c8d407e2011-04-28 21:27:171130 RevokeAccessForExtension(extension->id());
[email protected]62d30f42009-10-01 22:36:061131 }
[email protected]c8d407e2011-04-28 21:27:171132#endif
1133
1134 UpdateActiveExtensionsInCrashReporter();
[email protected]62d30f42009-10-01 22:36:061135}
1136
[email protected]0a071a32011-02-08 00:18:241137Profile* ExtensionService::profile() {
1138 return profile_;
1139}
1140
[email protected]45759612012-07-10 17:21:231141extensions::ExtensionPrefs* ExtensionService::extension_prefs() {
[email protected]ddb1e5a2010-12-13 20:10:451142 return extension_prefs_;
1143}
1144
[email protected]0d9a2202011-11-09 13:48:411145extensions::SettingsFrontend* ExtensionService::settings_frontend() {
[email protected]cccdf0aa2011-11-11 03:43:381146 return settings_frontend_.get();
[email protected]b7f853e282011-08-10 09:24:201147}
1148
[email protected]e2d720aa2012-05-17 00:11:351149extensions::ContentSettingsStore* ExtensionService::GetContentSettingsStore() {
[email protected]b790b072011-05-20 09:46:441150 return extension_prefs()->content_settings_store();
1151}
1152
[email protected]25ae0152011-11-18 14:40:021153bool ExtensionService::is_ready() {
[email protected]4a10006a2013-05-17 23:18:351154 return ready_->is_signaled();
[email protected]25ae0152011-11-18 14:40:021155}
1156
[email protected]7f8f24f2012-11-15 19:40:141157base::SequencedTaskRunner* ExtensionService::GetFileTaskRunner() {
[email protected]dc24976f2013-06-02 21:15:091158 if (file_task_runner_.get())
1159 return file_task_runner_.get();
[email protected]7f8f24f2012-11-15 19:40:141160
1161 // We should be able to interrupt any part of extension install process during
1162 // shutdown. SKIP_ON_SHUTDOWN ensures that not started extension install tasks
1163 // will be ignored/deleted while we will block on started tasks.
1164 std::string token("ext_install-");
1165 token.append(profile_->GetPath().AsUTF8Unsafe());
1166 file_task_runner_ = BrowserThread::GetBlockingPool()->
1167 GetSequencedTaskRunnerWithShutdownBehavior(
1168 BrowserThread::GetBlockingPool()->GetNamedSequenceToken(token),
1169 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
[email protected]dc24976f2013-06-02 21:15:091170 return file_task_runner_.get();
[email protected]7f8f24f2012-11-15 19:40:141171}
1172
[email protected]42a08162012-03-16 18:09:111173extensions::ExtensionUpdater* ExtensionService::updater() {
[email protected]2859946f2011-04-04 18:18:061174 return updater_.get();
1175}
1176
[email protected]4ee07c62012-08-21 12:40:421177void ExtensionService::CheckManagementPolicy() {
[email protected]aa96d3a2010-08-21 08:45:251178 std::vector<std::string> to_be_removed;
[email protected]695b5712012-12-06 23:55:281179
[email protected]aa96d3a2010-08-21 08:45:251180 // Loop through extensions list, unload installed extensions.
[email protected]84df8332011-12-06 18:22:461181 for (ExtensionSet::const_iterator iter = extensions_.begin();
[email protected]aa96d3a2010-08-21 08:45:251182 iter != extensions_.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361183 const Extension* extension = (iter->get());
[email protected]4ee07c62012-08-21 12:40:421184 if (!system_->management_policy()->UserMayLoad(extension, NULL))
[email protected]aa96d3a2010-08-21 08:45:251185 to_be_removed.push_back(extension->id());
1186 }
1187
1188 // UnloadExtension will change the extensions_ list. So, we should
1189 // call it outside the iterator loop.
[email protected]31bb5ee62012-09-12 22:58:401190 for (size_t i = 0; i < to_be_removed.size(); ++i)
[email protected]814a7bf0f2011-08-13 05:30:591191 UnloadExtension(to_be_removed[i], extension_misc::UNLOAD_REASON_DISABLE);
[email protected]aa96d3a2010-08-21 08:45:251192}
1193
[email protected]31206602011-04-13 23:07:321194void ExtensionService::CheckForUpdatesSoon() {
[email protected]c3cfb012011-04-06 22:07:351195 if (updater()) {
[email protected]94fde232012-04-27 10:22:301196 if (AreAllExternalProvidersReady()) {
1197 updater()->CheckSoon();
1198 } else {
1199 // Sync can start updating before all the external providers are ready
1200 // during startup. Start the update as soon as those providers are ready,
1201 // but not before.
1202 update_once_all_providers_are_ready_ = true;
1203 }
[email protected]c3cfb012011-04-06 22:07:351204 } else {
[email protected]31206602011-04-13 23:07:321205 LOG(WARNING) << "CheckForUpdatesSoon() called with auto-update turned off";
[email protected]c3cfb012011-04-06 22:07:351206 }
1207}
1208
[email protected]3f98fd92012-11-17 15:54:291209syncer::SyncMergeResult ExtensionService::MergeDataAndStartSyncing(
[email protected]a4a147652012-07-03 23:41:321210 syncer::ModelType type,
[email protected]65f173552012-06-28 22:43:581211 const syncer::SyncDataList& initial_sync_data,
1212 scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
1213 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) {
[email protected]5db9ada2012-04-11 13:48:201214 CHECK(sync_processor.get());
[email protected]5d36e052012-04-20 20:12:281215 CHECK(sync_error_factory.get());
[email protected]3bdba0d2011-08-23 07:17:301216
1217 switch (type) {
[email protected]a4a147652012-07-03 23:41:321218 case syncer::EXTENSIONS:
[email protected]5db9ada2012-04-11 13:48:201219 extension_sync_bundle_.SetupSync(sync_processor.release(),
[email protected]5d36e052012-04-20 20:12:281220 sync_error_factory.release(),
[email protected]5db9ada2012-04-11 13:48:201221 initial_sync_data);
[email protected]3bdba0d2011-08-23 07:17:301222 break;
1223
[email protected]a4a147652012-07-03 23:41:321224 case syncer::APPS:
[email protected]5d36e052012-04-20 20:12:281225 app_sync_bundle_.SetupSync(sync_processor.release(),
1226 sync_error_factory.release(),
1227 initial_sync_data);
[email protected]3bdba0d2011-08-23 07:17:301228 break;
1229
1230 default:
1231 LOG(FATAL) << "Got " << type << " ModelType";
1232 }
[email protected]3bdba0d2011-08-23 07:17:301233
[email protected]b1417ab2011-12-15 17:09:111234 // Process local extensions.
1235 // TODO(yoz): Determine whether pending extensions should be considered too.
1236 // See crbug.com/104399.
[email protected]65f173552012-06-28 22:43:581237 syncer::SyncDataList sync_data_list = GetAllSyncData(type);
1238 syncer::SyncChangeList sync_change_list;
1239 for (syncer::SyncDataList::const_iterator i = sync_data_list.begin();
[email protected]3bdba0d2011-08-23 07:17:301240 i != sync_data_list.end();
1241 ++i) {
[email protected]5db9ada2012-04-11 13:48:201242 switch (type) {
[email protected]a4a147652012-07-03 23:41:321243 case syncer::EXTENSIONS:
[email protected]5db9ada2012-04-11 13:48:201244 sync_change_list.push_back(
1245 extension_sync_bundle_.CreateSyncChange(*i));
1246 break;
[email protected]a4a147652012-07-03 23:41:321247 case syncer::APPS:
[email protected]5db9ada2012-04-11 13:48:201248 sync_change_list.push_back(app_sync_bundle_.CreateSyncChange(*i));
1249 break;
1250 default:
1251 LOG(FATAL) << "Got " << type << " ModelType";
[email protected]31bb5ee62012-09-12 22:58:401252 }
[email protected]3bdba0d2011-08-23 07:17:301253 }
[email protected]3bdba0d2011-08-23 07:17:301254
[email protected]5db9ada2012-04-11 13:48:201255
[email protected]a4a147652012-07-03 23:41:321256 if (type == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:201257 extension_sync_bundle_.ProcessSyncChangeList(sync_change_list);
[email protected]a4a147652012-07-03 23:41:321258 } else if (type == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:201259 app_sync_bundle_.ProcessSyncChangeList(sync_change_list);
1260 }
[email protected]0ffd9e52012-01-26 21:44:341261
[email protected]3f98fd92012-11-17 15:54:291262 return syncer::SyncMergeResult(type);
[email protected]3bdba0d2011-08-23 07:17:301263}
1264
[email protected]a4a147652012-07-03 23:41:321265void ExtensionService::StopSyncing(syncer::ModelType type) {
1266 if (type == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:201267 app_sync_bundle_.Reset();
[email protected]a4a147652012-07-03 23:41:321268 } else if (type == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:201269 extension_sync_bundle_.Reset();
1270 }
[email protected]3bdba0d2011-08-23 07:17:301271}
1272
[email protected]65f173552012-06-28 22:43:581273syncer::SyncDataList ExtensionService::GetAllSyncData(
[email protected]a4a147652012-07-03 23:41:321274 syncer::ModelType type) const {
[email protected]31bb5ee62012-09-12 22:58:401275 if (type == syncer::EXTENSIONS)
[email protected]5db9ada2012-04-11 13:48:201276 return extension_sync_bundle_.GetAllSyncData();
[email protected]31bb5ee62012-09-12 22:58:401277 if (type == syncer::APPS)
[email protected]5db9ada2012-04-11 13:48:201278 return app_sync_bundle_.GetAllSyncData();
[email protected]5db9ada2012-04-11 13:48:201279
1280 // We should only get sync data for extensions and apps.
1281 NOTREACHED();
1282
[email protected]65f173552012-06-28 22:43:581283 return syncer::SyncDataList();
[email protected]3bdba0d2011-08-23 07:17:301284}
1285
[email protected]65f173552012-06-28 22:43:581286syncer::SyncError ExtensionService::ProcessSyncChanges(
[email protected]3bdba0d2011-08-23 07:17:301287 const tracked_objects::Location& from_here,
[email protected]65f173552012-06-28 22:43:581288 const syncer::SyncChangeList& change_list) {
1289 for (syncer::SyncChangeList::const_iterator i = change_list.begin();
[email protected]3bdba0d2011-08-23 07:17:301290 i != change_list.end();
1291 ++i) {
[email protected]a4a147652012-07-03 23:41:321292 syncer::ModelType type = i->sync_data().GetDataType();
1293 if (type == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:201294 extension_sync_bundle_.ProcessSyncChange(
1295 extensions::ExtensionSyncData(*i));
[email protected]a4a147652012-07-03 23:41:321296 } else if (type == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:201297 app_sync_bundle_.ProcessSyncChange(extensions::AppSyncData(*i));
1298 }
[email protected]3bdba0d2011-08-23 07:17:301299 }
1300
[email protected]0ffd9e52012-01-26 21:44:341301 extension_prefs()->extension_sorting()->FixNTPOrdinalCollisions();
1302
[email protected]65f173552012-06-28 22:43:581303 return syncer::SyncError();
[email protected]b05fb9ff2011-04-23 00:07:561304}
1305
[email protected]5db9ada2012-04-11 13:48:201306extensions::ExtensionSyncData ExtensionService::GetExtensionSyncData(
1307 const Extension& extension) const {
1308 return extensions::ExtensionSyncData(extension,
1309 IsExtensionEnabled(extension.id()),
1310 IsIncognitoEnabled(extension.id()));
[email protected]b05fb9ff2011-04-23 00:07:561311}
1312
[email protected]5db9ada2012-04-11 13:48:201313extensions::AppSyncData ExtensionService::GetAppSyncData(
1314 const Extension& extension) const {
1315 return extensions::AppSyncData(
1316 extension,
1317 IsExtensionEnabled(extension.id()),
1318 IsIncognitoEnabled(extension.id()),
[email protected]5db9ada2012-04-11 13:48:201319 extension_prefs_->extension_sorting()->GetAppLaunchOrdinal(
1320 extension.id()),
1321 extension_prefs_->extension_sorting()->GetPageOrdinal(extension.id()));
1322}
[email protected]3bdba0d2011-08-23 07:17:301323
[email protected]5db9ada2012-04-11 13:48:201324std::vector<extensions::ExtensionSyncData>
1325 ExtensionService::GetExtensionSyncDataList() const {
1326 std::vector<extensions::ExtensionSyncData> extension_sync_list;
1327 extension_sync_bundle_.GetExtensionSyncDataListHelper(extensions_,
1328 &extension_sync_list);
1329 extension_sync_bundle_.GetExtensionSyncDataListHelper(disabled_extensions_,
1330 &extension_sync_list);
1331 extension_sync_bundle_.GetExtensionSyncDataListHelper(terminated_extensions_,
1332 &extension_sync_list);
1333
1334 std::vector<extensions::ExtensionSyncData> pending_extensions =
1335 extension_sync_bundle_.GetPendingData();
1336 extension_sync_list.insert(extension_sync_list.begin(),
1337 pending_extensions.begin(),
1338 pending_extensions.end());
[email protected]3bdba0d2011-08-23 07:17:301339
1340 return extension_sync_list;
[email protected]b05fb9ff2011-04-23 00:07:561341}
1342
[email protected]5db9ada2012-04-11 13:48:201343std::vector<extensions::AppSyncData> ExtensionService::GetAppSyncDataList()
1344 const {
1345 std::vector<extensions::AppSyncData> app_sync_list;
1346 app_sync_bundle_.GetAppSyncDataListHelper(extensions_, &app_sync_list);
1347 app_sync_bundle_.GetAppSyncDataListHelper(disabled_extensions_,
1348 &app_sync_list);
1349 app_sync_bundle_.GetAppSyncDataListHelper(terminated_extensions_,
1350 &app_sync_list);
1351
1352 std::vector<extensions::AppSyncData> pending_apps =
1353 app_sync_bundle_.GetPendingData();
1354 app_sync_list.insert(app_sync_list.begin(),
1355 pending_apps.begin(),
1356 pending_apps.end());
1357
1358 return app_sync_list;
1359}
1360
1361bool ExtensionService::ProcessExtensionSyncData(
1362 const extensions::ExtensionSyncData& extension_sync_data) {
1363 if (!ProcessExtensionSyncDataHelper(extension_sync_data,
[email protected]a4a147652012-07-03 23:41:321364 syncer::EXTENSIONS)) {
[email protected]5db9ada2012-04-11 13:48:201365 extension_sync_bundle_.AddPendingExtension(extension_sync_data.id(),
1366 extension_sync_data);
1367 CheckForUpdatesSoon();
1368 return false;
1369 }
1370
1371 return true;
1372}
1373
1374bool ExtensionService::ProcessAppSyncData(
1375 const extensions::AppSyncData& app_sync_data) {
1376 const std::string& id = app_sync_data.id();
[email protected]5db9ada2012-04-11 13:48:201377
1378 if (app_sync_data.app_launch_ordinal().IsValid() &&
1379 app_sync_data.page_ordinal().IsValid()) {
1380 extension_prefs_->extension_sorting()->SetAppLaunchOrdinal(
1381 id,
1382 app_sync_data.app_launch_ordinal());
1383 extension_prefs_->extension_sorting()->SetPageOrdinal(
1384 id,
1385 app_sync_data.page_ordinal());
1386 }
1387
[email protected]5db9ada2012-04-11 13:48:201388 if (!ProcessExtensionSyncDataHelper(app_sync_data.extension_sync_data(),
[email protected]a4a147652012-07-03 23:41:321389 syncer::APPS)) {
[email protected]5db9ada2012-04-11 13:48:201390 app_sync_bundle_.AddPendingApp(id, app_sync_data);
1391 CheckForUpdatesSoon();
1392 return false;
1393 }
1394
1395 return true;
1396}
1397
1398bool ExtensionService::IsCorrectSyncType(const Extension& extension,
[email protected]a4a147652012-07-03 23:41:321399 syncer::ModelType type) const {
1400 if (type == syncer::EXTENSIONS &&
[email protected]8f3bcbd2013-06-05 08:42:401401 extensions::sync_helper::IsSyncableExtension(&extension)) {
[email protected]5db9ada2012-04-11 13:48:201402 return true;
[email protected]31bb5ee62012-09-12 22:58:401403 }
[email protected]5db9ada2012-04-11 13:48:201404
[email protected]a4a147652012-07-03 23:41:321405 if (type == syncer::APPS &&
[email protected]8f3bcbd2013-06-05 08:42:401406 extensions::sync_helper::IsSyncableApp(&extension)) {
[email protected]5db9ada2012-04-11 13:48:201407 return true;
[email protected]31bb5ee62012-09-12 22:58:401408 }
[email protected]5db9ada2012-04-11 13:48:201409
1410 return false;
1411}
1412
1413bool ExtensionService::ProcessExtensionSyncDataHelper(
1414 const extensions::ExtensionSyncData& extension_sync_data,
[email protected]a4a147652012-07-03 23:41:321415 syncer::ModelType type) {
[email protected]3bdba0d2011-08-23 07:17:301416 const std::string& id = extension_sync_data.id();
[email protected]96e989b2011-08-30 19:35:061417 const Extension* extension = GetInstalledExtension(id);
1418
1419 // TODO(bolms): we should really handle this better. The particularly bad
1420 // case is where an app becomes an extension or vice versa, and we end up with
1421 // a zombie extension that won't go away.
[email protected]5db9ada2012-04-11 13:48:201422 if (extension && !IsCorrectSyncType(*extension, type))
1423 return true;
[email protected]90310d92011-04-17 07:35:041424
1425 // Handle uninstalls first.
[email protected]3bdba0d2011-08-23 07:17:301426 if (extension_sync_data.uninstalled()) {
[email protected]90310d92011-04-17 07:35:041427 std::string error;
1428 if (!UninstallExtensionHelper(this, id)) {
1429 LOG(WARNING) << "Could not uninstall extension " << id
1430 << " for sync";
1431 }
[email protected]5db9ada2012-04-11 13:48:201432 return true;
[email protected]90310d92011-04-17 07:35:041433 }
1434
[email protected]caf2d442012-05-04 10:00:501435 // Extension from sync was uninstalled by the user as external extensions.
1436 // Honor user choice and skip installation/enabling.
1437 if (IsExternalExtensionUninstalled(id)) {
1438 LOG(WARNING) << "Extension with id " << id
1439 << " from sync was uninstalled as external extension";
1440 return true;
1441 }
1442
[email protected]06f92562011-04-29 19:27:311443 // Set user settings.
[email protected]7e9e2422012-12-13 19:54:191444 // If the extension has been disabled from sync, it may not have
1445 // been installed yet, so we don't know if the disable reason was a
[email protected]b914e2952013-04-26 07:10:031446 // permissions increase. That will be updated once CheckPermissionsIncrease
[email protected]7e9e2422012-12-13 19:54:191447 // is called for it.
[email protected]31bb5ee62012-09-12 22:58:401448 if (extension_sync_data.enabled())
[email protected]06f92562011-04-29 19:27:311449 EnableExtension(id);
[email protected]31bb5ee62012-09-12 22:58:401450 else
[email protected]7e9e2422012-12-13 19:54:191451 DisableExtension(id, Extension::DISABLE_UNKNOWN_FROM_SYNC);
[email protected]81b14cc2011-04-29 00:39:371452
[email protected]406b5a92011-11-08 11:58:261453 // We need to cache some version information here because setting the
1454 // incognito flag invalidates the |extension| pointer (it reloads the
1455 // extension).
[email protected]9060d8b02012-01-13 02:14:301456 bool extension_installed = (extension != NULL);
[email protected]406b5a92011-11-08 11:58:261457 int result = extension ?
1458 extension->version()->CompareTo(extension_sync_data.version()) : 0;
1459 SetIsIncognitoEnabled(id, extension_sync_data.incognito_enabled());
1460 extension = NULL; // No longer safe to use.
1461
1462 if (extension_installed) {
[email protected]06f92562011-04-29 19:27:311463 // If the extension is already installed, check if it's outdated.
[email protected]90310d92011-04-17 07:35:041464 if (result < 0) {
1465 // Extension is outdated.
[email protected]5db9ada2012-04-11 13:48:201466 return false;
[email protected]b020dc12011-12-06 04:35:421467 }
[email protected]06f92562011-04-29 19:27:311468 } else {
[email protected]06f92562011-04-29 19:27:311469 // TODO(akalin): Replace silent update with a list of enabled
1470 // permissions.
[email protected]6cc7dbae2011-04-29 21:18:331471 const bool kInstallSilently = true;
[email protected]5db9ada2012-04-11 13:48:201472
[email protected]a4a147652012-07-03 23:41:321473 CHECK(type == syncer::EXTENSIONS || type == syncer::APPS);
[email protected]8f3bcbd2013-06-05 08:42:401474 extensions::PendingExtensionInfo::ShouldAllowInstallPredicate filter =
1475 (type == syncer::APPS) ? extensions::sync_helper::IsSyncableApp :
1476 extensions::sync_helper::IsSyncableExtension;
[email protected]5db9ada2012-04-11 13:48:201477
[email protected]06f92562011-04-29 19:27:311478 if (!pending_extension_manager()->AddFromSync(
1479 id,
[email protected]3bdba0d2011-08-23 07:17:301480 extension_sync_data.update_url(),
[email protected]5db9ada2012-04-11 13:48:201481 filter,
[email protected]6cc7dbae2011-04-29 21:18:331482 kInstallSilently)) {
[email protected]06f92562011-04-29 19:27:311483 LOG(WARNING) << "Could not add pending extension for " << id;
[email protected]3bdba0d2011-08-23 07:17:301484 // This means that the extension is already pending installation, with a
1485 // non-INTERNAL location. Add to pending_sync_data, even though it will
1486 // never be removed (we'll never install a syncable version of the
1487 // extension), so that GetAllSyncData() continues to send it.
[email protected]06f92562011-04-29 19:27:311488 }
[email protected]3bdba0d2011-08-23 07:17:301489 // Track pending extensions so that we can return them in GetAllSyncData().
[email protected]5db9ada2012-04-11 13:48:201490 return false;
[email protected]90310d92011-04-17 07:35:041491 }
[email protected]5db9ada2012-04-11 13:48:201492
1493 return true;
[email protected]90310d92011-04-17 07:35:041494}
1495
[email protected]c3cfb012011-04-06 22:07:351496bool ExtensionService::IsIncognitoEnabled(
1497 const std::string& extension_id) const {
[email protected]cdc7b1f42012-12-07 19:39:481498 const Extension* extension = GetInstalledExtension(extension_id);
1499 if (extension && !extension->can_be_incognito_enabled())
1500 return false;
[email protected]c3cfb012011-04-06 22:07:351501 // If this is an existing component extension we always allow it to
1502 // work in incognito mode.
[email protected]1d5e58b2013-01-31 08:41:401503 if (extension && extension->location() == Manifest::COMPONENT)
[email protected]cb0ce1e022010-03-10 19:54:411504 return true;
1505
1506 // Check the prefs.
[email protected]c3cfb012011-04-06 22:07:351507 return extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]db7331a2010-02-25 22:10:501508}
[email protected]55a35692010-02-11 23:25:211509
[email protected]c3cfb012011-04-06 22:07:351510void ExtensionService::SetIsIncognitoEnabled(
1511 const std::string& extension_id, bool enabled) {
[email protected]81b14cc2011-04-29 00:39:371512 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]cdc7b1f42012-12-07 19:39:481513 if (extension && !extension->can_be_incognito_enabled())
1514 return;
[email protected]1d5e58b2013-01-31 08:41:401515 if (extension && extension->location() == Manifest::COMPONENT) {
[email protected]b873cd92012-02-09 21:51:481516 // This shouldn't be called for component extensions unless they are
1517 // syncable.
[email protected]8f3bcbd2013-06-05 08:42:401518 DCHECK(extensions::sync_helper::IsSyncable(extension));
[email protected]b873cd92012-02-09 21:51:481519
1520 // If we are here, make sure the we aren't trying to change the value.
1521 DCHECK_EQ(enabled, IsIncognitoEnabled(extension_id));
1522
[email protected]c3cfb012011-04-06 22:07:351523 return;
1524 }
1525
[email protected]8c6c8c22011-03-09 12:52:341526 // Broadcast unloaded and loaded events to update browser state. Only bother
1527 // if the value changed and the extension is actually enabled, since there is
1528 // no UI otherwise.
[email protected]c3cfb012011-04-06 22:07:351529 bool old_enabled = extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]8c6c8c22011-03-09 12:52:341530 if (enabled == old_enabled)
1531 return;
1532
[email protected]c3cfb012011-04-06 22:07:351533 extension_prefs_->SetIsIncognitoEnabled(extension_id, enabled);
[email protected]81b14cc2011-04-29 00:39:371534
[email protected]ffbf3f12011-12-09 22:10:331535 bool extension_is_enabled = extensions_.Contains(extension_id);
[email protected]bd9892eb2011-11-11 19:41:341536
1537 // When we reload the extension the ID may be invalidated if we've passed it
1538 // by const ref everywhere. Make a copy to be safe.
1539 std::string id = extension_id;
[email protected]406b5a92011-11-08 11:58:261540 if (extension_is_enabled)
[email protected]ffbf3f12011-12-09 22:10:331541 ReloadExtension(id);
[email protected]3bdba0d2011-08-23 07:17:301542
[email protected]406b5a92011-11-08 11:58:261543 // Reloading the extension invalidates the |extension| pointer.
[email protected]bd9892eb2011-11-11 19:41:341544 extension = GetInstalledExtension(id);
[email protected]3bdba0d2011-08-23 07:17:301545 if (extension)
1546 SyncExtensionChangeIfNeeded(*extension);
[email protected]55a35692010-02-11 23:25:211547}
1548
[email protected]dc9a74f72012-08-17 18:07:211549bool ExtensionService::CanCrossIncognito(const Extension* extension) const {
[email protected]2a8f24e2010-11-03 21:37:051550 // We allow the extension to see events and data from another profile iff it
1551 // uses "spanning" behavior and it has incognito access. "split" mode
1552 // extensions only see events for a matching profile.
[email protected]0c41298f2011-09-13 21:17:221553 CHECK(extension);
[email protected]c3cfb012011-04-06 22:07:351554 return IsIncognitoEnabled(extension->id()) &&
[email protected]06492ed2013-03-24 22:13:141555 !extensions::IncognitoInfo::IsSplitMode(extension);
[email protected]2a8f24e2010-11-03 21:37:051556}
1557
[email protected]5ef7b002011-05-12 23:16:201558bool ExtensionService::CanLoadInIncognito(const Extension* extension) const {
1559 if (extension->is_hosted_app())
1560 return true;
1561 // Packaged apps and regular extensions need to be enabled specifically for
1562 // incognito (and split mode should be set).
[email protected]06492ed2013-03-24 22:13:141563 return extensions::IncognitoInfo::IsSplitMode(extension) &&
[email protected]5ef7b002011-05-12 23:16:201564 IsIncognitoEnabled(extension->id());
1565}
1566
[email protected]168389f2011-12-20 17:12:481567void ExtensionService::OnExtensionMoved(
1568 const std::string& moved_extension_id,
1569 const std::string& predecessor_extension_id,
1570 const std::string& successor_extension_id) {
[email protected]3569b502012-01-12 20:08:231571 extension_prefs_->extension_sorting()->OnExtensionMoved(
1572 moved_extension_id,
1573 predecessor_extension_id,
1574 successor_extension_id);
[email protected]168389f2011-12-20 17:12:481575
1576 const Extension* extension = GetInstalledExtension(moved_extension_id);
1577 if (extension)
1578 SyncExtensionChangeIfNeeded(*extension);
1579}
1580
[email protected]dc9a74f72012-08-17 18:07:211581bool ExtensionService::AllowFileAccess(const Extension* extension) const {
[email protected]05c82182010-06-24 17:49:081582 return (CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]334e04a2010-06-24 23:34:441583 switches::kDisableExtensionsFileAccessCheck) ||
[email protected]05c82182010-06-24 17:49:081584 extension_prefs_->AllowFileAccess(extension->id()));
1585}
1586
[email protected]eaa7dd182010-12-14 11:09:001587void ExtensionService::SetAllowFileAccess(const Extension* extension,
[email protected]3aff9ad2011-04-01 20:26:481588 bool allow) {
1589 // Reload to update browser state. Only bother if the value changed and the
1590 // extension is actually enabled, since there is no UI otherwise.
1591 bool old_allow = AllowFileAccess(extension);
1592 if (allow == old_allow)
1593 return;
1594
[email protected]05c82182010-06-24 17:49:081595 extension_prefs_->SetAllowFileAccess(extension->id(), allow);
[email protected]3aff9ad2011-04-01 20:26:481596
[email protected]84df8332011-12-06 18:22:461597 bool extension_is_enabled = extensions_.Contains(extension->id());
[email protected]3aff9ad2011-04-01 20:26:481598 if (extension_is_enabled)
1599 ReloadExtension(extension->id());
[email protected]05c82182010-06-24 17:49:081600}
1601
[email protected]8e4560b62011-01-14 10:09:141602// Some extensions will autoupdate themselves externally from Chrome. These
1603// are typically part of some larger client application package. To support
1604// these, the extension will register its location in the the preferences file
1605// (and also, on Windows, in the registry) and this code will periodically
1606// check that location for a .crx file, which it will then install locally if
1607// a new version is available.
1608// Errors are reported through ExtensionErrorReporter. Succcess is not
1609// reported.
[email protected]eaa7dd182010-12-14 11:09:001610void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:121611 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]8e4560b62011-01-14 10:09:141612
1613 // Note that this installation is intentionally silent (since it didn't
1614 // go through the front-end). Extensions that are registered in this
1615 // way are effectively considered 'pre-bundled', and so implicitly
1616 // trusted. In general, if something has HKLM or filesystem access,
1617 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:121618
[email protected]8e4560b62011-01-14 10:09:141619 // Ask each external extension provider to give us a call back for each
1620 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
[email protected]5df038b2012-07-16 19:03:271621 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141622 for (i = external_extension_providers_.begin();
1623 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:271624 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:141625 provider->VisitRegisteredExtension();
1626 }
1627
[email protected]50067e52011-10-20 23:17:071628 // Do any required work that we would have done after completion of all
1629 // providers.
1630 if (external_extension_providers_.empty()) {
1631 OnAllExternalProvidersReady();
1632 }
[email protected]9f1087e2009-06-15 17:29:321633}
1634
[email protected]50067e52011-10-20 23:17:071635void ExtensionService::OnExternalProviderReady(
[email protected]5df038b2012-07-16 19:03:271636 const extensions::ExternalProviderInterface* provider) {
[email protected]a29a517a2011-01-21 21:11:121637 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]50067e52011-10-20 23:17:071638 CHECK(provider->IsReady());
[email protected]a29a517a2011-01-21 21:11:121639
1640 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:141641 // if all of them are finished. So we check them first.
[email protected]94fde232012-04-27 10:22:301642 if (AreAllExternalProvidersReady())
1643 OnAllExternalProvidersReady();
1644}
1645
1646bool ExtensionService::AreAllExternalProvidersReady() const {
[email protected]5df038b2012-07-16 19:03:271647 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141648 for (i = external_extension_providers_.begin();
1649 i != external_extension_providers_.end(); ++i) {
[email protected]94fde232012-04-27 10:22:301650 if (!i->get()->IsReady())
1651 return false;
[email protected]8e4560b62011-01-14 10:09:141652 }
[email protected]94fde232012-04-27 10:22:301653 return true;
[email protected]50067e52011-10-20 23:17:071654}
1655
1656void ExtensionService::OnAllExternalProvidersReady() {
1657 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]94fde232012-04-27 10:22:301658 base::TimeDelta elapsed = base::Time::Now() - profile_->GetStartTime();
1659 UMA_HISTOGRAM_TIMES("Extension.ExternalProvidersReadyAfter", elapsed);
[email protected]50067e52011-10-20 23:17:071660
1661 // Install any pending extensions.
[email protected]94fde232012-04-27 10:22:301662 if (update_once_all_providers_are_ready_ && updater()) {
1663 update_once_all_providers_are_ready_ = false;
[email protected]42774e42012-11-14 21:01:061664 updater()->CheckNow(extensions::ExtensionUpdater::CheckParams());
[email protected]8e4560b62011-01-14 10:09:141665 }
1666
1667 // Uninstall all the unclaimed extensions.
[email protected]45759612012-07-10 17:21:231668 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> extensions_info(
[email protected]8e4560b62011-01-14 10:09:141669 extension_prefs_->GetInstalledExtensionsInfo());
1670 for (size_t i = 0; i < extensions_info->size(); ++i) {
1671 ExtensionInfo* info = extensions_info->at(i).get();
[email protected]1d5e58b2013-01-31 08:41:401672 if (Manifest::IsExternalLocation(info->extension_location))
[email protected]8e4560b62011-01-14 10:09:141673 CheckExternalUninstall(info->extension_id);
1674 }
[email protected]e5af875f2011-10-10 21:09:141675 IdentifyAlertableExtensions();
[email protected]e5af875f2011-10-10 21:09:141676}
1677
1678void ExtensionService::IdentifyAlertableExtensions() {
1679 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1680
[email protected]07c9f2f42012-02-29 18:45:221681 // Build up the lists of extensions that require acknowledgment. If this is
1682 // the first time, grandfather extensions that would have caused
1683 // notification.
[email protected]89226982012-07-16 20:09:181684 extension_error_ui_.reset(ExtensionErrorUI::Create(this));
[email protected]e5af875f2011-10-10 21:09:141685
[email protected]a9aa5932012-01-25 08:27:401686 bool did_show_alert = false;
[email protected]89226982012-07-16 20:09:181687 if (PopulateExtensionErrorUI(extension_error_ui_.get())) {
[email protected]460c6712013-04-24 07:20:011688 if (!is_first_run_) {
[email protected]a9aa5932012-01-25 08:27:401689 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]89226982012-07-16 20:09:181690 did_show_alert = extension_error_ui_->ShowErrorInBubbleView();
[email protected]e5af875f2011-10-10 21:09:141691 } else {
1692 // First run. Just acknowledge all the extensions, silently, by
1693 // shortcutting the display of the UI and going straight to the
1694 // callback for pressing the Accept button.
[email protected]a9aa5932012-01-25 08:27:401695 HandleExtensionAlertAccept();
[email protected]e5af875f2011-10-10 21:09:141696 }
1697 }
[email protected]a9aa5932012-01-25 08:27:401698
[email protected]612a1cb12012-10-17 13:18:031699 UpdateExternalExtensionAlert();
1700
[email protected]a9aa5932012-01-25 08:27:401701 if (!did_show_alert)
[email protected]89226982012-07-16 20:09:181702 extension_error_ui_.reset();
[email protected]e5af875f2011-10-10 21:09:141703}
1704
[email protected]89226982012-07-16 20:09:181705bool ExtensionService::PopulateExtensionErrorUI(
1706 ExtensionErrorUI* extension_error_ui) {
[email protected]07c9f2f42012-02-29 18:45:221707 bool needs_alert = false;
[email protected]695b5712012-12-06 23:55:281708
1709 // Extensions that are blacklisted.
1710 for (ExtensionSet::const_iterator it = blacklisted_extensions_.begin();
1711 it != blacklisted_extensions_.end(); ++it) {
1712 std::string id = (*it)->id();
1713 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(id)) {
1714 extension_error_ui->AddBlacklistedExtension(id);
1715 needs_alert = true;
1716 }
1717 }
1718
[email protected]07c9f2f42012-02-29 18:45:221719 for (ExtensionSet::const_iterator iter = extensions_.begin();
1720 iter != extensions_.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361721 const Extension* e = iter->get();
[email protected]695b5712012-12-06 23:55:281722
1723 // Extensions disabled by policy. Note: this no longer includes blacklisted
1724 // extensions, though we still show the same UI.
[email protected]fdd679b2012-11-15 20:49:391725 if (!system_->management_policy()->UserMayLoad(e, NULL)) {
[email protected]07c9f2f42012-02-29 18:45:221726 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(e->id())) {
[email protected]89226982012-07-16 20:09:181727 extension_error_ui->AddBlacklistedExtension(e->id());
[email protected]07c9f2f42012-02-29 18:45:221728 needs_alert = true;
1729 }
1730 }
[email protected]07c9f2f42012-02-29 18:45:221731 }
[email protected]695b5712012-12-06 23:55:281732
[email protected]07c9f2f42012-02-29 18:45:221733 return needs_alert;
1734}
1735
[email protected]a9aa5932012-01-25 08:27:401736void ExtensionService::HandleExtensionAlertClosed() {
[email protected]612a1cb12012-10-17 13:18:031737 const ExtensionIdSet* extension_ids =
1738 extension_error_ui_->get_blacklisted_extension_ids();
[email protected]e5af875f2011-10-10 21:09:141739 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1740 iter != extension_ids->end(); ++iter) {
1741 extension_prefs_->AcknowledgeBlacklistedExtension(*iter);
1742 }
[email protected]879a6092013-03-06 04:12:501743 extension_error_ui_.reset();
1744}
1745
1746void ExtensionService::HandleExtensionAlertAccept() {
[email protected]73a85e012013-04-04 10:45:301747 extension_error_ui_->Close();
[email protected]e5af875f2011-10-10 21:09:141748}
1749
[email protected]47fc70c2011-12-06 07:29:511750void ExtensionService::AcknowledgeExternalExtension(const std::string& id) {
1751 extension_prefs_->AcknowledgeExternalExtension(id);
[email protected]612a1cb12012-10-17 13:18:031752 UpdateExternalExtensionAlert();
[email protected]47fc70c2011-12-06 07:29:511753}
1754
[email protected]d96eb512012-11-01 23:44:081755bool ExtensionService::IsUnacknowledgedExternalExtension(
1756 const Extension* extension) {
[email protected]b82d18da2012-11-09 00:11:321757 if (!FeatureSwitch::prompt_for_external_extensions()->IsEnabled())
1758 return false;
1759
[email protected]1d5e58b2013-01-31 08:41:401760 return (Manifest::IsExternalLocation(extension->location()) &&
[email protected]d96eb512012-11-01 23:44:081761 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id()) &&
1762 !(extension_prefs_->GetDisableReasons(extension->id()) &
1763 Extension::DISABLE_SIDELOAD_WIPEOUT));
1764}
1765
[email protected]89226982012-07-16 20:09:181766void ExtensionService::HandleExtensionAlertDetails() {
1767 extension_error_ui_->ShowExtensions();
[email protected]73a85e012013-04-04 10:45:301768 // ShowExtensions may cause the error UI to close synchronously, e.g. if it
1769 // causes a navigation.
1770 if (extension_error_ui_)
1771 extension_error_ui_->Close();
[email protected]6c751e72010-11-23 10:11:101772}
1773
[email protected]612a1cb12012-10-17 13:18:031774void ExtensionService::UpdateExternalExtensionAlert() {
[email protected]41070e8d2012-10-24 01:34:361775 if (!FeatureSwitch::prompt_for_external_extensions()->IsEnabled())
[email protected]612a1cb12012-10-17 13:18:031776 return;
1777
1778 const Extension* extension = NULL;
1779 for (ExtensionSet::const_iterator iter = disabled_extensions_.begin();
1780 iter != disabled_extensions_.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361781 const Extension* e = iter->get();
[email protected]d96eb512012-11-01 23:44:081782 if (IsUnacknowledgedExternalExtension(e)) {
1783 extension = e;
1784 break;
[email protected]612a1cb12012-10-17 13:18:031785 }
1786 }
1787
1788 if (extension) {
[email protected]d96eb512012-11-01 23:44:081789 if (!extensions::HasExternalInstallError(this)) {
1790 if (extension_prefs_->IncrementAcknowledgePromptCount(extension->id()) >
[email protected]612a1cb12012-10-17 13:18:031791 kMaxExtensionAcknowledgePromptCount) {
[email protected]d96eb512012-11-01 23:44:081792 // Stop prompting for this extension, and check if there's another
1793 // one that needs prompting.
[email protected]612a1cb12012-10-17 13:18:031794 extension_prefs_->AcknowledgeExternalExtension(extension->id());
[email protected]d96eb512012-11-01 23:44:081795 UpdateExternalExtensionAlert();
1796 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
1797 EXTERNAL_EXTENSION_IGNORED,
1798 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
1799 return;
[email protected]612a1cb12012-10-17 13:18:031800 }
[email protected]b3aa7182013-04-25 04:45:231801 if (is_first_run_)
1802 extension_prefs_->SetExternalInstallFirstRun(extension->id());
1803 // first_run is true if the extension was installed during a first run
1804 // (even if it's post-first run now).
1805 bool first_run = extension_prefs_->IsExternalInstallFirstRun(
1806 extension->id());
1807 extensions::AddExternalInstallError(this, extension, first_run);
[email protected]612a1cb12012-10-17 13:18:031808 }
1809 } else {
1810 extensions::RemoveExternalInstallError(this);
1811 }
[email protected]612a1cb12012-10-17 13:18:031812}
1813
[email protected]a9f39a312010-12-23 22:14:271814void ExtensionService::UnloadExtension(
1815 const std::string& extension_id,
[email protected]814a7bf0f2011-08-13 05:30:591816 extension_misc::UnloadedExtensionReason reason) {
[email protected]27e469a2010-01-11 20:35:091817 // Make sure the extension gets deleted after we return from this function.
[email protected]695b5712012-12-06 23:55:281818 int include_mask = INCLUDE_EVERYTHING & ~INCLUDE_TERMINATED;
[email protected]9adb9692010-10-29 23:14:021819 scoped_refptr<const Extension> extension(
[email protected]695b5712012-12-06 23:55:281820 GetExtensionById(extension_id, include_mask));
[email protected]631cf822009-05-15 07:01:251821
[email protected]fa0c96732010-11-17 00:14:231822 // This method can be called via PostTask, so the extension may have been
1823 // unloaded by the time this runs.
[email protected]dc24976f2013-06-02 21:15:091824 if (!extension.get()) {
[email protected]dd163fb02011-05-04 22:22:171825 // In case the extension may have crashed/uninstalled. Allow the profile to
1826 // clean up its RequestContexts.
[email protected]31d8f5f22012-04-02 15:22:081827 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]fa0c96732010-11-17 00:14:231828 return;
[email protected]dd163fb02011-05-04 22:22:171829 }
[email protected]0c6da502009-08-14 22:32:391830
[email protected]20c8b942013-05-26 02:03:401831 // If uninstalling let RuntimeEventRouter know.
1832 if (reason == extension_misc::UNLOAD_REASON_UNINSTALL)
1833 extensions::RuntimeEventRouter::OnExtensionUninstalled(
1834 profile_, extension_id);
1835
[email protected]1eb175082010-02-10 09:26:161836 // Keep information about the extension so that we can reload it later
1837 // even if it's not permanently installed.
1838 unloaded_extension_paths_[extension->id()] = extension->path();
1839
[email protected]f17dbd42010-08-16 23:21:101840 // Clean up if the extension is meant to be enabled after a reload.
[email protected]b914e2952013-04-26 07:10:031841 reloading_extensions_.erase(extension->id());
[email protected]f17dbd42010-08-16 23:21:101842
[email protected]d7e9a862010-11-03 21:57:491843 // Clean up runtime data.
1844 extension_runtime_data_.erase(extension_id);
1845
[email protected]060d4972012-07-19 17:22:391846 if (disabled_extensions_.Contains(extension->id())) {
[email protected]dc24976f2013-06-02 21:15:091847 UnloadedExtensionInfo details(extension.get(), reason);
[email protected]a9f39a312010-12-23 22:14:271848 details.already_disabled = true;
[email protected]84df8332011-12-06 18:22:461849 disabled_extensions_.Remove(extension->id());
[email protected]ad50def52011-10-19 23:17:071850 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271851 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]6c2381d2011-10-19 02:52:531852 content::Source<Profile>(profile_),
1853 content::Details<UnloadedExtensionInfo>(&details));
[email protected]dd163fb02011-05-04 22:22:171854 // Make sure the profile cleans up its RequestContexts when an already
1855 // disabled extension is unloaded (since they are also tracking the disabled
1856 // extensions).
[email protected]31d8f5f22012-04-02 15:22:081857 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]0c6da502009-08-14 22:32:391858 return;
1859 }
1860
[email protected]060d4972012-07-19 17:22:391861 // Remove the extension from our list.
[email protected]84df8332011-12-06 18:22:461862 extensions_.Remove(extension->id());
[email protected]631cf822009-05-15 07:01:251863
[email protected]a9f39a312010-12-23 22:14:271864 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]631cf822009-05-15 07:01:251865}
1866
[email protected]eaa7dd182010-12-14 11:09:001867void ExtensionService::UnloadAllExtensions() {
[email protected]31bb5ee62012-09-12 22:58:401868 profile_->GetExtensionSpecialStoragePolicy()->RevokeRightsForAllExtensions();
[email protected]c8d407e2011-04-28 21:27:171869
[email protected]84df8332011-12-06 18:22:461870 extensions_.Clear();
1871 disabled_extensions_.Clear();
1872 terminated_extensions_.Clear();
[email protected]d7e9a862010-11-03 21:57:491873 extension_runtime_data_.clear();
[email protected]cd500f72010-06-25 23:44:321874
[email protected]c6e4a3412009-06-24 15:45:291875 // TODO(erikkay) should there be a notification for this? We can't use
1876 // EXTENSION_UNLOADED since that implies that the extension has been disabled
1877 // or uninstalled, and UnloadAll is just part of shutdown.
[email protected]9f1087e2009-06-15 17:29:321878}
1879
[email protected]eaa7dd182010-12-14 11:09:001880void ExtensionService::ReloadExtensions() {
[email protected]9f1087e2009-06-15 17:29:321881 UnloadAllExtensions();
[email protected]eac88332012-12-26 17:57:451882 component_loader_->LoadAll();
[email protected]d8c8f25f2011-11-02 18:18:011883 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]4a10006a2013-05-17 23:18:351884 // Don't call SetReadyAndNotifyListeners() since tests call this multiple
1885 // times.
[email protected]9f1087e2009-06-15 17:29:321886}
1887
[email protected]eaa7dd182010-12-14 11:09:001888void ExtensionService::GarbageCollectExtensions() {
[email protected]2f8757c32012-06-19 19:17:471889 if (extension_prefs_->pref_service()->ReadOnly())
1890 return;
1891
[email protected]9bd9a6862012-11-29 09:24:221892 if (pending_extension_manager()->HasPendingExtensions()) {
1893 // Don't garbage collect while there are pending installations, which may
1894 // be using the temporary installation directory. Try to garbage collect
1895 // again later.
[email protected]b3a25092013-05-28 22:08:161896 base::MessageLoop::current()->PostDelayedTask(
[email protected]9bd9a6862012-11-29 09:24:221897 FROM_HERE,
1898 base::Bind(&ExtensionService::GarbageCollectExtensions, AsWeakPtr()),
1899 base::TimeDelta::FromSeconds(kGarbageCollectRetryDelay));
1900 return;
1901 }
1902
[email protected]45759612012-07-10 17:21:231903 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> info(
[email protected]2f8757c32012-06-19 19:17:471904 extension_prefs_->GetInstalledExtensionsInfo());
1905
[email protected]650b2d52013-02-10 03:41:451906 std::multimap<std::string, base::FilePath> extension_paths;
[email protected]2f8757c32012-06-19 19:17:471907 for (size_t i = 0; i < info->size(); ++i)
[email protected]0db124b02012-11-07 04:55:051908 extension_paths.insert(std::make_pair(info->at(i)->extension_id,
1909 info->at(i)->extension_path));
1910
[email protected]399583b2012-12-11 09:33:421911 info = extension_prefs_->GetAllDelayedInstallInfo();
[email protected]0db124b02012-11-07 04:55:051912 for (size_t i = 0; i < info->size(); ++i)
1913 extension_paths.insert(std::make_pair(info->at(i)->extension_id,
1914 info->at(i)->extension_path));
[email protected]2f8757c32012-06-19 19:17:471915
[email protected]7f8f24f2012-11-15 19:40:141916 if (!GetFileTaskRunner()->PostTask(
1917 FROM_HERE,
[email protected]2f8757c32012-06-19 19:17:471918 base::Bind(
1919 &extension_file_util::GarbageCollectExtensions,
1920 install_directory_,
[email protected]31bb5ee62012-09-12 22:58:401921 extension_paths))) {
[email protected]2f8757c32012-06-19 19:17:471922 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:401923 }
[email protected]f6ccd652010-11-17 00:48:341924
[email protected]f766a9d2012-07-11 01:57:441925#if defined(ENABLE_THEMES)
[email protected]f6ccd652010-11-17 00:48:341926 // Also garbage-collect themes. We check |profile_| to be
1927 // defensive; in the future, we may call GarbageCollectExtensions()
1928 // from somewhere other than Init() (e.g., in a timer).
1929 if (profile_) {
[email protected]d5949312012-12-03 22:13:301930 ThemeServiceFactory::GetForProfile(profile_)->RemoveUnusedThemes();
[email protected]f6ccd652010-11-17 00:48:341931 }
[email protected]f766a9d2012-07-11 01:57:441932#endif
[email protected]3cf4f0992009-02-03 23:00:301933}
1934
[email protected]fb82dcd2012-03-21 14:15:461935void ExtensionService::SyncExtensionChangeIfNeeded(const Extension& extension) {
[email protected]5db9ada2012-04-11 13:48:201936 if (app_sync_bundle_.HandlesApp(extension)) {
1937 app_sync_bundle_.SyncChangeIfNeeded(extension);
1938 } else if (extension_sync_bundle_.HandlesExtension(extension)) {
1939 extension_sync_bundle_.SyncChangeIfNeeded(extension);
[email protected]fb82dcd2012-03-21 14:15:461940 }
1941}
1942
[email protected]820d9bd2013-04-03 03:46:031943void ExtensionService::SetReadyAndNotifyListeners() {
[email protected]4a10006a2013-05-17 23:18:351944 ready_->Signal();
[email protected]820d9bd2013-04-03 03:46:031945 content::NotificationService::current()->Notify(
1946 chrome::NOTIFICATION_EXTENSIONS_READY,
1947 content::Source<Profile>(profile_),
1948 content::NotificationService::NoDetails());
1949}
1950
[email protected]eaa7dd182010-12-14 11:09:001951void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]3eeddd892013-04-17 17:00:111952 if (updater_)
[email protected]93fd78f42009-07-10 16:43:171953 updater_->Start();
[email protected]fa6a9102010-11-22 15:38:501954
[email protected]bc151cf92013-02-12 04:57:261955 OnBlacklistUpdated();
[email protected]e72e8eb82009-06-18 17:21:511956}
1957
[email protected]fcb58a862012-05-01 01:03:151958void ExtensionService::AddExtension(const Extension* extension) {
[email protected]c8d407e2011-04-28 21:27:171959 // TODO(jstritar): We may be able to get rid of this branch by overriding the
1960 // default extension state to DISABLED when the --disable-extensions flag
1961 // is set (http://crbug.com/29067).
1962 if (!extensions_enabled() &&
1963 !extension->is_theme() &&
[email protected]1d5e58b2013-01-31 08:41:401964 extension->location() != Manifest::COMPONENT &&
1965 !Manifest::IsExternalLocation(extension->location())) {
[email protected]fcb58a862012-05-01 01:03:151966 return;
[email protected]330840c2012-08-29 22:21:011967 }
[email protected]c8d407e2011-04-28 21:27:171968
[email protected]b914e2952013-04-26 07:10:031969 bool is_extension_upgrade = false;
1970 if (const Extension* old = GetInstalledExtension(extension->id())) {
1971 is_extension_upgrade = true;
1972 DCHECK_NE(extension, old);
1973 // Other than for unpacked extensions, CrxInstaller should have guaranteed
1974 // that we aren't downgrading.
1975 if (!Manifest::IsUnpackedLocation(extension->location()))
1976 CHECK_GE(extension->version()->CompareTo(*(old->version())), 0);
1977 }
1978 SetBeingUpgraded(extension, is_extension_upgrade);
[email protected]c8d407e2011-04-28 21:27:171979
[email protected]1eb175082010-02-10 09:26:161980 // The extension is now loaded, remove its data from unloaded extension map.
1981 unloaded_extension_paths_.erase(extension->id());
1982
[email protected]bb7f40952011-01-13 00:21:201983 // If a terminated extension is loaded, remove it from the terminated list.
1984 UntrackTerminatedExtension(extension->id());
1985
[email protected]f17dbd42010-08-16 23:21:101986 // If the extension was disabled for a reload, then enable it.
[email protected]ca2e82022013-04-29 16:48:411987 bool reloading = reloading_extensions_.erase(extension->id()) > 0;
[email protected]f17dbd42010-08-16 23:21:101988
[email protected]b914e2952013-04-26 07:10:031989 // Check if the extension's privileges have changed and mark the
1990 // extension disabled if necessary.
1991 CheckPermissionsIncrease(extension, is_extension_upgrade);
1992
[email protected]ca2e82022013-04-29 16:48:411993 if (is_extension_upgrade && !reloading) {
1994 // To upgrade an extension in place, unload the old one and then load the
1995 // new one. ReloadExtension disables the extension, which is sufficient.
[email protected]b914e2952013-04-26 07:10:031996 UnloadExtension(extension->id(), extension_misc::UNLOAD_REASON_UPDATE);
1997 }
[email protected]0c6da502009-08-14 22:32:391998
[email protected]695b5712012-12-06 23:55:281999 if (extension_prefs_->IsExtensionBlacklisted(extension->id())) {
[email protected]ac875372013-02-28 04:36:092000 // Only prefs is checked for the blacklist. We rely on callers to check the
2001 // blacklist before calling into here, e.g. CrxInstaller checks before
2002 // installation, we check when loading installed extensions.
[email protected]695b5712012-12-06 23:55:282003 blacklisted_extensions_.Insert(extension);
[email protected]ca2e82022013-04-29 16:48:412004 } else if (!reloading &&
2005 extension_prefs_->IsExtensionDisabled(extension->id())) {
[email protected]6f6101832012-11-27 22:10:482006 disabled_extensions_.Insert(extension);
[email protected]3bdba0d2011-08-23 07:17:302007 SyncExtensionChangeIfNeeded(*extension);
[email protected]44d62b62012-04-11 00:06:032008 content::NotificationService::current()->Notify(
2009 chrome::NOTIFICATION_EXTENSION_UPDATE_DISABLED,
2010 content::Source<Profile>(profile_),
2011 content::Details<const Extension>(extension));
[email protected]62f051c2012-03-29 17:04:442012
[email protected]7e9e2422012-12-13 19:54:192013 // Show the extension disabled error if a permissions increase was the
2014 // only reason it was disabled.
2015 if (extension_prefs_->GetDisableReasons(extension->id()) ==
[email protected]44d62b62012-04-11 00:06:032016 Extension::DISABLE_PERMISSIONS_INCREASE) {
[email protected]62f051c2012-03-29 17:04:442017 extensions::AddExtensionDisabledError(this, extension);
2018 }
[email protected]ca2e82022013-04-29 16:48:412019 } else if (reloading) {
2020 // Replace the old extension with the new version.
2021 CHECK(!disabled_extensions_.Insert(extension));
2022 EnableExtension(extension->id());
[email protected]695b5712012-12-06 23:55:282023 } else {
2024 // All apps that are displayed in the launcher are ordered by their ordinals
2025 // so we must ensure they have valid ordinals.
2026 if (extension->RequiresSortOrdinal()) {
[email protected]bd36b652013-02-27 02:13:252027 if (!extension->ShouldDisplayInNewTabPage()) {
2028 extension_prefs_->extension_sorting()->MarkExtensionAsHidden(
2029 extension->id());
2030 }
[email protected]695b5712012-12-06 23:55:282031 extension_prefs_->extension_sorting()->EnsureValidOrdinals(
2032 extension->id(), syncer::StringOrdinal());
2033 }
2034
2035 extensions_.Insert(extension);
2036 SyncExtensionChangeIfNeeded(*extension);
2037 NotifyExtensionLoaded(extension);
[email protected]f574c402012-12-04 23:20:312038 }
[email protected]b914e2952013-04-26 07:10:032039 SetBeingUpgraded(extension, false);
[email protected]aab98a52009-12-02 03:22:352040}
2041
[email protected]8c484b742012-11-29 06:05:362042void ExtensionService::AddComponentExtension(const Extension* extension) {
2043 const std::string old_version_string(
2044 extension_prefs_->GetVersionString(extension->id()));
2045 const Version old_version(old_version_string);
2046
2047 if (!old_version.IsValid() || !old_version.Equals(*extension->version())) {
2048 VLOG(1) << "Component extension " << extension->name() << " ("
2049 << extension->id() << ") installing/upgrading from '"
2050 << old_version_string << "' to " << extension->version()->GetString();
2051
2052 AddNewOrUpdatedExtension(extension,
[email protected]399583b2012-12-11 09:33:422053 Extension::ENABLED_COMPONENT,
2054 syncer::StringOrdinal());
[email protected]8c484b742012-11-29 06:05:362055 return;
2056 }
2057
2058 AddExtension(extension);
2059}
2060
[email protected]b914e2952013-04-26 07:10:032061void ExtensionService::UpdateActivePermissions(const Extension* extension) {
[email protected]902fd7b2011-07-27 18:42:312062 // If the extension has used the optional permissions API, it will have a
2063 // custom set of active permissions defined in the extension prefs. Here,
2064 // we update the extension's active permissions based on the prefs.
[email protected]c2e66e12012-06-27 06:27:062065 scoped_refptr<PermissionSet> active_permissions =
[email protected]902fd7b2011-07-27 18:42:312066 extension_prefs()->GetActivePermissions(extension->id());
2067
[email protected]dc24976f2013-06-02 21:15:092068 if (active_permissions.get()) {
[email protected]902fd7b2011-07-27 18:42:312069 // We restrict the active permissions to be within the bounds defined in the
2070 // extension's manifest.
2071 // a) active permissions must be a subset of optional + default permissions
2072 // b) active permissions must contains all default permissions
[email protected]c2e66e12012-06-27 06:27:062073 scoped_refptr<PermissionSet> total_permissions =
2074 PermissionSet::CreateUnion(
[email protected]2cb5e302013-05-09 05:00:062075 extensions::PermissionsData::GetRequiredPermissions(extension),
2076 extensions::PermissionsData::GetOptionalPermissions(extension));
[email protected]902fd7b2011-07-27 18:42:312077
2078 // Make sure the active permissions contain no more than optional + default.
[email protected]c2e66e12012-06-27 06:27:062079 scoped_refptr<PermissionSet> adjusted_active =
2080 PermissionSet::CreateIntersection(
[email protected]902fd7b2011-07-27 18:42:312081 total_permissions.get(), active_permissions.get());
2082
2083 // Make sure the active permissions contain the default permissions.
[email protected]c2e66e12012-06-27 06:27:062084 adjusted_active = PermissionSet::CreateUnion(
[email protected]2cb5e302013-05-09 05:00:062085 extensions::PermissionsData::GetRequiredPermissions(extension),
2086 adjusted_active.get());
[email protected]902fd7b2011-07-27 18:42:312087
[email protected]c333e792012-01-06 16:57:392088 extensions::PermissionsUpdater perms_updater(profile());
[email protected]dc24976f2013-06-02 21:15:092089 perms_updater.UpdateActivePermissions(extension, adjusted_active.get());
[email protected]902fd7b2011-07-27 18:42:312090 }
[email protected]b914e2952013-04-26 07:10:032091}
2092
2093void ExtensionService::CheckPermissionsIncrease(const Extension* extension,
2094 bool is_extension_upgrade) {
2095 UpdateActivePermissions(extension);
[email protected]902fd7b2011-07-27 18:42:312096
[email protected]8d888c12010-11-30 00:00:252097 // We keep track of all permissions the user has granted each extension.
2098 // This allows extensions to gracefully support backwards compatibility
2099 // by including unknown permissions in their manifests. When the user
2100 // installs the extension, only the recognized permissions are recorded.
2101 // When the unknown permissions become recognized (e.g., through browser
2102 // upgrade), we can prompt the user to accept these new permissions.
2103 // Extensions can also silently upgrade to less permissions, and then
2104 // silently upgrade to a version that adds these permissions back.
2105 //
2106 // For example, pretend that Chrome 10 includes a permission "omnibox"
2107 // for an API that adds suggestions to the omnibox. An extension can
2108 // maintain backwards compatibility while still having "omnibox" in the
2109 // manifest. If a user installs the extension on Chrome 9, the browser
2110 // will record the permissions it recognized, not including "omnibox."
2111 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
2112 // will disable the extension and prompt the user to approve the increase
2113 // in privileges. The extension could then release a new version that
2114 // removes the "omnibox" permission. When the user upgrades, Chrome will
2115 // still remember that "omnibox" had been granted, so that if the
2116 // extension once again includes "omnibox" in an upgrade, the extension
2117 // can upgrade without requiring this user's approval.
[email protected]eb5e4f92012-08-15 23:33:282118 int disable_reasons = extension_prefs_->GetDisableReasons(extension->id());
[email protected]8d888c12010-11-30 00:00:252119
[email protected]211a3f32013-05-28 21:48:182120 bool auto_grant_permission =
2121 (!is_extension_upgrade && extension->was_installed_by_default()) ||
2122 chrome::IsRunningInForcedAppMode();
[email protected]b914e2952013-04-26 07:10:032123 // Silently grant all active permissions to default apps only on install.
2124 // After install they should behave like other apps.
[email protected]211a3f32013-05-28 21:48:182125 // Silently grant all active permissions to apps install in kiosk mode on both
2126 // install and update.
2127 if (auto_grant_permission)
[email protected]b914e2952013-04-26 07:10:032128 GrantPermissions(extension);
2129
2130 bool is_privilege_increase = false;
2131 // We only need to compare the granted permissions to the current permissions
2132 // if the extension is not allowed to silently increase its permissions.
[email protected]13c68b62013-05-17 11:29:052133 if (!extensions::PermissionsData::CanSilentlyIncreasePermissions(extension) &&
[email protected]211a3f32013-05-28 21:48:182134 !auto_grant_permission) {
[email protected]8d888c12010-11-30 00:00:252135 // Add all the recognized permissions if the granted permissions list
2136 // hasn't been initialized yet.
[email protected]c2e66e12012-06-27 06:27:062137 scoped_refptr<PermissionSet> granted_permissions =
[email protected]902fd7b2011-07-27 18:42:312138 extension_prefs_->GetGrantedPermissions(extension->id());
[email protected]0d3e4a22011-06-23 19:02:522139 CHECK(granted_permissions.get());
[email protected]8d888c12010-11-30 00:00:252140
2141 // Here, we check if an extension's privileges have increased in a manner
2142 // that requires the user's approval. This could occur because the browser
2143 // upgraded and recognized additional privileges, or an extension upgrades
2144 // to a version that requires additional privileges.
[email protected]cadac622013-06-11 16:46:362145 is_privilege_increase = granted_permissions->HasLessPrivilegesThan(
2146 extension->GetActivePermissions().get());
[email protected]8d888c12010-11-30 00:00:252147 }
2148
2149 if (is_extension_upgrade) {
[email protected]44d62b62012-04-11 00:06:032150 // If the extension was already disabled, suppress any alerts for becoming
2151 // disabled on permissions increase.
[email protected]b914e2952013-04-26 07:10:032152 bool previously_disabled =
2153 extension_prefs_->IsExtensionDisabled(extension->id());
[email protected]7e9e2422012-12-13 19:54:192154 // Legacy disabled extensions do not have a disable reason. Infer that if
2155 // there was no permission increase, it was likely disabled by the user.
2156 if (previously_disabled && disable_reasons == Extension::DISABLE_NONE &&
[email protected]b914e2952013-04-26 07:10:032157 !extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
[email protected]7e9e2422012-12-13 19:54:192158 disable_reasons |= Extension::DISABLE_USER_ACTION;
[email protected]44d62b62012-04-11 00:06:032159 }
[email protected]7e9e2422012-12-13 19:54:192160 // Extensions that came to us disabled from sync need a similar inference,
2161 // except based on the new version's permissions.
2162 if (previously_disabled &&
2163 disable_reasons == Extension::DISABLE_UNKNOWN_FROM_SYNC) {
2164 // Remove the DISABLE_UNKNOWN_FROM_SYNC reason.
2165 extension_prefs_->ClearDisableReasons(extension->id());
2166 if (!is_privilege_increase)
2167 disable_reasons |= Extension::DISABLE_USER_ACTION;
2168 }
[email protected]b914e2952013-04-26 07:10:032169 disable_reasons &= ~Extension::DISABLE_UNKNOWN_FROM_SYNC;
[email protected]8d888c12010-11-30 00:00:252170 }
2171
2172 // Extension has changed permissions significantly. Disable it. A
2173 // notification should be sent by the caller.
2174 if (is_privilege_increase) {
[email protected]7e9e2422012-12-13 19:54:192175 disable_reasons |= Extension::DISABLE_PERMISSIONS_INCREASE;
[email protected]fe2dd7742011-04-19 22:52:492176 if (!extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
2177 RecordPermissionMessagesHistogram(
2178 extension, "Extensions.Permissions_AutoDisable");
2179 }
[email protected]b3317ad2011-04-28 23:46:002180 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
[email protected]8d888c12010-11-30 00:00:252181 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
[email protected]b914e2952013-04-26 07:10:032182 }
2183 if (disable_reasons != Extension::DISABLE_NONE) {
[email protected]eb5e4f92012-08-15 23:33:282184 extension_prefs_->AddDisableReason(
2185 extension->id(),
2186 static_cast<Extension::DisableReason>(disable_reasons));
[email protected]8d888c12010-11-30 00:00:252187 }
2188}
2189
[email protected]eaa7dd182010-12-14 11:09:002190void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:392191 std::set<std::string> extension_ids;
[email protected]84df8332011-12-06 18:22:462192 for (ExtensionSet::const_iterator iter = extensions_.begin();
2193 iter != extensions_.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:362194 const Extension* extension = iter->get();
[email protected]1d5e58b2013-01-31 08:41:402195 if (!extension->is_theme() && extension->location() != Manifest::COMPONENT)
[email protected]84df8332011-12-06 18:22:462196 extension_ids.insert(extension->id());
[email protected]aab98a52009-12-02 03:22:352197 }
2198
2199 child_process_logging::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:252200}
2201
[email protected]8266d662011-07-12 21:53:262202void ExtensionService::OnExtensionInstalled(
[email protected]36a5c4c2011-12-14 16:34:502203 const Extension* extension,
[email protected]98270432012-09-11 20:51:242204 const syncer::StringOrdinal& page_ordinal,
[email protected]0db124b02012-11-07 04:55:052205 bool has_requirement_errors,
2206 bool wait_for_idle) {
[email protected]a29a517a2011-01-21 21:11:122207 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:202208
[email protected]c3cfb012011-04-06 22:07:352209 const std::string& id = extension->id();
[email protected]612a1cb12012-10-17 13:18:032210 bool initial_enable = ShouldEnableOnInstall(extension);
[email protected]3f213ad2012-07-26 23:39:412211 const extensions::PendingExtensionInfo* pending_extension_info = NULL;
[email protected]51a3bf8b2012-06-08 22:53:062212 if ((pending_extension_info = pending_extension_manager()->GetById(id))) {
[email protected]8f3bcbd2013-06-05 08:42:402213 if (!pending_extension_info->ShouldAllowInstall(extension)) {
[email protected]51a3bf8b2012-06-08 22:53:062214 pending_extension_manager()->Remove(id);
[email protected]8ef78fd2010-08-19 17:14:322215
[email protected]31bb5ee62012-09-12 22:58:402216 LOG(WARNING) << "ShouldAllowInstall() returned false for "
2217 << id << " of type " << extension->GetType()
[email protected]65348062013-01-15 07:27:222218 << " and update URL "
2219 << extensions::ManifestURL::GetUpdateURL(extension).spec()
[email protected]31bb5ee62012-09-12 22:58:402220 << "; not installing";
[email protected]ae6cb3022011-04-04 20:11:542221
[email protected]ad50def52011-10-19 23:17:072222 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272223 chrome::NOTIFICATION_EXTENSION_INSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:532224 content::Source<Profile>(profile_),
2225 content::Details<const Extension>(extension));
[email protected]ae6cb3022011-04-04 20:11:542226
[email protected]4416c5a2010-06-26 01:28:572227 // Delete the extension directory since we're not going to
2228 // load it.
[email protected]7f8f24f2012-11-15 19:40:142229 if (!GetFileTaskRunner()->PostTask(
2230 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362231 base::Bind(&extension_file_util::DeleteFile,
[email protected]31bb5ee62012-09-12 22:58:402232 extension->path(), true))) {
[email protected]14908b72011-04-20 06:54:362233 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:402234 }
[email protected]4416c5a2010-06-26 01:28:572235 return;
2236 }
[email protected]51a3bf8b2012-06-08 22:53:062237
2238 pending_extension_manager()->Remove(id);
[email protected]4416c5a2010-06-26 01:28:572239 } else {
[email protected]c3cfb012011-04-06 22:07:352240 // We explicitly want to re-enable an uninstalled external
2241 // extension; if we're here, that means the user is manually
2242 // installing the extension.
[email protected]6cc7dbae2011-04-29 21:18:332243 if (IsExternalExtensionUninstalled(id)) {
2244 initial_enable = true;
2245 }
[email protected]aa142702010-03-26 01:26:332246 }
2247
[email protected]98270432012-09-11 20:51:242248 // Unsupported requirements overrides the management policy.
2249 if (has_requirement_errors) {
2250 initial_enable = false;
2251 extension_prefs_->AddDisableReason(
2252 id, Extension::DISABLE_UNSUPPORTED_REQUIREMENT);
2253 // If the extension was disabled because of unsupported requirements but
2254 // now supports all requirements after an update and there are not other
2255 // disable reasons, enable it.
2256 } else if (extension_prefs_->GetDisableReasons(id) ==
2257 Extension::DISABLE_UNSUPPORTED_REQUIREMENT) {
2258 initial_enable = true;
2259 extension_prefs_->ClearDisableReasons(id);
2260 }
2261
[email protected]695b5712012-12-06 23:55:282262 if (!GetInstalledExtension(extension->id())) {
[email protected]07533022011-06-27 20:42:552263 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
2264 extension->GetType(), 100);
[email protected]69084dc2012-11-17 07:39:302265 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallSource",
[email protected]1d5e58b2013-01-31 08:41:402266 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]07533022011-06-27 20:42:552267 RecordPermissionMessagesHistogram(
2268 extension, "Extensions.Permissions_Install");
[email protected]69084dc2012-11-17 07:39:302269 } else {
2270 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateType",
2271 extension->GetType(), 100);
2272 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateSource",
[email protected]1d5e58b2013-01-31 08:41:402273 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]07533022011-06-27 20:42:552274 }
2275
[email protected]a39921b42012-02-28 03:42:542276 // Certain extension locations are specific enough that we can
2277 // auto-acknowledge any extension that came from one of them.
[email protected]1d5e58b2013-01-31 08:41:402278 if (extension->location() == Manifest::EXTERNAL_POLICY_DOWNLOAD)
[email protected]a39921b42012-02-28 03:42:542279 AcknowledgeExternalExtension(extension->id());
[email protected]399583b2012-12-11 09:33:422280 const Extension::State initial_state =
2281 initial_enable ? Extension::ENABLED : Extension::DISABLED;
[email protected]e7aa7b7e2012-11-27 04:51:222282 if (ShouldDelayExtensionUpdate(id, wait_for_idle)) {
[email protected]399583b2012-12-11 09:33:422283 extension_prefs_->SetDelayedInstallInfo(extension, initial_state,
2284 page_ordinal);
[email protected]0db124b02012-11-07 04:55:052285
2286 // Transfer ownership of |extension|.
[email protected]399583b2012-12-11 09:33:422287 delayed_updates_for_idle_.Insert(extension);
[email protected]0db124b02012-11-07 04:55:052288
2289 // Notify extension of available update.
2290 extensions::RuntimeEventRouter::DispatchOnUpdateAvailableEvent(
2291 profile_, id, extension->manifest()->value());
[email protected]75bdcb872013-03-13 00:41:452292
2293 // Notify observers that app update is available.
2294 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
2295 OnAppUpdateAvailable(extension->id()));
[email protected]0db124b02012-11-07 04:55:052296 return;
2297 }
2298
[email protected]399583b2012-12-11 09:33:422299 if (installs_delayed()) {
2300 extension_prefs_->SetDelayedInstallInfo(extension, initial_state,
2301 page_ordinal);
2302 delayed_installs_.Insert(extension);
2303 } else {
2304 AddNewOrUpdatedExtension(extension, initial_state, page_ordinal);
2305 }
[email protected]8c484b742012-11-29 06:05:362306}
2307
2308void ExtensionService::AddNewOrUpdatedExtension(
2309 const Extension* extension,
[email protected]399583b2012-12-11 09:33:422310 Extension::State initial_state,
2311 const syncer::StringOrdinal& page_ordinal) {
[email protected]8c484b742012-11-29 06:05:362312 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2313
[email protected]4416c5a2010-06-26 01:28:572314 extension_prefs_->OnExtensionInstalled(
[email protected]8266d662011-07-12 21:53:262315 extension,
[email protected]8c484b742012-11-29 06:05:362316 initial_state,
[email protected]36a5c4c2011-12-14 16:34:502317 page_ordinal);
[email protected]25b34332009-06-05 21:53:192318
[email protected]6f6101832012-11-27 22:10:482319 FinishInstallation(extension);
2320}
2321
2322void ExtensionService::MaybeFinishDelayedInstallation(
2323 const std::string& extension_id) {
2324 // Check if the extension already got updated.
[email protected]399583b2012-12-11 09:33:422325 if (!delayed_updates_for_idle_.Contains(extension_id))
[email protected]6f6101832012-11-27 22:10:482326 return;
2327 // Check if the extension is idle.
2328 if (!IsExtensionIdle(extension_id))
2329 return;
2330
2331 FinishDelayedInstallation(extension_id);
2332}
2333
2334void ExtensionService::FinishDelayedInstallation(
2335 const std::string& extension_id) {
2336 scoped_refptr<const Extension> extension(
2337 GetPendingExtensionUpdate(extension_id));
[email protected]dc24976f2013-06-02 21:15:092338 CHECK(extension.get());
[email protected]399583b2012-12-11 09:33:422339 delayed_updates_for_idle_.Remove(extension_id);
[email protected]6f6101832012-11-27 22:10:482340
[email protected]399583b2012-12-11 09:33:422341 if (!extension_prefs_->FinishDelayedInstallInfo(extension_id))
[email protected]6f6101832012-11-27 22:10:482342 NOTREACHED();
2343
[email protected]dc24976f2013-06-02 21:15:092344 FinishInstallation(extension.get());
[email protected]6f6101832012-11-27 22:10:482345}
2346
2347void ExtensionService::FinishInstallation(const Extension* extension) {
[email protected]e7554c3f2013-05-29 00:36:562348 const extensions::Extension* existing_extension =
2349 GetInstalledExtension(extension->id());
2350 bool is_update = false;
2351 std::string old_name;
2352 if (existing_extension) {
2353 is_update = true;
2354 old_name = existing_extension->name();
2355 }
2356 extensions::InstalledExtensionInfo details(extension, is_update, old_name);
[email protected]fcb58a862012-05-01 01:03:152357 content::NotificationService::current()->Notify(
2358 chrome::NOTIFICATION_EXTENSION_INSTALLED,
2359 content::Source<Profile>(profile_),
[email protected]41bb80bd2013-05-03 10:56:022360 content::Details<const extensions::InstalledExtensionInfo>(&details));
[email protected]fcb58a862012-05-01 01:03:152361
[email protected]d96eb512012-11-01 23:44:082362 bool unacknowledged_external = IsUnacknowledgedExternalExtension(extension);
[email protected]41070e8d2012-10-24 01:34:362363
[email protected]399583b2012-12-11 09:33:422364 // Unpacked extensions default to allowing file access, but if that has been
2365 // overridden, don't reset the value.
[email protected]1d5e58b2013-01-31 08:41:402366 if (Manifest::ShouldAlwaysAllowFileAccess(extension->location()) &&
[email protected]399583b2012-12-11 09:33:422367 !extension_prefs_->HasAllowFileAccessSetting(extension->id())) {
2368 extension_prefs_->SetAllowFileAccess(extension->id(), true);
2369 }
2370
[email protected]6f6101832012-11-27 22:10:482371 AddExtension(extension);
[email protected]612a1cb12012-10-17 13:18:032372
[email protected]a51c9e92012-12-09 09:19:202373#if defined(ENABLE_THEMES)
[email protected]bc151cf92013-02-12 04:57:262374 // We do this here since AddExtension() is always called on browser startup,
2375 // and we only really care about the last theme installed.
[email protected]a51c9e92012-12-09 09:19:202376 // If that ever changes and we have to move this code somewhere
2377 // else, it should be somewhere that's not in the startup path.
2378 if (extension->is_theme() && extensions_.GetByID(extension->id())) {
2379 DCHECK_EQ(extensions_.GetByID(extension->id()), extension);
2380 // Now that the theme extension is visible from outside the
2381 // ExtensionService, notify the ThemeService about the
2382 // newly-installed theme.
2383 ThemeServiceFactory::GetForProfile(profile_)->SetTheme(extension);
2384 }
2385#endif
2386
[email protected]612a1cb12012-10-17 13:18:032387 // If this is a new external extension that was disabled, alert the user
[email protected]d96eb512012-11-01 23:44:082388 // so he can reenable it. We do this last so that it has already been
2389 // added to our list of extensions.
2390 if (unacknowledged_external) {
[email protected]612a1cb12012-10-17 13:18:032391 UpdateExternalExtensionAlert();
[email protected]d96eb512012-11-01 23:44:082392 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
2393 EXTERNAL_EXTENSION_INSTALLED,
2394 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
2395 }
[email protected]4a190632009-05-09 01:07:422396}
2397
[email protected]76b65442012-11-17 14:11:482398const Extension* ExtensionService::GetPendingExtensionUpdate(
2399 const std::string& id) const {
[email protected]399583b2012-12-11 09:33:422400 return delayed_updates_for_idle_.GetByID(id);
[email protected]76b65442012-11-17 14:11:482401}
2402
[email protected]bb7f40952011-01-13 00:21:202403void ExtensionService::TrackTerminatedExtension(const Extension* extension) {
[email protected]84df8332011-12-06 18:22:462404 if (!terminated_extensions_.Contains(extension->id()))
2405 terminated_extensions_.Insert(make_scoped_refptr(extension));
[email protected]fa2416f2011-05-03 08:41:202406
[email protected]b3f7fe22011-11-11 19:27:562407 UnloadExtension(extension->id(), extension_misc::UNLOAD_REASON_TERMINATE);
[email protected]bb7f40952011-01-13 00:21:202408}
2409
2410void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
[email protected]fa2416f2011-05-03 08:41:202411 std::string lowercase_id = StringToLowerASCII(id);
[email protected]84df8332011-12-06 18:22:462412 terminated_extensions_.Remove(lowercase_id);
[email protected]bb7f40952011-01-13 00:21:202413}
2414
[email protected]0dfe05c2011-02-23 23:03:362415const Extension* ExtensionService::GetTerminatedExtension(
[email protected]8001df22011-04-28 19:59:472416 const std::string& id) const {
[email protected]695b5712012-12-06 23:55:282417 return GetExtensionById(id, INCLUDE_TERMINATED);
[email protected]8001df22011-04-28 19:59:472418}
2419
2420const Extension* ExtensionService::GetInstalledExtension(
2421 const std::string& id) const {
[email protected]695b5712012-12-06 23:55:282422 int include_mask = INCLUDE_ENABLED |
2423 INCLUDE_DISABLED |
2424 INCLUDE_TERMINATED |
2425 INCLUDE_BLACKLISTED;
2426 return GetExtensionById(id, include_mask);
[email protected]0dfe05c2011-02-23 23:03:362427}
2428
[email protected]eaa7dd182010-12-14 11:09:002429bool ExtensionService::ExtensionBindingsAllowed(const GURL& url) {
[email protected]615d88f2011-12-13 01:47:442430 // Allow bindings for all packaged extensions and component hosted apps.
2431 const Extension* extension = extensions_.GetExtensionOrAppByURL(
2432 ExtensionURLInfo(url));
2433 return extension && (!extension->is_hosted_app() ||
[email protected]1d5e58b2013-01-31 08:41:402434 extension->location() == Manifest::COMPONENT);
[email protected]6d2e60bd2010-06-03 22:37:392435}
2436
[email protected]4d007b312012-10-17 03:00:482437bool ExtensionService::ShouldBlockUrlInBrowserTab(GURL* url) {
2438 const Extension* extension = extensions_.GetExtensionOrAppByURL(
2439 ExtensionURLInfo(*url));
2440 if (extension && extension->is_platform_app()) {
2441 *url = GURL(chrome::kExtensionInvalidRequestURL);
2442 return true;
2443 }
2444
2445 return false;
2446}
2447
[email protected]9060d8b02012-01-13 02:14:302448bool ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:322449 const std::string& id,
[email protected]8e4560b62011-01-14 10:09:142450 const Version* version,
[email protected]650b2d52013-02-10 03:41:452451 const base::FilePath& path,
[email protected]1d5e58b2013-01-31 08:41:402452 Manifest::Location location,
[email protected]47fc70c2011-12-06 07:29:512453 int creation_flags,
2454 bool mark_acknowledged) {
[email protected]ab22ba42011-01-14 16:36:382455 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2456 CHECK(Extension::IdIsValid(id));
[email protected]79c833b52011-04-05 18:31:012457 if (extension_prefs_->IsExternalExtensionUninstalled(id))
[email protected]9060d8b02012-01-13 02:14:302458 return false;
[email protected]a8af9fdb2010-10-28 21:52:202459
[email protected]7577a5c52009-07-30 06:21:582460 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:492461 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:582462 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:022463 const Extension* existing = GetExtensionById(id, true);
[email protected]910f72ce2012-08-24 01:38:352464
[email protected]7577a5c52009-07-30 06:21:582465 if (existing) {
[email protected]910f72ce2012-08-24 01:38:352466 // The default apps will have the location set as INTERNAL. Since older
2467 // default apps are installed as EXTERNAL, we override them. However, if the
2468 // app is already installed as internal, then do the version check.
[email protected]31bb5ee62012-09-12 22:58:402469 // TODO(grv) : Remove after Q1-2013.
[email protected]910f72ce2012-08-24 01:38:352470 bool is_default_apps_migration =
[email protected]1d5e58b2013-01-31 08:41:402471 (location == Manifest::INTERNAL &&
2472 Manifest::IsExternalLocation(existing->location()));
[email protected]910f72ce2012-08-24 01:38:352473
2474 if (!is_default_apps_migration) {
2475 DCHECK(version);
2476
2477 switch (existing->version()->CompareTo(*version)) {
2478 case -1: // existing version is older, we should upgrade
2479 break;
2480 case 0: // existing version is same, do nothing
2481 return false;
2482 case 1: // existing version is newer, uh-oh
2483 LOG(WARNING) << "Found external version of extension " << id
2484 << "that is older than current version. Current version "
2485 << "is: " << existing->VersionString() << ". New "
2486 << "version is: " << version->GetString()
2487 << ". Keeping current version.";
2488 return false;
2489 }
[email protected]7577a5c52009-07-30 06:21:582490 }
2491 }
2492
[email protected]9060d8b02012-01-13 02:14:302493 // If the extension is already pending, don't start an install.
[email protected]e3987852012-05-04 10:06:302494 if (!pending_extension_manager()->AddFromExternalFile(
2495 id, location, *version)) {
[email protected]9060d8b02012-01-13 02:14:302496 return false;
[email protected]e3987852012-05-04 10:06:302497 }
[email protected]9c635f22010-12-02 09:36:362498
[email protected]14908b72011-04-20 06:54:362499 // no client (silent install)
[email protected]d8c8f25f2011-11-02 18:18:012500 scoped_refptr<CrxInstaller> installer(CrxInstaller::Create(this, NULL));
[email protected]6dfbbf82010-03-12 23:09:162501 installer->set_install_source(location);
2502 installer->set_expected_id(id);
[email protected]cb0e50312011-05-09 15:03:072503 installer->set_expected_version(*version);
2504 installer->set_install_cause(extension_misc::INSTALL_CAUSE_EXTERNAL_FILE);
[email protected]1bf73cc32011-10-26 22:38:312505 installer->set_creation_flags(creation_flags);
[email protected]88e8ec9152013-01-17 04:05:182506#if defined(OS_CHROMEOS)
2507 extensions::InstallLimiter::Get(profile_)->Add(installer, path);
2508#else
[email protected]6dfbbf82010-03-12 23:09:162509 installer->InstallCrx(path);
[email protected]88e8ec9152013-01-17 04:05:182510#endif
[email protected]47fc70c2011-12-06 07:29:512511
2512 // Depending on the source, a new external extension might not need a user
2513 // notification on installation. For such extensions, mark them acknowledged
2514 // now to suppress the notification.
2515 if (mark_acknowledged)
2516 AcknowledgeExternalExtension(id);
[email protected]9060d8b02012-01-13 02:14:302517
2518 return true;
[email protected]7577a5c52009-07-30 06:21:582519}
2520
[email protected]eaa7dd182010-12-14 11:09:002521void ExtensionService::ReportExtensionLoadError(
[email protected]650b2d52013-02-10 03:41:452522 const base::FilePath& extension_path,
[email protected]d11c8e92009-10-20 23:26:402523 const std::string &error,
[email protected]d11c8e92009-10-20 23:26:402524 bool be_noisy) {
[email protected]d8c8f25f2011-11-02 18:18:012525 content::NotificationService::current()->Notify(
2526 chrome::NOTIFICATION_EXTENSION_LOAD_ERROR,
2527 content::Source<Profile>(profile_),
2528 content::Details<const std::string>(&error));
[email protected]d11c8e92009-10-20 23:26:402529
[email protected]8a205c02011-02-04 20:41:332530 std::string path_str = UTF16ToUTF8(extension_path.LossyDisplayName());
[email protected]438afb12012-07-05 03:18:212531 string16 message = UTF8ToUTF16(base::StringPrintf(
[email protected]18d4b6c2010-09-21 03:21:042532 "Could not load extension from '%s'. %s",
[email protected]fc670822011-12-17 09:33:492533 path_str.c_str(), error.c_str()));
[email protected]d11c8e92009-10-20 23:26:402534 ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy);
2535}
2536
[email protected]eaa7dd182010-12-14 11:09:002537void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]3a1dc572012-07-31 22:25:132538 extensions::ExtensionHost* host) {
[email protected]406027c02010-09-27 08:03:182539 OrphanedDevTools::iterator iter =
[email protected]2d2f6cfc2011-05-06 21:09:332540 orphaned_dev_tools_.find(host->extension_id());
[email protected]406027c02010-09-27 08:03:182541 if (iter == orphaned_dev_tools_.end())
2542 return;
2543
[email protected]98f66112012-12-25 12:59:362544 DevToolsAgentHost::ConnectRenderViewHost(iter->second,
2545 host->render_view_host());
[email protected]406027c02010-09-27 08:03:182546 orphaned_dev_tools_.erase(iter);
2547}
2548
[email protected]432115822011-07-10 15:52:272549void ExtensionService::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:532550 const content::NotificationSource& source,
2551 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:272552 switch (type) {
[email protected]3c4abc82012-10-22 22:25:542553 case chrome::NOTIFICATION_APP_TERMINATING:
2554 // Shutdown has started. Don't start any more extension installs.
2555 // (We cannot use ExtensionService::Shutdown() for this because it
2556 // happens too late in browser teardown.)
2557 browser_terminating_ = true;
2558 break;
[email protected]432115822011-07-10 15:52:272559 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]6c2381d2011-10-19 02:52:532560 if (profile_ !=
2561 content::Source<Profile>(source).ptr()->GetOriginalProfile()) {
[email protected]bc535ee52010-08-31 18:40:322562 break;
[email protected]6c2381d2011-10-19 02:52:532563 }
[email protected]a4ed6282009-12-14 20:51:162564
[email protected]3a1dc572012-07-31 22:25:132565 extensions::ExtensionHost* host =
2566 content::Details<extensions::ExtensionHost>(details).ptr();
[email protected]f128af42010-08-05 18:05:262567
[email protected]fa2416f2011-05-03 08:41:202568 // Mark the extension as terminated and Unload it. We want it to
2569 // be in a consistent state: either fully working or not loaded
2570 // at all, but never half-crashed. We do it in a PostTask so
2571 // that other handlers of this notification will still have
2572 // access to the Extension and ExtensionHost.
[email protected]b3a25092013-05-28 22:08:162573 base::MessageLoop::current()->PostTask(
[email protected]14908b72011-04-20 06:54:362574 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362575 base::Bind(
[email protected]fa2416f2011-05-03 08:41:202576 &ExtensionService::TrackTerminatedExtension,
[email protected]d8c8f25f2011-11-02 18:18:012577 AsWeakPtr(),
[email protected]fa2416f2011-05-03 08:41:202578 host->extension()));
[email protected]31f77262009-12-02 20:48:532579 break;
2580 }
[email protected]432115822011-07-10 15:52:272581 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: {
[email protected]f3b1a082011-11-18 00:34:302582 content::RenderProcessHost* process =
2583 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192584 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302585 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192586 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2587 break;
2588
[email protected]15397b42012-05-16 23:56:062589 // Extensions need to know the channel for API restrictions.
2590 process->Send(new ExtensionMsg_SetChannel(
[email protected]5243df072012-07-11 21:41:012591 extensions::Feature::GetCurrentChannel()));
[email protected]15397b42012-05-16 23:56:062592
[email protected]4941fe92013-06-13 23:21:012593 // Platform apps need to know the system font.
2594 scoped_ptr<base::DictionaryValue> fonts(new base::DictionaryValue);
2595 webui::SetFontAndTextDirection(fonts.get());
2596 std::string font_family, font_size;
2597 fonts->GetString("fontfamily", &font_family);
2598 fonts->GetString("fontsize", &font_size);
2599 process->Send(new ExtensionMsg_SetSystemFont(
2600 font_family, font_size));
2601
[email protected]77a6970c2011-04-23 16:58:562602 // Valid extension function names, used to setup bindings in renderer.
2603 std::vector<std::string> function_names;
2604 ExtensionFunctionDispatcher::GetAllFunctionNames(&function_names);
2605 process->Send(new ExtensionMsg_SetFunctionNames(function_names));
[email protected]31f77262009-12-02 20:48:532606
[email protected]77a6970c2011-04-23 16:58:562607 // Scripting whitelist. This is modified by tests and must be communicated
2608 // to renderers.
2609 process->Send(new ExtensionMsg_SetScriptingWhitelist(
2610 *Extension::GetScriptingWhitelist()));
2611
2612 // Loaded extensions.
[email protected]9776e82e2011-11-15 02:17:532613 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions;
[email protected]84df8332011-12-06 18:22:462614 for (ExtensionSet::const_iterator iter = extensions_.begin();
2615 iter != extensions_.end(); ++iter) {
[email protected]19647262011-12-16 09:57:492616 // Renderers don't need to know about themes.
2617 if (!(*iter)->is_theme())
[email protected]cadac622013-06-11 16:46:362618 loaded_extensions.push_back(ExtensionMsg_Loaded_Params(iter->get()));
[email protected]77a6970c2011-04-23 16:58:562619 }
[email protected]9776e82e2011-11-15 02:17:532620 process->Send(new ExtensionMsg_Loaded(loaded_extensions));
[email protected]77a6970c2011-04-23 16:58:562621 break;
2622 }
[email protected]432115822011-07-10 15:52:272623 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
[email protected]f3b1a082011-11-18 00:34:302624 content::RenderProcessHost* process =
2625 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192626 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302627 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192628 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2629 break;
2630
[email protected]6bc04fd82011-12-04 02:29:352631 process_map_.RemoveAllFromProcess(process->GetID());
[email protected]6f371442011-11-09 06:45:462632 BrowserThread::PostTask(
2633 BrowserThread::IO, FROM_HERE,
2634 base::Bind(&ExtensionInfoMap::UnregisterAllExtensionsInProcess,
[email protected]31d8f5f22012-04-02 15:22:082635 system_->info_map(),
[email protected]f3b1a082011-11-18 00:34:302636 process->GetID()));
[email protected]da5683db2011-04-23 17:12:212637 break;
2638 }
[email protected]d91268022011-08-26 13:17:372639 case chrome::NOTIFICATION_IMPORT_FINISHED: {
[email protected]cf593af2011-12-30 05:44:392640 InitAfterImport();
[email protected]d91268022011-08-26 13:17:372641 break;
2642 }
[email protected]0db124b02012-11-07 04:55:052643 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: {
2644 extensions::ExtensionHost* host =
2645 content::Details<extensions::ExtensionHost>(details).ptr();
[email protected]e7aa7b7e2012-11-27 04:51:222646 std::string extension_id = host->extension_id();
[email protected]399583b2012-12-11 09:33:422647 if (delayed_updates_for_idle_.Contains(extension_id)) {
[email protected]e7aa7b7e2012-11-27 04:51:222648 // We were waiting for this extension to become idle, it now might have,
2649 // so maybe finish installation.
[email protected]b3a25092013-05-28 22:08:162650 base::MessageLoop::current()->PostDelayedTask(
[email protected]0db124b02012-11-07 04:55:052651 FROM_HERE,
[email protected]6f6101832012-11-27 22:10:482652 base::Bind(&ExtensionService::MaybeFinishDelayedInstallation,
[email protected]0db124b02012-11-07 04:55:052653 AsWeakPtr(), extension_id),
2654 base::TimeDelta::FromSeconds(kUpdateIdleDelay));
[email protected]0db124b02012-11-07 04:55:052655 }
2656 break;
2657 }
[email protected]92dd8d92013-02-26 00:41:082658 case chrome::NOTIFICATION_UPGRADE_RECOMMENDED: {
2659 // Notify extensions that chrome update is available.
2660 extensions::RuntimeEventRouter::DispatchOnBrowserUpdateAvailableEvent(
2661 profile_);
[email protected]75bdcb872013-03-13 00:41:452662
2663 // Notify observers that chrome update is available.
2664 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
2665 OnChromeUpdateAvailable());
[email protected]92dd8d92013-02-26 00:41:082666 break;
2667 }
[email protected]aa96d3a2010-08-21 08:45:252668
[email protected]4814b512009-11-07 00:12:292669 default:
2670 NOTREACHED() << "Unexpected notification type.";
2671 }
2672}
2673
[email protected]90bb38d2012-11-14 18:36:032674void ExtensionService::OnExtensionInstallPrefChanged() {
[email protected]a6a7ced2012-11-01 17:24:182675 IdentifyAlertableExtensions();
2676 CheckManagementPolicy();
2677}
2678
[email protected]eaa7dd182010-12-14 11:09:002679bool ExtensionService::HasApps() const {
[email protected]ec5b50d2010-10-09 16:35:182680 return !GetAppIds().empty();
2681}
[email protected]377011d2010-07-20 04:18:502682
[email protected]eaa7dd182010-12-14 11:09:002683ExtensionIdSet ExtensionService::GetAppIds() const {
[email protected]ec5b50d2010-10-09 16:35:182684 ExtensionIdSet result;
[email protected]84df8332011-12-06 18:22:462685 for (ExtensionSet::const_iterator it = extensions_.begin();
[email protected]377011d2010-07-20 04:18:502686 it != extensions_.end(); ++it) {
[email protected]1d5e58b2013-01-31 08:41:402687 if ((*it)->is_app() && (*it)->location() != Manifest::COMPONENT)
[email protected]ec5b50d2010-10-09 16:35:182688 result.insert((*it)->id());
[email protected]377011d2010-07-20 04:18:502689 }
2690
[email protected]ec5b50d2010-10-09 16:35:182691 return result;
[email protected]377011d2010-07-20 04:18:502692}
[email protected]d7e9a862010-11-03 21:57:492693
[email protected]dc9a74f72012-08-17 18:07:212694bool ExtensionService::IsBackgroundPageReady(const Extension* extension) const {
[email protected]9367eabc2013-03-01 01:29:292695 if (!extensions::BackgroundInfo::HasPersistentBackgroundPage(extension))
[email protected]dc9a74f72012-08-17 18:07:212696 return true;
2697 ExtensionRuntimeDataMap::const_iterator it =
2698 extension_runtime_data_.find(extension->id());
2699 return it == extension_runtime_data_.end() ? false :
2700 it->second.background_page_ready;
[email protected]d7e9a862010-11-03 21:57:492701}
2702
[email protected]eaa7dd182010-12-14 11:09:002703void ExtensionService::SetBackgroundPageReady(const Extension* extension) {
[email protected]9367eabc2013-03-01 01:29:292704 DCHECK(extensions::BackgroundInfo::HasBackgroundPage(extension));
[email protected]d7e9a862010-11-03 21:57:492705 extension_runtime_data_[extension->id()].background_page_ready = true;
[email protected]ad50def52011-10-19 23:17:072706 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272707 chrome::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY,
[email protected]6c2381d2011-10-19 02:52:532708 content::Source<const Extension>(extension),
[email protected]ad50def52011-10-19 23:17:072709 content::NotificationService::NoDetails());
[email protected]d7e9a862010-11-03 21:57:492710}
2711
[email protected]65992d52012-09-19 23:05:312712void ExtensionService::InspectBackgroundPage(const Extension* extension) {
2713 DCHECK(extension);
2714
[email protected]fdd679b2012-11-15 20:49:392715 ExtensionProcessManager* pm = system_->process_manager();
[email protected]65992d52012-09-19 23:05:312716 extensions::LazyBackgroundTaskQueue* queue =
[email protected]fdd679b2012-11-15 20:49:392717 system_->lazy_background_task_queue();
[email protected]65992d52012-09-19 23:05:312718
2719 extensions::ExtensionHost* host =
2720 pm->GetBackgroundHostForExtension(extension->id());
2721 if (host) {
2722 InspectExtensionHost(host);
2723 } else {
2724 queue->AddPendingTask(
2725 profile_, extension->id(),
2726 base::Bind(&ExtensionService::InspectExtensionHost,
2727 base::Unretained(this)));
2728 }
2729}
2730
[email protected]dc9a74f72012-08-17 18:07:212731bool ExtensionService::IsBeingUpgraded(const Extension* extension) const {
2732 ExtensionRuntimeDataMap::const_iterator it =
2733 extension_runtime_data_.find(extension->id());
2734 return it == extension_runtime_data_.end() ? false :
2735 it->second.being_upgraded;
[email protected]d7e9a862010-11-03 21:57:492736}
2737
[email protected]eaa7dd182010-12-14 11:09:002738void ExtensionService::SetBeingUpgraded(const Extension* extension,
[email protected]b914e2952013-04-26 07:10:032739 bool value) {
[email protected]d7e9a862010-11-03 21:57:492740 extension_runtime_data_[extension->id()].being_upgraded = value;
2741}
[email protected]1bead0712010-11-27 17:41:532742
[email protected]dc9a74f72012-08-17 18:07:212743bool ExtensionService::HasUsedWebRequest(const Extension* extension) const {
2744 ExtensionRuntimeDataMap::const_iterator it =
2745 extension_runtime_data_.find(extension->id());
2746 return it == extension_runtime_data_.end() ? false :
2747 it->second.has_used_webrequest;
[email protected]39a5b532011-10-22 01:47:072748}
2749
2750void ExtensionService::SetHasUsedWebRequest(const Extension* extension,
2751 bool value) {
2752 extension_runtime_data_[extension->id()].has_used_webrequest = value;
2753}
2754
[email protected]65992d52012-09-19 23:05:312755void ExtensionService::InspectExtensionHost(
2756 extensions::ExtensionHost* host) {
2757 if (host)
2758 DevToolsWindow::OpenDevToolsWindow(host->render_view_host());
2759}
[email protected]612a1cb12012-10-17 13:18:032760
2761bool ExtensionService::ShouldEnableOnInstall(const Extension* extension) {
2762 // Extensions installed by policy can't be disabled. So even if a previous
2763 // installation disabled the extension, make sure it is now enabled.
2764 if (system_->management_policy()->MustRemainEnabled(extension, NULL))
2765 return true;
2766
2767 if (extension_prefs_->IsExtensionDisabled(extension->id()))
2768 return false;
2769
[email protected]41070e8d2012-10-24 01:34:362770 if (FeatureSwitch::prompt_for_external_extensions()->IsEnabled()) {
[email protected]612a1cb12012-10-17 13:18:032771 // External extensions are initially disabled. We prompt the user before
[email protected]2c495c42013-01-04 21:49:542772 // enabling them. Hosted apps are excepted because they are not dangerous
2773 // (they need to be launched by the user anyway).
[email protected]1d5e58b2013-01-31 08:41:402774 if (extension->GetType() != Manifest::TYPE_HOSTED_APP &&
2775 Manifest::IsExternalLocation(extension->location()) &&
[email protected]612a1cb12012-10-17 13:18:032776 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id())) {
2777 return false;
2778 }
2779 }
[email protected]612a1cb12012-10-17 13:18:032780
2781 return true;
2782}
[email protected]0db124b02012-11-07 04:55:052783
2784bool ExtensionService::IsExtensionIdle(const std::string& extension_id) const {
[email protected]d9a61e12012-11-14 02:43:472785 ExtensionProcessManager* process_manager = system_->process_manager();
[email protected]fc332ae2012-11-14 20:17:332786 DCHECK(process_manager);
[email protected]0db124b02012-11-07 04:55:052787 extensions::ExtensionHost* host =
[email protected]d9a61e12012-11-14 02:43:472788 process_manager->GetBackgroundHostForExtension(extension_id);
[email protected]e7aa7b7e2012-11-27 04:51:222789 if (host)
2790 return false;
2791 return process_manager->GetRenderViewHostsForExtension(extension_id).empty();
2792}
2793
2794bool ExtensionService::ShouldDelayExtensionUpdate(
2795 const std::string& extension_id,
2796 bool wait_for_idle) const {
2797 const char kOnUpdateAvailableEvent[] = "runtime.onUpdateAvailable";
2798
2799 // If delayed updates are globally disabled, or just for this extension,
2800 // don't delay.
2801 if (!install_updates_when_idle_ || !wait_for_idle)
2802 return false;
2803
[email protected]695b5712012-12-06 23:55:282804 const Extension* old = GetInstalledExtension(extension_id);
[email protected]e7aa7b7e2012-11-27 04:51:222805 // If there is no old extension, this is not an update, so don't delay.
2806 if (!old)
2807 return false;
2808
[email protected]9367eabc2013-03-01 01:29:292809 if (extensions::BackgroundInfo::HasPersistentBackgroundPage(old)) {
[email protected]e7aa7b7e2012-11-27 04:51:222810 // Delay installation if the extension listens for the onUpdateAvailable
2811 // event.
2812 return system_->event_router()->ExtensionHasEventListener(
2813 extension_id, kOnUpdateAvailableEvent);
2814 } else {
2815 // Delay installation if the extension is not idle.
2816 return !IsExtensionIdle(extension_id);
2817 }
[email protected]0db124b02012-11-07 04:55:052818}
[email protected]fdd679b2012-11-15 20:49:392819
[email protected]399583b2012-12-11 09:33:422820void ExtensionService::GarbageCollectIsolatedStorage() {
[email protected]650b2d52013-02-10 03:41:452821 scoped_ptr<base::hash_set<base::FilePath> > active_paths(
2822 new base::hash_set<base::FilePath>());
[email protected]399583b2012-12-11 09:33:422823 for (ExtensionSet::const_iterator it = extensions_.begin();
2824 it != extensions_.end(); ++it) {
[email protected]cadac622013-06-11 16:46:362825 if (extensions::AppIsolationInfo::HasIsolatedStorage(it->get())) {
2826 active_paths->insert(BrowserContext::GetStoragePartitionForSite(
2827 profile_, GetSiteForExtensionId((*it)->id()))->GetPath());
[email protected]399583b2012-12-11 09:33:422828 }
2829 }
2830
2831 DCHECK(!installs_delayed());
2832 set_installs_delayed(true);
2833 BrowserContext::GarbageCollectStoragePartitions(
2834 profile_, active_paths.Pass(),
2835 base::Bind(&ExtensionService::OnGarbageCollectIsolatedStorageFinished,
2836 AsWeakPtr()));
2837}
2838
2839void ExtensionService::OnGarbageCollectIsolatedStorageFinished() {
2840 set_installs_delayed(false);
2841 for (ExtensionSet::const_iterator it = delayed_installs_.begin();
2842 it != delayed_installs_.end();
2843 ++it) {
2844 FinishDelayedInstallation((*it)->id());
2845 }
2846 for (ExtensionSet::const_iterator it = delayed_updates_for_idle_.begin();
2847 it != delayed_updates_for_idle_.end();
2848 ++it) {
2849 MaybeFinishDelayedInstallation((*it)->id());
2850 }
2851 delayed_installs_.Clear();
2852}
2853
2854void ExtensionService::OnNeedsToGarbageCollectIsolatedStorage() {
2855 extension_prefs_->SetNeedsStorageGarbageCollection(true);
2856}
2857
[email protected]fdd679b2012-11-15 20:49:392858void ExtensionService::OnBlacklistUpdated() {
[email protected]695b5712012-12-06 23:55:282859 blacklist_->GetBlacklistedIDs(
2860 GenerateInstalledExtensionsSet()->GetIDs(),
2861 base::Bind(&ExtensionService::ManageBlacklist,
2862 AsWeakPtr(),
2863 blacklisted_extensions_.GetIDs()));
2864}
2865
2866void ExtensionService::ManageBlacklist(
2867 const std::set<std::string>& old_blacklisted_ids,
2868 const std::set<std::string>& new_blacklisted_ids) {
2869 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2870
2871 std::set<std::string> no_longer_blacklisted;
2872 std::set_difference(old_blacklisted_ids.begin(), old_blacklisted_ids.end(),
2873 new_blacklisted_ids.begin(), new_blacklisted_ids.end(),
2874 std::inserter(no_longer_blacklisted,
2875 no_longer_blacklisted.begin()));
2876 std::set<std::string> not_yet_blacklisted;
2877 std::set_difference(new_blacklisted_ids.begin(), new_blacklisted_ids.end(),
2878 old_blacklisted_ids.begin(), old_blacklisted_ids.end(),
2879 std::inserter(not_yet_blacklisted,
2880 not_yet_blacklisted.begin()));
2881
2882 for (std::set<std::string>::iterator it = no_longer_blacklisted.begin();
2883 it != no_longer_blacklisted.end(); ++it) {
2884 scoped_refptr<const Extension> extension =
2885 blacklisted_extensions_.GetByID(*it);
[email protected]dc24976f2013-06-02 21:15:092886 DCHECK(extension.get());
2887 if (!extension.get())
[email protected]695b5712012-12-06 23:55:282888 continue;
2889 blacklisted_extensions_.Remove(*it);
[email protected]dc24976f2013-06-02 21:15:092890 AddExtension(extension.get());
[email protected]ac875372013-02-28 04:36:092891 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.UnblacklistInstalled",
[email protected]dc24976f2013-06-02 21:15:092892 extension->location(),
2893 Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:282894 }
2895
2896 for (std::set<std::string>::iterator it = not_yet_blacklisted.begin();
2897 it != not_yet_blacklisted.end(); ++it) {
2898 scoped_refptr<const Extension> extension = GetInstalledExtension(*it);
[email protected]dc24976f2013-06-02 21:15:092899 DCHECK(extension.get());
2900 if (!extension.get())
[email protected]695b5712012-12-06 23:55:282901 continue;
2902 blacklisted_extensions_.Insert(extension);
2903 UnloadExtension(*it, extension_misc::UNLOAD_REASON_BLACKLIST);
[email protected]ac875372013-02-28 04:36:092904 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.BlacklistInstalled",
2905 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:282906 }
2907
2908 IdentifyAlertableExtensions();
[email protected]fdd679b2012-11-15 20:49:392909}
[email protected]75bdcb872013-03-13 00:41:452910
2911void ExtensionService::AddUpdateObserver(extensions::UpdateObserver* observer) {
2912 update_observers_.AddObserver(observer);
2913}
2914
2915void ExtensionService::RemoveUpdateObserver(
2916 extensions::UpdateObserver* observer) {
2917 update_observers_.RemoveObserver(observer);
2918}