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

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

Issue 764673003: Adding CopresenceState (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "base/logging.h"
6 #include "base/time/time.h"
7 #include "components/copresence/copresence_state_impl.h"
8 #include "components/copresence/proto/data.pb.h"
9 #include "components/copresence/public/copresence_constants.h"
10 #include "components/copresence/public/copresence_observer.h"
11
12 namespace copresence {
13
14 namespace {
15
16 template<typename TokenType>
17 void HandleCommonFields(const TokenType& new_token, TokenType* current_token) {
18 if (current_token->id.empty()) {
19 current_token->id = new_token.id;
20 current_token->medium = new_token.medium;
21 current_token->start_time = new_token.start_time;
22 } else {
23 DCHECK_EQ(new_token.id, current_token->id);
24 DCHECK_EQ(new_token.medium, current_token->medium);
25 DCHECK_EQ(new_token.start_time, current_token->start_time);
26 }
27 }
28
29 void UpdateToken(const TransmittedToken& new_token,
30 TransmittedToken* current_token) {
31 HandleCommonFields(new_token, current_token);
32
33 current_token->stop_time = new_token.stop_time;
34 current_token->broadcast_confirmed = new_token.broadcast_confirmed;
35 }
36
37 void UpdateToken(const ReceivedToken& new_token,
38 ReceivedToken* current_token) {
39 HandleCommonFields(new_token, current_token);
40
41 current_token->last_time = new_token.last_time;
42 if (new_token.valid != ReceivedToken::UNKNOWN)
43 current_token->valid = new_token.valid;
44 }
45
46 } // namespace
47
48
49 // Public functions.
50
51 CopresenceStateImpl::CopresenceStateImpl() {}
52
53 CopresenceStateImpl::~CopresenceStateImpl() {}
54
55 void CopresenceStateImpl::AddObserver(CopresenceObserver* observer) {
56 DCHECK(observer);
57 observers_.AddObserver(observer);
58 }
59
60 void CopresenceStateImpl::RemoveObserver(CopresenceObserver* observer) {
61 DCHECK(observer);
62 observers_.RemoveObserver(observer);
63 }
64
65 const std::vector<Directive>& CopresenceStateImpl::active_directives() const {
66 return active_directives_;
67 }
68
69 const std::map<std::string, TransmittedToken>&
70 CopresenceStateImpl::transmitted_tokens() const {
71 return transmitted_tokens_;
72 }
73
74 const std::map<std::string, ReceivedToken>&
75 CopresenceStateImpl::received_tokens() const {
76 return received_tokens_;
77 }
78
79 // TODO(ckehoe): Only send updates if the directives have really changed.
80 void CopresenceStateImpl::UpdateDirectives(
81 const std::vector<Directive>& directives) {
82 active_directives_ = directives;
83 UpdateTransmittingTokens();
84 FOR_EACH_OBSERVER(CopresenceObserver, observers_, DirectivesUpdated());
85 }
86
87 void CopresenceStateImpl::UpdateTransmittedToken(
88 const TransmittedToken& token) {
89 UpdateToken(token, &transmitted_tokens_[token.id]);
90 FOR_EACH_OBSERVER(CopresenceObserver,
91 observers_,
92 TokenTransmitted(transmitted_tokens_[token.id]));
93 }
94
95 // TODO(ckehoe): Check which tokens are no longer heard and report them lost.
96 void CopresenceStateImpl::UpdateReceivedToken(const ReceivedToken& token) {
97 DCHECK(!token.id.empty());
98
99 // TODO(ckehoe): Have CopresenceManagerImpl::AudioCheck() use this to check
100 // if we can hear our token, and delete the logic from the AudioManager.
101 if (transmitted_tokens_.count(token.id) > 0) {
102 transmitted_tokens_[token.id].broadcast_confirmed = true;
103 FOR_EACH_OBSERVER(CopresenceObserver,
104 observers_,
105 TokenTransmitted(transmitted_tokens_[token.id]));
106 } else {
107 ReceivedToken& stored_token = received_tokens_[token.id];
108 UpdateToken(token, &stored_token);
109
110 // The decoder doesn't track when this token was heard before,
111 // so it should just fill in the last_time.
112 // If we've never seen this token, we populate the start time too.
113 if (stored_token.start_time.is_null())
114 stored_token.start_time = token.last_time;
115
116 FOR_EACH_OBSERVER(CopresenceObserver,
117 observers_,
118 TokenReceived(stored_token));
119 }
120 }
121
122 void CopresenceStateImpl::UpdateTokenStatus(const std::string& token_id,
123 TokenStatus status) {
124 if (transmitted_tokens_.count(token_id) > 0) {
125 LOG_IF(ERROR, status != VALID)
126 << "Broadcast token " << token_id << " is invalid";
127 } else if (received_tokens_.count(token_id) > 0) {
128 received_tokens_[token_id].valid = status == VALID ?
129 ReceivedToken::VALID : ReceivedToken::INVALID;
130 FOR_EACH_OBSERVER(CopresenceObserver,
131 observers_,
132 TokenReceived(received_tokens_[token_id]));
133 } else {
134 LOG(ERROR) << "Got status update for unrecognized token " << token_id;
135 }
136 }
137
138
139 // Private functions.
140
141 void CopresenceStateImpl::UpdateTransmittingTokens() {
142 std::set<std::string> tokens_to_update;
143 for (const auto& token_entry : transmitted_tokens_)
144 tokens_to_update.insert(token_entry.first);
145
146 for (const Directive& directive : active_directives_) {
147 const TokenInstruction& instruction = directive.token_instruction();
148 if (instruction.token_instruction_type() == TRANSMIT) {
149 tokens_to_update.erase(instruction.token_id());
150
151 TransmittedToken& token = transmitted_tokens_[instruction.token_id()];
152 token.id = instruction.token_id();
153 token.medium = instruction.medium();
154 token.start_time = base::Time::Now();
155 token.stop_time = base::Time::Now() +
156 base::TimeDelta::FromMilliseconds(directive.ttl_millis());
157
158 FOR_EACH_OBSERVER(CopresenceObserver,
159 observers_,
160 TokenTransmitted(token));
161 }
162 }
163
164 // Tokens not updated above are no longer transmitting.
165 base::Time now = base::Time::Now();
166 for (const std::string& token : tokens_to_update) {
167 if (transmitted_tokens_[token].stop_time > now)
168 transmitted_tokens_[token].stop_time = now;
169
170 FOR_EACH_OBSERVER(CopresenceObserver,
171 observers_,
172 TokenTransmitted(transmitted_tokens_[token]));
173 }
174 }
175
176 } // namespace copresence
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698