Home | History | Annotate | Download | only in options
      1 // Copyright 2013 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/options/password_manager_handler.h"
      6 
      7 #include "base/bind.h"
      8 #include "base/prefs/pref_service.h"
      9 #include "base/strings/string_number_conversions.h"
     10 #include "base/strings/utf_string_conversions.h"
     11 #include "base/values.h"
     12 #include "chrome/browser/chrome_notification_types.h"
     13 #include "chrome/browser/profiles/profile.h"
     14 #if defined(OS_WIN) && defined(USE_ASH)
     15 #include "chrome/browser/ui/ash/ash_util.h"
     16 #endif
     17 #include "chrome/common/pref_names.h"
     18 #include "chrome/common/url_constants.h"
     19 #include "components/autofill/core/common/password_form.h"
     20 #include "content/public/browser/notification_details.h"
     21 #include "content/public/browser/notification_source.h"
     22 #include "content/public/browser/user_metrics.h"
     23 #include "content/public/browser/web_contents.h"
     24 #include "content/public/browser/web_ui.h"
     25 #include "grit/chromium_strings.h"
     26 #include "grit/generated_resources.h"
     27 #include "net/base/net_util.h"
     28 #include "ui/base/l10n/l10n_util.h"
     29 
     30 namespace options {
     31 
     32 PasswordManagerHandler::PasswordManagerHandler()
     33     : password_manager_presenter_(this) {}
     34 
     35 PasswordManagerHandler::~PasswordManagerHandler() {}
     36 
     37 Profile* PasswordManagerHandler::GetProfile() {
     38   return Profile::FromWebUI(web_ui());
     39 }
     40 
     41 #if !defined(OS_ANDROID)
     42 gfx::NativeWindow PasswordManagerHandler::GetNativeWindow() {
     43   return web_ui()->GetWebContents()->GetTopLevelNativeWindow();
     44 }
     45 #endif
     46 
     47 void PasswordManagerHandler::GetLocalizedValues(
     48     base::DictionaryValue* localized_strings) {
     49   DCHECK(localized_strings);
     50 
     51   static const OptionsStringResource resources[] = {
     52     { "savedPasswordsTitle",
     53       IDS_PASSWORDS_SHOW_PASSWORDS_TAB_TITLE },
     54     { "passwordExceptionsTitle",
     55       IDS_PASSWORDS_EXCEPTIONS_TAB_TITLE },
     56     { "passwordSearchPlaceholder",
     57       IDS_PASSWORDS_PAGE_SEARCH_PASSWORDS },
     58     { "passwordShowButton",
     59       IDS_PASSWORDS_PAGE_VIEW_SHOW_BUTTON },
     60     { "passwordHideButton",
     61       IDS_PASSWORDS_PAGE_VIEW_HIDE_BUTTON },
     62     { "passwordsNoPasswordsDescription",
     63       IDS_PASSWORDS_PAGE_VIEW_NO_PASSWORDS_DESCRIPTION },
     64     { "passwordsNoExceptionsDescription",
     65       IDS_PASSWORDS_PAGE_VIEW_NO_EXCEPTIONS_DESCRIPTION },
     66   };
     67 
     68   RegisterStrings(localized_strings, resources, arraysize(resources));
     69   RegisterTitle(localized_strings, "passwordsPage",
     70                 IDS_PASSWORDS_EXCEPTIONS_WINDOW_TITLE);
     71 
     72   localized_strings->SetString("passwordManagerLearnMoreURL",
     73                                chrome::kPasswordManagerLearnMoreURL);
     74   bool disable_show_passwords = false;
     75 
     76 #if defined(OS_WIN) && defined(USE_ASH)
     77   // We disable the ability to show passwords when running in Windows Metro
     78   // interface.  This is because we cannot pop native Win32 dialogs from the
     79   // Metro process.
     80   // TODO(wfh): Revisit this if Metro usage grows.
     81   if (chrome::IsNativeWindowInAsh(GetNativeWindow()))
     82     disable_show_passwords = true;
     83 #endif
     84 
     85   localized_strings->SetBoolean("disableShowPasswords", disable_show_passwords);
     86 }
     87 
     88 void PasswordManagerHandler::RegisterMessages() {
     89   web_ui()->RegisterMessageCallback(
     90       "updatePasswordLists",
     91       base::Bind(&PasswordManagerHandler::HandleUpdatePasswordLists,
     92                  base::Unretained(this)));
     93   web_ui()->RegisterMessageCallback(
     94       "removeSavedPassword",
     95       base::Bind(&PasswordManagerHandler::HandleRemoveSavedPassword,
     96                  base::Unretained(this)));
     97   web_ui()->RegisterMessageCallback(
     98       "removePasswordException",
     99       base::Bind(&PasswordManagerHandler::HandleRemovePasswordException,
    100                  base::Unretained(this)));
    101   web_ui()->RegisterMessageCallback(
    102       "requestShowPassword",
    103       base::Bind(&PasswordManagerHandler::HandleRequestShowPassword,
    104                  base::Unretained(this)));
    105 }
    106 
    107 void PasswordManagerHandler::InitializeHandler() {
    108   password_manager_presenter_.Initialize();
    109 }
    110 
    111 void PasswordManagerHandler::HandleRemoveSavedPassword(
    112     const base::ListValue* args) {
    113   std::string string_value = base::UTF16ToUTF8(ExtractStringValue(args));
    114   int index;
    115   if (base::StringToInt(string_value, &index) && index >= 0) {
    116     password_manager_presenter_.RemoveSavedPassword(static_cast<size_t>(index));
    117   }
    118 }
    119 
    120 void PasswordManagerHandler::HandleRemovePasswordException(
    121     const base::ListValue* args) {
    122   std::string string_value = base::UTF16ToUTF8(ExtractStringValue(args));
    123   int index;
    124   if (base::StringToInt(string_value, &index) && index >= 0) {
    125     password_manager_presenter_.RemovePasswordException(
    126         static_cast<size_t>(index));
    127   }
    128 }
    129 
    130 void PasswordManagerHandler::HandleRequestShowPassword(
    131     const base::ListValue* args) {
    132   int index;
    133   if (!ExtractIntegerValue(args, &index))
    134     NOTREACHED();
    135 
    136   password_manager_presenter_.RequestShowPassword(static_cast<size_t>(index));
    137 }
    138 
    139 void PasswordManagerHandler::ShowPassword(
    140     size_t index,
    141     const base::string16& password_value) {
    142   // Call back the front end to reveal the password.
    143   web_ui()->CallJavascriptFunction(
    144       "PasswordManager.showPassword",
    145       base::FundamentalValue(static_cast<int>(index)),
    146       base::StringValue(password_value));
    147 }
    148 
    149 void PasswordManagerHandler::HandleUpdatePasswordLists(
    150     const base::ListValue* args) {
    151   password_manager_presenter_.UpdatePasswordLists();
    152 }
    153 
    154 void PasswordManagerHandler::SetPasswordList(
    155     const ScopedVector<autofill::PasswordForm>& password_list,
    156     bool show_passwords) {
    157   base::ListValue entries;
    158   languages_ = GetProfile()->GetPrefs()->GetString(prefs::kAcceptLanguages);
    159   base::string16 placeholder(base::ASCIIToUTF16("        "));
    160   for (size_t i = 0; i < password_list.size(); ++i) {
    161     base::ListValue* entry = new base::ListValue();
    162     entry->Append(new base::StringValue(net::FormatUrl(password_list[i]->origin,
    163                                                        languages_)));
    164     entry->Append(new base::StringValue(password_list[i]->username_value));
    165     if (show_passwords) {
    166       entry->Append(new base::StringValue(password_list[i]->password_value));
    167     } else {
    168       // Use a placeholder value with the same length as the password.
    169       entry->Append(new base::StringValue(
    170           base::string16(password_list[i]->password_value.length(), ' ')));
    171     }
    172     entries.Append(entry);
    173   }
    174 
    175   web_ui()->CallJavascriptFunction("PasswordManager.setSavedPasswordsList",
    176                                    entries);
    177 }
    178 
    179 void PasswordManagerHandler::SetPasswordExceptionList(
    180     const ScopedVector<autofill::PasswordForm>& password_exception_list) {
    181   base::ListValue entries;
    182   for (size_t i = 0; i < password_exception_list.size(); ++i) {
    183     entries.Append(new base::StringValue(
    184         net::FormatUrl(password_exception_list[i]->origin, languages_)));
    185   }
    186 
    187   web_ui()->CallJavascriptFunction("PasswordManager.setPasswordExceptionsList",
    188                                    entries);
    189 }
    190 
    191 }  // namespace options
    192