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 | |
| 19 // static | |
| 20 ToastManager* ToastManager::GetInstance() { | |
| 21 // This instance doesn't need to be cleaned up at exit. | |
| 22 return base::Singleton<ToastManager, | |
| 23 base::LeakySingletonTraits<ToastManager>>::get(); | |
| 24 } | |
| 25 | |
| 26 // private ctor | |
| 27 ToastManager::ToastManager() {} | |
| 28 | |
| 29 ToastManager::~ToastManager() { | |
| 30 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 31 } | |
| 32 | |
| 33 void ToastManager::Show(const std::string& text, uint64_t duration_ms) { | |
| 34 queue_.emplace(std::make_pair(text, duration_ms)); | |
| 35 | |
| 36 if (queue_.size() == 1 && overlay_ == nullptr) | |
| 37 ShowLatest(); | |
| 38 } | |
| 39 | |
| 40 void ToastManager::OnClosed() { | |
| 41 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 42 | |
| 43 overlay_.reset(); | |
| 44 | |
| 45 // Show the next toast if available. | |
| 46 if (queue_.size() != 0) | |
| 47 ShowLatest(); | |
| 48 } | |
| 49 | |
| 50 void ToastManager::ShowLatest() { | |
| 51 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 52 DCHECK(!overlay_); | |
| 53 | |
| 54 auto data = queue_.front(); | |
| 55 uint64_t duration_ms = std::max(data.second, kMinimumDurationMs); | |
| 56 | |
| 57 toast_id_++; | |
| 58 | |
| 59 overlay_.reset(new ToastOverlay(this, data.first /* text */)); | |
| 60 overlay_->Show(true); | |
| 61 | |
| 62 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
|
oshima
2016/03/11 02:06:29
Won't the posted task outlive ash?
yoshiki
2016/03/11 08:40:56
Done. Now, Ash::Shell has an instance.
| |
| 63 FROM_HERE, | |
| 64 base::Bind(&ToastManager::OnDurationPassed, | |
| 65 base::Unretained(this), // |this| is never destroyed. | |
| 66 toast_id_), | |
| 67 base::TimeDelta::FromMilliseconds(duration_ms)); | |
| 68 | |
| 69 queue_.pop(); | |
| 70 } | |
| 71 | |
| 72 void ToastManager::OnDurationPassed(int toast_id) { | |
| 73 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 74 | |
| 75 if (overlay_ && toast_id_ == toast_id) | |
| 76 overlay_->Show(false); | |
| 77 } | |
| 78 | |
| 79 } // namespace ash | |
| OLD | NEW |