summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/MatchGlob.h
blob: cfbb5f2f16c5ac82b6c91cd0c82e10effe2563c3 (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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
/* 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_extensions_MatchGlob_h
#define mozilla_extensions_MatchGlob_h

#include "mozilla/dom/BindingDeclarations.h"
#include "mozilla/dom/MatchGlobBinding.h"

#include "jspubtd.h"
#include "js/RootingAPI.h"

#include "mozilla/RustRegex.h"
#include "nsCOMPtr.h"
#include "nsCycleCollectionParticipant.h"
#include "nsISupports.h"
#include "nsWrapperCache.h"

namespace mozilla {
namespace extensions {

class MatchPattern;

class MatchGlobCore final {
 public:
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MatchGlobCore)

  MatchGlobCore(const nsACString& aGlob, bool aAllowQuestion, ErrorResult& aRv);

  bool Matches(const nsACString& aString) const;

  bool IsWildcard() const { return mIsPrefix && mPathLiteral.IsEmpty(); }

  void GetGlob(nsACString& aGlob) const { aGlob = mGlob; }

 private:
  ~MatchGlobCore() = default;

  // The original glob string that this glob object represents.
  const nsCString mGlob;

  // The literal path string to match against. If this contains a non-void
  // value, the glob matches against this exact literal string, rather than
  // performng a pattern match. If mIsPrefix is true, the literal must appear
  // at the start of the matched string. If it is false, the the literal must
  // be exactly equal to the matched string.
  nsCString mPathLiteral;
  bool mIsPrefix = false;

  // The regular expression object which is equivalent to this glob pattern.
  // Used for matching if, and only if, mPathLiteral is non-void.
  RustRegex mRegExp;
};

class MatchGlob final : public nsISupports, public nsWrapperCache {
 public:
  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(MatchGlob)

  static already_AddRefed<MatchGlob> Constructor(dom::GlobalObject& aGlobal,
                                                 const nsACString& aGlob,
                                                 bool aAllowQuestion,
                                                 ErrorResult& aRv);

  explicit MatchGlob(nsISupports* aParent,
                     already_AddRefed<MatchGlobCore> aCore)
      : mParent(aParent), mCore(std::move(aCore)) {}

  bool Matches(const nsACString& aString) const {
    return Core()->Matches(aString);
  }

  bool IsWildcard() const { return Core()->IsWildcard(); }

  void GetGlob(nsACString& aGlob) const { Core()->GetGlob(aGlob); }

  MatchGlobCore* Core() const { return mCore; }

  nsISupports* GetParentObject() const { return mParent; }

  virtual JSObject* WrapObject(JSContext* aCx,
                               JS::Handle<JSObject*> aGivenProto) override;

 private:
  ~MatchGlob() = default;

  nsCOMPtr<nsISupports> mParent;

  RefPtr<MatchGlobCore> mCore;
};

class MatchGlobSet final : public CopyableTArray<RefPtr<MatchGlobCore>> {
 public:
  // Note: We can't use the nsTArray constructors directly, since the static
  // analyzer doesn't handle their MOZ_IMPLICIT annotations correctly.
  MatchGlobSet() = default;
  explicit MatchGlobSet(size_type aCapacity) : CopyableTArray(aCapacity) {}
  explicit MatchGlobSet(const nsTArray& aOther) : CopyableTArray(aOther) {}
  MOZ_IMPLICIT MatchGlobSet(nsTArray&& aOther)
      : CopyableTArray(std::move(aOther)) {}
  MOZ_IMPLICIT MatchGlobSet(std::initializer_list<RefPtr<MatchGlobCore>> aIL)
      : CopyableTArray(aIL) {}

  bool Matches(const nsACString& aValue) const;
};

}  // namespace extensions
}  // namespace mozilla

#endif  // mozilla_extensions_MatchGlob_h