OLD | NEW |
| (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: | |
108 NOTREACHED() << "Unknown state in DoLoop"; | |
109 state_ = STATE_DONE; | |
110 break; | |
111 } | |
112 } while (status >= net::OK && state_ != STATE_DONE); | |
113 io_pending_ = (status == net::ERR_IO_PENDING); | |
114 return status; | |
115 } | |
116 | |
117 ServiceWorkerCacheWriter::ServiceWorkerCacheWriter( | |
118 const ResponseReaderCreator& reader_creator, | |
119 const ResponseWriterCreator& writer_creator) | |
120 : state_(STATE_START), | |
121 io_pending_(false), | |
122 comparing_(false), | |
123 did_replace_(false), | |
124 reader_creator_(reader_creator), | |
125 writer_creator_(writer_creator), | |
126 weak_factory_(this) {} | |
127 | |
128 ServiceWorkerCacheWriter::~ServiceWorkerCacheWriter() {} | |
129 | |
130 net::Error ServiceWorkerCacheWriter::MaybeWriteHeaders( | |
131 HttpResponseInfoIOBuffer* headers, | |
132 const OnWriteCompleteCallback& callback) { | |
133 DCHECK(!io_pending_); | |
134 | |
135 headers_to_write_ = headers; | |
136 pending_callback_ = callback; | |
137 DCHECK_EQ(state_, STATE_START); | |
138 int result = DoLoop(net::OK); | |
139 | |
140 // Synchronous errors and successes always go to STATE_DONE. | |
141 if (result != net::ERR_IO_PENDING) | |
142 DCHECK_EQ(state_, STATE_DONE); | |
143 | |
144 // ERR_IO_PENDING has to have one of the STATE_*_DONE states as the next state | |
145 // (not STATE_DONE itself). | |
146 if (result == net::ERR_IO_PENDING) { | |
147 DCHECK(state_ == STATE_READ_HEADERS_FOR_COMPARE_DONE || | |
148 state_ == STATE_WRITE_HEADERS_FOR_COPY_DONE || | |
149 state_ == STATE_WRITE_HEADERS_FOR_PASSTHROUGH_DONE) | |
150 << "Unexpected state: " << state_; | |
151 io_pending_ = true; | |
152 } | |
153 | |
154 return result >= 0 ? net::OK : static_cast<net::Error>(result); | |
155 } | |
156 | |
157 net::Error ServiceWorkerCacheWriter::MaybeWriteData( | |
158 net::IOBuffer* buf, | |
159 size_t buf_size, | |
160 const OnWriteCompleteCallback& callback) { | |
161 DCHECK(!io_pending_); | |
162 | |
163 data_to_write_ = buf; | |
164 len_to_write_ = buf_size; | |
165 pending_callback_ = callback; | |
166 | |
167 if (comparing_) | |
168 state_ = STATE_READ_DATA_FOR_COMPARE; | |
169 else | |
170 state_ = STATE_WRITE_DATA_FOR_PASSTHROUGH; | |
171 | |
172 int result = DoLoop(net::OK); | |
173 | |
174 // Synchronous completions are always STATE_DONE. | |
175 if (result != net::ERR_IO_PENDING) | |
176 DCHECK_EQ(state_, STATE_DONE); | |
177 | |
178 // Asynchronous completion means the state machine must be waiting in one of | |
179 // the Done states for an IO operation to complete: | |
180 if (result == net::ERR_IO_PENDING) { | |
181 // Note that STATE_READ_HEADERS_FOR_COMPARE_DONE is excluded because the | |
182 // headers are compared in MaybeWriteHeaders, not here, and | |
183 // STATE_WRITE_HEADERS_FOR_PASSTHROUGH_DONE is excluded because that write | |
184 // is done by MaybeWriteHeaders. | |
185 DCHECK(state_ == STATE_READ_DATA_FOR_COMPARE_DONE || | |
186 state_ == STATE_READ_HEADERS_FOR_COPY_DONE || | |
187 state_ == STATE_READ_DATA_FOR_COPY_DONE || | |
188 state_ == STATE_WRITE_HEADERS_FOR_COPY_DONE || | |
189 state_ == STATE_WRITE_DATA_FOR_COPY_DONE || | |
190 state_ == STATE_WRITE_DATA_FOR_PASSTHROUGH_DONE) | |
191 << "Unexpected state: " << state_; | |
192 } | |
193 | |
194 return result >= 0 ? net::OK : static_cast<net::Error>(result); | |
195 } | |
196 | |
197 int ServiceWorkerCacheWriter::DoStart(int result) { | |
198 bytes_written_ = 0; | |
199 compare_reader_ = reader_creator_.Run(); | |
200 if (compare_reader_.get()) { | |
201 state_ = STATE_READ_HEADERS_FOR_COMPARE; | |
202 comparing_ = true; | |
203 } else { | |
204 // No existing reader, just write the headers back directly. | |
205 state_ = STATE_WRITE_HEADERS_FOR_PASSTHROUGH; | |
206 comparing_ = false; | |
207 } | |
208 return net::OK; | |
209 } | |
210 | |
211 int ServiceWorkerCacheWriter::DoReadHeadersForCompare(int result) { | |
212 DCHECK(headers_to_write_); | |
213 | |
214 headers_to_read_ = new HttpResponseInfoIOBuffer; | |
215 state_ = STATE_READ_HEADERS_FOR_COMPARE_DONE; | |
216 return ReadInfoHelper(compare_reader_, headers_to_read_.get()); | |
217 } | |
218 | |
219 int ServiceWorkerCacheWriter::DoReadHeadersForCompareDone(int result) { | |
220 if (result < 0) { | |
221 state_ = STATE_DONE; | |
222 return result; | |
223 } | |
224 cached_length_ = headers_to_read_->response_data_size; | |
225 bytes_compared_ = 0; | |
226 state_ = STATE_DONE; | |
227 return net::OK; | |
228 } | |
229 | |
230 int ServiceWorkerCacheWriter::DoReadDataForCompare(int result) { | |
231 DCHECK(data_to_write_); | |
232 | |
233 data_to_read_ = new net::IOBuffer(len_to_write_); | |
234 len_to_read_ = len_to_write_; | |
235 state_ = STATE_READ_DATA_FOR_COMPARE_DONE; | |
236 compare_offset_ = 0; | |
237 // If this was an EOF, don't issue a read. | |
238 if (len_to_write_ > 0) | |
239 result = ReadDataHelper(compare_reader_, data_to_read_.get(), len_to_read_); | |
240 return result; | |
241 } | |
242 | |
243 int ServiceWorkerCacheWriter::DoReadDataForCompareDone(int result) { | |
244 DCHECK(data_to_read_); | |
245 DCHECK(data_to_write_); | |
246 DCHECK_EQ(len_to_read_, len_to_write_); | |
247 DCHECK_LE(result + compare_offset_, static_cast<size_t>(len_to_write_)); | |
248 | |
249 if (result < 0) { | |
250 state_ = STATE_DONE; | |
251 return result; | |
252 } | |
253 | |
254 // Premature EOF while reading the service worker script cache data to | |
255 // compare. Fail the comparison. | |
256 if (result == 0 && len_to_write_ != 0) { | |
257 comparing_ = false; | |
258 state_ = STATE_READ_HEADERS_FOR_COPY; | |
259 return net::OK; | |
260 } | |
261 | |
262 // Compare the data from the ServiceWorker script cache to the data from the | |
263 // network. | |
264 if (memcmp(data_to_read_->data(), data_to_write_->data() + compare_offset_, | |
265 result)) { | |
266 // Data mismatched. This method already validated that all the bytes through | |
267 // |bytes_compared_| were identical, so copy the first |bytes_compared_| | |
268 // over, then start writing network data back after the changed point. | |
269 comparing_ = false; | |
270 state_ = STATE_READ_HEADERS_FOR_COPY; | |
271 return net::OK; | |
272 } | |
273 | |
274 compare_offset_ += result; | |
275 | |
276 // This is a little bit tricky. It is possible that not enough data was read | |
277 // to finish comparing the entire block of data from the network (which is | |
278 // kept in len_to_write_), so this method may need to issue another read and | |
279 // return to this state. | |
280 // | |
281 // Compare isn't complete yet. Issue another read for the remaining data. Note | |
282 // that this reuses the same IOBuffer. | |
283 if (compare_offset_ < static_cast<size_t>(len_to_read_)) { | |
284 state_ = STATE_READ_DATA_FOR_COMPARE_DONE; | |
285 return ReadDataHelper(compare_reader_, data_to_read_.get(), | |
286 len_to_read_ - compare_offset_); | |
287 } | |
288 | |
289 // Cached entry is longer than the network entry but the prefix matches. Copy | |
290 // just the prefix. | |
291 if (len_to_read_ == 0 && bytes_compared_ + compare_offset_ < cached_length_) { | |
292 comparing_ = false; | |
293 state_ = STATE_READ_HEADERS_FOR_COPY; | |
294 return net::OK; | |
295 } | |
296 | |
297 // bytes_compared_ only gets incremented when a full block is compared, to | |
298 // avoid having to use only parts of the buffered network data. | |
299 bytes_compared_ += result; | |
300 state_ = STATE_DONE; | |
301 return net::OK; | |
302 } | |
303 | |
304 int ServiceWorkerCacheWriter::DoReadHeadersForCopy(int result) { | |
305 bytes_copied_ = 0; | |
306 copy_reader_ = reader_creator_.Run(); | |
307 headers_to_read_ = new HttpResponseInfoIOBuffer; | |
308 data_to_copy_ = new net::IOBuffer(kCopyBufferSize); | |
309 state_ = STATE_READ_HEADERS_FOR_COPY_DONE; | |
310 return ReadInfoHelper(copy_reader_, headers_to_read_.get()); | |
311 } | |
312 | |
313 int ServiceWorkerCacheWriter::DoReadHeadersForCopyDone(int result) { | |
314 if (result < 0) { | |
315 state_ = STATE_DONE; | |
316 return result; | |
317 } | |
318 state_ = STATE_WRITE_HEADERS_FOR_COPY; | |
319 return net::OK; | |
320 } | |
321 | |
322 // Write the just-read headers back to the cache. | |
323 // Note that this method must create |writer_|, since the only paths to this | |
324 // state never create a writer. | |
325 // Also note that this *discards* the read headers and replaces them with the | |
326 // net headers. | |
327 int ServiceWorkerCacheWriter::DoWriteHeadersForCopy(int result) { | |
328 DCHECK(!writer_); | |
329 writer_ = writer_creator_.Run(); | |
330 state_ = STATE_WRITE_HEADERS_FOR_COPY_DONE; | |
331 return WriteInfoHelper(writer_, headers_to_write_.get()); | |
332 } | |
333 | |
334 int ServiceWorkerCacheWriter::DoWriteHeadersForCopyDone(int result) { | |
335 if (result < 0) { | |
336 state_ = STATE_DONE; | |
337 return result; | |
338 } | |
339 state_ = STATE_READ_DATA_FOR_COPY; | |
340 return net::OK; | |
341 } | |
342 | |
343 int ServiceWorkerCacheWriter::DoReadDataForCopy(int result) { | |
344 size_t to_read = std::min(kCopyBufferSize, bytes_compared_ - bytes_copied_); | |
345 // At this point, all compared bytes have been read. Currently | |
346 // |data_to_write_| and |len_to_write_| hold the chunk of network input that | |
347 // caused the comparison failure, so those need to be written back and this | |
348 // object needs to go into passthrough mode. | |
349 if (to_read == 0) { | |
350 state_ = STATE_WRITE_DATA_FOR_PASSTHROUGH; | |
351 return net::OK; | |
352 } | |
353 state_ = STATE_READ_DATA_FOR_COPY_DONE; | |
354 return ReadDataHelper(copy_reader_, data_to_copy_.get(), to_read); | |
355 } | |
356 | |
357 int ServiceWorkerCacheWriter::DoReadDataForCopyDone(int result) { | |
358 if (result < 0) { | |
359 state_ = STATE_DONE; | |
360 return result; | |
361 } | |
362 state_ = STATE_WRITE_DATA_FOR_COPY; | |
363 return result; | |
364 } | |
365 | |
366 int ServiceWorkerCacheWriter::DoWriteDataForCopy(int result) { | |
367 state_ = STATE_WRITE_DATA_FOR_COPY_DONE; | |
368 DCHECK_GT(result, 0); | |
369 return WriteDataHelper(writer_, data_to_copy_.get(), result); | |
370 } | |
371 | |
372 int ServiceWorkerCacheWriter::DoWriteDataForCopyDone(int result) { | |
373 if (result < 0) { | |
374 state_ = STATE_DONE; | |
375 return result; | |
376 } | |
377 bytes_written_ += result; | |
378 bytes_copied_ += result; | |
379 state_ = STATE_READ_DATA_FOR_COPY; | |
380 return result; | |
381 } | |
382 | |
383 int ServiceWorkerCacheWriter::DoWriteHeadersForPassthrough(int result) { | |
384 writer_ = writer_creator_.Run(); | |
385 state_ = STATE_WRITE_HEADERS_FOR_PASSTHROUGH_DONE; | |
386 return WriteInfoHelper(writer_, headers_to_write_.get()); | |
387 } | |
388 | |
389 int ServiceWorkerCacheWriter::DoWriteHeadersForPassthroughDone(int result) { | |
390 state_ = STATE_DONE; | |
391 return net::OK; | |
392 } | |
393 | |
394 int ServiceWorkerCacheWriter::DoWriteDataForPassthrough(int result) { | |
395 state_ = STATE_WRITE_DATA_FOR_PASSTHROUGH_DONE; | |
396 if (len_to_write_ > 0) | |
397 result = WriteDataHelper(writer_, data_to_write_.get(), len_to_write_); | |
398 return result; | |
399 } | |
400 | |
401 int ServiceWorkerCacheWriter::DoWriteDataForPassthroughDone(int result) { | |
402 if (result < 0) { | |
403 state_ = STATE_DONE; | |
404 return result; | |
405 } | |
406 bytes_written_ += result; | |
407 state_ = STATE_DONE; | |
408 return net::OK; | |
409 } | |
410 | |
411 int ServiceWorkerCacheWriter::DoDone(int result) { | |
412 state_ = STATE_DONE; | |
413 return net::OK; | |
414 } | |
415 | |
416 // These helpers adapt the AppCache "always use the callback" pattern to the | |
417 // //net "only use the callback for async" pattern using | |
418 // AsyncCompletionCallbackAdaptor. | |
419 // | |
420 // Specifically, these methods return result codes directly for synchronous | |
421 // completions, and only run their callback (which is AsyncDoLoop) for | |
422 // asynchronous completions. | |
423 | |
424 int ServiceWorkerCacheWriter::ReadInfoHelper( | |
425 const scoped_ptr<ServiceWorkerResponseReader>& reader, | |
426 HttpResponseInfoIOBuffer* buf) { | |
427 net::CompletionCallback run_callback = base::Bind( | |
428 &ServiceWorkerCacheWriter::AsyncDoLoop, weak_factory_.GetWeakPtr()); | |
429 scoped_refptr<AsyncOnlyCompletionCallbackAdaptor> adaptor( | |
430 new AsyncOnlyCompletionCallbackAdaptor(run_callback)); | |
431 reader->ReadInfo( | |
432 buf, base::Bind(&AsyncOnlyCompletionCallbackAdaptor::WrappedCallback, | |
433 adaptor)); | |
434 adaptor->set_async(true); | |
435 return adaptor->result(); | |
436 } | |
437 | |
438 int ServiceWorkerCacheWriter::ReadDataHelper( | |
439 const scoped_ptr<ServiceWorkerResponseReader>& reader, | |
440 net::IOBuffer* buf, | |
441 int buf_len) { | |
442 net::CompletionCallback run_callback = base::Bind( | |
443 &ServiceWorkerCacheWriter::AsyncDoLoop, weak_factory_.GetWeakPtr()); | |
444 scoped_refptr<AsyncOnlyCompletionCallbackAdaptor> adaptor( | |
445 new AsyncOnlyCompletionCallbackAdaptor(run_callback)); | |
446 reader->ReadData( | |
447 buf, buf_len, | |
448 base::Bind(&AsyncOnlyCompletionCallbackAdaptor::WrappedCallback, | |
449 adaptor)); | |
450 adaptor->set_async(true); | |
451 return adaptor->result(); | |
452 } | |
453 | |
454 int ServiceWorkerCacheWriter::WriteInfoHelper( | |
455 const scoped_ptr<ServiceWorkerResponseWriter>& writer, | |
456 HttpResponseInfoIOBuffer* buf) { | |
457 did_replace_ = true; | |
458 net::CompletionCallback run_callback = base::Bind( | |
459 &ServiceWorkerCacheWriter::AsyncDoLoop, weak_factory_.GetWeakPtr()); | |
460 scoped_refptr<AsyncOnlyCompletionCallbackAdaptor> adaptor( | |
461 new AsyncOnlyCompletionCallbackAdaptor(run_callback)); | |
462 writer->WriteInfo( | |
463 buf, base::Bind(&AsyncOnlyCompletionCallbackAdaptor::WrappedCallback, | |
464 adaptor)); | |
465 adaptor->set_async(true); | |
466 return adaptor->result(); | |
467 } | |
468 | |
469 int ServiceWorkerCacheWriter::WriteDataHelper( | |
470 const scoped_ptr<ServiceWorkerResponseWriter>& writer, | |
471 net::IOBuffer* buf, | |
472 int buf_len) { | |
473 net::CompletionCallback run_callback = base::Bind( | |
474 &ServiceWorkerCacheWriter::AsyncDoLoop, weak_factory_.GetWeakPtr()); | |
475 scoped_refptr<AsyncOnlyCompletionCallbackAdaptor> adaptor( | |
476 new AsyncOnlyCompletionCallbackAdaptor(run_callback)); | |
477 writer->WriteData( | |
478 buf, buf_len, | |
479 base::Bind(&AsyncOnlyCompletionCallbackAdaptor::WrappedCallback, | |
480 adaptor)); | |
481 adaptor->set_async(true); | |
482 return adaptor->result(); | |
483 } | |
484 | |
485 void ServiceWorkerCacheWriter::AsyncDoLoop(int result) { | |
486 result = DoLoop(result); | |
487 // If the result is ERR_IO_PENDING, the pending callback will be run by a | |
488 // later invocation of AsyncDoLoop. | |
489 if (result != net::ERR_IO_PENDING) { | |
490 OnWriteCompleteCallback callback = pending_callback_; | |
491 pending_callback_.Reset(); | |
492 net::Error error = result >= 0 ? net::OK : static_cast<net::Error>(result); | |
493 io_pending_ = false; | |
494 callback.Run(error); | |
495 } | |
496 } | |
497 | |
498 } // namespace content | |
OLD | NEW |