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.
5 #include "components/copresence/handlers/audio/audio_directive_list.h"
11 AudioDirective::AudioDirective() {}
13 AudioDirective::AudioDirective(const std::string
& op_id
,
14 base::TimeTicks end_time
,
15 const Directive
& server_directive
)
18 server_directive(server_directive
) {}
20 AudioDirectiveList::AudioDirectiveList(
21 const scoped_refptr
<TickClockRefCounted
>& clock
)
24 AudioDirectiveList::~AudioDirectiveList() {}
26 void AudioDirectiveList::AddDirective(const std::string
& op_id
,
27 const Directive
& server_directive
) {
28 base::TimeTicks end_time
= clock_
->NowTicks() +
29 base::TimeDelta::FromMilliseconds(server_directive
.ttl_millis());
31 // If this op is already in the list, update it instead of adding it again.
32 auto it
= FindDirectiveByOpId(op_id
);
33 if (it
!= active_directives_
.end()) {
34 it
->end_time
= end_time
;
35 std::make_heap(active_directives_
.begin(),
36 active_directives_
.end(),
37 LatestFirstComparator());
41 active_directives_
.push_back(
42 AudioDirective(op_id
, end_time
, server_directive
));
43 std::push_heap(active_directives_
.begin(),
44 active_directives_
.end(),
45 LatestFirstComparator());
48 void AudioDirectiveList::RemoveDirective(const std::string
& op_id
) {
49 auto it
= FindDirectiveByOpId(op_id
);
50 if (it
!= active_directives_
.end())
51 active_directives_
.erase(it
);
53 std::make_heap(active_directives_
.begin(),
54 active_directives_
.end(),
55 LatestFirstComparator());
58 scoped_ptr
<AudioDirective
> AudioDirectiveList::GetActiveDirective() {
59 // The top is always the instruction that is ending the latest.
60 // If that time has passed, all our previous instructions have expired too.
61 // So we clear the list.
62 if (active_directives_
.empty() ||
63 active_directives_
.front().end_time
< clock_
->NowTicks()) {
64 active_directives_
.clear();
65 return scoped_ptr
<AudioDirective
>().Pass();
68 return make_scoped_ptr(new AudioDirective(active_directives_
.front()));
71 const std::vector
<AudioDirective
>& AudioDirectiveList::directives() const {
72 return active_directives_
;
78 std::vector
<AudioDirective
>::iterator
AudioDirectiveList::FindDirectiveByOpId(
79 const std::string
& op_id
) {
80 for (auto it
= active_directives_
.begin();
81 it
!= active_directives_
.end();
83 if (it
->op_id
== op_id
)
86 return active_directives_
.end();
89 } // namespace copresence