Chromium Code Reviews| Index: chrome/browser/media/router/issue_manager.cc |
| diff --git a/chrome/browser/media/router/issue_manager.cc b/chrome/browser/media/router/issue_manager.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..cc606b543594764444f3b6e672ac463d5e8a16a6 |
| --- /dev/null |
| +++ b/chrome/browser/media/router/issue_manager.cc |
| @@ -0,0 +1,124 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/media/router/issue_manager.h" |
| + |
| +namespace media_router { |
| + |
| +IssueManager::IssueManager() : top_issue_(nullptr) { |
| +} |
| + |
| +IssueManager::~IssueManager() { |
| +} |
| + |
| +void IssueManager::AddIssue(const Issue& issue) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + for (const Issue& next_issue : issues_) { |
| + if (next_issue.Equals(issue)) { |
| + return; |
| + } |
| + } |
| + issues_.push_back(issue); |
| + MaybeUpdateTopIssue(); |
| +} |
| + |
| +void IssueManager::ClearIssue(const Issue::IssueId& issue_id) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + for (auto issue_iter = issues_.begin(); issue_iter != issues_.end(); |
| + ++issue_iter) { |
| + if (issue_iter->id() == issue_id) { |
| + if (top_issue_ && top_issue_->id() == issue_id) { |
| + // Invalidate the top issue ptr if we are clearing it now. |
| + top_issue_ = nullptr; |
| + } |
| + issues_.erase(issue_iter); |
| + MaybeUpdateTopIssue(); |
| + return; |
| + } |
| + } |
| +} |
| + |
| +size_t IssueManager::GetIssueCount() const { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + return issues_.size(); |
| +} |
| + |
| +void IssueManager::ClearAllIssues() { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + issues_.clear(); |
| + MaybeUpdateTopIssue(); |
| +} |
| + |
| +void IssueManager::ClearGlobalIssues() { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + auto it = issues_.begin(); |
| + while (it != issues_.end()) { |
| + if (it->is_global()) { |
| + if (top_issue_ && it->Equals(*top_issue_)) { |
| + 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.
|
| + } |
| + issues_.erase(it++); |
| + } else { |
| + ++it; |
| + } |
| + } |
| + MaybeUpdateTopIssue(); |
| +} |
| + |
| +void IssueManager::ClearIssuesWithRouteId(const MediaRouteId& route_id) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + auto it = issues_.begin(); |
| + while (it != issues_.end()) { |
| + if (it->route_id() == route_id) { |
| + if (top_issue_ && it->Equals(*top_issue_)) { |
| + top_issue_ = nullptr; |
| + } |
| + issues_.erase(it++); |
| + } else { |
| + ++it; |
| + } |
| + } |
| + MaybeUpdateTopIssue(); |
| +} |
| + |
| +void IssueManager::RegisterObserver(IssuesObserver* observer) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + DCHECK(observer); |
| + DCHECK(!issues_observers_.HasObserver(observer)); |
| + |
| + issues_observers_.AddObserver(observer); |
| + if (top_issue_) |
| + observer->OnIssueUpdated(top_issue_); |
| +} |
| + |
| +void IssueManager::UnregisterObserver(IssuesObserver* observer) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + issues_observers_.RemoveObserver(observer); |
| +} |
| + |
| +void IssueManager::MaybeUpdateTopIssue() { |
| + if (issues_.empty()) { |
| + return; |
| + } |
| + |
| + // Select the first blocking issue in the list of issues. |
| + // If there are none, simply select the first issue in the list. |
| + Issue* new_top_issue = &(issues_.front()); |
| + for (auto it = issues_.begin(); it != issues_.end(); ++it) { |
| + // The first blocking issue is of higher priority than the first issue. |
| + if (it->is_blocking()) { |
| + new_top_issue = &(*it); |
| + break; |
| + } |
| + } |
| + |
| + // If we've found a new top issue, then report it via the observer. |
| + if (!top_issue_ || !new_top_issue->Equals(*top_issue_)) { |
| + top_issue_ = new_top_issue; |
| + FOR_EACH_OBSERVER(IssuesObserver, issues_observers_, |
| + OnIssueUpdated(top_issue_)); |
| + } |
| +} |
| + |
| +} // namespace media_router |