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

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: Dispatch a bare Service Worker event for a finished Background Fetch 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 request_context_ = 47 request_context_ =
29 make_scoped_refptr(storage_partition->GetURLRequestContext()); 48 make_scoped_refptr(storage_partition->GetURLRequestContext());
30 } 49 }
31 50
32 BackgroundFetchContext::~BackgroundFetchContext() { 51 BackgroundFetchContext::~BackgroundFetchContext() {
33 DCHECK_CURRENTLY_ON(BrowserThread::UI); 52 DCHECK_CURRENTLY_ON(BrowserThread::UI);
34 } 53 }
35 54
36 void BackgroundFetchContext::Shutdown() { 55 void BackgroundFetchContext::Shutdown() {
37 DCHECK_CURRENTLY_ON(BrowserThread::UI); 56 DCHECK_CURRENTLY_ON(BrowserThread::UI);
38 BrowserThread::PostTask( 57 BrowserThread::PostTask(
39 BrowserThread::IO, FROM_HERE, 58 BrowserThread::IO, FROM_HERE,
40 base::Bind(&BackgroundFetchContext::ShutdownOnIO, this)); 59 base::Bind(&BackgroundFetchContext::ShutdownOnIO, this));
41 } 60 }
42 61
43 void BackgroundFetchContext::ShutdownOnIO() { 62 void BackgroundFetchContext::ShutdownOnIO() {
44 DCHECK_CURRENTLY_ON(BrowserThread::IO); 63 DCHECK_CURRENTLY_ON(BrowserThread::IO);
45 active_fetches_.clear(); 64 active_fetches_.clear();
46 } 65 }
47 66
48 void BackgroundFetchContext::StartFetch( 67 void BackgroundFetchContext::StartFetch(
49 const BackgroundFetchRegistrationId& registration_id, 68 const BackgroundFetchRegistrationId& registration_id,
50 const std::vector<ServiceWorkerFetchRequest>& requests, 69 const std::vector<ServiceWorkerFetchRequest>& requests,
51 const BackgroundFetchOptions& options, 70 const BackgroundFetchOptions& options,
52 const blink::mojom::BackgroundFetchService::FetchCallback& callback) { 71 const blink::mojom::BackgroundFetchService::FetchCallback& callback) {
53 DCHECK_CURRENTLY_ON(BrowserThread::IO); 72 DCHECK_CURRENTLY_ON(BrowserThread::IO);
54 background_fetch_data_manager_->CreateRegistration( 73 data_manager_->CreateRegistration(
55 registration_id, requests, options, 74 registration_id, requests, options,
56 base::BindOnce(&BackgroundFetchContext::DidCreateRegistration, this, 75 base::BindOnce(&BackgroundFetchContext::DidCreateRegistration, this,
57 registration_id, options, callback)); 76 registration_id, options, callback));
58 } 77 }
59 78
60 void BackgroundFetchContext::DidCreateRegistration( 79 void BackgroundFetchContext::DidCreateRegistration(
61 const BackgroundFetchRegistrationId& registration_id, 80 const BackgroundFetchRegistrationId& registration_id,
62 const BackgroundFetchOptions& options, 81 const BackgroundFetchOptions& options,
63 const blink::mojom::BackgroundFetchService::FetchCallback& callback, 82 const blink::mojom::BackgroundFetchService::FetchCallback& callback,
64 blink::mojom::BackgroundFetchError error, 83 blink::mojom::BackgroundFetchError error,
65 std::vector<BackgroundFetchRequestInfo> initial_requests) { 84 std::vector<BackgroundFetchRequestInfo> initial_requests) {
85 RecordRegistrationCreatedError(error);
66 if (error != blink::mojom::BackgroundFetchError::NONE) { 86 if (error != blink::mojom::BackgroundFetchError::NONE) {
67 callback.Run(error, base::nullopt /* registration */); 87 callback.Run(error, base::nullopt /* registration */);
68 return; 88 return;
69 } 89 }
70 90
71 // Create the BackgroundFetchJobController, which will do the actual fetching. 91 // Create the BackgroundFetchJobController, which will do the actual fetching.
72 CreateController(registration_id, options, std::move(initial_requests)); 92 CreateController(registration_id, options, std::move(initial_requests));
73 93
74 // Create the BackgroundFetchRegistration the renderer process will receive, 94 // Create the BackgroundFetchRegistration the renderer process will receive,
75 // which enables it to resolve the promise telling the developer it worked. 95 // which enables it to resolve the promise telling the developer it worked.
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 138
119 return controller; 139 return controller;
120 } 140 }
121 141
122 void BackgroundFetchContext::CreateController( 142 void BackgroundFetchContext::CreateController(
123 const BackgroundFetchRegistrationId& registration_id, 143 const BackgroundFetchRegistrationId& registration_id,
124 const BackgroundFetchOptions& options, 144 const BackgroundFetchOptions& options,
125 std::vector<BackgroundFetchRequestInfo> initial_requests) { 145 std::vector<BackgroundFetchRequestInfo> initial_requests) {
126 std::unique_ptr<BackgroundFetchJobController> controller = 146 std::unique_ptr<BackgroundFetchJobController> controller =
127 base::MakeUnique<BackgroundFetchJobController>( 147 base::MakeUnique<BackgroundFetchJobController>(
128 registration_id, options, background_fetch_data_manager_.get(), 148 registration_id, options, data_manager_.get(), browser_context_,
129 browser_context_, request_context_, 149 request_context_,
130 base::BindOnce(&BackgroundFetchContext::DidCompleteJob, this)); 150 base::BindOnce(&BackgroundFetchContext::DidCompleteJob, this));
131 151
132 // TODO(peter): We should actually be able to use Background Fetch in layout 152 // TODO(peter): We should actually be able to use Background Fetch in layout
133 // tests. That requires a download manager and a request context. 153 // tests. That requires a download manager and a request context.
134 if (request_context_) { 154 if (request_context_) {
135 // Start fetching the |initial_requests| immediately. At some point in the 155 // Start fetching the |initial_requests| immediately. At some point in the
136 // future we may want a more elaborate scheduling mechanism here. 156 // future we may want a more elaborate scheduling mechanism here.
137 controller->Start(std::move(initial_requests)); 157 controller->Start(std::move(initial_requests));
138 } 158 }
139 159
140 active_fetches_.insert( 160 active_fetches_.insert(
141 std::make_pair(registration_id, std::move(controller))); 161 std::make_pair(registration_id, std::move(controller)));
142 } 162 }
143 163
144 void BackgroundFetchContext::DidCompleteJob( 164 void BackgroundFetchContext::DidCompleteJob(
145 BackgroundFetchJobController* controller) { 165 BackgroundFetchJobController* controller) {
146 const BackgroundFetchRegistrationId& registration_id = 166 const BackgroundFetchRegistrationId& registration_id =
147 controller->registration_id(); 167 controller->registration_id();
148 168
149 DCHECK_GT(active_fetches_.count(registration_id), 0u); 169 DCHECK_GT(active_fetches_.count(registration_id), 0u);
150 170
151 if (controller->state() == BackgroundFetchJobController::State::COMPLETED) { 171 // TODO(peter): Fire `backgroundfetchabort` if the |controller|'s state is
152 // TODO(peter): Dispatch the `backgroundfetched` or `backgroundfetchfail` 172 // ABORTED, which does not require a sequence of the settled fetches.
153 // event to the Service Worker to inform the developer. 173
174 // The `backgroundfetched` and/or `backgroundfetchfail` event will only be
175 // invoked for Background Fetch jobs which have been completed.
176 if (controller->state() != BackgroundFetchJobController::State::COMPLETED) {
177 DeleteRegistration(registration_id);
178 return;
154 } 179 }
155 180
181 // Get the sequence of settled fetches from the data manager.
182 data_manager_->GetSettledFetchesForRegistration(
183 registration_id,
184 base::BindOnce(&BackgroundFetchContext::DidGetSettledFetches, this,
185 registration_id));
186 }
187
188 void BackgroundFetchContext::DidGetSettledFetches(
189 const BackgroundFetchRegistrationId& registration_id,
190 blink::mojom::BackgroundFetchError error,
191 std::vector<BackgroundFetchSettledFetch> settled_fetches,
192 std::vector<std::unique_ptr<BlobHandle>> blob_handles) {
193 if (error != blink::mojom::BackgroundFetchError::NONE) {
194 DeleteRegistration(registration_id);
195 return;
196 }
197
198 // TODO(peter): Distinguish between the `backgroundfetched` and
199 // `backgroundfetchfail` events based on the status code of all fetches. We
200 // don't populate that field yet, so always assume it's successful for now.
201
202 event_dispatcher_->DispatchBackgroundFetchedEvent(
203 registration_id, std::move(settled_fetches),
204 base::Bind(&BackgroundFetchContext::DeleteRegistration, this,
205 registration_id));
206 }
207
208 void BackgroundFetchContext::DeleteRegistration(
209 const BackgroundFetchRegistrationId& registration_id) {
210 DCHECK_GT(active_fetches_.count(registration_id), 0u);
211
212 // Delete all persistent information associated with the |registration_id|.
213 data_manager_->DeleteRegistration(
214 registration_id, base::BindOnce(&RecordRegistrationDeletedError));
215
216 // Delete the local state associated with the |registration_id|.
156 active_fetches_.erase(registration_id); 217 active_fetches_.erase(registration_id);
157 } 218 }
158 219
159 } // namespace content 220 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698