Commit 58eab19b authored by Randy Smith's avatar Randy Smith Committed by Commit Bot

Shift profile_resetter over to CookieManager mojo interface.

Bug: 721395
Change-Id: I0d4c7152dededabbc37d11a0f799837d87930958
Reviewed-on: https://chromium-review.googlesource.com/759696Reviewed-by: default avatarDominic Battré <battre@chromium.org>
Reviewed-by: default avatarScott Violet <sky@chromium.org>
Commit-Queue: Randy Smith <rdsmith@chromium.org>
Cr-Commit-Position: refs/heads/master@{#516064}
parent aa02b4e9
...@@ -50,6 +50,7 @@ include_rules = [ ...@@ -50,6 +50,7 @@ include_rules = [
"+services/identity/public", "+services/identity/public",
"+services/metrics/metrics_mojo_service.h", "+services/metrics/metrics_mojo_service.h",
"+services/metrics/public", "+services/metrics/public",
"+services/network/public",
"+services/preferences/public/cpp", "+services/preferences/public/cpp",
"+services/preferences/public/interfaces", "+services/preferences/public/interfaces",
"+services/proxy_resolver", # Android runs the packaged proxy resolver "+services/proxy_resolver", # Android runs the packaged proxy resolver
......
...@@ -10,15 +10,17 @@ ...@@ -10,15 +10,17 @@
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h" #include "chrome/test/base/in_process_browser_test.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/test/test_utils.h" #include "content/public/test/test_utils.h"
#include "net/cookies/cookie_store.h" #include "net/cookies/canonical_cookie.h"
#include "net/url_request/url_request_context.h" #include "services/network/public/interfaces/cookie_manager.mojom.h"
#include "net/url_request/url_request_context_getter.h"
namespace { namespace {
const char kCookieDefinition[] = "A=1"; const char kCookieName[] = "A";
const char kCookieHostname[] = "http://host1"; const char kCookieValue[] = "1";
const char kCookieHostname[] = "host1.com";
using content::BrowserThread; using content::BrowserThread;
...@@ -29,24 +31,22 @@ class RemoveCookieTester { ...@@ -29,24 +31,22 @@ class RemoveCookieTester {
explicit RemoveCookieTester(Profile* profile); explicit RemoveCookieTester(Profile* profile);
~RemoveCookieTester(); ~RemoveCookieTester();
std::string GetCookie(const std::string& host); bool GetCookie(const std::string& host, net::CanonicalCookie* cookie);
void AddCookie(const std::string& host, const std::string& definition); void AddCookie(const std::string& host,
const std::string& name,
const std::string& value);
private: private:
void GetCookieOnIOThread(net::URLRequestContextGetter* context_getter, void GetCookieListCallback(const std::vector<net::CanonicalCookie>& cookies);
const std::string& host); void SetCanonicalCookieCallback(bool result);
void SetCookieOnIOThread(net::URLRequestContextGetter* context_getter,
const std::string& host,
const std::string& definition);
void GetCookieCallback(const std::string& cookies);
void SetCookieCallback(bool result);
void BlockUntilNotified(); void BlockUntilNotified();
void Notify(); void Notify();
std::string last_cookies_; std::vector<net::CanonicalCookie> last_cookies_;
bool waiting_callback_; bool waiting_callback_;
Profile* profile_; Profile* profile_;
network::mojom::CookieManagerPtr cookie_manager_;
scoped_refptr<content::MessageLoopRunner> runner_; scoped_refptr<content::MessageLoopRunner> runner_;
DISALLOW_COPY_AND_ASSIGN(RemoveCookieTester); DISALLOW_COPY_AND_ASSIGN(RemoveCookieTester);
...@@ -55,81 +55,57 @@ class RemoveCookieTester { ...@@ -55,81 +55,57 @@ class RemoveCookieTester {
RemoveCookieTester::RemoveCookieTester(Profile* profile) RemoveCookieTester::RemoveCookieTester(Profile* profile)
: waiting_callback_(false), : waiting_callback_(false),
profile_(profile) { profile_(profile) {
content::mojom::NetworkContext* network_context =
content::BrowserContext::GetDefaultStoragePartition(profile_)
->GetNetworkContext();
network_context->GetCookieManager(mojo::MakeRequest(&cookie_manager_));
} }
RemoveCookieTester::~RemoveCookieTester() {} RemoveCookieTester::~RemoveCookieTester() {}
// Returns true, if the given cookie exists in the cookie store. // Returns true and sets |*cookie| if the given cookie exists in
std::string RemoveCookieTester::GetCookie(const std::string& host) { // the cookie store.
bool RemoveCookieTester::GetCookie(const std::string& host,
net::CanonicalCookie* cookie) {
last_cookies_.clear(); last_cookies_.clear();
DCHECK(!waiting_callback_); DCHECK(!waiting_callback_);
waiting_callback_ = true; waiting_callback_ = true;
BrowserThread::PostTask( net::CookieOptions cookie_options;
BrowserThread::IO, FROM_HERE, cookie_manager_->GetCookieList(
base::BindOnce(&RemoveCookieTester::GetCookieOnIOThread, GURL("http://" + host + "/"), cookie_options,
base::Unretained(this), base::BindOnce(&RemoveCookieTester::GetCookieListCallback,
base::Unretained(profile_->GetRequestContext()), host)); base::Unretained(this)));
BlockUntilNotified(); BlockUntilNotified();
return last_cookies_; DCHECK_GE(1u, last_cookies_.size());
if (0u == last_cookies_.size())
return false;
*cookie = last_cookies_[0];
return true;
} }
void RemoveCookieTester::AddCookie(const std::string& host, void RemoveCookieTester::AddCookie(const std::string& host,
const std::string& definition) { const std::string& name,
const std::string& value) {
DCHECK(!waiting_callback_); DCHECK(!waiting_callback_);
waiting_callback_ = true; waiting_callback_ = true;
BrowserThread::PostTask( cookie_manager_->SetCanonicalCookie(
BrowserThread::IO, FROM_HERE, net::CanonicalCookie(name, value, host, "/", base::Time(), base::Time(),
base::BindOnce( base::Time(), false, false,
&RemoveCookieTester::SetCookieOnIOThread, base::Unretained(this), net::CookieSameSite::NO_RESTRICTION,
base::Unretained(profile_->GetRequestContext()), host, definition)); net::COOKIE_PRIORITY_MEDIUM),
false /* secure_source */, true /* modify_http_only */,
base::BindOnce(&RemoveCookieTester::SetCanonicalCookieCallback,
base::Unretained(this)));
BlockUntilNotified(); BlockUntilNotified();
} }
void RemoveCookieTester::GetCookieOnIOThread( void RemoveCookieTester::GetCookieListCallback(
net::URLRequestContextGetter* context_getter, const std::vector<net::CanonicalCookie>& cookies) {
const std::string& host) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
net::CookieStore* cookie_store = context_getter->
GetURLRequestContext()->cookie_store();
cookie_store->GetCookiesWithOptionsAsync(
GURL(host), net::CookieOptions(),
base::Bind(&RemoveCookieTester::GetCookieCallback,
base::Unretained(this)));
}
void RemoveCookieTester::SetCookieOnIOThread(
net::URLRequestContextGetter* context_getter,
const std::string& host,
const std::string& definition) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
net::CookieStore* cookie_store = context_getter->
GetURLRequestContext()->cookie_store();
cookie_store->SetCookieWithOptionsAsync(
GURL(host), definition, net::CookieOptions(),
base::Bind(&RemoveCookieTester::SetCookieCallback,
base::Unretained(this)));
}
void RemoveCookieTester::GetCookieCallback(const std::string& cookies) {
if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::BindOnce(&RemoveCookieTester::GetCookieCallback,
base::Unretained(this), cookies));
return;
}
last_cookies_ = cookies; last_cookies_ = cookies;
Notify(); Notify();
} }
void RemoveCookieTester::SetCookieCallback(bool result) { void RemoveCookieTester::SetCanonicalCookieCallback(bool result) {
if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::BindOnce(&RemoveCookieTester::SetCookieCallback,
base::Unretained(this), result));
return;
}
ASSERT_TRUE(result); ASSERT_TRUE(result);
Notify(); Notify();
} }
...@@ -161,12 +137,16 @@ class ProfileResetTest : public InProcessBrowserTest, ...@@ -161,12 +137,16 @@ class ProfileResetTest : public InProcessBrowserTest,
IN_PROC_BROWSER_TEST_F(ProfileResetTest, ResetCookiesAndSiteData) { IN_PROC_BROWSER_TEST_F(ProfileResetTest, ResetCookiesAndSiteData) {
RemoveCookieTester tester(browser()->profile()); RemoveCookieTester tester(browser()->profile());
tester.AddCookie(kCookieHostname, kCookieDefinition); std::string host_prefix("http://");
ASSERT_EQ(kCookieDefinition, tester.GetCookie(kCookieHostname)); tester.AddCookie(kCookieHostname, kCookieName, kCookieValue);
net::CanonicalCookie cookie;
ASSERT_TRUE(tester.GetCookie(kCookieHostname, &cookie));
EXPECT_EQ(kCookieName, cookie.Name());
EXPECT_EQ(kCookieValue, cookie.Value());
ResetAndWait(ProfileResetter::COOKIES_AND_SITE_DATA); ResetAndWait(ProfileResetter::COOKIES_AND_SITE_DATA);
EXPECT_EQ("", tester.GetCookie(kCookieHostname)); EXPECT_FALSE(tester.GetCookie(host_prefix + kCookieHostname, &cookie));
} }
} // namespace } // namespace
...@@ -2367,6 +2367,7 @@ test("browser_tests") { ...@@ -2367,6 +2367,7 @@ test("browser_tests") {
"//components/arc:arc_test_support", "//components/arc:arc_test_support",
"//components/prefs", "//components/prefs",
"//components/user_manager:test_support", "//components/user_manager:test_support",
"//services/network/public/interfaces",
"//services/preferences/public/cpp", "//services/preferences/public/cpp",
"//services/preferences/public/interfaces", "//services/preferences/public/interfaces",
"//services/service_manager/public/cpp", "//services/service_manager/public/cpp",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment