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.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" |
16 #include "base/message_loop_proxy.h" | 16 #include "base/message_loop_proxy.h" |
17 #include "base/platform_file.h" | 17 #include "base/platform_file.h" |
18 #include "base/stl_util.h" | 18 #include "base/stl_util.h" |
19 #include "base/string_util.h" | 19 #include "base/string_util.h" |
20 #include "base/threading/thread.h" | 20 #include "base/threading/thread.h" |
21 #include "content/public/common/url_fetcher_delegate.h" | 21 #include "content/public/common/url_fetcher_delegate.h" |
22 #include "content/public/common/url_fetcher_factory.h" | |
22 #include "googleurl/src/gurl.h" | 23 #include "googleurl/src/gurl.h" |
23 #include "net/base/host_port_pair.h" | 24 #include "net/base/host_port_pair.h" |
24 #include "net/base/io_buffer.h" | 25 #include "net/base/io_buffer.h" |
25 #include "net/base/load_flags.h" | 26 #include "net/base/load_flags.h" |
26 #include "net/base/net_errors.h" | 27 #include "net/base/net_errors.h" |
27 #include "net/http/http_request_headers.h" | 28 #include "net/http/http_request_headers.h" |
28 #include "net/http/http_response_headers.h" | 29 #include "net/http/http_response_headers.h" |
29 #include "net/url_request/url_request.h" | 30 #include "net/url_request/url_request.h" |
30 #include "net/url_request/url_request_context.h" | 31 #include "net/url_request/url_request_context.h" |
31 #include "net/url_request/url_request_context_getter.h" | 32 #include "net/url_request/url_request_context_getter.h" |
(...skipping 424 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
456 if (!temp_file_.empty()) { | 457 if (!temp_file_.empty()) { |
457 base::FileUtilProxy::Delete( | 458 base::FileUtilProxy::Delete( |
458 file_message_loop_proxy_, temp_file_, | 459 file_message_loop_proxy_, temp_file_, |
459 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. |
460 base::FileUtilProxy::StatusCallback()); // No callback: Ignore errors. | 461 base::FileUtilProxy::StatusCallback()); // No callback: Ignore errors. |
461 DisownTempFile(); | 462 DisownTempFile(); |
462 } | 463 } |
463 } | 464 } |
464 | 465 |
465 // static | 466 // static |
466 URLFetcher::Factory* URLFetcher::factory_ = NULL; | 467 static content::URLFetcherFactory* g_factory = NULL; |
wtc
2011/10/26 23:34:46
Remove the "// static" comment on the previous lin
wtc
2011/10/27 21:29:16
Just wondering: why did you change this static mem
jam
2011/10/27 22:34:41
because now I need to use it from the static conte
| |
467 | 468 |
468 // static | 469 // static |
469 static bool g_interception_enabled = false; | 470 static bool g_interception_enabled = false; |
470 | 471 |
471 // static | 472 // static |
472 content::URLFetcher* content::URLFetcher::Create( | 473 content::URLFetcher* content::URLFetcher::Create( |
473 const GURL& url, | 474 const GURL& url, |
474 RequestType request_type, | 475 RequestType request_type, |
475 content::URLFetcherDelegate* d) { | 476 content::URLFetcherDelegate* d) { |
476 return new ::URLFetcher(url, request_type, d); | 477 return new ::URLFetcher(url, request_type, d); |
477 } | 478 } |
478 | 479 |
479 // static | 480 // static |
480 content::URLFetcher* content::URLFetcher::Create( | 481 content::URLFetcher* content::URLFetcher::Create( |
481 int id, | 482 int id, |
482 const GURL& url, | 483 const GURL& url, |
483 RequestType request_type, | 484 RequestType request_type, |
484 content::URLFetcherDelegate* d) { | 485 content::URLFetcherDelegate* d) { |
485 return ::URLFetcher::Create(id, url, request_type, d); | 486 return g_factory ? g_factory->CreateURLFetcher(id, url, request_type, d) : |
487 new ::URLFetcher(url, request_type, d); | |
486 } | 488 } |
487 | 489 |
488 // static | 490 // static |
489 void content::URLFetcher::CancelAll() { | 491 void content::URLFetcher::CancelAll() { |
490 ::URLFetcher::CancelAll(); | 492 ::URLFetcher::CancelAll(); |
491 } | 493 } |
492 | 494 |
493 // static | 495 // static |
494 void content::URLFetcher::SetEnableInterceptionForTests(bool enabled) { | 496 void content::URLFetcher::SetEnableInterceptionForTests(bool enabled) { |
495 g_interception_enabled = enabled; | 497 g_interception_enabled = enabled; |
496 } | 498 } |
497 | 499 |
498 | 500 |
499 URLFetcher::URLFetcher(const GURL& url, | 501 URLFetcher::URLFetcher(const GURL& url, |
500 RequestType request_type, | 502 RequestType request_type, |
501 content::URLFetcherDelegate* d) | 503 content::URLFetcherDelegate* d) |
502 : ALLOW_THIS_IN_INITIALIZER_LIST( | 504 : ALLOW_THIS_IN_INITIALIZER_LIST( |
503 core_(new Core(this, url, request_type, d))) { | 505 core_(new Core(this, url, request_type, d))) { |
504 } | 506 } |
505 | 507 |
506 URLFetcher::~URLFetcher() { | 508 URLFetcher::~URLFetcher() { |
507 core_->Stop(); | 509 core_->Stop(); |
508 } | 510 } |
509 | 511 |
510 // static | |
511 URLFetcher* URLFetcher::Create(int id, const GURL& url, | |
512 RequestType request_type, | |
513 content::URLFetcherDelegate* d) { | |
514 return factory_ ? factory_->CreateURLFetcher(id, url, request_type, d) : | |
515 new URLFetcher(url, request_type, d); | |
516 } | |
517 | |
518 URLFetcher::Core::Core(URLFetcher* fetcher, | 512 URLFetcher::Core::Core(URLFetcher* fetcher, |
519 const GURL& original_url, | 513 const GURL& original_url, |
520 RequestType request_type, | 514 RequestType request_type, |
521 content::URLFetcherDelegate* d) | 515 content::URLFetcherDelegate* d) |
522 : fetcher_(fetcher), | 516 : fetcher_(fetcher), |
523 original_url_(original_url), | 517 original_url_(original_url), |
524 request_type_(request_type), | 518 request_type_(request_type), |
525 delegate_(d), | 519 delegate_(d), |
526 delegate_loop_proxy_( | 520 delegate_loop_proxy_( |
527 base::MessageLoopProxy::current()), | 521 base::MessageLoopProxy::current()), |
(...skipping 454 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
982 | 976 |
983 int URLFetcher::GetMaxRetries() const { | 977 int URLFetcher::GetMaxRetries() const { |
984 return core_->max_retries_; | 978 return core_->max_retries_; |
985 } | 979 } |
986 | 980 |
987 | 981 |
988 base::TimeDelta URLFetcher::GetBackoffDelay() const { | 982 base::TimeDelta URLFetcher::GetBackoffDelay() const { |
989 return core_->backoff_delay_; | 983 return core_->backoff_delay_; |
990 } | 984 } |
991 | 985 |
992 void URLFetcher::SetBackoffDelayForTesting( | |
993 base::TimeDelta backoff_delay) { | |
994 core_->backoff_delay_ = backoff_delay; | |
995 } | |
996 | |
997 void URLFetcher::SaveResponseToTemporaryFile( | 986 void URLFetcher::SaveResponseToTemporaryFile( |
998 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) { | 987 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) { |
999 core_->file_message_loop_proxy_ = file_message_loop_proxy; | 988 core_->file_message_loop_proxy_ = file_message_loop_proxy; |
1000 core_->response_destination_ = TEMP_FILE; | 989 core_->response_destination_ = TEMP_FILE; |
1001 } | 990 } |
1002 | 991 |
1003 net::HttpResponseHeaders* URLFetcher::GetResponseHeaders() const { | 992 net::HttpResponseHeaders* URLFetcher::GetResponseHeaders() const { |
1004 return core_->response_headers_; | 993 return core_->response_headers_; |
1005 } | 994 } |
1006 | 995 |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1075 } | 1064 } |
1076 | 1065 |
1077 bool URLFetcher::GetResponseAsString(std::string* out_response_string) const { | 1066 bool URLFetcher::GetResponseAsString(std::string* out_response_string) const { |
1078 if (core_->response_destination_ != STRING) | 1067 if (core_->response_destination_ != STRING) |
1079 return false; | 1068 return false; |
1080 | 1069 |
1081 *out_response_string = core_->data_; | 1070 *out_response_string = core_->data_; |
1082 return true; | 1071 return true; |
1083 } | 1072 } |
1084 | 1073 |
1085 void URLFetcher::SetResponseDestinationForTesting( | |
1086 ResponseDestinationType value) { | |
1087 core_->response_destination_ = value; | |
1088 } | |
1089 | |
1090 URLFetcher::ResponseDestinationType | |
1091 URLFetcher::GetResponseDestinationForTesting() const { | |
1092 return core_->response_destination_; | |
1093 } | |
1094 | |
1095 bool URLFetcher::GetResponseAsFilePath(bool take_ownership, | 1074 bool URLFetcher::GetResponseAsFilePath(bool take_ownership, |
1096 FilePath* out_response_path) const { | 1075 FilePath* out_response_path) const { |
1097 DCHECK(core_->delegate_loop_proxy_->BelongsToCurrentThread()); | 1076 DCHECK(core_->delegate_loop_proxy_->BelongsToCurrentThread()); |
1098 if (core_->response_destination_ != TEMP_FILE || | 1077 if (core_->response_destination_ != TEMP_FILE || |
1099 !core_->temp_file_writer_.get()) | 1078 !core_->temp_file_writer_.get()) |
1100 return false; | 1079 return false; |
1101 | 1080 |
1102 *out_response_path = core_->temp_file_writer_->temp_file(); | 1081 *out_response_path = core_->temp_file_writer_->temp_file(); |
1103 | 1082 |
1104 if (take_ownership) { | 1083 if (take_ownership) { |
1105 core_->io_message_loop_proxy_->PostTask( | 1084 core_->io_message_loop_proxy_->PostTask( |
1106 FROM_HERE, base::Bind(&Core::DisownTempFile, core_.get())); | 1085 FROM_HERE, base::Bind(&Core::DisownTempFile, core_.get())); |
1107 } | 1086 } |
1108 return true; | 1087 return true; |
1109 } | 1088 } |
1110 | 1089 |
1111 // static | 1090 // static |
1112 void URLFetcher::CancelAll() { | 1091 void URLFetcher::CancelAll() { |
1113 Core::CancelAll(); | 1092 Core::CancelAll(); |
1114 } | 1093 } |
1115 | 1094 |
1116 // static | 1095 // static |
1117 int URLFetcher::GetNumFetcherCores() { | 1096 int URLFetcher::GetNumFetcherCores() { |
1118 return Core::g_registry.Get().size(); | 1097 return Core::g_registry.Get().size(); |
1119 } | 1098 } |
1120 | 1099 |
1121 content::URLFetcherDelegate* URLFetcher::delegate() const { | 1100 content::URLFetcherDelegate* URLFetcher::delegate() const { |
1122 return core_->delegate(); | 1101 return core_->delegate(); |
1123 } | 1102 } |
1103 | |
1104 // static | |
1105 content::URLFetcherFactory* URLFetcher::factory() { | |
1106 return g_factory; | |
1107 } | |
1108 | |
1109 // static | |
1110 void URLFetcher::set_factory(content::URLFetcherFactory* factory) { | |
1111 g_factory = factory; | |
1112 } | |
OLD | NEW |