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 | 
| index 804536d40d710b92a73294c1b43e30129550f58e..c7d701bf4c144116ade73526b78cb012035bd88a 100644 | 
| --- a/third_party/WebKit/Source/core/mojo/MojoWatcher.cpp | 
| +++ b/third_party/WebKit/Source/core/mojo/MojoWatcher.cpp | 
| @@ -20,6 +20,7 @@ static void runWatchCallback(MojoWatchCallback* callback, | 
| callback->call(wrappable, result); | 
| } | 
| +// static | 
| MojoWatcher* MojoWatcher::create(mojo::Handle handle, | 
| const MojoHandleSignals& signalsDict, | 
| MojoWatchCallback* callback, | 
| @@ -27,8 +28,8 @@ MojoWatcher* MojoWatcher::create(mojo::Handle handle, | 
| 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. | 
| + // Current clients expect to recieve the initial error returned by | 
| + // MojoRegisterWatcher 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 | 
| @@ -39,43 +40,20 @@ MojoWatcher* MojoWatcher::create(mojo::Handle handle, | 
| BLINK_FROM_HERE, WTF::bind(&runWatchCallback, wrapPersistent(callback), | 
| wrapPersistent(watcher), result)); | 
| } | 
| + | 
| return watcher; | 
| } | 
| -MojoWatcher::MojoWatcher(ExecutionContext* context, MojoWatchCallback* callback) | 
| - : ContextLifecycleObserver(context), | 
| - m_taskRunner(TaskRunnerHelper::get(TaskType::UnspecedTimer, context)), | 
| - 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)); | 
| + MojoResult result = MojoUnregisterWatcher(m_handle.value(), | 
| + reinterpret_cast<uintptr_t>(this)); | 
| m_handle = mojo::Handle(); | 
| return result; | 
| } | 
| @@ -97,13 +75,54 @@ void MojoWatcher::contextDestroyed(ExecutionContext*) { | 
| cancel(); | 
| } | 
| +MojoWatcher::MojoWatcher(ExecutionContext* context, MojoWatchCallback* callback) | 
| + : ContextLifecycleObserver(context), | 
| + m_taskRunner(TaskRunnerHelper::get(TaskType::UnspecedTimer, context)), | 
| + m_callback(this, callback) {} | 
| + | 
| +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 = | 
| + MojoRegisterWatcher(handle.value(), signals, &MojoWatcher::onHandleReady, | 
| + reinterpret_cast<uintptr_t>(this)); | 
| + if (result == MOJO_RESULT_OK) { | 
| + m_handle = handle; | 
| + arm(); | 
| + } | 
| + return result; | 
| +} | 
| + | 
| +void MojoWatcher::arm() { | 
| + // Nothing to do if the watcher is inactive. | 
| + if (!m_handle.is_valid()) | 
| + return; | 
| + | 
| + MojoResult result = | 
| + MojoArmWatcher(m_handle.value(), reinterpret_cast<uintptr_t>(this)); | 
| + if (result == MOJO_RESULT_OK) | 
| + return; | 
| + if (result == MOJO_RESULT_ALREADY_EXISTS) | 
| + result = MOJO_RESULT_OK; | 
| + m_taskRunner->postTask( | 
| + BLINK_FROM_HERE, | 
| + WTF::bind(&MojoWatcher::runReadyCallback, wrapPersistent(this), result)); | 
| +} | 
| + | 
| 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. | 
| + // which unregisters the watcher. | 
| MojoWatcher* watcher = reinterpret_cast<MojoWatcher*>(context); | 
| watcher->m_taskRunner->postTask( | 
| BLINK_FROM_HERE, | 
| @@ -117,12 +136,15 @@ void MojoWatcher::runReadyCallback(MojoResult result) { | 
| 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. | 
| + // case watcher has been implicitly cancelled. There is no need to explicitly | 
| + // cancel the watcher. | 
| if (result == MOJO_RESULT_CANCELLED) | 
| m_handle = mojo::Handle(); | 
| runWatchCallback(m_callback, this, result); | 
| + | 
| + // Rearm the watcher so another notification can fire. | 
| + arm(); | 
| 
 
yzshen1
2017/03/03 00:03:50
Will it make things simpler if we use mojo::Watche
 
Ken Rockot(use gerrit already)
2017/03/03 00:37:05
Yes, most certainly, but I assume it will result i
 
 | 
| } | 
| } // namespace blink |