Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_PUBLIC_CPP_BINDINGS_LIB_MULTIPLEX_ROUTER_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_MULTIPLEX_ROUTER_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_MULTIPLEX_ROUTER_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_MULTIPLEX_ROUTER_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <deque> | 10 #include <deque> |
| 11 #include <map> | 11 #include <map> |
| 12 #include <memory> | 12 #include <memory> |
| 13 #include <string> | 13 #include <string> |
| 14 | 14 |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
| 18 #include "base/memory/ref_counted_delete_on_message_loop.h" | |
| 19 #include "base/memory/weak_ptr.h" | 18 #include "base/memory/weak_ptr.h" |
| 20 #include "base/single_thread_task_runner.h" | 19 #include "base/single_thread_task_runner.h" |
| 21 #include "base/synchronization/lock.h" | 20 #include "base/synchronization/lock.h" |
| 22 #include "base/threading/thread_checker.h" | 21 #include "base/threading/thread_checker.h" |
| 22 #include "mojo/public/cpp/bindings/associated_group_controller.h" | |
| 23 #include "mojo/public/cpp/bindings/interface_id.h" | |
| 23 #include "mojo/public/cpp/bindings/lib/connector.h" | 24 #include "mojo/public/cpp/bindings/lib/connector.h" |
| 24 #include "mojo/public/cpp/bindings/lib/interface_id.h" | |
| 25 #include "mojo/public/cpp/bindings/lib/message_header_validator.h" | 25 #include "mojo/public/cpp/bindings/lib/message_header_validator.h" |
| 26 #include "mojo/public/cpp/bindings/lib/pipe_control_message_handler.h" | 26 #include "mojo/public/cpp/bindings/lib/pipe_control_message_handler.h" |
| 27 #include "mojo/public/cpp/bindings/lib/pipe_control_message_handler_delegate.h" | 27 #include "mojo/public/cpp/bindings/lib/pipe_control_message_handler_delegate.h" |
| 28 #include "mojo/public/cpp/bindings/lib/pipe_control_message_proxy.h" | 28 #include "mojo/public/cpp/bindings/lib/pipe_control_message_proxy.h" |
| 29 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" | 29 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" |
| 30 | 30 |
| 31 namespace base { | 31 namespace base { |
| 32 class SingleThreadTaskRunner; | 32 class SingleThreadTaskRunner; |
| 33 } | 33 } |
| 34 | 34 |
| 35 namespace mojo { | 35 namespace mojo { |
| 36 | 36 |
| 37 class AssociatedGroup; | 37 class AssociatedGroup; |
| 38 | 38 |
| 39 namespace internal { | 39 namespace internal { |
| 40 | 40 |
| 41 class InterfaceEndpointClient; | |
| 42 class InterfaceEndpointController; | |
| 43 | |
| 44 // MultiplexRouter supports routing messages for multiple interfaces over a | 41 // MultiplexRouter supports routing messages for multiple interfaces over a |
| 45 // single message pipe. | 42 // single message pipe. |
| 46 // | 43 // |
| 47 // It is created on the thread where the master interface of the message pipe | 44 // It is created on the thread where the master interface of the message pipe |
| 48 // lives. Although it is ref-counted, it is guarateed to be destructed on the | 45 // lives. Although it is ref-counted, it is guarateed to be destructed on the |
| 49 // same thread. | 46 // same thread. |
| 50 // Some public methods are only allowed to be called on the creating thread; | 47 // Some public methods are only allowed to be called on the creating thread; |
| 51 // while the others are safe to call from any threads. Please see the method | 48 // while the others are safe to call from any threads. Please see the method |
| 52 // comments for more details. | 49 // comments for more details. |
| 53 class MultiplexRouter | 50 class MultiplexRouter |
| 54 : public MessageReceiver, | 51 : public MessageReceiver, |
| 55 public base::RefCountedDeleteOnMessageLoop<MultiplexRouter>, | 52 public AssociatedGroupController, |
| 56 public PipeControlMessageHandlerDelegate { | 53 public PipeControlMessageHandlerDelegate { |
| 57 public: | 54 public: |
| 58 // If |set_interface_id_namespace_bit| is true, the interface IDs generated by | 55 // If |set_interface_id_namespace_bit| is true, the interface IDs generated by |
| 59 // this router will have the highest bit set. | 56 // this router will have the highest bit set. |
| 60 MultiplexRouter(bool set_interface_id_namespace_bit, | 57 MultiplexRouter(bool set_interface_id_namespace_bit, |
| 61 ScopedMessagePipeHandle message_pipe, | 58 ScopedMessagePipeHandle message_pipe, |
| 62 scoped_refptr<base::SingleThreadTaskRunner> runner); | 59 scoped_refptr<base::SingleThreadTaskRunner> runner); |
| 63 | 60 |
| 64 // Sets the master interface name for this router. Only used when reporting | 61 // Sets the master interface name for this router. Only used when reporting |
| 65 // message header or control message validation errors. | 62 // message header or control message validation errors. |
| 66 void SetMasterInterfaceName(const std::string& name); | 63 void SetMasterInterfaceName(const std::string& name); |
| 67 | 64 |
| 68 // --------------------------------------------------------------------------- | 65 // --------------------------------------------------------------------------- |
| 69 // The following public methods are safe to call from any threads. | 66 // The following public methods are safe to call from any threads. |
| 70 | 67 |
| 71 // Creates a pair of interface endpoint handles. The method generates a new | 68 // AssociatedGroupController implementation: |
| 72 // interface ID and assigns it to the two handles. |local_endpoint| is used | 69 void CreateEndpointHandlePair( |
| 73 // locally; while |remote_endpoint| is sent over the message pipe. | 70 ScopedInterfaceEndpointHandle* local_endpoint, |
| 74 void CreateEndpointHandlePair(ScopedInterfaceEndpointHandle* local_endpoint, | 71 ScopedInterfaceEndpointHandle* remote_endpoint) override; |
| 75 ScopedInterfaceEndpointHandle* remote_endpoint); | 72 ScopedInterfaceEndpointHandle CreateLocalEndpointHandle( |
| 76 | 73 InterfaceId id) override; |
| 77 // Creates an interface endpoint handle from a given interface ID. The handle | 74 void CloseEndpointHandle(InterfaceId id, bool is_local) override; |
| 78 // is used locally. | |
| 79 // Typically, this method is used to (1) create an endpoint handle for the | |
| 80 // master interface; or (2) create an endpoint handle on receiving an | |
| 81 // interface ID from the message pipe. | |
| 82 ScopedInterfaceEndpointHandle CreateLocalEndpointHandle(InterfaceId id); | |
| 83 | |
| 84 // Closes an interface endpoint handle. | |
| 85 void CloseEndpointHandle(InterfaceId id, bool is_local); | |
| 86 | |
| 87 // Attaches a client to the specified endpoint to send and receive messages. | |
| 88 // The returned object is still owned by the router. It must only be used on | |
| 89 // the same thread as this call, and only before the client is detached using | |
| 90 // DetachEndpointClient(). | |
| 91 InterfaceEndpointController* AttachEndpointClient( | 75 InterfaceEndpointController* AttachEndpointClient( |
| 92 const ScopedInterfaceEndpointHandle& handle, | 76 const ScopedInterfaceEndpointHandle& handle, |
| 93 InterfaceEndpointClient* endpoint_client, | 77 InterfaceEndpointClient* endpoint_client, |
| 94 scoped_refptr<base::SingleThreadTaskRunner> runner); | 78 scoped_refptr<base::SingleThreadTaskRunner> runner) override; |
| 95 | 79 void DetachEndpointClient( |
| 96 // Detaches the client attached to the specified endpoint. It must be called | 80 const ScopedInterfaceEndpointHandle& handle) override; |
| 97 // on the same thread as the corresponding AttachEndpointClient() call. | 81 void RaiseError() override; |
| 98 void DetachEndpointClient(const ScopedInterfaceEndpointHandle& handle); | |
| 99 | |
| 100 // Raises an error on the underlying message pipe. It disconnects the pipe | |
| 101 // and notifies all interfaces running on this pipe. | |
| 102 void RaiseError(); | |
| 103 | |
| 104 std::unique_ptr<AssociatedGroup> CreateAssociatedGroup(); | |
| 105 | |
| 106 static MultiplexRouter* GetRouter(AssociatedGroup* associated_group); | |
| 107 | 82 |
| 108 // --------------------------------------------------------------------------- | 83 // --------------------------------------------------------------------------- |
| 109 // The following public methods are called on the creating thread. | 84 // The following public methods are called on the creating thread. |
| 110 | 85 |
| 111 // Please note that this method shouldn't be called unless it results from an | 86 // Please note that this method shouldn't be called unless it results from an |
| 112 // explicit request of the user of bindings (e.g., the user sets an | 87 // explicit request of the user of bindings (e.g., the user sets an |
| 113 // InterfacePtr to null or closes a Binding). | 88 // InterfacePtr to null or closes a Binding). |
| 114 void CloseMessagePipe(); | 89 void CloseMessagePipe(); |
| 115 | 90 |
| 116 // Extracts the underlying message pipe. | 91 // Extracts the underlying message pipe. |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 150 return connector_.is_valid(); | 125 return connector_.is_valid(); |
| 151 } | 126 } |
| 152 | 127 |
| 153 // TODO(yzshen): consider removing this getter. | 128 // TODO(yzshen): consider removing this getter. |
| 154 MessagePipeHandle handle() const { | 129 MessagePipeHandle handle() const { |
| 155 DCHECK(thread_checker_.CalledOnValidThread()); | 130 DCHECK(thread_checker_.CalledOnValidThread()); |
| 156 return connector_.handle(); | 131 return connector_.handle(); |
| 157 } | 132 } |
| 158 | 133 |
| 159 private: | 134 private: |
| 160 friend class base::RefCountedDeleteOnMessageLoop<MultiplexRouter>; | 135 friend class base::RefCountedDeleteOnMessageLoop<MultiplexRouter>; |
|
yzshen1
2016/06/30 15:34:42
Is this one still necessary?
Ken Rockot(use gerrit already)
2016/06/30 15:57:39
Nope, removed
| |
| 161 friend class base::DeleteHelper<MultiplexRouter>; | |
| 162 | 136 |
| 163 class InterfaceEndpoint; | 137 class InterfaceEndpoint; |
| 164 struct Task; | 138 struct Task; |
| 165 | 139 |
| 166 ~MultiplexRouter() override; | 140 ~MultiplexRouter() override; |
| 167 | 141 |
| 168 // MessageReceiver implementation: | 142 // MessageReceiver implementation: |
| 169 bool Accept(Message* message) override; | 143 bool Accept(Message* message) override; |
| 170 | 144 |
| 171 // PipeControlMessageHandlerDelegate implementation: | 145 // PipeControlMessageHandlerDelegate implementation: |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 259 | 233 |
| 260 bool testing_mode_; | 234 bool testing_mode_; |
| 261 | 235 |
| 262 DISALLOW_COPY_AND_ASSIGN(MultiplexRouter); | 236 DISALLOW_COPY_AND_ASSIGN(MultiplexRouter); |
| 263 }; | 237 }; |
| 264 | 238 |
| 265 } // namespace internal | 239 } // namespace internal |
| 266 } // namespace mojo | 240 } // namespace mojo |
| 267 | 241 |
| 268 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MULTIPLEX_ROUTER_H_ | 242 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MULTIPLEX_ROUTER_H_ |
| OLD | NEW |