1 // Copyright 2015 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.
5 #include "remoting/test/app_remoting_connection_helper.h"
7 #include "base/json/json_reader.h"
8 #include "base/logging.h"
9 #include "base/run_loop.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "base/timer/timer.h"
12 #include "base/values.h"
13 #include "remoting/protocol/host_stub.h"
14 #include "remoting/test/app_remoting_test_driver_environment.h"
15 #include "remoting/test/remote_application_details.h"
16 #include "remoting/test/test_chromoting_client.h"
19 const int kDefaultDPI
= 96;
20 const int kDefaultWidth
= 1024;
21 const int kDefaultHeight
= 768;
23 const char kHostProcessWindowTitle
[] = "Host Process";
29 AppRemotingConnectionHelper::AppRemotingConnectionHelper(
30 const RemoteApplicationDetails
& application_details
)
31 : application_details_(application_details
),
32 connection_is_ready_for_tests_(false),
33 timer_(new base::Timer(true, false)) {
36 AppRemotingConnectionHelper::~AppRemotingConnectionHelper() {
37 // |client_| destroys some of its members via DeleteSoon on the message loop's
38 // TaskRunner so we need to run the loop until it has no more work to do.
39 if (!connection_is_ready_for_tests_
) {
40 client_
->RemoveRemoteConnectionObserver(this);
44 base::RunLoop().RunUntilIdle();
47 void AppRemotingConnectionHelper::Initialize(
48 scoped_ptr
<TestChromotingClient
> test_chromoting_client
) {
49 client_
= test_chromoting_client
.Pass();
50 client_
->AddRemoteConnectionObserver(this);
53 bool AppRemotingConnectionHelper::StartConnection() {
54 DCHECK(thread_checker_
.CalledOnValidThread());
57 RemoteHostInfo remote_host_info
;
58 remoting::test::AppRemotingSharedData
->GetRemoteHostInfoForApplicationId(
59 application_details_
.application_id
, &remote_host_info
);
61 if (!remote_host_info
.IsReadyForConnection()) {
64 remoting::test::AppRemotingSharedData
->AddHostToReleaseList(
65 application_details_
.application_id
, remote_host_info
.host_id
);
67 DCHECK(!run_loop_
|| !run_loop_
->running());
68 run_loop_
.reset(new base::RunLoop());
70 // We will wait up to 30 seconds to complete the remote connection and for the
71 // main application window to become visible.
72 DCHECK(!timer_
->IsRunning());
73 timer_
->Start(FROM_HERE
, base::TimeDelta::FromSeconds(30),
74 run_loop_
->QuitClosure());
76 client_
->StartConnection(remote_host_info
.GenerateConnectionSetupInfo(
77 AppRemotingSharedData
->access_token(),
78 AppRemotingSharedData
->user_name()));
83 if (connection_is_ready_for_tests_
) {
86 client_
->EndConnection();
91 protocol::ClipboardStub
* AppRemotingConnectionHelper::clipboard_forwarder() {
92 return client_
->clipboard_forwarder();
95 protocol::HostStub
* AppRemotingConnectionHelper::host_stub() {
96 return client_
->host_stub();
99 protocol::InputStub
* AppRemotingConnectionHelper::input_stub() {
100 return client_
->input_stub();
103 void AppRemotingConnectionHelper::ConnectionStateChanged(
104 protocol::ConnectionToHost::State state
,
105 protocol::ErrorCode error_code
) {
106 DCHECK(thread_checker_
.CalledOnValidThread());
108 // If the connection is closed or failed then mark the connection as closed
109 // and quit the current RunLoop if it exists.
110 if (state
== protocol::ConnectionToHost::CLOSED
||
111 state
== protocol::ConnectionToHost::FAILED
||
112 error_code
!= protocol::OK
) {
113 connection_is_ready_for_tests_
= false;
121 void AppRemotingConnectionHelper::ConnectionReady(bool ready
) {
122 DCHECK(thread_checker_
.CalledOnValidThread());
125 SendClientConnectionDetailsToHost();
127 // We will only get called here with a false value for |ready| if the video
128 // renderer encounters an error.
129 connection_is_ready_for_tests_
= false;
137 void AppRemotingConnectionHelper::HostMessageReceived(
138 const protocol::ExtensionMessage
& message
) {
139 DCHECK(thread_checker_
.CalledOnValidThread());
141 VLOG(2) << "HostMessage received by HostMessageReceived()."
142 << " type: " << message
.type() << " data: " << message
.data();
144 if (message
.type() == "onWindowAdded") {
145 HandleOnWindowAddedMessage(message
);
147 VLOG(2) << "HostMessage not handled by HostMessageReceived().";
151 void AppRemotingConnectionHelper::SendClientConnectionDetailsToHost() {
152 // First send an access token which will be used for Google Drive access.
153 protocol::ExtensionMessage message
;
154 message
.set_type("accessToken");
155 message
.set_data(AppRemotingSharedData
->access_token());
157 VLOG(1) << "Sending access token to host";
158 client_
->host_stub()->DeliverClientMessage(message
);
160 // Next send the host a description of the client screen size.
161 protocol::ClientResolution client_resolution
;
162 client_resolution
.set_width(kDefaultWidth
);
163 client_resolution
.set_height(kDefaultHeight
);
164 client_resolution
.set_x_dpi(kDefaultDPI
);
165 client_resolution
.set_y_dpi(kDefaultDPI
);
166 client_resolution
.set_dips_width(kDefaultWidth
);
167 client_resolution
.set_dips_height(kDefaultHeight
);
169 VLOG(1) << "Sending ClientResolution details to host";
170 client_
->host_stub()->NotifyClientResolution(client_resolution
);
172 // Finally send a message to start sending us video packets.
173 protocol::VideoControl video_control
;
174 video_control
.set_enable(true);
176 VLOG(1) << "Sending enable VideoControl message to host";
177 client_
->host_stub()->ControlVideo(video_control
);
180 void AppRemotingConnectionHelper::HandleOnWindowAddedMessage(
181 const remoting::protocol::ExtensionMessage
& message
) {
182 DCHECK_EQ(message
.type(), "onWindowAdded");
184 const base::DictionaryValue
* message_data
= nullptr;
185 scoped_ptr
<base::Value
> host_message
= base::JSONReader::Read(message
.data());
186 if (!host_message
.get() || !host_message
->GetAsDictionary(&message_data
)) {
187 LOG(ERROR
) << "onWindowAdded message received was not valid JSON.";
194 std::string current_window_title
;
195 message_data
->GetString("title", ¤t_window_title
);
196 if (current_window_title
== kHostProcessWindowTitle
) {
197 LOG(ERROR
) << "Host Process Window is visible, this likely means that the "
198 << "underlying application is in a bad state, YMMV.";
201 std::string main_window_title
= application_details_
.main_window_title
;
202 if (current_window_title
.find_first_of(main_window_title
) == 0) {
203 connection_is_ready_for_tests_
= true;
204 client_
->RemoveRemoteConnectionObserver(this);
206 if (timer_
->IsRunning()) {
211 // Now that the main window is visible, give the app some time to settle
212 // before signaling that it is ready to run tests.
213 timer_
->Start(FROM_HERE
, base::TimeDelta::FromSeconds(2),
214 run_loop_
->QuitClosure());
219 } // namespace remoting