blob: cb99385e0b63c712df2dfdb05cd0a57f8bf18636 [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]b2907fd2011-03-25 16:43:3746#include "chrome/browser/extensions/pending_extension_manager.h"
[email protected]56ad3792010-05-28 17:45:3347#include "chrome/browser/net/chrome_url_request_context.h"
[email protected]37858e52010-08-26 00:22:0248#include "chrome/browser/prefs/pref_service.h"
[email protected]8ecad5e2010-12-02 21:18:3349#include "chrome/browser/profiles/profile.h"
[email protected]56ad3792010-05-28 17:45:3350#include "chrome/browser/search_engines/template_url_model.h"
[email protected]18280372011-03-22 18:05:2251#include "chrome/browser/themes/theme_service.h"
[email protected]a0ea76c2011-03-23 17:36:4252#include "chrome/browser/themes/theme_service_factory.h"
[email protected]fd42ac30f2011-02-27 19:33:3653#include "chrome/browser/ui/webui/shown_sections_handler.h"
[email protected]aab98a52009-12-02 03:22:3554#include "chrome/common/child_process_logging.h"
[email protected]e2eb43112009-05-29 21:19:5455#include "chrome/common/chrome_switches.h"
[email protected]5b1a0e22009-05-26 19:00:5856#include "chrome/common/extensions/extension.h"
[email protected]d7b36dc2009-10-29 21:47:4057#include "chrome/common/extensions/extension_constants.h"
[email protected]05c82182010-06-24 17:49:0858#include "chrome/common/extensions/extension_error_utils.h"
[email protected]7c927b62010-02-24 09:54:1359#include "chrome/common/extensions/extension_file_util.h"
[email protected]c6d474f82009-12-16 21:11:0660#include "chrome/common/extensions/extension_l10n_util.h"
[email protected]a315ba92010-11-16 14:12:2161#include "chrome/common/extensions/extension_resource.h"
[email protected]42b795652011-02-22 23:32:3862#include "chrome/common/json_value_serializer.h"
[email protected]25b34332009-06-05 21:53:1963#include "chrome/common/pref_names.h"
[email protected]a57209872009-05-04 22:53:1464#include "chrome/common/url_constants.h"
[email protected]5f945a0e2011-03-01 17:47:5365#include "content/browser/browser_thread.h"
[email protected]b3841c502011-03-09 01:21:3166#include "content/common/notification_service.h"
67#include "content/common/notification_type.h"
[email protected]c10da4b02010-03-25 14:38:3268#include "googleurl/src/gurl.h"
[email protected]1debbbb62010-10-06 17:23:4469#include "net/base/registry_controlled_domain.h"
[email protected]24b538a2010-02-27 01:22:4470#include "webkit/database/database_tracker.h"
71#include "webkit/database/database_util.h"
[email protected]c64631652009-04-29 22:24:3172
[email protected]5ef47ec2010-01-28 05:58:0573using base::Time;
74
[email protected]c6d474f82009-12-16 21:11:0675namespace errors = extension_manifest_errors;
76
[email protected]b6ab96d2009-08-20 18:58:1977namespace {
78
[email protected]29d0d4ac2010-09-08 21:10:3179#if defined(OS_LINUX)
80static const int kOmniboxIconPaddingLeft = 2;
81static const int kOmniboxIconPaddingRight = 2;
82#elif defined(OS_MACOSX)
83static const int kOmniboxIconPaddingLeft = 0;
84static const int kOmniboxIconPaddingRight = 2;
85#else
86static const int kOmniboxIconPaddingLeft = 0;
87static const int kOmniboxIconPaddingRight = 0;
88#endif
89
[email protected]a315ba92010-11-16 14:12:2190// The following enumeration is used in histograms matching
91// Extensions.ManifestReload* . Values may be added, as long
92// as existing values are not changed.
93enum ManifestReloadReason {
94 NOT_NEEDED = 0, // Reload not needed.
95 UNPACKED_DIR, // Unpacked directory
96 NEEDS_RELOCALIZATION, // The local has changed since we read this extension.
97 NUM_MANIFEST_RELOAD_REASONS
98};
[email protected]2111b1a2010-03-12 18:12:4499
[email protected]a315ba92010-11-16 14:12:21100ManifestReloadReason ShouldReloadExtensionManifest(const ExtensionInfo& info) {
101 // Always reload manifests of unpacked extensions, because they can change
102 // on disk independent of the manifest in our prefs.
103 if (info.extension_location == Extension::LOAD)
104 return UNPACKED_DIR;
105
106 // Reload the manifest if it needs to be relocalized.
107 if (extension_l10n_util::ShouldRelocalizeManifest(info))
108 return NEEDS_RELOCALIZATION;
109
110 return NOT_NEEDED;
[email protected]2111b1a2010-03-12 18:12:44111}
112
[email protected]c6d474f82009-12-16 21:11:06113} // namespace
[email protected]b6ab96d2009-08-20 18:58:19114
[email protected]d7e9a862010-11-03 21:57:49115
[email protected]eaa7dd182010-12-14 11:09:00116ExtensionService::ExtensionRuntimeData::ExtensionRuntimeData()
[email protected]d7e9a862010-11-03 21:57:49117 : background_page_ready(false),
118 being_upgraded(false) {
119}
120
[email protected]eaa7dd182010-12-14 11:09:00121ExtensionService::ExtensionRuntimeData::~ExtensionRuntimeData() {
[email protected]d7e9a862010-11-03 21:57:49122}
123
[email protected]eaa7dd182010-12-14 11:09:00124// ExtensionService.
[email protected]6014d672008-12-05 00:38:25125
[email protected]eaa7dd182010-12-14 11:09:00126const char* ExtensionService::kInstallDirectoryName = "Extensions";
127const char* ExtensionService::kCurrentVersionFileName = "Current Version";
[email protected]494c06e2009-07-25 01:06:42128
[email protected]eaa7dd182010-12-14 11:09:00129// Implements IO for the ExtensionService.
[email protected]7a4c6852010-09-16 03:44:22130
[email protected]eaa7dd182010-12-14 11:09:00131class ExtensionServiceBackend
[email protected]8e4560b62011-01-14 10:09:14132 : public base::RefCountedThreadSafe<ExtensionServiceBackend> {
[email protected]7a4c6852010-09-16 03:44:22133 public:
[email protected]1f830eb2010-09-28 08:25:14134 // |install_directory| is a path where to look for extensions to load.
[email protected]8e4560b62011-01-14 10:09:14135 explicit ExtensionServiceBackend(const FilePath& install_directory);
[email protected]7a4c6852010-09-16 03:44:22136
137 // Loads a single extension from |path| where |path| is the top directory of
138 // a specific extension where its manifest file lives.
139 // Errors are reported through ExtensionErrorReporter. On success,
[email protected]49cd1572011-02-08 21:38:45140 // AddExtension() is called.
[email protected]7a4c6852010-09-16 03:44:22141 // TODO(erikkay): It might be useful to be able to load a packed extension
142 // (presumably into memory) without installing it.
143 void LoadSingleExtension(const FilePath &path,
[email protected]eaa7dd182010-12-14 11:09:00144 scoped_refptr<ExtensionService> frontend);
[email protected]7a4c6852010-09-16 03:44:22145
[email protected]7a4c6852010-09-16 03:44:22146 private:
[email protected]eaa7dd182010-12-14 11:09:00147 friend class base::RefCountedThreadSafe<ExtensionServiceBackend>;
[email protected]7a4c6852010-09-16 03:44:22148
[email protected]eaa7dd182010-12-14 11:09:00149 virtual ~ExtensionServiceBackend();
[email protected]7a4c6852010-09-16 03:44:22150
151 // Finish installing the extension in |crx_path| after it has been unpacked to
152 // |unpacked_path|. If |expected_id| is not empty, it's verified against the
153 // extension's manifest before installation. If |silent| is true, there will
154 // be no install confirmation dialog. |from_gallery| indicates whether the
155 // crx was installed from our gallery, which results in different UI.
156 //
157 // Note: We take ownership of |extension|.
158 void OnExtensionUnpacked(const FilePath& crx_path,
159 const FilePath& unpacked_path,
[email protected]9adb9692010-10-29 23:14:02160 const Extension* extension,
[email protected]7a4c6852010-09-16 03:44:22161 const std::string expected_id);
162
163 // Notify the frontend that there was an error loading an extension.
164 void ReportExtensionLoadError(const FilePath& extension_path,
165 const std::string& error);
166
[email protected]7a4c6852010-09-16 03:44:22167 // This is a naked pointer which is set by each entry point.
168 // The entry point is responsible for ensuring lifetime.
[email protected]eaa7dd182010-12-14 11:09:00169 ExtensionService* frontend_;
[email protected]7a4c6852010-09-16 03:44:22170
171 // The top-level extensions directory being installed to.
172 FilePath install_directory_;
173
174 // Whether errors result in noisy alerts.
175 bool alert_on_error_;
176
[email protected]eaa7dd182010-12-14 11:09:00177 DISALLOW_COPY_AND_ASSIGN(ExtensionServiceBackend);
[email protected]7a4c6852010-09-16 03:44:22178};
179
[email protected]eaa7dd182010-12-14 11:09:00180ExtensionServiceBackend::ExtensionServiceBackend(
[email protected]aebe23a32010-12-10 22:15:48181 const FilePath& install_directory)
[email protected]7a4c6852010-09-16 03:44:22182 : frontend_(NULL),
183 install_directory_(install_directory),
[email protected]8e4560b62011-01-14 10:09:14184 alert_on_error_(false) {
[email protected]7a4c6852010-09-16 03:44:22185}
186
[email protected]eaa7dd182010-12-14 11:09:00187ExtensionServiceBackend::~ExtensionServiceBackend() {
[email protected]7a4c6852010-09-16 03:44:22188}
189
[email protected]eaa7dd182010-12-14 11:09:00190void ExtensionServiceBackend::LoadSingleExtension(
191 const FilePath& path_in, scoped_refptr<ExtensionService> frontend) {
[email protected]a29a517a2011-01-21 21:11:12192 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]a8af9fdb2010-10-28 21:52:20193
[email protected]7a4c6852010-09-16 03:44:22194 frontend_ = frontend;
195
196 // Explicit UI loads are always noisy.
197 alert_on_error_ = true;
198
199 FilePath extension_path = path_in;
200 file_util::AbsolutePath(&extension_path);
201
[email protected]83048a22011-03-29 00:14:13202 int flags = Extension::NO_FLAGS;
203 if (Extension::ShouldDoStrictErrorChecking(Extension::LOAD))
204 flags |= Extension::STRICT_ERROR_CHECKS;
[email protected]7a4c6852010-09-16 03:44:22205 std::string error;
[email protected]ad8e04a2010-11-01 04:16:27206 scoped_refptr<const Extension> extension(extension_file_util::LoadExtension(
[email protected]7a4c6852010-09-16 03:44:22207 extension_path,
[email protected]92888082010-10-18 19:24:57208 Extension::LOAD,
[email protected]83048a22011-03-29 00:14:13209 flags,
[email protected]ad8e04a2010-11-01 04:16:27210 &error));
[email protected]7a4c6852010-09-16 03:44:22211
212 if (!extension) {
213 ReportExtensionLoadError(extension_path, error);
214 return;
215 }
216
[email protected]7a4c6852010-09-16 03:44:22217 // Report this as an installed extension so that it gets remembered in the
218 // prefs.
[email protected]ca4b5fa32010-10-09 12:42:18219 BrowserThread::PostTask(
220 BrowserThread::UI, FROM_HERE,
[email protected]8d888c12010-11-30 00:00:25221 NewRunnableMethod(frontend_,
[email protected]eaa7dd182010-12-14 11:09:00222 &ExtensionService::OnExtensionInstalled,
[email protected]8d888c12010-11-30 00:00:25223 extension));
[email protected]7a4c6852010-09-16 03:44:22224}
225
[email protected]eaa7dd182010-12-14 11:09:00226void ExtensionServiceBackend::ReportExtensionLoadError(
[email protected]7a4c6852010-09-16 03:44:22227 const FilePath& extension_path, const std::string &error) {
[email protected]a29a517a2011-01-21 21:11:12228 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]ca4b5fa32010-10-09 12:42:18229 BrowserThread::PostTask(
230 BrowserThread::UI, FROM_HERE,
[email protected]7a4c6852010-09-16 03:44:22231 NewRunnableMethod(
232 frontend_,
[email protected]eaa7dd182010-12-14 11:09:00233 &ExtensionService::ReportExtensionLoadError, extension_path,
[email protected]7a4c6852010-09-16 03:44:22234 error, NotificationType::EXTENSION_INSTALL_ERROR, alert_on_error_));
235}
236
[email protected]8e4560b62011-01-14 10:09:14237void ExtensionService::CheckExternalUninstall(const std::string& id) {
[email protected]a29a517a2011-01-21 21:11:12238 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20239
[email protected]7a4c6852010-09-16 03:44:22240 // Check if the providers know about this extension.
[email protected]0a60a2e2010-10-25 16:15:21241 ProviderCollection::const_iterator i;
242 for (i = external_extension_providers_.begin();
243 i != external_extension_providers_.end(); ++i) {
[email protected]8e4560b62011-01-14 10:09:14244 DCHECK(i->get()->IsReady());
[email protected]0a60a2e2010-10-25 16:15:21245 if (i->get()->HasExtension(id))
246 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22247 }
248
[email protected]7a4c6852010-09-16 03:44:22249 // This is an external extension that we don't have registered. Uninstall.
[email protected]8e4560b62011-01-14 10:09:14250 UninstallExtension(id, true);
[email protected]7a4c6852010-09-16 03:44:22251}
252
[email protected]8e4560b62011-01-14 10:09:14253void ExtensionService::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22254 external_extension_providers_.clear();
255}
256
[email protected]8e4560b62011-01-14 10:09:14257void ExtensionService::AddProviderForTesting(
258 ExternalExtensionProviderInterface* test_provider) {
[email protected]a29a517a2011-01-21 21:11:12259 CHECK(test_provider);
[email protected]0a60a2e2010-10-25 16:15:21260 external_extension_providers_.push_back(
[email protected]8e4560b62011-01-14 10:09:14261 linked_ptr<ExternalExtensionProviderInterface>(test_provider));
[email protected]7a4c6852010-09-16 03:44:22262}
263
[email protected]8e4560b62011-01-14 10:09:14264void ExtensionService::OnExternalExtensionUpdateUrlFound(
[email protected]7a4c6852010-09-16 03:44:22265 const std::string& id,
[email protected]21a5a672010-11-04 10:47:42266 const GURL& update_url,
267 Extension::Location location) {
[email protected]ab22ba42011-01-14 16:36:38268 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
269 CHECK(Extension::IdIsValid(id));
[email protected]a8af9fdb2010-10-28 21:52:20270
[email protected]8e4560b62011-01-14 10:09:14271 if (GetExtensionById(id, true)) {
[email protected]7a4c6852010-09-16 03:44:22272 // Already installed. Do not change the update URL that the extension set.
273 return;
274 }
[email protected]b2907fd2011-03-25 16:43:37275 pending_extension_manager()->AddFromExternalUpdateUrl(
276 id, update_url, location);
[email protected]a29a517a2011-01-21 21:11:12277 external_extension_url_added_ |= true;
[email protected]7a4c6852010-09-16 03:44:22278}
279
[email protected]eaa7dd182010-12-14 11:09:00280bool ExtensionService::IsDownloadFromGallery(const GURL& download_url,
[email protected]a29a517a2011-01-21 21:11:12281 const GURL& referrer_url) {
[email protected]d3071992010-10-08 15:24:07282 // Special-case the themes mini-gallery.
283 // TODO(erikkay) When that gallery goes away, remove this code.
284 if (IsDownloadFromMiniGallery(download_url) &&
285 StartsWithASCII(referrer_url.spec(),
286 extension_urls::kMiniGalleryBrowsePrefix, false)) {
[email protected]334e04a2010-06-24 23:34:44287 return true;
[email protected]1debbbb62010-10-06 17:23:44288 }
[email protected]473ff6e2010-05-12 15:31:55289
[email protected]9adb9692010-10-29 23:14:02290 const Extension* download_extension = GetExtensionByWebExtent(download_url);
291 const Extension* referrer_extension = GetExtensionByWebExtent(referrer_url);
292 const Extension* webstore_app = GetWebStoreApp();
[email protected]d3071992010-10-08 15:24:07293
294 bool referrer_valid = (referrer_extension == webstore_app);
295 bool download_valid = (download_extension == webstore_app);
296
297 // If the command-line gallery URL is set, then be a bit more lenient.
298 GURL store_url =
299 GURL(CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
300 switches::kAppsGalleryURL));
301 if (!store_url.is_empty()) {
302 std::string store_tld =
303 net::RegistryControlledDomainService::GetDomainAndRegistry(store_url);
304 if (!referrer_valid) {
305 std::string referrer_tld =
306 net::RegistryControlledDomainService::GetDomainAndRegistry(
307 referrer_url);
308 // The referrer gets stripped when transitioning from https to http,
309 // or when hitting an unknown test cert and that commonly happens in
310 // testing environments. Given this, we allow an empty referrer when
311 // the command-line flag is set.
312 // Otherwise, the TLD must match the TLD of the command-line url.
313 referrer_valid = referrer_url.is_empty() || (referrer_tld == store_tld);
314 }
315
316 if (!download_valid) {
317 std::string download_tld =
318 net::RegistryControlledDomainService::GetDomainAndRegistry(
319 GURL(download_url));
320
321 // Otherwise, the TLD must match the TLD of the command-line url.
322 download_valid = (download_tld == store_tld);
323 }
324 }
325
326 return (referrer_valid && download_valid);
[email protected]b7c2f252009-12-08 00:47:23327}
328
[email protected]eaa7dd182010-12-14 11:09:00329bool ExtensionService::IsDownloadFromMiniGallery(const GURL& download_url) {
[email protected]ac025282009-12-16 19:16:38330 return StartsWithASCII(download_url.spec(),
331 extension_urls::kMiniGalleryDownloadPrefix,
332 false); // case_sensitive
333}
334
[email protected]d9696672011-03-15 22:45:09335const Extension* ExtensionService::GetInstalledApp(const GURL& url) {
[email protected]ffb204f22010-12-05 23:20:27336 // Check for hosted app.
[email protected]d9696672011-03-15 22:45:09337 const Extension* app = GetExtensionByWebExtent(url);
338 if (app)
339 return app;
[email protected]ffb204f22010-12-05 23:20:27340
341 // Check for packaged app.
[email protected]d9696672011-03-15 22:45:09342 app = GetExtensionByURL(url);
343 if (app && app->is_app())
344 return app;
345
346 return NULL;
347}
348
349bool ExtensionService::IsInstalledApp(const GURL& url) {
350 return !!GetInstalledApp(url);
[email protected]ffb204f22010-12-05 23:20:27351}
352
[email protected]6aeac8342010-10-01 20:21:18353// static
[email protected]eaa7dd182010-12-14 11:09:00354bool ExtensionService::UninstallExtensionHelper(
355 ExtensionService* extensions_service,
[email protected]6aeac8342010-10-01 20:21:18356 const std::string& extension_id) {
[email protected]6aeac8342010-10-01 20:21:18357
358 // We can't call UninstallExtension with an invalid extension ID, so check it
359 // first.
[email protected]0dfe05c2011-02-23 23:03:36360 if (extensions_service->GetExtensionById(extension_id, true) ||
361 extensions_service->GetTerminatedExtension(extension_id)) {
[email protected]6aeac8342010-10-01 20:21:18362 extensions_service->UninstallExtension(extension_id, false);
363 } else {
364 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]a8af9fdb2010-10-28 21:52:20365 << "id: " << extension_id;
[email protected]6aeac8342010-10-01 20:21:18366 return false;
367 }
368
369 return true;
370}
371
[email protected]eaa7dd182010-12-14 11:09:00372ExtensionService::ExtensionService(Profile* profile,
[email protected]f0841cd2011-01-19 15:07:24373 const CommandLine* command_line,
374 const FilePath& install_directory,
375 ExtensionPrefs* extension_prefs,
376 bool autoupdate_enabled)
[email protected]6ef635e42009-07-26 06:16:12377 : profile_(profile),
[email protected]73c47932010-12-06 18:13:43378 extension_prefs_(extension_prefs),
[email protected]b2907fd2011-03-25 16:43:37379 ALLOW_THIS_IN_INITIALIZER_LIST(pending_extension_manager_(*this)),
[email protected]a9b00ac2009-06-25 21:03:23380 install_directory_(install_directory),
[email protected]6d60703b2009-08-29 01:29:23381 extensions_enabled_(true),
[email protected]e81dba32009-06-19 20:19:13382 show_extensions_prompts_(true),
[email protected]3ecda252010-11-18 19:50:55383 ready_(false),
[email protected]ec5b50d2010-10-09 16:35:18384 ALLOW_THIS_IN_INITIALIZER_LIST(toolbar_model_(this)),
[email protected]2fc0f122011-01-04 23:44:30385 default_apps_(profile->GetPrefs(),
386 g_browser_process->GetApplicationLocale()),
[email protected]b3d62312b12010-10-14 21:10:18387 event_routers_initialized_(false) {
[email protected]a29a517a2011-01-21 21:11:12388 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20389
[email protected]36a784c2009-06-23 06:21:08390 // Figure out if extension installation should be enabled.
[email protected]6d60703b2009-08-29 01:29:23391 if (command_line->HasSwitch(switches::kDisableExtensions)) {
392 extensions_enabled_ = false;
393 } else if (profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions)) {
394 extensions_enabled_ = false;
[email protected]6b75ec32009-08-14 06:37:18395 }
[email protected]36a784c2009-06-23 06:21:08396
[email protected]a4ed6282009-12-14 20:51:16397 registrar_.Add(this, NotificationType::EXTENSION_PROCESS_TERMINATED,
[email protected]bc535ee52010-08-31 18:40:32398 NotificationService::AllSources());
[email protected]2fb7dc982010-09-29 12:24:28399 pref_change_registrar_.Init(profile->GetPrefs());
400 pref_change_registrar_.Add(prefs::kExtensionInstallAllowList, this);
401 pref_change_registrar_.Add(prefs::kExtensionInstallDenyList, this);
[email protected]4814b512009-11-07 00:12:29402
[email protected]93fd78f42009-07-10 16:43:17403 // Set up the ExtensionUpdater
404 if (autoupdate_enabled) {
405 int update_frequency = kDefaultUpdateFrequencySeconds;
406 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25407 base::StringToInt(command_line->GetSwitchValueASCII(
408 switches::kExtensionsUpdateFrequency),
409 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17410 }
[email protected]2fb7dc982010-09-29 12:24:28411 updater_ = new ExtensionUpdater(this,
412 profile->GetPrefs(),
413 update_frequency);
[email protected]93fd78f42009-07-10 16:43:17414 }
415
[email protected]8e4560b62011-01-14 10:09:14416 backend_ = new ExtensionServiceBackend(install_directory_);
417
[email protected]873531342011-03-09 12:16:05418 if (extensions_enabled()) {
419 ExternalExtensionProviderImpl::CreateExternalProviders(
420 this, profile_, &external_extension_providers_);
421 }
[email protected]b671760b2010-07-15 21:13:47422
[email protected]aa96d3a2010-08-21 08:45:25423 // Use monochrome icons for Omnibox icons.
[email protected]29d0d4ac2010-09-08 21:10:31424 omnibox_popup_icon_manager_.set_monochrome(true);
[email protected]b671760b2010-07-15 21:13:47425 omnibox_icon_manager_.set_monochrome(true);
[email protected]29d0d4ac2010-09-08 21:10:31426 omnibox_icon_manager_.set_padding(gfx::Insets(0, kOmniboxIconPaddingLeft,
427 0, kOmniboxIconPaddingRight));
[email protected]6014d672008-12-05 00:38:25428}
429
[email protected]eaa7dd182010-12-14 11:09:00430const ExtensionList* ExtensionService::extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45431 return &extensions_;
432}
433
[email protected]eaa7dd182010-12-14 11:09:00434const ExtensionList* ExtensionService::disabled_extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45435 return &disabled_extensions_;
436}
437
[email protected]bb7f40952011-01-13 00:21:20438const ExtensionList* ExtensionService::terminated_extensions() const {
439 return &terminated_extensions_;
440}
441
[email protected]b2907fd2011-03-25 16:43:37442PendingExtensionManager* ExtensionService::pending_extension_manager() {
443 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45444}
445
[email protected]eaa7dd182010-12-14 11:09:00446bool ExtensionService::HasInstalledExtensions() {
[email protected]ddb1e5a2010-12-13 20:10:45447 return !(extensions_.empty() && disabled_extensions_.empty());
448}
449
[email protected]eaa7dd182010-12-14 11:09:00450ExtensionService::~ExtensionService() {
[email protected]2fb7dc982010-09-29 12:24:28451 DCHECK(!profile_); // Profile should have told us it's going away.
[email protected]9f1087e2009-06-15 17:29:32452 UnloadAllExtensions();
[email protected]94b4ffc2011-02-04 05:55:17453
[email protected]8e4560b62011-01-14 10:09:14454 ProviderCollection::const_iterator i;
455 for (i = external_extension_providers_.begin();
456 i != external_extension_providers_.end(); ++i) {
457 ExternalExtensionProviderInterface* provider = i->get();
458 provider->ServiceShutdown();
459 }
[email protected]6014d672008-12-05 00:38:25460}
461
[email protected]eaa7dd182010-12-14 11:09:00462void ExtensionService::InitEventRouters() {
[email protected]b3d62312b12010-10-14 21:10:18463 if (event_routers_initialized_)
464 return;
465
[email protected]c5ae74ab2010-04-15 18:14:37466 ExtensionHistoryEventRouter::GetInstance()->ObserveProfile(profile_);
467 ExtensionAccessibilityEventRouter::GetInstance()->ObserveProfile(profile_);
[email protected]97d2f1d2011-01-15 00:41:08468 browser_event_router_.reset(new ExtensionBrowserEventRouter(profile_));
469 browser_event_router_->Init();
[email protected]118de7892011-03-16 13:31:40470 preference_event_router_.reset(new ExtensionPreferenceEventRouter(profile_));
[email protected]8e8bb6d2010-12-13 08:18:55471 ExtensionBookmarkEventRouter::GetInstance()->Observe(
[email protected]c5ae74ab2010-04-15 18:14:37472 profile_->GetBookmarkModel());
[email protected]2c5e1e12010-06-10 13:14:44473 ExtensionCookiesEventRouter::GetInstance()->Init();
[email protected]7596ce72010-08-30 05:10:46474 ExtensionManagementEventRouter::GetInstance()->Init();
[email protected]8a661f82010-10-19 21:47:11475 ExtensionProcessesEventRouter::GetInstance()->ObserveProfile(profile_);
[email protected]784688a62010-09-13 07:06:52476 ExtensionWebNavigationEventRouter::GetInstance()->Init();
[email protected]b3d62312b12010-10-14 21:10:18477 event_routers_initialized_ = true;
[email protected]c5ae74ab2010-04-15 18:14:37478}
479
[email protected]b2907fd2011-03-25 16:43:37480const Extension* ExtensionService::GetExtensionById(
481 const std::string& id, bool include_disabled) const {
[email protected]78994ab02010-12-08 18:06:44482 return GetExtensionByIdInternal(id, true, include_disabled);
483}
484
[email protected]eaa7dd182010-12-14 11:09:00485void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12486 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20487
[email protected]fa6a9102010-11-22 15:38:50488 DCHECK(!ready_); // Can't redo init.
[email protected]93fd78f42009-07-10 16:43:17489 DCHECK_EQ(extensions_.size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32490
[email protected]95dd38f2009-10-20 20:09:15491 // Hack: we need to ensure the ResourceDispatcherHost is ready before we load
492 // the first extension, because its members listen for loaded notifications.
493 g_browser_process->resource_dispatcher_host();
494
[email protected]9f1087e2009-06-15 17:29:32495 LoadAllExtensions();
[email protected]894bb502009-05-21 22:39:57496
[email protected]9f1087e2009-06-15 17:29:32497 // TODO(erikkay) this should probably be deferred to a future point
498 // rather than running immediately at startup.
[email protected]93fd78f42009-07-10 16:43:17499 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57500
[email protected]9f1087e2009-06-15 17:29:32501 // TODO(erikkay) this should probably be deferred as well.
502 GarbageCollectExtensions();
[email protected]6014d672008-12-05 00:38:25503}
504
[email protected]eaa7dd182010-12-14 11:09:00505void ExtensionService::UpdateExtension(const std::string& id,
[email protected]9a8c4022011-01-25 14:25:33506 const FilePath& extension_path,
507 const GURL& download_url) {
[email protected]a29a517a2011-01-21 21:11:12508 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20509
[email protected]b2907fd2011-03-25 16:43:37510 PendingExtensionInfo pending_extension_info;
511 bool is_pending_extension = pending_extension_manager_.GetById(
512 id, &pending_extension_info);
[email protected]8ef78fd2010-08-19 17:14:32513
[email protected]5eb375e92010-11-26 07:50:41514 const Extension* extension = GetExtensionByIdInternal(id, true, true);
515 if (!is_pending_extension && !extension) {
[email protected]aa142702010-03-26 01:26:33516 LOG(WARNING) << "Will not update extension " << id
517 << " because it is not installed or pending";
518 // Delete extension_path since we're not creating a CrxInstaller
519 // that would do it for us.
[email protected]ca4b5fa32010-10-09 12:42:18520 BrowserThread::PostTask(
521 BrowserThread::FILE, FROM_HERE,
[email protected]53da2c962011-03-03 17:08:05522 NewRunnableFunction(
523 extension_file_util::DeleteFile, extension_path, false));
[email protected]4c967932009-07-31 01:15:49524 return;
[email protected]e957fe52009-06-23 16:51:05525 }
526
[email protected]aa142702010-03-26 01:26:33527 // We want a silent install only for non-pending extensions and
528 // pending extensions that have install_silently set.
529 ExtensionInstallUI* client =
[email protected]b2907fd2011-03-25 16:43:37530 (!is_pending_extension || pending_extension_info.install_silently()) ?
[email protected]aa142702010-03-26 01:26:33531 NULL : new ExtensionInstallUI(profile_);
532
[email protected]6dfbbf82010-03-12 23:09:16533 scoped_refptr<CrxInstaller> installer(
[email protected]bb461532010-11-26 21:50:23534 new CrxInstaller(this, // frontend
[email protected]aa142702010-03-26 01:26:33535 client));
[email protected]6dfbbf82010-03-12 23:09:16536 installer->set_expected_id(id);
[email protected]ec5b50d2010-10-09 16:35:18537 if (is_pending_extension)
[email protected]b2907fd2011-03-25 16:43:37538 installer->set_install_source(pending_extension_info.install_source());
[email protected]5eb375e92010-11-26 07:50:41539 else if (extension)
540 installer->set_install_source(extension->location());
[email protected]6dfbbf82010-03-12 23:09:16541 installer->set_delete_source(true);
[email protected]5c8516202010-03-18 21:43:34542 installer->set_original_url(download_url);
[email protected]6dfbbf82010-03-12 23:09:16543 installer->InstallCrx(extension_path);
[email protected]e957fe52009-06-23 16:51:05544}
545
[email protected]eaa7dd182010-12-14 11:09:00546void ExtensionService::ReloadExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12547 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]b65272f2009-08-31 15:47:06548 FilePath path;
[email protected]9adb9692010-10-29 23:14:02549 const Extension* current_extension = GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40550
[email protected]f17dbd42010-08-16 23:21:10551 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]b65272f2009-08-31 15:47:06552 if (current_extension) {
[email protected]4814b512009-11-07 00:12:29553 // If the extension has an inspector open for its background page, detach
554 // the inspector and hang onto a cookie for it, so that we can reattach
555 // later.
556 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
557 ExtensionHost* host = manager->GetBackgroundHostForExtension(
558 current_extension);
559 if (host) {
560 // Look for an open inspector for the background page.
561 int devtools_cookie = DevToolsManager::GetInstance()->DetachClientHost(
562 host->render_view_host());
563 if (devtools_cookie >= 0)
564 orphaned_dev_tools_[extension_id] = devtools_cookie;
565 }
566
[email protected]b65272f2009-08-31 15:47:06567 path = current_extension->path();
[email protected]f17dbd42010-08-16 23:21:10568 DisableExtension(extension_id);
569 disabled_extension_paths_[extension_id] = path;
[email protected]1eb175082010-02-10 09:26:16570 } else {
571 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06572 }
573
[email protected]e6090e42010-03-23 22:44:08574 // Check the installed extensions to see if what we're reloading was already
575 // installed.
576 scoped_ptr<ExtensionInfo> installed_extension(
577 extension_prefs_->GetInstalledExtensionInfo(extension_id));
578 if (installed_extension.get() &&
579 installed_extension->extension_manifest.get()) {
580 LoadInstalledExtension(*installed_extension, false);
581 } else {
582 // We should always be able to remember the extension's path. If it's not in
583 // the map, someone failed to update |unloaded_extension_paths_|.
584 CHECK(!path.empty());
585 LoadExtension(path);
586 }
[email protected]9cddd4702009-07-27 22:09:40587}
588
[email protected]eaa7dd182010-12-14 11:09:00589void ExtensionService::UninstallExtension(const std::string& extension_id,
[email protected]604322d2011-03-22 16:51:56590 bool external_uninstall) {
[email protected]a29a517a2011-01-21 21:11:12591 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20592
[email protected]9adb9692010-10-29 23:14:02593 const Extension* extension =
594 GetExtensionByIdInternal(extension_id, true, true);
[email protected]0dfe05c2011-02-23 23:03:36595 if (!extension)
596 extension = GetTerminatedExtension(extension_id);
[email protected]631cf822009-05-15 07:01:25597
[email protected]e7afe2452010-08-22 16:19:13598 // Callers should not send us nonexistent extensions.
[email protected]aebe23a32010-12-10 22:15:48599 CHECK(extension);
[email protected]9f1087e2009-06-15 17:29:32600
[email protected]831aa212010-03-26 13:55:19601 // Get hold of information we need after unloading, since the extension
602 // pointer will be invalid then.
603 GURL extension_url(extension->url());
604 Extension::Location location(extension->location());
[email protected]211030342010-09-30 18:41:06605 UninstalledExtensionInfo uninstalled_extension_info(*extension);
[email protected]831aa212010-03-26 13:55:19606
[email protected]9b217652010-10-08 22:04:23607 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08608 extension->GetType(), 100);
[email protected]9b217652010-10-08 22:04:23609
[email protected]831aa212010-03-26 13:55:19610 // Also copy the extension identifier since the reference might have been
611 // obtained via Extension::id().
612 std::string extension_id_copy(extension_id);
613
[email protected]56ad3792010-05-28 17:45:33614 if (profile_->GetTemplateURLModel())
615 profile_->GetTemplateURLModel()->UnregisterExtensionKeyword(extension);
616
[email protected]831aa212010-03-26 13:55:19617 // Unload before doing more cleanup to ensure that nothing is hanging on to
618 // any of these resources.
[email protected]a9f39a312010-12-23 22:14:27619 UnloadExtension(extension_id, UnloadedExtensionInfo::UNINSTALL);
[email protected]831aa212010-03-26 13:55:19620
621 extension_prefs_->OnExtensionUninstalled(extension_id_copy, location,
622 external_uninstall);
[email protected]9f1087e2009-06-15 17:29:32623
624 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]831aa212010-03-26 13:55:19625 if (Extension::LOAD != location) {
[email protected]ca4b5fa32010-10-09 12:42:18626 BrowserThread::PostTask(
627 BrowserThread::FILE, FROM_HERE,
[email protected]95d29192009-10-30 01:49:06628 NewRunnableFunction(
[email protected]ca3dbf52010-05-19 22:27:06629 &extension_file_util::UninstallExtension,
630 install_directory_,
631 extension_id_copy));
[email protected]9f1087e2009-06-15 17:29:32632 }
633
[email protected]c10da4b02010-03-25 14:38:32634 ClearExtensionData(extension_url);
[email protected]0dfe05c2011-02-23 23:03:36635 UntrackTerminatedExtension(extension_id);
[email protected]211030342010-09-30 18:41:06636
637 // Notify interested parties that we've uninstalled this extension.
638 NotificationService::current()->Notify(
639 NotificationType::EXTENSION_UNINSTALLED,
640 Source<Profile>(profile_),
641 Details<UninstalledExtensionInfo>(&uninstalled_extension_info));
[email protected]c10da4b02010-03-25 14:38:32642}
643
[email protected]eaa7dd182010-12-14 11:09:00644void ExtensionService::ClearExtensionData(const GURL& extension_url) {
[email protected]c10da4b02010-03-25 14:38:32645 scoped_refptr<ExtensionDataDeleter> deleter(
646 new ExtensionDataDeleter(profile_, extension_url));
647 deleter->StartDeleting();
[email protected]9f1087e2009-06-15 17:29:32648}
649
[email protected]eaa7dd182010-12-14 11:09:00650void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12651 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20652
[email protected]9adb9692010-10-29 23:14:02653 const Extension* extension =
654 GetExtensionByIdInternal(extension_id, false, true);
[email protected]f681c782010-11-19 11:19:39655 if (!extension)
[email protected]0c6da502009-08-14 22:32:39656 return;
[email protected]0c6da502009-08-14 22:32:39657
[email protected]e8c729a2010-03-09 19:55:19658 extension_prefs_->SetExtensionState(extension, Extension::ENABLED);
[email protected]1784e83a2009-09-08 21:01:52659
[email protected]0c6da502009-08-14 22:32:39660 // Move it over to the enabled list.
[email protected]00cd9c42010-11-02 20:15:57661 extensions_.push_back(make_scoped_refptr(extension));
[email protected]0c6da502009-08-14 22:32:39662 ExtensionList::iterator iter = std::find(disabled_extensions_.begin(),
663 disabled_extensions_.end(),
664 extension);
665 disabled_extensions_.erase(iter);
666
[email protected]f681c782010-11-19 11:19:39667 // Make sure any browser action contained within it is not hidden.
668 extension_prefs_->SetBrowserActionVisibility(extension, true);
669
[email protected]8f9d4eb2011-02-05 01:39:10670 ExtensionWebUI::RegisterChromeURLOverrides(profile_,
[email protected]86c008e82009-08-28 20:26:05671 extension->GetChromeURLOverrides());
672
[email protected]62d30f42009-10-01 22:36:06673 NotifyExtensionLoaded(extension);
[email protected]aab98a52009-12-02 03:22:35674 UpdateActiveExtensionsInCrashReporter();
[email protected]0c6da502009-08-14 22:32:39675}
676
[email protected]eaa7dd182010-12-14 11:09:00677void ExtensionService::DisableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12678 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20679
[email protected]9adb9692010-10-29 23:14:02680 const Extension* extension =
681 GetExtensionByIdInternal(extension_id, true, false);
[email protected]b2ba9962009-12-10 20:10:15682 // The extension may have been disabled already.
683 if (!extension)
[email protected]1784e83a2009-09-08 21:01:52684 return;
[email protected]1784e83a2009-09-08 21:01:52685
[email protected]e8c729a2010-03-09 19:55:19686 extension_prefs_->SetExtensionState(extension, Extension::DISABLED);
[email protected]1784e83a2009-09-08 21:01:52687
688 // Move it over to the disabled list.
[email protected]00cd9c42010-11-02 20:15:57689 disabled_extensions_.push_back(make_scoped_refptr(extension));
[email protected]1784e83a2009-09-08 21:01:52690 ExtensionList::iterator iter = std::find(extensions_.begin(),
691 extensions_.end(),
692 extension);
693 extensions_.erase(iter);
694
[email protected]8f9d4eb2011-02-05 01:39:10695 ExtensionWebUI::UnregisterChromeURLOverrides(profile_,
[email protected]1784e83a2009-09-08 21:01:52696 extension->GetChromeURLOverrides());
697
[email protected]a9f39a312010-12-23 22:14:27698 NotifyExtensionUnloaded(extension, UnloadedExtensionInfo::DISABLE);
[email protected]aab98a52009-12-02 03:22:35699 UpdateActiveExtensionsInCrashReporter();
[email protected]1784e83a2009-09-08 21:01:52700}
701
[email protected]eaa7dd182010-12-14 11:09:00702void ExtensionService::GrantPermissions(const Extension* extension) {
[email protected]8d888c12010-11-30 00:00:25703 CHECK(extension);
704
705 // We only maintain the granted permissions prefs for INTERNAL extensions.
[email protected]139e5732011-03-16 19:29:51706 CHECK_EQ(Extension::INTERNAL, extension->location());
[email protected]8d888c12010-11-30 00:00:25707
708 ExtensionExtent effective_hosts = extension->GetEffectiveHostPermissions();
709 extension_prefs_->AddGrantedPermissions(extension->id(),
710 extension->HasFullPermissions(),
711 extension->api_permissions(),
712 effective_hosts);
713}
714
[email protected]eaa7dd182010-12-14 11:09:00715void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]8d888c12010-11-30 00:00:25716 const Extension* extension) {
717 CHECK(extension);
718 GrantPermissions(extension);
719 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
720 EnableExtension(extension->id());
721}
722
[email protected]eaa7dd182010-12-14 11:09:00723void ExtensionService::LoadExtension(const FilePath& extension_path) {
[email protected]ca4b5fa32010-10-09 12:42:18724 BrowserThread::PostTask(
725 BrowserThread::FILE, FROM_HERE,
[email protected]95d29192009-10-30 01:49:06726 NewRunnableMethod(
727 backend_.get(),
[email protected]eaa7dd182010-12-14 11:09:00728 &ExtensionServiceBackend::LoadSingleExtension,
729 extension_path, scoped_refptr<ExtensionService>(this)));
[email protected]9f1087e2009-06-15 17:29:32730}
731
[email protected]eaa7dd182010-12-14 11:09:00732void ExtensionService::LoadComponentExtensions() {
[email protected]1952c7d2010-03-04 23:48:34733 for (RegisteredComponentExtensions::iterator it =
734 component_extension_manifests_.begin();
735 it != component_extension_manifests_.end(); ++it) {
[email protected]61b55b62011-03-24 09:03:10736 LoadComponentExtension(*it);
[email protected]1952c7d2010-03-04 23:48:34737 }
738}
739
[email protected]61b55b62011-03-24 09:03:10740void ExtensionService::LoadComponentExtension(
741 const ComponentExtensionInfo &info) {
742 JSONStringValueSerializer serializer(info.manifest);
743 scoped_ptr<Value> manifest(serializer.Deserialize(NULL, NULL));
744 if (!manifest.get()) {
745 DLOG(ERROR) << "Failed to parse manifest for extension";
746 return;
747 }
748
[email protected]83048a22011-03-29 00:14:13749 int flags = Extension::REQUIRE_KEY;
750 if (Extension::ShouldDoStrictErrorChecking(Extension::COMPONENT))
751 flags |= Extension::STRICT_ERROR_CHECKS;
[email protected]61b55b62011-03-24 09:03:10752 std::string error;
753 scoped_refptr<const Extension> extension(Extension::Create(
754 info.root_directory,
755 Extension::COMPONENT,
756 *static_cast<DictionaryValue*>(manifest.get()),
[email protected]83048a22011-03-29 00:14:13757 flags,
[email protected]61b55b62011-03-24 09:03:10758 &error));
759 if (!extension.get()) {
760 NOTREACHED() << error;
761 return;
762 }
763 AddExtension(extension);
764}
765
[email protected]eaa7dd182010-12-14 11:09:00766void ExtensionService::LoadAllExtensions() {
[email protected]a29a517a2011-01-21 21:11:12767 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a315ba92010-11-16 14:12:21768
[email protected]cc2c3432009-11-06 17:24:36769 base::TimeTicks start_time = base::TimeTicks::Now();
770
[email protected]1952c7d2010-03-04 23:48:34771 // Load any component extensions.
772 LoadComponentExtensions();
773
[email protected]e72e8eb82009-06-18 17:21:51774 // Load the previously installed extensions.
[email protected]a315ba92010-11-16 14:12:21775 scoped_ptr<ExtensionPrefs::ExtensionsInfo> extensions_info(
[email protected]e6090e42010-03-23 22:44:08776 extension_prefs_->GetInstalledExtensionsInfo());
[email protected]c6d474f82009-12-16 21:11:06777
[email protected]a315ba92010-11-16 14:12:21778 std::vector<int> reload_reason_counts(NUM_MANIFEST_RELOAD_REASONS, 0);
779 bool should_write_prefs = false;
780
781 for (size_t i = 0; i < extensions_info->size(); ++i) {
782 ExtensionInfo* info = extensions_info->at(i).get();
783
784 ManifestReloadReason reload_reason = ShouldReloadExtensionManifest(*info);
785 ++reload_reason_counts[reload_reason];
786 UMA_HISTOGRAM_ENUMERATION("Extensions.ManifestReloadEnumValue",
787 reload_reason, 100);
788
789 if (reload_reason != NOT_NEEDED) {
790 // Reloading and extension reads files from disk. We do this on the
791 // UI thread because reloads should be very rare, and the complexity
792 // added by delaying the time when the extensions service knows about
793 // all extensions is significant. See crbug.com/37548 for details.
794 // |allow_io| disables tests that file operations run on the file
795 // thread.
796 base::ThreadRestrictions::ScopedAllowIO allow_io;
797
[email protected]83048a22011-03-29 00:14:13798 int flags = Extension::NO_FLAGS;
799 if (Extension::ShouldDoStrictErrorChecking(info->extension_location))
800 flags |= Extension::STRICT_ERROR_CHECKS;
[email protected]a315ba92010-11-16 14:12:21801 std::string error;
802 scoped_refptr<const Extension> extension(
803 extension_file_util::LoadExtension(
[email protected]542258c2011-03-04 21:25:31804 info->extension_path,
805 info->extension_location,
[email protected]83048a22011-03-29 00:14:13806 flags,
[email protected]542258c2011-03-04 21:25:31807 &error));
[email protected]a315ba92010-11-16 14:12:21808
809 if (extension.get()) {
810 extensions_info->at(i)->extension_manifest.reset(
811 static_cast<DictionaryValue*>(
812 extension->manifest_value()->DeepCopy()));
813 should_write_prefs = true;
814 }
[email protected]c6d474f82009-12-16 21:11:06815 }
816 }
817
[email protected]a315ba92010-11-16 14:12:21818 for (size_t i = 0; i < extensions_info->size(); ++i) {
819 LoadInstalledExtension(*extensions_info->at(i), should_write_prefs);
[email protected]c6d474f82009-12-16 21:11:06820 }
821
[email protected]ae09ca62009-08-21 19:46:46822 OnLoadedInstalledExtensions();
[email protected]cc2c3432009-11-06 17:24:36823
[email protected]a315ba92010-11-16 14:12:21824 // The histograms Extensions.ManifestReload* allow us to validate
825 // the assumption that reloading manifest is a rare event.
826 UMA_HISTOGRAM_COUNTS_100("Extensions.ManifestReloadNotNeeded",
827 reload_reason_counts[NOT_NEEDED]);
828 UMA_HISTOGRAM_COUNTS_100("Extensions.ManifestReloadUnpackedDir",
829 reload_reason_counts[UNPACKED_DIR]);
830 UMA_HISTOGRAM_COUNTS_100("Extensions.ManifestReloadNeedsRelocalization",
831 reload_reason_counts[NEEDS_RELOCALIZATION]);
832
[email protected]cc2c3432009-11-06 17:24:36833 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadAll", extensions_.size());
834 UMA_HISTOGRAM_COUNTS_100("Extensions.Disabled", disabled_extensions_.size());
835
[email protected]1952c7d2010-03-04 23:48:34836 UMA_HISTOGRAM_TIMES("Extensions.LoadAllTime",
837 base::TimeTicks::Now() - start_time);
[email protected]cc2c3432009-11-06 17:24:36838
[email protected]9b217652010-10-08 22:04:23839 int app_count = 0;
840 int hosted_app_count = 0;
841 int packaged_app_count = 0;
[email protected]1952c7d2010-03-04 23:48:34842 int user_script_count = 0;
843 int extension_count = 0;
844 int theme_count = 0;
845 int external_count = 0;
846 int page_action_count = 0;
847 int browser_action_count = 0;
848 ExtensionList::iterator ex;
849 for (ex = extensions_.begin(); ex != extensions_.end(); ++ex) {
[email protected]9b217652010-10-08 22:04:23850 Extension::Location location = (*ex)->location();
[email protected]7fa19f82010-12-21 19:40:08851 Extension::Type type = (*ex)->GetType();
[email protected]9b217652010-10-08 22:04:23852 if ((*ex)->is_app()) {
853 UMA_HISTOGRAM_ENUMERATION("Extensions.AppLocation",
854 location, 100);
855 } else if (type == Extension::TYPE_EXTENSION) {
856 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionLocation",
857 location, 100);
858 }
859
[email protected]1952c7d2010-03-04 23:48:34860 // Don't count component extensions, since they are only extensions as an
861 // implementation detail.
[email protected]9b217652010-10-08 22:04:23862 if (location == Extension::COMPONENT)
[email protected]1952c7d2010-03-04 23:48:34863 continue;
864
[email protected]e8c729a2010-03-09 19:55:19865 // Don't count unpacked extensions, since they're a developer-specific
866 // feature.
[email protected]9b217652010-10-08 22:04:23867 if (location == Extension::LOAD)
[email protected]e8c729a2010-03-09 19:55:19868 continue;
869
[email protected]9b217652010-10-08 22:04:23870 // Using an enumeration shows us the total installed ratio across all users.
871 // Using the totals per user at each startup tells us the distribution of
872 // usage for each user (e.g. 40% of users have at least one app installed).
873 UMA_HISTOGRAM_ENUMERATION("Extensions.LoadType", type, 100);
874 switch (type) {
875 case Extension::TYPE_THEME:
[email protected]a315ba92010-11-16 14:12:21876 ++theme_count;
[email protected]9b217652010-10-08 22:04:23877 break;
878 case Extension::TYPE_USER_SCRIPT:
[email protected]a315ba92010-11-16 14:12:21879 ++user_script_count;
[email protected]9b217652010-10-08 22:04:23880 break;
881 case Extension::TYPE_HOSTED_APP:
[email protected]a315ba92010-11-16 14:12:21882 ++app_count;
883 ++hosted_app_count;
[email protected]9b217652010-10-08 22:04:23884 break;
885 case Extension::TYPE_PACKAGED_APP:
[email protected]a315ba92010-11-16 14:12:21886 ++app_count;
887 ++packaged_app_count;
[email protected]9b217652010-10-08 22:04:23888 break;
889 case Extension::TYPE_EXTENSION:
890 default:
[email protected]a315ba92010-11-16 14:12:21891 ++extension_count;
[email protected]9b217652010-10-08 22:04:23892 break;
[email protected]cc2c3432009-11-06 17:24:36893 }
[email protected]9b217652010-10-08 22:04:23894 if (Extension::IsExternalLocation(location))
[email protected]a315ba92010-11-16 14:12:21895 ++external_count;
[email protected]9b217652010-10-08 22:04:23896 if ((*ex)->page_action() != NULL)
[email protected]a315ba92010-11-16 14:12:21897 ++page_action_count;
[email protected]9b217652010-10-08 22:04:23898 if ((*ex)->browser_action() != NULL)
[email protected]a315ba92010-11-16 14:12:21899 ++browser_action_count;
[email protected]cc2c3432009-11-06 17:24:36900 }
[email protected]9b217652010-10-08 22:04:23901 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadApp", app_count);
902 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadHostedApp", hosted_app_count);
903 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadPackagedApp", packaged_app_count);
[email protected]1952c7d2010-03-04 23:48:34904 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadExtension", extension_count);
905 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadUserScript", user_script_count);
906 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadTheme", theme_count);
907 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadExternal", external_count);
908 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadPageAction", page_action_count);
909 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadBrowserAction",
910 browser_action_count);
[email protected]ae09ca62009-08-21 19:46:46911}
912
[email protected]eaa7dd182010-12-14 11:09:00913void ExtensionService::LoadInstalledExtension(const ExtensionInfo& info,
[email protected]542258c2011-03-04 21:25:31914 bool write_to_prefs) {
[email protected]ae09ca62009-08-21 19:46:46915 std::string error;
[email protected]9adb9692010-10-29 23:14:02916 scoped_refptr<const Extension> extension(NULL);
[email protected]306a2bd2010-08-11 14:56:36917 if (!extension_prefs_->IsExtensionAllowedByPolicy(info.extension_id)) {
918 error = errors::kDisabledByPolicy;
919 } else if (info.extension_manifest.get()) {
[email protected]83048a22011-03-29 00:14:13920 int flags = Extension::NO_FLAGS;
921 if (info.extension_location != Extension::LOAD)
922 flags |= Extension::REQUIRE_KEY;
923 if (Extension::ShouldDoStrictErrorChecking(info.extension_location))
924 flags |= Extension::STRICT_ERROR_CHECKS;
[email protected]66e4eb32010-10-27 20:37:41925 extension = Extension::Create(
[email protected]542258c2011-03-04 21:25:31926 info.extension_path,
927 info.extension_location,
928 *info.extension_manifest,
[email protected]83048a22011-03-29 00:14:13929 flags,
[email protected]542258c2011-03-04 21:25:31930 &error);
[email protected]ae09ca62009-08-21 19:46:46931 } else {
[email protected]c6d474f82009-12-16 21:11:06932 error = errors::kManifestUnreadable;
[email protected]ae09ca62009-08-21 19:46:46933 }
934
935 if (!extension) {
[email protected]c6d474f82009-12-16 21:11:06936 ReportExtensionLoadError(info.extension_path,
[email protected]d11c8e92009-10-20 23:26:40937 error,
938 NotificationType::EXTENSION_INSTALL_ERROR,
939 false);
[email protected]ae09ca62009-08-21 19:46:46940 return;
941 }
942
[email protected]c6d474f82009-12-16 21:11:06943 if (write_to_prefs)
944 extension_prefs_->UpdateManifest(extension);
945
[email protected]49cd1572011-02-08 21:38:45946 AddExtension(extension);
[email protected]9f1087e2009-06-15 17:29:32947}
948
[email protected]eaa7dd182010-12-14 11:09:00949void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]57a777f72010-03-31 01:09:42950 // The ChromeURLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:06951 // was loaded, otherwise a race can arise where a renderer that is created
952 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:42953 // that the request context doesn't yet know about. The profile is responsible
954 // for ensuring its URLRequestContexts appropriately discover the loaded
955 // extension.
956 if (profile_) {
957 profile_->RegisterExtensionWithRequestContexts(extension);
[email protected]19eb80152011-02-26 00:28:43958 profile_->GetExtensionSpecialStoragePolicy()->
959 GrantRightsForExtension(extension);
[email protected]62d30f42009-10-01 22:36:06960 }
961
962 NotificationService::current()->Notify(
963 NotificationType::EXTENSION_LOADED,
[email protected]24e7a9d2009-11-04 11:11:34964 Source<Profile>(profile_),
[email protected]9adb9692010-10-29 23:14:02965 Details<const Extension>(extension));
[email protected]62d30f42009-10-01 22:36:06966}
967
[email protected]a9f39a312010-12-23 22:14:27968void ExtensionService::NotifyExtensionUnloaded(
969 const Extension* extension, UnloadedExtensionInfo::Reason reason) {
970 UnloadedExtensionInfo details(extension, reason);
[email protected]62d30f42009-10-01 22:36:06971 NotificationService::current()->Notify(
972 NotificationType::EXTENSION_UNLOADED,
[email protected]24e7a9d2009-11-04 11:11:34973 Source<Profile>(profile_),
[email protected]a9f39a312010-12-23 22:14:27974 Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:06975
[email protected]57a777f72010-03-31 01:09:42976 if (profile_) {
977 profile_->UnregisterExtensionWithRequestContexts(extension);
[email protected]19eb80152011-02-26 00:28:43978 profile_->GetExtensionSpecialStoragePolicy()->
979 RevokeRightsForExtension(extension);
[email protected]62d30f42009-10-01 22:36:06980 }
981}
982
[email protected]eaa7dd182010-12-14 11:09:00983void ExtensionService::UpdateExtensionBlacklist(
[email protected]6b75ec32009-08-14 06:37:18984 const std::vector<std::string>& blacklist) {
985 // Use this set to indicate if an extension in the blacklist has been used.
986 std::set<std::string> blacklist_set;
987 for (unsigned int i = 0; i < blacklist.size(); ++i) {
988 if (Extension::IdIsValid(blacklist[i])) {
989 blacklist_set.insert(blacklist[i]);
990 }
991 }
992 extension_prefs_->UpdateBlacklist(blacklist_set);
993 std::vector<std::string> to_be_removed;
994 // Loop current extensions, unload installed extensions.
995 for (ExtensionList::const_iterator iter = extensions_.begin();
996 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:02997 const Extension* extension = (*iter);
[email protected]6b75ec32009-08-14 06:37:18998 if (blacklist_set.find(extension->id()) != blacklist_set.end()) {
999 to_be_removed.push_back(extension->id());
1000 }
1001 }
1002
1003 // UnloadExtension will change the extensions_ list. So, we should
1004 // call it outside the iterator loop.
1005 for (unsigned int i = 0; i < to_be_removed.size(); ++i) {
[email protected]a9f39a312010-12-23 22:14:271006 UnloadExtension(to_be_removed[i], UnloadedExtensionInfo::DISABLE);
[email protected]6b75ec32009-08-14 06:37:181007 }
1008}
1009
[email protected]0a071a32011-02-08 00:18:241010Profile* ExtensionService::profile() {
1011 return profile_;
1012}
1013
[email protected]eaa7dd182010-12-14 11:09:001014void ExtensionService::DestroyingProfile() {
[email protected]94b4ffc2011-02-04 05:55:171015 if (updater_.get()) {
1016 updater_->Stop();
1017 }
[email protected]97d2f1d2011-01-15 00:41:081018 browser_event_router_.reset();
[email protected]118de7892011-03-16 13:31:401019 preference_event_router_.reset();
[email protected]2fb7dc982010-09-29 12:24:281020 pref_change_registrar_.RemoveAll();
[email protected]aa96d3a2010-08-21 08:45:251021 profile_ = NULL;
[email protected]2fb7dc982010-09-29 12:24:281022 toolbar_model_.DestroyingProfile();
[email protected]aa96d3a2010-08-21 08:45:251023}
1024
[email protected]eaa7dd182010-12-14 11:09:001025ExtensionPrefs* ExtensionService::extension_prefs() {
[email protected]ddb1e5a2010-12-13 20:10:451026 return extension_prefs_;
1027}
1028
[email protected]b2907fd2011-03-25 16:43:371029const ExtensionPrefs& ExtensionService::const_extension_prefs() const {
1030 return *extension_prefs_;
1031}
1032
[email protected]eaa7dd182010-12-14 11:09:001033void ExtensionService::CheckAdminBlacklist() {
[email protected]aa96d3a2010-08-21 08:45:251034 std::vector<std::string> to_be_removed;
1035 // Loop through extensions list, unload installed extensions.
1036 for (ExtensionList::const_iterator iter = extensions_.begin();
1037 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021038 const Extension* extension = (*iter);
[email protected]aa96d3a2010-08-21 08:45:251039 if (!extension_prefs_->IsExtensionAllowedByPolicy(extension->id()))
1040 to_be_removed.push_back(extension->id());
1041 }
1042
1043 // UnloadExtension will change the extensions_ list. So, we should
1044 // call it outside the iterator loop.
1045 for (unsigned int i = 0; i < to_be_removed.size(); ++i)
[email protected]a9f39a312010-12-23 22:14:271046 UnloadExtension(to_be_removed[i], UnloadedExtensionInfo::DISABLE);
[email protected]aa96d3a2010-08-21 08:45:251047}
1048
[email protected]eaa7dd182010-12-14 11:09:001049bool ExtensionService::IsIncognitoEnabled(const Extension* extension) {
[email protected]cb0ce1e022010-03-10 19:54:411050 // If this is a component extension we always allow it to work in incognito
1051 // mode.
1052 if (extension->location() == Extension::COMPONENT)
1053 return true;
1054
1055 // Check the prefs.
1056 return extension_prefs_->IsIncognitoEnabled(extension->id());
[email protected]db7331a2010-02-25 22:10:501057}
[email protected]55a35692010-02-11 23:25:211058
[email protected]eaa7dd182010-12-14 11:09:001059void ExtensionService::SetIsIncognitoEnabled(const Extension* extension,
[email protected]db7331a2010-02-25 22:10:501060 bool enabled) {
[email protected]8c6c8c22011-03-09 12:52:341061 // Broadcast unloaded and loaded events to update browser state. Only bother
1062 // if the value changed and the extension is actually enabled, since there is
1063 // no UI otherwise.
1064 bool old_enabled = extension_prefs_->IsIncognitoEnabled(extension->id());
1065 if (enabled == old_enabled)
1066 return;
1067
[email protected]cb0ce1e022010-03-10 19:54:411068 extension_prefs_->SetIsIncognitoEnabled(extension->id(), enabled);
[email protected]c1499f3d2010-03-05 00:33:241069
[email protected]8c6c8c22011-03-09 12:52:341070 bool extension_is_enabled = std::find(extensions_.begin(), extensions_.end(),
1071 extension) != extensions_.end();
1072 if (extension_is_enabled) {
[email protected]a9f39a312010-12-23 22:14:271073 NotifyExtensionUnloaded(extension, UnloadedExtensionInfo::DISABLE);
[email protected]568f33d2010-08-04 17:06:411074 NotifyExtensionLoaded(extension);
1075 }
[email protected]55a35692010-02-11 23:25:211076}
1077
[email protected]eaa7dd182010-12-14 11:09:001078bool ExtensionService::CanCrossIncognito(const Extension* extension) {
[email protected]2a8f24e2010-11-03 21:37:051079 // We allow the extension to see events and data from another profile iff it
1080 // uses "spanning" behavior and it has incognito access. "split" mode
1081 // extensions only see events for a matching profile.
1082 return IsIncognitoEnabled(extension) && !extension->incognito_split_mode();
1083}
1084
[email protected]eaa7dd182010-12-14 11:09:001085bool ExtensionService::AllowFileAccess(const Extension* extension) {
[email protected]05c82182010-06-24 17:49:081086 return (CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]334e04a2010-06-24 23:34:441087 switches::kDisableExtensionsFileAccessCheck) ||
[email protected]05c82182010-06-24 17:49:081088 extension_prefs_->AllowFileAccess(extension->id()));
1089}
1090
[email protected]eaa7dd182010-12-14 11:09:001091void ExtensionService::SetAllowFileAccess(const Extension* extension,
[email protected]9adb9692010-10-29 23:14:021092 bool allow) {
[email protected]05c82182010-06-24 17:49:081093 extension_prefs_->SetAllowFileAccess(extension->id(), allow);
1094 NotificationService::current()->Notify(
1095 NotificationType::EXTENSION_USER_SCRIPTS_UPDATED,
1096 Source<Profile>(profile_),
[email protected]9adb9692010-10-29 23:14:021097 Details<const Extension>(extension));
[email protected]05c82182010-06-24 17:49:081098}
1099
[email protected]eaa7dd182010-12-14 11:09:001100bool ExtensionService::GetBrowserActionVisibility(const Extension* extension) {
[email protected]f681c782010-11-19 11:19:391101 return extension_prefs_->GetBrowserActionVisibility(extension);
1102}
1103
[email protected]eaa7dd182010-12-14 11:09:001104void ExtensionService::SetBrowserActionVisibility(const Extension* extension,
[email protected]8e4560b62011-01-14 10:09:141105 bool visible) {
[email protected]f681c782010-11-19 11:19:391106 extension_prefs_->SetBrowserActionVisibility(extension, visible);
1107}
1108
[email protected]8e4560b62011-01-14 10:09:141109// Some extensions will autoupdate themselves externally from Chrome. These
1110// are typically part of some larger client application package. To support
1111// these, the extension will register its location in the the preferences file
1112// (and also, on Windows, in the registry) and this code will periodically
1113// check that location for a .crx file, which it will then install locally if
1114// a new version is available.
1115// Errors are reported through ExtensionErrorReporter. Succcess is not
1116// reported.
[email protected]eaa7dd182010-12-14 11:09:001117void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:121118 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]8e4560b62011-01-14 10:09:141119
1120 // Note that this installation is intentionally silent (since it didn't
1121 // go through the front-end). Extensions that are registered in this
1122 // way are effectively considered 'pre-bundled', and so implicitly
1123 // trusted. In general, if something has HKLM or filesystem access,
1124 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:121125
1126 // If any external extension records give a URL, a provider will set
1127 // this to true. Used by OnExternalProviderReady() to see if we need
1128 // to start an update check to fetch a new external extension.
1129 external_extension_url_added_ = false;
[email protected]8e4560b62011-01-14 10:09:141130
1131 // Ask each external extension provider to give us a call back for each
1132 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
1133 ProviderCollection::const_iterator i;
1134 for (i = external_extension_providers_.begin();
1135 i != external_extension_providers_.end(); ++i) {
1136 ExternalExtensionProviderInterface* provider = i->get();
1137 provider->VisitRegisteredExtension();
1138 }
1139
1140 // Uninstall of unclaimed extensions will happen after all the providers
[email protected]a29a517a2011-01-21 21:11:121141 // had reported ready. Every provider calls OnExternalProviderReady()
1142 // when it finishes, and OnExternalProviderReady() only acts when all
1143 // providers are ready. In case there are no providers, we call it
1144 // to trigger removal of extensions that used to have an external source.
1145 if (external_extension_providers_.empty())
1146 OnExternalProviderReady();
[email protected]9f1087e2009-06-15 17:29:321147}
1148
[email protected]8e4560b62011-01-14 10:09:141149void ExtensionService::OnExternalProviderReady() {
[email protected]a29a517a2011-01-21 21:11:121150 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1151
1152 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:141153 // if all of them are finished. So we check them first.
1154 ProviderCollection::const_iterator i;
1155 for (i = external_extension_providers_.begin();
1156 i != external_extension_providers_.end(); ++i) {
1157 ExternalExtensionProviderInterface* provider = i->get();
[email protected]a29a517a2011-01-21 21:11:121158 if (!provider->IsReady())
[email protected]8e4560b62011-01-14 10:09:141159 return;
[email protected]8e4560b62011-01-14 10:09:141160 }
1161
[email protected]a29a517a2011-01-21 21:11:121162 // All the providers are ready. Install any pending extensions.
1163 if (external_extension_url_added_ && updater()) {
1164 external_extension_url_added_ = false;
[email protected]8e4560b62011-01-14 10:09:141165 updater()->CheckNow();
1166 }
1167
1168 // Uninstall all the unclaimed extensions.
1169 scoped_ptr<ExtensionPrefs::ExtensionsInfo> extensions_info(
1170 extension_prefs_->GetInstalledExtensionsInfo());
1171 for (size_t i = 0; i < extensions_info->size(); ++i) {
1172 ExtensionInfo* info = extensions_info->at(i).get();
1173 if (Extension::IsExternalLocation(info->extension_location))
1174 CheckExternalUninstall(info->extension_id);
1175 }
[email protected]6c751e72010-11-23 10:11:101176}
1177
[email protected]a9f39a312010-12-23 22:14:271178void ExtensionService::UnloadExtension(
1179 const std::string& extension_id,
1180 UnloadedExtensionInfo::Reason reason) {
[email protected]27e469a2010-01-11 20:35:091181 // Make sure the extension gets deleted after we return from this function.
[email protected]9adb9692010-10-29 23:14:021182 scoped_refptr<const Extension> extension(
[email protected]0c6da502009-08-14 22:32:391183 GetExtensionByIdInternal(extension_id, true, true));
[email protected]631cf822009-05-15 07:01:251184
[email protected]fa0c96732010-11-17 00:14:231185 // This method can be called via PostTask, so the extension may have been
1186 // unloaded by the time this runs.
1187 if (!extension)
1188 return;
[email protected]0c6da502009-08-14 22:32:391189
[email protected]1eb175082010-02-10 09:26:161190 // Keep information about the extension so that we can reload it later
1191 // even if it's not permanently installed.
1192 unloaded_extension_paths_[extension->id()] = extension->path();
1193
[email protected]f17dbd42010-08-16 23:21:101194 // Clean up if the extension is meant to be enabled after a reload.
1195 disabled_extension_paths_.erase(extension->id());
1196
[email protected]d7e9a862010-11-03 21:57:491197 // Clean up runtime data.
1198 extension_runtime_data_.erase(extension_id);
1199
[email protected]8f9d4eb2011-02-05 01:39:101200 ExtensionWebUI::UnregisterChromeURLOverrides(profile_,
[email protected]86c008e82009-08-28 20:26:051201 extension->GetChromeURLOverrides());
1202
[email protected]0c6da502009-08-14 22:32:391203 ExtensionList::iterator iter = std::find(disabled_extensions_.begin(),
1204 disabled_extensions_.end(),
1205 extension.get());
1206 if (iter != disabled_extensions_.end()) {
[email protected]a9f39a312010-12-23 22:14:271207 UnloadedExtensionInfo details(extension, reason);
1208 details.already_disabled = true;
[email protected]0c6da502009-08-14 22:32:391209 disabled_extensions_.erase(iter);
[email protected]866930682009-08-18 22:53:471210 NotificationService::current()->Notify(
[email protected]a9f39a312010-12-23 22:14:271211 NotificationType::EXTENSION_UNLOADED,
[email protected]24e7a9d2009-11-04 11:11:341212 Source<Profile>(profile_),
[email protected]a9f39a312010-12-23 22:14:271213 Details<UnloadedExtensionInfo>(&details));
[email protected]0c6da502009-08-14 22:32:391214 return;
1215 }
1216
1217 iter = std::find(extensions_.begin(), extensions_.end(), extension.get());
[email protected]894bb502009-05-21 22:39:571218
[email protected]631cf822009-05-15 07:01:251219 // Remove the extension from our list.
1220 extensions_.erase(iter);
1221
[email protected]a9f39a312010-12-23 22:14:271222 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]aab98a52009-12-02 03:22:351223 UpdateActiveExtensionsInCrashReporter();
[email protected]631cf822009-05-15 07:01:251224}
1225
[email protected]eaa7dd182010-12-14 11:09:001226void ExtensionService::UnloadAllExtensions() {
[email protected]19eb80152011-02-26 00:28:431227 if (profile_) {
1228 profile_->GetExtensionSpecialStoragePolicy()->
1229 RevokeRightsForAllExtensions();
1230 }
[email protected]9f1087e2009-06-15 17:29:321231 extensions_.clear();
[email protected]cd500f72010-06-25 23:44:321232 disabled_extensions_.clear();
[email protected]bb7f40952011-01-13 00:21:201233 terminated_extension_ids_.clear();
1234 terminated_extensions_.clear();
[email protected]d7e9a862010-11-03 21:57:491235 extension_runtime_data_.clear();
[email protected]cd500f72010-06-25 23:44:321236
[email protected]c6e4a3412009-06-24 15:45:291237 // TODO(erikkay) should there be a notification for this? We can't use
1238 // EXTENSION_UNLOADED since that implies that the extension has been disabled
1239 // or uninstalled, and UnloadAll is just part of shutdown.
[email protected]9f1087e2009-06-15 17:29:321240}
1241
[email protected]eaa7dd182010-12-14 11:09:001242void ExtensionService::ReloadExtensions() {
[email protected]9f1087e2009-06-15 17:29:321243 UnloadAllExtensions();
1244 LoadAllExtensions();
1245}
1246
[email protected]eaa7dd182010-12-14 11:09:001247void ExtensionService::GarbageCollectExtensions() {
[email protected]39d9f62c2010-12-03 10:48:501248 if (extension_prefs_->pref_service()->ReadOnly())
[email protected]ba399672010-04-06 15:42:391249 return;
1250
[email protected]ca3dbf52010-05-19 22:27:061251 scoped_ptr<ExtensionPrefs::ExtensionsInfo> info(
1252 extension_prefs_->GetInstalledExtensionsInfo());
1253
1254 std::map<std::string, FilePath> extension_paths;
1255 for (size_t i = 0; i < info->size(); ++i)
1256 extension_paths[info->at(i)->extension_id] = info->at(i)->extension_path;
1257
[email protected]ca4b5fa32010-10-09 12:42:181258 BrowserThread::PostTask(
1259 BrowserThread::FILE, FROM_HERE,
[email protected]95d29192009-10-30 01:49:061260 NewRunnableFunction(
1261 &extension_file_util::GarbageCollectExtensions, install_directory_,
[email protected]ca3dbf52010-05-19 22:27:061262 extension_paths));
[email protected]f6ccd652010-11-17 00:48:341263
1264 // Also garbage-collect themes. We check |profile_| to be
1265 // defensive; in the future, we may call GarbageCollectExtensions()
1266 // from somewhere other than Init() (e.g., in a timer).
1267 if (profile_) {
[email protected]18280372011-03-22 18:05:221268 ThemeServiceFactory::GetForProfile(profile_)->RemoveUnusedThemes();
[email protected]f6ccd652010-11-17 00:48:341269 }
[email protected]3cf4f0992009-02-03 23:00:301270}
1271
[email protected]eaa7dd182010-12-14 11:09:001272void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]93fd78f42009-07-10 16:43:171273 if (updater_.get()) {
1274 updater_->Start();
1275 }
[email protected]fa6a9102010-11-22 15:38:501276
1277 ready_ = true;
[email protected]e72e8eb82009-06-18 17:21:511278 NotificationService::current()->Notify(
1279 NotificationType::EXTENSIONS_READY,
[email protected]24e7a9d2009-11-04 11:11:341280 Source<Profile>(profile_),
[email protected]e72e8eb82009-06-18 17:21:511281 NotificationService::NoDetails());
1282}
1283
[email protected]49cd1572011-02-08 21:38:451284void ExtensionService::AddExtension(const Extension* extension) {
[email protected]ae09ca62009-08-21 19:46:461285 // Ensure extension is deleted unless we transfer ownership.
[email protected]9adb9692010-10-29 23:14:021286 scoped_refptr<const Extension> scoped_extension(extension);
[email protected]9f1087e2009-06-15 17:29:321287
[email protected]1eb175082010-02-10 09:26:161288 // The extension is now loaded, remove its data from unloaded extension map.
1289 unloaded_extension_paths_.erase(extension->id());
1290
[email protected]bb7f40952011-01-13 00:21:201291 // If a terminated extension is loaded, remove it from the terminated list.
1292 UntrackTerminatedExtension(extension->id());
1293
[email protected]f17dbd42010-08-16 23:21:101294 // If the extension was disabled for a reload, then enable it.
1295 if (disabled_extension_paths_.erase(extension->id()) > 0)
1296 EnableExtension(extension->id());
1297
[email protected]d728e002010-12-08 04:46:231298 // TODO(jstritar): We may be able to get rid of this branch by overriding the
1299 // default extension state to DISABLED when the --disable-extensions flag
1300 // is set (http://crbug.com/29067).
1301 if (!extensions_enabled() &&
1302 !extension->is_theme() &&
[email protected]aebe23a32010-12-10 22:15:481303 extension->location() != Extension::COMPONENT &&
1304 !Extension::IsExternalLocation(extension->location()))
[email protected]d728e002010-12-08 04:46:231305 return;
1306
1307 // Check if the extension's privileges have changed and disable the
1308 // extension if necessary.
[email protected]8d888c12010-11-30 00:00:251309 DisableIfPrivilegeIncrease(extension);
[email protected]0c6da502009-08-14 22:32:391310
[email protected]8d888c12010-11-30 00:00:251311 switch (extension_prefs_->GetExtensionState(extension->id())) {
1312 case Extension::ENABLED:
1313 extensions_.push_back(scoped_extension);
[email protected]1e8c93f2010-02-08 22:58:311314
[email protected]8d888c12010-11-30 00:00:251315 NotifyExtensionLoaded(extension);
[email protected]0c6da502009-08-14 22:32:391316
[email protected]8f9d4eb2011-02-05 01:39:101317 ExtensionWebUI::RegisterChromeURLOverrides(
[email protected]8d888c12010-11-30 00:00:251318 profile_, extension->GetChromeURLOverrides());
1319 break;
1320 case Extension::DISABLED:
1321 disabled_extensions_.push_back(scoped_extension);
1322 NotificationService::current()->Notify(
1323 NotificationType::EXTENSION_UPDATE_DISABLED,
1324 Source<Profile>(profile_),
1325 Details<const Extension>(extension));
1326 break;
1327 default:
1328 NOTREACHED();
1329 break;
[email protected]e72e8eb82009-06-18 17:21:511330 }
[email protected]aab98a52009-12-02 03:22:351331
[email protected]d7e9a862010-11-03 21:57:491332 SetBeingUpgraded(extension, false);
[email protected]1e8c93f2010-02-08 22:58:311333
[email protected]aab98a52009-12-02 03:22:351334 UpdateActiveExtensionsInCrashReporter();
[email protected]0b004da2010-07-02 17:54:311335
1336 if (profile_->GetTemplateURLModel())
1337 profile_->GetTemplateURLModel()->RegisterExtensionKeyword(extension);
[email protected]b671760b2010-07-15 21:13:471338
1339 // Load the icon for omnibox-enabled extensions so it will be ready to display
1340 // in the URL bar.
[email protected]29d0d4ac2010-09-08 21:10:311341 if (!extension->omnibox_keyword().empty()) {
1342 omnibox_popup_icon_manager_.LoadIcon(extension);
[email protected]b671760b2010-07-15 21:13:471343 omnibox_icon_manager_.LoadIcon(extension);
[email protected]29d0d4ac2010-09-08 21:10:311344 }
[email protected]aab98a52009-12-02 03:22:351345}
1346
[email protected]eaa7dd182010-12-14 11:09:001347void ExtensionService::DisableIfPrivilegeIncrease(const Extension* extension) {
[email protected]8d888c12010-11-30 00:00:251348 // We keep track of all permissions the user has granted each extension.
1349 // This allows extensions to gracefully support backwards compatibility
1350 // by including unknown permissions in their manifests. When the user
1351 // installs the extension, only the recognized permissions are recorded.
1352 // When the unknown permissions become recognized (e.g., through browser
1353 // upgrade), we can prompt the user to accept these new permissions.
1354 // Extensions can also silently upgrade to less permissions, and then
1355 // silently upgrade to a version that adds these permissions back.
1356 //
1357 // For example, pretend that Chrome 10 includes a permission "omnibox"
1358 // for an API that adds suggestions to the omnibox. An extension can
1359 // maintain backwards compatibility while still having "omnibox" in the
1360 // manifest. If a user installs the extension on Chrome 9, the browser
1361 // will record the permissions it recognized, not including "omnibox."
1362 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
1363 // will disable the extension and prompt the user to approve the increase
1364 // in privileges. The extension could then release a new version that
1365 // removes the "omnibox" permission. When the user upgrades, Chrome will
1366 // still remember that "omnibox" had been granted, so that if the
1367 // extension once again includes "omnibox" in an upgrade, the extension
1368 // can upgrade without requiring this user's approval.
1369 const Extension* old = GetExtensionByIdInternal(extension->id(),
1370 true, true);
1371 bool granted_full_access;
1372 std::set<std::string> granted_apis;
1373 ExtensionExtent granted_extent;
1374
1375 bool is_extension_upgrade = old != NULL;
1376 bool is_privilege_increase = false;
1377
1378 // We only record the granted permissions for INTERNAL extensions, since
1379 // they can't silently increase privileges.
1380 if (extension->location() == Extension::INTERNAL) {
1381 // Add all the recognized permissions if the granted permissions list
1382 // hasn't been initialized yet.
1383 if (!extension_prefs_->GetGrantedPermissions(extension->id(),
1384 &granted_full_access,
1385 &granted_apis,
1386 &granted_extent)) {
1387 GrantPermissions(extension);
1388 CHECK(extension_prefs_->GetGrantedPermissions(extension->id(),
1389 &granted_full_access,
1390 &granted_apis,
1391 &granted_extent));
1392 }
1393
1394 // Here, we check if an extension's privileges have increased in a manner
1395 // that requires the user's approval. This could occur because the browser
1396 // upgraded and recognized additional privileges, or an extension upgrades
1397 // to a version that requires additional privileges.
1398 is_privilege_increase = Extension::IsPrivilegeIncrease(
1399 granted_full_access, granted_apis, granted_extent, extension);
1400 }
1401
1402 if (is_extension_upgrade) {
[email protected]66e26872010-12-03 20:07:251403 // Other than for unpacked extensions, CrxInstaller should have guaranteed
1404 // that we aren't downgrading.
1405 if (extension->location() != Extension::LOAD)
1406 CHECK(extension->version()->CompareTo(*(old->version())) >= 0);
[email protected]8d888c12010-11-30 00:00:251407
1408 // Extensions get upgraded if the privileges are allowed to increase or
1409 // the privileges haven't increased.
1410 if (!is_privilege_increase) {
1411 SetBeingUpgraded(old, true);
1412 SetBeingUpgraded(extension, true);
1413 }
1414
1415 // To upgrade an extension in place, unload the old one and
1416 // then load the new one.
[email protected]a9f39a312010-12-23 22:14:271417 UnloadExtension(old->id(), UnloadedExtensionInfo::UPDATE);
[email protected]8d888c12010-11-30 00:00:251418 old = NULL;
1419 }
1420
1421 // Extension has changed permissions significantly. Disable it. A
1422 // notification should be sent by the caller.
1423 if (is_privilege_increase) {
1424 extension_prefs_->SetExtensionState(extension, Extension::DISABLED);
1425 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
1426 }
1427}
1428
[email protected]eaa7dd182010-12-14 11:09:001429void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:391430 std::set<std::string> extension_ids;
[email protected]aab98a52009-12-02 03:22:351431 for (size_t i = 0; i < extensions_.size(); ++i) {
[email protected]2a500c1b2010-11-05 15:27:341432 if (!extensions_[i]->is_theme() &&
1433 extensions_[i]->location() != Extension::COMPONENT)
[email protected]c8865962009-12-16 07:47:391434 extension_ids.insert(extensions_[i]->id());
[email protected]aab98a52009-12-02 03:22:351435 }
1436
1437 child_process_logging::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:251438}
1439
[email protected]eaa7dd182010-12-14 11:09:001440void ExtensionService::OnExtensionInstalled(const Extension* extension) {
[email protected]a29a517a2011-01-21 21:11:121441 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:201442
[email protected]4416c5a2010-06-26 01:28:571443 // Ensure extension is deleted unless we transfer ownership.
[email protected]9adb9692010-10-29 23:14:021444 scoped_refptr<const Extension> scoped_extension(extension);
[email protected]4416c5a2010-06-26 01:28:571445 Extension::State initial_state = Extension::DISABLED;
1446 bool initial_enable_incognito = false;
[email protected]b2907fd2011-03-25 16:43:371447
1448 PendingExtensionInfo pending_extension_info;
1449 if (pending_extension_manager()->GetById(extension->id(),
1450 &pending_extension_info)) {
1451 pending_extension_manager()->Remove(extension->id());
[email protected]8ef78fd2010-08-19 17:14:321452
[email protected]483d1ff2011-03-03 17:12:021453 if (!pending_extension_info.ShouldAllowInstall(*extension)) {
[email protected]4416c5a2010-06-26 01:28:571454 LOG(WARNING)
[email protected]483d1ff2011-03-03 17:12:021455 << "should_install_extension() returned false for "
1456 << extension->id() << " of type " << extension->GetType()
[email protected]7fa19f82010-12-21 19:40:081457 << " and update URL " << extension->update_url().spec()
1458 << "; not installing";
[email protected]4416c5a2010-06-26 01:28:571459 // Delete the extension directory since we're not going to
1460 // load it.
[email protected]ca4b5fa32010-10-09 12:42:181461 BrowserThread::PostTask(
1462 BrowserThread::FILE, FROM_HERE,
[email protected]b2907fd2011-03-25 16:43:371463 NewRunnableFunction(&extension_file_util::DeleteFile,
1464 extension->path(), true));
[email protected]4416c5a2010-06-26 01:28:571465 return;
1466 }
[email protected]8ef78fd2010-08-19 17:14:321467
[email protected]8ef78fd2010-08-19 17:14:321468 if (extension->is_theme()) {
[email protected]483d1ff2011-03-03 17:12:021469 DCHECK(pending_extension_info.enable_on_install());
[email protected]4416c5a2010-06-26 01:28:571470 initial_state = Extension::ENABLED;
[email protected]483d1ff2011-03-03 17:12:021471 DCHECK(!pending_extension_info.enable_incognito_on_install());
[email protected]4416c5a2010-06-26 01:28:571472 initial_enable_incognito = false;
1473 } else {
1474 initial_state =
[email protected]483d1ff2011-03-03 17:12:021475 pending_extension_info.enable_on_install() ?
[email protected]4416c5a2010-06-26 01:28:571476 Extension::ENABLED : Extension::DISABLED;
1477 initial_enable_incognito =
[email protected]483d1ff2011-03-03 17:12:021478 pending_extension_info.enable_incognito_on_install();
[email protected]4416c5a2010-06-26 01:28:571479 }
[email protected]4416c5a2010-06-26 01:28:571480 } else {
[email protected]dbec3792010-08-10 00:08:451481 // Make sure we preserve enabled/disabled states.
[email protected]4416c5a2010-06-26 01:28:571482 Extension::State existing_state =
1483 extension_prefs_->GetExtensionState(extension->id());
1484 initial_state =
1485 (existing_state == Extension::DISABLED) ?
1486 Extension::DISABLED : Extension::ENABLED;
[email protected]dbec3792010-08-10 00:08:451487 initial_enable_incognito =
1488 extension_prefs_->IsIncognitoEnabled(extension->id());
[email protected]aa142702010-03-26 01:26:331489 }
1490
[email protected]9b217652010-10-08 22:04:231491 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
[email protected]7fa19f82010-12-21 19:40:081492 extension->GetType(), 100);
[email protected]eda3c362010-11-12 08:08:231493 ShownSectionsHandler::OnExtensionInstalled(profile_->GetPrefs(), extension);
[email protected]4416c5a2010-06-26 01:28:571494 extension_prefs_->OnExtensionInstalled(
1495 extension, initial_state, initial_enable_incognito);
[email protected]25b34332009-06-05 21:53:191496
[email protected]92a5b1d2010-07-20 00:42:001497 // Unpacked extensions start off with file access since they are a developer
1498 // feature.
1499 if (extension->location() == Extension::LOAD)
1500 extension_prefs_->SetAllowFileAccess(extension->id(), true);
1501
[email protected]4a190632009-05-09 01:07:421502 // If the extension is a theme, tell the profile (and therefore ThemeProvider)
1503 // to apply it.
[email protected]3ba0fd32010-06-19 05:39:101504 if (extension->is_theme()) {
[email protected]9ceb07342009-07-26 04:09:231505 NotificationService::current()->Notify(
1506 NotificationType::THEME_INSTALLED,
[email protected]24e7a9d2009-11-04 11:11:341507 Source<Profile>(profile_),
[email protected]9adb9692010-10-29 23:14:021508 Details<const Extension>(extension));
[email protected]9197f3b2009-06-02 00:49:271509 } else {
1510 NotificationService::current()->Notify(
1511 NotificationType::EXTENSION_INSTALLED,
[email protected]24e7a9d2009-11-04 11:11:341512 Source<Profile>(profile_),
[email protected]9adb9692010-10-29 23:14:021513 Details<const Extension>(extension));
[email protected]4a190632009-05-09 01:07:421514 }
[email protected]7577a5c52009-07-30 06:21:581515
[email protected]ec5b50d2010-10-09 16:35:181516 if (extension->is_app()) {
1517 ExtensionIdSet installed_ids = GetAppIds();
1518 installed_ids.insert(extension->id());
1519 default_apps_.DidInstallApp(installed_ids);
1520 }
1521
[email protected]49cd1572011-02-08 21:38:451522 // Transfer ownership of |extension| to AddExtension.
1523 AddExtension(scoped_extension);
[email protected]4a190632009-05-09 01:07:421524}
1525
[email protected]eaa7dd182010-12-14 11:09:001526const Extension* ExtensionService::GetExtensionByIdInternal(
[email protected]b2907fd2011-03-25 16:43:371527 const std::string& id, bool include_enabled, bool include_disabled) const {
[email protected]e957fe52009-06-23 16:51:051528 std::string lowercase_id = StringToLowerASCII(id);
[email protected]0c6da502009-08-14 22:32:391529 if (include_enabled) {
1530 for (ExtensionList::const_iterator iter = extensions_.begin();
1531 iter != extensions_.end(); ++iter) {
1532 if ((*iter)->id() == lowercase_id)
1533 return *iter;
1534 }
1535 }
1536 if (include_disabled) {
1537 for (ExtensionList::const_iterator iter = disabled_extensions_.begin();
1538 iter != disabled_extensions_.end(); ++iter) {
1539 if ((*iter)->id() == lowercase_id)
1540 return *iter;
1541 }
[email protected]ce5c4502009-05-06 16:46:111542 }
1543 return NULL;
1544}
1545
[email protected]bb7f40952011-01-13 00:21:201546void ExtensionService::TrackTerminatedExtension(const Extension* extension) {
1547 if (terminated_extension_ids_.insert(extension->id()).second)
1548 terminated_extensions_.push_back(make_scoped_refptr(extension));
1549}
1550
1551void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
1552 if (terminated_extension_ids_.erase(id) <= 0)
1553 return;
1554
1555 std::string lowercase_id = StringToLowerASCII(id);
1556 for (ExtensionList::iterator iter = terminated_extensions_.begin();
1557 iter != terminated_extensions_.end(); ++iter) {
1558 if ((*iter)->id() == lowercase_id) {
1559 terminated_extensions_.erase(iter);
1560 return;
1561 }
1562 }
1563}
1564
[email protected]0dfe05c2011-02-23 23:03:361565const Extension* ExtensionService::GetTerminatedExtension(
1566 const std::string& id) {
1567 std::string lowercase_id = StringToLowerASCII(id);
1568 for (ExtensionList::const_iterator iter = terminated_extensions_.begin();
1569 iter != terminated_extensions_.end(); ++iter) {
1570 if ((*iter)->id() == lowercase_id)
1571 return *iter;
1572 }
1573 return NULL;
1574}
1575
[email protected]eaa7dd182010-12-14 11:09:001576const Extension* ExtensionService::GetWebStoreApp() {
[email protected]d3071992010-10-08 15:24:071577 return GetExtensionById(extension_misc::kWebStoreAppId, false);
1578}
1579
[email protected]eaa7dd182010-12-14 11:09:001580const Extension* ExtensionService::GetExtensionByURL(const GURL& url) {
[email protected]a888b29e62010-04-01 13:38:571581 return url.scheme() != chrome::kExtensionScheme ? NULL :
1582 GetExtensionById(url.host(), false);
1583}
1584
[email protected]eaa7dd182010-12-14 11:09:001585const Extension* ExtensionService::GetExtensionByWebExtent(const GURL& url) {
[email protected]a888b29e62010-04-01 13:38:571586 for (size_t i = 0; i < extensions_.size(); ++i) {
1587 if (extensions_[i]->web_extent().ContainsURL(url))
1588 return extensions_[i];
1589 }
1590 return NULL;
[email protected]9f1087e2009-06-15 17:29:321591}
1592
[email protected]eaa7dd182010-12-14 11:09:001593bool ExtensionService::ExtensionBindingsAllowed(const GURL& url) {
[email protected]583d45c12010-08-31 02:48:121594 // Allow bindings for all packaged extension.
1595 if (GetExtensionByURL(url))
1596 return true;
1597
1598 // Allow bindings for all component, hosted apps.
[email protected]9adb9692010-10-29 23:14:021599 const Extension* extension = GetExtensionByWebExtent(url);
[email protected]583d45c12010-08-31 02:48:121600 return (extension && extension->location() == Extension::COMPONENT);
1601}
1602
[email protected]eaa7dd182010-12-14 11:09:001603const Extension* ExtensionService::GetExtensionByOverlappingWebExtent(
[email protected]9f72aa02010-06-25 10:01:051604 const ExtensionExtent& extent) {
[email protected]22c966c2010-06-26 06:35:021605 for (size_t i = 0; i < extensions_.size(); ++i) {
1606 if (extensions_[i]->web_extent().OverlapsWith(extent))
1607 return extensions_[i];
1608 }
1609
[email protected]6d2e60bd2010-06-03 22:37:391610 return NULL;
1611}
1612
[email protected]eaa7dd182010-12-14 11:09:001613const SkBitmap& ExtensionService::GetOmniboxIcon(
[email protected]b671760b2010-07-15 21:13:471614 const std::string& extension_id) {
1615 return omnibox_icon_manager_.GetIcon(extension_id);
1616}
1617
[email protected]eaa7dd182010-12-14 11:09:001618const SkBitmap& ExtensionService::GetOmniboxPopupIcon(
[email protected]29d0d4ac2010-09-08 21:10:311619 const std::string& extension_id) {
1620 return omnibox_popup_icon_manager_.GetIcon(extension_id);
1621}
1622
[email protected]eaa7dd182010-12-14 11:09:001623void ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:321624 const std::string& id,
[email protected]8e4560b62011-01-14 10:09:141625 const Version* version,
[email protected]8ef78fd2010-08-19 17:14:321626 const FilePath& path,
1627 Extension::Location location) {
[email protected]ab22ba42011-01-14 16:36:381628 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1629 CHECK(Extension::IdIsValid(id));
[email protected]683d0702010-12-06 16:25:571630 if (extension_prefs_->IsExtensionKilled(id))
1631 return;
[email protected]a8af9fdb2010-10-28 21:52:201632
[email protected]8e4560b62011-01-14 10:09:141633 DCHECK(version);
1634
[email protected]7577a5c52009-07-30 06:21:581635 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:491636 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:581637 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:021638 const Extension* existing = GetExtensionById(id, true);
[email protected]7577a5c52009-07-30 06:21:581639 if (existing) {
[email protected]8e4560b62011-01-14 10:09:141640 switch (existing->version()->CompareTo(*version)) {
[email protected]7577a5c52009-07-30 06:21:581641 case -1: // existing version is older, we should upgrade
1642 break;
1643 case 0: // existing version is same, do nothing
1644 return;
1645 case 1: // existing version is newer, uh-oh
1646 LOG(WARNING) << "Found external version of extension " << id
1647 << "that is older than current version. Current version "
1648 << "is: " << existing->VersionString() << ". New version "
1649 << "is: " << version << ". Keeping current version.";
1650 return;
1651 }
1652 }
1653
[email protected]b2907fd2011-03-25 16:43:371654 pending_extension_manager()->AddFromExternalFile(id, location);
[email protected]9c635f22010-12-02 09:36:361655
[email protected]6dfbbf82010-03-12 23:09:161656 scoped_refptr<CrxInstaller> installer(
[email protected]bb461532010-11-26 21:50:231657 new CrxInstaller(this, // frontend
[email protected]6dfbbf82010-03-12 23:09:161658 NULL)); // no client (silent install)
1659 installer->set_install_source(location);
1660 installer->set_expected_id(id);
[email protected]604322d2011-03-22 16:51:561661 installer->set_expected_version(*version),
[email protected]6dfbbf82010-03-12 23:09:161662 installer->InstallCrx(path);
[email protected]7577a5c52009-07-30 06:21:581663}
1664
[email protected]eaa7dd182010-12-14 11:09:001665void ExtensionService::ReportExtensionLoadError(
[email protected]d11c8e92009-10-20 23:26:401666 const FilePath& extension_path,
1667 const std::string &error,
1668 NotificationType type,
1669 bool be_noisy) {
1670 NotificationService* service = NotificationService::current();
1671 service->Notify(type,
[email protected]24e7a9d2009-11-04 11:11:341672 Source<Profile>(profile_),
[email protected]d11c8e92009-10-20 23:26:401673 Details<const std::string>(&error));
1674
[email protected]8a205c02011-02-04 20:41:331675 std::string path_str = UTF16ToUTF8(extension_path.LossyDisplayName());
[email protected]18d4b6c2010-09-21 03:21:041676 std::string message = base::StringPrintf(
1677 "Could not load extension from '%s'. %s",
1678 path_str.c_str(), error.c_str());
[email protected]d11c8e92009-10-20 23:26:401679 ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy);
1680}
1681
[email protected]eaa7dd182010-12-14 11:09:001682void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]406027c02010-09-27 08:03:181683 ExtensionHost* host) {
1684 OrphanedDevTools::iterator iter =
1685 orphaned_dev_tools_.find(host->extension()->id());
1686 if (iter == orphaned_dev_tools_.end())
1687 return;
1688
1689 DevToolsManager::GetInstance()->AttachClientHost(
1690 iter->second, host->render_view_host());
1691 orphaned_dev_tools_.erase(iter);
1692}
1693
[email protected]eaa7dd182010-12-14 11:09:001694void ExtensionService::Observe(NotificationType type,
[email protected]4814b512009-11-07 00:12:291695 const NotificationSource& source,
1696 const NotificationDetails& details) {
1697 switch (type.value) {
[email protected]a4ed6282009-12-14 20:51:161698 case NotificationType::EXTENSION_PROCESS_TERMINATED: {
[email protected]bc535ee52010-08-31 18:40:321699 if (profile_ != Source<Profile>(source).ptr()->GetOriginalProfile())
1700 break;
[email protected]a4ed6282009-12-14 20:51:161701
[email protected]f128af42010-08-05 18:05:261702 ExtensionHost* host = Details<ExtensionHost>(details).ptr();
[email protected]bb7f40952011-01-13 00:21:201703 TrackTerminatedExtension(host->extension());
[email protected]f128af42010-08-05 18:05:261704
[email protected]31f77262009-12-02 20:48:531705 // Unload the entire extension. We want it to be in a consistent state:
1706 // either fully working or not loaded at all, but never half-crashed.
[email protected]bc535ee52010-08-31 18:40:321707 // We do it in a PostTask so that other handlers of this notification will
1708 // still have access to the Extension and ExtensionHost.
1709 MessageLoop::current()->PostTask(FROM_HERE,
[email protected]a9f39a312010-12-23 22:14:271710 NewRunnableMethod(this,
1711 &ExtensionService::UnloadExtension,
1712 host->extension()->id(),
1713 UnloadedExtensionInfo::DISABLE));
[email protected]31f77262009-12-02 20:48:531714 break;
1715 }
1716
[email protected]aa96d3a2010-08-21 08:45:251717 case NotificationType::PREF_CHANGED: {
1718 std::string* pref_name = Details<std::string>(details).ptr();
[email protected]6c751e72010-11-23 10:11:101719 if (*pref_name == prefs::kExtensionInstallAllowList ||
1720 *pref_name == prefs::kExtensionInstallDenyList) {
1721 CheckAdminBlacklist();
[email protected]6c751e72010-11-23 10:11:101722 } else {
1723 NOTREACHED() << "Unexpected preference name.";
1724 }
[email protected]aa96d3a2010-08-21 08:45:251725 break;
1726 }
1727
[email protected]4814b512009-11-07 00:12:291728 default:
1729 NOTREACHED() << "Unexpected notification type.";
1730 }
1731}
1732
[email protected]eaa7dd182010-12-14 11:09:001733bool ExtensionService::HasApps() const {
[email protected]ec5b50d2010-10-09 16:35:181734 return !GetAppIds().empty();
1735}
[email protected]377011d2010-07-20 04:18:501736
[email protected]eaa7dd182010-12-14 11:09:001737ExtensionIdSet ExtensionService::GetAppIds() const {
[email protected]ec5b50d2010-10-09 16:35:181738 ExtensionIdSet result;
[email protected]377011d2010-07-20 04:18:501739 for (ExtensionList::const_iterator it = extensions_.begin();
1740 it != extensions_.end(); ++it) {
[email protected]ec5b50d2010-10-09 16:35:181741 if ((*it)->is_app() && (*it)->location() != Extension::COMPONENT)
1742 result.insert((*it)->id());
[email protected]377011d2010-07-20 04:18:501743 }
1744
[email protected]ec5b50d2010-10-09 16:35:181745 return result;
[email protected]377011d2010-07-20 04:18:501746}
[email protected]d7e9a862010-11-03 21:57:491747
[email protected]eaa7dd182010-12-14 11:09:001748bool ExtensionService::IsBackgroundPageReady(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:491749 return (extension->background_url().is_empty() ||
1750 extension_runtime_data_[extension->id()].background_page_ready);
1751}
1752
[email protected]eaa7dd182010-12-14 11:09:001753void ExtensionService::SetBackgroundPageReady(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:491754 DCHECK(!extension->background_url().is_empty());
1755 extension_runtime_data_[extension->id()].background_page_ready = true;
1756 NotificationService::current()->Notify(
1757 NotificationType::EXTENSION_BACKGROUND_PAGE_READY,
1758 Source<const Extension>(extension),
1759 NotificationService::NoDetails());
1760}
1761
[email protected]eaa7dd182010-12-14 11:09:001762bool ExtensionService::IsBeingUpgraded(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:491763 return extension_runtime_data_[extension->id()].being_upgraded;
1764}
1765
[email protected]eaa7dd182010-12-14 11:09:001766void ExtensionService::SetBeingUpgraded(const Extension* extension,
[email protected]d7e9a862010-11-03 21:57:491767 bool value) {
1768 extension_runtime_data_[extension->id()].being_upgraded = value;
1769}
[email protected]1bead0712010-11-27 17:41:531770
[email protected]eaa7dd182010-12-14 11:09:001771PropertyBag* ExtensionService::GetPropertyBag(const Extension* extension) {
[email protected]1bead0712010-11-27 17:41:531772 return &extension_runtime_data_[extension->id()].property_bag;
1773}