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 #include "base/ref_counted.h" | 5 #include "base/ref_counted.h" |
6 #include "net/base/io_buffer.h" | 6 #include "net/base/io_buffer.h" |
7 #include "remoting/proto/control.pb.h" | 7 #include "remoting/proto/control.pb.h" |
8 #include "remoting/proto/event.pb.h" | 8 #include "remoting/proto/event.pb.h" |
9 #include "remoting/proto/internal.pb.h" | 9 #include "remoting/proto/internal.pb.h" |
10 #include "remoting/protocol/host_message_dispatcher.h" | 10 #include "remoting/protocol/host_message_dispatcher.h" |
11 #include "remoting/protocol/host_stub.h" | 11 #include "remoting/protocol/host_stub.h" |
12 #include "remoting/protocol/input_stub.h" | 12 #include "remoting/protocol/input_stub.h" |
13 #include "remoting/protocol/message_reader.h" | 13 #include "remoting/protocol/message_reader.h" |
| 14 #include "remoting/protocol/ref_counted_message.h" |
14 #include "remoting/protocol/session.h" | 15 #include "remoting/protocol/session.h" |
15 | 16 |
16 namespace { | |
17 | |
18 // A single protobuf can contain multiple messages that will be handled by | |
19 // different message handlers. We use this wrapper to ensure that the | |
20 // protobuf is only deleted after all the handlers have finished executing. | |
21 template <typename T> | |
22 class RefCountedMessage : public base::RefCounted<RefCountedMessage<T> > { | |
23 public: | |
24 RefCountedMessage(T* message) : message_(message) { } | |
25 | |
26 T* message() { return message_.get(); } | |
27 | |
28 private: | |
29 scoped_ptr<T> message_; | |
30 }; | |
31 | |
32 // Dummy methods to destroy messages. | |
33 template <class T> | |
34 static void DeleteMessage(scoped_refptr<T> message) { } | |
35 | |
36 template <class T> | |
37 static Task* NewDeleteTask(scoped_refptr<T> message) { | |
38 return NewRunnableFunction(&DeleteMessage<T>, message); | |
39 } | |
40 | |
41 } // namespace | |
42 | |
43 namespace remoting { | 17 namespace remoting { |
44 namespace protocol { | 18 namespace protocol { |
45 | 19 |
46 HostMessageDispatcher::HostMessageDispatcher() : | 20 HostMessageDispatcher::HostMessageDispatcher() : |
47 host_stub_(NULL), | 21 host_stub_(NULL), |
48 input_stub_(NULL) { | 22 input_stub_(NULL) { |
49 } | 23 } |
50 | 24 |
51 HostMessageDispatcher::~HostMessageDispatcher() { | 25 HostMessageDispatcher::~HostMessageDispatcher() { |
52 } | 26 } |
53 | 27 |
54 bool HostMessageDispatcher::Initialize( | 28 void HostMessageDispatcher::Initialize( |
55 protocol::Session* session, | 29 protocol::Session* session, |
56 HostStub* host_stub, InputStub* input_stub) { | 30 HostStub* host_stub, InputStub* input_stub) { |
57 if (!session || !host_stub || !input_stub || | 31 if (!session || !host_stub || !input_stub || |
58 !session->event_channel() || !session->control_channel()) { | 32 !session->event_channel() || !session->control_channel()) { |
59 return false; | 33 return; |
60 } | 34 } |
61 | 35 |
62 control_message_reader_.reset(new MessageReader()); | 36 control_message_reader_.reset(new MessageReader()); |
63 event_message_reader_.reset(new MessageReader()); | 37 event_message_reader_.reset(new MessageReader()); |
64 host_stub_ = host_stub; | 38 host_stub_ = host_stub; |
65 input_stub_ = input_stub; | 39 input_stub_ = input_stub; |
66 | 40 |
67 // Initialize the readers on the sockets provided by channels. | 41 // Initialize the readers on the sockets provided by channels. |
68 event_message_reader_->Init<EventMessage>( | 42 event_message_reader_->Init<EventMessage>( |
69 session->event_channel(), | 43 session->event_channel(), |
70 NewCallback(this, &HostMessageDispatcher::OnEventMessageReceived)); | 44 NewCallback(this, &HostMessageDispatcher::OnEventMessageReceived)); |
71 control_message_reader_->Init<ControlMessage>( | 45 control_message_reader_->Init<ControlMessage>( |
72 session->control_channel(), | 46 session->control_channel(), |
73 NewCallback(this, &HostMessageDispatcher::OnControlMessageReceived)); | 47 NewCallback(this, &HostMessageDispatcher::OnControlMessageReceived)); |
74 return true; | |
75 } | 48 } |
76 | 49 |
77 void HostMessageDispatcher::OnControlMessageReceived(ControlMessage* message) { | 50 void HostMessageDispatcher::OnControlMessageReceived(ControlMessage* message) { |
78 scoped_refptr<RefCountedMessage<ControlMessage> > ref_msg = | 51 scoped_refptr<RefCountedMessage<ControlMessage> > ref_msg = |
79 new RefCountedMessage<ControlMessage>(message); | 52 new RefCountedMessage<ControlMessage>(message); |
80 if (message->has_suggest_resolution()) { | 53 if (message->has_suggest_resolution()) { |
81 host_stub_->SuggestResolution( | 54 host_stub_->SuggestResolution( |
82 &message->suggest_resolution(), NewDeleteTask(ref_msg)); | 55 &message->suggest_resolution(), NewDeleteTask(ref_msg)); |
| 56 } else if (message->has_begin_session_request()) { |
| 57 host_stub_->BeginSessionRequest( |
| 58 &message->begin_session_request().credentials(), |
| 59 NewDeleteTask(ref_msg)); |
| 60 } else { |
| 61 NOTREACHED() << "Invalid control message received"; |
83 } | 62 } |
84 } | 63 } |
85 | 64 |
86 void HostMessageDispatcher::OnEventMessageReceived( | 65 void HostMessageDispatcher::OnEventMessageReceived( |
87 EventMessage* message) { | 66 EventMessage* message) { |
88 scoped_refptr<RefCountedMessage<EventMessage> > ref_msg = | 67 scoped_refptr<RefCountedMessage<EventMessage> > ref_msg = |
89 new RefCountedMessage<EventMessage>(message); | 68 new RefCountedMessage<EventMessage>(message); |
90 for (int i = 0; i < message->event_size(); ++i) { | 69 for (int i = 0; i < message->event_size(); ++i) { |
91 if (message->event(i).has_key()) { | 70 if (message->event(i).has_key()) { |
92 input_stub_->InjectKeyEvent( | 71 input_stub_->InjectKeyEvent( |
93 &message->event(i).key(), NewDeleteTask(ref_msg)); | 72 &message->event(i).key(), NewDeleteTask(ref_msg)); |
94 } | 73 } |
95 if (message->event(i).has_mouse()) { | 74 if (message->event(i).has_mouse()) { |
96 input_stub_->InjectMouseEvent( | 75 input_stub_->InjectMouseEvent( |
97 &message->event(i).mouse(), NewDeleteTask(ref_msg)); | 76 &message->event(i).mouse(), NewDeleteTask(ref_msg)); |
98 } | 77 } |
99 } | 78 } |
100 } | 79 } |
101 | 80 |
102 } // namespace protocol | 81 } // namespace protocol |
103 } // namespace remoting | 82 } // namespace remoting |
OLD | NEW |