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; | |
|
oshima
2016/03/11 09:40:24
new line after this
yoshiki
2016/03/11 14:30:34
Done.
| |
| 17 } | |
|
oshima
2016/03/11 09:40:24
} // namespace
yoshiki
2016/03/11 14:30:34
Done.
| |
| 18 | |
| 19 ToastManager::ToastManager() {} | |
| 20 | |
| 21 ToastManager::~ToastManager() { | |
| 22 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 23 } | |
| 24 | |
| 25 void ToastManager::Show(const std::string& text, uint64_t duration_ms) { | |
| 26 queue_.emplace(std::make_pair(text, duration_ms)); | |
| 27 | |
| 28 if (queue_.size() == 1 && overlay_ == nullptr) | |
| 29 ShowLatest(); | |
| 30 } | |
| 31 | |
| 32 void ToastManager::OnClosed() { | |
| 33 DCHECK(thread_checker_.CalledOnValidThread()); | |
|
oshima
2016/03/11 09:40:24
Since this isn't multi threaded, you can remove th
yoshiki
2016/03/11 14:30:34
Done.
| |
| 34 | |
| 35 overlay_.reset(); | |
| 36 | |
| 37 // Show the next toast if available. | |
| 38 if (queue_.size() != 0) | |
| 39 ShowLatest(); | |
| 40 } | |
| 41 | |
| 42 void ToastManager::ShowLatest() { | |
| 43 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 44 DCHECK(!overlay_); | |
| 45 | |
| 46 auto data = queue_.front(); | |
| 47 uint64_t duration_ms = std::max(data.second, kMinimumDurationMs); | |
| 48 | |
| 49 toast_id_++; | |
| 50 | |
| 51 overlay_.reset(new ToastOverlay(this, data.first /* text */)); | |
| 52 overlay_->Show(true); | |
| 53 | |
| 54 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
| 55 FROM_HERE, | |
| 56 base::Bind(&ToastManager::OnDurationPassed, | |
| 57 base::Unretained(this), // |this| is never destroyed. | |
| 58 toast_id_), | |
| 59 base::TimeDelta::FromMilliseconds(duration_ms)); | |
| 60 | |
| 61 queue_.pop(); | |
| 62 } | |
| 63 | |
| 64 void ToastManager::OnDurationPassed(int toast_id) { | |
| 65 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 66 | |
| 67 if (overlay_ && toast_id_ == toast_id) | |
| 68 overlay_->Show(false); | |
| 69 } | |
| 70 | |
| 71 } // namespace ash | |
| OLD | NEW |