tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

KeyPath.h (3573B)


      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/. */
      6 
      7 #ifndef mozilla_dom_indexeddb_keypath_h__
      8 #define mozilla_dom_indexeddb_keypath_h__
      9 
     10 #include <new>
     11 #include <utility>
     12 
     13 #include "js/TypeDecls.h"
     14 #include "mozilla/Result.h"
     15 #include "mozilla/ipc/IPCForwards.h"
     16 #include "nsError.h"
     17 #include "nsISupports.h"
     18 #include "nsString.h"
     19 #include "nsTArray.h"
     20 
     21 namespace JS {
     22 template <class T>
     23 class Heap;
     24 }
     25 
     26 namespace mozilla::dom {
     27 
     28 class OwningStringOrStringSequence;
     29 template <typename T>
     30 class Sequence;
     31 template <typename T>
     32 struct Nullable;
     33 
     34 namespace indexedDB {
     35 
     36 class IndexMetadata;
     37 class Key;
     38 class ObjectStoreMetadata;
     39 
     40 class KeyPath {
     41  // This private constructor is only to be used by IPDL-generated classes.
     42  friend class IndexMetadata;
     43  friend class ObjectStoreMetadata;
     44  ALLOW_DEPRECATED_READPARAM
     45 
     46  KeyPath() : mType(KeyPathType::NonExistent) { MOZ_COUNT_CTOR(KeyPath); }
     47 
     48 public:
     49  using VoidOrObjectStoreKeyPathString = nsAString;
     50 
     51  enum class KeyPathType { NonExistent, String, Array, EndGuard };
     52 
     53  void SetType(KeyPathType aType);
     54 
     55  bool AppendStringWithValidation(const nsAString& aString);
     56 
     57  explicit KeyPath(int aDummy) : mType(KeyPathType::NonExistent) {
     58    MOZ_COUNT_CTOR(KeyPath);
     59  }
     60 
     61  KeyPath(KeyPath&& aOther) {
     62    MOZ_COUNT_CTOR(KeyPath);
     63    *this = std::move(aOther);
     64  }
     65  KeyPath& operator=(KeyPath&&) = default;
     66 
     67  KeyPath(const KeyPath& aOther) {
     68    MOZ_COUNT_CTOR(KeyPath);
     69    *this = aOther;
     70  }
     71  KeyPath& operator=(const KeyPath&) = default;
     72 
     73  MOZ_COUNTED_DTOR(KeyPath)
     74 
     75  static Result<KeyPath, nsresult> Parse(const nsAString& aString);
     76 
     77  static Result<KeyPath, nsresult> Parse(const Sequence<nsString>& aStrings);
     78 
     79  static Result<KeyPath, nsresult> Parse(
     80      const Nullable<OwningStringOrStringSequence>& aValue);
     81 
     82  nsresult ExtractKey(
     83      JSContext* aCx, const JS::Value& aValue, Key& aKey,
     84      const VoidOrObjectStoreKeyPathString& aAutoIncrementedObjectStoreKeyPath =
     85          VoidString()) const;
     86 
     87  nsresult ExtractKeyAsJSVal(JSContext* aCx, const JS::Value& aValue,
     88                             JS::Value* aOutVal) const;
     89 
     90  using ExtractOrCreateKeyCallback = nsresult (*)(JSContext*, void*);
     91 
     92  nsresult ExtractOrCreateKey(JSContext* aCx, const JS::Value& aValue,
     93                              Key& aKey, ExtractOrCreateKeyCallback aCallback,
     94                              void* aClosure) const;
     95 
     96  inline bool IsValid() const { return mType != KeyPathType::NonExistent; }
     97 
     98  inline bool IsArray() const { return mType == KeyPathType::Array; }
     99 
    100  inline bool IsString() const { return mType == KeyPathType::String; }
    101 
    102  inline bool IsEmpty() const {
    103    return mType == KeyPathType::String && mStrings[0].IsEmpty();
    104  }
    105 
    106  bool operator==(const KeyPath& aOther) const {
    107    return mType == aOther.mType && mStrings == aOther.mStrings;
    108  }
    109 
    110  nsAutoString SerializeToString() const;
    111  static KeyPath DeserializeFromString(const nsAString& aString);
    112 
    113  nsresult ToJSVal(JSContext* aCx, JS::MutableHandle<JS::Value> aValue) const;
    114  nsresult ToJSVal(JSContext* aCx, JS::Heap<JS::Value>& aValue) const;
    115 
    116  bool IsAllowedForObjectStore(bool aAutoIncrement) const;
    117 
    118  KeyPathType mType;
    119 
    120  CopyableTArray<nsString> mStrings;
    121 };
    122 
    123 }  // namespace indexedDB
    124 }  // namespace mozilla::dom
    125 
    126 #endif  // mozilla_dom_indexeddb_keypath_h__