OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "webkit/appcache/appcache_response.h" | 5 #include "webkit/appcache/appcache_response.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | |
9 #include "base/compiler_specific.h" | |
8 #include "base/logging.h" | 10 #include "base/logging.h" |
9 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
10 #include "base/pickle.h" | 12 #include "base/pickle.h" |
11 #include "base/string_util.h" | 13 #include "base/string_util.h" |
12 #include "net/base/completion_callback.h" | 14 #include "net/base/completion_callback.h" |
13 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
14 #include "net/base/io_buffer.h" | 16 #include "net/base/io_buffer.h" |
15 #include "webkit/appcache/appcache_service.h" | 17 #include "webkit/appcache/appcache_service.h" |
16 | 18 |
17 namespace appcache { | 19 namespace appcache { |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
130 DCHECK_NE(net::ERR_IO_PENDING, result); | 132 DCHECK_NE(net::ERR_IO_PENDING, result); |
131 OnIOComplete(result); | 133 OnIOComplete(result); |
132 } | 134 } |
133 | 135 |
134 | 136 |
135 // AppCacheResponseReader ---------------------------------------------- | 137 // AppCacheResponseReader ---------------------------------------------- |
136 | 138 |
137 AppCacheResponseReader::AppCacheResponseReader( | 139 AppCacheResponseReader::AppCacheResponseReader( |
138 int64 response_id, int64 group_id, AppCacheDiskCacheInterface* disk_cache) | 140 int64 response_id, int64 group_id, AppCacheDiskCacheInterface* disk_cache) |
139 : AppCacheResponseIO(response_id, group_id, disk_cache), | 141 : AppCacheResponseIO(response_id, group_id, disk_cache), |
140 range_offset_(0), range_length_(kint32max), | 142 range_offset_(0), |
141 read_position_(0) { | 143 range_length_(kint32max), |
144 read_position_(0), | |
145 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | |
142 } | 146 } |
143 | 147 |
144 AppCacheResponseReader::~AppCacheResponseReader() { | 148 AppCacheResponseReader::~AppCacheResponseReader() { |
michaeln
2012/01/04 02:07:17
Is disk_cache_->OpenEntry() 'copy' of the callback
James Hawkins
2012/01/05 22:44:56
No, because of the WeakPtr.
michaeln
2012/01/05 23:34:54
Great, so AppCacheResponseReader::OnOpenEntryCompl
| |
145 if (open_callback_) | |
146 open_callback_.release()->Cancel(); | |
147 } | 149 } |
148 | 150 |
149 void AppCacheResponseReader::ReadInfo(HttpResponseInfoIOBuffer* info_buf, | 151 void AppCacheResponseReader::ReadInfo(HttpResponseInfoIOBuffer* info_buf, |
150 const net::CompletionCallback& callback) { | 152 const net::CompletionCallback& callback) { |
151 DCHECK(!callback.is_null()); | 153 DCHECK(!callback.is_null()); |
152 DCHECK(!IsReadPending()); | 154 DCHECK(!IsReadPending()); |
153 DCHECK(info_buf); | 155 DCHECK(info_buf); |
154 DCHECK(!info_buf->http_info.get()); | 156 DCHECK(!info_buf->http_info.get()); |
155 DCHECK(!buffer_.get()); | 157 DCHECK(!buffer_.get()); |
156 DCHECK(!info_buffer_.get()); | 158 DCHECK(!info_buffer_.get()); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
233 entry_->GetSize(kResponseContentIndex); | 235 entry_->GetSize(kResponseContentIndex); |
234 } else { | 236 } else { |
235 read_position_ += result; | 237 read_position_ += result; |
236 } | 238 } |
237 } | 239 } |
238 InvokeUserCompletionCallback(result); | 240 InvokeUserCompletionCallback(result); |
239 } | 241 } |
240 | 242 |
241 void AppCacheResponseReader::OpenEntryIfNeededAndContinue() { | 243 void AppCacheResponseReader::OpenEntryIfNeededAndContinue() { |
242 int rv; | 244 int rv; |
245 AppCacheDiskCacheInterface::Entry** entry_ptr = NULL; | |
243 if (entry_) { | 246 if (entry_) { |
244 rv = net::OK; | 247 rv = net::OK; |
245 } else if (!disk_cache_) { | 248 } else if (!disk_cache_) { |
246 rv = net::ERR_FAILED; | 249 rv = net::ERR_FAILED; |
247 } else { | 250 } else { |
248 open_callback_ = new EntryCallback<AppCacheResponseReader>( | 251 entry_ptr = new(AppCacheDiskCacheInterface::Entry*); |
249 this, &AppCacheResponseReader::OnOpenEntryComplete); | 252 open_callback_ = |
250 rv = disk_cache_->OpenEntry( | 253 base::Bind(&AppCacheResponseReader::OnOpenEntryComplete, |
251 response_id_, &open_callback_->entry_ptr_, | 254 weak_factory_.GetWeakPtr(), base::Owned(entry_ptr)); |
252 base::Bind(&net::OldCompletionCallbackAdapter, open_callback_)); | 255 rv = disk_cache_->OpenEntry(response_id_, entry_ptr, open_callback_); |
253 } | 256 } |
254 | 257 |
255 if (rv != net::ERR_IO_PENDING) | 258 if (rv != net::ERR_IO_PENDING) |
256 OnOpenEntryComplete(rv); | 259 OnOpenEntryComplete(entry_ptr, rv); |
257 } | 260 } |
258 | 261 |
259 void AppCacheResponseReader::OnOpenEntryComplete(int rv) { | 262 void AppCacheResponseReader::OnOpenEntryComplete( |
263 AppCacheDiskCacheInterface::Entry** entry, int rv) { | |
260 DCHECK(info_buffer_.get() || buffer_.get()); | 264 DCHECK(info_buffer_.get() || buffer_.get()); |
261 | 265 |
262 if (open_callback_) { | 266 if (!open_callback_.is_null()) { |
263 if (rv == net::OK) { | 267 if (rv == net::OK) { |
264 entry_ = open_callback_->entry_ptr_; | 268 DCHECK(entry); |
265 open_callback_->entry_ptr_ = NULL; | 269 entry_ = *entry; |
266 } | 270 } |
267 open_callback_ = NULL; | 271 open_callback_.Reset(); |
268 } | 272 } |
269 | 273 |
270 if (info_buffer_) | 274 if (info_buffer_) |
271 ContinueReadInfo(); | 275 ContinueReadInfo(); |
272 else | 276 else |
273 ContinueReadData(); | 277 ContinueReadData(); |
274 } | 278 } |
275 | 279 |
276 // AppCacheResponseWriter ---------------------------------------------- | 280 // AppCacheResponseWriter ---------------------------------------------- |
277 | 281 |
278 AppCacheResponseWriter::AppCacheResponseWriter( | 282 AppCacheResponseWriter::AppCacheResponseWriter( |
279 int64 response_id, int64 group_id, AppCacheDiskCacheInterface* disk_cache) | 283 int64 response_id, int64 group_id, AppCacheDiskCacheInterface* disk_cache) |
280 : AppCacheResponseIO(response_id, group_id, disk_cache), | 284 : AppCacheResponseIO(response_id, group_id, disk_cache), |
281 info_size_(0), write_position_(0), write_amount_(0), | 285 info_size_(0), |
282 creation_phase_(INITIAL_ATTEMPT) { | 286 write_position_(0), |
287 write_amount_(0), | |
288 creation_phase_(INITIAL_ATTEMPT), | |
289 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | |
283 } | 290 } |
284 | 291 |
285 AppCacheResponseWriter::~AppCacheResponseWriter() { | 292 AppCacheResponseWriter::~AppCacheResponseWriter() { |
286 if (create_callback_) | |
287 create_callback_.release()->Cancel(); | |
288 } | 293 } |
289 | 294 |
290 void AppCacheResponseWriter::WriteInfo( | 295 void AppCacheResponseWriter::WriteInfo( |
291 HttpResponseInfoIOBuffer* info_buf, | 296 HttpResponseInfoIOBuffer* info_buf, |
292 const net::CompletionCallback& callback) { | 297 const net::CompletionCallback& callback) { |
293 DCHECK(!callback.is_null()); | 298 DCHECK(!callback.is_null()); |
294 DCHECK(!IsWritePending()); | 299 DCHECK(!IsWritePending()); |
295 DCHECK(info_buf); | 300 DCHECK(info_buf); |
296 DCHECK(info_buf->http_info.get()); | 301 DCHECK(info_buf->http_info.get()); |
297 DCHECK(!buffer_.get()); | 302 DCHECK(!buffer_.get()); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
347 if (!info_buffer_.get()) | 352 if (!info_buffer_.get()) |
348 write_position_ += result; | 353 write_position_ += result; |
349 else | 354 else |
350 info_size_ = result; | 355 info_size_ = result; |
351 } | 356 } |
352 InvokeUserCompletionCallback(result); | 357 InvokeUserCompletionCallback(result); |
353 } | 358 } |
354 | 359 |
355 void AppCacheResponseWriter::CreateEntryIfNeededAndContinue() { | 360 void AppCacheResponseWriter::CreateEntryIfNeededAndContinue() { |
356 int rv; | 361 int rv; |
362 AppCacheDiskCacheInterface::Entry** entry_ptr = NULL; | |
awong
2012/01/04 01:50:28
Move into else cause.
James Hawkins
2012/01/05 22:44:56
See michaeln's comment below as to why that's not
| |
357 if (entry_) { | 363 if (entry_) { |
358 creation_phase_ = NO_ATTEMPT; | 364 creation_phase_ = NO_ATTEMPT; |
359 rv = net::OK; | 365 rv = net::OK; |
360 } else if (!disk_cache_) { | 366 } else if (!disk_cache_) { |
361 creation_phase_ = NO_ATTEMPT; | 367 creation_phase_ = NO_ATTEMPT; |
362 rv = net::ERR_FAILED; | 368 rv = net::ERR_FAILED; |
363 } else { | 369 } else { |
364 creation_phase_ = INITIAL_ATTEMPT; | 370 creation_phase_ = INITIAL_ATTEMPT; |
365 create_callback_ = new EntryCallback<AppCacheResponseWriter>( | 371 entry_ptr = new(AppCacheDiskCacheInterface::Entry*); |
366 this, &AppCacheResponseWriter::OnCreateEntryComplete); | 372 create_callback_ = |
367 rv = disk_cache_->CreateEntry( | 373 base::Bind(&AppCacheResponseWriter::OnCreateEntryComplete, |
368 response_id_, &create_callback_->entry_ptr_, | 374 base::Unretained(this), base::Owned(entry_ptr)); |
michaeln
2012/01/04 02:07:17
use weak_factory_.GetWeakPtr() here?
James Hawkins
2012/01/05 22:44:56
Done.
| |
369 base::Bind(&net::OldCompletionCallbackAdapter, create_callback_)); | 375 rv = disk_cache_->CreateEntry(response_id_, entry_ptr, create_callback_); |
370 } | 376 } |
371 if (rv != net::ERR_IO_PENDING) | 377 if (rv != net::ERR_IO_PENDING) |
372 OnCreateEntryComplete(rv); | 378 OnCreateEntryComplete(entry_ptr, rv); |
awong
2012/01/04 01:50:28
Call with NULL explicitly.
Same with the pattern
michaeln
2012/01/04 02:07:17
It's not necessarily a NULL value at this point, r
| |
373 } | 379 } |
374 | 380 |
375 void AppCacheResponseWriter::OnCreateEntryComplete(int rv) { | 381 void AppCacheResponseWriter::OnCreateEntryComplete( |
382 AppCacheDiskCacheInterface::Entry** entry, int rv) { | |
376 DCHECK(info_buffer_.get() || buffer_.get()); | 383 DCHECK(info_buffer_.get() || buffer_.get()); |
377 | 384 |
385 AppCacheDiskCacheInterface::Entry** entry_ptr = NULL; | |
awong
2012/01/04 01:50:28
Move this into the "else if" clause?
James Hawkins
2012/01/05 22:44:56
Done.
| |
386 | |
378 if (creation_phase_ == INITIAL_ATTEMPT) { | 387 if (creation_phase_ == INITIAL_ATTEMPT) { |
379 if (rv != net::OK) { | 388 if (rv != net::OK) { |
380 // We may try to overwrite existing entries. | 389 // We may try to overwrite existing entries. |
381 creation_phase_ = DOOM_EXISTING; | 390 creation_phase_ = DOOM_EXISTING; |
382 rv = disk_cache_->DoomEntry( | 391 rv = disk_cache_->DoomEntry(response_id_, create_callback_); |
383 response_id_, base::Bind(&net::OldCompletionCallbackAdapter, | |
384 create_callback_)); | |
385 if (rv != net::ERR_IO_PENDING) | 392 if (rv != net::ERR_IO_PENDING) |
386 OnCreateEntryComplete(rv); | 393 OnCreateEntryComplete(NULL, rv); |
387 return; | 394 return; |
388 } | 395 } |
389 } else if (creation_phase_ == DOOM_EXISTING) { | 396 } else if (creation_phase_ == DOOM_EXISTING) { |
390 creation_phase_ = SECOND_ATTEMPT; | 397 creation_phase_ = SECOND_ATTEMPT; |
391 rv = disk_cache_->CreateEntry( | 398 entry_ptr = new(AppCacheDiskCacheInterface::Entry*); |
awong
2012/01/04 01:50:28
nit: personally I prefer
new AppCacheDiskCacheI
michaeln
2012/01/04 02:07:17
new(T) style looked odd to me too... prefer new T
James Hawkins
2012/01/05 22:44:56
Done.
James Hawkins
2012/01/05 22:44:56
Done.
| |
392 response_id_, &create_callback_->entry_ptr_, | 399 create_callback_ = |
393 base::Bind(&net::OldCompletionCallbackAdapter, create_callback_)); | 400 base::Bind(&AppCacheResponseWriter::OnCreateEntryComplete, |
401 base::Unretained(this), base::Owned(entry_ptr)); | |
michaeln
2012/01/04 02:07:17
use weak_factory_.GetWeakPtr() here?
James Hawkins
2012/01/05 22:44:56
Done.
| |
402 rv = disk_cache_->CreateEntry(response_id_, entry_ptr, create_callback_); | |
394 if (rv != net::ERR_IO_PENDING) | 403 if (rv != net::ERR_IO_PENDING) |
395 OnCreateEntryComplete(rv); | 404 OnCreateEntryComplete(entry_ptr, rv); |
396 return; | 405 return; |
397 } | 406 } |
398 | 407 |
399 if (create_callback_) { | 408 if (!create_callback_.is_null()) { |
400 if (rv == net::OK) { | 409 if (rv == net::OK) |
401 entry_ = create_callback_->entry_ptr_; | 410 entry_ = *entry; |
402 create_callback_->entry_ptr_ = NULL; | 411 |
403 } | 412 create_callback_.Reset(); |
404 create_callback_ = NULL; | |
405 } | 413 } |
406 | 414 |
407 if (info_buffer_) | 415 if (info_buffer_) |
408 ContinueWriteInfo(); | 416 ContinueWriteInfo(); |
409 else | 417 else |
410 ContinueWriteData(); | 418 ContinueWriteData(); |
411 } | 419 } |
412 | 420 |
413 } // namespace appcache | 421 } // namespace appcache |
OLD | NEW |