Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / ash / system / chromeos / session / logout_confirmation_controller.cc
1 // Copyright 2014 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/chromeos/session/logout_confirmation_controller.h"
6
7 #include "ash/session/session_state_delegate.h"
8 #include "ash/shell.h"
9 #include "ash/system/chromeos/session/logout_confirmation_dialog.h"
10 #include "base/location.h"
11 #include "base/time/default_tick_clock.h"
12 #include "base/time/tick_clock.h"
13 #include "ui/views/widget/widget.h"
14
15 namespace ash {
16
17 LogoutConfirmationController::LogoutConfirmationController(
18     const base::Closure& logout_closure)
19     : clock_(new base::DefaultTickClock),
20       logout_closure_(logout_closure),
21       dialog_(NULL),
22       logout_timer_(false, false) {
23   if (Shell::HasInstance())
24     Shell::GetInstance()->AddShellObserver(this);
25 }
26
27 LogoutConfirmationController::~LogoutConfirmationController() {
28   if (Shell::HasInstance())
29     Shell::GetInstance()->RemoveShellObserver(this);
30   if (dialog_)
31     dialog_->GetWidget()->Close();
32 }
33
34 void LogoutConfirmationController::ConfirmLogout(
35     base::TimeTicks logout_time) {
36   if (!logout_time_.is_null() && logout_time >= logout_time_) {
37     // If a confirmation dialog is already being shown and its countdown expires
38     // no later than the |logout_time| requested now, keep the current dialog
39     // open.
40     return;
41   }
42   logout_time_ = logout_time;
43
44   if (!dialog_) {
45     // Show confirmation dialog unless this is a unit test without a Shell.
46     if (Shell::HasInstance())
47       dialog_ = new LogoutConfirmationDialog(this, logout_time_);
48   } else {
49     dialog_->Update(logout_time_);
50   }
51
52   logout_timer_.Start(FROM_HERE,
53                       logout_time_ - clock_->NowTicks(),
54                       logout_closure_);
55 }
56
57 void LogoutConfirmationController::SetClockForTesting(
58     scoped_ptr<base::TickClock> clock) {
59   clock_ = clock.Pass();
60 }
61
62 void LogoutConfirmationController::OnLockStateChanged(bool locked) {
63   if (!locked || logout_time_.is_null())
64     return;
65
66   // If the screen is locked while a confirmation dialog is being shown, close
67   // the dialog.
68   logout_time_ = base::TimeTicks();
69   if (dialog_)
70     dialog_->GetWidget()->Close();
71   logout_timer_.Stop();
72 }
73
74 void LogoutConfirmationController::OnLogoutConfirmed() {
75   logout_timer_.Stop();
76   logout_closure_.Run();
77 }
78
79 void LogoutConfirmationController::OnDialogClosed() {
80   logout_time_ = base::TimeTicks();
81   dialog_ = NULL;
82   logout_timer_.Stop();
83 }
84
85 }  // namespace ash