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