1// Copyright (c) 2011 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/login/login_ui.h"
6
7#include "base/memory/ref_counted_memory.h"
8#include "base/memory/singleton.h"
9#include "base/string_piece.h"
10#include "base/values.h"
11#include "chrome/browser/chromeos/cros/cros_library.h"
12#include "chrome/browser/chromeos/cros/power_library.h"
13#include "chrome/browser/chromeos/login/dom_login_display.h"
14#include "chrome/browser/profiles/profile.h"
15#include "chrome/browser/ui/browser.h"
16#include "chrome/browser/ui/browser_window.h"
17#include "chrome/browser/ui/webui/chrome_url_data_manager.h"
18#include "chrome/browser/ui/webui/chromeos/login/login_ui_helpers.h"
19#include "chrome/common/url_constants.h"
20#include "content/browser/browser_thread.h"
21#include "content/browser/tab_contents/tab_contents.h"
22
23namespace {
24const char* kResetPrompt = "resetPrompt";
25}  // namespace
26
27namespace chromeos {
28
29// LoginUIHTMLSource, public: --------------------------------------------------
30
31LoginUIHTMLSource::LoginUIHTMLSource(MessageLoop* message_loop)
32    : DataSource(chrome::kChromeUILoginHost, message_loop),
33      html_operations_(new HTMLOperationsInterface()) {
34}
35
36void LoginUIHTMLSource::StartDataRequest(const std::string& path,
37                                         bool is_incognito,
38                                         int request_id) {
39  DictionaryValue localized_strings;
40  SetFontAndTextDirection(&localized_strings);
41
42  base::StringPiece login_html = html_operations_->GetLoginHTML();
43  std::string full_html = html_operations_->GetFullHTML(login_html,
44                                                        &localized_strings);
45  scoped_refptr<RefCountedBytes> html_bytes(
46      html_operations_->CreateHTMLBytes(full_html));
47  SendResponse(request_id,
48               (html_bytes.get()));
49}
50
51std::string LoginUIHTMLSource::GetMimeType(const std::string&) const {
52  return "text/html";
53}
54
55// LoginUIHandlerDelegate, protected: ------------------------------------------
56
57LoginUIHandlerDelegate::~LoginUIHandlerDelegate() {}
58
59// LoginUIHandler, public: -----------------------------------------------------
60
61LoginUIHandler::LoginUIHandler() {
62  delegate_ = DOMLoginDisplay::GetInstance();
63  delegate_->set_login_handler(this);
64}
65
66WebUIMessageHandler* LoginUIHandler::Attach(WebUI* web_ui) {
67  return WebUIMessageHandler::Attach(web_ui);
68}
69
70void LoginUIHandler::RegisterMessages() {
71  web_ui_->RegisterMessageCallback(
72      "LaunchIncognito",
73      NewCallback(this,
74                  &LoginUIHandler::HandleLaunchIncognito));
75  web_ui_->RegisterMessageCallback(
76      "AuthenticateUser",
77      NewCallback(this,
78                  &LoginUIHandler::HandleAuthenticateUser));
79  web_ui_->RegisterMessageCallback(
80      "ShutdownSystem",
81      NewCallback(this,
82                  &LoginUIHandler::HandleShutdownSystem));
83}
84
85void LoginUIHandler::HandleAuthenticateUser(const ListValue* args) {
86  std::string username;
87  std::string password;
88  size_t expected_size = 2;
89  CHECK_EQ(args->GetSize(), expected_size);
90  args->GetString(0, &username);
91  args->GetString(1, &password);
92  delegate_->Login(username, password);
93}
94
95void LoginUIHandler::HandleLaunchIncognito(const ListValue* args) {
96  delegate_->LoginAsGuest();
97}
98
99void LoginUIHandler::HandleShutdownSystem(const ListValue* args) {
100  DCHECK(CrosLibrary::Get()->EnsureLoaded());
101  CrosLibrary::Get()->GetPowerLibrary()->RequestShutdown();
102}
103
104void LoginUIHandler::ClearAndEnablePassword() {
105  web_ui_->CallJavascriptFunction(kResetPrompt);
106}
107
108
109// LoginUI, public: ------------------------------------------------------------
110
111LoginUI::LoginUI(TabContents* contents)
112    : WebUI(contents) {
113  LoginUIHandler* handler = new LoginUIHandler();
114  AddMessageHandler(handler->Attach(this));
115  LoginUIHTMLSource* html_source =
116      new LoginUIHTMLSource(MessageLoop::current());
117
118  contents->profile()->GetChromeURLDataManager()->AddDataSource(html_source);
119}
120
121}  // namespace chromeos
122