Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(229)

Side by Side Diff: chrome/browser/media/router/issue_manager.cc

Issue 1103273013: Upstream the Media Router's Issue class. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed feedback, switched manager to std::list, explicitly invalidated top_issue on clear Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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() : top_issue_(nullptr) {
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 issue_iter = issues_.begin(); issue_iter != issues_.end();
29 ++issue_iter) {
30 if (issue_iter->id() == issue_id) {
31 if (top_issue_ && top_issue_->id() == issue_id) {
32 // Invalidate the top issue ptr if we are clearing it now.
33 top_issue_ = nullptr;
34 }
35 issues_.erase(issue_iter);
36 MaybeUpdateTopIssue();
37 return;
38 }
39 }
40 }
41
42 size_t IssueManager::GetIssueCount() const {
43 DCHECK(thread_checker_.CalledOnValidThread());
44 return issues_.size();
45 }
46
47 void IssueManager::ClearAllIssues() {
48 DCHECK(thread_checker_.CalledOnValidThread());
49 issues_.clear();
50 MaybeUpdateTopIssue();
51 }
52
53 void IssueManager::ClearGlobalIssues() {
54 DCHECK(thread_checker_.CalledOnValidThread());
55 auto it = issues_.begin();
56 while (it != issues_.end()) {
57 if (it->is_global()) {
58 if (top_issue_ && it->Equals(*top_issue_)) {
59 top_issue_ = nullptr;
apacible 2015/05/01 20:18:08 Do we need a comment about invalidating top_issue_
Kevin Marshall 2015/05/01 20:26:03 Done.
60 }
61 issues_.erase(it++);
62 } else {
63 ++it;
64 }
65 }
66 MaybeUpdateTopIssue();
67 }
68
69 void IssueManager::ClearIssuesWithRouteId(const MediaRouteId& route_id) {
70 DCHECK(thread_checker_.CalledOnValidThread());
71 auto it = issues_.begin();
72 while (it != issues_.end()) {
73 if (it->route_id() == route_id) {
74 if (top_issue_ && it->Equals(*top_issue_)) {
75 top_issue_ = nullptr;
76 }
77 issues_.erase(it++);
78 } else {
79 ++it;
80 }
81 }
82 MaybeUpdateTopIssue();
83 }
84
85 void IssueManager::RegisterObserver(IssuesObserver* observer) {
86 DCHECK(thread_checker_.CalledOnValidThread());
87 DCHECK(observer);
88 DCHECK(!issues_observers_.HasObserver(observer));
89
90 issues_observers_.AddObserver(observer);
91 if (top_issue_)
92 observer->OnIssueUpdated(top_issue_);
93 }
94
95 void IssueManager::UnregisterObserver(IssuesObserver* observer) {
96 DCHECK(thread_checker_.CalledOnValidThread());
97 issues_observers_.RemoveObserver(observer);
98 }
99
100 void IssueManager::MaybeUpdateTopIssue() {
101 if (issues_.empty()) {
102 return;
103 }
104
105 // Select the first blocking issue in the list of issues.
106 // If there are none, simply select the first issue in the list.
107 Issue* new_top_issue = &(issues_.front());
108 for (auto it = issues_.begin(); it != issues_.end(); ++it) {
109 // The first blocking issue is of higher priority than the first issue.
110 if (it->is_blocking()) {
111 new_top_issue = &(*it);
112 break;
113 }
114 }
115
116 // If we've found a new top issue, then report it via the observer.
117 if (!top_issue_ || !new_top_issue->Equals(*top_issue_)) {
118 top_issue_ = new_top_issue;
119 FOR_EACH_OBSERVER(IssuesObserver, issues_observers_,
120 OnIssueUpdated(top_issue_));
121 }
122 }
123
124 } // namespace media_router
OLDNEW
« no previous file with comments | « chrome/browser/media/router/issue_manager.h ('k') | chrome/browser/media/router/issue_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698