Index: components/copresence/copresence_state_impl.cc |
diff --git a/components/copresence/copresence_state_impl.cc b/components/copresence/copresence_state_impl.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..789154dac7afecb87753f54a5ce6945cedcc3214 |
--- /dev/null |
+++ b/components/copresence/copresence_state_impl.cc |
@@ -0,0 +1,173 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/logging.h" |
+#include "base/time/time.h" |
+#include "components/copresence/copresence_state_impl.h" |
+#include "components/copresence/proto/data.pb.h" |
+#include "components/copresence/public/copresence_constants.h" |
+#include "components/copresence/public/copresence_observer.h" |
+ |
+namespace copresence { |
+ |
+namespace { |
+ |
+template<class T> |
+void CheckUnchangedFields(const T& new_token, const T& current_token) { |
+ if (current_token.id.empty()) |
+ return; |
+ |
+ // These fields identify the token. |
+ DCHECK_EQ(new_token.id, current_token.id); |
+ DCHECK_EQ(new_token.medium, current_token.medium); |
+ |
+ // start_time may not be changed. |
+ DCHECK_EQ(new_token.start_time, current_token.start_time); |
+} |
+ |
+void UpdateToken(const TransmittedToken& new_token, |
+ TransmittedToken* current_token) { |
+ CheckUnchangedFields(new_token, *current_token); |
+ |
+ current_token->stop_time = new_token.stop_time; |
+ current_token->broadcast_confirmed = new_token.broadcast_confirmed; |
+} |
+ |
+void UpdateToken(const ReceivedToken& new_token, |
+ ReceivedToken* current_token) { |
+ CheckUnchangedFields(new_token, *current_token); |
+ |
+ current_token->last_time = new_token.last_time; |
+ if (new_token.valid != ReceivedToken::UNKNOWN) |
+ current_token->valid = new_token.valid; |
+} |
+ |
+} // namespace |
+ |
+ |
+// Public functions. |
+ |
+CopresenceStateImpl::CopresenceStateImpl() {} |
+ |
+CopresenceStateImpl::~CopresenceStateImpl() {} |
+ |
+void CopresenceStateImpl::AddObserver(CopresenceObserver* observer) { |
+ DCHECK(observer); |
+ bool not_duplicate = observers_.insert(observer).second; |
+ DCHECK(not_duplicate); |
+} |
+ |
+void CopresenceStateImpl::RemoveObserver(CopresenceObserver* observer) { |
+ DCHECK(observer); |
+ int elts_deleted = observers_.erase(observer); |
+ DCHECK_GT(elts_deleted, 0); |
+} |
+ |
+const std::vector<Directive>& CopresenceStateImpl::active_directives() { |
+ return active_directives_; |
+} |
+ |
+const std::map<std::string, TransmittedToken>& |
+CopresenceStateImpl::transmitted_tokens() { |
+ return transmitted_tokens_; |
+} |
+ |
+const std::map<std::string, ReceivedToken>& |
+CopresenceStateImpl::received_tokens() { |
+ return received_tokens_; |
+} |
+ |
+// TODO(ckehoe): Only send updates if the directives have really changed. |
+void CopresenceStateImpl::UpdateDirectives( |
+ const std::vector<Directive>& directives) { |
+ active_directives_ = directives; |
+ UpdateTransmittingTokens(); |
+ |
+ for (CopresenceObserver* observer : observers_) |
+ observer->DirectivesUpdated(); |
+} |
+ |
+void CopresenceStateImpl::UpdateTransmittedToken( |
+ const TransmittedToken& token) { |
+ UpdateToken(token, &transmitted_tokens_[token.id]); |
xiyuan
2014/12/18 18:10:51
General question: when would a token be removed fr
Charlie
2014/12/19 03:53:33
Currently this doesn't happen. This is useful to a
|
+ for (CopresenceObserver* observer : observers_) |
+ observer->TokenTransmitted(transmitted_tokens_[token.id]); |
+} |
+ |
+// TODO(ckehoe): Check which tokens are no longer heard and report them lost. |
+void CopresenceStateImpl::UpdateReceivedToken(const ReceivedToken& token) { |
+ DCHECK(!token.id.empty()); |
+ |
+ // TODO(ckehoe): Have CopresenceManagerImpl::AudioCheck() use this to check |
+ // if we can hear our token, and delete the logic from the AudioManager. |
+ if (transmitted_tokens_.count(token.id) > 0) { |
+ transmitted_tokens_[token.id].broadcast_confirmed = true; |
+ for (CopresenceObserver* observer : observers_) |
+ observer->TokenTransmitted(transmitted_tokens_[token.id]); |
+ } else { |
+ ReceivedToken& stored_token = received_tokens_[token.id]; |
+ UpdateToken(token, &stored_token); |
+ |
+ // The decoder doesn't track when this token was heard before, |
+ // so it should just fill in the last_time. |
+ // If we've never seen this token, we populate the start time too. |
+ if (stored_token.start_time.is_null()) |
+ stored_token.start_time = token.last_time; |
+ |
+ for (CopresenceObserver* observer : observers_) |
+ observer->TokenReceived(stored_token); |
+ } |
+} |
+ |
+void CopresenceStateImpl::UpdateTokenStatus(const std::string& token_id, |
+ TokenStatus status) { |
+ if (transmitted_tokens_.count(token_id) > 0) { |
+ LOG_IF(ERROR, status != VALID) |
+ << "Broadcast token " << token_id << " is invalid"; |
+ } else if (received_tokens_.count(token_id) > 0) { |
+ received_tokens_[token_id].valid = status == VALID ? |
+ ReceivedToken::VALID : ReceivedToken::INVALID; |
+ for (CopresenceObserver* observer : observers_) |
+ observer->TokenReceived(received_tokens_[token_id]); |
+ } else { |
+ LOG(ERROR) << "Got status update for unrecognized token " << token_id; |
+ } |
+} |
+ |
+ |
+// Private functions. |
+ |
+void CopresenceStateImpl::UpdateTransmittingTokens() { |
+ std::set<std::string> tokens_to_update; |
+ for (const auto& token_entry : transmitted_tokens_) |
+ tokens_to_update.insert(token_entry.first); |
+ |
+ for (const Directive& directive : active_directives_) { |
+ const TokenInstruction& instruction = directive.token_instruction(); |
+ if (instruction.token_instruction_type() == TRANSMIT) { |
+ tokens_to_update.erase(instruction.token_id()); |
+ |
+ TransmittedToken& token = transmitted_tokens_[instruction.token_id()]; |
+ token.id = instruction.token_id(); |
+ token.medium = instruction.medium(); |
+ token.start_time = base::Time::Now(); |
+ token.stop_time = base::Time::Now() + |
+ base::TimeDelta::FromMilliseconds(directive.ttl_millis()); |
+ |
+ for (CopresenceObserver* observer : observers_) |
+ observer->TokenTransmitted(token); |
+ } |
+ } |
+ |
+ // Tokens not updated above are no longer transmitting. |
+ base::Time now = base::Time::Now(); |
+ for (const std::string& token : tokens_to_update) { |
+ if (transmitted_tokens_[token].stop_time > now) |
+ transmitted_tokens_[token].stop_time = now; |
+ for (CopresenceObserver* observer : observers_) |
+ observer->TokenTransmitted(transmitted_tokens_[token]); |
+ } |
+} |
+ |
+} // namespace copresence |