Chromium Code Reviews| 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() : 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) { | |
|
DaleCurtis
2015/05/02 18:13:32
Seems like this could be a helper function since a
Kevin M
2015/05/04 18:45:48
Done.
| |
| 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 // Invalidate the top issue ptr if we are clearing it now. | |
| 60 top_issue_ = nullptr; | |
| 61 } | |
| 62 issues_.erase(it++); | |
| 63 } else { | |
| 64 ++it; | |
| 65 } | |
| 66 } | |
| 67 MaybeUpdateTopIssue(); | |
| 68 } | |
| 69 | |
| 70 void IssueManager::ClearIssuesWithRouteId(const MediaRouteId& route_id) { | |
| 71 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 72 auto it = issues_.begin(); | |
| 73 while (it != issues_.end()) { | |
| 74 if (it->route_id() == route_id) { | |
| 75 if (top_issue_ && it->Equals(*top_issue_)) { | |
| 76 // Invalidate the top issue ptr if we are clearing it now. | |
| 77 top_issue_ = nullptr; | |
| 78 } | |
| 79 issues_.erase(it++); | |
| 80 } else { | |
| 81 ++it; | |
| 82 } | |
| 83 } | |
| 84 MaybeUpdateTopIssue(); | |
| 85 } | |
| 86 | |
| 87 void IssueManager::RegisterObserver(IssuesObserver* observer) { | |
| 88 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 89 DCHECK(observer); | |
| 90 DCHECK(!issues_observers_.HasObserver(observer)); | |
| 91 | |
| 92 issues_observers_.AddObserver(observer); | |
| 93 if (top_issue_) | |
| 94 observer->OnIssueUpdated(top_issue_); | |
| 95 } | |
| 96 | |
| 97 void IssueManager::UnregisterObserver(IssuesObserver* observer) { | |
| 98 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 99 issues_observers_.RemoveObserver(observer); | |
| 100 } | |
| 101 | |
| 102 void IssueManager::MaybeUpdateTopIssue() { | |
| 103 if (issues_.empty()) { | |
| 104 return; | |
| 105 } | |
| 106 | |
| 107 // Select the first blocking issue in the list of issues. | |
| 108 // If there are none, simply select the first issue in the list. | |
| 109 Issue* new_top_issue = &(issues_.front()); | |
| 110 for (auto it = issues_.begin(); it != issues_.end(); ++it) { | |
| 111 // The first blocking issue is of higher priority than the first issue. | |
| 112 if (it->is_blocking()) { | |
| 113 new_top_issue = &(*it); | |
| 114 break; | |
| 115 } | |
| 116 } | |
| 117 | |
| 118 // If we've found a new top issue, then report it via the observer. | |
| 119 if (!top_issue_ || !new_top_issue->Equals(*top_issue_)) { | |
| 120 top_issue_ = new_top_issue; | |
| 121 FOR_EACH_OBSERVER(IssuesObserver, issues_observers_, | |
| 122 OnIssueUpdated(top_issue_)); | |
| 123 } | |
| 124 } | |
| 125 | |
| 126 } // namespace media_router | |
| OLD | NEW |