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