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.
5 #ifndef CHROME_BROWSER_COMPONENT_UPDATER_BACKGROUND_DOWNLOADER_WIN_H_
6 #define CHROME_BROWSER_COMPONENT_UPDATER_BACKGROUND_DOWNLOADER_WIN_H_
11 #include "base/strings/string16.h"
12 #include "base/time/time.h"
13 #include "base/timer/timer.h"
14 #include "base/win/scoped_comptr.h"
15 #include "chrome/browser/component_updater/crx_downloader.h"
21 namespace component_updater {
23 // 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
25 // from the UI thread. The rest of the class code runs on the FILE thread in
26 // a single threaded apartment. Instances of this class are created and
27 // destroyed in the UI thread. See the implementation of the class destructor
28 // for details regarding the clean up of resources acquired in this class.
29 class BackgroundDownloader : public CrxDownloader {
31 friend class CrxDownloader;
32 BackgroundDownloader(scoped_ptr<CrxDownloader> successor,
33 net::URLRequestContextGetter* context_getter,
34 scoped_refptr<base::SequencedTaskRunner> task_runner);
35 virtual ~BackgroundDownloader();
38 // Overrides for CrxDownloader.
39 virtual void DoStartDownload(const GURL& url) OVERRIDE;
41 // Called asynchronously on the FILE thread at different stages during
42 // the download. |OnDownloading| can be called multiple times.
43 // |EndDownload| switches the execution flow from the FILE to the UI thread.
44 // Accessing any data members of this object on the FILE thread after
45 // calling |EndDownload| is unsafe.
46 void BeginDownload(const GURL& url);
48 void EndDownload(HRESULT hr);
50 // Handles the job state transitions to a final state.
51 void OnStateTransferred();
53 void OnStateCancelled();
54 void OnStateAcknowledged();
56 // Handles the transition to a transient state where the job is in the
57 // queue but not actively transferring data.
60 // Handles the job state transition to a transient, non-final error state.
61 void OnStateTransientError();
63 // Handles the job state corresponding to transferring data.
64 void OnStateTransferring();
66 HRESULT QueueBitsJob(const GURL& url);
67 HRESULT CreateOrOpenJob(const GURL& url);
68 HRESULT InitializeNewJob(const GURL& url);
70 // Returns true if at the time of the call, it appears that the job
71 // has not been making progress toward completion.
74 // Makes the downloaded file available to the caller by renaming the
75 // temporary file to its destination and removing it from the BITS queue.
76 HRESULT CompleteJob();
78 net::URLRequestContextGetter* context_getter_;
79 scoped_refptr<base::SequencedTaskRunner> task_runner_;
81 // The timer and the BITS interface pointers have thread affinity. These
82 // members are initialized on the FILE thread and they must be destroyed
83 // on the FILE thread.
84 scoped_ptr<base::RepeatingTimer<BackgroundDownloader> > timer_;
86 base::win::ScopedComPtr<IBackgroundCopyManager> bits_manager_;
87 base::win::ScopedComPtr<IBackgroundCopyJob> job_;
89 // Contains the time when the download of the current url has started.
90 base::Time download_start_time_;
92 // Contains the time when the BITS job is last seen making progress.
93 base::Time job_stuck_begin_time_;
95 // True if EndDownload was called.
98 // Contains the path of the downloaded file if the download was successful.
99 base::FilePath response_;
101 DISALLOW_COPY_AND_ASSIGN(BackgroundDownloader);
104 } // namespace component_updater
106 #endif // CHROME_BROWSER_COMPONENT_UPDATER_BACKGROUND_DOWNLOADER_WIN_H_