blob: 4d1ce3bf3876143ab5cc4e8ed4e5fe4696aa4063 [file] [log] [blame]
[email protected]1e8c93f2010-02-08 22:58:311// Copyright (c) 2010 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
5#include "chrome/browser/extensions/extensions_service.h"
6
[email protected]654512b2010-09-01 02:09:427#include <algorithm>
8
[email protected]24b538a2010-02-27 01:22:449#include "base/basictypes.h"
[email protected]e2eb43112009-05-29 21:19:5410#include "base/command_line.h"
[email protected]6014d672008-12-05 00:38:2511#include "base/file_util.h"
[email protected]cc2c3432009-11-06 17:24:3612#include "base/histogram.h"
[email protected]cd500f72010-06-25 23:44:3213#include "base/stl_util-inl.h"
[email protected]24b538a2010-02-27 01:22:4414#include "base/string16.h"
[email protected]e83326f2010-07-31 17:29:2515#include "base/string_number_conversions.h"
[email protected]6014d672008-12-05 00:38:2516#include "base/string_util.h"
[email protected]cc2c3432009-11-06 17:24:3617#include "base/time.h"
[email protected]ce7f62e32010-08-10 23:43:5918#include "base/utf_string_conversions.h"
[email protected]cc655912009-01-29 23:19:1919#include "base/values.h"
[email protected]aa142702010-03-26 01:26:3320#include "base/version.h"
[email protected]15730c42009-09-03 00:03:2021#include "chrome/browser/browser_process.h"
[email protected]dbb92e0d2009-08-20 16:18:2122#include "chrome/browser/chrome_thread.h"
[email protected]4814b512009-11-07 00:12:2923#include "chrome/browser/debugger/devtools_manager.h"
[email protected]7577a5c52009-07-30 06:21:5824#include "chrome/browser/extensions/crx_installer.h"
[email protected]5cbe1e22010-01-30 01:18:5625#include "chrome/browser/extensions/extension_accessibility_api.h"
[email protected]840b0db2009-11-20 03:00:3826#include "chrome/browser/extensions/extension_bookmarks_module.h"
[email protected]b68d5ed2009-04-16 02:41:2827#include "chrome/browser/extensions/extension_browser_event_router.h"
[email protected]2c5e1e12010-06-10 13:14:4428#include "chrome/browser/extensions/extension_cookies_api.h"
[email protected]c10da4b02010-03-25 14:38:3229#include "chrome/browser/extensions/extension_data_deleter.h"
[email protected]86c008e82009-08-28 20:26:0530#include "chrome/browser/extensions/extension_dom_ui.h"
[email protected]14a000d2010-04-29 21:44:2431#include "chrome/browser/extensions/extension_error_reporter.h"
[email protected]de768a832009-10-30 05:25:0132#include "chrome/browser/extensions/extension_history_api.h"
[email protected]b1748b1d82009-11-30 20:32:5633#include "chrome/browser/extensions/extension_host.h"
[email protected]7596ce72010-08-30 05:10:4634#include "chrome/browser/extensions/extension_management_api.h"
[email protected]4814b512009-11-07 00:12:2935#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]93fd78f42009-07-10 16:43:1736#include "chrome/browser/extensions/extension_updater.h"
[email protected]a1257b12009-06-12 02:51:3437#include "chrome/browser/extensions/external_extension_provider.h"
38#include "chrome/browser/extensions/external_pref_extension_provider.h"
[email protected]56ad3792010-05-28 17:45:3339#include "chrome/browser/net/chrome_url_request_context.h"
[email protected]37858e52010-08-26 00:22:0240#include "chrome/browser/prefs/pref_service.h"
[email protected]81e63782009-02-27 19:35:0941#include "chrome/browser/profile.h"
[email protected]56ad3792010-05-28 17:45:3342#include "chrome/browser/search_engines/template_url_model.h"
[email protected]2a3e22b12010-08-13 04:55:1743#include "chrome/browser/sync/glue/extension_sync_traits.h"
[email protected]11edd1e2010-07-21 00:14:5044#include "chrome/browser/sync/glue/extension_util.h"
[email protected]aab98a52009-12-02 03:22:3545#include "chrome/common/child_process_logging.h"
[email protected]e2eb43112009-05-29 21:19:5446#include "chrome/common/chrome_switches.h"
[email protected]5b1a0e22009-05-26 19:00:5847#include "chrome/common/extensions/extension.h"
[email protected]d7b36dc2009-10-29 21:47:4048#include "chrome/common/extensions/extension_constants.h"
[email protected]05c82182010-06-24 17:49:0849#include "chrome/common/extensions/extension_error_utils.h"
[email protected]7c927b62010-02-24 09:54:1350#include "chrome/common/extensions/extension_file_util.h"
[email protected]c6d474f82009-12-16 21:11:0651#include "chrome/common/extensions/extension_l10n_util.h"
[email protected]82891262008-12-24 00:21:2652#include "chrome/common/notification_service.h"
[email protected]4814b512009-11-07 00:12:2953#include "chrome/common/notification_type.h"
[email protected]1952c7d2010-03-04 23:48:3454#include "chrome/common/json_value_serializer.h"
[email protected]25b34332009-06-05 21:53:1955#include "chrome/common/pref_names.h"
[email protected]a57209872009-05-04 22:53:1456#include "chrome/common/url_constants.h"
[email protected]c10da4b02010-03-25 14:38:3257#include "googleurl/src/gurl.h"
[email protected]24b538a2010-02-27 01:22:4458#include "webkit/database/database_tracker.h"
59#include "webkit/database/database_util.h"
[email protected]c64631652009-04-29 22:24:3160
[email protected]79db6232009-02-13 20:51:2061#if defined(OS_WIN)
[email protected]a1257b12009-06-12 02:51:3462#include "chrome/browser/extensions/external_registry_extension_provider_win.h"
[email protected]79db6232009-02-13 20:51:2063#endif
[email protected]6014d672008-12-05 00:38:2564
[email protected]5ef47ec2010-01-28 05:58:0565using base::Time;
66
[email protected]c6d474f82009-12-16 21:11:0667namespace errors = extension_manifest_errors;
68
[email protected]b6ab96d2009-08-20 18:58:1969namespace {
70
[email protected]c2c263c2010-08-13 21:59:4871bool ShouldReloadExtensionManifest(const ExtensionInfo& info) {
[email protected]2111b1a2010-03-12 18:12:4472 // Always reload LOAD extension manifests, because they can change on disk
73 // independent of the manifest in our prefs.
74 if (info.extension_location == Extension::LOAD)
75 return true;
76
77 // Otherwise, reload the manifest it needs to be relocalized.
78 return extension_l10n_util::ShouldRelocalizeManifest(info);
79}
80
[email protected]c2c263c2010-08-13 21:59:4881void GetExplicitOriginsInExtent(Extension* extension,
82 std::vector<GURL>* origins) {
83 typedef std::vector<URLPattern> PatternList;
84 std::set<GURL> set;
85 const PatternList& patterns = extension->web_extent().patterns();
86 for (PatternList::const_iterator pattern = patterns.begin();
87 pattern != patterns.end(); ++pattern) {
88 if (pattern->match_subdomains() || pattern->match_all_urls())
89 continue;
[email protected]654512b2010-09-01 02:09:4290 // Wildcard URL schemes won't parse into a valid GURL, so explicit schemes
91 // must be used.
92 PatternList explicit_patterns = pattern->ConvertToExplicitSchemes();
93 for (PatternList::const_iterator explicit_p = explicit_patterns.begin();
94 explicit_p != explicit_patterns.end(); ++explicit_p) {
95 GURL origin = GURL(explicit_p->GetAsString()).GetOrigin();
96 if (origin.is_valid()) {
97 set.insert(origin);
98 } else {
99 NOTREACHED();
100 }
101 }
[email protected]c2c263c2010-08-13 21:59:48102 }
103
104 for (std::set<GURL>::const_iterator unique = set.begin();
105 unique != set.end(); ++unique) {
106 origins->push_back(*unique);
107 }
108}
109
[email protected]c6d474f82009-12-16 21:11:06110} // namespace
[email protected]b6ab96d2009-08-20 18:58:19111
[email protected]8ef78fd2010-08-19 17:14:32112PendingExtensionInfo::PendingExtensionInfo(
113 const GURL& update_url,
114 PendingExtensionInfo::ExpectedCrxType expected_crx_type,
115 bool is_from_sync,
116 bool install_silently,
117 bool enable_on_install,
118 bool enable_incognito_on_install)
[email protected]aa142702010-03-26 01:26:33119 : update_url(update_url),
[email protected]8ef78fd2010-08-19 17:14:32120 expected_crx_type(expected_crx_type),
121 is_from_sync(is_from_sync),
[email protected]4416c5a2010-06-26 01:28:57122 install_silently(install_silently),
123 enable_on_install(enable_on_install),
124 enable_incognito_on_install(enable_incognito_on_install) {}
[email protected]aa142702010-03-26 01:26:33125
126PendingExtensionInfo::PendingExtensionInfo()
127 : update_url(),
[email protected]8ef78fd2010-08-19 17:14:32128 expected_crx_type(PendingExtensionInfo::UNKNOWN),
129 is_from_sync(true),
[email protected]4416c5a2010-06-26 01:28:57130 install_silently(false),
131 enable_on_install(false),
132 enable_incognito_on_install(false) {}
[email protected]aa142702010-03-26 01:26:33133
[email protected]25b34332009-06-05 21:53:19134// ExtensionsService.
[email protected]6014d672008-12-05 00:38:25135
[email protected]cc655912009-01-29 23:19:19136const char* ExtensionsService::kInstallDirectoryName = "Extensions";
137const char* ExtensionsService::kCurrentVersionFileName = "Current Version";
[email protected]494c06e2009-07-25 01:06:42138
[email protected]06f37542010-08-14 18:26:28139namespace {
140
141bool IsGalleryDownloadURL(const GURL& download_url) {
[email protected]b7c2f252009-12-08 00:47:23142 if (StartsWithASCII(download_url.spec(),
[email protected]334e04a2010-06-24 23:34:44143 extension_urls::kMiniGalleryDownloadPrefix, false))
144 return true;
[email protected]b7c2f252009-12-08 00:47:23145
[email protected]06f37542010-08-14 18:26:28146 if (StartsWithASCII(download_url.spec(),
147 extension_urls::kGalleryDownloadPrefix, false))
148 return true;
[email protected]b7c2f252009-12-08 00:47:23149
[email protected]473ff6e2010-05-12 15:31:55150 // Allow command line gallery url to be referrer for the gallery downloads.
151 std::string command_line_gallery_url =
152 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
[email protected]2bce5e12010-05-13 01:18:07153 switches::kAppsGalleryURL);
[email protected]473ff6e2010-05-12 15:31:55154 if (!command_line_gallery_url.empty() &&
155 StartsWithASCII(download_url.spec(),
[email protected]334e04a2010-06-24 23:34:44156 extension_urls::kGalleryDownloadPrefix, false))
[email protected]473ff6e2010-05-12 15:31:55157 return true;
[email protected]334e04a2010-06-24 23:34:44158
159 return false;
160}
161
[email protected]06f37542010-08-14 18:26:28162} // namespace
163
[email protected]334e04a2010-06-24 23:34:44164// static
165bool ExtensionsService::IsDownloadFromGallery(const GURL& download_url,
166 const GURL& referrer_url) {
167 if (!IsGalleryDownloadURL(download_url))
168 return false;
169
170 if (StartsWithASCII(referrer_url.spec(),
171 extension_urls::kMiniGalleryBrowsePrefix, false))
172 return true;
173
174 if (StartsWithASCII(referrer_url.spec(),
175 Extension::ChromeStoreURL(), false))
176 return true;
177
178 // Allow command line gallery url to be referrer for the gallery downloads.
179 std::string command_line_gallery_url =
180 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
181 switches::kAppsGalleryURL);
182 if (!command_line_gallery_url.empty() &&
183 StartsWithASCII(referrer_url.spec(),
184 command_line_gallery_url, false))
185 return true;
[email protected]473ff6e2010-05-12 15:31:55186
[email protected]b7c2f252009-12-08 00:47:23187 return false;
188}
189
[email protected]ac025282009-12-16 19:16:38190bool ExtensionsService::IsDownloadFromMiniGallery(const GURL& download_url) {
191 return StartsWithASCII(download_url.spec(),
192 extension_urls::kMiniGalleryDownloadPrefix,
193 false); // case_sensitive
194}
195
[email protected]81e63782009-02-27 19:35:09196ExtensionsService::ExtensionsService(Profile* profile,
[email protected]36a784c2009-06-23 06:21:08197 const CommandLine* command_line,
[email protected]a9b00ac2009-06-25 21:03:23198 PrefService* prefs,
199 const FilePath& install_directory,
[email protected]93fd78f42009-07-10 16:43:17200 bool autoupdate_enabled)
[email protected]6ef635e42009-07-26 06:16:12201 : profile_(profile),
202 extension_prefs_(new ExtensionPrefs(prefs, install_directory)),
[email protected]a9b00ac2009-06-25 21:03:23203 install_directory_(install_directory),
[email protected]6d60703b2009-08-29 01:29:23204 extensions_enabled_(true),
[email protected]e81dba32009-06-19 20:19:13205 show_extensions_prompts_(true),
[email protected]e0360f2c2009-12-07 22:34:31206 ready_(false),
207 ALLOW_THIS_IN_INITIALIZER_LIST(toolbar_model_(this)) {
[email protected]36a784c2009-06-23 06:21:08208 // Figure out if extension installation should be enabled.
[email protected]6d60703b2009-08-29 01:29:23209 if (command_line->HasSwitch(switches::kDisableExtensions)) {
210 extensions_enabled_ = false;
211 } else if (profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions)) {
212 extensions_enabled_ = false;
[email protected]6b75ec32009-08-14 06:37:18213 }
[email protected]36a784c2009-06-23 06:21:08214
[email protected]4814b512009-11-07 00:12:29215 registrar_.Add(this, NotificationType::EXTENSION_HOST_DID_STOP_LOADING,
216 NotificationService::AllSources());
[email protected]a4ed6282009-12-14 20:51:16217 registrar_.Add(this, NotificationType::EXTENSION_PROCESS_TERMINATED,
[email protected]bc535ee52010-08-31 18:40:32218 NotificationService::AllSources());
[email protected]aa96d3a2010-08-21 08:45:25219 prefs->AddPrefObserver(prefs::kExtensionInstallAllowList, this);
220 prefs->AddPrefObserver(prefs::kExtensionInstallDenyList, this);
[email protected]4814b512009-11-07 00:12:29221
[email protected]93fd78f42009-07-10 16:43:17222 // Set up the ExtensionUpdater
223 if (autoupdate_enabled) {
224 int update_frequency = kDefaultUpdateFrequencySeconds;
225 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25226 base::StringToInt(command_line->GetSwitchValueASCII(
227 switches::kExtensionsUpdateFrequency),
228 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17229 }
[email protected]95d29192009-10-30 01:49:06230 updater_ = new ExtensionUpdater(this, prefs, update_frequency);
[email protected]93fd78f42009-07-10 16:43:17231 }
232
[email protected]95d29192009-10-30 01:49:06233 backend_ = new ExtensionsServiceBackend(install_directory_);
[email protected]b671760b2010-07-15 21:13:47234
[email protected]aa96d3a2010-08-21 08:45:25235 // Use monochrome icons for Omnibox icons.
[email protected]b671760b2010-07-15 21:13:47236 omnibox_icon_manager_.set_monochrome(true);
[email protected]6014d672008-12-05 00:38:25237}
238
239ExtensionsService::~ExtensionsService() {
[email protected]9f1087e2009-06-15 17:29:32240 UnloadAllExtensions();
[email protected]93fd78f42009-07-10 16:43:17241 if (updater_.get()) {
242 updater_->Stop();
243 }
[email protected]6014d672008-12-05 00:38:25244}
245
[email protected]c5ae74ab2010-04-15 18:14:37246void ExtensionsService::InitEventRouters() {
247 ExtensionHistoryEventRouter::GetInstance()->ObserveProfile(profile_);
248 ExtensionAccessibilityEventRouter::GetInstance()->ObserveProfile(profile_);
[email protected]56ee0152010-06-16 01:54:42249 ExtensionBrowserEventRouter::GetInstance()->Init(profile_);
[email protected]c5ae74ab2010-04-15 18:14:37250 ExtensionBookmarkEventRouter::GetSingleton()->Observe(
251 profile_->GetBookmarkModel());
[email protected]2c5e1e12010-06-10 13:14:44252 ExtensionCookiesEventRouter::GetInstance()->Init();
[email protected]7596ce72010-08-30 05:10:46253 ExtensionManagementEventRouter::GetInstance()->Init();
[email protected]c5ae74ab2010-04-15 18:14:37254}
255
[email protected]9f1087e2009-06-15 17:29:32256void ExtensionsService::Init() {
[email protected]c6e4a3412009-06-24 15:45:29257 DCHECK(!ready_);
[email protected]93fd78f42009-07-10 16:43:17258 DCHECK_EQ(extensions_.size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32259
[email protected]95dd38f2009-10-20 20:09:15260 // Hack: we need to ensure the ResourceDispatcherHost is ready before we load
261 // the first extension, because its members listen for loaded notifications.
262 g_browser_process->resource_dispatcher_host();
263
[email protected]9f1087e2009-06-15 17:29:32264 LoadAllExtensions();
[email protected]894bb502009-05-21 22:39:57265
[email protected]9f1087e2009-06-15 17:29:32266 // TODO(erikkay) this should probably be deferred to a future point
267 // rather than running immediately at startup.
[email protected]93fd78f42009-07-10 16:43:17268 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57269
[email protected]9f1087e2009-06-15 17:29:32270 // TODO(erikkay) this should probably be deferred as well.
271 GarbageCollectExtensions();
[email protected]6014d672008-12-05 00:38:25272}
273
[email protected]3cf4f0992009-02-03 23:00:30274void ExtensionsService::InstallExtension(const FilePath& extension_path) {
[email protected]6dfbbf82010-03-12 23:09:16275 scoped_refptr<CrxInstaller> installer(
276 new CrxInstaller(install_directory_,
277 this, // frontend
278 NULL)); // no client (silent install)
279 installer->set_allow_privilege_increase(true);
280 installer->InstallCrx(extension_path);
[email protected]3cf4f0992009-02-03 23:00:30281}
282
[email protected]aa142702010-03-26 01:26:33283namespace {
284 // TODO(akalin): Put this somewhere where both crx_installer.cc and
285 // this file can use it.
286 void DeleteFileHelper(const FilePath& path, bool recursive) {
287 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
288 file_util::Delete(path, recursive);
289 }
290} // namespace
291
[email protected]e957fe52009-06-23 16:51:05292void ExtensionsService::UpdateExtension(const std::string& id,
[email protected]5c8516202010-03-18 21:43:34293 const FilePath& extension_path,
294 const GURL& download_url) {
[email protected]aa142702010-03-26 01:26:33295 PendingExtensionMap::const_iterator it = pending_extensions_.find(id);
[email protected]8ef78fd2010-08-19 17:14:32296 bool is_pending_extension = (it != pending_extensions_.end());
297
298 if (!is_pending_extension &&
[email protected]aa142702010-03-26 01:26:33299 !GetExtensionByIdInternal(id, true, true)) {
300 LOG(WARNING) << "Will not update extension " << id
301 << " because it is not installed or pending";
302 // Delete extension_path since we're not creating a CrxInstaller
303 // that would do it for us.
304 ChromeThread::PostTask(
305 ChromeThread::FILE, FROM_HERE,
306 NewRunnableFunction(&DeleteFileHelper, extension_path, false));
[email protected]4c967932009-07-31 01:15:49307 return;
[email protected]e957fe52009-06-23 16:51:05308 }
309
[email protected]aa142702010-03-26 01:26:33310 // We want a silent install only for non-pending extensions and
311 // pending extensions that have install_silently set.
312 ExtensionInstallUI* client =
[email protected]8ef78fd2010-08-19 17:14:32313 (!is_pending_extension || it->second.install_silently) ?
[email protected]aa142702010-03-26 01:26:33314 NULL : new ExtensionInstallUI(profile_);
315
[email protected]6dfbbf82010-03-12 23:09:16316 scoped_refptr<CrxInstaller> installer(
317 new CrxInstaller(install_directory_,
318 this, // frontend
[email protected]aa142702010-03-26 01:26:33319 client));
[email protected]6dfbbf82010-03-12 23:09:16320 installer->set_expected_id(id);
[email protected]8ef78fd2010-08-19 17:14:32321 if (is_pending_extension && !it->second.is_from_sync)
322 installer->set_install_source(Extension::EXTERNAL_PREF_DOWNLOAD);
[email protected]6dfbbf82010-03-12 23:09:16323 installer->set_delete_source(true);
[email protected]5c8516202010-03-18 21:43:34324 installer->set_original_url(download_url);
[email protected]6dfbbf82010-03-12 23:09:16325 installer->InstallCrx(extension_path);
[email protected]e957fe52009-06-23 16:51:05326}
327
[email protected]8ef78fd2010-08-19 17:14:32328void ExtensionsService::AddPendingExtensionFromSync(
[email protected]aa142702010-03-26 01:26:33329 const std::string& id, const GURL& update_url,
[email protected]8ef78fd2010-08-19 17:14:32330 PendingExtensionInfo::ExpectedCrxType expected_crx_type,
331 bool install_silently, bool enable_on_install,
332 bool enable_incognito_on_install) {
[email protected]aa142702010-03-26 01:26:33333 if (GetExtensionByIdInternal(id, true, true)) {
[email protected]efee9f262010-03-29 21:26:25334 LOG(DFATAL) << "Trying to add pending extension " << id
335 << " which already exists";
[email protected]aa142702010-03-26 01:26:33336 return;
337 }
[email protected]4416c5a2010-06-26 01:28:57338 AddPendingExtensionInternal(
[email protected]8ef78fd2010-08-19 17:14:32339 id, update_url, expected_crx_type, true, install_silently,
[email protected]4416c5a2010-06-26 01:28:57340 enable_on_install, enable_incognito_on_install);
[email protected]aa142702010-03-26 01:26:33341}
342
[email protected]8ef78fd2010-08-19 17:14:32343void ExtensionsService::AddPendingExtensionFromExternalUpdateUrl(
344 const std::string& id, const GURL& update_url) {
345 // Add the extension to this list of extensions to update.
346 // We do not know if the id refers to a theme, so make is_theme unknown.
347 const PendingExtensionInfo::ExpectedCrxType kExpectedCrxType =
348 PendingExtensionInfo::UNKNOWN;
349 const bool kIsFromSync = false;
350 const bool kInstallSilently = true;
351 const bool kEnableOnInstall = true;
352 const bool kEnableIncognitoOnInstall = false;
353
354 if (GetExtensionByIdInternal(id, true, true)) {
355 LOG(DFATAL) << "Trying to add extension " << id
356 << " by external update, but it is already installed.";
357 return;
358 }
359
360 AddPendingExtensionInternal(id, update_url, kExpectedCrxType, kIsFromSync,
361 kInstallSilently, kEnableOnInstall,
362 kEnableIncognitoOnInstall);
363}
364
[email protected]aa142702010-03-26 01:26:33365void ExtensionsService::AddPendingExtensionInternal(
366 const std::string& id, const GURL& update_url,
[email protected]8ef78fd2010-08-19 17:14:32367 PendingExtensionInfo::ExpectedCrxType expected_crx_type,
368 bool is_from_sync, bool install_silently,
[email protected]4416c5a2010-06-26 01:28:57369 bool enable_on_install, bool enable_incognito_on_install) {
[email protected]aa142702010-03-26 01:26:33370 pending_extensions_[id] =
[email protected]8ef78fd2010-08-19 17:14:32371 PendingExtensionInfo(update_url, expected_crx_type, is_from_sync,
372 install_silently, enable_on_install,
373 enable_incognito_on_install);
[email protected]aa142702010-03-26 01:26:33374}
375
[email protected]9cddd4702009-07-27 22:09:40376void ExtensionsService::ReloadExtension(const std::string& extension_id) {
[email protected]b65272f2009-08-31 15:47:06377 FilePath path;
[email protected]61b411612009-11-10 23:17:41378 Extension* current_extension = GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40379
[email protected]f17dbd42010-08-16 23:21:10380 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]b65272f2009-08-31 15:47:06381 if (current_extension) {
[email protected]4814b512009-11-07 00:12:29382 // If the extension has an inspector open for its background page, detach
383 // the inspector and hang onto a cookie for it, so that we can reattach
384 // later.
385 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
386 ExtensionHost* host = manager->GetBackgroundHostForExtension(
387 current_extension);
388 if (host) {
389 // Look for an open inspector for the background page.
390 int devtools_cookie = DevToolsManager::GetInstance()->DetachClientHost(
391 host->render_view_host());
392 if (devtools_cookie >= 0)
393 orphaned_dev_tools_[extension_id] = devtools_cookie;
394 }
395
[email protected]b65272f2009-08-31 15:47:06396 path = current_extension->path();
[email protected]f17dbd42010-08-16 23:21:10397 DisableExtension(extension_id);
398 disabled_extension_paths_[extension_id] = path;
[email protected]1eb175082010-02-10 09:26:16399 } else {
400 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06401 }
402
[email protected]e6090e42010-03-23 22:44:08403 // Check the installed extensions to see if what we're reloading was already
404 // installed.
405 scoped_ptr<ExtensionInfo> installed_extension(
406 extension_prefs_->GetInstalledExtensionInfo(extension_id));
407 if (installed_extension.get() &&
408 installed_extension->extension_manifest.get()) {
409 LoadInstalledExtension(*installed_extension, false);
410 } else {
411 // We should always be able to remember the extension's path. If it's not in
412 // the map, someone failed to update |unloaded_extension_paths_|.
413 CHECK(!path.empty());
414 LoadExtension(path);
415 }
[email protected]9cddd4702009-07-27 22:09:40416}
417
[email protected]27b985d2009-06-25 17:53:15418void ExtensionsService::UninstallExtension(const std::string& extension_id,
419 bool external_uninstall) {
[email protected]0c6da502009-08-14 22:32:39420 Extension* extension = GetExtensionByIdInternal(extension_id, true, true);
[email protected]631cf822009-05-15 07:01:25421
[email protected]e7afe2452010-08-22 16:19:13422 // Callers should not send us nonexistent extensions.
[email protected]e72e8eb82009-06-18 17:21:51423 DCHECK(extension);
[email protected]9f1087e2009-06-15 17:29:32424
[email protected]4c793f02010-08-18 20:55:45425 // Notify interested parties that we're uninstalling this extension.
426 NotificationService::current()->Notify(
427 NotificationType::EXTENSION_UNINSTALLED,
428 Source<Profile>(profile_),
429 Details<Extension>(extension));
430
[email protected]831aa212010-03-26 13:55:19431 // Get hold of information we need after unloading, since the extension
432 // pointer will be invalid then.
433 GURL extension_url(extension->url());
434 Extension::Location location(extension->location());
435
436 // Also copy the extension identifier since the reference might have been
437 // obtained via Extension::id().
438 std::string extension_id_copy(extension_id);
439
[email protected]56ad3792010-05-28 17:45:33440 if (profile_->GetTemplateURLModel())
441 profile_->GetTemplateURLModel()->UnregisterExtensionKeyword(extension);
442
[email protected]831aa212010-03-26 13:55:19443 // Unload before doing more cleanup to ensure that nothing is hanging on to
444 // any of these resources.
445 UnloadExtension(extension_id);
446
447 extension_prefs_->OnExtensionUninstalled(extension_id_copy, location,
448 external_uninstall);
[email protected]9f1087e2009-06-15 17:29:32449
450 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]831aa212010-03-26 13:55:19451 if (Extension::LOAD != location) {
[email protected]95d29192009-10-30 01:49:06452 ChromeThread::PostTask(
453 ChromeThread::FILE, FROM_HERE,
454 NewRunnableFunction(
[email protected]ca3dbf52010-05-19 22:27:06455 &extension_file_util::UninstallExtension,
456 install_directory_,
457 extension_id_copy));
[email protected]9f1087e2009-06-15 17:29:32458 }
459
[email protected]c10da4b02010-03-25 14:38:32460 ClearExtensionData(extension_url);
461}
462
463void ExtensionsService::ClearExtensionData(const GURL& extension_url) {
464 scoped_refptr<ExtensionDataDeleter> deleter(
465 new ExtensionDataDeleter(profile_, extension_url));
466 deleter->StartDeleting();
[email protected]9f1087e2009-06-15 17:29:32467}
468
[email protected]0c6da502009-08-14 22:32:39469void ExtensionsService::EnableExtension(const std::string& extension_id) {
470 Extension* extension = GetExtensionByIdInternal(extension_id, false, true);
471 if (!extension) {
472 NOTREACHED() << "Trying to enable an extension that isn't disabled.";
473 return;
474 }
475
[email protected]e8c729a2010-03-09 19:55:19476 extension_prefs_->SetExtensionState(extension, Extension::ENABLED);
[email protected]1784e83a2009-09-08 21:01:52477
[email protected]0c6da502009-08-14 22:32:39478 // Move it over to the enabled list.
[email protected]0c6da502009-08-14 22:32:39479 extensions_.push_back(extension);
480 ExtensionList::iterator iter = std::find(disabled_extensions_.begin(),
481 disabled_extensions_.end(),
482 extension);
483 disabled_extensions_.erase(iter);
484
[email protected]86c008e82009-08-28 20:26:05485 ExtensionDOMUI::RegisterChromeURLOverrides(profile_,
486 extension->GetChromeURLOverrides());
487
[email protected]62d30f42009-10-01 22:36:06488 NotifyExtensionLoaded(extension);
[email protected]aab98a52009-12-02 03:22:35489 UpdateActiveExtensionsInCrashReporter();
[email protected]0c6da502009-08-14 22:32:39490}
491
[email protected]1784e83a2009-09-08 21:01:52492void ExtensionsService::DisableExtension(const std::string& extension_id) {
493 Extension* extension = GetExtensionByIdInternal(extension_id, true, false);
[email protected]b2ba9962009-12-10 20:10:15494 // The extension may have been disabled already.
495 if (!extension)
[email protected]1784e83a2009-09-08 21:01:52496 return;
[email protected]1784e83a2009-09-08 21:01:52497
[email protected]e8c729a2010-03-09 19:55:19498 extension_prefs_->SetExtensionState(extension, Extension::DISABLED);
[email protected]1784e83a2009-09-08 21:01:52499
500 // Move it over to the disabled list.
501 disabled_extensions_.push_back(extension);
502 ExtensionList::iterator iter = std::find(extensions_.begin(),
503 extensions_.end(),
504 extension);
505 extensions_.erase(iter);
506
507 ExtensionDOMUI::UnregisterChromeURLOverrides(profile_,
508 extension->GetChromeURLOverrides());
509
[email protected]62d30f42009-10-01 22:36:06510 NotifyExtensionUnloaded(extension);
[email protected]aab98a52009-12-02 03:22:35511 UpdateActiveExtensionsInCrashReporter();
[email protected]1784e83a2009-09-08 21:01:52512}
513
[email protected]9f1087e2009-06-15 17:29:32514void ExtensionsService::LoadExtension(const FilePath& extension_path) {
[email protected]95d29192009-10-30 01:49:06515 ChromeThread::PostTask(
516 ChromeThread::FILE, FROM_HERE,
517 NewRunnableMethod(
518 backend_.get(),
519 &ExtensionsServiceBackend::LoadSingleExtension,
520 extension_path, scoped_refptr<ExtensionsService>(this)));
[email protected]9f1087e2009-06-15 17:29:32521}
522
[email protected]1952c7d2010-03-04 23:48:34523void ExtensionsService::LoadComponentExtensions() {
524 for (RegisteredComponentExtensions::iterator it =
525 component_extension_manifests_.begin();
526 it != component_extension_manifests_.end(); ++it) {
527 JSONStringValueSerializer serializer(it->manifest);
[email protected]ba399672010-04-06 15:42:39528 scoped_ptr<Value> manifest(serializer.Deserialize(NULL, NULL));
[email protected]999731f2010-03-22 19:13:53529 if (!manifest.get()) {
[email protected]960ef8ae2010-05-06 17:12:29530 DLOG(ERROR) << "Failed to retrieve manifest for extension";
[email protected]999731f2010-03-22 19:13:53531 continue;
532 }
[email protected]1952c7d2010-03-04 23:48:34533
534 scoped_ptr<Extension> extension(new Extension(it->root_directory));
535 extension->set_location(Extension::COMPONENT);
536
537 std::string error;
538 if (!extension->InitFromValue(
539 *static_cast<DictionaryValue*>(manifest.get()),
540 true, // require key
541 &error)) {
[email protected]4fdbc1492010-07-01 01:20:59542 NOTREACHED() << error;
[email protected]1952c7d2010-03-04 23:48:34543 return;
544 }
545
[email protected]fe13bf62010-08-26 14:33:19546 // In order for the --apps-gallery-url switch to work with the gallery
547 // process isolation, we must insert any provided value into the component
548 // app's launch url and web extent.
[email protected]7596ce72010-08-30 05:10:46549 if (extension->id() == extension_misc::kWebStoreAppId) {
[email protected]fe13bf62010-08-26 14:33:19550 GURL gallery_url(CommandLine::ForCurrentProcess()
551 ->GetSwitchValueASCII(switches::kAppsGalleryURL));
552 if (gallery_url.is_valid()) {
553 extension->set_launch_web_url(gallery_url.spec());
554 URLPattern pattern(URLPattern::SCHEME_HTTP | URLPattern::SCHEME_HTTPS);
555 pattern.Parse(gallery_url.spec());
556 pattern.set_path(pattern.path() + '*');
557 extension->web_extent().AddPattern(pattern);
558 }
559 }
560
[email protected]1952c7d2010-03-04 23:48:34561 OnExtensionLoaded(extension.release(), false); // Don't allow privilege
562 // increase.
563 }
564}
565
[email protected]9f1087e2009-06-15 17:29:32566void ExtensionsService::LoadAllExtensions() {
[email protected]cc2c3432009-11-06 17:24:36567 base::TimeTicks start_time = base::TimeTicks::Now();
568
[email protected]1952c7d2010-03-04 23:48:34569 // Load any component extensions.
570 LoadComponentExtensions();
571
[email protected]e72e8eb82009-06-18 17:21:51572 // Load the previously installed extensions.
[email protected]c6d474f82009-12-16 21:11:06573 scoped_ptr<ExtensionPrefs::ExtensionsInfo> info(
[email protected]e6090e42010-03-23 22:44:08574 extension_prefs_->GetInstalledExtensionsInfo());
[email protected]c6d474f82009-12-16 21:11:06575
576 // If any extensions need localization, we bounce them all to the file thread
577 // for re-reading and localization.
578 for (size_t i = 0; i < info->size(); ++i) {
[email protected]2111b1a2010-03-12 18:12:44579 if (ShouldReloadExtensionManifest(*info->at(i))) {
[email protected]c6d474f82009-12-16 21:11:06580 ChromeThread::PostTask(
581 ChromeThread::FILE, FROM_HERE, NewRunnableMethod(
582 backend_.get(),
[email protected]2111b1a2010-03-12 18:12:44583 &ExtensionsServiceBackend::ReloadExtensionManifests,
[email protected]c6d474f82009-12-16 21:11:06584 info.release(), // Callee takes ownership of the memory.
585 start_time,
586 scoped_refptr<ExtensionsService>(this)));
587 return;
588 }
589 }
590
591 // Don't update prefs.
592 // Callee takes ownership of the memory.
593 ContinueLoadAllExtensions(info.release(), start_time, false);
594}
595
596void ExtensionsService::ContinueLoadAllExtensions(
597 ExtensionPrefs::ExtensionsInfo* extensions_info,
598 base::TimeTicks start_time,
599 bool write_to_prefs) {
600 scoped_ptr<ExtensionPrefs::ExtensionsInfo> info(extensions_info);
601
602 for (size_t i = 0; i < info->size(); ++i) {
603 LoadInstalledExtension(*info->at(i), write_to_prefs);
604 }
605
[email protected]ae09ca62009-08-21 19:46:46606 OnLoadedInstalledExtensions();
[email protected]cc2c3432009-11-06 17:24:36607
608 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadAll", extensions_.size());
609 UMA_HISTOGRAM_COUNTS_100("Extensions.Disabled", disabled_extensions_.size());
610
[email protected]1952c7d2010-03-04 23:48:34611 UMA_HISTOGRAM_TIMES("Extensions.LoadAllTime",
612 base::TimeTicks::Now() - start_time);
[email protected]cc2c3432009-11-06 17:24:36613
[email protected]1952c7d2010-03-04 23:48:34614 int user_script_count = 0;
615 int extension_count = 0;
616 int theme_count = 0;
617 int external_count = 0;
618 int page_action_count = 0;
619 int browser_action_count = 0;
620 ExtensionList::iterator ex;
621 for (ex = extensions_.begin(); ex != extensions_.end(); ++ex) {
622 // Don't count component extensions, since they are only extensions as an
623 // implementation detail.
624 if ((*ex)->location() == Extension::COMPONENT)
625 continue;
626
[email protected]e8c729a2010-03-09 19:55:19627 // Don't count unpacked extensions, since they're a developer-specific
628 // feature.
629 if ((*ex)->location() == Extension::LOAD)
630 continue;
631
[email protected]3ba0fd32010-06-19 05:39:10632 if ((*ex)->is_theme()) {
[email protected]1952c7d2010-03-04 23:48:34633 theme_count++;
634 } else if ((*ex)->converted_from_user_script()) {
635 user_script_count++;
636 } else {
637 extension_count++;
[email protected]cc2c3432009-11-06 17:24:36638 }
[email protected]1952c7d2010-03-04 23:48:34639 if (Extension::IsExternalLocation((*ex)->location())) {
640 external_count++;
641 }
642 if ((*ex)->page_action() != NULL) {
643 page_action_count++;
644 }
645 if ((*ex)->browser_action() != NULL) {
646 browser_action_count++;
647 }
[email protected]cc2c3432009-11-06 17:24:36648 }
[email protected]1952c7d2010-03-04 23:48:34649 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadExtension", extension_count);
650 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadUserScript", user_script_count);
651 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadTheme", theme_count);
652 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadExternal", external_count);
653 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadPageAction", page_action_count);
654 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadBrowserAction",
655 browser_action_count);
[email protected]ae09ca62009-08-21 19:46:46656}
657
[email protected]c6d474f82009-12-16 21:11:06658void ExtensionsService::LoadInstalledExtension(const ExtensionInfo& info,
659 bool write_to_prefs) {
[email protected]ae09ca62009-08-21 19:46:46660 std::string error;
661 Extension* extension = NULL;
[email protected]306a2bd2010-08-11 14:56:36662 if (!extension_prefs_->IsExtensionAllowedByPolicy(info.extension_id)) {
663 error = errors::kDisabledByPolicy;
664 } else if (info.extension_manifest.get()) {
[email protected]c6d474f82009-12-16 21:11:06665 scoped_ptr<Extension> tmp(new Extension(info.extension_path));
[email protected]e8c729a2010-03-09 19:55:19666 bool require_key = info.extension_location != Extension::LOAD;
667 if (tmp->InitFromValue(*info.extension_manifest, require_key, &error))
[email protected]ae09ca62009-08-21 19:46:46668 extension = tmp.release();
[email protected]ae09ca62009-08-21 19:46:46669 } else {
[email protected]c6d474f82009-12-16 21:11:06670 error = errors::kManifestUnreadable;
[email protected]ae09ca62009-08-21 19:46:46671 }
672
673 if (!extension) {
[email protected]c6d474f82009-12-16 21:11:06674 ReportExtensionLoadError(info.extension_path,
[email protected]d11c8e92009-10-20 23:26:40675 error,
676 NotificationType::EXTENSION_INSTALL_ERROR,
677 false);
[email protected]ae09ca62009-08-21 19:46:46678 return;
679 }
680
[email protected]c6d474f82009-12-16 21:11:06681 extension->set_location(info.extension_location);
682
683 if (write_to_prefs)
684 extension_prefs_->UpdateManifest(extension);
685
[email protected]2a409532009-08-28 19:39:44686 OnExtensionLoaded(extension, true);
[email protected]ae09ca62009-08-21 19:46:46687
[email protected]c6d474f82009-12-16 21:11:06688 if (info.extension_location == Extension::EXTERNAL_PREF ||
689 info.extension_location == Extension::EXTERNAL_REGISTRY) {
[email protected]95d29192009-10-30 01:49:06690 ChromeThread::PostTask(
691 ChromeThread::FILE, FROM_HERE,
692 NewRunnableMethod(
[email protected]f17dbd42010-08-16 23:21:10693 backend_.get(),
694 &ExtensionsServiceBackend::CheckExternalUninstall,
695 scoped_refptr<ExtensionsService>(this),
696 info.extension_id,
697 info.extension_location));
[email protected]ae09ca62009-08-21 19:46:46698 }
[email protected]9f1087e2009-06-15 17:29:32699}
700
[email protected]62d30f42009-10-01 22:36:06701void ExtensionsService::NotifyExtensionLoaded(Extension* extension) {
[email protected]57a777f72010-03-31 01:09:42702 // The ChromeURLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:06703 // was loaded, otherwise a race can arise where a renderer that is created
704 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:42705 // that the request context doesn't yet know about. The profile is responsible
706 // for ensuring its URLRequestContexts appropriately discover the loaded
707 // extension.
708 if (profile_) {
709 profile_->RegisterExtensionWithRequestContexts(extension);
[email protected]24b538a2010-02-27 01:22:44710
711 // Check if this permission requires unlimited storage quota
[email protected]c2c263c2010-08-13 21:59:48712 if (extension->HasApiPermission(Extension::kUnlimitedStoragePermission))
713 GrantUnlimitedStorage(extension);
[email protected]654512b2010-09-01 02:09:42714
715 // If the extension is an app, protect its local storage from
716 // "Clear browsing data."
717 if (extension->is_app())
718 GrantProtectedStorage(extension);
[email protected]62d30f42009-10-01 22:36:06719 }
720
[email protected]57a777f72010-03-31 01:09:42721 LOG(INFO) << "Sending EXTENSION_LOADED";
722
[email protected]62d30f42009-10-01 22:36:06723 NotificationService::current()->Notify(
724 NotificationType::EXTENSION_LOADED,
[email protected]24e7a9d2009-11-04 11:11:34725 Source<Profile>(profile_),
[email protected]62d30f42009-10-01 22:36:06726 Details<Extension>(extension));
727}
728
729void ExtensionsService::NotifyExtensionUnloaded(Extension* extension) {
730 LOG(INFO) << "Sending EXTENSION_UNLOADED";
731
732 NotificationService::current()->Notify(
733 NotificationType::EXTENSION_UNLOADED,
[email protected]24e7a9d2009-11-04 11:11:34734 Source<Profile>(profile_),
[email protected]62d30f42009-10-01 22:36:06735 Details<Extension>(extension));
736
[email protected]57a777f72010-03-31 01:09:42737 if (profile_) {
738 profile_->UnregisterExtensionWithRequestContexts(extension);
739
740 // Check if this permission required unlimited storage quota, reset its
741 // in-memory quota.
[email protected]c2c263c2010-08-13 21:59:48742 if (extension->HasApiPermission(Extension::kUnlimitedStoragePermission))
743 RevokeUnlimitedStorage(extension);
[email protected]654512b2010-09-01 02:09:42744
745 // If this is an app, then stop protecting its storage so it can be deleted.
746 if (extension->is_app())
747 RevokeProtectedStorage(extension);
748 }
749}
750
751void ExtensionsService::GrantProtectedStorage(Extension* extension) {
752 DCHECK(extension->is_app()) << "Only Apps are allowed protected storage.";
753 std::vector<GURL> origins;
754 GetExplicitOriginsInExtent(extension, &origins);
755 for (size_t i = 0; i < origins.size(); ++i)
756 ++protected_storage_map_[origins[i]];
757}
758
759void ExtensionsService::RevokeProtectedStorage(Extension* extension) {
760 DCHECK(extension->is_app()) << "Attempting to revoke protected storage from "
761 << " a non-app extension.";
762 std::vector<GURL> origins;
763 GetExplicitOriginsInExtent(extension, &origins);
764 for (size_t i = 0; i < origins.size(); ++i) {
765 const GURL& origin = origins[i];
766 DCHECK(protected_storage_map_[origin] > 0);
767 if (--protected_storage_map_[origin] <= 0)
768 protected_storage_map_.erase(origin);
[email protected]c2c263c2010-08-13 21:59:48769 }
770}
771
772void ExtensionsService::GrantUnlimitedStorage(Extension* extension) {
773 DCHECK(extension->HasApiPermission(Extension::kUnlimitedStoragePermission));
774 std::vector<GURL> origins;
775 GetExplicitOriginsInExtent(extension, &origins);
776 origins.push_back(extension->url());
777
778 for (size_t i = 0; i < origins.size(); ++i) {
779 const GURL& origin = origins[i];
780 if (++unlimited_storage_map_[origin] == 1) {
[email protected]57a777f72010-03-31 01:09:42781 string16 origin_identifier =
[email protected]c2c263c2010-08-13 21:59:48782 webkit_database::DatabaseUtil::GetOriginIdentifier(origin);
783 ChromeThread::PostTask(
784 ChromeThread::FILE, FROM_HERE,
785 NewRunnableMethod(
786 profile_->GetDatabaseTracker(),
787 &webkit_database::DatabaseTracker::SetOriginQuotaInMemory,
788 origin_identifier,
789 kint64max));
790 ChromeThread::PostTask(
791 ChromeThread::IO, FROM_HERE,
792 NewRunnableMethod(
793 profile_->GetAppCacheService(),
794 &ChromeAppCacheService::SetOriginQuotaInMemory,
795 origin,
796 kint64max));
797 }
798 }
799}
800
801void ExtensionsService::RevokeUnlimitedStorage(Extension* extension) {
802 DCHECK(extension->HasApiPermission(Extension::kUnlimitedStoragePermission));
803 std::vector<GURL> origins;
804 GetExplicitOriginsInExtent(extension, &origins);
805 origins.push_back(extension->url());
806
807 for (size_t i = 0; i < origins.size(); ++i) {
808 const GURL& origin = origins[i];
809 DCHECK(unlimited_storage_map_[origin] > 0);
810 if (--unlimited_storage_map_[origin] == 0) {
811 unlimited_storage_map_.erase(origin);
812 string16 origin_identifier =
813 webkit_database::DatabaseUtil::GetOriginIdentifier(origin);
[email protected]95d29192009-10-30 01:49:06814 ChromeThread::PostTask(
[email protected]57a777f72010-03-31 01:09:42815 ChromeThread::FILE, FROM_HERE,
[email protected]be180c802009-10-23 06:33:31816 NewRunnableMethod(
[email protected]57a777f72010-03-31 01:09:42817 profile_->GetDatabaseTracker(),
818 &webkit_database::DatabaseTracker::ResetOriginQuotaInMemory,
819 origin_identifier));
[email protected]c2c263c2010-08-13 21:59:48820 ChromeThread::PostTask(
821 ChromeThread::IO, FROM_HERE,
822 NewRunnableMethod(
823 profile_->GetAppCacheService(),
824 &ChromeAppCacheService::ResetOriginQuotaInMemory,
825 origin));
[email protected]62d30f42009-10-01 22:36:06826 }
827 }
828}
829
[email protected]6b75ec32009-08-14 06:37:18830void ExtensionsService::UpdateExtensionBlacklist(
831 const std::vector<std::string>& blacklist) {
832 // Use this set to indicate if an extension in the blacklist has been used.
833 std::set<std::string> blacklist_set;
834 for (unsigned int i = 0; i < blacklist.size(); ++i) {
835 if (Extension::IdIsValid(blacklist[i])) {
836 blacklist_set.insert(blacklist[i]);
837 }
838 }
839 extension_prefs_->UpdateBlacklist(blacklist_set);
840 std::vector<std::string> to_be_removed;
841 // Loop current extensions, unload installed extensions.
842 for (ExtensionList::const_iterator iter = extensions_.begin();
843 iter != extensions_.end(); ++iter) {
844 Extension* extension = (*iter);
845 if (blacklist_set.find(extension->id()) != blacklist_set.end()) {
846 to_be_removed.push_back(extension->id());
847 }
848 }
849
850 // UnloadExtension will change the extensions_ list. So, we should
851 // call it outside the iterator loop.
852 for (unsigned int i = 0; i < to_be_removed.size(); ++i) {
853 UnloadExtension(to_be_removed[i]);
854 }
855}
856
[email protected]aa96d3a2010-08-21 08:45:25857void ExtensionsService::DestroyingProfile() {
858 profile_->GetPrefs()->RemovePrefObserver(
859 prefs::kExtensionInstallAllowList, this);
860 profile_->GetPrefs()->RemovePrefObserver(
861 prefs::kExtensionInstallDenyList, this);
862
863 profile_ = NULL;
864}
865
866void ExtensionsService::CheckAdminBlacklist() {
867 std::vector<std::string> to_be_removed;
868 // Loop through extensions list, unload installed extensions.
869 for (ExtensionList::const_iterator iter = extensions_.begin();
870 iter != extensions_.end(); ++iter) {
871 Extension* extension = (*iter);
872 if (!extension_prefs_->IsExtensionAllowedByPolicy(extension->id()))
873 to_be_removed.push_back(extension->id());
874 }
875
876 // UnloadExtension will change the extensions_ list. So, we should
877 // call it outside the iterator loop.
878 for (unsigned int i = 0; i < to_be_removed.size(); ++i)
879 UnloadExtension(to_be_removed[i]);
880}
881
[email protected]cb0ce1e022010-03-10 19:54:41882bool ExtensionsService::IsIncognitoEnabled(const Extension* extension) {
883 // If this is a component extension we always allow it to work in incognito
884 // mode.
885 if (extension->location() == Extension::COMPONENT)
886 return true;
887
888 // Check the prefs.
889 return extension_prefs_->IsIncognitoEnabled(extension->id());
[email protected]db7331a2010-02-25 22:10:50890}
[email protected]55a35692010-02-11 23:25:21891
[email protected]cb0ce1e022010-03-10 19:54:41892void ExtensionsService::SetIsIncognitoEnabled(Extension* extension,
[email protected]db7331a2010-02-25 22:10:50893 bool enabled) {
[email protected]cb0ce1e022010-03-10 19:54:41894 extension_prefs_->SetIsIncognitoEnabled(extension->id(), enabled);
[email protected]c1499f3d2010-03-05 00:33:24895
[email protected]568f33d2010-08-04 17:06:41896 // Broadcast unloaded and loaded events to update browser state. Only bother
897 // if the extension is actually enabled, since there is no UI otherwise.
898 bool is_enabled = std::find(extensions_.begin(), extensions_.end(),
899 extension) != extensions_.end();
900 if (is_enabled) {
901 NotifyExtensionUnloaded(extension);
902 NotifyExtensionLoaded(extension);
903 }
[email protected]55a35692010-02-11 23:25:21904}
905
[email protected]05c82182010-06-24 17:49:08906bool ExtensionsService::AllowFileAccess(const Extension* extension) {
907 return (CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]334e04a2010-06-24 23:34:44908 switches::kDisableExtensionsFileAccessCheck) ||
[email protected]05c82182010-06-24 17:49:08909 extension_prefs_->AllowFileAccess(extension->id()));
910}
911
912void ExtensionsService::SetAllowFileAccess(Extension* extension, bool allow) {
913 extension_prefs_->SetAllowFileAccess(extension->id(), allow);
914 NotificationService::current()->Notify(
915 NotificationType::EXTENSION_USER_SCRIPTS_UPDATED,
916 Source<Profile>(profile_),
917 Details<Extension>(extension));
918}
919
920bool ExtensionsService::CanExecuteScriptOnHost(Extension* extension,
921 const GURL& url,
922 std::string* error) const {
923 // No extensions are allowed to execute script on the gallery because that
924 // would allow extensions to manipulate their own install pages.
[email protected]638d45d2010-07-09 17:35:08925 if (url.host() == GURL(Extension::ChromeStoreURL()).host()
926 && !CommandLine::ForCurrentProcess()->HasSwitch(
927 switches::kAllowScriptingGallery)) {
[email protected]05c82182010-06-24 17:49:08928 if (error)
929 *error = errors::kCannotScriptGallery;
930 return false;
931 }
932
933 if (extension->HasHostPermission(url))
934 return true;
935
936 if (error) {
937 *error = ExtensionErrorUtils::FormatErrorMessage(errors::kCannotAccessPage,
938 url.spec());
939 }
940
941 return false;
942}
943
[email protected]93fd78f42009-07-10 16:43:17944void ExtensionsService::CheckForExternalUpdates() {
[email protected]9f1087e2009-06-15 17:29:32945 // This installs or updates externally provided extensions.
[email protected]7577a5c52009-07-30 06:21:58946 // TODO(aa): Why pass this list into the provider, why not just filter it
947 // later?
[email protected]9f1087e2009-06-15 17:29:32948 std::set<std::string> killed_extensions;
[email protected]e72e8eb82009-06-18 17:21:51949 extension_prefs_->GetKilledExtensionIds(&killed_extensions);
[email protected]95d29192009-10-30 01:49:06950 ChromeThread::PostTask(
951 ChromeThread::FILE, FROM_HERE,
952 NewRunnableMethod(
953 backend_.get(), &ExtensionsServiceBackend::CheckForExternalUpdates,
954 killed_extensions, scoped_refptr<ExtensionsService>(this)));
[email protected]9f1087e2009-06-15 17:29:32955}
956
957void ExtensionsService::UnloadExtension(const std::string& extension_id) {
[email protected]27e469a2010-01-11 20:35:09958 // Make sure the extension gets deleted after we return from this function.
[email protected]0c6da502009-08-14 22:32:39959 scoped_ptr<Extension> extension(
960 GetExtensionByIdInternal(extension_id, true, true));
[email protected]631cf822009-05-15 07:01:25961
[email protected]e7afe2452010-08-22 16:19:13962 // Callers should not send us nonexistent extensions.
[email protected]0c6da502009-08-14 22:32:39963 CHECK(extension.get());
964
[email protected]1eb175082010-02-10 09:26:16965 // Keep information about the extension so that we can reload it later
966 // even if it's not permanently installed.
967 unloaded_extension_paths_[extension->id()] = extension->path();
968
[email protected]f17dbd42010-08-16 23:21:10969 // Clean up if the extension is meant to be enabled after a reload.
970 disabled_extension_paths_.erase(extension->id());
971
[email protected]86c008e82009-08-28 20:26:05972 ExtensionDOMUI::UnregisterChromeURLOverrides(profile_,
973 extension->GetChromeURLOverrides());
974
[email protected]0c6da502009-08-14 22:32:39975 ExtensionList::iterator iter = std::find(disabled_extensions_.begin(),
976 disabled_extensions_.end(),
977 extension.get());
978 if (iter != disabled_extensions_.end()) {
[email protected]0c6da502009-08-14 22:32:39979 disabled_extensions_.erase(iter);
[email protected]866930682009-08-18 22:53:47980 NotificationService::current()->Notify(
981 NotificationType::EXTENSION_UNLOADED_DISABLED,
[email protected]24e7a9d2009-11-04 11:11:34982 Source<Profile>(profile_),
[email protected]866930682009-08-18 22:53:47983 Details<Extension>(extension.get()));
[email protected]0c6da502009-08-14 22:32:39984 return;
985 }
986
987 iter = std::find(extensions_.begin(), extensions_.end(), extension.get());
[email protected]894bb502009-05-21 22:39:57988
[email protected]631cf822009-05-15 07:01:25989 // Remove the extension from our list.
990 extensions_.erase(iter);
991
[email protected]62d30f42009-10-01 22:36:06992 NotifyExtensionUnloaded(extension.get());
[email protected]aab98a52009-12-02 03:22:35993 UpdateActiveExtensionsInCrashReporter();
[email protected]631cf822009-05-15 07:01:25994}
995
[email protected]9f1087e2009-06-15 17:29:32996void ExtensionsService::UnloadAllExtensions() {
[email protected]cd500f72010-06-25 23:44:32997 STLDeleteContainerPointers(extensions_.begin(), extensions_.end());
[email protected]9f1087e2009-06-15 17:29:32998 extensions_.clear();
[email protected]c6e4a3412009-06-24 15:45:29999
[email protected]cd500f72010-06-25 23:44:321000 STLDeleteContainerPointers(disabled_extensions_.begin(),
1001 disabled_extensions_.end());
1002 disabled_extensions_.clear();
1003
[email protected]c6e4a3412009-06-24 15:45:291004 // TODO(erikkay) should there be a notification for this? We can't use
1005 // EXTENSION_UNLOADED since that implies that the extension has been disabled
1006 // or uninstalled, and UnloadAll is just part of shutdown.
[email protected]9f1087e2009-06-15 17:29:321007}
1008
1009void ExtensionsService::ReloadExtensions() {
1010 UnloadAllExtensions();
1011 LoadAllExtensions();
1012}
1013
1014void ExtensionsService::GarbageCollectExtensions() {
[email protected]ba399672010-04-06 15:42:391015 if (extension_prefs_->pref_service()->read_only())
1016 return;
1017
[email protected]ca3dbf52010-05-19 22:27:061018 scoped_ptr<ExtensionPrefs::ExtensionsInfo> info(
1019 extension_prefs_->GetInstalledExtensionsInfo());
1020
1021 std::map<std::string, FilePath> extension_paths;
1022 for (size_t i = 0; i < info->size(); ++i)
1023 extension_paths[info->at(i)->extension_id] = info->at(i)->extension_path;
1024
[email protected]95d29192009-10-30 01:49:061025 ChromeThread::PostTask(
1026 ChromeThread::FILE, FROM_HERE,
1027 NewRunnableFunction(
1028 &extension_file_util::GarbageCollectExtensions, install_directory_,
[email protected]ca3dbf52010-05-19 22:27:061029 extension_paths));
[email protected]3cf4f0992009-02-03 23:00:301030}
1031
[email protected]e72e8eb82009-06-18 17:21:511032void ExtensionsService::OnLoadedInstalledExtensions() {
[email protected]e81dba32009-06-19 20:19:131033 ready_ = true;
[email protected]93fd78f42009-07-10 16:43:171034 if (updater_.get()) {
1035 updater_->Start();
1036 }
[email protected]e72e8eb82009-06-18 17:21:511037 NotificationService::current()->Notify(
1038 NotificationType::EXTENSIONS_READY,
[email protected]24e7a9d2009-11-04 11:11:341039 Source<Profile>(profile_),
[email protected]e72e8eb82009-06-18 17:21:511040 NotificationService::NoDetails());
1041}
1042
[email protected]6d2e60bd2010-06-03 22:37:391043void ExtensionsService::OnExtensionLoaded(Extension* extension,
[email protected]2a409532009-08-28 19:39:441044 bool allow_privilege_increase) {
[email protected]ae09ca62009-08-21 19:46:461045 // Ensure extension is deleted unless we transfer ownership.
1046 scoped_ptr<Extension> scoped_extension(extension);
[email protected]9f1087e2009-06-15 17:29:321047
[email protected]1eb175082010-02-10 09:26:161048 // The extension is now loaded, remove its data from unloaded extension map.
1049 unloaded_extension_paths_.erase(extension->id());
1050
[email protected]f17dbd42010-08-16 23:21:101051 // If the extension was disabled for a reload, then enable it.
1052 if (disabled_extension_paths_.erase(extension->id()) > 0)
1053 EnableExtension(extension->id());
1054
[email protected]ceefd3d2010-03-12 09:10:291055 // TODO(aa): Need to re-evaluate this branch. Does this still make sense now
1056 // that extensions are enabled by default?
[email protected]ae09ca62009-08-21 19:46:461057 if (extensions_enabled() ||
[email protected]3ba0fd32010-06-19 05:39:101058 extension->is_theme() ||
[email protected]ae09ca62009-08-21 19:46:461059 extension->location() == Extension::LOAD ||
1060 Extension::IsExternalLocation(extension->location())) {
1061 Extension* old = GetExtensionByIdInternal(extension->id(), true, true);
1062 if (old) {
[email protected]ca3dbf52010-05-19 22:27:061063 // CrxInstaller should have guaranteed that we aren't downgrading.
1064 CHECK(extension->version()->CompareTo(*(old->version())) >= 0);
[email protected]0c6da502009-08-14 22:32:391065
[email protected]ca3dbf52010-05-19 22:27:061066 bool allow_silent_upgrade =
1067 allow_privilege_increase || !Extension::IsPrivilegeIncrease(
1068 old, extension);
[email protected]1e8c93f2010-02-08 22:58:311069
[email protected]ca3dbf52010-05-19 22:27:061070 // Extensions get upgraded if silent upgrades are allowed, otherwise
1071 // they get disabled.
1072 if (allow_silent_upgrade) {
1073 old->set_being_upgraded(true);
1074 extension->set_being_upgraded(true);
1075 }
[email protected]0c6da502009-08-14 22:32:391076
[email protected]ca3dbf52010-05-19 22:27:061077 // To upgrade an extension in place, unload the old one and
1078 // then load the new one.
1079 UnloadExtension(old->id());
1080 old = NULL;
1081
1082 if (!allow_silent_upgrade) {
1083 // Extension has changed permissions significantly. Disable it. We
1084 // send a notification below.
1085 extension_prefs_->SetExtensionState(extension, Extension::DISABLED);
1086 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
[email protected]0c6da502009-08-14 22:32:391087 }
[email protected]ba74f352009-06-11 18:54:451088 }
[email protected]86a274072009-06-11 02:06:451089
[email protected]ae09ca62009-08-21 19:46:461090 switch (extension_prefs_->GetExtensionState(extension->id())) {
1091 case Extension::ENABLED:
1092 extensions_.push_back(scoped_extension.release());
1093
[email protected]62d30f42009-10-01 22:36:061094 NotifyExtensionLoaded(extension);
[email protected]ae09ca62009-08-21 19:46:461095
[email protected]e8c729a2010-03-09 19:55:191096 ExtensionDOMUI::RegisterChromeURLOverrides(profile_,
1097 extension->GetChromeURLOverrides());
[email protected]ae09ca62009-08-21 19:46:461098 break;
1099 case Extension::DISABLED:
[email protected]6d27a7b2009-12-18 23:25:451100 disabled_extensions_.push_back(scoped_extension.release());
[email protected]d11c8e92009-10-20 23:26:401101 NotificationService::current()->Notify(
1102 NotificationType::EXTENSION_UPDATE_DISABLED,
[email protected]24e7a9d2009-11-04 11:11:341103 Source<Profile>(profile_),
[email protected]d11c8e92009-10-20 23:26:401104 Details<Extension>(extension));
[email protected]ae09ca62009-08-21 19:46:461105 break;
1106 default:
[email protected]d11c8e92009-10-20 23:26:401107 NOTREACHED();
[email protected]ae09ca62009-08-21 19:46:461108 break;
[email protected]811f3432009-07-25 19:38:211109 }
[email protected]e72e8eb82009-06-18 17:21:511110 }
[email protected]aab98a52009-12-02 03:22:351111
[email protected]1e8c93f2010-02-08 22:58:311112 extension->set_being_upgraded(false);
1113
[email protected]aab98a52009-12-02 03:22:351114 UpdateActiveExtensionsInCrashReporter();
[email protected]0b004da2010-07-02 17:54:311115
1116 if (profile_->GetTemplateURLModel())
1117 profile_->GetTemplateURLModel()->RegisterExtensionKeyword(extension);
[email protected]b671760b2010-07-15 21:13:471118
1119 // Load the icon for omnibox-enabled extensions so it will be ready to display
1120 // in the URL bar.
1121 if (!extension->omnibox_keyword().empty())
1122 omnibox_icon_manager_.LoadIcon(extension);
[email protected]aab98a52009-12-02 03:22:351123}
1124
1125void ExtensionsService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:391126 std::set<std::string> extension_ids;
[email protected]aab98a52009-12-02 03:22:351127 for (size_t i = 0; i < extensions_.size(); ++i) {
[email protected]3ba0fd32010-06-19 05:39:101128 if (!extensions_[i]->is_theme())
[email protected]c8865962009-12-16 07:47:391129 extension_ids.insert(extensions_[i]->id());
[email protected]aab98a52009-12-02 03:22:351130 }
1131
1132 child_process_logging::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:251133}
1134
[email protected]2a409532009-08-28 19:39:441135void ExtensionsService::OnExtensionInstalled(Extension* extension,
1136 bool allow_privilege_increase) {
[email protected]4416c5a2010-06-26 01:28:571137 // Ensure extension is deleted unless we transfer ownership.
1138 scoped_ptr<Extension> scoped_extension(extension);
1139 Extension::State initial_state = Extension::DISABLED;
1140 bool initial_enable_incognito = false;
[email protected]aa142702010-03-26 01:26:331141 PendingExtensionMap::iterator it =
1142 pending_extensions_.find(extension->id());
[email protected]4416c5a2010-06-26 01:28:571143 if (it != pending_extensions_.end()) {
[email protected]11edd1e2010-07-21 00:14:501144 PendingExtensionInfo pending_extension_info = it->second;
[email protected]8ef78fd2010-08-19 17:14:321145 PendingExtensionInfo::ExpectedCrxType expected_crx_type =
1146 pending_extension_info.expected_crx_type;
1147 bool is_from_sync = pending_extension_info.is_from_sync;
[email protected]11edd1e2010-07-21 00:14:501148 pending_extensions_.erase(it);
1149 it = pending_extensions_.end();
[email protected]8ef78fd2010-08-19 17:14:321150
[email protected]4416c5a2010-06-26 01:28:571151 // Set initial state from pending extension data.
[email protected]8ef78fd2010-08-19 17:14:321152 PendingExtensionInfo::ExpectedCrxType actual_crx_type =
1153 (extension->is_theme() ? PendingExtensionInfo::THEME
1154 : PendingExtensionInfo::EXTENSION);
1155
1156 if (expected_crx_type != PendingExtensionInfo::UNKNOWN &&
1157 expected_crx_type != actual_crx_type) {
[email protected]4416c5a2010-06-26 01:28:571158 LOG(WARNING)
1159 << "Not installing pending extension " << extension->id()
[email protected]8ef78fd2010-08-19 17:14:321160 << " with is_theme = " << extension->is_theme();
[email protected]4416c5a2010-06-26 01:28:571161 // Delete the extension directory since we're not going to
1162 // load it.
1163 ChromeThread::PostTask(
1164 ChromeThread::FILE, FROM_HERE,
1165 NewRunnableFunction(&DeleteFileHelper, extension->path(), true));
1166 return;
1167 }
[email protected]8ef78fd2010-08-19 17:14:321168
1169 // If |extension| is not syncable, and was installed via sync, disallow
1170 // the instanation.
1171 //
1172 // Themes are always allowed. Because they contain no active code, they
1173 // are less of a risk than extensions.
1174 //
1175 // If |is_from_sync| is false, then the install was not initiated by sync,
1176 // and this check should pass. Extensions that were installed from an
1177 // update URL in external_extensions.json are an example. They are not
1178 // syncable, because the user did not make an explicit choice to install
1179 // them. However, they were installed through the update mechanism, so
1180 // control must pass into this function.
1181 //
1182 // TODO(akalin): When we do apps sync, we have to work with its
1183 // traits, too.
[email protected]2a3e22b12010-08-13 04:55:171184 const browser_sync::ExtensionSyncTraits extension_sync_traits =
1185 browser_sync::GetExtensionSyncTraits();
[email protected]06e33202010-08-16 23:45:151186 const browser_sync::ExtensionSyncTraits app_sync_traits =
1187 browser_sync::GetAppSyncTraits();
[email protected]2a3e22b12010-08-13 04:55:171188 // If an extension is a theme, we bypass the valid/syncable check
1189 // as themes are harmless.
[email protected]8ef78fd2010-08-19 17:14:321190 if (!extension->is_theme() && is_from_sync &&
[email protected]2a3e22b12010-08-13 04:55:171191 !browser_sync::IsExtensionValidAndSyncable(
[email protected]06e33202010-08-16 23:45:151192 *extension, extension_sync_traits.allowed_extension_types) &&
1193 !browser_sync::IsExtensionValidAndSyncable(
1194 *extension, app_sync_traits.allowed_extension_types)) {
[email protected]11edd1e2010-07-21 00:14:501195 // We're an extension installed via sync that is unsyncable,
1196 // i.e. we may have been syncable previously. We block these
1197 // installs. We'll have to update the clause above if we decide
1198 // to sync other extension-like things, like apps or user
1199 // scripts.
1200 //
1201 // Note that this creates a small window where a user who tries
1202 // to download/install an extension that is simultaneously
1203 // installed via sync (and blocked) will find his download
1204 // blocked.
1205 //
1206 // TODO(akalin): Remove this check once we've put in UI to
1207 // approve synced extensions.
1208 LOG(WARNING)
[email protected]2a3e22b12010-08-13 04:55:171209 << "Not installing invalid or unsyncable extension "
1210 << extension->id();
[email protected]11edd1e2010-07-21 00:14:501211 // Delete the extension directory since we're not going to
1212 // load it.
1213 ChromeThread::PostTask(
1214 ChromeThread::FILE, FROM_HERE,
1215 NewRunnableFunction(&DeleteFileHelper, extension->path(), true));
1216 return;
1217 }
[email protected]8ef78fd2010-08-19 17:14:321218 if (extension->is_theme()) {
[email protected]11edd1e2010-07-21 00:14:501219 DCHECK(pending_extension_info.enable_on_install);
[email protected]4416c5a2010-06-26 01:28:571220 initial_state = Extension::ENABLED;
[email protected]11edd1e2010-07-21 00:14:501221 DCHECK(!pending_extension_info.enable_incognito_on_install);
[email protected]4416c5a2010-06-26 01:28:571222 initial_enable_incognito = false;
1223 } else {
1224 initial_state =
[email protected]11edd1e2010-07-21 00:14:501225 pending_extension_info.enable_on_install ?
[email protected]4416c5a2010-06-26 01:28:571226 Extension::ENABLED : Extension::DISABLED;
1227 initial_enable_incognito =
[email protected]11edd1e2010-07-21 00:14:501228 pending_extension_info.enable_incognito_on_install;
[email protected]4416c5a2010-06-26 01:28:571229 }
[email protected]4416c5a2010-06-26 01:28:571230 } else {
[email protected]dbec3792010-08-10 00:08:451231 // Make sure we preserve enabled/disabled states.
[email protected]4416c5a2010-06-26 01:28:571232 Extension::State existing_state =
1233 extension_prefs_->GetExtensionState(extension->id());
1234 initial_state =
1235 (existing_state == Extension::DISABLED) ?
1236 Extension::DISABLED : Extension::ENABLED;
[email protected]dbec3792010-08-10 00:08:451237 initial_enable_incognito =
1238 extension_prefs_->IsIncognitoEnabled(extension->id());
[email protected]aa142702010-03-26 01:26:331239 }
1240
[email protected]4416c5a2010-06-26 01:28:571241 extension_prefs_->OnExtensionInstalled(
1242 extension, initial_state, initial_enable_incognito);
[email protected]25b34332009-06-05 21:53:191243
[email protected]92a5b1d2010-07-20 00:42:001244 // Unpacked extensions start off with file access since they are a developer
1245 // feature.
1246 if (extension->location() == Extension::LOAD)
1247 extension_prefs_->SetAllowFileAccess(extension->id(), true);
1248
[email protected]4a190632009-05-09 01:07:421249 // If the extension is a theme, tell the profile (and therefore ThemeProvider)
1250 // to apply it.
[email protected]3ba0fd32010-06-19 05:39:101251 if (extension->is_theme()) {
[email protected]9ceb07342009-07-26 04:09:231252 NotificationService::current()->Notify(
1253 NotificationType::THEME_INSTALLED,
[email protected]24e7a9d2009-11-04 11:11:341254 Source<Profile>(profile_),
[email protected]9ceb07342009-07-26 04:09:231255 Details<Extension>(extension));
[email protected]9197f3b2009-06-02 00:49:271256 } else {
1257 NotificationService::current()->Notify(
1258 NotificationType::EXTENSION_INSTALLED,
[email protected]24e7a9d2009-11-04 11:11:341259 Source<Profile>(profile_),
[email protected]9197f3b2009-06-02 00:49:271260 Details<Extension>(extension));
[email protected]4a190632009-05-09 01:07:421261 }
[email protected]7577a5c52009-07-30 06:21:581262
[email protected]4416c5a2010-06-26 01:28:571263 // Transfer ownership of |extension| to OnExtensionLoaded.
1264 OnExtensionLoaded(scoped_extension.release(), allow_privilege_increase);
[email protected]4a190632009-05-09 01:07:421265}
1266
[email protected]0c6da502009-08-14 22:32:391267Extension* ExtensionsService::GetExtensionByIdInternal(const std::string& id,
1268 bool include_enabled,
1269 bool include_disabled) {
[email protected]e957fe52009-06-23 16:51:051270 std::string lowercase_id = StringToLowerASCII(id);
[email protected]0c6da502009-08-14 22:32:391271 if (include_enabled) {
1272 for (ExtensionList::const_iterator iter = extensions_.begin();
1273 iter != extensions_.end(); ++iter) {
1274 if ((*iter)->id() == lowercase_id)
1275 return *iter;
1276 }
1277 }
1278 if (include_disabled) {
1279 for (ExtensionList::const_iterator iter = disabled_extensions_.begin();
1280 iter != disabled_extensions_.end(); ++iter) {
1281 if ((*iter)->id() == lowercase_id)
1282 return *iter;
1283 }
[email protected]ce5c4502009-05-06 16:46:111284 }
1285 return NULL;
1286}
1287
[email protected]9f1087e2009-06-15 17:29:321288Extension* ExtensionsService::GetExtensionByURL(const GURL& url) {
[email protected]a888b29e62010-04-01 13:38:571289 return url.scheme() != chrome::kExtensionScheme ? NULL :
1290 GetExtensionById(url.host(), false);
1291}
1292
1293Extension* ExtensionsService::GetExtensionByWebExtent(const GURL& url) {
1294 for (size_t i = 0; i < extensions_.size(); ++i) {
1295 if (extensions_[i]->web_extent().ContainsURL(url))
1296 return extensions_[i];
1297 }
1298 return NULL;
[email protected]9f1087e2009-06-15 17:29:321299}
1300
[email protected]583d45c12010-08-31 02:48:121301bool ExtensionsService::ExtensionBindingsAllowed(const GURL& url) {
1302 // Allow bindings for all packaged extension.
1303 if (GetExtensionByURL(url))
1304 return true;
1305
1306 // Allow bindings for all component, hosted apps.
1307 Extension* extension = GetExtensionByWebExtent(url);
1308 return (extension && extension->location() == Extension::COMPONENT);
1309}
1310
[email protected]6d2e60bd2010-06-03 22:37:391311Extension* ExtensionsService::GetExtensionByOverlappingWebExtent(
[email protected]9f72aa02010-06-25 10:01:051312 const ExtensionExtent& extent) {
[email protected]22c966c2010-06-26 06:35:021313 for (size_t i = 0; i < extensions_.size(); ++i) {
1314 if (extensions_[i]->web_extent().OverlapsWith(extent))
1315 return extensions_[i];
1316 }
1317
[email protected]6d2e60bd2010-06-03 22:37:391318 return NULL;
1319}
1320
[email protected]b671760b2010-07-15 21:13:471321const SkBitmap& ExtensionsService::GetOmniboxIcon(
1322 const std::string& extension_id) {
1323 return omnibox_icon_manager_.GetIcon(extension_id);
1324}
1325
[email protected]a1257b12009-06-12 02:51:341326void ExtensionsService::ClearProvidersForTesting() {
[email protected]95d29192009-10-30 01:49:061327 ChromeThread::PostTask(
1328 ChromeThread::FILE, FROM_HERE,
1329 NewRunnableMethod(
1330 backend_.get(), &ExtensionsServiceBackend::ClearProvidersForTesting));
[email protected]a1257b12009-06-12 02:51:341331}
1332
1333void ExtensionsService::SetProviderForTesting(
1334 Extension::Location location, ExternalExtensionProvider* test_provider) {
[email protected]95d29192009-10-30 01:49:061335 ChromeThread::PostTask(
1336 ChromeThread::FILE, FROM_HERE,
1337 NewRunnableMethod(
1338 backend_.get(), &ExtensionsServiceBackend::SetProviderForTesting,
1339 location, test_provider));
[email protected]a1257b12009-06-12 02:51:341340}
1341
[email protected]8ef78fd2010-08-19 17:14:321342void ExtensionsService::OnExternalExtensionFileFound(
1343 const std::string& id,
1344 const std::string& version,
1345 const FilePath& path,
1346 Extension::Location location) {
[email protected]7577a5c52009-07-30 06:21:581347 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:491348 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:581349 // installed on every startup.
[email protected]61b411612009-11-10 23:17:411350 Extension* existing = GetExtensionById(id, true);
[email protected]a3a63ff82009-08-04 06:44:111351 scoped_ptr<Version> other(Version::GetVersionFromString(version));
[email protected]7577a5c52009-07-30 06:21:581352 if (existing) {
[email protected]a3a63ff82009-08-04 06:44:111353 switch (existing->version()->CompareTo(*other)) {
[email protected]7577a5c52009-07-30 06:21:581354 case -1: // existing version is older, we should upgrade
1355 break;
1356 case 0: // existing version is same, do nothing
1357 return;
1358 case 1: // existing version is newer, uh-oh
1359 LOG(WARNING) << "Found external version of extension " << id
1360 << "that is older than current version. Current version "
1361 << "is: " << existing->VersionString() << ". New version "
1362 << "is: " << version << ". Keeping current version.";
1363 return;
1364 }
1365 }
1366
[email protected]6dfbbf82010-03-12 23:09:161367 scoped_refptr<CrxInstaller> installer(
1368 new CrxInstaller(install_directory_,
1369 this, // frontend
1370 NULL)); // no client (silent install)
1371 installer->set_install_source(location);
1372 installer->set_expected_id(id);
1373 installer->set_allow_privilege_increase(true);
1374 installer->InstallCrx(path);
[email protected]7577a5c52009-07-30 06:21:581375}
1376
[email protected]d11c8e92009-10-20 23:26:401377void ExtensionsService::ReportExtensionLoadError(
1378 const FilePath& extension_path,
1379 const std::string &error,
1380 NotificationType type,
1381 bool be_noisy) {
1382 NotificationService* service = NotificationService::current();
1383 service->Notify(type,
[email protected]24e7a9d2009-11-04 11:11:341384 Source<Profile>(profile_),
[email protected]d11c8e92009-10-20 23:26:401385 Details<const std::string>(&error));
1386
1387 // TODO(port): note that this isn't guaranteed to work properly on Linux.
[email protected]99efb7b12009-12-18 02:39:161388 std::string path_str = WideToUTF8(extension_path.ToWStringHack());
[email protected]d11c8e92009-10-20 23:26:401389 std::string message = StringPrintf("Could not load extension from '%s'. %s",
1390 path_str.c_str(), error.c_str());
1391 ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy);
1392}
1393
[email protected]4814b512009-11-07 00:12:291394void ExtensionsService::Observe(NotificationType type,
1395 const NotificationSource& source,
1396 const NotificationDetails& details) {
1397 switch (type.value) {
1398 case NotificationType::EXTENSION_HOST_DID_STOP_LOADING: {
1399 ExtensionHost* host = Details<ExtensionHost>(details).ptr();
1400 OrphanedDevTools::iterator iter =
1401 orphaned_dev_tools_.find(host->extension()->id());
1402 if (iter == orphaned_dev_tools_.end())
1403 return;
1404
1405 DevToolsManager::GetInstance()->AttachClientHost(
1406 iter->second, host->render_view_host());
1407 orphaned_dev_tools_.erase(iter);
1408 break;
1409 }
1410
[email protected]a4ed6282009-12-14 20:51:161411 case NotificationType::EXTENSION_PROCESS_TERMINATED: {
[email protected]bc535ee52010-08-31 18:40:321412 if (profile_ != Source<Profile>(source).ptr()->GetOriginalProfile())
1413 break;
[email protected]a4ed6282009-12-14 20:51:161414
[email protected]f128af42010-08-05 18:05:261415 ExtensionHost* host = Details<ExtensionHost>(details).ptr();
1416
1417 // TODO(rafaelw): Remove this check and ExtensionHost::recently_deleted().
1418 // This is only here to help track down crbug.com/49114.
1419 ExtensionHost::HostPointerList::iterator iter =
1420 ExtensionHost::recently_deleted()->begin();
1421 for (; iter != ExtensionHost::recently_deleted()->end(); iter++) {
1422 if (*iter == host) {
1423 CHECK(host->GetURL().spec().size() + 2 != 0);
1424 break;
1425 }
1426 }
1427 if (iter == ExtensionHost::recently_deleted()->end())
1428 CHECK(host->GetURL().spec().size() + 1 != 0);
1429
[email protected]31f77262009-12-02 20:48:531430 // Unload the entire extension. We want it to be in a consistent state:
1431 // either fully working or not loaded at all, but never half-crashed.
[email protected]bc535ee52010-08-31 18:40:321432 // We do it in a PostTask so that other handlers of this notification will
1433 // still have access to the Extension and ExtensionHost.
1434 MessageLoop::current()->PostTask(FROM_HERE,
1435 NewRunnableMethod(this, &ExtensionsService::UnloadExtension,
1436 host->extension()->id()));
[email protected]31f77262009-12-02 20:48:531437 break;
1438 }
1439
[email protected]aa96d3a2010-08-21 08:45:251440 case NotificationType::PREF_CHANGED: {
1441 std::string* pref_name = Details<std::string>(details).ptr();
1442 DCHECK(*pref_name == prefs::kExtensionInstallAllowList ||
1443 *pref_name == prefs::kExtensionInstallDenyList);
1444 CheckAdminBlacklist();
1445 break;
1446 }
1447
[email protected]4814b512009-11-07 00:12:291448 default:
1449 NOTREACHED() << "Unexpected notification type.";
1450 }
1451}
1452
[email protected]377011d2010-07-20 04:18:501453bool ExtensionsService::HasApps() {
1454 if (!extensions_enabled_)
1455 return false;
1456
1457 for (ExtensionList::const_iterator it = extensions_.begin();
1458 it != extensions_.end(); ++it) {
1459 if ((*it)->is_app())
1460 return true;
1461 }
1462
1463 return false;
1464}
[email protected]4814b512009-11-07 00:12:291465
[email protected]6014d672008-12-05 00:38:251466// ExtensionsServicesBackend
1467
[email protected]894bb502009-05-21 22:39:571468ExtensionsServiceBackend::ExtensionsServiceBackend(
[email protected]95d29192009-10-30 01:49:061469 const FilePath& install_directory)
[email protected]0c7bc4b2009-05-30 01:47:081470 : frontend_(NULL),
1471 install_directory_(install_directory),
[email protected]8ef78fd2010-08-19 17:14:321472 alert_on_error_(false),
1473 external_extension_added_(false) {
[email protected]7577a5c52009-07-30 06:21:581474 // TODO(aa): This ends up doing blocking IO on the UI thread because it reads
1475 // pref data in the ctor and that is called on the UI thread. Would be better
1476 // to re-read data each time we list external extensions, anyway.
[email protected]a1257b12009-06-12 02:51:341477 external_extension_providers_[Extension::EXTERNAL_PREF] =
[email protected]da50530a2009-06-15 17:43:011478 linked_ptr<ExternalExtensionProvider>(
[email protected]27b985d2009-06-25 17:53:151479 new ExternalPrefExtensionProvider());
[email protected]a1257b12009-06-12 02:51:341480#if defined(OS_WIN)
1481 external_extension_providers_[Extension::EXTERNAL_REGISTRY] =
[email protected]da50530a2009-06-15 17:43:011482 linked_ptr<ExternalExtensionProvider>(
1483 new ExternalRegistryExtensionProvider());
[email protected]a1257b12009-06-12 02:51:341484#endif
1485}
1486
1487ExtensionsServiceBackend::~ExtensionsServiceBackend() {
[email protected]894bb502009-05-21 22:39:571488}
1489
[email protected]b0beaa662009-02-26 00:04:151490void ExtensionsServiceBackend::LoadSingleExtension(
[email protected]894bb502009-05-21 22:39:571491 const FilePath& path_in, scoped_refptr<ExtensionsService> frontend) {
[email protected]b0beaa662009-02-26 00:04:151492 frontend_ = frontend;
1493
1494 // Explicit UI loads are always noisy.
1495 alert_on_error_ = true;
1496
[email protected]cc5da332009-03-04 08:02:511497 FilePath extension_path = path_in;
[email protected]f36fa4fb2009-06-19 18:23:501498 file_util::AbsolutePath(&extension_path);
[email protected]bf24d2c2009-02-24 23:07:451499
1500 LOG(INFO) << "Loading single extension from " <<
[email protected]99efb7b12009-12-18 02:39:161501 extension_path.BaseName().value();
[email protected]bf24d2c2009-02-24 23:07:451502
[email protected]ab6f2b22009-07-28 23:28:371503 std::string error;
1504 Extension* extension = extension_file_util::LoadExtension(
1505 extension_path,
1506 false, // Don't require id
1507 &error);
1508
1509 if (!extension) {
1510 ReportExtensionLoadError(extension_path, error);
1511 return;
[email protected]0877fd92009-02-03 16:34:061512 }
[email protected]ab6f2b22009-07-28 23:28:371513
1514 extension->set_location(Extension::LOAD);
[email protected]e8c729a2010-03-09 19:55:191515
1516 // Report this as an installed extension so that it gets remembered in the
1517 // prefs.
1518 ChromeThread::PostTask(
1519 ChromeThread::UI, FROM_HERE,
1520 NewRunnableMethod(frontend_, &ExtensionsService::OnExtensionInstalled,
1521 extension, true));
[email protected]0877fd92009-02-03 16:34:061522}
1523
[email protected]6014d672008-12-05 00:38:251524void ExtensionsServiceBackend::ReportExtensionLoadError(
[email protected]cc5da332009-03-04 08:02:511525 const FilePath& extension_path, const std::string &error) {
[email protected]95d29192009-10-30 01:49:061526 ChromeThread::PostTask(
1527 ChromeThread::UI, FROM_HERE,
1528 NewRunnableMethod(
1529 frontend_,
[email protected]d11c8e92009-10-20 23:26:401530 &ExtensionsService::ReportExtensionLoadError, extension_path,
1531 error, NotificationType::EXTENSION_INSTALL_ERROR, alert_on_error_));
[email protected]6014d672008-12-05 00:38:251532}
1533
[email protected]a1257b12009-06-12 02:51:341534bool ExtensionsServiceBackend::LookupExternalExtension(
1535 const std::string& id, Version** version, Extension::Location* location) {
1536 scoped_ptr<Version> extension_version;
1537 for (ProviderMap::const_iterator i = external_extension_providers_.begin();
1538 i != external_extension_providers_.end(); ++i) {
[email protected]da50530a2009-06-15 17:43:011539 const ExternalExtensionProvider* provider = i->second.get();
[email protected]a1257b12009-06-12 02:51:341540 extension_version.reset(provider->RegisteredVersion(id, location));
1541 if (extension_version.get()) {
1542 if (version)
1543 *version = extension_version.release();
1544 return true;
1545 }
1546 }
1547 return false;
1548}
1549
[email protected]b0beaa662009-02-26 00:04:151550// Some extensions will autoupdate themselves externally from Chrome. These
1551// are typically part of some larger client application package. To support
[email protected]25b34332009-06-05 21:53:191552// these, the extension will register its location in the the preferences file
1553// (and also, on Windows, in the registry) and this code will periodically
[email protected]b0beaa662009-02-26 00:04:151554// check that location for a .crx file, which it will then install locally if
1555// a new version is available.
1556void ExtensionsServiceBackend::CheckForExternalUpdates(
[email protected]894bb502009-05-21 22:39:571557 std::set<std::string> ids_to_ignore,
1558 scoped_refptr<ExtensionsService> frontend) {
[email protected]b0beaa662009-02-26 00:04:151559 // Note that this installation is intentionally silent (since it didn't
1560 // go through the front-end). Extensions that are registered in this
1561 // way are effectively considered 'pre-bundled', and so implicitly
1562 // trusted. In general, if something has HKLM or filesystem access,
1563 // they could install an extension manually themselves anyway.
1564 alert_on_error_ = false;
1565 frontend_ = frontend;
[email protected]8ef78fd2010-08-19 17:14:321566 external_extension_added_ = false;
[email protected]b0beaa662009-02-26 00:04:151567
[email protected]a1257b12009-06-12 02:51:341568 // Ask each external extension provider to give us a call back for each
[email protected]8ef78fd2010-08-19 17:14:321569 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
1570
[email protected]a1257b12009-06-12 02:51:341571 for (ProviderMap::const_iterator i = external_extension_providers_.begin();
1572 i != external_extension_providers_.end(); ++i) {
[email protected]da50530a2009-06-15 17:43:011573 ExternalExtensionProvider* provider = i->second.get();
[email protected]a1257b12009-06-12 02:51:341574 provider->VisitRegisteredExtension(this, ids_to_ignore);
[email protected]25b34332009-06-05 21:53:191575 }
[email protected]8ef78fd2010-08-19 17:14:321576
1577 if (external_extension_added_ && frontend->updater()) {
1578 ChromeThread::PostTask(
1579 ChromeThread::UI, FROM_HERE,
1580 NewRunnableMethod(
1581 frontend->updater(), &ExtensionUpdater::CheckNow));
1582 }
[email protected]b0beaa662009-02-26 00:04:151583}
1584
[email protected]ae09ca62009-08-21 19:46:461585void ExtensionsServiceBackend::CheckExternalUninstall(
1586 scoped_refptr<ExtensionsService> frontend, const std::string& id,
1587 Extension::Location location) {
[email protected]a1257b12009-06-12 02:51:341588 // Check if the providers know about this extension.
1589 ProviderMap::const_iterator i = external_extension_providers_.find(location);
[email protected]ae09ca62009-08-21 19:46:461590 if (i == external_extension_providers_.end()) {
1591 NOTREACHED() << "CheckExternalUninstall called for non-external extension "
1592 << location;
1593 return;
[email protected]b0beaa662009-02-26 00:04:151594 }
[email protected]25b34332009-06-05 21:53:191595
[email protected]ae09ca62009-08-21 19:46:461596 scoped_ptr<Version> version;
1597 version.reset(i->second->RegisteredVersion(id, NULL));
1598 if (version.get())
1599 return; // Yup, known extension, don't uninstall.
1600
1601 // This is an external extension that we don't have registered. Uninstall.
[email protected]95d29192009-10-30 01:49:061602 ChromeThread::PostTask(
1603 ChromeThread::UI, FROM_HERE,
1604 NewRunnableMethod(
1605 frontend.get(), &ExtensionsService::UninstallExtension, id, true));
[email protected]b0beaa662009-02-26 00:04:151606}
1607
[email protected]a1257b12009-06-12 02:51:341608void ExtensionsServiceBackend::ClearProvidersForTesting() {
1609 external_extension_providers_.clear();
1610}
1611
1612void ExtensionsServiceBackend::SetProviderForTesting(
1613 Extension::Location location,
1614 ExternalExtensionProvider* test_provider) {
1615 DCHECK(test_provider);
[email protected]da50530a2009-06-15 17:43:011616 external_extension_providers_[location] =
1617 linked_ptr<ExternalExtensionProvider>(test_provider);
[email protected]a1257b12009-06-12 02:51:341618}
1619
[email protected]8ef78fd2010-08-19 17:14:321620void ExtensionsServiceBackend::OnExternalExtensionFileFound(
[email protected]7577a5c52009-07-30 06:21:581621 const std::string& id, const Version* version, const FilePath& path,
1622 Extension::Location location) {
[email protected]95d29192009-10-30 01:49:061623 ChromeThread::PostTask(
1624 ChromeThread::UI, FROM_HERE,
1625 NewRunnableMethod(
[email protected]8ef78fd2010-08-19 17:14:321626 frontend_, &ExtensionsService::OnExternalExtensionFileFound, id,
[email protected]95d29192009-10-30 01:49:061627 version->GetString(), path, location));
[email protected]cc655912009-01-29 23:19:191628}
[email protected]c6d474f82009-12-16 21:11:061629
[email protected]8ef78fd2010-08-19 17:14:321630void ExtensionsServiceBackend::OnExternalExtensionUpdateUrlFound(
1631 const std::string& id, const GURL& update_url) {
1632 if (frontend_->GetExtensionById(id, true)) {
1633 // Already installed. Do not change the update URL that the extension set.
1634 return;
1635 }
1636
1637 frontend_->AddPendingExtensionFromExternalUpdateUrl(id, update_url);
1638 external_extension_added_ |= true;
1639}
1640
[email protected]2111b1a2010-03-12 18:12:441641void ExtensionsServiceBackend::ReloadExtensionManifests(
[email protected]c6d474f82009-12-16 21:11:061642 ExtensionPrefs::ExtensionsInfo* extensions_to_reload,
1643 base::TimeTicks start_time,
1644 scoped_refptr<ExtensionsService> frontend) {
1645 frontend_ = frontend;
1646
1647 for (size_t i = 0; i < extensions_to_reload->size(); ++i) {
1648 ExtensionInfo* info = extensions_to_reload->at(i).get();
[email protected]2111b1a2010-03-12 18:12:441649 if (!ShouldReloadExtensionManifest(*info))
[email protected]c6d474f82009-12-16 21:11:061650 continue;
1651
1652 // We need to reload original manifest in order to localize properly.
1653 std::string error;
1654 scoped_ptr<Extension> extension(extension_file_util::LoadExtension(
1655 info->extension_path, false, &error));
1656
1657 if (extension.get())
1658 extensions_to_reload->at(i)->extension_manifest.reset(
1659 static_cast<DictionaryValue*>(
1660 extension->manifest_value()->DeepCopy()));
1661 }
1662
1663 // Finish installing on UI thread.
1664 ChromeThread::PostTask(
1665 ChromeThread::UI, FROM_HERE,
1666 NewRunnableMethod(
1667 frontend_,
1668 &ExtensionsService::ContinueLoadAllExtensions,
1669 extensions_to_reload,
1670 start_time,
1671 true));
1672}