Skip to main content

script/dom/
quotaexceedederror.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 dom_struct::dom_struct;
6use js::context::{JSContext, NoGC};
7use js::gc::HandleObject;
8use rustc_hash::FxHashMap;
9use script_bindings::codegen::GenericBindings::QuotaExceededErrorBinding::{
10    QuotaExceededErrorMethods, QuotaExceededErrorOptions,
11};
12use script_bindings::num::Finite;
13use script_bindings::reflector::{reflect_dom_object, reflect_dom_object_with_proto};
14use script_bindings::root::DomRoot;
15use script_bindings::str::DOMString;
16use servo_base::id::{QuotaExceededErrorId, QuotaExceededErrorIndex};
17use servo_constellation_traits::SerializableQuotaExceededError;
18
19use crate::dom::bindings::error::Error;
20use crate::dom::bindings::serializable::Serializable;
21use crate::dom::bindings::structuredclone::StructuredData;
22use crate::dom::types::{DOMException, GlobalScope};
23
24/// <https://webidl.spec.whatwg.org/#quotaexceedederror>
25#[dom_struct]
26pub(crate) struct QuotaExceededError {
27    /// <https://webidl.spec.whatwg.org/#idl-DOMException>
28    dom_exception: DOMException,
29    /// <https://webidl.spec.whatwg.org/#dom-quotaexceedederror-quota>
30    quota: Option<Finite<f64>>,
31    /// <https://webidl.spec.whatwg.org/#dom-quotaexceedederror-requested>
32    requested: Option<Finite<f64>>,
33}
34
35impl QuotaExceededError {
36    fn new_inherited(
37        message: DOMString,
38        quota: Option<Finite<f64>>,
39        requested: Option<Finite<f64>>,
40    ) -> Self {
41        Self {
42            dom_exception: DOMException::new_inherited(
43                message,
44                DOMString::from_static("QuotaExceededError"),
45            ),
46            quota,
47            requested,
48        }
49    }
50
51    pub(crate) fn new(
52        cx: &mut JSContext,
53        global: &GlobalScope,
54        message: DOMString,
55        quota: Option<Finite<f64>>,
56        requested: Option<Finite<f64>>,
57    ) -> DomRoot<Self> {
58        reflect_dom_object(
59            cx,
60            Box::new(Self::new_inherited(message, quota, requested)),
61            global,
62        )
63    }
64}
65
66impl QuotaExceededErrorMethods<crate::DomTypeHolder> for QuotaExceededError {
67    /// <https://webidl.spec.whatwg.org/#dom-quotaexceedederror-quotaexceedederror>
68    fn Constructor(
69        cx: &mut JSContext,
70        global: &GlobalScope,
71        proto: Option<HandleObject>,
72        message: DOMString,
73        options: &QuotaExceededErrorOptions,
74    ) -> Result<DomRoot<Self>, Error> {
75        // If options["quota"] is present:
76        if let Some(quota) = options.quota {
77            // If options["quota"] is less than 0, then throw a RangeError.
78            if *quota < 0.0 {
79                return Err(Error::Range(
80                    c"quota must be at least zero if present".to_owned(),
81                ));
82            }
83        }
84        // If options["requested"] is present:
85        if let Some(requested) = options.requested {
86            // If options["requested"] is less than 0, then throw a RangeError.
87            if *requested < 0.0 {
88                return Err(Error::Range(
89                    c"requested must be at least zero if present".to_owned(),
90                ));
91            }
92        }
93        // If this’s quota is not null, this’s requested is not null, and this’s requested
94        // is less than this’s quota, then throw a RangeError.
95        if let (Some(quota), Some(requested)) = (options.quota, options.requested) &&
96            *requested < *quota
97        {
98            return Err(Error::Range(c"requested is less than quota".to_owned()));
99        }
100        Ok(reflect_dom_object_with_proto(
101            cx,
102            Box::new(QuotaExceededError::new_inherited(
103                message,
104                options.quota,
105                options.requested,
106            )),
107            global,
108            proto,
109        ))
110    }
111
112    /// <https://webidl.spec.whatwg.org/#dom-quotaexceedederror-quota>
113    fn GetQuota(&self) -> Option<Finite<f64>> {
114        // The quota getter steps are to return this’s quota.
115        self.quota
116    }
117
118    /// <https://webidl.spec.whatwg.org/#dom-quotaexceedederror-requested>
119    fn GetRequested(&self) -> Option<Finite<f64>> {
120        // The requested getter steps are to return this’s requested.
121        self.requested
122    }
123}
124
125impl Serializable for QuotaExceededError {
126    type Index = QuotaExceededErrorIndex;
127    type Data = SerializableQuotaExceededError;
128
129    /// <https://webidl.spec.whatwg.org/#quotaexceedederror>
130    fn serialize(&self, no_gc: &NoGC) -> Result<(QuotaExceededErrorId, Self::Data), ()> {
131        let (_, dom_exception) = self.dom_exception.serialize(no_gc)?;
132        let serialized = SerializableQuotaExceededError {
133            dom_exception,
134            quota: self.quota.as_deref().copied(),
135            requested: self.requested.as_deref().copied(),
136        };
137        Ok((QuotaExceededErrorId::new(), serialized))
138    }
139
140    /// <https://webidl.spec.whatwg.org/#quotaexceedederror>
141    fn deserialize(
142        cx: &mut JSContext,
143        owner: &GlobalScope,
144        serialized: Self::Data,
145    ) -> Result<DomRoot<Self>, ()>
146    where
147        Self: Sized,
148    {
149        Ok(Self::new(
150            cx,
151            owner,
152            DOMString::from(serialized.dom_exception.message),
153            serialized
154                .quota
155                .map(|val| Finite::new(val).ok_or(()))
156                .transpose()?,
157            serialized
158                .requested
159                .map(|val| Finite::new(val).ok_or(()))
160                .transpose()?,
161        ))
162    }
163
164    /// <https://webidl.spec.whatwg.org/#quotaexceedederror>
165    fn serialized_storage<'a>(
166        data: StructuredData<'a, '_>,
167    ) -> &'a mut Option<FxHashMap<QuotaExceededErrorId, Self::Data>> {
168        match data {
169            StructuredData::Reader(reader) => &mut reader.quota_exceeded_errors,
170            StructuredData::Writer(writer) => &mut writer.quota_exceeded_errors,
171        }
172    }
173}