| 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/callback_forward.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/optional.h" | 15 #include "base/optional.h" |
| 16 #include "content/browser/background_fetch/background_fetch_job_info.h" | |
| 17 #include "content/browser/background_fetch/background_fetch_job_response_data.h" | |
| 18 #include "content/browser/background_fetch/background_fetch_registration_id.h" | 16 #include "content/browser/background_fetch/background_fetch_registration_id.h" |
| 19 #include "content/browser/background_fetch/background_fetch_request_info.h" | 17 #include "content/browser/background_fetch/background_fetch_request_info.h" |
| 20 #include "content/common/content_export.h" | 18 #include "content/common/content_export.h" |
| 21 #include "third_party/WebKit/public/platform/modules/background_fetch/background
_fetch.mojom.h" | 19 #include "third_party/WebKit/public/platform/modules/background_fetch/background
_fetch.mojom.h" |
| 22 #include "url/origin.h" | 20 #include "url/origin.h" |
| 23 | 21 |
| 24 namespace content { | 22 namespace content { |
| 25 | 23 |
| 24 struct BackgroundFetchSettledFetch; |
| 25 class BlobHandle; |
| 26 class BrowserContext; | 26 class BrowserContext; |
| 27 class ChromeBlobStorageContext; | 27 class ChromeBlobStorageContext; |
| 28 | 28 |
| 29 // The BackgroundFetchDataManager keeps track of all of the outstanding requests | 29 // The BackgroundFetchDataManager keeps track of all of the outstanding requests |
| 30 // which are in process in the DownloadManager. When Chromium restarts, it is | 30 // which are in process in the DownloadManager. When Chromium restarts, it is |
| 31 // responsibile for reconnecting all the in progress downloads with an observer | 31 // responsibile for reconnecting all the in progress downloads with an observer |
| 32 // which will keep the metadata up to date. | 32 // which will keep the metadata up to date. |
| 33 class CONTENT_EXPORT BackgroundFetchDataManager { | 33 class CONTENT_EXPORT BackgroundFetchDataManager { |
| 34 public: | 34 public: |
| 35 using CreateRegistrationCallback = | 35 using CreateRegistrationCallback = |
| 36 base::OnceCallback<void(blink::mojom::BackgroundFetchError, | 36 base::OnceCallback<void(blink::mojom::BackgroundFetchError, |
| 37 std::vector<BackgroundFetchRequestInfo>)>; | 37 std::vector<BackgroundFetchRequestInfo>)>; |
| 38 using DeleteRegistrationCallback = | 38 using DeleteRegistrationCallback = |
| 39 base::OnceCallback<void(blink::mojom::BackgroundFetchError)>; | 39 base::OnceCallback<void(blink::mojom::BackgroundFetchError)>; |
| 40 using NextRequestCallback = base::OnceCallback<void( | 40 using NextRequestCallback = base::OnceCallback<void( |
| 41 const base::Optional<BackgroundFetchRequestInfo>&)>; | 41 const base::Optional<BackgroundFetchRequestInfo>&)>; |
| 42 using SettledFetchesCallback = |
| 43 base::OnceCallback<void(blink::mojom::BackgroundFetchError, |
| 44 std::vector<BackgroundFetchSettledFetch>, |
| 45 std::vector<std::unique_ptr<BlobHandle>>)>; |
| 42 | 46 |
| 43 explicit BackgroundFetchDataManager(BrowserContext* browser_context); | 47 explicit BackgroundFetchDataManager(BrowserContext* browser_context); |
| 44 ~BackgroundFetchDataManager(); | 48 ~BackgroundFetchDataManager(); |
| 45 | 49 |
| 46 // Creates and stores a new registration with the given properties. Will | 50 // Creates and stores a new registration with the given properties. Will |
| 47 // invoke the |callback| when the registration has been created, which may | 51 // invoke the |callback| when the registration has been created, which may |
| 48 // fail due to invalid input or storage errors. | 52 // fail due to invalid input or storage errors. |
| 49 void CreateRegistration( | 53 void CreateRegistration( |
| 50 const BackgroundFetchRegistrationId& registration_id, | 54 const BackgroundFetchRegistrationId& registration_id, |
| 51 const std::vector<ServiceWorkerFetchRequest>& requests, | 55 const std::vector<ServiceWorkerFetchRequest>& requests, |
| 52 const BackgroundFetchOptions& options, | 56 const BackgroundFetchOptions& options, |
| 53 CreateRegistrationCallback callback); | 57 CreateRegistrationCallback callback); |
| 54 | 58 |
| 55 // Marks that the |request|, part of the Background Fetch identified by | 59 // Marks that the |request|, part of the Background Fetch identified by |
| 56 // |registration_id|, has been started as |download_guid|. | 60 // |registration_id|, has been started as |download_guid|. |
| 57 void MarkRequestAsStarted( | 61 void MarkRequestAsStarted( |
| 58 const BackgroundFetchRegistrationId& registration_id, | 62 const BackgroundFetchRegistrationId& registration_id, |
| 59 const BackgroundFetchRequestInfo& request, | 63 const BackgroundFetchRequestInfo& request, |
| 60 const std::string& download_guid); | 64 const std::string& download_guid); |
| 61 | 65 |
| 62 // Marks that the |request|, part of the Background Fetch identified by | 66 // Marks that the |request|, part of the Background Fetch identified by |
| 63 // |registration_id|, has completed. Will invoke the |callback| with the | 67 // |registration_id|, has completed. Will invoke the |callback| with the |
| 64 // next request, if any, when the operation has completed. | 68 // next request, if any, when the operation has completed. |
| 65 void MarkRequestAsCompleteAndGetNextRequest( | 69 void MarkRequestAsCompleteAndGetNextRequest( |
| 66 const BackgroundFetchRegistrationId& registration_id, | 70 const BackgroundFetchRegistrationId& registration_id, |
| 67 const BackgroundFetchRequestInfo& request, | 71 const BackgroundFetchRequestInfo& request, |
| 68 NextRequestCallback callback); | 72 NextRequestCallback callback); |
| 69 | 73 |
| 74 // Reads all settled fetches for the given |registration_id|. Both the Request |
| 75 // and Response objects will be initialised based on the stored data. Will |
| 76 // invoke the |callback| when the list of fetches has been compiled. |
| 77 void GetSettledFetchesForRegistration( |
| 78 const BackgroundFetchRegistrationId& registration_id, |
| 79 SettledFetchesCallback callback); |
| 80 |
| 70 // Deletes the registration identified by |registration_id|. Will invoke the | 81 // Deletes the registration identified by |registration_id|. Will invoke the |
| 71 // |callback| when the registration has been deleted from storage. | 82 // |callback| when the registration has been deleted from storage. |
| 72 void DeleteRegistration(const BackgroundFetchRegistrationId& registration_id, | 83 void DeleteRegistration(const BackgroundFetchRegistrationId& registration_id, |
| 73 DeleteRegistrationCallback callback); | 84 DeleteRegistrationCallback callback); |
| 74 | 85 |
| 75 // TODO(harkness): Replace the OnceClosure with a callback to return the | |
| 76 // response object once it is decided whether lifetime should be passed to the | |
| 77 // caller or stay here. | |
| 78 void GetJobResponse(const std::string& job_guid, | |
| 79 const BackgroundFetchResponseCompleteCallback& callback); | |
| 80 | |
| 81 // The following methods are called by the JobController to update job state. | |
| 82 // Returns a boolean indicating whether there are more requests to process. | |
| 83 bool UpdateRequestState(const std::string& job_guid, | |
| 84 const std::string& request_guid, | |
| 85 DownloadItem::DownloadState state, | |
| 86 DownloadInterruptReason interrupt_reason); | |
| 87 void UpdateRequestStorageState(const std::string& job_guid, | |
| 88 const std::string& request_guid, | |
| 89 const base::FilePath& file_path, | |
| 90 int64_t received_bytes); | |
| 91 void UpdateRequestDownloadGuid(const std::string& job_guid, | |
| 92 const std::string& request_guid, | |
| 93 const std::string& download_guid); | |
| 94 | |
| 95 // Called by the JobController to get a BackgroundFetchRequestInfo to | |
| 96 // process. | |
| 97 const BackgroundFetchRequestInfo& GetNextBackgroundFetchRequestInfo( | |
| 98 const std::string& job_guid); | |
| 99 | |
| 100 // Indicates whether all requests have been handed to the JobController. | |
| 101 bool HasRequestsRemaining(const std::string& job_guid) const; | |
| 102 | |
| 103 // Indicates whether all requests have been handed out and completed. | |
| 104 bool IsComplete(const std::string& job_guid) const; | |
| 105 | |
| 106 private: | 86 private: |
| 107 friend class BackgroundFetchDataManagerTest; | 87 friend class BackgroundFetchDataManagerTest; |
| 108 | 88 |
| 109 class RegistrationData; | 89 class RegistrationData; |
| 110 | 90 |
| 111 // Storage interface. | 91 // The blob storage request with which response information will be stored. |
| 112 void WriteJobToStorage( | 92 scoped_refptr<ChromeBlobStorageContext> blob_storage_context_; |
| 113 std::unique_ptr<BackgroundFetchJobInfo> job_info, | |
| 114 std::vector<std::unique_ptr<BackgroundFetchRequestInfo>> request_infos); | |
| 115 void WriteRequestToStorage(const std::string& job_guid, | |
| 116 BackgroundFetchRequestInfo* request_info); | |
| 117 std::unique_ptr<BackgroundFetchRequestInfo> GetRequestInfo( | |
| 118 const std::string& job_guid, | |
| 119 size_t request_index); | |
| 120 | |
| 121 void DidGetBlobStorageContext( | |
| 122 const std::string& job_guid, | |
| 123 const BackgroundFetchResponseCompleteCallback& callback, | |
| 124 ChromeBlobStorageContext* blob_context); | |
| 125 | |
| 126 // Indirectly, this is owned by the BrowserContext. | |
| 127 BrowserContext* browser_context_; | |
| 128 | 93 |
| 129 // Map of known background fetch registration ids to their associated data. | 94 // Map of known background fetch registration ids to their associated data. |
| 130 std::map<BackgroundFetchRegistrationId, std::unique_ptr<RegistrationData>> | 95 std::map<BackgroundFetchRegistrationId, std::unique_ptr<RegistrationData>> |
| 131 registrations_; | 96 registrations_; |
| 132 | 97 |
| 133 // Map from job_guid to JobInfo. | |
| 134 std::unordered_map<std::string, std::unique_ptr<BackgroundFetchJobInfo>> | |
| 135 job_map_; | |
| 136 | |
| 137 // Temporary map to hold data which will be written to storage. | |
| 138 // Map from job_guid to RequestInfos. | |
| 139 std::unordered_map<std::string, std::vector<BackgroundFetchRequestInfo>> | |
| 140 request_map_; | |
| 141 | |
| 142 base::WeakPtrFactory<BackgroundFetchDataManager> weak_ptr_factory_; | 98 base::WeakPtrFactory<BackgroundFetchDataManager> weak_ptr_factory_; |
| 143 | 99 |
| 144 DISALLOW_COPY_AND_ASSIGN(BackgroundFetchDataManager); | 100 DISALLOW_COPY_AND_ASSIGN(BackgroundFetchDataManager); |
| 145 }; | 101 }; |
| 146 | 102 |
| 147 } // namespace content | 103 } // namespace content |
| 148 | 104 |
| 149 #endif // CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_DATA_MANAGER_H_ | 105 #endif // CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_DATA_MANAGER_H_ |
| OLD | NEW |