Commit 4e188d7c authored by zhaobin's avatar zhaobin Committed by Commit bot

[Presentation API] fire onconnectionavailable and onconnect event asynchronously

We would like to ensure following execution order when calling PresentationRequest.start():

1. resolve start() promise
2. fire PresentationRequest onconnectionavailable event
3. fire PresentationConnection onconnect event

To fire event after resolving promise, we need to dispatch events asynchronously

BUG=669213

Review-Url: https://codereview.chromium.org/2547143002
Cr-Commit-Position: refs/heads/master@{#437598}
parent d93ce395
......@@ -9,6 +9,7 @@
#include "core/dom/DOMArrayBufferView.h"
#include "core/dom/Document.h"
#include "core/dom/ExceptionCode.h"
#include "core/dom/ExecutionContextTask.h"
#include "core/events/Event.h"
#include "core/events/MessageEvent.h"
#include "core/fileapi/FileReaderLoader.h"
......@@ -191,8 +192,14 @@ PresentationConnection* PresentationConnection::take(
PresentationConnection* connection = new PresentationConnection(
controller->frame(), client->getId(), client->getUrl());
controller->registerConnection(connection);
request->dispatchEvent(PresentationConnectionAvailableEvent::create(
EventTypeNames::connectionavailable, connection));
// Fire onconnectionavailable event asynchronously.
auto* event = PresentationConnectionAvailableEvent::create(
EventTypeNames::connectionavailable, connection);
request->getExecutionContext()->postTask(
BLINK_FROM_HERE,
createSameThreadTask(&PresentationConnection::dispatchEventAsync,
wrapPersistent(request), wrapPersistent(event)));
return connection;
}
......@@ -415,10 +422,10 @@ void PresentationConnection::didChangeState(
NOTREACHED();
return;
case WebPresentationConnectionState::Connected:
dispatchEvent(Event::create(EventTypeNames::connect));
dispatchStateChangeEvent(Event::create(EventTypeNames::connect));
return;
case WebPresentationConnectionState::Terminated:
dispatchEvent(Event::create(EventTypeNames::terminate));
dispatchStateChangeEvent(Event::create(EventTypeNames::terminate));
return;
// Closed state is handled in |didClose()|.
case WebPresentationConnectionState::Closed:
......@@ -435,7 +442,7 @@ void PresentationConnection::didClose(
return;
m_state = WebPresentationConnectionState::Closed;
dispatchEvent(PresentationConnectionCloseEvent::create(
dispatchStateChangeEvent(PresentationConnectionCloseEvent::create(
EventTypeNames::close, connectionCloseReasonToString(reason), message));
}
......@@ -464,6 +471,21 @@ void PresentationConnection::didFailLoadingBlob(
handleMessageQueue();
}
void PresentationConnection::dispatchStateChangeEvent(Event* event) {
getExecutionContext()->postTask(
BLINK_FROM_HERE,
createSameThreadTask(&PresentationConnection::dispatchEventAsync,
wrapPersistent(this), wrapPersistent(event)));
}
// static
void PresentationConnection::dispatchEventAsync(EventTarget* target,
Event* event) {
DCHECK(target);
DCHECK(event);
target->dispatchEvent(event);
}
void PresentationConnection::tearDown() {
// Cancel current Blob loading if any.
if (m_blobLoader) {
......
......@@ -114,6 +114,10 @@ class PresentationConnection final : public EventTargetWithInlineData,
void didFinishLoadingBlob(DOMArrayBuffer*);
void didFailLoadingBlob(FileError::ErrorCode);
// Internal helper function to dispatch state change events asynchronously.
void dispatchStateChangeEvent(Event*);
static void dispatchEventAsync(EventTarget*, Event*);
// Cancel loads and pending messages when the connection is closed.
void tearDown();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment