1 // Copyright 2015 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 "chrome/browser/media/router/issue_manager.h"
9 namespace media_router
{
11 IssueManager::IssueManager() {
14 IssueManager::~IssueManager() {
17 void IssueManager::AddIssue(const Issue
& issue
) {
18 DCHECK(thread_checker_
.CalledOnValidThread());
19 for (const Issue
& next_issue
: issues_
) {
20 if (next_issue
.Equals(issue
)) {
24 issues_
.push_back(issue
);
25 MaybeUpdateTopIssue();
28 void IssueManager::ClearIssue(const Issue::Id
& issue_id
) {
29 DCHECK(thread_checker_
.CalledOnValidThread());
30 issues_
.erase(std::remove_if(issues_
.begin(), issues_
.end(),
31 [&issue_id
](const Issue
& issue
) {
32 return issue_id
== issue
.id();
35 MaybeUpdateTopIssue();
38 size_t IssueManager::GetIssueCount() const {
39 DCHECK(thread_checker_
.CalledOnValidThread());
40 return issues_
.size();
43 void IssueManager::ClearAllIssues() {
44 DCHECK(thread_checker_
.CalledOnValidThread());
46 MaybeUpdateTopIssue();
49 void IssueManager::ClearGlobalIssues() {
50 DCHECK(thread_checker_
.CalledOnValidThread());
52 std::remove_if(issues_
.begin(), issues_
.end(), [](const Issue
& issue
) {
53 return issue
.is_global();
55 MaybeUpdateTopIssue();
58 void IssueManager::ClearIssuesWithRouteId(const MediaRoute::Id
& route_id
) {
59 DCHECK(thread_checker_
.CalledOnValidThread());
60 issues_
.erase(std::remove_if(issues_
.begin(), issues_
.end(),
61 [&route_id
](const Issue
& issue
) {
62 return route_id
== issue
.route_id();
65 MaybeUpdateTopIssue();
68 void IssueManager::RegisterObserver(IssuesObserver
* observer
) {
69 DCHECK(thread_checker_
.CalledOnValidThread());
71 DCHECK(!issues_observers_
.HasObserver(observer
));
73 issues_observers_
.AddObserver(observer
);
74 if (!top_issue_id_
.empty()) {
75 // Find the current top issue and report it to the observer.
76 for (const auto& next_issue
: issues_
) {
77 if (next_issue
.id() == top_issue_id_
) {
78 observer
->OnIssueUpdated(&next_issue
);
84 void IssueManager::UnregisterObserver(IssuesObserver
* observer
) {
85 DCHECK(thread_checker_
.CalledOnValidThread());
86 issues_observers_
.RemoveObserver(observer
);
89 void IssueManager::MaybeUpdateTopIssue() {
90 Issue
* new_top_issue
= nullptr;
92 if (issues_
.empty()) {
93 FOR_EACH_OBSERVER(IssuesObserver
, issues_observers_
,
94 OnIssueUpdated(new_top_issue
));
98 // Select the first blocking issue in the list of issues.
99 // If there are none, simply select the first issue in the list.
100 new_top_issue
= &(issues_
.front());
101 for (auto it
= issues_
.begin(); it
!= issues_
.end(); ++it
) {
102 // The first blocking issue is of higher priority than the first issue.
103 if (it
->is_blocking()) {
104 new_top_issue
= &(*it
);
109 // If we've found a new top issue, then report it via the observer.
110 if (new_top_issue
->id() != top_issue_id_
) {
111 top_issue_id_
= new_top_issue
->id();
112 FOR_EACH_OBSERVER(IssuesObserver
, issues_observers_
,
113 OnIssueUpdated(new_top_issue
));
117 } // namespace media_router