blob: d2830b8750522d7e6f7ea408f0673ff3fa041a98 [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]58edca52012-02-16 21:51:3832#include "chrome/browser/extensions/api/api_resource_controller.h"
[email protected]92ba7b012012-05-16 03:36:1733#include "chrome/browser/extensions/api/cookies/cookies_api.h"
[email protected]895a1e52012-05-15 02:50:1234#include "chrome/browser/extensions/api/declarative/rules_registry_service.h"
35#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]d8c8f25f2011-11-02 18:18:0139#include "chrome/browser/extensions/component_loader.h"
[email protected]e0785902011-05-19 23:34:1740#include "chrome/browser/extensions/crx_installer.h"
[email protected]49098f702011-10-13 03:47:1841#include "chrome/browser/extensions/default_apps_trial.h"
[email protected]b68d5ed2009-04-16 02:41:2842#include "chrome/browser/extensions/extension_browser_event_router.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]bdd34452012-04-16 15:58:4046#include "chrome/browser/extensions/extension_font_settings_api.h"
[email protected]e5af875f2011-10-10 21:09:1447#include "chrome/browser/extensions/extension_global_error.h"
[email protected]b1748b1d82009-11-30 20:32:5648#include "chrome/browser/extensions/extension_host.h"
[email protected]8baf0dbb2011-11-27 22:47:0249#include "chrome/browser/extensions/extension_input_ime_api.h"
[email protected]8a4cf842012-05-16 13:35:3050#include "chrome/browser/extensions/extension_managed_mode_api.h"
[email protected]7596ce72010-08-30 05:10:4651#include "chrome/browser/extensions/extension_management_api.h"
[email protected]118de7892011-03-16 13:31:4052#include "chrome/browser/extensions/extension_preference_api.h"
[email protected]4814b512009-11-07 00:12:2953#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]8a661f82010-10-19 21:47:1154#include "chrome/browser/extensions/extension_processes_api.h"
[email protected]3569b502012-01-12 20:08:2355#include "chrome/browser/extensions/extension_sorting.h"
[email protected]19eb80152011-02-26 00:28:4356#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]90310d92011-04-17 07:35:0457#include "chrome/browser/extensions/extension_sync_data.h"
[email protected]31d8f5f22012-04-02 15:22:0858#include "chrome/browser/extensions/extension_system.h"
[email protected]8f9d4eb2011-02-05 01:39:1059#include "chrome/browser/extensions/extension_web_ui.h"
[email protected]8e4560b62011-01-14 10:09:1460#include "chrome/browser/extensions/external_extension_provider_impl.h"
[email protected]42b795652011-02-22 23:32:3861#include "chrome/browser/extensions/external_extension_provider_interface.h"
[email protected]d8c8f25f2011-11-02 18:18:0162#include "chrome/browser/extensions/installed_loader.h"
[email protected]b2907fd2011-03-25 16:43:3763#include "chrome/browser/extensions/pending_extension_manager.h"
[email protected]c333e792012-01-06 16:57:3964#include "chrome/browser/extensions/permissions_updater.h"
[email protected]cccdf0aa2011-11-11 03:43:3865#include "chrome/browser/extensions/settings/settings_frontend.h"
[email protected]d8c8f25f2011-11-02 18:18:0166#include "chrome/browser/extensions/unpacked_installer.h"
[email protected]42a08162012-03-16 18:09:1167#include "chrome/browser/extensions/updater/extension_updater.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]e5af875f2011-10-10 21:09:1477#include "chrome/browser/ui/browser.h"
[email protected]d8748142012-05-16 21:13:4378#include "chrome/browser/ui/browser_finder.h"
[email protected]5d9cace72012-06-21 16:07:1279#include "chrome/browser/ui/chrome_pages.h"
[email protected]e5af875f2011-10-10 21:09:1480#include "chrome/browser/ui/global_error_service.h"
81#include "chrome/browser/ui/global_error_service_factory.h"
[email protected]c8d407e2011-04-28 21:27:1782#include "chrome/browser/ui/webui/chrome_url_data_manager.h"
83#include "chrome/browser/ui/webui/favicon_source.h"
[email protected]5eddc3e2011-10-26 04:33:3184#include "chrome/browser/ui/webui/ntp/thumbnail_source.h"
[email protected]aab98a52009-12-02 03:22:3585#include "chrome/common/child_process_logging.h"
[email protected]432115822011-07-10 15:52:2786#include "chrome/common/chrome_notification_types.h"
[email protected]1e127022011-11-29 21:11:4787#include "chrome/common/chrome_paths.h"
[email protected]e2eb43112009-05-29 21:19:5488#include "chrome/common/chrome_switches.h"
[email protected]15397b42012-05-16 23:56:0689#include "chrome/common/chrome_version_info.h"
[email protected]5b1a0e22009-05-26 19:00:5890#include "chrome/common/extensions/extension.h"
[email protected]05c82182010-06-24 17:49:0891#include "chrome/common/extensions/extension_error_utils.h"
[email protected]7c927b62010-02-24 09:54:1392#include "chrome/common/extensions/extension_file_util.h"
[email protected]895a1e52012-05-15 02:50:1293#include "chrome/common/extensions/extension_manifest_constants.h"
[email protected]77a6970c2011-04-23 16:58:5694#include "chrome/common/extensions/extension_messages.h"
[email protected]a315ba92010-11-16 14:12:2195#include "chrome/common/extensions/extension_resource.h"
[email protected]25b34332009-06-05 21:53:1996#include "chrome/common/pref_names.h"
[email protected]a57209872009-05-04 22:53:1497#include "chrome/common/url_constants.h"
[email protected]c38831a12011-10-28 12:44:4998#include "content/public/browser/browser_thread.h"
[email protected]0e12d7d2011-12-01 16:21:4499#include "content/public/browser/devtools_agent_host_registry.h"
100#include "content/public/browser/devtools_manager.h"
[email protected]05aad2da2011-10-28 10:12:37101#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:16102#include "content/public/browser/notification_types.h"
[email protected]3a5180ae2011-12-21 02:39:38103#include "content/public/browser/plugin_service.h"
[email protected]f3b1a082011-11-18 00:34:30104#include "content/public/browser/render_process_host.h"
[email protected]1e127022011-11-29 21:11:47105#include "content/public/common/pepper_plugin_info.h"
[email protected]c10da4b02010-03-25 14:38:32106#include "googleurl/src/gurl.h"
[email protected]65187152012-06-02 13:14:14107#include "grit/generated_resources.h"
[email protected]1debbbb62010-10-06 17:23:44108#include "net/base/registry_controlled_domain.h"
[email protected]895a1e52012-05-15 02:50:12109#include "sync/api/sync_change.h"
110#include "sync/api/sync_error_factory.h"
[email protected]65187152012-06-02 13:14:14111#include "ui/base/l10n/l10n_util.h"
[email protected]24b538a2010-02-27 01:22:44112#include "webkit/database/database_tracker.h"
113#include "webkit/database/database_util.h"
[email protected]c64631652009-04-29 22:24:31114
[email protected]eed367e2011-04-12 03:43:31115#if defined(OS_CHROMEOS)
[email protected]14da67b02011-08-02 05:49:13116#include "chrome/browser/chromeos/cros/cros_library.h"
[email protected]f463bc92012-03-22 16:38:23117#include "chrome/browser/chromeos/extensions/bluetooth_event_router.h"
[email protected]eed367e2011-04-12 03:43:31118#include "chrome/browser/chromeos/extensions/file_browser_event_router.h"
[email protected]be5bd5742011-07-14 07:21:01119#include "chrome/browser/chromeos/extensions/input_method_event_router.h"
[email protected]ce9802042011-05-27 10:11:40120#include "chrome/browser/chromeos/extensions/media_player_event_router.h"
[email protected]14da67b02011-08-02 05:49:13121#include "chrome/browser/chromeos/input_method/input_method_manager.h"
[email protected]2f8757c32012-06-19 19:17:47122#include "chrome/browser/extensions/extension_input_ime_api.h"
[email protected]b777b332011-04-16 04:01:08123#include "webkit/fileapi/file_system_context.h"
124#include "webkit/fileapi/file_system_mount_point_provider.h"
[email protected]eed367e2011-04-12 03:43:31125#endif
126
[email protected]5ef47ec2010-01-28 05:58:05127using base::Time;
[email protected]55eb70e762012-02-20 17:38:39128using content::BrowserContext;
[email protected]631bb742011-11-02 11:29:39129using content::BrowserThread;
[email protected]0e12d7d2011-12-01 16:21:44130using content::DevToolsAgentHost;
131using content::DevToolsAgentHostRegistry;
[email protected]3a5180ae2011-12-21 02:39:38132using content::PluginService;
[email protected]1c321ee52012-05-21 03:02:34133using extensions::Extension;
134using extensions::ExtensionIdSet;
135using extensions::ExtensionInfo;
136using extensions::UnloadedExtensionInfo;
[email protected]c2e66e12012-06-27 06:27:06137using extensions::PermissionMessage;
138using extensions::PermissionMessages;
139using extensions::PermissionSet;
[email protected]5ef47ec2010-01-28 05:58:05140
[email protected]c6d474f82009-12-16 21:11:06141namespace errors = extension_manifest_errors;
142
[email protected]b6ab96d2009-08-20 18:58:19143namespace {
144
[email protected]29d0d4ac2010-09-08 21:10:31145#if defined(OS_LINUX)
146static const int kOmniboxIconPaddingLeft = 2;
147static const int kOmniboxIconPaddingRight = 2;
148#elif defined(OS_MACOSX)
149static const int kOmniboxIconPaddingLeft = 0;
150static const int kOmniboxIconPaddingRight = 2;
151#else
152static const int kOmniboxIconPaddingLeft = 0;
153static const int kOmniboxIconPaddingRight = 0;
154#endif
155
[email protected]300c0ea2011-07-15 23:04:33156const char* kNaClPluginMimeType = "application/x-nacl";
157
[email protected]3bdba0d2011-08-23 07:17:30158static bool IsSyncableExtension(const Extension& extension) {
159 return extension.GetSyncType() == Extension::SYNC_TYPE_EXTENSION;
160}
161
162static bool IsSyncableApp(const Extension& extension) {
163 return extension.GetSyncType() == Extension::SYNC_TYPE_APP;
164}
165
[email protected]c6d474f82009-12-16 21:11:06166} // namespace
[email protected]b6ab96d2009-08-20 18:58:19167
[email protected]eaa7dd182010-12-14 11:09:00168ExtensionService::ExtensionRuntimeData::ExtensionRuntimeData()
[email protected]d7e9a862010-11-03 21:57:49169 : background_page_ready(false),
[email protected]f8bf5992011-10-24 22:38:57170 being_upgraded(false),
171 has_used_webrequest(false) {
[email protected]d7e9a862010-11-03 21:57:49172}
173
[email protected]eaa7dd182010-12-14 11:09:00174ExtensionService::ExtensionRuntimeData::~ExtensionRuntimeData() {
[email protected]d7e9a862010-11-03 21:57:49175}
176
[email protected]cebc3dc2011-04-18 17:15:00177ExtensionService::NaClModuleInfo::NaClModuleInfo() {
178}
179
180ExtensionService::NaClModuleInfo::~NaClModuleInfo() {
181}
182
[email protected]eaa7dd182010-12-14 11:09:00183// ExtensionService.
[email protected]6014d672008-12-05 00:38:25184
[email protected]eaa7dd182010-12-14 11:09:00185const char* ExtensionService::kInstallDirectoryName = "Extensions";
[email protected]15d5b472011-11-24 04:17:24186
187const char* ExtensionService::kLocalAppSettingsDirectoryName =
188 "Local App Settings";
189const char* ExtensionService::kLocalExtensionSettingsDirectoryName =
190 "Local Extension Settings";
191const char* ExtensionService::kSyncAppSettingsDirectoryName =
192 "Sync App Settings";
193const char* ExtensionService::kSyncExtensionSettingsDirectoryName =
194 "Sync Extension Settings";
[email protected]90e800c2012-06-12 23:11:00195const char* ExtensionService::kStateStoreName = "Extension State";
[email protected]494c06e2009-07-25 01:06:42196
[email protected]8e4560b62011-01-14 10:09:14197void ExtensionService::CheckExternalUninstall(const std::string& id) {
[email protected]a29a517a2011-01-21 21:11:12198 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20199
[email protected]7a4c6852010-09-16 03:44:22200 // Check if the providers know about this extension.
[email protected]0a60a2e2010-10-25 16:15:21201 ProviderCollection::const_iterator i;
202 for (i = external_extension_providers_.begin();
203 i != external_extension_providers_.end(); ++i) {
[email protected]8e4560b62011-01-14 10:09:14204 DCHECK(i->get()->IsReady());
[email protected]0a60a2e2010-10-25 16:15:21205 if (i->get()->HasExtension(id))
206 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22207 }
208
[email protected]0f48fca2011-05-19 18:46:35209 // We get the list of external extensions to check from preferences.
210 // It is possible that an extension has preferences but is not loaded.
211 // For example, an extension that requires experimental permissions
212 // will not be loaded if the experimental command line flag is not used.
213 // In this case, do not uninstall.
214 const Extension* extension = GetInstalledExtension(id);
215 if (!extension) {
216 // We can't call UninstallExtension with an unloaded/invalid
217 // extension ID.
218 LOG(WARNING) << "Attempted uninstallation of unloaded/invalid extension "
219 << "with id: " << id;
220 return;
221 }
[email protected]d6ebc9792011-04-07 18:18:33222 UninstallExtension(id, true, NULL);
[email protected]7a4c6852010-09-16 03:44:22223}
224
[email protected]8e4560b62011-01-14 10:09:14225void ExtensionService::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22226 external_extension_providers_.clear();
227}
228
[email protected]8e4560b62011-01-14 10:09:14229void ExtensionService::AddProviderForTesting(
230 ExternalExtensionProviderInterface* test_provider) {
[email protected]a29a517a2011-01-21 21:11:12231 CHECK(test_provider);
[email protected]0a60a2e2010-10-25 16:15:21232 external_extension_providers_.push_back(
[email protected]8e4560b62011-01-14 10:09:14233 linked_ptr<ExternalExtensionProviderInterface>(test_provider));
[email protected]7a4c6852010-09-16 03:44:22234}
235
[email protected]9060d8b02012-01-13 02:14:30236bool ExtensionService::OnExternalExtensionUpdateUrlFound(
[email protected]7a4c6852010-09-16 03:44:22237 const std::string& id,
[email protected]21a5a672010-11-04 10:47:42238 const GURL& update_url,
239 Extension::Location location) {
[email protected]ab22ba42011-01-14 16:36:38240 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
241 CHECK(Extension::IdIsValid(id));
[email protected]a8af9fdb2010-10-28 21:52:20242
[email protected]8a87a5332011-08-11 17:54:59243 const Extension* extension = GetExtensionById(id, true);
244 if (extension) {
245 // Already installed. Skip this install if the current location has
246 // higher priority than |location|.
247 Extension::Location current = extension->location();
248 if (current == Extension::GetHigherPriorityLocation(current, location))
[email protected]9060d8b02012-01-13 02:14:30249 return false;
[email protected]8a87a5332011-08-11 17:54:59250 // Otherwise, overwrite the current installation.
[email protected]7a4c6852010-09-16 03:44:22251 }
[email protected]9060d8b02012-01-13 02:14:30252
253 // Add |id| to the set of pending extensions. If it can not be added,
254 // then there is already a pending record from a higher-priority install
255 // source. In this case, signal that this extension will not be
256 // installed by returning false.
257 if (!pending_extension_manager()->AddFromExternalUpdateUrl(
258 id, update_url, location))
259 return false;
260
[email protected]94fde232012-04-27 10:22:30261 update_once_all_providers_are_ready_ = true;
[email protected]9060d8b02012-01-13 02:14:30262 return true;
[email protected]7a4c6852010-09-16 03:44:22263}
264
[email protected]d9696672011-03-15 22:45:09265const Extension* ExtensionService::GetInstalledApp(const GURL& url) {
[email protected]615d88f2011-12-13 01:47:44266 const Extension* extension = extensions_.GetExtensionOrAppByURL(
267 ExtensionURLInfo(url));
268 if (extension && extension->is_app())
269 return extension;
[email protected]d9696672011-03-15 22:45:09270
271 return NULL;
272}
273
274bool ExtensionService::IsInstalledApp(const GURL& url) {
275 return !!GetInstalledApp(url);
[email protected]ffb204f22010-12-05 23:20:27276}
277
[email protected]da5683db2011-04-23 17:12:21278void ExtensionService::SetInstalledAppForRenderer(int renderer_child_id,
279 const Extension* app) {
280 installed_app_hosts_[renderer_child_id] = app;
281}
282
283const Extension* ExtensionService::GetInstalledAppForRenderer(
284 int renderer_child_id) {
285 InstalledAppMap::iterator i = installed_app_hosts_.find(renderer_child_id);
286 if (i == installed_app_hosts_.end())
287 return NULL;
288 return i->second;
289}
290
[email protected]6aeac8342010-10-01 20:21:18291// static
[email protected]d6ebc9792011-04-07 18:18:33292// This function is used to implement the command-line switch
[email protected]3bdba0d2011-08-23 07:17:30293// --uninstall-extension, and to uninstall an extension via sync. The LOG
294// statements within this function are used to inform the user if the uninstall
295// cannot be done.
[email protected]eaa7dd182010-12-14 11:09:00296bool ExtensionService::UninstallExtensionHelper(
297 ExtensionService* extensions_service,
[email protected]6aeac8342010-10-01 20:21:18298 const std::string& extension_id) {
[email protected]6aeac8342010-10-01 20:21:18299
[email protected]95da88c42011-03-31 10:07:33300 const Extension* extension =
[email protected]8001df22011-04-28 19:59:47301 extensions_service->GetInstalledExtension(extension_id);
[email protected]95da88c42011-03-31 10:07:33302
[email protected]d6ebc9792011-04-07 18:18:33303 // We can't call UninstallExtension with an invalid extension ID.
[email protected]95da88c42011-03-31 10:07:33304 if (!extension) {
[email protected]6aeac8342010-10-01 20:21:18305 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]95da88c42011-03-31 10:07:33306 << "id: " << extension_id;
307 return false;
[email protected]6aeac8342010-10-01 20:21:18308 }
309
[email protected]d6ebc9792011-04-07 18:18:33310 // The following call to UninstallExtension will not allow an uninstall of a
311 // policy-controlled extension.
[email protected]65187152012-06-02 13:14:14312 string16 error;
[email protected]d6ebc9792011-04-07 18:18:33313 if (!extensions_service->UninstallExtension(extension_id, false, &error)) {
314 LOG(WARNING) << "Cannot uninstall extension with id " << extension_id
315 << ": " << error;
316 return false;
317 }
[email protected]95da88c42011-03-31 10:07:33318
[email protected]6aeac8342010-10-01 20:21:18319 return true;
320}
321
[email protected]eaa7dd182010-12-14 11:09:00322ExtensionService::ExtensionService(Profile* profile,
[email protected]f0841cd2011-01-19 15:07:24323 const CommandLine* command_line,
324 const FilePath& install_directory,
325 ExtensionPrefs* extension_prefs,
[email protected]0436b102011-04-15 18:30:03326 bool autoupdate_enabled,
327 bool extensions_enabled)
[email protected]d8c8f25f2011-11-02 18:18:01328 : profile_(profile),
[email protected]749d59a2012-04-05 00:23:24329 system_(ExtensionSystem::Get(profile)),
[email protected]73c47932010-12-06 18:13:43330 extension_prefs_(extension_prefs),
[email protected]cccdf0aa2011-11-11 03:43:38331 settings_frontend_(extensions::SettingsFrontend::Create(profile)),
[email protected]14908b72011-04-20 06:54:36332 pending_extension_manager_(*ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]a9b00ac2009-06-25 21:03:23333 install_directory_(install_directory),
[email protected]0436b102011-04-15 18:30:03334 extensions_enabled_(extensions_enabled),
[email protected]e81dba32009-06-19 20:19:13335 show_extensions_prompts_(true),
[email protected]3ecda252010-11-18 19:50:55336 ready_(false),
[email protected]14908b72011-04-20 06:54:36337 toolbar_model_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]8b280302011-10-13 22:22:23338 menu_manager_(profile),
[email protected]af9db5f2011-10-05 05:13:15339 app_notification_manager_(new AppNotificationManager(profile)),
[email protected]18cc5a52011-04-15 16:03:38340 apps_promo_(profile->GetPrefs()),
[email protected]7c1490da2011-10-11 18:53:25341 event_routers_initialized_(false),
[email protected]dff1e042012-04-27 10:59:18342 update_once_all_providers_are_ready_(false),
[email protected]5db9ada2012-04-11 13:48:20343 app_sync_bundle_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
344 extension_sync_bundle_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]a1755bd2011-12-09 00:40:20345 extension_warnings_(profile),
[email protected]58edca52012-02-16 21:51:38346 api_resource_controller_(NULL),
[email protected]178f8512012-02-09 01:49:36347 app_shortcut_manager_(profile) {
[email protected]a29a517a2011-01-21 21:11:12348 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20349
[email protected]36a784c2009-06-23 06:21:08350 // Figure out if extension installation should be enabled.
[email protected]6d60703b2009-08-29 01:29:23351 if (command_line->HasSwitch(switches::kDisableExtensions)) {
352 extensions_enabled_ = false;
353 } else if (profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions)) {
354 extensions_enabled_ = false;
[email protected]6b75ec32009-08-14 06:37:18355 }
[email protected]36a784c2009-06-23 06:21:08356
[email protected]432115822011-07-10 15:52:27357 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07358 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27359 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED,
[email protected]ad50def52011-10-19 23:17:07360 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27361 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07362 content::NotificationService::AllBrowserContextsAndSources());
[email protected]2fb7dc982010-09-29 12:24:28363 pref_change_registrar_.Init(profile->GetPrefs());
364 pref_change_registrar_.Add(prefs::kExtensionInstallAllowList, this);
365 pref_change_registrar_.Add(prefs::kExtensionInstallDenyList, this);
[email protected]4814b512009-11-07 00:12:29366
[email protected]93fd78f42009-07-10 16:43:17367 // Set up the ExtensionUpdater
368 if (autoupdate_enabled) {
369 int update_frequency = kDefaultUpdateFrequencySeconds;
370 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25371 base::StringToInt(command_line->GetSwitchValueASCII(
372 switches::kExtensionsUpdateFrequency),
373 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17374 }
[email protected]42a08162012-03-16 18:09:11375 updater_.reset(new extensions::ExtensionUpdater(this,
376 extension_prefs,
377 profile->GetPrefs(),
378 profile,
379 update_frequency));
[email protected]93fd78f42009-07-10 16:43:17380 }
381
[email protected]25ae0152011-11-18 14:40:02382 component_loader_.reset(
383 new extensions::ComponentLoader(this,
384 profile->GetPrefs(),
385 g_browser_process->local_state()));
[email protected]8e4560b62011-01-14 10:09:14386
[email protected]af9db5f2011-10-05 05:13:15387 app_notification_manager_->Init();
388
[email protected]0436b102011-04-15 18:30:03389 if (extensions_enabled_) {
[email protected]1f2f3002011-12-28 00:40:21390 if (!command_line->HasSwitch(switches::kImport) &&
391 !command_line->HasSwitch(switches::kImportFromFile)) {
392 ExternalExtensionProviderImpl::CreateExternalProviders(
393 this, profile_, &external_extension_providers_);
394 }
[email protected]873531342011-03-09 12:16:05395 }
[email protected]b671760b2010-07-15 21:13:47396
[email protected]aa96d3a2010-08-21 08:45:25397 // Use monochrome icons for Omnibox icons.
[email protected]29d0d4ac2010-09-08 21:10:31398 omnibox_popup_icon_manager_.set_monochrome(true);
[email protected]b671760b2010-07-15 21:13:47399 omnibox_icon_manager_.set_monochrome(true);
[email protected]29d0d4ac2010-09-08 21:10:31400 omnibox_icon_manager_.set_padding(gfx::Insets(0, kOmniboxIconPaddingLeft,
401 0, kOmniboxIconPaddingRight));
[email protected]cb0e50312011-05-09 15:03:07402
[email protected]fb82dcd2012-03-21 14:15:46403 // Set this as the ExtensionService for extension sorting to ensure it
404 // cause syncs if required.
405 extension_prefs_->extension_sorting()->SetExtensionService(this);
406
[email protected]cb0e50312011-05-09 15:03:07407 // How long is the path to the Extensions directory?
408 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ExtensionRootPathLength",
409 install_directory_.value().length(), 0, 500, 100);
[email protected]6014d672008-12-05 00:38:25410}
411
[email protected]84df8332011-12-06 18:22:46412const ExtensionSet* ExtensionService::extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45413 return &extensions_;
414}
415
[email protected]84df8332011-12-06 18:22:46416const ExtensionSet* ExtensionService::disabled_extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45417 return &disabled_extensions_;
418}
419
[email protected]84df8332011-12-06 18:22:46420const ExtensionSet* ExtensionService::terminated_extensions() const {
[email protected]bb7f40952011-01-13 00:21:20421 return &terminated_extensions_;
422}
423
[email protected]7f4308d2012-01-18 07:43:01424const ExtensionSet* ExtensionService::GenerateInstalledExtensionsSet() const {
425 ExtensionSet* installed_extensions = new ExtensionSet();
426 installed_extensions->InsertAll(extensions_);
427 installed_extensions->InsertAll(disabled_extensions_);
428 installed_extensions->InsertAll(terminated_extensions_);
429 return installed_extensions;
430}
431
[email protected]b2907fd2011-03-25 16:43:37432PendingExtensionManager* ExtensionService::pending_extension_manager() {
433 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45434}
435
[email protected]eaa7dd182010-12-14 11:09:00436ExtensionService::~ExtensionService() {
[email protected]c8d407e2011-04-28 21:27:17437 // No need to unload extensions here because they are profile-scoped, and the
438 // profile is in the process of being deleted.
[email protected]94b4ffc2011-02-04 05:55:17439
[email protected]8e4560b62011-01-14 10:09:14440 ProviderCollection::const_iterator i;
441 for (i = external_extension_providers_.begin();
442 i != external_extension_providers_.end(); ++i) {
443 ExternalExtensionProviderInterface* provider = i->get();
444 provider->ServiceShutdown();
445 }
[email protected]6014d672008-12-05 00:38:25446}
447
[email protected]d91268022011-08-26 13:17:37448void ExtensionService::InitEventRoutersAfterImport() {
[email protected]cf593af2011-12-30 05:44:39449 RegisterForImportFinished();
450}
451
452void ExtensionService::RegisterForImportFinished() {
453 if (!registrar_.IsRegistered(this, chrome::NOTIFICATION_IMPORT_FINISHED,
454 content::Source<Profile>(profile_))) {
455 registrar_.Add(this, chrome::NOTIFICATION_IMPORT_FINISHED,
456 content::Source<Profile>(profile_));
457 }
458}
459
460void ExtensionService::InitAfterImport() {
461 CheckForExternalUpdates();
462
463 GarbageCollectExtensions();
464
465 // Idempotent, so although there is a possible race if the import
466 // process finished sometime in the middle of ProfileImpl::InitExtensions,
467 // it cannot happen twice.
468 InitEventRouters();
[email protected]d91268022011-08-26 13:17:37469}
470
[email protected]eaa7dd182010-12-14 11:09:00471void ExtensionService::InitEventRouters() {
[email protected]b3d62312b12010-10-14 21:10:18472 if (event_routers_initialized_)
473 return;
474
[email protected]6a3cd37e2012-04-17 17:13:34475#if defined(ENABLE_EXTENSIONS)
[email protected]d2e6bd62011-10-24 20:29:07476 history_event_router_.reset(new HistoryExtensionEventRouter());
[email protected]f825df22011-06-28 17:36:10477 history_event_router_->ObserveProfile(profile_);
[email protected]97d2f1d2011-01-15 00:41:08478 browser_event_router_.reset(new ExtensionBrowserEventRouter(profile_));
[email protected]34517242012-05-25 02:37:18479 browser_event_router_->Init();
[email protected]118de7892011-03-16 13:31:40480 preference_event_router_.reset(new ExtensionPreferenceEventRouter(profile_));
[email protected]23d5f172011-10-25 05:49:53481 bookmark_event_router_.reset(new BookmarkExtensionEventRouter(
[email protected]f825df22011-06-28 17:36:10482 profile_->GetBookmarkModel()));
483 bookmark_event_router_->Init();
[email protected]92ba7b012012-05-16 03:36:17484 cookies_event_router_.reset(new
485 extensions::ExtensionCookiesEventRouter(profile_));
[email protected]f825df22011-06-28 17:36:10486 cookies_event_router_->Init();
487 management_event_router_.reset(new ExtensionManagementEventRouter(profile_));
488 management_event_router_->Init();
[email protected]7da9a8232011-05-27 21:07:11489 ExtensionProcessesEventRouter::GetInstance()->ObserveProfile(profile_);
[email protected]f825df22011-06-28 17:36:10490 web_navigation_event_router_.reset(
[email protected]017ecde92012-04-04 09:11:26491 new extensions::WebNavigationEventRouter(profile_));
[email protected]f825df22011-06-28 17:36:10492 web_navigation_event_router_->Init();
[email protected]bdd34452012-04-16 15:58:40493 font_settings_event_router_.reset(
494 new ExtensionFontSettingsEventRouter(profile_));
495 font_settings_event_router_->Init();
[email protected]8a4cf842012-05-16 13:35:30496 managed_mode_event_router_.reset(
497 new ExtensionManagedModeEventRouter(profile_));
498 managed_mode_event_router_->Init();
[email protected]27072cad2011-05-09 19:46:40499
[email protected]eed367e2011-04-12 03:43:31500#if defined(OS_CHROMEOS)
[email protected]2a72c9f2012-03-17 15:10:30501 FileBrowserEventRouterFactory::GetForProfile(
502 profile_)->ObserveFileSystemEvents();
[email protected]771a6182011-10-05 16:32:52503
[email protected]f463bc92012-03-22 16:38:23504 bluetooth_event_router_.reset(
505 new chromeos::ExtensionBluetoothEventRouter(profile_));
506
[email protected]771a6182011-10-05 16:32:52507 input_method_event_router_.reset(
508 new chromeos::ExtensionInputMethodEventRouter);
[email protected]be5bd5742011-07-14 07:21:01509
[email protected]c8bf09be2011-06-25 03:22:27510 ExtensionMediaPlayerEventRouter::GetInstance()->Init(profile_);
[email protected]8baf0dbb2011-11-27 22:47:02511 ExtensionInputImeEventRouter::GetInstance()->Init();
[email protected]eed367e2011-04-12 03:43:31512#endif
[email protected]6a3cd37e2012-04-17 17:13:34513#endif // defined(ENABLE_EXTENSIONS)
[email protected]b3d62312b12010-10-14 21:10:18514 event_routers_initialized_ = true;
[email protected]c5ae74ab2010-04-15 18:14:37515}
516
[email protected]b2907fd2011-03-25 16:43:37517const Extension* ExtensionService::GetExtensionById(
518 const std::string& id, bool include_disabled) const {
[email protected]8001df22011-04-28 19:59:47519 return GetExtensionByIdInternal(id, true, include_disabled, false);
[email protected]78994ab02010-12-08 18:06:44520}
521
[email protected]eaa7dd182010-12-14 11:09:00522void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12523 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20524
[email protected]fa6a9102010-11-22 15:38:50525 DCHECK(!ready_); // Can't redo init.
[email protected]93fd78f42009-07-10 16:43:17526 DCHECK_EQ(extensions_.size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32527
[email protected]d8c8f25f2011-11-02 18:18:01528 component_loader_->LoadAll();
529 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]894bb502009-05-21 22:39:57530
[email protected]7a161db2011-12-15 20:45:51531 // If we are running in the import process, don't bother initializing the
532 // extension service since this can interfere with the main browser process
533 // that is already running an extension service for this profile.
534 // TODO(aa): can we start up even less of ExtensionService?
535 // http://crbug.com/107636
[email protected]1f2f3002011-12-28 00:40:21536 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kImport) &&
537 !CommandLine::ForCurrentProcess()->HasSwitch(switches::kImportFromFile)) {
[email protected]cf593af2011-12-30 05:44:39538 if (g_browser_process->profile_manager() &&
539 g_browser_process->profile_manager()->will_import()) {
540 RegisterForImportFinished();
541 } else {
542 // TODO(erikkay) this should probably be deferred to a future point
543 // rather than running immediately at startup.
544 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57545
[email protected]cf593af2011-12-30 05:44:39546 // TODO(erikkay) this should probably be deferred as well.
547 GarbageCollectExtensions();
548 }
[email protected]7a161db2011-12-15 20:45:51549 }
[email protected]6014d672008-12-05 00:38:25550}
551
[email protected]420a0ec2011-06-01 01:07:03552bool ExtensionService::UpdateExtension(
553 const std::string& id,
554 const FilePath& extension_path,
555 const GURL& download_url,
556 CrxInstaller** out_crx_installer) {
[email protected]a29a517a2011-01-21 21:11:12557 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20558
[email protected]51a3bf8b2012-06-08 22:53:06559 const PendingExtensionInfo* pending_extension_info =
560 pending_extension_manager()->GetById(id);
[email protected]8ef78fd2010-08-19 17:14:32561
[email protected]8001df22011-04-28 19:59:47562 const Extension* extension =
563 GetExtensionByIdInternal(id, true, true, false);
[email protected]51a3bf8b2012-06-08 22:53:06564 if (!pending_extension_info && !extension) {
[email protected]aa142702010-03-26 01:26:33565 LOG(WARNING) << "Will not update extension " << id
566 << " because it is not installed or pending";
567 // Delete extension_path since we're not creating a CrxInstaller
568 // that would do it for us.
[email protected]14908b72011-04-20 06:54:36569 if (!BrowserThread::PostTask(
570 BrowserThread::FILE, FROM_HERE,
[email protected]53612e82011-10-18 18:00:36571 base::Bind(
[email protected]7296f2762011-11-21 19:23:44572 &extension_file_util::DeleteFile, extension_path, false)))
[email protected]14908b72011-04-20 06:54:36573 NOTREACHED();
[email protected]420a0ec2011-06-01 01:07:03574
575 return false;
[email protected]e957fe52009-06-23 16:51:05576 }
577
[email protected]aa142702010-03-26 01:26:33578 // We want a silent install only for non-pending extensions and
579 // pending extensions that have install_silently set.
[email protected]32fc4ff72012-06-15 21:50:01580 Browser* browser = browser::FindLastActiveWithProfile(profile_);
[email protected]c82da8c42012-06-08 19:49:11581 ExtensionInstallPrompt* client =
[email protected]51a3bf8b2012-06-08 22:53:06582 (!pending_extension_info || pending_extension_info->install_silently()) ?
[email protected]32fc4ff72012-06-15 21:50:01583 NULL : new ExtensionInstallPrompt(browser);
[email protected]aa142702010-03-26 01:26:33584
[email protected]d8c8f25f2011-11-02 18:18:01585 scoped_refptr<CrxInstaller> installer(CrxInstaller::Create(this, client));
[email protected]6dfbbf82010-03-12 23:09:16586 installer->set_expected_id(id);
[email protected]51a3bf8b2012-06-08 22:53:06587 if (pending_extension_info) {
588 installer->set_install_source(pending_extension_info->install_source());
589 if (pending_extension_info->install_silently())
590 installer->set_allow_silent_install(true);
591 } else if (extension) {
[email protected]5eb375e92010-11-26 07:50:41592 installer->set_install_source(extension->location());
[email protected]51a3bf8b2012-06-08 22:53:06593 }
[email protected]7d8b7072012-04-07 01:07:46594 // If the extension was installed from or has migrated to the webstore, or
595 // if the extension came from sync and its auto-update URL is from the
[email protected]75764512011-12-19 19:54:28596 // webstore, treat it as a webstore install. Note that we ignore some older
597 // extensions with blank auto-update URLs because we are mostly concerned
598 // with restrictions on NaCl extensions, which are newer.
[email protected]a12ce8b22012-01-17 18:40:53599 int creation_flags = Extension::NO_FLAGS;
[email protected]75764512011-12-19 19:54:28600 if ((extension && extension->from_webstore()) ||
[email protected]7d8b7072012-04-07 01:07:46601 (extension && extension->UpdatesFromGallery()) ||
[email protected]51a3bf8b2012-06-08 22:53:06602 (!extension && pending_extension_info->is_from_sync() &&
[email protected]75764512011-12-19 19:54:28603 extension_urls::IsWebstoreUpdateUrl(
[email protected]51a3bf8b2012-06-08 22:53:06604 pending_extension_info->update_url()))) {
[email protected]a12ce8b22012-01-17 18:40:53605 creation_flags |= Extension::FROM_WEBSTORE;
606 }
607
608 // Bookmark apps being updated is kind of a contradiction, but that's because
609 // we mark the default apps as bookmark apps, and they're hosted in the web
610 // store, thus they can get updated. See http://crbug.com/101605 for more
611 // details.
612 if (extension && extension->from_bookmark())
613 creation_flags |= Extension::FROM_BOOKMARK;
614
[email protected]a12ce8b22012-01-17 18:40:53615 installer->set_creation_flags(creation_flags);
616
[email protected]6dfbbf82010-03-12 23:09:16617 installer->set_delete_source(true);
[email protected]655b2b1a2011-10-13 17:13:06618 installer->set_download_url(download_url);
[email protected]cb0e50312011-05-09 15:03:07619 installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
[email protected]6dfbbf82010-03-12 23:09:16620 installer->InstallCrx(extension_path);
[email protected]420a0ec2011-06-01 01:07:03621
622 if (out_crx_installer)
623 *out_crx_installer = installer;
624
625 return true;
[email protected]e957fe52009-06-23 16:51:05626}
627
[email protected]eaa7dd182010-12-14 11:09:00628void ExtensionService::ReloadExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12629 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]b65272f2009-08-31 15:47:06630 FilePath path;
[email protected]9adb9692010-10-29 23:14:02631 const Extension* current_extension = GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40632
[email protected]f17dbd42010-08-16 23:21:10633 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]b65272f2009-08-31 15:47:06634 if (current_extension) {
[email protected]4814b512009-11-07 00:12:29635 // If the extension has an inspector open for its background page, detach
636 // the inspector and hang onto a cookie for it, so that we can reattach
637 // later.
[email protected]31d8f5f22012-04-02 15:22:08638 // TODO(yoz): this is not incognito-safe!
639 ExtensionProcessManager* manager = system_->process_manager();
[email protected]06024c62011-10-20 20:57:12640 ExtensionHost* host = manager->GetBackgroundHostForExtension(extension_id);
[email protected]0e12d7d2011-12-01 16:21:44641 if (host && DevToolsAgentHostRegistry::HasDevToolsAgentHost(
642 host->render_view_host())) {
[email protected]4814b512009-11-07 00:12:29643 // Look for an open inspector for the background page.
[email protected]0e12d7d2011-12-01 16:21:44644 DevToolsAgentHost* agent =
645 DevToolsAgentHostRegistry::GetDevToolsAgentHost(
646 host->render_view_host());
647 int devtools_cookie =
648 content::DevToolsManager::GetInstance()->DetachClientHost(agent);
[email protected]4814b512009-11-07 00:12:29649 if (devtools_cookie >= 0)
650 orphaned_dev_tools_[extension_id] = devtools_cookie;
651 }
652
[email protected]b65272f2009-08-31 15:47:06653 path = current_extension->path();
[email protected]44d62b62012-04-11 00:06:03654 DisableExtension(extension_id, Extension::DISABLE_RELOAD);
[email protected]f17dbd42010-08-16 23:21:10655 disabled_extension_paths_[extension_id] = path;
[email protected]1eb175082010-02-10 09:26:16656 } else {
657 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06658 }
659
[email protected]43ceb002012-02-10 23:19:15660 // If we're reloading a component extension, use the component extension
661 // loader's reloader.
662 if (component_loader_->Exists(extension_id)) {
663 component_loader_->Reload(extension_id);
664 return;
665 }
666
[email protected]e6090e42010-03-23 22:44:08667 // Check the installed extensions to see if what we're reloading was already
668 // installed.
669 scoped_ptr<ExtensionInfo> installed_extension(
670 extension_prefs_->GetInstalledExtensionInfo(extension_id));
671 if (installed_extension.get() &&
672 installed_extension->extension_manifest.get()) {
[email protected]d8c8f25f2011-11-02 18:18:01673 extensions::InstalledLoader(this).Load(*installed_extension, false);
[email protected]e6090e42010-03-23 22:44:08674 } else {
[email protected]d8c8f25f2011-11-02 18:18:01675 // Otherwise, the extension is unpacked (location LOAD).
[email protected]e6090e42010-03-23 22:44:08676 // We should always be able to remember the extension's path. If it's not in
677 // the map, someone failed to update |unloaded_extension_paths_|.
678 CHECK(!path.empty());
[email protected]d8c8f25f2011-11-02 18:18:01679 extensions::UnpackedInstaller::Create(this)->Load(path);
[email protected]e6090e42010-03-23 22:44:08680 }
[email protected]9cddd4702009-07-27 22:09:40681}
682
[email protected]fa2416f2011-05-03 08:41:20683bool ExtensionService::UninstallExtension(
[email protected]a12c706e2011-12-01 00:58:08684 std::string extension_id,
[email protected]fa2416f2011-05-03 08:41:20685 bool external_uninstall,
[email protected]65187152012-06-02 13:14:14686 string16* error) {
[email protected]a29a517a2011-01-21 21:11:12687 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20688
[email protected]0d6ec3a72011-09-02 02:09:43689 scoped_refptr<const Extension> extension(GetInstalledExtension(extension_id));
[email protected]631cf822009-05-15 07:01:25690
[email protected]e7afe2452010-08-22 16:19:13691 // Callers should not send us nonexistent extensions.
[email protected]aebe23a32010-12-10 22:15:48692 CHECK(extension);
[email protected]9f1087e2009-06-15 17:29:32693
[email protected]95da88c42011-03-31 10:07:33694 // Policy change which triggers an uninstall will always set
695 // |external_uninstall| to true so this is the only way to uninstall
696 // managed extensions.
[email protected]65187152012-06-02 13:14:14697 if (!external_uninstall &&
698 !system_->management_policy()->UserMayModifySettings(
699 extension.get(), error)) {
[email protected]ad50def52011-10-19 23:17:07700 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27701 chrome::NOTIFICATION_EXTENSION_UNINSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:53702 content::Source<Profile>(profile_),
703 content::Details<const Extension>(extension));
[email protected]d6ebc9792011-04-07 18:18:33704 return false;
705 }
[email protected]95da88c42011-03-31 10:07:33706
[email protected]3bdba0d2011-08-23 07:17:30707 // Extract the data we need for sync now, but don't actually sync until we've
708 // completed the uninstallation.
[email protected]65f173552012-06-28 22:43:58709 syncer::SyncChange sync_change;
[email protected]5db9ada2012-04-11 13:48:20710 if (app_sync_bundle_.HandlesApp(*extension)) {
711 sync_change = app_sync_bundle_.CreateSyncChangeToDelete(extension);
712 } else if (extension_sync_bundle_.HandlesExtension(*extension)) {
713 sync_change = extension_sync_bundle_.CreateSyncChangeToDelete(extension);
[email protected]3bdba0d2011-08-23 07:17:30714 }
715
[email protected]9b217652010-10-08 22:04:23716 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08717 extension->GetType(), 100);
[email protected]fe2dd7742011-04-19 22:52:49718 RecordPermissionMessagesHistogram(
719 extension, "Extensions.Permissions_Uninstall");
[email protected]9b217652010-10-08 22:04:23720
[email protected]8e5c89a2011-06-07 18:13:33721 TemplateURLService* url_service =
722 TemplateURLServiceFactory::GetForProfile(profile_);
723 if (url_service)
724 url_service->UnregisterExtensionKeyword(extension);
[email protected]56ad3792010-05-28 17:45:33725
[email protected]831aa212010-03-26 13:55:19726 // Unload before doing more cleanup to ensure that nothing is hanging on to
727 // any of these resources.
[email protected]814a7bf0f2011-08-13 05:30:59728 UnloadExtension(extension_id, extension_misc::UNLOAD_REASON_UNINSTALL);
[email protected]831aa212010-03-26 13:55:19729
[email protected]0d6ec3a72011-09-02 02:09:43730 extension_prefs_->OnExtensionUninstalled(extension_id, extension->location(),
[email protected]831aa212010-03-26 13:55:19731 external_uninstall);
[email protected]9f1087e2009-06-15 17:29:32732
733 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]0d6ec3a72011-09-02 02:09:43734 if (Extension::LOAD != extension->location()) {
[email protected]14908b72011-04-20 06:54:36735 if (!BrowserThread::PostTask(
736 BrowserThread::FILE, FROM_HERE,
[email protected]53612e82011-10-18 18:00:36737 base::Bind(
[email protected]14908b72011-04-20 06:54:36738 &extension_file_util::UninstallExtension,
739 install_directory_,
[email protected]fa2416f2011-05-03 08:41:20740 extension_id)))
[email protected]14908b72011-04-20 06:54:36741 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32742 }
743
[email protected]0d6ec3a72011-09-02 02:09:43744 GURL launch_web_url_origin(extension->launch_web_url());
745 launch_web_url_origin = launch_web_url_origin.GetOrigin();
[email protected]605fb8102012-05-04 01:36:55746 bool is_storage_isolated = extension->is_storage_isolated();
[email protected]0d6ec3a72011-09-02 02:09:43747
748 if (extension->is_hosted_app() &&
749 !profile_->GetExtensionSpecialStoragePolicy()->
750 IsStorageProtected(launch_web_url_origin)) {
[email protected]dc0b5a12011-10-14 00:06:13751 ExtensionDataDeleter::StartDeleting(
752 profile_, extension_id, launch_web_url_origin, is_storage_isolated);
[email protected]0d6ec3a72011-09-02 02:09:43753 }
[email protected]dc0b5a12011-10-14 00:06:13754 ExtensionDataDeleter::StartDeleting(
755 profile_, extension_id, extension->url(), is_storage_isolated);
[email protected]0d6ec3a72011-09-02 02:09:43756
[email protected]0dfe05c2011-02-23 23:03:36757 UntrackTerminatedExtension(extension_id);
[email protected]211030342010-09-30 18:41:06758
759 // Notify interested parties that we've uninstalled this extension.
[email protected]ad50def52011-10-19 23:17:07760 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27761 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
[email protected]6c2381d2011-10-19 02:52:53762 content::Source<Profile>(profile_),
763 content::Details<const std::string>(&extension_id));
[email protected]d6ebc9792011-04-07 18:18:33764
[email protected]5db9ada2012-04-11 13:48:20765 if (app_sync_bundle_.HasExtensionId(extension_id) &&
766 sync_change.sync_data().GetDataType() == syncable::APPS) {
767 app_sync_bundle_.ProcessDeletion(extension_id, sync_change);
768 } else if (extension_sync_bundle_.HasExtensionId(extension_id) &&
769 sync_change.sync_data().GetDataType() == syncable::EXTENSIONS) {
770 extension_sync_bundle_.ProcessDeletion(extension_id, sync_change);
[email protected]3bdba0d2011-08-23 07:17:30771 }
772
[email protected]333b1de2011-09-12 18:28:50773 // Track the uninstallation.
[email protected]49098f702011-10-13 03:47:18774 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionUninstalled", 1, 2);
775
776 static bool default_apps_trial_exists =
[email protected]568c3312011-12-06 15:36:04777 base::FieldTrialList::TrialExists(kDefaultAppsTrialName);
[email protected]49098f702011-10-13 03:47:18778 if (default_apps_trial_exists) {
779 UMA_HISTOGRAM_ENUMERATION(
780 base::FieldTrial::MakeName("Extensions.ExtensionUninstalled",
[email protected]568c3312011-12-06 15:36:04781 kDefaultAppsTrialName),
[email protected]49098f702011-10-13 03:47:18782 1, 2);
783 }
[email protected]333b1de2011-09-12 18:28:50784
[email protected]7c1490da2011-10-11 18:53:25785 // Uninstalling one extension might have solved the problems of others.
786 // Therefore, we clear warnings of this type for all extensions.
787 std::set<ExtensionWarningSet::WarningType> warnings;
788 extension_warnings_.GetWarningsAffectingExtension(extension_id, &warnings);
789 extension_warnings_.ClearWarnings(warnings);
790
[email protected]d6ebc9792011-04-07 18:18:33791 return true;
[email protected]c10da4b02010-03-25 14:38:32792}
793
[email protected]c3cfb012011-04-06 22:07:35794bool ExtensionService::IsExtensionEnabled(
795 const std::string& extension_id) const {
[email protected]84df8332011-12-06 18:22:46796 if (extensions_.Contains(extension_id) ||
797 terminated_extensions_.Contains(extension_id))
[email protected]36429da2011-07-11 20:25:18798 return true;
799
[email protected]84df8332011-12-06 18:22:46800 if (disabled_extensions_.Contains(extension_id))
[email protected]ad83ca242011-07-29 01:32:25801 return false;
802
803 // If the extension hasn't been loaded yet, check the prefs for it. Assume
804 // enabled unless otherwise noted.
805 return !extension_prefs_->IsExtensionDisabled(extension_id) &&
806 !extension_prefs_->IsExternalExtensionUninstalled(extension_id);
[email protected]c3cfb012011-04-06 22:07:35807}
808
809bool ExtensionService::IsExternalExtensionUninstalled(
810 const std::string& extension_id) const {
811 return extension_prefs_->IsExternalExtensionUninstalled(extension_id);
812}
813
[email protected]eaa7dd182010-12-14 11:09:00814void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12815 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20816
[email protected]06f92562011-04-29 19:27:31817 if (IsExtensionEnabled(extension_id))
[email protected]0c6da502009-08-14 22:32:39818 return;
[email protected]0c6da502009-08-14 22:32:39819
[email protected]b3317ad2011-04-28 23:46:00820 extension_prefs_->SetExtensionState(extension_id, Extension::ENABLED);
[email protected]44d62b62012-04-11 00:06:03821 extension_prefs_->RemoveDisableReason(extension_id);
[email protected]1784e83a2009-09-08 21:01:52822
[email protected]06f92562011-04-29 19:27:31823 const Extension* extension =
824 GetExtensionByIdInternal(extension_id, false, true, false);
825 // This can happen if sync enables an extension that is not
826 // installed yet.
827 if (!extension)
828 return;
829
[email protected]0c6da502009-08-14 22:32:39830 // Move it over to the enabled list.
[email protected]84df8332011-12-06 18:22:46831 extensions_.Insert(make_scoped_refptr(extension));
832 disabled_extensions_.Remove(extension->id());
[email protected]0c6da502009-08-14 22:32:39833
[email protected]f681c782010-11-19 11:19:39834 // Make sure any browser action contained within it is not hidden.
835 extension_prefs_->SetBrowserActionVisibility(extension, true);
836
[email protected]62d30f42009-10-01 22:36:06837 NotifyExtensionLoaded(extension);
[email protected]3bdba0d2011-08-23 07:17:30838
[email protected]f74d7f32012-06-19 19:22:51839 // Notify listeners that the extension was enabled.
840 content::NotificationService::current()->Notify(
841 chrome::NOTIFICATION_EXTENSION_ENABLED,
842 content::Source<Profile>(profile_),
843 content::Details<const Extension>(extension));
844
[email protected]3bdba0d2011-08-23 07:17:30845 SyncExtensionChangeIfNeeded(*extension);
[email protected]0c6da502009-08-14 22:32:39846}
847
[email protected]44d62b62012-04-11 00:06:03848void ExtensionService::DisableExtension(
849 const std::string& extension_id,
850 Extension::DisableReason disable_reason) {
[email protected]a29a517a2011-01-21 21:11:12851 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20852
[email protected]b2ba9962009-12-10 20:10:15853 // The extension may have been disabled already.
[email protected]06f92562011-04-29 19:27:31854 if (!IsExtensionEnabled(extension_id))
[email protected]1784e83a2009-09-08 21:01:52855 return;
[email protected]1784e83a2009-09-08 21:01:52856
[email protected]06f92562011-04-29 19:27:31857 const Extension* extension = GetInstalledExtension(extension_id);
858 // |extension| can be NULL if sync disables an extension that is not
859 // installed yet.
[email protected]65187152012-06-02 13:14:14860 if (extension &&
861 !system_->management_policy()->UserMayModifySettings(extension, NULL)) {
[email protected]95da88c42011-03-31 10:07:33862 return;
[email protected]65187152012-06-02 13:14:14863 }
[email protected]95da88c42011-03-31 10:07:33864
[email protected]b3317ad2011-04-28 23:46:00865 extension_prefs_->SetExtensionState(extension_id, Extension::DISABLED);
[email protected]44d62b62012-04-11 00:06:03866 extension_prefs_->SetDisableReason(extension_id, disable_reason);
[email protected]1784e83a2009-09-08 21:01:52867
[email protected]06f92562011-04-29 19:27:31868 extension = GetExtensionByIdInternal(extension_id, true, false, true);
869 if (!extension)
870 return;
871
[email protected]1784e83a2009-09-08 21:01:52872 // Move it over to the disabled list.
[email protected]84df8332011-12-06 18:22:46873 disabled_extensions_.Insert(make_scoped_refptr(extension));
874 if (extensions_.Contains(extension->id()))
875 extensions_.Remove(extension->id());
876 else
877 terminated_extensions_.Remove(extension->id());
[email protected]1784e83a2009-09-08 21:01:52878
[email protected]814a7bf0f2011-08-13 05:30:59879 NotifyExtensionUnloaded(extension, extension_misc::UNLOAD_REASON_DISABLE);
[email protected]3bdba0d2011-08-23 07:17:30880
881 SyncExtensionChangeIfNeeded(*extension);
[email protected]7c1490da2011-10-11 18:53:25882
883 // Deactivating one extension might have solved the problems of others.
884 // Therefore, we clear warnings of this type for all extensions.
885 std::set<ExtensionWarningSet::WarningType> warnings;
886 extension_warnings_.GetWarningsAffectingExtension(extension_id, &warnings);
887 extension_warnings_.ClearWarnings(warnings);
[email protected]1784e83a2009-09-08 21:01:52888}
889
[email protected]eaa7dd182010-12-14 11:09:00890void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]b70a2d92012-06-28 19:51:21891 const Extension* extension, bool record_oauth2_grant) {
[email protected]8d888c12010-11-30 00:00:25892 CHECK(extension);
[email protected]fe2dd7742011-04-19 22:52:49893 RecordPermissionMessagesHistogram(
894 extension, "Extensions.Permissions_ReEnable");
[email protected]c333e792012-01-06 16:57:39895 extensions::PermissionsUpdater perms_updater(profile());
[email protected]b70a2d92012-06-28 19:51:21896 perms_updater.GrantActivePermissions(extension, record_oauth2_grant);
[email protected]8d888c12010-11-30 00:00:25897 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
898 EnableExtension(extension->id());
899}
900
[email protected]fe2dd7742011-04-19 22:52:49901// static
902void ExtensionService::RecordPermissionMessagesHistogram(
903 const Extension* e, const char* histogram) {
904 // Since this is called from multiple sources, and since the Histogram macros
905 // use statics, we need to manually lookup the Histogram ourselves.
906 base::Histogram* counter = base::LinearHistogram::FactoryGet(
907 histogram,
908 1,
[email protected]c2e66e12012-06-27 06:27:06909 PermissionMessage::kEnumBoundary,
910 PermissionMessage::kEnumBoundary + 1,
[email protected]fe2dd7742011-04-19 22:52:49911 base::Histogram::kUmaTargetedHistogramFlag);
912
[email protected]c2e66e12012-06-27 06:27:06913 PermissionMessages permissions = e->GetPermissionMessages();
[email protected]fe2dd7742011-04-19 22:52:49914 if (permissions.empty()) {
[email protected]c2e66e12012-06-27 06:27:06915 counter->Add(PermissionMessage::kNone);
[email protected]fe2dd7742011-04-19 22:52:49916 } else {
[email protected]c2e66e12012-06-27 06:27:06917 for (PermissionMessages::iterator it = permissions.begin();
[email protected]0d3e4a22011-06-23 19:02:52918 it != permissions.end(); ++it)
919 counter->Add(it->id());
[email protected]fe2dd7742011-04-19 22:52:49920 }
921}
922
[email protected]eaa7dd182010-12-14 11:09:00923void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]57a777f72010-03-31 01:09:42924 // The ChromeURLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:06925 // was loaded, otherwise a race can arise where a renderer that is created
926 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:42927 // that the request context doesn't yet know about. The profile is responsible
928 // for ensuring its URLRequestContexts appropriately discover the loaded
929 // extension.
[email protected]31d8f5f22012-04-02 15:22:08930 system_->RegisterExtensionWithRequestContexts(extension);
[email protected]62d30f42009-10-01 22:36:06931
[email protected]19647262011-12-16 09:57:49932 // Tell renderers about the new extension, unless it's a theme (renderers
933 // don't need to know about themes).
934 if (!extension->is_theme()) {
935 for (content::RenderProcessHost::iterator i(
936 content::RenderProcessHost::AllHostsIterator());
937 !i.IsAtEnd(); i.Advance()) {
938 content::RenderProcessHost* host = i.GetCurrentValue();
939 Profile* host_profile =
940 Profile::FromBrowserContext(host->GetBrowserContext());
941 if (host_profile->GetOriginalProfile() ==
942 profile_->GetOriginalProfile()) {
943 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions(
944 1, ExtensionMsg_Loaded_Params(extension));
945 host->Send(
946 new ExtensionMsg_Loaded(loaded_extensions));
947 }
[email protected]c8d407e2011-04-28 21:27:17948 }
[email protected]77a6970c2011-04-23 16:58:56949 }
950
[email protected]3442a662012-01-12 08:06:17951 // Tell subsystems that use the EXTENSION_LOADED notification about the new
952 // extension.
953 //
954 // NOTE: It is important that this happen after notifying the renderers about
955 // the new extensions so that if we navigate to an extension URL in
956 // NOTIFICATION_EXTENSION_LOADED, the renderer is guaranteed to know about it.
957 content::NotificationService::current()->Notify(
958 chrome::NOTIFICATION_EXTENSION_LOADED,
959 content::Source<Profile>(profile_),
960 content::Details<const Extension>(extension));
961
[email protected]c8d407e2011-04-28 21:27:17962 // Tell a random-ass collection of other subsystems about the new extension.
963 // TODO(aa): What should we do with all this goop? Can it move into the
964 // relevant objects via EXTENSION_LOADED?
965
966 profile_->GetExtensionSpecialStoragePolicy()->
967 GrantRightsForExtension(extension);
968
969 UpdateActiveExtensionsInCrashReporter();
970
971 ExtensionWebUI::RegisterChromeURLOverrides(
972 profile_, extension->GetChromeURLOverrides());
973
[email protected]8e5c89a2011-06-07 18:13:33974 TemplateURLService* url_service =
975 TemplateURLServiceFactory::GetForProfile(profile_);
976 if (url_service)
977 url_service->RegisterExtensionKeyword(extension);
[email protected]c8d407e2011-04-28 21:27:17978
979 // Load the icon for omnibox-enabled extensions so it will be ready to display
980 // in the URL bar.
981 if (!extension->omnibox_keyword().empty()) {
982 omnibox_popup_icon_manager_.LoadIcon(extension);
983 omnibox_icon_manager_.LoadIcon(extension);
984 }
985
986 // If the extension has permission to load chrome://favicon/ resources we need
987 // to make sure that the FaviconSource is registered with the
988 // ChromeURLDataManager.
989 if (extension->HasHostPermission(GURL(chrome::kChromeUIFaviconURL))) {
990 FaviconSource* favicon_source = new FaviconSource(profile_,
991 FaviconSource::FAVICON);
[email protected]ef92e172012-04-25 19:40:41992 ChromeURLDataManager::AddDataSource(profile_, favicon_source);
[email protected]c8d407e2011-04-28 21:27:17993 }
[email protected]5eddc3e2011-10-26 04:33:31994 // Same for chrome://thumb/ resources.
995 if (extension->HasHostPermission(GURL(chrome::kChromeUIThumbnailURL))) {
996 ThumbnailSource* thumbnail_source = new ThumbnailSource(profile_);
[email protected]ef92e172012-04-25 19:40:41997 ChromeURLDataManagerFactory::GetForProfile(profile_)->
998 AddDataSource(thumbnail_source);
[email protected]5eddc3e2011-10-26 04:33:31999 }
[email protected]c8d407e2011-04-28 21:27:171000
1001 // TODO(mpcomplete): This ends up affecting all profiles. See crbug.com/80757.
[email protected]2de307592011-04-05 21:16:581002 bool plugins_changed = false;
1003 for (size_t i = 0; i < extension->plugins().size(); ++i) {
1004 const Extension::PluginInfo& plugin = extension->plugins()[i];
[email protected]f520b5b2011-11-08 02:42:141005 PluginService::GetInstance()->RefreshPlugins();
1006 PluginService::GetInstance()->AddExtraPluginPath(plugin.path);
[email protected]2de307592011-04-05 21:16:581007 plugins_changed = true;
[email protected]476a27032011-09-13 13:40:491008 ChromePluginServiceFilter* filter =
1009 ChromePluginServiceFilter::GetInstance();
1010 if (plugin.is_public) {
1011 filter->RestrictPluginToProfileAndOrigin(
1012 plugin.path, profile_, GURL());
1013 } else {
1014 filter->RestrictPluginToProfileAndOrigin(
1015 plugin.path, profile_, extension->url());
[email protected]2de307592011-04-05 21:16:581016 }
1017 }
[email protected]84396dbc2011-04-14 06:33:421018
1019 bool nacl_modules_changed = false;
1020 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1021 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
[email protected]cebc3dc2011-04-18 17:15:001022 RegisterNaClModule(module.url, module.mime_type);
[email protected]84396dbc2011-04-14 06:33:421023 nacl_modules_changed = true;
1024 }
1025
[email protected]ed0ba002011-05-26 16:55:131026 if (nacl_modules_changed)
1027 UpdatePluginListWithNaClModules();
[email protected]84396dbc2011-04-14 06:33:421028
1029 if (plugins_changed || nacl_modules_changed)
[email protected]45a22e62011-10-12 09:48:021030 PluginService::GetInstance()->PurgePluginListCache(profile_, false);
[email protected]b4d9d122011-06-17 01:58:541031
[email protected]14da67b02011-08-02 05:49:131032#if defined(OS_CHROMEOS)
[email protected]b4d9d122011-06-17 01:58:541033 for (std::vector<Extension::InputComponentInfo>::const_iterator component =
1034 extension->input_components().begin();
1035 component != extension->input_components().end();
1036 ++component) {
[email protected]14da67b02011-08-02 05:49:131037 if (component->type == Extension::INPUT_COMPONENT_TYPE_IME) {
[email protected]8baf0dbb2011-11-27 22:47:021038 ExtensionInputImeEventRouter::GetInstance()->RegisterIme(
[email protected]14da67b02011-08-02 05:49:131039 profile_, extension->id(), *component);
1040 }
[email protected]b4d9d122011-06-17 01:58:541041 }
1042#endif
[email protected]62d30f42009-10-01 22:36:061043}
1044
[email protected]a9f39a312010-12-23 22:14:271045void ExtensionService::NotifyExtensionUnloaded(
[email protected]814a7bf0f2011-08-13 05:30:591046 const Extension* extension,
1047 extension_misc::UnloadedExtensionReason reason) {
[email protected]a9f39a312010-12-23 22:14:271048 UnloadedExtensionInfo details(extension, reason);
[email protected]ad50def52011-10-19 23:17:071049 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271050 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]6c2381d2011-10-19 02:52:531051 content::Source<Profile>(profile_),
1052 content::Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061053
[email protected]f3b1a082011-11-18 00:34:301054 for (content::RenderProcessHost::iterator i(
1055 content::RenderProcessHost::AllHostsIterator());
[email protected]77a6970c2011-04-23 16:58:561056 !i.IsAtEnd(); i.Advance()) {
[email protected]f3b1a082011-11-18 00:34:301057 content::RenderProcessHost* host = i.GetCurrentValue();
[email protected]9b62ecf2011-07-27 20:23:081058 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:301059 Profile::FromBrowserContext(host->GetBrowserContext());
[email protected]9b62ecf2011-07-27 20:23:081060 if (host_profile->GetOriginalProfile() == profile_->GetOriginalProfile())
[email protected]c8d407e2011-04-28 21:27:171061 host->Send(new ExtensionMsg_Unloaded(extension->id()));
[email protected]77a6970c2011-04-23 16:58:561062 }
1063
[email protected]31d8f5f22012-04-02 15:22:081064 system_->UnregisterExtensionWithRequestContexts(extension->id(), reason);
[email protected]c8d407e2011-04-28 21:27:171065 profile_->GetExtensionSpecialStoragePolicy()->
1066 RevokeRightsForExtension(extension);
1067
1068 ExtensionWebUI::UnregisterChromeURLOverrides(
1069 profile_, extension->GetChromeURLOverrides());
1070
[email protected]b777b332011-04-16 04:01:081071#if defined(OS_CHROMEOS)
1072 // Revoke external file access to
[email protected]55eb70e762012-02-20 17:38:391073 if (BrowserContext::GetFileSystemContext(profile_) &&
1074 BrowserContext::GetFileSystemContext(profile_)->external_provider()) {
1075 BrowserContext::GetFileSystemContext(profile_)->external_provider()->
[email protected]c8d407e2011-04-28 21:27:171076 RevokeAccessForExtension(extension->id());
[email protected]62d30f42009-10-01 22:36:061077 }
[email protected]54ccafa2011-12-06 23:36:321078
1079 if (extension->input_components().size() > 0) {
1080 ExtensionInputImeEventRouter::GetInstance()->UnregisterAllImes(
1081 profile_, extension->id());
1082 }
[email protected]c8d407e2011-04-28 21:27:171083#endif
1084
1085 UpdateActiveExtensionsInCrashReporter();
[email protected]2de307592011-04-05 21:16:581086
1087 bool plugins_changed = false;
1088 for (size_t i = 0; i < extension->plugins().size(); ++i) {
1089 const Extension::PluginInfo& plugin = extension->plugins()[i];
[email protected]b6a2f8de2012-01-31 17:28:491090 PluginService::GetInstance()->ForcePluginShutdown(plugin.path);
[email protected]f520b5b2011-11-08 02:42:141091 PluginService::GetInstance()->RefreshPlugins();
1092 PluginService::GetInstance()->RemoveExtraPluginPath(plugin.path);
[email protected]2de307592011-04-05 21:16:581093 plugins_changed = true;
[email protected]476a27032011-09-13 13:40:491094 ChromePluginServiceFilter::GetInstance()->UnrestrictPlugin(plugin.path);
[email protected]2de307592011-04-05 21:16:581095 }
[email protected]84396dbc2011-04-14 06:33:421096
1097 bool nacl_modules_changed = false;
1098 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1099 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
[email protected]cebc3dc2011-04-18 17:15:001100 UnregisterNaClModule(module.url);
[email protected]84396dbc2011-04-14 06:33:421101 nacl_modules_changed = true;
1102 }
1103
[email protected]ed0ba002011-05-26 16:55:131104 if (nacl_modules_changed)
1105 UpdatePluginListWithNaClModules();
[email protected]84396dbc2011-04-14 06:33:421106
1107 if (plugins_changed || nacl_modules_changed)
[email protected]45a22e62011-10-12 09:48:021108 PluginService::GetInstance()->PurgePluginListCache(profile_, false);
[email protected]62d30f42009-10-01 22:36:061109}
1110
[email protected]eaa7dd182010-12-14 11:09:001111void ExtensionService::UpdateExtensionBlacklist(
[email protected]6b75ec32009-08-14 06:37:181112 const std::vector<std::string>& blacklist) {
1113 // Use this set to indicate if an extension in the blacklist has been used.
1114 std::set<std::string> blacklist_set;
1115 for (unsigned int i = 0; i < blacklist.size(); ++i) {
1116 if (Extension::IdIsValid(blacklist[i])) {
1117 blacklist_set.insert(blacklist[i]);
1118 }
1119 }
1120 extension_prefs_->UpdateBlacklist(blacklist_set);
1121 std::vector<std::string> to_be_removed;
1122 // Loop current extensions, unload installed extensions.
[email protected]84df8332011-12-06 18:22:461123 for (ExtensionSet::const_iterator iter = extensions_.begin();
[email protected]6b75ec32009-08-14 06:37:181124 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021125 const Extension* extension = (*iter);
[email protected]6b75ec32009-08-14 06:37:181126 if (blacklist_set.find(extension->id()) != blacklist_set.end()) {
1127 to_be_removed.push_back(extension->id());
1128 }
1129 }
1130
1131 // UnloadExtension will change the extensions_ list. So, we should
1132 // call it outside the iterator loop.
1133 for (unsigned int i = 0; i < to_be_removed.size(); ++i) {
[email protected]814a7bf0f2011-08-13 05:30:591134 UnloadExtension(to_be_removed[i], extension_misc::UNLOAD_REASON_DISABLE);
[email protected]6b75ec32009-08-14 06:37:181135 }
1136}
1137
[email protected]0a071a32011-02-08 00:18:241138Profile* ExtensionService::profile() {
1139 return profile_;
1140}
1141
[email protected]eaa7dd182010-12-14 11:09:001142ExtensionPrefs* ExtensionService::extension_prefs() {
[email protected]ddb1e5a2010-12-13 20:10:451143 return extension_prefs_;
1144}
1145
[email protected]0d9a2202011-11-09 13:48:411146extensions::SettingsFrontend* ExtensionService::settings_frontend() {
[email protected]cccdf0aa2011-11-11 03:43:381147 return settings_frontend_.get();
[email protected]b7f853e282011-08-10 09:24:201148}
1149
[email protected]e2d720aa2012-05-17 00:11:351150extensions::ContentSettingsStore* ExtensionService::GetContentSettingsStore() {
[email protected]b790b072011-05-20 09:46:441151 return extension_prefs()->content_settings_store();
1152}
1153
[email protected]25ae0152011-11-18 14:40:021154bool ExtensionService::is_ready() {
1155 return ready_;
1156}
1157
[email protected]42a08162012-03-16 18:09:111158extensions::ExtensionUpdater* ExtensionService::updater() {
[email protected]2859946f2011-04-04 18:18:061159 return updater_.get();
1160}
1161
[email protected]eaa7dd182010-12-14 11:09:001162void ExtensionService::CheckAdminBlacklist() {
[email protected]aa96d3a2010-08-21 08:45:251163 std::vector<std::string> to_be_removed;
1164 // Loop through extensions list, unload installed extensions.
[email protected]84df8332011-12-06 18:22:461165 for (ExtensionSet::const_iterator iter = extensions_.begin();
[email protected]aa96d3a2010-08-21 08:45:251166 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021167 const Extension* extension = (*iter);
[email protected]65187152012-06-02 13:14:141168 if (!system_->management_policy()->UserMayLoad(extension, NULL)) {
[email protected]aa96d3a2010-08-21 08:45:251169 to_be_removed.push_back(extension->id());
[email protected]05aad2da2011-10-28 10:12:371170 }
[email protected]aa96d3a2010-08-21 08:45:251171 }
1172
1173 // UnloadExtension will change the extensions_ list. So, we should
1174 // call it outside the iterator loop.
1175 for (unsigned int i = 0; i < to_be_removed.size(); ++i)
[email protected]814a7bf0f2011-08-13 05:30:591176 UnloadExtension(to_be_removed[i], extension_misc::UNLOAD_REASON_DISABLE);
[email protected]aa96d3a2010-08-21 08:45:251177}
1178
[email protected]31206602011-04-13 23:07:321179void ExtensionService::CheckForUpdatesSoon() {
[email protected]c3cfb012011-04-06 22:07:351180 if (updater()) {
[email protected]94fde232012-04-27 10:22:301181 if (AreAllExternalProvidersReady()) {
1182 updater()->CheckSoon();
1183 } else {
1184 // Sync can start updating before all the external providers are ready
1185 // during startup. Start the update as soon as those providers are ready,
1186 // but not before.
1187 update_once_all_providers_are_ready_ = true;
1188 }
[email protected]c3cfb012011-04-06 22:07:351189 } else {
[email protected]31206602011-04-13 23:07:321190 LOG(WARNING) << "CheckForUpdatesSoon() called with auto-update turned off";
[email protected]c3cfb012011-04-06 22:07:351191 }
1192}
1193
[email protected]65f173552012-06-28 22:43:581194syncer::SyncError ExtensionService::MergeDataAndStartSyncing(
[email protected]3bdba0d2011-08-23 07:17:301195 syncable::ModelType type,
[email protected]65f173552012-06-28 22:43:581196 const syncer::SyncDataList& initial_sync_data,
1197 scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
1198 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) {
[email protected]5db9ada2012-04-11 13:48:201199 CHECK(sync_processor.get());
[email protected]5d36e052012-04-20 20:12:281200 CHECK(sync_error_factory.get());
[email protected]3bdba0d2011-08-23 07:17:301201
1202 switch (type) {
1203 case syncable::EXTENSIONS:
[email protected]5db9ada2012-04-11 13:48:201204 extension_sync_bundle_.SetupSync(sync_processor.release(),
[email protected]5d36e052012-04-20 20:12:281205 sync_error_factory.release(),
[email protected]5db9ada2012-04-11 13:48:201206 initial_sync_data);
[email protected]3bdba0d2011-08-23 07:17:301207 break;
1208
1209 case syncable::APPS:
[email protected]5d36e052012-04-20 20:12:281210 app_sync_bundle_.SetupSync(sync_processor.release(),
1211 sync_error_factory.release(),
1212 initial_sync_data);
[email protected]3bdba0d2011-08-23 07:17:301213 break;
1214
1215 default:
1216 LOG(FATAL) << "Got " << type << " ModelType";
1217 }
[email protected]3bdba0d2011-08-23 07:17:301218
[email protected]b1417ab2011-12-15 17:09:111219 // Process local extensions.
1220 // TODO(yoz): Determine whether pending extensions should be considered too.
1221 // See crbug.com/104399.
[email protected]65f173552012-06-28 22:43:581222 syncer::SyncDataList sync_data_list = GetAllSyncData(type);
1223 syncer::SyncChangeList sync_change_list;
1224 for (syncer::SyncDataList::const_iterator i = sync_data_list.begin();
[email protected]3bdba0d2011-08-23 07:17:301225 i != sync_data_list.end();
1226 ++i) {
[email protected]5db9ada2012-04-11 13:48:201227 switch (type) {
1228 case syncable::EXTENSIONS:
1229 sync_change_list.push_back(
1230 extension_sync_bundle_.CreateSyncChange(*i));
1231 break;
1232 case syncable::APPS:
1233 sync_change_list.push_back(app_sync_bundle_.CreateSyncChange(*i));
1234 break;
1235 default:
1236 LOG(FATAL) << "Got " << type << " ModelType";
1237 }
[email protected]3bdba0d2011-08-23 07:17:301238 }
[email protected]3bdba0d2011-08-23 07:17:301239
[email protected]5db9ada2012-04-11 13:48:201240
1241 if (type == syncable::EXTENSIONS) {
1242 extension_sync_bundle_.ProcessSyncChangeList(sync_change_list);
1243 } else if (type == syncable::APPS) {
1244 app_sync_bundle_.ProcessSyncChangeList(sync_change_list);
1245 }
[email protected]0ffd9e52012-01-26 21:44:341246
[email protected]65f173552012-06-28 22:43:581247 return syncer::SyncError();
[email protected]3bdba0d2011-08-23 07:17:301248}
1249
1250void ExtensionService::StopSyncing(syncable::ModelType type) {
[email protected]5db9ada2012-04-11 13:48:201251 if (type == syncable::APPS) {
1252 app_sync_bundle_.Reset();
1253 } else if (type == syncable::EXTENSIONS) {
1254 extension_sync_bundle_.Reset();
1255 }
[email protected]3bdba0d2011-08-23 07:17:301256}
1257
[email protected]65f173552012-06-28 22:43:581258syncer::SyncDataList ExtensionService::GetAllSyncData(
[email protected]cb02f612012-06-27 03:15:501259 syncable::ModelType type) const {
[email protected]5db9ada2012-04-11 13:48:201260 if (type == syncable::EXTENSIONS) {
1261 return extension_sync_bundle_.GetAllSyncData();
1262
1263 } else if (type == syncable::APPS) {
1264 return app_sync_bundle_.GetAllSyncData();
[email protected]3bdba0d2011-08-23 07:17:301265 }
[email protected]5db9ada2012-04-11 13:48:201266
1267 // We should only get sync data for extensions and apps.
1268 NOTREACHED();
1269
[email protected]65f173552012-06-28 22:43:581270 return syncer::SyncDataList();
[email protected]3bdba0d2011-08-23 07:17:301271}
1272
[email protected]65f173552012-06-28 22:43:581273syncer::SyncError ExtensionService::ProcessSyncChanges(
[email protected]3bdba0d2011-08-23 07:17:301274 const tracked_objects::Location& from_here,
[email protected]65f173552012-06-28 22:43:581275 const syncer::SyncChangeList& change_list) {
1276 for (syncer::SyncChangeList::const_iterator i = change_list.begin();
[email protected]3bdba0d2011-08-23 07:17:301277 i != change_list.end();
1278 ++i) {
[email protected]5db9ada2012-04-11 13:48:201279 syncable::ModelType type = i->sync_data().GetDataType();
1280 if (type == syncable::EXTENSIONS) {
1281 extension_sync_bundle_.ProcessSyncChange(
1282 extensions::ExtensionSyncData(*i));
1283 } else if (type == syncable::APPS) {
1284 app_sync_bundle_.ProcessSyncChange(extensions::AppSyncData(*i));
1285 }
[email protected]3bdba0d2011-08-23 07:17:301286 }
1287
[email protected]0ffd9e52012-01-26 21:44:341288 extension_prefs()->extension_sorting()->FixNTPOrdinalCollisions();
1289
[email protected]65f173552012-06-28 22:43:581290 return syncer::SyncError();
[email protected]b05fb9ff2011-04-23 00:07:561291}
1292
[email protected]5db9ada2012-04-11 13:48:201293extensions::ExtensionSyncData ExtensionService::GetExtensionSyncData(
1294 const Extension& extension) const {
1295 return extensions::ExtensionSyncData(extension,
1296 IsExtensionEnabled(extension.id()),
1297 IsIncognitoEnabled(extension.id()));
[email protected]b05fb9ff2011-04-23 00:07:561298}
1299
[email protected]5db9ada2012-04-11 13:48:201300extensions::AppSyncData ExtensionService::GetAppSyncData(
1301 const Extension& extension) const {
1302 return extensions::AppSyncData(
1303 extension,
1304 IsExtensionEnabled(extension.id()),
1305 IsIncognitoEnabled(extension.id()),
1306 extension_prefs_->GetAppNotificationClientId(extension.id()),
1307 extension_prefs_->IsAppNotificationDisabled(extension.id()),
1308 extension_prefs_->extension_sorting()->GetAppLaunchOrdinal(
1309 extension.id()),
1310 extension_prefs_->extension_sorting()->GetPageOrdinal(extension.id()));
1311}
[email protected]3bdba0d2011-08-23 07:17:301312
[email protected]5db9ada2012-04-11 13:48:201313std::vector<extensions::ExtensionSyncData>
1314 ExtensionService::GetExtensionSyncDataList() const {
1315 std::vector<extensions::ExtensionSyncData> extension_sync_list;
1316 extension_sync_bundle_.GetExtensionSyncDataListHelper(extensions_,
1317 &extension_sync_list);
1318 extension_sync_bundle_.GetExtensionSyncDataListHelper(disabled_extensions_,
1319 &extension_sync_list);
1320 extension_sync_bundle_.GetExtensionSyncDataListHelper(terminated_extensions_,
1321 &extension_sync_list);
1322
1323 std::vector<extensions::ExtensionSyncData> pending_extensions =
1324 extension_sync_bundle_.GetPendingData();
1325 extension_sync_list.insert(extension_sync_list.begin(),
1326 pending_extensions.begin(),
1327 pending_extensions.end());
[email protected]3bdba0d2011-08-23 07:17:301328
1329 return extension_sync_list;
[email protected]b05fb9ff2011-04-23 00:07:561330}
1331
[email protected]5db9ada2012-04-11 13:48:201332std::vector<extensions::AppSyncData> ExtensionService::GetAppSyncDataList()
1333 const {
1334 std::vector<extensions::AppSyncData> app_sync_list;
1335 app_sync_bundle_.GetAppSyncDataListHelper(extensions_, &app_sync_list);
1336 app_sync_bundle_.GetAppSyncDataListHelper(disabled_extensions_,
1337 &app_sync_list);
1338 app_sync_bundle_.GetAppSyncDataListHelper(terminated_extensions_,
1339 &app_sync_list);
1340
1341 std::vector<extensions::AppSyncData> pending_apps =
1342 app_sync_bundle_.GetPendingData();
1343 app_sync_list.insert(app_sync_list.begin(),
1344 pending_apps.begin(),
1345 pending_apps.end());
1346
1347 return app_sync_list;
1348}
1349
1350bool ExtensionService::ProcessExtensionSyncData(
1351 const extensions::ExtensionSyncData& extension_sync_data) {
1352 if (!ProcessExtensionSyncDataHelper(extension_sync_data,
1353 syncable::EXTENSIONS)) {
1354 extension_sync_bundle_.AddPendingExtension(extension_sync_data.id(),
1355 extension_sync_data);
1356 CheckForUpdatesSoon();
1357 return false;
1358 }
1359
1360 return true;
1361}
1362
1363bool ExtensionService::ProcessAppSyncData(
1364 const extensions::AppSyncData& app_sync_data) {
1365 const std::string& id = app_sync_data.id();
1366 const Extension* extension = GetInstalledExtension(id);
1367 bool extension_installed = (extension != NULL);
1368
1369 if (app_sync_data.app_launch_ordinal().IsValid() &&
1370 app_sync_data.page_ordinal().IsValid()) {
1371 extension_prefs_->extension_sorting()->SetAppLaunchOrdinal(
1372 id,
1373 app_sync_data.app_launch_ordinal());
1374 extension_prefs_->extension_sorting()->SetPageOrdinal(
1375 id,
1376 app_sync_data.page_ordinal());
1377 }
1378
1379 if (extension_installed) {
1380 if (app_sync_data.notifications_disabled() !=
1381 extension_prefs_->IsAppNotificationDisabled(id)) {
1382 extension_prefs_->SetAppNotificationDisabled(
1383 id, app_sync_data.notifications_disabled());
1384 }
1385 }
1386
1387 if (!ProcessExtensionSyncDataHelper(app_sync_data.extension_sync_data(),
1388 syncable::APPS)) {
1389 app_sync_bundle_.AddPendingApp(id, app_sync_data);
1390 CheckForUpdatesSoon();
1391 return false;
1392 }
1393
1394 return true;
1395}
1396
1397bool ExtensionService::IsCorrectSyncType(const Extension& extension,
1398 syncable::ModelType type) const {
1399 if (type == syncable::EXTENSIONS &&
1400 extension.GetSyncType() == Extension::SYNC_TYPE_EXTENSION)
1401 return true;
1402
1403 if (type == syncable::APPS &&
1404 extension.GetSyncType() == Extension::SYNC_TYPE_APP)
1405 return true;
1406
1407 return false;
1408}
1409
1410bool ExtensionService::ProcessExtensionSyncDataHelper(
1411 const extensions::ExtensionSyncData& extension_sync_data,
1412 syncable::ModelType type) {
[email protected]3bdba0d2011-08-23 07:17:301413 const std::string& id = extension_sync_data.id();
[email protected]96e989b2011-08-30 19:35:061414 const Extension* extension = GetInstalledExtension(id);
1415
1416 // TODO(bolms): we should really handle this better. The particularly bad
1417 // case is where an app becomes an extension or vice versa, and we end up with
1418 // a zombie extension that won't go away.
[email protected]5db9ada2012-04-11 13:48:201419 if (extension && !IsCorrectSyncType(*extension, type))
1420 return true;
[email protected]90310d92011-04-17 07:35:041421
1422 // Handle uninstalls first.
[email protected]3bdba0d2011-08-23 07:17:301423 if (extension_sync_data.uninstalled()) {
[email protected]90310d92011-04-17 07:35:041424 std::string error;
1425 if (!UninstallExtensionHelper(this, id)) {
1426 LOG(WARNING) << "Could not uninstall extension " << id
1427 << " for sync";
1428 }
[email protected]5db9ada2012-04-11 13:48:201429 return true;
[email protected]90310d92011-04-17 07:35:041430 }
1431
[email protected]caf2d442012-05-04 10:00:501432 // Extension from sync was uninstalled by the user as external extensions.
1433 // Honor user choice and skip installation/enabling.
1434 if (IsExternalExtensionUninstalled(id)) {
1435 LOG(WARNING) << "Extension with id " << id
1436 << " from sync was uninstalled as external extension";
1437 return true;
1438 }
1439
[email protected]06f92562011-04-29 19:27:311440 // Set user settings.
[email protected]3bdba0d2011-08-23 07:17:301441 if (extension_sync_data.enabled()) {
[email protected]06f92562011-04-29 19:27:311442 EnableExtension(id);
1443 } else {
[email protected]44d62b62012-04-11 00:06:031444 DisableExtension(id, Extension::DISABLE_USER_ACTION);
[email protected]06f92562011-04-29 19:27:311445 }
[email protected]81b14cc2011-04-29 00:39:371446
[email protected]406b5a92011-11-08 11:58:261447 // We need to cache some version information here because setting the
1448 // incognito flag invalidates the |extension| pointer (it reloads the
1449 // extension).
[email protected]9060d8b02012-01-13 02:14:301450 bool extension_installed = (extension != NULL);
[email protected]406b5a92011-11-08 11:58:261451 int result = extension ?
1452 extension->version()->CompareTo(extension_sync_data.version()) : 0;
1453 SetIsIncognitoEnabled(id, extension_sync_data.incognito_enabled());
1454 extension = NULL; // No longer safe to use.
1455
1456 if (extension_installed) {
[email protected]06f92562011-04-29 19:27:311457 // If the extension is already installed, check if it's outdated.
[email protected]90310d92011-04-17 07:35:041458 if (result < 0) {
1459 // Extension is outdated.
[email protected]5db9ada2012-04-11 13:48:201460 return false;
[email protected]b020dc12011-12-06 04:35:421461 }
[email protected]06f92562011-04-29 19:27:311462 } else {
[email protected]06f92562011-04-29 19:27:311463 // TODO(akalin): Replace silent update with a list of enabled
1464 // permissions.
[email protected]6cc7dbae2011-04-29 21:18:331465 const bool kInstallSilently = true;
[email protected]5db9ada2012-04-11 13:48:201466
1467 CHECK(type == syncable::EXTENSIONS || type == syncable::APPS);
1468 ExtensionFilter filter =
1469 (type == syncable::APPS) ? IsSyncableApp : IsSyncableExtension;
1470
[email protected]06f92562011-04-29 19:27:311471 if (!pending_extension_manager()->AddFromSync(
1472 id,
[email protected]3bdba0d2011-08-23 07:17:301473 extension_sync_data.update_url(),
[email protected]5db9ada2012-04-11 13:48:201474 filter,
[email protected]6cc7dbae2011-04-29 21:18:331475 kInstallSilently)) {
[email protected]06f92562011-04-29 19:27:311476 LOG(WARNING) << "Could not add pending extension for " << id;
[email protected]3bdba0d2011-08-23 07:17:301477 // This means that the extension is already pending installation, with a
1478 // non-INTERNAL location. Add to pending_sync_data, even though it will
1479 // never be removed (we'll never install a syncable version of the
1480 // extension), so that GetAllSyncData() continues to send it.
[email protected]06f92562011-04-29 19:27:311481 }
[email protected]3bdba0d2011-08-23 07:17:301482 // Track pending extensions so that we can return them in GetAllSyncData().
[email protected]5db9ada2012-04-11 13:48:201483 return false;
[email protected]90310d92011-04-17 07:35:041484 }
[email protected]5db9ada2012-04-11 13:48:201485
1486 return true;
[email protected]90310d92011-04-17 07:35:041487}
1488
[email protected]c3cfb012011-04-06 22:07:351489bool ExtensionService::IsIncognitoEnabled(
1490 const std::string& extension_id) const {
1491 // If this is an existing component extension we always allow it to
1492 // work in incognito mode.
[email protected]81b14cc2011-04-29 00:39:371493 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]c3cfb012011-04-06 22:07:351494 if (extension && extension->location() == Extension::COMPONENT)
[email protected]cb0ce1e022010-03-10 19:54:411495 return true;
1496
1497 // Check the prefs.
[email protected]c3cfb012011-04-06 22:07:351498 return extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]db7331a2010-02-25 22:10:501499}
[email protected]55a35692010-02-11 23:25:211500
[email protected]c3cfb012011-04-06 22:07:351501void ExtensionService::SetIsIncognitoEnabled(
1502 const std::string& extension_id, bool enabled) {
[email protected]81b14cc2011-04-29 00:39:371503 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]c3cfb012011-04-06 22:07:351504 if (extension && extension->location() == Extension::COMPONENT) {
[email protected]b873cd92012-02-09 21:51:481505 // This shouldn't be called for component extensions unless they are
1506 // syncable.
1507 DCHECK(extension->IsSyncable());
1508
1509 // If we are here, make sure the we aren't trying to change the value.
1510 DCHECK_EQ(enabled, IsIncognitoEnabled(extension_id));
1511
[email protected]c3cfb012011-04-06 22:07:351512 return;
1513 }
1514
[email protected]8c6c8c22011-03-09 12:52:341515 // Broadcast unloaded and loaded events to update browser state. Only bother
1516 // if the value changed and the extension is actually enabled, since there is
1517 // no UI otherwise.
[email protected]c3cfb012011-04-06 22:07:351518 bool old_enabled = extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]8c6c8c22011-03-09 12:52:341519 if (enabled == old_enabled)
1520 return;
1521
[email protected]c3cfb012011-04-06 22:07:351522 extension_prefs_->SetIsIncognitoEnabled(extension_id, enabled);
[email protected]81b14cc2011-04-29 00:39:371523
[email protected]ffbf3f12011-12-09 22:10:331524 bool extension_is_enabled = extensions_.Contains(extension_id);
[email protected]bd9892eb2011-11-11 19:41:341525
1526 // When we reload the extension the ID may be invalidated if we've passed it
1527 // by const ref everywhere. Make a copy to be safe.
1528 std::string id = extension_id;
[email protected]406b5a92011-11-08 11:58:261529 if (extension_is_enabled)
[email protected]ffbf3f12011-12-09 22:10:331530 ReloadExtension(id);
[email protected]3bdba0d2011-08-23 07:17:301531
[email protected]406b5a92011-11-08 11:58:261532 // Reloading the extension invalidates the |extension| pointer.
[email protected]bd9892eb2011-11-11 19:41:341533 extension = GetInstalledExtension(id);
[email protected]3bdba0d2011-08-23 07:17:301534 if (extension)
1535 SyncExtensionChangeIfNeeded(*extension);
[email protected]55a35692010-02-11 23:25:211536}
1537
[email protected]aa7599d2011-10-28 07:24:321538void ExtensionService::SetAppNotificationSetupDone(
1539 const std::string& extension_id,
[email protected]b2689a902011-12-01 00:41:091540 const std::string& oauth_client_id) {
[email protected]aa7599d2011-10-28 07:24:321541 const Extension* extension = GetInstalledExtension(extension_id);
1542 // This method is called when the user sets up app notifications.
1543 // So it is not expected to be called until the extension is installed.
1544 if (!extension) {
1545 NOTREACHED();
1546 return;
1547 }
[email protected]b2689a902011-12-01 00:41:091548 extension_prefs_->SetAppNotificationClientId(extension_id, oauth_client_id);
[email protected]aa7599d2011-10-28 07:24:321549 SyncExtensionChangeIfNeeded(*extension);
1550}
1551
1552void ExtensionService::SetAppNotificationDisabled(
1553 const std::string& extension_id,
1554 bool value) {
1555 const Extension* extension = GetInstalledExtension(extension_id);
1556 // This method is called when the user enables/disables app notifications.
1557 // So it is not expected to be called until the extension is installed.
1558 if (!extension) {
1559 NOTREACHED();
1560 return;
1561 }
[email protected]97e856b12011-12-14 00:21:131562 if (value)
1563 UMA_HISTOGRAM_COUNTS("Apps.SetAppNotificationsDisabled", 1);
1564 else
1565 UMA_HISTOGRAM_COUNTS("Apps.SetAppNotificationsEnabled", 1);
[email protected]aa7599d2011-10-28 07:24:321566 extension_prefs_->SetAppNotificationDisabled(extension_id, value);
1567 SyncExtensionChangeIfNeeded(*extension);
1568}
1569
[email protected]eaa7dd182010-12-14 11:09:001570bool ExtensionService::CanCrossIncognito(const Extension* extension) {
[email protected]2a8f24e2010-11-03 21:37:051571 // We allow the extension to see events and data from another profile iff it
1572 // uses "spanning" behavior and it has incognito access. "split" mode
1573 // extensions only see events for a matching profile.
[email protected]0c41298f2011-09-13 21:17:221574 CHECK(extension);
[email protected]c3cfb012011-04-06 22:07:351575 return IsIncognitoEnabled(extension->id()) &&
1576 !extension->incognito_split_mode();
[email protected]2a8f24e2010-11-03 21:37:051577}
1578
[email protected]5ef7b002011-05-12 23:16:201579bool ExtensionService::CanLoadInIncognito(const Extension* extension) const {
1580 if (extension->is_hosted_app())
1581 return true;
1582 // Packaged apps and regular extensions need to be enabled specifically for
1583 // incognito (and split mode should be set).
1584 return extension->incognito_split_mode() &&
1585 IsIncognitoEnabled(extension->id());
1586}
1587
[email protected]168389f2011-12-20 17:12:481588void ExtensionService::OnExtensionMoved(
1589 const std::string& moved_extension_id,
1590 const std::string& predecessor_extension_id,
1591 const std::string& successor_extension_id) {
[email protected]3569b502012-01-12 20:08:231592 extension_prefs_->extension_sorting()->OnExtensionMoved(
1593 moved_extension_id,
1594 predecessor_extension_id,
1595 successor_extension_id);
[email protected]168389f2011-12-20 17:12:481596
1597 const Extension* extension = GetInstalledExtension(moved_extension_id);
1598 if (extension)
1599 SyncExtensionChangeIfNeeded(*extension);
1600}
1601
[email protected]eaa7dd182010-12-14 11:09:001602bool ExtensionService::AllowFileAccess(const Extension* extension) {
[email protected]05c82182010-06-24 17:49:081603 return (CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]334e04a2010-06-24 23:34:441604 switches::kDisableExtensionsFileAccessCheck) ||
[email protected]05c82182010-06-24 17:49:081605 extension_prefs_->AllowFileAccess(extension->id()));
1606}
1607
[email protected]eaa7dd182010-12-14 11:09:001608void ExtensionService::SetAllowFileAccess(const Extension* extension,
[email protected]3aff9ad2011-04-01 20:26:481609 bool allow) {
1610 // Reload to update browser state. Only bother if the value changed and the
1611 // extension is actually enabled, since there is no UI otherwise.
1612 bool old_allow = AllowFileAccess(extension);
1613 if (allow == old_allow)
1614 return;
1615
[email protected]05c82182010-06-24 17:49:081616 extension_prefs_->SetAllowFileAccess(extension->id(), allow);
[email protected]3aff9ad2011-04-01 20:26:481617
[email protected]84df8332011-12-06 18:22:461618 bool extension_is_enabled = extensions_.Contains(extension->id());
[email protected]3aff9ad2011-04-01 20:26:481619 if (extension_is_enabled)
1620 ReloadExtension(extension->id());
[email protected]05c82182010-06-24 17:49:081621}
1622
[email protected]8e4560b62011-01-14 10:09:141623// Some extensions will autoupdate themselves externally from Chrome. These
1624// are typically part of some larger client application package. To support
1625// these, the extension will register its location in the the preferences file
1626// (and also, on Windows, in the registry) and this code will periodically
1627// check that location for a .crx file, which it will then install locally if
1628// a new version is available.
1629// Errors are reported through ExtensionErrorReporter. Succcess is not
1630// reported.
[email protected]eaa7dd182010-12-14 11:09:001631void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:121632 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]8e4560b62011-01-14 10:09:141633
1634 // Note that this installation is intentionally silent (since it didn't
1635 // go through the front-end). Extensions that are registered in this
1636 // way are effectively considered 'pre-bundled', and so implicitly
1637 // trusted. In general, if something has HKLM or filesystem access,
1638 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:121639
[email protected]8e4560b62011-01-14 10:09:141640 // Ask each external extension provider to give us a call back for each
1641 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
1642 ProviderCollection::const_iterator i;
1643 for (i = external_extension_providers_.begin();
1644 i != external_extension_providers_.end(); ++i) {
1645 ExternalExtensionProviderInterface* provider = i->get();
1646 provider->VisitRegisteredExtension();
1647 }
1648
[email protected]50067e52011-10-20 23:17:071649 // Do any required work that we would have done after completion of all
1650 // providers.
1651 if (external_extension_providers_.empty()) {
1652 OnAllExternalProvidersReady();
1653 }
[email protected]9f1087e2009-06-15 17:29:321654}
1655
[email protected]50067e52011-10-20 23:17:071656void ExtensionService::OnExternalProviderReady(
1657 const ExternalExtensionProviderInterface* provider) {
[email protected]a29a517a2011-01-21 21:11:121658 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]50067e52011-10-20 23:17:071659 CHECK(provider->IsReady());
[email protected]a29a517a2011-01-21 21:11:121660
1661 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:141662 // if all of them are finished. So we check them first.
[email protected]94fde232012-04-27 10:22:301663 if (AreAllExternalProvidersReady())
1664 OnAllExternalProvidersReady();
1665}
1666
1667bool ExtensionService::AreAllExternalProvidersReady() const {
[email protected]8e4560b62011-01-14 10:09:141668 ProviderCollection::const_iterator i;
1669 for (i = external_extension_providers_.begin();
1670 i != external_extension_providers_.end(); ++i) {
[email protected]94fde232012-04-27 10:22:301671 if (!i->get()->IsReady())
1672 return false;
[email protected]8e4560b62011-01-14 10:09:141673 }
[email protected]94fde232012-04-27 10:22:301674 return true;
[email protected]50067e52011-10-20 23:17:071675}
1676
1677void ExtensionService::OnAllExternalProvidersReady() {
1678 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]94fde232012-04-27 10:22:301679 base::TimeDelta elapsed = base::Time::Now() - profile_->GetStartTime();
1680 UMA_HISTOGRAM_TIMES("Extension.ExternalProvidersReadyAfter", elapsed);
[email protected]50067e52011-10-20 23:17:071681
1682 // Install any pending extensions.
[email protected]94fde232012-04-27 10:22:301683 if (update_once_all_providers_are_ready_ && updater()) {
1684 update_once_all_providers_are_ready_ = false;
[email protected]8e4560b62011-01-14 10:09:141685 updater()->CheckNow();
1686 }
1687
1688 // Uninstall all the unclaimed extensions.
1689 scoped_ptr<ExtensionPrefs::ExtensionsInfo> extensions_info(
1690 extension_prefs_->GetInstalledExtensionsInfo());
1691 for (size_t i = 0; i < extensions_info->size(); ++i) {
1692 ExtensionInfo* info = extensions_info->at(i).get();
1693 if (Extension::IsExternalLocation(info->extension_location))
1694 CheckExternalUninstall(info->extension_id);
1695 }
[email protected]e5af875f2011-10-10 21:09:141696 IdentifyAlertableExtensions();
[email protected]e5af875f2011-10-10 21:09:141697}
1698
1699void ExtensionService::IdentifyAlertableExtensions() {
1700 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1701
[email protected]07c9f2f42012-02-29 18:45:221702 // Build up the lists of extensions that require acknowledgment. If this is
1703 // the first time, grandfather extensions that would have caused
1704 // notification.
[email protected]a9aa5932012-01-25 08:27:401705 extension_global_error_.reset(new ExtensionGlobalError(this));
[email protected]e5af875f2011-10-10 21:09:141706
[email protected]a9aa5932012-01-25 08:27:401707 bool did_show_alert = false;
[email protected]07c9f2f42012-02-29 18:45:221708 if (PopulateExtensionGlobalError(extension_global_error_.get())) {
[email protected]e5af875f2011-10-10 21:09:141709 if (extension_prefs_->SetAlertSystemFirstRun()) {
[email protected]a9aa5932012-01-25 08:27:401710 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]d8748142012-05-16 21:13:431711 Browser* browser = browser::FindLastActiveWithProfile(profile_);
[email protected]a9aa5932012-01-25 08:27:401712 if (browser) {
1713 extension_global_error_->ShowBubbleView(browser);
1714 did_show_alert = true;
1715 }
[email protected]e5af875f2011-10-10 21:09:141716 } else {
1717 // First run. Just acknowledge all the extensions, silently, by
1718 // shortcutting the display of the UI and going straight to the
1719 // callback for pressing the Accept button.
[email protected]a9aa5932012-01-25 08:27:401720 HandleExtensionAlertAccept();
[email protected]e5af875f2011-10-10 21:09:141721 }
1722 }
[email protected]a9aa5932012-01-25 08:27:401723
1724 if (!did_show_alert)
1725 extension_global_error_.reset();
[email protected]e5af875f2011-10-10 21:09:141726}
1727
[email protected]07c9f2f42012-02-29 18:45:221728bool ExtensionService::PopulateExtensionGlobalError(
1729 ExtensionGlobalError* extension_global_error) {
1730 bool needs_alert = false;
1731 for (ExtensionSet::const_iterator iter = extensions_.begin();
1732 iter != extensions_.end(); ++iter) {
1733 const Extension* e = *iter;
1734 if (Extension::IsExternalLocation(e->location())) {
1735 if (!e->is_hosted_app()) {
1736 if (!extension_prefs_->IsExternalExtensionAcknowledged(e->id())) {
1737 extension_global_error->AddExternalExtension(e->id());
1738 needs_alert = true;
1739 }
1740 }
1741 }
1742 if (extension_prefs_->IsExtensionBlacklisted(e->id())) {
1743 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(e->id())) {
1744 extension_global_error->AddBlacklistedExtension(e->id());
1745 needs_alert = true;
1746 }
1747 }
1748 if (extension_prefs_->IsExtensionOrphaned(e->id())) {
1749 if (!extension_prefs_->IsOrphanedExtensionAcknowledged(e->id())) {
1750 extension_global_error->AddOrphanedExtension(e->id());
1751 needs_alert = true;
1752 }
1753 }
1754 }
1755 return needs_alert;
1756}
1757
[email protected]a9aa5932012-01-25 08:27:401758void ExtensionService::HandleExtensionAlertClosed() {
1759 extension_global_error_.reset();
[email protected]62433d32011-10-12 22:33:121760}
1761
[email protected]a9aa5932012-01-25 08:27:401762void ExtensionService::HandleExtensionAlertAccept() {
[email protected]e5af875f2011-10-10 21:09:141763 const ExtensionIdSet *extension_ids =
[email protected]a9aa5932012-01-25 08:27:401764 extension_global_error_->get_external_extension_ids();
[email protected]e5af875f2011-10-10 21:09:141765 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1766 iter != extension_ids->end(); ++iter) {
[email protected]47fc70c2011-12-06 07:29:511767 AcknowledgeExternalExtension(*iter);
[email protected]e5af875f2011-10-10 21:09:141768 }
[email protected]a9aa5932012-01-25 08:27:401769 extension_ids = extension_global_error_->get_blacklisted_extension_ids();
[email protected]e5af875f2011-10-10 21:09:141770 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1771 iter != extension_ids->end(); ++iter) {
1772 extension_prefs_->AcknowledgeBlacklistedExtension(*iter);
1773 }
[email protected]a9aa5932012-01-25 08:27:401774 extension_ids = extension_global_error_->get_orphaned_extension_ids();
[email protected]e5af875f2011-10-10 21:09:141775 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1776 iter != extension_ids->end(); ++iter) {
1777 extension_prefs_->AcknowledgeOrphanedExtension(*iter);
1778 }
1779}
1780
[email protected]47fc70c2011-12-06 07:29:511781void ExtensionService::AcknowledgeExternalExtension(const std::string& id) {
1782 extension_prefs_->AcknowledgeExternalExtension(id);
1783}
1784
[email protected]a9aa5932012-01-25 08:27:401785void ExtensionService::HandleExtensionAlertDetails(Browser* browser) {
1786 DCHECK(browser);
[email protected]5d9cace72012-06-21 16:07:121787 chrome::ShowExtensions(browser);
[email protected]6c751e72010-11-23 10:11:101788}
1789
[email protected]a9f39a312010-12-23 22:14:271790void ExtensionService::UnloadExtension(
1791 const std::string& extension_id,
[email protected]814a7bf0f2011-08-13 05:30:591792 extension_misc::UnloadedExtensionReason reason) {
[email protected]27e469a2010-01-11 20:35:091793 // Make sure the extension gets deleted after we return from this function.
[email protected]9adb9692010-10-29 23:14:021794 scoped_refptr<const Extension> extension(
[email protected]8001df22011-04-28 19:59:471795 GetExtensionByIdInternal(extension_id, true, true, false));
[email protected]631cf822009-05-15 07:01:251796
[email protected]fa0c96732010-11-17 00:14:231797 // This method can be called via PostTask, so the extension may have been
1798 // unloaded by the time this runs.
[email protected]dd163fb02011-05-04 22:22:171799 if (!extension) {
1800 // In case the extension may have crashed/uninstalled. Allow the profile to
1801 // clean up its RequestContexts.
[email protected]31d8f5f22012-04-02 15:22:081802 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]fa0c96732010-11-17 00:14:231803 return;
[email protected]dd163fb02011-05-04 22:22:171804 }
[email protected]0c6da502009-08-14 22:32:391805
[email protected]1eb175082010-02-10 09:26:161806 // Keep information about the extension so that we can reload it later
1807 // even if it's not permanently installed.
1808 unloaded_extension_paths_[extension->id()] = extension->path();
1809
[email protected]f17dbd42010-08-16 23:21:101810 // Clean up if the extension is meant to be enabled after a reload.
1811 disabled_extension_paths_.erase(extension->id());
1812
[email protected]d7e9a862010-11-03 21:57:491813 // Clean up runtime data.
1814 extension_runtime_data_.erase(extension_id);
1815
[email protected]32fc4ff72012-06-15 21:50:011816if (disabled_extensions_.Contains(extension->id())) {
[email protected]a9f39a312010-12-23 22:14:271817 UnloadedExtensionInfo details(extension, reason);
1818 details.already_disabled = true;
[email protected]84df8332011-12-06 18:22:461819 disabled_extensions_.Remove(extension->id());
[email protected]ad50def52011-10-19 23:17:071820 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271821 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]6c2381d2011-10-19 02:52:531822 content::Source<Profile>(profile_),
1823 content::Details<UnloadedExtensionInfo>(&details));
[email protected]dd163fb02011-05-04 22:22:171824 // Make sure the profile cleans up its RequestContexts when an already
1825 // disabled extension is unloaded (since they are also tracking the disabled
1826 // extensions).
[email protected]31d8f5f22012-04-02 15:22:081827 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]0c6da502009-08-14 22:32:391828 return;
1829 }
1830
[email protected]84df8332011-12-06 18:22:461831// Remove the extension from our list.
1832 extensions_.Remove(extension->id());
[email protected]631cf822009-05-15 07:01:251833
[email protected]a9f39a312010-12-23 22:14:271834 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]631cf822009-05-15 07:01:251835}
1836
[email protected]eaa7dd182010-12-14 11:09:001837void ExtensionService::UnloadAllExtensions() {
[email protected]c8d407e2011-04-28 21:27:171838 profile_->GetExtensionSpecialStoragePolicy()->
1839 RevokeRightsForAllExtensions();
1840
[email protected]84df8332011-12-06 18:22:461841 extensions_.Clear();
1842 disabled_extensions_.Clear();
1843 terminated_extensions_.Clear();
[email protected]d7e9a862010-11-03 21:57:491844 extension_runtime_data_.clear();
[email protected]cd500f72010-06-25 23:44:321845
[email protected]c6e4a3412009-06-24 15:45:291846 // TODO(erikkay) should there be a notification for this? We can't use
1847 // EXTENSION_UNLOADED since that implies that the extension has been disabled
1848 // or uninstalled, and UnloadAll is just part of shutdown.
[email protected]9f1087e2009-06-15 17:29:321849}
1850
[email protected]eaa7dd182010-12-14 11:09:001851void ExtensionService::ReloadExtensions() {
[email protected]9f1087e2009-06-15 17:29:321852 UnloadAllExtensions();
[email protected]d8c8f25f2011-11-02 18:18:011853 component_loader_->LoadAll();
1854 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]9f1087e2009-06-15 17:29:321855}
1856
[email protected]eaa7dd182010-12-14 11:09:001857void ExtensionService::GarbageCollectExtensions() {
[email protected]2f8757c32012-06-19 19:17:471858 if (extension_prefs_->pref_service()->ReadOnly())
1859 return;
1860
1861 scoped_ptr<ExtensionPrefs::ExtensionsInfo> info(
1862 extension_prefs_->GetInstalledExtensionsInfo());
1863
1864 std::map<std::string, FilePath> extension_paths;
1865 for (size_t i = 0; i < info->size(); ++i)
1866 extension_paths[info->at(i)->extension_id] = info->at(i)->extension_path;
1867
1868 if (!BrowserThread::PostTask(
1869 BrowserThread::FILE, FROM_HERE,
1870 base::Bind(
1871 &extension_file_util::GarbageCollectExtensions,
1872 install_directory_,
1873 extension_paths)))
1874 NOTREACHED();
[email protected]f6ccd652010-11-17 00:48:341875
1876 // Also garbage-collect themes. We check |profile_| to be
1877 // defensive; in the future, we may call GarbageCollectExtensions()
1878 // from somewhere other than Init() (e.g., in a timer).
1879 if (profile_) {
[email protected]18280372011-03-22 18:05:221880 ThemeServiceFactory::GetForProfile(profile_)->RemoveUnusedThemes();
[email protected]f6ccd652010-11-17 00:48:341881 }
[email protected]3cf4f0992009-02-03 23:00:301882}
1883
[email protected]fb82dcd2012-03-21 14:15:461884void ExtensionService::SyncExtensionChangeIfNeeded(const Extension& extension) {
[email protected]5db9ada2012-04-11 13:48:201885 if (app_sync_bundle_.HandlesApp(extension)) {
1886 app_sync_bundle_.SyncChangeIfNeeded(extension);
1887 } else if (extension_sync_bundle_.HandlesExtension(extension)) {
1888 extension_sync_bundle_.SyncChangeIfNeeded(extension);
[email protected]fb82dcd2012-03-21 14:15:461889 }
1890}
1891
[email protected]eaa7dd182010-12-14 11:09:001892void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]93fd78f42009-07-10 16:43:171893 if (updater_.get()) {
1894 updater_->Start();
1895 }
[email protected]fa6a9102010-11-22 15:38:501896
1897 ready_ = true;
[email protected]ad50def52011-10-19 23:17:071898 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271899 chrome::NOTIFICATION_EXTENSIONS_READY,
[email protected]6c2381d2011-10-19 02:52:531900 content::Source<Profile>(profile_),
[email protected]ad50def52011-10-19 23:17:071901 content::NotificationService::NoDetails());
[email protected]e72e8eb82009-06-18 17:21:511902}
1903
[email protected]fcb58a862012-05-01 01:03:151904void ExtensionService::AddExtension(const Extension* extension) {
[email protected]ae09ca62009-08-21 19:46:461905 // Ensure extension is deleted unless we transfer ownership.
[email protected]9adb9692010-10-29 23:14:021906 scoped_refptr<const Extension> scoped_extension(extension);
[email protected]9f1087e2009-06-15 17:29:321907
[email protected]c8d407e2011-04-28 21:27:171908 // TODO(jstritar): We may be able to get rid of this branch by overriding the
1909 // default extension state to DISABLED when the --disable-extensions flag
1910 // is set (http://crbug.com/29067).
1911 if (!extensions_enabled() &&
1912 !extension->is_theme() &&
1913 extension->location() != Extension::COMPONENT &&
1914 !Extension::IsExternalLocation(extension->location()))
[email protected]fcb58a862012-05-01 01:03:151915 return;
[email protected]c8d407e2011-04-28 21:27:171916
1917 SetBeingUpgraded(extension, false);
1918
[email protected]1eb175082010-02-10 09:26:161919 // The extension is now loaded, remove its data from unloaded extension map.
1920 unloaded_extension_paths_.erase(extension->id());
1921
[email protected]bb7f40952011-01-13 00:21:201922 // If a terminated extension is loaded, remove it from the terminated list.
1923 UntrackTerminatedExtension(extension->id());
1924
[email protected]f17dbd42010-08-16 23:21:101925 // If the extension was disabled for a reload, then enable it.
1926 if (disabled_extension_paths_.erase(extension->id()) > 0)
1927 EnableExtension(extension->id());
1928
[email protected]d728e002010-12-08 04:46:231929 // Check if the extension's privileges have changed and disable the
1930 // extension if necessary.
[email protected]902fd7b2011-07-27 18:42:311931 InitializePermissions(extension);
[email protected]0c6da502009-08-14 22:32:391932
[email protected]ad83ca242011-07-29 01:32:251933 bool disabled = extension_prefs_->IsExtensionDisabled(extension->id());
[email protected]36429da2011-07-11 20:25:181934 if (disabled) {
[email protected]84df8332011-12-06 18:22:461935 disabled_extensions_.Insert(scoped_extension);
[email protected]3bdba0d2011-08-23 07:17:301936 SyncExtensionChangeIfNeeded(*extension);
[email protected]44d62b62012-04-11 00:06:031937 content::NotificationService::current()->Notify(
1938 chrome::NOTIFICATION_EXTENSION_UPDATE_DISABLED,
1939 content::Source<Profile>(profile_),
1940 content::Details<const Extension>(extension));
[email protected]62f051c2012-03-29 17:04:441941
[email protected]44d62b62012-04-11 00:06:031942 if (extension_prefs_->GetDisableReason(extension->id()) ==
1943 Extension::DISABLE_PERMISSIONS_INCREASE) {
[email protected]62f051c2012-03-29 17:04:441944 extensions::AddExtensionDisabledError(this, extension);
1945 }
[email protected]fcb58a862012-05-01 01:03:151946 return;
[email protected]e72e8eb82009-06-18 17:21:511947 }
[email protected]aab98a52009-12-02 03:22:351948
[email protected]b873cd92012-02-09 21:51:481949 // All apps that are displayed in the launcher are ordered by their ordinals
1950 // so we must ensure they have valid ordinals.
1951 if (extension->ShouldDisplayInLauncher())
1952 extension_prefs_->extension_sorting()->EnsureValidOrdinals(extension->id());
1953
[email protected]84df8332011-12-06 18:22:461954 extensions_.Insert(scoped_extension);
[email protected]3bdba0d2011-08-23 07:17:301955 SyncExtensionChangeIfNeeded(*extension);
[email protected]c8d407e2011-04-28 21:27:171956 NotifyExtensionLoaded(extension);
[email protected]aab98a52009-12-02 03:22:351957}
1958
[email protected]902fd7b2011-07-27 18:42:311959void ExtensionService::InitializePermissions(const Extension* extension) {
1960 // If the extension has used the optional permissions API, it will have a
1961 // custom set of active permissions defined in the extension prefs. Here,
1962 // we update the extension's active permissions based on the prefs.
[email protected]c2e66e12012-06-27 06:27:061963 scoped_refptr<PermissionSet> active_permissions =
[email protected]902fd7b2011-07-27 18:42:311964 extension_prefs()->GetActivePermissions(extension->id());
1965
1966 if (active_permissions.get()) {
1967 // We restrict the active permissions to be within the bounds defined in the
1968 // extension's manifest.
1969 // a) active permissions must be a subset of optional + default permissions
1970 // b) active permissions must contains all default permissions
[email protected]c2e66e12012-06-27 06:27:061971 scoped_refptr<PermissionSet> total_permissions =
1972 PermissionSet::CreateUnion(
[email protected]902fd7b2011-07-27 18:42:311973 extension->required_permission_set(),
1974 extension->optional_permission_set());
1975
1976 // Make sure the active permissions contain no more than optional + default.
[email protected]c2e66e12012-06-27 06:27:061977 scoped_refptr<PermissionSet> adjusted_active =
1978 PermissionSet::CreateIntersection(
[email protected]902fd7b2011-07-27 18:42:311979 total_permissions.get(), active_permissions.get());
1980
1981 // Make sure the active permissions contain the default permissions.
[email protected]c2e66e12012-06-27 06:27:061982 adjusted_active = PermissionSet::CreateUnion(
[email protected]902fd7b2011-07-27 18:42:311983 extension->required_permission_set(), adjusted_active.get());
1984
[email protected]c333e792012-01-06 16:57:391985 extensions::PermissionsUpdater perms_updater(profile());
1986 perms_updater.UpdateActivePermissions(extension, adjusted_active);
[email protected]902fd7b2011-07-27 18:42:311987 }
1988
[email protected]8d888c12010-11-30 00:00:251989 // We keep track of all permissions the user has granted each extension.
1990 // This allows extensions to gracefully support backwards compatibility
1991 // by including unknown permissions in their manifests. When the user
1992 // installs the extension, only the recognized permissions are recorded.
1993 // When the unknown permissions become recognized (e.g., through browser
1994 // upgrade), we can prompt the user to accept these new permissions.
1995 // Extensions can also silently upgrade to less permissions, and then
1996 // silently upgrade to a version that adds these permissions back.
1997 //
1998 // For example, pretend that Chrome 10 includes a permission "omnibox"
1999 // for an API that adds suggestions to the omnibox. An extension can
2000 // maintain backwards compatibility while still having "omnibox" in the
2001 // manifest. If a user installs the extension on Chrome 9, the browser
2002 // will record the permissions it recognized, not including "omnibox."
2003 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
2004 // will disable the extension and prompt the user to approve the increase
2005 // in privileges. The extension could then release a new version that
2006 // removes the "omnibox" permission. When the user upgrades, Chrome will
2007 // still remember that "omnibox" had been granted, so that if the
2008 // extension once again includes "omnibox" in an upgrade, the extension
2009 // can upgrade without requiring this user's approval.
2010 const Extension* old = GetExtensionByIdInternal(extension->id(),
[email protected]8001df22011-04-28 19:59:472011 true, true, false);
[email protected]8d888c12010-11-30 00:00:252012 bool is_extension_upgrade = old != NULL;
2013 bool is_privilege_increase = false;
[email protected]44d62b62012-04-11 00:06:032014 bool previously_disabled = false;
2015 Extension::DisableReason disable_reason =
2016 extension_prefs_->GetDisableReason(extension->id());
[email protected]8d888c12010-11-30 00:00:252017
[email protected]902fd7b2011-07-27 18:42:312018 // We only need to compare the granted permissions to the current permissions
2019 // if the extension is not allowed to silently increase its permissions.
2020 if (!extension->CanSilentlyIncreasePermissions()) {
[email protected]8d888c12010-11-30 00:00:252021 // Add all the recognized permissions if the granted permissions list
2022 // hasn't been initialized yet.
[email protected]c2e66e12012-06-27 06:27:062023 scoped_refptr<PermissionSet> granted_permissions =
[email protected]902fd7b2011-07-27 18:42:312024 extension_prefs_->GetGrantedPermissions(extension->id());
[email protected]0d3e4a22011-06-23 19:02:522025 CHECK(granted_permissions.get());
[email protected]8d888c12010-11-30 00:00:252026
2027 // Here, we check if an extension's privileges have increased in a manner
2028 // that requires the user's approval. This could occur because the browser
2029 // upgraded and recognized additional privileges, or an extension upgrades
2030 // to a version that requires additional privileges.
[email protected]0d3e4a22011-06-23 19:02:522031 is_privilege_increase =
[email protected]902fd7b2011-07-27 18:42:312032 granted_permissions->HasLessPrivilegesThan(
2033 extension->GetActivePermissions());
[email protected]8d888c12010-11-30 00:00:252034 }
2035
2036 if (is_extension_upgrade) {
[email protected]66e26872010-12-03 20:07:252037 // Other than for unpacked extensions, CrxInstaller should have guaranteed
2038 // that we aren't downgrading.
2039 if (extension->location() != Extension::LOAD)
[email protected]2f8757c32012-06-19 19:17:472040 CHECK(extension->version()->CompareTo(*(old->version())) >= 0);
[email protected]8d888c12010-11-30 00:00:252041
2042 // Extensions get upgraded if the privileges are allowed to increase or
2043 // the privileges haven't increased.
2044 if (!is_privilege_increase) {
2045 SetBeingUpgraded(old, true);
2046 SetBeingUpgraded(extension, true);
2047 }
2048
[email protected]44d62b62012-04-11 00:06:032049 // If the extension was already disabled, suppress any alerts for becoming
2050 // disabled on permissions increase.
2051 previously_disabled = extension_prefs_->IsExtensionDisabled(old->id());
2052 if (previously_disabled) {
2053 Extension::DisableReason reason = extension_prefs_->GetDisableReason(
2054 old->id());
2055 if (reason == Extension::DISABLE_UNKNOWN) {
2056 // Initialize the reason for legacy disabled extensions from whether the
2057 // extension already exceeded granted permissions.
2058 if (extension_prefs_->DidExtensionEscalatePermissions(old->id()))
2059 disable_reason = Extension::DISABLE_PERMISSIONS_INCREASE;
2060 else
2061 disable_reason = Extension::DISABLE_USER_ACTION;
2062 }
2063 } else {
2064 disable_reason = Extension::DISABLE_PERMISSIONS_INCREASE;
2065 }
2066
[email protected]8d888c12010-11-30 00:00:252067 // To upgrade an extension in place, unload the old one and
2068 // then load the new one.
[email protected]814a7bf0f2011-08-13 05:30:592069 UnloadExtension(old->id(), extension_misc::UNLOAD_REASON_UPDATE);
[email protected]8d888c12010-11-30 00:00:252070 old = NULL;
2071 }
2072
2073 // Extension has changed permissions significantly. Disable it. A
2074 // notification should be sent by the caller.
2075 if (is_privilege_increase) {
[email protected]fe2dd7742011-04-19 22:52:492076 if (!extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
2077 RecordPermissionMessagesHistogram(
2078 extension, "Extensions.Permissions_AutoDisable");
2079 }
[email protected]b3317ad2011-04-28 23:46:002080 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
[email protected]8d888c12010-11-30 00:00:252081 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
[email protected]44d62b62012-04-11 00:06:032082 extension_prefs_->SetDisableReason(extension->id(), disable_reason);
[email protected]8d888c12010-11-30 00:00:252083 }
2084}
2085
[email protected]eaa7dd182010-12-14 11:09:002086void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:392087 std::set<std::string> extension_ids;
[email protected]84df8332011-12-06 18:22:462088 for (ExtensionSet::const_iterator iter = extensions_.begin();
2089 iter != extensions_.end(); ++iter) {
2090 const Extension* extension = *iter;
2091 if (!extension->is_theme() && extension->location() != Extension::COMPONENT)
2092 extension_ids.insert(extension->id());
[email protected]aab98a52009-12-02 03:22:352093 }
2094
2095 child_process_logging::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:252096}
2097
[email protected]8266d662011-07-12 21:53:262098void ExtensionService::OnExtensionInstalled(
[email protected]36a5c4c2011-12-14 16:34:502099 const Extension* extension,
2100 bool from_webstore,
2101 const StringOrdinal& page_ordinal) {
[email protected]a29a517a2011-01-21 21:11:122102 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:202103
[email protected]4416c5a2010-06-26 01:28:572104 // Ensure extension is deleted unless we transfer ownership.
[email protected]9adb9692010-10-29 23:14:022105 scoped_refptr<const Extension> scoped_extension(extension);
[email protected]c3cfb012011-04-06 22:07:352106 const std::string& id = extension->id();
[email protected]8a87a5332011-08-11 17:54:592107 // Extensions installed by policy can't be disabled. So even if a previous
2108 // installation disabled the extension, make sure it is now enabled.
2109 bool initial_enable =
2110 !extension_prefs_->IsExtensionDisabled(id) ||
[email protected]65187152012-06-02 13:14:142111 system_->management_policy()->MustRemainEnabled(extension, NULL);
[email protected]51a3bf8b2012-06-08 22:53:062112 const PendingExtensionInfo* pending_extension_info = NULL;
2113 if ((pending_extension_info = pending_extension_manager()->GetById(id))) {
2114 if (!pending_extension_info->ShouldAllowInstall(*extension)) {
2115 pending_extension_manager()->Remove(id);
[email protected]8ef78fd2010-08-19 17:14:322116
[email protected]4416c5a2010-06-26 01:28:572117 LOG(WARNING)
[email protected]ae6cb3022011-04-04 20:11:542118 << "ShouldAllowInstall() returned false for "
[email protected]c3cfb012011-04-06 22:07:352119 << id << " of type " << extension->GetType()
[email protected]7fa19f82010-12-21 19:40:082120 << " and update URL " << extension->update_url().spec()
2121 << "; not installing";
[email protected]ae6cb3022011-04-04 20:11:542122
[email protected]ad50def52011-10-19 23:17:072123 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272124 chrome::NOTIFICATION_EXTENSION_INSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:532125 content::Source<Profile>(profile_),
2126 content::Details<const Extension>(extension));
[email protected]ae6cb3022011-04-04 20:11:542127
[email protected]4416c5a2010-06-26 01:28:572128 // Delete the extension directory since we're not going to
2129 // load it.
[email protected]14908b72011-04-20 06:54:362130 if (!BrowserThread::PostTask(
2131 BrowserThread::FILE, FROM_HERE,
[email protected]53612e82011-10-18 18:00:362132 base::Bind(&extension_file_util::DeleteFile,
2133 extension->path(), true)))
[email protected]14908b72011-04-20 06:54:362134 NOTREACHED();
[email protected]4416c5a2010-06-26 01:28:572135 return;
2136 }
[email protected]51a3bf8b2012-06-08 22:53:062137
2138 pending_extension_manager()->Remove(id);
[email protected]4416c5a2010-06-26 01:28:572139 } else {
[email protected]c3cfb012011-04-06 22:07:352140 // We explicitly want to re-enable an uninstalled external
2141 // extension; if we're here, that means the user is manually
2142 // installing the extension.
[email protected]6cc7dbae2011-04-29 21:18:332143 if (IsExternalExtensionUninstalled(id)) {
2144 initial_enable = true;
2145 }
[email protected]aa142702010-03-26 01:26:332146 }
2147
[email protected]07533022011-06-27 20:42:552148 // Do not record the install histograms for upgrades.
2149 if (!GetExtensionByIdInternal(extension->id(), true, true, false)) {
2150 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
2151 extension->GetType(), 100);
2152 RecordPermissionMessagesHistogram(
2153 extension, "Extensions.Permissions_Install");
2154 }
2155
[email protected]a39921b42012-02-28 03:42:542156 // Certain extension locations are specific enough that we can
2157 // auto-acknowledge any extension that came from one of them.
2158 if (extension->location() == Extension::EXTERNAL_POLICY_DOWNLOAD) {
2159 AcknowledgeExternalExtension(extension->id());
2160 }
2161
[email protected]4416c5a2010-06-26 01:28:572162 extension_prefs_->OnExtensionInstalled(
[email protected]8266d662011-07-12 21:53:262163 extension,
2164 initial_enable ? Extension::ENABLED : Extension::DISABLED,
[email protected]cc829cf2011-08-22 21:01:352165 from_webstore,
[email protected]36a5c4c2011-12-14 16:34:502166 page_ordinal);
[email protected]25b34332009-06-05 21:53:192167
[email protected]cdfca9702011-08-08 16:07:012168 // Unpacked extensions default to allowing file access, but if that has been
2169 // overridden, don't reset the value.
[email protected]b6e64fd2011-08-09 19:49:192170 if (Extension::ShouldAlwaysAllowFileAccess(extension->location()) &&
[email protected]cdfca9702011-08-08 16:07:012171 !extension_prefs_->HasAllowFileAccessSetting(id)) {
2172 extension_prefs_->SetAllowFileAccess(id, true);
2173 }
2174
[email protected]fcb58a862012-05-01 01:03:152175 content::NotificationService::current()->Notify(
2176 chrome::NOTIFICATION_EXTENSION_INSTALLED,
2177 content::Source<Profile>(profile_),
2178 content::Details<const Extension>(extension));
2179
[email protected]49cd1572011-02-08 21:38:452180 // Transfer ownership of |extension| to AddExtension.
[email protected]fcb58a862012-05-01 01:03:152181 AddExtension(scoped_extension);
[email protected]4a190632009-05-09 01:07:422182}
2183
[email protected]eaa7dd182010-12-14 11:09:002184const Extension* ExtensionService::GetExtensionByIdInternal(
[email protected]8001df22011-04-28 19:59:472185 const std::string& id, bool include_enabled, bool include_disabled,
2186 bool include_terminated) const {
[email protected]e957fe52009-06-23 16:51:052187 std::string lowercase_id = StringToLowerASCII(id);
[email protected]0c6da502009-08-14 22:32:392188 if (include_enabled) {
[email protected]84df8332011-12-06 18:22:462189 const Extension* extension = extensions_.GetByID(lowercase_id);
2190 if (extension)
2191 return extension;
[email protected]0c6da502009-08-14 22:32:392192 }
2193 if (include_disabled) {
[email protected]84df8332011-12-06 18:22:462194 const Extension* extension = disabled_extensions_.GetByID(lowercase_id);
2195 if (extension)
2196 return extension;
[email protected]ce5c4502009-05-06 16:46:112197 }
[email protected]8001df22011-04-28 19:59:472198 if (include_terminated) {
[email protected]84df8332011-12-06 18:22:462199 const Extension* extension = terminated_extensions_.GetByID(lowercase_id);
2200 if (extension)
2201 return extension;
[email protected]8001df22011-04-28 19:59:472202 }
[email protected]ce5c4502009-05-06 16:46:112203 return NULL;
2204}
2205
[email protected]bb7f40952011-01-13 00:21:202206void ExtensionService::TrackTerminatedExtension(const Extension* extension) {
[email protected]84df8332011-12-06 18:22:462207 if (!terminated_extensions_.Contains(extension->id()))
2208 terminated_extensions_.Insert(make_scoped_refptr(extension));
[email protected]fa2416f2011-05-03 08:41:202209
[email protected]b3f7fe22011-11-11 19:27:562210 UnloadExtension(extension->id(), extension_misc::UNLOAD_REASON_TERMINATE);
[email protected]bb7f40952011-01-13 00:21:202211}
2212
2213void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
[email protected]fa2416f2011-05-03 08:41:202214 std::string lowercase_id = StringToLowerASCII(id);
[email protected]84df8332011-12-06 18:22:462215 terminated_extensions_.Remove(lowercase_id);
[email protected]bb7f40952011-01-13 00:21:202216}
2217
[email protected]0dfe05c2011-02-23 23:03:362218const Extension* ExtensionService::GetTerminatedExtension(
[email protected]8001df22011-04-28 19:59:472219 const std::string& id) const {
2220 return GetExtensionByIdInternal(id, false, false, true);
2221}
2222
2223const Extension* ExtensionService::GetInstalledExtension(
2224 const std::string& id) const {
2225 return GetExtensionByIdInternal(id, true, true, true);
[email protected]0dfe05c2011-02-23 23:03:362226}
2227
[email protected]eaa7dd182010-12-14 11:09:002228bool ExtensionService::ExtensionBindingsAllowed(const GURL& url) {
[email protected]615d88f2011-12-13 01:47:442229 // Allow bindings for all packaged extensions and component hosted apps.
2230 const Extension* extension = extensions_.GetExtensionOrAppByURL(
2231 ExtensionURLInfo(url));
2232 return extension && (!extension->is_hosted_app() ||
2233 extension->location() == Extension::COMPONENT);
[email protected]6d2e60bd2010-06-03 22:37:392234}
2235
[email protected]eaa7dd182010-12-14 11:09:002236const SkBitmap& ExtensionService::GetOmniboxIcon(
[email protected]b671760b2010-07-15 21:13:472237 const std::string& extension_id) {
2238 return omnibox_icon_manager_.GetIcon(extension_id);
2239}
2240
[email protected]eaa7dd182010-12-14 11:09:002241const SkBitmap& ExtensionService::GetOmniboxPopupIcon(
[email protected]29d0d4ac2010-09-08 21:10:312242 const std::string& extension_id) {
2243 return omnibox_popup_icon_manager_.GetIcon(extension_id);
2244}
2245
[email protected]9060d8b02012-01-13 02:14:302246bool ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:322247 const std::string& id,
[email protected]8e4560b62011-01-14 10:09:142248 const Version* version,
[email protected]8ef78fd2010-08-19 17:14:322249 const FilePath& path,
[email protected]1bf73cc32011-10-26 22:38:312250 Extension::Location location,
[email protected]47fc70c2011-12-06 07:29:512251 int creation_flags,
2252 bool mark_acknowledged) {
[email protected]ab22ba42011-01-14 16:36:382253 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2254 CHECK(Extension::IdIsValid(id));
[email protected]79c833b52011-04-05 18:31:012255 if (extension_prefs_->IsExternalExtensionUninstalled(id))
[email protected]9060d8b02012-01-13 02:14:302256 return false;
[email protected]a8af9fdb2010-10-28 21:52:202257
[email protected]8e4560b62011-01-14 10:09:142258 DCHECK(version);
2259
[email protected]7577a5c52009-07-30 06:21:582260 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:492261 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:582262 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:022263 const Extension* existing = GetExtensionById(id, true);
[email protected]7577a5c52009-07-30 06:21:582264 if (existing) {
[email protected]8e4560b62011-01-14 10:09:142265 switch (existing->version()->CompareTo(*version)) {
[email protected]7577a5c52009-07-30 06:21:582266 case -1: // existing version is older, we should upgrade
2267 break;
2268 case 0: // existing version is same, do nothing
[email protected]9060d8b02012-01-13 02:14:302269 return false;
[email protected]7577a5c52009-07-30 06:21:582270 case 1: // existing version is newer, uh-oh
2271 LOG(WARNING) << "Found external version of extension " << id
2272 << "that is older than current version. Current version "
2273 << "is: " << existing->VersionString() << ". New version "
[email protected]16d900042012-01-04 21:24:322274 << "is: " << version->GetString()
2275 << ". Keeping current version.";
[email protected]9060d8b02012-01-13 02:14:302276 return false;
[email protected]7577a5c52009-07-30 06:21:582277 }
2278 }
2279
[email protected]9060d8b02012-01-13 02:14:302280 // If the extension is already pending, don't start an install.
[email protected]e3987852012-05-04 10:06:302281 if (!pending_extension_manager()->AddFromExternalFile(
2282 id, location, *version)) {
[email protected]9060d8b02012-01-13 02:14:302283 return false;
[email protected]e3987852012-05-04 10:06:302284 }
[email protected]9c635f22010-12-02 09:36:362285
[email protected]14908b72011-04-20 06:54:362286 // no client (silent install)
[email protected]d8c8f25f2011-11-02 18:18:012287 scoped_refptr<CrxInstaller> installer(CrxInstaller::Create(this, NULL));
[email protected]6dfbbf82010-03-12 23:09:162288 installer->set_install_source(location);
2289 installer->set_expected_id(id);
[email protected]cb0e50312011-05-09 15:03:072290 installer->set_expected_version(*version);
2291 installer->set_install_cause(extension_misc::INSTALL_CAUSE_EXTERNAL_FILE);
[email protected]1bf73cc32011-10-26 22:38:312292 installer->set_creation_flags(creation_flags);
[email protected]6dfbbf82010-03-12 23:09:162293 installer->InstallCrx(path);
[email protected]47fc70c2011-12-06 07:29:512294
2295 // Depending on the source, a new external extension might not need a user
2296 // notification on installation. For such extensions, mark them acknowledged
2297 // now to suppress the notification.
2298 if (mark_acknowledged)
2299 AcknowledgeExternalExtension(id);
[email protected]9060d8b02012-01-13 02:14:302300
2301 return true;
[email protected]7577a5c52009-07-30 06:21:582302}
2303
[email protected]eaa7dd182010-12-14 11:09:002304void ExtensionService::ReportExtensionLoadError(
[email protected]d11c8e92009-10-20 23:26:402305 const FilePath& extension_path,
2306 const std::string &error,
[email protected]d11c8e92009-10-20 23:26:402307 bool be_noisy) {
[email protected]d8c8f25f2011-11-02 18:18:012308 content::NotificationService::current()->Notify(
2309 chrome::NOTIFICATION_EXTENSION_LOAD_ERROR,
2310 content::Source<Profile>(profile_),
2311 content::Details<const std::string>(&error));
[email protected]d11c8e92009-10-20 23:26:402312
[email protected]8a205c02011-02-04 20:41:332313 std::string path_str = UTF16ToUTF8(extension_path.LossyDisplayName());
[email protected]fc670822011-12-17 09:33:492314 string16 message = ASCIIToUTF16(base::StringPrintf(
[email protected]18d4b6c2010-09-21 03:21:042315 "Could not load extension from '%s'. %s",
[email protected]fc670822011-12-17 09:33:492316 path_str.c_str(), error.c_str()));
[email protected]d11c8e92009-10-20 23:26:402317 ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy);
2318}
2319
[email protected]eaa7dd182010-12-14 11:09:002320void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]406027c02010-09-27 08:03:182321 ExtensionHost* host) {
2322 OrphanedDevTools::iterator iter =
[email protected]2d2f6cfc2011-05-06 21:09:332323 orphaned_dev_tools_.find(host->extension_id());
[email protected]406027c02010-09-27 08:03:182324 if (iter == orphaned_dev_tools_.end())
2325 return;
2326
[email protected]0e12d7d2011-12-01 16:21:442327 DevToolsAgentHost* agent = DevToolsAgentHostRegistry::GetDevToolsAgentHost(
2328 host->render_view_host());
2329 content::DevToolsManager::GetInstance()->AttachClientHost(iter->second,
2330 agent);
[email protected]406027c02010-09-27 08:03:182331 orphaned_dev_tools_.erase(iter);
2332}
2333
[email protected]432115822011-07-10 15:52:272334void ExtensionService::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:532335 const content::NotificationSource& source,
2336 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:272337 switch (type) {
2338 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]6c2381d2011-10-19 02:52:532339 if (profile_ !=
2340 content::Source<Profile>(source).ptr()->GetOriginalProfile()) {
[email protected]bc535ee52010-08-31 18:40:322341 break;
[email protected]6c2381d2011-10-19 02:52:532342 }
[email protected]a4ed6282009-12-14 20:51:162343
[email protected]6c2381d2011-10-19 02:52:532344 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr();
[email protected]f128af42010-08-05 18:05:262345
[email protected]fa2416f2011-05-03 08:41:202346 // Mark the extension as terminated and Unload it. We want it to
2347 // be in a consistent state: either fully working or not loaded
2348 // at all, but never half-crashed. We do it in a PostTask so
2349 // that other handlers of this notification will still have
2350 // access to the Extension and ExtensionHost.
[email protected]14908b72011-04-20 06:54:362351 MessageLoop::current()->PostTask(
2352 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362353 base::Bind(
[email protected]fa2416f2011-05-03 08:41:202354 &ExtensionService::TrackTerminatedExtension,
[email protected]d8c8f25f2011-11-02 18:18:012355 AsWeakPtr(),
[email protected]fa2416f2011-05-03 08:41:202356 host->extension()));
[email protected]31f77262009-12-02 20:48:532357 break;
2358 }
[email protected]432115822011-07-10 15:52:272359 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: {
[email protected]f3b1a082011-11-18 00:34:302360 content::RenderProcessHost* process =
2361 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192362 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302363 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192364 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2365 break;
2366
[email protected]15397b42012-05-16 23:56:062367 // Extensions need to know the channel for API restrictions.
2368 process->Send(new ExtensionMsg_SetChannel(
2369 chrome::VersionInfo::GetChannel()));
2370
[email protected]77a6970c2011-04-23 16:58:562371 // Valid extension function names, used to setup bindings in renderer.
2372 std::vector<std::string> function_names;
2373 ExtensionFunctionDispatcher::GetAllFunctionNames(&function_names);
2374 process->Send(new ExtensionMsg_SetFunctionNames(function_names));
[email protected]31f77262009-12-02 20:48:532375
[email protected]77a6970c2011-04-23 16:58:562376 // Scripting whitelist. This is modified by tests and must be communicated
2377 // to renderers.
2378 process->Send(new ExtensionMsg_SetScriptingWhitelist(
2379 *Extension::GetScriptingWhitelist()));
2380
2381 // Loaded extensions.
[email protected]9776e82e2011-11-15 02:17:532382 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions;
[email protected]84df8332011-12-06 18:22:462383 for (ExtensionSet::const_iterator iter = extensions_.begin();
2384 iter != extensions_.end(); ++iter) {
[email protected]19647262011-12-16 09:57:492385 // Renderers don't need to know about themes.
2386 if (!(*iter)->is_theme())
2387 loaded_extensions.push_back(ExtensionMsg_Loaded_Params(*iter));
[email protected]77a6970c2011-04-23 16:58:562388 }
[email protected]9776e82e2011-11-15 02:17:532389 process->Send(new ExtensionMsg_Loaded(loaded_extensions));
[email protected]77a6970c2011-04-23 16:58:562390 break;
2391 }
[email protected]432115822011-07-10 15:52:272392 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
[email protected]f3b1a082011-11-18 00:34:302393 content::RenderProcessHost* process =
2394 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192395 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302396 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192397 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2398 break;
2399
[email protected]f3b1a082011-11-18 00:34:302400 installed_app_hosts_.erase(process->GetID());
[email protected]6f371442011-11-09 06:45:462401
[email protected]6bc04fd82011-12-04 02:29:352402 process_map_.RemoveAllFromProcess(process->GetID());
[email protected]6f371442011-11-09 06:45:462403 BrowserThread::PostTask(
2404 BrowserThread::IO, FROM_HERE,
2405 base::Bind(&ExtensionInfoMap::UnregisterAllExtensionsInProcess,
[email protected]31d8f5f22012-04-02 15:22:082406 system_->info_map(),
[email protected]f3b1a082011-11-18 00:34:302407 process->GetID()));
[email protected]da5683db2011-04-23 17:12:212408 break;
2409 }
[email protected]432115822011-07-10 15:52:272410 case chrome::NOTIFICATION_PREF_CHANGED: {
[email protected]6c2381d2011-10-19 02:52:532411 std::string* pref_name = content::Details<std::string>(details).ptr();
[email protected]6c751e72010-11-23 10:11:102412 if (*pref_name == prefs::kExtensionInstallAllowList ||
2413 *pref_name == prefs::kExtensionInstallDenyList) {
2414 CheckAdminBlacklist();
[email protected]6c751e72010-11-23 10:11:102415 } else {
2416 NOTREACHED() << "Unexpected preference name.";
2417 }
[email protected]aa96d3a2010-08-21 08:45:252418 break;
2419 }
[email protected]d91268022011-08-26 13:17:372420 case chrome::NOTIFICATION_IMPORT_FINISHED: {
[email protected]cf593af2011-12-30 05:44:392421 InitAfterImport();
[email protected]d91268022011-08-26 13:17:372422 break;
2423 }
[email protected]aa96d3a2010-08-21 08:45:252424
[email protected]4814b512009-11-07 00:12:292425 default:
2426 NOTREACHED() << "Unexpected notification type.";
2427 }
2428}
2429
[email protected]eaa7dd182010-12-14 11:09:002430bool ExtensionService::HasApps() const {
[email protected]ec5b50d2010-10-09 16:35:182431 return !GetAppIds().empty();
2432}
[email protected]377011d2010-07-20 04:18:502433
[email protected]eaa7dd182010-12-14 11:09:002434ExtensionIdSet ExtensionService::GetAppIds() const {
[email protected]ec5b50d2010-10-09 16:35:182435 ExtensionIdSet result;
[email protected]84df8332011-12-06 18:22:462436 for (ExtensionSet::const_iterator it = extensions_.begin();
[email protected]377011d2010-07-20 04:18:502437 it != extensions_.end(); ++it) {
[email protected]ec5b50d2010-10-09 16:35:182438 if ((*it)->is_app() && (*it)->location() != Extension::COMPONENT)
2439 result.insert((*it)->id());
[email protected]377011d2010-07-20 04:18:502440 }
2441
[email protected]ec5b50d2010-10-09 16:35:182442 return result;
[email protected]377011d2010-07-20 04:18:502443}
[email protected]d7e9a862010-11-03 21:57:492444
[email protected]eaa7dd182010-12-14 11:09:002445bool ExtensionService::IsBackgroundPageReady(const Extension* extension) {
[email protected]4762e7902012-03-28 23:34:042446 return (!extension->has_persistent_background_page() ||
[email protected]d7e9a862010-11-03 21:57:492447 extension_runtime_data_[extension->id()].background_page_ready);
2448}
2449
[email protected]eaa7dd182010-12-14 11:09:002450void ExtensionService::SetBackgroundPageReady(const Extension* extension) {
[email protected]a03d4448f2012-01-10 23:25:282451 DCHECK(extension->has_background_page());
[email protected]d7e9a862010-11-03 21:57:492452 extension_runtime_data_[extension->id()].background_page_ready = true;
[email protected]ad50def52011-10-19 23:17:072453 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272454 chrome::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY,
[email protected]6c2381d2011-10-19 02:52:532455 content::Source<const Extension>(extension),
[email protected]ad50def52011-10-19 23:17:072456 content::NotificationService::NoDetails());
[email protected]d7e9a862010-11-03 21:57:492457}
2458
[email protected]eaa7dd182010-12-14 11:09:002459bool ExtensionService::IsBeingUpgraded(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:492460 return extension_runtime_data_[extension->id()].being_upgraded;
2461}
2462
[email protected]eaa7dd182010-12-14 11:09:002463void ExtensionService::SetBeingUpgraded(const Extension* extension,
[email protected]d7e9a862010-11-03 21:57:492464 bool value) {
2465 extension_runtime_data_[extension->id()].being_upgraded = value;
2466}
[email protected]1bead0712010-11-27 17:41:532467
[email protected]39a5b532011-10-22 01:47:072468bool ExtensionService::HasUsedWebRequest(const Extension* extension) {
2469 return extension_runtime_data_[extension->id()].has_used_webrequest;
2470}
2471
2472void ExtensionService::SetHasUsedWebRequest(const Extension* extension,
2473 bool value) {
2474 extension_runtime_data_[extension->id()].has_used_webrequest = value;
2475}
2476
[email protected]45644f62011-11-23 00:58:232477base::PropertyBag* ExtensionService::GetPropertyBag(
2478 const Extension* extension) {
[email protected]1bead0712010-11-27 17:41:532479 return &extension_runtime_data_[extension->id()].property_bag;
2480}
[email protected]cebc3dc2011-04-18 17:15:002481
2482void ExtensionService::RegisterNaClModule(const GURL& url,
2483 const std::string& mime_type) {
2484 NaClModuleInfo info;
2485 info.url = url;
2486 info.mime_type = mime_type;
2487
2488 DCHECK(FindNaClModule(url) == nacl_module_list_.end());
2489 nacl_module_list_.push_front(info);
2490}
2491
2492void ExtensionService::UnregisterNaClModule(const GURL& url) {
2493 NaClModuleInfoList::iterator iter = FindNaClModule(url);
2494 DCHECK(iter != nacl_module_list_.end());
2495 nacl_module_list_.erase(iter);
2496}
2497
[email protected]ed0ba002011-05-26 16:55:132498void ExtensionService::UpdatePluginListWithNaClModules() {
[email protected]300c0ea2011-07-15 23:04:332499 // An extension has been added which has a nacl_module component, which means
2500 // there is a MIME type that module wants to handle, so we need to add that
2501 // MIME type to plugins which handle NaCl modules in order to allow the
2502 // individual modules to handle these types.
[email protected]1e127022011-11-29 21:11:472503 FilePath path;
2504 if (!PathService::Get(chrome::FILE_NACL_PLUGIN, &path))
2505 return;
2506 const content::PepperPluginInfo* pepper_info =
2507 PluginService::GetInstance()->GetRegisteredPpapiPluginInfo(path);
2508 if (!pepper_info)
2509 return;
[email protected]300c0ea2011-07-15 23:04:332510
[email protected]1e127022011-11-29 21:11:472511 std::vector<webkit::WebPluginMimeType>::const_iterator mime_iter;
2512 // Check each MIME type the plugins handle for the NaCl MIME type.
2513 for (mime_iter = pepper_info->mime_types.begin();
2514 mime_iter != pepper_info->mime_types.end(); ++mime_iter) {
2515 if (mime_iter->mime_type == kNaClPluginMimeType) {
2516 // This plugin handles "application/x-nacl".
[email protected]300c0ea2011-07-15 23:04:332517
[email protected]1e127022011-11-29 21:11:472518 PluginService::GetInstance()->
2519 UnregisterInternalPlugin(pepper_info->path);
[email protected]300c0ea2011-07-15 23:04:332520
[email protected]1e127022011-11-29 21:11:472521 webkit::WebPluginInfo info = pepper_info->ToWebPluginInfo();
[email protected]300c0ea2011-07-15 23:04:332522
[email protected]1e127022011-11-29 21:11:472523 for (ExtensionService::NaClModuleInfoList::const_iterator iter =
2524 nacl_module_list_.begin();
2525 iter != nacl_module_list_.end(); ++iter) {
2526 // Add the MIME type specified in the extension to this NaCl plugin,
2527 // With an extra "nacl" argument to specify the location of the NaCl
2528 // manifest file.
2529 webkit::WebPluginMimeType mime_type_info;
2530 mime_type_info.mime_type = iter->mime_type;
2531 mime_type_info.additional_param_names.push_back(UTF8ToUTF16("nacl"));
2532 mime_type_info.additional_param_values.push_back(
2533 UTF8ToUTF16(iter->url.spec()));
2534 info.mime_types.push_back(mime_type_info);
[email protected]300c0ea2011-07-15 23:04:332535 }
[email protected]1e127022011-11-29 21:11:472536
2537 PluginService::GetInstance()->RefreshPlugins();
[email protected]c6f3dea2012-01-14 02:23:112538 PluginService::GetInstance()->RegisterInternalPlugin(info, true);
[email protected]1e127022011-11-29 21:11:472539 // This plugin has been modified, no need to check the rest of its
2540 // types, but continue checking other plugins.
2541 break;
[email protected]ed0ba002011-05-26 16:55:132542 }
2543 }
[email protected]ed0ba002011-05-26 16:55:132544}
2545
[email protected]cebc3dc2011-04-18 17:15:002546ExtensionService::NaClModuleInfoList::iterator
2547 ExtensionService::FindNaClModule(const GURL& url) {
2548 for (NaClModuleInfoList::iterator iter = nacl_module_list_.begin();
2549 iter != nacl_module_list_.end(); ++iter) {
2550 if (iter->url == url)
2551 return iter;
2552 }
2553 return nacl_module_list_.end();
2554}
[email protected]9de83282011-12-12 11:06:372555
[email protected]58edca52012-02-16 21:51:382556extensions::APIResourceController*
2557ExtensionService::api_resource_controller() {
[email protected]2404a28272012-05-30 02:31:142558 // TODO(miket): Find a better place for this thing to live. Like every other
2559 // piece of baggage on ExtensionService, it's scoped along with a Profile.
2560
2561 // To coexist with certain unit tests that don't have a work-thread message
[email protected]e1fae1c2011-12-13 22:17:172562 // loop available at ExtensionService shutdown, we lazy-initialize this
[email protected]58edca52012-02-16 21:51:382563 // object so that those cases neither create nor destroy an
2564 // APIResourceController.
[email protected]2404a28272012-05-30 02:31:142565 if (!api_resource_controller_.get()) {
2566 api_resource_controller_.reset(new extensions::APIResourceController());
[email protected]e1fae1c2011-12-13 22:17:172567 }
[email protected]2404a28272012-05-30 02:31:142568 return api_resource_controller_.get();
[email protected]e1fae1c2011-12-13 22:17:172569}