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