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

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

Issue 2176613003: [Media Router] Clean up issues related code. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years 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
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/media/router/issue_manager.h" 5 #include "chrome/browser/media/router/issue_manager.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/memory/ptr_util.h"
dcheng 2016/12/07 18:58:02 Nit: remove this, as it's unused
imcheng 2016/12/09 23:19:53 Done.
9 #include "content/public/browser/browser_thread.h" 10 #include "content/public/browser/browser_thread.h"
10 11
11 namespace media_router { 12 namespace media_router {
12 13
13 IssueManager::IssueManager() { 14 IssueManager::IssueManager() : top_issue_(nullptr) {
14 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 15 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
15 } 16 }
16 17
17 IssueManager::~IssueManager() { 18 IssueManager::~IssueManager() {
18 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 19 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
19 } 20 }
20 21
21 void IssueManager::AddIssue(const Issue& issue) { 22 void IssueManager::AddIssue(const IssueInfo& issue_info) {
22 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 23 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
23 for (const Issue& next_issue : issues_) { 24 auto it = std::find_if(
24 if (next_issue.Equals(issue)) { 25 issues_.begin(), issues_.end(),
25 return; 26 [&issue_info](const Issue& issue) { return issue_info == issue.info(); });
26 } 27 if (it != issues_.end())
27 } 28 return;
28 issues_.push_back(issue); 29
30 issues_.push_back(Issue(issue_info));
29 MaybeUpdateTopIssue(); 31 MaybeUpdateTopIssue();
30 } 32 }
31 33
32 void IssueManager::ClearIssue(const Issue::Id& issue_id) { 34 void IssueManager::ClearIssue(const Issue::Id& issue_id) {
33 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 35 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
34 issues_.erase(std::remove_if(issues_.begin(), issues_.end(), 36 issues_.erase(std::remove_if(issues_.begin(), issues_.end(),
35 [&issue_id](const Issue& issue) { 37 [&issue_id](const Issue& issue) {
36 return issue_id == issue.id(); 38 return issue_id == issue.id();
37 }), 39 }),
38 issues_.end()); 40 issues_.end());
39 MaybeUpdateTopIssue(); 41 MaybeUpdateTopIssue();
40 } 42 }
41 43
42 size_t IssueManager::GetIssueCount() const {
43 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
44 return issues_.size();
45 }
46
47 void IssueManager::ClearAllIssues() {
48 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
49 issues_.clear();
50 MaybeUpdateTopIssue();
51 }
52
53 void IssueManager::ClearGlobalIssues() {
54 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
55 issues_.erase(
56 std::remove_if(issues_.begin(), issues_.end(), [](const Issue& issue) {
57 return issue.is_global();
58 }), issues_.end());
59 MaybeUpdateTopIssue();
60 }
61
62 void IssueManager::ClearIssuesWithRouteId(const MediaRoute::Id& route_id) {
63 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
64 issues_.erase(std::remove_if(issues_.begin(), issues_.end(),
65 [&route_id](const Issue& issue) {
66 return route_id == issue.route_id();
67 }),
68 issues_.end());
69 MaybeUpdateTopIssue();
70 }
71
72 void IssueManager::RegisterObserver(IssuesObserver* observer) { 44 void IssueManager::RegisterObserver(IssuesObserver* observer) {
73 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 45 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
74 DCHECK(observer); 46 DCHECK(observer);
75 DCHECK(!issues_observers_.HasObserver(observer)); 47 DCHECK(!issues_observers_.HasObserver(observer));
76 48
77 issues_observers_.AddObserver(observer); 49 issues_observers_.AddObserver(observer);
78 if (top_issue_id_.empty()) 50 if (top_issue_)
79 return; 51 observer->OnIssue(*top_issue_);
80
81 // Find the current top issue and report it to the observer.
82 for (const auto& next_issue : issues_) {
83 if (next_issue.id() == top_issue_id_) {
84 observer->OnIssueUpdated(&next_issue);
85 }
86 }
87 } 52 }
88 53
89 void IssueManager::UnregisterObserver(IssuesObserver* observer) { 54 void IssueManager::UnregisterObserver(IssuesObserver* observer) {
90 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 55 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
91 issues_observers_.RemoveObserver(observer); 56 issues_observers_.RemoveObserver(observer);
92 } 57 }
93 58
94 void IssueManager::MaybeUpdateTopIssue() { 59 void IssueManager::MaybeUpdateTopIssue() {
95 const Issue* new_top_issue = nullptr; 60 const Issue* new_top_issue = nullptr;
61 if (!issues_.empty()) {
62 // Select the first blocking issue in the list of issues.
63 // If there are none, simply select the first issue in the list.
64 auto it = std::find_if(
65 issues_.begin(), issues_.end(),
66 [](const Issue& issue) { return issue.info().is_blocking; });
67 if (it == issues_.end())
68 it = issues_.begin();
96 69
97 if (issues_.empty()) { 70 new_top_issue = &*it;
98 for (auto& observer : issues_observers_)
99 observer.OnIssueUpdated(new_top_issue);
100 return;
101 }
102
103 // Select the first blocking issue in the list of issues.
104 // If there are none, simply select the first issue in the list.
105 new_top_issue = &(issues_.front());
106 for (const auto& issue : issues_) {
107 // The first blocking issue is of higher priority than the first issue.
108 if (issue.is_blocking()) {
109 new_top_issue = &issue;
110 break;
111 }
112 } 71 }
113 72
114 // If we've found a new top issue, then report it via the observer. 73 // If we've found a new top issue, then report it via the observer.
115 if (new_top_issue->id() != top_issue_id_) { 74 if (new_top_issue != top_issue_) {
116 top_issue_id_ = new_top_issue->id(); 75 top_issue_ = new_top_issue;
117 for (auto& observer : issues_observers_) 76 for (auto& observer : issues_observers_) {
118 observer.OnIssueUpdated(new_top_issue); 77 if (top_issue_)
78 observer.OnIssue(*top_issue_);
79 else
80 observer.OnIssuesCleared();
81 }
119 } 82 }
120 } 83 }
121 84
122 } // namespace media_router 85 } // namespace media_router
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698