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