Chromium Code Reviews| Index: third_party/WebKit/Source/core/mojo/MojoWatcher.cpp |
| diff --git a/third_party/WebKit/Source/core/mojo/MojoWatcher.cpp b/third_party/WebKit/Source/core/mojo/MojoWatcher.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5c1397aa6e478f02f339219b4546b2cd91b63f39 |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/core/mojo/MojoWatcher.cpp |
| @@ -0,0 +1,85 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "core/mojo/MojoWatcher.h" |
| + |
| +#include "bindings/core/v8/MojoWatchCallback.h" |
| +#include "bindings/core/v8/ScriptState.h" |
| +#include "core/dom/ExecutionContext.h" |
| +#include "core/dom/ExecutionContextTask.h" |
| + |
| +namespace blink { |
| + |
| +MojoWatcher* MojoWatcher::create(mojo::Handle handle, |
| + MojoHandleSignals signals, |
| + ScriptState* scriptState, |
| + MojoWatchCallback* callback) { |
| + MojoWatcher* watcher = new MojoWatcher(handle, scriptState, callback); |
| + |
| + MojoResult result = |
| + MojoWatch(handle.value(), signals, &MojoWatcher::onHandleReady, |
| + reinterpret_cast<uintptr_t>(watcher)); |
| + // TODO(alokp): Consider raising an exception. |
| + // Current clients expect to recieve the initial error returned by MojoWatch |
| + // via watch callback. |
| + if (result != MOJO_RESULT_OK) { |
| + watcher->m_taskRunner->postTask( |
| + BLINK_FROM_HERE, |
| + createSameThreadTask(&MojoWatcher::runReadyCallback, |
| + wrapWeakPersistent(watcher), result)); |
| + } |
| + return watcher; |
| +} |
| + |
| +MojoWatcher::MojoWatcher(mojo::Handle handle, |
| + ScriptState* scriptState, |
| + MojoWatchCallback* callback) |
| + : m_handle(handle), |
| + m_scriptState(scriptState), |
| + m_callback(callback), |
| + m_taskRunner(scriptState->getExecutionContext()) {} |
| + |
| +MojoWatcher::~MojoWatcher() { |
| + cancel(); |
| +} |
| + |
| +MojoResult MojoWatcher::cancel() { |
| + MojoResult result = MOJO_RESULT_OK; |
| + if (m_handle.is_valid()) { |
| + result = |
| + MojoCancelWatch(m_handle.value(), reinterpret_cast<uintptr_t>(this)); |
| + m_handle = mojo::Handle(); |
| + } |
| + return result; |
| +} |
| + |
| +DEFINE_TRACE(MojoWatcher) { |
| + visitor->trace(m_callback); |
| +} |
| + |
| +void MojoWatcher::onHandleReady(uintptr_t context, |
| + MojoResult result, |
| + MojoHandleSignalsState, |
| + MojoWatchNotificationFlags) { |
| + // It is safe to assume the MojoWatcher still exists because this |
| + // callback will never be run after MojoWatcher destructor, |
| + // which cancels the watch. |
| + MojoWatcher* watcher = reinterpret_cast<MojoWatcher*>(context); |
| + watcher->m_taskRunner->postTask( |
|
jbroman
2016/12/20 23:21:57
Even though this reference to ExecutionContext is
alokp
2017/01/09 23:33:10
Hmm I see multiple other instances of cross-thread
jbroman
2017/01/10 20:46:30
I believe that is probably safe, but for subtle re
alokp
2017/01/11 21:53:01
Thanks for the code snippet. It works. Done.
|
| + BLINK_FROM_HERE, |
| + createCrossThreadTask(&MojoWatcher::runReadyCallback, |
| + wrapCrossThreadWeakPersistent(watcher), result)); |
| +} |
| + |
| +void MojoWatcher::runReadyCallback(MojoResult result) { |
| + if (!m_handle.is_valid()) |
| + return; |
| + |
| + if (result != MOJO_RESULT_OK) |
| + cancel(); |
| + |
| + m_callback->call(m_scriptState, this, result); |
|
jbroman
2016/12/20 23:21:57
I don't think m_scriptState is needed for this (an
alokp
2017/01/09 23:33:10
Done.
|
| +} |
| + |
| +} // namespace blink |