blob: e23bba9898c7d32d4adfa82d72bfb5b33e8825c1 [file] [log] [blame]
[email protected]8e4560b62011-01-14 10:09:141// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]6014d672008-12-05 00:38:252// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]eaa7dd182010-12-14 11:09:005#include "chrome/browser/extensions/extension_service.h"
[email protected]6014d672008-12-05 00:38:256
[email protected]654512b2010-09-01 02:09:427#include <algorithm>
[email protected]97d2f1d2011-01-15 00:41:088#include <set>
[email protected]654512b2010-09-01 02:09:429
[email protected]53612e82011-10-18 18:00:3610#include "base/bind.h"
[email protected]24b538a2010-02-27 01:22:4411#include "base/basictypes.h"
[email protected]e5af875f2011-10-10 21:09:1412#include "base/callback.h"
[email protected]62433d32011-10-12 22:33:1213#include "base/command_line.h"
[email protected]6014d672008-12-05 00:38:2514#include "base/file_util.h"
[email protected]005bab02011-10-07 18:36:0015#include "base/json/json_value_serializer.h"
[email protected]14908b72011-04-20 06:54:3616#include "base/logging.h"
[email protected]49098f702011-10-13 03:47:1817#include "base/metrics/field_trial.h"
[email protected]835d7c82010-10-14 04:38:3818#include "base/metrics/histogram.h"
[email protected]cebc3dc2011-04-18 17:15:0019#include "base/path_service.h"
[email protected]7286e3fc2011-07-19 22:13:2420#include "base/stl_util.h"
[email protected]24b538a2010-02-27 01:22:4421#include "base/string16.h"
[email protected]e83326f2010-07-31 17:29:2522#include "base/string_number_conversions.h"
[email protected]6014d672008-12-05 00:38:2523#include "base/string_util.h"
[email protected]18d4b6c2010-09-21 03:21:0424#include "base/stringprintf.h"
[email protected]34b99632011-01-01 01:01:0625#include "base/threading/thread_restrictions.h"
[email protected]cc2c3432009-11-06 17:24:3626#include "base/time.h"
[email protected]ce7f62e32010-08-10 23:43:5927#include "base/utf_string_conversions.h"
[email protected]cc655912009-01-29 23:19:1928#include "base/values.h"
[email protected]aa142702010-03-26 01:26:3329#include "base/version.h"
[email protected]15730c42009-09-03 00:03:2030#include "chrome/browser/browser_process.h"
[email protected]dfba8762011-09-02 12:49:5431#include "chrome/browser/chrome_plugin_service_filter.h"
[email protected]af9db5f2011-10-05 05:13:1532#include "chrome/browser/extensions/app_notification_manager.h"
[email protected]18cc5a52011-04-15 16:03:3833#include "chrome/browser/extensions/apps_promo.h"
[email protected]e0785902011-05-19 23:34:1734#include "chrome/browser/extensions/crx_installer.h"
[email protected]49098f702011-10-13 03:47:1835#include "chrome/browser/extensions/default_apps_trial.h"
[email protected]5cbe1e22010-01-30 01:18:5636#include "chrome/browser/extensions/extension_accessibility_api.h"
[email protected]840b0db2009-11-20 03:00:3837#include "chrome/browser/extensions/extension_bookmarks_module.h"
[email protected]b68d5ed2009-04-16 02:41:2838#include "chrome/browser/extensions/extension_browser_event_router.h"
[email protected]2c5e1e12010-06-10 13:14:4439#include "chrome/browser/extensions/extension_cookies_api.h"
[email protected]c10da4b02010-03-25 14:38:3240#include "chrome/browser/extensions/extension_data_deleter.h"
[email protected]370e9502011-09-14 19:31:4441#include "chrome/browser/extensions/extension_downloads_api.h"
[email protected]14a000d2010-04-29 21:44:2442#include "chrome/browser/extensions/extension_error_reporter.h"
[email protected]e5af875f2011-10-10 21:09:1443#include "chrome/browser/extensions/extension_global_error.h"
[email protected]de768a832009-10-30 05:25:0144#include "chrome/browser/extensions/extension_history_api.h"
[email protected]b1748b1d82009-11-30 20:32:5645#include "chrome/browser/extensions/extension_host.h"
[email protected]14da67b02011-08-02 05:49:1346#include "chrome/browser/extensions/extension_input_ime_api.h"
[email protected]5fb889382011-06-03 00:29:2047#include "chrome/browser/extensions/extension_install_ui.h"
[email protected]7596ce72010-08-30 05:10:4648#include "chrome/browser/extensions/extension_management_api.h"
[email protected]118de7892011-03-16 13:31:4049#include "chrome/browser/extensions/extension_preference_api.h"
[email protected]4814b512009-11-07 00:12:2950#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]8a661f82010-10-19 21:47:1151#include "chrome/browser/extensions/extension_processes_api.h"
[email protected]19eb80152011-02-26 00:28:4352#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]90310d92011-04-17 07:35:0453#include "chrome/browser/extensions/extension_sync_data.h"
[email protected]93fd78f42009-07-10 16:43:1754#include "chrome/browser/extensions/extension_updater.h"
[email protected]8f9d4eb2011-02-05 01:39:1055#include "chrome/browser/extensions/extension_web_ui.h"
[email protected]784688a62010-09-13 07:06:5256#include "chrome/browser/extensions/extension_webnavigation_api.h"
[email protected]8e4560b62011-01-14 10:09:1457#include "chrome/browser/extensions/external_extension_provider_impl.h"
[email protected]42b795652011-02-22 23:32:3858#include "chrome/browser/extensions/external_extension_provider_interface.h"
[email protected]b2907fd2011-03-25 16:43:3759#include "chrome/browser/extensions/pending_extension_manager.h"
[email protected]56ad3792010-05-28 17:45:3360#include "chrome/browser/net/chrome_url_request_context.h"
[email protected]37858e52010-08-26 00:22:0261#include "chrome/browser/prefs/pref_service.h"
[email protected]8ecad5e2010-12-02 21:18:3362#include "chrome/browser/profiles/profile.h"
[email protected]8e5c89a2011-06-07 18:13:3363#include "chrome/browser/search_engines/template_url_service.h"
64#include "chrome/browser/search_engines/template_url_service_factory.h"
[email protected]3bdba0d2011-08-23 07:17:3065#include "chrome/browser/sync/api/sync_change.h"
[email protected]18280372011-03-22 18:05:2266#include "chrome/browser/themes/theme_service.h"
[email protected]a0ea76c2011-03-23 17:36:4267#include "chrome/browser/themes/theme_service_factory.h"
[email protected]e5af875f2011-10-10 21:09:1468#include "chrome/browser/ui/browser.h"
[email protected]62433d32011-10-12 22:33:1269#include "chrome/browser/ui/browser_list.h"
[email protected]e5af875f2011-10-10 21:09:1470#include "chrome/browser/ui/global_error_service.h"
71#include "chrome/browser/ui/global_error_service_factory.h"
[email protected]c8d407e2011-04-28 21:27:1772#include "chrome/browser/ui/webui/chrome_url_data_manager.h"
73#include "chrome/browser/ui/webui/favicon_source.h"
[email protected]7c5c6f3a2011-04-28 19:56:4974#include "chrome/browser/ui/webui/ntp/shown_sections_handler.h"
[email protected]aab98a52009-12-02 03:22:3575#include "chrome/common/child_process_logging.h"
[email protected]432115822011-07-10 15:52:2776#include "chrome/common/chrome_notification_types.h"
[email protected]cebc3dc2011-04-18 17:15:0077#include "chrome/common/chrome_paths.h"
[email protected]e2eb43112009-05-29 21:19:5478#include "chrome/common/chrome_switches.h"
[email protected]5b1a0e22009-05-26 19:00:5879#include "chrome/common/extensions/extension.h"
[email protected]d7b36dc2009-10-29 21:47:4080#include "chrome/common/extensions/extension_constants.h"
[email protected]05c82182010-06-24 17:49:0881#include "chrome/common/extensions/extension_error_utils.h"
[email protected]7c927b62010-02-24 09:54:1382#include "chrome/common/extensions/extension_file_util.h"
[email protected]c6d474f82009-12-16 21:11:0683#include "chrome/common/extensions/extension_l10n_util.h"
[email protected]77a6970c2011-04-23 16:58:5684#include "chrome/common/extensions/extension_messages.h"
[email protected]a315ba92010-11-16 14:12:2185#include "chrome/common/extensions/extension_resource.h"
[email protected]25b34332009-06-05 21:53:1986#include "chrome/common/pref_names.h"
[email protected]a57209872009-05-04 22:53:1487#include "chrome/common/url_constants.h"
[email protected]5f945a0e2011-03-01 17:47:5388#include "content/browser/browser_thread.h"
[email protected]b46442d7e2011-06-29 02:16:0689#include "content/browser/debugger/devtools_manager.h"
[email protected]2de307592011-04-05 21:16:5890#include "content/browser/plugin_process_host.h"
91#include "content/browser/plugin_service.h"
[email protected]77a6970c2011-04-23 16:58:5692#include "content/browser/renderer_host/render_process_host.h"
[email protected]333b1de2011-09-12 18:28:5093#include "content/browser/user_metrics.h"
[email protected]ad50def52011-10-19 23:17:0794#include "content/public/browser/notification_service.h"
[email protected]cebc3dc2011-04-18 17:15:0095#include "content/common/pepper_plugin_registry.h"
[email protected]0d6e9bd2011-10-18 04:29:1696#include "content/public/browser/notification_types.h"
[email protected]c10da4b02010-03-25 14:38:3297#include "googleurl/src/gurl.h"
[email protected]1debbbb62010-10-06 17:23:4498#include "net/base/registry_controlled_domain.h"
[email protected]24b538a2010-02-27 01:22:4499#include "webkit/database/database_tracker.h"
100#include "webkit/database/database_util.h"
[email protected]2de307592011-04-05 21:16:58101#include "webkit/plugins/npapi/plugin_list.h"
[email protected]c64631652009-04-29 22:24:31102
[email protected]eed367e2011-04-12 03:43:31103#if defined(OS_CHROMEOS)
[email protected]14da67b02011-08-02 05:49:13104#include "chrome/browser/chromeos/cros/cros_library.h"
[email protected]eed367e2011-04-12 03:43:31105#include "chrome/browser/chromeos/extensions/file_browser_event_router.h"
[email protected]be5bd5742011-07-14 07:21:01106#include "chrome/browser/chromeos/extensions/input_method_event_router.h"
[email protected]ce9802042011-05-27 10:11:40107#include "chrome/browser/chromeos/extensions/media_player_event_router.h"
[email protected]14da67b02011-08-02 05:49:13108#include "chrome/browser/chromeos/input_method/input_method_manager.h"
109#include "chrome/browser/extensions/extension_input_ime_api.h"
[email protected]b777b332011-04-16 04:01:08110#include "webkit/fileapi/file_system_context.h"
111#include "webkit/fileapi/file_system_mount_point_provider.h"
112#include "webkit/fileapi/file_system_path_manager.h"
[email protected]eed367e2011-04-12 03:43:31113#endif
114
[email protected]27072cad2011-05-09 19:46:40115#if defined(OS_CHROMEOS) && defined(TOUCH_UI)
116#include "chrome/browser/extensions/extension_input_ui_api.h"
117#endif
118
[email protected]5ef47ec2010-01-28 05:58:05119using base::Time;
120
[email protected]c6d474f82009-12-16 21:11:06121namespace errors = extension_manifest_errors;
122
[email protected]b6ab96d2009-08-20 18:58:19123namespace {
124
[email protected]29d0d4ac2010-09-08 21:10:31125#if defined(OS_LINUX)
126static const int kOmniboxIconPaddingLeft = 2;
127static const int kOmniboxIconPaddingRight = 2;
128#elif defined(OS_MACOSX)
129static const int kOmniboxIconPaddingLeft = 0;
130static const int kOmniboxIconPaddingRight = 2;
131#else
132static const int kOmniboxIconPaddingLeft = 0;
133static const int kOmniboxIconPaddingRight = 0;
134#endif
135
[email protected]300c0ea2011-07-15 23:04:33136const char* kNaClPluginMimeType = "application/x-nacl";
137
[email protected]a315ba92010-11-16 14:12:21138// The following enumeration is used in histograms matching
139// Extensions.ManifestReload* . Values may be added, as long
140// as existing values are not changed.
141enum ManifestReloadReason {
142 NOT_NEEDED = 0, // Reload not needed.
143 UNPACKED_DIR, // Unpacked directory
144 NEEDS_RELOCALIZATION, // The local has changed since we read this extension.
145 NUM_MANIFEST_RELOAD_REASONS
146};
[email protected]2111b1a2010-03-12 18:12:44147
[email protected]a315ba92010-11-16 14:12:21148ManifestReloadReason ShouldReloadExtensionManifest(const ExtensionInfo& info) {
149 // Always reload manifests of unpacked extensions, because they can change
150 // on disk independent of the manifest in our prefs.
151 if (info.extension_location == Extension::LOAD)
152 return UNPACKED_DIR;
153
154 // Reload the manifest if it needs to be relocalized.
155 if (extension_l10n_util::ShouldRelocalizeManifest(info))
156 return NEEDS_RELOCALIZATION;
157
158 return NOT_NEEDED;
[email protected]2111b1a2010-03-12 18:12:44159}
160
[email protected]2de307592011-04-05 21:16:58161static void ForceShutdownPlugin(const FilePath& plugin_path) {
162 PluginProcessHost* plugin =
163 PluginService::GetInstance()->FindNpapiPluginProcess(plugin_path);
164 if (plugin)
165 plugin->ForceShutdown();
166}
167
[email protected]3bdba0d2011-08-23 07:17:30168static bool IsSyncableExtension(const Extension& extension) {
169 return extension.GetSyncType() == Extension::SYNC_TYPE_EXTENSION;
170}
171
172static bool IsSyncableApp(const Extension& extension) {
173 return extension.GetSyncType() == Extension::SYNC_TYPE_APP;
174}
175
[email protected]5fb889382011-06-03 00:29:20176// Manages an ExtensionInstallUI for a particular extension.
177class SimpleExtensionLoadPrompt : public ExtensionInstallUI::Delegate {
178 public:
179 SimpleExtensionLoadPrompt(Profile* profile,
180 base::WeakPtr<ExtensionService> extension_service,
181 const Extension* extension);
182 ~SimpleExtensionLoadPrompt();
183
184 void ShowPrompt();
185
186 // ExtensionInstallUI::Delegate
187 virtual void InstallUIProceed();
[email protected]d828fac2011-06-28 05:43:04188 virtual void InstallUIAbort(bool user_initiated);
[email protected]5fb889382011-06-03 00:29:20189
190 private:
191 base::WeakPtr<ExtensionService> extension_service_;
192 scoped_ptr<ExtensionInstallUI> install_ui_;
193 scoped_refptr<const Extension> extension_;
194};
195
196SimpleExtensionLoadPrompt::SimpleExtensionLoadPrompt(
197 Profile* profile,
198 base::WeakPtr<ExtensionService> extension_service,
199 const Extension* extension)
200 : extension_service_(extension_service),
201 install_ui_(new ExtensionInstallUI(profile)),
202 extension_(extension) {
203}
204
205SimpleExtensionLoadPrompt::~SimpleExtensionLoadPrompt() {
206}
207
208void SimpleExtensionLoadPrompt::ShowPrompt() {
209 install_ui_->ConfirmInstall(this, extension_);
210}
211
212void SimpleExtensionLoadPrompt::InstallUIProceed() {
213 if (extension_service_.get())
[email protected]8266d662011-07-12 21:53:26214 extension_service_->OnExtensionInstalled(
[email protected]f1b66652011-08-25 23:30:58215 extension_, false, -1); // Not from web store.
[email protected]5fb889382011-06-03 00:29:20216 delete this;
217}
218
[email protected]d828fac2011-06-28 05:43:04219void SimpleExtensionLoadPrompt::InstallUIAbort(bool user_initiated) {
[email protected]5fb889382011-06-03 00:29:20220 delete this;
221}
222
[email protected]c6d474f82009-12-16 21:11:06223} // namespace
[email protected]b6ab96d2009-08-20 18:58:19224
[email protected]ad63a792011-06-16 06:41:11225bool ExtensionService::ComponentExtensionInfo::Equals(
226 const ComponentExtensionInfo& other) const {
227 return other.manifest == manifest && other.root_directory == root_directory;
228}
229
[email protected]eaa7dd182010-12-14 11:09:00230ExtensionService::ExtensionRuntimeData::ExtensionRuntimeData()
[email protected]d7e9a862010-11-03 21:57:49231 : background_page_ready(false),
232 being_upgraded(false) {
233}
234
[email protected]eaa7dd182010-12-14 11:09:00235ExtensionService::ExtensionRuntimeData::~ExtensionRuntimeData() {
[email protected]d7e9a862010-11-03 21:57:49236}
237
[email protected]cebc3dc2011-04-18 17:15:00238ExtensionService::NaClModuleInfo::NaClModuleInfo() {
239}
240
241ExtensionService::NaClModuleInfo::~NaClModuleInfo() {
242}
243
[email protected]eaa7dd182010-12-14 11:09:00244// ExtensionService.
[email protected]6014d672008-12-05 00:38:25245
[email protected]eaa7dd182010-12-14 11:09:00246const char* ExtensionService::kInstallDirectoryName = "Extensions";
[email protected]b7f853e282011-08-10 09:24:20247const char* ExtensionService::kSettingsDirectoryName = "Extension Settings";
[email protected]494c06e2009-07-25 01:06:42248
[email protected]eaa7dd182010-12-14 11:09:00249// Implements IO for the ExtensionService.
[email protected]7a4c6852010-09-16 03:44:22250
[email protected]eaa7dd182010-12-14 11:09:00251class ExtensionServiceBackend
[email protected]8e4560b62011-01-14 10:09:14252 : public base::RefCountedThreadSafe<ExtensionServiceBackend> {
[email protected]7a4c6852010-09-16 03:44:22253 public:
[email protected]1f830eb2010-09-28 08:25:14254 // |install_directory| is a path where to look for extensions to load.
[email protected]14908b72011-04-20 06:54:36255 ExtensionServiceBackend(
256 base::WeakPtr<ExtensionService> frontend,
257 const FilePath& install_directory);
[email protected]7a4c6852010-09-16 03:44:22258
259 // Loads a single extension from |path| where |path| is the top directory of
[email protected]e49002a2011-07-13 03:40:44260 // a specific extension where its manifest file lives. If |prompt_for_plugins|
261 // is true and the extension contains plugins, we prompt the user before
262 // loading.
[email protected]7a4c6852010-09-16 03:44:22263 // Errors are reported through ExtensionErrorReporter. On success,
[email protected]49cd1572011-02-08 21:38:45264 // AddExtension() is called.
[email protected]7a4c6852010-09-16 03:44:22265 // TODO(erikkay): It might be useful to be able to load a packed extension
266 // (presumably into memory) without installing it.
[email protected]e49002a2011-07-13 03:40:44267 void LoadSingleExtension(const FilePath &path, bool prompt_for_plugins);
[email protected]7a4c6852010-09-16 03:44:22268
[email protected]7a4c6852010-09-16 03:44:22269 private:
[email protected]eaa7dd182010-12-14 11:09:00270 friend class base::RefCountedThreadSafe<ExtensionServiceBackend>;
[email protected]7a4c6852010-09-16 03:44:22271
[email protected]eaa7dd182010-12-14 11:09:00272 virtual ~ExtensionServiceBackend();
[email protected]7a4c6852010-09-16 03:44:22273
[email protected]d10f4602011-06-16 15:44:50274 // LoadSingleExtension needs to check the file access preference, which needs
275 // to happen back on the UI thread, so it posts CheckExtensionFileAccess on
276 // the UI thread. In turn, once that gets the pref, it goes back to the
277 // file thread with LoadSingleExtensionWithFileAccess.
[email protected]e49002a2011-07-13 03:40:44278 void CheckExtensionFileAccess(const FilePath& extension_path,
279 bool prompt_for_plugins);
[email protected]d10f4602011-06-16 15:44:50280 void LoadSingleExtensionWithFileAccess(
[email protected]e49002a2011-07-13 03:40:44281 const FilePath &path, bool allow_file_access, bool prompt_for_plugins);
[email protected]d10f4602011-06-16 15:44:50282
[email protected]7a4c6852010-09-16 03:44:22283 // Notify the frontend that there was an error loading an extension.
284 void ReportExtensionLoadError(const FilePath& extension_path,
285 const std::string& error);
286
[email protected]14908b72011-04-20 06:54:36287 // Notify the frontend that an extension was installed.
[email protected]e49002a2011-07-13 03:40:44288 void OnLoadSingleExtension(const scoped_refptr<const Extension>& extension,
289 bool prompt_for_plugins);
[email protected]14908b72011-04-20 06:54:36290
291 base::WeakPtr<ExtensionService> frontend_;
[email protected]7a4c6852010-09-16 03:44:22292
293 // The top-level extensions directory being installed to.
294 FilePath install_directory_;
295
[email protected]eaa7dd182010-12-14 11:09:00296 DISALLOW_COPY_AND_ASSIGN(ExtensionServiceBackend);
[email protected]7a4c6852010-09-16 03:44:22297};
298
[email protected]eaa7dd182010-12-14 11:09:00299ExtensionServiceBackend::ExtensionServiceBackend(
[email protected]14908b72011-04-20 06:54:36300 base::WeakPtr<ExtensionService> frontend,
[email protected]aebe23a32010-12-10 22:15:48301 const FilePath& install_directory)
[email protected]14908b72011-04-20 06:54:36302 : frontend_(frontend),
303 install_directory_(install_directory) {
304 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]7a4c6852010-09-16 03:44:22305}
306
[email protected]eaa7dd182010-12-14 11:09:00307ExtensionServiceBackend::~ExtensionServiceBackend() {
[email protected]14908b72011-04-20 06:54:36308 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
309 BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]7a4c6852010-09-16 03:44:22310}
311
[email protected]e49002a2011-07-13 03:40:44312void ExtensionServiceBackend::LoadSingleExtension(const FilePath& path_in,
313 bool prompt_for_plugins) {
[email protected]a29a517a2011-01-21 21:11:12314 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]a8af9fdb2010-10-28 21:52:20315
[email protected]7a4c6852010-09-16 03:44:22316 FilePath extension_path = path_in;
317 file_util::AbsolutePath(&extension_path);
318
[email protected]e49002a2011-07-13 03:40:44319 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
[email protected]53612e82011-10-18 18:00:36320 base::Bind(&ExtensionServiceBackend::CheckExtensionFileAccess,
321 this, extension_path, prompt_for_plugins));
[email protected]d10f4602011-06-16 15:44:50322}
323
324void ExtensionServiceBackend::CheckExtensionFileAccess(
[email protected]e49002a2011-07-13 03:40:44325 const FilePath& extension_path, bool prompt_for_plugins) {
[email protected]d10f4602011-06-16 15:44:50326 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
327 std::string id = Extension::GenerateIdForPath(extension_path);
[email protected]cdfca9702011-08-08 16:07:01328 // Unpacked extensions default to allowing file access, but if that has been
329 // overridden, don't reset the value.
330 bool allow_file_access =
331 Extension::ShouldAlwaysAllowFileAccess(Extension::LOAD);
332 if (frontend_->extension_prefs()->HasAllowFileAccessSetting(id))
333 allow_file_access = frontend_->extension_prefs()->AllowFileAccess(id);
[email protected]d10f4602011-06-16 15:44:50334
[email protected]e49002a2011-07-13 03:40:44335 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
[email protected]53612e82011-10-18 18:00:36336 base::Bind(
[email protected]e49002a2011-07-13 03:40:44337 &ExtensionServiceBackend::LoadSingleExtensionWithFileAccess,
[email protected]53612e82011-10-18 18:00:36338 this, extension_path, allow_file_access, prompt_for_plugins));
[email protected]d10f4602011-06-16 15:44:50339}
340
[email protected]d10f4602011-06-16 15:44:50341void ExtensionServiceBackend::LoadSingleExtensionWithFileAccess(
[email protected]e49002a2011-07-13 03:40:44342 const FilePath& extension_path,
343 bool allow_file_access,
344 bool prompt_for_plugins) {
[email protected]d10f4602011-06-16 15:44:50345 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
346 int flags = allow_file_access ?
[email protected]3aff9ad2011-04-01 20:26:48347 Extension::ALLOW_FILE_ACCESS : Extension::NO_FLAGS;
[email protected]83048a22011-03-29 00:14:13348 if (Extension::ShouldDoStrictErrorChecking(Extension::LOAD))
349 flags |= Extension::STRICT_ERROR_CHECKS;
[email protected]7a4c6852010-09-16 03:44:22350 std::string error;
[email protected]ad8e04a2010-11-01 04:16:27351 scoped_refptr<const Extension> extension(extension_file_util::LoadExtension(
[email protected]7a4c6852010-09-16 03:44:22352 extension_path,
[email protected]92888082010-10-18 19:24:57353 Extension::LOAD,
[email protected]83048a22011-03-29 00:14:13354 flags,
[email protected]ad8e04a2010-11-01 04:16:27355 &error));
[email protected]7a4c6852010-09-16 03:44:22356
357 if (!extension) {
[email protected]e49002a2011-07-13 03:40:44358 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
[email protected]53612e82011-10-18 18:00:36359 base::Bind(
[email protected]704d77e2011-10-17 18:05:29360 &ExtensionServiceBackend::ReportExtensionLoadError,
[email protected]53612e82011-10-18 18:00:36361 this,
[email protected]e49002a2011-07-13 03:40:44362 extension_path, error));
[email protected]7a4c6852010-09-16 03:44:22363 return;
364 }
365
[email protected]7a4c6852010-09-16 03:44:22366 // Report this as an installed extension so that it gets remembered in the
367 // prefs.
[email protected]e49002a2011-07-13 03:40:44368 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
[email protected]53612e82011-10-18 18:00:36369 base::Bind(
[email protected]e49002a2011-07-13 03:40:44370 &ExtensionServiceBackend::OnLoadSingleExtension,
[email protected]53612e82011-10-18 18:00:36371 this, extension, prompt_for_plugins));
[email protected]7a4c6852010-09-16 03:44:22372}
373
[email protected]eaa7dd182010-12-14 11:09:00374void ExtensionServiceBackend::ReportExtensionLoadError(
[email protected]7a4c6852010-09-16 03:44:22375 const FilePath& extension_path, const std::string &error) {
[email protected]14908b72011-04-20 06:54:36376 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
377 if (frontend_.get())
378 frontend_->ReportExtensionLoadError(
[email protected]655b2b1a2011-10-13 17:13:06379 extension_path, error, true /* alert_on_error */);
[email protected]14908b72011-04-20 06:54:36380}
381
[email protected]5fb889382011-06-03 00:29:20382void ExtensionServiceBackend::OnLoadSingleExtension(
[email protected]e49002a2011-07-13 03:40:44383 const scoped_refptr<const Extension>& extension, bool prompt_for_plugins) {
[email protected]14908b72011-04-20 06:54:36384 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
385 if (frontend_.get())
[email protected]e49002a2011-07-13 03:40:44386 frontend_->OnLoadSingleExtension(extension, prompt_for_plugins);
[email protected]7a4c6852010-09-16 03:44:22387}
388
[email protected]8e4560b62011-01-14 10:09:14389void ExtensionService::CheckExternalUninstall(const std::string& id) {
[email protected]a29a517a2011-01-21 21:11:12390 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20391
[email protected]7a4c6852010-09-16 03:44:22392 // Check if the providers know about this extension.
[email protected]0a60a2e2010-10-25 16:15:21393 ProviderCollection::const_iterator i;
394 for (i = external_extension_providers_.begin();
395 i != external_extension_providers_.end(); ++i) {
[email protected]8e4560b62011-01-14 10:09:14396 DCHECK(i->get()->IsReady());
[email protected]0a60a2e2010-10-25 16:15:21397 if (i->get()->HasExtension(id))
398 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22399 }
400
[email protected]0f48fca2011-05-19 18:46:35401 // We get the list of external extensions to check from preferences.
402 // It is possible that an extension has preferences but is not loaded.
403 // For example, an extension that requires experimental permissions
404 // will not be loaded if the experimental command line flag is not used.
405 // In this case, do not uninstall.
406 const Extension* extension = GetInstalledExtension(id);
407 if (!extension) {
408 // We can't call UninstallExtension with an unloaded/invalid
409 // extension ID.
410 LOG(WARNING) << "Attempted uninstallation of unloaded/invalid extension "
411 << "with id: " << id;
412 return;
413 }
[email protected]d6ebc9792011-04-07 18:18:33414 UninstallExtension(id, true, NULL);
[email protected]7a4c6852010-09-16 03:44:22415}
416
[email protected]8e4560b62011-01-14 10:09:14417void ExtensionService::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22418 external_extension_providers_.clear();
419}
420
[email protected]8e4560b62011-01-14 10:09:14421void ExtensionService::AddProviderForTesting(
422 ExternalExtensionProviderInterface* test_provider) {
[email protected]a29a517a2011-01-21 21:11:12423 CHECK(test_provider);
[email protected]0a60a2e2010-10-25 16:15:21424 external_extension_providers_.push_back(
[email protected]8e4560b62011-01-14 10:09:14425 linked_ptr<ExternalExtensionProviderInterface>(test_provider));
[email protected]7a4c6852010-09-16 03:44:22426}
427
[email protected]8e4560b62011-01-14 10:09:14428void ExtensionService::OnExternalExtensionUpdateUrlFound(
[email protected]7a4c6852010-09-16 03:44:22429 const std::string& id,
[email protected]21a5a672010-11-04 10:47:42430 const GURL& update_url,
431 Extension::Location location) {
[email protected]ab22ba42011-01-14 16:36:38432 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
433 CHECK(Extension::IdIsValid(id));
[email protected]a8af9fdb2010-10-28 21:52:20434
[email protected]8a87a5332011-08-11 17:54:59435 const Extension* extension = GetExtensionById(id, true);
436 if (extension) {
437 // Already installed. Skip this install if the current location has
438 // higher priority than |location|.
439 Extension::Location current = extension->location();
440 if (current == Extension::GetHigherPriorityLocation(current, location))
441 return;
442 // Otherwise, overwrite the current installation.
[email protected]7a4c6852010-09-16 03:44:22443 }
[email protected]b2907fd2011-03-25 16:43:37444 pending_extension_manager()->AddFromExternalUpdateUrl(
445 id, update_url, location);
[email protected]50067e52011-10-20 23:17:07446 external_extension_url_added_ = true;
[email protected]7a4c6852010-09-16 03:44:22447}
448
[email protected]454434f2011-08-24 04:00:00449// If a download url matches one of these patterns and has a referrer of the
450// webstore, then we're willing to treat that as a gallery download.
451static const char* kAllowedDownloadURLPatterns[] = {
452 "https://clients2.google.com/service/update2*",
453 "https://clients2.googleusercontent.com/crx/*"
454};
455
[email protected]eaa7dd182010-12-14 11:09:00456bool ExtensionService::IsDownloadFromGallery(const GURL& download_url,
[email protected]a29a517a2011-01-21 21:11:12457 const GURL& referrer_url) {
[email protected]d3071992010-10-08 15:24:07458 // Special-case the themes mini-gallery.
459 // TODO(erikkay) When that gallery goes away, remove this code.
460 if (IsDownloadFromMiniGallery(download_url) &&
461 StartsWithASCII(referrer_url.spec(),
462 extension_urls::kMiniGalleryBrowsePrefix, false)) {
[email protected]334e04a2010-06-24 23:34:44463 return true;
[email protected]1debbbb62010-10-06 17:23:44464 }
[email protected]473ff6e2010-05-12 15:31:55465
[email protected]9adb9692010-10-29 23:14:02466 const Extension* download_extension = GetExtensionByWebExtent(download_url);
467 const Extension* referrer_extension = GetExtensionByWebExtent(referrer_url);
468 const Extension* webstore_app = GetWebStoreApp();
[email protected]d3071992010-10-08 15:24:07469
470 bool referrer_valid = (referrer_extension == webstore_app);
471 bool download_valid = (download_extension == webstore_app);
472
[email protected]454434f2011-08-24 04:00:00473 // We also allow the download to be from a small set of trusted paths.
474 if (!download_valid) {
475 for (size_t i = 0; i < arraysize(kAllowedDownloadURLPatterns); i++) {
476 URLPattern pattern(URLPattern::SCHEME_HTTPS,
477 kAllowedDownloadURLPatterns[i]);
478 if (pattern.MatchesURL(download_url)) {
479 download_valid = true;
480 break;
481 }
482 }
483 }
484
[email protected]d3071992010-10-08 15:24:07485 // If the command-line gallery URL is set, then be a bit more lenient.
486 GURL store_url =
487 GURL(CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
488 switches::kAppsGalleryURL));
489 if (!store_url.is_empty()) {
490 std::string store_tld =
491 net::RegistryControlledDomainService::GetDomainAndRegistry(store_url);
492 if (!referrer_valid) {
493 std::string referrer_tld =
494 net::RegistryControlledDomainService::GetDomainAndRegistry(
495 referrer_url);
496 // The referrer gets stripped when transitioning from https to http,
497 // or when hitting an unknown test cert and that commonly happens in
498 // testing environments. Given this, we allow an empty referrer when
499 // the command-line flag is set.
500 // Otherwise, the TLD must match the TLD of the command-line url.
501 referrer_valid = referrer_url.is_empty() || (referrer_tld == store_tld);
502 }
503
504 if (!download_valid) {
505 std::string download_tld =
506 net::RegistryControlledDomainService::GetDomainAndRegistry(
[email protected]04866c42011-05-03 20:03:50507 download_url);
[email protected]d3071992010-10-08 15:24:07508
509 // Otherwise, the TLD must match the TLD of the command-line url.
510 download_valid = (download_tld == store_tld);
511 }
512 }
513
514 return (referrer_valid && download_valid);
[email protected]b7c2f252009-12-08 00:47:23515}
516
[email protected]eaa7dd182010-12-14 11:09:00517bool ExtensionService::IsDownloadFromMiniGallery(const GURL& download_url) {
[email protected]ac025282009-12-16 19:16:38518 return StartsWithASCII(download_url.spec(),
519 extension_urls::kMiniGalleryDownloadPrefix,
520 false); // case_sensitive
521}
522
[email protected]d9696672011-03-15 22:45:09523const Extension* ExtensionService::GetInstalledApp(const GURL& url) {
[email protected]ffb204f22010-12-05 23:20:27524 // Check for hosted app.
[email protected]d9696672011-03-15 22:45:09525 const Extension* app = GetExtensionByWebExtent(url);
526 if (app)
527 return app;
[email protected]ffb204f22010-12-05 23:20:27528
529 // Check for packaged app.
[email protected]d9696672011-03-15 22:45:09530 app = GetExtensionByURL(url);
531 if (app && app->is_app())
532 return app;
533
534 return NULL;
535}
536
537bool ExtensionService::IsInstalledApp(const GURL& url) {
538 return !!GetInstalledApp(url);
[email protected]ffb204f22010-12-05 23:20:27539}
540
[email protected]da5683db2011-04-23 17:12:21541void ExtensionService::SetInstalledAppForRenderer(int renderer_child_id,
542 const Extension* app) {
543 installed_app_hosts_[renderer_child_id] = app;
544}
545
546const Extension* ExtensionService::GetInstalledAppForRenderer(
547 int renderer_child_id) {
548 InstalledAppMap::iterator i = installed_app_hosts_.find(renderer_child_id);
549 if (i == installed_app_hosts_.end())
550 return NULL;
551 return i->second;
552}
553
[email protected]6aeac8342010-10-01 20:21:18554// static
[email protected]d6ebc9792011-04-07 18:18:33555// This function is used to implement the command-line switch
[email protected]3bdba0d2011-08-23 07:17:30556// --uninstall-extension, and to uninstall an extension via sync. The LOG
557// statements within this function are used to inform the user if the uninstall
558// cannot be done.
[email protected]eaa7dd182010-12-14 11:09:00559bool ExtensionService::UninstallExtensionHelper(
560 ExtensionService* extensions_service,
[email protected]6aeac8342010-10-01 20:21:18561 const std::string& extension_id) {
[email protected]6aeac8342010-10-01 20:21:18562
[email protected]95da88c42011-03-31 10:07:33563 const Extension* extension =
[email protected]8001df22011-04-28 19:59:47564 extensions_service->GetInstalledExtension(extension_id);
[email protected]95da88c42011-03-31 10:07:33565
[email protected]d6ebc9792011-04-07 18:18:33566 // We can't call UninstallExtension with an invalid extension ID.
[email protected]95da88c42011-03-31 10:07:33567 if (!extension) {
[email protected]6aeac8342010-10-01 20:21:18568 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]95da88c42011-03-31 10:07:33569 << "id: " << extension_id;
570 return false;
[email protected]6aeac8342010-10-01 20:21:18571 }
572
[email protected]d6ebc9792011-04-07 18:18:33573 // The following call to UninstallExtension will not allow an uninstall of a
574 // policy-controlled extension.
575 std::string error;
576 if (!extensions_service->UninstallExtension(extension_id, false, &error)) {
577 LOG(WARNING) << "Cannot uninstall extension with id " << extension_id
578 << ": " << error;
579 return false;
580 }
[email protected]95da88c42011-03-31 10:07:33581
[email protected]6aeac8342010-10-01 20:21:18582 return true;
583}
584
[email protected]eaa7dd182010-12-14 11:09:00585ExtensionService::ExtensionService(Profile* profile,
[email protected]f0841cd2011-01-19 15:07:24586 const CommandLine* command_line,
587 const FilePath& install_directory,
588 ExtensionPrefs* extension_prefs,
[email protected]0436b102011-04-15 18:30:03589 bool autoupdate_enabled,
590 bool extensions_enabled)
[email protected]14908b72011-04-20 06:54:36591 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]14908b72011-04-20 06:54:36592 profile_(profile),
[email protected]73c47932010-12-06 18:13:43593 extension_prefs_(extension_prefs),
[email protected]fc84e0e2011-09-27 19:18:22594 extension_settings_frontend_(
[email protected]adfc5462011-09-21 19:07:13595 profile->GetPath().AppendASCII(kSettingsDirectoryName)),
[email protected]14908b72011-04-20 06:54:36596 pending_extension_manager_(*ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]a9b00ac2009-06-25 21:03:23597 install_directory_(install_directory),
[email protected]0436b102011-04-15 18:30:03598 extensions_enabled_(extensions_enabled),
[email protected]e81dba32009-06-19 20:19:13599 show_extensions_prompts_(true),
[email protected]3ecda252010-11-18 19:50:55600 ready_(false),
[email protected]14908b72011-04-20 06:54:36601 toolbar_model_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]8b280302011-10-13 22:22:23602 menu_manager_(profile),
[email protected]af9db5f2011-10-05 05:13:15603 app_notification_manager_(new AppNotificationManager(profile)),
[email protected]902fd7b2011-07-27 18:42:31604 permissions_manager_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]18cc5a52011-04-15 16:03:38605 apps_promo_(profile->GetPrefs()),
[email protected]655b2b1a2011-10-13 17:13:06606 webstore_installer_(profile),
[email protected]7c1490da2011-10-11 18:53:25607 event_routers_initialized_(false),
608 extension_warnings_(profile) {
[email protected]a29a517a2011-01-21 21:11:12609 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20610
[email protected]36a784c2009-06-23 06:21:08611 // Figure out if extension installation should be enabled.
[email protected]6d60703b2009-08-29 01:29:23612 if (command_line->HasSwitch(switches::kDisableExtensions)) {
613 extensions_enabled_ = false;
614 } else if (profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions)) {
615 extensions_enabled_ = false;
[email protected]6b75ec32009-08-14 06:37:18616 }
[email protected]36a784c2009-06-23 06:21:08617
[email protected]432115822011-07-10 15:52:27618 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07619 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27620 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED,
[email protected]ad50def52011-10-19 23:17:07621 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27622 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07623 content::NotificationService::AllBrowserContextsAndSources());
[email protected]2fb7dc982010-09-29 12:24:28624 pref_change_registrar_.Init(profile->GetPrefs());
625 pref_change_registrar_.Add(prefs::kExtensionInstallAllowList, this);
626 pref_change_registrar_.Add(prefs::kExtensionInstallDenyList, this);
[email protected]4814b512009-11-07 00:12:29627
[email protected]93fd78f42009-07-10 16:43:17628 // Set up the ExtensionUpdater
629 if (autoupdate_enabled) {
630 int update_frequency = kDefaultUpdateFrequencySeconds;
631 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25632 base::StringToInt(command_line->GetSwitchValueASCII(
633 switches::kExtensionsUpdateFrequency),
634 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17635 }
[email protected]82b6e512011-04-12 20:33:27636 updater_.reset(new ExtensionUpdater(this,
637 extension_prefs,
638 profile->GetPrefs(),
639 profile,
640 update_frequency));
[email protected]93fd78f42009-07-10 16:43:17641 }
642
[email protected]14908b72011-04-20 06:54:36643 backend_ =
644 new ExtensionServiceBackend(weak_ptr_factory_.GetWeakPtr(),
645 install_directory_);
[email protected]8e4560b62011-01-14 10:09:14646
[email protected]af9db5f2011-10-05 05:13:15647 app_notification_manager_->Init();
648
[email protected]0436b102011-04-15 18:30:03649 if (extensions_enabled_) {
[email protected]873531342011-03-09 12:16:05650 ExternalExtensionProviderImpl::CreateExternalProviders(
651 this, profile_, &external_extension_providers_);
652 }
[email protected]b671760b2010-07-15 21:13:47653
[email protected]aa96d3a2010-08-21 08:45:25654 // Use monochrome icons for Omnibox icons.
[email protected]29d0d4ac2010-09-08 21:10:31655 omnibox_popup_icon_manager_.set_monochrome(true);
[email protected]b671760b2010-07-15 21:13:47656 omnibox_icon_manager_.set_monochrome(true);
[email protected]29d0d4ac2010-09-08 21:10:31657 omnibox_icon_manager_.set_padding(gfx::Insets(0, kOmniboxIconPaddingLeft,
658 0, kOmniboxIconPaddingRight));
[email protected]cb0e50312011-05-09 15:03:07659
660 // How long is the path to the Extensions directory?
661 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ExtensionRootPathLength",
662 install_directory_.value().length(), 0, 500, 100);
[email protected]6014d672008-12-05 00:38:25663}
664
[email protected]eaa7dd182010-12-14 11:09:00665const ExtensionList* ExtensionService::extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45666 return &extensions_;
667}
668
[email protected]eaa7dd182010-12-14 11:09:00669const ExtensionList* ExtensionService::disabled_extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45670 return &disabled_extensions_;
671}
672
[email protected]bb7f40952011-01-13 00:21:20673const ExtensionList* ExtensionService::terminated_extensions() const {
674 return &terminated_extensions_;
675}
676
[email protected]b2907fd2011-03-25 16:43:37677PendingExtensionManager* ExtensionService::pending_extension_manager() {
678 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45679}
680
[email protected]ad63a792011-06-16 06:41:11681void ExtensionService::UnregisterComponentExtension(
682 const ComponentExtensionInfo& info) {
683 RegisteredComponentExtensions new_component_extension_manifests;
684 for (RegisteredComponentExtensions::iterator it =
685 component_extension_manifests_.begin();
686 it != component_extension_manifests_.end(); ++it) {
687 if (!it->Equals(info))
688 new_component_extension_manifests.push_back(*it);
689 }
690 component_extension_manifests_.swap(new_component_extension_manifests);
691}
692
[email protected]eaa7dd182010-12-14 11:09:00693ExtensionService::~ExtensionService() {
[email protected]c8d407e2011-04-28 21:27:17694 // No need to unload extensions here because they are profile-scoped, and the
695 // profile is in the process of being deleted.
[email protected]94b4ffc2011-02-04 05:55:17696
[email protected]8e4560b62011-01-14 10:09:14697 ProviderCollection::const_iterator i;
698 for (i = external_extension_providers_.begin();
699 i != external_extension_providers_.end(); ++i) {
700 ExternalExtensionProviderInterface* provider = i->get();
701 provider->ServiceShutdown();
702 }
[email protected]6014d672008-12-05 00:38:25703}
704
[email protected]d91268022011-08-26 13:17:37705void ExtensionService::InitEventRoutersAfterImport() {
706 registrar_.Add(this, chrome::NOTIFICATION_IMPORT_FINISHED,
[email protected]6c2381d2011-10-19 02:52:53707 content::Source<Profile>(profile_));
[email protected]d91268022011-08-26 13:17:37708}
709
[email protected]eaa7dd182010-12-14 11:09:00710void ExtensionService::InitEventRouters() {
[email protected]b3d62312b12010-10-14 21:10:18711 if (event_routers_initialized_)
712 return;
713
[email protected]370e9502011-09-14 19:31:44714 downloads_event_router_.reset(new ExtensionDownloadsEventRouter(profile_));
[email protected]f825df22011-06-28 17:36:10715 history_event_router_.reset(new ExtensionHistoryEventRouter());
716 history_event_router_->ObserveProfile(profile_);
[email protected]97d2f1d2011-01-15 00:41:08717 browser_event_router_.reset(new ExtensionBrowserEventRouter(profile_));
718 browser_event_router_->Init();
[email protected]118de7892011-03-16 13:31:40719 preference_event_router_.reset(new ExtensionPreferenceEventRouter(profile_));
[email protected]f825df22011-06-28 17:36:10720 bookmark_event_router_.reset(new ExtensionBookmarkEventRouter(
721 profile_->GetBookmarkModel()));
722 bookmark_event_router_->Init();
723 cookies_event_router_.reset(new ExtensionCookiesEventRouter(profile_));
724 cookies_event_router_->Init();
725 management_event_router_.reset(new ExtensionManagementEventRouter(profile_));
726 management_event_router_->Init();
[email protected]7da9a8232011-05-27 21:07:11727 ExtensionProcessesEventRouter::GetInstance()->ObserveProfile(profile_);
[email protected]f825df22011-06-28 17:36:10728 web_navigation_event_router_.reset(
729 new ExtensionWebNavigationEventRouter(profile_));
730 web_navigation_event_router_->Init();
[email protected]27072cad2011-05-09 19:46:40731
[email protected]eed367e2011-04-12 03:43:31732#if defined(OS_CHROMEOS)
[email protected]b16fc2e2011-07-21 21:32:58733 file_browser_event_router_.reset(
734 new ExtensionFileBrowserEventRouter(profile_));
735 file_browser_event_router_->ObserveFileSystemEvents();
[email protected]771a6182011-10-05 16:32:52736
737 input_method_event_router_.reset(
738 new chromeos::ExtensionInputMethodEventRouter);
[email protected]be5bd5742011-07-14 07:21:01739
[email protected]c8bf09be2011-06-25 03:22:27740 ExtensionMediaPlayerEventRouter::GetInstance()->Init(profile_);
[email protected]14da67b02011-08-02 05:49:13741 ExtensionInputImeEventRouter::GetInstance()->Init();
[email protected]eed367e2011-04-12 03:43:31742#endif
[email protected]27072cad2011-05-09 19:46:40743
744#if defined(OS_CHROMEOS) && defined(TOUCH_UI)
745 ExtensionInputUiEventRouter::GetInstance()->Init();
746#endif
747
[email protected]b3d62312b12010-10-14 21:10:18748 event_routers_initialized_ = true;
[email protected]c5ae74ab2010-04-15 18:14:37749}
750
[email protected]b2907fd2011-03-25 16:43:37751const Extension* ExtensionService::GetExtensionById(
752 const std::string& id, bool include_disabled) const {
[email protected]8001df22011-04-28 19:59:47753 return GetExtensionByIdInternal(id, true, include_disabled, false);
[email protected]78994ab02010-12-08 18:06:44754}
755
[email protected]eaa7dd182010-12-14 11:09:00756void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12757 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20758
[email protected]fa6a9102010-11-22 15:38:50759 DCHECK(!ready_); // Can't redo init.
[email protected]93fd78f42009-07-10 16:43:17760 DCHECK_EQ(extensions_.size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32761
[email protected]95dd38f2009-10-20 20:09:15762 // Hack: we need to ensure the ResourceDispatcherHost is ready before we load
763 // the first extension, because its members listen for loaded notifications.
764 g_browser_process->resource_dispatcher_host();
765
[email protected]9f1087e2009-06-15 17:29:32766 LoadAllExtensions();
[email protected]894bb502009-05-21 22:39:57767
[email protected]9f1087e2009-06-15 17:29:32768 // TODO(erikkay) this should probably be deferred to a future point
769 // rather than running immediately at startup.
[email protected]93fd78f42009-07-10 16:43:17770 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57771
[email protected]9f1087e2009-06-15 17:29:32772 // TODO(erikkay) this should probably be deferred as well.
773 GarbageCollectExtensions();
[email protected]6014d672008-12-05 00:38:25774}
775
[email protected]420a0ec2011-06-01 01:07:03776bool ExtensionService::UpdateExtension(
777 const std::string& id,
778 const FilePath& extension_path,
779 const GURL& download_url,
780 CrxInstaller** out_crx_installer) {
[email protected]a29a517a2011-01-21 21:11:12781 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20782
[email protected]b2907fd2011-03-25 16:43:37783 PendingExtensionInfo pending_extension_info;
784 bool is_pending_extension = pending_extension_manager_.GetById(
785 id, &pending_extension_info);
[email protected]8ef78fd2010-08-19 17:14:32786
[email protected]8001df22011-04-28 19:59:47787 const Extension* extension =
788 GetExtensionByIdInternal(id, true, true, false);
[email protected]5eb375e92010-11-26 07:50:41789 if (!is_pending_extension && !extension) {
[email protected]aa142702010-03-26 01:26:33790 LOG(WARNING) << "Will not update extension " << id
791 << " because it is not installed or pending";
792 // Delete extension_path since we're not creating a CrxInstaller
793 // that would do it for us.
[email protected]14908b72011-04-20 06:54:36794 if (!BrowserThread::PostTask(
795 BrowserThread::FILE, FROM_HERE,
[email protected]53612e82011-10-18 18:00:36796 base::Bind(
[email protected]14908b72011-04-20 06:54:36797 extension_file_util::DeleteFile, extension_path, false)))
798 NOTREACHED();
[email protected]420a0ec2011-06-01 01:07:03799
800 return false;
[email protected]e957fe52009-06-23 16:51:05801 }
802
[email protected]aa142702010-03-26 01:26:33803 // We want a silent install only for non-pending extensions and
804 // pending extensions that have install_silently set.
805 ExtensionInstallUI* client =
[email protected]b2907fd2011-03-25 16:43:37806 (!is_pending_extension || pending_extension_info.install_silently()) ?
[email protected]aa142702010-03-26 01:26:33807 NULL : new ExtensionInstallUI(profile_);
808
[email protected]14908b72011-04-20 06:54:36809 scoped_refptr<CrxInstaller> installer(MakeCrxInstaller(client));
[email protected]6dfbbf82010-03-12 23:09:16810 installer->set_expected_id(id);
[email protected]ec5b50d2010-10-09 16:35:18811 if (is_pending_extension)
[email protected]b2907fd2011-03-25 16:43:37812 installer->set_install_source(pending_extension_info.install_source());
[email protected]5eb375e92010-11-26 07:50:41813 else if (extension)
814 installer->set_install_source(extension->location());
[email protected]0d3e4a22011-06-23 19:02:52815 if (pending_extension_info.install_silently())
816 installer->set_allow_silent_install(true);
[email protected]3d729722011-09-20 02:57:09817 if (extension && extension->from_webstore())
818 installer->set_is_gallery_install(true);
[email protected]6dfbbf82010-03-12 23:09:16819 installer->set_delete_source(true);
[email protected]655b2b1a2011-10-13 17:13:06820 installer->set_download_url(download_url);
[email protected]cb0e50312011-05-09 15:03:07821 installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
[email protected]6dfbbf82010-03-12 23:09:16822 installer->InstallCrx(extension_path);
[email protected]420a0ec2011-06-01 01:07:03823
824 if (out_crx_installer)
825 *out_crx_installer = installer;
826
827 return true;
[email protected]e957fe52009-06-23 16:51:05828}
829
[email protected]eaa7dd182010-12-14 11:09:00830void ExtensionService::ReloadExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12831 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]b65272f2009-08-31 15:47:06832 FilePath path;
[email protected]9adb9692010-10-29 23:14:02833 const Extension* current_extension = GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40834
[email protected]f17dbd42010-08-16 23:21:10835 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]b65272f2009-08-31 15:47:06836 if (current_extension) {
[email protected]4814b512009-11-07 00:12:29837 // If the extension has an inspector open for its background page, detach
838 // the inspector and hang onto a cookie for it, so that we can reattach
839 // later.
840 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
[email protected]06024c62011-10-20 20:57:12841 ExtensionHost* host = manager->GetBackgroundHostForExtension(extension_id);
[email protected]4814b512009-11-07 00:12:29842 if (host) {
843 // Look for an open inspector for the background page.
844 int devtools_cookie = DevToolsManager::GetInstance()->DetachClientHost(
845 host->render_view_host());
846 if (devtools_cookie >= 0)
847 orphaned_dev_tools_[extension_id] = devtools_cookie;
848 }
849
[email protected]b65272f2009-08-31 15:47:06850 path = current_extension->path();
[email protected]f17dbd42010-08-16 23:21:10851 DisableExtension(extension_id);
852 disabled_extension_paths_[extension_id] = path;
[email protected]1eb175082010-02-10 09:26:16853 } else {
854 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06855 }
856
[email protected]e6090e42010-03-23 22:44:08857 // Check the installed extensions to see if what we're reloading was already
858 // installed.
859 scoped_ptr<ExtensionInfo> installed_extension(
860 extension_prefs_->GetInstalledExtensionInfo(extension_id));
861 if (installed_extension.get() &&
862 installed_extension->extension_manifest.get()) {
863 LoadInstalledExtension(*installed_extension, false);
864 } else {
865 // We should always be able to remember the extension's path. If it's not in
866 // the map, someone failed to update |unloaded_extension_paths_|.
867 CHECK(!path.empty());
868 LoadExtension(path);
869 }
[email protected]9cddd4702009-07-27 22:09:40870}
871
[email protected]fa2416f2011-05-03 08:41:20872bool ExtensionService::UninstallExtension(
873 const std::string& extension_id_unsafe,
874 bool external_uninstall,
875 std::string* error) {
[email protected]a29a517a2011-01-21 21:11:12876 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20877
[email protected]fa2416f2011-05-03 08:41:20878 // Copy the extension identifier since the reference might have been
879 // obtained via Extension::id() and the extension may be deleted in
880 // this function.
881 std::string extension_id(extension_id_unsafe);
882
[email protected]0d6ec3a72011-09-02 02:09:43883 scoped_refptr<const Extension> extension(GetInstalledExtension(extension_id));
[email protected]631cf822009-05-15 07:01:25884
[email protected]e7afe2452010-08-22 16:19:13885 // Callers should not send us nonexistent extensions.
[email protected]aebe23a32010-12-10 22:15:48886 CHECK(extension);
[email protected]9f1087e2009-06-15 17:29:32887
[email protected]95da88c42011-03-31 10:07:33888 // Policy change which triggers an uninstall will always set
889 // |external_uninstall| to true so this is the only way to uninstall
890 // managed extensions.
[email protected]0d6ec3a72011-09-02 02:09:43891 if (!Extension::UserMayDisable(extension->location()) &&
892 !external_uninstall) {
[email protected]ad50def52011-10-19 23:17:07893 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27894 chrome::NOTIFICATION_EXTENSION_UNINSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:53895 content::Source<Profile>(profile_),
896 content::Details<const Extension>(extension));
[email protected]d6ebc9792011-04-07 18:18:33897 if (error != NULL) {
898 *error = errors::kCannotUninstallManagedExtension;
899 }
900 return false;
901 }
[email protected]95da88c42011-03-31 10:07:33902
[email protected]3bdba0d2011-08-23 07:17:30903 // Extract the data we need for sync now, but don't actually sync until we've
904 // completed the uninstallation.
905 SyncBundle* sync_bundle = GetSyncBundleForExtension(*extension);
906
907 SyncChange sync_change;
908 if (sync_bundle) {
909 ExtensionSyncData extension_sync_data(*extension,
910 IsExtensionEnabled(extension_id),
911 IsIncognitoEnabled(extension_id));
912 sync_change = extension_sync_data.GetSyncChange(SyncChange::ACTION_DELETE);
913 }
914
[email protected]9b217652010-10-08 22:04:23915 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08916 extension->GetType(), 100);
[email protected]fe2dd7742011-04-19 22:52:49917 RecordPermissionMessagesHistogram(
918 extension, "Extensions.Permissions_Uninstall");
[email protected]9b217652010-10-08 22:04:23919
[email protected]8e5c89a2011-06-07 18:13:33920 TemplateURLService* url_service =
921 TemplateURLServiceFactory::GetForProfile(profile_);
922 if (url_service)
923 url_service->UnregisterExtensionKeyword(extension);
[email protected]56ad3792010-05-28 17:45:33924
[email protected]831aa212010-03-26 13:55:19925 // Unload before doing more cleanup to ensure that nothing is hanging on to
926 // any of these resources.
[email protected]814a7bf0f2011-08-13 05:30:59927 UnloadExtension(extension_id, extension_misc::UNLOAD_REASON_UNINSTALL);
[email protected]831aa212010-03-26 13:55:19928
[email protected]0d6ec3a72011-09-02 02:09:43929 extension_prefs_->OnExtensionUninstalled(extension_id, extension->location(),
[email protected]831aa212010-03-26 13:55:19930 external_uninstall);
[email protected]9f1087e2009-06-15 17:29:32931
932 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]0d6ec3a72011-09-02 02:09:43933 if (Extension::LOAD != extension->location()) {
[email protected]14908b72011-04-20 06:54:36934 if (!BrowserThread::PostTask(
935 BrowserThread::FILE, FROM_HERE,
[email protected]53612e82011-10-18 18:00:36936 base::Bind(
[email protected]14908b72011-04-20 06:54:36937 &extension_file_util::UninstallExtension,
938 install_directory_,
[email protected]fa2416f2011-05-03 08:41:20939 extension_id)))
[email protected]14908b72011-04-20 06:54:36940 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32941 }
942
[email protected]0d6ec3a72011-09-02 02:09:43943 GURL launch_web_url_origin(extension->launch_web_url());
944 launch_web_url_origin = launch_web_url_origin.GetOrigin();
945 bool is_storage_isolated =
946 (extension->is_storage_isolated() &&
947 extension->HasAPIPermission(ExtensionAPIPermission::kExperimental));
948
949 if (extension->is_hosted_app() &&
950 !profile_->GetExtensionSpecialStoragePolicy()->
951 IsStorageProtected(launch_web_url_origin)) {
[email protected]dc0b5a12011-10-14 00:06:13952 ExtensionDataDeleter::StartDeleting(
953 profile_, extension_id, launch_web_url_origin, is_storage_isolated);
[email protected]0d6ec3a72011-09-02 02:09:43954 }
[email protected]dc0b5a12011-10-14 00:06:13955 ExtensionDataDeleter::StartDeleting(
956 profile_, extension_id, extension->url(), is_storage_isolated);
[email protected]0d6ec3a72011-09-02 02:09:43957
[email protected]0dfe05c2011-02-23 23:03:36958 UntrackTerminatedExtension(extension_id);
[email protected]211030342010-09-30 18:41:06959
960 // Notify interested parties that we've uninstalled this extension.
[email protected]ad50def52011-10-19 23:17:07961 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27962 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
[email protected]6c2381d2011-10-19 02:52:53963 content::Source<Profile>(profile_),
964 content::Details<const std::string>(&extension_id));
[email protected]d6ebc9792011-04-07 18:18:33965
[email protected]3bdba0d2011-08-23 07:17:30966 if (sync_bundle && sync_bundle->HasExtensionId(extension_id)) {
967 sync_bundle->sync_processor->ProcessSyncChanges(
968 FROM_HERE, SyncChangeList(1, sync_change));
969 sync_bundle->synced_extensions.erase(extension_id);
970 }
971
[email protected]333b1de2011-09-12 18:28:50972 // Track the uninstallation.
[email protected]49098f702011-10-13 03:47:18973 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionUninstalled", 1, 2);
974
975 static bool default_apps_trial_exists =
976 base::FieldTrialList::TrialExists(kDefaultAppsTrial_Name);
977 if (default_apps_trial_exists) {
978 UMA_HISTOGRAM_ENUMERATION(
979 base::FieldTrial::MakeName("Extensions.ExtensionUninstalled",
980 kDefaultAppsTrial_Name),
981 1, 2);
982 }
[email protected]333b1de2011-09-12 18:28:50983
[email protected]7c1490da2011-10-11 18:53:25984 // Uninstalling one extension might have solved the problems of others.
985 // Therefore, we clear warnings of this type for all extensions.
986 std::set<ExtensionWarningSet::WarningType> warnings;
987 extension_warnings_.GetWarningsAffectingExtension(extension_id, &warnings);
988 extension_warnings_.ClearWarnings(warnings);
989
[email protected]d6ebc9792011-04-07 18:18:33990 return true;
[email protected]c10da4b02010-03-25 14:38:32991}
992
[email protected]c3cfb012011-04-06 22:07:35993bool ExtensionService::IsExtensionEnabled(
994 const std::string& extension_id) const {
[email protected]36429da2011-07-11 20:25:18995 const Extension* extension =
[email protected]ad83ca242011-07-29 01:32:25996 GetExtensionByIdInternal(extension_id, true, false, true);
[email protected]36429da2011-07-11 20:25:18997 if (extension)
998 return true;
999
[email protected]ad83ca242011-07-29 01:32:251000 extension =
1001 GetExtensionByIdInternal(extension_id, false, true, false);
1002 if (extension)
1003 return false;
1004
1005 // If the extension hasn't been loaded yet, check the prefs for it. Assume
1006 // enabled unless otherwise noted.
1007 return !extension_prefs_->IsExtensionDisabled(extension_id) &&
1008 !extension_prefs_->IsExternalExtensionUninstalled(extension_id);
[email protected]c3cfb012011-04-06 22:07:351009}
1010
1011bool ExtensionService::IsExternalExtensionUninstalled(
1012 const std::string& extension_id) const {
1013 return extension_prefs_->IsExternalExtensionUninstalled(extension_id);
1014}
1015
[email protected]eaa7dd182010-12-14 11:09:001016void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:121017 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:201018
[email protected]06f92562011-04-29 19:27:311019 if (IsExtensionEnabled(extension_id))
[email protected]0c6da502009-08-14 22:32:391020 return;
[email protected]0c6da502009-08-14 22:32:391021
[email protected]b3317ad2011-04-28 23:46:001022 extension_prefs_->SetExtensionState(extension_id, Extension::ENABLED);
[email protected]1784e83a2009-09-08 21:01:521023
[email protected]06f92562011-04-29 19:27:311024 const Extension* extension =
1025 GetExtensionByIdInternal(extension_id, false, true, false);
1026 // This can happen if sync enables an extension that is not
1027 // installed yet.
1028 if (!extension)
1029 return;
1030
[email protected]0c6da502009-08-14 22:32:391031 // Move it over to the enabled list.
[email protected]00cd9c42010-11-02 20:15:571032 extensions_.push_back(make_scoped_refptr(extension));
[email protected]0c6da502009-08-14 22:32:391033 ExtensionList::iterator iter = std::find(disabled_extensions_.begin(),
1034 disabled_extensions_.end(),
1035 extension);
1036 disabled_extensions_.erase(iter);
1037
[email protected]f681c782010-11-19 11:19:391038 // Make sure any browser action contained within it is not hidden.
1039 extension_prefs_->SetBrowserActionVisibility(extension, true);
1040
[email protected]62d30f42009-10-01 22:36:061041 NotifyExtensionLoaded(extension);
[email protected]3bdba0d2011-08-23 07:17:301042
1043 SyncExtensionChangeIfNeeded(*extension);
[email protected]0c6da502009-08-14 22:32:391044}
1045
[email protected]eaa7dd182010-12-14 11:09:001046void ExtensionService::DisableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:121047 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:201048
[email protected]b2ba9962009-12-10 20:10:151049 // The extension may have been disabled already.
[email protected]06f92562011-04-29 19:27:311050 if (!IsExtensionEnabled(extension_id))
[email protected]1784e83a2009-09-08 21:01:521051 return;
[email protected]1784e83a2009-09-08 21:01:521052
[email protected]06f92562011-04-29 19:27:311053 const Extension* extension = GetInstalledExtension(extension_id);
1054 // |extension| can be NULL if sync disables an extension that is not
1055 // installed yet.
1056 if (extension && !Extension::UserMayDisable(extension->location()))
[email protected]95da88c42011-03-31 10:07:331057 return;
1058
[email protected]b3317ad2011-04-28 23:46:001059 extension_prefs_->SetExtensionState(extension_id, Extension::DISABLED);
[email protected]1784e83a2009-09-08 21:01:521060
[email protected]06f92562011-04-29 19:27:311061 extension = GetExtensionByIdInternal(extension_id, true, false, true);
1062 if (!extension)
1063 return;
1064
[email protected]1784e83a2009-09-08 21:01:521065 // Move it over to the disabled list.
[email protected]00cd9c42010-11-02 20:15:571066 disabled_extensions_.push_back(make_scoped_refptr(extension));
[email protected]1784e83a2009-09-08 21:01:521067 ExtensionList::iterator iter = std::find(extensions_.begin(),
1068 extensions_.end(),
1069 extension);
[email protected]06f92562011-04-29 19:27:311070 if (iter != extensions_.end()) {
1071 extensions_.erase(iter);
1072 } else {
1073 iter = std::find(terminated_extensions_.begin(),
1074 terminated_extensions_.end(),
1075 extension);
1076 terminated_extensions_.erase(iter);
1077 }
[email protected]1784e83a2009-09-08 21:01:521078
[email protected]814a7bf0f2011-08-13 05:30:591079 NotifyExtensionUnloaded(extension, extension_misc::UNLOAD_REASON_DISABLE);
[email protected]3bdba0d2011-08-23 07:17:301080
1081 SyncExtensionChangeIfNeeded(*extension);
[email protected]7c1490da2011-10-11 18:53:251082
1083 // Deactivating one extension might have solved the problems of others.
1084 // Therefore, we clear warnings of this type for all extensions.
1085 std::set<ExtensionWarningSet::WarningType> warnings;
1086 extension_warnings_.GetWarningsAffectingExtension(extension_id, &warnings);
1087 extension_warnings_.ClearWarnings(warnings);
[email protected]1784e83a2009-09-08 21:01:521088}
1089
[email protected]eaa7dd182010-12-14 11:09:001090void ExtensionService::GrantPermissions(const Extension* extension) {
[email protected]8d888c12010-11-30 00:00:251091 CHECK(extension);
1092
[email protected]902fd7b2011-07-27 18:42:311093 // We only maintain the granted permissions prefs for extensions that can't
1094 // silently increase their permissions.
1095 if (extension->CanSilentlyIncreasePermissions())
1096 return;
[email protected]8d888c12010-11-30 00:00:251097
[email protected]8d888c12010-11-30 00:00:251098 extension_prefs_->AddGrantedPermissions(extension->id(),
[email protected]902fd7b2011-07-27 18:42:311099 extension->GetActivePermissions());
[email protected]8d888c12010-11-30 00:00:251100}
1101
[email protected]eaa7dd182010-12-14 11:09:001102void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]8d888c12010-11-30 00:00:251103 const Extension* extension) {
1104 CHECK(extension);
[email protected]fe2dd7742011-04-19 22:52:491105 RecordPermissionMessagesHistogram(
1106 extension, "Extensions.Permissions_ReEnable");
[email protected]8d888c12010-11-30 00:00:251107 GrantPermissions(extension);
1108 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
1109 EnableExtension(extension->id());
1110}
1111
[email protected]902fd7b2011-07-27 18:42:311112void ExtensionService::UpdateActivePermissions(
1113 const Extension* extension,
1114 const ExtensionPermissionSet* permissions) {
1115 extension_prefs()->SetActivePermissions(extension->id(), permissions);
1116 extension->SetActivePermissions(permissions);
1117}
1118
[email protected]eaa7dd182010-12-14 11:09:001119void ExtensionService::LoadExtension(const FilePath& extension_path) {
[email protected]71eb1e6d2011-08-02 22:57:401120 LoadExtension(extension_path, true);
1121}
1122
1123void ExtensionService::LoadExtension(const FilePath& extension_path,
1124 bool prompt_for_plugins) {
[email protected]e49002a2011-07-13 03:40:441125 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
[email protected]53612e82011-10-18 18:00:361126 base::Bind(&ExtensionServiceBackend::LoadSingleExtension, backend_.get(),
1127 extension_path, prompt_for_plugins));
[email protected]e49002a2011-07-13 03:40:441128}
1129
1130void ExtensionService::LoadExtensionFromCommandLine(
[email protected]67d2d802011-07-21 02:54:281131 const FilePath& path_in) {
1132
1133 // Load extensions from the command line synchronously to avoid a race
1134 // between extension loading and loading an URL from the command line.
1135 base::ThreadRestrictions::ScopedAllowIO allow_io;
1136
1137 FilePath extension_path = path_in;
1138 file_util::AbsolutePath(&extension_path);
1139
1140 std::string id = Extension::GenerateIdForPath(extension_path);
[email protected]cdfca9702011-08-08 16:07:011141 bool allow_file_access =
1142 Extension::ShouldAlwaysAllowFileAccess(Extension::LOAD);
1143 if (extension_prefs()->HasAllowFileAccessSetting(id))
1144 allow_file_access = extension_prefs()->AllowFileAccess(id);
[email protected]67d2d802011-07-21 02:54:281145
1146 int flags = Extension::NO_FLAGS;
[email protected]cdfca9702011-08-08 16:07:011147 if (allow_file_access)
[email protected]67d2d802011-07-21 02:54:281148 flags |= Extension::ALLOW_FILE_ACCESS;
1149 if (Extension::ShouldDoStrictErrorChecking(Extension::LOAD))
1150 flags |= Extension::STRICT_ERROR_CHECKS;
1151
1152 std::string error;
1153 scoped_refptr<const Extension> extension(extension_file_util::LoadExtension(
1154 extension_path,
1155 Extension::LOAD,
1156 flags,
1157 &error));
1158
1159 if (!extension) {
[email protected]655b2b1a2011-10-13 17:13:061160 ReportExtensionLoadError(extension_path, error, true);
[email protected]67d2d802011-07-21 02:54:281161 return;
1162 }
1163
1164 OnLoadSingleExtension(extension, false);
[email protected]9f1087e2009-06-15 17:29:321165}
1166
[email protected]eaa7dd182010-12-14 11:09:001167void ExtensionService::LoadComponentExtensions() {
[email protected]1952c7d2010-03-04 23:48:341168 for (RegisteredComponentExtensions::iterator it =
1169 component_extension_manifests_.begin();
1170 it != component_extension_manifests_.end(); ++it) {
[email protected]61b55b62011-03-24 09:03:101171 LoadComponentExtension(*it);
[email protected]1952c7d2010-03-04 23:48:341172 }
1173}
1174
[email protected]a964e112011-04-14 21:52:511175const Extension* ExtensionService::LoadComponentExtension(
[email protected]61b55b62011-03-24 09:03:101176 const ComponentExtensionInfo &info) {
1177 JSONStringValueSerializer serializer(info.manifest);
1178 scoped_ptr<Value> manifest(serializer.Deserialize(NULL, NULL));
1179 if (!manifest.get()) {
[email protected]0b05c262011-08-11 01:42:211180 LOG(ERROR) << "Failed to parse manifest for extension";
[email protected]a964e112011-04-14 21:52:511181 return NULL;
[email protected]61b55b62011-03-24 09:03:101182 }
1183
[email protected]83048a22011-03-29 00:14:131184 int flags = Extension::REQUIRE_KEY;
1185 if (Extension::ShouldDoStrictErrorChecking(Extension::COMPONENT))
1186 flags |= Extension::STRICT_ERROR_CHECKS;
[email protected]61b55b62011-03-24 09:03:101187 std::string error;
1188 scoped_refptr<const Extension> extension(Extension::Create(
1189 info.root_directory,
1190 Extension::COMPONENT,
1191 *static_cast<DictionaryValue*>(manifest.get()),
[email protected]83048a22011-03-29 00:14:131192 flags,
[email protected]61b55b62011-03-24 09:03:101193 &error));
1194 if (!extension.get()) {
[email protected]0b05c262011-08-11 01:42:211195 LOG(ERROR) << error;
[email protected]a964e112011-04-14 21:52:511196 return NULL;
[email protected]61b55b62011-03-24 09:03:101197 }
1198 AddExtension(extension);
[email protected]a964e112011-04-14 21:52:511199 return extension;
[email protected]61b55b62011-03-24 09:03:101200}
1201
[email protected]ad63a792011-06-16 06:41:111202void ExtensionService::UnloadComponentExtension(
1203 const ComponentExtensionInfo& info) {
1204 JSONStringValueSerializer serializer(info.manifest);
1205 scoped_ptr<Value> manifest(serializer.Deserialize(NULL, NULL));
1206 if (!manifest.get()) {
[email protected]0b05c262011-08-11 01:42:211207 LOG(ERROR) << "Failed to parse manifest for extension";
[email protected]ad63a792011-06-16 06:41:111208 return;
1209 }
1210 std::string public_key;
1211 std::string public_key_bytes;
1212 std::string id;
1213 if (!static_cast<DictionaryValue*>(manifest.get())->
1214 GetString(extension_manifest_keys::kPublicKey, &public_key) ||
1215 !Extension::ParsePEMKeyBytes(public_key, &public_key_bytes) ||
1216 !Extension::GenerateId(public_key_bytes, &id)) {
[email protected]0b05c262011-08-11 01:42:211217 LOG(ERROR) << "Failed to get extension id";
[email protected]ad63a792011-06-16 06:41:111218 return;
1219 }
[email protected]814a7bf0f2011-08-13 05:30:591220 UnloadExtension(id, extension_misc::UNLOAD_REASON_DISABLE);
[email protected]ad63a792011-06-16 06:41:111221}
1222
[email protected]eaa7dd182010-12-14 11:09:001223void ExtensionService::LoadAllExtensions() {
[email protected]a29a517a2011-01-21 21:11:121224 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a315ba92010-11-16 14:12:211225
[email protected]cc2c3432009-11-06 17:24:361226 base::TimeTicks start_time = base::TimeTicks::Now();
1227
[email protected]1952c7d2010-03-04 23:48:341228 // Load any component extensions.
1229 LoadComponentExtensions();
1230
[email protected]e72e8eb82009-06-18 17:21:511231 // Load the previously installed extensions.
[email protected]a315ba92010-11-16 14:12:211232 scoped_ptr<ExtensionPrefs::ExtensionsInfo> extensions_info(
[email protected]e6090e42010-03-23 22:44:081233 extension_prefs_->GetInstalledExtensionsInfo());
[email protected]c6d474f82009-12-16 21:11:061234
[email protected]a315ba92010-11-16 14:12:211235 std::vector<int> reload_reason_counts(NUM_MANIFEST_RELOAD_REASONS, 0);
1236 bool should_write_prefs = false;
1237
1238 for (size_t i = 0; i < extensions_info->size(); ++i) {
1239 ExtensionInfo* info = extensions_info->at(i).get();
1240
1241 ManifestReloadReason reload_reason = ShouldReloadExtensionManifest(*info);
1242 ++reload_reason_counts[reload_reason];
1243 UMA_HISTOGRAM_ENUMERATION("Extensions.ManifestReloadEnumValue",
1244 reload_reason, 100);
1245
1246 if (reload_reason != NOT_NEEDED) {
1247 // Reloading and extension reads files from disk. We do this on the
1248 // UI thread because reloads should be very rare, and the complexity
1249 // added by delaying the time when the extensions service knows about
1250 // all extensions is significant. See crbug.com/37548 for details.
1251 // |allow_io| disables tests that file operations run on the file
1252 // thread.
1253 base::ThreadRestrictions::ScopedAllowIO allow_io;
1254
1255 std::string error;
1256 scoped_refptr<const Extension> extension(
1257 extension_file_util::LoadExtension(
[email protected]542258c2011-03-04 21:25:311258 info->extension_path,
1259 info->extension_location,
[email protected]2af666c2011-10-16 16:23:191260 GetExtensionCreateFlagsForInstalledExtension(info),
[email protected]542258c2011-03-04 21:25:311261 &error));
[email protected]a315ba92010-11-16 14:12:211262
1263 if (extension.get()) {
1264 extensions_info->at(i)->extension_manifest.reset(
1265 static_cast<DictionaryValue*>(
1266 extension->manifest_value()->DeepCopy()));
1267 should_write_prefs = true;
1268 }
[email protected]c6d474f82009-12-16 21:11:061269 }
1270 }
1271
[email protected]a315ba92010-11-16 14:12:211272 for (size_t i = 0; i < extensions_info->size(); ++i) {
1273 LoadInstalledExtension(*extensions_info->at(i), should_write_prefs);
[email protected]c6d474f82009-12-16 21:11:061274 }
1275
[email protected]ae09ca62009-08-21 19:46:461276 OnLoadedInstalledExtensions();
[email protected]cc2c3432009-11-06 17:24:361277
[email protected]a315ba92010-11-16 14:12:211278 // The histograms Extensions.ManifestReload* allow us to validate
1279 // the assumption that reloading manifest is a rare event.
1280 UMA_HISTOGRAM_COUNTS_100("Extensions.ManifestReloadNotNeeded",
1281 reload_reason_counts[NOT_NEEDED]);
1282 UMA_HISTOGRAM_COUNTS_100("Extensions.ManifestReloadUnpackedDir",
1283 reload_reason_counts[UNPACKED_DIR]);
1284 UMA_HISTOGRAM_COUNTS_100("Extensions.ManifestReloadNeedsRelocalization",
1285 reload_reason_counts[NEEDS_RELOCALIZATION]);
1286
[email protected]cc2c3432009-11-06 17:24:361287 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadAll", extensions_.size());
1288 UMA_HISTOGRAM_COUNTS_100("Extensions.Disabled", disabled_extensions_.size());
1289
[email protected]1952c7d2010-03-04 23:48:341290 UMA_HISTOGRAM_TIMES("Extensions.LoadAllTime",
1291 base::TimeTicks::Now() - start_time);
[email protected]cc2c3432009-11-06 17:24:361292
[email protected]d101c4a2011-07-22 22:51:361293 int app_user_count = 0;
1294 int app_external_count = 0;
[email protected]9b217652010-10-08 22:04:231295 int hosted_app_count = 0;
1296 int packaged_app_count = 0;
[email protected]1952c7d2010-03-04 23:48:341297 int user_script_count = 0;
[email protected]d101c4a2011-07-22 22:51:361298 int extension_user_count = 0;
1299 int extension_external_count = 0;
[email protected]1952c7d2010-03-04 23:48:341300 int theme_count = 0;
[email protected]1952c7d2010-03-04 23:48:341301 int page_action_count = 0;
1302 int browser_action_count = 0;
1303 ExtensionList::iterator ex;
1304 for (ex = extensions_.begin(); ex != extensions_.end(); ++ex) {
[email protected]9b217652010-10-08 22:04:231305 Extension::Location location = (*ex)->location();
[email protected]7fa19f82010-12-21 19:40:081306 Extension::Type type = (*ex)->GetType();
[email protected]9b217652010-10-08 22:04:231307 if ((*ex)->is_app()) {
1308 UMA_HISTOGRAM_ENUMERATION("Extensions.AppLocation",
1309 location, 100);
1310 } else if (type == Extension::TYPE_EXTENSION) {
1311 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionLocation",
1312 location, 100);
1313 }
1314
[email protected]1952c7d2010-03-04 23:48:341315 // Don't count component extensions, since they are only extensions as an
1316 // implementation detail.
[email protected]9b217652010-10-08 22:04:231317 if (location == Extension::COMPONENT)
[email protected]1952c7d2010-03-04 23:48:341318 continue;
1319
[email protected]e8c729a2010-03-09 19:55:191320 // Don't count unpacked extensions, since they're a developer-specific
1321 // feature.
[email protected]9b217652010-10-08 22:04:231322 if (location == Extension::LOAD)
[email protected]e8c729a2010-03-09 19:55:191323 continue;
1324
[email protected]9b217652010-10-08 22:04:231325 // Using an enumeration shows us the total installed ratio across all users.
1326 // Using the totals per user at each startup tells us the distribution of
1327 // usage for each user (e.g. 40% of users have at least one app installed).
1328 UMA_HISTOGRAM_ENUMERATION("Extensions.LoadType", type, 100);
1329 switch (type) {
1330 case Extension::TYPE_THEME:
[email protected]a315ba92010-11-16 14:12:211331 ++theme_count;
[email protected]9b217652010-10-08 22:04:231332 break;
1333 case Extension::TYPE_USER_SCRIPT:
[email protected]a315ba92010-11-16 14:12:211334 ++user_script_count;
[email protected]9b217652010-10-08 22:04:231335 break;
1336 case Extension::TYPE_HOSTED_APP:
[email protected]a315ba92010-11-16 14:12:211337 ++hosted_app_count;
[email protected]d101c4a2011-07-22 22:51:361338 if (Extension::IsExternalLocation(location)) {
1339 ++app_external_count;
1340 } else {
1341 ++app_user_count;
1342 }
[email protected]9b217652010-10-08 22:04:231343 break;
1344 case Extension::TYPE_PACKAGED_APP:
[email protected]a315ba92010-11-16 14:12:211345 ++packaged_app_count;
[email protected]d101c4a2011-07-22 22:51:361346 if (Extension::IsExternalLocation(location)) {
1347 ++app_external_count;
1348 } else {
1349 ++app_user_count;
1350 }
[email protected]9b217652010-10-08 22:04:231351 break;
1352 case Extension::TYPE_EXTENSION:
1353 default:
[email protected]d101c4a2011-07-22 22:51:361354 if (Extension::IsExternalLocation(location)) {
1355 ++extension_external_count;
1356 } else {
1357 ++extension_user_count;
1358 }
[email protected]9b217652010-10-08 22:04:231359 break;
[email protected]cc2c3432009-11-06 17:24:361360 }
[email protected]9b217652010-10-08 22:04:231361 if ((*ex)->page_action() != NULL)
[email protected]a315ba92010-11-16 14:12:211362 ++page_action_count;
[email protected]9b217652010-10-08 22:04:231363 if ((*ex)->browser_action() != NULL)
[email protected]a315ba92010-11-16 14:12:211364 ++browser_action_count;
[email protected]fe2dd7742011-04-19 22:52:491365
1366 RecordPermissionMessagesHistogram(
1367 ex->get(), "Extensions.Permissions_Load");
[email protected]cc2c3432009-11-06 17:24:361368 }
[email protected]d101c4a2011-07-22 22:51:361369 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadApp",
1370 app_user_count + app_external_count);
1371 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadAppUser", app_user_count);
1372 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadAppExternal", app_external_count);
[email protected]9b217652010-10-08 22:04:231373 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadHostedApp", hosted_app_count);
1374 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadPackagedApp", packaged_app_count);
[email protected]d101c4a2011-07-22 22:51:361375 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadExtension",
1376 extension_user_count + extension_external_count);
1377 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadExtensionUser",
1378 extension_user_count);
1379 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadExtensionExternal",
1380 extension_external_count);
[email protected]1952c7d2010-03-04 23:48:341381 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadUserScript", user_script_count);
1382 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadTheme", theme_count);
[email protected]1952c7d2010-03-04 23:48:341383 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadPageAction", page_action_count);
1384 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadBrowserAction",
1385 browser_action_count);
[email protected]ae09ca62009-08-21 19:46:461386}
1387
[email protected]fe2dd7742011-04-19 22:52:491388// static
1389void ExtensionService::RecordPermissionMessagesHistogram(
1390 const Extension* e, const char* histogram) {
1391 // Since this is called from multiple sources, and since the Histogram macros
1392 // use statics, we need to manually lookup the Histogram ourselves.
1393 base::Histogram* counter = base::LinearHistogram::FactoryGet(
1394 histogram,
1395 1,
[email protected]0d3e4a22011-06-23 19:02:521396 ExtensionPermissionMessage::kEnumBoundary,
1397 ExtensionPermissionMessage::kEnumBoundary + 1,
[email protected]fe2dd7742011-04-19 22:52:491398 base::Histogram::kUmaTargetedHistogramFlag);
1399
[email protected]0d3e4a22011-06-23 19:02:521400 ExtensionPermissionMessages permissions = e->GetPermissionMessages();
[email protected]fe2dd7742011-04-19 22:52:491401 if (permissions.empty()) {
[email protected]0d3e4a22011-06-23 19:02:521402 counter->Add(ExtensionPermissionMessage::kNone);
[email protected]fe2dd7742011-04-19 22:52:491403 } else {
[email protected]0d3e4a22011-06-23 19:02:521404 for (ExtensionPermissionMessages::iterator it = permissions.begin();
1405 it != permissions.end(); ++it)
1406 counter->Add(it->id());
[email protected]fe2dd7742011-04-19 22:52:491407 }
1408}
1409
[email protected]eaa7dd182010-12-14 11:09:001410void ExtensionService::LoadInstalledExtension(const ExtensionInfo& info,
[email protected]542258c2011-03-04 21:25:311411 bool write_to_prefs) {
[email protected]ae09ca62009-08-21 19:46:461412 std::string error;
[email protected]9adb9692010-10-29 23:14:021413 scoped_refptr<const Extension> extension(NULL);
[email protected]306a2bd2010-08-11 14:56:361414 if (!extension_prefs_->IsExtensionAllowedByPolicy(info.extension_id)) {
1415 error = errors::kDisabledByPolicy;
1416 } else if (info.extension_manifest.get()) {
[email protected]66e4eb32010-10-27 20:37:411417 extension = Extension::Create(
[email protected]542258c2011-03-04 21:25:311418 info.extension_path,
1419 info.extension_location,
1420 *info.extension_manifest,
[email protected]2af666c2011-10-16 16:23:191421 GetExtensionCreateFlagsForInstalledExtension(&info),
[email protected]542258c2011-03-04 21:25:311422 &error);
[email protected]ae09ca62009-08-21 19:46:461423 } else {
[email protected]c6d474f82009-12-16 21:11:061424 error = errors::kManifestUnreadable;
[email protected]ae09ca62009-08-21 19:46:461425 }
1426
[email protected]199cc8bc72011-09-27 16:22:341427 // Once installed, non-unpacked extensions cannot change their IDs (e.g., by
1428 // updating the 'key' field in their manifest).
1429 if (extension &&
1430 extension->location() != Extension::LOAD &&
1431 info.extension_id != extension->id()) {
1432 error = errors::kCannotChangeExtensionID;
1433 extension = NULL;
1434 UserMetrics::RecordAction(UserMetricsAction("Extensions.IDChangedError"));
1435 }
1436
[email protected]ae09ca62009-08-21 19:46:461437 if (!extension) {
[email protected]655b2b1a2011-10-13 17:13:061438 ReportExtensionLoadError(info.extension_path, error, false);
[email protected]ae09ca62009-08-21 19:46:461439 return;
1440 }
1441
[email protected]c6d474f82009-12-16 21:11:061442 if (write_to_prefs)
1443 extension_prefs_->UpdateManifest(extension);
1444
[email protected]49cd1572011-02-08 21:38:451445 AddExtension(extension);
[email protected]9f1087e2009-06-15 17:29:321446}
1447
[email protected]2af666c2011-10-16 16:23:191448int ExtensionService::GetExtensionCreateFlagsForInstalledExtension(
1449 const ExtensionInfo* info) {
1450 int flags = Extension::NO_FLAGS;
1451 if (info->extension_location != Extension::LOAD)
1452 flags |= Extension::REQUIRE_KEY;
1453 if (Extension::ShouldDoStrictErrorChecking(info->extension_location))
1454 flags |= Extension::STRICT_ERROR_CHECKS;
1455 if (extension_prefs_->AllowFileAccess(info->extension_id))
1456 flags |= Extension::ALLOW_FILE_ACCESS;
1457 if (extension_prefs_->IsFromWebStore(info->extension_id))
1458 flags |= Extension::FROM_WEBSTORE;
1459 if (extension_prefs_->IsFromBookmark(info->extension_id))
1460 flags |= Extension::FROM_BOOKMARK;
1461 return flags;
1462}
1463
[email protected]eaa7dd182010-12-14 11:09:001464void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]57a777f72010-03-31 01:09:421465 // The ChromeURLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:061466 // was loaded, otherwise a race can arise where a renderer that is created
1467 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:421468 // that the request context doesn't yet know about. The profile is responsible
1469 // for ensuring its URLRequestContexts appropriately discover the loaded
1470 // extension.
[email protected]c8d407e2011-04-28 21:27:171471 profile_->RegisterExtensionWithRequestContexts(extension);
[email protected]62d30f42009-10-01 22:36:061472
[email protected]c8d407e2011-04-28 21:27:171473 // Tell subsystems that use the EXTENSION_LOADED notification about the new
1474 // extension.
[email protected]ad50def52011-10-19 23:17:071475 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271476 chrome::NOTIFICATION_EXTENSION_LOADED,
[email protected]6c2381d2011-10-19 02:52:531477 content::Source<Profile>(profile_),
1478 content::Details<const Extension>(extension));
[email protected]2de307592011-04-05 21:16:581479
[email protected]c8d407e2011-04-28 21:27:171480 // Tell renderers about the new extension.
[email protected]77a6970c2011-04-23 16:58:561481 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator());
1482 !i.IsAtEnd(); i.Advance()) {
[email protected]c8d407e2011-04-28 21:27:171483 RenderProcessHost* host = i.GetCurrentValue();
[email protected]9b62ecf2011-07-27 20:23:081484 Profile* host_profile =
1485 Profile::FromBrowserContext(host->browser_context());
1486 if (host_profile->GetOriginalProfile() == profile_->GetOriginalProfile()) {
[email protected]c8d407e2011-04-28 21:27:171487 host->Send(
[email protected]15379642011-08-11 22:22:051488 new ExtensionMsg_Loaded(ExtensionMsg_Loaded_Params(extension)));
[email protected]c8d407e2011-04-28 21:27:171489 }
[email protected]77a6970c2011-04-23 16:58:561490 }
1491
[email protected]c8d407e2011-04-28 21:27:171492 // Tell a random-ass collection of other subsystems about the new extension.
1493 // TODO(aa): What should we do with all this goop? Can it move into the
1494 // relevant objects via EXTENSION_LOADED?
1495
1496 profile_->GetExtensionSpecialStoragePolicy()->
1497 GrantRightsForExtension(extension);
1498
1499 UpdateActiveExtensionsInCrashReporter();
1500
1501 ExtensionWebUI::RegisterChromeURLOverrides(
1502 profile_, extension->GetChromeURLOverrides());
1503
[email protected]8e5c89a2011-06-07 18:13:331504 TemplateURLService* url_service =
1505 TemplateURLServiceFactory::GetForProfile(profile_);
1506 if (url_service)
1507 url_service->RegisterExtensionKeyword(extension);
[email protected]c8d407e2011-04-28 21:27:171508
1509 // Load the icon for omnibox-enabled extensions so it will be ready to display
1510 // in the URL bar.
1511 if (!extension->omnibox_keyword().empty()) {
1512 omnibox_popup_icon_manager_.LoadIcon(extension);
1513 omnibox_icon_manager_.LoadIcon(extension);
1514 }
1515
1516 // If the extension has permission to load chrome://favicon/ resources we need
1517 // to make sure that the FaviconSource is registered with the
1518 // ChromeURLDataManager.
1519 if (extension->HasHostPermission(GURL(chrome::kChromeUIFaviconURL))) {
1520 FaviconSource* favicon_source = new FaviconSource(profile_,
1521 FaviconSource::FAVICON);
1522 profile_->GetChromeURLDataManager()->AddDataSource(favicon_source);
1523 }
1524
1525 // TODO(mpcomplete): This ends up affecting all profiles. See crbug.com/80757.
[email protected]2de307592011-04-05 21:16:581526 bool plugins_changed = false;
1527 for (size_t i = 0; i < extension->plugins().size(); ++i) {
1528 const Extension::PluginInfo& plugin = extension->plugins()[i];
1529 webkit::npapi::PluginList::Singleton()->RefreshPlugins();
1530 webkit::npapi::PluginList::Singleton()->AddExtraPluginPath(plugin.path);
1531 plugins_changed = true;
[email protected]476a27032011-09-13 13:40:491532 ChromePluginServiceFilter* filter =
1533 ChromePluginServiceFilter::GetInstance();
1534 if (plugin.is_public) {
1535 filter->RestrictPluginToProfileAndOrigin(
1536 plugin.path, profile_, GURL());
1537 } else {
1538 filter->RestrictPluginToProfileAndOrigin(
1539 plugin.path, profile_, extension->url());
[email protected]2de307592011-04-05 21:16:581540 }
1541 }
[email protected]84396dbc2011-04-14 06:33:421542
1543 bool nacl_modules_changed = false;
1544 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1545 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
[email protected]cebc3dc2011-04-18 17:15:001546 RegisterNaClModule(module.url, module.mime_type);
[email protected]84396dbc2011-04-14 06:33:421547 nacl_modules_changed = true;
1548 }
1549
[email protected]ed0ba002011-05-26 16:55:131550 if (nacl_modules_changed)
1551 UpdatePluginListWithNaClModules();
[email protected]84396dbc2011-04-14 06:33:421552
1553 if (plugins_changed || nacl_modules_changed)
[email protected]45a22e62011-10-12 09:48:021554 PluginService::GetInstance()->PurgePluginListCache(profile_, false);
[email protected]b4d9d122011-06-17 01:58:541555
[email protected]14da67b02011-08-02 05:49:131556#if defined(OS_CHROMEOS)
1557#if defined(TOUCH_UI)
[email protected]c8d4e9462011-06-29 23:18:571558 chromeos::input_method::InputMethodManager* input_method_manager =
1559 chromeos::input_method::InputMethodManager::GetInstance();
[email protected]14da67b02011-08-02 05:49:131560#endif
[email protected]b4d9d122011-06-17 01:58:541561 for (std::vector<Extension::InputComponentInfo>::const_iterator component =
1562 extension->input_components().begin();
1563 component != extension->input_components().end();
1564 ++component) {
[email protected]14da67b02011-08-02 05:49:131565 if (component->type == Extension::INPUT_COMPONENT_TYPE_IME) {
1566 ExtensionInputImeEventRouter::GetInstance()->RegisterIme(
1567 profile_, extension->id(), *component);
1568 }
1569#if defined(TOUCH_UI)
[email protected]b4d9d122011-06-17 01:58:541570 if (component->type == Extension::INPUT_COMPONENT_TYPE_VIRTUAL_KEYBOARD &&
1571 !component->layouts.empty()) {
1572 const bool is_system =
1573 !Extension::IsExternalLocation(extension->location());
[email protected]cbd4c042011-08-13 18:22:191574 input_method_manager->RegisterVirtualKeyboard(
1575 extension->url(),
1576 component->name, // human-readable name of the keyboard extension.
1577 component->layouts,
1578 is_system);
[email protected]b4d9d122011-06-17 01:58:541579 }
[email protected]14da67b02011-08-02 05:49:131580#endif
[email protected]b4d9d122011-06-17 01:58:541581 }
1582#endif
[email protected]62d30f42009-10-01 22:36:061583}
1584
[email protected]a9f39a312010-12-23 22:14:271585void ExtensionService::NotifyExtensionUnloaded(
[email protected]814a7bf0f2011-08-13 05:30:591586 const Extension* extension,
1587 extension_misc::UnloadedExtensionReason reason) {
[email protected]a9f39a312010-12-23 22:14:271588 UnloadedExtensionInfo details(extension, reason);
[email protected]ad50def52011-10-19 23:17:071589 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271590 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]6c2381d2011-10-19 02:52:531591 content::Source<Profile>(profile_),
1592 content::Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061593
[email protected]77a6970c2011-04-23 16:58:561594 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator());
1595 !i.IsAtEnd(); i.Advance()) {
[email protected]c8d407e2011-04-28 21:27:171596 RenderProcessHost* host = i.GetCurrentValue();
[email protected]9b62ecf2011-07-27 20:23:081597 Profile* host_profile =
1598 Profile::FromBrowserContext(host->browser_context());
1599 if (host_profile->GetOriginalProfile() == profile_->GetOriginalProfile())
[email protected]c8d407e2011-04-28 21:27:171600 host->Send(new ExtensionMsg_Unloaded(extension->id()));
[email protected]77a6970c2011-04-23 16:58:561601 }
1602
[email protected]dd163fb02011-05-04 22:22:171603 profile_->UnregisterExtensionWithRequestContexts(extension->id(), reason);
[email protected]c8d407e2011-04-28 21:27:171604 profile_->GetExtensionSpecialStoragePolicy()->
1605 RevokeRightsForExtension(extension);
1606
1607 ExtensionWebUI::UnregisterChromeURLOverrides(
1608 profile_, extension->GetChromeURLOverrides());
1609
[email protected]b777b332011-04-16 04:01:081610#if defined(OS_CHROMEOS)
1611 // Revoke external file access to
[email protected]c8d407e2011-04-28 21:27:171612 if (profile_->GetFileSystemContext() &&
1613 profile_->GetFileSystemContext()->path_manager() &&
1614 profile_->GetFileSystemContext()->path_manager()->external_provider()) {
1615 profile_->GetFileSystemContext()->path_manager()->external_provider()->
1616 RevokeAccessForExtension(extension->id());
[email protected]62d30f42009-10-01 22:36:061617 }
[email protected]c8d407e2011-04-28 21:27:171618#endif
1619
1620 UpdateActiveExtensionsInCrashReporter();
[email protected]2de307592011-04-05 21:16:581621
1622 bool plugins_changed = false;
1623 for (size_t i = 0; i < extension->plugins().size(); ++i) {
1624 const Extension::PluginInfo& plugin = extension->plugins()[i];
[email protected]14908b72011-04-20 06:54:361625 if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
[email protected]53612e82011-10-18 18:00:361626 base::Bind(&ForceShutdownPlugin, plugin.path)))
[email protected]14908b72011-04-20 06:54:361627 NOTREACHED();
[email protected]2de307592011-04-05 21:16:581628 webkit::npapi::PluginList::Singleton()->RefreshPlugins();
1629 webkit::npapi::PluginList::Singleton()->RemoveExtraPluginPath(
1630 plugin.path);
1631 plugins_changed = true;
[email protected]476a27032011-09-13 13:40:491632 ChromePluginServiceFilter::GetInstance()->UnrestrictPlugin(plugin.path);
[email protected]2de307592011-04-05 21:16:581633 }
[email protected]84396dbc2011-04-14 06:33:421634
1635 bool nacl_modules_changed = false;
1636 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1637 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
[email protected]cebc3dc2011-04-18 17:15:001638 UnregisterNaClModule(module.url);
[email protected]84396dbc2011-04-14 06:33:421639 nacl_modules_changed = true;
1640 }
1641
[email protected]ed0ba002011-05-26 16:55:131642 if (nacl_modules_changed)
1643 UpdatePluginListWithNaClModules();
[email protected]84396dbc2011-04-14 06:33:421644
1645 if (plugins_changed || nacl_modules_changed)
[email protected]45a22e62011-10-12 09:48:021646 PluginService::GetInstance()->PurgePluginListCache(profile_, false);
[email protected]62d30f42009-10-01 22:36:061647}
1648
[email protected]eaa7dd182010-12-14 11:09:001649void ExtensionService::UpdateExtensionBlacklist(
[email protected]6b75ec32009-08-14 06:37:181650 const std::vector<std::string>& blacklist) {
1651 // Use this set to indicate if an extension in the blacklist has been used.
1652 std::set<std::string> blacklist_set;
1653 for (unsigned int i = 0; i < blacklist.size(); ++i) {
1654 if (Extension::IdIsValid(blacklist[i])) {
1655 blacklist_set.insert(blacklist[i]);
1656 }
1657 }
1658 extension_prefs_->UpdateBlacklist(blacklist_set);
1659 std::vector<std::string> to_be_removed;
1660 // Loop current extensions, unload installed extensions.
1661 for (ExtensionList::const_iterator iter = extensions_.begin();
1662 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021663 const Extension* extension = (*iter);
[email protected]6b75ec32009-08-14 06:37:181664 if (blacklist_set.find(extension->id()) != blacklist_set.end()) {
1665 to_be_removed.push_back(extension->id());
1666 }
1667 }
1668
1669 // UnloadExtension will change the extensions_ list. So, we should
1670 // call it outside the iterator loop.
1671 for (unsigned int i = 0; i < to_be_removed.size(); ++i) {
[email protected]814a7bf0f2011-08-13 05:30:591672 UnloadExtension(to_be_removed[i], extension_misc::UNLOAD_REASON_DISABLE);
[email protected]6b75ec32009-08-14 06:37:181673 }
1674}
1675
[email protected]0a071a32011-02-08 00:18:241676Profile* ExtensionService::profile() {
1677 return profile_;
1678}
1679
[email protected]eaa7dd182010-12-14 11:09:001680ExtensionPrefs* ExtensionService::extension_prefs() {
[email protected]ddb1e5a2010-12-13 20:10:451681 return extension_prefs_;
1682}
1683
[email protected]fc84e0e2011-09-27 19:18:221684ExtensionSettingsFrontend* ExtensionService::extension_settings_frontend() {
1685 return &extension_settings_frontend_;
[email protected]b7f853e282011-08-10 09:24:201686}
1687
[email protected]b790b072011-05-20 09:46:441688ExtensionContentSettingsStore*
1689 ExtensionService::GetExtensionContentSettingsStore() {
1690 return extension_prefs()->content_settings_store();
1691}
1692
[email protected]2859946f2011-04-04 18:18:061693ExtensionUpdater* ExtensionService::updater() {
1694 return updater_.get();
1695}
1696
[email protected]eaa7dd182010-12-14 11:09:001697void ExtensionService::CheckAdminBlacklist() {
[email protected]aa96d3a2010-08-21 08:45:251698 std::vector<std::string> to_be_removed;
1699 // Loop through extensions list, unload installed extensions.
1700 for (ExtensionList::const_iterator iter = extensions_.begin();
1701 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021702 const Extension* extension = (*iter);
[email protected]aa96d3a2010-08-21 08:45:251703 if (!extension_prefs_->IsExtensionAllowedByPolicy(extension->id()))
1704 to_be_removed.push_back(extension->id());
1705 }
1706
1707 // UnloadExtension will change the extensions_ list. So, we should
1708 // call it outside the iterator loop.
1709 for (unsigned int i = 0; i < to_be_removed.size(); ++i)
[email protected]814a7bf0f2011-08-13 05:30:591710 UnloadExtension(to_be_removed[i], extension_misc::UNLOAD_REASON_DISABLE);
[email protected]aa96d3a2010-08-21 08:45:251711}
1712
[email protected]31206602011-04-13 23:07:321713void ExtensionService::CheckForUpdatesSoon() {
[email protected]c3cfb012011-04-06 22:07:351714 if (updater()) {
[email protected]31206602011-04-13 23:07:321715 updater()->CheckSoon();
[email protected]c3cfb012011-04-06 22:07:351716 } else {
[email protected]31206602011-04-13 23:07:321717 LOG(WARNING) << "CheckForUpdatesSoon() called with auto-update turned off";
[email protected]c3cfb012011-04-06 22:07:351718 }
1719}
1720
[email protected]3bdba0d2011-08-23 07:17:301721namespace {
1722 bool IsSyncableNone(const Extension& extension) { return false; }
1723} // namespace
1724
1725ExtensionService::SyncBundle::SyncBundle()
1726 : filter(IsSyncableNone),
1727 sync_processor(NULL) {
[email protected]b05fb9ff2011-04-23 00:07:561728}
1729
[email protected]3bdba0d2011-08-23 07:17:301730ExtensionService::SyncBundle::~SyncBundle() {
1731}
1732
1733bool ExtensionService::SyncBundle::HasExtensionId(const std::string& id) const {
1734 return synced_extensions.find(id) != synced_extensions.end();
1735}
1736
1737bool ExtensionService::SyncBundle::HasPendingExtensionId(const std::string& id)
1738 const {
1739 return pending_sync_data.find(id) != pending_sync_data.end();
1740}
1741
1742void ExtensionService::SyncExtensionChangeIfNeeded(const Extension& extension) {
1743 SyncBundle* sync_bundle = GetSyncBundleForExtension(extension);
1744 if (sync_bundle) {
1745 ExtensionSyncData extension_sync_data(extension,
1746 IsExtensionEnabled(extension.id()),
1747 IsIncognitoEnabled(extension.id()));
1748
1749 SyncChangeList sync_change_list(1, extension_sync_data.GetSyncChange(
1750 sync_bundle->HasExtensionId(extension.id()) ?
1751 SyncChange::ACTION_UPDATE : SyncChange::ACTION_ADD));
1752 sync_bundle->sync_processor->ProcessSyncChanges(
1753 FROM_HERE, sync_change_list);
1754 sync_bundle->synced_extensions.insert(extension.id());
1755 sync_bundle->pending_sync_data.erase(extension.id());
[email protected]b05fb9ff2011-04-23 00:07:561756 }
[email protected]3bdba0d2011-08-23 07:17:301757}
1758
1759ExtensionService::SyncBundle* ExtensionService::GetSyncBundleForExtension(
1760 const Extension& extension) {
1761 if (app_sync_bundle_.filter(extension))
1762 return &app_sync_bundle_;
1763 else if (extension_sync_bundle_.filter(extension))
1764 return &extension_sync_bundle_;
1765 else
1766 return NULL;
1767}
1768
1769ExtensionService::SyncBundle*
1770 ExtensionService::GetSyncBundleForExtensionSyncData(
1771 const ExtensionSyncData& extension_sync_data) {
1772 switch (extension_sync_data.type()) {
1773 case Extension::SYNC_TYPE_APP:
1774 return &app_sync_bundle_;
1775 case Extension::SYNC_TYPE_EXTENSION:
1776 return &extension_sync_bundle_;
1777 default:
1778 NOTREACHED();
1779 return NULL;
1780 }
1781}
1782
1783#define GET_SYNC_BUNDLE_FOR_MODEL_TYPE_BODY() \
1784 do { \
1785 switch (type) { \
1786 case syncable::APPS: \
1787 return &app_sync_bundle_; \
1788 case syncable::EXTENSIONS: \
1789 return &extension_sync_bundle_; \
1790 default: \
1791 NOTREACHED(); \
1792 return NULL; \
1793 } \
1794 } while (0)
1795
1796const ExtensionService::SyncBundle*
1797 ExtensionService::GetSyncBundleForModelTypeConst(
1798 syncable::ModelType type) const {
1799 GET_SYNC_BUNDLE_FOR_MODEL_TYPE_BODY();
1800}
1801
1802ExtensionService::SyncBundle* ExtensionService::GetSyncBundleForModelType(
1803 syncable::ModelType type) {
1804 GET_SYNC_BUNDLE_FOR_MODEL_TYPE_BODY();
1805}
1806
1807#undef GET_SYNC_BUNDLE_FOR_MODEL_TYPE_BODY
1808
1809SyncError ExtensionService::MergeDataAndStartSyncing(
1810 syncable::ModelType type,
1811 const SyncDataList& initial_sync_data,
1812 SyncChangeProcessor* sync_processor) {
1813 CHECK(sync_processor);
1814
1815 SyncBundle* bundle = NULL;
1816
1817 switch (type) {
1818 case syncable::EXTENSIONS:
1819 bundle = &extension_sync_bundle_;
1820 bundle->filter = IsSyncableExtension;
1821 break;
1822
1823 case syncable::APPS:
1824 bundle = &app_sync_bundle_;
1825 bundle->filter = IsSyncableApp;
1826 break;
1827
1828 default:
1829 LOG(FATAL) << "Got " << type << " ModelType";
1830 }
1831
1832 bundle->sync_processor = sync_processor;
1833
1834 for (SyncDataList::const_iterator i = initial_sync_data.begin();
1835 i != initial_sync_data.end();
1836 ++i) {
1837 ExtensionSyncData extension_sync_data = ExtensionSyncData(*i);
1838 bundle->synced_extensions.insert(extension_sync_data.id());
1839 ProcessExtensionSyncData(extension_sync_data, *bundle);
1840 }
1841
1842 SyncDataList sync_data_list = GetAllSyncData(type);
1843 SyncChangeList sync_change_list;
1844 for (SyncDataList::const_iterator i = sync_data_list.begin();
1845 i != sync_data_list.end();
1846 ++i) {
1847 if (bundle->HasExtensionId(i->GetTag()))
1848 sync_change_list.push_back(SyncChange(SyncChange::ACTION_UPDATE, *i));
1849 else
1850 sync_change_list.push_back(SyncChange(SyncChange::ACTION_ADD, *i));
1851 }
1852 bundle->sync_processor->ProcessSyncChanges(FROM_HERE, sync_change_list);
1853
1854 return SyncError();
1855}
1856
1857void ExtensionService::StopSyncing(syncable::ModelType type) {
1858 SyncBundle* bundle = GetSyncBundleForModelType(type);
1859 CHECK(bundle);
1860 // This is the simplest way to clear out the bundle.
1861 *bundle = SyncBundle();
1862}
1863
1864SyncDataList ExtensionService::GetAllSyncData(syncable::ModelType type) const {
1865 const SyncBundle* bundle = GetSyncBundleForModelTypeConst(type);
1866 CHECK(bundle);
1867 std::vector<ExtensionSyncData> extension_sync_data = GetSyncDataList(*bundle);
1868 SyncDataList result(extension_sync_data.size());
1869 for (int i = 0; i < static_cast<int>(extension_sync_data.size()); ++i) {
1870 result[i] = extension_sync_data[i].GetSyncData();
1871 }
1872 return result;
1873}
1874
1875SyncError ExtensionService::ProcessSyncChanges(
1876 const tracked_objects::Location& from_here,
1877 const SyncChangeList& change_list) {
1878 for (SyncChangeList::const_iterator i = change_list.begin();
1879 i != change_list.end();
1880 ++i) {
1881 ExtensionSyncData extension_sync_data = ExtensionSyncData(*i);
1882 SyncBundle* bundle = GetSyncBundleForExtensionSyncData(extension_sync_data);
1883 CHECK(bundle);
1884
1885 if (extension_sync_data.uninstalled())
1886 bundle->synced_extensions.erase(extension_sync_data.id());
1887 else
1888 bundle->synced_extensions.insert(extension_sync_data.id());
1889 ProcessExtensionSyncData(extension_sync_data, *bundle);
1890 }
1891
1892 return SyncError();
[email protected]b05fb9ff2011-04-23 00:07:561893}
1894
1895void ExtensionService::GetSyncDataListHelper(
1896 const ExtensionList& extensions,
[email protected]3bdba0d2011-08-23 07:17:301897 const SyncBundle& bundle,
[email protected]b05fb9ff2011-04-23 00:07:561898 std::vector<ExtensionSyncData>* sync_data_list) const {
1899 for (ExtensionList::const_iterator it = extensions.begin();
1900 it != extensions.end(); ++it) {
1901 const Extension& extension = **it;
[email protected]3bdba0d2011-08-23 07:17:301902 if (bundle.filter(extension) &&
1903 // If we have pending extension data for this extension, then this
1904 // version is out of date. We'll sync back the version we got from
1905 // sync.
1906 !bundle.HasPendingExtensionId(extension.id())) {
1907 sync_data_list->push_back(
1908 ExtensionSyncData(extension,
1909 IsExtensionEnabled(extension.id()),
1910 IsIncognitoEnabled(extension.id())));
[email protected]b05fb9ff2011-04-23 00:07:561911 }
1912 }
1913}
1914
1915std::vector<ExtensionSyncData> ExtensionService::GetSyncDataList(
[email protected]3bdba0d2011-08-23 07:17:301916 const SyncBundle& bundle) const {
1917 std::vector<ExtensionSyncData> extension_sync_list;
1918 GetSyncDataListHelper(extensions_, bundle, &extension_sync_list);
1919 GetSyncDataListHelper(disabled_extensions_, bundle, &extension_sync_list);
1920 GetSyncDataListHelper(terminated_extensions_, bundle, &extension_sync_list);
1921
1922 for (std::map<std::string, ExtensionSyncData>::const_iterator i =
1923 bundle.pending_sync_data.begin();
1924 i != bundle.pending_sync_data.end();
1925 ++i) {
1926 extension_sync_list.push_back(i->second);
1927 }
1928
1929 return extension_sync_list;
[email protected]b05fb9ff2011-04-23 00:07:561930}
1931
[email protected]3bdba0d2011-08-23 07:17:301932void ExtensionService::ProcessExtensionSyncData(
[email protected]90310d92011-04-17 07:35:041933 const ExtensionSyncData& extension_sync_data,
[email protected]3bdba0d2011-08-23 07:17:301934 SyncBundle& bundle) {
1935 const std::string& id = extension_sync_data.id();
[email protected]96e989b2011-08-30 19:35:061936 const Extension* extension = GetInstalledExtension(id);
1937
1938 // TODO(bolms): we should really handle this better. The particularly bad
1939 // case is where an app becomes an extension or vice versa, and we end up with
1940 // a zombie extension that won't go away.
1941 if (extension && !bundle.filter(*extension))
1942 return;
[email protected]90310d92011-04-17 07:35:041943
1944 // Handle uninstalls first.
[email protected]3bdba0d2011-08-23 07:17:301945 if (extension_sync_data.uninstalled()) {
[email protected]90310d92011-04-17 07:35:041946 std::string error;
1947 if (!UninstallExtensionHelper(this, id)) {
1948 LOG(WARNING) << "Could not uninstall extension " << id
1949 << " for sync";
1950 }
1951 return;
1952 }
1953
[email protected]06f92562011-04-29 19:27:311954 // Set user settings.
[email protected]3bdba0d2011-08-23 07:17:301955 if (extension_sync_data.enabled()) {
[email protected]06f92562011-04-29 19:27:311956 EnableExtension(id);
1957 } else {
1958 DisableExtension(id);
1959 }
[email protected]3bdba0d2011-08-23 07:17:301960 SetIsIncognitoEnabled(id, extension_sync_data.incognito_enabled());
[email protected]81b14cc2011-04-29 00:39:371961
[email protected]90310d92011-04-17 07:35:041962 if (extension) {
[email protected]06f92562011-04-29 19:27:311963 // If the extension is already installed, check if it's outdated.
[email protected]3bdba0d2011-08-23 07:17:301964 int result = extension->version()->CompareTo(extension_sync_data.version());
[email protected]90310d92011-04-17 07:35:041965 if (result < 0) {
1966 // Extension is outdated.
[email protected]3bdba0d2011-08-23 07:17:301967 bundle.pending_sync_data[extension_sync_data.id()] = extension_sync_data;
[email protected]90310d92011-04-17 07:35:041968 CheckForUpdatesSoon();
[email protected]90310d92011-04-17 07:35:041969 }
[email protected]06f92562011-04-29 19:27:311970 } else {
[email protected]06f92562011-04-29 19:27:311971 // TODO(akalin): Replace silent update with a list of enabled
1972 // permissions.
[email protected]6cc7dbae2011-04-29 21:18:331973 const bool kInstallSilently = true;
[email protected]06f92562011-04-29 19:27:311974 if (!pending_extension_manager()->AddFromSync(
1975 id,
[email protected]3bdba0d2011-08-23 07:17:301976 extension_sync_data.update_url(),
1977 bundle.filter,
[email protected]6cc7dbae2011-04-29 21:18:331978 kInstallSilently)) {
[email protected]06f92562011-04-29 19:27:311979 LOG(WARNING) << "Could not add pending extension for " << id;
[email protected]3bdba0d2011-08-23 07:17:301980 // This means that the extension is already pending installation, with a
1981 // non-INTERNAL location. Add to pending_sync_data, even though it will
1982 // never be removed (we'll never install a syncable version of the
1983 // extension), so that GetAllSyncData() continues to send it.
[email protected]06f92562011-04-29 19:27:311984 }
[email protected]3bdba0d2011-08-23 07:17:301985 // Track pending extensions so that we can return them in GetAllSyncData().
1986 bundle.pending_sync_data[extension_sync_data.id()] = extension_sync_data;
[email protected]06f92562011-04-29 19:27:311987 CheckForUpdatesSoon();
[email protected]90310d92011-04-17 07:35:041988 }
[email protected]90310d92011-04-17 07:35:041989}
1990
[email protected]c3cfb012011-04-06 22:07:351991bool ExtensionService::IsIncognitoEnabled(
1992 const std::string& extension_id) const {
1993 // If this is an existing component extension we always allow it to
1994 // work in incognito mode.
[email protected]81b14cc2011-04-29 00:39:371995 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]c3cfb012011-04-06 22:07:351996 if (extension && extension->location() == Extension::COMPONENT)
[email protected]cb0ce1e022010-03-10 19:54:411997 return true;
1998
1999 // Check the prefs.
[email protected]c3cfb012011-04-06 22:07:352000 return extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]db7331a2010-02-25 22:10:502001}
[email protected]55a35692010-02-11 23:25:212002
[email protected]c3cfb012011-04-06 22:07:352003void ExtensionService::SetIsIncognitoEnabled(
2004 const std::string& extension_id, bool enabled) {
[email protected]81b14cc2011-04-29 00:39:372005 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]c3cfb012011-04-06 22:07:352006 if (extension && extension->location() == Extension::COMPONENT) {
2007 // This shouldn't be called for component extensions.
2008 NOTREACHED();
2009 return;
2010 }
2011
[email protected]8c6c8c22011-03-09 12:52:342012 // Broadcast unloaded and loaded events to update browser state. Only bother
2013 // if the value changed and the extension is actually enabled, since there is
2014 // no UI otherwise.
[email protected]c3cfb012011-04-06 22:07:352015 bool old_enabled = extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]8c6c8c22011-03-09 12:52:342016 if (enabled == old_enabled)
2017 return;
2018
[email protected]c3cfb012011-04-06 22:07:352019 extension_prefs_->SetIsIncognitoEnabled(extension_id, enabled);
[email protected]81b14cc2011-04-29 00:39:372020
2021 // If the extension is enabled (and not terminated), unload and
2022 // reload it to update UI.
2023 const Extension* enabled_extension = GetExtensionById(extension_id, false);
2024 if (enabled_extension) {
[email protected]814a7bf0f2011-08-13 05:30:592025 NotifyExtensionUnloaded(
2026 enabled_extension, extension_misc::UNLOAD_REASON_DISABLE);
[email protected]81b14cc2011-04-29 00:39:372027 NotifyExtensionLoaded(enabled_extension);
[email protected]568f33d2010-08-04 17:06:412028 }
[email protected]3bdba0d2011-08-23 07:17:302029
2030 if (extension)
2031 SyncExtensionChangeIfNeeded(*extension);
[email protected]55a35692010-02-11 23:25:212032}
2033
[email protected]eaa7dd182010-12-14 11:09:002034bool ExtensionService::CanCrossIncognito(const Extension* extension) {
[email protected]2a8f24e2010-11-03 21:37:052035 // We allow the extension to see events and data from another profile iff it
2036 // uses "spanning" behavior and it has incognito access. "split" mode
2037 // extensions only see events for a matching profile.
[email protected]0c41298f2011-09-13 21:17:222038 CHECK(extension);
[email protected]c3cfb012011-04-06 22:07:352039 return IsIncognitoEnabled(extension->id()) &&
2040 !extension->incognito_split_mode();
[email protected]2a8f24e2010-11-03 21:37:052041}
2042
[email protected]5ef7b002011-05-12 23:16:202043bool ExtensionService::CanLoadInIncognito(const Extension* extension) const {
2044 if (extension->is_hosted_app())
2045 return true;
2046 // Packaged apps and regular extensions need to be enabled specifically for
2047 // incognito (and split mode should be set).
2048 return extension->incognito_split_mode() &&
2049 IsIncognitoEnabled(extension->id());
2050}
2051
[email protected]eaa7dd182010-12-14 11:09:002052bool ExtensionService::AllowFileAccess(const Extension* extension) {
[email protected]05c82182010-06-24 17:49:082053 return (CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]334e04a2010-06-24 23:34:442054 switches::kDisableExtensionsFileAccessCheck) ||
[email protected]05c82182010-06-24 17:49:082055 extension_prefs_->AllowFileAccess(extension->id()));
2056}
2057
[email protected]eaa7dd182010-12-14 11:09:002058void ExtensionService::SetAllowFileAccess(const Extension* extension,
[email protected]3aff9ad2011-04-01 20:26:482059 bool allow) {
2060 // Reload to update browser state. Only bother if the value changed and the
2061 // extension is actually enabled, since there is no UI otherwise.
2062 bool old_allow = AllowFileAccess(extension);
2063 if (allow == old_allow)
2064 return;
2065
[email protected]05c82182010-06-24 17:49:082066 extension_prefs_->SetAllowFileAccess(extension->id(), allow);
[email protected]3aff9ad2011-04-01 20:26:482067
2068 bool extension_is_enabled = std::find(extensions_.begin(), extensions_.end(),
2069 extension) != extensions_.end();
2070 if (extension_is_enabled)
2071 ReloadExtension(extension->id());
[email protected]05c82182010-06-24 17:49:082072}
2073
[email protected]eaa7dd182010-12-14 11:09:002074bool ExtensionService::GetBrowserActionVisibility(const Extension* extension) {
[email protected]f681c782010-11-19 11:19:392075 return extension_prefs_->GetBrowserActionVisibility(extension);
2076}
2077
[email protected]eaa7dd182010-12-14 11:09:002078void ExtensionService::SetBrowserActionVisibility(const Extension* extension,
[email protected]8e4560b62011-01-14 10:09:142079 bool visible) {
[email protected]f681c782010-11-19 11:19:392080 extension_prefs_->SetBrowserActionVisibility(extension, visible);
2081}
2082
[email protected]8e4560b62011-01-14 10:09:142083// Some extensions will autoupdate themselves externally from Chrome. These
2084// are typically part of some larger client application package. To support
2085// these, the extension will register its location in the the preferences file
2086// (and also, on Windows, in the registry) and this code will periodically
2087// check that location for a .crx file, which it will then install locally if
2088// a new version is available.
2089// Errors are reported through ExtensionErrorReporter. Succcess is not
2090// reported.
[email protected]eaa7dd182010-12-14 11:09:002091void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:122092 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]8e4560b62011-01-14 10:09:142093
2094 // Note that this installation is intentionally silent (since it didn't
2095 // go through the front-end). Extensions that are registered in this
2096 // way are effectively considered 'pre-bundled', and so implicitly
2097 // trusted. In general, if something has HKLM or filesystem access,
2098 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:122099
2100 // If any external extension records give a URL, a provider will set
2101 // this to true. Used by OnExternalProviderReady() to see if we need
2102 // to start an update check to fetch a new external extension.
2103 external_extension_url_added_ = false;
[email protected]8e4560b62011-01-14 10:09:142104
2105 // Ask each external extension provider to give us a call back for each
2106 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
2107 ProviderCollection::const_iterator i;
2108 for (i = external_extension_providers_.begin();
2109 i != external_extension_providers_.end(); ++i) {
2110 ExternalExtensionProviderInterface* provider = i->get();
2111 provider->VisitRegisteredExtension();
2112 }
2113
[email protected]50067e52011-10-20 23:17:072114 // Do any required work that we would have done after completion of all
2115 // providers.
2116 if (external_extension_providers_.empty()) {
2117 OnAllExternalProvidersReady();
2118 }
[email protected]9f1087e2009-06-15 17:29:322119}
2120
[email protected]50067e52011-10-20 23:17:072121void ExtensionService::OnExternalProviderReady(
2122 const ExternalExtensionProviderInterface* provider) {
[email protected]a29a517a2011-01-21 21:11:122123 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]50067e52011-10-20 23:17:072124#if 0
2125 // TODO(rogerta): Remove guard and this comment when bug described
2126 // in http://codereview.chromium.org/8245018/ is fixed.
2127 CHECK(provider->IsReady());
2128#endif
[email protected]a29a517a2011-01-21 21:11:122129
2130 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:142131 // if all of them are finished. So we check them first.
2132 ProviderCollection::const_iterator i;
2133 for (i = external_extension_providers_.begin();
2134 i != external_extension_providers_.end(); ++i) {
2135 ExternalExtensionProviderInterface* provider = i->get();
[email protected]a29a517a2011-01-21 21:11:122136 if (!provider->IsReady())
[email protected]8e4560b62011-01-14 10:09:142137 return;
[email protected]8e4560b62011-01-14 10:09:142138 }
2139
[email protected]50067e52011-10-20 23:17:072140 OnAllExternalProvidersReady();
2141}
2142
2143void ExtensionService::OnAllExternalProvidersReady() {
2144 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2145
2146 // Install any pending extensions.
[email protected]a29a517a2011-01-21 21:11:122147 if (external_extension_url_added_ && updater()) {
2148 external_extension_url_added_ = false;
[email protected]8e4560b62011-01-14 10:09:142149 updater()->CheckNow();
2150 }
2151
2152 // Uninstall all the unclaimed extensions.
2153 scoped_ptr<ExtensionPrefs::ExtensionsInfo> extensions_info(
2154 extension_prefs_->GetInstalledExtensionsInfo());
2155 for (size_t i = 0; i < extensions_info->size(); ++i) {
2156 ExtensionInfo* info = extensions_info->at(i).get();
2157 if (Extension::IsExternalLocation(info->extension_location))
2158 CheckExternalUninstall(info->extension_id);
2159 }
[email protected]e5af875f2011-10-10 21:09:142160 IdentifyAlertableExtensions();
[email protected]e5af875f2011-10-10 21:09:142161}
2162
2163void ExtensionService::IdentifyAlertableExtensions() {
[email protected]e5af875f2011-10-10 21:09:142164 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2165
[email protected]08b83362011-10-19 05:23:172166 if (!CommandLine::ForCurrentProcess()->HasSwitch(
2167 switches::kEnableExtensionAlerts)) {
2168 return; // TODO(miket): enable unconditionally when done.
2169 }
2170
[email protected]e5af875f2011-10-10 21:09:142171 // Build up the lists of extensions that require acknowledgment.
2172 // If this is the first time, grandfather extensions that would have
2173 // caused notification.
2174 scoped_ptr<ExtensionGlobalError> global_error(
2175 new ExtensionGlobalError(weak_ptr_factory_.GetWeakPtr()));
2176 bool needs_alert = false;
2177 for (ExtensionList::const_iterator iter = extensions_.begin();
2178 iter != extensions_.end(); ++iter) {
2179 const Extension* e = *iter;
2180 if (!IsExtensionEnabled(e->id())) {
2181 continue;
2182 }
2183 if (Extension::IsExternalLocation(e->location())) {
2184 if (!extension_prefs_->IsExternalExtensionAcknowledged(e->id())) {
2185 global_error->AddExternalExtension(e->id());
2186 needs_alert = true;
2187 }
2188 }
2189 if (extension_prefs_->IsExtensionBlacklisted(e->id())) {
2190 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(e->id())) {
2191 global_error->AddBlacklistedExtension(e->id());
2192 needs_alert = true;
2193 }
2194 }
2195 if (extension_prefs_->IsExtensionOrphaned(e->id())) {
2196 if (!extension_prefs_->IsOrphanedExtensionAcknowledged(e->id())) {
2197 global_error->AddOrphanedExtension(e->id());
2198 needs_alert = true;
2199 }
2200 }
2201 }
2202
2203 if (needs_alert) {
2204 if (extension_prefs_->SetAlertSystemFirstRun()) {
2205 global_error->set_accept_callback(
2206 base::Bind(&ExtensionService::HandleExtensionAlertAccept,
2207 base::Unretained(this)));
2208 global_error->set_cancel_callback(
2209 base::Bind(&ExtensionService::HandleExtensionAlertDetails,
2210 base::Unretained(this)));
[email protected]62433d32011-10-12 22:33:122211 ShowExtensionAlert(global_error.release());
[email protected]e5af875f2011-10-10 21:09:142212 } else {
2213 // First run. Just acknowledge all the extensions, silently, by
2214 // shortcutting the display of the UI and going straight to the
2215 // callback for pressing the Accept button.
[email protected]62433d32011-10-12 22:33:122216 HandleExtensionAlertAccept(*global_error.get(), NULL);
[email protected]e5af875f2011-10-10 21:09:142217 }
2218 }
[email protected]e5af875f2011-10-10 21:09:142219}
2220
[email protected]62433d32011-10-12 22:33:122221void ExtensionService::ShowExtensionAlert(ExtensionGlobalError* global_error) {
2222 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2223 Browser* browser = BrowserList::GetLastActiveWithProfile(profile_);
2224 if (browser) {
2225 global_error->ShowBubbleView(browser);
2226 }
2227}
2228
[email protected]e5af875f2011-10-10 21:09:142229void ExtensionService::HandleExtensionAlertAccept(
[email protected]62433d32011-10-12 22:33:122230 const ExtensionGlobalError& global_error, Browser* browser) {
[email protected]e5af875f2011-10-10 21:09:142231 const ExtensionIdSet *extension_ids =
2232 global_error.get_external_extension_ids();
2233 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
2234 iter != extension_ids->end(); ++iter) {
2235 extension_prefs_->AcknowledgeExternalExtension(*iter);
2236 }
2237 extension_ids = global_error.get_blacklisted_extension_ids();
2238 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
2239 iter != extension_ids->end(); ++iter) {
2240 extension_prefs_->AcknowledgeBlacklistedExtension(*iter);
2241 }
2242 extension_ids = global_error.get_orphaned_extension_ids();
2243 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
2244 iter != extension_ids->end(); ++iter) {
2245 extension_prefs_->AcknowledgeOrphanedExtension(*iter);
2246 }
2247}
2248
2249void ExtensionService::HandleExtensionAlertDetails(
[email protected]62433d32011-10-12 22:33:122250 const ExtensionGlobalError& global_error, Browser* browser) {
2251 if (browser) {
2252 browser->ShowExtensionsTab();
2253 }
[email protected]6c751e72010-11-23 10:11:102254}
2255
[email protected]a9f39a312010-12-23 22:14:272256void ExtensionService::UnloadExtension(
2257 const std::string& extension_id,
[email protected]814a7bf0f2011-08-13 05:30:592258 extension_misc::UnloadedExtensionReason reason) {
[email protected]27e469a2010-01-11 20:35:092259 // Make sure the extension gets deleted after we return from this function.
[email protected]9adb9692010-10-29 23:14:022260 scoped_refptr<const Extension> extension(
[email protected]8001df22011-04-28 19:59:472261 GetExtensionByIdInternal(extension_id, true, true, false));
[email protected]631cf822009-05-15 07:01:252262
[email protected]fa0c96732010-11-17 00:14:232263 // This method can be called via PostTask, so the extension may have been
2264 // unloaded by the time this runs.
[email protected]dd163fb02011-05-04 22:22:172265 if (!extension) {
2266 // In case the extension may have crashed/uninstalled. Allow the profile to
2267 // clean up its RequestContexts.
2268 profile_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]fa0c96732010-11-17 00:14:232269 return;
[email protected]dd163fb02011-05-04 22:22:172270 }
[email protected]0c6da502009-08-14 22:32:392271
[email protected]1eb175082010-02-10 09:26:162272 // Keep information about the extension so that we can reload it later
2273 // even if it's not permanently installed.
2274 unloaded_extension_paths_[extension->id()] = extension->path();
2275
[email protected]f17dbd42010-08-16 23:21:102276 // Clean up if the extension is meant to be enabled after a reload.
2277 disabled_extension_paths_.erase(extension->id());
2278
[email protected]d7e9a862010-11-03 21:57:492279 // Clean up runtime data.
2280 extension_runtime_data_.erase(extension_id);
2281
[email protected]0c6da502009-08-14 22:32:392282 ExtensionList::iterator iter = std::find(disabled_extensions_.begin(),
2283 disabled_extensions_.end(),
2284 extension.get());
2285 if (iter != disabled_extensions_.end()) {
[email protected]a9f39a312010-12-23 22:14:272286 UnloadedExtensionInfo details(extension, reason);
2287 details.already_disabled = true;
[email protected]0c6da502009-08-14 22:32:392288 disabled_extensions_.erase(iter);
[email protected]ad50def52011-10-19 23:17:072289 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272290 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]6c2381d2011-10-19 02:52:532291 content::Source<Profile>(profile_),
2292 content::Details<UnloadedExtensionInfo>(&details));
[email protected]dd163fb02011-05-04 22:22:172293 // Make sure the profile cleans up its RequestContexts when an already
2294 // disabled extension is unloaded (since they are also tracking the disabled
2295 // extensions).
2296 profile_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]0c6da502009-08-14 22:32:392297 return;
2298 }
2299
2300 iter = std::find(extensions_.begin(), extensions_.end(), extension.get());
[email protected]894bb502009-05-21 22:39:572301
[email protected]631cf822009-05-15 07:01:252302 // Remove the extension from our list.
2303 extensions_.erase(iter);
2304
[email protected]a9f39a312010-12-23 22:14:272305 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]631cf822009-05-15 07:01:252306}
2307
[email protected]eaa7dd182010-12-14 11:09:002308void ExtensionService::UnloadAllExtensions() {
[email protected]c8d407e2011-04-28 21:27:172309 profile_->GetExtensionSpecialStoragePolicy()->
2310 RevokeRightsForAllExtensions();
2311
[email protected]9f1087e2009-06-15 17:29:322312 extensions_.clear();
[email protected]cd500f72010-06-25 23:44:322313 disabled_extensions_.clear();
[email protected]bb7f40952011-01-13 00:21:202314 terminated_extension_ids_.clear();
2315 terminated_extensions_.clear();
[email protected]d7e9a862010-11-03 21:57:492316 extension_runtime_data_.clear();
[email protected]cd500f72010-06-25 23:44:322317
[email protected]c6e4a3412009-06-24 15:45:292318 // TODO(erikkay) should there be a notification for this? We can't use
2319 // EXTENSION_UNLOADED since that implies that the extension has been disabled
2320 // or uninstalled, and UnloadAll is just part of shutdown.
[email protected]9f1087e2009-06-15 17:29:322321}
2322
[email protected]eaa7dd182010-12-14 11:09:002323void ExtensionService::ReloadExtensions() {
[email protected]9f1087e2009-06-15 17:29:322324 UnloadAllExtensions();
2325 LoadAllExtensions();
2326}
2327
[email protected]eaa7dd182010-12-14 11:09:002328void ExtensionService::GarbageCollectExtensions() {
[email protected]39d9f62c2010-12-03 10:48:502329 if (extension_prefs_->pref_service()->ReadOnly())
[email protected]ba399672010-04-06 15:42:392330 return;
2331
[email protected]ca3dbf52010-05-19 22:27:062332 scoped_ptr<ExtensionPrefs::ExtensionsInfo> info(
2333 extension_prefs_->GetInstalledExtensionsInfo());
2334
2335 std::map<std::string, FilePath> extension_paths;
2336 for (size_t i = 0; i < info->size(); ++i)
2337 extension_paths[info->at(i)->extension_id] = info->at(i)->extension_path;
2338
[email protected]14908b72011-04-20 06:54:362339 if (!BrowserThread::PostTask(
2340 BrowserThread::FILE, FROM_HERE,
[email protected]53612e82011-10-18 18:00:362341 base::Bind(
[email protected]14908b72011-04-20 06:54:362342 &extension_file_util::GarbageCollectExtensions,
2343 install_directory_,
2344 extension_paths)))
2345 NOTREACHED();
[email protected]f6ccd652010-11-17 00:48:342346
2347 // Also garbage-collect themes. We check |profile_| to be
2348 // defensive; in the future, we may call GarbageCollectExtensions()
2349 // from somewhere other than Init() (e.g., in a timer).
2350 if (profile_) {
[email protected]18280372011-03-22 18:05:222351 ThemeServiceFactory::GetForProfile(profile_)->RemoveUnusedThemes();
[email protected]f6ccd652010-11-17 00:48:342352 }
[email protected]3cf4f0992009-02-03 23:00:302353}
2354
[email protected]eaa7dd182010-12-14 11:09:002355void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]93fd78f42009-07-10 16:43:172356 if (updater_.get()) {
2357 updater_->Start();
2358 }
[email protected]fa6a9102010-11-22 15:38:502359
2360 ready_ = true;
[email protected]ad50def52011-10-19 23:17:072361 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272362 chrome::NOTIFICATION_EXTENSIONS_READY,
[email protected]6c2381d2011-10-19 02:52:532363 content::Source<Profile>(profile_),
[email protected]ad50def52011-10-19 23:17:072364 content::NotificationService::NoDetails());
[email protected]e72e8eb82009-06-18 17:21:512365}
2366
[email protected]49cd1572011-02-08 21:38:452367void ExtensionService::AddExtension(const Extension* extension) {
[email protected]ae09ca62009-08-21 19:46:462368 // Ensure extension is deleted unless we transfer ownership.
[email protected]9adb9692010-10-29 23:14:022369 scoped_refptr<const Extension> scoped_extension(extension);
[email protected]9f1087e2009-06-15 17:29:322370
[email protected]c8d407e2011-04-28 21:27:172371 // TODO(jstritar): We may be able to get rid of this branch by overriding the
2372 // default extension state to DISABLED when the --disable-extensions flag
2373 // is set (http://crbug.com/29067).
2374 if (!extensions_enabled() &&
2375 !extension->is_theme() &&
2376 extension->location() != Extension::COMPONENT &&
2377 !Extension::IsExternalLocation(extension->location()))
2378 return;
2379
2380 SetBeingUpgraded(extension, false);
2381
[email protected]1eb175082010-02-10 09:26:162382 // The extension is now loaded, remove its data from unloaded extension map.
2383 unloaded_extension_paths_.erase(extension->id());
2384
[email protected]bb7f40952011-01-13 00:21:202385 // If a terminated extension is loaded, remove it from the terminated list.
2386 UntrackTerminatedExtension(extension->id());
2387
[email protected]f17dbd42010-08-16 23:21:102388 // If the extension was disabled for a reload, then enable it.
2389 if (disabled_extension_paths_.erase(extension->id()) > 0)
2390 EnableExtension(extension->id());
2391
[email protected]d728e002010-12-08 04:46:232392 // Check if the extension's privileges have changed and disable the
2393 // extension if necessary.
[email protected]902fd7b2011-07-27 18:42:312394 InitializePermissions(extension);
[email protected]0c6da502009-08-14 22:32:392395
[email protected]ad83ca242011-07-29 01:32:252396 bool disabled = extension_prefs_->IsExtensionDisabled(extension->id());
[email protected]36429da2011-07-11 20:25:182397 if (disabled) {
[email protected]c8d407e2011-04-28 21:27:172398 disabled_extensions_.push_back(scoped_extension);
2399 // TODO(aa): This seems dodgy. It seems that AddExtension() could get called
2400 // with a disabled extension for other reasons other than that an update was
2401 // disabled.
[email protected]ad50def52011-10-19 23:17:072402 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272403 chrome::NOTIFICATION_EXTENSION_UPDATE_DISABLED,
[email protected]6c2381d2011-10-19 02:52:532404 content::Source<Profile>(profile_),
2405 content::Details<const Extension>(extension));
[email protected]3bdba0d2011-08-23 07:17:302406 SyncExtensionChangeIfNeeded(*extension);
[email protected]c8d407e2011-04-28 21:27:172407 return;
[email protected]e72e8eb82009-06-18 17:21:512408 }
[email protected]aab98a52009-12-02 03:22:352409
[email protected]c8d407e2011-04-28 21:27:172410 extensions_.push_back(scoped_extension);
[email protected]3bdba0d2011-08-23 07:17:302411 SyncExtensionChangeIfNeeded(*extension);
[email protected]c8d407e2011-04-28 21:27:172412 NotifyExtensionLoaded(extension);
[email protected]d1282c22011-10-18 02:00:392413 IdentifyAlertableExtensions();
[email protected]aab98a52009-12-02 03:22:352414}
2415
[email protected]902fd7b2011-07-27 18:42:312416void ExtensionService::InitializePermissions(const Extension* extension) {
2417 // If the extension has used the optional permissions API, it will have a
2418 // custom set of active permissions defined in the extension prefs. Here,
2419 // we update the extension's active permissions based on the prefs.
2420 scoped_refptr<ExtensionPermissionSet> active_permissions =
2421 extension_prefs()->GetActivePermissions(extension->id());
2422
2423 if (active_permissions.get()) {
2424 // We restrict the active permissions to be within the bounds defined in the
2425 // extension's manifest.
2426 // a) active permissions must be a subset of optional + default permissions
2427 // b) active permissions must contains all default permissions
2428 scoped_refptr<ExtensionPermissionSet> total_permissions =
2429 ExtensionPermissionSet::CreateUnion(
2430 extension->required_permission_set(),
2431 extension->optional_permission_set());
2432
2433 // Make sure the active permissions contain no more than optional + default.
2434 scoped_refptr<ExtensionPermissionSet> adjusted_active =
2435 ExtensionPermissionSet::CreateIntersection(
2436 total_permissions.get(), active_permissions.get());
2437
2438 // Make sure the active permissions contain the default permissions.
2439 adjusted_active = ExtensionPermissionSet::CreateUnion(
2440 extension->required_permission_set(), adjusted_active.get());
2441
2442 UpdateActivePermissions(extension, adjusted_active);
2443 }
2444
[email protected]8d888c12010-11-30 00:00:252445 // We keep track of all permissions the user has granted each extension.
2446 // This allows extensions to gracefully support backwards compatibility
2447 // by including unknown permissions in their manifests. When the user
2448 // installs the extension, only the recognized permissions are recorded.
2449 // When the unknown permissions become recognized (e.g., through browser
2450 // upgrade), we can prompt the user to accept these new permissions.
2451 // Extensions can also silently upgrade to less permissions, and then
2452 // silently upgrade to a version that adds these permissions back.
2453 //
2454 // For example, pretend that Chrome 10 includes a permission "omnibox"
2455 // for an API that adds suggestions to the omnibox. An extension can
2456 // maintain backwards compatibility while still having "omnibox" in the
2457 // manifest. If a user installs the extension on Chrome 9, the browser
2458 // will record the permissions it recognized, not including "omnibox."
2459 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
2460 // will disable the extension and prompt the user to approve the increase
2461 // in privileges. The extension could then release a new version that
2462 // removes the "omnibox" permission. When the user upgrades, Chrome will
2463 // still remember that "omnibox" had been granted, so that if the
2464 // extension once again includes "omnibox" in an upgrade, the extension
2465 // can upgrade without requiring this user's approval.
2466 const Extension* old = GetExtensionByIdInternal(extension->id(),
[email protected]8001df22011-04-28 19:59:472467 true, true, false);
[email protected]8d888c12010-11-30 00:00:252468 bool is_extension_upgrade = old != NULL;
2469 bool is_privilege_increase = false;
2470
[email protected]902fd7b2011-07-27 18:42:312471 // We only need to compare the granted permissions to the current permissions
2472 // if the extension is not allowed to silently increase its permissions.
2473 if (!extension->CanSilentlyIncreasePermissions()) {
[email protected]8d888c12010-11-30 00:00:252474 // Add all the recognized permissions if the granted permissions list
2475 // hasn't been initialized yet.
[email protected]902fd7b2011-07-27 18:42:312476 scoped_refptr<ExtensionPermissionSet> granted_permissions =
2477 extension_prefs_->GetGrantedPermissions(extension->id());
[email protected]0d3e4a22011-06-23 19:02:522478 CHECK(granted_permissions.get());
[email protected]8d888c12010-11-30 00:00:252479
2480 // Here, we check if an extension's privileges have increased in a manner
2481 // that requires the user's approval. This could occur because the browser
2482 // upgraded and recognized additional privileges, or an extension upgrades
2483 // to a version that requires additional privileges.
[email protected]0d3e4a22011-06-23 19:02:522484 is_privilege_increase =
[email protected]902fd7b2011-07-27 18:42:312485 granted_permissions->HasLessPrivilegesThan(
2486 extension->GetActivePermissions());
[email protected]8d888c12010-11-30 00:00:252487 }
2488
2489 if (is_extension_upgrade) {
[email protected]66e26872010-12-03 20:07:252490 // Other than for unpacked extensions, CrxInstaller should have guaranteed
2491 // that we aren't downgrading.
2492 if (extension->location() != Extension::LOAD)
2493 CHECK(extension->version()->CompareTo(*(old->version())) >= 0);
[email protected]8d888c12010-11-30 00:00:252494
2495 // Extensions get upgraded if the privileges are allowed to increase or
2496 // the privileges haven't increased.
2497 if (!is_privilege_increase) {
2498 SetBeingUpgraded(old, true);
2499 SetBeingUpgraded(extension, true);
2500 }
2501
2502 // To upgrade an extension in place, unload the old one and
2503 // then load the new one.
[email protected]814a7bf0f2011-08-13 05:30:592504 UnloadExtension(old->id(), extension_misc::UNLOAD_REASON_UPDATE);
[email protected]8d888c12010-11-30 00:00:252505 old = NULL;
2506 }
2507
2508 // Extension has changed permissions significantly. Disable it. A
2509 // notification should be sent by the caller.
2510 if (is_privilege_increase) {
[email protected]fe2dd7742011-04-19 22:52:492511 if (!extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
2512 RecordPermissionMessagesHistogram(
2513 extension, "Extensions.Permissions_AutoDisable");
2514 }
[email protected]b3317ad2011-04-28 23:46:002515 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
[email protected]8d888c12010-11-30 00:00:252516 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
2517 }
2518}
2519
[email protected]eaa7dd182010-12-14 11:09:002520void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:392521 std::set<std::string> extension_ids;
[email protected]aab98a52009-12-02 03:22:352522 for (size_t i = 0; i < extensions_.size(); ++i) {
[email protected]2a500c1b2010-11-05 15:27:342523 if (!extensions_[i]->is_theme() &&
2524 extensions_[i]->location() != Extension::COMPONENT)
[email protected]c8865962009-12-16 07:47:392525 extension_ids.insert(extensions_[i]->id());
[email protected]aab98a52009-12-02 03:22:352526 }
2527
2528 child_process_logging::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:252529}
2530
[email protected]e49002a2011-07-13 03:40:442531void ExtensionService::OnLoadSingleExtension(const Extension* extension,
2532 bool prompt_for_plugins) {
[email protected]5fb889382011-06-03 00:29:202533 // If this is a new install of an extension with plugins, prompt the user
2534 // first.
[email protected]e49002a2011-07-13 03:40:442535 if (show_extensions_prompts_ && prompt_for_plugins &&
[email protected]5fb889382011-06-03 00:29:202536 !extension->plugins().empty() &&
2537 disabled_extension_paths_.find(extension->id()) ==
2538 disabled_extension_paths_.end()) {
2539 SimpleExtensionLoadPrompt* prompt = new SimpleExtensionLoadPrompt(
2540 profile_, weak_ptr_factory_.GetWeakPtr(), extension);
2541 prompt->ShowPrompt();
2542 return; // continues in SimpleExtensionLoadPrompt::InstallUI*
2543 }
[email protected]f1b66652011-08-25 23:30:582544 OnExtensionInstalled(extension, false, -1); // Not from web store.
[email protected]5fb889382011-06-03 00:29:202545}
2546
[email protected]8266d662011-07-12 21:53:262547void ExtensionService::OnExtensionInstalled(
[email protected]cc829cf2011-08-22 21:01:352548 const Extension* extension, bool from_webstore, int page_index) {
[email protected]a29a517a2011-01-21 21:11:122549 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:202550
[email protected]4416c5a2010-06-26 01:28:572551 // Ensure extension is deleted unless we transfer ownership.
[email protected]9adb9692010-10-29 23:14:022552 scoped_refptr<const Extension> scoped_extension(extension);
[email protected]c3cfb012011-04-06 22:07:352553 const std::string& id = extension->id();
[email protected]8a87a5332011-08-11 17:54:592554 // Extensions installed by policy can't be disabled. So even if a previous
2555 // installation disabled the extension, make sure it is now enabled.
2556 bool initial_enable =
2557 !extension_prefs_->IsExtensionDisabled(id) ||
2558 !Extension::UserMayDisable(extension->location());
[email protected]b2907fd2011-03-25 16:43:372559 PendingExtensionInfo pending_extension_info;
[email protected]c3cfb012011-04-06 22:07:352560 if (pending_extension_manager()->GetById(id, &pending_extension_info)) {
2561 pending_extension_manager()->Remove(id);
[email protected]8ef78fd2010-08-19 17:14:322562
[email protected]483d1ff2011-03-03 17:12:022563 if (!pending_extension_info.ShouldAllowInstall(*extension)) {
[email protected]4416c5a2010-06-26 01:28:572564 LOG(WARNING)
[email protected]ae6cb3022011-04-04 20:11:542565 << "ShouldAllowInstall() returned false for "
[email protected]c3cfb012011-04-06 22:07:352566 << id << " of type " << extension->GetType()
[email protected]7fa19f82010-12-21 19:40:082567 << " and update URL " << extension->update_url().spec()
2568 << "; not installing";
[email protected]ae6cb3022011-04-04 20:11:542569
[email protected]ad50def52011-10-19 23:17:072570 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272571 chrome::NOTIFICATION_EXTENSION_INSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:532572 content::Source<Profile>(profile_),
2573 content::Details<const Extension>(extension));
[email protected]ae6cb3022011-04-04 20:11:542574
[email protected]4416c5a2010-06-26 01:28:572575 // Delete the extension directory since we're not going to
2576 // load it.
[email protected]14908b72011-04-20 06:54:362577 if (!BrowserThread::PostTask(
2578 BrowserThread::FILE, FROM_HERE,
[email protected]53612e82011-10-18 18:00:362579 base::Bind(&extension_file_util::DeleteFile,
2580 extension->path(), true)))
[email protected]14908b72011-04-20 06:54:362581 NOTREACHED();
[email protected]4416c5a2010-06-26 01:28:572582 return;
2583 }
[email protected]4416c5a2010-06-26 01:28:572584 } else {
[email protected]c3cfb012011-04-06 22:07:352585 // We explicitly want to re-enable an uninstalled external
2586 // extension; if we're here, that means the user is manually
2587 // installing the extension.
[email protected]6cc7dbae2011-04-29 21:18:332588 if (IsExternalExtensionUninstalled(id)) {
2589 initial_enable = true;
2590 }
[email protected]aa142702010-03-26 01:26:332591 }
2592
[email protected]07533022011-06-27 20:42:552593 // Do not record the install histograms for upgrades.
2594 if (!GetExtensionByIdInternal(extension->id(), true, true, false)) {
2595 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
2596 extension->GetType(), 100);
2597 RecordPermissionMessagesHistogram(
2598 extension, "Extensions.Permissions_Install");
2599 }
2600
[email protected]eda3c362010-11-12 08:08:232601 ShownSectionsHandler::OnExtensionInstalled(profile_->GetPrefs(), extension);
[email protected]4416c5a2010-06-26 01:28:572602 extension_prefs_->OnExtensionInstalled(
[email protected]8266d662011-07-12 21:53:262603 extension,
2604 initial_enable ? Extension::ENABLED : Extension::DISABLED,
[email protected]cc829cf2011-08-22 21:01:352605 from_webstore,
2606 page_index);
[email protected]25b34332009-06-05 21:53:192607
[email protected]cdfca9702011-08-08 16:07:012608 // Unpacked extensions default to allowing file access, but if that has been
2609 // overridden, don't reset the value.
[email protected]b6e64fd2011-08-09 19:49:192610 if (Extension::ShouldAlwaysAllowFileAccess(extension->location()) &&
[email protected]cdfca9702011-08-08 16:07:012611 !extension_prefs_->HasAllowFileAccessSetting(id)) {
2612 extension_prefs_->SetAllowFileAccess(id, true);
2613 }
2614
[email protected]b6cf240f2011-10-15 22:09:532615 // If the extension should automatically block network startup (e.g., it uses
2616 // the webRequest API), set the preference. Otherwise clear it, in case the
2617 // extension stopped using a relevant API.
2618 extension_prefs_->SetDelaysNetworkRequests(
2619 extension->id(), extension->ImplicitlyDelaysNetworkStartup());
2620
[email protected]ad50def52011-10-19 23:17:072621 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272622 chrome::NOTIFICATION_EXTENSION_INSTALLED,
[email protected]6c2381d2011-10-19 02:52:532623 content::Source<Profile>(profile_),
2624 content::Details<const Extension>(extension));
[email protected]7577a5c52009-07-30 06:21:582625
[email protected]49cd1572011-02-08 21:38:452626 // Transfer ownership of |extension| to AddExtension.
2627 AddExtension(scoped_extension);
[email protected]4a190632009-05-09 01:07:422628}
2629
[email protected]eaa7dd182010-12-14 11:09:002630const Extension* ExtensionService::GetExtensionByIdInternal(
[email protected]8001df22011-04-28 19:59:472631 const std::string& id, bool include_enabled, bool include_disabled,
2632 bool include_terminated) const {
[email protected]e957fe52009-06-23 16:51:052633 std::string lowercase_id = StringToLowerASCII(id);
[email protected]0c6da502009-08-14 22:32:392634 if (include_enabled) {
2635 for (ExtensionList::const_iterator iter = extensions_.begin();
2636 iter != extensions_.end(); ++iter) {
2637 if ((*iter)->id() == lowercase_id)
2638 return *iter;
2639 }
2640 }
2641 if (include_disabled) {
2642 for (ExtensionList::const_iterator iter = disabled_extensions_.begin();
2643 iter != disabled_extensions_.end(); ++iter) {
2644 if ((*iter)->id() == lowercase_id)
2645 return *iter;
2646 }
[email protected]ce5c4502009-05-06 16:46:112647 }
[email protected]8001df22011-04-28 19:59:472648 if (include_terminated) {
2649 for (ExtensionList::const_iterator iter = terminated_extensions_.begin();
2650 iter != terminated_extensions_.end(); ++iter) {
2651 if ((*iter)->id() == lowercase_id)
2652 return *iter;
2653 }
2654 }
[email protected]ce5c4502009-05-06 16:46:112655 return NULL;
2656}
2657
[email protected]bb7f40952011-01-13 00:21:202658void ExtensionService::TrackTerminatedExtension(const Extension* extension) {
2659 if (terminated_extension_ids_.insert(extension->id()).second)
2660 terminated_extensions_.push_back(make_scoped_refptr(extension));
[email protected]fa2416f2011-05-03 08:41:202661
[email protected]814a7bf0f2011-08-13 05:30:592662 UnloadExtension(extension->id(), extension_misc::UNLOAD_REASON_DISABLE);
[email protected]bb7f40952011-01-13 00:21:202663}
2664
2665void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
[email protected]fa2416f2011-05-03 08:41:202666 std::string lowercase_id = StringToLowerASCII(id);
2667 if (terminated_extension_ids_.erase(lowercase_id) <= 0)
[email protected]bb7f40952011-01-13 00:21:202668 return;
2669
[email protected]bb7f40952011-01-13 00:21:202670 for (ExtensionList::iterator iter = terminated_extensions_.begin();
2671 iter != terminated_extensions_.end(); ++iter) {
2672 if ((*iter)->id() == lowercase_id) {
2673 terminated_extensions_.erase(iter);
2674 return;
2675 }
2676 }
2677}
2678
[email protected]0dfe05c2011-02-23 23:03:362679const Extension* ExtensionService::GetTerminatedExtension(
[email protected]8001df22011-04-28 19:59:472680 const std::string& id) const {
2681 return GetExtensionByIdInternal(id, false, false, true);
2682}
2683
2684const Extension* ExtensionService::GetInstalledExtension(
2685 const std::string& id) const {
2686 return GetExtensionByIdInternal(id, true, true, true);
[email protected]0dfe05c2011-02-23 23:03:362687}
2688
[email protected]eaa7dd182010-12-14 11:09:002689const Extension* ExtensionService::GetWebStoreApp() {
[email protected]d3071992010-10-08 15:24:072690 return GetExtensionById(extension_misc::kWebStoreAppId, false);
2691}
2692
[email protected]eaa7dd182010-12-14 11:09:002693const Extension* ExtensionService::GetExtensionByURL(const GURL& url) {
[email protected]a888b29e62010-04-01 13:38:572694 return url.scheme() != chrome::kExtensionScheme ? NULL :
2695 GetExtensionById(url.host(), false);
2696}
2697
[email protected]eaa7dd182010-12-14 11:09:002698const Extension* ExtensionService::GetExtensionByWebExtent(const GURL& url) {
[email protected]a888b29e62010-04-01 13:38:572699 for (size_t i = 0; i < extensions_.size(); ++i) {
[email protected]cced75a2011-05-20 08:31:122700 if (extensions_[i]->web_extent().MatchesURL(url))
[email protected]a888b29e62010-04-01 13:38:572701 return extensions_[i];
2702 }
2703 return NULL;
[email protected]9f1087e2009-06-15 17:29:322704}
2705
[email protected]f9c292b32011-09-13 16:14:052706const Extension* ExtensionService::GetDisabledExtensionByWebExtent(
2707 const GURL& url) {
2708 for (size_t i = 0; i < disabled_extensions_.size(); ++i) {
2709 if (disabled_extensions_[i]->web_extent().MatchesURL(url))
2710 return disabled_extensions_[i];
2711 }
2712 return NULL;
2713}
2714
[email protected]eaa7dd182010-12-14 11:09:002715bool ExtensionService::ExtensionBindingsAllowed(const GURL& url) {
[email protected]056ad2a2011-07-12 02:13:552716 // Allow bindings for all packaged extensions.
2717 // Note that GetExtensionByURL may return an Extension for hosted apps
2718 // if the URL came from GetEffectiveURL.
2719 const Extension* extension = GetExtensionByURL(url);
2720 if (extension && extension->GetType() != Extension::TYPE_HOSTED_APP)
[email protected]583d45c12010-08-31 02:48:122721 return true;
2722
2723 // Allow bindings for all component, hosted apps.
[email protected]056ad2a2011-07-12 02:13:552724 if (!extension)
2725 extension = GetExtensionByWebExtent(url);
[email protected]583d45c12010-08-31 02:48:122726 return (extension && extension->location() == Extension::COMPONENT);
2727}
2728
[email protected]eaa7dd182010-12-14 11:09:002729const Extension* ExtensionService::GetExtensionByOverlappingWebExtent(
[email protected]cced75a2011-05-20 08:31:122730 const URLPatternSet& extent) {
[email protected]22c966c2010-06-26 06:35:022731 for (size_t i = 0; i < extensions_.size(); ++i) {
2732 if (extensions_[i]->web_extent().OverlapsWith(extent))
2733 return extensions_[i];
2734 }
2735
[email protected]6d2e60bd2010-06-03 22:37:392736 return NULL;
2737}
2738
[email protected]eaa7dd182010-12-14 11:09:002739const SkBitmap& ExtensionService::GetOmniboxIcon(
[email protected]b671760b2010-07-15 21:13:472740 const std::string& extension_id) {
2741 return omnibox_icon_manager_.GetIcon(extension_id);
2742}
2743
[email protected]eaa7dd182010-12-14 11:09:002744const SkBitmap& ExtensionService::GetOmniboxPopupIcon(
[email protected]29d0d4ac2010-09-08 21:10:312745 const std::string& extension_id) {
2746 return omnibox_popup_icon_manager_.GetIcon(extension_id);
2747}
2748
[email protected]eaa7dd182010-12-14 11:09:002749void ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:322750 const std::string& id,
[email protected]8e4560b62011-01-14 10:09:142751 const Version* version,
[email protected]8ef78fd2010-08-19 17:14:322752 const FilePath& path,
2753 Extension::Location location) {
[email protected]ab22ba42011-01-14 16:36:382754 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2755 CHECK(Extension::IdIsValid(id));
[email protected]79c833b52011-04-05 18:31:012756 if (extension_prefs_->IsExternalExtensionUninstalled(id))
[email protected]683d0702010-12-06 16:25:572757 return;
[email protected]a8af9fdb2010-10-28 21:52:202758
[email protected]8e4560b62011-01-14 10:09:142759 DCHECK(version);
2760
[email protected]7577a5c52009-07-30 06:21:582761 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:492762 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:582763 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:022764 const Extension* existing = GetExtensionById(id, true);
[email protected]7577a5c52009-07-30 06:21:582765 if (existing) {
[email protected]8e4560b62011-01-14 10:09:142766 switch (existing->version()->CompareTo(*version)) {
[email protected]7577a5c52009-07-30 06:21:582767 case -1: // existing version is older, we should upgrade
2768 break;
2769 case 0: // existing version is same, do nothing
2770 return;
2771 case 1: // existing version is newer, uh-oh
2772 LOG(WARNING) << "Found external version of extension " << id
2773 << "that is older than current version. Current version "
2774 << "is: " << existing->VersionString() << ". New version "
2775 << "is: " << version << ". Keeping current version.";
2776 return;
2777 }
2778 }
2779
[email protected]b2907fd2011-03-25 16:43:372780 pending_extension_manager()->AddFromExternalFile(id, location);
[email protected]9c635f22010-12-02 09:36:362781
[email protected]14908b72011-04-20 06:54:362782 // no client (silent install)
2783 scoped_refptr<CrxInstaller> installer(MakeCrxInstaller(NULL));
[email protected]6dfbbf82010-03-12 23:09:162784 installer->set_install_source(location);
2785 installer->set_expected_id(id);
[email protected]cb0e50312011-05-09 15:03:072786 installer->set_expected_version(*version);
2787 installer->set_install_cause(extension_misc::INSTALL_CAUSE_EXTERNAL_FILE);
[email protected]6dfbbf82010-03-12 23:09:162788 installer->InstallCrx(path);
[email protected]7577a5c52009-07-30 06:21:582789}
2790
[email protected]eaa7dd182010-12-14 11:09:002791void ExtensionService::ReportExtensionLoadError(
[email protected]d11c8e92009-10-20 23:26:402792 const FilePath& extension_path,
2793 const std::string &error,
[email protected]d11c8e92009-10-20 23:26:402794 bool be_noisy) {
[email protected]ad50def52011-10-19 23:17:072795 content::NotificationService* service =
2796 content::NotificationService::current();
[email protected]655b2b1a2011-10-13 17:13:062797 service->Notify(chrome::NOTIFICATION_EXTENSION_LOAD_ERROR,
[email protected]6c2381d2011-10-19 02:52:532798 content::Source<Profile>(profile_),
2799 content::Details<const std::string>(&error));
[email protected]d11c8e92009-10-20 23:26:402800
[email protected]8a205c02011-02-04 20:41:332801 std::string path_str = UTF16ToUTF8(extension_path.LossyDisplayName());
[email protected]18d4b6c2010-09-21 03:21:042802 std::string message = base::StringPrintf(
2803 "Could not load extension from '%s'. %s",
2804 path_str.c_str(), error.c_str());
[email protected]d11c8e92009-10-20 23:26:402805 ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy);
2806}
2807
[email protected]eaa7dd182010-12-14 11:09:002808void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]406027c02010-09-27 08:03:182809 ExtensionHost* host) {
2810 OrphanedDevTools::iterator iter =
[email protected]2d2f6cfc2011-05-06 21:09:332811 orphaned_dev_tools_.find(host->extension_id());
[email protected]406027c02010-09-27 08:03:182812 if (iter == orphaned_dev_tools_.end())
2813 return;
2814
2815 DevToolsManager::GetInstance()->AttachClientHost(
2816 iter->second, host->render_view_host());
2817 orphaned_dev_tools_.erase(iter);
2818}
2819
[email protected]432115822011-07-10 15:52:272820void ExtensionService::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:532821 const content::NotificationSource& source,
2822 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:272823 switch (type) {
2824 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]6c2381d2011-10-19 02:52:532825 if (profile_ !=
2826 content::Source<Profile>(source).ptr()->GetOriginalProfile()) {
[email protected]bc535ee52010-08-31 18:40:322827 break;
[email protected]6c2381d2011-10-19 02:52:532828 }
[email protected]a4ed6282009-12-14 20:51:162829
[email protected]6c2381d2011-10-19 02:52:532830 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr();
[email protected]f128af42010-08-05 18:05:262831
[email protected]fa2416f2011-05-03 08:41:202832 // Mark the extension as terminated and Unload it. We want it to
2833 // be in a consistent state: either fully working or not loaded
2834 // at all, but never half-crashed. We do it in a PostTask so
2835 // that other handlers of this notification will still have
2836 // access to the Extension and ExtensionHost.
[email protected]14908b72011-04-20 06:54:362837 MessageLoop::current()->PostTask(
2838 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362839 base::Bind(
[email protected]fa2416f2011-05-03 08:41:202840 &ExtensionService::TrackTerminatedExtension,
[email protected]53612e82011-10-18 18:00:362841 weak_ptr_factory_.GetWeakPtr(),
[email protected]fa2416f2011-05-03 08:41:202842 host->extension()));
[email protected]31f77262009-12-02 20:48:532843 break;
2844 }
[email protected]432115822011-07-10 15:52:272845 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: {
[email protected]6c2381d2011-10-19 02:52:532846 RenderProcessHost* process =
2847 content::Source<RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192848 Profile* host_profile =
2849 Profile::FromBrowserContext(process->browser_context());
2850 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2851 break;
2852
[email protected]77a6970c2011-04-23 16:58:562853 // Valid extension function names, used to setup bindings in renderer.
2854 std::vector<std::string> function_names;
2855 ExtensionFunctionDispatcher::GetAllFunctionNames(&function_names);
2856 process->Send(new ExtensionMsg_SetFunctionNames(function_names));
[email protected]31f77262009-12-02 20:48:532857
[email protected]77a6970c2011-04-23 16:58:562858 // Scripting whitelist. This is modified by tests and must be communicated
2859 // to renderers.
2860 process->Send(new ExtensionMsg_SetScriptingWhitelist(
2861 *Extension::GetScriptingWhitelist()));
2862
2863 // Loaded extensions.
2864 for (size_t i = 0; i < extensions_.size(); ++i) {
2865 process->Send(new ExtensionMsg_Loaded(
[email protected]15379642011-08-11 22:22:052866 ExtensionMsg_Loaded_Params(extensions_[i])));
[email protected]77a6970c2011-04-23 16:58:562867 }
2868 break;
2869 }
[email protected]432115822011-07-10 15:52:272870 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
[email protected]6c2381d2011-10-19 02:52:532871 RenderProcessHost* process =
2872 content::Source<RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192873 Profile* host_profile =
2874 Profile::FromBrowserContext(process->browser_context());
2875 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2876 break;
2877
[email protected]da5683db2011-04-23 17:12:212878 installed_app_hosts_.erase(process->id());
2879 break;
2880 }
[email protected]432115822011-07-10 15:52:272881 case chrome::NOTIFICATION_PREF_CHANGED: {
[email protected]6c2381d2011-10-19 02:52:532882 std::string* pref_name = content::Details<std::string>(details).ptr();
[email protected]6c751e72010-11-23 10:11:102883 if (*pref_name == prefs::kExtensionInstallAllowList ||
2884 *pref_name == prefs::kExtensionInstallDenyList) {
2885 CheckAdminBlacklist();
[email protected]6c751e72010-11-23 10:11:102886 } else {
2887 NOTREACHED() << "Unexpected preference name.";
2888 }
[email protected]aa96d3a2010-08-21 08:45:252889 break;
2890 }
[email protected]d91268022011-08-26 13:17:372891 case chrome::NOTIFICATION_IMPORT_FINISHED: {
2892 registrar_.Remove(this, chrome::NOTIFICATION_IMPORT_FINISHED,
[email protected]6c2381d2011-10-19 02:52:532893 content::Source<Profile>(profile_));
[email protected]d91268022011-08-26 13:17:372894 InitEventRouters();
2895 break;
2896 }
[email protected]aa96d3a2010-08-21 08:45:252897
[email protected]4814b512009-11-07 00:12:292898 default:
2899 NOTREACHED() << "Unexpected notification type.";
2900 }
2901}
2902
[email protected]eaa7dd182010-12-14 11:09:002903bool ExtensionService::HasApps() const {
[email protected]ec5b50d2010-10-09 16:35:182904 return !GetAppIds().empty();
2905}
[email protected]377011d2010-07-20 04:18:502906
[email protected]eaa7dd182010-12-14 11:09:002907ExtensionIdSet ExtensionService::GetAppIds() const {
[email protected]ec5b50d2010-10-09 16:35:182908 ExtensionIdSet result;
[email protected]377011d2010-07-20 04:18:502909 for (ExtensionList::const_iterator it = extensions_.begin();
2910 it != extensions_.end(); ++it) {
[email protected]ec5b50d2010-10-09 16:35:182911 if ((*it)->is_app() && (*it)->location() != Extension::COMPONENT)
2912 result.insert((*it)->id());
[email protected]377011d2010-07-20 04:18:502913 }
2914
[email protected]ec5b50d2010-10-09 16:35:182915 return result;
[email protected]377011d2010-07-20 04:18:502916}
[email protected]d7e9a862010-11-03 21:57:492917
[email protected]14908b72011-04-20 06:54:362918scoped_refptr<CrxInstaller> ExtensionService::MakeCrxInstaller(
2919 ExtensionInstallUI* client) {
2920 return new CrxInstaller(weak_ptr_factory_.GetWeakPtr(), client);
2921}
2922
[email protected]eaa7dd182010-12-14 11:09:002923bool ExtensionService::IsBackgroundPageReady(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:492924 return (extension->background_url().is_empty() ||
2925 extension_runtime_data_[extension->id()].background_page_ready);
2926}
2927
[email protected]eaa7dd182010-12-14 11:09:002928void ExtensionService::SetBackgroundPageReady(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:492929 DCHECK(!extension->background_url().is_empty());
2930 extension_runtime_data_[extension->id()].background_page_ready = true;
[email protected]ad50def52011-10-19 23:17:072931 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272932 chrome::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY,
[email protected]6c2381d2011-10-19 02:52:532933 content::Source<const Extension>(extension),
[email protected]ad50def52011-10-19 23:17:072934 content::NotificationService::NoDetails());
[email protected]d7e9a862010-11-03 21:57:492935}
2936
[email protected]eaa7dd182010-12-14 11:09:002937bool ExtensionService::IsBeingUpgraded(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:492938 return extension_runtime_data_[extension->id()].being_upgraded;
2939}
2940
[email protected]eaa7dd182010-12-14 11:09:002941void ExtensionService::SetBeingUpgraded(const Extension* extension,
[email protected]d7e9a862010-11-03 21:57:492942 bool value) {
2943 extension_runtime_data_[extension->id()].being_upgraded = value;
2944}
[email protected]1bead0712010-11-27 17:41:532945
[email protected]eaa7dd182010-12-14 11:09:002946PropertyBag* ExtensionService::GetPropertyBag(const Extension* extension) {
[email protected]1bead0712010-11-27 17:41:532947 return &extension_runtime_data_[extension->id()].property_bag;
2948}
[email protected]cebc3dc2011-04-18 17:15:002949
2950void ExtensionService::RegisterNaClModule(const GURL& url,
2951 const std::string& mime_type) {
2952 NaClModuleInfo info;
2953 info.url = url;
2954 info.mime_type = mime_type;
2955
2956 DCHECK(FindNaClModule(url) == nacl_module_list_.end());
2957 nacl_module_list_.push_front(info);
2958}
2959
2960void ExtensionService::UnregisterNaClModule(const GURL& url) {
2961 NaClModuleInfoList::iterator iter = FindNaClModule(url);
2962 DCHECK(iter != nacl_module_list_.end());
2963 nacl_module_list_.erase(iter);
2964}
2965
[email protected]ed0ba002011-05-26 16:55:132966void ExtensionService::UpdatePluginListWithNaClModules() {
[email protected]300c0ea2011-07-15 23:04:332967 // An extension has been added which has a nacl_module component, which means
2968 // there is a MIME type that module wants to handle, so we need to add that
2969 // MIME type to plugins which handle NaCl modules in order to allow the
2970 // individual modules to handle these types.
[email protected]ed0ba002011-05-26 16:55:132971 const PepperPluginInfo* pepper_info = NULL;
2972 std::vector<PepperPluginInfo> plugins;
2973 PepperPluginRegistry::ComputeList(&plugins);
[email protected]300c0ea2011-07-15 23:04:332974
2975 // Search the entire plugin list for plugins that handle the NaCl MIME type.
2976 // There can be multiple plugins like this, for instance the internal NaCl
2977 // plugin and a plugin registered by --register-pepper-plugins during tests.
[email protected]ed0ba002011-05-26 16:55:132978 for (size_t i = 0; i < plugins.size(); ++i) {
[email protected]300c0ea2011-07-15 23:04:332979 pepper_info = &plugins[i];
2980 CHECK(pepper_info);
[email protected]91d9f3d2011-08-14 05:24:442981 std::vector<webkit::WebPluginMimeType>::const_iterator mime_iter;
[email protected]300c0ea2011-07-15 23:04:332982 // Check each MIME type the plugins handle for the NaCl MIME type.
2983 for (mime_iter = pepper_info->mime_types.begin();
2984 mime_iter != pepper_info->mime_types.end(); ++mime_iter) {
2985 if (mime_iter->mime_type == kNaClPluginMimeType) {
2986 // This plugin handles "application/x-nacl".
2987
2988 webkit::npapi::PluginList::Singleton()->
2989 UnregisterInternalPlugin(pepper_info->path);
2990
[email protected]91d9f3d2011-08-14 05:24:442991 webkit::WebPluginInfo info = pepper_info->ToWebPluginInfo();
[email protected]300c0ea2011-07-15 23:04:332992
2993 for (ExtensionService::NaClModuleInfoList::const_iterator iter =
2994 nacl_module_list_.begin();
2995 iter != nacl_module_list_.end(); ++iter) {
2996 // Add the MIME type specified in the extension to this NaCl plugin,
2997 // With an extra "nacl" argument to specify the location of the NaCl
2998 // manifest file.
[email protected]91d9f3d2011-08-14 05:24:442999 webkit::WebPluginMimeType mime_type_info;
[email protected]300c0ea2011-07-15 23:04:333000 mime_type_info.mime_type = iter->mime_type;
3001 mime_type_info.additional_param_names.push_back(UTF8ToUTF16("nacl"));
3002 mime_type_info.additional_param_values.push_back(
3003 UTF8ToUTF16(iter->url.spec()));
3004 info.mime_types.push_back(mime_type_info);
3005 }
3006
3007 webkit::npapi::PluginList::Singleton()->RefreshPlugins();
3008 webkit::npapi::PluginList::Singleton()->RegisterInternalPlugin(info);
3009 // This plugin has been modified, no need to check the rest of its
3010 // types, but continue checking other plugins.
3011 break;
3012 }
[email protected]ed0ba002011-05-26 16:55:133013 }
3014 }
[email protected]ed0ba002011-05-26 16:55:133015}
3016
[email protected]cebc3dc2011-04-18 17:15:003017ExtensionService::NaClModuleInfoList::iterator
3018 ExtensionService::FindNaClModule(const GURL& url) {
3019 for (NaClModuleInfoList::iterator iter = nacl_module_list_.begin();
3020 iter != nacl_module_list_.end(); ++iter) {
3021 if (iter->url == url)
3022 return iter;
3023 }
3024 return nacl_module_list_.end();
3025}