blob: fb705c4038933c603f5445819f843362931a240e [file] [log] [blame]
Nick Brayda897d62015-04-08 16:28:55 -07001// Copyright 2015 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
Viet-Trung Luu604aea92015-10-22 12:50:28 -07005#include "base/logging.h"
Nick Brayda897d62015-04-08 16:28:55 -07006#include "mojo/edk/embedder/embedder_internal.h"
7#include "mojo/edk/system/core.h"
8#include "mojo/edk/system/dispatcher.h"
Viet-Trung Luuc1ffb9f2016-05-05 09:14:11 -07009#include "mojo/edk/system/handle.h"
Viet-Trung Luu6a1f1a82015-10-30 15:18:01 -070010#include "mojo/edk/util/ref_ptr.h"
Nick Brayda897d62015-04-08 16:28:55 -070011#include "mojo/public/c/system/buffer.h"
12#include "mojo/public/c/system/data_pipe.h"
Viet-Trung Luua2e2b6d2016-03-11 12:39:43 -080013#include "mojo/public/c/system/handle.h"
Nick Brayda897d62015-04-08 16:28:55 -070014#include "mojo/public/c/system/message_pipe.h"
Viet-Trung Luua2e2b6d2016-03-11 12:39:43 -080015#include "mojo/public/c/system/result.h"
16#include "mojo/public/c/system/time.h"
17#include "mojo/public/c/system/wait.h"
Nick Brayda897d62015-04-08 16:28:55 -070018#include "mojo/public/platform/native/system_impl_private.h"
19
20using mojo::embedder::internal::g_core;
21using mojo::system::Core;
22using mojo::system::Dispatcher;
Viet-Trung Luu453f3462016-05-09 16:11:49 -070023using mojo::system::Handle;
Nick Brayda897d62015-04-08 16:28:55 -070024using mojo::system::MakeUserPointer;
Viet-Trung Luu6a1f1a82015-10-30 15:18:01 -070025using mojo::util::RefPtr;
Nick Brayda897d62015-04-08 16:28:55 -070026
27// Definitions of the system functions, but with an explicit parameter for the
28// core object rather than using the default singleton. Also includes functions
29// for manipulating core objects.
30extern "C" {
31
32MojoSystemImpl MojoSystemImplGetDefaultImpl() {
33 return static_cast<MojoSystemImpl>(g_core);
34}
35
36MojoSystemImpl MojoSystemImplCreateImpl() {
37 Core* created_core = new Core(g_core->platform_support());
38 return static_cast<MojoSystemImpl>(created_core);
39}
40
41MojoResult MojoSystemImplTransferHandle(MojoSystemImpl from_system,
42 MojoHandle handle,
43 MojoSystemImpl to_system,
44 MojoHandle* result_handle) {
45 Core* from_core = static_cast<Core*>(from_system);
46 if (from_core == nullptr)
47 return MOJO_RESULT_INVALID_ARGUMENT;
48
49 if (handle == MOJO_HANDLE_INVALID)
50 return MOJO_RESULT_INVALID_ARGUMENT;
51
52 Core* to_core = static_cast<Core*>(to_system);
53 if (to_core == nullptr)
54 return MOJO_RESULT_INVALID_ARGUMENT;
55
56 if (result_handle == nullptr)
57 return MOJO_RESULT_INVALID_ARGUMENT;
58
Viet-Trung Luu453f3462016-05-09 16:11:49 -070059 Handle h;
60 MojoResult result = from_core->GetAndRemoveHandle(handle, &h);
Nick Brayda897d62015-04-08 16:28:55 -070061 if (result != MOJO_RESULT_OK)
62 return result;
63
Viet-Trung Luu453f3462016-05-09 16:11:49 -070064 MojoHandle created_handle =
65 to_core->AddHandle(Handle(h.dispatcher.Clone(), h.rights));
Nick Brayda897d62015-04-08 16:28:55 -070066 if (created_handle == MOJO_HANDLE_INVALID) {
67 // The handle has been lost, unfortunately. There's no guarentee we can put
68 // it back where it came from, or get the original ID back. Holding locks
69 // for multiple cores risks deadlock, so that isn't a solution. This case
70 // should not happen for reasonable uses of this API, however.
Viet-Trung Luu453f3462016-05-09 16:11:49 -070071 // TODO(vtl): This behaviour is pretty crappy. This can be fixed by marking
72 // the original handle as busy and only removing it on success, though
73 // that'd require some work.
Nick Brayda897d62015-04-08 16:28:55 -070074 LOG(ERROR) << "Could not transfer handle";
Viet-Trung Luu453f3462016-05-09 16:11:49 -070075 h.dispatcher->Close();
Nick Brayda897d62015-04-08 16:28:55 -070076 return MOJO_RESULT_RESOURCE_EXHAUSTED;
77 }
78
79 MakeUserPointer(result_handle).Put(created_handle);
80 return MOJO_RESULT_OK;
81}
82
83MojoTimeTicks MojoSystemImplGetTimeTicksNow(MojoSystemImpl system) {
84 mojo::system::Core* core = static_cast<mojo::system::Core*>(system);
85 DCHECK(core);
86 return core->GetTimeTicksNow();
87}
88
89MojoResult MojoSystemImplClose(MojoSystemImpl system, MojoHandle handle) {
90 mojo::system::Core* core = static_cast<mojo::system::Core*>(system);
91 DCHECK(core);
92 return core->Close(handle);
93}
94
Viet-Trung Luue5ace232016-05-19 14:54:48 -070095MojoResult MojoSystemImplGetRights(MojoSystemImpl system,
96 MojoHandle handle,
97 MojoHandleRights* rights) {
98 mojo::system::Core* core = static_cast<mojo::system::Core*>(system);
99 DCHECK(core);
100 return core->GetRights(handle, MakeUserPointer(rights));
101}
102
Viet-Trung Luuef5cd662016-05-23 12:14:01 -0700103MojoResult MojoSystemImplDuplicateHandleWithReducedRights(
104 MojoSystemImpl system,
105 MojoHandle handle,
106 MojoHandleRights rights_to_remove,
107 MojoHandle* new_handle) {
108 mojo::system::Core* core = static_cast<mojo::system::Core*>(system);
109 DCHECK(core);
110 return core->DuplicateHandleWithReducedRights(handle, rights_to_remove,
111 MakeUserPointer(new_handle));
112}
113
114MojoResult MojoSystemImplDuplicateHandle(MojoSystemImpl system,
115 MojoHandle handle,
116 MojoHandle* new_handle) {
117 mojo::system::Core* core = static_cast<mojo::system::Core*>(system);
118 DCHECK(core);
119 return core->DuplicateHandleWithReducedRights(handle, MOJO_HANDLE_RIGHT_NONE,
120 MakeUserPointer(new_handle));
121}
122
Nick Brayda897d62015-04-08 16:28:55 -0700123MojoResult MojoSystemImplWait(MojoSystemImpl system,
124 MojoHandle handle,
125 MojoHandleSignals signals,
126 MojoDeadline deadline,
127 MojoHandleSignalsState* signals_state) {
128 mojo::system::Core* core = static_cast<mojo::system::Core*>(system);
129 DCHECK(core);
130 return core->Wait(handle, signals, deadline, MakeUserPointer(signals_state));
131}
132
133MojoResult MojoSystemImplWaitMany(MojoSystemImpl system,
134 const MojoHandle* handles,
135 const MojoHandleSignals* signals,
136 uint32_t num_handles,
137 MojoDeadline deadline,
138 uint32_t* result_index,
139 MojoHandleSignalsState* signals_states) {
140 mojo::system::Core* core = static_cast<mojo::system::Core*>(system);
141 DCHECK(core);
142 return core->WaitMany(MakeUserPointer(handles), MakeUserPointer(signals),
143 num_handles, deadline, MakeUserPointer(result_index),
144 MakeUserPointer(signals_states));
145}
146
147MojoResult MojoSystemImplCreateMessagePipe(
148 MojoSystemImpl system,
149 const MojoCreateMessagePipeOptions* options,
150 MojoHandle* message_pipe_handle0,
151 MojoHandle* message_pipe_handle1) {
152 mojo::system::Core* core = static_cast<mojo::system::Core*>(system);
153 DCHECK(core);
154 return core->CreateMessagePipe(MakeUserPointer(options),
155 MakeUserPointer(message_pipe_handle0),
156 MakeUserPointer(message_pipe_handle1));
157}
158
159MojoResult MojoSystemImplWriteMessage(MojoSystemImpl system,
160 MojoHandle message_pipe_handle,
161 const void* bytes,
162 uint32_t num_bytes,
163 const MojoHandle* handles,
164 uint32_t num_handles,
165 MojoWriteMessageFlags flags) {
166 mojo::system::Core* core = static_cast<mojo::system::Core*>(system);
167 DCHECK(core);
168 return core->WriteMessage(message_pipe_handle, MakeUserPointer(bytes),
169 num_bytes, MakeUserPointer(handles), num_handles,
170 flags);
171}
172
173MojoResult MojoSystemImplReadMessage(MojoSystemImpl system,
174 MojoHandle message_pipe_handle,
175 void* bytes,
176 uint32_t* num_bytes,
177 MojoHandle* handles,
178 uint32_t* num_handles,
179 MojoReadMessageFlags flags) {
180 mojo::system::Core* core = static_cast<mojo::system::Core*>(system);
181 DCHECK(core);
182 return core->ReadMessage(message_pipe_handle, MakeUserPointer(bytes),
183 MakeUserPointer(num_bytes), MakeUserPointer(handles),
184 MakeUserPointer(num_handles), flags);
185}
186
187MojoResult MojoSystemImplCreateDataPipe(
188 MojoSystemImpl system,
189 const MojoCreateDataPipeOptions* options,
190 MojoHandle* data_pipe_producer_handle,
191 MojoHandle* data_pipe_consumer_handle) {
192 mojo::system::Core* core = static_cast<mojo::system::Core*>(system);
193 DCHECK(core);
194 return core->CreateDataPipe(MakeUserPointer(options),
195 MakeUserPointer(data_pipe_producer_handle),
196 MakeUserPointer(data_pipe_consumer_handle));
197}
198
Viet-Trung Luud1825d22016-04-13 17:13:45 -0700199MojoResult MojoSystemImplSetDataPipeProducerOptions(
200 MojoSystemImpl system,
201 MojoHandle data_pipe_producer_handle,
202 const struct MojoDataPipeProducerOptions* options) {
203 mojo::system::Core* core = static_cast<mojo::system::Core*>(system);
204 DCHECK(core);
205 return core->SetDataPipeProducerOptions(data_pipe_producer_handle,
206 MakeUserPointer(options));
207}
208
209MojoResult MojoSystemImplGetDataPipeProducerOptions(
210 MojoSystemImpl system,
211 MojoHandle data_pipe_producer_handle,
212 struct MojoDataPipeProducerOptions* options,
213 uint32_t options_num_bytes) {
214 mojo::system::Core* core = static_cast<mojo::system::Core*>(system);
215 DCHECK(core);
216 return core->GetDataPipeProducerOptions(
217 data_pipe_producer_handle, MakeUserPointer(options), options_num_bytes);
218}
219
Nick Brayda897d62015-04-08 16:28:55 -0700220MojoResult MojoSystemImplWriteData(MojoSystemImpl system,
221 MojoHandle data_pipe_producer_handle,
222 const void* elements,
223 uint32_t* num_elements,
224 MojoWriteDataFlags flags) {
225 mojo::system::Core* core = static_cast<mojo::system::Core*>(system);
226 DCHECK(core);
227 return core->WriteData(data_pipe_producer_handle, MakeUserPointer(elements),
228 MakeUserPointer(num_elements), flags);
229}
230
231MojoResult MojoSystemImplBeginWriteData(MojoSystemImpl system,
232 MojoHandle data_pipe_producer_handle,
233 void** buffer,
234 uint32_t* buffer_num_elements,
235 MojoWriteDataFlags flags) {
236 mojo::system::Core* core = static_cast<mojo::system::Core*>(system);
237 DCHECK(core);
238 return core->BeginWriteData(data_pipe_producer_handle,
239 MakeUserPointer(buffer),
240 MakeUserPointer(buffer_num_elements), flags);
241}
242
243MojoResult MojoSystemImplEndWriteData(MojoSystemImpl system,
244 MojoHandle data_pipe_producer_handle,
245 uint32_t num_elements_written) {
246 mojo::system::Core* core = static_cast<mojo::system::Core*>(system);
247 DCHECK(core);
248 return core->EndWriteData(data_pipe_producer_handle, num_elements_written);
249}
250
Viet-Trung Luu58ec2b72016-04-05 13:43:25 -0700251MojoResult MojoSystemImplSetDataPipeConsumerOptions(
252 MojoSystemImpl system,
253 MojoHandle data_pipe_consumer_handle,
254 const struct MojoDataPipeConsumerOptions* options) {
255 mojo::system::Core* core = static_cast<mojo::system::Core*>(system);
256 DCHECK(core);
257 return core->SetDataPipeConsumerOptions(data_pipe_consumer_handle,
258 MakeUserPointer(options));
259}
260
261MojoResult MojoSystemImplGetDataPipeConsumerOptions(
262 MojoSystemImpl system,
263 MojoHandle data_pipe_consumer_handle,
264 struct MojoDataPipeConsumerOptions* options,
265 uint32_t options_num_bytes) {
266 mojo::system::Core* core = static_cast<mojo::system::Core*>(system);
267 DCHECK(core);
268 return core->GetDataPipeConsumerOptions(
269 data_pipe_consumer_handle, MakeUserPointer(options), options_num_bytes);
270}
271
Nick Brayda897d62015-04-08 16:28:55 -0700272MojoResult MojoSystemImplReadData(MojoSystemImpl system,
273 MojoHandle data_pipe_consumer_handle,
274 void* elements,
275 uint32_t* num_elements,
276 MojoReadDataFlags flags) {
277 mojo::system::Core* core = static_cast<mojo::system::Core*>(system);
278 DCHECK(core);
279 return core->ReadData(data_pipe_consumer_handle, MakeUserPointer(elements),
280 MakeUserPointer(num_elements), flags);
281}
282
283MojoResult MojoSystemImplBeginReadData(MojoSystemImpl system,
284 MojoHandle data_pipe_consumer_handle,
285 const void** buffer,
286 uint32_t* buffer_num_elements,
287 MojoReadDataFlags flags) {
288 mojo::system::Core* core = static_cast<mojo::system::Core*>(system);
289 DCHECK(core);
290 return core->BeginReadData(data_pipe_consumer_handle, MakeUserPointer(buffer),
291 MakeUserPointer(buffer_num_elements), flags);
292}
293
294MojoResult MojoSystemImplEndReadData(MojoSystemImpl system,
295 MojoHandle data_pipe_consumer_handle,
296 uint32_t num_elements_read) {
297 mojo::system::Core* core = static_cast<mojo::system::Core*>(system);
298 DCHECK(core);
299 return core->EndReadData(data_pipe_consumer_handle, num_elements_read);
300}
301
302MojoResult MojoSystemImplCreateSharedBuffer(
303 MojoSystemImpl system,
304 const MojoCreateSharedBufferOptions* options,
305 uint64_t num_bytes,
306 MojoHandle* shared_buffer_handle) {
307 mojo::system::Core* core = static_cast<mojo::system::Core*>(system);
308 DCHECK(core);
309 return core->CreateSharedBuffer(MakeUserPointer(options), num_bytes,
310 MakeUserPointer(shared_buffer_handle));
311}
312
313MojoResult MojoSystemImplDuplicateBufferHandle(
314 MojoSystemImpl system,
315 MojoHandle buffer_handle,
316 const MojoDuplicateBufferHandleOptions* options,
317 MojoHandle* new_buffer_handle) {
318 mojo::system::Core* core = static_cast<mojo::system::Core*>(system);
319 DCHECK(core);
320 return core->DuplicateBufferHandle(buffer_handle, MakeUserPointer(options),
321 MakeUserPointer(new_buffer_handle));
322}
323
Viet-Trung Luu9beb4f72016-03-09 15:58:11 -0800324MojoResult MojoSystemImplGetBufferInformation(MojoSystemImpl system,
325 MojoHandle buffer_handle,
326 MojoBufferInformation* info,
327 uint32_t info_num_bytes) {
328 mojo::system::Core* core = static_cast<mojo::system::Core*>(system);
329 DCHECK(core);
330 return core->GetBufferInformation(buffer_handle, MakeUserPointer(info),
331 info_num_bytes);
332}
333
Nick Brayda897d62015-04-08 16:28:55 -0700334MojoResult MojoSystemImplMapBuffer(MojoSystemImpl system,
335 MojoHandle buffer_handle,
336 uint64_t offset,
337 uint64_t num_bytes,
338 void** buffer,
339 MojoMapBufferFlags flags) {
340 mojo::system::Core* core = static_cast<mojo::system::Core*>(system);
341 DCHECK(core);
342 return core->MapBuffer(buffer_handle, offset, num_bytes,
343 MakeUserPointer(buffer), flags);
344}
345
346MojoResult MojoSystemImplUnmapBuffer(MojoSystemImpl system, void* buffer) {
347 mojo::system::Core* core = static_cast<mojo::system::Core*>(system);
348 DCHECK(core);
349 return core->UnmapBuffer(MakeUserPointer(buffer));
350}
351
352} // extern "C"