| 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 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 LogIfErrorStatus(response.manage_messages_response().status(), "Publish"); | 77 LogIfErrorStatus(response.manage_messages_response().status(), "Publish"); |
| 78 if (response.has_manage_subscriptions_response()) { | 78 if (response.has_manage_subscriptions_response()) { |
| 79 LogIfErrorStatus(response.manage_subscriptions_response().status(), | 79 LogIfErrorStatus(response.manage_subscriptions_response().status(), |
| 80 "Subscribe"); | 80 "Subscribe"); |
| 81 } | 81 } |
| 82 | 82 |
| 83 return result; | 83 return result; |
| 84 } | 84 } |
| 85 | 85 |
| 86 // Request construction | 86 // Request construction |
| 87 // TODO(ckehoe): Move these into a separate file? |
| 87 | 88 |
| 88 template <typename T> | 89 template <typename T> |
| 89 BroadcastScanConfiguration GetBroadcastScanConfig(const T& msg) { | 90 BroadcastScanConfiguration GetBroadcastScanConfig(const T& msg) { |
| 90 if (msg.has_token_exchange_strategy() && | 91 if (msg.has_token_exchange_strategy() && |
| 91 msg.token_exchange_strategy().has_broadcast_scan_configuration()) { | 92 msg.token_exchange_strategy().has_broadcast_scan_configuration()) { |
| 92 return msg.token_exchange_strategy().broadcast_scan_configuration(); | 93 return msg.token_exchange_strategy().broadcast_scan_configuration(); |
| 93 } | 94 } |
| 94 return BROADCAST_SCAN_CONFIGURATION_UNKNOWN; | 95 return BROADCAST_SCAN_CONFIGURATION_UNKNOWN; |
| 95 } | 96 } |
| 96 | 97 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 112 GetBroadcastScanConfig(messages.Get(i)); | 113 GetBroadcastScanConfig(messages.Get(i)); |
| 113 broadcast_only = broadcast_only || config == BROADCAST_ONLY; | 114 broadcast_only = broadcast_only || config == BROADCAST_ONLY; |
| 114 scan_only = scan_only || config == SCAN_ONLY; | 115 scan_only = scan_only || config == SCAN_ONLY; |
| 115 if (config == BROADCAST_AND_SCAN || (broadcast_only && scan_only)) | 116 if (config == BROADCAST_AND_SCAN || (broadcast_only && scan_only)) |
| 116 return BROADCAST_AND_SCAN; | 117 return BROADCAST_AND_SCAN; |
| 117 } | 118 } |
| 118 } | 119 } |
| 119 | 120 |
| 120 // Strategies for subscriptions. | 121 // Strategies for subscriptions. |
| 121 if (request.has_manage_subscriptions_request()) { | 122 if (request.has_manage_subscriptions_request()) { |
| 122 const RepeatedPtrField<Subscription>& messages = | 123 const RepeatedPtrField<Subscription> subscriptions = |
| 123 request.manage_subscriptions_request().subscription(); | 124 request.manage_subscriptions_request().subscription(); |
| 124 for (int i = 0; i < messages.size(); ++i) { | 125 for (int i = 0; i < subscriptions.size(); ++i) { |
| 125 BroadcastScanConfiguration config = | 126 BroadcastScanConfiguration config = |
| 126 GetBroadcastScanConfig(messages.Get(i)); | 127 GetBroadcastScanConfig(subscriptions.Get(i)); |
| 127 broadcast_only = broadcast_only || config == BROADCAST_ONLY; | 128 broadcast_only = broadcast_only || config == BROADCAST_ONLY; |
| 128 scan_only = scan_only || config == SCAN_ONLY; | 129 scan_only = scan_only || config == SCAN_ONLY; |
| 129 if (config == BROADCAST_AND_SCAN || (broadcast_only && scan_only)) | 130 if (config == BROADCAST_AND_SCAN || (broadcast_only && scan_only)) |
| 130 return BROADCAST_AND_SCAN; | 131 return BROADCAST_AND_SCAN; |
| 131 } | 132 } |
| 132 } | 133 } |
| 133 | 134 |
| 134 if (broadcast_only) | 135 if (broadcast_only) |
| 135 return BROADCAST_ONLY; | 136 return BROADCAST_ONLY; |
| 136 if (scan_only) | 137 if (scan_only) |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 TokenObservation* token_observation = | 199 TokenObservation* token_observation = |
| 199 request->mutable_update_signals_request()->add_token_observation(); | 200 request->mutable_update_signals_request()->add_token_observation(); |
| 200 token_observation->set_token_id(ToUrlSafe(token.token)); | 201 token_observation->set_token_id(ToUrlSafe(token.token)); |
| 201 | 202 |
| 202 TokenSignals* signals = token_observation->add_signals(); | 203 TokenSignals* signals = token_observation->add_signals(); |
| 203 signals->set_medium(token.audible ? AUDIO_AUDIBLE_DTMF | 204 signals->set_medium(token.audible ? AUDIO_AUDIBLE_DTMF |
| 204 : AUDIO_ULTRASOUND_PASSBAND); | 205 : AUDIO_ULTRASOUND_PASSBAND); |
| 205 signals->set_observed_time_millis(base::Time::Now().ToJsTime()); | 206 signals->set_observed_time_millis(base::Time::Now().ToJsTime()); |
| 206 } | 207 } |
| 207 | 208 |
| 209 OptInStateFilter* CreateOptedInOrOutFilter() { |
| 210 OptInStateFilter* filter = new OptInStateFilter; |
| 211 filter->add_allowed_opt_in_state(copresence::OPTED_IN); |
| 212 filter->add_allowed_opt_in_state(copresence::OPTED_OUT); |
| 213 return filter; |
| 214 } |
| 215 |
| 216 void AllowOptedOutMessages(ReportRequest* request) { |
| 217 // TODO(ckehoe): Collapse this pattern into ProcessPublish() |
| 218 // and ProcessSubscribe() methods. |
| 219 |
| 220 RepeatedPtrField<PublishedMessage>* messages = |
| 221 request->mutable_manage_messages_request()->mutable_message_to_publish(); |
| 222 for (int i = 0; i < messages->size(); ++i) { |
| 223 PublishedMessage* message = messages->Mutable(i); |
| 224 if (!message->has_opt_in_state_filter()) |
| 225 message->set_allocated_opt_in_state_filter(CreateOptedInOrOutFilter()); |
| 226 } |
| 227 |
| 228 RepeatedPtrField<Subscription>* subscriptions = |
| 229 request->mutable_manage_subscriptions_request()->mutable_subscription(); |
| 230 for (int i = 0; i < subscriptions->size(); ++i) { |
| 231 Subscription* subscription = subscriptions->Mutable(i); |
| 232 if (!subscription->has_opt_in_state_filter()) { |
| 233 subscription->set_allocated_opt_in_state_filter( |
| 234 CreateOptedInOrOutFilter()); |
| 235 } |
| 236 } |
| 237 } |
| 238 |
| 208 } // namespace | 239 } // namespace |
| 209 | 240 |
| 210 // Public methods | 241 // Public methods |
| 211 | 242 |
| 212 RpcHandler::RpcHandler(CopresenceClientDelegate* delegate) | 243 RpcHandler::RpcHandler(CopresenceClientDelegate* delegate) |
| 213 : delegate_(delegate), | 244 : delegate_(delegate), |
| 214 invalid_audio_token_cache_( | 245 invalid_audio_token_cache_( |
| 215 base::TimeDelta::FromMilliseconds(kInvalidTokenExpiryTimeMs), | 246 base::TimeDelta::FromMilliseconds(kInvalidTokenExpiryTimeMs), |
| 216 kMaxInvalidTokens), | 247 kMaxInvalidTokens), |
| 217 server_post_callback_(base::Bind(&RpcHandler::SendHttpPost, | 248 server_post_callback_(base::Bind(&RpcHandler::SendHttpPost, |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 | 295 |
| 265 // If we are unpublishing or unsubscribing, we need to stop those publish or | 296 // If we are unpublishing or unsubscribing, we need to stop those publish or |
| 266 // subscribes right away, we don't need to wait for the server to tell us. | 297 // subscribes right away, we don't need to wait for the server to tell us. |
| 267 ProcessRemovedOperations(*request); | 298 ProcessRemovedOperations(*request); |
| 268 | 299 |
| 269 request->mutable_update_signals_request()->set_allocated_state( | 300 request->mutable_update_signals_request()->set_allocated_state( |
| 270 GetDeviceCapabilities(*request).release()); | 301 GetDeviceCapabilities(*request).release()); |
| 271 | 302 |
| 272 AddPlayingTokens(request.get()); | 303 AddPlayingTokens(request.get()); |
| 273 | 304 |
| 305 AllowOptedOutMessages(request.get()); |
| 274 SendServerRequest(kReportRequestRpcName, | 306 SendServerRequest(kReportRequestRpcName, |
| 275 app_id, | 307 app_id, |
| 276 request.Pass(), | 308 request.Pass(), |
| 277 // On destruction, this request will be cancelled. | 309 // On destruction, this request will be cancelled. |
| 278 base::Bind(&RpcHandler::ReportResponseHandler, | 310 base::Bind(&RpcHandler::ReportResponseHandler, |
| 279 base::Unretained(this), | 311 base::Unretained(this), |
| 280 status_callback)); | 312 status_callback)); |
| 281 } | 313 } |
| 282 | 314 |
| 283 void RpcHandler::ReportTokens(const std::vector<AudioToken>& tokens) { | 315 void RpcHandler::ReportTokens(const std::vector<AudioToken>& tokens) { |
| (...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 540 bool audible, | 572 bool audible, |
| 541 const WhispernetClient::SamplesCallback& samples_callback) { | 573 const WhispernetClient::SamplesCallback& samples_callback) { |
| 542 WhispernetClient* whispernet_client = delegate_->GetWhispernetClient(); | 574 WhispernetClient* whispernet_client = delegate_->GetWhispernetClient(); |
| 543 if (whispernet_client) { | 575 if (whispernet_client) { |
| 544 whispernet_client->RegisterSamplesCallback(samples_callback); | 576 whispernet_client->RegisterSamplesCallback(samples_callback); |
| 545 whispernet_client->EncodeToken(token, audible); | 577 whispernet_client->EncodeToken(token, audible); |
| 546 } | 578 } |
| 547 } | 579 } |
| 548 | 580 |
| 549 } // namespace copresence | 581 } // namespace copresence |
| OLD | NEW |