From 99bfc9b7f5333e76afabdb10573c8339b34afcd7 Mon Sep 17 00:00:00 2001 From: Haojian Wu Date: Tue, 16 Jun 2015 17:23:29 +0800 Subject: [PATCH] Move cookies APIs to webContents.session.cookies namespace. --- atom/browser/api/atom_api_cookies.cc | 13 ---- atom/browser/api/atom_api_session.cc | 45 ++++++++++++++ atom/browser/api/atom_api_session.h | 39 ++++++++++++ atom/browser/api/atom_api_web_contents.cc | 10 +++ atom/browser/api/atom_api_web_contents.h | 4 ++ atom/browser/api/lib/cookies.coffee | 3 - atom/common/node_bindings.cc | 1 - docs/README.md | 1 - docs/api/browser-window.md | 74 +++++++++++++++++++++++ docs/api/cookies.md | 63 ------------------- filenames.gypi | 3 +- spec/api-cookies-spec.coffee | 67 +++++++++++--------- 12 files changed, 211 insertions(+), 112 deletions(-) create mode 100644 atom/browser/api/atom_api_session.cc create mode 100644 atom/browser/api/atom_api_session.h delete mode 100644 atom/browser/api/lib/cookies.coffee delete mode 100644 docs/api/cookies.md diff --git a/atom/browser/api/atom_api_cookies.cc b/atom/browser/api/atom_api_cookies.cc index a52841b5fe3..eb31dcc3728 100644 --- a/atom/browser/api/atom_api_cookies.cc +++ b/atom/browser/api/atom_api_cookies.cc @@ -353,16 +353,3 @@ mate::Handle Cookies::Create(v8::Isolate* isolate) { } // namespace api } // namespace atom - -namespace { - -void Initialize(v8::Local exports, v8::Local unused, - v8::Local context, void* priv) { - v8::Isolate* isolate = context->GetIsolate(); - mate::Dictionary dict(isolate, exports); - dict.Set("cookies", atom::api::Cookies::Create(isolate)); -} - -} // namespace - -NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_cookies, Initialize); diff --git a/atom/browser/api/atom_api_session.cc b/atom/browser/api/atom_api_session.cc new file mode 100644 index 00000000000..316d4b8ff70 --- /dev/null +++ b/atom/browser/api/atom_api_session.cc @@ -0,0 +1,45 @@ +// 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" + +#include "atom/browser/api/atom_api_cookies.h" +#include "native_mate/callback.h" +#include "native_mate/dictionary.h" +#include "native_mate/object_template_builder.h" + +#include "atom/common/node_includes.h" + +namespace atom { + +namespace api { + +Session::Session() { +} + +Session::~Session() { +} + +v8::Local Session::Cookies(v8::Isolate* isolate) { + if (cookies_.IsEmpty()) { + auto handle = atom::api::Cookies::Create(isolate); + cookies_.Reset(isolate, handle.ToV8()); + } + return v8::Local::New(isolate, cookies_); +} + +mate::ObjectTemplateBuilder Session::GetObjectTemplateBuilder( + v8::Isolate* isolate) { + return mate::ObjectTemplateBuilder(isolate) + .SetProperty("cookies", &Session::Cookies); +} + +// static +mate::Handle Session::Create(v8::Isolate* isolate) { + return CreateHandle(isolate, new Session); +} + +} // namespace api + +} // namespace atom diff --git a/atom/browser/api/atom_api_session.h b/atom/browser/api/atom_api_session.h new file mode 100644 index 00000000000..459d541b951 --- /dev/null +++ b/atom/browser/api/atom_api_session.h @@ -0,0 +1,39 @@ +// Copyright (c) 2015 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#ifndef ATOM_BROWSER_API_ATOM_API_SESSION_H_ +#define ATOM_BROWSER_API_ATOM_API_SESSION_H_ + +#include "native_mate/handle.h" +#include "native_mate/wrappable.h" + +namespace atom { + +namespace api { + +class Session: public mate::Wrappable { + public: + static mate::Handle Create(v8::Isolate* isolate); + + protected: + Session(); + ~Session(); + + // mate::Wrappable implementations: + mate::ObjectTemplateBuilder GetObjectTemplateBuilder( + v8::Isolate* isolate) override; + + private: + v8::Local Cookies(v8::Isolate* isolate); + + v8::Global cookies_; + + DISALLOW_COPY_AND_ASSIGN(Session); +}; + +} // namespace api + +} // namespace atom + +#endif // ATOM_BROWSER_API_ATOM_API_SESSION_H_ diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index af00112d09a..65838744794 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -6,6 +6,7 @@ #include +#include "atom/browser/api/atom_api_session.h" #include "atom/browser/atom_browser_client.h" #include "atom/browser/atom_browser_context.h" #include "atom/browser/atom_browser_main_parts.h" @@ -584,6 +585,14 @@ void WebContents::InspectServiceWorker() { } } +v8::Local WebContents::Session(v8::Isolate* isolate) { + if (session_.IsEmpty()) { + auto handle = Session::Create(isolate); + session_.Reset(isolate, handle.ToV8()); + } + return v8::Local::New(isolate, session_); +} + void WebContents::HasServiceWorker( const base::Callback& callback) { auto context = GetServiceWorkerContext(web_contents()); @@ -804,6 +813,7 @@ mate::ObjectTemplateBuilder WebContents::GetObjectTemplateBuilder( .SetMethod("inspectServiceWorker", &WebContents::InspectServiceWorker) .SetMethod("print", &WebContents::Print) .SetMethod("_printToPDF", &WebContents::PrintToPDF) + .SetProperty("session", &WebContents::Session) .Build()); return mate::ObjectTemplateBuilder( diff --git a/atom/browser/api/atom_api_web_contents.h b/atom/browser/api/atom_api_web_contents.h index 655087f9b0f..c83e029c776 100644 --- a/atom/browser/api/atom_api_web_contents.h +++ b/atom/browser/api/atom_api_web_contents.h @@ -239,6 +239,10 @@ class WebContents : public mate::EventEmitter, // Returns the default size of the guestview. gfx::Size GetDefaultSize() const; + v8::Local Session(v8::Isolate* isolate); + + v8::Global session_; + // Stores whether the contents of the guest can be transparent. bool guest_opaque_; diff --git a/atom/browser/api/lib/cookies.coffee b/atom/browser/api/lib/cookies.coffee deleted file mode 100644 index 1d9114fd2c1..00000000000 --- a/atom/browser/api/lib/cookies.coffee +++ /dev/null @@ -1,3 +0,0 @@ -bindings = process.atomBinding 'cookies' - -module.exports = bindings.cookies diff --git a/atom/common/node_bindings.cc b/atom/common/node_bindings.cc index 329683c6060..eb627d06b75 100644 --- a/atom/common/node_bindings.cc +++ b/atom/common/node_bindings.cc @@ -32,7 +32,6 @@ using content::BrowserThread; REFERENCE_MODULE(atom_browser_app); REFERENCE_MODULE(atom_browser_auto_updater); REFERENCE_MODULE(atom_browser_content_tracing); -REFERENCE_MODULE(atom_browser_cookies); REFERENCE_MODULE(atom_browser_dialog); REFERENCE_MODULE(atom_browser_menu); REFERENCE_MODULE(atom_browser_power_monitor); diff --git a/docs/README.md b/docs/README.md index c01a748d05e..98b4219d343 100644 --- a/docs/README.md +++ b/docs/README.md @@ -32,7 +32,6 @@ Modules for the main process: * [auto-updater](api/auto-updater.md) * [browser-window](api/browser-window.md) * [content-tracing](api/content-tracing.md) -* [cookies](api/cookies.md) * [dialog](api/dialog.md) * [global-shortcut](api/global-shortcut.md) * [ipc (main process)](api/ipc-main-process.md) diff --git a/docs/api/browser-window.md b/docs/api/browser-window.md index 05cddc7978a..01298ce4dea 100644 --- a/docs/api/browser-window.md +++ b/docs/api/browser-window.md @@ -1020,3 +1020,77 @@ app.on('ready', function() { is different from the handlers on the main process. 2. There is no way to send synchronous messages from the main process to a renderer process, because it would be very easy to cause dead locks. + +## Class: WebContents.session.cookies + +The `cookies` gives you ability to query and modify cookies, an example is: + +```javascipt +var BrowserWindow = require('browser-window'); + +var win = new BrowserWindow({ width: 800, height: 600 }); + +win.loadUrl('https://github.com'); + +win.webContents.on('did-finish-load', function() { + // Query all cookies. + win.webContents.session.cookies.get({}, function(error, cookies) { + if (error) throw error; + console.log(cookies); + }); + + // Query all cookies that are associated with a specific url. + win.webContents.session.cookies.get({ url : "http://www.github.com" }, + function(error, cookies) { + if (error) throw error; + console.log(cookies); + }); + + // Set a cookie with the given cookie data; + // may overwrite equivalent cookies if they exist. + win.webContents.session.cookies.set( + { url : "http://www.github.com", name : "dummy_name", value : "dummy"}, + function(error, cookies) { + if (error) throw error; + console.log(cookies); + }); +}); +``` + +### WebContents.session.cookies.get(details, callback) + +* `details` Object + * `url` String - Retrieves cookies which are associated with `url`. + Empty imples retrieving cookies of all urls. + * `name` String - Filters cookies by name + * `domain` String - Retrieves cookies whose domains match or are subdomains of `domains` + * `path` String - Retrieves cookies whose path matches `path` + * `secure` Boolean - Filters cookies by their Secure property + * `session` Boolean - Filters out session or persistent cookies. +* `callback` Function - function(error, cookies) + * `error` Error + * `cookies` Array - array of cookie objects. + +### WebContents.session.cookies.set(details, callback) + +* `details` Object + * `url` String - Retrieves cookies which are associated with `url` + * `name` String - The name of the cookie. Empty by default if omitted. + * `value` String - The value of the cookie. Empty by default if omitted. + * `domain` String - The domain of the cookie. Empty by default if omitted. + * `path` String - The path of the cookie. Empty by default if omitted. + * `secure` Boolean - Whether the cookie should be marked as Secure. Defaults to false. + * `session` Boolean - Whether the cookie should be marked as HttpOnly. Defaults to false. + * `expirationDate` Double - The expiration date of the cookie as the number of + seconds since the UNIX epoch. If omitted, the cookie becomes a session cookie. + +* `callback` Function - function(error) + * `error` Error + +### WebContents.session.cookies.remove(details, callback) + +* `details` Object + * `url` String - The URL associated with the cookie + * `name` String - The name of cookie to remove +* `callback` Function - function(error) + * `error` Error diff --git a/docs/api/cookies.md b/docs/api/cookies.md deleted file mode 100644 index d84e871be8d..00000000000 --- a/docs/api/cookies.md +++ /dev/null @@ -1,63 +0,0 @@ -# cookies - -The `cookies` module gives you ability to query and modify cookies, an example -is: - -```javascipt -var cookies = require('cookies'); - -// Query all cookies. -cookies.get({}, function(error, cookies) { - if (error) throw error; - console.log(cookies); -}); - -// Query all cookies that are associated with a specific url. -cookies.get({ url : "http://www.github.com" }, function(error, cookies) { - if (error) throw error; - console.log(cookies); -}); - -// Set a cookie with the given cookie data; may overwrite equivalent cookies if they exist. -cookies.set({ url : "http://www.github.com", name : "dummy_name", value : "dummy"}, - function(error, cookies) { - if (error) throw error; - console.log(cookies); -}); -``` - -## cookies.get(details, callback) - -* `details` Object - * `url` String - Retrieves cookies which are associated with `url`. Empty imples retrieving cookies of all urls. - * `name` String - Filters cookies by name - * `domain` String - Retrieves cookies whose domains match or are subdomains of `domains` - * `path` String - Retrieves cookies whose path matches `path` - * `secure` Boolean - Filters cookies by their Secure property - * `session` Boolean - Filters out session or persistent cookies. -* `callback` Function - function(error, cookies) - * `error` Error - * `cookies` Array - array of cookie objects. - -## cookies.set(details, callback) - -* `details` Object - * `url` String - Retrieves cookies which are associated with `url` - * `name` String - The name of the cookie. Empty by default if omitted. - * `value` String - The value of the cookie. Empty by default if omitted. - * `domain` String - The domain of the cookie. Empty by default if omitted. - * `path` String - The path of the cookie. Empty by default if omitted. - * `secure` Boolean - Whether the cookie should be marked as Secure. Defaults to false. - * `session` Boolean - Whether the cookie should be marked as HttpOnly. Defaults to false. - * `expirationDate` Double - The expiration date of the cookie as the number of seconds since the UNIX epoch. If omitted, the cookie becomes a session cookie. - -* `callback` Function - function(error) - * `error` Error - -## cookies.remove(details, callback) - -* `details` Object - * `url` String - The URL associated with the cookie - * `name` String - The name of cookie to remove -* `callback` Function - function(error) - * `error` Error diff --git a/filenames.gypi b/filenames.gypi index e4da4c89398..992b16636f7 100644 --- a/filenames.gypi +++ b/filenames.gypi @@ -13,7 +13,6 @@ 'atom/browser/api/lib/auto-updater.coffee', 'atom/browser/api/lib/browser-window.coffee', 'atom/browser/api/lib/content-tracing.coffee', - 'atom/browser/api/lib/cookies.coffee', 'atom/browser/api/lib/dialog.coffee', 'atom/browser/api/lib/global-shortcut.coffee', 'atom/browser/api/lib/ipc.coffee', @@ -87,6 +86,8 @@ 'atom/browser/api/atom_api_protocol.h', 'atom/browser/api/atom_api_screen.cc', 'atom/browser/api/atom_api_screen.h', + 'atom/browser/api/atom_api_session.cc', + 'atom/browser/api/atom_api_session.h', 'atom/browser/api/atom_api_tray.cc', 'atom/browser/api/atom_api_tray.h', 'atom/browser/api/atom_api_web_contents.cc', diff --git a/spec/api-cookies-spec.coffee b/spec/api-cookies-spec.coffee index b15be889f52..55d10de152d 100644 --- a/spec/api-cookies-spec.coffee +++ b/spec/api-cookies-spec.coffee @@ -1,12 +1,14 @@ assert = require 'assert' remote = require 'remote' -http = require 'http' -cookies = remote.require 'cookies' +http = require 'http' +path = require 'path' BrowserWindow = remote.require 'browser-window' describe 'cookies module', -> + fixtures = path.resolve __dirname, 'fixtures' w = null url = "http://127.0.0.1:9999" + beforeEach -> w = new BrowserWindow(show: true) afterEach -> w.destroy() @@ -20,7 +22,7 @@ describe 'cookies module', -> {port} = server.address() w.loadUrl url w.webContents.on 'did-finish-load', ()-> - cookies.get {url:url}, (error, cookies) -> + w.webContents.session.cookies.get {url:url}, (error, cookies) -> throw error if error assert.equal 1, cookies.length assert.equal 'type', cookies[0].name @@ -28,36 +30,41 @@ describe 'cookies module', -> done() it 'should overwrite the existent cookie', (done) -> - cookies.set {url:url, name:'type', value:'dummy2'}, (error) -> - throw error if error - cookies.get {url:url}, (error, cookies_list) -> + w.loadUrl 'file://' + path.join(fixtures, 'page', 'a.html') + w.webContents.on 'did-finish-load', ()-> + w.webContents.session.cookies.set {url:url, name:'type', value:'dummy2'}, (error) -> throw error if error - assert.equal 1, cookies_list.length - assert.equal 'type', cookies_list[0].name - assert.equal 'dummy2', cookies_list[0].value - done() + w.webContents.session.cookies.get {url:url}, (error, cookies_list) -> + throw error if error + assert.equal 1, cookies_list.length + assert.equal 'type', cookies_list[0].name + assert.equal 'dummy2', cookies_list[0].value + done() it 'should set new cookie', (done) -> - cookies.set {url:url, name:'key', value:'dummy2'}, (error) -> - throw error if error - cookies.get {url:url}, (error, cookies_list) -> + w.loadUrl 'file://' + path.join(fixtures, 'page', 'a.html') + w.webContents.on 'did-finish-load', ()-> + w.webContents.session.cookies.set {url:url, name:'key', value:'dummy2'}, (error) -> throw error if error - assert.equal 2, cookies_list.length - for cookie in cookies_list - if cookie.name is 'key' - assert.equal 'dummy2', cookie.value - done(); + w.webContents.session.cookies.get {url:url}, (error, cookies_list) -> + throw error if error + assert.equal 2, cookies_list.length + for cookie in cookies_list + if cookie.name is 'key' + assert.equal 'dummy2', cookie.value + done(); it 'should remove cookies', (done) -> - cookies.get {url:url}, (error, cookies_list) -> - count = 0 - console.log cookies_list - for cookie in cookies_list - cookies.remove {url:url, name:cookie.name}, (error) -> - throw error if error - ++count - if count == cookies_list.length - cookies.get {url:url}, (error, cookies_list) -> - throw error if error - assert.equal 0, cookies_list.length - done() + w.loadUrl 'file://' + path.join(fixtures, 'page', 'a.html') + w.webContents.on 'did-finish-load', ()-> + w.webContents.session.cookies.get {url:url}, (error, cookies_list) -> + count = 0 + for cookie in cookies_list + w.webContents.session.cookies.remove {url:url, name:cookie.name}, (error) -> + throw error if error + ++count + if count == cookies_list.length + w.webContents.session.cookies.get {url:url}, (error, cookies_list) -> + throw error if error + assert.equal 0, cookies_list.length + done()