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_DATA_MANAGER_H_ | 5 #ifndef CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_DATA_MANAGER_H_ |
6 #define CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_DATA_MANAGER_H_ | 6 #define CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_DATA_MANAGER_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
11 #include <unordered_map> | 11 #include <unordered_map> |
12 | 12 |
| 13 #include "base/callback_forward.h" |
13 #include "base/macros.h" | 14 #include "base/macros.h" |
14 #include "content/browser/background_fetch/background_fetch_job_data.h" | |
15 #include "content/browser/background_fetch/background_fetch_job_info.h" | 15 #include "content/browser/background_fetch/background_fetch_job_info.h" |
| 16 #include "content/browser/background_fetch/background_fetch_job_response_data.h" |
16 #include "content/browser/background_fetch/background_fetch_registration_id.h" | 17 #include "content/browser/background_fetch/background_fetch_registration_id.h" |
| 18 #include "content/browser/background_fetch/background_fetch_request_info.h" |
17 #include "content/common/content_export.h" | 19 #include "content/common/content_export.h" |
18 #include "url/origin.h" | 20 #include "url/origin.h" |
19 | 21 |
20 namespace content { | 22 namespace content { |
21 | 23 |
22 class BackgroundFetchContext; | 24 class BrowserContext; |
| 25 class BlobHandle; |
23 | 26 |
24 // The BackgroundFetchDataManager keeps track of all of the outstanding requests | 27 // The BackgroundFetchDataManager keeps track of all of the outstanding requests |
25 // which are in process in the DownloadManager. When Chromium restarts, it is | 28 // which are in process in the DownloadManager. When Chromium restarts, it is |
26 // responsibile for reconnecting all the in progress downloads with an observer | 29 // responsibile for reconnecting all the in progress downloads with an observer |
27 // which will keep the metadata up to date. | 30 // which will keep the metadata up to date. |
28 class CONTENT_EXPORT BackgroundFetchDataManager { | 31 class CONTENT_EXPORT BackgroundFetchDataManager { |
29 public: | 32 public: |
30 explicit BackgroundFetchDataManager( | 33 explicit BackgroundFetchDataManager(BrowserContext* browser_context); |
31 BackgroundFetchContext* background_fetch_context); | |
32 ~BackgroundFetchDataManager(); | 34 ~BackgroundFetchDataManager(); |
33 | 35 |
34 // Called by BackgroundFetchContext when a new request is started, this will | 36 // Called by BackgroundFetchContext when a new request is started, this will |
35 // store all of the necessary metadata to track the request. | 37 // store all of the necessary metadata to track the request. |
36 std::unique_ptr<BackgroundFetchJobData> CreateRequest( | 38 void CreateRequest( |
37 std::unique_ptr<BackgroundFetchJobInfo> job_info, | 39 std::unique_ptr<BackgroundFetchJobInfo> job_info, |
38 BackgroundFetchRequestInfos request_infos); | 40 std::vector<std::unique_ptr<BackgroundFetchRequestInfo>> request_infos); |
| 41 |
| 42 // TODO(harkness): Replace the OnceClosure with a callback to return the |
| 43 // response object once it is decided whether lifetime should be passed to the |
| 44 // caller or stay here. |
| 45 void GetJobResponse(const std::string& job_guid, |
| 46 const BackgroundFetchResponseCompleteCallback& callback); |
| 47 |
| 48 // The following methods are called by the JobController to update job state. |
| 49 // Returns a boolean indicating whether there are more requests to process. |
| 50 virtual bool UpdateRequestState(const std::string& job_guid, |
| 51 const std::string& request_guid, |
| 52 DownloadItem::DownloadState state, |
| 53 DownloadInterruptReason interrupt_reason); |
| 54 virtual void UpdateRequestStorageState(const std::string& job_guid, |
| 55 const std::string& request_guid, |
| 56 const base::FilePath& file_path, |
| 57 int64_t received_bytes); |
| 58 virtual void UpdateRequestDownloadGuid(const std::string& job_guid, |
| 59 const std::string& request_guid, |
| 60 const std::string& download_guid); |
| 61 |
| 62 // Called by the JobController to get a BackgroundFetchRequestInfo to |
| 63 // process. |
| 64 virtual const BackgroundFetchRequestInfo& GetNextBackgroundFetchRequestInfo( |
| 65 const std::string& job_guid); |
| 66 |
| 67 // Indicates whether all requests have been handed to the JobController. |
| 68 virtual bool HasRequestsRemaining(const std::string& job_guid) const; |
| 69 |
| 70 // Indicates whether all requests have been handed out and completed. |
| 71 virtual bool IsComplete(const std::string& job_guid) const; |
39 | 72 |
40 private: | 73 private: |
41 void WriteJobToStorage(std::unique_ptr<BackgroundFetchJobInfo> job_info, | 74 // Storage interface. |
42 BackgroundFetchRequestInfos request_infos); | 75 void WriteJobToStorage( |
| 76 std::unique_ptr<BackgroundFetchJobInfo> job_info, |
| 77 std::vector<std::unique_ptr<BackgroundFetchRequestInfo>> request_infos); |
| 78 void WriteRequestToStorage(const std::string& job_guid, |
| 79 BackgroundFetchRequestInfo* request_info); |
| 80 std::unique_ptr<BackgroundFetchRequestInfo> GetRequestInfo( |
| 81 const std::string& job_guid, |
| 82 size_t request_index); |
43 | 83 |
44 BackgroundFetchRequestInfos& ReadRequestsFromStorage( | 84 void DidGetRequestResponse(const std::string& job_guid, |
45 const std::string& job_guid); | 85 int request_sequence_number, |
| 86 std::unique_ptr<BlobHandle> blob_handle); |
46 | 87 |
47 // BackgroundFetchContext owns this BackgroundFetchDataManager, so the | 88 // Indirectly, this is owned by the BrowserContext. |
48 // DataManager is guaranteed to be destructed before the Context. | 89 BrowserContext* browser_context_; |
49 BackgroundFetchContext* background_fetch_context_; | |
50 | 90 |
51 // Set of known background fetch registration ids. | 91 // Set of known background fetch registration ids. |
52 std::set<BackgroundFetchRegistrationId> known_registrations_; | 92 std::set<BackgroundFetchRegistrationId> known_registrations_; |
53 | 93 |
54 // Temporary map to hold data which will be written to storage. | |
55 // Map from job_guid to JobInfo. | 94 // Map from job_guid to JobInfo. |
56 std::unordered_map<std::string, std::unique_ptr<BackgroundFetchJobInfo>> | 95 std::unordered_map<std::string, std::unique_ptr<BackgroundFetchJobInfo>> |
57 job_map_; | 96 job_map_; |
| 97 |
| 98 // Temporary map to hold data which will be written to storage. |
58 // Map from job_guid to RequestInfos. | 99 // Map from job_guid to RequestInfos. |
59 std::unordered_map<std::string, BackgroundFetchRequestInfos> request_map_; | 100 std::unordered_map<std::string, std::vector<BackgroundFetchRequestInfo>> |
| 101 request_map_; |
| 102 |
| 103 base::WeakPtrFactory<BackgroundFetchDataManager> weak_ptr_factory_; |
60 | 104 |
61 DISALLOW_COPY_AND_ASSIGN(BackgroundFetchDataManager); | 105 DISALLOW_COPY_AND_ASSIGN(BackgroundFetchDataManager); |
62 }; | 106 }; |
63 | 107 |
64 } // namespace content | 108 } // namespace content |
65 | 109 |
66 #endif // CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_DATA_MANAGER_H_ | 110 #endif // CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_DATA_MANAGER_H_ |
OLD | NEW |