Merge pull request #2043 from atom/default-session
Add app.defaultSession
This commit is contained in:
commit
b8cf9a2788
6 changed files with 96 additions and 71 deletions
|
@ -12,11 +12,11 @@
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "atom/browser/api/atom_api_menu.h"
|
#include "atom/browser/api/atom_api_menu.h"
|
||||||
|
#include "atom/browser/api/atom_api_session.h"
|
||||||
#include "atom/browser/atom_browser_context.h"
|
#include "atom/browser/atom_browser_context.h"
|
||||||
#include "atom/browser/atom_browser_main_parts.h"
|
#include "atom/browser/atom_browser_main_parts.h"
|
||||||
#include "atom/browser/browser.h"
|
#include "atom/browser/browser.h"
|
||||||
#include "atom/common/native_mate_converters/file_path_converter.h"
|
#include "atom/common/native_mate_converters/file_path_converter.h"
|
||||||
#include "atom/common/native_mate_converters/gurl_converter.h"
|
|
||||||
#include "base/command_line.h"
|
#include "base/command_line.h"
|
||||||
#include "base/environment.h"
|
#include "base/environment.h"
|
||||||
#include "base/files/file_path.h"
|
#include "base/files/file_path.h"
|
||||||
|
@ -25,10 +25,6 @@
|
||||||
#include "native_mate/callback.h"
|
#include "native_mate/callback.h"
|
||||||
#include "native_mate/dictionary.h"
|
#include "native_mate/dictionary.h"
|
||||||
#include "native_mate/object_template_builder.h"
|
#include "native_mate/object_template_builder.h"
|
||||||
#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"
|
|
||||||
|
|
||||||
#if defined(OS_WIN)
|
#if defined(OS_WIN)
|
||||||
#include "base/strings/utf_string_conversions.h"
|
#include "base/strings/utf_string_conversions.h"
|
||||||
|
@ -94,43 +90,6 @@ int GetPathConstant(const std::string& name) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
class ResolveProxyHelper {
|
|
||||||
public:
|
|
||||||
ResolveProxyHelper(const GURL& url, App::ResolveProxyCallback callback)
|
|
||||||
: callback_(callback) {
|
|
||||||
auto browser_context = AtomBrowserMainParts::Get()->browser_context();
|
|
||||||
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:
|
|
||||||
App::ResolveProxyCallback callback_;
|
|
||||||
net::ProxyInfo proxy_info_;
|
|
||||||
net::ProxyService::PacRequest* pac_req_;
|
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(ResolveProxyHelper);
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
App::App() {
|
App::App() {
|
||||||
|
@ -199,10 +158,6 @@ void App::SetPath(mate::Arguments* args,
|
||||||
args->ThrowError("Failed to set path");
|
args->ThrowError("Failed to set path");
|
||||||
}
|
}
|
||||||
|
|
||||||
void App::ResolveProxy(const GURL& url, ResolveProxyCallback callback) {
|
|
||||||
new ResolveProxyHelper(url, callback);
|
|
||||||
}
|
|
||||||
|
|
||||||
void App::SetDesktopName(const std::string& desktop_name) {
|
void App::SetDesktopName(const std::string& desktop_name) {
|
||||||
#if defined(OS_LINUX)
|
#if defined(OS_LINUX)
|
||||||
scoped_ptr<base::Environment> env(base::Environment::Create());
|
scoped_ptr<base::Environment> env(base::Environment::Create());
|
||||||
|
@ -217,6 +172,16 @@ void App::SetAppUserModelId(const std::string& app_id) {
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
v8::Local<v8::Value> App::DefaultSession(v8::Isolate* isolate) {
|
||||||
|
if (default_session_.IsEmpty()) {
|
||||||
|
auto browser_context = static_cast<AtomBrowserContext*>(
|
||||||
|
AtomBrowserMainParts::Get()->browser_context());
|
||||||
|
auto handle = Session::Create(isolate, browser_context);
|
||||||
|
default_session_.Reset(isolate, handle.ToV8());
|
||||||
|
}
|
||||||
|
return v8::Local<v8::Value>::New(isolate, default_session_);
|
||||||
|
}
|
||||||
|
|
||||||
mate::ObjectTemplateBuilder App::GetObjectTemplateBuilder(
|
mate::ObjectTemplateBuilder App::GetObjectTemplateBuilder(
|
||||||
v8::Isolate* isolate) {
|
v8::Isolate* isolate) {
|
||||||
auto browser = base::Unretained(Browser::Get());
|
auto browser = base::Unretained(Browser::Get());
|
||||||
|
@ -238,9 +203,9 @@ mate::ObjectTemplateBuilder App::GetObjectTemplateBuilder(
|
||||||
#endif
|
#endif
|
||||||
.SetMethod("setPath", &App::SetPath)
|
.SetMethod("setPath", &App::SetPath)
|
||||||
.SetMethod("getPath", &App::GetPath)
|
.SetMethod("getPath", &App::GetPath)
|
||||||
.SetMethod("resolveProxy", &App::ResolveProxy)
|
|
||||||
.SetMethod("setDesktopName", &App::SetDesktopName)
|
.SetMethod("setDesktopName", &App::SetDesktopName)
|
||||||
.SetMethod("setAppUserModelId", &App::SetAppUserModelId);
|
.SetMethod("setAppUserModelId", &App::SetAppUserModelId)
|
||||||
|
.SetProperty("defaultSession", &App::DefaultSession);
|
||||||
}
|
}
|
||||||
|
|
||||||
// static
|
// static
|
||||||
|
|
|
@ -9,11 +9,8 @@
|
||||||
|
|
||||||
#include "atom/browser/api/event_emitter.h"
|
#include "atom/browser/api/event_emitter.h"
|
||||||
#include "atom/browser/browser_observer.h"
|
#include "atom/browser/browser_observer.h"
|
||||||
#include "base/callback.h"
|
|
||||||
#include "native_mate/handle.h"
|
#include "native_mate/handle.h"
|
||||||
|
|
||||||
class GURL;
|
|
||||||
|
|
||||||
namespace base {
|
namespace base {
|
||||||
class FilePath;
|
class FilePath;
|
||||||
}
|
}
|
||||||
|
@ -29,8 +26,6 @@ namespace api {
|
||||||
class App : public mate::EventEmitter,
|
class App : public mate::EventEmitter,
|
||||||
public BrowserObserver {
|
public BrowserObserver {
|
||||||
public:
|
public:
|
||||||
typedef base::Callback<void(std::string)> ResolveProxyCallback;
|
|
||||||
|
|
||||||
static mate::Handle<App> Create(v8::Isolate* isolate);
|
static mate::Handle<App> Create(v8::Isolate* isolate);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
@ -59,9 +54,11 @@ class App : public mate::EventEmitter,
|
||||||
const std::string& name,
|
const std::string& name,
|
||||||
const base::FilePath& path);
|
const base::FilePath& path);
|
||||||
|
|
||||||
void ResolveProxy(const GURL& url, ResolveProxyCallback callback);
|
|
||||||
void SetDesktopName(const std::string& desktop_name);
|
void SetDesktopName(const std::string& desktop_name);
|
||||||
void SetAppUserModelId(const std::string& app_id);
|
void SetAppUserModelId(const std::string& app_id);
|
||||||
|
v8::Local<v8::Value> DefaultSession(v8::Isolate* isolate);
|
||||||
|
|
||||||
|
v8::Global<v8::Value> default_session_;
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(App);
|
DISALLOW_COPY_AND_ASSIGN(App);
|
||||||
};
|
};
|
||||||
|
|
|
@ -4,11 +4,17 @@
|
||||||
|
|
||||||
#include "atom/browser/api/atom_api_session.h"
|
#include "atom/browser/api/atom_api_session.h"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include "atom/browser/api/atom_api_cookies.h"
|
#include "atom/browser/api/atom_api_cookies.h"
|
||||||
#include "content/public/browser/browser_context.h"
|
#include "atom/browser/atom_browser_context.h"
|
||||||
|
#include "atom/common/native_mate_converters/gurl_converter.h"
|
||||||
#include "native_mate/callback.h"
|
#include "native_mate/callback.h"
|
||||||
#include "native_mate/dictionary.h"
|
|
||||||
#include "native_mate/object_template_builder.h"
|
#include "native_mate/object_template_builder.h"
|
||||||
|
#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"
|
||||||
|
|
||||||
#include "atom/common/node_includes.h"
|
#include "atom/common/node_includes.h"
|
||||||
|
|
||||||
|
@ -16,13 +22,61 @@ namespace atom {
|
||||||
|
|
||||||
namespace api {
|
namespace api {
|
||||||
|
|
||||||
Session::Session(content::BrowserContext* browser_context):
|
namespace {
|
||||||
browser_context_(browser_context) {
|
|
||||||
|
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
|
||||||
|
|
||||||
|
Session::Session(AtomBrowserContext* browser_context)
|
||||||
|
: browser_context_(browser_context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Session::~Session() {
|
Session::~Session() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Session::ResolveProxy(const GURL& url, ResolveProxyCallback callback) {
|
||||||
|
new ResolveProxyHelper(browser_context_, url, callback);
|
||||||
|
}
|
||||||
|
|
||||||
v8::Local<v8::Value> Session::Cookies(v8::Isolate* isolate) {
|
v8::Local<v8::Value> Session::Cookies(v8::Isolate* isolate) {
|
||||||
if (cookies_.IsEmpty()) {
|
if (cookies_.IsEmpty()) {
|
||||||
auto handle = atom::api::Cookies::Create(isolate, browser_context_);
|
auto handle = atom::api::Cookies::Create(isolate, browser_context_);
|
||||||
|
@ -34,13 +88,14 @@ v8::Local<v8::Value> Session::Cookies(v8::Isolate* isolate) {
|
||||||
mate::ObjectTemplateBuilder Session::GetObjectTemplateBuilder(
|
mate::ObjectTemplateBuilder Session::GetObjectTemplateBuilder(
|
||||||
v8::Isolate* isolate) {
|
v8::Isolate* isolate) {
|
||||||
return mate::ObjectTemplateBuilder(isolate)
|
return mate::ObjectTemplateBuilder(isolate)
|
||||||
|
.SetMethod("resolveProxy", &Session::ResolveProxy)
|
||||||
.SetProperty("cookies", &Session::Cookies);
|
.SetProperty("cookies", &Session::Cookies);
|
||||||
}
|
}
|
||||||
|
|
||||||
// static
|
// static
|
||||||
mate::Handle<Session> Session::Create(
|
mate::Handle<Session> Session::Create(
|
||||||
v8::Isolate* isolate,
|
v8::Isolate* isolate,
|
||||||
content::BrowserContext* browser_context) {
|
AtomBrowserContext* browser_context) {
|
||||||
return mate::CreateHandle(isolate, new Session(browser_context));
|
return mate::CreateHandle(isolate, new Session(browser_context));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,24 +5,29 @@
|
||||||
#ifndef ATOM_BROWSER_API_ATOM_API_SESSION_H_
|
#ifndef ATOM_BROWSER_API_ATOM_API_SESSION_H_
|
||||||
#define ATOM_BROWSER_API_ATOM_API_SESSION_H_
|
#define ATOM_BROWSER_API_ATOM_API_SESSION_H_
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "base/callback.h"
|
||||||
#include "native_mate/handle.h"
|
#include "native_mate/handle.h"
|
||||||
#include "native_mate/wrappable.h"
|
#include "native_mate/wrappable.h"
|
||||||
|
|
||||||
namespace content {
|
class GURL;
|
||||||
class BrowserContext;
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
|
||||||
|
class AtomBrowserContext;
|
||||||
|
|
||||||
namespace api {
|
namespace api {
|
||||||
|
|
||||||
class Session: public mate::Wrappable {
|
class Session: public mate::Wrappable {
|
||||||
public:
|
public:
|
||||||
|
using ResolveProxyCallback = base::Callback<void(std::string)>;
|
||||||
|
|
||||||
static mate::Handle<Session> Create(v8::Isolate* isolate,
|
static mate::Handle<Session> Create(v8::Isolate* isolate,
|
||||||
content::BrowserContext* browser_context);
|
AtomBrowserContext* browser_context);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
explicit Session(content::BrowserContext* browser_context);
|
explicit Session(AtomBrowserContext* browser_context);
|
||||||
~Session();
|
~Session();
|
||||||
|
|
||||||
// mate::Wrappable implementations:
|
// mate::Wrappable implementations:
|
||||||
|
@ -30,12 +35,12 @@ class Session: public mate::Wrappable {
|
||||||
v8::Isolate* isolate) override;
|
v8::Isolate* isolate) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void ResolveProxy(const GURL& url, ResolveProxyCallback callback);
|
||||||
v8::Local<v8::Value> Cookies(v8::Isolate* isolate);
|
v8::Local<v8::Value> Cookies(v8::Isolate* isolate);
|
||||||
|
|
||||||
v8::Global<v8::Value> cookies_;
|
v8::Global<v8::Value> cookies_;
|
||||||
|
|
||||||
// The webContents which owns the Sesssion.
|
AtomBrowserContext* browser_context_; // weak ref
|
||||||
content::BrowserContext* browser_context_;
|
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(Session);
|
DISALLOW_COPY_AND_ASSIGN(Session);
|
||||||
};
|
};
|
||||||
|
|
|
@ -608,7 +608,9 @@ void WebContents::InspectServiceWorker() {
|
||||||
|
|
||||||
v8::Local<v8::Value> WebContents::Session(v8::Isolate* isolate) {
|
v8::Local<v8::Value> WebContents::Session(v8::Isolate* isolate) {
|
||||||
if (session_.IsEmpty()) {
|
if (session_.IsEmpty()) {
|
||||||
auto handle = Session::Create(isolate, web_contents()->GetBrowserContext());
|
mate::Handle<api::Session> handle = Session::Create(
|
||||||
|
isolate,
|
||||||
|
static_cast<AtomBrowserContext*>(web_contents()->GetBrowserContext()));
|
||||||
session_.Reset(isolate, handle.ToV8());
|
session_.Reset(isolate, handle.ToV8());
|
||||||
}
|
}
|
||||||
return v8::Local<v8::Value>::New(isolate, session_);
|
return v8::Local<v8::Value>::New(isolate, session_);
|
||||||
|
|
|
@ -26,12 +26,13 @@ if process.platform is 'darwin'
|
||||||
setMenu: bindings.dockSetMenu
|
setMenu: bindings.dockSetMenu
|
||||||
|
|
||||||
# Be compatible with old API.
|
# Be compatible with old API.
|
||||||
app.once 'ready', -> app.emit 'finish-launching'
|
app.once 'ready', -> @emit 'finish-launching'
|
||||||
app.terminate = app.quit
|
app.terminate = app.quit
|
||||||
app.exit = process.exit
|
app.exit = process.exit
|
||||||
app.getHomeDir = -> app.getPath 'home'
|
app.getHomeDir = -> @getPath 'home'
|
||||||
app.getDataPath = -> app.getPath 'userData'
|
app.getDataPath = -> @getPath 'userData'
|
||||||
app.setDataPath = (path) -> app.setPath 'userData', path
|
app.setDataPath = (path) -> @setPath 'userData', path
|
||||||
|
app.resolveProxy = -> @defaultSession.resolveProxy.apply @defaultSession, arguments
|
||||||
|
|
||||||
# Only one App object pemitted.
|
# Only one App object pemitted.
|
||||||
module.exports = app
|
module.exports = app
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue