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

Side by Side Diff: content/browser/service_worker/service_worker_cache_writer.cc

Issue 1315443003: ServiceWorkerWriteToCacheJob: refactor (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Proper DoLoop pattern Created 5 years, 3 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
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "content/browser/service_worker/service_worker_cache_writer.h"
6
7 #include <algorithm>
8 #include <string>
9
10 #include "content/browser/appcache/appcache_response.h"
11 #include "content/browser/service_worker/service_worker_disk_cache.h"
12 #include "content/browser/service_worker/service_worker_storage.h"
13
14 namespace {
15
16 const size_t kCopyBufferSize = 16 * 1024;
17
18 // Shim class used to turn always-async functions into async-or-result
19 // functions. See the comments below near ReadInfoHelper.
20 class AsyncOnlyCompletionCallbackAdaptor
21 : public base::RefCounted<AsyncOnlyCompletionCallbackAdaptor> {
22 public:
23 explicit AsyncOnlyCompletionCallbackAdaptor(
24 const net::CompletionCallback& callback)
25 : async_(false), result_(net::ERR_IO_PENDING), callback_(callback) {}
26
27 void set_async(bool async) { async_ = async; }
28 bool async() { return async_; }
29 int result() { return result_; }
30
31 void WrappedCallback(int result) {
32 result_ = result;
33 if (async_)
34 callback_.Run(result);
35 }
36
37 private:
38 friend class base::RefCounted<AsyncOnlyCompletionCallbackAdaptor>;
39 virtual ~AsyncOnlyCompletionCallbackAdaptor() {}
40
41 bool async_;
42 int result_;
43 net::CompletionCallback callback_;
44 };
45
46 } // namespace
47
48 namespace content {
49
50 int ServiceWorkerCacheWriter::DoLoop(int status) {
51 do {
52 switch (state_) {
53 case STATE_START:
54 status = DoStart(status);
55 break;
56 case STATE_READ_HEADERS_FOR_COMPARE:
57 status = DoReadHeadersForCompare(status);
58 break;
59 case STATE_READ_HEADERS_FOR_COMPARE_DONE:
60 status = DoReadHeadersForCompareDone(status);
61 break;
62 case STATE_READ_DATA_FOR_COMPARE:
63 status = DoReadDataForCompare(status);
64 break;
65 case STATE_READ_DATA_FOR_COMPARE_DONE:
66 status = DoReadDataForCompareDone(status);
67 break;
68 case STATE_READ_HEADERS_FOR_COPY:
69 status = DoReadHeadersForCopy(status);
70 break;
71 case STATE_READ_HEADERS_FOR_COPY_DONE:
72 status = DoReadHeadersForCopyDone(status);
73 break;
74 case STATE_READ_DATA_FOR_COPY:
75 status = DoReadDataForCopy(status);
76 break;
77 case STATE_READ_DATA_FOR_COPY_DONE:
78 status = DoReadDataForCopyDone(status);
79 break;
80 case STATE_WRITE_HEADERS_FOR_PASSTHROUGH:
81 status = DoWriteHeadersForPassthrough(status);
82 break;
83 case STATE_WRITE_HEADERS_FOR_PASSTHROUGH_DONE:
84 status = DoWriteHeadersForPassthroughDone(status);
85 break;
86 case STATE_WRITE_DATA_FOR_PASSTHROUGH:
87 status = DoWriteDataForPassthrough(status);
88 break;
89 case STATE_WRITE_DATA_FOR_PASSTHROUGH_DONE:
90 status = DoWriteDataForPassthroughDone(status);
91 break;
92 case STATE_WRITE_HEADERS_FOR_COPY:
93 status = DoWriteHeadersForCopy(status);
94 break;
95 case STATE_WRITE_HEADERS_FOR_COPY_DONE:
96 status = DoWriteHeadersForCopyDone(status);
97 break;
98 case STATE_WRITE_DATA_FOR_COPY:
99 status = DoWriteDataForCopy(status);
100 break;
101 case STATE_WRITE_DATA_FOR_COPY_DONE:
102 status = DoWriteDataForCopyDone(status);
103 break;
104 case STATE_DONE:
105 status = DoDone(status);
106 break;
107 default:
Randy Smith (Not in Mondays) 2015/09/16 18:57:28 Under what circumstances should this happen? Shou
Elly Fong-Jones 2015/09/16 19:25:57 Done.
108 state_ = STATE_DONE;
109 break;
110 }
111 } while (status >= net::OK && state_ != STATE_DONE);
Randy Smith (Not in Mondays) 2015/09/16 18:57:28 In other code I've read, I think of "STATE_NONE" a
Elly Fong-Jones 2015/09/16 19:25:57 Done.
112 io_pending_ = (status == net::ERR_IO_PENDING);
113 return status;
114 }
115
116 ServiceWorkerCacheWriter::ServiceWorkerCacheWriter(
117 const ResponseReaderCreator& reader_creator,
118 const ResponseWriterCreator& writer_creator)
119 : state_(STATE_START),
120 io_pending_(false),
121 comparing_(false),
122 did_replace_(false),
123 reader_creator_(reader_creator),
124 writer_creator_(writer_creator),
125 weak_factory_(this) {}
126
127 ServiceWorkerCacheWriter::~ServiceWorkerCacheWriter() {}
128
129 net::Error ServiceWorkerCacheWriter::MaybeWriteHeaders(
130 HttpResponseInfoIOBuffer* headers,
131 const OnWriteCompleteCallback& callback) {
132 DCHECK(!io_pending_);
133
134 headers_to_write_ = headers;
135 pending_callback_ = callback;
136 DCHECK_EQ(state_, STATE_START);
137 int result = DoLoop(net::OK);
138
139 // Synchronous errors always go to ERR_IO_PENDING.
Randy Smith (Not in Mondays) 2015/09/16 18:57:28 I don't understand this comment?
Elly Fong-Jones 2015/09/16 19:25:57 That's because it makes absolutely no sense. Fixed
140 if (result != net::ERR_IO_PENDING)
141 DCHECK_EQ(state_, STATE_DONE);
142
143 // ERR_IO_PENDING has to have one of the "done" states as the next state.
Randy Smith (Not in Mondays) 2015/09/16 18:57:28 nit, suggestion: Make clear you're not talking abo
Elly Fong-Jones 2015/09/16 19:25:57 Done.
144 if (result == net::ERR_IO_PENDING) {
Randy Smith (Not in Mondays) 2015/09/16 18:57:28 nit, random thought (i.e. not even a suggestion, f
Elly Fong-Jones 2015/09/16 19:25:57 I think it's already clear from getting this messa
Randy Smith (Not in Mondays) 2015/09/16 20:00:48 It's targeting the folks who have a vague idea of
145 DCHECK(state_ == STATE_READ_HEADERS_FOR_COMPARE_DONE ||
146 state_ == STATE_WRITE_HEADERS_FOR_COPY_DONE ||
147 state_ == STATE_WRITE_HEADERS_FOR_PASSTHROUGH_DONE)
148 << "Unexpected state: " << state_;
149 io_pending_ = true;
150 }
151
152 return result >= 0 ? net::OK : static_cast<net::Error>(result);
153 }
154
155 net::Error ServiceWorkerCacheWriter::MaybeWriteData(
156 net::IOBuffer* buf,
157 size_t buf_size,
158 const OnWriteCompleteCallback& callback) {
159 DCHECK(!io_pending_);
160
161 data_to_write_ = buf;
162 len_to_write_ = buf_size;
163 pending_callback_ = callback;
164
165 if (comparing_)
166 state_ = STATE_READ_DATA_FOR_COMPARE;
167 else
168 state_ = STATE_WRITE_DATA_FOR_PASSTHROUGH;
169
170 int result = DoLoop(net::OK);
171
172 // Synchronous completions are always STATE_DONE.
173 if (result != net::ERR_IO_PENDING)
174 DCHECK_EQ(state_, STATE_DONE);
175
176 // Asynchronous completion means the state machine must be waiting in one of
177 // the Done states for an IO operation to complete:
178 if (result == net::ERR_IO_PENDING) {
179 // Note that STATE_READ_HEADERS_FOR_COMPARE_DONE is excluded because the
180 // headers are compared in MaybeWriteHeaders, not here, and
181 // STATE_WRITE_HEADERS_FOR_PASSTHROUGH_DONE is excluded because that write
182 // is done by MaybeWriteHeaders.
183 DCHECK(state_ == STATE_READ_DATA_FOR_COMPARE_DONE ||
184 state_ == STATE_READ_HEADERS_FOR_COPY_DONE ||
185 state_ == STATE_READ_DATA_FOR_COPY_DONE ||
186 state_ == STATE_WRITE_HEADERS_FOR_COPY_DONE ||
187 state_ == STATE_WRITE_DATA_FOR_COPY_DONE ||
188 state_ == STATE_WRITE_DATA_FOR_PASSTHROUGH_DONE)
189 << "Unexpected state: " << state_;
190 }
191
192 return result >= 0 ? net::OK : static_cast<net::Error>(result);
193 }
194
195 int ServiceWorkerCacheWriter::DoStart(int result) {
196 bytes_written_ = 0;
197 compare_reader_ = reader_creator_.Run();
198 if (compare_reader_.get()) {
199 state_ = STATE_READ_HEADERS_FOR_COMPARE;
200 comparing_ = true;
201 } else {
202 // No existing reader, just write the headers back directly.
203 state_ = STATE_WRITE_HEADERS_FOR_PASSTHROUGH;
204 comparing_ = false;
205 }
206 return net::OK;
207 }
208
209 int ServiceWorkerCacheWriter::DoReadHeadersForCompare(int result) {
210 DCHECK(headers_to_write_);
211
212 headers_to_read_ = new HttpResponseInfoIOBuffer;
213 state_ = STATE_READ_HEADERS_FOR_COMPARE_DONE;
214 return ReadInfoHelper(compare_reader_, headers_to_read_.get());
215 }
216
217 int ServiceWorkerCacheWriter::DoReadHeadersForCompareDone(int result) {
218 if (result < 0) {
219 state_ = STATE_DONE;
220 return result;
221 }
222 cached_length_ = headers_to_read_->response_data_size;
223 bytes_compared_ = 0;
224 state_ = STATE_DONE;
225 return net::OK;
226 }
227
228 int ServiceWorkerCacheWriter::DoReadDataForCompare(int result) {
229 DCHECK(data_to_write_);
230
231 data_to_read_ = new net::IOBuffer(len_to_write_);
232 len_to_read_ = len_to_write_;
233 state_ = STATE_READ_DATA_FOR_COMPARE_DONE;
234 compare_offset_ = 0;
235 // If this was an EOF, don't issue a read.
236 if (len_to_write_ > 0)
237 result = ReadDataHelper(compare_reader_, data_to_read_.get(), len_to_read_);
238 return result;
239 }
240
241 int ServiceWorkerCacheWriter::DoReadDataForCompareDone(int result) {
242 DCHECK(data_to_read_);
243 DCHECK(data_to_write_);
244 DCHECK_EQ(len_to_read_, len_to_write_);
245 DCHECK_LE(result + compare_offset_, static_cast<size_t>(len_to_write_));
246
247 if (result < 0) {
248 state_ = STATE_DONE;
249 return result;
250 }
251
252 // Premature EOF while reading the service worker script cache data to
253 // compare. Fail the comparison.
254 if (result == 0 && len_to_write_ != 0) {
255 comparing_ = false;
256 state_ = STATE_READ_HEADERS_FOR_COPY;
257 return net::OK;
258 }
259
260 // Compare the data from the ServiceWorker script cache to the data from the
261 // network.
262 if (memcmp(data_to_read_->data(), data_to_write_->data() + compare_offset_,
263 result)) {
264 // Data mismatched. This method already validated that all the bytes through
265 // |bytes_compared_| were identical, so copy the first |bytes_compared_|
266 // over, then start writing network data back after the changed point.
267 comparing_ = false;
268 state_ = STATE_READ_HEADERS_FOR_COPY;
269 return net::OK;
270 }
271
272 compare_offset_ += result;
273
274 // This is a little bit tricky. It is possible that not enough data was read
275 // to finish comparing the entire block of data from the network (which is
276 // kept in len_to_write_), so this method may need to issue another read and
277 // return to this state.
278 //
279 // Compare isn't complete yet. Issue another read for the remaining data. Note
280 // that this reuses the same IOBuffer.
281 if (compare_offset_ < static_cast<size_t>(len_to_read_)) {
282 state_ = STATE_READ_DATA_FOR_COMPARE_DONE;
283 return ReadDataHelper(compare_reader_, data_to_read_.get(),
284 len_to_read_ - compare_offset_);
285 }
286
287 // Cached entry is longer than the network entry but the prefix matches. Copy
288 // just the prefix.
289 if (len_to_read_ == 0 && bytes_compared_ + compare_offset_ < cached_length_) {
290 comparing_ = false;
291 state_ = STATE_READ_HEADERS_FOR_COPY;
292 return net::OK;
293 }
294
295 // bytes_compared_ only gets incremented when a full block is compared, to
296 // avoid having to use only parts of the buffered network data.
297 bytes_compared_ += result;
298 state_ = STATE_DONE;
299 return net::OK;
300 }
301
302 int ServiceWorkerCacheWriter::DoReadHeadersForCopy(int result) {
303 bytes_copied_ = 0;
304 copy_reader_ = reader_creator_.Run();
305 headers_to_read_ = new HttpResponseInfoIOBuffer;
306 data_to_copy_ = new net::IOBuffer(kCopyBufferSize);
307 state_ = STATE_READ_HEADERS_FOR_COPY_DONE;
308 return ReadInfoHelper(copy_reader_, headers_to_read_.get());
309 }
310
311 int ServiceWorkerCacheWriter::DoReadHeadersForCopyDone(int result) {
312 if (result < 0) {
313 state_ = STATE_DONE;
314 return result;
315 }
316 state_ = STATE_WRITE_HEADERS_FOR_COPY;
317 return net::OK;
318 }
319
320 // Write the just-read headers back to the cache.
321 // Note that this method must create |writer_|, since the only paths to this
322 // state never create a writer.
323 // Also note that this *discards* the read headers and replaces them with the
324 // net headers.
325 int ServiceWorkerCacheWriter::DoWriteHeadersForCopy(int result) {
326 DCHECK(!writer_);
327 writer_ = writer_creator_.Run();
328 state_ = STATE_WRITE_HEADERS_FOR_COPY_DONE;
329 return WriteInfoHelper(writer_, headers_to_write_.get());
330 }
331
332 int ServiceWorkerCacheWriter::DoWriteHeadersForCopyDone(int result) {
333 if (result < 0) {
334 state_ = STATE_DONE;
335 return result;
336 }
337 state_ = STATE_READ_DATA_FOR_COPY;
338 return net::OK;
339 }
340
341 int ServiceWorkerCacheWriter::DoReadDataForCopy(int result) {
342 size_t to_read = std::min(kCopyBufferSize, bytes_compared_ - bytes_copied_);
343 // At this point, all compared bytes have been read. Currently
344 // |data_to_write_| and |len_to_write_| hold the chunk of network input that
345 // caused the comparison failure, so those need to be written back and this
346 // object needs to go into passthrough mode.
347 if (to_read == 0) {
348 state_ = STATE_WRITE_DATA_FOR_PASSTHROUGH;
349 return net::OK;
350 }
351 state_ = STATE_READ_DATA_FOR_COPY_DONE;
352 return ReadDataHelper(copy_reader_, data_to_copy_.get(), to_read);
353 }
354
355 int ServiceWorkerCacheWriter::DoReadDataForCopyDone(int result) {
356 if (result < 0) {
357 state_ = STATE_DONE;
358 return result;
359 }
360 state_ = STATE_WRITE_DATA_FOR_COPY;
361 return result;
362 }
363
364 int ServiceWorkerCacheWriter::DoWriteDataForCopy(int result) {
365 state_ = STATE_WRITE_DATA_FOR_COPY_DONE;
366 DCHECK_GT(result, 0);
367 return WriteDataHelper(writer_, data_to_copy_.get(), result);
368 }
369
370 int ServiceWorkerCacheWriter::DoWriteDataForCopyDone(int result) {
371 if (result < 0) {
372 state_ = STATE_DONE;
373 return result;
374 }
375 bytes_written_ += result;
376 bytes_copied_ += result;
377 state_ = STATE_READ_DATA_FOR_COPY;
378 return result;
379 }
380
381 int ServiceWorkerCacheWriter::DoWriteHeadersForPassthrough(int result) {
382 writer_ = writer_creator_.Run();
383 state_ = STATE_WRITE_HEADERS_FOR_PASSTHROUGH_DONE;
384 return WriteInfoHelper(writer_, headers_to_write_.get());
385 }
386
387 int ServiceWorkerCacheWriter::DoWriteHeadersForPassthroughDone(int result) {
388 state_ = STATE_DONE;
389 return net::OK;
390 }
391
392 int ServiceWorkerCacheWriter::DoWriteDataForPassthrough(int result) {
393 state_ = STATE_WRITE_DATA_FOR_PASSTHROUGH_DONE;
394 if (len_to_write_ > 0)
395 result = WriteDataHelper(writer_, data_to_write_.get(), len_to_write_);
396 return result;
397 }
398
399 int ServiceWorkerCacheWriter::DoWriteDataForPassthroughDone(int result) {
400 if (result < 0) {
401 state_ = STATE_DONE;
402 return result;
403 }
404 bytes_written_ += result;
405 state_ = STATE_DONE;
406 return net::OK;
407 }
408
409 int ServiceWorkerCacheWriter::DoDone(int result) {
410 state_ = STATE_DONE;
411 return net::OK;
412 }
413
414 // These helpers adapt the AppCache "always use the callback" pattern to the
415 // //net "only use the callback for async" pattern using
416 // AsyncCompletionCallbackAdaptor.
417 //
418 // Specifically, these methods return result codes directly for synchronous
419 // completions, and only run their callback (which is AsyncDoLoop) for
420 // asynchronous completions.
421
422 int ServiceWorkerCacheWriter::ReadInfoHelper(
423 const scoped_ptr<ServiceWorkerResponseReader>& reader,
424 HttpResponseInfoIOBuffer* buf) {
425 net::CompletionCallback run_callback = base::Bind(
426 &ServiceWorkerCacheWriter::AsyncDoLoop, weak_factory_.GetWeakPtr());
427 scoped_refptr<AsyncOnlyCompletionCallbackAdaptor> adaptor(
428 new AsyncOnlyCompletionCallbackAdaptor(run_callback));
429 reader->ReadInfo(
430 buf, base::Bind(&AsyncOnlyCompletionCallbackAdaptor::WrappedCallback,
431 adaptor));
432 adaptor->set_async(true);
433 return adaptor->result();
434 }
435
436 int ServiceWorkerCacheWriter::ReadDataHelper(
437 const scoped_ptr<ServiceWorkerResponseReader>& reader,
438 net::IOBuffer* buf,
439 int buf_len) {
440 net::CompletionCallback run_callback = base::Bind(
441 &ServiceWorkerCacheWriter::AsyncDoLoop, weak_factory_.GetWeakPtr());
442 scoped_refptr<AsyncOnlyCompletionCallbackAdaptor> adaptor(
443 new AsyncOnlyCompletionCallbackAdaptor(run_callback));
444 reader->ReadData(
445 buf, buf_len,
446 base::Bind(&AsyncOnlyCompletionCallbackAdaptor::WrappedCallback,
447 adaptor));
448 adaptor->set_async(true);
449 return adaptor->result();
450 }
451
452 int ServiceWorkerCacheWriter::WriteInfoHelper(
453 const scoped_ptr<ServiceWorkerResponseWriter>& writer,
454 HttpResponseInfoIOBuffer* buf) {
455 did_replace_ = true;
456 net::CompletionCallback run_callback = base::Bind(
457 &ServiceWorkerCacheWriter::AsyncDoLoop, weak_factory_.GetWeakPtr());
458 scoped_refptr<AsyncOnlyCompletionCallbackAdaptor> adaptor(
459 new AsyncOnlyCompletionCallbackAdaptor(run_callback));
460 writer->WriteInfo(
461 buf, base::Bind(&AsyncOnlyCompletionCallbackAdaptor::WrappedCallback,
462 adaptor));
463 adaptor->set_async(true);
464 return adaptor->result();
465 }
466
467 int ServiceWorkerCacheWriter::WriteDataHelper(
468 const scoped_ptr<ServiceWorkerResponseWriter>& writer,
469 net::IOBuffer* buf,
470 int buf_len) {
471 net::CompletionCallback run_callback = base::Bind(
472 &ServiceWorkerCacheWriter::AsyncDoLoop, weak_factory_.GetWeakPtr());
473 scoped_refptr<AsyncOnlyCompletionCallbackAdaptor> adaptor(
474 new AsyncOnlyCompletionCallbackAdaptor(run_callback));
475 writer->WriteData(
476 buf, buf_len,
477 base::Bind(&AsyncOnlyCompletionCallbackAdaptor::WrappedCallback,
478 adaptor));
479 adaptor->set_async(true);
480 return adaptor->result();
481 }
482
483 void ServiceWorkerCacheWriter::AsyncDoLoop(int result) {
484 result = DoLoop(result);
485 // If the result is ERR_IO_PENDING, the pending callback will be run by a
486 // later invocation of AsyncDoLoop.
487 if (result != net::ERR_IO_PENDING) {
488 OnWriteCompleteCallback callback = pending_callback_;
489 pending_callback_.Reset();
490 net::Error error = result >= 0 ? net::OK : static_cast<net::Error>(result);
491 io_pending_ = false;
492 callback.Run(error);
493 }
494 }
495
496 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698