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
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* 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_StreamFilterParent_h
#define mozilla_extensions_StreamFilterParent_h
#include "StreamFilterBase.h"
#include "mozilla/extensions/PStreamFilterParent.h"
#include "mozilla/LinkedList.h"
#include "mozilla/Mutex.h"
#include "mozilla/SystemGroup.h"
#include "mozilla/WebRequestService.h"
#include "nsIStreamListener.h"
#include "nsIThread.h"
#include "nsIThreadRetargetableStreamListener.h"
#include "nsThreadUtils.h"
#if defined(_MSC_VER)
#define FUNC __FUNCSIG__
#else
#define FUNC __PRETTY_FUNCTION__
#endif
namespace mozilla {
namespace dom {
class ContentParent;
}
namespace net {
class ChannelEventQueue;
class nsHttpChannel;
} // namespace net
namespace extensions {
using namespace mozilla::dom;
using mozilla::ipc::IPCResult;
class StreamFilterParent final : public PStreamFilterParent,
public nsIStreamListener,
public nsIThreadRetargetableStreamListener,
public StreamFilterBase {
public:
NS_DECL_THREADSAFE_ISUPPORTS
NS_DECL_NSISTREAMLISTENER
NS_DECL_NSIREQUESTOBSERVER
NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
StreamFilterParent();
using ParentEndpoint = mozilla::ipc::Endpoint<PStreamFilterParent>;
static bool Create(ContentParent* aContentParent, uint64_t aChannelId,
const nsAString& aAddonId,
mozilla::ipc::Endpoint<PStreamFilterChild>* aEndpoint);
static void Attach(nsIChannel* aChannel, ParentEndpoint&& aEndpoint);
enum class State {
// The parent has been created, but not yet constructed by the child.
Uninitialized,
// The parent has been successfully constructed.
Initialized,
// The OnRequestStarted event has been received, and data is being
// transferred to the child.
TransferringData,
// The channel is suspended.
Suspended,
// The channel has been closed by the child, and will send or receive data.
Closed,
// The channel is being disconnected from the child, so that all further
// data and events pass unfiltered to the output listener. Any data
// currnetly in transit to, or buffered by, the child will be written to the
// output listener before we enter the Disconnected atate.
Disconnecting,
// The channel has been disconnected from the child, and all further data
// and events will be passed directly to the output listener.
Disconnected,
};
protected:
virtual ~StreamFilterParent();
virtual IPCResult RecvWrite(Data&& aData) override;
virtual IPCResult RecvFlushedData() override;
virtual IPCResult RecvSuspend() override;
virtual IPCResult RecvResume() override;
virtual IPCResult RecvClose() override;
virtual IPCResult RecvDisconnect() override;
virtual IPCResult RecvDestroy() override;
virtual void DeallocPStreamFilterParent() override;
private:
bool IPCActive() {
return (mState != State::Closed && mState != State::Disconnecting &&
mState != State::Disconnected);
}
void Init(nsIChannel* aChannel);
void Bind(ParentEndpoint&& aEndpoint);
void Destroy();
nsresult FlushBufferedData();
nsresult Write(Data& aData);
void WriteMove(Data&& aData);
void DoSendData(Data&& aData);
nsresult EmitStopRequest(nsresult aStatusCode);
virtual void ActorDestroy(ActorDestroyReason aWhy) override;
void Broken();
void CheckResult(bool aResult) {
if (NS_WARN_IF(!aResult)) {
Broken();
}
}
inline nsIEventTarget* ActorThread();
inline nsIEventTarget* IOThread();
inline bool IsIOThread();
inline bool IsActorThread();
inline void AssertIsActorThread();
inline void AssertIsIOThread();
static void AssertIsMainThread() { MOZ_ASSERT(NS_IsMainThread()); }
template <typename Function>
void RunOnMainThread(const char* aName, Function&& aFunc);
void RunOnMainThread(already_AddRefed<Runnable> aRunnable);
template <typename Function>
void RunOnActorThread(const char* aName, Function&& aFunc);
template <typename Function>
void RunOnIOThread(const char* aName, Function&& aFunc);
void RunOnIOThread(already_AddRefed<Runnable>);
nsCOMPtr<nsIChannel> mChannel;
nsCOMPtr<nsIStreamListener> mOrigListener;
nsCOMPtr<nsIEventTarget> mMainThread;
nsCOMPtr<nsIEventTarget> mIOThread;
RefPtr<net::ChannelEventQueue> mQueue;
Mutex mBufferMutex;
bool mReceivedStop;
bool mSentStop;
bool mDisconnected = false;
nsCOMPtr<nsISupports> mContext;
uint64_t mOffset;
volatile State mState;
};
} // namespace extensions
} // namespace mozilla
#endif // mozilla_extensions_StreamFilterParent_h
|