already_AddRefed<MediaEncryptedEvent> MediaEncryptedEvent::Constructor(const GlobalObject& aGlobal, const nsAString& aType, const MediaKeyNeededEventInit& aEventInitDict, ErrorResult& aRv) { nsCOMPtr<EventTarget> owner = do_QueryInterface(aGlobal.GetAsSupports()); RefPtr<MediaEncryptedEvent> e = new MediaEncryptedEvent(owner); bool trusted = e->Init(owner); e->InitEvent(aType, aEventInitDict.mBubbles, aEventInitDict.mCancelable); e->mInitDataType = aEventInitDict.mInitDataType; if (!aEventInitDict.mInitData.IsNull()) { const auto& a = aEventInitDict.mInitData.Value(); a.ComputeLengthAndData(); e->mInitData = ArrayBuffer::Create(aGlobal.Context(), a.Length(), a.Data()); if (!e->mInitData) { aRv.Throw(NS_ERROR_OUT_OF_MEMORY); return nullptr; } } e->SetTrusted(trusted); return e.forget(); }
/* static */ already_AddRefed<MessageEvent> MessageEvent::Constructor(EventTarget* aEventTarget, const nsAString& aType, const MessageEventInit& aParam) { RefPtr<MessageEvent> event = new MessageEvent(aEventTarget, nullptr, nullptr); event->InitEvent(aType, aParam.mBubbles, aParam.mCancelable); bool trusted = event->Init(aEventTarget); event->SetTrusted(trusted); event->mData = aParam.mData; mozilla::HoldJSObjects(event.get()); event->mOrigin = aParam.mOrigin; event->mLastEventId = aParam.mLastEventId; if (!aParam.mSource.IsNull()) { if (aParam.mSource.Value().IsWindowProxy()) { event->mWindowSource = aParam.mSource.Value().GetAsWindowProxy(); } else if (aParam.mSource.Value().IsMessagePort()) { event->mPortSource = aParam.mSource.Value().GetAsMessagePort(); } else { event->mServiceWorkerSource = aParam.mSource.Value().GetAsServiceWorker(); } MOZ_ASSERT(event->mWindowSource || event->mPortSource || event->mServiceWorkerSource); } event->mPorts.AppendElements(aParam.mPorts); return event.forget(); }
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; } }
bool nsAutoWindowStateHelper::DispatchEventToChrome(const char* aEventName) { // XXXbz should we skip dispatching the event if the inner changed? // That is, should we store both the inner and the outer? if (!mWindow) { return true; } // The functions of nsContentUtils do not provide the required behavior, // so the following is inlined. nsIDocument* doc = mWindow->GetExtantDoc(); if (!doc) { return true; } ErrorResult rv; RefPtr<Event> event = doc->CreateEvent(NS_LITERAL_STRING("Events"), rv); if (rv.Failed()) { return false; } event->InitEvent(NS_ConvertASCIItoUTF16(aEventName), true, true); event->SetTrusted(true); event->GetInternalNSEvent()->mFlags.mOnlyChromeDispatch = true; nsCOMPtr<EventTarget> target = do_QueryInterface(mWindow); bool defaultActionEnabled; target->DispatchEvent(event, &defaultActionEnabled); return defaultActionEnabled; }
nsresult nsDOMOfflineResourceList::SendEvent(const nsAString &aEventName) { // Don't send events to closed windows if (!GetOwner()) { return NS_OK; } if (!GetOwner()->GetDocShell()) { return NS_OK; } RefPtr<Event> event = NS_NewDOMEvent(this, nullptr, nullptr); event->InitEvent(aEventName, false, true); // We assume anyone that managed to call SendEvent is trusted event->SetTrusted(true); // If the window is frozen or we're still catching up on events that were // queued while frozen, save the event for later. if (GetOwner()->IsFrozen() || mPendingEvents.Count() > 0) { mPendingEvents.AppendObject(event); return NS_OK; } bool dummy; DispatchEvent(event, &dummy); return NS_OK; }
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; } }
nsresult DOMEventTargetHelper::DispatchTrustedEvent(const nsAString& aEventName) { RefPtr<Event> event = NS_NewDOMEvent(this, nullptr, nullptr); event->InitEvent(aEventName, false, false); return DispatchTrustedEvent(event); }
void nsPerformance::DispatchBufferFullEvent() { RefPtr<Event> event = NS_NewDOMEvent(this, nullptr, nullptr); // it bubbles, and it isn't cancelable event->InitEvent(NS_LITERAL_STRING("resourcetimingbufferfull"), true, false); event->SetTrusted(true); DispatchDOMEvent(nullptr, event, nullptr, nullptr); }
// Sets up |output| iff buffers are set in event handlers. void DispatchAudioProcessEvent(ScriptProcessorNode* aNode, AudioChunk* aOutput) { AudioContext* context = aNode->Context(); if (!context) { return; } AutoJSAPI jsapi; if (NS_WARN_IF(!jsapi.Init(aNode->GetOwner()))) { return; } JSContext* cx = jsapi.cx(); uint32_t inputChannelCount = aNode->ChannelCount(); // Create the input buffer RefPtr<AudioBuffer> inputBuffer; if (mInputBuffer) { ErrorResult rv; inputBuffer = AudioBuffer::Create(context->GetOwner(), inputChannelCount, aNode->BufferSize(), context->SampleRate(), mInputBuffer.forget(), rv); if (rv.Failed()) { rv.SuppressException(); return; } } // Ask content to produce data in the output buffer // Note that we always avoid creating the output buffer here, and we try to // avoid creating the input buffer as well. The AudioProcessingEvent class // knows how to lazily create them if needed once the script tries to access // them. Otherwise, we may be able to get away without creating them! RefPtr<AudioProcessingEvent> event = new AudioProcessingEvent(aNode, nullptr, nullptr); event->InitEvent(inputBuffer, inputChannelCount, mPlaybackTime); aNode->DispatchTrustedEvent(event); // Steal the output buffers if they have been set. // Don't create a buffer if it hasn't been used to return output; // FinishProducingOutputBuffer() will optimize output = null. // GetThreadSharedChannelsForRate() may also return null after OOM. if (event->HasOutputBuffer()) { ErrorResult rv; AudioBuffer* buffer = event->GetOutputBuffer(rv); // HasOutputBuffer() returning true means that GetOutputBuffer() // will not fail. MOZ_ASSERT(!rv.Failed()); *aOutput = buffer->GetThreadSharedChannelsForRate(cx); MOZ_ASSERT(aOutput->IsNull() || aOutput->mBufferFormat == AUDIO_FORMAT_FLOAT32, "AudioBuffers initialized from JS have float data"); } }
void HTMLMarqueeElement::DispatchEventToShadowRoot( const nsAString& aEventTypeArg) { // Dispatch the event to the UA Widget Shadow Root, make it inaccessible to // document. RefPtr<nsINode> shadow = GetShadowRoot(); MOZ_ASSERT(shadow); RefPtr<Event> event = new Event(shadow, nullptr, nullptr); event->InitEvent(aEventTypeArg, false, false); event->SetTrusted(true); shadow->DispatchEvent(*event, IgnoreErrors()); }
void TextTrackList::CreateAndDispatchChangeEvent() { RefPtr<Event> event = NS_NewDOMEvent(this, nullptr, nullptr); event->InitEvent(NS_LITERAL_STRING("change"), false, false); event->SetTrusted(true); nsCOMPtr<nsIRunnable> eventRunner = new TrackEventRunner(this, event); NS_DispatchToMainThread(eventRunner); }
void FlyWebPublishedServer::FireWebsocketEvent(InternalRequest* aConnectRequest) { nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetOwner()); RefPtr<FlyWebFetchEvent> e = new FlyWebWebSocketEvent(this, new Request(global, aConnectRequest), aConnectRequest); e->Init(this); e->InitEvent(NS_LITERAL_STRING("websocket"), false, false); DispatchTrustedEvent(e); }
void DOMRequest::FireEvent(const nsAString& aType, bool aBubble, bool aCancelable) { if (NS_FAILED(CheckInnerWindowCorrectness())) { return; } RefPtr<Event> event = NS_NewDOMEvent(this, nullptr, nullptr); event->InitEvent(aType, aBubble, aCancelable); event->SetTrusted(true); bool dummy; DispatchEvent(event, &dummy); }
/* static */ already_AddRefed<MessageEvent> MessageEvent::Constructor(EventTarget* aEventTarget, const nsAString& aType, const MessageEventInit& aParam, ErrorResult& aRv) { RefPtr<MessageEvent> event = new MessageEvent(aEventTarget, nullptr, nullptr); aRv = event->InitEvent(aType, aParam.mBubbles, aParam.mCancelable); if (aRv.Failed()) { return nullptr; } bool trusted = event->Init(aEventTarget); event->SetTrusted(trusted); event->mData = aParam.mData; mozilla::HoldJSObjects(event.get()); if (aParam.mOrigin.WasPassed()) { event->mOrigin = aParam.mOrigin.Value(); } if (aParam.mLastEventId.WasPassed()) { event->mLastEventId = aParam.mLastEventId.Value(); } if (!aParam.mSource.IsNull()) { if (aParam.mSource.Value().IsWindow()) { event->mWindowSource = aParam.mSource.Value().GetAsWindow(); } else { event->mPortSource = aParam.mSource.Value().GetAsMessagePort(); } MOZ_ASSERT(event->mWindowSource || event->mPortSource); } if (aParam.mPorts.WasPassed() && !aParam.mPorts.Value().IsNull()) { nsTArray<RefPtr<MessagePort>> ports; for (uint32_t i = 0, len = aParam.mPorts.Value().Value().Length(); i < len; ++i) { ports.AppendElement(aParam.mPorts.Value().Value()[i].get()); } event->mPorts = new MessagePortList(static_cast<Event*>(event), ports); } return event.forget(); }
void BluetoothGatt::UpdateConnectionState(BluetoothConnectionState aState) { BT_LOGR("GATT connection state changes to: %d", int(aState)); mConnectionState = aState; // Dispatch connectionstatechanged event to application RefPtr<Event> event = NS_NewDOMEvent(this, nullptr, nullptr); event->InitEvent(NS_LITERAL_STRING(GATT_CONNECTION_STATE_CHANGED_ID), false, false); DispatchTrustedEvent(event); }
/* static */ already_AddRefed<SharedMapChangeEvent> SharedMapChangeEvent::Constructor(EventTarget* aEventTarget, const nsAString& aType, const MozSharedMapChangeEventInit& aInit) { RefPtr<SharedMapChangeEvent> event = new SharedMapChangeEvent(aEventTarget); bool trusted = event->Init(aEventTarget); event->InitEvent(aType, aInit.mBubbles, aInit.mCancelable); event->SetTrusted(trusted); event->SetComposed(aInit.mComposed); event->mChangedKeys = aInit.mChangedKeys; return event.forget(); }
NS_IMETHODIMP TCPServerSocket::OnStopListening(nsIServerSocket* aServer, nsresult aStatus) { if (aStatus != NS_BINDING_ABORTED) { RefPtr<Event> event = new Event(GetOwner()); event->InitEvent(NS_LITERAL_STRING("error"), false, false); event->SetTrusted(true); bool dummy; DispatchEvent(event, &dummy); NS_WARNING("Server socket was closed by unexpected reason."); return NS_ERROR_FAILURE; } mServerSocket = nullptr; return NS_OK; }
nsresult nsDOMDataChannel::OnSimpleEvent(nsISupports* aContext, const nsAString& aName) { MOZ_ASSERT(NS_IsMainThread()); nsresult rv = CheckInnerWindowCorrectness(); if (NS_FAILED(rv)) { return NS_OK; } RefPtr<Event> event = NS_NewDOMEvent(this, nullptr, nullptr); event->InitEvent(aName, false, false); event->SetTrusted(true); return DispatchDOMEvent(nullptr, event, nullptr, nullptr); }
/*static*/ already_AddRefed<FetchEvent> FetchEvent::Constructor(const GlobalObject& aGlobal, const nsAString& aType, const FetchEventInit& aOptions, ErrorResult& aRv) { RefPtr<EventTarget> owner = do_QueryObject(aGlobal.GetAsSupports()); MOZ_ASSERT(owner); RefPtr<FetchEvent> e = new FetchEvent(owner); bool trusted = e->Init(owner); e->InitEvent(aType, aOptions.mBubbles, aOptions.mCancelable); e->SetTrusted(trusted); e->mRequest = aOptions.mRequest; e->mClientId = aOptions.mClientId; e->mIsReload = aOptions.mIsReload; return e.forget(); }
NS_IMETHODIMP AsyncEventDispatcher::Run() { RefPtr<Event> event = mEvent ? mEvent->InternalDOMEvent() : nullptr; if (!event) { event = NS_NewDOMEvent(mTarget, nullptr, nullptr); event->InitEvent(mEventType, mBubbles, false); event->SetTrusted(true); } if (mOnlyChromeDispatch) { MOZ_ASSERT(event->IsTrusted()); event->WidgetEventPtr()->mFlags.mOnlyChromeDispatch = true; } bool dummy; mTarget->DispatchEvent(event, &dummy); return NS_OK; }
already_AddRefed<BluetoothLeDeviceEvent> BluetoothLeDeviceEvent::Constructor( mozilla::dom::EventTarget* aOwner, const nsAString& aType, BluetoothDevice* const aDevice, const int16_t aRssi, const nsTArray<uint8_t>& aScanRecord) { RefPtr<BluetoothLeDeviceEvent> e = new BluetoothLeDeviceEvent(aOwner); bool trusted = e->Init(aOwner); e->InitEvent(aType, false, false); e->mDevice = aDevice; e->mRssi = aRssi; e->mRawScanRecord = aScanRecord; e->SetTrusted(trusted); return e.forget(); }
void TextTrackList::CreateAndDispatchChangeEvent() { MOZ_ASSERT(NS_IsMainThread()); if (!mPendingTextTrackChange) { nsPIDOMWindowInner* win = GetOwner(); if (!win) { return; } mPendingTextTrackChange = true; RefPtr<Event> event = NS_NewDOMEvent(this, nullptr, nullptr); event->InitEvent(NS_LITERAL_STRING("change"), false, false); event->SetTrusted(true); nsCOMPtr<nsIRunnable> eventRunner = new ChangeEventRunner(this, event); nsGlobalWindow::Cast(win)->Dispatch( "TextTrackList::CreateAndDispatchChangeEvent", TaskCategory::Other, eventRunner.forget()); } }
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; } }