| OLD | NEW |
| 1 // Copyright (c) 2012 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" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 if (!entry_) { | 197 if (!entry_) { |
| 198 ScheduleIOCompletionCallback(net::ERR_CACHE_MISS); | 198 ScheduleIOCompletionCallback(net::ERR_CACHE_MISS); |
| 199 return; | 199 return; |
| 200 } | 200 } |
| 201 | 201 |
| 202 if (read_position_ + buffer_len_ > range_length_) { | 202 if (read_position_ + buffer_len_ > range_length_) { |
| 203 // TODO(michaeln): What about integer overflows? | 203 // TODO(michaeln): What about integer overflows? |
| 204 DCHECK(range_length_ >= read_position_); | 204 DCHECK(range_length_ >= read_position_); |
| 205 buffer_len_ = range_length_ - read_position_; | 205 buffer_len_ = range_length_ - read_position_; |
| 206 } | 206 } |
| 207 ReadRaw(kResponseContentIndex, range_offset_ + read_position_, | 207 ReadRaw(kResponseContentIndex, |
| 208 buffer_, buffer_len_); | 208 range_offset_ + read_position_, |
| 209 buffer_.get(), |
| 210 buffer_len_); |
| 209 } | 211 } |
| 210 | 212 |
| 211 void AppCacheResponseReader::SetReadRange(int offset, int length) { | 213 void AppCacheResponseReader::SetReadRange(int offset, int length) { |
| 212 DCHECK(!IsReadPending() && !read_position_); | 214 DCHECK(!IsReadPending() && !read_position_); |
| 213 range_offset_ = offset; | 215 range_offset_ = offset; |
| 214 range_length_ = length; | 216 range_length_ = length; |
| 215 } | 217 } |
| 216 | 218 |
| 217 void AppCacheResponseReader::OnIOComplete(int result) { | 219 void AppCacheResponseReader::OnIOComplete(int result) { |
| 218 if (result >= 0) { | 220 if (result >= 0) { |
| 219 if (info_buffer_) { | 221 if (info_buffer_.get()) { |
| 220 // Deserialize the http info structure, ensuring we got headers. | 222 // Deserialize the http info structure, ensuring we got headers. |
| 221 Pickle pickle(buffer_->data(), result); | 223 Pickle pickle(buffer_->data(), result); |
| 222 scoped_ptr<net::HttpResponseInfo> info(new net::HttpResponseInfo); | 224 scoped_ptr<net::HttpResponseInfo> info(new net::HttpResponseInfo); |
| 223 bool response_truncated = false; | 225 bool response_truncated = false; |
| 224 if (!info->InitFromPickle(pickle, &response_truncated) || | 226 if (!info->InitFromPickle(pickle, &response_truncated) || |
| 225 !info->headers) { | 227 !info->headers.get()) { |
| 226 InvokeUserCompletionCallback(net::ERR_FAILED); | 228 InvokeUserCompletionCallback(net::ERR_FAILED); |
| 227 return; | 229 return; |
| 228 } | 230 } |
| 229 DCHECK(!response_truncated); | 231 DCHECK(!response_truncated); |
| 230 info_buffer_->http_info.reset(info.release()); | 232 info_buffer_->http_info.reset(info.release()); |
| 231 | 233 |
| 232 // Also return the size of the response body | 234 // Also return the size of the response body |
| 233 DCHECK(entry_); | 235 DCHECK(entry_); |
| 234 info_buffer_->response_data_size = | 236 info_buffer_->response_data_size = |
| 235 entry_->GetSize(kResponseContentIndex); | 237 entry_->GetSize(kResponseContentIndex); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 264 DCHECK(info_buffer_.get() || buffer_.get()); | 266 DCHECK(info_buffer_.get() || buffer_.get()); |
| 265 | 267 |
| 266 if (!open_callback_.is_null()) { | 268 if (!open_callback_.is_null()) { |
| 267 if (rv == net::OK) { | 269 if (rv == net::OK) { |
| 268 DCHECK(entry); | 270 DCHECK(entry); |
| 269 entry_ = *entry; | 271 entry_ = *entry; |
| 270 } | 272 } |
| 271 open_callback_.Reset(); | 273 open_callback_.Reset(); |
| 272 } | 274 } |
| 273 | 275 |
| 274 if (info_buffer_) | 276 if (info_buffer_.get()) |
| 275 ContinueReadInfo(); | 277 ContinueReadInfo(); |
| 276 else | 278 else |
| 277 ContinueReadData(); | 279 ContinueReadData(); |
| 278 } | 280 } |
| 279 | 281 |
| 280 // AppCacheResponseWriter ---------------------------------------------- | 282 // AppCacheResponseWriter ---------------------------------------------- |
| 281 | 283 |
| 282 AppCacheResponseWriter::AppCacheResponseWriter( | 284 AppCacheResponseWriter::AppCacheResponseWriter( |
| 283 int64 response_id, int64 group_id, AppCacheDiskCacheInterface* disk_cache) | 285 int64 response_id, int64 group_id, AppCacheDiskCacheInterface* disk_cache) |
| 284 : AppCacheResponseIO(response_id, group_id, disk_cache), | 286 : AppCacheResponseIO(response_id, group_id, disk_cache), |
| 285 info_size_(0), | 287 info_size_(0), |
| 286 write_position_(0), | 288 write_position_(0), |
| 287 write_amount_(0), | 289 write_amount_(0), |
| 288 creation_phase_(INITIAL_ATTEMPT), | 290 creation_phase_(INITIAL_ATTEMPT), |
| 289 weak_factory_(this) { | 291 weak_factory_(this) { |
| 290 } | 292 } |
| 291 | 293 |
| 292 AppCacheResponseWriter::~AppCacheResponseWriter() { | 294 AppCacheResponseWriter::~AppCacheResponseWriter() { |
| 293 } | 295 } |
| 294 | 296 |
| 295 void AppCacheResponseWriter::WriteInfo( | 297 void AppCacheResponseWriter::WriteInfo( |
| 296 HttpResponseInfoIOBuffer* info_buf, | 298 HttpResponseInfoIOBuffer* info_buf, |
| 297 const net::CompletionCallback& callback) { | 299 const net::CompletionCallback& callback) { |
| 298 DCHECK(!callback.is_null()); | 300 DCHECK(!callback.is_null()); |
| 299 DCHECK(!IsWritePending()); | 301 DCHECK(!IsWritePending()); |
| 300 DCHECK(info_buf); | 302 DCHECK(info_buf); |
| 301 DCHECK(info_buf->http_info.get()); | 303 DCHECK(info_buf->http_info.get()); |
| 302 DCHECK(!buffer_.get()); | 304 DCHECK(!buffer_.get()); |
| 303 DCHECK(!info_buffer_.get()); | 305 DCHECK(!info_buffer_.get()); |
| 304 DCHECK(info_buf->http_info->headers); | 306 DCHECK(info_buf->http_info->headers.get()); |
| 305 | 307 |
| 306 info_buffer_ = info_buf; | 308 info_buffer_ = info_buf; |
| 307 callback_ = callback; // cleared on completion | 309 callback_ = callback; // cleared on completion |
| 308 CreateEntryIfNeededAndContinue(); | 310 CreateEntryIfNeededAndContinue(); |
| 309 } | 311 } |
| 310 | 312 |
| 311 void AppCacheResponseWriter::ContinueWriteInfo() { | 313 void AppCacheResponseWriter::ContinueWriteInfo() { |
| 312 if (!entry_) { | 314 if (!entry_) { |
| 313 ScheduleIOCompletionCallback(net::ERR_FAILED); | 315 ScheduleIOCompletionCallback(net::ERR_FAILED); |
| 314 return; | 316 return; |
| 315 } | 317 } |
| 316 | 318 |
| 317 const bool kSkipTransientHeaders = true; | 319 const bool kSkipTransientHeaders = true; |
| 318 const bool kTruncated = false; | 320 const bool kTruncated = false; |
| 319 Pickle* pickle = new Pickle; | 321 Pickle* pickle = new Pickle; |
| 320 info_buffer_->http_info->Persist(pickle, kSkipTransientHeaders, kTruncated); | 322 info_buffer_->http_info->Persist(pickle, kSkipTransientHeaders, kTruncated); |
| 321 write_amount_ = static_cast<int>(pickle->size()); | 323 write_amount_ = static_cast<int>(pickle->size()); |
| 322 buffer_ = new WrappedPickleIOBuffer(pickle); // takes ownership of pickle | 324 buffer_ = new WrappedPickleIOBuffer(pickle); // takes ownership of pickle |
| 323 WriteRaw(kResponseInfoIndex, 0, buffer_, write_amount_); | 325 WriteRaw(kResponseInfoIndex, 0, buffer_.get(), write_amount_); |
| 324 } | 326 } |
| 325 | 327 |
| 326 void AppCacheResponseWriter::WriteData( | 328 void AppCacheResponseWriter::WriteData( |
| 327 net::IOBuffer* buf, int buf_len, const net::CompletionCallback& callback) { | 329 net::IOBuffer* buf, int buf_len, const net::CompletionCallback& callback) { |
| 328 DCHECK(!callback.is_null()); | 330 DCHECK(!callback.is_null()); |
| 329 DCHECK(!IsWritePending()); | 331 DCHECK(!IsWritePending()); |
| 330 DCHECK(buf); | 332 DCHECK(buf); |
| 331 DCHECK(buf_len >= 0); | 333 DCHECK(buf_len >= 0); |
| 332 DCHECK(!buffer_.get()); | 334 DCHECK(!buffer_.get()); |
| 333 DCHECK(!info_buffer_.get()); | 335 DCHECK(!info_buffer_.get()); |
| 334 | 336 |
| 335 buffer_ = buf; | 337 buffer_ = buf; |
| 336 write_amount_ = buf_len; | 338 write_amount_ = buf_len; |
| 337 callback_ = callback; // cleared on completion | 339 callback_ = callback; // cleared on completion |
| 338 CreateEntryIfNeededAndContinue(); | 340 CreateEntryIfNeededAndContinue(); |
| 339 } | 341 } |
| 340 | 342 |
| 341 void AppCacheResponseWriter::ContinueWriteData() { | 343 void AppCacheResponseWriter::ContinueWriteData() { |
| 342 if (!entry_) { | 344 if (!entry_) { |
| 343 ScheduleIOCompletionCallback(net::ERR_FAILED); | 345 ScheduleIOCompletionCallback(net::ERR_FAILED); |
| 344 return; | 346 return; |
| 345 } | 347 } |
| 346 WriteRaw(kResponseContentIndex, write_position_, buffer_, write_amount_); | 348 WriteRaw( |
| 349 kResponseContentIndex, write_position_, buffer_.get(), write_amount_); |
| 347 } | 350 } |
| 348 | 351 |
| 349 void AppCacheResponseWriter::OnIOComplete(int result) { | 352 void AppCacheResponseWriter::OnIOComplete(int result) { |
| 350 if (result >= 0) { | 353 if (result >= 0) { |
| 351 DCHECK(write_amount_ == result); | 354 DCHECK(write_amount_ == result); |
| 352 if (!info_buffer_) | 355 if (!info_buffer_.get()) |
| 353 write_position_ += result; | 356 write_position_ += result; |
| 354 else | 357 else |
| 355 info_size_ = result; | 358 info_size_ = result; |
| 356 } | 359 } |
| 357 InvokeUserCompletionCallback(result); | 360 InvokeUserCompletionCallback(result); |
| 358 } | 361 } |
| 359 | 362 |
| 360 void AppCacheResponseWriter::CreateEntryIfNeededAndContinue() { | 363 void AppCacheResponseWriter::CreateEntryIfNeededAndContinue() { |
| 361 int rv; | 364 int rv; |
| 362 AppCacheDiskCacheInterface::Entry** entry_ptr = NULL; | 365 AppCacheDiskCacheInterface::Entry** entry_ptr = NULL; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 404 return; | 407 return; |
| 405 } | 408 } |
| 406 | 409 |
| 407 if (!create_callback_.is_null()) { | 410 if (!create_callback_.is_null()) { |
| 408 if (rv == net::OK) | 411 if (rv == net::OK) |
| 409 entry_ = *entry; | 412 entry_ = *entry; |
| 410 | 413 |
| 411 create_callback_.Reset(); | 414 create_callback_.Reset(); |
| 412 } | 415 } |
| 413 | 416 |
| 414 if (info_buffer_) | 417 if (info_buffer_.get()) |
| 415 ContinueWriteInfo(); | 418 ContinueWriteInfo(); |
| 416 else | 419 else |
| 417 ContinueWriteData(); | 420 ContinueWriteData(); |
| 418 } | 421 } |
| 419 | 422 |
| 420 } // namespace appcache | 423 } // namespace appcache |
| OLD | NEW |