| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/stateful_external_extension_provider.h" | 5 #include "chrome/browser/extensions/external_extension_provider_impl.h" |
| 6 | 6 |
| 7 #include "app/app_paths.h" | 7 #include "app/app_paths.h" |
| 8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/linked_ptr.h" |
| 10 #include "base/path_service.h" | 11 #include "base/path_service.h" |
| 11 #include "base/values.h" | 12 #include "base/values.h" |
| 12 #include "base/version.h" | 13 #include "base/version.h" |
| 13 #include "chrome/browser/browser_thread.h" | 14 #include "chrome/browser/browser_thread.h" |
| 15 #include "chrome/browser/extensions/external_extension_provider_interface.h" |
| 16 #include "chrome/browser/extensions/external_policy_extension_loader.h" |
| 17 #include "chrome/browser/extensions/external_pref_extension_loader.h" |
| 18 #include "chrome/browser/profiles/profile.h" |
| 14 | 19 |
| 15 namespace { | 20 #if defined(OS_WIN) |
| 21 #include "chrome/browser/extensions/external_registry_extension_loader_win.h" |
| 22 #endif |
| 16 | 23 |
| 17 // Constants for keeping track of extension preferences. | 24 // Constants for keeping track of extension preferences in a dictionary. |
| 18 const char kLocation[] = "location"; | 25 const char ExternalExtensionProviderImpl::kLocation[] = "location"; |
| 19 const char kState[] = "state"; | 26 const char ExternalExtensionProviderImpl::kState[] = "state"; |
| 20 const char kExternalCrx[] = "external_crx"; | 27 const char ExternalExtensionProviderImpl::kExternalCrx[] = "external_crx"; |
| 21 const char kExternalVersion[] = "external_version"; | 28 const char ExternalExtensionProviderImpl::kExternalVersion[] = |
| 22 const char kExternalUpdateUrl[] = "external_update_url"; | 29 "external_version"; |
| 30 const char ExternalExtensionProviderImpl::kExternalUpdateUrl[] = |
| 31 "external_update_url"; |
| 23 | 32 |
| 24 } | 33 ExternalExtensionProviderImpl::ExternalExtensionProviderImpl( |
| 25 | 34 VisitorInterface* service, |
| 26 StatefulExternalExtensionProvider::StatefulExternalExtensionProvider( | 35 ExternalExtensionLoader* loader, |
| 27 Extension::Location crx_location, | 36 Extension::Location crx_location, |
| 28 Extension::Location download_location) | 37 Extension::Location download_location) |
| 29 : crx_location_(crx_location), | 38 : crx_location_(crx_location), |
| 30 download_location_(download_location) { | 39 download_location_(download_location), |
| 31 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 40 service_(service), |
| 41 prefs_(NULL), |
| 42 ready_(false), |
| 43 loader_(loader) { |
| 44 loader_->Init(this); |
| 32 } | 45 } |
| 33 | 46 |
| 34 StatefulExternalExtensionProvider::~StatefulExternalExtensionProvider() { | 47 ExternalExtensionProviderImpl::~ExternalExtensionProviderImpl() { |
| 35 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 48 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 49 loader_->OwnerShutdown(); |
| 36 } | 50 } |
| 37 | 51 |
| 38 void StatefulExternalExtensionProvider::VisitRegisteredExtension( | 52 void ExternalExtensionProviderImpl::VisitRegisteredExtension() const { |
| 39 Visitor* visitor) const { | 53 // The loader will call back to SetPrefs. |
| 40 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 54 loader_->StartLoading(); |
| 41 DCHECK(prefs_.get()); | 55 } |
| 56 |
| 57 void ExternalExtensionProviderImpl::SetPrefs(DictionaryValue* prefs) { |
| 58 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 59 |
| 60 // Check if the service is still alive. It is possible that it had went |
| 61 // away while |loader_| was working on the FILE thread. |
| 62 if (!service_) return; |
| 63 |
| 64 prefs_.reset(prefs); |
| 65 ready_ = true; // Queries for extensions are allowed from this point. |
| 66 |
| 67 // Notify ExtensionService about all the extensions this provider has. |
| 42 for (DictionaryValue::key_iterator i = prefs_->begin_keys(); | 68 for (DictionaryValue::key_iterator i = prefs_->begin_keys(); |
| 43 i != prefs_->end_keys(); ++i) { | 69 i != prefs_->end_keys(); ++i) { |
| 44 const std::string& extension_id = *i; | 70 const std::string& extension_id = *i; |
| 45 DictionaryValue* extension; | 71 DictionaryValue* extension; |
| 46 if (!prefs_->GetDictionaryWithoutPathExpansion(extension_id, &extension)) | 72 if (!prefs_->GetDictionaryWithoutPathExpansion(extension_id, &extension)) |
| 47 continue; | 73 continue; |
| 48 | 74 |
| 49 FilePath::StringType external_crx; | 75 FilePath::StringType external_crx; |
| 50 std::string external_version; | 76 std::string external_version; |
| 51 std::string external_update_url; | 77 std::string external_update_url; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 } | 119 } |
| 94 | 120 |
| 95 scoped_ptr<Version> version; | 121 scoped_ptr<Version> version; |
| 96 version.reset(Version::GetVersionFromString(external_version)); | 122 version.reset(Version::GetVersionFromString(external_version)); |
| 97 if (!version.get()) { | 123 if (!version.get()) { |
| 98 LOG(WARNING) << "Malformed extension dictionary for extension: " | 124 LOG(WARNING) << "Malformed extension dictionary for extension: " |
| 99 << extension_id.c_str() << ". Invalid version string \"" | 125 << extension_id.c_str() << ". Invalid version string \"" |
| 100 << external_version << "\"."; | 126 << external_version << "\"."; |
| 101 continue; | 127 continue; |
| 102 } | 128 } |
| 103 visitor->OnExternalExtensionFileFound(extension_id, version.get(), path, | 129 service_->OnExternalExtensionFileFound(extension_id, version.get(), path, |
| 104 crx_location_); | 130 crx_location_); |
| 105 } else { // if (has_external_update_url) | 131 } else { // if (has_external_update_url) |
| 106 DCHECK(has_external_update_url); // Checking of keys above ensures this. | 132 CHECK(has_external_update_url); // Checking of keys above ensures this. |
| 107 if (download_location_ == Extension::INVALID) { | 133 if (download_location_ == Extension::INVALID) { |
| 108 LOG(WARNING) << "This provider does not support installing external " | 134 LOG(WARNING) << "This provider does not support installing external " |
| 109 << "extensions from update URLs."; | 135 << "extensions from update URLs."; |
| 110 continue; | 136 continue; |
| 111 } | 137 } |
| 112 GURL update_url(external_update_url); | 138 GURL update_url(external_update_url); |
| 113 if (!update_url.is_valid()) { | 139 if (!update_url.is_valid()) { |
| 114 LOG(WARNING) << "Malformed extension dictionary for extension: " | 140 LOG(WARNING) << "Malformed extension dictionary for extension: " |
| 115 << extension_id.c_str() << ". " << kExternalUpdateUrl | 141 << extension_id.c_str() << ". " << kExternalUpdateUrl |
| 116 << " must be a valid URL. Saw \"" << external_update_url | 142 << " must be a valid URL. Saw \"" << external_update_url |
| 117 << "\"."; | 143 << "\"."; |
| 118 continue; | 144 continue; |
| 119 } | 145 } |
| 120 visitor->OnExternalExtensionUpdateUrlFound( | 146 service_->OnExternalExtensionUpdateUrlFound( |
| 121 extension_id, update_url, download_location_); | 147 extension_id, update_url, download_location_); |
| 122 } | 148 } |
| 123 } | 149 } |
| 150 |
| 151 service_->OnExternalProviderReady(); |
| 124 } | 152 } |
| 125 | 153 |
| 126 bool StatefulExternalExtensionProvider::HasExtension( | 154 void ExternalExtensionProviderImpl::ServiceShutdown() { |
| 155 service_ = NULL; |
| 156 } |
| 157 |
| 158 bool ExternalExtensionProviderImpl::IsReady() { |
| 159 return ready_; |
| 160 } |
| 161 |
| 162 bool ExternalExtensionProviderImpl::HasExtension( |
| 127 const std::string& id) const { | 163 const std::string& id) const { |
| 128 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 164 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 129 DCHECK(prefs_.get()); | 165 CHECK(prefs_.get()); |
| 166 CHECK(ready_); |
| 130 return prefs_->HasKey(id); | 167 return prefs_->HasKey(id); |
| 131 } | 168 } |
| 132 | 169 |
| 133 bool StatefulExternalExtensionProvider::GetExtensionDetails( | 170 bool ExternalExtensionProviderImpl::GetExtensionDetails( |
| 134 const std::string& id, Extension::Location* location, | 171 const std::string& id, Extension::Location* location, |
| 135 scoped_ptr<Version>* version) const { | 172 scoped_ptr<Version>* version) const { |
| 136 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 173 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 137 DCHECK(prefs_.get()); | 174 CHECK(prefs_.get()); |
| 175 CHECK(ready_); |
| 138 DictionaryValue* extension = NULL; | 176 DictionaryValue* extension = NULL; |
| 139 if (!prefs_->GetDictionary(id, &extension)) | 177 if (!prefs_->GetDictionary(id, &extension)) |
| 140 return false; | 178 return false; |
| 141 | 179 |
| 142 Extension::Location loc = Extension::INVALID; | 180 Extension::Location loc = Extension::INVALID; |
| 143 if (extension->HasKey(kExternalUpdateUrl)) { | 181 if (extension->HasKey(kExternalUpdateUrl)) { |
| 144 loc = download_location_; | 182 loc = download_location_; |
| 145 | 183 |
| 146 } else if (extension->HasKey(kExternalCrx)) { | 184 } else if (extension->HasKey(kExternalCrx)) { |
| 147 loc = crx_location_; | 185 loc = crx_location_; |
| 148 | 186 |
| 149 std::string external_version; | 187 std::string external_version; |
| 150 if (!extension->GetString(kExternalVersion, &external_version)) | 188 if (!extension->GetString(kExternalVersion, &external_version)) |
| 151 return false; | 189 return false; |
| 152 | 190 |
| 153 if (version) | 191 if (version) |
| 154 version->reset(Version::GetVersionFromString(external_version)); | 192 version->reset(Version::GetVersionFromString(external_version)); |
| 155 | 193 |
| 156 } else { | 194 } else { |
| 157 NOTREACHED(); // Chrome should not allow prefs to get into this state. | 195 NOTREACHED(); // Chrome should not allow prefs to get into this state. |
| 158 return false; | 196 return false; |
| 159 } | 197 } |
| 160 | 198 |
| 161 if (location) | 199 if (location) |
| 162 *location = loc; | 200 *location = loc; |
| 163 | 201 |
| 164 return true; | 202 return true; |
| 165 } | 203 } |
| 166 | 204 |
| 167 void StatefulExternalExtensionProvider::set_prefs(DictionaryValue* prefs) { | 205 // static |
| 168 prefs_.reset(prefs); | 206 void ExternalExtensionProviderImpl::CreateExternalProviders( |
| 207 VisitorInterface* service, |
| 208 Profile* profile, |
| 209 ProviderCollection* provider_list) { |
| 210 provider_list->push_back( |
| 211 linked_ptr<ExternalExtensionProviderInterface>( |
| 212 new ExternalExtensionProviderImpl( |
| 213 service, |
| 214 new ExternalPrefExtensionLoader, |
| 215 Extension::EXTERNAL_PREF, |
| 216 Extension::EXTERNAL_PREF_DOWNLOAD))); |
| 217 #if defined(OS_WIN) |
| 218 provider_list->push_back( |
| 219 linked_ptr<ExternalExtensionProviderInterface>( |
| 220 new ExternalExtensionProviderImpl( |
| 221 service, |
| 222 new ExternalRegistryExtensionLoader, |
| 223 Extension::EXTERNAL_REGISTRY, |
| 224 Extension::INVALID))); |
| 225 #endif |
| 226 provider_list->push_back( |
| 227 linked_ptr<ExternalExtensionProviderInterface>( |
| 228 new ExternalExtensionProviderImpl( |
| 229 service, |
| 230 new ExternalPolicyExtensionLoader(profile), |
| 231 Extension::INVALID, |
| 232 Extension::EXTERNAL_POLICY_DOWNLOAD))); |
| 169 } | 233 } |
| OLD | NEW |