KeyPath.h 3.0 KB

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