| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #import "chrome/browser/autofill/autofill_text_field_mac.h" | 5 #import "chrome/browser/autofill/autofill_text_field_mac.h" |
| 6 | 6 |
| 7 #include "base/sys_string_conversions.h" | 7 #include "base/sys_string_conversions.h" |
| 8 #include "chrome/browser/autofill/credit_card.h" | 8 #include "chrome/browser/autofill/credit_card.h" |
| 9 | 9 |
| 10 @implementation AutoFillTextField | 10 @implementation AutoFillTextField |
| 11 | 11 |
| 12 - (void)awakeFromNib { | 12 - (void)awakeFromNib { |
| 13 if ([self tag] == AUTOFILL_CC_TAG) | 13 // Fields tagged with this value in the nib file will be treated as credit |
| 14 // card number fields. |
| 15 const int kAutoFillCreditCardTag = 22; |
| 16 |
| 17 if ([self tag] == kAutoFillCreditCardTag) { |
| 14 isCreditCardField_ = YES; | 18 isCreditCardField_ = YES; |
| 15 } | |
| 16 | 19 |
| 17 // Override NSResponder method for when the text field may gain focus. We | 20 // KVO bindings initialize fields prior to |awakeFromNib|. In the credit |
| 18 // call |scrollRectToVisible| to ensure that this text field is visible within | 21 // card field case we need to re-initialize the value to the obfuscated |
| 19 // the scrolling area. | 22 // version. |
| 20 - (BOOL)becomeFirstResponder { | 23 [self setObjectValue:[self objectValue]]; |
| 21 // Vertical inset is negative to indicate "outset". Provides some visual | |
| 22 // space above and below when tabbing between fields. | |
| 23 const CGFloat kVerticalInset = -40.0; | |
| 24 BOOL becoming = [super becomeFirstResponder]; | |
| 25 if (becoming) { | |
| 26 [self scrollRectToVisible:NSInsetRect([self bounds], 0.0, kVerticalInset)]; | |
| 27 } | 24 } |
| 28 return becoming; | |
| 29 } | 25 } |
| 30 | 26 |
| 31 - (void)setObjectValue:(id)object { | 27 - (void)setObjectValue:(id)object { |
| 32 if (isCreditCardField_ && [object isKindOfClass:[NSString class]]) { | 28 if (isCreditCardField_ && [object isKindOfClass:[NSString class]]) { |
| 33 // Obfuscate the number. | 29 // Obfuscate the number. |
| 34 NSString* string = object; | 30 NSString* string = object; |
| 35 CreditCard card; | 31 CreditCard card; |
| 36 card.SetInfo(AutoFillType(CREDIT_CARD_NUMBER), | 32 card.SetInfo(AutoFillType(CREDIT_CARD_NUMBER), |
| 37 base::SysNSStringToUTF16(string)); | 33 base::SysNSStringToUTF16(string)); |
| 38 NSString* starredString = base::SysUTF16ToNSString(card.ObfuscatedNumber()); | 34 NSString* starredString = base::SysUTF16ToNSString(card.ObfuscatedNumber()); |
| 39 | 35 |
| 40 [super setObjectValue:starredString]; | 36 [super setObjectValue:starredString]; |
| 41 isObfuscated_ = YES; | 37 isObfuscated_ = YES; |
| 42 obfuscatedValue_.reset([string copy]); | 38 obfuscatedValue_.reset([string copy]); |
| 43 } else { | 39 } else { |
| 44 [super setObjectValue:object]; | 40 [super setObjectValue:object]; |
| 45 } | 41 } |
| 46 } | 42 } |
| 47 | 43 |
| 48 - (id)objectValue { | 44 - (id)objectValue { |
| 49 if (isObfuscated_) { | 45 if (isObfuscated_) { |
| 50 // This should not happen. This field is bound, and its value will only be | 46 // This should not happen. This field is bound, and its value will only be |
| 51 // fetched if it is changed, and since we force selection, that should clear | 47 // fetched if it is changed, and since we force selection, that should clear |
| 52 // the obfuscation. Nevertheless, we'll be paranoid here since we don't want | 48 // the obfuscation. Nevertheless, we'll be paranoid here since we don't want |
| 53 // the obfuscating ***s to end up in the database. | 49 // the obfuscating ***s to end up in the database. |
| 50 NOTREACHED(); |
| 54 return obfuscatedValue_.get(); | 51 return obfuscatedValue_.get(); |
| 55 } else { | 52 } else { |
| 56 return [super objectValue]; | 53 return [super objectValue]; |
| 57 } | 54 } |
| 58 } | 55 } |
| 59 | 56 |
| 60 // |self| is automatically set to be the delegate of the field editor; this | 57 // |self| is automatically set to be the delegate of the field editor; this |
| 61 // method is called by the field editor. | 58 // method is called by the field editor. |
| 62 - (void)textViewDidChangeSelection:(NSNotification *)notification { | 59 - (void)textViewDidChangeSelection:(NSNotification *)notification { |
| 63 if (isCreditCardField_ && !isBeingSelected_ && isObfuscated_) { | 60 if (isCreditCardField_ && !isBeingSelected_ && isObfuscated_) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 74 - (BOOL)textShouldBeginEditing:(NSText*)textObject { | 71 - (BOOL)textShouldBeginEditing:(NSText*)textObject { |
| 75 BOOL should = [super textShouldBeginEditing:textObject]; | 72 BOOL should = [super textShouldBeginEditing:textObject]; |
| 76 // On editing, since everything is selected, the field is now clear. | 73 // On editing, since everything is selected, the field is now clear. |
| 77 isObfuscated_ = !should; | 74 isObfuscated_ = !should; |
| 78 if (!isObfuscated_) | 75 if (!isObfuscated_) |
| 79 obfuscatedValue_.reset(); | 76 obfuscatedValue_.reset(); |
| 80 return should; | 77 return should; |
| 81 } | 78 } |
| 82 | 79 |
| 83 @end | 80 @end |
| OLD | NEW |