OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "core/mojo/MojoWatcher.h" | 5 #include "core/mojo/MojoWatcher.h" |
6 | 6 |
7 #include "bindings/core/v8/MojoWatchCallback.h" | 7 #include "bindings/core/v8/MojoWatchCallback.h" |
8 #include "bindings/core/v8/ScriptState.h" | 8 #include "bindings/core/v8/ScriptState.h" |
9 #include "core/dom/ExecutionContext.h" | 9 #include "core/dom/ExecutionContext.h" |
10 #include "core/dom/TaskRunnerHelper.h" | 10 #include "core/dom/TaskRunnerHelper.h" |
11 #include "core/mojo/MojoHandleSignals.h" | 11 #include "core/mojo/MojoHandleSignals.h" |
12 #include "platform/CrossThreadFunctional.h" | 12 #include "platform/CrossThreadFunctional.h" |
13 #include "platform/WebTaskRunner.h" | 13 #include "platform/WebTaskRunner.h" |
14 | 14 |
15 namespace blink { | 15 namespace blink { |
16 | 16 |
17 static void runWatchCallback(MojoWatchCallback* callback, | 17 static void runWatchCallback(MojoWatchCallback* callback, |
18 ScriptWrappable* wrappable, | 18 ScriptWrappable* wrappable, |
19 MojoResult result) { | 19 MojoResult result) { |
20 callback->call(wrappable, result); | 20 callback->call(wrappable, result); |
21 } | 21 } |
22 | 22 |
| 23 // static |
23 MojoWatcher* MojoWatcher::create(mojo::Handle handle, | 24 MojoWatcher* MojoWatcher::create(mojo::Handle handle, |
24 const MojoHandleSignals& signalsDict, | 25 const MojoHandleSignals& signalsDict, |
25 MojoWatchCallback* callback, | 26 MojoWatchCallback* callback, |
26 ExecutionContext* context) { | 27 ExecutionContext* context) { |
27 MojoWatcher* watcher = new MojoWatcher(context, callback); | 28 MojoWatcher* watcher = new MojoWatcher(context, callback); |
28 MojoResult result = watcher->watch(handle, signalsDict); | 29 MojoResult result = watcher->watch(handle, signalsDict); |
29 // TODO(alokp): Consider raising an exception. | 30 // TODO(alokp): Consider raising an exception. |
30 // Current clients expect to recieve the initial error returned by MojoWatch | 31 // Current clients expect to recieve the initial error returned by MojoWatch |
31 // via watch callback. | 32 // via watch callback. |
32 // | 33 // |
33 // Note that the usage of wrapPersistent is intentional so that the intial | 34 // Note that the usage of wrapPersistent is intentional so that the intial |
34 // error is guaranteed to be reported to the client in case where the given | 35 // error is guaranteed to be reported to the client in case where the given |
35 // handle is invalid and garbage collection happens before the callback | 36 // handle is invalid and garbage collection happens before the callback |
36 // is scheduled. | 37 // is scheduled. |
37 if (result != MOJO_RESULT_OK) { | 38 if (result != MOJO_RESULT_OK) { |
38 watcher->m_taskRunner->postTask( | 39 watcher->m_taskRunner->postTask( |
39 BLINK_FROM_HERE, WTF::bind(&runWatchCallback, wrapPersistent(callback), | 40 BLINK_FROM_HERE, WTF::bind(&runWatchCallback, wrapPersistent(callback), |
40 wrapPersistent(watcher), result)); | 41 wrapPersistent(watcher), result)); |
41 } | 42 } |
42 return watcher; | 43 return watcher; |
43 } | 44 } |
44 | 45 |
45 MojoWatcher::MojoWatcher(ExecutionContext* context, MojoWatchCallback* callback) | |
46 : ContextLifecycleObserver(context), | |
47 m_taskRunner(TaskRunnerHelper::get(TaskType::UnspecedTimer, context)), | |
48 m_callback(this, callback) {} | |
49 | |
50 MojoWatcher::~MojoWatcher() { | 46 MojoWatcher::~MojoWatcher() { |
51 DCHECK(!m_handle.is_valid()); | 47 DCHECK(!m_handle.is_valid()); |
52 } | 48 } |
53 | 49 |
54 MojoResult MojoWatcher::watch(mojo::Handle handle, | 50 MojoResult MojoWatcher::cancel() { |
55 const MojoHandleSignals& signalsDict) { | 51 if (!m_watcherHandle.is_valid()) |
56 ::MojoHandleSignals signals = MOJO_HANDLE_SIGNAL_NONE; | 52 return MOJO_RESULT_INVALID_ARGUMENT; |
57 if (signalsDict.readable()) | |
58 signals |= MOJO_HANDLE_SIGNAL_READABLE; | |
59 if (signalsDict.writable()) | |
60 signals |= MOJO_HANDLE_SIGNAL_WRITABLE; | |
61 if (signalsDict.peerClosed()) | |
62 signals |= MOJO_HANDLE_SIGNAL_PEER_CLOSED; | |
63 | 53 |
64 MojoResult result = | 54 m_watcherHandle.reset(); |
65 MojoWatch(handle.value(), signals, &MojoWatcher::onHandleReady, | 55 return MOJO_RESULT_OK; |
66 reinterpret_cast<uintptr_t>(this)); | |
67 if (result == MOJO_RESULT_OK) { | |
68 m_handle = handle; | |
69 } | |
70 return result; | |
71 } | |
72 | |
73 MojoResult MojoWatcher::cancel() { | |
74 if (!m_handle.is_valid()) | |
75 return MOJO_RESULT_OK; | |
76 | |
77 MojoResult result = | |
78 MojoCancelWatch(m_handle.value(), reinterpret_cast<uintptr_t>(this)); | |
79 m_handle = mojo::Handle(); | |
80 return result; | |
81 } | 56 } |
82 | 57 |
83 DEFINE_TRACE(MojoWatcher) { | 58 DEFINE_TRACE(MojoWatcher) { |
84 visitor->trace(m_callback); | 59 visitor->trace(m_callback); |
85 ContextLifecycleObserver::trace(visitor); | 60 ContextLifecycleObserver::trace(visitor); |
86 } | 61 } |
87 | 62 |
88 DEFINE_TRACE_WRAPPERS(MojoWatcher) { | 63 DEFINE_TRACE_WRAPPERS(MojoWatcher) { |
89 visitor->traceWrappers(m_callback); | 64 visitor->traceWrappers(m_callback); |
90 } | 65 } |
91 | 66 |
92 bool MojoWatcher::hasPendingActivity() const { | 67 bool MojoWatcher::hasPendingActivity() const { |
93 return m_handle.is_valid(); | 68 return m_handle.is_valid(); |
94 } | 69 } |
95 | 70 |
96 void MojoWatcher::contextDestroyed(ExecutionContext*) { | 71 void MojoWatcher::contextDestroyed(ExecutionContext*) { |
97 cancel(); | 72 cancel(); |
98 } | 73 } |
99 | 74 |
| 75 MojoWatcher::MojoWatcher(ExecutionContext* context, MojoWatchCallback* callback) |
| 76 : ContextLifecycleObserver(context), |
| 77 m_taskRunner(TaskRunnerHelper::get(TaskType::UnspecedTimer, context)), |
| 78 m_callback(this, callback) {} |
| 79 |
| 80 MojoResult MojoWatcher::watch(mojo::Handle handle, |
| 81 const MojoHandleSignals& signalsDict) { |
| 82 ::MojoHandleSignals signals = MOJO_HANDLE_SIGNAL_NONE; |
| 83 if (signalsDict.readable()) |
| 84 signals |= MOJO_HANDLE_SIGNAL_READABLE; |
| 85 if (signalsDict.writable()) |
| 86 signals |= MOJO_HANDLE_SIGNAL_WRITABLE; |
| 87 if (signalsDict.peerClosed()) |
| 88 signals |= MOJO_HANDLE_SIGNAL_PEER_CLOSED; |
| 89 |
| 90 MojoResult result = |
| 91 mojo::CreateWatcher(&MojoWatcher::onHandleReady, &m_watcherHandle); |
| 92 DCHECK_EQ(MOJO_RESULT_OK, result); |
| 93 |
| 94 result = MojoWatch(m_watcherHandle.get().value(), handle.value(), signals, |
| 95 reinterpret_cast<uintptr_t>(this)); |
| 96 if (result != MOJO_RESULT_OK) |
| 97 return result; |
| 98 |
| 99 m_handle = handle; |
| 100 |
| 101 MojoResult readyResult; |
| 102 result = arm(&readyResult); |
| 103 if (result == MOJO_RESULT_OK) |
| 104 return result; |
| 105 |
| 106 // We couldn't arm the watcher because the handle is already ready to |
| 107 // trigger a success notification. Post a notification manually. |
| 108 DCHECK_EQ(MOJO_RESULT_FAILED_PRECONDITION, result); |
| 109 m_taskRunner->postTask(BLINK_FROM_HERE, |
| 110 WTF::bind(&MojoWatcher::runReadyCallback, |
| 111 wrapPersistent(this), readyResult)); |
| 112 return MOJO_RESULT_OK; |
| 113 } |
| 114 |
| 115 MojoResult MojoWatcher::arm(MojoResult* readyResult) { |
| 116 // Nothing to do if the watcher is inactive. |
| 117 if (!m_handle.is_valid()) |
| 118 return MOJO_RESULT_OK; |
| 119 |
| 120 uint32_t numReadyContexts = 1; |
| 121 uintptr_t readyContext; |
| 122 MojoResult localReadyResult; |
| 123 MojoHandleSignalsState readySignals; |
| 124 MojoResult result = |
| 125 MojoArmWatcher(m_watcherHandle.get().value(), &numReadyContexts, |
| 126 &readyContext, &localReadyResult, &readySignals); |
| 127 if (result == MOJO_RESULT_OK) |
| 128 return MOJO_RESULT_OK; |
| 129 |
| 130 DCHECK_EQ(MOJO_RESULT_FAILED_PRECONDITION, result); |
| 131 DCHECK_EQ(1u, numReadyContexts); |
| 132 DCHECK_EQ(reinterpret_cast<uintptr_t>(this), readyContext); |
| 133 *readyResult = localReadyResult; |
| 134 return result; |
| 135 } |
| 136 |
100 void MojoWatcher::onHandleReady(uintptr_t context, | 137 void MojoWatcher::onHandleReady(uintptr_t context, |
101 MojoResult result, | 138 MojoResult result, |
102 MojoHandleSignalsState, | 139 MojoHandleSignalsState, |
103 MojoWatchNotificationFlags) { | 140 MojoWatcherNotificationFlags) { |
104 // It is safe to assume the MojoWatcher still exists because this | 141 // It is safe to assume the MojoWatcher still exists. It stays alive at least |
105 // callback will never be run after MojoWatcher destructor, | 142 // as long as |m_handle| is valid, and |m_handle| is only reset after we |
106 // which cancels the watch. | 143 // dispatch a |MOJO_RESULT_CANCELLED| notification. That is always the last |
| 144 // notification received by this callback. |
107 MojoWatcher* watcher = reinterpret_cast<MojoWatcher*>(context); | 145 MojoWatcher* watcher = reinterpret_cast<MojoWatcher*>(context); |
108 watcher->m_taskRunner->postTask( | 146 watcher->m_taskRunner->postTask( |
109 BLINK_FROM_HERE, | 147 BLINK_FROM_HERE, |
110 crossThreadBind(&MojoWatcher::runReadyCallback, | 148 crossThreadBind(&MojoWatcher::runReadyCallback, |
111 wrapCrossThreadWeakPersistent(watcher), result)); | 149 wrapCrossThreadWeakPersistent(watcher), result)); |
112 } | 150 } |
113 | 151 |
114 void MojoWatcher::runReadyCallback(MojoResult result) { | 152 void MojoWatcher::runReadyCallback(MojoResult result) { |
| 153 if (result == MOJO_RESULT_CANCELLED) { |
| 154 // Last notification. |
| 155 m_handle = mojo::Handle(); |
| 156 |
| 157 // Only dispatch to the callback if this cancellation was implicit due to |
| 158 // |m_handle| closure. If it was explicit, |m_watcherHandle| has already |
| 159 // been reset. |
| 160 if (m_watcherHandle.is_valid()) { |
| 161 m_watcherHandle.reset(); |
| 162 runWatchCallback(m_callback, this, result); |
| 163 } |
| 164 return; |
| 165 } |
| 166 |
115 // Ignore callbacks if not watching. | 167 // Ignore callbacks if not watching. |
116 if (!m_handle.is_valid()) | 168 if (!m_watcherHandle.is_valid()) |
117 return; | 169 return; |
118 | 170 |
119 // MOJO_RESULT_CANCELLED indicates that the handle has been closed, in which | 171 runWatchCallback(m_callback, this, result); |
120 // case watch has been implicitly cancelled. There is no need to explicitly | |
121 // cancel the watch. | |
122 if (result == MOJO_RESULT_CANCELLED) | |
123 m_handle = mojo::Handle(); | |
124 | 172 |
125 runWatchCallback(m_callback, this, result); | 173 // Rearm the watcher so another notification can fire. |
| 174 // |
| 175 // TODO(rockot): MojoWatcher should expose some better approximation of the |
| 176 // new watcher API, including explicit add and removal of handles from the |
| 177 // watcher, as well as explicit arming. |
| 178 MojoResult readyResult; |
| 179 MojoResult armResult = arm(&readyResult); |
| 180 if (armResult == MOJO_RESULT_OK) |
| 181 return; |
| 182 |
| 183 DCHECK_EQ(MOJO_RESULT_FAILED_PRECONDITION, armResult); |
| 184 |
| 185 m_taskRunner->postTask(BLINK_FROM_HERE, |
| 186 WTF::bind(&MojoWatcher::runReadyCallback, |
| 187 wrapWeakPersistent(this), readyResult)); |
126 } | 188 } |
127 | 189 |
128 } // namespace blink | 190 } // namespace blink |
OLD | NEW |