Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / athena / system / shutdown_dialog.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 "athena/system/shutdown_dialog.h"
6
7 #include "athena/screen/public/screen_manager.h"
8 #include "athena/strings/grit/athena_strings.h"
9 #include "chromeos/dbus/dbus_thread_manager.h"
10 #include "chromeos/dbus/power_manager_client.h"
11 #include "ui/base/l10n/l10n_util.h"
12 #include "ui/gfx/font_list.h"
13 #include "ui/views/background.h"
14 #include "ui/views/border.h"
15 #include "ui/views/controls/label.h"
16 #include "ui/views/layout/box_layout.h"
17 #include "ui/views/widget/widget.h"
18 #include "ui/views/window/dialog_delegate.h"
19
20 namespace athena {
21 namespace {
22
23 // The amount of time that the power button must be held to shut down the
24 // device after shutdown dialog is shown.
25 const int kShutdownTimeoutMs = 4000;
26
27 class ModalWidgetDelegate : public views::WidgetDelegate {
28  public:
29   explicit ModalWidgetDelegate(views::View* contents_view)
30       : contents_view_(contents_view) {}
31   ~ModalWidgetDelegate() override {}
32
33   // Overridden from WidgetDelegate:
34   virtual views::Widget* GetWidget() override {
35     return contents_view_->GetWidget();
36   }
37   virtual const views::Widget* GetWidget() const override {
38     return contents_view_->GetWidget();
39   }
40   virtual views::View* GetContentsView() override { return contents_view_; }
41   virtual ui::ModalType GetModalType() const override {
42     return ui::MODAL_TYPE_SYSTEM;
43   }
44
45  private:
46   views::View* contents_view_;
47
48   DISALLOW_COPY_AND_ASSIGN(ModalWidgetDelegate);
49 };
50
51 }  // namespace
52
53 ShutdownDialog::ShutdownDialog() : state_(STATE_OTHER) {
54   InputManager::Get()->AddPowerButtonObserver(this);
55 }
56
57 ShutdownDialog::~ShutdownDialog() {
58   InputManager::Get()->RemovePowerButtonObserver(this);
59 }
60
61 void ShutdownDialog::ShowShutdownWarningDialog() {
62   state_ = STATE_SHUTDOWN_WARNING_VISIBLE;
63
64   views::Label* label =
65       new views::Label(l10n_util::GetStringUTF16(IDS_ATHENA_SHUTDOWN_WARNING));
66   label->SetBackgroundColor(SK_ColorWHITE);
67   label->SetFontList(gfx::FontList().DeriveWithStyle(gfx::Font::BOLD));
68
69   views::View* container = new views::View;
70   container->AddChildView(label);
71
72   const int kBorderSpacing = 50;
73   container->SetLayoutManager(new views::BoxLayout(
74       views::BoxLayout::kHorizontal, kBorderSpacing, kBorderSpacing, 0));
75   container->set_background(
76       views::Background::CreateSolidBackground(SK_ColorWHITE));
77   container->SetBorder(views::Border::CreateSolidBorder(1, SK_ColorBLACK));
78
79   views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP);
80   params.delegate = new ModalWidgetDelegate(container);
81   params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
82   params.context = ScreenManager::Get()->GetContext();
83   // Use top most modal container.
84   params.keep_on_top = true;
85
86   shutdown_warning_message_.reset(new views::Widget);
87   shutdown_warning_message_->Init(params);
88   shutdown_warning_message_->Show();
89   shutdown_warning_message_->CenterWindow(container->GetPreferredSize());
90
91   timer_.Start(FROM_HERE,
92                base::TimeDelta::FromMilliseconds(kShutdownTimeoutMs),
93                this,
94                &ShutdownDialog::Shutdown);
95 }
96
97 void ShutdownDialog::Shutdown() {
98   state_ = STATE_SHUTDOWN_REQUESTED;
99   chromeos::DBusThreadManager::Get()
100       ->GetPowerManagerClient()
101       ->RequestShutdown();
102 }
103
104 void ShutdownDialog::OnPowerButtonStateChanged(
105     PowerButtonObserver::State state) {
106   if (state_ == STATE_SHUTDOWN_REQUESTED)
107     return;
108
109   switch (state) {
110     case PRESSED:
111       state_ = STATE_SUSPEND_ON_RELEASE;
112       break;
113     case LONG_PRESSED:
114       ShowShutdownWarningDialog();
115       break;
116     case RELEASED:
117       if (state_ == STATE_SUSPEND_ON_RELEASE) {
118         chromeos::DBusThreadManager::Get()
119             ->GetPowerManagerClient()
120             ->RequestSuspend();
121       }
122       state_ = STATE_OTHER;
123       timer_.Stop();
124       shutdown_warning_message_.reset();
125       break;
126   }
127 }
128
129 }  // namespace athena