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 // Adds |channel| to the set of |Channel|s managed by this |ChannelManager|; | |
36 // |channel_thread_task_runner| should be the task runner for |channel|'s | |
37 // creation (a.k.a. I/O) thread. |channel| should either already be | |
38 // initialized. It should not be managed by any |ChannelManager| yet. Returns | |
39 // the ID for the added channel. | |
40 ChannelId AddChannel( | |
41 scoped_refptr<Channel> channel, | |
42 scoped_refptr<base::TaskRunner> channel_thread_task_runner); | |
43 | |
44 // Informs the channel manager (and thus channel) that it will be shutdown | |
45 // soon (by calling |ShutdownChannel()|). Calling this is optional (and may in | |
46 // fact be called multiple times) but it will suppress certain warnings (e.g., | |
47 // for the channel being broken) and enable others (if messages are written to | |
48 // the channel). | |
49 void WillShutdownChannel(ChannelId channel_id); | |
50 | |
51 // Shuts down the channel specified by the given ID. It is up to the caller to | |
52 // guarantee that this is only called once per channel (that was added using | |
53 // |AddChannel()|). If called from the chanel's creation thread (i.e., | |
54 // |base::MessageLoopProxy::current()| is the channel thread's |TaskRunner|), | |
55 // this will complete synchronously. | |
56 void ShutdownChannel(ChannelId channel_id); | |
57 | |
58 private: | |
59 // Gets the ID for a given channel. | |
60 // | |
61 // Note: This is currently a static method and thus may be called under | |
62 // |lock_|. If this is ever made non-static (i.e., made specific to a given | |
63 // |ChannelManager|), those call sites may have to changed. | |
64 static ChannelId GetChannelId(const Channel* channel) { | |
65 return reinterpret_cast<ChannelId>(channel); | |
66 } | |
67 | |
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); | |
71 | |
72 // Note: |Channel| methods should not be called under |lock_|. | |
73 base::Lock lock_; // Protects the members below. | |
74 | |
75 base::hash_map<ChannelId, ChannelInfo> channel_infos_; | |
76 | |
77 DISALLOW_COPY_AND_ASSIGN(ChannelManager); | |
78 }; | |
79 | |
80 } // namespace system | |
81 } // namespace mojo | |
82 | |
83 #endif // MOJO_EDK_SYSTEM_CHANNEL_MANAGER_H_ | |
OLD | NEW |