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. |channel| should either already be |
| 47 // initialized. It should not be managed by any |ChannelManager| yet. Returns |
| 48 // the ID for the added channel. |
| 49 ChannelId AddChannel( |
| 50 scoped_refptr<Channel> channel, |
| 51 scoped_refptr<base::TaskRunner> channel_thread_task_runner); |
| 52 |
| 53 // Informs the channel manager (and thus channel) that it will be shutdown |
| 54 // soon (by calling |ShutdownChannel()|). Calling this is optional (and may in |
| 55 // fact be called multiple times) but it will suppress certain warnings (e.g., |
| 56 // for the channel being broken) and enable others (if messages are written to |
| 57 // the channel). |
| 58 void WillShutdownChannel(ChannelId channel_id); |
| 59 |
| 60 // Shuts down the channel specified by the given ID. It is up to the caller to |
| 61 // guarantee that this is only called once per channel (that was added using |
| 62 // |AddChannel()|). If called from the chanel's creation thread (i.e., |
| 63 // |base::MessageLoopProxy::current()| is the channel thread's |TaskRunner|), |
| 64 // this will complete synchronously. |
| 65 void ShutdownChannel(ChannelId channel_id); |
| 66 |
| 67 private: |
| 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 |