Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / component_updater / background_downloader_win.h
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. 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 {
27  protected:
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();
34
35  private:
36   // Overrides for CrxDownloader.
37   virtual void DoStartDownload(const GURL& url) OVERRIDE;
38
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);
45   void OnDownloading();
46   void EndDownload(HRESULT hr);
47
48   // Handles the job state transitions to a final state.
49   void OnStateTransferred();
50   void OnStateError();
51   void OnStateCancelled();
52   void OnStateAcknowledged();
53
54   // Handles the transition to a transient state where the job is in the
55   // queue but not actively transferring data.
56   void OnStateQueued();
57
58   // Handles the job state transition to a transient, non-final error state.
59   void OnStateTransientError();
60
61   // Handles the job state corresponding to transferring data.
62   void OnStateTransferring();
63
64   HRESULT QueueBitsJob(const GURL& url);
65   HRESULT CreateOrOpenJob(const GURL& url);
66   HRESULT InitializeNewJob(const GURL& url);
67
68   // Returns true if at the time of the call, it appears that the job
69   // has not been making progress toward completion.
70   bool IsStuck();
71
72   net::URLRequestContextGetter* context_getter_;
73   scoped_refptr<base::SequencedTaskRunner> task_runner_;
74
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_;
79
80   base::win::ScopedComPtr<IBackgroundCopyManager> bits_manager_;
81   base::win::ScopedComPtr<IBackgroundCopyJob> job_;
82
83   // Contains the time when the download of the current url has started.
84   base::Time download_start_time_;
85
86   // Contains the time when the BITS job is last seen making progress.
87   base::Time job_stuck_begin_time_;
88
89   bool is_completed_;
90
91   DISALLOW_COPY_AND_ASSIGN(BackgroundDownloader);
92 };
93
94 }  // namespace component_updater
95
96 #endif  // CHROME_BROWSER_COMPONENT_UPDATER_BACKGROUND_DOWNLOADER_WIN_H_
97