OLD | NEW |
---|---|
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 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
178 | 178 |
179 // Proxy resolver that fails every time. | 179 // Proxy resolver that fails every time. |
180 class ProxyResolverNull : public ProxyResolver { | 180 class ProxyResolverNull : public ProxyResolver { |
181 public: | 181 public: |
182 ProxyResolverNull() {} | 182 ProxyResolverNull() {} |
183 | 183 |
184 // ProxyResolver implementation. | 184 // ProxyResolver implementation. |
185 int GetProxyForURL(const GURL& url, | 185 int GetProxyForURL(const GURL& url, |
186 ProxyInfo* results, | 186 ProxyInfo* results, |
187 const CompletionCallback& callback, | 187 const CompletionCallback& callback, |
188 RequestHandle* request, | 188 scoped_ptr<Request>* request, |
189 const BoundNetLog& net_log) override { | 189 const BoundNetLog& net_log) override { |
190 return ERR_NOT_IMPLEMENTED; | 190 return ERR_NOT_IMPLEMENTED; |
191 } | 191 } |
192 | |
193 void CancelRequest(RequestHandle request) override { NOTREACHED(); } | |
194 | |
195 LoadState GetLoadState(RequestHandle request) const override { | |
196 NOTREACHED(); | |
197 return LOAD_STATE_IDLE; | |
198 } | |
199 | |
200 }; | 192 }; |
201 | 193 |
202 // ProxyResolver that simulates a PAC script which returns | 194 // ProxyResolver that simulates a PAC script which returns |
203 // |pac_string| for every single URL. | 195 // |pac_string| for every single URL. |
204 class ProxyResolverFromPacString : public ProxyResolver { | 196 class ProxyResolverFromPacString : public ProxyResolver { |
205 public: | 197 public: |
206 explicit ProxyResolverFromPacString(const std::string& pac_string) | 198 explicit ProxyResolverFromPacString(const std::string& pac_string) |
207 : pac_string_(pac_string) {} | 199 : pac_string_(pac_string) {} |
208 | 200 |
209 int GetProxyForURL(const GURL& url, | 201 int GetProxyForURL(const GURL& url, |
210 ProxyInfo* results, | 202 ProxyInfo* results, |
211 const CompletionCallback& callback, | 203 const CompletionCallback& callback, |
212 RequestHandle* request, | 204 scoped_ptr<Request>* request, |
213 const BoundNetLog& net_log) override { | 205 const BoundNetLog& net_log) override { |
214 results->UsePacString(pac_string_); | 206 results->UsePacString(pac_string_); |
215 return OK; | 207 return OK; |
216 } | 208 } |
217 | 209 |
218 void CancelRequest(RequestHandle request) override { NOTREACHED(); } | |
219 | |
220 LoadState GetLoadState(RequestHandle request) const override { | |
221 NOTREACHED(); | |
222 return LOAD_STATE_IDLE; | |
223 } | |
224 | |
225 private: | 210 private: |
226 const std::string pac_string_; | 211 const std::string pac_string_; |
227 }; | 212 }; |
228 | 213 |
229 // Creates ProxyResolvers using a platform-specific implementation. | 214 // Creates ProxyResolvers using a platform-specific implementation. |
230 class ProxyResolverFactoryForSystem : public MultiThreadedProxyResolverFactory { | 215 class ProxyResolverFactoryForSystem : public MultiThreadedProxyResolverFactory { |
231 public: | 216 public: |
232 explicit ProxyResolverFactoryForSystem(size_t max_num_threads) | 217 explicit ProxyResolverFactoryForSystem(size_t max_num_threads) |
233 : MultiThreadedProxyResolverFactory(max_num_threads, | 218 : MultiThreadedProxyResolverFactory(max_num_threads, |
234 false /*expects_pac_bytes*/) {} | 219 false /*expects_pac_bytes*/) {} |
(...skipping 536 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
771 ProxyDelegate* proxy_delegate, | 756 ProxyDelegate* proxy_delegate, |
772 ProxyInfo* results, | 757 ProxyInfo* results, |
773 const CompletionCallback& user_callback, | 758 const CompletionCallback& user_callback, |
774 const BoundNetLog& net_log) | 759 const BoundNetLog& net_log) |
775 : service_(service), | 760 : service_(service), |
776 user_callback_(user_callback), | 761 user_callback_(user_callback), |
777 results_(results), | 762 results_(results), |
778 url_(url), | 763 url_(url), |
779 load_flags_(load_flags), | 764 load_flags_(load_flags), |
780 proxy_delegate_(proxy_delegate), | 765 proxy_delegate_(proxy_delegate), |
781 resolve_job_(NULL), | |
782 config_id_(ProxyConfig::kInvalidConfigID), | 766 config_id_(ProxyConfig::kInvalidConfigID), |
783 config_source_(PROXY_CONFIG_SOURCE_UNKNOWN), | 767 config_source_(PROXY_CONFIG_SOURCE_UNKNOWN), |
784 net_log_(net_log), | 768 net_log_(net_log), |
785 creation_time_(TimeTicks::Now()) { | 769 creation_time_(TimeTicks::Now()) { |
786 DCHECK(!user_callback.is_null()); | 770 DCHECK(!user_callback.is_null()); |
787 } | 771 } |
788 | 772 |
789 // Starts the resolve proxy request. | 773 // Starts the resolve proxy request. |
790 int Start() { | 774 int Start() { |
791 DCHECK(!was_cancelled()); | 775 DCHECK(!was_cancelled()); |
792 DCHECK(!is_started()); | 776 DCHECK(!is_started()); |
793 | 777 |
794 DCHECK(service_->config_.is_valid()); | 778 DCHECK(service_->config_.is_valid()); |
795 | 779 |
796 config_id_ = service_->config_.id(); | 780 config_id_ = service_->config_.id(); |
797 config_source_ = service_->config_.source(); | 781 config_source_ = service_->config_.source(); |
798 | 782 |
799 return resolver()->GetProxyForURL( | 783 return resolver()->GetProxyForURL( |
800 url_, results_, | 784 url_, results_, |
801 base::Bind(&PacRequest::QueryComplete, base::Unretained(this)), | 785 base::Bind(&PacRequest::QueryComplete, base::Unretained(this)), |
802 &resolve_job_, net_log_); | 786 &resolve_job_, net_log_); |
803 } | 787 } |
804 | 788 |
805 bool is_started() const { | 789 bool is_started() const { |
806 // Note that !! casts to bool. (VS gives a warning otherwise). | 790 // Note that !! casts to bool. (VS gives a warning otherwise). |
807 return !!resolve_job_; | 791 return !!resolve_job_.get(); |
808 } | 792 } |
809 | 793 |
810 void StartAndCompleteCheckingForSynchronous() { | 794 void StartAndCompleteCheckingForSynchronous() { |
811 int rv = service_->TryToCompleteSynchronously(url_, load_flags_, | 795 int rv = service_->TryToCompleteSynchronously(url_, load_flags_, |
812 proxy_delegate_, results_); | 796 proxy_delegate_, results_); |
813 if (rv == ERR_IO_PENDING) | 797 if (rv == ERR_IO_PENDING) |
814 rv = Start(); | 798 rv = Start(); |
815 if (rv != ERR_IO_PENDING) | 799 if (rv != ERR_IO_PENDING) |
816 QueryComplete(rv); | 800 QueryComplete(rv); |
817 } | 801 } |
818 | 802 |
819 void CancelResolveJob() { | 803 void CancelResolveJob() { |
820 DCHECK(is_started()); | 804 DCHECK(is_started()); |
821 // The request may already be running in the resolver. | 805 // The request may already be running in the resolver. |
822 resolver()->CancelRequest(resolve_job_); | 806 resolve_job_.reset(); |
823 resolve_job_ = NULL; | |
824 DCHECK(!is_started()); | 807 DCHECK(!is_started()); |
825 } | 808 } |
826 | 809 |
827 void Cancel() { | 810 void Cancel() { |
828 net_log_.AddEvent(NetLog::TYPE_CANCELLED); | 811 net_log_.AddEvent(NetLog::TYPE_CANCELLED); |
829 | 812 |
830 if (is_started()) | 813 if (is_started()) |
831 CancelResolveJob(); | 814 CancelResolveJob(); |
832 | 815 |
833 // Mark as cancelled, to prevent accessing this again later. | 816 // Mark as cancelled, to prevent accessing this again later. |
834 service_ = NULL; | 817 service_ = NULL; |
835 user_callback_.Reset(); | 818 user_callback_.Reset(); |
836 results_ = NULL; | 819 results_ = NULL; |
837 | 820 |
838 net_log_.EndEvent(NetLog::TYPE_PROXY_SERVICE); | 821 net_log_.EndEvent(NetLog::TYPE_PROXY_SERVICE); |
839 } | 822 } |
840 | 823 |
841 // Returns true if Cancel() has been called. | 824 // Returns true if Cancel() has been called. |
842 bool was_cancelled() const { | 825 bool was_cancelled() const { |
843 return user_callback_.is_null(); | 826 return user_callback_.is_null(); |
844 } | 827 } |
845 | 828 |
846 // Helper to call after ProxyResolver completion (both synchronous and | 829 // Helper to call after ProxyResolver completion (both synchronous and |
847 // asynchronous). Fixes up the result that is to be returned to user. | 830 // asynchronous). Fixes up the result that is to be returned to user. |
848 int QueryDidComplete(int result_code) { | 831 int QueryDidComplete(int result_code) { |
849 DCHECK(!was_cancelled()); | 832 DCHECK(!was_cancelled()); |
850 | 833 |
851 // This state is cleared when resolve_job_ is set to nullptr below. | 834 // This state is cleared when resolve_job_ is set to nullptr below. |
eroman
2016/02/25 17:53:44
nit: "is set to nullptr below." --> "is reset belo
| |
852 bool script_executed = is_started(); | 835 bool script_executed = is_started(); |
853 | 836 |
854 // Clear |resolve_job_| so is_started() returns false while | 837 // Clear |resolve_job_| so is_started() returns false while |
855 // DidFinishResolvingProxy() runs. | 838 // DidFinishResolvingProxy() runs. |
856 resolve_job_ = nullptr; | 839 resolve_job_.reset(); |
857 | 840 |
858 // Note that DidFinishResolvingProxy might modify |results_|. | 841 // Note that DidFinishResolvingProxy might modify |results_|. |
859 int rv = service_->DidFinishResolvingProxy( | 842 int rv = service_->DidFinishResolvingProxy( |
860 url_, load_flags_, proxy_delegate_, results_, result_code, net_log_, | 843 url_, load_flags_, proxy_delegate_, results_, result_code, net_log_, |
861 creation_time_, script_executed); | 844 creation_time_, script_executed); |
862 | 845 |
863 // Make a note in the results which configuration was in use at the | 846 // Make a note in the results which configuration was in use at the |
864 // time of the resolve. | 847 // time of the resolve. |
865 results_->config_id_ = config_id_; | 848 results_->config_id_ = config_id_; |
866 results_->config_source_ = config_source_; | 849 results_->config_source_ = config_source_; |
867 results_->did_use_pac_script_ = true; | 850 results_->did_use_pac_script_ = true; |
868 results_->proxy_resolve_start_time_ = creation_time_; | 851 results_->proxy_resolve_start_time_ = creation_time_; |
869 results_->proxy_resolve_end_time_ = TimeTicks::Now(); | 852 results_->proxy_resolve_end_time_ = TimeTicks::Now(); |
870 | 853 |
871 // Reset the state associated with in-progress-resolve. | 854 // Reset the state associated with in-progress-resolve. |
872 config_id_ = ProxyConfig::kInvalidConfigID; | 855 config_id_ = ProxyConfig::kInvalidConfigID; |
873 config_source_ = PROXY_CONFIG_SOURCE_UNKNOWN; | 856 config_source_ = PROXY_CONFIG_SOURCE_UNKNOWN; |
874 | 857 |
875 return rv; | 858 return rv; |
876 } | 859 } |
877 | 860 |
878 BoundNetLog* net_log() { return &net_log_; } | 861 BoundNetLog* net_log() { return &net_log_; } |
879 | 862 |
880 LoadState GetLoadState() const { | 863 LoadState GetLoadState() const { |
881 if (is_started()) | 864 if (is_started()) |
882 return resolver()->GetLoadState(resolve_job_); | 865 return resolve_job_->GetLoadState(); |
883 return LOAD_STATE_RESOLVING_PROXY_FOR_URL; | 866 return LOAD_STATE_RESOLVING_PROXY_FOR_URL; |
884 } | 867 } |
885 | 868 |
886 private: | 869 private: |
887 friend class base::RefCounted<ProxyService::PacRequest>; | 870 friend class base::RefCounted<ProxyService::PacRequest>; |
888 | 871 |
889 ~PacRequest() {} | 872 ~PacRequest() {} |
890 | 873 |
891 // Callback for when the ProxyResolver request has completed. | 874 // Callback for when the ProxyResolver request has completed. |
892 void QueryComplete(int result_code) { | 875 void QueryComplete(int result_code) { |
(...skipping 12 matching lines...) Expand all Loading... | |
905 | 888 |
906 // Note that we don't hold a reference to the ProxyService. Outstanding | 889 // Note that we don't hold a reference to the ProxyService. Outstanding |
907 // requests are cancelled during ~ProxyService, so this is guaranteed | 890 // requests are cancelled during ~ProxyService, so this is guaranteed |
908 // to be valid throughout our lifetime. | 891 // to be valid throughout our lifetime. |
909 ProxyService* service_; | 892 ProxyService* service_; |
910 CompletionCallback user_callback_; | 893 CompletionCallback user_callback_; |
911 ProxyInfo* results_; | 894 ProxyInfo* results_; |
912 GURL url_; | 895 GURL url_; |
913 int load_flags_; | 896 int load_flags_; |
914 ProxyDelegate* proxy_delegate_; | 897 ProxyDelegate* proxy_delegate_; |
915 ProxyResolver::RequestHandle resolve_job_; | 898 scoped_ptr<ProxyResolver::Request> resolve_job_; |
916 ProxyConfig::ID config_id_; // The config id when the resolve was started. | 899 ProxyConfig::ID config_id_; // The config id when the resolve was started. |
917 ProxyConfigSource config_source_; // The source of proxy settings. | 900 ProxyConfigSource config_source_; // The source of proxy settings. |
918 BoundNetLog net_log_; | 901 BoundNetLog net_log_; |
919 // Time when the request was created. Stored here rather than in |results_| | 902 // Time when the request was created. Stored here rather than in |results_| |
920 // because the time in |results_| will be cleared. | 903 // because the time in |results_| will be cleared. |
921 TimeTicks creation_time_; | 904 TimeTicks creation_time_; |
922 }; | 905 }; |
923 | 906 |
924 // ProxyService --------------------------------------------------------------- | 907 // ProxyService --------------------------------------------------------------- |
925 | 908 |
(...skipping 713 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1639 State previous_state = ResetProxyConfig(false); | 1622 State previous_state = ResetProxyConfig(false); |
1640 if (previous_state != STATE_NONE) | 1623 if (previous_state != STATE_NONE) |
1641 ApplyProxyConfigIfAvailable(); | 1624 ApplyProxyConfigIfAvailable(); |
1642 } | 1625 } |
1643 | 1626 |
1644 void ProxyService::OnDNSChanged() { | 1627 void ProxyService::OnDNSChanged() { |
1645 OnIPAddressChanged(); | 1628 OnIPAddressChanged(); |
1646 } | 1629 } |
1647 | 1630 |
1648 } // namespace net | 1631 } // namespace net |
OLD | NEW |