Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(85)

Side by Side Diff: content/browser/background_fetch/background_fetch_context.cc

Issue 2786783002: Dispatch a bare Service Worker event for a finished Background Fetch (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_job_controller.h" 10 #include "content/browser/background_fetch/background_fetch_job_controller.h"
10 #include "content/browser/background_fetch/background_fetch_registration_id.h" 11 #include "content/browser/background_fetch/background_fetch_registration_id.h"
11 #include "content/browser/service_worker/service_worker_context_wrapper.h" 12 #include "content/browser/service_worker/service_worker_context_wrapper.h"
12 #include "content/browser/storage_partition_impl.h" 13 #include "content/browser/storage_partition_impl.h"
14 #include "content/public/browser/blob_handle.h"
13 #include "content/public/browser/browser_context.h" 15 #include "content/public/browser/browser_context.h"
14 #include "net/url_request/url_request_context_getter.h" 16 #include "net/url_request/url_request_context_getter.h"
15 #include "url/origin.h" 17 #include "url/origin.h"
16 18
17 namespace content { 19 namespace content {
18 20
21 namespace {
22
23 // Records the |error| status issued by the DataManager after it was requested
24 // to create and store a new Background Fetch registration.
25 void RecordRegistrationCreatedError(blink::mojom::BackgroundFetchError error) {
26 // TODO(peter): Add UMA.
27 }
28
29 // Records the |error| status issued by the DataManager after the storage
30 // associated with a registration has been completely deleted.
31 void RecordRegistrationDeletedError(blink::mojom::BackgroundFetchError error) {
32 // TODO(peter): Add UMA.
33 }
34
35 } // namespace
36
19 BackgroundFetchContext::BackgroundFetchContext( 37 BackgroundFetchContext::BackgroundFetchContext(
20 BrowserContext* browser_context, 38 BrowserContext* browser_context,
21 StoragePartitionImpl* storage_partition, 39 StoragePartitionImpl* storage_partition,
22 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context) 40 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context)
23 : browser_context_(browser_context), 41 : browser_context_(browser_context),
24 service_worker_context_(service_worker_context), 42 data_manager_(
25 background_fetch_data_manager_( 43 base::MakeUnique<BackgroundFetchDataManager>(browser_context)),
26 base::MakeUnique<BackgroundFetchDataManager>(browser_context)) { 44 event_dispatcher_(base::MakeUnique<BackgroundFetchEventDispatcher>(
45 std::move(service_worker_context))) {
27 DCHECK_CURRENTLY_ON(BrowserThread::UI); 46 DCHECK_CURRENTLY_ON(BrowserThread::UI);
28 download_manager_ = BrowserContext::GetDownloadManager(browser_context_); 47 download_manager_ = BrowserContext::GetDownloadManager(browser_context_);
29 request_context_ = 48 request_context_ =
30 make_scoped_refptr(storage_partition->GetURLRequestContext()); 49 make_scoped_refptr(storage_partition->GetURLRequestContext());
31 } 50 }
32 51
33 BackgroundFetchContext::~BackgroundFetchContext() { 52 BackgroundFetchContext::~BackgroundFetchContext() {
34 DCHECK_CURRENTLY_ON(BrowserThread::UI); 53 DCHECK_CURRENTLY_ON(BrowserThread::UI);
35 } 54 }
36 55
37 void BackgroundFetchContext::Shutdown() { 56 void BackgroundFetchContext::Shutdown() {
38 DCHECK_CURRENTLY_ON(BrowserThread::UI); 57 DCHECK_CURRENTLY_ON(BrowserThread::UI);
39 BrowserThread::PostTask( 58 BrowserThread::PostTask(
40 BrowserThread::IO, FROM_HERE, 59 BrowserThread::IO, FROM_HERE,
41 base::Bind(&BackgroundFetchContext::ShutdownOnIO, this)); 60 base::Bind(&BackgroundFetchContext::ShutdownOnIO, this));
42 } 61 }
43 62
44 void BackgroundFetchContext::ShutdownOnIO() { 63 void BackgroundFetchContext::ShutdownOnIO() {
45 DCHECK_CURRENTLY_ON(BrowserThread::IO); 64 DCHECK_CURRENTLY_ON(BrowserThread::IO);
46 active_fetches_.clear(); 65 active_fetches_.clear();
47 } 66 }
48 67
49 void BackgroundFetchContext::StartFetch( 68 void BackgroundFetchContext::StartFetch(
50 const BackgroundFetchRegistrationId& registration_id, 69 const BackgroundFetchRegistrationId& registration_id,
51 const std::vector<ServiceWorkerFetchRequest>& requests, 70 const std::vector<ServiceWorkerFetchRequest>& requests,
52 const BackgroundFetchOptions& options, 71 const BackgroundFetchOptions& options,
53 const blink::mojom::BackgroundFetchService::FetchCallback& callback) { 72 const blink::mojom::BackgroundFetchService::FetchCallback& callback) {
54 DCHECK_CURRENTLY_ON(BrowserThread::IO); 73 DCHECK_CURRENTLY_ON(BrowserThread::IO);
55 background_fetch_data_manager_->CreateRegistration( 74 data_manager_->CreateRegistration(
56 registration_id, requests, options, 75 registration_id, requests, options,
57 base::BindOnce(&BackgroundFetchContext::DidCreateRegistration, this, 76 base::BindOnce(&BackgroundFetchContext::DidCreateRegistration, this,
58 registration_id, options, callback)); 77 registration_id, options, callback));
59 } 78 }
60 79
61 void BackgroundFetchContext::DidCreateRegistration( 80 void BackgroundFetchContext::DidCreateRegistration(
62 const BackgroundFetchRegistrationId& registration_id, 81 const BackgroundFetchRegistrationId& registration_id,
63 const BackgroundFetchOptions& options, 82 const BackgroundFetchOptions& options,
64 const blink::mojom::BackgroundFetchService::FetchCallback& callback, 83 const blink::mojom::BackgroundFetchService::FetchCallback& callback,
65 blink::mojom::BackgroundFetchError error, 84 blink::mojom::BackgroundFetchError error,
66 std::vector<BackgroundFetchRequestInfo> initial_requests) { 85 std::vector<BackgroundFetchRequestInfo> initial_requests) {
86 RecordRegistrationCreatedError(error);
67 if (error != blink::mojom::BackgroundFetchError::NONE) { 87 if (error != blink::mojom::BackgroundFetchError::NONE) {
68 callback.Run(error, base::nullopt /* registration */); 88 callback.Run(error, base::nullopt /* registration */);
69 return; 89 return;
70 } 90 }
71 91
72 // Create the BackgroundFetchJobController, which will do the actual fetching. 92 // Create the BackgroundFetchJobController, which will do the actual fetching.
73 CreateController(registration_id, options, std::move(initial_requests)); 93 CreateController(registration_id, options, std::move(initial_requests));
74 94
75 // Create the BackgroundFetchRegistration the renderer process will receive, 95 // Create the BackgroundFetchRegistration the renderer process will receive,
76 // which enables it to resolve the promise telling the developer it worked. 96 // which enables it to resolve the promise telling the developer it worked.
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 139
120 return controller; 140 return controller;
121 } 141 }
122 142
123 void BackgroundFetchContext::CreateController( 143 void BackgroundFetchContext::CreateController(
124 const BackgroundFetchRegistrationId& registration_id, 144 const BackgroundFetchRegistrationId& registration_id,
125 const BackgroundFetchOptions& options, 145 const BackgroundFetchOptions& options,
126 std::vector<BackgroundFetchRequestInfo> initial_requests) { 146 std::vector<BackgroundFetchRequestInfo> initial_requests) {
127 std::unique_ptr<BackgroundFetchJobController> controller = 147 std::unique_ptr<BackgroundFetchJobController> controller =
128 base::MakeUnique<BackgroundFetchJobController>( 148 base::MakeUnique<BackgroundFetchJobController>(
129 registration_id, options, background_fetch_data_manager_.get(), 149 registration_id, options, data_manager_.get(), download_manager_,
130 download_manager_, request_context_, 150 request_context_,
131 base::BindOnce(&BackgroundFetchContext::DidCompleteJob, this)); 151 base::BindOnce(&BackgroundFetchContext::DidCompleteJob, this));
132 152
133 // TODO(peter): We should actually be able to use Background Fetch in layout 153 // TODO(peter): We should actually be able to use Background Fetch in layout
134 // tests. That requires a download manager and a request context. 154 // tests. That requires a download manager and a request context.
135 if (download_manager_ && request_context_) { 155 if (download_manager_ && request_context_) {
136 // Start fetching the |initial_requests| immediately. At some point in the 156 // Start fetching the |initial_requests| immediately. At some point in the
137 // future we may want a more elaborate scheduling mechanism here. 157 // future we may want a more elaborate scheduling mechanism here.
138 controller->Start(std::move(initial_requests)); 158 controller->Start(std::move(initial_requests));
139 } 159 }
140 160
141 active_fetches_.insert( 161 active_fetches_.insert(
142 std::make_pair(registration_id, std::move(controller))); 162 std::make_pair(registration_id, std::move(controller)));
143 } 163 }
144 164
145 void BackgroundFetchContext::DidCompleteJob( 165 void BackgroundFetchContext::DidCompleteJob(
146 BackgroundFetchJobController* controller) { 166 BackgroundFetchJobController* controller) {
147 const BackgroundFetchRegistrationId& registration_id = 167 const BackgroundFetchRegistrationId& registration_id =
148 controller->registration_id(); 168 controller->registration_id();
149 169
150 DCHECK_GT(active_fetches_.count(registration_id), 0u); 170 DCHECK_GT(active_fetches_.count(registration_id), 0u);
151 171
152 if (controller->state() == BackgroundFetchJobController::State::COMPLETED) { 172 // The `backgroundfetched` and/or `backgroundfetchfail` event will only be
153 // TODO(peter): Dispatch the `backgroundfetched` or `backgroundfetchfail` 173 // invoked for Background Fetch jobs which have been completed.
154 // event to the Service Worker to inform the developer. 174 if (controller->state() != BackgroundFetchJobController::State::COMPLETED) {
harkness 2017/03/29 22:12:33 We need to call the aborted event if that's the st
Peter Beverloo 2017/03/29 22:20:28 As I understand it, `backgroundfetchabort` is for
Peter Beverloo 2017/03/30 10:19:37 https://github.com/WICG/background-fetch#reacting-
harkness 2017/03/30 14:15:28 Acknowledged.
175 DeleteRegistration(registration_id);
176 return;
155 } 177 }
156 178
179 // Get the sequence of settled fetches from the data manager.
180 data_manager_->GetSettledFetchesForRegistration(
181 registration_id,
182 base::BindOnce(&BackgroundFetchContext::DidGetSettledFetches, this,
183 registration_id));
184 }
185
186 void BackgroundFetchContext::DidGetSettledFetches(
187 const BackgroundFetchRegistrationId& registration_id,
188 blink::mojom::BackgroundFetchError error,
189 std::vector<BackgroundFetchSettledFetch> settled_fetches,
190 std::vector<std::unique_ptr<BlobHandle>> blob_handles) {
191 if (error != blink::mojom::BackgroundFetchError::NONE) {
192 DeleteRegistration(registration_id);
193 return;
194 }
195
196 // TODO(peter): Distinguish between the `backgroundfetched` and
197 // `backgroundfetchfail` events based on the status code of all fetches. We
198 // don't populate that field yet, so always assume it's successful for now.
199
200 event_dispatcher_->DispatchBackgroundFetchedEvent(
201 registration_id, std::move(settled_fetches),
202 base::Bind(&BackgroundFetchContext::DeleteRegistration, this,
203 registration_id));
204 }
205
206 void BackgroundFetchContext::DeleteRegistration(
207 const BackgroundFetchRegistrationId& registration_id) {
208 DCHECK_GT(active_fetches_.count(registration_id), 0u);
209
210 // Delete all persistent information associated with the |registration_id|.
211 data_manager_->DeleteRegistration(
212 registration_id, base::BindOnce(&RecordRegistrationDeletedError));
213
214 // Delete the local state associated with the |registration_id|.
157 active_fetches_.erase(registration_id); 215 active_fetches_.erase(registration_id);
158 } 216 }
159 217
160 } // namespace content 218 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698