summaryrefslogtreecommitdiffstats
path: root/remoting/test/remote_host_info_fetcher.cc
blob: 4d9bfb882f0a3a42e9360328d1fbc85f87c68880 (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
// 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/remote_host_info_fetcher.h"

#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/json/json_reader.h"
#include "base/logging.h"
#include "base/strings/stringprintf.h"
#include "base/thread_task_runner_handle.h"
#include "base/values.h"
#include "net/http/http_response_headers.h"
#include "net/http/http_status_code.h"
#include "net/url_request/url_fetcher.h"
#include "remoting/base/url_request_context_getter.h"

namespace {
const char kRequestTestOrigin[] =
    "Origin: chrome-extension://ljacajndfccfgnfohlgkdphmbnpkjflk";
}

namespace remoting {
namespace test {

RemoteHostInfoFetcher::RemoteHostInfoFetcher() {
}

RemoteHostInfoFetcher::~RemoteHostInfoFetcher() {
}

bool RemoteHostInfoFetcher::RetrieveRemoteHostInfo(
    const std::string& application_id,
    const std::string& access_token,
    ServiceEnvironment service_environment,
    const RemoteHostInfoCallback& callback) {
  DCHECK(!application_id.empty());
  DCHECK(!access_token.empty());
  DCHECK(!callback.is_null());
  DCHECK(remote_host_info_callback_.is_null());

  VLOG(2) << "RemoteHostInfoFetcher::RetrieveRemoteHostInfo() called";

  std::string service_url(
      GetRunApplicationUrl(application_id, service_environment));
  if (service_url.empty()) {
    LOG(ERROR) << "Unrecognized service type: " << service_environment;
    return false;
  }
  VLOG(1) << "Using remote host service request url: " << service_url;

  remote_host_info_callback_ = callback;

  request_context_getter_ = new remoting::URLRequestContextGetter(
      base::ThreadTaskRunnerHandle::Get(),   // network_runner
      base::ThreadTaskRunnerHandle::Get());  // file_runner

  request_ =
      net::URLFetcher::Create(GURL(service_url), net::URLFetcher::POST, this);
  request_->SetRequestContext(request_context_getter_.get());
  request_->AddExtraRequestHeader("Authorization: OAuth " + access_token);
  request_->AddExtraRequestHeader(kRequestTestOrigin);
  request_->SetUploadData("application/json; charset=UTF-8", "{}");
  request_->Start();

  return true;
}

void RemoteHostInfoFetcher::OnURLFetchComplete(const net::URLFetcher* source) {
  DCHECK(source);
  VLOG(2) << "URL Fetch Completed for: " << source->GetOriginalURL();

  RemoteHostInfo remote_host_info;
  int response_code = request_->GetResponseCode();
  if (response_code != net::HTTP_OK) {
    LOG(ERROR) << "RemoteHostInfo request failed with error code: "
               << response_code;
    base::ResetAndReturn(&remote_host_info_callback_).Run(remote_host_info);
    return;
  }

  std::string response_string;
  if (!request_->GetResponseAsString(&response_string)) {
    LOG(ERROR) << "Failed to retrieve RemoteHostInfo response data";
    base::ResetAndReturn(&remote_host_info_callback_).Run(remote_host_info);
    return;
  }

  scoped_ptr<base::Value> response_value(
      base::JSONReader::Read(response_string));
  if (!response_value ||
      !response_value->IsType(base::Value::TYPE_DICTIONARY)) {
    LOG(ERROR) << "Failed to parse response string to JSON";
    base::ResetAndReturn(&remote_host_info_callback_).Run(remote_host_info);
    return;
  }

  std::string remote_host_status;
  const base::DictionaryValue* response;
  if (response_value->GetAsDictionary(&response)) {
    response->GetString("status", &remote_host_status);
  } else {
    LOG(ERROR) << "Failed to convert parsed JSON to a dictionary object";
    base::ResetAndReturn(&remote_host_info_callback_).Run(remote_host_info);
    return;
  }

  remote_host_info.SetRemoteHostStatusFromString(remote_host_status);

  if (remote_host_info.IsReadyForConnection()) {
    response->GetString("host.applicationId", &remote_host_info.application_id);
    response->GetString("host.hostId", &remote_host_info.host_id);
    response->GetString("hostJid", &remote_host_info.host_jid);
    response->GetString("authorizationCode",
                        &remote_host_info.authorization_code);
    response->GetString("sharedSecret", &remote_host_info.shared_secret);
  }

  base::ResetAndReturn(&remote_host_info_callback_).Run(remote_host_info);
}

}  // namespace test
}  // namespace remoting