OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/profile_resetter/profile_resetter.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "chrome/browser/profile_resetter/profile_resetter_test_base.h" | |
9 #include "chrome/browser/profiles/profile.h" | |
10 #include "chrome/browser/ui/browser.h" | |
11 #include "chrome/test/base/in_process_browser_test.h" | |
12 #include "content/public/test/test_utils.h" | |
13 #include "net/cookies/cookie_store.h" | |
14 #include "net/url_request/url_request_context.h" | |
15 #include "net/url_request/url_request_context_getter.h" | |
16 | |
17 namespace { | |
18 | |
19 const char kCookieDefinition[] = "A=1"; | |
20 const char kCookieHostname[] = "http://host1"; | |
21 | |
22 using content::BrowserThread; | |
23 | |
24 // RemoveCookieTester provides the user with the ability to set and get a | |
25 // cookie for given profile. | |
26 class RemoveCookieTester { | |
27 public: | |
28 explicit RemoveCookieTester(Profile* profile); | |
29 ~RemoveCookieTester(); | |
30 | |
31 std::string GetCookie(const std::string& host); | |
32 void AddCookie(const std::string& host, const std::string& definition); | |
33 | |
34 private: | |
35 void GetCookieOnIOThread(net::URLRequestContextGetter* context_getter, | |
36 const std::string& host); | |
37 void SetCookieOnIOThread(net::URLRequestContextGetter* context_getter, | |
38 const std::string& host, | |
39 const std::string& definition); | |
40 void GetCookieCallback(const std::string& cookies); | |
41 void SetCookieCallback(bool result); | |
42 | |
43 void BlockUntilNotified(); | |
44 void Notify(); | |
45 | |
46 std::string last_cookies_; | |
47 bool waiting_callback_; | |
48 Profile* profile_; | |
49 scoped_refptr<content::MessageLoopRunner> runner_; | |
50 | |
51 DISALLOW_COPY_AND_ASSIGN(RemoveCookieTester); | |
52 }; | |
53 | |
54 RemoveCookieTester::RemoveCookieTester(Profile* profile) | |
55 : waiting_callback_(false), | |
56 profile_(profile) { | |
57 } | |
58 | |
59 RemoveCookieTester::~RemoveCookieTester() {} | |
60 | |
61 // Returns true, if the given cookie exists in the cookie store. | |
62 std::string RemoveCookieTester::GetCookie(const std::string& host) { | |
63 last_cookies_.clear(); | |
battre
2013/06/21 04:54:56
DCHECK(!waiting_callback_); ?
vasilii
2013/06/21 07:44:17
Done.
| |
64 waiting_callback_ = true; | |
65 BrowserThread::PostTask( | |
66 BrowserThread::IO, FROM_HERE, | |
67 base::Bind(&RemoveCookieTester::GetCookieOnIOThread, | |
68 base::Unretained(this), | |
69 base::Unretained(profile_->GetRequestContext()), | |
70 host)); | |
71 BlockUntilNotified(); | |
72 return last_cookies_; | |
73 } | |
74 | |
75 void RemoveCookieTester::AddCookie(const std::string& host, | |
76 const std::string& definition) { | |
77 waiting_callback_ = true; | |
78 BrowserThread::PostTask( | |
79 BrowserThread::IO, FROM_HERE, | |
80 base::Bind(&RemoveCookieTester::SetCookieOnIOThread, | |
81 base::Unretained(this), | |
82 base::Unretained(profile_->GetRequestContext()), | |
83 host, | |
84 definition)); | |
85 BlockUntilNotified(); | |
86 } | |
87 | |
88 void RemoveCookieTester::GetCookieOnIOThread( | |
89 net::URLRequestContextGetter* context_getter, | |
90 const std::string& host) { | |
91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
92 net::CookieStore* cookie_store = context_getter-> | |
93 GetURLRequestContext()->cookie_store(); | |
94 cookie_store->GetCookiesWithOptionsAsync( | |
95 GURL(host), net::CookieOptions(), | |
96 base::Bind(&RemoveCookieTester::GetCookieCallback, | |
97 base::Unretained(this))); | |
98 } | |
99 | |
100 void RemoveCookieTester::SetCookieOnIOThread( | |
101 net::URLRequestContextGetter* context_getter, | |
102 const std::string& host, | |
103 const std::string& definition) { | |
104 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
105 net::CookieStore* cookie_store = context_getter-> | |
106 GetURLRequestContext()->cookie_store(); | |
107 cookie_store->SetCookieWithOptionsAsync( | |
108 GURL(host), definition, net::CookieOptions(), | |
109 base::Bind(&RemoveCookieTester::SetCookieCallback, | |
110 base::Unretained(this))); | |
111 } | |
112 | |
113 void RemoveCookieTester::GetCookieCallback(const std::string& cookies) { | |
114 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | |
115 BrowserThread::PostTask( | |
116 BrowserThread::UI, FROM_HERE, | |
117 base::Bind(&RemoveCookieTester::GetCookieCallback, | |
118 base::Unretained(this), cookies)); | |
119 return; | |
120 } | |
121 last_cookies_ = cookies; | |
122 Notify(); | |
123 } | |
124 | |
125 void RemoveCookieTester::SetCookieCallback(bool result) { | |
126 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | |
127 BrowserThread::PostTask( | |
128 BrowserThread::UI, FROM_HERE, | |
129 base::Bind(&RemoveCookieTester::SetCookieCallback, | |
130 base::Unretained(this), result)); | |
131 return; | |
132 } | |
133 ASSERT_TRUE(result); | |
134 Notify(); | |
135 } | |
136 | |
137 void RemoveCookieTester::BlockUntilNotified() { | |
138 DCHECK(!runner_); | |
139 if (waiting_callback_) { | |
140 runner_ = new content::MessageLoopRunner; | |
141 runner_->Run(); | |
142 runner_ = NULL; | |
143 } | |
144 } | |
145 | |
146 void RemoveCookieTester::Notify() { | |
147 DCHECK(waiting_callback_); | |
148 waiting_callback_ = false; | |
149 if (runner_) | |
150 runner_->Quit(); | |
151 } | |
152 | |
153 class ProfileResetTest : public InProcessBrowserTest, | |
154 public ProfileResetterTestBase { | |
155 protected: | |
156 virtual void SetUpOnMainThread() OVERRIDE { | |
157 resetter_.reset(new ProfileResetter(browser()->profile())); | |
158 } | |
159 }; | |
160 | |
161 | |
162 IN_PROC_BROWSER_TEST_F(ProfileResetTest, ResetCookiesAndSiteData) { | |
163 RemoveCookieTester tester(browser()->profile()); | |
164 tester.AddCookie(kCookieHostname, kCookieDefinition); | |
165 ASSERT_EQ(kCookieDefinition, tester.GetCookie(kCookieHostname)); | |
166 | |
167 resetter_->Reset(ProfileResetter::COOKIES_AND_SITE_DATA, | |
168 base::Bind(&ProfileResetterMockObject::StopLoop, | |
169 base::Unretained(&mock_object_))); | |
170 mock_object_.RunLoop(); | |
171 | |
172 EXPECT_EQ("", tester.GetCookie(kCookieHostname)); | |
173 } | |
174 | |
175 } // namespace | |
OLD | NEW |