already_AddRefed<Promise> JSWindowActor::SendQuery( JSContext* aCx, const nsAString& aMessageName, JS::Handle<JS::Value> aObj, JS::Handle<JS::Value> aTransfers, ErrorResult& aRv) { ipc::StructuredCloneData data; if (!aObj.isUndefined() && !nsFrameMessageManager::GetParamsForMessage( aCx, aObj, aTransfers, data)) { aRv.Throw(NS_ERROR_DOM_DATA_CLONE_ERR); return nullptr; } nsIGlobalObject* global = xpc::CurrentNativeGlobal(aCx); if (NS_WARN_IF(!global)) { aRv.Throw(NS_ERROR_UNEXPECTED); return nullptr; } RefPtr<Promise> promise = Promise::Create(global, aRv); if (NS_WARN_IF(aRv.Failed())) { return nullptr; } JSWindowActorMessageMeta meta; meta.actorName() = mName; meta.messageName() = aMessageName; meta.queryId() = mNextQueryId++; meta.kind() = JSWindowActorMessageKind::Query; mPendingQueries.Put(meta.queryId(), promise); SendRawMessage(meta, std::move(data), aRv); return promise.forget(); }
// static nsresult IDBKeyRange::FromJSVal(JSContext* aCx, JS::Handle<JS::Value> aVal, IDBKeyRange** aKeyRange) { MOZ_ASSERT_IF(!aCx, aVal.isUndefined()); RefPtr<IDBKeyRange> keyRange; if (aVal.isNullOrUndefined()) { // undefined and null returns no IDBKeyRange. keyRange.forget(aKeyRange); return NS_OK; } JS::Rooted<JSObject*> obj(aCx, aVal.isObject() ? &aVal.toObject() : nullptr); bool isValidKey = aVal.isPrimitive(); if (!isValidKey) { js::ESClass cls; if (!js::GetBuiltinClass(aCx, obj, &cls)) { return NS_ERROR_UNEXPECTED; } isValidKey = cls == js::ESClass::Array || cls == js::ESClass::Date; } if (isValidKey) { // A valid key returns an 'only' IDBKeyRange. keyRange = new IDBKeyRange(nullptr, false, false, true); nsresult rv = GetKeyFromJSVal(aCx, aVal, keyRange->Lower()); if (NS_FAILED(rv)) { return rv; } } else { MOZ_ASSERT(aVal.isObject()); // An object is not permitted unless it's another IDBKeyRange. if (NS_FAILED(UNWRAP_OBJECT(IDBKeyRange, obj, keyRange))) { return NS_ERROR_DOM_INDEXEDDB_DATA_ERR; } } keyRange.forget(aKeyRange); return NS_OK; }
void JSWindowActor::SendAsyncMessage(JSContext* aCx, const nsAString& aMessageName, JS::Handle<JS::Value> aObj, JS::Handle<JS::Value> aTransfers, ErrorResult& aRv) { ipc::StructuredCloneData data; if (!aObj.isUndefined() && !nsFrameMessageManager::GetParamsForMessage( aCx, aObj, aTransfers, data)) { aRv.Throw(NS_ERROR_DOM_DATA_CLONE_ERR); return; } JSWindowActorMessageMeta meta; meta.actorName() = mName; meta.messageName() = aMessageName; meta.kind() = JSWindowActorMessageKind::Message; SendRawMessage(meta, std::move(data), aRv); }
void CompareCache::ManageValueResult(JSContext* aCx, JS::Handle<JS::Value> aValue) { AssertIsOnMainThread(); // The cache returns undefined if the object is not stored. if (aValue.isUndefined()) { mManager->CacheFinished(NS_OK, false); return; } MOZ_ASSERT(aValue.isObject()); JS::Rooted<JSObject*> obj(aCx, &aValue.toObject()); if (NS_WARN_IF(!obj)) { mManager->CacheFinished(NS_ERROR_FAILURE, false); return; } Response* response = nullptr; nsresult rv = UNWRAP_OBJECT(Response, obj, response); if (NS_WARN_IF(NS_FAILED(rv))) { mManager->CacheFinished(rv, false); return; } MOZ_ASSERT(response->Ok()); nsCOMPtr<nsIInputStream> inputStream; response->GetBody(getter_AddRefs(inputStream)); MOZ_ASSERT(inputStream); MOZ_ASSERT(!mPump); rv = NS_NewInputStreamPump(getter_AddRefs(mPump), inputStream); if (NS_WARN_IF(NS_FAILED(rv))) { mManager->CacheFinished(rv, false); return; } nsCOMPtr<nsIStreamLoader> loader; rv = NS_NewStreamLoader(getter_AddRefs(loader), this); if (NS_WARN_IF(NS_FAILED(rv))) { mManager->CacheFinished(rv, false); return; } rv = mPump->AsyncRead(loader, nullptr); if (NS_WARN_IF(NS_FAILED(rv))) { mPump = nullptr; mManager->CacheFinished(rv, false); return; } nsCOMPtr<nsIThreadRetargetableRequest> rr = do_QueryInterface(mPump); if (rr) { nsCOMPtr<nsIEventTarget> sts = do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID); rv = rr->RetargetDeliveryTo(sts); if (NS_WARN_IF(NS_FAILED(rv))) { mPump = nullptr; mManager->CacheFinished(rv, false); return; } } }
NS_IMETHODIMP TCPSocketParent::SendEvent(const nsAString& aType, JS::Handle<JS::Value> aDataVal, const nsAString& aReadyState, JSContext* aCx) { if (!mIPCOpen) { NS_WARNING("Dropping callback due to no IPC connection"); return NS_OK; } CallbackData data; if (aDataVal.isString()) { JSString* jsstr = aDataVal.toString(); nsAutoJSString str; if (!str.init(aCx, jsstr)) { FireInteralError(this, __LINE__); return NS_ERROR_OUT_OF_MEMORY; } data = SendableData(str); } else if (aDataVal.isUndefined() || aDataVal.isNull()) { data = mozilla::void_t(); } else if (aDataVal.isObject()) { JS::Rooted<JSObject *> obj(aCx, &aDataVal.toObject()); if (JS_IsArrayBufferObject(obj)) { FallibleTArray<uint8_t> fallibleArr; uint32_t errLine = 0; do { JS::AutoCheckCannotGC nogc; uint32_t nbytes = JS_GetArrayBufferByteLength(obj); uint8_t* buffer = JS_GetArrayBufferData(obj, nogc); if (!buffer) { errLine = __LINE__; break; } if (!fallibleArr.InsertElementsAt(0, buffer, nbytes, fallible)) { errLine = __LINE__; break; } } while (false); if (errLine) { FireInteralError(this, errLine); return NS_ERROR_OUT_OF_MEMORY; } InfallibleTArray<uint8_t> arr; arr.SwapElements(fallibleArr); data = SendableData(arr); } else { nsAutoJSString name; JS::Rooted<JS::Value> val(aCx); if (!JS_GetProperty(aCx, obj, "name", &val)) { NS_ERROR("No name property on supposed error object"); } else if (val.isString()) { if (!name.init(aCx, val.toString())) { NS_WARNING("couldn't initialize string"); } } data = TCPError(name); } } else { NS_ERROR("Unexpected JS value encountered"); FireInteralError(this, __LINE__); return NS_ERROR_FAILURE; } mozilla::unused << PTCPSocketParent::SendCallback(nsString(aType), data, nsString(aReadyState)); return NS_OK; }