blob: 634926744e565a81ce2e5023172641c52bee1a75 [file] [log] [blame]
[email protected]8e4560b62011-01-14 10:09:141// Copyright (c) 2011 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]e2eb43112009-05-29 21:19:5411#include "base/command_line.h"
[email protected]6014d672008-12-05 00:38:2512#include "base/file_util.h"
[email protected]835d7c82010-10-14 04:38:3813#include "base/metrics/histogram.h"
[email protected]cd500f72010-06-25 23:44:3214#include "base/stl_util-inl.h"
[email protected]24b538a2010-02-27 01:22:4415#include "base/string16.h"
[email protected]e83326f2010-07-31 17:29:2516#include "base/string_number_conversions.h"
[email protected]6014d672008-12-05 00:38:2517#include "base/string_util.h"
[email protected]18d4b6c2010-09-21 03:21:0418#include "base/stringprintf.h"
[email protected]34b99632011-01-01 01:01:0619#include "base/threading/thread_restrictions.h"
[email protected]cc2c3432009-11-06 17:24:3620#include "base/time.h"
[email protected]ce7f62e32010-08-10 23:43:5921#include "base/utf_string_conversions.h"
[email protected]cc655912009-01-29 23:19:1922#include "base/values.h"
[email protected]aa142702010-03-26 01:26:3323#include "base/version.h"
[email protected]15730c42009-09-03 00:03:2024#include "chrome/browser/browser_process.h"
[email protected]4814b512009-11-07 00:12:2925#include "chrome/browser/debugger/devtools_manager.h"
[email protected]7577a5c52009-07-30 06:21:5826#include "chrome/browser/extensions/crx_installer.h"
[email protected]ec5b50d2010-10-09 16:35:1827#include "chrome/browser/extensions/default_apps.h"
[email protected]5cbe1e22010-01-30 01:18:5628#include "chrome/browser/extensions/extension_accessibility_api.h"
[email protected]840b0db2009-11-20 03:00:3829#include "chrome/browser/extensions/extension_bookmarks_module.h"
[email protected]b68d5ed2009-04-16 02:41:2830#include "chrome/browser/extensions/extension_browser_event_router.h"
[email protected]2c5e1e12010-06-10 13:14:4431#include "chrome/browser/extensions/extension_cookies_api.h"
[email protected]c10da4b02010-03-25 14:38:3232#include "chrome/browser/extensions/extension_data_deleter.h"
[email protected]14a000d2010-04-29 21:44:2433#include "chrome/browser/extensions/extension_error_reporter.h"
[email protected]de768a832009-10-30 05:25:0134#include "chrome/browser/extensions/extension_history_api.h"
[email protected]b1748b1d82009-11-30 20:32:5635#include "chrome/browser/extensions/extension_host.h"
[email protected]7596ce72010-08-30 05:10:4636#include "chrome/browser/extensions/extension_management_api.h"
[email protected]118de7892011-03-16 13:31:4037#include "chrome/browser/extensions/extension_preference_api.h"
[email protected]4814b512009-11-07 00:12:2938#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]8a661f82010-10-19 21:47:1139#include "chrome/browser/extensions/extension_processes_api.h"
[email protected]19eb80152011-02-26 00:28:4340#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]93fd78f42009-07-10 16:43:1741#include "chrome/browser/extensions/extension_updater.h"
[email protected]8f9d4eb2011-02-05 01:39:1042#include "chrome/browser/extensions/extension_web_ui.h"
[email protected]784688a62010-09-13 07:06:5243#include "chrome/browser/extensions/extension_webnavigation_api.h"
[email protected]8e4560b62011-01-14 10:09:1444#include "chrome/browser/extensions/external_extension_provider_impl.h"
[email protected]42b795652011-02-22 23:32:3845#include "chrome/browser/extensions/external_extension_provider_interface.h"
[email protected]56ad3792010-05-28 17:45:3346#include "chrome/browser/net/chrome_url_request_context.h"
[email protected]37858e52010-08-26 00:22:0247#include "chrome/browser/prefs/pref_service.h"
[email protected]8ecad5e2010-12-02 21:18:3348#include "chrome/browser/profiles/profile.h"
[email protected]56ad3792010-05-28 17:45:3349#include "chrome/browser/search_engines/template_url_model.h"
[email protected]18280372011-03-22 18:05:2250#include "chrome/browser/themes/theme_service.h"
[email protected]a0ea76c2011-03-23 17:36:4251#include "chrome/browser/themes/theme_service_factory.h"
[email protected]fd42ac30f2011-02-27 19:33:3652#include "chrome/browser/ui/webui/shown_sections_handler.h"
[email protected]aab98a52009-12-02 03:22:3553#include "chrome/common/child_process_logging.h"
[email protected]e2eb43112009-05-29 21:19:5454#include "chrome/common/chrome_switches.h"
[email protected]5b1a0e22009-05-26 19:00:5855#include "chrome/common/extensions/extension.h"
[email protected]d7b36dc2009-10-29 21:47:4056#include "chrome/common/extensions/extension_constants.h"
[email protected]05c82182010-06-24 17:49:0857#include "chrome/common/extensions/extension_error_utils.h"
[email protected]7c927b62010-02-24 09:54:1358#include "chrome/common/extensions/extension_file_util.h"
[email protected]c6d474f82009-12-16 21:11:0659#include "chrome/common/extensions/extension_l10n_util.h"
[email protected]a315ba92010-11-16 14:12:2160#include "chrome/common/extensions/extension_resource.h"
[email protected]42b795652011-02-22 23:32:3861#include "chrome/common/json_value_serializer.h"
[email protected]25b34332009-06-05 21:53:1962#include "chrome/common/pref_names.h"
[email protected]a57209872009-05-04 22:53:1463#include "chrome/common/url_constants.h"
[email protected]5f945a0e2011-03-01 17:47:5364#include "content/browser/browser_thread.h"
[email protected]b3841c502011-03-09 01:21:3165#include "content/common/notification_service.h"
66#include "content/common/notification_type.h"
[email protected]c10da4b02010-03-25 14:38:3267#include "googleurl/src/gurl.h"
[email protected]1debbbb62010-10-06 17:23:4468#include "net/base/registry_controlled_domain.h"
[email protected]24b538a2010-02-27 01:22:4469#include "webkit/database/database_tracker.h"
70#include "webkit/database/database_util.h"
[email protected]c64631652009-04-29 22:24:3171
[email protected]5ef47ec2010-01-28 05:58:0572using base::Time;
73
[email protected]c6d474f82009-12-16 21:11:0674namespace errors = extension_manifest_errors;
75
[email protected]b6ab96d2009-08-20 18:58:1976namespace {
77
[email protected]29d0d4ac2010-09-08 21:10:3178#if defined(OS_LINUX)
79static const int kOmniboxIconPaddingLeft = 2;
80static const int kOmniboxIconPaddingRight = 2;
81#elif defined(OS_MACOSX)
82static const int kOmniboxIconPaddingLeft = 0;
83static const int kOmniboxIconPaddingRight = 2;
84#else
85static const int kOmniboxIconPaddingLeft = 0;
86static const int kOmniboxIconPaddingRight = 0;
87#endif
88
[email protected]a315ba92010-11-16 14:12:2189// The following enumeration is used in histograms matching
90// Extensions.ManifestReload* . Values may be added, as long
91// as existing values are not changed.
92enum ManifestReloadReason {
93 NOT_NEEDED = 0, // Reload not needed.
94 UNPACKED_DIR, // Unpacked directory
95 NEEDS_RELOCALIZATION, // The local has changed since we read this extension.
96 NUM_MANIFEST_RELOAD_REASONS
97};
[email protected]2111b1a2010-03-12 18:12:4498
[email protected]a315ba92010-11-16 14:12:2199ManifestReloadReason ShouldReloadExtensionManifest(const ExtensionInfo& info) {
100 // Always reload manifests of unpacked extensions, because they can change
101 // on disk independent of the manifest in our prefs.
102 if (info.extension_location == Extension::LOAD)
103 return UNPACKED_DIR;
104
105 // Reload the manifest if it needs to be relocalized.
106 if (extension_l10n_util::ShouldRelocalizeManifest(info))
107 return NEEDS_RELOCALIZATION;
108
109 return NOT_NEEDED;
[email protected]2111b1a2010-03-12 18:12:44110}
111
[email protected]c6d474f82009-12-16 21:11:06112} // namespace
[email protected]b6ab96d2009-08-20 18:58:19113
[email protected]d7e9a862010-11-03 21:57:49114
[email protected]eaa7dd182010-12-14 11:09:00115ExtensionService::ExtensionRuntimeData::ExtensionRuntimeData()
[email protected]d7e9a862010-11-03 21:57:49116 : background_page_ready(false),
117 being_upgraded(false) {
118}
119
[email protected]eaa7dd182010-12-14 11:09:00120ExtensionService::ExtensionRuntimeData::~ExtensionRuntimeData() {
[email protected]d7e9a862010-11-03 21:57:49121}
122
[email protected]eaa7dd182010-12-14 11:09:00123// ExtensionService.
[email protected]6014d672008-12-05 00:38:25124
[email protected]eaa7dd182010-12-14 11:09:00125const char* ExtensionService::kInstallDirectoryName = "Extensions";
126const char* ExtensionService::kCurrentVersionFileName = "Current Version";
[email protected]494c06e2009-07-25 01:06:42127
[email protected]eaa7dd182010-12-14 11:09:00128// Implements IO for the ExtensionService.
[email protected]7a4c6852010-09-16 03:44:22129
[email protected]eaa7dd182010-12-14 11:09:00130class ExtensionServiceBackend
[email protected]8e4560b62011-01-14 10:09:14131 : public base::RefCountedThreadSafe<ExtensionServiceBackend> {
[email protected]7a4c6852010-09-16 03:44:22132 public:
[email protected]1f830eb2010-09-28 08:25:14133 // |install_directory| is a path where to look for extensions to load.
[email protected]8e4560b62011-01-14 10:09:14134 explicit ExtensionServiceBackend(const FilePath& install_directory);
[email protected]7a4c6852010-09-16 03:44:22135
136 // Loads a single extension from |path| where |path| is the top directory of
137 // a specific extension where its manifest file lives.
138 // Errors are reported through ExtensionErrorReporter. On success,
[email protected]49cd1572011-02-08 21:38:45139 // AddExtension() is called.
[email protected]7a4c6852010-09-16 03:44:22140 // TODO(erikkay): It might be useful to be able to load a packed extension
141 // (presumably into memory) without installing it.
142 void LoadSingleExtension(const FilePath &path,
[email protected]eaa7dd182010-12-14 11:09:00143 scoped_refptr<ExtensionService> frontend);
[email protected]7a4c6852010-09-16 03:44:22144
[email protected]7a4c6852010-09-16 03:44:22145 private:
[email protected]eaa7dd182010-12-14 11:09:00146 friend class base::RefCountedThreadSafe<ExtensionServiceBackend>;
[email protected]7a4c6852010-09-16 03:44:22147
[email protected]eaa7dd182010-12-14 11:09:00148 virtual ~ExtensionServiceBackend();
[email protected]7a4c6852010-09-16 03:44:22149
150 // Finish installing the extension in |crx_path| after it has been unpacked to
151 // |unpacked_path|. If |expected_id| is not empty, it's verified against the
152 // extension's manifest before installation. If |silent| is true, there will
153 // be no install confirmation dialog. |from_gallery| indicates whether the
154 // crx was installed from our gallery, which results in different UI.
155 //
156 // Note: We take ownership of |extension|.
157 void OnExtensionUnpacked(const FilePath& crx_path,
158 const FilePath& unpacked_path,
[email protected]9adb9692010-10-29 23:14:02159 const Extension* extension,
[email protected]7a4c6852010-09-16 03:44:22160 const std::string expected_id);
161
162 // Notify the frontend that there was an error loading an extension.
163 void ReportExtensionLoadError(const FilePath& extension_path,
164 const std::string& error);
165
[email protected]7a4c6852010-09-16 03:44:22166 // This is a naked pointer which is set by each entry point.
167 // The entry point is responsible for ensuring lifetime.
[email protected]eaa7dd182010-12-14 11:09:00168 ExtensionService* frontend_;
[email protected]7a4c6852010-09-16 03:44:22169
170 // The top-level extensions directory being installed to.
171 FilePath install_directory_;
172
173 // Whether errors result in noisy alerts.
174 bool alert_on_error_;
175
[email protected]eaa7dd182010-12-14 11:09:00176 DISALLOW_COPY_AND_ASSIGN(ExtensionServiceBackend);
[email protected]7a4c6852010-09-16 03:44:22177};
178
[email protected]eaa7dd182010-12-14 11:09:00179ExtensionServiceBackend::ExtensionServiceBackend(
[email protected]aebe23a32010-12-10 22:15:48180 const FilePath& install_directory)
[email protected]7a4c6852010-09-16 03:44:22181 : frontend_(NULL),
182 install_directory_(install_directory),
[email protected]8e4560b62011-01-14 10:09:14183 alert_on_error_(false) {
[email protected]7a4c6852010-09-16 03:44:22184}
185
[email protected]eaa7dd182010-12-14 11:09:00186ExtensionServiceBackend::~ExtensionServiceBackend() {
[email protected]7a4c6852010-09-16 03:44:22187}
188
[email protected]eaa7dd182010-12-14 11:09:00189void ExtensionServiceBackend::LoadSingleExtension(
190 const FilePath& path_in, scoped_refptr<ExtensionService> frontend) {
[email protected]a29a517a2011-01-21 21:11:12191 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]a8af9fdb2010-10-28 21:52:20192
[email protected]7a4c6852010-09-16 03:44:22193 frontend_ = frontend;
194
195 // Explicit UI loads are always noisy.
196 alert_on_error_ = true;
197
198 FilePath extension_path = path_in;
199 file_util::AbsolutePath(&extension_path);
200
[email protected]7a4c6852010-09-16 03:44:22201 std::string error;
[email protected]ad8e04a2010-11-01 04:16:27202 scoped_refptr<const Extension> extension(extension_file_util::LoadExtension(
[email protected]7a4c6852010-09-16 03:44:22203 extension_path,
[email protected]92888082010-10-18 19:24:57204 Extension::LOAD,
[email protected]7a4c6852010-09-16 03:44:22205 false, // Don't require id
[email protected]542258c2011-03-04 21:25:31206 Extension::ShouldDoStrictErrorChecking(Extension::LOAD),
[email protected]ad8e04a2010-11-01 04:16:27207 &error));
[email protected]7a4c6852010-09-16 03:44:22208
209 if (!extension) {
210 ReportExtensionLoadError(extension_path, error);
211 return;
212 }
213
[email protected]7a4c6852010-09-16 03:44:22214 // Report this as an installed extension so that it gets remembered in the
215 // prefs.
[email protected]ca4b5fa32010-10-09 12:42:18216 BrowserThread::PostTask(
217 BrowserThread::UI, FROM_HERE,
[email protected]8d888c12010-11-30 00:00:25218 NewRunnableMethod(frontend_,
[email protected]eaa7dd182010-12-14 11:09:00219 &ExtensionService::OnExtensionInstalled,
[email protected]8d888c12010-11-30 00:00:25220 extension));
[email protected]7a4c6852010-09-16 03:44:22221}
222
[email protected]eaa7dd182010-12-14 11:09:00223void ExtensionServiceBackend::ReportExtensionLoadError(
[email protected]7a4c6852010-09-16 03:44:22224 const FilePath& extension_path, const std::string &error) {
[email protected]a29a517a2011-01-21 21:11:12225 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]ca4b5fa32010-10-09 12:42:18226 BrowserThread::PostTask(
227 BrowserThread::UI, FROM_HERE,
[email protected]7a4c6852010-09-16 03:44:22228 NewRunnableMethod(
229 frontend_,
[email protected]eaa7dd182010-12-14 11:09:00230 &ExtensionService::ReportExtensionLoadError, extension_path,
[email protected]7a4c6852010-09-16 03:44:22231 error, NotificationType::EXTENSION_INSTALL_ERROR, alert_on_error_));
232}
233
[email protected]8e4560b62011-01-14 10:09:14234void ExtensionService::CheckExternalUninstall(const std::string& id) {
[email protected]a29a517a2011-01-21 21:11:12235 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20236
[email protected]7a4c6852010-09-16 03:44:22237 // Check if the providers know about this extension.
[email protected]0a60a2e2010-10-25 16:15:21238 ProviderCollection::const_iterator i;
239 for (i = external_extension_providers_.begin();
240 i != external_extension_providers_.end(); ++i) {
[email protected]8e4560b62011-01-14 10:09:14241 DCHECK(i->get()->IsReady());
[email protected]0a60a2e2010-10-25 16:15:21242 if (i->get()->HasExtension(id))
243 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22244 }
245
[email protected]7a4c6852010-09-16 03:44:22246 // This is an external extension that we don't have registered. Uninstall.
[email protected]8e4560b62011-01-14 10:09:14247 UninstallExtension(id, true);
[email protected]7a4c6852010-09-16 03:44:22248}
249
[email protected]8e4560b62011-01-14 10:09:14250void ExtensionService::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22251 external_extension_providers_.clear();
252}
253
[email protected]8e4560b62011-01-14 10:09:14254void ExtensionService::AddProviderForTesting(
255 ExternalExtensionProviderInterface* test_provider) {
[email protected]a29a517a2011-01-21 21:11:12256 CHECK(test_provider);
[email protected]0a60a2e2010-10-25 16:15:21257 external_extension_providers_.push_back(
[email protected]8e4560b62011-01-14 10:09:14258 linked_ptr<ExternalExtensionProviderInterface>(test_provider));
[email protected]7a4c6852010-09-16 03:44:22259}
260
[email protected]8e4560b62011-01-14 10:09:14261void ExtensionService::OnExternalExtensionUpdateUrlFound(
[email protected]7a4c6852010-09-16 03:44:22262 const std::string& id,
[email protected]21a5a672010-11-04 10:47:42263 const GURL& update_url,
264 Extension::Location location) {
[email protected]ab22ba42011-01-14 16:36:38265 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
266 CHECK(Extension::IdIsValid(id));
[email protected]a8af9fdb2010-10-28 21:52:20267
[email protected]8e4560b62011-01-14 10:09:14268 if (GetExtensionById(id, true)) {
[email protected]7a4c6852010-09-16 03:44:22269 // Already installed. Do not change the update URL that the extension set.
270 return;
271 }
[email protected]8e4560b62011-01-14 10:09:14272 AddPendingExtensionFromExternalUpdateUrl(id, update_url, location);
[email protected]a29a517a2011-01-21 21:11:12273 external_extension_url_added_ |= true;
[email protected]7a4c6852010-09-16 03:44:22274}
275
[email protected]eaa7dd182010-12-14 11:09:00276bool ExtensionService::IsDownloadFromGallery(const GURL& download_url,
[email protected]a29a517a2011-01-21 21:11:12277 const GURL& referrer_url) {
[email protected]d3071992010-10-08 15:24:07278 // Special-case the themes mini-gallery.
279 // TODO(erikkay) When that gallery goes away, remove this code.
280 if (IsDownloadFromMiniGallery(download_url) &&
281 StartsWithASCII(referrer_url.spec(),
282 extension_urls::kMiniGalleryBrowsePrefix, false)) {
[email protected]334e04a2010-06-24 23:34:44283 return true;
[email protected]1debbbb62010-10-06 17:23:44284 }
[email protected]473ff6e2010-05-12 15:31:55285
[email protected]9adb9692010-10-29 23:14:02286 const Extension* download_extension = GetExtensionByWebExtent(download_url);
287 const Extension* referrer_extension = GetExtensionByWebExtent(referrer_url);
288 const Extension* webstore_app = GetWebStoreApp();
[email protected]d3071992010-10-08 15:24:07289
290 bool referrer_valid = (referrer_extension == webstore_app);
291 bool download_valid = (download_extension == webstore_app);
292
293 // If the command-line gallery URL is set, then be a bit more lenient.
294 GURL store_url =
295 GURL(CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
296 switches::kAppsGalleryURL));
297 if (!store_url.is_empty()) {
298 std::string store_tld =
299 net::RegistryControlledDomainService::GetDomainAndRegistry(store_url);
300 if (!referrer_valid) {
301 std::string referrer_tld =
302 net::RegistryControlledDomainService::GetDomainAndRegistry(
303 referrer_url);
304 // The referrer gets stripped when transitioning from https to http,
305 // or when hitting an unknown test cert and that commonly happens in
306 // testing environments. Given this, we allow an empty referrer when
307 // the command-line flag is set.
308 // Otherwise, the TLD must match the TLD of the command-line url.
309 referrer_valid = referrer_url.is_empty() || (referrer_tld == store_tld);
310 }
311
312 if (!download_valid) {
313 std::string download_tld =
314 net::RegistryControlledDomainService::GetDomainAndRegistry(
315 GURL(download_url));
316
317 // Otherwise, the TLD must match the TLD of the command-line url.
318 download_valid = (download_tld == store_tld);
319 }
320 }
321
322 return (referrer_valid && download_valid);
[email protected]b7c2f252009-12-08 00:47:23323}
324
[email protected]eaa7dd182010-12-14 11:09:00325bool ExtensionService::IsDownloadFromMiniGallery(const GURL& download_url) {
[email protected]ac025282009-12-16 19:16:38326 return StartsWithASCII(download_url.spec(),
327 extension_urls::kMiniGalleryDownloadPrefix,
328 false); // case_sensitive
329}
330
[email protected]d9696672011-03-15 22:45:09331const Extension* ExtensionService::GetInstalledApp(const GURL& url) {
[email protected]ffb204f22010-12-05 23:20:27332 // Check for hosted app.
[email protected]d9696672011-03-15 22:45:09333 const Extension* app = GetExtensionByWebExtent(url);
334 if (app)
335 return app;
[email protected]ffb204f22010-12-05 23:20:27336
337 // Check for packaged app.
[email protected]d9696672011-03-15 22:45:09338 app = GetExtensionByURL(url);
339 if (app && app->is_app())
340 return app;
341
342 return NULL;
343}
344
345bool ExtensionService::IsInstalledApp(const GURL& url) {
346 return !!GetInstalledApp(url);
[email protected]ffb204f22010-12-05 23:20:27347}
348
[email protected]6aeac8342010-10-01 20:21:18349// static
[email protected]eaa7dd182010-12-14 11:09:00350bool ExtensionService::UninstallExtensionHelper(
351 ExtensionService* extensions_service,
[email protected]6aeac8342010-10-01 20:21:18352 const std::string& extension_id) {
[email protected]6aeac8342010-10-01 20:21:18353
354 // We can't call UninstallExtension with an invalid extension ID, so check it
355 // first.
[email protected]0dfe05c2011-02-23 23:03:36356 if (extensions_service->GetExtensionById(extension_id, true) ||
357 extensions_service->GetTerminatedExtension(extension_id)) {
[email protected]6aeac8342010-10-01 20:21:18358 extensions_service->UninstallExtension(extension_id, false);
359 } else {
360 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]a8af9fdb2010-10-28 21:52:20361 << "id: " << extension_id;
[email protected]6aeac8342010-10-01 20:21:18362 return false;
363 }
364
365 return true;
366}
367
[email protected]eaa7dd182010-12-14 11:09:00368ExtensionService::ExtensionService(Profile* profile,
[email protected]f0841cd2011-01-19 15:07:24369 const CommandLine* command_line,
370 const FilePath& install_directory,
371 ExtensionPrefs* extension_prefs,
372 bool autoupdate_enabled)
[email protected]6ef635e42009-07-26 06:16:12373 : profile_(profile),
[email protected]73c47932010-12-06 18:13:43374 extension_prefs_(extension_prefs),
[email protected]a9b00ac2009-06-25 21:03:23375 install_directory_(install_directory),
[email protected]6d60703b2009-08-29 01:29:23376 extensions_enabled_(true),
[email protected]e81dba32009-06-19 20:19:13377 show_extensions_prompts_(true),
[email protected]3ecda252010-11-18 19:50:55378 ready_(false),
[email protected]ec5b50d2010-10-09 16:35:18379 ALLOW_THIS_IN_INITIALIZER_LIST(toolbar_model_(this)),
[email protected]2fc0f122011-01-04 23:44:30380 default_apps_(profile->GetPrefs(),
381 g_browser_process->GetApplicationLocale()),
[email protected]b3d62312b12010-10-14 21:10:18382 event_routers_initialized_(false) {
[email protected]a29a517a2011-01-21 21:11:12383 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20384
[email protected]36a784c2009-06-23 06:21:08385 // Figure out if extension installation should be enabled.
[email protected]6d60703b2009-08-29 01:29:23386 if (command_line->HasSwitch(switches::kDisableExtensions)) {
387 extensions_enabled_ = false;
388 } else if (profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions)) {
389 extensions_enabled_ = false;
[email protected]6b75ec32009-08-14 06:37:18390 }
[email protected]36a784c2009-06-23 06:21:08391
[email protected]a4ed6282009-12-14 20:51:16392 registrar_.Add(this, NotificationType::EXTENSION_PROCESS_TERMINATED,
[email protected]bc535ee52010-08-31 18:40:32393 NotificationService::AllSources());
[email protected]2fb7dc982010-09-29 12:24:28394 pref_change_registrar_.Init(profile->GetPrefs());
395 pref_change_registrar_.Add(prefs::kExtensionInstallAllowList, this);
396 pref_change_registrar_.Add(prefs::kExtensionInstallDenyList, this);
[email protected]4814b512009-11-07 00:12:29397
[email protected]93fd78f42009-07-10 16:43:17398 // Set up the ExtensionUpdater
399 if (autoupdate_enabled) {
400 int update_frequency = kDefaultUpdateFrequencySeconds;
401 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25402 base::StringToInt(command_line->GetSwitchValueASCII(
403 switches::kExtensionsUpdateFrequency),
404 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17405 }
[email protected]2fb7dc982010-09-29 12:24:28406 updater_ = new ExtensionUpdater(this,
407 profile->GetPrefs(),
408 update_frequency);
[email protected]93fd78f42009-07-10 16:43:17409 }
410
[email protected]8e4560b62011-01-14 10:09:14411 backend_ = new ExtensionServiceBackend(install_directory_);
412
[email protected]873531342011-03-09 12:16:05413 if (extensions_enabled()) {
414 ExternalExtensionProviderImpl::CreateExternalProviders(
415 this, profile_, &external_extension_providers_);
416 }
[email protected]b671760b2010-07-15 21:13:47417
[email protected]aa96d3a2010-08-21 08:45:25418 // Use monochrome icons for Omnibox icons.
[email protected]29d0d4ac2010-09-08 21:10:31419 omnibox_popup_icon_manager_.set_monochrome(true);
[email protected]b671760b2010-07-15 21:13:47420 omnibox_icon_manager_.set_monochrome(true);
[email protected]29d0d4ac2010-09-08 21:10:31421 omnibox_icon_manager_.set_padding(gfx::Insets(0, kOmniboxIconPaddingLeft,
422 0, kOmniboxIconPaddingRight));
[email protected]6014d672008-12-05 00:38:25423}
424
[email protected]eaa7dd182010-12-14 11:09:00425const ExtensionList* ExtensionService::extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45426 return &extensions_;
427}
428
[email protected]eaa7dd182010-12-14 11:09:00429const ExtensionList* ExtensionService::disabled_extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45430 return &disabled_extensions_;
431}
432
[email protected]bb7f40952011-01-13 00:21:20433const ExtensionList* ExtensionService::terminated_extensions() const {
434 return &terminated_extensions_;
435}
436
[email protected]eaa7dd182010-12-14 11:09:00437const PendingExtensionMap& ExtensionService::pending_extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45438 return pending_extensions_;
439}
440
[email protected]eaa7dd182010-12-14 11:09:00441bool ExtensionService::HasInstalledExtensions() {
[email protected]ddb1e5a2010-12-13 20:10:45442 return !(extensions_.empty() && disabled_extensions_.empty());
443}
444
[email protected]eaa7dd182010-12-14 11:09:00445ExtensionService::~ExtensionService() {
[email protected]2fb7dc982010-09-29 12:24:28446 DCHECK(!profile_); // Profile should have told us it's going away.
[email protected]9f1087e2009-06-15 17:29:32447 UnloadAllExtensions();
[email protected]94b4ffc2011-02-04 05:55:17448
[email protected]8e4560b62011-01-14 10:09:14449 ProviderCollection::const_iterator i;
450 for (i = external_extension_providers_.begin();
451 i != external_extension_providers_.end(); ++i) {
452 ExternalExtensionProviderInterface* provider = i->get();
453 provider->ServiceShutdown();
454 }
[email protected]6014d672008-12-05 00:38:25455}
456
[email protected]eaa7dd182010-12-14 11:09:00457void ExtensionService::InitEventRouters() {
[email protected]b3d62312b12010-10-14 21:10:18458 if (event_routers_initialized_)
459 return;
460
[email protected]c5ae74ab2010-04-15 18:14:37461 ExtensionHistoryEventRouter::GetInstance()->ObserveProfile(profile_);
462 ExtensionAccessibilityEventRouter::GetInstance()->ObserveProfile(profile_);
[email protected]97d2f1d2011-01-15 00:41:08463 browser_event_router_.reset(new ExtensionBrowserEventRouter(profile_));
464 browser_event_router_->Init();
[email protected]118de7892011-03-16 13:31:40465 preference_event_router_.reset(new ExtensionPreferenceEventRouter(profile_));
[email protected]8e8bb6d2010-12-13 08:18:55466 ExtensionBookmarkEventRouter::GetInstance()->Observe(
[email protected]c5ae74ab2010-04-15 18:14:37467 profile_->GetBookmarkModel());
[email protected]2c5e1e12010-06-10 13:14:44468 ExtensionCookiesEventRouter::GetInstance()->Init();
[email protected]7596ce72010-08-30 05:10:46469 ExtensionManagementEventRouter::GetInstance()->Init();
[email protected]8a661f82010-10-19 21:47:11470 ExtensionProcessesEventRouter::GetInstance()->ObserveProfile(profile_);
[email protected]784688a62010-09-13 07:06:52471 ExtensionWebNavigationEventRouter::GetInstance()->Init();
[email protected]b3d62312b12010-10-14 21:10:18472 event_routers_initialized_ = true;
[email protected]c5ae74ab2010-04-15 18:14:37473}
474
[email protected]eaa7dd182010-12-14 11:09:00475const Extension* ExtensionService::GetExtensionById(const std::string& id,
[email protected]78994ab02010-12-08 18:06:44476 bool include_disabled) {
477 return GetExtensionByIdInternal(id, true, include_disabled);
478}
479
[email protected]eaa7dd182010-12-14 11:09:00480void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12481 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20482
[email protected]fa6a9102010-11-22 15:38:50483 DCHECK(!ready_); // Can't redo init.
[email protected]93fd78f42009-07-10 16:43:17484 DCHECK_EQ(extensions_.size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32485
[email protected]95dd38f2009-10-20 20:09:15486 // Hack: we need to ensure the ResourceDispatcherHost is ready before we load
487 // the first extension, because its members listen for loaded notifications.
488 g_browser_process->resource_dispatcher_host();
489
[email protected]9f1087e2009-06-15 17:29:32490 LoadAllExtensions();
[email protected]894bb502009-05-21 22:39:57491
[email protected]9f1087e2009-06-15 17:29:32492 // TODO(erikkay) this should probably be deferred to a future point
493 // rather than running immediately at startup.
[email protected]93fd78f42009-07-10 16:43:17494 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57495
[email protected]9f1087e2009-06-15 17:29:32496 // TODO(erikkay) this should probably be deferred as well.
497 GarbageCollectExtensions();
[email protected]6014d672008-12-05 00:38:25498}
499
[email protected]eaa7dd182010-12-14 11:09:00500void ExtensionService::UpdateExtension(const std::string& id,
[email protected]9a8c4022011-01-25 14:25:33501 const FilePath& extension_path,
502 const GURL& download_url) {
[email protected]a29a517a2011-01-21 21:11:12503 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20504
[email protected]aa142702010-03-26 01:26:33505 PendingExtensionMap::const_iterator it = pending_extensions_.find(id);
[email protected]8ef78fd2010-08-19 17:14:32506 bool is_pending_extension = (it != pending_extensions_.end());
507
[email protected]5eb375e92010-11-26 07:50:41508 const Extension* extension = GetExtensionByIdInternal(id, true, true);
509 if (!is_pending_extension && !extension) {
[email protected]aa142702010-03-26 01:26:33510 LOG(WARNING) << "Will not update extension " << id
511 << " because it is not installed or pending";
512 // Delete extension_path since we're not creating a CrxInstaller
513 // that would do it for us.
[email protected]ca4b5fa32010-10-09 12:42:18514 BrowserThread::PostTask(
515 BrowserThread::FILE, FROM_HERE,
[email protected]53da2c962011-03-03 17:08:05516 NewRunnableFunction(
517 extension_file_util::DeleteFile, extension_path, false));
[email protected]4c967932009-07-31 01:15:49518 return;
[email protected]e957fe52009-06-23 16:51:05519 }
520
[email protected]aa142702010-03-26 01:26:33521 // We want a silent install only for non-pending extensions and
522 // pending extensions that have install_silently set.
523 ExtensionInstallUI* client =
[email protected]483d1ff2011-03-03 17:12:02524 (!is_pending_extension || it->second.install_silently()) ?
[email protected]aa142702010-03-26 01:26:33525 NULL : new ExtensionInstallUI(profile_);
526
[email protected]6dfbbf82010-03-12 23:09:16527 scoped_refptr<CrxInstaller> installer(
[email protected]bb461532010-11-26 21:50:23528 new CrxInstaller(this, // frontend
[email protected]aa142702010-03-26 01:26:33529 client));
[email protected]6dfbbf82010-03-12 23:09:16530 installer->set_expected_id(id);
[email protected]ec5b50d2010-10-09 16:35:18531 if (is_pending_extension)
[email protected]483d1ff2011-03-03 17:12:02532 installer->set_install_source(it->second.install_source());
[email protected]5eb375e92010-11-26 07:50:41533 else if (extension)
534 installer->set_install_source(extension->location());
[email protected]6dfbbf82010-03-12 23:09:16535 installer->set_delete_source(true);
[email protected]5c8516202010-03-18 21:43:34536 installer->set_original_url(download_url);
[email protected]6dfbbf82010-03-12 23:09:16537 installer->InstallCrx(extension_path);
[email protected]e957fe52009-06-23 16:51:05538}
539
[email protected]eaa7dd182010-12-14 11:09:00540void ExtensionService::AddPendingExtensionFromSync(
[email protected]aa142702010-03-26 01:26:33541 const std::string& id, const GURL& update_url,
[email protected]483d1ff2011-03-03 17:12:02542 PendingExtensionInfo::ShouldAllowInstallPredicate should_allow_install,
[email protected]8ef78fd2010-08-19 17:14:32543 bool install_silently, bool enable_on_install,
544 bool enable_incognito_on_install) {
[email protected]aa142702010-03-26 01:26:33545 if (GetExtensionByIdInternal(id, true, true)) {
[email protected]efee9f262010-03-29 21:26:25546 LOG(DFATAL) << "Trying to add pending extension " << id
547 << " which already exists";
[email protected]aa142702010-03-26 01:26:33548 return;
549 }
[email protected]ec5b50d2010-10-09 16:35:18550
[email protected]483d1ff2011-03-03 17:12:02551 AddPendingExtensionInternal(id, update_url, should_allow_install, true,
[email protected]ec5b50d2010-10-09 16:35:18552 install_silently, enable_on_install,
553 enable_incognito_on_install,
554 Extension::INTERNAL);
[email protected]aa142702010-03-26 01:26:33555}
556
[email protected]7fa19f82010-12-21 19:40:08557namespace {
558
559bool AlwaysInstall(const Extension& extension) {
560 return true;
561}
562
563} // namespace
564
[email protected]eaa7dd182010-12-14 11:09:00565void ExtensionService::AddPendingExtensionFromExternalUpdateUrl(
[email protected]21a5a672010-11-04 10:47:42566 const std::string& id, const GURL& update_url,
567 Extension::Location location) {
[email protected]8ef78fd2010-08-19 17:14:32568 const bool kIsFromSync = false;
569 const bool kInstallSilently = true;
570 const bool kEnableOnInstall = true;
[email protected]a424d84c2010-09-24 09:31:15571 const bool kEnableIncognitoOnInstall = false;
[email protected]683d0702010-12-06 16:25:57572 if (extension_prefs_->IsExtensionKilled(id))
573 return;
[email protected]8ef78fd2010-08-19 17:14:32574
575 if (GetExtensionByIdInternal(id, true, true)) {
576 LOG(DFATAL) << "Trying to add extension " << id
577 << " by external update, but it is already installed.";
578 return;
579 }
580
[email protected]7fa19f82010-12-21 19:40:08581 AddPendingExtensionInternal(id, update_url, &AlwaysInstall,
582 kIsFromSync, kInstallSilently,
583 kEnableOnInstall, kEnableIncognitoOnInstall,
[email protected]21a5a672010-11-04 10:47:42584 location);
[email protected]ec5b50d2010-10-09 16:35:18585}
586
[email protected]7fa19f82010-12-21 19:40:08587namespace {
588
589bool IsApp(const Extension& extension) {
590 return extension.is_app();
591}
592
593} // namespace
594
595// TODO(akalin): Change DefaultAppList to DefaultExtensionList and
596// remove the IsApp() check.
597
[email protected]eaa7dd182010-12-14 11:09:00598void ExtensionService::AddPendingExtensionFromDefaultAppList(
[email protected]ec5b50d2010-10-09 16:35:18599 const std::string& id) {
[email protected]ec5b50d2010-10-09 16:35:18600 const bool kIsFromSync = false;
601 const bool kInstallSilently = true;
602 const bool kEnableOnInstall = true;
603 const bool kEnableIncognitoOnInstall = true;
604
605 // This can legitimately happen if the user manually installed one of the
606 // default apps before this code ran.
607 if (GetExtensionByIdInternal(id, true, true))
608 return;
609
[email protected]7fa19f82010-12-21 19:40:08610 AddPendingExtensionInternal(id, GURL(), &IsApp,
611 kIsFromSync, kInstallSilently,
612 kEnableOnInstall, kEnableIncognitoOnInstall,
[email protected]ec5b50d2010-10-09 16:35:18613 Extension::INTERNAL);
[email protected]8ef78fd2010-08-19 17:14:32614}
615
[email protected]eaa7dd182010-12-14 11:09:00616void ExtensionService::AddPendingExtensionInternal(
[email protected]aa142702010-03-26 01:26:33617 const std::string& id, const GURL& update_url,
[email protected]483d1ff2011-03-03 17:12:02618 PendingExtensionInfo::ShouldAllowInstallPredicate should_allow_install,
[email protected]8ef78fd2010-08-19 17:14:32619 bool is_from_sync, bool install_silently,
[email protected]ec5b50d2010-10-09 16:35:18620 bool enable_on_install, bool enable_incognito_on_install,
621 Extension::Location install_source) {
[email protected]a29a517a2011-01-21 21:11:12622 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20623
[email protected]1afaf2a52010-11-02 19:29:17624 // If a non-sync update is pending, a sync request should not
625 // overwrite it. This is important for external extensions.
626 // If an external extension download is pending, and the user has
627 // the extension in their sync profile, the install should set the
628 // type to be external. An external extension should not be
629 // rejected if it fails the safty checks for a syncable extension.
630 // TODO(skerner): Work out other potential overlapping conditions.
[email protected]a315ba92010-11-16 14:12:21631 // (crbug.com/61000)
[email protected]1afaf2a52010-11-02 19:29:17632 PendingExtensionMap::iterator it = pending_extensions_.find(id);
633 if (it != pending_extensions_.end()) {
634 VLOG(1) << "Extension id " << id
635 << " was entered for update more than once."
[email protected]483d1ff2011-03-03 17:12:02636 << " old is_from_sync = " << it->second.is_from_sync()
[email protected]1afaf2a52010-11-02 19:29:17637 << " new is_from_sync = " << is_from_sync;
[email protected]483d1ff2011-03-03 17:12:02638 if (!it->second.is_from_sync() && is_from_sync)
[email protected]1afaf2a52010-11-02 19:29:17639 return;
640 }
641
[email protected]aa142702010-03-26 01:26:33642 pending_extensions_[id] =
[email protected]483d1ff2011-03-03 17:12:02643 PendingExtensionInfo(update_url, should_allow_install,
[email protected]7fa19f82010-12-21 19:40:08644 is_from_sync, install_silently, enable_on_install,
[email protected]ec5b50d2010-10-09 16:35:18645 enable_incognito_on_install, install_source);
[email protected]aa142702010-03-26 01:26:33646}
647
[email protected]eaa7dd182010-12-14 11:09:00648void ExtensionService::ReloadExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12649 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]b65272f2009-08-31 15:47:06650 FilePath path;
[email protected]9adb9692010-10-29 23:14:02651 const Extension* current_extension = GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40652
[email protected]f17dbd42010-08-16 23:21:10653 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]b65272f2009-08-31 15:47:06654 if (current_extension) {
[email protected]4814b512009-11-07 00:12:29655 // If the extension has an inspector open for its background page, detach
656 // the inspector and hang onto a cookie for it, so that we can reattach
657 // later.
658 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
659 ExtensionHost* host = manager->GetBackgroundHostForExtension(
660 current_extension);
661 if (host) {
662 // Look for an open inspector for the background page.
663 int devtools_cookie = DevToolsManager::GetInstance()->DetachClientHost(
664 host->render_view_host());
665 if (devtools_cookie >= 0)
666 orphaned_dev_tools_[extension_id] = devtools_cookie;
667 }
668
[email protected]b65272f2009-08-31 15:47:06669 path = current_extension->path();
[email protected]f17dbd42010-08-16 23:21:10670 DisableExtension(extension_id);
671 disabled_extension_paths_[extension_id] = path;
[email protected]1eb175082010-02-10 09:26:16672 } else {
673 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06674 }
675
[email protected]e6090e42010-03-23 22:44:08676 // Check the installed extensions to see if what we're reloading was already
677 // installed.
678 scoped_ptr<ExtensionInfo> installed_extension(
679 extension_prefs_->GetInstalledExtensionInfo(extension_id));
680 if (installed_extension.get() &&
681 installed_extension->extension_manifest.get()) {
682 LoadInstalledExtension(*installed_extension, false);
683 } else {
684 // We should always be able to remember the extension's path. If it's not in
685 // the map, someone failed to update |unloaded_extension_paths_|.
686 CHECK(!path.empty());
687 LoadExtension(path);
688 }
[email protected]9cddd4702009-07-27 22:09:40689}
690
[email protected]eaa7dd182010-12-14 11:09:00691void ExtensionService::UninstallExtension(const std::string& extension_id,
[email protected]604322d2011-03-22 16:51:56692 bool external_uninstall) {
[email protected]a29a517a2011-01-21 21:11:12693 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20694
[email protected]9adb9692010-10-29 23:14:02695 const Extension* extension =
696 GetExtensionByIdInternal(extension_id, true, true);
[email protected]0dfe05c2011-02-23 23:03:36697 if (!extension)
698 extension = GetTerminatedExtension(extension_id);
[email protected]631cf822009-05-15 07:01:25699
[email protected]e7afe2452010-08-22 16:19:13700 // Callers should not send us nonexistent extensions.
[email protected]aebe23a32010-12-10 22:15:48701 CHECK(extension);
[email protected]9f1087e2009-06-15 17:29:32702
[email protected]831aa212010-03-26 13:55:19703 // Get hold of information we need after unloading, since the extension
704 // pointer will be invalid then.
705 GURL extension_url(extension->url());
706 Extension::Location location(extension->location());
[email protected]211030342010-09-30 18:41:06707 UninstalledExtensionInfo uninstalled_extension_info(*extension);
[email protected]831aa212010-03-26 13:55:19708
[email protected]9b217652010-10-08 22:04:23709 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08710 extension->GetType(), 100);
[email protected]9b217652010-10-08 22:04:23711
[email protected]831aa212010-03-26 13:55:19712 // Also copy the extension identifier since the reference might have been
713 // obtained via Extension::id().
714 std::string extension_id_copy(extension_id);
715
[email protected]56ad3792010-05-28 17:45:33716 if (profile_->GetTemplateURLModel())
717 profile_->GetTemplateURLModel()->UnregisterExtensionKeyword(extension);
718
[email protected]831aa212010-03-26 13:55:19719 // Unload before doing more cleanup to ensure that nothing is hanging on to
720 // any of these resources.
[email protected]a9f39a312010-12-23 22:14:27721 UnloadExtension(extension_id, UnloadedExtensionInfo::UNINSTALL);
[email protected]831aa212010-03-26 13:55:19722
723 extension_prefs_->OnExtensionUninstalled(extension_id_copy, location,
724 external_uninstall);
[email protected]9f1087e2009-06-15 17:29:32725
726 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]831aa212010-03-26 13:55:19727 if (Extension::LOAD != location) {
[email protected]ca4b5fa32010-10-09 12:42:18728 BrowserThread::PostTask(
729 BrowserThread::FILE, FROM_HERE,
[email protected]95d29192009-10-30 01:49:06730 NewRunnableFunction(
[email protected]ca3dbf52010-05-19 22:27:06731 &extension_file_util::UninstallExtension,
732 install_directory_,
733 extension_id_copy));
[email protected]9f1087e2009-06-15 17:29:32734 }
735
[email protected]c10da4b02010-03-25 14:38:32736 ClearExtensionData(extension_url);
[email protected]0dfe05c2011-02-23 23:03:36737 UntrackTerminatedExtension(extension_id);
[email protected]211030342010-09-30 18:41:06738
739 // Notify interested parties that we've uninstalled this extension.
740 NotificationService::current()->Notify(
741 NotificationType::EXTENSION_UNINSTALLED,
742 Source<Profile>(profile_),
743 Details<UninstalledExtensionInfo>(&uninstalled_extension_info));
[email protected]c10da4b02010-03-25 14:38:32744}
745
[email protected]eaa7dd182010-12-14 11:09:00746void ExtensionService::ClearExtensionData(const GURL& extension_url) {
[email protected]c10da4b02010-03-25 14:38:32747 scoped_refptr<ExtensionDataDeleter> deleter(
748 new ExtensionDataDeleter(profile_, extension_url));
749 deleter->StartDeleting();
[email protected]9f1087e2009-06-15 17:29:32750}
751
[email protected]eaa7dd182010-12-14 11:09:00752void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12753 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20754
[email protected]9adb9692010-10-29 23:14:02755 const Extension* extension =
756 GetExtensionByIdInternal(extension_id, false, true);
[email protected]f681c782010-11-19 11:19:39757 if (!extension)
[email protected]0c6da502009-08-14 22:32:39758 return;
[email protected]0c6da502009-08-14 22:32:39759
[email protected]e8c729a2010-03-09 19:55:19760 extension_prefs_->SetExtensionState(extension, Extension::ENABLED);
[email protected]1784e83a2009-09-08 21:01:52761
[email protected]0c6da502009-08-14 22:32:39762 // Move it over to the enabled list.
[email protected]00cd9c42010-11-02 20:15:57763 extensions_.push_back(make_scoped_refptr(extension));
[email protected]0c6da502009-08-14 22:32:39764 ExtensionList::iterator iter = std::find(disabled_extensions_.begin(),
765 disabled_extensions_.end(),
766 extension);
767 disabled_extensions_.erase(iter);
768
[email protected]f681c782010-11-19 11:19:39769 // Make sure any browser action contained within it is not hidden.
770 extension_prefs_->SetBrowserActionVisibility(extension, true);
771
[email protected]8f9d4eb2011-02-05 01:39:10772 ExtensionWebUI::RegisterChromeURLOverrides(profile_,
[email protected]86c008e82009-08-28 20:26:05773 extension->GetChromeURLOverrides());
774
[email protected]62d30f42009-10-01 22:36:06775 NotifyExtensionLoaded(extension);
[email protected]aab98a52009-12-02 03:22:35776 UpdateActiveExtensionsInCrashReporter();
[email protected]0c6da502009-08-14 22:32:39777}
778
[email protected]eaa7dd182010-12-14 11:09:00779void ExtensionService::DisableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12780 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20781
[email protected]9adb9692010-10-29 23:14:02782 const Extension* extension =
783 GetExtensionByIdInternal(extension_id, true, false);
[email protected]b2ba9962009-12-10 20:10:15784 // The extension may have been disabled already.
785 if (!extension)
[email protected]1784e83a2009-09-08 21:01:52786 return;
[email protected]1784e83a2009-09-08 21:01:52787
[email protected]e8c729a2010-03-09 19:55:19788 extension_prefs_->SetExtensionState(extension, Extension::DISABLED);
[email protected]1784e83a2009-09-08 21:01:52789
790 // Move it over to the disabled list.
[email protected]00cd9c42010-11-02 20:15:57791 disabled_extensions_.push_back(make_scoped_refptr(extension));
[email protected]1784e83a2009-09-08 21:01:52792 ExtensionList::iterator iter = std::find(extensions_.begin(),
793 extensions_.end(),
794 extension);
795 extensions_.erase(iter);
796
[email protected]8f9d4eb2011-02-05 01:39:10797 ExtensionWebUI::UnregisterChromeURLOverrides(profile_,
[email protected]1784e83a2009-09-08 21:01:52798 extension->GetChromeURLOverrides());
799
[email protected]a9f39a312010-12-23 22:14:27800 NotifyExtensionUnloaded(extension, UnloadedExtensionInfo::DISABLE);
[email protected]aab98a52009-12-02 03:22:35801 UpdateActiveExtensionsInCrashReporter();
[email protected]1784e83a2009-09-08 21:01:52802}
803
[email protected]eaa7dd182010-12-14 11:09:00804void ExtensionService::GrantPermissions(const Extension* extension) {
[email protected]8d888c12010-11-30 00:00:25805 CHECK(extension);
806
807 // We only maintain the granted permissions prefs for INTERNAL extensions.
[email protected]139e5732011-03-16 19:29:51808 CHECK_EQ(Extension::INTERNAL, extension->location());
[email protected]8d888c12010-11-30 00:00:25809
810 ExtensionExtent effective_hosts = extension->GetEffectiveHostPermissions();
811 extension_prefs_->AddGrantedPermissions(extension->id(),
812 extension->HasFullPermissions(),
813 extension->api_permissions(),
814 effective_hosts);
815}
816
[email protected]eaa7dd182010-12-14 11:09:00817void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]8d888c12010-11-30 00:00:25818 const Extension* extension) {
819 CHECK(extension);
820 GrantPermissions(extension);
821 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
822 EnableExtension(extension->id());
823}
824
[email protected]eaa7dd182010-12-14 11:09:00825void ExtensionService::LoadExtension(const FilePath& extension_path) {
[email protected]ca4b5fa32010-10-09 12:42:18826 BrowserThread::PostTask(
827 BrowserThread::FILE, FROM_HERE,
[email protected]95d29192009-10-30 01:49:06828 NewRunnableMethod(
829 backend_.get(),
[email protected]eaa7dd182010-12-14 11:09:00830 &ExtensionServiceBackend::LoadSingleExtension,
831 extension_path, scoped_refptr<ExtensionService>(this)));
[email protected]9f1087e2009-06-15 17:29:32832}
833
[email protected]eaa7dd182010-12-14 11:09:00834void ExtensionService::LoadComponentExtensions() {
[email protected]1952c7d2010-03-04 23:48:34835 for (RegisteredComponentExtensions::iterator it =
836 component_extension_manifests_.begin();
837 it != component_extension_manifests_.end(); ++it) {
[email protected]61b55b62011-03-24 09:03:10838 LoadComponentExtension(*it);
[email protected]1952c7d2010-03-04 23:48:34839 }
840}
841
[email protected]61b55b62011-03-24 09:03:10842void ExtensionService::LoadComponentExtension(
843 const ComponentExtensionInfo &info) {
844 JSONStringValueSerializer serializer(info.manifest);
845 scoped_ptr<Value> manifest(serializer.Deserialize(NULL, NULL));
846 if (!manifest.get()) {
847 DLOG(ERROR) << "Failed to parse manifest for extension";
848 return;
849 }
850
851 std::string error;
852 scoped_refptr<const Extension> extension(Extension::Create(
853 info.root_directory,
854 Extension::COMPONENT,
855 *static_cast<DictionaryValue*>(manifest.get()),
856 true, // Require key
857 Extension::ShouldDoStrictErrorChecking(Extension::COMPONENT),
858 &error));
859 if (!extension.get()) {
860 NOTREACHED() << error;
861 return;
862 }
863 AddExtension(extension);
864}
865
[email protected]eaa7dd182010-12-14 11:09:00866void ExtensionService::LoadAllExtensions() {
[email protected]a29a517a2011-01-21 21:11:12867 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a315ba92010-11-16 14:12:21868
[email protected]cc2c3432009-11-06 17:24:36869 base::TimeTicks start_time = base::TimeTicks::Now();
870
[email protected]1952c7d2010-03-04 23:48:34871 // Load any component extensions.
872 LoadComponentExtensions();
873
[email protected]e72e8eb82009-06-18 17:21:51874 // Load the previously installed extensions.
[email protected]a315ba92010-11-16 14:12:21875 scoped_ptr<ExtensionPrefs::ExtensionsInfo> extensions_info(
[email protected]e6090e42010-03-23 22:44:08876 extension_prefs_->GetInstalledExtensionsInfo());
[email protected]c6d474f82009-12-16 21:11:06877
[email protected]a315ba92010-11-16 14:12:21878 std::vector<int> reload_reason_counts(NUM_MANIFEST_RELOAD_REASONS, 0);
879 bool should_write_prefs = false;
880
881 for (size_t i = 0; i < extensions_info->size(); ++i) {
882 ExtensionInfo* info = extensions_info->at(i).get();
883
884 ManifestReloadReason reload_reason = ShouldReloadExtensionManifest(*info);
885 ++reload_reason_counts[reload_reason];
886 UMA_HISTOGRAM_ENUMERATION("Extensions.ManifestReloadEnumValue",
887 reload_reason, 100);
888
889 if (reload_reason != NOT_NEEDED) {
890 // Reloading and extension reads files from disk. We do this on the
891 // UI thread because reloads should be very rare, and the complexity
892 // added by delaying the time when the extensions service knows about
893 // all extensions is significant. See crbug.com/37548 for details.
894 // |allow_io| disables tests that file operations run on the file
895 // thread.
896 base::ThreadRestrictions::ScopedAllowIO allow_io;
897
898 std::string error;
899 scoped_refptr<const Extension> extension(
900 extension_file_util::LoadExtension(
[email protected]542258c2011-03-04 21:25:31901 info->extension_path,
902 info->extension_location,
903 false, // Don't require key
904 Extension::ShouldDoStrictErrorChecking(info->extension_location),
905 &error));
[email protected]a315ba92010-11-16 14:12:21906
907 if (extension.get()) {
908 extensions_info->at(i)->extension_manifest.reset(
909 static_cast<DictionaryValue*>(
910 extension->manifest_value()->DeepCopy()));
911 should_write_prefs = true;
912 }
[email protected]c6d474f82009-12-16 21:11:06913 }
914 }
915
[email protected]a315ba92010-11-16 14:12:21916 for (size_t i = 0; i < extensions_info->size(); ++i) {
917 LoadInstalledExtension(*extensions_info->at(i), should_write_prefs);
[email protected]c6d474f82009-12-16 21:11:06918 }
919
[email protected]ae09ca62009-08-21 19:46:46920 OnLoadedInstalledExtensions();
[email protected]cc2c3432009-11-06 17:24:36921
[email protected]a315ba92010-11-16 14:12:21922 // The histograms Extensions.ManifestReload* allow us to validate
923 // the assumption that reloading manifest is a rare event.
924 UMA_HISTOGRAM_COUNTS_100("Extensions.ManifestReloadNotNeeded",
925 reload_reason_counts[NOT_NEEDED]);
926 UMA_HISTOGRAM_COUNTS_100("Extensions.ManifestReloadUnpackedDir",
927 reload_reason_counts[UNPACKED_DIR]);
928 UMA_HISTOGRAM_COUNTS_100("Extensions.ManifestReloadNeedsRelocalization",
929 reload_reason_counts[NEEDS_RELOCALIZATION]);
930
[email protected]cc2c3432009-11-06 17:24:36931 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadAll", extensions_.size());
932 UMA_HISTOGRAM_COUNTS_100("Extensions.Disabled", disabled_extensions_.size());
933
[email protected]1952c7d2010-03-04 23:48:34934 UMA_HISTOGRAM_TIMES("Extensions.LoadAllTime",
935 base::TimeTicks::Now() - start_time);
[email protected]cc2c3432009-11-06 17:24:36936
[email protected]9b217652010-10-08 22:04:23937 int app_count = 0;
938 int hosted_app_count = 0;
939 int packaged_app_count = 0;
[email protected]1952c7d2010-03-04 23:48:34940 int user_script_count = 0;
941 int extension_count = 0;
942 int theme_count = 0;
943 int external_count = 0;
944 int page_action_count = 0;
945 int browser_action_count = 0;
946 ExtensionList::iterator ex;
947 for (ex = extensions_.begin(); ex != extensions_.end(); ++ex) {
[email protected]9b217652010-10-08 22:04:23948 Extension::Location location = (*ex)->location();
[email protected]7fa19f82010-12-21 19:40:08949 Extension::Type type = (*ex)->GetType();
[email protected]9b217652010-10-08 22:04:23950 if ((*ex)->is_app()) {
951 UMA_HISTOGRAM_ENUMERATION("Extensions.AppLocation",
952 location, 100);
953 } else if (type == Extension::TYPE_EXTENSION) {
954 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionLocation",
955 location, 100);
956 }
957
[email protected]1952c7d2010-03-04 23:48:34958 // Don't count component extensions, since they are only extensions as an
959 // implementation detail.
[email protected]9b217652010-10-08 22:04:23960 if (location == Extension::COMPONENT)
[email protected]1952c7d2010-03-04 23:48:34961 continue;
962
[email protected]e8c729a2010-03-09 19:55:19963 // Don't count unpacked extensions, since they're a developer-specific
964 // feature.
[email protected]9b217652010-10-08 22:04:23965 if (location == Extension::LOAD)
[email protected]e8c729a2010-03-09 19:55:19966 continue;
967
[email protected]9b217652010-10-08 22:04:23968 // Using an enumeration shows us the total installed ratio across all users.
969 // Using the totals per user at each startup tells us the distribution of
970 // usage for each user (e.g. 40% of users have at least one app installed).
971 UMA_HISTOGRAM_ENUMERATION("Extensions.LoadType", type, 100);
972 switch (type) {
973 case Extension::TYPE_THEME:
[email protected]a315ba92010-11-16 14:12:21974 ++theme_count;
[email protected]9b217652010-10-08 22:04:23975 break;
976 case Extension::TYPE_USER_SCRIPT:
[email protected]a315ba92010-11-16 14:12:21977 ++user_script_count;
[email protected]9b217652010-10-08 22:04:23978 break;
979 case Extension::TYPE_HOSTED_APP:
[email protected]a315ba92010-11-16 14:12:21980 ++app_count;
981 ++hosted_app_count;
[email protected]9b217652010-10-08 22:04:23982 break;
983 case Extension::TYPE_PACKAGED_APP:
[email protected]a315ba92010-11-16 14:12:21984 ++app_count;
985 ++packaged_app_count;
[email protected]9b217652010-10-08 22:04:23986 break;
987 case Extension::TYPE_EXTENSION:
988 default:
[email protected]a315ba92010-11-16 14:12:21989 ++extension_count;
[email protected]9b217652010-10-08 22:04:23990 break;
[email protected]cc2c3432009-11-06 17:24:36991 }
[email protected]9b217652010-10-08 22:04:23992 if (Extension::IsExternalLocation(location))
[email protected]a315ba92010-11-16 14:12:21993 ++external_count;
[email protected]9b217652010-10-08 22:04:23994 if ((*ex)->page_action() != NULL)
[email protected]a315ba92010-11-16 14:12:21995 ++page_action_count;
[email protected]9b217652010-10-08 22:04:23996 if ((*ex)->browser_action() != NULL)
[email protected]a315ba92010-11-16 14:12:21997 ++browser_action_count;
[email protected]cc2c3432009-11-06 17:24:36998 }
[email protected]9b217652010-10-08 22:04:23999 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadApp", app_count);
1000 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadHostedApp", hosted_app_count);
1001 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadPackagedApp", packaged_app_count);
[email protected]1952c7d2010-03-04 23:48:341002 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadExtension", extension_count);
1003 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadUserScript", user_script_count);
1004 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadTheme", theme_count);
1005 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadExternal", external_count);
1006 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadPageAction", page_action_count);
1007 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadBrowserAction",
1008 browser_action_count);
[email protected]ae09ca62009-08-21 19:46:461009}
1010
[email protected]eaa7dd182010-12-14 11:09:001011void ExtensionService::LoadInstalledExtension(const ExtensionInfo& info,
[email protected]542258c2011-03-04 21:25:311012 bool write_to_prefs) {
[email protected]ae09ca62009-08-21 19:46:461013 std::string error;
[email protected]9adb9692010-10-29 23:14:021014 scoped_refptr<const Extension> extension(NULL);
[email protected]306a2bd2010-08-11 14:56:361015 if (!extension_prefs_->IsExtensionAllowedByPolicy(info.extension_id)) {
1016 error = errors::kDisabledByPolicy;
1017 } else if (info.extension_manifest.get()) {
[email protected]e8c729a2010-03-09 19:55:191018 bool require_key = info.extension_location != Extension::LOAD;
[email protected]66e4eb32010-10-27 20:37:411019 extension = Extension::Create(
[email protected]542258c2011-03-04 21:25:311020 info.extension_path,
1021 info.extension_location,
1022 *info.extension_manifest,
1023 require_key,
1024 Extension::ShouldDoStrictErrorChecking(info.extension_location),
1025 &error);
[email protected]ae09ca62009-08-21 19:46:461026 } else {
[email protected]c6d474f82009-12-16 21:11:061027 error = errors::kManifestUnreadable;
[email protected]ae09ca62009-08-21 19:46:461028 }
1029
1030 if (!extension) {
[email protected]c6d474f82009-12-16 21:11:061031 ReportExtensionLoadError(info.extension_path,
[email protected]d11c8e92009-10-20 23:26:401032 error,
1033 NotificationType::EXTENSION_INSTALL_ERROR,
1034 false);
[email protected]ae09ca62009-08-21 19:46:461035 return;
1036 }
1037
[email protected]c6d474f82009-12-16 21:11:061038 if (write_to_prefs)
1039 extension_prefs_->UpdateManifest(extension);
1040
[email protected]49cd1572011-02-08 21:38:451041 AddExtension(extension);
[email protected]9f1087e2009-06-15 17:29:321042}
1043
[email protected]eaa7dd182010-12-14 11:09:001044void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]57a777f72010-03-31 01:09:421045 // The ChromeURLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:061046 // was loaded, otherwise a race can arise where a renderer that is created
1047 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:421048 // that the request context doesn't yet know about. The profile is responsible
1049 // for ensuring its URLRequestContexts appropriately discover the loaded
1050 // extension.
1051 if (profile_) {
1052 profile_->RegisterExtensionWithRequestContexts(extension);
[email protected]19eb80152011-02-26 00:28:431053 profile_->GetExtensionSpecialStoragePolicy()->
1054 GrantRightsForExtension(extension);
[email protected]62d30f42009-10-01 22:36:061055 }
1056
1057 NotificationService::current()->Notify(
1058 NotificationType::EXTENSION_LOADED,
[email protected]24e7a9d2009-11-04 11:11:341059 Source<Profile>(profile_),
[email protected]9adb9692010-10-29 23:14:021060 Details<const Extension>(extension));
[email protected]62d30f42009-10-01 22:36:061061}
1062
[email protected]a9f39a312010-12-23 22:14:271063void ExtensionService::NotifyExtensionUnloaded(
1064 const Extension* extension, UnloadedExtensionInfo::Reason reason) {
1065 UnloadedExtensionInfo details(extension, reason);
[email protected]62d30f42009-10-01 22:36:061066 NotificationService::current()->Notify(
1067 NotificationType::EXTENSION_UNLOADED,
[email protected]24e7a9d2009-11-04 11:11:341068 Source<Profile>(profile_),
[email protected]a9f39a312010-12-23 22:14:271069 Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061070
[email protected]57a777f72010-03-31 01:09:421071 if (profile_) {
1072 profile_->UnregisterExtensionWithRequestContexts(extension);
[email protected]19eb80152011-02-26 00:28:431073 profile_->GetExtensionSpecialStoragePolicy()->
1074 RevokeRightsForExtension(extension);
[email protected]62d30f42009-10-01 22:36:061075 }
1076}
1077
[email protected]eaa7dd182010-12-14 11:09:001078void ExtensionService::UpdateExtensionBlacklist(
[email protected]6b75ec32009-08-14 06:37:181079 const std::vector<std::string>& blacklist) {
1080 // Use this set to indicate if an extension in the blacklist has been used.
1081 std::set<std::string> blacklist_set;
1082 for (unsigned int i = 0; i < blacklist.size(); ++i) {
1083 if (Extension::IdIsValid(blacklist[i])) {
1084 blacklist_set.insert(blacklist[i]);
1085 }
1086 }
1087 extension_prefs_->UpdateBlacklist(blacklist_set);
1088 std::vector<std::string> to_be_removed;
1089 // Loop current extensions, unload installed extensions.
1090 for (ExtensionList::const_iterator iter = extensions_.begin();
1091 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021092 const Extension* extension = (*iter);
[email protected]6b75ec32009-08-14 06:37:181093 if (blacklist_set.find(extension->id()) != blacklist_set.end()) {
1094 to_be_removed.push_back(extension->id());
1095 }
1096 }
1097
1098 // UnloadExtension will change the extensions_ list. So, we should
1099 // call it outside the iterator loop.
1100 for (unsigned int i = 0; i < to_be_removed.size(); ++i) {
[email protected]a9f39a312010-12-23 22:14:271101 UnloadExtension(to_be_removed[i], UnloadedExtensionInfo::DISABLE);
[email protected]6b75ec32009-08-14 06:37:181102 }
1103}
1104
[email protected]0a071a32011-02-08 00:18:241105Profile* ExtensionService::profile() {
1106 return profile_;
1107}
1108
[email protected]eaa7dd182010-12-14 11:09:001109void ExtensionService::DestroyingProfile() {
[email protected]94b4ffc2011-02-04 05:55:171110 if (updater_.get()) {
1111 updater_->Stop();
1112 }
[email protected]97d2f1d2011-01-15 00:41:081113 browser_event_router_.reset();
[email protected]118de7892011-03-16 13:31:401114 preference_event_router_.reset();
[email protected]2fb7dc982010-09-29 12:24:281115 pref_change_registrar_.RemoveAll();
[email protected]aa96d3a2010-08-21 08:45:251116 profile_ = NULL;
[email protected]2fb7dc982010-09-29 12:24:281117 toolbar_model_.DestroyingProfile();
[email protected]aa96d3a2010-08-21 08:45:251118}
1119
[email protected]eaa7dd182010-12-14 11:09:001120ExtensionPrefs* ExtensionService::extension_prefs() {
[email protected]ddb1e5a2010-12-13 20:10:451121 return extension_prefs_;
1122}
1123
[email protected]eaa7dd182010-12-14 11:09:001124void ExtensionService::CheckAdminBlacklist() {
[email protected]aa96d3a2010-08-21 08:45:251125 std::vector<std::string> to_be_removed;
1126 // Loop through extensions list, unload installed extensions.
1127 for (ExtensionList::const_iterator iter = extensions_.begin();
1128 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021129 const Extension* extension = (*iter);
[email protected]aa96d3a2010-08-21 08:45:251130 if (!extension_prefs_->IsExtensionAllowedByPolicy(extension->id()))
1131 to_be_removed.push_back(extension->id());
1132 }
1133
1134 // UnloadExtension will change the extensions_ list. So, we should
1135 // call it outside the iterator loop.
1136 for (unsigned int i = 0; i < to_be_removed.size(); ++i)
[email protected]a9f39a312010-12-23 22:14:271137 UnloadExtension(to_be_removed[i], UnloadedExtensionInfo::DISABLE);
[email protected]aa96d3a2010-08-21 08:45:251138}
1139
[email protected]eaa7dd182010-12-14 11:09:001140bool ExtensionService::IsIncognitoEnabled(const Extension* extension) {
[email protected]cb0ce1e022010-03-10 19:54:411141 // If this is a component extension we always allow it to work in incognito
1142 // mode.
1143 if (extension->location() == Extension::COMPONENT)
1144 return true;
1145
1146 // Check the prefs.
1147 return extension_prefs_->IsIncognitoEnabled(extension->id());
[email protected]db7331a2010-02-25 22:10:501148}
[email protected]55a35692010-02-11 23:25:211149
[email protected]eaa7dd182010-12-14 11:09:001150void ExtensionService::SetIsIncognitoEnabled(const Extension* extension,
[email protected]db7331a2010-02-25 22:10:501151 bool enabled) {
[email protected]8c6c8c22011-03-09 12:52:341152 // Broadcast unloaded and loaded events to update browser state. Only bother
1153 // if the value changed and the extension is actually enabled, since there is
1154 // no UI otherwise.
1155 bool old_enabled = extension_prefs_->IsIncognitoEnabled(extension->id());
1156 if (enabled == old_enabled)
1157 return;
1158
[email protected]cb0ce1e022010-03-10 19:54:411159 extension_prefs_->SetIsIncognitoEnabled(extension->id(), enabled);
[email protected]c1499f3d2010-03-05 00:33:241160
[email protected]8c6c8c22011-03-09 12:52:341161 bool extension_is_enabled = std::find(extensions_.begin(), extensions_.end(),
1162 extension) != extensions_.end();
1163 if (extension_is_enabled) {
[email protected]a9f39a312010-12-23 22:14:271164 NotifyExtensionUnloaded(extension, UnloadedExtensionInfo::DISABLE);
[email protected]568f33d2010-08-04 17:06:411165 NotifyExtensionLoaded(extension);
1166 }
[email protected]55a35692010-02-11 23:25:211167}
1168
[email protected]eaa7dd182010-12-14 11:09:001169bool ExtensionService::CanCrossIncognito(const Extension* extension) {
[email protected]2a8f24e2010-11-03 21:37:051170 // We allow the extension to see events and data from another profile iff it
1171 // uses "spanning" behavior and it has incognito access. "split" mode
1172 // extensions only see events for a matching profile.
1173 return IsIncognitoEnabled(extension) && !extension->incognito_split_mode();
1174}
1175
[email protected]eaa7dd182010-12-14 11:09:001176bool ExtensionService::AllowFileAccess(const Extension* extension) {
[email protected]05c82182010-06-24 17:49:081177 return (CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]334e04a2010-06-24 23:34:441178 switches::kDisableExtensionsFileAccessCheck) ||
[email protected]05c82182010-06-24 17:49:081179 extension_prefs_->AllowFileAccess(extension->id()));
1180}
1181
[email protected]eaa7dd182010-12-14 11:09:001182void ExtensionService::SetAllowFileAccess(const Extension* extension,
[email protected]9adb9692010-10-29 23:14:021183 bool allow) {
[email protected]05c82182010-06-24 17:49:081184 extension_prefs_->SetAllowFileAccess(extension->id(), allow);
1185 NotificationService::current()->Notify(
1186 NotificationType::EXTENSION_USER_SCRIPTS_UPDATED,
1187 Source<Profile>(profile_),
[email protected]9adb9692010-10-29 23:14:021188 Details<const Extension>(extension));
[email protected]05c82182010-06-24 17:49:081189}
1190
[email protected]eaa7dd182010-12-14 11:09:001191bool ExtensionService::GetBrowserActionVisibility(const Extension* extension) {
[email protected]f681c782010-11-19 11:19:391192 return extension_prefs_->GetBrowserActionVisibility(extension);
1193}
1194
[email protected]eaa7dd182010-12-14 11:09:001195void ExtensionService::SetBrowserActionVisibility(const Extension* extension,
[email protected]8e4560b62011-01-14 10:09:141196 bool visible) {
[email protected]f681c782010-11-19 11:19:391197 extension_prefs_->SetBrowserActionVisibility(extension, visible);
1198}
1199
[email protected]8e4560b62011-01-14 10:09:141200// Some extensions will autoupdate themselves externally from Chrome. These
1201// are typically part of some larger client application package. To support
1202// these, the extension will register its location in the the preferences file
1203// (and also, on Windows, in the registry) and this code will periodically
1204// check that location for a .crx file, which it will then install locally if
1205// a new version is available.
1206// Errors are reported through ExtensionErrorReporter. Succcess is not
1207// reported.
[email protected]eaa7dd182010-12-14 11:09:001208void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:121209 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]8e4560b62011-01-14 10:09:141210
1211 // Note that this installation is intentionally silent (since it didn't
1212 // go through the front-end). Extensions that are registered in this
1213 // way are effectively considered 'pre-bundled', and so implicitly
1214 // trusted. In general, if something has HKLM or filesystem access,
1215 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:121216
1217 // If any external extension records give a URL, a provider will set
1218 // this to true. Used by OnExternalProviderReady() to see if we need
1219 // to start an update check to fetch a new external extension.
1220 external_extension_url_added_ = false;
[email protected]8e4560b62011-01-14 10:09:141221
1222 // Ask each external extension provider to give us a call back for each
1223 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
1224 ProviderCollection::const_iterator i;
1225 for (i = external_extension_providers_.begin();
1226 i != external_extension_providers_.end(); ++i) {
1227 ExternalExtensionProviderInterface* provider = i->get();
1228 provider->VisitRegisteredExtension();
1229 }
1230
1231 // Uninstall of unclaimed extensions will happen after all the providers
[email protected]a29a517a2011-01-21 21:11:121232 // had reported ready. Every provider calls OnExternalProviderReady()
1233 // when it finishes, and OnExternalProviderReady() only acts when all
1234 // providers are ready. In case there are no providers, we call it
1235 // to trigger removal of extensions that used to have an external source.
1236 if (external_extension_providers_.empty())
1237 OnExternalProviderReady();
[email protected]9f1087e2009-06-15 17:29:321238}
1239
[email protected]8e4560b62011-01-14 10:09:141240void ExtensionService::OnExternalProviderReady() {
[email protected]a29a517a2011-01-21 21:11:121241 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1242
1243 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:141244 // if all of them are finished. So we check them first.
1245 ProviderCollection::const_iterator i;
1246 for (i = external_extension_providers_.begin();
1247 i != external_extension_providers_.end(); ++i) {
1248 ExternalExtensionProviderInterface* provider = i->get();
[email protected]a29a517a2011-01-21 21:11:121249 if (!provider->IsReady())
[email protected]8e4560b62011-01-14 10:09:141250 return;
[email protected]8e4560b62011-01-14 10:09:141251 }
1252
[email protected]a29a517a2011-01-21 21:11:121253 // All the providers are ready. Install any pending extensions.
1254 if (external_extension_url_added_ && updater()) {
1255 external_extension_url_added_ = false;
[email protected]8e4560b62011-01-14 10:09:141256 updater()->CheckNow();
1257 }
1258
1259 // Uninstall all the unclaimed extensions.
1260 scoped_ptr<ExtensionPrefs::ExtensionsInfo> extensions_info(
1261 extension_prefs_->GetInstalledExtensionsInfo());
1262 for (size_t i = 0; i < extensions_info->size(); ++i) {
1263 ExtensionInfo* info = extensions_info->at(i).get();
1264 if (Extension::IsExternalLocation(info->extension_location))
1265 CheckExternalUninstall(info->extension_id);
1266 }
[email protected]6c751e72010-11-23 10:11:101267}
1268
[email protected]a9f39a312010-12-23 22:14:271269void ExtensionService::UnloadExtension(
1270 const std::string& extension_id,
1271 UnloadedExtensionInfo::Reason reason) {
[email protected]27e469a2010-01-11 20:35:091272 // Make sure the extension gets deleted after we return from this function.
[email protected]9adb9692010-10-29 23:14:021273 scoped_refptr<const Extension> extension(
[email protected]0c6da502009-08-14 22:32:391274 GetExtensionByIdInternal(extension_id, true, true));
[email protected]631cf822009-05-15 07:01:251275
[email protected]fa0c96732010-11-17 00:14:231276 // This method can be called via PostTask, so the extension may have been
1277 // unloaded by the time this runs.
1278 if (!extension)
1279 return;
[email protected]0c6da502009-08-14 22:32:391280
[email protected]1eb175082010-02-10 09:26:161281 // Keep information about the extension so that we can reload it later
1282 // even if it's not permanently installed.
1283 unloaded_extension_paths_[extension->id()] = extension->path();
1284
[email protected]f17dbd42010-08-16 23:21:101285 // Clean up if the extension is meant to be enabled after a reload.
1286 disabled_extension_paths_.erase(extension->id());
1287
[email protected]d7e9a862010-11-03 21:57:491288 // Clean up runtime data.
1289 extension_runtime_data_.erase(extension_id);
1290
[email protected]8f9d4eb2011-02-05 01:39:101291 ExtensionWebUI::UnregisterChromeURLOverrides(profile_,
[email protected]86c008e82009-08-28 20:26:051292 extension->GetChromeURLOverrides());
1293
[email protected]0c6da502009-08-14 22:32:391294 ExtensionList::iterator iter = std::find(disabled_extensions_.begin(),
1295 disabled_extensions_.end(),
1296 extension.get());
1297 if (iter != disabled_extensions_.end()) {
[email protected]a9f39a312010-12-23 22:14:271298 UnloadedExtensionInfo details(extension, reason);
1299 details.already_disabled = true;
[email protected]0c6da502009-08-14 22:32:391300 disabled_extensions_.erase(iter);
[email protected]866930682009-08-18 22:53:471301 NotificationService::current()->Notify(
[email protected]a9f39a312010-12-23 22:14:271302 NotificationType::EXTENSION_UNLOADED,
[email protected]24e7a9d2009-11-04 11:11:341303 Source<Profile>(profile_),
[email protected]a9f39a312010-12-23 22:14:271304 Details<UnloadedExtensionInfo>(&details));
[email protected]0c6da502009-08-14 22:32:391305 return;
1306 }
1307
1308 iter = std::find(extensions_.begin(), extensions_.end(), extension.get());
[email protected]894bb502009-05-21 22:39:571309
[email protected]631cf822009-05-15 07:01:251310 // Remove the extension from our list.
1311 extensions_.erase(iter);
1312
[email protected]a9f39a312010-12-23 22:14:271313 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]aab98a52009-12-02 03:22:351314 UpdateActiveExtensionsInCrashReporter();
[email protected]631cf822009-05-15 07:01:251315}
1316
[email protected]eaa7dd182010-12-14 11:09:001317void ExtensionService::UnloadAllExtensions() {
[email protected]19eb80152011-02-26 00:28:431318 if (profile_) {
1319 profile_->GetExtensionSpecialStoragePolicy()->
1320 RevokeRightsForAllExtensions();
1321 }
[email protected]9f1087e2009-06-15 17:29:321322 extensions_.clear();
[email protected]cd500f72010-06-25 23:44:321323 disabled_extensions_.clear();
[email protected]bb7f40952011-01-13 00:21:201324 terminated_extension_ids_.clear();
1325 terminated_extensions_.clear();
[email protected]d7e9a862010-11-03 21:57:491326 extension_runtime_data_.clear();
[email protected]cd500f72010-06-25 23:44:321327
[email protected]c6e4a3412009-06-24 15:45:291328 // TODO(erikkay) should there be a notification for this? We can't use
1329 // EXTENSION_UNLOADED since that implies that the extension has been disabled
1330 // or uninstalled, and UnloadAll is just part of shutdown.
[email protected]9f1087e2009-06-15 17:29:321331}
1332
[email protected]eaa7dd182010-12-14 11:09:001333void ExtensionService::ReloadExtensions() {
[email protected]9f1087e2009-06-15 17:29:321334 UnloadAllExtensions();
1335 LoadAllExtensions();
1336}
1337
[email protected]eaa7dd182010-12-14 11:09:001338void ExtensionService::GarbageCollectExtensions() {
[email protected]39d9f62c2010-12-03 10:48:501339 if (extension_prefs_->pref_service()->ReadOnly())
[email protected]ba399672010-04-06 15:42:391340 return;
1341
[email protected]ca3dbf52010-05-19 22:27:061342 scoped_ptr<ExtensionPrefs::ExtensionsInfo> info(
1343 extension_prefs_->GetInstalledExtensionsInfo());
1344
1345 std::map<std::string, FilePath> extension_paths;
1346 for (size_t i = 0; i < info->size(); ++i)
1347 extension_paths[info->at(i)->extension_id] = info->at(i)->extension_path;
1348
[email protected]ca4b5fa32010-10-09 12:42:181349 BrowserThread::PostTask(
1350 BrowserThread::FILE, FROM_HERE,
[email protected]95d29192009-10-30 01:49:061351 NewRunnableFunction(
1352 &extension_file_util::GarbageCollectExtensions, install_directory_,
[email protected]ca3dbf52010-05-19 22:27:061353 extension_paths));
[email protected]f6ccd652010-11-17 00:48:341354
1355 // Also garbage-collect themes. We check |profile_| to be
1356 // defensive; in the future, we may call GarbageCollectExtensions()
1357 // from somewhere other than Init() (e.g., in a timer).
1358 if (profile_) {
[email protected]18280372011-03-22 18:05:221359 ThemeServiceFactory::GetForProfile(profile_)->RemoveUnusedThemes();
[email protected]f6ccd652010-11-17 00:48:341360 }
[email protected]3cf4f0992009-02-03 23:00:301361}
1362
[email protected]eaa7dd182010-12-14 11:09:001363void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]93fd78f42009-07-10 16:43:171364 if (updater_.get()) {
1365 updater_->Start();
1366 }
[email protected]fa6a9102010-11-22 15:38:501367
1368 ready_ = true;
[email protected]e72e8eb82009-06-18 17:21:511369 NotificationService::current()->Notify(
1370 NotificationType::EXTENSIONS_READY,
[email protected]24e7a9d2009-11-04 11:11:341371 Source<Profile>(profile_),
[email protected]e72e8eb82009-06-18 17:21:511372 NotificationService::NoDetails());
1373}
1374
[email protected]49cd1572011-02-08 21:38:451375void ExtensionService::AddExtension(const Extension* extension) {
[email protected]ae09ca62009-08-21 19:46:461376 // Ensure extension is deleted unless we transfer ownership.
[email protected]9adb9692010-10-29 23:14:021377 scoped_refptr<const Extension> scoped_extension(extension);
[email protected]9f1087e2009-06-15 17:29:321378
[email protected]1eb175082010-02-10 09:26:161379 // The extension is now loaded, remove its data from unloaded extension map.
1380 unloaded_extension_paths_.erase(extension->id());
1381
[email protected]bb7f40952011-01-13 00:21:201382 // If a terminated extension is loaded, remove it from the terminated list.
1383 UntrackTerminatedExtension(extension->id());
1384
[email protected]f17dbd42010-08-16 23:21:101385 // If the extension was disabled for a reload, then enable it.
1386 if (disabled_extension_paths_.erase(extension->id()) > 0)
1387 EnableExtension(extension->id());
1388
[email protected]d728e002010-12-08 04:46:231389 // TODO(jstritar): We may be able to get rid of this branch by overriding the
1390 // default extension state to DISABLED when the --disable-extensions flag
1391 // is set (http://crbug.com/29067).
1392 if (!extensions_enabled() &&
1393 !extension->is_theme() &&
[email protected]aebe23a32010-12-10 22:15:481394 extension->location() != Extension::COMPONENT &&
1395 !Extension::IsExternalLocation(extension->location()))
[email protected]d728e002010-12-08 04:46:231396 return;
1397
1398 // Check if the extension's privileges have changed and disable the
1399 // extension if necessary.
[email protected]8d888c12010-11-30 00:00:251400 DisableIfPrivilegeIncrease(extension);
[email protected]0c6da502009-08-14 22:32:391401
[email protected]8d888c12010-11-30 00:00:251402 switch (extension_prefs_->GetExtensionState(extension->id())) {
1403 case Extension::ENABLED:
1404 extensions_.push_back(scoped_extension);
[email protected]1e8c93f2010-02-08 22:58:311405
[email protected]8d888c12010-11-30 00:00:251406 NotifyExtensionLoaded(extension);
[email protected]0c6da502009-08-14 22:32:391407
[email protected]8f9d4eb2011-02-05 01:39:101408 ExtensionWebUI::RegisterChromeURLOverrides(
[email protected]8d888c12010-11-30 00:00:251409 profile_, extension->GetChromeURLOverrides());
1410 break;
1411 case Extension::DISABLED:
1412 disabled_extensions_.push_back(scoped_extension);
1413 NotificationService::current()->Notify(
1414 NotificationType::EXTENSION_UPDATE_DISABLED,
1415 Source<Profile>(profile_),
1416 Details<const Extension>(extension));
1417 break;
1418 default:
1419 NOTREACHED();
1420 break;
[email protected]e72e8eb82009-06-18 17:21:511421 }
[email protected]aab98a52009-12-02 03:22:351422
[email protected]d7e9a862010-11-03 21:57:491423 SetBeingUpgraded(extension, false);
[email protected]1e8c93f2010-02-08 22:58:311424
[email protected]aab98a52009-12-02 03:22:351425 UpdateActiveExtensionsInCrashReporter();
[email protected]0b004da2010-07-02 17:54:311426
1427 if (profile_->GetTemplateURLModel())
1428 profile_->GetTemplateURLModel()->RegisterExtensionKeyword(extension);
[email protected]b671760b2010-07-15 21:13:471429
1430 // Load the icon for omnibox-enabled extensions so it will be ready to display
1431 // in the URL bar.
[email protected]29d0d4ac2010-09-08 21:10:311432 if (!extension->omnibox_keyword().empty()) {
1433 omnibox_popup_icon_manager_.LoadIcon(extension);
[email protected]b671760b2010-07-15 21:13:471434 omnibox_icon_manager_.LoadIcon(extension);
[email protected]29d0d4ac2010-09-08 21:10:311435 }
[email protected]aab98a52009-12-02 03:22:351436}
1437
[email protected]eaa7dd182010-12-14 11:09:001438void ExtensionService::DisableIfPrivilegeIncrease(const Extension* extension) {
[email protected]8d888c12010-11-30 00:00:251439 // We keep track of all permissions the user has granted each extension.
1440 // This allows extensions to gracefully support backwards compatibility
1441 // by including unknown permissions in their manifests. When the user
1442 // installs the extension, only the recognized permissions are recorded.
1443 // When the unknown permissions become recognized (e.g., through browser
1444 // upgrade), we can prompt the user to accept these new permissions.
1445 // Extensions can also silently upgrade to less permissions, and then
1446 // silently upgrade to a version that adds these permissions back.
1447 //
1448 // For example, pretend that Chrome 10 includes a permission "omnibox"
1449 // for an API that adds suggestions to the omnibox. An extension can
1450 // maintain backwards compatibility while still having "omnibox" in the
1451 // manifest. If a user installs the extension on Chrome 9, the browser
1452 // will record the permissions it recognized, not including "omnibox."
1453 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
1454 // will disable the extension and prompt the user to approve the increase
1455 // in privileges. The extension could then release a new version that
1456 // removes the "omnibox" permission. When the user upgrades, Chrome will
1457 // still remember that "omnibox" had been granted, so that if the
1458 // extension once again includes "omnibox" in an upgrade, the extension
1459 // can upgrade without requiring this user's approval.
1460 const Extension* old = GetExtensionByIdInternal(extension->id(),
1461 true, true);
1462 bool granted_full_access;
1463 std::set<std::string> granted_apis;
1464 ExtensionExtent granted_extent;
1465
1466 bool is_extension_upgrade = old != NULL;
1467 bool is_privilege_increase = false;
1468
1469 // We only record the granted permissions for INTERNAL extensions, since
1470 // they can't silently increase privileges.
1471 if (extension->location() == Extension::INTERNAL) {
1472 // Add all the recognized permissions if the granted permissions list
1473 // hasn't been initialized yet.
1474 if (!extension_prefs_->GetGrantedPermissions(extension->id(),
1475 &granted_full_access,
1476 &granted_apis,
1477 &granted_extent)) {
1478 GrantPermissions(extension);
1479 CHECK(extension_prefs_->GetGrantedPermissions(extension->id(),
1480 &granted_full_access,
1481 &granted_apis,
1482 &granted_extent));
1483 }
1484
1485 // Here, we check if an extension's privileges have increased in a manner
1486 // that requires the user's approval. This could occur because the browser
1487 // upgraded and recognized additional privileges, or an extension upgrades
1488 // to a version that requires additional privileges.
1489 is_privilege_increase = Extension::IsPrivilegeIncrease(
1490 granted_full_access, granted_apis, granted_extent, extension);
1491 }
1492
1493 if (is_extension_upgrade) {
[email protected]66e26872010-12-03 20:07:251494 // Other than for unpacked extensions, CrxInstaller should have guaranteed
1495 // that we aren't downgrading.
1496 if (extension->location() != Extension::LOAD)
1497 CHECK(extension->version()->CompareTo(*(old->version())) >= 0);
[email protected]8d888c12010-11-30 00:00:251498
1499 // Extensions get upgraded if the privileges are allowed to increase or
1500 // the privileges haven't increased.
1501 if (!is_privilege_increase) {
1502 SetBeingUpgraded(old, true);
1503 SetBeingUpgraded(extension, true);
1504 }
1505
1506 // To upgrade an extension in place, unload the old one and
1507 // then load the new one.
[email protected]a9f39a312010-12-23 22:14:271508 UnloadExtension(old->id(), UnloadedExtensionInfo::UPDATE);
[email protected]8d888c12010-11-30 00:00:251509 old = NULL;
1510 }
1511
1512 // Extension has changed permissions significantly. Disable it. A
1513 // notification should be sent by the caller.
1514 if (is_privilege_increase) {
1515 extension_prefs_->SetExtensionState(extension, Extension::DISABLED);
1516 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
1517 }
1518}
1519
[email protected]eaa7dd182010-12-14 11:09:001520void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:391521 std::set<std::string> extension_ids;
[email protected]aab98a52009-12-02 03:22:351522 for (size_t i = 0; i < extensions_.size(); ++i) {
[email protected]2a500c1b2010-11-05 15:27:341523 if (!extensions_[i]->is_theme() &&
1524 extensions_[i]->location() != Extension::COMPONENT)
[email protected]c8865962009-12-16 07:47:391525 extension_ids.insert(extensions_[i]->id());
[email protected]aab98a52009-12-02 03:22:351526 }
1527
1528 child_process_logging::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:251529}
1530
[email protected]eaa7dd182010-12-14 11:09:001531void ExtensionService::OnExtensionInstalled(const Extension* extension) {
[email protected]a29a517a2011-01-21 21:11:121532 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:201533
[email protected]4416c5a2010-06-26 01:28:571534 // Ensure extension is deleted unless we transfer ownership.
[email protected]9adb9692010-10-29 23:14:021535 scoped_refptr<const Extension> scoped_extension(extension);
[email protected]4416c5a2010-06-26 01:28:571536 Extension::State initial_state = Extension::DISABLED;
1537 bool initial_enable_incognito = false;
[email protected]aa142702010-03-26 01:26:331538 PendingExtensionMap::iterator it =
1539 pending_extensions_.find(extension->id());
[email protected]4416c5a2010-06-26 01:28:571540 if (it != pending_extensions_.end()) {
[email protected]11edd1e2010-07-21 00:14:501541 PendingExtensionInfo pending_extension_info = it->second;
1542 pending_extensions_.erase(it);
1543 it = pending_extensions_.end();
[email protected]8ef78fd2010-08-19 17:14:321544
[email protected]483d1ff2011-03-03 17:12:021545 if (!pending_extension_info.ShouldAllowInstall(*extension)) {
[email protected]4416c5a2010-06-26 01:28:571546 LOG(WARNING)
[email protected]483d1ff2011-03-03 17:12:021547 << "should_install_extension() returned false for "
1548 << extension->id() << " of type " << extension->GetType()
[email protected]7fa19f82010-12-21 19:40:081549 << " and update URL " << extension->update_url().spec()
1550 << "; not installing";
[email protected]4416c5a2010-06-26 01:28:571551 // Delete the extension directory since we're not going to
1552 // load it.
[email protected]ca4b5fa32010-10-09 12:42:181553 BrowserThread::PostTask(
1554 BrowserThread::FILE, FROM_HERE,
[email protected]53da2c962011-03-03 17:08:051555 NewRunnableFunction(&extension_file_util::DeleteFile, extension->path(), true));
[email protected]4416c5a2010-06-26 01:28:571556 return;
1557 }
[email protected]8ef78fd2010-08-19 17:14:321558
[email protected]8ef78fd2010-08-19 17:14:321559 if (extension->is_theme()) {
[email protected]483d1ff2011-03-03 17:12:021560 DCHECK(pending_extension_info.enable_on_install());
[email protected]4416c5a2010-06-26 01:28:571561 initial_state = Extension::ENABLED;
[email protected]483d1ff2011-03-03 17:12:021562 DCHECK(!pending_extension_info.enable_incognito_on_install());
[email protected]4416c5a2010-06-26 01:28:571563 initial_enable_incognito = false;
1564 } else {
1565 initial_state =
[email protected]483d1ff2011-03-03 17:12:021566 pending_extension_info.enable_on_install() ?
[email protected]4416c5a2010-06-26 01:28:571567 Extension::ENABLED : Extension::DISABLED;
1568 initial_enable_incognito =
[email protected]483d1ff2011-03-03 17:12:021569 pending_extension_info.enable_incognito_on_install();
[email protected]4416c5a2010-06-26 01:28:571570 }
[email protected]4416c5a2010-06-26 01:28:571571 } else {
[email protected]dbec3792010-08-10 00:08:451572 // Make sure we preserve enabled/disabled states.
[email protected]4416c5a2010-06-26 01:28:571573 Extension::State existing_state =
1574 extension_prefs_->GetExtensionState(extension->id());
1575 initial_state =
1576 (existing_state == Extension::DISABLED) ?
1577 Extension::DISABLED : Extension::ENABLED;
[email protected]dbec3792010-08-10 00:08:451578 initial_enable_incognito =
1579 extension_prefs_->IsIncognitoEnabled(extension->id());
[email protected]aa142702010-03-26 01:26:331580 }
1581
[email protected]9b217652010-10-08 22:04:231582 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
[email protected]7fa19f82010-12-21 19:40:081583 extension->GetType(), 100);
[email protected]eda3c362010-11-12 08:08:231584 ShownSectionsHandler::OnExtensionInstalled(profile_->GetPrefs(), extension);
[email protected]4416c5a2010-06-26 01:28:571585 extension_prefs_->OnExtensionInstalled(
1586 extension, initial_state, initial_enable_incognito);
[email protected]25b34332009-06-05 21:53:191587
[email protected]92a5b1d2010-07-20 00:42:001588 // Unpacked extensions start off with file access since they are a developer
1589 // feature.
1590 if (extension->location() == Extension::LOAD)
1591 extension_prefs_->SetAllowFileAccess(extension->id(), true);
1592
[email protected]4a190632009-05-09 01:07:421593 // If the extension is a theme, tell the profile (and therefore ThemeProvider)
1594 // to apply it.
[email protected]3ba0fd32010-06-19 05:39:101595 if (extension->is_theme()) {
[email protected]9ceb07342009-07-26 04:09:231596 NotificationService::current()->Notify(
1597 NotificationType::THEME_INSTALLED,
[email protected]24e7a9d2009-11-04 11:11:341598 Source<Profile>(profile_),
[email protected]9adb9692010-10-29 23:14:021599 Details<const Extension>(extension));
[email protected]9197f3b2009-06-02 00:49:271600 } else {
1601 NotificationService::current()->Notify(
1602 NotificationType::EXTENSION_INSTALLED,
[email protected]24e7a9d2009-11-04 11:11:341603 Source<Profile>(profile_),
[email protected]9adb9692010-10-29 23:14:021604 Details<const Extension>(extension));
[email protected]4a190632009-05-09 01:07:421605 }
[email protected]7577a5c52009-07-30 06:21:581606
[email protected]ec5b50d2010-10-09 16:35:181607 if (extension->is_app()) {
1608 ExtensionIdSet installed_ids = GetAppIds();
1609 installed_ids.insert(extension->id());
1610 default_apps_.DidInstallApp(installed_ids);
1611 }
1612
[email protected]49cd1572011-02-08 21:38:451613 // Transfer ownership of |extension| to AddExtension.
1614 AddExtension(scoped_extension);
[email protected]4a190632009-05-09 01:07:421615}
1616
[email protected]eaa7dd182010-12-14 11:09:001617const Extension* ExtensionService::GetExtensionByIdInternal(
[email protected]9adb9692010-10-29 23:14:021618 const std::string& id, bool include_enabled, bool include_disabled) {
[email protected]e957fe52009-06-23 16:51:051619 std::string lowercase_id = StringToLowerASCII(id);
[email protected]0c6da502009-08-14 22:32:391620 if (include_enabled) {
1621 for (ExtensionList::const_iterator iter = extensions_.begin();
1622 iter != extensions_.end(); ++iter) {
1623 if ((*iter)->id() == lowercase_id)
1624 return *iter;
1625 }
1626 }
1627 if (include_disabled) {
1628 for (ExtensionList::const_iterator iter = disabled_extensions_.begin();
1629 iter != disabled_extensions_.end(); ++iter) {
1630 if ((*iter)->id() == lowercase_id)
1631 return *iter;
1632 }
[email protected]ce5c4502009-05-06 16:46:111633 }
1634 return NULL;
1635}
1636
[email protected]bb7f40952011-01-13 00:21:201637void ExtensionService::TrackTerminatedExtension(const Extension* extension) {
1638 if (terminated_extension_ids_.insert(extension->id()).second)
1639 terminated_extensions_.push_back(make_scoped_refptr(extension));
1640}
1641
1642void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
1643 if (terminated_extension_ids_.erase(id) <= 0)
1644 return;
1645
1646 std::string lowercase_id = StringToLowerASCII(id);
1647 for (ExtensionList::iterator iter = terminated_extensions_.begin();
1648 iter != terminated_extensions_.end(); ++iter) {
1649 if ((*iter)->id() == lowercase_id) {
1650 terminated_extensions_.erase(iter);
1651 return;
1652 }
1653 }
1654}
1655
[email protected]0dfe05c2011-02-23 23:03:361656const Extension* ExtensionService::GetTerminatedExtension(
1657 const std::string& id) {
1658 std::string lowercase_id = StringToLowerASCII(id);
1659 for (ExtensionList::const_iterator iter = terminated_extensions_.begin();
1660 iter != terminated_extensions_.end(); ++iter) {
1661 if ((*iter)->id() == lowercase_id)
1662 return *iter;
1663 }
1664 return NULL;
1665}
1666
[email protected]eaa7dd182010-12-14 11:09:001667const Extension* ExtensionService::GetWebStoreApp() {
[email protected]d3071992010-10-08 15:24:071668 return GetExtensionById(extension_misc::kWebStoreAppId, false);
1669}
1670
[email protected]eaa7dd182010-12-14 11:09:001671const Extension* ExtensionService::GetExtensionByURL(const GURL& url) {
[email protected]a888b29e62010-04-01 13:38:571672 return url.scheme() != chrome::kExtensionScheme ? NULL :
1673 GetExtensionById(url.host(), false);
1674}
1675
[email protected]eaa7dd182010-12-14 11:09:001676const Extension* ExtensionService::GetExtensionByWebExtent(const GURL& url) {
[email protected]a888b29e62010-04-01 13:38:571677 for (size_t i = 0; i < extensions_.size(); ++i) {
1678 if (extensions_[i]->web_extent().ContainsURL(url))
1679 return extensions_[i];
1680 }
1681 return NULL;
[email protected]9f1087e2009-06-15 17:29:321682}
1683
[email protected]eaa7dd182010-12-14 11:09:001684bool ExtensionService::ExtensionBindingsAllowed(const GURL& url) {
[email protected]583d45c12010-08-31 02:48:121685 // Allow bindings for all packaged extension.
1686 if (GetExtensionByURL(url))
1687 return true;
1688
1689 // Allow bindings for all component, hosted apps.
[email protected]9adb9692010-10-29 23:14:021690 const Extension* extension = GetExtensionByWebExtent(url);
[email protected]583d45c12010-08-31 02:48:121691 return (extension && extension->location() == Extension::COMPONENT);
1692}
1693
[email protected]eaa7dd182010-12-14 11:09:001694const Extension* ExtensionService::GetExtensionByOverlappingWebExtent(
[email protected]9f72aa02010-06-25 10:01:051695 const ExtensionExtent& extent) {
[email protected]22c966c2010-06-26 06:35:021696 for (size_t i = 0; i < extensions_.size(); ++i) {
1697 if (extensions_[i]->web_extent().OverlapsWith(extent))
1698 return extensions_[i];
1699 }
1700
[email protected]6d2e60bd2010-06-03 22:37:391701 return NULL;
1702}
1703
[email protected]eaa7dd182010-12-14 11:09:001704const SkBitmap& ExtensionService::GetOmniboxIcon(
[email protected]b671760b2010-07-15 21:13:471705 const std::string& extension_id) {
1706 return omnibox_icon_manager_.GetIcon(extension_id);
1707}
1708
[email protected]eaa7dd182010-12-14 11:09:001709const SkBitmap& ExtensionService::GetOmniboxPopupIcon(
[email protected]29d0d4ac2010-09-08 21:10:311710 const std::string& extension_id) {
1711 return omnibox_popup_icon_manager_.GetIcon(extension_id);
1712}
1713
[email protected]eaa7dd182010-12-14 11:09:001714void ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:321715 const std::string& id,
[email protected]8e4560b62011-01-14 10:09:141716 const Version* version,
[email protected]8ef78fd2010-08-19 17:14:321717 const FilePath& path,
1718 Extension::Location location) {
[email protected]ab22ba42011-01-14 16:36:381719 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1720 CHECK(Extension::IdIsValid(id));
[email protected]683d0702010-12-06 16:25:571721 if (extension_prefs_->IsExtensionKilled(id))
1722 return;
[email protected]a8af9fdb2010-10-28 21:52:201723
[email protected]8e4560b62011-01-14 10:09:141724 DCHECK(version);
1725
[email protected]7577a5c52009-07-30 06:21:581726 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:491727 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:581728 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:021729 const Extension* existing = GetExtensionById(id, true);
[email protected]7577a5c52009-07-30 06:21:581730 if (existing) {
[email protected]8e4560b62011-01-14 10:09:141731 switch (existing->version()->CompareTo(*version)) {
[email protected]7577a5c52009-07-30 06:21:581732 case -1: // existing version is older, we should upgrade
1733 break;
1734 case 0: // existing version is same, do nothing
1735 return;
1736 case 1: // existing version is newer, uh-oh
1737 LOG(WARNING) << "Found external version of extension " << id
1738 << "that is older than current version. Current version "
1739 << "is: " << existing->VersionString() << ". New version "
1740 << "is: " << version << ". Keeping current version.";
1741 return;
1742 }
1743 }
1744
[email protected]9c635f22010-12-02 09:36:361745 GURL update_url = GURL();
[email protected]9c635f22010-12-02 09:36:361746 bool is_from_sync = false;
1747 bool install_silently = true;
1748 bool enable_on_install = true;
1749 bool enable_incognito_on_install = false;
1750 pending_extensions_[id] = PendingExtensionInfo(
1751 update_url,
[email protected]7fa19f82010-12-21 19:40:081752 &AlwaysInstall,
[email protected]9c635f22010-12-02 09:36:361753 is_from_sync,
1754 install_silently,
1755 enable_on_install,
1756 enable_incognito_on_install,
1757 location);
1758
[email protected]6dfbbf82010-03-12 23:09:161759 scoped_refptr<CrxInstaller> installer(
[email protected]bb461532010-11-26 21:50:231760 new CrxInstaller(this, // frontend
[email protected]6dfbbf82010-03-12 23:09:161761 NULL)); // no client (silent install)
1762 installer->set_install_source(location);
1763 installer->set_expected_id(id);
[email protected]604322d2011-03-22 16:51:561764 installer->set_expected_version(*version),
[email protected]6dfbbf82010-03-12 23:09:161765 installer->InstallCrx(path);
[email protected]7577a5c52009-07-30 06:21:581766}
1767
[email protected]eaa7dd182010-12-14 11:09:001768void ExtensionService::ReportExtensionLoadError(
[email protected]d11c8e92009-10-20 23:26:401769 const FilePath& extension_path,
1770 const std::string &error,
1771 NotificationType type,
1772 bool be_noisy) {
1773 NotificationService* service = NotificationService::current();
1774 service->Notify(type,
[email protected]24e7a9d2009-11-04 11:11:341775 Source<Profile>(profile_),
[email protected]d11c8e92009-10-20 23:26:401776 Details<const std::string>(&error));
1777
[email protected]8a205c02011-02-04 20:41:331778 std::string path_str = UTF16ToUTF8(extension_path.LossyDisplayName());
[email protected]18d4b6c2010-09-21 03:21:041779 std::string message = base::StringPrintf(
1780 "Could not load extension from '%s'. %s",
1781 path_str.c_str(), error.c_str());
[email protected]d11c8e92009-10-20 23:26:401782 ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy);
1783}
1784
[email protected]eaa7dd182010-12-14 11:09:001785void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]406027c02010-09-27 08:03:181786 ExtensionHost* host) {
1787 OrphanedDevTools::iterator iter =
1788 orphaned_dev_tools_.find(host->extension()->id());
1789 if (iter == orphaned_dev_tools_.end())
1790 return;
1791
1792 DevToolsManager::GetInstance()->AttachClientHost(
1793 iter->second, host->render_view_host());
1794 orphaned_dev_tools_.erase(iter);
1795}
1796
[email protected]eaa7dd182010-12-14 11:09:001797void ExtensionService::Observe(NotificationType type,
[email protected]4814b512009-11-07 00:12:291798 const NotificationSource& source,
1799 const NotificationDetails& details) {
1800 switch (type.value) {
[email protected]a4ed6282009-12-14 20:51:161801 case NotificationType::EXTENSION_PROCESS_TERMINATED: {
[email protected]bc535ee52010-08-31 18:40:321802 if (profile_ != Source<Profile>(source).ptr()->GetOriginalProfile())
1803 break;
[email protected]a4ed6282009-12-14 20:51:161804
[email protected]f128af42010-08-05 18:05:261805 ExtensionHost* host = Details<ExtensionHost>(details).ptr();
[email protected]bb7f40952011-01-13 00:21:201806 TrackTerminatedExtension(host->extension());
[email protected]f128af42010-08-05 18:05:261807
[email protected]31f77262009-12-02 20:48:531808 // Unload the entire extension. We want it to be in a consistent state:
1809 // either fully working or not loaded at all, but never half-crashed.
[email protected]bc535ee52010-08-31 18:40:321810 // We do it in a PostTask so that other handlers of this notification will
1811 // still have access to the Extension and ExtensionHost.
1812 MessageLoop::current()->PostTask(FROM_HERE,
[email protected]a9f39a312010-12-23 22:14:271813 NewRunnableMethod(this,
1814 &ExtensionService::UnloadExtension,
1815 host->extension()->id(),
1816 UnloadedExtensionInfo::DISABLE));
[email protected]31f77262009-12-02 20:48:531817 break;
1818 }
1819
[email protected]aa96d3a2010-08-21 08:45:251820 case NotificationType::PREF_CHANGED: {
1821 std::string* pref_name = Details<std::string>(details).ptr();
[email protected]6c751e72010-11-23 10:11:101822 if (*pref_name == prefs::kExtensionInstallAllowList ||
1823 *pref_name == prefs::kExtensionInstallDenyList) {
1824 CheckAdminBlacklist();
[email protected]6c751e72010-11-23 10:11:101825 } else {
1826 NOTREACHED() << "Unexpected preference name.";
1827 }
[email protected]aa96d3a2010-08-21 08:45:251828 break;
1829 }
1830
[email protected]4814b512009-11-07 00:12:291831 default:
1832 NOTREACHED() << "Unexpected notification type.";
1833 }
1834}
1835
[email protected]eaa7dd182010-12-14 11:09:001836bool ExtensionService::HasApps() const {
[email protected]ec5b50d2010-10-09 16:35:181837 return !GetAppIds().empty();
1838}
[email protected]377011d2010-07-20 04:18:501839
[email protected]eaa7dd182010-12-14 11:09:001840ExtensionIdSet ExtensionService::GetAppIds() const {
[email protected]ec5b50d2010-10-09 16:35:181841 ExtensionIdSet result;
[email protected]377011d2010-07-20 04:18:501842 for (ExtensionList::const_iterator it = extensions_.begin();
1843 it != extensions_.end(); ++it) {
[email protected]ec5b50d2010-10-09 16:35:181844 if ((*it)->is_app() && (*it)->location() != Extension::COMPONENT)
1845 result.insert((*it)->id());
[email protected]377011d2010-07-20 04:18:501846 }
1847
[email protected]ec5b50d2010-10-09 16:35:181848 return result;
[email protected]377011d2010-07-20 04:18:501849}
[email protected]d7e9a862010-11-03 21:57:491850
[email protected]eaa7dd182010-12-14 11:09:001851bool ExtensionService::IsBackgroundPageReady(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:491852 return (extension->background_url().is_empty() ||
1853 extension_runtime_data_[extension->id()].background_page_ready);
1854}
1855
[email protected]eaa7dd182010-12-14 11:09:001856void ExtensionService::SetBackgroundPageReady(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:491857 DCHECK(!extension->background_url().is_empty());
1858 extension_runtime_data_[extension->id()].background_page_ready = true;
1859 NotificationService::current()->Notify(
1860 NotificationType::EXTENSION_BACKGROUND_PAGE_READY,
1861 Source<const Extension>(extension),
1862 NotificationService::NoDetails());
1863}
1864
[email protected]eaa7dd182010-12-14 11:09:001865bool ExtensionService::IsBeingUpgraded(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:491866 return extension_runtime_data_[extension->id()].being_upgraded;
1867}
1868
[email protected]eaa7dd182010-12-14 11:09:001869void ExtensionService::SetBeingUpgraded(const Extension* extension,
[email protected]d7e9a862010-11-03 21:57:491870 bool value) {
1871 extension_runtime_data_[extension->id()].being_upgraded = value;
1872}
[email protected]1bead0712010-11-27 17:41:531873
[email protected]eaa7dd182010-12-14 11:09:001874PropertyBag* ExtensionService::GetPropertyBag(const Extension* extension) {
[email protected]1bead0712010-11-27 17:41:531875 return &extension_runtime_data_[extension->id()].property_bag;
1876}