OLD | NEW |
| (Empty) |
1 // Copyright 2013 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 #ifndef MOJO_EDK_SYSTEM_CORE_H_ | |
6 #define MOJO_EDK_SYSTEM_CORE_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include "base/callback.h" | |
11 #include "base/macros.h" | |
12 #include "base/memory/ref_counted.h" | |
13 #include "base/memory/scoped_ptr.h" | |
14 #include "base/synchronization/lock.h" | |
15 #include "mojo/edk/system/handle_table.h" | |
16 #include "mojo/edk/system/mapping_table.h" | |
17 #include "mojo/edk/system/memory.h" | |
18 #include "mojo/edk/system/system_impl_export.h" | |
19 #include "mojo/public/c/system/buffer.h" | |
20 #include "mojo/public/c/system/data_pipe.h" | |
21 #include "mojo/public/c/system/message_pipe.h" | |
22 #include "mojo/public/c/system/types.h" | |
23 | |
24 namespace mojo { | |
25 | |
26 namespace embedder { | |
27 class PlatformSupport; | |
28 } | |
29 | |
30 namespace system { | |
31 | |
32 class Dispatcher; | |
33 struct HandleSignalsState; | |
34 | |
35 // |Core| is an object that implements the Mojo system calls. All public methods | |
36 // are thread-safe. | |
37 class MOJO_SYSTEM_IMPL_EXPORT Core { | |
38 public: | |
39 // --------------------------------------------------------------------------- | |
40 | |
41 // These methods are only to be used by via the embedder API (and internally): | |
42 explicit Core(scoped_ptr<embedder::PlatformSupport> platform_support); | |
43 virtual ~Core(); | |
44 | |
45 // Adds |dispatcher| to the handle table, returning the handle for it. Returns | |
46 // |MOJO_HANDLE_INVALID| on failure, namely if the handle table is full. | |
47 MojoHandle AddDispatcher(const scoped_refptr<Dispatcher>& dispatcher); | |
48 | |
49 // Looks up the dispatcher for the given handle. Returns null if the handle is | |
50 // invalid. | |
51 scoped_refptr<Dispatcher> GetDispatcher(MojoHandle handle); | |
52 | |
53 // Watches on the given handle for the given signals, calling |callback| when | |
54 // a signal is satisfied or when all signals become unsatisfiable. |callback| | |
55 // must satisfy stringent requirements -- see |Awakable::Awake()| in | |
56 // awakable.h. In particular, it must not call any Mojo system functions. | |
57 MojoResult AsyncWait(MojoHandle handle, | |
58 MojoHandleSignals signals, | |
59 base::Callback<void(MojoResult)> callback); | |
60 | |
61 embedder::PlatformSupport* platform_support() const { | |
62 return platform_support_.get(); | |
63 } | |
64 | |
65 // --------------------------------------------------------------------------- | |
66 | |
67 // System calls implementation: | |
68 MojoTimeTicks GetTimeTicksNow(); | |
69 MojoResult Close(MojoHandle handle); | |
70 MojoResult Wait(MojoHandle handle, | |
71 MojoHandleSignals signals, | |
72 MojoDeadline deadline, | |
73 UserPointer<MojoHandleSignalsState> signals_state); | |
74 MojoResult WaitMany(UserPointer<const MojoHandle> handles, | |
75 UserPointer<const MojoHandleSignals> signals, | |
76 uint32_t num_handles, | |
77 MojoDeadline deadline, | |
78 UserPointer<uint32_t> result_index, | |
79 UserPointer<MojoHandleSignalsState> signals_states); | |
80 MojoResult CreateMessagePipe( | |
81 UserPointer<const MojoCreateMessagePipeOptions> options, | |
82 UserPointer<MojoHandle> message_pipe_handle0, | |
83 UserPointer<MojoHandle> message_pipe_handle1); | |
84 MojoResult WriteMessage(MojoHandle message_pipe_handle, | |
85 UserPointer<const void> bytes, | |
86 uint32_t num_bytes, | |
87 UserPointer<const MojoHandle> handles, | |
88 uint32_t num_handles, | |
89 MojoWriteMessageFlags flags); | |
90 MojoResult ReadMessage(MojoHandle message_pipe_handle, | |
91 UserPointer<void> bytes, | |
92 UserPointer<uint32_t> num_bytes, | |
93 UserPointer<MojoHandle> handles, | |
94 UserPointer<uint32_t> num_handles, | |
95 MojoReadMessageFlags flags); | |
96 MojoResult CreateDataPipe( | |
97 UserPointer<const MojoCreateDataPipeOptions> options, | |
98 UserPointer<MojoHandle> data_pipe_producer_handle, | |
99 UserPointer<MojoHandle> data_pipe_consumer_handle); | |
100 MojoResult WriteData(MojoHandle data_pipe_producer_handle, | |
101 UserPointer<const void> elements, | |
102 UserPointer<uint32_t> num_bytes, | |
103 MojoWriteDataFlags flags); | |
104 MojoResult BeginWriteData(MojoHandle data_pipe_producer_handle, | |
105 UserPointer<void*> buffer, | |
106 UserPointer<uint32_t> buffer_num_bytes, | |
107 MojoWriteDataFlags flags); | |
108 MojoResult EndWriteData(MojoHandle data_pipe_producer_handle, | |
109 uint32_t num_bytes_written); | |
110 MojoResult ReadData(MojoHandle data_pipe_consumer_handle, | |
111 UserPointer<void> elements, | |
112 UserPointer<uint32_t> num_bytes, | |
113 MojoReadDataFlags flags); | |
114 MojoResult BeginReadData(MojoHandle data_pipe_consumer_handle, | |
115 UserPointer<const void*> buffer, | |
116 UserPointer<uint32_t> buffer_num_bytes, | |
117 MojoReadDataFlags flags); | |
118 MojoResult EndReadData(MojoHandle data_pipe_consumer_handle, | |
119 uint32_t num_bytes_read); | |
120 MojoResult CreateSharedBuffer( | |
121 UserPointer<const MojoCreateSharedBufferOptions> options, | |
122 uint64_t num_bytes, | |
123 UserPointer<MojoHandle> shared_buffer_handle); | |
124 MojoResult DuplicateBufferHandle( | |
125 MojoHandle buffer_handle, | |
126 UserPointer<const MojoDuplicateBufferHandleOptions> options, | |
127 UserPointer<MojoHandle> new_buffer_handle); | |
128 MojoResult MapBuffer(MojoHandle buffer_handle, | |
129 uint64_t offset, | |
130 uint64_t num_bytes, | |
131 UserPointer<void*> buffer, | |
132 MojoMapBufferFlags flags); | |
133 MojoResult UnmapBuffer(UserPointer<void> buffer); | |
134 | |
135 private: | |
136 friend bool internal::ShutdownCheckNoLeaks(Core*); | |
137 | |
138 // Internal implementation of |Wait()| and |WaitMany()|; doesn't do basic | |
139 // validation of arguments. |*result_index| is only set if the result (whether | |
140 // success or failure) applies to a specific handle, so its value should be | |
141 // preinitialized to |static_cast<uint32_t>(-1)|. | |
142 MojoResult WaitManyInternal(const MojoHandle* handles, | |
143 const MojoHandleSignals* signals, | |
144 uint32_t num_handles, | |
145 MojoDeadline deadline, | |
146 uint32_t* result_index, | |
147 HandleSignalsState* signals_states); | |
148 | |
149 const scoped_ptr<embedder::PlatformSupport> platform_support_; | |
150 | |
151 // TODO(vtl): |handle_table_lock_| should be a reader-writer lock (if only we | |
152 // had them). | |
153 base::Lock handle_table_lock_; // Protects |handle_table_|. | |
154 HandleTable handle_table_; | |
155 | |
156 base::Lock mapping_table_lock_; // Protects |mapping_table_|. | |
157 MappingTable mapping_table_; | |
158 | |
159 DISALLOW_COPY_AND_ASSIGN(Core); | |
160 }; | |
161 | |
162 } // namespace system | |
163 } // namespace mojo | |
164 | |
165 #endif // MOJO_EDK_SYSTEM_CORE_H_ | |
OLD | NEW |