OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "mojo/edk/js/core.h" | 5 #include "mojo/edk/js/core.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "gin/arguments.h" | 12 #include "gin/arguments.h" |
13 #include "gin/array_buffer.h" | 13 #include "gin/array_buffer.h" |
14 #include "gin/converter.h" | 14 #include "gin/converter.h" |
15 #include "gin/dictionary.h" | 15 #include "gin/dictionary.h" |
16 #include "gin/function_template.h" | 16 #include "gin/function_template.h" |
17 #include "gin/handle.h" | 17 #include "gin/handle.h" |
18 #include "gin/object_template_builder.h" | 18 #include "gin/object_template_builder.h" |
19 #include "gin/per_isolate_data.h" | 19 #include "gin/per_isolate_data.h" |
20 #include "gin/public/wrapper_info.h" | 20 #include "gin/public/wrapper_info.h" |
21 #include "gin/wrappable.h" | 21 #include "gin/wrappable.h" |
22 #include "mojo/edk/js/drain_data.h" | 22 #include "mojo/edk/js/drain_data.h" |
23 #include "mojo/edk/js/handle.h" | 23 #include "mojo/edk/js/handle.h" |
| 24 #include "mojo/public/cpp/system/wait.h" |
24 | 25 |
25 namespace mojo { | 26 namespace mojo { |
26 namespace edk { | 27 namespace edk { |
27 namespace js { | 28 namespace js { |
28 | 29 |
29 namespace { | 30 namespace { |
30 | 31 |
31 MojoResult CloseHandle(gin::Handle<HandleWrapper> handle) { | 32 MojoResult CloseHandle(gin::Handle<HandleWrapper> handle) { |
32 if (!handle->get().is_valid()) | 33 if (!handle->get().is_valid()) |
33 return MOJO_RESULT_INVALID_ARGUMENT; | 34 return MOJO_RESULT_INVALID_ARGUMENT; |
(...skipping 10 matching lines...) Expand all Loading... |
44 HandleSignalsState state = handle.QuerySignalsState(); | 45 HandleSignalsState state = handle.QuerySignalsState(); |
45 dictionary.Set("result", MOJO_RESULT_OK); | 46 dictionary.Set("result", MOJO_RESULT_OK); |
46 dictionary.Set("satisfiedSignals", state.satisfied_signals); | 47 dictionary.Set("satisfiedSignals", state.satisfied_signals); |
47 dictionary.Set("satisfiableSignals", state.satisfiable_signals); | 48 dictionary.Set("satisfiableSignals", state.satisfiable_signals); |
48 } | 49 } |
49 return dictionary; | 50 return dictionary; |
50 } | 51 } |
51 | 52 |
52 gin::Dictionary WaitHandle(const gin::Arguments& args, | 53 gin::Dictionary WaitHandle(const gin::Arguments& args, |
53 mojo::Handle handle, | 54 mojo::Handle handle, |
54 MojoHandleSignals signals, | 55 MojoHandleSignals signals) { |
55 MojoDeadline deadline) { | |
56 v8::Isolate* isolate = args.isolate(); | 56 v8::Isolate* isolate = args.isolate(); |
57 gin::Dictionary dictionary = gin::Dictionary::CreateEmpty(isolate); | 57 gin::Dictionary dictionary = gin::Dictionary::CreateEmpty(isolate); |
58 | 58 |
59 MojoHandleSignalsState signals_state; | 59 MojoHandleSignalsState signals_state; |
60 MojoResult result = mojo::Wait(handle, signals, deadline, &signals_state); | 60 MojoResult result = Wait(handle, signals, &signals_state); |
61 dictionary.Set("result", result); | 61 dictionary.Set("result", result); |
62 | 62 |
63 mojo::WaitManyResult wmv(result, 0); | 63 if (result != MOJO_RESULT_OK && result != MOJO_RESULT_FAILED_PRECONDITION) { |
64 if (!wmv.AreSignalsStatesValid()) { | |
65 dictionary.Set("signalsState", v8::Null(isolate).As<v8::Value>()); | 64 dictionary.Set("signalsState", v8::Null(isolate).As<v8::Value>()); |
66 } else { | 65 } else { |
67 gin::Dictionary signalsStateDict = gin::Dictionary::CreateEmpty(isolate); | 66 gin::Dictionary signalsStateDict = gin::Dictionary::CreateEmpty(isolate); |
68 signalsStateDict.Set("satisfiedSignals", signals_state.satisfied_signals); | 67 signalsStateDict.Set("satisfiedSignals", signals_state.satisfied_signals); |
69 signalsStateDict.Set("satisfiableSignals", | 68 signalsStateDict.Set("satisfiableSignals", |
70 signals_state.satisfiable_signals); | 69 signals_state.satisfiable_signals); |
71 dictionary.Set("signalsState", signalsStateDict); | 70 dictionary.Set("signalsState", signalsStateDict); |
72 } | 71 } |
73 | 72 |
74 return dictionary; | 73 return dictionary; |
75 } | 74 } |
76 | 75 |
77 gin::Dictionary WaitMany(const gin::Arguments& args, | |
78 const std::vector<mojo::Handle>& handles, | |
79 const std::vector<MojoHandleSignals>& signals, | |
80 MojoDeadline deadline) { | |
81 v8::Isolate* isolate = args.isolate(); | |
82 gin::Dictionary dictionary = gin::Dictionary::CreateEmpty(isolate); | |
83 | |
84 std::vector<MojoHandleSignalsState> signals_states(signals.size()); | |
85 mojo::WaitManyResult wmv = | |
86 mojo::WaitMany(handles, signals, deadline, &signals_states); | |
87 dictionary.Set("result", wmv.result); | |
88 if (wmv.IsIndexValid()) { | |
89 dictionary.Set("index", wmv.index); | |
90 } else { | |
91 dictionary.Set("index", v8::Null(isolate).As<v8::Value>()); | |
92 } | |
93 if (wmv.AreSignalsStatesValid()) { | |
94 std::vector<gin::Dictionary> vec; | |
95 for (size_t i = 0; i < handles.size(); ++i) { | |
96 gin::Dictionary signalsStateDict = gin::Dictionary::CreateEmpty(isolate); | |
97 signalsStateDict.Set("satisfiedSignals", | |
98 signals_states[i].satisfied_signals); | |
99 signalsStateDict.Set("satisfiableSignals", | |
100 signals_states[i].satisfiable_signals); | |
101 vec.push_back(signalsStateDict); | |
102 } | |
103 dictionary.Set("signalsState", vec); | |
104 } else { | |
105 dictionary.Set("signalsState", v8::Null(isolate).As<v8::Value>()); | |
106 } | |
107 | |
108 return dictionary; | |
109 } | |
110 | |
111 gin::Dictionary CreateMessagePipe(const gin::Arguments& args) { | 76 gin::Dictionary CreateMessagePipe(const gin::Arguments& args) { |
112 gin::Dictionary dictionary = gin::Dictionary::CreateEmpty(args.isolate()); | 77 gin::Dictionary dictionary = gin::Dictionary::CreateEmpty(args.isolate()); |
113 dictionary.Set("result", MOJO_RESULT_INVALID_ARGUMENT); | 78 dictionary.Set("result", MOJO_RESULT_INVALID_ARGUMENT); |
114 | 79 |
115 MojoHandle handle0 = MOJO_HANDLE_INVALID; | 80 MojoHandle handle0 = MOJO_HANDLE_INVALID; |
116 MojoHandle handle1 = MOJO_HANDLE_INVALID; | 81 MojoHandle handle1 = MOJO_HANDLE_INVALID; |
117 MojoResult result = MOJO_RESULT_OK; | 82 MojoResult result = MOJO_RESULT_OK; |
118 | 83 |
119 v8::Handle<v8::Value> options_value = args.PeekNext(); | 84 v8::Handle<v8::Value> options_value = args.PeekNext(); |
120 if (options_value.IsEmpty() || options_value->IsNull() || | 85 if (options_value.IsEmpty() || options_value->IsNull() || |
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
397 &g_wrapper_info); | 362 &g_wrapper_info); |
398 | 363 |
399 if (templ.IsEmpty()) { | 364 if (templ.IsEmpty()) { |
400 templ = | 365 templ = |
401 gin::ObjectTemplateBuilder(isolate) | 366 gin::ObjectTemplateBuilder(isolate) |
402 // TODO(mpcomplete): Should these just be methods on the JS Handle | 367 // TODO(mpcomplete): Should these just be methods on the JS Handle |
403 // object? | 368 // object? |
404 .SetMethod("close", CloseHandle) | 369 .SetMethod("close", CloseHandle) |
405 .SetMethod("queryHandleSignalsState", QueryHandleSignalsState) | 370 .SetMethod("queryHandleSignalsState", QueryHandleSignalsState) |
406 .SetMethod("wait", WaitHandle) | 371 .SetMethod("wait", WaitHandle) |
407 .SetMethod("waitMany", WaitMany) | |
408 .SetMethod("createMessagePipe", CreateMessagePipe) | 372 .SetMethod("createMessagePipe", CreateMessagePipe) |
409 .SetMethod("writeMessage", WriteMessage) | 373 .SetMethod("writeMessage", WriteMessage) |
410 .SetMethod("readMessage", ReadMessage) | 374 .SetMethod("readMessage", ReadMessage) |
411 .SetMethod("createDataPipe", CreateDataPipe) | 375 .SetMethod("createDataPipe", CreateDataPipe) |
412 .SetMethod("writeData", WriteData) | 376 .SetMethod("writeData", WriteData) |
413 .SetMethod("readData", ReadData) | 377 .SetMethod("readData", ReadData) |
414 .SetMethod("drainData", DoDrainData) | 378 .SetMethod("drainData", DoDrainData) |
415 .SetMethod("isHandle", IsHandle) | 379 .SetMethod("isHandle", IsHandle) |
416 .SetMethod("createSharedBuffer", CreateSharedBuffer) | 380 .SetMethod("createSharedBuffer", CreateSharedBuffer) |
417 .SetMethod("duplicateBufferHandle", DuplicateBufferHandle) | 381 .SetMethod("duplicateBufferHandle", DuplicateBufferHandle) |
(...skipping 14 matching lines...) Expand all Loading... |
432 MOJO_RESULT_FAILED_PRECONDITION) | 396 MOJO_RESULT_FAILED_PRECONDITION) |
433 .SetValue("RESULT_ABORTED", MOJO_RESULT_ABORTED) | 397 .SetValue("RESULT_ABORTED", MOJO_RESULT_ABORTED) |
434 .SetValue("RESULT_OUT_OF_RANGE", MOJO_RESULT_OUT_OF_RANGE) | 398 .SetValue("RESULT_OUT_OF_RANGE", MOJO_RESULT_OUT_OF_RANGE) |
435 .SetValue("RESULT_UNIMPLEMENTED", MOJO_RESULT_UNIMPLEMENTED) | 399 .SetValue("RESULT_UNIMPLEMENTED", MOJO_RESULT_UNIMPLEMENTED) |
436 .SetValue("RESULT_INTERNAL", MOJO_RESULT_INTERNAL) | 400 .SetValue("RESULT_INTERNAL", MOJO_RESULT_INTERNAL) |
437 .SetValue("RESULT_UNAVAILABLE", MOJO_RESULT_UNAVAILABLE) | 401 .SetValue("RESULT_UNAVAILABLE", MOJO_RESULT_UNAVAILABLE) |
438 .SetValue("RESULT_DATA_LOSS", MOJO_RESULT_DATA_LOSS) | 402 .SetValue("RESULT_DATA_LOSS", MOJO_RESULT_DATA_LOSS) |
439 .SetValue("RESULT_BUSY", MOJO_RESULT_BUSY) | 403 .SetValue("RESULT_BUSY", MOJO_RESULT_BUSY) |
440 .SetValue("RESULT_SHOULD_WAIT", MOJO_RESULT_SHOULD_WAIT) | 404 .SetValue("RESULT_SHOULD_WAIT", MOJO_RESULT_SHOULD_WAIT) |
441 | 405 |
442 .SetValue("DEADLINE_INDEFINITE", MOJO_DEADLINE_INDEFINITE) | |
443 | |
444 .SetValue("HANDLE_SIGNAL_NONE", MOJO_HANDLE_SIGNAL_NONE) | 406 .SetValue("HANDLE_SIGNAL_NONE", MOJO_HANDLE_SIGNAL_NONE) |
445 .SetValue("HANDLE_SIGNAL_READABLE", MOJO_HANDLE_SIGNAL_READABLE) | 407 .SetValue("HANDLE_SIGNAL_READABLE", MOJO_HANDLE_SIGNAL_READABLE) |
446 .SetValue("HANDLE_SIGNAL_WRITABLE", MOJO_HANDLE_SIGNAL_WRITABLE) | 408 .SetValue("HANDLE_SIGNAL_WRITABLE", MOJO_HANDLE_SIGNAL_WRITABLE) |
447 .SetValue("HANDLE_SIGNAL_PEER_CLOSED", | 409 .SetValue("HANDLE_SIGNAL_PEER_CLOSED", |
448 MOJO_HANDLE_SIGNAL_PEER_CLOSED) | 410 MOJO_HANDLE_SIGNAL_PEER_CLOSED) |
449 | 411 |
450 .SetValue("CREATE_MESSAGE_PIPE_OPTIONS_FLAG_NONE", | 412 .SetValue("CREATE_MESSAGE_PIPE_OPTIONS_FLAG_NONE", |
451 MOJO_CREATE_MESSAGE_PIPE_OPTIONS_FLAG_NONE) | 413 MOJO_CREATE_MESSAGE_PIPE_OPTIONS_FLAG_NONE) |
452 | 414 |
453 .SetValue("WRITE_MESSAGE_FLAG_NONE", MOJO_WRITE_MESSAGE_FLAG_NONE) | 415 .SetValue("WRITE_MESSAGE_FLAG_NONE", MOJO_WRITE_MESSAGE_FLAG_NONE) |
(...skipping 29 matching lines...) Expand all Loading... |
483 | 445 |
484 data->SetObjectTemplate(&g_wrapper_info, templ); | 446 data->SetObjectTemplate(&g_wrapper_info, templ); |
485 } | 447 } |
486 | 448 |
487 return templ->NewInstance(); | 449 return templ->NewInstance(); |
488 } | 450 } |
489 | 451 |
490 } // namespace js | 452 } // namespace js |
491 } // namespace edk | 453 } // namespace edk |
492 } // namespace mojo | 454 } // namespace mojo |
OLD | NEW |