blob: 11e2c9ddd3e9dd822e0924b144d5c1b00a4029ab [file] [log] [blame]
[email protected]16d900042012-01-04 21:24:321// Copyright (c) 2012 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]7286e3fc2011-07-19 22:13:2419#include "base/stl_util.h"
[email protected]e83326f2010-07-31 17:29:2520#include "base/string_number_conversions.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]7f8f24f2012-11-15 19:40:1423#include "base/threading/sequenced_worker_pool.h"
[email protected]34b99632011-01-01 01:01:0624#include "base/threading/thread_restrictions.h"
[email protected]cc2c3432009-11-06 17:24:3625#include "base/time.h"
[email protected]ce7f62e32010-08-10 23:43:5926#include "base/utf_string_conversions.h"
[email protected]cc655912009-01-29 23:19:1927#include "base/values.h"
[email protected]aa142702010-03-26 01:26:3328#include "base/version.h"
[email protected]a692a952011-11-28 08:23:5429#include "chrome/browser/accessibility/accessibility_extension_api.h"
[email protected]e07f2da2012-08-01 22:46:1230#include "chrome/browser/bookmarks/bookmark_model_factory.h"
[email protected]15730c42009-09-03 00:03:2031#include "chrome/browser/browser_process.h"
[email protected]dfba8762011-09-02 12:49:5432#include "chrome/browser/chrome_plugin_service_filter.h"
[email protected]70019152012-12-19 11:44:1933#include "chrome/browser/devtools/devtools_window.h"
[email protected]32b36c62012-10-24 05:37:4134#include "chrome/browser/extensions/api/app_runtime/app_runtime_api.h"
[email protected]895a1e52012-05-15 02:50:1235#include "chrome/browser/extensions/api/declarative/rules_registry_service.h"
[email protected]c77f2352012-08-08 22:07:5836#include "chrome/browser/extensions/api/extension_action/extension_actions_api.h"
[email protected]7e6935b2012-12-17 22:11:1937#include "chrome/browser/extensions/api/push_messaging/push_messaging_api_factory.h"
[email protected]e9f541a2012-11-19 21:52:3138#include "chrome/browser/extensions/api/runtime/runtime_api.h"
[email protected]af9db5f2011-10-05 05:13:1539#include "chrome/browser/extensions/app_notification_manager.h"
[email protected]5db9ada2012-04-11 13:48:2040#include "chrome/browser/extensions/app_sync_data.h"
[email protected]5a38dfd2012-07-23 23:22:1041#include "chrome/browser/extensions/browser_event_router.h"
[email protected]d8c8f25f2011-11-02 18:18:0142#include "chrome/browser/extensions/component_loader.h"
[email protected]e0785902011-05-19 23:34:1743#include "chrome/browser/extensions/crx_installer.h"
[email protected]d9ede582012-08-14 19:21:3844#include "chrome/browser/extensions/data_deleter.h"
[email protected]62f051c2012-03-29 17:04:4445#include "chrome/browser/extensions/extension_disabled_ui.h"
[email protected]14a000d2010-04-29 21:44:2446#include "chrome/browser/extensions/extension_error_reporter.h"
[email protected]89226982012-07-16 20:09:1847#include "chrome/browser/extensions/extension_error_ui.h"
[email protected]b1748b1d82009-11-30 20:32:5648#include "chrome/browser/extensions/extension_host.h"
[email protected]00b38242012-07-18 18:43:2249#include "chrome/browser/extensions/extension_install_ui.h"
[email protected]4814b512009-11-07 00:12:2950#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]3569b502012-01-12 20:08:2351#include "chrome/browser/extensions/extension_sorting.h"
[email protected]19eb80152011-02-26 00:28:4352#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]90310d92011-04-17 07:35:0453#include "chrome/browser/extensions/extension_sync_data.h"
[email protected]31d8f5f22012-04-02 15:22:0854#include "chrome/browser/extensions/extension_system.h"
[email protected]8f9d4eb2011-02-05 01:39:1055#include "chrome/browser/extensions/extension_web_ui.h"
[email protected]612a1cb12012-10-17 13:18:0356#include "chrome/browser/extensions/external_install_ui.h"
[email protected]5df038b2012-07-16 19:03:2757#include "chrome/browser/extensions/external_provider_impl.h"
58#include "chrome/browser/extensions/external_provider_interface.h"
[email protected]d8c8f25f2011-11-02 18:18:0159#include "chrome/browser/extensions/installed_loader.h"
[email protected]6cafe35a2012-08-27 22:40:5060#include "chrome/browser/extensions/lazy_background_task_queue.h"
[email protected]fdd679b2012-11-15 20:49:3961#include "chrome/browser/extensions/management_policy.h"
[email protected]b2907fd2011-03-25 16:43:3762#include "chrome/browser/extensions/pending_extension_manager.h"
[email protected]c333e792012-01-06 16:57:3963#include "chrome/browser/extensions/permissions_updater.h"
[email protected]6cafe35a2012-08-27 22:40:5064#include "chrome/browser/extensions/platform_app_launcher.h"
[email protected]cccdf0aa2011-11-11 03:43:3865#include "chrome/browser/extensions/settings/settings_frontend.h"
[email protected]6cafe35a2012-08-27 22:40:5066#include "chrome/browser/extensions/shell_window_registry.h"
[email protected]d8c8f25f2011-11-02 18:18:0167#include "chrome/browser/extensions/unpacked_installer.h"
[email protected]42a08162012-03-16 18:09:1168#include "chrome/browser/extensions/updater/extension_updater.h"
[email protected]56ad3792010-05-28 17:45:3369#include "chrome/browser/net/chrome_url_request_context.h"
[email protected]37858e52010-08-26 00:22:0270#include "chrome/browser/prefs/pref_service.h"
[email protected]8ecad5e2010-12-02 21:18:3371#include "chrome/browser/profiles/profile.h"
[email protected]cf593af2011-12-30 05:44:3972#include "chrome/browser/profiles/profile_manager.h"
[email protected]d5949312012-12-03 22:13:3073#include "chrome/browser/themes/theme_service.h"
74#include "chrome/browser/themes/theme_service_factory.h"
[email protected]c8d407e2011-04-28 21:27:1775#include "chrome/browser/ui/webui/chrome_url_data_manager.h"
76#include "chrome/browser/ui/webui/favicon_source.h"
[email protected]5eddc3e2011-10-26 04:33:3177#include "chrome/browser/ui/webui/ntp/thumbnail_source.h"
[email protected]b07e606e2012-09-15 20:16:1578#include "chrome/browser/ui/webui/theme_source.h"
[email protected]aab98a52009-12-02 03:22:3579#include "chrome/common/child_process_logging.h"
[email protected]432115822011-07-10 15:52:2780#include "chrome/common/chrome_notification_types.h"
[email protected]1e127022011-11-29 21:11:4781#include "chrome/common/chrome_paths.h"
[email protected]e2eb43112009-05-29 21:19:5482#include "chrome/common/chrome_switches.h"
[email protected]15397b42012-05-16 23:56:0683#include "chrome/common/chrome_version_info.h"
[email protected]5b1a0e22009-05-26 19:00:5884#include "chrome/common/extensions/extension.h"
[email protected]7c927b62010-02-24 09:54:1385#include "chrome/common/extensions/extension_file_util.h"
[email protected]895a1e52012-05-15 02:50:1286#include "chrome/common/extensions/extension_manifest_constants.h"
[email protected]77a6970c2011-04-23 16:58:5687#include "chrome/common/extensions/extension_messages.h"
[email protected]a315ba92010-11-16 14:12:2188#include "chrome/common/extensions/extension_resource.h"
[email protected]612a1cb12012-10-17 13:18:0389#include "chrome/common/extensions/feature_switch.h"
[email protected]e9f541a2012-11-19 21:52:3190#include "chrome/common/extensions/features/feature.h"
[email protected]0db124b02012-11-07 04:55:0591#include "chrome/common/extensions/manifest.h"
[email protected]25b34332009-06-05 21:53:1992#include "chrome/common/pref_names.h"
[email protected]a57209872009-05-04 22:53:1493#include "chrome/common/url_constants.h"
[email protected]c38831a12011-10-28 12:44:4994#include "content/public/browser/browser_thread.h"
[email protected]0e12d7d2011-12-01 16:21:4495#include "content/public/browser/devtools_agent_host_registry.h"
96#include "content/public/browser/devtools_manager.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]1e127022011-11-29 21:11:47103#include "content/public/common/pepper_plugin_info.h"
[email protected]e9f541a2012-11-19 21:52:31104#include "extensions/common/error_utils.h"
[email protected]c10da4b02010-03-25 14:38:32105#include "googleurl/src/gurl.h"
[email protected]65187152012-06-02 13:14:14106#include "grit/generated_resources.h"
[email protected]be28b5f42012-07-20 11:31:25107#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
[email protected]895a1e52012-05-15 02:50:12108#include "sync/api/sync_change.h"
109#include "sync/api/sync_error_factory.h"
[email protected]24b538a2010-02-27 01:22:44110#include "webkit/database/database_tracker.h"
111#include "webkit/database/database_util.h"
[email protected]79a60642012-10-20 21:03:18112
[email protected]eed367e2011-04-12 03:43:31113#if defined(OS_CHROMEOS)
[email protected]14da67b02011-08-02 05:49:13114#include "chrome/browser/chromeos/cros/cros_library.h"
[email protected]eed367e2011-04-12 03:43:31115#include "chrome/browser/chromeos/extensions/file_browser_event_router.h"
[email protected]ce9802042011-05-27 10:11:40116#include "chrome/browser/chromeos/extensions/media_player_event_router.h"
[email protected]14da67b02011-08-02 05:49:13117#include "chrome/browser/chromeos/input_method/input_method_manager.h"
[email protected]1364c6d2012-07-24 11:57:19118#include "chrome/browser/extensions/api/input_ime/input_ime_api.h"
[email protected]10eb28162012-09-18 03:04:09119#include "content/public/browser/storage_partition.h"
[email protected]b777b332011-04-16 04:01:08120#include "webkit/fileapi/file_system_context.h"
121#include "webkit/fileapi/file_system_mount_point_provider.h"
[email protected]eed367e2011-04-12 03:43:31122#endif
123
[email protected]55eb70e762012-02-20 17:38:39124using content::BrowserContext;
[email protected]631bb742011-11-02 11:29:39125using content::BrowserThread;
[email protected]0e12d7d2011-12-01 16:21:44126using content::DevToolsAgentHost;
127using content::DevToolsAgentHostRegistry;
[email protected]3a5180ae2011-12-21 02:39:38128using content::PluginService;
[email protected]bf3d9df2012-07-24 23:20:27129using extensions::CrxInstaller;
[email protected]1c321ee52012-05-21 03:02:34130using extensions::Extension;
131using extensions::ExtensionIdSet;
132using extensions::ExtensionInfo;
[email protected]215a7be2012-10-22 19:53:42133using extensions::FeatureSwitch;
[email protected]c2e66e12012-06-27 06:27:06134using extensions::PermissionMessage;
135using extensions::PermissionMessages;
136using extensions::PermissionSet;
[email protected]e410b5f2012-12-14 14:02:24137using extensions::UnloadedExtensionInfo;
[email protected]5ef47ec2010-01-28 05:58:05138
[email protected]c6d474f82009-12-16 21:11:06139namespace errors = extension_manifest_errors;
140
[email protected]b6ab96d2009-08-20 18:58:19141namespace {
142
[email protected]d96eb512012-11-01 23:44:08143// Histogram values for logging events related to externally installed
144// extensions.
145enum ExternalExtensionEvent {
146 EXTERNAL_EXTENSION_INSTALLED = 0,
147 EXTERNAL_EXTENSION_IGNORED,
148 EXTERNAL_EXTENSION_REENABLED,
149 EXTERNAL_EXTENSION_UNINSTALLED,
150 EXTERNAL_EXTENSION_BUCKET_BOUNDARY,
151};
152
[email protected]612a1cb12012-10-17 13:18:03153// Prompt the user this many times before considering an extension acknowledged.
154static const int kMaxExtensionAcknowledgePromptCount = 3;
155
[email protected]0db124b02012-11-07 04:55:05156// Wait this many seconds after an extensions becomes idle before updating it.
157static const int kUpdateIdleDelay = 5;
158
[email protected]9bd9a6862012-11-29 09:24:22159// Wait this many seconds before trying to garbage collect extensions again.
160static const int kGarbageCollectRetryDelay = 30;
161
[email protected]300c0ea2011-07-15 23:04:33162const char* kNaClPluginMimeType = "application/x-nacl";
163
[email protected]3bdba0d2011-08-23 07:17:30164static bool IsSyncableExtension(const Extension& extension) {
165 return extension.GetSyncType() == Extension::SYNC_TYPE_EXTENSION;
166}
167
168static bool IsSyncableApp(const Extension& extension) {
169 return extension.GetSyncType() == Extension::SYNC_TYPE_APP;
170}
171
[email protected]c6d474f82009-12-16 21:11:06172} // namespace
[email protected]b6ab96d2009-08-20 18:58:19173
[email protected]eaa7dd182010-12-14 11:09:00174ExtensionService::ExtensionRuntimeData::ExtensionRuntimeData()
[email protected]d7e9a862010-11-03 21:57:49175 : background_page_ready(false),
[email protected]f8bf5992011-10-24 22:38:57176 being_upgraded(false),
177 has_used_webrequest(false) {
[email protected]d7e9a862010-11-03 21:57:49178}
179
[email protected]eaa7dd182010-12-14 11:09:00180ExtensionService::ExtensionRuntimeData::~ExtensionRuntimeData() {
[email protected]d7e9a862010-11-03 21:57:49181}
182
[email protected]cebc3dc2011-04-18 17:15:00183ExtensionService::NaClModuleInfo::NaClModuleInfo() {
184}
185
186ExtensionService::NaClModuleInfo::~NaClModuleInfo() {
187}
188
[email protected]eaa7dd182010-12-14 11:09:00189// ExtensionService.
[email protected]6014d672008-12-05 00:38:25190
[email protected]31bb5ee62012-09-12 22:58:40191const char ExtensionService::kInstallDirectoryName[] = "Extensions";
[email protected]15d5b472011-11-24 04:17:24192
[email protected]31bb5ee62012-09-12 22:58:40193const char ExtensionService::kLocalAppSettingsDirectoryName[] =
[email protected]15d5b472011-11-24 04:17:24194 "Local App Settings";
[email protected]31bb5ee62012-09-12 22:58:40195const char ExtensionService::kLocalExtensionSettingsDirectoryName[] =
[email protected]15d5b472011-11-24 04:17:24196 "Local Extension Settings";
[email protected]31bb5ee62012-09-12 22:58:40197const char ExtensionService::kSyncAppSettingsDirectoryName[] =
[email protected]15d5b472011-11-24 04:17:24198 "Sync App Settings";
[email protected]31bb5ee62012-09-12 22:58:40199const char ExtensionService::kSyncExtensionSettingsDirectoryName[] =
[email protected]15d5b472011-11-24 04:17:24200 "Sync Extension Settings";
[email protected]675bb01012012-12-14 22:03:29201const char ExtensionService::kManagedSettingsDirectoryName[] =
202 "Managed Extension Settings";
[email protected]31bb5ee62012-09-12 22:58:40203const char ExtensionService::kStateStoreName[] = "Extension State";
[email protected]494c06e2009-07-25 01:06:42204
[email protected]8e4560b62011-01-14 10:09:14205void ExtensionService::CheckExternalUninstall(const std::string& id) {
[email protected]a29a517a2011-01-21 21:11:12206 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20207
[email protected]7a4c6852010-09-16 03:44:22208 // Check if the providers know about this extension.
[email protected]5df038b2012-07-16 19:03:27209 extensions::ProviderCollection::const_iterator i;
[email protected]0a60a2e2010-10-25 16:15:21210 for (i = external_extension_providers_.begin();
211 i != external_extension_providers_.end(); ++i) {
[email protected]8e4560b62011-01-14 10:09:14212 DCHECK(i->get()->IsReady());
[email protected]0a60a2e2010-10-25 16:15:21213 if (i->get()->HasExtension(id))
214 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22215 }
216
[email protected]0f48fca2011-05-19 18:46:35217 // We get the list of external extensions to check from preferences.
218 // It is possible that an extension has preferences but is not loaded.
219 // For example, an extension that requires experimental permissions
220 // will not be loaded if the experimental command line flag is not used.
221 // In this case, do not uninstall.
[email protected]31bb5ee62012-09-12 22:58:40222 if (!GetInstalledExtension(id)) {
[email protected]0f48fca2011-05-19 18:46:35223 // We can't call UninstallExtension with an unloaded/invalid
224 // extension ID.
225 LOG(WARNING) << "Attempted uninstallation of unloaded/invalid extension "
226 << "with id: " << id;
227 return;
228 }
[email protected]d6ebc9792011-04-07 18:18:33229 UninstallExtension(id, true, NULL);
[email protected]7a4c6852010-09-16 03:44:22230}
231
[email protected]7f8f24f2012-11-15 19:40:14232void ExtensionService::SetFileTaskRunnerForTesting(
233 base::SequencedTaskRunner* task_runner) {
234 file_task_runner_ = task_runner;
235}
236
[email protected]8e4560b62011-01-14 10:09:14237void ExtensionService::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22238 external_extension_providers_.clear();
239}
240
[email protected]8e4560b62011-01-14 10:09:14241void ExtensionService::AddProviderForTesting(
[email protected]5df038b2012-07-16 19:03:27242 extensions::ExternalProviderInterface* test_provider) {
[email protected]a29a517a2011-01-21 21:11:12243 CHECK(test_provider);
[email protected]0a60a2e2010-10-25 16:15:21244 external_extension_providers_.push_back(
[email protected]5df038b2012-07-16 19:03:27245 linked_ptr<extensions::ExternalProviderInterface>(test_provider));
[email protected]7a4c6852010-09-16 03:44:22246}
247
[email protected]9060d8b02012-01-13 02:14:30248bool ExtensionService::OnExternalExtensionUpdateUrlFound(
[email protected]7a4c6852010-09-16 03:44:22249 const std::string& id,
[email protected]21a5a672010-11-04 10:47:42250 const GURL& update_url,
251 Extension::Location location) {
[email protected]ab22ba42011-01-14 16:36:38252 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
253 CHECK(Extension::IdIsValid(id));
[email protected]a8af9fdb2010-10-28 21:52:20254
[email protected]8a87a5332011-08-11 17:54:59255 const Extension* extension = GetExtensionById(id, true);
256 if (extension) {
257 // Already installed. Skip this install if the current location has
258 // higher priority than |location|.
259 Extension::Location current = extension->location();
260 if (current == Extension::GetHigherPriorityLocation(current, location))
[email protected]9060d8b02012-01-13 02:14:30261 return false;
[email protected]8a87a5332011-08-11 17:54:59262 // Otherwise, overwrite the current installation.
[email protected]7a4c6852010-09-16 03:44:22263 }
[email protected]9060d8b02012-01-13 02:14:30264
265 // Add |id| to the set of pending extensions. If it can not be added,
266 // then there is already a pending record from a higher-priority install
267 // source. In this case, signal that this extension will not be
268 // installed by returning false.
269 if (!pending_extension_manager()->AddFromExternalUpdateUrl(
[email protected]31bb5ee62012-09-12 22:58:40270 id, update_url, location)) {
[email protected]9060d8b02012-01-13 02:14:30271 return false;
[email protected]31bb5ee62012-09-12 22:58:40272 }
[email protected]9060d8b02012-01-13 02:14:30273
[email protected]94fde232012-04-27 10:22:30274 update_once_all_providers_are_ready_ = true;
[email protected]9060d8b02012-01-13 02:14:30275 return true;
[email protected]7a4c6852010-09-16 03:44:22276}
277
[email protected]dc9a74f72012-08-17 18:07:21278const Extension* ExtensionService::GetInstalledApp(const GURL& url) const {
[email protected]615d88f2011-12-13 01:47:44279 const Extension* extension = extensions_.GetExtensionOrAppByURL(
280 ExtensionURLInfo(url));
[email protected]dc9a74f72012-08-17 18:07:21281 return (extension && extension->is_app()) ? extension : NULL;
[email protected]d9696672011-03-15 22:45:09282}
283
[email protected]dc9a74f72012-08-17 18:07:21284bool ExtensionService::IsInstalledApp(const GURL& url) const {
[email protected]d9696672011-03-15 22:45:09285 return !!GetInstalledApp(url);
[email protected]ffb204f22010-12-05 23:20:27286}
287
[email protected]404fb002012-10-02 21:06:17288const Extension* ExtensionService::GetIsolatedAppForRenderer(
[email protected]dc9a74f72012-08-17 18:07:21289 int renderer_child_id) const {
[email protected]404fb002012-10-02 21:06:17290 std::set<std::string> extension_ids =
291 process_map_.GetExtensionsInProcess(renderer_child_id);
292 // All apps in one process share the same partition.
293 // It is only possible for the app to have isolated storage
294 // if there is only 1 app in the process.
295 if (extension_ids.size() != 1)
296 return NULL;
297
298 const extensions::Extension* extension =
299 extensions_.GetByID(*(extension_ids.begin()));
300 // We still need to check is_storage_isolated(),
301 // because it's common for there to be one extension in a process
302 // with is_storage_isolated() == false.
303 if (extension && extension->is_storage_isolated())
304 return extension;
305
306 return NULL;
[email protected]da5683db2011-04-23 17:12:21307}
308
[email protected]6aeac8342010-10-01 20:21:18309// static
[email protected]d6ebc9792011-04-07 18:18:33310// This function is used to implement the command-line switch
[email protected]3bdba0d2011-08-23 07:17:30311// --uninstall-extension, and to uninstall an extension via sync. The LOG
312// statements within this function are used to inform the user if the uninstall
313// cannot be done.
[email protected]eaa7dd182010-12-14 11:09:00314bool ExtensionService::UninstallExtensionHelper(
315 ExtensionService* extensions_service,
[email protected]6aeac8342010-10-01 20:21:18316 const std::string& extension_id) {
[email protected]d6ebc9792011-04-07 18:18:33317 // We can't call UninstallExtension with an invalid extension ID.
[email protected]31bb5ee62012-09-12 22:58:40318 if (!extensions_service->GetInstalledExtension(extension_id)) {
[email protected]6aeac8342010-10-01 20:21:18319 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]95da88c42011-03-31 10:07:33320 << "id: " << extension_id;
321 return false;
[email protected]6aeac8342010-10-01 20:21:18322 }
323
[email protected]d6ebc9792011-04-07 18:18:33324 // The following call to UninstallExtension will not allow an uninstall of a
325 // policy-controlled extension.
[email protected]65187152012-06-02 13:14:14326 string16 error;
[email protected]d6ebc9792011-04-07 18:18:33327 if (!extensions_service->UninstallExtension(extension_id, false, &error)) {
328 LOG(WARNING) << "Cannot uninstall extension with id " << extension_id
329 << ": " << error;
330 return false;
331 }
[email protected]95da88c42011-03-31 10:07:33332
[email protected]6aeac8342010-10-01 20:21:18333 return true;
334}
335
[email protected]eaa7dd182010-12-14 11:09:00336ExtensionService::ExtensionService(Profile* profile,
[email protected]f0841cd2011-01-19 15:07:24337 const CommandLine* command_line,
338 const FilePath& install_directory,
[email protected]45759612012-07-10 17:21:23339 extensions::ExtensionPrefs* extension_prefs,
[email protected]fdd679b2012-11-15 20:49:39340 extensions::Blacklist* blacklist,
[email protected]0436b102011-04-15 18:30:03341 bool autoupdate_enabled,
342 bool extensions_enabled)
[email protected]fdd679b2012-11-15 20:49:39343 : extensions::Blacklist::Observer(blacklist),
344 profile_(profile),
[email protected]bd306722012-07-11 20:43:59345 system_(extensions::ExtensionSystem::Get(profile)),
[email protected]73c47932010-12-06 18:13:43346 extension_prefs_(extension_prefs),
[email protected]695b5712012-12-06 23:55:28347 blacklist_(blacklist),
[email protected]cccdf0aa2011-11-11 03:43:38348 settings_frontend_(extensions::SettingsFrontend::Create(profile)),
[email protected]14908b72011-04-20 06:54:36349 pending_extension_manager_(*ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]a9b00ac2009-06-25 21:03:23350 install_directory_(install_directory),
[email protected]0436b102011-04-15 18:30:03351 extensions_enabled_(extensions_enabled),
[email protected]e81dba32009-06-19 20:19:13352 show_extensions_prompts_(true),
[email protected]fc332ae2012-11-14 20:17:33353 install_updates_when_idle_(true),
[email protected]3ecda252010-11-18 19:50:55354 ready_(false),
[email protected]14908b72011-04-20 06:54:36355 toolbar_model_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]8b280302011-10-13 22:22:23356 menu_manager_(profile),
[email protected]8d92e0382012-08-01 18:06:52357 app_notification_manager_(
358 new extensions::AppNotificationManager(profile)),
[email protected]7c1490da2011-10-11 18:53:25359 event_routers_initialized_(false),
[email protected]dff1e042012-04-27 10:59:18360 update_once_all_providers_are_ready_(false),
[email protected]3c4abc82012-10-22 22:25:54361 browser_terminating_(false),
[email protected]399583b2012-12-11 09:33:42362 installs_delayed_(false),
[email protected]1ff4d282012-12-08 00:39:21363 wipeout_is_active_(false),
[email protected]5db9ada2012-04-11 13:48:20364 app_sync_bundle_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
365 extension_sync_bundle_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]178f8512012-02-09 01:49:36366 app_shortcut_manager_(profile) {
[email protected]a29a517a2011-01-21 21:11:12367 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20368
[email protected]36a784c2009-06-23 06:21:08369 // Figure out if extension installation should be enabled.
[email protected]31bb5ee62012-09-12 22:58:40370 if (command_line->HasSwitch(switches::kDisableExtensions) ||
371 profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions)) {
[email protected]6d60703b2009-08-29 01:29:23372 extensions_enabled_ = false;
[email protected]6b75ec32009-08-14 06:37:18373 }
[email protected]36a784c2009-06-23 06:21:08374
[email protected]3c4abc82012-10-22 22:25:54375 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING,
376 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27377 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07378 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27379 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED,
[email protected]ad50def52011-10-19 23:17:07380 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27381 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07382 content::NotificationService::AllBrowserContextsAndSources());
[email protected]0db124b02012-11-07 04:55:05383 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
384 content::NotificationService::AllBrowserContextsAndSources());
[email protected]2fb7dc982010-09-29 12:24:28385 pref_change_registrar_.Init(profile->GetPrefs());
[email protected]90bb38d2012-11-14 18:36:03386 base::Closure callback =
387 base::Bind(&ExtensionService::OnExtensionInstallPrefChanged,
388 base::Unretained(this));
389 pref_change_registrar_.Add(prefs::kExtensionInstallAllowList, callback);
390 pref_change_registrar_.Add(prefs::kExtensionInstallDenyList, callback);
[email protected]e410b5f2012-12-14 14:02:24391 pref_change_registrar_.Add(prefs::kExtensionAllowedTypes, callback);
[email protected]4814b512009-11-07 00:12:29392
[email protected]93fd78f42009-07-10 16:43:17393 // Set up the ExtensionUpdater
394 if (autoupdate_enabled) {
395 int update_frequency = kDefaultUpdateFrequencySeconds;
396 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25397 base::StringToInt(command_line->GetSwitchValueASCII(
398 switches::kExtensionsUpdateFrequency),
399 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17400 }
[email protected]42a08162012-03-16 18:09:11401 updater_.reset(new extensions::ExtensionUpdater(this,
402 extension_prefs,
403 profile->GetPrefs(),
404 profile,
[email protected]fdd679b2012-11-15 20:49:39405 blacklist,
[email protected]42a08162012-03-16 18:09:11406 update_frequency));
[email protected]93fd78f42009-07-10 16:43:17407 }
408
[email protected]25ae0152011-11-18 14:40:02409 component_loader_.reset(
410 new extensions::ComponentLoader(this,
411 profile->GetPrefs(),
412 g_browser_process->local_state()));
[email protected]8e4560b62011-01-14 10:09:14413
[email protected]af9db5f2011-10-05 05:13:15414 app_notification_manager_->Init();
415
[email protected]0436b102011-04-15 18:30:03416 if (extensions_enabled_) {
[email protected]1f2f3002011-12-28 00:40:21417 if (!command_line->HasSwitch(switches::kImport) &&
418 !command_line->HasSwitch(switches::kImportFromFile)) {
[email protected]5df038b2012-07-16 19:03:27419 extensions::ExternalProviderImpl::CreateExternalProviders(
[email protected]1f2f3002011-12-28 00:40:21420 this, profile_, &external_extension_providers_);
421 }
[email protected]873531342011-03-09 12:16:05422 }
[email protected]b671760b2010-07-15 21:13:47423
[email protected]fb82dcd2012-03-21 14:15:46424 // Set this as the ExtensionService for extension sorting to ensure it
425 // cause syncs if required.
426 extension_prefs_->extension_sorting()->SetExtensionService(this);
427
[email protected]9ce11d22012-08-08 23:20:13428#if defined(ENABLE_EXTENSIONS)
[email protected]c77f2352012-08-08 22:07:58429 extension_action_storage_manager_.reset(
430 new extensions::ExtensionActionStorageManager(profile_));
[email protected]9ce11d22012-08-08 23:20:13431#endif
[email protected]c77f2352012-08-08 22:07:58432
[email protected]1ff4d282012-12-08 00:39:21433 // Before loading any extensions, determine whether Sideload Wipeout is on.
434 wipeout_is_active_ = FeatureSwitch::sideload_wipeout()->IsEnabled() &&
435 !extension_prefs_->GetSideloadWipeoutDone();
436
[email protected]cb0e50312011-05-09 15:03:07437 // How long is the path to the Extensions directory?
438 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ExtensionRootPathLength",
439 install_directory_.value().length(), 0, 500, 100);
[email protected]6014d672008-12-05 00:38:25440}
441
[email protected]84df8332011-12-06 18:22:46442const ExtensionSet* ExtensionService::extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45443 return &extensions_;
444}
445
[email protected]84df8332011-12-06 18:22:46446const ExtensionSet* ExtensionService::disabled_extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45447 return &disabled_extensions_;
448}
449
[email protected]84df8332011-12-06 18:22:46450const ExtensionSet* ExtensionService::terminated_extensions() const {
[email protected]bb7f40952011-01-13 00:21:20451 return &terminated_extensions_;
452}
453
[email protected]695b5712012-12-06 23:55:28454const ExtensionSet* ExtensionService::blacklisted_extensions() const {
455 return &blacklisted_extensions_;
456}
457
458scoped_ptr<const ExtensionSet>
459 ExtensionService::GenerateInstalledExtensionsSet() const {
460 scoped_ptr<ExtensionSet> installed_extensions(new ExtensionSet());
[email protected]7f4308d2012-01-18 07:43:01461 installed_extensions->InsertAll(extensions_);
462 installed_extensions->InsertAll(disabled_extensions_);
463 installed_extensions->InsertAll(terminated_extensions_);
[email protected]695b5712012-12-06 23:55:28464 installed_extensions->InsertAll(blacklisted_extensions_);
465 return installed_extensions.PassAs<const ExtensionSet>();
[email protected]7f4308d2012-01-18 07:43:01466}
467
[email protected]695b5712012-12-06 23:55:28468scoped_ptr<const ExtensionSet> ExtensionService::GetWipedOutExtensions() const {
469 scoped_ptr<ExtensionSet> extension_set(new ExtensionSet());
[email protected]215a7be2012-10-22 19:53:42470 for (ExtensionSet::const_iterator iter = disabled_extensions_.begin();
471 iter != disabled_extensions_.end(); ++iter) {
472 int disabled_reason = extension_prefs_->GetDisableReasons((*iter)->id());
473 if ((disabled_reason & Extension::DISABLE_SIDELOAD_WIPEOUT) != 0)
474 extension_set->Insert(*iter);
475 }
[email protected]695b5712012-12-06 23:55:28476 return extension_set.PassAs<const ExtensionSet>();
[email protected]215a7be2012-10-22 19:53:42477}
478
[email protected]3f213ad2012-07-26 23:39:41479extensions::PendingExtensionManager*
480 ExtensionService::pending_extension_manager() {
[email protected]b2907fd2011-03-25 16:43:37481 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45482}
483
[email protected]eaa7dd182010-12-14 11:09:00484ExtensionService::~ExtensionService() {
[email protected]c8d407e2011-04-28 21:27:17485 // No need to unload extensions here because they are profile-scoped, and the
486 // profile is in the process of being deleted.
[email protected]94b4ffc2011-02-04 05:55:17487
[email protected]5df038b2012-07-16 19:03:27488 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:14489 for (i = external_extension_providers_.begin();
490 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:27491 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:14492 provider->ServiceShutdown();
493 }
[email protected]6014d672008-12-05 00:38:25494}
495
[email protected]d91268022011-08-26 13:17:37496void ExtensionService::InitEventRoutersAfterImport() {
[email protected]cf593af2011-12-30 05:44:39497 RegisterForImportFinished();
498}
499
500void ExtensionService::RegisterForImportFinished() {
501 if (!registrar_.IsRegistered(this, chrome::NOTIFICATION_IMPORT_FINISHED,
502 content::Source<Profile>(profile_))) {
503 registrar_.Add(this, chrome::NOTIFICATION_IMPORT_FINISHED,
504 content::Source<Profile>(profile_));
505 }
506}
507
508void ExtensionService::InitAfterImport() {
509 CheckForExternalUpdates();
510
511 GarbageCollectExtensions();
512
513 // Idempotent, so although there is a possible race if the import
514 // process finished sometime in the middle of ProfileImpl::InitExtensions,
515 // it cannot happen twice.
516 InitEventRouters();
[email protected]d91268022011-08-26 13:17:37517}
518
[email protected]eaa7dd182010-12-14 11:09:00519void ExtensionService::InitEventRouters() {
[email protected]b3d62312b12010-10-14 21:10:18520 if (event_routers_initialized_)
521 return;
522
[email protected]6a3cd37e2012-04-17 17:13:34523#if defined(ENABLE_EXTENSIONS)
[email protected]5a38dfd2012-07-23 23:22:10524 browser_event_router_.reset(new extensions::BrowserEventRouter(profile_));
[email protected]27072cad2011-05-09 19:46:40525
[email protected]7e6935b2012-12-17 22:11:19526 extensions::PushMessagingAPIFactory::GetForProfile(profile_);
527
[email protected]eed367e2011-04-12 03:43:31528#if defined(OS_CHROMEOS)
[email protected]2a72c9f2012-03-17 15:10:30529 FileBrowserEventRouterFactory::GetForProfile(
530 profile_)->ObserveFileSystemEvents();
[email protected]771a6182011-10-05 16:32:52531
[email protected]c8bf09be2011-06-25 03:22:27532 ExtensionMediaPlayerEventRouter::GetInstance()->Init(profile_);
[email protected]1364c6d2012-07-24 11:57:19533 extensions::InputImeEventRouter::GetInstance()->Init();
[email protected]31bb5ee62012-09-12 22:58:40534#endif // defined(OS_CHROMEOS)
[email protected]6a3cd37e2012-04-17 17:13:34535#endif // defined(ENABLE_EXTENSIONS)
[email protected]b3d62312b12010-10-14 21:10:18536 event_routers_initialized_ = true;
[email protected]c5ae74ab2010-04-15 18:14:37537}
538
[email protected]037228a2012-10-05 01:36:16539void ExtensionService::Shutdown() {
540 // Do nothing for now.
541}
542
[email protected]b2907fd2011-03-25 16:43:37543const Extension* ExtensionService::GetExtensionById(
544 const std::string& id, bool include_disabled) const {
[email protected]81f0d162012-08-15 04:21:47545 int include_mask = INCLUDE_ENABLED;
[email protected]695b5712012-12-06 23:55:28546 if (include_disabled) {
547 // Include blacklisted extensions here because there are hundreds of
548 // callers of this function, and many might assume that this includes those
549 // that have been disabled due to blacklisting.
550 include_mask |= INCLUDE_DISABLED | INCLUDE_BLACKLISTED;
551 }
552 return GetExtensionById(id, include_mask);
553}
554
[email protected]399583b2012-12-11 09:33:42555GURL ExtensionService::GetSiteForExtensionId(const std::string& extension_id) {
556 return content::SiteInstance::GetSiteForURL(
557 profile_,
558 Extension::GetBaseURLFromExtensionId(extension_id));
559}
560
[email protected]695b5712012-12-06 23:55:28561const Extension* ExtensionService::GetExtensionById(
562 const std::string& id, int include_mask) const {
563 std::string lowercase_id = StringToLowerASCII(id);
564 if (include_mask & INCLUDE_ENABLED) {
565 const Extension* extension = extensions_.GetByID(lowercase_id);
566 if (extension)
567 return extension;
568 }
569 if (include_mask & INCLUDE_DISABLED) {
570 const Extension* extension = disabled_extensions_.GetByID(lowercase_id);
571 if (extension)
572 return extension;
573 }
574 if (include_mask & INCLUDE_TERMINATED) {
575 const Extension* extension = terminated_extensions_.GetByID(lowercase_id);
576 if (extension)
577 return extension;
578 }
579 if (include_mask & INCLUDE_BLACKLISTED) {
580 const Extension* extension = blacklisted_extensions_.GetByID(lowercase_id);
581 if (extension)
582 return extension;
583 }
584 return NULL;
[email protected]78994ab02010-12-08 18:06:44585}
586
[email protected]eaa7dd182010-12-14 11:09:00587void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12588 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20589
[email protected]fa6a9102010-11-22 15:38:50590 DCHECK(!ready_); // Can't redo init.
[email protected]93fd78f42009-07-10 16:43:17591 DCHECK_EQ(extensions_.size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32592
[email protected]399583b2012-12-11 09:33:42593 // TODO(mek): It might be cleaner to do the FinishDelayedInstallInfo stuff
594 // here instead of in installedloader.
[email protected]d8c8f25f2011-11-02 18:18:01595 component_loader_->LoadAll();
596 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]894bb502009-05-21 22:39:57597
[email protected]215a7be2012-10-22 19:53:42598 // The Sideload Wipeout effort takes place during load (see above), so once
599 // that is done the flag can be set so that we don't have to check again.
[email protected]1ff4d282012-12-08 00:39:21600 if (wipeout_is_active_) {
[email protected]215a7be2012-10-22 19:53:42601 extension_prefs_->SetSideloadWipeoutDone();
[email protected]1ff4d282012-12-08 00:39:21602 wipeout_is_active_ = false; // Wipeout is only on during load.
603 }
[email protected]215a7be2012-10-22 19:53:42604
[email protected]7a161db2011-12-15 20:45:51605 // If we are running in the import process, don't bother initializing the
606 // extension service since this can interfere with the main browser process
607 // that is already running an extension service for this profile.
608 // TODO(aa): can we start up even less of ExtensionService?
609 // http://crbug.com/107636
[email protected]1f2f3002011-12-28 00:40:21610 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kImport) &&
611 !CommandLine::ForCurrentProcess()->HasSwitch(switches::kImportFromFile)) {
[email protected]cf593af2011-12-30 05:44:39612 if (g_browser_process->profile_manager() &&
613 g_browser_process->profile_manager()->will_import()) {
614 RegisterForImportFinished();
615 } else {
[email protected]1f4728f2012-12-05 20:40:05616 // TODO(erikkay) this should probably be deferred to a future point
617 // rather than running immediately at startup.
618 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57619
[email protected]1f4728f2012-12-05 20:40:05620 // TODO(erikkay) this should probably be deferred as well.
621 GarbageCollectExtensions();
[email protected]cf593af2011-12-30 05:44:39622 }
[email protected]7a161db2011-12-15 20:45:51623 }
[email protected]399583b2012-12-11 09:33:42624
625 if (extension_prefs_->NeedsStorageGarbageCollection()) {
626 GarbageCollectIsolatedStorage();
627 extension_prefs_->SetNeedsStorageGarbageCollection(false);
628 }
[email protected]6014d672008-12-05 00:38:25629}
630
[email protected]31bb5ee62012-09-12 22:58:40631bool ExtensionService::UpdateExtension(const std::string& id,
632 const FilePath& extension_path,
633 const GURL& download_url,
634 CrxInstaller** out_crx_installer) {
[email protected]a29a517a2011-01-21 21:11:12635 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]3c4abc82012-10-22 22:25:54636 if (browser_terminating_) {
637 LOG(WARNING) << "Skipping UpdateExtension due to browser shutdown";
638 // Leak the temp file at extension_path. We don't want to add to the disk
639 // I/O burden at shutdown, we can't rely on the I/O completing anyway, and
640 // the file is in the OS temp directory which should be cleaned up for us.
641 return false;
642 }
[email protected]a8af9fdb2010-10-28 21:52:20643
[email protected]3f213ad2012-07-26 23:39:41644 const extensions::PendingExtensionInfo* pending_extension_info =
[email protected]51a3bf8b2012-06-08 22:53:06645 pending_extension_manager()->GetById(id);
[email protected]8ef78fd2010-08-19 17:14:32646
[email protected]695b5712012-12-06 23:55:28647 const Extension* extension = GetInstalledExtension(id);
[email protected]51a3bf8b2012-06-08 22:53:06648 if (!pending_extension_info && !extension) {
[email protected]aa142702010-03-26 01:26:33649 LOG(WARNING) << "Will not update extension " << id
650 << " because it is not installed or pending";
651 // Delete extension_path since we're not creating a CrxInstaller
652 // that would do it for us.
[email protected]7f8f24f2012-11-15 19:40:14653 if (!GetFileTaskRunner()->PostTask(
654 FROM_HERE,
[email protected]53612e82011-10-18 18:00:36655 base::Bind(
[email protected]7296f2762011-11-21 19:23:44656 &extension_file_util::DeleteFile, extension_path, false)))
[email protected]14908b72011-04-20 06:54:36657 NOTREACHED();
[email protected]420a0ec2011-06-01 01:07:03658
659 return false;
[email protected]e957fe52009-06-23 16:51:05660 }
661
[email protected]aa142702010-03-26 01:26:33662 // We want a silent install only for non-pending extensions and
663 // pending extensions that have install_silently set.
[email protected]91e51d612012-10-21 23:03:05664 ExtensionInstallPrompt* client = NULL;
665 if (pending_extension_info && !pending_extension_info->install_silently())
666 client = ExtensionInstallUI::CreateInstallPromptWithProfile(profile_);
[email protected]aa142702010-03-26 01:26:33667
[email protected]d8c8f25f2011-11-02 18:18:01668 scoped_refptr<CrxInstaller> installer(CrxInstaller::Create(this, client));
[email protected]6dfbbf82010-03-12 23:09:16669 installer->set_expected_id(id);
[email protected]51a3bf8b2012-06-08 22:53:06670 if (pending_extension_info) {
671 installer->set_install_source(pending_extension_info->install_source());
672 if (pending_extension_info->install_silently())
673 installer->set_allow_silent_install(true);
674 } else if (extension) {
[email protected]5eb375e92010-11-26 07:50:41675 installer->set_install_source(extension->location());
[email protected]51a3bf8b2012-06-08 22:53:06676 }
[email protected]7d8b7072012-04-07 01:07:46677 // If the extension was installed from or has migrated to the webstore, or
[email protected]fcb2c2c2012-10-17 21:08:45678 // its auto-update URL is from the webstore, treat it as a webstore install.
679 // Note that we ignore some older extensions with blank auto-update URLs
680 // because we are mostly concerned with restrictions on NaCl extensions,
681 // which are newer.
[email protected]a12ce8b22012-01-17 18:40:53682 int creation_flags = Extension::NO_FLAGS;
[email protected]75764512011-12-19 19:54:28683 if ((extension && extension->from_webstore()) ||
[email protected]7d8b7072012-04-07 01:07:46684 (extension && extension->UpdatesFromGallery()) ||
[email protected]963d13c2012-09-29 17:13:35685 (!extension && extension_urls::IsWebstoreUpdateUrl(
[email protected]51a3bf8b2012-06-08 22:53:06686 pending_extension_info->update_url()))) {
[email protected]a12ce8b22012-01-17 18:40:53687 creation_flags |= Extension::FROM_WEBSTORE;
688 }
689
690 // Bookmark apps being updated is kind of a contradiction, but that's because
691 // we mark the default apps as bookmark apps, and they're hosted in the web
692 // store, thus they can get updated. See http://crbug.com/101605 for more
693 // details.
694 if (extension && extension->from_bookmark())
695 creation_flags |= Extension::FROM_BOOKMARK;
696
[email protected]e33bbc22012-08-27 22:05:46697 if (extension && extension->was_installed_by_default())
698 creation_flags |= Extension::WAS_INSTALLED_BY_DEFAULT;
699
[email protected]a12ce8b22012-01-17 18:40:53700 installer->set_creation_flags(creation_flags);
701
[email protected]6dfbbf82010-03-12 23:09:16702 installer->set_delete_source(true);
[email protected]655b2b1a2011-10-13 17:13:06703 installer->set_download_url(download_url);
[email protected]cb0e50312011-05-09 15:03:07704 installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
[email protected]6dfbbf82010-03-12 23:09:16705 installer->InstallCrx(extension_path);
[email protected]420a0ec2011-06-01 01:07:03706
707 if (out_crx_installer)
708 *out_crx_installer = installer;
709
710 return true;
[email protected]e957fe52009-06-23 16:51:05711}
712
[email protected]eaa7dd182010-12-14 11:09:00713void ExtensionService::ReloadExtension(const std::string& extension_id) {
[email protected]32b36c62012-10-24 05:37:41714 int events = HasShellWindows(extension_id) ? EVENT_LAUNCHED : EVENT_NONE;
715 ReloadExtensionWithEvents(extension_id, events);
716}
717
718void ExtensionService::RestartExtension(const std::string& extension_id) {
719 int events = HasShellWindows(extension_id) ? EVENT_RESTARTED : EVENT_NONE;
720 ReloadExtensionWithEvents(extension_id, events);
721}
722
723void ExtensionService::ReloadExtensionWithEvents(
724 const std::string& extension_id,
725 int events) {
[email protected]a29a517a2011-01-21 21:11:12726 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]b65272f2009-08-31 15:47:06727 FilePath path;
[email protected]9adb9692010-10-29 23:14:02728 const Extension* current_extension = GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40729
[email protected]f17dbd42010-08-16 23:21:10730 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]b65272f2009-08-31 15:47:06731 if (current_extension) {
[email protected]4814b512009-11-07 00:12:29732 // If the extension has an inspector open for its background page, detach
733 // the inspector and hang onto a cookie for it, so that we can reattach
734 // later.
[email protected]31d8f5f22012-04-02 15:22:08735 // TODO(yoz): this is not incognito-safe!
736 ExtensionProcessManager* manager = system_->process_manager();
[email protected]3a1dc572012-07-31 22:25:13737 extensions::ExtensionHost* host =
738 manager->GetBackgroundHostForExtension(extension_id);
[email protected]0e12d7d2011-12-01 16:21:44739 if (host && DevToolsAgentHostRegistry::HasDevToolsAgentHost(
740 host->render_view_host())) {
[email protected]4814b512009-11-07 00:12:29741 // Look for an open inspector for the background page.
[email protected]0e12d7d2011-12-01 16:21:44742 DevToolsAgentHost* agent =
743 DevToolsAgentHostRegistry::GetDevToolsAgentHost(
744 host->render_view_host());
745 int devtools_cookie =
746 content::DevToolsManager::GetInstance()->DetachClientHost(agent);
[email protected]4814b512009-11-07 00:12:29747 if (devtools_cookie >= 0)
748 orphaned_dev_tools_[extension_id] = devtools_cookie;
749 }
750
[email protected]32b36c62012-10-24 05:37:41751 on_load_events_[extension_id] = events;
[email protected]6cafe35a2012-08-27 22:40:50752
[email protected]b65272f2009-08-31 15:47:06753 path = current_extension->path();
[email protected]44d62b62012-04-11 00:06:03754 DisableExtension(extension_id, Extension::DISABLE_RELOAD);
[email protected]f17dbd42010-08-16 23:21:10755 disabled_extension_paths_[extension_id] = path;
[email protected]1eb175082010-02-10 09:26:16756 } else {
757 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06758 }
759
[email protected]399583b2012-12-11 09:33:42760 if (delayed_updates_for_idle_.Contains(extension_id)) {
[email protected]6f6101832012-11-27 22:10:48761 FinishDelayedInstallation(extension_id);
[email protected]0db124b02012-11-07 04:55:05762 return;
763 }
764
[email protected]43ceb002012-02-10 23:19:15765 // If we're reloading a component extension, use the component extension
766 // loader's reloader.
767 if (component_loader_->Exists(extension_id)) {
768 component_loader_->Reload(extension_id);
769 return;
770 }
771
[email protected]e6090e42010-03-23 22:44:08772 // Check the installed extensions to see if what we're reloading was already
773 // installed.
774 scoped_ptr<ExtensionInfo> installed_extension(
775 extension_prefs_->GetInstalledExtensionInfo(extension_id));
776 if (installed_extension.get() &&
777 installed_extension->extension_manifest.get()) {
[email protected]d8c8f25f2011-11-02 18:18:01778 extensions::InstalledLoader(this).Load(*installed_extension, false);
[email protected]e6090e42010-03-23 22:44:08779 } else {
[email protected]d8c8f25f2011-11-02 18:18:01780 // Otherwise, the extension is unpacked (location LOAD).
[email protected]e6090e42010-03-23 22:44:08781 // We should always be able to remember the extension's path. If it's not in
782 // the map, someone failed to update |unloaded_extension_paths_|.
783 CHECK(!path.empty());
[email protected]d8c8f25f2011-11-02 18:18:01784 extensions::UnpackedInstaller::Create(this)->Load(path);
[email protected]e6090e42010-03-23 22:44:08785 }
[email protected]9cddd4702009-07-27 22:09:40786}
787
[email protected]fa2416f2011-05-03 08:41:20788bool ExtensionService::UninstallExtension(
[email protected]a12c706e2011-12-01 00:58:08789 std::string extension_id,
[email protected]fa2416f2011-05-03 08:41:20790 bool external_uninstall,
[email protected]65187152012-06-02 13:14:14791 string16* error) {
[email protected]a29a517a2011-01-21 21:11:12792 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20793
[email protected]0d6ec3a72011-09-02 02:09:43794 scoped_refptr<const Extension> extension(GetInstalledExtension(extension_id));
[email protected]631cf822009-05-15 07:01:25795
[email protected]e7afe2452010-08-22 16:19:13796 // Callers should not send us nonexistent extensions.
[email protected]aebe23a32010-12-10 22:15:48797 CHECK(extension);
[email protected]9f1087e2009-06-15 17:29:32798
[email protected]95da88c42011-03-31 10:07:33799 // Policy change which triggers an uninstall will always set
800 // |external_uninstall| to true so this is the only way to uninstall
801 // managed extensions.
[email protected]65187152012-06-02 13:14:14802 if (!external_uninstall &&
803 !system_->management_policy()->UserMayModifySettings(
804 extension.get(), error)) {
[email protected]ad50def52011-10-19 23:17:07805 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27806 chrome::NOTIFICATION_EXTENSION_UNINSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:53807 content::Source<Profile>(profile_),
808 content::Details<const Extension>(extension));
[email protected]d6ebc9792011-04-07 18:18:33809 return false;
810 }
[email protected]95da88c42011-03-31 10:07:33811
[email protected]3bdba0d2011-08-23 07:17:30812 // Extract the data we need for sync now, but don't actually sync until we've
813 // completed the uninstallation.
[email protected]65f173552012-06-28 22:43:58814 syncer::SyncChange sync_change;
[email protected]5db9ada2012-04-11 13:48:20815 if (app_sync_bundle_.HandlesApp(*extension)) {
816 sync_change = app_sync_bundle_.CreateSyncChangeToDelete(extension);
817 } else if (extension_sync_bundle_.HandlesExtension(*extension)) {
818 sync_change = extension_sync_bundle_.CreateSyncChangeToDelete(extension);
[email protected]3bdba0d2011-08-23 07:17:30819 }
820
[email protected]d96eb512012-11-01 23:44:08821 if (IsUnacknowledgedExternalExtension(extension)) {
822 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
823 EXTERNAL_EXTENSION_UNINSTALLED,
824 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
825 }
[email protected]9b217652010-10-08 22:04:23826 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08827 extension->GetType(), 100);
[email protected]fe2dd7742011-04-19 22:52:49828 RecordPermissionMessagesHistogram(
829 extension, "Extensions.Permissions_Uninstall");
[email protected]9b217652010-10-08 22:04:23830
[email protected]831aa212010-03-26 13:55:19831 // Unload before doing more cleanup to ensure that nothing is hanging on to
832 // any of these resources.
[email protected]814a7bf0f2011-08-13 05:30:59833 UnloadExtension(extension_id, extension_misc::UNLOAD_REASON_UNINSTALL);
[email protected]831aa212010-03-26 13:55:19834
[email protected]0d6ec3a72011-09-02 02:09:43835 extension_prefs_->OnExtensionUninstalled(extension_id, extension->location(),
[email protected]831aa212010-03-26 13:55:19836 external_uninstall);
[email protected]9f1087e2009-06-15 17:29:32837
838 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]0d6ec3a72011-09-02 02:09:43839 if (Extension::LOAD != extension->location()) {
[email protected]7f8f24f2012-11-15 19:40:14840 if (!GetFileTaskRunner()->PostTask(
841 FROM_HERE,
[email protected]53612e82011-10-18 18:00:36842 base::Bind(
[email protected]14908b72011-04-20 06:54:36843 &extension_file_util::UninstallExtension,
844 install_directory_,
[email protected]fa2416f2011-05-03 08:41:20845 extension_id)))
[email protected]14908b72011-04-20 06:54:36846 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32847 }
848
[email protected]0d6ec3a72011-09-02 02:09:43849 GURL launch_web_url_origin(extension->launch_web_url());
850 launch_web_url_origin = launch_web_url_origin.GetOrigin();
[email protected]605fb8102012-05-04 01:36:55851 bool is_storage_isolated = extension->is_storage_isolated();
[email protected]0d6ec3a72011-09-02 02:09:43852
[email protected]399583b2012-12-11 09:33:42853 if (is_storage_isolated) {
854 BrowserContext::AsyncObliterateStoragePartition(
855 profile_,
856 GetSiteForExtensionId(extension_id),
857 base::Bind(&ExtensionService::OnNeedsToGarbageCollectIsolatedStorage,
858 AsWeakPtr()));
859 } else {
860 if (extension->is_hosted_app() &&
861 !profile_->GetExtensionSpecialStoragePolicy()->
862 IsStorageProtected(launch_web_url_origin)) {
863 extensions::DataDeleter::StartDeleting(
864 profile_, extension_id, launch_web_url_origin);
865 }
866 extensions::DataDeleter::StartDeleting(profile_, extension_id,
867 extension->url());
[email protected]0d6ec3a72011-09-02 02:09:43868 }
[email protected]0d6ec3a72011-09-02 02:09:43869
[email protected]0dfe05c2011-02-23 23:03:36870 UntrackTerminatedExtension(extension_id);
[email protected]211030342010-09-30 18:41:06871
872 // Notify interested parties that we've uninstalled this extension.
[email protected]ad50def52011-10-19 23:17:07873 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27874 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
[email protected]6c2381d2011-10-19 02:52:53875 content::Source<Profile>(profile_),
[email protected]d67d9412012-07-17 17:19:42876 content::Details<const Extension>(extension));
[email protected]d6ebc9792011-04-07 18:18:33877
[email protected]5db9ada2012-04-11 13:48:20878 if (app_sync_bundle_.HasExtensionId(extension_id) &&
[email protected]a4a147652012-07-03 23:41:32879 sync_change.sync_data().GetDataType() == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:20880 app_sync_bundle_.ProcessDeletion(extension_id, sync_change);
881 } else if (extension_sync_bundle_.HasExtensionId(extension_id) &&
[email protected]a4a147652012-07-03 23:41:32882 sync_change.sync_data().GetDataType() == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:20883 extension_sync_bundle_.ProcessDeletion(extension_id, sync_change);
[email protected]3bdba0d2011-08-23 07:17:30884 }
885
[email protected]399583b2012-12-11 09:33:42886 delayed_updates_for_idle_.Remove(extension_id);
887 delayed_installs_.Remove(extension_id);
[email protected]0db124b02012-11-07 04:55:05888
[email protected]333b1de2011-09-12 18:28:50889 // Track the uninstallation.
[email protected]49098f702011-10-13 03:47:18890 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionUninstalled", 1, 2);
891
[email protected]d6ebc9792011-04-07 18:18:33892 return true;
[email protected]c10da4b02010-03-25 14:38:32893}
894
[email protected]c3cfb012011-04-06 22:07:35895bool ExtensionService::IsExtensionEnabled(
896 const std::string& extension_id) const {
[email protected]84df8332011-12-06 18:22:46897 if (extensions_.Contains(extension_id) ||
[email protected]dc9a74f72012-08-17 18:07:21898 terminated_extensions_.Contains(extension_id)) {
[email protected]36429da2011-07-11 20:25:18899 return true;
[email protected]dc9a74f72012-08-17 18:07:21900 }
[email protected]36429da2011-07-11 20:25:18901
[email protected]695b5712012-12-06 23:55:28902 if (disabled_extensions_.Contains(extension_id) ||
903 blacklisted_extensions_.Contains(extension_id)) {
[email protected]ad83ca242011-07-29 01:32:25904 return false;
[email protected]695b5712012-12-06 23:55:28905 }
[email protected]ad83ca242011-07-29 01:32:25906
907 // If the extension hasn't been loaded yet, check the prefs for it. Assume
908 // enabled unless otherwise noted.
909 return !extension_prefs_->IsExtensionDisabled(extension_id) &&
[email protected]f574c402012-12-04 23:20:31910 !extension_prefs_->IsExternalExtensionUninstalled(extension_id);
[email protected]c3cfb012011-04-06 22:07:35911}
912
913bool ExtensionService::IsExternalExtensionUninstalled(
914 const std::string& extension_id) const {
915 return extension_prefs_->IsExternalExtensionUninstalled(extension_id);
916}
917
[email protected]eaa7dd182010-12-14 11:09:00918void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12919 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20920
[email protected]06f92562011-04-29 19:27:31921 if (IsExtensionEnabled(extension_id))
[email protected]0c6da502009-08-14 22:32:39922 return;
[email protected]0c6da502009-08-14 22:32:39923
[email protected]c0231d72012-11-26 22:37:57924 int disable_reasons = extension_prefs_->GetDisableReasons(extension_id);
[email protected]b3317ad2011-04-28 23:46:00925 extension_prefs_->SetExtensionState(extension_id, Extension::ENABLED);
[email protected]eb5e4f92012-08-15 23:33:28926 extension_prefs_->ClearDisableReasons(extension_id);
[email protected]1784e83a2009-09-08 21:01:52927
[email protected]695b5712012-12-06 23:55:28928 const Extension* extension = disabled_extensions_.GetByID(extension_id);
[email protected]06f92562011-04-29 19:27:31929 // This can happen if sync enables an extension that is not
930 // installed yet.
931 if (!extension)
932 return;
933
[email protected]d96eb512012-11-01 23:44:08934 if (IsUnacknowledgedExternalExtension(extension)) {
935 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
936 EXTERNAL_EXTENSION_REENABLED,
937 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]612a1cb12012-10-17 13:18:03938 AcknowledgeExternalExtension(extension->id());
[email protected]d96eb512012-11-01 23:44:08939 }
[email protected]612a1cb12012-10-17 13:18:03940
[email protected]c0231d72012-11-26 22:37:57941 if (disable_reasons & Extension::DISABLE_SIDELOAD_WIPEOUT)
[email protected]94e14592012-11-19 10:49:42942 UMA_HISTOGRAM_BOOLEAN("DisabledExtension.ExtensionWipedStatus", false);
943
[email protected]0c6da502009-08-14 22:32:39944 // Move it over to the enabled list.
[email protected]84df8332011-12-06 18:22:46945 extensions_.Insert(make_scoped_refptr(extension));
946 disabled_extensions_.Remove(extension->id());
[email protected]0c6da502009-08-14 22:32:39947
[email protected]62d30f42009-10-01 22:36:06948 NotifyExtensionLoaded(extension);
[email protected]3bdba0d2011-08-23 07:17:30949
[email protected]f74d7f32012-06-19 19:22:51950 // Notify listeners that the extension was enabled.
951 content::NotificationService::current()->Notify(
952 chrome::NOTIFICATION_EXTENSION_ENABLED,
953 content::Source<Profile>(profile_),
954 content::Details<const Extension>(extension));
955
[email protected]3bdba0d2011-08-23 07:17:30956 SyncExtensionChangeIfNeeded(*extension);
[email protected]0c6da502009-08-14 22:32:39957}
958
[email protected]44d62b62012-04-11 00:06:03959void ExtensionService::DisableExtension(
960 const std::string& extension_id,
961 Extension::DisableReason disable_reason) {
[email protected]a29a517a2011-01-21 21:11:12962 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20963
[email protected]b2ba9962009-12-10 20:10:15964 // The extension may have been disabled already.
[email protected]06f92562011-04-29 19:27:31965 if (!IsExtensionEnabled(extension_id))
[email protected]1784e83a2009-09-08 21:01:52966 return;
[email protected]1784e83a2009-09-08 21:01:52967
[email protected]06f92562011-04-29 19:27:31968 const Extension* extension = GetInstalledExtension(extension_id);
969 // |extension| can be NULL if sync disables an extension that is not
970 // installed yet.
[email protected]65187152012-06-02 13:14:14971 if (extension &&
972 !system_->management_policy()->UserMayModifySettings(extension, NULL)) {
[email protected]95da88c42011-03-31 10:07:33973 return;
[email protected]65187152012-06-02 13:14:14974 }
[email protected]95da88c42011-03-31 10:07:33975
[email protected]b3317ad2011-04-28 23:46:00976 extension_prefs_->SetExtensionState(extension_id, Extension::DISABLED);
[email protected]eb5e4f92012-08-15 23:33:28977 extension_prefs_->AddDisableReason(extension_id, disable_reason);
[email protected]1784e83a2009-09-08 21:01:52978
[email protected]695b5712012-12-06 23:55:28979 int include_mask = INCLUDE_EVERYTHING & ~INCLUDE_DISABLED;
980 extension = GetExtensionById(extension_id, include_mask);
[email protected]06f92562011-04-29 19:27:31981 if (!extension)
982 return;
983
[email protected]5c094792012-09-07 19:44:53984 // Move it over to the disabled list. Don't send a second unload notification
985 // for terminated extensions being disabled.
[email protected]84df8332011-12-06 18:22:46986 disabled_extensions_.Insert(make_scoped_refptr(extension));
[email protected]5c094792012-09-07 19:44:53987 if (extensions_.Contains(extension->id())) {
[email protected]84df8332011-12-06 18:22:46988 extensions_.Remove(extension->id());
[email protected]5c094792012-09-07 19:44:53989 NotifyExtensionUnloaded(extension, extension_misc::UNLOAD_REASON_DISABLE);
990 } else {
[email protected]84df8332011-12-06 18:22:46991 terminated_extensions_.Remove(extension->id());
[email protected]5c094792012-09-07 19:44:53992 }
[email protected]3bdba0d2011-08-23 07:17:30993
994 SyncExtensionChangeIfNeeded(*extension);
[email protected]1784e83a2009-09-08 21:01:52995}
996
[email protected]eaa7dd182010-12-14 11:09:00997void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]b70a2d92012-06-28 19:51:21998 const Extension* extension, bool record_oauth2_grant) {
[email protected]be083862012-09-01 03:53:45999 GrantPermissions(extension, record_oauth2_grant);
[email protected]fe2dd7742011-04-19 22:52:491000 RecordPermissionMessagesHistogram(
1001 extension, "Extensions.Permissions_ReEnable");
[email protected]8d888c12010-11-30 00:00:251002 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
1003 EnableExtension(extension->id());
1004}
1005
[email protected]be083862012-09-01 03:53:451006void ExtensionService::GrantPermissions(const Extension* extension,
1007 bool record_oauth2_grant) {
1008 CHECK(extension);
1009 extensions::PermissionsUpdater perms_updater(profile());
1010 perms_updater.GrantActivePermissions(extension, record_oauth2_grant);
1011}
1012
[email protected]fe2dd7742011-04-19 22:52:491013// static
1014void ExtensionService::RecordPermissionMessagesHistogram(
1015 const Extension* e, const char* histogram) {
1016 // Since this is called from multiple sources, and since the Histogram macros
1017 // use statics, we need to manually lookup the Histogram ourselves.
1018 base::Histogram* counter = base::LinearHistogram::FactoryGet(
1019 histogram,
1020 1,
[email protected]c2e66e12012-06-27 06:27:061021 PermissionMessage::kEnumBoundary,
1022 PermissionMessage::kEnumBoundary + 1,
[email protected]fe2dd7742011-04-19 22:52:491023 base::Histogram::kUmaTargetedHistogramFlag);
1024
[email protected]c2e66e12012-06-27 06:27:061025 PermissionMessages permissions = e->GetPermissionMessages();
[email protected]fe2dd7742011-04-19 22:52:491026 if (permissions.empty()) {
[email protected]c2e66e12012-06-27 06:27:061027 counter->Add(PermissionMessage::kNone);
[email protected]fe2dd7742011-04-19 22:52:491028 } else {
[email protected]c2e66e12012-06-27 06:27:061029 for (PermissionMessages::iterator it = permissions.begin();
[email protected]0d3e4a22011-06-23 19:02:521030 it != permissions.end(); ++it)
1031 counter->Add(it->id());
[email protected]fe2dd7742011-04-19 22:52:491032 }
1033}
1034
[email protected]eaa7dd182010-12-14 11:09:001035void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]57a777f72010-03-31 01:09:421036 // The ChromeURLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:061037 // was loaded, otherwise a race can arise where a renderer that is created
1038 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:421039 // that the request context doesn't yet know about. The profile is responsible
1040 // for ensuring its URLRequestContexts appropriately discover the loaded
1041 // extension.
[email protected]31d8f5f22012-04-02 15:22:081042 system_->RegisterExtensionWithRequestContexts(extension);
[email protected]62d30f42009-10-01 22:36:061043
[email protected]d5949312012-12-03 22:13:301044 // Tell renderers about the new extension, unless it's a theme (renderers
1045 // don't need to know about themes).
[email protected]a58f599c2012-12-01 03:08:191046 if (!extension->is_theme()) {
[email protected]19647262011-12-16 09:57:491047 for (content::RenderProcessHost::iterator i(
1048 content::RenderProcessHost::AllHostsIterator());
1049 !i.IsAtEnd(); i.Advance()) {
1050 content::RenderProcessHost* host = i.GetCurrentValue();
1051 Profile* host_profile =
1052 Profile::FromBrowserContext(host->GetBrowserContext());
1053 if (host_profile->GetOriginalProfile() ==
1054 profile_->GetOriginalProfile()) {
1055 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions(
1056 1, ExtensionMsg_Loaded_Params(extension));
1057 host->Send(
1058 new ExtensionMsg_Loaded(loaded_extensions));
1059 }
[email protected]c8d407e2011-04-28 21:27:171060 }
[email protected]77a6970c2011-04-23 16:58:561061 }
1062
[email protected]3442a662012-01-12 08:06:171063 // Tell subsystems that use the EXTENSION_LOADED notification about the new
1064 // extension.
1065 //
1066 // NOTE: It is important that this happen after notifying the renderers about
1067 // the new extensions so that if we navigate to an extension URL in
1068 // NOTIFICATION_EXTENSION_LOADED, the renderer is guaranteed to know about it.
1069 content::NotificationService::current()->Notify(
1070 chrome::NOTIFICATION_EXTENSION_LOADED,
1071 content::Source<Profile>(profile_),
1072 content::Details<const Extension>(extension));
1073
[email protected]c8d407e2011-04-28 21:27:171074 // Tell a random-ass collection of other subsystems about the new extension.
1075 // TODO(aa): What should we do with all this goop? Can it move into the
1076 // relevant objects via EXTENSION_LOADED?
1077
1078 profile_->GetExtensionSpecialStoragePolicy()->
1079 GrantRightsForExtension(extension);
1080
1081 UpdateActiveExtensionsInCrashReporter();
1082
1083 ExtensionWebUI::RegisterChromeURLOverrides(
1084 profile_, extension->GetChromeURLOverrides());
1085
[email protected]c8d407e2011-04-28 21:27:171086 // If the extension has permission to load chrome://favicon/ resources we need
1087 // to make sure that the FaviconSource is registered with the
1088 // ChromeURLDataManager.
1089 if (extension->HasHostPermission(GURL(chrome::kChromeUIFaviconURL))) {
1090 FaviconSource* favicon_source = new FaviconSource(profile_,
1091 FaviconSource::FAVICON);
[email protected]ef92e172012-04-25 19:40:411092 ChromeURLDataManager::AddDataSource(profile_, favicon_source);
[email protected]c8d407e2011-04-28 21:27:171093 }
[email protected]b07e606e2012-09-15 20:16:151094
1095#if !defined(OS_ANDROID)
1096 // Same for chrome://theme/ resources.
1097 if (extension->HasHostPermission(GURL(chrome::kChromeUIThemeURL))) {
1098 ThemeSource* theme_source = new ThemeSource(profile_);
1099 ChromeURLDataManager::AddDataSource(profile_, theme_source);
1100 }
1101#endif
1102
[email protected]5eddc3e2011-10-26 04:33:311103 // Same for chrome://thumb/ resources.
1104 if (extension->HasHostPermission(GURL(chrome::kChromeUIThumbnailURL))) {
1105 ThumbnailSource* thumbnail_source = new ThumbnailSource(profile_);
[email protected]ef92e172012-04-25 19:40:411106 ChromeURLDataManagerFactory::GetForProfile(profile_)->
1107 AddDataSource(thumbnail_source);
[email protected]5eddc3e2011-10-26 04:33:311108 }
[email protected]c8d407e2011-04-28 21:27:171109
1110 // TODO(mpcomplete): This ends up affecting all profiles. See crbug.com/80757.
[email protected]2de307592011-04-05 21:16:581111 bool plugins_changed = false;
1112 for (size_t i = 0; i < extension->plugins().size(); ++i) {
1113 const Extension::PluginInfo& plugin = extension->plugins()[i];
[email protected]f520b5b2011-11-08 02:42:141114 PluginService::GetInstance()->RefreshPlugins();
1115 PluginService::GetInstance()->AddExtraPluginPath(plugin.path);
[email protected]2de307592011-04-05 21:16:581116 plugins_changed = true;
[email protected]476a27032011-09-13 13:40:491117 ChromePluginServiceFilter* filter =
1118 ChromePluginServiceFilter::GetInstance();
1119 if (plugin.is_public) {
1120 filter->RestrictPluginToProfileAndOrigin(
1121 plugin.path, profile_, GURL());
1122 } else {
1123 filter->RestrictPluginToProfileAndOrigin(
1124 plugin.path, profile_, extension->url());
[email protected]2de307592011-04-05 21:16:581125 }
1126 }
[email protected]84396dbc2011-04-14 06:33:421127
1128 bool nacl_modules_changed = false;
1129 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1130 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
[email protected]cebc3dc2011-04-18 17:15:001131 RegisterNaClModule(module.url, module.mime_type);
[email protected]84396dbc2011-04-14 06:33:421132 nacl_modules_changed = true;
1133 }
1134
[email protected]ed0ba002011-05-26 16:55:131135 if (nacl_modules_changed)
1136 UpdatePluginListWithNaClModules();
[email protected]84396dbc2011-04-14 06:33:421137
1138 if (plugins_changed || nacl_modules_changed)
[email protected]45a22e62011-10-12 09:48:021139 PluginService::GetInstance()->PurgePluginListCache(profile_, false);
[email protected]b4d9d122011-06-17 01:58:541140
[email protected]14da67b02011-08-02 05:49:131141#if defined(OS_CHROMEOS)
[email protected]b4d9d122011-06-17 01:58:541142 for (std::vector<Extension::InputComponentInfo>::const_iterator component =
1143 extension->input_components().begin();
1144 component != extension->input_components().end();
1145 ++component) {
[email protected]14da67b02011-08-02 05:49:131146 if (component->type == Extension::INPUT_COMPONENT_TYPE_IME) {
[email protected]1364c6d2012-07-24 11:57:191147 extensions::InputImeEventRouter::GetInstance()->RegisterIme(
[email protected]14da67b02011-08-02 05:49:131148 profile_, extension->id(), *component);
1149 }
[email protected]b4d9d122011-06-17 01:58:541150 }
1151#endif
[email protected]62d30f42009-10-01 22:36:061152}
1153
[email protected]a9f39a312010-12-23 22:14:271154void ExtensionService::NotifyExtensionUnloaded(
[email protected]814a7bf0f2011-08-13 05:30:591155 const Extension* extension,
1156 extension_misc::UnloadedExtensionReason reason) {
[email protected]a9f39a312010-12-23 22:14:271157 UnloadedExtensionInfo details(extension, reason);
[email protected]ad50def52011-10-19 23:17:071158 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271159 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]6c2381d2011-10-19 02:52:531160 content::Source<Profile>(profile_),
1161 content::Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061162
[email protected]903d70a2012-12-04 13:00:171163#if defined(ENABLE_THEMES)
1164 // If the current theme is being unloaded, tell ThemeService to revert back
1165 // to the default theme.
1166 if (reason != extension_misc::UNLOAD_REASON_UPDATE && extension->is_theme()) {
1167 ThemeService* theme_service = ThemeServiceFactory::GetForProfile(profile_);
1168 if (extension->id() == theme_service->GetThemeID())
1169 theme_service->UseDefaultTheme();
1170 }
1171#endif
1172
[email protected]f3b1a082011-11-18 00:34:301173 for (content::RenderProcessHost::iterator i(
1174 content::RenderProcessHost::AllHostsIterator());
[email protected]77a6970c2011-04-23 16:58:561175 !i.IsAtEnd(); i.Advance()) {
[email protected]f3b1a082011-11-18 00:34:301176 content::RenderProcessHost* host = i.GetCurrentValue();
[email protected]9b62ecf2011-07-27 20:23:081177 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:301178 Profile::FromBrowserContext(host->GetBrowserContext());
[email protected]9b62ecf2011-07-27 20:23:081179 if (host_profile->GetOriginalProfile() == profile_->GetOriginalProfile())
[email protected]c8d407e2011-04-28 21:27:171180 host->Send(new ExtensionMsg_Unloaded(extension->id()));
[email protected]77a6970c2011-04-23 16:58:561181 }
1182
[email protected]31d8f5f22012-04-02 15:22:081183 system_->UnregisterExtensionWithRequestContexts(extension->id(), reason);
[email protected]c8d407e2011-04-28 21:27:171184 profile_->GetExtensionSpecialStoragePolicy()->
1185 RevokeRightsForExtension(extension);
1186
1187 ExtensionWebUI::UnregisterChromeURLOverrides(
1188 profile_, extension->GetChromeURLOverrides());
1189
[email protected]b777b332011-04-16 04:01:081190#if defined(OS_CHROMEOS)
[email protected]10eb28162012-09-18 03:04:091191 // Revoke external file access to third party extensions.
1192 fileapi::FileSystemContext* filesystem_context =
1193 BrowserContext::GetDefaultStoragePartition(profile_)->
1194 GetFileSystemContext();
1195 if (filesystem_context && filesystem_context->external_provider()) {
1196 filesystem_context->external_provider()->
[email protected]c8d407e2011-04-28 21:27:171197 RevokeAccessForExtension(extension->id());
[email protected]62d30f42009-10-01 22:36:061198 }
[email protected]54ccafa2011-12-06 23:36:321199
1200 if (extension->input_components().size() > 0) {
[email protected]1364c6d2012-07-24 11:57:191201 extensions::InputImeEventRouter::GetInstance()->UnregisterAllImes(
[email protected]54ccafa2011-12-06 23:36:321202 profile_, extension->id());
1203 }
[email protected]c8d407e2011-04-28 21:27:171204#endif
1205
1206 UpdateActiveExtensionsInCrashReporter();
[email protected]2de307592011-04-05 21:16:581207
1208 bool plugins_changed = false;
1209 for (size_t i = 0; i < extension->plugins().size(); ++i) {
1210 const Extension::PluginInfo& plugin = extension->plugins()[i];
[email protected]b6a2f8de2012-01-31 17:28:491211 PluginService::GetInstance()->ForcePluginShutdown(plugin.path);
[email protected]f520b5b2011-11-08 02:42:141212 PluginService::GetInstance()->RefreshPlugins();
1213 PluginService::GetInstance()->RemoveExtraPluginPath(plugin.path);
[email protected]2de307592011-04-05 21:16:581214 plugins_changed = true;
[email protected]476a27032011-09-13 13:40:491215 ChromePluginServiceFilter::GetInstance()->UnrestrictPlugin(plugin.path);
[email protected]2de307592011-04-05 21:16:581216 }
[email protected]84396dbc2011-04-14 06:33:421217
1218 bool nacl_modules_changed = false;
1219 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1220 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
[email protected]cebc3dc2011-04-18 17:15:001221 UnregisterNaClModule(module.url);
[email protected]84396dbc2011-04-14 06:33:421222 nacl_modules_changed = true;
1223 }
1224
[email protected]ed0ba002011-05-26 16:55:131225 if (nacl_modules_changed)
1226 UpdatePluginListWithNaClModules();
[email protected]84396dbc2011-04-14 06:33:421227
1228 if (plugins_changed || nacl_modules_changed)
[email protected]45a22e62011-10-12 09:48:021229 PluginService::GetInstance()->PurgePluginListCache(profile_, false);
[email protected]62d30f42009-10-01 22:36:061230}
1231
[email protected]0a071a32011-02-08 00:18:241232Profile* ExtensionService::profile() {
1233 return profile_;
1234}
1235
[email protected]45759612012-07-10 17:21:231236extensions::ExtensionPrefs* ExtensionService::extension_prefs() {
[email protected]ddb1e5a2010-12-13 20:10:451237 return extension_prefs_;
1238}
1239
[email protected]0d9a2202011-11-09 13:48:411240extensions::SettingsFrontend* ExtensionService::settings_frontend() {
[email protected]cccdf0aa2011-11-11 03:43:381241 return settings_frontend_.get();
[email protected]b7f853e282011-08-10 09:24:201242}
1243
[email protected]e2d720aa2012-05-17 00:11:351244extensions::ContentSettingsStore* ExtensionService::GetContentSettingsStore() {
[email protected]b790b072011-05-20 09:46:441245 return extension_prefs()->content_settings_store();
1246}
1247
[email protected]25ae0152011-11-18 14:40:021248bool ExtensionService::is_ready() {
1249 return ready_;
1250}
1251
[email protected]7f8f24f2012-11-15 19:40:141252base::SequencedTaskRunner* ExtensionService::GetFileTaskRunner() {
1253 if (file_task_runner_)
1254 return file_task_runner_;
1255
1256 // We should be able to interrupt any part of extension install process during
1257 // shutdown. SKIP_ON_SHUTDOWN ensures that not started extension install tasks
1258 // will be ignored/deleted while we will block on started tasks.
1259 std::string token("ext_install-");
1260 token.append(profile_->GetPath().AsUTF8Unsafe());
1261 file_task_runner_ = BrowserThread::GetBlockingPool()->
1262 GetSequencedTaskRunnerWithShutdownBehavior(
1263 BrowserThread::GetBlockingPool()->GetNamedSequenceToken(token),
1264 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
1265 return file_task_runner_;
1266}
1267
[email protected]42a08162012-03-16 18:09:111268extensions::ExtensionUpdater* ExtensionService::updater() {
[email protected]2859946f2011-04-04 18:18:061269 return updater_.get();
1270}
1271
[email protected]4ee07c62012-08-21 12:40:421272void ExtensionService::CheckManagementPolicy() {
[email protected]aa96d3a2010-08-21 08:45:251273 std::vector<std::string> to_be_removed;
[email protected]695b5712012-12-06 23:55:281274
[email protected]aa96d3a2010-08-21 08:45:251275 // Loop through extensions list, unload installed extensions.
[email protected]84df8332011-12-06 18:22:461276 for (ExtensionSet::const_iterator iter = extensions_.begin();
[email protected]aa96d3a2010-08-21 08:45:251277 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021278 const Extension* extension = (*iter);
[email protected]4ee07c62012-08-21 12:40:421279 if (!system_->management_policy()->UserMayLoad(extension, NULL))
[email protected]aa96d3a2010-08-21 08:45:251280 to_be_removed.push_back(extension->id());
1281 }
1282
1283 // UnloadExtension will change the extensions_ list. So, we should
1284 // call it outside the iterator loop.
[email protected]31bb5ee62012-09-12 22:58:401285 for (size_t i = 0; i < to_be_removed.size(); ++i)
[email protected]814a7bf0f2011-08-13 05:30:591286 UnloadExtension(to_be_removed[i], extension_misc::UNLOAD_REASON_DISABLE);
[email protected]aa96d3a2010-08-21 08:45:251287}
1288
[email protected]31206602011-04-13 23:07:321289void ExtensionService::CheckForUpdatesSoon() {
[email protected]c3cfb012011-04-06 22:07:351290 if (updater()) {
[email protected]94fde232012-04-27 10:22:301291 if (AreAllExternalProvidersReady()) {
1292 updater()->CheckSoon();
1293 } else {
1294 // Sync can start updating before all the external providers are ready
1295 // during startup. Start the update as soon as those providers are ready,
1296 // but not before.
1297 update_once_all_providers_are_ready_ = true;
1298 }
[email protected]c3cfb012011-04-06 22:07:351299 } else {
[email protected]31206602011-04-13 23:07:321300 LOG(WARNING) << "CheckForUpdatesSoon() called with auto-update turned off";
[email protected]c3cfb012011-04-06 22:07:351301 }
1302}
1303
[email protected]3f98fd92012-11-17 15:54:291304syncer::SyncMergeResult ExtensionService::MergeDataAndStartSyncing(
[email protected]a4a147652012-07-03 23:41:321305 syncer::ModelType type,
[email protected]65f173552012-06-28 22:43:581306 const syncer::SyncDataList& initial_sync_data,
1307 scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
1308 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) {
[email protected]5db9ada2012-04-11 13:48:201309 CHECK(sync_processor.get());
[email protected]5d36e052012-04-20 20:12:281310 CHECK(sync_error_factory.get());
[email protected]3bdba0d2011-08-23 07:17:301311
1312 switch (type) {
[email protected]a4a147652012-07-03 23:41:321313 case syncer::EXTENSIONS:
[email protected]5db9ada2012-04-11 13:48:201314 extension_sync_bundle_.SetupSync(sync_processor.release(),
[email protected]5d36e052012-04-20 20:12:281315 sync_error_factory.release(),
[email protected]5db9ada2012-04-11 13:48:201316 initial_sync_data);
[email protected]3bdba0d2011-08-23 07:17:301317 break;
1318
[email protected]a4a147652012-07-03 23:41:321319 case syncer::APPS:
[email protected]5d36e052012-04-20 20:12:281320 app_sync_bundle_.SetupSync(sync_processor.release(),
1321 sync_error_factory.release(),
1322 initial_sync_data);
[email protected]3bdba0d2011-08-23 07:17:301323 break;
1324
1325 default:
1326 LOG(FATAL) << "Got " << type << " ModelType";
1327 }
[email protected]3bdba0d2011-08-23 07:17:301328
[email protected]b1417ab2011-12-15 17:09:111329 // Process local extensions.
1330 // TODO(yoz): Determine whether pending extensions should be considered too.
1331 // See crbug.com/104399.
[email protected]65f173552012-06-28 22:43:581332 syncer::SyncDataList sync_data_list = GetAllSyncData(type);
1333 syncer::SyncChangeList sync_change_list;
1334 for (syncer::SyncDataList::const_iterator i = sync_data_list.begin();
[email protected]3bdba0d2011-08-23 07:17:301335 i != sync_data_list.end();
1336 ++i) {
[email protected]5db9ada2012-04-11 13:48:201337 switch (type) {
[email protected]a4a147652012-07-03 23:41:321338 case syncer::EXTENSIONS:
[email protected]5db9ada2012-04-11 13:48:201339 sync_change_list.push_back(
1340 extension_sync_bundle_.CreateSyncChange(*i));
1341 break;
[email protected]a4a147652012-07-03 23:41:321342 case syncer::APPS:
[email protected]5db9ada2012-04-11 13:48:201343 sync_change_list.push_back(app_sync_bundle_.CreateSyncChange(*i));
1344 break;
1345 default:
1346 LOG(FATAL) << "Got " << type << " ModelType";
[email protected]31bb5ee62012-09-12 22:58:401347 }
[email protected]3bdba0d2011-08-23 07:17:301348 }
[email protected]3bdba0d2011-08-23 07:17:301349
[email protected]5db9ada2012-04-11 13:48:201350
[email protected]a4a147652012-07-03 23:41:321351 if (type == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:201352 extension_sync_bundle_.ProcessSyncChangeList(sync_change_list);
[email protected]a4a147652012-07-03 23:41:321353 } else if (type == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:201354 app_sync_bundle_.ProcessSyncChangeList(sync_change_list);
1355 }
[email protected]0ffd9e52012-01-26 21:44:341356
[email protected]3f98fd92012-11-17 15:54:291357 return syncer::SyncMergeResult(type);
[email protected]3bdba0d2011-08-23 07:17:301358}
1359
[email protected]a4a147652012-07-03 23:41:321360void ExtensionService::StopSyncing(syncer::ModelType type) {
1361 if (type == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:201362 app_sync_bundle_.Reset();
[email protected]a4a147652012-07-03 23:41:321363 } else if (type == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:201364 extension_sync_bundle_.Reset();
1365 }
[email protected]3bdba0d2011-08-23 07:17:301366}
1367
[email protected]65f173552012-06-28 22:43:581368syncer::SyncDataList ExtensionService::GetAllSyncData(
[email protected]a4a147652012-07-03 23:41:321369 syncer::ModelType type) const {
[email protected]31bb5ee62012-09-12 22:58:401370 if (type == syncer::EXTENSIONS)
[email protected]5db9ada2012-04-11 13:48:201371 return extension_sync_bundle_.GetAllSyncData();
[email protected]31bb5ee62012-09-12 22:58:401372 if (type == syncer::APPS)
[email protected]5db9ada2012-04-11 13:48:201373 return app_sync_bundle_.GetAllSyncData();
[email protected]5db9ada2012-04-11 13:48:201374
1375 // We should only get sync data for extensions and apps.
1376 NOTREACHED();
1377
[email protected]65f173552012-06-28 22:43:581378 return syncer::SyncDataList();
[email protected]3bdba0d2011-08-23 07:17:301379}
1380
[email protected]65f173552012-06-28 22:43:581381syncer::SyncError ExtensionService::ProcessSyncChanges(
[email protected]3bdba0d2011-08-23 07:17:301382 const tracked_objects::Location& from_here,
[email protected]65f173552012-06-28 22:43:581383 const syncer::SyncChangeList& change_list) {
1384 for (syncer::SyncChangeList::const_iterator i = change_list.begin();
[email protected]3bdba0d2011-08-23 07:17:301385 i != change_list.end();
1386 ++i) {
[email protected]a4a147652012-07-03 23:41:321387 syncer::ModelType type = i->sync_data().GetDataType();
1388 if (type == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:201389 extension_sync_bundle_.ProcessSyncChange(
1390 extensions::ExtensionSyncData(*i));
[email protected]a4a147652012-07-03 23:41:321391 } else if (type == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:201392 app_sync_bundle_.ProcessSyncChange(extensions::AppSyncData(*i));
1393 }
[email protected]3bdba0d2011-08-23 07:17:301394 }
1395
[email protected]0ffd9e52012-01-26 21:44:341396 extension_prefs()->extension_sorting()->FixNTPOrdinalCollisions();
1397
[email protected]65f173552012-06-28 22:43:581398 return syncer::SyncError();
[email protected]b05fb9ff2011-04-23 00:07:561399}
1400
[email protected]5db9ada2012-04-11 13:48:201401extensions::ExtensionSyncData ExtensionService::GetExtensionSyncData(
1402 const Extension& extension) const {
1403 return extensions::ExtensionSyncData(extension,
1404 IsExtensionEnabled(extension.id()),
1405 IsIncognitoEnabled(extension.id()));
[email protected]b05fb9ff2011-04-23 00:07:561406}
1407
[email protected]5db9ada2012-04-11 13:48:201408extensions::AppSyncData ExtensionService::GetAppSyncData(
1409 const Extension& extension) const {
1410 return extensions::AppSyncData(
1411 extension,
1412 IsExtensionEnabled(extension.id()),
1413 IsIncognitoEnabled(extension.id()),
1414 extension_prefs_->GetAppNotificationClientId(extension.id()),
1415 extension_prefs_->IsAppNotificationDisabled(extension.id()),
1416 extension_prefs_->extension_sorting()->GetAppLaunchOrdinal(
1417 extension.id()),
1418 extension_prefs_->extension_sorting()->GetPageOrdinal(extension.id()));
1419}
[email protected]3bdba0d2011-08-23 07:17:301420
[email protected]5db9ada2012-04-11 13:48:201421std::vector<extensions::ExtensionSyncData>
1422 ExtensionService::GetExtensionSyncDataList() const {
1423 std::vector<extensions::ExtensionSyncData> extension_sync_list;
1424 extension_sync_bundle_.GetExtensionSyncDataListHelper(extensions_,
1425 &extension_sync_list);
1426 extension_sync_bundle_.GetExtensionSyncDataListHelper(disabled_extensions_,
1427 &extension_sync_list);
1428 extension_sync_bundle_.GetExtensionSyncDataListHelper(terminated_extensions_,
1429 &extension_sync_list);
1430
1431 std::vector<extensions::ExtensionSyncData> pending_extensions =
1432 extension_sync_bundle_.GetPendingData();
1433 extension_sync_list.insert(extension_sync_list.begin(),
1434 pending_extensions.begin(),
1435 pending_extensions.end());
[email protected]3bdba0d2011-08-23 07:17:301436
1437 return extension_sync_list;
[email protected]b05fb9ff2011-04-23 00:07:561438}
1439
[email protected]5db9ada2012-04-11 13:48:201440std::vector<extensions::AppSyncData> ExtensionService::GetAppSyncDataList()
1441 const {
1442 std::vector<extensions::AppSyncData> app_sync_list;
1443 app_sync_bundle_.GetAppSyncDataListHelper(extensions_, &app_sync_list);
1444 app_sync_bundle_.GetAppSyncDataListHelper(disabled_extensions_,
1445 &app_sync_list);
1446 app_sync_bundle_.GetAppSyncDataListHelper(terminated_extensions_,
1447 &app_sync_list);
1448
1449 std::vector<extensions::AppSyncData> pending_apps =
1450 app_sync_bundle_.GetPendingData();
1451 app_sync_list.insert(app_sync_list.begin(),
1452 pending_apps.begin(),
1453 pending_apps.end());
1454
1455 return app_sync_list;
1456}
1457
1458bool ExtensionService::ProcessExtensionSyncData(
1459 const extensions::ExtensionSyncData& extension_sync_data) {
1460 if (!ProcessExtensionSyncDataHelper(extension_sync_data,
[email protected]a4a147652012-07-03 23:41:321461 syncer::EXTENSIONS)) {
[email protected]5db9ada2012-04-11 13:48:201462 extension_sync_bundle_.AddPendingExtension(extension_sync_data.id(),
1463 extension_sync_data);
1464 CheckForUpdatesSoon();
1465 return false;
1466 }
1467
1468 return true;
1469}
1470
1471bool ExtensionService::ProcessAppSyncData(
1472 const extensions::AppSyncData& app_sync_data) {
1473 const std::string& id = app_sync_data.id();
1474 const Extension* extension = GetInstalledExtension(id);
1475 bool extension_installed = (extension != NULL);
1476
1477 if (app_sync_data.app_launch_ordinal().IsValid() &&
1478 app_sync_data.page_ordinal().IsValid()) {
1479 extension_prefs_->extension_sorting()->SetAppLaunchOrdinal(
1480 id,
1481 app_sync_data.app_launch_ordinal());
1482 extension_prefs_->extension_sorting()->SetPageOrdinal(
1483 id,
1484 app_sync_data.page_ordinal());
1485 }
1486
1487 if (extension_installed) {
1488 if (app_sync_data.notifications_disabled() !=
1489 extension_prefs_->IsAppNotificationDisabled(id)) {
1490 extension_prefs_->SetAppNotificationDisabled(
1491 id, app_sync_data.notifications_disabled());
1492 }
1493 }
1494
1495 if (!ProcessExtensionSyncDataHelper(app_sync_data.extension_sync_data(),
[email protected]a4a147652012-07-03 23:41:321496 syncer::APPS)) {
[email protected]5db9ada2012-04-11 13:48:201497 app_sync_bundle_.AddPendingApp(id, app_sync_data);
1498 CheckForUpdatesSoon();
1499 return false;
1500 }
1501
1502 return true;
1503}
1504
1505bool ExtensionService::IsCorrectSyncType(const Extension& extension,
[email protected]a4a147652012-07-03 23:41:321506 syncer::ModelType type) const {
1507 if (type == syncer::EXTENSIONS &&
[email protected]31bb5ee62012-09-12 22:58:401508 extension.GetSyncType() == Extension::SYNC_TYPE_EXTENSION) {
[email protected]5db9ada2012-04-11 13:48:201509 return true;
[email protected]31bb5ee62012-09-12 22:58:401510 }
[email protected]5db9ada2012-04-11 13:48:201511
[email protected]a4a147652012-07-03 23:41:321512 if (type == syncer::APPS &&
[email protected]31bb5ee62012-09-12 22:58:401513 extension.GetSyncType() == Extension::SYNC_TYPE_APP) {
[email protected]5db9ada2012-04-11 13:48:201514 return true;
[email protected]31bb5ee62012-09-12 22:58:401515 }
[email protected]5db9ada2012-04-11 13:48:201516
1517 return false;
1518}
1519
1520bool ExtensionService::ProcessExtensionSyncDataHelper(
1521 const extensions::ExtensionSyncData& extension_sync_data,
[email protected]a4a147652012-07-03 23:41:321522 syncer::ModelType type) {
[email protected]3bdba0d2011-08-23 07:17:301523 const std::string& id = extension_sync_data.id();
[email protected]96e989b2011-08-30 19:35:061524 const Extension* extension = GetInstalledExtension(id);
1525
1526 // TODO(bolms): we should really handle this better. The particularly bad
1527 // case is where an app becomes an extension or vice versa, and we end up with
1528 // a zombie extension that won't go away.
[email protected]5db9ada2012-04-11 13:48:201529 if (extension && !IsCorrectSyncType(*extension, type))
1530 return true;
[email protected]90310d92011-04-17 07:35:041531
1532 // Handle uninstalls first.
[email protected]3bdba0d2011-08-23 07:17:301533 if (extension_sync_data.uninstalled()) {
[email protected]90310d92011-04-17 07:35:041534 std::string error;
1535 if (!UninstallExtensionHelper(this, id)) {
1536 LOG(WARNING) << "Could not uninstall extension " << id
1537 << " for sync";
1538 }
[email protected]5db9ada2012-04-11 13:48:201539 return true;
[email protected]90310d92011-04-17 07:35:041540 }
1541
[email protected]caf2d442012-05-04 10:00:501542 // Extension from sync was uninstalled by the user as external extensions.
1543 // Honor user choice and skip installation/enabling.
1544 if (IsExternalExtensionUninstalled(id)) {
1545 LOG(WARNING) << "Extension with id " << id
1546 << " from sync was uninstalled as external extension";
1547 return true;
1548 }
1549
[email protected]06f92562011-04-29 19:27:311550 // Set user settings.
[email protected]7e9e2422012-12-13 19:54:191551 // If the extension has been disabled from sync, it may not have
1552 // been installed yet, so we don't know if the disable reason was a
1553 // permissions increase. That will be updated once InitializePermissions
1554 // is called for it.
[email protected]31bb5ee62012-09-12 22:58:401555 if (extension_sync_data.enabled())
[email protected]06f92562011-04-29 19:27:311556 EnableExtension(id);
[email protected]31bb5ee62012-09-12 22:58:401557 else
[email protected]7e9e2422012-12-13 19:54:191558 DisableExtension(id, Extension::DISABLE_UNKNOWN_FROM_SYNC);
[email protected]81b14cc2011-04-29 00:39:371559
[email protected]406b5a92011-11-08 11:58:261560 // We need to cache some version information here because setting the
1561 // incognito flag invalidates the |extension| pointer (it reloads the
1562 // extension).
[email protected]9060d8b02012-01-13 02:14:301563 bool extension_installed = (extension != NULL);
[email protected]406b5a92011-11-08 11:58:261564 int result = extension ?
1565 extension->version()->CompareTo(extension_sync_data.version()) : 0;
1566 SetIsIncognitoEnabled(id, extension_sync_data.incognito_enabled());
1567 extension = NULL; // No longer safe to use.
1568
1569 if (extension_installed) {
[email protected]06f92562011-04-29 19:27:311570 // If the extension is already installed, check if it's outdated.
[email protected]90310d92011-04-17 07:35:041571 if (result < 0) {
1572 // Extension is outdated.
[email protected]5db9ada2012-04-11 13:48:201573 return false;
[email protected]b020dc12011-12-06 04:35:421574 }
[email protected]06f92562011-04-29 19:27:311575 } else {
[email protected]06f92562011-04-29 19:27:311576 // TODO(akalin): Replace silent update with a list of enabled
1577 // permissions.
[email protected]6cc7dbae2011-04-29 21:18:331578 const bool kInstallSilently = true;
[email protected]5db9ada2012-04-11 13:48:201579
[email protected]a4a147652012-07-03 23:41:321580 CHECK(type == syncer::EXTENSIONS || type == syncer::APPS);
[email protected]5db9ada2012-04-11 13:48:201581 ExtensionFilter filter =
[email protected]a4a147652012-07-03 23:41:321582 (type == syncer::APPS) ? IsSyncableApp : IsSyncableExtension;
[email protected]5db9ada2012-04-11 13:48:201583
[email protected]06f92562011-04-29 19:27:311584 if (!pending_extension_manager()->AddFromSync(
1585 id,
[email protected]3bdba0d2011-08-23 07:17:301586 extension_sync_data.update_url(),
[email protected]5db9ada2012-04-11 13:48:201587 filter,
[email protected]6cc7dbae2011-04-29 21:18:331588 kInstallSilently)) {
[email protected]06f92562011-04-29 19:27:311589 LOG(WARNING) << "Could not add pending extension for " << id;
[email protected]3bdba0d2011-08-23 07:17:301590 // This means that the extension is already pending installation, with a
1591 // non-INTERNAL location. Add to pending_sync_data, even though it will
1592 // never be removed (we'll never install a syncable version of the
1593 // extension), so that GetAllSyncData() continues to send it.
[email protected]06f92562011-04-29 19:27:311594 }
[email protected]3bdba0d2011-08-23 07:17:301595 // Track pending extensions so that we can return them in GetAllSyncData().
[email protected]5db9ada2012-04-11 13:48:201596 return false;
[email protected]90310d92011-04-17 07:35:041597 }
[email protected]5db9ada2012-04-11 13:48:201598
1599 return true;
[email protected]90310d92011-04-17 07:35:041600}
1601
[email protected]c3cfb012011-04-06 22:07:351602bool ExtensionService::IsIncognitoEnabled(
1603 const std::string& extension_id) const {
[email protected]cdc7b1f42012-12-07 19:39:481604 const Extension* extension = GetInstalledExtension(extension_id);
1605 if (extension && !extension->can_be_incognito_enabled())
1606 return false;
[email protected]c3cfb012011-04-06 22:07:351607 // If this is an existing component extension we always allow it to
1608 // work in incognito mode.
[email protected]c3cfb012011-04-06 22:07:351609 if (extension && extension->location() == Extension::COMPONENT)
[email protected]cb0ce1e022010-03-10 19:54:411610 return true;
1611
1612 // Check the prefs.
[email protected]c3cfb012011-04-06 22:07:351613 return extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]db7331a2010-02-25 22:10:501614}
[email protected]55a35692010-02-11 23:25:211615
[email protected]c3cfb012011-04-06 22:07:351616void ExtensionService::SetIsIncognitoEnabled(
1617 const std::string& extension_id, bool enabled) {
[email protected]81b14cc2011-04-29 00:39:371618 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]cdc7b1f42012-12-07 19:39:481619 if (extension && !extension->can_be_incognito_enabled())
1620 return;
[email protected]c3cfb012011-04-06 22:07:351621 if (extension && extension->location() == Extension::COMPONENT) {
[email protected]b873cd92012-02-09 21:51:481622 // This shouldn't be called for component extensions unless they are
1623 // syncable.
1624 DCHECK(extension->IsSyncable());
1625
1626 // If we are here, make sure the we aren't trying to change the value.
1627 DCHECK_EQ(enabled, IsIncognitoEnabled(extension_id));
1628
[email protected]c3cfb012011-04-06 22:07:351629 return;
1630 }
1631
[email protected]8c6c8c22011-03-09 12:52:341632 // Broadcast unloaded and loaded events to update browser state. Only bother
1633 // if the value changed and the extension is actually enabled, since there is
1634 // no UI otherwise.
[email protected]c3cfb012011-04-06 22:07:351635 bool old_enabled = extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]8c6c8c22011-03-09 12:52:341636 if (enabled == old_enabled)
1637 return;
1638
[email protected]c3cfb012011-04-06 22:07:351639 extension_prefs_->SetIsIncognitoEnabled(extension_id, enabled);
[email protected]81b14cc2011-04-29 00:39:371640
[email protected]ffbf3f12011-12-09 22:10:331641 bool extension_is_enabled = extensions_.Contains(extension_id);
[email protected]bd9892eb2011-11-11 19:41:341642
1643 // When we reload the extension the ID may be invalidated if we've passed it
1644 // by const ref everywhere. Make a copy to be safe.
1645 std::string id = extension_id;
[email protected]406b5a92011-11-08 11:58:261646 if (extension_is_enabled)
[email protected]ffbf3f12011-12-09 22:10:331647 ReloadExtension(id);
[email protected]3bdba0d2011-08-23 07:17:301648
[email protected]406b5a92011-11-08 11:58:261649 // Reloading the extension invalidates the |extension| pointer.
[email protected]bd9892eb2011-11-11 19:41:341650 extension = GetInstalledExtension(id);
[email protected]3bdba0d2011-08-23 07:17:301651 if (extension)
1652 SyncExtensionChangeIfNeeded(*extension);
[email protected]55a35692010-02-11 23:25:211653}
1654
[email protected]aa7599d2011-10-28 07:24:321655void ExtensionService::SetAppNotificationSetupDone(
1656 const std::string& extension_id,
[email protected]b2689a902011-12-01 00:41:091657 const std::string& oauth_client_id) {
[email protected]aa7599d2011-10-28 07:24:321658 const Extension* extension = GetInstalledExtension(extension_id);
1659 // This method is called when the user sets up app notifications.
1660 // So it is not expected to be called until the extension is installed.
1661 if (!extension) {
1662 NOTREACHED();
1663 return;
1664 }
[email protected]b2689a902011-12-01 00:41:091665 extension_prefs_->SetAppNotificationClientId(extension_id, oauth_client_id);
[email protected]aa7599d2011-10-28 07:24:321666 SyncExtensionChangeIfNeeded(*extension);
1667}
1668
1669void ExtensionService::SetAppNotificationDisabled(
1670 const std::string& extension_id,
1671 bool value) {
1672 const Extension* extension = GetInstalledExtension(extension_id);
1673 // This method is called when the user enables/disables app notifications.
1674 // So it is not expected to be called until the extension is installed.
1675 if (!extension) {
1676 NOTREACHED();
1677 return;
1678 }
[email protected]97e856b12011-12-14 00:21:131679 if (value)
1680 UMA_HISTOGRAM_COUNTS("Apps.SetAppNotificationsDisabled", 1);
1681 else
1682 UMA_HISTOGRAM_COUNTS("Apps.SetAppNotificationsEnabled", 1);
[email protected]aa7599d2011-10-28 07:24:321683 extension_prefs_->SetAppNotificationDisabled(extension_id, value);
1684 SyncExtensionChangeIfNeeded(*extension);
1685}
1686
[email protected]dc9a74f72012-08-17 18:07:211687bool ExtensionService::CanCrossIncognito(const Extension* extension) const {
[email protected]2a8f24e2010-11-03 21:37:051688 // We allow the extension to see events and data from another profile iff it
1689 // uses "spanning" behavior and it has incognito access. "split" mode
1690 // extensions only see events for a matching profile.
[email protected]0c41298f2011-09-13 21:17:221691 CHECK(extension);
[email protected]c3cfb012011-04-06 22:07:351692 return IsIncognitoEnabled(extension->id()) &&
1693 !extension->incognito_split_mode();
[email protected]2a8f24e2010-11-03 21:37:051694}
1695
[email protected]5ef7b002011-05-12 23:16:201696bool ExtensionService::CanLoadInIncognito(const Extension* extension) const {
1697 if (extension->is_hosted_app())
1698 return true;
1699 // Packaged apps and regular extensions need to be enabled specifically for
1700 // incognito (and split mode should be set).
1701 return extension->incognito_split_mode() &&
1702 IsIncognitoEnabled(extension->id());
1703}
1704
[email protected]168389f2011-12-20 17:12:481705void ExtensionService::OnExtensionMoved(
1706 const std::string& moved_extension_id,
1707 const std::string& predecessor_extension_id,
1708 const std::string& successor_extension_id) {
[email protected]3569b502012-01-12 20:08:231709 extension_prefs_->extension_sorting()->OnExtensionMoved(
1710 moved_extension_id,
1711 predecessor_extension_id,
1712 successor_extension_id);
[email protected]168389f2011-12-20 17:12:481713
1714 const Extension* extension = GetInstalledExtension(moved_extension_id);
1715 if (extension)
1716 SyncExtensionChangeIfNeeded(*extension);
1717}
1718
[email protected]dc9a74f72012-08-17 18:07:211719bool ExtensionService::AllowFileAccess(const Extension* extension) const {
[email protected]05c82182010-06-24 17:49:081720 return (CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]334e04a2010-06-24 23:34:441721 switches::kDisableExtensionsFileAccessCheck) ||
[email protected]05c82182010-06-24 17:49:081722 extension_prefs_->AllowFileAccess(extension->id()));
1723}
1724
[email protected]eaa7dd182010-12-14 11:09:001725void ExtensionService::SetAllowFileAccess(const Extension* extension,
[email protected]3aff9ad2011-04-01 20:26:481726 bool allow) {
1727 // Reload to update browser state. Only bother if the value changed and the
1728 // extension is actually enabled, since there is no UI otherwise.
1729 bool old_allow = AllowFileAccess(extension);
1730 if (allow == old_allow)
1731 return;
1732
[email protected]05c82182010-06-24 17:49:081733 extension_prefs_->SetAllowFileAccess(extension->id(), allow);
[email protected]3aff9ad2011-04-01 20:26:481734
[email protected]84df8332011-12-06 18:22:461735 bool extension_is_enabled = extensions_.Contains(extension->id());
[email protected]3aff9ad2011-04-01 20:26:481736 if (extension_is_enabled)
1737 ReloadExtension(extension->id());
[email protected]05c82182010-06-24 17:49:081738}
1739
[email protected]8e4560b62011-01-14 10:09:141740// Some extensions will autoupdate themselves externally from Chrome. These
1741// are typically part of some larger client application package. To support
1742// these, the extension will register its location in the the preferences file
1743// (and also, on Windows, in the registry) and this code will periodically
1744// check that location for a .crx file, which it will then install locally if
1745// a new version is available.
1746// Errors are reported through ExtensionErrorReporter. Succcess is not
1747// reported.
[email protected]eaa7dd182010-12-14 11:09:001748void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:121749 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]8e4560b62011-01-14 10:09:141750
1751 // Note that this installation is intentionally silent (since it didn't
1752 // go through the front-end). Extensions that are registered in this
1753 // way are effectively considered 'pre-bundled', and so implicitly
1754 // trusted. In general, if something has HKLM or filesystem access,
1755 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:121756
[email protected]8e4560b62011-01-14 10:09:141757 // Ask each external extension provider to give us a call back for each
1758 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
[email protected]5df038b2012-07-16 19:03:271759 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141760 for (i = external_extension_providers_.begin();
1761 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:271762 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:141763 provider->VisitRegisteredExtension();
1764 }
1765
[email protected]50067e52011-10-20 23:17:071766 // Do any required work that we would have done after completion of all
1767 // providers.
1768 if (external_extension_providers_.empty()) {
1769 OnAllExternalProvidersReady();
1770 }
[email protected]9f1087e2009-06-15 17:29:321771}
1772
[email protected]50067e52011-10-20 23:17:071773void ExtensionService::OnExternalProviderReady(
[email protected]5df038b2012-07-16 19:03:271774 const extensions::ExternalProviderInterface* provider) {
[email protected]a29a517a2011-01-21 21:11:121775 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]50067e52011-10-20 23:17:071776 CHECK(provider->IsReady());
[email protected]a29a517a2011-01-21 21:11:121777
1778 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:141779 // if all of them are finished. So we check them first.
[email protected]94fde232012-04-27 10:22:301780 if (AreAllExternalProvidersReady())
1781 OnAllExternalProvidersReady();
1782}
1783
1784bool ExtensionService::AreAllExternalProvidersReady() const {
[email protected]5df038b2012-07-16 19:03:271785 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141786 for (i = external_extension_providers_.begin();
1787 i != external_extension_providers_.end(); ++i) {
[email protected]94fde232012-04-27 10:22:301788 if (!i->get()->IsReady())
1789 return false;
[email protected]8e4560b62011-01-14 10:09:141790 }
[email protected]94fde232012-04-27 10:22:301791 return true;
[email protected]50067e52011-10-20 23:17:071792}
1793
1794void ExtensionService::OnAllExternalProvidersReady() {
1795 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]94fde232012-04-27 10:22:301796 base::TimeDelta elapsed = base::Time::Now() - profile_->GetStartTime();
1797 UMA_HISTOGRAM_TIMES("Extension.ExternalProvidersReadyAfter", elapsed);
[email protected]50067e52011-10-20 23:17:071798
1799 // Install any pending extensions.
[email protected]94fde232012-04-27 10:22:301800 if (update_once_all_providers_are_ready_ && updater()) {
1801 update_once_all_providers_are_ready_ = false;
[email protected]42774e42012-11-14 21:01:061802 updater()->CheckNow(extensions::ExtensionUpdater::CheckParams());
[email protected]8e4560b62011-01-14 10:09:141803 }
1804
1805 // Uninstall all the unclaimed extensions.
[email protected]45759612012-07-10 17:21:231806 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> extensions_info(
[email protected]8e4560b62011-01-14 10:09:141807 extension_prefs_->GetInstalledExtensionsInfo());
1808 for (size_t i = 0; i < extensions_info->size(); ++i) {
1809 ExtensionInfo* info = extensions_info->at(i).get();
1810 if (Extension::IsExternalLocation(info->extension_location))
1811 CheckExternalUninstall(info->extension_id);
1812 }
[email protected]e5af875f2011-10-10 21:09:141813 IdentifyAlertableExtensions();
[email protected]e5af875f2011-10-10 21:09:141814}
1815
1816void ExtensionService::IdentifyAlertableExtensions() {
1817 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1818
[email protected]07c9f2f42012-02-29 18:45:221819 // Build up the lists of extensions that require acknowledgment. If this is
1820 // the first time, grandfather extensions that would have caused
1821 // notification.
[email protected]89226982012-07-16 20:09:181822 extension_error_ui_.reset(ExtensionErrorUI::Create(this));
[email protected]e5af875f2011-10-10 21:09:141823
[email protected]a9aa5932012-01-25 08:27:401824 bool did_show_alert = false;
[email protected]89226982012-07-16 20:09:181825 if (PopulateExtensionErrorUI(extension_error_ui_.get())) {
[email protected]e5af875f2011-10-10 21:09:141826 if (extension_prefs_->SetAlertSystemFirstRun()) {
[email protected]a9aa5932012-01-25 08:27:401827 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]89226982012-07-16 20:09:181828 did_show_alert = extension_error_ui_->ShowErrorInBubbleView();
[email protected]e5af875f2011-10-10 21:09:141829 } else {
1830 // First run. Just acknowledge all the extensions, silently, by
1831 // shortcutting the display of the UI and going straight to the
1832 // callback for pressing the Accept button.
[email protected]a9aa5932012-01-25 08:27:401833 HandleExtensionAlertAccept();
[email protected]e5af875f2011-10-10 21:09:141834 }
1835 }
[email protected]a9aa5932012-01-25 08:27:401836
[email protected]612a1cb12012-10-17 13:18:031837 UpdateExternalExtensionAlert();
1838
[email protected]a9aa5932012-01-25 08:27:401839 if (!did_show_alert)
[email protected]89226982012-07-16 20:09:181840 extension_error_ui_.reset();
[email protected]e5af875f2011-10-10 21:09:141841}
1842
[email protected]89226982012-07-16 20:09:181843bool ExtensionService::PopulateExtensionErrorUI(
1844 ExtensionErrorUI* extension_error_ui) {
[email protected]07c9f2f42012-02-29 18:45:221845 bool needs_alert = false;
[email protected]695b5712012-12-06 23:55:281846
1847 // Extensions that are blacklisted.
1848 for (ExtensionSet::const_iterator it = blacklisted_extensions_.begin();
1849 it != blacklisted_extensions_.end(); ++it) {
1850 std::string id = (*it)->id();
1851 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(id)) {
1852 extension_error_ui->AddBlacklistedExtension(id);
1853 needs_alert = true;
1854 }
1855 }
1856
[email protected]07c9f2f42012-02-29 18:45:221857 for (ExtensionSet::const_iterator iter = extensions_.begin();
1858 iter != extensions_.end(); ++iter) {
1859 const Extension* e = *iter;
[email protected]695b5712012-12-06 23:55:281860
1861 // Extensions disabled by policy. Note: this no longer includes blacklisted
1862 // extensions, though we still show the same UI.
[email protected]fdd679b2012-11-15 20:49:391863 if (!system_->management_policy()->UserMayLoad(e, NULL)) {
[email protected]07c9f2f42012-02-29 18:45:221864 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(e->id())) {
[email protected]89226982012-07-16 20:09:181865 extension_error_ui->AddBlacklistedExtension(e->id());
[email protected]07c9f2f42012-02-29 18:45:221866 needs_alert = true;
1867 }
1868 }
[email protected]695b5712012-12-06 23:55:281869
1870 // Orphaned extensions.
[email protected]07c9f2f42012-02-29 18:45:221871 if (extension_prefs_->IsExtensionOrphaned(e->id())) {
1872 if (!extension_prefs_->IsOrphanedExtensionAcknowledged(e->id())) {
[email protected]89226982012-07-16 20:09:181873 extension_error_ui->AddOrphanedExtension(e->id());
[email protected]07c9f2f42012-02-29 18:45:221874 needs_alert = true;
1875 }
1876 }
1877 }
[email protected]695b5712012-12-06 23:55:281878
[email protected]07c9f2f42012-02-29 18:45:221879 return needs_alert;
1880}
1881
[email protected]a9aa5932012-01-25 08:27:401882void ExtensionService::HandleExtensionAlertClosed() {
[email protected]89226982012-07-16 20:09:181883 extension_error_ui_.reset();
[email protected]62433d32011-10-12 22:33:121884}
1885
[email protected]a9aa5932012-01-25 08:27:401886void ExtensionService::HandleExtensionAlertAccept() {
[email protected]612a1cb12012-10-17 13:18:031887 const ExtensionIdSet* extension_ids =
1888 extension_error_ui_->get_blacklisted_extension_ids();
[email protected]e5af875f2011-10-10 21:09:141889 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1890 iter != extension_ids->end(); ++iter) {
1891 extension_prefs_->AcknowledgeBlacklistedExtension(*iter);
1892 }
[email protected]89226982012-07-16 20:09:181893 extension_ids = extension_error_ui_->get_orphaned_extension_ids();
[email protected]e5af875f2011-10-10 21:09:141894 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1895 iter != extension_ids->end(); ++iter) {
1896 extension_prefs_->AcknowledgeOrphanedExtension(*iter);
1897 }
1898}
1899
[email protected]47fc70c2011-12-06 07:29:511900void ExtensionService::AcknowledgeExternalExtension(const std::string& id) {
1901 extension_prefs_->AcknowledgeExternalExtension(id);
[email protected]612a1cb12012-10-17 13:18:031902 UpdateExternalExtensionAlert();
[email protected]47fc70c2011-12-06 07:29:511903}
1904
[email protected]d96eb512012-11-01 23:44:081905bool ExtensionService::IsUnacknowledgedExternalExtension(
1906 const Extension* extension) {
[email protected]b82d18da2012-11-09 00:11:321907 if (!FeatureSwitch::prompt_for_external_extensions()->IsEnabled())
1908 return false;
1909
[email protected]d96eb512012-11-01 23:44:081910 return (Extension::IsExternalLocation(extension->location()) &&
1911 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id()) &&
1912 !(extension_prefs_->GetDisableReasons(extension->id()) &
1913 Extension::DISABLE_SIDELOAD_WIPEOUT));
1914}
1915
[email protected]89226982012-07-16 20:09:181916void ExtensionService::HandleExtensionAlertDetails() {
1917 extension_error_ui_->ShowExtensions();
[email protected]6c751e72010-11-23 10:11:101918}
1919
[email protected]612a1cb12012-10-17 13:18:031920void ExtensionService::UpdateExternalExtensionAlert() {
[email protected]41070e8d2012-10-24 01:34:361921 if (!FeatureSwitch::prompt_for_external_extensions()->IsEnabled())
[email protected]612a1cb12012-10-17 13:18:031922 return;
1923
1924 const Extension* extension = NULL;
1925 for (ExtensionSet::const_iterator iter = disabled_extensions_.begin();
1926 iter != disabled_extensions_.end(); ++iter) {
1927 const Extension* e = *iter;
[email protected]d96eb512012-11-01 23:44:081928 if (IsUnacknowledgedExternalExtension(e)) {
1929 extension = e;
1930 break;
[email protected]612a1cb12012-10-17 13:18:031931 }
1932 }
1933
1934 if (extension) {
[email protected]d96eb512012-11-01 23:44:081935 if (!extensions::HasExternalInstallError(this)) {
1936 if (extension_prefs_->IncrementAcknowledgePromptCount(extension->id()) >
[email protected]612a1cb12012-10-17 13:18:031937 kMaxExtensionAcknowledgePromptCount) {
[email protected]d96eb512012-11-01 23:44:081938 // Stop prompting for this extension, and check if there's another
1939 // one that needs prompting.
[email protected]612a1cb12012-10-17 13:18:031940 extension_prefs_->AcknowledgeExternalExtension(extension->id());
[email protected]d96eb512012-11-01 23:44:081941 UpdateExternalExtensionAlert();
1942 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
1943 EXTERNAL_EXTENSION_IGNORED,
1944 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
1945 return;
[email protected]612a1cb12012-10-17 13:18:031946 }
[email protected]d96eb512012-11-01 23:44:081947 extensions::AddExternalInstallError(this, extension);
[email protected]612a1cb12012-10-17 13:18:031948 }
1949 } else {
1950 extensions::RemoveExternalInstallError(this);
1951 }
[email protected]612a1cb12012-10-17 13:18:031952}
1953
[email protected]a9f39a312010-12-23 22:14:271954void ExtensionService::UnloadExtension(
1955 const std::string& extension_id,
[email protected]814a7bf0f2011-08-13 05:30:591956 extension_misc::UnloadedExtensionReason reason) {
[email protected]27e469a2010-01-11 20:35:091957 // Make sure the extension gets deleted after we return from this function.
[email protected]695b5712012-12-06 23:55:281958 int include_mask = INCLUDE_EVERYTHING & ~INCLUDE_TERMINATED;
[email protected]9adb9692010-10-29 23:14:021959 scoped_refptr<const Extension> extension(
[email protected]695b5712012-12-06 23:55:281960 GetExtensionById(extension_id, include_mask));
[email protected]631cf822009-05-15 07:01:251961
[email protected]fa0c96732010-11-17 00:14:231962 // This method can be called via PostTask, so the extension may have been
1963 // unloaded by the time this runs.
[email protected]dd163fb02011-05-04 22:22:171964 if (!extension) {
1965 // In case the extension may have crashed/uninstalled. Allow the profile to
1966 // clean up its RequestContexts.
[email protected]31d8f5f22012-04-02 15:22:081967 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]fa0c96732010-11-17 00:14:231968 return;
[email protected]dd163fb02011-05-04 22:22:171969 }
[email protected]0c6da502009-08-14 22:32:391970
[email protected]1eb175082010-02-10 09:26:161971 // Keep information about the extension so that we can reload it later
1972 // even if it's not permanently installed.
1973 unloaded_extension_paths_[extension->id()] = extension->path();
1974
[email protected]f17dbd42010-08-16 23:21:101975 // Clean up if the extension is meant to be enabled after a reload.
1976 disabled_extension_paths_.erase(extension->id());
1977
[email protected]d7e9a862010-11-03 21:57:491978 // Clean up runtime data.
1979 extension_runtime_data_.erase(extension_id);
1980
[email protected]060d4972012-07-19 17:22:391981 if (disabled_extensions_.Contains(extension->id())) {
[email protected]a9f39a312010-12-23 22:14:271982 UnloadedExtensionInfo details(extension, reason);
1983 details.already_disabled = true;
[email protected]84df8332011-12-06 18:22:461984 disabled_extensions_.Remove(extension->id());
[email protected]ad50def52011-10-19 23:17:071985 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271986 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]6c2381d2011-10-19 02:52:531987 content::Source<Profile>(profile_),
1988 content::Details<UnloadedExtensionInfo>(&details));
[email protected]dd163fb02011-05-04 22:22:171989 // Make sure the profile cleans up its RequestContexts when an already
1990 // disabled extension is unloaded (since they are also tracking the disabled
1991 // extensions).
[email protected]31d8f5f22012-04-02 15:22:081992 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]0c6da502009-08-14 22:32:391993 return;
1994 }
1995
[email protected]060d4972012-07-19 17:22:391996 // Remove the extension from our list.
[email protected]84df8332011-12-06 18:22:461997 extensions_.Remove(extension->id());
[email protected]631cf822009-05-15 07:01:251998
[email protected]a9f39a312010-12-23 22:14:271999 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]631cf822009-05-15 07:01:252000}
2001
[email protected]eaa7dd182010-12-14 11:09:002002void ExtensionService::UnloadAllExtensions() {
[email protected]31bb5ee62012-09-12 22:58:402003 profile_->GetExtensionSpecialStoragePolicy()->RevokeRightsForAllExtensions();
[email protected]c8d407e2011-04-28 21:27:172004
[email protected]84df8332011-12-06 18:22:462005 extensions_.Clear();
2006 disabled_extensions_.Clear();
2007 terminated_extensions_.Clear();
[email protected]d7e9a862010-11-03 21:57:492008 extension_runtime_data_.clear();
[email protected]cd500f72010-06-25 23:44:322009
[email protected]c6e4a3412009-06-24 15:45:292010 // TODO(erikkay) should there be a notification for this? We can't use
2011 // EXTENSION_UNLOADED since that implies that the extension has been disabled
2012 // or uninstalled, and UnloadAll is just part of shutdown.
[email protected]9f1087e2009-06-15 17:29:322013}
2014
[email protected]eaa7dd182010-12-14 11:09:002015void ExtensionService::ReloadExtensions() {
[email protected]9f1087e2009-06-15 17:29:322016 UnloadAllExtensions();
[email protected]d8c8f25f2011-11-02 18:18:012017 component_loader_->LoadAll();
2018 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]9f1087e2009-06-15 17:29:322019}
2020
[email protected]eaa7dd182010-12-14 11:09:002021void ExtensionService::GarbageCollectExtensions() {
[email protected]2f8757c32012-06-19 19:17:472022 if (extension_prefs_->pref_service()->ReadOnly())
2023 return;
2024
[email protected]9bd9a6862012-11-29 09:24:222025 if (pending_extension_manager()->HasPendingExtensions()) {
2026 // Don't garbage collect while there are pending installations, which may
2027 // be using the temporary installation directory. Try to garbage collect
2028 // again later.
2029 MessageLoop::current()->PostDelayedTask(
2030 FROM_HERE,
2031 base::Bind(&ExtensionService::GarbageCollectExtensions, AsWeakPtr()),
2032 base::TimeDelta::FromSeconds(kGarbageCollectRetryDelay));
2033 return;
2034 }
2035
[email protected]45759612012-07-10 17:21:232036 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> info(
[email protected]2f8757c32012-06-19 19:17:472037 extension_prefs_->GetInstalledExtensionsInfo());
2038
[email protected]0db124b02012-11-07 04:55:052039 std::multimap<std::string, FilePath> extension_paths;
[email protected]2f8757c32012-06-19 19:17:472040 for (size_t i = 0; i < info->size(); ++i)
[email protected]0db124b02012-11-07 04:55:052041 extension_paths.insert(std::make_pair(info->at(i)->extension_id,
2042 info->at(i)->extension_path));
2043
[email protected]399583b2012-12-11 09:33:422044 info = extension_prefs_->GetAllDelayedInstallInfo();
[email protected]0db124b02012-11-07 04:55:052045 for (size_t i = 0; i < info->size(); ++i)
2046 extension_paths.insert(std::make_pair(info->at(i)->extension_id,
2047 info->at(i)->extension_path));
[email protected]2f8757c32012-06-19 19:17:472048
[email protected]7f8f24f2012-11-15 19:40:142049 if (!GetFileTaskRunner()->PostTask(
2050 FROM_HERE,
[email protected]2f8757c32012-06-19 19:17:472051 base::Bind(
2052 &extension_file_util::GarbageCollectExtensions,
2053 install_directory_,
[email protected]31bb5ee62012-09-12 22:58:402054 extension_paths))) {
[email protected]2f8757c32012-06-19 19:17:472055 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:402056 }
[email protected]f6ccd652010-11-17 00:48:342057
[email protected]f766a9d2012-07-11 01:57:442058#if defined(ENABLE_THEMES)
[email protected]f6ccd652010-11-17 00:48:342059 // Also garbage-collect themes. We check |profile_| to be
2060 // defensive; in the future, we may call GarbageCollectExtensions()
2061 // from somewhere other than Init() (e.g., in a timer).
2062 if (profile_) {
[email protected]d5949312012-12-03 22:13:302063 ThemeServiceFactory::GetForProfile(profile_)->RemoveUnusedThemes();
[email protected]f6ccd652010-11-17 00:48:342064 }
[email protected]f766a9d2012-07-11 01:57:442065#endif
[email protected]3cf4f0992009-02-03 23:00:302066}
2067
[email protected]fb82dcd2012-03-21 14:15:462068void ExtensionService::SyncExtensionChangeIfNeeded(const Extension& extension) {
[email protected]5db9ada2012-04-11 13:48:202069 if (app_sync_bundle_.HandlesApp(extension)) {
2070 app_sync_bundle_.SyncChangeIfNeeded(extension);
2071 } else if (extension_sync_bundle_.HandlesExtension(extension)) {
2072 extension_sync_bundle_.SyncChangeIfNeeded(extension);
[email protected]fb82dcd2012-03-21 14:15:462073 }
2074}
2075
[email protected]eaa7dd182010-12-14 11:09:002076void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]31bb5ee62012-09-12 22:58:402077 if (updater_.get())
[email protected]93fd78f42009-07-10 16:43:172078 updater_->Start();
[email protected]fa6a9102010-11-22 15:38:502079
2080 ready_ = true;
[email protected]ad50def52011-10-19 23:17:072081 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272082 chrome::NOTIFICATION_EXTENSIONS_READY,
[email protected]6c2381d2011-10-19 02:52:532083 content::Source<Profile>(profile_),
[email protected]ad50def52011-10-19 23:17:072084 content::NotificationService::NoDetails());
[email protected]e72e8eb82009-06-18 17:21:512085}
2086
[email protected]fcb58a862012-05-01 01:03:152087void ExtensionService::AddExtension(const Extension* extension) {
[email protected]c8d407e2011-04-28 21:27:172088 // TODO(jstritar): We may be able to get rid of this branch by overriding the
2089 // default extension state to DISABLED when the --disable-extensions flag
2090 // is set (http://crbug.com/29067).
2091 if (!extensions_enabled() &&
2092 !extension->is_theme() &&
2093 extension->location() != Extension::COMPONENT &&
[email protected]330840c2012-08-29 22:21:012094 !Extension::IsExternalLocation(extension->location())) {
[email protected]fcb58a862012-05-01 01:03:152095 return;
[email protected]330840c2012-08-29 22:21:012096 }
[email protected]c8d407e2011-04-28 21:27:172097
2098 SetBeingUpgraded(extension, false);
2099
[email protected]1eb175082010-02-10 09:26:162100 // The extension is now loaded, remove its data from unloaded extension map.
2101 unloaded_extension_paths_.erase(extension->id());
2102
[email protected]bb7f40952011-01-13 00:21:202103 // If a terminated extension is loaded, remove it from the terminated list.
2104 UntrackTerminatedExtension(extension->id());
2105
[email protected]f17dbd42010-08-16 23:21:102106 // If the extension was disabled for a reload, then enable it.
2107 if (disabled_extension_paths_.erase(extension->id()) > 0)
2108 EnableExtension(extension->id());
2109
[email protected]d728e002010-12-08 04:46:232110 // Check if the extension's privileges have changed and disable the
2111 // extension if necessary.
[email protected]902fd7b2011-07-27 18:42:312112 InitializePermissions(extension);
[email protected]0c6da502009-08-14 22:32:392113
[email protected]215a7be2012-10-22 19:53:422114 // If this extension is a sideloaded extension and we've not performed a
2115 // wipeout before, we might disable this extension here.
2116 MaybeWipeout(extension);
2117
[email protected]695b5712012-12-06 23:55:282118 // Communicated to the Blacklist.
2119 std::set<std::string> already_in_blacklist;
2120
2121 if (extension_prefs_->IsExtensionBlacklisted(extension->id())) {
2122 // Don't check the Blacklist yet because it's asynchronous (we do it at
2123 // the end). This pre-emptive check is because we will always store the
2124 // blacklisted state of *installed* extensions in prefs, and it's important
2125 // not to re-enable blacklisted extensions.
2126 blacklisted_extensions_.Insert(extension);
2127 already_in_blacklist.insert(extension->id());
2128 } else if (extension_prefs_->IsExtensionDisabled(extension->id())) {
[email protected]6f6101832012-11-27 22:10:482129 disabled_extensions_.Insert(extension);
[email protected]3bdba0d2011-08-23 07:17:302130 SyncExtensionChangeIfNeeded(*extension);
[email protected]44d62b62012-04-11 00:06:032131 content::NotificationService::current()->Notify(
2132 chrome::NOTIFICATION_EXTENSION_UPDATE_DISABLED,
2133 content::Source<Profile>(profile_),
2134 content::Details<const Extension>(extension));
[email protected]62f051c2012-03-29 17:04:442135
[email protected]7e9e2422012-12-13 19:54:192136 // Show the extension disabled error if a permissions increase was the
2137 // only reason it was disabled.
2138 if (extension_prefs_->GetDisableReasons(extension->id()) ==
[email protected]44d62b62012-04-11 00:06:032139 Extension::DISABLE_PERMISSIONS_INCREASE) {
[email protected]62f051c2012-03-29 17:04:442140 extensions::AddExtensionDisabledError(this, extension);
2141 }
[email protected]695b5712012-12-06 23:55:282142 } else {
2143 // All apps that are displayed in the launcher are ordered by their ordinals
2144 // so we must ensure they have valid ordinals.
2145 if (extension->RequiresSortOrdinal()) {
2146 extension_prefs_->extension_sorting()->EnsureValidOrdinals(
2147 extension->id(), syncer::StringOrdinal());
2148 }
2149
2150 extensions_.Insert(extension);
2151 SyncExtensionChangeIfNeeded(*extension);
2152 NotifyExtensionLoaded(extension);
2153 DoPostLoadTasks(extension);
[email protected]f574c402012-12-04 23:20:312154 }
2155
[email protected]695b5712012-12-06 23:55:282156 // Lastly, begin the process for checking the blacklist status of extensions.
2157 // This may need to go to other threads so is asynchronous.
2158 std::set<std::string> id_set;
2159 id_set.insert(extension->id());
2160 blacklist_->GetBlacklistedIDs(
2161 id_set,
2162 base::Bind(&ExtensionService::ManageBlacklist,
2163 AsWeakPtr(),
2164 already_in_blacklist));
[email protected]aab98a52009-12-02 03:22:352165}
2166
[email protected]8c484b742012-11-29 06:05:362167void ExtensionService::AddComponentExtension(const Extension* extension) {
2168 const std::string old_version_string(
2169 extension_prefs_->GetVersionString(extension->id()));
2170 const Version old_version(old_version_string);
2171
2172 if (!old_version.IsValid() || !old_version.Equals(*extension->version())) {
2173 VLOG(1) << "Component extension " << extension->name() << " ("
2174 << extension->id() << ") installing/upgrading from '"
2175 << old_version_string << "' to " << extension->version()->GetString();
2176
2177 AddNewOrUpdatedExtension(extension,
[email protected]399583b2012-12-11 09:33:422178 Extension::ENABLED_COMPONENT,
2179 syncer::StringOrdinal());
[email protected]8c484b742012-11-29 06:05:362180 return;
2181 }
2182
2183 AddExtension(extension);
2184}
2185
[email protected]902fd7b2011-07-27 18:42:312186void ExtensionService::InitializePermissions(const Extension* extension) {
2187 // If the extension has used the optional permissions API, it will have a
2188 // custom set of active permissions defined in the extension prefs. Here,
2189 // we update the extension's active permissions based on the prefs.
[email protected]c2e66e12012-06-27 06:27:062190 scoped_refptr<PermissionSet> active_permissions =
[email protected]902fd7b2011-07-27 18:42:312191 extension_prefs()->GetActivePermissions(extension->id());
2192
2193 if (active_permissions.get()) {
2194 // We restrict the active permissions to be within the bounds defined in the
2195 // extension's manifest.
2196 // a) active permissions must be a subset of optional + default permissions
2197 // b) active permissions must contains all default permissions
[email protected]c2e66e12012-06-27 06:27:062198 scoped_refptr<PermissionSet> total_permissions =
2199 PermissionSet::CreateUnion(
[email protected]902fd7b2011-07-27 18:42:312200 extension->required_permission_set(),
2201 extension->optional_permission_set());
2202
2203 // Make sure the active permissions contain no more than optional + default.
[email protected]c2e66e12012-06-27 06:27:062204 scoped_refptr<PermissionSet> adjusted_active =
2205 PermissionSet::CreateIntersection(
[email protected]902fd7b2011-07-27 18:42:312206 total_permissions.get(), active_permissions.get());
2207
2208 // Make sure the active permissions contain the default permissions.
[email protected]c2e66e12012-06-27 06:27:062209 adjusted_active = PermissionSet::CreateUnion(
[email protected]902fd7b2011-07-27 18:42:312210 extension->required_permission_set(), adjusted_active.get());
2211
[email protected]c333e792012-01-06 16:57:392212 extensions::PermissionsUpdater perms_updater(profile());
2213 perms_updater.UpdateActivePermissions(extension, adjusted_active);
[email protected]902fd7b2011-07-27 18:42:312214 }
2215
[email protected]8d888c12010-11-30 00:00:252216 // We keep track of all permissions the user has granted each extension.
2217 // This allows extensions to gracefully support backwards compatibility
2218 // by including unknown permissions in their manifests. When the user
2219 // installs the extension, only the recognized permissions are recorded.
2220 // When the unknown permissions become recognized (e.g., through browser
2221 // upgrade), we can prompt the user to accept these new permissions.
2222 // Extensions can also silently upgrade to less permissions, and then
2223 // silently upgrade to a version that adds these permissions back.
2224 //
2225 // For example, pretend that Chrome 10 includes a permission "omnibox"
2226 // for an API that adds suggestions to the omnibox. An extension can
2227 // maintain backwards compatibility while still having "omnibox" in the
2228 // manifest. If a user installs the extension on Chrome 9, the browser
2229 // will record the permissions it recognized, not including "omnibox."
2230 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
2231 // will disable the extension and prompt the user to approve the increase
2232 // in privileges. The extension could then release a new version that
2233 // removes the "omnibox" permission. When the user upgrades, Chrome will
2234 // still remember that "omnibox" had been granted, so that if the
2235 // extension once again includes "omnibox" in an upgrade, the extension
2236 // can upgrade without requiring this user's approval.
[email protected]695b5712012-12-06 23:55:282237 const Extension* old = GetInstalledExtension(extension->id());
[email protected]8d888c12010-11-30 00:00:252238 bool is_extension_upgrade = old != NULL;
2239 bool is_privilege_increase = false;
[email protected]44d62b62012-04-11 00:06:032240 bool previously_disabled = false;
[email protected]eb5e4f92012-08-15 23:33:282241 int disable_reasons = extension_prefs_->GetDisableReasons(extension->id());
[email protected]8d888c12010-11-30 00:00:252242
[email protected]902fd7b2011-07-27 18:42:312243 // We only need to compare the granted permissions to the current permissions
2244 // if the extension is not allowed to silently increase its permissions.
[email protected]be083862012-09-01 03:53:452245 bool is_default_app_install =
2246 (!is_extension_upgrade && extension->was_installed_by_default());
2247 if (!(extension->CanSilentlyIncreasePermissions()
2248 || is_default_app_install)) {
[email protected]8d888c12010-11-30 00:00:252249 // Add all the recognized permissions if the granted permissions list
2250 // hasn't been initialized yet.
[email protected]c2e66e12012-06-27 06:27:062251 scoped_refptr<PermissionSet> granted_permissions =
[email protected]902fd7b2011-07-27 18:42:312252 extension_prefs_->GetGrantedPermissions(extension->id());
[email protected]0d3e4a22011-06-23 19:02:522253 CHECK(granted_permissions.get());
[email protected]8d888c12010-11-30 00:00:252254
2255 // Here, we check if an extension's privileges have increased in a manner
2256 // that requires the user's approval. This could occur because the browser
2257 // upgraded and recognized additional privileges, or an extension upgrades
2258 // to a version that requires additional privileges.
[email protected]0d3e4a22011-06-23 19:02:522259 is_privilege_increase =
[email protected]902fd7b2011-07-27 18:42:312260 granted_permissions->HasLessPrivilegesThan(
2261 extension->GetActivePermissions());
[email protected]8d888c12010-11-30 00:00:252262 }
2263
[email protected]be083862012-09-01 03:53:452264 // Silently grant all active permissions to default apps only on install.
2265 // After install they should behave like other apps.
[email protected]c856e9762012-10-12 19:42:382266 if (is_default_app_install)
2267 GrantPermissions(extension, true);
[email protected]be083862012-09-01 03:53:452268
[email protected]8d888c12010-11-30 00:00:252269 if (is_extension_upgrade) {
[email protected]66e26872010-12-03 20:07:252270 // Other than for unpacked extensions, CrxInstaller should have guaranteed
2271 // that we aren't downgrading.
2272 if (extension->location() != Extension::LOAD)
[email protected]31bb5ee62012-09-12 22:58:402273 CHECK_GE(extension->version()->CompareTo(*(old->version())), 0);
[email protected]8d888c12010-11-30 00:00:252274
2275 // Extensions get upgraded if the privileges are allowed to increase or
2276 // the privileges haven't increased.
2277 if (!is_privilege_increase) {
2278 SetBeingUpgraded(old, true);
2279 SetBeingUpgraded(extension, true);
2280 }
2281
[email protected]44d62b62012-04-11 00:06:032282 // If the extension was already disabled, suppress any alerts for becoming
2283 // disabled on permissions increase.
2284 previously_disabled = extension_prefs_->IsExtensionDisabled(old->id());
[email protected]7e9e2422012-12-13 19:54:192285 // Legacy disabled extensions do not have a disable reason. Infer that if
2286 // there was no permission increase, it was likely disabled by the user.
2287 if (previously_disabled && disable_reasons == Extension::DISABLE_NONE &&
2288 !extension_prefs_->DidExtensionEscalatePermissions(old->id())) {
2289 disable_reasons |= Extension::DISABLE_USER_ACTION;
[email protected]44d62b62012-04-11 00:06:032290 }
[email protected]7e9e2422012-12-13 19:54:192291 // Extensions that came to us disabled from sync need a similar inference,
2292 // except based on the new version's permissions.
2293 if (previously_disabled &&
2294 disable_reasons == Extension::DISABLE_UNKNOWN_FROM_SYNC) {
2295 // Remove the DISABLE_UNKNOWN_FROM_SYNC reason.
2296 extension_prefs_->ClearDisableReasons(extension->id());
2297 if (!is_privilege_increase)
2298 disable_reasons |= Extension::DISABLE_USER_ACTION;
2299 }
2300 if (disable_reasons != Extension::DISABLE_NONE)
2301 disable_reasons &= ~Extension::DISABLE_UNKNOWN_FROM_SYNC;
[email protected]44d62b62012-04-11 00:06:032302
[email protected]8d888c12010-11-30 00:00:252303 // To upgrade an extension in place, unload the old one and
2304 // then load the new one.
[email protected]814a7bf0f2011-08-13 05:30:592305 UnloadExtension(old->id(), extension_misc::UNLOAD_REASON_UPDATE);
[email protected]8d888c12010-11-30 00:00:252306 old = NULL;
2307 }
2308
2309 // Extension has changed permissions significantly. Disable it. A
2310 // notification should be sent by the caller.
2311 if (is_privilege_increase) {
[email protected]7e9e2422012-12-13 19:54:192312 disable_reasons |= Extension::DISABLE_PERMISSIONS_INCREASE;
[email protected]fe2dd7742011-04-19 22:52:492313 if (!extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
2314 RecordPermissionMessagesHistogram(
2315 extension, "Extensions.Permissions_AutoDisable");
2316 }
[email protected]b3317ad2011-04-28 23:46:002317 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
[email protected]8d888c12010-11-30 00:00:252318 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
[email protected]eb5e4f92012-08-15 23:33:282319 extension_prefs_->AddDisableReason(
2320 extension->id(),
2321 static_cast<Extension::DisableReason>(disable_reasons));
[email protected]8d888c12010-11-30 00:00:252322 }
2323}
2324
[email protected]215a7be2012-10-22 19:53:422325void ExtensionService::MaybeWipeout(
2326 const extensions::Extension* extension) {
[email protected]1ff4d282012-12-08 00:39:212327 if (!wipeout_is_active_)
[email protected]215a7be2012-10-22 19:53:422328 return;
2329
[email protected]1ff4d282012-12-08 00:39:212330 if (extension->GetType() != Extension::TYPE_EXTENSION)
[email protected]215a7be2012-10-22 19:53:422331 return;
2332
[email protected]1ff4d282012-12-08 00:39:212333 Extension::Location location = extension->location();
[email protected]eea97032012-12-14 00:16:182334 if (location != Extension::EXTERNAL_REGISTRY)
[email protected]1ff4d282012-12-08 00:39:212335 return;
2336
2337 if (extension_prefs_->IsExternalExtensionExcludedFromWipeout(extension->id()))
2338 return;
2339
[email protected]215a7be2012-10-22 19:53:422340 int disable_reasons = extension_prefs_->GetDisableReasons(extension->id());
[email protected]1ff4d282012-12-08 00:39:212341 if (disable_reasons == Extension::DISABLE_NONE) {
2342 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
2343 extension_prefs_->AddDisableReason(
2344 extension->id(),
2345 static_cast<Extension::DisableReason>(
2346 Extension::DISABLE_SIDELOAD_WIPEOUT));
2347 UMA_HISTOGRAM_BOOLEAN("DisabledExtension.ExtensionWipedStatus", true);
[email protected]215a7be2012-10-22 19:53:422348 }
2349}
2350
[email protected]eaa7dd182010-12-14 11:09:002351void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:392352 std::set<std::string> extension_ids;
[email protected]84df8332011-12-06 18:22:462353 for (ExtensionSet::const_iterator iter = extensions_.begin();
2354 iter != extensions_.end(); ++iter) {
2355 const Extension* extension = *iter;
2356 if (!extension->is_theme() && extension->location() != Extension::COMPONENT)
2357 extension_ids.insert(extension->id());
[email protected]aab98a52009-12-02 03:22:352358 }
2359
2360 child_process_logging::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:252361}
2362
[email protected]8266d662011-07-12 21:53:262363void ExtensionService::OnExtensionInstalled(
[email protected]36a5c4c2011-12-14 16:34:502364 const Extension* extension,
[email protected]98270432012-09-11 20:51:242365 const syncer::StringOrdinal& page_ordinal,
[email protected]0db124b02012-11-07 04:55:052366 bool has_requirement_errors,
2367 bool wait_for_idle) {
[email protected]a29a517a2011-01-21 21:11:122368 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:202369
[email protected]c3cfb012011-04-06 22:07:352370 const std::string& id = extension->id();
[email protected]612a1cb12012-10-17 13:18:032371 bool initial_enable = ShouldEnableOnInstall(extension);
[email protected]3f213ad2012-07-26 23:39:412372 const extensions::PendingExtensionInfo* pending_extension_info = NULL;
[email protected]51a3bf8b2012-06-08 22:53:062373 if ((pending_extension_info = pending_extension_manager()->GetById(id))) {
2374 if (!pending_extension_info->ShouldAllowInstall(*extension)) {
2375 pending_extension_manager()->Remove(id);
[email protected]8ef78fd2010-08-19 17:14:322376
[email protected]31bb5ee62012-09-12 22:58:402377 LOG(WARNING) << "ShouldAllowInstall() returned false for "
2378 << id << " of type " << extension->GetType()
2379 << " and update URL " << extension->update_url().spec()
2380 << "; not installing";
[email protected]ae6cb3022011-04-04 20:11:542381
[email protected]ad50def52011-10-19 23:17:072382 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272383 chrome::NOTIFICATION_EXTENSION_INSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:532384 content::Source<Profile>(profile_),
2385 content::Details<const Extension>(extension));
[email protected]ae6cb3022011-04-04 20:11:542386
[email protected]4416c5a2010-06-26 01:28:572387 // Delete the extension directory since we're not going to
2388 // load it.
[email protected]7f8f24f2012-11-15 19:40:142389 if (!GetFileTaskRunner()->PostTask(
2390 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362391 base::Bind(&extension_file_util::DeleteFile,
[email protected]31bb5ee62012-09-12 22:58:402392 extension->path(), true))) {
[email protected]14908b72011-04-20 06:54:362393 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:402394 }
[email protected]4416c5a2010-06-26 01:28:572395 return;
2396 }
[email protected]51a3bf8b2012-06-08 22:53:062397
2398 pending_extension_manager()->Remove(id);
[email protected]4416c5a2010-06-26 01:28:572399 } else {
[email protected]c3cfb012011-04-06 22:07:352400 // We explicitly want to re-enable an uninstalled external
2401 // extension; if we're here, that means the user is manually
2402 // installing the extension.
[email protected]6cc7dbae2011-04-29 21:18:332403 if (IsExternalExtensionUninstalled(id)) {
2404 initial_enable = true;
2405 }
[email protected]aa142702010-03-26 01:26:332406 }
2407
[email protected]98270432012-09-11 20:51:242408 // Unsupported requirements overrides the management policy.
2409 if (has_requirement_errors) {
2410 initial_enable = false;
2411 extension_prefs_->AddDisableReason(
2412 id, Extension::DISABLE_UNSUPPORTED_REQUIREMENT);
2413 // If the extension was disabled because of unsupported requirements but
2414 // now supports all requirements after an update and there are not other
2415 // disable reasons, enable it.
2416 } else if (extension_prefs_->GetDisableReasons(id) ==
2417 Extension::DISABLE_UNSUPPORTED_REQUIREMENT) {
2418 initial_enable = true;
2419 extension_prefs_->ClearDisableReasons(id);
2420 }
2421
[email protected]695b5712012-12-06 23:55:282422 if (!GetInstalledExtension(extension->id())) {
[email protected]07533022011-06-27 20:42:552423 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
2424 extension->GetType(), 100);
[email protected]69084dc2012-11-17 07:39:302425 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallSource",
2426 extension->location(), Extension::NUM_LOCATIONS);
[email protected]07533022011-06-27 20:42:552427 RecordPermissionMessagesHistogram(
2428 extension, "Extensions.Permissions_Install");
[email protected]69084dc2012-11-17 07:39:302429 } else {
2430 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateType",
2431 extension->GetType(), 100);
2432 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateSource",
2433 extension->location(), Extension::NUM_LOCATIONS);
[email protected]07533022011-06-27 20:42:552434 }
2435
[email protected]a39921b42012-02-28 03:42:542436 // Certain extension locations are specific enough that we can
2437 // auto-acknowledge any extension that came from one of them.
[email protected]31bb5ee62012-09-12 22:58:402438 if (extension->location() == Extension::EXTERNAL_POLICY_DOWNLOAD)
[email protected]a39921b42012-02-28 03:42:542439 AcknowledgeExternalExtension(extension->id());
[email protected]399583b2012-12-11 09:33:422440 const Extension::State initial_state =
2441 initial_enable ? Extension::ENABLED : Extension::DISABLED;
[email protected]e7aa7b7e2012-11-27 04:51:222442 if (ShouldDelayExtensionUpdate(id, wait_for_idle)) {
[email protected]399583b2012-12-11 09:33:422443 extension_prefs_->SetDelayedInstallInfo(extension, initial_state,
2444 page_ordinal);
[email protected]0db124b02012-11-07 04:55:052445
2446 // Transfer ownership of |extension|.
[email protected]399583b2012-12-11 09:33:422447 delayed_updates_for_idle_.Insert(extension);
[email protected]0db124b02012-11-07 04:55:052448
2449 // Notify extension of available update.
2450 extensions::RuntimeEventRouter::DispatchOnUpdateAvailableEvent(
2451 profile_, id, extension->manifest()->value());
2452 return;
2453 }
2454
[email protected]399583b2012-12-11 09:33:422455 if (installs_delayed()) {
2456 extension_prefs_->SetDelayedInstallInfo(extension, initial_state,
2457 page_ordinal);
2458 delayed_installs_.Insert(extension);
2459 } else {
2460 AddNewOrUpdatedExtension(extension, initial_state, page_ordinal);
2461 }
[email protected]8c484b742012-11-29 06:05:362462}
2463
2464void ExtensionService::AddNewOrUpdatedExtension(
2465 const Extension* extension,
[email protected]399583b2012-12-11 09:33:422466 Extension::State initial_state,
2467 const syncer::StringOrdinal& page_ordinal) {
[email protected]8c484b742012-11-29 06:05:362468 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2469
[email protected]4416c5a2010-06-26 01:28:572470 extension_prefs_->OnExtensionInstalled(
[email protected]8266d662011-07-12 21:53:262471 extension,
[email protected]8c484b742012-11-29 06:05:362472 initial_state,
[email protected]36a5c4c2011-12-14 16:34:502473 page_ordinal);
[email protected]25b34332009-06-05 21:53:192474
[email protected]6f6101832012-11-27 22:10:482475 FinishInstallation(extension);
2476}
2477
2478void ExtensionService::MaybeFinishDelayedInstallation(
2479 const std::string& extension_id) {
2480 // Check if the extension already got updated.
[email protected]399583b2012-12-11 09:33:422481 if (!delayed_updates_for_idle_.Contains(extension_id))
[email protected]6f6101832012-11-27 22:10:482482 return;
2483 // Check if the extension is idle.
2484 if (!IsExtensionIdle(extension_id))
2485 return;
2486
2487 FinishDelayedInstallation(extension_id);
2488}
2489
2490void ExtensionService::FinishDelayedInstallation(
2491 const std::string& extension_id) {
2492 scoped_refptr<const Extension> extension(
2493 GetPendingExtensionUpdate(extension_id));
2494 CHECK(extension);
[email protected]399583b2012-12-11 09:33:422495 delayed_updates_for_idle_.Remove(extension_id);
[email protected]6f6101832012-11-27 22:10:482496
[email protected]399583b2012-12-11 09:33:422497 if (!extension_prefs_->FinishDelayedInstallInfo(extension_id))
[email protected]6f6101832012-11-27 22:10:482498 NOTREACHED();
2499
2500 FinishInstallation(extension);
2501}
2502
2503void ExtensionService::FinishInstallation(const Extension* extension) {
[email protected]fcb58a862012-05-01 01:03:152504 content::NotificationService::current()->Notify(
2505 chrome::NOTIFICATION_EXTENSION_INSTALLED,
2506 content::Source<Profile>(profile_),
2507 content::Details<const Extension>(extension));
2508
[email protected]d96eb512012-11-01 23:44:082509 bool unacknowledged_external = IsUnacknowledgedExternalExtension(extension);
[email protected]41070e8d2012-10-24 01:34:362510
[email protected]399583b2012-12-11 09:33:422511 // Unpacked extensions default to allowing file access, but if that has been
2512 // overridden, don't reset the value.
2513 if (Extension::ShouldAlwaysAllowFileAccess(extension->location()) &&
2514 !extension_prefs_->HasAllowFileAccessSetting(extension->id())) {
2515 extension_prefs_->SetAllowFileAccess(extension->id(), true);
2516 }
2517
[email protected]6f6101832012-11-27 22:10:482518 AddExtension(extension);
[email protected]612a1cb12012-10-17 13:18:032519
[email protected]a51c9e92012-12-09 09:19:202520#if defined(ENABLE_THEMES)
2521 // We do this here since AddExtension() is always called on browser
2522 // startup, and we only really care about the last theme installed.
2523 // If that ever changes and we have to move this code somewhere
2524 // else, it should be somewhere that's not in the startup path.
2525 if (extension->is_theme() && extensions_.GetByID(extension->id())) {
2526 DCHECK_EQ(extensions_.GetByID(extension->id()), extension);
2527 // Now that the theme extension is visible from outside the
2528 // ExtensionService, notify the ThemeService about the
2529 // newly-installed theme.
2530 ThemeServiceFactory::GetForProfile(profile_)->SetTheme(extension);
2531 }
2532#endif
2533
[email protected]612a1cb12012-10-17 13:18:032534 // If this is a new external extension that was disabled, alert the user
[email protected]d96eb512012-11-01 23:44:082535 // so he can reenable it. We do this last so that it has already been
2536 // added to our list of extensions.
2537 if (unacknowledged_external) {
[email protected]612a1cb12012-10-17 13:18:032538 UpdateExternalExtensionAlert();
[email protected]d96eb512012-11-01 23:44:082539 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
2540 EXTERNAL_EXTENSION_INSTALLED,
2541 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
2542 }
[email protected]4a190632009-05-09 01:07:422543}
2544
[email protected]76b65442012-11-17 14:11:482545const Extension* ExtensionService::GetPendingExtensionUpdate(
2546 const std::string& id) const {
[email protected]399583b2012-12-11 09:33:422547 return delayed_updates_for_idle_.GetByID(id);
[email protected]76b65442012-11-17 14:11:482548}
2549
[email protected]bb7f40952011-01-13 00:21:202550void ExtensionService::TrackTerminatedExtension(const Extension* extension) {
[email protected]84df8332011-12-06 18:22:462551 if (!terminated_extensions_.Contains(extension->id()))
2552 terminated_extensions_.Insert(make_scoped_refptr(extension));
[email protected]fa2416f2011-05-03 08:41:202553
[email protected]b3f7fe22011-11-11 19:27:562554 UnloadExtension(extension->id(), extension_misc::UNLOAD_REASON_TERMINATE);
[email protected]bb7f40952011-01-13 00:21:202555}
2556
2557void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
[email protected]fa2416f2011-05-03 08:41:202558 std::string lowercase_id = StringToLowerASCII(id);
[email protected]84df8332011-12-06 18:22:462559 terminated_extensions_.Remove(lowercase_id);
[email protected]bb7f40952011-01-13 00:21:202560}
2561
[email protected]0dfe05c2011-02-23 23:03:362562const Extension* ExtensionService::GetTerminatedExtension(
[email protected]8001df22011-04-28 19:59:472563 const std::string& id) const {
[email protected]695b5712012-12-06 23:55:282564 return GetExtensionById(id, INCLUDE_TERMINATED);
[email protected]8001df22011-04-28 19:59:472565}
2566
2567const Extension* ExtensionService::GetInstalledExtension(
2568 const std::string& id) const {
[email protected]695b5712012-12-06 23:55:282569 int include_mask = INCLUDE_ENABLED |
2570 INCLUDE_DISABLED |
2571 INCLUDE_TERMINATED |
2572 INCLUDE_BLACKLISTED;
2573 return GetExtensionById(id, include_mask);
[email protected]0dfe05c2011-02-23 23:03:362574}
2575
[email protected]eaa7dd182010-12-14 11:09:002576bool ExtensionService::ExtensionBindingsAllowed(const GURL& url) {
[email protected]615d88f2011-12-13 01:47:442577 // Allow bindings for all packaged extensions and component hosted apps.
2578 const Extension* extension = extensions_.GetExtensionOrAppByURL(
2579 ExtensionURLInfo(url));
2580 return extension && (!extension->is_hosted_app() ||
2581 extension->location() == Extension::COMPONENT);
[email protected]6d2e60bd2010-06-03 22:37:392582}
2583
[email protected]4d007b312012-10-17 03:00:482584bool ExtensionService::ShouldBlockUrlInBrowserTab(GURL* url) {
2585 const Extension* extension = extensions_.GetExtensionOrAppByURL(
2586 ExtensionURLInfo(*url));
2587 if (extension && extension->is_platform_app()) {
2588 *url = GURL(chrome::kExtensionInvalidRequestURL);
2589 return true;
2590 }
2591
2592 return false;
2593}
2594
[email protected]9060d8b02012-01-13 02:14:302595bool ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:322596 const std::string& id,
[email protected]8e4560b62011-01-14 10:09:142597 const Version* version,
[email protected]8ef78fd2010-08-19 17:14:322598 const FilePath& path,
[email protected]1bf73cc32011-10-26 22:38:312599 Extension::Location location,
[email protected]47fc70c2011-12-06 07:29:512600 int creation_flags,
2601 bool mark_acknowledged) {
[email protected]ab22ba42011-01-14 16:36:382602 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2603 CHECK(Extension::IdIsValid(id));
[email protected]79c833b52011-04-05 18:31:012604 if (extension_prefs_->IsExternalExtensionUninstalled(id))
[email protected]9060d8b02012-01-13 02:14:302605 return false;
[email protected]a8af9fdb2010-10-28 21:52:202606
[email protected]7577a5c52009-07-30 06:21:582607 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:492608 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:582609 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:022610 const Extension* existing = GetExtensionById(id, true);
[email protected]910f72ce2012-08-24 01:38:352611
[email protected]7577a5c52009-07-30 06:21:582612 if (existing) {
[email protected]910f72ce2012-08-24 01:38:352613 // The default apps will have the location set as INTERNAL. Since older
2614 // default apps are installed as EXTERNAL, we override them. However, if the
2615 // app is already installed as internal, then do the version check.
[email protected]31bb5ee62012-09-12 22:58:402616 // TODO(grv) : Remove after Q1-2013.
[email protected]910f72ce2012-08-24 01:38:352617 bool is_default_apps_migration =
2618 (location == Extension::INTERNAL &&
2619 Extension::IsExternalLocation(existing->location()));
2620
2621 if (!is_default_apps_migration) {
2622 DCHECK(version);
2623
2624 switch (existing->version()->CompareTo(*version)) {
2625 case -1: // existing version is older, we should upgrade
2626 break;
2627 case 0: // existing version is same, do nothing
2628 return false;
2629 case 1: // existing version is newer, uh-oh
2630 LOG(WARNING) << "Found external version of extension " << id
2631 << "that is older than current version. Current version "
2632 << "is: " << existing->VersionString() << ". New "
2633 << "version is: " << version->GetString()
2634 << ". Keeping current version.";
2635 return false;
2636 }
[email protected]7577a5c52009-07-30 06:21:582637 }
2638 }
2639
[email protected]9060d8b02012-01-13 02:14:302640 // If the extension is already pending, don't start an install.
[email protected]e3987852012-05-04 10:06:302641 if (!pending_extension_manager()->AddFromExternalFile(
2642 id, location, *version)) {
[email protected]9060d8b02012-01-13 02:14:302643 return false;
[email protected]e3987852012-05-04 10:06:302644 }
[email protected]9c635f22010-12-02 09:36:362645
[email protected]14908b72011-04-20 06:54:362646 // no client (silent install)
[email protected]d8c8f25f2011-11-02 18:18:012647 scoped_refptr<CrxInstaller> installer(CrxInstaller::Create(this, NULL));
[email protected]6dfbbf82010-03-12 23:09:162648 installer->set_install_source(location);
2649 installer->set_expected_id(id);
[email protected]cb0e50312011-05-09 15:03:072650 installer->set_expected_version(*version);
2651 installer->set_install_cause(extension_misc::INSTALL_CAUSE_EXTERNAL_FILE);
[email protected]1bf73cc32011-10-26 22:38:312652 installer->set_creation_flags(creation_flags);
[email protected]6dfbbf82010-03-12 23:09:162653 installer->InstallCrx(path);
[email protected]47fc70c2011-12-06 07:29:512654
2655 // Depending on the source, a new external extension might not need a user
2656 // notification on installation. For such extensions, mark them acknowledged
2657 // now to suppress the notification.
2658 if (mark_acknowledged)
2659 AcknowledgeExternalExtension(id);
[email protected]9060d8b02012-01-13 02:14:302660
2661 return true;
[email protected]7577a5c52009-07-30 06:21:582662}
2663
[email protected]eaa7dd182010-12-14 11:09:002664void ExtensionService::ReportExtensionLoadError(
[email protected]d11c8e92009-10-20 23:26:402665 const FilePath& extension_path,
2666 const std::string &error,
[email protected]d11c8e92009-10-20 23:26:402667 bool be_noisy) {
[email protected]d8c8f25f2011-11-02 18:18:012668 content::NotificationService::current()->Notify(
2669 chrome::NOTIFICATION_EXTENSION_LOAD_ERROR,
2670 content::Source<Profile>(profile_),
2671 content::Details<const std::string>(&error));
[email protected]d11c8e92009-10-20 23:26:402672
[email protected]8a205c02011-02-04 20:41:332673 std::string path_str = UTF16ToUTF8(extension_path.LossyDisplayName());
[email protected]438afb12012-07-05 03:18:212674 string16 message = UTF8ToUTF16(base::StringPrintf(
[email protected]18d4b6c2010-09-21 03:21:042675 "Could not load extension from '%s'. %s",
[email protected]fc670822011-12-17 09:33:492676 path_str.c_str(), error.c_str()));
[email protected]d11c8e92009-10-20 23:26:402677 ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy);
2678}
2679
[email protected]eaa7dd182010-12-14 11:09:002680void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]3a1dc572012-07-31 22:25:132681 extensions::ExtensionHost* host) {
[email protected]406027c02010-09-27 08:03:182682 OrphanedDevTools::iterator iter =
[email protected]2d2f6cfc2011-05-06 21:09:332683 orphaned_dev_tools_.find(host->extension_id());
[email protected]406027c02010-09-27 08:03:182684 if (iter == orphaned_dev_tools_.end())
2685 return;
2686
[email protected]0e12d7d2011-12-01 16:21:442687 DevToolsAgentHost* agent = DevToolsAgentHostRegistry::GetDevToolsAgentHost(
2688 host->render_view_host());
2689 content::DevToolsManager::GetInstance()->AttachClientHost(iter->second,
2690 agent);
[email protected]406027c02010-09-27 08:03:182691 orphaned_dev_tools_.erase(iter);
2692}
2693
[email protected]432115822011-07-10 15:52:272694void ExtensionService::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:532695 const content::NotificationSource& source,
2696 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:272697 switch (type) {
[email protected]3c4abc82012-10-22 22:25:542698 case chrome::NOTIFICATION_APP_TERMINATING:
2699 // Shutdown has started. Don't start any more extension installs.
2700 // (We cannot use ExtensionService::Shutdown() for this because it
2701 // happens too late in browser teardown.)
2702 browser_terminating_ = true;
2703 break;
[email protected]432115822011-07-10 15:52:272704 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]6c2381d2011-10-19 02:52:532705 if (profile_ !=
2706 content::Source<Profile>(source).ptr()->GetOriginalProfile()) {
[email protected]bc535ee52010-08-31 18:40:322707 break;
[email protected]6c2381d2011-10-19 02:52:532708 }
[email protected]a4ed6282009-12-14 20:51:162709
[email protected]3a1dc572012-07-31 22:25:132710 extensions::ExtensionHost* host =
2711 content::Details<extensions::ExtensionHost>(details).ptr();
[email protected]f128af42010-08-05 18:05:262712
[email protected]fa2416f2011-05-03 08:41:202713 // Mark the extension as terminated and Unload it. We want it to
2714 // be in a consistent state: either fully working or not loaded
2715 // at all, but never half-crashed. We do it in a PostTask so
2716 // that other handlers of this notification will still have
2717 // access to the Extension and ExtensionHost.
[email protected]14908b72011-04-20 06:54:362718 MessageLoop::current()->PostTask(
2719 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362720 base::Bind(
[email protected]fa2416f2011-05-03 08:41:202721 &ExtensionService::TrackTerminatedExtension,
[email protected]d8c8f25f2011-11-02 18:18:012722 AsWeakPtr(),
[email protected]fa2416f2011-05-03 08:41:202723 host->extension()));
[email protected]31f77262009-12-02 20:48:532724 break;
2725 }
[email protected]432115822011-07-10 15:52:272726 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: {
[email protected]f3b1a082011-11-18 00:34:302727 content::RenderProcessHost* process =
2728 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192729 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302730 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192731 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2732 break;
2733
[email protected]15397b42012-05-16 23:56:062734 // Extensions need to know the channel for API restrictions.
2735 process->Send(new ExtensionMsg_SetChannel(
[email protected]5243df072012-07-11 21:41:012736 extensions::Feature::GetCurrentChannel()));
[email protected]15397b42012-05-16 23:56:062737
[email protected]77a6970c2011-04-23 16:58:562738 // Valid extension function names, used to setup bindings in renderer.
2739 std::vector<std::string> function_names;
2740 ExtensionFunctionDispatcher::GetAllFunctionNames(&function_names);
2741 process->Send(new ExtensionMsg_SetFunctionNames(function_names));
[email protected]31f77262009-12-02 20:48:532742
[email protected]77a6970c2011-04-23 16:58:562743 // Scripting whitelist. This is modified by tests and must be communicated
2744 // to renderers.
2745 process->Send(new ExtensionMsg_SetScriptingWhitelist(
2746 *Extension::GetScriptingWhitelist()));
2747
2748 // Loaded extensions.
[email protected]9776e82e2011-11-15 02:17:532749 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions;
[email protected]84df8332011-12-06 18:22:462750 for (ExtensionSet::const_iterator iter = extensions_.begin();
2751 iter != extensions_.end(); ++iter) {
[email protected]19647262011-12-16 09:57:492752 // Renderers don't need to know about themes.
2753 if (!(*iter)->is_theme())
2754 loaded_extensions.push_back(ExtensionMsg_Loaded_Params(*iter));
[email protected]77a6970c2011-04-23 16:58:562755 }
[email protected]9776e82e2011-11-15 02:17:532756 process->Send(new ExtensionMsg_Loaded(loaded_extensions));
[email protected]77a6970c2011-04-23 16:58:562757 break;
2758 }
[email protected]432115822011-07-10 15:52:272759 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
[email protected]f3b1a082011-11-18 00:34:302760 content::RenderProcessHost* process =
2761 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192762 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302763 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192764 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2765 break;
2766
[email protected]6bc04fd82011-12-04 02:29:352767 process_map_.RemoveAllFromProcess(process->GetID());
[email protected]6f371442011-11-09 06:45:462768 BrowserThread::PostTask(
2769 BrowserThread::IO, FROM_HERE,
2770 base::Bind(&ExtensionInfoMap::UnregisterAllExtensionsInProcess,
[email protected]31d8f5f22012-04-02 15:22:082771 system_->info_map(),
[email protected]f3b1a082011-11-18 00:34:302772 process->GetID()));
[email protected]da5683db2011-04-23 17:12:212773 break;
2774 }
[email protected]d91268022011-08-26 13:17:372775 case chrome::NOTIFICATION_IMPORT_FINISHED: {
[email protected]cf593af2011-12-30 05:44:392776 InitAfterImport();
[email protected]d91268022011-08-26 13:17:372777 break;
2778 }
[email protected]0db124b02012-11-07 04:55:052779 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: {
2780 extensions::ExtensionHost* host =
2781 content::Details<extensions::ExtensionHost>(details).ptr();
[email protected]e7aa7b7e2012-11-27 04:51:222782 std::string extension_id = host->extension_id();
[email protected]399583b2012-12-11 09:33:422783 if (delayed_updates_for_idle_.Contains(extension_id)) {
[email protected]e7aa7b7e2012-11-27 04:51:222784 // We were waiting for this extension to become idle, it now might have,
2785 // so maybe finish installation.
2786 MessageLoop::current()->PostDelayedTask(
[email protected]0db124b02012-11-07 04:55:052787 FROM_HERE,
[email protected]6f6101832012-11-27 22:10:482788 base::Bind(&ExtensionService::MaybeFinishDelayedInstallation,
[email protected]0db124b02012-11-07 04:55:052789 AsWeakPtr(), extension_id),
2790 base::TimeDelta::FromSeconds(kUpdateIdleDelay));
[email protected]0db124b02012-11-07 04:55:052791 }
2792 break;
2793 }
[email protected]aa96d3a2010-08-21 08:45:252794
[email protected]4814b512009-11-07 00:12:292795 default:
2796 NOTREACHED() << "Unexpected notification type.";
2797 }
2798}
2799
[email protected]90bb38d2012-11-14 18:36:032800void ExtensionService::OnExtensionInstallPrefChanged() {
[email protected]a6a7ced2012-11-01 17:24:182801 IdentifyAlertableExtensions();
2802 CheckManagementPolicy();
2803}
2804
[email protected]eaa7dd182010-12-14 11:09:002805bool ExtensionService::HasApps() const {
[email protected]ec5b50d2010-10-09 16:35:182806 return !GetAppIds().empty();
2807}
[email protected]377011d2010-07-20 04:18:502808
[email protected]eaa7dd182010-12-14 11:09:002809ExtensionIdSet ExtensionService::GetAppIds() const {
[email protected]ec5b50d2010-10-09 16:35:182810 ExtensionIdSet result;
[email protected]84df8332011-12-06 18:22:462811 for (ExtensionSet::const_iterator it = extensions_.begin();
[email protected]377011d2010-07-20 04:18:502812 it != extensions_.end(); ++it) {
[email protected]ec5b50d2010-10-09 16:35:182813 if ((*it)->is_app() && (*it)->location() != Extension::COMPONENT)
2814 result.insert((*it)->id());
[email protected]377011d2010-07-20 04:18:502815 }
2816
[email protected]ec5b50d2010-10-09 16:35:182817 return result;
[email protected]377011d2010-07-20 04:18:502818}
[email protected]d7e9a862010-11-03 21:57:492819
[email protected]dc9a74f72012-08-17 18:07:212820bool ExtensionService::IsBackgroundPageReady(const Extension* extension) const {
2821 if (!extension->has_persistent_background_page())
2822 return true;
2823 ExtensionRuntimeDataMap::const_iterator it =
2824 extension_runtime_data_.find(extension->id());
2825 return it == extension_runtime_data_.end() ? false :
2826 it->second.background_page_ready;
[email protected]d7e9a862010-11-03 21:57:492827}
2828
[email protected]eaa7dd182010-12-14 11:09:002829void ExtensionService::SetBackgroundPageReady(const Extension* extension) {
[email protected]a03d4448f2012-01-10 23:25:282830 DCHECK(extension->has_background_page());
[email protected]d7e9a862010-11-03 21:57:492831 extension_runtime_data_[extension->id()].background_page_ready = true;
[email protected]ad50def52011-10-19 23:17:072832 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272833 chrome::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY,
[email protected]6c2381d2011-10-19 02:52:532834 content::Source<const Extension>(extension),
[email protected]ad50def52011-10-19 23:17:072835 content::NotificationService::NoDetails());
[email protected]d7e9a862010-11-03 21:57:492836}
2837
[email protected]65992d52012-09-19 23:05:312838void ExtensionService::InspectBackgroundPage(const Extension* extension) {
2839 DCHECK(extension);
2840
[email protected]fdd679b2012-11-15 20:49:392841 ExtensionProcessManager* pm = system_->process_manager();
[email protected]65992d52012-09-19 23:05:312842 extensions::LazyBackgroundTaskQueue* queue =
[email protected]fdd679b2012-11-15 20:49:392843 system_->lazy_background_task_queue();
[email protected]65992d52012-09-19 23:05:312844
2845 extensions::ExtensionHost* host =
2846 pm->GetBackgroundHostForExtension(extension->id());
2847 if (host) {
2848 InspectExtensionHost(host);
2849 } else {
2850 queue->AddPendingTask(
2851 profile_, extension->id(),
2852 base::Bind(&ExtensionService::InspectExtensionHost,
2853 base::Unretained(this)));
2854 }
2855}
2856
[email protected]dc9a74f72012-08-17 18:07:212857bool ExtensionService::IsBeingUpgraded(const Extension* extension) const {
2858 ExtensionRuntimeDataMap::const_iterator it =
2859 extension_runtime_data_.find(extension->id());
2860 return it == extension_runtime_data_.end() ? false :
2861 it->second.being_upgraded;
[email protected]d7e9a862010-11-03 21:57:492862}
2863
[email protected]eaa7dd182010-12-14 11:09:002864void ExtensionService::SetBeingUpgraded(const Extension* extension,
[email protected]d7e9a862010-11-03 21:57:492865 bool value) {
2866 extension_runtime_data_[extension->id()].being_upgraded = value;
2867}
[email protected]1bead0712010-11-27 17:41:532868
[email protected]dc9a74f72012-08-17 18:07:212869bool ExtensionService::HasUsedWebRequest(const Extension* extension) const {
2870 ExtensionRuntimeDataMap::const_iterator it =
2871 extension_runtime_data_.find(extension->id());
2872 return it == extension_runtime_data_.end() ? false :
2873 it->second.has_used_webrequest;
[email protected]39a5b532011-10-22 01:47:072874}
2875
2876void ExtensionService::SetHasUsedWebRequest(const Extension* extension,
2877 bool value) {
2878 extension_runtime_data_[extension->id()].has_used_webrequest = value;
2879}
2880
[email protected]cebc3dc2011-04-18 17:15:002881void ExtensionService::RegisterNaClModule(const GURL& url,
2882 const std::string& mime_type) {
2883 NaClModuleInfo info;
2884 info.url = url;
2885 info.mime_type = mime_type;
2886
2887 DCHECK(FindNaClModule(url) == nacl_module_list_.end());
2888 nacl_module_list_.push_front(info);
2889}
2890
2891void ExtensionService::UnregisterNaClModule(const GURL& url) {
2892 NaClModuleInfoList::iterator iter = FindNaClModule(url);
2893 DCHECK(iter != nacl_module_list_.end());
2894 nacl_module_list_.erase(iter);
2895}
2896
[email protected]ed0ba002011-05-26 16:55:132897void ExtensionService::UpdatePluginListWithNaClModules() {
[email protected]300c0ea2011-07-15 23:04:332898 // An extension has been added which has a nacl_module component, which means
2899 // there is a MIME type that module wants to handle, so we need to add that
2900 // MIME type to plugins which handle NaCl modules in order to allow the
2901 // individual modules to handle these types.
[email protected]1e127022011-11-29 21:11:472902 FilePath path;
2903 if (!PathService::Get(chrome::FILE_NACL_PLUGIN, &path))
2904 return;
2905 const content::PepperPluginInfo* pepper_info =
2906 PluginService::GetInstance()->GetRegisteredPpapiPluginInfo(path);
2907 if (!pepper_info)
2908 return;
[email protected]300c0ea2011-07-15 23:04:332909
[email protected]1e127022011-11-29 21:11:472910 std::vector<webkit::WebPluginMimeType>::const_iterator mime_iter;
2911 // Check each MIME type the plugins handle for the NaCl MIME type.
2912 for (mime_iter = pepper_info->mime_types.begin();
2913 mime_iter != pepper_info->mime_types.end(); ++mime_iter) {
2914 if (mime_iter->mime_type == kNaClPluginMimeType) {
2915 // This plugin handles "application/x-nacl".
[email protected]300c0ea2011-07-15 23:04:332916
[email protected]1e127022011-11-29 21:11:472917 PluginService::GetInstance()->
2918 UnregisterInternalPlugin(pepper_info->path);
[email protected]300c0ea2011-07-15 23:04:332919
[email protected]1e127022011-11-29 21:11:472920 webkit::WebPluginInfo info = pepper_info->ToWebPluginInfo();
[email protected]300c0ea2011-07-15 23:04:332921
[email protected]1e127022011-11-29 21:11:472922 for (ExtensionService::NaClModuleInfoList::const_iterator iter =
2923 nacl_module_list_.begin();
2924 iter != nacl_module_list_.end(); ++iter) {
2925 // Add the MIME type specified in the extension to this NaCl plugin,
2926 // With an extra "nacl" argument to specify the location of the NaCl
2927 // manifest file.
2928 webkit::WebPluginMimeType mime_type_info;
2929 mime_type_info.mime_type = iter->mime_type;
2930 mime_type_info.additional_param_names.push_back(UTF8ToUTF16("nacl"));
2931 mime_type_info.additional_param_values.push_back(
2932 UTF8ToUTF16(iter->url.spec()));
2933 info.mime_types.push_back(mime_type_info);
[email protected]300c0ea2011-07-15 23:04:332934 }
[email protected]1e127022011-11-29 21:11:472935
2936 PluginService::GetInstance()->RefreshPlugins();
[email protected]c6f3dea2012-01-14 02:23:112937 PluginService::GetInstance()->RegisterInternalPlugin(info, true);
[email protected]1e127022011-11-29 21:11:472938 // This plugin has been modified, no need to check the rest of its
2939 // types, but continue checking other plugins.
2940 break;
[email protected]ed0ba002011-05-26 16:55:132941 }
2942 }
[email protected]ed0ba002011-05-26 16:55:132943}
2944
[email protected]cebc3dc2011-04-18 17:15:002945ExtensionService::NaClModuleInfoList::iterator
2946 ExtensionService::FindNaClModule(const GURL& url) {
2947 for (NaClModuleInfoList::iterator iter = nacl_module_list_.begin();
2948 iter != nacl_module_list_.end(); ++iter) {
2949 if (iter->url == url)
2950 return iter;
2951 }
2952 return nacl_module_list_.end();
2953}
[email protected]6cafe35a2012-08-27 22:40:502954
[email protected]32b36c62012-10-24 05:37:412955void ExtensionService::DoPostLoadTasks(const Extension* extension) {
2956 std::map<std::string, int>::iterator it =
2957 on_load_events_.find(extension->id());
2958 if (it == on_load_events_.end())
2959 return;
2960
2961 int events_to_fire = it->second;
2962 extensions::LazyBackgroundTaskQueue* queue =
2963 system_->lazy_background_task_queue();
2964 if (queue->ShouldEnqueueTask(profile(), extension)) {
2965 if (events_to_fire & EVENT_LAUNCHED)
[email protected]6cafe35a2012-08-27 22:40:502966 queue->AddPendingTask(profile(), extension->id(),
2967 base::Bind(&ExtensionService::LaunchApplication));
[email protected]32b36c62012-10-24 05:37:412968 if (events_to_fire & EVENT_RESTARTED)
2969 queue->AddPendingTask(profile(), extension->id(),
2970 base::Bind(&ExtensionService::RestartApplication));
[email protected]6cafe35a2012-08-27 22:40:502971 }
[email protected]32b36c62012-10-24 05:37:412972
2973 on_load_events_.erase(it);
[email protected]6cafe35a2012-08-27 22:40:502974}
2975
2976// static
2977void ExtensionService::LaunchApplication(
2978 extensions::ExtensionHost* extension_host) {
2979 if (!extension_host)
2980 return;
2981
2982#if !defined(OS_ANDROID)
2983 extensions::LaunchPlatformApp(extension_host->profile(),
2984 extension_host->extension(),
2985 NULL, FilePath());
2986#endif
2987}
[email protected]65992d52012-09-19 23:05:312988
[email protected]32b36c62012-10-24 05:37:412989// static
2990void ExtensionService::RestartApplication(
2991 extensions::ExtensionHost* extension_host) {
2992 if (!extension_host)
2993 return;
2994
2995#if !defined(OS_ANDROID)
2996 extensions::AppEventRouter::DispatchOnRestartedEvent(
2997 extension_host->profile(), extension_host->extension());
2998#endif
2999}
3000
3001bool ExtensionService::HasShellWindows(const std::string& extension_id) {
3002 const Extension* current_extension = GetExtensionById(extension_id, false);
3003 return current_extension && current_extension->is_platform_app() &&
3004 !extensions::ShellWindowRegistry::Get(profile_)->
3005 GetShellWindowsForApp(extension_id).empty();
3006}
3007
[email protected]65992d52012-09-19 23:05:313008void ExtensionService::InspectExtensionHost(
3009 extensions::ExtensionHost* host) {
3010 if (host)
3011 DevToolsWindow::OpenDevToolsWindow(host->render_view_host());
3012}
[email protected]612a1cb12012-10-17 13:18:033013
3014bool ExtensionService::ShouldEnableOnInstall(const Extension* extension) {
3015 // Extensions installed by policy can't be disabled. So even if a previous
3016 // installation disabled the extension, make sure it is now enabled.
3017 if (system_->management_policy()->MustRemainEnabled(extension, NULL))
3018 return true;
3019
3020 if (extension_prefs_->IsExtensionDisabled(extension->id()))
3021 return false;
3022
[email protected]41070e8d2012-10-24 01:34:363023 if (FeatureSwitch::prompt_for_external_extensions()->IsEnabled()) {
[email protected]612a1cb12012-10-17 13:18:033024 // External extensions are initially disabled. We prompt the user before
3025 // enabling them.
3026 if (Extension::IsExternalLocation(extension->location()) &&
3027 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id())) {
3028 return false;
3029 }
3030 }
[email protected]612a1cb12012-10-17 13:18:033031
3032 return true;
3033}
[email protected]0db124b02012-11-07 04:55:053034
3035bool ExtensionService::IsExtensionIdle(const std::string& extension_id) const {
[email protected]d9a61e12012-11-14 02:43:473036 ExtensionProcessManager* process_manager = system_->process_manager();
[email protected]fc332ae2012-11-14 20:17:333037 DCHECK(process_manager);
[email protected]0db124b02012-11-07 04:55:053038 extensions::ExtensionHost* host =
[email protected]d9a61e12012-11-14 02:43:473039 process_manager->GetBackgroundHostForExtension(extension_id);
[email protected]e7aa7b7e2012-11-27 04:51:223040 if (host)
3041 return false;
3042 return process_manager->GetRenderViewHostsForExtension(extension_id).empty();
3043}
3044
3045bool ExtensionService::ShouldDelayExtensionUpdate(
3046 const std::string& extension_id,
3047 bool wait_for_idle) const {
3048 const char kOnUpdateAvailableEvent[] = "runtime.onUpdateAvailable";
3049
3050 // If delayed updates are globally disabled, or just for this extension,
3051 // don't delay.
3052 if (!install_updates_when_idle_ || !wait_for_idle)
3053 return false;
3054
[email protected]695b5712012-12-06 23:55:283055 const Extension* old = GetInstalledExtension(extension_id);
[email protected]e7aa7b7e2012-11-27 04:51:223056 // If there is no old extension, this is not an update, so don't delay.
3057 if (!old)
3058 return false;
3059
3060 if (old->has_persistent_background_page()) {
3061 // Delay installation if the extension listens for the onUpdateAvailable
3062 // event.
3063 return system_->event_router()->ExtensionHasEventListener(
3064 extension_id, kOnUpdateAvailableEvent);
3065 } else {
3066 // Delay installation if the extension is not idle.
3067 return !IsExtensionIdle(extension_id);
3068 }
[email protected]0db124b02012-11-07 04:55:053069}
[email protected]fdd679b2012-11-15 20:49:393070
[email protected]399583b2012-12-11 09:33:423071void ExtensionService::GarbageCollectIsolatedStorage() {
3072 scoped_ptr<base::hash_set<FilePath> > active_paths(
3073 new base::hash_set<FilePath>());
3074 for (ExtensionSet::const_iterator it = extensions_.begin();
3075 it != extensions_.end(); ++it) {
3076 if ((*it)->is_storage_isolated()) {
3077 active_paths->insert(
3078 BrowserContext::GetStoragePartitionForSite(
3079 profile_,
3080 GetSiteForExtensionId((*it)->id()))->GetPath());
3081 }
3082 }
3083
3084 DCHECK(!installs_delayed());
3085 set_installs_delayed(true);
3086 BrowserContext::GarbageCollectStoragePartitions(
3087 profile_, active_paths.Pass(),
3088 base::Bind(&ExtensionService::OnGarbageCollectIsolatedStorageFinished,
3089 AsWeakPtr()));
3090}
3091
3092void ExtensionService::OnGarbageCollectIsolatedStorageFinished() {
3093 set_installs_delayed(false);
3094 for (ExtensionSet::const_iterator it = delayed_installs_.begin();
3095 it != delayed_installs_.end();
3096 ++it) {
3097 FinishDelayedInstallation((*it)->id());
3098 }
3099 for (ExtensionSet::const_iterator it = delayed_updates_for_idle_.begin();
3100 it != delayed_updates_for_idle_.end();
3101 ++it) {
3102 MaybeFinishDelayedInstallation((*it)->id());
3103 }
3104 delayed_installs_.Clear();
3105}
3106
3107void ExtensionService::OnNeedsToGarbageCollectIsolatedStorage() {
3108 extension_prefs_->SetNeedsStorageGarbageCollection(true);
3109}
3110
[email protected]fdd679b2012-11-15 20:49:393111void ExtensionService::OnBlacklistUpdated() {
[email protected]695b5712012-12-06 23:55:283112 blacklist_->GetBlacklistedIDs(
3113 GenerateInstalledExtensionsSet()->GetIDs(),
3114 base::Bind(&ExtensionService::ManageBlacklist,
3115 AsWeakPtr(),
3116 blacklisted_extensions_.GetIDs()));
3117}
3118
3119void ExtensionService::ManageBlacklist(
3120 const std::set<std::string>& old_blacklisted_ids,
3121 const std::set<std::string>& new_blacklisted_ids) {
3122 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
3123
3124 std::set<std::string> no_longer_blacklisted;
3125 std::set_difference(old_blacklisted_ids.begin(), old_blacklisted_ids.end(),
3126 new_blacklisted_ids.begin(), new_blacklisted_ids.end(),
3127 std::inserter(no_longer_blacklisted,
3128 no_longer_blacklisted.begin()));
3129 std::set<std::string> not_yet_blacklisted;
3130 std::set_difference(new_blacklisted_ids.begin(), new_blacklisted_ids.end(),
3131 old_blacklisted_ids.begin(), old_blacklisted_ids.end(),
3132 std::inserter(not_yet_blacklisted,
3133 not_yet_blacklisted.begin()));
3134
3135 for (std::set<std::string>::iterator it = no_longer_blacklisted.begin();
3136 it != no_longer_blacklisted.end(); ++it) {
3137 scoped_refptr<const Extension> extension =
3138 blacklisted_extensions_.GetByID(*it);
3139 DCHECK(extension);
3140 if (!extension)
3141 continue;
3142 blacklisted_extensions_.Remove(*it);
3143 AddExtension(extension);
3144 }
3145
3146 for (std::set<std::string>::iterator it = not_yet_blacklisted.begin();
3147 it != not_yet_blacklisted.end(); ++it) {
3148 scoped_refptr<const Extension> extension = GetInstalledExtension(*it);
3149 DCHECK(extension);
3150 if (!extension)
3151 continue;
3152 blacklisted_extensions_.Insert(extension);
3153 UnloadExtension(*it, extension_misc::UNLOAD_REASON_BLACKLIST);
3154 }
3155
3156 IdentifyAlertableExtensions();
[email protected]fdd679b2012-11-15 20:49:393157}