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

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

Issue 2400563002: Adds Mojo IDL. (Closed)
Patch Set: implements messagepipe Created 4 years, 2 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 2016 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/Mojo.h"
6
7 #include "bindings/core/v8/MojoWatchCallback.h"
8 #include "bindings/core/v8/ScriptState.h"
9 #include "core/dom/DOMArrayBuffer.h"
10 #include "core/mojo/MojoCreateMessagePipeOptions.h"
11 #include "core/mojo/MojoCreateMessagePipeResult.h"
12 #include "core/mojo/MojoHandle.h"
13 #include "core/mojo/MojoMessagePipeHandle.h"
14 #include "core/mojo/MojoReadMessageResult.h"
15 #include "public/platform/Platform.h"
16 #include "public/platform/WebTaskRunner.h"
17
18 static const unsigned kInvalidWatchId = 0;
19
20 namespace blink {
21
22 DEFINE_TRACE(Mojo::WatchEntry) {
23 visitor->trace(m_callback);
24 }
25
26 Mojo* Mojo::create() {
27 return new Mojo(
28 Platform::current()->currentThread()->getWebTaskRunner()->clone());
29 }
30
31 Mojo::Mojo(std::unique_ptr<WebTaskRunner> taskRunner)
32 : m_taskRunner(std::move(taskRunner)), m_nextWatchId(1) {}
33
34 Mojo::~Mojo() {}
35
36 unsigned Mojo::watch(ScriptState* scriptState,
37 MojoHandle* handle,
38 MojoHandleSignals signals,
39 MojoWatchCallback* callback) {
40 WatchEntry* entry = new WatchEntry(m_taskRunner.get());
41 std::unique_ptr<MojoHandleWatcher::ReadyCallback> readyCallback =
42 WTF::bind(&Mojo::watchCallback, wrapWeakPersistent(this), m_nextWatchId);
43 if (entry->m_watcher.start(handle->get(), signals,
44 std::move(readyCallback)) != MOJO_RESULT_OK) {
45 return kInvalidWatchId;
46 }
47
48 entry->m_scriptState = scriptState;
49 entry->m_callback = callback;
50 m_watchMap.add(m_nextWatchId, entry);
51 return m_nextWatchId++;
52 }
53
54 void Mojo::cancelWatch(unsigned watchId) {
55 WatchMap::iterator iter = m_watchMap.find(watchId);
56 if (iter != m_watchMap.end()) {
57 iter->value->m_watcher.cancel();
58 m_watchMap.remove(iter);
59 }
60 }
61
62 void Mojo::createMessagePipe(const MojoCreateMessagePipeOptions& optionsDict,
63 MojoCreateMessagePipeResult& resultDict) {
64 ::MojoCreateMessagePipeOptions options = {0};
65 options.struct_size = sizeof(::MojoCreateMessagePipeOptions);
66 options.flags = optionsDict.flags();
67
68 ::MojoHandle handle0, handle1;
69 MojoResult result = MojoCreateMessagePipe(&options, &handle0, &handle1);
70
71 resultDict.setResult(result);
72 if (result == MOJO_RESULT_OK) {
73 resultDict.setHandle0(MojoMessagePipeHandle::create(handle0));
74 resultDict.setHandle1(MojoMessagePipeHandle::create(handle1));
75 }
76 }
77
78 MojoResult Mojo::writeMessage(MojoMessagePipeHandle* pipe,
79 DOMArrayBuffer* buffer,
80 const HeapVector<Member<MojoHandle>>& handles,
81 MojoWriteMessageFlags flags) {
82 // MojoWriteMessage takes ownership of the handles, so release them here.
83 std::vector<::MojoHandle> rawHandles(handles.size());
84 for (size_t i = 0; i < handles.size(); ++i)
85 rawHandles[i] = handles[i]->release();
86
87 return MojoWriteMessage(pipe->get(), buffer->data(), buffer->byteLength(),
88 rawHandles.data(), rawHandles.size(), flags);
89 }
90
91 void Mojo::readMessage(MojoMessagePipeHandle* pipe,
92 MojoReadMessageFlags flags,
93 MojoReadMessageResult& resultDict) {
94 uint32_t numBytes = 0, numHandles = 0;
95 MojoResult result = MojoReadMessage(pipe->get(), nullptr, &numBytes, nullptr,
96 &numHandles, flags);
97 if (result != MOJO_RESULT_RESOURCE_EXHAUSTED) {
98 resultDict.setResult(result);
99 return;
100 }
101
102 DCHECK_GT(numBytes, 0u);
103 DOMArrayBuffer* buffer = DOMArrayBuffer::createUninitialized(numBytes, 1);
104 std::vector<::MojoHandle> rawHandles(numHandles);
105 result = MojoReadMessage(pipe->get(), buffer->data(), &numBytes,
106 rawHandles.data(), &numHandles, flags);
107
108 HeapVector<Member<MojoHandle>> handles(numHandles);
109 for (size_t i = 0; i < numHandles; ++i) {
110 handles[i] = new MojoHandle(rawHandles[i]);
111 }
112
113 resultDict.setResult(result);
114 resultDict.setBuffer(buffer);
115 resultDict.setHandles(handles);
116 }
117
118 DEFINE_TRACE(Mojo) {
119 visitor->trace(m_watchMap);
120 }
121
122 void Mojo::watchCallback(unsigned watchId, MojoResult result) {
123 WatchMap::const_iterator iter = m_watchMap.find(watchId);
124 if (iter != m_watchMap.end()) {
125 const WatchEntry* entry = iter->value;
126 entry->m_callback->call(entry->m_scriptState.get(), nullptr, result);
127
128 if (result != MOJO_RESULT_OK)
129 cancelWatch(watchId);
130 }
131 }
132
133 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698