blob: bb24e1e51bfdae6f55ae2cccdec0eb299c2d1699 [file] [log] [blame]
[email protected]1e8c93f2010-02-08 22:58:311// Copyright (c) 2010 The Chromium Authors. All rights reserved.
[email protected]6014d672008-12-05 00:38:252// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[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>
8
[email protected]24b538a2010-02-27 01:22:449#include "base/basictypes.h"
[email protected]e2eb43112009-05-29 21:19:5410#include "base/command_line.h"
[email protected]6014d672008-12-05 00:38:2511#include "base/file_util.h"
[email protected]835d7c82010-10-14 04:38:3812#include "base/metrics/histogram.h"
[email protected]cd500f72010-06-25 23:44:3213#include "base/stl_util-inl.h"
[email protected]24b538a2010-02-27 01:22:4414#include "base/string16.h"
[email protected]e83326f2010-07-31 17:29:2515#include "base/string_number_conversions.h"
[email protected]6014d672008-12-05 00:38:2516#include "base/string_util.h"
[email protected]18d4b6c2010-09-21 03:21:0417#include "base/stringprintf.h"
[email protected]a315ba92010-11-16 14:12:2118#include "base/thread_restrictions.h"
[email protected]cc2c3432009-11-06 17:24:3619#include "base/time.h"
[email protected]ce7f62e32010-08-10 23:43:5920#include "base/utf_string_conversions.h"
[email protected]cc655912009-01-29 23:19:1921#include "base/values.h"
[email protected]6c751e72010-11-23 10:11:1022#include "base/values_util.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]f6ccd652010-11-17 00:48:3425#include "chrome/browser/themes/browser_theme_provider.h"
[email protected]ed7e6dd2010-10-12 02:02:4526#include "chrome/browser/browser_thread.h"
[email protected]4814b512009-11-07 00:12:2927#include "chrome/browser/debugger/devtools_manager.h"
[email protected]eda3c362010-11-12 08:08:2328#include "chrome/browser/dom_ui/shown_sections_handler.h"
[email protected]7577a5c52009-07-30 06:21:5829#include "chrome/browser/extensions/crx_installer.h"
[email protected]ec5b50d2010-10-09 16:35:1830#include "chrome/browser/extensions/default_apps.h"
[email protected]5cbe1e22010-01-30 01:18:5631#include "chrome/browser/extensions/extension_accessibility_api.h"
[email protected]840b0db2009-11-20 03:00:3832#include "chrome/browser/extensions/extension_bookmarks_module.h"
[email protected]b68d5ed2009-04-16 02:41:2833#include "chrome/browser/extensions/extension_browser_event_router.h"
[email protected]2c5e1e12010-06-10 13:14:4434#include "chrome/browser/extensions/extension_cookies_api.h"
[email protected]c10da4b02010-03-25 14:38:3235#include "chrome/browser/extensions/extension_data_deleter.h"
[email protected]86c008e82009-08-28 20:26:0536#include "chrome/browser/extensions/extension_dom_ui.h"
[email protected]14a000d2010-04-29 21:44:2437#include "chrome/browser/extensions/extension_error_reporter.h"
[email protected]de768a832009-10-30 05:25:0138#include "chrome/browser/extensions/extension_history_api.h"
[email protected]b1748b1d82009-11-30 20:32:5639#include "chrome/browser/extensions/extension_host.h"
[email protected]7596ce72010-08-30 05:10:4640#include "chrome/browser/extensions/extension_management_api.h"
[email protected]4814b512009-11-07 00:12:2941#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]8a661f82010-10-19 21:47:1142#include "chrome/browser/extensions/extension_processes_api.h"
[email protected]93fd78f42009-07-10 16:43:1743#include "chrome/browser/extensions/extension_updater.h"
[email protected]784688a62010-09-13 07:06:5244#include "chrome/browser/extensions/extension_webnavigation_api.h"
[email protected]a1257b12009-06-12 02:51:3445#include "chrome/browser/extensions/external_extension_provider.h"
[email protected]21a5a672010-11-04 10:47:4246#include "chrome/browser/extensions/external_policy_extension_provider.h"
[email protected]a1257b12009-06-12 02:51:3447#include "chrome/browser/extensions/external_pref_extension_provider.h"
[email protected]56ad3792010-05-28 17:45:3348#include "chrome/browser/net/chrome_url_request_context.h"
[email protected]37858e52010-08-26 00:22:0249#include "chrome/browser/prefs/pref_service.h"
[email protected]8ecad5e2010-12-02 21:18:3350#include "chrome/browser/profiles/profile.h"
[email protected]56ad3792010-05-28 17:45:3351#include "chrome/browser/search_engines/template_url_model.h"
[email protected]aab98a52009-12-02 03:22:3552#include "chrome/common/child_process_logging.h"
[email protected]e2eb43112009-05-29 21:19:5453#include "chrome/common/chrome_switches.h"
[email protected]5b1a0e22009-05-26 19:00:5854#include "chrome/common/extensions/extension.h"
[email protected]d7b36dc2009-10-29 21:47:4055#include "chrome/common/extensions/extension_constants.h"
[email protected]05c82182010-06-24 17:49:0856#include "chrome/common/extensions/extension_error_utils.h"
[email protected]7c927b62010-02-24 09:54:1357#include "chrome/common/extensions/extension_file_util.h"
[email protected]c6d474f82009-12-16 21:11:0658#include "chrome/common/extensions/extension_l10n_util.h"
[email protected]a315ba92010-11-16 14:12:2159#include "chrome/common/extensions/extension_resource.h"
[email protected]82891262008-12-24 00:21:2660#include "chrome/common/notification_service.h"
[email protected]4814b512009-11-07 00:12:2961#include "chrome/common/notification_type.h"
[email protected]1952c7d2010-03-04 23:48:3462#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]c10da4b02010-03-25 14:38:3265#include "googleurl/src/gurl.h"
[email protected]1debbbb62010-10-06 17:23:4466#include "net/base/registry_controlled_domain.h"
[email protected]24b538a2010-02-27 01:22:4467#include "webkit/database/database_tracker.h"
68#include "webkit/database/database_util.h"
[email protected]c64631652009-04-29 22:24:3169
[email protected]79db6232009-02-13 20:51:2070#if defined(OS_WIN)
[email protected]a1257b12009-06-12 02:51:3471#include "chrome/browser/extensions/external_registry_extension_provider_win.h"
[email protected]79db6232009-02-13 20:51:2072#endif
[email protected]6014d672008-12-05 00:38:2573
[email protected]5ef47ec2010-01-28 05:58:0574using base::Time;
75
[email protected]c6d474f82009-12-16 21:11:0676namespace errors = extension_manifest_errors;
77
[email protected]b6ab96d2009-08-20 18:58:1978namespace {
79
[email protected]29d0d4ac2010-09-08 21:10:3180#if defined(OS_LINUX)
81static const int kOmniboxIconPaddingLeft = 2;
82static const int kOmniboxIconPaddingRight = 2;
83#elif defined(OS_MACOSX)
84static const int kOmniboxIconPaddingLeft = 0;
85static const int kOmniboxIconPaddingRight = 2;
86#else
87static const int kOmniboxIconPaddingLeft = 0;
88static const int kOmniboxIconPaddingRight = 0;
89#endif
90
[email protected]a315ba92010-11-16 14:12:2191// The following enumeration is used in histograms matching
92// Extensions.ManifestReload* . Values may be added, as long
93// as existing values are not changed.
94enum ManifestReloadReason {
95 NOT_NEEDED = 0, // Reload not needed.
96 UNPACKED_DIR, // Unpacked directory
97 NEEDS_RELOCALIZATION, // The local has changed since we read this extension.
98 NUM_MANIFEST_RELOAD_REASONS
99};
[email protected]2111b1a2010-03-12 18:12:44100
[email protected]a315ba92010-11-16 14:12:21101ManifestReloadReason ShouldReloadExtensionManifest(const ExtensionInfo& info) {
102 // Always reload manifests of unpacked extensions, because they can change
103 // on disk independent of the manifest in our prefs.
104 if (info.extension_location == Extension::LOAD)
105 return UNPACKED_DIR;
106
107 // Reload the manifest if it needs to be relocalized.
108 if (extension_l10n_util::ShouldRelocalizeManifest(info))
109 return NEEDS_RELOCALIZATION;
110
111 return NOT_NEEDED;
[email protected]2111b1a2010-03-12 18:12:44112}
113
[email protected]9adb9692010-10-29 23:14:02114void GetExplicitOriginsInExtent(const Extension* extension,
[email protected]c2c263c2010-08-13 21:59:48115 std::vector<GURL>* origins) {
116 typedef std::vector<URLPattern> PatternList;
117 std::set<GURL> set;
118 const PatternList& patterns = extension->web_extent().patterns();
119 for (PatternList::const_iterator pattern = patterns.begin();
120 pattern != patterns.end(); ++pattern) {
121 if (pattern->match_subdomains() || pattern->match_all_urls())
122 continue;
[email protected]654512b2010-09-01 02:09:42123 // Wildcard URL schemes won't parse into a valid GURL, so explicit schemes
124 // must be used.
125 PatternList explicit_patterns = pattern->ConvertToExplicitSchemes();
126 for (PatternList::const_iterator explicit_p = explicit_patterns.begin();
127 explicit_p != explicit_patterns.end(); ++explicit_p) {
128 GURL origin = GURL(explicit_p->GetAsString()).GetOrigin();
129 if (origin.is_valid()) {
130 set.insert(origin);
131 } else {
132 NOTREACHED();
133 }
134 }
[email protected]c2c263c2010-08-13 21:59:48135 }
136
137 for (std::set<GURL>::const_iterator unique = set.begin();
138 unique != set.end(); ++unique) {
139 origins->push_back(*unique);
140 }
141}
142
[email protected]c6d474f82009-12-16 21:11:06143} // namespace
[email protected]b6ab96d2009-08-20 18:58:19144
[email protected]8ef78fd2010-08-19 17:14:32145PendingExtensionInfo::PendingExtensionInfo(
146 const GURL& update_url,
[email protected]7fa19f82010-12-21 19:40:08147 ShouldInstallExtensionPredicate should_install_extension,
[email protected]8ef78fd2010-08-19 17:14:32148 bool is_from_sync,
149 bool install_silently,
150 bool enable_on_install,
[email protected]ec5b50d2010-10-09 16:35:18151 bool enable_incognito_on_install,
152 Extension::Location location)
[email protected]aa142702010-03-26 01:26:33153 : update_url(update_url),
[email protected]7fa19f82010-12-21 19:40:08154 should_install_extension(should_install_extension),
[email protected]8ef78fd2010-08-19 17:14:32155 is_from_sync(is_from_sync),
[email protected]4416c5a2010-06-26 01:28:57156 install_silently(install_silently),
157 enable_on_install(enable_on_install),
[email protected]ec5b50d2010-10-09 16:35:18158 enable_incognito_on_install(enable_incognito_on_install),
159 install_source(location) {}
[email protected]aa142702010-03-26 01:26:33160
161PendingExtensionInfo::PendingExtensionInfo()
162 : update_url(),
[email protected]7fa19f82010-12-21 19:40:08163 should_install_extension(NULL),
[email protected]8ef78fd2010-08-19 17:14:32164 is_from_sync(true),
[email protected]4416c5a2010-06-26 01:28:57165 install_silently(false),
166 enable_on_install(false),
[email protected]ec5b50d2010-10-09 16:35:18167 enable_incognito_on_install(false),
168 install_source(Extension::INVALID) {}
[email protected]aa142702010-03-26 01:26:33169
[email protected]d7e9a862010-11-03 21:57:49170
[email protected]eaa7dd182010-12-14 11:09:00171ExtensionService::ExtensionRuntimeData::ExtensionRuntimeData()
[email protected]d7e9a862010-11-03 21:57:49172 : background_page_ready(false),
173 being_upgraded(false) {
174}
175
[email protected]eaa7dd182010-12-14 11:09:00176ExtensionService::ExtensionRuntimeData::~ExtensionRuntimeData() {
[email protected]d7e9a862010-11-03 21:57:49177}
178
[email protected]eaa7dd182010-12-14 11:09:00179// ExtensionService.
[email protected]6014d672008-12-05 00:38:25180
[email protected]eaa7dd182010-12-14 11:09:00181const char* ExtensionService::kInstallDirectoryName = "Extensions";
182const char* ExtensionService::kCurrentVersionFileName = "Current Version";
[email protected]494c06e2009-07-25 01:06:42183
[email protected]eaa7dd182010-12-14 11:09:00184// Implements IO for the ExtensionService.
[email protected]7a4c6852010-09-16 03:44:22185
[email protected]eaa7dd182010-12-14 11:09:00186class ExtensionServiceBackend
187 : public base::RefCountedThreadSafe<ExtensionServiceBackend>,
[email protected]7a4c6852010-09-16 03:44:22188 public ExternalExtensionProvider::Visitor {
189 public:
[email protected]1f830eb2010-09-28 08:25:14190 // |install_directory| is a path where to look for extensions to load.
[email protected]eaa7dd182010-12-14 11:09:00191 ExtensionServiceBackend(PrefService* prefs,
[email protected]aebe23a32010-12-10 22:15:48192 const FilePath& install_directory);
[email protected]7a4c6852010-09-16 03:44:22193
194 // Loads a single extension from |path| where |path| is the top directory of
195 // a specific extension where its manifest file lives.
196 // Errors are reported through ExtensionErrorReporter. On success,
197 // OnExtensionLoaded() is called.
198 // TODO(erikkay): It might be useful to be able to load a packed extension
199 // (presumably into memory) without installing it.
200 void LoadSingleExtension(const FilePath &path,
[email protected]eaa7dd182010-12-14 11:09:00201 scoped_refptr<ExtensionService> frontend);
[email protected]7a4c6852010-09-16 03:44:22202
203 // Check externally updated extensions for updates and install if necessary.
204 // Errors are reported through ExtensionErrorReporter. Succcess is not
205 // reported.
[email protected]eaa7dd182010-12-14 11:09:00206 void CheckForExternalUpdates(scoped_refptr<ExtensionService> frontend);
[email protected]7a4c6852010-09-16 03:44:22207
208 // For the extension in |version_path| with |id|, check to see if it's an
209 // externally managed extension. If so, tell the frontend to uninstall it.
[email protected]eaa7dd182010-12-14 11:09:00210 void CheckExternalUninstall(scoped_refptr<ExtensionService> frontend,
[email protected]0a60a2e2010-10-25 16:15:21211 const std::string& id);
[email protected]7a4c6852010-09-16 03:44:22212
213 // Clear all ExternalExtensionProviders.
214 void ClearProvidersForTesting();
215
[email protected]0a60a2e2010-10-25 16:15:21216 // Adds an ExternalExtensionProvider for the service to use during testing.
217 // Takes ownership of |test_provider|.
218 void AddProviderForTesting(ExternalExtensionProvider* test_provider);
[email protected]7a4c6852010-09-16 03:44:22219
220 // ExternalExtensionProvider::Visitor implementation.
221 virtual void OnExternalExtensionFileFound(const std::string& id,
222 const Version* version,
223 const FilePath& path,
224 Extension::Location location);
225
[email protected]a424d84c2010-09-24 09:31:15226 virtual void OnExternalExtensionUpdateUrlFound(const std::string& id,
[email protected]21a5a672010-11-04 10:47:42227 const GURL& update_url,
228 Extension::Location location);
[email protected]7a4c6852010-09-16 03:44:22229
[email protected]6c751e72010-11-23 10:11:10230 virtual void UpdateExternalPolicyExtensionProvider(
231 scoped_refptr<RefCountedList> forcelist);
232
[email protected]7a4c6852010-09-16 03:44:22233 private:
[email protected]eaa7dd182010-12-14 11:09:00234 friend class base::RefCountedThreadSafe<ExtensionServiceBackend>;
[email protected]7a4c6852010-09-16 03:44:22235
[email protected]eaa7dd182010-12-14 11:09:00236 virtual ~ExtensionServiceBackend();
[email protected]7a4c6852010-09-16 03:44:22237
238 // Finish installing the extension in |crx_path| after it has been unpacked to
239 // |unpacked_path|. If |expected_id| is not empty, it's verified against the
240 // extension's manifest before installation. If |silent| is true, there will
241 // be no install confirmation dialog. |from_gallery| indicates whether the
242 // crx was installed from our gallery, which results in different UI.
243 //
244 // Note: We take ownership of |extension|.
245 void OnExtensionUnpacked(const FilePath& crx_path,
246 const FilePath& unpacked_path,
[email protected]9adb9692010-10-29 23:14:02247 const Extension* extension,
[email protected]7a4c6852010-09-16 03:44:22248 const std::string expected_id);
249
250 // Notify the frontend that there was an error loading an extension.
251 void ReportExtensionLoadError(const FilePath& extension_path,
252 const std::string& error);
253
[email protected]7a4c6852010-09-16 03:44:22254 // This is a naked pointer which is set by each entry point.
255 // The entry point is responsible for ensuring lifetime.
[email protected]eaa7dd182010-12-14 11:09:00256 ExtensionService* frontend_;
[email protected]7a4c6852010-09-16 03:44:22257
258 // The top-level extensions directory being installed to.
259 FilePath install_directory_;
260
261 // Whether errors result in noisy alerts.
262 bool alert_on_error_;
263
[email protected]0a60a2e2010-10-25 16:15:21264 // A collection of external extension providers. Each provider reads
265 // a source of external extension information. Examples include the
266 // windows registry and external_extensions.json.
267 typedef std::vector<linked_ptr<ExternalExtensionProvider> >
268 ProviderCollection;
269 ProviderCollection external_extension_providers_;
[email protected]6c751e72010-11-23 10:11:10270 linked_ptr<ExternalPolicyExtensionProvider>
271 external_policy_extension_provider_;
[email protected]7a4c6852010-09-16 03:44:22272
273 // Set to true by OnExternalExtensionUpdateUrlFound() when an external
274 // extension URL is found. Used in CheckForExternalUpdates() to see
275 // if an update check is needed to install pending extensions.
276 bool external_extension_added_;
277
[email protected]eaa7dd182010-12-14 11:09:00278 DISALLOW_COPY_AND_ASSIGN(ExtensionServiceBackend);
[email protected]7a4c6852010-09-16 03:44:22279};
280
[email protected]eaa7dd182010-12-14 11:09:00281ExtensionServiceBackend::ExtensionServiceBackend(
[email protected]21a5a672010-11-04 10:47:42282 PrefService* prefs,
[email protected]aebe23a32010-12-10 22:15:48283 const FilePath& install_directory)
[email protected]7a4c6852010-09-16 03:44:22284 : frontend_(NULL),
285 install_directory_(install_directory),
286 alert_on_error_(false),
287 external_extension_added_(false) {
288 // TODO(aa): This ends up doing blocking IO on the UI thread because it reads
289 // pref data in the ctor and that is called on the UI thread. Would be better
290 // to re-read data each time we list external extensions, anyway.
[email protected]0a60a2e2010-10-25 16:15:21291 external_extension_providers_.push_back(
[email protected]7a4c6852010-09-16 03:44:22292 linked_ptr<ExternalExtensionProvider>(
[email protected]0a60a2e2010-10-25 16:15:21293 new ExternalPrefExtensionProvider()));
[email protected]7a4c6852010-09-16 03:44:22294#if defined(OS_WIN)
[email protected]0a60a2e2010-10-25 16:15:21295 external_extension_providers_.push_back(
[email protected]7a4c6852010-09-16 03:44:22296 linked_ptr<ExternalExtensionProvider>(
[email protected]0a60a2e2010-10-25 16:15:21297 new ExternalRegistryExtensionProvider()));
[email protected]7a4c6852010-09-16 03:44:22298#endif
[email protected]6c751e72010-11-23 10:11:10299 // The policy-controlled extension provider is also stored in a member
300 // variable so that UpdateExternalPolicyExtensionProvider can access it and
301 // update its extension list later.
302 external_policy_extension_provider_.reset(
[email protected]7d947da2010-12-17 11:06:57303 new ExternalPolicyExtensionProvider(
304 prefs->GetList(prefs::kExtensionInstallForceList)));
[email protected]6c751e72010-11-23 10:11:10305 external_extension_providers_.push_back(external_policy_extension_provider_);
[email protected]7a4c6852010-09-16 03:44:22306}
307
[email protected]eaa7dd182010-12-14 11:09:00308ExtensionServiceBackend::~ExtensionServiceBackend() {
[email protected]7a4c6852010-09-16 03:44:22309}
310
[email protected]eaa7dd182010-12-14 11:09:00311void ExtensionServiceBackend::LoadSingleExtension(
312 const FilePath& path_in, scoped_refptr<ExtensionService> frontend) {
[email protected]a8af9fdb2010-10-28 21:52:20313 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
314
[email protected]7a4c6852010-09-16 03:44:22315 frontend_ = frontend;
316
317 // Explicit UI loads are always noisy.
318 alert_on_error_ = true;
319
320 FilePath extension_path = path_in;
321 file_util::AbsolutePath(&extension_path);
322
[email protected]7a4c6852010-09-16 03:44:22323 std::string error;
[email protected]ad8e04a2010-11-01 04:16:27324 scoped_refptr<const Extension> extension(extension_file_util::LoadExtension(
[email protected]7a4c6852010-09-16 03:44:22325 extension_path,
[email protected]92888082010-10-18 19:24:57326 Extension::LOAD,
[email protected]7a4c6852010-09-16 03:44:22327 false, // Don't require id
[email protected]ad8e04a2010-11-01 04:16:27328 &error));
[email protected]7a4c6852010-09-16 03:44:22329
330 if (!extension) {
331 ReportExtensionLoadError(extension_path, error);
332 return;
333 }
334
[email protected]7a4c6852010-09-16 03:44:22335 // Report this as an installed extension so that it gets remembered in the
336 // prefs.
[email protected]ca4b5fa32010-10-09 12:42:18337 BrowserThread::PostTask(
338 BrowserThread::UI, FROM_HERE,
[email protected]8d888c12010-11-30 00:00:25339 NewRunnableMethod(frontend_,
[email protected]eaa7dd182010-12-14 11:09:00340 &ExtensionService::OnExtensionInstalled,
[email protected]8d888c12010-11-30 00:00:25341 extension));
[email protected]7a4c6852010-09-16 03:44:22342}
343
[email protected]eaa7dd182010-12-14 11:09:00344void ExtensionServiceBackend::ReportExtensionLoadError(
[email protected]7a4c6852010-09-16 03:44:22345 const FilePath& extension_path, const std::string &error) {
[email protected]a8af9fdb2010-10-28 21:52:20346 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]ca4b5fa32010-10-09 12:42:18347 BrowserThread::PostTask(
348 BrowserThread::UI, FROM_HERE,
[email protected]7a4c6852010-09-16 03:44:22349 NewRunnableMethod(
350 frontend_,
[email protected]eaa7dd182010-12-14 11:09:00351 &ExtensionService::ReportExtensionLoadError, extension_path,
[email protected]7a4c6852010-09-16 03:44:22352 error, NotificationType::EXTENSION_INSTALL_ERROR, alert_on_error_));
353}
354
[email protected]7a4c6852010-09-16 03:44:22355// Some extensions will autoupdate themselves externally from Chrome. These
356// are typically part of some larger client application package. To support
357// these, the extension will register its location in the the preferences file
358// (and also, on Windows, in the registry) and this code will periodically
359// check that location for a .crx file, which it will then install locally if
360// a new version is available.
[email protected]eaa7dd182010-12-14 11:09:00361void ExtensionServiceBackend::CheckForExternalUpdates(
362 scoped_refptr<ExtensionService> frontend) {
[email protected]a8af9fdb2010-10-28 21:52:20363 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
364
[email protected]7a4c6852010-09-16 03:44:22365 // Note that this installation is intentionally silent (since it didn't
366 // go through the front-end). Extensions that are registered in this
367 // way are effectively considered 'pre-bundled', and so implicitly
368 // trusted. In general, if something has HKLM or filesystem access,
369 // they could install an extension manually themselves anyway.
370 alert_on_error_ = false;
371 frontend_ = frontend;
372 external_extension_added_ = false;
373
374 // Ask each external extension provider to give us a call back for each
375 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
[email protected]0a60a2e2010-10-25 16:15:21376 ProviderCollection::const_iterator i;
377 for (i = external_extension_providers_.begin();
[email protected]7a4c6852010-09-16 03:44:22378 i != external_extension_providers_.end(); ++i) {
[email protected]0a60a2e2010-10-25 16:15:21379 ExternalExtensionProvider* provider = i->get();
[email protected]683d0702010-12-06 16:25:57380 provider->VisitRegisteredExtension(this);
[email protected]7a4c6852010-09-16 03:44:22381 }
382
383 if (external_extension_added_ && frontend->updater()) {
[email protected]ca4b5fa32010-10-09 12:42:18384 BrowserThread::PostTask(
385 BrowserThread::UI, FROM_HERE,
[email protected]7a4c6852010-09-16 03:44:22386 NewRunnableMethod(
387 frontend->updater(), &ExtensionUpdater::CheckNow));
388 }
389}
390
[email protected]eaa7dd182010-12-14 11:09:00391void ExtensionServiceBackend::CheckExternalUninstall(
392 scoped_refptr<ExtensionService> frontend, const std::string& id) {
[email protected]a8af9fdb2010-10-28 21:52:20393 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
394
[email protected]7a4c6852010-09-16 03:44:22395 // Check if the providers know about this extension.
[email protected]0a60a2e2010-10-25 16:15:21396 ProviderCollection::const_iterator i;
397 for (i = external_extension_providers_.begin();
398 i != external_extension_providers_.end(); ++i) {
399 if (i->get()->HasExtension(id))
400 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22401 }
402
[email protected]7a4c6852010-09-16 03:44:22403 // This is an external extension that we don't have registered. Uninstall.
[email protected]ca4b5fa32010-10-09 12:42:18404 BrowserThread::PostTask(
405 BrowserThread::UI, FROM_HERE,
[email protected]7a4c6852010-09-16 03:44:22406 NewRunnableMethod(
[email protected]eaa7dd182010-12-14 11:09:00407 frontend.get(), &ExtensionService::UninstallExtension, id, true));
[email protected]7a4c6852010-09-16 03:44:22408}
409
[email protected]eaa7dd182010-12-14 11:09:00410void ExtensionServiceBackend::UpdateExternalPolicyExtensionProvider(
[email protected]6c751e72010-11-23 10:11:10411 scoped_refptr<RefCountedList> forcelist) {
412 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
413 external_policy_extension_provider_->SetPreferences(forcelist->Get());
414}
415
[email protected]eaa7dd182010-12-14 11:09:00416void ExtensionServiceBackend::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22417 external_extension_providers_.clear();
418}
419
[email protected]eaa7dd182010-12-14 11:09:00420void ExtensionServiceBackend::AddProviderForTesting(
[email protected]7a4c6852010-09-16 03:44:22421 ExternalExtensionProvider* test_provider) {
422 DCHECK(test_provider);
[email protected]0a60a2e2010-10-25 16:15:21423 external_extension_providers_.push_back(
424 linked_ptr<ExternalExtensionProvider>(test_provider));
[email protected]7a4c6852010-09-16 03:44:22425}
426
[email protected]eaa7dd182010-12-14 11:09:00427void ExtensionServiceBackend::OnExternalExtensionFileFound(
[email protected]7a4c6852010-09-16 03:44:22428 const std::string& id, const Version* version, const FilePath& path,
429 Extension::Location location) {
[email protected]a8af9fdb2010-10-28 21:52:20430 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
431
[email protected]7a4c6852010-09-16 03:44:22432 DCHECK(version);
[email protected]ca4b5fa32010-10-09 12:42:18433 BrowserThread::PostTask(
434 BrowserThread::UI, FROM_HERE,
[email protected]7a4c6852010-09-16 03:44:22435 NewRunnableMethod(
[email protected]eaa7dd182010-12-14 11:09:00436 frontend_, &ExtensionService::OnExternalExtensionFileFound, id,
[email protected]7a4c6852010-09-16 03:44:22437 version->GetString(), path, location));
438}
439
[email protected]eaa7dd182010-12-14 11:09:00440void ExtensionServiceBackend::OnExternalExtensionUpdateUrlFound(
[email protected]7a4c6852010-09-16 03:44:22441 const std::string& id,
[email protected]21a5a672010-11-04 10:47:42442 const GURL& update_url,
443 Extension::Location location) {
[email protected]a8af9fdb2010-10-28 21:52:20444 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
445
[email protected]7a4c6852010-09-16 03:44:22446 if (frontend_->GetExtensionById(id, true)) {
447 // Already installed. Do not change the update URL that the extension set.
448 return;
449 }
450
[email protected]a8af9fdb2010-10-28 21:52:20451 BrowserThread::PostTask(
452 BrowserThread::UI, FROM_HERE,
453 NewRunnableMethod(
454 frontend_,
[email protected]eaa7dd182010-12-14 11:09:00455 &ExtensionService::AddPendingExtensionFromExternalUpdateUrl,
[email protected]21a5a672010-11-04 10:47:42456 id, update_url, location));
[email protected]7a4c6852010-09-16 03:44:22457 external_extension_added_ |= true;
458}
459
[email protected]eaa7dd182010-12-14 11:09:00460bool ExtensionService::IsDownloadFromGallery(const GURL& download_url,
[email protected]334e04a2010-06-24 23:34:44461 const GURL& referrer_url) {
[email protected]d3071992010-10-08 15:24:07462 // Special-case the themes mini-gallery.
463 // TODO(erikkay) When that gallery goes away, remove this code.
464 if (IsDownloadFromMiniGallery(download_url) &&
465 StartsWithASCII(referrer_url.spec(),
466 extension_urls::kMiniGalleryBrowsePrefix, false)) {
[email protected]334e04a2010-06-24 23:34:44467 return true;
[email protected]1debbbb62010-10-06 17:23:44468 }
[email protected]473ff6e2010-05-12 15:31:55469
[email protected]9adb9692010-10-29 23:14:02470 const Extension* download_extension = GetExtensionByWebExtent(download_url);
471 const Extension* referrer_extension = GetExtensionByWebExtent(referrer_url);
472 const Extension* webstore_app = GetWebStoreApp();
[email protected]d3071992010-10-08 15:24:07473
474 bool referrer_valid = (referrer_extension == webstore_app);
475 bool download_valid = (download_extension == webstore_app);
476
477 // If the command-line gallery URL is set, then be a bit more lenient.
478 GURL store_url =
479 GURL(CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
480 switches::kAppsGalleryURL));
481 if (!store_url.is_empty()) {
482 std::string store_tld =
483 net::RegistryControlledDomainService::GetDomainAndRegistry(store_url);
484 if (!referrer_valid) {
485 std::string referrer_tld =
486 net::RegistryControlledDomainService::GetDomainAndRegistry(
487 referrer_url);
488 // The referrer gets stripped when transitioning from https to http,
489 // or when hitting an unknown test cert and that commonly happens in
490 // testing environments. Given this, we allow an empty referrer when
491 // the command-line flag is set.
492 // Otherwise, the TLD must match the TLD of the command-line url.
493 referrer_valid = referrer_url.is_empty() || (referrer_tld == store_tld);
494 }
495
496 if (!download_valid) {
497 std::string download_tld =
498 net::RegistryControlledDomainService::GetDomainAndRegistry(
499 GURL(download_url));
500
501 // Otherwise, the TLD must match the TLD of the command-line url.
502 download_valid = (download_tld == store_tld);
503 }
504 }
505
506 return (referrer_valid && download_valid);
[email protected]b7c2f252009-12-08 00:47:23507}
508
[email protected]eaa7dd182010-12-14 11:09:00509bool ExtensionService::IsDownloadFromMiniGallery(const GURL& download_url) {
[email protected]ac025282009-12-16 19:16:38510 return StartsWithASCII(download_url.spec(),
511 extension_urls::kMiniGalleryDownloadPrefix,
512 false); // case_sensitive
513}
514
[email protected]eaa7dd182010-12-14 11:09:00515bool ExtensionService::IsInstalledApp(const GURL& url) {
[email protected]ffb204f22010-12-05 23:20:27516 // Check for hosted app.
517 if (GetExtensionByWebExtent(url) != NULL)
518 return true;
519
520 // Check for packaged app.
521 const Extension* extension = GetExtensionByURL(url);
522 return extension != NULL && extension->is_app();
523}
524
[email protected]6aeac8342010-10-01 20:21:18525// static
[email protected]eaa7dd182010-12-14 11:09:00526bool ExtensionService::UninstallExtensionHelper(
527 ExtensionService* extensions_service,
[email protected]6aeac8342010-10-01 20:21:18528 const std::string& extension_id) {
529 DCHECK(extensions_service);
530
531 // We can't call UninstallExtension with an invalid extension ID, so check it
532 // first.
533 if (extensions_service->GetExtensionById(extension_id, true)) {
534 extensions_service->UninstallExtension(extension_id, false);
535 } else {
536 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]a8af9fdb2010-10-28 21:52:20537 << "id: " << extension_id;
[email protected]6aeac8342010-10-01 20:21:18538 return false;
539 }
540
541 return true;
542}
543
[email protected]eaa7dd182010-12-14 11:09:00544ExtensionService::ExtensionService(Profile* profile,
[email protected]36a784c2009-06-23 06:21:08545 const CommandLine* command_line,
[email protected]a9b00ac2009-06-25 21:03:23546 const FilePath& install_directory,
[email protected]73c47932010-12-06 18:13:43547 ExtensionPrefs* extension_prefs,
[email protected]93fd78f42009-07-10 16:43:17548 bool autoupdate_enabled)
[email protected]6ef635e42009-07-26 06:16:12549 : profile_(profile),
[email protected]73c47932010-12-06 18:13:43550 extension_prefs_(extension_prefs),
[email protected]a9b00ac2009-06-25 21:03:23551 install_directory_(install_directory),
[email protected]6d60703b2009-08-29 01:29:23552 extensions_enabled_(true),
[email protected]e81dba32009-06-19 20:19:13553 show_extensions_prompts_(true),
[email protected]3ecda252010-11-18 19:50:55554 ready_(false),
[email protected]ec5b50d2010-10-09 16:35:18555 ALLOW_THIS_IN_INITIALIZER_LIST(toolbar_model_(this)),
[email protected]b3d62312b12010-10-14 21:10:18556 default_apps_(profile->GetPrefs()),
557 event_routers_initialized_(false) {
[email protected]a8af9fdb2010-10-28 21:52:20558 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
559
[email protected]36a784c2009-06-23 06:21:08560 // Figure out if extension installation should be enabled.
[email protected]6d60703b2009-08-29 01:29:23561 if (command_line->HasSwitch(switches::kDisableExtensions)) {
562 extensions_enabled_ = false;
563 } else if (profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions)) {
564 extensions_enabled_ = false;
[email protected]6b75ec32009-08-14 06:37:18565 }
[email protected]36a784c2009-06-23 06:21:08566
[email protected]a4ed6282009-12-14 20:51:16567 registrar_.Add(this, NotificationType::EXTENSION_PROCESS_TERMINATED,
[email protected]bc535ee52010-08-31 18:40:32568 NotificationService::AllSources());
[email protected]2fb7dc982010-09-29 12:24:28569 pref_change_registrar_.Init(profile->GetPrefs());
570 pref_change_registrar_.Add(prefs::kExtensionInstallAllowList, this);
571 pref_change_registrar_.Add(prefs::kExtensionInstallDenyList, this);
[email protected]6c751e72010-11-23 10:11:10572 pref_change_registrar_.Add(prefs::kExtensionInstallForceList, this);
[email protected]4814b512009-11-07 00:12:29573
[email protected]93fd78f42009-07-10 16:43:17574 // Set up the ExtensionUpdater
575 if (autoupdate_enabled) {
576 int update_frequency = kDefaultUpdateFrequencySeconds;
577 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25578 base::StringToInt(command_line->GetSwitchValueASCII(
579 switches::kExtensionsUpdateFrequency),
580 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17581 }
[email protected]2fb7dc982010-09-29 12:24:28582 updater_ = new ExtensionUpdater(this,
583 profile->GetPrefs(),
584 update_frequency);
[email protected]93fd78f42009-07-10 16:43:17585 }
586
[email protected]eaa7dd182010-12-14 11:09:00587 backend_ = new ExtensionServiceBackend(profile->GetPrefs(),
[email protected]aebe23a32010-12-10 22:15:48588 install_directory_);
[email protected]b671760b2010-07-15 21:13:47589
[email protected]aa96d3a2010-08-21 08:45:25590 // Use monochrome icons for Omnibox icons.
[email protected]29d0d4ac2010-09-08 21:10:31591 omnibox_popup_icon_manager_.set_monochrome(true);
[email protected]b671760b2010-07-15 21:13:47592 omnibox_icon_manager_.set_monochrome(true);
[email protected]29d0d4ac2010-09-08 21:10:31593 omnibox_icon_manager_.set_padding(gfx::Insets(0, kOmniboxIconPaddingLeft,
594 0, kOmniboxIconPaddingRight));
[email protected]6014d672008-12-05 00:38:25595}
596
[email protected]eaa7dd182010-12-14 11:09:00597const ExtensionList* ExtensionService::extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45598 return &extensions_;
599}
600
[email protected]eaa7dd182010-12-14 11:09:00601const ExtensionList* ExtensionService::disabled_extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45602 return &disabled_extensions_;
603}
604
[email protected]eaa7dd182010-12-14 11:09:00605const PendingExtensionMap& ExtensionService::pending_extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45606 return pending_extensions_;
607}
608
[email protected]eaa7dd182010-12-14 11:09:00609bool ExtensionService::HasInstalledExtensions() {
[email protected]ddb1e5a2010-12-13 20:10:45610 return !(extensions_.empty() && disabled_extensions_.empty());
611}
612
[email protected]eaa7dd182010-12-14 11:09:00613ExtensionService::~ExtensionService() {
[email protected]2fb7dc982010-09-29 12:24:28614 DCHECK(!profile_); // Profile should have told us it's going away.
[email protected]9f1087e2009-06-15 17:29:32615 UnloadAllExtensions();
[email protected]93fd78f42009-07-10 16:43:17616 if (updater_.get()) {
617 updater_->Stop();
618 }
[email protected]6014d672008-12-05 00:38:25619}
620
[email protected]eaa7dd182010-12-14 11:09:00621void ExtensionService::InitEventRouters() {
[email protected]b3d62312b12010-10-14 21:10:18622 if (event_routers_initialized_)
623 return;
624
[email protected]c5ae74ab2010-04-15 18:14:37625 ExtensionHistoryEventRouter::GetInstance()->ObserveProfile(profile_);
626 ExtensionAccessibilityEventRouter::GetInstance()->ObserveProfile(profile_);
[email protected]56ee0152010-06-16 01:54:42627 ExtensionBrowserEventRouter::GetInstance()->Init(profile_);
[email protected]8e8bb6d2010-12-13 08:18:55628 ExtensionBookmarkEventRouter::GetInstance()->Observe(
[email protected]c5ae74ab2010-04-15 18:14:37629 profile_->GetBookmarkModel());
[email protected]2c5e1e12010-06-10 13:14:44630 ExtensionCookiesEventRouter::GetInstance()->Init();
[email protected]7596ce72010-08-30 05:10:46631 ExtensionManagementEventRouter::GetInstance()->Init();
[email protected]8a661f82010-10-19 21:47:11632 ExtensionProcessesEventRouter::GetInstance()->ObserveProfile(profile_);
[email protected]784688a62010-09-13 07:06:52633 ExtensionWebNavigationEventRouter::GetInstance()->Init();
[email protected]b3d62312b12010-10-14 21:10:18634 event_routers_initialized_ = true;
[email protected]c5ae74ab2010-04-15 18:14:37635}
636
[email protected]eaa7dd182010-12-14 11:09:00637const Extension* ExtensionService::GetExtensionById(const std::string& id,
[email protected]78994ab02010-12-08 18:06:44638 bool include_disabled) {
639 return GetExtensionByIdInternal(id, true, include_disabled);
640}
641
[email protected]eaa7dd182010-12-14 11:09:00642void ExtensionService::Init() {
[email protected]a8af9fdb2010-10-28 21:52:20643 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
644
[email protected]fa6a9102010-11-22 15:38:50645 DCHECK(!ready_); // Can't redo init.
[email protected]93fd78f42009-07-10 16:43:17646 DCHECK_EQ(extensions_.size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32647
[email protected]95dd38f2009-10-20 20:09:15648 // Hack: we need to ensure the ResourceDispatcherHost is ready before we load
649 // the first extension, because its members listen for loaded notifications.
650 g_browser_process->resource_dispatcher_host();
651
[email protected]9f1087e2009-06-15 17:29:32652 LoadAllExtensions();
[email protected]894bb502009-05-21 22:39:57653
[email protected]9f1087e2009-06-15 17:29:32654 // TODO(erikkay) this should probably be deferred to a future point
655 // rather than running immediately at startup.
[email protected]93fd78f42009-07-10 16:43:17656 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57657
[email protected]9f1087e2009-06-15 17:29:32658 // TODO(erikkay) this should probably be deferred as well.
659 GarbageCollectExtensions();
[email protected]6014d672008-12-05 00:38:25660}
661
[email protected]eaa7dd182010-12-14 11:09:00662void ExtensionService::InstallExtension(const FilePath& extension_path) {
[email protected]6dfbbf82010-03-12 23:09:16663 scoped_refptr<CrxInstaller> installer(
[email protected]bb461532010-11-26 21:50:23664 new CrxInstaller(this, // frontend
[email protected]6dfbbf82010-03-12 23:09:16665 NULL)); // no client (silent install)
[email protected]6dfbbf82010-03-12 23:09:16666 installer->InstallCrx(extension_path);
[email protected]3cf4f0992009-02-03 23:00:30667}
668
[email protected]aa142702010-03-26 01:26:33669namespace {
670 // TODO(akalin): Put this somewhere where both crx_installer.cc and
671 // this file can use it.
672 void DeleteFileHelper(const FilePath& path, bool recursive) {
[email protected]ca4b5fa32010-10-09 12:42:18673 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]aa142702010-03-26 01:26:33674 file_util::Delete(path, recursive);
675 }
676} // namespace
677
[email protected]eaa7dd182010-12-14 11:09:00678void ExtensionService::UpdateExtension(const std::string& id,
[email protected]5c8516202010-03-18 21:43:34679 const FilePath& extension_path,
680 const GURL& download_url) {
[email protected]a8af9fdb2010-10-28 21:52:20681 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
682
[email protected]aa142702010-03-26 01:26:33683 PendingExtensionMap::const_iterator it = pending_extensions_.find(id);
[email protected]8ef78fd2010-08-19 17:14:32684 bool is_pending_extension = (it != pending_extensions_.end());
685
[email protected]5eb375e92010-11-26 07:50:41686 const Extension* extension = GetExtensionByIdInternal(id, true, true);
687 if (!is_pending_extension && !extension) {
[email protected]aa142702010-03-26 01:26:33688 LOG(WARNING) << "Will not update extension " << id
689 << " because it is not installed or pending";
690 // Delete extension_path since we're not creating a CrxInstaller
691 // that would do it for us.
[email protected]ca4b5fa32010-10-09 12:42:18692 BrowserThread::PostTask(
693 BrowserThread::FILE, FROM_HERE,
[email protected]aa142702010-03-26 01:26:33694 NewRunnableFunction(&DeleteFileHelper, extension_path, false));
[email protected]4c967932009-07-31 01:15:49695 return;
[email protected]e957fe52009-06-23 16:51:05696 }
697
[email protected]aa142702010-03-26 01:26:33698 // We want a silent install only for non-pending extensions and
699 // pending extensions that have install_silently set.
700 ExtensionInstallUI* client =
[email protected]8ef78fd2010-08-19 17:14:32701 (!is_pending_extension || it->second.install_silently) ?
[email protected]aa142702010-03-26 01:26:33702 NULL : new ExtensionInstallUI(profile_);
703
[email protected]6dfbbf82010-03-12 23:09:16704 scoped_refptr<CrxInstaller> installer(
[email protected]bb461532010-11-26 21:50:23705 new CrxInstaller(this, // frontend
[email protected]aa142702010-03-26 01:26:33706 client));
[email protected]6dfbbf82010-03-12 23:09:16707 installer->set_expected_id(id);
[email protected]ec5b50d2010-10-09 16:35:18708 if (is_pending_extension)
709 installer->set_install_source(it->second.install_source);
[email protected]5eb375e92010-11-26 07:50:41710 else if (extension)
711 installer->set_install_source(extension->location());
[email protected]6dfbbf82010-03-12 23:09:16712 installer->set_delete_source(true);
[email protected]5c8516202010-03-18 21:43:34713 installer->set_original_url(download_url);
[email protected]6dfbbf82010-03-12 23:09:16714 installer->InstallCrx(extension_path);
[email protected]e957fe52009-06-23 16:51:05715}
716
[email protected]eaa7dd182010-12-14 11:09:00717void ExtensionService::AddPendingExtensionFromSync(
[email protected]aa142702010-03-26 01:26:33718 const std::string& id, const GURL& update_url,
[email protected]7fa19f82010-12-21 19:40:08719 ShouldInstallExtensionPredicate should_install_extension,
[email protected]8ef78fd2010-08-19 17:14:32720 bool install_silently, bool enable_on_install,
721 bool enable_incognito_on_install) {
[email protected]aa142702010-03-26 01:26:33722 if (GetExtensionByIdInternal(id, true, true)) {
[email protected]efee9f262010-03-29 21:26:25723 LOG(DFATAL) << "Trying to add pending extension " << id
724 << " which already exists";
[email protected]aa142702010-03-26 01:26:33725 return;
726 }
[email protected]ec5b50d2010-10-09 16:35:18727
[email protected]7fa19f82010-12-21 19:40:08728 AddPendingExtensionInternal(id, update_url, should_install_extension, true,
[email protected]ec5b50d2010-10-09 16:35:18729 install_silently, enable_on_install,
730 enable_incognito_on_install,
731 Extension::INTERNAL);
[email protected]aa142702010-03-26 01:26:33732}
733
[email protected]7fa19f82010-12-21 19:40:08734namespace {
735
736bool AlwaysInstall(const Extension& extension) {
737 return true;
738}
739
740} // namespace
741
[email protected]eaa7dd182010-12-14 11:09:00742void ExtensionService::AddPendingExtensionFromExternalUpdateUrl(
[email protected]21a5a672010-11-04 10:47:42743 const std::string& id, const GURL& update_url,
744 Extension::Location location) {
[email protected]8ef78fd2010-08-19 17:14:32745 const bool kIsFromSync = false;
746 const bool kInstallSilently = true;
747 const bool kEnableOnInstall = true;
[email protected]a424d84c2010-09-24 09:31:15748 const bool kEnableIncognitoOnInstall = false;
[email protected]683d0702010-12-06 16:25:57749 if (extension_prefs_->IsExtensionKilled(id))
750 return;
[email protected]8ef78fd2010-08-19 17:14:32751
752 if (GetExtensionByIdInternal(id, true, true)) {
753 LOG(DFATAL) << "Trying to add extension " << id
754 << " by external update, but it is already installed.";
755 return;
756 }
757
[email protected]7fa19f82010-12-21 19:40:08758 AddPendingExtensionInternal(id, update_url, &AlwaysInstall,
759 kIsFromSync, kInstallSilently,
760 kEnableOnInstall, kEnableIncognitoOnInstall,
[email protected]21a5a672010-11-04 10:47:42761 location);
[email protected]ec5b50d2010-10-09 16:35:18762}
763
[email protected]7fa19f82010-12-21 19:40:08764namespace {
765
766bool IsApp(const Extension& extension) {
767 return extension.is_app();
768}
769
770} // namespace
771
772// TODO(akalin): Change DefaultAppList to DefaultExtensionList and
773// remove the IsApp() check.
774
[email protected]eaa7dd182010-12-14 11:09:00775void ExtensionService::AddPendingExtensionFromDefaultAppList(
[email protected]ec5b50d2010-10-09 16:35:18776 const std::string& id) {
[email protected]ec5b50d2010-10-09 16:35:18777 const bool kIsFromSync = false;
778 const bool kInstallSilently = true;
779 const bool kEnableOnInstall = true;
780 const bool kEnableIncognitoOnInstall = true;
781
782 // This can legitimately happen if the user manually installed one of the
783 // default apps before this code ran.
784 if (GetExtensionByIdInternal(id, true, true))
785 return;
786
[email protected]7fa19f82010-12-21 19:40:08787 AddPendingExtensionInternal(id, GURL(), &IsApp,
788 kIsFromSync, kInstallSilently,
789 kEnableOnInstall, kEnableIncognitoOnInstall,
[email protected]ec5b50d2010-10-09 16:35:18790 Extension::INTERNAL);
[email protected]8ef78fd2010-08-19 17:14:32791}
792
[email protected]eaa7dd182010-12-14 11:09:00793void ExtensionService::AddPendingExtensionInternal(
[email protected]aa142702010-03-26 01:26:33794 const std::string& id, const GURL& update_url,
[email protected]7fa19f82010-12-21 19:40:08795 ShouldInstallExtensionPredicate should_install_extension,
[email protected]8ef78fd2010-08-19 17:14:32796 bool is_from_sync, bool install_silently,
[email protected]ec5b50d2010-10-09 16:35:18797 bool enable_on_install, bool enable_incognito_on_install,
798 Extension::Location install_source) {
[email protected]a8af9fdb2010-10-28 21:52:20799 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
800
[email protected]1afaf2a52010-11-02 19:29:17801 // If a non-sync update is pending, a sync request should not
802 // overwrite it. This is important for external extensions.
803 // If an external extension download is pending, and the user has
804 // the extension in their sync profile, the install should set the
805 // type to be external. An external extension should not be
806 // rejected if it fails the safty checks for a syncable extension.
807 // TODO(skerner): Work out other potential overlapping conditions.
[email protected]a315ba92010-11-16 14:12:21808 // (crbug.com/61000)
[email protected]1afaf2a52010-11-02 19:29:17809 PendingExtensionMap::iterator it = pending_extensions_.find(id);
810 if (it != pending_extensions_.end()) {
811 VLOG(1) << "Extension id " << id
812 << " was entered for update more than once."
813 << " old is_from_sync = " << it->second.is_from_sync
814 << " new is_from_sync = " << is_from_sync;
815 if (!it->second.is_from_sync && is_from_sync)
816 return;
817 }
818
[email protected]aa142702010-03-26 01:26:33819 pending_extensions_[id] =
[email protected]7fa19f82010-12-21 19:40:08820 PendingExtensionInfo(update_url, should_install_extension,
821 is_from_sync, install_silently, enable_on_install,
[email protected]ec5b50d2010-10-09 16:35:18822 enable_incognito_on_install, install_source);
[email protected]aa142702010-03-26 01:26:33823}
824
[email protected]eaa7dd182010-12-14 11:09:00825void ExtensionService::ReloadExtension(const std::string& extension_id) {
[email protected]a8af9fdb2010-10-28 21:52:20826 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]b65272f2009-08-31 15:47:06827 FilePath path;
[email protected]9adb9692010-10-29 23:14:02828 const Extension* current_extension = GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40829
[email protected]f17dbd42010-08-16 23:21:10830 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]b65272f2009-08-31 15:47:06831 if (current_extension) {
[email protected]4814b512009-11-07 00:12:29832 // If the extension has an inspector open for its background page, detach
833 // the inspector and hang onto a cookie for it, so that we can reattach
834 // later.
835 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
836 ExtensionHost* host = manager->GetBackgroundHostForExtension(
837 current_extension);
838 if (host) {
839 // Look for an open inspector for the background page.
840 int devtools_cookie = DevToolsManager::GetInstance()->DetachClientHost(
841 host->render_view_host());
842 if (devtools_cookie >= 0)
843 orphaned_dev_tools_[extension_id] = devtools_cookie;
844 }
845
[email protected]b65272f2009-08-31 15:47:06846 path = current_extension->path();
[email protected]f17dbd42010-08-16 23:21:10847 DisableExtension(extension_id);
848 disabled_extension_paths_[extension_id] = path;
[email protected]1eb175082010-02-10 09:26:16849 } else {
850 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06851 }
852
[email protected]e6090e42010-03-23 22:44:08853 // Check the installed extensions to see if what we're reloading was already
854 // installed.
855 scoped_ptr<ExtensionInfo> installed_extension(
856 extension_prefs_->GetInstalledExtensionInfo(extension_id));
857 if (installed_extension.get() &&
858 installed_extension->extension_manifest.get()) {
859 LoadInstalledExtension(*installed_extension, false);
860 } else {
861 // We should always be able to remember the extension's path. If it's not in
862 // the map, someone failed to update |unloaded_extension_paths_|.
863 CHECK(!path.empty());
864 LoadExtension(path);
865 }
[email protected]9cddd4702009-07-27 22:09:40866}
867
[email protected]eaa7dd182010-12-14 11:09:00868void ExtensionService::UninstallExtension(const std::string& extension_id,
[email protected]27b985d2009-06-25 17:53:15869 bool external_uninstall) {
[email protected]a8af9fdb2010-10-28 21:52:20870 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
871
[email protected]9adb9692010-10-29 23:14:02872 const Extension* extension =
873 GetExtensionByIdInternal(extension_id, true, true);
[email protected]631cf822009-05-15 07:01:25874
[email protected]e7afe2452010-08-22 16:19:13875 // Callers should not send us nonexistent extensions.
[email protected]aebe23a32010-12-10 22:15:48876 CHECK(extension);
[email protected]9f1087e2009-06-15 17:29:32877
[email protected]831aa212010-03-26 13:55:19878 // Get hold of information we need after unloading, since the extension
879 // pointer will be invalid then.
880 GURL extension_url(extension->url());
881 Extension::Location location(extension->location());
[email protected]211030342010-09-30 18:41:06882 UninstalledExtensionInfo uninstalled_extension_info(*extension);
[email protected]831aa212010-03-26 13:55:19883
[email protected]9b217652010-10-08 22:04:23884 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08885 extension->GetType(), 100);
[email protected]9b217652010-10-08 22:04:23886
[email protected]831aa212010-03-26 13:55:19887 // Also copy the extension identifier since the reference might have been
888 // obtained via Extension::id().
889 std::string extension_id_copy(extension_id);
890
[email protected]56ad3792010-05-28 17:45:33891 if (profile_->GetTemplateURLModel())
892 profile_->GetTemplateURLModel()->UnregisterExtensionKeyword(extension);
893
[email protected]831aa212010-03-26 13:55:19894 // Unload before doing more cleanup to ensure that nothing is hanging on to
895 // any of these resources.
[email protected]a9f39a312010-12-23 22:14:27896 UnloadExtension(extension_id, UnloadedExtensionInfo::UNINSTALL);
[email protected]831aa212010-03-26 13:55:19897
898 extension_prefs_->OnExtensionUninstalled(extension_id_copy, location,
899 external_uninstall);
[email protected]9f1087e2009-06-15 17:29:32900
901 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]831aa212010-03-26 13:55:19902 if (Extension::LOAD != location) {
[email protected]ca4b5fa32010-10-09 12:42:18903 BrowserThread::PostTask(
904 BrowserThread::FILE, FROM_HERE,
[email protected]95d29192009-10-30 01:49:06905 NewRunnableFunction(
[email protected]ca3dbf52010-05-19 22:27:06906 &extension_file_util::UninstallExtension,
907 install_directory_,
908 extension_id_copy));
[email protected]9f1087e2009-06-15 17:29:32909 }
910
[email protected]c10da4b02010-03-25 14:38:32911 ClearExtensionData(extension_url);
[email protected]211030342010-09-30 18:41:06912
913 // Notify interested parties that we've uninstalled this extension.
914 NotificationService::current()->Notify(
915 NotificationType::EXTENSION_UNINSTALLED,
916 Source<Profile>(profile_),
917 Details<UninstalledExtensionInfo>(&uninstalled_extension_info));
[email protected]c10da4b02010-03-25 14:38:32918}
919
[email protected]eaa7dd182010-12-14 11:09:00920void ExtensionService::ClearExtensionData(const GURL& extension_url) {
[email protected]c10da4b02010-03-25 14:38:32921 scoped_refptr<ExtensionDataDeleter> deleter(
922 new ExtensionDataDeleter(profile_, extension_url));
923 deleter->StartDeleting();
[email protected]9f1087e2009-06-15 17:29:32924}
925
[email protected]eaa7dd182010-12-14 11:09:00926void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a8af9fdb2010-10-28 21:52:20927 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
928
[email protected]9adb9692010-10-29 23:14:02929 const Extension* extension =
930 GetExtensionByIdInternal(extension_id, false, true);
[email protected]f681c782010-11-19 11:19:39931 if (!extension)
[email protected]0c6da502009-08-14 22:32:39932 return;
[email protected]0c6da502009-08-14 22:32:39933
[email protected]e8c729a2010-03-09 19:55:19934 extension_prefs_->SetExtensionState(extension, Extension::ENABLED);
[email protected]1784e83a2009-09-08 21:01:52935
[email protected]0c6da502009-08-14 22:32:39936 // Move it over to the enabled list.
[email protected]00cd9c42010-11-02 20:15:57937 extensions_.push_back(make_scoped_refptr(extension));
[email protected]0c6da502009-08-14 22:32:39938 ExtensionList::iterator iter = std::find(disabled_extensions_.begin(),
939 disabled_extensions_.end(),
940 extension);
941 disabled_extensions_.erase(iter);
942
[email protected]f681c782010-11-19 11:19:39943 // Make sure any browser action contained within it is not hidden.
944 extension_prefs_->SetBrowserActionVisibility(extension, true);
945
[email protected]86c008e82009-08-28 20:26:05946 ExtensionDOMUI::RegisterChromeURLOverrides(profile_,
947 extension->GetChromeURLOverrides());
948
[email protected]62d30f42009-10-01 22:36:06949 NotifyExtensionLoaded(extension);
[email protected]aab98a52009-12-02 03:22:35950 UpdateActiveExtensionsInCrashReporter();
[email protected]0c6da502009-08-14 22:32:39951}
952
[email protected]eaa7dd182010-12-14 11:09:00953void ExtensionService::DisableExtension(const std::string& extension_id) {
[email protected]a8af9fdb2010-10-28 21:52:20954 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
955
[email protected]9adb9692010-10-29 23:14:02956 const Extension* extension =
957 GetExtensionByIdInternal(extension_id, true, false);
[email protected]b2ba9962009-12-10 20:10:15958 // The extension may have been disabled already.
959 if (!extension)
[email protected]1784e83a2009-09-08 21:01:52960 return;
[email protected]1784e83a2009-09-08 21:01:52961
[email protected]e8c729a2010-03-09 19:55:19962 extension_prefs_->SetExtensionState(extension, Extension::DISABLED);
[email protected]1784e83a2009-09-08 21:01:52963
964 // Move it over to the disabled list.
[email protected]00cd9c42010-11-02 20:15:57965 disabled_extensions_.push_back(make_scoped_refptr(extension));
[email protected]1784e83a2009-09-08 21:01:52966 ExtensionList::iterator iter = std::find(extensions_.begin(),
967 extensions_.end(),
968 extension);
969 extensions_.erase(iter);
970
971 ExtensionDOMUI::UnregisterChromeURLOverrides(profile_,
972 extension->GetChromeURLOverrides());
973
[email protected]a9f39a312010-12-23 22:14:27974 NotifyExtensionUnloaded(extension, UnloadedExtensionInfo::DISABLE);
[email protected]aab98a52009-12-02 03:22:35975 UpdateActiveExtensionsInCrashReporter();
[email protected]1784e83a2009-09-08 21:01:52976}
977
[email protected]eaa7dd182010-12-14 11:09:00978void ExtensionService::GrantPermissions(const Extension* extension) {
[email protected]8d888c12010-11-30 00:00:25979 CHECK(extension);
980
981 // We only maintain the granted permissions prefs for INTERNAL extensions.
982 CHECK(extension->location() == Extension::INTERNAL);
983
984 ExtensionExtent effective_hosts = extension->GetEffectiveHostPermissions();
985 extension_prefs_->AddGrantedPermissions(extension->id(),
986 extension->HasFullPermissions(),
987 extension->api_permissions(),
988 effective_hosts);
989}
990
[email protected]eaa7dd182010-12-14 11:09:00991void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]8d888c12010-11-30 00:00:25992 const Extension* extension) {
993 CHECK(extension);
994 GrantPermissions(extension);
995 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
996 EnableExtension(extension->id());
997}
998
[email protected]eaa7dd182010-12-14 11:09:00999void ExtensionService::LoadExtension(const FilePath& extension_path) {
[email protected]ca4b5fa32010-10-09 12:42:181000 BrowserThread::PostTask(
1001 BrowserThread::FILE, FROM_HERE,
[email protected]95d29192009-10-30 01:49:061002 NewRunnableMethod(
1003 backend_.get(),
[email protected]eaa7dd182010-12-14 11:09:001004 &ExtensionServiceBackend::LoadSingleExtension,
1005 extension_path, scoped_refptr<ExtensionService>(this)));
[email protected]9f1087e2009-06-15 17:29:321006}
1007
[email protected]eaa7dd182010-12-14 11:09:001008void ExtensionService::LoadComponentExtensions() {
[email protected]1952c7d2010-03-04 23:48:341009 for (RegisteredComponentExtensions::iterator it =
1010 component_extension_manifests_.begin();
1011 it != component_extension_manifests_.end(); ++it) {
1012 JSONStringValueSerializer serializer(it->manifest);
[email protected]ba399672010-04-06 15:42:391013 scoped_ptr<Value> manifest(serializer.Deserialize(NULL, NULL));
[email protected]999731f2010-03-22 19:13:531014 if (!manifest.get()) {
[email protected]a94d57d2010-09-13 22:53:001015 DLOG(ERROR) << "Failed to parse manifest for extension";
[email protected]999731f2010-03-22 19:13:531016 continue;
1017 }
[email protected]1952c7d2010-03-04 23:48:341018
[email protected]1952c7d2010-03-04 23:48:341019 std::string error;
[email protected]9adb9692010-10-29 23:14:021020 scoped_refptr<const Extension> extension(Extension::Create(
[email protected]66e4eb32010-10-27 20:37:411021 it->root_directory,
1022 Extension::COMPONENT,
1023 *static_cast<DictionaryValue*>(manifest.get()),
1024 true, // require key
1025 &error));
1026 if (!extension.get()) {
[email protected]4fdbc1492010-07-01 01:20:591027 NOTREACHED() << error;
[email protected]1952c7d2010-03-04 23:48:341028 return;
1029 }
1030
[email protected]8d888c12010-11-30 00:00:251031 OnExtensionLoaded(extension);
[email protected]1952c7d2010-03-04 23:48:341032 }
1033}
1034
[email protected]eaa7dd182010-12-14 11:09:001035void ExtensionService::LoadAllExtensions() {
[email protected]a315ba92010-11-16 14:12:211036 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1037
[email protected]cc2c3432009-11-06 17:24:361038 base::TimeTicks start_time = base::TimeTicks::Now();
1039
[email protected]1952c7d2010-03-04 23:48:341040 // Load any component extensions.
1041 LoadComponentExtensions();
1042
[email protected]e72e8eb82009-06-18 17:21:511043 // Load the previously installed extensions.
[email protected]a315ba92010-11-16 14:12:211044 scoped_ptr<ExtensionPrefs::ExtensionsInfo> extensions_info(
[email protected]e6090e42010-03-23 22:44:081045 extension_prefs_->GetInstalledExtensionsInfo());
[email protected]c6d474f82009-12-16 21:11:061046
[email protected]a315ba92010-11-16 14:12:211047 std::vector<int> reload_reason_counts(NUM_MANIFEST_RELOAD_REASONS, 0);
1048 bool should_write_prefs = false;
1049
1050 for (size_t i = 0; i < extensions_info->size(); ++i) {
1051 ExtensionInfo* info = extensions_info->at(i).get();
1052
1053 ManifestReloadReason reload_reason = ShouldReloadExtensionManifest(*info);
1054 ++reload_reason_counts[reload_reason];
1055 UMA_HISTOGRAM_ENUMERATION("Extensions.ManifestReloadEnumValue",
1056 reload_reason, 100);
1057
1058 if (reload_reason != NOT_NEEDED) {
1059 // Reloading and extension reads files from disk. We do this on the
1060 // UI thread because reloads should be very rare, and the complexity
1061 // added by delaying the time when the extensions service knows about
1062 // all extensions is significant. See crbug.com/37548 for details.
1063 // |allow_io| disables tests that file operations run on the file
1064 // thread.
1065 base::ThreadRestrictions::ScopedAllowIO allow_io;
1066
1067 std::string error;
1068 scoped_refptr<const Extension> extension(
1069 extension_file_util::LoadExtension(
1070 info->extension_path, info->extension_location, false, &error));
1071
1072 if (extension.get()) {
1073 extensions_info->at(i)->extension_manifest.reset(
1074 static_cast<DictionaryValue*>(
1075 extension->manifest_value()->DeepCopy()));
1076 should_write_prefs = true;
1077 }
[email protected]c6d474f82009-12-16 21:11:061078 }
1079 }
1080
[email protected]a315ba92010-11-16 14:12:211081 for (size_t i = 0; i < extensions_info->size(); ++i) {
1082 LoadInstalledExtension(*extensions_info->at(i), should_write_prefs);
[email protected]c6d474f82009-12-16 21:11:061083 }
1084
[email protected]ae09ca62009-08-21 19:46:461085 OnLoadedInstalledExtensions();
[email protected]cc2c3432009-11-06 17:24:361086
[email protected]a315ba92010-11-16 14:12:211087 // The histograms Extensions.ManifestReload* allow us to validate
1088 // the assumption that reloading manifest is a rare event.
1089 UMA_HISTOGRAM_COUNTS_100("Extensions.ManifestReloadNotNeeded",
1090 reload_reason_counts[NOT_NEEDED]);
1091 UMA_HISTOGRAM_COUNTS_100("Extensions.ManifestReloadUnpackedDir",
1092 reload_reason_counts[UNPACKED_DIR]);
1093 UMA_HISTOGRAM_COUNTS_100("Extensions.ManifestReloadNeedsRelocalization",
1094 reload_reason_counts[NEEDS_RELOCALIZATION]);
1095
[email protected]cc2c3432009-11-06 17:24:361096 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadAll", extensions_.size());
1097 UMA_HISTOGRAM_COUNTS_100("Extensions.Disabled", disabled_extensions_.size());
1098
[email protected]1952c7d2010-03-04 23:48:341099 UMA_HISTOGRAM_TIMES("Extensions.LoadAllTime",
1100 base::TimeTicks::Now() - start_time);
[email protected]cc2c3432009-11-06 17:24:361101
[email protected]9b217652010-10-08 22:04:231102 int app_count = 0;
1103 int hosted_app_count = 0;
1104 int packaged_app_count = 0;
[email protected]1952c7d2010-03-04 23:48:341105 int user_script_count = 0;
1106 int extension_count = 0;
1107 int theme_count = 0;
1108 int external_count = 0;
1109 int page_action_count = 0;
1110 int browser_action_count = 0;
1111 ExtensionList::iterator ex;
1112 for (ex = extensions_.begin(); ex != extensions_.end(); ++ex) {
[email protected]9b217652010-10-08 22:04:231113 Extension::Location location = (*ex)->location();
[email protected]7fa19f82010-12-21 19:40:081114 Extension::Type type = (*ex)->GetType();
[email protected]9b217652010-10-08 22:04:231115 if ((*ex)->is_app()) {
1116 UMA_HISTOGRAM_ENUMERATION("Extensions.AppLocation",
1117 location, 100);
1118 } else if (type == Extension::TYPE_EXTENSION) {
1119 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionLocation",
1120 location, 100);
1121 }
1122
[email protected]1952c7d2010-03-04 23:48:341123 // Don't count component extensions, since they are only extensions as an
1124 // implementation detail.
[email protected]9b217652010-10-08 22:04:231125 if (location == Extension::COMPONENT)
[email protected]1952c7d2010-03-04 23:48:341126 continue;
1127
[email protected]e8c729a2010-03-09 19:55:191128 // Don't count unpacked extensions, since they're a developer-specific
1129 // feature.
[email protected]9b217652010-10-08 22:04:231130 if (location == Extension::LOAD)
[email protected]e8c729a2010-03-09 19:55:191131 continue;
1132
[email protected]9b217652010-10-08 22:04:231133 // Using an enumeration shows us the total installed ratio across all users.
1134 // Using the totals per user at each startup tells us the distribution of
1135 // usage for each user (e.g. 40% of users have at least one app installed).
1136 UMA_HISTOGRAM_ENUMERATION("Extensions.LoadType", type, 100);
1137 switch (type) {
1138 case Extension::TYPE_THEME:
[email protected]a315ba92010-11-16 14:12:211139 ++theme_count;
[email protected]9b217652010-10-08 22:04:231140 break;
1141 case Extension::TYPE_USER_SCRIPT:
[email protected]a315ba92010-11-16 14:12:211142 ++user_script_count;
[email protected]9b217652010-10-08 22:04:231143 break;
1144 case Extension::TYPE_HOSTED_APP:
[email protected]a315ba92010-11-16 14:12:211145 ++app_count;
1146 ++hosted_app_count;
[email protected]9b217652010-10-08 22:04:231147 break;
1148 case Extension::TYPE_PACKAGED_APP:
[email protected]a315ba92010-11-16 14:12:211149 ++app_count;
1150 ++packaged_app_count;
[email protected]9b217652010-10-08 22:04:231151 break;
1152 case Extension::TYPE_EXTENSION:
1153 default:
[email protected]a315ba92010-11-16 14:12:211154 ++extension_count;
[email protected]9b217652010-10-08 22:04:231155 break;
[email protected]cc2c3432009-11-06 17:24:361156 }
[email protected]9b217652010-10-08 22:04:231157 if (Extension::IsExternalLocation(location))
[email protected]a315ba92010-11-16 14:12:211158 ++external_count;
[email protected]9b217652010-10-08 22:04:231159 if ((*ex)->page_action() != NULL)
[email protected]a315ba92010-11-16 14:12:211160 ++page_action_count;
[email protected]9b217652010-10-08 22:04:231161 if ((*ex)->browser_action() != NULL)
[email protected]a315ba92010-11-16 14:12:211162 ++browser_action_count;
[email protected]cc2c3432009-11-06 17:24:361163 }
[email protected]9b217652010-10-08 22:04:231164 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadApp", app_count);
1165 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadHostedApp", hosted_app_count);
1166 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadPackagedApp", packaged_app_count);
[email protected]1952c7d2010-03-04 23:48:341167 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadExtension", extension_count);
1168 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadUserScript", user_script_count);
1169 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadTheme", theme_count);
1170 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadExternal", external_count);
1171 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadPageAction", page_action_count);
1172 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadBrowserAction",
1173 browser_action_count);
[email protected]ae09ca62009-08-21 19:46:461174}
1175
[email protected]eaa7dd182010-12-14 11:09:001176void ExtensionService::LoadInstalledExtension(const ExtensionInfo& info,
[email protected]c6d474f82009-12-16 21:11:061177 bool write_to_prefs) {
[email protected]ae09ca62009-08-21 19:46:461178 std::string error;
[email protected]9adb9692010-10-29 23:14:021179 scoped_refptr<const Extension> extension(NULL);
[email protected]306a2bd2010-08-11 14:56:361180 if (!extension_prefs_->IsExtensionAllowedByPolicy(info.extension_id)) {
1181 error = errors::kDisabledByPolicy;
1182 } else if (info.extension_manifest.get()) {
[email protected]e8c729a2010-03-09 19:55:191183 bool require_key = info.extension_location != Extension::LOAD;
[email protected]66e4eb32010-10-27 20:37:411184 extension = Extension::Create(
1185 info.extension_path, info.extension_location, *info.extension_manifest,
1186 require_key, &error);
[email protected]ae09ca62009-08-21 19:46:461187 } else {
[email protected]c6d474f82009-12-16 21:11:061188 error = errors::kManifestUnreadable;
[email protected]ae09ca62009-08-21 19:46:461189 }
1190
1191 if (!extension) {
[email protected]c6d474f82009-12-16 21:11:061192 ReportExtensionLoadError(info.extension_path,
[email protected]d11c8e92009-10-20 23:26:401193 error,
1194 NotificationType::EXTENSION_INSTALL_ERROR,
1195 false);
[email protected]ae09ca62009-08-21 19:46:461196 return;
1197 }
1198
[email protected]c6d474f82009-12-16 21:11:061199 if (write_to_prefs)
1200 extension_prefs_->UpdateManifest(extension);
1201
[email protected]8d888c12010-11-30 00:00:251202 OnExtensionLoaded(extension);
[email protected]ae09ca62009-08-21 19:46:461203
[email protected]55196e92010-09-29 15:04:461204 if (Extension::IsExternalLocation(info.extension_location)) {
[email protected]ca4b5fa32010-10-09 12:42:181205 BrowserThread::PostTask(
1206 BrowserThread::FILE, FROM_HERE,
[email protected]95d29192009-10-30 01:49:061207 NewRunnableMethod(
[email protected]f17dbd42010-08-16 23:21:101208 backend_.get(),
[email protected]eaa7dd182010-12-14 11:09:001209 &ExtensionServiceBackend::CheckExternalUninstall,
1210 scoped_refptr<ExtensionService>(this),
[email protected]0a60a2e2010-10-25 16:15:211211 info.extension_id));
[email protected]ae09ca62009-08-21 19:46:461212 }
[email protected]9f1087e2009-06-15 17:29:321213}
1214
[email protected]eaa7dd182010-12-14 11:09:001215void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]57a777f72010-03-31 01:09:421216 // The ChromeURLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:061217 // was loaded, otherwise a race can arise where a renderer that is created
1218 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:421219 // that the request context doesn't yet know about. The profile is responsible
1220 // for ensuring its URLRequestContexts appropriately discover the loaded
1221 // extension.
1222 if (profile_) {
1223 profile_->RegisterExtensionWithRequestContexts(extension);
[email protected]24b538a2010-02-27 01:22:441224
1225 // Check if this permission requires unlimited storage quota
[email protected]c2c263c2010-08-13 21:59:481226 if (extension->HasApiPermission(Extension::kUnlimitedStoragePermission))
1227 GrantUnlimitedStorage(extension);
[email protected]654512b2010-09-01 02:09:421228
1229 // If the extension is an app, protect its local storage from
1230 // "Clear browsing data."
1231 if (extension->is_app())
1232 GrantProtectedStorage(extension);
[email protected]62d30f42009-10-01 22:36:061233 }
1234
1235 NotificationService::current()->Notify(
1236 NotificationType::EXTENSION_LOADED,
[email protected]24e7a9d2009-11-04 11:11:341237 Source<Profile>(profile_),
[email protected]9adb9692010-10-29 23:14:021238 Details<const Extension>(extension));
[email protected]62d30f42009-10-01 22:36:061239}
1240
[email protected]a9f39a312010-12-23 22:14:271241void ExtensionService::NotifyExtensionUnloaded(
1242 const Extension* extension, UnloadedExtensionInfo::Reason reason) {
1243 UnloadedExtensionInfo details(extension, reason);
[email protected]62d30f42009-10-01 22:36:061244 NotificationService::current()->Notify(
1245 NotificationType::EXTENSION_UNLOADED,
[email protected]24e7a9d2009-11-04 11:11:341246 Source<Profile>(profile_),
[email protected]a9f39a312010-12-23 22:14:271247 Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061248
[email protected]57a777f72010-03-31 01:09:421249 if (profile_) {
1250 profile_->UnregisterExtensionWithRequestContexts(extension);
1251
1252 // Check if this permission required unlimited storage quota, reset its
1253 // in-memory quota.
[email protected]c2c263c2010-08-13 21:59:481254 if (extension->HasApiPermission(Extension::kUnlimitedStoragePermission))
1255 RevokeUnlimitedStorage(extension);
[email protected]654512b2010-09-01 02:09:421256
1257 // If this is an app, then stop protecting its storage so it can be deleted.
1258 if (extension->is_app())
1259 RevokeProtectedStorage(extension);
1260 }
1261}
1262
[email protected]eaa7dd182010-12-14 11:09:001263void ExtensionService::GrantProtectedStorage(const Extension* extension) {
[email protected]654512b2010-09-01 02:09:421264 DCHECK(extension->is_app()) << "Only Apps are allowed protected storage.";
1265 std::vector<GURL> origins;
1266 GetExplicitOriginsInExtent(extension, &origins);
1267 for (size_t i = 0; i < origins.size(); ++i)
1268 ++protected_storage_map_[origins[i]];
1269}
1270
[email protected]eaa7dd182010-12-14 11:09:001271void ExtensionService::RevokeProtectedStorage(const Extension* extension) {
[email protected]654512b2010-09-01 02:09:421272 DCHECK(extension->is_app()) << "Attempting to revoke protected storage from "
1273 << " a non-app extension.";
1274 std::vector<GURL> origins;
1275 GetExplicitOriginsInExtent(extension, &origins);
1276 for (size_t i = 0; i < origins.size(); ++i) {
1277 const GURL& origin = origins[i];
1278 DCHECK(protected_storage_map_[origin] > 0);
1279 if (--protected_storage_map_[origin] <= 0)
1280 protected_storage_map_.erase(origin);
[email protected]c2c263c2010-08-13 21:59:481281 }
1282}
1283
[email protected]eaa7dd182010-12-14 11:09:001284void ExtensionService::GrantUnlimitedStorage(const Extension* extension) {
[email protected]c2c263c2010-08-13 21:59:481285 DCHECK(extension->HasApiPermission(Extension::kUnlimitedStoragePermission));
1286 std::vector<GURL> origins;
1287 GetExplicitOriginsInExtent(extension, &origins);
1288 origins.push_back(extension->url());
1289
1290 for (size_t i = 0; i < origins.size(); ++i) {
1291 const GURL& origin = origins[i];
1292 if (++unlimited_storage_map_[origin] == 1) {
[email protected]57a777f72010-03-31 01:09:421293 string16 origin_identifier =
[email protected]c2c263c2010-08-13 21:59:481294 webkit_database::DatabaseUtil::GetOriginIdentifier(origin);
[email protected]ca4b5fa32010-10-09 12:42:181295 BrowserThread::PostTask(
1296 BrowserThread::FILE, FROM_HERE,
[email protected]c2c263c2010-08-13 21:59:481297 NewRunnableMethod(
1298 profile_->GetDatabaseTracker(),
1299 &webkit_database::DatabaseTracker::SetOriginQuotaInMemory,
1300 origin_identifier,
1301 kint64max));
[email protected]ca4b5fa32010-10-09 12:42:181302 BrowserThread::PostTask(
1303 BrowserThread::IO, FROM_HERE,
[email protected]c2c263c2010-08-13 21:59:481304 NewRunnableMethod(
1305 profile_->GetAppCacheService(),
1306 &ChromeAppCacheService::SetOriginQuotaInMemory,
1307 origin,
1308 kint64max));
[email protected]ca4b5fa32010-10-09 12:42:181309 BrowserThread::PostTask(
1310 BrowserThread::IO, FROM_HERE,
[email protected]70c6c042010-10-08 09:52:071311 NewRunnableMethod(
[email protected]6c20697e2010-11-09 06:59:161312 profile_->GetFileSystemContext(),
[email protected]b7b82eb2010-12-14 01:12:121313 &fileapi::SandboxedFileSystemContext::SetOriginQuotaUnlimited,
[email protected]70c6c042010-10-08 09:52:071314 origin));
[email protected]c2c263c2010-08-13 21:59:481315 }
1316 }
1317}
1318
[email protected]eaa7dd182010-12-14 11:09:001319void ExtensionService::RevokeUnlimitedStorage(const Extension* extension) {
[email protected]c2c263c2010-08-13 21:59:481320 DCHECK(extension->HasApiPermission(Extension::kUnlimitedStoragePermission));
1321 std::vector<GURL> origins;
1322 GetExplicitOriginsInExtent(extension, &origins);
1323 origins.push_back(extension->url());
1324
1325 for (size_t i = 0; i < origins.size(); ++i) {
1326 const GURL& origin = origins[i];
1327 DCHECK(unlimited_storage_map_[origin] > 0);
1328 if (--unlimited_storage_map_[origin] == 0) {
1329 unlimited_storage_map_.erase(origin);
1330 string16 origin_identifier =
1331 webkit_database::DatabaseUtil::GetOriginIdentifier(origin);
[email protected]ca4b5fa32010-10-09 12:42:181332 BrowserThread::PostTask(
1333 BrowserThread::FILE, FROM_HERE,
[email protected]be180c802009-10-23 06:33:311334 NewRunnableMethod(
[email protected]57a777f72010-03-31 01:09:421335 profile_->GetDatabaseTracker(),
1336 &webkit_database::DatabaseTracker::ResetOriginQuotaInMemory,
1337 origin_identifier));
[email protected]ca4b5fa32010-10-09 12:42:181338 BrowserThread::PostTask(
1339 BrowserThread::IO, FROM_HERE,
[email protected]c2c263c2010-08-13 21:59:481340 NewRunnableMethod(
1341 profile_->GetAppCacheService(),
1342 &ChromeAppCacheService::ResetOriginQuotaInMemory,
1343 origin));
[email protected]ca4b5fa32010-10-09 12:42:181344 BrowserThread::PostTask(
1345 BrowserThread::IO, FROM_HERE,
[email protected]70c6c042010-10-08 09:52:071346 NewRunnableMethod(
[email protected]6c20697e2010-11-09 06:59:161347 profile_->GetFileSystemContext(),
[email protected]b7b82eb2010-12-14 01:12:121348 &fileapi::SandboxedFileSystemContext::ResetOriginQuotaUnlimited,
[email protected]70c6c042010-10-08 09:52:071349 origin));
[email protected]62d30f42009-10-01 22:36:061350 }
1351 }
1352}
1353
[email protected]eaa7dd182010-12-14 11:09:001354void ExtensionService::UpdateExtensionBlacklist(
[email protected]6b75ec32009-08-14 06:37:181355 const std::vector<std::string>& blacklist) {
1356 // Use this set to indicate if an extension in the blacklist has been used.
1357 std::set<std::string> blacklist_set;
1358 for (unsigned int i = 0; i < blacklist.size(); ++i) {
1359 if (Extension::IdIsValid(blacklist[i])) {
1360 blacklist_set.insert(blacklist[i]);
1361 }
1362 }
1363 extension_prefs_->UpdateBlacklist(blacklist_set);
1364 std::vector<std::string> to_be_removed;
1365 // Loop current extensions, unload installed extensions.
1366 for (ExtensionList::const_iterator iter = extensions_.begin();
1367 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021368 const Extension* extension = (*iter);
[email protected]6b75ec32009-08-14 06:37:181369 if (blacklist_set.find(extension->id()) != blacklist_set.end()) {
1370 to_be_removed.push_back(extension->id());
1371 }
1372 }
1373
1374 // UnloadExtension will change the extensions_ list. So, we should
1375 // call it outside the iterator loop.
1376 for (unsigned int i = 0; i < to_be_removed.size(); ++i) {
[email protected]a9f39a312010-12-23 22:14:271377 UnloadExtension(to_be_removed[i], UnloadedExtensionInfo::DISABLE);
[email protected]6b75ec32009-08-14 06:37:181378 }
1379}
1380
[email protected]eaa7dd182010-12-14 11:09:001381void ExtensionService::DestroyingProfile() {
[email protected]2fb7dc982010-09-29 12:24:281382 pref_change_registrar_.RemoveAll();
[email protected]aa96d3a2010-08-21 08:45:251383 profile_ = NULL;
[email protected]2fb7dc982010-09-29 12:24:281384 toolbar_model_.DestroyingProfile();
[email protected]aa96d3a2010-08-21 08:45:251385}
1386
[email protected]eaa7dd182010-12-14 11:09:001387ExtensionPrefs* ExtensionService::extension_prefs() {
[email protected]ddb1e5a2010-12-13 20:10:451388 return extension_prefs_;
1389}
1390
[email protected]eaa7dd182010-12-14 11:09:001391void ExtensionService::CheckAdminBlacklist() {
[email protected]aa96d3a2010-08-21 08:45:251392 std::vector<std::string> to_be_removed;
1393 // Loop through extensions list, unload installed extensions.
1394 for (ExtensionList::const_iterator iter = extensions_.begin();
1395 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021396 const Extension* extension = (*iter);
[email protected]aa96d3a2010-08-21 08:45:251397 if (!extension_prefs_->IsExtensionAllowedByPolicy(extension->id()))
1398 to_be_removed.push_back(extension->id());
1399 }
1400
1401 // UnloadExtension will change the extensions_ list. So, we should
1402 // call it outside the iterator loop.
1403 for (unsigned int i = 0; i < to_be_removed.size(); ++i)
[email protected]a9f39a312010-12-23 22:14:271404 UnloadExtension(to_be_removed[i], UnloadedExtensionInfo::DISABLE);
[email protected]aa96d3a2010-08-21 08:45:251405}
1406
[email protected]eaa7dd182010-12-14 11:09:001407bool ExtensionService::IsIncognitoEnabled(const Extension* extension) {
[email protected]cb0ce1e022010-03-10 19:54:411408 // If this is a component extension we always allow it to work in incognito
1409 // mode.
1410 if (extension->location() == Extension::COMPONENT)
1411 return true;
1412
1413 // Check the prefs.
1414 return extension_prefs_->IsIncognitoEnabled(extension->id());
[email protected]db7331a2010-02-25 22:10:501415}
[email protected]55a35692010-02-11 23:25:211416
[email protected]eaa7dd182010-12-14 11:09:001417void ExtensionService::SetIsIncognitoEnabled(const Extension* extension,
[email protected]db7331a2010-02-25 22:10:501418 bool enabled) {
[email protected]cb0ce1e022010-03-10 19:54:411419 extension_prefs_->SetIsIncognitoEnabled(extension->id(), enabled);
[email protected]c1499f3d2010-03-05 00:33:241420
[email protected]568f33d2010-08-04 17:06:411421 // Broadcast unloaded and loaded events to update browser state. Only bother
1422 // if the extension is actually enabled, since there is no UI otherwise.
1423 bool is_enabled = std::find(extensions_.begin(), extensions_.end(),
1424 extension) != extensions_.end();
1425 if (is_enabled) {
[email protected]a9f39a312010-12-23 22:14:271426 NotifyExtensionUnloaded(extension, UnloadedExtensionInfo::DISABLE);
[email protected]568f33d2010-08-04 17:06:411427 NotifyExtensionLoaded(extension);
1428 }
[email protected]55a35692010-02-11 23:25:211429}
1430
[email protected]eaa7dd182010-12-14 11:09:001431bool ExtensionService::CanCrossIncognito(const Extension* extension) {
[email protected]2a8f24e2010-11-03 21:37:051432 // We allow the extension to see events and data from another profile iff it
1433 // uses "spanning" behavior and it has incognito access. "split" mode
1434 // extensions only see events for a matching profile.
1435 return IsIncognitoEnabled(extension) && !extension->incognito_split_mode();
1436}
1437
[email protected]eaa7dd182010-12-14 11:09:001438bool ExtensionService::AllowFileAccess(const Extension* extension) {
[email protected]05c82182010-06-24 17:49:081439 return (CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]334e04a2010-06-24 23:34:441440 switches::kDisableExtensionsFileAccessCheck) ||
[email protected]05c82182010-06-24 17:49:081441 extension_prefs_->AllowFileAccess(extension->id()));
1442}
1443
[email protected]eaa7dd182010-12-14 11:09:001444void ExtensionService::SetAllowFileAccess(const Extension* extension,
[email protected]9adb9692010-10-29 23:14:021445 bool allow) {
[email protected]05c82182010-06-24 17:49:081446 extension_prefs_->SetAllowFileAccess(extension->id(), allow);
1447 NotificationService::current()->Notify(
1448 NotificationType::EXTENSION_USER_SCRIPTS_UPDATED,
1449 Source<Profile>(profile_),
[email protected]9adb9692010-10-29 23:14:021450 Details<const Extension>(extension));
[email protected]05c82182010-06-24 17:49:081451}
1452
[email protected]eaa7dd182010-12-14 11:09:001453bool ExtensionService::GetBrowserActionVisibility(const Extension* extension) {
[email protected]f681c782010-11-19 11:19:391454 return extension_prefs_->GetBrowserActionVisibility(extension);
1455}
1456
[email protected]eaa7dd182010-12-14 11:09:001457void ExtensionService::SetBrowserActionVisibility(const Extension* extension,
[email protected]f681c782010-11-19 11:19:391458 bool visible) {
1459 extension_prefs_->SetBrowserActionVisibility(extension, visible);
1460}
1461
[email protected]eaa7dd182010-12-14 11:09:001462void ExtensionService::CheckForExternalUpdates() {
[email protected]ca4b5fa32010-10-09 12:42:181463 BrowserThread::PostTask(
1464 BrowserThread::FILE, FROM_HERE,
[email protected]95d29192009-10-30 01:49:061465 NewRunnableMethod(
[email protected]eaa7dd182010-12-14 11:09:001466 backend_.get(), &ExtensionServiceBackend::CheckForExternalUpdates,
1467 scoped_refptr<ExtensionService>(this)));
[email protected]9f1087e2009-06-15 17:29:321468}
1469
[email protected]eaa7dd182010-12-14 11:09:001470void ExtensionService::UpdateExternalPolicyExtensionProvider() {
[email protected]6c751e72010-11-23 10:11:101471 const ListValue* list_pref =
1472 profile_->GetPrefs()->GetList(prefs::kExtensionInstallForceList);
[email protected]f2ea0e7e2010-12-02 09:27:031473 ListValue* list_copy = NULL;
1474 if (list_pref)
1475 list_copy = static_cast<ListValue*>(list_pref->DeepCopy());
[email protected]6c751e72010-11-23 10:11:101476 BrowserThread::PostTask(
1477 BrowserThread::FILE, FROM_HERE,
1478 NewRunnableMethod(
1479 backend_.get(),
[email protected]eaa7dd182010-12-14 11:09:001480 &ExtensionServiceBackend::UpdateExternalPolicyExtensionProvider,
[email protected]f2ea0e7e2010-12-02 09:27:031481 scoped_refptr<RefCountedList>(
1482 new RefCountedList(list_copy))));
[email protected]6c751e72010-11-23 10:11:101483}
1484
[email protected]a9f39a312010-12-23 22:14:271485void ExtensionService::UnloadExtension(
1486 const std::string& extension_id,
1487 UnloadedExtensionInfo::Reason reason) {
[email protected]27e469a2010-01-11 20:35:091488 // Make sure the extension gets deleted after we return from this function.
[email protected]9adb9692010-10-29 23:14:021489 scoped_refptr<const Extension> extension(
[email protected]0c6da502009-08-14 22:32:391490 GetExtensionByIdInternal(extension_id, true, true));
[email protected]631cf822009-05-15 07:01:251491
[email protected]fa0c96732010-11-17 00:14:231492 // This method can be called via PostTask, so the extension may have been
1493 // unloaded by the time this runs.
1494 if (!extension)
1495 return;
[email protected]0c6da502009-08-14 22:32:391496
[email protected]1eb175082010-02-10 09:26:161497 // Keep information about the extension so that we can reload it later
1498 // even if it's not permanently installed.
1499 unloaded_extension_paths_[extension->id()] = extension->path();
1500
[email protected]f17dbd42010-08-16 23:21:101501 // Clean up if the extension is meant to be enabled after a reload.
1502 disabled_extension_paths_.erase(extension->id());
1503
[email protected]d7e9a862010-11-03 21:57:491504 // Clean up runtime data.
1505 extension_runtime_data_.erase(extension_id);
1506
[email protected]86c008e82009-08-28 20:26:051507 ExtensionDOMUI::UnregisterChromeURLOverrides(profile_,
1508 extension->GetChromeURLOverrides());
1509
[email protected]0c6da502009-08-14 22:32:391510 ExtensionList::iterator iter = std::find(disabled_extensions_.begin(),
1511 disabled_extensions_.end(),
1512 extension.get());
1513 if (iter != disabled_extensions_.end()) {
[email protected]a9f39a312010-12-23 22:14:271514 UnloadedExtensionInfo details(extension, reason);
1515 details.already_disabled = true;
[email protected]0c6da502009-08-14 22:32:391516 disabled_extensions_.erase(iter);
[email protected]866930682009-08-18 22:53:471517 NotificationService::current()->Notify(
[email protected]a9f39a312010-12-23 22:14:271518 NotificationType::EXTENSION_UNLOADED,
[email protected]24e7a9d2009-11-04 11:11:341519 Source<Profile>(profile_),
[email protected]a9f39a312010-12-23 22:14:271520 Details<UnloadedExtensionInfo>(&details));
[email protected]0c6da502009-08-14 22:32:391521 return;
1522 }
1523
1524 iter = std::find(extensions_.begin(), extensions_.end(), extension.get());
[email protected]894bb502009-05-21 22:39:571525
[email protected]631cf822009-05-15 07:01:251526 // Remove the extension from our list.
1527 extensions_.erase(iter);
1528
[email protected]a9f39a312010-12-23 22:14:271529 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]aab98a52009-12-02 03:22:351530 UpdateActiveExtensionsInCrashReporter();
[email protected]631cf822009-05-15 07:01:251531}
1532
[email protected]eaa7dd182010-12-14 11:09:001533void ExtensionService::UnloadAllExtensions() {
[email protected]9f1087e2009-06-15 17:29:321534 extensions_.clear();
[email protected]cd500f72010-06-25 23:44:321535 disabled_extensions_.clear();
[email protected]d7e9a862010-11-03 21:57:491536 extension_runtime_data_.clear();
[email protected]cd500f72010-06-25 23:44:321537
[email protected]c6e4a3412009-06-24 15:45:291538 // TODO(erikkay) should there be a notification for this? We can't use
1539 // EXTENSION_UNLOADED since that implies that the extension has been disabled
1540 // or uninstalled, and UnloadAll is just part of shutdown.
[email protected]9f1087e2009-06-15 17:29:321541}
1542
[email protected]eaa7dd182010-12-14 11:09:001543void ExtensionService::ReloadExtensions() {
[email protected]9f1087e2009-06-15 17:29:321544 UnloadAllExtensions();
1545 LoadAllExtensions();
1546}
1547
[email protected]eaa7dd182010-12-14 11:09:001548void ExtensionService::GarbageCollectExtensions() {
[email protected]39d9f62c2010-12-03 10:48:501549 if (extension_prefs_->pref_service()->ReadOnly())
[email protected]ba399672010-04-06 15:42:391550 return;
1551
[email protected]ca3dbf52010-05-19 22:27:061552 scoped_ptr<ExtensionPrefs::ExtensionsInfo> info(
1553 extension_prefs_->GetInstalledExtensionsInfo());
1554
1555 std::map<std::string, FilePath> extension_paths;
1556 for (size_t i = 0; i < info->size(); ++i)
1557 extension_paths[info->at(i)->extension_id] = info->at(i)->extension_path;
1558
[email protected]ca4b5fa32010-10-09 12:42:181559 BrowserThread::PostTask(
1560 BrowserThread::FILE, FROM_HERE,
[email protected]95d29192009-10-30 01:49:061561 NewRunnableFunction(
1562 &extension_file_util::GarbageCollectExtensions, install_directory_,
[email protected]ca3dbf52010-05-19 22:27:061563 extension_paths));
[email protected]f6ccd652010-11-17 00:48:341564
1565 // Also garbage-collect themes. We check |profile_| to be
1566 // defensive; in the future, we may call GarbageCollectExtensions()
1567 // from somewhere other than Init() (e.g., in a timer).
1568 if (profile_) {
1569 profile_->GetThemeProvider()->RemoveUnusedThemes();
1570 }
[email protected]3cf4f0992009-02-03 23:00:301571}
1572
[email protected]eaa7dd182010-12-14 11:09:001573void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]93fd78f42009-07-10 16:43:171574 if (updater_.get()) {
1575 updater_->Start();
1576 }
[email protected]fa6a9102010-11-22 15:38:501577
1578 ready_ = true;
[email protected]e72e8eb82009-06-18 17:21:511579 NotificationService::current()->Notify(
1580 NotificationType::EXTENSIONS_READY,
[email protected]24e7a9d2009-11-04 11:11:341581 Source<Profile>(profile_),
[email protected]e72e8eb82009-06-18 17:21:511582 NotificationService::NoDetails());
1583}
1584
[email protected]eaa7dd182010-12-14 11:09:001585void ExtensionService::OnExtensionLoaded(const Extension* extension) {
[email protected]ae09ca62009-08-21 19:46:461586 // Ensure extension is deleted unless we transfer ownership.
[email protected]9adb9692010-10-29 23:14:021587 scoped_refptr<const Extension> scoped_extension(extension);
[email protected]9f1087e2009-06-15 17:29:321588
[email protected]1eb175082010-02-10 09:26:161589 // The extension is now loaded, remove its data from unloaded extension map.
1590 unloaded_extension_paths_.erase(extension->id());
1591
[email protected]f17dbd42010-08-16 23:21:101592 // If the extension was disabled for a reload, then enable it.
1593 if (disabled_extension_paths_.erase(extension->id()) > 0)
1594 EnableExtension(extension->id());
1595
[email protected]d728e002010-12-08 04:46:231596 // TODO(jstritar): We may be able to get rid of this branch by overriding the
1597 // default extension state to DISABLED when the --disable-extensions flag
1598 // is set (http://crbug.com/29067).
1599 if (!extensions_enabled() &&
1600 !extension->is_theme() &&
[email protected]aebe23a32010-12-10 22:15:481601 extension->location() != Extension::COMPONENT &&
1602 !Extension::IsExternalLocation(extension->location()))
[email protected]d728e002010-12-08 04:46:231603 return;
1604
1605 // Check if the extension's privileges have changed and disable the
1606 // extension if necessary.
[email protected]8d888c12010-11-30 00:00:251607 DisableIfPrivilegeIncrease(extension);
[email protected]0c6da502009-08-14 22:32:391608
[email protected]8d888c12010-11-30 00:00:251609 switch (extension_prefs_->GetExtensionState(extension->id())) {
1610 case Extension::ENABLED:
1611 extensions_.push_back(scoped_extension);
[email protected]1e8c93f2010-02-08 22:58:311612
[email protected]8d888c12010-11-30 00:00:251613 NotifyExtensionLoaded(extension);
[email protected]0c6da502009-08-14 22:32:391614
[email protected]8d888c12010-11-30 00:00:251615 ExtensionDOMUI::RegisterChromeURLOverrides(
1616 profile_, extension->GetChromeURLOverrides());
1617 break;
1618 case Extension::DISABLED:
1619 disabled_extensions_.push_back(scoped_extension);
1620 NotificationService::current()->Notify(
1621 NotificationType::EXTENSION_UPDATE_DISABLED,
1622 Source<Profile>(profile_),
1623 Details<const Extension>(extension));
1624 break;
1625 default:
1626 NOTREACHED();
1627 break;
[email protected]e72e8eb82009-06-18 17:21:511628 }
[email protected]aab98a52009-12-02 03:22:351629
[email protected]d7e9a862010-11-03 21:57:491630 SetBeingUpgraded(extension, false);
[email protected]1e8c93f2010-02-08 22:58:311631
[email protected]aab98a52009-12-02 03:22:351632 UpdateActiveExtensionsInCrashReporter();
[email protected]0b004da2010-07-02 17:54:311633
1634 if (profile_->GetTemplateURLModel())
1635 profile_->GetTemplateURLModel()->RegisterExtensionKeyword(extension);
[email protected]b671760b2010-07-15 21:13:471636
1637 // Load the icon for omnibox-enabled extensions so it will be ready to display
1638 // in the URL bar.
[email protected]29d0d4ac2010-09-08 21:10:311639 if (!extension->omnibox_keyword().empty()) {
1640 omnibox_popup_icon_manager_.LoadIcon(extension);
[email protected]b671760b2010-07-15 21:13:471641 omnibox_icon_manager_.LoadIcon(extension);
[email protected]29d0d4ac2010-09-08 21:10:311642 }
[email protected]aab98a52009-12-02 03:22:351643}
1644
[email protected]eaa7dd182010-12-14 11:09:001645void ExtensionService::DisableIfPrivilegeIncrease(const Extension* extension) {
[email protected]8d888c12010-11-30 00:00:251646 // We keep track of all permissions the user has granted each extension.
1647 // This allows extensions to gracefully support backwards compatibility
1648 // by including unknown permissions in their manifests. When the user
1649 // installs the extension, only the recognized permissions are recorded.
1650 // When the unknown permissions become recognized (e.g., through browser
1651 // upgrade), we can prompt the user to accept these new permissions.
1652 // Extensions can also silently upgrade to less permissions, and then
1653 // silently upgrade to a version that adds these permissions back.
1654 //
1655 // For example, pretend that Chrome 10 includes a permission "omnibox"
1656 // for an API that adds suggestions to the omnibox. An extension can
1657 // maintain backwards compatibility while still having "omnibox" in the
1658 // manifest. If a user installs the extension on Chrome 9, the browser
1659 // will record the permissions it recognized, not including "omnibox."
1660 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
1661 // will disable the extension and prompt the user to approve the increase
1662 // in privileges. The extension could then release a new version that
1663 // removes the "omnibox" permission. When the user upgrades, Chrome will
1664 // still remember that "omnibox" had been granted, so that if the
1665 // extension once again includes "omnibox" in an upgrade, the extension
1666 // can upgrade without requiring this user's approval.
1667 const Extension* old = GetExtensionByIdInternal(extension->id(),
1668 true, true);
1669 bool granted_full_access;
1670 std::set<std::string> granted_apis;
1671 ExtensionExtent granted_extent;
1672
1673 bool is_extension_upgrade = old != NULL;
1674 bool is_privilege_increase = false;
1675
1676 // We only record the granted permissions for INTERNAL extensions, since
1677 // they can't silently increase privileges.
1678 if (extension->location() == Extension::INTERNAL) {
1679 // Add all the recognized permissions if the granted permissions list
1680 // hasn't been initialized yet.
1681 if (!extension_prefs_->GetGrantedPermissions(extension->id(),
1682 &granted_full_access,
1683 &granted_apis,
1684 &granted_extent)) {
1685 GrantPermissions(extension);
1686 CHECK(extension_prefs_->GetGrantedPermissions(extension->id(),
1687 &granted_full_access,
1688 &granted_apis,
1689 &granted_extent));
1690 }
1691
1692 // Here, we check if an extension's privileges have increased in a manner
1693 // that requires the user's approval. This could occur because the browser
1694 // upgraded and recognized additional privileges, or an extension upgrades
1695 // to a version that requires additional privileges.
1696 is_privilege_increase = Extension::IsPrivilegeIncrease(
1697 granted_full_access, granted_apis, granted_extent, extension);
1698 }
1699
1700 if (is_extension_upgrade) {
[email protected]66e26872010-12-03 20:07:251701 // Other than for unpacked extensions, CrxInstaller should have guaranteed
1702 // that we aren't downgrading.
1703 if (extension->location() != Extension::LOAD)
1704 CHECK(extension->version()->CompareTo(*(old->version())) >= 0);
[email protected]8d888c12010-11-30 00:00:251705
1706 // Extensions get upgraded if the privileges are allowed to increase or
1707 // the privileges haven't increased.
1708 if (!is_privilege_increase) {
1709 SetBeingUpgraded(old, true);
1710 SetBeingUpgraded(extension, true);
1711 }
1712
1713 // To upgrade an extension in place, unload the old one and
1714 // then load the new one.
[email protected]a9f39a312010-12-23 22:14:271715 UnloadExtension(old->id(), UnloadedExtensionInfo::UPDATE);
[email protected]8d888c12010-11-30 00:00:251716 old = NULL;
1717 }
1718
1719 // Extension has changed permissions significantly. Disable it. A
1720 // notification should be sent by the caller.
1721 if (is_privilege_increase) {
1722 extension_prefs_->SetExtensionState(extension, Extension::DISABLED);
1723 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
1724 }
1725}
1726
[email protected]eaa7dd182010-12-14 11:09:001727void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:391728 std::set<std::string> extension_ids;
[email protected]aab98a52009-12-02 03:22:351729 for (size_t i = 0; i < extensions_.size(); ++i) {
[email protected]2a500c1b2010-11-05 15:27:341730 if (!extensions_[i]->is_theme() &&
1731 extensions_[i]->location() != Extension::COMPONENT)
[email protected]c8865962009-12-16 07:47:391732 extension_ids.insert(extensions_[i]->id());
[email protected]aab98a52009-12-02 03:22:351733 }
1734
1735 child_process_logging::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:251736}
1737
[email protected]eaa7dd182010-12-14 11:09:001738void ExtensionService::OnExtensionInstalled(const Extension* extension) {
[email protected]a8af9fdb2010-10-28 21:52:201739 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1740
[email protected]4416c5a2010-06-26 01:28:571741 // Ensure extension is deleted unless we transfer ownership.
[email protected]9adb9692010-10-29 23:14:021742 scoped_refptr<const Extension> scoped_extension(extension);
[email protected]4416c5a2010-06-26 01:28:571743 Extension::State initial_state = Extension::DISABLED;
1744 bool initial_enable_incognito = false;
[email protected]aa142702010-03-26 01:26:331745 PendingExtensionMap::iterator it =
1746 pending_extensions_.find(extension->id());
[email protected]4416c5a2010-06-26 01:28:571747 if (it != pending_extensions_.end()) {
[email protected]11edd1e2010-07-21 00:14:501748 PendingExtensionInfo pending_extension_info = it->second;
[email protected]11edd1e2010-07-21 00:14:501749 pending_extensions_.erase(it);
1750 it = pending_extensions_.end();
[email protected]8ef78fd2010-08-19 17:14:321751
[email protected]7fa19f82010-12-21 19:40:081752 if (!pending_extension_info.should_install_extension(*extension)) {
[email protected]4416c5a2010-06-26 01:28:571753 LOG(WARNING)
[email protected]7fa19f82010-12-21 19:40:081754 << "should_install_extension ("
1755 << pending_extension_info.should_install_extension
1756 << ") returned false for " << extension->id()
1757 << " of type " << extension->GetType()
1758 << " and update URL " << extension->update_url().spec()
1759 << "; not installing";
[email protected]4416c5a2010-06-26 01:28:571760 // Delete the extension directory since we're not going to
1761 // load it.
[email protected]ca4b5fa32010-10-09 12:42:181762 BrowserThread::PostTask(
1763 BrowserThread::FILE, FROM_HERE,
[email protected]4416c5a2010-06-26 01:28:571764 NewRunnableFunction(&DeleteFileHelper, extension->path(), true));
1765 return;
1766 }
[email protected]8ef78fd2010-08-19 17:14:321767
[email protected]8ef78fd2010-08-19 17:14:321768 if (extension->is_theme()) {
[email protected]11edd1e2010-07-21 00:14:501769 DCHECK(pending_extension_info.enable_on_install);
[email protected]4416c5a2010-06-26 01:28:571770 initial_state = Extension::ENABLED;
[email protected]11edd1e2010-07-21 00:14:501771 DCHECK(!pending_extension_info.enable_incognito_on_install);
[email protected]4416c5a2010-06-26 01:28:571772 initial_enable_incognito = false;
1773 } else {
1774 initial_state =
[email protected]11edd1e2010-07-21 00:14:501775 pending_extension_info.enable_on_install ?
[email protected]4416c5a2010-06-26 01:28:571776 Extension::ENABLED : Extension::DISABLED;
1777 initial_enable_incognito =
[email protected]11edd1e2010-07-21 00:14:501778 pending_extension_info.enable_incognito_on_install;
[email protected]4416c5a2010-06-26 01:28:571779 }
[email protected]4416c5a2010-06-26 01:28:571780 } else {
[email protected]dbec3792010-08-10 00:08:451781 // Make sure we preserve enabled/disabled states.
[email protected]4416c5a2010-06-26 01:28:571782 Extension::State existing_state =
1783 extension_prefs_->GetExtensionState(extension->id());
1784 initial_state =
1785 (existing_state == Extension::DISABLED) ?
1786 Extension::DISABLED : Extension::ENABLED;
[email protected]dbec3792010-08-10 00:08:451787 initial_enable_incognito =
1788 extension_prefs_->IsIncognitoEnabled(extension->id());
[email protected]aa142702010-03-26 01:26:331789 }
1790
[email protected]9b217652010-10-08 22:04:231791 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
[email protected]7fa19f82010-12-21 19:40:081792 extension->GetType(), 100);
[email protected]eda3c362010-11-12 08:08:231793 ShownSectionsHandler::OnExtensionInstalled(profile_->GetPrefs(), extension);
[email protected]4416c5a2010-06-26 01:28:571794 extension_prefs_->OnExtensionInstalled(
1795 extension, initial_state, initial_enable_incognito);
[email protected]25b34332009-06-05 21:53:191796
[email protected]92a5b1d2010-07-20 00:42:001797 // Unpacked extensions start off with file access since they are a developer
1798 // feature.
1799 if (extension->location() == Extension::LOAD)
1800 extension_prefs_->SetAllowFileAccess(extension->id(), true);
1801
[email protected]4a190632009-05-09 01:07:421802 // If the extension is a theme, tell the profile (and therefore ThemeProvider)
1803 // to apply it.
[email protected]3ba0fd32010-06-19 05:39:101804 if (extension->is_theme()) {
[email protected]9ceb07342009-07-26 04:09:231805 NotificationService::current()->Notify(
1806 NotificationType::THEME_INSTALLED,
[email protected]24e7a9d2009-11-04 11:11:341807 Source<Profile>(profile_),
[email protected]9adb9692010-10-29 23:14:021808 Details<const Extension>(extension));
[email protected]9197f3b2009-06-02 00:49:271809 } else {
1810 NotificationService::current()->Notify(
1811 NotificationType::EXTENSION_INSTALLED,
[email protected]24e7a9d2009-11-04 11:11:341812 Source<Profile>(profile_),
[email protected]9adb9692010-10-29 23:14:021813 Details<const Extension>(extension));
[email protected]4a190632009-05-09 01:07:421814 }
[email protected]7577a5c52009-07-30 06:21:581815
[email protected]ec5b50d2010-10-09 16:35:181816 if (extension->is_app()) {
1817 ExtensionIdSet installed_ids = GetAppIds();
1818 installed_ids.insert(extension->id());
1819 default_apps_.DidInstallApp(installed_ids);
1820 }
1821
[email protected]4416c5a2010-06-26 01:28:571822 // Transfer ownership of |extension| to OnExtensionLoaded.
[email protected]8d888c12010-11-30 00:00:251823 OnExtensionLoaded(scoped_extension);
[email protected]4a190632009-05-09 01:07:421824}
1825
[email protected]eaa7dd182010-12-14 11:09:001826const Extension* ExtensionService::GetExtensionByIdInternal(
[email protected]9adb9692010-10-29 23:14:021827 const std::string& id, bool include_enabled, bool include_disabled) {
[email protected]e957fe52009-06-23 16:51:051828 std::string lowercase_id = StringToLowerASCII(id);
[email protected]0c6da502009-08-14 22:32:391829 if (include_enabled) {
1830 for (ExtensionList::const_iterator iter = extensions_.begin();
1831 iter != extensions_.end(); ++iter) {
1832 if ((*iter)->id() == lowercase_id)
1833 return *iter;
1834 }
1835 }
1836 if (include_disabled) {
1837 for (ExtensionList::const_iterator iter = disabled_extensions_.begin();
1838 iter != disabled_extensions_.end(); ++iter) {
1839 if ((*iter)->id() == lowercase_id)
1840 return *iter;
1841 }
[email protected]ce5c4502009-05-06 16:46:111842 }
1843 return NULL;
1844}
1845
[email protected]eaa7dd182010-12-14 11:09:001846const Extension* ExtensionService::GetWebStoreApp() {
[email protected]d3071992010-10-08 15:24:071847 return GetExtensionById(extension_misc::kWebStoreAppId, false);
1848}
1849
[email protected]eaa7dd182010-12-14 11:09:001850const Extension* ExtensionService::GetExtensionByURL(const GURL& url) {
[email protected]a888b29e62010-04-01 13:38:571851 return url.scheme() != chrome::kExtensionScheme ? NULL :
1852 GetExtensionById(url.host(), false);
1853}
1854
[email protected]eaa7dd182010-12-14 11:09:001855const Extension* ExtensionService::GetExtensionByWebExtent(const GURL& url) {
[email protected]a888b29e62010-04-01 13:38:571856 for (size_t i = 0; i < extensions_.size(); ++i) {
1857 if (extensions_[i]->web_extent().ContainsURL(url))
1858 return extensions_[i];
1859 }
1860 return NULL;
[email protected]9f1087e2009-06-15 17:29:321861}
1862
[email protected]eaa7dd182010-12-14 11:09:001863bool ExtensionService::ExtensionBindingsAllowed(const GURL& url) {
[email protected]583d45c12010-08-31 02:48:121864 // Allow bindings for all packaged extension.
1865 if (GetExtensionByURL(url))
1866 return true;
1867
1868 // Allow bindings for all component, hosted apps.
[email protected]9adb9692010-10-29 23:14:021869 const Extension* extension = GetExtensionByWebExtent(url);
[email protected]583d45c12010-08-31 02:48:121870 return (extension && extension->location() == Extension::COMPONENT);
1871}
1872
[email protected]eaa7dd182010-12-14 11:09:001873const Extension* ExtensionService::GetExtensionByOverlappingWebExtent(
[email protected]9f72aa02010-06-25 10:01:051874 const ExtensionExtent& extent) {
[email protected]22c966c2010-06-26 06:35:021875 for (size_t i = 0; i < extensions_.size(); ++i) {
1876 if (extensions_[i]->web_extent().OverlapsWith(extent))
1877 return extensions_[i];
1878 }
1879
[email protected]6d2e60bd2010-06-03 22:37:391880 return NULL;
1881}
1882
[email protected]eaa7dd182010-12-14 11:09:001883const SkBitmap& ExtensionService::GetOmniboxIcon(
[email protected]b671760b2010-07-15 21:13:471884 const std::string& extension_id) {
1885 return omnibox_icon_manager_.GetIcon(extension_id);
1886}
1887
[email protected]eaa7dd182010-12-14 11:09:001888const SkBitmap& ExtensionService::GetOmniboxPopupIcon(
[email protected]29d0d4ac2010-09-08 21:10:311889 const std::string& extension_id) {
1890 return omnibox_popup_icon_manager_.GetIcon(extension_id);
1891}
1892
[email protected]eaa7dd182010-12-14 11:09:001893void ExtensionService::ClearProvidersForTesting() {
[email protected]ca4b5fa32010-10-09 12:42:181894 BrowserThread::PostTask(
1895 BrowserThread::FILE, FROM_HERE,
[email protected]95d29192009-10-30 01:49:061896 NewRunnableMethod(
[email protected]eaa7dd182010-12-14 11:09:001897 backend_.get(), &ExtensionServiceBackend::ClearProvidersForTesting));
[email protected]a1257b12009-06-12 02:51:341898}
1899
[email protected]eaa7dd182010-12-14 11:09:001900void ExtensionService::AddProviderForTesting(
[email protected]0a60a2e2010-10-25 16:15:211901 ExternalExtensionProvider* test_provider) {
[email protected]ca4b5fa32010-10-09 12:42:181902 BrowserThread::PostTask(
1903 BrowserThread::FILE, FROM_HERE,
[email protected]95d29192009-10-30 01:49:061904 NewRunnableMethod(
[email protected]eaa7dd182010-12-14 11:09:001905 backend_.get(), &ExtensionServiceBackend::AddProviderForTesting,
[email protected]0a60a2e2010-10-25 16:15:211906 test_provider));
[email protected]a1257b12009-06-12 02:51:341907}
1908
[email protected]eaa7dd182010-12-14 11:09:001909void ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:321910 const std::string& id,
1911 const std::string& version,
1912 const FilePath& path,
1913 Extension::Location location) {
[email protected]a8af9fdb2010-10-28 21:52:201914 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]683d0702010-12-06 16:25:571915 if (extension_prefs_->IsExtensionKilled(id))
1916 return;
[email protected]a8af9fdb2010-10-28 21:52:201917
[email protected]7577a5c52009-07-30 06:21:581918 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:491919 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:581920 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:021921 const Extension* existing = GetExtensionById(id, true);
[email protected]a3a63ff82009-08-04 06:44:111922 scoped_ptr<Version> other(Version::GetVersionFromString(version));
[email protected]7577a5c52009-07-30 06:21:581923 if (existing) {
[email protected]a3a63ff82009-08-04 06:44:111924 switch (existing->version()->CompareTo(*other)) {
[email protected]7577a5c52009-07-30 06:21:581925 case -1: // existing version is older, we should upgrade
1926 break;
1927 case 0: // existing version is same, do nothing
1928 return;
1929 case 1: // existing version is newer, uh-oh
1930 LOG(WARNING) << "Found external version of extension " << id
1931 << "that is older than current version. Current version "
1932 << "is: " << existing->VersionString() << ". New version "
1933 << "is: " << version << ". Keeping current version.";
1934 return;
1935 }
1936 }
1937
[email protected]9c635f22010-12-02 09:36:361938 GURL update_url = GURL();
[email protected]9c635f22010-12-02 09:36:361939 bool is_from_sync = false;
1940 bool install_silently = true;
1941 bool enable_on_install = true;
1942 bool enable_incognito_on_install = false;
1943 pending_extensions_[id] = PendingExtensionInfo(
1944 update_url,
[email protected]7fa19f82010-12-21 19:40:081945 &AlwaysInstall,
[email protected]9c635f22010-12-02 09:36:361946 is_from_sync,
1947 install_silently,
1948 enable_on_install,
1949 enable_incognito_on_install,
1950 location);
1951
[email protected]6dfbbf82010-03-12 23:09:161952 scoped_refptr<CrxInstaller> installer(
[email protected]bb461532010-11-26 21:50:231953 new CrxInstaller(this, // frontend
[email protected]6dfbbf82010-03-12 23:09:161954 NULL)); // no client (silent install)
1955 installer->set_install_source(location);
1956 installer->set_expected_id(id);
[email protected]6dfbbf82010-03-12 23:09:161957 installer->InstallCrx(path);
[email protected]7577a5c52009-07-30 06:21:581958}
1959
[email protected]eaa7dd182010-12-14 11:09:001960void ExtensionService::ReportExtensionLoadError(
[email protected]d11c8e92009-10-20 23:26:401961 const FilePath& extension_path,
1962 const std::string &error,
1963 NotificationType type,
1964 bool be_noisy) {
1965 NotificationService* service = NotificationService::current();
1966 service->Notify(type,
[email protected]24e7a9d2009-11-04 11:11:341967 Source<Profile>(profile_),
[email protected]d11c8e92009-10-20 23:26:401968 Details<const std::string>(&error));
1969
1970 // TODO(port): note that this isn't guaranteed to work properly on Linux.
[email protected]99efb7b12009-12-18 02:39:161971 std::string path_str = WideToUTF8(extension_path.ToWStringHack());
[email protected]18d4b6c2010-09-21 03:21:041972 std::string message = base::StringPrintf(
1973 "Could not load extension from '%s'. %s",
1974 path_str.c_str(), error.c_str());
[email protected]d11c8e92009-10-20 23:26:401975 ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy);
1976}
1977
[email protected]eaa7dd182010-12-14 11:09:001978void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]406027c02010-09-27 08:03:181979 ExtensionHost* host) {
1980 OrphanedDevTools::iterator iter =
1981 orphaned_dev_tools_.find(host->extension()->id());
1982 if (iter == orphaned_dev_tools_.end())
1983 return;
1984
1985 DevToolsManager::GetInstance()->AttachClientHost(
1986 iter->second, host->render_view_host());
1987 orphaned_dev_tools_.erase(iter);
1988}
1989
[email protected]eaa7dd182010-12-14 11:09:001990void ExtensionService::Observe(NotificationType type,
[email protected]4814b512009-11-07 00:12:291991 const NotificationSource& source,
1992 const NotificationDetails& details) {
1993 switch (type.value) {
[email protected]a4ed6282009-12-14 20:51:161994 case NotificationType::EXTENSION_PROCESS_TERMINATED: {
[email protected]bc535ee52010-08-31 18:40:321995 if (profile_ != Source<Profile>(source).ptr()->GetOriginalProfile())
1996 break;
[email protected]a4ed6282009-12-14 20:51:161997
[email protected]f128af42010-08-05 18:05:261998 ExtensionHost* host = Details<ExtensionHost>(details).ptr();
1999
[email protected]31f77262009-12-02 20:48:532000 // Unload the entire extension. We want it to be in a consistent state:
2001 // either fully working or not loaded at all, but never half-crashed.
[email protected]bc535ee52010-08-31 18:40:322002 // We do it in a PostTask so that other handlers of this notification will
2003 // still have access to the Extension and ExtensionHost.
2004 MessageLoop::current()->PostTask(FROM_HERE,
[email protected]a9f39a312010-12-23 22:14:272005 NewRunnableMethod(this,
2006 &ExtensionService::UnloadExtension,
2007 host->extension()->id(),
2008 UnloadedExtensionInfo::DISABLE));
[email protected]31f77262009-12-02 20:48:532009 break;
2010 }
2011
[email protected]aa96d3a2010-08-21 08:45:252012 case NotificationType::PREF_CHANGED: {
2013 std::string* pref_name = Details<std::string>(details).ptr();
[email protected]6c751e72010-11-23 10:11:102014 if (*pref_name == prefs::kExtensionInstallAllowList ||
2015 *pref_name == prefs::kExtensionInstallDenyList) {
2016 CheckAdminBlacklist();
2017 } else if (*pref_name == prefs::kExtensionInstallForceList) {
2018 UpdateExternalPolicyExtensionProvider();
2019 CheckForExternalUpdates();
2020 // TODO(gfeher): Also check for external extensions that can be
2021 // uninstalled because they were removed from the pref.
2022 // (crbug.com/63667)
2023 } else {
2024 NOTREACHED() << "Unexpected preference name.";
2025 }
[email protected]aa96d3a2010-08-21 08:45:252026 break;
2027 }
2028
[email protected]4814b512009-11-07 00:12:292029 default:
2030 NOTREACHED() << "Unexpected notification type.";
2031 }
2032}
2033
[email protected]eaa7dd182010-12-14 11:09:002034bool ExtensionService::HasApps() const {
[email protected]ec5b50d2010-10-09 16:35:182035 return !GetAppIds().empty();
2036}
[email protected]377011d2010-07-20 04:18:502037
[email protected]eaa7dd182010-12-14 11:09:002038ExtensionIdSet ExtensionService::GetAppIds() const {
[email protected]ec5b50d2010-10-09 16:35:182039 ExtensionIdSet result;
[email protected]377011d2010-07-20 04:18:502040 for (ExtensionList::const_iterator it = extensions_.begin();
2041 it != extensions_.end(); ++it) {
[email protected]ec5b50d2010-10-09 16:35:182042 if ((*it)->is_app() && (*it)->location() != Extension::COMPONENT)
2043 result.insert((*it)->id());
[email protected]377011d2010-07-20 04:18:502044 }
2045
[email protected]ec5b50d2010-10-09 16:35:182046 return result;
[email protected]377011d2010-07-20 04:18:502047}
[email protected]d7e9a862010-11-03 21:57:492048
[email protected]eaa7dd182010-12-14 11:09:002049bool ExtensionService::IsBackgroundPageReady(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:492050 return (extension->background_url().is_empty() ||
2051 extension_runtime_data_[extension->id()].background_page_ready);
2052}
2053
[email protected]eaa7dd182010-12-14 11:09:002054void ExtensionService::SetBackgroundPageReady(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:492055 DCHECK(!extension->background_url().is_empty());
2056 extension_runtime_data_[extension->id()].background_page_ready = true;
2057 NotificationService::current()->Notify(
2058 NotificationType::EXTENSION_BACKGROUND_PAGE_READY,
2059 Source<const Extension>(extension),
2060 NotificationService::NoDetails());
2061}
2062
[email protected]eaa7dd182010-12-14 11:09:002063bool ExtensionService::IsBeingUpgraded(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:492064 return extension_runtime_data_[extension->id()].being_upgraded;
2065}
2066
[email protected]eaa7dd182010-12-14 11:09:002067void ExtensionService::SetBeingUpgraded(const Extension* extension,
[email protected]d7e9a862010-11-03 21:57:492068 bool value) {
2069 extension_runtime_data_[extension->id()].being_upgraded = value;
2070}
[email protected]1bead0712010-11-27 17:41:532071
[email protected]eaa7dd182010-12-14 11:09:002072PropertyBag* ExtensionService::GetPropertyBag(const Extension* extension) {
[email protected]1bead0712010-11-27 17:41:532073 return &extension_runtime_data_[extension->id()].property_bag;
2074}