Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ash/system/toast/toast_manager.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/location.h" | |
| 9 #include "base/thread_task_runner_handle.h" | |
| 10 | |
| 11 namespace ash { | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 // Minimum duration for a toast to be visible (in millisecond). | |
| 16 uint64_t kMinimumDurationMs = 200; | |
| 17 | |
| 18 } // anonymous namespace | |
| 19 | |
| 20 ToastManager::ToastManager() {} | |
| 21 | |
| 22 ToastManager::~ToastManager() {} | |
| 23 | |
| 24 void ToastManager::Show(const std::string& text, uint64_t duration_ms) { | |
| 25 queue_.emplace(std::make_pair(text, duration_ms)); | |
| 26 | |
| 27 if (queue_.size() == 1 && overlay_ == nullptr) | |
| 28 ShowLatest(); | |
| 29 } | |
| 30 | |
| 31 void ToastManager::OnClosed() { | |
| 32 overlay_.reset(); | |
| 33 | |
| 34 // Show the next toast if available. | |
| 35 if (queue_.size() != 0) | |
| 36 ShowLatest(); | |
| 37 } | |
| 38 | |
| 39 void ToastManager::ShowLatest() { | |
| 40 DCHECK(!overlay_); | |
| 41 | |
| 42 auto data = queue_.front(); | |
| 43 uint64_t duration_ms = std::max(data.second, kMinimumDurationMs); | |
| 44 | |
| 45 toast_id_++; | |
| 46 | |
| 47 overlay_.reset(new ToastOverlay(this, data.first /* text */)); | |
| 48 overlay_->Show(true); | |
| 49 | |
| 50 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
| 51 FROM_HERE, | |
| 52 base::Bind(&ToastManager::OnDurationPassed, | |
| 53 base::Unretained(this), // |this| is never destroyed. | |
|
oshima
2016/03/15 19:03:19
Use weak ptr and update the comment.
yoshiki
2016/03/17 07:59:10
Done.
| |
| 54 toast_id_), | |
| 55 base::TimeDelta::FromMilliseconds(duration_ms)); | |
| 56 | |
| 57 queue_.pop(); | |
| 58 } | |
| 59 | |
| 60 void ToastManager::OnDurationPassed(int toast_id) { | |
| 61 if (overlay_ && toast_id_ == toast_id) | |
| 62 overlay_->Show(false); | |
| 63 } | |
| 64 | |
| 65 } // namespace ash | |
| OLD | NEW |