| 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_context.h" | 5 #include "content/browser/background_fetch/background_fetch_context.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "content/browser/background_fetch/background_fetch_data_manager.h" | 8 #include "content/browser/background_fetch/background_fetch_data_manager.h" |
| 9 #include "content/browser/background_fetch/background_fetch_event_dispatcher.h" | 9 #include "content/browser/background_fetch/background_fetch_event_dispatcher.h" |
| 10 #include "content/browser/background_fetch/background_fetch_job_controller.h" | 10 #include "content/browser/background_fetch/background_fetch_job_controller.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 | 37 |
| 38 BackgroundFetchContext::BackgroundFetchContext( | 38 BackgroundFetchContext::BackgroundFetchContext( |
| 39 BrowserContext* browser_context, | 39 BrowserContext* browser_context, |
| 40 StoragePartitionImpl* storage_partition, | 40 StoragePartitionImpl* storage_partition, |
| 41 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) | 41 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) |
| 42 : browser_context_(browser_context), | 42 : browser_context_(browser_context), |
| 43 data_manager_( | 43 data_manager_( |
| 44 base::MakeUnique<BackgroundFetchDataManager>(browser_context)), | 44 base::MakeUnique<BackgroundFetchDataManager>(browser_context)), |
| 45 event_dispatcher_(base::MakeUnique<BackgroundFetchEventDispatcher>( | 45 event_dispatcher_(base::MakeUnique<BackgroundFetchEventDispatcher>( |
| 46 std::move(service_worker_context))) { | 46 std::move(service_worker_context))) { |
| 47 // Although this lives only on the IO thread, it is constructed on UI thread. |
| 47 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 48 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 48 } | 49 } |
| 49 | 50 |
| 50 BackgroundFetchContext::~BackgroundFetchContext() { | 51 BackgroundFetchContext::~BackgroundFetchContext() { |
| 51 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 52 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 52 } | 53 } |
| 53 | 54 |
| 54 void BackgroundFetchContext::InitializeOnIOThread( | 55 void BackgroundFetchContext::InitializeOnIOThread( |
| 55 scoped_refptr<net::URLRequestContextGetter> request_context_getter) { | 56 scoped_refptr<net::URLRequestContextGetter> request_context_getter) { |
| 56 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 57 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 58 |
| 57 request_context_getter_ = request_context_getter; | 59 request_context_getter_ = request_context_getter; |
| 58 } | 60 } |
| 59 | 61 |
| 60 void BackgroundFetchContext::Shutdown() { | |
| 61 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 62 BrowserThread::PostTask( | |
| 63 BrowserThread::IO, FROM_HERE, | |
| 64 base::Bind(&BackgroundFetchContext::ShutdownOnIO, this)); | |
| 65 } | |
| 66 | |
| 67 void BackgroundFetchContext::ShutdownOnIO() { | |
| 68 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 69 active_fetches_.clear(); | |
| 70 } | |
| 71 | |
| 72 void BackgroundFetchContext::StartFetch( | 62 void BackgroundFetchContext::StartFetch( |
| 73 const BackgroundFetchRegistrationId& registration_id, | 63 const BackgroundFetchRegistrationId& registration_id, |
| 74 const std::vector<ServiceWorkerFetchRequest>& requests, | 64 const std::vector<ServiceWorkerFetchRequest>& requests, |
| 75 const BackgroundFetchOptions& options, | 65 const BackgroundFetchOptions& options, |
| 76 blink::mojom::BackgroundFetchService::FetchCallback callback) { | 66 blink::mojom::BackgroundFetchService::FetchCallback callback) { |
| 77 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 67 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 68 |
| 78 data_manager_->CreateRegistration( | 69 data_manager_->CreateRegistration( |
| 79 registration_id, requests, options, | 70 registration_id, requests, options, |
| 80 base::BindOnce(&BackgroundFetchContext::DidCreateRegistration, this, | 71 base::BindOnce(&BackgroundFetchContext::DidCreateRegistration, this, |
| 81 registration_id, options, std::move(callback))); | 72 registration_id, options, std::move(callback))); |
| 82 } | 73 } |
| 83 | 74 |
| 84 void BackgroundFetchContext::DidCreateRegistration( | 75 void BackgroundFetchContext::DidCreateRegistration( |
| 85 const BackgroundFetchRegistrationId& registration_id, | 76 const BackgroundFetchRegistrationId& registration_id, |
| 86 const BackgroundFetchOptions& options, | 77 const BackgroundFetchOptions& options, |
| 87 blink::mojom::BackgroundFetchService::FetchCallback callback, | 78 blink::mojom::BackgroundFetchService::FetchCallback callback, |
| 88 blink::mojom::BackgroundFetchError error, | 79 blink::mojom::BackgroundFetchError error, |
| 89 std::vector<scoped_refptr<BackgroundFetchRequestInfo>> initial_requests) { | 80 std::vector<scoped_refptr<BackgroundFetchRequestInfo>> initial_requests) { |
| 81 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 82 |
| 90 RecordRegistrationCreatedError(error); | 83 RecordRegistrationCreatedError(error); |
| 91 if (error != blink::mojom::BackgroundFetchError::NONE) { | 84 if (error != blink::mojom::BackgroundFetchError::NONE) { |
| 92 std::move(callback).Run(error, base::nullopt /* registration */); | 85 std::move(callback).Run(error, base::nullopt /* registration */); |
| 93 return; | 86 return; |
| 94 } | 87 } |
| 95 | 88 |
| 96 // Create the BackgroundFetchJobController, which will do the actual fetching. | 89 // Create the BackgroundFetchJobController, which will do the actual fetching. |
| 97 CreateController(registration_id, options, std::move(initial_requests)); | 90 CreateController(registration_id, options, std::move(initial_requests)); |
| 98 | 91 |
| 99 // Create the BackgroundFetchRegistration the renderer process will receive, | 92 // Create the BackgroundFetchRegistration the renderer process will receive, |
| 100 // which enables it to resolve the promise telling the developer it worked. | 93 // which enables it to resolve the promise telling the developer it worked. |
| 101 BackgroundFetchRegistration registration; | 94 BackgroundFetchRegistration registration; |
| 102 registration.tag = registration_id.tag(); | 95 registration.tag = registration_id.tag(); |
| 103 registration.icons = options.icons; | 96 registration.icons = options.icons; |
| 104 registration.title = options.title; | 97 registration.title = options.title; |
| 105 registration.total_download_size = options.total_download_size; | 98 registration.total_download_size = options.total_download_size; |
| 106 | 99 |
| 107 std::move(callback).Run(blink::mojom::BackgroundFetchError::NONE, | 100 std::move(callback).Run(blink::mojom::BackgroundFetchError::NONE, |
| 108 registration); | 101 registration); |
| 109 } | 102 } |
| 110 | 103 |
| 111 std::vector<std::string> | 104 std::vector<std::string> |
| 112 BackgroundFetchContext::GetActiveTagsForServiceWorkerRegistration( | 105 BackgroundFetchContext::GetActiveTagsForServiceWorkerRegistration( |
| 113 int64_t service_worker_registration_id, | 106 int64_t service_worker_registration_id, |
| 114 const url::Origin& origin) const { | 107 const url::Origin& origin) const { |
| 108 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 109 |
| 115 std::vector<std::string> tags; | 110 std::vector<std::string> tags; |
| 116 for (const auto& pair : active_fetches_) { | 111 for (const auto& pair : active_fetches_) { |
| 117 const BackgroundFetchRegistrationId& registration_id = | 112 const BackgroundFetchRegistrationId& registration_id = |
| 118 pair.second->registration_id(); | 113 pair.second->registration_id(); |
| 119 | 114 |
| 120 // Only return the tags when the origin and SW registration id match. | 115 // Only return the tags when the origin and SW registration id match. |
| 121 if (registration_id.origin() == origin && | 116 if (registration_id.origin() == origin && |
| 122 registration_id.service_worker_registration_id() == | 117 registration_id.service_worker_registration_id() == |
| 123 service_worker_registration_id) { | 118 service_worker_registration_id) { |
| 124 tags.push_back(pair.second->registration_id().tag()); | 119 tags.push_back(pair.second->registration_id().tag()); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 142 return nullptr; | 137 return nullptr; |
| 143 } | 138 } |
| 144 | 139 |
| 145 return controller; | 140 return controller; |
| 146 } | 141 } |
| 147 | 142 |
| 148 void BackgroundFetchContext::CreateController( | 143 void BackgroundFetchContext::CreateController( |
| 149 const BackgroundFetchRegistrationId& registration_id, | 144 const BackgroundFetchRegistrationId& registration_id, |
| 150 const BackgroundFetchOptions& options, | 145 const BackgroundFetchOptions& options, |
| 151 std::vector<scoped_refptr<BackgroundFetchRequestInfo>> initial_requests) { | 146 std::vector<scoped_refptr<BackgroundFetchRequestInfo>> initial_requests) { |
| 147 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 148 |
| 152 net::NetworkTrafficAnnotationTag traffic_annotation = | 149 net::NetworkTrafficAnnotationTag traffic_annotation = |
| 153 net::DefineNetworkTrafficAnnotation("background_fetch_context", R"( | 150 net::DefineNetworkTrafficAnnotation("background_fetch_context", R"( |
| 154 semantics { | 151 semantics { |
| 155 sender: "Background Fetch API" | 152 sender: "Background Fetch API" |
| 156 description: | 153 description: |
| 157 "The Background Fetch API enables developers to upload or download " | 154 "The Background Fetch API enables developers to upload or download " |
| 158 "files on behalf of the user. Such fetches will yield a user " | 155 "files on behalf of the user. Such fetches will yield a user " |
| 159 "visible notification to inform the user of the operation, through " | 156 "visible notification to inform the user of the operation, through " |
| 160 "which it can be suspended, resumed and/or cancelled. The " | 157 "which it can be suspended, resumed and/or cancelled. The " |
| 161 "developer retains control of the file once the fetch is " | 158 "developer retains control of the file once the fetch is " |
| (...skipping 27 matching lines...) Expand all Loading... |
| 189 // future we may want a more elaborate scheduling mechanism here. | 186 // future we may want a more elaborate scheduling mechanism here. |
| 190 controller->Start(std::move(initial_requests)); | 187 controller->Start(std::move(initial_requests)); |
| 191 } | 188 } |
| 192 | 189 |
| 193 active_fetches_.insert( | 190 active_fetches_.insert( |
| 194 std::make_pair(registration_id, std::move(controller))); | 191 std::make_pair(registration_id, std::move(controller))); |
| 195 } | 192 } |
| 196 | 193 |
| 197 void BackgroundFetchContext::DidCompleteJob( | 194 void BackgroundFetchContext::DidCompleteJob( |
| 198 BackgroundFetchJobController* controller) { | 195 BackgroundFetchJobController* controller) { |
| 196 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 197 |
| 199 const BackgroundFetchRegistrationId& registration_id = | 198 const BackgroundFetchRegistrationId& registration_id = |
| 200 controller->registration_id(); | 199 controller->registration_id(); |
| 201 | 200 |
| 202 DCHECK_GT(active_fetches_.count(registration_id), 0u); | 201 DCHECK_GT(active_fetches_.count(registration_id), 0u); |
| 203 switch (controller->state()) { | 202 switch (controller->state()) { |
| 204 case BackgroundFetchJobController::State::ABORTED: | 203 case BackgroundFetchJobController::State::ABORTED: |
| 205 event_dispatcher_->DispatchBackgroundFetchAbortEvent( | 204 event_dispatcher_->DispatchBackgroundFetchAbortEvent( |
| 206 registration_id, | 205 registration_id, |
| 207 base::Bind(&BackgroundFetchContext::DeleteRegistration, this, | 206 base::Bind(&BackgroundFetchContext::DeleteRegistration, this, |
| 208 registration_id, | 207 registration_id, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 222 | 221 |
| 223 NOTREACHED(); | 222 NOTREACHED(); |
| 224 } | 223 } |
| 225 | 224 |
| 226 void BackgroundFetchContext::DidGetSettledFetches( | 225 void BackgroundFetchContext::DidGetSettledFetches( |
| 227 const BackgroundFetchRegistrationId& registration_id, | 226 const BackgroundFetchRegistrationId& registration_id, |
| 228 blink::mojom::BackgroundFetchError error, | 227 blink::mojom::BackgroundFetchError error, |
| 229 bool background_fetch_succeeded, | 228 bool background_fetch_succeeded, |
| 230 std::vector<BackgroundFetchSettledFetch> settled_fetches, | 229 std::vector<BackgroundFetchSettledFetch> settled_fetches, |
| 231 std::vector<std::unique_ptr<BlobHandle>> blob_handles) { | 230 std::vector<std::unique_ptr<BlobHandle>> blob_handles) { |
| 231 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 232 |
| 232 if (error != blink::mojom::BackgroundFetchError::NONE) { | 233 if (error != blink::mojom::BackgroundFetchError::NONE) { |
| 233 DeleteRegistration(registration_id, std::move(blob_handles)); | 234 DeleteRegistration(registration_id, std::move(blob_handles)); |
| 234 return; | 235 return; |
| 235 } | 236 } |
| 236 | 237 |
| 237 // The `backgroundfetched` event will be invoked when all requests in the | 238 // The `backgroundfetched` event will be invoked when all requests in the |
| 238 // registration have completed successfully. In all other cases, the | 239 // registration have completed successfully. In all other cases, the |
| 239 // `backgroundfetchfail` event will be invoked instead. | 240 // `backgroundfetchfail` event will be invoked instead. |
| 240 if (background_fetch_succeeded) { | 241 if (background_fetch_succeeded) { |
| 241 event_dispatcher_->DispatchBackgroundFetchedEvent( | 242 event_dispatcher_->DispatchBackgroundFetchedEvent( |
| 242 registration_id, std::move(settled_fetches), | 243 registration_id, std::move(settled_fetches), |
| 243 base::Bind(&BackgroundFetchContext::DeleteRegistration, this, | 244 base::Bind(&BackgroundFetchContext::DeleteRegistration, this, |
| 244 registration_id, std::move(blob_handles))); | 245 registration_id, std::move(blob_handles))); |
| 245 } else { | 246 } else { |
| 246 event_dispatcher_->DispatchBackgroundFetchFailEvent( | 247 event_dispatcher_->DispatchBackgroundFetchFailEvent( |
| 247 registration_id, std::move(settled_fetches), | 248 registration_id, std::move(settled_fetches), |
| 248 base::Bind(&BackgroundFetchContext::DeleteRegistration, this, | 249 base::Bind(&BackgroundFetchContext::DeleteRegistration, this, |
| 249 registration_id, std::move(blob_handles))); | 250 registration_id, std::move(blob_handles))); |
| 250 } | 251 } |
| 251 } | 252 } |
| 252 | 253 |
| 253 void BackgroundFetchContext::DeleteRegistration( | 254 void BackgroundFetchContext::DeleteRegistration( |
| 254 const BackgroundFetchRegistrationId& registration_id, | 255 const BackgroundFetchRegistrationId& registration_id, |
| 255 const std::vector<std::unique_ptr<BlobHandle>>& blob_handles) { | 256 const std::vector<std::unique_ptr<BlobHandle>>& blob_handles) { |
| 257 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 256 DCHECK_GT(active_fetches_.count(registration_id), 0u); | 258 DCHECK_GT(active_fetches_.count(registration_id), 0u); |
| 257 | 259 |
| 258 // Delete all persistent information associated with the |registration_id|. | 260 // Delete all persistent information associated with the |registration_id|. |
| 259 data_manager_->DeleteRegistration( | 261 data_manager_->DeleteRegistration( |
| 260 registration_id, base::BindOnce(&RecordRegistrationDeletedError)); | 262 registration_id, base::BindOnce(&RecordRegistrationDeletedError)); |
| 261 | 263 |
| 262 // Delete the local state associated with the |registration_id|. | 264 // Delete the local state associated with the |registration_id|. |
| 263 active_fetches_.erase(registration_id); | 265 active_fetches_.erase(registration_id); |
| 264 } | 266 } |
| 265 | 267 |
| 266 } // namespace content | 268 } // namespace content |
| OLD | NEW |