summaryrefslogtreecommitdiffstats
path: root/ash/system/toast/toast_manager.cc
blob: 137a71f94266b5ac4d1e6b3e3e00b9a19a4896c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/system/toast/toast_manager.h"

#include "base/bind.h"
#include "base/location.h"
#include "base/thread_task_runner_handle.h"

namespace ash {

namespace {

// Minimum duration for a toast to be visible (in millisecond).
uint64_t kMinimumDurationMs = 200;

}  // anonymous namespace

ToastManager::ToastManager() : weak_ptr_factory_(this) {}

ToastManager::~ToastManager() {}

void ToastManager::Show(const std::string& text, uint64_t duration_ms) {
  queue_.emplace(std::make_pair(text, duration_ms));

  if (queue_.size() == 1 && overlay_ == nullptr)
    ShowLatest();
}

void ToastManager::OnClosed() {
  overlay_.reset();

  // Show the next toast if available.
  if (queue_.size() != 0)
    ShowLatest();
}

void ToastManager::ShowLatest() {
  DCHECK(!overlay_);

  auto data = queue_.front();
  uint64_t duration_ms = std::max(data.second, kMinimumDurationMs);

  toast_id_++;

  overlay_.reset(new ToastOverlay(this, data.first /* text */));
  overlay_->Show(true);

  base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
      FROM_HERE, base::Bind(&ToastManager::OnDurationPassed,
                            weak_ptr_factory_.GetWeakPtr(), toast_id_),
      base::TimeDelta::FromMilliseconds(duration_ms));

  queue_.pop();
}

void ToastManager::OnDurationPassed(int toast_id) {
  if (overlay_ && toast_id_ == toast_id)
    overlay_->Show(false);
}

}  // namespace ash