1 /* -*- Mode: C++; tab-width: 2; 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/. */
6 #ifndef mozilla_extensions_WebExtensionContentScript_h
7 #define mozilla_extensions_WebExtensionContentScript_h
9 #include "mozilla/dom/BindingDeclarations.h"
10 #include "mozilla/dom/WebExtensionContentScriptBinding.h"
14 #include "mozilla/Maybe.h"
15 #include "mozilla/Variant.h"
16 #include "mozilla/extensions/MatchGlob.h"
17 #include "mozilla/extensions/MatchPattern.h"
19 #include "nsCycleCollectionParticipant.h"
20 #include "nsISupports.h"
21 #include "nsIDocShell.h"
22 #include "nsPIDOMWindow.h"
23 #include "nsWrapperCache.h"
26 class nsPIDOMWindowOuter
;
30 class WindowGlobalChild
;
33 namespace extensions
{
36 using ContentScriptInit
= dom::WebExtensionContentScriptInit
;
38 class WebExtensionPolicy
;
40 class MOZ_STACK_CLASS DocInfo final
{
42 DocInfo(const URLInfo
& aURL
, nsILoadInfo
* aLoadInfo
);
44 MOZ_IMPLICIT
DocInfo(nsPIDOMWindowOuter
* aWindow
);
46 const URLInfo
& URL() const { return mURL
; }
48 // The principal of the document, or the expected principal of a request.
49 // May be null for non-DOMWindow DocInfo objects unless
50 // URL().InheritsPrincipal() is true.
51 nsIPrincipal
* Principal() const;
53 // Returns the URL of the document's principal. Note that this must *only*
54 // be called for content principals.
55 const URLInfo
& PrincipalURL() const;
57 bool IsTopLevel() const;
58 bool ShouldMatchActiveTabPermission() const;
60 uint64_t FrameID() const;
62 nsPIDOMWindowOuter
* GetWindow() const {
63 if (mObj
.is
<Window
>()) {
64 return mObj
.as
<Window
>();
69 nsILoadInfo
* GetLoadInfo() const {
70 if (mObj
.is
<LoadInfo
>()) {
71 return mObj
.as
<LoadInfo
>();
76 already_AddRefed
<nsILoadContext
> GetLoadContext() const {
77 nsCOMPtr
<nsILoadContext
> loadContext
;
78 if (nsPIDOMWindowOuter
* window
= GetWindow()) {
79 nsIDocShell
* docShell
= window
->GetDocShell();
80 loadContext
= do_QueryInterface(docShell
);
81 } else if (nsILoadInfo
* loadInfo
= GetLoadInfo()) {
82 nsCOMPtr
<nsISupports
> requestingContext
= loadInfo
->GetLoadingContext();
83 loadContext
= do_QueryInterface(requestingContext
);
85 return loadContext
.forget();
89 void SetURL(const URLInfo
& aURL
);
92 mutable Maybe
<const URLInfo
> mPrincipalURL
;
94 mutable Maybe
<bool> mIsTopLevel
;
96 mutable Maybe
<nsCOMPtr
<nsIPrincipal
>> mPrincipal
;
97 mutable Maybe
<uint64_t> mFrameID
;
99 using Window
= nsPIDOMWindowOuter
*;
100 using LoadInfo
= nsILoadInfo
*;
102 const Variant
<LoadInfo
, Window
> mObj
;
105 class MozDocumentMatcher
: public nsISupports
, public nsWrapperCache
{
106 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
107 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(MozDocumentMatcher
)
109 using MatchGlobArray
= nsTArray
<RefPtr
<MatchGlob
>>;
111 static already_AddRefed
<MozDocumentMatcher
> Constructor(
112 dom::GlobalObject
& aGlobal
, const dom::MozDocumentMatcherInit
& aInit
,
115 bool Matches(const DocInfo
& aDoc
) const;
116 bool MatchesURI(const URLInfo
& aURL
) const;
118 bool MatchesWindowGlobal(dom::WindowGlobalChild
& aWindow
) const;
120 WebExtensionPolicy
* GetExtension() { return mExtension
; }
122 WebExtensionPolicy
* Extension() { return mExtension
; }
123 const WebExtensionPolicy
* Extension() const { return mExtension
; }
125 bool AllFrames() const { return mAllFrames
; }
126 bool CheckPermissions() const { return mCheckPermissions
; }
127 bool MatchAboutBlank() const { return mMatchAboutBlank
; }
129 MatchPatternSet
* Matches() { return mMatches
; }
130 const MatchPatternSet
* GetMatches() const { return mMatches
; }
132 MatchPatternSet
* GetExcludeMatches() { return mExcludeMatches
; }
133 const MatchPatternSet
* GetExcludeMatches() const { return mExcludeMatches
; }
135 void GetIncludeGlobs(Nullable
<MatchGlobArray
>& aGlobs
) {
136 ToNullable(mExcludeGlobs
, aGlobs
);
138 void GetExcludeGlobs(Nullable
<MatchGlobArray
>& aGlobs
) {
139 ToNullable(mExcludeGlobs
, aGlobs
);
142 Nullable
<uint64_t> GetFrameID() const { return mFrameID
; }
144 void GetOriginAttributesPatterns(JSContext
* aCx
,
145 JS::MutableHandle
<JS::Value
> aVal
,
146 ErrorResult
& aError
) const;
148 WebExtensionPolicy
* GetParentObject() const { return mExtension
; }
149 virtual JSObject
* WrapObject(JSContext
* aCx
,
150 JS::HandleObject aGivenProto
) override
;
153 friend class WebExtensionPolicy
;
155 virtual ~MozDocumentMatcher() = default;
157 MozDocumentMatcher(dom::GlobalObject
& aGlobal
,
158 const dom::MozDocumentMatcherInit
& aInit
, bool aRestricted
,
161 RefPtr
<WebExtensionPolicy
> mExtension
;
163 bool mHasActiveTabPermission
;
166 RefPtr
<MatchPatternSet
> mMatches
;
167 RefPtr
<MatchPatternSet
> mExcludeMatches
;
169 Nullable
<MatchGlobSet
> mIncludeGlobs
;
170 Nullable
<MatchGlobSet
> mExcludeGlobs
;
173 bool mCheckPermissions
;
174 Nullable
<uint64_t> mFrameID
;
175 bool mMatchAboutBlank
;
176 Nullable
<dom::Sequence
<OriginAttributesPattern
>> mOriginAttributesPatterns
;
179 template <typename T
, typename U
>
180 void ToNullable(const Nullable
<T
>& aInput
, Nullable
<U
>& aOutput
) {
181 if (aInput
.IsNull()) {
184 aOutput
.SetValue(aInput
.Value());
188 template <typename T
, typename U
>
189 void ToNullable(const Nullable
<T
>& aInput
, Nullable
<nsTArray
<U
>>& aOutput
) {
190 if (aInput
.IsNull()) {
193 aOutput
.SetValue(aInput
.Value().Clone());
198 class WebExtensionContentScript final
: public MozDocumentMatcher
{
200 using RunAtEnum
= dom::ContentScriptRunAt
;
202 static already_AddRefed
<WebExtensionContentScript
> Constructor(
203 dom::GlobalObject
& aGlobal
, WebExtensionPolicy
& aExtension
,
204 const ContentScriptInit
& aInit
, ErrorResult
& aRv
);
206 RunAtEnum
RunAt() const { return mRunAt
; }
208 void GetCssPaths(nsTArray
<nsString
>& aPaths
) const {
209 aPaths
.AppendElements(mCssPaths
);
211 void GetJsPaths(nsTArray
<nsString
>& aPaths
) const {
212 aPaths
.AppendElements(mJsPaths
);
215 virtual JSObject
* WrapObject(JSContext
* aCx
,
216 JS::HandleObject aGivenProto
) override
;
219 friend class WebExtensionPolicy
;
221 virtual ~WebExtensionContentScript() = default;
223 WebExtensionContentScript(dom::GlobalObject
& aGlobal
,
224 WebExtensionPolicy
& aExtension
,
225 const ContentScriptInit
& aInit
, ErrorResult
& aRv
);
228 nsTArray
<nsString
> mCssPaths
;
229 nsTArray
<nsString
> mJsPaths
;
234 } // namespace extensions
235 } // namespace mozilla
237 #endif // mozilla_extensions_WebExtensionContentScript_h