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 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_COMPONENT_UPDATER_BACKGROUND_DOWNLOADER_WIN_H_ 6 #define CHROME_BROWSER_COMPONENT_UPDATER_BACKGROUND_DOWNLOADER_WIN_H_ 7 8 #include "chrome/browser/component_updater/crx_downloader.h" 9 10 #include <windows.h> 11 #include <bits.h> 12 13 #include "base/strings/string16.h" 14 #include "base/time/time.h" 15 #include "base/timer/timer.h" 16 #include "base/win/scoped_comptr.h" 17 18 namespace component_updater { 19 20 // Implements a downloader in terms of the BITS service. The public interface 21 // of this class and the CrxDownloader overrides are expected to be called 22 // from the UI thread. The rest of the class code runs on the FILE thread in 23 // a single threaded apartment. 24 class BackgroundDownloader : public CrxDownloader { 25 protected: 26 friend class CrxDownloader; 27 BackgroundDownloader(scoped_ptr<CrxDownloader> successor, 28 net::URLRequestContextGetter* context_getter, 29 scoped_refptr<base::SequencedTaskRunner> task_runner, 30 const DownloadCallback& download_callback); 31 virtual ~BackgroundDownloader(); 32 33 private: 34 // Overrides for CrxDownloader. 35 virtual void DoStartDownload(const GURL& url) OVERRIDE; 36 37 // Called asynchronously on the FILE thread at different stages during 38 // the download. |OnDownloading| can be called multiple times. 39 // |EndDownload| switches the execution flow from the FILE to the UI thread. 40 // Accessing any data members of this object on the FILE thread after 41 // calling |EndDownload| is unsafe. 42 void BeginDownload(const GURL& url); 43 void OnDownloading(); 44 void EndDownload(HRESULT hr); 45 46 // Handles the job state transitions to a final state. 47 void OnStateTransferred(); 48 void OnStateError(); 49 void OnStateCancelled(); 50 void OnStateAcknowledged(); 51 52 // Handles the transition to a transient state where the job is in the 53 // queue but not actively transferring data. 54 void OnStateQueued(); 55 56 // Handles the job state transition to a transient, non-final error state. 57 void OnStateTransientError(); 58 59 // Handles the job state corresponding to transferring data. 60 void OnStateTransferring(); 61 62 HRESULT QueueBitsJob(const GURL& url); 63 HRESULT CreateOrOpenJob(const GURL& url); 64 HRESULT InitializeNewJob(const GURL& url); 65 66 // Returns true if at the time of the call, it appears that the job 67 // has not been making progress toward completion. 68 bool IsStuck(); 69 70 net::URLRequestContextGetter* context_getter_; 71 scoped_refptr<base::SequencedTaskRunner> task_runner_; 72 73 scoped_ptr<base::RepeatingTimer<BackgroundDownloader> > timer_; 74 75 base::win::ScopedComPtr<IBackgroundCopyManager> bits_manager_; 76 base::win::ScopedComPtr<IBackgroundCopyJob> job_; 77 78 // Contains the time when the download of the current url has started. 79 base::Time download_start_time_; 80 81 // Contains the time when the BITS job is last seen making progress. 82 base::Time job_stuck_begin_time_; 83 84 bool is_completed_; 85 86 DISALLOW_COPY_AND_ASSIGN(BackgroundDownloader); 87 }; 88 89 } // namespace component_updater 90 91 #endif // CHROME_BROWSER_COMPONENT_UPDATER_BACKGROUND_DOWNLOADER_WIN_H_ 92 93