blob: 451ccd5ef1b50e227ec8b331cbf43abe2cf56b10 [file] [log] [blame]
sergeyuba520442016-02-20 01:43:311// Copyright 2016 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.
4
5#include "remoting/protocol/http_ice_config_request.h"
6
7#include "base/bind.h"
8#include "base/callback_helpers.h"
sergeyuba520442016-02-20 01:43:319#include "base/values.h"
rhalavati6826b7b2017-05-09 12:12:1110#include "net/traffic_annotation/network_traffic_annotation.h"
sergeyuba520442016-02-20 01:43:3111#include "remoting/protocol/ice_config.h"
12
13namespace remoting {
14namespace protocol {
15
sergeyuba520442016-02-20 01:43:3116HttpIceConfigRequest::HttpIceConfigRequest(
17 UrlRequestFactory* url_request_factory,
Sergey Ulanovfe2d1ba22017-06-13 20:16:1718 const std::string& url,
19 OAuthTokenGetter* oauth_token_getter)
20 : url_(url), weak_factory_(this) {
rhalavati6826b7b2017-05-09 12:12:1121 net::NetworkTrafficAnnotationTag traffic_annotation =
22 net::DefineNetworkTrafficAnnotation("CRD_ice_config_request", R"(
23 semantics {
24 sender: "Chrome Remote Desktop"
25 description:
26 "Request is used by Chrome Remote Desktop to fetch ICE "
27 "configuration which contains list of STUN & TURN servers and TURN "
28 "credentials."
29 trigger:
30 "When a Chrome Remote Desktop session is being connected and "
31 "periodically while a session is active, as necessary. Currently "
32 "the API issues credentials that expire every 24 hours, so this "
33 "request will only be sent again while session is active more than "
34 "24 hours and it needs to renegotiate the ICE connection. The 24 "
35 "hour period is controlled by the server and may change. In some "
36 "cases, e.g. if direct connection is used, it will not trigger "
rhalavatic14b45bb2017-05-18 06:09:3637 "periodically."
rhalavati6826b7b2017-05-09 12:12:1138 data: "None."
39 destination: GOOGLE_OWNED_SERVICE
40 }
41 policy {
Ramin Halavati3b979782017-07-21 11:40:2642 cookies_allowed: NO
rhalavati6826b7b2017-05-09 12:12:1143 setting:
44 "This feature cannot be disabled by settings. You can block Chrome "
45 "Remote Desktop as specified here: "
46 "https://support.google.com/chrome/?p=remote_desktop"
47 chrome_policy {
48 RemoteAccessHostFirewallTraversal {
49 policy_options {mode: MANDATORY}
50 RemoteAccessHostFirewallTraversal: false
51 }
52 }
Ramin Halavati6985c452017-07-28 05:15:4653 }
54 comments:
55 "Above specified policy is only applicable on the host side and "
56 "doesn't have effect in Android and iOS client apps. The product "
57 "is shipped separately from Chromium, except on Chrome OS."
58 )");
rhalavati6826b7b2017-05-09 12:12:1159 url_request_ = url_request_factory->CreateUrlRequest(
Sergey Ulanovfe2d1ba22017-06-13 20:16:1760 UrlRequest::Type::GET, url_, traffic_annotation);
61 oauth_token_getter_ = oauth_token_getter;
sergeyuba520442016-02-20 01:43:3162 url_request_->SetPostData("application/json", "");
63}
64
Chris Watkins6fe52aa2017-11-28 03:24:0565HttpIceConfigRequest::~HttpIceConfigRequest() = default;
sergeyuba520442016-02-20 01:43:3166
67void HttpIceConfigRequest::Send(const OnIceConfigCallback& callback) {
68 DCHECK(on_ice_config_callback_.is_null());
69 DCHECK(!callback.is_null());
70
71 on_ice_config_callback_ = callback;
Sergey Ulanovfe2d1ba22017-06-13 20:16:1772
73 if (oauth_token_getter_) {
74 oauth_token_getter_->CallWithToken(base::Bind(
75 &HttpIceConfigRequest::OnOAuthToken, weak_factory_.GetWeakPtr()));
76 } else {
77 SendRequest();
78 }
79}
80
81void HttpIceConfigRequest::OnOAuthToken(OAuthTokenGetter::Status status,
82 const std::string& user_email,
83 const std::string& access_token) {
84 if (status != OAuthTokenGetter::SUCCESS) {
85 LOG(ERROR) << "Failed to get OAuth token for IceConfig request.";
86 base::ResetAndReturn(&on_ice_config_callback_).Run(IceConfig());
87 return;
88 }
89
90 url_request_->AddHeader("Authorization:Bearer " + access_token);
91 SendRequest();
92}
93
94void HttpIceConfigRequest::SendRequest() {
sergeyuba520442016-02-20 01:43:3195 url_request_->Start(
96 base::Bind(&HttpIceConfigRequest::OnResponse, base::Unretained(this)));
97}
98
99void HttpIceConfigRequest::OnResponse(const UrlRequest::Result& result) {
100 DCHECK(!on_ice_config_callback_.is_null());
101
102 if (!result.success) {
103 LOG(ERROR) << "Failed to fetch " << url_;
104 base::ResetAndReturn(&on_ice_config_callback_).Run(IceConfig());
105 return;
106 }
107
108 if (result.status != 200) {
109 LOG(ERROR) << "Received status code " << result.status << " from " << url_
110 << ": " << result.response_body;
111 base::ResetAndReturn(&on_ice_config_callback_).Run(IceConfig());
112 return;
113 }
114
Sergey Ulanovd1b9d022017-06-06 01:15:28115 IceConfig ice_config = IceConfig::Parse(result.response_body);
116 if (ice_config.is_null()) {
sergeyuba520442016-02-20 01:43:31117 LOG(ERROR) << "Received invalid response from " << url_ << ": "
118 << result.response_body;
sergeyuba520442016-02-20 01:43:31119 }
120
121 base::ResetAndReturn(&on_ice_config_callback_).Run(ice_config);
122}
123
124} // namespace protocol
125} // namespace remoting