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..dc1a97d5f8bb9b04a26d450d45a529db0de5cca3 |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/core/mojo/MojoWatcher.cpp |
| @@ -0,0 +1,131 @@ |
| +// Copyright 2017 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" |
| +#include "core/dom/TaskRunnerHelper.h" |
| +#include "core/mojo/MojoHandleSignals.h" |
| +#include "platform/WebTaskRunner.h" |
| + |
| +namespace blink { |
| + |
| +static const TaskType kMojoTaskType = TaskType::MiscPlatformAPI; |
|
haraken
2017/02/08 06:32:10
This is not a speced task, right? Then we should u
alokp
2017/02/08 17:34:41
Done.
|
| + |
| +static void runWatchCallback(MojoWatchCallback* callback, |
| + ScriptWrappable* wrappable, |
| + MojoResult result) { |
| + callback->call(wrappable, result); |
| +} |
| + |
| +MojoWatcher* MojoWatcher::create(mojo::Handle handle, |
| + const MojoHandleSignals& signalsDict, |
| + MojoWatchCallback* callback, |
| + ExecutionContext* context) { |
| + MojoWatcher* watcher = new MojoWatcher(context, callback); |
| + MojoResult result = watcher->watch(handle, signalsDict); |
| + // TODO(alokp): Consider raising an exception. |
| + // Current clients expect to recieve the initial error returned by MojoWatch |
| + // via watch callback. |
| + // |
| + // Note that the usage of wrapPersistent is intentional so that the intial |
| + // error is guaranteed to be reported to the client in case where the given |
| + // handle is invalid and garbage collection happens before the callback |
| + // is scheduled. |
| + if (result != MOJO_RESULT_OK) { |
| + context->postTask( |
|
haraken
2017/02/08 06:32:10
We're deprecating ExecutionContextTask. Can we use
jbroman
2017/02/08 13:20:04
Or, since we have it sitting around anyways, m_tas
alokp
2017/02/08 17:34:41
Done.
|
| + kMojoTaskType, BLINK_FROM_HERE, |
| + createSameThreadTask(&runWatchCallback, wrapPersistent(callback), |
| + wrapPersistent(watcher), result)); |
| + } |
| + return watcher; |
| +} |
| + |
| +MojoWatcher::MojoWatcher(ExecutionContext* context, MojoWatchCallback* callback) |
| + : ContextLifecycleObserver(context), |
| + m_taskRunner(TaskRunnerHelper::get(kMojoTaskType, context)), |
|
haraken
2017/02/08 06:32:10
You don't need to hold m_taskRunner, since you can
jbroman
2017/02/08 13:20:04
It's accessed off the main thread, where calling g
|
| + m_callback(this, callback) {} |
| + |
| +MojoWatcher::~MojoWatcher() { |
| + DCHECK(!m_handle.is_valid()); |
| +} |
| + |
| +MojoResult MojoWatcher::watch(mojo::Handle handle, |
| + const MojoHandleSignals& signalsDict) { |
| + ::MojoHandleSignals signals = MOJO_HANDLE_SIGNAL_NONE; |
| + if (signalsDict.readable()) |
| + signals |= MOJO_HANDLE_SIGNAL_READABLE; |
| + if (signalsDict.writable()) |
| + signals |= MOJO_HANDLE_SIGNAL_WRITABLE; |
| + if (signalsDict.peerClosed()) |
| + signals |= MOJO_HANDLE_SIGNAL_PEER_CLOSED; |
| + |
| + MojoResult result = |
| + MojoWatch(handle.value(), signals, &MojoWatcher::onHandleReady, |
| + reinterpret_cast<uintptr_t>(this)); |
| + if (result == MOJO_RESULT_OK) { |
| + m_handle = handle; |
| + } |
| + return result; |
| +} |
| + |
| +MojoResult MojoWatcher::cancel() { |
| + if (!m_handle.is_valid()) |
| + return MOJO_RESULT_OK; |
| + |
| + MojoResult result = |
| + MojoCancelWatch(m_handle.value(), reinterpret_cast<uintptr_t>(this)); |
| + m_handle = mojo::Handle(); |
| + return result; |
| +} |
| + |
| +DEFINE_TRACE(MojoWatcher) { |
| + visitor->trace(m_callback); |
| + ContextLifecycleObserver::trace(visitor); |
| +} |
| + |
| +DEFINE_TRACE_WRAPPERS(MojoWatcher) { |
| + visitor->traceWrappers(m_callback); |
| +} |
| + |
| +bool MojoWatcher::hasPendingActivity() const { |
| + return m_handle.is_valid(); |
| +} |
| + |
| +void MojoWatcher::contextDestroyed(ExecutionContext*) { |
| + cancel(); |
| +} |
| + |
| +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( |
| + BLINK_FROM_HERE, |
| + crossThreadBind(&MojoWatcher::runReadyCallback, |
|
haraken
2017/02/08 06:32:09
Does this need to be a cross-thread task (instead
jbroman
2017/02/08 13:20:04
Ditto: this is called by the Mojo core on a backgr
|
| + wrapCrossThreadWeakPersistent(watcher), result)); |
| +} |
| + |
| +void MojoWatcher::runReadyCallback(MojoResult result) { |
| + // Ignore callbacks if not watching. |
| + if (!m_handle.is_valid()) |
| + return; |
| + |
| + // MOJO_RESULT_CANCELLED indicates that the handle has been closed, in which |
| + // case watch has been implicitly cancelled. There is no need to explicitly |
| + // cancel the watch. |
| + if (result == MOJO_RESULT_CANCELLED) |
| + m_handle = mojo::Handle(); |
| + |
| + runWatchCallback(m_callback, this, result); |
| +} |
| + |
| +} // namespace blink |