#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"
};
// 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.
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);
}
}
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*;
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)
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_);
.SetMethod("resolveProxy", &Session::ResolveProxy)
.SetMethod("clearCache", &Session::ClearCache)
.SetMethod("clearStorageData", &Session::ClearStorageData)
+ .SetMethod("setProxy", &Session::SetProxy)
.SetProperty("cookies", &Session::Cookies);
}
* `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.
+```