Rename GetIconID to GetIconId
[chromium-blink-merge.git] / chrome / browser / media / router / issue_manager.cc
blobe8075fa29ffdbaeeba7a690f46c6c26f4ad5ab8e
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"
7 #include <algorithm>
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)) {
21 return;
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();
33 }),
34 issues_.end());
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());
45 issues_.clear();
46 MaybeUpdateTopIssue();
49 void IssueManager::ClearGlobalIssues() {
50 DCHECK(thread_checker_.CalledOnValidThread());
51 issues_.erase(
52 std::remove_if(issues_.begin(), issues_.end(), [](const Issue& issue) {
53 return issue.is_global();
54 }), issues_.end());
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();
63 }),
64 issues_.end());
65 MaybeUpdateTopIssue();
68 void IssueManager::RegisterObserver(IssuesObserver* observer) {
69 DCHECK(thread_checker_.CalledOnValidThread());
70 DCHECK(observer);
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 if (issues_.empty()) {
91 return;
94 // Select the first blocking issue in the list of issues.
95 // If there are none, simply select the first issue in the list.
96 Issue* new_top_issue = &(issues_.front());
97 for (auto it = issues_.begin(); it != issues_.end(); ++it) {
98 // The first blocking issue is of higher priority than the first issue.
99 if (it->is_blocking()) {
100 new_top_issue = &(*it);
101 break;
105 // If we've found a new top issue, then report it via the observer.
106 if (new_top_issue->id() != top_issue_id_) {
107 top_issue_id_ = new_top_issue->id();
108 FOR_EACH_OBSERVER(IssuesObserver, issues_observers_,
109 OnIssueUpdated(new_top_issue));
113 } // namespace media_router