Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "core/mojo/MojoWatcher.h" | |
| 6 | |
| 7 #include "bindings/core/v8/MojoWatchCallback.h" | |
| 8 #include "bindings/core/v8/ScriptState.h" | |
| 9 #include "core/dom/ExecutionContext.h" | |
| 10 #include "core/dom/ExecutionContextTask.h" | |
| 11 #include "core/dom/TaskRunnerHelper.h" | |
| 12 #include "core/mojo/MojoWatchOptions.h" | |
| 13 #include "platform/WebTaskRunner.h" | |
| 14 | |
| 15 namespace blink { | |
| 16 | |
| 17 MojoWatcher* MojoWatcher::create(mojo::Handle handle, | |
|
jbroman
2017/01/11 23:00:00
What if !handle.is_valid() to begin with? (i.e. we
alokp
2017/01/12 01:20:26
Great catch.
I have TODOs about using exception i
| |
| 18 const MojoWatchOptions& options, | |
| 19 MojoWatchCallback* callback, | |
| 20 ExecutionContext* context) { | |
| 21 MojoWatcher* watcher = new MojoWatcher(handle, callback, context); | |
| 22 | |
| 23 MojoHandleSignals signals = MOJO_HANDLE_SIGNAL_NONE; | |
| 24 if (options.readable()) | |
| 25 signals |= MOJO_HANDLE_SIGNAL_READABLE; | |
| 26 if (options.writable()) | |
| 27 signals |= MOJO_HANDLE_SIGNAL_WRITABLE; | |
| 28 if (options.peerClosed()) | |
| 29 signals |= MOJO_HANDLE_SIGNAL_PEER_CLOSED; | |
| 30 | |
| 31 MojoResult result = | |
| 32 MojoWatch(handle.value(), signals, &MojoWatcher::onHandleReady, | |
| 33 reinterpret_cast<uintptr_t>(watcher)); | |
| 34 // TODO(alokp): Consider raising an exception. | |
| 35 // Current clients expect to recieve the initial error returned by MojoWatch | |
| 36 // via watch callback. | |
| 37 if (result != MOJO_RESULT_OK) { | |
| 38 watcher->m_taskRunner->postTask( | |
| 39 BLINK_FROM_HERE, WTF::bind(&MojoWatcher::runReadyCallback, | |
| 40 wrapWeakPersistent(watcher), result)); | |
| 41 } | |
| 42 return watcher; | |
| 43 } | |
| 44 | |
| 45 MojoWatcher::MojoWatcher(mojo::Handle handle, | |
| 46 MojoWatchCallback* callback, | |
| 47 ExecutionContext* context) | |
| 48 : ContextLifecycleObserver(context), | |
| 49 m_handle(handle), | |
| 50 m_callback(this, callback), | |
| 51 m_taskRunner(TaskRunnerHelper::get(TaskType::MiscPlatformAPI, context)) {} | |
| 52 | |
| 53 MojoWatcher::~MojoWatcher() { | |
| 54 DCHECK(!m_handle.is_valid()); | |
| 55 } | |
| 56 | |
| 57 MojoResult MojoWatcher::cancel() { | |
| 58 MojoResult result = MOJO_RESULT_OK; | |
| 59 if (m_handle.is_valid()) { | |
|
jbroman
2017/01/11 23:00:00
nit: I mildly prefer early return, but I don't fee
alokp
2017/01/12 01:20:26
Done.
| |
| 60 result = | |
| 61 MojoCancelWatch(m_handle.value(), reinterpret_cast<uintptr_t>(this)); | |
| 62 m_handle = mojo::Handle(); | |
| 63 } | |
| 64 return result; | |
| 65 } | |
| 66 | |
| 67 DEFINE_TRACE(MojoWatcher) { | |
| 68 ContextLifecycleObserver::trace(visitor); | |
| 69 } | |
|
jbroman
2017/01/11 23:00:01
m_callback must be traced here as well
alokp
2017/01/12 01:20:26
Done.
| |
| 70 | |
| 71 DEFINE_TRACE_WRAPPERS(MojoWatcher) { | |
| 72 visitor->traceWrappers(m_callback); | |
| 73 } | |
| 74 | |
| 75 bool MojoWatcher::hasPendingActivity() const { | |
| 76 return m_handle.is_valid() && getExecutionContext(); | |
|
jbroman
2017/01/11 23:00:01
nit: the getExecutionContext() check is implied an
alokp
2017/01/12 01:20:26
Done.
| |
| 77 } | |
| 78 | |
| 79 void MojoWatcher::contextDestroyed() { | |
| 80 cancel(); | |
| 81 } | |
| 82 | |
| 83 void MojoWatcher::onHandleReady(uintptr_t context, | |
| 84 MojoResult result, | |
| 85 MojoHandleSignalsState, | |
| 86 MojoWatchNotificationFlags) { | |
| 87 // It is safe to assume the MojoWatcher still exists because this | |
| 88 // callback will never be run after MojoWatcher destructor, | |
| 89 // which cancels the watch. | |
| 90 MojoWatcher* watcher = reinterpret_cast<MojoWatcher*>(context); | |
| 91 watcher->m_taskRunner->postTask( | |
| 92 BLINK_FROM_HERE, | |
| 93 crossThreadBind(&MojoWatcher::runReadyCallback, | |
| 94 wrapCrossThreadWeakPersistent(watcher), result)); | |
| 95 } | |
| 96 | |
| 97 void MojoWatcher::runReadyCallback(MojoResult result) { | |
| 98 if (!m_handle.is_valid()) | |
| 99 return; | |
| 100 | |
| 101 if (result != MOJO_RESULT_OK) | |
| 102 cancel(); | |
|
jbroman
2017/01/11 23:00:01
Is this the right thing to do here? mojo/public/c/
alokp
2017/01/12 01:20:26
Fixed.
| |
| 103 | |
| 104 m_callback->call(this, result); | |
| 105 } | |
| 106 | |
| 107 } // namespace blink | |
| OLD | NEW |