| 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/guid.h" | 9 #include "base/guid.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 #include "components/copresence/handlers/directive_handler.h" | 24 #include "components/copresence/handlers/directive_handler.h" |
| 25 #include "components/copresence/handlers/gcm_handler.h" | 25 #include "components/copresence/handlers/gcm_handler.h" |
| 26 #include "components/copresence/proto/codes.pb.h" | 26 #include "components/copresence/proto/codes.pb.h" |
| 27 #include "components/copresence/proto/data.pb.h" | 27 #include "components/copresence/proto/data.pb.h" |
| 28 #include "components/copresence/proto/rpcs.pb.h" | 28 #include "components/copresence/proto/rpcs.pb.h" |
| 29 #include "components/copresence/public/copresence_constants.h" | 29 #include "components/copresence/public/copresence_constants.h" |
| 30 #include "components/copresence/public/copresence_delegate.h" | 30 #include "components/copresence/public/copresence_delegate.h" |
| 31 #include "components/copresence/rpc/http_post.h" | 31 #include "components/copresence/rpc/http_post.h" |
| 32 #include "net/http/http_status_code.h" | 32 #include "net/http/http_status_code.h" |
| 33 | 33 |
| 34 using google::protobuf::MessageLite; |
| 35 |
| 34 // TODO(ckehoe): Return error messages for bad requests. | 36 // TODO(ckehoe): Return error messages for bad requests. |
| 35 | 37 |
| 36 namespace copresence { | 38 namespace copresence { |
| 37 | 39 |
| 38 using google::protobuf::MessageLite; | |
| 39 using google::protobuf::RepeatedPtrField; | |
| 40 | |
| 41 const char RpcHandler::kReportRequestRpcName[] = "report"; | 40 const char RpcHandler::kReportRequestRpcName[] = "report"; |
| 42 | 41 |
| 43 namespace { | 42 namespace { |
| 44 | 43 |
| 45 const int kTokenLoggingSuffix = 5; | 44 const int kTokenLoggingSuffix = 5; |
| 46 const int kInvalidTokenExpiryTimeMs = 10 * 60 * 1000; // 10 minutes. | 45 const int kInvalidTokenExpiryTimeMs = 10 * 60 * 1000; // 10 minutes. |
| 47 const int kMaxInvalidTokens = 10000; | 46 const int kMaxInvalidTokens = 10000; |
| 48 const char kRegisterDeviceRpcName[] = "registerdevice"; | 47 const char kRegisterDeviceRpcName[] = "registerdevice"; |
| 49 const char kDefaultCopresenceServer[] = | 48 const char kDefaultCopresenceServer[] = |
| 50 "https://www.googleapis.com/copresence/v2/copresence"; | 49 "https://www.googleapis.com/copresence/v2/copresence"; |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 | 161 |
| 163 } // namespace | 162 } // namespace |
| 164 | 163 |
| 165 | 164 |
| 166 // Public functions. | 165 // Public functions. |
| 167 | 166 |
| 168 RpcHandler::RpcHandler(CopresenceDelegate* delegate, | 167 RpcHandler::RpcHandler(CopresenceDelegate* delegate, |
| 169 CopresenceStateImpl* state, | 168 CopresenceStateImpl* state, |
| 170 DirectiveHandler* directive_handler, | 169 DirectiveHandler* directive_handler, |
| 171 GCMHandler* gcm_handler, | 170 GCMHandler* gcm_handler, |
| 171 const MessagesCallback& new_messages_callback, |
| 172 const PostCallback& server_post_callback) | 172 const PostCallback& server_post_callback) |
| 173 : delegate_(delegate), | 173 : delegate_(delegate), |
| 174 state_(state), | 174 state_(state), |
| 175 directive_handler_(directive_handler), | 175 directive_handler_(directive_handler), |
| 176 gcm_handler_(gcm_handler), | 176 gcm_handler_(gcm_handler), |
| 177 new_messages_callback_(new_messages_callback), |
| 177 server_post_callback_(server_post_callback), | 178 server_post_callback_(server_post_callback), |
| 178 invalid_audio_token_cache_( | 179 invalid_audio_token_cache_( |
| 179 base::TimeDelta::FromMilliseconds(kInvalidTokenExpiryTimeMs), | 180 base::TimeDelta::FromMilliseconds(kInvalidTokenExpiryTimeMs), |
| 180 kMaxInvalidTokens) { | 181 kMaxInvalidTokens) { |
| 181 DCHECK(delegate_); | 182 DCHECK(delegate_); |
| 182 DCHECK(directive_handler_); | 183 DCHECK(directive_handler_); |
| 183 // |gcm_handler_| is optional. | 184 // |gcm_handler_| is optional. |
| 184 | 185 |
| 185 if (server_post_callback_.is_null()) { | 186 if (server_post_callback_.is_null()) { |
| 186 server_post_callback_ = | 187 server_post_callback_ = |
| (...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 481 } | 482 } |
| 482 | 483 |
| 483 for (const SubscriptionResult& result : | 484 for (const SubscriptionResult& result : |
| 484 response.manage_subscriptions_response().subscription_result()) { | 485 response.manage_subscriptions_response().subscription_result()) { |
| 485 DVLOG(2) << "Created subscription with id " << result.subscription_id(); | 486 DVLOG(2) << "Created subscription with id " << result.subscription_id(); |
| 486 } | 487 } |
| 487 | 488 |
| 488 if (response.has_update_signals_response()) { | 489 if (response.has_update_signals_response()) { |
| 489 const UpdateSignalsResponse& update_response = | 490 const UpdateSignalsResponse& update_response = |
| 490 response.update_signals_response(); | 491 response.update_signals_response(); |
| 491 DispatchMessages(update_response.message()); | 492 new_messages_callback_.Run(update_response.message()); |
| 492 | 493 |
| 493 for (const Directive& directive : update_response.directive()) | 494 for (const Directive& directive : update_response.directive()) |
| 494 directive_handler_->AddDirective(directive); | 495 directive_handler_->AddDirective(directive); |
| 495 | 496 |
| 496 for (const Token& token : update_response.token()) { | 497 for (const Token& token : update_response.token()) { |
| 497 state_->UpdateTokenStatus(token.id(), token.status()); | 498 state_->UpdateTokenStatus(token.id(), token.status()); |
| 498 switch (token.status()) { | 499 switch (token.status()) { |
| 499 case VALID: | 500 case VALID: |
| 500 // TODO(rkc/ckehoe): Store the token in a |valid_token_cache_| with a | 501 // TODO(rkc/ckehoe): Store the token in a |valid_token_cache_| with a |
| 501 // short TTL (like 10s) and send it up with every report request. | 502 // short TTL (like 10s) and send it up with every report request. |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 541 directive_handler_->GetCurrentAudioToken(AUDIBLE); | 542 directive_handler_->GetCurrentAudioToken(AUDIBLE); |
| 542 const std::string& inaudible_token = | 543 const std::string& inaudible_token = |
| 543 directive_handler_->GetCurrentAudioToken(INAUDIBLE); | 544 directive_handler_->GetCurrentAudioToken(INAUDIBLE); |
| 544 | 545 |
| 545 if (!audible_token.empty()) | 546 if (!audible_token.empty()) |
| 546 AddTokenToRequest(AudioToken(audible_token, true), request); | 547 AddTokenToRequest(AudioToken(audible_token, true), request); |
| 547 if (!inaudible_token.empty()) | 548 if (!inaudible_token.empty()) |
| 548 AddTokenToRequest(AudioToken(inaudible_token, false), request); | 549 AddTokenToRequest(AudioToken(inaudible_token, false), request); |
| 549 } | 550 } |
| 550 | 551 |
| 551 void RpcHandler::DispatchMessages( | |
| 552 const RepeatedPtrField<SubscribedMessage>& messages) { | |
| 553 if (messages.size() == 0) | |
| 554 return; | |
| 555 | |
| 556 // Index the messages by subscription id. | |
| 557 std::map<std::string, std::vector<Message>> messages_by_subscription; | |
| 558 DVLOG(3) << "Dispatching " << messages.size() << " messages"; | |
| 559 for (const SubscribedMessage& message : messages) { | |
| 560 for (const std::string& subscription_id : message.subscription_id()) { | |
| 561 messages_by_subscription[subscription_id].push_back( | |
| 562 message.published_message()); | |
| 563 } | |
| 564 } | |
| 565 | |
| 566 // Send the messages for each subscription. | |
| 567 for (const auto& map_entry : messages_by_subscription) { | |
| 568 // TODO(ckehoe): Once we have the app ID from the server, we need to pass | |
| 569 // it in here and get rid of the app id registry from the main API class. | |
| 570 const std::string& subscription = map_entry.first; | |
| 571 const std::vector<Message>& messages = map_entry.second; | |
| 572 delegate_->HandleMessages(std::string(), subscription, messages); | |
| 573 } | |
| 574 } | |
| 575 | |
| 576 // TODO(ckehoe): Pass in the version string and | 552 // TODO(ckehoe): Pass in the version string and |
| 577 // group this with the local functions up top. | 553 // group this with the local functions up top. |
| 578 RequestHeader* RpcHandler::CreateRequestHeader( | 554 RequestHeader* RpcHandler::CreateRequestHeader( |
| 579 const std::string& client_name, | 555 const std::string& client_name, |
| 580 const std::string& device_id) const { | 556 const std::string& device_id) const { |
| 581 RequestHeader* header = new RequestHeader; | 557 RequestHeader* header = new RequestHeader; |
| 582 | 558 |
| 583 header->set_allocated_framework_version(CreateVersion( | 559 header->set_allocated_framework_version(CreateVersion( |
| 584 "Chrome", delegate_->GetPlatformVersionString())); | 560 "Chrome", delegate_->GetPlatformVersionString())); |
| 585 if (!client_name.empty()) { | 561 if (!client_name.empty()) { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 636 api_key, | 612 api_key, |
| 637 auth_token, | 613 auth_token, |
| 638 command_line->GetSwitchValueASCII(switches::kCopresenceTracingToken), | 614 command_line->GetSwitchValueASCII(switches::kCopresenceTracingToken), |
| 639 *request_proto); | 615 *request_proto); |
| 640 | 616 |
| 641 http_post->Start(base::Bind(callback, http_post)); | 617 http_post->Start(base::Bind(callback, http_post)); |
| 642 pending_posts_.insert(http_post); | 618 pending_posts_.insert(http_post); |
| 643 } | 619 } |
| 644 | 620 |
| 645 } // namespace copresence | 621 } // namespace copresence |
| OLD | NEW |