1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/extensions/external_cache.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/callback.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_util.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/strings/string_util.h"
#include "base/values.h"
#include "base/version.h"
#include "chrome/browser/extensions/crx_installer.h"
#include "chrome/browser/extensions/external_provider_impl.h"
#include "chrome/browser/extensions/updater/chrome_extension_downloader_factory.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
#include "extensions/browser/notification_types.h"
#include "extensions/browser/updater/extension_downloader.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_urls.h"
#include "net/url_request/url_request_context_getter.h"
namespace chromeos {
ExternalCache::ExternalCache(const base::FilePath& cache_dir,
net::URLRequestContextGetter* request_context,
const scoped_refptr<base::SequencedTaskRunner>&
backend_task_runner,
Delegate* delegate,
bool always_check_updates,
bool wait_for_cache_initialization)
: local_cache_(cache_dir, 0, base::TimeDelta(), backend_task_runner),
request_context_(request_context),
backend_task_runner_(backend_task_runner),
delegate_(delegate),
always_check_updates_(always_check_updates),
wait_for_cache_initialization_(wait_for_cache_initialization),
cached_extensions_(new base::DictionaryValue()),
weak_ptr_factory_(this) {
notification_registrar_.Add(
this,
extensions::NOTIFICATION_EXTENSION_INSTALL_ERROR,
content::NotificationService::AllBrowserContextsAndSources());
}
ExternalCache::~ExternalCache() {
}
void ExternalCache::Shutdown(const base::Closure& callback) {
local_cache_.Shutdown(callback);
}
void ExternalCache::UpdateExtensionsList(
scoped_ptr<base::DictionaryValue> prefs) {
extensions_ = prefs.Pass();
if (extensions_->empty()) {
// If list of know extensions is empty, don't init cache on disk. It is
// important shortcut for test to don't wait forever for cache dir
// initialization that should happen outside of Chrome on real device.
cached_extensions_->Clear();
UpdateExtensionLoader();
return;
}
if (local_cache_.is_uninitialized()) {
local_cache_.Init(wait_for_cache_initialization_,
base::Bind(&ExternalCache::CheckCache,
weak_ptr_factory_.GetWeakPtr()));
} else {
CheckCache();
}
}
void ExternalCache::OnDamagedFileDetected(const base::FilePath& path) {
for (base::DictionaryValue::Iterator it(*cached_extensions_.get());
!it.IsAtEnd(); it.Advance()) {
const base::DictionaryValue* entry = NULL;
if (!it.value().GetAsDictionary(&entry)) {
NOTREACHED() << "ExternalCache found bad entry with type "
<< it.value().GetType();
continue;
}
std::string external_crx;
if (entry->GetString(extensions::ExternalProviderImpl::kExternalCrx,
&external_crx) &&
external_crx == path.value()) {
std::string id = it.key();
LOG(ERROR) << "ExternalCache extension at " << path.value()
<< " failed to install, deleting it.";
cached_extensions_->Remove(id, NULL);
extensions_->Remove(id, NULL);
local_cache_.RemoveExtension(id);
UpdateExtensionLoader();
// Don't try to DownloadMissingExtensions() from here,
// since it can cause a fail/retry loop.
return;
}
}
LOG(ERROR) << "ExternalCache cannot find external_crx " << path.value();
}
void ExternalCache::RemoveExtensions(const std::vector<std::string>& ids) {
if (ids.empty())
return;
for (size_t i = 0; i < ids.size(); ++i) {
cached_extensions_->Remove(ids[i], NULL);
extensions_->Remove(ids[i], NULL);
local_cache_.RemoveExtension(ids[i]);
}
UpdateExtensionLoader();
}
bool ExternalCache::GetExtension(const std::string& id,
base::FilePath* file_path,
std::string* version) {
return local_cache_.GetExtension(id, file_path, version);
}
void ExternalCache::PutExternalExtension(
const std::string& id,
const base::FilePath& crx_file_path,
const std::string& version,
const PutExternalExtensionCallback& callback) {
local_cache_.PutExtension(id,
crx_file_path,
version,
base::Bind(&ExternalCache::OnPutExternalExtension,
weak_ptr_factory_.GetWeakPtr(),
id,
callback));
}
void ExternalCache::Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
switch (type) {
case extensions::NOTIFICATION_EXTENSION_INSTALL_ERROR: {
extensions::CrxInstaller* installer =
content::Source<extensions::CrxInstaller>(source).ptr();
OnDamagedFileDetected(installer->source_file());
break;
}
default:
NOTREACHED();
}
}
void ExternalCache::OnExtensionDownloadFailed(
const std::string& id,
extensions::ExtensionDownloaderDelegate::Error error,
const extensions::ExtensionDownloaderDelegate::PingResult& ping_result,
const std::set<int>& request_ids) {
if (error == NO_UPDATE_AVAILABLE) {
if (!cached_extensions_->HasKey(id)) {
LOG(ERROR) << "ExternalCache extension " << id
<< " not found on update server";
delegate_->OnExtensionDownloadFailed(id, error);
} else {
// No version update for an already cached extension.
delegate_->OnExtensionLoadedInCache(id);
}
} else {
LOG(ERROR) << "ExternalCache failed to download extension " << id
<< ", error " << error;
delegate_->OnExtensionDownloadFailed(id, error);
}
}
void ExternalCache::OnExtensionDownloadFinished(
const std::string& id,
const base::FilePath& path,
bool file_ownership_passed,
const GURL& download_url,
const std::string& version,
const extensions::ExtensionDownloaderDelegate::PingResult& ping_result,
const std::set<int>& request_ids) {
DCHECK(file_ownership_passed);
local_cache_.PutExtension(id, path, version,
base::Bind(&ExternalCache::OnPutExtension,
weak_ptr_factory_.GetWeakPtr(),
id));
}
bool ExternalCache::IsExtensionPending(const std::string& id) {
// Pending means that there is no installed version yet.
return extensions_->HasKey(id) && !cached_extensions_->HasKey(id);
}
bool ExternalCache::GetExtensionExistingVersion(const std::string& id,
std::string* version) {
base::DictionaryValue* extension_dictionary = NULL;
if (cached_extensions_->GetDictionary(id, &extension_dictionary)) {
if (extension_dictionary->GetString(
extensions::ExternalProviderImpl::kExternalVersion, version)) {
return true;
}
*version = delegate_->GetInstalledExtensionVersion(id);
return !version->empty();
}
return false;
}
void ExternalCache::UpdateExtensionLoader() {
VLOG(1) << "Notify ExternalCache delegate about cache update";
if (delegate_)
delegate_->OnExtensionListsUpdated(cached_extensions_.get());
}
void ExternalCache::CheckCache() {
if (local_cache_.is_shutdown())
return;
// If request_context_ is missing we can't download anything.
if (!downloader_ && request_context_.get()) {
downloader_ = ChromeExtensionDownloaderFactory::CreateForRequestContext(
request_context_.get(), this);
}
cached_extensions_->Clear();
for (base::DictionaryValue::Iterator it(*extensions_.get());
!it.IsAtEnd(); it.Advance()) {
const base::DictionaryValue* entry = NULL;
if (!it.value().GetAsDictionary(&entry)) {
LOG(ERROR) << "ExternalCache found bad entry with type "
<< it.value().GetType();
continue;
}
bool keep_if_present =
entry->HasKey(extensions::ExternalProviderImpl::kKeepIfPresent);
std::string external_update_url;
entry->GetString(extensions::ExternalProviderImpl::kExternalUpdateUrl,
&external_update_url);
if (downloader_ && !keep_if_present) {
GURL update_url;
if (!external_update_url.empty())
update_url = GURL(external_update_url);
else if (always_check_updates_)
update_url = extension_urls::GetWebstoreUpdateUrl();
if (update_url.is_valid())
downloader_->AddPendingExtension(it.key(), update_url, 0);
}
base::FilePath file_path;
std::string version;
if (local_cache_.GetExtension(it.key(), &file_path, &version)) {
// Copy entry to don't modify it inside extensions_.
base::DictionaryValue* entry_copy = entry->DeepCopy();
if (extension_urls::IsWebstoreUpdateUrl(GURL(external_update_url))) {
entry_copy->SetBoolean(
extensions::ExternalProviderImpl::kIsFromWebstore, true);
}
entry_copy->Remove(extensions::ExternalProviderImpl::kExternalUpdateUrl,
NULL);
entry_copy->SetString(extensions::ExternalProviderImpl::kExternalVersion,
version);
entry_copy->SetString(extensions::ExternalProviderImpl::kExternalCrx,
file_path.value());
cached_extensions_->Set(it.key(), entry_copy);
} else {
bool has_external_crx = entry->HasKey(
extensions::ExternalProviderImpl::kExternalCrx);
bool is_already_installed =
!delegate_->GetInstalledExtensionVersion(it.key()).empty();
if (keep_if_present || has_external_crx || is_already_installed) {
// Copy entry to don't modify it inside extensions_.
cached_extensions_->Set(it.key(), entry->DeepCopy());
}
}
}
if (downloader_)
downloader_->StartAllPending(NULL);
VLOG(1) << "Updated ExternalCache, there are "
<< cached_extensions_->size() << " extensions cached";
UpdateExtensionLoader();
}
void ExternalCache::OnPutExtension(const std::string& id,
const base::FilePath& file_path,
bool file_ownership_passed) {
if (local_cache_.is_shutdown() || file_ownership_passed) {
backend_task_runner_->PostTask(FROM_HERE,
base::Bind(base::IgnoreResult(&base::DeleteFile), file_path, true));
return;
}
VLOG(1) << "ExternalCache installed a new extension in the cache " << id;
base::DictionaryValue* entry = NULL;
if (!extensions_->GetDictionary(id, &entry)) {
LOG(ERROR) << "ExternalCache cannot find entry for extension " << id;
return;
}
// Copy entry to don't modify it inside extensions_.
entry = entry->DeepCopy();
std::string version;
if (!local_cache_.GetExtension(id, NULL, &version)) {
// Copy entry to don't modify it inside extensions_.
LOG(ERROR) << "Can't find installed extension in cache " << id;
return;
}
std::string update_url;
if (entry->GetString(extensions::ExternalProviderImpl::kExternalUpdateUrl,
&update_url) &&
extension_urls::IsWebstoreUpdateUrl(GURL(update_url))) {
entry->SetBoolean(extensions::ExternalProviderImpl::kIsFromWebstore, true);
}
entry->Remove(extensions::ExternalProviderImpl::kExternalUpdateUrl, NULL);
entry->SetString(extensions::ExternalProviderImpl::kExternalVersion, version);
entry->SetString(extensions::ExternalProviderImpl::kExternalCrx,
file_path.value());
cached_extensions_->Set(id, entry);
if (delegate_)
delegate_->OnExtensionLoadedInCache(id);
UpdateExtensionLoader();
}
void ExternalCache::OnPutExternalExtension(
const std::string& id,
const PutExternalExtensionCallback& callback,
const base::FilePath& file_path,
bool file_ownership_passed) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
OnPutExtension(id, file_path, file_ownership_passed);
callback.Run(id, !file_ownership_passed);
}
std::string ExternalCache::Delegate::GetInstalledExtensionVersion(
const std::string& id) {
return std::string();
}
} // namespace chromeos
|