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" |
(...skipping 14 matching lines...) Expand all Loading... |
25 #include "net/base/load_flags.h" | 25 #include "net/base/load_flags.h" |
26 #include "net/base/net_errors.h" | 26 #include "net/base/net_errors.h" |
27 #include "net/http/http_request_headers.h" | 27 #include "net/http/http_request_headers.h" |
28 #include "net/http/http_response_headers.h" | 28 #include "net/http/http_response_headers.h" |
29 #include "net/url_request/url_request.h" | 29 #include "net/url_request/url_request.h" |
30 #include "net/url_request/url_request_context.h" | 30 #include "net/url_request/url_request_context.h" |
31 #include "net/url_request/url_request_context_getter.h" | 31 #include "net/url_request/url_request_context_getter.h" |
32 #include "net/url_request/url_request_throttler_manager.h" | 32 #include "net/url_request/url_request_throttler_manager.h" |
33 | 33 |
34 static const int kBufferSize = 4096; | 34 static const int kBufferSize = 4096; |
35 const int URLFetcher::kInvalidHttpResponseCode = -1; | 35 const int content::URLFetcher::kInvalidHttpResponseCode = -1; |
36 | 36 |
37 class URLFetcher::Core | 37 class URLFetcher::Core |
38 : public base::RefCountedThreadSafe<URLFetcher::Core>, | 38 : public base::RefCountedThreadSafe<URLFetcher::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(URLFetcher* fetcher, |
(...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 | 466 // static |
467 URLFetcher::Factory* URLFetcher::factory_ = NULL; | 467 URLFetcher::Factory* URLFetcher::factory_ = NULL; |
468 | 468 |
469 // static | 469 // static |
470 bool URLFetcher::g_interception_enabled = false; | 470 static bool g_interception_enabled = false; |
| 471 |
| 472 // static |
| 473 content::URLFetcher* content::URLFetcher::Create( |
| 474 const GURL& url, |
| 475 RequestType request_type, |
| 476 content::URLFetcherDelegate* d) { |
| 477 return new ::URLFetcher(url, request_type, d); |
| 478 } |
| 479 |
| 480 // static |
| 481 void content::URLFetcher::CancelAll() { |
| 482 ::URLFetcher::CancelAll(); |
| 483 } |
| 484 |
| 485 // static |
| 486 void content::URLFetcher::SetEnableInterceptionForTests(bool enabled) { |
| 487 g_interception_enabled = enabled; |
| 488 } |
| 489 |
471 | 490 |
472 URLFetcher::URLFetcher(const GURL& url, | 491 URLFetcher::URLFetcher(const GURL& url, |
473 RequestType request_type, | 492 RequestType request_type, |
474 content::URLFetcherDelegate* d) | 493 content::URLFetcherDelegate* d) |
475 : ALLOW_THIS_IN_INITIALIZER_LIST( | 494 : ALLOW_THIS_IN_INITIALIZER_LIST( |
476 core_(new Core(this, url, request_type, d))) { | 495 core_(new Core(this, url, request_type, d))) { |
477 } | 496 } |
478 | 497 |
479 URLFetcher::~URLFetcher() { | 498 URLFetcher::~URLFetcher() { |
480 core_->Stop(); | 499 core_->Stop(); |
(...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
884 if (url_throttler_entry_ != NULL && | 903 if (url_throttler_entry_ != NULL && |
885 original_url_throttler_entry_ != url_throttler_entry_) { | 904 original_url_throttler_entry_ != url_throttler_entry_) { |
886 destination_url_backoff = | 905 destination_url_backoff = |
887 url_throttler_entry_->GetExponentialBackoffReleaseTime(); | 906 url_throttler_entry_->GetExponentialBackoffReleaseTime(); |
888 } | 907 } |
889 | 908 |
890 return original_url_backoff > destination_url_backoff ? | 909 return original_url_backoff > destination_url_backoff ? |
891 original_url_backoff : destination_url_backoff; | 910 original_url_backoff : destination_url_backoff; |
892 } | 911 } |
893 | 912 |
894 void URLFetcher::set_upload_data(const std::string& upload_content_type, | 913 void URLFetcher::SetUploadData(const std::string& upload_content_type, |
895 const std::string& upload_content) { | 914 const std::string& upload_content) { |
896 DCHECK(!core_->is_chunked_upload_); | 915 DCHECK(!core_->is_chunked_upload_); |
897 core_->upload_content_type_ = upload_content_type; | 916 core_->upload_content_type_ = upload_content_type; |
898 core_->upload_content_ = upload_content; | 917 core_->upload_content_ = upload_content; |
899 } | 918 } |
900 | 919 |
901 void URLFetcher::set_chunked_upload(const std::string& content_type) { | 920 void URLFetcher::SetChunkedUpload(const std::string& content_type) { |
902 DCHECK(core_->is_chunked_upload_ || | 921 DCHECK(core_->is_chunked_upload_ || |
903 (core_->upload_content_type_.empty() && | 922 (core_->upload_content_type_.empty() && |
904 core_->upload_content_.empty())); | 923 core_->upload_content_.empty())); |
905 core_->upload_content_type_ = content_type; | 924 core_->upload_content_type_ = content_type; |
906 core_->upload_content_.clear(); | 925 core_->upload_content_.clear(); |
907 core_->is_chunked_upload_ = true; | 926 core_->is_chunked_upload_ = true; |
908 } | 927 } |
909 | 928 |
910 void URLFetcher::AppendChunkToUpload(const std::string& data, | 929 void URLFetcher::AppendChunkToUpload(const std::string& data, |
911 bool is_last_chunk) { | 930 bool is_last_chunk) { |
912 DCHECK(data.length()); | 931 DCHECK(data.length()); |
913 core_->AppendChunkToUpload(data, is_last_chunk); | 932 core_->AppendChunkToUpload(data, is_last_chunk); |
914 } | 933 } |
915 | 934 |
916 const std::string& URLFetcher::upload_data() const { | 935 const std::string& URLFetcher::upload_data() const { |
917 return core_->upload_content_; | 936 return core_->upload_content_; |
918 } | 937 } |
919 | 938 |
920 void URLFetcher::set_referrer(const std::string& referrer) { | 939 void URLFetcher::SetReferrer(const std::string& referrer) { |
921 core_->referrer_ = referrer; | 940 core_->referrer_ = referrer; |
922 } | 941 } |
923 | 942 |
924 void URLFetcher::set_load_flags(int load_flags) { | 943 void URLFetcher::SetLoadFlags(int load_flags) { |
925 core_->load_flags_ = load_flags; | 944 core_->load_flags_ = load_flags; |
926 } | 945 } |
927 | 946 |
928 int URLFetcher::load_flags() const { | 947 int URLFetcher::GetLoadFlags() const { |
929 return core_->load_flags_; | 948 return core_->load_flags_; |
930 } | 949 } |
931 | 950 |
932 void URLFetcher::set_extra_request_headers( | 951 void URLFetcher::SetExtraRequestHeaders( |
933 const std::string& extra_request_headers) { | 952 const std::string& extra_request_headers) { |
934 core_->extra_request_headers_.Clear(); | 953 core_->extra_request_headers_.Clear(); |
935 core_->extra_request_headers_.AddHeadersFromString(extra_request_headers); | 954 core_->extra_request_headers_.AddHeadersFromString(extra_request_headers); |
936 } | 955 } |
937 | 956 |
938 void URLFetcher::GetExtraRequestHeaders(net::HttpRequestHeaders* headers) { | 957 void URLFetcher::GetExtraRequestHeaders(net::HttpRequestHeaders* headers) { |
939 headers->CopyFrom(core_->extra_request_headers_); | 958 headers->CopyFrom(core_->extra_request_headers_); |
940 } | 959 } |
941 | 960 |
942 void URLFetcher::set_request_context( | 961 void URLFetcher::SetRequestContext( |
943 net::URLRequestContextGetter* request_context_getter) { | 962 net::URLRequestContextGetter* request_context_getter) { |
944 DCHECK(!core_->request_context_getter_); | 963 DCHECK(!core_->request_context_getter_); |
945 core_->request_context_getter_ = request_context_getter; | 964 core_->request_context_getter_ = request_context_getter; |
946 } | 965 } |
947 | 966 |
948 void URLFetcher::set_automatically_retry_on_5xx(bool retry) { | 967 void URLFetcher::SetAutomaticallyRetryOn5xx(bool retry) { |
949 core_->automatically_retry_on_5xx_ = retry; | 968 core_->automatically_retry_on_5xx_ = retry; |
950 } | 969 } |
951 | 970 |
952 int URLFetcher::max_retries() const { | 971 void URLFetcher::SetMaxRetries(int max_retries) { |
| 972 core_->max_retries_ = max_retries; |
| 973 } |
| 974 |
| 975 int URLFetcher::GetMaxRetries() const { |
953 return core_->max_retries_; | 976 return core_->max_retries_; |
954 } | 977 } |
955 | 978 |
956 void URLFetcher::set_max_retries(int max_retries) { | |
957 core_->max_retries_ = max_retries; | |
958 } | |
959 | 979 |
960 base::TimeDelta URLFetcher::backoff_delay() const { | 980 base::TimeDelta URLFetcher::GetBackoffDelay() const { |
961 return core_->backoff_delay_; | 981 return core_->backoff_delay_; |
962 } | 982 } |
963 | 983 |
964 void URLFetcher::set_backoff_delay_for_testing( | 984 void URLFetcher::SetBackoffDelayForTesting( |
965 base::TimeDelta backoff_delay) { | 985 base::TimeDelta backoff_delay) { |
966 core_->backoff_delay_ = backoff_delay; | 986 core_->backoff_delay_ = backoff_delay; |
967 } | 987 } |
968 | 988 |
969 void URLFetcher::SaveResponseToTemporaryFile( | 989 void URLFetcher::SaveResponseToTemporaryFile( |
970 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) { | 990 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) { |
971 core_->file_message_loop_proxy_ = file_message_loop_proxy; | 991 core_->file_message_loop_proxy_ = file_message_loop_proxy; |
972 core_->response_destination_ = TEMP_FILE; | 992 core_->response_destination_ = TEMP_FILE; |
973 } | 993 } |
974 | 994 |
975 net::HttpResponseHeaders* URLFetcher::response_headers() const { | 995 net::HttpResponseHeaders* URLFetcher::GetResponseHeaders() const { |
976 return core_->response_headers_; | 996 return core_->response_headers_; |
977 } | 997 } |
978 | 998 |
979 void URLFetcher::set_response_headers( | 999 void URLFetcher::set_response_headers( |
980 scoped_refptr<net::HttpResponseHeaders> headers) { | 1000 scoped_refptr<net::HttpResponseHeaders> headers) { |
981 core_->response_headers_ = headers; | 1001 core_->response_headers_ = headers; |
982 } | 1002 } |
983 | 1003 |
984 // TODO(panayiotis): socket_address_ is written in the IO thread, | 1004 // TODO(panayiotis): socket_address_ is written in the IO thread, |
985 // if this is accessed in the UI thread, this could result in a race. | 1005 // if this is accessed in the UI thread, this could result in a race. |
986 // Same for response_headers_ above and was_fetched_via_proxy_ below. | 1006 // Same for response_headers_ above and was_fetched_via_proxy_ below. |
987 net::HostPortPair URLFetcher::socket_address() const { | 1007 net::HostPortPair URLFetcher::GetSocketAddress() const { |
988 return core_->socket_address_; | 1008 return core_->socket_address_; |
989 } | 1009 } |
990 | 1010 |
991 bool URLFetcher::was_fetched_via_proxy() const { | 1011 bool URLFetcher::WasFetchedViaProxy() const { |
992 return core_->was_fetched_via_proxy_; | 1012 return core_->was_fetched_via_proxy_; |
993 } | 1013 } |
994 | 1014 |
995 void URLFetcher::set_was_fetched_via_proxy(bool flag) { | 1015 void URLFetcher::set_was_fetched_via_proxy(bool flag) { |
996 core_->was_fetched_via_proxy_ = flag; | 1016 core_->was_fetched_via_proxy_ = flag; |
997 } | 1017 } |
998 | 1018 |
999 void URLFetcher::Start() { | 1019 void URLFetcher::Start() { |
1000 core_->Start(); | 1020 core_->Start(); |
1001 } | 1021 } |
1002 | 1022 |
1003 void URLFetcher::StartWithRequestContextGetter( | 1023 void URLFetcher::StartWithRequestContextGetter( |
1004 net::URLRequestContextGetter* request_context_getter) { | 1024 net::URLRequestContextGetter* request_context_getter) { |
1005 set_request_context(request_context_getter); | 1025 SetRequestContext(request_context_getter); |
1006 core_->Start(); | 1026 core_->Start(); |
1007 } | 1027 } |
1008 | 1028 |
1009 const GURL& URLFetcher::original_url() const { | 1029 const GURL& URLFetcher::GetOriginalUrl() const { |
1010 return core_->original_url_; | 1030 return core_->original_url_; |
1011 } | 1031 } |
1012 | 1032 |
1013 const GURL& URLFetcher::url() const { | 1033 const GURL& URLFetcher::GetUrl() const { |
1014 return core_->url_; | 1034 return core_->url_; |
1015 } | 1035 } |
1016 | 1036 |
1017 const net::URLRequestStatus& URLFetcher::status() const { | 1037 const net::URLRequestStatus& URLFetcher::GetStatus() const { |
1018 return core_->status_; | 1038 return core_->status_; |
1019 } | 1039 } |
1020 | 1040 |
1021 int URLFetcher::response_code() const { | 1041 int URLFetcher::GetResponseCode() const { |
1022 return core_->response_code_; | 1042 return core_->response_code_; |
1023 } | 1043 } |
1024 | 1044 |
1025 const net::ResponseCookies& URLFetcher::cookies() const { | 1045 const net::ResponseCookies& URLFetcher::GetCookies() const { |
1026 return core_->cookies_; | 1046 return core_->cookies_; |
1027 } | 1047 } |
1028 | 1048 |
1029 bool URLFetcher::FileErrorOccurred( | 1049 bool URLFetcher::FileErrorOccurred( |
1030 base::PlatformFileError* out_error_code) const { | 1050 base::PlatformFileError* out_error_code) const { |
1031 | 1051 |
1032 // Can't have a file error if no file is being created or written to. | 1052 // Can't have a file error if no file is being created or written to. |
1033 if (!core_->temp_file_writer_.get()) { | 1053 if (!core_->temp_file_writer_.get()) { |
1034 return false; | 1054 return false; |
1035 } | 1055 } |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1086 } | 1106 } |
1087 | 1107 |
1088 // static | 1108 // static |
1089 int URLFetcher::GetNumFetcherCores() { | 1109 int URLFetcher::GetNumFetcherCores() { |
1090 return Core::g_registry.Get().size(); | 1110 return Core::g_registry.Get().size(); |
1091 } | 1111 } |
1092 | 1112 |
1093 content::URLFetcherDelegate* URLFetcher::delegate() const { | 1113 content::URLFetcherDelegate* URLFetcher::delegate() const { |
1094 return core_->delegate(); | 1114 return core_->delegate(); |
1095 } | 1115 } |
OLD | NEW |