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_
8 #include "chrome/browser/component_updater/crx_downloader.h"
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"
18 namespace component_updater {
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. Instances of this class are created and
24 // destroyed in the UI thread. See the implementation of the class destructor
25 // for details regarding the clean up of resources acquired in this class.
26 class BackgroundDownloader : public CrxDownloader {
28 friend class CrxDownloader;
29 BackgroundDownloader(scoped_ptr<CrxDownloader> successor,
30 net::URLRequestContextGetter* context_getter,
31 scoped_refptr<base::SequencedTaskRunner> task_runner,
32 const DownloadCallback& download_callback);
33 virtual ~BackgroundDownloader();
36 // Overrides for CrxDownloader.
37 virtual void DoStartDownload(const GURL& url) OVERRIDE;
39 // Called asynchronously on the FILE thread at different stages during
40 // the download. |OnDownloading| can be called multiple times.
41 // |EndDownload| switches the execution flow from the FILE to the UI thread.
42 // Accessing any data members of this object on the FILE thread after
43 // calling |EndDownload| is unsafe.
44 void BeginDownload(const GURL& url);
46 void EndDownload(HRESULT hr);
48 // Handles the job state transitions to a final state.
49 void OnStateTransferred();
51 void OnStateCancelled();
52 void OnStateAcknowledged();
54 // Handles the transition to a transient state where the job is in the
55 // queue but not actively transferring data.
58 // Handles the job state transition to a transient, non-final error state.
59 void OnStateTransientError();
61 // Handles the job state corresponding to transferring data.
62 void OnStateTransferring();
64 HRESULT QueueBitsJob(const GURL& url);
65 HRESULT CreateOrOpenJob(const GURL& url);
66 HRESULT InitializeNewJob(const GURL& url);
68 // Returns true if at the time of the call, it appears that the job
69 // has not been making progress toward completion.
72 net::URLRequestContextGetter* context_getter_;
73 scoped_refptr<base::SequencedTaskRunner> task_runner_;
75 // The timer and the BITS interface pointers have thread affinity. These
76 // members are initialized on the FILE thread and they must be destroyed
77 // on the FILE thread.
78 scoped_ptr<base::RepeatingTimer<BackgroundDownloader> > timer_;
80 base::win::ScopedComPtr<IBackgroundCopyManager> bits_manager_;
81 base::win::ScopedComPtr<IBackgroundCopyJob> job_;
83 // Contains the time when the download of the current url has started.
84 base::Time download_start_time_;
86 // Contains the time when the BITS job is last seen making progress.
87 base::Time job_stuck_begin_time_;
91 DISALLOW_COPY_AND_ASSIGN(BackgroundDownloader);
94 } // namespace component_updater
96 #endif // CHROME_BROWSER_COMPONENT_UPDATER_BACKGROUND_DOWNLOADER_WIN_H_