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 | |
24 MojoWatcher* MojoWatcher::create(mojo::Handle handle, | 23 MojoWatcher* MojoWatcher::create(mojo::Handle handle, |
25 const MojoHandleSignals& signalsDict, | 24 const MojoHandleSignals& signalsDict, |
26 MojoWatchCallback* callback, | 25 MojoWatchCallback* callback, |
27 ExecutionContext* context) { | 26 ExecutionContext* context) { |
28 MojoWatcher* watcher = new MojoWatcher(context, callback); | 27 MojoWatcher* watcher = new MojoWatcher(context, callback); |
29 MojoResult result = watcher->watch(handle, signalsDict); | 28 MojoResult result = watcher->watch(handle, signalsDict); |
30 // TODO(alokp): Consider raising an exception. | 29 // TODO(alokp): Consider raising an exception. |
31 // Current clients expect to recieve the initial error returned by MojoWatch | 30 // Current clients expect to recieve the initial error returned by MojoWatch |
32 // via watch callback. | 31 // via watch callback. |
33 // | 32 // |
34 // Note that the usage of wrapPersistent is intentional so that the intial | 33 // Note that the usage of wrapPersistent is intentional so that the intial |
35 // error is guaranteed to be reported to the client in case where the given | 34 // error is guaranteed to be reported to the client in case where the given |
36 // handle is invalid and garbage collection happens before the callback | 35 // handle is invalid and garbage collection happens before the callback |
37 // is scheduled. | 36 // is scheduled. |
38 if (result != MOJO_RESULT_OK) { | 37 if (result != MOJO_RESULT_OK) { |
39 watcher->m_taskRunner->postTask( | 38 watcher->m_taskRunner->postTask( |
40 BLINK_FROM_HERE, WTF::bind(&runWatchCallback, wrapPersistent(callback), | 39 BLINK_FROM_HERE, WTF::bind(&runWatchCallback, wrapPersistent(callback), |
41 wrapPersistent(watcher), result)); | 40 wrapPersistent(watcher), result)); |
42 } | 41 } |
43 return watcher; | 42 return watcher; |
44 } | 43 } |
45 | 44 |
| 45 MojoWatcher::MojoWatcher(ExecutionContext* context, MojoWatchCallback* callback) |
| 46 : ContextLifecycleObserver(context), |
| 47 m_taskRunner(TaskRunnerHelper::get(TaskType::UnspecedTimer, context)), |
| 48 m_callback(this, callback) {} |
| 49 |
46 MojoWatcher::~MojoWatcher() { | 50 MojoWatcher::~MojoWatcher() { |
47 DCHECK(!m_handle.is_valid()); | 51 DCHECK(!m_handle.is_valid()); |
48 } | 52 } |
49 | 53 |
| 54 MojoResult MojoWatcher::watch(mojo::Handle handle, |
| 55 const MojoHandleSignals& signalsDict) { |
| 56 ::MojoHandleSignals signals = MOJO_HANDLE_SIGNAL_NONE; |
| 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 |
| 64 MojoResult result = |
| 65 MojoWatch(handle.value(), signals, &MojoWatcher::onHandleReady, |
| 66 reinterpret_cast<uintptr_t>(this)); |
| 67 if (result == MOJO_RESULT_OK) { |
| 68 m_handle = handle; |
| 69 } |
| 70 return result; |
| 71 } |
| 72 |
50 MojoResult MojoWatcher::cancel() { | 73 MojoResult MojoWatcher::cancel() { |
51 if (!m_watcherHandle.is_valid()) | 74 if (!m_handle.is_valid()) |
52 return MOJO_RESULT_INVALID_ARGUMENT; | 75 return MOJO_RESULT_OK; |
53 | 76 |
54 m_watcherHandle.reset(); | 77 MojoResult result = |
55 return MOJO_RESULT_OK; | 78 MojoCancelWatch(m_handle.value(), reinterpret_cast<uintptr_t>(this)); |
| 79 m_handle = mojo::Handle(); |
| 80 return result; |
56 } | 81 } |
57 | 82 |
58 DEFINE_TRACE(MojoWatcher) { | 83 DEFINE_TRACE(MojoWatcher) { |
59 visitor->trace(m_callback); | 84 visitor->trace(m_callback); |
60 ContextLifecycleObserver::trace(visitor); | 85 ContextLifecycleObserver::trace(visitor); |
61 } | 86 } |
62 | 87 |
63 DEFINE_TRACE_WRAPPERS(MojoWatcher) { | 88 DEFINE_TRACE_WRAPPERS(MojoWatcher) { |
64 visitor->traceWrappers(m_callback); | 89 visitor->traceWrappers(m_callback); |
65 } | 90 } |
66 | 91 |
67 bool MojoWatcher::hasPendingActivity() const { | 92 bool MojoWatcher::hasPendingActivity() const { |
68 return m_handle.is_valid(); | 93 return m_handle.is_valid(); |
69 } | 94 } |
70 | 95 |
71 void MojoWatcher::contextDestroyed(ExecutionContext*) { | 96 void MojoWatcher::contextDestroyed(ExecutionContext*) { |
72 cancel(); | 97 cancel(); |
73 } | 98 } |
74 | 99 |
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 | |
137 void MojoWatcher::onHandleReady(uintptr_t context, | 100 void MojoWatcher::onHandleReady(uintptr_t context, |
138 MojoResult result, | 101 MojoResult result, |
139 MojoHandleSignalsState, | 102 MojoHandleSignalsState, |
140 MojoWatcherNotificationFlags) { | 103 MojoWatchNotificationFlags) { |
141 // It is safe to assume the MojoWatcher still exists. It stays alive at least | 104 // It is safe to assume the MojoWatcher still exists because this |
142 // as long as |m_handle| is valid, and |m_handle| is only reset after we | 105 // callback will never be run after MojoWatcher destructor, |
143 // dispatch a |MOJO_RESULT_CANCELLED| notification. That is always the last | 106 // which cancels the watch. |
144 // notification received by this callback. | |
145 MojoWatcher* watcher = reinterpret_cast<MojoWatcher*>(context); | 107 MojoWatcher* watcher = reinterpret_cast<MojoWatcher*>(context); |
146 watcher->m_taskRunner->postTask( | 108 watcher->m_taskRunner->postTask( |
147 BLINK_FROM_HERE, | 109 BLINK_FROM_HERE, |
148 crossThreadBind(&MojoWatcher::runReadyCallback, | 110 crossThreadBind(&MojoWatcher::runReadyCallback, |
149 wrapCrossThreadWeakPersistent(watcher), result)); | 111 wrapCrossThreadWeakPersistent(watcher), result)); |
150 } | 112 } |
151 | 113 |
152 void MojoWatcher::runReadyCallback(MojoResult result) { | 114 void MojoWatcher::runReadyCallback(MojoResult result) { |
153 if (result == MOJO_RESULT_CANCELLED) { | 115 // Ignore callbacks if not watching. |
154 // Last notification. | 116 if (!m_handle.is_valid()) |
| 117 return; |
| 118 |
| 119 // MOJO_RESULT_CANCELLED indicates that the handle has been closed, in which |
| 120 // case watch has been implicitly cancelled. There is no need to explicitly |
| 121 // cancel the watch. |
| 122 if (result == MOJO_RESULT_CANCELLED) |
155 m_handle = mojo::Handle(); | 123 m_handle = mojo::Handle(); |
156 | 124 |
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 | |
167 // Ignore callbacks if not watching. | |
168 if (!m_watcherHandle.is_valid()) | |
169 return; | |
170 | |
171 runWatchCallback(m_callback, this, result); | 125 runWatchCallback(m_callback, this, result); |
172 | |
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)); | |
188 } | 126 } |
189 | 127 |
190 } // namespace blink | 128 } // namespace blink |
OLD | NEW |