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

Side by Side Diff: components/copresence/handlers/audio/audio_directive_list.cc

Issue 690213004: Refactoring AudioDirectiveHandler to support testing (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/handlers/audio/audio_directive_list.h" 5 #include "components/copresence/handlers/audio/audio_directive_list.h"
6 6
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/time/default_tick_clock.h"
11 #include "base/time/time.h"
12 #include "components/copresence/handlers/audio/tick_clock_ref_counted.h"
13
14 namespace copresence { 7 namespace copresence {
15 8
16 // Public methods. 9 // Public functions.
17 10
18 AudioDirective::AudioDirective() { 11 AudioDirective::AudioDirective() {}
19 }
20 12
21 AudioDirective::AudioDirective(const std::string& op_id, 13 AudioDirective::AudioDirective(const std::string& op_id,
22 base::TimeTicks end_time) 14 base::TimeTicks end_time)
23 : op_id(op_id), end_time(end_time) { 15 : op_id(op_id), end_time(end_time) {}
24 }
25 16
26 AudioDirectiveList::AudioDirectiveList() 17 AudioDirectiveList::AudioDirectiveList(
27 : clock_(new TickClockRefCounted( 18 const scoped_refptr<TickClockRefCounted>& clock)
28 make_scoped_ptr(new base::DefaultTickClock))) { 19 : clock_(clock) {}
29 }
30 20
31 AudioDirectiveList::~AudioDirectiveList() { 21 AudioDirectiveList::~AudioDirectiveList() {}
32 }
33 22
34 void AudioDirectiveList::AddDirective(const std::string& op_id, 23 void AudioDirectiveList::AddDirective(const std::string& op_id,
35 base::TimeDelta ttl) { 24 base::TimeDelta ttl) {
36 base::TimeTicks end_time = clock_->NowTicks() + ttl; 25 base::TimeTicks end_time = clock_->NowTicks() + ttl;
37 26
38 // In case this op is already in the list, update it instead of adding 27 // If this op is already in the list, update it instead of adding it again.
39 // it again. 28 auto it = FindDirectiveByOpId(op_id);
40 std::vector<AudioDirective>::iterator it = FindDirectiveByOpId(op_id);
41 if (it != active_directives_.end()) { 29 if (it != active_directives_.end()) {
42 it->end_time = end_time; 30 it->end_time = end_time;
43 std::make_heap(active_directives_.begin(), 31 std::make_heap(active_directives_.begin(),
44 active_directives_.end(), 32 active_directives_.end(),
45 LatestFirstComparator()); 33 LatestFirstComparator());
46 return; 34 return;
47 } 35 }
48 36
49 active_directives_.push_back(AudioDirective(op_id, end_time)); 37 active_directives_.push_back(AudioDirective(op_id, end_time));
50 std::push_heap(active_directives_.begin(), 38 std::push_heap(active_directives_.begin(),
51 active_directives_.end(), 39 active_directives_.end(),
52 LatestFirstComparator()); 40 LatestFirstComparator());
53 } 41 }
54 42
55 void AudioDirectiveList::RemoveDirective(const std::string& op_id) { 43 void AudioDirectiveList::RemoveDirective(const std::string& op_id) {
56 std::vector<AudioDirective>::iterator it = FindDirectiveByOpId(op_id); 44 auto it = FindDirectiveByOpId(op_id);
57 if (it != active_directives_.end()) 45 if (it != active_directives_.end())
58 active_directives_.erase(it); 46 active_directives_.erase(it);
59 47
60 std::make_heap(active_directives_.begin(), 48 std::make_heap(active_directives_.begin(),
61 active_directives_.end(), 49 active_directives_.end(),
62 LatestFirstComparator()); 50 LatestFirstComparator());
63 } 51 }
64 52
65 scoped_ptr<AudioDirective> AudioDirectiveList::GetActiveDirective() { 53 scoped_ptr<AudioDirective> AudioDirectiveList::GetActiveDirective() {
66 // The top is always the instruction that is ending the latest. If that time 54 // The top is always the instruction that is ending the latest.
67 // has passed, means all our previous instructions have expired too, hence 55 // If that time has passed, all our previous instructions have expired too.
68 // clear the list. 56 // So we clear the list.
69 if (!active_directives_.empty() && 57 if (active_directives_.empty() ||
70 active_directives_.front().end_time < clock_->NowTicks()) { 58 active_directives_.front().end_time < clock_->NowTicks()) {
71 active_directives_.clear(); 59 active_directives_.clear();
60 return scoped_ptr<AudioDirective>().Pass();
72 } 61 }
73 62
74 if (active_directives_.empty())
75 return scoped_ptr<AudioDirective>().Pass();
76
77 return make_scoped_ptr(new AudioDirective(active_directives_.front())); 63 return make_scoped_ptr(new AudioDirective(active_directives_.front()));
78 } 64 }
79 65
80 void AudioDirectiveList::set_clock_for_testing(
81 const scoped_refptr<TickClockRefCounted>& clock) {
82 clock_ = clock;
83 }
84 66
85 // Private methods. 67 // Private functions.
86 68
87 std::vector<AudioDirective>::iterator AudioDirectiveList::FindDirectiveByOpId( 69 std::vector<AudioDirective>::iterator AudioDirectiveList::FindDirectiveByOpId(
88 const std::string& op_id) { 70 const std::string& op_id) {
89 for (std::vector<AudioDirective>::iterator it = active_directives_.begin(); 71 for (auto it = active_directives_.begin();
90 it != active_directives_.end(); 72 it != active_directives_.end();
91 ++it) { 73 ++it) {
92 if (it->op_id == op_id) 74 if (it->op_id == op_id)
93 return it; 75 return it;
94 } 76 }
95 return active_directives_.end(); 77 return active_directives_.end();
96 } 78 }
97 79
98 } // namespace copresence 80 } // namespace copresence
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698