| 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 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 73 NewCallback(this, &HostMessageDispatcher::OnControlMessageReceived)); | 47 NewCallback(this, &HostMessageDispatcher::OnControlMessageReceived)); |
| 74 return true; | 48 return true; |
| 75 } | 49 } |
| 76 | 50 |
| 77 void HostMessageDispatcher::OnControlMessageReceived(ControlMessage* message) { | 51 void HostMessageDispatcher::OnControlMessageReceived(ControlMessage* message) { |
| 78 scoped_refptr<RefCountedMessage<ControlMessage> > ref_msg = | 52 scoped_refptr<RefCountedMessage<ControlMessage> > ref_msg = |
| 79 new RefCountedMessage<ControlMessage>(message); | 53 new RefCountedMessage<ControlMessage>(message); |
| 80 if (message->has_suggest_resolution()) { | 54 if (message->has_suggest_resolution()) { |
| 81 host_stub_->SuggestResolution( | 55 host_stub_->SuggestResolution( |
| 82 &message->suggest_resolution(), NewDeleteTask(ref_msg)); | 56 &message->suggest_resolution(), NewDeleteTask(ref_msg)); |
| 57 } else if (message->has_begin_session_request()) { |
| 58 host_stub_->BeginSessionRequest( |
| 59 &message->begin_session_request().credentials(), |
| 60 NewDeleteTask(ref_msg)); |
| 61 } else { |
| 62 NOTREACHED() << "Invalid control message received"; |
| 83 } | 63 } |
| 84 } | 64 } |
| 85 | 65 |
| 86 void HostMessageDispatcher::OnEventMessageReceived( | 66 void HostMessageDispatcher::OnEventMessageReceived( |
| 87 EventMessage* message) { | 67 EventMessage* message) { |
| 88 scoped_refptr<RefCountedMessage<EventMessage> > ref_msg = | 68 scoped_refptr<RefCountedMessage<EventMessage> > ref_msg = |
| 89 new RefCountedMessage<EventMessage>(message); | 69 new RefCountedMessage<EventMessage>(message); |
| 90 for (int i = 0; i < message->event_size(); ++i) { | 70 for (int i = 0; i < message->event_size(); ++i) { |
| 91 if (message->event(i).has_key()) { | 71 if (message->event(i).has_key()) { |
| 92 input_stub_->InjectKeyEvent( | 72 input_stub_->InjectKeyEvent( |
| 93 &message->event(i).key(), NewDeleteTask(ref_msg)); | 73 &message->event(i).key(), NewDeleteTask(ref_msg)); |
| 94 } | 74 } |
| 95 if (message->event(i).has_mouse()) { | 75 if (message->event(i).has_mouse()) { |
| 96 input_stub_->InjectMouseEvent( | 76 input_stub_->InjectMouseEvent( |
| 97 &message->event(i).mouse(), NewDeleteTask(ref_msg)); | 77 &message->event(i).mouse(), NewDeleteTask(ref_msg)); |
| 98 } | 78 } |
| 99 } | 79 } |
| 100 } | 80 } |
| 101 | 81 |
| 102 } // namespace protocol | 82 } // namespace protocol |
| 103 } // namespace remoting | 83 } // namespace remoting |
| OLD | NEW |