Use kDefaultURL throughout SpdySessionTest.
[chromium-blink-merge.git] / components / update_client / background_downloader_win.h
blob59113f4bbfbe49c81c6b62793acc68566863104d
1 // Copyright 2014 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 COMPONENTS_UPDATE_CLIENT_BACKGROUND_DOWNLOADER_WIN_H_
6 #define COMPONENTS_UPDATE_CLIENT_BACKGROUND_DOWNLOADER_WIN_H_
8 #include <windows.h>
9 #include <bits.h>
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/strings/string16.h"
14 #include "base/threading/thread_checker.h"
15 #include "base/time/time.h"
16 #include "base/timer/timer.h"
17 #include "base/win/scoped_comptr.h"
18 #include "components/update_client/crx_downloader.h"
20 namespace base {
21 class FilePath;
22 class SingleThreadTaskRunner;
25 namespace update_client {
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 ~BackgroundDownloader() override;
42 private:
43 // Overrides for CrxDownloader.
44 void DoStartDownload(const GURL& url) override;
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);
55 // Handles the job state transitions to a final state.
56 void OnStateTransferred();
57 void OnStateError();
58 void OnStateCancelled();
59 void OnStateAcknowledged();
61 // Handles the transition to a transient state where the job is in the
62 // queue but not actively transferring data.
63 void OnStateQueued();
65 // Handles the job state transition to a transient, non-final error state.
66 void OnStateTransientError();
68 // Handles the job state corresponding to transferring data.
69 void OnStateTransferring();
71 HRESULT QueueBitsJob(const GURL& url);
72 HRESULT CreateOrOpenJob(const GURL& url);
73 HRESULT InitializeNewJob(const GURL& url);
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();
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();
83 // Ensures that we are running on the same thread we created the object on.
84 base::ThreadChecker thread_checker_;
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_;
90 net::URLRequestContextGetter* context_getter_;
91 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
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_;
98 base::win::ScopedComPtr<IBackgroundCopyManager> bits_manager_;
99 base::win::ScopedComPtr<IBackgroundCopyJob> job_;
101 // Contains the time when the download of the current url has started.
102 base::Time download_start_time_;
104 // Contains the time when the BITS job is last seen making progress.
105 base::Time job_stuck_begin_time_;
107 // True if EndDownload was called.
108 bool is_completed_;
110 // Contains the path of the downloaded file if the download was successful.
111 base::FilePath response_;
113 DISALLOW_COPY_AND_ASSIGN(BackgroundDownloader);
116 } // namespace update_client
118 #endif // COMPONENTS_UPDATE_CLIENT_BACKGROUND_DOWNLOADER_WIN_H_