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/chromeos/options/passphrase_textfield.h" 6 7 #include "base/strings/utf_string_conversions.h" 8 9 namespace chromeos { 10 PassphraseTextfield()11PassphraseTextfield::PassphraseTextfield() 12 : Textfield(views::Textfield::STYLE_OBSCURED), 13 show_fake_(false), 14 changed_(true) { 15 } 16 SetShowFake(bool show_fake)17void PassphraseTextfield::SetShowFake(bool show_fake) { 18 show_fake_ = show_fake; 19 if (show_fake_) 20 SetFakePassphrase(); 21 else 22 ClearFakePassphrase(); 23 } 24 OnFocus()25void PassphraseTextfield::OnFocus() { 26 // If showing the fake password, then clear it when focused. 27 if (show_fake_ && !changed_) 28 ClearFakePassphrase(); 29 Textfield::OnFocus(); 30 } 31 OnBlur()32void PassphraseTextfield::OnBlur() { 33 // If password is not changed, then show the fake password when blurred. 34 if (show_fake_ && text().empty()) 35 SetFakePassphrase(); 36 Textfield::OnBlur(); 37 } 38 GetPassphrase()39std::string PassphraseTextfield::GetPassphrase() { 40 return changed_ ? UTF16ToUTF8(text()) : std::string(); 41 } 42 SetFakePassphrase()43void PassphraseTextfield::SetFakePassphrase() { 44 CR_DEFINE_STATIC_LOCAL(base::string16, fake_passphrase, 45 (ASCIIToUTF16("********"))); 46 SetText(fake_passphrase); 47 changed_ = false; 48 } 49 ClearFakePassphrase()50void PassphraseTextfield::ClearFakePassphrase() { 51 SetText(base::string16()); 52 changed_ = true; 53 } 54 55 } // namespace chromeos 56