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