Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "blimp/client/session/assignment_source.h" | 5 #include "blimp/client/session/assignment_source.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback_helpers.h" | |
| 8 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/json/json_reader.h" | |
| 11 #include "base/json/json_writer.h" | |
| 9 #include "base/location.h" | 12 #include "base/location.h" |
| 10 #include "base/numerics/safe_conversions.h" | 13 #include "base/numerics/safe_conversions.h" |
| 11 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/values.h" | |
| 12 #include "blimp/client/app/blimp_client_switches.h" | 16 #include "blimp/client/app/blimp_client_switches.h" |
| 17 #include "blimp/common/protocol_version.h" | |
| 13 #include "net/base/ip_address.h" | 18 #include "net/base/ip_address.h" |
| 14 #include "net/base/ip_endpoint.h" | 19 #include "net/base/ip_endpoint.h" |
| 20 #include "net/base/load_flags.h" | |
| 21 #include "net/base/net_errors.h" | |
| 22 #include "net/base/url_util.h" | |
| 23 #include "net/http/http_status_code.h" | |
| 24 #include "net/proxy/proxy_config_service.h" | |
| 25 #include "net/proxy/proxy_service.h" | |
| 26 #include "net/url_request/url_fetcher.h" | |
| 27 #include "net/url_request/url_request_context.h" | |
| 28 #include "net/url_request/url_request_context_builder.h" | |
| 29 #include "net/url_request/url_request_context_getter.h" | |
| 15 | 30 |
| 16 namespace blimp { | 31 namespace blimp { |
| 32 namespace client { | |
| 33 | |
| 17 namespace { | 34 namespace { |
| 18 | 35 |
| 19 // TODO(kmarshall): Take values from configuration data. | 36 // Assignment request JSON keys. |
| 20 const char kDummyClientToken[] = "MyVoiceIsMyPassport"; | 37 const char kProtocolVersionKey[] = "protocol_version"; |
| 21 const std::string kDefaultBlimpletIPAddress = "127.0.0.1"; | 38 |
| 22 const uint16_t kDefaultBlimpletTCPPort = 25467; | 39 // Assignment response JSON keys. |
| 23 | 40 const char kClientTokenKey[] = "clientToken"; |
| 24 net::IPAddress GetBlimpletIPAddress() { | 41 const char kHostKey[] = "host"; |
| 42 const char kPortKey[] = "port"; | |
| 43 const char kCertificateFingerprintKey[] = "certificateFingerprint"; | |
| 44 const char kCertificateKey[] = "certificate"; | |
| 45 | |
| 46 Assignment GetCustomBlimpletAssignment() { | |
| 47 GURL url(base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
| 48 switches::kBlimpletEndpoint)); | |
| 49 | |
| 25 std::string host; | 50 std::string host; |
| 26 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 51 int port; |
| 27 switches::kBlimpletHost)) { | 52 if (url.is_empty() || !url.is_valid() || !url.has_scheme() || |
| 28 host = base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 53 !net::ParseHostAndPort(url.path(), &host, &port)) { |
| 29 switches::kBlimpletHost); | 54 return Assignment(); |
| 30 } else { | 55 } |
| 31 host = kDefaultBlimpletIPAddress; | 56 |
| 32 } | |
| 33 net::IPAddress ip_address; | 57 net::IPAddress ip_address; |
| 34 if (!ip_address.AssignFromIPLiteral(host)) | 58 if (!ip_address.AssignFromIPLiteral(host)) { |
| 35 CHECK(false) << "Invalid BlimpletAssignment host " << host; | 59 CHECK(false) << "Invalid BlimpletAssignment host " << host; |
| 36 return ip_address; | 60 } |
| 37 } | 61 |
| 38 | 62 if (!base::IsValueInRangeForNumericType<uint16_t>(port)) { |
| 39 uint16_t GetBlimpletTCPPort() { | 63 CHECK(false) << "Invalid BlimpletAssignment port " << port; |
| 40 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 64 } |
| 41 switches::kBlimpletTCPPort)) { | 65 |
| 42 std::string port_str = | 66 Assignment::TransportProtocol protocol = |
| 43 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 67 Assignment::TransportProtocol::UNKNOWN; |
| 44 switches::kBlimpletTCPPort); | 68 if (url.has_scheme()) { |
| 45 uint port_64t; | 69 if (url.SchemeIs("ssl")) { |
|
Kevin M
2016/02/18 22:14:14
Can you make these into kConstants?
| |
| 46 if (!base::StringToUint(port_str, &port_64t) || | 70 protocol = Assignment::TransportProtocol::SSL; |
| 47 !base::IsValueInRangeForNumericType<uint16_t>(port_64t)) { | 71 } else if (url.SchemeIs("tcp")) { |
| 48 CHECK(false) << "Invalid BlimpletAssignment port " << port_str; | 72 protocol = Assignment::TransportProtocol::TCP; |
| 73 } else if (url.SchemeIs("quic")) { | |
| 74 protocol = Assignment::TransportProtocol::QUIC; | |
| 75 } else { | |
| 76 CHECK(false) << "Invalid BlimpletAssignment scheme " << url.scheme(); | |
| 49 } | 77 } |
| 50 return base::checked_cast<uint16_t>(port_64t); | 78 } |
| 51 } else { | 79 |
| 52 return kDefaultBlimpletTCPPort; | 80 Assignment assignment; |
| 53 } | 81 assignment.transport_protocol = protocol; |
| 54 } | 82 assignment.ip_endpoint = net::IPEndPoint(ip_address, port); |
| 83 assignment.client_token = kDummyClientToken; | |
| 84 return assignment; | |
| 85 } | |
| 86 | |
| 87 GURL GetBlimpAssignerURL() { | |
| 88 // TODO(dtrainor): Add a way to specify another assigner. | |
| 89 return GURL(kDefaultAssignerURL); | |
| 90 } | |
| 91 | |
| 92 class SimpleURLRequestContextGetter : public net::URLRequestContextGetter { | |
| 93 public: | |
| 94 SimpleURLRequestContextGetter( | |
| 95 const scoped_refptr<base::SingleThreadTaskRunner>& io_loop_task_runner) | |
| 96 : io_loop_task_runner_(io_loop_task_runner), | |
| 97 proxy_config_service_(net::ProxyService::CreateSystemProxyConfigService( | |
| 98 io_loop_task_runner_, | |
| 99 io_loop_task_runner_)) {} | |
| 100 | |
| 101 // net::URLRequestContextGetter implementation. | |
| 102 net::URLRequestContext* GetURLRequestContext() override { | |
| 103 if (!url_request_context_) { | |
| 104 net::URLRequestContextBuilder builder; | |
| 105 builder.set_proxy_config_service(std::move(proxy_config_service_)); | |
| 106 builder.DisableHttpCache(); | |
| 107 url_request_context_ = builder.Build(); | |
| 108 } | |
| 109 | |
| 110 return url_request_context_.get(); | |
| 111 } | |
| 112 | |
| 113 scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner() | |
| 114 const override { | |
| 115 return io_loop_task_runner_; | |
| 116 } | |
| 117 | |
| 118 private: | |
| 119 ~SimpleURLRequestContextGetter() override {} | |
| 120 | |
| 121 scoped_refptr<base::SingleThreadTaskRunner> io_loop_task_runner_; | |
| 122 scoped_ptr<net::URLRequestContext> url_request_context_; | |
| 123 | |
| 124 // Temporary storage for the ProxyConfigService, which needs to be created on | |
| 125 // the main thread but cleared on the IO thread. This will be built in the | |
| 126 // constructor and cleared on the IO thread. Due to the usage of this class | |
| 127 // this is safe. | |
| 128 scoped_ptr<net::ProxyConfigService> proxy_config_service_; | |
| 129 | |
| 130 DISALLOW_COPY_AND_ASSIGN(SimpleURLRequestContextGetter); | |
| 131 }; | |
| 55 | 132 |
| 56 } // namespace | 133 } // namespace |
| 57 | 134 |
| 58 namespace client { | 135 Assignment::Assignment() : transport_protocol(TransportProtocol::UNKNOWN) {} |
| 136 | |
| 137 Assignment::~Assignment() {} | |
| 138 | |
| 139 bool Assignment::is_null() const { | |
| 140 return ip_endpoint.address().empty() || ip_endpoint.port() == 0 || | |
| 141 transport_protocol == TransportProtocol::UNKNOWN; | |
| 142 } | |
| 59 | 143 |
| 60 AssignmentSource::AssignmentSource( | 144 AssignmentSource::AssignmentSource( |
| 61 const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner) | 145 const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner, |
| 62 : main_task_runner_(main_task_runner) {} | 146 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) |
| 147 : main_task_runner_(main_task_runner), | |
| 148 url_request_context_(new SimpleURLRequestContextGetter(io_task_runner)) {} | |
| 63 | 149 |
| 64 AssignmentSource::~AssignmentSource() {} | 150 AssignmentSource::~AssignmentSource() {} |
| 65 | 151 |
| 66 void AssignmentSource::GetAssignment(const AssignmentCallback& callback) { | 152 void AssignmentSource::GetAssignment(const std::string& client_auth_token, |
| 153 const AssignmentCallback& callback) { | |
| 67 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 154 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 155 | |
| 156 // Cancel any outstanding callback. | |
| 157 if (!callback_.is_null()) { | |
| 158 base::ResetAndReturn(&callback_) | |
| 159 .Run(AssignmentSource::Result::RESULT_SERVER_INTERRUPTED, Assignment()); | |
| 160 } | |
| 161 callback_ = AssignmentCallback(callback); | |
| 162 | |
| 163 Assignment assignment = GetCustomBlimpletAssignment(); | |
| 164 if (!assignment.is_null()) { | |
| 165 // Post the result so that the behavior of this function is consistent. | |
| 166 main_task_runner_->PostTask( | |
| 167 FROM_HERE, base::Bind(base::ResetAndReturn(&callback_), | |
| 168 AssignmentSource::Result::RESULT_OK, assignment)); | |
| 169 return; | |
| 170 } | |
| 171 | |
| 172 // Call out to the network for a real assignment. Build the network request | |
| 173 // to hit the assigner. | |
| 174 url_fetcher_ = net::URLFetcher::Create(GetBlimpAssignerURL(), | |
| 175 net::URLFetcher::POST, this); | |
| 176 url_fetcher_->SetRequestContext(url_request_context_.get()); | |
| 177 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES | | |
| 178 net::LOAD_DO_NOT_SEND_COOKIES); | |
| 179 url_fetcher_->AddExtraRequestHeader("Authorization: Bearer " + | |
| 180 client_auth_token); | |
| 181 | |
| 182 // Write the JSON for the request data. | |
| 183 base::DictionaryValue dictionary; | |
| 184 dictionary.SetString(kProtocolVersionKey, blimp::kEngineVersion); | |
| 185 std::string json; | |
| 186 base::JSONWriter::Write(dictionary, &json); | |
| 187 url_fetcher_->SetUploadData("application/json", json); | |
| 188 | |
| 189 url_fetcher_->Start(); | |
| 190 } | |
| 191 | |
| 192 void AssignmentSource::OnURLFetchComplete(const net::URLFetcher* source) { | |
| 193 DCHECK(main_task_runner_->BelongsToCurrentThread()); | |
| 194 DCHECK(!callback_.is_null()); | |
| 195 DCHECK_EQ(url_fetcher_.get(), source); | |
| 196 | |
| 197 if (!source->GetStatus().is_success()) { | |
| 198 DVLOG(1) << "Assignment request failed due to network error: " | |
| 199 << net::ErrorToString(source->GetStatus().error()); | |
| 200 base::ResetAndReturn(&callback_) | |
| 201 .Run(AssignmentSource::Result::RESULT_NETWORK_FAILURE, Assignment()); | |
| 202 return; | |
| 203 } | |
| 204 | |
| 205 switch (source->GetResponseCode()) { | |
| 206 case net::HTTP_OK: | |
| 207 ParseAssignerResponse(); | |
| 208 break; | |
| 209 case net::HTTP_BAD_REQUEST: | |
| 210 base::ResetAndReturn(&callback_) | |
| 211 .Run(AssignmentSource::Result::RESULT_BAD_REQUEST, Assignment()); | |
| 212 break; | |
| 213 case net::HTTP_UNAUTHORIZED: | |
| 214 base::ResetAndReturn(&callback_) | |
| 215 .Run(AssignmentSource::Result::RESULT_EXPIRED_ACCESS_TOKEN, | |
| 216 Assignment()); | |
| 217 break; | |
| 218 case net::HTTP_FORBIDDEN: | |
| 219 base::ResetAndReturn(&callback_) | |
| 220 .Run(AssignmentSource::Result::RESULT_USER_INVALID, Assignment()); | |
| 221 break; | |
| 222 case 429: // Too Many Requests | |
| 223 base::ResetAndReturn(&callback_) | |
| 224 .Run(AssignmentSource::Result::RESULT_OUT_OF_VMS, Assignment()); | |
| 225 break; | |
| 226 case net::HTTP_INTERNAL_SERVER_ERROR: | |
| 227 base::ResetAndReturn(&callback_) | |
| 228 .Run(AssignmentSource::Result::RESULT_SERVER_ERROR, Assignment()); | |
| 229 break; | |
| 230 default: | |
| 231 base::ResetAndReturn(&callback_) | |
| 232 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); | |
| 233 break; | |
| 234 } | |
| 235 } | |
| 236 | |
| 237 void AssignmentSource::ParseAssignerResponse() { | |
| 238 DCHECK(url_fetcher_.get()); | |
| 239 DCHECK(url_fetcher_->GetStatus().is_success()); | |
| 240 DCHECK_EQ(net::HTTP_OK, url_fetcher_->GetResponseCode()); | |
| 241 | |
| 242 // Grab the response from the assigner request. | |
| 243 std::string response; | |
| 244 if (!url_fetcher_->GetResponseAsString(&response)) { | |
| 245 base::ResetAndReturn(&callback_) | |
| 246 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); | |
| 247 return; | |
| 248 } | |
| 249 | |
| 250 // Attempt to interpret the response as JSON and treat it as a dictionary. | |
| 251 scoped_ptr<base::Value> json = base::JSONReader::Read(response); | |
| 252 if (!json) { | |
| 253 base::ResetAndReturn(&callback_) | |
| 254 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); | |
| 255 return; | |
| 256 } | |
| 257 | |
| 258 const base::DictionaryValue* dict; | |
| 259 if (!json->GetAsDictionary(&dict)) { | |
| 260 base::ResetAndReturn(&callback_) | |
| 261 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); | |
| 262 return; | |
| 263 } | |
| 264 | |
| 265 // Validate that all the expected fields are present. | |
| 266 std::string client_token; | |
| 267 std::string host; | |
| 268 int port; | |
| 269 std::string cert_fingerprint; | |
| 270 std::string cert; | |
| 271 if (!(dict->GetString(kClientTokenKey, &client_token) && | |
| 272 dict->GetString(kHostKey, &host) && dict->GetInteger(kPortKey, &port) && | |
| 273 dict->GetString(kCertificateFingerprintKey, &cert_fingerprint) && | |
| 274 dict->GetString(kCertificateKey, &cert))) { | |
| 275 base::ResetAndReturn(&callback_) | |
| 276 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); | |
| 277 return; | |
| 278 } | |
| 279 | |
| 280 net::IPAddress ip_address; | |
| 281 if (!ip_address.AssignFromIPLiteral(host)) { | |
| 282 base::ResetAndReturn(&callback_) | |
| 283 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); | |
| 284 return; | |
| 285 } | |
| 286 | |
| 287 if (!base::IsValueInRangeForNumericType<uint16_t>(port)) { | |
| 288 base::ResetAndReturn(&callback_) | |
| 289 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); | |
| 290 return; | |
| 291 } | |
| 292 | |
| 68 Assignment assignment; | 293 Assignment assignment; |
| 69 assignment.ip_endpoint = | 294 // The assigner assumes SSL-only and all engines it assigns only communicate |
| 70 net::IPEndPoint(GetBlimpletIPAddress(), GetBlimpletTCPPort()); | 295 // over SSL. |
| 71 assignment.client_token = kDummyClientToken; | 296 assignment.transport_protocol = Assignment::TransportProtocol::SSL; |
| 72 main_task_runner_->PostTask(FROM_HERE, base::Bind(callback, assignment)); | 297 assignment.ip_endpoint = net::IPEndPoint(ip_address, port); |
| 298 assignment.client_token = client_token; | |
| 299 assignment.certificate = cert; | |
| 300 assignment.certificate_fingerprint = cert_fingerprint; | |
| 301 | |
| 302 base::ResetAndReturn(&callback_) | |
| 303 .Run(AssignmentSource::Result::RESULT_OK, assignment); | |
| 73 } | 304 } |
| 74 | 305 |
| 75 } // namespace client | 306 } // namespace client |
| 76 } // namespace blimp | 307 } // namespace blimp |
| OLD | NEW |