|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim: set ts=2 et sw=2 tw=80: */ |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #ifndef mozilla_dom_indexeddb_keypath_h__ |
|
8 #define mozilla_dom_indexeddb_keypath_h__ |
|
9 |
|
10 #include "mozilla/dom/indexedDB/IndexedDatabase.h" |
|
11 |
|
12 #include "mozilla/dom/BindingDeclarations.h" |
|
13 |
|
14 BEGIN_INDEXEDDB_NAMESPACE |
|
15 |
|
16 class Key; |
|
17 |
|
18 class KeyPath |
|
19 { |
|
20 public: |
|
21 enum KeyPathType { |
|
22 NONEXISTENT, |
|
23 STRING, |
|
24 ARRAY, |
|
25 ENDGUARD |
|
26 }; |
|
27 |
|
28 void SetType(KeyPathType aType); |
|
29 |
|
30 // This does not set exceptions. |
|
31 bool AppendStringWithValidation(JSContext* aCx, const nsAString& aString); |
|
32 |
|
33 explicit KeyPath(int aDummy) |
|
34 : mType(NONEXISTENT) |
|
35 { |
|
36 MOZ_COUNT_CTOR(KeyPath); |
|
37 } |
|
38 |
|
39 KeyPath(const KeyPath& aOther) |
|
40 { |
|
41 MOZ_COUNT_CTOR(KeyPath); |
|
42 *this = aOther; |
|
43 } |
|
44 |
|
45 ~KeyPath() |
|
46 { |
|
47 MOZ_COUNT_DTOR(KeyPath); |
|
48 } |
|
49 |
|
50 static nsresult |
|
51 Parse(JSContext* aCx, const nsAString& aString, KeyPath* aKeyPath); |
|
52 |
|
53 static nsresult |
|
54 Parse(JSContext* aCx, const Sequence<nsString>& aStrings, KeyPath* aKeyPath); |
|
55 |
|
56 static nsresult |
|
57 Parse(JSContext* aCx, const JS::Value& aValue, KeyPath* aKeyPath); |
|
58 |
|
59 nsresult |
|
60 ExtractKey(JSContext* aCx, const JS::Value& aValue, Key& aKey) const; |
|
61 |
|
62 nsresult |
|
63 ExtractKeyAsJSVal(JSContext* aCx, const JS::Value& aValue, |
|
64 JS::Value* aOutVal) const; |
|
65 |
|
66 typedef nsresult |
|
67 (*ExtractOrCreateKeyCallback)(JSContext* aCx, void* aClosure); |
|
68 |
|
69 nsresult |
|
70 ExtractOrCreateKey(JSContext* aCx, const JS::Value& aValue, Key& aKey, |
|
71 ExtractOrCreateKeyCallback aCallback, |
|
72 void* aClosure) const; |
|
73 |
|
74 inline bool IsValid() const { |
|
75 return mType != NONEXISTENT; |
|
76 } |
|
77 |
|
78 inline bool IsArray() const { |
|
79 return mType == ARRAY; |
|
80 } |
|
81 |
|
82 inline bool IsString() const { |
|
83 return mType == STRING; |
|
84 } |
|
85 |
|
86 inline bool IsEmpty() const { |
|
87 return mType == STRING && mStrings[0].IsEmpty(); |
|
88 } |
|
89 |
|
90 bool operator==(const KeyPath& aOther) const |
|
91 { |
|
92 return mType == aOther.mType && mStrings == aOther.mStrings; |
|
93 } |
|
94 |
|
95 void SerializeToString(nsAString& aString) const; |
|
96 static KeyPath DeserializeFromString(const nsAString& aString); |
|
97 |
|
98 nsresult ToJSVal(JSContext* aCx, JS::MutableHandle<JS::Value> aValue) const; |
|
99 nsresult ToJSVal(JSContext* aCx, JS::Heap<JS::Value>& aValue) const; |
|
100 |
|
101 bool IsAllowedForObjectStore(bool aAutoIncrement) const; |
|
102 |
|
103 KeyPathType mType; |
|
104 |
|
105 nsTArray<nsString> mStrings; |
|
106 }; |
|
107 |
|
108 END_INDEXEDDB_NAMESPACE |
|
109 |
|
110 #endif /* mozilla_dom_indexeddb_keypath_h__ */ |