- add sources.
[platform/framework/web/crosswalk.git] / src / mojo / system / core_impl.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_IMPL_H_
6 #define MOJO_SYSTEM_CORE_IMPL_H_
7
8 #include "base/basictypes.h"
9 #include "base/containers/hash_tables.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/synchronization/lock.h"
12 #include "mojo/public/system/core.h"
13
14 namespace mojo {
15 namespace system {
16
17 class CoreImpl;
18 class Dispatcher;
19
20 namespace test {
21 class CoreTestBase;
22 }
23
24 // |CoreImpl| is a singleton object that implements the Mojo system calls. With
25 // the (obvious) exception of |Init()|, which must be called first (and the call
26 // completed) before making any other calls, all the public methods are
27 // thread-safe.
28 class MOJO_SYSTEM_EXPORT CoreImpl {
29  public:
30   static void Init();
31
32   static CoreImpl* Get() {
33     return singleton_;
34   }
35
36   MojoResult Close(MojoHandle handle);
37
38   MojoResult Wait(MojoHandle handle,
39                   MojoWaitFlags flags,
40                   MojoDeadline deadline);
41
42   MojoResult WaitMany(const MojoHandle* handles,
43                       const MojoWaitFlags* flags,
44                       uint32_t num_handles,
45                       MojoDeadline deadline);
46
47   MojoResult CreateMessagePipe(MojoHandle* handle_0, MojoHandle* handle_1);
48
49   MojoResult WriteMessage(MojoHandle handle,
50                           const void* bytes, uint32_t num_bytes,
51                           const MojoHandle* handles, uint32_t num_handles,
52                           MojoWriteMessageFlags flags);
53
54   MojoResult ReadMessage(MojoHandle handle,
55                          void* bytes, uint32_t* num_bytes,
56                          MojoHandle* handles, uint32_t* num_handles,
57                          MojoReadMessageFlags flags);
58
59  private:
60   friend class test::CoreTestBase;
61
62   typedef base::hash_map<MojoHandle, scoped_refptr<Dispatcher> >
63       HandleTableMap;
64
65   CoreImpl();
66   ~CoreImpl();
67
68   // Looks up the dispatcher for the given handle. Returns null if the handle is
69   // invalid.
70   scoped_refptr<Dispatcher> GetDispatcher(MojoHandle handle);
71
72   // Assigns a new handle for the given dispatcher (which must be valid);
73   // returns |MOJO_HANDLE_INVALID| on failure (due to hitting resource limits).
74   // Must be called under |handle_table_lock_|.
75   MojoHandle AddDispatcherNoLock(scoped_refptr<Dispatcher> dispatcher);
76
77   // Internal implementation of |Wait()| and |WaitMany()|; doesn't do basic
78   // validation of arguments.
79   MojoResult WaitManyInternal(const MojoHandle* handles,
80                               const MojoWaitFlags* flags,
81                               uint32_t num_handles,
82                               MojoDeadline deadline);
83
84   // ---------------------------------------------------------------------------
85
86   static CoreImpl* singleton_;
87
88   // ---------------------------------------------------------------------------
89
90   // TODO(vtl): |handle_table_lock_| should be a reader-writer lock (if only we
91   // had them).
92   base::Lock handle_table_lock_;  // Protects the immediately-following members.
93   HandleTableMap handle_table_;
94   MojoHandle next_handle_;  // Invariant: never |MOJO_HANDLE_INVALID|.
95
96   // ---------------------------------------------------------------------------
97
98   DISALLOW_COPY_AND_ASSIGN(CoreImpl);
99 };
100
101 }  // namespace system
102 }  // namespace mojo
103
104 #endif  // MOJO_SYSTEM_CORE_IMPL_H_