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/url_util.h" | |
| 22 #include "net/http/http_status_code.h" | |
| 23 #include "net/proxy/proxy_config_service.h" | |
| 24 #include "net/proxy/proxy_service.h" | |
| 25 #include "net/url_request/url_fetcher.h" | |
| 26 #include "net/url_request/url_request_context.h" | |
| 27 #include "net/url_request/url_request_context_builder.h" | |
| 28 #include "net/url_request/url_request_context_getter.h" | |
| 15 | 29 |
| 16 namespace blimp { | 30 namespace blimp { |
| 31 namespace client { | |
| 32 | |
| 17 namespace { | 33 namespace { |
| 18 | 34 |
| 19 // TODO(kmarshall): Take values from configuration data. | 35 // Assignment request JSON keys. |
| 20 const char kDummyClientToken[] = "MyVoiceIsMyPassport"; | 36 const char kProtocolVersionKey[] = "protocol_version"; |
| 21 const std::string kDefaultBlimpletIPAddress = "127.0.0.1"; | 37 |
| 22 const uint16_t kDefaultBlimpletTCPPort = 25467; | 38 // Assignment response JSON keys. |
| 23 | 39 const char kClientTokenKey[] = "clientToken"; |
| 24 net::IPAddress GetBlimpletIPAddress() { | 40 const char kHostKey[] = "host"; |
| 41 const char kPortKey[] = "port"; | |
| 42 const char kCertificateFingerprintKey[] = "certificateFingerprint"; | |
| 43 const char kCertificateKey[] = "certificate"; | |
| 44 | |
| 45 Assignment GetCustomBlimpletAssignment() { | |
| 46 GURL url(base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
| 47 switches::kBlimpletEndpoint)); | |
| 48 | |
| 49 if (url.is_empty() || !url.is_valid() || !url.has_scheme()) | |
| 50 return Assignment(); | |
| 51 | |
| 25 std::string host; | 52 std::string host; |
| 26 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 53 int port; |
| 27 switches::kBlimpletHost)) { | 54 if (url.is_empty() || !url.is_valid() || |
|
David Trainor- moved to gerrit
2016/02/17 23:05:19
oops will fix.
| |
| 28 host = base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 55 !net::ParseHostAndPort(url.path(), &host, &port)) { |
| 29 switches::kBlimpletHost); | 56 return Assignment(); |
| 30 } else { | 57 } |
| 31 host = kDefaultBlimpletIPAddress; | 58 |
| 32 } | |
| 33 net::IPAddress ip_address; | 59 net::IPAddress ip_address; |
| 34 if (!ip_address.AssignFromIPLiteral(host)) | 60 if (!ip_address.AssignFromIPLiteral(host)) { |
| 35 CHECK(false) << "Invalid BlimpletAssignment host " << host; | 61 CHECK(false) << "Invalid BlimpletAssignment host " << host; |
| 36 return ip_address; | 62 } |
| 37 } | 63 |
| 38 | 64 if (!base::IsValueInRangeForNumericType<uint16_t>(port)) { |
| 39 uint16_t GetBlimpletTCPPort() { | 65 CHECK(false) << "Invalid BlimpletAssignment port " << port; |
| 40 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 66 } |
| 41 switches::kBlimpletTCPPort)) { | 67 |
| 42 std::string port_str = | 68 Assignment::TransportProtocol protocol = |
| 43 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 69 Assignment::TransportProtocol::UNKNOWN; |
| 44 switches::kBlimpletTCPPort); | 70 if (url.has_scheme()) { |
| 45 uint port_64t; | 71 if (url.SchemeIs("ssl")) { |
| 46 if (!base::StringToUint(port_str, &port_64t) || | 72 protocol = Assignment::TransportProtocol::SSL; |
| 47 !base::IsValueInRangeForNumericType<uint16_t>(port_64t)) { | 73 } else if (url.SchemeIs("tcp")) { |
| 48 CHECK(false) << "Invalid BlimpletAssignment port " << port_str; | 74 protocol = Assignment::TransportProtocol::TCP; |
| 75 } else if (url.SchemeIs("quic")) { | |
| 76 protocol = Assignment::TransportProtocol::QUIC; | |
| 77 } else { | |
| 78 CHECK(false) << "Invalid BlimpletAssignment scheme " << url.scheme(); | |
| 49 } | 79 } |
| 50 return base::checked_cast<uint16_t>(port_64t); | 80 } |
| 51 } else { | 81 |
| 52 return kDefaultBlimpletTCPPort; | 82 Assignment assignment; |
| 53 } | 83 assignment.transport_protocol = protocol; |
| 54 } | 84 assignment.ip_endpoint = net::IPEndPoint(ip_address, port); |
| 85 assignment.client_token = kDummyClientToken; | |
| 86 return assignment; | |
| 87 } | |
| 88 | |
| 89 GURL GetBlimpAssignerURL() { | |
| 90 // TODO(dtrainor): Add a way to specify another assigner. | |
| 91 return GURL(kDefaultAssignerURL); | |
| 92 } | |
| 93 | |
| 94 class SimpleURLRequestContextGetter : public net::URLRequestContextGetter { | |
| 95 public: | |
| 96 SimpleURLRequestContextGetter( | |
| 97 const scoped_refptr<base::SingleThreadTaskRunner>& io_loop_task_runner) | |
| 98 : io_loop_task_runner_(io_loop_task_runner) {} | |
| 99 | |
| 100 // net::URLRequestContextGetter implementation. | |
| 101 net::URLRequestContext* GetURLRequestContext() override { | |
| 102 if (!url_request_context_) { | |
| 103 net::URLRequestContextBuilder builder; | |
| 104 builder.set_proxy_config_service( | |
| 105 net::ProxyService::CreateSystemProxyConfigService( | |
| 106 io_loop_task_runner_, base::ThreadTaskRunnerHandle::Get())); | |
|
mmenke
2016/02/17 23:12:19
On Android, this has to be created on the main thr
David Trainor- moved to gerrit
2016/02/17 23:28:10
Sadly we don't really have an IOThread object. Th
| |
| 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 DISALLOW_COPY_AND_ASSIGN(SimpleURLRequestContextGetter); | |
| 125 }; | |
| 55 | 126 |
| 56 } // namespace | 127 } // namespace |
| 57 | 128 |
| 58 namespace client { | 129 Assignment::Assignment() : transport_protocol(TransportProtocol::UNKNOWN) {} |
| 130 | |
| 131 Assignment::~Assignment() {} | |
| 132 | |
| 133 bool Assignment::is_null() const { | |
| 134 return ip_endpoint.address().empty() || ip_endpoint.port() == 0 || | |
| 135 transport_protocol == TransportProtocol::UNKNOWN; | |
| 136 } | |
| 59 | 137 |
| 60 AssignmentSource::AssignmentSource( | 138 AssignmentSource::AssignmentSource( |
| 61 const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner) | 139 const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner, |
| 62 : main_task_runner_(main_task_runner) {} | 140 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) |
| 141 : main_task_runner_(main_task_runner), | |
| 142 url_request_context_(new SimpleURLRequestContextGetter(io_task_runner)) {} | |
| 63 | 143 |
| 64 AssignmentSource::~AssignmentSource() {} | 144 AssignmentSource::~AssignmentSource() {} |
| 65 | 145 |
| 66 void AssignmentSource::GetAssignment(const AssignmentCallback& callback) { | 146 void AssignmentSource::GetAssignment(const std::string& client_auth_token, |
| 147 const AssignmentCallback& callback) { | |
| 67 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 148 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 149 | |
| 150 // Cancel any outstanding callback. | |
| 151 if (!callback_.is_null()) { | |
| 152 base::ResetAndReturn(&callback_) | |
| 153 .Run(AssignmentSource::Result::RESULT_SERVER_INTERRUPTED, Assignment()); | |
| 154 } | |
| 155 callback_ = AssignmentCallback(callback); | |
| 156 | |
| 157 Assignment assignment = GetCustomBlimpletAssignment(); | |
| 158 if (!assignment.is_null()) { | |
| 159 // Post the result so that the behavior of this function is consistent. | |
| 160 main_task_runner_->PostTask( | |
| 161 FROM_HERE, base::Bind(base::ResetAndReturn(&callback_), | |
| 162 AssignmentSource::Result::RESULT_OK, assignment)); | |
| 163 return; | |
| 164 } | |
| 165 | |
| 166 // Call out to the network for a real assignment. Build the network request | |
| 167 // to hit the assigner. | |
| 168 url_fetcher_ = net::URLFetcher::Create(GetBlimpAssignerURL(), | |
| 169 net::URLFetcher::POST, this); | |
| 170 url_fetcher_->SetRequestContext(url_request_context_.get()); | |
| 171 url_fetcher_->SetAutomaticallyRetryOn5xx(false); | |
| 172 url_fetcher_->SetAutomaticallyRetryOnNetworkChanges(0); | |
| 173 url_fetcher_->SetLoadFlags(net::LOAD_BYPASS_CACHE | net::LOAD_DISABLE_CACHE | | |
| 174 net::LOAD_DO_NOT_SAVE_COOKIES | | |
| 175 net::LOAD_DO_NOT_SEND_COOKIES); | |
| 176 url_fetcher_->AddExtraRequestHeader("Authorization: Bearer " + | |
| 177 client_auth_token); | |
| 178 | |
| 179 // Write the JSON for the request data. | |
| 180 base::DictionaryValue dictionary; | |
| 181 dictionary.SetString(kProtocolVersionKey, blimp::kEngineVersion); | |
| 182 std::string json; | |
| 183 base::JSONWriter::Write(dictionary, &json); | |
| 184 url_fetcher_->SetUploadData("application/json", json); | |
| 185 | |
| 186 url_fetcher_->Start(); | |
| 187 } | |
| 188 | |
| 189 void AssignmentSource::OnURLFetchComplete(const net::URLFetcher* source) { | |
| 190 DCHECK(!callback_.is_null()); | |
| 191 DCHECK_EQ(url_fetcher_.get(), source); | |
| 192 | |
| 193 if (!source->GetStatus().is_success()) { | |
| 194 base::ResetAndReturn(&callback_) | |
| 195 .Run(AssignmentSource::Result::RESULT_NETWORK_FAILURE, Assignment()); | |
| 196 return; | |
| 197 } | |
| 198 | |
| 199 switch (source->GetResponseCode()) { | |
| 200 case net::HTTP_OK: | |
| 201 ParseAssignerResponse(); | |
| 202 break; | |
| 203 case net::HTTP_BAD_REQUEST: | |
| 204 base::ResetAndReturn(&callback_) | |
| 205 .Run(AssignmentSource::Result::RESULT_BAD_REQUEST, Assignment()); | |
| 206 break; | |
| 207 case net::HTTP_UNAUTHORIZED: | |
| 208 base::ResetAndReturn(&callback_) | |
| 209 .Run(AssignmentSource::Result::RESULT_EXPIRED_ACCESS_TOKEN, | |
| 210 Assignment()); | |
| 211 break; | |
| 212 case net::HTTP_FORBIDDEN: | |
| 213 base::ResetAndReturn(&callback_) | |
| 214 .Run(AssignmentSource::Result::RESULT_USER_INVALID, Assignment()); | |
| 215 break; | |
| 216 case 429: // Too Many Requests | |
| 217 base::ResetAndReturn(&callback_) | |
| 218 .Run(AssignmentSource::Result::RESULT_OUT_OF_VMS, Assignment()); | |
| 219 break; | |
| 220 case net::HTTP_INTERNAL_SERVER_ERROR: | |
| 221 base::ResetAndReturn(&callback_) | |
| 222 .Run(AssignmentSource::Result::RESULT_SERVER_ERROR, Assignment()); | |
| 223 break; | |
| 224 default: | |
| 225 base::ResetAndReturn(&callback_) | |
| 226 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); | |
| 227 break; | |
| 228 } | |
| 229 } | |
| 230 | |
| 231 void AssignmentSource::ParseAssignerResponse() { | |
| 232 DCHECK(url_fetcher_.get()); | |
| 233 DCHECK(url_fetcher_->GetStatus().is_success()); | |
| 234 DCHECK_EQ(net::HTTP_OK, url_fetcher_->GetResponseCode()); | |
| 235 | |
| 236 // Grab the response from the assigner request. | |
| 237 std::string response; | |
| 238 if (!url_fetcher_->GetResponseAsString(&response)) { | |
| 239 base::ResetAndReturn(&callback_) | |
| 240 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); | |
| 241 return; | |
| 242 } | |
| 243 | |
| 244 // Attempt to interpret the response as JSON and treat it as a dictionary. | |
| 245 scoped_ptr<base::Value> json = base::JSONReader::Read(response); | |
| 246 if (!json) { | |
| 247 base::ResetAndReturn(&callback_) | |
| 248 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); | |
| 249 return; | |
| 250 } | |
| 251 | |
| 252 const base::DictionaryValue* dict; | |
| 253 if (!json->GetAsDictionary(&dict)) { | |
| 254 base::ResetAndReturn(&callback_) | |
| 255 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); | |
| 256 return; | |
| 257 } | |
| 258 | |
| 259 // Validate that all the expected fields are present. | |
| 260 std::string client_token; | |
| 261 std::string host; | |
| 262 int port; | |
| 263 std::string cert_fingerprint; | |
| 264 std::string cert; | |
| 265 if (!(dict->GetString(kClientTokenKey, &client_token) && | |
| 266 dict->GetString(kHostKey, &host) && dict->GetInteger(kPortKey, &port) && | |
| 267 dict->GetString(kCertificateFingerprintKey, &cert_fingerprint) && | |
| 268 dict->GetString(kCertificateKey, &cert))) { | |
| 269 base::ResetAndReturn(&callback_) | |
| 270 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); | |
| 271 return; | |
| 272 } | |
| 273 | |
| 274 net::IPAddress ip_address; | |
| 275 if (!ip_address.AssignFromIPLiteral(host)) { | |
| 276 base::ResetAndReturn(&callback_) | |
| 277 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); | |
| 278 return; | |
| 279 } | |
| 280 | |
| 281 if (!base::IsValueInRangeForNumericType<uint16_t>(port)) { | |
| 282 base::ResetAndReturn(&callback_) | |
| 283 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); | |
| 284 return; | |
| 285 } | |
| 286 | |
| 68 Assignment assignment; | 287 Assignment assignment; |
| 69 assignment.ip_endpoint = | 288 assignment.transport_protocol = Assignment::TransportProtocol::SSL; |
| 70 net::IPEndPoint(GetBlimpletIPAddress(), GetBlimpletTCPPort()); | 289 assignment.ip_endpoint = net::IPEndPoint(ip_address, port); |
| 71 assignment.client_token = kDummyClientToken; | 290 assignment.client_token = client_token; |
| 72 main_task_runner_->PostTask(FROM_HERE, base::Bind(callback, assignment)); | 291 assignment.certificate = cert; |
| 292 assignment.certificate_fingerprint = cert_fingerprint; | |
| 293 | |
| 294 base::ResetAndReturn(&callback_) | |
| 295 .Run(AssignmentSource::Result::RESULT_OK, assignment); | |
| 73 } | 296 } |
| 74 | 297 |
| 75 } // namespace client | 298 } // namespace client |
| 76 } // namespace blimp | 299 } // namespace blimp |
| OLD | NEW |