sergeyu | ba52044 | 2016-02-20 01:43:31 | [diff] [blame] | 1 | // 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" |
sergeyu | ba52044 | 2016-02-20 01:43:31 | [diff] [blame] | 9 | #include "base/values.h" |
rhalavati | 6826b7b | 2017-05-09 12:12:11 | [diff] [blame] | 10 | #include "net/traffic_annotation/network_traffic_annotation.h" |
sergeyu | ba52044 | 2016-02-20 01:43:31 | [diff] [blame] | 11 | #include "remoting/protocol/ice_config.h" |
| 12 | |
| 13 | namespace remoting { |
| 14 | namespace protocol { |
| 15 | |
sergeyu | ba52044 | 2016-02-20 01:43:31 | [diff] [blame] | 16 | HttpIceConfigRequest::HttpIceConfigRequest( |
| 17 | UrlRequestFactory* url_request_factory, |
Sergey Ulanov | fe2d1ba2 | 2017-06-13 20:16:17 | [diff] [blame] | 18 | const std::string& url, |
| 19 | OAuthTokenGetter* oauth_token_getter) |
| 20 | : url_(url), weak_factory_(this) { |
rhalavati | 6826b7b | 2017-05-09 12:12:11 | [diff] [blame] | 21 | 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 " |
rhalavati | c14b45bb | 2017-05-18 06:09:36 | [diff] [blame] | 37 | "periodically." |
rhalavati | 6826b7b | 2017-05-09 12:12:11 | [diff] [blame] | 38 | data: "None." |
| 39 | destination: GOOGLE_OWNED_SERVICE |
| 40 | } |
| 41 | policy { |
Ramin Halavati | 3b97978 | 2017-07-21 11:40:26 | [diff] [blame] | 42 | cookies_allowed: NO |
rhalavati | 6826b7b | 2017-05-09 12:12:11 | [diff] [blame] | 43 | 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 Halavati | 6985c45 | 2017-07-28 05:15:46 | [diff] [blame] | 53 | } |
| 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 | )"); |
rhalavati | 6826b7b | 2017-05-09 12:12:11 | [diff] [blame] | 59 | url_request_ = url_request_factory->CreateUrlRequest( |
Sergey Ulanov | fe2d1ba2 | 2017-06-13 20:16:17 | [diff] [blame] | 60 | UrlRequest::Type::GET, url_, traffic_annotation); |
| 61 | oauth_token_getter_ = oauth_token_getter; |
sergeyu | ba52044 | 2016-02-20 01:43:31 | [diff] [blame] | 62 | url_request_->SetPostData("application/json", ""); |
| 63 | } |
| 64 | |
Chris Watkins | 6fe52aa | 2017-11-28 03:24:05 | [diff] [blame] | 65 | HttpIceConfigRequest::~HttpIceConfigRequest() = default; |
sergeyu | ba52044 | 2016-02-20 01:43:31 | [diff] [blame] | 66 | |
| 67 | void 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 Ulanov | fe2d1ba2 | 2017-06-13 20:16:17 | [diff] [blame] | 72 | |
| 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 | |
| 81 | void 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 | |
| 94 | void HttpIceConfigRequest::SendRequest() { |
sergeyu | ba52044 | 2016-02-20 01:43:31 | [diff] [blame] | 95 | url_request_->Start( |
| 96 | base::Bind(&HttpIceConfigRequest::OnResponse, base::Unretained(this))); |
| 97 | } |
| 98 | |
| 99 | void 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 Ulanov | d1b9d02 | 2017-06-06 01:15:28 | [diff] [blame] | 115 | IceConfig ice_config = IceConfig::Parse(result.response_body); |
| 116 | if (ice_config.is_null()) { |
sergeyu | ba52044 | 2016-02-20 01:43:31 | [diff] [blame] | 117 | LOG(ERROR) << "Received invalid response from " << url_ << ": " |
| 118 | << result.response_body; |
sergeyu | ba52044 | 2016-02-20 01:43:31 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | base::ResetAndReturn(&on_ice_config_callback_).Run(ice_config); |
| 122 | } |
| 123 | |
| 124 | } // namespace protocol |
| 125 | } // namespace remoting |