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 "content/common/net/url_fetcher.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" |
11 #include "base/file_path.h" | 11 #include "base/file_path.h" |
12 #include "base/file_util_proxy.h" | 12 #include "base/file_util_proxy.h" |
13 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
(...skipping 11 matching lines...) Expand all Loading... |
27 #include "net/base/net_errors.h" | 27 #include "net/base/net_errors.h" |
28 #include "net/http/http_request_headers.h" | 28 #include "net/http/http_request_headers.h" |
29 #include "net/http/http_response_headers.h" | 29 #include "net/http/http_response_headers.h" |
30 #include "net/url_request/url_request.h" | 30 #include "net/url_request/url_request.h" |
31 #include "net/url_request/url_request_context.h" | 31 #include "net/url_request/url_request_context.h" |
32 #include "net/url_request/url_request_context_getter.h" | 32 #include "net/url_request/url_request_context_getter.h" |
33 #include "net/url_request/url_request_throttler_manager.h" | 33 #include "net/url_request/url_request_throttler_manager.h" |
34 | 34 |
35 static const int kBufferSize = 4096; | 35 static const int kBufferSize = 4096; |
36 | 36 |
37 class URLFetcher::Core | 37 class URLFetcherImpl::Core |
38 : public base::RefCountedThreadSafe<URLFetcher::Core>, | 38 : public base::RefCountedThreadSafe<URLFetcherImpl::Core>, |
39 public net::URLRequest::Delegate { | 39 public net::URLRequest::Delegate { |
40 public: | 40 public: |
41 // For POST requests, set |content_type| to the MIME type of the content | 41 // For POST requests, set |content_type| to the MIME type of the content |
42 // and set |content| to the data to upload. |flags| are flags to apply to | 42 // and set |content| to the data to upload. |flags| are flags to apply to |
43 // the load operation--these should be one or more of the LOAD_* flags | 43 // the load operation--these should be one or more of the LOAD_* flags |
44 // defined in net/base/load_flags.h. | 44 // defined in net/base/load_flags.h. |
45 Core(URLFetcher* fetcher, | 45 Core(URLFetcherImpl* fetcher, |
46 const GURL& original_url, | 46 const GURL& original_url, |
47 RequestType request_type, | 47 RequestType request_type, |
48 content::URLFetcherDelegate* d); | 48 content::URLFetcherDelegate* d); |
49 | 49 |
50 // Starts the load. It's important that this not happen in the constructor | 50 // Starts the load. It's important that this not happen in the constructor |
51 // because it causes the IO thread to begin AddRef()ing and Release()ing | 51 // because it causes the IO thread to begin AddRef()ing and Release()ing |
52 // us. If our caller hasn't had time to fully construct us and take a | 52 // us. If our caller hasn't had time to fully construct us and take a |
53 // reference, the IO thread could interrupt things, run a task, Release() | 53 // reference, the IO thread could interrupt things, run a task, Release() |
54 // us, and destroy us, leaving the caller with an already-destroyed object | 54 // us, and destroy us, leaving the caller with an already-destroyed object |
55 // when construction finishes. | 55 // when construction finishes. |
(...skipping 12 matching lines...) Expand all Loading... |
68 void ReceivedContentWasMalformed(); | 68 void ReceivedContentWasMalformed(); |
69 | 69 |
70 // Overridden from net::URLRequest::Delegate: | 70 // Overridden from net::URLRequest::Delegate: |
71 virtual void OnResponseStarted(net::URLRequest* request); | 71 virtual void OnResponseStarted(net::URLRequest* request); |
72 virtual void OnReadCompleted(net::URLRequest* request, int bytes_read); | 72 virtual void OnReadCompleted(net::URLRequest* request, int bytes_read); |
73 | 73 |
74 content::URLFetcherDelegate* delegate() const { return delegate_; } | 74 content::URLFetcherDelegate* delegate() const { return delegate_; } |
75 static void CancelAll(); | 75 static void CancelAll(); |
76 | 76 |
77 private: | 77 private: |
78 friend class base::RefCountedThreadSafe<URLFetcher::Core>; | 78 friend class base::RefCountedThreadSafe<URLFetcherImpl::Core>; |
79 | 79 |
80 class Registry { | 80 class Registry { |
81 public: | 81 public: |
82 Registry(); | 82 Registry(); |
83 ~Registry(); | 83 ~Registry(); |
84 | 84 |
85 void AddURLFetcherCore(Core* core); | 85 void AddURLFetcherCore(Core* core); |
86 void RemoveURLFetcherCore(Core* core); | 86 void RemoveURLFetcherCore(Core* core); |
87 | 87 |
88 void CancelAll(); | 88 void CancelAll(); |
(...skipping 10 matching lines...) Expand all Loading... |
99 | 99 |
100 // Class TempFileWriter encapsulates all state involved in writing | 100 // Class TempFileWriter encapsulates all state involved in writing |
101 // response bytes to a temporary file. It is only used if | 101 // response bytes to a temporary file. It is only used if |
102 // |Core::response_destination_| == TEMP_FILE. Each instance of | 102 // |Core::response_destination_| == TEMP_FILE. Each instance of |
103 // TempFileWriter is owned by a URLFetcher::Core, which manages | 103 // TempFileWriter is owned by a URLFetcher::Core, which manages |
104 // its lifetime and never transfers ownership. While writing to | 104 // its lifetime and never transfers ownership. While writing to |
105 // a file, all function calls happen on the IO thread. | 105 // a file, all function calls happen on the IO thread. |
106 class TempFileWriter { | 106 class TempFileWriter { |
107 public: | 107 public: |
108 TempFileWriter( | 108 TempFileWriter( |
109 URLFetcher::Core* core, | 109 URLFetcherImpl::Core* core, |
110 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy); | 110 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy); |
111 | 111 |
112 ~TempFileWriter(); | 112 ~TempFileWriter(); |
113 void CreateTempFile(); | 113 void CreateTempFile(); |
114 void DidCreateTempFile(base::PlatformFileError error_code, | 114 void DidCreateTempFile(base::PlatformFileError error_code, |
115 base::PassPlatformFile file_handle, | 115 base::PassPlatformFile file_handle, |
116 FilePath file_path); | 116 FilePath file_path); |
117 | 117 |
118 // Record |num_bytes_| response bytes in |core_->buffer_| to the file. | 118 // Record |num_bytes_| response bytes in |core_->buffer_| to the file. |
119 void WriteBuffer(int num_bytes); | 119 void WriteBuffer(int num_bytes); |
(...skipping 14 matching lines...) Expand all Loading... |
134 void RemoveTempFile(); | 134 void RemoveTempFile(); |
135 | 135 |
136 const FilePath& temp_file() const { return temp_file_; } | 136 const FilePath& temp_file() const { return temp_file_; } |
137 int64 total_bytes_written() { return total_bytes_written_; } | 137 int64 total_bytes_written() { return total_bytes_written_; } |
138 base::PlatformFileError error_code() const { return error_code_; } | 138 base::PlatformFileError error_code() const { return error_code_; } |
139 | 139 |
140 private: | 140 private: |
141 // Callback which gets the result of closing the temp file. | 141 // Callback which gets the result of closing the temp file. |
142 void DidCloseTempFile(base::PlatformFileError error); | 142 void DidCloseTempFile(base::PlatformFileError error); |
143 | 143 |
144 // The URLFetcher::Core which instantiated this class. | 144 // The URLFetcherImpl::Core which instantiated this class. |
145 URLFetcher::Core* core_; | 145 URLFetcherImpl::Core* core_; |
146 | 146 |
147 // The last error encountered on a file operation. base::PLATFORM_FILE_OK | 147 // The last error encountered on a file operation. base::PLATFORM_FILE_OK |
148 // if no error occurred. | 148 // if no error occurred. |
149 base::PlatformFileError error_code_; | 149 base::PlatformFileError error_code_; |
150 | 150 |
151 // Callbacks are created for use with base::FileUtilProxy. | 151 // Callbacks are created for use with base::FileUtilProxy. |
152 base::WeakPtrFactory<URLFetcher::Core::TempFileWriter> weak_factory_; | 152 base::WeakPtrFactory<URLFetcherImpl::Core::TempFileWriter> weak_factory_; |
153 | 153 |
154 // Message loop on which file operations should happen. | 154 // Message loop on which file operations should happen. |
155 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy_; | 155 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy_; |
156 | 156 |
157 // Path to the temporary file. This path is empty when there | 157 // Path to the temporary file. This path is empty when there |
158 // is no temp file. | 158 // is no temp file. |
159 FilePath temp_file_; | 159 FilePath temp_file_; |
160 | 160 |
161 // Handle to the temp file. | 161 // Handle to the temp file. |
162 base::PlatformFile temp_file_handle_; | 162 base::PlatformFile temp_file_handle_; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 // returns false, it will post a task that will read more bytes once the | 209 // returns false, it will post a task that will read more bytes once the |
210 // write is complete. | 210 // write is complete. |
211 bool WriteBuffer(int num_bytes); | 211 bool WriteBuffer(int num_bytes); |
212 | 212 |
213 // Read response bytes from the request. | 213 // Read response bytes from the request. |
214 void ReadResponse(); | 214 void ReadResponse(); |
215 | 215 |
216 // Drop ownership of any temp file managed by |temp_file_|. | 216 // Drop ownership of any temp file managed by |temp_file_|. |
217 void DisownTempFile(); | 217 void DisownTempFile(); |
218 | 218 |
219 URLFetcher* fetcher_; // Corresponding fetcher object | 219 URLFetcherImpl* fetcher_; // Corresponding fetcher object |
220 GURL original_url_; // The URL we were asked to fetch | 220 GURL original_url_; // The URL we were asked to fetch |
221 GURL url_; // The URL we eventually wound up at | 221 GURL url_; // The URL we eventually wound up at |
222 RequestType request_type_; // What type of request is this? | 222 RequestType request_type_; // What type of request is this? |
223 net::URLRequestStatus status_; // Status of the request | 223 net::URLRequestStatus status_; // Status of the request |
224 content::URLFetcherDelegate* delegate_; // Object to notify on completion | 224 content::URLFetcherDelegate* delegate_; // Object to notify on completion |
225 scoped_refptr<base::MessageLoopProxy> delegate_loop_proxy_; | 225 scoped_refptr<base::MessageLoopProxy> delegate_loop_proxy_; |
226 // Message loop proxy of the creating | 226 // Message loop proxy of the creating |
227 // thread. | 227 // thread. |
228 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; | 228 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; |
229 // The message loop proxy for the thread | 229 // The message loop proxy for the thread |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 // re-execute the request, after the back-off delay has expired. | 284 // re-execute the request, after the back-off delay has expired. |
285 // true by default. | 285 // true by default. |
286 bool automatically_retry_on_5xx_; | 286 bool automatically_retry_on_5xx_; |
287 // Maximum retries allowed. | 287 // Maximum retries allowed. |
288 int max_retries_; | 288 int max_retries_; |
289 // Back-off time delay. 0 by default. | 289 // Back-off time delay. 0 by default. |
290 base::TimeDelta backoff_delay_; | 290 base::TimeDelta backoff_delay_; |
291 | 291 |
292 static base::LazyInstance<Registry> g_registry; | 292 static base::LazyInstance<Registry> g_registry; |
293 | 293 |
294 friend class URLFetcher; | 294 friend class URLFetcherImpl; |
295 DISALLOW_COPY_AND_ASSIGN(Core); | 295 DISALLOW_COPY_AND_ASSIGN(Core); |
296 }; | 296 }; |
297 | 297 |
298 URLFetcher::Core::Registry::Registry() {} | 298 URLFetcherImpl::Core::Registry::Registry() {} |
299 URLFetcher::Core::Registry::~Registry() {} | 299 URLFetcherImpl::Core::Registry::~Registry() {} |
300 | 300 |
301 void URLFetcher::Core::Registry::AddURLFetcherCore(Core* core) { | 301 void URLFetcherImpl::Core::Registry::AddURLFetcherCore(Core* core) { |
302 DCHECK(!ContainsKey(fetchers_, core)); | 302 DCHECK(!ContainsKey(fetchers_, core)); |
303 fetchers_.insert(core); | 303 fetchers_.insert(core); |
304 } | 304 } |
305 | 305 |
306 void URLFetcher::Core::Registry::RemoveURLFetcherCore(Core* core) { | 306 void URLFetcherImpl::Core::Registry::RemoveURLFetcherCore(Core* core) { |
307 DCHECK(ContainsKey(fetchers_, core)); | 307 DCHECK(ContainsKey(fetchers_, core)); |
308 fetchers_.erase(core); | 308 fetchers_.erase(core); |
309 } | 309 } |
310 | 310 |
311 void URLFetcher::Core::Registry::CancelAll() { | 311 void URLFetcherImpl::Core::Registry::CancelAll() { |
312 while (!fetchers_.empty()) | 312 while (!fetchers_.empty()) |
313 (*fetchers_.begin())->CancelURLRequest(); | 313 (*fetchers_.begin())->CancelURLRequest(); |
314 } | 314 } |
315 | 315 |
316 // static | 316 // static |
317 base::LazyInstance<URLFetcher::Core::Registry> | 317 base::LazyInstance<URLFetcherImpl::Core::Registry> |
318 URLFetcher::Core::g_registry(base::LINKER_INITIALIZED); | 318 URLFetcherImpl::Core::g_registry(base::LINKER_INITIALIZED); |
319 | 319 |
320 URLFetcher::Core::TempFileWriter::TempFileWriter( | 320 URLFetcherImpl::Core::TempFileWriter::TempFileWriter( |
321 URLFetcher::Core* core, | 321 URLFetcherImpl::Core* core, |
322 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) | 322 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) |
323 : core_(core), | 323 : core_(core), |
324 error_code_(base::PLATFORM_FILE_OK), | 324 error_code_(base::PLATFORM_FILE_OK), |
325 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), | 325 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), |
326 file_message_loop_proxy_(file_message_loop_proxy), | 326 file_message_loop_proxy_(file_message_loop_proxy), |
327 temp_file_handle_(base::kInvalidPlatformFileValue) { | 327 temp_file_handle_(base::kInvalidPlatformFileValue) { |
328 } | 328 } |
329 | 329 |
330 URLFetcher::Core::TempFileWriter::~TempFileWriter() { | 330 URLFetcherImpl::Core::TempFileWriter::~TempFileWriter() { |
331 RemoveTempFile(); | 331 RemoveTempFile(); |
332 } | 332 } |
333 | 333 |
334 void URLFetcher::Core::TempFileWriter::CreateTempFile() { | 334 void URLFetcherImpl::Core::TempFileWriter::CreateTempFile() { |
335 DCHECK(core_->io_message_loop_proxy_->BelongsToCurrentThread()); | 335 DCHECK(core_->io_message_loop_proxy_->BelongsToCurrentThread()); |
336 CHECK(file_message_loop_proxy_.get()); | 336 CHECK(file_message_loop_proxy_.get()); |
337 base::FileUtilProxy::CreateTemporary( | 337 base::FileUtilProxy::CreateTemporary( |
338 file_message_loop_proxy_, | 338 file_message_loop_proxy_, |
339 0, // No additional file flags. | 339 0, // No additional file flags. |
340 base::Bind(&URLFetcher::Core::TempFileWriter::DidCreateTempFile, | 340 base::Bind(&URLFetcherImpl::Core::TempFileWriter::DidCreateTempFile, |
341 weak_factory_.GetWeakPtr())); | 341 weak_factory_.GetWeakPtr())); |
342 } | 342 } |
343 | 343 |
344 void URLFetcher::Core::TempFileWriter::DidCreateTempFile( | 344 void URLFetcherImpl::Core::TempFileWriter::DidCreateTempFile( |
345 base::PlatformFileError error_code, | 345 base::PlatformFileError error_code, |
346 base::PassPlatformFile file_handle, | 346 base::PassPlatformFile file_handle, |
347 FilePath file_path) { | 347 FilePath file_path) { |
348 DCHECK(core_->io_message_loop_proxy_->BelongsToCurrentThread()); | 348 DCHECK(core_->io_message_loop_proxy_->BelongsToCurrentThread()); |
349 | 349 |
350 if (base::PLATFORM_FILE_OK != error_code) { | 350 if (base::PLATFORM_FILE_OK != error_code) { |
351 error_code_ = error_code; | 351 error_code_ = error_code; |
352 RemoveTempFile(); | 352 RemoveTempFile(); |
353 core_->delegate_loop_proxy_->PostTask( | 353 core_->delegate_loop_proxy_->PostTask( |
354 FROM_HERE, base::Bind(&Core::InformDelegateFetchIsComplete, core_)); | 354 FROM_HERE, base::Bind(&Core::InformDelegateFetchIsComplete, core_)); |
355 return; | 355 return; |
356 } | 356 } |
357 | 357 |
358 temp_file_ = file_path; | 358 temp_file_ = file_path; |
359 temp_file_handle_ = file_handle.ReleaseValue(); | 359 temp_file_handle_ = file_handle.ReleaseValue(); |
360 total_bytes_written_ = 0; | 360 total_bytes_written_ = 0; |
361 | 361 |
362 core_->io_message_loop_proxy_->PostTask( | 362 core_->io_message_loop_proxy_->PostTask( |
363 FROM_HERE, base::Bind(&Core::StartURLRequestWhenAppropriate, core_)); | 363 FROM_HERE, base::Bind(&Core::StartURLRequestWhenAppropriate, core_)); |
364 } | 364 } |
365 | 365 |
366 void URLFetcher::Core::TempFileWriter::WriteBuffer(int num_bytes) { | 366 void URLFetcherImpl::Core::TempFileWriter::WriteBuffer(int num_bytes) { |
367 DCHECK(core_->io_message_loop_proxy_->BelongsToCurrentThread()); | 367 DCHECK(core_->io_message_loop_proxy_->BelongsToCurrentThread()); |
368 | 368 |
369 // Start writing to the temp file by setting the initial state | 369 // Start writing to the temp file by setting the initial state |
370 // of |pending_bytes_| and |buffer_offset_| to indicate that the | 370 // of |pending_bytes_| and |buffer_offset_| to indicate that the |
371 // entire buffer has not yet been written. | 371 // entire buffer has not yet been written. |
372 pending_bytes_ = num_bytes; | 372 pending_bytes_ = num_bytes; |
373 buffer_offset_ = 0; | 373 buffer_offset_ = 0; |
374 ContinueWrite(base::PLATFORM_FILE_OK, 0); | 374 ContinueWrite(base::PLATFORM_FILE_OK, 0); |
375 } | 375 } |
376 | 376 |
377 void URLFetcher::Core::TempFileWriter::ContinueWrite( | 377 void URLFetcherImpl::Core::TempFileWriter::ContinueWrite( |
378 base::PlatformFileError error_code, | 378 base::PlatformFileError error_code, |
379 int bytes_written) { | 379 int bytes_written) { |
380 DCHECK(core_->io_message_loop_proxy_->BelongsToCurrentThread()); | 380 DCHECK(core_->io_message_loop_proxy_->BelongsToCurrentThread()); |
381 | 381 |
382 if (base::PLATFORM_FILE_OK != error_code) { | 382 if (base::PLATFORM_FILE_OK != error_code) { |
383 error_code_ = error_code; | 383 error_code_ = error_code; |
384 RemoveTempFile(); | 384 RemoveTempFile(); |
385 core_->delegate_loop_proxy_->PostTask( | 385 core_->delegate_loop_proxy_->PostTask( |
386 FROM_HERE, base::Bind(&Core::InformDelegateFetchIsComplete, core_)); | 386 FROM_HERE, base::Bind(&Core::InformDelegateFetchIsComplete, core_)); |
387 return; | 387 return; |
388 } | 388 } |
389 | 389 |
390 total_bytes_written_ += bytes_written; | 390 total_bytes_written_ += bytes_written; |
391 buffer_offset_ += bytes_written; | 391 buffer_offset_ += bytes_written; |
392 pending_bytes_ -= bytes_written; | 392 pending_bytes_ -= bytes_written; |
393 | 393 |
394 if (pending_bytes_ > 0) { | 394 if (pending_bytes_ > 0) { |
395 base::FileUtilProxy::Write( | 395 base::FileUtilProxy::Write( |
396 file_message_loop_proxy_, temp_file_handle_, | 396 file_message_loop_proxy_, temp_file_handle_, |
397 total_bytes_written_, // Append to the end | 397 total_bytes_written_, // Append to the end |
398 (core_->buffer_->data() + buffer_offset_), pending_bytes_, | 398 (core_->buffer_->data() + buffer_offset_), pending_bytes_, |
399 base::Bind(&URLFetcher::Core::TempFileWriter::ContinueWrite, | 399 base::Bind(&URLFetcherImpl::Core::TempFileWriter::ContinueWrite, |
400 weak_factory_.GetWeakPtr())); | 400 weak_factory_.GetWeakPtr())); |
401 } else { | 401 } else { |
402 // Finished writing core_->buffer_ to the file. Read some more. | 402 // Finished writing core_->buffer_ to the file. Read some more. |
403 core_->ReadResponse(); | 403 core_->ReadResponse(); |
404 } | 404 } |
405 } | 405 } |
406 | 406 |
407 void URLFetcher::Core::TempFileWriter::DisownTempFile() { | 407 void URLFetcherImpl::Core::TempFileWriter::DisownTempFile() { |
408 DCHECK(core_->io_message_loop_proxy_->BelongsToCurrentThread()); | 408 DCHECK(core_->io_message_loop_proxy_->BelongsToCurrentThread()); |
409 | 409 |
410 // Disowning is done by the delegate's OnURLFetchComplete method. | 410 // Disowning is done by the delegate's OnURLFetchComplete method. |
411 // The temp file should be closed by the time that method is called. | 411 // The temp file should be closed by the time that method is called. |
412 DCHECK(temp_file_handle_ == base::kInvalidPlatformFileValue); | 412 DCHECK(temp_file_handle_ == base::kInvalidPlatformFileValue); |
413 | 413 |
414 // Forget about any temp file by reseting the path. | 414 // Forget about any temp file by reseting the path. |
415 temp_file_ = FilePath(); | 415 temp_file_ = FilePath(); |
416 } | 416 } |
417 | 417 |
418 void URLFetcher::Core::TempFileWriter::CloseTempFileAndCompleteRequest() { | 418 void URLFetcherImpl::Core::TempFileWriter::CloseTempFileAndCompleteRequest() { |
419 DCHECK(core_->io_message_loop_proxy_->BelongsToCurrentThread()); | 419 DCHECK(core_->io_message_loop_proxy_->BelongsToCurrentThread()); |
420 | 420 |
421 if (temp_file_handle_ != base::kInvalidPlatformFileValue) { | 421 if (temp_file_handle_ != base::kInvalidPlatformFileValue) { |
422 base::FileUtilProxy::Close( | 422 base::FileUtilProxy::Close( |
423 file_message_loop_proxy_, temp_file_handle_, | 423 file_message_loop_proxy_, temp_file_handle_, |
424 base::Bind(&URLFetcher::Core::TempFileWriter::DidCloseTempFile, | 424 base::Bind(&URLFetcherImpl::Core::TempFileWriter::DidCloseTempFile, |
425 weak_factory_.GetWeakPtr())); | 425 weak_factory_.GetWeakPtr())); |
426 temp_file_handle_ = base::kInvalidPlatformFileValue; | 426 temp_file_handle_ = base::kInvalidPlatformFileValue; |
427 } | 427 } |
428 } | 428 } |
429 | 429 |
430 void URLFetcher::Core::TempFileWriter::DidCloseTempFile( | 430 void URLFetcherImpl::Core::TempFileWriter::DidCloseTempFile( |
431 base::PlatformFileError error_code) { | 431 base::PlatformFileError error_code) { |
432 DCHECK(core_->io_message_loop_proxy_->BelongsToCurrentThread()); | 432 DCHECK(core_->io_message_loop_proxy_->BelongsToCurrentThread()); |
433 | 433 |
434 if (base::PLATFORM_FILE_OK != error_code) { | 434 if (base::PLATFORM_FILE_OK != error_code) { |
435 error_code_ = error_code; | 435 error_code_ = error_code; |
436 RemoveTempFile(); | 436 RemoveTempFile(); |
437 core_->delegate_loop_proxy_->PostTask( | 437 core_->delegate_loop_proxy_->PostTask( |
438 FROM_HERE, base::Bind(&Core::InformDelegateFetchIsComplete, core_)); | 438 FROM_HERE, base::Bind(&Core::InformDelegateFetchIsComplete, core_)); |
439 return; | 439 return; |
440 } | 440 } |
441 | 441 |
442 // If the file was successfully closed, then the URL request is complete. | 442 // If the file was successfully closed, then the URL request is complete. |
443 core_->RetryOrCompleteUrlFetch(); | 443 core_->RetryOrCompleteUrlFetch(); |
444 } | 444 } |
445 | 445 |
446 void URLFetcher::Core::TempFileWriter::RemoveTempFile() { | 446 void URLFetcherImpl::Core::TempFileWriter::RemoveTempFile() { |
447 DCHECK(core_->io_message_loop_proxy_->BelongsToCurrentThread()); | 447 DCHECK(core_->io_message_loop_proxy_->BelongsToCurrentThread()); |
448 | 448 |
449 // Close the temp file if it is open. | 449 // Close the temp file if it is open. |
450 if (temp_file_handle_ != base::kInvalidPlatformFileValue) { | 450 if (temp_file_handle_ != base::kInvalidPlatformFileValue) { |
451 base::FileUtilProxy::Close( | 451 base::FileUtilProxy::Close( |
452 file_message_loop_proxy_, temp_file_handle_, | 452 file_message_loop_proxy_, temp_file_handle_, |
453 base::FileUtilProxy::StatusCallback()); // No callback: Ignore errors. | 453 base::FileUtilProxy::StatusCallback()); // No callback: Ignore errors. |
454 temp_file_handle_ = base::kInvalidPlatformFileValue; | 454 temp_file_handle_ = base::kInvalidPlatformFileValue; |
455 } | 455 } |
456 | 456 |
457 if (!temp_file_.empty()) { | 457 if (!temp_file_.empty()) { |
458 base::FileUtilProxy::Delete( | 458 base::FileUtilProxy::Delete( |
459 file_message_loop_proxy_, temp_file_, | 459 file_message_loop_proxy_, temp_file_, |
460 false, // No need to recurse, as the path is to a file. | 460 false, // No need to recurse, as the path is to a file. |
461 base::FileUtilProxy::StatusCallback()); // No callback: Ignore errors. | 461 base::FileUtilProxy::StatusCallback()); // No callback: Ignore errors. |
462 DisownTempFile(); | 462 DisownTempFile(); |
463 } | 463 } |
464 } | 464 } |
465 | 465 |
466 static content::URLFetcherFactory* g_factory = NULL; | 466 static content::URLFetcherFactory* g_factory = NULL; |
467 static bool g_interception_enabled = false; | 467 static bool g_interception_enabled = false; |
468 | 468 |
469 // static | 469 // static |
470 content::URLFetcher* content::URLFetcher::Create( | 470 content::URLFetcher* content::URLFetcher::Create( |
471 const GURL& url, | 471 const GURL& url, |
472 RequestType request_type, | 472 RequestType request_type, |
473 content::URLFetcherDelegate* d) { | 473 content::URLFetcherDelegate* d) { |
474 return new ::URLFetcher(url, request_type, d); | 474 return new URLFetcherImpl(url, request_type, d); |
475 } | 475 } |
476 | 476 |
477 // static | 477 // static |
478 content::URLFetcher* content::URLFetcher::Create( | 478 content::URLFetcher* content::URLFetcher::Create( |
479 int id, | 479 int id, |
480 const GURL& url, | 480 const GURL& url, |
481 RequestType request_type, | 481 RequestType request_type, |
482 content::URLFetcherDelegate* d) { | 482 content::URLFetcherDelegate* d) { |
483 return g_factory ? g_factory->CreateURLFetcher(id, url, request_type, d) : | 483 return g_factory ? g_factory->CreateURLFetcher(id, url, request_type, d) : |
484 new ::URLFetcher(url, request_type, d); | 484 new URLFetcherImpl(url, request_type, d); |
485 } | 485 } |
486 | 486 |
487 // static | 487 // static |
488 void content::URLFetcher::CancelAll() { | 488 void content::URLFetcher::CancelAll() { |
489 ::URLFetcher::CancelAll(); | 489 URLFetcherImpl::CancelAll(); |
490 } | 490 } |
491 | 491 |
492 // static | 492 // static |
493 void content::URLFetcher::SetEnableInterceptionForTests(bool enabled) { | 493 void content::URLFetcher::SetEnableInterceptionForTests(bool enabled) { |
494 g_interception_enabled = enabled; | 494 g_interception_enabled = enabled; |
495 } | 495 } |
496 | 496 |
497 | 497 |
498 URLFetcher::URLFetcher(const GURL& url, | 498 URLFetcherImpl::URLFetcherImpl(const GURL& url, |
499 RequestType request_type, | 499 RequestType request_type, |
500 content::URLFetcherDelegate* d) | 500 content::URLFetcherDelegate* d) |
501 : ALLOW_THIS_IN_INITIALIZER_LIST( | 501 : ALLOW_THIS_IN_INITIALIZER_LIST( |
502 core_(new Core(this, url, request_type, d))) { | 502 core_(new Core(this, url, request_type, d))) { |
503 } | 503 } |
504 | 504 |
505 URLFetcher::~URLFetcher() { | 505 URLFetcherImpl::~URLFetcherImpl() { |
506 core_->Stop(); | 506 core_->Stop(); |
507 } | 507 } |
508 | 508 |
509 URLFetcher::Core::Core(URLFetcher* fetcher, | 509 URLFetcherImpl::Core::Core(URLFetcherImpl* fetcher, |
510 const GURL& original_url, | 510 const GURL& original_url, |
511 RequestType request_type, | 511 RequestType request_type, |
512 content::URLFetcherDelegate* d) | 512 content::URLFetcherDelegate* d) |
513 : fetcher_(fetcher), | 513 : fetcher_(fetcher), |
514 original_url_(original_url), | 514 original_url_(original_url), |
515 request_type_(request_type), | 515 request_type_(request_type), |
516 delegate_(d), | 516 delegate_(d), |
517 delegate_loop_proxy_( | 517 delegate_loop_proxy_( |
518 base::MessageLoopProxy::current()), | 518 base::MessageLoopProxy::current()), |
519 request_(NULL), | 519 request_(NULL), |
520 load_flags_(net::LOAD_NORMAL), | 520 load_flags_(net::LOAD_NORMAL), |
521 response_code_(RESPONSE_CODE_INVALID), | 521 response_code_(RESPONSE_CODE_INVALID), |
522 buffer_(new net::IOBuffer(kBufferSize)), | 522 buffer_(new net::IOBuffer(kBufferSize)), |
523 was_fetched_via_proxy_(false), | 523 was_fetched_via_proxy_(false), |
524 is_chunked_upload_(false), | 524 is_chunked_upload_(false), |
525 num_retries_(0), | 525 num_retries_(0), |
526 was_cancelled_(false), | 526 was_cancelled_(false), |
527 response_destination_(STRING), | 527 response_destination_(STRING), |
528 automatically_retry_on_5xx_(true), | 528 automatically_retry_on_5xx_(true), |
529 max_retries_(0) { | 529 max_retries_(0) { |
530 } | 530 } |
531 | 531 |
532 URLFetcher::Core::~Core() { | 532 URLFetcherImpl::Core::~Core() { |
533 // |request_| should be NULL. If not, it's unsafe to delete it here since we | 533 // |request_| should be NULL. If not, it's unsafe to delete it here since we |
534 // may not be on the IO thread. | 534 // may not be on the IO thread. |
535 DCHECK(!request_.get()); | 535 DCHECK(!request_.get()); |
536 } | 536 } |
537 | 537 |
538 void URLFetcher::Core::Start() { | 538 void URLFetcherImpl::Core::Start() { |
539 DCHECK(delegate_loop_proxy_); | 539 DCHECK(delegate_loop_proxy_); |
540 CHECK(request_context_getter_) << "We need an URLRequestContext!"; | 540 CHECK(request_context_getter_) << "We need an URLRequestContext!"; |
541 if (io_message_loop_proxy_) { | 541 if (io_message_loop_proxy_) { |
542 DCHECK_EQ(io_message_loop_proxy_, | 542 DCHECK_EQ(io_message_loop_proxy_, |
543 request_context_getter_->GetIOMessageLoopProxy()); | 543 request_context_getter_->GetIOMessageLoopProxy()); |
544 } else { | 544 } else { |
545 io_message_loop_proxy_ = request_context_getter_->GetIOMessageLoopProxy(); | 545 io_message_loop_proxy_ = request_context_getter_->GetIOMessageLoopProxy(); |
546 } | 546 } |
547 CHECK(io_message_loop_proxy_.get()) << "We need an IO message loop proxy"; | 547 CHECK(io_message_loop_proxy_.get()) << "We need an IO message loop proxy"; |
548 | 548 |
549 io_message_loop_proxy_->PostTask( | 549 io_message_loop_proxy_->PostTask( |
550 FROM_HERE, base::Bind(&Core::StartOnIOThread, this)); | 550 FROM_HERE, base::Bind(&Core::StartOnIOThread, this)); |
551 } | 551 } |
552 | 552 |
553 void URLFetcher::Core::StartOnIOThread() { | 553 void URLFetcherImpl::Core::StartOnIOThread() { |
554 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 554 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
555 | 555 |
556 switch (response_destination_) { | 556 switch (response_destination_) { |
557 case STRING: | 557 case STRING: |
558 StartURLRequestWhenAppropriate(); | 558 StartURLRequestWhenAppropriate(); |
559 break; | 559 break; |
560 | 560 |
561 case TEMP_FILE: | 561 case TEMP_FILE: |
562 DCHECK(file_message_loop_proxy_.get()) | 562 DCHECK(file_message_loop_proxy_.get()) |
563 << "Need to set the file message loop proxy."; | 563 << "Need to set the file message loop proxy."; |
564 | 564 |
565 temp_file_writer_.reset( | 565 temp_file_writer_.reset( |
566 new TempFileWriter(this, file_message_loop_proxy_)); | 566 new TempFileWriter(this, file_message_loop_proxy_)); |
567 | 567 |
568 // If the temp file is successfully created, | 568 // If the temp file is successfully created, |
569 // Core::StartURLRequestWhenAppropriate() will be called. | 569 // Core::StartURLRequestWhenAppropriate() will be called. |
570 temp_file_writer_->CreateTempFile(); | 570 temp_file_writer_->CreateTempFile(); |
571 break; | 571 break; |
572 | 572 |
573 default: | 573 default: |
574 NOTREACHED(); | 574 NOTREACHED(); |
575 } | 575 } |
576 } | 576 } |
577 | 577 |
578 void URLFetcher::Core::Stop() { | 578 void URLFetcherImpl::Core::Stop() { |
579 if (delegate_loop_proxy_) { // May be NULL in tests. | 579 if (delegate_loop_proxy_) { // May be NULL in tests. |
580 DCHECK(delegate_loop_proxy_->BelongsToCurrentThread()); | 580 DCHECK(delegate_loop_proxy_->BelongsToCurrentThread()); |
581 } | 581 } |
582 delegate_ = NULL; | 582 delegate_ = NULL; |
583 fetcher_ = NULL; | 583 fetcher_ = NULL; |
584 if (io_message_loop_proxy_.get()) { | 584 if (io_message_loop_proxy_.get()) { |
585 io_message_loop_proxy_->PostTask( | 585 io_message_loop_proxy_->PostTask( |
586 FROM_HERE, base::Bind(&Core::CancelURLRequest, this)); | 586 FROM_HERE, base::Bind(&Core::CancelURLRequest, this)); |
587 } | 587 } |
588 } | 588 } |
589 | 589 |
590 void URLFetcher::Core::ReceivedContentWasMalformed() { | 590 void URLFetcherImpl::Core::ReceivedContentWasMalformed() { |
591 DCHECK(delegate_loop_proxy_->BelongsToCurrentThread()); | 591 DCHECK(delegate_loop_proxy_->BelongsToCurrentThread()); |
592 if (io_message_loop_proxy_.get()) { | 592 if (io_message_loop_proxy_.get()) { |
593 io_message_loop_proxy_->PostTask( | 593 io_message_loop_proxy_->PostTask( |
594 FROM_HERE, base::Bind(&Core::NotifyMalformedContent, this)); | 594 FROM_HERE, base::Bind(&Core::NotifyMalformedContent, this)); |
595 } | 595 } |
596 } | 596 } |
597 | 597 |
598 void URLFetcher::Core::CancelAll() { | 598 void URLFetcherImpl::Core::CancelAll() { |
599 g_registry.Get().CancelAll(); | 599 g_registry.Get().CancelAll(); |
600 } | 600 } |
601 | 601 |
602 void URLFetcher::Core::OnResponseStarted(net::URLRequest* request) { | 602 void URLFetcherImpl::Core::OnResponseStarted(net::URLRequest* request) { |
603 DCHECK_EQ(request, request_.get()); | 603 DCHECK_EQ(request, request_.get()); |
604 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 604 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
605 if (request_->status().is_success()) { | 605 if (request_->status().is_success()) { |
606 response_code_ = request_->GetResponseCode(); | 606 response_code_ = request_->GetResponseCode(); |
607 response_headers_ = request_->response_headers(); | 607 response_headers_ = request_->response_headers(); |
608 socket_address_ = request_->GetSocketAddress(); | 608 socket_address_ = request_->GetSocketAddress(); |
609 was_fetched_via_proxy_ = request_->was_fetched_via_proxy(); | 609 was_fetched_via_proxy_ = request_->was_fetched_via_proxy(); |
610 } | 610 } |
611 | 611 |
612 ReadResponse(); | 612 ReadResponse(); |
613 } | 613 } |
614 | 614 |
615 void URLFetcher::Core::CompleteAddingUploadDataChunk( | 615 void URLFetcherImpl::Core::CompleteAddingUploadDataChunk( |
616 const std::string& content, bool is_last_chunk) { | 616 const std::string& content, bool is_last_chunk) { |
617 DCHECK(is_chunked_upload_); | 617 DCHECK(is_chunked_upload_); |
618 DCHECK(request_.get()); | 618 DCHECK(request_.get()); |
619 DCHECK(!content.empty()); | 619 DCHECK(!content.empty()); |
620 request_->AppendChunkToUpload(content.data(), | 620 request_->AppendChunkToUpload(content.data(), |
621 static_cast<int>(content.length()), | 621 static_cast<int>(content.length()), |
622 is_last_chunk); | 622 is_last_chunk); |
623 } | 623 } |
624 | 624 |
625 void URLFetcher::Core::AppendChunkToUpload(const std::string& content, | 625 void URLFetcherImpl::Core::AppendChunkToUpload(const std::string& content, |
626 bool is_last_chunk) { | 626 bool is_last_chunk) { |
627 DCHECK(delegate_loop_proxy_); | 627 DCHECK(delegate_loop_proxy_); |
628 CHECK(io_message_loop_proxy_.get()); | 628 CHECK(io_message_loop_proxy_.get()); |
629 io_message_loop_proxy_->PostTask( | 629 io_message_loop_proxy_->PostTask( |
630 FROM_HERE, | 630 FROM_HERE, |
631 base::Bind(&Core::CompleteAddingUploadDataChunk, this, content, | 631 base::Bind(&Core::CompleteAddingUploadDataChunk, this, content, |
632 is_last_chunk)); | 632 is_last_chunk)); |
633 } | 633 } |
634 | 634 |
635 // Return true if the write was done and reading may continue. | 635 // Return true if the write was done and reading may continue. |
636 // Return false if the write is pending, and the next read will | 636 // Return false if the write is pending, and the next read will |
637 // be done later. | 637 // be done later. |
638 bool URLFetcher::Core::WriteBuffer(int num_bytes) { | 638 bool URLFetcherImpl::Core::WriteBuffer(int num_bytes) { |
639 bool write_complete = false; | 639 bool write_complete = false; |
640 switch (response_destination_) { | 640 switch (response_destination_) { |
641 case STRING: | 641 case STRING: |
642 data_.append(buffer_->data(), num_bytes); | 642 data_.append(buffer_->data(), num_bytes); |
643 write_complete = true; | 643 write_complete = true; |
644 break; | 644 break; |
645 | 645 |
646 case TEMP_FILE: | 646 case TEMP_FILE: |
647 temp_file_writer_->WriteBuffer(num_bytes); | 647 temp_file_writer_->WriteBuffer(num_bytes); |
648 // WriteBuffer() sends a request the file thread. | 648 // WriteBuffer() sends a request the file thread. |
649 // The write is not done yet. | 649 // The write is not done yet. |
650 write_complete = false; | 650 write_complete = false; |
651 break; | 651 break; |
652 | 652 |
653 default: | 653 default: |
654 NOTREACHED(); | 654 NOTREACHED(); |
655 } | 655 } |
656 return write_complete; | 656 return write_complete; |
657 } | 657 } |
658 | 658 |
659 void URLFetcher::Core::OnReadCompleted(net::URLRequest* request, | 659 void URLFetcherImpl::Core::OnReadCompleted(net::URLRequest* request, |
660 int bytes_read) { | 660 int bytes_read) { |
661 DCHECK(request == request_); | 661 DCHECK(request == request_); |
662 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 662 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
663 | 663 |
664 url_ = request->url(); | 664 url_ = request->url(); |
665 url_throttler_entry_ = | 665 url_throttler_entry_ = |
666 net::URLRequestThrottlerManager::GetInstance()->RegisterRequestUrl(url_); | 666 net::URLRequestThrottlerManager::GetInstance()->RegisterRequestUrl(url_); |
667 | 667 |
668 bool waiting_on_write = false; | 668 bool waiting_on_write = false; |
669 do { | 669 do { |
(...skipping 24 matching lines...) Expand all Loading... |
694 // If the file is open, close it. After closing the file, | 694 // If the file is open, close it. After closing the file, |
695 // RetryOrCompleteUrlFetch() will be called. | 695 // RetryOrCompleteUrlFetch() will be called. |
696 temp_file_writer_->CloseTempFileAndCompleteRequest(); | 696 temp_file_writer_->CloseTempFileAndCompleteRequest(); |
697 } else { | 697 } else { |
698 // Otherwise, complete or retry the URL request directly. | 698 // Otherwise, complete or retry the URL request directly. |
699 RetryOrCompleteUrlFetch(); | 699 RetryOrCompleteUrlFetch(); |
700 } | 700 } |
701 } | 701 } |
702 } | 702 } |
703 | 703 |
704 void URLFetcher::Core::RetryOrCompleteUrlFetch() { | 704 void URLFetcherImpl::Core::RetryOrCompleteUrlFetch() { |
705 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 705 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
706 base::TimeDelta backoff_delay; | 706 base::TimeDelta backoff_delay; |
707 | 707 |
708 // Checks the response from server. | 708 // Checks the response from server. |
709 if (response_code_ >= 500 || | 709 if (response_code_ >= 500 || |
710 status_.error() == net::ERR_TEMPORARILY_THROTTLED) { | 710 status_.error() == net::ERR_TEMPORARILY_THROTTLED) { |
711 // When encountering a server error, we will send the request again | 711 // When encountering a server error, we will send the request again |
712 // after backoff time. | 712 // after backoff time. |
713 ++num_retries_; | 713 ++num_retries_; |
714 | 714 |
(...skipping 15 matching lines...) Expand all Loading... |
730 } | 730 } |
731 request_context_getter_ = NULL; | 731 request_context_getter_ = NULL; |
732 bool posted = delegate_loop_proxy_->PostTask( | 732 bool posted = delegate_loop_proxy_->PostTask( |
733 FROM_HERE, base::Bind(&Core::OnCompletedURLRequest, this, backoff_delay)); | 733 FROM_HERE, base::Bind(&Core::OnCompletedURLRequest, this, backoff_delay)); |
734 | 734 |
735 // If the delegate message loop does not exist any more, then the delegate | 735 // If the delegate message loop does not exist any more, then the delegate |
736 // should be gone too. | 736 // should be gone too. |
737 DCHECK(posted || !delegate_); | 737 DCHECK(posted || !delegate_); |
738 } | 738 } |
739 | 739 |
740 void URLFetcher::Core::ReadResponse() { | 740 void URLFetcherImpl::Core::ReadResponse() { |
741 // Some servers may treat HEAD requests as GET requests. To free up the | 741 // Some servers may treat HEAD requests as GET requests. To free up the |
742 // network connection as soon as possible, signal that the request has | 742 // network connection as soon as possible, signal that the request has |
743 // completed immediately, without trying to read any data back (all we care | 743 // completed immediately, without trying to read any data back (all we care |
744 // about is the response code and headers, which we already have). | 744 // about is the response code and headers, which we already have). |
745 int bytes_read = 0; | 745 int bytes_read = 0; |
746 if (request_->status().is_success() && (request_type_ != HEAD)) | 746 if (request_->status().is_success() && (request_type_ != HEAD)) |
747 request_->Read(buffer_, kBufferSize, &bytes_read); | 747 request_->Read(buffer_, kBufferSize, &bytes_read); |
748 OnReadCompleted(request_.get(), bytes_read); | 748 OnReadCompleted(request_.get(), bytes_read); |
749 } | 749 } |
750 | 750 |
751 void URLFetcher::Core::DisownTempFile() { | 751 void URLFetcherImpl::Core::DisownTempFile() { |
752 temp_file_writer_->DisownTempFile(); | 752 temp_file_writer_->DisownTempFile(); |
753 } | 753 } |
754 | 754 |
755 void URLFetcher::Core::StartURLRequest() { | 755 void URLFetcherImpl::Core::StartURLRequest() { |
756 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 756 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
757 | 757 |
758 if (was_cancelled_) { | 758 if (was_cancelled_) { |
759 // Since StartURLRequest() is posted as a *delayed* task, it may | 759 // Since StartURLRequest() is posted as a *delayed* task, it may |
760 // run after the URLFetcher was already stopped. | 760 // run after the URLFetcher was already stopped. |
761 return; | 761 return; |
762 } | 762 } |
763 | 763 |
764 CHECK(request_context_getter_); | 764 CHECK(request_context_getter_); |
765 DCHECK(!request_.get()); | 765 DCHECK(!request_.get()); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
808 data_.clear(); | 808 data_.clear(); |
809 | 809 |
810 // If we are writing the response to a file, the only caller | 810 // If we are writing the response to a file, the only caller |
811 // of this function should have created it and not written yet. | 811 // of this function should have created it and not written yet. |
812 CHECK(!temp_file_writer_.get() || | 812 CHECK(!temp_file_writer_.get() || |
813 temp_file_writer_->total_bytes_written() == 0); | 813 temp_file_writer_->total_bytes_written() == 0); |
814 | 814 |
815 request_->Start(); | 815 request_->Start(); |
816 } | 816 } |
817 | 817 |
818 void URLFetcher::Core::StartURLRequestWhenAppropriate() { | 818 void URLFetcherImpl::Core::StartURLRequestWhenAppropriate() { |
819 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 819 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
820 | 820 |
821 if (was_cancelled_) | 821 if (was_cancelled_) |
822 return; | 822 return; |
823 | 823 |
824 if (original_url_throttler_entry_ == NULL) { | 824 if (original_url_throttler_entry_ == NULL) { |
825 original_url_throttler_entry_ = | 825 original_url_throttler_entry_ = |
826 net::URLRequestThrottlerManager::GetInstance()->RegisterRequestUrl( | 826 net::URLRequestThrottlerManager::GetInstance()->RegisterRequestUrl( |
827 original_url_); | 827 original_url_); |
828 } | 828 } |
829 | 829 |
830 int64 delay = original_url_throttler_entry_->ReserveSendingTimeForNextRequest( | 830 int64 delay = original_url_throttler_entry_->ReserveSendingTimeForNextRequest( |
831 GetBackoffReleaseTime()); | 831 GetBackoffReleaseTime()); |
832 if (delay == 0) { | 832 if (delay == 0) { |
833 StartURLRequest(); | 833 StartURLRequest(); |
834 } else { | 834 } else { |
835 MessageLoop::current()->PostDelayedTask( | 835 MessageLoop::current()->PostDelayedTask( |
836 FROM_HERE, base::Bind(&Core::StartURLRequest, this), delay); | 836 FROM_HERE, base::Bind(&Core::StartURLRequest, this), delay); |
837 } | 837 } |
838 } | 838 } |
839 | 839 |
840 void URLFetcher::Core::CancelURLRequest() { | 840 void URLFetcherImpl::Core::CancelURLRequest() { |
841 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 841 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
842 | 842 |
843 if (request_.get()) { | 843 if (request_.get()) { |
844 request_->Cancel(); | 844 request_->Cancel(); |
845 ReleaseRequest(); | 845 ReleaseRequest(); |
846 } | 846 } |
847 // Release the reference to the request context. There could be multiple | 847 // Release the reference to the request context. There could be multiple |
848 // references to URLFetcher::Core at this point so it may take a while to | 848 // references to URLFetcher::Core at this point so it may take a while to |
849 // delete the object, but we cannot delay the destruction of the request | 849 // delete the object, but we cannot delay the destruction of the request |
850 // context. | 850 // context. |
851 request_context_getter_ = NULL; | 851 request_context_getter_ = NULL; |
852 was_cancelled_ = true; | 852 was_cancelled_ = true; |
853 temp_file_writer_.reset(); | 853 temp_file_writer_.reset(); |
854 } | 854 } |
855 | 855 |
856 void URLFetcher::Core::OnCompletedURLRequest( | 856 void URLFetcherImpl::Core::OnCompletedURLRequest( |
857 base::TimeDelta backoff_delay) { | 857 base::TimeDelta backoff_delay) { |
858 DCHECK(delegate_loop_proxy_->BelongsToCurrentThread()); | 858 DCHECK(delegate_loop_proxy_->BelongsToCurrentThread()); |
859 | 859 |
860 // Save the status and backoff_delay so that delegates can read it. | 860 // Save the status and backoff_delay so that delegates can read it. |
861 if (delegate_) { | 861 if (delegate_) { |
862 backoff_delay_ = backoff_delay; | 862 backoff_delay_ = backoff_delay; |
863 InformDelegateFetchIsComplete(); | 863 InformDelegateFetchIsComplete(); |
864 } | 864 } |
865 } | 865 } |
866 | 866 |
867 void URLFetcher::Core::InformDelegateFetchIsComplete() { | 867 void URLFetcherImpl::Core::InformDelegateFetchIsComplete() { |
868 CHECK(delegate_loop_proxy_->BelongsToCurrentThread()); | 868 CHECK(delegate_loop_proxy_->BelongsToCurrentThread()); |
869 if (delegate_) { | 869 if (delegate_) { |
870 delegate_->OnURLFetchComplete(fetcher_); | 870 delegate_->OnURLFetchComplete(fetcher_); |
871 } | 871 } |
872 } | 872 } |
873 | 873 |
874 void URLFetcher::Core::NotifyMalformedContent() { | 874 void URLFetcherImpl::Core::NotifyMalformedContent() { |
875 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 875 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
876 if (url_throttler_entry_ != NULL) { | 876 if (url_throttler_entry_ != NULL) { |
877 int status_code = response_code_; | 877 int status_code = response_code_; |
878 if (status_code == RESPONSE_CODE_INVALID) { | 878 if (status_code == RESPONSE_CODE_INVALID) { |
879 // The status code will generally be known by the time clients | 879 // The status code will generally be known by the time clients |
880 // call the |ReceivedContentWasMalformed()| function (which ends up | 880 // call the |ReceivedContentWasMalformed()| function (which ends up |
881 // calling the current function) but if it's not, we need to assume | 881 // calling the current function) but if it's not, we need to assume |
882 // the response was successful so that the total failure count | 882 // the response was successful so that the total failure count |
883 // used to calculate exponential back-off goes up. | 883 // used to calculate exponential back-off goes up. |
884 status_code = 200; | 884 status_code = 200; |
885 } | 885 } |
886 url_throttler_entry_->ReceivedContentWasMalformed(status_code); | 886 url_throttler_entry_->ReceivedContentWasMalformed(status_code); |
887 } | 887 } |
888 } | 888 } |
889 | 889 |
890 void URLFetcher::Core::ReleaseRequest() { | 890 void URLFetcherImpl::Core::ReleaseRequest() { |
891 request_.reset(); | 891 request_.reset(); |
892 g_registry.Get().RemoveURLFetcherCore(this); | 892 g_registry.Get().RemoveURLFetcherCore(this); |
893 } | 893 } |
894 | 894 |
895 base::TimeTicks URLFetcher::Core::GetBackoffReleaseTime() { | 895 base::TimeTicks URLFetcherImpl::Core::GetBackoffReleaseTime() { |
896 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 896 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
897 DCHECK(original_url_throttler_entry_ != NULL); | 897 DCHECK(original_url_throttler_entry_ != NULL); |
898 | 898 |
899 base::TimeTicks original_url_backoff = | 899 base::TimeTicks original_url_backoff = |
900 original_url_throttler_entry_->GetExponentialBackoffReleaseTime(); | 900 original_url_throttler_entry_->GetExponentialBackoffReleaseTime(); |
901 base::TimeTicks destination_url_backoff; | 901 base::TimeTicks destination_url_backoff; |
902 if (url_throttler_entry_ != NULL && | 902 if (url_throttler_entry_ != NULL && |
903 original_url_throttler_entry_ != url_throttler_entry_) { | 903 original_url_throttler_entry_ != url_throttler_entry_) { |
904 destination_url_backoff = | 904 destination_url_backoff = |
905 url_throttler_entry_->GetExponentialBackoffReleaseTime(); | 905 url_throttler_entry_->GetExponentialBackoffReleaseTime(); |
906 } | 906 } |
907 | 907 |
908 return original_url_backoff > destination_url_backoff ? | 908 return original_url_backoff > destination_url_backoff ? |
909 original_url_backoff : destination_url_backoff; | 909 original_url_backoff : destination_url_backoff; |
910 } | 910 } |
911 | 911 |
912 void URLFetcher::SetUploadData(const std::string& upload_content_type, | 912 void URLFetcherImpl::SetUploadData(const std::string& upload_content_type, |
913 const std::string& upload_content) { | 913 const std::string& upload_content) { |
914 DCHECK(!core_->is_chunked_upload_); | 914 DCHECK(!core_->is_chunked_upload_); |
915 core_->upload_content_type_ = upload_content_type; | 915 core_->upload_content_type_ = upload_content_type; |
916 core_->upload_content_ = upload_content; | 916 core_->upload_content_ = upload_content; |
917 } | 917 } |
918 | 918 |
919 void URLFetcher::SetChunkedUpload(const std::string& content_type) { | 919 void URLFetcherImpl::SetChunkedUpload(const std::string& content_type) { |
920 DCHECK(core_->is_chunked_upload_ || | 920 DCHECK(core_->is_chunked_upload_ || |
921 (core_->upload_content_type_.empty() && | 921 (core_->upload_content_type_.empty() && |
922 core_->upload_content_.empty())); | 922 core_->upload_content_.empty())); |
923 core_->upload_content_type_ = content_type; | 923 core_->upload_content_type_ = content_type; |
924 core_->upload_content_.clear(); | 924 core_->upload_content_.clear(); |
925 core_->is_chunked_upload_ = true; | 925 core_->is_chunked_upload_ = true; |
926 } | 926 } |
927 | 927 |
928 void URLFetcher::AppendChunkToUpload(const std::string& data, | 928 void URLFetcherImpl::AppendChunkToUpload(const std::string& data, |
929 bool is_last_chunk) { | 929 bool is_last_chunk) { |
930 DCHECK(data.length()); | 930 DCHECK(data.length()); |
931 core_->AppendChunkToUpload(data, is_last_chunk); | 931 core_->AppendChunkToUpload(data, is_last_chunk); |
932 } | 932 } |
933 | 933 |
934 const std::string& URLFetcher::upload_data() const { | 934 const std::string& URLFetcherImpl::upload_data() const { |
935 return core_->upload_content_; | 935 return core_->upload_content_; |
936 } | 936 } |
937 | 937 |
938 void URLFetcher::SetReferrer(const std::string& referrer) { | 938 void URLFetcherImpl::SetReferrer(const std::string& referrer) { |
939 core_->referrer_ = referrer; | 939 core_->referrer_ = referrer; |
940 } | 940 } |
941 | 941 |
942 void URLFetcher::SetLoadFlags(int load_flags) { | 942 void URLFetcherImpl::SetLoadFlags(int load_flags) { |
943 core_->load_flags_ = load_flags; | 943 core_->load_flags_ = load_flags; |
944 } | 944 } |
945 | 945 |
946 int URLFetcher::GetLoadFlags() const { | 946 int URLFetcherImpl::GetLoadFlags() const { |
947 return core_->load_flags_; | 947 return core_->load_flags_; |
948 } | 948 } |
949 | 949 |
950 void URLFetcher::SetExtraRequestHeaders( | 950 void URLFetcherImpl::SetExtraRequestHeaders( |
951 const std::string& extra_request_headers) { | 951 const std::string& extra_request_headers) { |
952 core_->extra_request_headers_.Clear(); | 952 core_->extra_request_headers_.Clear(); |
953 core_->extra_request_headers_.AddHeadersFromString(extra_request_headers); | 953 core_->extra_request_headers_.AddHeadersFromString(extra_request_headers); |
954 } | 954 } |
955 | 955 |
956 void URLFetcher::GetExtraRequestHeaders(net::HttpRequestHeaders* headers) { | 956 void URLFetcherImpl::GetExtraRequestHeaders(net::HttpRequestHeaders* headers) { |
957 headers->CopyFrom(core_->extra_request_headers_); | 957 headers->CopyFrom(core_->extra_request_headers_); |
958 } | 958 } |
959 | 959 |
960 void URLFetcher::SetRequestContext( | 960 void URLFetcherImpl::SetRequestContext( |
961 net::URLRequestContextGetter* request_context_getter) { | 961 net::URLRequestContextGetter* request_context_getter) { |
962 DCHECK(!core_->request_context_getter_); | 962 DCHECK(!core_->request_context_getter_); |
963 core_->request_context_getter_ = request_context_getter; | 963 core_->request_context_getter_ = request_context_getter; |
964 } | 964 } |
965 | 965 |
966 void URLFetcher::SetAutomaticallyRetryOn5xx(bool retry) { | 966 void URLFetcherImpl::SetAutomaticallyRetryOn5xx(bool retry) { |
967 core_->automatically_retry_on_5xx_ = retry; | 967 core_->automatically_retry_on_5xx_ = retry; |
968 } | 968 } |
969 | 969 |
970 void URLFetcher::SetMaxRetries(int max_retries) { | 970 void URLFetcherImpl::SetMaxRetries(int max_retries) { |
971 core_->max_retries_ = max_retries; | 971 core_->max_retries_ = max_retries; |
972 } | 972 } |
973 | 973 |
974 int URLFetcher::GetMaxRetries() const { | 974 int URLFetcherImpl::GetMaxRetries() const { |
975 return core_->max_retries_; | 975 return core_->max_retries_; |
976 } | 976 } |
977 | 977 |
978 | 978 |
979 base::TimeDelta URLFetcher::GetBackoffDelay() const { | 979 base::TimeDelta URLFetcherImpl::GetBackoffDelay() const { |
980 return core_->backoff_delay_; | 980 return core_->backoff_delay_; |
981 } | 981 } |
982 | 982 |
983 void URLFetcher::SaveResponseToTemporaryFile( | 983 void URLFetcherImpl::SaveResponseToTemporaryFile( |
984 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) { | 984 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) { |
985 core_->file_message_loop_proxy_ = file_message_loop_proxy; | 985 core_->file_message_loop_proxy_ = file_message_loop_proxy; |
986 core_->response_destination_ = TEMP_FILE; | 986 core_->response_destination_ = TEMP_FILE; |
987 } | 987 } |
988 | 988 |
989 net::HttpResponseHeaders* URLFetcher::GetResponseHeaders() const { | 989 net::HttpResponseHeaders* URLFetcherImpl::GetResponseHeaders() const { |
990 return core_->response_headers_; | 990 return core_->response_headers_; |
991 } | 991 } |
992 | 992 |
993 void URLFetcher::set_response_headers( | 993 void URLFetcherImpl::set_response_headers( |
994 scoped_refptr<net::HttpResponseHeaders> headers) { | 994 scoped_refptr<net::HttpResponseHeaders> headers) { |
995 core_->response_headers_ = headers; | 995 core_->response_headers_ = headers; |
996 } | 996 } |
997 | 997 |
998 // TODO(panayiotis): socket_address_ is written in the IO thread, | 998 // TODO(panayiotis): socket_address_ is written in the IO thread, |
999 // if this is accessed in the UI thread, this could result in a race. | 999 // if this is accessed in the UI thread, this could result in a race. |
1000 // Same for response_headers_ above and was_fetched_via_proxy_ below. | 1000 // Same for response_headers_ above and was_fetched_via_proxy_ below. |
1001 net::HostPortPair URLFetcher::GetSocketAddress() const { | 1001 net::HostPortPair URLFetcherImpl::GetSocketAddress() const { |
1002 return core_->socket_address_; | 1002 return core_->socket_address_; |
1003 } | 1003 } |
1004 | 1004 |
1005 bool URLFetcher::WasFetchedViaProxy() const { | 1005 bool URLFetcherImpl::WasFetchedViaProxy() const { |
1006 return core_->was_fetched_via_proxy_; | 1006 return core_->was_fetched_via_proxy_; |
1007 } | 1007 } |
1008 | 1008 |
1009 void URLFetcher::set_was_fetched_via_proxy(bool flag) { | 1009 void URLFetcherImpl::set_was_fetched_via_proxy(bool flag) { |
1010 core_->was_fetched_via_proxy_ = flag; | 1010 core_->was_fetched_via_proxy_ = flag; |
1011 } | 1011 } |
1012 | 1012 |
1013 void URLFetcher::Start() { | 1013 void URLFetcherImpl::Start() { |
1014 core_->Start(); | 1014 core_->Start(); |
1015 } | 1015 } |
1016 | 1016 |
1017 void URLFetcher::StartWithRequestContextGetter( | 1017 void URLFetcherImpl::StartWithRequestContextGetter( |
1018 net::URLRequestContextGetter* request_context_getter) { | 1018 net::URLRequestContextGetter* request_context_getter) { |
1019 SetRequestContext(request_context_getter); | 1019 SetRequestContext(request_context_getter); |
1020 core_->Start(); | 1020 core_->Start(); |
1021 } | 1021 } |
1022 | 1022 |
1023 const GURL& URLFetcher::GetOriginalUrl() const { | 1023 const GURL& URLFetcherImpl::GetOriginalUrl() const { |
1024 return core_->original_url_; | 1024 return core_->original_url_; |
1025 } | 1025 } |
1026 | 1026 |
1027 const GURL& URLFetcher::GetUrl() const { | 1027 const GURL& URLFetcherImpl::GetUrl() const { |
1028 return core_->url_; | 1028 return core_->url_; |
1029 } | 1029 } |
1030 | 1030 |
1031 const net::URLRequestStatus& URLFetcher::GetStatus() const { | 1031 const net::URLRequestStatus& URLFetcherImpl::GetStatus() const { |
1032 return core_->status_; | 1032 return core_->status_; |
1033 } | 1033 } |
1034 | 1034 |
1035 int URLFetcher::GetResponseCode() const { | 1035 int URLFetcherImpl::GetResponseCode() const { |
1036 return core_->response_code_; | 1036 return core_->response_code_; |
1037 } | 1037 } |
1038 | 1038 |
1039 const net::ResponseCookies& URLFetcher::GetCookies() const { | 1039 const net::ResponseCookies& URLFetcherImpl::GetCookies() const { |
1040 return core_->cookies_; | 1040 return core_->cookies_; |
1041 } | 1041 } |
1042 | 1042 |
1043 bool URLFetcher::FileErrorOccurred( | 1043 bool URLFetcherImpl::FileErrorOccurred( |
1044 base::PlatformFileError* out_error_code) const { | 1044 base::PlatformFileError* out_error_code) const { |
1045 | 1045 |
1046 // Can't have a file error if no file is being created or written to. | 1046 // Can't have a file error if no file is being created or written to. |
1047 if (!core_->temp_file_writer_.get()) { | 1047 if (!core_->temp_file_writer_.get()) { |
1048 return false; | 1048 return false; |
1049 } | 1049 } |
1050 | 1050 |
1051 base::PlatformFileError error_code = core_->temp_file_writer_->error_code(); | 1051 base::PlatformFileError error_code = core_->temp_file_writer_->error_code(); |
1052 if (error_code == base::PLATFORM_FILE_OK) | 1052 if (error_code == base::PLATFORM_FILE_OK) |
1053 return false; | 1053 return false; |
1054 | 1054 |
1055 *out_error_code = error_code; | 1055 *out_error_code = error_code; |
1056 return true; | 1056 return true; |
1057 } | 1057 } |
1058 | 1058 |
1059 void URLFetcher::ReceivedContentWasMalformed() { | 1059 void URLFetcherImpl::ReceivedContentWasMalformed() { |
1060 core_->ReceivedContentWasMalformed(); | 1060 core_->ReceivedContentWasMalformed(); |
1061 } | 1061 } |
1062 | 1062 |
1063 bool URLFetcher::GetResponseAsString(std::string* out_response_string) const { | 1063 bool URLFetcherImpl::GetResponseAsString( |
| 1064 std::string* out_response_string) const { |
1064 if (core_->response_destination_ != STRING) | 1065 if (core_->response_destination_ != STRING) |
1065 return false; | 1066 return false; |
1066 | 1067 |
1067 *out_response_string = core_->data_; | 1068 *out_response_string = core_->data_; |
1068 return true; | 1069 return true; |
1069 } | 1070 } |
1070 | 1071 |
1071 bool URLFetcher::GetResponseAsFilePath(bool take_ownership, | 1072 bool URLFetcherImpl::GetResponseAsFilePath(bool take_ownership, |
1072 FilePath* out_response_path) const { | 1073 FilePath* out_response_path) const { |
1073 DCHECK(core_->delegate_loop_proxy_->BelongsToCurrentThread()); | 1074 DCHECK(core_->delegate_loop_proxy_->BelongsToCurrentThread()); |
1074 if (core_->response_destination_ != TEMP_FILE || | 1075 if (core_->response_destination_ != TEMP_FILE || |
1075 !core_->temp_file_writer_.get()) | 1076 !core_->temp_file_writer_.get()) |
1076 return false; | 1077 return false; |
1077 | 1078 |
1078 *out_response_path = core_->temp_file_writer_->temp_file(); | 1079 *out_response_path = core_->temp_file_writer_->temp_file(); |
1079 | 1080 |
1080 if (take_ownership) { | 1081 if (take_ownership) { |
1081 core_->io_message_loop_proxy_->PostTask( | 1082 core_->io_message_loop_proxy_->PostTask( |
1082 FROM_HERE, base::Bind(&Core::DisownTempFile, core_.get())); | 1083 FROM_HERE, base::Bind(&Core::DisownTempFile, core_.get())); |
1083 } | 1084 } |
1084 return true; | 1085 return true; |
1085 } | 1086 } |
1086 | 1087 |
1087 // static | 1088 // static |
1088 void URLFetcher::CancelAll() { | 1089 void URLFetcherImpl::CancelAll() { |
1089 Core::CancelAll(); | 1090 Core::CancelAll(); |
1090 } | 1091 } |
1091 | 1092 |
1092 // static | 1093 // static |
1093 int URLFetcher::GetNumFetcherCores() { | 1094 int URLFetcherImpl::GetNumFetcherCores() { |
1094 return Core::g_registry.Get().size(); | 1095 return Core::g_registry.Get().size(); |
1095 } | 1096 } |
1096 | 1097 |
1097 content::URLFetcherDelegate* URLFetcher::delegate() const { | 1098 content::URLFetcherDelegate* URLFetcherImpl::delegate() const { |
1098 return core_->delegate(); | 1099 return core_->delegate(); |
1099 } | 1100 } |
1100 | 1101 |
1101 // static | 1102 // static |
1102 content::URLFetcherFactory* URLFetcher::factory() { | 1103 content::URLFetcherFactory* URLFetcherImpl::factory() { |
1103 return g_factory; | 1104 return g_factory; |
1104 } | 1105 } |
1105 | 1106 |
1106 // static | 1107 // static |
1107 void URLFetcher::set_factory(content::URLFetcherFactory* factory) { | 1108 void URLFetcherImpl::set_factory(content::URLFetcherFactory* factory) { |
1108 g_factory = factory; | 1109 g_factory = factory; |
1109 } | 1110 } |
OLD | NEW |