| OLD | NEW |
| 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 "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
| 10 | 10 |
| 11 namespace media_router { | 11 namespace media_router { |
| 12 | 12 |
| 13 IssueManager::IssueManager() { | 13 IssueManager::IssueManager() : top_issue_(nullptr) { |
| 14 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 14 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 15 } | 15 } |
| 16 | 16 |
| 17 IssueManager::~IssueManager() { | 17 IssueManager::~IssueManager() { |
| 18 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 18 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 19 } | 19 } |
| 20 | 20 |
| 21 void IssueManager::AddIssue(const Issue& issue) { | 21 void IssueManager::AddIssue(const Issue& issue) { |
| 22 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 22 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 23 for (const Issue& next_issue : issues_) { | 23 if (ContainsValue(issues_, issue)) |
| 24 if (next_issue.Equals(issue)) { | 24 return; |
| 25 return; | 25 |
| 26 } | |
| 27 } | |
| 28 issues_.push_back(issue); | 26 issues_.push_back(issue); |
| 29 MaybeUpdateTopIssue(); | 27 MaybeUpdateTopIssue(); |
| 30 } | 28 } |
| 31 | 29 |
| 32 void IssueManager::ClearIssue(const Issue::Id& issue_id) { | 30 void IssueManager::ClearIssue(const Issue::Id& issue_id) { |
| 33 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 31 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 34 issues_.erase(std::remove_if(issues_.begin(), issues_.end(), | 32 issues_.erase(std::remove_if(issues_.begin(), issues_.end(), |
| 35 [&issue_id](const Issue& issue) { | 33 [&issue_id](const Issue& issue) { |
| 36 return issue_id == issue.id(); | 34 return issue_id == issue.id(); |
| 37 }), | 35 }), |
| 38 issues_.end()); | 36 issues_.end()); |
| 39 MaybeUpdateTopIssue(); | 37 MaybeUpdateTopIssue(); |
| 40 } | 38 } |
| 41 | 39 |
| 42 size_t IssueManager::GetIssueCount() const { | 40 size_t IssueManager::GetIssueCount() const { |
| 43 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 41 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 44 return issues_.size(); | 42 return issues_.size(); |
| 45 } | 43 } |
| 46 | 44 |
| 47 void IssueManager::ClearAllIssues() { | 45 void IssueManager::ClearAllIssues() { |
| 48 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 46 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 49 issues_.clear(); | 47 issues_.clear(); |
| 50 MaybeUpdateTopIssue(); | 48 MaybeUpdateTopIssue(); |
| 51 } | 49 } |
| 52 | 50 |
| 53 void IssueManager::ClearGlobalIssues() { | 51 void IssueManager::ClearGlobalIssues() { |
| 54 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 52 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 55 issues_.erase( | 53 issues_.erase( |
| 56 std::remove_if(issues_.begin(), issues_.end(), [](const Issue& issue) { | 54 std::remove_if(issues_.begin(), issues_.end(), |
| 57 return issue.is_global(); | 55 [](const Issue& issue) { return issue.IsGlobal(); }), |
| 58 }), issues_.end()); | 56 issues_.end()); |
| 59 MaybeUpdateTopIssue(); | 57 MaybeUpdateTopIssue(); |
| 60 } | 58 } |
| 61 | 59 |
| 62 void IssueManager::ClearIssuesWithRouteId(const MediaRoute::Id& route_id) { | 60 void IssueManager::ClearIssuesWithRouteId(const MediaRoute::Id& route_id) { |
| 63 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 61 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 64 issues_.erase(std::remove_if(issues_.begin(), issues_.end(), | 62 issues_.erase(std::remove_if(issues_.begin(), issues_.end(), |
| 65 [&route_id](const Issue& issue) { | 63 [&route_id](const Issue& issue) { |
| 66 return route_id == issue.route_id(); | 64 return route_id == issue.route_id(); |
| 67 }), | 65 }), |
| 68 issues_.end()); | 66 issues_.end()); |
| 69 MaybeUpdateTopIssue(); | 67 MaybeUpdateTopIssue(); |
| 70 } | 68 } |
| 71 | 69 |
| 72 void IssueManager::RegisterObserver(IssuesObserver* observer) { | 70 void IssueManager::RegisterObserver(IssuesObserver* observer) { |
| 73 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 71 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 74 DCHECK(observer); | 72 DCHECK(observer); |
| 75 DCHECK(!issues_observers_.HasObserver(observer)); | 73 DCHECK(!issues_observers_.HasObserver(observer)); |
| 76 | 74 |
| 77 issues_observers_.AddObserver(observer); | 75 issues_observers_.AddObserver(observer); |
| 78 if (top_issue_id_.empty()) | 76 if (top_issue_) |
| 79 return; | 77 observer->OnIssueUpdated(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 } | 78 } |
| 88 | 79 |
| 89 void IssueManager::UnregisterObserver(IssuesObserver* observer) { | 80 void IssueManager::UnregisterObserver(IssuesObserver* observer) { |
| 90 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 81 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 91 issues_observers_.RemoveObserver(observer); | 82 issues_observers_.RemoveObserver(observer); |
| 92 } | 83 } |
| 93 | 84 |
| 94 void IssueManager::MaybeUpdateTopIssue() { | 85 void IssueManager::MaybeUpdateTopIssue() { |
| 95 const Issue* new_top_issue = nullptr; | 86 const Issue* new_top_issue = nullptr; |
| 87 if (!issues_.empty()) { |
| 88 // Select the first blocking issue in the list of issues. |
| 89 // If there are none, simply select the first issue in the list. |
| 90 auto it = |
| 91 std::find_if(issues_.begin(), issues_.end(), |
| 92 [](const Issue& issue) { return issue.IsBlocking(); }); |
| 93 if (it == issues_.end()) |
| 94 it = issues_.begin(); |
| 96 | 95 |
| 97 if (issues_.empty()) { | 96 new_top_issue = &*it; |
| 97 } |
| 98 // If we've found a new top issue, then report it via the observer. |
| 99 if (new_top_issue != top_issue_) { |
| 100 top_issue_ = new_top_issue; |
| 98 FOR_EACH_OBSERVER(IssuesObserver, issues_observers_, | 101 FOR_EACH_OBSERVER(IssuesObserver, issues_observers_, |
| 99 OnIssueUpdated(new_top_issue)); | 102 OnIssueUpdated(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 } | |
| 113 | |
| 114 // If we've found a new top issue, then report it via the observer. | |
| 115 if (new_top_issue->id() != top_issue_id_) { | |
| 116 top_issue_id_ = new_top_issue->id(); | |
| 117 FOR_EACH_OBSERVER(IssuesObserver, issues_observers_, | |
| 118 OnIssueUpdated(new_top_issue)); | |
| 119 } | 103 } |
| 120 } | 104 } |
| 121 | 105 |
| 122 } // namespace media_router | 106 } // namespace media_router |
| OLD | NEW |