1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "android_webview/browser/net/aw_cookie_store_wrapper.h"
#include <string>
#include "android_webview/browser/net/init_native_callback.h"
#include "base/memory/ref_counted_delete_on_message_loop.h"
#include "base/thread_task_runner_handle.h"
#include "url/gurl.h"
namespace android_webview {
namespace {
// Posts |task| to the thread that the global CookieStore lives on.
void PostTaskToCookieStoreTaskRunner(const base::Closure& task) {
GetCookieStoreTaskRunner()->PostTask(FROM_HERE, task);
}
// Wraps a subscription to cookie change notifications for the global
// CookieStore for a consumer that lives on another thread. Handles passing
// messages between thread, and destroys itself when the consumer unsubscribes.
// Must be created on the consumer's thread. Each instance only supports a
// single subscription.
class SubscriptionWrapper {
public:
SubscriptionWrapper() : weak_factory_(this) {}
scoped_ptr<net::CookieStore::CookieChangedSubscription> Subscribe(
const GURL& url,
const std::string& name,
const net::CookieStore::CookieChangedCallback& callback) {
// This class is only intended to be used for a single subscription.
DCHECK(callback_list_.empty());
nested_subscription_ =
new NestedSubscription(url, name, weak_factory_.GetWeakPtr());
return callback_list_.Add(callback);
}
private:
// The NestedSubscription is responsible for creating and managing the
// underlying subscription to the real CookieStore, and posting notifications
// back to |callback_list_|.
class NestedSubscription
: public base::RefCountedDeleteOnMessageLoop<NestedSubscription> {
public:
NestedSubscription(const GURL& url,
const std::string& name,
base::WeakPtr<SubscriptionWrapper> subscription_wrapper)
: base::RefCountedDeleteOnMessageLoop<NestedSubscription>(
GetCookieStoreTaskRunner()),
subscription_wrapper_(subscription_wrapper),
client_task_runner_(base::ThreadTaskRunnerHandle::Get()) {
PostTaskToCookieStoreTaskRunner(
base::Bind(&NestedSubscription::Subscribe, this, url, name));
}
private:
friend class base::RefCountedDeleteOnMessageLoop<NestedSubscription>;
friend class base::DeleteHelper<NestedSubscription>;
~NestedSubscription() {}
void Subscribe(const GURL& url, const std::string& name) {
GetCookieStore()->AddCallbackForCookie(
url, name, base::Bind(&NestedSubscription::OnChanged, this));
}
void OnChanged(const net::CanonicalCookie& cookie, bool removed) {
client_task_runner_->PostTask(
FROM_HERE, base::Bind(&SubscriptionWrapper::OnChanged,
subscription_wrapper_, cookie, removed));
}
base::WeakPtr<SubscriptionWrapper> subscription_wrapper_;
scoped_refptr<base::TaskRunner> client_task_runner_;
scoped_ptr<net::CookieStore::CookieChangedSubscription> subscription_;
DISALLOW_COPY_AND_ASSIGN(NestedSubscription);
};
void OnChanged(const net::CanonicalCookie& cookie, bool removed) {
callback_list_.Notify(cookie, removed);
}
// The "list" only had one entry, so can just clean up now.
void OnUnsubscribe() { delete this; }
scoped_refptr<NestedSubscription> nested_subscription_;
net::CookieStore::CookieChangedCallbackList callback_list_;
base::WeakPtrFactory<SubscriptionWrapper> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(SubscriptionWrapper);
};
void SetCookieWithOptionsAsyncOnCookieThread(
const GURL& url,
const std::string& cookie_line,
const net::CookieOptions& options,
const net::CookieStore::SetCookiesCallback& callback) {
GetCookieStore()->SetCookieWithOptionsAsync(url, cookie_line, options,
callback);
}
void SetCookieWithDetailsAsyncOnCookieThread(
const GURL& url,
const std::string& name,
const std::string& value,
const std::string& domain,
const std::string& path,
base::Time creation_time,
base::Time expiration_time,
base::Time last_access_time,
bool secure,
bool http_only,
net::CookieSameSite same_site,
bool enforce_strict_secure,
net::CookiePriority priority,
const net::CookieStore::SetCookiesCallback& callback) {
GetCookieStore()->SetCookieWithDetailsAsync(
url, name, value, domain, path, creation_time, expiration_time,
last_access_time, secure, http_only, same_site, enforce_strict_secure,
priority, callback);
}
void GetCookiesWithOptionsAsyncOnCookieThread(
const GURL& url,
const net::CookieOptions& options,
const net::CookieStore::GetCookiesCallback& callback) {
GetCookieStore()->GetCookiesWithOptionsAsync(url, options, callback);
}
void GetCookieListWithOptionsAsyncOnCookieThread(
const GURL& url,
const net::CookieOptions& options,
const net::CookieStore::GetCookieListCallback& callback) {
GetCookieStore()->GetCookieListWithOptionsAsync(url, options, callback);
}
void GetAllCookiesAsyncOnCookieThread(
const net::CookieStore::GetCookieListCallback& callback) {
GetCookieStore()->GetAllCookiesAsync(callback);
}
void DeleteCookieAsyncOnCookieThread(const GURL& url,
const std::string& cookie_name,
const base::Closure& callback) {
GetCookieStore()->DeleteCookieAsync(url, cookie_name, callback);
}
void DeleteCanonicalCookieAsyncOnCookieThread(
const net::CanonicalCookie& cookie,
const net::CookieStore::DeleteCallback& callback) {
GetCookieStore()->DeleteCanonicalCookieAsync(cookie, callback);
}
void DeleteAllCreatedBetweenAsyncOnCookieThread(
const base::Time& delete_begin,
const base::Time& delete_end,
const net::CookieStore::DeleteCallback& callback) {
GetCookieStore()->DeleteAllCreatedBetweenAsync(delete_begin, delete_end,
callback);
}
void DeleteAllCreatedBetweenForHostAsyncOnCookieThread(
const base::Time delete_begin,
const base::Time delete_end,
const GURL& url,
const net::CookieStore::DeleteCallback& callback) {
GetCookieStore()->DeleteAllCreatedBetweenForHostAsync(
delete_begin, delete_end, url, callback);
}
void DeleteSessionCookiesAsyncOnCookieThread(
const net::CookieStore::DeleteCallback& callback) {
GetCookieStore()->DeleteSessionCookiesAsync(callback);
}
void FlushStoreOnCookieThread(const base::Closure& callback) {
GetCookieStore()->FlushStore(callback);
}
void SetForceKeepSessionStateOnCookieThread() {
GetCookieStore()->SetForceKeepSessionState();
}
} // namespace
AwCookieStoreWrapper::AwCookieStoreWrapper()
: client_task_runner_(base::ThreadTaskRunnerHandle::Get()),
weak_factory_(this) {}
AwCookieStoreWrapper::~AwCookieStoreWrapper() {}
void AwCookieStoreWrapper::SetCookieWithOptionsAsync(
const GURL& url,
const std::string& cookie_line,
const net::CookieOptions& options,
const net::CookieStore::SetCookiesCallback& callback) {
DCHECK(client_task_runner_->RunsTasksOnCurrentThread());
PostTaskToCookieStoreTaskRunner(
base::Bind(&SetCookieWithOptionsAsyncOnCookieThread, url, cookie_line,
options, CreateWrappedCallback<bool>(callback)));
}
void AwCookieStoreWrapper::SetCookieWithDetailsAsync(
const GURL& url,
const std::string& name,
const std::string& value,
const std::string& domain,
const std::string& path,
base::Time creation_time,
base::Time expiration_time,
base::Time last_access_time,
bool secure,
bool http_only,
net::CookieSameSite same_site,
bool enforce_strict_secure,
net::CookiePriority priority,
const SetCookiesCallback& callback) {
DCHECK(client_task_runner_->RunsTasksOnCurrentThread());
PostTaskToCookieStoreTaskRunner(
base::Bind(&SetCookieWithDetailsAsyncOnCookieThread, url, name, value,
domain, path, creation_time, expiration_time, last_access_time,
secure, http_only, same_site, enforce_strict_secure, priority,
CreateWrappedCallback<bool>(callback)));
}
void AwCookieStoreWrapper::GetCookiesWithOptionsAsync(
const GURL& url,
const net::CookieOptions& options,
const GetCookiesCallback& callback) {
DCHECK(client_task_runner_->RunsTasksOnCurrentThread());
PostTaskToCookieStoreTaskRunner(
base::Bind(&GetCookiesWithOptionsAsyncOnCookieThread, url, options,
CreateWrappedCallback<const std::string&>(callback)));
}
void AwCookieStoreWrapper::GetCookieListWithOptionsAsync(
const GURL& url,
const net::CookieOptions& options,
const GetCookieListCallback& callback) {
DCHECK(client_task_runner_->RunsTasksOnCurrentThread());
PostTaskToCookieStoreTaskRunner(
base::Bind(&GetCookieListWithOptionsAsyncOnCookieThread, url, options,
CreateWrappedCallback<const net::CookieList&>(callback)));
}
void AwCookieStoreWrapper::GetAllCookiesAsync(
const GetCookieListCallback& callback) {
DCHECK(client_task_runner_->RunsTasksOnCurrentThread());
PostTaskToCookieStoreTaskRunner(
base::Bind(&GetAllCookiesAsyncOnCookieThread,
CreateWrappedCallback<const net::CookieList&>(callback)));
}
void AwCookieStoreWrapper::DeleteCookieAsync(const GURL& url,
const std::string& cookie_name,
const base::Closure& callback) {
DCHECK(client_task_runner_->RunsTasksOnCurrentThread());
PostTaskToCookieStoreTaskRunner(
base::Bind(&DeleteCookieAsyncOnCookieThread, url, cookie_name,
CreateWrappedClosureCallback(callback)));
}
void AwCookieStoreWrapper::DeleteCanonicalCookieAsync(
const net::CanonicalCookie& cookie,
const DeleteCallback& callback) {
DCHECK(client_task_runner_->RunsTasksOnCurrentThread());
PostTaskToCookieStoreTaskRunner(
base::Bind(&DeleteCanonicalCookieAsyncOnCookieThread, cookie,
CreateWrappedCallback<int>(callback)));
}
void AwCookieStoreWrapper::DeleteAllCreatedBetweenAsync(
const base::Time& delete_begin,
const base::Time& delete_end,
const DeleteCallback& callback) {
DCHECK(client_task_runner_->RunsTasksOnCurrentThread());
PostTaskToCookieStoreTaskRunner(
base::Bind(&DeleteAllCreatedBetweenAsyncOnCookieThread, delete_begin,
delete_end, CreateWrappedCallback<int>(callback)));
}
void AwCookieStoreWrapper::DeleteAllCreatedBetweenForHostAsync(
const base::Time delete_begin,
const base::Time delete_end,
const GURL& url,
const DeleteCallback& callback) {
DCHECK(client_task_runner_->RunsTasksOnCurrentThread());
PostTaskToCookieStoreTaskRunner(base::Bind(
&DeleteAllCreatedBetweenForHostAsyncOnCookieThread, delete_begin,
delete_end, url, CreateWrappedCallback<int>(callback)));
}
void AwCookieStoreWrapper::DeleteSessionCookiesAsync(
const DeleteCallback& callback) {
DCHECK(client_task_runner_->RunsTasksOnCurrentThread());
PostTaskToCookieStoreTaskRunner(
base::Bind(&DeleteSessionCookiesAsyncOnCookieThread,
CreateWrappedCallback<int>(callback)));
}
void AwCookieStoreWrapper::FlushStore(const base::Closure& callback) {
DCHECK(client_task_runner_->RunsTasksOnCurrentThread());
PostTaskToCookieStoreTaskRunner(base::Bind(
&FlushStoreOnCookieThread, CreateWrappedClosureCallback(callback)));
}
void AwCookieStoreWrapper::SetForceKeepSessionState() {
DCHECK(client_task_runner_->RunsTasksOnCurrentThread());
PostTaskToCookieStoreTaskRunner(
base::Bind(&SetForceKeepSessionStateOnCookieThread));
}
scoped_ptr<net::CookieStore::CookieChangedSubscription>
AwCookieStoreWrapper::AddCallbackForCookie(
const GURL& url,
const std::string& name,
const CookieChangedCallback& callback) {
DCHECK(client_task_runner_->RunsTasksOnCurrentThread());
// The SubscriptionWrapper is owned by the subscription itself, and has no
// connection to the AwCookieStoreWrapper after creation. Other CookieStore
// implementations DCHECK if a subscription outlasts the cookie store,
// unfortunately, this design makes DCHECKing if there's an outstanding
// subscription when the AwCookieStoreWrapper is destroyed a bit ugly.
// TODO(mmenke): Still worth adding a DCHECK?
SubscriptionWrapper* subscription = new SubscriptionWrapper();
return subscription->Subscribe(url, name, callback);
}
bool AwCookieStoreWrapper::IsEphemeral() {
return GetCookieStore()->IsEphemeral();
}
base::Closure AwCookieStoreWrapper::CreateWrappedClosureCallback(
const base::Closure& callback) {
if (callback.is_null())
return callback;
return base::Bind(base::IgnoreResult(&base::TaskRunner::PostTask),
client_task_runner_, FROM_HERE,
base::Bind(&AwCookieStoreWrapper::RunClosureCallback,
weak_factory_.GetWeakPtr(), callback));
}
void AwCookieStoreWrapper::RunClosureCallback(const base::Closure& callback) {
DCHECK(client_task_runner_->RunsTasksOnCurrentThread());
callback.Run();
}
} // namespace android_webview
|