OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/ref_counted.h" | |
6 #include "net/base/io_buffer.h" | |
7 #include "remoting/proto/control.pb.h" | |
8 #include "remoting/proto/event.pb.h" | |
9 #include "remoting/proto/internal.pb.h" | |
10 #include "remoting/protocol/client_message_dispatcher.h" | |
11 #include "remoting/protocol/client_stub.h" | |
12 #include "remoting/protocol/input_stub.h" | |
13 #include "remoting/protocol/message_reader.h" | |
14 #include "remoting/protocol/session.h" | |
15 | |
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> > { | |
awong
2010/12/23 01:15:10
Is this cut/paste code? Can we factor it out?
Alpha Left Google
2010/12/23 02:17:58
Done.
| |
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 { | |
44 namespace protocol { | |
45 | |
46 ClientMessageDispatcher::ClientMessageDispatcher() : client_stub_(NULL) { | |
47 } | |
48 | |
49 ClientMessageDispatcher::~ClientMessageDispatcher() { | |
50 } | |
51 | |
52 bool ClientMessageDispatcher::Initialize( | |
53 protocol::Session* session, ClientStub* client_stub) { | |
54 if (!session || !client_stub || !session->control_channel()) { | |
55 return false; | |
56 } | |
57 | |
58 control_message_reader_.reset(new MessageReader()); | |
59 client_stub_ = client_stub; | |
60 | |
61 control_message_reader_->Init<ControlMessage>( | |
62 session->control_channel(), | |
63 NewCallback(this, &ClientMessageDispatcher::OnControlMessageReceived)); | |
64 return true; | |
65 } | |
66 | |
67 void ClientMessageDispatcher::OnControlMessageReceived( | |
68 ControlMessage* message) { | |
69 scoped_refptr<RefCountedMessage<ControlMessage> > ref_msg = | |
70 new RefCountedMessage<ControlMessage>(message); | |
71 if (message->has_notify_resolution()) { | |
72 client_stub_->NotifyResolution( | |
73 &message->notify_resolution(), NewDeleteTask(ref_msg)); | |
74 } else if (message->has_begin_session_response()) { | |
75 client_stub_->BeginSessionResponse( | |
76 &message->begin_session_response().login_status(), | |
77 NewDeleteTask(ref_msg)); | |
78 } else { | |
79 NOTREACHED() << "Invalid control message received"; | |
80 } | |
81 } | |
82 | |
83 } // namespace protocol | |
84 } // namespace remoting | |
OLD | NEW |