2015-06-16 09:23:29 +00:00
|
|
|
// Copyright (c) 2015 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "atom/browser/api/atom_api_session.h"
|
|
|
|
|
2015-06-24 03:59:11 +00:00
|
|
|
#include <string>
|
|
|
|
|
2015-06-16 09:23:29 +00:00
|
|
|
#include "atom/browser/api/atom_api_cookies.h"
|
2015-06-23 15:40:41 +00:00
|
|
|
#include "atom/browser/atom_browser_context.h"
|
2015-06-24 03:59:11 +00:00
|
|
|
#include "atom/common/native_mate_converters/gurl_converter.h"
|
2015-06-16 09:23:29 +00:00
|
|
|
#include "native_mate/callback.h"
|
|
|
|
#include "native_mate/object_template_builder.h"
|
2015-06-24 03:59:11 +00:00
|
|
|
#include "net/base/load_flags.h"
|
|
|
|
#include "net/proxy/proxy_service.h"
|
|
|
|
#include "net/url_request/url_request_context.h"
|
|
|
|
#include "net/url_request/url_request_context_getter.h"
|
2015-06-16 09:23:29 +00:00
|
|
|
|
|
|
|
#include "atom/common/node_includes.h"
|
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
2015-06-24 03:59:11 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class ResolveProxyHelper {
|
|
|
|
public:
|
|
|
|
ResolveProxyHelper(AtomBrowserContext* browser_context,
|
|
|
|
const GURL& url,
|
|
|
|
Session::ResolveProxyCallback callback)
|
|
|
|
: callback_(callback) {
|
|
|
|
net::ProxyService* proxy_service = browser_context->
|
|
|
|
url_request_context_getter()->GetURLRequestContext()->proxy_service();
|
|
|
|
|
|
|
|
// Start the request.
|
|
|
|
int result = proxy_service->ResolveProxy(
|
|
|
|
url, net::LOAD_NORMAL, &proxy_info_,
|
|
|
|
base::Bind(&ResolveProxyHelper::OnResolveProxyCompleted,
|
|
|
|
base::Unretained(this)),
|
|
|
|
&pac_req_, nullptr, net::BoundNetLog());
|
|
|
|
|
|
|
|
// Completed synchronously.
|
|
|
|
if (result != net::ERR_IO_PENDING)
|
|
|
|
OnResolveProxyCompleted(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnResolveProxyCompleted(int result) {
|
|
|
|
std::string proxy;
|
|
|
|
if (result == net::OK)
|
|
|
|
proxy = proxy_info_.ToPacString();
|
|
|
|
callback_.Run(proxy);
|
|
|
|
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Session::ResolveProxyCallback callback_;
|
|
|
|
net::ProxyInfo proxy_info_;
|
|
|
|
net::ProxyService::PacRequest* pac_req_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(ResolveProxyHelper);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2015-06-23 15:40:41 +00:00
|
|
|
Session::Session(AtomBrowserContext* browser_context)
|
|
|
|
: browser_context_(browser_context) {
|
2015-06-24 07:37:04 +00:00
|
|
|
AttachAsUserData(browser_context);
|
2015-06-16 09:23:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Session::~Session() {
|
|
|
|
}
|
|
|
|
|
2015-06-24 03:59:11 +00:00
|
|
|
void Session::ResolveProxy(const GURL& url, ResolveProxyCallback callback) {
|
|
|
|
new ResolveProxyHelper(browser_context_, url, callback);
|
|
|
|
}
|
|
|
|
|
2015-06-16 09:23:29 +00:00
|
|
|
v8::Local<v8::Value> Session::Cookies(v8::Isolate* isolate) {
|
|
|
|
if (cookies_.IsEmpty()) {
|
2015-06-20 02:41:40 +00:00
|
|
|
auto handle = atom::api::Cookies::Create(isolate, browser_context_);
|
2015-06-16 09:23:29 +00:00
|
|
|
cookies_.Reset(isolate, handle.ToV8());
|
|
|
|
}
|
|
|
|
return v8::Local<v8::Value>::New(isolate, cookies_);
|
|
|
|
}
|
|
|
|
|
|
|
|
mate::ObjectTemplateBuilder Session::GetObjectTemplateBuilder(
|
|
|
|
v8::Isolate* isolate) {
|
|
|
|
return mate::ObjectTemplateBuilder(isolate)
|
2015-06-24 03:59:11 +00:00
|
|
|
.SetMethod("resolveProxy", &Session::ResolveProxy)
|
2015-06-16 09:23:29 +00:00
|
|
|
.SetProperty("cookies", &Session::Cookies);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2015-06-24 07:37:04 +00:00
|
|
|
mate::Handle<Session> Session::CreateFrom(
|
2015-06-20 02:41:40 +00:00
|
|
|
v8::Isolate* isolate,
|
2015-06-23 15:40:41 +00:00
|
|
|
AtomBrowserContext* browser_context) {
|
2015-06-24 07:37:04 +00:00
|
|
|
auto existing = TrackableObject::FromWrappedClass(isolate, browser_context);
|
|
|
|
if (existing)
|
|
|
|
return mate::CreateHandle(isolate, static_cast<Session*>(existing));
|
|
|
|
|
2015-06-23 02:18:43 +00:00
|
|
|
return mate::CreateHandle(isolate, new Session(browser_context));
|
2015-06-16 09:23:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace api
|
|
|
|
|
|
|
|
} // namespace atom
|