Skip to content

Commit

Permalink
feat: promisify cookies api (#16702)
Browse files Browse the repository at this point in the history
* feat: promisify cookies api (#16464)

* feat: promisify the Cookie API

* chore: update specs to test promisified cookies

* chore: add deprecate wrapper for cookie callback API

* docs: update docs to cookie promise changes

* chore: remove redundant namespace use

* docs: improve cookie example

* docs: restore docs for cookie callback API

* chore: restore cookie callback tests

* fix: syntax of cookie promise return types

* fix: use new promisify helper
  • Loading branch information
codebytere committed Feb 3, 2019
1 parent 6d76da5 commit 60af6c2
Show file tree
Hide file tree
Showing 8 changed files with 377 additions and 214 deletions.
100 changes: 71 additions & 29 deletions atom/browser/api/atom_api_cookies.cc
Expand Up @@ -136,32 +136,50 @@ inline net::CookieStore* GetCookieStore(
return getter->GetURLRequestContext()->cookie_store(); return getter->GetURLRequestContext()->cookie_store();
} }


void ResolvePromiseWithCookies(scoped_refptr<util::Promise> promise,
net::CookieList cookieList) {
promise->Resolve(cookieList);
}

void ResolvePromise(scoped_refptr<util::Promise> promise) {
promise->Resolve();
}

// Resolve |promise| in UI thread.
void ResolvePromiseInUI(scoped_refptr<util::Promise> promise) {
base::PostTaskWithTraits(FROM_HERE, {BrowserThread::UI},
base::BindOnce(ResolvePromise, std::move(promise)));
}

// Run |callback| on UI thread. // Run |callback| on UI thread.
void RunCallbackInUI(const base::Closure& callback) { void RunCallbackInUI(const base::Closure& callback) {
base::PostTaskWithTraits(FROM_HERE, {BrowserThread::UI}, callback); base::PostTaskWithTraits(FROM_HERE, {BrowserThread::UI}, callback);
} }


// Remove cookies from |list| not matching |filter|, and pass it to |callback|. // Remove cookies from |list| not matching |filter|, and pass it to |callback|.
void FilterCookies(std::unique_ptr<base::DictionaryValue> filter, void FilterCookies(std::unique_ptr<base::DictionaryValue> filter,
const Cookies::GetCallback& callback, scoped_refptr<util::Promise> promise,
const net::CookieList& list) { const net::CookieList& list) {
net::CookieList result; net::CookieList result;
for (const auto& cookie : list) { for (const auto& cookie : list) {
if (MatchesCookie(filter.get(), cookie)) if (MatchesCookie(filter.get(), cookie))
result.push_back(cookie); result.push_back(cookie);
} }
RunCallbackInUI(base::Bind(callback, Cookies::SUCCESS, result));
base::PostTaskWithTraits(
FROM_HERE, {BrowserThread::UI},
base::BindOnce(ResolvePromiseWithCookies, std::move(promise), result));
} }


// Receives cookies matching |filter| in IO thread. // Receives cookies matching |filter| in IO thread.
void GetCookiesOnIO(scoped_refptr<net::URLRequestContextGetter> getter, void GetCookiesOnIO(scoped_refptr<net::URLRequestContextGetter> getter,
std::unique_ptr<base::DictionaryValue> filter, std::unique_ptr<base::DictionaryValue> filter,
const Cookies::GetCallback& callback) { scoped_refptr<util::Promise> promise) {
std::string url; std::string url;
filter->GetString("url", &url); filter->GetString("url", &url);


auto filtered_callback = auto filtered_callback =
base::Bind(FilterCookies, base::Passed(&filter), callback); base::Bind(FilterCookies, base::Passed(&filter), std::move(promise));


// Empty url will match all url cookies. // Empty url will match all url cookies.
if (url.empty()) if (url.empty())
Expand All @@ -172,31 +190,42 @@ void GetCookiesOnIO(scoped_refptr<net::URLRequestContextGetter> getter,
} }


// Removes cookie with |url| and |name| in IO thread. // Removes cookie with |url| and |name| in IO thread.
void RemoveCookieOnIOThread(scoped_refptr<net::URLRequestContextGetter> getter, void RemoveCookieOnIO(scoped_refptr<net::URLRequestContextGetter> getter,
const GURL& url, const GURL& url,
const std::string& name, const std::string& name,
const base::Closure& callback) { scoped_refptr<util::Promise> promise) {
GetCookieStore(getter)->DeleteCookieAsync( GetCookieStore(getter)->DeleteCookieAsync(
url, name, base::BindOnce(RunCallbackInUI, callback)); url, name, base::BindOnce(ResolvePromiseInUI, promise));
}

// Resolves/rejects the |promise| in UI thread.
void SettlePromiseInUI(scoped_refptr<util::Promise> promise,
const std::string& errmsg) {
if (errmsg.empty()) {
promise->Resolve();
} else {
promise->RejectWithErrorMessage(errmsg);
}
} }


// Callback of SetCookie. // Callback of SetCookie.
void OnSetCookie(const Cookies::SetCallback& callback, bool success) { void OnSetCookie(scoped_refptr<util::Promise> promise, bool success) {
RunCallbackInUI( const std::string errmsg = success ? "" : "Setting cookie failed";
base::Bind(callback, success ? Cookies::SUCCESS : Cookies::FAILED)); RunCallbackInUI(base::Bind(SettlePromiseInUI, std::move(promise), errmsg));
} }


// Flushes cookie store in IO thread. // Flushes cookie store in IO thread.
void FlushCookieStoreOnIOThread( void FlushCookieStoreOnIOThread(
scoped_refptr<net::URLRequestContextGetter> getter, scoped_refptr<net::URLRequestContextGetter> getter,
const base::Closure& callback) { scoped_refptr<util::Promise> promise) {
GetCookieStore(getter)->FlushStore(base::BindOnce(RunCallbackInUI, callback)); GetCookieStore(getter)->FlushStore(
base::BindOnce(ResolvePromiseInUI, promise));
} }


// Sets cookie with |details| in IO thread. // Sets cookie with |details| in IO thread.
void SetCookieOnIO(scoped_refptr<net::URLRequestContextGetter> getter, void SetCookieOnIO(scoped_refptr<net::URLRequestContextGetter> getter,
std::unique_ptr<base::DictionaryValue> details, std::unique_ptr<base::DictionaryValue> details,
const Cookies::SetCallback& callback) { scoped_refptr<util::Promise> promise) {
std::string url, name, value, domain, path; std::string url, name, value, domain, path;
bool secure = false; bool secure = false;
bool http_only = false; bool http_only = false;
Expand Down Expand Up @@ -237,7 +266,7 @@ void SetCookieOnIO(scoped_refptr<net::URLRequestContextGetter> getter,
GURL(url), name, value, domain, path, creation_time, expiration_time, GURL(url), name, value, domain, path, creation_time, expiration_time,
last_access_time, secure, http_only, last_access_time, secure, http_only,
net::CookieSameSite::DEFAULT_MODE, net::COOKIE_PRIORITY_DEFAULT)); net::CookieSameSite::DEFAULT_MODE, net::COOKIE_PRIORITY_DEFAULT));
auto completion_callback = base::BindOnce(OnSetCookie, callback); auto completion_callback = base::BindOnce(OnSetCookie, std::move(promise));
if (!canonical_cookie || !canonical_cookie->IsCanonical()) { if (!canonical_cookie || !canonical_cookie->IsCanonical()) {
std::move(completion_callback).Run(false); std::move(completion_callback).Run(false);
return; return;
Expand Down Expand Up @@ -267,43 +296,56 @@ Cookies::Cookies(v8::Isolate* isolate, AtomBrowserContext* browser_context)


Cookies::~Cookies() {} Cookies::~Cookies() {}


void Cookies::Get(const base::DictionaryValue& filter, v8::Local<v8::Promise> Cookies::Get(const base::DictionaryValue& filter) {
const GetCallback& callback) { scoped_refptr<util::Promise> promise = new util::Promise(isolate());

auto copy = base::DictionaryValue::From( auto copy = base::DictionaryValue::From(
base::Value::ToUniquePtrValue(filter.Clone())); base::Value::ToUniquePtrValue(filter.Clone()));
auto* getter = browser_context_->GetRequestContext(); auto* getter = browser_context_->GetRequestContext();
base::PostTaskWithTraits( base::PostTaskWithTraits(
FROM_HERE, {BrowserThread::IO}, FROM_HERE, {BrowserThread::IO},
base::BindOnce(GetCookiesOnIO, base::RetainedRef(getter), std::move(copy), base::BindOnce(GetCookiesOnIO, base::RetainedRef(getter), std::move(copy),
callback)); promise));

return promise->GetHandle();
} }


void Cookies::Remove(const GURL& url, v8::Local<v8::Promise> Cookies::Remove(const GURL& url,
const std::string& name, const std::string& name) {
const base::Closure& callback) { scoped_refptr<util::Promise> promise = new util::Promise(isolate());

auto* getter = browser_context_->GetRequestContext(); auto* getter = browser_context_->GetRequestContext();
base::PostTaskWithTraits( base::PostTaskWithTraits(
FROM_HERE, {BrowserThread::IO}, FROM_HERE, {BrowserThread::IO},
base::BindOnce(RemoveCookieOnIOThread, base::RetainedRef(getter), url, base::BindOnce(RemoveCookieOnIO, base::RetainedRef(getter), url, name,
name, callback)); promise));

return promise->GetHandle();
} }


void Cookies::Set(const base::DictionaryValue& details, v8::Local<v8::Promise> Cookies::Set(const base::DictionaryValue& details) {
const SetCallback& callback) { scoped_refptr<util::Promise> promise = new util::Promise(isolate());

auto copy = base::DictionaryValue::From( auto copy = base::DictionaryValue::From(
base::Value::ToUniquePtrValue(details.Clone())); base::Value::ToUniquePtrValue(details.Clone()));
auto* getter = browser_context_->GetRequestContext(); auto* getter = browser_context_->GetRequestContext();
base::PostTaskWithTraits( base::PostTaskWithTraits(
FROM_HERE, {BrowserThread::IO}, FROM_HERE, {BrowserThread::IO},
base::BindOnce(SetCookieOnIO, base::RetainedRef(getter), std::move(copy), base::BindOnce(SetCookieOnIO, base::RetainedRef(getter), std::move(copy),
callback)); promise));

return promise->GetHandle();
} }


void Cookies::FlushStore(const base::Closure& callback) { v8::Local<v8::Promise> Cookies::FlushStore() {
scoped_refptr<util::Promise> promise = new util::Promise(isolate());

auto* getter = browser_context_->GetRequestContext(); auto* getter = browser_context_->GetRequestContext();
base::PostTaskWithTraits(FROM_HERE, {BrowserThread::IO}, base::PostTaskWithTraits(FROM_HERE, {BrowserThread::IO},
base::BindOnce(FlushCookieStoreOnIOThread, base::BindOnce(FlushCookieStoreOnIOThread,
base::RetainedRef(getter), callback)); base::RetainedRef(getter), promise));

return promise->GetHandle();
} }


void Cookies::OnCookieChanged(const CookieDetails* details) { void Cookies::OnCookieChanged(const CookieDetails* details) {
Expand Down
14 changes: 5 additions & 9 deletions atom/browser/api/atom_api_cookies.h
Expand Up @@ -10,6 +10,7 @@


#include "atom/browser/api/trackable_object.h" #include "atom/browser/api/trackable_object.h"
#include "atom/browser/net/cookie_details.h" #include "atom/browser/net/cookie_details.h"
#include "atom/common/promise_util.h"
#include "base/callback_list.h" #include "base/callback_list.h"
#include "native_mate/handle.h" #include "native_mate/handle.h"
#include "net/cookies/canonical_cookie.h" #include "net/cookies/canonical_cookie.h"
Expand All @@ -35,9 +36,6 @@ class Cookies : public mate::TrackableObject<Cookies> {
FAILED, FAILED,
}; };


using GetCallback = base::Callback<void(Error, const net::CookieList&)>;
using SetCallback = base::Callback<void(Error)>;

static mate::Handle<Cookies> Create(v8::Isolate* isolate, static mate::Handle<Cookies> Create(v8::Isolate* isolate,
AtomBrowserContext* browser_context); AtomBrowserContext* browser_context);


Expand All @@ -49,12 +47,10 @@ class Cookies : public mate::TrackableObject<Cookies> {
Cookies(v8::Isolate* isolate, AtomBrowserContext* browser_context); Cookies(v8::Isolate* isolate, AtomBrowserContext* browser_context);
~Cookies() override; ~Cookies() override;


void Get(const base::DictionaryValue& filter, const GetCallback& callback); v8::Local<v8::Promise> Get(const base::DictionaryValue& filter);
void Remove(const GURL& url, v8::Local<v8::Promise> Set(const base::DictionaryValue& details);
const std::string& name, v8::Local<v8::Promise> Remove(const GURL& url, const std::string& name);
const base::Closure& callback); v8::Local<v8::Promise> FlushStore();
void Set(const base::DictionaryValue& details, const SetCallback& callback);
void FlushStore(const base::Closure& callback);


// CookieChangeNotifier subscription: // CookieChangeNotifier subscription:
void OnCookieChanged(const CookieDetails*); void OnCookieChanged(const CookieDetails*);
Expand Down
87 changes: 78 additions & 9 deletions docs/api/cookies.md
Expand Up @@ -13,21 +13,30 @@ For example:
const { session } = require('electron') const { session } = require('electron')


// Query all cookies. // Query all cookies.
session.defaultSession.cookies.get({}, (error, cookies) => { session.defaultSession.cookies.get({})
console.log(error, cookies) .then((cookies) => {
}) console.log(cookies)
}).catch((error) => {
console.log(error)
})


// Query all cookies associated with a specific url. // Query all cookies associated with a specific url.
session.defaultSession.cookies.get({ url: 'http://www.github.com' }, (error, cookies) => { session.defaultSession.cookies.get({ url: 'http://www.github.com' })
console.log(error, cookies) .then((cookies) => {
}) console.log(cookies)
}).catch((error) => {
console.log(error)
})


// Set a cookie with the given cookie data; // Set a cookie with the given cookie data;
// may overwrite equivalent cookies if they exist. // may overwrite equivalent cookies if they exist.
const cookie = { url: 'http://www.github.com', name: 'dummy_name', value: 'dummy' } const cookie = { url: 'http://www.github.com', name: 'dummy_name', value: 'dummy' }
session.defaultSession.cookies.set(cookie, (error) => { session.defaultSession.cookies.set(cookie)
if (error) console.error(error) .then(() => {
}) // success
}, (error) => {
console.error(error)
})
``` ```


### Instance Events ### Instance Events
Expand Down Expand Up @@ -55,6 +64,23 @@ expired.


The following methods are available on instances of `Cookies`: The following methods are available on instances of `Cookies`:


#### `cookies.get(filter)`

* `filter` Object
* `url` String (optional) - Retrieves cookies which are associated with
`url`. Empty implies retrieving cookies of all urls.
* `name` String (optional) - Filters cookies by name.
* `domain` String (optional) - Retrieves cookies whose domains match or are
subdomains of `domains`.
* `path` String (optional) - Retrieves cookies whose path matches `path`.
* `secure` Boolean (optional) - Filters cookies by their Secure property.
* `session` Boolean (optional) - Filters out session or persistent cookies.

Returns `Promise<Cookie[]>` - A promise which resolves an array of cookie objects.

Sends a request to get all cookies matching `filter`, and resolves a promise with
the response.

#### `cookies.get(filter, callback)` #### `cookies.get(filter, callback)`


* `filter` Object * `filter` Object
Expand All @@ -73,6 +99,28 @@ The following methods are available on instances of `Cookies`:
Sends a request to get all cookies matching `filter`, `callback` will be called Sends a request to get all cookies matching `filter`, `callback` will be called
with `callback(error, cookies)` on complete. with `callback(error, cookies)` on complete.


**[Deprecated Soon](promisification.md)**

#### `cookies.set(details)`

* `details` Object
* `url` String - The url to associate the cookie with.
* `name` String (optional) - The name of the cookie. Empty by default if omitted.
* `value` String (optional) - The value of the cookie. Empty by default if omitted.
* `domain` String (optional) - The domain of the cookie. Empty by default if omitted.
* `path` String (optional) - The path of the cookie. Empty by default if omitted.
* `secure` Boolean (optional) - Whether the cookie should be marked as Secure. Defaults to
false.
* `httpOnly` Boolean (optional) - Whether the cookie should be marked as HTTP only.
Defaults to false.
* `expirationDate` Double (optional) - The expiration date of the cookie as the number of
seconds since the UNIX epoch. If omitted then the cookie becomes a session
cookie and will not be retained between sessions.

Returns `Promise<void>` - A promise which resolves when the cookie has been set

Sets a cookie with `details`.

#### `cookies.set(details, callback)` #### `cookies.set(details, callback)`


* `details` Object * `details` Object
Expand All @@ -94,6 +142,17 @@ with `callback(error, cookies)` on complete.
Sets a cookie with `details`, `callback` will be called with `callback(error)` Sets a cookie with `details`, `callback` will be called with `callback(error)`
on complete. on complete.


**[Deprecated Soon](promisification.md)**

#### `cookies.remove(url, name)`

* `url` String - The URL associated with the cookie.
* `name` String - The name of cookie to remove.

Returns `Promise<void>` - A promise which resolves when the cookie has been removed

Removes the cookies matching `url` and `name`

#### `cookies.remove(url, name, callback)` #### `cookies.remove(url, name, callback)`


* `url` String - The URL associated with the cookie. * `url` String - The URL associated with the cookie.
Expand All @@ -103,8 +162,18 @@ on complete.
Removes the cookies matching `url` and `name`, `callback` will called with Removes the cookies matching `url` and `name`, `callback` will called with
`callback()` on complete. `callback()` on complete.


**[Deprecated Soon](promisification.md)**

#### `cookies.flushStore()`

Returns `Promise<void>` - A promise which resolves when the cookie store has been flushed

Writes any unwritten cookies data to disk.

#### `cookies.flushStore(callback)` #### `cookies.flushStore(callback)`


* `callback` Function * `callback` Function


Writes any unwritten cookies data to disk. Writes any unwritten cookies data to disk.

**[Deprecated Soon](promisification.md)**
17 changes: 8 additions & 9 deletions docs/api/promisification.md
Expand Up @@ -12,10 +12,6 @@ When a majority of affected functions are migrated, this flag will be enabled by
- [request.write(chunk[, encoding][, callback])](https://github.com/electron/electron/blob/master/docs/api/client-request.md#write) - [request.write(chunk[, encoding][, callback])](https://github.com/electron/electron/blob/master/docs/api/client-request.md#write)
- [request.end([chunk][, encoding][, callback])](https://github.com/electron/electron/blob/master/docs/api/client-request.md#end) - [request.end([chunk][, encoding][, callback])](https://github.com/electron/electron/blob/master/docs/api/client-request.md#end)
- [contentTracing.getTraceBufferUsage(callback)](https://github.com/electron/electron/blob/master/docs/api/content-tracing.md#getTraceBufferUsage) - [contentTracing.getTraceBufferUsage(callback)](https://github.com/electron/electron/blob/master/docs/api/content-tracing.md#getTraceBufferUsage)
- [cookies.get(filter, callback)](https://github.com/electron/electron/blob/master/docs/api/cookies.md#get)
- [cookies.set(details, callback)](https://github.com/electron/electron/blob/master/docs/api/cookies.md#set)
- [cookies.remove(url, name, callback)](https://github.com/electron/electron/blob/master/docs/api/cookies.md#remove)
- [cookies.flushStore(callback)](https://github.com/electron/electron/blob/master/docs/api/cookies.md#flushStore)
- [debugger.sendCommand(method[, commandParams, callback])](https://github.com/electron/electron/blob/master/docs/api/debugger.md#sendCommand) - [debugger.sendCommand(method[, commandParams, callback])](https://github.com/electron/electron/blob/master/docs/api/debugger.md#sendCommand)
- [dialog.showOpenDialog([browserWindow, ]options[, callback])](https://github.com/electron/electron/blob/master/docs/api/dialog.md#showOpenDialog) - [dialog.showOpenDialog([browserWindow, ]options[, callback])](https://github.com/electron/electron/blob/master/docs/api/dialog.md#showOpenDialog)
- [dialog.showSaveDialog([browserWindow, ]options[, callback])](https://github.com/electron/electron/blob/master/docs/api/dialog.md#showSaveDialog) - [dialog.showSaveDialog([browserWindow, ]options[, callback])](https://github.com/electron/electron/blob/master/docs/api/dialog.md#showSaveDialog)
Expand Down Expand Up @@ -46,14 +42,17 @@ When a majority of affected functions are migrated, this flag will be enabled by


### Converted Functions ### Converted Functions


- [win.capturePage([rect, ]callback)](https://github.com/electron/electron/blob/master/docs/api/browser-window.md#capturePage)
- [webviewTag.capturePage([rect, ]callback)](https://github.com/electron/electron/blob/master/docs/api/webview-tag.md#capturePage)
- [contents.capturePage([rect, ]callback)](https://github.com/electron/electron/blob/master/docs/api/web-contents.md#capturePage)
- [app.getFileIcon(path[, options], callback)](https://github.com/electron/electron/blob/master/docs/api/app.md#getFileIcon) - [app.getFileIcon(path[, options], callback)](https://github.com/electron/electron/blob/master/docs/api/app.md#getFileIcon)
- [contents.capturePage([rect, ]callback)](https://github.com/electron/electron/blob/master/docs/api/web-contents.md#capturePage)
- [contentTracing.getCategories(callback)](https://github.com/electron/electron/blob/master/docs/api/content-tracing.md#getCategories) - [contentTracing.getCategories(callback)](https://github.com/electron/electron/blob/master/docs/api/content-tracing.md#getCategories)
- [contentTracing.startRecording(options, callback)](https://github.com/electron/electron/blob/master/docs/api/content-tracing.md#startRecording) - [contentTracing.startRecording(options, callback)](https://github.com/electron/electron/blob/master/docs/api/content-tracing.md#startRecording)
- [contentTracing.stopRecording(resultFilePath, callback)](https://github.com/electron/electron/blob/master/docs/api/content-tracing.md#stopRecording) - [contentTracing.stopRecording(resultFilePath, callback)](https://github.com/electron/electron/blob/master/docs/api/content-tracing.md#stopRecording)
- [cookies.flushStore(callback)](https://github.com/electron/electron/blob/master/docs/api/cookies.md#flushStore)
- [cookies.get(filter, callback)](https://github.com/electron/electron/blob/master/docs/api/cookies.md#get)
- [cookies.remove(url, name, callback)](https://github.com/electron/electron/blob/master/docs/api/cookies.md#remove)
- [cookies.set(details, callback)](https://github.com/electron/electron/blob/master/docs/api/cookies.md#set)
- [desktopCapturer.getSources(options, callback)](https://github.com/electron/electron/blob/master/docs/api/desktop-capturer.md#getSources) - [desktopCapturer.getSources(options, callback)](https://github.com/electron/electron/blob/master/docs/api/desktop-capturer.md#getSources)
- [shell.openExternal(url[, options, callback])](https://github.com/electron/electron/blob/master/docs/api/shell.md#openExternal)
- [protocol.isProtocolHandled(scheme, callback)](https://github.com/electron/electron/blob/master/docs/api/protocol.md#isProtocolHandled) - [protocol.isProtocolHandled(scheme, callback)](https://github.com/electron/electron/blob/master/docs/api/protocol.md#isProtocolHandled)
- [desktopCapturer.getSources(options, callback)](https://github.com/electron/electron/blob/master/docs/api/desktop-capturer.md#getSources) - [shell.openExternal(url[, options, callback])](https://github.com/electron/electron/blob/master/docs/api/shell.md#openExternal)
- [webviewTag.capturePage([rect, ]callback)](https://github.com/electron/electron/blob/master/docs/api/webview-tag.md#capturePage)
- [win.capturePage([rect, ]callback)](https://github.com/electron/electron/blob/master/docs/api/browser-window.md#capturePage)

0 comments on commit 60af6c2

Please sign in to comment.