session: api to set proxy
This commit is contained in:
parent
8cf9df2d8d
commit
d3055a5ca0
3 changed files with 62 additions and 5 deletions
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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_;
|
||||
|
|
|
@ -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.
|
||||
```
|
||||
|
|
Loading…
Reference in a new issue