Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(895)

Side by Side Diff: content/common/net/url_fetcher.cc

Issue 8375039: Create a content::UrlFetcher interface that lives in content/public/common and convert users to i... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: review comments Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « content/common/net/url_fetcher.h ('k') | content/common/net/url_fetcher_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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;
36 35
37 class URLFetcher::Core 36 class URLFetcher::Core
38 : public base::RefCountedThreadSafe<URLFetcher::Core>, 37 : public base::RefCountedThreadSafe<URLFetcher::Core>,
39 public net::URLRequest::Delegate { 38 public net::URLRequest::Delegate {
40 public: 39 public:
41 // For POST requests, set |content_type| to the MIME type of the content 40 // 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 41 // 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 42 // the load operation--these should be one or more of the LOAD_* flags
44 // defined in net/base/load_flags.h. 43 // defined in net/base/load_flags.h.
45 Core(URLFetcher* fetcher, 44 Core(URLFetcher* fetcher,
(...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after
460 false, // No need to recurse, as the path is to a file. 459 false, // No need to recurse, as the path is to a file.
461 base::FileUtilProxy::StatusCallback()); // No callback: Ignore errors. 460 base::FileUtilProxy::StatusCallback()); // No callback: Ignore errors.
462 DisownTempFile(); 461 DisownTempFile();
463 } 462 }
464 } 463 }
465 464
466 // static 465 // static
467 URLFetcher::Factory* URLFetcher::factory_ = NULL; 466 URLFetcher::Factory* URLFetcher::factory_ = NULL;
468 467
469 // static 468 // static
470 bool URLFetcher::g_interception_enabled = false; 469 static bool g_interception_enabled = false;
470
471 // static
472 content::URLFetcher* content::URLFetcher::Create(
473 const GURL& url,
474 RequestType request_type,
475 content::URLFetcherDelegate* d) {
476 return new ::URLFetcher(url, request_type, d);
477 }
478
479 // static
480 void content::URLFetcher::CancelAll() {
481 ::URLFetcher::CancelAll();
482 }
483
484 // static
485 void content::URLFetcher::SetEnableInterceptionForTests(bool enabled) {
486 g_interception_enabled = enabled;
487 }
488
471 489
472 URLFetcher::URLFetcher(const GURL& url, 490 URLFetcher::URLFetcher(const GURL& url,
473 RequestType request_type, 491 RequestType request_type,
474 content::URLFetcherDelegate* d) 492 content::URLFetcherDelegate* d)
475 : ALLOW_THIS_IN_INITIALIZER_LIST( 493 : ALLOW_THIS_IN_INITIALIZER_LIST(
476 core_(new Core(this, url, request_type, d))) { 494 core_(new Core(this, url, request_type, d))) {
477 } 495 }
478 496
479 URLFetcher::~URLFetcher() { 497 URLFetcher::~URLFetcher() {
480 core_->Stop(); 498 core_->Stop();
(...skipping 12 matching lines...) Expand all
493 RequestType request_type, 511 RequestType request_type,
494 content::URLFetcherDelegate* d) 512 content::URLFetcherDelegate* d)
495 : fetcher_(fetcher), 513 : fetcher_(fetcher),
496 original_url_(original_url), 514 original_url_(original_url),
497 request_type_(request_type), 515 request_type_(request_type),
498 delegate_(d), 516 delegate_(d),
499 delegate_loop_proxy_( 517 delegate_loop_proxy_(
500 base::MessageLoopProxy::current()), 518 base::MessageLoopProxy::current()),
501 request_(NULL), 519 request_(NULL),
502 load_flags_(net::LOAD_NORMAL), 520 load_flags_(net::LOAD_NORMAL),
503 response_code_(URLFetcher::kInvalidHttpResponseCode), 521 response_code_(RESPONSE_CODE_INVALID),
504 buffer_(new net::IOBuffer(kBufferSize)), 522 buffer_(new net::IOBuffer(kBufferSize)),
505 was_fetched_via_proxy_(false), 523 was_fetched_via_proxy_(false),
506 is_chunked_upload_(false), 524 is_chunked_upload_(false),
507 num_retries_(0), 525 num_retries_(0),
508 was_cancelled_(false), 526 was_cancelled_(false),
509 response_destination_(STRING), 527 response_destination_(STRING),
510 automatically_retry_on_5xx_(true), 528 automatically_retry_on_5xx_(true),
511 max_retries_(0) { 529 max_retries_(0) {
512 } 530 }
513 531
(...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after
850 CHECK(delegate_loop_proxy_->BelongsToCurrentThread()); 868 CHECK(delegate_loop_proxy_->BelongsToCurrentThread());
851 if (delegate_) { 869 if (delegate_) {
852 delegate_->OnURLFetchComplete(fetcher_); 870 delegate_->OnURLFetchComplete(fetcher_);
853 } 871 }
854 } 872 }
855 873
856 void URLFetcher::Core::NotifyMalformedContent() { 874 void URLFetcher::Core::NotifyMalformedContent() {
857 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); 875 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread());
858 if (url_throttler_entry_ != NULL) { 876 if (url_throttler_entry_ != NULL) {
859 int status_code = response_code_; 877 int status_code = response_code_;
860 if (status_code == kInvalidHttpResponseCode) { 878 if (status_code == RESPONSE_CODE_INVALID) {
861 // The status code will generally be known by the time clients 879 // The status code will generally be known by the time clients
862 // call the |ReceivedContentWasMalformed()| function (which ends up 880 // call the |ReceivedContentWasMalformed()| function (which ends up
863 // 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
864 // the response was successful so that the total failure count 882 // the response was successful so that the total failure count
865 // used to calculate exponential back-off goes up. 883 // used to calculate exponential back-off goes up.
866 status_code = 200; 884 status_code = 200;
867 } 885 }
868 url_throttler_entry_->ReceivedContentWasMalformed(status_code); 886 url_throttler_entry_->ReceivedContentWasMalformed(status_code);
869 } 887 }
870 } 888 }
(...skipping 13 matching lines...) Expand all
884 if (url_throttler_entry_ != NULL && 902 if (url_throttler_entry_ != NULL &&
885 original_url_throttler_entry_ != url_throttler_entry_) { 903 original_url_throttler_entry_ != url_throttler_entry_) {
886 destination_url_backoff = 904 destination_url_backoff =
887 url_throttler_entry_->GetExponentialBackoffReleaseTime(); 905 url_throttler_entry_->GetExponentialBackoffReleaseTime();
888 } 906 }
889 907
890 return original_url_backoff > destination_url_backoff ? 908 return original_url_backoff > destination_url_backoff ?
891 original_url_backoff : destination_url_backoff; 909 original_url_backoff : destination_url_backoff;
892 } 910 }
893 911
894 void URLFetcher::set_upload_data(const std::string& upload_content_type, 912 void URLFetcher::SetUploadData(const std::string& upload_content_type,
895 const std::string& upload_content) { 913 const std::string& upload_content) {
896 DCHECK(!core_->is_chunked_upload_); 914 DCHECK(!core_->is_chunked_upload_);
897 core_->upload_content_type_ = upload_content_type; 915 core_->upload_content_type_ = upload_content_type;
898 core_->upload_content_ = upload_content; 916 core_->upload_content_ = upload_content;
899 } 917 }
900 918
901 void URLFetcher::set_chunked_upload(const std::string& content_type) { 919 void URLFetcher::SetChunkedUpload(const std::string& content_type) {
902 DCHECK(core_->is_chunked_upload_ || 920 DCHECK(core_->is_chunked_upload_ ||
903 (core_->upload_content_type_.empty() && 921 (core_->upload_content_type_.empty() &&
904 core_->upload_content_.empty())); 922 core_->upload_content_.empty()));
905 core_->upload_content_type_ = content_type; 923 core_->upload_content_type_ = content_type;
906 core_->upload_content_.clear(); 924 core_->upload_content_.clear();
907 core_->is_chunked_upload_ = true; 925 core_->is_chunked_upload_ = true;
908 } 926 }
909 927
910 void URLFetcher::AppendChunkToUpload(const std::string& data, 928 void URLFetcher::AppendChunkToUpload(const std::string& data,
911 bool is_last_chunk) { 929 bool is_last_chunk) {
912 DCHECK(data.length()); 930 DCHECK(data.length());
913 core_->AppendChunkToUpload(data, is_last_chunk); 931 core_->AppendChunkToUpload(data, is_last_chunk);
914 } 932 }
915 933
916 const std::string& URLFetcher::upload_data() const { 934 const std::string& URLFetcher::upload_data() const {
917 return core_->upload_content_; 935 return core_->upload_content_;
918 } 936 }
919 937
920 void URLFetcher::set_referrer(const std::string& referrer) { 938 void URLFetcher::SetReferrer(const std::string& referrer) {
921 core_->referrer_ = referrer; 939 core_->referrer_ = referrer;
922 } 940 }
923 941
924 void URLFetcher::set_load_flags(int load_flags) { 942 void URLFetcher::SetLoadFlags(int load_flags) {
925 core_->load_flags_ = load_flags; 943 core_->load_flags_ = load_flags;
926 } 944 }
927 945
928 int URLFetcher::load_flags() const { 946 int URLFetcher::GetLoadFlags() const {
929 return core_->load_flags_; 947 return core_->load_flags_;
930 } 948 }
931 949
932 void URLFetcher::set_extra_request_headers( 950 void URLFetcher::SetExtraRequestHeaders(
933 const std::string& extra_request_headers) { 951 const std::string& extra_request_headers) {
934 core_->extra_request_headers_.Clear(); 952 core_->extra_request_headers_.Clear();
935 core_->extra_request_headers_.AddHeadersFromString(extra_request_headers); 953 core_->extra_request_headers_.AddHeadersFromString(extra_request_headers);
936 } 954 }
937 955
938 void URLFetcher::GetExtraRequestHeaders(net::HttpRequestHeaders* headers) { 956 void URLFetcher::GetExtraRequestHeaders(net::HttpRequestHeaders* headers) {
939 headers->CopyFrom(core_->extra_request_headers_); 957 headers->CopyFrom(core_->extra_request_headers_);
940 } 958 }
941 959
942 void URLFetcher::set_request_context( 960 void URLFetcher::SetRequestContext(
943 net::URLRequestContextGetter* request_context_getter) { 961 net::URLRequestContextGetter* request_context_getter) {
944 DCHECK(!core_->request_context_getter_); 962 DCHECK(!core_->request_context_getter_);
945 core_->request_context_getter_ = request_context_getter; 963 core_->request_context_getter_ = request_context_getter;
946 } 964 }
947 965
948 void URLFetcher::set_automatically_retry_on_5xx(bool retry) { 966 void URLFetcher::SetAutomaticallyRetryOn5xx(bool retry) {
949 core_->automatically_retry_on_5xx_ = retry; 967 core_->automatically_retry_on_5xx_ = retry;
950 } 968 }
951 969
952 int URLFetcher::max_retries() const { 970 void URLFetcher::SetMaxRetries(int max_retries) {
971 core_->max_retries_ = max_retries;
972 }
973
974 int URLFetcher::GetMaxRetries() const {
953 return core_->max_retries_; 975 return core_->max_retries_;
954 } 976 }
955 977
956 void URLFetcher::set_max_retries(int max_retries) {
957 core_->max_retries_ = max_retries;
958 }
959 978
960 base::TimeDelta URLFetcher::backoff_delay() const { 979 base::TimeDelta URLFetcher::GetBackoffDelay() const {
961 return core_->backoff_delay_; 980 return core_->backoff_delay_;
962 } 981 }
963 982
964 void URLFetcher::set_backoff_delay_for_testing( 983 void URLFetcher::SetBackoffDelayForTesting(
965 base::TimeDelta backoff_delay) { 984 base::TimeDelta backoff_delay) {
966 core_->backoff_delay_ = backoff_delay; 985 core_->backoff_delay_ = backoff_delay;
967 } 986 }
968 987
969 void URLFetcher::SaveResponseToTemporaryFile( 988 void URLFetcher::SaveResponseToTemporaryFile(
970 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) { 989 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) {
971 core_->file_message_loop_proxy_ = file_message_loop_proxy; 990 core_->file_message_loop_proxy_ = file_message_loop_proxy;
972 core_->response_destination_ = TEMP_FILE; 991 core_->response_destination_ = TEMP_FILE;
973 } 992 }
974 993
975 net::HttpResponseHeaders* URLFetcher::response_headers() const { 994 net::HttpResponseHeaders* URLFetcher::GetResponseHeaders() const {
976 return core_->response_headers_; 995 return core_->response_headers_;
977 } 996 }
978 997
979 void URLFetcher::set_response_headers( 998 void URLFetcher::set_response_headers(
980 scoped_refptr<net::HttpResponseHeaders> headers) { 999 scoped_refptr<net::HttpResponseHeaders> headers) {
981 core_->response_headers_ = headers; 1000 core_->response_headers_ = headers;
982 } 1001 }
983 1002
984 // TODO(panayiotis): socket_address_ is written in the IO thread, 1003 // 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. 1004 // 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. 1005 // Same for response_headers_ above and was_fetched_via_proxy_ below.
987 net::HostPortPair URLFetcher::socket_address() const { 1006 net::HostPortPair URLFetcher::GetSocketAddress() const {
988 return core_->socket_address_; 1007 return core_->socket_address_;
989 } 1008 }
990 1009
991 bool URLFetcher::was_fetched_via_proxy() const { 1010 bool URLFetcher::WasFetchedViaProxy() const {
992 return core_->was_fetched_via_proxy_; 1011 return core_->was_fetched_via_proxy_;
993 } 1012 }
994 1013
995 void URLFetcher::set_was_fetched_via_proxy(bool flag) { 1014 void URLFetcher::set_was_fetched_via_proxy(bool flag) {
996 core_->was_fetched_via_proxy_ = flag; 1015 core_->was_fetched_via_proxy_ = flag;
997 } 1016 }
998 1017
999 void URLFetcher::Start() { 1018 void URLFetcher::Start() {
1000 core_->Start(); 1019 core_->Start();
1001 } 1020 }
1002 1021
1003 void URLFetcher::StartWithRequestContextGetter( 1022 void URLFetcher::StartWithRequestContextGetter(
1004 net::URLRequestContextGetter* request_context_getter) { 1023 net::URLRequestContextGetter* request_context_getter) {
1005 set_request_context(request_context_getter); 1024 SetRequestContext(request_context_getter);
1006 core_->Start(); 1025 core_->Start();
1007 } 1026 }
1008 1027
1009 const GURL& URLFetcher::original_url() const { 1028 const GURL& URLFetcher::GetOriginalUrl() const {
1010 return core_->original_url_; 1029 return core_->original_url_;
1011 } 1030 }
1012 1031
1013 const GURL& URLFetcher::url() const { 1032 const GURL& URLFetcher::GetUrl() const {
1014 return core_->url_; 1033 return core_->url_;
1015 } 1034 }
1016 1035
1017 const net::URLRequestStatus& URLFetcher::status() const { 1036 const net::URLRequestStatus& URLFetcher::GetStatus() const {
1018 return core_->status_; 1037 return core_->status_;
1019 } 1038 }
1020 1039
1021 int URLFetcher::response_code() const { 1040 int URLFetcher::GetResponseCode() const {
1022 return core_->response_code_; 1041 return core_->response_code_;
1023 } 1042 }
1024 1043
1025 const net::ResponseCookies& URLFetcher::cookies() const { 1044 const net::ResponseCookies& URLFetcher::GetCookies() const {
1026 return core_->cookies_; 1045 return core_->cookies_;
1027 } 1046 }
1028 1047
1029 bool URLFetcher::FileErrorOccurred( 1048 bool URLFetcher::FileErrorOccurred(
1030 base::PlatformFileError* out_error_code) const { 1049 base::PlatformFileError* out_error_code) const {
1031 1050
1032 // Can't have a file error if no file is being created or written to. 1051 // Can't have a file error if no file is being created or written to.
1033 if (!core_->temp_file_writer_.get()) { 1052 if (!core_->temp_file_writer_.get()) {
1034 return false; 1053 return false;
1035 } 1054 }
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
1086 } 1105 }
1087 1106
1088 // static 1107 // static
1089 int URLFetcher::GetNumFetcherCores() { 1108 int URLFetcher::GetNumFetcherCores() {
1090 return Core::g_registry.Get().size(); 1109 return Core::g_registry.Get().size();
1091 } 1110 }
1092 1111
1093 content::URLFetcherDelegate* URLFetcher::delegate() const { 1112 content::URLFetcherDelegate* URLFetcher::delegate() const {
1094 return core_->delegate(); 1113 return core_->delegate();
1095 } 1114 }
OLDNEW
« no previous file with comments | « content/common/net/url_fetcher.h ('k') | content/common/net/url_fetcher_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698