]> khamidou — GitHub mirrors - electron.git/commitdiff
session: allow setproxy to use external pac script
authorRobo <hop2deep@gmail.com>
Thu, 22 Oct 2015 17:26:36 +0000 (22:56 +0530)
committerRobo <hop2deep@gmail.com>
Thu, 22 Oct 2015 17:33:47 +0000 (23:03 +0530)
atom/browser/api/atom_api_session.cc
atom/browser/api/atom_api_session.h
docs/api/session.md

index 3c92266be33cb02d6f397cd6c753de590ee2dcf0..5bb96710f22697b95202a089eb43ac8e972c67c3 100644 (file)
@@ -105,6 +105,24 @@ struct Converter<ClearStorageDataOptions> {
   }
 };
 
+template<>
+struct Converter<net::ProxyConfig> {
+  static bool FromV8(v8::Isolate* isolate,
+                     v8::Local<v8::Value> val,
+                     net::ProxyConfig* out) {
+    std::string proxy;
+    if (!ConvertFromV8(isolate, val, &proxy))
+      return false;
+    auto pac_url = GURL(proxy);
+    if (pac_url.is_valid()) {
+      out->set_pac_url(pac_url);
+    } else {
+      out->proxy_rules().ParseFromString(proxy);
+    }
+    return true;
+  }
+};
+
 }  // namespace mate
 
 namespace atom {
@@ -209,12 +227,12 @@ void ClearHttpCacheInIO(
 }
 
 void SetProxyInIO(net::URLRequestContextGetter* getter,
-                  const std::string& proxy,
+                  const net::ProxyConfig& config,
                   const base::Closure& callback) {
-  net::ProxyConfig config;
-  config.proxy_rules().ParseFromString(proxy);
   auto proxy_service = getter->GetURLRequestContext()->proxy_service();
   proxy_service->ResetConfigService(new net::ProxyConfigServiceFixed(config));
+  // Refetches and applies the new pac script if provided.
+  proxy_service->ForceReloadProxyConfig();
   RunCallbackInUI(callback);
 }
 
@@ -287,11 +305,11 @@ void Session::ClearStorageData(mate::Arguments* args) {
       base::Time(), base::Time::Max(), callback);
 }
 
-void Session::SetProxy(const std::string& proxy,
+void Session::SetProxy(const net::ProxyConfig& config,
                        const base::Closure& callback) {
   auto getter = browser_context_->GetRequestContext();
   BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
-      base::Bind(&SetProxyInIO, base::Unretained(getter), proxy, callback));
+      base::Bind(&SetProxyInIO, base::Unretained(getter), config, callback));
 }
 
 void Session::SetDownloadPath(const base::FilePath& path) {
index 68ee3634e6c4f3dbb400c5324baa366b315f509f..39712f6c8486d495fe2a13d9456f3d6ea89c1ef2 100644 (file)
@@ -23,6 +23,10 @@ class Arguments;
 class Dictionary;
 }
 
+namespace net {
+class ProxyConfig;
+}
+
 namespace atom {
 
 class AtomBrowserContext;
@@ -64,7 +68,7 @@ class Session: public mate::TrackableObject<Session>,
   void ResolveProxy(const GURL& url, ResolveProxyCallback callback);
   void ClearCache(const net::CompletionCallback& callback);
   void ClearStorageData(mate::Arguments* args);
-  void SetProxy(const std::string& proxy, const base::Closure& callback);
+  void SetProxy(const net::ProxyConfig& config, const base::Closure& callback);
   void SetDownloadPath(const base::FilePath& path);
   void EnableNetworkEmulation(const mate::Dictionary& options);
   void DisableNetworkEmulation();
index 392b6826414d73e15b2d54b2781d07b13124af5c..25db92b73b25e7d9f7269c53251701d7202e199d 100644 (file)
@@ -154,7 +154,9 @@ Clears the data of web storages.
 * `config` String
 * `callback` Function - Called when operation is done.
 
-Parses the `config` indicating which proxies to use for the session.
+If `config` is a PAC url, it is used directly otherwise
+`config` is parsed based on the following rules indicating which
+proxies to use for the session.
 
 ```
 config = scheme-proxies[";"<scheme-proxies>]