void BluetoothDevice::SetPropertyByValue(const BluetoothNamedValue& aValue) { const nsString& name = aValue.name(); const BluetoothValue& value = aValue.value(); if (name.EqualsLiteral("Name")) { mName = value.get_nsString(); } else if (name.EqualsLiteral("Path")) { MOZ_ASSERT(value.get_nsString().Length() > 0); mPath = value.get_nsString(); } else if (name.EqualsLiteral("Address")) { mAddress = value.get_nsString(); } else if (name.EqualsLiteral("Class")) { mClass = value.get_uint32_t(); } else if (name.EqualsLiteral("Icon")) { mIcon = value.get_nsString(); } else if (name.EqualsLiteral("Connected")) { mConnected = value.get_bool(); } else if (name.EqualsLiteral("Paired")) { mPaired = value.get_bool(); } else if (name.EqualsLiteral("UUIDs")) { mUuids = value.get_ArrayOfnsString(); nsresult rv; nsIScriptContext* sc = GetContextForEventHandlers(&rv); if (sc) { rv = nsTArrayToJSArray(sc->GetNativeContext(), mUuids, &mJsUuids); if (NS_FAILED(rv)) { NS_WARNING("Cannot set JS UUIDs object!"); return; } Root(); } else { NS_WARNING("Could not get context!"); } } else if (name.EqualsLiteral("Services")) { mServices = value.get_ArrayOfnsString(); nsresult rv; nsIScriptContext* sc = GetContextForEventHandlers(&rv); if (sc) { rv = nsTArrayToJSArray(sc->GetNativeContext(), mServices, &mJsServices); if (NS_FAILED(rv)) { NS_WARNING("Cannot set JS Services object!"); return; } Root(); } else { NS_WARNING("Could not get context!"); } #ifdef DEBUG } else { nsCString warningMsg; warningMsg.AssignLiteral("Not handling device property: "); warningMsg.Append(NS_ConvertUTF16toUTF8(name)); NS_WARNING(warningMsg.get()); #endif } }
void BluetoothDevice::SetPropertyByValue(const BluetoothNamedValue& aValue) { const nsString& name = aValue.name(); const BluetoothValue& value = aValue.value(); if (name.EqualsLiteral("Name")) { mName = value.get_nsString(); } else if (name.EqualsLiteral("Path")) { MOZ_ASSERT(value.get_nsString().Length() > 0); mPath = value.get_nsString(); } else if (name.EqualsLiteral("Address")) { mAddress = value.get_nsString(); } else if (name.EqualsLiteral("Class")) { mClass = value.get_uint32_t(); } else if (name.EqualsLiteral("Icon")) { mIcon = value.get_nsString(); } else if (name.EqualsLiteral("Connected")) { mConnected = value.get_bool(); } else if (name.EqualsLiteral("Paired")) { mPaired = value.get_bool(); } else if (name.EqualsLiteral("UUIDs")) { mUuids = value.get_ArrayOfnsString(); nsresult rv; nsIScriptContext* sc = GetContextForEventHandlers(&rv); NS_ENSURE_SUCCESS_VOID(rv); NS_ENSURE_TRUE_VOID(sc); AutoPushJSContext cx(sc->GetNativeContext()); JS::Rooted<JSObject*> uuids(cx); if (NS_FAILED(nsTArrayToJSArray(cx, mUuids, uuids.address()))) { BT_WARNING("Cannot set JS UUIDs object!"); return; } mJsUuids = uuids; Root(); } else if (name.EqualsLiteral("Services")) { mServices = value.get_ArrayOfnsString(); nsresult rv; nsIScriptContext* sc = GetContextForEventHandlers(&rv); NS_ENSURE_SUCCESS_VOID(rv); NS_ENSURE_TRUE_VOID(sc); AutoPushJSContext cx(sc->GetNativeContext()); JS::Rooted<JSObject*> services(cx); if (NS_FAILED(nsTArrayToJSArray(cx, mServices, services.address()))) { BT_WARNING("Cannot set JS Services object!"); return; } mJsServices = services; Root(); } else { nsCString warningMsg; warningMsg.AssignLiteral("Not handling device property: "); warningMsg.Append(NS_ConvertUTF16toUTF8(name)); BT_WARNING(warningMsg.get()); } }
nsresult EventSource::GetBaseURI(nsIURI **aBaseURI) { NS_ENSURE_ARG_POINTER(aBaseURI); *aBaseURI = nullptr; nsCOMPtr<nsIURI> baseURI; // first we try from document->GetBaseURI() nsresult rv; nsIScriptContext* sc = GetContextForEventHandlers(&rv); nsCOMPtr<nsIDocument> doc = nsContentUtils::GetDocumentFromScriptContext(sc); if (doc) { baseURI = doc->GetBaseURI(); } // otherwise we get from the doc's principal if (!baseURI) { rv = mPrincipal->GetURI(getter_AddRefs(baseURI)); NS_ENSURE_SUCCESS(rv, rv); } NS_ENSURE_STATE(baseURI); baseURI.forget(aBaseURI); return NS_OK; }
NS_IMETHODIMP WebSocket::GetInterface(const nsIID& aIID, void** aResult) { NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread"); if (mReadyState == WebSocket::CLOSED) return NS_ERROR_FAILURE; if (aIID.Equals(NS_GET_IID(nsIAuthPrompt)) || aIID.Equals(NS_GET_IID(nsIAuthPrompt2))) { nsresult rv; nsIScriptContext* sc = GetContextForEventHandlers(&rv); nsCOMPtr<nsIDocument> doc = nsContentUtils::GetDocumentFromScriptContext(sc); if (!doc) return NS_ERROR_NOT_AVAILABLE; nsCOMPtr<nsIPromptFactory> wwatch = do_GetService(NS_WINDOWWATCHER_CONTRACTID, &rv); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsPIDOMWindow> outerWindow = doc->GetWindow(); return wwatch->GetPrompt(outerWindow, aIID, aResult); } return QueryInterface(aIID, aResult); }
bool SmsRequest::SetSuccessInternal(nsISupports* aObject) { NS_PRECONDITION(!mDone, "mDone shouldn't have been set to true already!"); NS_PRECONDITION(!mError, "mError shouldn't have been set!"); NS_PRECONDITION(mResult == JSVAL_VOID, "mResult shouldn't have been set!"); nsresult rv; nsIScriptContext* sc = GetContextForEventHandlers(&rv); if (!sc) { SetError(nsISmsRequest::INTERNAL_ERROR); return false; } JSContext* cx = sc->GetNativeContext(); NS_ASSERTION(cx, "Failed to get a context!"); JSObject* global = sc->GetNativeGlobal(); NS_ASSERTION(global, "Failed to get global object!"); JSAutoRequest ar(cx); JSAutoCompartment ac(cx, global); RootResult(); if (NS_FAILED(nsContentUtils::WrapNative(cx, global, aObject, &mResult))) { UnrootResult(); SetError(nsISmsRequest::INTERNAL_ERROR); return false; } mDone = true; return true; }
JSContext* IDBRequest::GetJSContext() { NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); JSContext* cx; if (GetScriptOwner()) { nsIThreadJSContextStack* cxStack = nsContentUtils::ThreadJSContextStack(); NS_ASSERTION(cxStack, "Failed to get thread context stack!"); cx = cxStack->GetSafeJSContext(); NS_ENSURE_TRUE(cx, nullptr); return cx; } nsresult rv; nsIScriptContext* sc = GetContextForEventHandlers(&rv); NS_ENSURE_SUCCESS(rv, nullptr); NS_ENSURE_TRUE(sc, nullptr); cx = sc->GetNativeContext(); NS_ASSERTION(cx, "Failed to get a context!"); return cx; }
NS_IMETHODIMP nsDOMEventTargetHelper::AddEventListener(const nsAString& aType, nsIDOMEventListener *aListener, PRBool aUseCapture, PRBool aWantsUntrusted, PRUint8 aOptionalArgc) { NS_ASSERTION(!aWantsUntrusted || aOptionalArgc > 1, "Won't check if this is chrome, you want to set " "aWantsUntrusted to PR_FALSE or make the aWantsUntrusted " "explicit by making aOptionalArgc non-zero."); if (aOptionalArgc < 2) { nsresult rv; nsIScriptContext* context = GetContextForEventHandlers(&rv); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIDocument> doc = nsContentUtils::GetDocumentFromScriptContext(context); aWantsUntrusted = doc && !nsContentUtils::IsChromeDoc(doc); } nsEventListenerManager* elm = GetListenerManager(PR_TRUE); NS_ENSURE_STATE(elm); elm->AddEventListener(aType, aListener, aUseCapture, aWantsUntrusted); return NS_OK; }
NS_IMETHODIMP nsDOMEventTargetHelper::AddEventListener(const nsAString& aType, nsIDOMEventListener *aListener, PRBool aUseCapture, PRBool aWantsUntrusted, PRUint8 optional_argc) { NS_ASSERTION(!aWantsUntrusted || optional_argc > 0, "Won't check if this is chrome, you want to set " "aWantsUntrusted to PR_FALSE or make the aWantsUntrusted " "explicit by making optional_argc non-zero."); nsIEventListenerManager* elm = GetListenerManager(PR_TRUE); NS_ENSURE_STATE(elm); PRInt32 flags = aUseCapture ? NS_EVENT_FLAG_CAPTURE : NS_EVENT_FLAG_BUBBLE; if (optional_argc == 0) { nsresult rv; nsIScriptContext* context = GetContextForEventHandlers(&rv); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIDocument> doc = nsContentUtils::GetDocumentFromScriptContext(context); aWantsUntrusted = doc && !nsContentUtils::IsChromeDoc(doc); } if (aWantsUntrusted) { flags |= NS_PRIV_EVENT_UNTRUSTED_PERMITTED; } return elm->AddEventListenerByType(aListener, aType, flags, nsnull); }
net::ReferrerPolicy EventSource::GetReferrerPolicy() { nsresult rv; nsIScriptContext* sc = GetContextForEventHandlers(&rv); NS_ENSURE_SUCCESS(rv, mozilla::net::RP_Default); nsCOMPtr<nsIDocument> doc = nsContentUtils::GetDocumentFromScriptContext(sc); return doc ? doc->GetReferrerPolicy() : mozilla::net::RP_Default; }
nsresult DOMEventTargetHelper::WantsUntrusted(bool* aRetVal) { nsresult rv; nsIScriptContext* context = GetContextForEventHandlers(&rv); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIDocument> doc = nsContentUtils::GetDocumentFromScriptContext(context); // We can let listeners on workers to always handle all the events. *aRetVal = (doc && !nsContentUtils::IsChromeDoc(doc)) || !NS_IsMainThread(); return rv; }
nsresult ArchiveRequest::ReaderReady(nsTArray<nsCOMPtr<nsIDOMFile> >& aFileList, nsresult aStatus) { if (NS_FAILED(aStatus)) { FireError(aStatus); return NS_OK; } jsval result; nsresult rv; nsIScriptContext* sc = GetContextForEventHandlers(&rv); NS_ENSURE_STATE(sc); JSContext* cx = sc->GetNativeContext(); NS_ASSERTION(cx, "Failed to get a context!"); JSObject* global = sc->GetNativeGlobal(); NS_ASSERTION(global, "Failed to get global object!"); JSAutoRequest ar(cx); JSAutoEnterCompartment ac; if (ac.enter(cx, global)) { switch (mOperation) { case GetFilenames: rv = GetFilenamesResult(cx, &result, aFileList); break; case GetFile: rv = GetFileResult(cx, &result, aFileList); break; } if (NS_FAILED(rv)) { NS_WARNING("Get*Result failed!"); } } else { NS_WARNING("Failed to enter correct compartment!"); rv = NS_ERROR_FAILURE; } if (NS_SUCCEEDED(rv)) { FireSuccess(result); } else { FireError(rv); } return NS_OK; }
nsresult ArchiveRequest::ReaderReady(nsTArray<nsCOMPtr<nsIDOMFile> >& aFileList, nsresult aStatus) { if (NS_FAILED(aStatus)) { FireError(aStatus); return NS_OK; } JS::Value result; nsresult rv; nsIScriptContext* sc = GetContextForEventHandlers(&rv); NS_ENSURE_STATE(sc); AutoPushJSContext cx(sc->GetNativeContext()); NS_ASSERTION(cx, "Failed to get a context!"); JS::Rooted<JSObject*> global(cx, sc->GetNativeGlobal()); NS_ASSERTION(global, "Failed to get global object!"); JSAutoRequest ar(cx); JSAutoCompartment ac(cx, global); switch (mOperation) { case GetFilenames: rv = GetFilenamesResult(cx, &result, aFileList); break; case GetFile: rv = GetFileResult(cx, &result, aFileList); break; case GetFiles: rv = GetFilesResult(cx, &result, aFileList); break; } if (NS_FAILED(rv)) { NS_WARNING("Get*Result failed!"); } if (NS_SUCCEEDED(rv)) { FireSuccess(result); } else { FireError(rv); } return NS_OK; }
NS_IMETHODIMP SmsManager::Send(const jsval& aNumber, const nsAString& aMessage, jsval* aReturn) { nsresult rv; nsIScriptContext* sc = GetContextForEventHandlers(&rv); NS_ENSURE_STATE(sc); AutoPushJSContext cx(sc->GetNativeContext()); NS_ASSERTION(cx, "Failed to get a context!"); if (!aNumber.isString() && !(aNumber.isObject() && JS_IsArrayObject(cx, &aNumber.toObject()))) { return NS_ERROR_INVALID_ARG; } JSObject* global = sc->GetNativeGlobal(); NS_ASSERTION(global, "Failed to get global object!"); JSAutoRequest ar(cx); JSAutoCompartment ac(cx, global); if (aNumber.isString()) { return Send(cx, global, aNumber.toString(), aMessage, aReturn); } // Must be an array then. JSObject& numbers = aNumber.toObject(); uint32_t size; JS_ALWAYS_TRUE(JS_GetArrayLength(cx, &numbers, &size)); jsval* requests = new jsval[size]; for (uint32_t i=0; i<size; ++i) { jsval number; if (!JS_GetElement(cx, &numbers, i, &number)) { return NS_ERROR_INVALID_ARG; } nsresult rv = Send(cx, global, number.toString(), aMessage, &requests[i]); NS_ENSURE_SUCCESS(rv, rv); } aReturn->setObjectOrNull(JS_NewArrayObject(cx, size, requests)); NS_ENSURE_TRUE(aReturn->isObject(), NS_ERROR_FAILURE); return NS_OK; }
nsresult FileRequest::NotifyHelperCompleted(FileHelper* aFileHelper) { NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); nsresult rv = aFileHelper->mResultCode; // If the request failed then fire error event and return. if (NS_FAILED(rv)) { FireError(rv); return NS_OK; } // Otherwise we need to get the result from the helper. jsval result; nsIScriptContext* sc = GetContextForEventHandlers(&rv); NS_ENSURE_STATE(sc); JSContext* cx = sc->GetNativeContext(); NS_ASSERTION(cx, "Failed to get a context!"); JSObject* global = sc->GetNativeGlobal(); NS_ASSERTION(global, "Failed to get global object!"); JSAutoRequest ar(cx); JSAutoCompartment ac(cx, global); rv = aFileHelper->GetSuccessResult(cx, &result); if (NS_FAILED(rv)) { NS_WARNING("GetSuccessResult failed!"); } if (NS_SUCCEEDED(rv)) { FireSuccess(result); } else { FireError(rv); } return NS_OK; }
NS_IMETHODIMP SmsManager::Delete(const jsval& aParam, nsIDOMMozSmsRequest** aRequest) { if (aParam.isInt32()) { return Delete(aParam.toInt32(), aRequest); } if (!aParam.isObject()) { return NS_ERROR_INVALID_ARG; } nsresult rv; nsIScriptContext* sc = GetContextForEventHandlers(&rv); AutoPushJSContext cx(sc->GetNativeContext()); NS_ENSURE_STATE(sc); nsCOMPtr<nsIDOMMozSmsMessage> message = do_QueryInterface(nsContentUtils::XPConnect()->GetNativeOfWrapper(cx, &aParam.toObject())); NS_ENSURE_TRUE(message, NS_ERROR_INVALID_ARG); int32_t id; message->GetId(&id); return Delete(id, aRequest); }
void SmsRequest::NotifyThreadList(const InfallibleTArray<ThreadListItem>& aItems) { MOZ_ASSERT(!mParent); MOZ_ASSERT(GetOwner()); nsresult rv; nsIScriptContext* sc = GetContextForEventHandlers(&rv); NS_ENSURE_SUCCESS_VOID(rv); NS_ENSURE_TRUE_VOID(sc); JSContext* cx = sc->GetNativeContext(); MOZ_ASSERT(cx); nsCOMPtr<nsIScriptGlobalObject> sgo = do_QueryInterface(GetOwner()); JSObject* ownerObj = sgo->GetGlobalJSObject(); NS_ENSURE_TRUE_VOID(ownerObj); nsCxPusher pusher; NS_ENSURE_TRUE_VOID(pusher.Push(cx, false)); JSAutoRequest ar(cx); JSAutoCompartment ac(cx, ownerObj); JSObject* array = JS_NewArrayObject(cx, aItems.Length(), nullptr); NS_ENSURE_TRUE_VOID(array); bool ok; for (uint32_t i = 0; i < aItems.Length(); i++) { const ThreadListItem& source = aItems[i]; nsString temp = source.senderOrReceiver(); jsval senderOrReceiver; ok = xpc::StringToJsval(cx, temp, &senderOrReceiver); NS_ENSURE_TRUE_VOID(ok); JSObject* timestampObj = JS_NewDateObjectMsec(cx, source.timestamp()); NS_ENSURE_TRUE_VOID(timestampObj); jsval timestamp = OBJECT_TO_JSVAL(timestampObj); temp = source.body(); jsval body; ok = xpc::StringToJsval(cx, temp, &body); NS_ENSURE_TRUE_VOID(ok); jsval unreadCount = JS_NumberValue(double(source.unreadCount())); JSObject* elementObj = JS_NewObject(cx, nullptr, nullptr, nullptr); NS_ENSURE_TRUE_VOID(elementObj); ok = JS_SetProperty(cx, elementObj, "senderOrReceiver", &senderOrReceiver); NS_ENSURE_TRUE_VOID(ok); ok = JS_SetProperty(cx, elementObj, "timestamp", ×tamp); NS_ENSURE_TRUE_VOID(ok); ok = JS_SetProperty(cx, elementObj, "body", &body); NS_ENSURE_TRUE_VOID(ok); ok = JS_SetProperty(cx, elementObj, "unreadCount", &unreadCount); NS_ENSURE_TRUE_VOID(ok); jsval element = OBJECT_TO_JSVAL(elementObj); ok = JS_SetElement(cx, array, i, &element); NS_ENSURE_TRUE_VOID(ok); } NotifyThreadList(OBJECT_TO_JSVAL(array), cx); }
NS_IMETHODIMP SmsManager::Send(const jsval& aNumber, const nsAString& aMessage, jsval* aReturn) { nsresult rv; nsIScriptContext* sc = GetContextForEventHandlers(&rv); NS_ENSURE_STATE(sc); JSContext* cx = sc->GetNativeContext(); NS_ASSERTION(cx, "Failed to get a context!"); if (!aNumber.isString() && !(aNumber.isObject() && JS_IsArrayObject(cx, &aNumber.toObject()))) { return NS_ERROR_INVALID_ARG; } JSObject* global = sc->GetNativeGlobal(); NS_ASSERTION(global, "Failed to get global object!"); JSAutoRequest ar(cx); JSAutoCompartment ac(cx, global); if (aNumber.isString()) { return Send(cx, global, aNumber.toString(), aMessage, aReturn); } // Must be an object then. if (!aNumber.isObject()) { return NS_ERROR_FAILURE; } JSObject& numbers = aNumber.toObject(); uint32_t size; if (!JS_GetArrayLength(cx, &numbers, &size)) { return NS_ERROR_FAILURE; } JS::AutoValueVector requests(cx); if (!requests.resize(size)) { return NS_ERROR_FAILURE; } JSString *str; for (uint32_t i = 0; i < size; ++i) { jsval number; if (!JS_GetElement(cx, &numbers, i, &number)) { return NS_ERROR_INVALID_ARG; } str = JS_ValueToString(cx, number); if (!str) { return NS_ERROR_FAILURE; } nsresult rv = Send(cx, global, str, aMessage, &requests[i]); NS_ENSURE_SUCCESS(rv, rv); } JSObject* obj = JS_NewArrayObject(cx, requests.length(), requests.begin()); if (!obj) { return NS_ERROR_FAILURE; } aReturn->setObject(*obj); return NS_OK; }
nsresult EventSource::InitChannelAndRequestEventSource() { if (mReadyState == CLOSED) { return NS_ERROR_ABORT; } bool isValidScheme = (NS_SUCCEEDED(mSrc->SchemeIs("http", &isValidScheme)) && isValidScheme) || (NS_SUCCEEDED(mSrc->SchemeIs("https", &isValidScheme)) && isValidScheme); nsresult rv = CheckInnerWindowCorrectness(); if (NS_FAILED(rv) || !isValidScheme) { DispatchFailConnection(); return NS_ERROR_DOM_SECURITY_ERR; } nsLoadFlags loadFlags; loadFlags = nsIRequest::LOAD_BACKGROUND | nsIRequest::LOAD_BYPASS_CACHE; nsIScriptContext* sc = GetContextForEventHandlers(&rv); nsCOMPtr<nsIDocument> doc = nsContentUtils::GetDocumentFromScriptContext(sc); nsSecurityFlags securityFlags = nsILoadInfo::SEC_REQUIRE_CORS_DATA_INHERITS; if (mWithCredentials) { securityFlags |= nsILoadInfo::SEC_REQUIRE_CORS_WITH_CREDENTIALS; } nsCOMPtr<nsIChannel> channel; // If we have the document, use it if (doc) { rv = NS_NewChannel(getter_AddRefs(channel), mSrc, doc, securityFlags, nsIContentPolicy::TYPE_INTERNAL_EVENTSOURCE, mLoadGroup, // loadGroup nullptr, // aCallbacks loadFlags); // aLoadFlags } else { // otherwise use the principal rv = NS_NewChannel(getter_AddRefs(channel), mSrc, mPrincipal, securityFlags, nsIContentPolicy::TYPE_INTERNAL_EVENTSOURCE, mLoadGroup, // loadGroup nullptr, // aCallbacks loadFlags); // aLoadFlags } NS_ENSURE_SUCCESS(rv, rv); mHttpChannel = do_QueryInterface(channel); NS_ENSURE_TRUE(mHttpChannel, NS_ERROR_NO_INTERFACE); rv = SetupHttpChannel(); NS_ENSURE_SUCCESS(rv, rv); #ifdef DEBUG { nsCOMPtr<nsIInterfaceRequestor> notificationCallbacks; mHttpChannel->GetNotificationCallbacks(getter_AddRefs(notificationCallbacks)); MOZ_ASSERT(!notificationCallbacks); } #endif mHttpChannel->SetNotificationCallbacks(this); // Start reading from the channel rv = mHttpChannel->AsyncOpen2(this); if (NS_FAILED(rv)) { DispatchFailConnection(); return rv; } mWaitingForOnStopRequest = true; return rv; }