OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_JOB_CONTROLLER_H_ | 5 #ifndef CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_JOB_CONTROLLER_H_ |
6 #define CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_JOB_CONTROLLER_H_ | 6 #define CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_JOB_CONTROLLER_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
10 #include <unordered_map> | 10 #include <unordered_map> |
11 | 11 |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
15 #include "base/optional.h" | 15 #include "base/optional.h" |
16 #include "content/browser/background_fetch/background_fetch_registration_id.h" | 16 #include "content/browser/background_fetch/background_fetch_registration_id.h" |
17 #include "content/browser/background_fetch/background_fetch_request_info.h" | 17 #include "content/browser/background_fetch/background_fetch_request_info.h" |
18 #include "content/common/background_fetch/background_fetch_types.h" | 18 #include "content/common/background_fetch/background_fetch_types.h" |
19 #include "content/common/content_export.h" | 19 #include "content/common/content_export.h" |
20 #include "content/public/browser/download_item.h" | 20 #include "content/public/browser/browser_thread.h" |
| 21 |
| 22 namespace net { |
| 23 class URLRequestContextGetter; |
| 24 } |
21 | 25 |
22 namespace content { | 26 namespace content { |
23 | 27 |
24 class BackgroundFetchDataManager; | 28 class BackgroundFetchDataManager; |
25 class DownloadManager; | 29 class DownloadManager; |
26 class StoragePartition; | |
27 | 30 |
28 // The JobController will be responsible for coordinating communication with the | 31 // The JobController will be responsible for coordinating communication with the |
29 // DownloadManager. It will get requests from the DataManager and dispatch them | 32 // DownloadManager. It will get requests from the DataManager and dispatch them |
30 // to the DownloadManager. It lives entirely on the IO thread. | 33 // to the DownloadManager. It lives entirely on the IO thread. |
31 class CONTENT_EXPORT BackgroundFetchJobController | 34 class CONTENT_EXPORT BackgroundFetchJobController { |
32 : public DownloadItem::Observer { | |
33 public: | 35 public: |
34 enum class State { INITIALIZED, FETCHING, ABORTED, COMPLETED }; | 36 enum class State { INITIALIZED, FETCHING, ABORTED, COMPLETED }; |
35 | 37 |
36 using CompletedCallback = | 38 using CompletedCallback = |
37 base::OnceCallback<void(BackgroundFetchJobController*)>; | 39 base::OnceCallback<void(BackgroundFetchJobController*)>; |
38 | 40 |
39 BackgroundFetchJobController( | 41 BackgroundFetchJobController( |
40 const BackgroundFetchRegistrationId& registration_id, | 42 const BackgroundFetchRegistrationId& registration_id, |
41 const BackgroundFetchOptions& options, | 43 const BackgroundFetchOptions& options, |
| 44 BackgroundFetchDataManager* data_manager, |
42 DownloadManager* download_manager, | 45 DownloadManager* download_manager, |
43 StoragePartition* storage_partition, | 46 scoped_refptr<net::URLRequestContextGetter> request_context, |
44 BackgroundFetchDataManager* data_manager, | |
45 CompletedCallback completed_callback); | 47 CompletedCallback completed_callback); |
46 ~BackgroundFetchJobController() override; | 48 ~BackgroundFetchJobController(); |
47 | 49 |
48 // Starts fetching the |initial_fetches|. The controller will continue to | 50 // Starts fetching the |initial_fetches|. The controller will continue to |
49 // fetch new content until all requests have been handled. | 51 // fetch new content until all requests have been handled. |
50 void Start(std::vector<BackgroundFetchRequestInfo> initial_requests); | 52 void Start(std::vector<BackgroundFetchRequestInfo> initial_requests); |
51 | 53 |
52 // Updates the representation of this Background Fetch in the user interface | 54 // Updates the representation of this Background Fetch in the user interface |
53 // to match the given |title|. | 55 // to match the given |title|. |
54 void UpdateUI(const std::string& title); | 56 void UpdateUI(const std::string& title); |
55 | 57 |
56 // Immediately aborts this Background Fetch by request of the developer. | 58 // Immediately aborts this Background Fetch by request of the developer. |
57 void Abort(); | 59 void Abort(); |
58 | 60 |
59 // Returns the current state of this Job Controller. | 61 // Returns the current state of this Job Controller. |
60 State state() const { return state_; } | 62 State state() const { return state_; } |
61 | 63 |
62 // Returns the registration id for which this job is fetching data. | 64 // Returns the registration id for which this job is fetching data. |
63 const BackgroundFetchRegistrationId& registration_id() const { | 65 const BackgroundFetchRegistrationId& registration_id() const { |
64 return registration_id_; | 66 return registration_id_; |
65 } | 67 } |
66 | 68 |
67 // Returns the options with which this job is fetching data. | 69 // Returns the options with which this job is fetching data. |
68 const BackgroundFetchOptions& options() const { return options_; } | 70 const BackgroundFetchOptions& options() const { return options_; } |
69 | 71 |
70 // DownloadItem::Observer methods. | 72 private: |
71 void OnDownloadUpdated(DownloadItem* item) override; | 73 class Core; |
72 void OnDownloadDestroyed(DownloadItem* item) override; | |
73 | 74 |
74 private: | |
75 // Requests the download manager to start fetching |request|. | 75 // Requests the download manager to start fetching |request|. |
76 void StartRequest(const BackgroundFetchRequestInfo& request); | 76 void StartRequest(const BackgroundFetchRequestInfo& request); |
77 | 77 |
78 // Called when the request identified by |request_index| has been started. | 78 // Called when the given |request| has started fetching, after having been |
79 // The |download_item| continues to be owned by the download system. The | 79 // assigned the |download_guid| by the download system. |
80 // |interrupt_reason| will indicate when a request could not be started. | |
81 void DidStartRequest(const BackgroundFetchRequestInfo& request, | 80 void DidStartRequest(const BackgroundFetchRequestInfo& request, |
82 DownloadItem* download_item, | 81 const std::string& download_guid); |
83 DownloadInterruptReason interrupt_reason); | 82 |
| 83 // Called when the given |request| has been completed. |
| 84 void DidCompleteRequest(const BackgroundFetchRequestInfo& request); |
84 | 85 |
85 // Called when a completed download has been marked as such in the DataManager | 86 // Called when a completed download has been marked as such in the DataManager |
86 // and the next request, if any, has been read from storage. | 87 // and the next request, if any, has been read from storage. |
87 void DidGetNextRequest( | 88 void DidGetNextRequest( |
88 const base::Optional<BackgroundFetchRequestInfo>& request); | 89 const base::Optional<BackgroundFetchRequestInfo>& request); |
89 | 90 |
90 // The registration id on behalf of which this controller is fetching data. | 91 // The registration id on behalf of which this controller is fetching data. |
91 BackgroundFetchRegistrationId registration_id_; | 92 BackgroundFetchRegistrationId registration_id_; |
92 | 93 |
93 // Options for the represented background fetch registration. | 94 // Options for the represented background fetch registration. |
94 BackgroundFetchOptions options_; | 95 BackgroundFetchOptions options_; |
95 | 96 |
96 // The current state of this Job Controller. | 97 // The current state of this Job Controller. |
97 State state_ = State::INITIALIZED; | 98 State state_ = State::INITIALIZED; |
98 | 99 |
99 // Map from DownloadItem* to the request info for the in-progress downloads. | 100 // Inner core of this job controller which lives on the UI thread. |
100 std::unordered_map<DownloadItem*, BackgroundFetchRequestInfo> downloads_; | 101 std::unique_ptr<Core, BrowserThread::DeleteOnUIThread> ui_core_; |
101 | 102 base::WeakPtr<Core> ui_core_ptr_; |
102 // Number of outstanding acknowledgements we expect to get from the | |
103 // DataManager about | |
104 int pending_completed_file_acknowledgements_ = 0; | |
105 | |
106 // The download manager this Job Controller will be fetching data with. The | |
107 // instance is owned by the profile, which also owns |this|. | |
108 DownloadManager* download_manager_; | |
109 | |
110 // Pointer to the storage partition. This object is owned by the partition | |
111 // (through a sequence of other classes). | |
112 StoragePartition* storage_partition_; | |
113 | 103 |
114 // The DataManager's lifetime is controlled by the BackgroundFetchContext and | 104 // The DataManager's lifetime is controlled by the BackgroundFetchContext and |
115 // will be kept alive until after the JobController is destroyed. | 105 // will be kept alive until after the JobController is destroyed. |
116 BackgroundFetchDataManager* data_manager_; | 106 BackgroundFetchDataManager* data_manager_; |
117 | 107 |
| 108 // Number of outstanding acknowledgements we still expect to receive. |
| 109 int pending_completed_file_acknowledgements_ = 0; |
| 110 |
118 // Callback for when all fetches have been completed. | 111 // Callback for when all fetches have been completed. |
119 CompletedCallback completed_callback_; | 112 CompletedCallback completed_callback_; |
120 | 113 |
121 base::WeakPtrFactory<BackgroundFetchJobController> weak_ptr_factory_; | 114 base::WeakPtrFactory<BackgroundFetchJobController> weak_ptr_factory_; |
122 | 115 |
123 DISALLOW_COPY_AND_ASSIGN(BackgroundFetchJobController); | 116 DISALLOW_COPY_AND_ASSIGN(BackgroundFetchJobController); |
124 }; | 117 }; |
125 | 118 |
126 } // namespace content | 119 } // namespace content |
127 | 120 |
128 #endif // CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_JOB_CONTROLLER_H_ | 121 #endif // CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_JOB_CONTROLLER_H_ |
OLD | NEW |