]> khamidou — GitHub mirrors - electron.git/commitdiff
session: api to set proxy
authorRobo <hop2deep@gmail.com>
Thu, 16 Jul 2015 14:36:48 +0000 (20:06 +0530)
committerRobo <hop2deep@gmail.com>
Mon, 20 Jul 2015 09:51:47 +0000 (15:21 +0530)
atom/browser/api/atom_api_session.cc
atom/browser/api/atom_api_session.h
docs/api/browser-window.md

index 93f8de52dd45bdaeee47abfa1bb84882b7612caf..483b730d1c708cad480f44f999b3f77dbc6bee56 100644 (file)
@@ -19,6 +19,7 @@
 #include "net/base/load_flags.h"
 #include "net/disk_cache/disk_cache.h"
 #include "net/proxy/proxy_service.h"
+#include "net/proxy/proxy_config_service_fixed.h"
 #include "net/url_request/url_request_context.h"
 #include "net/url_request/url_request_context_getter.h"
 
@@ -157,9 +158,10 @@ class ResolveProxyHelper {
 };
 
 // Runs the callback in UI thread.
-void RunCallbackInUI(const net::CompletionCallback& callback, int result) {
+template <typename ...T>
+void RunCallbackInUI(const base::Callback<void(T...)>& callback, T... result) {
   BrowserThread::PostTask(
-      BrowserThread::UI, FROM_HERE, base::Bind(callback, result));
+      BrowserThread::UI, FROM_HERE, base::Bind(callback, result...));
 }
 
 // Callback of HttpCache::GetBackend.
@@ -169,9 +171,9 @@ void OnGetBackend(disk_cache::Backend** backend_ptr,
   if (result != net::OK) {
     RunCallbackInUI(callback, result);
   } else if (backend_ptr && *backend_ptr) {
-    (*backend_ptr)->DoomAllEntries(base::Bind(&RunCallbackInUI, callback));
+    (*backend_ptr)->DoomAllEntries(base::Bind(&RunCallbackInUI<int>, callback));
   } else {
-    RunCallbackInUI(callback, net::ERR_FAILED);
+    RunCallbackInUI<int>(callback, net::ERR_FAILED);
   }
 }
 
@@ -181,7 +183,7 @@ void ClearHttpCacheInIO(content::BrowserContext* browser_context,
       browser_context->GetRequestContext()->GetURLRequestContext();
   auto http_cache = request_context->http_transaction_factory()->GetCache();
   if (!http_cache)
-    RunCallbackInUI(callback, net::ERR_FAILED);
+    RunCallbackInUI<int>(callback, net::ERR_FAILED);
 
   // Call GetBackend and make the backend's ptr accessable in OnGetBackend.
   using BackendPtr = disk_cache::Backend*;
@@ -193,6 +195,16 @@ void ClearHttpCacheInIO(content::BrowserContext* browser_context,
     on_get_backend.Run(net::OK);
 }
 
+void SetProxyInIO(net::URLRequestContextGetter* getter,
+                  const std::string& proxy,
+                  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));
+  RunCallbackInUI(callback);
+}
+
 }  // namespace
 
 Session::Session(AtomBrowserContext* browser_context)
@@ -232,6 +244,13 @@ void Session::ClearStorageData(mate::Arguments* args) {
       base::Time(), base::Time::Max(), callback);
 }
 
+void Session::SetProxy(const std::string& proxy,
+                       const base::Closure& callback) {
+  auto getter = browser_context_->GetRequestContext();
+  BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
+      base::Bind(&SetProxyInIO, base::Unretained(getter), proxy, callback));
+}
+
 v8::Local<v8::Value> Session::Cookies(v8::Isolate* isolate) {
   if (cookies_.IsEmpty()) {
     auto handle = atom::api::Cookies::Create(isolate, browser_context_);
@@ -246,6 +265,7 @@ mate::ObjectTemplateBuilder Session::GetObjectTemplateBuilder(
       .SetMethod("resolveProxy", &Session::ResolveProxy)
       .SetMethod("clearCache", &Session::ClearCache)
       .SetMethod("clearStorageData", &Session::ClearStorageData)
+      .SetMethod("setProxy", &Session::SetProxy)
       .SetProperty("cookies", &Session::Cookies);
 }
 
index 8e14c3a37834d00f5ae331c29cd879a19ab6d217..3963ed21189a9693145b58785b04beb60935c158 100644 (file)
@@ -45,6 +45,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);
   v8::Local<v8::Value> Cookies(v8::Isolate* isolate);
 
   v8::Global<v8::Value> cookies_;
index 48a118ffa3c274f5723b527f39969ca4900bab5f..ebd511fff0dbc0ca9176a9edb5f537d2676d6bd4 100644 (file)
@@ -1167,3 +1167,39 @@ Clears the session's HTTP cache.
 * `callback` Function - Called when operation is done
 
 Clears the data of web storages.
+
+### Session.setProxy(config, callback)
+
+* `config` String
+* `callback` Function - Called when operation is done
+
+Parses the `config` indicating which proxies to use for the session.
+
+```
+config = scheme-proxies[";"<scheme-proxies>]
+scheme-proxies = [<url-scheme>"="]<proxy-uri-list>
+url-scheme = "http" | "https" | "ftp" | "socks"
+proxy-uri-list = <proxy-uri>[","<proxy-uri-list>]
+proxy-uri = [<proxy-scheme>"://"]<proxy-host>[":"<proxy-port>]
+
+  For example:
+       "http=foopy:80;ftp=foopy2"  -- use HTTP proxy "foopy:80" for http://
+                                      URLs, and HTTP proxy "foopy2:80" for
+                                      ftp:// URLs.
+       "foopy:80"                  -- use HTTP proxy "foopy:80" for all URLs.
+       "foopy:80,bar,direct://"    -- use HTTP proxy "foopy:80" for all URLs,
+                                      failing over to "bar" if "foopy:80" is
+                                      unavailable, and after that using no
+                                      proxy.
+       "socks4://foopy"            -- use SOCKS v4 proxy "foopy:1080" for all
+                                      URLs.
+       "http=foopy,socks5://bar.com -- use HTTP proxy "foopy" for http URLs,
+                                      and fail over to the SOCKS5 proxy
+                                      "bar.com" if "foopy" is unavailable.
+       "http=foopy,direct://       -- use HTTP proxy "foopy" for http URLs,
+                                      and use no proxy if "foopy" is
+                                      unavailable.
+       "http=foopy;socks=foopy2   --  use HTTP proxy "foopy" for http URLs,
+                                      and use socks4://foopy2 for all other
+                                      URLs.
+```