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