Upstream version 10.38.222.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 <windows.h>
9 #include <bits.h>
10
11 #include "base/memory/ref_counted.h"
12 #include "base/strings/string16.h"
13 #include "base/threading/thread_checker.h"
14 #include "base/time/time.h"
15 #include "base/timer/timer.h"
16 #include "base/win/scoped_comptr.h"
17 #include "chrome/browser/component_updater/crx_downloader.h"
18
19 namespace base {
20 class FilePath;
21 class MessageLoopProxy;
22 class SingleThreadTaskRunner;
23 }
24
25 namespace component_updater {
26
27 // Implements a downloader in terms of the BITS service. The public interface
28 // of this class and the CrxDownloader overrides are expected to be called
29 // from the main thread. The rest of the class code runs on a single thread
30 // task runner. This task runner must be initialized to work with COM objects.
31 // Instances of this class are created and destroyed in the main thread.
32 // See the implementation of the class destructor for details regarding the
33 // clean up of resources acquired in this class.
34 class BackgroundDownloader : public CrxDownloader {
35  protected:
36   friend class CrxDownloader;
37   BackgroundDownloader(scoped_ptr<CrxDownloader> successor,
38                        net::URLRequestContextGetter* context_getter,
39                        scoped_refptr<base::SingleThreadTaskRunner> task_runner);
40   virtual ~BackgroundDownloader();
41
42  private:
43   // Overrides for CrxDownloader.
44   virtual void DoStartDownload(const GURL& url) OVERRIDE;
45
46   // Called asynchronously on the |task_runner_| at different stages during
47   // the download. |OnDownloading| can be called multiple times.
48   // |EndDownload| switches the execution flow from the |task_runner_| to the
49   // main thread. Accessing any data members of this object from the
50   // |task_runner_|after calling |EndDownload| is unsafe.
51   void BeginDownload(const GURL& url);
52   void OnDownloading();
53   void EndDownload(HRESULT hr);
54
55   // Handles the job state transitions to a final state.
56   void OnStateTransferred();
57   void OnStateError();
58   void OnStateCancelled();
59   void OnStateAcknowledged();
60
61   // Handles the transition to a transient state where the job is in the
62   // queue but not actively transferring data.
63   void OnStateQueued();
64
65   // Handles the job state transition to a transient, non-final error state.
66   void OnStateTransientError();
67
68   // Handles the job state corresponding to transferring data.
69   void OnStateTransferring();
70
71   HRESULT QueueBitsJob(const GURL& url);
72   HRESULT CreateOrOpenJob(const GURL& url);
73   HRESULT InitializeNewJob(const GURL& url);
74
75   // Returns true if at the time of the call, it appears that the job
76   // has not been making progress toward completion.
77   bool IsStuck();
78
79   // Makes the downloaded file available to the caller by renaming the
80   // temporary file to its destination and removing it from the BITS queue.
81   HRESULT CompleteJob();
82
83   // Ensures that we are running on the same thread we created the object on.
84   base::ThreadChecker thread_checker_;
85
86   // Used to post responses back to the main thread. Initialized on the main
87   // loop but accessed from the task runner.
88   scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
89
90   net::URLRequestContextGetter* context_getter_;
91   scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
92
93   // The timer and the BITS interface pointers have thread affinity. These
94   // members are initialized on the task runner and they must be destroyed
95   // on the task runner.
96   scoped_ptr<base::RepeatingTimer<BackgroundDownloader> > timer_;
97
98   base::win::ScopedComPtr<IBackgroundCopyManager> bits_manager_;
99   base::win::ScopedComPtr<IBackgroundCopyJob> job_;
100
101   // Contains the time when the download of the current url has started.
102   base::Time download_start_time_;
103
104   // Contains the time when the BITS job is last seen making progress.
105   base::Time job_stuck_begin_time_;
106
107   // True if EndDownload was called.
108   bool is_completed_;
109
110   // Contains the path of the downloaded file if the download was successful.
111   base::FilePath response_;
112
113   DISALLOW_COPY_AND_ASSIGN(BackgroundDownloader);
114 };
115
116 }  // namespace component_updater
117
118 #endif  // CHROME_BROWSER_COMPONENT_UPDATER_BACKGROUND_DOWNLOADER_WIN_H_