| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "ash/system/toast/toast_manager.h" | 5 #include "ash/system/toast/toast_manager.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/threading/thread_task_runner_handle.h" | 11 #include "base/threading/thread_task_runner_handle.h" |
| 12 | 12 |
| 13 namespace ash { | 13 namespace ash { |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 // Minimum duration for a toast to be visible (in millisecond). | 17 // Minimum duration for a toast to be visible (in millisecond). |
| 18 const int32_t kMinimumDurationMs = 200; | 18 const int32_t kMinimumDurationMs = 200; |
| 19 | 19 |
| 20 } // anonymous namespace | 20 } // anonymous namespace |
| 21 | 21 |
| 22 ToastManager::ToastManager() : weak_ptr_factory_(this) {} | 22 ToastManager::ToastManager() : weak_ptr_factory_(this) {} |
| 23 | 23 |
| 24 ToastManager::~ToastManager() {} | 24 ToastManager::~ToastManager() {} |
| 25 | 25 |
| 26 void ToastManager::Show(const ToastData& data) { | 26 void ToastManager::Show(const ToastData& data) { |
| 27 const std::string& id = data.id; | 27 const std::string& id = data.id; |
| 28 DCHECK(!id.empty()); | 28 DCHECK(!id.empty()); |
| 29 | 29 |
| 30 LOG(WARNING) << "Show id=" << id; |
| 31 |
| 30 if (current_toast_id_ == id) { | 32 if (current_toast_id_ == id) { |
| 31 // TODO(yoshiki): Replaces the visible toast. | 33 // TODO(yoshiki): Replaces the visible toast. |
| 34 LOG(WARNING) << "1"; |
| 32 return; | 35 return; |
| 33 } | 36 } |
| 34 | 37 |
| 35 auto existing_toast = | 38 auto existing_toast = |
| 36 std::find_if(queue_.begin(), queue_.end(), | 39 std::find_if(queue_.begin(), queue_.end(), |
| 37 [&id](const ToastData& data) { return data.id == id; }); | 40 [&id](const ToastData& data) { return data.id == id; }); |
| 38 | 41 |
| 39 if (existing_toast == queue_.end()) { | 42 if (existing_toast == queue_.end()) { |
| 43 LOG(WARNING) << "2"; |
| 40 queue_.emplace_back(data); | 44 queue_.emplace_back(data); |
| 41 } else { | 45 } else { |
| 46 LOG(WARNING) << "3"; |
| 42 *existing_toast = data; | 47 *existing_toast = data; |
| 43 } | 48 } |
| 44 | 49 |
| 45 if (queue_.size() == 1 && overlay_ == nullptr) | 50 if (queue_.size() == 1 && overlay_ == nullptr) { |
| 51 LOG(WARNING) << "4"; |
| 46 ShowLatest(); | 52 ShowLatest(); |
| 53 } |
| 47 } | 54 } |
| 48 | 55 |
| 49 void ToastManager::Cancel(const std::string& id) { | 56 void ToastManager::Cancel(const std::string& id) { |
| 57 LOG(WARNING) << "Cancel id=" << id; |
| 50 if (id == current_toast_id_) { | 58 if (id == current_toast_id_) { |
| 51 overlay_->Show(false); | 59 overlay_->Show(false); |
| 52 return; | 60 return; |
| 53 } | 61 } |
| 54 | 62 |
| 55 auto cancelled_toast = | 63 auto cancelled_toast = |
| 56 std::find_if(queue_.begin(), queue_.end(), | 64 std::find_if(queue_.begin(), queue_.end(), |
| 57 [&id](const ToastData& data) { return data.id == id; }); | 65 [&id](const ToastData& data) { return data.id == id; }); |
| 58 if (cancelled_toast != queue_.end()) | 66 if (cancelled_toast != queue_.end()) { |
| 67 LOG(WARNING) << "erase"; |
| 59 queue_.erase(cancelled_toast); | 68 queue_.erase(cancelled_toast); |
| 69 } |
| 60 } | 70 } |
| 61 | 71 |
| 62 void ToastManager::OnClosed() { | 72 void ToastManager::OnClosed() { |
| 73 LOG(WARNING) << "OnClosed"; |
| 63 overlay_.reset(); | 74 overlay_.reset(); |
| 64 current_toast_id_.clear(); | 75 current_toast_id_.clear(); |
| 65 | 76 |
| 66 // Show the next toast if available. | 77 // Show the next toast if available. |
| 67 if (!queue_.empty()) | 78 if (!queue_.empty()) { |
| 79 LOG(WARNING) << "calling ShowLatest"; |
| 68 ShowLatest(); | 80 ShowLatest(); |
| 81 } |
| 69 } | 82 } |
| 70 | 83 |
| 71 void ToastManager::ShowLatest() { | 84 void ToastManager::ShowLatest() { |
| 85 LOG(WARNING) << "ShowLatest"; |
| 72 DCHECK(!overlay_); | 86 DCHECK(!overlay_); |
| 73 | 87 |
| 74 const ToastData data = std::move(queue_.front()); | 88 const ToastData data = std::move(queue_.front()); |
| 75 queue_.pop_front(); | 89 queue_.pop_front(); |
| 76 | 90 |
| 77 current_toast_id_ = data.id; | 91 current_toast_id_ = data.id; |
| 78 serial_++; | 92 serial_++; |
| 79 | 93 |
| 80 overlay_.reset(new ToastOverlay(this, data.text, data.dismiss_text)); | 94 overlay_.reset(new ToastOverlay(this, data.text, data.dismiss_text)); |
| 81 overlay_->Show(true); | 95 overlay_->Show(true); |
| 82 | 96 |
| 83 if (data.duration_ms != ToastData::kInfiniteDuration) { | 97 if (data.duration_ms != ToastData::kInfiniteDuration) { |
| 98 LOG(WARNING) << "postking!"; |
| 84 int32_t duration_ms = std::max(data.duration_ms, kMinimumDurationMs); | 99 int32_t duration_ms = std::max(data.duration_ms, kMinimumDurationMs); |
| 85 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 100 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 86 FROM_HERE, base::Bind(&ToastManager::OnDurationPassed, | 101 FROM_HERE, base::Bind(&ToastManager::OnDurationPassed, |
| 87 weak_ptr_factory_.GetWeakPtr(), serial_), | 102 weak_ptr_factory_.GetWeakPtr(), serial_), |
| 88 base::TimeDelta::FromMilliseconds(duration_ms)); | 103 base::TimeDelta::FromMilliseconds(duration_ms)); |
| 89 } | 104 } |
| 90 } | 105 } |
| 91 | 106 |
| 92 void ToastManager::OnDurationPassed(int toast_number) { | 107 void ToastManager::OnDurationPassed(int toast_number) { |
| 93 if (overlay_ && serial_ == toast_number) | 108 LOG(WARNING) << "OnDurationPassed " << toast_number; |
| 109 if (overlay_ && serial_ == toast_number) { |
| 110 LOG(WARNING) << "OnDurationPassed - showing"; |
| 94 overlay_->Show(false); | 111 overlay_->Show(false); |
| 112 } |
| 95 } | 113 } |
| 96 | 114 |
| 97 } // namespace ash | 115 } // namespace ash |
| OLD | NEW |