Thu, 15 Jan 2015 15:55:04 +0100
Back out 97036ab72558 which inappropriately compared turds to third parties.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set sw=2 ts=8 et tw=80 : */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "WebSocketLog.h" |
michael@0 | 8 | #include "base/compiler_specific.h" |
michael@0 | 9 | #include "mozilla/dom/TabChild.h" |
michael@0 | 10 | #include "mozilla/net/NeckoChild.h" |
michael@0 | 11 | #include "WebSocketChannelChild.h" |
michael@0 | 12 | #include "nsITabChild.h" |
michael@0 | 13 | #include "nsNetUtil.h" |
michael@0 | 14 | #include "mozilla/ipc/InputStreamUtils.h" |
michael@0 | 15 | #include "mozilla/ipc/URIUtils.h" |
michael@0 | 16 | #include "mozilla/net/ChannelEventQueue.h" |
michael@0 | 17 | #include "SerializedLoadContext.h" |
michael@0 | 18 | |
michael@0 | 19 | using namespace mozilla::ipc; |
michael@0 | 20 | |
michael@0 | 21 | namespace mozilla { |
michael@0 | 22 | namespace net { |
michael@0 | 23 | |
michael@0 | 24 | NS_IMPL_ADDREF(WebSocketChannelChild) |
michael@0 | 25 | |
michael@0 | 26 | NS_IMETHODIMP_(MozExternalRefCountType) WebSocketChannelChild::Release() |
michael@0 | 27 | { |
michael@0 | 28 | NS_PRECONDITION(0 != mRefCnt, "dup release"); |
michael@0 | 29 | NS_ASSERT_OWNINGTHREAD(WebSocketChannelChild); |
michael@0 | 30 | --mRefCnt; |
michael@0 | 31 | NS_LOG_RELEASE(this, mRefCnt, "WebSocketChannelChild"); |
michael@0 | 32 | |
michael@0 | 33 | if (mRefCnt == 1 && mIPCOpen) { |
michael@0 | 34 | SendDeleteSelf(); |
michael@0 | 35 | return mRefCnt; |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | if (mRefCnt == 0) { |
michael@0 | 39 | mRefCnt = 1; /* stabilize */ |
michael@0 | 40 | delete this; |
michael@0 | 41 | return 0; |
michael@0 | 42 | } |
michael@0 | 43 | return mRefCnt; |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | NS_INTERFACE_MAP_BEGIN(WebSocketChannelChild) |
michael@0 | 47 | NS_INTERFACE_MAP_ENTRY(nsIWebSocketChannel) |
michael@0 | 48 | NS_INTERFACE_MAP_ENTRY(nsIProtocolHandler) |
michael@0 | 49 | NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIWebSocketChannel) |
michael@0 | 50 | NS_INTERFACE_MAP_ENTRY(nsIThreadRetargetableRequest) |
michael@0 | 51 | NS_INTERFACE_MAP_END |
michael@0 | 52 | |
michael@0 | 53 | WebSocketChannelChild::WebSocketChannelChild(bool aSecure) |
michael@0 | 54 | : mIPCOpen(false) |
michael@0 | 55 | { |
michael@0 | 56 | NS_ABORT_IF_FALSE(NS_IsMainThread(), "not main thread"); |
michael@0 | 57 | |
michael@0 | 58 | LOG(("WebSocketChannelChild::WebSocketChannelChild() %p\n", this)); |
michael@0 | 59 | BaseWebSocketChannel::mEncrypted = aSecure; |
michael@0 | 60 | mEventQ = new ChannelEventQueue(static_cast<nsIWebSocketChannel*>(this)); |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | WebSocketChannelChild::~WebSocketChannelChild() |
michael@0 | 64 | { |
michael@0 | 65 | LOG(("WebSocketChannelChild::~WebSocketChannelChild() %p\n", this)); |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | void |
michael@0 | 69 | WebSocketChannelChild::AddIPDLReference() |
michael@0 | 70 | { |
michael@0 | 71 | NS_ABORT_IF_FALSE(!mIPCOpen, "Attempt to retain more than one IPDL reference"); |
michael@0 | 72 | mIPCOpen = true; |
michael@0 | 73 | AddRef(); |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | void |
michael@0 | 77 | WebSocketChannelChild::ReleaseIPDLReference() |
michael@0 | 78 | { |
michael@0 | 79 | NS_ABORT_IF_FALSE(mIPCOpen, "Attempt to release nonexistent IPDL reference"); |
michael@0 | 80 | mIPCOpen = false; |
michael@0 | 81 | Release(); |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | class WrappedChannelEvent : public nsRunnable |
michael@0 | 85 | { |
michael@0 | 86 | public: |
michael@0 | 87 | WrappedChannelEvent(ChannelEvent *aChannelEvent) |
michael@0 | 88 | : mChannelEvent(aChannelEvent) |
michael@0 | 89 | { |
michael@0 | 90 | MOZ_RELEASE_ASSERT(aChannelEvent); |
michael@0 | 91 | } |
michael@0 | 92 | NS_IMETHOD Run() |
michael@0 | 93 | { |
michael@0 | 94 | mChannelEvent->Run(); |
michael@0 | 95 | return NS_OK; |
michael@0 | 96 | } |
michael@0 | 97 | private: |
michael@0 | 98 | nsAutoPtr<ChannelEvent> mChannelEvent; |
michael@0 | 99 | }; |
michael@0 | 100 | |
michael@0 | 101 | void |
michael@0 | 102 | WebSocketChannelChild::DispatchToTargetThread(ChannelEvent *aChannelEvent) |
michael@0 | 103 | { |
michael@0 | 104 | MOZ_RELEASE_ASSERT(NS_IsMainThread()); |
michael@0 | 105 | MOZ_RELEASE_ASSERT(mTargetThread); |
michael@0 | 106 | MOZ_RELEASE_ASSERT(aChannelEvent); |
michael@0 | 107 | |
michael@0 | 108 | mTargetThread->Dispatch(new WrappedChannelEvent(aChannelEvent), |
michael@0 | 109 | NS_DISPATCH_NORMAL); |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | class StartEvent : public ChannelEvent |
michael@0 | 113 | { |
michael@0 | 114 | public: |
michael@0 | 115 | StartEvent(WebSocketChannelChild* aChild, |
michael@0 | 116 | const nsCString& aProtocol, |
michael@0 | 117 | const nsCString& aExtensions) |
michael@0 | 118 | : mChild(aChild) |
michael@0 | 119 | , mProtocol(aProtocol) |
michael@0 | 120 | , mExtensions(aExtensions) |
michael@0 | 121 | {} |
michael@0 | 122 | |
michael@0 | 123 | void Run() |
michael@0 | 124 | { |
michael@0 | 125 | mChild->OnStart(mProtocol, mExtensions); |
michael@0 | 126 | } |
michael@0 | 127 | private: |
michael@0 | 128 | WebSocketChannelChild* mChild; |
michael@0 | 129 | nsCString mProtocol; |
michael@0 | 130 | nsCString mExtensions; |
michael@0 | 131 | }; |
michael@0 | 132 | |
michael@0 | 133 | bool |
michael@0 | 134 | WebSocketChannelChild::RecvOnStart(const nsCString& aProtocol, |
michael@0 | 135 | const nsCString& aExtensions) |
michael@0 | 136 | { |
michael@0 | 137 | if (mEventQ->ShouldEnqueue()) { |
michael@0 | 138 | mEventQ->Enqueue(new StartEvent(this, aProtocol, aExtensions)); |
michael@0 | 139 | } else if (mTargetThread) { |
michael@0 | 140 | DispatchToTargetThread(new StartEvent(this, aProtocol, aExtensions)); |
michael@0 | 141 | } else { |
michael@0 | 142 | OnStart(aProtocol, aExtensions); |
michael@0 | 143 | } |
michael@0 | 144 | return true; |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | void |
michael@0 | 148 | WebSocketChannelChild::OnStart(const nsCString& aProtocol, |
michael@0 | 149 | const nsCString& aExtensions) |
michael@0 | 150 | { |
michael@0 | 151 | LOG(("WebSocketChannelChild::RecvOnStart() %p\n", this)); |
michael@0 | 152 | SetProtocol(aProtocol); |
michael@0 | 153 | mNegotiatedExtensions = aExtensions; |
michael@0 | 154 | |
michael@0 | 155 | if (mListener) { |
michael@0 | 156 | AutoEventEnqueuer ensureSerialDispatch(mEventQ);; |
michael@0 | 157 | mListener->OnStart(mContext); |
michael@0 | 158 | } |
michael@0 | 159 | } |
michael@0 | 160 | |
michael@0 | 161 | class StopEvent : public ChannelEvent |
michael@0 | 162 | { |
michael@0 | 163 | public: |
michael@0 | 164 | StopEvent(WebSocketChannelChild* aChild, |
michael@0 | 165 | const nsresult& aStatusCode) |
michael@0 | 166 | : mChild(aChild) |
michael@0 | 167 | , mStatusCode(aStatusCode) |
michael@0 | 168 | {} |
michael@0 | 169 | |
michael@0 | 170 | void Run() |
michael@0 | 171 | { |
michael@0 | 172 | mChild->OnStop(mStatusCode); |
michael@0 | 173 | } |
michael@0 | 174 | private: |
michael@0 | 175 | WebSocketChannelChild* mChild; |
michael@0 | 176 | nsresult mStatusCode; |
michael@0 | 177 | }; |
michael@0 | 178 | |
michael@0 | 179 | bool |
michael@0 | 180 | WebSocketChannelChild::RecvOnStop(const nsresult& aStatusCode) |
michael@0 | 181 | { |
michael@0 | 182 | if (mEventQ->ShouldEnqueue()) { |
michael@0 | 183 | mEventQ->Enqueue(new StopEvent(this, aStatusCode)); |
michael@0 | 184 | } else if (mTargetThread) { |
michael@0 | 185 | DispatchToTargetThread(new StopEvent(this, aStatusCode)); |
michael@0 | 186 | } else { |
michael@0 | 187 | OnStop(aStatusCode); |
michael@0 | 188 | } |
michael@0 | 189 | return true; |
michael@0 | 190 | } |
michael@0 | 191 | |
michael@0 | 192 | void |
michael@0 | 193 | WebSocketChannelChild::OnStop(const nsresult& aStatusCode) |
michael@0 | 194 | { |
michael@0 | 195 | LOG(("WebSocketChannelChild::RecvOnStop() %p\n", this)); |
michael@0 | 196 | if (mListener) { |
michael@0 | 197 | AutoEventEnqueuer ensureSerialDispatch(mEventQ);; |
michael@0 | 198 | mListener->OnStop(mContext, aStatusCode); |
michael@0 | 199 | } |
michael@0 | 200 | } |
michael@0 | 201 | |
michael@0 | 202 | class MessageEvent : public ChannelEvent |
michael@0 | 203 | { |
michael@0 | 204 | public: |
michael@0 | 205 | MessageEvent(WebSocketChannelChild* aChild, |
michael@0 | 206 | const nsCString& aMessage, |
michael@0 | 207 | bool aBinary) |
michael@0 | 208 | : mChild(aChild) |
michael@0 | 209 | , mMessage(aMessage) |
michael@0 | 210 | , mBinary(aBinary) |
michael@0 | 211 | {} |
michael@0 | 212 | |
michael@0 | 213 | void Run() |
michael@0 | 214 | { |
michael@0 | 215 | if (!mBinary) { |
michael@0 | 216 | mChild->OnMessageAvailable(mMessage); |
michael@0 | 217 | } else { |
michael@0 | 218 | mChild->OnBinaryMessageAvailable(mMessage); |
michael@0 | 219 | } |
michael@0 | 220 | } |
michael@0 | 221 | private: |
michael@0 | 222 | WebSocketChannelChild* mChild; |
michael@0 | 223 | nsCString mMessage; |
michael@0 | 224 | bool mBinary; |
michael@0 | 225 | }; |
michael@0 | 226 | |
michael@0 | 227 | bool |
michael@0 | 228 | WebSocketChannelChild::RecvOnMessageAvailable(const nsCString& aMsg) |
michael@0 | 229 | { |
michael@0 | 230 | if (mEventQ->ShouldEnqueue()) { |
michael@0 | 231 | mEventQ->Enqueue(new MessageEvent(this, aMsg, false)); |
michael@0 | 232 | } else if (mTargetThread) { |
michael@0 | 233 | DispatchToTargetThread(new MessageEvent(this, aMsg, false)); |
michael@0 | 234 | } else { |
michael@0 | 235 | OnMessageAvailable(aMsg); |
michael@0 | 236 | } |
michael@0 | 237 | return true; |
michael@0 | 238 | } |
michael@0 | 239 | |
michael@0 | 240 | void |
michael@0 | 241 | WebSocketChannelChild::OnMessageAvailable(const nsCString& aMsg) |
michael@0 | 242 | { |
michael@0 | 243 | LOG(("WebSocketChannelChild::RecvOnMessageAvailable() %p\n", this)); |
michael@0 | 244 | if (mListener) { |
michael@0 | 245 | AutoEventEnqueuer ensureSerialDispatch(mEventQ);; |
michael@0 | 246 | mListener->OnMessageAvailable(mContext, aMsg); |
michael@0 | 247 | } |
michael@0 | 248 | } |
michael@0 | 249 | |
michael@0 | 250 | bool |
michael@0 | 251 | WebSocketChannelChild::RecvOnBinaryMessageAvailable(const nsCString& aMsg) |
michael@0 | 252 | { |
michael@0 | 253 | if (mEventQ->ShouldEnqueue()) { |
michael@0 | 254 | mEventQ->Enqueue(new MessageEvent(this, aMsg, true)); |
michael@0 | 255 | } else if (mTargetThread) { |
michael@0 | 256 | DispatchToTargetThread(new MessageEvent(this, aMsg, true)); |
michael@0 | 257 | } else { |
michael@0 | 258 | OnBinaryMessageAvailable(aMsg); |
michael@0 | 259 | } |
michael@0 | 260 | return true; |
michael@0 | 261 | } |
michael@0 | 262 | |
michael@0 | 263 | void |
michael@0 | 264 | WebSocketChannelChild::OnBinaryMessageAvailable(const nsCString& aMsg) |
michael@0 | 265 | { |
michael@0 | 266 | LOG(("WebSocketChannelChild::RecvOnBinaryMessageAvailable() %p\n", this)); |
michael@0 | 267 | if (mListener) { |
michael@0 | 268 | AutoEventEnqueuer ensureSerialDispatch(mEventQ);; |
michael@0 | 269 | mListener->OnBinaryMessageAvailable(mContext, aMsg); |
michael@0 | 270 | } |
michael@0 | 271 | } |
michael@0 | 272 | |
michael@0 | 273 | class AcknowledgeEvent : public ChannelEvent |
michael@0 | 274 | { |
michael@0 | 275 | public: |
michael@0 | 276 | AcknowledgeEvent(WebSocketChannelChild* aChild, |
michael@0 | 277 | const uint32_t& aSize) |
michael@0 | 278 | : mChild(aChild) |
michael@0 | 279 | , mSize(aSize) |
michael@0 | 280 | {} |
michael@0 | 281 | |
michael@0 | 282 | void Run() |
michael@0 | 283 | { |
michael@0 | 284 | mChild->OnAcknowledge(mSize); |
michael@0 | 285 | } |
michael@0 | 286 | private: |
michael@0 | 287 | WebSocketChannelChild* mChild; |
michael@0 | 288 | uint32_t mSize; |
michael@0 | 289 | }; |
michael@0 | 290 | |
michael@0 | 291 | bool |
michael@0 | 292 | WebSocketChannelChild::RecvOnAcknowledge(const uint32_t& aSize) |
michael@0 | 293 | { |
michael@0 | 294 | if (mEventQ->ShouldEnqueue()) { |
michael@0 | 295 | mEventQ->Enqueue(new AcknowledgeEvent(this, aSize)); |
michael@0 | 296 | } else if (mTargetThread) { |
michael@0 | 297 | DispatchToTargetThread(new AcknowledgeEvent(this, aSize)); |
michael@0 | 298 | } else { |
michael@0 | 299 | OnAcknowledge(aSize); |
michael@0 | 300 | } |
michael@0 | 301 | return true; |
michael@0 | 302 | } |
michael@0 | 303 | |
michael@0 | 304 | void |
michael@0 | 305 | WebSocketChannelChild::OnAcknowledge(const uint32_t& aSize) |
michael@0 | 306 | { |
michael@0 | 307 | LOG(("WebSocketChannelChild::RecvOnAcknowledge() %p\n", this)); |
michael@0 | 308 | if (mListener) { |
michael@0 | 309 | AutoEventEnqueuer ensureSerialDispatch(mEventQ);; |
michael@0 | 310 | mListener->OnAcknowledge(mContext, aSize); |
michael@0 | 311 | } |
michael@0 | 312 | } |
michael@0 | 313 | |
michael@0 | 314 | class ServerCloseEvent : public ChannelEvent |
michael@0 | 315 | { |
michael@0 | 316 | public: |
michael@0 | 317 | ServerCloseEvent(WebSocketChannelChild* aChild, |
michael@0 | 318 | const uint16_t aCode, |
michael@0 | 319 | const nsCString &aReason) |
michael@0 | 320 | : mChild(aChild) |
michael@0 | 321 | , mCode(aCode) |
michael@0 | 322 | , mReason(aReason) |
michael@0 | 323 | {} |
michael@0 | 324 | |
michael@0 | 325 | void Run() |
michael@0 | 326 | { |
michael@0 | 327 | mChild->OnServerClose(mCode, mReason); |
michael@0 | 328 | } |
michael@0 | 329 | private: |
michael@0 | 330 | WebSocketChannelChild* mChild; |
michael@0 | 331 | uint16_t mCode; |
michael@0 | 332 | nsCString mReason; |
michael@0 | 333 | }; |
michael@0 | 334 | |
michael@0 | 335 | bool |
michael@0 | 336 | WebSocketChannelChild::RecvOnServerClose(const uint16_t& aCode, |
michael@0 | 337 | const nsCString& aReason) |
michael@0 | 338 | { |
michael@0 | 339 | if (mEventQ->ShouldEnqueue()) { |
michael@0 | 340 | mEventQ->Enqueue(new ServerCloseEvent(this, aCode, aReason)); |
michael@0 | 341 | } else if (mTargetThread) { |
michael@0 | 342 | DispatchToTargetThread(new ServerCloseEvent(this, aCode, aReason)); |
michael@0 | 343 | } else { |
michael@0 | 344 | OnServerClose(aCode, aReason); |
michael@0 | 345 | } |
michael@0 | 346 | return true; |
michael@0 | 347 | } |
michael@0 | 348 | |
michael@0 | 349 | void |
michael@0 | 350 | WebSocketChannelChild::OnServerClose(const uint16_t& aCode, |
michael@0 | 351 | const nsCString& aReason) |
michael@0 | 352 | { |
michael@0 | 353 | LOG(("WebSocketChannelChild::RecvOnServerClose() %p\n", this)); |
michael@0 | 354 | if (mListener) { |
michael@0 | 355 | AutoEventEnqueuer ensureSerialDispatch(mEventQ);; |
michael@0 | 356 | mListener->OnServerClose(mContext, aCode, aReason); |
michael@0 | 357 | } |
michael@0 | 358 | } |
michael@0 | 359 | |
michael@0 | 360 | NS_IMETHODIMP |
michael@0 | 361 | WebSocketChannelChild::AsyncOpen(nsIURI *aURI, |
michael@0 | 362 | const nsACString &aOrigin, |
michael@0 | 363 | nsIWebSocketListener *aListener, |
michael@0 | 364 | nsISupports *aContext) |
michael@0 | 365 | { |
michael@0 | 366 | LOG(("WebSocketChannelChild::AsyncOpen() %p\n", this)); |
michael@0 | 367 | |
michael@0 | 368 | NS_ABORT_IF_FALSE(NS_IsMainThread(), "not main thread"); |
michael@0 | 369 | NS_ABORT_IF_FALSE(aURI && aListener && !mListener, |
michael@0 | 370 | "Invalid state for WebSocketChannelChild::AsyncOpen"); |
michael@0 | 371 | |
michael@0 | 372 | mozilla::dom::TabChild* tabChild = nullptr; |
michael@0 | 373 | nsCOMPtr<nsITabChild> iTabChild; |
michael@0 | 374 | NS_QueryNotificationCallbacks(mCallbacks, mLoadGroup, |
michael@0 | 375 | NS_GET_IID(nsITabChild), |
michael@0 | 376 | getter_AddRefs(iTabChild)); |
michael@0 | 377 | if (iTabChild) { |
michael@0 | 378 | tabChild = static_cast<mozilla::dom::TabChild*>(iTabChild.get()); |
michael@0 | 379 | } |
michael@0 | 380 | if (MissingRequiredTabChild(tabChild, "websocket")) { |
michael@0 | 381 | return NS_ERROR_ILLEGAL_VALUE; |
michael@0 | 382 | } |
michael@0 | 383 | |
michael@0 | 384 | URIParams uri; |
michael@0 | 385 | SerializeURI(aURI, uri); |
michael@0 | 386 | |
michael@0 | 387 | // Corresponding release in DeallocPWebSocket |
michael@0 | 388 | AddIPDLReference(); |
michael@0 | 389 | |
michael@0 | 390 | gNeckoChild->SendPWebSocketConstructor(this, tabChild, |
michael@0 | 391 | IPC::SerializedLoadContext(this)); |
michael@0 | 392 | if (!SendAsyncOpen(uri, nsCString(aOrigin), mProtocol, mEncrypted, |
michael@0 | 393 | mPingInterval, mClientSetPingInterval, |
michael@0 | 394 | mPingResponseTimeout, mClientSetPingTimeout)) |
michael@0 | 395 | return NS_ERROR_UNEXPECTED; |
michael@0 | 396 | |
michael@0 | 397 | mOriginalURI = aURI; |
michael@0 | 398 | mURI = mOriginalURI; |
michael@0 | 399 | mListener = aListener; |
michael@0 | 400 | mContext = aContext; |
michael@0 | 401 | mOrigin = aOrigin; |
michael@0 | 402 | mWasOpened = 1; |
michael@0 | 403 | |
michael@0 | 404 | return NS_OK; |
michael@0 | 405 | } |
michael@0 | 406 | |
michael@0 | 407 | class CloseEvent : public nsRunnable |
michael@0 | 408 | { |
michael@0 | 409 | public: |
michael@0 | 410 | CloseEvent(WebSocketChannelChild *aChild, |
michael@0 | 411 | uint16_t aCode, |
michael@0 | 412 | const nsACString& aReason) |
michael@0 | 413 | : mChild(aChild) |
michael@0 | 414 | , mCode(aCode) |
michael@0 | 415 | , mReason(aReason) |
michael@0 | 416 | { |
michael@0 | 417 | MOZ_RELEASE_ASSERT(!NS_IsMainThread()); |
michael@0 | 418 | MOZ_ASSERT(aChild); |
michael@0 | 419 | } |
michael@0 | 420 | NS_IMETHOD Run() |
michael@0 | 421 | { |
michael@0 | 422 | MOZ_RELEASE_ASSERT(NS_IsMainThread()); |
michael@0 | 423 | mChild->Close(mCode, mReason); |
michael@0 | 424 | return NS_OK; |
michael@0 | 425 | } |
michael@0 | 426 | private: |
michael@0 | 427 | nsRefPtr<WebSocketChannelChild> mChild; |
michael@0 | 428 | uint16_t mCode; |
michael@0 | 429 | nsCString mReason; |
michael@0 | 430 | }; |
michael@0 | 431 | |
michael@0 | 432 | NS_IMETHODIMP |
michael@0 | 433 | WebSocketChannelChild::Close(uint16_t code, const nsACString & reason) |
michael@0 | 434 | { |
michael@0 | 435 | if (!NS_IsMainThread()) { |
michael@0 | 436 | MOZ_RELEASE_ASSERT(NS_GetCurrentThread() == mTargetThread); |
michael@0 | 437 | return NS_DispatchToMainThread(new CloseEvent(this, code, reason)); |
michael@0 | 438 | } |
michael@0 | 439 | LOG(("WebSocketChannelChild::Close() %p\n", this)); |
michael@0 | 440 | |
michael@0 | 441 | if (!mIPCOpen || !SendClose(code, nsCString(reason))) |
michael@0 | 442 | return NS_ERROR_UNEXPECTED; |
michael@0 | 443 | return NS_OK; |
michael@0 | 444 | } |
michael@0 | 445 | |
michael@0 | 446 | class MsgEvent : public nsRunnable |
michael@0 | 447 | { |
michael@0 | 448 | public: |
michael@0 | 449 | MsgEvent(WebSocketChannelChild *aChild, |
michael@0 | 450 | const nsACString &aMsg, |
michael@0 | 451 | bool aBinaryMsg) |
michael@0 | 452 | : mChild(aChild) |
michael@0 | 453 | , mMsg(aMsg) |
michael@0 | 454 | , mBinaryMsg(aBinaryMsg) |
michael@0 | 455 | { |
michael@0 | 456 | MOZ_RELEASE_ASSERT(!NS_IsMainThread()); |
michael@0 | 457 | MOZ_ASSERT(aChild); |
michael@0 | 458 | } |
michael@0 | 459 | NS_IMETHOD Run() |
michael@0 | 460 | { |
michael@0 | 461 | MOZ_RELEASE_ASSERT(NS_IsMainThread()); |
michael@0 | 462 | if (mBinaryMsg) { |
michael@0 | 463 | mChild->SendBinaryMsg(mMsg); |
michael@0 | 464 | } else { |
michael@0 | 465 | mChild->SendMsg(mMsg); |
michael@0 | 466 | } |
michael@0 | 467 | return NS_OK; |
michael@0 | 468 | } |
michael@0 | 469 | private: |
michael@0 | 470 | nsRefPtr<WebSocketChannelChild> mChild; |
michael@0 | 471 | nsCString mMsg; |
michael@0 | 472 | bool mBinaryMsg; |
michael@0 | 473 | }; |
michael@0 | 474 | |
michael@0 | 475 | NS_IMETHODIMP |
michael@0 | 476 | WebSocketChannelChild::SendMsg(const nsACString &aMsg) |
michael@0 | 477 | { |
michael@0 | 478 | if (!NS_IsMainThread()) { |
michael@0 | 479 | MOZ_RELEASE_ASSERT(NS_GetCurrentThread() == mTargetThread); |
michael@0 | 480 | return NS_DispatchToMainThread(new MsgEvent(this, aMsg, false)); |
michael@0 | 481 | } |
michael@0 | 482 | LOG(("WebSocketChannelChild::SendMsg() %p\n", this)); |
michael@0 | 483 | |
michael@0 | 484 | if (!mIPCOpen || !SendSendMsg(nsCString(aMsg))) |
michael@0 | 485 | return NS_ERROR_UNEXPECTED; |
michael@0 | 486 | return NS_OK; |
michael@0 | 487 | } |
michael@0 | 488 | |
michael@0 | 489 | NS_IMETHODIMP |
michael@0 | 490 | WebSocketChannelChild::SendBinaryMsg(const nsACString &aMsg) |
michael@0 | 491 | { |
michael@0 | 492 | if (!NS_IsMainThread()) { |
michael@0 | 493 | MOZ_RELEASE_ASSERT(NS_GetCurrentThread() == mTargetThread); |
michael@0 | 494 | return NS_DispatchToMainThread(new MsgEvent(this, aMsg, true)); |
michael@0 | 495 | } |
michael@0 | 496 | LOG(("WebSocketChannelChild::SendBinaryMsg() %p\n", this)); |
michael@0 | 497 | |
michael@0 | 498 | if (!mIPCOpen || !SendSendBinaryMsg(nsCString(aMsg))) |
michael@0 | 499 | return NS_ERROR_UNEXPECTED; |
michael@0 | 500 | return NS_OK; |
michael@0 | 501 | } |
michael@0 | 502 | |
michael@0 | 503 | class BinaryStreamEvent : public nsRunnable |
michael@0 | 504 | { |
michael@0 | 505 | public: |
michael@0 | 506 | BinaryStreamEvent(WebSocketChannelChild *aChild, |
michael@0 | 507 | OptionalInputStreamParams *aStream, |
michael@0 | 508 | uint32_t aLength) |
michael@0 | 509 | : mChild(aChild) |
michael@0 | 510 | , mStream(aStream) |
michael@0 | 511 | , mLength(aLength) |
michael@0 | 512 | { |
michael@0 | 513 | MOZ_RELEASE_ASSERT(!NS_IsMainThread()); |
michael@0 | 514 | MOZ_ASSERT(aChild); |
michael@0 | 515 | } |
michael@0 | 516 | NS_IMETHOD Run() |
michael@0 | 517 | { |
michael@0 | 518 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 519 | mChild->SendBinaryStream(mStream, mLength); |
michael@0 | 520 | return NS_OK; |
michael@0 | 521 | } |
michael@0 | 522 | private: |
michael@0 | 523 | nsRefPtr<WebSocketChannelChild> mChild; |
michael@0 | 524 | nsAutoPtr<OptionalInputStreamParams> mStream; |
michael@0 | 525 | uint32_t mLength; |
michael@0 | 526 | }; |
michael@0 | 527 | |
michael@0 | 528 | NS_IMETHODIMP |
michael@0 | 529 | WebSocketChannelChild::SendBinaryStream(nsIInputStream *aStream, |
michael@0 | 530 | uint32_t aLength) |
michael@0 | 531 | { |
michael@0 | 532 | OptionalInputStreamParams *stream = new OptionalInputStreamParams(); |
michael@0 | 533 | nsTArray<mozilla::ipc::FileDescriptor> fds; |
michael@0 | 534 | SerializeInputStream(aStream, *stream, fds); |
michael@0 | 535 | |
michael@0 | 536 | MOZ_ASSERT(fds.IsEmpty()); |
michael@0 | 537 | |
michael@0 | 538 | if (!NS_IsMainThread()) { |
michael@0 | 539 | MOZ_RELEASE_ASSERT(NS_GetCurrentThread() == mTargetThread); |
michael@0 | 540 | return NS_DispatchToMainThread(new BinaryStreamEvent(this, stream, aLength), |
michael@0 | 541 | NS_DISPATCH_NORMAL); |
michael@0 | 542 | } |
michael@0 | 543 | return SendBinaryStream(stream, aLength); |
michael@0 | 544 | } |
michael@0 | 545 | |
michael@0 | 546 | nsresult |
michael@0 | 547 | WebSocketChannelChild::SendBinaryStream(OptionalInputStreamParams *aStream, |
michael@0 | 548 | uint32_t aLength) |
michael@0 | 549 | { |
michael@0 | 550 | LOG(("WebSocketChannelChild::SendBinaryStream() %p\n", this)); |
michael@0 | 551 | |
michael@0 | 552 | nsAutoPtr<OptionalInputStreamParams> stream(aStream); |
michael@0 | 553 | |
michael@0 | 554 | if (!mIPCOpen || !SendSendBinaryStream(*stream, aLength)) |
michael@0 | 555 | return NS_ERROR_UNEXPECTED; |
michael@0 | 556 | return NS_OK; |
michael@0 | 557 | } |
michael@0 | 558 | |
michael@0 | 559 | NS_IMETHODIMP |
michael@0 | 560 | WebSocketChannelChild::GetSecurityInfo(nsISupports **aSecurityInfo) |
michael@0 | 561 | { |
michael@0 | 562 | LOG(("WebSocketChannelChild::GetSecurityInfo() %p\n", this)); |
michael@0 | 563 | return NS_ERROR_NOT_AVAILABLE; |
michael@0 | 564 | } |
michael@0 | 565 | |
michael@0 | 566 | //----------------------------------------------------------------------------- |
michael@0 | 567 | // WebSocketChannelChild::nsIThreadRetargetableRequest |
michael@0 | 568 | //----------------------------------------------------------------------------- |
michael@0 | 569 | |
michael@0 | 570 | NS_IMETHODIMP |
michael@0 | 571 | WebSocketChannelChild::RetargetDeliveryTo(nsIEventTarget* aTargetThread) |
michael@0 | 572 | { |
michael@0 | 573 | nsresult rv = BaseWebSocketChannel::RetargetDeliveryTo(aTargetThread); |
michael@0 | 574 | MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv)); |
michael@0 | 575 | |
michael@0 | 576 | return mEventQ->RetargetDeliveryTo(aTargetThread); |
michael@0 | 577 | } |
michael@0 | 578 | |
michael@0 | 579 | } // namespace net |
michael@0 | 580 | } // namespace mozilla |