script/dom/
idbfactory.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/. */
4use dom_struct::dom_struct;
5use js::rust::HandleValue;
6use servo_url::origin::ImmutableOrigin;
7
8use crate::dom::bindings::codegen::Bindings::IDBFactoryBinding::IDBFactoryMethods;
9use crate::dom::bindings::error::{Error, Fallible};
10use crate::dom::bindings::import::base::SafeJSContext;
11use crate::dom::bindings::reflector::{DomGlobal, Reflector, reflect_dom_object};
12use crate::dom::bindings::root::DomRoot;
13use crate::dom::bindings::str::DOMString;
14use crate::dom::globalscope::GlobalScope;
15use crate::dom::idbopendbrequest::IDBOpenDBRequest;
16use crate::indexed_db::convert_value_to_key;
17use crate::script_runtime::CanGc;
18
19#[dom_struct]
20pub struct IDBFactory {
21    reflector_: Reflector,
22}
23
24impl IDBFactory {
25    pub fn new_inherited() -> IDBFactory {
26        IDBFactory {
27            reflector_: Reflector::new(),
28        }
29    }
30
31    pub fn new(global: &GlobalScope, can_gc: CanGc) -> DomRoot<IDBFactory> {
32        reflect_dom_object(Box::new(IDBFactory::new_inherited()), global, can_gc)
33    }
34}
35
36impl IDBFactoryMethods<crate::DomTypeHolder> for IDBFactory {
37    // https://www.w3.org/TR/IndexedDB-2/#dom-idbfactory-open
38    fn Open(&self, name: DOMString, version: Option<u64>) -> Fallible<DomRoot<IDBOpenDBRequest>> {
39        // Step 1: If version is 0 (zero), throw a TypeError.
40        if version == Some(0) {
41            return Err(Error::Type(
42                "The version must be an integer >= 1".to_owned(),
43            ));
44        };
45
46        // Step 2: Let origin be the origin of the global scope used to
47        // access this IDBFactory.
48        let global = self.global();
49        let origin = global.origin();
50
51        // Step 3: if origin is an opaque origin,
52        // throw a "SecurityError" DOMException and abort these steps.
53        if let ImmutableOrigin::Opaque(_) = origin.immutable() {
54            return Err(Error::Security);
55        }
56
57        // Step 4: Let request be a new open request.
58        let request = IDBOpenDBRequest::new(&self.global(), CanGc::note());
59
60        // Step 5: Runs in parallel
61        if request.open_database(name, version).is_err() {
62            return Err(Error::Operation);
63        }
64
65        // Step 6
66        Ok(request)
67    }
68
69    // https://www.w3.org/TR/IndexedDB-2/#dom-idbfactory-deletedatabase
70    fn DeleteDatabase(&self, name: DOMString) -> Fallible<DomRoot<IDBOpenDBRequest>> {
71        // Step 1: Let origin be the origin of the global scope used to
72        // access this IDBFactory.
73        let global = self.global();
74        let origin = global.origin();
75
76        // Step 2: if origin is an opaque origin,
77        // throw a "SecurityError" DOMException and abort these steps.
78        if let ImmutableOrigin::Opaque(_) = origin.immutable() {
79            return Err(Error::Security);
80        }
81
82        // Step 3: Let request be a new open request
83        let request = IDBOpenDBRequest::new(&self.global(), CanGc::note());
84
85        // Step 4: Runs in parallel
86        if request.delete_database(name.to_string()).is_err() {
87            return Err(Error::Operation);
88        }
89
90        // Step 5: Return request
91        Ok(request)
92    }
93
94    // // https://www.w3.org/TR/IndexedDB-2/#dom-idbfactory-databases
95    // fn Databases(&self) -> Rc<Promise> {
96    //     unimplemented!();
97    // }
98
99    // https://www.w3.org/TR/IndexedDB-2/#dom-idbfactory-cmp
100    fn Cmp(&self, cx: SafeJSContext, first: HandleValue, second: HandleValue) -> Fallible<i16> {
101        let first_key = convert_value_to_key(cx, first, None)?.into_result()?;
102        let second_key = convert_value_to_key(cx, second, None)?.into_result()?;
103        let cmp = first_key.partial_cmp(&second_key);
104        if let Some(cmp) = cmp {
105            match cmp {
106                std::cmp::Ordering::Less => Ok(-1),
107                std::cmp::Ordering::Equal => Ok(0),
108                std::cmp::Ordering::Greater => Ok(1),
109            }
110        } else {
111            Ok(i16::MAX)
112        }
113    }
114}