Home | History | Annotate | Download | only in component_updater
      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
      3 // found in the LICENSE file.
      4 
      5 #ifndef COMPONENTS_COMPONENT_UPDATER_BACKGROUND_DOWNLOADER_WIN_H_
      6 #define COMPONENTS_COMPONENT_UPDATER_BACKGROUND_DOWNLOADER_WIN_H_
      7 
      8 #include <windows.h>
      9 #include <bits.h>
     10 
     11 #include "base/macros.h"
     12 #include "base/memory/ref_counted.h"
     13 #include "base/strings/string16.h"
     14 #include "base/threading/thread_checker.h"
     15 #include "base/time/time.h"
     16 #include "base/timer/timer.h"
     17 #include "base/win/scoped_comptr.h"
     18 #include "components/component_updater/crx_downloader.h"
     19 
     20 namespace base {
     21 class FilePath;
     22 class MessageLoopProxy;
     23 class SingleThreadTaskRunner;
     24 }
     25 
     26 namespace component_updater {
     27 
     28 // Implements a downloader in terms of the BITS service. The public interface
     29 // of this class and the CrxDownloader overrides are expected to be called
     30 // from the main thread. The rest of the class code runs on a single thread
     31 // task runner. This task runner must be initialized to work with COM objects.
     32 // Instances of this class are created and destroyed in the main thread.
     33 // See the implementation of the class destructor for details regarding the
     34 // clean up of resources acquired in this class.
     35 class BackgroundDownloader : public CrxDownloader {
     36  protected:
     37   friend class CrxDownloader;
     38   BackgroundDownloader(scoped_ptr<CrxDownloader> successor,
     39                        net::URLRequestContextGetter* context_getter,
     40                        scoped_refptr<base::SingleThreadTaskRunner> task_runner);
     41   virtual ~BackgroundDownloader();
     42 
     43  private:
     44   // Overrides for CrxDownloader.
     45   virtual void DoStartDownload(const GURL& url) OVERRIDE;
     46 
     47   // Called asynchronously on the |task_runner_| at different stages during
     48   // the download. |OnDownloading| can be called multiple times.
     49   // |EndDownload| switches the execution flow from the |task_runner_| to the
     50   // main thread. Accessing any data members of this object from the
     51   // |task_runner_|after calling |EndDownload| is unsafe.
     52   void BeginDownload(const GURL& url);
     53   void OnDownloading();
     54   void EndDownload(HRESULT hr);
     55 
     56   // Handles the job state transitions to a final state.
     57   void OnStateTransferred();
     58   void OnStateError();
     59   void OnStateCancelled();
     60   void OnStateAcknowledged();
     61 
     62   // Handles the transition to a transient state where the job is in the
     63   // queue but not actively transferring data.
     64   void OnStateQueued();
     65 
     66   // Handles the job state transition to a transient, non-final error state.
     67   void OnStateTransientError();
     68 
     69   // Handles the job state corresponding to transferring data.
     70   void OnStateTransferring();
     71 
     72   HRESULT QueueBitsJob(const GURL& url);
     73   HRESULT CreateOrOpenJob(const GURL& url);
     74   HRESULT InitializeNewJob(const GURL& url);
     75 
     76   // Returns true if at the time of the call, it appears that the job
     77   // has not been making progress toward completion.
     78   bool IsStuck();
     79 
     80   // Makes the downloaded file available to the caller by renaming the
     81   // temporary file to its destination and removing it from the BITS queue.
     82   HRESULT CompleteJob();
     83 
     84   // Ensures that we are running on the same thread we created the object on.
     85   base::ThreadChecker thread_checker_;
     86 
     87   // Used to post responses back to the main thread. Initialized on the main
     88   // loop but accessed from the task runner.
     89   scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
     90 
     91   net::URLRequestContextGetter* context_getter_;
     92   scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
     93 
     94   // The timer and the BITS interface pointers have thread affinity. These
     95   // members are initialized on the task runner and they must be destroyed
     96   // on the task runner.
     97   scoped_ptr<base::RepeatingTimer<BackgroundDownloader> > timer_;
     98 
     99   base::win::ScopedComPtr<IBackgroundCopyManager> bits_manager_;
    100   base::win::ScopedComPtr<IBackgroundCopyJob> job_;
    101 
    102   // Contains the time when the download of the current url has started.
    103   base::Time download_start_time_;
    104 
    105   // Contains the time when the BITS job is last seen making progress.
    106   base::Time job_stuck_begin_time_;
    107 
    108   // True if EndDownload was called.
    109   bool is_completed_;
    110 
    111   // Contains the path of the downloaded file if the download was successful.
    112   base::FilePath response_;
    113 
    114   DISALLOW_COPY_AND_ASSIGN(BackgroundDownloader);
    115 };
    116 
    117 }  // namespace component_updater
    118 
    119 #endif  // COMPONENTS_COMPONENT_UPDATER_BACKGROUND_DOWNLOADER_WIN_H_
    120