Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/copresence/rpc/rpc_handler.h" | 5 #include "components/copresence/rpc/rpc_handler.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 133 | 133 |
| 134 if (broadcast_only) | 134 if (broadcast_only) |
| 135 return BROADCAST_ONLY; | 135 return BROADCAST_ONLY; |
| 136 if (scan_only) | 136 if (scan_only) |
| 137 return SCAN_ONLY; | 137 return SCAN_ONLY; |
| 138 | 138 |
| 139 // If nothing else is specified, default to both broadcast and scan. | 139 // If nothing else is specified, default to both broadcast and scan. |
| 140 return BROADCAST_AND_SCAN; | 140 return BROADCAST_AND_SCAN; |
| 141 } | 141 } |
| 142 | 142 |
| 143 // TODO(rkc): Fix this hack once the server supports setting strategies per | |
| 144 // operation. | |
| 145 bool ExtractIsAudibleStrategy(const ReportRequest& request) { | |
| 146 if (request.has_manage_messages_request()) { | |
| 147 const RepeatedPtrField<PublishedMessage> messages = | |
| 148 request.manage_messages_request().message_to_publish(); | |
| 149 for (int i = 0; i < messages.size(); ++i) { | |
| 150 const PublishedMessage& msg = messages.Get(i); | |
| 151 if (msg.has_token_exchange_strategy() && | |
| 152 msg.token_exchange_strategy().has_use_audible() && | |
| 153 msg.token_exchange_strategy().use_audible()) { | |
| 154 return true; | |
| 155 } | |
| 156 } | |
| 157 } | |
| 158 return false; | |
| 159 } | |
| 160 | |
| 143 scoped_ptr<DeviceState> GetDeviceCapabilities(const ReportRequest& request) { | 161 scoped_ptr<DeviceState> GetDeviceCapabilities(const ReportRequest& request) { |
| 144 scoped_ptr<DeviceState> state(new DeviceState); | 162 scoped_ptr<DeviceState> state(new DeviceState); |
| 145 | 163 |
| 146 TokenTechnology* token_technology = | 164 TokenTechnology* token_technology = |
| 147 state->mutable_capabilities()->add_token_technology(); | 165 state->mutable_capabilities()->add_token_technology(); |
| 148 token_technology->set_medium(AUDIO_ULTRASOUND_PASSBAND); | 166 token_technology->set_medium(AUDIO_ULTRASOUND_PASSBAND); |
|
Charlie
2014/08/08 22:24:15
token_technology->set_medium(ExtractIsAudibleStrat
rkc
2014/08/09 06:58:54
We set audible in addition to ultrasound, not inst
| |
| 167 if (ExtractIsAudibleStrategy(request)) | |
| 168 token_technology->set_medium(AUDIO_AUDIBLE_DTMF); | |
| 149 | 169 |
| 150 BroadcastScanConfiguration config = | 170 BroadcastScanConfiguration config = |
| 151 ExtractTokenExchangeStrategy(request); | 171 ExtractTokenExchangeStrategy(request); |
| 152 if (config == BROADCAST_ONLY || config == BROADCAST_AND_SCAN) | 172 if (config == BROADCAST_ONLY || config == BROADCAST_AND_SCAN) |
| 153 token_technology->add_instruction_type(TRANSMIT); | 173 token_technology->add_instruction_type(TRANSMIT); |
| 154 if (config == SCAN_ONLY || config == BROADCAST_AND_SCAN) | 174 if (config == SCAN_ONLY || config == BROADCAST_AND_SCAN) |
| 155 token_technology->add_instruction_type(RECEIVE); | 175 token_technology->add_instruction_type(RECEIVE); |
| 156 | 176 |
| 157 return state.Pass(); | 177 return state.Pass(); |
| 158 } | 178 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 191 | 211 |
| 192 if (delegate_ && delegate_->GetWhispernetClient()) { | 212 if (delegate_ && delegate_->GetWhispernetClient()) { |
| 193 delegate_->GetWhispernetClient()->RegisterTokensCallback( | 213 delegate_->GetWhispernetClient()->RegisterTokensCallback( |
| 194 WhispernetClient::TokensCallback()); | 214 WhispernetClient::TokensCallback()); |
| 195 } | 215 } |
| 196 } | 216 } |
| 197 | 217 |
| 198 void RpcHandler::Initialize(const SuccessCallback& init_done_callback) { | 218 void RpcHandler::Initialize(const SuccessCallback& init_done_callback) { |
| 199 scoped_ptr<RegisterDeviceRequest> request(new RegisterDeviceRequest); | 219 scoped_ptr<RegisterDeviceRequest> request(new RegisterDeviceRequest); |
| 200 DCHECK(device_id_.empty()); | 220 DCHECK(device_id_.empty()); |
| 201 device_id_ = delegate_->GetDeviceId(); | |
| 202 if (!device_id_.empty()) { | |
| 203 init_done_callback.Run(true); | |
| 204 return; | |
| 205 } | |
| 206 | |
| 207 request->mutable_push_service()->set_service(PUSH_SERVICE_NONE); | 221 request->mutable_push_service()->set_service(PUSH_SERVICE_NONE); |
| 208 Identity* identity = | 222 Identity* identity = |
| 209 request->mutable_device_identifiers()->mutable_registrant(); | 223 request->mutable_device_identifiers()->mutable_registrant(); |
| 210 identity->set_type(CHROME); | 224 identity->set_type(CHROME); |
| 211 identity->set_chrome_id(base::GenerateGUID()); | 225 identity->set_chrome_id(base::GenerateGUID()); |
| 212 SendServerRequest( | 226 SendServerRequest( |
| 213 kRegisterDeviceRpcName, | 227 kRegisterDeviceRpcName, |
| 214 std::string(), | 228 std::string(), |
| 215 request.Pass(), | 229 request.Pass(), |
| 216 base::Bind(&RpcHandler::RegisterResponseHandler, | 230 base::Bind(&RpcHandler::RegisterResponseHandler, |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 283 base::Bind(&RpcHandler::ReportTokens, | 297 base::Bind(&RpcHandler::ReportTokens, |
| 284 // On destruction, this callback will be disconnected. | 298 // On destruction, this callback will be disconnected. |
| 285 base::Unretained(this), | 299 base::Unretained(this), |
| 286 AUDIO_ULTRASOUND_PASSBAND)); | 300 AUDIO_ULTRASOUND_PASSBAND)); |
| 287 } | 301 } |
| 288 | 302 |
| 289 // Private methods | 303 // Private methods |
| 290 | 304 |
| 291 void RpcHandler::RegisterResponseHandler( | 305 void RpcHandler::RegisterResponseHandler( |
| 292 const SuccessCallback& init_done_callback, | 306 const SuccessCallback& init_done_callback, |
| 307 HttpPost* completed_post, | |
| 293 int http_status_code, | 308 int http_status_code, |
| 294 const std::string& response_data, | 309 const std::string& response_data) { |
| 295 HttpPost* completed_post) { | |
| 296 if (completed_post) { | 310 if (completed_post) { |
| 297 DCHECK(pending_posts_.erase(completed_post)); | 311 DCHECK(pending_posts_.erase(completed_post)); |
| 298 delete completed_post; | 312 delete completed_post; |
| 299 } | 313 } |
| 300 | 314 |
| 301 if (http_status_code != net::HTTP_OK) { | 315 if (http_status_code != net::HTTP_OK) { |
| 302 init_done_callback.Run(false); | 316 init_done_callback.Run(false); |
| 303 return; | 317 return; |
| 304 } | 318 } |
| 305 | 319 |
| 306 RegisterDeviceResponse response; | 320 RegisterDeviceResponse response; |
| 307 if (!response.ParseFromString(response_data)) { | 321 if (!response.ParseFromString(response_data)) { |
| 308 LOG(ERROR) << "Invalid RegisterDeviceResponse:\n" << response_data; | 322 LOG(ERROR) << "Invalid RegisterDeviceResponse:\n" << response_data; |
| 309 init_done_callback.Run(false); | 323 init_done_callback.Run(false); |
| 310 return; | 324 return; |
| 311 } | 325 } |
| 312 | 326 |
| 313 if (CopresenceErrorLogged(response.header().status())) | 327 if (CopresenceErrorLogged(response.header().status())) |
| 314 return; | 328 return; |
| 315 device_id_ = response.registered_device_id(); | 329 device_id_ = response.registered_device_id(); |
| 316 DCHECK(!device_id_.empty()); | 330 DCHECK(!device_id_.empty()); |
| 317 DVLOG(2) << "Device registration successful: id " << device_id_; | 331 DVLOG(2) << "Device registration successful: id " << device_id_; |
| 318 delegate_->SaveDeviceId(device_id_); | |
| 319 init_done_callback.Run(true); | 332 init_done_callback.Run(true); |
| 320 } | 333 } |
| 321 | 334 |
| 322 void RpcHandler::ReportResponseHandler(const StatusCallback& status_callback, | 335 void RpcHandler::ReportResponseHandler(const StatusCallback& status_callback, |
| 336 HttpPost* completed_post, | |
| 323 int http_status_code, | 337 int http_status_code, |
| 324 const std::string& response_data, | 338 const std::string& response_data) { |
| 325 HttpPost* completed_post) { | |
| 326 if (completed_post) { | 339 if (completed_post) { |
| 327 DCHECK(pending_posts_.erase(completed_post)); | 340 DCHECK(pending_posts_.erase(completed_post)); |
| 328 delete completed_post; | 341 delete completed_post; |
| 329 } | 342 } |
| 330 | 343 |
| 331 if (http_status_code != net::HTTP_OK) { | 344 if (http_status_code != net::HTTP_OK) { |
| 332 if (!status_callback.is_null()) | 345 if (!status_callback.is_null()) |
| 333 status_callback.Run(FAIL); | 346 status_callback.Run(FAIL); |
| 334 return; | 347 return; |
| 335 } | 348 } |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 438 header->set_allocated_client_version( | 451 header->set_allocated_client_version( |
| 439 CreateVersion(client_name, std::string())); | 452 CreateVersion(client_name, std::string())); |
| 440 } | 453 } |
| 441 header->set_current_time_millis(base::Time::Now().ToJsTime()); | 454 header->set_current_time_millis(base::Time::Now().ToJsTime()); |
| 442 header->set_registered_device_id(device_id_); | 455 header->set_registered_device_id(device_id_); |
| 443 | 456 |
| 444 return header; | 457 return header; |
| 445 } | 458 } |
| 446 | 459 |
| 447 template <class T> | 460 template <class T> |
| 448 void RpcHandler::SendServerRequest( | 461 void RpcHandler::SendServerRequest(const std::string& rpc_name, |
| 449 const std::string& rpc_name, | 462 const std::string& app_id, |
| 450 const std::string& app_id, | 463 scoped_ptr<T> request, |
| 451 scoped_ptr<T> request, | 464 const PostCallback& response_handler) { |
| 452 const HttpPost::ResponseCallback& response_handler) { | |
| 453 request->set_allocated_header(CreateRequestHeader(app_id)); | 465 request->set_allocated_header(CreateRequestHeader(app_id)); |
| 454 server_post_callback_.Run(delegate_->GetRequestContext(), | 466 server_post_callback_.Run(delegate_->GetRequestContext(), |
| 455 rpc_name, | 467 rpc_name, |
| 456 make_scoped_ptr<MessageLite>(request.release()), | 468 make_scoped_ptr<MessageLite>(request.release()), |
| 457 response_handler); | 469 response_handler); |
| 458 } | 470 } |
| 459 | 471 |
| 460 void RpcHandler::SendHttpPost(net::URLRequestContextGetter* url_context_getter, | 472 void RpcHandler::SendHttpPost(net::URLRequestContextGetter* url_context_getter, |
| 461 const std::string& rpc_name, | 473 const std::string& rpc_name, |
| 462 scoped_ptr<MessageLite> request_proto, | 474 scoped_ptr<MessageLite> request_proto, |
| 463 const HttpPost::ResponseCallback& callback) { | 475 const PostCallback& callback) { |
| 464 // Create the base URL to call. | 476 // Create the base URL to call. |
| 465 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 477 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 466 const std::string copresence_server_host = | 478 const std::string copresence_server_host = |
| 467 command_line->HasSwitch(switches::kCopresenceServer) ? | 479 command_line->HasSwitch(switches::kCopresenceServer) ? |
| 468 command_line->GetSwitchValueASCII(switches::kCopresenceServer) : | 480 command_line->GetSwitchValueASCII(switches::kCopresenceServer) : |
| 469 kDefaultCopresenceServer; | 481 kDefaultCopresenceServer; |
| 470 | 482 |
| 471 // Create the request and keep a pointer until it completes. | 483 // Create the request and keep a pointer until it completes. |
| 472 pending_posts_.insert(new HttpPost(url_context_getter, | 484 HttpPost* post = new HttpPost( |
| 473 copresence_server_host, | 485 url_context_getter, copresence_server_host, rpc_name, *request_proto); |
| 474 rpc_name, | 486 pending_posts_.insert(post); |
| 475 *request_proto, | 487 post->Start(base::Bind(callback, post)); |
| 476 callback)); | |
| 477 } | 488 } |
| 478 | 489 |
| 479 void RpcHandler::AudioDirectiveListToWhispernetConnector( | 490 void RpcHandler::AudioDirectiveListToWhispernetConnector( |
| 480 const std::string& token, | 491 const std::string& token, |
| 492 bool audible, | |
| 481 const WhispernetClient::SamplesCallback& samples_callback) { | 493 const WhispernetClient::SamplesCallback& samples_callback) { |
| 482 WhispernetClient* whispernet_client = delegate_->GetWhispernetClient(); | 494 WhispernetClient* whispernet_client = delegate_->GetWhispernetClient(); |
| 483 if (whispernet_client) { | 495 if (whispernet_client) { |
| 484 whispernet_client->RegisterSamplesCallback(samples_callback); | 496 whispernet_client->RegisterSamplesCallback(samples_callback); |
| 485 whispernet_client->EncodeToken(token); | 497 whispernet_client->EncodeToken(token, audible); |
| 486 } | 498 } |
| 487 } | 499 } |
| 488 | 500 |
| 489 } // namespace copresence | 501 } // namespace copresence |
| OLD | NEW |