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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::cell::{Cell, RefCell};
use bitflags::bitflags;
use cookie::Cookie;
use log::warn;
use net_traits::pub_domains::registered_domain_name;
use net_traits::{CookieOperationId, ResourceThreads, SiteDescriptor};
use rustc_hash::FxHashMap;
use servo_url::ServoUrl;
use storage_traits::StorageThreads;
use storage_traits::webstorage_thread::{OriginDescriptor, WebStorageType};
use url::Url;
use crate::CookieSource;
bitflags! {
/// Identifies categories of site data associated with a site.
///
/// This type is used by `SiteDataManager` to query, describe, and manage
/// different kinds of data stored by the user agent for a given site.
///
/// Additional storage categories (e.g. IndexedDB) may be added in the
/// future.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct StorageType: u8 {
/// Corresponds to the HTTP cookies:
/// <https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Cookies>
const Cookies = 1 << 0;
/// Corresponds to the `localStorage` Web API:
/// <https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage>
const Local = 1 << 1;
/// Corresponds to the `sessionStorage` Web API:
/// <https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage>
const Session = 1 << 2;
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct SiteData {
name: String,
storage_types: StorageType,
}
impl SiteData {
pub fn new(name: impl Into<String>, storage_types: StorageType) -> SiteData {
SiteData {
name: name.into(),
storage_types,
}
}
pub fn name(&self) -> String {
self.name.clone()
}
pub fn storage_types(&self) -> StorageType {
self.storage_types
}
}
/// The response data for a pending embedder cookie operation.
pub(crate) enum CookieOperationResponse {
/// Cookies returned from a get operation.
Cookies(Vec<Cookie<'static>>),
/// Acknowledgement that a operation is completed.
Done,
}
/// A callback for a pending embedder cookie operation,
/// paired with the [`CookieOperationResponse`].
enum CookieOperationCallback {
Cookies(Box<dyn FnOnce(Vec<Cookie<'static>>)>),
Done(Box<dyn FnOnce()>),
DoneAfterResponses {
remaining_responses: u8,
callback: Box<dyn FnOnce()>,
},
}
/// Provides APIs for inspecting and managing site data.
///
/// `SiteDataManager` exposes information about data that is conceptually
/// associated with a site (equivalent to an eTLD+1), such as web exposed
/// storage mechanisms like `localStorage` and `sessionStorage`.
///
/// The manager can be used by embedders to list sites with stored data.
/// Support for site scoped management operations (e.g. clearing data for a
/// specific site) will be added in the future.
///
/// Note: Network layer state (such as the HTTP cache) is intentionally not
/// handled by `SiteDataManager`. That functionality lives in `NetworkManager`.
pub struct SiteDataManager {
public_resource_threads: ResourceThreads,
private_resource_threads: ResourceThreads,
public_storage_threads: StorageThreads,
private_storage_threads: StorageThreads,
next_cookie_op_id: Cell<u64>,
pending_cookie_callbacks: RefCell<FxHashMap<CookieOperationId, CookieOperationCallback>>,
}
impl SiteDataManager {
pub(crate) fn new(
public_resource_threads: ResourceThreads,
private_resource_threads: ResourceThreads,
public_storage_threads: StorageThreads,
private_storage_threads: StorageThreads,
) -> Self {
Self {
public_resource_threads,
private_resource_threads,
public_storage_threads,
private_storage_threads,
next_cookie_op_id: Cell::new(0),
pending_cookie_callbacks: RefCell::new(FxHashMap::default()),
}
}
/// Return a list of sites that have associated site data.
///
/// The returned list is filtered by the provided `storage_types` bitflags.
/// Each [`SiteData`] entry represents a site (equivalent to an eTLD+1)
/// and indicates which kinds of storage data are present for it (e.g.
/// localStorage, sessionStorage).
///
/// The returned list is sorted by site name.
///
/// Both public and private storage are included in the result.
pub fn site_data(&self, storage_types: StorageType) -> Vec<SiteData> {
let mut all_sites: FxHashMap<String, StorageType> = FxHashMap::default();
let mut add_sites = |sites: Vec<SiteDescriptor>, storage_type: StorageType| {
for site in sites {
all_sites
.entry(site.name)
.and_modify(|storage_types| *storage_types |= storage_type)
.or_insert(storage_type);
}
};
if storage_types.contains(StorageType::Cookies) {
let public_cookies = self.public_resource_threads.cookies();
add_sites(public_cookies, StorageType::Cookies);
let private_cookies = self.private_resource_threads.cookies();
add_sites(private_cookies, StorageType::Cookies);
}
let mut add_origins = |origins: Vec<OriginDescriptor>, storage_type: StorageType| {
for origin in origins {
let url =
ServoUrl::parse(&origin.name).expect("Should always be able to parse origins.");
let Some(domain) = registered_domain_name(&url) else {
warn!("Failed to get a registered domain name for: {url}.");
continue;
};
let domain = domain.to_string();
all_sites
.entry(domain)
.and_modify(|storage_types| *storage_types |= storage_type)
.or_insert(storage_type);
}
};
if storage_types.contains(StorageType::Local) {
let public_origins = self
.public_storage_threads
.webstorage_origins(WebStorageType::Local);
add_origins(public_origins, StorageType::Local);
let private_origins = self
.private_storage_threads
.webstorage_origins(WebStorageType::Local);
add_origins(private_origins, StorageType::Local);
}
if storage_types.contains(StorageType::Session) {
let public_origins = self
.public_storage_threads
.webstorage_origins(WebStorageType::Session);
add_origins(public_origins, StorageType::Session);
let private_origins = self
.private_storage_threads
.webstorage_origins(WebStorageType::Session);
add_origins(private_origins, StorageType::Session);
}
let mut result: Vec<SiteData> = all_sites
.into_iter()
.map(|(name, storage_types)| SiteData::new(name, storage_types))
.collect();
result.sort_by_key(SiteData::name);
result
}
/// Clear site data for the given sites.
///
/// The clearing is restricted to the provided `storage_types` bitflags.
/// Both public and private browsing data are affected.
pub fn clear_site_data(&self, sites: &[&str], storage_types: StorageType) {
if storage_types.contains(StorageType::Cookies) {
self.public_resource_threads.clear_cookies_for_sites(sites);
self.private_resource_threads.clear_cookies_for_sites(sites);
}
if storage_types.contains(StorageType::Local) {
self.public_storage_threads
.clear_webstorage_for_sites(WebStorageType::Local, sites);
self.private_storage_threads
.clear_webstorage_for_sites(WebStorageType::Local, sites);
}
if storage_types.contains(StorageType::Session) {
self.public_storage_threads
.clear_webstorage_for_sites(WebStorageType::Session, sites);
self.private_storage_threads
.clear_webstorage_for_sites(WebStorageType::Session, sites);
}
}
/// Clears all cookies from both the public and private cookie jars.
///
/// An optional callback is provided for async operation.
pub fn clear_cookies(&self, callback: Option<Box<dyn FnOnce()>>) {
match callback {
None => {
self.public_resource_threads.clear_cookies();
self.private_resource_threads.clear_cookies();
},
Some(callback) => {
let id = self.next_operation_id();
self.pending_cookie_callbacks.borrow_mut().insert(
id,
CookieOperationCallback::DoneAfterResponses {
remaining_responses: 2,
callback,
},
);
self.public_resource_threads.clear_cookies_async(id);
self.private_resource_threads.clear_cookies_async(id);
},
}
}
/// Delete all session cookies (cookies that have no expiry or max-age).
///
/// Session cookies from both the public and private browsing session cookies are removed.
/// An optional callback is provided for async operation.
pub fn clear_session_cookies(&self, callback: Option<Box<dyn FnOnce()>>) {
match callback {
None => {
self.public_resource_threads.clear_session_cookies();
self.private_resource_threads.clear_session_cookies();
},
Some(callback) => {
let id = self.next_operation_id();
self.pending_cookie_callbacks.borrow_mut().insert(
id,
CookieOperationCallback::DoneAfterResponses {
remaining_responses: 2,
callback,
},
);
self.public_resource_threads.clear_session_cookies_async(id);
self.private_resource_threads
.clear_session_cookies_async(id);
},
}
}
/// Returns the cookies for the domain associated with the given [`Url`].
pub fn cookies_for_url(&self, url: Url, source: CookieSource) -> Vec<Cookie<'static>> {
self.public_resource_threads
.cookies_for_url(url.into(), source)
}
/// Asynchronously returns the cookies for the domain associated with the given [`Url`].
pub fn cookies_for_url_async(
&self,
url: Url,
source: CookieSource,
callback: impl FnOnce(Vec<Cookie<'static>>) + 'static,
) {
let id = self.next_operation_id();
self.pending_cookie_callbacks
.borrow_mut()
.insert(id, CookieOperationCallback::Cookies(Box::new(callback)));
self.public_resource_threads
.cookies_for_url_async(id, url.into(), source);
}
/// Sets a cookie for the domain associated with the given [`Url`].
///
/// An optional callback is provided for async operation.
pub fn set_cookie_for_url(
&self,
url: Url,
cookie: Cookie<'static>,
callback: Option<Box<dyn FnOnce()>>,
) {
match callback {
None => {
self.public_resource_threads.set_cookie_for_url_sync(
url.into(),
cookie,
CookieSource::HTTP,
);
},
Some(callback) => {
let id = self.next_operation_id();
self.pending_cookie_callbacks
.borrow_mut()
.insert(id, CookieOperationCallback::Done(callback));
self.public_resource_threads.set_cookie_for_url_async(
id,
url.into(),
cookie,
CookieSource::HTTP,
);
},
}
}
/// Handle a cookie operation response from the resource thread.
///
/// This is called by the event loop when an embedder cookie response is received.
pub(crate) fn handle_cookie_response(
&self,
id: CookieOperationId,
response: CookieOperationResponse,
) {
let Some(callback) = self.pending_cookie_callbacks.borrow_mut().remove(&id) else {
warn!("Received cookie response for unknown operation {id:?}");
return;
};
match (response, callback) {
(CookieOperationResponse::Cookies(cookies), CookieOperationCallback::Cookies(cb)) => {
cb(cookies);
},
(CookieOperationResponse::Done, CookieOperationCallback::Done(cb)) => {
cb();
},
(
CookieOperationResponse::Done,
CookieOperationCallback::DoneAfterResponses {
remaining_responses,
callback,
},
) => {
if remaining_responses > 1 {
self.pending_cookie_callbacks.borrow_mut().insert(
id,
CookieOperationCallback::DoneAfterResponses {
remaining_responses: remaining_responses - 1,
callback,
},
);
} else {
callback();
}
},
_ => {
warn!("Cookie response type mismatch for operation {id:?}");
},
}
}
fn next_operation_id(&self) -> CookieOperationId {
let id = CookieOperationId(self.next_cookie_op_id.get());
self.next_cookie_op_id.set(id.0 + 1);
id
}
}