| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 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/MojoHandleSignals.h" |
| 13 #include "platform/WebTaskRunner.h" |
| 14 |
| 15 namespace blink { |
| 16 |
| 17 MojoWatcher* MojoWatcher::create(mojo::Handle handle, |
| 18 const MojoHandleSignals& signalsDict, |
| 19 MojoWatchCallback* callback, |
| 20 ExecutionContext* context) { |
| 21 MojoWatcher* watcher = new MojoWatcher(handle, callback, context); |
| 22 |
| 23 ::MojoHandleSignals signals = MOJO_HANDLE_SIGNAL_NONE; |
| 24 if (signalsDict.readable()) |
| 25 signals |= MOJO_HANDLE_SIGNAL_READABLE; |
| 26 if (signalsDict.writable()) |
| 27 signals |= MOJO_HANDLE_SIGNAL_WRITABLE; |
| 28 if (signalsDict.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 // |
| 38 // Note that the usage of wrapPersistent is intentional so that the intial |
| 39 // error is guaranteed to be reported to the client in case where the given |
| 40 // handle is invalid and garbage collection happens before the callback |
| 41 // is scheduled. |
| 42 if (result != MOJO_RESULT_OK) { |
| 43 watcher->m_taskRunner->postTask(BLINK_FROM_HERE, |
| 44 WTF::bind(&MojoWatcher::runReadyCallback, |
| 45 wrapPersistent(watcher), result)); |
| 46 } |
| 47 return watcher; |
| 48 } |
| 49 |
| 50 MojoWatcher::MojoWatcher(mojo::Handle handle, |
| 51 MojoWatchCallback* callback, |
| 52 ExecutionContext* context) |
| 53 : ContextLifecycleObserver(context), |
| 54 m_handle(handle), |
| 55 m_callback(this, callback), |
| 56 m_taskRunner(TaskRunnerHelper::get(TaskType::MiscPlatformAPI, context)) {} |
| 57 |
| 58 MojoWatcher::~MojoWatcher() { |
| 59 DCHECK(!m_handle.is_valid()); |
| 60 } |
| 61 |
| 62 MojoResult MojoWatcher::cancel() { |
| 63 if (!m_handle.is_valid()) |
| 64 return MOJO_RESULT_OK; |
| 65 |
| 66 MojoResult result = |
| 67 MojoCancelWatch(m_handle.value(), reinterpret_cast<uintptr_t>(this)); |
| 68 m_handle = mojo::Handle(); |
| 69 return result; |
| 70 } |
| 71 |
| 72 DEFINE_TRACE(MojoWatcher) { |
| 73 visitor->trace(m_callback); |
| 74 ContextLifecycleObserver::trace(visitor); |
| 75 } |
| 76 |
| 77 DEFINE_TRACE_WRAPPERS(MojoWatcher) { |
| 78 visitor->traceWrappers(m_callback); |
| 79 } |
| 80 |
| 81 bool MojoWatcher::hasPendingActivity() const { |
| 82 return m_handle.is_valid(); |
| 83 } |
| 84 |
| 85 void MojoWatcher::contextDestroyed(ExecutionContext*) { |
| 86 cancel(); |
| 87 } |
| 88 |
| 89 void MojoWatcher::onHandleReady(uintptr_t context, |
| 90 MojoResult result, |
| 91 MojoHandleSignalsState, |
| 92 MojoWatchNotificationFlags) { |
| 93 // It is safe to assume the MojoWatcher still exists because this |
| 94 // callback will never be run after MojoWatcher destructor, |
| 95 // which cancels the watch. |
| 96 MojoWatcher* watcher = reinterpret_cast<MojoWatcher*>(context); |
| 97 watcher->m_taskRunner->postTask( |
| 98 BLINK_FROM_HERE, |
| 99 crossThreadBind(&MojoWatcher::runReadyCallback, |
| 100 wrapCrossThreadWeakPersistent(watcher), result)); |
| 101 } |
| 102 |
| 103 void MojoWatcher::runReadyCallback(MojoResult result) { |
| 104 if (!m_handle.is_valid()) |
| 105 return; |
| 106 |
| 107 // MOJO_RESULT_CANCELLED indicates that the handle has been closed, in which |
| 108 // case watch has been implicitly cancelled. There is no need to explicitly |
| 109 // cancel the watch. |
| 110 if (result == MOJO_RESULT_CANCELLED) |
| 111 m_handle = mojo::Handle(); |
| 112 |
| 113 m_callback->call(this, result); |
| 114 } |
| 115 |
| 116 } // namespace blink |
| OLD | NEW |