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