script/dom/indexeddb/
idbindex.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::gc::MutableHandleValue;
6use script_bindings::codegen::GenericBindings::IDBIndexBinding::IDBIndexMethods;
7use script_bindings::conversions::SafeToJSValConvertible;
8use script_bindings::str::DOMString;
9
10use crate::dom::bindings::import::base::SafeJSContext;
11use crate::dom::bindings::reflector::{Reflector, reflect_dom_object};
12use crate::dom::bindings::root::DomRoot;
13use crate::dom::globalscope::GlobalScope;
14use crate::dom::idbobjectstore::KeyPath;
15use crate::dom::indexeddb::idbobjectstore::IDBObjectStore;
16use crate::script_runtime::CanGc;
17
18#[dom_struct]
19pub(crate) struct IDBIndex {
20    reflector_: Reflector,
21    object_store: DomRoot<IDBObjectStore>,
22    name: DOMString,
23    multi_entry: bool,
24    unique: bool,
25    key_path: KeyPath,
26}
27
28impl IDBIndex {
29    pub fn new_inherited(
30        object_store: DomRoot<IDBObjectStore>,
31        name: DOMString,
32        multi_entry: bool,
33        unique: bool,
34        key_path: KeyPath,
35    ) -> IDBIndex {
36        IDBIndex {
37            reflector_: Reflector::new(),
38            object_store,
39            name,
40            multi_entry,
41            unique,
42            key_path,
43        }
44    }
45
46    pub fn new(
47        global: &GlobalScope,
48        object_store: DomRoot<IDBObjectStore>,
49        name: DOMString,
50        multi_entry: bool,
51        unique: bool,
52        key_path: KeyPath,
53        can_gc: CanGc,
54    ) -> DomRoot<IDBIndex> {
55        reflect_dom_object(
56            Box::new(IDBIndex::new_inherited(
57                object_store,
58                name,
59                multi_entry,
60                unique,
61                key_path,
62            )),
63            global,
64            can_gc,
65        )
66    }
67}
68
69impl IDBIndexMethods<crate::DomTypeHolder> for IDBIndex {
70    /// <https://www.w3.org/TR/IndexedDB/#dom-idbindex-objectstore>
71    fn ObjectStore(&self) -> DomRoot<IDBObjectStore> {
72        self.object_store.clone()
73    }
74
75    /// <https://www.w3.org/TR/IndexedDB/#dom-idbindex-multientry>
76    fn MultiEntry(&self) -> bool {
77        self.multi_entry
78    }
79
80    /// <https://www.w3.org/TR/IndexedDB/#dom-idbindex-unique>
81    fn Unique(&self) -> bool {
82        self.unique
83    }
84
85    /// <https://www.w3.org/TR/IndexedDB/#dom-idbindex-keypath>
86    fn KeyPath(&self, cx: SafeJSContext, can_gc: CanGc, retval: MutableHandleValue) {
87        match &self.key_path {
88            KeyPath::String(string) => {
89                string.safe_to_jsval(cx, retval, can_gc);
90            },
91            KeyPath::StringSequence(sequence) => {
92                sequence.safe_to_jsval(cx, retval, can_gc);
93            },
94        }
95    }
96}