1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=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/. */
7 #ifndef mozilla_dom_indexeddb_keypath_h__
8 #define mozilla_dom_indexeddb_keypath_h__
12 #include "js/TypeDecls.h"
13 #include "mozilla/Result.h"
14 #include "mozilla/ipc/IPCForwards.h"
15 #include "nsISupports.h"
25 namespace mozilla::dom
{
27 class OwningStringOrStringSequence
;
37 class ObjectStoreMetadata
;
40 // This private constructor is only to be used by IPDL-generated classes.
41 friend class IndexMetadata
;
42 friend class ObjectStoreMetadata
;
43 ALLOW_DEPRECATED_READPARAM
45 KeyPath() : mType(KeyPathType::NonExistent
) { MOZ_COUNT_CTOR(KeyPath
); }
48 using VoidOrObjectStoreKeyPathString
= nsAString
;
50 enum class KeyPathType
{ NonExistent
, String
, Array
, EndGuard
};
52 void SetType(KeyPathType aType
);
54 bool AppendStringWithValidation(const nsAString
& aString
);
56 explicit KeyPath(int aDummy
) : mType(KeyPathType::NonExistent
) {
57 MOZ_COUNT_CTOR(KeyPath
);
60 KeyPath(KeyPath
&& aOther
) {
61 MOZ_COUNT_CTOR(KeyPath
);
62 *this = std::move(aOther
);
64 KeyPath
& operator=(KeyPath
&&) = default;
66 KeyPath(const KeyPath
& aOther
) {
67 MOZ_COUNT_CTOR(KeyPath
);
70 KeyPath
& operator=(const KeyPath
&) = default;
72 MOZ_COUNTED_DTOR(KeyPath
)
74 static Result
<KeyPath
, nsresult
> Parse(const nsAString
& aString
);
76 static Result
<KeyPath
, nsresult
> Parse(const Sequence
<nsString
>& aStrings
);
78 static Result
<KeyPath
, nsresult
> Parse(
79 const Nullable
<OwningStringOrStringSequence
>& aValue
);
82 JSContext
* aCx
, const JS::Value
& aValue
, Key
& aKey
,
83 const VoidOrObjectStoreKeyPathString
& aAutoIncrementedObjectStoreKeyPath
=
86 nsresult
ExtractKeyAsJSVal(JSContext
* aCx
, const JS::Value
& aValue
,
87 JS::Value
* aOutVal
) const;
89 using ExtractOrCreateKeyCallback
= nsresult (*)(JSContext
*, void*);
91 nsresult
ExtractOrCreateKey(JSContext
* aCx
, const JS::Value
& aValue
,
92 Key
& aKey
, ExtractOrCreateKeyCallback aCallback
,
93 void* aClosure
) const;
95 inline bool IsValid() const { return mType
!= KeyPathType::NonExistent
; }
97 inline bool IsArray() const { return mType
== KeyPathType::Array
; }
99 inline bool IsString() const { return mType
== KeyPathType::String
; }
101 inline bool IsEmpty() const {
102 return mType
== KeyPathType::String
&& mStrings
[0].IsEmpty();
105 bool operator==(const KeyPath
& aOther
) const {
106 return mType
== aOther
.mType
&& mStrings
== aOther
.mStrings
;
109 nsAutoString
SerializeToString() const;
110 static KeyPath
DeserializeFromString(const nsAString
& aString
);
112 nsresult
ToJSVal(JSContext
* aCx
, JS::MutableHandle
<JS::Value
> aValue
) const;
113 nsresult
ToJSVal(JSContext
* aCx
, JS::Heap
<JS::Value
>& aValue
) const;
115 bool IsAllowedForObjectStore(bool aAutoIncrement
) const;
119 CopyableTArray
<nsString
> mStrings
;
122 } // namespace indexedDB
123 } // namespace mozilla::dom
125 #endif // mozilla_dom_indexeddb_keypath_h__