blob: f1cb6c0fca5eb92a9142da64f0add913356c6922 [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]1e127022011-11-29 21:11:4718#include "base/path_service.h"
[email protected]3853a4c2013-02-11 17:15:5719#include "base/prefs/pref_service.h"
[email protected]7286e3fc2011-07-19 22:13:2420#include "base/stl_util.h"
[email protected]6014d672008-12-05 00:38:2521#include "base/string_util.h"
[email protected]18d4b6c2010-09-21 03:21:0422#include "base/stringprintf.h"
[email protected]3ea1b182013-02-08 22:38:4123#include "base/strings/string_number_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]ce7f62e32010-08-10 23:43:5927#include "base/utf_string_conversions.h"
[email protected]cc655912009-01-29 23:19:1928#include "base/values.h"
[email protected]aa142702010-03-26 01:26:3329#include "base/version.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]c2295f72013-01-03 22:18:5666#include "chrome/browser/plugins/chrome_plugin_service_filter.h"
[email protected]8ecad5e2010-12-02 21:18:3367#include "chrome/browser/profiles/profile.h"
[email protected]cf593af2011-12-30 05:44:3968#include "chrome/browser/profiles/profile_manager.h"
[email protected]d5949312012-12-03 22:13:3069#include "chrome/browser/themes/theme_service.h"
70#include "chrome/browser/themes/theme_service_factory.h"
[email protected]c8d407e2011-04-28 21:27:1771#include "chrome/browser/ui/webui/favicon_source.h"
[email protected]5eddc3e2011-10-26 04:33:3172#include "chrome/browser/ui/webui/ntp/thumbnail_source.h"
[email protected]b07e606e2012-09-15 20:16:1573#include "chrome/browser/ui/webui/theme_source.h"
[email protected]aab98a52009-12-02 03:22:3574#include "chrome/common/child_process_logging.h"
[email protected]432115822011-07-10 15:52:2775#include "chrome/common/chrome_notification_types.h"
[email protected]1e127022011-11-29 21:11:4776#include "chrome/common/chrome_paths.h"
[email protected]e2eb43112009-05-29 21:19:5477#include "chrome/common/chrome_switches.h"
[email protected]15397b42012-05-16 23:56:0678#include "chrome/common/chrome_version_info.h"
[email protected]098fa7a2013-03-08 22:11:1779#include "chrome/common/extensions/api/plugins/plugins_handler.h"
[email protected]9367eabc2013-03-01 01:29:2980#include "chrome/common/extensions/background_info.h"
[email protected]5b1a0e22009-05-26 19:00:5881#include "chrome/common/extensions/extension.h"
[email protected]7c927b62010-02-24 09:54:1382#include "chrome/common/extensions/extension_file_util.h"
[email protected]895a1e52012-05-15 02:50:1283#include "chrome/common/extensions/extension_manifest_constants.h"
[email protected]77a6970c2011-04-23 16:58:5684#include "chrome/common/extensions/extension_messages.h"
[email protected]612a1cb12012-10-17 13:18:0385#include "chrome/common/extensions/feature_switch.h"
[email protected]e9f541a2012-11-19 21:52:3186#include "chrome/common/extensions/features/feature.h"
[email protected]06492ed2013-03-24 22:13:1487#include "chrome/common/extensions/incognito_handler.h"
[email protected]ac875372013-02-28 04:36:0988#include "chrome/common/extensions/manifest.h"
[email protected]561e33d52013-04-03 06:58:4389#include "chrome/common/extensions/manifest_handlers/app_isolation_info.h"
[email protected]65348062013-01-15 07:27:2290#include "chrome/common/extensions/manifest_url_handler.h"
[email protected]2cb5e302013-05-09 05:00:0691#include "chrome/common/extensions/permissions/permissions_data.h"
[email protected]25b34332009-06-05 21:53:1992#include "chrome/common/pref_names.h"
[email protected]21f2e2d2013-01-23 22:59:5293#include "chrome/common/startup_metric_utils.h"
[email protected]a57209872009-05-04 22:53:1494#include "chrome/common/url_constants.h"
[email protected]c38831a12011-10-28 12:44:4995#include "content/public/browser/browser_thread.h"
[email protected]98f66112012-12-25 12:59:3696#include "content/public/browser/devtools_agent_host.h"
[email protected]05aad2da2011-10-28 10:12:3797#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1698#include "content/public/browser/notification_types.h"
[email protected]3a5180ae2011-12-21 02:39:3899#include "content/public/browser/plugin_service.h"
[email protected]f3b1a082011-11-18 00:34:30100#include "content/public/browser/render_process_host.h"
[email protected]399583b2012-12-11 09:33:42101#include "content/public/browser/site_instance.h"
102#include "content/public/browser/storage_partition.h"
[email protected]24ea7a12013-01-27 23:54:53103#include "content/public/browser/url_data_source.h"
[email protected]1e127022011-11-29 21:11:47104#include "content/public/common/pepper_plugin_info.h"
[email protected]836e2982013-05-16 08:07:42105#include "extensions/common/constants.h"
[email protected]e9f541a2012-11-19 21:52:31106#include "extensions/common/error_utils.h"
[email protected]c10da4b02010-03-25 14:38:32107#include "googleurl/src/gurl.h"
[email protected]65187152012-06-02 13:14:14108#include "grit/generated_resources.h"
[email protected]be28b5f42012-07-20 11:31:25109#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
[email protected]895a1e52012-05-15 02:50:12110#include "sync/api/sync_change.h"
111#include "sync/api/sync_error_factory.h"
[email protected]08b1f75f2013-05-22 22:02:38112#include "webkit/browser/database/database_tracker.h"
113#include "webkit/browser/database/database_util.h"
[email protected]79a60642012-10-20 21:03:18114
[email protected]eed367e2011-04-12 03:43:31115#if defined(OS_CHROMEOS)
[email protected]88e8ec9152013-01-17 04:05:18116#include "chrome/browser/chromeos/extensions/install_limiter.h"
[email protected]c4298d02013-05-20 05:42:52117#include "webkit/browser/fileapi/file_system_mount_point_provider.h"
[email protected]b777b332011-04-16 04:01:08118#include "webkit/fileapi/file_system_context.h"
[email protected]eed367e2011-04-12 03:43:31119#endif
120
[email protected]55eb70e762012-02-20 17:38:39121using content::BrowserContext;
[email protected]631bb742011-11-02 11:29:39122using content::BrowserThread;
[email protected]98f66112012-12-25 12:59:36123using content::DevToolsAgentHost;
[email protected]3a5180ae2011-12-21 02:39:38124using content::PluginService;
[email protected]bf3d9df2012-07-24 23:20:27125using extensions::CrxInstaller;
[email protected]1c321ee52012-05-21 03:02:34126using extensions::Extension;
127using extensions::ExtensionIdSet;
128using extensions::ExtensionInfo;
[email protected]215a7be2012-10-22 19:53:42129using extensions::FeatureSwitch;
[email protected]1d5e58b2013-01-31 08:41:40130using extensions::Manifest;
[email protected]c2e66e12012-06-27 06:27:06131using extensions::PermissionMessage;
132using extensions::PermissionMessages;
133using extensions::PermissionSet;
[email protected]e410b5f2012-12-14 14:02:24134using extensions::UnloadedExtensionInfo;
[email protected]5ef47ec2010-01-28 05:58:05135
[email protected]c6d474f82009-12-16 21:11:06136namespace errors = extension_manifest_errors;
137
[email protected]b6ab96d2009-08-20 18:58:19138namespace {
139
[email protected]d96eb512012-11-01 23:44:08140// Histogram values for logging events related to externally installed
141// extensions.
142enum ExternalExtensionEvent {
143 EXTERNAL_EXTENSION_INSTALLED = 0,
144 EXTERNAL_EXTENSION_IGNORED,
145 EXTERNAL_EXTENSION_REENABLED,
146 EXTERNAL_EXTENSION_UNINSTALLED,
147 EXTERNAL_EXTENSION_BUCKET_BOUNDARY,
148};
149
[email protected]612a1cb12012-10-17 13:18:03150// Prompt the user this many times before considering an extension acknowledged.
151static const int kMaxExtensionAcknowledgePromptCount = 3;
152
[email protected]0db124b02012-11-07 04:55:05153// Wait this many seconds after an extensions becomes idle before updating it.
154static const int kUpdateIdleDelay = 5;
155
[email protected]9bd9a6862012-11-29 09:24:22156// Wait this many seconds before trying to garbage collect extensions again.
157static const int kGarbageCollectRetryDelay = 30;
158
[email protected]300c0ea2011-07-15 23:04:33159const char* kNaClPluginMimeType = "application/x-nacl";
160
[email protected]3bdba0d2011-08-23 07:17:30161static bool IsSyncableExtension(const Extension& extension) {
162 return extension.GetSyncType() == Extension::SYNC_TYPE_EXTENSION;
163}
164
165static bool IsSyncableApp(const Extension& extension) {
166 return extension.GetSyncType() == Extension::SYNC_TYPE_APP;
167}
168
[email protected]c6d474f82009-12-16 21:11:06169} // namespace
[email protected]b6ab96d2009-08-20 18:58:19170
[email protected]eaa7dd182010-12-14 11:09:00171ExtensionService::ExtensionRuntimeData::ExtensionRuntimeData()
[email protected]d7e9a862010-11-03 21:57:49172 : background_page_ready(false),
[email protected]f8bf5992011-10-24 22:38:57173 being_upgraded(false),
174 has_used_webrequest(false) {
[email protected]d7e9a862010-11-03 21:57:49175}
176
[email protected]eaa7dd182010-12-14 11:09:00177ExtensionService::ExtensionRuntimeData::~ExtensionRuntimeData() {
[email protected]d7e9a862010-11-03 21:57:49178}
179
[email protected]cebc3dc2011-04-18 17:15:00180ExtensionService::NaClModuleInfo::NaClModuleInfo() {
181}
182
183ExtensionService::NaClModuleInfo::~NaClModuleInfo() {
184}
185
[email protected]eaa7dd182010-12-14 11:09:00186// ExtensionService.
[email protected]6014d672008-12-05 00:38:25187
[email protected]31bb5ee62012-09-12 22:58:40188const char ExtensionService::kLocalAppSettingsDirectoryName[] =
[email protected]15d5b472011-11-24 04:17:24189 "Local App Settings";
[email protected]31bb5ee62012-09-12 22:58:40190const char ExtensionService::kLocalExtensionSettingsDirectoryName[] =
[email protected]15d5b472011-11-24 04:17:24191 "Local Extension Settings";
[email protected]31bb5ee62012-09-12 22:58:40192const char ExtensionService::kSyncAppSettingsDirectoryName[] =
[email protected]15d5b472011-11-24 04:17:24193 "Sync App Settings";
[email protected]31bb5ee62012-09-12 22:58:40194const char ExtensionService::kSyncExtensionSettingsDirectoryName[] =
[email protected]15d5b472011-11-24 04:17:24195 "Sync Extension Settings";
[email protected]675bb01012012-12-14 22:03:29196const char ExtensionService::kManagedSettingsDirectoryName[] =
197 "Managed Extension Settings";
[email protected]31bb5ee62012-09-12 22:58:40198const char ExtensionService::kStateStoreName[] = "Extension State";
[email protected]a690e292012-12-19 19:22:49199const char ExtensionService::kRulesStoreName[] = "Extension Rules";
[email protected]494c06e2009-07-25 01:06:42200
[email protected]8e4560b62011-01-14 10:09:14201void ExtensionService::CheckExternalUninstall(const std::string& id) {
[email protected]a29a517a2011-01-21 21:11:12202 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20203
[email protected]7a4c6852010-09-16 03:44:22204 // Check if the providers know about this extension.
[email protected]5df038b2012-07-16 19:03:27205 extensions::ProviderCollection::const_iterator i;
[email protected]0a60a2e2010-10-25 16:15:21206 for (i = external_extension_providers_.begin();
207 i != external_extension_providers_.end(); ++i) {
[email protected]8e4560b62011-01-14 10:09:14208 DCHECK(i->get()->IsReady());
[email protected]0a60a2e2010-10-25 16:15:21209 if (i->get()->HasExtension(id))
210 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22211 }
212
[email protected]0f48fca2011-05-19 18:46:35213 // We get the list of external extensions to check from preferences.
214 // It is possible that an extension has preferences but is not loaded.
215 // For example, an extension that requires experimental permissions
216 // will not be loaded if the experimental command line flag is not used.
217 // In this case, do not uninstall.
[email protected]31bb5ee62012-09-12 22:58:40218 if (!GetInstalledExtension(id)) {
[email protected]0f48fca2011-05-19 18:46:35219 // We can't call UninstallExtension with an unloaded/invalid
220 // extension ID.
221 LOG(WARNING) << "Attempted uninstallation of unloaded/invalid extension "
222 << "with id: " << id;
223 return;
224 }
[email protected]d6ebc9792011-04-07 18:18:33225 UninstallExtension(id, true, NULL);
[email protected]7a4c6852010-09-16 03:44:22226}
227
[email protected]7f8f24f2012-11-15 19:40:14228void ExtensionService::SetFileTaskRunnerForTesting(
229 base::SequencedTaskRunner* task_runner) {
230 file_task_runner_ = task_runner;
231}
232
[email protected]8e4560b62011-01-14 10:09:14233void ExtensionService::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22234 external_extension_providers_.clear();
235}
236
[email protected]8e4560b62011-01-14 10:09:14237void ExtensionService::AddProviderForTesting(
[email protected]5df038b2012-07-16 19:03:27238 extensions::ExternalProviderInterface* test_provider) {
[email protected]a29a517a2011-01-21 21:11:12239 CHECK(test_provider);
[email protected]0a60a2e2010-10-25 16:15:21240 external_extension_providers_.push_back(
[email protected]5df038b2012-07-16 19:03:27241 linked_ptr<extensions::ExternalProviderInterface>(test_provider));
[email protected]7a4c6852010-09-16 03:44:22242}
243
[email protected]9060d8b02012-01-13 02:14:30244bool ExtensionService::OnExternalExtensionUpdateUrlFound(
[email protected]7a4c6852010-09-16 03:44:22245 const std::string& id,
[email protected]21a5a672010-11-04 10:47:42246 const GURL& update_url,
[email protected]1d5e58b2013-01-31 08:41:40247 Manifest::Location location) {
[email protected]ab22ba42011-01-14 16:36:38248 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
249 CHECK(Extension::IdIsValid(id));
[email protected]a8af9fdb2010-10-28 21:52:20250
[email protected]8a87a5332011-08-11 17:54:59251 const Extension* extension = GetExtensionById(id, true);
252 if (extension) {
253 // Already installed. Skip this install if the current location has
254 // higher priority than |location|.
[email protected]1d5e58b2013-01-31 08:41:40255 Manifest::Location current = extension->location();
256 if (current == Manifest::GetHigherPriorityLocation(current, location))
[email protected]9060d8b02012-01-13 02:14:30257 return false;
[email protected]8a87a5332011-08-11 17:54:59258 // Otherwise, overwrite the current installation.
[email protected]7a4c6852010-09-16 03:44:22259 }
[email protected]9060d8b02012-01-13 02:14:30260
261 // Add |id| to the set of pending extensions. If it can not be added,
262 // then there is already a pending record from a higher-priority install
263 // source. In this case, signal that this extension will not be
264 // installed by returning false.
265 if (!pending_extension_manager()->AddFromExternalUpdateUrl(
[email protected]31bb5ee62012-09-12 22:58:40266 id, update_url, location)) {
[email protected]9060d8b02012-01-13 02:14:30267 return false;
[email protected]31bb5ee62012-09-12 22:58:40268 }
[email protected]9060d8b02012-01-13 02:14:30269
[email protected]94fde232012-04-27 10:22:30270 update_once_all_providers_are_ready_ = true;
[email protected]9060d8b02012-01-13 02:14:30271 return true;
[email protected]7a4c6852010-09-16 03:44:22272}
273
[email protected]dc9a74f72012-08-17 18:07:21274const Extension* ExtensionService::GetInstalledApp(const GURL& url) const {
[email protected]615d88f2011-12-13 01:47:44275 const Extension* extension = extensions_.GetExtensionOrAppByURL(
276 ExtensionURLInfo(url));
[email protected]dc9a74f72012-08-17 18:07:21277 return (extension && extension->is_app()) ? extension : NULL;
[email protected]d9696672011-03-15 22:45:09278}
279
[email protected]dc9a74f72012-08-17 18:07:21280bool ExtensionService::IsInstalledApp(const GURL& url) const {
[email protected]d9696672011-03-15 22:45:09281 return !!GetInstalledApp(url);
[email protected]ffb204f22010-12-05 23:20:27282}
283
[email protected]404fb002012-10-02 21:06:17284const Extension* ExtensionService::GetIsolatedAppForRenderer(
[email protected]dc9a74f72012-08-17 18:07:21285 int renderer_child_id) const {
[email protected]404fb002012-10-02 21:06:17286 std::set<std::string> extension_ids =
287 process_map_.GetExtensionsInProcess(renderer_child_id);
288 // All apps in one process share the same partition.
289 // It is only possible for the app to have isolated storage
290 // if there is only 1 app in the process.
291 if (extension_ids.size() != 1)
292 return NULL;
293
294 const extensions::Extension* extension =
295 extensions_.GetByID(*(extension_ids.begin()));
[email protected]561e33d52013-04-03 06:58:43296 // We still need to check if the extension has isolated storage,
[email protected]404fb002012-10-02 21:06:17297 // because it's common for there to be one extension in a process
[email protected]561e33d52013-04-03 06:58:43298 // without isolated storage.
299 if (extension &&
300 extensions::AppIsolationInfo::HasIsolatedStorage(extension))
[email protected]404fb002012-10-02 21:06:17301 return extension;
302
303 return NULL;
[email protected]da5683db2011-04-23 17:12:21304}
305
[email protected]6aeac8342010-10-01 20:21:18306// static
[email protected]d6ebc9792011-04-07 18:18:33307// This function is used to implement the command-line switch
[email protected]3bdba0d2011-08-23 07:17:30308// --uninstall-extension, and to uninstall an extension via sync. The LOG
309// statements within this function are used to inform the user if the uninstall
310// cannot be done.
[email protected]eaa7dd182010-12-14 11:09:00311bool ExtensionService::UninstallExtensionHelper(
312 ExtensionService* extensions_service,
[email protected]6aeac8342010-10-01 20:21:18313 const std::string& extension_id) {
[email protected]d6ebc9792011-04-07 18:18:33314 // We can't call UninstallExtension with an invalid extension ID.
[email protected]31bb5ee62012-09-12 22:58:40315 if (!extensions_service->GetInstalledExtension(extension_id)) {
[email protected]6aeac8342010-10-01 20:21:18316 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]95da88c42011-03-31 10:07:33317 << "id: " << extension_id;
318 return false;
[email protected]6aeac8342010-10-01 20:21:18319 }
320
[email protected]d6ebc9792011-04-07 18:18:33321 // The following call to UninstallExtension will not allow an uninstall of a
322 // policy-controlled extension.
[email protected]65187152012-06-02 13:14:14323 string16 error;
[email protected]d6ebc9792011-04-07 18:18:33324 if (!extensions_service->UninstallExtension(extension_id, false, &error)) {
325 LOG(WARNING) << "Cannot uninstall extension with id " << extension_id
326 << ": " << error;
327 return false;
328 }
[email protected]95da88c42011-03-31 10:07:33329
[email protected]6aeac8342010-10-01 20:21:18330 return true;
331}
332
[email protected]eaa7dd182010-12-14 11:09:00333ExtensionService::ExtensionService(Profile* profile,
[email protected]f0841cd2011-01-19 15:07:24334 const CommandLine* command_line,
[email protected]650b2d52013-02-10 03:41:45335 const base::FilePath& install_directory,
[email protected]45759612012-07-10 17:21:23336 extensions::ExtensionPrefs* extension_prefs,
[email protected]fdd679b2012-11-15 20:49:39337 extensions::Blacklist* blacklist,
[email protected]0436b102011-04-15 18:30:03338 bool autoupdate_enabled,
[email protected]4a10006a2013-05-17 23:18:35339 bool extensions_enabled,
340 extensions::OneShotEvent* ready)
[email protected]fdd679b2012-11-15 20:49:39341 : extensions::Blacklist::Observer(blacklist),
342 profile_(profile),
[email protected]bd306722012-07-11 20:43:59343 system_(extensions::ExtensionSystem::Get(profile)),
[email protected]73c47932010-12-06 18:13:43344 extension_prefs_(extension_prefs),
[email protected]695b5712012-12-06 23:55:28345 blacklist_(blacklist),
[email protected]cccdf0aa2011-11-11 03:43:38346 settings_frontend_(extensions::SettingsFrontend::Create(profile)),
[email protected]9c009092013-05-01 03:14:09347 pending_extension_manager_(*this),
[email protected]a9b00ac2009-06-25 21:03:23348 install_directory_(install_directory),
[email protected]0436b102011-04-15 18:30:03349 extensions_enabled_(extensions_enabled),
[email protected]e81dba32009-06-19 20:19:13350 show_extensions_prompts_(true),
[email protected]fc332ae2012-11-14 20:17:33351 install_updates_when_idle_(true),
[email protected]4a10006a2013-05-17 23:18:35352 ready_(ready),
[email protected]9c009092013-05-01 03:14:09353 toolbar_model_(this),
[email protected]8b280302011-10-13 22:22:23354 menu_manager_(profile),
[email protected]7c1490da2011-10-11 18:53:25355 event_routers_initialized_(false),
[email protected]dff1e042012-04-27 10:59:18356 update_once_all_providers_are_ready_(false),
[email protected]3c4abc82012-10-22 22:25:54357 browser_terminating_(false),
[email protected]399583b2012-12-11 09:33:42358 installs_delayed_(false),
[email protected]460c6712013-04-24 07:20:01359 is_first_run_(false),
[email protected]9c009092013-05-01 03:14:09360 app_sync_bundle_(this),
361 extension_sync_bundle_(this) {
[email protected]a29a517a2011-01-21 21:11:12362 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20363
[email protected]36a784c2009-06-23 06:21:08364 // Figure out if extension installation should be enabled.
[email protected]31bb5ee62012-09-12 22:58:40365 if (command_line->HasSwitch(switches::kDisableExtensions) ||
366 profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions)) {
[email protected]6d60703b2009-08-29 01:29:23367 extensions_enabled_ = false;
[email protected]6b75ec32009-08-14 06:37:18368 }
[email protected]36a784c2009-06-23 06:21:08369
[email protected]3c4abc82012-10-22 22:25:54370 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING,
371 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27372 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07373 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27374 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED,
[email protected]ad50def52011-10-19 23:17:07375 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27376 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07377 content::NotificationService::AllBrowserContextsAndSources());
[email protected]0db124b02012-11-07 04:55:05378 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
379 content::NotificationService::AllBrowserContextsAndSources());
[email protected]92dd8d92013-02-26 00:41:08380 registrar_.Add(this, chrome::NOTIFICATION_UPGRADE_RECOMMENDED,
381 content::NotificationService::AllBrowserContextsAndSources());
[email protected]2fb7dc982010-09-29 12:24:28382 pref_change_registrar_.Init(profile->GetPrefs());
[email protected]90bb38d2012-11-14 18:36:03383 base::Closure callback =
384 base::Bind(&ExtensionService::OnExtensionInstallPrefChanged,
385 base::Unretained(this));
386 pref_change_registrar_.Add(prefs::kExtensionInstallAllowList, callback);
387 pref_change_registrar_.Add(prefs::kExtensionInstallDenyList, callback);
[email protected]e410b5f2012-12-14 14:02:24388 pref_change_registrar_.Add(prefs::kExtensionAllowedTypes, callback);
[email protected]4814b512009-11-07 00:12:29389
[email protected]93fd78f42009-07-10 16:43:17390 // Set up the ExtensionUpdater
391 if (autoupdate_enabled) {
392 int update_frequency = kDefaultUpdateFrequencySeconds;
393 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25394 base::StringToInt(command_line->GetSwitchValueASCII(
395 switches::kExtensionsUpdateFrequency),
396 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17397 }
[email protected]42a08162012-03-16 18:09:11398 updater_.reset(new extensions::ExtensionUpdater(this,
399 extension_prefs,
400 profile->GetPrefs(),
401 profile,
[email protected]fdd679b2012-11-15 20:49:39402 blacklist,
[email protected]42a08162012-03-16 18:09:11403 update_frequency));
[email protected]93fd78f42009-07-10 16:43:17404 }
405
[email protected]25ae0152011-11-18 14:40:02406 component_loader_.reset(
407 new extensions::ComponentLoader(this,
408 profile->GetPrefs(),
409 g_browser_process->local_state()));
[email protected]8e4560b62011-01-14 10:09:14410
[email protected]0436b102011-04-15 18:30:03411 if (extensions_enabled_) {
[email protected]ae4c37e2012-12-21 01:16:25412 extensions::ExternalProviderImpl::CreateExternalProviders(
413 this, profile_, &external_extension_providers_);
[email protected]873531342011-03-09 12:16:05414 }
[email protected]b671760b2010-07-15 21:13:47415
[email protected]fb82dcd2012-03-21 14:15:46416 // Set this as the ExtensionService for extension sorting to ensure it
417 // cause syncs if required.
418 extension_prefs_->extension_sorting()->SetExtensionService(this);
419
[email protected]b3aa7182013-04-25 04:45:23420 is_first_run_ = !extension_prefs_->SetAlertSystemFirstRun();
421
[email protected]9ce11d22012-08-08 23:20:13422#if defined(ENABLE_EXTENSIONS)
[email protected]c77f2352012-08-08 22:07:58423 extension_action_storage_manager_.reset(
424 new extensions::ExtensionActionStorageManager(profile_));
[email protected]9ce11d22012-08-08 23:20:13425#endif
[email protected]c77f2352012-08-08 22:07:58426
[email protected]cb0e50312011-05-09 15:03:07427 // How long is the path to the Extensions directory?
428 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ExtensionRootPathLength",
429 install_directory_.value().length(), 0, 500, 100);
[email protected]6014d672008-12-05 00:38:25430}
431
[email protected]84df8332011-12-06 18:22:46432const ExtensionSet* ExtensionService::extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45433 return &extensions_;
434}
435
[email protected]84df8332011-12-06 18:22:46436const ExtensionSet* ExtensionService::disabled_extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45437 return &disabled_extensions_;
438}
439
[email protected]84df8332011-12-06 18:22:46440const ExtensionSet* ExtensionService::terminated_extensions() const {
[email protected]bb7f40952011-01-13 00:21:20441 return &terminated_extensions_;
442}
443
[email protected]695b5712012-12-06 23:55:28444const ExtensionSet* ExtensionService::blacklisted_extensions() const {
445 return &blacklisted_extensions_;
446}
447
448scoped_ptr<const ExtensionSet>
449 ExtensionService::GenerateInstalledExtensionsSet() const {
450 scoped_ptr<ExtensionSet> installed_extensions(new ExtensionSet());
[email protected]7f4308d2012-01-18 07:43:01451 installed_extensions->InsertAll(extensions_);
452 installed_extensions->InsertAll(disabled_extensions_);
453 installed_extensions->InsertAll(terminated_extensions_);
[email protected]695b5712012-12-06 23:55:28454 installed_extensions->InsertAll(blacklisted_extensions_);
455 return installed_extensions.PassAs<const ExtensionSet>();
[email protected]7f4308d2012-01-18 07:43:01456}
457
[email protected]3f213ad2012-07-26 23:39:41458extensions::PendingExtensionManager*
459 ExtensionService::pending_extension_manager() {
[email protected]b2907fd2011-03-25 16:43:37460 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45461}
462
[email protected]eaa7dd182010-12-14 11:09:00463ExtensionService::~ExtensionService() {
[email protected]c8d407e2011-04-28 21:27:17464 // No need to unload extensions here because they are profile-scoped, and the
465 // profile is in the process of being deleted.
[email protected]94b4ffc2011-02-04 05:55:17466
[email protected]5df038b2012-07-16 19:03:27467 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:14468 for (i = external_extension_providers_.begin();
469 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:27470 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:14471 provider->ServiceShutdown();
472 }
[email protected]6014d672008-12-05 00:38:25473}
474
[email protected]d91268022011-08-26 13:17:37475void ExtensionService::InitEventRoutersAfterImport() {
[email protected]cf593af2011-12-30 05:44:39476 RegisterForImportFinished();
477}
478
479void ExtensionService::RegisterForImportFinished() {
480 if (!registrar_.IsRegistered(this, chrome::NOTIFICATION_IMPORT_FINISHED,
481 content::Source<Profile>(profile_))) {
482 registrar_.Add(this, chrome::NOTIFICATION_IMPORT_FINISHED,
483 content::Source<Profile>(profile_));
484 }
485}
486
487void ExtensionService::InitAfterImport() {
[email protected]21f2e2d2013-01-23 22:59:52488 startup_metric_utils::ScopedSlowStartupUMA
489 scoped_timer("Startup.SlowStartupExtensionServiceInitAfterImport");
[email protected]eac88332012-12-26 17:57:45490 component_loader_->LoadAll();
[email protected]ae4c37e2012-12-21 01:16:25491
[email protected]cf593af2011-12-30 05:44:39492 CheckForExternalUpdates();
493
494 GarbageCollectExtensions();
495
496 // Idempotent, so although there is a possible race if the import
497 // process finished sometime in the middle of ProfileImpl::InitExtensions,
498 // it cannot happen twice.
499 InitEventRouters();
[email protected]d91268022011-08-26 13:17:37500}
501
[email protected]eaa7dd182010-12-14 11:09:00502void ExtensionService::InitEventRouters() {
[email protected]b3d62312b12010-10-14 21:10:18503 if (event_routers_initialized_)
504 return;
505
[email protected]6a3cd37e2012-04-17 17:13:34506#if defined(ENABLE_EXTENSIONS)
[email protected]5a38dfd2012-07-23 23:22:10507 browser_event_router_.reset(new extensions::BrowserEventRouter(profile_));
[email protected]6a3cd37e2012-04-17 17:13:34508#endif // defined(ENABLE_EXTENSIONS)
[email protected]b3d62312b12010-10-14 21:10:18509 event_routers_initialized_ = true;
[email protected]c5ae74ab2010-04-15 18:14:37510}
511
[email protected]037228a2012-10-05 01:36:16512void ExtensionService::Shutdown() {
513 // Do nothing for now.
514}
515
[email protected]b2907fd2011-03-25 16:43:37516const Extension* ExtensionService::GetExtensionById(
517 const std::string& id, bool include_disabled) const {
[email protected]81f0d162012-08-15 04:21:47518 int include_mask = INCLUDE_ENABLED;
[email protected]695b5712012-12-06 23:55:28519 if (include_disabled) {
520 // Include blacklisted extensions here because there are hundreds of
521 // callers of this function, and many might assume that this includes those
522 // that have been disabled due to blacklisting.
523 include_mask |= INCLUDE_DISABLED | INCLUDE_BLACKLISTED;
524 }
525 return GetExtensionById(id, include_mask);
526}
527
[email protected]399583b2012-12-11 09:33:42528GURL ExtensionService::GetSiteForExtensionId(const std::string& extension_id) {
529 return content::SiteInstance::GetSiteForURL(
530 profile_,
531 Extension::GetBaseURLFromExtensionId(extension_id));
532}
533
[email protected]695b5712012-12-06 23:55:28534const Extension* ExtensionService::GetExtensionById(
535 const std::string& id, int include_mask) const {
536 std::string lowercase_id = StringToLowerASCII(id);
537 if (include_mask & INCLUDE_ENABLED) {
538 const Extension* extension = extensions_.GetByID(lowercase_id);
539 if (extension)
540 return extension;
541 }
542 if (include_mask & INCLUDE_DISABLED) {
543 const Extension* extension = disabled_extensions_.GetByID(lowercase_id);
544 if (extension)
545 return extension;
546 }
547 if (include_mask & INCLUDE_TERMINATED) {
548 const Extension* extension = terminated_extensions_.GetByID(lowercase_id);
549 if (extension)
550 return extension;
551 }
552 if (include_mask & INCLUDE_BLACKLISTED) {
553 const Extension* extension = blacklisted_extensions_.GetByID(lowercase_id);
554 if (extension)
555 return extension;
556 }
557 return NULL;
[email protected]78994ab02010-12-08 18:06:44558}
559
[email protected]eaa7dd182010-12-14 11:09:00560void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12561 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20562
[email protected]4a10006a2013-05-17 23:18:35563 DCHECK(!is_ready()); // Can't redo init.
[email protected]93fd78f42009-07-10 16:43:17564 DCHECK_EQ(extensions_.size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32565
[email protected]820d9bd2013-04-03 03:46:03566 const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
[email protected]820d9bd2013-04-03 03:46:03567 if (cmd_line->HasSwitch(switches::kInstallFromWebstore) ||
568 cmd_line->HasSwitch(switches::kLimitedInstallFromWebstore)) {
569 // The sole purpose of this launch is to install a new extension from CWS
570 // and immediately terminate: loading already installed extensions is
571 // unnecessary and may interfere with the inline install dialog (e.g. if an
572 // extension listens to onStartup and opens a window).
573 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25574 } else {
[email protected]820d9bd2013-04-03 03:46:03575 // TODO(mek): It might be cleaner to do the FinishDelayedInstallInfo stuff
576 // here instead of in installedloader.
577 if (g_browser_process->profile_manager() &&
578 g_browser_process->profile_manager()->will_import()) {
579 // Do not load any component extensions, since they may conflict with the
580 // import process.
[email protected]ae4c37e2012-12-21 01:16:25581
[email protected]820d9bd2013-04-03 03:46:03582 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]4a10006a2013-05-17 23:18:35583 SetReadyAndNotifyListeners();
[email protected]820d9bd2013-04-03 03:46:03584 RegisterForImportFinished();
585 } else {
[email protected]4a10006a2013-05-17 23:18:35586 // In this case, LoadAllExtensions() calls OnLoadedInstalledExtensions().
[email protected]820d9bd2013-04-03 03:46:03587 component_loader_->LoadAll();
588 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]4a10006a2013-05-17 23:18:35589 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25590
[email protected]820d9bd2013-04-03 03:46:03591 // TODO(erikkay) this should probably be deferred to a future point
592 // rather than running immediately at startup.
593 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57594
[email protected]820d9bd2013-04-03 03:46:03595 // TODO(erikkay) this should probably be deferred as well.
596 GarbageCollectExtensions();
597 }
[email protected]215a7be2012-10-22 19:53:42598
[email protected]820d9bd2013-04-03 03:46:03599 if (extension_prefs_->NeedsStorageGarbageCollection()) {
600 GarbageCollectIsolatedStorage();
601 extension_prefs_->SetNeedsStorageGarbageCollection(false);
602 }
[email protected]399583b2012-12-11 09:33:42603 }
[email protected]6014d672008-12-05 00:38:25604}
605
[email protected]31bb5ee62012-09-12 22:58:40606bool ExtensionService::UpdateExtension(const std::string& id,
[email protected]650b2d52013-02-10 03:41:45607 const base::FilePath& extension_path,
[email protected]31bb5ee62012-09-12 22:58:40608 const GURL& download_url,
609 CrxInstaller** out_crx_installer) {
[email protected]a29a517a2011-01-21 21:11:12610 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]3c4abc82012-10-22 22:25:54611 if (browser_terminating_) {
612 LOG(WARNING) << "Skipping UpdateExtension due to browser shutdown";
613 // Leak the temp file at extension_path. We don't want to add to the disk
614 // I/O burden at shutdown, we can't rely on the I/O completing anyway, and
615 // the file is in the OS temp directory which should be cleaned up for us.
616 return false;
617 }
[email protected]a8af9fdb2010-10-28 21:52:20618
[email protected]3f213ad2012-07-26 23:39:41619 const extensions::PendingExtensionInfo* pending_extension_info =
[email protected]51a3bf8b2012-06-08 22:53:06620 pending_extension_manager()->GetById(id);
[email protected]8ef78fd2010-08-19 17:14:32621
[email protected]695b5712012-12-06 23:55:28622 const Extension* extension = GetInstalledExtension(id);
[email protected]51a3bf8b2012-06-08 22:53:06623 if (!pending_extension_info && !extension) {
[email protected]aa142702010-03-26 01:26:33624 LOG(WARNING) << "Will not update extension " << id
625 << " because it is not installed or pending";
626 // Delete extension_path since we're not creating a CrxInstaller
627 // that would do it for us.
[email protected]7f8f24f2012-11-15 19:40:14628 if (!GetFileTaskRunner()->PostTask(
629 FROM_HERE,
[email protected]53612e82011-10-18 18:00:36630 base::Bind(
[email protected]7296f2762011-11-21 19:23:44631 &extension_file_util::DeleteFile, extension_path, false)))
[email protected]14908b72011-04-20 06:54:36632 NOTREACHED();
[email protected]420a0ec2011-06-01 01:07:03633
634 return false;
[email protected]e957fe52009-06-23 16:51:05635 }
636
[email protected]aa142702010-03-26 01:26:33637 // We want a silent install only for non-pending extensions and
638 // pending extensions that have install_silently set.
[email protected]91e51d612012-10-21 23:03:05639 ExtensionInstallPrompt* client = NULL;
640 if (pending_extension_info && !pending_extension_info->install_silently())
641 client = ExtensionInstallUI::CreateInstallPromptWithProfile(profile_);
[email protected]aa142702010-03-26 01:26:33642
[email protected]d8c8f25f2011-11-02 18:18:01643 scoped_refptr<CrxInstaller> installer(CrxInstaller::Create(this, client));
[email protected]6dfbbf82010-03-12 23:09:16644 installer->set_expected_id(id);
[email protected]51a3bf8b2012-06-08 22:53:06645 if (pending_extension_info) {
646 installer->set_install_source(pending_extension_info->install_source());
647 if (pending_extension_info->install_silently())
648 installer->set_allow_silent_install(true);
649 } else if (extension) {
[email protected]5eb375e92010-11-26 07:50:41650 installer->set_install_source(extension->location());
[email protected]51a3bf8b2012-06-08 22:53:06651 }
[email protected]7d8b7072012-04-07 01:07:46652 // If the extension was installed from or has migrated to the webstore, or
[email protected]fcb2c2c2012-10-17 21:08:45653 // its auto-update URL is from the webstore, treat it as a webstore install.
654 // Note that we ignore some older extensions with blank auto-update URLs
655 // because we are mostly concerned with restrictions on NaCl extensions,
656 // which are newer.
[email protected]a12ce8b22012-01-17 18:40:53657 int creation_flags = Extension::NO_FLAGS;
[email protected]75764512011-12-19 19:54:28658 if ((extension && extension->from_webstore()) ||
[email protected]7d8b7072012-04-07 01:07:46659 (extension && extension->UpdatesFromGallery()) ||
[email protected]963d13c2012-09-29 17:13:35660 (!extension && extension_urls::IsWebstoreUpdateUrl(
[email protected]51a3bf8b2012-06-08 22:53:06661 pending_extension_info->update_url()))) {
[email protected]a12ce8b22012-01-17 18:40:53662 creation_flags |= Extension::FROM_WEBSTORE;
663 }
664
665 // Bookmark apps being updated is kind of a contradiction, but that's because
666 // we mark the default apps as bookmark apps, and they're hosted in the web
667 // store, thus they can get updated. See http://crbug.com/101605 for more
668 // details.
669 if (extension && extension->from_bookmark())
670 creation_flags |= Extension::FROM_BOOKMARK;
671
[email protected]e33bbc22012-08-27 22:05:46672 if (extension && extension->was_installed_by_default())
673 creation_flags |= Extension::WAS_INSTALLED_BY_DEFAULT;
674
[email protected]a12ce8b22012-01-17 18:40:53675 installer->set_creation_flags(creation_flags);
676
[email protected]6dfbbf82010-03-12 23:09:16677 installer->set_delete_source(true);
[email protected]655b2b1a2011-10-13 17:13:06678 installer->set_download_url(download_url);
[email protected]cb0e50312011-05-09 15:03:07679 installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
[email protected]6dfbbf82010-03-12 23:09:16680 installer->InstallCrx(extension_path);
[email protected]420a0ec2011-06-01 01:07:03681
682 if (out_crx_installer)
683 *out_crx_installer = installer;
684
685 return true;
[email protected]e957fe52009-06-23 16:51:05686}
687
[email protected]eaa7dd182010-12-14 11:09:00688void ExtensionService::ReloadExtension(const std::string& extension_id) {
[email protected]32b36c62012-10-24 05:37:41689 int events = HasShellWindows(extension_id) ? EVENT_LAUNCHED : EVENT_NONE;
690 ReloadExtensionWithEvents(extension_id, events);
691}
692
693void ExtensionService::RestartExtension(const std::string& extension_id) {
[email protected]2380c6b2013-01-09 02:33:13694 ReloadExtensionWithEvents(extension_id, EVENT_RESTARTED);
[email protected]32b36c62012-10-24 05:37:41695}
696
697void ExtensionService::ReloadExtensionWithEvents(
698 const std::string& extension_id,
699 int events) {
[email protected]a29a517a2011-01-21 21:11:12700 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]2a947202013-03-06 04:58:05701
702 // If the extension is already reloading, don't reload again.
703 if (extension_prefs_->GetDisableReasons(extension_id) &
704 Extension::DISABLE_RELOAD) {
705 return;
706 }
707
[email protected]650b2d52013-02-10 03:41:45708 base::FilePath path;
[email protected]9adb9692010-10-29 23:14:02709 const Extension* current_extension = GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40710
[email protected]f17dbd42010-08-16 23:21:10711 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]b65272f2009-08-31 15:47:06712 if (current_extension) {
[email protected]4814b512009-11-07 00:12:29713 // If the extension has an inspector open for its background page, detach
714 // the inspector and hang onto a cookie for it, so that we can reattach
715 // later.
[email protected]31d8f5f22012-04-02 15:22:08716 // TODO(yoz): this is not incognito-safe!
717 ExtensionProcessManager* manager = system_->process_manager();
[email protected]3a1dc572012-07-31 22:25:13718 extensions::ExtensionHost* host =
719 manager->GetBackgroundHostForExtension(extension_id);
[email protected]98f66112012-12-25 12:59:36720 if (host && DevToolsAgentHost::HasFor(host->render_view_host())) {
[email protected]4814b512009-11-07 00:12:29721 // Look for an open inspector for the background page.
[email protected]ead056352013-03-12 08:47:54722 std::string devtools_cookie = DevToolsAgentHost::DisconnectRenderViewHost(
[email protected]562e20c2012-12-24 18:27:20723 host->render_view_host());
[email protected]ead056352013-03-12 08:47:54724 if (devtools_cookie != std::string())
[email protected]4814b512009-11-07 00:12:29725 orphaned_dev_tools_[extension_id] = devtools_cookie;
726 }
727
[email protected]b65272f2009-08-31 15:47:06728 path = current_extension->path();
[email protected]44d62b62012-04-11 00:06:03729 DisableExtension(extension_id, Extension::DISABLE_RELOAD);
[email protected]b914e2952013-04-26 07:10:03730 reloading_extensions_.insert(extension_id);
[email protected]1eb175082010-02-10 09:26:16731 } else {
732 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06733 }
734
[email protected]8b6d9642013-02-01 03:37:40735 on_load_events_[extension_id] = events;
[email protected]8b6d9642013-02-01 03:37:40736
[email protected]399583b2012-12-11 09:33:42737 if (delayed_updates_for_idle_.Contains(extension_id)) {
[email protected]6f6101832012-11-27 22:10:48738 FinishDelayedInstallation(extension_id);
[email protected]0db124b02012-11-07 04:55:05739 return;
740 }
741
[email protected]43ceb002012-02-10 23:19:15742 // If we're reloading a component extension, use the component extension
743 // loader's reloader.
744 if (component_loader_->Exists(extension_id)) {
745 component_loader_->Reload(extension_id);
746 return;
747 }
748
[email protected]e6090e42010-03-23 22:44:08749 // Check the installed extensions to see if what we're reloading was already
750 // installed.
751 scoped_ptr<ExtensionInfo> installed_extension(
752 extension_prefs_->GetInstalledExtensionInfo(extension_id));
753 if (installed_extension.get() &&
754 installed_extension->extension_manifest.get()) {
[email protected]d8c8f25f2011-11-02 18:18:01755 extensions::InstalledLoader(this).Load(*installed_extension, false);
[email protected]e6090e42010-03-23 22:44:08756 } else {
[email protected]d8c8f25f2011-11-02 18:18:01757 // Otherwise, the extension is unpacked (location LOAD).
[email protected]e6090e42010-03-23 22:44:08758 // We should always be able to remember the extension's path. If it's not in
759 // the map, someone failed to update |unloaded_extension_paths_|.
760 CHECK(!path.empty());
[email protected]d8c8f25f2011-11-02 18:18:01761 extensions::UnpackedInstaller::Create(this)->Load(path);
[email protected]e6090e42010-03-23 22:44:08762 }
[email protected]9cddd4702009-07-27 22:09:40763}
764
[email protected]fa2416f2011-05-03 08:41:20765bool ExtensionService::UninstallExtension(
[email protected]a12c706e2011-12-01 00:58:08766 std::string extension_id,
[email protected]fa2416f2011-05-03 08:41:20767 bool external_uninstall,
[email protected]65187152012-06-02 13:14:14768 string16* error) {
[email protected]a29a517a2011-01-21 21:11:12769 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20770
[email protected]0d6ec3a72011-09-02 02:09:43771 scoped_refptr<const Extension> extension(GetInstalledExtension(extension_id));
[email protected]631cf822009-05-15 07:01:25772
[email protected]e7afe2452010-08-22 16:19:13773 // Callers should not send us nonexistent extensions.
[email protected]aebe23a32010-12-10 22:15:48774 CHECK(extension);
[email protected]9f1087e2009-06-15 17:29:32775
[email protected]95da88c42011-03-31 10:07:33776 // Policy change which triggers an uninstall will always set
777 // |external_uninstall| to true so this is the only way to uninstall
778 // managed extensions.
[email protected]65187152012-06-02 13:14:14779 if (!external_uninstall &&
780 !system_->management_policy()->UserMayModifySettings(
781 extension.get(), error)) {
[email protected]ad50def52011-10-19 23:17:07782 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27783 chrome::NOTIFICATION_EXTENSION_UNINSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:53784 content::Source<Profile>(profile_),
785 content::Details<const Extension>(extension));
[email protected]d6ebc9792011-04-07 18:18:33786 return false;
787 }
[email protected]95da88c42011-03-31 10:07:33788
[email protected]3bdba0d2011-08-23 07:17:30789 // Extract the data we need for sync now, but don't actually sync until we've
790 // completed the uninstallation.
[email protected]65f173552012-06-28 22:43:58791 syncer::SyncChange sync_change;
[email protected]5db9ada2012-04-11 13:48:20792 if (app_sync_bundle_.HandlesApp(*extension)) {
793 sync_change = app_sync_bundle_.CreateSyncChangeToDelete(extension);
794 } else if (extension_sync_bundle_.HandlesExtension(*extension)) {
795 sync_change = extension_sync_bundle_.CreateSyncChangeToDelete(extension);
[email protected]3bdba0d2011-08-23 07:17:30796 }
797
[email protected]d96eb512012-11-01 23:44:08798 if (IsUnacknowledgedExternalExtension(extension)) {
799 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
800 EXTERNAL_EXTENSION_UNINSTALLED,
801 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
802 }
[email protected]9b217652010-10-08 22:04:23803 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08804 extension->GetType(), 100);
[email protected]fe2dd7742011-04-19 22:52:49805 RecordPermissionMessagesHistogram(
806 extension, "Extensions.Permissions_Uninstall");
[email protected]9b217652010-10-08 22:04:23807
[email protected]831aa212010-03-26 13:55:19808 // Unload before doing more cleanup to ensure that nothing is hanging on to
809 // any of these resources.
[email protected]814a7bf0f2011-08-13 05:30:59810 UnloadExtension(extension_id, extension_misc::UNLOAD_REASON_UNINSTALL);
[email protected]831aa212010-03-26 13:55:19811
[email protected]0d6ec3a72011-09-02 02:09:43812 extension_prefs_->OnExtensionUninstalled(extension_id, extension->location(),
[email protected]831aa212010-03-26 13:55:19813 external_uninstall);
[email protected]9f1087e2009-06-15 17:29:32814
815 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]12075d12013-02-27 05:38:05816 if (!Manifest::IsUnpackedLocation(extension->location())) {
[email protected]7f8f24f2012-11-15 19:40:14817 if (!GetFileTaskRunner()->PostTask(
818 FROM_HERE,
[email protected]53612e82011-10-18 18:00:36819 base::Bind(
[email protected]14908b72011-04-20 06:54:36820 &extension_file_util::UninstallExtension,
821 install_directory_,
[email protected]fa2416f2011-05-03 08:41:20822 extension_id)))
[email protected]14908b72011-04-20 06:54:36823 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32824 }
825
[email protected]0d6ec3a72011-09-02 02:09:43826 GURL launch_web_url_origin(extension->launch_web_url());
827 launch_web_url_origin = launch_web_url_origin.GetOrigin();
[email protected]561e33d52013-04-03 06:58:43828 bool is_storage_isolated =
829 extensions::AppIsolationInfo::HasIsolatedStorage(extension);
[email protected]0d6ec3a72011-09-02 02:09:43830
[email protected]399583b2012-12-11 09:33:42831 if (is_storage_isolated) {
832 BrowserContext::AsyncObliterateStoragePartition(
833 profile_,
834 GetSiteForExtensionId(extension_id),
835 base::Bind(&ExtensionService::OnNeedsToGarbageCollectIsolatedStorage,
836 AsWeakPtr()));
837 } else {
838 if (extension->is_hosted_app() &&
839 !profile_->GetExtensionSpecialStoragePolicy()->
840 IsStorageProtected(launch_web_url_origin)) {
841 extensions::DataDeleter::StartDeleting(
842 profile_, extension_id, launch_web_url_origin);
843 }
844 extensions::DataDeleter::StartDeleting(profile_, extension_id,
845 extension->url());
[email protected]0d6ec3a72011-09-02 02:09:43846 }
[email protected]0d6ec3a72011-09-02 02:09:43847
[email protected]0dfe05c2011-02-23 23:03:36848 UntrackTerminatedExtension(extension_id);
[email protected]211030342010-09-30 18:41:06849
850 // Notify interested parties that we've uninstalled this extension.
[email protected]ad50def52011-10-19 23:17:07851 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27852 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
[email protected]6c2381d2011-10-19 02:52:53853 content::Source<Profile>(profile_),
[email protected]d67d9412012-07-17 17:19:42854 content::Details<const Extension>(extension));
[email protected]d6ebc9792011-04-07 18:18:33855
[email protected]5db9ada2012-04-11 13:48:20856 if (app_sync_bundle_.HasExtensionId(extension_id) &&
[email protected]a4a147652012-07-03 23:41:32857 sync_change.sync_data().GetDataType() == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:20858 app_sync_bundle_.ProcessDeletion(extension_id, sync_change);
859 } else if (extension_sync_bundle_.HasExtensionId(extension_id) &&
[email protected]a4a147652012-07-03 23:41:32860 sync_change.sync_data().GetDataType() == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:20861 extension_sync_bundle_.ProcessDeletion(extension_id, sync_change);
[email protected]3bdba0d2011-08-23 07:17:30862 }
863
[email protected]399583b2012-12-11 09:33:42864 delayed_updates_for_idle_.Remove(extension_id);
865 delayed_installs_.Remove(extension_id);
[email protected]0db124b02012-11-07 04:55:05866
[email protected]333b1de2011-09-12 18:28:50867 // Track the uninstallation.
[email protected]49098f702011-10-13 03:47:18868 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionUninstalled", 1, 2);
869
[email protected]d6ebc9792011-04-07 18:18:33870 return true;
[email protected]c10da4b02010-03-25 14:38:32871}
872
[email protected]c3cfb012011-04-06 22:07:35873bool ExtensionService::IsExtensionEnabled(
874 const std::string& extension_id) const {
[email protected]84df8332011-12-06 18:22:46875 if (extensions_.Contains(extension_id) ||
[email protected]dc9a74f72012-08-17 18:07:21876 terminated_extensions_.Contains(extension_id)) {
[email protected]36429da2011-07-11 20:25:18877 return true;
[email protected]dc9a74f72012-08-17 18:07:21878 }
[email protected]36429da2011-07-11 20:25:18879
[email protected]695b5712012-12-06 23:55:28880 if (disabled_extensions_.Contains(extension_id) ||
881 blacklisted_extensions_.Contains(extension_id)) {
[email protected]ad83ca242011-07-29 01:32:25882 return false;
[email protected]695b5712012-12-06 23:55:28883 }
[email protected]ad83ca242011-07-29 01:32:25884
885 // If the extension hasn't been loaded yet, check the prefs for it. Assume
886 // enabled unless otherwise noted.
887 return !extension_prefs_->IsExtensionDisabled(extension_id) &&
[email protected]f574c402012-12-04 23:20:31888 !extension_prefs_->IsExternalExtensionUninstalled(extension_id);
[email protected]c3cfb012011-04-06 22:07:35889}
890
891bool ExtensionService::IsExternalExtensionUninstalled(
892 const std::string& extension_id) const {
893 return extension_prefs_->IsExternalExtensionUninstalled(extension_id);
894}
895
[email protected]e2377032013-01-18 17:53:22896bool ExtensionService::IsExtensionEnabledForLauncher(
897 const std::string& extension_id) const {
898 return IsExtensionEnabled(extension_id) &&
899 !GetTerminatedExtension(extension_id);
900}
901
[email protected]eaa7dd182010-12-14 11:09:00902void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12903 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20904
[email protected]06f92562011-04-29 19:27:31905 if (IsExtensionEnabled(extension_id))
[email protected]0c6da502009-08-14 22:32:39906 return;
[email protected]0c6da502009-08-14 22:32:39907
[email protected]b3317ad2011-04-28 23:46:00908 extension_prefs_->SetExtensionState(extension_id, Extension::ENABLED);
[email protected]eb5e4f92012-08-15 23:33:28909 extension_prefs_->ClearDisableReasons(extension_id);
[email protected]1784e83a2009-09-08 21:01:52910
[email protected]695b5712012-12-06 23:55:28911 const Extension* extension = disabled_extensions_.GetByID(extension_id);
[email protected]06f92562011-04-29 19:27:31912 // This can happen if sync enables an extension that is not
913 // installed yet.
914 if (!extension)
915 return;
916
[email protected]d96eb512012-11-01 23:44:08917 if (IsUnacknowledgedExternalExtension(extension)) {
918 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
919 EXTERNAL_EXTENSION_REENABLED,
920 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]612a1cb12012-10-17 13:18:03921 AcknowledgeExternalExtension(extension->id());
[email protected]d96eb512012-11-01 23:44:08922 }
[email protected]612a1cb12012-10-17 13:18:03923
[email protected]0c6da502009-08-14 22:32:39924 // Move it over to the enabled list.
[email protected]84df8332011-12-06 18:22:46925 extensions_.Insert(make_scoped_refptr(extension));
926 disabled_extensions_.Remove(extension->id());
[email protected]0c6da502009-08-14 22:32:39927
[email protected]62d30f42009-10-01 22:36:06928 NotifyExtensionLoaded(extension);
[email protected]3bdba0d2011-08-23 07:17:30929
[email protected]f74d7f32012-06-19 19:22:51930 // Notify listeners that the extension was enabled.
931 content::NotificationService::current()->Notify(
932 chrome::NOTIFICATION_EXTENSION_ENABLED,
933 content::Source<Profile>(profile_),
934 content::Details<const Extension>(extension));
935
[email protected]3bdba0d2011-08-23 07:17:30936 SyncExtensionChangeIfNeeded(*extension);
[email protected]0c6da502009-08-14 22:32:39937}
938
[email protected]44d62b62012-04-11 00:06:03939void ExtensionService::DisableExtension(
940 const std::string& extension_id,
941 Extension::DisableReason disable_reason) {
[email protected]a29a517a2011-01-21 21:11:12942 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20943
[email protected]b2ba9962009-12-10 20:10:15944 // The extension may have been disabled already.
[email protected]06f92562011-04-29 19:27:31945 if (!IsExtensionEnabled(extension_id))
[email protected]1784e83a2009-09-08 21:01:52946 return;
[email protected]1784e83a2009-09-08 21:01:52947
[email protected]06f92562011-04-29 19:27:31948 const Extension* extension = GetInstalledExtension(extension_id);
949 // |extension| can be NULL if sync disables an extension that is not
950 // installed yet.
[email protected]65187152012-06-02 13:14:14951 if (extension &&
952 !system_->management_policy()->UserMayModifySettings(extension, NULL)) {
[email protected]95da88c42011-03-31 10:07:33953 return;
[email protected]65187152012-06-02 13:14:14954 }
[email protected]95da88c42011-03-31 10:07:33955
[email protected]b3317ad2011-04-28 23:46:00956 extension_prefs_->SetExtensionState(extension_id, Extension::DISABLED);
[email protected]eb5e4f92012-08-15 23:33:28957 extension_prefs_->AddDisableReason(extension_id, disable_reason);
[email protected]1784e83a2009-09-08 21:01:52958
[email protected]695b5712012-12-06 23:55:28959 int include_mask = INCLUDE_EVERYTHING & ~INCLUDE_DISABLED;
960 extension = GetExtensionById(extension_id, include_mask);
[email protected]06f92562011-04-29 19:27:31961 if (!extension)
962 return;
963
[email protected]64742de32013-04-22 08:44:34964 // Reset the background_page_ready flag
965 if (extensions::BackgroundInfo::HasBackgroundPage(extension))
966 extension_runtime_data_[extension->id()].background_page_ready = false;
967
[email protected]5c094792012-09-07 19:44:53968 // Move it over to the disabled list. Don't send a second unload notification
969 // for terminated extensions being disabled.
[email protected]84df8332011-12-06 18:22:46970 disabled_extensions_.Insert(make_scoped_refptr(extension));
[email protected]5c094792012-09-07 19:44:53971 if (extensions_.Contains(extension->id())) {
[email protected]84df8332011-12-06 18:22:46972 extensions_.Remove(extension->id());
[email protected]5c094792012-09-07 19:44:53973 NotifyExtensionUnloaded(extension, extension_misc::UNLOAD_REASON_DISABLE);
974 } else {
[email protected]84df8332011-12-06 18:22:46975 terminated_extensions_.Remove(extension->id());
[email protected]5c094792012-09-07 19:44:53976 }
[email protected]3bdba0d2011-08-23 07:17:30977
978 SyncExtensionChangeIfNeeded(*extension);
[email protected]1784e83a2009-09-08 21:01:52979}
980
[email protected]eaa7dd182010-12-14 11:09:00981void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]009633c2013-03-07 22:08:28982 const Extension* extension) {
983 GrantPermissions(extension);
[email protected]fe2dd7742011-04-19 22:52:49984 RecordPermissionMessagesHistogram(
985 extension, "Extensions.Permissions_ReEnable");
[email protected]8d888c12010-11-30 00:00:25986 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
987 EnableExtension(extension->id());
988}
989
[email protected]009633c2013-03-07 22:08:28990void ExtensionService::GrantPermissions(const Extension* extension) {
[email protected]be083862012-09-01 03:53:45991 CHECK(extension);
992 extensions::PermissionsUpdater perms_updater(profile());
[email protected]009633c2013-03-07 22:08:28993 perms_updater.GrantActivePermissions(extension);
[email protected]be083862012-09-01 03:53:45994}
995
[email protected]fe2dd7742011-04-19 22:52:49996// static
997void ExtensionService::RecordPermissionMessagesHistogram(
[email protected]13c68b62013-05-17 11:29:05998 const Extension* extension, const char* histogram) {
[email protected]de415552013-01-23 04:12:17999 // Since this is called from multiple sources, and since the histogram macros
1000 // use statics, we need to manually lookup the histogram ourselves.
1001 base::HistogramBase* counter = base::LinearHistogram::FactoryGet(
[email protected]fe2dd7742011-04-19 22:52:491002 histogram,
1003 1,
[email protected]c2e66e12012-06-27 06:27:061004 PermissionMessage::kEnumBoundary,
1005 PermissionMessage::kEnumBoundary + 1,
[email protected]de415552013-01-23 04:12:171006 base::HistogramBase::kUmaTargetedHistogramFlag);
[email protected]fe2dd7742011-04-19 22:52:491007
[email protected]13c68b62013-05-17 11:29:051008 PermissionMessages permissions =
1009 extensions::PermissionsData::GetPermissionMessages(extension);
[email protected]fe2dd7742011-04-19 22:52:491010 if (permissions.empty()) {
[email protected]c2e66e12012-06-27 06:27:061011 counter->Add(PermissionMessage::kNone);
[email protected]fe2dd7742011-04-19 22:52:491012 } else {
[email protected]c2e66e12012-06-27 06:27:061013 for (PermissionMessages::iterator it = permissions.begin();
[email protected]0d3e4a22011-06-23 19:02:521014 it != permissions.end(); ++it)
1015 counter->Add(it->id());
[email protected]fe2dd7742011-04-19 22:52:491016 }
1017}
1018
[email protected]eaa7dd182010-12-14 11:09:001019void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]57a777f72010-03-31 01:09:421020 // The ChromeURLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:061021 // was loaded, otherwise a race can arise where a renderer that is created
1022 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:421023 // that the request context doesn't yet know about. The profile is responsible
1024 // for ensuring its URLRequestContexts appropriately discover the loaded
1025 // extension.
[email protected]31d8f5f22012-04-02 15:22:081026 system_->RegisterExtensionWithRequestContexts(extension);
[email protected]62d30f42009-10-01 22:36:061027
[email protected]d5949312012-12-03 22:13:301028 // Tell renderers about the new extension, unless it's a theme (renderers
1029 // don't need to know about themes).
[email protected]a58f599c2012-12-01 03:08:191030 if (!extension->is_theme()) {
[email protected]19647262011-12-16 09:57:491031 for (content::RenderProcessHost::iterator i(
1032 content::RenderProcessHost::AllHostsIterator());
1033 !i.IsAtEnd(); i.Advance()) {
1034 content::RenderProcessHost* host = i.GetCurrentValue();
1035 Profile* host_profile =
1036 Profile::FromBrowserContext(host->GetBrowserContext());
1037 if (host_profile->GetOriginalProfile() ==
1038 profile_->GetOriginalProfile()) {
1039 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions(
1040 1, ExtensionMsg_Loaded_Params(extension));
1041 host->Send(
1042 new ExtensionMsg_Loaded(loaded_extensions));
1043 }
[email protected]c8d407e2011-04-28 21:27:171044 }
[email protected]77a6970c2011-04-23 16:58:561045 }
1046
[email protected]3442a662012-01-12 08:06:171047 // Tell subsystems that use the EXTENSION_LOADED notification about the new
1048 // extension.
1049 //
1050 // NOTE: It is important that this happen after notifying the renderers about
1051 // the new extensions so that if we navigate to an extension URL in
1052 // NOTIFICATION_EXTENSION_LOADED, the renderer is guaranteed to know about it.
1053 content::NotificationService::current()->Notify(
1054 chrome::NOTIFICATION_EXTENSION_LOADED,
1055 content::Source<Profile>(profile_),
1056 content::Details<const Extension>(extension));
1057
[email protected]c8d407e2011-04-28 21:27:171058 // Tell a random-ass collection of other subsystems about the new extension.
1059 // TODO(aa): What should we do with all this goop? Can it move into the
1060 // relevant objects via EXTENSION_LOADED?
1061
1062 profile_->GetExtensionSpecialStoragePolicy()->
1063 GrantRightsForExtension(extension);
1064
1065 UpdateActiveExtensionsInCrashReporter();
1066
[email protected]c8d407e2011-04-28 21:27:171067 // If the extension has permission to load chrome://favicon/ resources we need
1068 // to make sure that the FaviconSource is registered with the
1069 // ChromeURLDataManager.
[email protected]13c68b62013-05-17 11:29:051070 if (extensions::PermissionsData::HasHostPermission(
1071 extension, GURL(chrome::kChromeUIFaviconURL))) {
[email protected]c8d407e2011-04-28 21:27:171072 FaviconSource* favicon_source = new FaviconSource(profile_,
1073 FaviconSource::FAVICON);
[email protected]24ea7a12013-01-27 23:54:531074 content::URLDataSource::Add(profile_, favicon_source);
[email protected]c8d407e2011-04-28 21:27:171075 }
[email protected]b07e606e2012-09-15 20:16:151076
1077#if !defined(OS_ANDROID)
1078 // Same for chrome://theme/ resources.
[email protected]13c68b62013-05-17 11:29:051079 if (extensions::PermissionsData::HasHostPermission(
1080 extension, GURL(chrome::kChromeUIThemeURL))) {
[email protected]b07e606e2012-09-15 20:16:151081 ThemeSource* theme_source = new ThemeSource(profile_);
[email protected]24ea7a12013-01-27 23:54:531082 content::URLDataSource::Add(profile_, theme_source);
[email protected]b07e606e2012-09-15 20:16:151083 }
1084#endif
1085
[email protected]5eddc3e2011-10-26 04:33:311086 // Same for chrome://thumb/ resources.
[email protected]13c68b62013-05-17 11:29:051087 if (extensions::PermissionsData::HasHostPermission(
1088 extension, GURL(chrome::kChromeUIThumbnailURL))) {
[email protected]5eddc3e2011-10-26 04:33:311089 ThumbnailSource* thumbnail_source = new ThumbnailSource(profile_);
[email protected]24ea7a12013-01-27 23:54:531090 content::URLDataSource::Add(profile_, thumbnail_source);
[email protected]5eddc3e2011-10-26 04:33:311091 }
[email protected]c8d407e2011-04-28 21:27:171092
[email protected]ebd71962012-12-20 02:56:551093#if defined(ENABLE_PLUGINS)
[email protected]c8d407e2011-04-28 21:27:171094 // TODO(mpcomplete): This ends up affecting all profiles. See crbug.com/80757.
[email protected]2de307592011-04-05 21:16:581095 bool plugins_changed = false;
[email protected]098fa7a2013-03-08 22:11:171096 if (extensions::PluginInfo::HasPlugins(extension)) {
1097 const extensions::PluginInfo::PluginVector* plugins =
1098 extensions::PluginInfo::GetPlugins(extension);
1099 CHECK(plugins);
[email protected]2de307592011-04-05 21:16:581100 plugins_changed = true;
[email protected]098fa7a2013-03-08 22:11:171101 for (extensions::PluginInfo::PluginVector::const_iterator plugin =
1102 plugins->begin();
1103 plugin != plugins->end(); ++plugin) {
1104 PluginService::GetInstance()->RefreshPlugins();
1105 PluginService::GetInstance()->AddExtraPluginPath(plugin->path);
1106 ChromePluginServiceFilter* filter =
1107 ChromePluginServiceFilter::GetInstance();
1108 if (plugin->is_public) {
1109 filter->RestrictPluginToProfileAndOrigin(
1110 plugin->path, profile_, GURL());
1111 } else {
1112 filter->RestrictPluginToProfileAndOrigin(
1113 plugin->path, profile_, extension->url());
1114 }
[email protected]2de307592011-04-05 21:16:581115 }
1116 }
[email protected]84396dbc2011-04-14 06:33:421117
1118 bool nacl_modules_changed = false;
1119 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1120 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
[email protected]cebc3dc2011-04-18 17:15:001121 RegisterNaClModule(module.url, module.mime_type);
[email protected]84396dbc2011-04-14 06:33:421122 nacl_modules_changed = true;
1123 }
1124
[email protected]ed0ba002011-05-26 16:55:131125 if (nacl_modules_changed)
1126 UpdatePluginListWithNaClModules();
[email protected]84396dbc2011-04-14 06:33:421127
1128 if (plugins_changed || nacl_modules_changed)
[email protected]45a22e62011-10-12 09:48:021129 PluginService::GetInstance()->PurgePluginListCache(profile_, false);
[email protected]ebd71962012-12-20 02:56:551130#endif // defined(ENABLE_PLUGINS)
[email protected]62d30f42009-10-01 22:36:061131}
1132
[email protected]a9f39a312010-12-23 22:14:271133void ExtensionService::NotifyExtensionUnloaded(
[email protected]814a7bf0f2011-08-13 05:30:591134 const Extension* extension,
1135 extension_misc::UnloadedExtensionReason reason) {
[email protected]a9f39a312010-12-23 22:14:271136 UnloadedExtensionInfo details(extension, reason);
[email protected]ad50def52011-10-19 23:17:071137 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271138 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]6c2381d2011-10-19 02:52:531139 content::Source<Profile>(profile_),
1140 content::Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061141
[email protected]903d70a2012-12-04 13:00:171142#if defined(ENABLE_THEMES)
1143 // If the current theme is being unloaded, tell ThemeService to revert back
1144 // to the default theme.
1145 if (reason != extension_misc::UNLOAD_REASON_UPDATE && extension->is_theme()) {
1146 ThemeService* theme_service = ThemeServiceFactory::GetForProfile(profile_);
1147 if (extension->id() == theme_service->GetThemeID())
1148 theme_service->UseDefaultTheme();
1149 }
1150#endif
1151
[email protected]f3b1a082011-11-18 00:34:301152 for (content::RenderProcessHost::iterator i(
1153 content::RenderProcessHost::AllHostsIterator());
[email protected]77a6970c2011-04-23 16:58:561154 !i.IsAtEnd(); i.Advance()) {
[email protected]f3b1a082011-11-18 00:34:301155 content::RenderProcessHost* host = i.GetCurrentValue();
[email protected]9b62ecf2011-07-27 20:23:081156 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:301157 Profile::FromBrowserContext(host->GetBrowserContext());
[email protected]9b62ecf2011-07-27 20:23:081158 if (host_profile->GetOriginalProfile() == profile_->GetOriginalProfile())
[email protected]c8d407e2011-04-28 21:27:171159 host->Send(new ExtensionMsg_Unloaded(extension->id()));
[email protected]77a6970c2011-04-23 16:58:561160 }
1161
[email protected]31d8f5f22012-04-02 15:22:081162 system_->UnregisterExtensionWithRequestContexts(extension->id(), reason);
[email protected]c8d407e2011-04-28 21:27:171163 profile_->GetExtensionSpecialStoragePolicy()->
1164 RevokeRightsForExtension(extension);
1165
[email protected]b777b332011-04-16 04:01:081166#if defined(OS_CHROMEOS)
[email protected]ac66e452013-01-11 09:21:231167 // Revoke external file access for the extension from its file system context.
1168 // It is safe to access the extension's storage partition at this point. The
1169 // storage partition may get destroyed only after the extension gets unloaded.
1170 GURL site = extensions::ExtensionSystem::Get(profile_)->extension_service()->
1171 GetSiteForExtensionId(extension->id());
[email protected]10eb28162012-09-18 03:04:091172 fileapi::FileSystemContext* filesystem_context =
[email protected]ac66e452013-01-11 09:21:231173 BrowserContext::GetStoragePartitionForSite(profile_, site)->
[email protected]10eb28162012-09-18 03:04:091174 GetFileSystemContext();
1175 if (filesystem_context && filesystem_context->external_provider()) {
1176 filesystem_context->external_provider()->
[email protected]c8d407e2011-04-28 21:27:171177 RevokeAccessForExtension(extension->id());
[email protected]62d30f42009-10-01 22:36:061178 }
[email protected]c8d407e2011-04-28 21:27:171179#endif
1180
1181 UpdateActiveExtensionsInCrashReporter();
[email protected]2de307592011-04-05 21:16:581182
[email protected]ebd71962012-12-20 02:56:551183#if defined(ENABLE_PLUGINS)
[email protected]2de307592011-04-05 21:16:581184 bool plugins_changed = false;
[email protected]098fa7a2013-03-08 22:11:171185 if (extensions::PluginInfo::HasPlugins(extension)) {
1186 const extensions::PluginInfo::PluginVector* plugins =
1187 extensions::PluginInfo::GetPlugins(extension);
[email protected]2de307592011-04-05 21:16:581188 plugins_changed = true;
[email protected]098fa7a2013-03-08 22:11:171189 for (extensions::PluginInfo::PluginVector::const_iterator plugin =
1190 plugins->begin();
1191 plugin != plugins->end(); ++plugin) {
1192 PluginService::GetInstance()->ForcePluginShutdown(plugin->path);
1193 PluginService::GetInstance()->RefreshPlugins();
1194 PluginService::GetInstance()->RemoveExtraPluginPath(plugin->path);
1195 ChromePluginServiceFilter::GetInstance()->UnrestrictPlugin(plugin->path);
1196 }
[email protected]2de307592011-04-05 21:16:581197 }
[email protected]84396dbc2011-04-14 06:33:421198
1199 bool nacl_modules_changed = false;
1200 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1201 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
[email protected]cebc3dc2011-04-18 17:15:001202 UnregisterNaClModule(module.url);
[email protected]84396dbc2011-04-14 06:33:421203 nacl_modules_changed = true;
1204 }
1205
[email protected]ed0ba002011-05-26 16:55:131206 if (nacl_modules_changed)
1207 UpdatePluginListWithNaClModules();
[email protected]84396dbc2011-04-14 06:33:421208
1209 if (plugins_changed || nacl_modules_changed)
[email protected]45a22e62011-10-12 09:48:021210 PluginService::GetInstance()->PurgePluginListCache(profile_, false);
[email protected]ebd71962012-12-20 02:56:551211#endif // defined(ENABLE_PLUGINS)
[email protected]62d30f42009-10-01 22:36:061212}
1213
[email protected]0a071a32011-02-08 00:18:241214Profile* ExtensionService::profile() {
1215 return profile_;
1216}
1217
[email protected]45759612012-07-10 17:21:231218extensions::ExtensionPrefs* ExtensionService::extension_prefs() {
[email protected]ddb1e5a2010-12-13 20:10:451219 return extension_prefs_;
1220}
1221
[email protected]0d9a2202011-11-09 13:48:411222extensions::SettingsFrontend* ExtensionService::settings_frontend() {
[email protected]cccdf0aa2011-11-11 03:43:381223 return settings_frontend_.get();
[email protected]b7f853e282011-08-10 09:24:201224}
1225
[email protected]e2d720aa2012-05-17 00:11:351226extensions::ContentSettingsStore* ExtensionService::GetContentSettingsStore() {
[email protected]b790b072011-05-20 09:46:441227 return extension_prefs()->content_settings_store();
1228}
1229
[email protected]25ae0152011-11-18 14:40:021230bool ExtensionService::is_ready() {
[email protected]4a10006a2013-05-17 23:18:351231 return ready_->is_signaled();
[email protected]25ae0152011-11-18 14:40:021232}
1233
[email protected]7f8f24f2012-11-15 19:40:141234base::SequencedTaskRunner* ExtensionService::GetFileTaskRunner() {
1235 if (file_task_runner_)
1236 return file_task_runner_;
1237
1238 // We should be able to interrupt any part of extension install process during
1239 // shutdown. SKIP_ON_SHUTDOWN ensures that not started extension install tasks
1240 // will be ignored/deleted while we will block on started tasks.
1241 std::string token("ext_install-");
1242 token.append(profile_->GetPath().AsUTF8Unsafe());
1243 file_task_runner_ = BrowserThread::GetBlockingPool()->
1244 GetSequencedTaskRunnerWithShutdownBehavior(
1245 BrowserThread::GetBlockingPool()->GetNamedSequenceToken(token),
1246 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
1247 return file_task_runner_;
1248}
1249
[email protected]42a08162012-03-16 18:09:111250extensions::ExtensionUpdater* ExtensionService::updater() {
[email protected]2859946f2011-04-04 18:18:061251 return updater_.get();
1252}
1253
[email protected]4ee07c62012-08-21 12:40:421254void ExtensionService::CheckManagementPolicy() {
[email protected]aa96d3a2010-08-21 08:45:251255 std::vector<std::string> to_be_removed;
[email protected]695b5712012-12-06 23:55:281256
[email protected]aa96d3a2010-08-21 08:45:251257 // Loop through extensions list, unload installed extensions.
[email protected]84df8332011-12-06 18:22:461258 for (ExtensionSet::const_iterator iter = extensions_.begin();
[email protected]aa96d3a2010-08-21 08:45:251259 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021260 const Extension* extension = (*iter);
[email protected]4ee07c62012-08-21 12:40:421261 if (!system_->management_policy()->UserMayLoad(extension, NULL))
[email protected]aa96d3a2010-08-21 08:45:251262 to_be_removed.push_back(extension->id());
1263 }
1264
1265 // UnloadExtension will change the extensions_ list. So, we should
1266 // call it outside the iterator loop.
[email protected]31bb5ee62012-09-12 22:58:401267 for (size_t i = 0; i < to_be_removed.size(); ++i)
[email protected]814a7bf0f2011-08-13 05:30:591268 UnloadExtension(to_be_removed[i], extension_misc::UNLOAD_REASON_DISABLE);
[email protected]aa96d3a2010-08-21 08:45:251269}
1270
[email protected]31206602011-04-13 23:07:321271void ExtensionService::CheckForUpdatesSoon() {
[email protected]c3cfb012011-04-06 22:07:351272 if (updater()) {
[email protected]94fde232012-04-27 10:22:301273 if (AreAllExternalProvidersReady()) {
1274 updater()->CheckSoon();
1275 } else {
1276 // Sync can start updating before all the external providers are ready
1277 // during startup. Start the update as soon as those providers are ready,
1278 // but not before.
1279 update_once_all_providers_are_ready_ = true;
1280 }
[email protected]c3cfb012011-04-06 22:07:351281 } else {
[email protected]31206602011-04-13 23:07:321282 LOG(WARNING) << "CheckForUpdatesSoon() called with auto-update turned off";
[email protected]c3cfb012011-04-06 22:07:351283 }
1284}
1285
[email protected]3f98fd92012-11-17 15:54:291286syncer::SyncMergeResult ExtensionService::MergeDataAndStartSyncing(
[email protected]a4a147652012-07-03 23:41:321287 syncer::ModelType type,
[email protected]65f173552012-06-28 22:43:581288 const syncer::SyncDataList& initial_sync_data,
1289 scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
1290 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) {
[email protected]5db9ada2012-04-11 13:48:201291 CHECK(sync_processor.get());
[email protected]5d36e052012-04-20 20:12:281292 CHECK(sync_error_factory.get());
[email protected]3bdba0d2011-08-23 07:17:301293
1294 switch (type) {
[email protected]a4a147652012-07-03 23:41:321295 case syncer::EXTENSIONS:
[email protected]5db9ada2012-04-11 13:48:201296 extension_sync_bundle_.SetupSync(sync_processor.release(),
[email protected]5d36e052012-04-20 20:12:281297 sync_error_factory.release(),
[email protected]5db9ada2012-04-11 13:48:201298 initial_sync_data);
[email protected]3bdba0d2011-08-23 07:17:301299 break;
1300
[email protected]a4a147652012-07-03 23:41:321301 case syncer::APPS:
[email protected]5d36e052012-04-20 20:12:281302 app_sync_bundle_.SetupSync(sync_processor.release(),
1303 sync_error_factory.release(),
1304 initial_sync_data);
[email protected]3bdba0d2011-08-23 07:17:301305 break;
1306
1307 default:
1308 LOG(FATAL) << "Got " << type << " ModelType";
1309 }
[email protected]3bdba0d2011-08-23 07:17:301310
[email protected]b1417ab2011-12-15 17:09:111311 // Process local extensions.
1312 // TODO(yoz): Determine whether pending extensions should be considered too.
1313 // See crbug.com/104399.
[email protected]65f173552012-06-28 22:43:581314 syncer::SyncDataList sync_data_list = GetAllSyncData(type);
1315 syncer::SyncChangeList sync_change_list;
1316 for (syncer::SyncDataList::const_iterator i = sync_data_list.begin();
[email protected]3bdba0d2011-08-23 07:17:301317 i != sync_data_list.end();
1318 ++i) {
[email protected]5db9ada2012-04-11 13:48:201319 switch (type) {
[email protected]a4a147652012-07-03 23:41:321320 case syncer::EXTENSIONS:
[email protected]5db9ada2012-04-11 13:48:201321 sync_change_list.push_back(
1322 extension_sync_bundle_.CreateSyncChange(*i));
1323 break;
[email protected]a4a147652012-07-03 23:41:321324 case syncer::APPS:
[email protected]5db9ada2012-04-11 13:48:201325 sync_change_list.push_back(app_sync_bundle_.CreateSyncChange(*i));
1326 break;
1327 default:
1328 LOG(FATAL) << "Got " << type << " ModelType";
[email protected]31bb5ee62012-09-12 22:58:401329 }
[email protected]3bdba0d2011-08-23 07:17:301330 }
[email protected]3bdba0d2011-08-23 07:17:301331
[email protected]5db9ada2012-04-11 13:48:201332
[email protected]a4a147652012-07-03 23:41:321333 if (type == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:201334 extension_sync_bundle_.ProcessSyncChangeList(sync_change_list);
[email protected]a4a147652012-07-03 23:41:321335 } else if (type == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:201336 app_sync_bundle_.ProcessSyncChangeList(sync_change_list);
1337 }
[email protected]0ffd9e52012-01-26 21:44:341338
[email protected]3f98fd92012-11-17 15:54:291339 return syncer::SyncMergeResult(type);
[email protected]3bdba0d2011-08-23 07:17:301340}
1341
[email protected]a4a147652012-07-03 23:41:321342void ExtensionService::StopSyncing(syncer::ModelType type) {
1343 if (type == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:201344 app_sync_bundle_.Reset();
[email protected]a4a147652012-07-03 23:41:321345 } else if (type == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:201346 extension_sync_bundle_.Reset();
1347 }
[email protected]3bdba0d2011-08-23 07:17:301348}
1349
[email protected]65f173552012-06-28 22:43:581350syncer::SyncDataList ExtensionService::GetAllSyncData(
[email protected]a4a147652012-07-03 23:41:321351 syncer::ModelType type) const {
[email protected]31bb5ee62012-09-12 22:58:401352 if (type == syncer::EXTENSIONS)
[email protected]5db9ada2012-04-11 13:48:201353 return extension_sync_bundle_.GetAllSyncData();
[email protected]31bb5ee62012-09-12 22:58:401354 if (type == syncer::APPS)
[email protected]5db9ada2012-04-11 13:48:201355 return app_sync_bundle_.GetAllSyncData();
[email protected]5db9ada2012-04-11 13:48:201356
1357 // We should only get sync data for extensions and apps.
1358 NOTREACHED();
1359
[email protected]65f173552012-06-28 22:43:581360 return syncer::SyncDataList();
[email protected]3bdba0d2011-08-23 07:17:301361}
1362
[email protected]65f173552012-06-28 22:43:581363syncer::SyncError ExtensionService::ProcessSyncChanges(
[email protected]3bdba0d2011-08-23 07:17:301364 const tracked_objects::Location& from_here,
[email protected]65f173552012-06-28 22:43:581365 const syncer::SyncChangeList& change_list) {
1366 for (syncer::SyncChangeList::const_iterator i = change_list.begin();
[email protected]3bdba0d2011-08-23 07:17:301367 i != change_list.end();
1368 ++i) {
[email protected]a4a147652012-07-03 23:41:321369 syncer::ModelType type = i->sync_data().GetDataType();
1370 if (type == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:201371 extension_sync_bundle_.ProcessSyncChange(
1372 extensions::ExtensionSyncData(*i));
[email protected]a4a147652012-07-03 23:41:321373 } else if (type == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:201374 app_sync_bundle_.ProcessSyncChange(extensions::AppSyncData(*i));
1375 }
[email protected]3bdba0d2011-08-23 07:17:301376 }
1377
[email protected]0ffd9e52012-01-26 21:44:341378 extension_prefs()->extension_sorting()->FixNTPOrdinalCollisions();
1379
[email protected]65f173552012-06-28 22:43:581380 return syncer::SyncError();
[email protected]b05fb9ff2011-04-23 00:07:561381}
1382
[email protected]5db9ada2012-04-11 13:48:201383extensions::ExtensionSyncData ExtensionService::GetExtensionSyncData(
1384 const Extension& extension) const {
1385 return extensions::ExtensionSyncData(extension,
1386 IsExtensionEnabled(extension.id()),
1387 IsIncognitoEnabled(extension.id()));
[email protected]b05fb9ff2011-04-23 00:07:561388}
1389
[email protected]5db9ada2012-04-11 13:48:201390extensions::AppSyncData ExtensionService::GetAppSyncData(
1391 const Extension& extension) const {
1392 return extensions::AppSyncData(
1393 extension,
1394 IsExtensionEnabled(extension.id()),
1395 IsIncognitoEnabled(extension.id()),
[email protected]5db9ada2012-04-11 13:48:201396 extension_prefs_->extension_sorting()->GetAppLaunchOrdinal(
1397 extension.id()),
1398 extension_prefs_->extension_sorting()->GetPageOrdinal(extension.id()));
1399}
[email protected]3bdba0d2011-08-23 07:17:301400
[email protected]5db9ada2012-04-11 13:48:201401std::vector<extensions::ExtensionSyncData>
1402 ExtensionService::GetExtensionSyncDataList() const {
1403 std::vector<extensions::ExtensionSyncData> extension_sync_list;
1404 extension_sync_bundle_.GetExtensionSyncDataListHelper(extensions_,
1405 &extension_sync_list);
1406 extension_sync_bundle_.GetExtensionSyncDataListHelper(disabled_extensions_,
1407 &extension_sync_list);
1408 extension_sync_bundle_.GetExtensionSyncDataListHelper(terminated_extensions_,
1409 &extension_sync_list);
1410
1411 std::vector<extensions::ExtensionSyncData> pending_extensions =
1412 extension_sync_bundle_.GetPendingData();
1413 extension_sync_list.insert(extension_sync_list.begin(),
1414 pending_extensions.begin(),
1415 pending_extensions.end());
[email protected]3bdba0d2011-08-23 07:17:301416
1417 return extension_sync_list;
[email protected]b05fb9ff2011-04-23 00:07:561418}
1419
[email protected]5db9ada2012-04-11 13:48:201420std::vector<extensions::AppSyncData> ExtensionService::GetAppSyncDataList()
1421 const {
1422 std::vector<extensions::AppSyncData> app_sync_list;
1423 app_sync_bundle_.GetAppSyncDataListHelper(extensions_, &app_sync_list);
1424 app_sync_bundle_.GetAppSyncDataListHelper(disabled_extensions_,
1425 &app_sync_list);
1426 app_sync_bundle_.GetAppSyncDataListHelper(terminated_extensions_,
1427 &app_sync_list);
1428
1429 std::vector<extensions::AppSyncData> pending_apps =
1430 app_sync_bundle_.GetPendingData();
1431 app_sync_list.insert(app_sync_list.begin(),
1432 pending_apps.begin(),
1433 pending_apps.end());
1434
1435 return app_sync_list;
1436}
1437
1438bool ExtensionService::ProcessExtensionSyncData(
1439 const extensions::ExtensionSyncData& extension_sync_data) {
1440 if (!ProcessExtensionSyncDataHelper(extension_sync_data,
[email protected]a4a147652012-07-03 23:41:321441 syncer::EXTENSIONS)) {
[email protected]5db9ada2012-04-11 13:48:201442 extension_sync_bundle_.AddPendingExtension(extension_sync_data.id(),
1443 extension_sync_data);
1444 CheckForUpdatesSoon();
1445 return false;
1446 }
1447
1448 return true;
1449}
1450
1451bool ExtensionService::ProcessAppSyncData(
1452 const extensions::AppSyncData& app_sync_data) {
1453 const std::string& id = app_sync_data.id();
[email protected]5db9ada2012-04-11 13:48:201454
1455 if (app_sync_data.app_launch_ordinal().IsValid() &&
1456 app_sync_data.page_ordinal().IsValid()) {
1457 extension_prefs_->extension_sorting()->SetAppLaunchOrdinal(
1458 id,
1459 app_sync_data.app_launch_ordinal());
1460 extension_prefs_->extension_sorting()->SetPageOrdinal(
1461 id,
1462 app_sync_data.page_ordinal());
1463 }
1464
[email protected]5db9ada2012-04-11 13:48:201465 if (!ProcessExtensionSyncDataHelper(app_sync_data.extension_sync_data(),
[email protected]a4a147652012-07-03 23:41:321466 syncer::APPS)) {
[email protected]5db9ada2012-04-11 13:48:201467 app_sync_bundle_.AddPendingApp(id, app_sync_data);
1468 CheckForUpdatesSoon();
1469 return false;
1470 }
1471
1472 return true;
1473}
1474
1475bool ExtensionService::IsCorrectSyncType(const Extension& extension,
[email protected]a4a147652012-07-03 23:41:321476 syncer::ModelType type) const {
1477 if (type == syncer::EXTENSIONS &&
[email protected]31bb5ee62012-09-12 22:58:401478 extension.GetSyncType() == Extension::SYNC_TYPE_EXTENSION) {
[email protected]5db9ada2012-04-11 13:48:201479 return true;
[email protected]31bb5ee62012-09-12 22:58:401480 }
[email protected]5db9ada2012-04-11 13:48:201481
[email protected]a4a147652012-07-03 23:41:321482 if (type == syncer::APPS &&
[email protected]31bb5ee62012-09-12 22:58:401483 extension.GetSyncType() == Extension::SYNC_TYPE_APP) {
[email protected]5db9ada2012-04-11 13:48:201484 return true;
[email protected]31bb5ee62012-09-12 22:58:401485 }
[email protected]5db9ada2012-04-11 13:48:201486
1487 return false;
1488}
1489
1490bool ExtensionService::ProcessExtensionSyncDataHelper(
1491 const extensions::ExtensionSyncData& extension_sync_data,
[email protected]a4a147652012-07-03 23:41:321492 syncer::ModelType type) {
[email protected]3bdba0d2011-08-23 07:17:301493 const std::string& id = extension_sync_data.id();
[email protected]96e989b2011-08-30 19:35:061494 const Extension* extension = GetInstalledExtension(id);
1495
1496 // TODO(bolms): we should really handle this better. The particularly bad
1497 // case is where an app becomes an extension or vice versa, and we end up with
1498 // a zombie extension that won't go away.
[email protected]5db9ada2012-04-11 13:48:201499 if (extension && !IsCorrectSyncType(*extension, type))
1500 return true;
[email protected]90310d92011-04-17 07:35:041501
1502 // Handle uninstalls first.
[email protected]3bdba0d2011-08-23 07:17:301503 if (extension_sync_data.uninstalled()) {
[email protected]90310d92011-04-17 07:35:041504 std::string error;
1505 if (!UninstallExtensionHelper(this, id)) {
1506 LOG(WARNING) << "Could not uninstall extension " << id
1507 << " for sync";
1508 }
[email protected]5db9ada2012-04-11 13:48:201509 return true;
[email protected]90310d92011-04-17 07:35:041510 }
1511
[email protected]caf2d442012-05-04 10:00:501512 // Extension from sync was uninstalled by the user as external extensions.
1513 // Honor user choice and skip installation/enabling.
1514 if (IsExternalExtensionUninstalled(id)) {
1515 LOG(WARNING) << "Extension with id " << id
1516 << " from sync was uninstalled as external extension";
1517 return true;
1518 }
1519
[email protected]06f92562011-04-29 19:27:311520 // Set user settings.
[email protected]7e9e2422012-12-13 19:54:191521 // If the extension has been disabled from sync, it may not have
1522 // been installed yet, so we don't know if the disable reason was a
[email protected]b914e2952013-04-26 07:10:031523 // permissions increase. That will be updated once CheckPermissionsIncrease
[email protected]7e9e2422012-12-13 19:54:191524 // is called for it.
[email protected]31bb5ee62012-09-12 22:58:401525 if (extension_sync_data.enabled())
[email protected]06f92562011-04-29 19:27:311526 EnableExtension(id);
[email protected]31bb5ee62012-09-12 22:58:401527 else
[email protected]7e9e2422012-12-13 19:54:191528 DisableExtension(id, Extension::DISABLE_UNKNOWN_FROM_SYNC);
[email protected]81b14cc2011-04-29 00:39:371529
[email protected]406b5a92011-11-08 11:58:261530 // We need to cache some version information here because setting the
1531 // incognito flag invalidates the |extension| pointer (it reloads the
1532 // extension).
[email protected]9060d8b02012-01-13 02:14:301533 bool extension_installed = (extension != NULL);
[email protected]406b5a92011-11-08 11:58:261534 int result = extension ?
1535 extension->version()->CompareTo(extension_sync_data.version()) : 0;
1536 SetIsIncognitoEnabled(id, extension_sync_data.incognito_enabled());
1537 extension = NULL; // No longer safe to use.
1538
1539 if (extension_installed) {
[email protected]06f92562011-04-29 19:27:311540 // If the extension is already installed, check if it's outdated.
[email protected]90310d92011-04-17 07:35:041541 if (result < 0) {
1542 // Extension is outdated.
[email protected]5db9ada2012-04-11 13:48:201543 return false;
[email protected]b020dc12011-12-06 04:35:421544 }
[email protected]06f92562011-04-29 19:27:311545 } else {
[email protected]06f92562011-04-29 19:27:311546 // TODO(akalin): Replace silent update with a list of enabled
1547 // permissions.
[email protected]6cc7dbae2011-04-29 21:18:331548 const bool kInstallSilently = true;
[email protected]5db9ada2012-04-11 13:48:201549
[email protected]a4a147652012-07-03 23:41:321550 CHECK(type == syncer::EXTENSIONS || type == syncer::APPS);
[email protected]5db9ada2012-04-11 13:48:201551 ExtensionFilter filter =
[email protected]a4a147652012-07-03 23:41:321552 (type == syncer::APPS) ? IsSyncableApp : IsSyncableExtension;
[email protected]5db9ada2012-04-11 13:48:201553
[email protected]06f92562011-04-29 19:27:311554 if (!pending_extension_manager()->AddFromSync(
1555 id,
[email protected]3bdba0d2011-08-23 07:17:301556 extension_sync_data.update_url(),
[email protected]5db9ada2012-04-11 13:48:201557 filter,
[email protected]6cc7dbae2011-04-29 21:18:331558 kInstallSilently)) {
[email protected]06f92562011-04-29 19:27:311559 LOG(WARNING) << "Could not add pending extension for " << id;
[email protected]3bdba0d2011-08-23 07:17:301560 // This means that the extension is already pending installation, with a
1561 // non-INTERNAL location. Add to pending_sync_data, even though it will
1562 // never be removed (we'll never install a syncable version of the
1563 // extension), so that GetAllSyncData() continues to send it.
[email protected]06f92562011-04-29 19:27:311564 }
[email protected]3bdba0d2011-08-23 07:17:301565 // Track pending extensions so that we can return them in GetAllSyncData().
[email protected]5db9ada2012-04-11 13:48:201566 return false;
[email protected]90310d92011-04-17 07:35:041567 }
[email protected]5db9ada2012-04-11 13:48:201568
1569 return true;
[email protected]90310d92011-04-17 07:35:041570}
1571
[email protected]c3cfb012011-04-06 22:07:351572bool ExtensionService::IsIncognitoEnabled(
1573 const std::string& extension_id) const {
[email protected]cdc7b1f42012-12-07 19:39:481574 const Extension* extension = GetInstalledExtension(extension_id);
1575 if (extension && !extension->can_be_incognito_enabled())
1576 return false;
[email protected]c3cfb012011-04-06 22:07:351577 // If this is an existing component extension we always allow it to
1578 // work in incognito mode.
[email protected]1d5e58b2013-01-31 08:41:401579 if (extension && extension->location() == Manifest::COMPONENT)
[email protected]cb0ce1e022010-03-10 19:54:411580 return true;
1581
1582 // Check the prefs.
[email protected]c3cfb012011-04-06 22:07:351583 return extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]db7331a2010-02-25 22:10:501584}
[email protected]55a35692010-02-11 23:25:211585
[email protected]c3cfb012011-04-06 22:07:351586void ExtensionService::SetIsIncognitoEnabled(
1587 const std::string& extension_id, bool enabled) {
[email protected]81b14cc2011-04-29 00:39:371588 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]cdc7b1f42012-12-07 19:39:481589 if (extension && !extension->can_be_incognito_enabled())
1590 return;
[email protected]1d5e58b2013-01-31 08:41:401591 if (extension && extension->location() == Manifest::COMPONENT) {
[email protected]b873cd92012-02-09 21:51:481592 // This shouldn't be called for component extensions unless they are
1593 // syncable.
1594 DCHECK(extension->IsSyncable());
1595
1596 // If we are here, make sure the we aren't trying to change the value.
1597 DCHECK_EQ(enabled, IsIncognitoEnabled(extension_id));
1598
[email protected]c3cfb012011-04-06 22:07:351599 return;
1600 }
1601
[email protected]8c6c8c22011-03-09 12:52:341602 // Broadcast unloaded and loaded events to update browser state. Only bother
1603 // if the value changed and the extension is actually enabled, since there is
1604 // no UI otherwise.
[email protected]c3cfb012011-04-06 22:07:351605 bool old_enabled = extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]8c6c8c22011-03-09 12:52:341606 if (enabled == old_enabled)
1607 return;
1608
[email protected]c3cfb012011-04-06 22:07:351609 extension_prefs_->SetIsIncognitoEnabled(extension_id, enabled);
[email protected]81b14cc2011-04-29 00:39:371610
[email protected]ffbf3f12011-12-09 22:10:331611 bool extension_is_enabled = extensions_.Contains(extension_id);
[email protected]bd9892eb2011-11-11 19:41:341612
1613 // When we reload the extension the ID may be invalidated if we've passed it
1614 // by const ref everywhere. Make a copy to be safe.
1615 std::string id = extension_id;
[email protected]406b5a92011-11-08 11:58:261616 if (extension_is_enabled)
[email protected]ffbf3f12011-12-09 22:10:331617 ReloadExtension(id);
[email protected]3bdba0d2011-08-23 07:17:301618
[email protected]406b5a92011-11-08 11:58:261619 // Reloading the extension invalidates the |extension| pointer.
[email protected]bd9892eb2011-11-11 19:41:341620 extension = GetInstalledExtension(id);
[email protected]3bdba0d2011-08-23 07:17:301621 if (extension)
1622 SyncExtensionChangeIfNeeded(*extension);
[email protected]55a35692010-02-11 23:25:211623}
1624
[email protected]dc9a74f72012-08-17 18:07:211625bool ExtensionService::CanCrossIncognito(const Extension* extension) const {
[email protected]2a8f24e2010-11-03 21:37:051626 // We allow the extension to see events and data from another profile iff it
1627 // uses "spanning" behavior and it has incognito access. "split" mode
1628 // extensions only see events for a matching profile.
[email protected]0c41298f2011-09-13 21:17:221629 CHECK(extension);
[email protected]c3cfb012011-04-06 22:07:351630 return IsIncognitoEnabled(extension->id()) &&
[email protected]06492ed2013-03-24 22:13:141631 !extensions::IncognitoInfo::IsSplitMode(extension);
[email protected]2a8f24e2010-11-03 21:37:051632}
1633
[email protected]5ef7b002011-05-12 23:16:201634bool ExtensionService::CanLoadInIncognito(const Extension* extension) const {
1635 if (extension->is_hosted_app())
1636 return true;
1637 // Packaged apps and regular extensions need to be enabled specifically for
1638 // incognito (and split mode should be set).
[email protected]06492ed2013-03-24 22:13:141639 return extensions::IncognitoInfo::IsSplitMode(extension) &&
[email protected]5ef7b002011-05-12 23:16:201640 IsIncognitoEnabled(extension->id());
1641}
1642
[email protected]168389f2011-12-20 17:12:481643void ExtensionService::OnExtensionMoved(
1644 const std::string& moved_extension_id,
1645 const std::string& predecessor_extension_id,
1646 const std::string& successor_extension_id) {
[email protected]3569b502012-01-12 20:08:231647 extension_prefs_->extension_sorting()->OnExtensionMoved(
1648 moved_extension_id,
1649 predecessor_extension_id,
1650 successor_extension_id);
[email protected]168389f2011-12-20 17:12:481651
1652 const Extension* extension = GetInstalledExtension(moved_extension_id);
1653 if (extension)
1654 SyncExtensionChangeIfNeeded(*extension);
1655}
1656
[email protected]dc9a74f72012-08-17 18:07:211657bool ExtensionService::AllowFileAccess(const Extension* extension) const {
[email protected]05c82182010-06-24 17:49:081658 return (CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]334e04a2010-06-24 23:34:441659 switches::kDisableExtensionsFileAccessCheck) ||
[email protected]05c82182010-06-24 17:49:081660 extension_prefs_->AllowFileAccess(extension->id()));
1661}
1662
[email protected]eaa7dd182010-12-14 11:09:001663void ExtensionService::SetAllowFileAccess(const Extension* extension,
[email protected]3aff9ad2011-04-01 20:26:481664 bool allow) {
1665 // Reload to update browser state. Only bother if the value changed and the
1666 // extension is actually enabled, since there is no UI otherwise.
1667 bool old_allow = AllowFileAccess(extension);
1668 if (allow == old_allow)
1669 return;
1670
[email protected]05c82182010-06-24 17:49:081671 extension_prefs_->SetAllowFileAccess(extension->id(), allow);
[email protected]3aff9ad2011-04-01 20:26:481672
[email protected]84df8332011-12-06 18:22:461673 bool extension_is_enabled = extensions_.Contains(extension->id());
[email protected]3aff9ad2011-04-01 20:26:481674 if (extension_is_enabled)
1675 ReloadExtension(extension->id());
[email protected]05c82182010-06-24 17:49:081676}
1677
[email protected]8e4560b62011-01-14 10:09:141678// Some extensions will autoupdate themselves externally from Chrome. These
1679// are typically part of some larger client application package. To support
1680// these, the extension will register its location in the the preferences file
1681// (and also, on Windows, in the registry) and this code will periodically
1682// check that location for a .crx file, which it will then install locally if
1683// a new version is available.
1684// Errors are reported through ExtensionErrorReporter. Succcess is not
1685// reported.
[email protected]eaa7dd182010-12-14 11:09:001686void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:121687 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]8e4560b62011-01-14 10:09:141688
1689 // Note that this installation is intentionally silent (since it didn't
1690 // go through the front-end). Extensions that are registered in this
1691 // way are effectively considered 'pre-bundled', and so implicitly
1692 // trusted. In general, if something has HKLM or filesystem access,
1693 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:121694
[email protected]8e4560b62011-01-14 10:09:141695 // Ask each external extension provider to give us a call back for each
1696 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
[email protected]5df038b2012-07-16 19:03:271697 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141698 for (i = external_extension_providers_.begin();
1699 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:271700 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:141701 provider->VisitRegisteredExtension();
1702 }
1703
[email protected]50067e52011-10-20 23:17:071704 // Do any required work that we would have done after completion of all
1705 // providers.
1706 if (external_extension_providers_.empty()) {
1707 OnAllExternalProvidersReady();
1708 }
[email protected]9f1087e2009-06-15 17:29:321709}
1710
[email protected]50067e52011-10-20 23:17:071711void ExtensionService::OnExternalProviderReady(
[email protected]5df038b2012-07-16 19:03:271712 const extensions::ExternalProviderInterface* provider) {
[email protected]a29a517a2011-01-21 21:11:121713 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]50067e52011-10-20 23:17:071714 CHECK(provider->IsReady());
[email protected]a29a517a2011-01-21 21:11:121715
1716 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:141717 // if all of them are finished. So we check them first.
[email protected]94fde232012-04-27 10:22:301718 if (AreAllExternalProvidersReady())
1719 OnAllExternalProvidersReady();
1720}
1721
1722bool ExtensionService::AreAllExternalProvidersReady() const {
[email protected]5df038b2012-07-16 19:03:271723 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141724 for (i = external_extension_providers_.begin();
1725 i != external_extension_providers_.end(); ++i) {
[email protected]94fde232012-04-27 10:22:301726 if (!i->get()->IsReady())
1727 return false;
[email protected]8e4560b62011-01-14 10:09:141728 }
[email protected]94fde232012-04-27 10:22:301729 return true;
[email protected]50067e52011-10-20 23:17:071730}
1731
1732void ExtensionService::OnAllExternalProvidersReady() {
1733 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]94fde232012-04-27 10:22:301734 base::TimeDelta elapsed = base::Time::Now() - profile_->GetStartTime();
1735 UMA_HISTOGRAM_TIMES("Extension.ExternalProvidersReadyAfter", elapsed);
[email protected]50067e52011-10-20 23:17:071736
1737 // Install any pending extensions.
[email protected]94fde232012-04-27 10:22:301738 if (update_once_all_providers_are_ready_ && updater()) {
1739 update_once_all_providers_are_ready_ = false;
[email protected]42774e42012-11-14 21:01:061740 updater()->CheckNow(extensions::ExtensionUpdater::CheckParams());
[email protected]8e4560b62011-01-14 10:09:141741 }
1742
1743 // Uninstall all the unclaimed extensions.
[email protected]45759612012-07-10 17:21:231744 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> extensions_info(
[email protected]8e4560b62011-01-14 10:09:141745 extension_prefs_->GetInstalledExtensionsInfo());
1746 for (size_t i = 0; i < extensions_info->size(); ++i) {
1747 ExtensionInfo* info = extensions_info->at(i).get();
[email protected]1d5e58b2013-01-31 08:41:401748 if (Manifest::IsExternalLocation(info->extension_location))
[email protected]8e4560b62011-01-14 10:09:141749 CheckExternalUninstall(info->extension_id);
1750 }
[email protected]e5af875f2011-10-10 21:09:141751 IdentifyAlertableExtensions();
[email protected]e5af875f2011-10-10 21:09:141752}
1753
1754void ExtensionService::IdentifyAlertableExtensions() {
1755 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1756
[email protected]07c9f2f42012-02-29 18:45:221757 // Build up the lists of extensions that require acknowledgment. If this is
1758 // the first time, grandfather extensions that would have caused
1759 // notification.
[email protected]89226982012-07-16 20:09:181760 extension_error_ui_.reset(ExtensionErrorUI::Create(this));
[email protected]e5af875f2011-10-10 21:09:141761
[email protected]a9aa5932012-01-25 08:27:401762 bool did_show_alert = false;
[email protected]89226982012-07-16 20:09:181763 if (PopulateExtensionErrorUI(extension_error_ui_.get())) {
[email protected]460c6712013-04-24 07:20:011764 if (!is_first_run_) {
[email protected]a9aa5932012-01-25 08:27:401765 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]89226982012-07-16 20:09:181766 did_show_alert = extension_error_ui_->ShowErrorInBubbleView();
[email protected]e5af875f2011-10-10 21:09:141767 } else {
1768 // First run. Just acknowledge all the extensions, silently, by
1769 // shortcutting the display of the UI and going straight to the
1770 // callback for pressing the Accept button.
[email protected]a9aa5932012-01-25 08:27:401771 HandleExtensionAlertAccept();
[email protected]e5af875f2011-10-10 21:09:141772 }
1773 }
[email protected]a9aa5932012-01-25 08:27:401774
[email protected]612a1cb12012-10-17 13:18:031775 UpdateExternalExtensionAlert();
1776
[email protected]a9aa5932012-01-25 08:27:401777 if (!did_show_alert)
[email protected]89226982012-07-16 20:09:181778 extension_error_ui_.reset();
[email protected]e5af875f2011-10-10 21:09:141779}
1780
[email protected]89226982012-07-16 20:09:181781bool ExtensionService::PopulateExtensionErrorUI(
1782 ExtensionErrorUI* extension_error_ui) {
[email protected]07c9f2f42012-02-29 18:45:221783 bool needs_alert = false;
[email protected]695b5712012-12-06 23:55:281784
1785 // Extensions that are blacklisted.
1786 for (ExtensionSet::const_iterator it = blacklisted_extensions_.begin();
1787 it != blacklisted_extensions_.end(); ++it) {
1788 std::string id = (*it)->id();
1789 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(id)) {
1790 extension_error_ui->AddBlacklistedExtension(id);
1791 needs_alert = true;
1792 }
1793 }
1794
[email protected]07c9f2f42012-02-29 18:45:221795 for (ExtensionSet::const_iterator iter = extensions_.begin();
1796 iter != extensions_.end(); ++iter) {
1797 const Extension* e = *iter;
[email protected]695b5712012-12-06 23:55:281798
1799 // Extensions disabled by policy. Note: this no longer includes blacklisted
1800 // extensions, though we still show the same UI.
[email protected]fdd679b2012-11-15 20:49:391801 if (!system_->management_policy()->UserMayLoad(e, NULL)) {
[email protected]07c9f2f42012-02-29 18:45:221802 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(e->id())) {
[email protected]89226982012-07-16 20:09:181803 extension_error_ui->AddBlacklistedExtension(e->id());
[email protected]07c9f2f42012-02-29 18:45:221804 needs_alert = true;
1805 }
1806 }
[email protected]07c9f2f42012-02-29 18:45:221807 }
[email protected]695b5712012-12-06 23:55:281808
[email protected]07c9f2f42012-02-29 18:45:221809 return needs_alert;
1810}
1811
[email protected]a9aa5932012-01-25 08:27:401812void ExtensionService::HandleExtensionAlertClosed() {
[email protected]612a1cb12012-10-17 13:18:031813 const ExtensionIdSet* extension_ids =
1814 extension_error_ui_->get_blacklisted_extension_ids();
[email protected]e5af875f2011-10-10 21:09:141815 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1816 iter != extension_ids->end(); ++iter) {
1817 extension_prefs_->AcknowledgeBlacklistedExtension(*iter);
1818 }
[email protected]879a6092013-03-06 04:12:501819 extension_error_ui_.reset();
1820}
1821
1822void ExtensionService::HandleExtensionAlertAccept() {
[email protected]73a85e012013-04-04 10:45:301823 extension_error_ui_->Close();
[email protected]e5af875f2011-10-10 21:09:141824}
1825
[email protected]47fc70c2011-12-06 07:29:511826void ExtensionService::AcknowledgeExternalExtension(const std::string& id) {
1827 extension_prefs_->AcknowledgeExternalExtension(id);
[email protected]612a1cb12012-10-17 13:18:031828 UpdateExternalExtensionAlert();
[email protected]47fc70c2011-12-06 07:29:511829}
1830
[email protected]d96eb512012-11-01 23:44:081831bool ExtensionService::IsUnacknowledgedExternalExtension(
1832 const Extension* extension) {
[email protected]b82d18da2012-11-09 00:11:321833 if (!FeatureSwitch::prompt_for_external_extensions()->IsEnabled())
1834 return false;
1835
[email protected]1d5e58b2013-01-31 08:41:401836 return (Manifest::IsExternalLocation(extension->location()) &&
[email protected]d96eb512012-11-01 23:44:081837 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id()) &&
1838 !(extension_prefs_->GetDisableReasons(extension->id()) &
1839 Extension::DISABLE_SIDELOAD_WIPEOUT));
1840}
1841
[email protected]89226982012-07-16 20:09:181842void ExtensionService::HandleExtensionAlertDetails() {
1843 extension_error_ui_->ShowExtensions();
[email protected]73a85e012013-04-04 10:45:301844 // ShowExtensions may cause the error UI to close synchronously, e.g. if it
1845 // causes a navigation.
1846 if (extension_error_ui_)
1847 extension_error_ui_->Close();
[email protected]6c751e72010-11-23 10:11:101848}
1849
[email protected]612a1cb12012-10-17 13:18:031850void ExtensionService::UpdateExternalExtensionAlert() {
[email protected]41070e8d2012-10-24 01:34:361851 if (!FeatureSwitch::prompt_for_external_extensions()->IsEnabled())
[email protected]612a1cb12012-10-17 13:18:031852 return;
1853
1854 const Extension* extension = NULL;
1855 for (ExtensionSet::const_iterator iter = disabled_extensions_.begin();
1856 iter != disabled_extensions_.end(); ++iter) {
1857 const Extension* e = *iter;
[email protected]d96eb512012-11-01 23:44:081858 if (IsUnacknowledgedExternalExtension(e)) {
1859 extension = e;
1860 break;
[email protected]612a1cb12012-10-17 13:18:031861 }
1862 }
1863
1864 if (extension) {
[email protected]d96eb512012-11-01 23:44:081865 if (!extensions::HasExternalInstallError(this)) {
1866 if (extension_prefs_->IncrementAcknowledgePromptCount(extension->id()) >
[email protected]612a1cb12012-10-17 13:18:031867 kMaxExtensionAcknowledgePromptCount) {
[email protected]d96eb512012-11-01 23:44:081868 // Stop prompting for this extension, and check if there's another
1869 // one that needs prompting.
[email protected]612a1cb12012-10-17 13:18:031870 extension_prefs_->AcknowledgeExternalExtension(extension->id());
[email protected]d96eb512012-11-01 23:44:081871 UpdateExternalExtensionAlert();
1872 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
1873 EXTERNAL_EXTENSION_IGNORED,
1874 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
1875 return;
[email protected]612a1cb12012-10-17 13:18:031876 }
[email protected]b3aa7182013-04-25 04:45:231877 if (is_first_run_)
1878 extension_prefs_->SetExternalInstallFirstRun(extension->id());
1879 // first_run is true if the extension was installed during a first run
1880 // (even if it's post-first run now).
1881 bool first_run = extension_prefs_->IsExternalInstallFirstRun(
1882 extension->id());
1883 extensions::AddExternalInstallError(this, extension, first_run);
[email protected]612a1cb12012-10-17 13:18:031884 }
1885 } else {
1886 extensions::RemoveExternalInstallError(this);
1887 }
[email protected]612a1cb12012-10-17 13:18:031888}
1889
[email protected]a9f39a312010-12-23 22:14:271890void ExtensionService::UnloadExtension(
1891 const std::string& extension_id,
[email protected]814a7bf0f2011-08-13 05:30:591892 extension_misc::UnloadedExtensionReason reason) {
[email protected]27e469a2010-01-11 20:35:091893 // Make sure the extension gets deleted after we return from this function.
[email protected]695b5712012-12-06 23:55:281894 int include_mask = INCLUDE_EVERYTHING & ~INCLUDE_TERMINATED;
[email protected]9adb9692010-10-29 23:14:021895 scoped_refptr<const Extension> extension(
[email protected]695b5712012-12-06 23:55:281896 GetExtensionById(extension_id, include_mask));
[email protected]631cf822009-05-15 07:01:251897
[email protected]fa0c96732010-11-17 00:14:231898 // This method can be called via PostTask, so the extension may have been
1899 // unloaded by the time this runs.
[email protected]dd163fb02011-05-04 22:22:171900 if (!extension) {
1901 // In case the extension may have crashed/uninstalled. Allow the profile to
1902 // clean up its RequestContexts.
[email protected]31d8f5f22012-04-02 15:22:081903 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]fa0c96732010-11-17 00:14:231904 return;
[email protected]dd163fb02011-05-04 22:22:171905 }
[email protected]0c6da502009-08-14 22:32:391906
[email protected]20c8b942013-05-26 02:03:401907 // If uninstalling let RuntimeEventRouter know.
1908 if (reason == extension_misc::UNLOAD_REASON_UNINSTALL)
1909 extensions::RuntimeEventRouter::OnExtensionUninstalled(
1910 profile_, extension_id);
1911
[email protected]1eb175082010-02-10 09:26:161912 // Keep information about the extension so that we can reload it later
1913 // even if it's not permanently installed.
1914 unloaded_extension_paths_[extension->id()] = extension->path();
1915
[email protected]f17dbd42010-08-16 23:21:101916 // Clean up if the extension is meant to be enabled after a reload.
[email protected]b914e2952013-04-26 07:10:031917 reloading_extensions_.erase(extension->id());
[email protected]f17dbd42010-08-16 23:21:101918
[email protected]d7e9a862010-11-03 21:57:491919 // Clean up runtime data.
1920 extension_runtime_data_.erase(extension_id);
1921
[email protected]060d4972012-07-19 17:22:391922 if (disabled_extensions_.Contains(extension->id())) {
[email protected]a9f39a312010-12-23 22:14:271923 UnloadedExtensionInfo details(extension, reason);
1924 details.already_disabled = true;
[email protected]84df8332011-12-06 18:22:461925 disabled_extensions_.Remove(extension->id());
[email protected]ad50def52011-10-19 23:17:071926 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271927 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]6c2381d2011-10-19 02:52:531928 content::Source<Profile>(profile_),
1929 content::Details<UnloadedExtensionInfo>(&details));
[email protected]dd163fb02011-05-04 22:22:171930 // Make sure the profile cleans up its RequestContexts when an already
1931 // disabled extension is unloaded (since they are also tracking the disabled
1932 // extensions).
[email protected]31d8f5f22012-04-02 15:22:081933 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]0c6da502009-08-14 22:32:391934 return;
1935 }
1936
[email protected]060d4972012-07-19 17:22:391937 // Remove the extension from our list.
[email protected]84df8332011-12-06 18:22:461938 extensions_.Remove(extension->id());
[email protected]631cf822009-05-15 07:01:251939
[email protected]a9f39a312010-12-23 22:14:271940 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]631cf822009-05-15 07:01:251941}
1942
[email protected]eaa7dd182010-12-14 11:09:001943void ExtensionService::UnloadAllExtensions() {
[email protected]31bb5ee62012-09-12 22:58:401944 profile_->GetExtensionSpecialStoragePolicy()->RevokeRightsForAllExtensions();
[email protected]c8d407e2011-04-28 21:27:171945
[email protected]84df8332011-12-06 18:22:461946 extensions_.Clear();
1947 disabled_extensions_.Clear();
1948 terminated_extensions_.Clear();
[email protected]d7e9a862010-11-03 21:57:491949 extension_runtime_data_.clear();
[email protected]cd500f72010-06-25 23:44:321950
[email protected]c6e4a3412009-06-24 15:45:291951 // TODO(erikkay) should there be a notification for this? We can't use
1952 // EXTENSION_UNLOADED since that implies that the extension has been disabled
1953 // or uninstalled, and UnloadAll is just part of shutdown.
[email protected]9f1087e2009-06-15 17:29:321954}
1955
[email protected]eaa7dd182010-12-14 11:09:001956void ExtensionService::ReloadExtensions() {
[email protected]9f1087e2009-06-15 17:29:321957 UnloadAllExtensions();
[email protected]eac88332012-12-26 17:57:451958 component_loader_->LoadAll();
[email protected]d8c8f25f2011-11-02 18:18:011959 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]4a10006a2013-05-17 23:18:351960 // Don't call SetReadyAndNotifyListeners() since tests call this multiple
1961 // times.
[email protected]9f1087e2009-06-15 17:29:321962}
1963
[email protected]eaa7dd182010-12-14 11:09:001964void ExtensionService::GarbageCollectExtensions() {
[email protected]2f8757c32012-06-19 19:17:471965 if (extension_prefs_->pref_service()->ReadOnly())
1966 return;
1967
[email protected]9bd9a6862012-11-29 09:24:221968 if (pending_extension_manager()->HasPendingExtensions()) {
1969 // Don't garbage collect while there are pending installations, which may
1970 // be using the temporary installation directory. Try to garbage collect
1971 // again later.
1972 MessageLoop::current()->PostDelayedTask(
1973 FROM_HERE,
1974 base::Bind(&ExtensionService::GarbageCollectExtensions, AsWeakPtr()),
1975 base::TimeDelta::FromSeconds(kGarbageCollectRetryDelay));
1976 return;
1977 }
1978
[email protected]45759612012-07-10 17:21:231979 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> info(
[email protected]2f8757c32012-06-19 19:17:471980 extension_prefs_->GetInstalledExtensionsInfo());
1981
[email protected]650b2d52013-02-10 03:41:451982 std::multimap<std::string, base::FilePath> extension_paths;
[email protected]2f8757c32012-06-19 19:17:471983 for (size_t i = 0; i < info->size(); ++i)
[email protected]0db124b02012-11-07 04:55:051984 extension_paths.insert(std::make_pair(info->at(i)->extension_id,
1985 info->at(i)->extension_path));
1986
[email protected]399583b2012-12-11 09:33:421987 info = extension_prefs_->GetAllDelayedInstallInfo();
[email protected]0db124b02012-11-07 04:55:051988 for (size_t i = 0; i < info->size(); ++i)
1989 extension_paths.insert(std::make_pair(info->at(i)->extension_id,
1990 info->at(i)->extension_path));
[email protected]2f8757c32012-06-19 19:17:471991
[email protected]7f8f24f2012-11-15 19:40:141992 if (!GetFileTaskRunner()->PostTask(
1993 FROM_HERE,
[email protected]2f8757c32012-06-19 19:17:471994 base::Bind(
1995 &extension_file_util::GarbageCollectExtensions,
1996 install_directory_,
[email protected]31bb5ee62012-09-12 22:58:401997 extension_paths))) {
[email protected]2f8757c32012-06-19 19:17:471998 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:401999 }
[email protected]f6ccd652010-11-17 00:48:342000
[email protected]f766a9d2012-07-11 01:57:442001#if defined(ENABLE_THEMES)
[email protected]f6ccd652010-11-17 00:48:342002 // Also garbage-collect themes. We check |profile_| to be
2003 // defensive; in the future, we may call GarbageCollectExtensions()
2004 // from somewhere other than Init() (e.g., in a timer).
2005 if (profile_) {
[email protected]d5949312012-12-03 22:13:302006 ThemeServiceFactory::GetForProfile(profile_)->RemoveUnusedThemes();
[email protected]f6ccd652010-11-17 00:48:342007 }
[email protected]f766a9d2012-07-11 01:57:442008#endif
[email protected]3cf4f0992009-02-03 23:00:302009}
2010
[email protected]fb82dcd2012-03-21 14:15:462011void ExtensionService::SyncExtensionChangeIfNeeded(const Extension& extension) {
[email protected]5db9ada2012-04-11 13:48:202012 if (app_sync_bundle_.HandlesApp(extension)) {
2013 app_sync_bundle_.SyncChangeIfNeeded(extension);
2014 } else if (extension_sync_bundle_.HandlesExtension(extension)) {
2015 extension_sync_bundle_.SyncChangeIfNeeded(extension);
[email protected]fb82dcd2012-03-21 14:15:462016 }
2017}
2018
[email protected]820d9bd2013-04-03 03:46:032019void ExtensionService::SetReadyAndNotifyListeners() {
[email protected]4a10006a2013-05-17 23:18:352020 ready_->Signal();
[email protected]820d9bd2013-04-03 03:46:032021 content::NotificationService::current()->Notify(
2022 chrome::NOTIFICATION_EXTENSIONS_READY,
2023 content::Source<Profile>(profile_),
2024 content::NotificationService::NoDetails());
2025}
2026
[email protected]eaa7dd182010-12-14 11:09:002027void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]3eeddd892013-04-17 17:00:112028 if (updater_)
[email protected]93fd78f42009-07-10 16:43:172029 updater_->Start();
[email protected]fa6a9102010-11-22 15:38:502030
[email protected]bc151cf92013-02-12 04:57:262031 OnBlacklistUpdated();
[email protected]e72e8eb82009-06-18 17:21:512032}
2033
[email protected]fcb58a862012-05-01 01:03:152034void ExtensionService::AddExtension(const Extension* extension) {
[email protected]c8d407e2011-04-28 21:27:172035 // TODO(jstritar): We may be able to get rid of this branch by overriding the
2036 // default extension state to DISABLED when the --disable-extensions flag
2037 // is set (http://crbug.com/29067).
2038 if (!extensions_enabled() &&
2039 !extension->is_theme() &&
[email protected]1d5e58b2013-01-31 08:41:402040 extension->location() != Manifest::COMPONENT &&
2041 !Manifest::IsExternalLocation(extension->location())) {
[email protected]fcb58a862012-05-01 01:03:152042 return;
[email protected]330840c2012-08-29 22:21:012043 }
[email protected]c8d407e2011-04-28 21:27:172044
[email protected]b914e2952013-04-26 07:10:032045 bool is_extension_upgrade = false;
2046 if (const Extension* old = GetInstalledExtension(extension->id())) {
2047 is_extension_upgrade = true;
2048 DCHECK_NE(extension, old);
2049 // Other than for unpacked extensions, CrxInstaller should have guaranteed
2050 // that we aren't downgrading.
2051 if (!Manifest::IsUnpackedLocation(extension->location()))
2052 CHECK_GE(extension->version()->CompareTo(*(old->version())), 0);
2053 }
2054 SetBeingUpgraded(extension, is_extension_upgrade);
[email protected]c8d407e2011-04-28 21:27:172055
[email protected]1eb175082010-02-10 09:26:162056 // The extension is now loaded, remove its data from unloaded extension map.
2057 unloaded_extension_paths_.erase(extension->id());
2058
[email protected]bb7f40952011-01-13 00:21:202059 // If a terminated extension is loaded, remove it from the terminated list.
2060 UntrackTerminatedExtension(extension->id());
2061
[email protected]f17dbd42010-08-16 23:21:102062 // If the extension was disabled for a reload, then enable it.
[email protected]ca2e82022013-04-29 16:48:412063 bool reloading = reloading_extensions_.erase(extension->id()) > 0;
[email protected]f17dbd42010-08-16 23:21:102064
[email protected]b914e2952013-04-26 07:10:032065 // Check if the extension's privileges have changed and mark the
2066 // extension disabled if necessary.
2067 CheckPermissionsIncrease(extension, is_extension_upgrade);
2068
[email protected]ca2e82022013-04-29 16:48:412069 if (is_extension_upgrade && !reloading) {
2070 // To upgrade an extension in place, unload the old one and then load the
2071 // new one. ReloadExtension disables the extension, which is sufficient.
[email protected]b914e2952013-04-26 07:10:032072 UnloadExtension(extension->id(), extension_misc::UNLOAD_REASON_UPDATE);
2073 }
[email protected]0c6da502009-08-14 22:32:392074
[email protected]695b5712012-12-06 23:55:282075 if (extension_prefs_->IsExtensionBlacklisted(extension->id())) {
[email protected]ac875372013-02-28 04:36:092076 // Only prefs is checked for the blacklist. We rely on callers to check the
2077 // blacklist before calling into here, e.g. CrxInstaller checks before
2078 // installation, we check when loading installed extensions.
[email protected]695b5712012-12-06 23:55:282079 blacklisted_extensions_.Insert(extension);
[email protected]ca2e82022013-04-29 16:48:412080 } else if (!reloading &&
2081 extension_prefs_->IsExtensionDisabled(extension->id())) {
[email protected]6f6101832012-11-27 22:10:482082 disabled_extensions_.Insert(extension);
[email protected]3bdba0d2011-08-23 07:17:302083 SyncExtensionChangeIfNeeded(*extension);
[email protected]44d62b62012-04-11 00:06:032084 content::NotificationService::current()->Notify(
2085 chrome::NOTIFICATION_EXTENSION_UPDATE_DISABLED,
2086 content::Source<Profile>(profile_),
2087 content::Details<const Extension>(extension));
[email protected]62f051c2012-03-29 17:04:442088
[email protected]7e9e2422012-12-13 19:54:192089 // Show the extension disabled error if a permissions increase was the
2090 // only reason it was disabled.
2091 if (extension_prefs_->GetDisableReasons(extension->id()) ==
[email protected]44d62b62012-04-11 00:06:032092 Extension::DISABLE_PERMISSIONS_INCREASE) {
[email protected]62f051c2012-03-29 17:04:442093 extensions::AddExtensionDisabledError(this, extension);
2094 }
[email protected]ca2e82022013-04-29 16:48:412095 } else if (reloading) {
2096 // Replace the old extension with the new version.
2097 CHECK(!disabled_extensions_.Insert(extension));
2098 EnableExtension(extension->id());
2099 DoPostLoadTasks(extension);
[email protected]695b5712012-12-06 23:55:282100 } else {
2101 // All apps that are displayed in the launcher are ordered by their ordinals
2102 // so we must ensure they have valid ordinals.
2103 if (extension->RequiresSortOrdinal()) {
[email protected]bd36b652013-02-27 02:13:252104 if (!extension->ShouldDisplayInNewTabPage()) {
2105 extension_prefs_->extension_sorting()->MarkExtensionAsHidden(
2106 extension->id());
2107 }
[email protected]695b5712012-12-06 23:55:282108 extension_prefs_->extension_sorting()->EnsureValidOrdinals(
2109 extension->id(), syncer::StringOrdinal());
2110 }
2111
2112 extensions_.Insert(extension);
2113 SyncExtensionChangeIfNeeded(*extension);
2114 NotifyExtensionLoaded(extension);
2115 DoPostLoadTasks(extension);
[email protected]f574c402012-12-04 23:20:312116 }
[email protected]b914e2952013-04-26 07:10:032117 SetBeingUpgraded(extension, false);
[email protected]aab98a52009-12-02 03:22:352118}
2119
[email protected]8c484b742012-11-29 06:05:362120void ExtensionService::AddComponentExtension(const Extension* extension) {
2121 const std::string old_version_string(
2122 extension_prefs_->GetVersionString(extension->id()));
2123 const Version old_version(old_version_string);
2124
2125 if (!old_version.IsValid() || !old_version.Equals(*extension->version())) {
2126 VLOG(1) << "Component extension " << extension->name() << " ("
2127 << extension->id() << ") installing/upgrading from '"
2128 << old_version_string << "' to " << extension->version()->GetString();
2129
2130 AddNewOrUpdatedExtension(extension,
[email protected]399583b2012-12-11 09:33:422131 Extension::ENABLED_COMPONENT,
2132 syncer::StringOrdinal());
[email protected]8c484b742012-11-29 06:05:362133 return;
2134 }
2135
2136 AddExtension(extension);
2137}
2138
[email protected]b914e2952013-04-26 07:10:032139void ExtensionService::UpdateActivePermissions(const Extension* extension) {
[email protected]902fd7b2011-07-27 18:42:312140 // If the extension has used the optional permissions API, it will have a
2141 // custom set of active permissions defined in the extension prefs. Here,
2142 // we update the extension's active permissions based on the prefs.
[email protected]c2e66e12012-06-27 06:27:062143 scoped_refptr<PermissionSet> active_permissions =
[email protected]902fd7b2011-07-27 18:42:312144 extension_prefs()->GetActivePermissions(extension->id());
2145
[email protected]3eeddd892013-04-17 17:00:112146 if (active_permissions) {
[email protected]902fd7b2011-07-27 18:42:312147 // We restrict the active permissions to be within the bounds defined in the
2148 // extension's manifest.
2149 // a) active permissions must be a subset of optional + default permissions
2150 // b) active permissions must contains all default permissions
[email protected]c2e66e12012-06-27 06:27:062151 scoped_refptr<PermissionSet> total_permissions =
2152 PermissionSet::CreateUnion(
[email protected]2cb5e302013-05-09 05:00:062153 extensions::PermissionsData::GetRequiredPermissions(extension),
2154 extensions::PermissionsData::GetOptionalPermissions(extension));
[email protected]902fd7b2011-07-27 18:42:312155
2156 // Make sure the active permissions contain no more than optional + default.
[email protected]c2e66e12012-06-27 06:27:062157 scoped_refptr<PermissionSet> adjusted_active =
2158 PermissionSet::CreateIntersection(
[email protected]902fd7b2011-07-27 18:42:312159 total_permissions.get(), active_permissions.get());
2160
2161 // Make sure the active permissions contain the default permissions.
[email protected]c2e66e12012-06-27 06:27:062162 adjusted_active = PermissionSet::CreateUnion(
[email protected]2cb5e302013-05-09 05:00:062163 extensions::PermissionsData::GetRequiredPermissions(extension),
2164 adjusted_active.get());
[email protected]902fd7b2011-07-27 18:42:312165
[email protected]c333e792012-01-06 16:57:392166 extensions::PermissionsUpdater perms_updater(profile());
2167 perms_updater.UpdateActivePermissions(extension, adjusted_active);
[email protected]902fd7b2011-07-27 18:42:312168 }
[email protected]b914e2952013-04-26 07:10:032169}
2170
2171void ExtensionService::CheckPermissionsIncrease(const Extension* extension,
2172 bool is_extension_upgrade) {
2173 UpdateActivePermissions(extension);
[email protected]902fd7b2011-07-27 18:42:312174
[email protected]8d888c12010-11-30 00:00:252175 // We keep track of all permissions the user has granted each extension.
2176 // This allows extensions to gracefully support backwards compatibility
2177 // by including unknown permissions in their manifests. When the user
2178 // installs the extension, only the recognized permissions are recorded.
2179 // When the unknown permissions become recognized (e.g., through browser
2180 // upgrade), we can prompt the user to accept these new permissions.
2181 // Extensions can also silently upgrade to less permissions, and then
2182 // silently upgrade to a version that adds these permissions back.
2183 //
2184 // For example, pretend that Chrome 10 includes a permission "omnibox"
2185 // for an API that adds suggestions to the omnibox. An extension can
2186 // maintain backwards compatibility while still having "omnibox" in the
2187 // manifest. If a user installs the extension on Chrome 9, the browser
2188 // will record the permissions it recognized, not including "omnibox."
2189 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
2190 // will disable the extension and prompt the user to approve the increase
2191 // in privileges. The extension could then release a new version that
2192 // removes the "omnibox" permission. When the user upgrades, Chrome will
2193 // still remember that "omnibox" had been granted, so that if the
2194 // extension once again includes "omnibox" in an upgrade, the extension
2195 // can upgrade without requiring this user's approval.
[email protected]eb5e4f92012-08-15 23:33:282196 int disable_reasons = extension_prefs_->GetDisableReasons(extension->id());
[email protected]8d888c12010-11-30 00:00:252197
[email protected]be083862012-09-01 03:53:452198 bool is_default_app_install =
2199 (!is_extension_upgrade && extension->was_installed_by_default());
[email protected]b914e2952013-04-26 07:10:032200 // Silently grant all active permissions to default apps only on install.
2201 // After install they should behave like other apps.
2202 if (is_default_app_install)
2203 GrantPermissions(extension);
2204
2205 bool is_privilege_increase = false;
2206 // We only need to compare the granted permissions to the current permissions
2207 // if the extension is not allowed to silently increase its permissions.
[email protected]13c68b62013-05-17 11:29:052208 if (!extensions::PermissionsData::CanSilentlyIncreasePermissions(extension) &&
2209 !is_default_app_install) {
[email protected]8d888c12010-11-30 00:00:252210 // Add all the recognized permissions if the granted permissions list
2211 // hasn't been initialized yet.
[email protected]c2e66e12012-06-27 06:27:062212 scoped_refptr<PermissionSet> granted_permissions =
[email protected]902fd7b2011-07-27 18:42:312213 extension_prefs_->GetGrantedPermissions(extension->id());
[email protected]0d3e4a22011-06-23 19:02:522214 CHECK(granted_permissions.get());
[email protected]8d888c12010-11-30 00:00:252215
2216 // Here, we check if an extension's privileges have increased in a manner
2217 // that requires the user's approval. This could occur because the browser
2218 // upgraded and recognized additional privileges, or an extension upgrades
2219 // to a version that requires additional privileges.
[email protected]0d3e4a22011-06-23 19:02:522220 is_privilege_increase =
[email protected]902fd7b2011-07-27 18:42:312221 granted_permissions->HasLessPrivilegesThan(
2222 extension->GetActivePermissions());
[email protected]8d888c12010-11-30 00:00:252223 }
2224
2225 if (is_extension_upgrade) {
[email protected]44d62b62012-04-11 00:06:032226 // If the extension was already disabled, suppress any alerts for becoming
2227 // disabled on permissions increase.
[email protected]b914e2952013-04-26 07:10:032228 bool previously_disabled =
2229 extension_prefs_->IsExtensionDisabled(extension->id());
[email protected]7e9e2422012-12-13 19:54:192230 // Legacy disabled extensions do not have a disable reason. Infer that if
2231 // there was no permission increase, it was likely disabled by the user.
2232 if (previously_disabled && disable_reasons == Extension::DISABLE_NONE &&
[email protected]b914e2952013-04-26 07:10:032233 !extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
[email protected]7e9e2422012-12-13 19:54:192234 disable_reasons |= Extension::DISABLE_USER_ACTION;
[email protected]44d62b62012-04-11 00:06:032235 }
[email protected]7e9e2422012-12-13 19:54:192236 // Extensions that came to us disabled from sync need a similar inference,
2237 // except based on the new version's permissions.
2238 if (previously_disabled &&
2239 disable_reasons == Extension::DISABLE_UNKNOWN_FROM_SYNC) {
2240 // Remove the DISABLE_UNKNOWN_FROM_SYNC reason.
2241 extension_prefs_->ClearDisableReasons(extension->id());
2242 if (!is_privilege_increase)
2243 disable_reasons |= Extension::DISABLE_USER_ACTION;
2244 }
[email protected]b914e2952013-04-26 07:10:032245 disable_reasons &= ~Extension::DISABLE_UNKNOWN_FROM_SYNC;
[email protected]8d888c12010-11-30 00:00:252246 }
2247
2248 // Extension has changed permissions significantly. Disable it. A
2249 // notification should be sent by the caller.
2250 if (is_privilege_increase) {
[email protected]7e9e2422012-12-13 19:54:192251 disable_reasons |= Extension::DISABLE_PERMISSIONS_INCREASE;
[email protected]fe2dd7742011-04-19 22:52:492252 if (!extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
2253 RecordPermissionMessagesHistogram(
2254 extension, "Extensions.Permissions_AutoDisable");
2255 }
[email protected]b3317ad2011-04-28 23:46:002256 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
[email protected]8d888c12010-11-30 00:00:252257 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
[email protected]b914e2952013-04-26 07:10:032258 }
2259 if (disable_reasons != Extension::DISABLE_NONE) {
[email protected]eb5e4f92012-08-15 23:33:282260 extension_prefs_->AddDisableReason(
2261 extension->id(),
2262 static_cast<Extension::DisableReason>(disable_reasons));
[email protected]8d888c12010-11-30 00:00:252263 }
2264}
2265
[email protected]eaa7dd182010-12-14 11:09:002266void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:392267 std::set<std::string> extension_ids;
[email protected]84df8332011-12-06 18:22:462268 for (ExtensionSet::const_iterator iter = extensions_.begin();
2269 iter != extensions_.end(); ++iter) {
2270 const Extension* extension = *iter;
[email protected]1d5e58b2013-01-31 08:41:402271 if (!extension->is_theme() && extension->location() != Manifest::COMPONENT)
[email protected]84df8332011-12-06 18:22:462272 extension_ids.insert(extension->id());
[email protected]aab98a52009-12-02 03:22:352273 }
2274
2275 child_process_logging::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:252276}
2277
[email protected]9d02fa12013-02-19 05:12:572278void ExtensionService::ScheduleLaunchOnLoad(const std::string& extension_id) {
2279 on_load_events_[extension_id] = EVENT_LAUNCHED;
2280}
2281
[email protected]8266d662011-07-12 21:53:262282void ExtensionService::OnExtensionInstalled(
[email protected]36a5c4c2011-12-14 16:34:502283 const Extension* extension,
[email protected]98270432012-09-11 20:51:242284 const syncer::StringOrdinal& page_ordinal,
[email protected]0db124b02012-11-07 04:55:052285 bool has_requirement_errors,
2286 bool wait_for_idle) {
[email protected]a29a517a2011-01-21 21:11:122287 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:202288
[email protected]c3cfb012011-04-06 22:07:352289 const std::string& id = extension->id();
[email protected]612a1cb12012-10-17 13:18:032290 bool initial_enable = ShouldEnableOnInstall(extension);
[email protected]3f213ad2012-07-26 23:39:412291 const extensions::PendingExtensionInfo* pending_extension_info = NULL;
[email protected]51a3bf8b2012-06-08 22:53:062292 if ((pending_extension_info = pending_extension_manager()->GetById(id))) {
2293 if (!pending_extension_info->ShouldAllowInstall(*extension)) {
2294 pending_extension_manager()->Remove(id);
[email protected]8ef78fd2010-08-19 17:14:322295
[email protected]31bb5ee62012-09-12 22:58:402296 LOG(WARNING) << "ShouldAllowInstall() returned false for "
2297 << id << " of type " << extension->GetType()
[email protected]65348062013-01-15 07:27:222298 << " and update URL "
2299 << extensions::ManifestURL::GetUpdateURL(extension).spec()
[email protected]31bb5ee62012-09-12 22:58:402300 << "; not installing";
[email protected]ae6cb3022011-04-04 20:11:542301
[email protected]ad50def52011-10-19 23:17:072302 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272303 chrome::NOTIFICATION_EXTENSION_INSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:532304 content::Source<Profile>(profile_),
2305 content::Details<const Extension>(extension));
[email protected]ae6cb3022011-04-04 20:11:542306
[email protected]4416c5a2010-06-26 01:28:572307 // Delete the extension directory since we're not going to
2308 // load it.
[email protected]7f8f24f2012-11-15 19:40:142309 if (!GetFileTaskRunner()->PostTask(
2310 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362311 base::Bind(&extension_file_util::DeleteFile,
[email protected]31bb5ee62012-09-12 22:58:402312 extension->path(), true))) {
[email protected]14908b72011-04-20 06:54:362313 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:402314 }
[email protected]4416c5a2010-06-26 01:28:572315 return;
2316 }
[email protected]51a3bf8b2012-06-08 22:53:062317
2318 pending_extension_manager()->Remove(id);
[email protected]4416c5a2010-06-26 01:28:572319 } else {
[email protected]c3cfb012011-04-06 22:07:352320 // We explicitly want to re-enable an uninstalled external
2321 // extension; if we're here, that means the user is manually
2322 // installing the extension.
[email protected]6cc7dbae2011-04-29 21:18:332323 if (IsExternalExtensionUninstalled(id)) {
2324 initial_enable = true;
2325 }
[email protected]aa142702010-03-26 01:26:332326 }
2327
[email protected]98270432012-09-11 20:51:242328 // Unsupported requirements overrides the management policy.
2329 if (has_requirement_errors) {
2330 initial_enable = false;
2331 extension_prefs_->AddDisableReason(
2332 id, Extension::DISABLE_UNSUPPORTED_REQUIREMENT);
2333 // If the extension was disabled because of unsupported requirements but
2334 // now supports all requirements after an update and there are not other
2335 // disable reasons, enable it.
2336 } else if (extension_prefs_->GetDisableReasons(id) ==
2337 Extension::DISABLE_UNSUPPORTED_REQUIREMENT) {
2338 initial_enable = true;
2339 extension_prefs_->ClearDisableReasons(id);
2340 }
2341
[email protected]695b5712012-12-06 23:55:282342 if (!GetInstalledExtension(extension->id())) {
[email protected]07533022011-06-27 20:42:552343 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
2344 extension->GetType(), 100);
[email protected]69084dc2012-11-17 07:39:302345 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallSource",
[email protected]1d5e58b2013-01-31 08:41:402346 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]07533022011-06-27 20:42:552347 RecordPermissionMessagesHistogram(
2348 extension, "Extensions.Permissions_Install");
[email protected]69084dc2012-11-17 07:39:302349 } else {
2350 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateType",
2351 extension->GetType(), 100);
2352 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateSource",
[email protected]1d5e58b2013-01-31 08:41:402353 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]07533022011-06-27 20:42:552354 }
2355
[email protected]a39921b42012-02-28 03:42:542356 // Certain extension locations are specific enough that we can
2357 // auto-acknowledge any extension that came from one of them.
[email protected]1d5e58b2013-01-31 08:41:402358 if (extension->location() == Manifest::EXTERNAL_POLICY_DOWNLOAD)
[email protected]a39921b42012-02-28 03:42:542359 AcknowledgeExternalExtension(extension->id());
[email protected]399583b2012-12-11 09:33:422360 const Extension::State initial_state =
2361 initial_enable ? Extension::ENABLED : Extension::DISABLED;
[email protected]e7aa7b7e2012-11-27 04:51:222362 if (ShouldDelayExtensionUpdate(id, wait_for_idle)) {
[email protected]399583b2012-12-11 09:33:422363 extension_prefs_->SetDelayedInstallInfo(extension, initial_state,
2364 page_ordinal);
[email protected]0db124b02012-11-07 04:55:052365
2366 // Transfer ownership of |extension|.
[email protected]399583b2012-12-11 09:33:422367 delayed_updates_for_idle_.Insert(extension);
[email protected]0db124b02012-11-07 04:55:052368
2369 // Notify extension of available update.
2370 extensions::RuntimeEventRouter::DispatchOnUpdateAvailableEvent(
2371 profile_, id, extension->manifest()->value());
[email protected]75bdcb872013-03-13 00:41:452372
2373 // Notify observers that app update is available.
2374 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
2375 OnAppUpdateAvailable(extension->id()));
[email protected]0db124b02012-11-07 04:55:052376 return;
2377 }
2378
[email protected]399583b2012-12-11 09:33:422379 if (installs_delayed()) {
2380 extension_prefs_->SetDelayedInstallInfo(extension, initial_state,
2381 page_ordinal);
2382 delayed_installs_.Insert(extension);
2383 } else {
2384 AddNewOrUpdatedExtension(extension, initial_state, page_ordinal);
2385 }
[email protected]8c484b742012-11-29 06:05:362386}
2387
2388void ExtensionService::AddNewOrUpdatedExtension(
2389 const Extension* extension,
[email protected]399583b2012-12-11 09:33:422390 Extension::State initial_state,
2391 const syncer::StringOrdinal& page_ordinal) {
[email protected]8c484b742012-11-29 06:05:362392 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2393
[email protected]4416c5a2010-06-26 01:28:572394 extension_prefs_->OnExtensionInstalled(
[email protected]8266d662011-07-12 21:53:262395 extension,
[email protected]8c484b742012-11-29 06:05:362396 initial_state,
[email protected]36a5c4c2011-12-14 16:34:502397 page_ordinal);
[email protected]25b34332009-06-05 21:53:192398
[email protected]6f6101832012-11-27 22:10:482399 FinishInstallation(extension);
2400}
2401
2402void ExtensionService::MaybeFinishDelayedInstallation(
2403 const std::string& extension_id) {
2404 // Check if the extension already got updated.
[email protected]399583b2012-12-11 09:33:422405 if (!delayed_updates_for_idle_.Contains(extension_id))
[email protected]6f6101832012-11-27 22:10:482406 return;
2407 // Check if the extension is idle.
2408 if (!IsExtensionIdle(extension_id))
2409 return;
2410
2411 FinishDelayedInstallation(extension_id);
2412}
2413
2414void ExtensionService::FinishDelayedInstallation(
2415 const std::string& extension_id) {
2416 scoped_refptr<const Extension> extension(
2417 GetPendingExtensionUpdate(extension_id));
2418 CHECK(extension);
[email protected]399583b2012-12-11 09:33:422419 delayed_updates_for_idle_.Remove(extension_id);
[email protected]6f6101832012-11-27 22:10:482420
[email protected]399583b2012-12-11 09:33:422421 if (!extension_prefs_->FinishDelayedInstallInfo(extension_id))
[email protected]6f6101832012-11-27 22:10:482422 NOTREACHED();
2423
2424 FinishInstallation(extension);
2425}
2426
2427void ExtensionService::FinishInstallation(const Extension* extension) {
[email protected]41bb80bd2013-05-03 10:56:022428 bool is_update = GetInstalledExtension(extension->id()) != NULL;
2429 extensions::InstalledExtensionInfo details(extension, is_update);
[email protected]fcb58a862012-05-01 01:03:152430 content::NotificationService::current()->Notify(
2431 chrome::NOTIFICATION_EXTENSION_INSTALLED,
2432 content::Source<Profile>(profile_),
[email protected]41bb80bd2013-05-03 10:56:022433 content::Details<const extensions::InstalledExtensionInfo>(&details));
[email protected]fcb58a862012-05-01 01:03:152434
[email protected]d96eb512012-11-01 23:44:082435 bool unacknowledged_external = IsUnacknowledgedExternalExtension(extension);
[email protected]41070e8d2012-10-24 01:34:362436
[email protected]399583b2012-12-11 09:33:422437 // Unpacked extensions default to allowing file access, but if that has been
2438 // overridden, don't reset the value.
[email protected]1d5e58b2013-01-31 08:41:402439 if (Manifest::ShouldAlwaysAllowFileAccess(extension->location()) &&
[email protected]399583b2012-12-11 09:33:422440 !extension_prefs_->HasAllowFileAccessSetting(extension->id())) {
2441 extension_prefs_->SetAllowFileAccess(extension->id(), true);
2442 }
2443
[email protected]6f6101832012-11-27 22:10:482444 AddExtension(extension);
[email protected]612a1cb12012-10-17 13:18:032445
[email protected]a51c9e92012-12-09 09:19:202446#if defined(ENABLE_THEMES)
[email protected]bc151cf92013-02-12 04:57:262447 // We do this here since AddExtension() is always called on browser startup,
2448 // and we only really care about the last theme installed.
[email protected]a51c9e92012-12-09 09:19:202449 // If that ever changes and we have to move this code somewhere
2450 // else, it should be somewhere that's not in the startup path.
2451 if (extension->is_theme() && extensions_.GetByID(extension->id())) {
2452 DCHECK_EQ(extensions_.GetByID(extension->id()), extension);
2453 // Now that the theme extension is visible from outside the
2454 // ExtensionService, notify the ThemeService about the
2455 // newly-installed theme.
2456 ThemeServiceFactory::GetForProfile(profile_)->SetTheme(extension);
2457 }
2458#endif
2459
[email protected]612a1cb12012-10-17 13:18:032460 // If this is a new external extension that was disabled, alert the user
[email protected]d96eb512012-11-01 23:44:082461 // so he can reenable it. We do this last so that it has already been
2462 // added to our list of extensions.
2463 if (unacknowledged_external) {
[email protected]612a1cb12012-10-17 13:18:032464 UpdateExternalExtensionAlert();
[email protected]d96eb512012-11-01 23:44:082465 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
2466 EXTERNAL_EXTENSION_INSTALLED,
2467 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
2468 }
[email protected]4a190632009-05-09 01:07:422469}
2470
[email protected]76b65442012-11-17 14:11:482471const Extension* ExtensionService::GetPendingExtensionUpdate(
2472 const std::string& id) const {
[email protected]399583b2012-12-11 09:33:422473 return delayed_updates_for_idle_.GetByID(id);
[email protected]76b65442012-11-17 14:11:482474}
2475
[email protected]bb7f40952011-01-13 00:21:202476void ExtensionService::TrackTerminatedExtension(const Extension* extension) {
[email protected]84df8332011-12-06 18:22:462477 if (!terminated_extensions_.Contains(extension->id()))
2478 terminated_extensions_.Insert(make_scoped_refptr(extension));
[email protected]fa2416f2011-05-03 08:41:202479
[email protected]b3f7fe22011-11-11 19:27:562480 UnloadExtension(extension->id(), extension_misc::UNLOAD_REASON_TERMINATE);
[email protected]bb7f40952011-01-13 00:21:202481}
2482
2483void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
[email protected]fa2416f2011-05-03 08:41:202484 std::string lowercase_id = StringToLowerASCII(id);
[email protected]84df8332011-12-06 18:22:462485 terminated_extensions_.Remove(lowercase_id);
[email protected]bb7f40952011-01-13 00:21:202486}
2487
[email protected]0dfe05c2011-02-23 23:03:362488const Extension* ExtensionService::GetTerminatedExtension(
[email protected]8001df22011-04-28 19:59:472489 const std::string& id) const {
[email protected]695b5712012-12-06 23:55:282490 return GetExtensionById(id, INCLUDE_TERMINATED);
[email protected]8001df22011-04-28 19:59:472491}
2492
2493const Extension* ExtensionService::GetInstalledExtension(
2494 const std::string& id) const {
[email protected]695b5712012-12-06 23:55:282495 int include_mask = INCLUDE_ENABLED |
2496 INCLUDE_DISABLED |
2497 INCLUDE_TERMINATED |
2498 INCLUDE_BLACKLISTED;
2499 return GetExtensionById(id, include_mask);
[email protected]0dfe05c2011-02-23 23:03:362500}
2501
[email protected]eaa7dd182010-12-14 11:09:002502bool ExtensionService::ExtensionBindingsAllowed(const GURL& url) {
[email protected]615d88f2011-12-13 01:47:442503 // Allow bindings for all packaged extensions and component hosted apps.
2504 const Extension* extension = extensions_.GetExtensionOrAppByURL(
2505 ExtensionURLInfo(url));
2506 return extension && (!extension->is_hosted_app() ||
[email protected]1d5e58b2013-01-31 08:41:402507 extension->location() == Manifest::COMPONENT);
[email protected]6d2e60bd2010-06-03 22:37:392508}
2509
[email protected]4d007b312012-10-17 03:00:482510bool ExtensionService::ShouldBlockUrlInBrowserTab(GURL* url) {
2511 const Extension* extension = extensions_.GetExtensionOrAppByURL(
2512 ExtensionURLInfo(*url));
2513 if (extension && extension->is_platform_app()) {
2514 *url = GURL(chrome::kExtensionInvalidRequestURL);
2515 return true;
2516 }
2517
2518 return false;
2519}
2520
[email protected]9060d8b02012-01-13 02:14:302521bool ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:322522 const std::string& id,
[email protected]8e4560b62011-01-14 10:09:142523 const Version* version,
[email protected]650b2d52013-02-10 03:41:452524 const base::FilePath& path,
[email protected]1d5e58b2013-01-31 08:41:402525 Manifest::Location location,
[email protected]47fc70c2011-12-06 07:29:512526 int creation_flags,
2527 bool mark_acknowledged) {
[email protected]ab22ba42011-01-14 16:36:382528 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2529 CHECK(Extension::IdIsValid(id));
[email protected]79c833b52011-04-05 18:31:012530 if (extension_prefs_->IsExternalExtensionUninstalled(id))
[email protected]9060d8b02012-01-13 02:14:302531 return false;
[email protected]a8af9fdb2010-10-28 21:52:202532
[email protected]7577a5c52009-07-30 06:21:582533 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:492534 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:582535 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:022536 const Extension* existing = GetExtensionById(id, true);
[email protected]910f72ce2012-08-24 01:38:352537
[email protected]7577a5c52009-07-30 06:21:582538 if (existing) {
[email protected]910f72ce2012-08-24 01:38:352539 // The default apps will have the location set as INTERNAL. Since older
2540 // default apps are installed as EXTERNAL, we override them. However, if the
2541 // app is already installed as internal, then do the version check.
[email protected]31bb5ee62012-09-12 22:58:402542 // TODO(grv) : Remove after Q1-2013.
[email protected]910f72ce2012-08-24 01:38:352543 bool is_default_apps_migration =
[email protected]1d5e58b2013-01-31 08:41:402544 (location == Manifest::INTERNAL &&
2545 Manifest::IsExternalLocation(existing->location()));
[email protected]910f72ce2012-08-24 01:38:352546
2547 if (!is_default_apps_migration) {
2548 DCHECK(version);
2549
2550 switch (existing->version()->CompareTo(*version)) {
2551 case -1: // existing version is older, we should upgrade
2552 break;
2553 case 0: // existing version is same, do nothing
2554 return false;
2555 case 1: // existing version is newer, uh-oh
2556 LOG(WARNING) << "Found external version of extension " << id
2557 << "that is older than current version. Current version "
2558 << "is: " << existing->VersionString() << ". New "
2559 << "version is: " << version->GetString()
2560 << ". Keeping current version.";
2561 return false;
2562 }
[email protected]7577a5c52009-07-30 06:21:582563 }
2564 }
2565
[email protected]9060d8b02012-01-13 02:14:302566 // If the extension is already pending, don't start an install.
[email protected]e3987852012-05-04 10:06:302567 if (!pending_extension_manager()->AddFromExternalFile(
2568 id, location, *version)) {
[email protected]9060d8b02012-01-13 02:14:302569 return false;
[email protected]e3987852012-05-04 10:06:302570 }
[email protected]9c635f22010-12-02 09:36:362571
[email protected]14908b72011-04-20 06:54:362572 // no client (silent install)
[email protected]d8c8f25f2011-11-02 18:18:012573 scoped_refptr<CrxInstaller> installer(CrxInstaller::Create(this, NULL));
[email protected]6dfbbf82010-03-12 23:09:162574 installer->set_install_source(location);
2575 installer->set_expected_id(id);
[email protected]cb0e50312011-05-09 15:03:072576 installer->set_expected_version(*version);
2577 installer->set_install_cause(extension_misc::INSTALL_CAUSE_EXTERNAL_FILE);
[email protected]1bf73cc32011-10-26 22:38:312578 installer->set_creation_flags(creation_flags);
[email protected]88e8ec9152013-01-17 04:05:182579#if defined(OS_CHROMEOS)
2580 extensions::InstallLimiter::Get(profile_)->Add(installer, path);
2581#else
[email protected]6dfbbf82010-03-12 23:09:162582 installer->InstallCrx(path);
[email protected]88e8ec9152013-01-17 04:05:182583#endif
[email protected]47fc70c2011-12-06 07:29:512584
2585 // Depending on the source, a new external extension might not need a user
2586 // notification on installation. For such extensions, mark them acknowledged
2587 // now to suppress the notification.
2588 if (mark_acknowledged)
2589 AcknowledgeExternalExtension(id);
[email protected]9060d8b02012-01-13 02:14:302590
2591 return true;
[email protected]7577a5c52009-07-30 06:21:582592}
2593
[email protected]eaa7dd182010-12-14 11:09:002594void ExtensionService::ReportExtensionLoadError(
[email protected]650b2d52013-02-10 03:41:452595 const base::FilePath& extension_path,
[email protected]d11c8e92009-10-20 23:26:402596 const std::string &error,
[email protected]d11c8e92009-10-20 23:26:402597 bool be_noisy) {
[email protected]d8c8f25f2011-11-02 18:18:012598 content::NotificationService::current()->Notify(
2599 chrome::NOTIFICATION_EXTENSION_LOAD_ERROR,
2600 content::Source<Profile>(profile_),
2601 content::Details<const std::string>(&error));
[email protected]d11c8e92009-10-20 23:26:402602
[email protected]8a205c02011-02-04 20:41:332603 std::string path_str = UTF16ToUTF8(extension_path.LossyDisplayName());
[email protected]438afb12012-07-05 03:18:212604 string16 message = UTF8ToUTF16(base::StringPrintf(
[email protected]18d4b6c2010-09-21 03:21:042605 "Could not load extension from '%s'. %s",
[email protected]fc670822011-12-17 09:33:492606 path_str.c_str(), error.c_str()));
[email protected]d11c8e92009-10-20 23:26:402607 ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy);
2608}
2609
[email protected]eaa7dd182010-12-14 11:09:002610void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]3a1dc572012-07-31 22:25:132611 extensions::ExtensionHost* host) {
[email protected]406027c02010-09-27 08:03:182612 OrphanedDevTools::iterator iter =
[email protected]2d2f6cfc2011-05-06 21:09:332613 orphaned_dev_tools_.find(host->extension_id());
[email protected]406027c02010-09-27 08:03:182614 if (iter == orphaned_dev_tools_.end())
2615 return;
2616
[email protected]98f66112012-12-25 12:59:362617 DevToolsAgentHost::ConnectRenderViewHost(iter->second,
2618 host->render_view_host());
[email protected]406027c02010-09-27 08:03:182619 orphaned_dev_tools_.erase(iter);
2620}
2621
[email protected]432115822011-07-10 15:52:272622void ExtensionService::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:532623 const content::NotificationSource& source,
2624 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:272625 switch (type) {
[email protected]3c4abc82012-10-22 22:25:542626 case chrome::NOTIFICATION_APP_TERMINATING:
2627 // Shutdown has started. Don't start any more extension installs.
2628 // (We cannot use ExtensionService::Shutdown() for this because it
2629 // happens too late in browser teardown.)
2630 browser_terminating_ = true;
2631 break;
[email protected]432115822011-07-10 15:52:272632 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]6c2381d2011-10-19 02:52:532633 if (profile_ !=
2634 content::Source<Profile>(source).ptr()->GetOriginalProfile()) {
[email protected]bc535ee52010-08-31 18:40:322635 break;
[email protected]6c2381d2011-10-19 02:52:532636 }
[email protected]a4ed6282009-12-14 20:51:162637
[email protected]3a1dc572012-07-31 22:25:132638 extensions::ExtensionHost* host =
2639 content::Details<extensions::ExtensionHost>(details).ptr();
[email protected]f128af42010-08-05 18:05:262640
[email protected]fa2416f2011-05-03 08:41:202641 // Mark the extension as terminated and Unload it. We want it to
2642 // be in a consistent state: either fully working or not loaded
2643 // at all, but never half-crashed. We do it in a PostTask so
2644 // that other handlers of this notification will still have
2645 // access to the Extension and ExtensionHost.
[email protected]14908b72011-04-20 06:54:362646 MessageLoop::current()->PostTask(
2647 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362648 base::Bind(
[email protected]fa2416f2011-05-03 08:41:202649 &ExtensionService::TrackTerminatedExtension,
[email protected]d8c8f25f2011-11-02 18:18:012650 AsWeakPtr(),
[email protected]fa2416f2011-05-03 08:41:202651 host->extension()));
[email protected]31f77262009-12-02 20:48:532652 break;
2653 }
[email protected]432115822011-07-10 15:52:272654 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: {
[email protected]f3b1a082011-11-18 00:34:302655 content::RenderProcessHost* process =
2656 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192657 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302658 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192659 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2660 break;
2661
[email protected]15397b42012-05-16 23:56:062662 // Extensions need to know the channel for API restrictions.
2663 process->Send(new ExtensionMsg_SetChannel(
[email protected]5243df072012-07-11 21:41:012664 extensions::Feature::GetCurrentChannel()));
[email protected]15397b42012-05-16 23:56:062665
[email protected]77a6970c2011-04-23 16:58:562666 // Valid extension function names, used to setup bindings in renderer.
2667 std::vector<std::string> function_names;
2668 ExtensionFunctionDispatcher::GetAllFunctionNames(&function_names);
2669 process->Send(new ExtensionMsg_SetFunctionNames(function_names));
[email protected]31f77262009-12-02 20:48:532670
[email protected]77a6970c2011-04-23 16:58:562671 // Scripting whitelist. This is modified by tests and must be communicated
2672 // to renderers.
2673 process->Send(new ExtensionMsg_SetScriptingWhitelist(
2674 *Extension::GetScriptingWhitelist()));
2675
2676 // Loaded extensions.
[email protected]9776e82e2011-11-15 02:17:532677 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions;
[email protected]84df8332011-12-06 18:22:462678 for (ExtensionSet::const_iterator iter = extensions_.begin();
2679 iter != extensions_.end(); ++iter) {
[email protected]19647262011-12-16 09:57:492680 // Renderers don't need to know about themes.
2681 if (!(*iter)->is_theme())
2682 loaded_extensions.push_back(ExtensionMsg_Loaded_Params(*iter));
[email protected]77a6970c2011-04-23 16:58:562683 }
[email protected]9776e82e2011-11-15 02:17:532684 process->Send(new ExtensionMsg_Loaded(loaded_extensions));
[email protected]77a6970c2011-04-23 16:58:562685 break;
2686 }
[email protected]432115822011-07-10 15:52:272687 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
[email protected]f3b1a082011-11-18 00:34:302688 content::RenderProcessHost* process =
2689 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192690 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302691 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192692 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2693 break;
2694
[email protected]6bc04fd82011-12-04 02:29:352695 process_map_.RemoveAllFromProcess(process->GetID());
[email protected]6f371442011-11-09 06:45:462696 BrowserThread::PostTask(
2697 BrowserThread::IO, FROM_HERE,
2698 base::Bind(&ExtensionInfoMap::UnregisterAllExtensionsInProcess,
[email protected]31d8f5f22012-04-02 15:22:082699 system_->info_map(),
[email protected]f3b1a082011-11-18 00:34:302700 process->GetID()));
[email protected]da5683db2011-04-23 17:12:212701 break;
2702 }
[email protected]d91268022011-08-26 13:17:372703 case chrome::NOTIFICATION_IMPORT_FINISHED: {
[email protected]cf593af2011-12-30 05:44:392704 InitAfterImport();
[email protected]d91268022011-08-26 13:17:372705 break;
2706 }
[email protected]0db124b02012-11-07 04:55:052707 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: {
2708 extensions::ExtensionHost* host =
2709 content::Details<extensions::ExtensionHost>(details).ptr();
[email protected]e7aa7b7e2012-11-27 04:51:222710 std::string extension_id = host->extension_id();
[email protected]399583b2012-12-11 09:33:422711 if (delayed_updates_for_idle_.Contains(extension_id)) {
[email protected]e7aa7b7e2012-11-27 04:51:222712 // We were waiting for this extension to become idle, it now might have,
2713 // so maybe finish installation.
2714 MessageLoop::current()->PostDelayedTask(
[email protected]0db124b02012-11-07 04:55:052715 FROM_HERE,
[email protected]6f6101832012-11-27 22:10:482716 base::Bind(&ExtensionService::MaybeFinishDelayedInstallation,
[email protected]0db124b02012-11-07 04:55:052717 AsWeakPtr(), extension_id),
2718 base::TimeDelta::FromSeconds(kUpdateIdleDelay));
[email protected]0db124b02012-11-07 04:55:052719 }
2720 break;
2721 }
[email protected]92dd8d92013-02-26 00:41:082722 case chrome::NOTIFICATION_UPGRADE_RECOMMENDED: {
2723 // Notify extensions that chrome update is available.
2724 extensions::RuntimeEventRouter::DispatchOnBrowserUpdateAvailableEvent(
2725 profile_);
[email protected]75bdcb872013-03-13 00:41:452726
2727 // Notify observers that chrome update is available.
2728 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
2729 OnChromeUpdateAvailable());
[email protected]92dd8d92013-02-26 00:41:082730 break;
2731 }
[email protected]aa96d3a2010-08-21 08:45:252732
[email protected]4814b512009-11-07 00:12:292733 default:
2734 NOTREACHED() << "Unexpected notification type.";
2735 }
2736}
2737
[email protected]90bb38d2012-11-14 18:36:032738void ExtensionService::OnExtensionInstallPrefChanged() {
[email protected]a6a7ced2012-11-01 17:24:182739 IdentifyAlertableExtensions();
2740 CheckManagementPolicy();
2741}
2742
[email protected]eaa7dd182010-12-14 11:09:002743bool ExtensionService::HasApps() const {
[email protected]ec5b50d2010-10-09 16:35:182744 return !GetAppIds().empty();
2745}
[email protected]377011d2010-07-20 04:18:502746
[email protected]eaa7dd182010-12-14 11:09:002747ExtensionIdSet ExtensionService::GetAppIds() const {
[email protected]ec5b50d2010-10-09 16:35:182748 ExtensionIdSet result;
[email protected]84df8332011-12-06 18:22:462749 for (ExtensionSet::const_iterator it = extensions_.begin();
[email protected]377011d2010-07-20 04:18:502750 it != extensions_.end(); ++it) {
[email protected]1d5e58b2013-01-31 08:41:402751 if ((*it)->is_app() && (*it)->location() != Manifest::COMPONENT)
[email protected]ec5b50d2010-10-09 16:35:182752 result.insert((*it)->id());
[email protected]377011d2010-07-20 04:18:502753 }
2754
[email protected]ec5b50d2010-10-09 16:35:182755 return result;
[email protected]377011d2010-07-20 04:18:502756}
[email protected]d7e9a862010-11-03 21:57:492757
[email protected]dc9a74f72012-08-17 18:07:212758bool ExtensionService::IsBackgroundPageReady(const Extension* extension) const {
[email protected]9367eabc2013-03-01 01:29:292759 if (!extensions::BackgroundInfo::HasPersistentBackgroundPage(extension))
[email protected]dc9a74f72012-08-17 18:07:212760 return true;
2761 ExtensionRuntimeDataMap::const_iterator it =
2762 extension_runtime_data_.find(extension->id());
2763 return it == extension_runtime_data_.end() ? false :
2764 it->second.background_page_ready;
[email protected]d7e9a862010-11-03 21:57:492765}
2766
[email protected]eaa7dd182010-12-14 11:09:002767void ExtensionService::SetBackgroundPageReady(const Extension* extension) {
[email protected]9367eabc2013-03-01 01:29:292768 DCHECK(extensions::BackgroundInfo::HasBackgroundPage(extension));
[email protected]d7e9a862010-11-03 21:57:492769 extension_runtime_data_[extension->id()].background_page_ready = true;
[email protected]ad50def52011-10-19 23:17:072770 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272771 chrome::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY,
[email protected]6c2381d2011-10-19 02:52:532772 content::Source<const Extension>(extension),
[email protected]ad50def52011-10-19 23:17:072773 content::NotificationService::NoDetails());
[email protected]d7e9a862010-11-03 21:57:492774}
2775
[email protected]65992d52012-09-19 23:05:312776void ExtensionService::InspectBackgroundPage(const Extension* extension) {
2777 DCHECK(extension);
2778
[email protected]fdd679b2012-11-15 20:49:392779 ExtensionProcessManager* pm = system_->process_manager();
[email protected]65992d52012-09-19 23:05:312780 extensions::LazyBackgroundTaskQueue* queue =
[email protected]fdd679b2012-11-15 20:49:392781 system_->lazy_background_task_queue();
[email protected]65992d52012-09-19 23:05:312782
2783 extensions::ExtensionHost* host =
2784 pm->GetBackgroundHostForExtension(extension->id());
2785 if (host) {
2786 InspectExtensionHost(host);
2787 } else {
2788 queue->AddPendingTask(
2789 profile_, extension->id(),
2790 base::Bind(&ExtensionService::InspectExtensionHost,
2791 base::Unretained(this)));
2792 }
2793}
2794
[email protected]dc9a74f72012-08-17 18:07:212795bool ExtensionService::IsBeingUpgraded(const Extension* extension) const {
2796 ExtensionRuntimeDataMap::const_iterator it =
2797 extension_runtime_data_.find(extension->id());
2798 return it == extension_runtime_data_.end() ? false :
2799 it->second.being_upgraded;
[email protected]d7e9a862010-11-03 21:57:492800}
2801
[email protected]eaa7dd182010-12-14 11:09:002802void ExtensionService::SetBeingUpgraded(const Extension* extension,
[email protected]b914e2952013-04-26 07:10:032803 bool value) {
[email protected]d7e9a862010-11-03 21:57:492804 extension_runtime_data_[extension->id()].being_upgraded = value;
2805}
[email protected]1bead0712010-11-27 17:41:532806
[email protected]dc9a74f72012-08-17 18:07:212807bool ExtensionService::HasUsedWebRequest(const Extension* extension) const {
2808 ExtensionRuntimeDataMap::const_iterator it =
2809 extension_runtime_data_.find(extension->id());
2810 return it == extension_runtime_data_.end() ? false :
2811 it->second.has_used_webrequest;
[email protected]39a5b532011-10-22 01:47:072812}
2813
2814void ExtensionService::SetHasUsedWebRequest(const Extension* extension,
2815 bool value) {
2816 extension_runtime_data_[extension->id()].has_used_webrequest = value;
2817}
2818
[email protected]cebc3dc2011-04-18 17:15:002819void ExtensionService::RegisterNaClModule(const GURL& url,
2820 const std::string& mime_type) {
2821 NaClModuleInfo info;
2822 info.url = url;
2823 info.mime_type = mime_type;
2824
2825 DCHECK(FindNaClModule(url) == nacl_module_list_.end());
2826 nacl_module_list_.push_front(info);
2827}
2828
2829void ExtensionService::UnregisterNaClModule(const GURL& url) {
2830 NaClModuleInfoList::iterator iter = FindNaClModule(url);
2831 DCHECK(iter != nacl_module_list_.end());
2832 nacl_module_list_.erase(iter);
2833}
2834
[email protected]ed0ba002011-05-26 16:55:132835void ExtensionService::UpdatePluginListWithNaClModules() {
[email protected]300c0ea2011-07-15 23:04:332836 // An extension has been added which has a nacl_module component, which means
2837 // there is a MIME type that module wants to handle, so we need to add that
2838 // MIME type to plugins which handle NaCl modules in order to allow the
2839 // individual modules to handle these types.
[email protected]650b2d52013-02-10 03:41:452840 base::FilePath path;
[email protected]1e127022011-11-29 21:11:472841 if (!PathService::Get(chrome::FILE_NACL_PLUGIN, &path))
2842 return;
2843 const content::PepperPluginInfo* pepper_info =
2844 PluginService::GetInstance()->GetRegisteredPpapiPluginInfo(path);
2845 if (!pepper_info)
2846 return;
[email protected]300c0ea2011-07-15 23:04:332847
[email protected]1e127022011-11-29 21:11:472848 std::vector<webkit::WebPluginMimeType>::const_iterator mime_iter;
2849 // Check each MIME type the plugins handle for the NaCl MIME type.
2850 for (mime_iter = pepper_info->mime_types.begin();
2851 mime_iter != pepper_info->mime_types.end(); ++mime_iter) {
2852 if (mime_iter->mime_type == kNaClPluginMimeType) {
2853 // This plugin handles "application/x-nacl".
[email protected]300c0ea2011-07-15 23:04:332854
[email protected]1e127022011-11-29 21:11:472855 PluginService::GetInstance()->
2856 UnregisterInternalPlugin(pepper_info->path);
[email protected]300c0ea2011-07-15 23:04:332857
[email protected]1e127022011-11-29 21:11:472858 webkit::WebPluginInfo info = pepper_info->ToWebPluginInfo();
[email protected]300c0ea2011-07-15 23:04:332859
[email protected]1e127022011-11-29 21:11:472860 for (ExtensionService::NaClModuleInfoList::const_iterator iter =
2861 nacl_module_list_.begin();
2862 iter != nacl_module_list_.end(); ++iter) {
2863 // Add the MIME type specified in the extension to this NaCl plugin,
2864 // With an extra "nacl" argument to specify the location of the NaCl
2865 // manifest file.
2866 webkit::WebPluginMimeType mime_type_info;
2867 mime_type_info.mime_type = iter->mime_type;
2868 mime_type_info.additional_param_names.push_back(UTF8ToUTF16("nacl"));
2869 mime_type_info.additional_param_values.push_back(
2870 UTF8ToUTF16(iter->url.spec()));
2871 info.mime_types.push_back(mime_type_info);
[email protected]300c0ea2011-07-15 23:04:332872 }
[email protected]1e127022011-11-29 21:11:472873
2874 PluginService::GetInstance()->RefreshPlugins();
[email protected]c6f3dea2012-01-14 02:23:112875 PluginService::GetInstance()->RegisterInternalPlugin(info, true);
[email protected]1e127022011-11-29 21:11:472876 // This plugin has been modified, no need to check the rest of its
2877 // types, but continue checking other plugins.
2878 break;
[email protected]ed0ba002011-05-26 16:55:132879 }
2880 }
[email protected]ed0ba002011-05-26 16:55:132881}
2882
[email protected]cebc3dc2011-04-18 17:15:002883ExtensionService::NaClModuleInfoList::iterator
2884 ExtensionService::FindNaClModule(const GURL& url) {
2885 for (NaClModuleInfoList::iterator iter = nacl_module_list_.begin();
2886 iter != nacl_module_list_.end(); ++iter) {
2887 if (iter->url == url)
2888 return iter;
2889 }
2890 return nacl_module_list_.end();
2891}
[email protected]6cafe35a2012-08-27 22:40:502892
[email protected]32b36c62012-10-24 05:37:412893void ExtensionService::DoPostLoadTasks(const Extension* extension) {
2894 std::map<std::string, int>::iterator it =
2895 on_load_events_.find(extension->id());
2896 if (it == on_load_events_.end())
2897 return;
2898
2899 int events_to_fire = it->second;
2900 extensions::LazyBackgroundTaskQueue* queue =
2901 system_->lazy_background_task_queue();
2902 if (queue->ShouldEnqueueTask(profile(), extension)) {
2903 if (events_to_fire & EVENT_LAUNCHED)
[email protected]6cafe35a2012-08-27 22:40:502904 queue->AddPendingTask(profile(), extension->id(),
2905 base::Bind(&ExtensionService::LaunchApplication));
[email protected]fc2a40f2013-03-13 13:14:572906 if (events_to_fire & EVENT_RESTARTED) {
[email protected]32b36c62012-10-24 05:37:412907 queue->AddPendingTask(profile(), extension->id(),
[email protected]961745f2013-05-25 14:09:242908 base::Bind(&ExtensionService::RestartApplication));
[email protected]fc2a40f2013-03-13 13:14:572909 }
[email protected]6cafe35a2012-08-27 22:40:502910 }
[email protected]32b36c62012-10-24 05:37:412911
2912 on_load_events_.erase(it);
[email protected]6cafe35a2012-08-27 22:40:502913}
2914
2915// static
2916void ExtensionService::LaunchApplication(
2917 extensions::ExtensionHost* extension_host) {
2918 if (!extension_host)
2919 return;
2920
2921#if !defined(OS_ANDROID)
2922 extensions::LaunchPlatformApp(extension_host->profile(),
2923 extension_host->extension(),
[email protected]650b2d52013-02-10 03:41:452924 NULL, base::FilePath());
[email protected]6cafe35a2012-08-27 22:40:502925#endif
2926}
[email protected]65992d52012-09-19 23:05:312927
[email protected]32b36c62012-10-24 05:37:412928// static
2929void ExtensionService::RestartApplication(
[email protected]32b36c62012-10-24 05:37:412930 extensions::ExtensionHost* extension_host) {
2931 if (!extension_host)
2932 return;
2933
2934#if !defined(OS_ANDROID)
[email protected]961745f2013-05-25 14:09:242935 extensions::RestartPlatformApp(
2936 extension_host->profile(), extension_host->extension());
[email protected]32b36c62012-10-24 05:37:412937#endif
2938}
2939
2940bool ExtensionService::HasShellWindows(const std::string& extension_id) {
2941 const Extension* current_extension = GetExtensionById(extension_id, false);
2942 return current_extension && current_extension->is_platform_app() &&
2943 !extensions::ShellWindowRegistry::Get(profile_)->
2944 GetShellWindowsForApp(extension_id).empty();
2945}
2946
[email protected]65992d52012-09-19 23:05:312947void ExtensionService::InspectExtensionHost(
2948 extensions::ExtensionHost* host) {
2949 if (host)
2950 DevToolsWindow::OpenDevToolsWindow(host->render_view_host());
2951}
[email protected]612a1cb12012-10-17 13:18:032952
2953bool ExtensionService::ShouldEnableOnInstall(const Extension* extension) {
2954 // Extensions installed by policy can't be disabled. So even if a previous
2955 // installation disabled the extension, make sure it is now enabled.
2956 if (system_->management_policy()->MustRemainEnabled(extension, NULL))
2957 return true;
2958
2959 if (extension_prefs_->IsExtensionDisabled(extension->id()))
2960 return false;
2961
[email protected]41070e8d2012-10-24 01:34:362962 if (FeatureSwitch::prompt_for_external_extensions()->IsEnabled()) {
[email protected]612a1cb12012-10-17 13:18:032963 // External extensions are initially disabled. We prompt the user before
[email protected]2c495c42013-01-04 21:49:542964 // enabling them. Hosted apps are excepted because they are not dangerous
2965 // (they need to be launched by the user anyway).
[email protected]1d5e58b2013-01-31 08:41:402966 if (extension->GetType() != Manifest::TYPE_HOSTED_APP &&
2967 Manifest::IsExternalLocation(extension->location()) &&
[email protected]612a1cb12012-10-17 13:18:032968 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id())) {
2969 return false;
2970 }
2971 }
[email protected]612a1cb12012-10-17 13:18:032972
2973 return true;
2974}
[email protected]0db124b02012-11-07 04:55:052975
2976bool ExtensionService::IsExtensionIdle(const std::string& extension_id) const {
[email protected]d9a61e12012-11-14 02:43:472977 ExtensionProcessManager* process_manager = system_->process_manager();
[email protected]fc332ae2012-11-14 20:17:332978 DCHECK(process_manager);
[email protected]0db124b02012-11-07 04:55:052979 extensions::ExtensionHost* host =
[email protected]d9a61e12012-11-14 02:43:472980 process_manager->GetBackgroundHostForExtension(extension_id);
[email protected]e7aa7b7e2012-11-27 04:51:222981 if (host)
2982 return false;
2983 return process_manager->GetRenderViewHostsForExtension(extension_id).empty();
2984}
2985
2986bool ExtensionService::ShouldDelayExtensionUpdate(
2987 const std::string& extension_id,
2988 bool wait_for_idle) const {
2989 const char kOnUpdateAvailableEvent[] = "runtime.onUpdateAvailable";
2990
2991 // If delayed updates are globally disabled, or just for this extension,
2992 // don't delay.
2993 if (!install_updates_when_idle_ || !wait_for_idle)
2994 return false;
2995
[email protected]695b5712012-12-06 23:55:282996 const Extension* old = GetInstalledExtension(extension_id);
[email protected]e7aa7b7e2012-11-27 04:51:222997 // If there is no old extension, this is not an update, so don't delay.
2998 if (!old)
2999 return false;
3000
[email protected]9367eabc2013-03-01 01:29:293001 if (extensions::BackgroundInfo::HasPersistentBackgroundPage(old)) {
[email protected]e7aa7b7e2012-11-27 04:51:223002 // Delay installation if the extension listens for the onUpdateAvailable
3003 // event.
3004 return system_->event_router()->ExtensionHasEventListener(
3005 extension_id, kOnUpdateAvailableEvent);
3006 } else {
3007 // Delay installation if the extension is not idle.
3008 return !IsExtensionIdle(extension_id);
3009 }
[email protected]0db124b02012-11-07 04:55:053010}
[email protected]fdd679b2012-11-15 20:49:393011
[email protected]399583b2012-12-11 09:33:423012void ExtensionService::GarbageCollectIsolatedStorage() {
[email protected]650b2d52013-02-10 03:41:453013 scoped_ptr<base::hash_set<base::FilePath> > active_paths(
3014 new base::hash_set<base::FilePath>());
[email protected]399583b2012-12-11 09:33:423015 for (ExtensionSet::const_iterator it = extensions_.begin();
3016 it != extensions_.end(); ++it) {
[email protected]561e33d52013-04-03 06:58:433017 if (extensions::AppIsolationInfo::HasIsolatedStorage(*it)) {
[email protected]399583b2012-12-11 09:33:423018 active_paths->insert(
3019 BrowserContext::GetStoragePartitionForSite(
3020 profile_,
3021 GetSiteForExtensionId((*it)->id()))->GetPath());
3022 }
3023 }
3024
3025 DCHECK(!installs_delayed());
3026 set_installs_delayed(true);
3027 BrowserContext::GarbageCollectStoragePartitions(
3028 profile_, active_paths.Pass(),
3029 base::Bind(&ExtensionService::OnGarbageCollectIsolatedStorageFinished,
3030 AsWeakPtr()));
3031}
3032
3033void ExtensionService::OnGarbageCollectIsolatedStorageFinished() {
3034 set_installs_delayed(false);
3035 for (ExtensionSet::const_iterator it = delayed_installs_.begin();
3036 it != delayed_installs_.end();
3037 ++it) {
3038 FinishDelayedInstallation((*it)->id());
3039 }
3040 for (ExtensionSet::const_iterator it = delayed_updates_for_idle_.begin();
3041 it != delayed_updates_for_idle_.end();
3042 ++it) {
3043 MaybeFinishDelayedInstallation((*it)->id());
3044 }
3045 delayed_installs_.Clear();
3046}
3047
3048void ExtensionService::OnNeedsToGarbageCollectIsolatedStorage() {
3049 extension_prefs_->SetNeedsStorageGarbageCollection(true);
3050}
3051
[email protected]fdd679b2012-11-15 20:49:393052void ExtensionService::OnBlacklistUpdated() {
[email protected]695b5712012-12-06 23:55:283053 blacklist_->GetBlacklistedIDs(
3054 GenerateInstalledExtensionsSet()->GetIDs(),
3055 base::Bind(&ExtensionService::ManageBlacklist,
3056 AsWeakPtr(),
3057 blacklisted_extensions_.GetIDs()));
3058}
3059
3060void ExtensionService::ManageBlacklist(
3061 const std::set<std::string>& old_blacklisted_ids,
3062 const std::set<std::string>& new_blacklisted_ids) {
3063 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
3064
3065 std::set<std::string> no_longer_blacklisted;
3066 std::set_difference(old_blacklisted_ids.begin(), old_blacklisted_ids.end(),
3067 new_blacklisted_ids.begin(), new_blacklisted_ids.end(),
3068 std::inserter(no_longer_blacklisted,
3069 no_longer_blacklisted.begin()));
3070 std::set<std::string> not_yet_blacklisted;
3071 std::set_difference(new_blacklisted_ids.begin(), new_blacklisted_ids.end(),
3072 old_blacklisted_ids.begin(), old_blacklisted_ids.end(),
3073 std::inserter(not_yet_blacklisted,
3074 not_yet_blacklisted.begin()));
3075
3076 for (std::set<std::string>::iterator it = no_longer_blacklisted.begin();
3077 it != no_longer_blacklisted.end(); ++it) {
3078 scoped_refptr<const Extension> extension =
3079 blacklisted_extensions_.GetByID(*it);
3080 DCHECK(extension);
3081 if (!extension)
3082 continue;
3083 blacklisted_extensions_.Remove(*it);
3084 AddExtension(extension);
[email protected]ac875372013-02-28 04:36:093085 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.UnblacklistInstalled",
3086 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:283087 }
3088
3089 for (std::set<std::string>::iterator it = not_yet_blacklisted.begin();
3090 it != not_yet_blacklisted.end(); ++it) {
3091 scoped_refptr<const Extension> extension = GetInstalledExtension(*it);
3092 DCHECK(extension);
3093 if (!extension)
3094 continue;
3095 blacklisted_extensions_.Insert(extension);
3096 UnloadExtension(*it, extension_misc::UNLOAD_REASON_BLACKLIST);
[email protected]ac875372013-02-28 04:36:093097 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.BlacklistInstalled",
3098 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:283099 }
3100
3101 IdentifyAlertableExtensions();
[email protected]fdd679b2012-11-15 20:49:393102}
[email protected]75bdcb872013-03-13 00:41:453103
3104void ExtensionService::AddUpdateObserver(extensions::UpdateObserver* observer) {
3105 update_observers_.AddObserver(observer);
3106}
3107
3108void ExtensionService::RemoveUpdateObserver(
3109 extensions::UpdateObserver* observer) {
3110 update_observers_.RemoveObserver(observer);
3111}