OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 REMOTING_PROTOCOL_HOST_MESSAGE_DISPATCHER_H_ | 5 #ifndef REMOTING_PROTOCOL_HOST_MESSAGE_DISPATCHER_H_ |
6 #define REMOTING_PROTOCOL_HOST_MESSAGE_DISPATCHER_H_ | 6 #define REMOTING_PROTOCOL_HOST_MESSAGE_DISPATCHER_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/ref_counted.h" | 9 #include "base/ref_counted.h" |
10 #include "base/scoped_ptr.h" | 10 #include "base/scoped_ptr.h" |
11 | 11 |
12 namespace base { | |
13 class MessageLoopProxy; | |
14 } // namespace base | |
15 | |
16 namespace remoting { | 12 namespace remoting { |
17 | 13 |
18 class ChromotingClientMessage; | 14 class ChromotocolConnection; |
19 class ChromotingConnection; | 15 class ClientControlMessage; |
20 class EventStreamReader; | 16 class ClientEventMessage; |
21 class HostControlMessageHandler; | 17 class HostControlMessageHandler; |
22 class HostEventMessageHandler; | 18 class HostEventMessageHandler; |
| 19 class MessageReader; |
23 | 20 |
24 // A message dispatcher used to listen for messages received in | 21 // A message dispatcher used to listen for messages received in |
25 // ChromotingConnection. It dispatches messages to the corresponding | 22 // ChromotocolConnection. It dispatches messages to the corresponding |
26 // handler. | 23 // handler. |
27 // | 24 // |
28 // Internally it contains an EventStreamReader that decodes data on | 25 // Internally it contains an EventStreamReader that decodes data on |
29 // communications channels into protocol buffer messages. | 26 // communications channels into protocol buffer messages. |
30 // EventStreamReader is registered with ChromotingConnection given to it. | 27 // EventStreamReader is registered with ChromotocolConnection given to it. |
31 // | 28 // |
32 // Object of this class is owned by ChromotingHost to dispatch messages | 29 // Object of this class is owned by ChromotingHost to dispatch messages |
33 // to itself. | 30 // to itself. |
34 class HostMessageDispatcher { | 31 class HostMessageDispatcher : |
| 32 public base::RefCountedThreadSafe<HostMessageDispatcher> { |
35 public: | 33 public: |
36 // Construct a message dispatcher that dispatches messages received | 34 // Construct a message dispatcher. |
37 // in ChromotingConnection. | 35 HostMessageDispatcher(); |
38 HostMessageDispatcher(base::MessageLoopProxy* message_loop_proxy, | |
39 ChromotingConnection* connection, | |
40 HostControlMessageHandler* control_message_handler, | |
41 HostEventMessageHandler* event_message_handler); | |
42 | |
43 virtual ~HostMessageDispatcher(); | 36 virtual ~HostMessageDispatcher(); |
44 | 37 |
| 38 // Initialize the message dispatcher with the given connection and |
| 39 // message handlers. |
| 40 // Return true if initalization was successful. |
| 41 bool Initialize(ChromotocolConnection* connection, |
| 42 HostControlMessageHandler* control_message_handler, |
| 43 HostEventMessageHandler* event_message_handler); |
| 44 |
45 private: | 45 private: |
| 46 // A single protobuf can contain multiple messages that will be handled by |
| 47 // different message handlers. We use this wrapper to ensure that the |
| 48 // protobuf is only deleted after all the handlers have finished executing. |
| 49 template <typename T> |
| 50 class RefCountedMessage : public base::RefCounted<RefCountedMessage<T> > { |
| 51 public: |
| 52 RefCountedMessage(T* message) : message_(message) { } |
| 53 |
| 54 T* message() { return message_.get(); } |
| 55 |
| 56 private: |
| 57 scoped_ptr<T> message_; |
| 58 }; |
| 59 |
46 // This method is called by |control_channel_reader_| when a control | 60 // This method is called by |control_channel_reader_| when a control |
47 // message is received. | 61 // message is received. |
48 void OnControlMessageReceived(ChromotingClientMessage* message); | 62 void OnControlMessageReceived(ClientControlMessage* message); |
49 | 63 |
50 // This method is called by |event_channel_reader_| when a event | 64 // This method is called by |event_channel_reader_| when a event |
51 // message is received. | 65 // message is received. |
52 void OnEventMessageReceived(ChromotingClientMessage* message); | 66 void OnEventMessageReceived(ClientEventMessage* message); |
53 | 67 |
54 // Message loop to dispatch the messages. | 68 // Dummy methods to destroy messages. |
55 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; | 69 template <class T> |
| 70 static void DeleteMessage(scoped_refptr<T> message) { } |
56 | 71 |
57 // EventStreamReader that runs on the control channel. It runs a loop | 72 // MessageReader that runs on the control channel. It runs a loop |
58 // that parses data on the channel and then delegate the message to this | 73 // that parses data on the channel and then delegates the message to this |
59 // class. | 74 // class. |
60 scoped_ptr<EventStreamReader> control_channel_reader_; | 75 scoped_ptr<MessageReader> control_message_reader_; |
61 | 76 |
62 // EventStreamReader that runs on the event channel. | 77 // MessageReader that runs on the event channel. |
63 scoped_ptr<EventStreamReader> event_channel_reader_; | 78 scoped_ptr<MessageReader> event_message_reader_; |
64 | 79 |
65 // Event handlers for control channel and event channel respectively. | 80 // Event handlers for control channel and event channel respectively. |
66 // Method calls to these objects are made on the message loop given. | 81 // Method calls to these objects are made on the message loop given. |
67 scoped_ptr<HostControlMessageHandler> control_message_handler_; | 82 scoped_ptr<HostControlMessageHandler> control_message_handler_; |
68 scoped_ptr<HostEventMessageHandler> event_message_handler_; | 83 scoped_ptr<HostEventMessageHandler> event_message_handler_; |
69 }; | 84 }; |
70 | 85 |
71 } // namespace remoting | 86 } // namespace remoting |
72 | 87 |
73 #endif // REMOTING_PROTOCOL_HOST_MESSAGE_DISPATCHER_H_ | 88 #endif // REMOTING_PROTOCOL_HOST_MESSAGE_DISPATCHER_H_ |
OLD | NEW |