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_SYSTEM_CHANNEL_MANAGER_H_ | 5 #ifndef MOJO_EDK_SYSTEM_CHANNEL_MANAGER_H_ |
6 #define MOJO_EDK_SYSTEM_CHANNEL_MANAGER_H_ | 6 #define MOJO_EDK_SYSTEM_CHANNEL_MANAGER_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include "base/containers/hash_tables.h" | 10 #include "base/containers/hash_tables.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/synchronization/lock.h" | 13 #include "base/synchronization/lock.h" |
14 #include "mojo/edk/embedder/scoped_platform_handle.h" | 14 #include "base/task_runner.h" |
15 #include "mojo/edk/system/channel.h" | 15 #include "mojo/edk/system/channel.h" |
16 #include "mojo/edk/system/channel_info.h" | 16 #include "mojo/edk/system/channel_info.h" |
17 | 17 |
18 namespace base { | |
19 class TaskRunner; | |
20 } | |
21 | |
22 namespace mojo { | 18 namespace mojo { |
23 | |
24 namespace embedder { | |
25 class PlatformSupport; | |
26 } | |
27 | |
28 namespace system { | 19 namespace system { |
29 | 20 |
30 // IDs for |Channel|s managed by a |ChannelManager|. (IDs should be thought of | 21 // IDs for |Channel|s managed by a |ChannelManager|. (IDs should be thought of |
31 // as specific to a given |ChannelManager|.) 0 is never a valid ID. | 22 // as specific to a given |ChannelManager|.) 0 is never a valid ID. |
32 typedef uint64_t ChannelId; | 23 // |
33 | 24 // Note: We currently just use the pointer of the |Channel| casted to a |
34 const ChannelId kInvalidChannelId = 0; | 25 // |uintptr_t|, but we reserve the right to change this. |
| 26 typedef uintptr_t ChannelId; |
35 | 27 |
36 // This class manages and "owns" |Channel|s (which typically connect to other | 28 // This class manages and "owns" |Channel|s (which typically connect to other |
37 // processes) for a given process. This class is thread-safe, except as | 29 // processes) for a given process. This class is thread-safe. |
38 // specifically noted. | |
39 class MOJO_SYSTEM_IMPL_EXPORT ChannelManager { | 30 class MOJO_SYSTEM_IMPL_EXPORT ChannelManager { |
40 public: | 31 public: |
41 // |*platform_support| must remain alive longer than this object. | 32 ChannelManager(); |
42 explicit ChannelManager(embedder::PlatformSupport* platform_support); | |
43 ~ChannelManager(); | 33 ~ChannelManager(); |
44 | 34 |
45 // Creates a |Channel| and adds it to the set of channels managed by this | 35 // Adds |channel| to the set of |Channel|s managed by this |ChannelManager|; |
46 // |ChannelManager|. |channel_id| should be a valid |ChannelId| (i.e., | 36 // |channel_thread_task_runner| should be the task runner for |channel|'s |
47 // nonzero) not "assigned" to any other |Channel| being managed by this | 37 // creation (a.k.a. I/O) thread. |channel| should either already be |
48 // |ChannelManager|. | 38 // initialized. It should not be managed by any |ChannelManager| yet. Returns |
49 // TODO(vtl): Currently, this should be called on any I/O thread (which will | 39 // the ID for the added channel. |
50 // become the new channel's "channel thread"). Eventually, the channel manager | 40 ChannelId AddChannel( |
51 // will have an assigned I/O thread, on which this must be called. | 41 scoped_refptr<Channel> channel, |
52 // TODO(vtl): Probably this should return a message pipe dispatcher (for the | 42 scoped_refptr<base::TaskRunner> channel_thread_task_runner); |
53 // bootstrap message pipe) instead. | |
54 void CreateChannelOnIOThread( | |
55 ChannelId channel_id, | |
56 embedder::ScopedPlatformHandle platform_handle, | |
57 scoped_refptr<system::ChannelEndpoint> bootstrap_channel_endpoint); | |
58 | |
59 // Like |CreateChannelOnIOThread()|, but may be called from any thread. On | |
60 // completion, will call |callback| ("on" |io_thread_task_runner| if | |
61 // |callback_thread_task_runner| is null else by posting to using | |
62 // |callback_thread_task_runner|). Note: This will always post a task to the | |
63 // I/O thread, even if |io_thread_task_runner| is the task runner for the | |
64 // current thread. | |
65 // TODO(vtl): The |io_thread_task_runner| argument is temporary (we should use | |
66 // the channel manager's I/O thread). | |
67 // TODO(vtl): Probably this should return a message pipe dispatcher (for the | |
68 // bootstrap message pipe) instead. | |
69 void CreateChannel( | |
70 ChannelId channel_id, | |
71 embedder::ScopedPlatformHandle platform_handle, | |
72 scoped_refptr<system::ChannelEndpoint> bootstrap_channel_endpoint, | |
73 scoped_refptr<base::TaskRunner> io_thread_task_runner, | |
74 base::Closure callback, | |
75 scoped_refptr<base::TaskRunner> callback_thread_task_runner); | |
76 | |
77 // Gets the |Channel| with the given ID (which must exist). | |
78 scoped_refptr<Channel> GetChannel(ChannelId channel_id) const; | |
79 | 43 |
80 // Informs the channel manager (and thus channel) that it will be shutdown | 44 // Informs the channel manager (and thus channel) that it will be shutdown |
81 // soon (by calling |ShutdownChannel()|). Calling this is optional (and may in | 45 // soon (by calling |ShutdownChannel()|). Calling this is optional (and may in |
82 // fact be called multiple times) but it will suppress certain warnings (e.g., | 46 // fact be called multiple times) but it will suppress certain warnings (e.g., |
83 // for the channel being broken) and enable others (if messages are written to | 47 // for the channel being broken) and enable others (if messages are written to |
84 // the channel). | 48 // the channel). |
85 void WillShutdownChannel(ChannelId channel_id); | 49 void WillShutdownChannel(ChannelId channel_id); |
86 | 50 |
87 // Shuts down the channel specified by the given ID. It is up to the caller to | 51 // Shuts down the channel specified by the given ID. It is up to the caller to |
88 // guarantee that this is only called once per channel (that was added using | 52 // guarantee that this is only called once per channel (that was added using |
89 // |CreateChannelOnIOThread()|). If called from the channel's creation thread | 53 // |AddChannel()|). If called from the chanel's creation thread (i.e., |
90 // (i.e., |base::MessageLoopProxy::current()| is the channel thread's | 54 // |base::MessageLoopProxy::current()| is the channel thread's |TaskRunner|), |
91 // |TaskRunner|), this will complete synchronously. | 55 // this will complete synchronously. |
92 void ShutdownChannel(ChannelId channel_id); | 56 void ShutdownChannel(ChannelId channel_id); |
93 | 57 |
94 private: | 58 private: |
95 void CreateChannelHelper( | 59 // Gets the ID for a given channel. |
96 ChannelId channel_id, | 60 // |
97 embedder::ScopedPlatformHandle platform_handle, | 61 // Note: This is currently a static method and thus may be called under |
98 scoped_refptr<system::ChannelEndpoint> bootstrap_channel_endpoint, | 62 // |lock_|. If this is ever made non-static (i.e., made specific to a given |
99 base::Closure callback, | 63 // |ChannelManager|), those call sites may have to changed. |
100 scoped_refptr<base::TaskRunner> callback_thread_task_runner); | 64 static ChannelId GetChannelId(const Channel* channel) { |
| 65 return reinterpret_cast<ChannelId>(channel); |
| 66 } |
101 | 67 |
102 embedder::PlatformSupport* const platform_support_; | 68 // Gets the |ChannelInfo| for the channel specified by the given ID. (This |
| 69 // should *not* be called under lock.) |
| 70 ChannelInfo GetChannelInfo(ChannelId channel_id); |
103 | 71 |
104 // Note: |Channel| methods should not be called under |lock_|. | 72 // Note: |Channel| methods should not be called under |lock_|. |
105 mutable base::Lock lock_; // Protects the members below. | 73 base::Lock lock_; // Protects the members below. |
106 | 74 |
107 // TODO(vtl): Once we give the |ChannelManager| one single I/O thread, we can | |
108 // get rid of |ChannelInfo| (and just have ref pointers to |Channel|s). | |
109 base::hash_map<ChannelId, ChannelInfo> channel_infos_; | 75 base::hash_map<ChannelId, ChannelInfo> channel_infos_; |
110 | 76 |
111 DISALLOW_COPY_AND_ASSIGN(ChannelManager); | 77 DISALLOW_COPY_AND_ASSIGN(ChannelManager); |
112 }; | 78 }; |
113 | 79 |
114 } // namespace system | 80 } // namespace system |
115 } // namespace mojo | 81 } // namespace mojo |
116 | 82 |
117 #endif // MOJO_EDK_SYSTEM_CHANNEL_MANAGER_H_ | 83 #endif // MOJO_EDK_SYSTEM_CHANNEL_MANAGER_H_ |
OLD | NEW |