Chromium Code Reviews| 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 #include "content/browser/background_fetch/background_fetch_data_manager.h" | 5 #include "content/browser/background_fetch/background_fetch_data_manager.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <queue> | 8 #include <queue> |
| 9 | 9 |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 115 "RegistrationData::completed_requests_ assumes no parallelism"); | 115 "RegistrationData::completed_requests_ assumes no parallelism"); |
| 116 | 116 |
| 117 std::vector<scoped_refptr<BackgroundFetchRequestInfo>> completed_requests_; | 117 std::vector<scoped_refptr<BackgroundFetchRequestInfo>> completed_requests_; |
| 118 | 118 |
| 119 DISALLOW_COPY_AND_ASSIGN(RegistrationData); | 119 DISALLOW_COPY_AND_ASSIGN(RegistrationData); |
| 120 }; | 120 }; |
| 121 | 121 |
| 122 BackgroundFetchDataManager::BackgroundFetchDataManager( | 122 BackgroundFetchDataManager::BackgroundFetchDataManager( |
| 123 BrowserContext* browser_context) | 123 BrowserContext* browser_context) |
| 124 : weak_ptr_factory_(this) { | 124 : weak_ptr_factory_(this) { |
| 125 // Constructed on the UI thread, then used on a different thread. | |
| 125 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 126 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 127 DETACH_FROM_SEQUENCE(sequence_checker_); | |
| 128 | |
| 126 DCHECK(browser_context); | 129 DCHECK(browser_context); |
| 127 | 130 |
| 128 // Store the blob storage context for the given |browser_context|. | 131 // Store the blob storage context for the given |browser_context|. |
| 129 blob_storage_context_ = | 132 blob_storage_context_ = |
| 130 make_scoped_refptr(ChromeBlobStorageContext::GetFor(browser_context)); | 133 make_scoped_refptr(ChromeBlobStorageContext::GetFor(browser_context)); |
| 131 DCHECK(blob_storage_context_); | 134 DCHECK(blob_storage_context_); |
| 132 } | 135 } |
| 133 | 136 |
| 134 BackgroundFetchDataManager::~BackgroundFetchDataManager() = default; | 137 BackgroundFetchDataManager::~BackgroundFetchDataManager() { |
| 138 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | |
| 139 } | |
| 135 | 140 |
| 136 void BackgroundFetchDataManager::CreateRegistration( | 141 void BackgroundFetchDataManager::CreateRegistration( |
| 137 const BackgroundFetchRegistrationId& registration_id, | 142 const BackgroundFetchRegistrationId& registration_id, |
| 138 const std::vector<ServiceWorkerFetchRequest>& requests, | 143 const std::vector<ServiceWorkerFetchRequest>& requests, |
| 139 const BackgroundFetchOptions& options, | 144 const BackgroundFetchOptions& options, |
| 140 CreateRegistrationCallback callback) { | 145 CreateRegistrationCallback callback) { |
| 146 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | |
| 147 | |
| 141 if (registrations_.find(registration_id) != registrations_.end()) { | 148 if (registrations_.find(registration_id) != registrations_.end()) { |
| 142 std::move(callback).Run( | 149 std::move(callback).Run( |
| 143 blink::mojom::BackgroundFetchError::DUPLICATED_TAG, | 150 blink::mojom::BackgroundFetchError::DUPLICATED_TAG, |
| 144 std::vector<scoped_refptr<BackgroundFetchRequestInfo>>()); | 151 std::vector<scoped_refptr<BackgroundFetchRequestInfo>>()); |
| 145 return; | 152 return; |
| 146 } | 153 } |
| 147 | 154 |
| 148 std::unique_ptr<RegistrationData> registration_data = | 155 std::unique_ptr<RegistrationData> registration_data = |
| 149 base::MakeUnique<RegistrationData>(requests, options); | 156 base::MakeUnique<RegistrationData>(requests, options); |
| 150 | 157 |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 163 | 170 |
| 164 // Inform the |callback| of the newly created registration. | 171 // Inform the |callback| of the newly created registration. |
| 165 std::move(callback).Run(blink::mojom::BackgroundFetchError::NONE, | 172 std::move(callback).Run(blink::mojom::BackgroundFetchError::NONE, |
| 166 std::move(initial_requests)); | 173 std::move(initial_requests)); |
| 167 } | 174 } |
| 168 | 175 |
| 169 void BackgroundFetchDataManager::MarkRequestAsStarted( | 176 void BackgroundFetchDataManager::MarkRequestAsStarted( |
| 170 const BackgroundFetchRegistrationId& registration_id, | 177 const BackgroundFetchRegistrationId& registration_id, |
| 171 BackgroundFetchRequestInfo* request, | 178 BackgroundFetchRequestInfo* request, |
| 172 const std::string& download_guid) { | 179 const std::string& download_guid) { |
| 180 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | |
| 181 | |
| 173 auto iter = registrations_.find(registration_id); | 182 auto iter = registrations_.find(registration_id); |
| 174 DCHECK(iter != registrations_.end()); | 183 DCHECK(iter != registrations_.end()); |
| 175 | 184 |
| 176 RegistrationData* registration_data = iter->second.get(); | 185 RegistrationData* registration_data = iter->second.get(); |
| 177 registration_data->MarkRequestAsStarted(request, download_guid); | 186 registration_data->MarkRequestAsStarted(request, download_guid); |
| 178 } | 187 } |
| 179 | 188 |
| 180 void BackgroundFetchDataManager::MarkRequestAsCompleteAndGetNextRequest( | 189 void BackgroundFetchDataManager::MarkRequestAsCompleteAndGetNextRequest( |
| 181 const BackgroundFetchRegistrationId& registration_id, | 190 const BackgroundFetchRegistrationId& registration_id, |
| 182 BackgroundFetchRequestInfo* request, | 191 BackgroundFetchRequestInfo* request, |
| 183 NextRequestCallback callback) { | 192 NextRequestCallback callback) { |
| 193 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | |
| 194 | |
| 184 auto iter = registrations_.find(registration_id); | 195 auto iter = registrations_.find(registration_id); |
| 185 DCHECK(iter != registrations_.end()); | 196 DCHECK(iter != registrations_.end()); |
| 186 | 197 |
| 187 RegistrationData* registration_data = iter->second.get(); | 198 RegistrationData* registration_data = iter->second.get(); |
| 188 registration_data->MarkRequestAsComplete(request); | 199 registration_data->MarkRequestAsComplete(request); |
| 189 | 200 |
| 190 scoped_refptr<BackgroundFetchRequestInfo> next_request; | 201 scoped_refptr<BackgroundFetchRequestInfo> next_request; |
| 191 if (registration_data->HasPendingRequests()) | 202 if (registration_data->HasPendingRequests()) |
| 192 next_request = registration_data->GetPendingRequest(); | 203 next_request = registration_data->GetPendingRequest(); |
| 193 | 204 |
| 194 std::move(callback).Run(std::move(next_request)); | 205 std::move(callback).Run(std::move(next_request)); |
| 195 } | 206 } |
| 196 | 207 |
| 197 void BackgroundFetchDataManager::GetSettledFetchesForRegistration( | 208 void BackgroundFetchDataManager::GetSettledFetchesForRegistration( |
| 198 const BackgroundFetchRegistrationId& registration_id, | 209 const BackgroundFetchRegistrationId& registration_id, |
| 199 SettledFetchesCallback callback) { | 210 SettledFetchesCallback callback) { |
| 211 // This method calls ChromeBlobStorageContext, which must happen on IO thread. | |
| 212 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
|
Peter Beverloo
2017/07/10 13:13:21
ChromeBlobStorageContext::CreateFileBackedBlob() w
johnme
2017/07/10 13:41:07
Done.
| |
| 213 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | |
| 214 | |
| 200 auto iter = registrations_.find(registration_id); | 215 auto iter = registrations_.find(registration_id); |
| 201 DCHECK(iter != registrations_.end()); | 216 DCHECK(iter != registrations_.end()); |
| 202 | 217 |
| 203 RegistrationData* registration_data = iter->second.get(); | 218 RegistrationData* registration_data = iter->second.get(); |
| 204 DCHECK(!registration_data->HasPendingRequests()); | 219 DCHECK(!registration_data->HasPendingRequests()); |
| 205 | 220 |
| 206 const std::vector<scoped_refptr<BackgroundFetchRequestInfo>>& requests = | 221 const std::vector<scoped_refptr<BackgroundFetchRequestInfo>>& requests = |
| 207 registration_data->GetCompletedRequests(); | 222 registration_data->GetCompletedRequests(); |
| 208 | 223 |
| 209 bool background_fetch_succeeded = true; | 224 bool background_fetch_succeeded = true; |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 264 } | 279 } |
| 265 | 280 |
| 266 std::move(callback).Run(blink::mojom::BackgroundFetchError::NONE, | 281 std::move(callback).Run(blink::mojom::BackgroundFetchError::NONE, |
| 267 background_fetch_succeeded, | 282 background_fetch_succeeded, |
| 268 std::move(settled_fetches), std::move(blob_handles)); | 283 std::move(settled_fetches), std::move(blob_handles)); |
| 269 } | 284 } |
| 270 | 285 |
| 271 void BackgroundFetchDataManager::DeleteRegistration( | 286 void BackgroundFetchDataManager::DeleteRegistration( |
| 272 const BackgroundFetchRegistrationId& registration_id, | 287 const BackgroundFetchRegistrationId& registration_id, |
| 273 DeleteRegistrationCallback callback) { | 288 DeleteRegistrationCallback callback) { |
| 289 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | |
| 290 | |
| 274 auto iter = registrations_.find(registration_id); | 291 auto iter = registrations_.find(registration_id); |
| 275 if (iter == registrations_.end()) { | 292 if (iter == registrations_.end()) { |
| 276 std::move(callback).Run(blink::mojom::BackgroundFetchError::INVALID_TAG); | 293 std::move(callback).Run(blink::mojom::BackgroundFetchError::INVALID_TAG); |
| 277 return; | 294 return; |
| 278 } | 295 } |
| 279 | 296 |
| 280 registrations_.erase(iter); | 297 registrations_.erase(iter); |
| 281 | 298 |
| 282 std::move(callback).Run(blink::mojom::BackgroundFetchError::NONE); | 299 std::move(callback).Run(blink::mojom::BackgroundFetchError::NONE); |
| 283 } | 300 } |
| 284 | 301 |
| 285 } // namespace content | 302 } // namespace content |
| OLD | NEW |