blob: ba17841c743a37b9bcc5953b625b3b2402eb2918 [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]97d2f1d2011-01-15 00:41:088#include <set>
[email protected]654512b2010-09-01 02:09:429
[email protected]24b538a2010-02-27 01:22:4410#include "base/basictypes.h"
[email protected]05aad2da2011-10-28 10:12:3711#include "base/bind.h"
[email protected]e5af875f2011-10-10 21:09:1412#include "base/callback.h"
[email protected]62433d32011-10-12 22:33:1213#include "base/command_line.h"
[email protected]6014d672008-12-05 00:38:2514#include "base/file_util.h"
[email protected]14908b72011-04-20 06:54:3615#include "base/logging.h"
[email protected]49098f702011-10-13 03:47:1816#include "base/metrics/field_trial.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]34b99632011-01-01 01:01:0623#include "base/threading/thread_restrictions.h"
[email protected]cc2c3432009-11-06 17:24:3624#include "base/time.h"
[email protected]ce7f62e32010-08-10 23:43:5925#include "base/utf_string_conversions.h"
[email protected]cc655912009-01-29 23:19:1926#include "base/values.h"
[email protected]aa142702010-03-26 01:26:3327#include "base/version.h"
[email protected]a692a952011-11-28 08:23:5428#include "chrome/browser/accessibility/accessibility_extension_api.h"
[email protected]23d5f172011-10-25 05:49:5329#include "chrome/browser/bookmarks/bookmark_extension_api.h"
[email protected]15730c42009-09-03 00:03:2030#include "chrome/browser/browser_process.h"
[email protected]dfba8762011-09-02 12:49:5431#include "chrome/browser/chrome_plugin_service_filter.h"
[email protected]92ba7b012012-05-16 03:36:1732#include "chrome/browser/extensions/api/cookies/cookies_api.h"
[email protected]895a1e52012-05-15 02:50:1233#include "chrome/browser/extensions/api/declarative/rules_registry_service.h"
[email protected]9c70e0d92012-07-16 08:39:4434#include "chrome/browser/extensions/api/managed_mode/managed_mode_api.h"
[email protected]895a1e52012-05-15 02:50:1235#include "chrome/browser/extensions/api/web_navigation/web_navigation_api.h"
[email protected]af9db5f2011-10-05 05:13:1536#include "chrome/browser/extensions/app_notification_manager.h"
[email protected]5db9ada2012-04-11 13:48:2037#include "chrome/browser/extensions/app_sync_data.h"
[email protected]18cc5a52011-04-15 16:03:3838#include "chrome/browser/extensions/apps_promo.h"
[email protected]5a38dfd2012-07-23 23:22:1039#include "chrome/browser/extensions/browser_event_router.h"
[email protected]d8c8f25f2011-11-02 18:18:0140#include "chrome/browser/extensions/component_loader.h"
[email protected]e0785902011-05-19 23:34:1741#include "chrome/browser/extensions/crx_installer.h"
[email protected]49098f702011-10-13 03:47:1842#include "chrome/browser/extensions/default_apps_trial.h"
[email protected]c10da4b02010-03-25 14:38:3243#include "chrome/browser/extensions/extension_data_deleter.h"
[email protected]62f051c2012-03-29 17:04:4444#include "chrome/browser/extensions/extension_disabled_ui.h"
[email protected]14a000d2010-04-29 21:44:2445#include "chrome/browser/extensions/extension_error_reporter.h"
[email protected]89226982012-07-16 20:09:1846#include "chrome/browser/extensions/extension_error_ui.h"
[email protected]bdd34452012-04-16 15:58:4047#include "chrome/browser/extensions/extension_font_settings_api.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]7596ce72010-08-30 05:10:4650#include "chrome/browser/extensions/extension_management_api.h"
[email protected]118de7892011-03-16 13:31:4051#include "chrome/browser/extensions/extension_preference_api.h"
[email protected]4814b512009-11-07 00:12:2952#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]8a661f82010-10-19 21:47:1153#include "chrome/browser/extensions/extension_processes_api.h"
[email protected]3569b502012-01-12 20:08:2354#include "chrome/browser/extensions/extension_sorting.h"
[email protected]19eb80152011-02-26 00:28:4355#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]90310d92011-04-17 07:35:0456#include "chrome/browser/extensions/extension_sync_data.h"
[email protected]31d8f5f22012-04-02 15:22:0857#include "chrome/browser/extensions/extension_system.h"
[email protected]8f9d4eb2011-02-05 01:39:1058#include "chrome/browser/extensions/extension_web_ui.h"
[email protected]5df038b2012-07-16 19:03:2759#include "chrome/browser/extensions/external_provider_impl.h"
60#include "chrome/browser/extensions/external_provider_interface.h"
[email protected]d8c8f25f2011-11-02 18:18:0161#include "chrome/browser/extensions/installed_loader.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]cccdf0aa2011-11-11 03:43:3864#include "chrome/browser/extensions/settings/settings_frontend.h"
[email protected]d8c8f25f2011-11-02 18:18:0165#include "chrome/browser/extensions/unpacked_installer.h"
[email protected]42a08162012-03-16 18:09:1166#include "chrome/browser/extensions/updater/extension_updater.h"
[email protected]163ed192012-07-24 19:31:0767#include "chrome/browser/extensions/window_event_router.h"
[email protected]d2e6bd62011-10-24 20:29:0768#include "chrome/browser/history/history_extension_api.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]8e5c89a2011-06-07 18:13:3373#include "chrome/browser/search_engines/template_url_service.h"
74#include "chrome/browser/search_engines/template_url_service_factory.h"
[email protected]18280372011-03-22 18:05:2275#include "chrome/browser/themes/theme_service.h"
[email protected]a0ea76c2011-03-23 17:36:4276#include "chrome/browser/themes/theme_service_factory.h"
[email protected]c8d407e2011-04-28 21:27:1777#include "chrome/browser/ui/webui/chrome_url_data_manager.h"
78#include "chrome/browser/ui/webui/favicon_source.h"
[email protected]5eddc3e2011-10-26 04:33:3179#include "chrome/browser/ui/webui/ntp/thumbnail_source.h"
[email protected]aab98a52009-12-02 03:22:3580#include "chrome/common/child_process_logging.h"
[email protected]432115822011-07-10 15:52:2781#include "chrome/common/chrome_notification_types.h"
[email protected]1e127022011-11-29 21:11:4782#include "chrome/common/chrome_paths.h"
[email protected]e2eb43112009-05-29 21:19:5483#include "chrome/common/chrome_switches.h"
[email protected]15397b42012-05-16 23:56:0684#include "chrome/common/chrome_version_info.h"
[email protected]5b1a0e22009-05-26 19:00:5885#include "chrome/common/extensions/extension.h"
[email protected]05c82182010-06-24 17:49:0886#include "chrome/common/extensions/extension_error_utils.h"
[email protected]7c927b62010-02-24 09:54:1387#include "chrome/common/extensions/extension_file_util.h"
[email protected]895a1e52012-05-15 02:50:1288#include "chrome/common/extensions/extension_manifest_constants.h"
[email protected]77a6970c2011-04-23 16:58:5689#include "chrome/common/extensions/extension_messages.h"
[email protected]a315ba92010-11-16 14:12:2190#include "chrome/common/extensions/extension_resource.h"
[email protected]5243df072012-07-11 21:41:0191#include "chrome/common/extensions/features/feature.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]1e127022011-11-29 21:11:47101#include "content/public/common/pepper_plugin_info.h"
[email protected]c10da4b02010-03-25 14:38:32102#include "googleurl/src/gurl.h"
[email protected]65187152012-06-02 13:14:14103#include "grit/generated_resources.h"
[email protected]be28b5f42012-07-20 11:31:25104#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
[email protected]895a1e52012-05-15 02:50:12105#include "sync/api/sync_change.h"
106#include "sync/api/sync_error_factory.h"
[email protected]65187152012-06-02 13:14:14107#include "ui/base/l10n/l10n_util.h"
[email protected]24b538a2010-02-27 01:22:44108#include "webkit/database/database_tracker.h"
109#include "webkit/database/database_util.h"
[email protected]c64631652009-04-29 22:24:31110
[email protected]eed367e2011-04-12 03:43:31111#if defined(OS_CHROMEOS)
[email protected]14da67b02011-08-02 05:49:13112#include "chrome/browser/chromeos/cros/cros_library.h"
[email protected]f463bc92012-03-22 16:38:23113#include "chrome/browser/chromeos/extensions/bluetooth_event_router.h"
[email protected]eed367e2011-04-12 03:43:31114#include "chrome/browser/chromeos/extensions/file_browser_event_router.h"
[email protected]be5bd5742011-07-14 07:21:01115#include "chrome/browser/chromeos/extensions/input_method_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]b777b332011-04-16 04:01:08119#include "webkit/fileapi/file_system_context.h"
120#include "webkit/fileapi/file_system_mount_point_provider.h"
[email protected]eed367e2011-04-12 03:43:31121#endif
122
[email protected]5ef47ec2010-01-28 05:58:05123using base::Time;
[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;
133using extensions::UnloadedExtensionInfo;
[email protected]c2e66e12012-06-27 06:27:06134using extensions::PermissionMessage;
135using extensions::PermissionMessages;
136using extensions::PermissionSet;
[email protected]5ef47ec2010-01-28 05:58:05137
[email protected]c6d474f82009-12-16 21:11:06138namespace errors = extension_manifest_errors;
139
[email protected]b6ab96d2009-08-20 18:58:19140namespace {
141
[email protected]29d0d4ac2010-09-08 21:10:31142#if defined(OS_LINUX)
143static const int kOmniboxIconPaddingLeft = 2;
144static const int kOmniboxIconPaddingRight = 2;
145#elif defined(OS_MACOSX)
146static const int kOmniboxIconPaddingLeft = 0;
147static const int kOmniboxIconPaddingRight = 2;
148#else
149static const int kOmniboxIconPaddingLeft = 0;
150static const int kOmniboxIconPaddingRight = 0;
151#endif
152
[email protected]300c0ea2011-07-15 23:04:33153const char* kNaClPluginMimeType = "application/x-nacl";
154
[email protected]3bdba0d2011-08-23 07:17:30155static bool IsSyncableExtension(const Extension& extension) {
156 return extension.GetSyncType() == Extension::SYNC_TYPE_EXTENSION;
157}
158
159static bool IsSyncableApp(const Extension& extension) {
160 return extension.GetSyncType() == Extension::SYNC_TYPE_APP;
161}
162
[email protected]c6d474f82009-12-16 21:11:06163} // namespace
[email protected]b6ab96d2009-08-20 18:58:19164
[email protected]eaa7dd182010-12-14 11:09:00165ExtensionService::ExtensionRuntimeData::ExtensionRuntimeData()
[email protected]d7e9a862010-11-03 21:57:49166 : background_page_ready(false),
[email protected]f8bf5992011-10-24 22:38:57167 being_upgraded(false),
168 has_used_webrequest(false) {
[email protected]d7e9a862010-11-03 21:57:49169}
170
[email protected]eaa7dd182010-12-14 11:09:00171ExtensionService::ExtensionRuntimeData::~ExtensionRuntimeData() {
[email protected]d7e9a862010-11-03 21:57:49172}
173
[email protected]cebc3dc2011-04-18 17:15:00174ExtensionService::NaClModuleInfo::NaClModuleInfo() {
175}
176
177ExtensionService::NaClModuleInfo::~NaClModuleInfo() {
178}
179
[email protected]eaa7dd182010-12-14 11:09:00180// ExtensionService.
[email protected]6014d672008-12-05 00:38:25181
[email protected]eaa7dd182010-12-14 11:09:00182const char* ExtensionService::kInstallDirectoryName = "Extensions";
[email protected]15d5b472011-11-24 04:17:24183
184const char* ExtensionService::kLocalAppSettingsDirectoryName =
185 "Local App Settings";
186const char* ExtensionService::kLocalExtensionSettingsDirectoryName =
187 "Local Extension Settings";
188const char* ExtensionService::kSyncAppSettingsDirectoryName =
189 "Sync App Settings";
190const char* ExtensionService::kSyncExtensionSettingsDirectoryName =
191 "Sync Extension Settings";
[email protected]90e800c2012-06-12 23:11:00192const char* ExtensionService::kStateStoreName = "Extension State";
[email protected]494c06e2009-07-25 01:06:42193
[email protected]8e4560b62011-01-14 10:09:14194void ExtensionService::CheckExternalUninstall(const std::string& id) {
[email protected]a29a517a2011-01-21 21:11:12195 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20196
[email protected]7a4c6852010-09-16 03:44:22197 // Check if the providers know about this extension.
[email protected]5df038b2012-07-16 19:03:27198 extensions::ProviderCollection::const_iterator i;
[email protected]0a60a2e2010-10-25 16:15:21199 for (i = external_extension_providers_.begin();
200 i != external_extension_providers_.end(); ++i) {
[email protected]8e4560b62011-01-14 10:09:14201 DCHECK(i->get()->IsReady());
[email protected]0a60a2e2010-10-25 16:15:21202 if (i->get()->HasExtension(id))
203 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22204 }
205
[email protected]0f48fca2011-05-19 18:46:35206 // We get the list of external extensions to check from preferences.
207 // It is possible that an extension has preferences but is not loaded.
208 // For example, an extension that requires experimental permissions
209 // will not be loaded if the experimental command line flag is not used.
210 // In this case, do not uninstall.
211 const Extension* extension = GetInstalledExtension(id);
212 if (!extension) {
213 // We can't call UninstallExtension with an unloaded/invalid
214 // extension ID.
215 LOG(WARNING) << "Attempted uninstallation of unloaded/invalid extension "
216 << "with id: " << id;
217 return;
218 }
[email protected]d6ebc9792011-04-07 18:18:33219 UninstallExtension(id, true, NULL);
[email protected]7a4c6852010-09-16 03:44:22220}
221
[email protected]8e4560b62011-01-14 10:09:14222void ExtensionService::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22223 external_extension_providers_.clear();
224}
225
[email protected]8e4560b62011-01-14 10:09:14226void ExtensionService::AddProviderForTesting(
[email protected]5df038b2012-07-16 19:03:27227 extensions::ExternalProviderInterface* test_provider) {
[email protected]a29a517a2011-01-21 21:11:12228 CHECK(test_provider);
[email protected]0a60a2e2010-10-25 16:15:21229 external_extension_providers_.push_back(
[email protected]5df038b2012-07-16 19:03:27230 linked_ptr<extensions::ExternalProviderInterface>(test_provider));
[email protected]7a4c6852010-09-16 03:44:22231}
232
[email protected]9060d8b02012-01-13 02:14:30233bool ExtensionService::OnExternalExtensionUpdateUrlFound(
[email protected]7a4c6852010-09-16 03:44:22234 const std::string& id,
[email protected]21a5a672010-11-04 10:47:42235 const GURL& update_url,
236 Extension::Location location) {
[email protected]ab22ba42011-01-14 16:36:38237 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
238 CHECK(Extension::IdIsValid(id));
[email protected]a8af9fdb2010-10-28 21:52:20239
[email protected]8a87a5332011-08-11 17:54:59240 const Extension* extension = GetExtensionById(id, true);
241 if (extension) {
242 // Already installed. Skip this install if the current location has
243 // higher priority than |location|.
244 Extension::Location current = extension->location();
245 if (current == Extension::GetHigherPriorityLocation(current, location))
[email protected]9060d8b02012-01-13 02:14:30246 return false;
[email protected]8a87a5332011-08-11 17:54:59247 // Otherwise, overwrite the current installation.
[email protected]7a4c6852010-09-16 03:44:22248 }
[email protected]9060d8b02012-01-13 02:14:30249
250 // Add |id| to the set of pending extensions. If it can not be added,
251 // then there is already a pending record from a higher-priority install
252 // source. In this case, signal that this extension will not be
253 // installed by returning false.
254 if (!pending_extension_manager()->AddFromExternalUpdateUrl(
255 id, update_url, location))
256 return false;
257
[email protected]94fde232012-04-27 10:22:30258 update_once_all_providers_are_ready_ = true;
[email protected]9060d8b02012-01-13 02:14:30259 return true;
[email protected]7a4c6852010-09-16 03:44:22260}
261
[email protected]d9696672011-03-15 22:45:09262const Extension* ExtensionService::GetInstalledApp(const GURL& url) {
[email protected]615d88f2011-12-13 01:47:44263 const Extension* extension = extensions_.GetExtensionOrAppByURL(
264 ExtensionURLInfo(url));
265 if (extension && extension->is_app())
266 return extension;
[email protected]d9696672011-03-15 22:45:09267
268 return NULL;
269}
270
271bool ExtensionService::IsInstalledApp(const GURL& url) {
272 return !!GetInstalledApp(url);
[email protected]ffb204f22010-12-05 23:20:27273}
274
[email protected]da5683db2011-04-23 17:12:21275void ExtensionService::SetInstalledAppForRenderer(int renderer_child_id,
276 const Extension* app) {
277 installed_app_hosts_[renderer_child_id] = app;
278}
279
280const Extension* ExtensionService::GetInstalledAppForRenderer(
281 int renderer_child_id) {
282 InstalledAppMap::iterator i = installed_app_hosts_.find(renderer_child_id);
283 if (i == installed_app_hosts_.end())
284 return NULL;
285 return i->second;
286}
287
[email protected]6aeac8342010-10-01 20:21:18288// static
[email protected]d6ebc9792011-04-07 18:18:33289// This function is used to implement the command-line switch
[email protected]3bdba0d2011-08-23 07:17:30290// --uninstall-extension, and to uninstall an extension via sync. The LOG
291// statements within this function are used to inform the user if the uninstall
292// cannot be done.
[email protected]eaa7dd182010-12-14 11:09:00293bool ExtensionService::UninstallExtensionHelper(
294 ExtensionService* extensions_service,
[email protected]6aeac8342010-10-01 20:21:18295 const std::string& extension_id) {
[email protected]6aeac8342010-10-01 20:21:18296
[email protected]95da88c42011-03-31 10:07:33297 const Extension* extension =
[email protected]8001df22011-04-28 19:59:47298 extensions_service->GetInstalledExtension(extension_id);
[email protected]95da88c42011-03-31 10:07:33299
[email protected]d6ebc9792011-04-07 18:18:33300 // We can't call UninstallExtension with an invalid extension ID.
[email protected]95da88c42011-03-31 10:07:33301 if (!extension) {
[email protected]6aeac8342010-10-01 20:21:18302 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]95da88c42011-03-31 10:07:33303 << "id: " << extension_id;
304 return false;
[email protected]6aeac8342010-10-01 20:21:18305 }
306
[email protected]d6ebc9792011-04-07 18:18:33307 // The following call to UninstallExtension will not allow an uninstall of a
308 // policy-controlled extension.
[email protected]65187152012-06-02 13:14:14309 string16 error;
[email protected]d6ebc9792011-04-07 18:18:33310 if (!extensions_service->UninstallExtension(extension_id, false, &error)) {
311 LOG(WARNING) << "Cannot uninstall extension with id " << extension_id
312 << ": " << error;
313 return false;
314 }
[email protected]95da88c42011-03-31 10:07:33315
[email protected]6aeac8342010-10-01 20:21:18316 return true;
317}
318
[email protected]eaa7dd182010-12-14 11:09:00319ExtensionService::ExtensionService(Profile* profile,
[email protected]f0841cd2011-01-19 15:07:24320 const CommandLine* command_line,
321 const FilePath& install_directory,
[email protected]45759612012-07-10 17:21:23322 extensions::ExtensionPrefs* extension_prefs,
[email protected]0436b102011-04-15 18:30:03323 bool autoupdate_enabled,
324 bool extensions_enabled)
[email protected]d8c8f25f2011-11-02 18:18:01325 : profile_(profile),
[email protected]bd306722012-07-11 20:43:59326 system_(extensions::ExtensionSystem::Get(profile)),
[email protected]73c47932010-12-06 18:13:43327 extension_prefs_(extension_prefs),
[email protected]cccdf0aa2011-11-11 03:43:38328 settings_frontend_(extensions::SettingsFrontend::Create(profile)),
[email protected]14908b72011-04-20 06:54:36329 pending_extension_manager_(*ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]a9b00ac2009-06-25 21:03:23330 install_directory_(install_directory),
[email protected]0436b102011-04-15 18:30:03331 extensions_enabled_(extensions_enabled),
[email protected]e81dba32009-06-19 20:19:13332 show_extensions_prompts_(true),
[email protected]3ecda252010-11-18 19:50:55333 ready_(false),
[email protected]14908b72011-04-20 06:54:36334 toolbar_model_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]8b280302011-10-13 22:22:23335 menu_manager_(profile),
[email protected]af9db5f2011-10-05 05:13:15336 app_notification_manager_(new AppNotificationManager(profile)),
[email protected]18cc5a52011-04-15 16:03:38337 apps_promo_(profile->GetPrefs()),
[email protected]7c1490da2011-10-11 18:53:25338 event_routers_initialized_(false),
[email protected]dff1e042012-04-27 10:59:18339 update_once_all_providers_are_ready_(false),
[email protected]5db9ada2012-04-11 13:48:20340 app_sync_bundle_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
341 extension_sync_bundle_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]a1755bd2011-12-09 00:40:20342 extension_warnings_(profile),
[email protected]178f8512012-02-09 01:49:36343 app_shortcut_manager_(profile) {
[email protected]a29a517a2011-01-21 21:11:12344 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20345
[email protected]36a784c2009-06-23 06:21:08346 // Figure out if extension installation should be enabled.
[email protected]6d60703b2009-08-29 01:29:23347 if (command_line->HasSwitch(switches::kDisableExtensions)) {
348 extensions_enabled_ = false;
349 } else if (profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions)) {
350 extensions_enabled_ = false;
[email protected]6b75ec32009-08-14 06:37:18351 }
[email protected]36a784c2009-06-23 06:21:08352
[email protected]432115822011-07-10 15:52:27353 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07354 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27355 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED,
[email protected]ad50def52011-10-19 23:17:07356 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27357 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07358 content::NotificationService::AllBrowserContextsAndSources());
[email protected]2fb7dc982010-09-29 12:24:28359 pref_change_registrar_.Init(profile->GetPrefs());
360 pref_change_registrar_.Add(prefs::kExtensionInstallAllowList, this);
361 pref_change_registrar_.Add(prefs::kExtensionInstallDenyList, this);
[email protected]4814b512009-11-07 00:12:29362
[email protected]93fd78f42009-07-10 16:43:17363 // Set up the ExtensionUpdater
364 if (autoupdate_enabled) {
365 int update_frequency = kDefaultUpdateFrequencySeconds;
366 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25367 base::StringToInt(command_line->GetSwitchValueASCII(
368 switches::kExtensionsUpdateFrequency),
369 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17370 }
[email protected]42a08162012-03-16 18:09:11371 updater_.reset(new extensions::ExtensionUpdater(this,
372 extension_prefs,
373 profile->GetPrefs(),
374 profile,
375 update_frequency));
[email protected]93fd78f42009-07-10 16:43:17376 }
377
[email protected]25ae0152011-11-18 14:40:02378 component_loader_.reset(
379 new extensions::ComponentLoader(this,
380 profile->GetPrefs(),
381 g_browser_process->local_state()));
[email protected]8e4560b62011-01-14 10:09:14382
[email protected]af9db5f2011-10-05 05:13:15383 app_notification_manager_->Init();
384
[email protected]0436b102011-04-15 18:30:03385 if (extensions_enabled_) {
[email protected]1f2f3002011-12-28 00:40:21386 if (!command_line->HasSwitch(switches::kImport) &&
387 !command_line->HasSwitch(switches::kImportFromFile)) {
[email protected]5df038b2012-07-16 19:03:27388 extensions::ExternalProviderImpl::CreateExternalProviders(
[email protected]1f2f3002011-12-28 00:40:21389 this, profile_, &external_extension_providers_);
390 }
[email protected]873531342011-03-09 12:16:05391 }
[email protected]b671760b2010-07-15 21:13:47392
[email protected]aa96d3a2010-08-21 08:45:25393 // Use monochrome icons for Omnibox icons.
[email protected]29d0d4ac2010-09-08 21:10:31394 omnibox_popup_icon_manager_.set_monochrome(true);
[email protected]b671760b2010-07-15 21:13:47395 omnibox_icon_manager_.set_monochrome(true);
[email protected]29d0d4ac2010-09-08 21:10:31396 omnibox_icon_manager_.set_padding(gfx::Insets(0, kOmniboxIconPaddingLeft,
397 0, kOmniboxIconPaddingRight));
[email protected]cb0e50312011-05-09 15:03:07398
[email protected]fb82dcd2012-03-21 14:15:46399 // Set this as the ExtensionService for extension sorting to ensure it
400 // cause syncs if required.
401 extension_prefs_->extension_sorting()->SetExtensionService(this);
402
[email protected]cb0e50312011-05-09 15:03:07403 // How long is the path to the Extensions directory?
404 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ExtensionRootPathLength",
405 install_directory_.value().length(), 0, 500, 100);
[email protected]6014d672008-12-05 00:38:25406}
407
[email protected]84df8332011-12-06 18:22:46408const ExtensionSet* ExtensionService::extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45409 return &extensions_;
410}
411
[email protected]84df8332011-12-06 18:22:46412const ExtensionSet* ExtensionService::disabled_extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45413 return &disabled_extensions_;
414}
415
[email protected]84df8332011-12-06 18:22:46416const ExtensionSet* ExtensionService::terminated_extensions() const {
[email protected]bb7f40952011-01-13 00:21:20417 return &terminated_extensions_;
418}
419
[email protected]7f4308d2012-01-18 07:43:01420const ExtensionSet* ExtensionService::GenerateInstalledExtensionsSet() const {
421 ExtensionSet* installed_extensions = new ExtensionSet();
422 installed_extensions->InsertAll(extensions_);
423 installed_extensions->InsertAll(disabled_extensions_);
424 installed_extensions->InsertAll(terminated_extensions_);
425 return installed_extensions;
426}
427
[email protected]b2907fd2011-03-25 16:43:37428PendingExtensionManager* ExtensionService::pending_extension_manager() {
429 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45430}
431
[email protected]eaa7dd182010-12-14 11:09:00432ExtensionService::~ExtensionService() {
[email protected]c8d407e2011-04-28 21:27:17433 // No need to unload extensions here because they are profile-scoped, and the
434 // profile is in the process of being deleted.
[email protected]94b4ffc2011-02-04 05:55:17435
[email protected]5df038b2012-07-16 19:03:27436 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:14437 for (i = external_extension_providers_.begin();
438 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:27439 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:14440 provider->ServiceShutdown();
441 }
[email protected]6014d672008-12-05 00:38:25442}
443
[email protected]d91268022011-08-26 13:17:37444void ExtensionService::InitEventRoutersAfterImport() {
[email protected]cf593af2011-12-30 05:44:39445 RegisterForImportFinished();
446}
447
448void ExtensionService::RegisterForImportFinished() {
449 if (!registrar_.IsRegistered(this, chrome::NOTIFICATION_IMPORT_FINISHED,
450 content::Source<Profile>(profile_))) {
451 registrar_.Add(this, chrome::NOTIFICATION_IMPORT_FINISHED,
452 content::Source<Profile>(profile_));
453 }
454}
455
456void ExtensionService::InitAfterImport() {
457 CheckForExternalUpdates();
458
459 GarbageCollectExtensions();
460
461 // Idempotent, so although there is a possible race if the import
462 // process finished sometime in the middle of ProfileImpl::InitExtensions,
463 // it cannot happen twice.
464 InitEventRouters();
[email protected]d91268022011-08-26 13:17:37465}
466
[email protected]eaa7dd182010-12-14 11:09:00467void ExtensionService::InitEventRouters() {
[email protected]b3d62312b12010-10-14 21:10:18468 if (event_routers_initialized_)
469 return;
470
[email protected]6a3cd37e2012-04-17 17:13:34471#if defined(ENABLE_EXTENSIONS)
[email protected]d2e6bd62011-10-24 20:29:07472 history_event_router_.reset(new HistoryExtensionEventRouter());
[email protected]f825df22011-06-28 17:36:10473 history_event_router_->ObserveProfile(profile_);
[email protected]5a38dfd2012-07-23 23:22:10474 browser_event_router_.reset(new extensions::BrowserEventRouter(profile_));
[email protected]34517242012-05-25 02:37:18475 browser_event_router_->Init();
[email protected]163ed192012-07-24 19:31:07476 window_event_router_.reset(new extensions::WindowEventRouter(profile_));
477 window_event_router_->Init();
[email protected]118de7892011-03-16 13:31:40478 preference_event_router_.reset(new ExtensionPreferenceEventRouter(profile_));
[email protected]23d5f172011-10-25 05:49:53479 bookmark_event_router_.reset(new BookmarkExtensionEventRouter(
[email protected]f825df22011-06-28 17:36:10480 profile_->GetBookmarkModel()));
481 bookmark_event_router_->Init();
[email protected]92ba7b012012-05-16 03:36:17482 cookies_event_router_.reset(new
483 extensions::ExtensionCookiesEventRouter(profile_));
[email protected]f825df22011-06-28 17:36:10484 cookies_event_router_->Init();
485 management_event_router_.reset(new ExtensionManagementEventRouter(profile_));
486 management_event_router_->Init();
[email protected]7da9a8232011-05-27 21:07:11487 ExtensionProcessesEventRouter::GetInstance()->ObserveProfile(profile_);
[email protected]f825df22011-06-28 17:36:10488 web_navigation_event_router_.reset(
[email protected]017ecde92012-04-04 09:11:26489 new extensions::WebNavigationEventRouter(profile_));
[email protected]f825df22011-06-28 17:36:10490 web_navigation_event_router_->Init();
[email protected]bdd34452012-04-16 15:58:40491 font_settings_event_router_.reset(
492 new ExtensionFontSettingsEventRouter(profile_));
493 font_settings_event_router_->Init();
[email protected]8a4cf842012-05-16 13:35:30494 managed_mode_event_router_.reset(
[email protected]9c70e0d92012-07-16 08:39:44495 new extensions::ExtensionManagedModeEventRouter(profile_));
[email protected]8a4cf842012-05-16 13:35:30496 managed_mode_event_router_->Init();
[email protected]27072cad2011-05-09 19:46:40497
[email protected]eed367e2011-04-12 03:43:31498#if defined(OS_CHROMEOS)
[email protected]2a72c9f2012-03-17 15:10:30499 FileBrowserEventRouterFactory::GetForProfile(
500 profile_)->ObserveFileSystemEvents();
[email protected]771a6182011-10-05 16:32:52501
[email protected]f463bc92012-03-22 16:38:23502 bluetooth_event_router_.reset(
503 new chromeos::ExtensionBluetoothEventRouter(profile_));
504
[email protected]771a6182011-10-05 16:32:52505 input_method_event_router_.reset(
506 new chromeos::ExtensionInputMethodEventRouter);
[email protected]be5bd5742011-07-14 07:21:01507
[email protected]c8bf09be2011-06-25 03:22:27508 ExtensionMediaPlayerEventRouter::GetInstance()->Init(profile_);
[email protected]1364c6d2012-07-24 11:57:19509 extensions::InputImeEventRouter::GetInstance()->Init();
[email protected]eed367e2011-04-12 03:43:31510#endif
[email protected]6a3cd37e2012-04-17 17:13:34511#endif // defined(ENABLE_EXTENSIONS)
[email protected]b3d62312b12010-10-14 21:10:18512 event_routers_initialized_ = true;
[email protected]c5ae74ab2010-04-15 18:14:37513}
514
[email protected]b2907fd2011-03-25 16:43:37515const Extension* ExtensionService::GetExtensionById(
516 const std::string& id, bool include_disabled) const {
[email protected]8001df22011-04-28 19:59:47517 return GetExtensionByIdInternal(id, true, include_disabled, false);
[email protected]78994ab02010-12-08 18:06:44518}
519
[email protected]eaa7dd182010-12-14 11:09:00520void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12521 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20522
[email protected]fa6a9102010-11-22 15:38:50523 DCHECK(!ready_); // Can't redo init.
[email protected]93fd78f42009-07-10 16:43:17524 DCHECK_EQ(extensions_.size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32525
[email protected]d8c8f25f2011-11-02 18:18:01526 component_loader_->LoadAll();
527 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]894bb502009-05-21 22:39:57528
[email protected]7a161db2011-12-15 20:45:51529 // If we are running in the import process, don't bother initializing the
530 // extension service since this can interfere with the main browser process
531 // that is already running an extension service for this profile.
532 // TODO(aa): can we start up even less of ExtensionService?
533 // http://crbug.com/107636
[email protected]1f2f3002011-12-28 00:40:21534 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kImport) &&
535 !CommandLine::ForCurrentProcess()->HasSwitch(switches::kImportFromFile)) {
[email protected]cf593af2011-12-30 05:44:39536 if (g_browser_process->profile_manager() &&
537 g_browser_process->profile_manager()->will_import()) {
538 RegisterForImportFinished();
539 } else {
540 // TODO(erikkay) this should probably be deferred to a future point
541 // rather than running immediately at startup.
542 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57543
[email protected]cf593af2011-12-30 05:44:39544 // TODO(erikkay) this should probably be deferred as well.
545 GarbageCollectExtensions();
546 }
[email protected]7a161db2011-12-15 20:45:51547 }
[email protected]6014d672008-12-05 00:38:25548}
549
[email protected]420a0ec2011-06-01 01:07:03550bool ExtensionService::UpdateExtension(
551 const std::string& id,
552 const FilePath& extension_path,
553 const GURL& download_url,
554 CrxInstaller** out_crx_installer) {
[email protected]a29a517a2011-01-21 21:11:12555 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20556
[email protected]51a3bf8b2012-06-08 22:53:06557 const PendingExtensionInfo* pending_extension_info =
558 pending_extension_manager()->GetById(id);
[email protected]8ef78fd2010-08-19 17:14:32559
[email protected]8001df22011-04-28 19:59:47560 const Extension* extension =
561 GetExtensionByIdInternal(id, true, true, false);
[email protected]51a3bf8b2012-06-08 22:53:06562 if (!pending_extension_info && !extension) {
[email protected]aa142702010-03-26 01:26:33563 LOG(WARNING) << "Will not update extension " << id
564 << " because it is not installed or pending";
565 // Delete extension_path since we're not creating a CrxInstaller
566 // that would do it for us.
[email protected]14908b72011-04-20 06:54:36567 if (!BrowserThread::PostTask(
568 BrowserThread::FILE, FROM_HERE,
[email protected]53612e82011-10-18 18:00:36569 base::Bind(
[email protected]7296f2762011-11-21 19:23:44570 &extension_file_util::DeleteFile, extension_path, false)))
[email protected]14908b72011-04-20 06:54:36571 NOTREACHED();
[email protected]420a0ec2011-06-01 01:07:03572
573 return false;
[email protected]e957fe52009-06-23 16:51:05574 }
575
[email protected]aa142702010-03-26 01:26:33576 // We want a silent install only for non-pending extensions and
577 // pending extensions that have install_silently set.
[email protected]c82da8c42012-06-08 19:49:11578 ExtensionInstallPrompt* client =
[email protected]51a3bf8b2012-06-08 22:53:06579 (!pending_extension_info || pending_extension_info->install_silently()) ?
[email protected]00b38242012-07-18 18:43:22580 NULL : ExtensionInstallUI::CreateInstallPromptWithProfile(profile_);
[email protected]aa142702010-03-26 01:26:33581
[email protected]d8c8f25f2011-11-02 18:18:01582 scoped_refptr<CrxInstaller> installer(CrxInstaller::Create(this, client));
[email protected]6dfbbf82010-03-12 23:09:16583 installer->set_expected_id(id);
[email protected]51a3bf8b2012-06-08 22:53:06584 if (pending_extension_info) {
585 installer->set_install_source(pending_extension_info->install_source());
586 if (pending_extension_info->install_silently())
587 installer->set_allow_silent_install(true);
588 } else if (extension) {
[email protected]5eb375e92010-11-26 07:50:41589 installer->set_install_source(extension->location());
[email protected]51a3bf8b2012-06-08 22:53:06590 }
[email protected]7d8b7072012-04-07 01:07:46591 // If the extension was installed from or has migrated to the webstore, or
592 // if the extension came from sync and its auto-update URL is from the
[email protected]75764512011-12-19 19:54:28593 // webstore, treat it as a webstore install. Note that we ignore some older
594 // extensions with blank auto-update URLs because we are mostly concerned
595 // with restrictions on NaCl extensions, which are newer.
[email protected]a12ce8b22012-01-17 18:40:53596 int creation_flags = Extension::NO_FLAGS;
[email protected]75764512011-12-19 19:54:28597 if ((extension && extension->from_webstore()) ||
[email protected]7d8b7072012-04-07 01:07:46598 (extension && extension->UpdatesFromGallery()) ||
[email protected]51a3bf8b2012-06-08 22:53:06599 (!extension && pending_extension_info->is_from_sync() &&
[email protected]75764512011-12-19 19:54:28600 extension_urls::IsWebstoreUpdateUrl(
[email protected]51a3bf8b2012-06-08 22:53:06601 pending_extension_info->update_url()))) {
[email protected]a12ce8b22012-01-17 18:40:53602 creation_flags |= Extension::FROM_WEBSTORE;
603 }
604
605 // Bookmark apps being updated is kind of a contradiction, but that's because
606 // we mark the default apps as bookmark apps, and they're hosted in the web
607 // store, thus they can get updated. See http://crbug.com/101605 for more
608 // details.
609 if (extension && extension->from_bookmark())
610 creation_flags |= Extension::FROM_BOOKMARK;
611
[email protected]a12ce8b22012-01-17 18:40:53612 installer->set_creation_flags(creation_flags);
613
[email protected]6dfbbf82010-03-12 23:09:16614 installer->set_delete_source(true);
[email protected]655b2b1a2011-10-13 17:13:06615 installer->set_download_url(download_url);
[email protected]cb0e50312011-05-09 15:03:07616 installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
[email protected]6dfbbf82010-03-12 23:09:16617 installer->InstallCrx(extension_path);
[email protected]420a0ec2011-06-01 01:07:03618
619 if (out_crx_installer)
620 *out_crx_installer = installer;
621
622 return true;
[email protected]e957fe52009-06-23 16:51:05623}
624
[email protected]eaa7dd182010-12-14 11:09:00625void ExtensionService::ReloadExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12626 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]b65272f2009-08-31 15:47:06627 FilePath path;
[email protected]9adb9692010-10-29 23:14:02628 const Extension* current_extension = GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40629
[email protected]f17dbd42010-08-16 23:21:10630 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]b65272f2009-08-31 15:47:06631 if (current_extension) {
[email protected]4814b512009-11-07 00:12:29632 // If the extension has an inspector open for its background page, detach
633 // the inspector and hang onto a cookie for it, so that we can reattach
634 // later.
[email protected]31d8f5f22012-04-02 15:22:08635 // TODO(yoz): this is not incognito-safe!
636 ExtensionProcessManager* manager = system_->process_manager();
[email protected]06024c62011-10-20 20:57:12637 ExtensionHost* host = manager->GetBackgroundHostForExtension(extension_id);
[email protected]0e12d7d2011-12-01 16:21:44638 if (host && DevToolsAgentHostRegistry::HasDevToolsAgentHost(
639 host->render_view_host())) {
[email protected]4814b512009-11-07 00:12:29640 // Look for an open inspector for the background page.
[email protected]0e12d7d2011-12-01 16:21:44641 DevToolsAgentHost* agent =
642 DevToolsAgentHostRegistry::GetDevToolsAgentHost(
643 host->render_view_host());
644 int devtools_cookie =
645 content::DevToolsManager::GetInstance()->DetachClientHost(agent);
[email protected]4814b512009-11-07 00:12:29646 if (devtools_cookie >= 0)
647 orphaned_dev_tools_[extension_id] = devtools_cookie;
648 }
649
[email protected]b65272f2009-08-31 15:47:06650 path = current_extension->path();
[email protected]44d62b62012-04-11 00:06:03651 DisableExtension(extension_id, Extension::DISABLE_RELOAD);
[email protected]f17dbd42010-08-16 23:21:10652 disabled_extension_paths_[extension_id] = path;
[email protected]1eb175082010-02-10 09:26:16653 } else {
654 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06655 }
656
[email protected]43ceb002012-02-10 23:19:15657 // If we're reloading a component extension, use the component extension
658 // loader's reloader.
659 if (component_loader_->Exists(extension_id)) {
660 component_loader_->Reload(extension_id);
661 return;
662 }
663
[email protected]e6090e42010-03-23 22:44:08664 // Check the installed extensions to see if what we're reloading was already
665 // installed.
666 scoped_ptr<ExtensionInfo> installed_extension(
667 extension_prefs_->GetInstalledExtensionInfo(extension_id));
668 if (installed_extension.get() &&
669 installed_extension->extension_manifest.get()) {
[email protected]d8c8f25f2011-11-02 18:18:01670 extensions::InstalledLoader(this).Load(*installed_extension, false);
[email protected]e6090e42010-03-23 22:44:08671 } else {
[email protected]d8c8f25f2011-11-02 18:18:01672 // Otherwise, the extension is unpacked (location LOAD).
[email protected]e6090e42010-03-23 22:44:08673 // We should always be able to remember the extension's path. If it's not in
674 // the map, someone failed to update |unloaded_extension_paths_|.
675 CHECK(!path.empty());
[email protected]d8c8f25f2011-11-02 18:18:01676 extensions::UnpackedInstaller::Create(this)->Load(path);
[email protected]e6090e42010-03-23 22:44:08677 }
[email protected]9cddd4702009-07-27 22:09:40678}
679
[email protected]fa2416f2011-05-03 08:41:20680bool ExtensionService::UninstallExtension(
[email protected]a12c706e2011-12-01 00:58:08681 std::string extension_id,
[email protected]fa2416f2011-05-03 08:41:20682 bool external_uninstall,
[email protected]65187152012-06-02 13:14:14683 string16* error) {
[email protected]a29a517a2011-01-21 21:11:12684 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20685
[email protected]0d6ec3a72011-09-02 02:09:43686 scoped_refptr<const Extension> extension(GetInstalledExtension(extension_id));
[email protected]631cf822009-05-15 07:01:25687
[email protected]e7afe2452010-08-22 16:19:13688 // Callers should not send us nonexistent extensions.
[email protected]aebe23a32010-12-10 22:15:48689 CHECK(extension);
[email protected]9f1087e2009-06-15 17:29:32690
[email protected]95da88c42011-03-31 10:07:33691 // Policy change which triggers an uninstall will always set
692 // |external_uninstall| to true so this is the only way to uninstall
693 // managed extensions.
[email protected]65187152012-06-02 13:14:14694 if (!external_uninstall &&
695 !system_->management_policy()->UserMayModifySettings(
696 extension.get(), error)) {
[email protected]ad50def52011-10-19 23:17:07697 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27698 chrome::NOTIFICATION_EXTENSION_UNINSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:53699 content::Source<Profile>(profile_),
700 content::Details<const Extension>(extension));
[email protected]d6ebc9792011-04-07 18:18:33701 return false;
702 }
[email protected]95da88c42011-03-31 10:07:33703
[email protected]3bdba0d2011-08-23 07:17:30704 // Extract the data we need for sync now, but don't actually sync until we've
705 // completed the uninstallation.
[email protected]65f173552012-06-28 22:43:58706 syncer::SyncChange sync_change;
[email protected]5db9ada2012-04-11 13:48:20707 if (app_sync_bundle_.HandlesApp(*extension)) {
708 sync_change = app_sync_bundle_.CreateSyncChangeToDelete(extension);
709 } else if (extension_sync_bundle_.HandlesExtension(*extension)) {
710 sync_change = extension_sync_bundle_.CreateSyncChangeToDelete(extension);
[email protected]3bdba0d2011-08-23 07:17:30711 }
712
[email protected]9b217652010-10-08 22:04:23713 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08714 extension->GetType(), 100);
[email protected]fe2dd7742011-04-19 22:52:49715 RecordPermissionMessagesHistogram(
716 extension, "Extensions.Permissions_Uninstall");
[email protected]9b217652010-10-08 22:04:23717
[email protected]8e5c89a2011-06-07 18:13:33718 TemplateURLService* url_service =
719 TemplateURLServiceFactory::GetForProfile(profile_);
720 if (url_service)
721 url_service->UnregisterExtensionKeyword(extension);
[email protected]56ad3792010-05-28 17:45:33722
[email protected]831aa212010-03-26 13:55:19723 // Unload before doing more cleanup to ensure that nothing is hanging on to
724 // any of these resources.
[email protected]814a7bf0f2011-08-13 05:30:59725 UnloadExtension(extension_id, extension_misc::UNLOAD_REASON_UNINSTALL);
[email protected]831aa212010-03-26 13:55:19726
[email protected]0d6ec3a72011-09-02 02:09:43727 extension_prefs_->OnExtensionUninstalled(extension_id, extension->location(),
[email protected]831aa212010-03-26 13:55:19728 external_uninstall);
[email protected]9f1087e2009-06-15 17:29:32729
730 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]0d6ec3a72011-09-02 02:09:43731 if (Extension::LOAD != extension->location()) {
[email protected]14908b72011-04-20 06:54:36732 if (!BrowserThread::PostTask(
733 BrowserThread::FILE, FROM_HERE,
[email protected]53612e82011-10-18 18:00:36734 base::Bind(
[email protected]14908b72011-04-20 06:54:36735 &extension_file_util::UninstallExtension,
736 install_directory_,
[email protected]fa2416f2011-05-03 08:41:20737 extension_id)))
[email protected]14908b72011-04-20 06:54:36738 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32739 }
740
[email protected]0d6ec3a72011-09-02 02:09:43741 GURL launch_web_url_origin(extension->launch_web_url());
742 launch_web_url_origin = launch_web_url_origin.GetOrigin();
[email protected]605fb8102012-05-04 01:36:55743 bool is_storage_isolated = extension->is_storage_isolated();
[email protected]0d6ec3a72011-09-02 02:09:43744
745 if (extension->is_hosted_app() &&
746 !profile_->GetExtensionSpecialStoragePolicy()->
747 IsStorageProtected(launch_web_url_origin)) {
[email protected]dc0b5a12011-10-14 00:06:13748 ExtensionDataDeleter::StartDeleting(
749 profile_, extension_id, launch_web_url_origin, is_storage_isolated);
[email protected]0d6ec3a72011-09-02 02:09:43750 }
[email protected]dc0b5a12011-10-14 00:06:13751 ExtensionDataDeleter::StartDeleting(
752 profile_, extension_id, extension->url(), is_storage_isolated);
[email protected]0d6ec3a72011-09-02 02:09:43753
[email protected]0dfe05c2011-02-23 23:03:36754 UntrackTerminatedExtension(extension_id);
[email protected]211030342010-09-30 18:41:06755
756 // Notify interested parties that we've uninstalled this extension.
[email protected]ad50def52011-10-19 23:17:07757 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27758 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
[email protected]6c2381d2011-10-19 02:52:53759 content::Source<Profile>(profile_),
[email protected]d67d9412012-07-17 17:19:42760 content::Details<const Extension>(extension));
[email protected]d6ebc9792011-04-07 18:18:33761
[email protected]5db9ada2012-04-11 13:48:20762 if (app_sync_bundle_.HasExtensionId(extension_id) &&
[email protected]a4a147652012-07-03 23:41:32763 sync_change.sync_data().GetDataType() == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:20764 app_sync_bundle_.ProcessDeletion(extension_id, sync_change);
765 } else if (extension_sync_bundle_.HasExtensionId(extension_id) &&
[email protected]a4a147652012-07-03 23:41:32766 sync_change.sync_data().GetDataType() == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:20767 extension_sync_bundle_.ProcessDeletion(extension_id, sync_change);
[email protected]3bdba0d2011-08-23 07:17:30768 }
769
[email protected]333b1de2011-09-12 18:28:50770 // Track the uninstallation.
[email protected]49098f702011-10-13 03:47:18771 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionUninstalled", 1, 2);
772
773 static bool default_apps_trial_exists =
[email protected]568c3312011-12-06 15:36:04774 base::FieldTrialList::TrialExists(kDefaultAppsTrialName);
[email protected]49098f702011-10-13 03:47:18775 if (default_apps_trial_exists) {
776 UMA_HISTOGRAM_ENUMERATION(
777 base::FieldTrial::MakeName("Extensions.ExtensionUninstalled",
[email protected]568c3312011-12-06 15:36:04778 kDefaultAppsTrialName),
[email protected]49098f702011-10-13 03:47:18779 1, 2);
780 }
[email protected]333b1de2011-09-12 18:28:50781
[email protected]7c1490da2011-10-11 18:53:25782 // Uninstalling one extension might have solved the problems of others.
783 // Therefore, we clear warnings of this type for all extensions.
784 std::set<ExtensionWarningSet::WarningType> warnings;
785 extension_warnings_.GetWarningsAffectingExtension(extension_id, &warnings);
786 extension_warnings_.ClearWarnings(warnings);
787
[email protected]d6ebc9792011-04-07 18:18:33788 return true;
[email protected]c10da4b02010-03-25 14:38:32789}
790
[email protected]c3cfb012011-04-06 22:07:35791bool ExtensionService::IsExtensionEnabled(
792 const std::string& extension_id) const {
[email protected]84df8332011-12-06 18:22:46793 if (extensions_.Contains(extension_id) ||
794 terminated_extensions_.Contains(extension_id))
[email protected]36429da2011-07-11 20:25:18795 return true;
796
[email protected]84df8332011-12-06 18:22:46797 if (disabled_extensions_.Contains(extension_id))
[email protected]ad83ca242011-07-29 01:32:25798 return false;
799
800 // If the extension hasn't been loaded yet, check the prefs for it. Assume
801 // enabled unless otherwise noted.
802 return !extension_prefs_->IsExtensionDisabled(extension_id) &&
803 !extension_prefs_->IsExternalExtensionUninstalled(extension_id);
[email protected]c3cfb012011-04-06 22:07:35804}
805
806bool ExtensionService::IsExternalExtensionUninstalled(
807 const std::string& extension_id) const {
808 return extension_prefs_->IsExternalExtensionUninstalled(extension_id);
809}
810
[email protected]eaa7dd182010-12-14 11:09:00811void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12812 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20813
[email protected]06f92562011-04-29 19:27:31814 if (IsExtensionEnabled(extension_id))
[email protected]0c6da502009-08-14 22:32:39815 return;
[email protected]0c6da502009-08-14 22:32:39816
[email protected]b3317ad2011-04-28 23:46:00817 extension_prefs_->SetExtensionState(extension_id, Extension::ENABLED);
[email protected]44d62b62012-04-11 00:06:03818 extension_prefs_->RemoveDisableReason(extension_id);
[email protected]1784e83a2009-09-08 21:01:52819
[email protected]06f92562011-04-29 19:27:31820 const Extension* extension =
821 GetExtensionByIdInternal(extension_id, false, true, false);
822 // This can happen if sync enables an extension that is not
823 // installed yet.
824 if (!extension)
825 return;
826
[email protected]0c6da502009-08-14 22:32:39827 // Move it over to the enabled list.
[email protected]84df8332011-12-06 18:22:46828 extensions_.Insert(make_scoped_refptr(extension));
829 disabled_extensions_.Remove(extension->id());
[email protected]0c6da502009-08-14 22:32:39830
[email protected]f681c782010-11-19 11:19:39831 // Make sure any browser action contained within it is not hidden.
832 extension_prefs_->SetBrowserActionVisibility(extension, true);
833
[email protected]62d30f42009-10-01 22:36:06834 NotifyExtensionLoaded(extension);
[email protected]3bdba0d2011-08-23 07:17:30835
[email protected]f74d7f32012-06-19 19:22:51836 // Notify listeners that the extension was enabled.
837 content::NotificationService::current()->Notify(
838 chrome::NOTIFICATION_EXTENSION_ENABLED,
839 content::Source<Profile>(profile_),
840 content::Details<const Extension>(extension));
841
[email protected]3bdba0d2011-08-23 07:17:30842 SyncExtensionChangeIfNeeded(*extension);
[email protected]0c6da502009-08-14 22:32:39843}
844
[email protected]44d62b62012-04-11 00:06:03845void ExtensionService::DisableExtension(
846 const std::string& extension_id,
847 Extension::DisableReason disable_reason) {
[email protected]a29a517a2011-01-21 21:11:12848 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20849
[email protected]b2ba9962009-12-10 20:10:15850 // The extension may have been disabled already.
[email protected]06f92562011-04-29 19:27:31851 if (!IsExtensionEnabled(extension_id))
[email protected]1784e83a2009-09-08 21:01:52852 return;
[email protected]1784e83a2009-09-08 21:01:52853
[email protected]06f92562011-04-29 19:27:31854 const Extension* extension = GetInstalledExtension(extension_id);
855 // |extension| can be NULL if sync disables an extension that is not
856 // installed yet.
[email protected]65187152012-06-02 13:14:14857 if (extension &&
858 !system_->management_policy()->UserMayModifySettings(extension, NULL)) {
[email protected]95da88c42011-03-31 10:07:33859 return;
[email protected]65187152012-06-02 13:14:14860 }
[email protected]95da88c42011-03-31 10:07:33861
[email protected]b3317ad2011-04-28 23:46:00862 extension_prefs_->SetExtensionState(extension_id, Extension::DISABLED);
[email protected]44d62b62012-04-11 00:06:03863 extension_prefs_->SetDisableReason(extension_id, disable_reason);
[email protected]1784e83a2009-09-08 21:01:52864
[email protected]06f92562011-04-29 19:27:31865 extension = GetExtensionByIdInternal(extension_id, true, false, true);
866 if (!extension)
867 return;
868
[email protected]1784e83a2009-09-08 21:01:52869 // Move it over to the disabled list.
[email protected]84df8332011-12-06 18:22:46870 disabled_extensions_.Insert(make_scoped_refptr(extension));
871 if (extensions_.Contains(extension->id()))
872 extensions_.Remove(extension->id());
873 else
874 terminated_extensions_.Remove(extension->id());
[email protected]1784e83a2009-09-08 21:01:52875
[email protected]814a7bf0f2011-08-13 05:30:59876 NotifyExtensionUnloaded(extension, extension_misc::UNLOAD_REASON_DISABLE);
[email protected]3bdba0d2011-08-23 07:17:30877
878 SyncExtensionChangeIfNeeded(*extension);
[email protected]7c1490da2011-10-11 18:53:25879
880 // Deactivating one extension might have solved the problems of others.
881 // Therefore, we clear warnings of this type for all extensions.
882 std::set<ExtensionWarningSet::WarningType> warnings;
883 extension_warnings_.GetWarningsAffectingExtension(extension_id, &warnings);
884 extension_warnings_.ClearWarnings(warnings);
[email protected]1784e83a2009-09-08 21:01:52885}
886
[email protected]eaa7dd182010-12-14 11:09:00887void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]b70a2d92012-06-28 19:51:21888 const Extension* extension, bool record_oauth2_grant) {
[email protected]8d888c12010-11-30 00:00:25889 CHECK(extension);
[email protected]fe2dd7742011-04-19 22:52:49890 RecordPermissionMessagesHistogram(
891 extension, "Extensions.Permissions_ReEnable");
[email protected]c333e792012-01-06 16:57:39892 extensions::PermissionsUpdater perms_updater(profile());
[email protected]b70a2d92012-06-28 19:51:21893 perms_updater.GrantActivePermissions(extension, record_oauth2_grant);
[email protected]8d888c12010-11-30 00:00:25894 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
895 EnableExtension(extension->id());
896}
897
[email protected]fe2dd7742011-04-19 22:52:49898// static
899void ExtensionService::RecordPermissionMessagesHistogram(
900 const Extension* e, const char* histogram) {
901 // Since this is called from multiple sources, and since the Histogram macros
902 // use statics, we need to manually lookup the Histogram ourselves.
903 base::Histogram* counter = base::LinearHistogram::FactoryGet(
904 histogram,
905 1,
[email protected]c2e66e12012-06-27 06:27:06906 PermissionMessage::kEnumBoundary,
907 PermissionMessage::kEnumBoundary + 1,
[email protected]fe2dd7742011-04-19 22:52:49908 base::Histogram::kUmaTargetedHistogramFlag);
909
[email protected]c2e66e12012-06-27 06:27:06910 PermissionMessages permissions = e->GetPermissionMessages();
[email protected]fe2dd7742011-04-19 22:52:49911 if (permissions.empty()) {
[email protected]c2e66e12012-06-27 06:27:06912 counter->Add(PermissionMessage::kNone);
[email protected]fe2dd7742011-04-19 22:52:49913 } else {
[email protected]c2e66e12012-06-27 06:27:06914 for (PermissionMessages::iterator it = permissions.begin();
[email protected]0d3e4a22011-06-23 19:02:52915 it != permissions.end(); ++it)
916 counter->Add(it->id());
[email protected]fe2dd7742011-04-19 22:52:49917 }
918}
919
[email protected]eaa7dd182010-12-14 11:09:00920void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]57a777f72010-03-31 01:09:42921 // The ChromeURLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:06922 // was loaded, otherwise a race can arise where a renderer that is created
923 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:42924 // that the request context doesn't yet know about. The profile is responsible
925 // for ensuring its URLRequestContexts appropriately discover the loaded
926 // extension.
[email protected]31d8f5f22012-04-02 15:22:08927 system_->RegisterExtensionWithRequestContexts(extension);
[email protected]62d30f42009-10-01 22:36:06928
[email protected]19647262011-12-16 09:57:49929 // Tell renderers about the new extension, unless it's a theme (renderers
930 // don't need to know about themes).
931 if (!extension->is_theme()) {
932 for (content::RenderProcessHost::iterator i(
933 content::RenderProcessHost::AllHostsIterator());
934 !i.IsAtEnd(); i.Advance()) {
935 content::RenderProcessHost* host = i.GetCurrentValue();
936 Profile* host_profile =
937 Profile::FromBrowserContext(host->GetBrowserContext());
938 if (host_profile->GetOriginalProfile() ==
939 profile_->GetOriginalProfile()) {
940 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions(
941 1, ExtensionMsg_Loaded_Params(extension));
942 host->Send(
943 new ExtensionMsg_Loaded(loaded_extensions));
944 }
[email protected]c8d407e2011-04-28 21:27:17945 }
[email protected]77a6970c2011-04-23 16:58:56946 }
947
[email protected]3442a662012-01-12 08:06:17948 // Tell subsystems that use the EXTENSION_LOADED notification about the new
949 // extension.
950 //
951 // NOTE: It is important that this happen after notifying the renderers about
952 // the new extensions so that if we navigate to an extension URL in
953 // NOTIFICATION_EXTENSION_LOADED, the renderer is guaranteed to know about it.
954 content::NotificationService::current()->Notify(
955 chrome::NOTIFICATION_EXTENSION_LOADED,
956 content::Source<Profile>(profile_),
957 content::Details<const Extension>(extension));
958
[email protected]c8d407e2011-04-28 21:27:17959 // Tell a random-ass collection of other subsystems about the new extension.
960 // TODO(aa): What should we do with all this goop? Can it move into the
961 // relevant objects via EXTENSION_LOADED?
962
963 profile_->GetExtensionSpecialStoragePolicy()->
964 GrantRightsForExtension(extension);
965
966 UpdateActiveExtensionsInCrashReporter();
967
968 ExtensionWebUI::RegisterChromeURLOverrides(
969 profile_, extension->GetChromeURLOverrides());
970
[email protected]8e5c89a2011-06-07 18:13:33971 TemplateURLService* url_service =
972 TemplateURLServiceFactory::GetForProfile(profile_);
973 if (url_service)
974 url_service->RegisterExtensionKeyword(extension);
[email protected]c8d407e2011-04-28 21:27:17975
976 // Load the icon for omnibox-enabled extensions so it will be ready to display
977 // in the URL bar.
978 if (!extension->omnibox_keyword().empty()) {
979 omnibox_popup_icon_manager_.LoadIcon(extension);
980 omnibox_icon_manager_.LoadIcon(extension);
981 }
982
983 // If the extension has permission to load chrome://favicon/ resources we need
984 // to make sure that the FaviconSource is registered with the
985 // ChromeURLDataManager.
986 if (extension->HasHostPermission(GURL(chrome::kChromeUIFaviconURL))) {
987 FaviconSource* favicon_source = new FaviconSource(profile_,
988 FaviconSource::FAVICON);
[email protected]ef92e172012-04-25 19:40:41989 ChromeURLDataManager::AddDataSource(profile_, favicon_source);
[email protected]c8d407e2011-04-28 21:27:17990 }
[email protected]5eddc3e2011-10-26 04:33:31991 // Same for chrome://thumb/ resources.
992 if (extension->HasHostPermission(GURL(chrome::kChromeUIThumbnailURL))) {
993 ThumbnailSource* thumbnail_source = new ThumbnailSource(profile_);
[email protected]ef92e172012-04-25 19:40:41994 ChromeURLDataManagerFactory::GetForProfile(profile_)->
995 AddDataSource(thumbnail_source);
[email protected]5eddc3e2011-10-26 04:33:31996 }
[email protected]c8d407e2011-04-28 21:27:17997
998 // TODO(mpcomplete): This ends up affecting all profiles. See crbug.com/80757.
[email protected]2de307592011-04-05 21:16:58999 bool plugins_changed = false;
1000 for (size_t i = 0; i < extension->plugins().size(); ++i) {
1001 const Extension::PluginInfo& plugin = extension->plugins()[i];
[email protected]f520b5b2011-11-08 02:42:141002 PluginService::GetInstance()->RefreshPlugins();
1003 PluginService::GetInstance()->AddExtraPluginPath(plugin.path);
[email protected]2de307592011-04-05 21:16:581004 plugins_changed = true;
[email protected]476a27032011-09-13 13:40:491005 ChromePluginServiceFilter* filter =
1006 ChromePluginServiceFilter::GetInstance();
1007 if (plugin.is_public) {
1008 filter->RestrictPluginToProfileAndOrigin(
1009 plugin.path, profile_, GURL());
1010 } else {
1011 filter->RestrictPluginToProfileAndOrigin(
1012 plugin.path, profile_, extension->url());
[email protected]2de307592011-04-05 21:16:581013 }
1014 }
[email protected]84396dbc2011-04-14 06:33:421015
1016 bool nacl_modules_changed = false;
1017 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1018 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
[email protected]cebc3dc2011-04-18 17:15:001019 RegisterNaClModule(module.url, module.mime_type);
[email protected]84396dbc2011-04-14 06:33:421020 nacl_modules_changed = true;
1021 }
1022
[email protected]ed0ba002011-05-26 16:55:131023 if (nacl_modules_changed)
1024 UpdatePluginListWithNaClModules();
[email protected]84396dbc2011-04-14 06:33:421025
1026 if (plugins_changed || nacl_modules_changed)
[email protected]45a22e62011-10-12 09:48:021027 PluginService::GetInstance()->PurgePluginListCache(profile_, false);
[email protected]b4d9d122011-06-17 01:58:541028
[email protected]14da67b02011-08-02 05:49:131029#if defined(OS_CHROMEOS)
[email protected]b4d9d122011-06-17 01:58:541030 for (std::vector<Extension::InputComponentInfo>::const_iterator component =
1031 extension->input_components().begin();
1032 component != extension->input_components().end();
1033 ++component) {
[email protected]14da67b02011-08-02 05:49:131034 if (component->type == Extension::INPUT_COMPONENT_TYPE_IME) {
[email protected]1364c6d2012-07-24 11:57:191035 extensions::InputImeEventRouter::GetInstance()->RegisterIme(
[email protected]14da67b02011-08-02 05:49:131036 profile_, extension->id(), *component);
1037 }
[email protected]b4d9d122011-06-17 01:58:541038 }
1039#endif
[email protected]62d30f42009-10-01 22:36:061040}
1041
[email protected]a9f39a312010-12-23 22:14:271042void ExtensionService::NotifyExtensionUnloaded(
[email protected]814a7bf0f2011-08-13 05:30:591043 const Extension* extension,
1044 extension_misc::UnloadedExtensionReason reason) {
[email protected]a9f39a312010-12-23 22:14:271045 UnloadedExtensionInfo details(extension, reason);
[email protected]ad50def52011-10-19 23:17:071046 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271047 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]6c2381d2011-10-19 02:52:531048 content::Source<Profile>(profile_),
1049 content::Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061050
[email protected]f3b1a082011-11-18 00:34:301051 for (content::RenderProcessHost::iterator i(
1052 content::RenderProcessHost::AllHostsIterator());
[email protected]77a6970c2011-04-23 16:58:561053 !i.IsAtEnd(); i.Advance()) {
[email protected]f3b1a082011-11-18 00:34:301054 content::RenderProcessHost* host = i.GetCurrentValue();
[email protected]9b62ecf2011-07-27 20:23:081055 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:301056 Profile::FromBrowserContext(host->GetBrowserContext());
[email protected]9b62ecf2011-07-27 20:23:081057 if (host_profile->GetOriginalProfile() == profile_->GetOriginalProfile())
[email protected]c8d407e2011-04-28 21:27:171058 host->Send(new ExtensionMsg_Unloaded(extension->id()));
[email protected]77a6970c2011-04-23 16:58:561059 }
1060
[email protected]31d8f5f22012-04-02 15:22:081061 system_->UnregisterExtensionWithRequestContexts(extension->id(), reason);
[email protected]c8d407e2011-04-28 21:27:171062 profile_->GetExtensionSpecialStoragePolicy()->
1063 RevokeRightsForExtension(extension);
1064
1065 ExtensionWebUI::UnregisterChromeURLOverrides(
1066 profile_, extension->GetChromeURLOverrides());
1067
[email protected]b777b332011-04-16 04:01:081068#if defined(OS_CHROMEOS)
1069 // Revoke external file access to
[email protected]55eb70e762012-02-20 17:38:391070 if (BrowserContext::GetFileSystemContext(profile_) &&
1071 BrowserContext::GetFileSystemContext(profile_)->external_provider()) {
1072 BrowserContext::GetFileSystemContext(profile_)->external_provider()->
[email protected]c8d407e2011-04-28 21:27:171073 RevokeAccessForExtension(extension->id());
[email protected]62d30f42009-10-01 22:36:061074 }
[email protected]54ccafa2011-12-06 23:36:321075
1076 if (extension->input_components().size() > 0) {
[email protected]1364c6d2012-07-24 11:57:191077 extensions::InputImeEventRouter::GetInstance()->UnregisterAllImes(
[email protected]54ccafa2011-12-06 23:36:321078 profile_, extension->id());
1079 }
[email protected]c8d407e2011-04-28 21:27:171080#endif
1081
1082 UpdateActiveExtensionsInCrashReporter();
[email protected]2de307592011-04-05 21:16:581083
1084 bool plugins_changed = false;
1085 for (size_t i = 0; i < extension->plugins().size(); ++i) {
1086 const Extension::PluginInfo& plugin = extension->plugins()[i];
[email protected]b6a2f8de2012-01-31 17:28:491087 PluginService::GetInstance()->ForcePluginShutdown(plugin.path);
[email protected]f520b5b2011-11-08 02:42:141088 PluginService::GetInstance()->RefreshPlugins();
1089 PluginService::GetInstance()->RemoveExtraPluginPath(plugin.path);
[email protected]2de307592011-04-05 21:16:581090 plugins_changed = true;
[email protected]476a27032011-09-13 13:40:491091 ChromePluginServiceFilter::GetInstance()->UnrestrictPlugin(plugin.path);
[email protected]2de307592011-04-05 21:16:581092 }
[email protected]84396dbc2011-04-14 06:33:421093
1094 bool nacl_modules_changed = false;
1095 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1096 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
[email protected]cebc3dc2011-04-18 17:15:001097 UnregisterNaClModule(module.url);
[email protected]84396dbc2011-04-14 06:33:421098 nacl_modules_changed = true;
1099 }
1100
[email protected]ed0ba002011-05-26 16:55:131101 if (nacl_modules_changed)
1102 UpdatePluginListWithNaClModules();
[email protected]84396dbc2011-04-14 06:33:421103
1104 if (plugins_changed || nacl_modules_changed)
[email protected]45a22e62011-10-12 09:48:021105 PluginService::GetInstance()->PurgePluginListCache(profile_, false);
[email protected]62d30f42009-10-01 22:36:061106}
1107
[email protected]eaa7dd182010-12-14 11:09:001108void ExtensionService::UpdateExtensionBlacklist(
[email protected]6b75ec32009-08-14 06:37:181109 const std::vector<std::string>& blacklist) {
1110 // Use this set to indicate if an extension in the blacklist has been used.
1111 std::set<std::string> blacklist_set;
1112 for (unsigned int i = 0; i < blacklist.size(); ++i) {
1113 if (Extension::IdIsValid(blacklist[i])) {
1114 blacklist_set.insert(blacklist[i]);
1115 }
1116 }
1117 extension_prefs_->UpdateBlacklist(blacklist_set);
1118 std::vector<std::string> to_be_removed;
1119 // Loop current extensions, unload installed extensions.
[email protected]84df8332011-12-06 18:22:461120 for (ExtensionSet::const_iterator iter = extensions_.begin();
[email protected]6b75ec32009-08-14 06:37:181121 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021122 const Extension* extension = (*iter);
[email protected]6b75ec32009-08-14 06:37:181123 if (blacklist_set.find(extension->id()) != blacklist_set.end()) {
1124 to_be_removed.push_back(extension->id());
1125 }
1126 }
1127
1128 // UnloadExtension will change the extensions_ list. So, we should
1129 // call it outside the iterator loop.
1130 for (unsigned int i = 0; i < to_be_removed.size(); ++i) {
[email protected]814a7bf0f2011-08-13 05:30:591131 UnloadExtension(to_be_removed[i], extension_misc::UNLOAD_REASON_DISABLE);
[email protected]6b75ec32009-08-14 06:37:181132 }
1133}
1134
[email protected]0a071a32011-02-08 00:18:241135Profile* ExtensionService::profile() {
1136 return profile_;
1137}
1138
[email protected]45759612012-07-10 17:21:231139extensions::ExtensionPrefs* ExtensionService::extension_prefs() {
[email protected]ddb1e5a2010-12-13 20:10:451140 return extension_prefs_;
1141}
1142
[email protected]0d9a2202011-11-09 13:48:411143extensions::SettingsFrontend* ExtensionService::settings_frontend() {
[email protected]cccdf0aa2011-11-11 03:43:381144 return settings_frontend_.get();
[email protected]b7f853e282011-08-10 09:24:201145}
1146
[email protected]e2d720aa2012-05-17 00:11:351147extensions::ContentSettingsStore* ExtensionService::GetContentSettingsStore() {
[email protected]b790b072011-05-20 09:46:441148 return extension_prefs()->content_settings_store();
1149}
1150
[email protected]25ae0152011-11-18 14:40:021151bool ExtensionService::is_ready() {
1152 return ready_;
1153}
1154
[email protected]42a08162012-03-16 18:09:111155extensions::ExtensionUpdater* ExtensionService::updater() {
[email protected]2859946f2011-04-04 18:18:061156 return updater_.get();
1157}
1158
[email protected]eaa7dd182010-12-14 11:09:001159void ExtensionService::CheckAdminBlacklist() {
[email protected]aa96d3a2010-08-21 08:45:251160 std::vector<std::string> to_be_removed;
1161 // Loop through extensions list, unload installed extensions.
[email protected]84df8332011-12-06 18:22:461162 for (ExtensionSet::const_iterator iter = extensions_.begin();
[email protected]aa96d3a2010-08-21 08:45:251163 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021164 const Extension* extension = (*iter);
[email protected]65187152012-06-02 13:14:141165 if (!system_->management_policy()->UserMayLoad(extension, NULL)) {
[email protected]aa96d3a2010-08-21 08:45:251166 to_be_removed.push_back(extension->id());
[email protected]05aad2da2011-10-28 10:12:371167 }
[email protected]aa96d3a2010-08-21 08:45:251168 }
1169
1170 // UnloadExtension will change the extensions_ list. So, we should
1171 // call it outside the iterator loop.
1172 for (unsigned int i = 0; i < to_be_removed.size(); ++i)
[email protected]814a7bf0f2011-08-13 05:30:591173 UnloadExtension(to_be_removed[i], extension_misc::UNLOAD_REASON_DISABLE);
[email protected]aa96d3a2010-08-21 08:45:251174}
1175
[email protected]31206602011-04-13 23:07:321176void ExtensionService::CheckForUpdatesSoon() {
[email protected]c3cfb012011-04-06 22:07:351177 if (updater()) {
[email protected]94fde232012-04-27 10:22:301178 if (AreAllExternalProvidersReady()) {
1179 updater()->CheckSoon();
1180 } else {
1181 // Sync can start updating before all the external providers are ready
1182 // during startup. Start the update as soon as those providers are ready,
1183 // but not before.
1184 update_once_all_providers_are_ready_ = true;
1185 }
[email protected]c3cfb012011-04-06 22:07:351186 } else {
[email protected]31206602011-04-13 23:07:321187 LOG(WARNING) << "CheckForUpdatesSoon() called with auto-update turned off";
[email protected]c3cfb012011-04-06 22:07:351188 }
1189}
1190
[email protected]65f173552012-06-28 22:43:581191syncer::SyncError ExtensionService::MergeDataAndStartSyncing(
[email protected]a4a147652012-07-03 23:41:321192 syncer::ModelType type,
[email protected]65f173552012-06-28 22:43:581193 const syncer::SyncDataList& initial_sync_data,
1194 scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
1195 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) {
[email protected]5db9ada2012-04-11 13:48:201196 CHECK(sync_processor.get());
[email protected]5d36e052012-04-20 20:12:281197 CHECK(sync_error_factory.get());
[email protected]3bdba0d2011-08-23 07:17:301198
1199 switch (type) {
[email protected]a4a147652012-07-03 23:41:321200 case syncer::EXTENSIONS:
[email protected]5db9ada2012-04-11 13:48:201201 extension_sync_bundle_.SetupSync(sync_processor.release(),
[email protected]5d36e052012-04-20 20:12:281202 sync_error_factory.release(),
[email protected]5db9ada2012-04-11 13:48:201203 initial_sync_data);
[email protected]3bdba0d2011-08-23 07:17:301204 break;
1205
[email protected]a4a147652012-07-03 23:41:321206 case syncer::APPS:
[email protected]5d36e052012-04-20 20:12:281207 app_sync_bundle_.SetupSync(sync_processor.release(),
1208 sync_error_factory.release(),
1209 initial_sync_data);
[email protected]3bdba0d2011-08-23 07:17:301210 break;
1211
1212 default:
1213 LOG(FATAL) << "Got " << type << " ModelType";
1214 }
[email protected]3bdba0d2011-08-23 07:17:301215
[email protected]b1417ab2011-12-15 17:09:111216 // Process local extensions.
1217 // TODO(yoz): Determine whether pending extensions should be considered too.
1218 // See crbug.com/104399.
[email protected]65f173552012-06-28 22:43:581219 syncer::SyncDataList sync_data_list = GetAllSyncData(type);
1220 syncer::SyncChangeList sync_change_list;
1221 for (syncer::SyncDataList::const_iterator i = sync_data_list.begin();
[email protected]3bdba0d2011-08-23 07:17:301222 i != sync_data_list.end();
1223 ++i) {
[email protected]5db9ada2012-04-11 13:48:201224 switch (type) {
[email protected]a4a147652012-07-03 23:41:321225 case syncer::EXTENSIONS:
[email protected]5db9ada2012-04-11 13:48:201226 sync_change_list.push_back(
1227 extension_sync_bundle_.CreateSyncChange(*i));
1228 break;
[email protected]a4a147652012-07-03 23:41:321229 case syncer::APPS:
[email protected]5db9ada2012-04-11 13:48:201230 sync_change_list.push_back(app_sync_bundle_.CreateSyncChange(*i));
1231 break;
1232 default:
1233 LOG(FATAL) << "Got " << type << " ModelType";
1234 }
[email protected]3bdba0d2011-08-23 07:17:301235 }
[email protected]3bdba0d2011-08-23 07:17:301236
[email protected]5db9ada2012-04-11 13:48:201237
[email protected]a4a147652012-07-03 23:41:321238 if (type == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:201239 extension_sync_bundle_.ProcessSyncChangeList(sync_change_list);
[email protected]a4a147652012-07-03 23:41:321240 } else if (type == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:201241 app_sync_bundle_.ProcessSyncChangeList(sync_change_list);
1242 }
[email protected]0ffd9e52012-01-26 21:44:341243
[email protected]65f173552012-06-28 22:43:581244 return syncer::SyncError();
[email protected]3bdba0d2011-08-23 07:17:301245}
1246
[email protected]a4a147652012-07-03 23:41:321247void ExtensionService::StopSyncing(syncer::ModelType type) {
1248 if (type == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:201249 app_sync_bundle_.Reset();
[email protected]a4a147652012-07-03 23:41:321250 } else if (type == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:201251 extension_sync_bundle_.Reset();
1252 }
[email protected]3bdba0d2011-08-23 07:17:301253}
1254
[email protected]65f173552012-06-28 22:43:581255syncer::SyncDataList ExtensionService::GetAllSyncData(
[email protected]a4a147652012-07-03 23:41:321256 syncer::ModelType type) const {
1257 if (type == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:201258 return extension_sync_bundle_.GetAllSyncData();
1259
[email protected]a4a147652012-07-03 23:41:321260 } else if (type == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:201261 return app_sync_bundle_.GetAllSyncData();
[email protected]3bdba0d2011-08-23 07:17:301262 }
[email protected]5db9ada2012-04-11 13:48:201263
1264 // We should only get sync data for extensions and apps.
1265 NOTREACHED();
1266
[email protected]65f173552012-06-28 22:43:581267 return syncer::SyncDataList();
[email protected]3bdba0d2011-08-23 07:17:301268}
1269
[email protected]65f173552012-06-28 22:43:581270syncer::SyncError ExtensionService::ProcessSyncChanges(
[email protected]3bdba0d2011-08-23 07:17:301271 const tracked_objects::Location& from_here,
[email protected]65f173552012-06-28 22:43:581272 const syncer::SyncChangeList& change_list) {
1273 for (syncer::SyncChangeList::const_iterator i = change_list.begin();
[email protected]3bdba0d2011-08-23 07:17:301274 i != change_list.end();
1275 ++i) {
[email protected]a4a147652012-07-03 23:41:321276 syncer::ModelType type = i->sync_data().GetDataType();
1277 if (type == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:201278 extension_sync_bundle_.ProcessSyncChange(
1279 extensions::ExtensionSyncData(*i));
[email protected]a4a147652012-07-03 23:41:321280 } else if (type == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:201281 app_sync_bundle_.ProcessSyncChange(extensions::AppSyncData(*i));
1282 }
[email protected]3bdba0d2011-08-23 07:17:301283 }
1284
[email protected]0ffd9e52012-01-26 21:44:341285 extension_prefs()->extension_sorting()->FixNTPOrdinalCollisions();
1286
[email protected]65f173552012-06-28 22:43:581287 return syncer::SyncError();
[email protected]b05fb9ff2011-04-23 00:07:561288}
1289
[email protected]5db9ada2012-04-11 13:48:201290extensions::ExtensionSyncData ExtensionService::GetExtensionSyncData(
1291 const Extension& extension) const {
1292 return extensions::ExtensionSyncData(extension,
1293 IsExtensionEnabled(extension.id()),
1294 IsIncognitoEnabled(extension.id()));
[email protected]b05fb9ff2011-04-23 00:07:561295}
1296
[email protected]5db9ada2012-04-11 13:48:201297extensions::AppSyncData ExtensionService::GetAppSyncData(
1298 const Extension& extension) const {
1299 return extensions::AppSyncData(
1300 extension,
1301 IsExtensionEnabled(extension.id()),
1302 IsIncognitoEnabled(extension.id()),
1303 extension_prefs_->GetAppNotificationClientId(extension.id()),
1304 extension_prefs_->IsAppNotificationDisabled(extension.id()),
1305 extension_prefs_->extension_sorting()->GetAppLaunchOrdinal(
1306 extension.id()),
1307 extension_prefs_->extension_sorting()->GetPageOrdinal(extension.id()));
1308}
[email protected]3bdba0d2011-08-23 07:17:301309
[email protected]5db9ada2012-04-11 13:48:201310std::vector<extensions::ExtensionSyncData>
1311 ExtensionService::GetExtensionSyncDataList() const {
1312 std::vector<extensions::ExtensionSyncData> extension_sync_list;
1313 extension_sync_bundle_.GetExtensionSyncDataListHelper(extensions_,
1314 &extension_sync_list);
1315 extension_sync_bundle_.GetExtensionSyncDataListHelper(disabled_extensions_,
1316 &extension_sync_list);
1317 extension_sync_bundle_.GetExtensionSyncDataListHelper(terminated_extensions_,
1318 &extension_sync_list);
1319
1320 std::vector<extensions::ExtensionSyncData> pending_extensions =
1321 extension_sync_bundle_.GetPendingData();
1322 extension_sync_list.insert(extension_sync_list.begin(),
1323 pending_extensions.begin(),
1324 pending_extensions.end());
[email protected]3bdba0d2011-08-23 07:17:301325
1326 return extension_sync_list;
[email protected]b05fb9ff2011-04-23 00:07:561327}
1328
[email protected]5db9ada2012-04-11 13:48:201329std::vector<extensions::AppSyncData> ExtensionService::GetAppSyncDataList()
1330 const {
1331 std::vector<extensions::AppSyncData> app_sync_list;
1332 app_sync_bundle_.GetAppSyncDataListHelper(extensions_, &app_sync_list);
1333 app_sync_bundle_.GetAppSyncDataListHelper(disabled_extensions_,
1334 &app_sync_list);
1335 app_sync_bundle_.GetAppSyncDataListHelper(terminated_extensions_,
1336 &app_sync_list);
1337
1338 std::vector<extensions::AppSyncData> pending_apps =
1339 app_sync_bundle_.GetPendingData();
1340 app_sync_list.insert(app_sync_list.begin(),
1341 pending_apps.begin(),
1342 pending_apps.end());
1343
1344 return app_sync_list;
1345}
1346
1347bool ExtensionService::ProcessExtensionSyncData(
1348 const extensions::ExtensionSyncData& extension_sync_data) {
1349 if (!ProcessExtensionSyncDataHelper(extension_sync_data,
[email protected]a4a147652012-07-03 23:41:321350 syncer::EXTENSIONS)) {
[email protected]5db9ada2012-04-11 13:48:201351 extension_sync_bundle_.AddPendingExtension(extension_sync_data.id(),
1352 extension_sync_data);
1353 CheckForUpdatesSoon();
1354 return false;
1355 }
1356
1357 return true;
1358}
1359
1360bool ExtensionService::ProcessAppSyncData(
1361 const extensions::AppSyncData& app_sync_data) {
1362 const std::string& id = app_sync_data.id();
1363 const Extension* extension = GetInstalledExtension(id);
1364 bool extension_installed = (extension != NULL);
1365
1366 if (app_sync_data.app_launch_ordinal().IsValid() &&
1367 app_sync_data.page_ordinal().IsValid()) {
1368 extension_prefs_->extension_sorting()->SetAppLaunchOrdinal(
1369 id,
1370 app_sync_data.app_launch_ordinal());
1371 extension_prefs_->extension_sorting()->SetPageOrdinal(
1372 id,
1373 app_sync_data.page_ordinal());
1374 }
1375
1376 if (extension_installed) {
1377 if (app_sync_data.notifications_disabled() !=
1378 extension_prefs_->IsAppNotificationDisabled(id)) {
1379 extension_prefs_->SetAppNotificationDisabled(
1380 id, app_sync_data.notifications_disabled());
1381 }
1382 }
1383
1384 if (!ProcessExtensionSyncDataHelper(app_sync_data.extension_sync_data(),
[email protected]a4a147652012-07-03 23:41:321385 syncer::APPS)) {
[email protected]5db9ada2012-04-11 13:48:201386 app_sync_bundle_.AddPendingApp(id, app_sync_data);
1387 CheckForUpdatesSoon();
1388 return false;
1389 }
1390
1391 return true;
1392}
1393
1394bool ExtensionService::IsCorrectSyncType(const Extension& extension,
[email protected]a4a147652012-07-03 23:41:321395 syncer::ModelType type) const {
1396 if (type == syncer::EXTENSIONS &&
[email protected]5db9ada2012-04-11 13:48:201397 extension.GetSyncType() == Extension::SYNC_TYPE_EXTENSION)
1398 return true;
1399
[email protected]a4a147652012-07-03 23:41:321400 if (type == syncer::APPS &&
[email protected]5db9ada2012-04-11 13:48:201401 extension.GetSyncType() == Extension::SYNC_TYPE_APP)
1402 return true;
1403
1404 return false;
1405}
1406
1407bool ExtensionService::ProcessExtensionSyncDataHelper(
1408 const extensions::ExtensionSyncData& extension_sync_data,
[email protected]a4a147652012-07-03 23:41:321409 syncer::ModelType type) {
[email protected]3bdba0d2011-08-23 07:17:301410 const std::string& id = extension_sync_data.id();
[email protected]96e989b2011-08-30 19:35:061411 const Extension* extension = GetInstalledExtension(id);
1412
1413 // TODO(bolms): we should really handle this better. The particularly bad
1414 // case is where an app becomes an extension or vice versa, and we end up with
1415 // a zombie extension that won't go away.
[email protected]5db9ada2012-04-11 13:48:201416 if (extension && !IsCorrectSyncType(*extension, type))
1417 return true;
[email protected]90310d92011-04-17 07:35:041418
1419 // Handle uninstalls first.
[email protected]3bdba0d2011-08-23 07:17:301420 if (extension_sync_data.uninstalled()) {
[email protected]90310d92011-04-17 07:35:041421 std::string error;
1422 if (!UninstallExtensionHelper(this, id)) {
1423 LOG(WARNING) << "Could not uninstall extension " << id
1424 << " for sync";
1425 }
[email protected]5db9ada2012-04-11 13:48:201426 return true;
[email protected]90310d92011-04-17 07:35:041427 }
1428
[email protected]caf2d442012-05-04 10:00:501429 // Extension from sync was uninstalled by the user as external extensions.
1430 // Honor user choice and skip installation/enabling.
1431 if (IsExternalExtensionUninstalled(id)) {
1432 LOG(WARNING) << "Extension with id " << id
1433 << " from sync was uninstalled as external extension";
1434 return true;
1435 }
1436
[email protected]06f92562011-04-29 19:27:311437 // Set user settings.
[email protected]3bdba0d2011-08-23 07:17:301438 if (extension_sync_data.enabled()) {
[email protected]06f92562011-04-29 19:27:311439 EnableExtension(id);
1440 } else {
[email protected]44d62b62012-04-11 00:06:031441 DisableExtension(id, Extension::DISABLE_USER_ACTION);
[email protected]06f92562011-04-29 19:27:311442 }
[email protected]81b14cc2011-04-29 00:39:371443
[email protected]406b5a92011-11-08 11:58:261444 // We need to cache some version information here because setting the
1445 // incognito flag invalidates the |extension| pointer (it reloads the
1446 // extension).
[email protected]9060d8b02012-01-13 02:14:301447 bool extension_installed = (extension != NULL);
[email protected]406b5a92011-11-08 11:58:261448 int result = extension ?
1449 extension->version()->CompareTo(extension_sync_data.version()) : 0;
1450 SetIsIncognitoEnabled(id, extension_sync_data.incognito_enabled());
1451 extension = NULL; // No longer safe to use.
1452
1453 if (extension_installed) {
[email protected]06f92562011-04-29 19:27:311454 // If the extension is already installed, check if it's outdated.
[email protected]90310d92011-04-17 07:35:041455 if (result < 0) {
1456 // Extension is outdated.
[email protected]5db9ada2012-04-11 13:48:201457 return false;
[email protected]b020dc12011-12-06 04:35:421458 }
[email protected]06f92562011-04-29 19:27:311459 } else {
[email protected]06f92562011-04-29 19:27:311460 // TODO(akalin): Replace silent update with a list of enabled
1461 // permissions.
[email protected]6cc7dbae2011-04-29 21:18:331462 const bool kInstallSilently = true;
[email protected]5db9ada2012-04-11 13:48:201463
[email protected]a4a147652012-07-03 23:41:321464 CHECK(type == syncer::EXTENSIONS || type == syncer::APPS);
[email protected]5db9ada2012-04-11 13:48:201465 ExtensionFilter filter =
[email protected]a4a147652012-07-03 23:41:321466 (type == syncer::APPS) ? IsSyncableApp : IsSyncableExtension;
[email protected]5db9ada2012-04-11 13:48:201467
[email protected]06f92562011-04-29 19:27:311468 if (!pending_extension_manager()->AddFromSync(
1469 id,
[email protected]3bdba0d2011-08-23 07:17:301470 extension_sync_data.update_url(),
[email protected]5db9ada2012-04-11 13:48:201471 filter,
[email protected]6cc7dbae2011-04-29 21:18:331472 kInstallSilently)) {
[email protected]06f92562011-04-29 19:27:311473 LOG(WARNING) << "Could not add pending extension for " << id;
[email protected]3bdba0d2011-08-23 07:17:301474 // This means that the extension is already pending installation, with a
1475 // non-INTERNAL location. Add to pending_sync_data, even though it will
1476 // never be removed (we'll never install a syncable version of the
1477 // extension), so that GetAllSyncData() continues to send it.
[email protected]06f92562011-04-29 19:27:311478 }
[email protected]3bdba0d2011-08-23 07:17:301479 // Track pending extensions so that we can return them in GetAllSyncData().
[email protected]5db9ada2012-04-11 13:48:201480 return false;
[email protected]90310d92011-04-17 07:35:041481 }
[email protected]5db9ada2012-04-11 13:48:201482
1483 return true;
[email protected]90310d92011-04-17 07:35:041484}
1485
[email protected]c3cfb012011-04-06 22:07:351486bool ExtensionService::IsIncognitoEnabled(
1487 const std::string& extension_id) const {
1488 // If this is an existing component extension we always allow it to
1489 // work in incognito mode.
[email protected]81b14cc2011-04-29 00:39:371490 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]c3cfb012011-04-06 22:07:351491 if (extension && extension->location() == Extension::COMPONENT)
[email protected]cb0ce1e022010-03-10 19:54:411492 return true;
1493
1494 // Check the prefs.
[email protected]c3cfb012011-04-06 22:07:351495 return extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]db7331a2010-02-25 22:10:501496}
[email protected]55a35692010-02-11 23:25:211497
[email protected]c3cfb012011-04-06 22:07:351498void ExtensionService::SetIsIncognitoEnabled(
1499 const std::string& extension_id, bool enabled) {
[email protected]81b14cc2011-04-29 00:39:371500 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]c3cfb012011-04-06 22:07:351501 if (extension && extension->location() == Extension::COMPONENT) {
[email protected]b873cd92012-02-09 21:51:481502 // This shouldn't be called for component extensions unless they are
1503 // syncable.
1504 DCHECK(extension->IsSyncable());
1505
1506 // If we are here, make sure the we aren't trying to change the value.
1507 DCHECK_EQ(enabled, IsIncognitoEnabled(extension_id));
1508
[email protected]c3cfb012011-04-06 22:07:351509 return;
1510 }
1511
[email protected]8c6c8c22011-03-09 12:52:341512 // Broadcast unloaded and loaded events to update browser state. Only bother
1513 // if the value changed and the extension is actually enabled, since there is
1514 // no UI otherwise.
[email protected]c3cfb012011-04-06 22:07:351515 bool old_enabled = extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]8c6c8c22011-03-09 12:52:341516 if (enabled == old_enabled)
1517 return;
1518
[email protected]c3cfb012011-04-06 22:07:351519 extension_prefs_->SetIsIncognitoEnabled(extension_id, enabled);
[email protected]81b14cc2011-04-29 00:39:371520
[email protected]ffbf3f12011-12-09 22:10:331521 bool extension_is_enabled = extensions_.Contains(extension_id);
[email protected]bd9892eb2011-11-11 19:41:341522
1523 // When we reload the extension the ID may be invalidated if we've passed it
1524 // by const ref everywhere. Make a copy to be safe.
1525 std::string id = extension_id;
[email protected]406b5a92011-11-08 11:58:261526 if (extension_is_enabled)
[email protected]ffbf3f12011-12-09 22:10:331527 ReloadExtension(id);
[email protected]3bdba0d2011-08-23 07:17:301528
[email protected]406b5a92011-11-08 11:58:261529 // Reloading the extension invalidates the |extension| pointer.
[email protected]bd9892eb2011-11-11 19:41:341530 extension = GetInstalledExtension(id);
[email protected]3bdba0d2011-08-23 07:17:301531 if (extension)
1532 SyncExtensionChangeIfNeeded(*extension);
[email protected]55a35692010-02-11 23:25:211533}
1534
[email protected]aa7599d2011-10-28 07:24:321535void ExtensionService::SetAppNotificationSetupDone(
1536 const std::string& extension_id,
[email protected]b2689a902011-12-01 00:41:091537 const std::string& oauth_client_id) {
[email protected]aa7599d2011-10-28 07:24:321538 const Extension* extension = GetInstalledExtension(extension_id);
1539 // This method is called when the user sets up app notifications.
1540 // So it is not expected to be called until the extension is installed.
1541 if (!extension) {
1542 NOTREACHED();
1543 return;
1544 }
[email protected]b2689a902011-12-01 00:41:091545 extension_prefs_->SetAppNotificationClientId(extension_id, oauth_client_id);
[email protected]aa7599d2011-10-28 07:24:321546 SyncExtensionChangeIfNeeded(*extension);
1547}
1548
1549void ExtensionService::SetAppNotificationDisabled(
1550 const std::string& extension_id,
1551 bool value) {
1552 const Extension* extension = GetInstalledExtension(extension_id);
1553 // This method is called when the user enables/disables app notifications.
1554 // So it is not expected to be called until the extension is installed.
1555 if (!extension) {
1556 NOTREACHED();
1557 return;
1558 }
[email protected]97e856b12011-12-14 00:21:131559 if (value)
1560 UMA_HISTOGRAM_COUNTS("Apps.SetAppNotificationsDisabled", 1);
1561 else
1562 UMA_HISTOGRAM_COUNTS("Apps.SetAppNotificationsEnabled", 1);
[email protected]aa7599d2011-10-28 07:24:321563 extension_prefs_->SetAppNotificationDisabled(extension_id, value);
1564 SyncExtensionChangeIfNeeded(*extension);
1565}
1566
[email protected]eaa7dd182010-12-14 11:09:001567bool ExtensionService::CanCrossIncognito(const Extension* extension) {
[email protected]2a8f24e2010-11-03 21:37:051568 // We allow the extension to see events and data from another profile iff it
1569 // uses "spanning" behavior and it has incognito access. "split" mode
1570 // extensions only see events for a matching profile.
[email protected]0c41298f2011-09-13 21:17:221571 CHECK(extension);
[email protected]c3cfb012011-04-06 22:07:351572 return IsIncognitoEnabled(extension->id()) &&
1573 !extension->incognito_split_mode();
[email protected]2a8f24e2010-11-03 21:37:051574}
1575
[email protected]5ef7b002011-05-12 23:16:201576bool ExtensionService::CanLoadInIncognito(const Extension* extension) const {
1577 if (extension->is_hosted_app())
1578 return true;
1579 // Packaged apps and regular extensions need to be enabled specifically for
1580 // incognito (and split mode should be set).
1581 return extension->incognito_split_mode() &&
1582 IsIncognitoEnabled(extension->id());
1583}
1584
[email protected]168389f2011-12-20 17:12:481585void ExtensionService::OnExtensionMoved(
1586 const std::string& moved_extension_id,
1587 const std::string& predecessor_extension_id,
1588 const std::string& successor_extension_id) {
[email protected]3569b502012-01-12 20:08:231589 extension_prefs_->extension_sorting()->OnExtensionMoved(
1590 moved_extension_id,
1591 predecessor_extension_id,
1592 successor_extension_id);
[email protected]168389f2011-12-20 17:12:481593
1594 const Extension* extension = GetInstalledExtension(moved_extension_id);
1595 if (extension)
1596 SyncExtensionChangeIfNeeded(*extension);
1597}
1598
[email protected]eaa7dd182010-12-14 11:09:001599bool ExtensionService::AllowFileAccess(const Extension* extension) {
[email protected]05c82182010-06-24 17:49:081600 return (CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]334e04a2010-06-24 23:34:441601 switches::kDisableExtensionsFileAccessCheck) ||
[email protected]05c82182010-06-24 17:49:081602 extension_prefs_->AllowFileAccess(extension->id()));
1603}
1604
[email protected]eaa7dd182010-12-14 11:09:001605void ExtensionService::SetAllowFileAccess(const Extension* extension,
[email protected]3aff9ad2011-04-01 20:26:481606 bool allow) {
1607 // Reload to update browser state. Only bother if the value changed and the
1608 // extension is actually enabled, since there is no UI otherwise.
1609 bool old_allow = AllowFileAccess(extension);
1610 if (allow == old_allow)
1611 return;
1612
[email protected]05c82182010-06-24 17:49:081613 extension_prefs_->SetAllowFileAccess(extension->id(), allow);
[email protected]3aff9ad2011-04-01 20:26:481614
[email protected]84df8332011-12-06 18:22:461615 bool extension_is_enabled = extensions_.Contains(extension->id());
[email protected]3aff9ad2011-04-01 20:26:481616 if (extension_is_enabled)
1617 ReloadExtension(extension->id());
[email protected]05c82182010-06-24 17:49:081618}
1619
[email protected]8e4560b62011-01-14 10:09:141620// Some extensions will autoupdate themselves externally from Chrome. These
1621// are typically part of some larger client application package. To support
1622// these, the extension will register its location in the the preferences file
1623// (and also, on Windows, in the registry) and this code will periodically
1624// check that location for a .crx file, which it will then install locally if
1625// a new version is available.
1626// Errors are reported through ExtensionErrorReporter. Succcess is not
1627// reported.
[email protected]eaa7dd182010-12-14 11:09:001628void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:121629 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]8e4560b62011-01-14 10:09:141630
1631 // Note that this installation is intentionally silent (since it didn't
1632 // go through the front-end). Extensions that are registered in this
1633 // way are effectively considered 'pre-bundled', and so implicitly
1634 // trusted. In general, if something has HKLM or filesystem access,
1635 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:121636
[email protected]8e4560b62011-01-14 10:09:141637 // Ask each external extension provider to give us a call back for each
1638 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
[email protected]5df038b2012-07-16 19:03:271639 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141640 for (i = external_extension_providers_.begin();
1641 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:271642 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:141643 provider->VisitRegisteredExtension();
1644 }
1645
[email protected]50067e52011-10-20 23:17:071646 // Do any required work that we would have done after completion of all
1647 // providers.
1648 if (external_extension_providers_.empty()) {
1649 OnAllExternalProvidersReady();
1650 }
[email protected]9f1087e2009-06-15 17:29:321651}
1652
[email protected]50067e52011-10-20 23:17:071653void ExtensionService::OnExternalProviderReady(
[email protected]5df038b2012-07-16 19:03:271654 const extensions::ExternalProviderInterface* provider) {
[email protected]a29a517a2011-01-21 21:11:121655 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]50067e52011-10-20 23:17:071656 CHECK(provider->IsReady());
[email protected]a29a517a2011-01-21 21:11:121657
1658 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:141659 // if all of them are finished. So we check them first.
[email protected]94fde232012-04-27 10:22:301660 if (AreAllExternalProvidersReady())
1661 OnAllExternalProvidersReady();
1662}
1663
1664bool ExtensionService::AreAllExternalProvidersReady() const {
[email protected]5df038b2012-07-16 19:03:271665 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141666 for (i = external_extension_providers_.begin();
1667 i != external_extension_providers_.end(); ++i) {
[email protected]94fde232012-04-27 10:22:301668 if (!i->get()->IsReady())
1669 return false;
[email protected]8e4560b62011-01-14 10:09:141670 }
[email protected]94fde232012-04-27 10:22:301671 return true;
[email protected]50067e52011-10-20 23:17:071672}
1673
1674void ExtensionService::OnAllExternalProvidersReady() {
1675 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]94fde232012-04-27 10:22:301676 base::TimeDelta elapsed = base::Time::Now() - profile_->GetStartTime();
1677 UMA_HISTOGRAM_TIMES("Extension.ExternalProvidersReadyAfter", elapsed);
[email protected]50067e52011-10-20 23:17:071678
1679 // Install any pending extensions.
[email protected]94fde232012-04-27 10:22:301680 if (update_once_all_providers_are_ready_ && updater()) {
1681 update_once_all_providers_are_ready_ = false;
[email protected]8e4560b62011-01-14 10:09:141682 updater()->CheckNow();
1683 }
1684
1685 // Uninstall all the unclaimed extensions.
[email protected]45759612012-07-10 17:21:231686 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> extensions_info(
[email protected]8e4560b62011-01-14 10:09:141687 extension_prefs_->GetInstalledExtensionsInfo());
1688 for (size_t i = 0; i < extensions_info->size(); ++i) {
1689 ExtensionInfo* info = extensions_info->at(i).get();
1690 if (Extension::IsExternalLocation(info->extension_location))
1691 CheckExternalUninstall(info->extension_id);
1692 }
[email protected]e5af875f2011-10-10 21:09:141693 IdentifyAlertableExtensions();
[email protected]e5af875f2011-10-10 21:09:141694}
1695
1696void ExtensionService::IdentifyAlertableExtensions() {
1697 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1698
[email protected]07c9f2f42012-02-29 18:45:221699 // Build up the lists of extensions that require acknowledgment. If this is
1700 // the first time, grandfather extensions that would have caused
1701 // notification.
[email protected]89226982012-07-16 20:09:181702 extension_error_ui_.reset(ExtensionErrorUI::Create(this));
[email protected]e5af875f2011-10-10 21:09:141703
[email protected]a9aa5932012-01-25 08:27:401704 bool did_show_alert = false;
[email protected]89226982012-07-16 20:09:181705 if (PopulateExtensionErrorUI(extension_error_ui_.get())) {
[email protected]e5af875f2011-10-10 21:09:141706 if (extension_prefs_->SetAlertSystemFirstRun()) {
[email protected]a9aa5932012-01-25 08:27:401707 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]89226982012-07-16 20:09:181708 did_show_alert = extension_error_ui_->ShowErrorInBubbleView();
[email protected]e5af875f2011-10-10 21:09:141709 } else {
1710 // First run. Just acknowledge all the extensions, silently, by
1711 // shortcutting the display of the UI and going straight to the
1712 // callback for pressing the Accept button.
[email protected]a9aa5932012-01-25 08:27:401713 HandleExtensionAlertAccept();
[email protected]e5af875f2011-10-10 21:09:141714 }
1715 }
[email protected]a9aa5932012-01-25 08:27:401716
1717 if (!did_show_alert)
[email protected]89226982012-07-16 20:09:181718 extension_error_ui_.reset();
[email protected]e5af875f2011-10-10 21:09:141719}
1720
[email protected]89226982012-07-16 20:09:181721bool ExtensionService::PopulateExtensionErrorUI(
1722 ExtensionErrorUI* extension_error_ui) {
[email protected]07c9f2f42012-02-29 18:45:221723 bool needs_alert = false;
1724 for (ExtensionSet::const_iterator iter = extensions_.begin();
1725 iter != extensions_.end(); ++iter) {
1726 const Extension* e = *iter;
1727 if (Extension::IsExternalLocation(e->location())) {
1728 if (!e->is_hosted_app()) {
1729 if (!extension_prefs_->IsExternalExtensionAcknowledged(e->id())) {
[email protected]89226982012-07-16 20:09:181730 extension_error_ui->AddExternalExtension(e->id());
[email protected]07c9f2f42012-02-29 18:45:221731 needs_alert = true;
1732 }
1733 }
1734 }
1735 if (extension_prefs_->IsExtensionBlacklisted(e->id())) {
1736 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(e->id())) {
[email protected]89226982012-07-16 20:09:181737 extension_error_ui->AddBlacklistedExtension(e->id());
[email protected]07c9f2f42012-02-29 18:45:221738 needs_alert = true;
1739 }
1740 }
1741 if (extension_prefs_->IsExtensionOrphaned(e->id())) {
1742 if (!extension_prefs_->IsOrphanedExtensionAcknowledged(e->id())) {
[email protected]89226982012-07-16 20:09:181743 extension_error_ui->AddOrphanedExtension(e->id());
[email protected]07c9f2f42012-02-29 18:45:221744 needs_alert = true;
1745 }
1746 }
1747 }
1748 return needs_alert;
1749}
1750
[email protected]a9aa5932012-01-25 08:27:401751void ExtensionService::HandleExtensionAlertClosed() {
[email protected]89226982012-07-16 20:09:181752 extension_error_ui_.reset();
[email protected]62433d32011-10-12 22:33:121753}
1754
[email protected]a9aa5932012-01-25 08:27:401755void ExtensionService::HandleExtensionAlertAccept() {
[email protected]e5af875f2011-10-10 21:09:141756 const ExtensionIdSet *extension_ids =
[email protected]89226982012-07-16 20:09:181757 extension_error_ui_->get_external_extension_ids();
[email protected]e5af875f2011-10-10 21:09:141758 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1759 iter != extension_ids->end(); ++iter) {
[email protected]47fc70c2011-12-06 07:29:511760 AcknowledgeExternalExtension(*iter);
[email protected]e5af875f2011-10-10 21:09:141761 }
[email protected]89226982012-07-16 20:09:181762 extension_ids = extension_error_ui_->get_blacklisted_extension_ids();
[email protected]e5af875f2011-10-10 21:09:141763 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1764 iter != extension_ids->end(); ++iter) {
1765 extension_prefs_->AcknowledgeBlacklistedExtension(*iter);
1766 }
[email protected]89226982012-07-16 20:09:181767 extension_ids = extension_error_ui_->get_orphaned_extension_ids();
[email protected]e5af875f2011-10-10 21:09:141768 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1769 iter != extension_ids->end(); ++iter) {
1770 extension_prefs_->AcknowledgeOrphanedExtension(*iter);
1771 }
1772}
1773
[email protected]47fc70c2011-12-06 07:29:511774void ExtensionService::AcknowledgeExternalExtension(const std::string& id) {
1775 extension_prefs_->AcknowledgeExternalExtension(id);
1776}
1777
[email protected]89226982012-07-16 20:09:181778void ExtensionService::HandleExtensionAlertDetails() {
1779 extension_error_ui_->ShowExtensions();
[email protected]6c751e72010-11-23 10:11:101780}
1781
[email protected]a9f39a312010-12-23 22:14:271782void ExtensionService::UnloadExtension(
1783 const std::string& extension_id,
[email protected]814a7bf0f2011-08-13 05:30:591784 extension_misc::UnloadedExtensionReason reason) {
[email protected]27e469a2010-01-11 20:35:091785 // Make sure the extension gets deleted after we return from this function.
[email protected]9adb9692010-10-29 23:14:021786 scoped_refptr<const Extension> extension(
[email protected]8001df22011-04-28 19:59:471787 GetExtensionByIdInternal(extension_id, true, true, false));
[email protected]631cf822009-05-15 07:01:251788
[email protected]fa0c96732010-11-17 00:14:231789 // This method can be called via PostTask, so the extension may have been
1790 // unloaded by the time this runs.
[email protected]dd163fb02011-05-04 22:22:171791 if (!extension) {
1792 // In case the extension may have crashed/uninstalled. Allow the profile to
1793 // clean up its RequestContexts.
[email protected]31d8f5f22012-04-02 15:22:081794 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]fa0c96732010-11-17 00:14:231795 return;
[email protected]dd163fb02011-05-04 22:22:171796 }
[email protected]0c6da502009-08-14 22:32:391797
[email protected]1eb175082010-02-10 09:26:161798 // Keep information about the extension so that we can reload it later
1799 // even if it's not permanently installed.
1800 unloaded_extension_paths_[extension->id()] = extension->path();
1801
[email protected]f17dbd42010-08-16 23:21:101802 // Clean up if the extension is meant to be enabled after a reload.
1803 disabled_extension_paths_.erase(extension->id());
1804
[email protected]d7e9a862010-11-03 21:57:491805 // Clean up runtime data.
1806 extension_runtime_data_.erase(extension_id);
1807
[email protected]060d4972012-07-19 17:22:391808 if (disabled_extensions_.Contains(extension->id())) {
[email protected]a9f39a312010-12-23 22:14:271809 UnloadedExtensionInfo details(extension, reason);
1810 details.already_disabled = true;
[email protected]84df8332011-12-06 18:22:461811 disabled_extensions_.Remove(extension->id());
[email protected]ad50def52011-10-19 23:17:071812 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271813 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]6c2381d2011-10-19 02:52:531814 content::Source<Profile>(profile_),
1815 content::Details<UnloadedExtensionInfo>(&details));
[email protected]dd163fb02011-05-04 22:22:171816 // Make sure the profile cleans up its RequestContexts when an already
1817 // disabled extension is unloaded (since they are also tracking the disabled
1818 // extensions).
[email protected]31d8f5f22012-04-02 15:22:081819 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]0c6da502009-08-14 22:32:391820 return;
1821 }
1822
[email protected]060d4972012-07-19 17:22:391823 // Remove the extension from our list.
[email protected]84df8332011-12-06 18:22:461824 extensions_.Remove(extension->id());
[email protected]631cf822009-05-15 07:01:251825
[email protected]a9f39a312010-12-23 22:14:271826 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]631cf822009-05-15 07:01:251827}
1828
[email protected]eaa7dd182010-12-14 11:09:001829void ExtensionService::UnloadAllExtensions() {
[email protected]c8d407e2011-04-28 21:27:171830 profile_->GetExtensionSpecialStoragePolicy()->
1831 RevokeRightsForAllExtensions();
1832
[email protected]84df8332011-12-06 18:22:461833 extensions_.Clear();
1834 disabled_extensions_.Clear();
1835 terminated_extensions_.Clear();
[email protected]d7e9a862010-11-03 21:57:491836 extension_runtime_data_.clear();
[email protected]cd500f72010-06-25 23:44:321837
[email protected]c6e4a3412009-06-24 15:45:291838 // TODO(erikkay) should there be a notification for this? We can't use
1839 // EXTENSION_UNLOADED since that implies that the extension has been disabled
1840 // or uninstalled, and UnloadAll is just part of shutdown.
[email protected]9f1087e2009-06-15 17:29:321841}
1842
[email protected]eaa7dd182010-12-14 11:09:001843void ExtensionService::ReloadExtensions() {
[email protected]9f1087e2009-06-15 17:29:321844 UnloadAllExtensions();
[email protected]d8c8f25f2011-11-02 18:18:011845 component_loader_->LoadAll();
1846 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]9f1087e2009-06-15 17:29:321847}
1848
[email protected]eaa7dd182010-12-14 11:09:001849void ExtensionService::GarbageCollectExtensions() {
[email protected]2f8757c32012-06-19 19:17:471850 if (extension_prefs_->pref_service()->ReadOnly())
1851 return;
1852
[email protected]45759612012-07-10 17:21:231853 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> info(
[email protected]2f8757c32012-06-19 19:17:471854 extension_prefs_->GetInstalledExtensionsInfo());
1855
1856 std::map<std::string, FilePath> extension_paths;
1857 for (size_t i = 0; i < info->size(); ++i)
1858 extension_paths[info->at(i)->extension_id] = info->at(i)->extension_path;
1859
1860 if (!BrowserThread::PostTask(
1861 BrowserThread::FILE, FROM_HERE,
1862 base::Bind(
1863 &extension_file_util::GarbageCollectExtensions,
1864 install_directory_,
1865 extension_paths)))
1866 NOTREACHED();
[email protected]f6ccd652010-11-17 00:48:341867
[email protected]f766a9d2012-07-11 01:57:441868#if defined(ENABLE_THEMES)
[email protected]f6ccd652010-11-17 00:48:341869 // Also garbage-collect themes. We check |profile_| to be
1870 // defensive; in the future, we may call GarbageCollectExtensions()
1871 // from somewhere other than Init() (e.g., in a timer).
1872 if (profile_) {
[email protected]18280372011-03-22 18:05:221873 ThemeServiceFactory::GetForProfile(profile_)->RemoveUnusedThemes();
[email protected]f6ccd652010-11-17 00:48:341874 }
[email protected]f766a9d2012-07-11 01:57:441875#endif
[email protected]3cf4f0992009-02-03 23:00:301876}
1877
[email protected]fb82dcd2012-03-21 14:15:461878void ExtensionService::SyncExtensionChangeIfNeeded(const Extension& extension) {
[email protected]5db9ada2012-04-11 13:48:201879 if (app_sync_bundle_.HandlesApp(extension)) {
1880 app_sync_bundle_.SyncChangeIfNeeded(extension);
1881 } else if (extension_sync_bundle_.HandlesExtension(extension)) {
1882 extension_sync_bundle_.SyncChangeIfNeeded(extension);
[email protected]fb82dcd2012-03-21 14:15:461883 }
1884}
1885
[email protected]eaa7dd182010-12-14 11:09:001886void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]93fd78f42009-07-10 16:43:171887 if (updater_.get()) {
1888 updater_->Start();
1889 }
[email protected]fa6a9102010-11-22 15:38:501890
1891 ready_ = true;
[email protected]ad50def52011-10-19 23:17:071892 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271893 chrome::NOTIFICATION_EXTENSIONS_READY,
[email protected]6c2381d2011-10-19 02:52:531894 content::Source<Profile>(profile_),
[email protected]ad50def52011-10-19 23:17:071895 content::NotificationService::NoDetails());
[email protected]e72e8eb82009-06-18 17:21:511896}
1897
[email protected]fcb58a862012-05-01 01:03:151898void ExtensionService::AddExtension(const Extension* extension) {
[email protected]ae09ca62009-08-21 19:46:461899 // Ensure extension is deleted unless we transfer ownership.
[email protected]9adb9692010-10-29 23:14:021900 scoped_refptr<const Extension> scoped_extension(extension);
[email protected]9f1087e2009-06-15 17:29:321901
[email protected]c8d407e2011-04-28 21:27:171902 // TODO(jstritar): We may be able to get rid of this branch by overriding the
1903 // default extension state to DISABLED when the --disable-extensions flag
1904 // is set (http://crbug.com/29067).
1905 if (!extensions_enabled() &&
1906 !extension->is_theme() &&
1907 extension->location() != Extension::COMPONENT &&
1908 !Extension::IsExternalLocation(extension->location()))
[email protected]fcb58a862012-05-01 01:03:151909 return;
[email protected]c8d407e2011-04-28 21:27:171910
1911 SetBeingUpgraded(extension, false);
1912
[email protected]1eb175082010-02-10 09:26:161913 // The extension is now loaded, remove its data from unloaded extension map.
1914 unloaded_extension_paths_.erase(extension->id());
1915
[email protected]bb7f40952011-01-13 00:21:201916 // If a terminated extension is loaded, remove it from the terminated list.
1917 UntrackTerminatedExtension(extension->id());
1918
[email protected]f17dbd42010-08-16 23:21:101919 // If the extension was disabled for a reload, then enable it.
1920 if (disabled_extension_paths_.erase(extension->id()) > 0)
1921 EnableExtension(extension->id());
1922
[email protected]d728e002010-12-08 04:46:231923 // Check if the extension's privileges have changed and disable the
1924 // extension if necessary.
[email protected]902fd7b2011-07-27 18:42:311925 InitializePermissions(extension);
[email protected]0c6da502009-08-14 22:32:391926
[email protected]ad83ca242011-07-29 01:32:251927 bool disabled = extension_prefs_->IsExtensionDisabled(extension->id());
[email protected]36429da2011-07-11 20:25:181928 if (disabled) {
[email protected]84df8332011-12-06 18:22:461929 disabled_extensions_.Insert(scoped_extension);
[email protected]3bdba0d2011-08-23 07:17:301930 SyncExtensionChangeIfNeeded(*extension);
[email protected]44d62b62012-04-11 00:06:031931 content::NotificationService::current()->Notify(
1932 chrome::NOTIFICATION_EXTENSION_UPDATE_DISABLED,
1933 content::Source<Profile>(profile_),
1934 content::Details<const Extension>(extension));
[email protected]62f051c2012-03-29 17:04:441935
[email protected]44d62b62012-04-11 00:06:031936 if (extension_prefs_->GetDisableReason(extension->id()) ==
1937 Extension::DISABLE_PERMISSIONS_INCREASE) {
[email protected]62f051c2012-03-29 17:04:441938 extensions::AddExtensionDisabledError(this, extension);
1939 }
[email protected]fcb58a862012-05-01 01:03:151940 return;
[email protected]e72e8eb82009-06-18 17:21:511941 }
[email protected]aab98a52009-12-02 03:22:351942
[email protected]b873cd92012-02-09 21:51:481943 // All apps that are displayed in the launcher are ordered by their ordinals
1944 // so we must ensure they have valid ordinals.
1945 if (extension->ShouldDisplayInLauncher())
1946 extension_prefs_->extension_sorting()->EnsureValidOrdinals(extension->id());
1947
[email protected]84df8332011-12-06 18:22:461948 extensions_.Insert(scoped_extension);
[email protected]3bdba0d2011-08-23 07:17:301949 SyncExtensionChangeIfNeeded(*extension);
[email protected]c8d407e2011-04-28 21:27:171950 NotifyExtensionLoaded(extension);
[email protected]aab98a52009-12-02 03:22:351951}
1952
[email protected]902fd7b2011-07-27 18:42:311953void ExtensionService::InitializePermissions(const Extension* extension) {
1954 // If the extension has used the optional permissions API, it will have a
1955 // custom set of active permissions defined in the extension prefs. Here,
1956 // we update the extension's active permissions based on the prefs.
[email protected]c2e66e12012-06-27 06:27:061957 scoped_refptr<PermissionSet> active_permissions =
[email protected]902fd7b2011-07-27 18:42:311958 extension_prefs()->GetActivePermissions(extension->id());
1959
1960 if (active_permissions.get()) {
1961 // We restrict the active permissions to be within the bounds defined in the
1962 // extension's manifest.
1963 // a) active permissions must be a subset of optional + default permissions
1964 // b) active permissions must contains all default permissions
[email protected]c2e66e12012-06-27 06:27:061965 scoped_refptr<PermissionSet> total_permissions =
1966 PermissionSet::CreateUnion(
[email protected]902fd7b2011-07-27 18:42:311967 extension->required_permission_set(),
1968 extension->optional_permission_set());
1969
1970 // Make sure the active permissions contain no more than optional + default.
[email protected]c2e66e12012-06-27 06:27:061971 scoped_refptr<PermissionSet> adjusted_active =
1972 PermissionSet::CreateIntersection(
[email protected]902fd7b2011-07-27 18:42:311973 total_permissions.get(), active_permissions.get());
1974
1975 // Make sure the active permissions contain the default permissions.
[email protected]c2e66e12012-06-27 06:27:061976 adjusted_active = PermissionSet::CreateUnion(
[email protected]902fd7b2011-07-27 18:42:311977 extension->required_permission_set(), adjusted_active.get());
1978
[email protected]c333e792012-01-06 16:57:391979 extensions::PermissionsUpdater perms_updater(profile());
1980 perms_updater.UpdateActivePermissions(extension, adjusted_active);
[email protected]902fd7b2011-07-27 18:42:311981 }
1982
[email protected]8d888c12010-11-30 00:00:251983 // We keep track of all permissions the user has granted each extension.
1984 // This allows extensions to gracefully support backwards compatibility
1985 // by including unknown permissions in their manifests. When the user
1986 // installs the extension, only the recognized permissions are recorded.
1987 // When the unknown permissions become recognized (e.g., through browser
1988 // upgrade), we can prompt the user to accept these new permissions.
1989 // Extensions can also silently upgrade to less permissions, and then
1990 // silently upgrade to a version that adds these permissions back.
1991 //
1992 // For example, pretend that Chrome 10 includes a permission "omnibox"
1993 // for an API that adds suggestions to the omnibox. An extension can
1994 // maintain backwards compatibility while still having "omnibox" in the
1995 // manifest. If a user installs the extension on Chrome 9, the browser
1996 // will record the permissions it recognized, not including "omnibox."
1997 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
1998 // will disable the extension and prompt the user to approve the increase
1999 // in privileges. The extension could then release a new version that
2000 // removes the "omnibox" permission. When the user upgrades, Chrome will
2001 // still remember that "omnibox" had been granted, so that if the
2002 // extension once again includes "omnibox" in an upgrade, the extension
2003 // can upgrade without requiring this user's approval.
2004 const Extension* old = GetExtensionByIdInternal(extension->id(),
[email protected]8001df22011-04-28 19:59:472005 true, true, false);
[email protected]8d888c12010-11-30 00:00:252006 bool is_extension_upgrade = old != NULL;
2007 bool is_privilege_increase = false;
[email protected]44d62b62012-04-11 00:06:032008 bool previously_disabled = false;
2009 Extension::DisableReason disable_reason =
2010 extension_prefs_->GetDisableReason(extension->id());
[email protected]8d888c12010-11-30 00:00:252011
[email protected]902fd7b2011-07-27 18:42:312012 // We only need to compare the granted permissions to the current permissions
2013 // if the extension is not allowed to silently increase its permissions.
2014 if (!extension->CanSilentlyIncreasePermissions()) {
[email protected]8d888c12010-11-30 00:00:252015 // Add all the recognized permissions if the granted permissions list
2016 // hasn't been initialized yet.
[email protected]c2e66e12012-06-27 06:27:062017 scoped_refptr<PermissionSet> granted_permissions =
[email protected]902fd7b2011-07-27 18:42:312018 extension_prefs_->GetGrantedPermissions(extension->id());
[email protected]0d3e4a22011-06-23 19:02:522019 CHECK(granted_permissions.get());
[email protected]8d888c12010-11-30 00:00:252020
2021 // Here, we check if an extension's privileges have increased in a manner
2022 // that requires the user's approval. This could occur because the browser
2023 // upgraded and recognized additional privileges, or an extension upgrades
2024 // to a version that requires additional privileges.
[email protected]0d3e4a22011-06-23 19:02:522025 is_privilege_increase =
[email protected]902fd7b2011-07-27 18:42:312026 granted_permissions->HasLessPrivilegesThan(
2027 extension->GetActivePermissions());
[email protected]8d888c12010-11-30 00:00:252028 }
2029
2030 if (is_extension_upgrade) {
[email protected]66e26872010-12-03 20:07:252031 // Other than for unpacked extensions, CrxInstaller should have guaranteed
2032 // that we aren't downgrading.
2033 if (extension->location() != Extension::LOAD)
[email protected]2f8757c32012-06-19 19:17:472034 CHECK(extension->version()->CompareTo(*(old->version())) >= 0);
[email protected]8d888c12010-11-30 00:00:252035
2036 // Extensions get upgraded if the privileges are allowed to increase or
2037 // the privileges haven't increased.
2038 if (!is_privilege_increase) {
2039 SetBeingUpgraded(old, true);
2040 SetBeingUpgraded(extension, true);
2041 }
2042
[email protected]44d62b62012-04-11 00:06:032043 // If the extension was already disabled, suppress any alerts for becoming
2044 // disabled on permissions increase.
2045 previously_disabled = extension_prefs_->IsExtensionDisabled(old->id());
2046 if (previously_disabled) {
2047 Extension::DisableReason reason = extension_prefs_->GetDisableReason(
2048 old->id());
2049 if (reason == Extension::DISABLE_UNKNOWN) {
2050 // Initialize the reason for legacy disabled extensions from whether the
2051 // extension already exceeded granted permissions.
2052 if (extension_prefs_->DidExtensionEscalatePermissions(old->id()))
2053 disable_reason = Extension::DISABLE_PERMISSIONS_INCREASE;
2054 else
2055 disable_reason = Extension::DISABLE_USER_ACTION;
2056 }
2057 } else {
2058 disable_reason = Extension::DISABLE_PERMISSIONS_INCREASE;
2059 }
2060
[email protected]8d888c12010-11-30 00:00:252061 // To upgrade an extension in place, unload the old one and
2062 // then load the new one.
[email protected]814a7bf0f2011-08-13 05:30:592063 UnloadExtension(old->id(), extension_misc::UNLOAD_REASON_UPDATE);
[email protected]8d888c12010-11-30 00:00:252064 old = NULL;
2065 }
2066
2067 // Extension has changed permissions significantly. Disable it. A
2068 // notification should be sent by the caller.
2069 if (is_privilege_increase) {
[email protected]fe2dd7742011-04-19 22:52:492070 if (!extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
2071 RecordPermissionMessagesHistogram(
2072 extension, "Extensions.Permissions_AutoDisable");
2073 }
[email protected]b3317ad2011-04-28 23:46:002074 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
[email protected]8d888c12010-11-30 00:00:252075 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
[email protected]44d62b62012-04-11 00:06:032076 extension_prefs_->SetDisableReason(extension->id(), disable_reason);
[email protected]8d888c12010-11-30 00:00:252077 }
2078}
2079
[email protected]eaa7dd182010-12-14 11:09:002080void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:392081 std::set<std::string> extension_ids;
[email protected]84df8332011-12-06 18:22:462082 for (ExtensionSet::const_iterator iter = extensions_.begin();
2083 iter != extensions_.end(); ++iter) {
2084 const Extension* extension = *iter;
2085 if (!extension->is_theme() && extension->location() != Extension::COMPONENT)
2086 extension_ids.insert(extension->id());
[email protected]aab98a52009-12-02 03:22:352087 }
2088
2089 child_process_logging::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:252090}
2091
[email protected]8266d662011-07-12 21:53:262092void ExtensionService::OnExtensionInstalled(
[email protected]36a5c4c2011-12-14 16:34:502093 const Extension* extension,
2094 bool from_webstore,
2095 const StringOrdinal& page_ordinal) {
[email protected]a29a517a2011-01-21 21:11:122096 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:202097
[email protected]4416c5a2010-06-26 01:28:572098 // Ensure extension is deleted unless we transfer ownership.
[email protected]9adb9692010-10-29 23:14:022099 scoped_refptr<const Extension> scoped_extension(extension);
[email protected]c3cfb012011-04-06 22:07:352100 const std::string& id = extension->id();
[email protected]8a87a5332011-08-11 17:54:592101 // Extensions installed by policy can't be disabled. So even if a previous
2102 // installation disabled the extension, make sure it is now enabled.
2103 bool initial_enable =
2104 !extension_prefs_->IsExtensionDisabled(id) ||
[email protected]65187152012-06-02 13:14:142105 system_->management_policy()->MustRemainEnabled(extension, NULL);
[email protected]51a3bf8b2012-06-08 22:53:062106 const PendingExtensionInfo* pending_extension_info = NULL;
2107 if ((pending_extension_info = pending_extension_manager()->GetById(id))) {
2108 if (!pending_extension_info->ShouldAllowInstall(*extension)) {
2109 pending_extension_manager()->Remove(id);
[email protected]8ef78fd2010-08-19 17:14:322110
[email protected]4416c5a2010-06-26 01:28:572111 LOG(WARNING)
[email protected]ae6cb3022011-04-04 20:11:542112 << "ShouldAllowInstall() returned false for "
[email protected]c3cfb012011-04-06 22:07:352113 << id << " of type " << extension->GetType()
[email protected]7fa19f82010-12-21 19:40:082114 << " and update URL " << extension->update_url().spec()
2115 << "; not installing";
[email protected]ae6cb3022011-04-04 20:11:542116
[email protected]ad50def52011-10-19 23:17:072117 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272118 chrome::NOTIFICATION_EXTENSION_INSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:532119 content::Source<Profile>(profile_),
2120 content::Details<const Extension>(extension));
[email protected]ae6cb3022011-04-04 20:11:542121
[email protected]4416c5a2010-06-26 01:28:572122 // Delete the extension directory since we're not going to
2123 // load it.
[email protected]14908b72011-04-20 06:54:362124 if (!BrowserThread::PostTask(
2125 BrowserThread::FILE, FROM_HERE,
[email protected]53612e82011-10-18 18:00:362126 base::Bind(&extension_file_util::DeleteFile,
2127 extension->path(), true)))
[email protected]14908b72011-04-20 06:54:362128 NOTREACHED();
[email protected]4416c5a2010-06-26 01:28:572129 return;
2130 }
[email protected]51a3bf8b2012-06-08 22:53:062131
2132 pending_extension_manager()->Remove(id);
[email protected]4416c5a2010-06-26 01:28:572133 } else {
[email protected]c3cfb012011-04-06 22:07:352134 // We explicitly want to re-enable an uninstalled external
2135 // extension; if we're here, that means the user is manually
2136 // installing the extension.
[email protected]6cc7dbae2011-04-29 21:18:332137 if (IsExternalExtensionUninstalled(id)) {
2138 initial_enable = true;
2139 }
[email protected]aa142702010-03-26 01:26:332140 }
2141
[email protected]07533022011-06-27 20:42:552142 // Do not record the install histograms for upgrades.
2143 if (!GetExtensionByIdInternal(extension->id(), true, true, false)) {
2144 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
2145 extension->GetType(), 100);
2146 RecordPermissionMessagesHistogram(
2147 extension, "Extensions.Permissions_Install");
2148 }
2149
[email protected]a39921b42012-02-28 03:42:542150 // Certain extension locations are specific enough that we can
2151 // auto-acknowledge any extension that came from one of them.
2152 if (extension->location() == Extension::EXTERNAL_POLICY_DOWNLOAD) {
2153 AcknowledgeExternalExtension(extension->id());
2154 }
2155
[email protected]4416c5a2010-06-26 01:28:572156 extension_prefs_->OnExtensionInstalled(
[email protected]8266d662011-07-12 21:53:262157 extension,
2158 initial_enable ? Extension::ENABLED : Extension::DISABLED,
[email protected]cc829cf2011-08-22 21:01:352159 from_webstore,
[email protected]36a5c4c2011-12-14 16:34:502160 page_ordinal);
[email protected]25b34332009-06-05 21:53:192161
[email protected]cdfca9702011-08-08 16:07:012162 // Unpacked extensions default to allowing file access, but if that has been
2163 // overridden, don't reset the value.
[email protected]b6e64fd2011-08-09 19:49:192164 if (Extension::ShouldAlwaysAllowFileAccess(extension->location()) &&
[email protected]cdfca9702011-08-08 16:07:012165 !extension_prefs_->HasAllowFileAccessSetting(id)) {
2166 extension_prefs_->SetAllowFileAccess(id, true);
2167 }
2168
[email protected]fcb58a862012-05-01 01:03:152169 content::NotificationService::current()->Notify(
2170 chrome::NOTIFICATION_EXTENSION_INSTALLED,
2171 content::Source<Profile>(profile_),
2172 content::Details<const Extension>(extension));
2173
[email protected]49cd1572011-02-08 21:38:452174 // Transfer ownership of |extension| to AddExtension.
[email protected]fcb58a862012-05-01 01:03:152175 AddExtension(scoped_extension);
[email protected]4a190632009-05-09 01:07:422176}
2177
[email protected]eaa7dd182010-12-14 11:09:002178const Extension* ExtensionService::GetExtensionByIdInternal(
[email protected]8001df22011-04-28 19:59:472179 const std::string& id, bool include_enabled, bool include_disabled,
2180 bool include_terminated) const {
[email protected]e957fe52009-06-23 16:51:052181 std::string lowercase_id = StringToLowerASCII(id);
[email protected]0c6da502009-08-14 22:32:392182 if (include_enabled) {
[email protected]84df8332011-12-06 18:22:462183 const Extension* extension = extensions_.GetByID(lowercase_id);
2184 if (extension)
2185 return extension;
[email protected]0c6da502009-08-14 22:32:392186 }
2187 if (include_disabled) {
[email protected]84df8332011-12-06 18:22:462188 const Extension* extension = disabled_extensions_.GetByID(lowercase_id);
2189 if (extension)
2190 return extension;
[email protected]ce5c4502009-05-06 16:46:112191 }
[email protected]8001df22011-04-28 19:59:472192 if (include_terminated) {
[email protected]84df8332011-12-06 18:22:462193 const Extension* extension = terminated_extensions_.GetByID(lowercase_id);
2194 if (extension)
2195 return extension;
[email protected]8001df22011-04-28 19:59:472196 }
[email protected]ce5c4502009-05-06 16:46:112197 return NULL;
2198}
2199
[email protected]bb7f40952011-01-13 00:21:202200void ExtensionService::TrackTerminatedExtension(const Extension* extension) {
[email protected]84df8332011-12-06 18:22:462201 if (!terminated_extensions_.Contains(extension->id()))
2202 terminated_extensions_.Insert(make_scoped_refptr(extension));
[email protected]fa2416f2011-05-03 08:41:202203
[email protected]b3f7fe22011-11-11 19:27:562204 UnloadExtension(extension->id(), extension_misc::UNLOAD_REASON_TERMINATE);
[email protected]bb7f40952011-01-13 00:21:202205}
2206
2207void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
[email protected]fa2416f2011-05-03 08:41:202208 std::string lowercase_id = StringToLowerASCII(id);
[email protected]84df8332011-12-06 18:22:462209 terminated_extensions_.Remove(lowercase_id);
[email protected]bb7f40952011-01-13 00:21:202210}
2211
[email protected]0dfe05c2011-02-23 23:03:362212const Extension* ExtensionService::GetTerminatedExtension(
[email protected]8001df22011-04-28 19:59:472213 const std::string& id) const {
2214 return GetExtensionByIdInternal(id, false, false, true);
2215}
2216
2217const Extension* ExtensionService::GetInstalledExtension(
2218 const std::string& id) const {
2219 return GetExtensionByIdInternal(id, true, true, true);
[email protected]0dfe05c2011-02-23 23:03:362220}
2221
[email protected]eaa7dd182010-12-14 11:09:002222bool ExtensionService::ExtensionBindingsAllowed(const GURL& url) {
[email protected]615d88f2011-12-13 01:47:442223 // Allow bindings for all packaged extensions and component hosted apps.
2224 const Extension* extension = extensions_.GetExtensionOrAppByURL(
2225 ExtensionURLInfo(url));
2226 return extension && (!extension->is_hosted_app() ||
2227 extension->location() == Extension::COMPONENT);
[email protected]6d2e60bd2010-06-03 22:37:392228}
2229
[email protected]eaa7dd182010-12-14 11:09:002230const SkBitmap& ExtensionService::GetOmniboxIcon(
[email protected]b671760b2010-07-15 21:13:472231 const std::string& extension_id) {
2232 return omnibox_icon_manager_.GetIcon(extension_id);
2233}
2234
[email protected]eaa7dd182010-12-14 11:09:002235const SkBitmap& ExtensionService::GetOmniboxPopupIcon(
[email protected]29d0d4ac2010-09-08 21:10:312236 const std::string& extension_id) {
2237 return omnibox_popup_icon_manager_.GetIcon(extension_id);
2238}
2239
[email protected]9060d8b02012-01-13 02:14:302240bool ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:322241 const std::string& id,
[email protected]8e4560b62011-01-14 10:09:142242 const Version* version,
[email protected]8ef78fd2010-08-19 17:14:322243 const FilePath& path,
[email protected]1bf73cc32011-10-26 22:38:312244 Extension::Location location,
[email protected]47fc70c2011-12-06 07:29:512245 int creation_flags,
2246 bool mark_acknowledged) {
[email protected]ab22ba42011-01-14 16:36:382247 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2248 CHECK(Extension::IdIsValid(id));
[email protected]79c833b52011-04-05 18:31:012249 if (extension_prefs_->IsExternalExtensionUninstalled(id))
[email protected]9060d8b02012-01-13 02:14:302250 return false;
[email protected]a8af9fdb2010-10-28 21:52:202251
[email protected]8e4560b62011-01-14 10:09:142252 DCHECK(version);
2253
[email protected]7577a5c52009-07-30 06:21:582254 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:492255 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:582256 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:022257 const Extension* existing = GetExtensionById(id, true);
[email protected]7577a5c52009-07-30 06:21:582258 if (existing) {
[email protected]8e4560b62011-01-14 10:09:142259 switch (existing->version()->CompareTo(*version)) {
[email protected]7577a5c52009-07-30 06:21:582260 case -1: // existing version is older, we should upgrade
2261 break;
2262 case 0: // existing version is same, do nothing
[email protected]9060d8b02012-01-13 02:14:302263 return false;
[email protected]7577a5c52009-07-30 06:21:582264 case 1: // existing version is newer, uh-oh
2265 LOG(WARNING) << "Found external version of extension " << id
2266 << "that is older than current version. Current version "
2267 << "is: " << existing->VersionString() << ". New version "
[email protected]16d900042012-01-04 21:24:322268 << "is: " << version->GetString()
2269 << ". Keeping current version.";
[email protected]9060d8b02012-01-13 02:14:302270 return false;
[email protected]7577a5c52009-07-30 06:21:582271 }
2272 }
2273
[email protected]9060d8b02012-01-13 02:14:302274 // If the extension is already pending, don't start an install.
[email protected]e3987852012-05-04 10:06:302275 if (!pending_extension_manager()->AddFromExternalFile(
2276 id, location, *version)) {
[email protected]9060d8b02012-01-13 02:14:302277 return false;
[email protected]e3987852012-05-04 10:06:302278 }
[email protected]9c635f22010-12-02 09:36:362279
[email protected]14908b72011-04-20 06:54:362280 // no client (silent install)
[email protected]d8c8f25f2011-11-02 18:18:012281 scoped_refptr<CrxInstaller> installer(CrxInstaller::Create(this, NULL));
[email protected]6dfbbf82010-03-12 23:09:162282 installer->set_install_source(location);
2283 installer->set_expected_id(id);
[email protected]cb0e50312011-05-09 15:03:072284 installer->set_expected_version(*version);
2285 installer->set_install_cause(extension_misc::INSTALL_CAUSE_EXTERNAL_FILE);
[email protected]1bf73cc32011-10-26 22:38:312286 installer->set_creation_flags(creation_flags);
[email protected]6dfbbf82010-03-12 23:09:162287 installer->InstallCrx(path);
[email protected]47fc70c2011-12-06 07:29:512288
2289 // Depending on the source, a new external extension might not need a user
2290 // notification on installation. For such extensions, mark them acknowledged
2291 // now to suppress the notification.
2292 if (mark_acknowledged)
2293 AcknowledgeExternalExtension(id);
[email protected]9060d8b02012-01-13 02:14:302294
2295 return true;
[email protected]7577a5c52009-07-30 06:21:582296}
2297
[email protected]eaa7dd182010-12-14 11:09:002298void ExtensionService::ReportExtensionLoadError(
[email protected]d11c8e92009-10-20 23:26:402299 const FilePath& extension_path,
2300 const std::string &error,
[email protected]d11c8e92009-10-20 23:26:402301 bool be_noisy) {
[email protected]d8c8f25f2011-11-02 18:18:012302 content::NotificationService::current()->Notify(
2303 chrome::NOTIFICATION_EXTENSION_LOAD_ERROR,
2304 content::Source<Profile>(profile_),
2305 content::Details<const std::string>(&error));
[email protected]d11c8e92009-10-20 23:26:402306
[email protected]8a205c02011-02-04 20:41:332307 std::string path_str = UTF16ToUTF8(extension_path.LossyDisplayName());
[email protected]438afb12012-07-05 03:18:212308 string16 message = UTF8ToUTF16(base::StringPrintf(
[email protected]18d4b6c2010-09-21 03:21:042309 "Could not load extension from '%s'. %s",
[email protected]fc670822011-12-17 09:33:492310 path_str.c_str(), error.c_str()));
[email protected]d11c8e92009-10-20 23:26:402311 ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy);
2312}
2313
[email protected]eaa7dd182010-12-14 11:09:002314void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]406027c02010-09-27 08:03:182315 ExtensionHost* host) {
2316 OrphanedDevTools::iterator iter =
[email protected]2d2f6cfc2011-05-06 21:09:332317 orphaned_dev_tools_.find(host->extension_id());
[email protected]406027c02010-09-27 08:03:182318 if (iter == orphaned_dev_tools_.end())
2319 return;
2320
[email protected]0e12d7d2011-12-01 16:21:442321 DevToolsAgentHost* agent = DevToolsAgentHostRegistry::GetDevToolsAgentHost(
2322 host->render_view_host());
2323 content::DevToolsManager::GetInstance()->AttachClientHost(iter->second,
2324 agent);
[email protected]406027c02010-09-27 08:03:182325 orphaned_dev_tools_.erase(iter);
2326}
2327
[email protected]432115822011-07-10 15:52:272328void ExtensionService::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:532329 const content::NotificationSource& source,
2330 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:272331 switch (type) {
2332 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]6c2381d2011-10-19 02:52:532333 if (profile_ !=
2334 content::Source<Profile>(source).ptr()->GetOriginalProfile()) {
[email protected]bc535ee52010-08-31 18:40:322335 break;
[email protected]6c2381d2011-10-19 02:52:532336 }
[email protected]a4ed6282009-12-14 20:51:162337
[email protected]6c2381d2011-10-19 02:52:532338 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr();
[email protected]f128af42010-08-05 18:05:262339
[email protected]fa2416f2011-05-03 08:41:202340 // Mark the extension as terminated and Unload it. We want it to
2341 // be in a consistent state: either fully working or not loaded
2342 // at all, but never half-crashed. We do it in a PostTask so
2343 // that other handlers of this notification will still have
2344 // access to the Extension and ExtensionHost.
[email protected]14908b72011-04-20 06:54:362345 MessageLoop::current()->PostTask(
2346 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362347 base::Bind(
[email protected]fa2416f2011-05-03 08:41:202348 &ExtensionService::TrackTerminatedExtension,
[email protected]d8c8f25f2011-11-02 18:18:012349 AsWeakPtr(),
[email protected]fa2416f2011-05-03 08:41:202350 host->extension()));
[email protected]31f77262009-12-02 20:48:532351 break;
2352 }
[email protected]432115822011-07-10 15:52:272353 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: {
[email protected]f3b1a082011-11-18 00:34:302354 content::RenderProcessHost* process =
2355 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192356 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302357 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192358 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2359 break;
2360
[email protected]15397b42012-05-16 23:56:062361 // Extensions need to know the channel for API restrictions.
2362 process->Send(new ExtensionMsg_SetChannel(
[email protected]5243df072012-07-11 21:41:012363 extensions::Feature::GetCurrentChannel()));
[email protected]15397b42012-05-16 23:56:062364
[email protected]77a6970c2011-04-23 16:58:562365 // Valid extension function names, used to setup bindings in renderer.
2366 std::vector<std::string> function_names;
2367 ExtensionFunctionDispatcher::GetAllFunctionNames(&function_names);
2368 process->Send(new ExtensionMsg_SetFunctionNames(function_names));
[email protected]31f77262009-12-02 20:48:532369
[email protected]77a6970c2011-04-23 16:58:562370 // Scripting whitelist. This is modified by tests and must be communicated
2371 // to renderers.
2372 process->Send(new ExtensionMsg_SetScriptingWhitelist(
2373 *Extension::GetScriptingWhitelist()));
2374
2375 // Loaded extensions.
[email protected]9776e82e2011-11-15 02:17:532376 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions;
[email protected]84df8332011-12-06 18:22:462377 for (ExtensionSet::const_iterator iter = extensions_.begin();
2378 iter != extensions_.end(); ++iter) {
[email protected]19647262011-12-16 09:57:492379 // Renderers don't need to know about themes.
2380 if (!(*iter)->is_theme())
2381 loaded_extensions.push_back(ExtensionMsg_Loaded_Params(*iter));
[email protected]77a6970c2011-04-23 16:58:562382 }
[email protected]9776e82e2011-11-15 02:17:532383 process->Send(new ExtensionMsg_Loaded(loaded_extensions));
[email protected]77a6970c2011-04-23 16:58:562384 break;
2385 }
[email protected]432115822011-07-10 15:52:272386 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
[email protected]f3b1a082011-11-18 00:34:302387 content::RenderProcessHost* process =
2388 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192389 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302390 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192391 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2392 break;
2393
[email protected]f3b1a082011-11-18 00:34:302394 installed_app_hosts_.erase(process->GetID());
[email protected]6f371442011-11-09 06:45:462395
[email protected]6bc04fd82011-12-04 02:29:352396 process_map_.RemoveAllFromProcess(process->GetID());
[email protected]6f371442011-11-09 06:45:462397 BrowserThread::PostTask(
2398 BrowserThread::IO, FROM_HERE,
2399 base::Bind(&ExtensionInfoMap::UnregisterAllExtensionsInProcess,
[email protected]31d8f5f22012-04-02 15:22:082400 system_->info_map(),
[email protected]f3b1a082011-11-18 00:34:302401 process->GetID()));
[email protected]da5683db2011-04-23 17:12:212402 break;
2403 }
[email protected]432115822011-07-10 15:52:272404 case chrome::NOTIFICATION_PREF_CHANGED: {
[email protected]6c2381d2011-10-19 02:52:532405 std::string* pref_name = content::Details<std::string>(details).ptr();
[email protected]6c751e72010-11-23 10:11:102406 if (*pref_name == prefs::kExtensionInstallAllowList ||
2407 *pref_name == prefs::kExtensionInstallDenyList) {
2408 CheckAdminBlacklist();
[email protected]6c751e72010-11-23 10:11:102409 } else {
2410 NOTREACHED() << "Unexpected preference name.";
2411 }
[email protected]aa96d3a2010-08-21 08:45:252412 break;
2413 }
[email protected]d91268022011-08-26 13:17:372414 case chrome::NOTIFICATION_IMPORT_FINISHED: {
[email protected]cf593af2011-12-30 05:44:392415 InitAfterImport();
[email protected]d91268022011-08-26 13:17:372416 break;
2417 }
[email protected]aa96d3a2010-08-21 08:45:252418
[email protected]4814b512009-11-07 00:12:292419 default:
2420 NOTREACHED() << "Unexpected notification type.";
2421 }
2422}
2423
[email protected]eaa7dd182010-12-14 11:09:002424bool ExtensionService::HasApps() const {
[email protected]ec5b50d2010-10-09 16:35:182425 return !GetAppIds().empty();
2426}
[email protected]377011d2010-07-20 04:18:502427
[email protected]eaa7dd182010-12-14 11:09:002428ExtensionIdSet ExtensionService::GetAppIds() const {
[email protected]ec5b50d2010-10-09 16:35:182429 ExtensionIdSet result;
[email protected]84df8332011-12-06 18:22:462430 for (ExtensionSet::const_iterator it = extensions_.begin();
[email protected]377011d2010-07-20 04:18:502431 it != extensions_.end(); ++it) {
[email protected]ec5b50d2010-10-09 16:35:182432 if ((*it)->is_app() && (*it)->location() != Extension::COMPONENT)
2433 result.insert((*it)->id());
[email protected]377011d2010-07-20 04:18:502434 }
2435
[email protected]ec5b50d2010-10-09 16:35:182436 return result;
[email protected]377011d2010-07-20 04:18:502437}
[email protected]d7e9a862010-11-03 21:57:492438
[email protected]eaa7dd182010-12-14 11:09:002439bool ExtensionService::IsBackgroundPageReady(const Extension* extension) {
[email protected]4762e7902012-03-28 23:34:042440 return (!extension->has_persistent_background_page() ||
[email protected]d7e9a862010-11-03 21:57:492441 extension_runtime_data_[extension->id()].background_page_ready);
2442}
2443
[email protected]eaa7dd182010-12-14 11:09:002444void ExtensionService::SetBackgroundPageReady(const Extension* extension) {
[email protected]a03d4448f2012-01-10 23:25:282445 DCHECK(extension->has_background_page());
[email protected]d7e9a862010-11-03 21:57:492446 extension_runtime_data_[extension->id()].background_page_ready = true;
[email protected]ad50def52011-10-19 23:17:072447 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272448 chrome::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY,
[email protected]6c2381d2011-10-19 02:52:532449 content::Source<const Extension>(extension),
[email protected]ad50def52011-10-19 23:17:072450 content::NotificationService::NoDetails());
[email protected]d7e9a862010-11-03 21:57:492451}
2452
[email protected]eaa7dd182010-12-14 11:09:002453bool ExtensionService::IsBeingUpgraded(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:492454 return extension_runtime_data_[extension->id()].being_upgraded;
2455}
2456
[email protected]eaa7dd182010-12-14 11:09:002457void ExtensionService::SetBeingUpgraded(const Extension* extension,
[email protected]d7e9a862010-11-03 21:57:492458 bool value) {
2459 extension_runtime_data_[extension->id()].being_upgraded = value;
2460}
[email protected]1bead0712010-11-27 17:41:532461
[email protected]39a5b532011-10-22 01:47:072462bool ExtensionService::HasUsedWebRequest(const Extension* extension) {
2463 return extension_runtime_data_[extension->id()].has_used_webrequest;
2464}
2465
2466void ExtensionService::SetHasUsedWebRequest(const Extension* extension,
2467 bool value) {
2468 extension_runtime_data_[extension->id()].has_used_webrequest = value;
2469}
2470
[email protected]45644f62011-11-23 00:58:232471base::PropertyBag* ExtensionService::GetPropertyBag(
2472 const Extension* extension) {
[email protected]1bead0712010-11-27 17:41:532473 return &extension_runtime_data_[extension->id()].property_bag;
2474}
[email protected]cebc3dc2011-04-18 17:15:002475
2476void ExtensionService::RegisterNaClModule(const GURL& url,
2477 const std::string& mime_type) {
2478 NaClModuleInfo info;
2479 info.url = url;
2480 info.mime_type = mime_type;
2481
2482 DCHECK(FindNaClModule(url) == nacl_module_list_.end());
2483 nacl_module_list_.push_front(info);
2484}
2485
2486void ExtensionService::UnregisterNaClModule(const GURL& url) {
2487 NaClModuleInfoList::iterator iter = FindNaClModule(url);
2488 DCHECK(iter != nacl_module_list_.end());
2489 nacl_module_list_.erase(iter);
2490}
2491
[email protected]ed0ba002011-05-26 16:55:132492void ExtensionService::UpdatePluginListWithNaClModules() {
[email protected]300c0ea2011-07-15 23:04:332493 // An extension has been added which has a nacl_module component, which means
2494 // there is a MIME type that module wants to handle, so we need to add that
2495 // MIME type to plugins which handle NaCl modules in order to allow the
2496 // individual modules to handle these types.
[email protected]1e127022011-11-29 21:11:472497 FilePath path;
2498 if (!PathService::Get(chrome::FILE_NACL_PLUGIN, &path))
2499 return;
2500 const content::PepperPluginInfo* pepper_info =
2501 PluginService::GetInstance()->GetRegisteredPpapiPluginInfo(path);
2502 if (!pepper_info)
2503 return;
[email protected]300c0ea2011-07-15 23:04:332504
[email protected]1e127022011-11-29 21:11:472505 std::vector<webkit::WebPluginMimeType>::const_iterator mime_iter;
2506 // Check each MIME type the plugins handle for the NaCl MIME type.
2507 for (mime_iter = pepper_info->mime_types.begin();
2508 mime_iter != pepper_info->mime_types.end(); ++mime_iter) {
2509 if (mime_iter->mime_type == kNaClPluginMimeType) {
2510 // This plugin handles "application/x-nacl".
[email protected]300c0ea2011-07-15 23:04:332511
[email protected]1e127022011-11-29 21:11:472512 PluginService::GetInstance()->
2513 UnregisterInternalPlugin(pepper_info->path);
[email protected]300c0ea2011-07-15 23:04:332514
[email protected]1e127022011-11-29 21:11:472515 webkit::WebPluginInfo info = pepper_info->ToWebPluginInfo();
[email protected]300c0ea2011-07-15 23:04:332516
[email protected]1e127022011-11-29 21:11:472517 for (ExtensionService::NaClModuleInfoList::const_iterator iter =
2518 nacl_module_list_.begin();
2519 iter != nacl_module_list_.end(); ++iter) {
2520 // Add the MIME type specified in the extension to this NaCl plugin,
2521 // With an extra "nacl" argument to specify the location of the NaCl
2522 // manifest file.
2523 webkit::WebPluginMimeType mime_type_info;
2524 mime_type_info.mime_type = iter->mime_type;
2525 mime_type_info.additional_param_names.push_back(UTF8ToUTF16("nacl"));
2526 mime_type_info.additional_param_values.push_back(
2527 UTF8ToUTF16(iter->url.spec()));
2528 info.mime_types.push_back(mime_type_info);
[email protected]300c0ea2011-07-15 23:04:332529 }
[email protected]1e127022011-11-29 21:11:472530
2531 PluginService::GetInstance()->RefreshPlugins();
[email protected]c6f3dea2012-01-14 02:23:112532 PluginService::GetInstance()->RegisterInternalPlugin(info, true);
[email protected]1e127022011-11-29 21:11:472533 // This plugin has been modified, no need to check the rest of its
2534 // types, but continue checking other plugins.
2535 break;
[email protected]ed0ba002011-05-26 16:55:132536 }
2537 }
[email protected]ed0ba002011-05-26 16:55:132538}
2539
[email protected]cebc3dc2011-04-18 17:15:002540ExtensionService::NaClModuleInfoList::iterator
2541 ExtensionService::FindNaClModule(const GURL& url) {
2542 for (NaClModuleInfoList::iterator iter = nacl_module_list_.begin();
2543 iter != nacl_module_list_.end(); ++iter) {
2544 if (iter->url == url)
2545 return iter;
2546 }
2547 return nacl_module_list_.end();
2548}