From d3055a5ca09fba35b9960573ae8a64fe217e9512 Mon Sep 17 00:00:00 2001 From: Robo Date: Thu, 16 Jul 2015 20:06:48 +0530 Subject: [PATCH] session: api to set proxy --- atom/browser/api/atom_api_session.cc | 30 +++++++++++++++++++---- atom/browser/api/atom_api_session.h | 1 + docs/api/browser-window.md | 36 ++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 5 deletions(-) diff --git a/atom/browser/api/atom_api_session.cc b/atom/browser/api/atom_api_session.cc index 93f8de52dd4..483b730d1c7 100644 --- a/atom/browser/api/atom_api_session.cc +++ b/atom/browser/api/atom_api_session.cc @@ -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 +void RunCallbackInUI(const base::Callback& 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, callback)); } else { - RunCallbackInUI(callback, net::ERR_FAILED); + RunCallbackInUI(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(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 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); } diff --git a/atom/browser/api/atom_api_session.h b/atom/browser/api/atom_api_session.h index 8e14c3a3783..3963ed21189 100644 --- a/atom/browser/api/atom_api_session.h +++ b/atom/browser/api/atom_api_session.h @@ -45,6 +45,7 @@ class Session: public mate::TrackableObject { 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 Cookies(v8::Isolate* isolate); v8::Global cookies_; diff --git a/docs/api/browser-window.md b/docs/api/browser-window.md index 48a118ffa3c..ebd511fff0d 100644 --- a/docs/api/browser-window.md +++ b/docs/api/browser-window.md @@ -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 = ["="] +url-scheme = "http" | "https" | "ftp" | "socks" +proxy-uri-list = [","] +proxy-uri = ["://"][":"] + + 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. +```