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 enum class KeyPathType
{ NonExistent
, String
, Array
, EndGuard
};
50 void SetType(KeyPathType aType
);
52 bool AppendStringWithValidation(const nsAString
& aString
);
54 explicit KeyPath(int aDummy
) : mType(KeyPathType::NonExistent
) {
55 MOZ_COUNT_CTOR(KeyPath
);
58 KeyPath(KeyPath
&& aOther
) {
59 MOZ_COUNT_CTOR(KeyPath
);
60 *this = std::move(aOther
);
62 KeyPath
& operator=(KeyPath
&&) = default;
64 KeyPath(const KeyPath
& aOther
) {
65 MOZ_COUNT_CTOR(KeyPath
);
68 KeyPath
& operator=(const KeyPath
&) = default;
70 MOZ_COUNTED_DTOR(KeyPath
)
72 static Result
<KeyPath
, nsresult
> Parse(const nsAString
& aString
);
74 static Result
<KeyPath
, nsresult
> Parse(const Sequence
<nsString
>& aStrings
);
76 static Result
<KeyPath
, nsresult
> Parse(
77 const Nullable
<OwningStringOrStringSequence
>& aValue
);
79 nsresult
ExtractKey(JSContext
* aCx
, const JS::Value
& aValue
, Key
& aKey
) const;
81 nsresult
ExtractKeyAsJSVal(JSContext
* aCx
, const JS::Value
& aValue
,
82 JS::Value
* aOutVal
) const;
84 using ExtractOrCreateKeyCallback
= nsresult (*)(JSContext
*, void*);
86 nsresult
ExtractOrCreateKey(JSContext
* aCx
, const JS::Value
& aValue
,
87 Key
& aKey
, ExtractOrCreateKeyCallback aCallback
,
88 void* aClosure
) const;
90 inline bool IsValid() const { return mType
!= KeyPathType::NonExistent
; }
92 inline bool IsArray() const { return mType
== KeyPathType::Array
; }
94 inline bool IsString() const { return mType
== KeyPathType::String
; }
96 inline bool IsEmpty() const {
97 return mType
== KeyPathType::String
&& mStrings
[0].IsEmpty();
100 bool operator==(const KeyPath
& aOther
) const {
101 return mType
== aOther
.mType
&& mStrings
== aOther
.mStrings
;
104 nsAutoString
SerializeToString() const;
105 static KeyPath
DeserializeFromString(const nsAString
& aString
);
107 nsresult
ToJSVal(JSContext
* aCx
, JS::MutableHandle
<JS::Value
> aValue
) const;
108 nsresult
ToJSVal(JSContext
* aCx
, JS::Heap
<JS::Value
>& aValue
) const;
110 bool IsAllowedForObjectStore(bool aAutoIncrement
) const;
114 CopyableTArray
<nsString
> mStrings
;
117 } // namespace indexedDB
118 } // namespace mozilla::dom
120 #endif // mozilla_dom_indexeddb_keypath_h__