[email protected] | ce5c450 | 2009-05-06 16:46:11 | [diff] [blame] | 1 | // Copyright (c) 2009 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] | e2eb4311 | 2009-05-29 21:19:54 | [diff] [blame] | 7 | #include "base/command_line.h" |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 8 | #include "base/file_util.h" |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 9 | #include "base/string_util.h" |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 10 | #include "base/values.h" |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 11 | #include "chrome/browser/extensions/crx_installer.h" |
[email protected] | b68d5ed | 2009-04-16 02:41:28 | [diff] [blame] | 12 | #include "chrome/browser/extensions/extension_browser_event_router.h" |
[email protected] | ab6f2b2 | 2009-07-28 23:28:37 | [diff] [blame] | 13 | #include "chrome/browser/extensions/extension_file_util.h" |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 14 | #include "chrome/browser/extensions/extension_updater.h" |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 15 | #include "chrome/browser/extensions/external_extension_provider.h" |
| 16 | #include "chrome/browser/extensions/external_pref_extension_provider.h" |
[email protected] | 81e6378 | 2009-02-27 19:35:09 | [diff] [blame] | 17 | #include "chrome/browser/profile.h" |
[email protected] | e2eb4311 | 2009-05-29 21:19:54 | [diff] [blame] | 18 | #include "chrome/common/chrome_switches.h" |
[email protected] | 5b1a0e2 | 2009-05-26 19:00:58 | [diff] [blame] | 19 | #include "chrome/common/extensions/extension.h" |
[email protected] | 5b1a0e2 | 2009-05-26 19:00:58 | [diff] [blame] | 20 | #include "chrome/common/extensions/extension_error_reporter.h" |
[email protected] | 8289126 | 2008-12-24 00:21:26 | [diff] [blame] | 21 | #include "chrome/common/notification_service.h" |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 22 | #include "chrome/common/pref_names.h" |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 23 | #include "chrome/common/pref_service.h" |
[email protected] | a5720987 | 2009-05-04 22:53:14 | [diff] [blame] | 24 | #include "chrome/common/url_constants.h" |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 25 | |
[email protected] | 79db623 | 2009-02-13 20:51:20 | [diff] [blame] | 26 | #if defined(OS_WIN) |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 27 | #include "chrome/browser/extensions/external_registry_extension_provider_win.h" |
[email protected] | 79db623 | 2009-02-13 20:51:20 | [diff] [blame] | 28 | #endif |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 29 | |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 30 | // ExtensionsService. |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 31 | |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 32 | const char* ExtensionsService::kInstallDirectoryName = "Extensions"; |
| 33 | const char* ExtensionsService::kCurrentVersionFileName = "Current Version"; |
[email protected] | 494c06e | 2009-07-25 01:06:42 | [diff] [blame] | 34 | |
[email protected] | 494c06e | 2009-07-25 01:06:42 | [diff] [blame] | 35 | const char* ExtensionsService::kGalleryDownloadURLPrefix = |
| 36 | "https://dl-ssl.google.com/chrome/"; |
[email protected] | 75a2567 | 2009-07-24 17:41:39 | [diff] [blame] | 37 | const char* ExtensionsService::kGalleryURLPrefix = |
| 38 | "https://tools.google.com/chrome/"; |
[email protected] | 75a2567 | 2009-07-24 17:41:39 | [diff] [blame] | 39 | |
[email protected] | 4289d9b | 2009-07-25 21:17:34 | [diff] [blame] | 40 | // static |
| 41 | bool ExtensionsService::IsDownloadFromGallery(const GURL& download_url, |
| 42 | const GURL& referrer_url) { |
| 43 | if (StartsWithASCII(download_url.spec(), kGalleryDownloadURLPrefix, false) && |
| 44 | StartsWithASCII(referrer_url.spec(), kGalleryURLPrefix, false)) { |
| 45 | return true; |
| 46 | } else { |
| 47 | return false; |
| 48 | } |
| 49 | } |
| 50 | |
[email protected] | 81e6378 | 2009-02-27 19:35:09 | [diff] [blame] | 51 | ExtensionsService::ExtensionsService(Profile* profile, |
[email protected] | 36a784c | 2009-06-23 06:21:08 | [diff] [blame] | 52 | const CommandLine* command_line, |
[email protected] | a9b00ac | 2009-06-25 21:03:23 | [diff] [blame] | 53 | PrefService* prefs, |
| 54 | const FilePath& install_directory, |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 55 | MessageLoop* frontend_loop, |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 56 | MessageLoop* backend_loop, |
| 57 | bool autoupdate_enabled) |
[email protected] | 6ef635e4 | 2009-07-26 06:16:12 | [diff] [blame] | 58 | : profile_(profile), |
| 59 | extension_prefs_(new ExtensionPrefs(prefs, install_directory)), |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 60 | backend_loop_(backend_loop), |
[email protected] | a9b00ac | 2009-06-25 21:03:23 | [diff] [blame] | 61 | install_directory_(install_directory), |
[email protected] | abe7a894 | 2009-06-23 05:14:29 | [diff] [blame] | 62 | extensions_enabled_(false), |
[email protected] | e81dba3 | 2009-06-19 20:19:13 | [diff] [blame] | 63 | show_extensions_prompts_(true), |
| 64 | ready_(false) { |
[email protected] | 36a784c | 2009-06-23 06:21:08 | [diff] [blame] | 65 | // Figure out if extension installation should be enabled. |
[email protected] | 6b75ec3 | 2009-08-14 06:37:18 | [diff] [blame] | 66 | if (command_line->HasSwitch(switches::kEnableExtensions)) { |
[email protected] | 36a784c | 2009-06-23 06:21:08 | [diff] [blame] | 67 | extensions_enabled_ = true; |
[email protected] | 6b75ec3 | 2009-08-14 06:37:18 | [diff] [blame] | 68 | } else if (profile->GetPrefs()->GetBoolean(prefs::kEnableExtensions)) { |
[email protected] | 36a784c | 2009-06-23 06:21:08 | [diff] [blame] | 69 | extensions_enabled_ = true; |
[email protected] | 6b75ec3 | 2009-08-14 06:37:18 | [diff] [blame] | 70 | } |
[email protected] | 36a784c | 2009-06-23 06:21:08 | [diff] [blame] | 71 | |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 72 | // Set up the ExtensionUpdater |
| 73 | if (autoupdate_enabled) { |
| 74 | int update_frequency = kDefaultUpdateFrequencySeconds; |
| 75 | if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) { |
| 76 | update_frequency = StringToInt(WideToASCII(command_line->GetSwitchValue( |
| 77 | switches::kExtensionsUpdateFrequency))); |
| 78 | } |
[email protected] | 951073e | 2009-08-03 17:54:34 | [diff] [blame] | 79 | updater_ = new ExtensionUpdater(this, prefs, update_frequency, |
| 80 | backend_loop_); |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 81 | } |
| 82 | |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 83 | backend_ = new ExtensionsServiceBackend(install_directory_, frontend_loop); |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | ExtensionsService::~ExtensionsService() { |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 87 | UnloadAllExtensions(); |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 88 | if (updater_.get()) { |
| 89 | updater_->Stop(); |
| 90 | } |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 91 | } |
| 92 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 93 | void ExtensionsService::Init() { |
[email protected] | c6e4a341 | 2009-06-24 15:45:29 | [diff] [blame] | 94 | DCHECK(!ready_); |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 95 | DCHECK_EQ(extensions_.size(), 0u); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 96 | |
[email protected] | b68d5ed | 2009-04-16 02:41:28 | [diff] [blame] | 97 | // Start up the extension event routers. |
| 98 | ExtensionBrowserEventRouter::GetInstance()->Init(); |
| 99 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 100 | LoadAllExtensions(); |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 101 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 102 | // TODO(erikkay) this should probably be deferred to a future point |
| 103 | // rather than running immediately at startup. |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 104 | CheckForExternalUpdates(); |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 105 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 106 | // TODO(erikkay) this should probably be deferred as well. |
| 107 | GarbageCollectExtensions(); |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 108 | } |
| 109 | |
[email protected] | 3cf4f099 | 2009-02-03 23:00:30 | [diff] [blame] | 110 | void ExtensionsService::InstallExtension(const FilePath& extension_path) { |
[email protected] | 2a464a9 | 2009-08-01 17:58:35 | [diff] [blame] | 111 | CrxInstaller::Start(extension_path, install_directory_, Extension::INTERNAL, |
| 112 | "", // no expected id |
| 113 | false, // don't delete crx when complete |
| 114 | backend_loop_, |
| 115 | this, |
| 116 | NULL); // no client (silent install) |
[email protected] | 3cf4f099 | 2009-02-03 23:00:30 | [diff] [blame] | 117 | } |
| 118 | |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 119 | void ExtensionsService::UpdateExtension(const std::string& id, |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 120 | const FilePath& extension_path) { |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame^] | 121 | if (!GetExtensionByIdInternal(id, true, true)) { |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 122 | LOG(WARNING) << "Will not update extension " << id << " because it is not " |
[email protected] | 4c96793 | 2009-07-31 01:15:49 | [diff] [blame] | 123 | << "installed"; |
| 124 | return; |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 125 | } |
| 126 | |
[email protected] | 2a464a9 | 2009-08-01 17:58:35 | [diff] [blame] | 127 | CrxInstaller::Start(extension_path, install_directory_, Extension::INTERNAL, |
| 128 | id, |
| 129 | true, // delete crx when complete |
| 130 | backend_loop_, |
| 131 | this, |
| 132 | NULL); // no client (silent install) |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 133 | } |
| 134 | |
[email protected] | 9cddd470 | 2009-07-27 22:09:40 | [diff] [blame] | 135 | void ExtensionsService::ReloadExtension(const std::string& extension_id) { |
[email protected] | 572479e | 2009-08-11 01:57:37 | [diff] [blame] | 136 | Extension* extension = GetExtensionById(extension_id); |
| 137 | FilePath extension_path = extension->path(); |
[email protected] | 9cddd470 | 2009-07-27 22:09:40 | [diff] [blame] | 138 | |
[email protected] | 572479e | 2009-08-11 01:57:37 | [diff] [blame] | 139 | UnloadExtension(extension_id); |
| 140 | LoadExtension(extension_path); |
[email protected] | 9cddd470 | 2009-07-27 22:09:40 | [diff] [blame] | 141 | } |
| 142 | |
[email protected] | 27b985d | 2009-06-25 17:53:15 | [diff] [blame] | 143 | void ExtensionsService::UninstallExtension(const std::string& extension_id, |
| 144 | bool external_uninstall) { |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame^] | 145 | Extension* extension = GetExtensionByIdInternal(extension_id, true, true); |
[email protected] | 631cf82 | 2009-05-15 07:01:25 | [diff] [blame] | 146 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 147 | // Callers should not send us nonexistant extensions. |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 148 | DCHECK(extension); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 149 | |
[email protected] | 27b985d | 2009-06-25 17:53:15 | [diff] [blame] | 150 | extension_prefs_->OnExtensionUninstalled(extension, external_uninstall); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 151 | |
| 152 | // Tell the backend to start deleting installed extensions on the file thread. |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 153 | if (Extension::LOAD != extension->location()) { |
[email protected] | ab6f2b2 | 2009-07-28 23:28:37 | [diff] [blame] | 154 | backend_loop_->PostTask(FROM_HERE, NewRunnableFunction( |
| 155 | &extension_file_util::UninstallExtension, extension_id, |
| 156 | install_directory_)); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | UnloadExtension(extension_id); |
| 160 | } |
| 161 | |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame^] | 162 | void ExtensionsService::EnableExtension(const std::string& extension_id) { |
| 163 | Extension* extension = GetExtensionByIdInternal(extension_id, false, true); |
| 164 | if (!extension) { |
| 165 | NOTREACHED() << "Trying to enable an extension that isn't disabled."; |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | // Move it over to the enabled list. |
| 170 | extension_prefs_->SetExtensionState(extension, Extension::ENABLED); |
| 171 | extensions_.push_back(extension); |
| 172 | ExtensionList::iterator iter = std::find(disabled_extensions_.begin(), |
| 173 | disabled_extensions_.end(), |
| 174 | extension); |
| 175 | disabled_extensions_.erase(iter); |
| 176 | |
| 177 | ExtensionList extensions; |
| 178 | extensions.push_back(extension); |
| 179 | NotificationService::current()->Notify( |
| 180 | NotificationType::EXTENSIONS_LOADED, |
| 181 | Source<ExtensionsService>(this), |
| 182 | Details<ExtensionList>(&extensions)); |
| 183 | } |
| 184 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 185 | void ExtensionsService::LoadExtension(const FilePath& extension_path) { |
| 186 | backend_loop_->PostTask(FROM_HERE, NewRunnableMethod(backend_.get(), |
| 187 | &ExtensionsServiceBackend::LoadSingleExtension, |
| 188 | extension_path, scoped_refptr<ExtensionsService>(this))); |
| 189 | } |
| 190 | |
| 191 | void ExtensionsService::LoadAllExtensions() { |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 192 | // Load the previously installed extensions. |
| 193 | backend_loop_->PostTask(FROM_HERE, NewRunnableMethod(backend_.get(), |
| 194 | &ExtensionsServiceBackend::LoadInstalledExtensions, |
| 195 | scoped_refptr<ExtensionsService>(this), |
| 196 | new InstalledExtensions(extension_prefs_.get()))); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 197 | } |
| 198 | |
[email protected] | 6b75ec3 | 2009-08-14 06:37:18 | [diff] [blame] | 199 | void ExtensionsService::UpdateExtensionBlacklist( |
| 200 | const std::vector<std::string>& blacklist) { |
| 201 | // Use this set to indicate if an extension in the blacklist has been used. |
| 202 | std::set<std::string> blacklist_set; |
| 203 | for (unsigned int i = 0; i < blacklist.size(); ++i) { |
| 204 | if (Extension::IdIsValid(blacklist[i])) { |
| 205 | blacklist_set.insert(blacklist[i]); |
| 206 | } |
| 207 | } |
| 208 | extension_prefs_->UpdateBlacklist(blacklist_set); |
| 209 | std::vector<std::string> to_be_removed; |
| 210 | // Loop current extensions, unload installed extensions. |
| 211 | for (ExtensionList::const_iterator iter = extensions_.begin(); |
| 212 | iter != extensions_.end(); ++iter) { |
| 213 | Extension* extension = (*iter); |
| 214 | if (blacklist_set.find(extension->id()) != blacklist_set.end()) { |
| 215 | to_be_removed.push_back(extension->id()); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | // UnloadExtension will change the extensions_ list. So, we should |
| 220 | // call it outside the iterator loop. |
| 221 | for (unsigned int i = 0; i < to_be_removed.size(); ++i) { |
| 222 | UnloadExtension(to_be_removed[i]); |
| 223 | } |
| 224 | } |
| 225 | |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 226 | void ExtensionsService::CheckForExternalUpdates() { |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 227 | // This installs or updates externally provided extensions. |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 228 | // TODO(aa): Why pass this list into the provider, why not just filter it |
| 229 | // later? |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 230 | std::set<std::string> killed_extensions; |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 231 | extension_prefs_->GetKilledExtensionIds(&killed_extensions); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 232 | backend_loop_->PostTask(FROM_HERE, NewRunnableMethod(backend_.get(), |
| 233 | &ExtensionsServiceBackend::CheckForExternalUpdates, |
| 234 | killed_extensions, |
| 235 | scoped_refptr<ExtensionsService>(this))); |
| 236 | } |
| 237 | |
| 238 | void ExtensionsService::UnloadExtension(const std::string& extension_id) { |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame^] | 239 | scoped_ptr<Extension> extension( |
| 240 | GetExtensionByIdInternal(extension_id, true, true)); |
[email protected] | 631cf82 | 2009-05-15 07:01:25 | [diff] [blame] | 241 | |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 242 | // Callers should not send us nonexistant extensions. |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame^] | 243 | CHECK(extension.get()); |
| 244 | |
| 245 | ExtensionList::iterator iter = std::find(disabled_extensions_.begin(), |
| 246 | disabled_extensions_.end(), |
| 247 | extension.get()); |
| 248 | if (iter != disabled_extensions_.end()) { |
| 249 | // It's disabled, so don't send the unload notification. |
| 250 | disabled_extensions_.erase(iter); |
| 251 | return; |
| 252 | } |
| 253 | |
| 254 | iter = std::find(extensions_.begin(), extensions_.end(), extension.get()); |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 255 | |
[email protected] | 631cf82 | 2009-05-15 07:01:25 | [diff] [blame] | 256 | // Remove the extension from our list. |
| 257 | extensions_.erase(iter); |
| 258 | |
[email protected] | 631cf82 | 2009-05-15 07:01:25 | [diff] [blame] | 259 | // Tell other services the extension is gone. |
| 260 | NotificationService::current()->Notify(NotificationType::EXTENSION_UNLOADED, |
[email protected] | c6e4a341 | 2009-06-24 15:45:29 | [diff] [blame] | 261 | Source<ExtensionsService>(this), |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame^] | 262 | Details<Extension>(extension.get())); |
[email protected] | 631cf82 | 2009-05-15 07:01:25 | [diff] [blame] | 263 | } |
| 264 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 265 | void ExtensionsService::UnloadAllExtensions() { |
| 266 | ExtensionList::iterator iter; |
[email protected] | c6e4a341 | 2009-06-24 15:45:29 | [diff] [blame] | 267 | for (iter = extensions_.begin(); iter != extensions_.end(); ++iter) |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 268 | delete *iter; |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 269 | extensions_.clear(); |
[email protected] | c6e4a341 | 2009-06-24 15:45:29 | [diff] [blame] | 270 | |
| 271 | // TODO(erikkay) should there be a notification for this? We can't use |
| 272 | // EXTENSION_UNLOADED since that implies that the extension has been disabled |
| 273 | // or uninstalled, and UnloadAll is just part of shutdown. |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | void ExtensionsService::ReloadExtensions() { |
| 277 | UnloadAllExtensions(); |
| 278 | LoadAllExtensions(); |
| 279 | } |
| 280 | |
| 281 | void ExtensionsService::GarbageCollectExtensions() { |
[email protected] | ab6f2b2 | 2009-07-28 23:28:37 | [diff] [blame] | 282 | backend_loop_->PostTask(FROM_HERE, NewRunnableFunction( |
| 283 | &extension_file_util::GarbageCollectExtensions, install_directory_)); |
[email protected] | 3cf4f099 | 2009-02-03 23:00:30 | [diff] [blame] | 284 | } |
| 285 | |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 286 | void ExtensionsService::OnLoadedInstalledExtensions() { |
[email protected] | e81dba3 | 2009-06-19 20:19:13 | [diff] [blame] | 287 | ready_ = true; |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 288 | if (updater_.get()) { |
| 289 | updater_->Start(); |
| 290 | } |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 291 | NotificationService::current()->Notify( |
| 292 | NotificationType::EXTENSIONS_READY, |
| 293 | Source<ExtensionsService>(this), |
| 294 | NotificationService::NoDetails()); |
| 295 | } |
| 296 | |
[email protected] | 4a8d327 | 2009-03-10 19:15:08 | [diff] [blame] | 297 | void ExtensionsService::OnExtensionsLoaded(ExtensionList* new_extensions) { |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 298 | scoped_ptr<ExtensionList> cleanup(new_extensions); |
| 299 | |
[email protected] | 75a2567 | 2009-07-24 17:41:39 | [diff] [blame] | 300 | // Filter out any extensions that shouldn't be loaded. If extensions are |
| 301 | // enabled, load everything. Otherwise load only: |
| 302 | // - themes |
| 303 | // - --load-extension |
| 304 | // - externally installed extensions |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 305 | ExtensionList enabled_extensions; |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame^] | 306 | ExtensionList disabled_extensions; |
[email protected] | 86a27407 | 2009-06-11 02:06:45 | [diff] [blame] | 307 | for (ExtensionList::iterator iter = new_extensions->begin(); |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 308 | iter != new_extensions->end(); ++iter) { |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame^] | 309 | // Extensions that get enabled get added to extensions_ and deleted later. |
| 310 | // Anything skipped must be deleted now so we don't leak. |
| 311 | scoped_ptr<Extension> extension(*iter); |
[email protected] | 75a2567 | 2009-07-24 17:41:39 | [diff] [blame] | 312 | if (extensions_enabled() || |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame^] | 313 | extension->IsTheme() || |
| 314 | extension->location() == Extension::LOAD || |
| 315 | Extension::IsExternalLocation(extension->location())) { |
| 316 | Extension* old = GetExtensionById(extension->id()); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 317 | if (old) { |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame^] | 318 | if (extension->version()->CompareTo(*(old->version())) > 0) { |
| 319 | bool higher_permissions = |
| 320 | (extension->GetPermissionClass() > old->GetPermissionClass()); |
| 321 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 322 | // To upgrade an extension in place, unload the old one and |
| 323 | // then load the new one. |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 324 | UnloadExtension(old->id()); |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame^] | 325 | old = NULL; |
| 326 | |
| 327 | if (higher_permissions) { |
| 328 | // Extension was upgraded to a high permission class. Disable it and |
| 329 | // notify the user. |
| 330 | extension_prefs_->SetExtensionState(extension.get(), |
| 331 | Extension::DISABLED); |
| 332 | NotificationService::current()->Notify( |
| 333 | NotificationType::EXTENSION_UPDATE_DISABLED, |
| 334 | Source<ExtensionsService>(this), |
| 335 | Details<Extension>(extension.get())); |
| 336 | } |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 337 | } else { |
| 338 | // We already have the extension of the same or older version. |
| 339 | LOG(WARNING) << "Duplicate extension load attempt: " << (*iter)->id(); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 340 | continue; |
| 341 | } |
| 342 | } |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame^] | 343 | |
| 344 | switch (extension_prefs_->GetExtensionState(extension->id())) { |
| 345 | case Extension::ENABLED: |
| 346 | enabled_extensions.push_back(extension.get()); |
| 347 | extensions_.push_back(extension.release()); |
| 348 | break; |
| 349 | case Extension::DISABLED: |
| 350 | disabled_extensions.push_back(extension.get()); |
| 351 | disabled_extensions_.push_back(extension.release()); |
| 352 | break; |
| 353 | default: |
| 354 | break; |
| 355 | } |
[email protected] | ba74f35 | 2009-06-11 18:54:45 | [diff] [blame] | 356 | } |
[email protected] | 86a27407 | 2009-06-11 02:06:45 | [diff] [blame] | 357 | } |
| 358 | |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 359 | if (enabled_extensions.size()) { |
| 360 | NotificationService::current()->Notify( |
| 361 | NotificationType::EXTENSIONS_LOADED, |
[email protected] | c6e4a341 | 2009-06-24 15:45:29 | [diff] [blame] | 362 | Source<ExtensionsService>(this), |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 363 | Details<ExtensionList>(&enabled_extensions)); |
[email protected] | 811f343 | 2009-07-25 19:38:21 | [diff] [blame] | 364 | for (ExtensionList::iterator iter = enabled_extensions.begin(); |
| 365 | iter != enabled_extensions.end(); ++iter) { |
| 366 | if ((*iter)->IsTheme() && (*iter)->location() == Extension::LOAD) { |
| 367 | NotificationService::current()->Notify( |
| 368 | NotificationType::THEME_INSTALLED, |
| 369 | Source<ExtensionsService>(this), |
| 370 | Details<Extension>(*iter)); |
| 371 | } |
| 372 | } |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 373 | } |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 374 | } |
| 375 | |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 376 | void ExtensionsService::OnExtensionInstalled(Extension* extension) { |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 377 | extension_prefs_->OnExtensionInstalled(extension); |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 378 | |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 379 | // If the extension is a theme, tell the profile (and therefore ThemeProvider) |
| 380 | // to apply it. |
| 381 | if (extension->IsTheme()) { |
[email protected] | 9ceb0734 | 2009-07-26 04:09:23 | [diff] [blame] | 382 | NotificationService::current()->Notify( |
| 383 | NotificationType::THEME_INSTALLED, |
| 384 | Source<ExtensionsService>(this), |
| 385 | Details<Extension>(extension)); |
[email protected] | 9197f3b | 2009-06-02 00:49:27 | [diff] [blame] | 386 | } else { |
| 387 | NotificationService::current()->Notify( |
| 388 | NotificationType::EXTENSION_INSTALLED, |
[email protected] | c6e4a341 | 2009-06-24 15:45:29 | [diff] [blame] | 389 | Source<ExtensionsService>(this), |
[email protected] | 9197f3b | 2009-06-02 00:49:27 | [diff] [blame] | 390 | Details<Extension>(extension)); |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 391 | } |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 392 | |
| 393 | // Also load the extension. |
| 394 | ExtensionList* list = new ExtensionList; |
| 395 | list->push_back(extension); |
| 396 | OnExtensionsLoaded(list); |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 397 | } |
| 398 | |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 399 | void ExtensionsService::OnExtensionOverinstallAttempted(const std::string& id) { |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 400 | Extension* extension = GetExtensionById(id); |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 401 | if (extension && extension->IsTheme()) { |
[email protected] | 9ceb0734 | 2009-07-26 04:09:23 | [diff] [blame] | 402 | NotificationService::current()->Notify( |
| 403 | NotificationType::THEME_INSTALLED, |
| 404 | Source<ExtensionsService>(this), |
| 405 | Details<Extension>(extension)); |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 406 | } |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 407 | } |
| 408 | |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame^] | 409 | Extension* ExtensionsService::GetExtensionByIdInternal(const std::string& id, |
| 410 | bool include_enabled, |
| 411 | bool include_disabled) { |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 412 | std::string lowercase_id = StringToLowerASCII(id); |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame^] | 413 | if (include_enabled) { |
| 414 | for (ExtensionList::const_iterator iter = extensions_.begin(); |
| 415 | iter != extensions_.end(); ++iter) { |
| 416 | if ((*iter)->id() == lowercase_id) |
| 417 | return *iter; |
| 418 | } |
| 419 | } |
| 420 | if (include_disabled) { |
| 421 | for (ExtensionList::const_iterator iter = disabled_extensions_.begin(); |
| 422 | iter != disabled_extensions_.end(); ++iter) { |
| 423 | if ((*iter)->id() == lowercase_id) |
| 424 | return *iter; |
| 425 | } |
[email protected] | ce5c450 | 2009-05-06 16:46:11 | [diff] [blame] | 426 | } |
| 427 | return NULL; |
| 428 | } |
| 429 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 430 | Extension* ExtensionsService::GetExtensionByURL(const GURL& url) { |
| 431 | std::string host = url.host(); |
| 432 | return GetExtensionById(host); |
| 433 | } |
| 434 | |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 435 | void ExtensionsService::ClearProvidersForTesting() { |
| 436 | backend_loop_->PostTask(FROM_HERE, NewRunnableMethod(backend_.get(), |
| 437 | &ExtensionsServiceBackend::ClearProvidersForTesting)); |
| 438 | } |
| 439 | |
| 440 | void ExtensionsService::SetProviderForTesting( |
| 441 | Extension::Location location, ExternalExtensionProvider* test_provider) { |
| 442 | backend_loop_->PostTask(FROM_HERE, NewRunnableMethod(backend_.get(), |
| 443 | &ExtensionsServiceBackend::SetProviderForTesting, |
| 444 | location, test_provider)); |
| 445 | } |
| 446 | |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 447 | void ExtensionsService::OnExternalExtensionFound(const std::string& id, |
| 448 | const std::string& version, |
| 449 | const FilePath& path, |
| 450 | Extension::Location location) { |
| 451 | // Before even bothering to unpack, check and see if we already have this |
[email protected] | 4c96793 | 2009-07-31 01:15:49 | [diff] [blame] | 452 | // version. This is important because these extensions are going to get |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 453 | // installed on every startup. |
| 454 | Extension* existing = GetExtensionById(id); |
[email protected] | a3a63ff8 | 2009-08-04 06:44:11 | [diff] [blame] | 455 | scoped_ptr<Version> other(Version::GetVersionFromString(version)); |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 456 | if (existing) { |
[email protected] | a3a63ff8 | 2009-08-04 06:44:11 | [diff] [blame] | 457 | switch (existing->version()->CompareTo(*other)) { |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 458 | case -1: // existing version is older, we should upgrade |
| 459 | break; |
| 460 | case 0: // existing version is same, do nothing |
| 461 | return; |
| 462 | case 1: // existing version is newer, uh-oh |
| 463 | LOG(WARNING) << "Found external version of extension " << id |
| 464 | << "that is older than current version. Current version " |
| 465 | << "is: " << existing->VersionString() << ". New version " |
| 466 | << "is: " << version << ". Keeping current version."; |
| 467 | return; |
| 468 | } |
| 469 | } |
| 470 | |
[email protected] | 2a464a9 | 2009-08-01 17:58:35 | [diff] [blame] | 471 | CrxInstaller::Start(path, install_directory_, location, id, |
| 472 | false, // don't delete crx when complete |
| 473 | backend_loop_, |
| 474 | this, |
| 475 | NULL); // no client (silent install) |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 476 | } |
| 477 | |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 478 | |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 479 | // ExtensionsServicesBackend |
| 480 | |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 481 | ExtensionsServiceBackend::ExtensionsServiceBackend( |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 482 | const FilePath& install_directory, MessageLoop* frontend_loop) |
[email protected] | 0c7bc4b | 2009-05-30 01:47:08 | [diff] [blame] | 483 | : frontend_(NULL), |
| 484 | install_directory_(install_directory), |
[email protected] | 0c7bc4b | 2009-05-30 01:47:08 | [diff] [blame] | 485 | alert_on_error_(false), |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 486 | frontend_loop_(frontend_loop) { |
| 487 | // TODO(aa): This ends up doing blocking IO on the UI thread because it reads |
| 488 | // pref data in the ctor and that is called on the UI thread. Would be better |
| 489 | // to re-read data each time we list external extensions, anyway. |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 490 | external_extension_providers_[Extension::EXTERNAL_PREF] = |
[email protected] | da50530a | 2009-06-15 17:43:01 | [diff] [blame] | 491 | linked_ptr<ExternalExtensionProvider>( |
[email protected] | 27b985d | 2009-06-25 17:53:15 | [diff] [blame] | 492 | new ExternalPrefExtensionProvider()); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 493 | #if defined(OS_WIN) |
| 494 | external_extension_providers_[Extension::EXTERNAL_REGISTRY] = |
[email protected] | da50530a | 2009-06-15 17:43:01 | [diff] [blame] | 495 | linked_ptr<ExternalExtensionProvider>( |
| 496 | new ExternalRegistryExtensionProvider()); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 497 | #endif |
| 498 | } |
| 499 | |
| 500 | ExtensionsServiceBackend::~ExtensionsServiceBackend() { |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 501 | } |
| 502 | |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 503 | void ExtensionsServiceBackend::LoadInstalledExtensions( |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 504 | scoped_refptr<ExtensionsService> frontend, |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 505 | InstalledExtensions* installed) { |
| 506 | scoped_ptr<InstalledExtensions> cleanup(installed); |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 507 | frontend_ = frontend; |
| 508 | alert_on_error_ = false; |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 509 | |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 510 | // Call LoadInstalledExtension for each extension |installed| knows about. |
| 511 | scoped_ptr<InstalledExtensions::Callback> callback( |
| 512 | NewCallback(this, &ExtensionsServiceBackend::LoadInstalledExtension)); |
| 513 | installed->VisitInstalledExtensions(callback.get()); |
| 514 | |
| 515 | frontend_loop_->PostTask(FROM_HERE, NewRunnableMethod( |
| 516 | frontend_, &ExtensionsService::OnLoadedInstalledExtensions)); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 517 | } |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 518 | |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 519 | void ExtensionsServiceBackend::LoadSingleExtension( |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 520 | const FilePath& path_in, scoped_refptr<ExtensionsService> frontend) { |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 521 | frontend_ = frontend; |
| 522 | |
| 523 | // Explicit UI loads are always noisy. |
| 524 | alert_on_error_ = true; |
| 525 | |
[email protected] | cc5da33 | 2009-03-04 08:02:51 | [diff] [blame] | 526 | FilePath extension_path = path_in; |
[email protected] | f36fa4fb | 2009-06-19 18:23:50 | [diff] [blame] | 527 | file_util::AbsolutePath(&extension_path); |
[email protected] | bf24d2c | 2009-02-24 23:07:45 | [diff] [blame] | 528 | |
| 529 | LOG(INFO) << "Loading single extension from " << |
[email protected] | cc5da33 | 2009-03-04 08:02:51 | [diff] [blame] | 530 | WideToASCII(extension_path.BaseName().ToWStringHack()); |
[email protected] | bf24d2c | 2009-02-24 23:07:45 | [diff] [blame] | 531 | |
[email protected] | ab6f2b2 | 2009-07-28 23:28:37 | [diff] [blame] | 532 | std::string error; |
| 533 | Extension* extension = extension_file_util::LoadExtension( |
| 534 | extension_path, |
| 535 | false, // Don't require id |
| 536 | &error); |
| 537 | |
| 538 | if (!extension) { |
| 539 | ReportExtensionLoadError(extension_path, error); |
| 540 | return; |
[email protected] | 0877fd9 | 2009-02-03 16:34:06 | [diff] [blame] | 541 | } |
[email protected] | ab6f2b2 | 2009-07-28 23:28:37 | [diff] [blame] | 542 | |
| 543 | extension->set_location(Extension::LOAD); |
| 544 | ExtensionList* extensions = new ExtensionList; |
| 545 | extensions->push_back(extension); |
| 546 | ReportExtensionsLoaded(extensions); |
[email protected] | 0877fd9 | 2009-02-03 16:34:06 | [diff] [blame] | 547 | } |
| 548 | |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 549 | void ExtensionsServiceBackend::LoadInstalledExtension( |
| 550 | const std::string& id, const FilePath& path, Extension::Location location) { |
| 551 | if (CheckExternalUninstall(id, location)) { |
[email protected] | 27b985d | 2009-06-25 17:53:15 | [diff] [blame] | 552 | frontend_loop_->PostTask(FROM_HERE, NewRunnableMethod( |
| 553 | frontend_, |
| 554 | &ExtensionsService::UninstallExtension, |
| 555 | id, true)); |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 556 | |
| 557 | // No error needs to be reported. The extension effectively doesn't exist. |
| 558 | return; |
| 559 | } |
| 560 | |
[email protected] | ab6f2b2 | 2009-07-28 23:28:37 | [diff] [blame] | 561 | std::string error; |
| 562 | Extension* extension = extension_file_util::LoadExtension( |
| 563 | path, |
| 564 | true, // Require id |
| 565 | &error); |
| 566 | |
| 567 | if (!extension) { |
| 568 | ReportExtensionLoadError(path, error); |
| 569 | return; |
| 570 | } |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 571 | |
| 572 | // TODO(erikkay) now we only report a single extension loaded at a time. |
| 573 | // Perhaps we should change the notifications to remove ExtensionList. |
[email protected] | ab6f2b2 | 2009-07-28 23:28:37 | [diff] [blame] | 574 | extension->set_location(location); |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 575 | ExtensionList* extensions = new ExtensionList; |
| 576 | if (extension) |
| 577 | extensions->push_back(extension); |
| 578 | ReportExtensionsLoaded(extensions); |
| 579 | } |
| 580 | |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 581 | void ExtensionsServiceBackend::ReportExtensionLoadError( |
[email protected] | cc5da33 | 2009-03-04 08:02:51 | [diff] [blame] | 582 | const FilePath& extension_path, const std::string &error) { |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 583 | // TODO(port): note that this isn't guaranteed to work properly on Linux. |
[email protected] | cc5da33 | 2009-03-04 08:02:51 | [diff] [blame] | 584 | std::string path_str = WideToASCII(extension_path.ToWStringHack()); |
[email protected] | 3acbd42 | 2008-12-08 18:25:00 | [diff] [blame] | 585 | std::string message = StringPrintf("Could not load extension from '%s'. %s", |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 586 | path_str.c_str(), error.c_str()); |
[email protected] | bb28e06 | 2009-02-27 17:19:18 | [diff] [blame] | 587 | ExtensionErrorReporter::GetInstance()->ReportError(message, alert_on_error_); |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | void ExtensionsServiceBackend::ReportExtensionsLoaded( |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 591 | ExtensionList* extensions) { |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 592 | frontend_loop_->PostTask(FROM_HERE, NewRunnableMethod( |
| 593 | frontend_, &ExtensionsService::OnExtensionsLoaded, extensions)); |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 594 | } |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 595 | |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 596 | bool ExtensionsServiceBackend::LookupExternalExtension( |
| 597 | const std::string& id, Version** version, Extension::Location* location) { |
| 598 | scoped_ptr<Version> extension_version; |
| 599 | for (ProviderMap::const_iterator i = external_extension_providers_.begin(); |
| 600 | i != external_extension_providers_.end(); ++i) { |
[email protected] | da50530a | 2009-06-15 17:43:01 | [diff] [blame] | 601 | const ExternalExtensionProvider* provider = i->second.get(); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 602 | extension_version.reset(provider->RegisteredVersion(id, location)); |
| 603 | if (extension_version.get()) { |
| 604 | if (version) |
| 605 | *version = extension_version.release(); |
| 606 | return true; |
| 607 | } |
| 608 | } |
| 609 | return false; |
| 610 | } |
| 611 | |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 612 | // Some extensions will autoupdate themselves externally from Chrome. These |
| 613 | // are typically part of some larger client application package. To support |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 614 | // these, the extension will register its location in the the preferences file |
| 615 | // (and also, on Windows, in the registry) and this code will periodically |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 616 | // check that location for a .crx file, which it will then install locally if |
| 617 | // a new version is available. |
| 618 | void ExtensionsServiceBackend::CheckForExternalUpdates( |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 619 | std::set<std::string> ids_to_ignore, |
| 620 | scoped_refptr<ExtensionsService> frontend) { |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 621 | // Note that this installation is intentionally silent (since it didn't |
| 622 | // go through the front-end). Extensions that are registered in this |
| 623 | // way are effectively considered 'pre-bundled', and so implicitly |
| 624 | // trusted. In general, if something has HKLM or filesystem access, |
| 625 | // they could install an extension manually themselves anyway. |
| 626 | alert_on_error_ = false; |
| 627 | frontend_ = frontend; |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 628 | |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 629 | // Ask each external extension provider to give us a call back for each |
| 630 | // extension they know about. See OnExternalExtensionFound. |
| 631 | for (ProviderMap::const_iterator i = external_extension_providers_.begin(); |
| 632 | i != external_extension_providers_.end(); ++i) { |
[email protected] | da50530a | 2009-06-15 17:43:01 | [diff] [blame] | 633 | ExternalExtensionProvider* provider = i->second.get(); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 634 | provider->VisitRegisteredExtension(this, ids_to_ignore); |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 635 | } |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 636 | } |
| 637 | |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 638 | bool ExtensionsServiceBackend::CheckExternalUninstall( |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 639 | const std::string& id, Extension::Location location) { |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 640 | // Check if the providers know about this extension. |
| 641 | ProviderMap::const_iterator i = external_extension_providers_.find(location); |
| 642 | if (i != external_extension_providers_.end()) { |
| 643 | scoped_ptr<Version> version; |
| 644 | version.reset(i->second->RegisteredVersion(id, NULL)); |
| 645 | if (version.get()) |
| 646 | return false; // Yup, known extension, don't uninstall. |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 647 | } else { |
| 648 | // Not from an external provider, so it's fine. |
| 649 | return false; |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 650 | } |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 651 | |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 652 | return true; // This is not a known extension, uninstall. |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 653 | } |
| 654 | |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 655 | void ExtensionsServiceBackend::ClearProvidersForTesting() { |
| 656 | external_extension_providers_.clear(); |
| 657 | } |
| 658 | |
| 659 | void ExtensionsServiceBackend::SetProviderForTesting( |
| 660 | Extension::Location location, |
| 661 | ExternalExtensionProvider* test_provider) { |
| 662 | DCHECK(test_provider); |
[email protected] | da50530a | 2009-06-15 17:43:01 | [diff] [blame] | 663 | external_extension_providers_[location] = |
| 664 | linked_ptr<ExternalExtensionProvider>(test_provider); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | void ExtensionsServiceBackend::OnExternalExtensionFound( |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 668 | const std::string& id, const Version* version, const FilePath& path, |
| 669 | Extension::Location location) { |
| 670 | frontend_loop_->PostTask(FROM_HERE, NewRunnableMethod(frontend_, |
| 671 | &ExtensionsService::OnExternalExtensionFound, id, version->GetString(), |
| 672 | path, location)); |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 673 | } |