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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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_ChannelWrapper_h
#define mozilla_extensions_ChannelWrapper_h
#include "mozilla/dom/BindingDeclarations.h"
#include "mozilla/dom/ChannelWrapperBinding.h"
#include "mozilla/WebRequestService.h"
#include "mozilla/extensions/MatchPattern.h"
#include "mozilla/extensions/WebExtensionPolicy.h"
#include "mozilla/Attributes.h"
#include "mozilla/LinkedList.h"
#include "mozilla/Maybe.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/WeakPtr.h"
#include "mozilla/DOMEventTargetHelper.h"
#include "nsAtomHashKeys.h"
#include "nsCOMPtr.h"
#include "nsCycleCollectionParticipant.h"
#include "nsIChannel.h"
#include "nsIHttpChannel.h"
#include "nsIMultiPartChannel.h"
#include "nsIStreamListener.h"
#include "nsIRemoteTab.h"
#include "nsIThreadRetargetableStreamListener.h"
#include "nsInterfaceHashtable.h"
#include "nsIWeakReferenceUtils.h"
#include "nsWrapperCache.h"
#define NS_CHANNELWRAPPER_IID \
{ \
0xc06162d2, 0xb803, 0x43b4, { \
0xaa, 0x31, 0xcf, 0x69, 0x7f, 0x93, 0x68, 0x1c \
} \
}
class nsILoadContext;
class nsITraceableChannel;
namespace mozilla {
namespace dom {
class ContentParent;
class Element;
} // namespace dom
namespace extensions {
namespace detail {
// We need to store our wrapped channel as a weak reference, since channels
// are not cycle collected, and we're going to be hanging this wrapper
// instance off the channel in order to ensure the same channel always has
// the same wrapper.
//
// But since performance matters here, and we don't want to have to
// QueryInterface the channel every time we touch it, we store separate
// nsIChannel and nsIHttpChannel weak references, and check that the WeakPtr
// is alive before returning it.
//
// Although the class is designed for use with generic nsIChannel instances,
// the dependency on weak refs implies that we can only wrap channels that
// implement nsISupportsWeakReference. In practice, only nsHttpChannel meets
// that requirement.
//
// This holder class prevents us from accidentally touching the weak pointer
// members directly from our ChannelWrapper class.
struct ChannelHolder {
explicit ChannelHolder(nsIChannel* aChannel)
: mChannel(do_GetWeakReference(aChannel)), mWeakChannel(aChannel) {}
bool HaveChannel() const { return mChannel && mChannel->IsAlive(); }
void SetChannel(nsIChannel* aChannel) {
mChannel = do_GetWeakReference(aChannel);
mWeakChannel = aChannel;
mWeakHttpChannel.reset();
}
already_AddRefed<nsIChannel> MaybeChannel() const {
if (!HaveChannel()) {
mWeakChannel = nullptr;
}
return do_AddRef(mWeakChannel);
}
already_AddRefed<nsIHttpChannel> MaybeHttpChannel() const {
if (mWeakHttpChannel.isNothing()) {
nsCOMPtr<nsIHttpChannel> chan = QueryChannel();
mWeakHttpChannel.emplace(chan.get());
}
if (!HaveChannel()) {
mWeakHttpChannel.ref() = nullptr;
}
return do_AddRef(mWeakHttpChannel.value());
}
const nsQueryReferent QueryChannel() const {
return do_QueryReferent(mChannel);
}
private:
nsWeakPtr mChannel;
mutable nsIChannel* MOZ_NON_OWNING_REF mWeakChannel;
mutable Maybe<nsIHttpChannel*> MOZ_NON_OWNING_REF mWeakHttpChannel;
};
} // namespace detail
class WebRequestChannelEntry;
class ChannelWrapper final : public DOMEventTargetHelper,
public SupportsWeakPtr,
public LinkedListElement<ChannelWrapper>,
private detail::ChannelHolder {
public:
NS_DECL_ISUPPORTS_INHERITED
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ChannelWrapper, DOMEventTargetHelper)
NS_DECLARE_STATIC_IID_ACCESSOR(NS_CHANNELWRAPPER_IID)
void Die();
static already_AddRefed<extensions::ChannelWrapper> Get(
const dom::GlobalObject& global, nsIChannel* channel);
static already_AddRefed<extensions::ChannelWrapper> GetRegisteredChannel(
const dom::GlobalObject& global, uint64_t aChannelId,
const WebExtensionPolicy& aAddon, nsIRemoteTab* aBrowserParent);
uint64_t Id() const { return mId; }
already_AddRefed<nsIChannel> GetChannel() const { return MaybeChannel(); }
void SetChannel(nsIChannel* aChannel);
void Cancel(uint32_t result, uint32_t reason, ErrorResult& aRv);
void RedirectTo(nsIURI* uri, ErrorResult& aRv);
void UpgradeToSecure(ErrorResult& aRv);
bool Suspended() const { return mSuspended; }
void Suspend(const nsCString& aProfileMarkerText, ErrorResult& aRv);
void Resume(ErrorResult& aRv);
void GetContentType(nsCString& aContentType) const;
void SetContentType(const nsACString& aContentType);
void RegisterTraceableChannel(const WebExtensionPolicy& aAddon,
nsIRemoteTab* aBrowserParent);
already_AddRefed<nsITraceableChannel> GetTraceableChannel(
const WebExtensionPolicy& aAddon,
dom::ContentParent* aContentParent) const;
void GetMethod(nsCString& aRetVal) const;
dom::MozContentPolicyType Type() const;
uint32_t StatusCode() const;
uint64_t ResponseSize() const;
uint64_t RequestSize() const;
void GetStatusLine(nsCString& aRetVal) const;
void GetErrorString(nsString& aRetVal) const;
void ErrorCheck();
IMPL_EVENT_HANDLER(error);
IMPL_EVENT_HANDLER(start);
IMPL_EVENT_HANDLER(stop);
already_AddRefed<nsIURI> GetFinalURI() const;
void GetFinalURL(nsString& aRetVal) const;
bool Matches(const dom::MozRequestFilter& aFilter,
const WebExtensionPolicy* aExtension,
const dom::MozRequestMatchOptions& aOptions) const;
already_AddRefed<nsILoadInfo> GetLoadInfo() const {
nsCOMPtr<nsIChannel> chan = MaybeChannel();
if (chan) {
return chan->LoadInfo();
}
return nullptr;
}
int64_t FrameId() const;
int64_t ParentFrameId() const;
void GetFrameAncestors(
dom::Nullable<nsTArray<dom::MozFrameAncestorInfo>>& aFrameAncestors,
ErrorResult& aRv) const;
bool IsServiceWorkerScript() const;
static bool IsServiceWorkerScript(const nsCOMPtr<nsIChannel>& aChannel);
bool IsSystemLoad() const;
void GetOriginURL(nsCString& aRetVal) const;
void GetDocumentURL(nsCString& aRetVal) const;
already_AddRefed<nsIURI> GetOriginURI() const;
already_AddRefed<nsIURI> GetDocumentURI() const;
already_AddRefed<nsILoadContext> GetLoadContext() const;
already_AddRefed<dom::Element> GetBrowserElement() const;
bool CanModify() const;
bool GetCanModify(ErrorResult& aRv) const { return CanModify(); }
void GetProxyInfo(dom::Nullable<dom::MozProxyInfo>& aRetVal,
ErrorResult& aRv) const;
void GetRemoteAddress(nsCString& aRetVal) const;
void GetRequestHeaders(nsTArray<dom::MozHTTPHeader>& aRetVal,
ErrorResult& aRv) const;
void GetRequestHeader(const nsCString& aHeader, nsCString& aResult,
ErrorResult& aRv) const;
void GetResponseHeaders(nsTArray<dom::MozHTTPHeader>& aRetVal,
ErrorResult& aRv) const;
void SetRequestHeader(const nsCString& header, const nsCString& value,
bool merge, ErrorResult& aRv);
void SetResponseHeader(const nsCString& header, const nsCString& value,
bool merge, ErrorResult& aRv);
void GetUrlClassification(dom::Nullable<dom::MozUrlClassification>& aRetVal,
ErrorResult& aRv) const;
bool ThirdParty() const;
using EventTarget::EventListenerAdded;
using EventTarget::EventListenerRemoved;
virtual void EventListenerAdded(nsAtom* aType) override;
virtual void EventListenerRemoved(nsAtom* aType) override;
nsISupports* GetParentObject() const { return mParent; }
JSObject* WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) override;
protected:
~ChannelWrapper();
private:
ChannelWrapper(nsISupports* aParent, nsIChannel* aChannel);
void ClearCachedAttributes();
bool CheckAlive(ErrorResult& aRv) const {
if (!HaveChannel()) {
aRv.Throw(NS_ERROR_UNEXPECTED);
return false;
}
return true;
}
void FireEvent(const nsAString& aType);
const URLInfo& FinalURLInfo() const;
const URLInfo* DocumentURLInfo() const;
uint64_t BrowsingContextId(nsILoadInfo* aLoadInfo) const;
nsresult GetFrameAncestors(
nsILoadInfo* aLoadInfo,
nsTArray<dom::MozFrameAncestorInfo>& aFrameAncestors) const;
static uint64_t GetNextId() {
static uint64_t sNextId = 1;
return ++sNextId;
}
void CheckEventListeners();
class ChannelWrapperStub final : public nsISupports {
public:
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_CLASS(ChannelWrapperStub)
explicit ChannelWrapperStub(ChannelWrapper* aChannelWrapper)
: mChannelWrapper(aChannelWrapper) {}
private:
friend class ChannelWrapper;
RefPtr<ChannelWrapper> mChannelWrapper;
protected:
~ChannelWrapperStub() = default;
};
RefPtr<ChannelWrapperStub> mStub;
mutable Maybe<URLInfo> mFinalURLInfo;
mutable Maybe<URLInfo> mDocumentURLInfo;
UniquePtr<WebRequestChannelEntry> mChannelEntry;
// The overridden Content-Type header value.
nsCString mContentTypeHdr = VoidCString();
const uint64_t mId = GetNextId();
nsCOMPtr<nsISupports> mParent;
bool mAddedStreamListener = false;
bool mFiredErrorEvent = false;
bool mSuspended = false;
bool mResponseStarted = false;
nsInterfaceHashtable<nsAtomHashKey, nsIRemoteTab> mAddonEntries;
// The text for the "Extension Suspend" marker, set from the Suspend method
// when called for the first time and then cleared on the Resume method.
nsCString mSuspendedMarkerText = VoidCString();
class RequestListener final : public nsIMultiPartChannelListener,
public nsIThreadRetargetableStreamListener {
public:
NS_DECL_THREADSAFE_ISUPPORTS
NS_DECL_NSIREQUESTOBSERVER
NS_DECL_NSISTREAMLISTENER
NS_DECL_NSIMULTIPARTCHANNELLISTENER
NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
explicit RequestListener(ChannelWrapper* aWrapper)
: mChannelWrapper(aWrapper) {}
nsresult Init();
protected:
virtual ~RequestListener();
private:
RefPtr<ChannelWrapper> mChannelWrapper;
nsCOMPtr<nsIStreamListener> mOrigStreamListener;
};
};
NS_DEFINE_STATIC_IID_ACCESSOR(ChannelWrapper, NS_CHANNELWRAPPER_IID)
} // namespace extensions
} // namespace mozilla
#endif // mozilla_extensions_ChannelWrapper_h
|