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

Unified 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, 8 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 side-by-side diff with in-line comments
Download patch
« 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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
« 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