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