OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/media/router/issue_manager.h" |
| 6 |
| 7 namespace media_router { |
| 8 |
| 9 IssueManager::IssueManager() : top_issue_(nullptr) { |
| 10 } |
| 11 |
| 12 IssueManager::~IssueManager() { |
| 13 } |
| 14 |
| 15 void IssueManager::AddIssue(const Issue& issue) { |
| 16 DCHECK(thread_checker_.CalledOnValidThread()); |
| 17 media_issue_map_.insert(std::make_pair(issue.id(), issue)); |
| 18 |
| 19 MaybeUpdateTopIssue(); |
| 20 } |
| 21 |
| 22 void IssueManager::ClearIssue(const Issue::IssueId& issue_id) { |
| 23 DCHECK(thread_checker_.CalledOnValidThread()); |
| 24 media_issue_map_.erase(issue_id); |
| 25 |
| 26 MaybeUpdateTopIssue(); |
| 27 } |
| 28 |
| 29 size_t IssueManager::GetIssueCount() const { |
| 30 DCHECK(thread_checker_.CalledOnValidThread()); |
| 31 return media_issue_map_.size(); |
| 32 } |
| 33 |
| 34 void IssueManager::ClearAllIssues() { |
| 35 DCHECK(thread_checker_.CalledOnValidThread()); |
| 36 media_issue_map_.clear(); |
| 37 |
| 38 MaybeUpdateTopIssue(); |
| 39 } |
| 40 |
| 41 void IssueManager::ClearGlobalIssues() { |
| 42 DCHECK(thread_checker_.CalledOnValidThread()); |
| 43 auto it = media_issue_map_.begin(); |
| 44 while (it != media_issue_map_.end()) { |
| 45 if (it->second.IsGlobal()) { |
| 46 media_issue_map_.erase(it++); |
| 47 } else { |
| 48 ++it; |
| 49 } |
| 50 } |
| 51 |
| 52 MaybeUpdateTopIssue(); |
| 53 } |
| 54 |
| 55 void IssueManager::ClearIssuesWithRouteId(const MediaRouteId& route_id) { |
| 56 DCHECK(thread_checker_.CalledOnValidThread()); |
| 57 auto it = media_issue_map_.begin(); |
| 58 while (it != media_issue_map_.end()) { |
| 59 if (it->second.route_id() == route_id) { |
| 60 media_issue_map_.erase(it++); |
| 61 } else { |
| 62 ++it; |
| 63 } |
| 64 } |
| 65 |
| 66 MaybeUpdateTopIssue(); |
| 67 } |
| 68 |
| 69 void IssueManager::RegisterObserver(IssuesObserver* observer) { |
| 70 DCHECK(thread_checker_.CalledOnValidThread()); |
| 71 DCHECK(observer); |
| 72 DCHECK(!issues_observers_.HasObserver(observer)); |
| 73 |
| 74 issues_observers_.AddObserver(observer); |
| 75 if (top_issue_) |
| 76 observer->OnIssueUpdated(top_issue_); |
| 77 } |
| 78 |
| 79 void IssueManager::UnregisterObserver(IssuesObserver* observer) { |
| 80 DCHECK(thread_checker_.CalledOnValidThread()); |
| 81 issues_observers_.RemoveObserver(observer); |
| 82 } |
| 83 |
| 84 void IssueManager::MaybeUpdateTopIssue() { |
| 85 Issue* new_top_issue = nullptr; |
| 86 if (!media_issue_map_.empty()) { |
| 87 auto it = media_issue_map_.begin(); |
| 88 |
| 89 // Assumes there's at least one issue in the map. This is the first issue |
| 90 // regardless of whether it's blocking or not. |
| 91 new_top_issue = &it->second; |
| 92 |
| 93 while (it != media_issue_map_.end()) { |
| 94 // The first blocking issue is of higher priority than the first issue. |
| 95 if (it->second.IsBlocking()) { |
| 96 new_top_issue = &it->second; |
| 97 break; |
| 98 } |
| 99 |
| 100 ++it; |
| 101 } |
| 102 } |
| 103 |
| 104 // If the top issue is different from |top_issue_|, update. |
| 105 if (new_top_issue != top_issue_) { |
| 106 top_issue_ = new_top_issue; |
| 107 |
| 108 FOR_EACH_OBSERVER(IssuesObserver, issues_observers_, |
| 109 OnIssueUpdated(top_issue_)); |
| 110 } |
| 111 } |
| 112 |
| 113 } // namespace media_router |
OLD | NEW |