summaryrefslogtreecommitdiffstats
path: root/dom/indexedDB/KeyPath.h
blob: 4e1042650fc8e4a013728235f1b5e9fd0ccb0a4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_dom_indexeddb_keypath_h__
#define mozilla_dom_indexeddb_keypath_h__

#include <new>
#include <utility>
#include "js/TypeDecls.h"
#include "mozilla/Result.h"
#include "mozilla/ipc/IPCForwards.h"
#include "nsISupports.h"
#include "nsError.h"
#include "nsString.h"
#include "nsTArray.h"

namespace JS {
template <class T>
class Heap;
}

namespace mozilla::dom {

class OwningStringOrStringSequence;
template <typename T>
class Sequence;
template <typename T>
struct Nullable;

namespace indexedDB {

class IndexMetadata;
class Key;
class ObjectStoreMetadata;

class KeyPath {
  // This private constructor is only to be used by IPDL-generated classes.
  friend class IndexMetadata;
  friend class ObjectStoreMetadata;
  ALLOW_DEPRECATED_READPARAM

  KeyPath() : mType(KeyPathType::NonExistent) { MOZ_COUNT_CTOR(KeyPath); }

 public:
  enum class KeyPathType { NonExistent, String, Array, EndGuard };

  void SetType(KeyPathType aType);

  bool AppendStringWithValidation(const nsAString& aString);

  explicit KeyPath(int aDummy) : mType(KeyPathType::NonExistent) {
    MOZ_COUNT_CTOR(KeyPath);
  }

  KeyPath(KeyPath&& aOther) {
    MOZ_COUNT_CTOR(KeyPath);
    *this = std::move(aOther);
  }
  KeyPath& operator=(KeyPath&&) = default;

  KeyPath(const KeyPath& aOther) {
    MOZ_COUNT_CTOR(KeyPath);
    *this = aOther;
  }
  KeyPath& operator=(const KeyPath&) = default;

  MOZ_COUNTED_DTOR(KeyPath)

  static Result<KeyPath, nsresult> Parse(const nsAString& aString);

  static Result<KeyPath, nsresult> Parse(const Sequence<nsString>& aStrings);

  static Result<KeyPath, nsresult> Parse(
      const Nullable<OwningStringOrStringSequence>& aValue);

  nsresult ExtractKey(JSContext* aCx, const JS::Value& aValue, Key& aKey) const;

  nsresult ExtractKeyAsJSVal(JSContext* aCx, const JS::Value& aValue,
                             JS::Value* aOutVal) const;

  using ExtractOrCreateKeyCallback = nsresult (*)(JSContext*, void*);

  nsresult ExtractOrCreateKey(JSContext* aCx, const JS::Value& aValue,
                              Key& aKey, ExtractOrCreateKeyCallback aCallback,
                              void* aClosure) const;

  inline bool IsValid() const { return mType != KeyPathType::NonExistent; }

  inline bool IsArray() const { return mType == KeyPathType::Array; }

  inline bool IsString() const { return mType == KeyPathType::String; }

  inline bool IsEmpty() const {
    return mType == KeyPathType::String && mStrings[0].IsEmpty();
  }

  bool operator==(const KeyPath& aOther) const {
    return mType == aOther.mType && mStrings == aOther.mStrings;
  }

  nsAutoString SerializeToString() const;
  static KeyPath DeserializeFromString(const nsAString& aString);

  nsresult ToJSVal(JSContext* aCx, JS::MutableHandle<JS::Value> aValue) const;
  nsresult ToJSVal(JSContext* aCx, JS::Heap<JS::Value>& aValue) const;

  bool IsAllowedForObjectStore(bool aAutoIncrement) const;

  KeyPathType mType;

  CopyableTArray<nsString> mStrings;
};

}  // namespace indexedDB
}  // namespace mozilla::dom

#endif  // mozilla_dom_indexeddb_keypath_h__