| 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_INTERFACE_ENDPOINT_CLIENT_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_ENDPOINT_CLIENT_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_ENDPOINT_CLIENT_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_ENDPOINT_CLIENT_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| 11 | 11 |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "base/threading/thread_checker.h" | 16 #include "base/threading/thread_checker.h" |
| 17 #include "mojo/public/cpp/bindings/callback.h" | 17 #include "mojo/public/cpp/bindings/callback.h" |
| 18 #include "mojo/public/cpp/bindings/lib/scoped_interface_endpoint_handle.h" | 18 #include "mojo/public/cpp/bindings/lib/scoped_interface_endpoint_handle.h" |
| 19 #include "mojo/public/cpp/bindings/message.h" | 19 #include "mojo/public/cpp/bindings/message.h" |
| 20 #include "mojo/public/cpp/bindings/message_filter.h" | 20 #include "mojo/public/cpp/bindings/message_filter.h" |
| 21 | 21 |
| 22 namespace mojo { | 22 namespace mojo { |
| 23 | 23 |
| 24 class AssociatedGroup; | 24 class AssociatedGroup; |
| 25 | 25 |
| 26 namespace internal { | 26 namespace internal { |
| 27 | 27 |
| 28 class MultiplexRouter; | 28 class MultiplexRouter; |
| 29 class InterfaceEndpointController; |
| 29 | 30 |
| 30 // InterfaceEndpointClient handles message sending and receiving of an interface | 31 // InterfaceEndpointClient handles message sending and receiving of an interface |
| 31 // endpoint, either the implementation side or the client side. | 32 // endpoint, either the implementation side or the client side. |
| 32 // It should only be accessed and destructed on the creating thread. | 33 // It should only be accessed and destructed on the creating thread. |
| 33 class InterfaceEndpointClient : public MessageReceiverWithResponder { | 34 class InterfaceEndpointClient : public MessageReceiverWithResponder { |
| 34 public: | 35 public: |
| 35 // |receiver| is okay to be null. If it is not null, it must outlive this | 36 // |receiver| is okay to be null. If it is not null, it must outlive this |
| 36 // object. | 37 // object. |
| 37 InterfaceEndpointClient(ScopedInterfaceEndpointHandle handle, | 38 InterfaceEndpointClient(ScopedInterfaceEndpointHandle handle, |
| 38 MessageReceiverWithResponderStatus* receiver, | 39 MessageReceiverWithResponderStatus* receiver, |
| 39 scoped_ptr<MessageFilter> payload_validator); | 40 scoped_ptr<MessageFilter> payload_validator, |
| 41 bool expect_sync_requests); |
| 40 ~InterfaceEndpointClient() override; | 42 ~InterfaceEndpointClient() override; |
| 41 | 43 |
| 42 // Sets the error handler to receive notifications when an error is | 44 // Sets the error handler to receive notifications when an error is |
| 43 // encountered. | 45 // encountered. |
| 44 void set_connection_error_handler(const Closure& error_handler) { | 46 void set_connection_error_handler(const Closure& error_handler) { |
| 45 DCHECK(thread_checker_.CalledOnValidThread()); | 47 DCHECK(thread_checker_.CalledOnValidThread()); |
| 46 error_handler_ = error_handler; | 48 error_handler_ = error_handler; |
| 47 } | 49 } |
| 48 | 50 |
| 49 // Returns true if an error was encountered. | 51 // Returns true if an error was encountered. |
| 50 bool encountered_error() const { | 52 bool encountered_error() const { |
| 51 DCHECK(thread_checker_.CalledOnValidThread()); | 53 DCHECK(thread_checker_.CalledOnValidThread()); |
| 52 return encountered_error_; | 54 return encountered_error_; |
| 53 } | 55 } |
| 54 | 56 |
| 55 // Returns true if this endpoint has any pending callbacks. | 57 // Returns true if this endpoint has any pending callbacks. |
| 56 bool has_pending_responders() const { | 58 bool has_pending_responders() const { |
| 57 DCHECK(thread_checker_.CalledOnValidThread()); | 59 DCHECK(thread_checker_.CalledOnValidThread()); |
| 58 return !responders_.empty(); | 60 return !async_responders_.empty() || !sync_responses_.empty(); |
| 59 } | 61 } |
| 60 | 62 |
| 61 MultiplexRouter* router() const { return handle_.router(); } | 63 MultiplexRouter* router() const { return handle_.router(); } |
| 62 AssociatedGroup* associated_group(); | 64 AssociatedGroup* associated_group(); |
| 63 uint32_t interface_id() const; | 65 uint32_t interface_id() const; |
| 64 | 66 |
| 65 // After this call the object is in an invalid state and shouldn't be reused. | 67 // After this call the object is in an invalid state and shouldn't be reused. |
| 66 ScopedInterfaceEndpointHandle PassHandle(); | 68 ScopedInterfaceEndpointHandle PassHandle(); |
| 67 | 69 |
| 68 // Raises an error on the underlying message pipe. It disconnects the pipe | 70 // Raises an error on the underlying message pipe. It disconnects the pipe |
| 69 // and notifies all interfaces running on this pipe. | 71 // and notifies all interfaces running on this pipe. |
| 70 void RaiseError(); | 72 void RaiseError(); |
| 71 | 73 |
| 72 // MessageReceiverWithResponder implementation: | 74 // MessageReceiverWithResponder implementation: |
| 73 bool Accept(Message* message) override; | 75 bool Accept(Message* message) override; |
| 74 bool AcceptWithResponder(Message* message, | 76 bool AcceptWithResponder(Message* message, |
| 75 MessageReceiver* responder) override; | 77 MessageReceiver* responder) override; |
| 76 | 78 |
| 77 // The following methods are called by the router. They must be called | 79 // The following methods are called by the router. They must be called |
| 78 // outside of the router's lock. | 80 // outside of the router's lock. |
| 79 | 81 |
| 80 // NOTE: |message| must have passed message header validation. | 82 // NOTE: |message| must have passed message header validation. |
| 81 bool HandleIncomingMessage(Message* message); | 83 bool HandleIncomingMessage(Message* message); |
| 82 void NotifyError(); | 84 void NotifyError(); |
| 83 | 85 |
| 84 private: | 86 private: |
| 85 using ResponderMap = std::map<uint64_t, MessageReceiver*>; | 87 // Maps from the id of a response to the MessageReceiver that handles the |
| 88 // response. |
| 89 using AsyncResponderMap = std::map<uint64_t, scoped_ptr<MessageReceiver>>; |
| 90 |
| 91 struct SyncResponseInfo { |
| 92 public: |
| 93 explicit SyncResponseInfo(bool* in_response_received); |
| 94 ~SyncResponseInfo(); |
| 95 |
| 96 scoped_ptr<Message> response; |
| 97 |
| 98 // Points to a stack-allocated variable. |
| 99 bool* response_received; |
| 100 |
| 101 private: |
| 102 DISALLOW_COPY_AND_ASSIGN(SyncResponseInfo); |
| 103 }; |
| 104 |
| 105 using SyncResponseMap = std::map<uint64_t, scoped_ptr<SyncResponseInfo>>; |
| 86 | 106 |
| 87 // Used as the sink for |payload_validator_| and forwards messages to | 107 // Used as the sink for |payload_validator_| and forwards messages to |
| 88 // HandleValidatedMessage(). | 108 // HandleValidatedMessage(). |
| 89 class HandleIncomingMessageThunk : public MessageReceiver { | 109 class HandleIncomingMessageThunk : public MessageReceiver { |
| 90 public: | 110 public: |
| 91 explicit HandleIncomingMessageThunk(InterfaceEndpointClient* owner); | 111 explicit HandleIncomingMessageThunk(InterfaceEndpointClient* owner); |
| 92 ~HandleIncomingMessageThunk() override; | 112 ~HandleIncomingMessageThunk() override; |
| 93 | 113 |
| 94 // MessageReceiver implementation: | 114 // MessageReceiver implementation: |
| 95 bool Accept(Message* message) override; | 115 bool Accept(Message* message) override; |
| 96 | 116 |
| 97 private: | 117 private: |
| 98 InterfaceEndpointClient* const owner_; | 118 InterfaceEndpointClient* const owner_; |
| 99 | 119 |
| 100 DISALLOW_COPY_AND_ASSIGN(HandleIncomingMessageThunk); | 120 DISALLOW_COPY_AND_ASSIGN(HandleIncomingMessageThunk); |
| 101 }; | 121 }; |
| 102 | 122 |
| 103 bool HandleValidatedMessage(Message* message); | 123 bool HandleValidatedMessage(Message* message); |
| 104 | 124 |
| 105 ScopedInterfaceEndpointHandle handle_; | 125 ScopedInterfaceEndpointHandle handle_; |
| 106 scoped_ptr<AssociatedGroup> associated_group_; | 126 scoped_ptr<AssociatedGroup> associated_group_; |
| 127 InterfaceEndpointController* controller_; |
| 107 | 128 |
| 108 MessageReceiverWithResponderStatus* const incoming_receiver_; | 129 MessageReceiverWithResponderStatus* const incoming_receiver_; |
| 109 scoped_ptr<MessageFilter> payload_validator_; | 130 scoped_ptr<MessageFilter> payload_validator_; |
| 110 HandleIncomingMessageThunk thunk_; | 131 HandleIncomingMessageThunk thunk_; |
| 111 | 132 |
| 112 // Maps from the ID of a response to the MessageReceiver that handles the | 133 AsyncResponderMap async_responders_; |
| 113 // response. | 134 SyncResponseMap sync_responses_; |
| 114 ResponderMap responders_; | 135 |
| 115 uint64_t next_request_id_; | 136 uint64_t next_request_id_; |
| 116 | 137 |
| 117 Closure error_handler_; | 138 Closure error_handler_; |
| 118 bool encountered_error_; | 139 bool encountered_error_; |
| 119 | 140 |
| 120 base::ThreadChecker thread_checker_; | 141 base::ThreadChecker thread_checker_; |
| 121 | 142 |
| 122 base::WeakPtrFactory<InterfaceEndpointClient> weak_ptr_factory_; | 143 base::WeakPtrFactory<InterfaceEndpointClient> weak_ptr_factory_; |
| 123 | 144 |
| 124 DISALLOW_COPY_AND_ASSIGN(InterfaceEndpointClient); | 145 DISALLOW_COPY_AND_ASSIGN(InterfaceEndpointClient); |
| 125 }; | 146 }; |
| 126 | 147 |
| 127 } // namespace internal | 148 } // namespace internal |
| 128 } // namespace mojo | 149 } // namespace mojo |
| 129 | 150 |
| 130 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_ENDPOINT_CLIENT_H_ | 151 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_ENDPOINT_CLIENT_H_ |
| OLD | NEW |