From fb9d1bd83c0d912eed119a45ea0f7b9dac73fc78 Mon Sep 17 00:00:00 2001 From: deepak1556 Date: Wed, 27 Feb 2019 16:05:52 +0530 Subject: [PATCH] Change net::CookieStore::SetCookiesCallback to return CookieInclusionStatus https://chromium-review.googlesource.com/c/chromium/src/+/1470931 --- atom/browser/api/atom_api_cookies.cc | 54 ++++++++++++++++++++++------ spec/api-session-spec.js | 2 +- 2 files changed, 44 insertions(+), 12 deletions(-) diff --git a/atom/browser/api/atom_api_cookies.cc b/atom/browser/api/atom_api_cookies.cc index 3bef56322cc4..43fb36429db3 100644 --- a/atom/browser/api/atom_api_cookies.cc +++ b/atom/browser/api/atom_api_cookies.cc @@ -189,16 +189,42 @@ void RemoveCookieOnIO(scoped_refptr getter, } // Callback of SetCookie. -void OnSetCookie(util::Promise promise, bool success) { - if (success) { - base::PostTaskWithTraits( - FROM_HERE, {BrowserThread::UI}, +void OnSetCookie(util::Promise promise, + net::CanonicalCookie::CookieInclusionStatus status) { + std::string errmsg; + switch (status) { + case net::CanonicalCookie::CookieInclusionStatus::EXCLUDE_HTTP_ONLY: + errmsg = "Failed to create httponly cookie"; + break; + case net::CanonicalCookie::CookieInclusionStatus::EXCLUDE_SECURE_ONLY: + errmsg = "Cannot create a secure cookie from an insecure URL"; + break; + case net::CanonicalCookie::CookieInclusionStatus::EXCLUDE_FAILURE_TO_STORE: + errmsg = "Failed to parse cookie"; + break; + case net::CanonicalCookie::CookieInclusionStatus::EXCLUDE_INVALID_DOMAIN: + errmsg = "Failed to get cookie domain"; + break; + case net::CanonicalCookie::CookieInclusionStatus::EXCLUDE_INVALID_PREFIX: + errmsg = "Failed because the cookie violated prefix rules."; + break; + case net::CanonicalCookie::CookieInclusionStatus:: + EXCLUDE_NONCOOKIEABLE_SCHEME: + errmsg = "Cannot set cookie for current scheme"; + break; + case net::CanonicalCookie::CookieInclusionStatus::INCLUDE: + errmsg = ""; + break; + default: + errmsg = "Setting cookie failed"; + break; + } + if (errmsg.empty()) { + RunCallbackInUI( base::BindOnce(util::Promise::ResolveEmptyPromise, std::move(promise))); } else { - base::PostTaskWithTraits( - FROM_HERE, {BrowserThread::UI}, - base::BindOnce(util::Promise::RejectPromise, std::move(promise), - "Setting cookie failed")); + RunCallbackInUI( + base::BindOnce(util::Promise::RejectPromise, std::move(promise), errmsg)); } } @@ -257,15 +283,21 @@ void SetCookieOnIO(scoped_refptr getter, net::CookieSameSite::DEFAULT_MODE, net::COOKIE_PRIORITY_DEFAULT)); auto completion_callback = base::BindOnce(OnSetCookie, std::move(promise)); if (!canonical_cookie || !canonical_cookie->IsCanonical()) { - std::move(completion_callback).Run(false); + std::move(completion_callback) + .Run(net::CanonicalCookie::CookieInclusionStatus:: + EXCLUDE_FAILURE_TO_STORE); return; } if (url.is_empty()) { - std::move(completion_callback).Run(false); + std::move(completion_callback) + .Run(net::CanonicalCookie::CookieInclusionStatus:: + EXCLUDE_INVALID_DOMAIN); return; } if (name.empty()) { - std::move(completion_callback).Run(false); + std::move(completion_callback) + .Run(net::CanonicalCookie::CookieInclusionStatus:: + EXCLUDE_FAILURE_TO_STORE); return; } GetCookieStore(getter)->SetCanonicalCookieAsync( diff --git a/spec/api-session-spec.js b/spec/api-session-spec.js index 0a9556c435e6..0a91855fba57 100644 --- a/spec/api-session-spec.js +++ b/spec/api-session-spec.js @@ -142,7 +142,7 @@ describe('session module', () => { error = e } expect(error).is.an('Error') - expect(error).to.have.property('message').which.equals('Setting cookie failed') + expect(error).to.have.property('message').which.equals('Failed to get cookie domain') }) it('should overwrite previous cookies', async () => {