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