summaryrefslogtreecommitdiffstats
path: root/ash/system/chromeos/session/logout_confirmation_controller.cc
blob: fdf1cc627c751d59bb5b55777ddcc57463fb2b8a (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright 2014 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/chromeos/session/logout_confirmation_controller.h"

#include "ash/session/session_state_delegate.h"
#include "ash/shell.h"
#include "ash/system/chromeos/session/logout_confirmation_dialog.h"
#include "base/location.h"
#include "base/time/default_tick_clock.h"
#include "base/time/tick_clock.h"
#include "ui/views/widget/widget.h"

namespace ash {

LogoutConfirmationController::LogoutConfirmationController(
    const base::Closure& logout_closure)
    : clock_(new base::DefaultTickClock),
      logout_closure_(logout_closure),
      dialog_(NULL),
      logout_timer_(false, false) {
  if (Shell::HasInstance())
    Shell::GetInstance()->AddShellObserver(this);
}

LogoutConfirmationController::~LogoutConfirmationController() {
  if (Shell::HasInstance())
    Shell::GetInstance()->RemoveShellObserver(this);
  if (dialog_)
    dialog_->GetWidget()->Close();
}

void LogoutConfirmationController::ConfirmLogout(
    base::TimeTicks logout_time) {
  if (!logout_time_.is_null() && logout_time >= logout_time_) {
    // If a confirmation dialog is already being shown and its countdown expires
    // no later than the |logout_time| requested now, keep the current dialog
    // open.
    return;
  }
  logout_time_ = logout_time;

  if (!dialog_) {
    // Show confirmation dialog unless this is a unit test without a Shell.
    if (Shell::HasInstance())
      dialog_ = new LogoutConfirmationDialog(this, logout_time_);
  } else {
    dialog_->Update(logout_time_);
  }

  logout_timer_.Start(FROM_HERE,
                      logout_time_ - clock_->NowTicks(),
                      logout_closure_);
}

void LogoutConfirmationController::SetClockForTesting(
    scoped_ptr<base::TickClock> clock) {
  clock_ = clock.Pass();
}

void LogoutConfirmationController::OnLockStateChanged(bool locked) {
  if (!locked || logout_time_.is_null())
    return;

  // If the screen is locked while a confirmation dialog is being shown, close
  // the dialog.
  logout_time_ = base::TimeTicks();
  if (dialog_)
    dialog_->GetWidget()->Close();
  logout_timer_.Stop();
}

void LogoutConfirmationController::OnLogoutConfirmed() {
  logout_timer_.Stop();
  logout_closure_.Run();
}

void LogoutConfirmationController::OnDialogClosed() {
  logout_time_ = base::TimeTicks();
  dialog_ = NULL;
  logout_timer_.Stop();
}

}  // namespace ash