diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /toolkit/components/extensions/MatchGlob.h | |
parent | Initial commit. (diff) | |
download | firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/components/extensions/MatchGlob.h')
-rw-r--r-- | toolkit/components/extensions/MatchGlob.h | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/toolkit/components/extensions/MatchGlob.h b/toolkit/components/extensions/MatchGlob.h new file mode 100644 index 0000000000..3b2069e185 --- /dev/null +++ b/toolkit/components/extensions/MatchGlob.h @@ -0,0 +1,93 @@ +/* -*- 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 "nsCOMPtr.h" +#include "nsCycleCollectionParticipant.h" +#include "nsISupports.h" +#include "nsWrapperCache.h" + +namespace mozilla { +namespace extensions { + +class MatchPattern; + +class MatchGlob final : public nsISupports, public nsWrapperCache { + public: + NS_DECL_CYCLE_COLLECTING_ISUPPORTS + NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(MatchGlob) + + static already_AddRefed<MatchGlob> Constructor(dom::GlobalObject& aGlobal, + const nsAString& aGlob, + bool aAllowQuestion, + ErrorResult& aRv); + + bool Matches(const nsAString& aString) const; + + bool IsWildcard() const { return mIsPrefix && mPathLiteral.IsEmpty(); } + + void GetGlob(nsAString& aGlob) const { aGlob = mGlob; } + + nsISupports* GetParentObject() const { return mParent; } + + virtual JSObject* WrapObject(JSContext* aCx, + JS::HandleObject aGivenProto) override; + + protected: + virtual ~MatchGlob(); + + private: + friend class MatchPattern; + + explicit MatchGlob(nsISupports* aParent) : mParent(aParent) {} + + void Init(JSContext* aCx, const nsAString& aGlob, bool aAllowQuestion, + ErrorResult& aRv); + + nsCOMPtr<nsISupports> mParent; + + // The original glob string that this glob object represents. + nsString 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. + nsString 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. + JS::Heap<JSObject*> mRegExp; +}; + +class MatchGlobSet final : public CopyableTArray<RefPtr<MatchGlob>> { + 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<MatchGlob>> aIL) + : CopyableTArray(aIL) {} + + bool Matches(const nsAString& aValue) const; +}; + +} // namespace extensions +} // namespace mozilla + +#endif // mozilla_extensions_MatchGlob_h |