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

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: Annotate virtual methods 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 // States for the state machine.
19 //
20 // The state machine flows roughly like this: if there is no existing cache
21 // entry, incoming headers and data are written directly back to the cache
22 // ("passthrough mode", the PASSTHROUGH states). If there is an existing cache
23 // entry, incoming headers and data are compared to the existing cache entry
24 // ("compare mode", the COMPARE states); if at any point the incoming
25 // headers/data are not equal to the cached headers/data, this class copies the
26 // cached data up to the point where the incoming data and the cached data
27 // diverged ("copy mode", the COPY states), then switches to "passthrough mode"
28 // to write the remainder of the incoming data. The overall effect is to avoid
29 // rewriting the cache entry if the incoming data is identical to the cached
30 // data.
Randy Smith (Not in Mondays) 2015/09/13 20:56:36 This is an awesome explanatory comment, with a gol
Elly Fong-Jones 2015/09/15 15:08:24 Acknowledged.
31 enum {
32 STATE_START,
33 // Control flows linearly through these four states, then loops from
34 // READ_DATA_FOR_COMPARE_DONE to READ_DATA_FOR_COMPARE, or exits to
35 // READ_HEADERS_FOR_COPY.
36 STATE_READ_HEADERS_FOR_COMPARE,
37 STATE_READ_HEADERS_FOR_COMPARE_DONE,
38 STATE_READ_DATA_FOR_COMPARE,
39 STATE_READ_DATA_FOR_COMPARE_DONE,
40
41 // Control flows linearly through these states, with each pass from
42 // READ_DATA_FOR_COPY to WRITE_DATA_FOR_COPY_DONE copying one block of data
43 // at a time. Control loops from WRITE_DATA_FOR_COPY_DONE back to
44 // READ_DATA_FOR_COPY if there is more data to copy, or exits to
45 // WRITE_DATA_FOR_PASSTHROUGH.
46 STATE_READ_HEADERS_FOR_COPY,
47 STATE_READ_HEADERS_FOR_COPY_DONE,
48 STATE_WRITE_HEADERS_FOR_COPY,
49 STATE_WRITE_HEADERS_FOR_COPY_DONE,
50 STATE_READ_DATA_FOR_COPY,
51 STATE_READ_DATA_FOR_COPY_DONE,
52 STATE_WRITE_DATA_FOR_COPY,
53 STATE_WRITE_DATA_FOR_COPY_DONE,
54
55 // Control flows linearly through these states, with a loop between
56 // WRITE_DATA_FOR_PASSTHROUGH and WRITE_DATA_FOR_PASSTHROUGH_DONE.
57 STATE_WRITE_HEADERS_FOR_PASSTHROUGH,
58 STATE_WRITE_HEADERS_FOR_PASSTHROUGH_DONE,
59 STATE_WRITE_DATA_FOR_PASSTHROUGH,
60 STATE_WRITE_DATA_FOR_PASSTHROUGH_DONE,
61 STATE_DONE,
62 };
63
64 // Shim class used to turn always-async functions into async-or-result
65 // functions. See the comments below near ReadInfoHelper.
66 class AsyncOnlyCompletionCallbackAdaptor
67 : public base::RefCounted<AsyncOnlyCompletionCallbackAdaptor> {
68 public:
69 explicit AsyncOnlyCompletionCallbackAdaptor(
70 const net::CompletionCallback& callback)
71 : async_(false), result_(net::ERR_IO_PENDING), callback_(callback) {}
72
73 void set_async(bool async) { async_ = async; }
74 bool async() { return async_; }
75 int result() { return result_; }
76
77 void WrappedCallback(int result) {
78 result_ = result;
79 if (async_)
80 callback_.Run(result);
81 }
82
83 private:
84 friend class base::RefCounted<AsyncOnlyCompletionCallbackAdaptor>;
85 virtual ~AsyncOnlyCompletionCallbackAdaptor() {}
86
87 bool async_;
88 int result_;
89 net::CompletionCallback callback_;
90 };
91
92 } // namespace
93
94 namespace content {
95
96 int ServiceWorkerCacheWriter::DoLoop(int status) {
97 bool pause = false;
98 do {
99 int next_state = -1;
100 switch (state_) {
101 case STATE_START:
102 status = DoStart(&next_state, &pause, status);
103 break;
104 case STATE_READ_HEADERS_FOR_COMPARE:
105 status = DoReadHeadersForCompare(&next_state, &pause, status);
106 break;
107 case STATE_READ_HEADERS_FOR_COMPARE_DONE:
108 status = DoReadHeadersForCompareDone(&next_state, &pause, status);
109 break;
110 case STATE_READ_DATA_FOR_COMPARE:
111 status = DoReadDataForCompare(&next_state, &pause, status);
112 break;
113 case STATE_READ_DATA_FOR_COMPARE_DONE:
114 status = DoReadDataForCompareDone(&next_state, &pause, status);
115 break;
116 case STATE_READ_HEADERS_FOR_COPY:
117 status = DoReadHeadersForCopy(&next_state, &pause, status);
118 break;
119 case STATE_READ_HEADERS_FOR_COPY_DONE:
120 status = DoReadHeadersForCopyDone(&next_state, &pause, status);
121 break;
122 case STATE_READ_DATA_FOR_COPY:
123 status = DoReadDataForCopy(&next_state, &pause, status);
124 break;
125 case STATE_READ_DATA_FOR_COPY_DONE:
126 status = DoReadDataForCopyDone(&next_state, &pause, status);
127 break;
128 case STATE_WRITE_HEADERS_FOR_PASSTHROUGH:
129 status = DoWriteHeadersForPassthrough(&next_state, &pause, status);
130 break;
131 case STATE_WRITE_HEADERS_FOR_PASSTHROUGH_DONE:
132 status = DoWriteHeadersForPassthroughDone(&next_state, &pause, status);
133 break;
134 case STATE_WRITE_DATA_FOR_PASSTHROUGH:
135 status = DoWriteDataForPassthrough(&next_state, &pause, status);
136 break;
137 case STATE_WRITE_DATA_FOR_PASSTHROUGH_DONE:
138 status = DoWriteDataForPassthroughDone(&next_state, &pause, status);
139 break;
140 case STATE_WRITE_HEADERS_FOR_COPY:
141 status = DoWriteHeadersForCopy(&next_state, &pause, status);
142 break;
143 case STATE_WRITE_HEADERS_FOR_COPY_DONE:
144 status = DoWriteHeadersForCopyDone(&next_state, &pause, status);
145 break;
146 case STATE_WRITE_DATA_FOR_COPY:
147 status = DoWriteDataForCopy(&next_state, &pause, status);
148 break;
149 case STATE_WRITE_DATA_FOR_COPY_DONE:
150 status = DoWriteDataForCopyDone(&next_state, &pause, status);
151 break;
152 case STATE_DONE:
153 status = DoDone(&next_state, &pause, status);
154 break;
155 default:
156 next_state = STATE_DONE;
157 break;
158 }
159 state_ = next_state;
160 } while (status >= net::OK && state_ != STATE_DONE && !pause);
161 return status;
162 }
163
164 ServiceWorkerCacheWriter::ServiceWorkerCacheWriter(
165 const ResponseReaderCreator& reader_creator,
166 const ResponseWriterCreator& writer_creator)
167 : state_(STATE_START),
168 did_replace_(false),
169 reader_creator_(reader_creator),
170 writer_creator_(writer_creator),
171 weak_factory_(this) {}
172
173 ServiceWorkerCacheWriter::~ServiceWorkerCacheWriter() {}
174
175 net::Error ServiceWorkerCacheWriter::MaybeWriteHeaders(
176 HttpResponseInfoIOBuffer* headers,
177 const OnWriteCompleteCallback& callback) {
178 headers_to_write_ = headers;
179 pending_callback_ = callback;
180 DCHECK_EQ(STATE_START, state_);
181 int result = DoLoop(net::OK);
182
183 // Ensure that the state machine is in one of the expected pause states.
184 // Synchronous errors always go to ERR_IO_PENDING.
185 if (result < 0 && result != net::ERR_IO_PENDING)
186 DCHECK_EQ(STATE_DONE, state_);
187
188 // Synchronous successes either go into passthrough mode or compare/copy mode.
189 if (result >= 0) {
190 DCHECK(state_ == STATE_READ_DATA_FOR_COMPARE ||
191 state_ == STATE_WRITE_DATA_FOR_PASSTHROUGH)
192 << "Unexpected state: " << state_;
193 }
194
195 // ERR_IO_PENDING has to have one of the "done" states as the next state.
196 if (result == net::ERR_IO_PENDING) {
197 DCHECK(state_ == STATE_READ_HEADERS_FOR_COMPARE_DONE ||
198 state_ == STATE_WRITE_HEADERS_FOR_COPY_DONE ||
199 state_ == STATE_WRITE_HEADERS_FOR_PASSTHROUGH_DONE)
200 << "Unexpected state: " << state_;
201 }
202
203 return result >= 0 ? net::OK : static_cast<net::Error>(result);
204 }
205
206 net::Error ServiceWorkerCacheWriter::MaybeWriteData(
207 net::IOBuffer* buf,
208 size_t buf_size,
209 const OnWriteCompleteCallback& callback) {
210 data_to_write_ = buf;
211 len_to_write_ = buf_size;
212 pending_callback_ = callback;
213 int result = DoLoop(net::OK);
214
215 // Check that the state machine is in one of the expected pause states.
216
217 // Synchronous errors are always STATE_DONE.
218 if (result < 0 && result != net::ERR_IO_PENDING)
219 DCHECK_EQ(STATE_DONE, state_);
220
221 // Synchronous successes mean that this object is immediately ready for a new
222 // call to MaybeWriteData, so it must be waiting in one of the ReadData
223 // states, or writing data directly in passthrough mode:
224 if (result >= 0) {
225 DCHECK(state_ == STATE_READ_DATA_FOR_COMPARE ||
226 state_ == STATE_READ_DATA_FOR_COPY ||
227 state_ == STATE_WRITE_DATA_FOR_PASSTHROUGH)
228 << "Unexpected state: " << state_;
229 }
230
231 // Asynchronous completion means the state machine must be waiting in one of
232 // the Done states for an IO operation to complete:
233 if (result == net::ERR_IO_PENDING) {
234 // Note that STATE_READ_HEADERS_FOR_COMPARE_DONE is excluded because the
235 // headers are compared in MaybeWriteHeaders, not here, and
236 // STATE_WRITE_HEADERS_FOR_PASSTHROUGH_DONE is excluded because that write
237 // is done by MaybeWriteHeaders.
238 DCHECK(state_ == STATE_READ_DATA_FOR_COMPARE_DONE ||
239 state_ == STATE_READ_HEADERS_FOR_COPY_DONE ||
240 state_ == STATE_READ_DATA_FOR_COPY_DONE ||
241 state_ == STATE_WRITE_HEADERS_FOR_COPY_DONE ||
242 state_ == STATE_WRITE_DATA_FOR_COPY_DONE ||
243 state_ == STATE_WRITE_DATA_FOR_PASSTHROUGH_DONE)
244 << "Unexpected state: " << state_;
245 }
246
247 return result >= 0 ? net::OK : static_cast<net::Error>(result);
248 }
249
250 int ServiceWorkerCacheWriter::DoStart(int* next_state,
251 bool* pause,
252 int result) {
253 bytes_written_ = 0;
254 compare_reader_ = reader_creator_.Run();
255 if (compare_reader_.get()) {
256 *next_state = STATE_READ_HEADERS_FOR_COMPARE;
257 } else {
258 // No existing reader, just write the headers back directly.
259 *next_state = STATE_WRITE_HEADERS_FOR_PASSTHROUGH;
260 }
261 return net::OK;
262 }
263
264 int ServiceWorkerCacheWriter::DoReadHeadersForCompare(int* next_state,
265 bool* pause,
266 int result) {
267 DCHECK(headers_to_write_);
268
269 headers_to_read_ = new HttpResponseInfoIOBuffer;
270 *next_state = STATE_READ_HEADERS_FOR_COMPARE_DONE;
271 return ReadInfoHelper(compare_reader_, headers_to_read_.get());
272 }
273
274 int ServiceWorkerCacheWriter::DoReadHeadersForCompareDone(int* next_state,
275 bool* pause,
276 int result) {
277 if (result < 0) {
278 *next_state = STATE_DONE;
279 return result;
280 }
281 cached_length_ = headers_to_read_->response_data_size;
282 bytes_compared_ = 0;
283 *pause = true;
284 *next_state = STATE_READ_DATA_FOR_COMPARE;
285 return net::OK;
286 }
287
288 int ServiceWorkerCacheWriter::DoReadDataForCompare(int* next_state,
289 bool* pause,
290 int result) {
291 DCHECK(data_to_write_);
292
293 data_to_read_ = new net::IOBuffer(len_to_write_);
294 len_to_read_ = len_to_write_;
295 *next_state = STATE_READ_DATA_FOR_COMPARE_DONE;
296 compare_offset_ = 0;
297 // If this was an EOF, don't issue a read.
298 if (len_to_write_ > 0)
299 result = ReadDataHelper(compare_reader_, data_to_read_.get(), len_to_read_);
300 return result;
301 }
302
303 int ServiceWorkerCacheWriter::DoReadDataForCompareDone(int* next_state,
304 bool* pause,
305 int result) {
306 DCHECK(data_to_read_);
307 DCHECK(data_to_write_);
308 DCHECK_EQ(len_to_read_, len_to_write_);
309 DCHECK_LE(result + compare_offset_, static_cast<size_t>(len_to_write_));
310
311 if (result < 0) {
312 *next_state = STATE_DONE;
313 return result;
314 }
315
316 // Premature EOF while reading the service worker script cache data to
317 // compare. Fail the comparison.
318 if (result == 0 && len_to_write_ != 0) {
319 *next_state = STATE_READ_HEADERS_FOR_COPY;
320 return net::OK;
321 }
322
323 // Compare the data from the ServiceWorker script cache to the data from the
324 // network.
325 if (memcmp(data_to_read_->data(), data_to_write_->data() + compare_offset_,
326 result)) {
327 // Data mismatched. This method already validated that all the bytes through
328 // |bytes_compared_| were identical, so copy the first |bytes_compared_|
329 // over, then start writing network data back after the changed point.
330 //
331 // Note that the state machine does NOT get paused here, since there is
332 // still further work to do before this object is ready to handle another
333 // WriteData call.
334 *next_state = STATE_READ_HEADERS_FOR_COPY;
335 return net::OK;
336 }
337
338 compare_offset_ += result;
339
340 // This is a little bit tricky. It is possible that not enough data was read
341 // to finish comparing the entire block of data from the network (which is
342 // kept in len_to_write_), so this method may need to issue another read and
343 // return to this state.
344 //
345 // Compare isn't complete yet. Issue another read for the remaining data. Note
346 // that this reuses the same IOBuffer.
347 if (compare_offset_ < static_cast<size_t>(len_to_read_)) {
348 *next_state = STATE_READ_DATA_FOR_COMPARE_DONE;
349 return ReadDataHelper(compare_reader_, data_to_read_.get(),
350 len_to_read_ - compare_offset_);
351 }
352
353 // Cached entry is longer than the network entry but the prefix matches. Copy
354 // just the prefix.
355 if (len_to_read_ == 0 && bytes_compared_ + compare_offset_ < cached_length_) {
356 *next_state = STATE_READ_HEADERS_FOR_COPY;
357 return net::OK;
358 }
359
360 // bytes_compared_ only gets incremented when a full block is compared, to
361 // avoid having to use only parts of the buffered network data.
362 bytes_compared_ += result;
363 *next_state = STATE_READ_DATA_FOR_COMPARE;
364 *pause = true;
365 return net::OK;
366 }
367
368 int ServiceWorkerCacheWriter::DoReadHeadersForCopy(int* next_state,
369 bool* pause,
370 int result) {
371 bytes_copied_ = 0;
372 copy_reader_ = reader_creator_.Run();
373 headers_to_read_ = new HttpResponseInfoIOBuffer;
374 data_to_copy_ = new net::IOBuffer(kCopyBufferSize);
375 *next_state = STATE_READ_HEADERS_FOR_COPY_DONE;
376 return ReadInfoHelper(copy_reader_, headers_to_read_.get());
377 }
378
379 int ServiceWorkerCacheWriter::DoReadHeadersForCopyDone(int* next_state,
380 bool* pause,
381 int result) {
382 if (result < 0) {
383 *next_state = STATE_DONE;
384 return result;
385 }
386 *next_state = STATE_WRITE_HEADERS_FOR_COPY;
387 return net::OK;
388 }
389
390 // Write the just-read headers back to the cache.
391 // Note that this method must create |writer_|, since the only paths to this
392 // state never create a writer.
393 // Also note that this *discards* the read headers and replaces them with the
394 // net headers.
395 int ServiceWorkerCacheWriter::DoWriteHeadersForCopy(int* next_state,
396 bool* pause,
397 int result) {
398 DCHECK(!writer_);
399 writer_ = writer_creator_.Run();
400 *next_state = STATE_WRITE_HEADERS_FOR_COPY_DONE;
401 return WriteInfoHelper(writer_, headers_to_write_.get());
402 }
403
404 int ServiceWorkerCacheWriter::DoWriteHeadersForCopyDone(int* next_state,
405 bool* pause,
406 int result) {
407 if (result < 0) {
408 *next_state = STATE_DONE;
409 return result;
410 }
411 *next_state = STATE_READ_DATA_FOR_COPY;
412 return net::OK;
413 }
414
415 int ServiceWorkerCacheWriter::DoReadDataForCopy(int* next_state,
416 bool* pause,
417 int result) {
418 size_t to_read = std::min(kCopyBufferSize, bytes_compared_ - bytes_copied_);
419 // At this point, all compared bytes have been read. Currently
420 // |data_to_write_| and |len_to_write_| hold the chunk of network input that
421 // caused the comparison failure, so those need to be written back and this
422 // object needs to go into passthrough mode.
423 if (to_read == 0) {
424 *next_state = STATE_WRITE_DATA_FOR_PASSTHROUGH;
425 return net::OK;
426 }
427 *next_state = STATE_READ_DATA_FOR_COPY_DONE;
428 return ReadDataHelper(copy_reader_, data_to_copy_.get(), to_read);
429 }
430
431 int ServiceWorkerCacheWriter::DoReadDataForCopyDone(int* next_state,
432 bool* pause,
433 int result) {
434 if (result < 0) {
435 *next_state = STATE_DONE;
436 return result;
437 }
438 *next_state = STATE_WRITE_DATA_FOR_COPY;
439 return result;
440 }
441
442 int ServiceWorkerCacheWriter::DoWriteDataForCopy(int* next_state,
443 bool* pause,
444 int result) {
445 *next_state = STATE_WRITE_DATA_FOR_COPY_DONE;
446 DCHECK_GT(result, 0);
447 return WriteDataHelper(writer_, data_to_copy_.get(), result);
448 }
449
450 int ServiceWorkerCacheWriter::DoWriteDataForCopyDone(int* next_state,
451 bool* pause,
452 int result) {
453 if (result < 0) {
454 *next_state = STATE_DONE;
455 return result;
456 }
457 bytes_written_ += result;
458 bytes_copied_ += result;
459 *next_state = STATE_READ_DATA_FOR_COPY;
460 return result;
461 }
462
463 int ServiceWorkerCacheWriter::DoWriteHeadersForPassthrough(int* next_state,
464 bool* pause,
465 int result) {
466 writer_ = writer_creator_.Run();
467 *next_state = STATE_WRITE_HEADERS_FOR_PASSTHROUGH_DONE;
468 return WriteInfoHelper(writer_, headers_to_write_.get());
469 }
470
471 int ServiceWorkerCacheWriter::DoWriteHeadersForPassthroughDone(int* next_state,
472 bool* pause,
473 int result) {
474 *next_state = STATE_WRITE_DATA_FOR_PASSTHROUGH;
475 *pause = true;
476 return net::OK;
477 }
478
479 int ServiceWorkerCacheWriter::DoWriteDataForPassthrough(int* next_state,
480 bool* pause,
481 int result) {
482 *next_state = STATE_WRITE_DATA_FOR_PASSTHROUGH_DONE;
483 if (len_to_write_ > 0)
484 result = WriteDataHelper(writer_, data_to_write_.get(), len_to_write_);
485 return result;
486 }
487
488 int ServiceWorkerCacheWriter::DoWriteDataForPassthroughDone(int* next_state,
489 bool* pause,
490 int result) {
491 if (result < 0) {
492 *next_state = STATE_DONE;
493 return result;
494 }
495 bytes_written_ += result;
496 *next_state = STATE_WRITE_DATA_FOR_PASSTHROUGH;
497 *pause = true;
498 return net::OK;
499 }
500
501 int ServiceWorkerCacheWriter::DoDone(int* next_state, bool* pause, int result) {
502 *next_state = STATE_DONE;
503 return net::OK;
504 }
505
506 // These helpers adapt the AppCache "always use the callback" pattern to the
507 // //net "only use the callback for async" pattern using
508 // AsyncCompletionCallbackAdaptor.
509 //
510 // Specifically, these methods return result codes directly for synchronous
511 // completions, and only run their callback (which is AsyncDoLoop) for
512 // asynchronous completions.
513
514 int ServiceWorkerCacheWriter::ReadInfoHelper(
515 const scoped_ptr<ServiceWorkerResponseReader>& reader,
516 HttpResponseInfoIOBuffer* buf) {
517 net::CompletionCallback run_callback = base::Bind(
518 &ServiceWorkerCacheWriter::AsyncDoLoop, weak_factory_.GetWeakPtr());
519 scoped_refptr<AsyncOnlyCompletionCallbackAdaptor> adaptor(
520 new AsyncOnlyCompletionCallbackAdaptor(run_callback));
521 reader->ReadInfo(
522 buf, base::Bind(&AsyncOnlyCompletionCallbackAdaptor::WrappedCallback,
523 adaptor));
524 adaptor->set_async(true);
525 return adaptor->result();
526 }
527
528 int ServiceWorkerCacheWriter::ReadDataHelper(
529 const scoped_ptr<ServiceWorkerResponseReader>& reader,
530 net::IOBuffer* buf,
531 int buf_len) {
532 net::CompletionCallback run_callback = base::Bind(
533 &ServiceWorkerCacheWriter::AsyncDoLoop, weak_factory_.GetWeakPtr());
534 scoped_refptr<AsyncOnlyCompletionCallbackAdaptor> adaptor(
535 new AsyncOnlyCompletionCallbackAdaptor(run_callback));
536 reader->ReadData(
537 buf, buf_len,
538 base::Bind(&AsyncOnlyCompletionCallbackAdaptor::WrappedCallback,
539 adaptor));
540 adaptor->set_async(true);
541 return adaptor->result();
542 }
543
544 int ServiceWorkerCacheWriter::WriteInfoHelper(
545 const scoped_ptr<ServiceWorkerResponseWriter>& writer,
546 HttpResponseInfoIOBuffer* buf) {
547 did_replace_ = true;
548 net::CompletionCallback run_callback = base::Bind(
549 &ServiceWorkerCacheWriter::AsyncDoLoop, weak_factory_.GetWeakPtr());
550 scoped_refptr<AsyncOnlyCompletionCallbackAdaptor> adaptor(
551 new AsyncOnlyCompletionCallbackAdaptor(run_callback));
552 writer->WriteInfo(
553 buf, base::Bind(&AsyncOnlyCompletionCallbackAdaptor::WrappedCallback,
554 adaptor));
555 adaptor->set_async(true);
556 return adaptor->result();
557 }
558
559 int ServiceWorkerCacheWriter::WriteDataHelper(
560 const scoped_ptr<ServiceWorkerResponseWriter>& writer,
561 net::IOBuffer* buf,
562 int buf_len) {
563 net::CompletionCallback run_callback = base::Bind(
564 &ServiceWorkerCacheWriter::AsyncDoLoop, weak_factory_.GetWeakPtr());
565 scoped_refptr<AsyncOnlyCompletionCallbackAdaptor> adaptor(
566 new AsyncOnlyCompletionCallbackAdaptor(run_callback));
567 writer->WriteData(
568 buf, buf_len,
569 base::Bind(&AsyncOnlyCompletionCallbackAdaptor::WrappedCallback,
570 adaptor));
571 adaptor->set_async(true);
572 return adaptor->result();
573 }
574
575 void ServiceWorkerCacheWriter::AsyncDoLoop(int result) {
576 result = DoLoop(result);
577 // If the result is ERR_IO_PENDING, the pending callback will be run by a
578 // later invocation of AsyncDoLoop.
579 if (result != net::ERR_IO_PENDING) {
580 OnWriteCompleteCallback callback = pending_callback_;
581 pending_callback_.Reset();
582 net::Error error = result >= 0 ? net::OK : static_cast<net::Error>(result);
583 callback.Run(error);
584 }
585 }
586
587 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698