Chromium Code Reviews| 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; | |
| 19 } | 22 } |
| 20 | 23 |
| 21 namespace component_updater { | 24 namespace component_updater { |
| 22 | 25 |
| 23 // Implements a downloader in terms of the BITS service. The public interface | 26 // 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 | 27 // 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 | 28 // 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 | 29 // 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 | 30 // 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. | 31 // See the implementation of the class destructor for details regarding the |
| 32 // clean up of resources acquired in this class. | |
| 29 class BackgroundDownloader : public CrxDownloader { | 33 class BackgroundDownloader : public CrxDownloader { |
| 30 protected: | 34 protected: |
| 31 friend class CrxDownloader; | 35 friend class CrxDownloader; |
| 32 BackgroundDownloader(scoped_ptr<CrxDownloader> successor, | 36 BackgroundDownloader(scoped_ptr<CrxDownloader> successor, |
| 33 net::URLRequestContextGetter* context_getter, | 37 net::URLRequestContextGetter* context_getter, |
| 34 scoped_refptr<base::SequencedTaskRunner> task_runner); | 38 scoped_refptr<base::SingleThreadTaskRunner> task_runner); |
|
blundell
2014/07/15 08:45:20
This could probably use a more descriptive name, e
tommycli
2014/07/15 18:58:25
Done. I tried |bits_task_runner|. I'll let Sorin s
| |
| 35 virtual ~BackgroundDownloader(); | 39 virtual ~BackgroundDownloader(); |
| 36 | 40 |
| 37 private: | 41 private: |
| 38 // Overrides for CrxDownloader. | 42 // Overrides for CrxDownloader. |
| 39 virtual void DoStartDownload(const GURL& url) OVERRIDE; | 43 virtual void DoStartDownload(const GURL& url) OVERRIDE; |
| 40 | 44 |
| 41 // Called asynchronously on the FILE thread at different stages during | 45 // Called asynchronously on the |task_runner_| at different stages during |
| 42 // the download. |OnDownloading| can be called multiple times. | 46 // the download. |OnDownloading| can be called multiple times. |
| 43 // |EndDownload| switches the execution flow from the FILE to the UI thread. | 47 // |EndDownload| switches the execution flow from the |task_runner_| to the |
| 44 // Accessing any data members of this object on the FILE thread after | 48 // main thread. Accessing any data members of this object from the |
| 45 // calling |EndDownload| is unsafe. | 49 // |task_runner_|after calling |EndDownload| is unsafe. |
| 46 void BeginDownload(const GURL& url); | 50 void BeginDownload(const GURL& url); |
| 47 void OnDownloading(); | 51 void OnDownloading(); |
| 48 void EndDownload(HRESULT hr); | 52 void EndDownload(HRESULT hr); |
| 49 | 53 |
| 50 // Handles the job state transitions to a final state. | 54 // Handles the job state transitions to a final state. |
| 51 void OnStateTransferred(); | 55 void OnStateTransferred(); |
| 52 void OnStateError(); | 56 void OnStateError(); |
| 53 void OnStateCancelled(); | 57 void OnStateCancelled(); |
| 54 void OnStateAcknowledged(); | 58 void OnStateAcknowledged(); |
| 55 | 59 |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 68 HRESULT InitializeNewJob(const GURL& url); | 72 HRESULT InitializeNewJob(const GURL& url); |
| 69 | 73 |
| 70 // Returns true if at the time of the call, it appears that the job | 74 // Returns true if at the time of the call, it appears that the job |
| 71 // has not been making progress toward completion. | 75 // has not been making progress toward completion. |
| 72 bool IsStuck(); | 76 bool IsStuck(); |
| 73 | 77 |
| 74 // Makes the downloaded file available to the caller by renaming the | 78 // Makes the downloaded file available to the caller by renaming the |
| 75 // temporary file to its destination and removing it from the BITS queue. | 79 // temporary file to its destination and removing it from the BITS queue. |
| 76 HRESULT CompleteJob(); | 80 HRESULT CompleteJob(); |
| 77 | 81 |
| 82 // Ensures that we are running on the same thread we created the object on. | |
| 83 base::ThreadChecker thread_checker_; | |
| 84 | |
| 85 // Used to post responses back to the main thread. Initialized on the main | |
| 86 // loop but accessed from the task runner. | |
| 87 scoped_refptr<base::MessageLoopProxy> main_loop_; | |
|
blundell
2014/07/15 08:45:20
Is there a reason this has to be a MLP rather than
tommycli
2014/07/15 18:58:25
Done.
| |
| 88 | |
| 78 net::URLRequestContextGetter* context_getter_; | 89 net::URLRequestContextGetter* context_getter_; |
| 79 scoped_refptr<base::SequencedTaskRunner> task_runner_; | 90 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 80 | 91 |
| 81 // The timer and the BITS interface pointers have thread affinity. These | 92 // The timer and the BITS interface pointers have thread affinity. These |
| 82 // members are initialized on the FILE thread and they must be destroyed | 93 // members are initialized on the task runner and they must be destroyed |
| 83 // on the FILE thread. | 94 // on the task runner. |
| 84 scoped_ptr<base::RepeatingTimer<BackgroundDownloader> > timer_; | 95 scoped_ptr<base::RepeatingTimer<BackgroundDownloader> > timer_; |
| 85 | 96 |
| 86 base::win::ScopedComPtr<IBackgroundCopyManager> bits_manager_; | 97 base::win::ScopedComPtr<IBackgroundCopyManager> bits_manager_; |
| 87 base::win::ScopedComPtr<IBackgroundCopyJob> job_; | 98 base::win::ScopedComPtr<IBackgroundCopyJob> job_; |
| 88 | 99 |
| 89 // Contains the time when the download of the current url has started. | 100 // Contains the time when the download of the current url has started. |
| 90 base::Time download_start_time_; | 101 base::Time download_start_time_; |
| 91 | 102 |
| 92 // Contains the time when the BITS job is last seen making progress. | 103 // Contains the time when the BITS job is last seen making progress. |
| 93 base::Time job_stuck_begin_time_; | 104 base::Time job_stuck_begin_time_; |
| 94 | 105 |
| 95 // True if EndDownload was called. | 106 // True if EndDownload was called. |
| 96 bool is_completed_; | 107 bool is_completed_; |
| 97 | 108 |
| 98 // Contains the path of the downloaded file if the download was successful. | 109 // Contains the path of the downloaded file if the download was successful. |
| 99 base::FilePath response_; | 110 base::FilePath response_; |
| 100 | 111 |
| 101 DISALLOW_COPY_AND_ASSIGN(BackgroundDownloader); | 112 DISALLOW_COPY_AND_ASSIGN(BackgroundDownloader); |
| 102 }; | 113 }; |
| 103 | 114 |
| 104 } // namespace component_updater | 115 } // namespace component_updater |
| 105 | 116 |
| 106 #endif // CHROME_BROWSER_COMPONENT_UPDATER_BACKGROUND_DOWNLOADER_WIN_H_ | 117 #endif // CHROME_BROWSER_COMPONENT_UPDATER_BACKGROUND_DOWNLOADER_WIN_H_ |
| OLD | NEW |