Line data Source code
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/. */
5 :
6 : #ifndef mozilla_extensions_WebExtensionContentScript_h
7 : #define mozilla_extensions_WebExtensionContentScript_h
8 :
9 : #include "mozilla/dom/BindingDeclarations.h"
10 : #include "mozilla/dom/WebExtensionContentScriptBinding.h"
11 :
12 : #include "jspubtd.h"
13 :
14 : #include "mozilla/Maybe.h"
15 : #include "mozilla/Variant.h"
16 : #include "mozilla/extensions/MatchGlob.h"
17 : #include "mozilla/extensions/MatchPattern.h"
18 : #include "nsCOMPtr.h"
19 : #include "nsCycleCollectionParticipant.h"
20 : #include "nsISupports.h"
21 : #include "nsWrapperCache.h"
22 :
23 : class nsILoadInfo;
24 : class nsPIDOMWindowOuter;
25 :
26 : namespace mozilla {
27 : namespace extensions {
28 :
29 : using dom::Nullable;
30 : using ContentScriptInit = dom::WebExtensionContentScriptInit;
31 :
32 : class WebExtensionPolicy;
33 :
34 0 : class MOZ_STACK_CLASS DocInfo final
35 : {
36 : public:
37 : DocInfo(const URLInfo& aURL, nsILoadInfo* aLoadInfo);
38 :
39 : MOZ_IMPLICIT DocInfo(nsPIDOMWindowOuter* aWindow);
40 :
41 0 : const URLInfo& URL() const { return mURL; }
42 :
43 : // The principal of the document, or the expected principal of a request.
44 : // May be null for non-DOMWindow DocInfo objects unless
45 : // URL().InheritsPrincipal() is true.
46 : nsIPrincipal* Principal() const;
47 :
48 : // Returns the URL of the document's principal. Note that this must *only*
49 : // be called for codebase principals.
50 : const URLInfo& PrincipalURL() const;
51 :
52 : bool IsTopLevel() const;
53 :
54 : uint64_t FrameID() const;
55 :
56 0 : nsPIDOMWindowOuter* GetWindow() const
57 : {
58 0 : if (mObj.is<Window>()) {
59 0 : return mObj.as<Window>();
60 : }
61 0 : return nullptr;
62 : }
63 :
64 : private:
65 : void SetURL(const URLInfo& aURL);
66 :
67 : const URLInfo mURL;
68 : mutable Maybe<const URLInfo> mPrincipalURL;
69 :
70 : mutable Maybe<bool> mIsTopLevel;
71 : mutable Maybe<nsCOMPtr<nsIPrincipal>> mPrincipal;
72 : mutable Maybe<uint64_t> mFrameID;
73 :
74 : using Window = nsPIDOMWindowOuter*;
75 : using LoadInfo = nsILoadInfo*;
76 :
77 : const Variant<LoadInfo, Window> mObj;
78 : };
79 :
80 :
81 : class WebExtensionContentScript final : public nsISupports
82 : , public nsWrapperCache
83 : {
84 : NS_DECL_CYCLE_COLLECTING_ISUPPORTS
85 0 : NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(WebExtensionContentScript)
86 :
87 :
88 : using MatchGlobArray = nsTArray<RefPtr<MatchGlob>>;
89 : using RunAtEnum = dom::ContentScriptRunAt;
90 :
91 : static already_AddRefed<WebExtensionContentScript>
92 : Constructor(dom::GlobalObject& aGlobal,
93 : WebExtensionPolicy& aExtension,
94 : const ContentScriptInit& aInit,
95 : ErrorResult& aRv);
96 :
97 :
98 : bool Matches(const DocInfo& aDoc) const;
99 : bool MatchesURI(const URLInfo& aURL) const;
100 :
101 0 : bool MatchesLoadInfo(const URLInfo& aURL, nsILoadInfo* aLoadInfo) const
102 : {
103 0 : return Matches({aURL, aLoadInfo});
104 : }
105 0 : bool MatchesWindow(nsPIDOMWindowOuter* aWindow) const
106 : {
107 0 : return Matches(aWindow);
108 : }
109 :
110 :
111 0 : WebExtensionPolicy* Extension() { return mExtension; }
112 : const WebExtensionPolicy* Extension() const { return mExtension; }
113 :
114 0 : bool AllFrames() const { return mAllFrames; }
115 0 : bool MatchAboutBlank() const { return mMatchAboutBlank; }
116 0 : RunAtEnum RunAt() const { return mRunAt; }
117 :
118 0 : Nullable<uint64_t> GetFrameID() const { return mFrameID; }
119 :
120 0 : MatchPatternSet* Matches() { return mMatches; }
121 : const MatchPatternSet* GetMatches() const { return mMatches; }
122 :
123 0 : MatchPatternSet* GetExcludeMatches() { return mExcludeMatches; }
124 : const MatchPatternSet* GetExcludeMatches() const { return mExcludeMatches; }
125 :
126 0 : void GetIncludeGlobs(Nullable<MatchGlobArray>& aGlobs)
127 : {
128 0 : ToNullable(mExcludeGlobs, aGlobs);
129 0 : }
130 0 : void GetExcludeGlobs(Nullable<MatchGlobArray>& aGlobs)
131 : {
132 0 : ToNullable(mExcludeGlobs, aGlobs);
133 0 : }
134 :
135 0 : void GetCssPaths(nsTArray<nsString>& aPaths) const
136 : {
137 0 : aPaths.AppendElements(mCssPaths);
138 0 : }
139 0 : void GetJsPaths(nsTArray<nsString>& aPaths) const
140 : {
141 0 : aPaths.AppendElements(mJsPaths);
142 0 : }
143 :
144 :
145 0 : WebExtensionPolicy* GetParentObject() const { return mExtension; }
146 :
147 : virtual JSObject* WrapObject(JSContext* aCx, JS::HandleObject aGivenProto) override;
148 :
149 : protected:
150 : friend class WebExtensionPolicy;
151 :
152 0 : virtual ~WebExtensionContentScript() = default;
153 :
154 : WebExtensionContentScript(WebExtensionPolicy& aExtension,
155 : const ContentScriptInit& aInit,
156 : ErrorResult& aRv);
157 :
158 : private:
159 : RefPtr<WebExtensionPolicy> mExtension;
160 :
161 : RefPtr<MatchPatternSet> mMatches;
162 : RefPtr<MatchPatternSet> mExcludeMatches;
163 :
164 : Nullable<MatchGlobSet> mIncludeGlobs;
165 : Nullable<MatchGlobSet> mExcludeGlobs;
166 :
167 : nsTArray<nsString> mCssPaths;
168 : nsTArray<nsString> mJsPaths;
169 :
170 : RunAtEnum mRunAt;
171 :
172 : bool mAllFrames;
173 : Nullable<uint64_t> mFrameID;
174 : bool mMatchAboutBlank;
175 :
176 : template <typename T, typename U>
177 : void
178 0 : ToNullable(const Nullable<T>& aInput, Nullable<U>& aOutput)
179 : {
180 0 : if (aInput.IsNull()) {
181 0 : aOutput.SetNull();
182 : } else {
183 0 : aOutput.SetValue(aInput.Value());
184 : }
185 0 : }
186 : };
187 :
188 : } // namespace extensions
189 : } // namespace mozilla
190 :
191 : #endif // mozilla_extensions_WebExtensionContentScript_h
|