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_with_cx, 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(message, "QuotaExceededError".into()),
43            quota,
44            requested,
45        }
46    }
47
48    pub(crate) fn new(
49        cx: &mut JSContext,
50        global: &GlobalScope,
51        message: DOMString,
52        quota: Option<Finite<f64>>,
53        requested: Option<Finite<f64>>,
54    ) -> DomRoot<Self> {
55        reflect_dom_object_with_cx(
56            Box::new(Self::new_inherited(message, quota, requested)),
57            global,
58            cx,
59        )
60    }
61}
62
63impl QuotaExceededErrorMethods<crate::DomTypeHolder> for QuotaExceededError {
64    /// <https://webidl.spec.whatwg.org/#dom-quotaexceedederror-quotaexceedederror>
65    fn Constructor(
66        cx: &mut JSContext,
67        global: &GlobalScope,
68        proto: Option<HandleObject>,
69        message: DOMString,
70        options: &QuotaExceededErrorOptions,
71    ) -> Result<DomRoot<Self>, Error> {
72        // If options["quota"] is present:
73        if let Some(quota) = options.quota {
74            // If options["quota"] is less than 0, then throw a RangeError.
75            if *quota < 0.0 {
76                return Err(Error::Range(
77                    c"quota must be at least zero if present".to_owned(),
78                ));
79            }
80        }
81        // If options["requested"] is present:
82        if let Some(requested) = options.requested {
83            // If options["requested"] is less than 0, then throw a RangeError.
84            if *requested < 0.0 {
85                return Err(Error::Range(
86                    c"requested must be at least zero if present".to_owned(),
87                ));
88            }
89        }
90        // If this’s quota is not null, this’s requested is not null, and this’s requested
91        // is less than this’s quota, then throw a RangeError.
92        if let (Some(quota), Some(requested)) = (options.quota, options.requested) &&
93            *requested < *quota
94        {
95            return Err(Error::Range(c"requested is less than quota".to_owned()));
96        }
97        Ok(reflect_dom_object_with_proto(
98            cx,
99            Box::new(QuotaExceededError::new_inherited(
100                message,
101                options.quota,
102                options.requested,
103            )),
104            global,
105            proto,
106        ))
107    }
108
109    /// <https://webidl.spec.whatwg.org/#dom-quotaexceedederror-quota>
110    fn GetQuota(&self) -> Option<Finite<f64>> {
111        // The quota getter steps are to return this’s quota.
112        self.quota
113    }
114
115    /// <https://webidl.spec.whatwg.org/#dom-quotaexceedederror-requested>
116    fn GetRequested(&self) -> Option<Finite<f64>> {
117        // The requested getter steps are to return this’s requested.
118        self.requested
119    }
120}
121
122impl Serializable for QuotaExceededError {
123    type Index = QuotaExceededErrorIndex;
124    type Data = SerializableQuotaExceededError;
125
126    /// <https://webidl.spec.whatwg.org/#quotaexceedederror>
127    fn serialize(&self, no_gc: &NoGC) -> Result<(QuotaExceededErrorId, Self::Data), ()> {
128        let (_, dom_exception) = self.dom_exception.serialize(no_gc)?;
129        let serialized = SerializableQuotaExceededError {
130            dom_exception,
131            quota: self.quota.as_deref().copied(),
132            requested: self.requested.as_deref().copied(),
133        };
134        Ok((QuotaExceededErrorId::new(), serialized))
135    }
136
137    /// <https://webidl.spec.whatwg.org/#quotaexceedederror>
138    fn deserialize(
139        cx: &mut JSContext,
140        owner: &GlobalScope,
141        serialized: Self::Data,
142    ) -> Result<DomRoot<Self>, ()>
143    where
144        Self: Sized,
145    {
146        Ok(Self::new(
147            cx,
148            owner,
149            DOMString::from(serialized.dom_exception.message),
150            serialized
151                .quota
152                .map(|val| Finite::new(val).ok_or(()))
153                .transpose()?,
154            serialized
155                .requested
156                .map(|val| Finite::new(val).ok_or(()))
157                .transpose()?,
158        ))
159    }
160
161    /// <https://webidl.spec.whatwg.org/#quotaexceedederror>
162    fn serialized_storage<'a>(
163        data: StructuredData<'a, '_>,
164    ) -> &'a mut Option<FxHashMap<QuotaExceededErrorId, Self::Data>> {
165        match data {
166            StructuredData::Reader(reader) => &mut reader.quota_exceeded_errors,
167            StructuredData::Writer(writer) => &mut writer.quota_exceeded_errors,
168        }
169    }
170}