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

Side by Side Diff: net/proxy/proxy_service.cc

Issue 2299963002: Reland "Change ProxyResolver::GetProxyForURL() to take a unique_ptr<Request>* " (Closed)
Patch Set: remove fields proposed by eroman Created 4 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
« no previous file with comments | « net/proxy/proxy_resolver_winhttp.cc ('k') | net/proxy/proxy_service_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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/proxy/proxy_service.h" 5 #include "net/proxy/proxy_service.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cmath> 8 #include <cmath>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 180
181 // Proxy resolver that fails every time. 181 // Proxy resolver that fails every time.
182 class ProxyResolverNull : public ProxyResolver { 182 class ProxyResolverNull : public ProxyResolver {
183 public: 183 public:
184 ProxyResolverNull() {} 184 ProxyResolverNull() {}
185 185
186 // ProxyResolver implementation. 186 // ProxyResolver implementation.
187 int GetProxyForURL(const GURL& url, 187 int GetProxyForURL(const GURL& url,
188 ProxyInfo* results, 188 ProxyInfo* results,
189 const CompletionCallback& callback, 189 const CompletionCallback& callback,
190 RequestHandle* request, 190 std::unique_ptr<Request>* request,
191 const NetLogWithSource& net_log) override { 191 const NetLogWithSource& net_log) override {
192 return ERR_NOT_IMPLEMENTED; 192 return ERR_NOT_IMPLEMENTED;
193 } 193 }
194 194
195 void CancelRequest(RequestHandle request) override { NOTREACHED(); }
196
197 LoadState GetLoadState(RequestHandle request) const override {
198 NOTREACHED();
199 return LOAD_STATE_IDLE;
200 }
201
202 }; 195 };
203 196
204 // ProxyResolver that simulates a PAC script which returns 197 // ProxyResolver that simulates a PAC script which returns
205 // |pac_string| for every single URL. 198 // |pac_string| for every single URL.
206 class ProxyResolverFromPacString : public ProxyResolver { 199 class ProxyResolverFromPacString : public ProxyResolver {
207 public: 200 public:
208 explicit ProxyResolverFromPacString(const std::string& pac_string) 201 explicit ProxyResolverFromPacString(const std::string& pac_string)
209 : pac_string_(pac_string) {} 202 : pac_string_(pac_string) {}
210 203
211 int GetProxyForURL(const GURL& url, 204 int GetProxyForURL(const GURL& url,
212 ProxyInfo* results, 205 ProxyInfo* results,
213 const CompletionCallback& callback, 206 const CompletionCallback& callback,
214 RequestHandle* request, 207 std::unique_ptr<Request>* request,
215 const NetLogWithSource& net_log) override { 208 const NetLogWithSource& net_log) override {
216 results->UsePacString(pac_string_); 209 results->UsePacString(pac_string_);
217 return OK; 210 return OK;
218 } 211 }
219 212
220 void CancelRequest(RequestHandle request) override { NOTREACHED(); }
221
222 LoadState GetLoadState(RequestHandle request) const override {
223 NOTREACHED();
224 return LOAD_STATE_IDLE;
225 }
226
227 private: 213 private:
228 const std::string pac_string_; 214 const std::string pac_string_;
229 }; 215 };
230 216
231 // Creates ProxyResolvers using a platform-specific implementation. 217 // Creates ProxyResolvers using a platform-specific implementation.
232 class ProxyResolverFactoryForSystem : public MultiThreadedProxyResolverFactory { 218 class ProxyResolverFactoryForSystem : public MultiThreadedProxyResolverFactory {
233 public: 219 public:
234 explicit ProxyResolverFactoryForSystem(size_t max_num_threads) 220 explicit ProxyResolverFactoryForSystem(size_t max_num_threads)
235 : MultiThreadedProxyResolverFactory(max_num_threads, 221 : MultiThreadedProxyResolverFactory(max_num_threads,
236 false /*expects_pac_bytes*/) {} 222 false /*expects_pac_bytes*/) {}
(...skipping 556 matching lines...) Expand 10 before | Expand all | Expand 10 after
793 ProxyDelegate* proxy_delegate, 779 ProxyDelegate* proxy_delegate,
794 ProxyInfo* results, 780 ProxyInfo* results,
795 const CompletionCallback& user_callback, 781 const CompletionCallback& user_callback,
796 const NetLogWithSource& net_log) 782 const NetLogWithSource& net_log)
797 : service_(service), 783 : service_(service),
798 user_callback_(user_callback), 784 user_callback_(user_callback),
799 results_(results), 785 results_(results),
800 url_(url), 786 url_(url),
801 method_(method), 787 method_(method),
802 proxy_delegate_(proxy_delegate), 788 proxy_delegate_(proxy_delegate),
803 resolve_job_(NULL), 789 resolve_job_(nullptr),
804 config_id_(ProxyConfig::kInvalidConfigID), 790 config_id_(ProxyConfig::kInvalidConfigID),
805 config_source_(PROXY_CONFIG_SOURCE_UNKNOWN), 791 config_source_(PROXY_CONFIG_SOURCE_UNKNOWN),
806 net_log_(net_log), 792 net_log_(net_log),
807 creation_time_(TimeTicks::Now()) { 793 creation_time_(TimeTicks::Now()) {
808 DCHECK(!user_callback.is_null()); 794 DCHECK(!user_callback.is_null());
809 } 795 }
810 796
811 // Starts the resolve proxy request. 797 // Starts the resolve proxy request.
812 int Start() { 798 int Start() {
813 DCHECK(!was_cancelled()); 799 DCHECK(!was_cancelled());
814 DCHECK(!is_started()); 800 DCHECK(!is_started());
815 801
816 DCHECK(service_->config_.is_valid()); 802 DCHECK(service_->config_.is_valid());
817 803
818 config_id_ = service_->config_.id(); 804 config_id_ = service_->config_.id();
819 config_source_ = service_->config_.source(); 805 config_source_ = service_->config_.source();
820 806
821 return resolver()->GetProxyForURL( 807 return resolver()->GetProxyForURL(
822 url_, results_, 808 url_, results_,
823 base::Bind(&PacRequest::QueryComplete, base::Unretained(this)), 809 base::Bind(&PacRequest::QueryComplete, base::Unretained(this)),
824 &resolve_job_, net_log_); 810 &resolve_job_, net_log_);
825 } 811 }
826 812
827 bool is_started() const { 813 bool is_started() const {
828 // Note that !! casts to bool. (VS gives a warning otherwise). 814 // Note that !! casts to bool. (VS gives a warning otherwise).
829 return !!resolve_job_; 815 return !!resolve_job_.get();
830 } 816 }
831 817
832 void StartAndCompleteCheckingForSynchronous() { 818 void StartAndCompleteCheckingForSynchronous() {
833 int rv = 819 int rv =
834 service_->TryToCompleteSynchronously(url_, proxy_delegate_, results_); 820 service_->TryToCompleteSynchronously(url_, proxy_delegate_, results_);
835 if (rv == ERR_IO_PENDING) 821 if (rv == ERR_IO_PENDING)
836 rv = Start(); 822 rv = Start();
837 if (rv != ERR_IO_PENDING) 823 if (rv != ERR_IO_PENDING)
838 QueryComplete(rv); 824 QueryComplete(rv);
839 } 825 }
840 826
841 void CancelResolveJob() { 827 void CancelResolveJob() {
842 DCHECK(is_started()); 828 DCHECK(is_started());
843 // The request may already be running in the resolver. 829 // The request may already be running in the resolver.
844 resolver()->CancelRequest(resolve_job_); 830 resolve_job_.reset();
845 resolve_job_ = NULL;
846 DCHECK(!is_started()); 831 DCHECK(!is_started());
847 } 832 }
848 833
849 void Cancel() { 834 void Cancel() {
850 net_log_.AddEvent(NetLogEventType::CANCELLED); 835 net_log_.AddEvent(NetLogEventType::CANCELLED);
851 836
852 if (is_started()) 837 if (is_started())
853 CancelResolveJob(); 838 CancelResolveJob();
854 839
855 // Mark as cancelled, to prevent accessing this again later. 840 // Mark as cancelled, to prevent accessing this again later.
856 service_ = NULL; 841 service_ = NULL;
857 user_callback_.Reset(); 842 user_callback_.Reset();
858 results_ = NULL; 843 results_ = NULL;
859 844
860 net_log_.EndEvent(NetLogEventType::PROXY_SERVICE); 845 net_log_.EndEvent(NetLogEventType::PROXY_SERVICE);
861 } 846 }
862 847
863 // Returns true if Cancel() has been called. 848 // Returns true if Cancel() has been called.
864 bool was_cancelled() const { 849 bool was_cancelled() const {
865 return user_callback_.is_null(); 850 return user_callback_.is_null();
866 } 851 }
867 852
868 // Helper to call after ProxyResolver completion (both synchronous and 853 // Helper to call after ProxyResolver completion (both synchronous and
869 // asynchronous). Fixes up the result that is to be returned to user. 854 // asynchronous). Fixes up the result that is to be returned to user.
870 int QueryDidComplete(int result_code) { 855 int QueryDidComplete(int result_code) {
871 DCHECK(!was_cancelled()); 856 DCHECK(!was_cancelled());
872 857
873 // This state is cleared when resolve_job_ is set to nullptr below. 858 // This state is cleared when resolve_job_ is reset below.
874 bool script_executed = is_started(); 859 bool script_executed = is_started();
875 860
876 // Clear |resolve_job_| so is_started() returns false while 861 // Clear |resolve_job_| so is_started() returns false while
877 // DidFinishResolvingProxy() runs. 862 // DidFinishResolvingProxy() runs.
878 resolve_job_ = nullptr; 863 resolve_job_.reset();
879 864
880 // Note that DidFinishResolvingProxy might modify |results_|. 865 // Note that DidFinishResolvingProxy might modify |results_|.
881 int rv = service_->DidFinishResolvingProxy(url_, method_, proxy_delegate_, 866 int rv = service_->DidFinishResolvingProxy(url_, method_, proxy_delegate_,
882 results_, result_code, net_log_, 867 results_, result_code, net_log_,
883 creation_time_, script_executed); 868 creation_time_, script_executed);
884 869
885 // Make a note in the results which configuration was in use at the 870 // Make a note in the results which configuration was in use at the
886 // time of the resolve. 871 // time of the resolve.
887 results_->config_id_ = config_id_; 872 results_->config_id_ = config_id_;
888 results_->config_source_ = config_source_; 873 results_->config_source_ = config_source_;
889 results_->did_use_pac_script_ = true; 874 results_->did_use_pac_script_ = true;
890 results_->proxy_resolve_start_time_ = creation_time_; 875 results_->proxy_resolve_start_time_ = creation_time_;
891 results_->proxy_resolve_end_time_ = TimeTicks::Now(); 876 results_->proxy_resolve_end_time_ = TimeTicks::Now();
892 877
893 // Reset the state associated with in-progress-resolve. 878 // Reset the state associated with in-progress-resolve.
894 config_id_ = ProxyConfig::kInvalidConfigID; 879 config_id_ = ProxyConfig::kInvalidConfigID;
895 config_source_ = PROXY_CONFIG_SOURCE_UNKNOWN; 880 config_source_ = PROXY_CONFIG_SOURCE_UNKNOWN;
896 881
897 return rv; 882 return rv;
898 } 883 }
899 884
900 NetLogWithSource* net_log() { return &net_log_; } 885 NetLogWithSource* net_log() { return &net_log_; }
901 886
902 LoadState GetLoadState() const { 887 LoadState GetLoadState() const {
903 if (is_started()) 888 if (is_started())
904 return resolver()->GetLoadState(resolve_job_); 889 return resolve_job_->GetLoadState();
905 return LOAD_STATE_RESOLVING_PROXY_FOR_URL; 890 return LOAD_STATE_RESOLVING_PROXY_FOR_URL;
906 } 891 }
907 892
908 private: 893 private:
909 friend class base::RefCounted<ProxyService::PacRequest>; 894 friend class base::RefCounted<ProxyService::PacRequest>;
910 895
911 ~PacRequest() {} 896 ~PacRequest() {}
912 897
913 // Callback for when the ProxyResolver request has completed. 898 // Callback for when the ProxyResolver request has completed.
914 void QueryComplete(int result_code) { 899 void QueryComplete(int result_code) {
(...skipping 12 matching lines...) Expand all
927 912
928 // Note that we don't hold a reference to the ProxyService. Outstanding 913 // Note that we don't hold a reference to the ProxyService. Outstanding
929 // requests are cancelled during ~ProxyService, so this is guaranteed 914 // requests are cancelled during ~ProxyService, so this is guaranteed
930 // to be valid throughout our lifetime. 915 // to be valid throughout our lifetime.
931 ProxyService* service_; 916 ProxyService* service_;
932 CompletionCallback user_callback_; 917 CompletionCallback user_callback_;
933 ProxyInfo* results_; 918 ProxyInfo* results_;
934 GURL url_; 919 GURL url_;
935 std::string method_; 920 std::string method_;
936 ProxyDelegate* proxy_delegate_; 921 ProxyDelegate* proxy_delegate_;
937 ProxyResolver::RequestHandle resolve_job_; 922 std::unique_ptr<ProxyResolver::Request> resolve_job_;
938 ProxyConfig::ID config_id_; // The config id when the resolve was started. 923 ProxyConfig::ID config_id_; // The config id when the resolve was started.
939 ProxyConfigSource config_source_; // The source of proxy settings. 924 ProxyConfigSource config_source_; // The source of proxy settings.
940 NetLogWithSource net_log_; 925 NetLogWithSource net_log_;
941 // Time when the request was created. Stored here rather than in |results_| 926 // Time when the request was created. Stored here rather than in |results_|
942 // because the time in |results_| will be cleared. 927 // because the time in |results_| will be cleared.
943 TimeTicks creation_time_; 928 TimeTicks creation_time_;
944 }; 929 };
945 930
946 // ProxyService --------------------------------------------------------------- 931 // ProxyService ---------------------------------------------------------------
947 932
(...skipping 723 matching lines...) Expand 10 before | Expand all | Expand 10 after
1671 State previous_state = ResetProxyConfig(false); 1656 State previous_state = ResetProxyConfig(false);
1672 if (previous_state != STATE_NONE) 1657 if (previous_state != STATE_NONE)
1673 ApplyProxyConfigIfAvailable(); 1658 ApplyProxyConfigIfAvailable();
1674 } 1659 }
1675 1660
1676 void ProxyService::OnDNSChanged() { 1661 void ProxyService::OnDNSChanged() {
1677 OnIPAddressChanged(); 1662 OnIPAddressChanged();
1678 } 1663 }
1679 1664
1680 } // namespace net 1665 } // namespace net
OLDNEW
« no previous file with comments | « net/proxy/proxy_resolver_winhttp.cc ('k') | net/proxy/proxy_service_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698