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