Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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.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 Visitor* 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 DCHECK(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 loader_->StartLoading(); |
| 40 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 54 } |
| 41 DCHECK(prefs_.get()); | 55 |
| 56 void ExternalExtensionProviderImpl::SetPrefs(DictionaryValue* prefs) { | |
| 57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 58 | |
| 59 prefs_.reset(prefs); | |
| 60 if (!service_) return; | |
|
Sam Kerner (Chrome)
2011/01/04 14:06:00
Can this happen? I see two ways: User called Ser
gfeher
2011/01/04 23:37:09
It is possible that ExtensionService calls Service
| |
| 61 | |
| 62 ready_ = true; | |
| 42 for (DictionaryValue::key_iterator i = prefs_->begin_keys(); | 63 for (DictionaryValue::key_iterator i = prefs_->begin_keys(); |
| 43 i != prefs_->end_keys(); ++i) { | 64 i != prefs_->end_keys(); ++i) { |
| 44 const std::string& extension_id = *i; | 65 const std::string& extension_id = *i; |
| 45 DictionaryValue* extension; | 66 DictionaryValue* extension; |
| 46 if (!prefs_->GetDictionaryWithoutPathExpansion(extension_id, &extension)) | 67 if (!prefs_->GetDictionaryWithoutPathExpansion(extension_id, &extension)) |
| 47 continue; | 68 continue; |
| 48 | 69 |
| 49 FilePath::StringType external_crx; | 70 FilePath::StringType external_crx; |
| 50 std::string external_version; | 71 std::string external_version; |
| 51 std::string external_update_url; | 72 std::string external_update_url; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 93 } | 114 } |
| 94 | 115 |
| 95 scoped_ptr<Version> version; | 116 scoped_ptr<Version> version; |
| 96 version.reset(Version::GetVersionFromString(external_version)); | 117 version.reset(Version::GetVersionFromString(external_version)); |
| 97 if (!version.get()) { | 118 if (!version.get()) { |
| 98 LOG(WARNING) << "Malformed extension dictionary for extension: " | 119 LOG(WARNING) << "Malformed extension dictionary for extension: " |
| 99 << extension_id.c_str() << ". Invalid version string \"" | 120 << extension_id.c_str() << ". Invalid version string \"" |
| 100 << external_version << "\"."; | 121 << external_version << "\"."; |
| 101 continue; | 122 continue; |
| 102 } | 123 } |
| 103 visitor->OnExternalExtensionFileFound(extension_id, version.get(), path, | 124 service_->OnExternalExtensionFileFound(extension_id, version.get(), path, |
| 104 crx_location_); | 125 crx_location_); |
| 105 } else { // if (has_external_update_url) | 126 } else { // if (has_external_update_url) |
| 106 DCHECK(has_external_update_url); // Checking of keys above ensures this. | 127 DCHECK(has_external_update_url); // Checking of keys above ensures this. |
| 107 if (download_location_ == Extension::INVALID) { | 128 if (download_location_ == Extension::INVALID) { |
| 108 LOG(WARNING) << "This provider does not support installing external " | 129 LOG(WARNING) << "This provider does not support installing external " |
| 109 << "extensions from update URLs."; | 130 << "extensions from update URLs."; |
| 110 continue; | 131 continue; |
| 111 } | 132 } |
| 112 GURL update_url(external_update_url); | 133 GURL update_url(external_update_url); |
| 113 if (!update_url.is_valid()) { | 134 if (!update_url.is_valid()) { |
| 114 LOG(WARNING) << "Malformed extension dictionary for extension: " | 135 LOG(WARNING) << "Malformed extension dictionary for extension: " |
| 115 << extension_id.c_str() << ". " << kExternalUpdateUrl | 136 << extension_id.c_str() << ". " << kExternalUpdateUrl |
| 116 << " must be a valid URL. Saw \"" << external_update_url | 137 << " must be a valid URL. Saw \"" << external_update_url |
| 117 << "\"."; | 138 << "\"."; |
| 118 continue; | 139 continue; |
| 119 } | 140 } |
| 120 visitor->OnExternalExtensionUpdateUrlFound( | 141 service_->OnExternalExtensionUpdateUrlFound( |
| 121 extension_id, update_url, download_location_); | 142 extension_id, update_url, download_location_); |
| 122 } | 143 } |
| 123 } | 144 } |
| 145 ready_ = true; | |
| 146 service_->OnExternalProviderReady(); | |
| 124 } | 147 } |
| 125 | 148 |
| 126 bool StatefulExternalExtensionProvider::HasExtension( | 149 void ExternalExtensionProviderImpl::ServiceShutdown() { |
| 150 service_ = NULL; | |
| 151 } | |
| 152 | |
| 153 bool ExternalExtensionProviderImpl::IsReady() { | |
| 154 return ready_; | |
| 155 } | |
| 156 | |
| 157 bool ExternalExtensionProviderImpl::HasExtension( | |
| 127 const std::string& id) const { | 158 const std::string& id) const { |
| 128 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 159 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 129 DCHECK(prefs_.get()); | 160 DCHECK(prefs_.get()); |
| 161 DCHECK(ready_); | |
| 130 return prefs_->HasKey(id); | 162 return prefs_->HasKey(id); |
| 131 } | 163 } |
| 132 | 164 |
| 133 bool StatefulExternalExtensionProvider::GetExtensionDetails( | 165 bool ExternalExtensionProviderImpl::GetExtensionDetails( |
| 134 const std::string& id, Extension::Location* location, | 166 const std::string& id, Extension::Location* location, |
| 135 scoped_ptr<Version>* version) const { | 167 scoped_ptr<Version>* version) const { |
| 136 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 168 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 137 DCHECK(prefs_.get()); | 169 DCHECK(prefs_.get()); |
| 170 DCHECK(ready_); | |
| 138 DictionaryValue* extension = NULL; | 171 DictionaryValue* extension = NULL; |
| 139 if (!prefs_->GetDictionary(id, &extension)) | 172 if (!prefs_->GetDictionary(id, &extension)) |
| 140 return false; | 173 return false; |
| 141 | 174 |
| 142 Extension::Location loc = Extension::INVALID; | 175 Extension::Location loc = Extension::INVALID; |
| 143 if (extension->HasKey(kExternalUpdateUrl)) { | 176 if (extension->HasKey(kExternalUpdateUrl)) { |
| 144 loc = download_location_; | 177 loc = download_location_; |
| 145 | 178 |
| 146 } else if (extension->HasKey(kExternalCrx)) { | 179 } else if (extension->HasKey(kExternalCrx)) { |
| 147 loc = crx_location_; | 180 loc = crx_location_; |
| 148 | 181 |
| 149 std::string external_version; | 182 std::string external_version; |
| 150 if (!extension->GetString(kExternalVersion, &external_version)) | 183 if (!extension->GetString(kExternalVersion, &external_version)) |
| 151 return false; | 184 return false; |
| 152 | 185 |
| 153 if (version) | 186 if (version) |
| 154 version->reset(Version::GetVersionFromString(external_version)); | 187 version->reset(Version::GetVersionFromString(external_version)); |
| 155 | 188 |
| 156 } else { | 189 } else { |
| 157 NOTREACHED(); // Chrome should not allow prefs to get into this state. | 190 NOTREACHED(); // Chrome should not allow prefs to get into this state. |
| 158 return false; | 191 return false; |
| 159 } | 192 } |
| 160 | 193 |
| 161 if (location) | 194 if (location) |
| 162 *location = loc; | 195 *location = loc; |
| 163 | 196 |
| 164 return true; | 197 return true; |
| 165 } | 198 } |
| 166 | 199 |
| 167 void StatefulExternalExtensionProvider::set_prefs(DictionaryValue* prefs) { | 200 // static |
| 168 prefs_.reset(prefs); | 201 void ExternalExtensionProviderImpl::CreateExternalProviders( |
| 202 Visitor* service, | |
| 203 Profile* profile, | |
| 204 ProviderCollection* provider_list) { | |
| 205 provider_list->push_back( | |
| 206 linked_ptr<ExternalExtensionProvider>( | |
| 207 new ExternalExtensionProviderImpl( | |
| 208 service, | |
| 209 new ExternalPrefExtensionLoader, | |
| 210 Extension::EXTERNAL_PREF, | |
| 211 Extension::EXTERNAL_PREF_DOWNLOAD))); | |
| 212 #if defined(OS_WIN) | |
| 213 provider_list->push_back( | |
| 214 linked_ptr<ExternalExtensionProvider>( | |
| 215 new ExternalExtensionProviderImpl( | |
| 216 service, | |
| 217 new ExternalRegistryExtensionLoader, | |
| 218 Extension::EXTERNAL_REGISTRY, | |
| 219 Extension::INVALID))); | |
| 220 #endif | |
| 221 provider_list->push_back( | |
| 222 linked_ptr<ExternalExtensionProvider>( | |
| 223 new ExternalExtensionProviderImpl( | |
| 224 service, | |
| 225 new ExternalPolicyExtensionLoader(profile), | |
| 226 Extension::INVALID, | |
| 227 Extension::EXTERNAL_POLICY_DOWNLOAD))); | |
| 169 } | 228 } |
| OLD | NEW |