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 namespace media_router { | |
8 | |
9 IssueManager::IssueManager() { | |
10 } | |
11 | |
12 IssueManager::~IssueManager() { | |
13 } | |
14 | |
15 void IssueManager::AddIssue(const Issue& issue) { | |
16 DCHECK(thread_checker_.CalledOnValidThread()); | |
17 for (const Issue& next_issue : issues_) { | |
18 if (next_issue.Equals(issue)) { | |
19 return; | |
20 } | |
21 } | |
22 issues_.push_back(issue); | |
23 MaybeUpdateTopIssue(); | |
24 } | |
25 | |
26 void IssueManager::ClearIssue(const Issue::IssueId& issue_id) { | |
27 DCHECK(thread_checker_.CalledOnValidThread()); | |
28 for (auto it = issues_.begin(); it != issues_.end(); ++it) { | |
DaleCurtis
2015/05/04 20:03:29
You can use std::remove_if() with a lambda for id
Kevin M
2015/05/04 23:40:18
Done.
| |
29 if (it->id() == issue_id) { | |
30 RemoveIssue(it); | |
31 MaybeUpdateTopIssue(); | |
32 return; | |
33 } | |
34 } | |
35 } | |
36 | |
37 size_t IssueManager::GetIssueCount() const { | |
38 DCHECK(thread_checker_.CalledOnValidThread()); | |
39 return issues_.size(); | |
40 } | |
41 | |
42 void IssueManager::ClearAllIssues() { | |
43 DCHECK(thread_checker_.CalledOnValidThread()); | |
44 issues_.clear(); | |
45 MaybeUpdateTopIssue(); | |
46 } | |
47 | |
48 void IssueManager::ClearGlobalIssues() { | |
49 DCHECK(thread_checker_.CalledOnValidThread()); | |
50 auto it = issues_.begin(); | |
51 while (it != issues_.end()) { | |
DaleCurtis
2015/05/04 20:03:29
Ditto.
Kevin M
2015/05/04 23:40:18
Done.
| |
52 if (it->is_global()) { | |
53 it = RemoveIssue(it); | |
54 } else { | |
55 it++; | |
56 } | |
57 } | |
58 MaybeUpdateTopIssue(); | |
59 } | |
60 | |
61 void IssueManager::ClearIssuesWithRouteId(const MediaRouteId& route_id) { | |
62 DCHECK(thread_checker_.CalledOnValidThread()); | |
63 auto it = issues_.begin(); | |
64 while (it != issues_.end()) { | |
65 if (it->route_id() == route_id) { | |
DaleCurtis
2015/05/04 20:03:29
Ditto.
Kevin M
2015/05/04 23:40:18
Done.
| |
66 it = RemoveIssue(it); | |
67 } else { | |
68 it++; | |
69 } | |
70 } | |
71 MaybeUpdateTopIssue(); | |
72 } | |
73 | |
74 void IssueManager::RegisterObserver(IssuesObserver* observer) { | |
75 DCHECK(thread_checker_.CalledOnValidThread()); | |
76 DCHECK(observer); | |
77 DCHECK(!issues_observers_.HasObserver(observer)); | |
78 | |
79 issues_observers_.AddObserver(observer); | |
80 if (!top_issue_id_.empty()) { | |
81 // Find the current top issue and report it to the observer. | |
82 for (const auto& next_issue : issues_) { | |
83 if (next_issue.id() == top_issue_id_) { | |
84 observer->OnIssueUpdated(&next_issue); | |
85 } | |
86 } | |
87 } | |
88 } | |
89 | |
90 void IssueManager::UnregisterObserver(IssuesObserver* observer) { | |
91 DCHECK(thread_checker_.CalledOnValidThread()); | |
92 issues_observers_.RemoveObserver(observer); | |
93 } | |
94 | |
95 std::vector<Issue>::iterator IssueManager::RemoveIssue( | |
96 std::vector<Issue>::iterator issue_it) { | |
97 if (top_issue_id_ == issue_it->id()) { | |
98 top_issue_id_.clear(); | |
99 } | |
100 return issues_.erase(issue_it); | |
101 } | |
102 | |
103 void IssueManager::MaybeUpdateTopIssue() { | |
104 if (issues_.empty()) { | |
105 return; | |
106 } | |
107 | |
108 // Select the first blocking issue in the list of issues. | |
109 // If there are none, simply select the first issue in the list. | |
110 Issue* new_top_issue = &(issues_.front()); | |
111 for (auto it = issues_.begin(); it != issues_.end(); ++it) { | |
112 // The first blocking issue is of higher priority than the first issue. | |
113 if (it->is_blocking()) { | |
114 new_top_issue = &(*it); | |
115 break; | |
116 } | |
117 } | |
118 | |
119 // If we've found a new top issue, then report it via the observer. | |
120 if (new_top_issue->id() != top_issue_id_) { | |
121 top_issue_id_ = new_top_issue->id(); | |
122 FOR_EACH_OBSERVER(IssuesObserver, issues_observers_, | |
123 OnIssueUpdated(new_top_issue)); | |
124 } | |
125 } | |
126 | |
127 } // namespace media_router | |
OLD | NEW |