OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef MOJO_PUBLIC_BINDINGS_LIB_ROUTER_H_ | |
6 #define MOJO_PUBLIC_BINDINGS_LIB_ROUTER_H_ | |
7 | |
8 #include <map> | |
9 | |
10 #include "mojo/public/bindings/lib/connector.h" | |
11 #include "mojo/public/bindings/lib/shared_data.h" | |
12 | |
13 namespace mojo { | |
14 namespace internal { | |
15 | |
16 class Router : public MessageReceiver { | |
17 public: | |
18 // The Router takes ownership of |message_pipe|. | |
19 explicit Router(ScopedMessagePipeHandle message_pipe, | |
20 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter()); | |
21 virtual ~Router(); | |
22 | |
23 // Sets the receiver to handle messages read from the message pipe that do | |
24 // not have the kMessageIsResponse flag set. | |
25 void set_incoming_receiver(MessageReceiver* receiver) { | |
26 incoming_receiver_ = receiver; | |
27 } | |
28 | |
29 // Sets the error handler to receive notifications when an error is | |
30 // encountered while reading from the pipe or waiting to read from the pipe. | |
31 void set_error_handler(ErrorHandler* error_handler) { | |
32 connector_.set_error_handler(error_handler); | |
33 } | |
34 | |
35 // Returns true if an error was encountered while reading from the pipe or | |
36 // waiting to read from the pipe. | |
37 bool encountered_error() const { return connector_.encountered_error(); } | |
38 | |
39 // MessageReceiver implementation: | |
40 virtual bool Accept(Message* message) MOJO_OVERRIDE; | |
41 virtual bool AcceptWithResponder(Message* message, MessageReceiver* responder) | |
42 MOJO_OVERRIDE; | |
43 | |
44 private: | |
45 typedef std::map<uint64_t, MessageReceiver*> ResponderMap; | |
46 | |
47 class HandleIncomingMessageThunk : public MessageReceiver { | |
48 public: | |
49 HandleIncomingMessageThunk(Router* router); | |
50 virtual ~HandleIncomingMessageThunk(); | |
51 | |
52 // MessageReceiver implementation: | |
53 virtual bool Accept(Message* message) MOJO_OVERRIDE; | |
54 virtual bool AcceptWithResponder(Message* message, | |
55 MessageReceiver* responder) MOJO_OVERRIDE; | |
56 private: | |
57 Router* router_; | |
58 }; | |
59 | |
60 bool HandleIncomingMessage(Message* message); | |
61 | |
62 Connector connector_; | |
63 SharedData<Router*> weak_self_; | |
64 MessageReceiver* incoming_receiver_; | |
65 HandleIncomingMessageThunk thunk_; | |
66 ResponderMap responders_; | |
67 uint64_t next_request_id_; | |
68 }; | |
69 | |
70 } // namespace internal | |
71 } // namespace mojo | |
72 | |
73 #endif // MOJO_PUBLIC_BINDINGS_LIB_ROUTER_H_ | |
OLD | NEW |