Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include <algorithm> |
michael@0 | 6 | #include "TCPSocketChild.h" |
michael@0 | 7 | #include "mozilla/unused.h" |
michael@0 | 8 | #include "mozilla/net/NeckoChild.h" |
michael@0 | 9 | #include "mozilla/dom/PBrowserChild.h" |
michael@0 | 10 | #include "mozilla/dom/TabChild.h" |
michael@0 | 11 | #include "nsIDOMTCPSocket.h" |
michael@0 | 12 | #include "nsJSUtils.h" |
michael@0 | 13 | #include "nsContentUtils.h" |
michael@0 | 14 | #include "jsapi.h" |
michael@0 | 15 | #include "jsfriendapi.h" |
michael@0 | 16 | #include "jswrapper.h" |
michael@0 | 17 | |
michael@0 | 18 | using mozilla::net::gNeckoChild; |
michael@0 | 19 | |
michael@0 | 20 | namespace IPC { |
michael@0 | 21 | |
michael@0 | 22 | bool |
michael@0 | 23 | DeserializeArrayBuffer(JS::Handle<JSObject*> aObj, |
michael@0 | 24 | const InfallibleTArray<uint8_t>& aBuffer, |
michael@0 | 25 | JS::MutableHandle<JS::Value> aVal) |
michael@0 | 26 | { |
michael@0 | 27 | mozilla::AutoSafeJSContext cx; |
michael@0 | 28 | JSAutoCompartment ac(cx, aObj); |
michael@0 | 29 | |
michael@0 | 30 | JS::Rooted<JSObject*> obj(cx, JS_NewArrayBuffer(cx, aBuffer.Length())); |
michael@0 | 31 | if (!obj) |
michael@0 | 32 | return false; |
michael@0 | 33 | uint8_t* data = JS_GetArrayBufferData(obj); |
michael@0 | 34 | if (!data) |
michael@0 | 35 | return false; |
michael@0 | 36 | memcpy(data, aBuffer.Elements(), aBuffer.Length()); |
michael@0 | 37 | aVal.set(OBJECT_TO_JSVAL(obj)); |
michael@0 | 38 | return true; |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | } // namespace IPC |
michael@0 | 42 | |
michael@0 | 43 | namespace mozilla { |
michael@0 | 44 | namespace dom { |
michael@0 | 45 | |
michael@0 | 46 | NS_IMPL_CYCLE_COLLECTION(TCPSocketChildBase, mSocket) |
michael@0 | 47 | NS_IMPL_CYCLE_COLLECTING_ADDREF(TCPSocketChildBase) |
michael@0 | 48 | NS_IMPL_CYCLE_COLLECTING_RELEASE(TCPSocketChildBase) |
michael@0 | 49 | |
michael@0 | 50 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TCPSocketChildBase) |
michael@0 | 51 | NS_INTERFACE_MAP_ENTRY(nsITCPSocketChild) |
michael@0 | 52 | NS_INTERFACE_MAP_ENTRY(nsISupports) |
michael@0 | 53 | NS_INTERFACE_MAP_END |
michael@0 | 54 | |
michael@0 | 55 | TCPSocketChildBase::TCPSocketChildBase() |
michael@0 | 56 | : mIPCOpen(false) |
michael@0 | 57 | { |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | TCPSocketChildBase::~TCPSocketChildBase() |
michael@0 | 61 | { |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | NS_IMETHODIMP_(MozExternalRefCountType) TCPSocketChild::Release(void) |
michael@0 | 65 | { |
michael@0 | 66 | nsrefcnt refcnt = TCPSocketChildBase::Release(); |
michael@0 | 67 | if (refcnt == 1 && mIPCOpen) { |
michael@0 | 68 | PTCPSocketChild::SendRequestDelete(); |
michael@0 | 69 | return 1; |
michael@0 | 70 | } |
michael@0 | 71 | return refcnt; |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | TCPSocketChild::TCPSocketChild() |
michael@0 | 75 | : mWindowObj(nullptr) |
michael@0 | 76 | { |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | NS_IMETHODIMP |
michael@0 | 80 | TCPSocketChild::SendOpen(nsITCPSocketInternal* aSocket, |
michael@0 | 81 | const nsAString& aHost, uint16_t aPort, |
michael@0 | 82 | bool aUseSSL, const nsAString& aBinaryType, |
michael@0 | 83 | nsIDOMWindow* aWindow, JS::Handle<JS::Value> aWindowObj, |
michael@0 | 84 | JSContext* aCx) |
michael@0 | 85 | { |
michael@0 | 86 | mSocket = aSocket; |
michael@0 | 87 | |
michael@0 | 88 | MOZ_ASSERT(aWindowObj.isObject()); |
michael@0 | 89 | mWindowObj = js::CheckedUnwrap(&aWindowObj.toObject()); |
michael@0 | 90 | if (!mWindowObj) { |
michael@0 | 91 | return NS_ERROR_FAILURE; |
michael@0 | 92 | } |
michael@0 | 93 | AddIPDLReference(); |
michael@0 | 94 | gNeckoChild->SendPTCPSocketConstructor(this); |
michael@0 | 95 | PTCPSocketChild::SendOpen(nsString(aHost), aPort, |
michael@0 | 96 | aUseSSL, nsString(aBinaryType)); |
michael@0 | 97 | return NS_OK; |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | void |
michael@0 | 101 | TCPSocketChildBase::ReleaseIPDLReference() |
michael@0 | 102 | { |
michael@0 | 103 | MOZ_ASSERT(mIPCOpen); |
michael@0 | 104 | mIPCOpen = false; |
michael@0 | 105 | this->Release(); |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | void |
michael@0 | 109 | TCPSocketChildBase::AddIPDLReference() |
michael@0 | 110 | { |
michael@0 | 111 | MOZ_ASSERT(!mIPCOpen); |
michael@0 | 112 | mIPCOpen = true; |
michael@0 | 113 | this->AddRef(); |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | TCPSocketChild::~TCPSocketChild() |
michael@0 | 117 | { |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | bool |
michael@0 | 121 | TCPSocketChild::RecvUpdateBufferedAmount(const uint32_t& aBuffered, |
michael@0 | 122 | const uint32_t& aTrackingNumber) |
michael@0 | 123 | { |
michael@0 | 124 | if (NS_FAILED(mSocket->UpdateBufferedAmount(aBuffered, aTrackingNumber))) { |
michael@0 | 125 | NS_ERROR("Shouldn't fail!"); |
michael@0 | 126 | } |
michael@0 | 127 | return true; |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | bool |
michael@0 | 131 | TCPSocketChild::RecvCallback(const nsString& aType, |
michael@0 | 132 | const CallbackData& aData, |
michael@0 | 133 | const nsString& aReadyState) |
michael@0 | 134 | { |
michael@0 | 135 | if (NS_FAILED(mSocket->UpdateReadyState(aReadyState))) |
michael@0 | 136 | NS_ERROR("Shouldn't fail!"); |
michael@0 | 137 | |
michael@0 | 138 | nsresult rv = NS_ERROR_FAILURE; |
michael@0 | 139 | if (aData.type() == CallbackData::Tvoid_t) { |
michael@0 | 140 | rv = mSocket->CallListenerVoid(aType); |
michael@0 | 141 | |
michael@0 | 142 | } else if (aData.type() == CallbackData::TTCPError) { |
michael@0 | 143 | const TCPError& err(aData.get_TCPError()); |
michael@0 | 144 | rv = mSocket->CallListenerError(aType, err.name()); |
michael@0 | 145 | |
michael@0 | 146 | } else if (aData.type() == CallbackData::TSendableData) { |
michael@0 | 147 | const SendableData& data = aData.get_SendableData(); |
michael@0 | 148 | |
michael@0 | 149 | if (data.type() == SendableData::TArrayOfuint8_t) { |
michael@0 | 150 | JSContext* cx = nsContentUtils::GetSafeJSContext(); |
michael@0 | 151 | JSAutoRequest ar(cx); |
michael@0 | 152 | JS::Rooted<JS::Value> val(cx); |
michael@0 | 153 | JS::Rooted<JSObject*> window(cx, mWindowObj); |
michael@0 | 154 | bool ok = IPC::DeserializeArrayBuffer(window, data.get_ArrayOfuint8_t(), &val); |
michael@0 | 155 | NS_ENSURE_TRUE(ok, true); |
michael@0 | 156 | rv = mSocket->CallListenerArrayBuffer(aType, val); |
michael@0 | 157 | |
michael@0 | 158 | } else if (data.type() == SendableData::TnsString) { |
michael@0 | 159 | rv = mSocket->CallListenerData(aType, data.get_nsString()); |
michael@0 | 160 | |
michael@0 | 161 | } else { |
michael@0 | 162 | MOZ_CRASH("Invalid callback data type!"); |
michael@0 | 163 | } |
michael@0 | 164 | |
michael@0 | 165 | } else { |
michael@0 | 166 | MOZ_CRASH("Invalid callback type!"); |
michael@0 | 167 | } |
michael@0 | 168 | NS_ENSURE_SUCCESS(rv, true); |
michael@0 | 169 | return true; |
michael@0 | 170 | } |
michael@0 | 171 | |
michael@0 | 172 | NS_IMETHODIMP |
michael@0 | 173 | TCPSocketChild::SendStartTLS() |
michael@0 | 174 | { |
michael@0 | 175 | PTCPSocketChild::SendStartTLS(); |
michael@0 | 176 | return NS_OK; |
michael@0 | 177 | } |
michael@0 | 178 | |
michael@0 | 179 | NS_IMETHODIMP |
michael@0 | 180 | TCPSocketChild::SendSuspend() |
michael@0 | 181 | { |
michael@0 | 182 | PTCPSocketChild::SendSuspend(); |
michael@0 | 183 | return NS_OK; |
michael@0 | 184 | } |
michael@0 | 185 | |
michael@0 | 186 | NS_IMETHODIMP |
michael@0 | 187 | TCPSocketChild::SendResume() |
michael@0 | 188 | { |
michael@0 | 189 | PTCPSocketChild::SendResume(); |
michael@0 | 190 | return NS_OK; |
michael@0 | 191 | } |
michael@0 | 192 | |
michael@0 | 193 | NS_IMETHODIMP |
michael@0 | 194 | TCPSocketChild::SendClose() |
michael@0 | 195 | { |
michael@0 | 196 | PTCPSocketChild::SendClose(); |
michael@0 | 197 | return NS_OK; |
michael@0 | 198 | } |
michael@0 | 199 | |
michael@0 | 200 | NS_IMETHODIMP |
michael@0 | 201 | TCPSocketChild::SendSend(JS::Handle<JS::Value> aData, |
michael@0 | 202 | uint32_t aByteOffset, |
michael@0 | 203 | uint32_t aByteLength, |
michael@0 | 204 | uint32_t aTrackingNumber, |
michael@0 | 205 | JSContext* aCx) |
michael@0 | 206 | { |
michael@0 | 207 | if (aData.isString()) { |
michael@0 | 208 | JSString* jsstr = aData.toString(); |
michael@0 | 209 | nsDependentJSString str; |
michael@0 | 210 | bool ok = str.init(aCx, jsstr); |
michael@0 | 211 | NS_ENSURE_TRUE(ok, NS_ERROR_FAILURE); |
michael@0 | 212 | SendData(str, aTrackingNumber); |
michael@0 | 213 | } else { |
michael@0 | 214 | NS_ENSURE_TRUE(aData.isObject(), NS_ERROR_FAILURE); |
michael@0 | 215 | JS::Rooted<JSObject*> obj(aCx, &aData.toObject()); |
michael@0 | 216 | NS_ENSURE_TRUE(JS_IsArrayBufferObject(obj), NS_ERROR_FAILURE); |
michael@0 | 217 | uint32_t buflen = JS_GetArrayBufferByteLength(obj); |
michael@0 | 218 | aByteOffset = std::min(buflen, aByteOffset); |
michael@0 | 219 | uint32_t nbytes = std::min(buflen - aByteOffset, aByteLength); |
michael@0 | 220 | uint8_t* data = JS_GetArrayBufferData(obj); |
michael@0 | 221 | if (!data) { |
michael@0 | 222 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 223 | } |
michael@0 | 224 | FallibleTArray<uint8_t> fallibleArr; |
michael@0 | 225 | if (!fallibleArr.InsertElementsAt(0, data + aByteOffset, nbytes)) { |
michael@0 | 226 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 227 | } |
michael@0 | 228 | InfallibleTArray<uint8_t> arr; |
michael@0 | 229 | arr.SwapElements(fallibleArr); |
michael@0 | 230 | SendData(arr, aTrackingNumber); |
michael@0 | 231 | } |
michael@0 | 232 | return NS_OK; |
michael@0 | 233 | } |
michael@0 | 234 | |
michael@0 | 235 | NS_IMETHODIMP |
michael@0 | 236 | TCPSocketChild::SetSocketAndWindow(nsITCPSocketInternal *aSocket, |
michael@0 | 237 | JS::Handle<JS::Value> aWindowObj, |
michael@0 | 238 | JSContext* aCx) |
michael@0 | 239 | { |
michael@0 | 240 | mSocket = aSocket; |
michael@0 | 241 | MOZ_ASSERT(aWindowObj.isObject()); |
michael@0 | 242 | mWindowObj = js::CheckedUnwrap(&aWindowObj.toObject()); |
michael@0 | 243 | if (!mWindowObj) { |
michael@0 | 244 | return NS_ERROR_FAILURE; |
michael@0 | 245 | } |
michael@0 | 246 | return NS_OK; |
michael@0 | 247 | } |
michael@0 | 248 | |
michael@0 | 249 | bool |
michael@0 | 250 | TCPSocketChild::RecvRequestDelete() |
michael@0 | 251 | { |
michael@0 | 252 | mozilla::unused << Send__delete__(this); |
michael@0 | 253 | return true; |
michael@0 | 254 | } |
michael@0 | 255 | |
michael@0 | 256 | } // namespace dom |
michael@0 | 257 | } // namespace mozilla |