OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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 "chrome/browser/ui/webui/chromeos/idle_logout_dialog.h" |
| 6 |
| 7 #include <string> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" |
| 12 #include "base/json/json_reader.h" |
| 13 #include "base/utf_string_conversions.h" |
| 14 #include "base/values.h" |
| 15 #include "chrome/browser/chromeos/dbus/dbus_thread_manager.h" |
| 16 #include "chrome/browser/chromeos/kiosk_mode/kiosk_mode_helper.h" |
| 17 #include "chrome/browser/chromeos/dbus/power_manager_client.h" |
| 18 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/browser/ui/browser.h" |
| 20 #include "chrome/browser/ui/browser_list.h" |
| 21 #include "chrome/browser/ui/browser_dialogs.h" |
| 22 #include "chrome/browser/ui/dialog_style.h" |
| 23 #include "chrome/browser/ui/tab_contents/core_tab_helper.h" |
| 24 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 25 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" |
| 26 #include "chrome/browser/ui/webui/html_dialog_ui.h" |
| 27 #include "chrome/common/logging_chrome.h" |
| 28 #include "chrome/common/url_constants.h" |
| 29 #include "content/browser/renderer_host/render_view_host.h" |
| 30 #include "content/public/browser/render_process_host.h" |
| 31 #include "content/public/browser/web_contents.h" |
| 32 #include "content/public/browser/web_ui_message_handler.h" |
| 33 #include "content/public/common/result_codes.h" |
| 34 #include "grit/browser_resources.h" |
| 35 #include "grit/generated_resources.h" |
| 36 #include "ui/base/l10n/l10n_util.h" |
| 37 |
| 38 using content::WebContents; |
| 39 using content::WebUIMessageHandler; |
| 40 |
| 41 namespace { |
| 42 |
| 43 // Global singleton instance of our dialog class. |
| 44 IdleLogoutDialog* g_instance = NULL; |
| 45 // Height/Width of the logout dialog. |
| 46 const int kIdleLogoutDialogWidth = 400; |
| 47 const int kIdleLogoutDialogHeight = 120; |
| 48 |
| 49 } // namespace |
| 50 |
| 51 //////////////////////////////////////////////////////////////////////////////// |
| 52 // IdleLogoutDialogHandler |
| 53 |
| 54 class IdleLogoutDialogHandler : public content::WebUIMessageHandler { |
| 55 public: |
| 56 IdleLogoutDialogHandler() : contents_(NULL) {} |
| 57 virtual void RegisterMessages() OVERRIDE; |
| 58 void CloseDialog(); |
| 59 |
| 60 private: |
| 61 void RequestCountdown(const base::ListValue*); |
| 62 void RequestLogout(const base::ListValue*); |
| 63 |
| 64 content::WebContents* contents_; |
| 65 |
| 66 DISALLOW_COPY_AND_ASSIGN(IdleLogoutDialogHandler); |
| 67 }; |
| 68 |
| 69 //////////////////////////////////////////////////////////////////////////////// |
| 70 // IdleLogoutDialog public static methods |
| 71 |
| 72 void IdleLogoutDialog::ShowIdleLogoutDialog() { |
| 73 if (!g_instance) { |
| 74 g_instance = new IdleLogoutDialog(); |
| 75 } |
| 76 g_instance->ShowDialog(); |
| 77 } |
| 78 |
| 79 void IdleLogoutDialog::CloseIdleLogoutDialog() { |
| 80 if (g_instance) |
| 81 g_instance->CloseDialog(); |
| 82 } |
| 83 |
| 84 //////////////////////////////////////////////////////////////////////////////// |
| 85 // IdleLogoutDialog private methods |
| 86 |
| 87 IdleLogoutDialog::IdleLogoutDialog() |
| 88 : contents_(NULL), handler_(NULL) { |
| 89 } |
| 90 |
| 91 IdleLogoutDialog::~IdleLogoutDialog() { |
| 92 } |
| 93 |
| 94 void IdleLogoutDialog::ShowDialog() { |
| 95 Browser* browser = BrowserList::GetLastActive(); |
| 96 DCHECK(browser); |
| 97 handler_ = new IdleLogoutDialogHandler(); |
| 98 browser->BrowserShowHtmlDialog(this, NULL, STYLE_GENERIC); |
| 99 } |
| 100 |
| 101 void IdleLogoutDialog::CloseDialog() { |
| 102 contents_ = NULL; |
| 103 handler_->CloseDialog(); |
| 104 } |
| 105 |
| 106 //////////////////////////////////////////////////////////////////////////////// |
| 107 // Overridden from HtmlDialogUIDelegate |
| 108 ui::ModalType IdleLogoutDialog::GetDialogModalType() const { |
| 109 return ui::MODAL_TYPE_WINDOW; |
| 110 } |
| 111 |
| 112 string16 IdleLogoutDialog::GetDialogTitle() const { |
| 113 return string16(); |
| 114 } |
| 115 |
| 116 GURL IdleLogoutDialog::GetDialogContentURL() const { |
| 117 return GURL(chrome::kChromeUIIdleLogoutDialogURL); |
| 118 } |
| 119 |
| 120 void IdleLogoutDialog::GetWebUIMessageHandlers( |
| 121 std::vector<WebUIMessageHandler*>* handlers) const { |
| 122 handlers->push_back(handler_); |
| 123 } |
| 124 |
| 125 void IdleLogoutDialog::GetDialogSize(gfx::Size* size) const { |
| 126 size->SetSize(kIdleLogoutDialogWidth, kIdleLogoutDialogHeight); |
| 127 } |
| 128 |
| 129 std::string IdleLogoutDialog::GetDialogArgs() const { |
| 130 return std::string(); |
| 131 } |
| 132 |
| 133 void IdleLogoutDialog::OnDialogClosed(const std::string& json_retval) { |
| 134 g_instance = NULL; |
| 135 delete this; |
| 136 } |
| 137 |
| 138 void IdleLogoutDialog::OnCloseContents(WebContents* source, |
| 139 bool* out_close_dialog) { |
| 140 NOTIMPLEMENTED(); |
| 141 } |
| 142 |
| 143 bool IdleLogoutDialog::ShouldShowDialogTitle() const { |
| 144 return false; |
| 145 } |
| 146 |
| 147 //////////////////////////////////////////////////////////////////////////////// |
| 148 // IdleLogoutDialogUI methods |
| 149 |
| 150 IdleLogoutDialogUI::IdleLogoutDialogUI(content::WebUI* web_ui) |
| 151 : HtmlDialogUI(web_ui) { |
| 152 ChromeWebUIDataSource* source = |
| 153 new ChromeWebUIDataSource(chrome::kChromeUIIdleLogoutDialogHost); |
| 154 |
| 155 source->AddLocalizedString("title", IDS_IDLE_LOGOUT_TITLE); |
| 156 source->AddLocalizedString("warning", IDS_IDLE_LOGOUT_WARNING); |
| 157 |
| 158 source->set_json_path("strings.js"); |
| 159 |
| 160 source->add_resource_path("idle_logout_dialog.js", |
| 161 IDR_IDLE_LOGOUT_DIALOG_JS); |
| 162 source->add_resource_path("idle_logout_dialog.css", |
| 163 IDR_IDLE_LOGOUT_DIALOG_CSS); |
| 164 |
| 165 source->set_default_resource(IDR_IDLE_LOGOUT_DIALOG_HTML); |
| 166 |
| 167 Profile* profile = Profile::FromWebUI(web_ui); |
| 168 profile->GetChromeURLDataManager()->AddDataSource(source); |
| 169 } |
| 170 |
| 171 //////////////////////////////////////////////////////////////////////////////// |
| 172 // IdleLogoutDialogHandler methods |
| 173 |
| 174 void IdleLogoutDialogHandler::RegisterMessages() { |
| 175 web_ui()->RegisterMessageCallback("requestCountdown", |
| 176 base::Bind(&IdleLogoutDialogHandler::RequestCountdown, |
| 177 base::Unretained(this))); |
| 178 web_ui()->RegisterMessageCallback("requestLogout", |
| 179 base::Bind(&IdleLogoutDialogHandler::RequestLogout, |
| 180 base::Unretained(this))); |
| 181 } |
| 182 |
| 183 void IdleLogoutDialogHandler::CloseDialog() { |
| 184 static_cast<HtmlDialogUI*>(web_ui()->GetController())->CloseDialog(NULL); |
| 185 } |
| 186 |
| 187 void IdleLogoutDialogHandler::RequestCountdown(const base::ListValue*) { |
| 188 if (!chromeos::KioskModeHelper::Get()->is_initialized()) { |
| 189 chromeos::KioskModeHelper::Get()->Initialize( |
| 190 base::Bind(&IdleLogoutDialogHandler::RequestCountdown, |
| 191 base::Unretained(this), (base::ListValue*) NULL)); |
| 192 return; |
| 193 } |
| 194 base::FundamentalValue warning_timeout((int) |
| 195 chromeos::KioskModeHelper::Get()->GetIdleLogoutWarningTimeout()); |
| 196 web_ui()->CallJavascriptFunction("startCountdown", warning_timeout); |
| 197 } |
| 198 |
| 199 void IdleLogoutDialogHandler::RequestLogout(const base::ListValue*) { |
| 200 BrowserList::AttemptUserExit(); |
| 201 } |
OLD | NEW |