Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/mozilla/extensions/WebExtensionContentScript.h
Line
Count
Source (jump to first uncovered line)
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
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
  bool ShouldMatchActiveTabPermission() const;
54
55
  uint64_t FrameID() const;
56
57
  nsPIDOMWindowOuter* GetWindow() const
58
0
  {
59
0
    if (mObj.is<Window>()) {
60
0
      return mObj.as<Window>();
61
0
    }
62
0
    return nullptr;
63
0
  }
64
65
  nsILoadInfo* GetLoadInfo() const
66
0
  {
67
0
    if (mObj.is<LoadInfo>()) {
68
0
      return mObj.as<LoadInfo>();
69
0
    }
70
0
    return nullptr;
71
0
  }
72
73
private:
74
  void SetURL(const URLInfo& aURL);
75
76
  const URLInfo mURL;
77
  mutable Maybe<const URLInfo> mPrincipalURL;
78
79
  mutable Maybe<bool> mIsTopLevel;
80
81
  mutable Maybe<nsCOMPtr<nsIPrincipal>> mPrincipal;
82
  mutable Maybe<uint64_t> mFrameID;
83
84
  using Window = nsPIDOMWindowOuter*;
85
  using LoadInfo = nsILoadInfo*;
86
87
  const Variant<LoadInfo, Window> mObj;
88
};
89
90
91
class MozDocumentMatcher : public nsISupports
92
                         , public nsWrapperCache
93
{
94
  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
95
  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(MozDocumentMatcher)
96
97
  using MatchGlobArray = nsTArray<RefPtr<MatchGlob>>;
98
99
  static already_AddRefed<MozDocumentMatcher>
100
  Constructor(dom::GlobalObject& aGlobal,
101
              const dom::MozDocumentMatcherInit& aInit,
102
              ErrorResult& aRv);
103
104
  bool Matches(const DocInfo& aDoc) const;
105
  bool MatchesURI(const URLInfo& aURL) const;
106
107
  bool MatchesLoadInfo(const URLInfo& aURL, nsILoadInfo* aLoadInfo) const
108
0
  {
109
0
    return Matches({aURL, aLoadInfo});
110
0
  }
111
  bool MatchesWindow(nsPIDOMWindowOuter* aWindow) const
112
0
  {
113
0
    return Matches(aWindow);
114
0
  }
115
116
117
0
  WebExtensionPolicy* GetExtension() { return mExtension; }
118
119
0
  WebExtensionPolicy* Extension() { return mExtension; }
120
0
  const WebExtensionPolicy* Extension() const { return mExtension; }
121
122
0
  bool AllFrames() const { return mAllFrames; }
123
0
  bool MatchAboutBlank() const { return mMatchAboutBlank; }
124
125
0
  MatchPatternSet* Matches() { return mMatches; }
126
0
  const MatchPatternSet* GetMatches() const { return mMatches; }
127
128
0
  MatchPatternSet* GetExcludeMatches() { return mExcludeMatches; }
129
0
  const MatchPatternSet* GetExcludeMatches() const { return mExcludeMatches; }
130
131
  void GetIncludeGlobs(Nullable<MatchGlobArray>& aGlobs)
132
0
  {
133
0
    ToNullable(mExcludeGlobs, aGlobs);
134
0
  }
135
  void GetExcludeGlobs(Nullable<MatchGlobArray>& aGlobs)
136
0
  {
137
0
    ToNullable(mExcludeGlobs, aGlobs);
138
0
  }
139
140
0
  Nullable<uint64_t> GetFrameID() const { return mFrameID; }
141
142
143
0
  WebExtensionPolicy* GetParentObject() const { return mExtension; }
144
  virtual JSObject* WrapObject(JSContext* aCx, JS::HandleObject aGivenProto) override;
145
146
protected:
147
  friend class WebExtensionPolicy;
148
149
0
  virtual ~MozDocumentMatcher() = default;
150
151
  MozDocumentMatcher(dom::GlobalObject& aGlobal,
152
                     const dom::MozDocumentMatcherInit& aInit,
153
                     bool aRestricted, ErrorResult& aRv);
154
155
  RefPtr<WebExtensionPolicy> mExtension;
156
157
  bool mHasActiveTabPermission;
158
  bool mRestricted;
159
160
  RefPtr<MatchPatternSet> mMatches;
161
  RefPtr<MatchPatternSet> mExcludeMatches;
162
163
  Nullable<MatchGlobSet> mIncludeGlobs;
164
  Nullable<MatchGlobSet> mExcludeGlobs;
165
166
167
  bool mAllFrames;
168
  Nullable<uint64_t> mFrameID;
169
  bool mMatchAboutBlank;
170
171
private:
172
  template <typename T, typename U>
173
  void
174
  ToNullable(const Nullable<T>& aInput, Nullable<U>& aOutput)
175
0
  {
176
0
    if (aInput.IsNull()) {
177
0
      aOutput.SetNull();
178
0
    } else {
179
0
      aOutput.SetValue(aInput.Value());
180
0
    }
181
0
  }
182
};
183
184
class WebExtensionContentScript final : public MozDocumentMatcher
185
{
186
public:
187
188
  using RunAtEnum = dom::ContentScriptRunAt;
189
190
  static already_AddRefed<WebExtensionContentScript>
191
  Constructor(dom::GlobalObject& aGlobal,
192
              WebExtensionPolicy& aExtension,
193
              const ContentScriptInit& aInit,
194
              ErrorResult& aRv);
195
196
0
  RunAtEnum RunAt() const { return mRunAt; }
197
198
  void GetCssPaths(nsTArray<nsString>& aPaths) const
199
0
  {
200
0
    aPaths.AppendElements(mCssPaths);
201
0
  }
202
  void GetJsPaths(nsTArray<nsString>& aPaths) const
203
0
  {
204
0
    aPaths.AppendElements(mJsPaths);
205
0
  }
206
207
  virtual JSObject* WrapObject(JSContext* aCx, JS::HandleObject aGivenProto) override;
208
209
protected:
210
  friend class WebExtensionPolicy;
211
212
0
  virtual ~WebExtensionContentScript() = default;
213
214
  WebExtensionContentScript(dom::GlobalObject& aGlobal,
215
                            WebExtensionPolicy& aExtension,
216
                            const ContentScriptInit& aInit,
217
                            ErrorResult& aRv);
218
219
private:
220
  nsTArray<nsString> mCssPaths;
221
  nsTArray<nsString> mJsPaths;
222
223
  RunAtEnum mRunAt;
224
};
225
226
} // namespace extensions
227
} // namespace mozilla
228
229
#endif // mozilla_extensions_WebExtensionContentScript_h