| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/browser/content_hash_fetcher.h" | 5 #include "extensions/browser/content_hash_fetcher.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/files/file_enumerator.h" | 11 #include "base/files/file_enumerator.h" |
| 12 #include "base/json/json_reader.h" | 12 #include "base/json/json_reader.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/metrics/histogram.h" | 14 #include "base/metrics/histogram.h" |
| 15 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
| 16 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
| 17 #include "base/task_runner_util.h" | 17 #include "base/task_runner_util.h" |
| 18 #include "base/timer/elapsed_timer.h" | 18 #include "base/timer/elapsed_timer.h" |
| 19 #include "base/version.h" | 19 #include "base/version.h" |
| 20 #include "content/public/browser/browser_context.h" | 20 #include "content/public/browser/browser_context.h" |
| 21 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
| 22 #include "crypto/secure_hash.h" | 22 #include "crypto/secure_hash.h" |
| 23 #include "crypto/sha2.h" | 23 #include "crypto/sha2.h" |
| 24 #include "extensions/browser/computed_hashes.h" | 24 #include "extensions/browser/computed_hashes.h" |
| 25 #include "extensions/browser/content_hash_tree.h" | 25 #include "extensions/browser/content_hash_tree.h" |
| 26 #include "extensions/browser/content_verifier_delegate.h" |
| 26 #include "extensions/browser/extension_registry.h" | 27 #include "extensions/browser/extension_registry.h" |
| 27 #include "extensions/browser/verified_contents.h" | 28 #include "extensions/browser/verified_contents.h" |
| 28 #include "extensions/common/constants.h" | 29 #include "extensions/common/constants.h" |
| 29 #include "extensions/common/extension.h" | 30 #include "extensions/common/extension.h" |
| 30 #include "extensions/common/file_util.h" | 31 #include "extensions/common/file_util.h" |
| 31 #include "net/base/load_flags.h" | 32 #include "net/base/load_flags.h" |
| 32 #include "net/url_request/url_fetcher.h" | 33 #include "net/url_request/url_fetcher.h" |
| 33 #include "net/url_request/url_fetcher_delegate.h" | 34 #include "net/url_request/url_fetcher_delegate.h" |
| 34 #include "net/url_request/url_request_status.h" | 35 #include "net/url_request/url_request_status.h" |
| 35 | 36 |
| 36 namespace { | 37 namespace { |
| 37 | 38 |
| 38 typedef std::set<base::FilePath> SortedFilePathSet; | 39 typedef std::set<base::FilePath> SortedFilePathSet; |
| 39 | 40 |
| 40 } // namespace | 41 } // namespace |
| 41 | 42 |
| 42 namespace extensions { | 43 namespace extensions { |
| 43 | 44 |
| 44 // This class takes care of doing the disk and network I/O work to ensure we | 45 // This class takes care of doing the disk and network I/O work to ensure we |
| 45 // have both verified_contents.json files from the webstore and | 46 // have both verified_contents.json files from the webstore and |
| 46 // computed_hashes.json files computed over the files in an extension's | 47 // computed_hashes.json files computed over the files in an extension's |
| 47 // directory. | 48 // directory. |
| 48 class ContentHashFetcherJob | 49 class ContentHashFetcherJob |
| 49 : public base::RefCountedThreadSafe<ContentHashFetcherJob>, | 50 : public base::RefCountedThreadSafe<ContentHashFetcherJob>, |
| 50 public net::URLFetcherDelegate { | 51 public net::URLFetcherDelegate { |
| 51 public: | 52 public: |
| 52 typedef base::Callback<void(ContentHashFetcherJob*)> CompletionCallback; | 53 typedef base::Callback<void(ContentHashFetcherJob*)> CompletionCallback; |
| 53 ContentHashFetcherJob(net::URLRequestContextGetter* request_context, | 54 ContentHashFetcherJob(net::URLRequestContextGetter* request_context, |
| 54 ContentVerifierKey key, | 55 const ContentVerifierKey& key, |
| 55 const std::string& extension_id, | 56 const std::string& extension_id, |
| 56 const base::FilePath& extension_path, | 57 const base::FilePath& extension_path, |
| 57 const GURL& fetch_url, | 58 const GURL& fetch_url, |
| 58 bool force, | 59 bool force, |
| 59 const CompletionCallback& callback); | 60 const CompletionCallback& callback); |
| 60 | 61 |
| 61 void Start(); | 62 void Start(); |
| 62 | 63 |
| 63 // Cancels this job, which will attempt to stop I/O operations sooner than | 64 // Cancels this job, which will attempt to stop I/O operations sooner than |
| 64 // just waiting for the entire job to complete. Safe to call from any thread. | 65 // just waiting for the entire job to complete. Safe to call from any thread. |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 bool cancelled_; | 150 bool cancelled_; |
| 150 | 151 |
| 151 // A lock for synchronizing access to |cancelled_|. | 152 // A lock for synchronizing access to |cancelled_|. |
| 152 base::Lock cancelled_lock_; | 153 base::Lock cancelled_lock_; |
| 153 | 154 |
| 154 DISALLOW_COPY_AND_ASSIGN(ContentHashFetcherJob); | 155 DISALLOW_COPY_AND_ASSIGN(ContentHashFetcherJob); |
| 155 }; | 156 }; |
| 156 | 157 |
| 157 ContentHashFetcherJob::ContentHashFetcherJob( | 158 ContentHashFetcherJob::ContentHashFetcherJob( |
| 158 net::URLRequestContextGetter* request_context, | 159 net::URLRequestContextGetter* request_context, |
| 159 ContentVerifierKey key, | 160 const ContentVerifierKey& key, |
| 160 const std::string& extension_id, | 161 const std::string& extension_id, |
| 161 const base::FilePath& extension_path, | 162 const base::FilePath& extension_path, |
| 162 const GURL& fetch_url, | 163 const GURL& fetch_url, |
| 163 bool force, | 164 bool force, |
| 164 const CompletionCallback& callback) | 165 const CompletionCallback& callback) |
| 165 : request_context_(request_context), | 166 : request_context_(request_context), |
| 166 extension_id_(extension_id), | 167 extension_id_(extension_id), |
| 167 extension_path_(extension_path), | 168 extension_path_(extension_path), |
| 168 fetch_url_(fetch_url), | 169 fetch_url_(fetch_url), |
| 169 force_(force), | 170 force_(force), |
| (...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 412 } | 413 } |
| 413 | 414 |
| 414 // ---- | 415 // ---- |
| 415 | 416 |
| 416 ContentHashFetcher::ContentHashFetcher(content::BrowserContext* context, | 417 ContentHashFetcher::ContentHashFetcher(content::BrowserContext* context, |
| 417 ContentVerifierDelegate* delegate, | 418 ContentVerifierDelegate* delegate, |
| 418 const FetchCallback& callback) | 419 const FetchCallback& callback) |
| 419 : context_(context), | 420 : context_(context), |
| 420 delegate_(delegate), | 421 delegate_(delegate), |
| 421 fetch_callback_(callback), | 422 fetch_callback_(callback), |
| 422 observer_(this), | |
| 423 weak_ptr_factory_(this) { | 423 weak_ptr_factory_(this) { |
| 424 } | 424 } |
| 425 | 425 |
| 426 ContentHashFetcher::~ContentHashFetcher() { | 426 ContentHashFetcher::~ContentHashFetcher() { |
| 427 for (JobMap::iterator i = jobs_.begin(); i != jobs_.end(); ++i) { | 427 for (JobMap::iterator i = jobs_.begin(); i != jobs_.end(); ++i) { |
| 428 i->second->Cancel(); | 428 i->second->Cancel(); |
| 429 } | 429 } |
| 430 } | 430 } |
| 431 | 431 |
| 432 void ContentHashFetcher::Start() { | |
| 433 ExtensionRegistry* registry = ExtensionRegistry::Get(context_); | |
| 434 observer_.Add(registry); | |
| 435 } | |
| 436 | |
| 437 void ContentHashFetcher::DoFetch(const Extension* extension, bool force) { | 432 void ContentHashFetcher::DoFetch(const Extension* extension, bool force) { |
| 438 DCHECK(extension); | 433 DCHECK(extension); |
| 439 | 434 |
| 440 IdAndVersion key(extension->id(), extension->version()->GetString()); | 435 IdAndVersion key(extension->id(), extension->version()->GetString()); |
| 441 JobMap::iterator found = jobs_.find(key); | 436 JobMap::iterator found = jobs_.find(key); |
| 442 if (found != jobs_.end()) { | 437 if (found != jobs_.end()) { |
| 443 if (!force || found->second->force()) { | 438 if (!force || found->second->force()) { |
| 444 // Just let the existing job keep running. | 439 // Just let the existing job keep running. |
| 445 return; | 440 return; |
| 446 } else { | 441 } else { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 464 extension->id(), | 459 extension->id(), |
| 465 extension->path(), | 460 extension->path(), |
| 466 url, | 461 url, |
| 467 force, | 462 force, |
| 468 base::Bind(&ContentHashFetcher::JobFinished, | 463 base::Bind(&ContentHashFetcher::JobFinished, |
| 469 weak_ptr_factory_.GetWeakPtr())); | 464 weak_ptr_factory_.GetWeakPtr())); |
| 470 jobs_.insert(std::make_pair(key, job)); | 465 jobs_.insert(std::make_pair(key, job)); |
| 471 job->Start(); | 466 job->Start(); |
| 472 } | 467 } |
| 473 | 468 |
| 474 void ContentHashFetcher::OnExtensionLoaded( | 469 void ContentHashFetcher::ExtensionLoaded(const Extension* extension) { |
| 475 content::BrowserContext* browser_context, | |
| 476 const Extension* extension) { | |
| 477 CHECK(extension); | 470 CHECK(extension); |
| 478 DoFetch(extension, false); | 471 DoFetch(extension, false); |
| 479 } | 472 } |
| 480 | 473 |
| 481 void ContentHashFetcher::OnExtensionUnloaded( | 474 void ContentHashFetcher::ExtensionUnloaded(const Extension* extension) { |
| 482 content::BrowserContext* browser_context, | |
| 483 const Extension* extension, | |
| 484 UnloadedExtensionInfo::Reason reason) { | |
| 485 CHECK(extension); | 475 CHECK(extension); |
| 486 IdAndVersion key(extension->id(), extension->version()->GetString()); | 476 IdAndVersion key(extension->id(), extension->version()->GetString()); |
| 487 JobMap::iterator found = jobs_.find(key); | 477 JobMap::iterator found = jobs_.find(key); |
| 488 if (found != jobs_.end()) { | 478 if (found != jobs_.end()) { |
| 489 found->second->Cancel(); | 479 found->second->Cancel(); |
| 490 jobs_.erase(found); | 480 jobs_.erase(found); |
| 491 } | 481 } |
| 492 } | 482 } |
| 493 | 483 |
| 494 void ContentHashFetcher::JobFinished(ContentHashFetcherJob* job) { | 484 void ContentHashFetcher::JobFinished(ContentHashFetcherJob* job) { |
| 495 if (!job->IsCancelled()) { | 485 if (!job->IsCancelled()) { |
| 496 fetch_callback_.Run(job->extension_id(), | 486 fetch_callback_.Run(job->extension_id(), |
| 497 job->success(), | 487 job->success(), |
| 498 job->force(), | 488 job->force(), |
| 499 job->hash_mismatch_paths()); | 489 job->hash_mismatch_paths()); |
| 500 } | 490 } |
| 501 | 491 |
| 502 for (JobMap::iterator i = jobs_.begin(); i != jobs_.end(); ++i) { | 492 for (JobMap::iterator i = jobs_.begin(); i != jobs_.end(); ++i) { |
| 503 if (i->second.get() == job) { | 493 if (i->second.get() == job) { |
| 504 jobs_.erase(i); | 494 jobs_.erase(i); |
| 505 break; | 495 break; |
| 506 } | 496 } |
| 507 } | 497 } |
| 508 } | 498 } |
| 509 | 499 |
| 510 } // namespace extensions | 500 } // namespace extensions |
| OLD | NEW |