File: gcd_rest_client.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (146 lines) | stat: -rw-r--r-- 4,967 bytes parent folder | download
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
// 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/host/gcd_rest_client.h"

#include <stdint.h>

#include <utility>

#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/json/json_writer.h"
#include "base/message_loop/message_loop.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/time/default_clock.h"
#include "base/values.h"
#include "net/url_request/url_fetcher.h"
#include "remoting/base/logging.h"

namespace remoting {

GcdRestClient::GcdRestClient(const std::string& gcd_base_url,
                             const std::string& gcd_device_id,
                             const scoped_refptr<net::URLRequestContextGetter>&
                                 url_request_context_getter,
                             OAuthTokenGetter* token_getter)
    : gcd_base_url_(gcd_base_url),
      gcd_device_id_(gcd_device_id),
      url_request_context_getter_(url_request_context_getter),
      token_getter_(token_getter),
      clock_(new base::DefaultClock) {}

GcdRestClient::~GcdRestClient() {}

void GcdRestClient::PatchState(
    std::unique_ptr<base::DictionaryValue> patch_details,
    const GcdRestClient::ResultCallback& callback) {
  DCHECK(!HasPendingRequest());

  // Construct a status update message in the format GCD expects.  The
  // message looks like this, where "..." is filled in from
  // |patch_details|:
  //
  // {
  //   requestTimeMs: T,
  //   patches: [{
  //     timeMs: T,
  //     patch: {...}
  //   }]
  // }
  //
  // Note that |now| is deliberately using a double to hold an integer
  // value because |DictionaryValue| doesn't support int64_t values, and
  // GCD doesn't accept fractional values.
  double now = clock_->Now().ToJavaTime();
  std::unique_ptr<base::DictionaryValue> patch_dict(new base::DictionaryValue);
  patch_dict->SetDouble("requestTimeMs", now);
  std::unique_ptr<base::ListValue> patch_list(new base::ListValue);
  std::unique_ptr<base::DictionaryValue> patch_item(new base::DictionaryValue);
  patch_item->Set("patch", std::move(patch_details));
  patch_item->SetDouble("timeMs", now);
  patch_list->Append(std::move(patch_item));
  patch_dict->Set("patches", std::move(patch_list));

  // Stringify the message.
  std::string patch_string;
  if (!base::JSONWriter::Write(*patch_dict, &patch_string)) {
    LOG(ERROR) << "Error building GCD device state patch.";
    callback.Run(OTHER_ERROR);
    return;
  }
  DLOG(INFO) << "sending state patch: " << patch_string;

  std::string url =
      gcd_base_url_ + "/devices/" + gcd_device_id_ + "/patchState";

  // Prepare an HTTP request to issue once an auth token is available.
  callback_ = callback;
  url_fetcher_ =
      net::URLFetcher::Create(GURL(url), net::URLFetcher::POST, this);
  url_fetcher_->SetUploadData("application/json", patch_string);
  if (url_request_context_getter_) {
    url_fetcher_->SetRequestContext(url_request_context_getter_.get());
  }

  token_getter_->CallWithToken(
      base::Bind(&GcdRestClient::OnTokenReceived, base::Unretained(this)));
}

void GcdRestClient::SetClockForTest(std::unique_ptr<base::Clock> clock) {
  clock_ = std::move(clock);
}

void GcdRestClient::OnTokenReceived(OAuthTokenGetter::Status status,
                                    const std::string& user_email,
                                    const std::string& access_token) {
  DCHECK(HasPendingRequest());

  if (status != OAuthTokenGetter::SUCCESS) {
    LOG(ERROR) << "Error getting OAuth token for GCD request: "
               << url_fetcher_->GetOriginalURL();
    if (status == OAuthTokenGetter::NETWORK_ERROR) {
      FinishCurrentRequest(NETWORK_ERROR);
    } else {
      FinishCurrentRequest(OTHER_ERROR);
    }
    return;
  }

  url_fetcher_->SetExtraRequestHeaders(
      "Authorization: Bearer " + access_token);
  url_fetcher_->Start();
}

void GcdRestClient::FinishCurrentRequest(Result result) {
  DCHECK(HasPendingRequest());
  url_fetcher_.reset();
  base::ResetAndReturn(&callback_).Run(result);
}

void GcdRestClient::OnURLFetchComplete(const net::URLFetcher* source) {
  DCHECK(HasPendingRequest());

  const GURL& request_url = url_fetcher_->GetOriginalURL();
  Result status = OTHER_ERROR;
  int response = source->GetResponseCode();
  if (response >= 200 && response < 300) {
    DLOG(INFO) << "GCD request succeeded:" << request_url;
    status = SUCCESS;
  } else if (response == 404) {
    LOG(WARNING) << "Host not found (" << response
                 << ") fetching URL: " << request_url;
    status = NO_SUCH_HOST;
  } else if (response == 0) {
    LOG(ERROR) << "Network error (" << response
               << ") fetching URL: " << request_url;
    status = NETWORK_ERROR;
  } else {
    LOG(ERROR) << "Error (" << response << ") fetching URL: " << request_url;
  }

  FinishCurrentRequest(status);
}

}  // namespace remoting