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/ExecutionContextTask.h" | 10 #include "core/dom/ExecutionContextTask.h" |
11 #include "core/dom/TaskRunnerHelper.h" | 11 #include "core/dom/TaskRunnerHelper.h" |
12 #include "core/mojo/MojoHandleSignals.h" | 12 #include "core/mojo/MojoHandleSignals.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 |
31 // via watch callback. | 32 // MojoRegisterWatcher 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 } |
43 | |
42 return watcher; | 44 return watcher; |
43 } | 45 } |
44 | 46 |
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() { | 47 MojoWatcher::~MojoWatcher() { |
51 DCHECK(!m_handle.is_valid()); | 48 DCHECK(!m_handle.is_valid()); |
52 } | 49 } |
53 | 50 |
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 | |
73 MojoResult MojoWatcher::cancel() { | 51 MojoResult MojoWatcher::cancel() { |
74 if (!m_handle.is_valid()) | 52 if (!m_handle.is_valid()) |
75 return MOJO_RESULT_OK; | 53 return MOJO_RESULT_OK; |
76 | 54 |
77 MojoResult result = | 55 MojoResult result = MojoUnregisterWatcher(m_handle.value(), |
78 MojoCancelWatch(m_handle.value(), reinterpret_cast<uintptr_t>(this)); | 56 reinterpret_cast<uintptr_t>(this)); |
79 m_handle = mojo::Handle(); | 57 m_handle = mojo::Handle(); |
80 return result; | 58 return result; |
81 } | 59 } |
82 | 60 |
83 DEFINE_TRACE(MojoWatcher) { | 61 DEFINE_TRACE(MojoWatcher) { |
84 visitor->trace(m_callback); | 62 visitor->trace(m_callback); |
85 ContextLifecycleObserver::trace(visitor); | 63 ContextLifecycleObserver::trace(visitor); |
86 } | 64 } |
87 | 65 |
88 DEFINE_TRACE_WRAPPERS(MojoWatcher) { | 66 DEFINE_TRACE_WRAPPERS(MojoWatcher) { |
89 visitor->traceWrappers(m_callback); | 67 visitor->traceWrappers(m_callback); |
90 } | 68 } |
91 | 69 |
92 bool MojoWatcher::hasPendingActivity() const { | 70 bool MojoWatcher::hasPendingActivity() const { |
93 return m_handle.is_valid(); | 71 return m_handle.is_valid(); |
94 } | 72 } |
95 | 73 |
96 void MojoWatcher::contextDestroyed(ExecutionContext*) { | 74 void MojoWatcher::contextDestroyed(ExecutionContext*) { |
97 cancel(); | 75 cancel(); |
98 } | 76 } |
99 | 77 |
78 MojoWatcher::MojoWatcher(ExecutionContext* context, MojoWatchCallback* callback) | |
79 : ContextLifecycleObserver(context), | |
80 m_taskRunner(TaskRunnerHelper::get(TaskType::UnspecedTimer, context)), | |
81 m_callback(this, callback) {} | |
82 | |
83 MojoResult MojoWatcher::watch(mojo::Handle handle, | |
84 const MojoHandleSignals& signalsDict) { | |
85 ::MojoHandleSignals signals = MOJO_HANDLE_SIGNAL_NONE; | |
86 if (signalsDict.readable()) | |
87 signals |= MOJO_HANDLE_SIGNAL_READABLE; | |
88 if (signalsDict.writable()) | |
89 signals |= MOJO_HANDLE_SIGNAL_WRITABLE; | |
90 if (signalsDict.peerClosed()) | |
91 signals |= MOJO_HANDLE_SIGNAL_PEER_CLOSED; | |
92 | |
93 MojoResult result = | |
94 MojoRegisterWatcher(handle.value(), signals, &MojoWatcher::onHandleReady, | |
95 reinterpret_cast<uintptr_t>(this)); | |
96 if (result == MOJO_RESULT_OK) { | |
97 m_handle = handle; | |
98 arm(); | |
99 } | |
100 return result; | |
101 } | |
102 | |
103 void MojoWatcher::arm() { | |
104 // Nothing to do if the watcher is inactive. | |
105 if (!m_handle.is_valid()) | |
106 return; | |
107 | |
108 MojoResult result = | |
109 MojoArmWatcher(m_handle.value(), reinterpret_cast<uintptr_t>(this)); | |
110 if (result == MOJO_RESULT_OK) | |
111 return; | |
112 if (result == MOJO_RESULT_ALREADY_EXISTS) | |
113 result = MOJO_RESULT_OK; | |
114 m_taskRunner->postTask( | |
115 BLINK_FROM_HERE, | |
116 WTF::bind(&MojoWatcher::runReadyCallback, wrapPersistent(this), result)); | |
117 } | |
118 | |
100 void MojoWatcher::onHandleReady(uintptr_t context, | 119 void MojoWatcher::onHandleReady(uintptr_t context, |
101 MojoResult result, | 120 MojoResult result, |
102 MojoHandleSignalsState, | 121 MojoHandleSignalsState, |
103 MojoWatchNotificationFlags) { | 122 MojoWatchNotificationFlags) { |
104 // It is safe to assume the MojoWatcher still exists because this | 123 // It is safe to assume the MojoWatcher still exists because this |
105 // callback will never be run after MojoWatcher destructor, | 124 // callback will never be run after MojoWatcher destructor, |
106 // which cancels the watch. | 125 // which unregisters the watcher. |
107 MojoWatcher* watcher = reinterpret_cast<MojoWatcher*>(context); | 126 MojoWatcher* watcher = reinterpret_cast<MojoWatcher*>(context); |
108 watcher->m_taskRunner->postTask( | 127 watcher->m_taskRunner->postTask( |
109 BLINK_FROM_HERE, | 128 BLINK_FROM_HERE, |
110 crossThreadBind(&MojoWatcher::runReadyCallback, | 129 crossThreadBind(&MojoWatcher::runReadyCallback, |
111 wrapCrossThreadWeakPersistent(watcher), result)); | 130 wrapCrossThreadWeakPersistent(watcher), result)); |
112 } | 131 } |
113 | 132 |
114 void MojoWatcher::runReadyCallback(MojoResult result) { | 133 void MojoWatcher::runReadyCallback(MojoResult result) { |
115 // Ignore callbacks if not watching. | 134 // Ignore callbacks if not watching. |
116 if (!m_handle.is_valid()) | 135 if (!m_handle.is_valid()) |
117 return; | 136 return; |
118 | 137 |
119 // MOJO_RESULT_CANCELLED indicates that the handle has been closed, in which | 138 // 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 | 139 // case watcher has been implicitly cancelled. There is no need to explicitly |
121 // cancel the watch. | 140 // cancel the watcher. |
122 if (result == MOJO_RESULT_CANCELLED) | 141 if (result == MOJO_RESULT_CANCELLED) |
123 m_handle = mojo::Handle(); | 142 m_handle = mojo::Handle(); |
124 | 143 |
125 runWatchCallback(m_callback, this, result); | 144 runWatchCallback(m_callback, this, result); |
145 | |
146 // Rearm the watcher so another notification can fire. | |
147 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
| |
126 } | 148 } |
127 | 149 |
128 } // namespace blink | 150 } // namespace blink |
OLD | NEW |