OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/cookies/cookie_monster_store_test.h" | 5 #include "net/cookies/cookie_monster_store_test.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
12 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
14 #include "net/cookies/cookie_constants.h" | 14 #include "net/cookies/cookie_constants.h" |
15 #include "net/cookies/cookie_util.h" | 15 #include "net/cookies/cookie_util.h" |
16 #include "net/cookies/parsed_cookie.h" | 16 #include "net/cookies/parsed_cookie.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 #include "url/gurl.h" | 18 #include "url/gurl.h" |
19 | 19 |
20 namespace net { | 20 namespace net { |
21 | 21 |
22 LoadedCallbackTask::LoadedCallbackTask(LoadedCallback loaded_callback, | 22 LoadedCallbackTask::LoadedCallbackTask( |
23 std::vector<CanonicalCookie*> cookies) | 23 LoadedCallback loaded_callback, |
24 : loaded_callback_(loaded_callback), cookies_(cookies) { | 24 std::vector<std::unique_ptr<CanonicalCookie>> cookies) |
25 } | 25 : loaded_callback_(loaded_callback), cookies_(std::move(cookies)) {} |
26 | 26 |
27 LoadedCallbackTask::~LoadedCallbackTask() { | 27 LoadedCallbackTask::~LoadedCallbackTask() { |
28 } | 28 } |
29 | 29 |
30 CookieStoreCommand::CookieStoreCommand( | 30 CookieStoreCommand::CookieStoreCommand( |
31 Type type, | 31 Type type, |
32 const CookieMonster::PersistentCookieStore::LoadedCallback& loaded_callback, | 32 const CookieMonster::PersistentCookieStore::LoadedCallback& loaded_callback, |
33 const std::string& key) | 33 const std::string& key) |
34 : type(type), loaded_callback(loaded_callback), key(key) {} | 34 : type(type), loaded_callback(loaded_callback), key(key) {} |
35 | 35 |
36 CookieStoreCommand::CookieStoreCommand(Type type, const CanonicalCookie& cookie) | 36 CookieStoreCommand::CookieStoreCommand(Type type, const CanonicalCookie& cookie) |
37 : type(type), cookie(cookie) {} | 37 : type(type), cookie(cookie) {} |
38 | 38 |
39 CookieStoreCommand::CookieStoreCommand(const CookieStoreCommand& other) = | 39 CookieStoreCommand::CookieStoreCommand(const CookieStoreCommand& other) = |
40 default; | 40 default; |
41 | 41 |
42 CookieStoreCommand::~CookieStoreCommand() {} | 42 CookieStoreCommand::~CookieStoreCommand() {} |
43 | 43 |
44 MockPersistentCookieStore::MockPersistentCookieStore() | 44 MockPersistentCookieStore::MockPersistentCookieStore() |
45 : store_load_commands_(false), load_return_value_(true), loaded_(false) {} | 45 : store_load_commands_(false), load_return_value_(true), loaded_(false) {} |
46 | 46 |
47 void MockPersistentCookieStore::SetLoadExpectation( | 47 void MockPersistentCookieStore::SetLoadExpectation( |
48 bool return_value, | 48 bool return_value, |
49 const std::vector<CanonicalCookie*>& result) { | 49 std::vector<std::unique_ptr<CanonicalCookie>> result) { |
50 load_return_value_ = return_value; | 50 load_return_value_ = return_value; |
51 load_result_ = result; | 51 load_result_.swap(result); |
52 } | 52 } |
53 | 53 |
54 void MockPersistentCookieStore::Load(const LoadedCallback& loaded_callback) { | 54 void MockPersistentCookieStore::Load(const LoadedCallback& loaded_callback) { |
55 if (store_load_commands_) { | 55 if (store_load_commands_) { |
56 commands_.push_back( | 56 commands_.push_back( |
57 CookieStoreCommand(CookieStoreCommand::LOAD, loaded_callback, "")); | 57 CookieStoreCommand(CookieStoreCommand::LOAD, loaded_callback, "")); |
58 return; | 58 return; |
59 } | 59 } |
60 std::vector<CanonicalCookie*> out_cookies; | 60 std::vector<std::unique_ptr<CanonicalCookie>> out_cookies; |
61 if (load_return_value_) { | 61 if (load_return_value_) { |
62 out_cookies = load_result_; | 62 out_cookies.swap(load_result_); |
63 loaded_ = true; | 63 loaded_ = true; |
64 } | 64 } |
65 base::ThreadTaskRunnerHandle::Get()->PostTask( | 65 base::ThreadTaskRunnerHandle::Get()->PostTask( |
66 FROM_HERE, | 66 FROM_HERE, base::Bind(&LoadedCallbackTask::Run, |
67 base::Bind(&LoadedCallbackTask::Run, | 67 new LoadedCallbackTask(loaded_callback, |
68 new LoadedCallbackTask(loaded_callback, out_cookies))); | 68 std::move(out_cookies)))); |
69 } | 69 } |
70 | 70 |
71 void MockPersistentCookieStore::LoadCookiesForKey( | 71 void MockPersistentCookieStore::LoadCookiesForKey( |
72 const std::string& key, | 72 const std::string& key, |
73 const LoadedCallback& loaded_callback) { | 73 const LoadedCallback& loaded_callback) { |
74 if (store_load_commands_) { | 74 if (store_load_commands_) { |
75 commands_.push_back(CookieStoreCommand( | 75 commands_.push_back(CookieStoreCommand( |
76 CookieStoreCommand::LOAD_COOKIES_FOR_KEY, loaded_callback, key)); | 76 CookieStoreCommand::LOAD_COOKIES_FOR_KEY, loaded_callback, key)); |
77 return; | 77 return; |
78 } | 78 } |
79 if (!loaded_) { | 79 if (!loaded_) { |
80 Load(loaded_callback); | 80 Load(loaded_callback); |
81 } else { | 81 } else { |
82 base::ThreadTaskRunnerHandle::Get()->PostTask( | 82 base::ThreadTaskRunnerHandle::Get()->PostTask( |
83 FROM_HERE, | 83 FROM_HERE, |
84 base::Bind(&LoadedCallbackTask::Run, | 84 base::Bind(&LoadedCallbackTask::Run, |
85 new LoadedCallbackTask(loaded_callback, | 85 new LoadedCallbackTask( |
86 std::vector<CanonicalCookie*>()))); | 86 loaded_callback, |
| 87 std::vector<std::unique_ptr<CanonicalCookie>>()))); |
87 } | 88 } |
88 } | 89 } |
89 | 90 |
90 void MockPersistentCookieStore::AddCookie(const CanonicalCookie& cookie) { | 91 void MockPersistentCookieStore::AddCookie(const CanonicalCookie& cookie) { |
91 commands_.push_back(CookieStoreCommand(CookieStoreCommand::ADD, cookie)); | 92 commands_.push_back(CookieStoreCommand(CookieStoreCommand::ADD, cookie)); |
92 } | 93 } |
93 | 94 |
94 void MockPersistentCookieStore::UpdateCookieAccessTime( | 95 void MockPersistentCookieStore::UpdateCookieAccessTime( |
95 const CanonicalCookie& cookie) { | 96 const CanonicalCookie& cookie) { |
96 } | 97 } |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 | 145 |
145 return CanonicalCookie::Create(url, pc.Name(), pc.Value(), url.host(), | 146 return CanonicalCookie::Create(url, pc.Name(), pc.Value(), url.host(), |
146 cookie_path, creation_time, cookie_expires, | 147 cookie_path, creation_time, cookie_expires, |
147 pc.IsSecure(), pc.IsHttpOnly(), pc.SameSite(), | 148 pc.IsSecure(), pc.IsHttpOnly(), pc.SameSite(), |
148 false, pc.Priority()); | 149 false, pc.Priority()); |
149 } | 150 } |
150 | 151 |
151 void AddCookieToList(const GURL& url, | 152 void AddCookieToList(const GURL& url, |
152 const std::string& cookie_line, | 153 const std::string& cookie_line, |
153 const base::Time& creation_time, | 154 const base::Time& creation_time, |
154 std::vector<CanonicalCookie*>* out_list) { | 155 std::vector<std::unique_ptr<CanonicalCookie>>* out_list) { |
155 std::unique_ptr<CanonicalCookie> cookie( | 156 std::unique_ptr<CanonicalCookie> cookie( |
156 BuildCanonicalCookie(url, cookie_line, creation_time)); | 157 BuildCanonicalCookie(url, cookie_line, creation_time)); |
157 | 158 |
158 out_list->push_back(cookie.release()); | 159 out_list->push_back(std::move(cookie)); |
159 } | 160 } |
160 | 161 |
161 MockSimplePersistentCookieStore::MockSimplePersistentCookieStore() | 162 MockSimplePersistentCookieStore::MockSimplePersistentCookieStore() |
162 : loaded_(false) { | 163 : loaded_(false) { |
163 } | 164 } |
164 | 165 |
165 void MockSimplePersistentCookieStore::Load( | 166 void MockSimplePersistentCookieStore::Load( |
166 const LoadedCallback& loaded_callback) { | 167 const LoadedCallback& loaded_callback) { |
167 std::vector<CanonicalCookie*> out_cookies; | 168 std::vector<std::unique_ptr<CanonicalCookie>> out_cookies; |
168 | 169 |
169 for (CanonicalCookieMap::const_iterator it = cookies_.begin(); | 170 for (auto it = cookies_.begin(); it != cookies_.end(); it++) |
170 it != cookies_.end(); it++) | 171 out_cookies.push_back(base::MakeUnique<CanonicalCookie>(it->second)); |
171 out_cookies.push_back(new CanonicalCookie(it->second)); | |
172 | 172 |
173 base::ThreadTaskRunnerHandle::Get()->PostTask( | 173 base::ThreadTaskRunnerHandle::Get()->PostTask( |
174 FROM_HERE, | 174 FROM_HERE, base::Bind(&LoadedCallbackTask::Run, |
175 base::Bind(&LoadedCallbackTask::Run, | 175 new LoadedCallbackTask(loaded_callback, |
176 new LoadedCallbackTask(loaded_callback, out_cookies))); | 176 std::move(out_cookies)))); |
177 loaded_ = true; | 177 loaded_ = true; |
178 } | 178 } |
179 | 179 |
180 void MockSimplePersistentCookieStore::LoadCookiesForKey( | 180 void MockSimplePersistentCookieStore::LoadCookiesForKey( |
181 const std::string& key, | 181 const std::string& key, |
182 const LoadedCallback& loaded_callback) { | 182 const LoadedCallback& loaded_callback) { |
183 if (!loaded_) { | 183 if (!loaded_) { |
184 Load(loaded_callback); | 184 Load(loaded_callback); |
185 } else { | 185 } else { |
186 base::ThreadTaskRunnerHandle::Get()->PostTask( | 186 base::ThreadTaskRunnerHandle::Get()->PostTask( |
187 FROM_HERE, | 187 FROM_HERE, |
188 base::Bind(&LoadedCallbackTask::Run, | 188 base::Bind(&LoadedCallbackTask::Run, |
189 new LoadedCallbackTask(loaded_callback, | 189 new LoadedCallbackTask( |
190 std::vector<CanonicalCookie*>()))); | 190 loaded_callback, |
| 191 std::vector<std::unique_ptr<CanonicalCookie>>()))); |
191 } | 192 } |
192 } | 193 } |
193 | 194 |
194 void MockSimplePersistentCookieStore::AddCookie(const CanonicalCookie& cookie) { | 195 void MockSimplePersistentCookieStore::AddCookie(const CanonicalCookie& cookie) { |
195 int64_t creation_time = cookie.CreationDate().ToInternalValue(); | 196 int64_t creation_time = cookie.CreationDate().ToInternalValue(); |
196 EXPECT_TRUE(cookies_.find(creation_time) == cookies_.end()); | 197 EXPECT_TRUE(cookies_.find(creation_time) == cookies_.end()); |
197 cookies_[creation_time] = cookie; | 198 cookies_[creation_time] = cookie; |
198 } | 199 } |
199 | 200 |
200 void MockSimplePersistentCookieStore::UpdateCookieAccessTime( | 201 void MockSimplePersistentCookieStore::UpdateCookieAccessTime( |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 store->AddCookie(*cc); | 261 store->AddCookie(*cc); |
261 } | 262 } |
262 | 263 |
263 return base::MakeUnique<CookieMonster>(store.get(), nullptr); | 264 return base::MakeUnique<CookieMonster>(store.get(), nullptr); |
264 } | 265 } |
265 | 266 |
266 MockSimplePersistentCookieStore::~MockSimplePersistentCookieStore() { | 267 MockSimplePersistentCookieStore::~MockSimplePersistentCookieStore() { |
267 } | 268 } |
268 | 269 |
269 } // namespace net | 270 } // namespace net |
OLD | NEW |