Change net::CookieStore::SetCookiesCallback to return CookieInclusionStatus

https://chromium-review.googlesource.com/c/chromium/src/+/1470931
This commit is contained in:
deepak1556 2019-02-27 16:05:52 +05:30 committed by Samuel Attard
parent c735116cc3
commit fb9d1bd83c
2 changed files with 44 additions and 12 deletions

View file

@ -189,16 +189,42 @@ void RemoveCookieOnIO(scoped_refptr<net::URLRequestContextGetter> getter,
} }
// Callback of SetCookie. // Callback of SetCookie.
void OnSetCookie(util::Promise promise, bool success) { void OnSetCookie(util::Promise promise,
if (success) { net::CanonicalCookie::CookieInclusionStatus status) {
base::PostTaskWithTraits( std::string errmsg;
FROM_HERE, {BrowserThread::UI}, 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))); base::BindOnce(util::Promise::ResolveEmptyPromise, std::move(promise)));
} else { } else {
base::PostTaskWithTraits( RunCallbackInUI(
FROM_HERE, {BrowserThread::UI}, base::BindOnce(util::Promise::RejectPromise, std::move(promise), errmsg));
base::BindOnce(util::Promise::RejectPromise, std::move(promise),
"Setting cookie failed"));
} }
} }
@ -257,15 +283,21 @@ void SetCookieOnIO(scoped_refptr<net::URLRequestContextGetter> getter,
net::CookieSameSite::DEFAULT_MODE, net::COOKIE_PRIORITY_DEFAULT)); net::CookieSameSite::DEFAULT_MODE, net::COOKIE_PRIORITY_DEFAULT));
auto completion_callback = base::BindOnce(OnSetCookie, std::move(promise)); 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(net::CanonicalCookie::CookieInclusionStatus::
EXCLUDE_FAILURE_TO_STORE);
return; return;
} }
if (url.is_empty()) { if (url.is_empty()) {
std::move(completion_callback).Run(false); std::move(completion_callback)
.Run(net::CanonicalCookie::CookieInclusionStatus::
EXCLUDE_INVALID_DOMAIN);
return; return;
} }
if (name.empty()) { if (name.empty()) {
std::move(completion_callback).Run(false); std::move(completion_callback)
.Run(net::CanonicalCookie::CookieInclusionStatus::
EXCLUDE_FAILURE_TO_STORE);
return; return;
} }
GetCookieStore(getter)->SetCanonicalCookieAsync( GetCookieStore(getter)->SetCanonicalCookieAsync(

View file

@ -142,7 +142,7 @@ describe('session module', () => {
error = e error = e
} }
expect(error).is.an('Error') 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 () => { it('should overwrite previous cookies', async () => {