script/dom/
url.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::default::Default;
6
7use base::generic_channel::GenericSend;
8use dom_struct::dom_struct;
9use js::rust::HandleObject;
10use net_traits::CoreResourceMsg;
11use net_traits::blob_url_store::parse_blob_url;
12use net_traits::filemanager_thread::FileManagerThreadMsg;
13use profile_traits::ipc;
14use script_bindings::cformat;
15use servo_url::{ImmutableOrigin, ServoUrl};
16use uuid::Uuid;
17
18use crate::dom::bindings::cell::DomRefCell;
19use crate::dom::bindings::codegen::Bindings::URLBinding::URLMethods;
20use crate::dom::bindings::error::{Error, ErrorResult, Fallible};
21use crate::dom::bindings::reflector::{DomGlobal, Reflector, reflect_dom_object_with_proto};
22use crate::dom::bindings::root::{DomRoot, MutNullableDom};
23use crate::dom::bindings::str::{DOMString, USVString};
24use crate::dom::blob::Blob;
25use crate::dom::globalscope::GlobalScope;
26use crate::dom::urlhelper::UrlHelper;
27use crate::dom::urlsearchparams::URLSearchParams;
28use crate::script_runtime::CanGc;
29
30/// <https://url.spec.whatwg.org/#url>
31#[dom_struct]
32#[expect(clippy::upper_case_acronyms)]
33pub(crate) struct URL {
34    reflector_: Reflector,
35
36    /// <https://url.spec.whatwg.org/#concept-url-url>
37    #[no_trace]
38    url: DomRefCell<ServoUrl>,
39
40    /// <https://url.spec.whatwg.org/#dom-url-searchparams>
41    search_params: MutNullableDom<URLSearchParams>,
42}
43
44impl URL {
45    fn new_inherited(url: ServoUrl) -> URL {
46        URL {
47            reflector_: Reflector::new(),
48            url: DomRefCell::new(url),
49            search_params: Default::default(),
50        }
51    }
52
53    fn new(
54        global: &GlobalScope,
55        proto: Option<HandleObject>,
56        url: ServoUrl,
57        can_gc: CanGc,
58    ) -> DomRoot<URL> {
59        reflect_dom_object_with_proto(Box::new(URL::new_inherited(url)), global, proto, can_gc)
60    }
61
62    pub(crate) fn query_pairs(&self) -> Vec<(String, String)> {
63        self.url
64            .borrow()
65            .as_url()
66            .query_pairs()
67            .into_owned()
68            .collect()
69    }
70
71    pub(crate) fn origin(&self) -> ImmutableOrigin {
72        self.url.borrow().origin()
73    }
74
75    pub(crate) fn set_query_pairs(&self, pairs: &[(String, String)]) {
76        let mut url = self.url.borrow_mut();
77
78        if pairs.is_empty() {
79            url.as_mut_url().set_query(None);
80        } else {
81            url.as_mut_url()
82                .query_pairs_mut()
83                .clear()
84                .extend_pairs(pairs);
85        }
86    }
87
88    /// <https://w3c.github.io/FileAPI/#unicodeBlobURL>
89    fn unicode_serialization_blob_url(origin: &ImmutableOrigin, id: &Uuid) -> String {
90        // Step 1. Let result be the empty string.
91        // Step 2. Append the string "blob:" to result.
92        let mut result = "blob:".to_string();
93
94        // Step 3. Let settings be the current settings object.
95        // Step 4. Let origin be settings’s origin.
96        // Step 5. Let serialized be the ASCII serialization of origin.
97        // Step 6. If serialized is "null", set it to an implementation-defined value.
98        // Step 7. Append serialized to result.
99        // N.B. We leave it as "null" right now.
100        result.push_str(&origin.ascii_serialization());
101
102        // Step 8. Append U+0024 SOLIDUS (/) to result.
103        result.push('/');
104
105        // Step 9. Generate a UUID [RFC4122] as a string and append it to result.
106        result.push_str(&id.to_string());
107
108        // Step 10. Return result.
109        result
110    }
111}
112
113impl URLMethods<crate::DomTypeHolder> for URL {
114    /// <https://url.spec.whatwg.org/#constructors>
115    fn Constructor(
116        global: &GlobalScope,
117        proto: Option<HandleObject>,
118        can_gc: CanGc,
119        url: USVString,
120        base: Option<USVString>,
121    ) -> Fallible<DomRoot<URL>> {
122        // Step 1. Parse url with base.
123        let parsed_base = match base {
124            None => None,
125            Some(base) => {
126                match ServoUrl::parse(&base.0) {
127                    Ok(base) => Some(base),
128                    Err(error) => {
129                        // Step 2. Throw a TypeError if URL parsing fails.
130                        return Err(Error::Type(cformat!("could not parse base: {}", error)));
131                    },
132                }
133            },
134        };
135        let parsed_url = match ServoUrl::parse_with_base(parsed_base.as_ref(), &url.0) {
136            Ok(url) => url,
137            Err(error) => {
138                // Step 2. Throw a TypeError if URL parsing fails.
139                return Err(Error::Type(cformat!("could not parse URL: {}", error)));
140            },
141        };
142
143        // Skip the steps below.
144        // Instead of constructing a new `URLSearchParams` object here, construct it
145        // on-demand inside `URL::SearchParams`.
146        //
147        // Step 3. Let query be parsedURL’s query.
148        // Step 5. Set this’s query object to a new URLSearchParams object.
149        // Step 6. Initialize this’s query object with query.
150        // Step 7. Set this’s query object’s URL object to this.
151
152        // Step 4. Set this’s URL to parsedURL.
153        Ok(URL::new(global, proto, parsed_url, can_gc))
154    }
155
156    /// <https://url.spec.whatwg.org/#dom-url-canparse>
157    fn CanParse(_global: &GlobalScope, url: USVString, base: Option<USVString>) -> bool {
158        // Step 1.
159        let parsed_base = match base {
160            None => None,
161            Some(base) => match ServoUrl::parse(&base.0) {
162                Ok(base) => Some(base),
163                Err(_) => {
164                    // Step 2.1
165                    return false;
166                },
167            },
168        };
169        // Step 2.2, 3
170        ServoUrl::parse_with_base(parsed_base.as_ref(), &url.0).is_ok()
171    }
172
173    /// <https://url.spec.whatwg.org/#dom-url-parse>
174    fn Parse(
175        global: &GlobalScope,
176        url: USVString,
177        base: Option<USVString>,
178        can_gc: CanGc,
179    ) -> Option<DomRoot<URL>> {
180        // Step 1: Let parsedURL be the result of running the API URL parser on url with base,
181        // if given.
182        let parsed_base = base.and_then(|base| ServoUrl::parse(base.0.as_str()).ok());
183        let parsed_url = ServoUrl::parse_with_base(parsed_base.as_ref(), &url.0);
184
185        // Step 2: If parsedURL is failure, then return null.
186        // Step 3: Let url be a new URL object.
187        // Step 4: Initialize url with parsedURL.
188        // Step 5: Return url.
189
190        // These steps are all handled while mapping the Result to an Option<ServoUrl>.
191        // Regarding initialization, the same condition should apply here as stated in the comments
192        // in Self::Constructor above - construct it on-demand inside `URL::SearchParams`.
193        Some(URL::new(global, None, parsed_url.ok()?, can_gc))
194    }
195
196    /// <https://w3c.github.io/FileAPI/#dfn-createObjectURL>
197    fn CreateObjectURL(global: &GlobalScope, blob: &Blob) -> DOMString {
198        // XXX: Second field is an unicode-serialized Origin, it is a temporary workaround
199        //      and should not be trusted. See issue https://github.com/servo/servo/issues/11722
200        let origin = global.origin().immutable();
201
202        let id = blob.get_blob_url_id();
203
204        DOMString::from(URL::unicode_serialization_blob_url(origin, &id))
205    }
206
207    /// <https://w3c.github.io/FileAPI/#dfn-revokeObjectURL>
208    fn RevokeObjectURL(global: &GlobalScope, url: DOMString) {
209        // If the value provided for the url argument is not a Blob URL OR
210        // if the value provided for the url argument does not have an entry in the Blob URL Store,
211        // this method call does nothing. User agents may display a message on the error console.
212        let origin = global.origin().immutable();
213
214        if let Ok(url) = ServoUrl::parse(&url.str()) {
215            if url.fragment().is_none() && *origin == url.origin() {
216                if let Ok((id, _)) = parse_blob_url(&url) {
217                    let resource_threads = global.resource_threads();
218                    let (tx, rx) = ipc::channel(global.time_profiler_chan().clone()).unwrap();
219                    let msg = FileManagerThreadMsg::RevokeBlobURL(id, origin.clone(), tx);
220                    let _ = resource_threads.send(CoreResourceMsg::ToFileManager(msg));
221
222                    let _ = rx.recv().unwrap();
223                }
224            }
225        }
226    }
227
228    /// <https://url.spec.whatwg.org/#dom-url-hash>
229    fn Hash(&self) -> USVString {
230        UrlHelper::Hash(&self.url.borrow())
231    }
232
233    /// <https://url.spec.whatwg.org/#dom-url-hash>
234    fn SetHash(&self, value: USVString) {
235        UrlHelper::SetHash(&mut self.url.borrow_mut(), value);
236    }
237
238    /// <https://url.spec.whatwg.org/#dom-url-host>
239    fn Host(&self) -> USVString {
240        UrlHelper::Host(&self.url.borrow())
241    }
242
243    /// <https://url.spec.whatwg.org/#dom-url-host>
244    fn SetHost(&self, value: USVString) {
245        UrlHelper::SetHost(&mut self.url.borrow_mut(), value);
246    }
247
248    /// <https://url.spec.whatwg.org/#dom-url-hostname>
249    fn Hostname(&self) -> USVString {
250        UrlHelper::Hostname(&self.url.borrow())
251    }
252
253    /// <https://url.spec.whatwg.org/#dom-url-hostname>
254    fn SetHostname(&self, value: USVString) {
255        UrlHelper::SetHostname(&mut self.url.borrow_mut(), value);
256    }
257
258    /// <https://url.spec.whatwg.org/#dom-url-href>
259    fn Href(&self) -> USVString {
260        UrlHelper::Href(&self.url.borrow())
261    }
262
263    /// <https://url.spec.whatwg.org/#dom-url-href>
264    fn SetHref(&self, value: USVString) -> ErrorResult {
265        match ServoUrl::parse(&value.0) {
266            Ok(url) => {
267                *self.url.borrow_mut() = url;
268                self.search_params.set(None); // To be re-initialized in the SearchParams getter.
269                Ok(())
270            },
271            Err(error) => Err(Error::Type(cformat!("could not parse URL: {}", error))),
272        }
273    }
274
275    /// <https://url.spec.whatwg.org/#dom-url-password>
276    fn Password(&self) -> USVString {
277        UrlHelper::Password(&self.url.borrow())
278    }
279
280    /// <https://url.spec.whatwg.org/#dom-url-password>
281    fn SetPassword(&self, value: USVString) {
282        UrlHelper::SetPassword(&mut self.url.borrow_mut(), value);
283    }
284
285    /// <https://url.spec.whatwg.org/#dom-url-pathname>
286    fn Pathname(&self) -> USVString {
287        UrlHelper::Pathname(&self.url.borrow())
288    }
289
290    /// <https://url.spec.whatwg.org/#dom-url-pathname>
291    fn SetPathname(&self, value: USVString) {
292        UrlHelper::SetPathname(&mut self.url.borrow_mut(), value);
293    }
294
295    /// <https://url.spec.whatwg.org/#dom-url-port>
296    fn Port(&self) -> USVString {
297        UrlHelper::Port(&self.url.borrow())
298    }
299
300    /// <https://url.spec.whatwg.org/#dom-url-port>
301    fn SetPort(&self, value: USVString) {
302        UrlHelper::SetPort(&mut self.url.borrow_mut(), value);
303    }
304
305    /// <https://url.spec.whatwg.org/#dom-url-protocol>
306    fn Protocol(&self) -> USVString {
307        UrlHelper::Protocol(&self.url.borrow())
308    }
309
310    /// <https://url.spec.whatwg.org/#dom-url-protocol>
311    fn SetProtocol(&self, value: USVString) {
312        UrlHelper::SetProtocol(&mut self.url.borrow_mut(), value);
313    }
314
315    /// <https://url.spec.whatwg.org/#dom-url-origin>
316    fn Origin(&self) -> USVString {
317        UrlHelper::Origin(&self.url.borrow())
318    }
319
320    /// <https://url.spec.whatwg.org/#dom-url-search>
321    fn Search(&self) -> USVString {
322        UrlHelper::Search(&self.url.borrow())
323    }
324
325    /// <https://url.spec.whatwg.org/#dom-url-search>
326    fn SetSearch(&self, value: USVString) {
327        UrlHelper::SetSearch(&mut self.url.borrow_mut(), value);
328        if let Some(search_params) = self.search_params.get() {
329            search_params.set_list(self.query_pairs());
330        }
331    }
332
333    /// <https://url.spec.whatwg.org/#dom-url-searchparams>
334    fn SearchParams(&self, can_gc: CanGc) -> DomRoot<URLSearchParams> {
335        self.search_params
336            .or_init(|| URLSearchParams::new(&self.global(), Some(self), can_gc))
337    }
338
339    /// <https://url.spec.whatwg.org/#dom-url-username>
340    fn Username(&self) -> USVString {
341        UrlHelper::Username(&self.url.borrow())
342    }
343
344    /// <https://url.spec.whatwg.org/#dom-url-username>
345    fn SetUsername(&self, value: USVString) {
346        UrlHelper::SetUsername(&mut self.url.borrow_mut(), value);
347    }
348
349    /// <https://url.spec.whatwg.org/#dom-url-tojson>
350    fn ToJSON(&self) -> USVString {
351        self.Href()
352    }
353}