| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/webui/chromeos/login/login_ui.h" | 5 #include "chrome/browser/ui/webui/chromeos/login/login_ui.h" |
| 6 | 6 |
| 7 #include "base/memory/ref_counted_memory.h" | 7 #include "base/memory/ref_counted_memory.h" |
| 8 #include "base/memory/singleton.h" | |
| 9 #include "base/string_piece.h" | |
| 10 #include "base/stringprintf.h" | |
| 11 #include "base/utf_string_conversions.h" | |
| 12 #include "base/values.h" | 8 #include "base/values.h" |
| 13 #include "chrome/browser/chromeos/cros/cros_library.h" | |
| 14 #include "chrome/browser/chromeos/cros/power_library.h" | |
| 15 #include "chrome/browser/chromeos/login/user_manager.h" | |
| 16 #include "chrome/browser/chromeos/login/webui_login_display.h" | |
| 17 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/ui/browser.h" | |
| 19 #include "chrome/browser/ui/browser_window.h" | |
| 20 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" | 10 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" |
| 21 #include "chrome/browser/ui/webui/chromeos/login/login_ui_helpers.h" | 11 #include "chrome/browser/ui/webui/chromeos/login/login_ui_helpers.h" |
| 12 #include "chrome/browser/ui/webui/chromeos/login/signin_screen_handler.h" |
| 22 #include "chrome/browser/ui/webui/options/chromeos/user_image_source.h" | 13 #include "chrome/browser/ui/webui/options/chromeos/user_image_source.h" |
| 23 #include "chrome/browser/ui/webui/theme_source.h" | 14 #include "chrome/browser/ui/webui/theme_source.h" |
| 24 #include "chrome/common/url_constants.h" | 15 #include "chrome/common/url_constants.h" |
| 25 #include "content/browser/browser_thread.h" | |
| 26 #include "content/browser/tab_contents/tab_contents.h" | 16 #include "content/browser/tab_contents/tab_contents.h" |
| 27 #include "grit/generated_resources.h" | |
| 28 #include "grit/theme_resources.h" | |
| 29 #include "ui/base/l10n/l10n_util.h" | |
| 30 #include "ui/base/resource/resource_bundle.h" | |
| 31 | 17 |
| 32 namespace chromeos { | 18 namespace chromeos { |
| 33 | 19 |
| 20 // Boilerplate class that is used to associate the LoginUI code with the URL |
| 21 // "chrome://login" |
| 22 class LoginUIHTMLSource : public ChromeURLDataManager::DataSource { |
| 23 public: |
| 24 explicit LoginUIHTMLSource(base::DictionaryValue* localized_strings); |
| 25 virtual ~LoginUIHTMLSource(); |
| 26 |
| 27 virtual void StartDataRequest(const std::string& path, |
| 28 bool is_incognito, |
| 29 int request_id); |
| 30 virtual std::string GetMimeType(const std::string&) const; |
| 31 |
| 32 private: |
| 33 scoped_ptr<HTMLOperationsInterface> html_operations_; |
| 34 scoped_ptr<DictionaryValue> localized_strings_; |
| 35 |
| 36 DISALLOW_COPY_AND_ASSIGN(LoginUIHTMLSource); |
| 37 }; |
| 38 |
| 34 // LoginUIHTMLSource, public: -------------------------------------------------- | 39 // LoginUIHTMLSource, public: -------------------------------------------------- |
| 35 | 40 |
| 36 LoginUIHTMLSource::LoginUIHTMLSource(MessageLoop* message_loop) | 41 LoginUIHTMLSource::LoginUIHTMLSource(base::DictionaryValue* localized_strings) |
| 37 : DataSource(chrome::kChromeUILoginHost, message_loop), | 42 : DataSource(chrome::kChromeUILoginHost, MessageLoop::current()), |
| 38 html_operations_(new HTMLOperationsInterface()) { | 43 html_operations_(new HTMLOperationsInterface()), |
| 44 localized_strings_(localized_strings) { |
| 39 } | 45 } |
| 40 | 46 |
| 41 LoginUIHTMLSource::~LoginUIHTMLSource() { | 47 LoginUIHTMLSource::~LoginUIHTMLSource() { |
| 42 } | 48 } |
| 43 | 49 |
| 44 void LoginUIHTMLSource::StartDataRequest(const std::string& path, | 50 void LoginUIHTMLSource::StartDataRequest(const std::string& path, |
| 45 bool is_incognito, | 51 bool is_incognito, |
| 46 int request_id) { | 52 int request_id) { |
| 47 DictionaryValue localized_strings; | 53 SetFontAndTextDirection(localized_strings_.get()); |
| 48 SetFontAndTextDirection(&localized_strings); | |
| 49 | 54 |
| 50 base::StringPiece login_html = html_operations_->GetLoginHTML(); | 55 base::StringPiece login_html = html_operations_->GetLoginHTML(); |
| 51 std::string full_html = html_operations_->GetFullHTML(login_html, | 56 std::string full_html = html_operations_->GetFullHTML( |
| 52 &localized_strings); | 57 login_html, localized_strings_.get()); |
| 53 | 58 |
| 54 scoped_refptr<RefCountedBytes> html_bytes( | 59 scoped_refptr<RefCountedBytes> html_bytes( |
| 55 html_operations_->CreateHTMLBytes(full_html)); | 60 html_operations_->CreateHTMLBytes(full_html)); |
| 56 SendResponse(request_id, | 61 SendResponse(request_id, |
| 57 (html_bytes.get())); | 62 (html_bytes.get())); |
| 58 } | 63 } |
| 59 | 64 |
| 60 std::string LoginUIHTMLSource::GetMimeType(const std::string&) const { | 65 std::string LoginUIHTMLSource::GetMimeType(const std::string&) const { |
| 61 return "text/html"; | 66 return "text/html"; |
| 62 } | 67 } |
| 63 | 68 |
| 64 // LoginUIHandlerDelegate, public: ------------------------------------------ | |
| 65 | |
| 66 void LoginUIHandlerDelegate::set_login_handler( | |
| 67 BaseLoginUIHandler* login_handler) { | |
| 68 login_handler_ = login_handler; | |
| 69 } | |
| 70 | |
| 71 // LoginUIHandlerDelegate, protected: ------------------------------------------ | |
| 72 | |
| 73 LoginUIHandlerDelegate::~LoginUIHandlerDelegate() {} | |
| 74 | |
| 75 // LoginUIHandler, public: ----------------------------------------------------- | |
| 76 | |
| 77 LoginUIHandler::LoginUIHandler() { | |
| 78 delegate_ = WebUILoginDisplay::GetInstance(); | |
| 79 delegate_->set_login_handler(this); | |
| 80 } | |
| 81 | |
| 82 LoginUIHandler::~LoginUIHandler() { | |
| 83 } | |
| 84 | |
| 85 WebUIMessageHandler* LoginUIHandler::Attach(WebUI* web_ui) { | |
| 86 return WebUIMessageHandler::Attach(web_ui); | |
| 87 } | |
| 88 | |
| 89 void LoginUIHandler::RegisterMessages() { | |
| 90 web_ui_->RegisterMessageCallback( | |
| 91 "LaunchIncognito", | |
| 92 NewCallback(this, | |
| 93 &LoginUIHandler::HandleLaunchIncognito)); | |
| 94 web_ui_->RegisterMessageCallback( | |
| 95 "AuthenticateUser", | |
| 96 NewCallback(this, | |
| 97 &LoginUIHandler::HandleAuthenticateUser)); | |
| 98 web_ui_->RegisterMessageCallback( | |
| 99 "ShutdownSystem", | |
| 100 NewCallback(this, | |
| 101 &LoginUIHandler::HandleShutdownSystem)); | |
| 102 web_ui_->RegisterMessageCallback( | |
| 103 "GetUsers", | |
| 104 NewCallback(this, | |
| 105 &LoginUIHandler::HandleGetUsers)); | |
| 106 web_ui_->RegisterMessageCallback( | |
| 107 "RemoveUser", | |
| 108 NewCallback(this, | |
| 109 &LoginUIHandler::HandleRemoveUser)); | |
| 110 } | |
| 111 | |
| 112 void LoginUIHandler::HandleGetUsers(const ListValue* args) { | |
| 113 ListValue get_users_args; | |
| 114 | |
| 115 // Grab the users from the user manager. | |
| 116 UserVector users = UserManager::Get()->GetUsers(); | |
| 117 for (UserVector::const_iterator it = users.begin(); | |
| 118 it != users.end(); ++it) { | |
| 119 const std::string& email = it->email(); | |
| 120 DictionaryValue* user_dict = new DictionaryValue(); | |
| 121 user_dict->SetString("name", it->GetDisplayName()); | |
| 122 user_dict->SetString("email_address", email); | |
| 123 user_dict->SetBoolean("can_remove", !email.empty()); | |
| 124 if (!email.empty()) { | |
| 125 long long timestamp = base::TimeTicks::Now().ToInternalValue(); | |
| 126 std::string image_url( | |
| 127 StringPrintf("%s%s?id=%lld", | |
| 128 chrome::kChromeUIUserImageURL, | |
| 129 email.c_str(), | |
| 130 timestamp)); | |
| 131 user_dict->SetString("image_url", image_url); | |
| 132 } else { | |
| 133 std::string image_url(std::string(chrome::kChromeUIScheme) + "://" + | |
| 134 std::string(chrome::kChromeUIThemePath) + "/IDR_LOGIN_DEFAULT_USER"); | |
| 135 user_dict->SetString("image_url", image_url); | |
| 136 } | |
| 137 get_users_args.Append(user_dict); | |
| 138 } | |
| 139 | |
| 140 // Add the Guest to the user list. | |
| 141 DictionaryValue* guest_dict = new DictionaryValue(); | |
| 142 guest_dict->SetString("name", l10n_util::GetStringUTF16(IDS_GUEST)); | |
| 143 guest_dict->SetString("email_address", ""); | |
| 144 guest_dict->SetBoolean("can_remove", false); | |
| 145 std::string image_url(std::string(chrome::kChromeUIScheme) + "://" + | |
| 146 std::string(chrome::kChromeUIThemePath) + "/IDR_LOGIN_GUEST"); | |
| 147 guest_dict->SetString("image_url", image_url); | |
| 148 get_users_args.Append(guest_dict); | |
| 149 | |
| 150 // Call the Javascript callback | |
| 151 web_ui_->CallJavascriptFunction("getUsersCallback", get_users_args); | |
| 152 } | |
| 153 | |
| 154 // LoginUIHandler, private: ---------------------------------------------------- | |
| 155 | |
| 156 void LoginUIHandler::ClearAndEnablePassword() { | |
| 157 web_ui_->CallJavascriptFunction("resetPrompt"); | |
| 158 } | |
| 159 | |
| 160 void LoginUIHandler::ShowError(const std::string& error_text, | |
| 161 const std::string& help_link_text, | |
| 162 HelpAppLauncher::HelpTopic help_topic_id) { | |
| 163 // TODO(xiyuan): Pass error + help to a propery error UI and save topic id. | |
| 164 ClearAndEnablePassword(); | |
| 165 } | |
| 166 | |
| 167 void LoginUIHandler::HandleAuthenticateUser(const ListValue* args) { | |
| 168 std::string username; | |
| 169 std::string password; | |
| 170 if (!args->GetString(0, &username) || | |
| 171 !args->GetString(1, &password)) { | |
| 172 NOTREACHED(); | |
| 173 return; | |
| 174 } | |
| 175 | |
| 176 delegate_->Login(username, password); | |
| 177 } | |
| 178 | |
| 179 void LoginUIHandler::HandleLaunchIncognito(const ListValue* args) { | |
| 180 delegate_->LoginAsGuest(); | |
| 181 } | |
| 182 | |
| 183 void LoginUIHandler::HandleShutdownSystem(const ListValue* args) { | |
| 184 DCHECK(CrosLibrary::Get()->EnsureLoaded()); | |
| 185 CrosLibrary::Get()->GetPowerLibrary()->RequestShutdown(); | |
| 186 } | |
| 187 | |
| 188 void LoginUIHandler::HandleRemoveUser(const ListValue* args) { | |
| 189 std::string email; | |
| 190 DCHECK_EQ(args->GetSize(), 1UL); | |
| 191 args->GetString(0, &email); | |
| 192 UserManager::Get()->RemoveUserFromList(email); | |
| 193 } | |
| 194 | |
| 195 // LoginUI, public: ------------------------------------------------------------ | 69 // LoginUI, public: ------------------------------------------------------------ |
| 196 | 70 |
| 197 LoginUI::LoginUI(TabContents* contents) | 71 LoginUI::LoginUI(TabContents* contents) |
| 198 : ChromeWebUI(contents) { | 72 : ChromeWebUI(contents) { |
| 199 LoginUIHandler* handler = new LoginUIHandler(); | 73 scoped_ptr<base::DictionaryValue> localized_strings( |
| 200 AddMessageHandler(handler->Attach(this)); | 74 new base::DictionaryValue); |
| 75 |
| 76 BaseScreenHandler* signin_screen_handler = new SigninScreenHandler; |
| 77 AddMessageHandler(signin_screen_handler->Attach(this)); |
| 78 signin_screen_handler->GetLocalizedStrings(localized_strings.get()); |
| 79 |
| 201 LoginUIHTMLSource* html_source = | 80 LoginUIHTMLSource* html_source = |
| 202 new LoginUIHTMLSource(MessageLoop::current()); | 81 new LoginUIHTMLSource(localized_strings.release()); |
| 203 | |
| 204 contents->profile()->GetChromeURLDataManager()->AddDataSource(html_source); | 82 contents->profile()->GetChromeURLDataManager()->AddDataSource(html_source); |
| 205 | 83 |
| 206 // Load the theme URLs. | 84 // Load the theme URLs. |
| 207 ThemeSource* theme = new ThemeSource(contents->profile()); | 85 ThemeSource* theme = new ThemeSource(contents->profile()); |
| 208 contents->profile()->GetChromeURLDataManager()->AddDataSource(theme); | 86 contents->profile()->GetChromeURLDataManager()->AddDataSource(theme); |
| 209 | 87 |
| 210 // Load the user-image URLs | 88 // Load the user-image URLs |
| 211 // Set up the chrome://userimage/ source. | 89 // Set up the chrome://userimage/ source. |
| 212 chromeos::UserImageSource* user_image_source = | 90 chromeos::UserImageSource* user_image_source = |
| 213 new chromeos::UserImageSource(); | 91 new chromeos::UserImageSource(); |
| 214 contents->profile()->GetChromeURLDataManager()->AddDataSource( | 92 contents->profile()->GetChromeURLDataManager()->AddDataSource( |
| 215 user_image_source); | 93 user_image_source); |
| 216 } | 94 } |
| 217 | 95 |
| 218 } // namespace chromeos | 96 } // namespace chromeos |
| OLD | NEW |