summaryrefslogtreecommitdiffstats
path: root/remoting/test/app_remoting_connection_helper.cc
blob: f39b398c9b05a70dbaa281c2cf002caef710733d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/test/app_remoting_connection_helper.h"

#include "base/json/json_reader.h"
#include "base/logging.h"
#include "base/run_loop.h"
#include "base/thread_task_runner_handle.h"
#include "base/timer/timer.h"
#include "base/values.h"
#include "remoting/protocol/host_stub.h"
#include "remoting/test/app_remoting_test_driver_environment.h"
#include "remoting/test/remote_application_details.h"
#include "remoting/test/test_chromoting_client.h"

namespace {
const int kDefaultDPI = 96;
const int kDefaultWidth = 1024;
const int kDefaultHeight = 768;

const char kHostProcessWindowTitle[] = "Host Process";
}  // namespace

namespace remoting {
namespace test {

AppRemotingConnectionHelper::AppRemotingConnectionHelper(
    const RemoteApplicationDetails& application_details)
    : application_details_(application_details),
      connection_is_ready_for_tests_(false),
      timer_(new base::Timer(true, false)) {
}

AppRemotingConnectionHelper::~AppRemotingConnectionHelper() {
  // |client_| destroys some of its members via DeleteSoon on the message loop's
  // TaskRunner so we need to run the loop until it has no more work to do.
  if (!connection_is_ready_for_tests_) {
    client_->RemoveRemoteConnectionObserver(this);
  }
  client_.reset();

  base::RunLoop().RunUntilIdle();
}

void AppRemotingConnectionHelper::Initialize(
    scoped_ptr<TestChromotingClient> test_chromoting_client) {
  client_ = test_chromoting_client.Pass();
  client_->AddRemoteConnectionObserver(this);
}

bool AppRemotingConnectionHelper::StartConnection() {
  DCHECK(thread_checker_.CalledOnValidThread());
  DCHECK(client_);

  RemoteHostInfo remote_host_info;
  remoting::test::AppRemotingSharedData->GetRemoteHostInfoForApplicationId(
      application_details_.application_id, &remote_host_info);

  if (!remote_host_info.IsReadyForConnection()) {
    return false;
  }
  remoting::test::AppRemotingSharedData->AddHostToReleaseList(
      application_details_.application_id, remote_host_info.host_id);

  DCHECK(!run_loop_ || !run_loop_->running());
  run_loop_.reset(new base::RunLoop());

  // We will wait up to 30 seconds to complete the remote connection and for the
  // main application window to become visible.
  DCHECK(!timer_->IsRunning());
  timer_->Start(FROM_HERE, base::TimeDelta::FromSeconds(30),
                run_loop_->QuitClosure());

  client_->StartConnection(remote_host_info.GenerateConnectionSetupInfo(
      AppRemotingSharedData->access_token(),
      AppRemotingSharedData->user_name()));

  run_loop_->Run();
  timer_->Stop();

  if (connection_is_ready_for_tests_) {
    return true;
  } else {
    client_->EndConnection();
    return false;
  }
}

protocol::ClipboardStub* AppRemotingConnectionHelper::clipboard_forwarder() {
  return client_->clipboard_forwarder();
}

protocol::HostStub* AppRemotingConnectionHelper::host_stub() {
  return client_->host_stub();
}

protocol::InputStub* AppRemotingConnectionHelper::input_stub() {
  return client_->input_stub();
}

void AppRemotingConnectionHelper::ConnectionStateChanged(
    protocol::ConnectionToHost::State state,
    protocol::ErrorCode error_code) {
  DCHECK(thread_checker_.CalledOnValidThread());

  // If the connection is closed or failed then mark the connection as closed
  // and quit the current RunLoop if it exists.
  if (state == protocol::ConnectionToHost::CLOSED ||
      state == protocol::ConnectionToHost::FAILED ||
      error_code != protocol::OK) {
    connection_is_ready_for_tests_ = false;

    if (run_loop_) {
      run_loop_->Quit();
    }
  }
}

void AppRemotingConnectionHelper::ConnectionReady(bool ready) {
  DCHECK(thread_checker_.CalledOnValidThread());

  if (ready) {
    SendClientConnectionDetailsToHost();
  } else {
    // We will only get called here with a false value for |ready| if the video
    // renderer encounters an error.
    connection_is_ready_for_tests_ = false;

    if (run_loop_) {
      run_loop_->Quit();
    }
  }
}

void AppRemotingConnectionHelper::HostMessageReceived(
    const protocol::ExtensionMessage& message) {
  DCHECK(thread_checker_.CalledOnValidThread());

  VLOG(2) << "HostMessage received by HostMessageReceived()."
          << " type: " << message.type() << " data: " << message.data();

  if (message.type() == "onWindowAdded") {
    HandleOnWindowAddedMessage(message);
  } else {
    VLOG(2) << "HostMessage not handled by HostMessageReceived().";
  }
}

void AppRemotingConnectionHelper::SendClientConnectionDetailsToHost() {
  // First send an access token which will be used for Google Drive access.
  protocol::ExtensionMessage message;
  message.set_type("accessToken");
  message.set_data(AppRemotingSharedData->access_token());

  VLOG(1) << "Sending access token to host";
  client_->host_stub()->DeliverClientMessage(message);

  // Next send the host a description of the client screen size.
  protocol::ClientResolution client_resolution;
  client_resolution.set_width(kDefaultWidth);
  client_resolution.set_height(kDefaultHeight);
  client_resolution.set_x_dpi(kDefaultDPI);
  client_resolution.set_y_dpi(kDefaultDPI);
  client_resolution.set_dips_width(kDefaultWidth);
  client_resolution.set_dips_height(kDefaultHeight);

  VLOG(1) << "Sending ClientResolution details to host";
  client_->host_stub()->NotifyClientResolution(client_resolution);

  // Finally send a message to start sending us video packets.
  protocol::VideoControl video_control;
  video_control.set_enable(true);

  VLOG(1) << "Sending enable VideoControl message to host";
  client_->host_stub()->ControlVideo(video_control);
}

void AppRemotingConnectionHelper::HandleOnWindowAddedMessage(
    const remoting::protocol::ExtensionMessage& message) {
  DCHECK_EQ(message.type(), "onWindowAdded");

  const base::DictionaryValue* message_data = nullptr;
  scoped_ptr<base::Value> host_message = base::JSONReader::Read(message.data());
  if (!host_message.get() || !host_message->GetAsDictionary(&message_data)) {
    LOG(ERROR) << "onWindowAdded message received was not valid JSON.";
    if (run_loop_) {
      run_loop_->Quit();
    }
    return;
  }

  std::string current_window_title;
  message_data->GetString("title", &current_window_title);
  if (current_window_title == kHostProcessWindowTitle) {
    LOG(ERROR) << "Host Process Window is visible, this likely means that the "
               << "underlying application is in a bad state, YMMV.";
  }

  std::string main_window_title = application_details_.main_window_title;
  if (current_window_title.find_first_of(main_window_title) == 0) {
    connection_is_ready_for_tests_ = true;
    client_->RemoveRemoteConnectionObserver(this);

    if (timer_->IsRunning()) {
      timer_->Stop();
    }

    DCHECK(run_loop_);
    // Now that the main window is visible, give the app some time to settle
    // before signaling that it is ready to run tests.
    timer_->Start(FROM_HERE, base::TimeDelta::FromSeconds(2),
                  run_loop_->QuitClosure());
  }
}

}  // namespace test
}  // namespace remoting