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 base::id::{QuotaExceededErrorId, QuotaExceededErrorIndex};
6use constellation_traits::SerializableQuotaExceededError;
7use dom_struct::dom_struct;
8use js::gc::HandleObject;
9use rustc_hash::FxHashMap;
10use script_bindings::codegen::GenericBindings::QuotaExceededErrorBinding::{
11    QuotaExceededErrorMethods, QuotaExceededErrorOptions,
12};
13use script_bindings::num::Finite;
14use script_bindings::root::DomRoot;
15use script_bindings::script_runtime::CanGc;
16use script_bindings::str::DOMString;
17
18use crate::dom::bindings::error::Error;
19use crate::dom::bindings::reflector::{reflect_dom_object, reflect_dom_object_with_proto};
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        global: &GlobalScope,
50        message: DOMString,
51        quota: Option<Finite<f64>>,
52        requested: Option<Finite<f64>>,
53        can_gc: CanGc,
54    ) -> DomRoot<Self> {
55        reflect_dom_object(
56            Box::new(Self::new_inherited(message, quota, requested)),
57            global,
58            can_gc,
59        )
60    }
61}
62
63impl QuotaExceededErrorMethods<crate::DomTypeHolder> for QuotaExceededError {
64    /// <https://webidl.spec.whatwg.org/#dom-quotaexceedederror-quotaexceedederror>
65    fn Constructor(
66        global: &GlobalScope,
67        proto: Option<HandleObject>,
68        can_gc: CanGc,
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            if *requested < *quota {
94                return Err(Error::Range(c"requested is less than quota".to_owned()));
95            }
96        }
97        Ok(reflect_dom_object_with_proto(
98            Box::new(QuotaExceededError::new_inherited(
99                message,
100                options.quota,
101                options.requested,
102            )),
103            global,
104            proto,
105            can_gc,
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) -> Result<(QuotaExceededErrorId, Self::Data), ()> {
128        let (_, dom_exception) = self.dom_exception.serialize()?;
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        owner: &GlobalScope,
140        serialized: Self::Data,
141        can_gc: CanGc,
142    ) -> Result<DomRoot<Self>, ()>
143    where
144        Self: Sized,
145    {
146        Ok(Self::new(
147            owner,
148            DOMString::from(serialized.dom_exception.message),
149            serialized
150                .quota
151                .map(|val| Finite::new(val).ok_or(()))
152                .transpose()?,
153            serialized
154                .requested
155                .map(|val| Finite::new(val).ok_or(()))
156                .transpose()?,
157            can_gc,
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}