OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef MOJO_EDK_SYSTEM_CHANNEL_MANAGER_H_ | |
6 #define MOJO_EDK_SYSTEM_CHANNEL_MANAGER_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include "base/containers/hash_tables.h" | |
11 #include "base/macros.h" | |
12 #include "base/memory/ref_counted.h" | |
13 #include "base/synchronization/lock.h" | |
14 #include "base/task_runner.h" | |
15 #include "mojo/edk/system/channel.h" | |
16 #include "mojo/edk/system/channel_info.h" | |
17 | |
18 namespace mojo { | |
19 namespace system { | |
20 | |
21 // IDs for |Channel|s managed by a |ChannelManager|. (IDs should be thought of | |
22 // as specific to a given |ChannelManager|.) 0 is never a valid ID. | |
23 // | |
24 // Note: We currently just use the pointer of the |Channel| casted to a | |
25 // |uintptr_t|, but we reserve the right to change this. | |
26 typedef uintptr_t ChannelId; | |
27 | |
28 // This class manages and "owns" |Channel|s (which typically connect to other | |
29 // processes) for a given process. This class is thread-safe. | |
30 class MOJO_SYSTEM_IMPL_EXPORT ChannelManager { | |
31 public: | |
32 ChannelManager(); | |
33 ~ChannelManager(); | |
34 | |
35 // Gets the ID for a given channel. | |
36 // | |
37 // Note: This is currently a static method and thus may be called under | |
38 // |lock_|. If this is ever made non-static (i.e., made specific to a given | |
39 // |ChannelManager|), those call sites may have to changed. | |
40 static ChannelId GetChannelId(const Channel* channel) { | |
41 return reinterpret_cast<ChannelId>(channel); | |
42 } | |
43 | |
44 // Adds |channel| to the set of |Channel|s managed by this |ChannelManager|; | |
45 // |channel_thread_task_runner| should be the task runner for |channel|'s | |
46 // creation (a.k.a. I/O) thread. | |
47 // | |
48 // |channel| should either already be initialized or at least already have a | |
49 // task posted to |channel_thread_task_runner| to initialize it. It should not | |
50 // be managed by any |ChannelManager| yet. | |
51 // | |
52 // Returns the ID for the added channel. | |
53 ChannelId AddChannel( | |
54 scoped_refptr<Channel> channel, | |
55 scoped_refptr<base::TaskRunner> channel_thread_task_runner); | |
56 | |
57 // Shuts down the channel specified by the given ID. It is up to the caller to | |
58 // guarantee that this is only called once per channel (that was added using | |
59 // |AddChannel()|). | |
60 void ShutdownChannel(ChannelId channel_id); | |
61 | |
62 private: | |
63 // Note: |Channel| methods should not be called under |lock_|. | |
64 base::Lock lock_; // Protects the members below. | |
65 | |
66 base::hash_map<ChannelId, ChannelInfo> channel_infos_; | |
67 | |
68 DISALLOW_COPY_AND_ASSIGN(ChannelManager); | |
69 }; | |
70 | |
71 } // namespace system | |
72 } // namespace mojo | |
73 | |
74 #endif // MOJO_EDK_SYSTEM_CHANNEL_MANAGER_H_ | |
OLD | NEW |