From 33c2768a77df3e123a820c254d5d9f46ed9ecb2f Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 23 Jun 2015 23:40:41 +0800 Subject: [PATCH 1/3] Add app.defaultSession --- atom/browser/api/atom_api_app.cc | 14 +++++++++++++- atom/browser/api/atom_api_app.h | 3 +++ atom/browser/api/atom_api_session.cc | 8 ++++---- atom/browser/api/atom_api_session.h | 13 +++++-------- atom/browser/api/atom_api_web_contents.cc | 4 +++- 5 files changed, 28 insertions(+), 14 deletions(-) diff --git a/atom/browser/api/atom_api_app.cc b/atom/browser/api/atom_api_app.cc index 7468a4b8b83..2c52a19d800 100644 --- a/atom/browser/api/atom_api_app.cc +++ b/atom/browser/api/atom_api_app.cc @@ -12,6 +12,7 @@ #endif #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_main_parts.h" #include "atom/browser/browser.h" @@ -217,6 +218,16 @@ void App::SetAppUserModelId(const std::string& app_id) { #endif } +v8::Local App::DefaultSession(v8::Isolate* isolate) { + if (default_session_.IsEmpty()) { + auto browser_context = static_cast( + AtomBrowserMainParts::Get()->browser_context()); + auto handle = Session::Create(isolate, browser_context); + default_session_.Reset(isolate, handle.ToV8()); + } + return v8::Local::New(isolate, default_session_); +} + mate::ObjectTemplateBuilder App::GetObjectTemplateBuilder( v8::Isolate* isolate) { auto browser = base::Unretained(Browser::Get()); @@ -240,7 +251,8 @@ mate::ObjectTemplateBuilder App::GetObjectTemplateBuilder( .SetMethod("getPath", &App::GetPath) .SetMethod("resolveProxy", &App::ResolveProxy) .SetMethod("setDesktopName", &App::SetDesktopName) - .SetMethod("setAppUserModelId", &App::SetAppUserModelId); + .SetMethod("setAppUserModelId", &App::SetAppUserModelId) + .SetProperty("defaultSession", &App::DefaultSession); } // static diff --git a/atom/browser/api/atom_api_app.h b/atom/browser/api/atom_api_app.h index c2f9212b7ca..7808cfe5fa2 100644 --- a/atom/browser/api/atom_api_app.h +++ b/atom/browser/api/atom_api_app.h @@ -62,6 +62,9 @@ class App : public mate::EventEmitter, void ResolveProxy(const GURL& url, ResolveProxyCallback callback); void SetDesktopName(const std::string& desktop_name); void SetAppUserModelId(const std::string& app_id); + v8::Local DefaultSession(v8::Isolate* isolate); + + v8::Global default_session_; DISALLOW_COPY_AND_ASSIGN(App); }; diff --git a/atom/browser/api/atom_api_session.cc b/atom/browser/api/atom_api_session.cc index 18806ebf41f..7bddee1ccc9 100644 --- a/atom/browser/api/atom_api_session.cc +++ b/atom/browser/api/atom_api_session.cc @@ -5,7 +5,7 @@ #include "atom/browser/api/atom_api_session.h" #include "atom/browser/api/atom_api_cookies.h" -#include "content/public/browser/browser_context.h" +#include "atom/browser/atom_browser_context.h" #include "native_mate/callback.h" #include "native_mate/dictionary.h" #include "native_mate/object_template_builder.h" @@ -16,8 +16,8 @@ namespace atom { namespace api { -Session::Session(content::BrowserContext* browser_context): - browser_context_(browser_context) { +Session::Session(AtomBrowserContext* browser_context) + : browser_context_(browser_context) { } Session::~Session() { @@ -40,7 +40,7 @@ mate::ObjectTemplateBuilder Session::GetObjectTemplateBuilder( // static mate::Handle Session::Create( v8::Isolate* isolate, - content::BrowserContext* browser_context) { + AtomBrowserContext* browser_context) { return mate::CreateHandle(isolate, new Session(browser_context)); } diff --git a/atom/browser/api/atom_api_session.h b/atom/browser/api/atom_api_session.h index 4bdab075770..bd12a386156 100644 --- a/atom/browser/api/atom_api_session.h +++ b/atom/browser/api/atom_api_session.h @@ -8,21 +8,19 @@ #include "native_mate/handle.h" #include "native_mate/wrappable.h" -namespace content { -class BrowserContext; -} - namespace atom { +class AtomBrowserContext; + namespace api { class Session: public mate::Wrappable { public: static mate::Handle Create(v8::Isolate* isolate, - content::BrowserContext* browser_context); + AtomBrowserContext* browser_context); protected: - explicit Session(content::BrowserContext* browser_context); + explicit Session(AtomBrowserContext* browser_context); ~Session(); // mate::Wrappable implementations: @@ -34,8 +32,7 @@ class Session: public mate::Wrappable { v8::Global cookies_; - // The webContents which owns the Sesssion. - content::BrowserContext* browser_context_; + AtomBrowserContext* browser_context_; // weak ref DISALLOW_COPY_AND_ASSIGN(Session); }; diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index 48a1e6a020d..c64db2810ec 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -608,7 +608,9 @@ void WebContents::InspectServiceWorker() { v8::Local WebContents::Session(v8::Isolate* isolate) { if (session_.IsEmpty()) { - auto handle = Session::Create(isolate, web_contents()->GetBrowserContext()); + mate::Handle handle = Session::Create( + isolate, + static_cast(web_contents()->GetBrowserContext())); session_.Reset(isolate, handle.ToV8()); } return v8::Local::New(isolate, session_); From ea69e91e491150e8456c95b897c8f0e3a9555d09 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 24 Jun 2015 11:59:11 +0800 Subject: [PATCH 2/3] Move resolveProxy From app to session --- atom/browser/api/atom_api_app.cc | 47 ----------------------- atom/browser/api/atom_api_app.h | 6 --- atom/browser/api/atom_api_session.cc | 57 +++++++++++++++++++++++++++- atom/browser/api/atom_api_session.h | 8 ++++ 4 files changed, 64 insertions(+), 54 deletions(-) diff --git a/atom/browser/api/atom_api_app.cc b/atom/browser/api/atom_api_app.cc index 2c52a19d800..0c298f131f5 100644 --- a/atom/browser/api/atom_api_app.cc +++ b/atom/browser/api/atom_api_app.cc @@ -17,7 +17,6 @@ #include "atom/browser/atom_browser_main_parts.h" #include "atom/browser/browser.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/environment.h" #include "base/files/file_path.h" @@ -26,10 +25,6 @@ #include "native_mate/callback.h" #include "native_mate/dictionary.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) #include "base/strings/utf_string_conversions.h" @@ -95,43 +90,6 @@ int GetPathConstant(const std::string& name) { 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 App::App() { @@ -200,10 +158,6 @@ void App::SetPath(mate::Arguments* args, 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) { #if defined(OS_LINUX) scoped_ptr env(base::Environment::Create()); @@ -249,7 +203,6 @@ mate::ObjectTemplateBuilder App::GetObjectTemplateBuilder( #endif .SetMethod("setPath", &App::SetPath) .SetMethod("getPath", &App::GetPath) - .SetMethod("resolveProxy", &App::ResolveProxy) .SetMethod("setDesktopName", &App::SetDesktopName) .SetMethod("setAppUserModelId", &App::SetAppUserModelId) .SetProperty("defaultSession", &App::DefaultSession); diff --git a/atom/browser/api/atom_api_app.h b/atom/browser/api/atom_api_app.h index 7808cfe5fa2..f3041757669 100644 --- a/atom/browser/api/atom_api_app.h +++ b/atom/browser/api/atom_api_app.h @@ -9,11 +9,8 @@ #include "atom/browser/api/event_emitter.h" #include "atom/browser/browser_observer.h" -#include "base/callback.h" #include "native_mate/handle.h" -class GURL; - namespace base { class FilePath; } @@ -29,8 +26,6 @@ namespace api { class App : public mate::EventEmitter, public BrowserObserver { public: - typedef base::Callback ResolveProxyCallback; - static mate::Handle Create(v8::Isolate* isolate); protected: @@ -59,7 +54,6 @@ class App : public mate::EventEmitter, const std::string& name, const base::FilePath& path); - void ResolveProxy(const GURL& url, ResolveProxyCallback callback); void SetDesktopName(const std::string& desktop_name); void SetAppUserModelId(const std::string& app_id); v8::Local DefaultSession(v8::Isolate* isolate); diff --git a/atom/browser/api/atom_api_session.cc b/atom/browser/api/atom_api_session.cc index 7bddee1ccc9..0ff22889f7a 100644 --- a/atom/browser/api/atom_api_session.cc +++ b/atom/browser/api/atom_api_session.cc @@ -4,11 +4,17 @@ #include "atom/browser/api/atom_api_session.h" +#include + #include "atom/browser/api/atom_api_cookies.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/dictionary.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" @@ -16,6 +22,50 @@ namespace atom { namespace api { +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 + Session::Session(AtomBrowserContext* browser_context) : browser_context_(browser_context) { } @@ -23,6 +73,10 @@ Session::Session(AtomBrowserContext* browser_context) Session::~Session() { } +void Session::ResolveProxy(const GURL& url, ResolveProxyCallback callback) { + new ResolveProxyHelper(browser_context_, url, callback); +} + v8::Local Session::Cookies(v8::Isolate* isolate) { if (cookies_.IsEmpty()) { auto handle = atom::api::Cookies::Create(isolate, browser_context_); @@ -34,6 +88,7 @@ v8::Local Session::Cookies(v8::Isolate* isolate) { mate::ObjectTemplateBuilder Session::GetObjectTemplateBuilder( v8::Isolate* isolate) { return mate::ObjectTemplateBuilder(isolate) + .SetMethod("resolveProxy", &Session::ResolveProxy) .SetProperty("cookies", &Session::Cookies); } diff --git a/atom/browser/api/atom_api_session.h b/atom/browser/api/atom_api_session.h index bd12a386156..6e2c0fce196 100644 --- a/atom/browser/api/atom_api_session.h +++ b/atom/browser/api/atom_api_session.h @@ -5,9 +5,14 @@ #ifndef ATOM_BROWSER_API_ATOM_API_SESSION_H_ #define ATOM_BROWSER_API_ATOM_API_SESSION_H_ +#include + +#include "base/callback.h" #include "native_mate/handle.h" #include "native_mate/wrappable.h" +class GURL; + namespace atom { class AtomBrowserContext; @@ -16,6 +21,8 @@ namespace api { class Session: public mate::Wrappable { public: + using ResolveProxyCallback = base::Callback; + static mate::Handle Create(v8::Isolate* isolate, AtomBrowserContext* browser_context); @@ -28,6 +35,7 @@ class Session: public mate::Wrappable { v8::Isolate* isolate) override; private: + void ResolveProxy(const GURL& url, ResolveProxyCallback callback); v8::Local Cookies(v8::Isolate* isolate); v8::Global cookies_; From 09a6e37a090c6035f085dd1a1227c5c257e8e93c Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 24 Jun 2015 12:01:19 +0800 Subject: [PATCH 3/3] Keep compatibility with app.resolveProxy --- atom/browser/api/lib/app.coffee | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/atom/browser/api/lib/app.coffee b/atom/browser/api/lib/app.coffee index aad56fd9123..a2ec8bd10c9 100644 --- a/atom/browser/api/lib/app.coffee +++ b/atom/browser/api/lib/app.coffee @@ -26,12 +26,13 @@ if process.platform is 'darwin' setMenu: bindings.dockSetMenu # Be compatible with old API. -app.once 'ready', -> app.emit 'finish-launching' +app.once 'ready', -> @emit 'finish-launching' app.terminate = app.quit app.exit = process.exit -app.getHomeDir = -> app.getPath 'home' -app.getDataPath = -> app.getPath 'userData' -app.setDataPath = (path) -> app.setPath 'userData', path +app.getHomeDir = -> @getPath 'home' +app.getDataPath = -> @getPath 'userData' +app.setDataPath = (path) -> @setPath 'userData', path +app.resolveProxy = -> @defaultSession.resolveProxy.apply @defaultSession, arguments # Only one App object pemitted. module.exports = app