summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/WebExtensionContentScript.h
blob: bf6975df088aae015039684359207ccd35d7fea8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* -*- 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_WebExtensionContentScript_h
#define mozilla_extensions_WebExtensionContentScript_h

#include "mozilla/dom/BindingDeclarations.h"
#include "mozilla/dom/WebExtensionContentScriptBinding.h"

#include "jspubtd.h"

#include "mozilla/Maybe.h"
#include "mozilla/Variant.h"
#include "mozilla/extensions/MatchGlob.h"
#include "mozilla/extensions/MatchPattern.h"
#include "nsCOMPtr.h"
#include "nsCycleCollectionParticipant.h"
#include "nsISupports.h"
#include "nsIDocShell.h"
#include "nsPIDOMWindow.h"
#include "nsWrapperCache.h"

class nsILoadInfo;
class nsPIDOMWindowOuter;

namespace mozilla {
namespace dom {
class WindowGlobalChild;
}

namespace extensions {

using dom::Nullable;
using ContentScriptInit = dom::WebExtensionContentScriptInit;

class WebExtensionPolicy;

class MOZ_STACK_CLASS DocInfo final {
 public:
  DocInfo(const URLInfo& aURL, nsILoadInfo* aLoadInfo);

  MOZ_IMPLICIT DocInfo(nsPIDOMWindowOuter* aWindow);

  const URLInfo& URL() const { return mURL; }

  // The principal of the document, or the expected principal of a request.
  // May be null for non-DOMWindow DocInfo objects unless
  // URL().InheritsPrincipal() is true.
  nsIPrincipal* Principal() const;

  // Returns the URL of the document's principal. Note that this must *only*
  // be called for content principals.
  const URLInfo& PrincipalURL() const;

  bool IsTopLevel() const;
  bool IsSameOriginWithTop() const;
  bool ShouldMatchActiveTabPermission() const;

  uint64_t FrameID() const;

  nsPIDOMWindowOuter* GetWindow() const {
    if (mObj.is<Window>()) {
      return mObj.as<Window>();
    }
    return nullptr;
  }

  nsILoadInfo* GetLoadInfo() const {
    if (mObj.is<LoadInfo>()) {
      return mObj.as<LoadInfo>();
    }
    return nullptr;
  }

  already_AddRefed<nsILoadContext> GetLoadContext() const {
    nsCOMPtr<nsILoadContext> loadContext;
    if (nsPIDOMWindowOuter* window = GetWindow()) {
      nsIDocShell* docShell = window->GetDocShell();
      loadContext = do_QueryInterface(docShell);
    } else if (nsILoadInfo* loadInfo = GetLoadInfo()) {
      nsCOMPtr<nsISupports> requestingContext = loadInfo->GetLoadingContext();
      loadContext = do_QueryInterface(requestingContext);
    }
    return loadContext.forget();
  }

 private:
  void SetURL(const URLInfo& aURL);

  const URLInfo mURL;
  mutable Maybe<const URLInfo> mPrincipalURL;

  mutable Maybe<bool> mIsTopLevel;

  mutable Maybe<nsCOMPtr<nsIPrincipal>> mPrincipal;
  mutable Maybe<uint64_t> mFrameID;

  using Window = nsPIDOMWindowOuter*;
  using LoadInfo = nsILoadInfo*;

  const Variant<LoadInfo, Window> mObj;
};

class MozDocumentMatcher : public nsISupports, public nsWrapperCache {
  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(MozDocumentMatcher)

  using MatchGlobArray = nsTArray<RefPtr<MatchGlob>>;

  static already_AddRefed<MozDocumentMatcher> Constructor(
      dom::GlobalObject& aGlobal, const dom::MozDocumentMatcherInit& aInit,
      ErrorResult& aRv);

  bool Matches(const DocInfo& aDoc, bool aIgnorePermissions) const;
  bool Matches(const DocInfo& aDoc) const { return Matches(aDoc, false); }

  bool MatchesURI(const URLInfo& aURL, bool aIgnorePermissions) const;
  bool MatchesURI(const URLInfo& aURL) const { return MatchesURI(aURL, false); }

  bool MatchesWindowGlobal(dom::WindowGlobalChild& aWindow,
                           bool aIgnorePermissions) const;

  WebExtensionPolicy* GetExtension() { return mExtension; }

  WebExtensionPolicy* Extension() { return mExtension; }
  const WebExtensionPolicy* Extension() const { return mExtension; }

  bool AllFrames() const { return mAllFrames; }
  bool CheckPermissions() const { return mCheckPermissions; }
  bool MatchAboutBlank() const { return mMatchAboutBlank; }

  MatchPatternSet* Matches() { return mMatches; }
  const MatchPatternSet* GetMatches() const { return mMatches; }

  MatchPatternSet* GetExcludeMatches() { return mExcludeMatches; }
  const MatchPatternSet* GetExcludeMatches() const { return mExcludeMatches; }

  Nullable<uint64_t> GetFrameID() const { return mFrameID; }

  void GetOriginAttributesPatterns(JSContext* aCx,
                                   JS::MutableHandle<JS::Value> aVal,
                                   ErrorResult& aError) const;

  WebExtensionPolicy* GetParentObject() const { return mExtension; }
  virtual JSObject* WrapObject(JSContext* aCx,
                               JS::Handle<JSObject*> aGivenProto) override;

 protected:
  friend class WebExtensionPolicy;

  virtual ~MozDocumentMatcher() = default;

  MozDocumentMatcher(dom::GlobalObject& aGlobal,
                     const dom::MozDocumentMatcherInit& aInit, bool aRestricted,
                     ErrorResult& aRv);

  RefPtr<WebExtensionPolicy> mExtension;

  bool mHasActiveTabPermission;
  bool mRestricted;

  RefPtr<MatchPatternSet> mMatches;
  RefPtr<MatchPatternSet> mExcludeMatches;

  Nullable<MatchGlobSet> mIncludeGlobs;
  Nullable<MatchGlobSet> mExcludeGlobs;

  bool mAllFrames;
  bool mCheckPermissions;
  Nullable<uint64_t> mFrameID;
  bool mMatchAboutBlank;
  Nullable<dom::Sequence<OriginAttributesPattern>> mOriginAttributesPatterns;
};

class WebExtensionContentScript final : public MozDocumentMatcher {
 public:
  using RunAtEnum = dom::ContentScriptRunAt;

  static already_AddRefed<WebExtensionContentScript> Constructor(
      dom::GlobalObject& aGlobal, WebExtensionPolicy& aExtension,
      const ContentScriptInit& aInit, ErrorResult& aRv);

  RunAtEnum RunAt() const { return mRunAt; }

  void GetCssPaths(nsTArray<nsString>& aPaths) const {
    aPaths.AppendElements(mCssPaths);
  }
  void GetJsPaths(nsTArray<nsString>& aPaths) const {
    aPaths.AppendElements(mJsPaths);
  }

  virtual JSObject* WrapObject(JSContext* aCx,
                               JS::Handle<JSObject*> aGivenProto) override;

 protected:
  friend class WebExtensionPolicy;

  virtual ~WebExtensionContentScript() = default;

  WebExtensionContentScript(dom::GlobalObject& aGlobal,
                            WebExtensionPolicy& aExtension,
                            const ContentScriptInit& aInit, ErrorResult& aRv);

 private:
  nsTArray<nsString> mCssPaths;
  nsTArray<nsString> mJsPaths;

  RunAtEnum mRunAt;
};

}  // namespace extensions
}  // namespace mozilla

#endif  // mozilla_extensions_WebExtensionContentScript_h