void MediaRecorder::DispatchSimpleEvent(const nsAString & aStr) { NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread"); nsresult rv = CheckInnerWindowCorrectness(); if (NS_FAILED(rv)) { return; } nsCOMPtr<nsIDOMEvent> event; rv = NS_NewDOMEvent(getter_AddRefs(event), this, nullptr, nullptr); if (NS_FAILED(rv)) { NS_WARNING("Failed to create the error event!!!"); return; } rv = event->InitEvent(aStr, false, false); if (NS_FAILED(rv)) { NS_WARNING("Failed to init the error event!!!"); return; } event->SetTrusted(true); rv = DispatchDOMEvent(nullptr, event, nullptr, nullptr); if (NS_FAILED(rv)) { NS_ERROR("Failed to dispatch the event!!!"); return; } }
void MediaRecorder::NotifyError(nsresult aRv) { NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread"); nsresult rv = CheckInnerWindowCorrectness(); if (NS_FAILED(rv)) { return; } nsString errorMsg; switch (aRv) { case NS_ERROR_DOM_SECURITY_ERR: errorMsg = NS_LITERAL_STRING("SecurityError"); break; case NS_ERROR_OUT_OF_MEMORY: errorMsg = NS_LITERAL_STRING("OutOfMemoryError"); break; default: errorMsg = NS_LITERAL_STRING("GenericError"); } nsCOMPtr<nsIDOMEvent> event; rv = NS_NewDOMRecordErrorEvent(getter_AddRefs(event), this, nullptr, nullptr); nsCOMPtr<nsIDOMRecordErrorEvent> errorEvent = do_QueryInterface(event); rv = errorEvent->InitRecordErrorEvent(NS_LITERAL_STRING("error"), false, false, errorMsg); event->SetTrusted(true); rv = DispatchDOMEvent(nullptr, event, nullptr, nullptr); if (NS_FAILED(rv)) { NS_ERROR("Failed to dispatch the error event!!!"); return; } return; }
void EventSource::FailConnection() { if (mReadyState == CLOSED) { return; } nsresult rv = ConsoleError(); if (NS_FAILED(rv)) { NS_WARNING("Failed to print to the console error"); } // When a user agent is to fail the connection, the user agent must set the // readyState attribute to CLOSED and queue a task to fire a simple event // named error at the EventSource object. Close(); // it sets mReadyState to CLOSED rv = CheckInnerWindowCorrectness(); if (NS_FAILED(rv)) { return; } RefPtr<Event> event = NS_NewDOMEvent(this, nullptr, nullptr); // it doesn't bubble, and it isn't cancelable event->InitEvent(NS_LITERAL_STRING("error"), false, false); event->SetTrusted(true); rv = DispatchDOMEvent(nullptr, event, nullptr, nullptr); if (NS_FAILED(rv)) { NS_WARNING("Failed to dispatch the error event!!!"); return; } }
nsresult MediaRecorder::CreateAndDispatchBlobEvent() { NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread"); if (!CheckPrincipal()) { // Media is not same-origin, don't allow the data out. return NS_ERROR_DOM_SECURITY_ERR; } nsCOMPtr<nsIDOMBlob> blob; blob = mEncodedBufferCache->ExtractBlob(mMimeType); // create an event that uses the MessageEvent interface, // which does not bubble, is not cancelable, and has no default action nsCOMPtr<nsIDOMEvent> event; nsresult rv = NS_NewDOMBlobEvent(getter_AddRefs(event), this, nullptr, nullptr); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIDOMBlobEvent> blobEvent = do_QueryInterface(event); rv = blobEvent->InitBlobEvent(NS_LITERAL_STRING("dataavailable"), false, false, blob); NS_ENSURE_SUCCESS(rv, rv); event->SetTrusted(true); return DispatchDOMEvent(nullptr, event, nullptr, nullptr); }
void EventSource::AnnounceConnection() { if (mReadyState == CLOSED) { return; } if (mReadyState != CONNECTING) { NS_WARNING("Unexpected mReadyState!!!"); return; } // When a user agent is to announce the connection, the user agent must set // the readyState attribute to OPEN and queue a task to fire a simple event // named open at the EventSource object. mReadyState = OPEN; nsresult rv = CheckInnerWindowCorrectness(); if (NS_FAILED(rv)) { return; } RefPtr<Event> event = NS_NewDOMEvent(this, nullptr, nullptr); // it doesn't bubble, and it isn't cancelable event->InitEvent(NS_LITERAL_STRING("open"), false, false); event->SetTrusted(true); rv = DispatchDOMEvent(nullptr, event, nullptr, nullptr); if (NS_FAILED(rv)) { NS_WARNING("Failed to dispatch the open event!!!"); return; } }
void DesktopNotification::DispatchNotificationEvent(const nsString& aName) { if (NS_FAILED(CheckInnerWindowCorrectness())) { return; } nsCOMPtr<nsIDOMEvent> event; nsresult rv = NS_NewDOMEvent(getter_AddRefs(event), this, nullptr, nullptr); if (NS_SUCCEEDED(rv)) { // it doesn't bubble, and it isn't cancelable rv = event->InitEvent(aName, false, false); if (NS_SUCCEEDED(rv)) { event->SetTrusted(true); DispatchDOMEvent(nullptr, event, nullptr, nullptr); } } }
nsresult MediaRecorder::CreateAndDispatchBlobEvent(already_AddRefed<nsIDOMBlob>&& aBlob) { NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread"); if (!CheckPrincipal()) { // Media is not same-origin, don't allow the data out. nsRefPtr<nsIDOMBlob> blob = aBlob; return NS_ERROR_DOM_SECURITY_ERR; } BlobEventInit init; init.mBubbles = false; init.mCancelable = false; init.mData = aBlob; nsRefPtr<BlobEvent> event = BlobEvent::Constructor(this, NS_LITERAL_STRING("dataavailable"), init); event->SetTrusted(true); return DispatchDOMEvent(nullptr, event, nullptr, nullptr); }
nsresult FileReader::DispatchProgressEvent(const nsAString& aType) { ProgressEventInit init; init.mBubbles = false; init.mCancelable = false; init.mLoaded = mTransferred; if (mTotal != kUnknownSize) { init.mLengthComputable = true; init.mTotal = mTotal; } else { init.mLengthComputable = false; init.mTotal = 0; } RefPtr<ProgressEvent> event = ProgressEvent::Constructor(this, aType, init); event->SetTrusted(true); return DispatchDOMEvent(nullptr, event, nullptr, nullptr); }
void EventSource::ReestablishConnection() { if (mReadyState == CLOSED) { return; } nsresult rv = ResetConnection(); if (NS_FAILED(rv)) { NS_WARNING("Failed to reset the connection!!!"); return; } rv = CheckInnerWindowCorrectness(); if (NS_FAILED(rv)) { return; } RefPtr<Event> event = NS_NewDOMEvent(this, nullptr, nullptr); // it doesn't bubble, and it isn't cancelable event->InitEvent(NS_LITERAL_STRING("error"), false, false); event->SetTrusted(true); rv = DispatchDOMEvent(nullptr, event, nullptr, nullptr); if (NS_FAILED(rv)) { NS_WARNING("Failed to dispatch the error event!!!"); return; } rv = SetReconnectionTimeout(); if (NS_FAILED(rv)) { NS_WARNING("Failed to set the timeout for reestablishing the connection!!!"); return; } }
void EventSource::DispatchAllMessageEvents() { if (mReadyState == CLOSED || mFrozen) { return; } mGoingToDispatchAllMessages = false; nsresult rv = CheckInnerWindowCorrectness(); if (NS_FAILED(rv)) { return; } AutoJSAPI jsapi; if (NS_WARN_IF(!jsapi.Init(GetOwner()))) { return; } JSContext* cx = jsapi.cx(); while (mMessagesToDispatch.GetSize() > 0) { nsAutoPtr<Message> message(static_cast<Message*>(mMessagesToDispatch.PopFront())); // Now we can turn our string into a jsval JS::Rooted<JS::Value> jsData(cx); { JSString* jsString; jsString = JS_NewUCStringCopyN(cx, message->mData.get(), message->mData.Length()); NS_ENSURE_TRUE_VOID(jsString); jsData.setString(jsString); } // create an event that uses the MessageEvent interface, // which does not bubble, is not cancelable, and has no default action RefPtr<MessageEvent> event = NS_NewDOMMessageEvent(this, nullptr, nullptr); rv = event->InitMessageEvent(message->mEventName, false, false, jsData, mOrigin, message->mLastEventID, nullptr); if (NS_FAILED(rv)) { NS_WARNING("Failed to init the message event!!!"); return; } event->SetTrusted(true); rv = DispatchDOMEvent(nullptr, static_cast<Event*>(event), nullptr, nullptr); if (NS_FAILED(rv)) { NS_WARNING("Failed to dispatch the message event!!!"); return; } mLastEventID.Assign(message->mLastEventID); } }