Componentize component_updater: Replace content::BrowserThread usage with task runners

This patch strips out usage of specific threads defined in content/.

- Replaces FILE thread in BackgroundDownloaderWin with a SingleThreadTaskRunner backed by the FILE thread.
- Replaces a bunch of DCHECK(UI thread) with use of base::ThreadChecker (which just checks that it's all on the constructor thread).
- Replaces Posts to the UI thread with a post to the current message loop used in the constructor.

BUG=371463

Review URL: https://codereview.chromium.org/385013002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@284960 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/chrome/browser/component_updater/component_updater_service.cc b/chrome/browser/component_updater/component_updater_service.cc
index 3fa2aca..faecf0b 100644
--- a/chrome/browser/component_updater/component_updater_service.cc
+++ b/chrome/browser/component_updater/component_updater_service.cc
@@ -19,6 +19,7 @@
 #include "base/sequenced_task_runner.h"
 #include "base/stl_util.h"
 #include "base/threading/sequenced_worker_pool.h"
+#include "base/threading/thread_checker.h"
 #include "base/timer/timer.h"
 #include "chrome/browser/browser_process.h"
 #include "chrome/browser/component_updater/component_unpacker.h"
@@ -139,7 +140,7 @@
 // the tasks. Also when we do network requests there is only one |url_fetcher_|
 // in flight at a time.
 // There are no locks in this code, the main structure |work_items_| is mutated
-// only from the UI thread. The unpack and installation is done in a blocking
+// only from the main thread. The unpack and installation is done in a blocking
 // pool thread. The network requests are done in the IO thread or in the file
 // thread.
 class CrxUpdateService : public ComponentUpdateService, public OnDemandUpdater {
@@ -157,6 +158,8 @@
   virtual bool GetComponentDetails(const std::string& component_id,
                                    CrxUpdateItem* item) const OVERRIDE;
   virtual OnDemandUpdater& GetOnDemandUpdater() OVERRIDE;
+  virtual scoped_refptr<base::SequencedTaskRunner> GetSequencedTaskRunner()
+      OVERRIDE;
 
   // Overrides for OnDemandUpdater.
   virtual content::ResourceThrottle* GetOnDemandResourceThrottle(
@@ -260,6 +263,8 @@
 
   base::OneShotTimer<CrxUpdateService> timer_;
 
+  base::ThreadChecker thread_checker_;
+
   scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
 
   bool running_;
@@ -274,16 +279,12 @@
 CrxUpdateService::CrxUpdateService(Configurator* config)
     : config_(config),
       ping_manager_(new PingManager(*config)),
-      blocking_task_runner_(
-          BrowserThread::GetBlockingPool()->
-              GetSequencedTaskRunnerWithShutdownBehavior(
-                  BrowserThread::GetBlockingPool()->GetSequenceToken(),
-                  base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)),
+      blocking_task_runner_(config->GetSequencedTaskRunner()),
       running_(false) {
 }
 
 CrxUpdateService::~CrxUpdateService() {
-  // Because we are a singleton, at this point only the UI thread should be
+  // Because we are a singleton, at this point only the main thread should be
   // alive, this simplifies the management of the work that could be in
   // flight in other threads.
   Stop();
@@ -291,12 +292,12 @@
 }
 
 void CrxUpdateService::AddObserver(Observer* observer) {
-  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+  DCHECK(thread_checker_.CalledOnValidThread());
   observer_list_.AddObserver(observer);
 }
 
 void CrxUpdateService::RemoveObserver(Observer* observer) {
-  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+  DCHECK(thread_checker_.CalledOnValidThread());
   observer_list_.RemoveObserver(observer);
 }
 
@@ -348,7 +349,7 @@
 //  - a long delay when a full check/update cycle has completed for all
 //    components.
 void CrxUpdateService::ScheduleNextRun(StepDelayInterval step_delay) {
-  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+  DCHECK(thread_checker_.CalledOnValidThread());
   DCHECK(!update_checker_);
   CHECK(!timer_.IsRunning());
   // It could be the case that Stop() had been called while a url request
@@ -394,7 +395,7 @@
 // Given a extension-like component id, find the associated component.
 CrxUpdateItem* CrxUpdateService::FindUpdateItemById(
     const std::string& id) const {
-  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+  DCHECK(thread_checker_.CalledOnValidThread());
   CrxUpdateItem::FindById finder(id);
   UpdateItems::const_iterator it =
       std::find_if(work_items_.begin(), work_items_.end(), finder);
@@ -407,7 +408,7 @@
 // TODO(waffles): Do we want to add DCHECKS for valid state transitions here?
 void CrxUpdateService::ChangeItemState(CrxUpdateItem* item,
                                        CrxUpdateItem::Status to) {
-  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+  DCHECK(thread_checker_.CalledOnValidThread());
   if (to == CrxUpdateItem::kNoUpdate || to == CrxUpdateItem::kUpdated ||
       to == CrxUpdateItem::kUpToDate) {
     item->on_demand = false;
@@ -450,7 +451,7 @@
 // |to| status and returns how many have been changed.
 size_t CrxUpdateService::ChangeItemStatus(CrxUpdateItem::Status from,
                                           CrxUpdateItem::Status to) {
-  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+  DCHECK(thread_checker_.CalledOnValidThread());
   size_t count = 0;
   for (UpdateItems::iterator it = work_items_.begin();
        it != work_items_.end();
@@ -468,7 +469,7 @@
 // it will be replaced and the return code is kReplaced.
 ComponentUpdateService::Status CrxUpdateService::RegisterComponent(
     const CrxComponent& component) {
-  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+  DCHECK(thread_checker_.CalledOnValidThread());
   if (component.pk_hash.empty() || !component.version.IsValid() ||
       !component.installer)
     return kError;
@@ -507,7 +508,7 @@
 }
 
 std::vector<std::string> CrxUpdateService::GetComponentIDs() const {
-  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+  DCHECK(thread_checker_.CalledOnValidThread());
   std::vector<std::string> component_ids;
   for (UpdateItems::const_iterator it = work_items_.begin();
        it != work_items_.end();
@@ -520,7 +521,7 @@
 
 bool CrxUpdateService::GetComponentDetails(const std::string& component_id,
                                            CrxUpdateItem* item) const {
-  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+  DCHECK(thread_checker_.CalledOnValidThread());
   const CrxUpdateItem* crx_update_item(FindUpdateItemById(component_id));
   if (crx_update_item)
     *item = *crx_update_item;
@@ -531,11 +532,16 @@
   return *this;
 }
 
+scoped_refptr<base::SequencedTaskRunner>
+CrxUpdateService::GetSequencedTaskRunner() {
+  return config_->GetSequencedTaskRunner();
+}
+
 // This is the main loop of the component updater. It updates one component
 // at a time if updates are available. Otherwise, it does an update check or
 // takes a long sleep until the loop runs again.
 void CrxUpdateService::ProcessPendingItems() {
-  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+  DCHECK(thread_checker_.CalledOnValidThread());
 
   CrxUpdateItem* ready_upgrade = FindReadyComponent();
   if (ready_upgrade) {
@@ -655,9 +661,11 @@
                                       allow_background_download &&
                                       config_->UseBackgroundDownloader();
 
-  crx_downloader_.reset(CrxDownloader::Create(is_background_download,
-                                              config_->RequestContext(),
-                                              blocking_task_runner_));
+  crx_downloader_.reset(
+      CrxDownloader::Create(is_background_download,
+                            config_->RequestContext(),
+                            blocking_task_runner_,
+                            config_->GetSingleThreadTaskRunner()));
   crx_downloader_->set_progress_callback(
       base::Bind(&CrxUpdateService::DownloadProgress,
                  base::Unretained(this),
@@ -672,7 +680,7 @@
     int error,
     const std::string& error_message,
     const UpdateResponse::Results& results) {
-  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+  DCHECK(thread_checker_.CalledOnValidThread());
   update_checker_.reset();
   if (!error)
     OnUpdateCheckSucceeded(results);
@@ -688,7 +696,7 @@
 void CrxUpdateService::OnUpdateCheckSucceeded(
     const UpdateResponse::Results& results) {
   size_t num_updates_pending = 0;
-  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+  DCHECK(thread_checker_.CalledOnValidThread());
   VLOG(1) << "Update check succeeded.";
   std::vector<UpdateResponse::Result>::const_iterator it;
   for (it = results.list.begin(); it != results.list.end(); ++it) {
@@ -768,7 +776,7 @@
 
 void CrxUpdateService::OnUpdateCheckFailed(int error,
                                            const std::string& error_message) {
-  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+  DCHECK(thread_checker_.CalledOnValidThread());
   DCHECK(error);
   size_t count =
       ChangeItemStatus(CrxUpdateItem::kChecking, CrxUpdateItem::kNoUpdate);
@@ -783,7 +791,7 @@
 void CrxUpdateService::DownloadProgress(
     const std::string& component_id,
     const CrxDownloader::Result& download_result) {
-  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+  DCHECK(thread_checker_.CalledOnValidThread());
   NotifyObservers(Observer::COMPONENT_UPDATE_DOWNLOADING, component_id);
 }
 
@@ -793,7 +801,7 @@
 void CrxUpdateService::DownloadComplete(
     scoped_ptr<CRXContext> crx_context,
     const CrxDownloader::Result& download_result) {
-  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+  DCHECK(thread_checker_.CalledOnValidThread());
 
   CrxUpdateItem* crx = FindUpdateItemById(crx_context->id);
   DCHECK(crx->status == CrxUpdateItem::kDownloadingDiff ||
@@ -894,7 +902,7 @@
 void CrxUpdateService::DoneInstalling(const std::string& component_id,
                                       ComponentUnpacker::Error error,
                                       int extra_code) {
-  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+  DCHECK(thread_checker_.CalledOnValidThread());
 
   ErrorCategory error_category = kErrorNone;
   switch (error) {
@@ -942,7 +950,7 @@
 
 void CrxUpdateService::NotifyObservers(Observer::Events event,
                                        const std::string& id) {
-  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+  DCHECK(thread_checker_.CalledOnValidThread());
   FOR_EACH_OBSERVER(Observer, observer_list_, OnEvent(event, id));
 }
 
@@ -965,7 +973,7 @@
 void CrxUpdateService::OnNewResourceThrottle(
     base::WeakPtr<CUResourceThrottle> rt,
     const std::string& crx_id) {
-  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+  DCHECK(thread_checker_.CalledOnValidThread());
   // Check if we can on-demand update, else unblock the request anyway.
   CrxUpdateItem* item = FindUpdateItemById(crx_id);
   Status status = OnDemandUpdateWithCooldown(item);