OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "remoting/protocol/connection_to_client.h" | 5 #include "remoting/protocol/connection_to_client.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop_proxy.h" |
8 #include "google/protobuf/message.h" | 9 #include "google/protobuf/message.h" |
9 #include "net/base/io_buffer.h" | 10 #include "net/base/io_buffer.h" |
10 #include "remoting/protocol/client_control_sender.h" | 11 #include "remoting/protocol/client_control_sender.h" |
11 #include "remoting/protocol/host_message_dispatcher.h" | 12 #include "remoting/protocol/host_message_dispatcher.h" |
12 #include "remoting/protocol/host_stub.h" | 13 #include "remoting/protocol/host_stub.h" |
13 #include "remoting/protocol/input_stub.h" | 14 #include "remoting/protocol/input_stub.h" |
14 | 15 |
15 // TODO(hclam): Remove this header once MessageDispatcher is used. | 16 // TODO(hclam): Remove this header once MessageDispatcher is used. |
16 #include "remoting/base/compound_buffer.h" | 17 #include "remoting/base/compound_buffer.h" |
17 | 18 |
18 namespace remoting { | 19 namespace remoting { |
19 namespace protocol { | 20 namespace protocol { |
20 | 21 |
21 // Determine how many update streams we should count to find the size of | 22 // Determine how many update streams we should count to find the size of |
22 // average update stream. | 23 // average update stream. |
23 static const size_t kAverageUpdateStream = 10; | 24 static const size_t kAverageUpdateStream = 10; |
24 | 25 |
25 ConnectionToClient::ConnectionToClient(MessageLoop* message_loop, | 26 ConnectionToClient::ConnectionToClient(base::MessageLoopProxy* message_loop, |
26 EventHandler* handler) | 27 EventHandler* handler) |
27 : loop_(message_loop), | 28 : message_loop_(message_loop), |
28 handler_(handler), | 29 handler_(handler), |
29 host_stub_(NULL), | 30 host_stub_(NULL), |
30 input_stub_(NULL), | 31 input_stub_(NULL), |
31 control_connected_(false), | 32 control_connected_(false), |
32 input_connected_(false), | 33 input_connected_(false), |
33 video_connected_(false) { | 34 video_connected_(false) { |
34 DCHECK(loop_); | 35 DCHECK(message_loop_); |
35 DCHECK(handler_); | 36 DCHECK(handler_); |
36 } | 37 } |
37 | 38 |
38 ConnectionToClient::~ConnectionToClient() { | 39 ConnectionToClient::~ConnectionToClient() { |
39 // TODO(hclam): When we shut down the viewer we may have to close the | 40 // TODO(hclam): When we shut down the viewer we may have to close the |
40 // connection. | 41 // connection. |
41 } | 42 } |
42 | 43 |
43 void ConnectionToClient::Init(protocol::Session* session) { | 44 void ConnectionToClient::Init(protocol::Session* session) { |
44 DCHECK_EQ(loop_, MessageLoop::current()); | 45 DCHECK(message_loop_->BelongsToCurrentThread()); |
45 session_.reset(session); | 46 session_.reset(session); |
46 session_->SetStateChangeCallback( | 47 session_->SetStateChangeCallback( |
47 NewCallback(this, &ConnectionToClient::OnSessionStateChange)); | 48 NewCallback(this, &ConnectionToClient::OnSessionStateChange)); |
48 } | 49 } |
49 | 50 |
50 protocol::Session* ConnectionToClient::session() { | 51 protocol::Session* ConnectionToClient::session() { |
51 return session_.get(); | 52 return session_.get(); |
52 } | 53 } |
53 | 54 |
54 void ConnectionToClient::Disconnect() { | 55 void ConnectionToClient::Disconnect() { |
55 // This method can be called from main thread so perform threading switching. | 56 // This method can be called from main thread so perform threading switching. |
56 if (MessageLoop::current() != loop_) { | 57 if (!message_loop_->BelongsToCurrentThread()) { |
57 loop_->PostTask( | 58 message_loop_->PostTask( |
58 FROM_HERE, | 59 FROM_HERE, |
59 NewRunnableMethod(this, &ConnectionToClient::Disconnect)); | 60 NewRunnableMethod(this, &ConnectionToClient::Disconnect)); |
60 return; | 61 return; |
61 } | 62 } |
62 | 63 |
63 CloseChannels(); | 64 CloseChannels(); |
64 | 65 |
65 // If there is a session then release it, causing it to close. | 66 // If there is a session then release it, causing it to close. |
66 if (session_.get()) | 67 if (session_.get()) |
67 session_.reset(); | 68 session_.reset(); |
(...skipping 14 matching lines...) Expand all Loading... |
82 | 83 |
83 void ConnectionToClient::set_host_stub(protocol::HostStub* host_stub) { | 84 void ConnectionToClient::set_host_stub(protocol::HostStub* host_stub) { |
84 host_stub_ = host_stub; | 85 host_stub_ = host_stub; |
85 } | 86 } |
86 | 87 |
87 void ConnectionToClient::set_input_stub(protocol::InputStub* input_stub) { | 88 void ConnectionToClient::set_input_stub(protocol::InputStub* input_stub) { |
88 input_stub_ = input_stub; | 89 input_stub_ = input_stub; |
89 } | 90 } |
90 | 91 |
91 void ConnectionToClient::OnSessionStateChange(protocol::Session::State state) { | 92 void ConnectionToClient::OnSessionStateChange(protocol::Session::State state) { |
92 DCHECK_EQ(loop_, MessageLoop::current()); | 93 DCHECK(message_loop_->BelongsToCurrentThread()); |
93 | 94 |
94 DCHECK(handler_); | 95 DCHECK(handler_); |
95 switch(state) { | 96 switch(state) { |
96 case protocol::Session::CONNECTING: | 97 case protocol::Session::CONNECTING: |
97 // Don't care about this message. | 98 // Don't care about this message. |
98 break; | 99 break; |
99 | 100 |
100 case protocol::Session::CONNECTED: | 101 case protocol::Session::CONNECTED: |
101 video_writer_.reset(VideoWriter::Create(session_->config())); | 102 video_writer_.reset( |
| 103 VideoWriter::Create(message_loop_, session_->config())); |
102 video_writer_->Init( | 104 video_writer_->Init( |
103 session_.get(), base::Bind(&ConnectionToClient::OnVideoInitialized, | 105 session_.get(), base::Bind(&ConnectionToClient::OnVideoInitialized, |
104 base::Unretained(this))); | 106 base::Unretained(this))); |
105 break; | 107 break; |
106 | 108 |
107 case protocol::Session::CONNECTED_CHANNELS: | 109 case protocol::Session::CONNECTED_CHANNELS: |
108 client_control_sender_.reset( | 110 client_control_sender_.reset( |
109 new ClientControlSender(session_->control_channel())); | 111 new ClientControlSender(message_loop_, session_->control_channel())); |
110 dispatcher_.reset(new HostMessageDispatcher()); | 112 dispatcher_.reset(new HostMessageDispatcher()); |
111 dispatcher_->Initialize(this, host_stub_, input_stub_); | 113 dispatcher_->Initialize(this, host_stub_, input_stub_); |
112 | 114 |
113 control_connected_ = true; | 115 control_connected_ = true; |
114 input_connected_ = true; | 116 input_connected_ = true; |
115 NotifyIfChannelsReady(); | 117 NotifyIfChannelsReady(); |
116 break; | 118 break; |
117 | 119 |
118 case protocol::Session::CLOSED: | 120 case protocol::Session::CLOSED: |
119 CloseChannels(); | 121 CloseChannels(); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 | 155 |
154 void ConnectionToClient::CloseChannels() { | 156 void ConnectionToClient::CloseChannels() { |
155 if (video_writer_.get()) | 157 if (video_writer_.get()) |
156 video_writer_->Close(); | 158 video_writer_->Close(); |
157 if (client_control_sender_.get()) | 159 if (client_control_sender_.get()) |
158 client_control_sender_->Close(); | 160 client_control_sender_->Close(); |
159 } | 161 } |
160 | 162 |
161 } // namespace protocol | 163 } // namespace protocol |
162 } // namespace remoting | 164 } // namespace remoting |
OLD | NEW |