| 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_PUBLIC_CPP_BINDINGS_LIB_ROUTER_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_ROUTER_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_ROUTER_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_ROUTER_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| 11 #include <queue> |
| 11 | 12 |
| 12 #include "base/logging.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 14 #include "base/threading/thread_checker.h" | 16 #include "base/threading/thread_checker.h" |
| 15 #include "mojo/public/cpp/bindings/callback.h" | 17 #include "mojo/public/cpp/bindings/callback.h" |
| 16 #include "mojo/public/cpp/bindings/lib/connector.h" | 18 #include "mojo/public/cpp/bindings/lib/connector.h" |
| 17 #include "mojo/public/cpp/bindings/lib/filter_chain.h" | 19 #include "mojo/public/cpp/bindings/lib/filter_chain.h" |
| 18 #include "mojo/public/cpp/environment/environment.h" | 20 #include "mojo/public/cpp/environment/environment.h" |
| 19 | 21 |
| 20 namespace mojo { | 22 namespace mojo { |
| 21 namespace internal { | 23 namespace internal { |
| 22 | 24 |
| 23 class Router : public MessageReceiverWithResponder { | 25 class Router : public MessageReceiverWithResponder { |
| 24 public: | 26 public: |
| 25 Router(ScopedMessagePipeHandle message_pipe, | 27 Router(ScopedMessagePipeHandle message_pipe, |
| 26 FilterChain filters, | 28 FilterChain filters, |
| 29 bool expects_sync_requests, |
| 27 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()); | 30 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()); |
| 28 ~Router() override; | 31 ~Router() override; |
| 29 | 32 |
| 30 // Sets the receiver to handle messages read from the message pipe that do | 33 // Sets the receiver to handle messages read from the message pipe that do |
| 31 // not have the kMessageIsResponse flag set. | 34 // not have the kMessageIsResponse flag set. |
| 32 void set_incoming_receiver(MessageReceiverWithResponderStatus* receiver) { | 35 void set_incoming_receiver(MessageReceiverWithResponderStatus* receiver) { |
| 33 incoming_receiver_ = receiver; | 36 incoming_receiver_ = receiver; |
| 34 } | 37 } |
| 35 | 38 |
| 36 // Sets the error handler to receive notifications when an error is | 39 // Sets the error handler to receive notifications when an error is |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 // - the object is more tolerant of unrecognized response messages; | 100 // - the object is more tolerant of unrecognized response messages; |
| 98 // - the connector continues working after seeing errors from its incoming | 101 // - the connector continues working after seeing errors from its incoming |
| 99 // receiver. | 102 // receiver. |
| 100 void EnableTestingMode(); | 103 void EnableTestingMode(); |
| 101 | 104 |
| 102 MessagePipeHandle handle() const { return connector_.handle(); } | 105 MessagePipeHandle handle() const { return connector_.handle(); } |
| 103 | 106 |
| 104 // Returns true if this Router has any pending callbacks. | 107 // Returns true if this Router has any pending callbacks. |
| 105 bool has_pending_responders() const { | 108 bool has_pending_responders() const { |
| 106 DCHECK(thread_checker_.CalledOnValidThread()); | 109 DCHECK(thread_checker_.CalledOnValidThread()); |
| 107 return !async_responders_.empty() || !sync_responders_.empty(); | 110 return !async_responders_.empty() || !sync_responses_.empty(); |
| 108 } | 111 } |
| 109 | 112 |
| 110 private: | 113 private: |
| 111 // Maps from the id of a response to the MessageReceiver that handles the | 114 // Maps from the id of a response to the MessageReceiver that handles the |
| 112 // response. | 115 // response. |
| 113 typedef std::map<uint64_t, MessageReceiver*> ResponderMap; | 116 using AsyncResponderMap = std::map<uint64_t, scoped_ptr<MessageReceiver>>; |
| 117 |
| 118 struct SyncResponseInfo { |
| 119 public: |
| 120 explicit SyncResponseInfo(bool* in_response_received); |
| 121 ~SyncResponseInfo(); |
| 122 |
| 123 scoped_ptr<Message> response; |
| 124 |
| 125 // Points to a stack-allocated variable. |
| 126 bool* response_received; |
| 127 |
| 128 private: |
| 129 DISALLOW_COPY_AND_ASSIGN(SyncResponseInfo); |
| 130 }; |
| 131 |
| 132 using SyncResponseMap = std::map<uint64_t, scoped_ptr<SyncResponseInfo>>; |
| 114 | 133 |
| 115 class HandleIncomingMessageThunk : public MessageReceiver { | 134 class HandleIncomingMessageThunk : public MessageReceiver { |
| 116 public: | 135 public: |
| 117 HandleIncomingMessageThunk(Router* router); | 136 HandleIncomingMessageThunk(Router* router); |
| 118 ~HandleIncomingMessageThunk() override; | 137 ~HandleIncomingMessageThunk() override; |
| 119 | 138 |
| 120 // MessageReceiver implementation: | 139 // MessageReceiver implementation: |
| 121 bool Accept(Message* message) override; | 140 bool Accept(Message* message) override; |
| 122 | 141 |
| 123 private: | 142 private: |
| 124 Router* router_; | 143 Router* router_; |
| 125 }; | 144 }; |
| 126 | 145 |
| 127 bool HandleIncomingMessage(Message* message); | 146 bool HandleIncomingMessage(Message* message); |
| 147 void HandleQueuedMessages(); |
| 148 |
| 149 bool HandleMessageInternal(Message* message); |
| 128 | 150 |
| 129 HandleIncomingMessageThunk thunk_; | 151 HandleIncomingMessageThunk thunk_; |
| 130 FilterChain filters_; | 152 FilterChain filters_; |
| 131 Connector connector_; | 153 Connector connector_; |
| 132 MessageReceiverWithResponderStatus* incoming_receiver_; | 154 MessageReceiverWithResponderStatus* incoming_receiver_; |
| 133 ResponderMap async_responders_; | 155 AsyncResponderMap async_responders_; |
| 134 ResponderMap sync_responders_; | 156 SyncResponseMap sync_responses_; |
| 135 uint64_t next_request_id_; | 157 uint64_t next_request_id_; |
| 136 bool testing_mode_; | 158 bool testing_mode_; |
| 159 std::queue<scoped_ptr<Message>> pending_messages_; |
| 160 // Whether a task has been posted to trigger processing of |
| 161 // |pending_messages_|. |
| 162 bool pending_task_for_messages_; |
| 137 base::ThreadChecker thread_checker_; | 163 base::ThreadChecker thread_checker_; |
| 138 base::WeakPtrFactory<Router> weak_factory_; | 164 base::WeakPtrFactory<Router> weak_factory_; |
| 139 }; | 165 }; |
| 140 | 166 |
| 141 } // namespace internal | 167 } // namespace internal |
| 142 } // namespace mojo | 168 } // namespace mojo |
| 143 | 169 |
| 144 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ROUTER_H_ | 170 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ROUTER_H_ |
| OLD | NEW |