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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
|
// 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/file_util.h"
#include "base/files/file_enumerator.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/chrome_notification_types.h"
#include "chrome/browser/extensions/crx_installer.h"
#include "chrome/browser/extensions/external_provider_impl.h"
#include "chrome/browser/extensions/updater/extension_downloader.h"
#include "chrome/common/extensions/extension_constants.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
#include "extensions/common/extension.h"
namespace chromeos {
namespace {
// File name extension for CRX files (not case sensitive).
const char kCRXFileExtension[] = ".crx";
// Delay between checks for flag file presence when waiting for the cache to
// become ready.
const int64_t kCacheStatusPollingDelayMs = 1000;
} // namespace
const char ExternalCache::kCacheReadyFlagFileName[] = ".initialized";
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)
: cache_dir_(cache_dir),
request_context_(request_context),
delegate_(delegate),
shutdown_(false),
always_check_updates_(always_check_updates),
wait_for_cache_initialization_(wait_for_cache_initialization),
cached_extensions_(new base::DictionaryValue()),
backend_task_runner_(backend_task_runner),
weak_ptr_factory_(this) {
notification_registrar_.Add(
this,
chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR,
content::NotificationService::AllBrowserContextsAndSources());
}
ExternalCache::~ExternalCache() {
}
void ExternalCache::Shutdown(const base::Closure& callback) {
DCHECK(!shutdown_);
shutdown_ = true;
backend_task_runner_->PostTask(FROM_HERE,
base::Bind(&ExternalCache::BackendShudown,
callback));
}
void ExternalCache::UpdateExtensionsList(
scoped_ptr<base::DictionaryValue> prefs) {
if (shutdown_)
return;
extensions_ = prefs.Pass();
if (extensions_->empty()) {
// Don't check cache and clear it if there are no extensions in the list.
// It is important case because login to supervised user shouldn't clear
// cache for normal users.
// TODO(dpolukhin): introduce reference counting to preserve cache elements
// when they are not needed for current user but needed for other users.
cached_extensions_->Clear();
UpdateExtensionLoader();
} else {
CheckCache();
}
}
void ExternalCache::OnDamagedFileDetected(const base::FilePath& path) {
if (shutdown_)
return;
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()) {
LOG(ERROR) << "ExternalCache extension at " << path.value()
<< " failed to install, deleting it.";
cached_extensions_->Remove(it.key(), NULL);
UpdateExtensionLoader();
// The file will be downloaded again on the next restart.
if (cache_dir_.IsParent(path)) {
backend_task_runner_->PostTask(
FROM_HERE,
base::Bind(base::IgnoreResult(base::DeleteFile),
path,
true));
}
// 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::Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
if (shutdown_)
return;
switch (type) {
case chrome::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 (shutdown_)
return;
if (error == NO_UPDATE_AVAILABLE) {
if (!cached_extensions_->HasKey(id)) {
LOG(ERROR) << "ExternalCache extension " << id
<< " not found on update server";
}
} else {
LOG(ERROR) << "ExternalCache failed to download extension " << id
<< ", error " << error;
}
}
void ExternalCache::OnExtensionDownloadFinished(
const std::string& id,
const base::FilePath& path,
const GURL& download_url,
const std::string& version,
const extensions::ExtensionDownloaderDelegate::PingResult& ping_result,
const std::set<int>& request_ids) {
if (shutdown_)
return;
backend_task_runner_->PostTask(
FROM_HERE,
base::Bind(&ExternalCache::BackendInstallCacheEntry,
weak_ptr_factory_.GetWeakPtr(),
cache_dir_,
id,
path,
version));
}
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) {
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() {
if (shutdown_)
return;
VLOG(1) << "Notify ExternalCache delegate about cache update";
if (delegate_)
delegate_->OnExtensionListsUpdated(cached_extensions_.get());
}
void ExternalCache::CheckCache() {
if (wait_for_cache_initialization_) {
backend_task_runner_->PostTask(
FROM_HERE,
base::Bind(&ExternalCache::BackendCheckCacheStatus,
weak_ptr_factory_.GetWeakPtr(),
cache_dir_));
} else {
CheckCacheContents();
}
}
// static
void ExternalCache::BackendCheckCacheStatus(
base::WeakPtr<ExternalCache> external_cache,
const base::FilePath& cache_dir) {
content::BrowserThread::PostTask(
content::BrowserThread::UI,
FROM_HERE,
base::Bind(&ExternalCache::OnCacheStatusChecked,
external_cache,
base::PathExists(cache_dir.AppendASCII(kCacheReadyFlagFileName))));
}
void ExternalCache::OnCacheStatusChecked(bool ready) {
if (shutdown_)
return;
if (ready) {
CheckCacheContents();
} else {
content::BrowserThread::PostDelayedTask(
content::BrowserThread::UI,
FROM_HERE,
base::Bind(&ExternalCache::CheckCache,
weak_ptr_factory_.GetWeakPtr()),
base::TimeDelta::FromMilliseconds(kCacheStatusPollingDelayMs));
}
}
void ExternalCache::CheckCacheContents() {
if (shutdown_)
return;
backend_task_runner_->PostTask(
FROM_HERE,
base::Bind(&ExternalCache::BackendCheckCacheContents,
weak_ptr_factory_.GetWeakPtr(),
cache_dir_,
base::Passed(make_scoped_ptr(extensions_->DeepCopy()))));
}
// static
void ExternalCache::BackendCheckCacheContents(
base::WeakPtr<ExternalCache> external_cache,
const base::FilePath& cache_dir,
scoped_ptr<base::DictionaryValue> prefs) {
BackendCheckCacheContentsInternal(cache_dir, prefs.get());
content::BrowserThread::PostTask(content::BrowserThread::UI,
FROM_HERE,
base::Bind(&ExternalCache::OnCacheUpdated,
external_cache,
base::Passed(&prefs)));
}
// static
void ExternalCache::BackendCheckCacheContentsInternal(
const base::FilePath& cache_dir,
base::DictionaryValue* prefs) {
// Start by verifying that the cache_dir exists.
if (!base::DirectoryExists(cache_dir)) {
// Create it now.
if (!base::CreateDirectory(cache_dir)) {
LOG(ERROR) << "Failed to create ExternalCache directory at "
<< cache_dir.value();
}
// Nothing else to do. Cache won't be used.
return;
}
// Enumerate all the files in the cache |cache_dir|, including directories
// and symlinks. Each unrecognized file will be erased.
int types = base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES |
base::FileEnumerator::SHOW_SYM_LINKS;
base::FileEnumerator enumerator(cache_dir, false /* recursive */, types);
for (base::FilePath path = enumerator.Next();
!path.empty(); path = enumerator.Next()) {
base::FileEnumerator::FileInfo info = enumerator.GetInfo();
std::string basename = path.BaseName().value();
if (info.IsDirectory() || file_util::IsLink(info.GetName())) {
LOG(ERROR) << "Erasing bad file in ExternalCache directory: " << basename;
base::DeleteFile(path, true /* recursive */);
continue;
}
// Skip flag file that indicates that cache is ready.
if (basename == kCacheReadyFlagFileName)
continue;
// crx files in the cache are named <extension-id>-<version>.crx.
std::string id;
std::string version;
if (EndsWith(basename, kCRXFileExtension, false /* case-sensitive */)) {
size_t n = basename.find('-');
if (n != std::string::npos && n + 1 < basename.size() - 4) {
id = basename.substr(0, n);
// Size of |version| = total size - "<id>" - "-" - ".crx"
version = basename.substr(n + 1, basename.size() - 5 - id.size());
}
}
base::DictionaryValue* entry = NULL;
if (!extensions::Extension::IdIsValid(id)) {
LOG(ERROR) << "Bad extension id in ExternalCache: " << id;
id.clear();
} else if (!prefs->GetDictionary(id, &entry)) {
LOG(WARNING) << basename << " is in the cache but is not configured by "
<< "the ExternalCache source, and will be erased.";
id.clear();
}
if (!Version(version).IsValid()) {
LOG(ERROR) << "Bad extension version in ExternalCache: " << version;
version.clear();
}
if (id.empty() || version.empty()) {
LOG(ERROR) << "Invalid file in ExternalCache, erasing: " << basename;
base::DeleteFile(path, true /* recursive */);
continue;
}
// Enforce a lower-case id.
id = StringToLowerASCII(id);
std::string update_url;
std::string prev_version_string;
std::string prev_crx;
if (entry->GetString(extensions::ExternalProviderImpl::kExternalUpdateUrl,
&update_url)) {
VLOG(1) << "ExternalCache found cached version " << version
<< " for extension id: " << id;
entry->Remove(extensions::ExternalProviderImpl::kExternalUpdateUrl, NULL);
entry->SetString(extensions::ExternalProviderImpl::kExternalVersion,
version);
entry->SetString(extensions::ExternalProviderImpl::kExternalCrx,
path.value());
if (extension_urls::IsWebstoreUpdateUrl(GURL(update_url))) {
entry->SetBoolean(extensions::ExternalProviderImpl::kIsFromWebstore,
true);
}
} else if (
entry->GetString(extensions::ExternalProviderImpl::kExternalVersion,
&prev_version_string) &&
entry->GetString(extensions::ExternalProviderImpl::kExternalCrx,
&prev_crx)) {
Version prev_version(prev_version_string);
Version curr_version(version);
DCHECK(prev_version.IsValid());
DCHECK(curr_version.IsValid());
if (prev_version.CompareTo(curr_version) <= 0) {
VLOG(1) << "ExternalCache found old cached version "
<< prev_version_string << " path: " << prev_crx;
base::FilePath prev_crx_file(prev_crx);
if (cache_dir.IsParent(prev_crx_file)) {
// Only delete old cached files under cache_dir_ folder.
base::DeleteFile(base::FilePath(prev_crx), true /* recursive */);
}
entry->SetString(extensions::ExternalProviderImpl::kExternalVersion,
version);
entry->SetString(extensions::ExternalProviderImpl::kExternalCrx,
path.value());
} else {
VLOG(1) << "ExternalCache found old cached version "
<< version << " path: " << path.value();
base::DeleteFile(path, true /* recursive */);
}
} else {
NOTREACHED() << "ExternalCache found bad entry for extension id: " << id
<< " file path: " << path.value();
}
}
}
void ExternalCache::OnCacheUpdated(scoped_ptr<base::DictionaryValue> prefs) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
if (shutdown_)
return;
// If request_context_ is missing we can't download anything.
if (!downloader_ && request_context_) {
downloader_.reset(
new extensions::ExtensionDownloader(this, request_context_));
}
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);
// Check for updates for all extensions configured except for extensions
// marked as keep_if_present.
if (downloader_ && !keep_if_present) {
GURL update_url;
std::string external_update_url;
if (entry->GetString(extensions::ExternalProviderImpl::kExternalUpdateUrl,
&external_update_url)) {
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::DictionaryValue* cached_entry = NULL;
if (prefs->GetDictionary(it.key(), &cached_entry)) {
bool has_external_crx = cached_entry->HasKey(
extensions::ExternalProviderImpl::kExternalCrx);
bool is_already_installed =
!delegate_->GetInstalledExtensionVersion(it.key()).empty();
if (!downloader_ || keep_if_present || has_external_crx ||
is_already_installed) {
scoped_ptr<base::Value> value;
prefs->Remove(it.key(), &value);
cached_extensions_->Set(it.key(), value.release());
}
}
}
if (downloader_)
downloader_->StartAllPending();
VLOG(1) << "Updated ExternalCache, there are "
<< cached_extensions_->size() << " extensions cached";
UpdateExtensionLoader();
}
// static
void ExternalCache::BackendInstallCacheEntry(
base::WeakPtr<ExternalCache> external_cache,
const base::FilePath& cache_dir,
const std::string& id,
const base::FilePath& path,
const std::string& version) {
Version version_validator(version);
if (!version_validator.IsValid()) {
LOG(ERROR) << "ExternalCache downloaded extension " << id << " but got bad "
<< "version: " << version;
base::DeleteFile(path, true /* recursive */);
return;
}
std::string basename = id + "-" + version + kCRXFileExtension;
base::FilePath cached_crx_path = cache_dir.Append(basename);
if (base::PathExists(cached_crx_path)) {
LOG(WARNING) << "AppPack downloaded a crx whose filename will overwrite "
<< "an existing cached crx.";
base::DeleteFile(cached_crx_path, true /* recursive */);
}
if (!base::DirectoryExists(cache_dir)) {
LOG(ERROR) << "AppPack cache directory does not exist, creating now: "
<< cache_dir.value();
if (!base::CreateDirectory(cache_dir)) {
LOG(ERROR) << "Failed to create the AppPack cache dir!";
base::DeleteFile(path, true /* recursive */);
return;
}
}
if (!base::Move(path, cached_crx_path)) {
LOG(ERROR) << "Failed to move AppPack crx from " << path.value()
<< " to " << cached_crx_path.value();
base::DeleteFile(path, true /* recursive */);
return;
}
content::BrowserThread::PostTask(
content::BrowserThread::UI,
FROM_HERE,
base::Bind(&ExternalCache::OnCacheEntryInstalled,
external_cache,
id,
cached_crx_path,
version));
}
void ExternalCache::OnCacheEntryInstalled(const std::string& id,
const base::FilePath& path,
const std::string& version) {
if (shutdown_)
return;
VLOG(1) << "AppPack installed a new extension in the cache: " << path.value();
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 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,
path.value());
cached_extensions_->Set(id, entry);
UpdateExtensionLoader();
}
// static
void ExternalCache::BackendShudown(const base::Closure& callback) {
content::BrowserThread::PostTask(content::BrowserThread::UI,
FROM_HERE,
callback);
}
std::string ExternalCache::Delegate::GetInstalledExtensionVersion(
const std::string& id) {
return std::string();
}
} // namespace chromeos
|