WebSocket header continuations test case.
[chromium-blink-merge.git] / components / update_client / background_downloader_win.h
blob48ce6ddecf587b075bc3c169f8db64a7e435e226
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 MessageLoopProxy;
23 class SingleThreadTaskRunner;
26 namespace update_client {
28 // Implements a downloader in terms of the BITS service. The public interface
29 // of this class and the CrxDownloader overrides are expected to be called
30 // from the main thread. The rest of the class code runs on a single thread
31 // task runner. This task runner must be initialized to work with COM objects.
32 // Instances of this class are created and destroyed in the main thread.
33 // See the implementation of the class destructor for details regarding the
34 // clean up of resources acquired in this class.
35 class BackgroundDownloader : public CrxDownloader {
36 protected:
37 friend class CrxDownloader;
38 BackgroundDownloader(scoped_ptr<CrxDownloader> successor,
39 net::URLRequestContextGetter* context_getter,
40 scoped_refptr<base::SingleThreadTaskRunner> task_runner);
41 virtual ~BackgroundDownloader();
43 private:
44 // Overrides for CrxDownloader.
45 virtual void DoStartDownload(const GURL& url) override;
47 // Called asynchronously on the |task_runner_| at different stages during
48 // the download. |OnDownloading| can be called multiple times.
49 // |EndDownload| switches the execution flow from the |task_runner_| to the
50 // main thread. Accessing any data members of this object from the
51 // |task_runner_|after calling |EndDownload| is unsafe.
52 void BeginDownload(const GURL& url);
53 void OnDownloading();
54 void EndDownload(HRESULT hr);
56 // Handles the job state transitions to a final state.
57 void OnStateTransferred();
58 void OnStateError();
59 void OnStateCancelled();
60 void OnStateAcknowledged();
62 // Handles the transition to a transient state where the job is in the
63 // queue but not actively transferring data.
64 void OnStateQueued();
66 // Handles the job state transition to a transient, non-final error state.
67 void OnStateTransientError();
69 // Handles the job state corresponding to transferring data.
70 void OnStateTransferring();
72 HRESULT QueueBitsJob(const GURL& url);
73 HRESULT CreateOrOpenJob(const GURL& url);
74 HRESULT InitializeNewJob(const GURL& url);
76 // Returns true if at the time of the call, it appears that the job
77 // has not been making progress toward completion.
78 bool IsStuck();
80 // Makes the downloaded file available to the caller by renaming the
81 // temporary file to its destination and removing it from the BITS queue.
82 HRESULT CompleteJob();
84 // Ensures that we are running on the same thread we created the object on.
85 base::ThreadChecker thread_checker_;
87 // Used to post responses back to the main thread. Initialized on the main
88 // loop but accessed from the task runner.
89 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
91 net::URLRequestContextGetter* context_getter_;
92 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
94 // The timer and the BITS interface pointers have thread affinity. These
95 // members are initialized on the task runner and they must be destroyed
96 // on the task runner.
97 scoped_ptr<base::RepeatingTimer<BackgroundDownloader>> timer_;
99 base::win::ScopedComPtr<IBackgroundCopyManager> bits_manager_;
100 base::win::ScopedComPtr<IBackgroundCopyJob> job_;
102 // Contains the time when the download of the current url has started.
103 base::Time download_start_time_;
105 // Contains the time when the BITS job is last seen making progress.
106 base::Time job_stuck_begin_time_;
108 // True if EndDownload was called.
109 bool is_completed_;
111 // Contains the path of the downloaded file if the download was successful.
112 base::FilePath response_;
114 DISALLOW_COPY_AND_ASSIGN(BackgroundDownloader);
117 } // namespace update_client
119 #endif // COMPONENTS_UPDATE_CLIENT_BACKGROUND_DOWNLOADER_WIN_H_