Chromium Code Reviews| 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 "content/common/net/url_fetcher_impl.h" | 5 #include "content/common/net/url_fetcher_impl.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 92 int size() const { | 92 int size() const { |
| 93 return fetchers_.size(); | 93 return fetchers_.size(); |
| 94 } | 94 } |
| 95 | 95 |
| 96 private: | 96 private: |
| 97 std::set<Core*> fetchers_; | 97 std::set<Core*> fetchers_; |
| 98 | 98 |
| 99 DISALLOW_COPY_AND_ASSIGN(Registry); | 99 DISALLOW_COPY_AND_ASSIGN(Registry); |
| 100 }; | 100 }; |
| 101 | 101 |
| 102 // Class TempFileWriter encapsulates all state involved in writing | 102 // Class FileWriter encapsulates all state involved in writing response bytes |
| 103 // response bytes to a temporary file. It is only used if | 103 // to a file. It is only used if |Core::response_destination_| == FILE. |
| 104 // |Core::response_destination_| == TEMP_FILE. Each instance of | 104 // Each instance of FileWriter is owned by a URLFetcher::Core, which manages |
| 105 // TempFileWriter is owned by a URLFetcher::Core, which manages | |
| 106 // its lifetime and never transfers ownership. While writing to | 105 // its lifetime and never transfers ownership. While writing to |
| 107 // a file, all function calls happen on the IO thread. | 106 // a file, all function calls happen on the IO thread. |
| 108 class TempFileWriter { | 107 class FileWriter { |
| 109 public: | 108 public: |
| 110 TempFileWriter( | 109 FileWriter(URLFetcherImpl::Core* core, |
| 111 URLFetcherImpl::Core* core, | 110 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy); |
| 112 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy); | |
| 113 | 111 |
| 114 ~TempFileWriter(); | 112 ~FileWriter(); |
| 115 void CreateTempFile(); | 113 void CreateTempFile(); |
|
willchan no longer on Chromium
2012/03/02 19:48:22
Rename these too?
hashimoto
2012/03/03 02:56:37
CreateTempFile is not renamed because it is a meth
| |
| 116 void DidCreateTempFile(base::PlatformFileError error_code, | 114 void DidCreateTempFile(base::PlatformFileError error_code, |
| 117 base::PassPlatformFile file_handle, | 115 base::PassPlatformFile file_handle, |
| 118 const FilePath& file_path); | 116 const FilePath& file_path); |
| 119 | 117 |
| 120 // Record |num_bytes_| response bytes in |core_->buffer_| to the file. | 118 // Record |num_bytes_| response bytes in |core_->buffer_| to the file. |
| 121 void WriteBuffer(int num_bytes); | 119 void WriteBuffer(int num_bytes); |
| 122 | 120 |
| 123 // Called when a write has been done. Continues writing if there are | 121 // Called when a write has been done. Continues writing if there are |
| 124 // any more bytes to write. Otherwise, initiates a read in core_. | 122 // any more bytes to write. Otherwise, initiates a read in core_. |
| 125 void ContinueWrite(base::PlatformFileError error_code, | 123 void ContinueWrite(base::PlatformFileError error_code, int bytes_written); |
| 126 int bytes_written); | |
| 127 | 124 |
| 128 // Drop ownership of the file at path |temp_file_|. This class | 125 // Drop ownership of the file at |file_path_|. |
| 129 // will not delete it or write to it again. | 126 // This class will not delete it or write to it again. |
| 130 void DisownTempFile(); | 127 void DisownFile(); |
| 131 | 128 |
| 132 // Close the temp file if it is open. | 129 // Close the file if it is open. |
| 133 void CloseTempFileAndCompleteRequest(); | 130 void CloseFileAndCompleteRequest(); |
| 134 | 131 |
| 135 // Remove the temp file if we we created one. | 132 // Remove the file if we have created one. |
| 136 void RemoveTempFile(); | 133 void RemoveFile(); |
| 137 | 134 |
| 138 const FilePath& temp_file() const { return temp_file_; } | 135 const FilePath& file_path() const { return file_path_; } |
| 139 int64 total_bytes_written() { return total_bytes_written_; } | 136 int64 total_bytes_written() { return total_bytes_written_; } |
| 140 base::PlatformFileError error_code() const { return error_code_; } | 137 base::PlatformFileError error_code() const { return error_code_; } |
| 141 | 138 |
| 142 private: | 139 private: |
| 143 // Callback which gets the result of closing the temp file. | 140 // Callback which gets the result of closing the file. |
| 144 void DidCloseTempFile(base::PlatformFileError error); | 141 void DidCloseFile(base::PlatformFileError error); |
| 145 | 142 |
| 146 // The URLFetcherImpl::Core which instantiated this class. | 143 // The URLFetcherImpl::Core which instantiated this class. |
| 147 URLFetcherImpl::Core* core_; | 144 URLFetcherImpl::Core* core_; |
| 148 | 145 |
| 149 // The last error encountered on a file operation. base::PLATFORM_FILE_OK | 146 // The last error encountered on a file operation. base::PLATFORM_FILE_OK |
| 150 // if no error occurred. | 147 // if no error occurred. |
| 151 base::PlatformFileError error_code_; | 148 base::PlatformFileError error_code_; |
| 152 | 149 |
| 153 // Callbacks are created for use with base::FileUtilProxy. | 150 // Callbacks are created for use with base::FileUtilProxy. |
| 154 base::WeakPtrFactory<URLFetcherImpl::Core::TempFileWriter> weak_factory_; | 151 base::WeakPtrFactory<URLFetcherImpl::Core::FileWriter> weak_factory_; |
| 155 | 152 |
| 156 // Message loop on which file operations should happen. | 153 // Message loop on which file operations should happen. |
| 157 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy_; | 154 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy_; |
| 158 | 155 |
| 159 // Path to the temporary file. This path is empty when there | 156 // Path to the file. This path is empty when there is no file. |
| 160 // is no temp file. | 157 FilePath file_path_; |
| 161 FilePath temp_file_; | |
| 162 | 158 |
| 163 // Handle to the temp file. | 159 // Handle to the file. |
| 164 base::PlatformFile temp_file_handle_; | 160 base::PlatformFile file_handle_; |
| 165 | 161 |
| 166 // We always append to the file. Track the total number of bytes | 162 // We always append to the file. Track the total number of bytes |
| 167 // written, so that writes know the offset to give. | 163 // written, so that writes know the offset to give. |
| 168 int64 total_bytes_written_; | 164 int64 total_bytes_written_; |
| 169 | 165 |
| 170 // How many bytes did the last Write() try to write? Needed so | 166 // How many bytes did the last Write() try to write? Needed so |
| 171 // that if not all the bytes get written on a Write(), we can | 167 // that if not all the bytes get written on a Write(), we can |
| 172 // call Write() again with the rest. | 168 // call Write() again with the rest. |
| 173 int pending_bytes_; | 169 int pending_bytes_; |
| 174 | 170 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 208 // Store the response bytes in |buffer_| in the container indicated by | 204 // Store the response bytes in |buffer_| in the container indicated by |
| 209 // |response_destination_|. Return true if the write has been | 205 // |response_destination_|. Return true if the write has been |
| 210 // done, and another read can overwrite |buffer_|. If this function | 206 // done, and another read can overwrite |buffer_|. If this function |
| 211 // returns false, it will post a task that will read more bytes once the | 207 // returns false, it will post a task that will read more bytes once the |
| 212 // write is complete. | 208 // write is complete. |
| 213 bool WriteBuffer(int num_bytes); | 209 bool WriteBuffer(int num_bytes); |
| 214 | 210 |
| 215 // Read response bytes from the request. | 211 // Read response bytes from the request. |
| 216 void ReadResponse(); | 212 void ReadResponse(); |
| 217 | 213 |
| 218 // Drop ownership of any temp file managed by |temp_file_|. | 214 // Drop ownership of any file managed by |file_path_|. |
| 219 void DisownTempFile(); | 215 void DisownFile(); |
| 220 | 216 |
| 221 // Notify Delegate about the progress of downloading. | 217 // Notify Delegate about the progress of downloading. |
| 222 void InformDelegateDownloadProgress(); | 218 void InformDelegateDownloadProgress(); |
| 223 void InformDelegateDownloadProgressInDelegateThread(int64 current, | 219 void InformDelegateDownloadProgressInDelegateThread(int64 current, |
| 224 int64 total); | 220 int64 total); |
| 225 | 221 |
| 226 URLFetcherImpl* fetcher_; // Corresponding fetcher object | 222 URLFetcherImpl* fetcher_; // Corresponding fetcher object |
| 227 GURL original_url_; // The URL we were asked to fetch | 223 GURL original_url_; // The URL we were asked to fetch |
| 228 GURL url_; // The URL we eventually wound up at | 224 GURL url_; // The URL we eventually wound up at |
| 229 RequestType request_type_; // What type of request is this? | 225 RequestType request_type_; // What type of request is this? |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 272 scoped_refptr<net::URLRequestThrottlerEntryInterface> url_throttler_entry_; | 268 scoped_refptr<net::URLRequestThrottlerEntryInterface> url_throttler_entry_; |
| 273 | 269 |
| 274 // |num_retries_| indicates how many times we've failed to successfully | 270 // |num_retries_| indicates how many times we've failed to successfully |
| 275 // fetch this URL. Once this value exceeds the maximum number of retries | 271 // fetch this URL. Once this value exceeds the maximum number of retries |
| 276 // specified by the owner URLFetcher instance, we'll give up. | 272 // specified by the owner URLFetcher instance, we'll give up. |
| 277 int num_retries_; | 273 int num_retries_; |
| 278 | 274 |
| 279 // True if the URLFetcher has been cancelled. | 275 // True if the URLFetcher has been cancelled. |
| 280 bool was_cancelled_; | 276 bool was_cancelled_; |
| 281 | 277 |
| 282 // If writing results to a file, |temp_file_writer_| will manage creation, | 278 // If writing results to a file, |file_writer_| will manage creation, |
| 283 // writing, and destruction of that file. | 279 // writing, and destruction of that file. |
| 284 scoped_ptr<TempFileWriter> temp_file_writer_; | 280 scoped_ptr<FileWriter> file_writer_; |
| 285 | 281 |
| 286 // Where should responses be saved? | 282 // Where should responses be saved? |
| 287 ResponseDestinationType response_destination_; | 283 ResponseDestinationType response_destination_; |
| 288 | 284 |
| 289 // If |automatically_retry_on_5xx_| is false, 5xx responses will be | 285 // If |automatically_retry_on_5xx_| is false, 5xx responses will be |
| 290 // propagated to the observer, if it is true URLFetcher will automatically | 286 // propagated to the observer, if it is true URLFetcher will automatically |
| 291 // re-execute the request, after the back-off delay has expired. | 287 // re-execute the request, after the back-off delay has expired. |
| 292 // true by default. | 288 // true by default. |
| 293 bool automatically_retry_on_5xx_; | 289 bool automatically_retry_on_5xx_; |
| 294 // Maximum retries allowed. | 290 // Maximum retries allowed. |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 322 | 318 |
| 323 void URLFetcherImpl::Core::Registry::CancelAll() { | 319 void URLFetcherImpl::Core::Registry::CancelAll() { |
| 324 while (!fetchers_.empty()) | 320 while (!fetchers_.empty()) |
| 325 (*fetchers_.begin())->CancelURLRequest(); | 321 (*fetchers_.begin())->CancelURLRequest(); |
| 326 } | 322 } |
| 327 | 323 |
| 328 // static | 324 // static |
| 329 base::LazyInstance<URLFetcherImpl::Core::Registry> | 325 base::LazyInstance<URLFetcherImpl::Core::Registry> |
| 330 URLFetcherImpl::Core::g_registry = LAZY_INSTANCE_INITIALIZER; | 326 URLFetcherImpl::Core::g_registry = LAZY_INSTANCE_INITIALIZER; |
| 331 | 327 |
| 332 URLFetcherImpl::Core::TempFileWriter::TempFileWriter( | 328 URLFetcherImpl::Core::FileWriter::FileWriter( |
| 333 URLFetcherImpl::Core* core, | 329 URLFetcherImpl::Core* core, |
| 334 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) | 330 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) |
| 335 : core_(core), | 331 : core_(core), |
| 336 error_code_(base::PLATFORM_FILE_OK), | 332 error_code_(base::PLATFORM_FILE_OK), |
| 337 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), | 333 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), |
| 338 file_message_loop_proxy_(file_message_loop_proxy), | 334 file_message_loop_proxy_(file_message_loop_proxy), |
| 339 temp_file_handle_(base::kInvalidPlatformFileValue) { | 335 file_handle_(base::kInvalidPlatformFileValue) { |
| 340 } | 336 } |
| 341 | 337 |
| 342 URLFetcherImpl::Core::TempFileWriter::~TempFileWriter() { | 338 URLFetcherImpl::Core::FileWriter::~FileWriter() { |
| 343 RemoveTempFile(); | 339 RemoveFile(); |
| 344 } | 340 } |
| 345 | 341 |
| 346 void URLFetcherImpl::Core::TempFileWriter::CreateTempFile() { | 342 void URLFetcherImpl::Core::FileWriter::CreateTempFile() { |
| 347 DCHECK(core_->io_message_loop_proxy_->BelongsToCurrentThread()); | 343 DCHECK(core_->io_message_loop_proxy_->BelongsToCurrentThread()); |
| 348 DCHECK(file_message_loop_proxy_.get()); | 344 DCHECK(file_message_loop_proxy_.get()); |
| 349 base::FileUtilProxy::CreateTemporary( | 345 base::FileUtilProxy::CreateTemporary( |
| 350 file_message_loop_proxy_, | 346 file_message_loop_proxy_, |
| 351 0, // No additional file flags. | 347 0, // No additional file flags. |
| 352 base::Bind(&URLFetcherImpl::Core::TempFileWriter::DidCreateTempFile, | 348 base::Bind(&URLFetcherImpl::Core::FileWriter::DidCreateTempFile, |
| 353 weak_factory_.GetWeakPtr())); | 349 weak_factory_.GetWeakPtr())); |
| 354 } | 350 } |
| 355 | 351 |
| 356 void URLFetcherImpl::Core::TempFileWriter::DidCreateTempFile( | 352 void URLFetcherImpl::Core::FileWriter::DidCreateTempFile( |
| 357 base::PlatformFileError error_code, | 353 base::PlatformFileError error_code, |
| 358 base::PassPlatformFile file_handle, | 354 base::PassPlatformFile file_handle, |
| 359 const FilePath& file_path) { | 355 const FilePath& file_path) { |
| 360 DCHECK(core_->io_message_loop_proxy_->BelongsToCurrentThread()); | 356 DCHECK(core_->io_message_loop_proxy_->BelongsToCurrentThread()); |
| 361 | 357 |
| 362 if (base::PLATFORM_FILE_OK != error_code) { | 358 if (base::PLATFORM_FILE_OK != error_code) { |
| 363 error_code_ = error_code; | 359 error_code_ = error_code; |
| 364 RemoveTempFile(); | 360 RemoveFile(); |
| 365 core_->delegate_loop_proxy_->PostTask( | 361 core_->delegate_loop_proxy_->PostTask( |
| 366 FROM_HERE, base::Bind(&Core::InformDelegateFetchIsComplete, core_)); | 362 FROM_HERE, base::Bind(&Core::InformDelegateFetchIsComplete, core_)); |
| 367 return; | 363 return; |
| 368 } | 364 } |
| 369 | 365 |
| 370 temp_file_ = file_path; | 366 file_path_ = file_path; |
| 371 temp_file_handle_ = file_handle.ReleaseValue(); | 367 file_handle_ = file_handle.ReleaseValue(); |
| 372 total_bytes_written_ = 0; | 368 total_bytes_written_ = 0; |
| 373 | 369 |
| 374 core_->io_message_loop_proxy_->PostTask( | 370 core_->io_message_loop_proxy_->PostTask( |
| 375 FROM_HERE, base::Bind(&Core::StartURLRequestWhenAppropriate, core_)); | 371 FROM_HERE, base::Bind(&Core::StartURLRequestWhenAppropriate, core_)); |
| 376 } | 372 } |
| 377 | 373 |
| 378 void URLFetcherImpl::Core::TempFileWriter::WriteBuffer(int num_bytes) { | 374 void URLFetcherImpl::Core::FileWriter::WriteBuffer(int num_bytes) { |
| 379 DCHECK(core_->io_message_loop_proxy_->BelongsToCurrentThread()); | 375 DCHECK(core_->io_message_loop_proxy_->BelongsToCurrentThread()); |
| 380 | 376 |
| 381 // Start writing to the temp file by setting the initial state | 377 // Start writing to the file by setting the initial state |
| 382 // of |pending_bytes_| and |buffer_offset_| to indicate that the | 378 // of |pending_bytes_| and |buffer_offset_| to indicate that the |
| 383 // entire buffer has not yet been written. | 379 // entire buffer has not yet been written. |
| 384 pending_bytes_ = num_bytes; | 380 pending_bytes_ = num_bytes; |
| 385 buffer_offset_ = 0; | 381 buffer_offset_ = 0; |
| 386 ContinueWrite(base::PLATFORM_FILE_OK, 0); | 382 ContinueWrite(base::PLATFORM_FILE_OK, 0); |
| 387 } | 383 } |
| 388 | 384 |
| 389 void URLFetcherImpl::Core::TempFileWriter::ContinueWrite( | 385 void URLFetcherImpl::Core::FileWriter::ContinueWrite( |
| 390 base::PlatformFileError error_code, | 386 base::PlatformFileError error_code, |
| 391 int bytes_written) { | 387 int bytes_written) { |
| 392 DCHECK(core_->io_message_loop_proxy_->BelongsToCurrentThread()); | 388 DCHECK(core_->io_message_loop_proxy_->BelongsToCurrentThread()); |
| 393 | 389 |
| 394 if (temp_file_handle_ == base::kInvalidPlatformFileValue) { | 390 if (file_handle_ == base::kInvalidPlatformFileValue) { |
| 395 // While a write was being done on the file thread, a request | 391 // While a write was being done on the file thread, a request |
| 396 // to close or disown the file occured on the IO thread. At | 392 // to close or disown the file occured on the IO thread. At |
| 397 // this point a request to close the file is pending on the | 393 // this point a request to close the file is pending on the |
| 398 // file thread. | 394 // file thread. |
| 399 return; | 395 return; |
| 400 } | 396 } |
| 401 | 397 |
| 402 // Every code path that resets |core_->request_| should reset | 398 // Every code path that resets |core_->request_| should reset |
| 403 // |core->temp_file_writer_| or cause the temp file writer to | 399 // |core->file_writer_| or cause the file writer to disown the file. In the |
| 404 // disown the temp file. In the former case, this callback can | 400 // former case, this callback can not be called, because the weak pointer to |
| 405 // not be called, because the weak pointer to |this| will be NULL. | 401 // |this| will be NULL. In the latter case, the check of |file_handle_| at the |
| 406 // In the latter case, the check of |temp_file_handle_| at the start | 402 // start of this method ensures that we can not reach this point. |
| 407 // of this method ensures that we can not reach this point. | |
| 408 CHECK(core_->request_.get()); | 403 CHECK(core_->request_.get()); |
| 409 | 404 |
| 410 if (base::PLATFORM_FILE_OK != error_code) { | 405 if (base::PLATFORM_FILE_OK != error_code) { |
| 411 error_code_ = error_code; | 406 error_code_ = error_code; |
| 412 RemoveTempFile(); | 407 RemoveFile(); |
| 413 core_->delegate_loop_proxy_->PostTask( | 408 core_->delegate_loop_proxy_->PostTask( |
| 414 FROM_HERE, base::Bind(&Core::InformDelegateFetchIsComplete, core_)); | 409 FROM_HERE, base::Bind(&Core::InformDelegateFetchIsComplete, core_)); |
| 415 return; | 410 return; |
| 416 } | 411 } |
| 417 | 412 |
| 418 total_bytes_written_ += bytes_written; | 413 total_bytes_written_ += bytes_written; |
| 419 buffer_offset_ += bytes_written; | 414 buffer_offset_ += bytes_written; |
| 420 pending_bytes_ -= bytes_written; | 415 pending_bytes_ -= bytes_written; |
| 421 | 416 |
| 422 if (pending_bytes_ > 0) { | 417 if (pending_bytes_ > 0) { |
| 423 base::FileUtilProxy::Write( | 418 base::FileUtilProxy::Write( |
| 424 file_message_loop_proxy_, temp_file_handle_, | 419 file_message_loop_proxy_, file_handle_, |
| 425 total_bytes_written_, // Append to the end | 420 total_bytes_written_, // Append to the end |
| 426 (core_->buffer_->data() + buffer_offset_), pending_bytes_, | 421 (core_->buffer_->data() + buffer_offset_), pending_bytes_, |
| 427 base::Bind(&URLFetcherImpl::Core::TempFileWriter::ContinueWrite, | 422 base::Bind(&URLFetcherImpl::Core::FileWriter::ContinueWrite, |
| 428 weak_factory_.GetWeakPtr())); | 423 weak_factory_.GetWeakPtr())); |
| 429 } else { | 424 } else { |
| 430 // Finished writing core_->buffer_ to the file. Read some more. | 425 // Finished writing core_->buffer_ to the file. Read some more. |
| 431 core_->ReadResponse(); | 426 core_->ReadResponse(); |
| 432 } | 427 } |
| 433 } | 428 } |
| 434 | 429 |
| 435 void URLFetcherImpl::Core::TempFileWriter::DisownTempFile() { | 430 void URLFetcherImpl::Core::FileWriter::DisownFile() { |
| 436 DCHECK(core_->io_message_loop_proxy_->BelongsToCurrentThread()); | 431 DCHECK(core_->io_message_loop_proxy_->BelongsToCurrentThread()); |
| 437 | 432 |
| 438 // Disowning is done by the delegate's OnURLFetchComplete method. | 433 // Disowning is done by the delegate's OnURLFetchComplete method. |
| 439 // The temp file should be closed by the time that method is called. | 434 // The file should be closed by the time that method is called. |
| 440 DCHECK(temp_file_handle_ == base::kInvalidPlatformFileValue); | 435 DCHECK(file_handle_ == base::kInvalidPlatformFileValue); |
| 441 | 436 |
| 442 // Forget about any temp file by reseting the path. | 437 // Forget about any file by reseting the path. |
| 443 temp_file_ = FilePath(); | 438 file_path_.clear(); |
| 444 } | 439 } |
| 445 | 440 |
| 446 void URLFetcherImpl::Core::TempFileWriter::CloseTempFileAndCompleteRequest() { | 441 void URLFetcherImpl::Core::FileWriter::CloseFileAndCompleteRequest() { |
| 447 DCHECK(core_->io_message_loop_proxy_->BelongsToCurrentThread()); | 442 DCHECK(core_->io_message_loop_proxy_->BelongsToCurrentThread()); |
| 448 | 443 |
| 449 if (temp_file_handle_ != base::kInvalidPlatformFileValue) { | 444 if (file_handle_ != base::kInvalidPlatformFileValue) { |
| 450 base::FileUtilProxy::Close( | 445 base::FileUtilProxy::Close( |
| 451 file_message_loop_proxy_, temp_file_handle_, | 446 file_message_loop_proxy_, file_handle_, |
| 452 base::Bind(&URLFetcherImpl::Core::TempFileWriter::DidCloseTempFile, | 447 base::Bind(&URLFetcherImpl::Core::FileWriter::DidCloseFile, |
| 453 weak_factory_.GetWeakPtr())); | 448 weak_factory_.GetWeakPtr())); |
| 454 temp_file_handle_ = base::kInvalidPlatformFileValue; | 449 file_handle_ = base::kInvalidPlatformFileValue; |
| 455 } | 450 } |
| 456 } | 451 } |
| 457 | 452 |
| 458 void URLFetcherImpl::Core::TempFileWriter::DidCloseTempFile( | 453 void URLFetcherImpl::Core::FileWriter::DidCloseFile( |
| 459 base::PlatformFileError error_code) { | 454 base::PlatformFileError error_code) { |
| 460 DCHECK(core_->io_message_loop_proxy_->BelongsToCurrentThread()); | 455 DCHECK(core_->io_message_loop_proxy_->BelongsToCurrentThread()); |
| 461 | 456 |
| 462 if (base::PLATFORM_FILE_OK != error_code) { | 457 if (base::PLATFORM_FILE_OK != error_code) { |
| 463 error_code_ = error_code; | 458 error_code_ = error_code; |
| 464 RemoveTempFile(); | 459 RemoveFile(); |
| 465 core_->delegate_loop_proxy_->PostTask( | 460 core_->delegate_loop_proxy_->PostTask( |
| 466 FROM_HERE, base::Bind(&Core::InformDelegateFetchIsComplete, core_)); | 461 FROM_HERE, base::Bind(&Core::InformDelegateFetchIsComplete, core_)); |
| 467 return; | 462 return; |
| 468 } | 463 } |
| 469 | 464 |
| 470 // If the file was successfully closed, then the URL request is complete. | 465 // If the file was successfully closed, then the URL request is complete. |
| 471 core_->RetryOrCompleteUrlFetch(); | 466 core_->RetryOrCompleteUrlFetch(); |
| 472 } | 467 } |
| 473 | 468 |
| 474 void URLFetcherImpl::Core::TempFileWriter::RemoveTempFile() { | 469 void URLFetcherImpl::Core::FileWriter::RemoveFile() { |
| 475 DCHECK(core_->io_message_loop_proxy_->BelongsToCurrentThread()); | 470 DCHECK(core_->io_message_loop_proxy_->BelongsToCurrentThread()); |
| 476 | 471 |
| 477 // Close the temp file if it is open. | 472 // Close the file if it is open. |
| 478 if (temp_file_handle_ != base::kInvalidPlatformFileValue) { | 473 if (file_handle_ != base::kInvalidPlatformFileValue) { |
| 479 base::FileUtilProxy::Close( | 474 base::FileUtilProxy::Close( |
| 480 file_message_loop_proxy_, temp_file_handle_, | 475 file_message_loop_proxy_, file_handle_, |
| 481 base::FileUtilProxy::StatusCallback()); // No callback: Ignore errors. | 476 base::FileUtilProxy::StatusCallback()); // No callback: Ignore errors. |
| 482 temp_file_handle_ = base::kInvalidPlatformFileValue; | 477 file_handle_ = base::kInvalidPlatformFileValue; |
| 483 } | 478 } |
| 484 | 479 |
| 485 if (!temp_file_.empty()) { | 480 if (!file_path_.empty()) { |
| 486 base::FileUtilProxy::Delete( | 481 base::FileUtilProxy::Delete( |
| 487 file_message_loop_proxy_, temp_file_, | 482 file_message_loop_proxy_, file_path_, |
| 488 false, // No need to recurse, as the path is to a file. | 483 false, // No need to recurse, as the path is to a file. |
| 489 base::FileUtilProxy::StatusCallback()); // No callback: Ignore errors. | 484 base::FileUtilProxy::StatusCallback()); // No callback: Ignore errors. |
| 490 DisownTempFile(); | 485 DisownFile(); |
| 491 } | 486 } |
| 492 } | 487 } |
| 493 | 488 |
| 494 static content::URLFetcherFactory* g_factory = NULL; | 489 static content::URLFetcherFactory* g_factory = NULL; |
| 495 static bool g_interception_enabled = false; | 490 static bool g_interception_enabled = false; |
| 496 | 491 |
| 497 // static | 492 // static |
| 498 content::URLFetcher* content::URLFetcher::Create( | 493 content::URLFetcher* content::URLFetcher::Create( |
| 499 const GURL& url, | 494 const GURL& url, |
| 500 RequestType request_type, | 495 RequestType request_type, |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 581 } | 576 } |
| 582 | 577 |
| 583 void URLFetcherImpl::Core::StartOnIOThread() { | 578 void URLFetcherImpl::Core::StartOnIOThread() { |
| 584 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 579 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 585 | 580 |
| 586 switch (response_destination_) { | 581 switch (response_destination_) { |
| 587 case STRING: | 582 case STRING: |
| 588 StartURLRequestWhenAppropriate(); | 583 StartURLRequestWhenAppropriate(); |
| 589 break; | 584 break; |
| 590 | 585 |
| 591 case TEMP_FILE: | 586 case FILE: |
| 592 DCHECK(file_message_loop_proxy_.get()) | 587 DCHECK(file_message_loop_proxy_.get()) |
| 593 << "Need to set the file message loop proxy."; | 588 << "Need to set the file message loop proxy."; |
| 594 | 589 |
| 595 temp_file_writer_.reset( | 590 file_writer_.reset( |
| 596 new TempFileWriter(this, file_message_loop_proxy_)); | 591 new FileWriter(this, file_message_loop_proxy_)); |
| 597 | 592 |
| 598 // If the temp file is successfully created, | 593 // If the temp file is successfully created, |
| 599 // Core::StartURLRequestWhenAppropriate() will be called. | 594 // Core::StartURLRequestWhenAppropriate() will be called. |
| 600 temp_file_writer_->CreateTempFile(); | 595 file_writer_->CreateTempFile(); |
| 601 break; | 596 break; |
| 602 | 597 |
| 603 default: | 598 default: |
| 604 NOTREACHED(); | 599 NOTREACHED(); |
| 605 } | 600 } |
| 606 } | 601 } |
| 607 | 602 |
| 608 void URLFetcherImpl::Core::Stop() { | 603 void URLFetcherImpl::Core::Stop() { |
| 609 if (delegate_loop_proxy_) { // May be NULL in tests. | 604 if (delegate_loop_proxy_) // May be NULL in tests. |
| 610 DCHECK(delegate_loop_proxy_->BelongsToCurrentThread()); | 605 DCHECK(delegate_loop_proxy_->BelongsToCurrentThread()); |
| 611 } | 606 |
| 612 delegate_ = NULL; | 607 delegate_ = NULL; |
| 613 fetcher_ = NULL; | 608 fetcher_ = NULL; |
| 614 if (io_message_loop_proxy_.get()) { | 609 if (io_message_loop_proxy_.get()) { |
| 615 io_message_loop_proxy_->PostTask( | 610 io_message_loop_proxy_->PostTask( |
| 616 FROM_HERE, base::Bind(&Core::CancelURLRequest, this)); | 611 FROM_HERE, base::Bind(&Core::CancelURLRequest, this)); |
| 617 } | 612 } |
| 618 } | 613 } |
| 619 | 614 |
| 620 void URLFetcherImpl::Core::ReceivedContentWasMalformed() { | 615 void URLFetcherImpl::Core::ReceivedContentWasMalformed() { |
| 621 DCHECK(delegate_loop_proxy_->BelongsToCurrentThread()); | 616 DCHECK(delegate_loop_proxy_->BelongsToCurrentThread()); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 647 const std::string& content, bool is_last_chunk) { | 642 const std::string& content, bool is_last_chunk) { |
| 648 DCHECK(is_chunked_upload_); | 643 DCHECK(is_chunked_upload_); |
| 649 DCHECK(request_.get()); | 644 DCHECK(request_.get()); |
| 650 DCHECK(!content.empty()); | 645 DCHECK(!content.empty()); |
| 651 request_->AppendChunkToUpload(content.data(), | 646 request_->AppendChunkToUpload(content.data(), |
| 652 static_cast<int>(content.length()), | 647 static_cast<int>(content.length()), |
| 653 is_last_chunk); | 648 is_last_chunk); |
| 654 } | 649 } |
| 655 | 650 |
| 656 void URLFetcherImpl::Core::AppendChunkToUpload(const std::string& content, | 651 void URLFetcherImpl::Core::AppendChunkToUpload(const std::string& content, |
| 657 bool is_last_chunk) { | 652 bool is_last_chunk) { |
| 658 DCHECK(delegate_loop_proxy_); | 653 DCHECK(delegate_loop_proxy_); |
| 659 DCHECK(io_message_loop_proxy_.get()); | 654 DCHECK(io_message_loop_proxy_.get()); |
| 660 io_message_loop_proxy_->PostTask( | 655 io_message_loop_proxy_->PostTask( |
| 661 FROM_HERE, | 656 FROM_HERE, |
| 662 base::Bind(&Core::CompleteAddingUploadDataChunk, this, content, | 657 base::Bind(&Core::CompleteAddingUploadDataChunk, this, content, |
| 663 is_last_chunk)); | 658 is_last_chunk)); |
| 664 } | 659 } |
| 665 | 660 |
| 666 // Return true if the write was done and reading may continue. | 661 // Return true if the write was done and reading may continue. |
| 667 // Return false if the write is pending, and the next read will | 662 // Return false if the write is pending, and the next read will |
| 668 // be done later. | 663 // be done later. |
| 669 bool URLFetcherImpl::Core::WriteBuffer(int num_bytes) { | 664 bool URLFetcherImpl::Core::WriteBuffer(int num_bytes) { |
| 670 bool write_complete = false; | 665 bool write_complete = false; |
| 671 switch (response_destination_) { | 666 switch (response_destination_) { |
| 672 case STRING: | 667 case STRING: |
| 673 data_.append(buffer_->data(), num_bytes); | 668 data_.append(buffer_->data(), num_bytes); |
| 674 write_complete = true; | 669 write_complete = true; |
| 675 break; | 670 break; |
| 676 | 671 |
| 677 case TEMP_FILE: | 672 case FILE: |
| 678 temp_file_writer_->WriteBuffer(num_bytes); | 673 file_writer_->WriteBuffer(num_bytes); |
| 679 // WriteBuffer() sends a request the file thread. | 674 // WriteBuffer() sends a request the file thread. |
| 680 // The write is not done yet. | 675 // The write is not done yet. |
| 681 write_complete = false; | 676 write_complete = false; |
| 682 break; | 677 break; |
| 683 | 678 |
| 684 default: | 679 default: |
| 685 NOTREACHED(); | 680 NOTREACHED(); |
| 686 } | 681 } |
| 687 return write_complete; | 682 return write_complete; |
| 688 } | 683 } |
| 689 | 684 |
| 690 void URLFetcherImpl::Core::OnReadCompleted(net::URLRequest* request, | 685 void URLFetcherImpl::Core::OnReadCompleted(net::URLRequest* request, |
| 691 int bytes_read) { | 686 int bytes_read) { |
| 692 DCHECK(request == request_); | 687 DCHECK(request == request_); |
| 693 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 688 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 694 | 689 |
| 695 url_ = request->url(); | 690 url_ = request->url(); |
| 696 url_throttler_entry_ = | 691 url_throttler_entry_ = |
| 697 net::URLRequestThrottlerManager::GetInstance()->RegisterRequestUrl(url_); | 692 net::URLRequestThrottlerManager::GetInstance()->RegisterRequestUrl(url_); |
| 698 | 693 |
| 699 bool waiting_on_write = false; | 694 bool waiting_on_write = false; |
| 700 do { | 695 do { |
| 701 if (!request_->status().is_success() || bytes_read <= 0) | 696 if (!request_->status().is_success() || bytes_read <= 0) |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 716 | 711 |
| 717 if (status.is_success()) | 712 if (status.is_success()) |
| 718 request_->GetResponseCookies(&cookies_); | 713 request_->GetResponseCookies(&cookies_); |
| 719 | 714 |
| 720 // See comments re: HEAD requests in ReadResponse(). | 715 // See comments re: HEAD requests in ReadResponse(). |
| 721 if ((!status.is_io_pending() && !waiting_on_write) || | 716 if ((!status.is_io_pending() && !waiting_on_write) || |
| 722 (request_type_ == HEAD)) { | 717 (request_type_ == HEAD)) { |
| 723 status_ = status; | 718 status_ = status; |
| 724 ReleaseRequest(); | 719 ReleaseRequest(); |
| 725 | 720 |
| 726 // If a temp file is open, close it. | 721 // If a file is open, close it. |
| 727 if (temp_file_writer_.get()) { | 722 if (file_writer_.get()) { |
| 728 // If the file is open, close it. After closing the file, | 723 // If the file is open, close it. After closing the file, |
| 729 // RetryOrCompleteUrlFetch() will be called. | 724 // RetryOrCompleteUrlFetch() will be called. |
| 730 temp_file_writer_->CloseTempFileAndCompleteRequest(); | 725 file_writer_->CloseFileAndCompleteRequest(); |
| 731 } else { | 726 } else { |
| 732 // Otherwise, complete or retry the URL request directly. | 727 // Otherwise, complete or retry the URL request directly. |
| 733 RetryOrCompleteUrlFetch(); | 728 RetryOrCompleteUrlFetch(); |
| 734 } | 729 } |
| 735 } | 730 } |
| 736 } | 731 } |
| 737 | 732 |
| 738 void URLFetcherImpl::Core::RetryOrCompleteUrlFetch() { | 733 void URLFetcherImpl::Core::RetryOrCompleteUrlFetch() { |
| 739 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 734 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 740 base::TimeDelta backoff_delay; | 735 base::TimeDelta backoff_delay; |
| 741 | 736 |
| 742 // Checks the response from server. | 737 // Checks the response from server. |
| 743 if (response_code_ >= 500 || | 738 if (response_code_ >= 500 || |
| 744 status_.error() == net::ERR_TEMPORARILY_THROTTLED) { | 739 status_.error() == net::ERR_TEMPORARILY_THROTTLED) { |
| 745 // When encountering a server error, we will send the request again | 740 // When encountering a server error, we will send the request again |
| 746 // after backoff time. | 741 // after backoff time. |
| 747 ++num_retries_; | 742 ++num_retries_; |
| 748 | 743 |
| 749 // Note that backoff_delay may be 0 because (a) the URLRequestThrottler | 744 // Note that backoff_delay may be 0 because (a) the URLRequestThrottler |
| 750 // code does not necessarily back off on the first error, and (b) it | 745 // code does not necessarily back off on the first error, and (b) it |
| 751 // only backs off on some of the 5xx status codes. | 746 // only backs off on some of the 5xx status codes. |
| 752 base::TimeTicks backoff_release_time = GetBackoffReleaseTime(); | 747 base::TimeTicks backoff_release_time = GetBackoffReleaseTime(); |
| 753 backoff_delay = backoff_release_time - base::TimeTicks::Now(); | 748 backoff_delay = backoff_release_time - base::TimeTicks::Now(); |
| 754 if (backoff_delay < base::TimeDelta()) | 749 if (backoff_delay < base::TimeDelta()) |
| 755 backoff_delay = base::TimeDelta(); | 750 backoff_delay = base::TimeDelta(); |
| 756 | 751 |
| 757 if (automatically_retry_on_5xx_ && | 752 if (automatically_retry_on_5xx_ && num_retries_ <= max_retries_) { |
| 758 num_retries_ <= max_retries_) { | |
| 759 StartOnIOThread(); | 753 StartOnIOThread(); |
| 760 return; | 754 return; |
| 761 } | 755 } |
| 762 } else { | 756 } else { |
| 763 backoff_delay = base::TimeDelta(); | 757 backoff_delay = base::TimeDelta(); |
| 764 } | 758 } |
| 765 request_context_getter_ = NULL; | 759 request_context_getter_ = NULL; |
| 766 bool posted = delegate_loop_proxy_->PostTask( | 760 bool posted = delegate_loop_proxy_->PostTask( |
| 767 FROM_HERE, base::Bind(&Core::OnCompletedURLRequest, this, backoff_delay)); | 761 FROM_HERE, base::Bind(&Core::OnCompletedURLRequest, this, backoff_delay)); |
| 768 | 762 |
| 769 // If the delegate message loop does not exist any more, then the delegate | 763 // If the delegate message loop does not exist any more, then the delegate |
| 770 // should be gone too. | 764 // should be gone too. |
| 771 DCHECK(posted || !delegate_); | 765 DCHECK(posted || !delegate_); |
| 772 } | 766 } |
| 773 | 767 |
| 774 void URLFetcherImpl::Core::ReadResponse() { | 768 void URLFetcherImpl::Core::ReadResponse() { |
| 775 // Some servers may treat HEAD requests as GET requests. To free up the | 769 // Some servers may treat HEAD requests as GET requests. To free up the |
| 776 // network connection as soon as possible, signal that the request has | 770 // network connection as soon as possible, signal that the request has |
| 777 // completed immediately, without trying to read any data back (all we care | 771 // completed immediately, without trying to read any data back (all we care |
| 778 // about is the response code and headers, which we already have). | 772 // about is the response code and headers, which we already have). |
| 779 int bytes_read = 0; | 773 int bytes_read = 0; |
| 780 if (request_->status().is_success() && (request_type_ != HEAD)) | 774 if (request_->status().is_success() && (request_type_ != HEAD)) |
| 781 request_->Read(buffer_, kBufferSize, &bytes_read); | 775 request_->Read(buffer_, kBufferSize, &bytes_read); |
| 782 OnReadCompleted(request_.get(), bytes_read); | 776 OnReadCompleted(request_.get(), bytes_read); |
| 783 } | 777 } |
| 784 | 778 |
| 785 void URLFetcherImpl::Core::DisownTempFile() { | 779 void URLFetcherImpl::Core::DisownFile() { |
| 786 temp_file_writer_->DisownTempFile(); | 780 file_writer_->DisownFile(); |
| 787 } | 781 } |
| 788 | 782 |
| 789 void URLFetcherImpl::Core::StartURLRequest() { | 783 void URLFetcherImpl::Core::StartURLRequest() { |
| 790 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 784 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 791 | 785 |
| 792 if (was_cancelled_) { | 786 if (was_cancelled_) { |
| 793 // Since StartURLRequest() is posted as a *delayed* task, it may | 787 // Since StartURLRequest() is posted as a *delayed* task, it may |
| 794 // run after the URLFetcher was already stopped. | 788 // run after the URLFetcher was already stopped. |
| 795 return; | 789 return; |
| 796 } | 790 } |
| 797 | 791 |
| 798 DCHECK(request_context_getter_); | 792 DCHECK(request_context_getter_); |
| 799 DCHECK(!request_.get()); | 793 DCHECK(!request_.get()); |
| 800 | 794 |
| 801 g_registry.Get().AddURLFetcherCore(this); | 795 g_registry.Get().AddURLFetcherCore(this); |
| 802 current_response_bytes_ = 0; | 796 current_response_bytes_ = 0; |
| 803 request_.reset(new net::URLRequest(original_url_, this)); | 797 request_.reset(new net::URLRequest(original_url_, this)); |
| 804 int flags = request_->load_flags() | load_flags_; | 798 int flags = request_->load_flags() | load_flags_; |
| 805 if (!g_interception_enabled) { | 799 if (!g_interception_enabled) |
| 806 flags = flags | net::LOAD_DISABLE_INTERCEPT; | 800 flags = flags | net::LOAD_DISABLE_INTERCEPT; |
| 807 } | 801 |
| 808 if (is_chunked_upload_) | 802 if (is_chunked_upload_) |
| 809 request_->EnableChunkedUpload(); | 803 request_->EnableChunkedUpload(); |
| 810 request_->set_load_flags(flags); | 804 request_->set_load_flags(flags); |
| 811 request_->set_context(request_context_getter_->GetURLRequestContext()); | 805 request_->set_context(request_context_getter_->GetURLRequestContext()); |
| 812 request_->set_referrer(referrer_); | 806 request_->set_referrer(referrer_); |
| 813 | 807 |
| 814 switch (request_type_) { | 808 switch (request_type_) { |
| 815 case GET: | 809 case GET: |
| 816 break; | 810 break; |
| 817 | 811 |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 842 } | 836 } |
| 843 | 837 |
| 844 if (!extra_request_headers_.IsEmpty()) | 838 if (!extra_request_headers_.IsEmpty()) |
| 845 request_->SetExtraRequestHeaders(extra_request_headers_); | 839 request_->SetExtraRequestHeaders(extra_request_headers_); |
| 846 | 840 |
| 847 // There might be data left over from a previous request attempt. | 841 // There might be data left over from a previous request attempt. |
| 848 data_.clear(); | 842 data_.clear(); |
| 849 | 843 |
| 850 // If we are writing the response to a file, the only caller | 844 // If we are writing the response to a file, the only caller |
| 851 // of this function should have created it and not written yet. | 845 // of this function should have created it and not written yet. |
| 852 DCHECK(!temp_file_writer_.get() || | 846 DCHECK(!file_writer_.get() || file_writer_->total_bytes_written() == 0); |
| 853 temp_file_writer_->total_bytes_written() == 0); | |
| 854 | 847 |
| 855 request_->Start(); | 848 request_->Start(); |
| 856 } | 849 } |
| 857 | 850 |
| 858 void URLFetcherImpl::Core::StartURLRequestWhenAppropriate() { | 851 void URLFetcherImpl::Core::StartURLRequestWhenAppropriate() { |
| 859 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 852 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 860 | 853 |
| 861 if (was_cancelled_) | 854 if (was_cancelled_) |
| 862 return; | 855 return; |
| 863 | 856 |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 884 if (request_.get()) { | 877 if (request_.get()) { |
| 885 request_->Cancel(); | 878 request_->Cancel(); |
| 886 ReleaseRequest(); | 879 ReleaseRequest(); |
| 887 } | 880 } |
| 888 // Release the reference to the request context. There could be multiple | 881 // Release the reference to the request context. There could be multiple |
| 889 // references to URLFetcher::Core at this point so it may take a while to | 882 // references to URLFetcher::Core at this point so it may take a while to |
| 890 // delete the object, but we cannot delay the destruction of the request | 883 // delete the object, but we cannot delay the destruction of the request |
| 891 // context. | 884 // context. |
| 892 request_context_getter_ = NULL; | 885 request_context_getter_ = NULL; |
| 893 was_cancelled_ = true; | 886 was_cancelled_ = true; |
| 894 temp_file_writer_.reset(); | 887 file_writer_.reset(); |
| 895 } | 888 } |
| 896 | 889 |
| 897 void URLFetcherImpl::Core::OnCompletedURLRequest( | 890 void URLFetcherImpl::Core::OnCompletedURLRequest( |
| 898 base::TimeDelta backoff_delay) { | 891 base::TimeDelta backoff_delay) { |
| 899 DCHECK(delegate_loop_proxy_->BelongsToCurrentThread()); | 892 DCHECK(delegate_loop_proxy_->BelongsToCurrentThread()); |
| 900 | 893 |
| 901 // Save the status and backoff_delay so that delegates can read it. | 894 // Save the status and backoff_delay so that delegates can read it. |
| 902 if (delegate_) { | 895 if (delegate_) { |
| 903 backoff_delay_ = backoff_delay; | 896 backoff_delay_ = backoff_delay; |
| 904 InformDelegateFetchIsComplete(); | 897 InformDelegateFetchIsComplete(); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 915 | 908 |
| 916 void URLFetcherImpl::Core::InformDelegateDownloadProgressInDelegateThread( | 909 void URLFetcherImpl::Core::InformDelegateDownloadProgressInDelegateThread( |
| 917 int64 current, int64 total) { | 910 int64 current, int64 total) { |
| 918 DCHECK(delegate_loop_proxy_->BelongsToCurrentThread()); | 911 DCHECK(delegate_loop_proxy_->BelongsToCurrentThread()); |
| 919 if (delegate_) | 912 if (delegate_) |
| 920 delegate_->OnURLFetchDownloadProgress(fetcher_, current, total); | 913 delegate_->OnURLFetchDownloadProgress(fetcher_, current, total); |
| 921 } | 914 } |
| 922 | 915 |
| 923 void URLFetcherImpl::Core::InformDelegateFetchIsComplete() { | 916 void URLFetcherImpl::Core::InformDelegateFetchIsComplete() { |
| 924 DCHECK(delegate_loop_proxy_->BelongsToCurrentThread()); | 917 DCHECK(delegate_loop_proxy_->BelongsToCurrentThread()); |
| 925 if (delegate_) { | 918 if (delegate_) |
| 926 delegate_->OnURLFetchComplete(fetcher_); | 919 delegate_->OnURLFetchComplete(fetcher_); |
| 927 } | |
| 928 } | 920 } |
| 929 | 921 |
| 930 void URLFetcherImpl::Core::NotifyMalformedContent() { | 922 void URLFetcherImpl::Core::NotifyMalformedContent() { |
| 931 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 923 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 932 if (url_throttler_entry_ != NULL) { | 924 if (url_throttler_entry_ != NULL) { |
| 933 int status_code = response_code_; | 925 int status_code = response_code_; |
| 934 if (status_code == RESPONSE_CODE_INVALID) { | 926 if (status_code == RESPONSE_CODE_INVALID) { |
| 935 // The status code will generally be known by the time clients | 927 // The status code will generally be known by the time clients |
| 936 // call the |ReceivedContentWasMalformed()| function (which ends up | 928 // call the |ReceivedContentWasMalformed()| function (which ends up |
| 937 // calling the current function) but if it's not, we need to assume | 929 // calling the current function) but if it's not, we need to assume |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1036 } | 1028 } |
| 1037 | 1029 |
| 1038 | 1030 |
| 1039 base::TimeDelta URLFetcherImpl::GetBackoffDelay() const { | 1031 base::TimeDelta URLFetcherImpl::GetBackoffDelay() const { |
| 1040 return core_->backoff_delay_; | 1032 return core_->backoff_delay_; |
| 1041 } | 1033 } |
| 1042 | 1034 |
| 1043 void URLFetcherImpl::SaveResponseToTemporaryFile( | 1035 void URLFetcherImpl::SaveResponseToTemporaryFile( |
| 1044 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) { | 1036 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) { |
| 1045 core_->file_message_loop_proxy_ = file_message_loop_proxy; | 1037 core_->file_message_loop_proxy_ = file_message_loop_proxy; |
| 1046 core_->response_destination_ = TEMP_FILE; | 1038 core_->response_destination_ = FILE; |
| 1047 } | 1039 } |
| 1048 | 1040 |
| 1049 net::HttpResponseHeaders* URLFetcherImpl::GetResponseHeaders() const { | 1041 net::HttpResponseHeaders* URLFetcherImpl::GetResponseHeaders() const { |
| 1050 return core_->response_headers_; | 1042 return core_->response_headers_; |
| 1051 } | 1043 } |
| 1052 | 1044 |
| 1053 void URLFetcherImpl::set_response_headers( | 1045 void URLFetcherImpl::set_response_headers( |
| 1054 scoped_refptr<net::HttpResponseHeaders> headers) { | 1046 scoped_refptr<net::HttpResponseHeaders> headers) { |
| 1055 core_->response_headers_ = headers; | 1047 core_->response_headers_ = headers; |
| 1056 } | 1048 } |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1097 } | 1089 } |
| 1098 | 1090 |
| 1099 const net::ResponseCookies& URLFetcherImpl::GetCookies() const { | 1091 const net::ResponseCookies& URLFetcherImpl::GetCookies() const { |
| 1100 return core_->cookies_; | 1092 return core_->cookies_; |
| 1101 } | 1093 } |
| 1102 | 1094 |
| 1103 bool URLFetcherImpl::FileErrorOccurred( | 1095 bool URLFetcherImpl::FileErrorOccurred( |
| 1104 base::PlatformFileError* out_error_code) const { | 1096 base::PlatformFileError* out_error_code) const { |
| 1105 | 1097 |
| 1106 // Can't have a file error if no file is being created or written to. | 1098 // Can't have a file error if no file is being created or written to. |
| 1107 if (!core_->temp_file_writer_.get()) { | 1099 if (!core_->file_writer_.get()) |
| 1108 return false; | 1100 return false; |
| 1109 } | |
| 1110 | 1101 |
| 1111 base::PlatformFileError error_code = core_->temp_file_writer_->error_code(); | 1102 base::PlatformFileError error_code = core_->file_writer_->error_code(); |
| 1112 if (error_code == base::PLATFORM_FILE_OK) | 1103 if (error_code == base::PLATFORM_FILE_OK) |
| 1113 return false; | 1104 return false; |
| 1114 | 1105 |
| 1115 *out_error_code = error_code; | 1106 *out_error_code = error_code; |
| 1116 return true; | 1107 return true; |
| 1117 } | 1108 } |
| 1118 | 1109 |
| 1119 void URLFetcherImpl::ReceivedContentWasMalformed() { | 1110 void URLFetcherImpl::ReceivedContentWasMalformed() { |
| 1120 core_->ReceivedContentWasMalformed(); | 1111 core_->ReceivedContentWasMalformed(); |
| 1121 } | 1112 } |
| 1122 | 1113 |
| 1123 bool URLFetcherImpl::GetResponseAsString( | 1114 bool URLFetcherImpl::GetResponseAsString( |
| 1124 std::string* out_response_string) const { | 1115 std::string* out_response_string) const { |
| 1125 if (core_->response_destination_ != STRING) | 1116 if (core_->response_destination_ != STRING) |
| 1126 return false; | 1117 return false; |
| 1127 | 1118 |
| 1128 *out_response_string = core_->data_; | 1119 *out_response_string = core_->data_; |
| 1129 UMA_HISTOGRAM_MEMORY_KB("UrlFetcher.StringResponseSize", | 1120 UMA_HISTOGRAM_MEMORY_KB("UrlFetcher.StringResponseSize", |
| 1130 (core_->data_.length() / 1024)); | 1121 (core_->data_.length() / 1024)); |
| 1131 | 1122 |
| 1132 return true; | 1123 return true; |
| 1133 } | 1124 } |
| 1134 | 1125 |
| 1135 bool URLFetcherImpl::GetResponseAsFilePath(bool take_ownership, | 1126 bool URLFetcherImpl::GetResponseAsFilePath(bool take_ownership, |
| 1136 FilePath* out_response_path) const { | 1127 FilePath* out_response_path) const { |
| 1137 DCHECK(core_->delegate_loop_proxy_->BelongsToCurrentThread()); | 1128 DCHECK(core_->delegate_loop_proxy_->BelongsToCurrentThread()); |
| 1138 if (core_->response_destination_ != TEMP_FILE || | 1129 if (core_->response_destination_ != FILE || !core_->file_writer_.get()) |
| 1139 !core_->temp_file_writer_.get()) | |
| 1140 return false; | 1130 return false; |
| 1141 | 1131 |
| 1142 *out_response_path = core_->temp_file_writer_->temp_file(); | 1132 *out_response_path = core_->file_writer_->file_path(); |
| 1143 | 1133 |
| 1144 if (take_ownership) { | 1134 if (take_ownership) { |
| 1145 core_->io_message_loop_proxy_->PostTask( | 1135 core_->io_message_loop_proxy_->PostTask( |
| 1146 FROM_HERE, base::Bind(&Core::DisownTempFile, core_.get())); | 1136 FROM_HERE, base::Bind(&Core::DisownFile, core_.get())); |
| 1147 } | 1137 } |
| 1148 return true; | 1138 return true; |
| 1149 } | 1139 } |
| 1150 | 1140 |
| 1151 // static | 1141 // static |
| 1152 void URLFetcherImpl::CancelAll() { | 1142 void URLFetcherImpl::CancelAll() { |
| 1153 Core::CancelAll(); | 1143 Core::CancelAll(); |
| 1154 } | 1144 } |
| 1155 | 1145 |
| 1156 // static | 1146 // static |
| 1157 int URLFetcherImpl::GetNumFetcherCores() { | 1147 int URLFetcherImpl::GetNumFetcherCores() { |
| 1158 return Core::g_registry.Get().size(); | 1148 return Core::g_registry.Get().size(); |
| 1159 } | 1149 } |
| 1160 | 1150 |
| 1161 content::URLFetcherDelegate* URLFetcherImpl::delegate() const { | 1151 content::URLFetcherDelegate* URLFetcherImpl::delegate() const { |
| 1162 return core_->delegate(); | 1152 return core_->delegate(); |
| 1163 } | 1153 } |
| 1164 | 1154 |
| 1165 // static | 1155 // static |
| 1166 content::URLFetcherFactory* URLFetcherImpl::factory() { | 1156 content::URLFetcherFactory* URLFetcherImpl::factory() { |
| 1167 return g_factory; | 1157 return g_factory; |
| 1168 } | 1158 } |
| 1169 | 1159 |
| 1170 // static | 1160 // static |
| 1171 void URLFetcherImpl::set_factory(content::URLFetcherFactory* factory) { | 1161 void URLFetcherImpl::set_factory(content::URLFetcherFactory* factory) { |
| 1172 g_factory = factory; | 1162 g_factory = factory; |
| 1173 } | 1163 } |
| OLD | NEW |