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..6732163595137641d41cfee5e7a7bcaebd18c6fb |
--- /dev/null |
+++ b/chrome/browser/media/router/issue_manager.cc |
@@ -0,0 +1,127 @@ |
+// 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() { |
+} |
+ |
+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 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.
|
+ if (it->id() == issue_id) { |
+ RemoveIssue(it); |
+ 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()) { |
DaleCurtis
2015/05/04 20:03:29
Ditto.
Kevin M
2015/05/04 23:40:18
Done.
|
+ if (it->is_global()) { |
+ it = RemoveIssue(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) { |
DaleCurtis
2015/05/04 20:03:29
Ditto.
Kevin M
2015/05/04 23:40:18
Done.
|
+ it = RemoveIssue(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_id_.empty()) { |
+ // Find the current top issue and report it to the observer. |
+ for (const auto& next_issue : issues_) { |
+ if (next_issue.id() == top_issue_id_) { |
+ observer->OnIssueUpdated(&next_issue); |
+ } |
+ } |
+ } |
+} |
+ |
+void IssueManager::UnregisterObserver(IssuesObserver* observer) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ issues_observers_.RemoveObserver(observer); |
+} |
+ |
+std::vector<Issue>::iterator IssueManager::RemoveIssue( |
+ std::vector<Issue>::iterator issue_it) { |
+ if (top_issue_id_ == issue_it->id()) { |
+ top_issue_id_.clear(); |
+ } |
+ return issues_.erase(issue_it); |
+} |
+ |
+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 (new_top_issue->id() != top_issue_id_) { |
+ top_issue_id_ = new_top_issue->id(); |
+ FOR_EACH_OBSERVER(IssuesObserver, issues_observers_, |
+ OnIssueUpdated(new_top_issue)); |
+ } |
+} |
+ |
+} // namespace media_router |