OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "chrome/browser/media/router/issue_manager.h" | |
6 | |
7 #include <algorithm> | |
8 | |
9 namespace media_router { | |
10 | |
11 IssueManager::IssueManager() { | |
12 } | |
13 | |
14 IssueManager::~IssueManager() { | |
15 } | |
16 | |
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; | |
22 } | |
23 } | |
24 issues_.push_back(issue); | |
25 MaybeUpdateTopIssue(); | |
26 } | |
27 | |
28 void IssueManager::ClearIssue(const Issue::IssueId& 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(); | |
36 } | |
37 | |
38 size_t IssueManager::GetIssueCount() const { | |
39 DCHECK(thread_checker_.CalledOnValidThread()); | |
40 return issues_.size(); | |
41 } | |
42 | |
43 void IssueManager::ClearAllIssues() { | |
44 DCHECK(thread_checker_.CalledOnValidThread()); | |
45 issues_.clear(); | |
46 MaybeUpdateTopIssue(); | |
47 } | |
48 | |
49 void IssueManager::ClearGlobalIssues() { | |
DaleCurtis
2015/05/04 23:48:30
Have you tried calling this when there are no glob
| |
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(); | |
56 } | |
57 | |
58 void IssueManager::ClearIssuesWithRouteId(const MediaRouteId& route_id) { | |
59 DCHECK(thread_checker_.CalledOnValidThread()); | |
60 issues_.erase(std::remove_if(issues_.begin(), issues_.end(), | |
61 [route_id](const Issue& issue) { | |
DaleCurtis
2015/05/04 23:48:30
&route_id ?
| |
62 return route_id == issue.route_id(); | |
63 }), | |
64 issues_.end()); | |
65 MaybeUpdateTopIssue(); | |
66 } | |
67 | |
68 void IssueManager::RegisterObserver(IssuesObserver* observer) { | |
69 DCHECK(thread_checker_.CalledOnValidThread()); | |
70 DCHECK(observer); | |
71 DCHECK(!issues_observers_.HasObserver(observer)); | |
72 | |
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); | |
79 } | |
80 } | |
81 } | |
82 } | |
83 | |
84 void IssueManager::UnregisterObserver(IssuesObserver* observer) { | |
85 DCHECK(thread_checker_.CalledOnValidThread()); | |
86 issues_observers_.RemoveObserver(observer); | |
87 } | |
88 | |
89 void IssueManager::MaybeUpdateTopIssue() { | |
90 if (issues_.empty()) { | |
91 return; | |
92 } | |
93 | |
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; | |
102 } | |
103 } | |
104 | |
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)); | |
110 } | |
111 } | |
112 | |
113 } // namespace media_router | |
OLD | NEW |