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