[email protected] | 1e8c93f | 2010-02-08 22:58:31 | [diff] [blame] | 1 | // Copyright (c) 2010 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 | |
| 5 | #include "chrome/browser/extensions/extensions_service.h" |
| 6 | |
[email protected] | 24b538a | 2010-02-27 01:22:44 | [diff] [blame] | 7 | #include "base/basictypes.h" |
[email protected] | e2eb4311 | 2009-05-29 21:19:54 | [diff] [blame] | 8 | #include "base/command_line.h" |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 9 | #include "base/file_util.h" |
[email protected] | cc2c343 | 2009-11-06 17:24:36 | [diff] [blame] | 10 | #include "base/histogram.h" |
[email protected] | 24b538a | 2010-02-27 01:22:44 | [diff] [blame] | 11 | #include "base/string16.h" |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 12 | #include "base/string_util.h" |
[email protected] | cc2c343 | 2009-11-06 17:24:36 | [diff] [blame] | 13 | #include "base/time.h" |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 14 | #include "base/values.h" |
[email protected] | 15730c4 | 2009-09-03 00:03:20 | [diff] [blame] | 15 | #include "chrome/browser/browser_process.h" |
[email protected] | dbb92e0d | 2009-08-20 16:18:21 | [diff] [blame] | 16 | #include "chrome/browser/chrome_thread.h" |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 17 | #include "chrome/browser/debugger/devtools_manager.h" |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 18 | #include "chrome/browser/extensions/crx_installer.h" |
[email protected] | 5cbe1e2 | 2010-01-30 01:18:56 | [diff] [blame] | 19 | #include "chrome/browser/extensions/extension_accessibility_api.h" |
[email protected] | 840b0db | 2009-11-20 03:00:38 | [diff] [blame] | 20 | #include "chrome/browser/extensions/extension_bookmarks_module.h" |
[email protected] | b68d5ed | 2009-04-16 02:41:28 | [diff] [blame] | 21 | #include "chrome/browser/extensions/extension_browser_event_router.h" |
[email protected] | 86c008e8 | 2009-08-28 20:26:05 | [diff] [blame] | 22 | #include "chrome/browser/extensions/extension_dom_ui.h" |
[email protected] | de768a83 | 2009-10-30 05:25:01 | [diff] [blame] | 23 | #include "chrome/browser/extensions/extension_history_api.h" |
[email protected] | b1748b1d8 | 2009-11-30 20:32:56 | [diff] [blame] | 24 | #include "chrome/browser/extensions/extension_host.h" |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 25 | #include "chrome/browser/extensions/extension_process_manager.h" |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 26 | #include "chrome/browser/extensions/extension_updater.h" |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 27 | #include "chrome/browser/extensions/external_extension_provider.h" |
| 28 | #include "chrome/browser/extensions/external_pref_extension_provider.h" |
[email protected] | 052313b | 2010-02-19 09:43:08 | [diff] [blame] | 29 | #include "chrome/browser/pref_service.h" |
[email protected] | 81e6378 | 2009-02-27 19:35:09 | [diff] [blame] | 30 | #include "chrome/browser/profile.h" |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 31 | #include "chrome/browser/net/chrome_url_request_context.h" |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 32 | #include "chrome/common/child_process_logging.h" |
[email protected] | e2eb4311 | 2009-05-29 21:19:54 | [diff] [blame] | 33 | #include "chrome/common/chrome_switches.h" |
[email protected] | 5b1a0e2 | 2009-05-26 19:00:58 | [diff] [blame] | 34 | #include "chrome/common/extensions/extension.h" |
[email protected] | d7b36dc | 2009-10-29 21:47:40 | [diff] [blame] | 35 | #include "chrome/common/extensions/extension_constants.h" |
[email protected] | 5b1a0e2 | 2009-05-26 19:00:58 | [diff] [blame] | 36 | #include "chrome/common/extensions/extension_error_reporter.h" |
[email protected] | 7c927b6 | 2010-02-24 09:54:13 | [diff] [blame] | 37 | #include "chrome/common/extensions/extension_file_util.h" |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 38 | #include "chrome/common/extensions/extension_l10n_util.h" |
[email protected] | 8289126 | 2008-12-24 00:21:26 | [diff] [blame] | 39 | #include "chrome/common/notification_service.h" |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 40 | #include "chrome/common/notification_type.h" |
[email protected] | 1952c7d | 2010-03-04 23:48:34 | [diff] [blame] | 41 | #include "chrome/common/json_value_serializer.h" |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 42 | #include "chrome/common/pref_names.h" |
[email protected] | a5720987 | 2009-05-04 22:53:14 | [diff] [blame] | 43 | #include "chrome/common/url_constants.h" |
[email protected] | 24b538a | 2010-02-27 01:22:44 | [diff] [blame] | 44 | #include "webkit/database/database_tracker.h" |
| 45 | #include "webkit/database/database_util.h" |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 46 | |
[email protected] | 79db623 | 2009-02-13 20:51:20 | [diff] [blame] | 47 | #if defined(OS_WIN) |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 48 | #include "chrome/browser/extensions/external_registry_extension_provider_win.h" |
[email protected] | 79db623 | 2009-02-13 20:51:20 | [diff] [blame] | 49 | #endif |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 50 | |
[email protected] | 5ef47ec | 2010-01-28 05:58:05 | [diff] [blame] | 51 | using base::Time; |
| 52 | |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 53 | namespace errors = extension_manifest_errors; |
| 54 | |
[email protected] | b6ab96d | 2009-08-20 18:58:19 | [diff] [blame] | 55 | namespace { |
| 56 | |
| 57 | // Helper class to collect the IDs of every extension listed in the prefs. |
| 58 | class InstalledExtensionSet { |
| 59 | public: |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 60 | explicit InstalledExtensionSet(ExtensionPrefs* prefs) { |
| 61 | scoped_ptr<ExtensionPrefs::ExtensionsInfo> info( |
| 62 | ExtensionPrefs::CollectExtensionsInfo(prefs)); |
| 63 | |
| 64 | for (size_t i = 0; i < info->size(); ++i) { |
| 65 | std::string version; |
| 66 | const DictionaryValue* manifest = info->at(i)->extension_manifest.get(); |
| 67 | if (!manifest || |
| 68 | !manifest->GetString(extension_manifest_keys::kVersion, &version)) { |
| 69 | // Without a version, the extension is invalid. Ignoring it here will |
| 70 | // cause it to get garbage collected. |
| 71 | continue; |
| 72 | } |
| 73 | extensions_.insert(info->at(i)->extension_id); |
| 74 | versions_[info->at(i)->extension_id] = version; |
| 75 | } |
[email protected] | b6ab96d | 2009-08-20 18:58:19 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | const std::set<std::string>& extensions() { return extensions_; } |
[email protected] | 4559a7d | 2009-12-02 01:42:41 | [diff] [blame] | 79 | const std::map<std::string, std::string>& versions() { return versions_; } |
[email protected] | b6ab96d | 2009-08-20 18:58:19 | [diff] [blame] | 80 | |
| 81 | private: |
[email protected] | b6ab96d | 2009-08-20 18:58:19 | [diff] [blame] | 82 | std::set<std::string> extensions_; |
[email protected] | 4559a7d | 2009-12-02 01:42:41 | [diff] [blame] | 83 | std::map<std::string, std::string> versions_; |
[email protected] | b6ab96d | 2009-08-20 18:58:19 | [diff] [blame] | 84 | }; |
| 85 | |
[email protected] | 2111b1a | 2010-03-12 18:12:44 | [diff] [blame] | 86 | static bool ShouldReloadExtensionManifest(const ExtensionInfo& info) { |
| 87 | // Always reload LOAD extension manifests, because they can change on disk |
| 88 | // independent of the manifest in our prefs. |
| 89 | if (info.extension_location == Extension::LOAD) |
| 90 | return true; |
| 91 | |
| 92 | // Otherwise, reload the manifest it needs to be relocalized. |
| 93 | return extension_l10n_util::ShouldRelocalizeManifest(info); |
| 94 | } |
| 95 | |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 96 | } // namespace |
[email protected] | b6ab96d | 2009-08-20 18:58:19 | [diff] [blame] | 97 | |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 98 | // ExtensionsService. |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 99 | |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 100 | const char* ExtensionsService::kInstallDirectoryName = "Extensions"; |
| 101 | const char* ExtensionsService::kCurrentVersionFileName = "Current Version"; |
[email protected] | 494c06e | 2009-07-25 01:06:42 | [diff] [blame] | 102 | |
[email protected] | b7c2f25 | 2009-12-08 00:47:23 | [diff] [blame] | 103 | // static |
| 104 | bool ExtensionsService::IsDownloadFromGallery(const GURL& download_url, |
| 105 | const GURL& referrer_url) { |
| 106 | if (StartsWithASCII(download_url.spec(), |
| 107 | extension_urls::kMiniGalleryDownloadPrefix, false) && |
| 108 | StartsWithASCII(referrer_url.spec(), |
| 109 | extension_urls::kMiniGalleryBrowsePrefix, false)) { |
| 110 | return true; |
| 111 | } |
| 112 | |
| 113 | if (StartsWithASCII(download_url.spec(), |
| 114 | extension_urls::kGalleryDownloadPrefix, false) && |
| 115 | StartsWithASCII(referrer_url.spec(), |
| 116 | extension_urls::kGalleryBrowsePrefix, false)) { |
| 117 | return true; |
| 118 | } |
| 119 | |
| 120 | return false; |
| 121 | } |
| 122 | |
[email protected] | ac02528 | 2009-12-16 19:16:38 | [diff] [blame] | 123 | bool ExtensionsService::IsDownloadFromMiniGallery(const GURL& download_url) { |
| 124 | return StartsWithASCII(download_url.spec(), |
| 125 | extension_urls::kMiniGalleryDownloadPrefix, |
| 126 | false); // case_sensitive |
| 127 | } |
| 128 | |
[email protected] | 81e6378 | 2009-02-27 19:35:09 | [diff] [blame] | 129 | ExtensionsService::ExtensionsService(Profile* profile, |
[email protected] | 36a784c | 2009-06-23 06:21:08 | [diff] [blame] | 130 | const CommandLine* command_line, |
[email protected] | a9b00ac | 2009-06-25 21:03:23 | [diff] [blame] | 131 | PrefService* prefs, |
| 132 | const FilePath& install_directory, |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 133 | bool autoupdate_enabled) |
[email protected] | 6ef635e4 | 2009-07-26 06:16:12 | [diff] [blame] | 134 | : profile_(profile), |
| 135 | extension_prefs_(new ExtensionPrefs(prefs, install_directory)), |
[email protected] | a9b00ac | 2009-06-25 21:03:23 | [diff] [blame] | 136 | install_directory_(install_directory), |
[email protected] | 6d60703b | 2009-08-29 01:29:23 | [diff] [blame] | 137 | extensions_enabled_(true), |
[email protected] | e81dba3 | 2009-06-19 20:19:13 | [diff] [blame] | 138 | show_extensions_prompts_(true), |
[email protected] | e0360f2c | 2009-12-07 22:34:31 | [diff] [blame] | 139 | ready_(false), |
| 140 | ALLOW_THIS_IN_INITIALIZER_LIST(toolbar_model_(this)) { |
[email protected] | 36a784c | 2009-06-23 06:21:08 | [diff] [blame] | 141 | // Figure out if extension installation should be enabled. |
[email protected] | 6d60703b | 2009-08-29 01:29:23 | [diff] [blame] | 142 | if (command_line->HasSwitch(switches::kDisableExtensions)) { |
| 143 | extensions_enabled_ = false; |
| 144 | } else if (profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions)) { |
| 145 | extensions_enabled_ = false; |
[email protected] | 6b75ec3 | 2009-08-14 06:37:18 | [diff] [blame] | 146 | } |
[email protected] | 36a784c | 2009-06-23 06:21:08 | [diff] [blame] | 147 | |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 148 | registrar_.Add(this, NotificationType::EXTENSION_HOST_DID_STOP_LOADING, |
| 149 | NotificationService::AllSources()); |
[email protected] | a4ed628 | 2009-12-14 20:51:16 | [diff] [blame] | 150 | registrar_.Add(this, NotificationType::EXTENSION_PROCESS_TERMINATED, |
[email protected] | 31f7726 | 2009-12-02 20:48:53 | [diff] [blame] | 151 | Source<Profile>(profile_)); |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 152 | |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 153 | // Set up the ExtensionUpdater |
| 154 | if (autoupdate_enabled) { |
| 155 | int update_frequency = kDefaultUpdateFrequencySeconds; |
| 156 | if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) { |
[email protected] | c4e52f0d | 2009-11-06 19:55:16 | [diff] [blame] | 157 | update_frequency = StringToInt(command_line->GetSwitchValueASCII( |
| 158 | switches::kExtensionsUpdateFrequency)); |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 159 | } |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 160 | updater_ = new ExtensionUpdater(this, prefs, update_frequency); |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 161 | } |
| 162 | |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 163 | backend_ = new ExtensionsServiceBackend(install_directory_); |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | ExtensionsService::~ExtensionsService() { |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 167 | UnloadAllExtensions(); |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 168 | if (updater_.get()) { |
| 169 | updater_->Stop(); |
| 170 | } |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 171 | } |
| 172 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 173 | void ExtensionsService::Init() { |
[email protected] | c6e4a341 | 2009-06-24 15:45:29 | [diff] [blame] | 174 | DCHECK(!ready_); |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 175 | DCHECK_EQ(extensions_.size(), 0u); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 176 | |
[email protected] | 95dd38f | 2009-10-20 20:09:15 | [diff] [blame] | 177 | // Hack: we need to ensure the ResourceDispatcherHost is ready before we load |
| 178 | // the first extension, because its members listen for loaded notifications. |
| 179 | g_browser_process->resource_dispatcher_host(); |
| 180 | |
[email protected] | de768a83 | 2009-10-30 05:25:01 | [diff] [blame] | 181 | // Start up the extension event routers. |
| 182 | ExtensionHistoryEventRouter::GetInstance()->ObserveProfile(profile_); |
[email protected] | 5cbe1e2 | 2010-01-30 01:18:56 | [diff] [blame] | 183 | ExtensionAccessibilityEventRouter::GetInstance()->ObserveProfile(profile_); |
[email protected] | de768a83 | 2009-10-30 05:25:01 | [diff] [blame] | 184 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 185 | LoadAllExtensions(); |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 186 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 187 | // TODO(erikkay) this should probably be deferred to a future point |
| 188 | // rather than running immediately at startup. |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 189 | CheckForExternalUpdates(); |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 190 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 191 | // TODO(erikkay) this should probably be deferred as well. |
| 192 | GarbageCollectExtensions(); |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 193 | } |
| 194 | |
[email protected] | 3cf4f099 | 2009-02-03 23:00:30 | [diff] [blame] | 195 | void ExtensionsService::InstallExtension(const FilePath& extension_path) { |
[email protected] | 6dfbbf8 | 2010-03-12 23:09:16 | [diff] [blame] | 196 | scoped_refptr<CrxInstaller> installer( |
| 197 | new CrxInstaller(install_directory_, |
| 198 | this, // frontend |
| 199 | NULL)); // no client (silent install) |
| 200 | installer->set_allow_privilege_increase(true); |
| 201 | installer->InstallCrx(extension_path); |
[email protected] | 3cf4f099 | 2009-02-03 23:00:30 | [diff] [blame] | 202 | } |
| 203 | |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 204 | void ExtensionsService::UpdateExtension(const std::string& id, |
[email protected] | 5c851620 | 2010-03-18 21:43:34 | [diff] [blame^] | 205 | const FilePath& extension_path, |
| 206 | const GURL& download_url) { |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 207 | if (!GetExtensionByIdInternal(id, true, true)) { |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 208 | LOG(WARNING) << "Will not update extension " << id << " because it is not " |
[email protected] | 4c96793 | 2009-07-31 01:15:49 | [diff] [blame] | 209 | << "installed"; |
| 210 | return; |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 211 | } |
| 212 | |
[email protected] | 6dfbbf8 | 2010-03-12 23:09:16 | [diff] [blame] | 213 | scoped_refptr<CrxInstaller> installer( |
| 214 | new CrxInstaller(install_directory_, |
| 215 | this, // frontend |
| 216 | NULL)); // no client (silent install) |
| 217 | installer->set_expected_id(id); |
| 218 | installer->set_delete_source(true); |
[email protected] | 5c851620 | 2010-03-18 21:43:34 | [diff] [blame^] | 219 | installer->set_force_app_origin_to_download_url(true); |
| 220 | installer->set_original_url(download_url); |
[email protected] | 6dfbbf8 | 2010-03-12 23:09:16 | [diff] [blame] | 221 | installer->InstallCrx(extension_path); |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 222 | } |
| 223 | |
[email protected] | 9cddd470 | 2009-07-27 22:09:40 | [diff] [blame] | 224 | void ExtensionsService::ReloadExtension(const std::string& extension_id) { |
[email protected] | b65272f | 2009-08-31 15:47:06 | [diff] [blame] | 225 | FilePath path; |
[email protected] | 61b41161 | 2009-11-10 23:17:41 | [diff] [blame] | 226 | Extension* current_extension = GetExtensionById(extension_id, false); |
[email protected] | 9cddd470 | 2009-07-27 22:09:40 | [diff] [blame] | 227 | |
[email protected] | b65272f | 2009-08-31 15:47:06 | [diff] [blame] | 228 | // Unload the extension if it's loaded. It might not be loaded if it crashed. |
| 229 | if (current_extension) { |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 230 | // If the extension has an inspector open for its background page, detach |
| 231 | // the inspector and hang onto a cookie for it, so that we can reattach |
| 232 | // later. |
| 233 | ExtensionProcessManager* manager = profile_->GetExtensionProcessManager(); |
| 234 | ExtensionHost* host = manager->GetBackgroundHostForExtension( |
| 235 | current_extension); |
| 236 | if (host) { |
| 237 | // Look for an open inspector for the background page. |
| 238 | int devtools_cookie = DevToolsManager::GetInstance()->DetachClientHost( |
| 239 | host->render_view_host()); |
| 240 | if (devtools_cookie >= 0) |
| 241 | orphaned_dev_tools_[extension_id] = devtools_cookie; |
| 242 | } |
| 243 | |
[email protected] | b65272f | 2009-08-31 15:47:06 | [diff] [blame] | 244 | path = current_extension->path(); |
| 245 | UnloadExtension(extension_id); |
[email protected] | 1eb17508 | 2010-02-10 09:26:16 | [diff] [blame] | 246 | } else { |
| 247 | path = unloaded_extension_paths_[extension_id]; |
[email protected] | b65272f | 2009-08-31 15:47:06 | [diff] [blame] | 248 | } |
| 249 | |
[email protected] | 1eb17508 | 2010-02-10 09:26:16 | [diff] [blame] | 250 | // We should always be able to remember the extension's path. If it's not in |
| 251 | // the map, someone failed to update |unloaded_extension_paths_|. |
| 252 | CHECK(!path.empty()); |
[email protected] | b65272f | 2009-08-31 15:47:06 | [diff] [blame] | 253 | |
[email protected] | 1eb17508 | 2010-02-10 09:26:16 | [diff] [blame] | 254 | LoadExtension(path); |
[email protected] | 9cddd470 | 2009-07-27 22:09:40 | [diff] [blame] | 255 | } |
| 256 | |
[email protected] | 27b985d | 2009-06-25 17:53:15 | [diff] [blame] | 257 | void ExtensionsService::UninstallExtension(const std::string& extension_id, |
| 258 | bool external_uninstall) { |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 259 | Extension* extension = GetExtensionByIdInternal(extension_id, true, true); |
[email protected] | 631cf82 | 2009-05-15 07:01:25 | [diff] [blame] | 260 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 261 | // Callers should not send us nonexistant extensions. |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 262 | DCHECK(extension); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 263 | |
[email protected] | 27b985d | 2009-06-25 17:53:15 | [diff] [blame] | 264 | extension_prefs_->OnExtensionUninstalled(extension, external_uninstall); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 265 | |
| 266 | // Tell the backend to start deleting installed extensions on the file thread. |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 267 | if (Extension::LOAD != extension->location()) { |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 268 | ChromeThread::PostTask( |
| 269 | ChromeThread::FILE, FROM_HERE, |
| 270 | NewRunnableFunction( |
| 271 | &extension_file_util::UninstallExtension, extension_id, |
| 272 | install_directory_)); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 273 | } |
| 274 | |
[email protected] | 86c008e8 | 2009-08-28 20:26:05 | [diff] [blame] | 275 | ExtensionDOMUI::UnregisterChromeURLOverrides(profile_, |
| 276 | extension->GetChromeURLOverrides()); |
| 277 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 278 | UnloadExtension(extension_id); |
| 279 | } |
| 280 | |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 281 | void ExtensionsService::EnableExtension(const std::string& extension_id) { |
| 282 | Extension* extension = GetExtensionByIdInternal(extension_id, false, true); |
| 283 | if (!extension) { |
| 284 | NOTREACHED() << "Trying to enable an extension that isn't disabled."; |
| 285 | return; |
| 286 | } |
| 287 | |
[email protected] | e8c729a | 2010-03-09 19:55:19 | [diff] [blame] | 288 | extension_prefs_->SetExtensionState(extension, Extension::ENABLED); |
[email protected] | 1784e83a | 2009-09-08 21:01:52 | [diff] [blame] | 289 | |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 290 | // Move it over to the enabled list. |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 291 | extensions_.push_back(extension); |
| 292 | ExtensionList::iterator iter = std::find(disabled_extensions_.begin(), |
| 293 | disabled_extensions_.end(), |
| 294 | extension); |
| 295 | disabled_extensions_.erase(iter); |
| 296 | |
[email protected] | 86c008e8 | 2009-08-28 20:26:05 | [diff] [blame] | 297 | ExtensionDOMUI::RegisterChromeURLOverrides(profile_, |
| 298 | extension->GetChromeURLOverrides()); |
| 299 | |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 300 | NotifyExtensionLoaded(extension); |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 301 | UpdateActiveExtensionsInCrashReporter(); |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 302 | } |
| 303 | |
[email protected] | 1784e83a | 2009-09-08 21:01:52 | [diff] [blame] | 304 | void ExtensionsService::DisableExtension(const std::string& extension_id) { |
| 305 | Extension* extension = GetExtensionByIdInternal(extension_id, true, false); |
[email protected] | b2ba996 | 2009-12-10 20:10:15 | [diff] [blame] | 306 | // The extension may have been disabled already. |
| 307 | if (!extension) |
[email protected] | 1784e83a | 2009-09-08 21:01:52 | [diff] [blame] | 308 | return; |
[email protected] | 1784e83a | 2009-09-08 21:01:52 | [diff] [blame] | 309 | |
[email protected] | e8c729a | 2010-03-09 19:55:19 | [diff] [blame] | 310 | extension_prefs_->SetExtensionState(extension, Extension::DISABLED); |
[email protected] | 1784e83a | 2009-09-08 21:01:52 | [diff] [blame] | 311 | |
| 312 | // Move it over to the disabled list. |
| 313 | disabled_extensions_.push_back(extension); |
| 314 | ExtensionList::iterator iter = std::find(extensions_.begin(), |
| 315 | extensions_.end(), |
| 316 | extension); |
| 317 | extensions_.erase(iter); |
| 318 | |
| 319 | ExtensionDOMUI::UnregisterChromeURLOverrides(profile_, |
| 320 | extension->GetChromeURLOverrides()); |
| 321 | |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 322 | NotifyExtensionUnloaded(extension); |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 323 | UpdateActiveExtensionsInCrashReporter(); |
[email protected] | 1784e83a | 2009-09-08 21:01:52 | [diff] [blame] | 324 | } |
| 325 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 326 | void ExtensionsService::LoadExtension(const FilePath& extension_path) { |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 327 | ChromeThread::PostTask( |
| 328 | ChromeThread::FILE, FROM_HERE, |
| 329 | NewRunnableMethod( |
| 330 | backend_.get(), |
| 331 | &ExtensionsServiceBackend::LoadSingleExtension, |
| 332 | extension_path, scoped_refptr<ExtensionsService>(this))); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 333 | } |
| 334 | |
[email protected] | 1952c7d | 2010-03-04 23:48:34 | [diff] [blame] | 335 | void ExtensionsService::LoadComponentExtensions() { |
| 336 | for (RegisteredComponentExtensions::iterator it = |
| 337 | component_extension_manifests_.begin(); |
| 338 | it != component_extension_manifests_.end(); ++it) { |
| 339 | JSONStringValueSerializer serializer(it->manifest); |
| 340 | scoped_ptr<Value> manifest(serializer.Deserialize(NULL)); |
| 341 | DCHECK(manifest.get()); |
| 342 | |
| 343 | scoped_ptr<Extension> extension(new Extension(it->root_directory)); |
| 344 | extension->set_location(Extension::COMPONENT); |
| 345 | |
| 346 | std::string error; |
| 347 | if (!extension->InitFromValue( |
| 348 | *static_cast<DictionaryValue*>(manifest.get()), |
| 349 | true, // require key |
| 350 | &error)) { |
| 351 | NOTREACHED(); |
| 352 | return; |
| 353 | } |
| 354 | |
| 355 | OnExtensionLoaded(extension.release(), false); // Don't allow privilege |
| 356 | // increase. |
| 357 | } |
| 358 | } |
| 359 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 360 | void ExtensionsService::LoadAllExtensions() { |
[email protected] | cc2c343 | 2009-11-06 17:24:36 | [diff] [blame] | 361 | base::TimeTicks start_time = base::TimeTicks::Now(); |
| 362 | |
[email protected] | 1952c7d | 2010-03-04 23:48:34 | [diff] [blame] | 363 | // Load any component extensions. |
| 364 | LoadComponentExtensions(); |
| 365 | |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 366 | // Load the previously installed extensions. |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 367 | scoped_ptr<ExtensionPrefs::ExtensionsInfo> info( |
| 368 | ExtensionPrefs::CollectExtensionsInfo(extension_prefs_.get())); |
| 369 | |
| 370 | // If any extensions need localization, we bounce them all to the file thread |
| 371 | // for re-reading and localization. |
| 372 | for (size_t i = 0; i < info->size(); ++i) { |
[email protected] | 2111b1a | 2010-03-12 18:12:44 | [diff] [blame] | 373 | if (ShouldReloadExtensionManifest(*info->at(i))) { |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 374 | ChromeThread::PostTask( |
| 375 | ChromeThread::FILE, FROM_HERE, NewRunnableMethod( |
| 376 | backend_.get(), |
[email protected] | 2111b1a | 2010-03-12 18:12:44 | [diff] [blame] | 377 | &ExtensionsServiceBackend::ReloadExtensionManifests, |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 378 | info.release(), // Callee takes ownership of the memory. |
| 379 | start_time, |
| 380 | scoped_refptr<ExtensionsService>(this))); |
| 381 | return; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | // Don't update prefs. |
| 386 | // Callee takes ownership of the memory. |
| 387 | ContinueLoadAllExtensions(info.release(), start_time, false); |
| 388 | } |
| 389 | |
| 390 | void ExtensionsService::ContinueLoadAllExtensions( |
| 391 | ExtensionPrefs::ExtensionsInfo* extensions_info, |
| 392 | base::TimeTicks start_time, |
| 393 | bool write_to_prefs) { |
| 394 | scoped_ptr<ExtensionPrefs::ExtensionsInfo> info(extensions_info); |
| 395 | |
| 396 | for (size_t i = 0; i < info->size(); ++i) { |
| 397 | LoadInstalledExtension(*info->at(i), write_to_prefs); |
| 398 | } |
| 399 | |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 400 | OnLoadedInstalledExtensions(); |
[email protected] | cc2c343 | 2009-11-06 17:24:36 | [diff] [blame] | 401 | |
| 402 | UMA_HISTOGRAM_COUNTS_100("Extensions.LoadAll", extensions_.size()); |
| 403 | UMA_HISTOGRAM_COUNTS_100("Extensions.Disabled", disabled_extensions_.size()); |
| 404 | |
[email protected] | 1952c7d | 2010-03-04 23:48:34 | [diff] [blame] | 405 | UMA_HISTOGRAM_TIMES("Extensions.LoadAllTime", |
| 406 | base::TimeTicks::Now() - start_time); |
[email protected] | cc2c343 | 2009-11-06 17:24:36 | [diff] [blame] | 407 | |
[email protected] | 1952c7d | 2010-03-04 23:48:34 | [diff] [blame] | 408 | int user_script_count = 0; |
| 409 | int extension_count = 0; |
| 410 | int theme_count = 0; |
| 411 | int external_count = 0; |
| 412 | int page_action_count = 0; |
| 413 | int browser_action_count = 0; |
| 414 | ExtensionList::iterator ex; |
| 415 | for (ex = extensions_.begin(); ex != extensions_.end(); ++ex) { |
| 416 | // Don't count component extensions, since they are only extensions as an |
| 417 | // implementation detail. |
| 418 | if ((*ex)->location() == Extension::COMPONENT) |
| 419 | continue; |
| 420 | |
[email protected] | e8c729a | 2010-03-09 19:55:19 | [diff] [blame] | 421 | // Don't count unpacked extensions, since they're a developer-specific |
| 422 | // feature. |
| 423 | if ((*ex)->location() == Extension::LOAD) |
| 424 | continue; |
| 425 | |
[email protected] | 1952c7d | 2010-03-04 23:48:34 | [diff] [blame] | 426 | if ((*ex)->IsTheme()) { |
| 427 | theme_count++; |
| 428 | } else if ((*ex)->converted_from_user_script()) { |
| 429 | user_script_count++; |
| 430 | } else { |
| 431 | extension_count++; |
[email protected] | cc2c343 | 2009-11-06 17:24:36 | [diff] [blame] | 432 | } |
[email protected] | 1952c7d | 2010-03-04 23:48:34 | [diff] [blame] | 433 | if (Extension::IsExternalLocation((*ex)->location())) { |
| 434 | external_count++; |
| 435 | } |
| 436 | if ((*ex)->page_action() != NULL) { |
| 437 | page_action_count++; |
| 438 | } |
| 439 | if ((*ex)->browser_action() != NULL) { |
| 440 | browser_action_count++; |
| 441 | } |
[email protected] | cc2c343 | 2009-11-06 17:24:36 | [diff] [blame] | 442 | } |
[email protected] | 1952c7d | 2010-03-04 23:48:34 | [diff] [blame] | 443 | UMA_HISTOGRAM_COUNTS_100("Extensions.LoadExtension", extension_count); |
| 444 | UMA_HISTOGRAM_COUNTS_100("Extensions.LoadUserScript", user_script_count); |
| 445 | UMA_HISTOGRAM_COUNTS_100("Extensions.LoadTheme", theme_count); |
| 446 | UMA_HISTOGRAM_COUNTS_100("Extensions.LoadExternal", external_count); |
| 447 | UMA_HISTOGRAM_COUNTS_100("Extensions.LoadPageAction", page_action_count); |
| 448 | UMA_HISTOGRAM_COUNTS_100("Extensions.LoadBrowserAction", |
| 449 | browser_action_count); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 450 | } |
| 451 | |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 452 | void ExtensionsService::LoadInstalledExtension(const ExtensionInfo& info, |
| 453 | bool write_to_prefs) { |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 454 | std::string error; |
| 455 | Extension* extension = NULL; |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 456 | if (info.extension_manifest.get()) { |
| 457 | scoped_ptr<Extension> tmp(new Extension(info.extension_path)); |
[email protected] | e8c729a | 2010-03-09 19:55:19 | [diff] [blame] | 458 | bool require_key = info.extension_location != Extension::LOAD; |
| 459 | if (tmp->InitFromValue(*info.extension_manifest, require_key, &error)) |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 460 | extension = tmp.release(); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 461 | } else { |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 462 | error = errors::kManifestUnreadable; |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | if (!extension) { |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 466 | ReportExtensionLoadError(info.extension_path, |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 467 | error, |
| 468 | NotificationType::EXTENSION_INSTALL_ERROR, |
| 469 | false); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 470 | return; |
| 471 | } |
| 472 | |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 473 | extension->set_location(info.extension_location); |
| 474 | |
| 475 | if (write_to_prefs) |
| 476 | extension_prefs_->UpdateManifest(extension); |
| 477 | |
[email protected] | 2a40953 | 2009-08-28 19:39:44 | [diff] [blame] | 478 | OnExtensionLoaded(extension, true); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 479 | |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 480 | if (info.extension_location == Extension::EXTERNAL_PREF || |
| 481 | info.extension_location == Extension::EXTERNAL_REGISTRY) { |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 482 | ChromeThread::PostTask( |
| 483 | ChromeThread::FILE, FROM_HERE, |
| 484 | NewRunnableMethod( |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 485 | backend_.get(), |
| 486 | &ExtensionsServiceBackend::CheckExternalUninstall, |
| 487 | scoped_refptr<ExtensionsService>(this), |
| 488 | info.extension_id, |
| 489 | info.extension_location)); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 490 | } |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 491 | } |
| 492 | |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 493 | void ExtensionsService::NotifyExtensionLoaded(Extension* extension) { |
| 494 | LOG(INFO) << "Sending EXTENSION_LOADED"; |
| 495 | |
| 496 | // The ChromeURLRequestContext needs to be first to know that the extension |
| 497 | // was loaded, otherwise a race can arise where a renderer that is created |
| 498 | // for the extension may try to load an extension URL with an extension id |
| 499 | // that the request context doesn't yet know about. |
| 500 | if (profile_ && !profile_->IsOffTheRecord()) { |
[email protected] | be180c80 | 2009-10-23 06:33:31 | [diff] [blame] | 501 | ChromeURLRequestContextGetter* context_getter = |
| 502 | static_cast<ChromeURLRequestContextGetter*>( |
| 503 | profile_->GetRequestContext()); |
| 504 | if (context_getter) { |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 505 | ChromeThread::PostTask( |
| 506 | ChromeThread::IO, FROM_HERE, |
| 507 | NewRunnableMethod( |
| 508 | context_getter, |
| 509 | &ChromeURLRequestContextGetter::OnNewExtensions, |
| 510 | extension->id(), |
[email protected] | 0ce3f598 | 2010-01-28 23:04:27 | [diff] [blame] | 511 | new ChromeURLRequestContext::ExtensionInfo( |
| 512 | extension->path(), |
| 513 | extension->default_locale(), |
[email protected] | b30e0dd | 2010-01-29 23:33:21 | [diff] [blame] | 514 | extension->app_extent(), |
[email protected] | 0ce3f598 | 2010-01-28 23:04:27 | [diff] [blame] | 515 | extension->api_permissions()))); |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 516 | } |
[email protected] | 24b538a | 2010-02-27 01:22:44 | [diff] [blame] | 517 | |
| 518 | // Check if this permission requires unlimited storage quota |
| 519 | if (extension->HasApiPermission(Extension::kUnlimitedStoragePermission)) { |
| 520 | string16 origin_identifier = |
| 521 | webkit_database::DatabaseUtil::GetOriginIdentifier(extension->url()); |
| 522 | ChromeThread::PostTask( |
| 523 | ChromeThread::FILE, FROM_HERE, |
| 524 | NewRunnableMethod( |
| 525 | profile_->GetDatabaseTracker(), |
| 526 | &webkit_database::DatabaseTracker::SetOriginQuotaInMemory, |
| 527 | origin_identifier, |
| 528 | kint64max)); |
| 529 | } |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | NotificationService::current()->Notify( |
| 533 | NotificationType::EXTENSION_LOADED, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 534 | Source<Profile>(profile_), |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 535 | Details<Extension>(extension)); |
| 536 | } |
| 537 | |
| 538 | void ExtensionsService::NotifyExtensionUnloaded(Extension* extension) { |
| 539 | LOG(INFO) << "Sending EXTENSION_UNLOADED"; |
| 540 | |
| 541 | NotificationService::current()->Notify( |
| 542 | NotificationType::EXTENSION_UNLOADED, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 543 | Source<Profile>(profile_), |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 544 | Details<Extension>(extension)); |
| 545 | |
| 546 | if (profile_ && !profile_->IsOffTheRecord()) { |
[email protected] | be180c80 | 2009-10-23 06:33:31 | [diff] [blame] | 547 | ChromeURLRequestContextGetter* context_getter = |
| 548 | static_cast<ChromeURLRequestContextGetter*>( |
| 549 | profile_->GetRequestContext()); |
| 550 | if (context_getter) { |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 551 | ChromeThread::PostTask( |
| 552 | ChromeThread::IO, FROM_HERE, |
[email protected] | be180c80 | 2009-10-23 06:33:31 | [diff] [blame] | 553 | NewRunnableMethod( |
| 554 | context_getter, |
| 555 | &ChromeURLRequestContextGetter::OnUnloadedExtension, |
| 556 | extension->id())); |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 557 | } |
| 558 | } |
| 559 | } |
| 560 | |
[email protected] | 6b75ec3 | 2009-08-14 06:37:18 | [diff] [blame] | 561 | void ExtensionsService::UpdateExtensionBlacklist( |
| 562 | const std::vector<std::string>& blacklist) { |
| 563 | // Use this set to indicate if an extension in the blacklist has been used. |
| 564 | std::set<std::string> blacklist_set; |
| 565 | for (unsigned int i = 0; i < blacklist.size(); ++i) { |
| 566 | if (Extension::IdIsValid(blacklist[i])) { |
| 567 | blacklist_set.insert(blacklist[i]); |
| 568 | } |
| 569 | } |
| 570 | extension_prefs_->UpdateBlacklist(blacklist_set); |
| 571 | std::vector<std::string> to_be_removed; |
| 572 | // Loop current extensions, unload installed extensions. |
| 573 | for (ExtensionList::const_iterator iter = extensions_.begin(); |
| 574 | iter != extensions_.end(); ++iter) { |
| 575 | Extension* extension = (*iter); |
| 576 | if (blacklist_set.find(extension->id()) != blacklist_set.end()) { |
| 577 | to_be_removed.push_back(extension->id()); |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | // UnloadExtension will change the extensions_ list. So, we should |
| 582 | // call it outside the iterator loop. |
| 583 | for (unsigned int i = 0; i < to_be_removed.size(); ++i) { |
| 584 | UnloadExtension(to_be_removed[i]); |
| 585 | } |
| 586 | } |
| 587 | |
[email protected] | 5ef47ec | 2010-01-28 05:58:05 | [diff] [blame] | 588 | void ExtensionsService::SetLastPingDay(const std::string& extension_id, |
| 589 | const base::Time& time) { |
| 590 | extension_prefs_->SetLastPingDay(extension_id, time); |
| 591 | } |
| 592 | |
| 593 | base::Time ExtensionsService::LastPingDay(const std::string& extension_id) { |
| 594 | return extension_prefs_->LastPingDay(extension_id); |
| 595 | } |
| 596 | |
[email protected] | cb0ce1e02 | 2010-03-10 19:54:41 | [diff] [blame] | 597 | bool ExtensionsService::IsIncognitoEnabled(const Extension* extension) { |
| 598 | // If this is a component extension we always allow it to work in incognito |
| 599 | // mode. |
| 600 | if (extension->location() == Extension::COMPONENT) |
| 601 | return true; |
| 602 | |
| 603 | // Check the prefs. |
| 604 | return extension_prefs_->IsIncognitoEnabled(extension->id()); |
[email protected] | db7331a | 2010-02-25 22:10:50 | [diff] [blame] | 605 | } |
[email protected] | 55a3569 | 2010-02-11 23:25:21 | [diff] [blame] | 606 | |
[email protected] | cb0ce1e02 | 2010-03-10 19:54:41 | [diff] [blame] | 607 | void ExtensionsService::SetIsIncognitoEnabled(Extension* extension, |
[email protected] | db7331a | 2010-02-25 22:10:50 | [diff] [blame] | 608 | bool enabled) { |
[email protected] | cb0ce1e02 | 2010-03-10 19:54:41 | [diff] [blame] | 609 | extension_prefs_->SetIsIncognitoEnabled(extension->id(), enabled); |
[email protected] | c1499f3d | 2010-03-05 00:33:24 | [diff] [blame] | 610 | |
| 611 | // Broadcast unloaded and loaded events to update browser state. |
| 612 | NotifyExtensionUnloaded(extension); |
| 613 | NotifyExtensionLoaded(extension); |
[email protected] | 55a3569 | 2010-02-11 23:25:21 | [diff] [blame] | 614 | } |
| 615 | |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 616 | void ExtensionsService::CheckForExternalUpdates() { |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 617 | // This installs or updates externally provided extensions. |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 618 | // TODO(aa): Why pass this list into the provider, why not just filter it |
| 619 | // later? |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 620 | std::set<std::string> killed_extensions; |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 621 | extension_prefs_->GetKilledExtensionIds(&killed_extensions); |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 622 | ChromeThread::PostTask( |
| 623 | ChromeThread::FILE, FROM_HERE, |
| 624 | NewRunnableMethod( |
| 625 | backend_.get(), &ExtensionsServiceBackend::CheckForExternalUpdates, |
| 626 | killed_extensions, scoped_refptr<ExtensionsService>(this))); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | void ExtensionsService::UnloadExtension(const std::string& extension_id) { |
[email protected] | 27e469a | 2010-01-11 20:35:09 | [diff] [blame] | 630 | // Make sure the extension gets deleted after we return from this function. |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 631 | scoped_ptr<Extension> extension( |
| 632 | GetExtensionByIdInternal(extension_id, true, true)); |
[email protected] | 631cf82 | 2009-05-15 07:01:25 | [diff] [blame] | 633 | |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 634 | // Callers should not send us nonexistant extensions. |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 635 | CHECK(extension.get()); |
| 636 | |
[email protected] | 1eb17508 | 2010-02-10 09:26:16 | [diff] [blame] | 637 | // Keep information about the extension so that we can reload it later |
| 638 | // even if it's not permanently installed. |
| 639 | unloaded_extension_paths_[extension->id()] = extension->path(); |
| 640 | |
[email protected] | 86c008e8 | 2009-08-28 20:26:05 | [diff] [blame] | 641 | ExtensionDOMUI::UnregisterChromeURLOverrides(profile_, |
| 642 | extension->GetChromeURLOverrides()); |
| 643 | |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 644 | ExtensionList::iterator iter = std::find(disabled_extensions_.begin(), |
| 645 | disabled_extensions_.end(), |
| 646 | extension.get()); |
| 647 | if (iter != disabled_extensions_.end()) { |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 648 | disabled_extensions_.erase(iter); |
[email protected] | 86693068 | 2009-08-18 22:53:47 | [diff] [blame] | 649 | NotificationService::current()->Notify( |
| 650 | NotificationType::EXTENSION_UNLOADED_DISABLED, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 651 | Source<Profile>(profile_), |
[email protected] | 86693068 | 2009-08-18 22:53:47 | [diff] [blame] | 652 | Details<Extension>(extension.get())); |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 653 | return; |
| 654 | } |
| 655 | |
| 656 | iter = std::find(extensions_.begin(), extensions_.end(), extension.get()); |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 657 | |
[email protected] | 631cf82 | 2009-05-15 07:01:25 | [diff] [blame] | 658 | // Remove the extension from our list. |
| 659 | extensions_.erase(iter); |
| 660 | |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 661 | NotifyExtensionUnloaded(extension.get()); |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 662 | UpdateActiveExtensionsInCrashReporter(); |
[email protected] | 631cf82 | 2009-05-15 07:01:25 | [diff] [blame] | 663 | } |
| 664 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 665 | void ExtensionsService::UnloadAllExtensions() { |
| 666 | ExtensionList::iterator iter; |
[email protected] | c6e4a341 | 2009-06-24 15:45:29 | [diff] [blame] | 667 | for (iter = extensions_.begin(); iter != extensions_.end(); ++iter) |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 668 | delete *iter; |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 669 | extensions_.clear(); |
[email protected] | c6e4a341 | 2009-06-24 15:45:29 | [diff] [blame] | 670 | |
| 671 | // TODO(erikkay) should there be a notification for this? We can't use |
| 672 | // EXTENSION_UNLOADED since that implies that the extension has been disabled |
| 673 | // or uninstalled, and UnloadAll is just part of shutdown. |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | void ExtensionsService::ReloadExtensions() { |
| 677 | UnloadAllExtensions(); |
| 678 | LoadAllExtensions(); |
| 679 | } |
| 680 | |
| 681 | void ExtensionsService::GarbageCollectExtensions() { |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 682 | InstalledExtensionSet installed(extension_prefs_.get()); |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 683 | ChromeThread::PostTask( |
| 684 | ChromeThread::FILE, FROM_HERE, |
| 685 | NewRunnableFunction( |
| 686 | &extension_file_util::GarbageCollectExtensions, install_directory_, |
[email protected] | 4559a7d | 2009-12-02 01:42:41 | [diff] [blame] | 687 | installed.extensions(), installed.versions())); |
[email protected] | 3cf4f099 | 2009-02-03 23:00:30 | [diff] [blame] | 688 | } |
| 689 | |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 690 | void ExtensionsService::OnLoadedInstalledExtensions() { |
[email protected] | e81dba3 | 2009-06-19 20:19:13 | [diff] [blame] | 691 | ready_ = true; |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 692 | if (updater_.get()) { |
| 693 | updater_->Start(); |
| 694 | } |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 695 | NotificationService::current()->Notify( |
| 696 | NotificationType::EXTENSIONS_READY, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 697 | Source<Profile>(profile_), |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 698 | NotificationService::NoDetails()); |
| 699 | } |
| 700 | |
[email protected] | 2a40953 | 2009-08-28 19:39:44 | [diff] [blame] | 701 | void ExtensionsService::OnExtensionLoaded(Extension* extension, |
| 702 | bool allow_privilege_increase) { |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 703 | // Ensure extension is deleted unless we transfer ownership. |
| 704 | scoped_ptr<Extension> scoped_extension(extension); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 705 | |
[email protected] | 1eb17508 | 2010-02-10 09:26:16 | [diff] [blame] | 706 | // The extension is now loaded, remove its data from unloaded extension map. |
| 707 | unloaded_extension_paths_.erase(extension->id()); |
| 708 | |
[email protected] | ceefd3d | 2010-03-12 09:10:29 | [diff] [blame] | 709 | if (extension->IsApp() && |
| 710 | !CommandLine::ForCurrentProcess()->HasSwitch( |
| 711 | switches::kEnableExtensionApps)) { |
| 712 | ReportExtensionLoadError(extension->path(), errors::kAppsDisabled, |
| 713 | NotificationType::EXTENSION_INSTALL_ERROR, |
| 714 | true); // be noisy |
| 715 | return; |
| 716 | } |
| 717 | |
| 718 | // TODO(aa): Need to re-evaluate this branch. Does this still make sense now |
| 719 | // that extensions are enabled by default? |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 720 | if (extensions_enabled() || |
| 721 | extension->IsTheme() || |
| 722 | extension->location() == Extension::LOAD || |
| 723 | Extension::IsExternalLocation(extension->location())) { |
| 724 | Extension* old = GetExtensionByIdInternal(extension->id(), true, true); |
| 725 | if (old) { |
| 726 | if (extension->version()->CompareTo(*(old->version())) > 0) { |
[email protected] | 2a40953 | 2009-08-28 19:39:44 | [diff] [blame] | 727 | bool allow_silent_upgrade = |
| 728 | allow_privilege_increase || !Extension::IsPrivilegeIncrease( |
| 729 | old, extension); |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 730 | |
[email protected] | 1e8c93f | 2010-02-08 22:58:31 | [diff] [blame] | 731 | // Extensions get upgraded if silent upgrades are allowed, otherwise |
| 732 | // they get disabled. |
| 733 | if (allow_silent_upgrade) { |
| 734 | old->set_being_upgraded(true); |
| 735 | extension->set_being_upgraded(true); |
| 736 | } |
| 737 | |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 738 | // To upgrade an extension in place, unload the old one and |
| 739 | // then load the new one. |
| 740 | UnloadExtension(old->id()); |
| 741 | old = NULL; |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 742 | |
[email protected] | b24d831 | 2009-08-27 06:47:46 | [diff] [blame] | 743 | if (!allow_silent_upgrade) { |
[email protected] | 6d27a7b | 2009-12-18 23:25:45 | [diff] [blame] | 744 | // Extension has changed permissions significantly. Disable it. We |
| 745 | // send a notification below. |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 746 | extension_prefs_->SetExtensionState(extension, Extension::DISABLED); |
[email protected] | 7d84586 | 2010-01-04 21:28:57 | [diff] [blame] | 747 | extension_prefs_->SetShowInstallWarningOnEnable(extension, true); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 748 | } |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 749 | } else { |
| 750 | // We already have the extension of the same or older version. |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 751 | std::string error_message("Duplicate extension load attempt: "); |
| 752 | error_message += extension->id(); |
| 753 | LOG(WARNING) << error_message; |
| 754 | ReportExtensionLoadError(extension->path(), |
| 755 | error_message, |
| 756 | NotificationType::EXTENSION_OVERINSTALL_ERROR, |
| 757 | false); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 758 | return; |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 759 | } |
[email protected] | ba74f35 | 2009-06-11 18:54:45 | [diff] [blame] | 760 | } |
[email protected] | 86a27407 | 2009-06-11 02:06:45 | [diff] [blame] | 761 | |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 762 | switch (extension_prefs_->GetExtensionState(extension->id())) { |
| 763 | case Extension::ENABLED: |
| 764 | extensions_.push_back(scoped_extension.release()); |
| 765 | |
[email protected] | aeb53b3 | 2009-10-29 07:34:45 | [diff] [blame] | 766 | // We delay starting up the browser event router until at least one |
| 767 | // extension that needs it is loaded. |
| 768 | if (extension->HasApiPermission(Extension::kTabPermission)) { |
| 769 | ExtensionBrowserEventRouter::GetInstance()->Init(); |
| 770 | } |
[email protected] | 840b0db | 2009-11-20 03:00:38 | [diff] [blame] | 771 | if (extension->HasApiPermission(Extension::kBookmarkPermission)) { |
| 772 | ExtensionBookmarkEventRouter::GetSingleton()->Observe( |
| 773 | profile_->GetBookmarkModel()); |
| 774 | } |
[email protected] | aeb53b3 | 2009-10-29 07:34:45 | [diff] [blame] | 775 | |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 776 | NotifyExtensionLoaded(extension); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 777 | |
[email protected] | e8c729a | 2010-03-09 19:55:19 | [diff] [blame] | 778 | ExtensionDOMUI::RegisterChromeURLOverrides(profile_, |
| 779 | extension->GetChromeURLOverrides()); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 780 | break; |
| 781 | case Extension::DISABLED: |
[email protected] | 6d27a7b | 2009-12-18 23:25:45 | [diff] [blame] | 782 | disabled_extensions_.push_back(scoped_extension.release()); |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 783 | NotificationService::current()->Notify( |
| 784 | NotificationType::EXTENSION_UPDATE_DISABLED, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 785 | Source<Profile>(profile_), |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 786 | Details<Extension>(extension)); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 787 | break; |
| 788 | default: |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 789 | NOTREACHED(); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 790 | break; |
[email protected] | 811f343 | 2009-07-25 19:38:21 | [diff] [blame] | 791 | } |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 792 | } |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 793 | |
[email protected] | 1e8c93f | 2010-02-08 22:58:31 | [diff] [blame] | 794 | extension->set_being_upgraded(false); |
| 795 | |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 796 | UpdateActiveExtensionsInCrashReporter(); |
| 797 | } |
| 798 | |
| 799 | void ExtensionsService::UpdateActiveExtensionsInCrashReporter() { |
[email protected] | c886596 | 2009-12-16 07:47:39 | [diff] [blame] | 800 | std::set<std::string> extension_ids; |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 801 | for (size_t i = 0; i < extensions_.size(); ++i) { |
| 802 | if (!extensions_[i]->IsTheme()) |
[email protected] | c886596 | 2009-12-16 07:47:39 | [diff] [blame] | 803 | extension_ids.insert(extensions_[i]->id()); |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | child_process_logging::SetActiveExtensions(extension_ids); |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 807 | } |
| 808 | |
[email protected] | 2a40953 | 2009-08-28 19:39:44 | [diff] [blame] | 809 | void ExtensionsService::OnExtensionInstalled(Extension* extension, |
| 810 | bool allow_privilege_increase) { |
[email protected] | b6ab96d | 2009-08-20 18:58:19 | [diff] [blame] | 811 | extension_prefs_->OnExtensionInstalled(extension); |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 812 | |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 813 | // If the extension is a theme, tell the profile (and therefore ThemeProvider) |
| 814 | // to apply it. |
| 815 | if (extension->IsTheme()) { |
[email protected] | 9ceb0734 | 2009-07-26 04:09:23 | [diff] [blame] | 816 | NotificationService::current()->Notify( |
| 817 | NotificationType::THEME_INSTALLED, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 818 | Source<Profile>(profile_), |
[email protected] | 9ceb0734 | 2009-07-26 04:09:23 | [diff] [blame] | 819 | Details<Extension>(extension)); |
[email protected] | 9197f3b | 2009-06-02 00:49:27 | [diff] [blame] | 820 | } else { |
| 821 | NotificationService::current()->Notify( |
| 822 | NotificationType::EXTENSION_INSTALLED, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 823 | Source<Profile>(profile_), |
[email protected] | 9197f3b | 2009-06-02 00:49:27 | [diff] [blame] | 824 | Details<Extension>(extension)); |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 825 | } |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 826 | |
| 827 | // Also load the extension. |
[email protected] | 2a40953 | 2009-08-28 19:39:44 | [diff] [blame] | 828 | OnExtensionLoaded(extension, allow_privilege_increase); |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 829 | } |
| 830 | |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 831 | void ExtensionsService::OnExtensionOverinstallAttempted(const std::string& id) { |
[email protected] | 61b41161 | 2009-11-10 23:17:41 | [diff] [blame] | 832 | Extension* extension = GetExtensionById(id, false); |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 833 | if (extension && extension->IsTheme()) { |
[email protected] | 9ceb0734 | 2009-07-26 04:09:23 | [diff] [blame] | 834 | NotificationService::current()->Notify( |
| 835 | NotificationType::THEME_INSTALLED, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 836 | Source<Profile>(profile_), |
[email protected] | 9ceb0734 | 2009-07-26 04:09:23 | [diff] [blame] | 837 | Details<Extension>(extension)); |
[email protected] | 91e1bd8 | 2009-09-03 22:04:40 | [diff] [blame] | 838 | } else { |
| 839 | NotificationService::current()->Notify( |
| 840 | NotificationType::NO_THEME_DETECTED, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 841 | Source<Profile>(profile_), |
[email protected] | 91e1bd8 | 2009-09-03 22:04:40 | [diff] [blame] | 842 | NotificationService::NoDetails()); |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 843 | } |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 844 | } |
| 845 | |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 846 | Extension* ExtensionsService::GetExtensionByIdInternal(const std::string& id, |
| 847 | bool include_enabled, |
| 848 | bool include_disabled) { |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 849 | std::string lowercase_id = StringToLowerASCII(id); |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 850 | if (include_enabled) { |
| 851 | for (ExtensionList::const_iterator iter = extensions_.begin(); |
| 852 | iter != extensions_.end(); ++iter) { |
| 853 | if ((*iter)->id() == lowercase_id) |
| 854 | return *iter; |
| 855 | } |
| 856 | } |
| 857 | if (include_disabled) { |
| 858 | for (ExtensionList::const_iterator iter = disabled_extensions_.begin(); |
| 859 | iter != disabled_extensions_.end(); ++iter) { |
| 860 | if ((*iter)->id() == lowercase_id) |
| 861 | return *iter; |
| 862 | } |
[email protected] | ce5c450 | 2009-05-06 16:46:11 | [diff] [blame] | 863 | } |
| 864 | return NULL; |
| 865 | } |
| 866 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 867 | Extension* ExtensionsService::GetExtensionByURL(const GURL& url) { |
| 868 | std::string host = url.host(); |
[email protected] | 61b41161 | 2009-11-10 23:17:41 | [diff] [blame] | 869 | return GetExtensionById(host, false); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 870 | } |
| 871 | |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 872 | void ExtensionsService::ClearProvidersForTesting() { |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 873 | ChromeThread::PostTask( |
| 874 | ChromeThread::FILE, FROM_HERE, |
| 875 | NewRunnableMethod( |
| 876 | backend_.get(), &ExtensionsServiceBackend::ClearProvidersForTesting)); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | void ExtensionsService::SetProviderForTesting( |
| 880 | Extension::Location location, ExternalExtensionProvider* test_provider) { |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 881 | ChromeThread::PostTask( |
| 882 | ChromeThread::FILE, FROM_HERE, |
| 883 | NewRunnableMethod( |
| 884 | backend_.get(), &ExtensionsServiceBackend::SetProviderForTesting, |
| 885 | location, test_provider)); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 886 | } |
| 887 | |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 888 | void ExtensionsService::OnExternalExtensionFound(const std::string& id, |
| 889 | const std::string& version, |
| 890 | const FilePath& path, |
| 891 | Extension::Location location) { |
| 892 | // Before even bothering to unpack, check and see if we already have this |
[email protected] | 4c96793 | 2009-07-31 01:15:49 | [diff] [blame] | 893 | // version. This is important because these extensions are going to get |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 894 | // installed on every startup. |
[email protected] | 61b41161 | 2009-11-10 23:17:41 | [diff] [blame] | 895 | Extension* existing = GetExtensionById(id, true); |
[email protected] | a3a63ff8 | 2009-08-04 06:44:11 | [diff] [blame] | 896 | scoped_ptr<Version> other(Version::GetVersionFromString(version)); |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 897 | if (existing) { |
[email protected] | a3a63ff8 | 2009-08-04 06:44:11 | [diff] [blame] | 898 | switch (existing->version()->CompareTo(*other)) { |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 899 | case -1: // existing version is older, we should upgrade |
| 900 | break; |
| 901 | case 0: // existing version is same, do nothing |
| 902 | return; |
| 903 | case 1: // existing version is newer, uh-oh |
| 904 | LOG(WARNING) << "Found external version of extension " << id |
| 905 | << "that is older than current version. Current version " |
| 906 | << "is: " << existing->VersionString() << ". New version " |
| 907 | << "is: " << version << ". Keeping current version."; |
| 908 | return; |
| 909 | } |
| 910 | } |
| 911 | |
[email protected] | 6dfbbf8 | 2010-03-12 23:09:16 | [diff] [blame] | 912 | scoped_refptr<CrxInstaller> installer( |
| 913 | new CrxInstaller(install_directory_, |
| 914 | this, // frontend |
| 915 | NULL)); // no client (silent install) |
| 916 | installer->set_install_source(location); |
| 917 | installer->set_expected_id(id); |
| 918 | installer->set_allow_privilege_increase(true); |
| 919 | installer->InstallCrx(path); |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 920 | } |
| 921 | |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 922 | void ExtensionsService::ReportExtensionLoadError( |
| 923 | const FilePath& extension_path, |
| 924 | const std::string &error, |
| 925 | NotificationType type, |
| 926 | bool be_noisy) { |
| 927 | NotificationService* service = NotificationService::current(); |
| 928 | service->Notify(type, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 929 | Source<Profile>(profile_), |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 930 | Details<const std::string>(&error)); |
| 931 | |
| 932 | // TODO(port): note that this isn't guaranteed to work properly on Linux. |
[email protected] | 99efb7b1 | 2009-12-18 02:39:16 | [diff] [blame] | 933 | std::string path_str = WideToUTF8(extension_path.ToWStringHack()); |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 934 | std::string message = StringPrintf("Could not load extension from '%s'. %s", |
| 935 | path_str.c_str(), error.c_str()); |
| 936 | ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy); |
| 937 | } |
| 938 | |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 939 | void ExtensionsService::Observe(NotificationType type, |
| 940 | const NotificationSource& source, |
| 941 | const NotificationDetails& details) { |
| 942 | switch (type.value) { |
| 943 | case NotificationType::EXTENSION_HOST_DID_STOP_LOADING: { |
| 944 | ExtensionHost* host = Details<ExtensionHost>(details).ptr(); |
| 945 | OrphanedDevTools::iterator iter = |
| 946 | orphaned_dev_tools_.find(host->extension()->id()); |
| 947 | if (iter == orphaned_dev_tools_.end()) |
| 948 | return; |
| 949 | |
| 950 | DevToolsManager::GetInstance()->AttachClientHost( |
| 951 | iter->second, host->render_view_host()); |
| 952 | orphaned_dev_tools_.erase(iter); |
| 953 | break; |
| 954 | } |
| 955 | |
[email protected] | a4ed628 | 2009-12-14 20:51:16 | [diff] [blame] | 956 | case NotificationType::EXTENSION_PROCESS_TERMINATED: { |
[email protected] | 31f7726 | 2009-12-02 20:48:53 | [diff] [blame] | 957 | DCHECK_EQ(profile_, Source<Profile>(source).ptr()); |
[email protected] | a4ed628 | 2009-12-14 20:51:16 | [diff] [blame] | 958 | |
[email protected] | 31f7726 | 2009-12-02 20:48:53 | [diff] [blame] | 959 | ExtensionHost* host = Details<ExtensionHost>(details).ptr(); |
[email protected] | 27e469a | 2010-01-11 20:35:09 | [diff] [blame] | 960 | // TODO(phajdan.jr): Change to DCHECK after fixing http://crbug.com/30405. |
| 961 | CHECK(profile_->GetExtensionProcessManager()->HasExtensionHost(host)); |
| 962 | |
| 963 | // If we hit one of these assertions it means that the host's |
[email protected] | b0b567bf | 2010-01-22 19:22:53 | [diff] [blame] | 964 | // Extension pointer became invalid. http://crbug.com/30405 |
[email protected] | 27e469a | 2010-01-11 20:35:09 | [diff] [blame] | 965 | // TODO(phajdan.jr): Remove excessive debugging after fixing bug 30405. |
| 966 | std::string extension_id(host->extension()->id()); |
| 967 | CHECK(extension_id.length() == 32U); |
| 968 | Extension* extension = GetExtensionById(extension_id, true); |
[email protected] | b0b567bf | 2010-01-22 19:22:53 | [diff] [blame] | 969 | CHECK(extension == host->extension()); |
[email protected] | 6f39aee9 | 2010-01-26 22:33:58 | [diff] [blame] | 970 | if (!extension) { |
| 971 | NOTREACHED(); |
| 972 | return; |
| 973 | } |
[email protected] | 31f7726 | 2009-12-02 20:48:53 | [diff] [blame] | 974 | |
| 975 | // Unload the entire extension. We want it to be in a consistent state: |
| 976 | // either fully working or not loaded at all, but never half-crashed. |
[email protected] | 27e469a | 2010-01-11 20:35:09 | [diff] [blame] | 977 | UnloadExtension(extension_id); |
[email protected] | 31f7726 | 2009-12-02 20:48:53 | [diff] [blame] | 978 | break; |
| 979 | } |
| 980 | |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 981 | default: |
| 982 | NOTREACHED() << "Unexpected notification type."; |
| 983 | } |
| 984 | } |
| 985 | |
| 986 | |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 987 | // ExtensionsServicesBackend |
| 988 | |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 989 | ExtensionsServiceBackend::ExtensionsServiceBackend( |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 990 | const FilePath& install_directory) |
[email protected] | 0c7bc4b | 2009-05-30 01:47:08 | [diff] [blame] | 991 | : frontend_(NULL), |
| 992 | install_directory_(install_directory), |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 993 | alert_on_error_(false) { |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 994 | // TODO(aa): This ends up doing blocking IO on the UI thread because it reads |
| 995 | // pref data in the ctor and that is called on the UI thread. Would be better |
| 996 | // to re-read data each time we list external extensions, anyway. |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 997 | external_extension_providers_[Extension::EXTERNAL_PREF] = |
[email protected] | da50530a | 2009-06-15 17:43:01 | [diff] [blame] | 998 | linked_ptr<ExternalExtensionProvider>( |
[email protected] | 27b985d | 2009-06-25 17:53:15 | [diff] [blame] | 999 | new ExternalPrefExtensionProvider()); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 1000 | #if defined(OS_WIN) |
| 1001 | external_extension_providers_[Extension::EXTERNAL_REGISTRY] = |
[email protected] | da50530a | 2009-06-15 17:43:01 | [diff] [blame] | 1002 | linked_ptr<ExternalExtensionProvider>( |
| 1003 | new ExternalRegistryExtensionProvider()); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 1004 | #endif |
| 1005 | } |
| 1006 | |
| 1007 | ExtensionsServiceBackend::~ExtensionsServiceBackend() { |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 1008 | } |
| 1009 | |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 1010 | void ExtensionsServiceBackend::LoadSingleExtension( |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 1011 | const FilePath& path_in, scoped_refptr<ExtensionsService> frontend) { |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 1012 | frontend_ = frontend; |
| 1013 | |
| 1014 | // Explicit UI loads are always noisy. |
| 1015 | alert_on_error_ = true; |
| 1016 | |
[email protected] | cc5da33 | 2009-03-04 08:02:51 | [diff] [blame] | 1017 | FilePath extension_path = path_in; |
[email protected] | f36fa4fb | 2009-06-19 18:23:50 | [diff] [blame] | 1018 | file_util::AbsolutePath(&extension_path); |
[email protected] | bf24d2c | 2009-02-24 23:07:45 | [diff] [blame] | 1019 | |
| 1020 | LOG(INFO) << "Loading single extension from " << |
[email protected] | 99efb7b1 | 2009-12-18 02:39:16 | [diff] [blame] | 1021 | extension_path.BaseName().value(); |
[email protected] | bf24d2c | 2009-02-24 23:07:45 | [diff] [blame] | 1022 | |
[email protected] | ab6f2b2 | 2009-07-28 23:28:37 | [diff] [blame] | 1023 | std::string error; |
| 1024 | Extension* extension = extension_file_util::LoadExtension( |
| 1025 | extension_path, |
| 1026 | false, // Don't require id |
| 1027 | &error); |
| 1028 | |
| 1029 | if (!extension) { |
| 1030 | ReportExtensionLoadError(extension_path, error); |
| 1031 | return; |
[email protected] | 0877fd9 | 2009-02-03 16:34:06 | [diff] [blame] | 1032 | } |
[email protected] | ab6f2b2 | 2009-07-28 23:28:37 | [diff] [blame] | 1033 | |
| 1034 | extension->set_location(Extension::LOAD); |
[email protected] | e8c729a | 2010-03-09 19:55:19 | [diff] [blame] | 1035 | |
| 1036 | // Report this as an installed extension so that it gets remembered in the |
| 1037 | // prefs. |
| 1038 | ChromeThread::PostTask( |
| 1039 | ChromeThread::UI, FROM_HERE, |
| 1040 | NewRunnableMethod(frontend_, &ExtensionsService::OnExtensionInstalled, |
| 1041 | extension, true)); |
[email protected] | 0877fd9 | 2009-02-03 16:34:06 | [diff] [blame] | 1042 | } |
| 1043 | |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 1044 | void ExtensionsServiceBackend::ReportExtensionLoadError( |
[email protected] | cc5da33 | 2009-03-04 08:02:51 | [diff] [blame] | 1045 | const FilePath& extension_path, const std::string &error) { |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 1046 | ChromeThread::PostTask( |
| 1047 | ChromeThread::UI, FROM_HERE, |
| 1048 | NewRunnableMethod( |
| 1049 | frontend_, |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 1050 | &ExtensionsService::ReportExtensionLoadError, extension_path, |
| 1051 | error, NotificationType::EXTENSION_INSTALL_ERROR, alert_on_error_)); |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 1052 | } |
| 1053 | |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 1054 | bool ExtensionsServiceBackend::LookupExternalExtension( |
| 1055 | const std::string& id, Version** version, Extension::Location* location) { |
| 1056 | scoped_ptr<Version> extension_version; |
| 1057 | for (ProviderMap::const_iterator i = external_extension_providers_.begin(); |
| 1058 | i != external_extension_providers_.end(); ++i) { |
[email protected] | da50530a | 2009-06-15 17:43:01 | [diff] [blame] | 1059 | const ExternalExtensionProvider* provider = i->second.get(); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 1060 | extension_version.reset(provider->RegisteredVersion(id, location)); |
| 1061 | if (extension_version.get()) { |
| 1062 | if (version) |
| 1063 | *version = extension_version.release(); |
| 1064 | return true; |
| 1065 | } |
| 1066 | } |
| 1067 | return false; |
| 1068 | } |
| 1069 | |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 1070 | // Some extensions will autoupdate themselves externally from Chrome. These |
| 1071 | // are typically part of some larger client application package. To support |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 1072 | // these, the extension will register its location in the the preferences file |
| 1073 | // (and also, on Windows, in the registry) and this code will periodically |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 1074 | // check that location for a .crx file, which it will then install locally if |
| 1075 | // a new version is available. |
| 1076 | void ExtensionsServiceBackend::CheckForExternalUpdates( |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 1077 | std::set<std::string> ids_to_ignore, |
| 1078 | scoped_refptr<ExtensionsService> frontend) { |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 1079 | // Note that this installation is intentionally silent (since it didn't |
| 1080 | // go through the front-end). Extensions that are registered in this |
| 1081 | // way are effectively considered 'pre-bundled', and so implicitly |
| 1082 | // trusted. In general, if something has HKLM or filesystem access, |
| 1083 | // they could install an extension manually themselves anyway. |
| 1084 | alert_on_error_ = false; |
| 1085 | frontend_ = frontend; |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 1086 | |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 1087 | // Ask each external extension provider to give us a call back for each |
| 1088 | // extension they know about. See OnExternalExtensionFound. |
| 1089 | for (ProviderMap::const_iterator i = external_extension_providers_.begin(); |
| 1090 | i != external_extension_providers_.end(); ++i) { |
[email protected] | da50530a | 2009-06-15 17:43:01 | [diff] [blame] | 1091 | ExternalExtensionProvider* provider = i->second.get(); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 1092 | provider->VisitRegisteredExtension(this, ids_to_ignore); |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 1093 | } |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 1094 | } |
| 1095 | |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 1096 | void ExtensionsServiceBackend::CheckExternalUninstall( |
| 1097 | scoped_refptr<ExtensionsService> frontend, const std::string& id, |
| 1098 | Extension::Location location) { |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 1099 | // Check if the providers know about this extension. |
| 1100 | ProviderMap::const_iterator i = external_extension_providers_.find(location); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 1101 | if (i == external_extension_providers_.end()) { |
| 1102 | NOTREACHED() << "CheckExternalUninstall called for non-external extension " |
| 1103 | << location; |
| 1104 | return; |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 1105 | } |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 1106 | |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 1107 | scoped_ptr<Version> version; |
| 1108 | version.reset(i->second->RegisteredVersion(id, NULL)); |
| 1109 | if (version.get()) |
| 1110 | return; // Yup, known extension, don't uninstall. |
| 1111 | |
| 1112 | // This is an external extension that we don't have registered. Uninstall. |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 1113 | ChromeThread::PostTask( |
| 1114 | ChromeThread::UI, FROM_HERE, |
| 1115 | NewRunnableMethod( |
| 1116 | frontend.get(), &ExtensionsService::UninstallExtension, id, true)); |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 1117 | } |
| 1118 | |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 1119 | void ExtensionsServiceBackend::ClearProvidersForTesting() { |
| 1120 | external_extension_providers_.clear(); |
| 1121 | } |
| 1122 | |
| 1123 | void ExtensionsServiceBackend::SetProviderForTesting( |
| 1124 | Extension::Location location, |
| 1125 | ExternalExtensionProvider* test_provider) { |
| 1126 | DCHECK(test_provider); |
[email protected] | da50530a | 2009-06-15 17:43:01 | [diff] [blame] | 1127 | external_extension_providers_[location] = |
| 1128 | linked_ptr<ExternalExtensionProvider>(test_provider); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | void ExtensionsServiceBackend::OnExternalExtensionFound( |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 1132 | const std::string& id, const Version* version, const FilePath& path, |
| 1133 | Extension::Location location) { |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 1134 | ChromeThread::PostTask( |
| 1135 | ChromeThread::UI, FROM_HERE, |
| 1136 | NewRunnableMethod( |
| 1137 | frontend_, &ExtensionsService::OnExternalExtensionFound, id, |
| 1138 | version->GetString(), path, location)); |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 1139 | } |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 1140 | |
[email protected] | 2111b1a | 2010-03-12 18:12:44 | [diff] [blame] | 1141 | void ExtensionsServiceBackend::ReloadExtensionManifests( |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 1142 | ExtensionPrefs::ExtensionsInfo* extensions_to_reload, |
| 1143 | base::TimeTicks start_time, |
| 1144 | scoped_refptr<ExtensionsService> frontend) { |
| 1145 | frontend_ = frontend; |
| 1146 | |
| 1147 | for (size_t i = 0; i < extensions_to_reload->size(); ++i) { |
| 1148 | ExtensionInfo* info = extensions_to_reload->at(i).get(); |
[email protected] | 2111b1a | 2010-03-12 18:12:44 | [diff] [blame] | 1149 | if (!ShouldReloadExtensionManifest(*info)) |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 1150 | continue; |
| 1151 | |
| 1152 | // We need to reload original manifest in order to localize properly. |
| 1153 | std::string error; |
| 1154 | scoped_ptr<Extension> extension(extension_file_util::LoadExtension( |
| 1155 | info->extension_path, false, &error)); |
| 1156 | |
| 1157 | if (extension.get()) |
| 1158 | extensions_to_reload->at(i)->extension_manifest.reset( |
| 1159 | static_cast<DictionaryValue*>( |
| 1160 | extension->manifest_value()->DeepCopy())); |
| 1161 | } |
| 1162 | |
| 1163 | // Finish installing on UI thread. |
| 1164 | ChromeThread::PostTask( |
| 1165 | ChromeThread::UI, FROM_HERE, |
| 1166 | NewRunnableMethod( |
| 1167 | frontend_, |
| 1168 | &ExtensionsService::ContinueLoadAllExtensions, |
| 1169 | extensions_to_reload, |
| 1170 | start_time, |
| 1171 | true)); |
| 1172 | } |