Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1161)

Side by Side Diff: third_party/WebKit/Source/core/mojo/MojoWatcher.cpp

Issue 2400563002: Adds Mojo IDL. (Closed)
Patch Set: fixes typo Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 static const TaskType kMojoTaskType = TaskType::MiscPlatformAPI;
18
19 static void runWatchCallback(MojoWatchCallback* callback,
20 ScriptWrappable* wrappable,
21 MojoResult result) {
22 callback->call(wrappable, result);
23 }
24
25 MojoWatcher* MojoWatcher::create(mojo::Handle handle,
26 const MojoHandleSignals& signalsDict,
27 MojoWatchCallback* callback,
28 ExecutionContext* context) {
29 MojoWatcher* watcher = new MojoWatcher(context, callback);
30 MojoResult result = watcher->watch(handle, signalsDict);
31 // TODO(alokp): Consider raising an exception.
32 // Current clients expect to recieve the initial error returned by MojoWatch
33 // via watch callback.
34 //
35 // Note that the usage of wrapPersistent is intentional so that the intial
36 // error is guaranteed to be reported to the client in case where the given
37 // handle is invalid and garbage collection happens before the callback
38 // is scheduled.
39 if (result != MOJO_RESULT_OK) {
40 context->postTask(
41 kMojoTaskType, BLINK_FROM_HERE,
42 createSameThreadTask(&runWatchCallback, wrapPersistent(callback),
43 nullptr, result));
jbroman 2017/01/27 20:35:16 nit: It seems more consistent, if you're trying to
alokp 2017/01/27 23:54:46 Good idea - done.
44 }
45 return watcher;
46 }
47
48 MojoWatcher::MojoWatcher(ExecutionContext* context, MojoWatchCallback* callback)
49 : ContextLifecycleObserver(context),
50 m_taskRunner(TaskRunnerHelper::get(kMojoTaskType, context)),
51 m_callback(this, callback) {}
52
53 MojoWatcher::~MojoWatcher() {
54 DCHECK(!m_handle.is_valid());
55 }
56
57 MojoResult MojoWatcher::watch(mojo::Handle handle,
58 const MojoHandleSignals& signalsDict) {
59 ::MojoHandleSignals signals = MOJO_HANDLE_SIGNAL_NONE;
60 if (signalsDict.readable())
61 signals |= MOJO_HANDLE_SIGNAL_READABLE;
62 if (signalsDict.writable())
63 signals |= MOJO_HANDLE_SIGNAL_WRITABLE;
64 if (signalsDict.peerClosed())
65 signals |= MOJO_HANDLE_SIGNAL_PEER_CLOSED;
66
67 MojoResult result =
68 MojoWatch(handle.value(), signals, &MojoWatcher::onHandleReady,
69 reinterpret_cast<uintptr_t>(this));
70 if (result == MOJO_RESULT_OK) {
71 m_handle = handle;
72 }
73 return result;
74 }
75
76 MojoResult MojoWatcher::cancel() {
77 if (!m_handle.is_valid())
78 return MOJO_RESULT_OK;
79
80 MojoResult result =
81 MojoCancelWatch(m_handle.value(), reinterpret_cast<uintptr_t>(this));
82 m_handle = mojo::Handle();
83 return result;
84 }
85
86 DEFINE_TRACE(MojoWatcher) {
87 visitor->trace(m_callback);
88 ContextLifecycleObserver::trace(visitor);
89 }
90
91 DEFINE_TRACE_WRAPPERS(MojoWatcher) {
92 visitor->traceWrappers(m_callback);
93 }
94
95 bool MojoWatcher::hasPendingActivity() const {
96 return m_handle.is_valid();
97 }
98
99 void MojoWatcher::contextDestroyed(ExecutionContext*) {
100 cancel();
101 }
102
103 void MojoWatcher::onHandleReady(uintptr_t context,
104 MojoResult result,
105 MojoHandleSignalsState,
106 MojoWatchNotificationFlags) {
107 // It is safe to assume the MojoWatcher still exists because this
108 // callback will never be run after MojoWatcher destructor,
109 // which cancels the watch.
110 MojoWatcher* watcher = reinterpret_cast<MojoWatcher*>(context);
111 watcher->m_taskRunner->postTask(
112 BLINK_FROM_HERE,
113 crossThreadBind(&MojoWatcher::runReadyCallback,
114 wrapCrossThreadWeakPersistent(watcher), result));
115 }
116
117 void MojoWatcher::runReadyCallback(MojoResult result) {
118 // Ignore callbacks if not watching.
119 if (!m_handle.is_valid())
120 return;
121
122 // MOJO_RESULT_CANCELLED indicates that the handle has been closed, in which
123 // case watch has been implicitly cancelled. There is no need to explicitly
124 // cancel the watch.
125 if (result == MOJO_RESULT_CANCELLED)
126 m_handle = mojo::Handle();
127
128 runWatchCallback(m_callback, this, result);
129 }
130
131 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698