[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 5 | #include "chrome/browser/extensions/extension_service.h" |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 6 | |
[email protected] | 654512b | 2010-09-01 02:09:42 | [diff] [blame] | 7 | #include <algorithm> |
[email protected] | 97d2f1d | 2011-01-15 00:41:08 | [diff] [blame] | 8 | #include <set> |
[email protected] | 654512b | 2010-09-01 02:09:42 | [diff] [blame] | 9 | |
[email protected] | 24b538a | 2010-02-27 01:22:44 | [diff] [blame] | 10 | #include "base/basictypes.h" |
[email protected] | e2eb4311 | 2009-05-29 21:19:54 | [diff] [blame] | 11 | #include "base/command_line.h" |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 12 | #include "base/file_util.h" |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 13 | #include "base/metrics/histogram.h" |
[email protected] | cd500f7 | 2010-06-25 23:44:32 | [diff] [blame] | 14 | #include "base/stl_util-inl.h" |
[email protected] | 24b538a | 2010-02-27 01:22:44 | [diff] [blame] | 15 | #include "base/string16.h" |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 16 | #include "base/string_number_conversions.h" |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 17 | #include "base/string_util.h" |
[email protected] | 18d4b6c | 2010-09-21 03:21:04 | [diff] [blame] | 18 | #include "base/stringprintf.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 19 | #include "base/threading/thread_restrictions.h" |
[email protected] | cc2c343 | 2009-11-06 17:24:36 | [diff] [blame] | 20 | #include "base/time.h" |
[email protected] | ce7f62e3 | 2010-08-10 23:43:59 | [diff] [blame] | 21 | #include "base/utf_string_conversions.h" |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 22 | #include "base/values.h" |
[email protected] | aa14270 | 2010-03-26 01:26:33 | [diff] [blame] | 23 | #include "base/version.h" |
[email protected] | 15730c4 | 2009-09-03 00:03:20 | [diff] [blame] | 24 | #include "chrome/browser/browser_process.h" |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 25 | #include "chrome/browser/debugger/devtools_manager.h" |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 26 | #include "chrome/browser/extensions/crx_installer.h" |
[email protected] | ec5b50d | 2010-10-09 16:35:18 | [diff] [blame] | 27 | #include "chrome/browser/extensions/default_apps.h" |
[email protected] | 5cbe1e2 | 2010-01-30 01:18:56 | [diff] [blame] | 28 | #include "chrome/browser/extensions/extension_accessibility_api.h" |
[email protected] | 840b0db | 2009-11-20 03:00:38 | [diff] [blame] | 29 | #include "chrome/browser/extensions/extension_bookmarks_module.h" |
[email protected] | b68d5ed | 2009-04-16 02:41:28 | [diff] [blame] | 30 | #include "chrome/browser/extensions/extension_browser_event_router.h" |
[email protected] | 2c5e1e1 | 2010-06-10 13:14:44 | [diff] [blame] | 31 | #include "chrome/browser/extensions/extension_cookies_api.h" |
[email protected] | c10da4b0 | 2010-03-25 14:38:32 | [diff] [blame] | 32 | #include "chrome/browser/extensions/extension_data_deleter.h" |
[email protected] | 14a000d | 2010-04-29 21:44:24 | [diff] [blame] | 33 | #include "chrome/browser/extensions/extension_error_reporter.h" |
[email protected] | de768a83 | 2009-10-30 05:25:01 | [diff] [blame] | 34 | #include "chrome/browser/extensions/extension_history_api.h" |
[email protected] | b1748b1d8 | 2009-11-30 20:32:56 | [diff] [blame] | 35 | #include "chrome/browser/extensions/extension_host.h" |
[email protected] | 7596ce7 | 2010-08-30 05:10:46 | [diff] [blame] | 36 | #include "chrome/browser/extensions/extension_management_api.h" |
[email protected] | 118de789 | 2011-03-16 13:31:40 | [diff] [blame] | 37 | #include "chrome/browser/extensions/extension_preference_api.h" |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 38 | #include "chrome/browser/extensions/extension_process_manager.h" |
[email protected] | 8a661f8 | 2010-10-19 21:47:11 | [diff] [blame] | 39 | #include "chrome/browser/extensions/extension_processes_api.h" |
[email protected] | 19eb8015 | 2011-02-26 00:28:43 | [diff] [blame] | 40 | #include "chrome/browser/extensions/extension_special_storage_policy.h" |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 41 | #include "chrome/browser/extensions/extension_updater.h" |
[email protected] | 8f9d4eb | 2011-02-05 01:39:10 | [diff] [blame] | 42 | #include "chrome/browser/extensions/extension_web_ui.h" |
[email protected] | 784688a6 | 2010-09-13 07:06:52 | [diff] [blame] | 43 | #include "chrome/browser/extensions/extension_webnavigation_api.h" |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 44 | #include "chrome/browser/extensions/external_extension_provider_impl.h" |
[email protected] | 42b79565 | 2011-02-22 23:32:38 | [diff] [blame] | 45 | #include "chrome/browser/extensions/external_extension_provider_interface.h" |
[email protected] | 56ad379 | 2010-05-28 17:45:33 | [diff] [blame] | 46 | #include "chrome/browser/net/chrome_url_request_context.h" |
[email protected] | 37858e5 | 2010-08-26 00:22:02 | [diff] [blame] | 47 | #include "chrome/browser/prefs/pref_service.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 48 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 56ad379 | 2010-05-28 17:45:33 | [diff] [blame] | 49 | #include "chrome/browser/search_engines/template_url_model.h" |
[email protected] | 1828037 | 2011-03-22 18:05:22 | [diff] [blame] | 50 | #include "chrome/browser/themes/theme_service.h" |
[email protected] | a0ea76c | 2011-03-23 17:36:42 | [diff] [blame] | 51 | #include "chrome/browser/themes/theme_service_factory.h" |
[email protected] | fd42ac30f | 2011-02-27 19:33:36 | [diff] [blame] | 52 | #include "chrome/browser/ui/webui/shown_sections_handler.h" |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 53 | #include "chrome/common/child_process_logging.h" |
[email protected] | e2eb4311 | 2009-05-29 21:19:54 | [diff] [blame] | 54 | #include "chrome/common/chrome_switches.h" |
[email protected] | 5b1a0e2 | 2009-05-26 19:00:58 | [diff] [blame] | 55 | #include "chrome/common/extensions/extension.h" |
[email protected] | d7b36dc | 2009-10-29 21:47:40 | [diff] [blame] | 56 | #include "chrome/common/extensions/extension_constants.h" |
[email protected] | 05c8218 | 2010-06-24 17:49:08 | [diff] [blame] | 57 | #include "chrome/common/extensions/extension_error_utils.h" |
[email protected] | 7c927b6 | 2010-02-24 09:54:13 | [diff] [blame] | 58 | #include "chrome/common/extensions/extension_file_util.h" |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 59 | #include "chrome/common/extensions/extension_l10n_util.h" |
[email protected] | a315ba9 | 2010-11-16 14:12:21 | [diff] [blame] | 60 | #include "chrome/common/extensions/extension_resource.h" |
[email protected] | 42b79565 | 2011-02-22 23:32:38 | [diff] [blame] | 61 | #include "chrome/common/json_value_serializer.h" |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 62 | #include "chrome/common/pref_names.h" |
[email protected] | a5720987 | 2009-05-04 22:53:14 | [diff] [blame] | 63 | #include "chrome/common/url_constants.h" |
[email protected] | 5f945a0e | 2011-03-01 17:47:53 | [diff] [blame] | 64 | #include "content/browser/browser_thread.h" |
[email protected] | b3841c50 | 2011-03-09 01:21:31 | [diff] [blame] | 65 | #include "content/common/notification_service.h" |
| 66 | #include "content/common/notification_type.h" |
[email protected] | c10da4b0 | 2010-03-25 14:38:32 | [diff] [blame] | 67 | #include "googleurl/src/gurl.h" |
[email protected] | 1debbbb6 | 2010-10-06 17:23:44 | [diff] [blame] | 68 | #include "net/base/registry_controlled_domain.h" |
[email protected] | 24b538a | 2010-02-27 01:22:44 | [diff] [blame] | 69 | #include "webkit/database/database_tracker.h" |
| 70 | #include "webkit/database/database_util.h" |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 71 | |
[email protected] | 5ef47ec | 2010-01-28 05:58:05 | [diff] [blame] | 72 | using base::Time; |
| 73 | |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 74 | namespace errors = extension_manifest_errors; |
| 75 | |
[email protected] | b6ab96d | 2009-08-20 18:58:19 | [diff] [blame] | 76 | namespace { |
| 77 | |
[email protected] | 29d0d4ac | 2010-09-08 21:10:31 | [diff] [blame] | 78 | #if defined(OS_LINUX) |
| 79 | static const int kOmniboxIconPaddingLeft = 2; |
| 80 | static const int kOmniboxIconPaddingRight = 2; |
| 81 | #elif defined(OS_MACOSX) |
| 82 | static const int kOmniboxIconPaddingLeft = 0; |
| 83 | static const int kOmniboxIconPaddingRight = 2; |
| 84 | #else |
| 85 | static const int kOmniboxIconPaddingLeft = 0; |
| 86 | static const int kOmniboxIconPaddingRight = 0; |
| 87 | #endif |
| 88 | |
[email protected] | a315ba9 | 2010-11-16 14:12:21 | [diff] [blame] | 89 | // The following enumeration is used in histograms matching |
| 90 | // Extensions.ManifestReload* . Values may be added, as long |
| 91 | // as existing values are not changed. |
| 92 | enum ManifestReloadReason { |
| 93 | NOT_NEEDED = 0, // Reload not needed. |
| 94 | UNPACKED_DIR, // Unpacked directory |
| 95 | NEEDS_RELOCALIZATION, // The local has changed since we read this extension. |
| 96 | NUM_MANIFEST_RELOAD_REASONS |
| 97 | }; |
[email protected] | 2111b1a | 2010-03-12 18:12:44 | [diff] [blame] | 98 | |
[email protected] | a315ba9 | 2010-11-16 14:12:21 | [diff] [blame] | 99 | ManifestReloadReason ShouldReloadExtensionManifest(const ExtensionInfo& info) { |
| 100 | // Always reload manifests of unpacked extensions, because they can change |
| 101 | // on disk independent of the manifest in our prefs. |
| 102 | if (info.extension_location == Extension::LOAD) |
| 103 | return UNPACKED_DIR; |
| 104 | |
| 105 | // Reload the manifest if it needs to be relocalized. |
| 106 | if (extension_l10n_util::ShouldRelocalizeManifest(info)) |
| 107 | return NEEDS_RELOCALIZATION; |
| 108 | |
| 109 | return NOT_NEEDED; |
[email protected] | 2111b1a | 2010-03-12 18:12:44 | [diff] [blame] | 110 | } |
| 111 | |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 112 | } // namespace |
[email protected] | b6ab96d | 2009-08-20 18:58:19 | [diff] [blame] | 113 | |
[email protected] | d7e9a86 | 2010-11-03 21:57:49 | [diff] [blame] | 114 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 115 | ExtensionService::ExtensionRuntimeData::ExtensionRuntimeData() |
[email protected] | d7e9a86 | 2010-11-03 21:57:49 | [diff] [blame] | 116 | : background_page_ready(false), |
| 117 | being_upgraded(false) { |
| 118 | } |
| 119 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 120 | ExtensionService::ExtensionRuntimeData::~ExtensionRuntimeData() { |
[email protected] | d7e9a86 | 2010-11-03 21:57:49 | [diff] [blame] | 121 | } |
| 122 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 123 | // ExtensionService. |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 124 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 125 | const char* ExtensionService::kInstallDirectoryName = "Extensions"; |
| 126 | const char* ExtensionService::kCurrentVersionFileName = "Current Version"; |
[email protected] | 494c06e | 2009-07-25 01:06:42 | [diff] [blame] | 127 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 128 | // Implements IO for the ExtensionService. |
[email protected] | 7a4c685 | 2010-09-16 03:44:22 | [diff] [blame] | 129 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 130 | class ExtensionServiceBackend |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 131 | : public base::RefCountedThreadSafe<ExtensionServiceBackend> { |
[email protected] | 7a4c685 | 2010-09-16 03:44:22 | [diff] [blame] | 132 | public: |
[email protected] | 1f830eb | 2010-09-28 08:25:14 | [diff] [blame] | 133 | // |install_directory| is a path where to look for extensions to load. |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 134 | explicit ExtensionServiceBackend(const FilePath& install_directory); |
[email protected] | 7a4c685 | 2010-09-16 03:44:22 | [diff] [blame] | 135 | |
| 136 | // Loads a single extension from |path| where |path| is the top directory of |
| 137 | // a specific extension where its manifest file lives. |
| 138 | // Errors are reported through ExtensionErrorReporter. On success, |
[email protected] | 49cd157 | 2011-02-08 21:38:45 | [diff] [blame] | 139 | // AddExtension() is called. |
[email protected] | 7a4c685 | 2010-09-16 03:44:22 | [diff] [blame] | 140 | // TODO(erikkay): It might be useful to be able to load a packed extension |
| 141 | // (presumably into memory) without installing it. |
| 142 | void LoadSingleExtension(const FilePath &path, |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 143 | scoped_refptr<ExtensionService> frontend); |
[email protected] | 7a4c685 | 2010-09-16 03:44:22 | [diff] [blame] | 144 | |
[email protected] | 7a4c685 | 2010-09-16 03:44:22 | [diff] [blame] | 145 | private: |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 146 | friend class base::RefCountedThreadSafe<ExtensionServiceBackend>; |
[email protected] | 7a4c685 | 2010-09-16 03:44:22 | [diff] [blame] | 147 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 148 | virtual ~ExtensionServiceBackend(); |
[email protected] | 7a4c685 | 2010-09-16 03:44:22 | [diff] [blame] | 149 | |
| 150 | // Finish installing the extension in |crx_path| after it has been unpacked to |
| 151 | // |unpacked_path|. If |expected_id| is not empty, it's verified against the |
| 152 | // extension's manifest before installation. If |silent| is true, there will |
| 153 | // be no install confirmation dialog. |from_gallery| indicates whether the |
| 154 | // crx was installed from our gallery, which results in different UI. |
| 155 | // |
| 156 | // Note: We take ownership of |extension|. |
| 157 | void OnExtensionUnpacked(const FilePath& crx_path, |
| 158 | const FilePath& unpacked_path, |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 159 | const Extension* extension, |
[email protected] | 7a4c685 | 2010-09-16 03:44:22 | [diff] [blame] | 160 | const std::string expected_id); |
| 161 | |
| 162 | // Notify the frontend that there was an error loading an extension. |
| 163 | void ReportExtensionLoadError(const FilePath& extension_path, |
| 164 | const std::string& error); |
| 165 | |
[email protected] | 7a4c685 | 2010-09-16 03:44:22 | [diff] [blame] | 166 | // This is a naked pointer which is set by each entry point. |
| 167 | // The entry point is responsible for ensuring lifetime. |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 168 | ExtensionService* frontend_; |
[email protected] | 7a4c685 | 2010-09-16 03:44:22 | [diff] [blame] | 169 | |
| 170 | // The top-level extensions directory being installed to. |
| 171 | FilePath install_directory_; |
| 172 | |
| 173 | // Whether errors result in noisy alerts. |
| 174 | bool alert_on_error_; |
| 175 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 176 | DISALLOW_COPY_AND_ASSIGN(ExtensionServiceBackend); |
[email protected] | 7a4c685 | 2010-09-16 03:44:22 | [diff] [blame] | 177 | }; |
| 178 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 179 | ExtensionServiceBackend::ExtensionServiceBackend( |
[email protected] | aebe23a3 | 2010-12-10 22:15:48 | [diff] [blame] | 180 | const FilePath& install_directory) |
[email protected] | 7a4c685 | 2010-09-16 03:44:22 | [diff] [blame] | 181 | : frontend_(NULL), |
| 182 | install_directory_(install_directory), |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 183 | alert_on_error_(false) { |
[email protected] | 7a4c685 | 2010-09-16 03:44:22 | [diff] [blame] | 184 | } |
| 185 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 186 | ExtensionServiceBackend::~ExtensionServiceBackend() { |
[email protected] | 7a4c685 | 2010-09-16 03:44:22 | [diff] [blame] | 187 | } |
| 188 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 189 | void ExtensionServiceBackend::LoadSingleExtension( |
| 190 | const FilePath& path_in, scoped_refptr<ExtensionService> frontend) { |
[email protected] | a29a517a | 2011-01-21 21:11:12 | [diff] [blame] | 191 | CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
[email protected] | a8af9fdb | 2010-10-28 21:52:20 | [diff] [blame] | 192 | |
[email protected] | 7a4c685 | 2010-09-16 03:44:22 | [diff] [blame] | 193 | frontend_ = frontend; |
| 194 | |
| 195 | // Explicit UI loads are always noisy. |
| 196 | alert_on_error_ = true; |
| 197 | |
| 198 | FilePath extension_path = path_in; |
| 199 | file_util::AbsolutePath(&extension_path); |
| 200 | |
[email protected] | 7a4c685 | 2010-09-16 03:44:22 | [diff] [blame] | 201 | std::string error; |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 202 | scoped_refptr<const Extension> extension(extension_file_util::LoadExtension( |
[email protected] | 7a4c685 | 2010-09-16 03:44:22 | [diff] [blame] | 203 | extension_path, |
[email protected] | 9288808 | 2010-10-18 19:24:57 | [diff] [blame] | 204 | Extension::LOAD, |
[email protected] | 7a4c685 | 2010-09-16 03:44:22 | [diff] [blame] | 205 | false, // Don't require id |
[email protected] | 542258c | 2011-03-04 21:25:31 | [diff] [blame] | 206 | Extension::ShouldDoStrictErrorChecking(Extension::LOAD), |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 207 | &error)); |
[email protected] | 7a4c685 | 2010-09-16 03:44:22 | [diff] [blame] | 208 | |
| 209 | if (!extension) { |
| 210 | ReportExtensionLoadError(extension_path, error); |
| 211 | return; |
| 212 | } |
| 213 | |
[email protected] | 7a4c685 | 2010-09-16 03:44:22 | [diff] [blame] | 214 | // Report this as an installed extension so that it gets remembered in the |
| 215 | // prefs. |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 216 | BrowserThread::PostTask( |
| 217 | BrowserThread::UI, FROM_HERE, |
[email protected] | 8d888c1 | 2010-11-30 00:00:25 | [diff] [blame] | 218 | NewRunnableMethod(frontend_, |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 219 | &ExtensionService::OnExtensionInstalled, |
[email protected] | 8d888c1 | 2010-11-30 00:00:25 | [diff] [blame] | 220 | extension)); |
[email protected] | 7a4c685 | 2010-09-16 03:44:22 | [diff] [blame] | 221 | } |
| 222 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 223 | void ExtensionServiceBackend::ReportExtensionLoadError( |
[email protected] | 7a4c685 | 2010-09-16 03:44:22 | [diff] [blame] | 224 | const FilePath& extension_path, const std::string &error) { |
[email protected] | a29a517a | 2011-01-21 21:11:12 | [diff] [blame] | 225 | CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 226 | BrowserThread::PostTask( |
| 227 | BrowserThread::UI, FROM_HERE, |
[email protected] | 7a4c685 | 2010-09-16 03:44:22 | [diff] [blame] | 228 | NewRunnableMethod( |
| 229 | frontend_, |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 230 | &ExtensionService::ReportExtensionLoadError, extension_path, |
[email protected] | 7a4c685 | 2010-09-16 03:44:22 | [diff] [blame] | 231 | error, NotificationType::EXTENSION_INSTALL_ERROR, alert_on_error_)); |
| 232 | } |
| 233 | |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 234 | void ExtensionService::CheckExternalUninstall(const std::string& id) { |
[email protected] | a29a517a | 2011-01-21 21:11:12 | [diff] [blame] | 235 | CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | a8af9fdb | 2010-10-28 21:52:20 | [diff] [blame] | 236 | |
[email protected] | 7a4c685 | 2010-09-16 03:44:22 | [diff] [blame] | 237 | // Check if the providers know about this extension. |
[email protected] | 0a60a2e | 2010-10-25 16:15:21 | [diff] [blame] | 238 | ProviderCollection::const_iterator i; |
| 239 | for (i = external_extension_providers_.begin(); |
| 240 | i != external_extension_providers_.end(); ++i) { |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 241 | DCHECK(i->get()->IsReady()); |
[email protected] | 0a60a2e | 2010-10-25 16:15:21 | [diff] [blame] | 242 | if (i->get()->HasExtension(id)) |
| 243 | return; // Yup, known extension, don't uninstall. |
[email protected] | 7a4c685 | 2010-09-16 03:44:22 | [diff] [blame] | 244 | } |
| 245 | |
[email protected] | 7a4c685 | 2010-09-16 03:44:22 | [diff] [blame] | 246 | // This is an external extension that we don't have registered. Uninstall. |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 247 | UninstallExtension(id, true); |
[email protected] | 7a4c685 | 2010-09-16 03:44:22 | [diff] [blame] | 248 | } |
| 249 | |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 250 | void ExtensionService::ClearProvidersForTesting() { |
[email protected] | 7a4c685 | 2010-09-16 03:44:22 | [diff] [blame] | 251 | external_extension_providers_.clear(); |
| 252 | } |
| 253 | |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 254 | void ExtensionService::AddProviderForTesting( |
| 255 | ExternalExtensionProviderInterface* test_provider) { |
[email protected] | a29a517a | 2011-01-21 21:11:12 | [diff] [blame] | 256 | CHECK(test_provider); |
[email protected] | 0a60a2e | 2010-10-25 16:15:21 | [diff] [blame] | 257 | external_extension_providers_.push_back( |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 258 | linked_ptr<ExternalExtensionProviderInterface>(test_provider)); |
[email protected] | 7a4c685 | 2010-09-16 03:44:22 | [diff] [blame] | 259 | } |
| 260 | |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 261 | void ExtensionService::OnExternalExtensionUpdateUrlFound( |
[email protected] | 7a4c685 | 2010-09-16 03:44:22 | [diff] [blame] | 262 | const std::string& id, |
[email protected] | 21a5a67 | 2010-11-04 10:47:42 | [diff] [blame] | 263 | const GURL& update_url, |
| 264 | Extension::Location location) { |
[email protected] | ab22ba4 | 2011-01-14 16:36:38 | [diff] [blame] | 265 | CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 266 | CHECK(Extension::IdIsValid(id)); |
[email protected] | a8af9fdb | 2010-10-28 21:52:20 | [diff] [blame] | 267 | |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 268 | if (GetExtensionById(id, true)) { |
[email protected] | 7a4c685 | 2010-09-16 03:44:22 | [diff] [blame] | 269 | // Already installed. Do not change the update URL that the extension set. |
| 270 | return; |
| 271 | } |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 272 | AddPendingExtensionFromExternalUpdateUrl(id, update_url, location); |
[email protected] | a29a517a | 2011-01-21 21:11:12 | [diff] [blame] | 273 | external_extension_url_added_ |= true; |
[email protected] | 7a4c685 | 2010-09-16 03:44:22 | [diff] [blame] | 274 | } |
| 275 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 276 | bool ExtensionService::IsDownloadFromGallery(const GURL& download_url, |
[email protected] | a29a517a | 2011-01-21 21:11:12 | [diff] [blame] | 277 | const GURL& referrer_url) { |
[email protected] | d307199 | 2010-10-08 15:24:07 | [diff] [blame] | 278 | // Special-case the themes mini-gallery. |
| 279 | // TODO(erikkay) When that gallery goes away, remove this code. |
| 280 | if (IsDownloadFromMiniGallery(download_url) && |
| 281 | StartsWithASCII(referrer_url.spec(), |
| 282 | extension_urls::kMiniGalleryBrowsePrefix, false)) { |
[email protected] | 334e04a | 2010-06-24 23:34:44 | [diff] [blame] | 283 | return true; |
[email protected] | 1debbbb6 | 2010-10-06 17:23:44 | [diff] [blame] | 284 | } |
[email protected] | 473ff6e | 2010-05-12 15:31:55 | [diff] [blame] | 285 | |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 286 | const Extension* download_extension = GetExtensionByWebExtent(download_url); |
| 287 | const Extension* referrer_extension = GetExtensionByWebExtent(referrer_url); |
| 288 | const Extension* webstore_app = GetWebStoreApp(); |
[email protected] | d307199 | 2010-10-08 15:24:07 | [diff] [blame] | 289 | |
| 290 | bool referrer_valid = (referrer_extension == webstore_app); |
| 291 | bool download_valid = (download_extension == webstore_app); |
| 292 | |
| 293 | // If the command-line gallery URL is set, then be a bit more lenient. |
| 294 | GURL store_url = |
| 295 | GURL(CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 296 | switches::kAppsGalleryURL)); |
| 297 | if (!store_url.is_empty()) { |
| 298 | std::string store_tld = |
| 299 | net::RegistryControlledDomainService::GetDomainAndRegistry(store_url); |
| 300 | if (!referrer_valid) { |
| 301 | std::string referrer_tld = |
| 302 | net::RegistryControlledDomainService::GetDomainAndRegistry( |
| 303 | referrer_url); |
| 304 | // The referrer gets stripped when transitioning from https to http, |
| 305 | // or when hitting an unknown test cert and that commonly happens in |
| 306 | // testing environments. Given this, we allow an empty referrer when |
| 307 | // the command-line flag is set. |
| 308 | // Otherwise, the TLD must match the TLD of the command-line url. |
| 309 | referrer_valid = referrer_url.is_empty() || (referrer_tld == store_tld); |
| 310 | } |
| 311 | |
| 312 | if (!download_valid) { |
| 313 | std::string download_tld = |
| 314 | net::RegistryControlledDomainService::GetDomainAndRegistry( |
| 315 | GURL(download_url)); |
| 316 | |
| 317 | // Otherwise, the TLD must match the TLD of the command-line url. |
| 318 | download_valid = (download_tld == store_tld); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | return (referrer_valid && download_valid); |
[email protected] | b7c2f25 | 2009-12-08 00:47:23 | [diff] [blame] | 323 | } |
| 324 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 325 | bool ExtensionService::IsDownloadFromMiniGallery(const GURL& download_url) { |
[email protected] | ac02528 | 2009-12-16 19:16:38 | [diff] [blame] | 326 | return StartsWithASCII(download_url.spec(), |
| 327 | extension_urls::kMiniGalleryDownloadPrefix, |
| 328 | false); // case_sensitive |
| 329 | } |
| 330 | |
[email protected] | d969667 | 2011-03-15 22:45:09 | [diff] [blame] | 331 | const Extension* ExtensionService::GetInstalledApp(const GURL& url) { |
[email protected] | ffb204f2 | 2010-12-05 23:20:27 | [diff] [blame] | 332 | // Check for hosted app. |
[email protected] | d969667 | 2011-03-15 22:45:09 | [diff] [blame] | 333 | const Extension* app = GetExtensionByWebExtent(url); |
| 334 | if (app) |
| 335 | return app; |
[email protected] | ffb204f2 | 2010-12-05 23:20:27 | [diff] [blame] | 336 | |
| 337 | // Check for packaged app. |
[email protected] | d969667 | 2011-03-15 22:45:09 | [diff] [blame] | 338 | app = GetExtensionByURL(url); |
| 339 | if (app && app->is_app()) |
| 340 | return app; |
| 341 | |
| 342 | return NULL; |
| 343 | } |
| 344 | |
| 345 | bool ExtensionService::IsInstalledApp(const GURL& url) { |
| 346 | return !!GetInstalledApp(url); |
[email protected] | ffb204f2 | 2010-12-05 23:20:27 | [diff] [blame] | 347 | } |
| 348 | |
[email protected] | 6aeac834 | 2010-10-01 20:21:18 | [diff] [blame] | 349 | // static |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 350 | bool ExtensionService::UninstallExtensionHelper( |
| 351 | ExtensionService* extensions_service, |
[email protected] | 6aeac834 | 2010-10-01 20:21:18 | [diff] [blame] | 352 | const std::string& extension_id) { |
[email protected] | 6aeac834 | 2010-10-01 20:21:18 | [diff] [blame] | 353 | |
| 354 | // We can't call UninstallExtension with an invalid extension ID, so check it |
| 355 | // first. |
[email protected] | 0dfe05c | 2011-02-23 23:03:36 | [diff] [blame] | 356 | if (extensions_service->GetExtensionById(extension_id, true) || |
| 357 | extensions_service->GetTerminatedExtension(extension_id)) { |
[email protected] | 6aeac834 | 2010-10-01 20:21:18 | [diff] [blame] | 358 | extensions_service->UninstallExtension(extension_id, false); |
| 359 | } else { |
| 360 | LOG(WARNING) << "Attempted uninstallation of non-existent extension with " |
[email protected] | a8af9fdb | 2010-10-28 21:52:20 | [diff] [blame] | 361 | << "id: " << extension_id; |
[email protected] | 6aeac834 | 2010-10-01 20:21:18 | [diff] [blame] | 362 | return false; |
| 363 | } |
| 364 | |
| 365 | return true; |
| 366 | } |
| 367 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 368 | ExtensionService::ExtensionService(Profile* profile, |
[email protected] | f0841cd | 2011-01-19 15:07:24 | [diff] [blame] | 369 | const CommandLine* command_line, |
| 370 | const FilePath& install_directory, |
| 371 | ExtensionPrefs* extension_prefs, |
| 372 | bool autoupdate_enabled) |
[email protected] | 6ef635e4 | 2009-07-26 06:16:12 | [diff] [blame] | 373 | : profile_(profile), |
[email protected] | 73c4793 | 2010-12-06 18:13:43 | [diff] [blame] | 374 | extension_prefs_(extension_prefs), |
[email protected] | a9b00ac | 2009-06-25 21:03:23 | [diff] [blame] | 375 | install_directory_(install_directory), |
[email protected] | 6d60703b | 2009-08-29 01:29:23 | [diff] [blame] | 376 | extensions_enabled_(true), |
[email protected] | e81dba3 | 2009-06-19 20:19:13 | [diff] [blame] | 377 | show_extensions_prompts_(true), |
[email protected] | 3ecda25 | 2010-11-18 19:50:55 | [diff] [blame] | 378 | ready_(false), |
[email protected] | ec5b50d | 2010-10-09 16:35:18 | [diff] [blame] | 379 | ALLOW_THIS_IN_INITIALIZER_LIST(toolbar_model_(this)), |
[email protected] | 2fc0f12 | 2011-01-04 23:44:30 | [diff] [blame] | 380 | default_apps_(profile->GetPrefs(), |
| 381 | g_browser_process->GetApplicationLocale()), |
[email protected] | b3d62312b1 | 2010-10-14 21:10:18 | [diff] [blame] | 382 | event_routers_initialized_(false) { |
[email protected] | a29a517a | 2011-01-21 21:11:12 | [diff] [blame] | 383 | CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | a8af9fdb | 2010-10-28 21:52:20 | [diff] [blame] | 384 | |
[email protected] | 36a784c | 2009-06-23 06:21:08 | [diff] [blame] | 385 | // Figure out if extension installation should be enabled. |
[email protected] | 6d60703b | 2009-08-29 01:29:23 | [diff] [blame] | 386 | if (command_line->HasSwitch(switches::kDisableExtensions)) { |
| 387 | extensions_enabled_ = false; |
| 388 | } else if (profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions)) { |
| 389 | extensions_enabled_ = false; |
[email protected] | 6b75ec3 | 2009-08-14 06:37:18 | [diff] [blame] | 390 | } |
[email protected] | 36a784c | 2009-06-23 06:21:08 | [diff] [blame] | 391 | |
[email protected] | a4ed628 | 2009-12-14 20:51:16 | [diff] [blame] | 392 | registrar_.Add(this, NotificationType::EXTENSION_PROCESS_TERMINATED, |
[email protected] | bc535ee5 | 2010-08-31 18:40:32 | [diff] [blame] | 393 | NotificationService::AllSources()); |
[email protected] | 2fb7dc98 | 2010-09-29 12:24:28 | [diff] [blame] | 394 | pref_change_registrar_.Init(profile->GetPrefs()); |
| 395 | pref_change_registrar_.Add(prefs::kExtensionInstallAllowList, this); |
| 396 | pref_change_registrar_.Add(prefs::kExtensionInstallDenyList, this); |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 397 | |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 398 | // Set up the ExtensionUpdater |
| 399 | if (autoupdate_enabled) { |
| 400 | int update_frequency = kDefaultUpdateFrequencySeconds; |
| 401 | if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) { |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 402 | base::StringToInt(command_line->GetSwitchValueASCII( |
| 403 | switches::kExtensionsUpdateFrequency), |
| 404 | &update_frequency); |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 405 | } |
[email protected] | 2fb7dc98 | 2010-09-29 12:24:28 | [diff] [blame] | 406 | updater_ = new ExtensionUpdater(this, |
| 407 | profile->GetPrefs(), |
| 408 | update_frequency); |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 409 | } |
| 410 | |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 411 | backend_ = new ExtensionServiceBackend(install_directory_); |
| 412 | |
[email protected] | 87353134 | 2011-03-09 12:16:05 | [diff] [blame] | 413 | if (extensions_enabled()) { |
| 414 | ExternalExtensionProviderImpl::CreateExternalProviders( |
| 415 | this, profile_, &external_extension_providers_); |
| 416 | } |
[email protected] | b671760b | 2010-07-15 21:13:47 | [diff] [blame] | 417 | |
[email protected] | aa96d3a | 2010-08-21 08:45:25 | [diff] [blame] | 418 | // Use monochrome icons for Omnibox icons. |
[email protected] | 29d0d4ac | 2010-09-08 21:10:31 | [diff] [blame] | 419 | omnibox_popup_icon_manager_.set_monochrome(true); |
[email protected] | b671760b | 2010-07-15 21:13:47 | [diff] [blame] | 420 | omnibox_icon_manager_.set_monochrome(true); |
[email protected] | 29d0d4ac | 2010-09-08 21:10:31 | [diff] [blame] | 421 | omnibox_icon_manager_.set_padding(gfx::Insets(0, kOmniboxIconPaddingLeft, |
| 422 | 0, kOmniboxIconPaddingRight)); |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 423 | } |
| 424 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 425 | const ExtensionList* ExtensionService::extensions() const { |
[email protected] | ddb1e5a | 2010-12-13 20:10:45 | [diff] [blame] | 426 | return &extensions_; |
| 427 | } |
| 428 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 429 | const ExtensionList* ExtensionService::disabled_extensions() const { |
[email protected] | ddb1e5a | 2010-12-13 20:10:45 | [diff] [blame] | 430 | return &disabled_extensions_; |
| 431 | } |
| 432 | |
[email protected] | bb7f4095 | 2011-01-13 00:21:20 | [diff] [blame] | 433 | const ExtensionList* ExtensionService::terminated_extensions() const { |
| 434 | return &terminated_extensions_; |
| 435 | } |
| 436 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 437 | const PendingExtensionMap& ExtensionService::pending_extensions() const { |
[email protected] | ddb1e5a | 2010-12-13 20:10:45 | [diff] [blame] | 438 | return pending_extensions_; |
| 439 | } |
| 440 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 441 | bool ExtensionService::HasInstalledExtensions() { |
[email protected] | ddb1e5a | 2010-12-13 20:10:45 | [diff] [blame] | 442 | return !(extensions_.empty() && disabled_extensions_.empty()); |
| 443 | } |
| 444 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 445 | ExtensionService::~ExtensionService() { |
[email protected] | 2fb7dc98 | 2010-09-29 12:24:28 | [diff] [blame] | 446 | DCHECK(!profile_); // Profile should have told us it's going away. |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 447 | UnloadAllExtensions(); |
[email protected] | 94b4ffc | 2011-02-04 05:55:17 | [diff] [blame] | 448 | |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 449 | ProviderCollection::const_iterator i; |
| 450 | for (i = external_extension_providers_.begin(); |
| 451 | i != external_extension_providers_.end(); ++i) { |
| 452 | ExternalExtensionProviderInterface* provider = i->get(); |
| 453 | provider->ServiceShutdown(); |
| 454 | } |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 455 | } |
| 456 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 457 | void ExtensionService::InitEventRouters() { |
[email protected] | b3d62312b1 | 2010-10-14 21:10:18 | [diff] [blame] | 458 | if (event_routers_initialized_) |
| 459 | return; |
| 460 | |
[email protected] | c5ae74ab | 2010-04-15 18:14:37 | [diff] [blame] | 461 | ExtensionHistoryEventRouter::GetInstance()->ObserveProfile(profile_); |
| 462 | ExtensionAccessibilityEventRouter::GetInstance()->ObserveProfile(profile_); |
[email protected] | 97d2f1d | 2011-01-15 00:41:08 | [diff] [blame] | 463 | browser_event_router_.reset(new ExtensionBrowserEventRouter(profile_)); |
| 464 | browser_event_router_->Init(); |
[email protected] | 118de789 | 2011-03-16 13:31:40 | [diff] [blame] | 465 | preference_event_router_.reset(new ExtensionPreferenceEventRouter(profile_)); |
[email protected] | 8e8bb6d | 2010-12-13 08:18:55 | [diff] [blame] | 466 | ExtensionBookmarkEventRouter::GetInstance()->Observe( |
[email protected] | c5ae74ab | 2010-04-15 18:14:37 | [diff] [blame] | 467 | profile_->GetBookmarkModel()); |
[email protected] | 2c5e1e1 | 2010-06-10 13:14:44 | [diff] [blame] | 468 | ExtensionCookiesEventRouter::GetInstance()->Init(); |
[email protected] | 7596ce7 | 2010-08-30 05:10:46 | [diff] [blame] | 469 | ExtensionManagementEventRouter::GetInstance()->Init(); |
[email protected] | 8a661f8 | 2010-10-19 21:47:11 | [diff] [blame] | 470 | ExtensionProcessesEventRouter::GetInstance()->ObserveProfile(profile_); |
[email protected] | 784688a6 | 2010-09-13 07:06:52 | [diff] [blame] | 471 | ExtensionWebNavigationEventRouter::GetInstance()->Init(); |
[email protected] | b3d62312b1 | 2010-10-14 21:10:18 | [diff] [blame] | 472 | event_routers_initialized_ = true; |
[email protected] | c5ae74ab | 2010-04-15 18:14:37 | [diff] [blame] | 473 | } |
| 474 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 475 | const Extension* ExtensionService::GetExtensionById(const std::string& id, |
[email protected] | 78994ab0 | 2010-12-08 18:06:44 | [diff] [blame] | 476 | bool include_disabled) { |
| 477 | return GetExtensionByIdInternal(id, true, include_disabled); |
| 478 | } |
| 479 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 480 | void ExtensionService::Init() { |
[email protected] | a29a517a | 2011-01-21 21:11:12 | [diff] [blame] | 481 | CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | a8af9fdb | 2010-10-28 21:52:20 | [diff] [blame] | 482 | |
[email protected] | fa6a910 | 2010-11-22 15:38:50 | [diff] [blame] | 483 | DCHECK(!ready_); // Can't redo init. |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 484 | DCHECK_EQ(extensions_.size(), 0u); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 485 | |
[email protected] | 95dd38f | 2009-10-20 20:09:15 | [diff] [blame] | 486 | // Hack: we need to ensure the ResourceDispatcherHost is ready before we load |
| 487 | // the first extension, because its members listen for loaded notifications. |
| 488 | g_browser_process->resource_dispatcher_host(); |
| 489 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 490 | LoadAllExtensions(); |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 491 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 492 | // TODO(erikkay) this should probably be deferred to a future point |
| 493 | // rather than running immediately at startup. |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 494 | CheckForExternalUpdates(); |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 495 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 496 | // TODO(erikkay) this should probably be deferred as well. |
| 497 | GarbageCollectExtensions(); |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 498 | } |
| 499 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 500 | void ExtensionService::UpdateExtension(const std::string& id, |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 501 | const FilePath& extension_path, |
| 502 | const GURL& download_url) { |
[email protected] | a29a517a | 2011-01-21 21:11:12 | [diff] [blame] | 503 | CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | a8af9fdb | 2010-10-28 21:52:20 | [diff] [blame] | 504 | |
[email protected] | aa14270 | 2010-03-26 01:26:33 | [diff] [blame] | 505 | PendingExtensionMap::const_iterator it = pending_extensions_.find(id); |
[email protected] | 8ef78fd | 2010-08-19 17:14:32 | [diff] [blame] | 506 | bool is_pending_extension = (it != pending_extensions_.end()); |
| 507 | |
[email protected] | 5eb375e9 | 2010-11-26 07:50:41 | [diff] [blame] | 508 | const Extension* extension = GetExtensionByIdInternal(id, true, true); |
| 509 | if (!is_pending_extension && !extension) { |
[email protected] | aa14270 | 2010-03-26 01:26:33 | [diff] [blame] | 510 | LOG(WARNING) << "Will not update extension " << id |
| 511 | << " because it is not installed or pending"; |
| 512 | // Delete extension_path since we're not creating a CrxInstaller |
| 513 | // that would do it for us. |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 514 | BrowserThread::PostTask( |
| 515 | BrowserThread::FILE, FROM_HERE, |
[email protected] | 53da2c96 | 2011-03-03 17:08:05 | [diff] [blame] | 516 | NewRunnableFunction( |
| 517 | extension_file_util::DeleteFile, extension_path, false)); |
[email protected] | 4c96793 | 2009-07-31 01:15:49 | [diff] [blame] | 518 | return; |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 519 | } |
| 520 | |
[email protected] | aa14270 | 2010-03-26 01:26:33 | [diff] [blame] | 521 | // We want a silent install only for non-pending extensions and |
| 522 | // pending extensions that have install_silently set. |
| 523 | ExtensionInstallUI* client = |
[email protected] | 483d1ff | 2011-03-03 17:12:02 | [diff] [blame] | 524 | (!is_pending_extension || it->second.install_silently()) ? |
[email protected] | aa14270 | 2010-03-26 01:26:33 | [diff] [blame] | 525 | NULL : new ExtensionInstallUI(profile_); |
| 526 | |
[email protected] | 6dfbbf8 | 2010-03-12 23:09:16 | [diff] [blame] | 527 | scoped_refptr<CrxInstaller> installer( |
[email protected] | bb46153 | 2010-11-26 21:50:23 | [diff] [blame] | 528 | new CrxInstaller(this, // frontend |
[email protected] | aa14270 | 2010-03-26 01:26:33 | [diff] [blame] | 529 | client)); |
[email protected] | 6dfbbf8 | 2010-03-12 23:09:16 | [diff] [blame] | 530 | installer->set_expected_id(id); |
[email protected] | ec5b50d | 2010-10-09 16:35:18 | [diff] [blame] | 531 | if (is_pending_extension) |
[email protected] | 483d1ff | 2011-03-03 17:12:02 | [diff] [blame] | 532 | installer->set_install_source(it->second.install_source()); |
[email protected] | 5eb375e9 | 2010-11-26 07:50:41 | [diff] [blame] | 533 | else if (extension) |
| 534 | installer->set_install_source(extension->location()); |
[email protected] | 6dfbbf8 | 2010-03-12 23:09:16 | [diff] [blame] | 535 | installer->set_delete_source(true); |
[email protected] | 5c851620 | 2010-03-18 21:43:34 | [diff] [blame] | 536 | installer->set_original_url(download_url); |
[email protected] | 6dfbbf8 | 2010-03-12 23:09:16 | [diff] [blame] | 537 | installer->InstallCrx(extension_path); |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 538 | } |
| 539 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 540 | void ExtensionService::AddPendingExtensionFromSync( |
[email protected] | aa14270 | 2010-03-26 01:26:33 | [diff] [blame] | 541 | const std::string& id, const GURL& update_url, |
[email protected] | 483d1ff | 2011-03-03 17:12:02 | [diff] [blame] | 542 | PendingExtensionInfo::ShouldAllowInstallPredicate should_allow_install, |
[email protected] | 8ef78fd | 2010-08-19 17:14:32 | [diff] [blame] | 543 | bool install_silently, bool enable_on_install, |
| 544 | bool enable_incognito_on_install) { |
[email protected] | aa14270 | 2010-03-26 01:26:33 | [diff] [blame] | 545 | if (GetExtensionByIdInternal(id, true, true)) { |
[email protected] | efee9f26 | 2010-03-29 21:26:25 | [diff] [blame] | 546 | LOG(DFATAL) << "Trying to add pending extension " << id |
| 547 | << " which already exists"; |
[email protected] | aa14270 | 2010-03-26 01:26:33 | [diff] [blame] | 548 | return; |
| 549 | } |
[email protected] | ec5b50d | 2010-10-09 16:35:18 | [diff] [blame] | 550 | |
[email protected] | 483d1ff | 2011-03-03 17:12:02 | [diff] [blame] | 551 | AddPendingExtensionInternal(id, update_url, should_allow_install, true, |
[email protected] | ec5b50d | 2010-10-09 16:35:18 | [diff] [blame] | 552 | install_silently, enable_on_install, |
| 553 | enable_incognito_on_install, |
| 554 | Extension::INTERNAL); |
[email protected] | aa14270 | 2010-03-26 01:26:33 | [diff] [blame] | 555 | } |
| 556 | |
[email protected] | 7fa19f8 | 2010-12-21 19:40:08 | [diff] [blame] | 557 | namespace { |
| 558 | |
| 559 | bool AlwaysInstall(const Extension& extension) { |
| 560 | return true; |
| 561 | } |
| 562 | |
| 563 | } // namespace |
| 564 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 565 | void ExtensionService::AddPendingExtensionFromExternalUpdateUrl( |
[email protected] | 21a5a67 | 2010-11-04 10:47:42 | [diff] [blame] | 566 | const std::string& id, const GURL& update_url, |
| 567 | Extension::Location location) { |
[email protected] | 8ef78fd | 2010-08-19 17:14:32 | [diff] [blame] | 568 | const bool kIsFromSync = false; |
| 569 | const bool kInstallSilently = true; |
| 570 | const bool kEnableOnInstall = true; |
[email protected] | a424d84c | 2010-09-24 09:31:15 | [diff] [blame] | 571 | const bool kEnableIncognitoOnInstall = false; |
[email protected] | 683d070 | 2010-12-06 16:25:57 | [diff] [blame] | 572 | if (extension_prefs_->IsExtensionKilled(id)) |
| 573 | return; |
[email protected] | 8ef78fd | 2010-08-19 17:14:32 | [diff] [blame] | 574 | |
| 575 | if (GetExtensionByIdInternal(id, true, true)) { |
| 576 | LOG(DFATAL) << "Trying to add extension " << id |
| 577 | << " by external update, but it is already installed."; |
| 578 | return; |
| 579 | } |
| 580 | |
[email protected] | 7fa19f8 | 2010-12-21 19:40:08 | [diff] [blame] | 581 | AddPendingExtensionInternal(id, update_url, &AlwaysInstall, |
| 582 | kIsFromSync, kInstallSilently, |
| 583 | kEnableOnInstall, kEnableIncognitoOnInstall, |
[email protected] | 21a5a67 | 2010-11-04 10:47:42 | [diff] [blame] | 584 | location); |
[email protected] | ec5b50d | 2010-10-09 16:35:18 | [diff] [blame] | 585 | } |
| 586 | |
[email protected] | 7fa19f8 | 2010-12-21 19:40:08 | [diff] [blame] | 587 | namespace { |
| 588 | |
| 589 | bool IsApp(const Extension& extension) { |
| 590 | return extension.is_app(); |
| 591 | } |
| 592 | |
| 593 | } // namespace |
| 594 | |
| 595 | // TODO(akalin): Change DefaultAppList to DefaultExtensionList and |
| 596 | // remove the IsApp() check. |
| 597 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 598 | void ExtensionService::AddPendingExtensionFromDefaultAppList( |
[email protected] | ec5b50d | 2010-10-09 16:35:18 | [diff] [blame] | 599 | const std::string& id) { |
[email protected] | ec5b50d | 2010-10-09 16:35:18 | [diff] [blame] | 600 | const bool kIsFromSync = false; |
| 601 | const bool kInstallSilently = true; |
| 602 | const bool kEnableOnInstall = true; |
| 603 | const bool kEnableIncognitoOnInstall = true; |
| 604 | |
| 605 | // This can legitimately happen if the user manually installed one of the |
| 606 | // default apps before this code ran. |
| 607 | if (GetExtensionByIdInternal(id, true, true)) |
| 608 | return; |
| 609 | |
[email protected] | 7fa19f8 | 2010-12-21 19:40:08 | [diff] [blame] | 610 | AddPendingExtensionInternal(id, GURL(), &IsApp, |
| 611 | kIsFromSync, kInstallSilently, |
| 612 | kEnableOnInstall, kEnableIncognitoOnInstall, |
[email protected] | ec5b50d | 2010-10-09 16:35:18 | [diff] [blame] | 613 | Extension::INTERNAL); |
[email protected] | 8ef78fd | 2010-08-19 17:14:32 | [diff] [blame] | 614 | } |
| 615 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 616 | void ExtensionService::AddPendingExtensionInternal( |
[email protected] | aa14270 | 2010-03-26 01:26:33 | [diff] [blame] | 617 | const std::string& id, const GURL& update_url, |
[email protected] | 483d1ff | 2011-03-03 17:12:02 | [diff] [blame] | 618 | PendingExtensionInfo::ShouldAllowInstallPredicate should_allow_install, |
[email protected] | 8ef78fd | 2010-08-19 17:14:32 | [diff] [blame] | 619 | bool is_from_sync, bool install_silently, |
[email protected] | ec5b50d | 2010-10-09 16:35:18 | [diff] [blame] | 620 | bool enable_on_install, bool enable_incognito_on_install, |
| 621 | Extension::Location install_source) { |
[email protected] | a29a517a | 2011-01-21 21:11:12 | [diff] [blame] | 622 | CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | a8af9fdb | 2010-10-28 21:52:20 | [diff] [blame] | 623 | |
[email protected] | 1afaf2a5 | 2010-11-02 19:29:17 | [diff] [blame] | 624 | // If a non-sync update is pending, a sync request should not |
| 625 | // overwrite it. This is important for external extensions. |
| 626 | // If an external extension download is pending, and the user has |
| 627 | // the extension in their sync profile, the install should set the |
| 628 | // type to be external. An external extension should not be |
| 629 | // rejected if it fails the safty checks for a syncable extension. |
| 630 | // TODO(skerner): Work out other potential overlapping conditions. |
[email protected] | a315ba9 | 2010-11-16 14:12:21 | [diff] [blame] | 631 | // (crbug.com/61000) |
[email protected] | 1afaf2a5 | 2010-11-02 19:29:17 | [diff] [blame] | 632 | PendingExtensionMap::iterator it = pending_extensions_.find(id); |
| 633 | if (it != pending_extensions_.end()) { |
| 634 | VLOG(1) << "Extension id " << id |
| 635 | << " was entered for update more than once." |
[email protected] | 483d1ff | 2011-03-03 17:12:02 | [diff] [blame] | 636 | << " old is_from_sync = " << it->second.is_from_sync() |
[email protected] | 1afaf2a5 | 2010-11-02 19:29:17 | [diff] [blame] | 637 | << " new is_from_sync = " << is_from_sync; |
[email protected] | 483d1ff | 2011-03-03 17:12:02 | [diff] [blame] | 638 | if (!it->second.is_from_sync() && is_from_sync) |
[email protected] | 1afaf2a5 | 2010-11-02 19:29:17 | [diff] [blame] | 639 | return; |
| 640 | } |
| 641 | |
[email protected] | aa14270 | 2010-03-26 01:26:33 | [diff] [blame] | 642 | pending_extensions_[id] = |
[email protected] | 483d1ff | 2011-03-03 17:12:02 | [diff] [blame] | 643 | PendingExtensionInfo(update_url, should_allow_install, |
[email protected] | 7fa19f8 | 2010-12-21 19:40:08 | [diff] [blame] | 644 | is_from_sync, install_silently, enable_on_install, |
[email protected] | ec5b50d | 2010-10-09 16:35:18 | [diff] [blame] | 645 | enable_incognito_on_install, install_source); |
[email protected] | aa14270 | 2010-03-26 01:26:33 | [diff] [blame] | 646 | } |
| 647 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 648 | void ExtensionService::ReloadExtension(const std::string& extension_id) { |
[email protected] | a29a517a | 2011-01-21 21:11:12 | [diff] [blame] | 649 | CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | b65272f | 2009-08-31 15:47:06 | [diff] [blame] | 650 | FilePath path; |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 651 | const Extension* current_extension = GetExtensionById(extension_id, false); |
[email protected] | 9cddd470 | 2009-07-27 22:09:40 | [diff] [blame] | 652 | |
[email protected] | f17dbd4 | 2010-08-16 23:21:10 | [diff] [blame] | 653 | // Disable the extension if it's loaded. It might not be loaded if it crashed. |
[email protected] | b65272f | 2009-08-31 15:47:06 | [diff] [blame] | 654 | if (current_extension) { |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 655 | // If the extension has an inspector open for its background page, detach |
| 656 | // the inspector and hang onto a cookie for it, so that we can reattach |
| 657 | // later. |
| 658 | ExtensionProcessManager* manager = profile_->GetExtensionProcessManager(); |
| 659 | ExtensionHost* host = manager->GetBackgroundHostForExtension( |
| 660 | current_extension); |
| 661 | if (host) { |
| 662 | // Look for an open inspector for the background page. |
| 663 | int devtools_cookie = DevToolsManager::GetInstance()->DetachClientHost( |
| 664 | host->render_view_host()); |
| 665 | if (devtools_cookie >= 0) |
| 666 | orphaned_dev_tools_[extension_id] = devtools_cookie; |
| 667 | } |
| 668 | |
[email protected] | b65272f | 2009-08-31 15:47:06 | [diff] [blame] | 669 | path = current_extension->path(); |
[email protected] | f17dbd4 | 2010-08-16 23:21:10 | [diff] [blame] | 670 | DisableExtension(extension_id); |
| 671 | disabled_extension_paths_[extension_id] = path; |
[email protected] | 1eb17508 | 2010-02-10 09:26:16 | [diff] [blame] | 672 | } else { |
| 673 | path = unloaded_extension_paths_[extension_id]; |
[email protected] | b65272f | 2009-08-31 15:47:06 | [diff] [blame] | 674 | } |
| 675 | |
[email protected] | e6090e4 | 2010-03-23 22:44:08 | [diff] [blame] | 676 | // Check the installed extensions to see if what we're reloading was already |
| 677 | // installed. |
| 678 | scoped_ptr<ExtensionInfo> installed_extension( |
| 679 | extension_prefs_->GetInstalledExtensionInfo(extension_id)); |
| 680 | if (installed_extension.get() && |
| 681 | installed_extension->extension_manifest.get()) { |
| 682 | LoadInstalledExtension(*installed_extension, false); |
| 683 | } else { |
| 684 | // We should always be able to remember the extension's path. If it's not in |
| 685 | // the map, someone failed to update |unloaded_extension_paths_|. |
| 686 | CHECK(!path.empty()); |
| 687 | LoadExtension(path); |
| 688 | } |
[email protected] | 9cddd470 | 2009-07-27 22:09:40 | [diff] [blame] | 689 | } |
| 690 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 691 | void ExtensionService::UninstallExtension(const std::string& extension_id, |
[email protected] | 604322d | 2011-03-22 16:51:56 | [diff] [blame] | 692 | bool external_uninstall) { |
[email protected] | a29a517a | 2011-01-21 21:11:12 | [diff] [blame] | 693 | CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | a8af9fdb | 2010-10-28 21:52:20 | [diff] [blame] | 694 | |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 695 | const Extension* extension = |
| 696 | GetExtensionByIdInternal(extension_id, true, true); |
[email protected] | 0dfe05c | 2011-02-23 23:03:36 | [diff] [blame] | 697 | if (!extension) |
| 698 | extension = GetTerminatedExtension(extension_id); |
[email protected] | 631cf82 | 2009-05-15 07:01:25 | [diff] [blame] | 699 | |
[email protected] | e7afe245 | 2010-08-22 16:19:13 | [diff] [blame] | 700 | // Callers should not send us nonexistent extensions. |
[email protected] | aebe23a3 | 2010-12-10 22:15:48 | [diff] [blame] | 701 | CHECK(extension); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 702 | |
[email protected] | 831aa21 | 2010-03-26 13:55:19 | [diff] [blame] | 703 | // Get hold of information we need after unloading, since the extension |
| 704 | // pointer will be invalid then. |
| 705 | GURL extension_url(extension->url()); |
| 706 | Extension::Location location(extension->location()); |
[email protected] | 21103034 | 2010-09-30 18:41:06 | [diff] [blame] | 707 | UninstalledExtensionInfo uninstalled_extension_info(*extension); |
[email protected] | 831aa21 | 2010-03-26 13:55:19 | [diff] [blame] | 708 | |
[email protected] | 9b21765 | 2010-10-08 22:04:23 | [diff] [blame] | 709 | UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType", |
[email protected] | 7fa19f8 | 2010-12-21 19:40:08 | [diff] [blame] | 710 | extension->GetType(), 100); |
[email protected] | 9b21765 | 2010-10-08 22:04:23 | [diff] [blame] | 711 | |
[email protected] | 831aa21 | 2010-03-26 13:55:19 | [diff] [blame] | 712 | // Also copy the extension identifier since the reference might have been |
| 713 | // obtained via Extension::id(). |
| 714 | std::string extension_id_copy(extension_id); |
| 715 | |
[email protected] | 56ad379 | 2010-05-28 17:45:33 | [diff] [blame] | 716 | if (profile_->GetTemplateURLModel()) |
| 717 | profile_->GetTemplateURLModel()->UnregisterExtensionKeyword(extension); |
| 718 | |
[email protected] | 831aa21 | 2010-03-26 13:55:19 | [diff] [blame] | 719 | // Unload before doing more cleanup to ensure that nothing is hanging on to |
| 720 | // any of these resources. |
[email protected] | a9f39a31 | 2010-12-23 22:14:27 | [diff] [blame] | 721 | UnloadExtension(extension_id, UnloadedExtensionInfo::UNINSTALL); |
[email protected] | 831aa21 | 2010-03-26 13:55:19 | [diff] [blame] | 722 | |
| 723 | extension_prefs_->OnExtensionUninstalled(extension_id_copy, location, |
| 724 | external_uninstall); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 725 | |
| 726 | // Tell the backend to start deleting installed extensions on the file thread. |
[email protected] | 831aa21 | 2010-03-26 13:55:19 | [diff] [blame] | 727 | if (Extension::LOAD != location) { |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 728 | BrowserThread::PostTask( |
| 729 | BrowserThread::FILE, FROM_HERE, |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 730 | NewRunnableFunction( |
[email protected] | ca3dbf5 | 2010-05-19 22:27:06 | [diff] [blame] | 731 | &extension_file_util::UninstallExtension, |
| 732 | install_directory_, |
| 733 | extension_id_copy)); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 734 | } |
| 735 | |
[email protected] | c10da4b0 | 2010-03-25 14:38:32 | [diff] [blame] | 736 | ClearExtensionData(extension_url); |
[email protected] | 0dfe05c | 2011-02-23 23:03:36 | [diff] [blame] | 737 | UntrackTerminatedExtension(extension_id); |
[email protected] | 21103034 | 2010-09-30 18:41:06 | [diff] [blame] | 738 | |
| 739 | // Notify interested parties that we've uninstalled this extension. |
| 740 | NotificationService::current()->Notify( |
| 741 | NotificationType::EXTENSION_UNINSTALLED, |
| 742 | Source<Profile>(profile_), |
| 743 | Details<UninstalledExtensionInfo>(&uninstalled_extension_info)); |
[email protected] | c10da4b0 | 2010-03-25 14:38:32 | [diff] [blame] | 744 | } |
| 745 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 746 | void ExtensionService::ClearExtensionData(const GURL& extension_url) { |
[email protected] | c10da4b0 | 2010-03-25 14:38:32 | [diff] [blame] | 747 | scoped_refptr<ExtensionDataDeleter> deleter( |
| 748 | new ExtensionDataDeleter(profile_, extension_url)); |
| 749 | deleter->StartDeleting(); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 750 | } |
| 751 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 752 | void ExtensionService::EnableExtension(const std::string& extension_id) { |
[email protected] | a29a517a | 2011-01-21 21:11:12 | [diff] [blame] | 753 | CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | a8af9fdb | 2010-10-28 21:52:20 | [diff] [blame] | 754 | |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 755 | const Extension* extension = |
| 756 | GetExtensionByIdInternal(extension_id, false, true); |
[email protected] | f681c78 | 2010-11-19 11:19:39 | [diff] [blame] | 757 | if (!extension) |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 758 | return; |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 759 | |
[email protected] | e8c729a | 2010-03-09 19:55:19 | [diff] [blame] | 760 | extension_prefs_->SetExtensionState(extension, Extension::ENABLED); |
[email protected] | 1784e83a | 2009-09-08 21:01:52 | [diff] [blame] | 761 | |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 762 | // Move it over to the enabled list. |
[email protected] | 00cd9c4 | 2010-11-02 20:15:57 | [diff] [blame] | 763 | extensions_.push_back(make_scoped_refptr(extension)); |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 764 | ExtensionList::iterator iter = std::find(disabled_extensions_.begin(), |
| 765 | disabled_extensions_.end(), |
| 766 | extension); |
| 767 | disabled_extensions_.erase(iter); |
| 768 | |
[email protected] | f681c78 | 2010-11-19 11:19:39 | [diff] [blame] | 769 | // Make sure any browser action contained within it is not hidden. |
| 770 | extension_prefs_->SetBrowserActionVisibility(extension, true); |
| 771 | |
[email protected] | 8f9d4eb | 2011-02-05 01:39:10 | [diff] [blame] | 772 | ExtensionWebUI::RegisterChromeURLOverrides(profile_, |
[email protected] | 86c008e8 | 2009-08-28 20:26:05 | [diff] [blame] | 773 | extension->GetChromeURLOverrides()); |
| 774 | |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 775 | NotifyExtensionLoaded(extension); |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 776 | UpdateActiveExtensionsInCrashReporter(); |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 777 | } |
| 778 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 779 | void ExtensionService::DisableExtension(const std::string& extension_id) { |
[email protected] | a29a517a | 2011-01-21 21:11:12 | [diff] [blame] | 780 | CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | a8af9fdb | 2010-10-28 21:52:20 | [diff] [blame] | 781 | |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 782 | const Extension* extension = |
| 783 | GetExtensionByIdInternal(extension_id, true, false); |
[email protected] | b2ba996 | 2009-12-10 20:10:15 | [diff] [blame] | 784 | // The extension may have been disabled already. |
| 785 | if (!extension) |
[email protected] | 1784e83a | 2009-09-08 21:01:52 | [diff] [blame] | 786 | return; |
[email protected] | 1784e83a | 2009-09-08 21:01:52 | [diff] [blame] | 787 | |
[email protected] | e8c729a | 2010-03-09 19:55:19 | [diff] [blame] | 788 | extension_prefs_->SetExtensionState(extension, Extension::DISABLED); |
[email protected] | 1784e83a | 2009-09-08 21:01:52 | [diff] [blame] | 789 | |
| 790 | // Move it over to the disabled list. |
[email protected] | 00cd9c4 | 2010-11-02 20:15:57 | [diff] [blame] | 791 | disabled_extensions_.push_back(make_scoped_refptr(extension)); |
[email protected] | 1784e83a | 2009-09-08 21:01:52 | [diff] [blame] | 792 | ExtensionList::iterator iter = std::find(extensions_.begin(), |
| 793 | extensions_.end(), |
| 794 | extension); |
| 795 | extensions_.erase(iter); |
| 796 | |
[email protected] | 8f9d4eb | 2011-02-05 01:39:10 | [diff] [blame] | 797 | ExtensionWebUI::UnregisterChromeURLOverrides(profile_, |
[email protected] | 1784e83a | 2009-09-08 21:01:52 | [diff] [blame] | 798 | extension->GetChromeURLOverrides()); |
| 799 | |
[email protected] | a9f39a31 | 2010-12-23 22:14:27 | [diff] [blame] | 800 | NotifyExtensionUnloaded(extension, UnloadedExtensionInfo::DISABLE); |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 801 | UpdateActiveExtensionsInCrashReporter(); |
[email protected] | 1784e83a | 2009-09-08 21:01:52 | [diff] [blame] | 802 | } |
| 803 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 804 | void ExtensionService::GrantPermissions(const Extension* extension) { |
[email protected] | 8d888c1 | 2010-11-30 00:00:25 | [diff] [blame] | 805 | CHECK(extension); |
| 806 | |
| 807 | // We only maintain the granted permissions prefs for INTERNAL extensions. |
[email protected] | 139e573 | 2011-03-16 19:29:51 | [diff] [blame] | 808 | CHECK_EQ(Extension::INTERNAL, extension->location()); |
[email protected] | 8d888c1 | 2010-11-30 00:00:25 | [diff] [blame] | 809 | |
| 810 | ExtensionExtent effective_hosts = extension->GetEffectiveHostPermissions(); |
| 811 | extension_prefs_->AddGrantedPermissions(extension->id(), |
| 812 | extension->HasFullPermissions(), |
| 813 | extension->api_permissions(), |
| 814 | effective_hosts); |
| 815 | } |
| 816 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 817 | void ExtensionService::GrantPermissionsAndEnableExtension( |
[email protected] | 8d888c1 | 2010-11-30 00:00:25 | [diff] [blame] | 818 | const Extension* extension) { |
| 819 | CHECK(extension); |
| 820 | GrantPermissions(extension); |
| 821 | extension_prefs_->SetDidExtensionEscalatePermissions(extension, false); |
| 822 | EnableExtension(extension->id()); |
| 823 | } |
| 824 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 825 | void ExtensionService::LoadExtension(const FilePath& extension_path) { |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 826 | BrowserThread::PostTask( |
| 827 | BrowserThread::FILE, FROM_HERE, |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 828 | NewRunnableMethod( |
| 829 | backend_.get(), |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 830 | &ExtensionServiceBackend::LoadSingleExtension, |
| 831 | extension_path, scoped_refptr<ExtensionService>(this))); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 832 | } |
| 833 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 834 | void ExtensionService::LoadComponentExtensions() { |
[email protected] | 1952c7d | 2010-03-04 23:48:34 | [diff] [blame] | 835 | for (RegisteredComponentExtensions::iterator it = |
| 836 | component_extension_manifests_.begin(); |
| 837 | it != component_extension_manifests_.end(); ++it) { |
[email protected] | 61b55b6 | 2011-03-24 09:03:10 | [diff] [blame^] | 838 | LoadComponentExtension(*it); |
[email protected] | 1952c7d | 2010-03-04 23:48:34 | [diff] [blame] | 839 | } |
| 840 | } |
| 841 | |
[email protected] | 61b55b6 | 2011-03-24 09:03:10 | [diff] [blame^] | 842 | void ExtensionService::LoadComponentExtension( |
| 843 | const ComponentExtensionInfo &info) { |
| 844 | JSONStringValueSerializer serializer(info.manifest); |
| 845 | scoped_ptr<Value> manifest(serializer.Deserialize(NULL, NULL)); |
| 846 | if (!manifest.get()) { |
| 847 | DLOG(ERROR) << "Failed to parse manifest for extension"; |
| 848 | return; |
| 849 | } |
| 850 | |
| 851 | std::string error; |
| 852 | scoped_refptr<const Extension> extension(Extension::Create( |
| 853 | info.root_directory, |
| 854 | Extension::COMPONENT, |
| 855 | *static_cast<DictionaryValue*>(manifest.get()), |
| 856 | true, // Require key |
| 857 | Extension::ShouldDoStrictErrorChecking(Extension::COMPONENT), |
| 858 | &error)); |
| 859 | if (!extension.get()) { |
| 860 | NOTREACHED() << error; |
| 861 | return; |
| 862 | } |
| 863 | AddExtension(extension); |
| 864 | } |
| 865 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 866 | void ExtensionService::LoadAllExtensions() { |
[email protected] | a29a517a | 2011-01-21 21:11:12 | [diff] [blame] | 867 | CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | a315ba9 | 2010-11-16 14:12:21 | [diff] [blame] | 868 | |
[email protected] | cc2c343 | 2009-11-06 17:24:36 | [diff] [blame] | 869 | base::TimeTicks start_time = base::TimeTicks::Now(); |
| 870 | |
[email protected] | 1952c7d | 2010-03-04 23:48:34 | [diff] [blame] | 871 | // Load any component extensions. |
| 872 | LoadComponentExtensions(); |
| 873 | |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 874 | // Load the previously installed extensions. |
[email protected] | a315ba9 | 2010-11-16 14:12:21 | [diff] [blame] | 875 | scoped_ptr<ExtensionPrefs::ExtensionsInfo> extensions_info( |
[email protected] | e6090e4 | 2010-03-23 22:44:08 | [diff] [blame] | 876 | extension_prefs_->GetInstalledExtensionsInfo()); |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 877 | |
[email protected] | a315ba9 | 2010-11-16 14:12:21 | [diff] [blame] | 878 | std::vector<int> reload_reason_counts(NUM_MANIFEST_RELOAD_REASONS, 0); |
| 879 | bool should_write_prefs = false; |
| 880 | |
| 881 | for (size_t i = 0; i < extensions_info->size(); ++i) { |
| 882 | ExtensionInfo* info = extensions_info->at(i).get(); |
| 883 | |
| 884 | ManifestReloadReason reload_reason = ShouldReloadExtensionManifest(*info); |
| 885 | ++reload_reason_counts[reload_reason]; |
| 886 | UMA_HISTOGRAM_ENUMERATION("Extensions.ManifestReloadEnumValue", |
| 887 | reload_reason, 100); |
| 888 | |
| 889 | if (reload_reason != NOT_NEEDED) { |
| 890 | // Reloading and extension reads files from disk. We do this on the |
| 891 | // UI thread because reloads should be very rare, and the complexity |
| 892 | // added by delaying the time when the extensions service knows about |
| 893 | // all extensions is significant. See crbug.com/37548 for details. |
| 894 | // |allow_io| disables tests that file operations run on the file |
| 895 | // thread. |
| 896 | base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 897 | |
| 898 | std::string error; |
| 899 | scoped_refptr<const Extension> extension( |
| 900 | extension_file_util::LoadExtension( |
[email protected] | 542258c | 2011-03-04 21:25:31 | [diff] [blame] | 901 | info->extension_path, |
| 902 | info->extension_location, |
| 903 | false, // Don't require key |
| 904 | Extension::ShouldDoStrictErrorChecking(info->extension_location), |
| 905 | &error)); |
[email protected] | a315ba9 | 2010-11-16 14:12:21 | [diff] [blame] | 906 | |
| 907 | if (extension.get()) { |
| 908 | extensions_info->at(i)->extension_manifest.reset( |
| 909 | static_cast<DictionaryValue*>( |
| 910 | extension->manifest_value()->DeepCopy())); |
| 911 | should_write_prefs = true; |
| 912 | } |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 913 | } |
| 914 | } |
| 915 | |
[email protected] | a315ba9 | 2010-11-16 14:12:21 | [diff] [blame] | 916 | for (size_t i = 0; i < extensions_info->size(); ++i) { |
| 917 | LoadInstalledExtension(*extensions_info->at(i), should_write_prefs); |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 918 | } |
| 919 | |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 920 | OnLoadedInstalledExtensions(); |
[email protected] | cc2c343 | 2009-11-06 17:24:36 | [diff] [blame] | 921 | |
[email protected] | a315ba9 | 2010-11-16 14:12:21 | [diff] [blame] | 922 | // The histograms Extensions.ManifestReload* allow us to validate |
| 923 | // the assumption that reloading manifest is a rare event. |
| 924 | UMA_HISTOGRAM_COUNTS_100("Extensions.ManifestReloadNotNeeded", |
| 925 | reload_reason_counts[NOT_NEEDED]); |
| 926 | UMA_HISTOGRAM_COUNTS_100("Extensions.ManifestReloadUnpackedDir", |
| 927 | reload_reason_counts[UNPACKED_DIR]); |
| 928 | UMA_HISTOGRAM_COUNTS_100("Extensions.ManifestReloadNeedsRelocalization", |
| 929 | reload_reason_counts[NEEDS_RELOCALIZATION]); |
| 930 | |
[email protected] | cc2c343 | 2009-11-06 17:24:36 | [diff] [blame] | 931 | UMA_HISTOGRAM_COUNTS_100("Extensions.LoadAll", extensions_.size()); |
| 932 | UMA_HISTOGRAM_COUNTS_100("Extensions.Disabled", disabled_extensions_.size()); |
| 933 | |
[email protected] | 1952c7d | 2010-03-04 23:48:34 | [diff] [blame] | 934 | UMA_HISTOGRAM_TIMES("Extensions.LoadAllTime", |
| 935 | base::TimeTicks::Now() - start_time); |
[email protected] | cc2c343 | 2009-11-06 17:24:36 | [diff] [blame] | 936 | |
[email protected] | 9b21765 | 2010-10-08 22:04:23 | [diff] [blame] | 937 | int app_count = 0; |
| 938 | int hosted_app_count = 0; |
| 939 | int packaged_app_count = 0; |
[email protected] | 1952c7d | 2010-03-04 23:48:34 | [diff] [blame] | 940 | int user_script_count = 0; |
| 941 | int extension_count = 0; |
| 942 | int theme_count = 0; |
| 943 | int external_count = 0; |
| 944 | int page_action_count = 0; |
| 945 | int browser_action_count = 0; |
| 946 | ExtensionList::iterator ex; |
| 947 | for (ex = extensions_.begin(); ex != extensions_.end(); ++ex) { |
[email protected] | 9b21765 | 2010-10-08 22:04:23 | [diff] [blame] | 948 | Extension::Location location = (*ex)->location(); |
[email protected] | 7fa19f8 | 2010-12-21 19:40:08 | [diff] [blame] | 949 | Extension::Type type = (*ex)->GetType(); |
[email protected] | 9b21765 | 2010-10-08 22:04:23 | [diff] [blame] | 950 | if ((*ex)->is_app()) { |
| 951 | UMA_HISTOGRAM_ENUMERATION("Extensions.AppLocation", |
| 952 | location, 100); |
| 953 | } else if (type == Extension::TYPE_EXTENSION) { |
| 954 | UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionLocation", |
| 955 | location, 100); |
| 956 | } |
| 957 | |
[email protected] | 1952c7d | 2010-03-04 23:48:34 | [diff] [blame] | 958 | // Don't count component extensions, since they are only extensions as an |
| 959 | // implementation detail. |
[email protected] | 9b21765 | 2010-10-08 22:04:23 | [diff] [blame] | 960 | if (location == Extension::COMPONENT) |
[email protected] | 1952c7d | 2010-03-04 23:48:34 | [diff] [blame] | 961 | continue; |
| 962 | |
[email protected] | e8c729a | 2010-03-09 19:55:19 | [diff] [blame] | 963 | // Don't count unpacked extensions, since they're a developer-specific |
| 964 | // feature. |
[email protected] | 9b21765 | 2010-10-08 22:04:23 | [diff] [blame] | 965 | if (location == Extension::LOAD) |
[email protected] | e8c729a | 2010-03-09 19:55:19 | [diff] [blame] | 966 | continue; |
| 967 | |
[email protected] | 9b21765 | 2010-10-08 22:04:23 | [diff] [blame] | 968 | // Using an enumeration shows us the total installed ratio across all users. |
| 969 | // Using the totals per user at each startup tells us the distribution of |
| 970 | // usage for each user (e.g. 40% of users have at least one app installed). |
| 971 | UMA_HISTOGRAM_ENUMERATION("Extensions.LoadType", type, 100); |
| 972 | switch (type) { |
| 973 | case Extension::TYPE_THEME: |
[email protected] | a315ba9 | 2010-11-16 14:12:21 | [diff] [blame] | 974 | ++theme_count; |
[email protected] | 9b21765 | 2010-10-08 22:04:23 | [diff] [blame] | 975 | break; |
| 976 | case Extension::TYPE_USER_SCRIPT: |
[email protected] | a315ba9 | 2010-11-16 14:12:21 | [diff] [blame] | 977 | ++user_script_count; |
[email protected] | 9b21765 | 2010-10-08 22:04:23 | [diff] [blame] | 978 | break; |
| 979 | case Extension::TYPE_HOSTED_APP: |
[email protected] | a315ba9 | 2010-11-16 14:12:21 | [diff] [blame] | 980 | ++app_count; |
| 981 | ++hosted_app_count; |
[email protected] | 9b21765 | 2010-10-08 22:04:23 | [diff] [blame] | 982 | break; |
| 983 | case Extension::TYPE_PACKAGED_APP: |
[email protected] | a315ba9 | 2010-11-16 14:12:21 | [diff] [blame] | 984 | ++app_count; |
| 985 | ++packaged_app_count; |
[email protected] | 9b21765 | 2010-10-08 22:04:23 | [diff] [blame] | 986 | break; |
| 987 | case Extension::TYPE_EXTENSION: |
| 988 | default: |
[email protected] | a315ba9 | 2010-11-16 14:12:21 | [diff] [blame] | 989 | ++extension_count; |
[email protected] | 9b21765 | 2010-10-08 22:04:23 | [diff] [blame] | 990 | break; |
[email protected] | cc2c343 | 2009-11-06 17:24:36 | [diff] [blame] | 991 | } |
[email protected] | 9b21765 | 2010-10-08 22:04:23 | [diff] [blame] | 992 | if (Extension::IsExternalLocation(location)) |
[email protected] | a315ba9 | 2010-11-16 14:12:21 | [diff] [blame] | 993 | ++external_count; |
[email protected] | 9b21765 | 2010-10-08 22:04:23 | [diff] [blame] | 994 | if ((*ex)->page_action() != NULL) |
[email protected] | a315ba9 | 2010-11-16 14:12:21 | [diff] [blame] | 995 | ++page_action_count; |
[email protected] | 9b21765 | 2010-10-08 22:04:23 | [diff] [blame] | 996 | if ((*ex)->browser_action() != NULL) |
[email protected] | a315ba9 | 2010-11-16 14:12:21 | [diff] [blame] | 997 | ++browser_action_count; |
[email protected] | cc2c343 | 2009-11-06 17:24:36 | [diff] [blame] | 998 | } |
[email protected] | 9b21765 | 2010-10-08 22:04:23 | [diff] [blame] | 999 | UMA_HISTOGRAM_COUNTS_100("Extensions.LoadApp", app_count); |
| 1000 | UMA_HISTOGRAM_COUNTS_100("Extensions.LoadHostedApp", hosted_app_count); |
| 1001 | UMA_HISTOGRAM_COUNTS_100("Extensions.LoadPackagedApp", packaged_app_count); |
[email protected] | 1952c7d | 2010-03-04 23:48:34 | [diff] [blame] | 1002 | UMA_HISTOGRAM_COUNTS_100("Extensions.LoadExtension", extension_count); |
| 1003 | UMA_HISTOGRAM_COUNTS_100("Extensions.LoadUserScript", user_script_count); |
| 1004 | UMA_HISTOGRAM_COUNTS_100("Extensions.LoadTheme", theme_count); |
| 1005 | UMA_HISTOGRAM_COUNTS_100("Extensions.LoadExternal", external_count); |
| 1006 | UMA_HISTOGRAM_COUNTS_100("Extensions.LoadPageAction", page_action_count); |
| 1007 | UMA_HISTOGRAM_COUNTS_100("Extensions.LoadBrowserAction", |
| 1008 | browser_action_count); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 1009 | } |
| 1010 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 1011 | void ExtensionService::LoadInstalledExtension(const ExtensionInfo& info, |
[email protected] | 542258c | 2011-03-04 21:25:31 | [diff] [blame] | 1012 | bool write_to_prefs) { |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 1013 | std::string error; |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 1014 | scoped_refptr<const Extension> extension(NULL); |
[email protected] | 306a2bd | 2010-08-11 14:56:36 | [diff] [blame] | 1015 | if (!extension_prefs_->IsExtensionAllowedByPolicy(info.extension_id)) { |
| 1016 | error = errors::kDisabledByPolicy; |
| 1017 | } else if (info.extension_manifest.get()) { |
[email protected] | e8c729a | 2010-03-09 19:55:19 | [diff] [blame] | 1018 | bool require_key = info.extension_location != Extension::LOAD; |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 1019 | extension = Extension::Create( |
[email protected] | 542258c | 2011-03-04 21:25:31 | [diff] [blame] | 1020 | info.extension_path, |
| 1021 | info.extension_location, |
| 1022 | *info.extension_manifest, |
| 1023 | require_key, |
| 1024 | Extension::ShouldDoStrictErrorChecking(info.extension_location), |
| 1025 | &error); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 1026 | } else { |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 1027 | error = errors::kManifestUnreadable; |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | if (!extension) { |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 1031 | ReportExtensionLoadError(info.extension_path, |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 1032 | error, |
| 1033 | NotificationType::EXTENSION_INSTALL_ERROR, |
| 1034 | false); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 1035 | return; |
| 1036 | } |
| 1037 | |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 1038 | if (write_to_prefs) |
| 1039 | extension_prefs_->UpdateManifest(extension); |
| 1040 | |
[email protected] | 49cd157 | 2011-02-08 21:38:45 | [diff] [blame] | 1041 | AddExtension(extension); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 1042 | } |
| 1043 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 1044 | void ExtensionService::NotifyExtensionLoaded(const Extension* extension) { |
[email protected] | 57a777f7 | 2010-03-31 01:09:42 | [diff] [blame] | 1045 | // The ChromeURLRequestContexts need to be first to know that the extension |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 1046 | // was loaded, otherwise a race can arise where a renderer that is created |
| 1047 | // for the extension may try to load an extension URL with an extension id |
[email protected] | 57a777f7 | 2010-03-31 01:09:42 | [diff] [blame] | 1048 | // that the request context doesn't yet know about. The profile is responsible |
| 1049 | // for ensuring its URLRequestContexts appropriately discover the loaded |
| 1050 | // extension. |
| 1051 | if (profile_) { |
| 1052 | profile_->RegisterExtensionWithRequestContexts(extension); |
[email protected] | 19eb8015 | 2011-02-26 00:28:43 | [diff] [blame] | 1053 | profile_->GetExtensionSpecialStoragePolicy()-> |
| 1054 | GrantRightsForExtension(extension); |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | NotificationService::current()->Notify( |
| 1058 | NotificationType::EXTENSION_LOADED, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 1059 | Source<Profile>(profile_), |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 1060 | Details<const Extension>(extension)); |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 1061 | } |
| 1062 | |
[email protected] | a9f39a31 | 2010-12-23 22:14:27 | [diff] [blame] | 1063 | void ExtensionService::NotifyExtensionUnloaded( |
| 1064 | const Extension* extension, UnloadedExtensionInfo::Reason reason) { |
| 1065 | UnloadedExtensionInfo details(extension, reason); |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 1066 | NotificationService::current()->Notify( |
| 1067 | NotificationType::EXTENSION_UNLOADED, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 1068 | Source<Profile>(profile_), |
[email protected] | a9f39a31 | 2010-12-23 22:14:27 | [diff] [blame] | 1069 | Details<UnloadedExtensionInfo>(&details)); |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 1070 | |
[email protected] | 57a777f7 | 2010-03-31 01:09:42 | [diff] [blame] | 1071 | if (profile_) { |
| 1072 | profile_->UnregisterExtensionWithRequestContexts(extension); |
[email protected] | 19eb8015 | 2011-02-26 00:28:43 | [diff] [blame] | 1073 | profile_->GetExtensionSpecialStoragePolicy()-> |
| 1074 | RevokeRightsForExtension(extension); |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 1075 | } |
| 1076 | } |
| 1077 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 1078 | void ExtensionService::UpdateExtensionBlacklist( |
[email protected] | 6b75ec3 | 2009-08-14 06:37:18 | [diff] [blame] | 1079 | const std::vector<std::string>& blacklist) { |
| 1080 | // Use this set to indicate if an extension in the blacklist has been used. |
| 1081 | std::set<std::string> blacklist_set; |
| 1082 | for (unsigned int i = 0; i < blacklist.size(); ++i) { |
| 1083 | if (Extension::IdIsValid(blacklist[i])) { |
| 1084 | blacklist_set.insert(blacklist[i]); |
| 1085 | } |
| 1086 | } |
| 1087 | extension_prefs_->UpdateBlacklist(blacklist_set); |
| 1088 | std::vector<std::string> to_be_removed; |
| 1089 | // Loop current extensions, unload installed extensions. |
| 1090 | for (ExtensionList::const_iterator iter = extensions_.begin(); |
| 1091 | iter != extensions_.end(); ++iter) { |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 1092 | const Extension* extension = (*iter); |
[email protected] | 6b75ec3 | 2009-08-14 06:37:18 | [diff] [blame] | 1093 | if (blacklist_set.find(extension->id()) != blacklist_set.end()) { |
| 1094 | to_be_removed.push_back(extension->id()); |
| 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | // UnloadExtension will change the extensions_ list. So, we should |
| 1099 | // call it outside the iterator loop. |
| 1100 | for (unsigned int i = 0; i < to_be_removed.size(); ++i) { |
[email protected] | a9f39a31 | 2010-12-23 22:14:27 | [diff] [blame] | 1101 | UnloadExtension(to_be_removed[i], UnloadedExtensionInfo::DISABLE); |
[email protected] | 6b75ec3 | 2009-08-14 06:37:18 | [diff] [blame] | 1102 | } |
| 1103 | } |
| 1104 | |
[email protected] | 0a071a3 | 2011-02-08 00:18:24 | [diff] [blame] | 1105 | Profile* ExtensionService::profile() { |
| 1106 | return profile_; |
| 1107 | } |
| 1108 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 1109 | void ExtensionService::DestroyingProfile() { |
[email protected] | 94b4ffc | 2011-02-04 05:55:17 | [diff] [blame] | 1110 | if (updater_.get()) { |
| 1111 | updater_->Stop(); |
| 1112 | } |
[email protected] | 97d2f1d | 2011-01-15 00:41:08 | [diff] [blame] | 1113 | browser_event_router_.reset(); |
[email protected] | 118de789 | 2011-03-16 13:31:40 | [diff] [blame] | 1114 | preference_event_router_.reset(); |
[email protected] | 2fb7dc98 | 2010-09-29 12:24:28 | [diff] [blame] | 1115 | pref_change_registrar_.RemoveAll(); |
[email protected] | aa96d3a | 2010-08-21 08:45:25 | [diff] [blame] | 1116 | profile_ = NULL; |
[email protected] | 2fb7dc98 | 2010-09-29 12:24:28 | [diff] [blame] | 1117 | toolbar_model_.DestroyingProfile(); |
[email protected] | aa96d3a | 2010-08-21 08:45:25 | [diff] [blame] | 1118 | } |
| 1119 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 1120 | ExtensionPrefs* ExtensionService::extension_prefs() { |
[email protected] | ddb1e5a | 2010-12-13 20:10:45 | [diff] [blame] | 1121 | return extension_prefs_; |
| 1122 | } |
| 1123 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 1124 | void ExtensionService::CheckAdminBlacklist() { |
[email protected] | aa96d3a | 2010-08-21 08:45:25 | [diff] [blame] | 1125 | std::vector<std::string> to_be_removed; |
| 1126 | // Loop through extensions list, unload installed extensions. |
| 1127 | for (ExtensionList::const_iterator iter = extensions_.begin(); |
| 1128 | iter != extensions_.end(); ++iter) { |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 1129 | const Extension* extension = (*iter); |
[email protected] | aa96d3a | 2010-08-21 08:45:25 | [diff] [blame] | 1130 | if (!extension_prefs_->IsExtensionAllowedByPolicy(extension->id())) |
| 1131 | to_be_removed.push_back(extension->id()); |
| 1132 | } |
| 1133 | |
| 1134 | // UnloadExtension will change the extensions_ list. So, we should |
| 1135 | // call it outside the iterator loop. |
| 1136 | for (unsigned int i = 0; i < to_be_removed.size(); ++i) |
[email protected] | a9f39a31 | 2010-12-23 22:14:27 | [diff] [blame] | 1137 | UnloadExtension(to_be_removed[i], UnloadedExtensionInfo::DISABLE); |
[email protected] | aa96d3a | 2010-08-21 08:45:25 | [diff] [blame] | 1138 | } |
| 1139 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 1140 | bool ExtensionService::IsIncognitoEnabled(const Extension* extension) { |
[email protected] | cb0ce1e02 | 2010-03-10 19:54:41 | [diff] [blame] | 1141 | // If this is a component extension we always allow it to work in incognito |
| 1142 | // mode. |
| 1143 | if (extension->location() == Extension::COMPONENT) |
| 1144 | return true; |
| 1145 | |
| 1146 | // Check the prefs. |
| 1147 | return extension_prefs_->IsIncognitoEnabled(extension->id()); |
[email protected] | db7331a | 2010-02-25 22:10:50 | [diff] [blame] | 1148 | } |
[email protected] | 55a3569 | 2010-02-11 23:25:21 | [diff] [blame] | 1149 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 1150 | void ExtensionService::SetIsIncognitoEnabled(const Extension* extension, |
[email protected] | db7331a | 2010-02-25 22:10:50 | [diff] [blame] | 1151 | bool enabled) { |
[email protected] | 8c6c8c2 | 2011-03-09 12:52:34 | [diff] [blame] | 1152 | // Broadcast unloaded and loaded events to update browser state. Only bother |
| 1153 | // if the value changed and the extension is actually enabled, since there is |
| 1154 | // no UI otherwise. |
| 1155 | bool old_enabled = extension_prefs_->IsIncognitoEnabled(extension->id()); |
| 1156 | if (enabled == old_enabled) |
| 1157 | return; |
| 1158 | |
[email protected] | cb0ce1e02 | 2010-03-10 19:54:41 | [diff] [blame] | 1159 | extension_prefs_->SetIsIncognitoEnabled(extension->id(), enabled); |
[email protected] | c1499f3d | 2010-03-05 00:33:24 | [diff] [blame] | 1160 | |
[email protected] | 8c6c8c2 | 2011-03-09 12:52:34 | [diff] [blame] | 1161 | bool extension_is_enabled = std::find(extensions_.begin(), extensions_.end(), |
| 1162 | extension) != extensions_.end(); |
| 1163 | if (extension_is_enabled) { |
[email protected] | a9f39a31 | 2010-12-23 22:14:27 | [diff] [blame] | 1164 | NotifyExtensionUnloaded(extension, UnloadedExtensionInfo::DISABLE); |
[email protected] | 568f33d | 2010-08-04 17:06:41 | [diff] [blame] | 1165 | NotifyExtensionLoaded(extension); |
| 1166 | } |
[email protected] | 55a3569 | 2010-02-11 23:25:21 | [diff] [blame] | 1167 | } |
| 1168 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 1169 | bool ExtensionService::CanCrossIncognito(const Extension* extension) { |
[email protected] | 2a8f24e | 2010-11-03 21:37:05 | [diff] [blame] | 1170 | // We allow the extension to see events and data from another profile iff it |
| 1171 | // uses "spanning" behavior and it has incognito access. "split" mode |
| 1172 | // extensions only see events for a matching profile. |
| 1173 | return IsIncognitoEnabled(extension) && !extension->incognito_split_mode(); |
| 1174 | } |
| 1175 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 1176 | bool ExtensionService::AllowFileAccess(const Extension* extension) { |
[email protected] | 05c8218 | 2010-06-24 17:49:08 | [diff] [blame] | 1177 | return (CommandLine::ForCurrentProcess()->HasSwitch( |
[email protected] | 334e04a | 2010-06-24 23:34:44 | [diff] [blame] | 1178 | switches::kDisableExtensionsFileAccessCheck) || |
[email protected] | 05c8218 | 2010-06-24 17:49:08 | [diff] [blame] | 1179 | extension_prefs_->AllowFileAccess(extension->id())); |
| 1180 | } |
| 1181 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 1182 | void ExtensionService::SetAllowFileAccess(const Extension* extension, |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 1183 | bool allow) { |
[email protected] | 05c8218 | 2010-06-24 17:49:08 | [diff] [blame] | 1184 | extension_prefs_->SetAllowFileAccess(extension->id(), allow); |
| 1185 | NotificationService::current()->Notify( |
| 1186 | NotificationType::EXTENSION_USER_SCRIPTS_UPDATED, |
| 1187 | Source<Profile>(profile_), |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 1188 | Details<const Extension>(extension)); |
[email protected] | 05c8218 | 2010-06-24 17:49:08 | [diff] [blame] | 1189 | } |
| 1190 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 1191 | bool ExtensionService::GetBrowserActionVisibility(const Extension* extension) { |
[email protected] | f681c78 | 2010-11-19 11:19:39 | [diff] [blame] | 1192 | return extension_prefs_->GetBrowserActionVisibility(extension); |
| 1193 | } |
| 1194 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 1195 | void ExtensionService::SetBrowserActionVisibility(const Extension* extension, |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 1196 | bool visible) { |
[email protected] | f681c78 | 2010-11-19 11:19:39 | [diff] [blame] | 1197 | extension_prefs_->SetBrowserActionVisibility(extension, visible); |
| 1198 | } |
| 1199 | |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 1200 | // Some extensions will autoupdate themselves externally from Chrome. These |
| 1201 | // are typically part of some larger client application package. To support |
| 1202 | // these, the extension will register its location in the the preferences file |
| 1203 | // (and also, on Windows, in the registry) and this code will periodically |
| 1204 | // check that location for a .crx file, which it will then install locally if |
| 1205 | // a new version is available. |
| 1206 | // Errors are reported through ExtensionErrorReporter. Succcess is not |
| 1207 | // reported. |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 1208 | void ExtensionService::CheckForExternalUpdates() { |
[email protected] | a29a517a | 2011-01-21 21:11:12 | [diff] [blame] | 1209 | CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 1210 | |
| 1211 | // Note that this installation is intentionally silent (since it didn't |
| 1212 | // go through the front-end). Extensions that are registered in this |
| 1213 | // way are effectively considered 'pre-bundled', and so implicitly |
| 1214 | // trusted. In general, if something has HKLM or filesystem access, |
| 1215 | // they could install an extension manually themselves anyway. |
[email protected] | a29a517a | 2011-01-21 21:11:12 | [diff] [blame] | 1216 | |
| 1217 | // If any external extension records give a URL, a provider will set |
| 1218 | // this to true. Used by OnExternalProviderReady() to see if we need |
| 1219 | // to start an update check to fetch a new external extension. |
| 1220 | external_extension_url_added_ = false; |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 1221 | |
| 1222 | // Ask each external extension provider to give us a call back for each |
| 1223 | // extension they know about. See OnExternalExtension(File|UpdateUrl)Found. |
| 1224 | ProviderCollection::const_iterator i; |
| 1225 | for (i = external_extension_providers_.begin(); |
| 1226 | i != external_extension_providers_.end(); ++i) { |
| 1227 | ExternalExtensionProviderInterface* provider = i->get(); |
| 1228 | provider->VisitRegisteredExtension(); |
| 1229 | } |
| 1230 | |
| 1231 | // Uninstall of unclaimed extensions will happen after all the providers |
[email protected] | a29a517a | 2011-01-21 21:11:12 | [diff] [blame] | 1232 | // had reported ready. Every provider calls OnExternalProviderReady() |
| 1233 | // when it finishes, and OnExternalProviderReady() only acts when all |
| 1234 | // providers are ready. In case there are no providers, we call it |
| 1235 | // to trigger removal of extensions that used to have an external source. |
| 1236 | if (external_extension_providers_.empty()) |
| 1237 | OnExternalProviderReady(); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 1238 | } |
| 1239 | |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 1240 | void ExtensionService::OnExternalProviderReady() { |
[email protected] | a29a517a | 2011-01-21 21:11:12 | [diff] [blame] | 1241 | CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 1242 | |
| 1243 | // An external provider has finished loading. We only take action |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 1244 | // if all of them are finished. So we check them first. |
| 1245 | ProviderCollection::const_iterator i; |
| 1246 | for (i = external_extension_providers_.begin(); |
| 1247 | i != external_extension_providers_.end(); ++i) { |
| 1248 | ExternalExtensionProviderInterface* provider = i->get(); |
[email protected] | a29a517a | 2011-01-21 21:11:12 | [diff] [blame] | 1249 | if (!provider->IsReady()) |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 1250 | return; |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 1251 | } |
| 1252 | |
[email protected] | a29a517a | 2011-01-21 21:11:12 | [diff] [blame] | 1253 | // All the providers are ready. Install any pending extensions. |
| 1254 | if (external_extension_url_added_ && updater()) { |
| 1255 | external_extension_url_added_ = false; |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 1256 | updater()->CheckNow(); |
| 1257 | } |
| 1258 | |
| 1259 | // Uninstall all the unclaimed extensions. |
| 1260 | scoped_ptr<ExtensionPrefs::ExtensionsInfo> extensions_info( |
| 1261 | extension_prefs_->GetInstalledExtensionsInfo()); |
| 1262 | for (size_t i = 0; i < extensions_info->size(); ++i) { |
| 1263 | ExtensionInfo* info = extensions_info->at(i).get(); |
| 1264 | if (Extension::IsExternalLocation(info->extension_location)) |
| 1265 | CheckExternalUninstall(info->extension_id); |
| 1266 | } |
[email protected] | 6c751e7 | 2010-11-23 10:11:10 | [diff] [blame] | 1267 | } |
| 1268 | |
[email protected] | a9f39a31 | 2010-12-23 22:14:27 | [diff] [blame] | 1269 | void ExtensionService::UnloadExtension( |
| 1270 | const std::string& extension_id, |
| 1271 | UnloadedExtensionInfo::Reason reason) { |
[email protected] | 27e469a | 2010-01-11 20:35:09 | [diff] [blame] | 1272 | // Make sure the extension gets deleted after we return from this function. |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 1273 | scoped_refptr<const Extension> extension( |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 1274 | GetExtensionByIdInternal(extension_id, true, true)); |
[email protected] | 631cf82 | 2009-05-15 07:01:25 | [diff] [blame] | 1275 | |
[email protected] | fa0c9673 | 2010-11-17 00:14:23 | [diff] [blame] | 1276 | // This method can be called via PostTask, so the extension may have been |
| 1277 | // unloaded by the time this runs. |
| 1278 | if (!extension) |
| 1279 | return; |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 1280 | |
[email protected] | 1eb17508 | 2010-02-10 09:26:16 | [diff] [blame] | 1281 | // Keep information about the extension so that we can reload it later |
| 1282 | // even if it's not permanently installed. |
| 1283 | unloaded_extension_paths_[extension->id()] = extension->path(); |
| 1284 | |
[email protected] | f17dbd4 | 2010-08-16 23:21:10 | [diff] [blame] | 1285 | // Clean up if the extension is meant to be enabled after a reload. |
| 1286 | disabled_extension_paths_.erase(extension->id()); |
| 1287 | |
[email protected] | d7e9a86 | 2010-11-03 21:57:49 | [diff] [blame] | 1288 | // Clean up runtime data. |
| 1289 | extension_runtime_data_.erase(extension_id); |
| 1290 | |
[email protected] | 8f9d4eb | 2011-02-05 01:39:10 | [diff] [blame] | 1291 | ExtensionWebUI::UnregisterChromeURLOverrides(profile_, |
[email protected] | 86c008e8 | 2009-08-28 20:26:05 | [diff] [blame] | 1292 | extension->GetChromeURLOverrides()); |
| 1293 | |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 1294 | ExtensionList::iterator iter = std::find(disabled_extensions_.begin(), |
| 1295 | disabled_extensions_.end(), |
| 1296 | extension.get()); |
| 1297 | if (iter != disabled_extensions_.end()) { |
[email protected] | a9f39a31 | 2010-12-23 22:14:27 | [diff] [blame] | 1298 | UnloadedExtensionInfo details(extension, reason); |
| 1299 | details.already_disabled = true; |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 1300 | disabled_extensions_.erase(iter); |
[email protected] | 86693068 | 2009-08-18 22:53:47 | [diff] [blame] | 1301 | NotificationService::current()->Notify( |
[email protected] | a9f39a31 | 2010-12-23 22:14:27 | [diff] [blame] | 1302 | NotificationType::EXTENSION_UNLOADED, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 1303 | Source<Profile>(profile_), |
[email protected] | a9f39a31 | 2010-12-23 22:14:27 | [diff] [blame] | 1304 | Details<UnloadedExtensionInfo>(&details)); |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 1305 | return; |
| 1306 | } |
| 1307 | |
| 1308 | iter = std::find(extensions_.begin(), extensions_.end(), extension.get()); |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 1309 | |
[email protected] | 631cf82 | 2009-05-15 07:01:25 | [diff] [blame] | 1310 | // Remove the extension from our list. |
| 1311 | extensions_.erase(iter); |
| 1312 | |
[email protected] | a9f39a31 | 2010-12-23 22:14:27 | [diff] [blame] | 1313 | NotifyExtensionUnloaded(extension.get(), reason); |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 1314 | UpdateActiveExtensionsInCrashReporter(); |
[email protected] | 631cf82 | 2009-05-15 07:01:25 | [diff] [blame] | 1315 | } |
| 1316 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 1317 | void ExtensionService::UnloadAllExtensions() { |
[email protected] | 19eb8015 | 2011-02-26 00:28:43 | [diff] [blame] | 1318 | if (profile_) { |
| 1319 | profile_->GetExtensionSpecialStoragePolicy()-> |
| 1320 | RevokeRightsForAllExtensions(); |
| 1321 | } |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 1322 | extensions_.clear(); |
[email protected] | cd500f7 | 2010-06-25 23:44:32 | [diff] [blame] | 1323 | disabled_extensions_.clear(); |
[email protected] | bb7f4095 | 2011-01-13 00:21:20 | [diff] [blame] | 1324 | terminated_extension_ids_.clear(); |
| 1325 | terminated_extensions_.clear(); |
[email protected] | d7e9a86 | 2010-11-03 21:57:49 | [diff] [blame] | 1326 | extension_runtime_data_.clear(); |
[email protected] | cd500f7 | 2010-06-25 23:44:32 | [diff] [blame] | 1327 | |
[email protected] | c6e4a341 | 2009-06-24 15:45:29 | [diff] [blame] | 1328 | // TODO(erikkay) should there be a notification for this? We can't use |
| 1329 | // EXTENSION_UNLOADED since that implies that the extension has been disabled |
| 1330 | // or uninstalled, and UnloadAll is just part of shutdown. |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 1331 | } |
| 1332 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 1333 | void ExtensionService::ReloadExtensions() { |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 1334 | UnloadAllExtensions(); |
| 1335 | LoadAllExtensions(); |
| 1336 | } |
| 1337 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 1338 | void ExtensionService::GarbageCollectExtensions() { |
[email protected] | 39d9f62c | 2010-12-03 10:48:50 | [diff] [blame] | 1339 | if (extension_prefs_->pref_service()->ReadOnly()) |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 1340 | return; |
| 1341 | |
[email protected] | ca3dbf5 | 2010-05-19 22:27:06 | [diff] [blame] | 1342 | scoped_ptr<ExtensionPrefs::ExtensionsInfo> info( |
| 1343 | extension_prefs_->GetInstalledExtensionsInfo()); |
| 1344 | |
| 1345 | std::map<std::string, FilePath> extension_paths; |
| 1346 | for (size_t i = 0; i < info->size(); ++i) |
| 1347 | extension_paths[info->at(i)->extension_id] = info->at(i)->extension_path; |
| 1348 | |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 1349 | BrowserThread::PostTask( |
| 1350 | BrowserThread::FILE, FROM_HERE, |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 1351 | NewRunnableFunction( |
| 1352 | &extension_file_util::GarbageCollectExtensions, install_directory_, |
[email protected] | ca3dbf5 | 2010-05-19 22:27:06 | [diff] [blame] | 1353 | extension_paths)); |
[email protected] | f6ccd65 | 2010-11-17 00:48:34 | [diff] [blame] | 1354 | |
| 1355 | // Also garbage-collect themes. We check |profile_| to be |
| 1356 | // defensive; in the future, we may call GarbageCollectExtensions() |
| 1357 | // from somewhere other than Init() (e.g., in a timer). |
| 1358 | if (profile_) { |
[email protected] | 1828037 | 2011-03-22 18:05:22 | [diff] [blame] | 1359 | ThemeServiceFactory::GetForProfile(profile_)->RemoveUnusedThemes(); |
[email protected] | f6ccd65 | 2010-11-17 00:48:34 | [diff] [blame] | 1360 | } |
[email protected] | 3cf4f099 | 2009-02-03 23:00:30 | [diff] [blame] | 1361 | } |
| 1362 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 1363 | void ExtensionService::OnLoadedInstalledExtensions() { |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 1364 | if (updater_.get()) { |
| 1365 | updater_->Start(); |
| 1366 | } |
[email protected] | fa6a910 | 2010-11-22 15:38:50 | [diff] [blame] | 1367 | |
| 1368 | ready_ = true; |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 1369 | NotificationService::current()->Notify( |
| 1370 | NotificationType::EXTENSIONS_READY, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 1371 | Source<Profile>(profile_), |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 1372 | NotificationService::NoDetails()); |
| 1373 | } |
| 1374 | |
[email protected] | 49cd157 | 2011-02-08 21:38:45 | [diff] [blame] | 1375 | void ExtensionService::AddExtension(const Extension* extension) { |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 1376 | // Ensure extension is deleted unless we transfer ownership. |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 1377 | scoped_refptr<const Extension> scoped_extension(extension); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 1378 | |
[email protected] | 1eb17508 | 2010-02-10 09:26:16 | [diff] [blame] | 1379 | // The extension is now loaded, remove its data from unloaded extension map. |
| 1380 | unloaded_extension_paths_.erase(extension->id()); |
| 1381 | |
[email protected] | bb7f4095 | 2011-01-13 00:21:20 | [diff] [blame] | 1382 | // If a terminated extension is loaded, remove it from the terminated list. |
| 1383 | UntrackTerminatedExtension(extension->id()); |
| 1384 | |
[email protected] | f17dbd4 | 2010-08-16 23:21:10 | [diff] [blame] | 1385 | // If the extension was disabled for a reload, then enable it. |
| 1386 | if (disabled_extension_paths_.erase(extension->id()) > 0) |
| 1387 | EnableExtension(extension->id()); |
| 1388 | |
[email protected] | d728e00 | 2010-12-08 04:46:23 | [diff] [blame] | 1389 | // TODO(jstritar): We may be able to get rid of this branch by overriding the |
| 1390 | // default extension state to DISABLED when the --disable-extensions flag |
| 1391 | // is set (http://crbug.com/29067). |
| 1392 | if (!extensions_enabled() && |
| 1393 | !extension->is_theme() && |
[email protected] | aebe23a3 | 2010-12-10 22:15:48 | [diff] [blame] | 1394 | extension->location() != Extension::COMPONENT && |
| 1395 | !Extension::IsExternalLocation(extension->location())) |
[email protected] | d728e00 | 2010-12-08 04:46:23 | [diff] [blame] | 1396 | return; |
| 1397 | |
| 1398 | // Check if the extension's privileges have changed and disable the |
| 1399 | // extension if necessary. |
[email protected] | 8d888c1 | 2010-11-30 00:00:25 | [diff] [blame] | 1400 | DisableIfPrivilegeIncrease(extension); |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 1401 | |
[email protected] | 8d888c1 | 2010-11-30 00:00:25 | [diff] [blame] | 1402 | switch (extension_prefs_->GetExtensionState(extension->id())) { |
| 1403 | case Extension::ENABLED: |
| 1404 | extensions_.push_back(scoped_extension); |
[email protected] | 1e8c93f | 2010-02-08 22:58:31 | [diff] [blame] | 1405 | |
[email protected] | 8d888c1 | 2010-11-30 00:00:25 | [diff] [blame] | 1406 | NotifyExtensionLoaded(extension); |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 1407 | |
[email protected] | 8f9d4eb | 2011-02-05 01:39:10 | [diff] [blame] | 1408 | ExtensionWebUI::RegisterChromeURLOverrides( |
[email protected] | 8d888c1 | 2010-11-30 00:00:25 | [diff] [blame] | 1409 | profile_, extension->GetChromeURLOverrides()); |
| 1410 | break; |
| 1411 | case Extension::DISABLED: |
| 1412 | disabled_extensions_.push_back(scoped_extension); |
| 1413 | NotificationService::current()->Notify( |
| 1414 | NotificationType::EXTENSION_UPDATE_DISABLED, |
| 1415 | Source<Profile>(profile_), |
| 1416 | Details<const Extension>(extension)); |
| 1417 | break; |
| 1418 | default: |
| 1419 | NOTREACHED(); |
| 1420 | break; |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 1421 | } |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 1422 | |
[email protected] | d7e9a86 | 2010-11-03 21:57:49 | [diff] [blame] | 1423 | SetBeingUpgraded(extension, false); |
[email protected] | 1e8c93f | 2010-02-08 22:58:31 | [diff] [blame] | 1424 | |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 1425 | UpdateActiveExtensionsInCrashReporter(); |
[email protected] | 0b004da | 2010-07-02 17:54:31 | [diff] [blame] | 1426 | |
| 1427 | if (profile_->GetTemplateURLModel()) |
| 1428 | profile_->GetTemplateURLModel()->RegisterExtensionKeyword(extension); |
[email protected] | b671760b | 2010-07-15 21:13:47 | [diff] [blame] | 1429 | |
| 1430 | // Load the icon for omnibox-enabled extensions so it will be ready to display |
| 1431 | // in the URL bar. |
[email protected] | 29d0d4ac | 2010-09-08 21:10:31 | [diff] [blame] | 1432 | if (!extension->omnibox_keyword().empty()) { |
| 1433 | omnibox_popup_icon_manager_.LoadIcon(extension); |
[email protected] | b671760b | 2010-07-15 21:13:47 | [diff] [blame] | 1434 | omnibox_icon_manager_.LoadIcon(extension); |
[email protected] | 29d0d4ac | 2010-09-08 21:10:31 | [diff] [blame] | 1435 | } |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 1436 | } |
| 1437 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 1438 | void ExtensionService::DisableIfPrivilegeIncrease(const Extension* extension) { |
[email protected] | 8d888c1 | 2010-11-30 00:00:25 | [diff] [blame] | 1439 | // We keep track of all permissions the user has granted each extension. |
| 1440 | // This allows extensions to gracefully support backwards compatibility |
| 1441 | // by including unknown permissions in their manifests. When the user |
| 1442 | // installs the extension, only the recognized permissions are recorded. |
| 1443 | // When the unknown permissions become recognized (e.g., through browser |
| 1444 | // upgrade), we can prompt the user to accept these new permissions. |
| 1445 | // Extensions can also silently upgrade to less permissions, and then |
| 1446 | // silently upgrade to a version that adds these permissions back. |
| 1447 | // |
| 1448 | // For example, pretend that Chrome 10 includes a permission "omnibox" |
| 1449 | // for an API that adds suggestions to the omnibox. An extension can |
| 1450 | // maintain backwards compatibility while still having "omnibox" in the |
| 1451 | // manifest. If a user installs the extension on Chrome 9, the browser |
| 1452 | // will record the permissions it recognized, not including "omnibox." |
| 1453 | // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome |
| 1454 | // will disable the extension and prompt the user to approve the increase |
| 1455 | // in privileges. The extension could then release a new version that |
| 1456 | // removes the "omnibox" permission. When the user upgrades, Chrome will |
| 1457 | // still remember that "omnibox" had been granted, so that if the |
| 1458 | // extension once again includes "omnibox" in an upgrade, the extension |
| 1459 | // can upgrade without requiring this user's approval. |
| 1460 | const Extension* old = GetExtensionByIdInternal(extension->id(), |
| 1461 | true, true); |
| 1462 | bool granted_full_access; |
| 1463 | std::set<std::string> granted_apis; |
| 1464 | ExtensionExtent granted_extent; |
| 1465 | |
| 1466 | bool is_extension_upgrade = old != NULL; |
| 1467 | bool is_privilege_increase = false; |
| 1468 | |
| 1469 | // We only record the granted permissions for INTERNAL extensions, since |
| 1470 | // they can't silently increase privileges. |
| 1471 | if (extension->location() == Extension::INTERNAL) { |
| 1472 | // Add all the recognized permissions if the granted permissions list |
| 1473 | // hasn't been initialized yet. |
| 1474 | if (!extension_prefs_->GetGrantedPermissions(extension->id(), |
| 1475 | &granted_full_access, |
| 1476 | &granted_apis, |
| 1477 | &granted_extent)) { |
| 1478 | GrantPermissions(extension); |
| 1479 | CHECK(extension_prefs_->GetGrantedPermissions(extension->id(), |
| 1480 | &granted_full_access, |
| 1481 | &granted_apis, |
| 1482 | &granted_extent)); |
| 1483 | } |
| 1484 | |
| 1485 | // Here, we check if an extension's privileges have increased in a manner |
| 1486 | // that requires the user's approval. This could occur because the browser |
| 1487 | // upgraded and recognized additional privileges, or an extension upgrades |
| 1488 | // to a version that requires additional privileges. |
| 1489 | is_privilege_increase = Extension::IsPrivilegeIncrease( |
| 1490 | granted_full_access, granted_apis, granted_extent, extension); |
| 1491 | } |
| 1492 | |
| 1493 | if (is_extension_upgrade) { |
[email protected] | 66e2687 | 2010-12-03 20:07:25 | [diff] [blame] | 1494 | // Other than for unpacked extensions, CrxInstaller should have guaranteed |
| 1495 | // that we aren't downgrading. |
| 1496 | if (extension->location() != Extension::LOAD) |
| 1497 | CHECK(extension->version()->CompareTo(*(old->version())) >= 0); |
[email protected] | 8d888c1 | 2010-11-30 00:00:25 | [diff] [blame] | 1498 | |
| 1499 | // Extensions get upgraded if the privileges are allowed to increase or |
| 1500 | // the privileges haven't increased. |
| 1501 | if (!is_privilege_increase) { |
| 1502 | SetBeingUpgraded(old, true); |
| 1503 | SetBeingUpgraded(extension, true); |
| 1504 | } |
| 1505 | |
| 1506 | // To upgrade an extension in place, unload the old one and |
| 1507 | // then load the new one. |
[email protected] | a9f39a31 | 2010-12-23 22:14:27 | [diff] [blame] | 1508 | UnloadExtension(old->id(), UnloadedExtensionInfo::UPDATE); |
[email protected] | 8d888c1 | 2010-11-30 00:00:25 | [diff] [blame] | 1509 | old = NULL; |
| 1510 | } |
| 1511 | |
| 1512 | // Extension has changed permissions significantly. Disable it. A |
| 1513 | // notification should be sent by the caller. |
| 1514 | if (is_privilege_increase) { |
| 1515 | extension_prefs_->SetExtensionState(extension, Extension::DISABLED); |
| 1516 | extension_prefs_->SetDidExtensionEscalatePermissions(extension, true); |
| 1517 | } |
| 1518 | } |
| 1519 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 1520 | void ExtensionService::UpdateActiveExtensionsInCrashReporter() { |
[email protected] | c886596 | 2009-12-16 07:47:39 | [diff] [blame] | 1521 | std::set<std::string> extension_ids; |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 1522 | for (size_t i = 0; i < extensions_.size(); ++i) { |
[email protected] | 2a500c1b | 2010-11-05 15:27:34 | [diff] [blame] | 1523 | if (!extensions_[i]->is_theme() && |
| 1524 | extensions_[i]->location() != Extension::COMPONENT) |
[email protected] | c886596 | 2009-12-16 07:47:39 | [diff] [blame] | 1525 | extension_ids.insert(extensions_[i]->id()); |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 1526 | } |
| 1527 | |
| 1528 | child_process_logging::SetActiveExtensions(extension_ids); |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 1529 | } |
| 1530 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 1531 | void ExtensionService::OnExtensionInstalled(const Extension* extension) { |
[email protected] | a29a517a | 2011-01-21 21:11:12 | [diff] [blame] | 1532 | CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | a8af9fdb | 2010-10-28 21:52:20 | [diff] [blame] | 1533 | |
[email protected] | 4416c5a | 2010-06-26 01:28:57 | [diff] [blame] | 1534 | // Ensure extension is deleted unless we transfer ownership. |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 1535 | scoped_refptr<const Extension> scoped_extension(extension); |
[email protected] | 4416c5a | 2010-06-26 01:28:57 | [diff] [blame] | 1536 | Extension::State initial_state = Extension::DISABLED; |
| 1537 | bool initial_enable_incognito = false; |
[email protected] | aa14270 | 2010-03-26 01:26:33 | [diff] [blame] | 1538 | PendingExtensionMap::iterator it = |
| 1539 | pending_extensions_.find(extension->id()); |
[email protected] | 4416c5a | 2010-06-26 01:28:57 | [diff] [blame] | 1540 | if (it != pending_extensions_.end()) { |
[email protected] | 11edd1e | 2010-07-21 00:14:50 | [diff] [blame] | 1541 | PendingExtensionInfo pending_extension_info = it->second; |
| 1542 | pending_extensions_.erase(it); |
| 1543 | it = pending_extensions_.end(); |
[email protected] | 8ef78fd | 2010-08-19 17:14:32 | [diff] [blame] | 1544 | |
[email protected] | 483d1ff | 2011-03-03 17:12:02 | [diff] [blame] | 1545 | if (!pending_extension_info.ShouldAllowInstall(*extension)) { |
[email protected] | 4416c5a | 2010-06-26 01:28:57 | [diff] [blame] | 1546 | LOG(WARNING) |
[email protected] | 483d1ff | 2011-03-03 17:12:02 | [diff] [blame] | 1547 | << "should_install_extension() returned false for " |
| 1548 | << extension->id() << " of type " << extension->GetType() |
[email protected] | 7fa19f8 | 2010-12-21 19:40:08 | [diff] [blame] | 1549 | << " and update URL " << extension->update_url().spec() |
| 1550 | << "; not installing"; |
[email protected] | 4416c5a | 2010-06-26 01:28:57 | [diff] [blame] | 1551 | // Delete the extension directory since we're not going to |
| 1552 | // load it. |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 1553 | BrowserThread::PostTask( |
| 1554 | BrowserThread::FILE, FROM_HERE, |
[email protected] | 53da2c96 | 2011-03-03 17:08:05 | [diff] [blame] | 1555 | NewRunnableFunction(&extension_file_util::DeleteFile, extension->path(), true)); |
[email protected] | 4416c5a | 2010-06-26 01:28:57 | [diff] [blame] | 1556 | return; |
| 1557 | } |
[email protected] | 8ef78fd | 2010-08-19 17:14:32 | [diff] [blame] | 1558 | |
[email protected] | 8ef78fd | 2010-08-19 17:14:32 | [diff] [blame] | 1559 | if (extension->is_theme()) { |
[email protected] | 483d1ff | 2011-03-03 17:12:02 | [diff] [blame] | 1560 | DCHECK(pending_extension_info.enable_on_install()); |
[email protected] | 4416c5a | 2010-06-26 01:28:57 | [diff] [blame] | 1561 | initial_state = Extension::ENABLED; |
[email protected] | 483d1ff | 2011-03-03 17:12:02 | [diff] [blame] | 1562 | DCHECK(!pending_extension_info.enable_incognito_on_install()); |
[email protected] | 4416c5a | 2010-06-26 01:28:57 | [diff] [blame] | 1563 | initial_enable_incognito = false; |
| 1564 | } else { |
| 1565 | initial_state = |
[email protected] | 483d1ff | 2011-03-03 17:12:02 | [diff] [blame] | 1566 | pending_extension_info.enable_on_install() ? |
[email protected] | 4416c5a | 2010-06-26 01:28:57 | [diff] [blame] | 1567 | Extension::ENABLED : Extension::DISABLED; |
| 1568 | initial_enable_incognito = |
[email protected] | 483d1ff | 2011-03-03 17:12:02 | [diff] [blame] | 1569 | pending_extension_info.enable_incognito_on_install(); |
[email protected] | 4416c5a | 2010-06-26 01:28:57 | [diff] [blame] | 1570 | } |
[email protected] | 4416c5a | 2010-06-26 01:28:57 | [diff] [blame] | 1571 | } else { |
[email protected] | dbec379 | 2010-08-10 00:08:45 | [diff] [blame] | 1572 | // Make sure we preserve enabled/disabled states. |
[email protected] | 4416c5a | 2010-06-26 01:28:57 | [diff] [blame] | 1573 | Extension::State existing_state = |
| 1574 | extension_prefs_->GetExtensionState(extension->id()); |
| 1575 | initial_state = |
| 1576 | (existing_state == Extension::DISABLED) ? |
| 1577 | Extension::DISABLED : Extension::ENABLED; |
[email protected] | dbec379 | 2010-08-10 00:08:45 | [diff] [blame] | 1578 | initial_enable_incognito = |
| 1579 | extension_prefs_->IsIncognitoEnabled(extension->id()); |
[email protected] | aa14270 | 2010-03-26 01:26:33 | [diff] [blame] | 1580 | } |
| 1581 | |
[email protected] | 9b21765 | 2010-10-08 22:04:23 | [diff] [blame] | 1582 | UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType", |
[email protected] | 7fa19f8 | 2010-12-21 19:40:08 | [diff] [blame] | 1583 | extension->GetType(), 100); |
[email protected] | eda3c36 | 2010-11-12 08:08:23 | [diff] [blame] | 1584 | ShownSectionsHandler::OnExtensionInstalled(profile_->GetPrefs(), extension); |
[email protected] | 4416c5a | 2010-06-26 01:28:57 | [diff] [blame] | 1585 | extension_prefs_->OnExtensionInstalled( |
| 1586 | extension, initial_state, initial_enable_incognito); |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 1587 | |
[email protected] | 92a5b1d | 2010-07-20 00:42:00 | [diff] [blame] | 1588 | // Unpacked extensions start off with file access since they are a developer |
| 1589 | // feature. |
| 1590 | if (extension->location() == Extension::LOAD) |
| 1591 | extension_prefs_->SetAllowFileAccess(extension->id(), true); |
| 1592 | |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 1593 | // If the extension is a theme, tell the profile (and therefore ThemeProvider) |
| 1594 | // to apply it. |
[email protected] | 3ba0fd3 | 2010-06-19 05:39:10 | [diff] [blame] | 1595 | if (extension->is_theme()) { |
[email protected] | 9ceb0734 | 2009-07-26 04:09:23 | [diff] [blame] | 1596 | NotificationService::current()->Notify( |
| 1597 | NotificationType::THEME_INSTALLED, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 1598 | Source<Profile>(profile_), |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 1599 | Details<const Extension>(extension)); |
[email protected] | 9197f3b | 2009-06-02 00:49:27 | [diff] [blame] | 1600 | } else { |
| 1601 | NotificationService::current()->Notify( |
| 1602 | NotificationType::EXTENSION_INSTALLED, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 1603 | Source<Profile>(profile_), |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 1604 | Details<const Extension>(extension)); |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 1605 | } |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 1606 | |
[email protected] | ec5b50d | 2010-10-09 16:35:18 | [diff] [blame] | 1607 | if (extension->is_app()) { |
| 1608 | ExtensionIdSet installed_ids = GetAppIds(); |
| 1609 | installed_ids.insert(extension->id()); |
| 1610 | default_apps_.DidInstallApp(installed_ids); |
| 1611 | } |
| 1612 | |
[email protected] | 49cd157 | 2011-02-08 21:38:45 | [diff] [blame] | 1613 | // Transfer ownership of |extension| to AddExtension. |
| 1614 | AddExtension(scoped_extension); |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 1615 | } |
| 1616 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 1617 | const Extension* ExtensionService::GetExtensionByIdInternal( |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 1618 | const std::string& id, bool include_enabled, bool include_disabled) { |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 1619 | std::string lowercase_id = StringToLowerASCII(id); |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 1620 | if (include_enabled) { |
| 1621 | for (ExtensionList::const_iterator iter = extensions_.begin(); |
| 1622 | iter != extensions_.end(); ++iter) { |
| 1623 | if ((*iter)->id() == lowercase_id) |
| 1624 | return *iter; |
| 1625 | } |
| 1626 | } |
| 1627 | if (include_disabled) { |
| 1628 | for (ExtensionList::const_iterator iter = disabled_extensions_.begin(); |
| 1629 | iter != disabled_extensions_.end(); ++iter) { |
| 1630 | if ((*iter)->id() == lowercase_id) |
| 1631 | return *iter; |
| 1632 | } |
[email protected] | ce5c450 | 2009-05-06 16:46:11 | [diff] [blame] | 1633 | } |
| 1634 | return NULL; |
| 1635 | } |
| 1636 | |
[email protected] | bb7f4095 | 2011-01-13 00:21:20 | [diff] [blame] | 1637 | void ExtensionService::TrackTerminatedExtension(const Extension* extension) { |
| 1638 | if (terminated_extension_ids_.insert(extension->id()).second) |
| 1639 | terminated_extensions_.push_back(make_scoped_refptr(extension)); |
| 1640 | } |
| 1641 | |
| 1642 | void ExtensionService::UntrackTerminatedExtension(const std::string& id) { |
| 1643 | if (terminated_extension_ids_.erase(id) <= 0) |
| 1644 | return; |
| 1645 | |
| 1646 | std::string lowercase_id = StringToLowerASCII(id); |
| 1647 | for (ExtensionList::iterator iter = terminated_extensions_.begin(); |
| 1648 | iter != terminated_extensions_.end(); ++iter) { |
| 1649 | if ((*iter)->id() == lowercase_id) { |
| 1650 | terminated_extensions_.erase(iter); |
| 1651 | return; |
| 1652 | } |
| 1653 | } |
| 1654 | } |
| 1655 | |
[email protected] | 0dfe05c | 2011-02-23 23:03:36 | [diff] [blame] | 1656 | const Extension* ExtensionService::GetTerminatedExtension( |
| 1657 | const std::string& id) { |
| 1658 | std::string lowercase_id = StringToLowerASCII(id); |
| 1659 | for (ExtensionList::const_iterator iter = terminated_extensions_.begin(); |
| 1660 | iter != terminated_extensions_.end(); ++iter) { |
| 1661 | if ((*iter)->id() == lowercase_id) |
| 1662 | return *iter; |
| 1663 | } |
| 1664 | return NULL; |
| 1665 | } |
| 1666 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 1667 | const Extension* ExtensionService::GetWebStoreApp() { |
[email protected] | d307199 | 2010-10-08 15:24:07 | [diff] [blame] | 1668 | return GetExtensionById(extension_misc::kWebStoreAppId, false); |
| 1669 | } |
| 1670 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 1671 | const Extension* ExtensionService::GetExtensionByURL(const GURL& url) { |
[email protected] | a888b29e6 | 2010-04-01 13:38:57 | [diff] [blame] | 1672 | return url.scheme() != chrome::kExtensionScheme ? NULL : |
| 1673 | GetExtensionById(url.host(), false); |
| 1674 | } |
| 1675 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 1676 | const Extension* ExtensionService::GetExtensionByWebExtent(const GURL& url) { |
[email protected] | a888b29e6 | 2010-04-01 13:38:57 | [diff] [blame] | 1677 | for (size_t i = 0; i < extensions_.size(); ++i) { |
| 1678 | if (extensions_[i]->web_extent().ContainsURL(url)) |
| 1679 | return extensions_[i]; |
| 1680 | } |
| 1681 | return NULL; |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 1682 | } |
| 1683 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 1684 | bool ExtensionService::ExtensionBindingsAllowed(const GURL& url) { |
[email protected] | 583d45c1 | 2010-08-31 02:48:12 | [diff] [blame] | 1685 | // Allow bindings for all packaged extension. |
| 1686 | if (GetExtensionByURL(url)) |
| 1687 | return true; |
| 1688 | |
| 1689 | // Allow bindings for all component, hosted apps. |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 1690 | const Extension* extension = GetExtensionByWebExtent(url); |
[email protected] | 583d45c1 | 2010-08-31 02:48:12 | [diff] [blame] | 1691 | return (extension && extension->location() == Extension::COMPONENT); |
| 1692 | } |
| 1693 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 1694 | const Extension* ExtensionService::GetExtensionByOverlappingWebExtent( |
[email protected] | 9f72aa0 | 2010-06-25 10:01:05 | [diff] [blame] | 1695 | const ExtensionExtent& extent) { |
[email protected] | 22c966c | 2010-06-26 06:35:02 | [diff] [blame] | 1696 | for (size_t i = 0; i < extensions_.size(); ++i) { |
| 1697 | if (extensions_[i]->web_extent().OverlapsWith(extent)) |
| 1698 | return extensions_[i]; |
| 1699 | } |
| 1700 | |
[email protected] | 6d2e60bd | 2010-06-03 22:37:39 | [diff] [blame] | 1701 | return NULL; |
| 1702 | } |
| 1703 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 1704 | const SkBitmap& ExtensionService::GetOmniboxIcon( |
[email protected] | b671760b | 2010-07-15 21:13:47 | [diff] [blame] | 1705 | const std::string& extension_id) { |
| 1706 | return omnibox_icon_manager_.GetIcon(extension_id); |
| 1707 | } |
| 1708 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 1709 | const SkBitmap& ExtensionService::GetOmniboxPopupIcon( |
[email protected] | 29d0d4ac | 2010-09-08 21:10:31 | [diff] [blame] | 1710 | const std::string& extension_id) { |
| 1711 | return omnibox_popup_icon_manager_.GetIcon(extension_id); |
| 1712 | } |
| 1713 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 1714 | void ExtensionService::OnExternalExtensionFileFound( |
[email protected] | 8ef78fd | 2010-08-19 17:14:32 | [diff] [blame] | 1715 | const std::string& id, |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 1716 | const Version* version, |
[email protected] | 8ef78fd | 2010-08-19 17:14:32 | [diff] [blame] | 1717 | const FilePath& path, |
| 1718 | Extension::Location location) { |
[email protected] | ab22ba4 | 2011-01-14 16:36:38 | [diff] [blame] | 1719 | CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 1720 | CHECK(Extension::IdIsValid(id)); |
[email protected] | 683d070 | 2010-12-06 16:25:57 | [diff] [blame] | 1721 | if (extension_prefs_->IsExtensionKilled(id)) |
| 1722 | return; |
[email protected] | a8af9fdb | 2010-10-28 21:52:20 | [diff] [blame] | 1723 | |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 1724 | DCHECK(version); |
| 1725 | |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 1726 | // Before even bothering to unpack, check and see if we already have this |
[email protected] | 4c96793 | 2009-07-31 01:15:49 | [diff] [blame] | 1727 | // version. This is important because these extensions are going to get |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 1728 | // installed on every startup. |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 1729 | const Extension* existing = GetExtensionById(id, true); |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 1730 | if (existing) { |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 1731 | switch (existing->version()->CompareTo(*version)) { |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 1732 | case -1: // existing version is older, we should upgrade |
| 1733 | break; |
| 1734 | case 0: // existing version is same, do nothing |
| 1735 | return; |
| 1736 | case 1: // existing version is newer, uh-oh |
| 1737 | LOG(WARNING) << "Found external version of extension " << id |
| 1738 | << "that is older than current version. Current version " |
| 1739 | << "is: " << existing->VersionString() << ". New version " |
| 1740 | << "is: " << version << ". Keeping current version."; |
| 1741 | return; |
| 1742 | } |
| 1743 | } |
| 1744 | |
[email protected] | 9c635f2 | 2010-12-02 09:36:36 | [diff] [blame] | 1745 | GURL update_url = GURL(); |
[email protected] | 9c635f2 | 2010-12-02 09:36:36 | [diff] [blame] | 1746 | bool is_from_sync = false; |
| 1747 | bool install_silently = true; |
| 1748 | bool enable_on_install = true; |
| 1749 | bool enable_incognito_on_install = false; |
| 1750 | pending_extensions_[id] = PendingExtensionInfo( |
| 1751 | update_url, |
[email protected] | 7fa19f8 | 2010-12-21 19:40:08 | [diff] [blame] | 1752 | &AlwaysInstall, |
[email protected] | 9c635f2 | 2010-12-02 09:36:36 | [diff] [blame] | 1753 | is_from_sync, |
| 1754 | install_silently, |
| 1755 | enable_on_install, |
| 1756 | enable_incognito_on_install, |
| 1757 | location); |
| 1758 | |
[email protected] | 6dfbbf8 | 2010-03-12 23:09:16 | [diff] [blame] | 1759 | scoped_refptr<CrxInstaller> installer( |
[email protected] | bb46153 | 2010-11-26 21:50:23 | [diff] [blame] | 1760 | new CrxInstaller(this, // frontend |
[email protected] | 6dfbbf8 | 2010-03-12 23:09:16 | [diff] [blame] | 1761 | NULL)); // no client (silent install) |
| 1762 | installer->set_install_source(location); |
| 1763 | installer->set_expected_id(id); |
[email protected] | 604322d | 2011-03-22 16:51:56 | [diff] [blame] | 1764 | installer->set_expected_version(*version), |
[email protected] | 6dfbbf8 | 2010-03-12 23:09:16 | [diff] [blame] | 1765 | installer->InstallCrx(path); |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 1766 | } |
| 1767 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 1768 | void ExtensionService::ReportExtensionLoadError( |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 1769 | const FilePath& extension_path, |
| 1770 | const std::string &error, |
| 1771 | NotificationType type, |
| 1772 | bool be_noisy) { |
| 1773 | NotificationService* service = NotificationService::current(); |
| 1774 | service->Notify(type, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 1775 | Source<Profile>(profile_), |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 1776 | Details<const std::string>(&error)); |
| 1777 | |
[email protected] | 8a205c0 | 2011-02-04 20:41:33 | [diff] [blame] | 1778 | std::string path_str = UTF16ToUTF8(extension_path.LossyDisplayName()); |
[email protected] | 18d4b6c | 2010-09-21 03:21:04 | [diff] [blame] | 1779 | std::string message = base::StringPrintf( |
| 1780 | "Could not load extension from '%s'. %s", |
| 1781 | path_str.c_str(), error.c_str()); |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 1782 | ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy); |
| 1783 | } |
| 1784 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 1785 | void ExtensionService::DidCreateRenderViewForBackgroundPage( |
[email protected] | 406027c0 | 2010-09-27 08:03:18 | [diff] [blame] | 1786 | ExtensionHost* host) { |
| 1787 | OrphanedDevTools::iterator iter = |
| 1788 | orphaned_dev_tools_.find(host->extension()->id()); |
| 1789 | if (iter == orphaned_dev_tools_.end()) |
| 1790 | return; |
| 1791 | |
| 1792 | DevToolsManager::GetInstance()->AttachClientHost( |
| 1793 | iter->second, host->render_view_host()); |
| 1794 | orphaned_dev_tools_.erase(iter); |
| 1795 | } |
| 1796 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 1797 | void ExtensionService::Observe(NotificationType type, |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 1798 | const NotificationSource& source, |
| 1799 | const NotificationDetails& details) { |
| 1800 | switch (type.value) { |
[email protected] | a4ed628 | 2009-12-14 20:51:16 | [diff] [blame] | 1801 | case NotificationType::EXTENSION_PROCESS_TERMINATED: { |
[email protected] | bc535ee5 | 2010-08-31 18:40:32 | [diff] [blame] | 1802 | if (profile_ != Source<Profile>(source).ptr()->GetOriginalProfile()) |
| 1803 | break; |
[email protected] | a4ed628 | 2009-12-14 20:51:16 | [diff] [blame] | 1804 | |
[email protected] | f128af4 | 2010-08-05 18:05:26 | [diff] [blame] | 1805 | ExtensionHost* host = Details<ExtensionHost>(details).ptr(); |
[email protected] | bb7f4095 | 2011-01-13 00:21:20 | [diff] [blame] | 1806 | TrackTerminatedExtension(host->extension()); |
[email protected] | f128af4 | 2010-08-05 18:05:26 | [diff] [blame] | 1807 | |
[email protected] | 31f7726 | 2009-12-02 20:48:53 | [diff] [blame] | 1808 | // Unload the entire extension. We want it to be in a consistent state: |
| 1809 | // either fully working or not loaded at all, but never half-crashed. |
[email protected] | bc535ee5 | 2010-08-31 18:40:32 | [diff] [blame] | 1810 | // We do it in a PostTask so that other handlers of this notification will |
| 1811 | // still have access to the Extension and ExtensionHost. |
| 1812 | MessageLoop::current()->PostTask(FROM_HERE, |
[email protected] | a9f39a31 | 2010-12-23 22:14:27 | [diff] [blame] | 1813 | NewRunnableMethod(this, |
| 1814 | &ExtensionService::UnloadExtension, |
| 1815 | host->extension()->id(), |
| 1816 | UnloadedExtensionInfo::DISABLE)); |
[email protected] | 31f7726 | 2009-12-02 20:48:53 | [diff] [blame] | 1817 | break; |
| 1818 | } |
| 1819 | |
[email protected] | aa96d3a | 2010-08-21 08:45:25 | [diff] [blame] | 1820 | case NotificationType::PREF_CHANGED: { |
| 1821 | std::string* pref_name = Details<std::string>(details).ptr(); |
[email protected] | 6c751e7 | 2010-11-23 10:11:10 | [diff] [blame] | 1822 | if (*pref_name == prefs::kExtensionInstallAllowList || |
| 1823 | *pref_name == prefs::kExtensionInstallDenyList) { |
| 1824 | CheckAdminBlacklist(); |
[email protected] | 6c751e7 | 2010-11-23 10:11:10 | [diff] [blame] | 1825 | } else { |
| 1826 | NOTREACHED() << "Unexpected preference name."; |
| 1827 | } |
[email protected] | aa96d3a | 2010-08-21 08:45:25 | [diff] [blame] | 1828 | break; |
| 1829 | } |
| 1830 | |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 1831 | default: |
| 1832 | NOTREACHED() << "Unexpected notification type."; |
| 1833 | } |
| 1834 | } |
| 1835 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 1836 | bool ExtensionService::HasApps() const { |
[email protected] | ec5b50d | 2010-10-09 16:35:18 | [diff] [blame] | 1837 | return !GetAppIds().empty(); |
| 1838 | } |
[email protected] | 377011d | 2010-07-20 04:18:50 | [diff] [blame] | 1839 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 1840 | ExtensionIdSet ExtensionService::GetAppIds() const { |
[email protected] | ec5b50d | 2010-10-09 16:35:18 | [diff] [blame] | 1841 | ExtensionIdSet result; |
[email protected] | 377011d | 2010-07-20 04:18:50 | [diff] [blame] | 1842 | for (ExtensionList::const_iterator it = extensions_.begin(); |
| 1843 | it != extensions_.end(); ++it) { |
[email protected] | ec5b50d | 2010-10-09 16:35:18 | [diff] [blame] | 1844 | if ((*it)->is_app() && (*it)->location() != Extension::COMPONENT) |
| 1845 | result.insert((*it)->id()); |
[email protected] | 377011d | 2010-07-20 04:18:50 | [diff] [blame] | 1846 | } |
| 1847 | |
[email protected] | ec5b50d | 2010-10-09 16:35:18 | [diff] [blame] | 1848 | return result; |
[email protected] | 377011d | 2010-07-20 04:18:50 | [diff] [blame] | 1849 | } |
[email protected] | d7e9a86 | 2010-11-03 21:57:49 | [diff] [blame] | 1850 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 1851 | bool ExtensionService::IsBackgroundPageReady(const Extension* extension) { |
[email protected] | d7e9a86 | 2010-11-03 21:57:49 | [diff] [blame] | 1852 | return (extension->background_url().is_empty() || |
| 1853 | extension_runtime_data_[extension->id()].background_page_ready); |
| 1854 | } |
| 1855 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 1856 | void ExtensionService::SetBackgroundPageReady(const Extension* extension) { |
[email protected] | d7e9a86 | 2010-11-03 21:57:49 | [diff] [blame] | 1857 | DCHECK(!extension->background_url().is_empty()); |
| 1858 | extension_runtime_data_[extension->id()].background_page_ready = true; |
| 1859 | NotificationService::current()->Notify( |
| 1860 | NotificationType::EXTENSION_BACKGROUND_PAGE_READY, |
| 1861 | Source<const Extension>(extension), |
| 1862 | NotificationService::NoDetails()); |
| 1863 | } |
| 1864 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 1865 | bool ExtensionService::IsBeingUpgraded(const Extension* extension) { |
[email protected] | d7e9a86 | 2010-11-03 21:57:49 | [diff] [blame] | 1866 | return extension_runtime_data_[extension->id()].being_upgraded; |
| 1867 | } |
| 1868 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 1869 | void ExtensionService::SetBeingUpgraded(const Extension* extension, |
[email protected] | d7e9a86 | 2010-11-03 21:57:49 | [diff] [blame] | 1870 | bool value) { |
| 1871 | extension_runtime_data_[extension->id()].being_upgraded = value; |
| 1872 | } |
[email protected] | 1bead071 | 2010-11-27 17:41:53 | [diff] [blame] | 1873 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 1874 | PropertyBag* ExtensionService::GetPropertyBag(const Extension* extension) { |
[email protected] | 1bead071 | 2010-11-27 17:41:53 | [diff] [blame] | 1875 | return &extension_runtime_data_[extension->id()].property_bag; |
| 1876 | } |