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 #ifndef MOJO_EDK_EMBEDDER_EMBEDDER_H_ | 5 #ifndef MOJO_EDK_EMBEDDER_EMBEDDER_H_ |
6 #define MOJO_EDK_EMBEDDER_EMBEDDER_H_ | 6 #define MOJO_EDK_EMBEDDER_EMBEDDER_H_ |
7 | 7 |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/task_runner.h" | 11 #include "base/task_runner.h" |
12 #include "mojo/edk/embedder/channel_info_forward.h" | 12 #include "mojo/edk/embedder/channel_info_forward.h" |
13 #include "mojo/edk/embedder/scoped_platform_handle.h" | 13 #include "mojo/edk/embedder/scoped_platform_handle.h" |
14 #include "mojo/edk/system/system_impl_export.h" | 14 #include "mojo/edk/system/system_impl_export.h" |
15 #include "mojo/public/cpp/system/core.h" | 15 #include "mojo/public/cpp/system/message_pipe.h" |
16 | 16 |
17 namespace mojo { | 17 namespace mojo { |
18 namespace embedder { | 18 namespace embedder { |
19 | 19 |
| 20 struct Configuration; |
20 class PlatformSupport; | 21 class PlatformSupport; |
21 | 22 |
22 // Must be called first to initialize the (global, singleton) system. | 23 // Must be called first, or just after setting configuration parameters, |
| 24 // to initialize the (global, singleton) system. |
23 MOJO_SYSTEM_IMPL_EXPORT void Init(scoped_ptr<PlatformSupport> platform_support); | 25 MOJO_SYSTEM_IMPL_EXPORT void Init(scoped_ptr<PlatformSupport> platform_support); |
24 | 26 |
| 27 // Returns the global configuration. In general there should be no need to |
| 28 // change the configuration, but if you do so this must be done before calling |
| 29 // |Init()|. |
| 30 MOJO_SYSTEM_IMPL_EXPORT Configuration* GetConfiguration(); |
| 31 |
25 // A "channel" is a connection on top of an OS "pipe", on top of which Mojo | 32 // A "channel" is a connection on top of an OS "pipe", on top of which Mojo |
26 // message pipes (etc.) can be multiplexed. It must "live" on some I/O thread. | 33 // message pipes (etc.) can be multiplexed. It must "live" on some I/O thread. |
27 // | 34 // |
28 // There are two "channel" creation/destruction APIs: the synchronous | 35 // There are two channel creation APIs: |CreateChannelOnIOThread()| creates a |
29 // |CreateChannelOnIOThread()|/|DestroyChannelOnIOThread()|, which must be | 36 // channel synchronously and must be called from the I/O thread, while |
30 // called from the I/O thread, and the asynchronous | 37 // |CreateChannel()| is asynchronous and may be called from any thread. |
31 // |CreateChannel()|/|DestroyChannel()|, which may be called from any thread. | 38 // |DestroyChannel()| is used to destroy the channel in either case and may be |
| 39 // called from any thread, but completes synchronously when called from the I/O |
| 40 // thread. |
32 // | 41 // |
33 // Both creation functions have a |platform_handle| argument, which should be an | 42 // Both creation functions have a |platform_handle| argument, which should be an |
34 // OS-dependent handle to one side of a suitable bidirectional OS "pipe" (e.g., | 43 // OS-dependent handle to one side of a suitable bidirectional OS "pipe" (e.g., |
35 // a file descriptor to a socket on POSIX, a handle to a named pipe on Windows); | 44 // a file descriptor to a socket on POSIX, a handle to a named pipe on Windows); |
36 // this "pipe" should be connected and ready for operation (e.g., to be written | 45 // this "pipe" should be connected and ready for operation (e.g., to be written |
37 // to or read from). | 46 // to or read from). |
38 // | 47 // |
39 // Both (synchronously) return a handle to the bootstrap message pipe on the | 48 // Both (synchronously) return a handle to the bootstrap message pipe on the |
40 // channel that was (or is to be) created, or |MOJO_HANDLE_INVALID| on error | 49 // channel that was (or is to be) created, or |MOJO_HANDLE_INVALID| on error |
41 // (but note that this will happen only if, e.g., the handle table is full). | 50 // (but note that this will happen only if, e.g., the handle table is full). |
(...skipping 11 matching lines...) Expand all Loading... |
53 // Both also produce a |ChannelInfo*| (a pointer to an opaque object) -- the | 62 // Both also produce a |ChannelInfo*| (a pointer to an opaque object) -- the |
54 // first synchronously and second asynchronously. | 63 // first synchronously and second asynchronously. |
55 // | 64 // |
56 // The destruction functions are similarly synchronous and asynchronous, | 65 // The destruction functions are similarly synchronous and asynchronous, |
57 // respectively, and take the |ChannelInfo*| produced by the creation functions. | 66 // respectively, and take the |ChannelInfo*| produced by the creation functions. |
58 // | 67 // |
59 // TODO(vtl): Figure out channel teardown. | 68 // TODO(vtl): Figure out channel teardown. |
60 | 69 |
61 // Creates a channel; must only be called from the I/O thread. |platform_handle| | 70 // Creates a channel; must only be called from the I/O thread. |platform_handle| |
62 // should be a handle to a connected OS "pipe". Eventually (even on failure), | 71 // should be a handle to a connected OS "pipe". Eventually (even on failure), |
63 // the "out" value |*channel_info| should be passed to | 72 // the "out" value |*channel_info| should be passed to |DestoryChannel()| to |
64 // |DestroyChannelOnIOThread()| (or |DestoryChannel()|) to tear down the | 73 // tear down the channel. Returns a handle to the bootstrap message pipe. |
65 // channel. Returns a handle to the bootstrap message pipe. | |
66 MOJO_SYSTEM_IMPL_EXPORT ScopedMessagePipeHandle | 74 MOJO_SYSTEM_IMPL_EXPORT ScopedMessagePipeHandle |
67 CreateChannelOnIOThread(ScopedPlatformHandle platform_handle, | 75 CreateChannelOnIOThread(ScopedPlatformHandle platform_handle, |
68 ChannelInfo** channel_info); | 76 ChannelInfo** channel_info); |
69 | 77 |
70 typedef base::Callback<void(ChannelInfo*)> DidCreateChannelCallback; | 78 typedef base::Callback<void(ChannelInfo*)> DidCreateChannelCallback; |
71 // Creates a channel asynchronously; may be called from any thread. | 79 // Creates a channel asynchronously; may be called from any thread. |
72 // |platform_handle| should be a handle to a connected OS "pipe". | 80 // |platform_handle| should be a handle to a connected OS "pipe". |
73 // |io_thread_task_runner| should be the |TaskRunner| for the I/O thread. | 81 // |io_thread_task_runner| should be the |TaskRunner| for the I/O thread. |
74 // |callback| should be the callback to call with the |ChannelInfo*|, which | 82 // |callback| should be the callback to call with the |ChannelInfo*|, which |
75 // should eventually be passed to |DestroyChannel()| (or | 83 // should eventually be passed to |DestroyChannel()| to tear down the channel; |
76 // |DestroyChannelOnIOThread()|) to tear down the channel; the callback will be | 84 // the callback will be called using |callback_thread_task_runner| if that is |
77 // called using |callback_thread_task_runner| if that is non-null, or otherwise | 85 // non-null, or otherwise it will be called using |io_thread_task_runner|. |
78 // it will be called using |io_thread_task_runner|. Returns a handle to the | 86 // Returns a handle to the bootstrap message pipe. |
79 // bootstrap message pipe. | |
80 MOJO_SYSTEM_IMPL_EXPORT ScopedMessagePipeHandle | 87 MOJO_SYSTEM_IMPL_EXPORT ScopedMessagePipeHandle |
81 CreateChannel(ScopedPlatformHandle platform_handle, | 88 CreateChannel(ScopedPlatformHandle platform_handle, |
82 scoped_refptr<base::TaskRunner> io_thread_task_runner, | 89 scoped_refptr<base::TaskRunner> io_thread_task_runner, |
83 DidCreateChannelCallback callback, | 90 DidCreateChannelCallback callback, |
84 scoped_refptr<base::TaskRunner> callback_thread_task_runner); | 91 scoped_refptr<base::TaskRunner> callback_thread_task_runner); |
85 | 92 |
86 // Destroys a channel that was created using either |CreateChannelOnIOThread()| | 93 // Destroys a channel that was created using |CreateChannel()| (or |
87 // or |CreateChannel()|; must only be called from the I/O thread. |channel_info| | 94 // |CreateChannelOnIOThread()|); may be called from any thread. |channel_info| |
88 // should be the "out" value from |CreateChannelOnIOThread()| or the value | 95 // should be the value provided to the callback to |CreateChannel()| (or |
89 // provided to the callback to |CreateChannel()|. | 96 // returned by |CreateChannelOnIOThread()|). If called from the I/O thread, this |
90 MOJO_SYSTEM_IMPL_EXPORT void DestroyChannelOnIOThread( | 97 // will complete synchronously (in particular, it will post no tasks). |
91 ChannelInfo* channel_info); | |
92 | |
93 // Destroys a channel (asynchronously) that was created using |CreateChannel()|; | |
94 // may be called from any thread. |channel_info| should be the value provided to | |
95 // the callback to |CreateChannel()|. | |
96 MOJO_SYSTEM_IMPL_EXPORT void DestroyChannel(ChannelInfo* channel_info); | 98 MOJO_SYSTEM_IMPL_EXPORT void DestroyChannel(ChannelInfo* channel_info); |
97 | 99 |
98 // Inform the channel that it will soon be destroyed (doing so is optional). | 100 // Inform the channel that it will soon be destroyed (doing so is optional). |
99 // This may be called from any thread, but the caller must ensure that this is | 101 // This may be called from any thread, but the caller must ensure that this is |
100 // called before |DestroyChannel()| or |DestroyChannelOnIOThread()|. | 102 // called before |DestroyChannel()|. |
101 MOJO_SYSTEM_IMPL_EXPORT void WillDestroyChannelSoon(ChannelInfo* channel_info); | 103 MOJO_SYSTEM_IMPL_EXPORT void WillDestroyChannelSoon(ChannelInfo* channel_info); |
102 | 104 |
103 // Creates a |MojoHandle| that wraps the given |PlatformHandle| (taking | 105 // Creates a |MojoHandle| that wraps the given |PlatformHandle| (taking |
104 // ownership of it). This |MojoHandle| can then, e.g., be passed through message | 106 // ownership of it). This |MojoHandle| can then, e.g., be passed through message |
105 // pipes. Note: This takes ownership (and thus closes) |platform_handle| even on | 107 // pipes. Note: This takes ownership (and thus closes) |platform_handle| even on |
106 // failure, which is different from what you'd expect from a Mojo API, but it | 108 // failure, which is different from what you'd expect from a Mojo API, but it |
107 // makes for a more convenient embedder API. | 109 // makes for a more convenient embedder API. |
108 MOJO_SYSTEM_IMPL_EXPORT MojoResult | 110 MOJO_SYSTEM_IMPL_EXPORT MojoResult |
109 CreatePlatformHandleWrapper(ScopedPlatformHandle platform_handle, | 111 CreatePlatformHandleWrapper(ScopedPlatformHandle platform_handle, |
110 MojoHandle* platform_handle_wrapper_handle); | 112 MojoHandle* platform_handle_wrapper_handle); |
111 // Retrieves the |PlatformHandle| that was wrapped into a |MojoHandle| (using | 113 // Retrieves the |PlatformHandle| that was wrapped into a |MojoHandle| (using |
112 // |CreatePlatformHandleWrapper()| above). Note that the |MojoHandle| must still | 114 // |CreatePlatformHandleWrapper()| above). Note that the |MojoHandle| must still |
113 // be closed separately. | 115 // be closed separately. |
114 MOJO_SYSTEM_IMPL_EXPORT MojoResult | 116 MOJO_SYSTEM_IMPL_EXPORT MojoResult |
115 PassWrappedPlatformHandle(MojoHandle platform_handle_wrapper_handle, | 117 PassWrappedPlatformHandle(MojoHandle platform_handle_wrapper_handle, |
116 ScopedPlatformHandle* platform_handle); | 118 ScopedPlatformHandle* platform_handle); |
117 | 119 |
118 } // namespace embedder | 120 } // namespace embedder |
119 } // namespace mojo | 121 } // namespace mojo |
120 | 122 |
121 #endif // MOJO_EDK_EMBEDDER_EMBEDDER_H_ | 123 #endif // MOJO_EDK_EMBEDDER_EMBEDDER_H_ |
OLD | NEW |