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 3 // found in the LICENSE file. 4 5 #ifndef NET_PROXY_RESOLUTION_PROXY_CONFIG_SERVICE_ANDROID_H_ 6 #define NET_PROXY_RESOLUTION_PROXY_CONFIG_SERVICE_ANDROID_H_ 7 8 #include <string> 9 10 #include "base/android/jni_android.h" 11 #include "base/callback_forward.h" 12 #include "base/compiler_specific.h" 13 #include "base/macros.h" 14 #include "base/memory/ref_counted.h" 15 #include "net/base/net_export.h" 16 #include "net/proxy_resolution/proxy_config_service.h" 17 18 namespace base { 19 class SequencedTaskRunner; 20 } 21 22 namespace net { 23 24 class ProxyConfigWithAnnotation; 25 26 class NET_EXPORT ProxyConfigServiceAndroid : public ProxyConfigService { 27 public: 28 // Callback that returns the value of the property identified by the provided 29 // key. If it was not found, an empty string is returned. Note that this 30 // interface does not let you distinguish an empty property from a 31 // non-existing property. This callback is invoked on the JNI thread. 32 typedef base::RepeatingCallback<std::string(const std::string& property)> 33 GetPropertyCallback; 34 35 // Separate class whose instance is owned by the Delegate class implemented in 36 // the .cc file. 37 class JNIDelegate { 38 public: ~JNIDelegate()39 virtual ~JNIDelegate() {} 40 41 // Called from Java (on JNI thread) to signal that the proxy settings have 42 // changed. The string and int arguments (the host/port pair for the proxy) 43 // are either a host/port pair or ("", 0) to indicate "no proxy". 44 // The third argument indicates the PAC url. 45 // The fourth argument is the proxy exclusion list. 46 virtual void ProxySettingsChangedTo( 47 JNIEnv*, 48 const base::android::JavaParamRef<jobject>&, 49 const base::android::JavaParamRef<jstring>&, 50 jint, 51 const base::android::JavaParamRef<jstring>&, 52 const base::android::JavaParamRef<jobjectArray>&) = 0; 53 54 // Called from Java (on JNI thread) to signal that the proxy settings have 55 // changed. New proxy settings are fetched from the system property store. 56 virtual void ProxySettingsChanged( 57 JNIEnv*, 58 const base::android::JavaParamRef<jobject>&) = 0; 59 }; 60 61 ProxyConfigServiceAndroid( 62 const scoped_refptr<base::SequencedTaskRunner>& main_task_runner, 63 const scoped_refptr<base::SequencedTaskRunner>& jni_task_runner); 64 65 ~ProxyConfigServiceAndroid() override; 66 67 // Android provides a local HTTP proxy that does PAC resolution. When this 68 // setting is enabled, the proxy config service ignores the PAC URL and uses 69 // the local proxy for all proxy resolution. 70 void set_exclude_pac_url(bool enabled); 71 72 // ProxyConfigService: 73 // Called only on the network thread. 74 void AddObserver(Observer* observer) override; 75 void RemoveObserver(Observer* observer) override; 76 ConfigAvailability GetLatestProxyConfig( 77 ProxyConfigWithAnnotation* config) override; 78 79 // Holds a single proxy_url and the scheme for which it should be used. 80 // If url_scheme is "*", this proxy will be used for all schemes. 81 // The proxy_url is a string in the format: [scheme://] host [:port] for 82 // a proxy or "direct://" for direct. Scheme is optional and will default to 83 // HTTP; port is optional and defaults to 80 for HTTP, 443 for HTTPS and QUIC, 84 // and 1080 for SOCKS. Host must be one of: 85 // - IPv6 literal with brackets 86 // - IPv4 literal 87 // - A label or labels separated by periods 88 struct ProxyOverrideRule { ProxyOverrideRuleProxyOverrideRule89 ProxyOverrideRule(const std::string& url_scheme, 90 const std::string& proxy_url) 91 : url_scheme(url_scheme), proxy_url(proxy_url) {} 92 93 std::string url_scheme; 94 std::string proxy_url; 95 }; 96 97 // Receives a list of ProxyOverrideRule and a list of strings for bypass 98 // rules. Wildcards are accepted. URLs that match any pattern in any bypass 99 // rule will go to DIRECT. "<local>" and "<-loopback>" are also accepted. 100 // If no errors were found, returns an empty string, otherwise an UTF-8 string 101 // with a description of the error that was found. 102 // Callback is called (asynchronously) if input was valid. 103 std::string SetProxyOverride( 104 const std::vector<ProxyOverrideRule>& proxy_rules, 105 const std::vector<std::string>& bypass_rules, 106 base::OnceClosure callback); 107 void ClearProxyOverride(base::OnceClosure callback); 108 109 private: 110 friend class ProxyConfigServiceAndroidTestBase; 111 class Delegate; 112 113 // For tests. 114 ProxyConfigServiceAndroid( 115 const scoped_refptr<base::SequencedTaskRunner>& main_task_runner, 116 const scoped_refptr<base::SequencedTaskRunner>& jni_task_runner, 117 GetPropertyCallback get_property_callback); 118 119 // For tests. 120 void ProxySettingsChanged(); 121 122 // For tests. 123 void ProxySettingsChangedTo(const std::string& host, 124 int port, 125 const std::string& pac_url, 126 const std::vector<std::string>& exclusion_list); 127 128 scoped_refptr<Delegate> delegate_; 129 130 DISALLOW_COPY_AND_ASSIGN(ProxyConfigServiceAndroid); 131 }; 132 133 } // namespace net 134 135 #endif // NET_PROXY_RESOLUTION_PROXY_CONFIG_SERVICE_ANDROID_H_ 136