| 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 "base/memory/ptr_util.h" |
| 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(issues_.begin(), issues_.end(), |
| 24 if (next_issue.Equals(issue)) { | 25 [&issue_info](const std::unique_ptr<Issue>& issue) { |
| 25 return; | 26 return issue_info == issue->info(); |
| 26 } | 27 }); |
| 27 } | 28 if (it != issues_.end()) |
| 28 issues_.push_back(issue); | 29 return; |
| 30 |
| 31 issues_.push_back(base::MakeUnique<Issue>(issue_info)); |
| 29 MaybeUpdateTopIssue(); | 32 MaybeUpdateTopIssue(); |
| 30 } | 33 } |
| 31 | 34 |
| 32 void IssueManager::ClearIssue(const Issue::Id& issue_id) { | 35 void IssueManager::ClearIssue(const Issue::Id& issue_id) { |
| 33 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 36 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 34 issues_.erase(std::remove_if(issues_.begin(), issues_.end(), | 37 issues_.erase( |
| 35 [&issue_id](const Issue& issue) { | 38 std::remove_if(issues_.begin(), issues_.end(), |
| 36 return issue_id == issue.id(); | 39 [&issue_id](const std::unique_ptr<Issue>& issue) { |
| 37 }), | 40 return issue_id == issue->id(); |
| 38 issues_.end()); | 41 }), |
| 42 issues_.end()); |
| 39 MaybeUpdateTopIssue(); | 43 MaybeUpdateTopIssue(); |
| 40 } | 44 } |
| 41 | 45 |
| 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) { | 46 void IssueManager::RegisterObserver(IssuesObserver* observer) { |
| 73 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 47 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 74 DCHECK(observer); | 48 DCHECK(observer); |
| 75 DCHECK(!issues_observers_.HasObserver(observer)); | 49 DCHECK(!issues_observers_.HasObserver(observer)); |
| 76 | 50 |
| 77 issues_observers_.AddObserver(observer); | 51 issues_observers_.AddObserver(observer); |
| 78 if (top_issue_id_.empty()) | 52 MaybeUpdateTopIssue(); |
| 79 return; | 53 if (top_issue_) |
| 80 | 54 observer->OnIssue(*top_issue_); |
| 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 } | 55 } |
| 88 | 56 |
| 89 void IssueManager::UnregisterObserver(IssuesObserver* observer) { | 57 void IssueManager::UnregisterObserver(IssuesObserver* observer) { |
| 90 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 58 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 91 issues_observers_.RemoveObserver(observer); | 59 issues_observers_.RemoveObserver(observer); |
| 92 } | 60 } |
| 93 | 61 |
| 94 void IssueManager::MaybeUpdateTopIssue() { | 62 void IssueManager::MaybeUpdateTopIssue() { |
| 95 const Issue* new_top_issue = nullptr; | 63 const Issue* new_top_issue = nullptr; |
| 64 if (!issues_.empty()) { |
| 65 // Select the first blocking issue in the list of issues. |
| 66 // If there are none, simply select the first issue in the list. |
| 67 auto it = std::find_if(issues_.begin(), issues_.end(), |
| 68 [](const std::unique_ptr<Issue>& issue) { |
| 69 return issue->info().is_blocking; |
| 70 }); |
| 71 if (it == issues_.end()) |
| 72 it = issues_.begin(); |
| 96 | 73 |
| 97 if (issues_.empty()) { | 74 new_top_issue = it->get(); |
| 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 } | 75 } |
| 113 | 76 |
| 114 // If we've found a new top issue, then report it via the observer. | 77 // If we've found a new top issue, then report it via the observer. |
| 115 if (new_top_issue->id() != top_issue_id_) { | 78 if (new_top_issue != top_issue_) { |
| 116 top_issue_id_ = new_top_issue->id(); | 79 top_issue_ = new_top_issue; |
| 117 for (auto& observer : issues_observers_) | 80 for (auto& observer : issues_observers_) { |
| 118 observer.OnIssueUpdated(new_top_issue); | 81 if (top_issue_) |
| 82 observer.OnIssue(*top_issue_); |
| 83 else |
| 84 observer.OnIssuesCleared(); |
| 85 } |
| 119 } | 86 } |
| 120 } | 87 } |
| 121 | 88 |
| 122 } // namespace media_router | 89 } // namespace media_router |
| OLD | NEW |