blob: 33e5c2711f599c58950b1859b413a98592f86917 [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]24b538a2010-02-27 01:22:4420#include "base/string16.h"
[email protected]e83326f2010-07-31 17:29:2521#include "base/string_number_conversions.h"
[email protected]6014d672008-12-05 00:38:2522#include "base/string_util.h"
[email protected]18d4b6c2010-09-21 03:21:0423#include "base/stringprintf.h"
[email protected]34b99632011-01-01 01:01:0624#include "base/threading/thread_restrictions.h"
[email protected]cc2c3432009-11-06 17:24:3625#include "base/time.h"
[email protected]ce7f62e32010-08-10 23:43:5926#include "base/utf_string_conversions.h"
[email protected]cc655912009-01-29 23:19:1927#include "base/values.h"
[email protected]aa142702010-03-26 01:26:3328#include "base/version.h"
[email protected]a692a952011-11-28 08:23:5429#include "chrome/browser/accessibility/accessibility_extension_api.h"
[email protected]23d5f172011-10-25 05:49:5330#include "chrome/browser/bookmarks/bookmark_extension_api.h"
[email protected]15730c42009-09-03 00:03:2031#include "chrome/browser/browser_process.h"
[email protected]dfba8762011-09-02 12:49:5432#include "chrome/browser/chrome_plugin_service_filter.h"
[email protected]cdcf60242011-11-04 15:53:4333#include "chrome/browser/download/download_extension_api.h"
[email protected]a1755bd2011-12-09 00:40:2034#include "chrome/browser/extensions/api/socket/socket_api_controller.h"
[email protected]af9db5f2011-10-05 05:13:1535#include "chrome/browser/extensions/app_notification_manager.h"
[email protected]18cc5a52011-04-15 16:03:3836#include "chrome/browser/extensions/apps_promo.h"
[email protected]d8c8f25f2011-11-02 18:18:0137#include "chrome/browser/extensions/component_loader.h"
[email protected]e0785902011-05-19 23:34:1738#include "chrome/browser/extensions/crx_installer.h"
[email protected]49098f702011-10-13 03:47:1839#include "chrome/browser/extensions/default_apps_trial.h"
[email protected]b68d5ed2009-04-16 02:41:2840#include "chrome/browser/extensions/extension_browser_event_router.h"
[email protected]2c5e1e12010-06-10 13:14:4441#include "chrome/browser/extensions/extension_cookies_api.h"
[email protected]c10da4b02010-03-25 14:38:3242#include "chrome/browser/extensions/extension_data_deleter.h"
[email protected]14a000d2010-04-29 21:44:2443#include "chrome/browser/extensions/extension_error_reporter.h"
[email protected]e5af875f2011-10-10 21:09:1444#include "chrome/browser/extensions/extension_global_error.h"
[email protected]b1748b1d82009-11-30 20:32:5645#include "chrome/browser/extensions/extension_host.h"
[email protected]8baf0dbb2011-11-27 22:47:0246#include "chrome/browser/extensions/extension_input_ime_api.h"
[email protected]7596ce72010-08-30 05:10:4647#include "chrome/browser/extensions/extension_management_api.h"
[email protected]118de7892011-03-16 13:31:4048#include "chrome/browser/extensions/extension_preference_api.h"
[email protected]4814b512009-11-07 00:12:2949#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]8a661f82010-10-19 21:47:1150#include "chrome/browser/extensions/extension_processes_api.h"
[email protected]3569b502012-01-12 20:08:2351#include "chrome/browser/extensions/extension_sorting.h"
[email protected]19eb80152011-02-26 00:28:4352#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]90310d92011-04-17 07:35:0453#include "chrome/browser/extensions/extension_sync_data.h"
[email protected]93fd78f42009-07-10 16:43:1754#include "chrome/browser/extensions/extension_updater.h"
[email protected]8f9d4eb2011-02-05 01:39:1055#include "chrome/browser/extensions/extension_web_ui.h"
[email protected]784688a62010-09-13 07:06:5256#include "chrome/browser/extensions/extension_webnavigation_api.h"
[email protected]8e4560b62011-01-14 10:09:1457#include "chrome/browser/extensions/external_extension_provider_impl.h"
[email protected]42b795652011-02-22 23:32:3858#include "chrome/browser/extensions/external_extension_provider_interface.h"
[email protected]d8c8f25f2011-11-02 18:18:0159#include "chrome/browser/extensions/installed_loader.h"
[email protected]b2907fd2011-03-25 16:43:3760#include "chrome/browser/extensions/pending_extension_manager.h"
[email protected]c333e792012-01-06 16:57:3961#include "chrome/browser/extensions/permissions_updater.h"
[email protected]cccdf0aa2011-11-11 03:43:3862#include "chrome/browser/extensions/settings/settings_frontend.h"
[email protected]d8c8f25f2011-11-02 18:18:0163#include "chrome/browser/extensions/unpacked_installer.h"
[email protected]d2e6bd62011-10-24 20:29:0764#include "chrome/browser/history/history_extension_api.h"
[email protected]56ad3792010-05-28 17:45:3365#include "chrome/browser/net/chrome_url_request_context.h"
[email protected]37858e52010-08-26 00:22:0266#include "chrome/browser/prefs/pref_service.h"
[email protected]8ecad5e2010-12-02 21:18:3367#include "chrome/browser/profiles/profile.h"
[email protected]cf593af2011-12-30 05:44:3968#include "chrome/browser/profiles/profile_manager.h"
[email protected]8e5c89a2011-06-07 18:13:3369#include "chrome/browser/search_engines/template_url_service.h"
70#include "chrome/browser/search_engines/template_url_service_factory.h"
[email protected]3bdba0d2011-08-23 07:17:3071#include "chrome/browser/sync/api/sync_change.h"
[email protected]18280372011-03-22 18:05:2272#include "chrome/browser/themes/theme_service.h"
[email protected]a0ea76c2011-03-23 17:36:4273#include "chrome/browser/themes/theme_service_factory.h"
[email protected]e5af875f2011-10-10 21:09:1474#include "chrome/browser/ui/browser.h"
[email protected]62433d32011-10-12 22:33:1275#include "chrome/browser/ui/browser_list.h"
[email protected]e5af875f2011-10-10 21:09:1476#include "chrome/browser/ui/global_error_service.h"
77#include "chrome/browser/ui/global_error_service_factory.h"
[email protected]c8d407e2011-04-28 21:27:1778#include "chrome/browser/ui/webui/chrome_url_data_manager.h"
[email protected]c8d407e2011-04-28 21:27:1779#include "chrome/browser/ui/webui/favicon_source.h"
[email protected]5eddc3e2011-10-26 04:33:3180#include "chrome/browser/ui/webui/ntp/thumbnail_source.h"
[email protected]aab98a52009-12-02 03:22:3581#include "chrome/common/child_process_logging.h"
[email protected]432115822011-07-10 15:52:2782#include "chrome/common/chrome_notification_types.h"
[email protected]1e127022011-11-29 21:11:4783#include "chrome/common/chrome_paths.h"
[email protected]e2eb43112009-05-29 21:19:5484#include "chrome/common/chrome_switches.h"
[email protected]5b1a0e22009-05-26 19:00:5885#include "chrome/common/extensions/extension.h"
[email protected]d7b36dc2009-10-29 21:47:4086#include "chrome/common/extensions/extension_constants.h"
[email protected]05c82182010-06-24 17:49:0887#include "chrome/common/extensions/extension_error_utils.h"
[email protected]7c927b62010-02-24 09:54:1388#include "chrome/common/extensions/extension_file_util.h"
[email protected]77a6970c2011-04-23 16:58:5689#include "chrome/common/extensions/extension_messages.h"
[email protected]a315ba92010-11-16 14:12:2190#include "chrome/common/extensions/extension_resource.h"
[email protected]25b34332009-06-05 21:53:1991#include "chrome/common/pref_names.h"
[email protected]a57209872009-05-04 22:53:1492#include "chrome/common/url_constants.h"
[email protected]c38831a12011-10-28 12:44:4993#include "content/public/browser/browser_thread.h"
[email protected]0e12d7d2011-12-01 16:21:4494#include "content/public/browser/devtools_agent_host_registry.h"
95#include "content/public/browser/devtools_manager.h"
[email protected]05aad2da2011-10-28 10:12:3796#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1697#include "content/public/browser/notification_types.h"
[email protected]3a5180ae2011-12-21 02:39:3898#include "content/public/browser/plugin_service.h"
[email protected]f3b1a082011-11-18 00:34:3099#include "content/public/browser/render_process_host.h"
[email protected]1e127022011-11-29 21:11:47100#include "content/public/common/pepper_plugin_info.h"
[email protected]c10da4b02010-03-25 14:38:32101#include "googleurl/src/gurl.h"
[email protected]1debbbb62010-10-06 17:23:44102#include "net/base/registry_controlled_domain.h"
[email protected]24b538a2010-02-27 01:22:44103#include "webkit/database/database_tracker.h"
104#include "webkit/database/database_util.h"
[email protected]c64631652009-04-29 22:24:31105
[email protected]eed367e2011-04-12 03:43:31106#if defined(OS_CHROMEOS)
[email protected]14da67b02011-08-02 05:49:13107#include "chrome/browser/chromeos/cros/cros_library.h"
[email protected]eed367e2011-04-12 03:43:31108#include "chrome/browser/chromeos/extensions/file_browser_event_router.h"
[email protected]be5bd5742011-07-14 07:21:01109#include "chrome/browser/chromeos/extensions/input_method_event_router.h"
[email protected]ce9802042011-05-27 10:11:40110#include "chrome/browser/chromeos/extensions/media_player_event_router.h"
[email protected]14da67b02011-08-02 05:49:13111#include "chrome/browser/chromeos/input_method/input_method_manager.h"
[email protected]8baf0dbb2011-11-27 22:47:02112#include "chrome/browser/extensions/extension_input_ime_api.h"
[email protected]b777b332011-04-16 04:01:08113#include "webkit/fileapi/file_system_context.h"
114#include "webkit/fileapi/file_system_mount_point_provider.h"
[email protected]eed367e2011-04-12 03:43:31115#endif
116
[email protected]9a2aebe02011-12-02 21:54:14117#if defined(OS_CHROMEOS) && defined(USE_VIRTUAL_KEYBOARD)
[email protected]27072cad2011-05-09 19:46:40118#include "chrome/browser/extensions/extension_input_ui_api.h"
119#endif
120
[email protected]5ef47ec2010-01-28 05:58:05121using base::Time;
[email protected]631bb742011-11-02 11:29:39122using content::BrowserThread;
[email protected]0e12d7d2011-12-01 16:21:44123using content::DevToolsAgentHost;
124using content::DevToolsAgentHostRegistry;
[email protected]3a5180ae2011-12-21 02:39:38125using content::PluginService;
[email protected]5ef47ec2010-01-28 05:58:05126
[email protected]c6d474f82009-12-16 21:11:06127namespace errors = extension_manifest_errors;
128
[email protected]b6ab96d2009-08-20 18:58:19129namespace {
130
[email protected]29d0d4ac2010-09-08 21:10:31131#if defined(OS_LINUX)
132static const int kOmniboxIconPaddingLeft = 2;
133static const int kOmniboxIconPaddingRight = 2;
134#elif defined(OS_MACOSX)
135static const int kOmniboxIconPaddingLeft = 0;
136static const int kOmniboxIconPaddingRight = 2;
137#else
138static const int kOmniboxIconPaddingLeft = 0;
139static const int kOmniboxIconPaddingRight = 0;
140#endif
141
[email protected]300c0ea2011-07-15 23:04:33142const char* kNaClPluginMimeType = "application/x-nacl";
143
[email protected]3bdba0d2011-08-23 07:17:30144static bool IsSyncableExtension(const Extension& extension) {
145 return extension.GetSyncType() == Extension::SYNC_TYPE_EXTENSION;
146}
147
148static bool IsSyncableApp(const Extension& extension) {
149 return extension.GetSyncType() == Extension::SYNC_TYPE_APP;
150}
151
[email protected]c6d474f82009-12-16 21:11:06152} // namespace
[email protected]b6ab96d2009-08-20 18:58:19153
[email protected]eaa7dd182010-12-14 11:09:00154ExtensionService::ExtensionRuntimeData::ExtensionRuntimeData()
[email protected]d7e9a862010-11-03 21:57:49155 : background_page_ready(false),
[email protected]f8bf5992011-10-24 22:38:57156 being_upgraded(false),
157 has_used_webrequest(false) {
[email protected]d7e9a862010-11-03 21:57:49158}
159
[email protected]eaa7dd182010-12-14 11:09:00160ExtensionService::ExtensionRuntimeData::~ExtensionRuntimeData() {
[email protected]d7e9a862010-11-03 21:57:49161}
162
[email protected]cebc3dc2011-04-18 17:15:00163ExtensionService::NaClModuleInfo::NaClModuleInfo() {
164}
165
166ExtensionService::NaClModuleInfo::~NaClModuleInfo() {
167}
168
[email protected]eaa7dd182010-12-14 11:09:00169// ExtensionService.
[email protected]6014d672008-12-05 00:38:25170
[email protected]eaa7dd182010-12-14 11:09:00171const char* ExtensionService::kInstallDirectoryName = "Extensions";
[email protected]15d5b472011-11-24 04:17:24172
173const char* ExtensionService::kLocalAppSettingsDirectoryName =
174 "Local App Settings";
175const char* ExtensionService::kLocalExtensionSettingsDirectoryName =
176 "Local Extension Settings";
177const char* ExtensionService::kSyncAppSettingsDirectoryName =
178 "Sync App Settings";
179const char* ExtensionService::kSyncExtensionSettingsDirectoryName =
180 "Sync Extension Settings";
[email protected]494c06e2009-07-25 01:06:42181
[email protected]8e4560b62011-01-14 10:09:14182void ExtensionService::CheckExternalUninstall(const std::string& id) {
[email protected]a29a517a2011-01-21 21:11:12183 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20184
[email protected]7a4c6852010-09-16 03:44:22185 // Check if the providers know about this extension.
[email protected]0a60a2e2010-10-25 16:15:21186 ProviderCollection::const_iterator i;
187 for (i = external_extension_providers_.begin();
188 i != external_extension_providers_.end(); ++i) {
[email protected]8e4560b62011-01-14 10:09:14189 DCHECK(i->get()->IsReady());
[email protected]0a60a2e2010-10-25 16:15:21190 if (i->get()->HasExtension(id))
191 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22192 }
193
[email protected]0f48fca2011-05-19 18:46:35194 // We get the list of external extensions to check from preferences.
195 // It is possible that an extension has preferences but is not loaded.
196 // For example, an extension that requires experimental permissions
197 // will not be loaded if the experimental command line flag is not used.
198 // In this case, do not uninstall.
199 const Extension* extension = GetInstalledExtension(id);
200 if (!extension) {
201 // We can't call UninstallExtension with an unloaded/invalid
202 // extension ID.
203 LOG(WARNING) << "Attempted uninstallation of unloaded/invalid extension "
204 << "with id: " << id;
205 return;
206 }
[email protected]d6ebc9792011-04-07 18:18:33207 UninstallExtension(id, true, NULL);
[email protected]7a4c6852010-09-16 03:44:22208}
209
[email protected]8e4560b62011-01-14 10:09:14210void ExtensionService::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22211 external_extension_providers_.clear();
212}
213
[email protected]8e4560b62011-01-14 10:09:14214void ExtensionService::AddProviderForTesting(
215 ExternalExtensionProviderInterface* test_provider) {
[email protected]a29a517a2011-01-21 21:11:12216 CHECK(test_provider);
[email protected]0a60a2e2010-10-25 16:15:21217 external_extension_providers_.push_back(
[email protected]8e4560b62011-01-14 10:09:14218 linked_ptr<ExternalExtensionProviderInterface>(test_provider));
[email protected]7a4c6852010-09-16 03:44:22219}
220
[email protected]9060d8b02012-01-13 02:14:30221bool ExtensionService::OnExternalExtensionUpdateUrlFound(
[email protected]7a4c6852010-09-16 03:44:22222 const std::string& id,
[email protected]21a5a672010-11-04 10:47:42223 const GURL& update_url,
224 Extension::Location location) {
[email protected]ab22ba42011-01-14 16:36:38225 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
226 CHECK(Extension::IdIsValid(id));
[email protected]a8af9fdb2010-10-28 21:52:20227
[email protected]8a87a5332011-08-11 17:54:59228 const Extension* extension = GetExtensionById(id, true);
229 if (extension) {
230 // Already installed. Skip this install if the current location has
231 // higher priority than |location|.
232 Extension::Location current = extension->location();
233 if (current == Extension::GetHigherPriorityLocation(current, location))
[email protected]9060d8b02012-01-13 02:14:30234 return false;
[email protected]8a87a5332011-08-11 17:54:59235 // Otherwise, overwrite the current installation.
[email protected]7a4c6852010-09-16 03:44:22236 }
[email protected]9060d8b02012-01-13 02:14:30237
238 // Add |id| to the set of pending extensions. If it can not be added,
239 // then there is already a pending record from a higher-priority install
240 // source. In this case, signal that this extension will not be
241 // installed by returning false.
242 if (!pending_extension_manager()->AddFromExternalUpdateUrl(
243 id, update_url, location))
244 return false;
245
[email protected]50067e52011-10-20 23:17:07246 external_extension_url_added_ = true;
[email protected]9060d8b02012-01-13 02:14:30247 return true;
[email protected]7a4c6852010-09-16 03:44:22248}
249
[email protected]454434f2011-08-24 04:00:00250// If a download url matches one of these patterns and has a referrer of the
251// webstore, then we're willing to treat that as a gallery download.
252static const char* kAllowedDownloadURLPatterns[] = {
253 "https://clients2.google.com/service/update2*",
254 "https://clients2.googleusercontent.com/crx/*"
255};
256
[email protected]eaa7dd182010-12-14 11:09:00257bool ExtensionService::IsDownloadFromGallery(const GURL& download_url,
[email protected]a29a517a2011-01-21 21:11:12258 const GURL& referrer_url) {
[email protected]615d88f2011-12-13 01:47:44259 const Extension* download_extension =
260 extensions_.GetHostedAppByURL(ExtensionURLInfo(download_url));
261 const Extension* referrer_extension =
262 extensions_.GetHostedAppByURL(ExtensionURLInfo(referrer_url));
[email protected]9adb9692010-10-29 23:14:02263 const Extension* webstore_app = GetWebStoreApp();
[email protected]d3071992010-10-08 15:24:07264
265 bool referrer_valid = (referrer_extension == webstore_app);
266 bool download_valid = (download_extension == webstore_app);
267
[email protected]454434f2011-08-24 04:00:00268 // We also allow the download to be from a small set of trusted paths.
269 if (!download_valid) {
270 for (size_t i = 0; i < arraysize(kAllowedDownloadURLPatterns); i++) {
271 URLPattern pattern(URLPattern::SCHEME_HTTPS,
272 kAllowedDownloadURLPatterns[i]);
273 if (pattern.MatchesURL(download_url)) {
274 download_valid = true;
275 break;
276 }
277 }
278 }
279
[email protected]d3071992010-10-08 15:24:07280 // If the command-line gallery URL is set, then be a bit more lenient.
281 GURL store_url =
282 GURL(CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
283 switches::kAppsGalleryURL));
284 if (!store_url.is_empty()) {
285 std::string store_tld =
286 net::RegistryControlledDomainService::GetDomainAndRegistry(store_url);
287 if (!referrer_valid) {
288 std::string referrer_tld =
289 net::RegistryControlledDomainService::GetDomainAndRegistry(
290 referrer_url);
291 // The referrer gets stripped when transitioning from https to http,
292 // or when hitting an unknown test cert and that commonly happens in
293 // testing environments. Given this, we allow an empty referrer when
294 // the command-line flag is set.
295 // Otherwise, the TLD must match the TLD of the command-line url.
296 referrer_valid = referrer_url.is_empty() || (referrer_tld == store_tld);
297 }
298
299 if (!download_valid) {
300 std::string download_tld =
301 net::RegistryControlledDomainService::GetDomainAndRegistry(
[email protected]04866c42011-05-03 20:03:50302 download_url);
[email protected]d3071992010-10-08 15:24:07303
304 // Otherwise, the TLD must match the TLD of the command-line url.
305 download_valid = (download_tld == store_tld);
306 }
307 }
308
309 return (referrer_valid && download_valid);
[email protected]b7c2f252009-12-08 00:47:23310}
311
[email protected]d9696672011-03-15 22:45:09312const Extension* ExtensionService::GetInstalledApp(const GURL& url) {
[email protected]615d88f2011-12-13 01:47:44313 const Extension* extension = extensions_.GetExtensionOrAppByURL(
314 ExtensionURLInfo(url));
315 if (extension && extension->is_app())
316 return extension;
[email protected]d9696672011-03-15 22:45:09317
318 return NULL;
319}
320
321bool ExtensionService::IsInstalledApp(const GURL& url) {
322 return !!GetInstalledApp(url);
[email protected]ffb204f22010-12-05 23:20:27323}
324
[email protected]da5683db2011-04-23 17:12:21325void ExtensionService::SetInstalledAppForRenderer(int renderer_child_id,
326 const Extension* app) {
327 installed_app_hosts_[renderer_child_id] = app;
328}
329
330const Extension* ExtensionService::GetInstalledAppForRenderer(
331 int renderer_child_id) {
332 InstalledAppMap::iterator i = installed_app_hosts_.find(renderer_child_id);
333 if (i == installed_app_hosts_.end())
334 return NULL;
335 return i->second;
336}
337
[email protected]6aeac8342010-10-01 20:21:18338// static
[email protected]d6ebc9792011-04-07 18:18:33339// This function is used to implement the command-line switch
[email protected]3bdba0d2011-08-23 07:17:30340// --uninstall-extension, and to uninstall an extension via sync. The LOG
341// statements within this function are used to inform the user if the uninstall
342// cannot be done.
[email protected]eaa7dd182010-12-14 11:09:00343bool ExtensionService::UninstallExtensionHelper(
344 ExtensionService* extensions_service,
[email protected]6aeac8342010-10-01 20:21:18345 const std::string& extension_id) {
[email protected]6aeac8342010-10-01 20:21:18346
[email protected]95da88c42011-03-31 10:07:33347 const Extension* extension =
[email protected]8001df22011-04-28 19:59:47348 extensions_service->GetInstalledExtension(extension_id);
[email protected]95da88c42011-03-31 10:07:33349
[email protected]d6ebc9792011-04-07 18:18:33350 // We can't call UninstallExtension with an invalid extension ID.
[email protected]95da88c42011-03-31 10:07:33351 if (!extension) {
[email protected]6aeac8342010-10-01 20:21:18352 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]95da88c42011-03-31 10:07:33353 << "id: " << extension_id;
354 return false;
[email protected]6aeac8342010-10-01 20:21:18355 }
356
[email protected]d6ebc9792011-04-07 18:18:33357 // The following call to UninstallExtension will not allow an uninstall of a
358 // policy-controlled extension.
359 std::string error;
360 if (!extensions_service->UninstallExtension(extension_id, false, &error)) {
361 LOG(WARNING) << "Cannot uninstall extension with id " << extension_id
362 << ": " << error;
363 return false;
364 }
[email protected]95da88c42011-03-31 10:07:33365
[email protected]6aeac8342010-10-01 20:21:18366 return true;
367}
368
[email protected]eaa7dd182010-12-14 11:09:00369ExtensionService::ExtensionService(Profile* profile,
[email protected]f0841cd2011-01-19 15:07:24370 const CommandLine* command_line,
371 const FilePath& install_directory,
372 ExtensionPrefs* extension_prefs,
[email protected]0436b102011-04-15 18:30:03373 bool autoupdate_enabled,
374 bool extensions_enabled)
[email protected]d8c8f25f2011-11-02 18:18:01375 : profile_(profile),
[email protected]73c47932010-12-06 18:13:43376 extension_prefs_(extension_prefs),
[email protected]cccdf0aa2011-11-11 03:43:38377 settings_frontend_(extensions::SettingsFrontend::Create(profile)),
[email protected]14908b72011-04-20 06:54:36378 pending_extension_manager_(*ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]a9b00ac2009-06-25 21:03:23379 install_directory_(install_directory),
[email protected]0436b102011-04-15 18:30:03380 extensions_enabled_(extensions_enabled),
[email protected]e81dba32009-06-19 20:19:13381 show_extensions_prompts_(true),
[email protected]3ecda252010-11-18 19:50:55382 ready_(false),
[email protected]14908b72011-04-20 06:54:36383 toolbar_model_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]8b280302011-10-13 22:22:23384 menu_manager_(profile),
[email protected]af9db5f2011-10-05 05:13:15385 app_notification_manager_(new AppNotificationManager(profile)),
[email protected]18cc5a52011-04-15 16:03:38386 apps_promo_(profile->GetPrefs()),
[email protected]7c1490da2011-10-11 18:53:25387 event_routers_initialized_(false),
[email protected]a1755bd2011-12-09 00:40:20388 extension_warnings_(profile),
[email protected]e1fae1c2011-12-13 22:17:17389 socket_controller_(NULL),
[email protected]178f8512012-02-09 01:49:36390 app_shortcut_manager_(profile) {
[email protected]a29a517a2011-01-21 21:11:12391 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20392
[email protected]36a784c2009-06-23 06:21:08393 // Figure out if extension installation should be enabled.
[email protected]6d60703b2009-08-29 01:29:23394 if (command_line->HasSwitch(switches::kDisableExtensions)) {
395 extensions_enabled_ = false;
396 } else if (profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions)) {
397 extensions_enabled_ = false;
[email protected]6b75ec32009-08-14 06:37:18398 }
[email protected]36a784c2009-06-23 06:21:08399
[email protected]432115822011-07-10 15:52:27400 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07401 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27402 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED,
[email protected]ad50def52011-10-19 23:17:07403 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27404 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07405 content::NotificationService::AllBrowserContextsAndSources());
[email protected]2fb7dc982010-09-29 12:24:28406 pref_change_registrar_.Init(profile->GetPrefs());
407 pref_change_registrar_.Add(prefs::kExtensionInstallAllowList, this);
408 pref_change_registrar_.Add(prefs::kExtensionInstallDenyList, this);
[email protected]4814b512009-11-07 00:12:29409
[email protected]93fd78f42009-07-10 16:43:17410 // Set up the ExtensionUpdater
411 if (autoupdate_enabled) {
412 int update_frequency = kDefaultUpdateFrequencySeconds;
413 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25414 base::StringToInt(command_line->GetSwitchValueASCII(
415 switches::kExtensionsUpdateFrequency),
416 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17417 }
[email protected]82b6e512011-04-12 20:33:27418 updater_.reset(new ExtensionUpdater(this,
419 extension_prefs,
420 profile->GetPrefs(),
421 profile,
422 update_frequency));
[email protected]93fd78f42009-07-10 16:43:17423 }
424
[email protected]25ae0152011-11-18 14:40:02425 component_loader_.reset(
426 new extensions::ComponentLoader(this,
427 profile->GetPrefs(),
428 g_browser_process->local_state()));
[email protected]8e4560b62011-01-14 10:09:14429
[email protected]af9db5f2011-10-05 05:13:15430 app_notification_manager_->Init();
431
[email protected]0436b102011-04-15 18:30:03432 if (extensions_enabled_) {
[email protected]1f2f3002011-12-28 00:40:21433 if (!command_line->HasSwitch(switches::kImport) &&
434 !command_line->HasSwitch(switches::kImportFromFile)) {
435 ExternalExtensionProviderImpl::CreateExternalProviders(
436 this, profile_, &external_extension_providers_);
437 }
[email protected]873531342011-03-09 12:16:05438 }
[email protected]b671760b2010-07-15 21:13:47439
[email protected]aa96d3a2010-08-21 08:45:25440 // Use monochrome icons for Omnibox icons.
[email protected]29d0d4ac2010-09-08 21:10:31441 omnibox_popup_icon_manager_.set_monochrome(true);
[email protected]b671760b2010-07-15 21:13:47442 omnibox_icon_manager_.set_monochrome(true);
[email protected]29d0d4ac2010-09-08 21:10:31443 omnibox_icon_manager_.set_padding(gfx::Insets(0, kOmniboxIconPaddingLeft,
444 0, kOmniboxIconPaddingRight));
[email protected]cb0e50312011-05-09 15:03:07445
446 // How long is the path to the Extensions directory?
447 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ExtensionRootPathLength",
448 install_directory_.value().length(), 0, 500, 100);
[email protected]6014d672008-12-05 00:38:25449}
450
[email protected]84df8332011-12-06 18:22:46451const ExtensionSet* ExtensionService::extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45452 return &extensions_;
453}
454
[email protected]84df8332011-12-06 18:22:46455const ExtensionSet* ExtensionService::disabled_extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45456 return &disabled_extensions_;
457}
458
[email protected]84df8332011-12-06 18:22:46459const ExtensionSet* ExtensionService::terminated_extensions() const {
[email protected]bb7f40952011-01-13 00:21:20460 return &terminated_extensions_;
461}
462
[email protected]7f4308d2012-01-18 07:43:01463const ExtensionSet* ExtensionService::GenerateInstalledExtensionsSet() const {
464 ExtensionSet* installed_extensions = new ExtensionSet();
465 installed_extensions->InsertAll(extensions_);
466 installed_extensions->InsertAll(disabled_extensions_);
467 installed_extensions->InsertAll(terminated_extensions_);
468 return installed_extensions;
469}
470
[email protected]b2907fd2011-03-25 16:43:37471PendingExtensionManager* ExtensionService::pending_extension_manager() {
472 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45473}
474
[email protected]eaa7dd182010-12-14 11:09:00475ExtensionService::~ExtensionService() {
[email protected]c8d407e2011-04-28 21:27:17476 // No need to unload extensions here because they are profile-scoped, and the
477 // profile is in the process of being deleted.
[email protected]94b4ffc2011-02-04 05:55:17478
[email protected]8e4560b62011-01-14 10:09:14479 ProviderCollection::const_iterator i;
480 for (i = external_extension_providers_.begin();
481 i != external_extension_providers_.end(); ++i) {
482 ExternalExtensionProviderInterface* provider = i->get();
483 provider->ServiceShutdown();
484 }
[email protected]a1755bd2011-12-09 00:40:20485
486 // TODO(miket): if we find ourselves adding more and more per-API
487 // controllers, we should manage them all with an
488 // APIControllerController (still working on that name).
[email protected]e1fae1c2011-12-13 22:17:17489 if (socket_controller_) {
490 // If this check failed, then a unit test was using sockets but didn't
491 // provide the IO thread message loop needed for those sockets to do their
492 // job (including destroying themselves at shutdown).
493 DCHECK(BrowserThread::IsMessageLoopValid(BrowserThread::IO));
[email protected]cff06c572012-01-02 20:03:05494 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, socket_controller_);
[email protected]e1fae1c2011-12-13 22:17:17495 }
[email protected]6014d672008-12-05 00:38:25496}
497
[email protected]d91268022011-08-26 13:17:37498void ExtensionService::InitEventRoutersAfterImport() {
[email protected]cf593af2011-12-30 05:44:39499 RegisterForImportFinished();
500}
501
502void ExtensionService::RegisterForImportFinished() {
503 if (!registrar_.IsRegistered(this, chrome::NOTIFICATION_IMPORT_FINISHED,
504 content::Source<Profile>(profile_))) {
505 registrar_.Add(this, chrome::NOTIFICATION_IMPORT_FINISHED,
506 content::Source<Profile>(profile_));
507 }
508}
509
510void ExtensionService::InitAfterImport() {
511 CheckForExternalUpdates();
512
513 GarbageCollectExtensions();
514
515 // Idempotent, so although there is a possible race if the import
516 // process finished sometime in the middle of ProfileImpl::InitExtensions,
517 // it cannot happen twice.
518 InitEventRouters();
[email protected]d91268022011-08-26 13:17:37519}
520
[email protected]eaa7dd182010-12-14 11:09:00521void ExtensionService::InitEventRouters() {
[email protected]b3d62312b12010-10-14 21:10:18522 if (event_routers_initialized_)
523 return;
524
[email protected]370e9502011-09-14 19:31:44525 downloads_event_router_.reset(new ExtensionDownloadsEventRouter(profile_));
[email protected]d2e6bd62011-10-24 20:29:07526 history_event_router_.reset(new HistoryExtensionEventRouter());
[email protected]f825df22011-06-28 17:36:10527 history_event_router_->ObserveProfile(profile_);
[email protected]97d2f1d2011-01-15 00:41:08528 browser_event_router_.reset(new ExtensionBrowserEventRouter(profile_));
529 browser_event_router_->Init();
[email protected]118de7892011-03-16 13:31:40530 preference_event_router_.reset(new ExtensionPreferenceEventRouter(profile_));
[email protected]23d5f172011-10-25 05:49:53531 bookmark_event_router_.reset(new BookmarkExtensionEventRouter(
[email protected]f825df22011-06-28 17:36:10532 profile_->GetBookmarkModel()));
533 bookmark_event_router_->Init();
534 cookies_event_router_.reset(new ExtensionCookiesEventRouter(profile_));
535 cookies_event_router_->Init();
536 management_event_router_.reset(new ExtensionManagementEventRouter(profile_));
537 management_event_router_->Init();
[email protected]7da9a8232011-05-27 21:07:11538 ExtensionProcessesEventRouter::GetInstance()->ObserveProfile(profile_);
[email protected]f825df22011-06-28 17:36:10539 web_navigation_event_router_.reset(
540 new ExtensionWebNavigationEventRouter(profile_));
541 web_navigation_event_router_->Init();
[email protected]27072cad2011-05-09 19:46:40542
[email protected]eed367e2011-04-12 03:43:31543#if defined(OS_CHROMEOS)
[email protected]b16fc2e2011-07-21 21:32:58544 file_browser_event_router_.reset(
545 new ExtensionFileBrowserEventRouter(profile_));
546 file_browser_event_router_->ObserveFileSystemEvents();
[email protected]771a6182011-10-05 16:32:52547
548 input_method_event_router_.reset(
549 new chromeos::ExtensionInputMethodEventRouter);
[email protected]be5bd5742011-07-14 07:21:01550
[email protected]c8bf09be2011-06-25 03:22:27551 ExtensionMediaPlayerEventRouter::GetInstance()->Init(profile_);
[email protected]8baf0dbb2011-11-27 22:47:02552 ExtensionInputImeEventRouter::GetInstance()->Init();
[email protected]eed367e2011-04-12 03:43:31553#endif
[email protected]27072cad2011-05-09 19:46:40554
[email protected]9a2aebe02011-12-02 21:54:14555#if defined(OS_CHROMEOS) && defined(USE_VIRTUAL_KEYBOARD)
[email protected]27072cad2011-05-09 19:46:40556 ExtensionInputUiEventRouter::GetInstance()->Init();
557#endif
558
[email protected]b3d62312b12010-10-14 21:10:18559 event_routers_initialized_ = true;
[email protected]c5ae74ab2010-04-15 18:14:37560}
561
[email protected]b2907fd2011-03-25 16:43:37562const Extension* ExtensionService::GetExtensionById(
563 const std::string& id, bool include_disabled) const {
[email protected]8001df22011-04-28 19:59:47564 return GetExtensionByIdInternal(id, true, include_disabled, false);
[email protected]78994ab02010-12-08 18:06:44565}
566
[email protected]eaa7dd182010-12-14 11:09:00567void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12568 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20569
[email protected]fa6a9102010-11-22 15:38:50570 DCHECK(!ready_); // Can't redo init.
[email protected]93fd78f42009-07-10 16:43:17571 DCHECK_EQ(extensions_.size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32572
[email protected]d8c8f25f2011-11-02 18:18:01573 component_loader_->LoadAll();
574 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]894bb502009-05-21 22:39:57575
[email protected]7a161db2011-12-15 20:45:51576 // If we are running in the import process, don't bother initializing the
577 // extension service since this can interfere with the main browser process
578 // that is already running an extension service for this profile.
579 // TODO(aa): can we start up even less of ExtensionService?
580 // http://crbug.com/107636
[email protected]1f2f3002011-12-28 00:40:21581 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kImport) &&
582 !CommandLine::ForCurrentProcess()->HasSwitch(switches::kImportFromFile)) {
[email protected]cf593af2011-12-30 05:44:39583 if (g_browser_process->profile_manager() &&
584 g_browser_process->profile_manager()->will_import()) {
585 RegisterForImportFinished();
586 } else {
587 // TODO(erikkay) this should probably be deferred to a future point
588 // rather than running immediately at startup.
589 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57590
[email protected]cf593af2011-12-30 05:44:39591 // TODO(erikkay) this should probably be deferred as well.
592 GarbageCollectExtensions();
593 }
[email protected]7a161db2011-12-15 20:45:51594 }
[email protected]6014d672008-12-05 00:38:25595}
596
[email protected]420a0ec2011-06-01 01:07:03597bool ExtensionService::UpdateExtension(
598 const std::string& id,
599 const FilePath& extension_path,
600 const GURL& download_url,
601 CrxInstaller** out_crx_installer) {
[email protected]a29a517a2011-01-21 21:11:12602 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20603
[email protected]b2907fd2011-03-25 16:43:37604 PendingExtensionInfo pending_extension_info;
605 bool is_pending_extension = pending_extension_manager_.GetById(
606 id, &pending_extension_info);
[email protected]8ef78fd2010-08-19 17:14:32607
[email protected]8001df22011-04-28 19:59:47608 const Extension* extension =
609 GetExtensionByIdInternal(id, true, true, false);
[email protected]5eb375e92010-11-26 07:50:41610 if (!is_pending_extension && !extension) {
[email protected]aa142702010-03-26 01:26:33611 LOG(WARNING) << "Will not update extension " << id
612 << " because it is not installed or pending";
613 // Delete extension_path since we're not creating a CrxInstaller
614 // that would do it for us.
[email protected]14908b72011-04-20 06:54:36615 if (!BrowserThread::PostTask(
616 BrowserThread::FILE, FROM_HERE,
[email protected]53612e82011-10-18 18:00:36617 base::Bind(
[email protected]7296f2762011-11-21 19:23:44618 &extension_file_util::DeleteFile, extension_path, false)))
[email protected]14908b72011-04-20 06:54:36619 NOTREACHED();
[email protected]420a0ec2011-06-01 01:07:03620
621 return false;
[email protected]e957fe52009-06-23 16:51:05622 }
623
[email protected]aa142702010-03-26 01:26:33624 // We want a silent install only for non-pending extensions and
625 // pending extensions that have install_silently set.
626 ExtensionInstallUI* client =
[email protected]b2907fd2011-03-25 16:43:37627 (!is_pending_extension || pending_extension_info.install_silently()) ?
[email protected]aa142702010-03-26 01:26:33628 NULL : new ExtensionInstallUI(profile_);
629
[email protected]d8c8f25f2011-11-02 18:18:01630 scoped_refptr<CrxInstaller> installer(CrxInstaller::Create(this, client));
[email protected]6dfbbf82010-03-12 23:09:16631 installer->set_expected_id(id);
[email protected]ec5b50d2010-10-09 16:35:18632 if (is_pending_extension)
[email protected]b2907fd2011-03-25 16:43:37633 installer->set_install_source(pending_extension_info.install_source());
[email protected]5eb375e92010-11-26 07:50:41634 else if (extension)
635 installer->set_install_source(extension->location());
[email protected]0d3e4a22011-06-23 19:02:52636 if (pending_extension_info.install_silently())
637 installer->set_allow_silent_install(true);
[email protected]75764512011-12-19 19:54:28638 // If the extension came from sync and its auto-update URL is from the
639 // webstore, treat it as a webstore install. Note that we ignore some older
640 // extensions with blank auto-update URLs because we are mostly concerned
641 // with restrictions on NaCl extensions, which are newer.
[email protected]a12ce8b22012-01-17 18:40:53642 int creation_flags = Extension::NO_FLAGS;
[email protected]75764512011-12-19 19:54:28643 if ((extension && extension->from_webstore()) ||
644 (!extension && pending_extension_info.is_from_sync() &&
645 extension_urls::IsWebstoreUpdateUrl(
[email protected]a12ce8b22012-01-17 18:40:53646 pending_extension_info.update_url()))) {
647 creation_flags |= Extension::FROM_WEBSTORE;
648 }
649
650 // Bookmark apps being updated is kind of a contradiction, but that's because
651 // we mark the default apps as bookmark apps, and they're hosted in the web
652 // store, thus they can get updated. See http://crbug.com/101605 for more
653 // details.
654 if (extension && extension->from_bookmark())
655 creation_flags |= Extension::FROM_BOOKMARK;
656
657 if (extension) {
658 // Additionally, if the extension is an external extension, we preserve the
659 // creation flags (usually from_bookmark), even if the current pref values
660 // don't reflect them. This is to fix http://crbug.com/109791 for users that
661 // had default apps updated and lost the from_bookmark bit.
662 ProviderCollection::const_iterator i;
663 for (i = external_extension_providers_.begin();
664 i != external_extension_providers_.end(); ++i) {
665 ExternalExtensionProviderInterface* provider = i->get();
666 if (provider->HasExtension(extension->id())) {
667 creation_flags |= provider->GetCreationFlags();
668 break;
669 }
670 }
671 }
672 installer->set_creation_flags(creation_flags);
673
[email protected]6dfbbf82010-03-12 23:09:16674 installer->set_delete_source(true);
[email protected]655b2b1a2011-10-13 17:13:06675 installer->set_download_url(download_url);
[email protected]cb0e50312011-05-09 15:03:07676 installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
[email protected]6dfbbf82010-03-12 23:09:16677 installer->InstallCrx(extension_path);
[email protected]420a0ec2011-06-01 01:07:03678
679 if (out_crx_installer)
680 *out_crx_installer = installer;
681
682 return true;
[email protected]e957fe52009-06-23 16:51:05683}
684
[email protected]eaa7dd182010-12-14 11:09:00685void ExtensionService::ReloadExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12686 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]b65272f2009-08-31 15:47:06687 FilePath path;
[email protected]9adb9692010-10-29 23:14:02688 const Extension* current_extension = GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40689
[email protected]f17dbd42010-08-16 23:21:10690 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]b65272f2009-08-31 15:47:06691 if (current_extension) {
[email protected]4814b512009-11-07 00:12:29692 // If the extension has an inspector open for its background page, detach
693 // the inspector and hang onto a cookie for it, so that we can reattach
694 // later.
695 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
[email protected]06024c62011-10-20 20:57:12696 ExtensionHost* host = manager->GetBackgroundHostForExtension(extension_id);
[email protected]0e12d7d2011-12-01 16:21:44697 if (host && DevToolsAgentHostRegistry::HasDevToolsAgentHost(
698 host->render_view_host())) {
[email protected]4814b512009-11-07 00:12:29699 // Look for an open inspector for the background page.
[email protected]0e12d7d2011-12-01 16:21:44700 DevToolsAgentHost* agent =
701 DevToolsAgentHostRegistry::GetDevToolsAgentHost(
702 host->render_view_host());
703 int devtools_cookie =
704 content::DevToolsManager::GetInstance()->DetachClientHost(agent);
[email protected]4814b512009-11-07 00:12:29705 if (devtools_cookie >= 0)
706 orphaned_dev_tools_[extension_id] = devtools_cookie;
707 }
708
[email protected]b65272f2009-08-31 15:47:06709 path = current_extension->path();
[email protected]f17dbd42010-08-16 23:21:10710 DisableExtension(extension_id);
711 disabled_extension_paths_[extension_id] = path;
[email protected]1eb175082010-02-10 09:26:16712 } else {
713 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06714 }
715
[email protected]e6090e42010-03-23 22:44:08716 // Check the installed extensions to see if what we're reloading was already
717 // installed.
718 scoped_ptr<ExtensionInfo> installed_extension(
719 extension_prefs_->GetInstalledExtensionInfo(extension_id));
720 if (installed_extension.get() &&
721 installed_extension->extension_manifest.get()) {
[email protected]d8c8f25f2011-11-02 18:18:01722 extensions::InstalledLoader(this).Load(*installed_extension, false);
[email protected]e6090e42010-03-23 22:44:08723 } else {
[email protected]d8c8f25f2011-11-02 18:18:01724 // Otherwise, the extension is unpacked (location LOAD).
[email protected]e6090e42010-03-23 22:44:08725 // We should always be able to remember the extension's path. If it's not in
726 // the map, someone failed to update |unloaded_extension_paths_|.
727 CHECK(!path.empty());
[email protected]d8c8f25f2011-11-02 18:18:01728 extensions::UnpackedInstaller::Create(this)->Load(path);
[email protected]e6090e42010-03-23 22:44:08729 }
[email protected]9cddd4702009-07-27 22:09:40730}
731
[email protected]fa2416f2011-05-03 08:41:20732bool ExtensionService::UninstallExtension(
[email protected]a12c706e2011-12-01 00:58:08733 std::string extension_id,
[email protected]fa2416f2011-05-03 08:41:20734 bool external_uninstall,
735 std::string* error) {
[email protected]a29a517a2011-01-21 21:11:12736 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20737
[email protected]0d6ec3a72011-09-02 02:09:43738 scoped_refptr<const Extension> extension(GetInstalledExtension(extension_id));
[email protected]631cf822009-05-15 07:01:25739
[email protected]e7afe2452010-08-22 16:19:13740 // Callers should not send us nonexistent extensions.
[email protected]aebe23a32010-12-10 22:15:48741 CHECK(extension);
[email protected]9f1087e2009-06-15 17:29:32742
[email protected]95da88c42011-03-31 10:07:33743 // Policy change which triggers an uninstall will always set
744 // |external_uninstall| to true so this is the only way to uninstall
745 // managed extensions.
[email protected]0d6ec3a72011-09-02 02:09:43746 if (!Extension::UserMayDisable(extension->location()) &&
747 !external_uninstall) {
[email protected]ad50def52011-10-19 23:17:07748 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27749 chrome::NOTIFICATION_EXTENSION_UNINSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:53750 content::Source<Profile>(profile_),
751 content::Details<const Extension>(extension));
[email protected]d6ebc9792011-04-07 18:18:33752 if (error != NULL) {
753 *error = errors::kCannotUninstallManagedExtension;
754 }
755 return false;
756 }
[email protected]95da88c42011-03-31 10:07:33757
[email protected]3bdba0d2011-08-23 07:17:30758 // Extract the data we need for sync now, but don't actually sync until we've
759 // completed the uninstallation.
760 SyncBundle* sync_bundle = GetSyncBundleForExtension(*extension);
761
762 SyncChange sync_change;
763 if (sync_bundle) {
[email protected]aa7599d2011-10-28 07:24:32764 ExtensionSyncData extension_sync_data(
765 *extension,
766 IsExtensionEnabled(extension_id),
767 IsIncognitoEnabled(extension_id),
[email protected]b2689a902011-12-01 00:41:09768 extension_prefs_->GetAppNotificationClientId(extension_id),
[email protected]168389f2011-12-20 17:12:48769 extension_prefs_->IsAppNotificationDisabled(extension_id),
770 GetAppLaunchOrdinal(extension_id),
771 GetPageOrdinal(extension_id));
[email protected]3bdba0d2011-08-23 07:17:30772 sync_change = extension_sync_data.GetSyncChange(SyncChange::ACTION_DELETE);
773 }
774
[email protected]9b217652010-10-08 22:04:23775 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08776 extension->GetType(), 100);
[email protected]fe2dd7742011-04-19 22:52:49777 RecordPermissionMessagesHistogram(
778 extension, "Extensions.Permissions_Uninstall");
[email protected]9b217652010-10-08 22:04:23779
[email protected]8e5c89a2011-06-07 18:13:33780 TemplateURLService* url_service =
781 TemplateURLServiceFactory::GetForProfile(profile_);
782 if (url_service)
783 url_service->UnregisterExtensionKeyword(extension);
[email protected]56ad3792010-05-28 17:45:33784
[email protected]831aa212010-03-26 13:55:19785 // Unload before doing more cleanup to ensure that nothing is hanging on to
786 // any of these resources.
[email protected]814a7bf0f2011-08-13 05:30:59787 UnloadExtension(extension_id, extension_misc::UNLOAD_REASON_UNINSTALL);
[email protected]831aa212010-03-26 13:55:19788
[email protected]0d6ec3a72011-09-02 02:09:43789 extension_prefs_->OnExtensionUninstalled(extension_id, extension->location(),
[email protected]831aa212010-03-26 13:55:19790 external_uninstall);
[email protected]9f1087e2009-06-15 17:29:32791
792 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]0d6ec3a72011-09-02 02:09:43793 if (Extension::LOAD != extension->location()) {
[email protected]14908b72011-04-20 06:54:36794 if (!BrowserThread::PostTask(
795 BrowserThread::FILE, FROM_HERE,
[email protected]53612e82011-10-18 18:00:36796 base::Bind(
[email protected]14908b72011-04-20 06:54:36797 &extension_file_util::UninstallExtension,
798 install_directory_,
[email protected]fa2416f2011-05-03 08:41:20799 extension_id)))
[email protected]14908b72011-04-20 06:54:36800 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32801 }
802
[email protected]0d6ec3a72011-09-02 02:09:43803 GURL launch_web_url_origin(extension->launch_web_url());
804 launch_web_url_origin = launch_web_url_origin.GetOrigin();
805 bool is_storage_isolated =
806 (extension->is_storage_isolated() &&
807 extension->HasAPIPermission(ExtensionAPIPermission::kExperimental));
808
809 if (extension->is_hosted_app() &&
810 !profile_->GetExtensionSpecialStoragePolicy()->
811 IsStorageProtected(launch_web_url_origin)) {
[email protected]dc0b5a12011-10-14 00:06:13812 ExtensionDataDeleter::StartDeleting(
813 profile_, extension_id, launch_web_url_origin, is_storage_isolated);
[email protected]0d6ec3a72011-09-02 02:09:43814 }
[email protected]dc0b5a12011-10-14 00:06:13815 ExtensionDataDeleter::StartDeleting(
816 profile_, extension_id, extension->url(), is_storage_isolated);
[email protected]0d6ec3a72011-09-02 02:09:43817
[email protected]0dfe05c2011-02-23 23:03:36818 UntrackTerminatedExtension(extension_id);
[email protected]211030342010-09-30 18:41:06819
820 // Notify interested parties that we've uninstalled this extension.
[email protected]ad50def52011-10-19 23:17:07821 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27822 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
[email protected]6c2381d2011-10-19 02:52:53823 content::Source<Profile>(profile_),
824 content::Details<const std::string>(&extension_id));
[email protected]d6ebc9792011-04-07 18:18:33825
[email protected]3bdba0d2011-08-23 07:17:30826 if (sync_bundle && sync_bundle->HasExtensionId(extension_id)) {
827 sync_bundle->sync_processor->ProcessSyncChanges(
828 FROM_HERE, SyncChangeList(1, sync_change));
829 sync_bundle->synced_extensions.erase(extension_id);
830 }
831
[email protected]333b1de2011-09-12 18:28:50832 // Track the uninstallation.
[email protected]49098f702011-10-13 03:47:18833 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionUninstalled", 1, 2);
834
835 static bool default_apps_trial_exists =
[email protected]568c3312011-12-06 15:36:04836 base::FieldTrialList::TrialExists(kDefaultAppsTrialName);
[email protected]49098f702011-10-13 03:47:18837 if (default_apps_trial_exists) {
838 UMA_HISTOGRAM_ENUMERATION(
839 base::FieldTrial::MakeName("Extensions.ExtensionUninstalled",
[email protected]568c3312011-12-06 15:36:04840 kDefaultAppsTrialName),
[email protected]49098f702011-10-13 03:47:18841 1, 2);
842 }
[email protected]333b1de2011-09-12 18:28:50843
[email protected]7c1490da2011-10-11 18:53:25844 // Uninstalling one extension might have solved the problems of others.
845 // Therefore, we clear warnings of this type for all extensions.
846 std::set<ExtensionWarningSet::WarningType> warnings;
847 extension_warnings_.GetWarningsAffectingExtension(extension_id, &warnings);
848 extension_warnings_.ClearWarnings(warnings);
849
[email protected]d6ebc9792011-04-07 18:18:33850 return true;
[email protected]c10da4b02010-03-25 14:38:32851}
852
[email protected]c3cfb012011-04-06 22:07:35853bool ExtensionService::IsExtensionEnabled(
854 const std::string& extension_id) const {
[email protected]84df8332011-12-06 18:22:46855 if (extensions_.Contains(extension_id) ||
856 terminated_extensions_.Contains(extension_id))
[email protected]36429da2011-07-11 20:25:18857 return true;
858
[email protected]84df8332011-12-06 18:22:46859 if (disabled_extensions_.Contains(extension_id))
[email protected]ad83ca242011-07-29 01:32:25860 return false;
861
862 // If the extension hasn't been loaded yet, check the prefs for it. Assume
863 // enabled unless otherwise noted.
864 return !extension_prefs_->IsExtensionDisabled(extension_id) &&
865 !extension_prefs_->IsExternalExtensionUninstalled(extension_id);
[email protected]c3cfb012011-04-06 22:07:35866}
867
868bool ExtensionService::IsExternalExtensionUninstalled(
869 const std::string& extension_id) const {
870 return extension_prefs_->IsExternalExtensionUninstalled(extension_id);
871}
872
[email protected]eaa7dd182010-12-14 11:09:00873void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12874 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20875
[email protected]06f92562011-04-29 19:27:31876 if (IsExtensionEnabled(extension_id))
[email protected]0c6da502009-08-14 22:32:39877 return;
[email protected]0c6da502009-08-14 22:32:39878
[email protected]b3317ad2011-04-28 23:46:00879 extension_prefs_->SetExtensionState(extension_id, Extension::ENABLED);
[email protected]1784e83a2009-09-08 21:01:52880
[email protected]06f92562011-04-29 19:27:31881 const Extension* extension =
882 GetExtensionByIdInternal(extension_id, false, true, false);
883 // This can happen if sync enables an extension that is not
884 // installed yet.
885 if (!extension)
886 return;
887
[email protected]0c6da502009-08-14 22:32:39888 // Move it over to the enabled list.
[email protected]84df8332011-12-06 18:22:46889 extensions_.Insert(make_scoped_refptr(extension));
890 disabled_extensions_.Remove(extension->id());
[email protected]0c6da502009-08-14 22:32:39891
[email protected]f681c782010-11-19 11:19:39892 // Make sure any browser action contained within it is not hidden.
893 extension_prefs_->SetBrowserActionVisibility(extension, true);
894
[email protected]62d30f42009-10-01 22:36:06895 NotifyExtensionLoaded(extension);
[email protected]3bdba0d2011-08-23 07:17:30896
897 SyncExtensionChangeIfNeeded(*extension);
[email protected]0c6da502009-08-14 22:32:39898}
899
[email protected]eaa7dd182010-12-14 11:09:00900void ExtensionService::DisableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12901 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20902
[email protected]b2ba9962009-12-10 20:10:15903 // The extension may have been disabled already.
[email protected]06f92562011-04-29 19:27:31904 if (!IsExtensionEnabled(extension_id))
[email protected]1784e83a2009-09-08 21:01:52905 return;
[email protected]1784e83a2009-09-08 21:01:52906
[email protected]06f92562011-04-29 19:27:31907 const Extension* extension = GetInstalledExtension(extension_id);
908 // |extension| can be NULL if sync disables an extension that is not
909 // installed yet.
910 if (extension && !Extension::UserMayDisable(extension->location()))
[email protected]95da88c42011-03-31 10:07:33911 return;
912
[email protected]b3317ad2011-04-28 23:46:00913 extension_prefs_->SetExtensionState(extension_id, Extension::DISABLED);
[email protected]1784e83a2009-09-08 21:01:52914
[email protected]06f92562011-04-29 19:27:31915 extension = GetExtensionByIdInternal(extension_id, true, false, true);
916 if (!extension)
917 return;
918
[email protected]1784e83a2009-09-08 21:01:52919 // Move it over to the disabled list.
[email protected]84df8332011-12-06 18:22:46920 disabled_extensions_.Insert(make_scoped_refptr(extension));
921 if (extensions_.Contains(extension->id()))
922 extensions_.Remove(extension->id());
923 else
924 terminated_extensions_.Remove(extension->id());
[email protected]1784e83a2009-09-08 21:01:52925
[email protected]814a7bf0f2011-08-13 05:30:59926 NotifyExtensionUnloaded(extension, extension_misc::UNLOAD_REASON_DISABLE);
[email protected]3bdba0d2011-08-23 07:17:30927
928 SyncExtensionChangeIfNeeded(*extension);
[email protected]7c1490da2011-10-11 18:53:25929
930 // Deactivating one extension might have solved the problems of others.
931 // Therefore, we clear warnings of this type for all extensions.
932 std::set<ExtensionWarningSet::WarningType> warnings;
933 extension_warnings_.GetWarningsAffectingExtension(extension_id, &warnings);
934 extension_warnings_.ClearWarnings(warnings);
[email protected]1784e83a2009-09-08 21:01:52935}
936
[email protected]eaa7dd182010-12-14 11:09:00937void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]8d888c12010-11-30 00:00:25938 const Extension* extension) {
939 CHECK(extension);
[email protected]fe2dd7742011-04-19 22:52:49940 RecordPermissionMessagesHistogram(
941 extension, "Extensions.Permissions_ReEnable");
[email protected]c333e792012-01-06 16:57:39942 extensions::PermissionsUpdater perms_updater(profile());
943 perms_updater.GrantActivePermissions(extension);
[email protected]8d888c12010-11-30 00:00:25944 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
945 EnableExtension(extension->id());
946}
947
[email protected]fe2dd7742011-04-19 22:52:49948// static
949void ExtensionService::RecordPermissionMessagesHistogram(
950 const Extension* e, const char* histogram) {
951 // Since this is called from multiple sources, and since the Histogram macros
952 // use statics, we need to manually lookup the Histogram ourselves.
953 base::Histogram* counter = base::LinearHistogram::FactoryGet(
954 histogram,
955 1,
[email protected]0d3e4a22011-06-23 19:02:52956 ExtensionPermissionMessage::kEnumBoundary,
957 ExtensionPermissionMessage::kEnumBoundary + 1,
[email protected]fe2dd7742011-04-19 22:52:49958 base::Histogram::kUmaTargetedHistogramFlag);
959
[email protected]0d3e4a22011-06-23 19:02:52960 ExtensionPermissionMessages permissions = e->GetPermissionMessages();
[email protected]fe2dd7742011-04-19 22:52:49961 if (permissions.empty()) {
[email protected]0d3e4a22011-06-23 19:02:52962 counter->Add(ExtensionPermissionMessage::kNone);
[email protected]fe2dd7742011-04-19 22:52:49963 } else {
[email protected]0d3e4a22011-06-23 19:02:52964 for (ExtensionPermissionMessages::iterator it = permissions.begin();
965 it != permissions.end(); ++it)
966 counter->Add(it->id());
[email protected]fe2dd7742011-04-19 22:52:49967 }
968}
969
[email protected]eaa7dd182010-12-14 11:09:00970void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]57a777f72010-03-31 01:09:42971 // The ChromeURLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:06972 // was loaded, otherwise a race can arise where a renderer that is created
973 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:42974 // that the request context doesn't yet know about. The profile is responsible
975 // for ensuring its URLRequestContexts appropriately discover the loaded
976 // extension.
[email protected]c8d407e2011-04-28 21:27:17977 profile_->RegisterExtensionWithRequestContexts(extension);
[email protected]62d30f42009-10-01 22:36:06978
[email protected]19647262011-12-16 09:57:49979 // Tell renderers about the new extension, unless it's a theme (renderers
980 // don't need to know about themes).
981 if (!extension->is_theme()) {
982 for (content::RenderProcessHost::iterator i(
983 content::RenderProcessHost::AllHostsIterator());
984 !i.IsAtEnd(); i.Advance()) {
985 content::RenderProcessHost* host = i.GetCurrentValue();
986 Profile* host_profile =
987 Profile::FromBrowserContext(host->GetBrowserContext());
988 if (host_profile->GetOriginalProfile() ==
989 profile_->GetOriginalProfile()) {
990 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions(
991 1, ExtensionMsg_Loaded_Params(extension));
992 host->Send(
993 new ExtensionMsg_Loaded(loaded_extensions));
994 }
[email protected]c8d407e2011-04-28 21:27:17995 }
[email protected]77a6970c2011-04-23 16:58:56996 }
997
[email protected]3442a662012-01-12 08:06:17998 // Tell subsystems that use the EXTENSION_LOADED notification about the new
999 // extension.
1000 //
1001 // NOTE: It is important that this happen after notifying the renderers about
1002 // the new extensions so that if we navigate to an extension URL in
1003 // NOTIFICATION_EXTENSION_LOADED, the renderer is guaranteed to know about it.
1004 content::NotificationService::current()->Notify(
1005 chrome::NOTIFICATION_EXTENSION_LOADED,
1006 content::Source<Profile>(profile_),
1007 content::Details<const Extension>(extension));
1008
[email protected]c8d407e2011-04-28 21:27:171009 // Tell a random-ass collection of other subsystems about the new extension.
1010 // TODO(aa): What should we do with all this goop? Can it move into the
1011 // relevant objects via EXTENSION_LOADED?
1012
1013 profile_->GetExtensionSpecialStoragePolicy()->
1014 GrantRightsForExtension(extension);
1015
1016 UpdateActiveExtensionsInCrashReporter();
1017
1018 ExtensionWebUI::RegisterChromeURLOverrides(
1019 profile_, extension->GetChromeURLOverrides());
1020
[email protected]8e5c89a2011-06-07 18:13:331021 TemplateURLService* url_service =
1022 TemplateURLServiceFactory::GetForProfile(profile_);
1023 if (url_service)
1024 url_service->RegisterExtensionKeyword(extension);
[email protected]c8d407e2011-04-28 21:27:171025
1026 // Load the icon for omnibox-enabled extensions so it will be ready to display
1027 // in the URL bar.
1028 if (!extension->omnibox_keyword().empty()) {
1029 omnibox_popup_icon_manager_.LoadIcon(extension);
1030 omnibox_icon_manager_.LoadIcon(extension);
1031 }
1032
1033 // If the extension has permission to load chrome://favicon/ resources we need
1034 // to make sure that the FaviconSource is registered with the
1035 // ChromeURLDataManager.
1036 if (extension->HasHostPermission(GURL(chrome::kChromeUIFaviconURL))) {
1037 FaviconSource* favicon_source = new FaviconSource(profile_,
1038 FaviconSource::FAVICON);
1039 profile_->GetChromeURLDataManager()->AddDataSource(favicon_source);
1040 }
[email protected]5eddc3e2011-10-26 04:33:311041 // Same for chrome://thumb/ resources.
1042 if (extension->HasHostPermission(GURL(chrome::kChromeUIThumbnailURL))) {
1043 ThumbnailSource* thumbnail_source = new ThumbnailSource(profile_);
1044 profile_->GetChromeURLDataManager()->AddDataSource(thumbnail_source);
1045 }
[email protected]c8d407e2011-04-28 21:27:171046
1047 // TODO(mpcomplete): This ends up affecting all profiles. See crbug.com/80757.
[email protected]2de307592011-04-05 21:16:581048 bool plugins_changed = false;
1049 for (size_t i = 0; i < extension->plugins().size(); ++i) {
1050 const Extension::PluginInfo& plugin = extension->plugins()[i];
[email protected]f520b5b2011-11-08 02:42:141051 PluginService::GetInstance()->RefreshPlugins();
1052 PluginService::GetInstance()->AddExtraPluginPath(plugin.path);
[email protected]2de307592011-04-05 21:16:581053 plugins_changed = true;
[email protected]476a27032011-09-13 13:40:491054 ChromePluginServiceFilter* filter =
1055 ChromePluginServiceFilter::GetInstance();
1056 if (plugin.is_public) {
1057 filter->RestrictPluginToProfileAndOrigin(
1058 plugin.path, profile_, GURL());
1059 } else {
1060 filter->RestrictPluginToProfileAndOrigin(
1061 plugin.path, profile_, extension->url());
[email protected]2de307592011-04-05 21:16:581062 }
1063 }
[email protected]84396dbc2011-04-14 06:33:421064
1065 bool nacl_modules_changed = false;
1066 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1067 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
[email protected]cebc3dc2011-04-18 17:15:001068 RegisterNaClModule(module.url, module.mime_type);
[email protected]84396dbc2011-04-14 06:33:421069 nacl_modules_changed = true;
1070 }
1071
[email protected]ed0ba002011-05-26 16:55:131072 if (nacl_modules_changed)
1073 UpdatePluginListWithNaClModules();
[email protected]84396dbc2011-04-14 06:33:421074
1075 if (plugins_changed || nacl_modules_changed)
[email protected]45a22e62011-10-12 09:48:021076 PluginService::GetInstance()->PurgePluginListCache(profile_, false);
[email protected]b4d9d122011-06-17 01:58:541077
[email protected]14da67b02011-08-02 05:49:131078#if defined(OS_CHROMEOS)
[email protected]b4d9d122011-06-17 01:58:541079 for (std::vector<Extension::InputComponentInfo>::const_iterator component =
1080 extension->input_components().begin();
1081 component != extension->input_components().end();
1082 ++component) {
[email protected]14da67b02011-08-02 05:49:131083 if (component->type == Extension::INPUT_COMPONENT_TYPE_IME) {
[email protected]8baf0dbb2011-11-27 22:47:021084 ExtensionInputImeEventRouter::GetInstance()->RegisterIme(
[email protected]14da67b02011-08-02 05:49:131085 profile_, extension->id(), *component);
1086 }
[email protected]9a2aebe02011-12-02 21:54:141087#if defined(USE_VIRTUAL_KEYBOARD)
[email protected]b4d9d122011-06-17 01:58:541088 if (component->type == Extension::INPUT_COMPONENT_TYPE_VIRTUAL_KEYBOARD &&
1089 !component->layouts.empty()) {
[email protected]a4215ac02011-12-04 04:11:561090 chromeos::input_method::InputMethodManager* input_method_manager =
1091 chromeos::input_method::InputMethodManager::GetInstance();
1092 const bool is_system_keyboard =
1093 extension->location() == Extension::COMPONENT;
[email protected]cbd4c042011-08-13 18:22:191094 input_method_manager->RegisterVirtualKeyboard(
1095 extension->url(),
1096 component->name, // human-readable name of the keyboard extension.
1097 component->layouts,
[email protected]a4215ac02011-12-04 04:11:561098 is_system_keyboard);
[email protected]b4d9d122011-06-17 01:58:541099 }
[email protected]14da67b02011-08-02 05:49:131100#endif
[email protected]b4d9d122011-06-17 01:58:541101 }
1102#endif
[email protected]62d30f42009-10-01 22:36:061103}
1104
[email protected]a9f39a312010-12-23 22:14:271105void ExtensionService::NotifyExtensionUnloaded(
[email protected]814a7bf0f2011-08-13 05:30:591106 const Extension* extension,
1107 extension_misc::UnloadedExtensionReason reason) {
[email protected]a9f39a312010-12-23 22:14:271108 UnloadedExtensionInfo details(extension, reason);
[email protected]ad50def52011-10-19 23:17:071109 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271110 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]6c2381d2011-10-19 02:52:531111 content::Source<Profile>(profile_),
1112 content::Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061113
[email protected]f3b1a082011-11-18 00:34:301114 for (content::RenderProcessHost::iterator i(
1115 content::RenderProcessHost::AllHostsIterator());
[email protected]77a6970c2011-04-23 16:58:561116 !i.IsAtEnd(); i.Advance()) {
[email protected]f3b1a082011-11-18 00:34:301117 content::RenderProcessHost* host = i.GetCurrentValue();
[email protected]9b62ecf2011-07-27 20:23:081118 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:301119 Profile::FromBrowserContext(host->GetBrowserContext());
[email protected]9b62ecf2011-07-27 20:23:081120 if (host_profile->GetOriginalProfile() == profile_->GetOriginalProfile())
[email protected]c8d407e2011-04-28 21:27:171121 host->Send(new ExtensionMsg_Unloaded(extension->id()));
[email protected]77a6970c2011-04-23 16:58:561122 }
1123
[email protected]dd163fb02011-05-04 22:22:171124 profile_->UnregisterExtensionWithRequestContexts(extension->id(), reason);
[email protected]c8d407e2011-04-28 21:27:171125 profile_->GetExtensionSpecialStoragePolicy()->
1126 RevokeRightsForExtension(extension);
1127
1128 ExtensionWebUI::UnregisterChromeURLOverrides(
1129 profile_, extension->GetChromeURLOverrides());
1130
[email protected]b777b332011-04-16 04:01:081131#if defined(OS_CHROMEOS)
1132 // Revoke external file access to
[email protected]c8d407e2011-04-28 21:27:171133 if (profile_->GetFileSystemContext() &&
[email protected]e7e46732012-01-05 11:45:551134 profile_->GetFileSystemContext()->external_provider()) {
1135 profile_->GetFileSystemContext()->external_provider()->
[email protected]c8d407e2011-04-28 21:27:171136 RevokeAccessForExtension(extension->id());
[email protected]62d30f42009-10-01 22:36:061137 }
[email protected]54ccafa2011-12-06 23:36:321138
1139 if (extension->input_components().size() > 0) {
1140 ExtensionInputImeEventRouter::GetInstance()->UnregisterAllImes(
1141 profile_, extension->id());
1142 }
[email protected]c8d407e2011-04-28 21:27:171143#endif
1144
1145 UpdateActiveExtensionsInCrashReporter();
[email protected]2de307592011-04-05 21:16:581146
1147 bool plugins_changed = false;
1148 for (size_t i = 0; i < extension->plugins().size(); ++i) {
1149 const Extension::PluginInfo& plugin = extension->plugins()[i];
[email protected]b6a2f8de2012-01-31 17:28:491150 PluginService::GetInstance()->ForcePluginShutdown(plugin.path);
[email protected]f520b5b2011-11-08 02:42:141151 PluginService::GetInstance()->RefreshPlugins();
1152 PluginService::GetInstance()->RemoveExtraPluginPath(plugin.path);
[email protected]2de307592011-04-05 21:16:581153 plugins_changed = true;
[email protected]476a27032011-09-13 13:40:491154 ChromePluginServiceFilter::GetInstance()->UnrestrictPlugin(plugin.path);
[email protected]2de307592011-04-05 21:16:581155 }
[email protected]84396dbc2011-04-14 06:33:421156
1157 bool nacl_modules_changed = false;
1158 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1159 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
[email protected]cebc3dc2011-04-18 17:15:001160 UnregisterNaClModule(module.url);
[email protected]84396dbc2011-04-14 06:33:421161 nacl_modules_changed = true;
1162 }
1163
[email protected]ed0ba002011-05-26 16:55:131164 if (nacl_modules_changed)
1165 UpdatePluginListWithNaClModules();
[email protected]84396dbc2011-04-14 06:33:421166
1167 if (plugins_changed || nacl_modules_changed)
[email protected]45a22e62011-10-12 09:48:021168 PluginService::GetInstance()->PurgePluginListCache(profile_, false);
[email protected]62d30f42009-10-01 22:36:061169}
1170
[email protected]eaa7dd182010-12-14 11:09:001171void ExtensionService::UpdateExtensionBlacklist(
[email protected]6b75ec32009-08-14 06:37:181172 const std::vector<std::string>& blacklist) {
1173 // Use this set to indicate if an extension in the blacklist has been used.
1174 std::set<std::string> blacklist_set;
1175 for (unsigned int i = 0; i < blacklist.size(); ++i) {
1176 if (Extension::IdIsValid(blacklist[i])) {
1177 blacklist_set.insert(blacklist[i]);
1178 }
1179 }
1180 extension_prefs_->UpdateBlacklist(blacklist_set);
1181 std::vector<std::string> to_be_removed;
1182 // Loop current extensions, unload installed extensions.
[email protected]84df8332011-12-06 18:22:461183 for (ExtensionSet::const_iterator iter = extensions_.begin();
[email protected]6b75ec32009-08-14 06:37:181184 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021185 const Extension* extension = (*iter);
[email protected]6b75ec32009-08-14 06:37:181186 if (blacklist_set.find(extension->id()) != blacklist_set.end()) {
1187 to_be_removed.push_back(extension->id());
1188 }
1189 }
1190
1191 // UnloadExtension will change the extensions_ list. So, we should
1192 // call it outside the iterator loop.
1193 for (unsigned int i = 0; i < to_be_removed.size(); ++i) {
[email protected]814a7bf0f2011-08-13 05:30:591194 UnloadExtension(to_be_removed[i], extension_misc::UNLOAD_REASON_DISABLE);
[email protected]6b75ec32009-08-14 06:37:181195 }
1196}
1197
[email protected]0a071a32011-02-08 00:18:241198Profile* ExtensionService::profile() {
1199 return profile_;
1200}
1201
[email protected]eaa7dd182010-12-14 11:09:001202ExtensionPrefs* ExtensionService::extension_prefs() {
[email protected]ddb1e5a2010-12-13 20:10:451203 return extension_prefs_;
1204}
1205
[email protected]0d9a2202011-11-09 13:48:411206extensions::SettingsFrontend* ExtensionService::settings_frontend() {
[email protected]cccdf0aa2011-11-11 03:43:381207 return settings_frontend_.get();
[email protected]b7f853e282011-08-10 09:24:201208}
1209
[email protected]b790b072011-05-20 09:46:441210ExtensionContentSettingsStore*
1211 ExtensionService::GetExtensionContentSettingsStore() {
1212 return extension_prefs()->content_settings_store();
1213}
1214
[email protected]25ae0152011-11-18 14:40:021215bool ExtensionService::is_ready() {
1216 return ready_;
1217}
1218
[email protected]2859946f2011-04-04 18:18:061219ExtensionUpdater* ExtensionService::updater() {
1220 return updater_.get();
1221}
1222
[email protected]eaa7dd182010-12-14 11:09:001223void ExtensionService::CheckAdminBlacklist() {
[email protected]aa96d3a2010-08-21 08:45:251224 std::vector<std::string> to_be_removed;
1225 // Loop through extensions list, unload installed extensions.
[email protected]84df8332011-12-06 18:22:461226 for (ExtensionSet::const_iterator iter = extensions_.begin();
[email protected]aa96d3a2010-08-21 08:45:251227 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021228 const Extension* extension = (*iter);
[email protected]05aad2da2011-10-28 10:12:371229 if (!extension_prefs_->IsExtensionAllowedByPolicy(extension->id(),
1230 extension->location())) {
[email protected]aa96d3a2010-08-21 08:45:251231 to_be_removed.push_back(extension->id());
[email protected]05aad2da2011-10-28 10:12:371232 }
[email protected]aa96d3a2010-08-21 08:45:251233 }
1234
1235 // UnloadExtension will change the extensions_ list. So, we should
1236 // call it outside the iterator loop.
1237 for (unsigned int i = 0; i < to_be_removed.size(); ++i)
[email protected]814a7bf0f2011-08-13 05:30:591238 UnloadExtension(to_be_removed[i], extension_misc::UNLOAD_REASON_DISABLE);
[email protected]aa96d3a2010-08-21 08:45:251239}
1240
[email protected]31206602011-04-13 23:07:321241void ExtensionService::CheckForUpdatesSoon() {
[email protected]c3cfb012011-04-06 22:07:351242 if (updater()) {
[email protected]31206602011-04-13 23:07:321243 updater()->CheckSoon();
[email protected]c3cfb012011-04-06 22:07:351244 } else {
[email protected]31206602011-04-13 23:07:321245 LOG(WARNING) << "CheckForUpdatesSoon() called with auto-update turned off";
[email protected]c3cfb012011-04-06 22:07:351246 }
1247}
1248
[email protected]3bdba0d2011-08-23 07:17:301249namespace {
1250 bool IsSyncableNone(const Extension& extension) { return false; }
[email protected]406b5a92011-11-08 11:58:261251} // namespace
[email protected]3bdba0d2011-08-23 07:17:301252
1253ExtensionService::SyncBundle::SyncBundle()
1254 : filter(IsSyncableNone),
1255 sync_processor(NULL) {
[email protected]b05fb9ff2011-04-23 00:07:561256}
1257
[email protected]3bdba0d2011-08-23 07:17:301258ExtensionService::SyncBundle::~SyncBundle() {
1259}
1260
1261bool ExtensionService::SyncBundle::HasExtensionId(const std::string& id) const {
1262 return synced_extensions.find(id) != synced_extensions.end();
1263}
1264
1265bool ExtensionService::SyncBundle::HasPendingExtensionId(const std::string& id)
1266 const {
1267 return pending_sync_data.find(id) != pending_sync_data.end();
1268}
1269
1270void ExtensionService::SyncExtensionChangeIfNeeded(const Extension& extension) {
1271 SyncBundle* sync_bundle = GetSyncBundleForExtension(extension);
1272 if (sync_bundle) {
[email protected]aa7599d2011-10-28 07:24:321273 ExtensionSyncData extension_sync_data(
1274 extension,
1275 IsExtensionEnabled(extension.id()),
1276 IsIncognitoEnabled(extension.id()),
[email protected]b2689a902011-12-01 00:41:091277 extension_prefs_->GetAppNotificationClientId(extension.id()),
[email protected]168389f2011-12-20 17:12:481278 extension_prefs_->IsAppNotificationDisabled(extension.id()),
1279 GetAppLaunchOrdinal(extension.id()),
1280 GetPageOrdinal(extension.id()));
[email protected]3bdba0d2011-08-23 07:17:301281
1282 SyncChangeList sync_change_list(1, extension_sync_data.GetSyncChange(
1283 sync_bundle->HasExtensionId(extension.id()) ?
1284 SyncChange::ACTION_UPDATE : SyncChange::ACTION_ADD));
1285 sync_bundle->sync_processor->ProcessSyncChanges(
1286 FROM_HERE, sync_change_list);
1287 sync_bundle->synced_extensions.insert(extension.id());
1288 sync_bundle->pending_sync_data.erase(extension.id());
[email protected]b05fb9ff2011-04-23 00:07:561289 }
[email protected]3bdba0d2011-08-23 07:17:301290}
1291
1292ExtensionService::SyncBundle* ExtensionService::GetSyncBundleForExtension(
1293 const Extension& extension) {
1294 if (app_sync_bundle_.filter(extension))
1295 return &app_sync_bundle_;
1296 else if (extension_sync_bundle_.filter(extension))
1297 return &extension_sync_bundle_;
1298 else
1299 return NULL;
1300}
1301
1302ExtensionService::SyncBundle*
1303 ExtensionService::GetSyncBundleForExtensionSyncData(
1304 const ExtensionSyncData& extension_sync_data) {
1305 switch (extension_sync_data.type()) {
1306 case Extension::SYNC_TYPE_APP:
1307 return &app_sync_bundle_;
1308 case Extension::SYNC_TYPE_EXTENSION:
1309 return &extension_sync_bundle_;
1310 default:
1311 NOTREACHED();
1312 return NULL;
1313 }
1314}
1315
1316#define GET_SYNC_BUNDLE_FOR_MODEL_TYPE_BODY() \
1317 do { \
1318 switch (type) { \
1319 case syncable::APPS: \
1320 return &app_sync_bundle_; \
1321 case syncable::EXTENSIONS: \
1322 return &extension_sync_bundle_; \
1323 default: \
1324 NOTREACHED(); \
1325 return NULL; \
1326 } \
1327 } while (0)
1328
1329const ExtensionService::SyncBundle*
1330 ExtensionService::GetSyncBundleForModelTypeConst(
1331 syncable::ModelType type) const {
1332 GET_SYNC_BUNDLE_FOR_MODEL_TYPE_BODY();
1333}
1334
1335ExtensionService::SyncBundle* ExtensionService::GetSyncBundleForModelType(
1336 syncable::ModelType type) {
1337 GET_SYNC_BUNDLE_FOR_MODEL_TYPE_BODY();
1338}
1339
1340#undef GET_SYNC_BUNDLE_FOR_MODEL_TYPE_BODY
1341
1342SyncError ExtensionService::MergeDataAndStartSyncing(
1343 syncable::ModelType type,
1344 const SyncDataList& initial_sync_data,
1345 SyncChangeProcessor* sync_processor) {
1346 CHECK(sync_processor);
1347
1348 SyncBundle* bundle = NULL;
1349
1350 switch (type) {
1351 case syncable::EXTENSIONS:
1352 bundle = &extension_sync_bundle_;
1353 bundle->filter = IsSyncableExtension;
1354 break;
1355
1356 case syncable::APPS:
1357 bundle = &app_sync_bundle_;
1358 bundle->filter = IsSyncableApp;
1359 break;
1360
1361 default:
1362 LOG(FATAL) << "Got " << type << " ModelType";
1363 }
1364
1365 bundle->sync_processor = sync_processor;
1366
[email protected]b1417ab2011-12-15 17:09:111367 // Process extensions from sync.
[email protected]3bdba0d2011-08-23 07:17:301368 for (SyncDataList::const_iterator i = initial_sync_data.begin();
1369 i != initial_sync_data.end();
1370 ++i) {
1371 ExtensionSyncData extension_sync_data = ExtensionSyncData(*i);
1372 bundle->synced_extensions.insert(extension_sync_data.id());
1373 ProcessExtensionSyncData(extension_sync_data, *bundle);
1374 }
1375
[email protected]b1417ab2011-12-15 17:09:111376 // Process local extensions.
1377 // TODO(yoz): Determine whether pending extensions should be considered too.
1378 // See crbug.com/104399.
[email protected]3bdba0d2011-08-23 07:17:301379 SyncDataList sync_data_list = GetAllSyncData(type);
1380 SyncChangeList sync_change_list;
1381 for (SyncDataList::const_iterator i = sync_data_list.begin();
1382 i != sync_data_list.end();
1383 ++i) {
[email protected]b1417ab2011-12-15 17:09:111384 if (bundle->HasExtensionId(i->GetTag())) {
[email protected]3bdba0d2011-08-23 07:17:301385 sync_change_list.push_back(SyncChange(SyncChange::ACTION_UPDATE, *i));
[email protected]b1417ab2011-12-15 17:09:111386 } else {
1387 bundle->synced_extensions.insert(i->GetTag());
[email protected]3bdba0d2011-08-23 07:17:301388 sync_change_list.push_back(SyncChange(SyncChange::ACTION_ADD, *i));
[email protected]b1417ab2011-12-15 17:09:111389 }
[email protected]3bdba0d2011-08-23 07:17:301390 }
1391 bundle->sync_processor->ProcessSyncChanges(FROM_HERE, sync_change_list);
1392
[email protected]0ffd9e52012-01-26 21:44:341393 extension_prefs_->extension_sorting()->FixNTPOrdinalCollisions();
1394
[email protected]3bdba0d2011-08-23 07:17:301395 return SyncError();
1396}
1397
1398void ExtensionService::StopSyncing(syncable::ModelType type) {
1399 SyncBundle* bundle = GetSyncBundleForModelType(type);
1400 CHECK(bundle);
1401 // This is the simplest way to clear out the bundle.
1402 *bundle = SyncBundle();
1403}
1404
1405SyncDataList ExtensionService::GetAllSyncData(syncable::ModelType type) const {
1406 const SyncBundle* bundle = GetSyncBundleForModelTypeConst(type);
1407 CHECK(bundle);
1408 std::vector<ExtensionSyncData> extension_sync_data = GetSyncDataList(*bundle);
1409 SyncDataList result(extension_sync_data.size());
1410 for (int i = 0; i < static_cast<int>(extension_sync_data.size()); ++i) {
1411 result[i] = extension_sync_data[i].GetSyncData();
1412 }
1413 return result;
1414}
1415
1416SyncError ExtensionService::ProcessSyncChanges(
1417 const tracked_objects::Location& from_here,
1418 const SyncChangeList& change_list) {
1419 for (SyncChangeList::const_iterator i = change_list.begin();
1420 i != change_list.end();
1421 ++i) {
1422 ExtensionSyncData extension_sync_data = ExtensionSyncData(*i);
1423 SyncBundle* bundle = GetSyncBundleForExtensionSyncData(extension_sync_data);
1424 CHECK(bundle);
1425
1426 if (extension_sync_data.uninstalled())
1427 bundle->synced_extensions.erase(extension_sync_data.id());
1428 else
1429 bundle->synced_extensions.insert(extension_sync_data.id());
1430 ProcessExtensionSyncData(extension_sync_data, *bundle);
1431 }
1432
[email protected]0ffd9e52012-01-26 21:44:341433 extension_prefs()->extension_sorting()->FixNTPOrdinalCollisions();
1434
[email protected]3bdba0d2011-08-23 07:17:301435 return SyncError();
[email protected]b05fb9ff2011-04-23 00:07:561436}
1437
1438void ExtensionService::GetSyncDataListHelper(
[email protected]84df8332011-12-06 18:22:461439 const ExtensionSet& extensions,
[email protected]3bdba0d2011-08-23 07:17:301440 const SyncBundle& bundle,
[email protected]b05fb9ff2011-04-23 00:07:561441 std::vector<ExtensionSyncData>* sync_data_list) const {
[email protected]84df8332011-12-06 18:22:461442 for (ExtensionSet::const_iterator it = extensions.begin();
[email protected]b05fb9ff2011-04-23 00:07:561443 it != extensions.end(); ++it) {
1444 const Extension& extension = **it;
[email protected]3bdba0d2011-08-23 07:17:301445 if (bundle.filter(extension) &&
1446 // If we have pending extension data for this extension, then this
1447 // version is out of date. We'll sync back the version we got from
1448 // sync.
1449 !bundle.HasPendingExtensionId(extension.id())) {
[email protected]aa7599d2011-10-28 07:24:321450 sync_data_list->push_back(ExtensionSyncData(
1451 extension,
1452 IsExtensionEnabled(extension.id()),
1453 IsIncognitoEnabled(extension.id()),
[email protected]b2689a902011-12-01 00:41:091454 extension_prefs_->GetAppNotificationClientId(extension.id()),
[email protected]168389f2011-12-20 17:12:481455 extension_prefs_->IsAppNotificationDisabled(extension.id()),
1456 GetAppLaunchOrdinal(extension.id()),
1457 GetPageOrdinal(extension.id())));
[email protected]b05fb9ff2011-04-23 00:07:561458 }
1459 }
1460}
1461
1462std::vector<ExtensionSyncData> ExtensionService::GetSyncDataList(
[email protected]3bdba0d2011-08-23 07:17:301463 const SyncBundle& bundle) const {
1464 std::vector<ExtensionSyncData> extension_sync_list;
1465 GetSyncDataListHelper(extensions_, bundle, &extension_sync_list);
1466 GetSyncDataListHelper(disabled_extensions_, bundle, &extension_sync_list);
1467 GetSyncDataListHelper(terminated_extensions_, bundle, &extension_sync_list);
1468
1469 for (std::map<std::string, ExtensionSyncData>::const_iterator i =
1470 bundle.pending_sync_data.begin();
1471 i != bundle.pending_sync_data.end();
1472 ++i) {
1473 extension_sync_list.push_back(i->second);
1474 }
1475
1476 return extension_sync_list;
[email protected]b05fb9ff2011-04-23 00:07:561477}
1478
[email protected]3bdba0d2011-08-23 07:17:301479void ExtensionService::ProcessExtensionSyncData(
[email protected]90310d92011-04-17 07:35:041480 const ExtensionSyncData& extension_sync_data,
[email protected]3bdba0d2011-08-23 07:17:301481 SyncBundle& bundle) {
1482 const std::string& id = extension_sync_data.id();
[email protected]96e989b2011-08-30 19:35:061483 const Extension* extension = GetInstalledExtension(id);
1484
1485 // TODO(bolms): we should really handle this better. The particularly bad
1486 // case is where an app becomes an extension or vice versa, and we end up with
1487 // a zombie extension that won't go away.
1488 if (extension && !bundle.filter(*extension))
1489 return;
[email protected]90310d92011-04-17 07:35:041490
1491 // Handle uninstalls first.
[email protected]3bdba0d2011-08-23 07:17:301492 if (extension_sync_data.uninstalled()) {
[email protected]90310d92011-04-17 07:35:041493 std::string error;
1494 if (!UninstallExtensionHelper(this, id)) {
1495 LOG(WARNING) << "Could not uninstall extension " << id
1496 << " for sync";
1497 }
1498 return;
1499 }
1500
[email protected]06f92562011-04-29 19:27:311501 // Set user settings.
[email protected]3bdba0d2011-08-23 07:17:301502 if (extension_sync_data.enabled()) {
[email protected]06f92562011-04-29 19:27:311503 EnableExtension(id);
1504 } else {
1505 DisableExtension(id);
1506 }
[email protected]81b14cc2011-04-29 00:39:371507
[email protected]406b5a92011-11-08 11:58:261508 // We need to cache some version information here because setting the
1509 // incognito flag invalidates the |extension| pointer (it reloads the
1510 // extension).
[email protected]9060d8b02012-01-13 02:14:301511 bool extension_installed = (extension != NULL);
[email protected]406b5a92011-11-08 11:58:261512 int result = extension ?
1513 extension->version()->CompareTo(extension_sync_data.version()) : 0;
1514 SetIsIncognitoEnabled(id, extension_sync_data.incognito_enabled());
1515 extension = NULL; // No longer safe to use.
1516
[email protected]0ffd9e52012-01-26 21:44:341517 if (extension_sync_data.app_launch_ordinal().IsValid() &&
1518 extension_sync_data.page_ordinal().IsValid()) {
1519 SetAppLaunchOrdinal(id, extension_sync_data.app_launch_ordinal());
1520 SetPageOrdinal(id, extension_sync_data.page_ordinal());
1521 }
1522
[email protected]406b5a92011-11-08 11:58:261523 if (extension_installed) {
[email protected]06f92562011-04-29 19:27:311524 // If the extension is already installed, check if it's outdated.
[email protected]90310d92011-04-17 07:35:041525 if (result < 0) {
1526 // Extension is outdated.
[email protected]3bdba0d2011-08-23 07:17:301527 bundle.pending_sync_data[extension_sync_data.id()] = extension_sync_data;
[email protected]90310d92011-04-17 07:35:041528 CheckForUpdatesSoon();
[email protected]90310d92011-04-17 07:35:041529 }
[email protected]b10a4abe2011-12-10 02:31:231530 if (extension_sync_data.type() == Extension::SYNC_TYPE_APP &&
1531 extension_sync_data.notifications_disabled() !=
[email protected]b020dc12011-12-06 04:35:421532 extension_prefs_->IsAppNotificationDisabled(id)) {
1533 extension_prefs_->SetAppNotificationDisabled(
1534 id, extension_sync_data.notifications_disabled());
1535 }
[email protected]06f92562011-04-29 19:27:311536 } else {
[email protected]06f92562011-04-29 19:27:311537 // TODO(akalin): Replace silent update with a list of enabled
1538 // permissions.
[email protected]6cc7dbae2011-04-29 21:18:331539 const bool kInstallSilently = true;
[email protected]06f92562011-04-29 19:27:311540 if (!pending_extension_manager()->AddFromSync(
1541 id,
[email protected]3bdba0d2011-08-23 07:17:301542 extension_sync_data.update_url(),
1543 bundle.filter,
[email protected]6cc7dbae2011-04-29 21:18:331544 kInstallSilently)) {
[email protected]06f92562011-04-29 19:27:311545 LOG(WARNING) << "Could not add pending extension for " << id;
[email protected]3bdba0d2011-08-23 07:17:301546 // This means that the extension is already pending installation, with a
1547 // non-INTERNAL location. Add to pending_sync_data, even though it will
1548 // never be removed (we'll never install a syncable version of the
1549 // extension), so that GetAllSyncData() continues to send it.
[email protected]06f92562011-04-29 19:27:311550 }
[email protected]3bdba0d2011-08-23 07:17:301551 // Track pending extensions so that we can return them in GetAllSyncData().
1552 bundle.pending_sync_data[extension_sync_data.id()] = extension_sync_data;
[email protected]06f92562011-04-29 19:27:311553 CheckForUpdatesSoon();
[email protected]90310d92011-04-17 07:35:041554 }
[email protected]90310d92011-04-17 07:35:041555}
1556
[email protected]c3cfb012011-04-06 22:07:351557bool ExtensionService::IsIncognitoEnabled(
1558 const std::string& extension_id) const {
1559 // If this is an existing component extension we always allow it to
1560 // work in incognito mode.
[email protected]81b14cc2011-04-29 00:39:371561 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]c3cfb012011-04-06 22:07:351562 if (extension && extension->location() == Extension::COMPONENT)
[email protected]cb0ce1e022010-03-10 19:54:411563 return true;
1564
1565 // Check the prefs.
[email protected]c3cfb012011-04-06 22:07:351566 return extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]db7331a2010-02-25 22:10:501567}
[email protected]55a35692010-02-11 23:25:211568
[email protected]c3cfb012011-04-06 22:07:351569void ExtensionService::SetIsIncognitoEnabled(
1570 const std::string& extension_id, bool enabled) {
[email protected]81b14cc2011-04-29 00:39:371571 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]c3cfb012011-04-06 22:07:351572 if (extension && extension->location() == Extension::COMPONENT) {
[email protected]b873cd92012-02-09 21:51:481573 // This shouldn't be called for component extensions unless they are
1574 // syncable.
1575 DCHECK(extension->IsSyncable());
1576
1577 // If we are here, make sure the we aren't trying to change the value.
1578 DCHECK_EQ(enabled, IsIncognitoEnabled(extension_id));
1579
[email protected]c3cfb012011-04-06 22:07:351580 return;
1581 }
1582
[email protected]8c6c8c22011-03-09 12:52:341583 // Broadcast unloaded and loaded events to update browser state. Only bother
1584 // if the value changed and the extension is actually enabled, since there is
1585 // no UI otherwise.
[email protected]c3cfb012011-04-06 22:07:351586 bool old_enabled = extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]8c6c8c22011-03-09 12:52:341587 if (enabled == old_enabled)
1588 return;
1589
[email protected]c3cfb012011-04-06 22:07:351590 extension_prefs_->SetIsIncognitoEnabled(extension_id, enabled);
[email protected]81b14cc2011-04-29 00:39:371591
[email protected]ffbf3f12011-12-09 22:10:331592 bool extension_is_enabled = extensions_.Contains(extension_id);
[email protected]bd9892eb2011-11-11 19:41:341593
1594 // When we reload the extension the ID may be invalidated if we've passed it
1595 // by const ref everywhere. Make a copy to be safe.
1596 std::string id = extension_id;
[email protected]406b5a92011-11-08 11:58:261597 if (extension_is_enabled)
[email protected]ffbf3f12011-12-09 22:10:331598 ReloadExtension(id);
[email protected]3bdba0d2011-08-23 07:17:301599
[email protected]406b5a92011-11-08 11:58:261600 // Reloading the extension invalidates the |extension| pointer.
[email protected]bd9892eb2011-11-11 19:41:341601 extension = GetInstalledExtension(id);
[email protected]3bdba0d2011-08-23 07:17:301602 if (extension)
1603 SyncExtensionChangeIfNeeded(*extension);
[email protected]55a35692010-02-11 23:25:211604}
1605
[email protected]aa7599d2011-10-28 07:24:321606void ExtensionService::SetAppNotificationSetupDone(
1607 const std::string& extension_id,
[email protected]b2689a902011-12-01 00:41:091608 const std::string& oauth_client_id) {
[email protected]aa7599d2011-10-28 07:24:321609 const Extension* extension = GetInstalledExtension(extension_id);
1610 // This method is called when the user sets up app notifications.
1611 // So it is not expected to be called until the extension is installed.
1612 if (!extension) {
1613 NOTREACHED();
1614 return;
1615 }
[email protected]b2689a902011-12-01 00:41:091616 extension_prefs_->SetAppNotificationClientId(extension_id, oauth_client_id);
[email protected]aa7599d2011-10-28 07:24:321617 SyncExtensionChangeIfNeeded(*extension);
1618}
1619
1620void ExtensionService::SetAppNotificationDisabled(
1621 const std::string& extension_id,
1622 bool value) {
1623 const Extension* extension = GetInstalledExtension(extension_id);
1624 // This method is called when the user enables/disables app notifications.
1625 // So it is not expected to be called until the extension is installed.
1626 if (!extension) {
1627 NOTREACHED();
1628 return;
1629 }
[email protected]97e856b12011-12-14 00:21:131630 if (value)
1631 UMA_HISTOGRAM_COUNTS("Apps.SetAppNotificationsDisabled", 1);
1632 else
1633 UMA_HISTOGRAM_COUNTS("Apps.SetAppNotificationsEnabled", 1);
[email protected]aa7599d2011-10-28 07:24:321634 extension_prefs_->SetAppNotificationDisabled(extension_id, value);
1635 SyncExtensionChangeIfNeeded(*extension);
1636}
1637
[email protected]eaa7dd182010-12-14 11:09:001638bool ExtensionService::CanCrossIncognito(const Extension* extension) {
[email protected]2a8f24e2010-11-03 21:37:051639 // We allow the extension to see events and data from another profile iff it
1640 // uses "spanning" behavior and it has incognito access. "split" mode
1641 // extensions only see events for a matching profile.
[email protected]0c41298f2011-09-13 21:17:221642 CHECK(extension);
[email protected]c3cfb012011-04-06 22:07:351643 return IsIncognitoEnabled(extension->id()) &&
1644 !extension->incognito_split_mode();
[email protected]2a8f24e2010-11-03 21:37:051645}
1646
[email protected]5ef7b002011-05-12 23:16:201647bool ExtensionService::CanLoadInIncognito(const Extension* extension) const {
1648 if (extension->is_hosted_app())
1649 return true;
1650 // Packaged apps and regular extensions need to be enabled specifically for
1651 // incognito (and split mode should be set).
1652 return extension->incognito_split_mode() &&
1653 IsIncognitoEnabled(extension->id());
1654}
1655
[email protected]168389f2011-12-20 17:12:481656StringOrdinal ExtensionService::GetAppLaunchOrdinal(
1657 const std::string& extension_id) const {
[email protected]3569b502012-01-12 20:08:231658 return
1659 extension_prefs_->extension_sorting()->GetAppLaunchOrdinal(extension_id);
[email protected]168389f2011-12-20 17:12:481660}
1661
1662void ExtensionService::SetAppLaunchOrdinal(
1663 const std::string& extension_id,
[email protected]0ffd9e52012-01-26 21:44:341664 const StringOrdinal& app_launch_ordinal) {
1665 // Only apps should set this value, so we check that it is either an app or
1666 // that it is not yet installed (so we can't be sure it is an app). It is
1667 // possible to be setting this value through syncing before the app is
1668 // installed.
[email protected]168389f2011-12-20 17:12:481669 const Extension* ext = GetExtensionById(extension_id, true);
[email protected]0ffd9e52012-01-26 21:44:341670 DCHECK(!ext || ext->is_app());
[email protected]168389f2011-12-20 17:12:481671
[email protected]3569b502012-01-12 20:08:231672 extension_prefs_->extension_sorting()->SetAppLaunchOrdinal(
[email protected]0ffd9e52012-01-26 21:44:341673 extension_id, app_launch_ordinal);
[email protected]168389f2011-12-20 17:12:481674
1675 const Extension* extension = GetInstalledExtension(extension_id);
1676 if (extension)
1677 SyncExtensionChangeIfNeeded(*extension);
1678}
1679
1680StringOrdinal ExtensionService::GetPageOrdinal(
1681 const std::string& extension_id) const {
[email protected]3569b502012-01-12 20:08:231682 return extension_prefs_->extension_sorting()->GetPageOrdinal(extension_id);
[email protected]168389f2011-12-20 17:12:481683}
1684
1685void ExtensionService::SetPageOrdinal(const std::string& extension_id,
1686 const StringOrdinal& page_ordinal) {
[email protected]0ffd9e52012-01-26 21:44:341687 // Only apps should set this value, so we check that it is either an app or
1688 // that it is not yet installed (so we can't be sure it is an app). It is
1689 // possible to be setting this value through syncing before the app is
1690 // installed.
[email protected]168389f2011-12-20 17:12:481691 const Extension* ext = GetExtensionById(extension_id, true);
[email protected]0ffd9e52012-01-26 21:44:341692 DCHECK(!ext || ext->is_app());
[email protected]168389f2011-12-20 17:12:481693
[email protected]3569b502012-01-12 20:08:231694 extension_prefs_->extension_sorting()->SetPageOrdinal(
1695 extension_id, page_ordinal);
[email protected]168389f2011-12-20 17:12:481696
1697 const Extension* extension = GetInstalledExtension(extension_id);
1698 if (extension)
1699 SyncExtensionChangeIfNeeded(*extension);
1700}
1701
1702void ExtensionService::OnExtensionMoved(
1703 const std::string& moved_extension_id,
1704 const std::string& predecessor_extension_id,
1705 const std::string& successor_extension_id) {
[email protected]3569b502012-01-12 20:08:231706 extension_prefs_->extension_sorting()->OnExtensionMoved(
1707 moved_extension_id,
1708 predecessor_extension_id,
1709 successor_extension_id);
[email protected]168389f2011-12-20 17:12:481710
1711 const Extension* extension = GetInstalledExtension(moved_extension_id);
1712 if (extension)
1713 SyncExtensionChangeIfNeeded(*extension);
1714}
1715
[email protected]eaa7dd182010-12-14 11:09:001716bool ExtensionService::AllowFileAccess(const Extension* extension) {
[email protected]05c82182010-06-24 17:49:081717 return (CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]334e04a2010-06-24 23:34:441718 switches::kDisableExtensionsFileAccessCheck) ||
[email protected]05c82182010-06-24 17:49:081719 extension_prefs_->AllowFileAccess(extension->id()));
1720}
1721
[email protected]eaa7dd182010-12-14 11:09:001722void ExtensionService::SetAllowFileAccess(const Extension* extension,
[email protected]3aff9ad2011-04-01 20:26:481723 bool allow) {
1724 // Reload to update browser state. Only bother if the value changed and the
1725 // extension is actually enabled, since there is no UI otherwise.
1726 bool old_allow = AllowFileAccess(extension);
1727 if (allow == old_allow)
1728 return;
1729
[email protected]05c82182010-06-24 17:49:081730 extension_prefs_->SetAllowFileAccess(extension->id(), allow);
[email protected]3aff9ad2011-04-01 20:26:481731
[email protected]84df8332011-12-06 18:22:461732 bool extension_is_enabled = extensions_.Contains(extension->id());
[email protected]3aff9ad2011-04-01 20:26:481733 if (extension_is_enabled)
1734 ReloadExtension(extension->id());
[email protected]05c82182010-06-24 17:49:081735}
1736
[email protected]eaa7dd182010-12-14 11:09:001737bool ExtensionService::GetBrowserActionVisibility(const Extension* extension) {
[email protected]f681c782010-11-19 11:19:391738 return extension_prefs_->GetBrowserActionVisibility(extension);
1739}
1740
[email protected]eaa7dd182010-12-14 11:09:001741void ExtensionService::SetBrowserActionVisibility(const Extension* extension,
[email protected]8e4560b62011-01-14 10:09:141742 bool visible) {
[email protected]f681c782010-11-19 11:19:391743 extension_prefs_->SetBrowserActionVisibility(extension, visible);
1744}
1745
[email protected]8e4560b62011-01-14 10:09:141746// Some extensions will autoupdate themselves externally from Chrome. These
1747// are typically part of some larger client application package. To support
1748// these, the extension will register its location in the the preferences file
1749// (and also, on Windows, in the registry) and this code will periodically
1750// check that location for a .crx file, which it will then install locally if
1751// a new version is available.
1752// Errors are reported through ExtensionErrorReporter. Succcess is not
1753// reported.
[email protected]eaa7dd182010-12-14 11:09:001754void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:121755 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]8e4560b62011-01-14 10:09:141756
1757 // Note that this installation is intentionally silent (since it didn't
1758 // go through the front-end). Extensions that are registered in this
1759 // way are effectively considered 'pre-bundled', and so implicitly
1760 // trusted. In general, if something has HKLM or filesystem access,
1761 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:121762
1763 // If any external extension records give a URL, a provider will set
1764 // this to true. Used by OnExternalProviderReady() to see if we need
1765 // to start an update check to fetch a new external extension.
1766 external_extension_url_added_ = false;
[email protected]8e4560b62011-01-14 10:09:141767
1768 // Ask each external extension provider to give us a call back for each
1769 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
1770 ProviderCollection::const_iterator i;
1771 for (i = external_extension_providers_.begin();
1772 i != external_extension_providers_.end(); ++i) {
1773 ExternalExtensionProviderInterface* provider = i->get();
1774 provider->VisitRegisteredExtension();
1775 }
1776
[email protected]50067e52011-10-20 23:17:071777 // Do any required work that we would have done after completion of all
1778 // providers.
1779 if (external_extension_providers_.empty()) {
1780 OnAllExternalProvidersReady();
1781 }
[email protected]9f1087e2009-06-15 17:29:321782}
1783
[email protected]50067e52011-10-20 23:17:071784void ExtensionService::OnExternalProviderReady(
1785 const ExternalExtensionProviderInterface* provider) {
[email protected]a29a517a2011-01-21 21:11:121786 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]50067e52011-10-20 23:17:071787 CHECK(provider->IsReady());
[email protected]a29a517a2011-01-21 21:11:121788
1789 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:141790 // if all of them are finished. So we check them first.
1791 ProviderCollection::const_iterator i;
1792 for (i = external_extension_providers_.begin();
1793 i != external_extension_providers_.end(); ++i) {
1794 ExternalExtensionProviderInterface* provider = i->get();
[email protected]a29a517a2011-01-21 21:11:121795 if (!provider->IsReady())
[email protected]8e4560b62011-01-14 10:09:141796 return;
[email protected]8e4560b62011-01-14 10:09:141797 }
1798
[email protected]50067e52011-10-20 23:17:071799 OnAllExternalProvidersReady();
1800}
1801
1802void ExtensionService::OnAllExternalProvidersReady() {
1803 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1804
1805 // Install any pending extensions.
[email protected]a29a517a2011-01-21 21:11:121806 if (external_extension_url_added_ && updater()) {
1807 external_extension_url_added_ = false;
[email protected]8e4560b62011-01-14 10:09:141808 updater()->CheckNow();
1809 }
1810
1811 // Uninstall all the unclaimed extensions.
1812 scoped_ptr<ExtensionPrefs::ExtensionsInfo> extensions_info(
1813 extension_prefs_->GetInstalledExtensionsInfo());
1814 for (size_t i = 0; i < extensions_info->size(); ++i) {
1815 ExtensionInfo* info = extensions_info->at(i).get();
1816 if (Extension::IsExternalLocation(info->extension_location))
1817 CheckExternalUninstall(info->extension_id);
1818 }
[email protected]e5af875f2011-10-10 21:09:141819 IdentifyAlertableExtensions();
[email protected]e5af875f2011-10-10 21:09:141820}
1821
1822void ExtensionService::IdentifyAlertableExtensions() {
1823 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1824
[email protected]e5af875f2011-10-10 21:09:141825 // Build up the lists of extensions that require acknowledgment.
1826 // If this is the first time, grandfather extensions that would have
1827 // caused notification.
[email protected]a9aa5932012-01-25 08:27:401828 extension_global_error_.reset(new ExtensionGlobalError(this));
[email protected]e5af875f2011-10-10 21:09:141829 bool needs_alert = false;
[email protected]84df8332011-12-06 18:22:461830 for (ExtensionSet::const_iterator iter = extensions_.begin();
[email protected]e5af875f2011-10-10 21:09:141831 iter != extensions_.end(); ++iter) {
1832 const Extension* e = *iter;
[email protected]e5af875f2011-10-10 21:09:141833 if (Extension::IsExternalLocation(e->location())) {
1834 if (!extension_prefs_->IsExternalExtensionAcknowledged(e->id())) {
[email protected]a9aa5932012-01-25 08:27:401835 extension_global_error_->AddExternalExtension(e->id());
[email protected]e5af875f2011-10-10 21:09:141836 needs_alert = true;
1837 }
1838 }
1839 if (extension_prefs_->IsExtensionBlacklisted(e->id())) {
1840 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(e->id())) {
[email protected]a9aa5932012-01-25 08:27:401841 extension_global_error_->AddBlacklistedExtension(e->id());
[email protected]e5af875f2011-10-10 21:09:141842 needs_alert = true;
1843 }
1844 }
1845 if (extension_prefs_->IsExtensionOrphaned(e->id())) {
1846 if (!extension_prefs_->IsOrphanedExtensionAcknowledged(e->id())) {
[email protected]a9aa5932012-01-25 08:27:401847 extension_global_error_->AddOrphanedExtension(e->id());
[email protected]e5af875f2011-10-10 21:09:141848 needs_alert = true;
1849 }
1850 }
1851 }
1852
[email protected]a9aa5932012-01-25 08:27:401853 bool did_show_alert = false;
[email protected]e5af875f2011-10-10 21:09:141854 if (needs_alert) {
1855 if (extension_prefs_->SetAlertSystemFirstRun()) {
[email protected]a9aa5932012-01-25 08:27:401856 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1857 Browser* browser = BrowserList::GetLastActiveWithProfile(profile_);
1858 if (browser) {
1859 extension_global_error_->ShowBubbleView(browser);
1860 did_show_alert = true;
1861 }
[email protected]e5af875f2011-10-10 21:09:141862 } else {
1863 // First run. Just acknowledge all the extensions, silently, by
1864 // shortcutting the display of the UI and going straight to the
1865 // callback for pressing the Accept button.
[email protected]a9aa5932012-01-25 08:27:401866 HandleExtensionAlertAccept();
[email protected]e5af875f2011-10-10 21:09:141867 }
1868 }
[email protected]a9aa5932012-01-25 08:27:401869
1870 if (!did_show_alert)
1871 extension_global_error_.reset();
[email protected]e5af875f2011-10-10 21:09:141872}
1873
[email protected]a9aa5932012-01-25 08:27:401874void ExtensionService::HandleExtensionAlertClosed() {
1875 extension_global_error_.reset();
[email protected]62433d32011-10-12 22:33:121876}
1877
[email protected]a9aa5932012-01-25 08:27:401878void ExtensionService::HandleExtensionAlertAccept() {
[email protected]e5af875f2011-10-10 21:09:141879 const ExtensionIdSet *extension_ids =
[email protected]a9aa5932012-01-25 08:27:401880 extension_global_error_->get_external_extension_ids();
[email protected]e5af875f2011-10-10 21:09:141881 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1882 iter != extension_ids->end(); ++iter) {
[email protected]47fc70c2011-12-06 07:29:511883 AcknowledgeExternalExtension(*iter);
[email protected]e5af875f2011-10-10 21:09:141884 }
[email protected]a9aa5932012-01-25 08:27:401885 extension_ids = extension_global_error_->get_blacklisted_extension_ids();
[email protected]e5af875f2011-10-10 21:09:141886 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1887 iter != extension_ids->end(); ++iter) {
1888 extension_prefs_->AcknowledgeBlacklistedExtension(*iter);
1889 }
[email protected]a9aa5932012-01-25 08:27:401890 extension_ids = extension_global_error_->get_orphaned_extension_ids();
[email protected]e5af875f2011-10-10 21:09:141891 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1892 iter != extension_ids->end(); ++iter) {
1893 extension_prefs_->AcknowledgeOrphanedExtension(*iter);
1894 }
1895}
1896
[email protected]47fc70c2011-12-06 07:29:511897void ExtensionService::AcknowledgeExternalExtension(const std::string& id) {
1898 extension_prefs_->AcknowledgeExternalExtension(id);
1899}
1900
[email protected]a9aa5932012-01-25 08:27:401901void ExtensionService::HandleExtensionAlertDetails(Browser* browser) {
1902 DCHECK(browser);
1903 browser->ShowExtensionsTab();
[email protected]6c751e72010-11-23 10:11:101904}
1905
[email protected]a9f39a312010-12-23 22:14:271906void ExtensionService::UnloadExtension(
1907 const std::string& extension_id,
[email protected]814a7bf0f2011-08-13 05:30:591908 extension_misc::UnloadedExtensionReason reason) {
[email protected]27e469a2010-01-11 20:35:091909 // Make sure the extension gets deleted after we return from this function.
[email protected]9adb9692010-10-29 23:14:021910 scoped_refptr<const Extension> extension(
[email protected]8001df22011-04-28 19:59:471911 GetExtensionByIdInternal(extension_id, true, true, false));
[email protected]631cf822009-05-15 07:01:251912
[email protected]fa0c96732010-11-17 00:14:231913 // This method can be called via PostTask, so the extension may have been
1914 // unloaded by the time this runs.
[email protected]dd163fb02011-05-04 22:22:171915 if (!extension) {
1916 // In case the extension may have crashed/uninstalled. Allow the profile to
1917 // clean up its RequestContexts.
1918 profile_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]fa0c96732010-11-17 00:14:231919 return;
[email protected]dd163fb02011-05-04 22:22:171920 }
[email protected]0c6da502009-08-14 22:32:391921
[email protected]1eb175082010-02-10 09:26:161922 // Keep information about the extension so that we can reload it later
1923 // even if it's not permanently installed.
1924 unloaded_extension_paths_[extension->id()] = extension->path();
1925
[email protected]f17dbd42010-08-16 23:21:101926 // Clean up if the extension is meant to be enabled after a reload.
1927 disabled_extension_paths_.erase(extension->id());
1928
[email protected]d7e9a862010-11-03 21:57:491929 // Clean up runtime data.
1930 extension_runtime_data_.erase(extension_id);
1931
[email protected]84df8332011-12-06 18:22:461932if (disabled_extensions_.Contains(extension->id())) {
[email protected]a9f39a312010-12-23 22:14:271933 UnloadedExtensionInfo details(extension, reason);
1934 details.already_disabled = true;
[email protected]84df8332011-12-06 18:22:461935 disabled_extensions_.Remove(extension->id());
[email protected]ad50def52011-10-19 23:17:071936 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271937 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]6c2381d2011-10-19 02:52:531938 content::Source<Profile>(profile_),
1939 content::Details<UnloadedExtensionInfo>(&details));
[email protected]dd163fb02011-05-04 22:22:171940 // Make sure the profile cleans up its RequestContexts when an already
1941 // disabled extension is unloaded (since they are also tracking the disabled
1942 // extensions).
1943 profile_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]0c6da502009-08-14 22:32:391944 return;
1945 }
1946
[email protected]84df8332011-12-06 18:22:461947// Remove the extension from our list.
1948 extensions_.Remove(extension->id());
[email protected]631cf822009-05-15 07:01:251949
[email protected]a9f39a312010-12-23 22:14:271950 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]631cf822009-05-15 07:01:251951}
1952
[email protected]eaa7dd182010-12-14 11:09:001953void ExtensionService::UnloadAllExtensions() {
[email protected]c8d407e2011-04-28 21:27:171954 profile_->GetExtensionSpecialStoragePolicy()->
1955 RevokeRightsForAllExtensions();
1956
[email protected]84df8332011-12-06 18:22:461957 extensions_.Clear();
1958 disabled_extensions_.Clear();
1959 terminated_extensions_.Clear();
[email protected]d7e9a862010-11-03 21:57:491960 extension_runtime_data_.clear();
[email protected]cd500f72010-06-25 23:44:321961
[email protected]c6e4a3412009-06-24 15:45:291962 // TODO(erikkay) should there be a notification for this? We can't use
1963 // EXTENSION_UNLOADED since that implies that the extension has been disabled
1964 // or uninstalled, and UnloadAll is just part of shutdown.
[email protected]9f1087e2009-06-15 17:29:321965}
1966
[email protected]eaa7dd182010-12-14 11:09:001967void ExtensionService::ReloadExtensions() {
[email protected]9f1087e2009-06-15 17:29:321968 UnloadAllExtensions();
[email protected]d8c8f25f2011-11-02 18:18:011969 component_loader_->LoadAll();
1970 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]9f1087e2009-06-15 17:29:321971}
1972
[email protected]eaa7dd182010-12-14 11:09:001973void ExtensionService::GarbageCollectExtensions() {
[email protected]39d9f62c2010-12-03 10:48:501974 if (extension_prefs_->pref_service()->ReadOnly())
[email protected]ba399672010-04-06 15:42:391975 return;
1976
[email protected]ca3dbf52010-05-19 22:27:061977 scoped_ptr<ExtensionPrefs::ExtensionsInfo> info(
1978 extension_prefs_->GetInstalledExtensionsInfo());
1979
1980 std::map<std::string, FilePath> extension_paths;
1981 for (size_t i = 0; i < info->size(); ++i)
1982 extension_paths[info->at(i)->extension_id] = info->at(i)->extension_path;
1983
[email protected]14908b72011-04-20 06:54:361984 if (!BrowserThread::PostTask(
1985 BrowserThread::FILE, FROM_HERE,
[email protected]53612e82011-10-18 18:00:361986 base::Bind(
[email protected]14908b72011-04-20 06:54:361987 &extension_file_util::GarbageCollectExtensions,
1988 install_directory_,
1989 extension_paths)))
1990 NOTREACHED();
[email protected]f6ccd652010-11-17 00:48:341991
1992 // Also garbage-collect themes. We check |profile_| to be
1993 // defensive; in the future, we may call GarbageCollectExtensions()
1994 // from somewhere other than Init() (e.g., in a timer).
1995 if (profile_) {
[email protected]18280372011-03-22 18:05:221996 ThemeServiceFactory::GetForProfile(profile_)->RemoveUnusedThemes();
[email protected]f6ccd652010-11-17 00:48:341997 }
[email protected]3cf4f0992009-02-03 23:00:301998}
1999
[email protected]eaa7dd182010-12-14 11:09:002000void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]93fd78f42009-07-10 16:43:172001 if (updater_.get()) {
2002 updater_->Start();
2003 }
[email protected]fa6a9102010-11-22 15:38:502004
2005 ready_ = true;
[email protected]ad50def52011-10-19 23:17:072006 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272007 chrome::NOTIFICATION_EXTENSIONS_READY,
[email protected]6c2381d2011-10-19 02:52:532008 content::Source<Profile>(profile_),
[email protected]ad50def52011-10-19 23:17:072009 content::NotificationService::NoDetails());
[email protected]e72e8eb82009-06-18 17:21:512010}
2011
[email protected]49cd1572011-02-08 21:38:452012void ExtensionService::AddExtension(const Extension* extension) {
[email protected]ae09ca62009-08-21 19:46:462013 // Ensure extension is deleted unless we transfer ownership.
[email protected]9adb9692010-10-29 23:14:022014 scoped_refptr<const Extension> scoped_extension(extension);
[email protected]9f1087e2009-06-15 17:29:322015
[email protected]c8d407e2011-04-28 21:27:172016 // TODO(jstritar): We may be able to get rid of this branch by overriding the
2017 // default extension state to DISABLED when the --disable-extensions flag
2018 // is set (http://crbug.com/29067).
2019 if (!extensions_enabled() &&
2020 !extension->is_theme() &&
2021 extension->location() != Extension::COMPONENT &&
2022 !Extension::IsExternalLocation(extension->location()))
2023 return;
2024
2025 SetBeingUpgraded(extension, false);
2026
[email protected]1eb175082010-02-10 09:26:162027 // The extension is now loaded, remove its data from unloaded extension map.
2028 unloaded_extension_paths_.erase(extension->id());
2029
[email protected]bb7f40952011-01-13 00:21:202030 // If a terminated extension is loaded, remove it from the terminated list.
2031 UntrackTerminatedExtension(extension->id());
2032
[email protected]f17dbd42010-08-16 23:21:102033 // If the extension was disabled for a reload, then enable it.
2034 if (disabled_extension_paths_.erase(extension->id()) > 0)
2035 EnableExtension(extension->id());
2036
[email protected]d728e002010-12-08 04:46:232037 // Check if the extension's privileges have changed and disable the
2038 // extension if necessary.
[email protected]902fd7b2011-07-27 18:42:312039 InitializePermissions(extension);
[email protected]0c6da502009-08-14 22:32:392040
[email protected]ad83ca242011-07-29 01:32:252041 bool disabled = extension_prefs_->IsExtensionDisabled(extension->id());
[email protected]36429da2011-07-11 20:25:182042 if (disabled) {
[email protected]84df8332011-12-06 18:22:462043 disabled_extensions_.Insert(scoped_extension);
2044 // TODO(aa): This seems dodgy. AddExtension() could get called with a
2045 // disabled extension for other reasons other than that an update was
2046 // disabled, e.g. as in ExtensionManagementTest.InstallRequiresConfirm.
[email protected]ad50def52011-10-19 23:17:072047 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272048 chrome::NOTIFICATION_EXTENSION_UPDATE_DISABLED,
[email protected]6c2381d2011-10-19 02:52:532049 content::Source<Profile>(profile_),
2050 content::Details<const Extension>(extension));
[email protected]3bdba0d2011-08-23 07:17:302051 SyncExtensionChangeIfNeeded(*extension);
[email protected]c8d407e2011-04-28 21:27:172052 return;
[email protected]e72e8eb82009-06-18 17:21:512053 }
[email protected]aab98a52009-12-02 03:22:352054
[email protected]b873cd92012-02-09 21:51:482055 // All apps that are displayed in the launcher are ordered by their ordinals
2056 // so we must ensure they have valid ordinals.
2057 if (extension->ShouldDisplayInLauncher())
2058 extension_prefs_->extension_sorting()->EnsureValidOrdinals(extension->id());
2059
[email protected]84df8332011-12-06 18:22:462060 extensions_.Insert(scoped_extension);
[email protected]3bdba0d2011-08-23 07:17:302061 SyncExtensionChangeIfNeeded(*extension);
[email protected]c8d407e2011-04-28 21:27:172062 NotifyExtensionLoaded(extension);
[email protected]aab98a52009-12-02 03:22:352063}
2064
[email protected]902fd7b2011-07-27 18:42:312065void ExtensionService::InitializePermissions(const Extension* extension) {
2066 // If the extension has used the optional permissions API, it will have a
2067 // custom set of active permissions defined in the extension prefs. Here,
2068 // we update the extension's active permissions based on the prefs.
2069 scoped_refptr<ExtensionPermissionSet> active_permissions =
2070 extension_prefs()->GetActivePermissions(extension->id());
2071
2072 if (active_permissions.get()) {
2073 // We restrict the active permissions to be within the bounds defined in the
2074 // extension's manifest.
2075 // a) active permissions must be a subset of optional + default permissions
2076 // b) active permissions must contains all default permissions
2077 scoped_refptr<ExtensionPermissionSet> total_permissions =
2078 ExtensionPermissionSet::CreateUnion(
2079 extension->required_permission_set(),
2080 extension->optional_permission_set());
2081
2082 // Make sure the active permissions contain no more than optional + default.
2083 scoped_refptr<ExtensionPermissionSet> adjusted_active =
2084 ExtensionPermissionSet::CreateIntersection(
2085 total_permissions.get(), active_permissions.get());
2086
2087 // Make sure the active permissions contain the default permissions.
2088 adjusted_active = ExtensionPermissionSet::CreateUnion(
2089 extension->required_permission_set(), adjusted_active.get());
2090
[email protected]c333e792012-01-06 16:57:392091 extensions::PermissionsUpdater perms_updater(profile());
2092 perms_updater.UpdateActivePermissions(extension, adjusted_active);
[email protected]902fd7b2011-07-27 18:42:312093 }
2094
[email protected]8d888c12010-11-30 00:00:252095 // We keep track of all permissions the user has granted each extension.
2096 // This allows extensions to gracefully support backwards compatibility
2097 // by including unknown permissions in their manifests. When the user
2098 // installs the extension, only the recognized permissions are recorded.
2099 // When the unknown permissions become recognized (e.g., through browser
2100 // upgrade), we can prompt the user to accept these new permissions.
2101 // Extensions can also silently upgrade to less permissions, and then
2102 // silently upgrade to a version that adds these permissions back.
2103 //
2104 // For example, pretend that Chrome 10 includes a permission "omnibox"
2105 // for an API that adds suggestions to the omnibox. An extension can
2106 // maintain backwards compatibility while still having "omnibox" in the
2107 // manifest. If a user installs the extension on Chrome 9, the browser
2108 // will record the permissions it recognized, not including "omnibox."
2109 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
2110 // will disable the extension and prompt the user to approve the increase
2111 // in privileges. The extension could then release a new version that
2112 // removes the "omnibox" permission. When the user upgrades, Chrome will
2113 // still remember that "omnibox" had been granted, so that if the
2114 // extension once again includes "omnibox" in an upgrade, the extension
2115 // can upgrade without requiring this user's approval.
2116 const Extension* old = GetExtensionByIdInternal(extension->id(),
[email protected]8001df22011-04-28 19:59:472117 true, true, false);
[email protected]8d888c12010-11-30 00:00:252118 bool is_extension_upgrade = old != NULL;
2119 bool is_privilege_increase = false;
2120
[email protected]902fd7b2011-07-27 18:42:312121 // We only need to compare the granted permissions to the current permissions
2122 // if the extension is not allowed to silently increase its permissions.
2123 if (!extension->CanSilentlyIncreasePermissions()) {
[email protected]8d888c12010-11-30 00:00:252124 // Add all the recognized permissions if the granted permissions list
2125 // hasn't been initialized yet.
[email protected]902fd7b2011-07-27 18:42:312126 scoped_refptr<ExtensionPermissionSet> granted_permissions =
2127 extension_prefs_->GetGrantedPermissions(extension->id());
[email protected]0d3e4a22011-06-23 19:02:522128 CHECK(granted_permissions.get());
[email protected]8d888c12010-11-30 00:00:252129
2130 // Here, we check if an extension's privileges have increased in a manner
2131 // that requires the user's approval. This could occur because the browser
2132 // upgraded and recognized additional privileges, or an extension upgrades
2133 // to a version that requires additional privileges.
[email protected]0d3e4a22011-06-23 19:02:522134 is_privilege_increase =
[email protected]902fd7b2011-07-27 18:42:312135 granted_permissions->HasLessPrivilegesThan(
2136 extension->GetActivePermissions());
[email protected]8d888c12010-11-30 00:00:252137 }
2138
2139 if (is_extension_upgrade) {
[email protected]66e26872010-12-03 20:07:252140 // Other than for unpacked extensions, CrxInstaller should have guaranteed
2141 // that we aren't downgrading.
2142 if (extension->location() != Extension::LOAD)
2143 CHECK(extension->version()->CompareTo(*(old->version())) >= 0);
[email protected]8d888c12010-11-30 00:00:252144
2145 // Extensions get upgraded if the privileges are allowed to increase or
2146 // the privileges haven't increased.
2147 if (!is_privilege_increase) {
2148 SetBeingUpgraded(old, true);
2149 SetBeingUpgraded(extension, true);
2150 }
2151
2152 // To upgrade an extension in place, unload the old one and
2153 // then load the new one.
[email protected]814a7bf0f2011-08-13 05:30:592154 UnloadExtension(old->id(), extension_misc::UNLOAD_REASON_UPDATE);
[email protected]8d888c12010-11-30 00:00:252155 old = NULL;
2156 }
2157
2158 // Extension has changed permissions significantly. Disable it. A
2159 // notification should be sent by the caller.
2160 if (is_privilege_increase) {
[email protected]fe2dd7742011-04-19 22:52:492161 if (!extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
2162 RecordPermissionMessagesHistogram(
2163 extension, "Extensions.Permissions_AutoDisable");
2164 }
[email protected]b3317ad2011-04-28 23:46:002165 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
[email protected]8d888c12010-11-30 00:00:252166 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
2167 }
2168}
2169
[email protected]eaa7dd182010-12-14 11:09:002170void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:392171 std::set<std::string> extension_ids;
[email protected]84df8332011-12-06 18:22:462172 for (ExtensionSet::const_iterator iter = extensions_.begin();
2173 iter != extensions_.end(); ++iter) {
2174 const Extension* extension = *iter;
2175 if (!extension->is_theme() && extension->location() != Extension::COMPONENT)
2176 extension_ids.insert(extension->id());
[email protected]aab98a52009-12-02 03:22:352177 }
2178
2179 child_process_logging::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:252180}
2181
[email protected]8266d662011-07-12 21:53:262182void ExtensionService::OnExtensionInstalled(
[email protected]36a5c4c2011-12-14 16:34:502183 const Extension* extension,
2184 bool from_webstore,
2185 const StringOrdinal& page_ordinal) {
[email protected]a29a517a2011-01-21 21:11:122186 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:202187
[email protected]4416c5a2010-06-26 01:28:572188 // Ensure extension is deleted unless we transfer ownership.
[email protected]9adb9692010-10-29 23:14:022189 scoped_refptr<const Extension> scoped_extension(extension);
[email protected]c3cfb012011-04-06 22:07:352190 const std::string& id = extension->id();
[email protected]8a87a5332011-08-11 17:54:592191 // Extensions installed by policy can't be disabled. So even if a previous
2192 // installation disabled the extension, make sure it is now enabled.
2193 bool initial_enable =
2194 !extension_prefs_->IsExtensionDisabled(id) ||
2195 !Extension::UserMayDisable(extension->location());
[email protected]b2907fd2011-03-25 16:43:372196 PendingExtensionInfo pending_extension_info;
[email protected]c3cfb012011-04-06 22:07:352197 if (pending_extension_manager()->GetById(id, &pending_extension_info)) {
2198 pending_extension_manager()->Remove(id);
[email protected]8ef78fd2010-08-19 17:14:322199
[email protected]483d1ff2011-03-03 17:12:022200 if (!pending_extension_info.ShouldAllowInstall(*extension)) {
[email protected]4416c5a2010-06-26 01:28:572201 LOG(WARNING)
[email protected]ae6cb3022011-04-04 20:11:542202 << "ShouldAllowInstall() returned false for "
[email protected]c3cfb012011-04-06 22:07:352203 << id << " of type " << extension->GetType()
[email protected]7fa19f82010-12-21 19:40:082204 << " and update URL " << extension->update_url().spec()
2205 << "; not installing";
[email protected]ae6cb3022011-04-04 20:11:542206
[email protected]ad50def52011-10-19 23:17:072207 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272208 chrome::NOTIFICATION_EXTENSION_INSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:532209 content::Source<Profile>(profile_),
2210 content::Details<const Extension>(extension));
[email protected]ae6cb3022011-04-04 20:11:542211
[email protected]4416c5a2010-06-26 01:28:572212 // Delete the extension directory since we're not going to
2213 // load it.
[email protected]14908b72011-04-20 06:54:362214 if (!BrowserThread::PostTask(
2215 BrowserThread::FILE, FROM_HERE,
[email protected]53612e82011-10-18 18:00:362216 base::Bind(&extension_file_util::DeleteFile,
2217 extension->path(), true)))
[email protected]14908b72011-04-20 06:54:362218 NOTREACHED();
[email protected]4416c5a2010-06-26 01:28:572219 return;
2220 }
[email protected]4416c5a2010-06-26 01:28:572221 } else {
[email protected]c3cfb012011-04-06 22:07:352222 // We explicitly want to re-enable an uninstalled external
2223 // extension; if we're here, that means the user is manually
2224 // installing the extension.
[email protected]6cc7dbae2011-04-29 21:18:332225 if (IsExternalExtensionUninstalled(id)) {
2226 initial_enable = true;
2227 }
[email protected]aa142702010-03-26 01:26:332228 }
2229
[email protected]07533022011-06-27 20:42:552230 // Do not record the install histograms for upgrades.
2231 if (!GetExtensionByIdInternal(extension->id(), true, true, false)) {
2232 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
2233 extension->GetType(), 100);
2234 RecordPermissionMessagesHistogram(
2235 extension, "Extensions.Permissions_Install");
2236 }
2237
[email protected]4416c5a2010-06-26 01:28:572238 extension_prefs_->OnExtensionInstalled(
[email protected]8266d662011-07-12 21:53:262239 extension,
2240 initial_enable ? Extension::ENABLED : Extension::DISABLED,
[email protected]cc829cf2011-08-22 21:01:352241 from_webstore,
[email protected]36a5c4c2011-12-14 16:34:502242 page_ordinal);
[email protected]25b34332009-06-05 21:53:192243
[email protected]cdfca9702011-08-08 16:07:012244 // Unpacked extensions default to allowing file access, but if that has been
2245 // overridden, don't reset the value.
[email protected]b6e64fd2011-08-09 19:49:192246 if (Extension::ShouldAlwaysAllowFileAccess(extension->location()) &&
[email protected]cdfca9702011-08-08 16:07:012247 !extension_prefs_->HasAllowFileAccessSetting(id)) {
2248 extension_prefs_->SetAllowFileAccess(id, true);
2249 }
2250
[email protected]b6cf240f2011-10-15 22:09:532251 // If the extension should automatically block network startup (e.g., it uses
2252 // the webRequest API), set the preference. Otherwise clear it, in case the
2253 // extension stopped using a relevant API.
2254 extension_prefs_->SetDelaysNetworkRequests(
2255 extension->id(), extension->ImplicitlyDelaysNetworkStartup());
2256
[email protected]ad50def52011-10-19 23:17:072257 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272258 chrome::NOTIFICATION_EXTENSION_INSTALLED,
[email protected]6c2381d2011-10-19 02:52:532259 content::Source<Profile>(profile_),
2260 content::Details<const Extension>(extension));
[email protected]7577a5c52009-07-30 06:21:582261
[email protected]49cd1572011-02-08 21:38:452262 // Transfer ownership of |extension| to AddExtension.
2263 AddExtension(scoped_extension);
[email protected]4a190632009-05-09 01:07:422264}
2265
[email protected]eaa7dd182010-12-14 11:09:002266const Extension* ExtensionService::GetExtensionByIdInternal(
[email protected]8001df22011-04-28 19:59:472267 const std::string& id, bool include_enabled, bool include_disabled,
2268 bool include_terminated) const {
[email protected]e957fe52009-06-23 16:51:052269 std::string lowercase_id = StringToLowerASCII(id);
[email protected]0c6da502009-08-14 22:32:392270 if (include_enabled) {
[email protected]84df8332011-12-06 18:22:462271 const Extension* extension = extensions_.GetByID(lowercase_id);
2272 if (extension)
2273 return extension;
[email protected]0c6da502009-08-14 22:32:392274 }
2275 if (include_disabled) {
[email protected]84df8332011-12-06 18:22:462276 const Extension* extension = disabled_extensions_.GetByID(lowercase_id);
2277 if (extension)
2278 return extension;
[email protected]ce5c4502009-05-06 16:46:112279 }
[email protected]8001df22011-04-28 19:59:472280 if (include_terminated) {
[email protected]84df8332011-12-06 18:22:462281 const Extension* extension = terminated_extensions_.GetByID(lowercase_id);
2282 if (extension)
2283 return extension;
[email protected]8001df22011-04-28 19:59:472284 }
[email protected]ce5c4502009-05-06 16:46:112285 return NULL;
2286}
2287
[email protected]bb7f40952011-01-13 00:21:202288void ExtensionService::TrackTerminatedExtension(const Extension* extension) {
[email protected]84df8332011-12-06 18:22:462289 if (!terminated_extensions_.Contains(extension->id()))
2290 terminated_extensions_.Insert(make_scoped_refptr(extension));
[email protected]fa2416f2011-05-03 08:41:202291
[email protected]b3f7fe22011-11-11 19:27:562292 UnloadExtension(extension->id(), extension_misc::UNLOAD_REASON_TERMINATE);
[email protected]bb7f40952011-01-13 00:21:202293}
2294
2295void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
[email protected]fa2416f2011-05-03 08:41:202296 std::string lowercase_id = StringToLowerASCII(id);
[email protected]84df8332011-12-06 18:22:462297 terminated_extensions_.Remove(lowercase_id);
[email protected]bb7f40952011-01-13 00:21:202298}
2299
[email protected]0dfe05c2011-02-23 23:03:362300const Extension* ExtensionService::GetTerminatedExtension(
[email protected]8001df22011-04-28 19:59:472301 const std::string& id) const {
2302 return GetExtensionByIdInternal(id, false, false, true);
2303}
2304
2305const Extension* ExtensionService::GetInstalledExtension(
2306 const std::string& id) const {
2307 return GetExtensionByIdInternal(id, true, true, true);
[email protected]0dfe05c2011-02-23 23:03:362308}
2309
[email protected]eaa7dd182010-12-14 11:09:002310const Extension* ExtensionService::GetWebStoreApp() {
[email protected]d3071992010-10-08 15:24:072311 return GetExtensionById(extension_misc::kWebStoreAppId, false);
2312}
2313
[email protected]eaa7dd182010-12-14 11:09:002314bool ExtensionService::ExtensionBindingsAllowed(const GURL& url) {
[email protected]615d88f2011-12-13 01:47:442315 // Allow bindings for all packaged extensions and component hosted apps.
2316 const Extension* extension = extensions_.GetExtensionOrAppByURL(
2317 ExtensionURLInfo(url));
2318 return extension && (!extension->is_hosted_app() ||
2319 extension->location() == Extension::COMPONENT);
[email protected]6d2e60bd2010-06-03 22:37:392320}
2321
[email protected]eaa7dd182010-12-14 11:09:002322const SkBitmap& ExtensionService::GetOmniboxIcon(
[email protected]b671760b2010-07-15 21:13:472323 const std::string& extension_id) {
2324 return omnibox_icon_manager_.GetIcon(extension_id);
2325}
2326
[email protected]eaa7dd182010-12-14 11:09:002327const SkBitmap& ExtensionService::GetOmniboxPopupIcon(
[email protected]29d0d4ac2010-09-08 21:10:312328 const std::string& extension_id) {
2329 return omnibox_popup_icon_manager_.GetIcon(extension_id);
2330}
2331
[email protected]9060d8b02012-01-13 02:14:302332bool ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:322333 const std::string& id,
[email protected]8e4560b62011-01-14 10:09:142334 const Version* version,
[email protected]8ef78fd2010-08-19 17:14:322335 const FilePath& path,
[email protected]1bf73cc32011-10-26 22:38:312336 Extension::Location location,
[email protected]47fc70c2011-12-06 07:29:512337 int creation_flags,
2338 bool mark_acknowledged) {
[email protected]ab22ba42011-01-14 16:36:382339 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2340 CHECK(Extension::IdIsValid(id));
[email protected]79c833b52011-04-05 18:31:012341 if (extension_prefs_->IsExternalExtensionUninstalled(id))
[email protected]9060d8b02012-01-13 02:14:302342 return false;
[email protected]a8af9fdb2010-10-28 21:52:202343
[email protected]8e4560b62011-01-14 10:09:142344 DCHECK(version);
2345
[email protected]7577a5c52009-07-30 06:21:582346 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:492347 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:582348 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:022349 const Extension* existing = GetExtensionById(id, true);
[email protected]7577a5c52009-07-30 06:21:582350 if (existing) {
[email protected]8e4560b62011-01-14 10:09:142351 switch (existing->version()->CompareTo(*version)) {
[email protected]7577a5c52009-07-30 06:21:582352 case -1: // existing version is older, we should upgrade
2353 break;
2354 case 0: // existing version is same, do nothing
[email protected]9060d8b02012-01-13 02:14:302355 return false;
[email protected]7577a5c52009-07-30 06:21:582356 case 1: // existing version is newer, uh-oh
2357 LOG(WARNING) << "Found external version of extension " << id
2358 << "that is older than current version. Current version "
2359 << "is: " << existing->VersionString() << ". New version "
[email protected]16d900042012-01-04 21:24:322360 << "is: " << version->GetString()
2361 << ". Keeping current version.";
[email protected]9060d8b02012-01-13 02:14:302362 return false;
[email protected]7577a5c52009-07-30 06:21:582363 }
2364 }
2365
[email protected]9060d8b02012-01-13 02:14:302366 // If the extension is already pending, don't start an install.
2367 if (!pending_extension_manager()->AddFromExternalFile(id, location))
2368 return false;
[email protected]9c635f22010-12-02 09:36:362369
[email protected]14908b72011-04-20 06:54:362370 // no client (silent install)
[email protected]d8c8f25f2011-11-02 18:18:012371 scoped_refptr<CrxInstaller> installer(CrxInstaller::Create(this, NULL));
[email protected]6dfbbf82010-03-12 23:09:162372 installer->set_install_source(location);
2373 installer->set_expected_id(id);
[email protected]cb0e50312011-05-09 15:03:072374 installer->set_expected_version(*version);
2375 installer->set_install_cause(extension_misc::INSTALL_CAUSE_EXTERNAL_FILE);
[email protected]1bf73cc32011-10-26 22:38:312376 installer->set_creation_flags(creation_flags);
[email protected]6dfbbf82010-03-12 23:09:162377 installer->InstallCrx(path);
[email protected]47fc70c2011-12-06 07:29:512378
2379 // Depending on the source, a new external extension might not need a user
2380 // notification on installation. For such extensions, mark them acknowledged
2381 // now to suppress the notification.
2382 if (mark_acknowledged)
2383 AcknowledgeExternalExtension(id);
[email protected]9060d8b02012-01-13 02:14:302384
2385 return true;
[email protected]7577a5c52009-07-30 06:21:582386}
2387
[email protected]eaa7dd182010-12-14 11:09:002388void ExtensionService::ReportExtensionLoadError(
[email protected]d11c8e92009-10-20 23:26:402389 const FilePath& extension_path,
2390 const std::string &error,
[email protected]d11c8e92009-10-20 23:26:402391 bool be_noisy) {
[email protected]d8c8f25f2011-11-02 18:18:012392 content::NotificationService::current()->Notify(
2393 chrome::NOTIFICATION_EXTENSION_LOAD_ERROR,
2394 content::Source<Profile>(profile_),
2395 content::Details<const std::string>(&error));
[email protected]d11c8e92009-10-20 23:26:402396
[email protected]8a205c02011-02-04 20:41:332397 std::string path_str = UTF16ToUTF8(extension_path.LossyDisplayName());
[email protected]fc670822011-12-17 09:33:492398 string16 message = ASCIIToUTF16(base::StringPrintf(
[email protected]18d4b6c2010-09-21 03:21:042399 "Could not load extension from '%s'. %s",
[email protected]fc670822011-12-17 09:33:492400 path_str.c_str(), error.c_str()));
[email protected]d11c8e92009-10-20 23:26:402401 ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy);
2402}
2403
[email protected]eaa7dd182010-12-14 11:09:002404void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]406027c02010-09-27 08:03:182405 ExtensionHost* host) {
2406 OrphanedDevTools::iterator iter =
[email protected]2d2f6cfc2011-05-06 21:09:332407 orphaned_dev_tools_.find(host->extension_id());
[email protected]406027c02010-09-27 08:03:182408 if (iter == orphaned_dev_tools_.end())
2409 return;
2410
[email protected]0e12d7d2011-12-01 16:21:442411 DevToolsAgentHost* agent = DevToolsAgentHostRegistry::GetDevToolsAgentHost(
2412 host->render_view_host());
2413 content::DevToolsManager::GetInstance()->AttachClientHost(iter->second,
2414 agent);
[email protected]406027c02010-09-27 08:03:182415 orphaned_dev_tools_.erase(iter);
2416}
2417
[email protected]432115822011-07-10 15:52:272418void ExtensionService::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:532419 const content::NotificationSource& source,
2420 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:272421 switch (type) {
2422 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]6c2381d2011-10-19 02:52:532423 if (profile_ !=
2424 content::Source<Profile>(source).ptr()->GetOriginalProfile()) {
[email protected]bc535ee52010-08-31 18:40:322425 break;
[email protected]6c2381d2011-10-19 02:52:532426 }
[email protected]a4ed6282009-12-14 20:51:162427
[email protected]6c2381d2011-10-19 02:52:532428 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr();
[email protected]f128af42010-08-05 18:05:262429
[email protected]fa2416f2011-05-03 08:41:202430 // Mark the extension as terminated and Unload it. We want it to
2431 // be in a consistent state: either fully working or not loaded
2432 // at all, but never half-crashed. We do it in a PostTask so
2433 // that other handlers of this notification will still have
2434 // access to the Extension and ExtensionHost.
[email protected]14908b72011-04-20 06:54:362435 MessageLoop::current()->PostTask(
2436 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362437 base::Bind(
[email protected]fa2416f2011-05-03 08:41:202438 &ExtensionService::TrackTerminatedExtension,
[email protected]d8c8f25f2011-11-02 18:18:012439 AsWeakPtr(),
[email protected]fa2416f2011-05-03 08:41:202440 host->extension()));
[email protected]31f77262009-12-02 20:48:532441 break;
2442 }
[email protected]432115822011-07-10 15:52:272443 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: {
[email protected]f3b1a082011-11-18 00:34:302444 content::RenderProcessHost* process =
2445 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192446 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302447 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192448 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2449 break;
2450
[email protected]77a6970c2011-04-23 16:58:562451 // Valid extension function names, used to setup bindings in renderer.
2452 std::vector<std::string> function_names;
2453 ExtensionFunctionDispatcher::GetAllFunctionNames(&function_names);
2454 process->Send(new ExtensionMsg_SetFunctionNames(function_names));
[email protected]31f77262009-12-02 20:48:532455
[email protected]77a6970c2011-04-23 16:58:562456 // Scripting whitelist. This is modified by tests and must be communicated
2457 // to renderers.
2458 process->Send(new ExtensionMsg_SetScriptingWhitelist(
2459 *Extension::GetScriptingWhitelist()));
2460
2461 // Loaded extensions.
[email protected]9776e82e2011-11-15 02:17:532462 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions;
[email protected]84df8332011-12-06 18:22:462463 for (ExtensionSet::const_iterator iter = extensions_.begin();
2464 iter != extensions_.end(); ++iter) {
[email protected]19647262011-12-16 09:57:492465 // Renderers don't need to know about themes.
2466 if (!(*iter)->is_theme())
2467 loaded_extensions.push_back(ExtensionMsg_Loaded_Params(*iter));
[email protected]77a6970c2011-04-23 16:58:562468 }
[email protected]9776e82e2011-11-15 02:17:532469 process->Send(new ExtensionMsg_Loaded(loaded_extensions));
[email protected]77a6970c2011-04-23 16:58:562470 break;
2471 }
[email protected]432115822011-07-10 15:52:272472 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
[email protected]f3b1a082011-11-18 00:34:302473 content::RenderProcessHost* process =
2474 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192475 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302476 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192477 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2478 break;
2479
[email protected]f3b1a082011-11-18 00:34:302480 installed_app_hosts_.erase(process->GetID());
[email protected]6f371442011-11-09 06:45:462481
[email protected]6bc04fd82011-12-04 02:29:352482 process_map_.RemoveAllFromProcess(process->GetID());
[email protected]6f371442011-11-09 06:45:462483 BrowserThread::PostTask(
2484 BrowserThread::IO, FROM_HERE,
2485 base::Bind(&ExtensionInfoMap::UnregisterAllExtensionsInProcess,
2486 profile_->GetExtensionInfoMap(),
[email protected]f3b1a082011-11-18 00:34:302487 process->GetID()));
[email protected]da5683db2011-04-23 17:12:212488 break;
2489 }
[email protected]432115822011-07-10 15:52:272490 case chrome::NOTIFICATION_PREF_CHANGED: {
[email protected]6c2381d2011-10-19 02:52:532491 std::string* pref_name = content::Details<std::string>(details).ptr();
[email protected]6c751e72010-11-23 10:11:102492 if (*pref_name == prefs::kExtensionInstallAllowList ||
2493 *pref_name == prefs::kExtensionInstallDenyList) {
2494 CheckAdminBlacklist();
[email protected]6c751e72010-11-23 10:11:102495 } else {
2496 NOTREACHED() << "Unexpected preference name.";
2497 }
[email protected]aa96d3a2010-08-21 08:45:252498 break;
2499 }
[email protected]d91268022011-08-26 13:17:372500 case chrome::NOTIFICATION_IMPORT_FINISHED: {
[email protected]cf593af2011-12-30 05:44:392501 InitAfterImport();
[email protected]d91268022011-08-26 13:17:372502 break;
2503 }
[email protected]aa96d3a2010-08-21 08:45:252504
[email protected]4814b512009-11-07 00:12:292505 default:
2506 NOTREACHED() << "Unexpected notification type.";
2507 }
2508}
2509
[email protected]eaa7dd182010-12-14 11:09:002510bool ExtensionService::HasApps() const {
[email protected]ec5b50d2010-10-09 16:35:182511 return !GetAppIds().empty();
2512}
[email protected]377011d2010-07-20 04:18:502513
[email protected]eaa7dd182010-12-14 11:09:002514ExtensionIdSet ExtensionService::GetAppIds() const {
[email protected]ec5b50d2010-10-09 16:35:182515 ExtensionIdSet result;
[email protected]84df8332011-12-06 18:22:462516 for (ExtensionSet::const_iterator it = extensions_.begin();
[email protected]377011d2010-07-20 04:18:502517 it != extensions_.end(); ++it) {
[email protected]ec5b50d2010-10-09 16:35:182518 if ((*it)->is_app() && (*it)->location() != Extension::COMPONENT)
2519 result.insert((*it)->id());
[email protected]377011d2010-07-20 04:18:502520 }
2521
[email protected]ec5b50d2010-10-09 16:35:182522 return result;
[email protected]377011d2010-07-20 04:18:502523}
[email protected]d7e9a862010-11-03 21:57:492524
[email protected]eaa7dd182010-12-14 11:09:002525bool ExtensionService::IsBackgroundPageReady(const Extension* extension) {
[email protected]a03d4448f2012-01-10 23:25:282526 return (!extension->has_background_page() ||
[email protected]d7e9a862010-11-03 21:57:492527 extension_runtime_data_[extension->id()].background_page_ready);
2528}
2529
[email protected]eaa7dd182010-12-14 11:09:002530void ExtensionService::SetBackgroundPageReady(const Extension* extension) {
[email protected]a03d4448f2012-01-10 23:25:282531 DCHECK(extension->has_background_page());
[email protected]d7e9a862010-11-03 21:57:492532 extension_runtime_data_[extension->id()].background_page_ready = true;
[email protected]ad50def52011-10-19 23:17:072533 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272534 chrome::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY,
[email protected]6c2381d2011-10-19 02:52:532535 content::Source<const Extension>(extension),
[email protected]ad50def52011-10-19 23:17:072536 content::NotificationService::NoDetails());
[email protected]d7e9a862010-11-03 21:57:492537}
2538
[email protected]eaa7dd182010-12-14 11:09:002539bool ExtensionService::IsBeingUpgraded(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:492540 return extension_runtime_data_[extension->id()].being_upgraded;
2541}
2542
[email protected]eaa7dd182010-12-14 11:09:002543void ExtensionService::SetBeingUpgraded(const Extension* extension,
[email protected]d7e9a862010-11-03 21:57:492544 bool value) {
2545 extension_runtime_data_[extension->id()].being_upgraded = value;
2546}
[email protected]1bead0712010-11-27 17:41:532547
[email protected]39a5b532011-10-22 01:47:072548bool ExtensionService::HasUsedWebRequest(const Extension* extension) {
2549 return extension_runtime_data_[extension->id()].has_used_webrequest;
2550}
2551
2552void ExtensionService::SetHasUsedWebRequest(const Extension* extension,
2553 bool value) {
2554 extension_runtime_data_[extension->id()].has_used_webrequest = value;
2555}
2556
[email protected]45644f62011-11-23 00:58:232557base::PropertyBag* ExtensionService::GetPropertyBag(
2558 const Extension* extension) {
[email protected]1bead0712010-11-27 17:41:532559 return &extension_runtime_data_[extension->id()].property_bag;
2560}
[email protected]cebc3dc2011-04-18 17:15:002561
2562void ExtensionService::RegisterNaClModule(const GURL& url,
2563 const std::string& mime_type) {
2564 NaClModuleInfo info;
2565 info.url = url;
2566 info.mime_type = mime_type;
2567
2568 DCHECK(FindNaClModule(url) == nacl_module_list_.end());
2569 nacl_module_list_.push_front(info);
2570}
2571
2572void ExtensionService::UnregisterNaClModule(const GURL& url) {
2573 NaClModuleInfoList::iterator iter = FindNaClModule(url);
2574 DCHECK(iter != nacl_module_list_.end());
2575 nacl_module_list_.erase(iter);
2576}
2577
[email protected]ed0ba002011-05-26 16:55:132578void ExtensionService::UpdatePluginListWithNaClModules() {
[email protected]300c0ea2011-07-15 23:04:332579 // An extension has been added which has a nacl_module component, which means
2580 // there is a MIME type that module wants to handle, so we need to add that
2581 // MIME type to plugins which handle NaCl modules in order to allow the
2582 // individual modules to handle these types.
[email protected]1e127022011-11-29 21:11:472583 FilePath path;
2584 if (!PathService::Get(chrome::FILE_NACL_PLUGIN, &path))
2585 return;
2586 const content::PepperPluginInfo* pepper_info =
2587 PluginService::GetInstance()->GetRegisteredPpapiPluginInfo(path);
2588 if (!pepper_info)
2589 return;
[email protected]300c0ea2011-07-15 23:04:332590
[email protected]1e127022011-11-29 21:11:472591 std::vector<webkit::WebPluginMimeType>::const_iterator mime_iter;
2592 // Check each MIME type the plugins handle for the NaCl MIME type.
2593 for (mime_iter = pepper_info->mime_types.begin();
2594 mime_iter != pepper_info->mime_types.end(); ++mime_iter) {
2595 if (mime_iter->mime_type == kNaClPluginMimeType) {
2596 // This plugin handles "application/x-nacl".
[email protected]300c0ea2011-07-15 23:04:332597
[email protected]1e127022011-11-29 21:11:472598 PluginService::GetInstance()->
2599 UnregisterInternalPlugin(pepper_info->path);
[email protected]300c0ea2011-07-15 23:04:332600
[email protected]1e127022011-11-29 21:11:472601 webkit::WebPluginInfo info = pepper_info->ToWebPluginInfo();
[email protected]300c0ea2011-07-15 23:04:332602
[email protected]1e127022011-11-29 21:11:472603 for (ExtensionService::NaClModuleInfoList::const_iterator iter =
2604 nacl_module_list_.begin();
2605 iter != nacl_module_list_.end(); ++iter) {
2606 // Add the MIME type specified in the extension to this NaCl plugin,
2607 // With an extra "nacl" argument to specify the location of the NaCl
2608 // manifest file.
2609 webkit::WebPluginMimeType mime_type_info;
2610 mime_type_info.mime_type = iter->mime_type;
2611 mime_type_info.additional_param_names.push_back(UTF8ToUTF16("nacl"));
2612 mime_type_info.additional_param_values.push_back(
2613 UTF8ToUTF16(iter->url.spec()));
2614 info.mime_types.push_back(mime_type_info);
[email protected]300c0ea2011-07-15 23:04:332615 }
[email protected]1e127022011-11-29 21:11:472616
2617 PluginService::GetInstance()->RefreshPlugins();
[email protected]c6f3dea2012-01-14 02:23:112618 PluginService::GetInstance()->RegisterInternalPlugin(info, true);
[email protected]1e127022011-11-29 21:11:472619 // This plugin has been modified, no need to check the rest of its
2620 // types, but continue checking other plugins.
2621 break;
[email protected]ed0ba002011-05-26 16:55:132622 }
2623 }
[email protected]ed0ba002011-05-26 16:55:132624}
2625
[email protected]cebc3dc2011-04-18 17:15:002626ExtensionService::NaClModuleInfoList::iterator
2627 ExtensionService::FindNaClModule(const GURL& url) {
2628 for (NaClModuleInfoList::iterator iter = nacl_module_list_.begin();
2629 iter != nacl_module_list_.end(); ++iter) {
2630 if (iter->url == url)
2631 return iter;
2632 }
2633 return nacl_module_list_.end();
2634}
[email protected]9de83282011-12-12 11:06:372635
[email protected]e1fae1c2011-12-13 22:17:172636extensions::SocketController* ExtensionService::socket_controller() {
2637 // TODO(miket): Find a better place for SocketController to live. It needs
2638 // to be scoped such that it can be created and destroyed on the IO thread.
2639 //
2640 // To coexist with certain unit tests that don't have an IO thread message
2641 // loop available at ExtensionService shutdown, we lazy-initialize this
2642 // object so that those cases neither create nor destroy a SocketController.
2643 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
2644 if (!socket_controller_) {
2645 socket_controller_ = new extensions::SocketController();
2646 }
2647 return socket_controller_;
2648}