Skip to main content

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