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..ccdf7fd88bad8e657246d1a33ccc263701955856 |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/core/mojo/MojoWatcher.cpp |
| @@ -0,0 +1,105 @@ |
| +// 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 "public/platform/Platform.h" |
| +#include "public/platform/WebTaskRunner.h" |
| + |
| +namespace blink { |
| + |
| +class MojoWatcher::RunReadyCallbackTask : public WebTaskRunner::Task { |
| + public: |
| + RunReadyCallbackTask(MojoWatcher* watcher, MojoResult result) |
| + : m_watcher(watcher), m_result(result) {} |
| + |
| + void run() final { |
| + if (m_watcher) |
| + m_watcher->runReadyCallback(m_result); |
| + } |
| + |
| + private: |
| + CrossThreadWeakPersistent<MojoWatcher> m_watcher; |
|
esprehn
2016/10/25 03:34:06
Can we just WTF::Bind() and createBaseCallback() i
alokp
2016/10/25 05:01:37
Sure. But since you recommend using ExecutionConte
|
| + MojoResult m_result; |
| +}; |
| + |
| +MojoWatcher* MojoWatcher::create(mojo::Handle handle, |
| + MojoHandleSignals signals, |
| + ScriptState* scriptState, |
| + MojoWatchCallback* callback) { |
| + MojoWatcher* watcher = new MojoWatcher( |
| + handle, scriptState, callback, |
| + Platform::current()->currentThread()->getWebTaskRunner()->clone()); |
|
esprehn
2016/10/25 03:34:06
I think you want to use the TaskRunner from the Ex
alokp
2016/10/25 05:01:37
Yeah that should work. Is it safe to call ScriptSt
|
| + |
| + 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, |
| + new RunReadyCallbackTask(watcher, result)); |
| + } |
| + return watcher; |
| +} |
| + |
| +MojoWatcher::MojoWatcher(mojo::Handle handle, |
| + ScriptState* scriptState, |
| + MojoWatchCallback* callback, |
| + std::unique_ptr<WebTaskRunner> taskRunner) |
| + : m_handle(handle), |
| + m_scriptState(scriptState), |
| + m_callback(callback), |
| + m_taskRunner(std::move(taskRunner)) {} |
| + |
| +MojoWatcher::~MojoWatcher() { |
| + DCHECK(m_taskRunner->runsTasksOnCurrentThread()); |
| + cancel(); |
| +} |
| + |
| +MojoResult MojoWatcher::cancel() { |
| + DCHECK(m_taskRunner->runsTasksOnCurrentThread()); |
| + |
| + 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); |
| + if (watcher->m_taskRunner->runsTasksOnCurrentThread()) { |
|
esprehn
2016/10/25 03:34:06
Hmm, did it do this in the old bindings?
alokp
2016/10/25 05:01:37
Yes - the old bindings use mojo::Watcher, which do
|
| + watcher->runReadyCallback(result); |
| + } else { |
| + watcher->m_taskRunner->postTask(BLINK_FROM_HERE, |
| + new RunReadyCallbackTask(watcher, result)); |
| + } |
| +} |
| + |
| +void MojoWatcher::runReadyCallback(MojoResult result) { |
| + DCHECK(m_taskRunner->runsTasksOnCurrentThread()); |
| + |
| + if (result != MOJO_RESULT_OK) |
| + cancel(); |
| + |
| + m_callback->call(m_scriptState.get(), nullptr, result); |
| +} |
| + |
| +} // namespace blink |