| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/chromeos/extensions/external_cache.h" | 5 #include "chrome/browser/chromeos/extensions/external_cache.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 ExternalCache::~ExternalCache() { | 51 ExternalCache::~ExternalCache() { |
| 52 } | 52 } |
| 53 | 53 |
| 54 void ExternalCache::Shutdown(const base::Closure& callback) { | 54 void ExternalCache::Shutdown(const base::Closure& callback) { |
| 55 local_cache_.Shutdown(callback); | 55 local_cache_.Shutdown(callback); |
| 56 } | 56 } |
| 57 | 57 |
| 58 void ExternalCache::UpdateExtensionsList( | 58 void ExternalCache::UpdateExtensionsList( |
| 59 scoped_ptr<base::DictionaryValue> prefs) { | 59 scoped_ptr<base::DictionaryValue> prefs) { |
| 60 extensions_ = prefs.Pass(); | 60 extensions_ = prefs.Pass(); |
| 61 extensions_status_.clear(); |
| 61 | 62 |
| 62 if (extensions_->empty()) { | 63 if (extensions_->empty()) { |
| 63 // If list of know extensions is empty, don't init cache on disk. It is | 64 // If list of know extensions is empty, don't init cache on disk. It is |
| 64 // important shortcut for test to don't wait forever for cache dir | 65 // important shortcut for test to don't wait forever for cache dir |
| 65 // initialization that should happen outside of Chrome on real device. | 66 // initialization that should happen outside of Chrome on real device. |
| 66 cached_extensions_->Clear(); | 67 cached_extensions_->Clear(); |
| 67 UpdateExtensionLoader(); | 68 UpdateExtensionLoader(); |
| 68 return; | 69 return; |
| 69 } | 70 } |
| 70 | 71 |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 | 145 |
| 145 void ExternalCache::OnExtensionDownloadFailed( | 146 void ExternalCache::OnExtensionDownloadFailed( |
| 146 const std::string& id, | 147 const std::string& id, |
| 147 extensions::ExtensionDownloaderDelegate::Error error, | 148 extensions::ExtensionDownloaderDelegate::Error error, |
| 148 const extensions::ExtensionDownloaderDelegate::PingResult& ping_result, | 149 const extensions::ExtensionDownloaderDelegate::PingResult& ping_result, |
| 149 const std::set<int>& request_ids) { | 150 const std::set<int>& request_ids) { |
| 150 if (error == NO_UPDATE_AVAILABLE) { | 151 if (error == NO_UPDATE_AVAILABLE) { |
| 151 if (!cached_extensions_->HasKey(id)) { | 152 if (!cached_extensions_->HasKey(id)) { |
| 152 LOG(ERROR) << "ExternalCache extension " << id | 153 LOG(ERROR) << "ExternalCache extension " << id |
| 153 << " not found on update server"; | 154 << " not found on update server"; |
| 155 extensions_status_[id] = DOWNLOAD_FAILED; |
| 154 delegate_->OnExtensionDownloadFailed(id, error); | 156 delegate_->OnExtensionDownloadFailed(id, error); |
| 155 } else { | 157 } else { |
| 158 // No version update for an already cached extension. |
| 159 extensions_status_[id] = DOWNLOAD_SUCCEEDED; |
| 156 delegate_->OnExtensionLoadedInCache(id); | 160 delegate_->OnExtensionLoadedInCache(id); |
| 157 } | 161 } |
| 158 } else { | 162 } else { |
| 159 LOG(ERROR) << "ExternalCache failed to download extension " << id | 163 LOG(ERROR) << "ExternalCache failed to download extension " << id |
| 160 << ", error " << error; | 164 << ", error " << error; |
| 165 extensions_status_[id] = DOWNLOAD_FAILED; |
| 161 delegate_->OnExtensionDownloadFailed(id, error); | 166 delegate_->OnExtensionDownloadFailed(id, error); |
| 162 } | 167 } |
| 163 } | 168 } |
| 164 | 169 |
| 165 void ExternalCache::OnExtensionDownloadFinished( | 170 void ExternalCache::OnExtensionDownloadFinished( |
| 166 const std::string& id, | 171 const std::string& id, |
| 167 const base::FilePath& path, | 172 const base::FilePath& path, |
| 168 bool file_ownership_passed, | 173 bool file_ownership_passed, |
| 169 const GURL& download_url, | 174 const GURL& download_url, |
| 170 const std::string& version, | 175 const std::string& version, |
| (...skipping 18 matching lines...) Expand all Loading... |
| 189 if (extension_dictionary->GetString( | 194 if (extension_dictionary->GetString( |
| 190 extensions::ExternalProviderImpl::kExternalVersion, version)) { | 195 extensions::ExternalProviderImpl::kExternalVersion, version)) { |
| 191 return true; | 196 return true; |
| 192 } | 197 } |
| 193 *version = delegate_->GetInstalledExtensionVersion(id); | 198 *version = delegate_->GetInstalledExtensionVersion(id); |
| 194 return !version->empty(); | 199 return !version->empty(); |
| 195 } | 200 } |
| 196 return false; | 201 return false; |
| 197 } | 202 } |
| 198 | 203 |
| 204 bool ExternalCache::IsExtensionPendingForUpdateCheck(const std::string& id) { |
| 205 return extensions_->HasKey(id) && cached_extensions_->HasKey(id) && |
| 206 extensions_status_.find(id) != extensions_status_.end() && |
| 207 extensions_status_[id] == DOWNLOAD_PENDING; |
| 208 } |
| 209 |
| 210 bool ExternalCache::IsExtensionDownloadFailed(const std::string& id) { |
| 211 return extensions_status_.find(id) != extensions_status_.end() && |
| 212 extensions_status_[id] == DOWNLOAD_FAILED; |
| 213 } |
| 214 |
| 199 void ExternalCache::UpdateExtensionLoader() { | 215 void ExternalCache::UpdateExtensionLoader() { |
| 200 VLOG(1) << "Notify ExternalCache delegate about cache update"; | 216 VLOG(1) << "Notify ExternalCache delegate about cache update"; |
| 201 if (delegate_) | 217 if (delegate_) |
| 202 delegate_->OnExtensionListsUpdated(cached_extensions_.get()); | 218 delegate_->OnExtensionListsUpdated(cached_extensions_.get()); |
| 203 } | 219 } |
| 204 | 220 |
| 205 void ExternalCache::CheckCache() { | 221 void ExternalCache::CheckCache() { |
| 206 if (local_cache_.is_shutdown()) | 222 if (local_cache_.is_shutdown()) |
| 207 return; | 223 return; |
| 208 | 224 |
| 209 // If request_context_ is missing we can't download anything. | 225 // If request_context_ is missing we can't download anything. |
| 210 if (!downloader_ && request_context_) { | 226 if (!downloader_ && request_context_) { |
| 211 downloader_.reset( | 227 downloader_.reset( |
| 212 new extensions::ExtensionDownloader(this, request_context_)); | 228 new extensions::ExtensionDownloader(this, request_context_)); |
| 213 } | 229 } |
| 214 | 230 |
| 215 cached_extensions_->Clear(); | 231 cached_extensions_->Clear(); |
| 232 extensions_status_.clear(); |
| 216 for (base::DictionaryValue::Iterator it(*extensions_.get()); | 233 for (base::DictionaryValue::Iterator it(*extensions_.get()); |
| 217 !it.IsAtEnd(); it.Advance()) { | 234 !it.IsAtEnd(); it.Advance()) { |
| 218 const base::DictionaryValue* entry = NULL; | 235 const base::DictionaryValue* entry = NULL; |
| 219 if (!it.value().GetAsDictionary(&entry)) { | 236 if (!it.value().GetAsDictionary(&entry)) { |
| 220 LOG(ERROR) << "ExternalCache found bad entry with type " | 237 LOG(ERROR) << "ExternalCache found bad entry with type " |
| 221 << it.value().GetType(); | 238 << it.value().GetType(); |
| 222 continue; | 239 continue; |
| 223 } | 240 } |
| 224 | 241 |
| 225 bool keep_if_present = | 242 bool keep_if_present = |
| 226 entry->HasKey(extensions::ExternalProviderImpl::kKeepIfPresent); | 243 entry->HasKey(extensions::ExternalProviderImpl::kKeepIfPresent); |
| 227 std::string external_update_url; | 244 std::string external_update_url; |
| 228 entry->GetString(extensions::ExternalProviderImpl::kExternalUpdateUrl, | 245 entry->GetString(extensions::ExternalProviderImpl::kExternalUpdateUrl, |
| 229 &external_update_url); | 246 &external_update_url); |
| 230 if (downloader_ && !keep_if_present) { | 247 if (downloader_ && !keep_if_present) { |
| 231 GURL update_url; | 248 GURL update_url; |
| 232 if (!external_update_url.empty()) | 249 if (!external_update_url.empty()) |
| 233 update_url = GURL(external_update_url); | 250 update_url = GURL(external_update_url); |
| 234 else if (always_check_updates_) | 251 else if (always_check_updates_) |
| 235 update_url = extension_urls::GetWebstoreUpdateUrl(); | 252 update_url = extension_urls::GetWebstoreUpdateUrl(); |
| 236 | 253 |
| 237 if (update_url.is_valid()) | 254 if (update_url.is_valid()) { |
| 238 downloader_->AddPendingExtension(it.key(), update_url, 0); | 255 downloader_->AddPendingExtension(it.key(), update_url, 0); |
| 256 extensions_status_[it.key()] = DOWNLOAD_PENDING; |
| 257 } |
| 239 } | 258 } |
| 240 | 259 |
| 241 base::FilePath file_path; | 260 base::FilePath file_path; |
| 242 std::string version; | 261 std::string version; |
| 243 if (local_cache_.GetExtension(it.key(), &file_path, &version)) { | 262 if (local_cache_.GetExtension(it.key(), &file_path, &version)) { |
| 244 // Copy entry to don't modify it inside extensions_. | 263 // Copy entry to don't modify it inside extensions_. |
| 245 base::DictionaryValue* entry_copy = entry->DeepCopy(); | 264 base::DictionaryValue* entry_copy = entry->DeepCopy(); |
| 246 | 265 |
| 247 if (extension_urls::IsWebstoreUpdateUrl(GURL(external_update_url))) { | 266 if (extension_urls::IsWebstoreUpdateUrl(GURL(external_update_url))) { |
| 248 entry_copy->SetBoolean( | 267 entry_copy->SetBoolean( |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 308 &update_url) && | 327 &update_url) && |
| 309 extension_urls::IsWebstoreUpdateUrl(GURL(update_url))) { | 328 extension_urls::IsWebstoreUpdateUrl(GURL(update_url))) { |
| 310 entry->SetBoolean(extensions::ExternalProviderImpl::kIsFromWebstore, true); | 329 entry->SetBoolean(extensions::ExternalProviderImpl::kIsFromWebstore, true); |
| 311 } | 330 } |
| 312 entry->Remove(extensions::ExternalProviderImpl::kExternalUpdateUrl, NULL); | 331 entry->Remove(extensions::ExternalProviderImpl::kExternalUpdateUrl, NULL); |
| 313 entry->SetString(extensions::ExternalProviderImpl::kExternalVersion, version); | 332 entry->SetString(extensions::ExternalProviderImpl::kExternalVersion, version); |
| 314 entry->SetString(extensions::ExternalProviderImpl::kExternalCrx, | 333 entry->SetString(extensions::ExternalProviderImpl::kExternalCrx, |
| 315 file_path.value()); | 334 file_path.value()); |
| 316 | 335 |
| 317 cached_extensions_->Set(id, entry); | 336 cached_extensions_->Set(id, entry); |
| 337 extensions_status_[id] = DOWNLOAD_SUCCEEDED; |
| 318 if (delegate_) | 338 if (delegate_) |
| 319 delegate_->OnExtensionLoadedInCache(id); | 339 delegate_->OnExtensionLoadedInCache(id); |
| 320 UpdateExtensionLoader(); | 340 UpdateExtensionLoader(); |
| 321 } | 341 } |
| 322 | 342 |
| 323 std::string ExternalCache::Delegate::GetInstalledExtensionVersion( | 343 std::string ExternalCache::Delegate::GetInstalledExtensionVersion( |
| 324 const std::string& id) { | 344 const std::string& id) { |
| 325 return std::string(); | 345 return std::string(); |
| 326 } | 346 } |
| 327 | 347 |
| 328 } // namespace chromeos | 348 } // namespace chromeos |
| OLD | NEW |