Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(153)

Side by Side Diff: components/copresence/copresence_manager_impl.cc

Issue 704923002: Add polling and audio check to copresence. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/copresence_manager_impl.h" 5 #include "components/copresence/copresence_manager_impl.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/strings/stringprintf.h" 8 #include "base/strings/stringprintf.h"
9 #include "base/timer/timer.h"
9 #include "components/copresence/handlers/directive_handler.h" 10 #include "components/copresence/handlers/directive_handler.h"
10 #include "components/copresence/proto/rpcs.pb.h" 11 #include "components/copresence/proto/rpcs.pb.h"
11 #include "components/copresence/public/whispernet_client.h" 12 #include "components/copresence/public/whispernet_client.h"
12 #include "components/copresence/rpc/rpc_handler.h" 13 #include "components/copresence/rpc/rpc_handler.h"
13 14
14 namespace { 15 namespace {
15 16
16 // Number of characters of suffix to log for auth tokens 17 // Number of characters of suffix to log for auth tokens
17 const int kTokenSuffix = 5; 18 const int kTokenSuffix = 5;
19 const int kPollTimerIntervalMs = 3000; // milliseconds.
20 const int kAudioCheckIntervalMs = 1000; // milliseconds.
18 21
19 } // namespace 22 } // namespace
20 23
21 namespace copresence { 24 namespace copresence {
22 25
23 PendingRequest::PendingRequest(const ReportRequest& report, 26 PendingRequest::PendingRequest(const ReportRequest& report,
24 const std::string& app_id, 27 const std::string& app_id,
25 const std::string& auth_token, 28 const std::string& auth_token,
26 const StatusCallback& callback) 29 const StatusCallback& callback)
27 : report(new ReportRequest(report)), 30 : report(new ReportRequest(report)),
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 CopresenceManagerImpl::CopresenceManagerImpl(CopresenceDelegate* delegate) 96 CopresenceManagerImpl::CopresenceManagerImpl(CopresenceDelegate* delegate)
94 : delegate_(delegate), 97 : delegate_(delegate),
95 pending_init_operations_(0), 98 pending_init_operations_(0),
96 // This callback gets cancelled when we are destroyed. 99 // This callback gets cancelled when we are destroyed.
97 whispernet_init_callback_( 100 whispernet_init_callback_(
98 base::Bind(&CopresenceManagerImpl::InitStepComplete, 101 base::Bind(&CopresenceManagerImpl::InitStepComplete,
99 base::Unretained(this), 102 base::Unretained(this),
100 "Whispernet proxy initialization")), 103 "Whispernet proxy initialization")),
101 init_failed_(false), 104 init_failed_(false),
102 directive_handler_(new DirectiveHandler), 105 directive_handler_(new DirectiveHandler),
103 rpc_handler_(new RpcHandler(delegate, directive_handler_.get())) { 106 poll_timer_(new base::RepeatingTimer<CopresenceManagerImpl>),
107 audio_check_timer_(new base::RepeatingTimer<CopresenceManagerImpl>) {
104 DCHECK(delegate); 108 DCHECK(delegate);
105 DCHECK(delegate->GetWhispernetClient()); 109 DCHECK(delegate->GetWhispernetClient());
106 110
111 rpc_handler_.reset(new RpcHandler(delegate, directive_handler_.get()));
107 delegate->GetWhispernetClient()->Initialize( 112 delegate->GetWhispernetClient()->Initialize(
108 whispernet_init_callback_.callback()); 113 whispernet_init_callback_.callback());
109 pending_init_operations_++; 114 pending_init_operations_++;
110 } 115 }
111 116
112 void CopresenceManagerImpl::CompleteInitialization() { 117 void CopresenceManagerImpl::CompleteInitialization() {
113 if (pending_init_operations_) 118 if (pending_init_operations_)
114 return; 119 return;
115 120
116 if (!init_failed_) { 121 if (!init_failed_) {
117 // When RpcHandler is destroyed, it disconnects this callback. 122 // We destroy |directive_handler_| before |rpc_handler_|, hence passing
118 // TODO(ckehoe): Use a CancelableCallback instead. 123 // in |rpc_handler_|'s pointer is safe here.
119 delegate_->GetWhispernetClient()->RegisterTokensCallback( 124 directive_handler_->Start(delegate_->GetWhispernetClient(),
120 base::Bind(&RpcHandler::ReportTokens, 125 base::Bind(&RpcHandler::ReportTokens,
121 base::Unretained(rpc_handler_.get()))); 126 base::Unretained(rpc_handler_.get())));
122 directive_handler_->Start(delegate_->GetWhispernetClient()); 127
128 // Start up timers.
129 poll_timer_->Start(FROM_HERE,
130 base::TimeDelta::FromMilliseconds(kPollTimerIntervalMs),
131 base::Bind(&CopresenceManagerImpl::PollForMessages,
132 base::Unretained(this)));
133 audio_check_timer_->Start(
134 FROM_HERE, base::TimeDelta::FromMilliseconds(kAudioCheckIntervalMs),
135 base::Bind(&CopresenceManagerImpl::AudioCheck, base::Unretained(this)));
123 } 136 }
124 137
125 // Not const because SendReportRequest takes ownership of the ReportRequests. 138 // Not const because SendReportRequest takes ownership of the ReportRequests.
126 // This is ok though, as the entire queue is deleted afterwards. 139 // This is ok though, as the entire queue is deleted afterwards.
127 for (PendingRequest* request : pending_requests_queue_) { 140 for (PendingRequest* request : pending_requests_queue_) {
128 if (init_failed_) { 141 if (init_failed_) {
129 request->callback.Run(FAIL); 142 request->callback.Run(FAIL);
130 } else { 143 } else {
131 rpc_handler_->SendReportRequest( 144 rpc_handler_->SendReportRequest(
132 request->report.Pass(), 145 request->report.Pass(),
(...skipping 12 matching lines...) Expand all
145 init_failed_ = true; 158 init_failed_ = true;
146 // TODO(ckehoe): Retry for registration failures. But maybe not here. 159 // TODO(ckehoe): Retry for registration failures. But maybe not here.
147 } 160 }
148 161
149 DVLOG(3) << step << " complete."; 162 DVLOG(3) << step << " complete.";
150 DCHECK(pending_init_operations_ > 0); 163 DCHECK(pending_init_operations_ > 0);
151 pending_init_operations_--; 164 pending_init_operations_--;
152 CompleteInitialization(); 165 CompleteInitialization();
153 } 166 }
154 167
168 void CopresenceManagerImpl::PollForMessages() {
169 // Report our currently playing tokens.
170 const std::string& audible_token =
171 directive_handler_->GetCurrentAudioToken(AUDIBLE);
172 const std::string& inaudible_token =
173 directive_handler_->GetCurrentAudioToken(INAUDIBLE);
174
175 std::vector<AudioToken> tokens;
176 if (!audible_token.empty())
177 tokens.push_back(AudioToken(audible_token, true));
178 if (!inaudible_token.empty())
179 tokens.push_back(AudioToken(inaudible_token, false));
180
181 if (!tokens.empty())
182 rpc_handler_->ReportTokens(tokens);
183 }
184
185 void CopresenceManagerImpl::AudioCheck() {
186 if (!directive_handler_->GetCurrentAudioToken(AUDIBLE).empty() &&
187 !directive_handler_->IsAudioTokenHeard(AUDIBLE)) {
188 delegate_->HandleStatusUpdate(AUDIO_FAIL);
189 } else if (!directive_handler_->GetCurrentAudioToken(INAUDIBLE).empty() &&
190 !directive_handler_->IsAudioTokenHeard(INAUDIBLE)) {
191 delegate_->HandleStatusUpdate(AUDIO_FAIL);
192 }
193 }
194
155 } // namespace copresence 195 } // namespace copresence
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698