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
|
/* 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::collections::HashMap;
use std::num::NonZeroU32;
use base::id::{DomExceptionId, DomExceptionIndex, PipelineNamespaceId};
use constellation_traits::DomException;
use dom_struct::dom_struct;
use js::rust::HandleObject;
use crate::dom::bindings::codegen::Bindings::DOMExceptionBinding::{
DOMExceptionConstants, DOMExceptionMethods,
};
use crate::dom::bindings::error::Error;
use crate::dom::bindings::reflector::{
Reflector, reflect_dom_object, reflect_dom_object_with_proto,
};
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::serializable::{IntoStorageKey, Serializable, StorageKey};
use crate::dom::bindings::str::DOMString;
use crate::dom::bindings::structuredclone::{StructuredData, StructuredDataReader};
use crate::dom::globalscope::GlobalScope;
use crate::script_runtime::CanGc;
#[repr(u16)]
#[allow(clippy::enum_variant_names)]
#[derive(Clone, Copy, Debug, Eq, JSTraceable, MallocSizeOf, Ord, PartialEq, PartialOrd)]
pub(crate) enum DOMErrorName {
IndexSizeError = DOMExceptionConstants::INDEX_SIZE_ERR,
HierarchyRequestError = DOMExceptionConstants::HIERARCHY_REQUEST_ERR,
WrongDocumentError = DOMExceptionConstants::WRONG_DOCUMENT_ERR,
InvalidCharacterError = DOMExceptionConstants::INVALID_CHARACTER_ERR,
NoModificationAllowedError = DOMExceptionConstants::NO_MODIFICATION_ALLOWED_ERR,
NotFoundError = DOMExceptionConstants::NOT_FOUND_ERR,
NotSupportedError = DOMExceptionConstants::NOT_SUPPORTED_ERR,
InUseAttributeError = DOMExceptionConstants::INUSE_ATTRIBUTE_ERR,
InvalidStateError = DOMExceptionConstants::INVALID_STATE_ERR,
SyntaxError = DOMExceptionConstants::SYNTAX_ERR,
InvalidModificationError = DOMExceptionConstants::INVALID_MODIFICATION_ERR,
NamespaceError = DOMExceptionConstants::NAMESPACE_ERR,
InvalidAccessError = DOMExceptionConstants::INVALID_ACCESS_ERR,
SecurityError = DOMExceptionConstants::SECURITY_ERR,
NetworkError = DOMExceptionConstants::NETWORK_ERR,
AbortError = DOMExceptionConstants::ABORT_ERR,
TypeMismatchError = DOMExceptionConstants::TYPE_MISMATCH_ERR,
URLMismatchError = DOMExceptionConstants::URL_MISMATCH_ERR,
QuotaExceededError = DOMExceptionConstants::QUOTA_EXCEEDED_ERR,
TimeoutError = DOMExceptionConstants::TIMEOUT_ERR,
InvalidNodeTypeError = DOMExceptionConstants::INVALID_NODE_TYPE_ERR,
DataCloneError = DOMExceptionConstants::DATA_CLONE_ERR,
EncodingError,
NotReadableError,
DataError,
OperationError,
}
impl DOMErrorName {
pub(crate) fn from(s: &DOMString) -> Option<DOMErrorName> {
match s.as_ref() {
"IndexSizeError" => Some(DOMErrorName::IndexSizeError),
"HierarchyRequestError" => Some(DOMErrorName::HierarchyRequestError),
"WrongDocumentError" => Some(DOMErrorName::WrongDocumentError),
"InvalidCharacterError" => Some(DOMErrorName::InvalidCharacterError),
"NoModificationAllowedError" => Some(DOMErrorName::NoModificationAllowedError),
"NotFoundError" => Some(DOMErrorName::NotFoundError),
"NotSupportedError" => Some(DOMErrorName::NotSupportedError),
"InUseAttributeError" => Some(DOMErrorName::InUseAttributeError),
"InvalidStateError" => Some(DOMErrorName::InvalidStateError),
"SyntaxError" => Some(DOMErrorName::SyntaxError),
"InvalidModificationError" => Some(DOMErrorName::InvalidModificationError),
"NamespaceError" => Some(DOMErrorName::NamespaceError),
"InvalidAccessError" => Some(DOMErrorName::InvalidAccessError),
"SecurityError" => Some(DOMErrorName::SecurityError),
"NetworkError" => Some(DOMErrorName::NetworkError),
"AbortError" => Some(DOMErrorName::AbortError),
"TypeMismatchError" => Some(DOMErrorName::TypeMismatchError),
"URLMismatchError" => Some(DOMErrorName::URLMismatchError),
"QuotaExceededError" => Some(DOMErrorName::QuotaExceededError),
"TimeoutError" => Some(DOMErrorName::TimeoutError),
"InvalidNodeTypeError" => Some(DOMErrorName::InvalidNodeTypeError),
"DataCloneError" => Some(DOMErrorName::DataCloneError),
"EncodingError" => Some(DOMErrorName::EncodingError),
"NotReadableError" => Some(DOMErrorName::NotReadableError),
"DataError" => Some(DOMErrorName::DataError),
"OperationError" => Some(DOMErrorName::OperationError),
_ => None,
}
}
}
#[dom_struct]
pub(crate) struct DOMException {
reflector_: Reflector,
message: DOMString,
name: DOMString,
}
impl DOMException {
fn get_error_data_by_code(code: DOMErrorName) -> (DOMString, DOMString) {
let message = match &code {
DOMErrorName::IndexSizeError => "The index is not in the allowed range.",
DOMErrorName::HierarchyRequestError => {
"The operation would yield an incorrect node tree."
},
DOMErrorName::WrongDocumentError => "The object is in the wrong document.",
DOMErrorName::InvalidCharacterError => "The string contains invalid characters.",
DOMErrorName::NoModificationAllowedError => "The object can not be modified.",
DOMErrorName::NotFoundError => "The object can not be found here.",
DOMErrorName::NotSupportedError => "The operation is not supported.",
DOMErrorName::InUseAttributeError => "The attribute already in use.",
DOMErrorName::InvalidStateError => "The object is in an invalid state.",
DOMErrorName::SyntaxError => "The string did not match the expected pattern.",
DOMErrorName::InvalidModificationError => "The object can not be modified in this way.",
DOMErrorName::NamespaceError => "The operation is not allowed by Namespaces in XML.",
DOMErrorName::InvalidAccessError => {
"The object does not support the operation or argument."
},
DOMErrorName::SecurityError => "The operation is insecure.",
DOMErrorName::NetworkError => "A network error occurred.",
DOMErrorName::AbortError => "The operation was aborted.",
DOMErrorName::TypeMismatchError => "The given type does not match any expected type.",
DOMErrorName::URLMismatchError => "The given URL does not match another URL.",
DOMErrorName::QuotaExceededError => "The quota has been exceeded.",
DOMErrorName::TimeoutError => "The operation timed out.",
DOMErrorName::InvalidNodeTypeError => {
"The supplied node is incorrect or has an incorrect ancestor for this operation."
},
DOMErrorName::DataCloneError => "The object can not be cloned.",
DOMErrorName::EncodingError => {
"The encoding operation (either encoded or decoding) failed."
},
DOMErrorName::NotReadableError => "The I/O read operation failed.",
DOMErrorName::DataError => "Provided data is inadequate.",
DOMErrorName::OperationError => {
"The operation failed for an operation-specific reason."
},
};
(
DOMString::from(message),
DOMString::from(format!("{:?}", code)),
)
}
pub(crate) fn new_inherited(message: DOMString, name: DOMString) -> DOMException {
DOMException {
reflector_: Reflector::new(),
message,
name,
}
}
pub(crate) fn new(
global: &GlobalScope,
code: DOMErrorName,
can_gc: CanGc,
) -> DomRoot<DOMException> {
let (message, name) = DOMException::get_error_data_by_code(code);
reflect_dom_object(
Box::new(DOMException::new_inherited(message, name)),
global,
can_gc,
)
}
pub(crate) fn new_with_custom_message(
global: &GlobalScope,
code: DOMErrorName,
message: String,
can_gc: CanGc,
) -> DomRoot<DOMException> {
let (_, name) = DOMException::get_error_data_by_code(code);
reflect_dom_object(
Box::new(DOMException::new_inherited(DOMString::from(message), name)),
global,
can_gc,
)
}
// not an IDL stringifier, used internally
pub(crate) fn stringifier(&self) -> DOMString {
DOMString::from(format!("{}: {}", self.name, self.message))
}
}
impl DOMExceptionMethods<crate::DomTypeHolder> for DOMException {
// https://webidl.spec.whatwg.org/#dom-domexception-domexception
fn Constructor(
global: &GlobalScope,
proto: Option<HandleObject>,
can_gc: CanGc,
message: DOMString,
name: DOMString,
) -> Result<DomRoot<DOMException>, Error> {
Ok(reflect_dom_object_with_proto(
Box::new(DOMException::new_inherited(message, name)),
global,
proto,
can_gc,
))
}
// https://webidl.spec.whatwg.org/#dom-domexception-code
fn Code(&self) -> u16 {
match DOMErrorName::from(&self.name) {
Some(code) if code <= DOMErrorName::DataCloneError => code as u16,
_ => 0,
}
}
// https://webidl.spec.whatwg.org/#dom-domexception-name
fn Name(&self) -> DOMString {
self.name.clone()
}
// https://webidl.spec.whatwg.org/#dom-domexception-message
fn Message(&self) -> DOMString {
self.message.clone()
}
}
impl Serializable for DOMException {
type Id = DomExceptionId;
type Data = DomException;
// https://webidl.spec.whatwg.org/#idl-DOMException
fn serialize(&self) -> Result<(Self::Id, Self::Data), ()> {
let serialized = DomException {
message: self.message.to_string(),
name: self.name.to_string(),
};
Ok((DomExceptionId::new(), serialized))
}
// https://webidl.spec.whatwg.org/#idl-DOMException
fn deserialize(
owner: &GlobalScope,
serialized: Self::Data,
can_gc: CanGc,
) -> Result<DomRoot<Self>, ()>
where
Self: Sized,
{
Ok(Self::new_with_custom_message(
owner,
DOMErrorName::from(&DOMString::from_string(serialized.name)).ok_or(())?,
serialized.message,
can_gc,
))
}
fn serialized_storage(data: StructuredData<'_>) -> &mut Option<HashMap<Self::Id, Self::Data>> {
match data {
StructuredData::Reader(reader) => &mut reader.exceptions,
StructuredData::Writer(writer) => &mut writer.exceptions,
}
}
fn deserialized_storage(
reader: &mut StructuredDataReader,
) -> &mut Option<HashMap<StorageKey, DomRoot<Self>>> {
&mut reader.dom_exceptions
}
}
impl From<StorageKey> for DomExceptionId {
fn from(storage_key: StorageKey) -> DomExceptionId {
let namespace_id = PipelineNamespaceId(storage_key.name_space);
let index = DomExceptionIndex(
NonZeroU32::new(storage_key.index).expect("Deserialized exception index is zero"),
);
DomExceptionId {
namespace_id,
index,
}
}
}
impl IntoStorageKey for DomExceptionId {
fn into_storage_key(self) -> StorageKey {
let DomExceptionIndex(index) = self.index;
StorageKey::new(self.namespace_id, index)
}
}
|