1 // Copyright 2012 The Chromium Authors
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 "net/cookies/cookie_store_test_helpers.h"
6
7 #include <string>
8 #include <utility>
9
10 #include "base/functional/bind.h"
11 #include "base/location.h"
12 #include "base/strings/string_util.h"
13 #include "base/task/single_thread_task_runner.h"
14 #include "base/time/time.h"
15 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
16 #include "net/cookies/cookie_store.h"
17 #include "net/cookies/cookie_util.h"
18 #include "third_party/abseil-cpp/absl/types/optional.h"
19 #include "url/gurl.h"
20
21 using net::registry_controlled_domains::GetDomainAndRegistry;
22 using net::registry_controlled_domains::GetRegistryLength;
23 using net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES;
24 using net::registry_controlled_domains::INCLUDE_UNKNOWN_REGISTRIES;
25 using TimeRange = net::CookieDeletionInfo::TimeRange;
26
27 namespace {
28
GetRegistry(const GURL & url)29 std::string GetRegistry(const GURL& url) {
30 size_t registry_length = GetRegistryLength(url, INCLUDE_UNKNOWN_REGISTRIES,
31 INCLUDE_PRIVATE_REGISTRIES);
32 if (registry_length == 0)
33 return std::string();
34 return std::string(url.host(), url.host().length() - registry_length,
35 registry_length);
36 }
37
38 } // namespace
39
40 namespace net {
41
42 const int kDelayedTime = 0;
43
44 DelayedCookieMonsterChangeDispatcher::DelayedCookieMonsterChangeDispatcher() =
45 default;
46 DelayedCookieMonsterChangeDispatcher::~DelayedCookieMonsterChangeDispatcher() =
47 default;
48
49 std::unique_ptr<CookieChangeSubscription>
AddCallbackForCookie(const GURL & url,const std::string & name,const absl::optional<CookiePartitionKey> & cookie_partition_key,CookieChangeCallback callback)50 DelayedCookieMonsterChangeDispatcher::AddCallbackForCookie(
51 const GURL& url,
52 const std::string& name,
53 const absl::optional<CookiePartitionKey>& cookie_partition_key,
54 CookieChangeCallback callback) {
55 ADD_FAILURE();
56 return nullptr;
57 }
58 std::unique_ptr<CookieChangeSubscription>
AddCallbackForUrl(const GURL & url,const absl::optional<CookiePartitionKey> & cookie_partition_key,CookieChangeCallback callback)59 DelayedCookieMonsterChangeDispatcher::AddCallbackForUrl(
60 const GURL& url,
61 const absl::optional<CookiePartitionKey>& cookie_partition_key,
62 CookieChangeCallback callback) {
63 ADD_FAILURE();
64 return nullptr;
65 }
66 std::unique_ptr<CookieChangeSubscription>
AddCallbackForAllChanges(CookieChangeCallback callback)67 DelayedCookieMonsterChangeDispatcher::AddCallbackForAllChanges(
68 CookieChangeCallback callback) {
69 ADD_FAILURE();
70 return nullptr;
71 }
72
DelayedCookieMonster()73 DelayedCookieMonster::DelayedCookieMonster()
74 : cookie_monster_(std::make_unique<CookieMonster>(nullptr /* store */,
75 nullptr /* netlog */)),
76 result_(CookieAccessResult(CookieInclusionStatus(
77 CookieInclusionStatus::EXCLUDE_FAILURE_TO_STORE))) {}
78
79 DelayedCookieMonster::~DelayedCookieMonster() = default;
80
SetCookiesInternalCallback(CookieAccessResult result)81 void DelayedCookieMonster::SetCookiesInternalCallback(
82 CookieAccessResult result) {
83 result_ = result;
84 did_run_ = true;
85 }
86
GetCookieListWithOptionsInternalCallback(const CookieAccessResultList & cookie_list,const CookieAccessResultList & excluded_cookies)87 void DelayedCookieMonster::GetCookieListWithOptionsInternalCallback(
88 const CookieAccessResultList& cookie_list,
89 const CookieAccessResultList& excluded_cookies) {
90 cookie_access_result_list_ = cookie_list;
91 cookie_list_ = cookie_util::StripAccessResults(cookie_access_result_list_);
92 did_run_ = true;
93 }
94
SetCanonicalCookieAsync(std::unique_ptr<CanonicalCookie> cookie,const GURL & source_url,const CookieOptions & options,SetCookiesCallback callback,absl::optional<CookieAccessResult> cookie_access_result)95 void DelayedCookieMonster::SetCanonicalCookieAsync(
96 std::unique_ptr<CanonicalCookie> cookie,
97 const GURL& source_url,
98 const CookieOptions& options,
99 SetCookiesCallback callback,
100 absl::optional<CookieAccessResult> cookie_access_result) {
101 did_run_ = false;
102 cookie_monster_->SetCanonicalCookieAsync(
103 std::move(cookie), source_url, options,
104 base::BindOnce(&DelayedCookieMonster::SetCookiesInternalCallback,
105 base::Unretained(this)),
106 std::move(cookie_access_result));
107 DCHECK_EQ(did_run_, true);
108 base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
109 FROM_HERE,
110 base::BindOnce(&DelayedCookieMonster::InvokeSetCookiesCallback,
111 base::Unretained(this), std::move(callback)),
112 base::Milliseconds(kDelayedTime));
113 }
114
GetCookieListWithOptionsAsync(const GURL & url,const CookieOptions & options,const CookiePartitionKeyCollection & cookie_partition_key_collection,CookieMonster::GetCookieListCallback callback)115 void DelayedCookieMonster::GetCookieListWithOptionsAsync(
116 const GURL& url,
117 const CookieOptions& options,
118 const CookiePartitionKeyCollection& cookie_partition_key_collection,
119 CookieMonster::GetCookieListCallback callback) {
120 did_run_ = false;
121 cookie_monster_->GetCookieListWithOptionsAsync(
122 url, options, cookie_partition_key_collection,
123 base::BindOnce(
124 &DelayedCookieMonster::GetCookieListWithOptionsInternalCallback,
125 base::Unretained(this)));
126 DCHECK_EQ(did_run_, true);
127 base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
128 FROM_HERE,
129 base::BindOnce(&DelayedCookieMonster::InvokeGetCookieListCallback,
130 base::Unretained(this), std::move(callback)),
131 base::Milliseconds(kDelayedTime));
132 }
133
GetAllCookiesAsync(GetAllCookiesCallback callback)134 void DelayedCookieMonster::GetAllCookiesAsync(GetAllCookiesCallback callback) {
135 cookie_monster_->GetAllCookiesAsync(std::move(callback));
136 }
137
InvokeSetCookiesCallback(CookieMonster::SetCookiesCallback callback)138 void DelayedCookieMonster::InvokeSetCookiesCallback(
139 CookieMonster::SetCookiesCallback callback) {
140 if (!callback.is_null())
141 std::move(callback).Run(result_);
142 }
143
InvokeGetCookieListCallback(CookieMonster::GetCookieListCallback callback)144 void DelayedCookieMonster::InvokeGetCookieListCallback(
145 CookieMonster::GetCookieListCallback callback) {
146 if (!callback.is_null())
147 std::move(callback).Run(cookie_access_result_list_,
148 CookieAccessResultList());
149 }
150
DeleteCanonicalCookieAsync(const CanonicalCookie & cookie,DeleteCallback callback)151 void DelayedCookieMonster::DeleteCanonicalCookieAsync(
152 const CanonicalCookie& cookie,
153 DeleteCallback callback) {
154 ADD_FAILURE();
155 }
156
DeleteAllCreatedInTimeRangeAsync(const TimeRange & creation_range,DeleteCallback callback)157 void DelayedCookieMonster::DeleteAllCreatedInTimeRangeAsync(
158 const TimeRange& creation_range,
159 DeleteCallback callback) {
160 ADD_FAILURE();
161 }
162
DeleteAllMatchingInfoAsync(net::CookieDeletionInfo delete_info,DeleteCallback callback)163 void DelayedCookieMonster::DeleteAllMatchingInfoAsync(
164 net::CookieDeletionInfo delete_info,
165 DeleteCallback callback) {
166 ADD_FAILURE();
167 }
168
DeleteMatchingCookiesAsync(DeletePredicate,DeleteCallback)169 void DelayedCookieMonster::DeleteMatchingCookiesAsync(DeletePredicate,
170 DeleteCallback) {
171 ADD_FAILURE();
172 }
173
DeleteSessionCookiesAsync(DeleteCallback)174 void DelayedCookieMonster::DeleteSessionCookiesAsync(DeleteCallback) {
175 ADD_FAILURE();
176 }
177
FlushStore(base::OnceClosure callback)178 void DelayedCookieMonster::FlushStore(base::OnceClosure callback) {
179 ADD_FAILURE();
180 }
181
GetChangeDispatcher()182 CookieChangeDispatcher& DelayedCookieMonster::GetChangeDispatcher() {
183 return change_dispatcher_;
184 }
185
SetCookieableSchemes(const std::vector<std::string> & schemes,SetCookieableSchemesCallback callback)186 void DelayedCookieMonster::SetCookieableSchemes(
187 const std::vector<std::string>& schemes,
188 SetCookieableSchemesCallback callback) {
189 ADD_FAILURE();
190 }
191
192 //
193 // CookieURLHelper
194 //
CookieURLHelper(const std::string & url_string)195 CookieURLHelper::CookieURLHelper(const std::string& url_string)
196 : url_(url_string),
197 registry_(GetRegistry(url_)),
198 domain_and_registry_(
199 GetDomainAndRegistry(url_, INCLUDE_PRIVATE_REGISTRIES)) {}
200
AppendPath(const std::string & path) const201 const GURL CookieURLHelper::AppendPath(const std::string& path) const {
202 return GURL(url_.spec() + path);
203 }
204
Format(const std::string & format_string) const205 std::string CookieURLHelper::Format(const std::string& format_string) const {
206 std::string new_string = format_string;
207 base::ReplaceSubstringsAfterOffset(&new_string, 0, "%D",
208 domain_and_registry_);
209 base::ReplaceSubstringsAfterOffset(&new_string, 0, "%R", registry_);
210 return new_string;
211 }
212
213 //
214 // FlushablePersistentStore
215 //
216 FlushablePersistentStore::FlushablePersistentStore() = default;
217
Load(LoadedCallback loaded_callback,const NetLogWithSource &)218 void FlushablePersistentStore::Load(LoadedCallback loaded_callback,
219 const NetLogWithSource& /* net_log */) {
220 std::vector<std::unique_ptr<CanonicalCookie>> out_cookies;
221 base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
222 FROM_HERE,
223 base::BindOnce(std::move(loaded_callback), std::move(out_cookies)));
224 }
225
LoadCookiesForKey(const std::string & key,LoadedCallback loaded_callback)226 void FlushablePersistentStore::LoadCookiesForKey(
227 const std::string& key,
228 LoadedCallback loaded_callback) {
229 Load(std::move(loaded_callback), NetLogWithSource());
230 }
231
AddCookie(const CanonicalCookie &)232 void FlushablePersistentStore::AddCookie(const CanonicalCookie&) {}
233
UpdateCookieAccessTime(const CanonicalCookie &)234 void FlushablePersistentStore::UpdateCookieAccessTime(const CanonicalCookie&) {}
235
DeleteCookie(const CanonicalCookie &)236 void FlushablePersistentStore::DeleteCookie(const CanonicalCookie&) {}
237
SetForceKeepSessionState()238 void FlushablePersistentStore::SetForceKeepSessionState() {}
239
SetBeforeCommitCallback(base::RepeatingClosure callback)240 void FlushablePersistentStore::SetBeforeCommitCallback(
241 base::RepeatingClosure callback) {}
242
Flush(base::OnceClosure callback)243 void FlushablePersistentStore::Flush(base::OnceClosure callback) {
244 base::AutoLock lock(flush_count_lock_);
245 ++flush_count_;
246 std::move(callback).Run();
247 }
248
flush_count()249 int FlushablePersistentStore::flush_count() {
250 base::AutoLock lock(flush_count_lock_);
251 return flush_count_;
252 }
253
254 FlushablePersistentStore::~FlushablePersistentStore() = default;
255
256 //
257 // CallbackCounter
258 //
259 CallbackCounter::CallbackCounter() = default;
260
Callback()261 void CallbackCounter::Callback() {
262 base::AutoLock lock(callback_count_lock_);
263 ++callback_count_;
264 }
265
callback_count()266 int CallbackCounter::callback_count() {
267 base::AutoLock lock(callback_count_lock_);
268 return callback_count_;
269 }
270
271 CallbackCounter::~CallbackCounter() = default;
272
FutureCookieExpirationString()273 std::string FutureCookieExpirationString() {
274 return "; expires=" +
275 base::TimeFormatHTTP(base::Time::Now() + base::Days(365));
276 }
277
278 } // namespace net
279