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 IsSameOriginWithTop() const;
59 bool ShouldMatchActiveTabPermission() const;
61 uint64_t FrameID() const;
63 nsPIDOMWindowOuter
* GetWindow() const {
64 if (mObj
.is
<Window
>()) {
65 return mObj
.as
<Window
>();
70 nsILoadInfo
* GetLoadInfo() const {
71 if (mObj
.is
<LoadInfo
>()) {
72 return mObj
.as
<LoadInfo
>();
77 already_AddRefed
<nsILoadContext
> GetLoadContext() const {
78 nsCOMPtr
<nsILoadContext
> loadContext
;
79 if (nsPIDOMWindowOuter
* window
= GetWindow()) {
80 nsIDocShell
* docShell
= window
->GetDocShell();
81 loadContext
= do_QueryInterface(docShell
);
82 } else if (nsILoadInfo
* loadInfo
= GetLoadInfo()) {
83 nsCOMPtr
<nsISupports
> requestingContext
= loadInfo
->GetLoadingContext();
84 loadContext
= do_QueryInterface(requestingContext
);
86 return loadContext
.forget();
90 void SetURL(const URLInfo
& aURL
);
93 mutable Maybe
<const URLInfo
> mPrincipalURL
;
95 mutable Maybe
<bool> mIsTopLevel
;
97 mutable Maybe
<nsCOMPtr
<nsIPrincipal
>> mPrincipal
;
98 mutable Maybe
<uint64_t> mFrameID
;
100 using Window
= nsPIDOMWindowOuter
*;
101 using LoadInfo
= nsILoadInfo
*;
103 const Variant
<LoadInfo
, Window
> mObj
;
106 class MozDocumentMatcher
: public nsISupports
, public nsWrapperCache
{
107 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
108 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(MozDocumentMatcher
)
110 using MatchGlobArray
= nsTArray
<RefPtr
<MatchGlob
>>;
112 static already_AddRefed
<MozDocumentMatcher
> Constructor(
113 dom::GlobalObject
& aGlobal
, const dom::MozDocumentMatcherInit
& aInit
,
116 bool Matches(const DocInfo
& aDoc
, bool aIgnorePermissions
) const;
117 bool Matches(const DocInfo
& aDoc
) const { return Matches(aDoc
, false); }
119 bool MatchesURI(const URLInfo
& aURL
, bool aIgnorePermissions
) const;
120 bool MatchesURI(const URLInfo
& aURL
) const { return MatchesURI(aURL
, false); }
122 bool MatchesWindowGlobal(dom::WindowGlobalChild
& aWindow
,
123 bool aIgnorePermissions
) const;
125 WebExtensionPolicy
* GetExtension() { return mExtension
; }
127 WebExtensionPolicy
* Extension() { return mExtension
; }
128 const WebExtensionPolicy
* Extension() const { return mExtension
; }
130 bool AllFrames() const { return mAllFrames
; }
131 bool CheckPermissions() const { return mCheckPermissions
; }
132 bool MatchAboutBlank() const { return mMatchAboutBlank
; }
134 MatchPatternSet
* Matches() { return mMatches
; }
135 const MatchPatternSet
* GetMatches() const { return mMatches
; }
137 MatchPatternSet
* GetExcludeMatches() { return mExcludeMatches
; }
138 const MatchPatternSet
* GetExcludeMatches() const { return mExcludeMatches
; }
140 Nullable
<uint64_t> GetFrameID() const { return mFrameID
; }
142 void GetOriginAttributesPatterns(JSContext
* aCx
,
143 JS::MutableHandle
<JS::Value
> aVal
,
144 ErrorResult
& aError
) const;
146 WebExtensionPolicy
* GetParentObject() const { return mExtension
; }
147 virtual JSObject
* WrapObject(JSContext
* aCx
,
148 JS::Handle
<JSObject
*> aGivenProto
) override
;
151 friend class WebExtensionPolicy
;
153 virtual ~MozDocumentMatcher() = default;
155 MozDocumentMatcher(dom::GlobalObject
& aGlobal
,
156 const dom::MozDocumentMatcherInit
& aInit
, bool aRestricted
,
159 RefPtr
<WebExtensionPolicy
> mExtension
;
161 bool mHasActiveTabPermission
;
164 RefPtr
<MatchPatternSet
> mMatches
;
165 RefPtr
<MatchPatternSet
> mExcludeMatches
;
167 Nullable
<MatchGlobSet
> mIncludeGlobs
;
168 Nullable
<MatchGlobSet
> mExcludeGlobs
;
171 bool mCheckPermissions
;
172 Nullable
<uint64_t> mFrameID
;
173 bool mMatchAboutBlank
;
174 Nullable
<dom::Sequence
<OriginAttributesPattern
>> mOriginAttributesPatterns
;
177 class WebExtensionContentScript final
: public MozDocumentMatcher
{
179 using RunAtEnum
= dom::ContentScriptRunAt
;
181 static already_AddRefed
<WebExtensionContentScript
> Constructor(
182 dom::GlobalObject
& aGlobal
, WebExtensionPolicy
& aExtension
,
183 const ContentScriptInit
& aInit
, ErrorResult
& aRv
);
185 RunAtEnum
RunAt() const { return mRunAt
; }
187 void GetCssPaths(nsTArray
<nsString
>& aPaths
) const {
188 aPaths
.AppendElements(mCssPaths
);
190 void GetJsPaths(nsTArray
<nsString
>& aPaths
) const {
191 aPaths
.AppendElements(mJsPaths
);
194 virtual JSObject
* WrapObject(JSContext
* aCx
,
195 JS::Handle
<JSObject
*> aGivenProto
) override
;
198 friend class WebExtensionPolicy
;
200 virtual ~WebExtensionContentScript() = default;
202 WebExtensionContentScript(dom::GlobalObject
& aGlobal
,
203 WebExtensionPolicy
& aExtension
,
204 const ContentScriptInit
& aInit
, ErrorResult
& aRv
);
207 nsTArray
<nsString
> mCssPaths
;
208 nsTArray
<nsString
> mJsPaths
;
213 } // namespace extensions
214 } // namespace mozilla
216 #endif // mozilla_extensions_WebExtensionContentScript_h