| 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_dialog_controller_mac.h" | 5 #import "chrome/browser/autofill/autofill_dialog_controller_mac.h" |
| 6 #include "app/l10n_util.h" | 6 #include "app/l10n_util.h" |
| 7 #include "app/resource_bundle.h" | 7 #include "app/resource_bundle.h" |
| 8 #include "base/mac_util.h" | 8 #include "base/mac_util.h" |
| 9 #include "base/sys_string_conversions.h" | 9 #include "base/sys_string_conversions.h" |
| 10 #include "chrome/browser/browser.h" | 10 #include "chrome/browser/browser.h" |
| 11 #include "chrome/browser/browser_list.h" | 11 #include "chrome/browser/browser_list.h" |
| 12 #import "chrome/browser/autofill/autofill_address_model_mac.h" | 12 #import "chrome/browser/autofill/autofill_address_model_mac.h" |
| 13 #import "chrome/browser/autofill/autofill_address_sheet_controller_mac.h" | 13 #import "chrome/browser/autofill/autofill_address_sheet_controller_mac.h" |
| 14 #import "chrome/browser/autofill/autofill_credit_card_model_mac.h" | 14 #import "chrome/browser/autofill/autofill_credit_card_model_mac.h" |
| 15 #import "chrome/browser/autofill/autofill_credit_card_sheet_controller_mac.h" | 15 #import "chrome/browser/autofill/autofill_credit_card_sheet_controller_mac.h" |
| 16 #import "chrome/browser/autofill/personal_data_manager.h" | 16 #import "chrome/browser/autofill/personal_data_manager.h" |
| 17 #include "chrome/browser/browser_process.h" | 17 #include "chrome/browser/browser_process.h" |
| 18 #import "chrome/browser/cocoa/window_size_autosaver.h" | 18 #import "chrome/browser/cocoa/window_size_autosaver.h" |
| 19 #include "chrome/browser/pref_service.h" | 19 #include "chrome/browser/pref_service.h" |
| 20 #include "chrome/browser/profile.h" | 20 #include "chrome/browser/profile.h" |
| 21 #include "chrome/common/notification_details.h" |
| 22 #include "chrome/common/notification_observer.h" |
| 21 #include "chrome/common/pref_names.h" | 23 #include "chrome/common/pref_names.h" |
| 22 #include "grit/generated_resources.h" | 24 #include "grit/generated_resources.h" |
| 23 #include "grit/app_resources.h" | 25 #include "grit/app_resources.h" |
| 24 #include "grit/theme_resources.h" | 26 #include "grit/theme_resources.h" |
| 25 | 27 |
| 26 namespace { | 28 namespace { |
| 27 | 29 |
| 28 // Update profile labels passed as |input|. When profile data changes as a | 30 // Update profile labels passed as |input|. When profile data changes as a |
| 29 // result of adding new profiles, edititing existing profiles, or deleting a | 31 // result of adding new profiles, edititing existing profiles, or deleting a |
| 30 // profile, then the list of profiles need to have their derived labels | 32 // profile, then the list of profiles need to have their derived labels |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 @end | 80 @end |
| 79 | 81 |
| 80 // Private interface. | 82 // Private interface. |
| 81 @interface AutoFillDialogController (PrivateMethods) | 83 @interface AutoFillDialogController (PrivateMethods) |
| 82 // Asyncronous handler for when PersonalDataManager data loads. The | 84 // Asyncronous handler for when PersonalDataManager data loads. The |
| 83 // personal data manager notifies the dialog with this method when the | 85 // personal data manager notifies the dialog with this method when the |
| 84 // data loading is complete and ready to be used. | 86 // data loading is complete and ready to be used. |
| 85 - (void)onPersonalDataLoaded:(const std::vector<AutoFillProfile*>&)profiles | 87 - (void)onPersonalDataLoaded:(const std::vector<AutoFillProfile*>&)profiles |
| 86 creditCards:(const std::vector<CreditCard*>&)creditCards; | 88 creditCards:(const std::vector<CreditCard*>&)creditCards; |
| 87 | 89 |
| 90 // Called upon changes to AutoFill preferences that should be reflected in the |
| 91 // UI. |
| 92 - (void)onPrefChanged:(const std::string&)prefName; |
| 93 |
| 88 // Returns true if |row| is an index to a valid profile in |tableView_|, and | 94 // Returns true if |row| is an index to a valid profile in |tableView_|, and |
| 89 // false otherwise. | 95 // false otherwise. |
| 90 - (BOOL)isProfileRow:(NSInteger)row; | 96 - (BOOL)isProfileRow:(NSInteger)row; |
| 91 | 97 |
| 92 // Returns true if |row| is an index to the profile group row in |tableView_|, | 98 // Returns true if |row| is an index to the profile group row in |tableView_|, |
| 93 // and false otherwise. | 99 // and false otherwise. |
| 94 - (BOOL)isProfileGroupRow:(NSInteger)row; | 100 - (BOOL)isProfileGroupRow:(NSInteger)row; |
| 95 | 101 |
| 96 // Returns true if |row| is an index to a valid credit card in |tableView_|, and | 102 // Returns true if |row| is an index to a valid credit card in |tableView_|, and |
| 97 // false otherwise. | 103 // false otherwise. |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 } | 183 } |
| 178 | 184 |
| 179 // The data is ready so display our data. Notify the dialog controller that | 185 // The data is ready so display our data. Notify the dialog controller that |
| 180 // the data is ready. Once done we clear the observer. | 186 // the data is ready. Once done we clear the observer. |
| 181 void PersonalDataManagerObserver::OnPersonalDataLoaded() { | 187 void PersonalDataManagerObserver::OnPersonalDataLoaded() { |
| 182 RemoveObserver(); | 188 RemoveObserver(); |
| 183 [controller_ onPersonalDataLoaded:personal_data_manager_->web_profiles() | 189 [controller_ onPersonalDataLoaded:personal_data_manager_->web_profiles() |
| 184 creditCards:personal_data_manager_->credit_cards()]; | 190 creditCards:personal_data_manager_->credit_cards()]; |
| 185 } | 191 } |
| 186 | 192 |
| 193 // Bridges preference changed notifications to the dialog controller. |
| 194 class PrefObserver : public NotificationObserver { |
| 195 public: |
| 196 explicit PrefObserver(AutoFillDialogController* controller) |
| 197 : controller_(controller) {} |
| 198 |
| 199 // Overridden from NotificationObserver: |
| 200 virtual void Observe(NotificationType type, |
| 201 const NotificationSource& source, |
| 202 const NotificationDetails& details) { |
| 203 if (type == NotificationType::PREF_CHANGED) { |
| 204 const std::string* pref = Details<std::string>(details).ptr(); |
| 205 if (pref) { |
| 206 [controller_ onPrefChanged:*pref]; |
| 207 } |
| 208 } |
| 209 } |
| 210 |
| 211 private: |
| 212 AutoFillDialogController* controller_; |
| 213 |
| 214 DISALLOW_COPY_AND_ASSIGN(PrefObserver); |
| 215 }; |
| 216 |
| 187 } // namespace AutoFillDialogControllerInternal | 217 } // namespace AutoFillDialogControllerInternal |
| 188 | 218 |
| 189 @implementation AutoFillDialogController | 219 @implementation AutoFillDialogController |
| 190 | 220 |
| 191 @synthesize autoFillEnabled = autoFillEnabled_; | 221 @synthesize autoFillEnabled = autoFillEnabled_; |
| 222 @synthesize autoFillManaged = autoFillManaged_; |
| 223 @synthesize autoFillManagedAndDisabled = autoFillManagedAndDisabled_; |
| 192 @synthesize auxiliaryEnabled = auxiliaryEnabled_; | 224 @synthesize auxiliaryEnabled = auxiliaryEnabled_; |
| 193 @synthesize itemIsSelected = itemIsSelected_; | 225 @synthesize itemIsSelected = itemIsSelected_; |
| 194 @synthesize multipleSelected = multipleSelected_; | 226 @synthesize multipleSelected = multipleSelected_; |
| 195 | 227 |
| 196 + (void)showAutoFillDialogWithObserver:(AutoFillDialogObserver*)observer | 228 + (void)showAutoFillDialogWithObserver:(AutoFillDialogObserver*)observer |
| 197 profile:(Profile*)profile | 229 profile:(Profile*)profile |
| 198 importedProfile:(AutoFillProfile*) importedProfile | 230 importedProfile:(AutoFillProfile*) importedProfile |
| 199 importedCreditCard:(CreditCard*) importedCreditCard { | 231 importedCreditCard:(CreditCard*) importedCreditCard { |
| 200 AutoFillDialogController* controller = | 232 AutoFillDialogController* controller = |
| 201 [AutoFillDialogController controllerWithObserver:observer | 233 [AutoFillDialogController controllerWithObserver:observer |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 // NSWindow Delegate callback. When the window closes the controller can | 270 // NSWindow Delegate callback. When the window closes the controller can |
| 239 // be released. | 271 // be released. |
| 240 - (void)windowWillClose:(NSNotification *)notification { | 272 - (void)windowWillClose:(NSNotification *)notification { |
| 241 [tableView_ setDataSource:nil]; | 273 [tableView_ setDataSource:nil]; |
| 242 [tableView_ setDelegate:nil]; | 274 [tableView_ setDelegate:nil]; |
| 243 [self autorelease]; | 275 [self autorelease]; |
| 244 } | 276 } |
| 245 | 277 |
| 246 // Called when the user clicks the save button. | 278 // Called when the user clicks the save button. |
| 247 - (IBAction)save:(id)sender { | 279 - (IBAction)save:(id)sender { |
| 248 // If we have an |observer_| then communicate the changes back. | 280 // If we have an |observer_| then communicate the changes back, unless |
| 249 if (observer_) { | 281 // AutoFill has been disabled through policy in the mean time. |
| 250 profile_->GetPrefs()->SetBoolean(prefs::kAutoFillEnabled, autoFillEnabled_); | 282 if (observer_ && !autoFillManagedAndDisabled_) { |
| 283 prefAutoFillEnabled_.SetValueIfNotManaged(autoFillEnabled_); |
| 251 profile_->GetPrefs()->SetBoolean(prefs::kAutoFillAuxiliaryProfilesEnabled, | 284 profile_->GetPrefs()->SetBoolean(prefs::kAutoFillAuxiliaryProfilesEnabled, |
| 252 auxiliaryEnabled_); | 285 auxiliaryEnabled_); |
| 253 observer_->OnAutoFillDialogApply(&profiles_, &creditCards_); | 286 observer_->OnAutoFillDialogApply(&profiles_, &creditCards_); |
| 254 } | 287 } |
| 255 [self closeDialog]; | 288 [self closeDialog]; |
| 256 } | 289 } |
| 257 | 290 |
| 258 // Called when the user clicks the cancel button. All we need to do is stop | 291 // Called when the user clicks the cancel button. All we need to do is stop |
| 259 // the modal session. | 292 // the modal session. |
| 260 - (IBAction)cancel:(id)sender { | 293 - (IBAction)cancel:(id)sender { |
| (...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 624 AutoFillDialogController* controller = | 657 AutoFillDialogController* controller = |
| 625 [[self alloc] initWithObserver:observer | 658 [[self alloc] initWithObserver:observer |
| 626 profile:profile | 659 profile:profile |
| 627 importedProfile:importedProfile | 660 importedProfile:importedProfile |
| 628 importedCreditCard:importedCreditCard]; | 661 importedCreditCard:importedCreditCard]; |
| 629 return controller; | 662 return controller; |
| 630 } | 663 } |
| 631 | 664 |
| 632 | 665 |
| 633 // This is the designated initializer for this class. | 666 // This is the designated initializer for this class. |
| 634 // |profiles| are non-retained immutable list of autofill profiles. | 667 // |profiles| are non-retained immutable list of AutoFill profiles. |
| 635 // |creditCards| are non-retained immutable list of credit card info. | 668 // |creditCards| are non-retained immutable list of credit card info. |
| 636 - (id)initWithObserver:(AutoFillDialogObserver*)observer | 669 - (id)initWithObserver:(AutoFillDialogObserver*)observer |
| 637 profile:(Profile*)profile | 670 profile:(Profile*)profile |
| 638 importedProfile:(AutoFillProfile*)importedProfile | 671 importedProfile:(AutoFillProfile*)importedProfile |
| 639 importedCreditCard:(CreditCard*)importedCreditCard { | 672 importedCreditCard:(CreditCard*)importedCreditCard { |
| 640 DCHECK(profile); | 673 DCHECK(profile); |
| 641 // Use initWithWindowNibPath: instead of initWithWindowNibName: so we | 674 // Use initWithWindowNibPath: instead of initWithWindowNibName: so we |
| 642 // can override it in a unit test. | 675 // can override it in a unit test. |
| 643 NSString* nibpath = [mac_util::MainAppBundle() | 676 NSString* nibpath = [mac_util::MainAppBundle() |
| 644 pathForResource:@"AutoFillDialog" | 677 pathForResource:@"AutoFillDialog" |
| 645 ofType:@"nib"]; | 678 ofType:@"nib"]; |
| 646 if ((self = [super initWithWindowNibPath:nibpath owner:self])) { | 679 if ((self = [super initWithWindowNibPath:nibpath owner:self])) { |
| 647 // Initialize member variables based on input. | 680 // Initialize member variables based on input. |
| 648 observer_ = observer; | 681 observer_ = observer; |
| 649 profile_ = profile; | 682 profile_ = profile; |
| 650 importedProfile_ = importedProfile; | 683 importedProfile_ = importedProfile; |
| 651 importedCreditCard_ = importedCreditCard; | 684 importedCreditCard_ = importedCreditCard; |
| 652 | 685 |
| 653 // Use property here to trigger KVO binding. | 686 // Initialize the preference observer and watch kAutoFillEnabled. |
| 654 [self setAutoFillEnabled:profile_->GetPrefs()->GetBoolean( | 687 prefObserver_.reset( |
| 655 prefs::kAutoFillEnabled)]; | 688 new AutoFillDialogControllerInternal::PrefObserver(self)); |
| 689 prefAutoFillEnabled_.Init(prefs::kAutoFillEnabled, profile_->GetPrefs(), |
| 690 prefObserver_.get()); |
| 691 |
| 692 // Call onPrefChanged in order to initialize UI state of the checkbox and |
| 693 // save button. |
| 694 [self onPrefChanged:prefs::kAutoFillEnabled]; |
| 656 | 695 |
| 657 // Use property here to trigger KVO binding. | 696 // Use property here to trigger KVO binding. |
| 658 [self setAuxiliaryEnabled:profile_->GetPrefs()->GetBoolean( | 697 [self setAuxiliaryEnabled:profile_->GetPrefs()->GetBoolean( |
| 659 prefs::kAutoFillAuxiliaryProfilesEnabled)]; | 698 prefs::kAutoFillAuxiliaryProfilesEnabled)]; |
| 660 | 699 |
| 661 // Do not use [NSMutableArray array] here; we need predictable destruction | 700 // Do not use [NSMutableArray array] here; we need predictable destruction |
| 662 // which will be prevented by having a reference held by an autorelease | 701 // which will be prevented by having a reference held by an autorelease |
| 663 // pool. | 702 // pool. |
| 664 } | 703 } |
| 665 return self; | 704 return self; |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 743 | 782 |
| 744 // Make local copy of |creditCards|. | 783 // Make local copy of |creditCards|. |
| 745 for (std::vector<CreditCard*>::const_iterator iter = creditCards.begin(); | 784 for (std::vector<CreditCard*>::const_iterator iter = creditCards.begin(); |
| 746 iter != creditCards.end(); ++iter) | 785 iter != creditCards.end(); ++iter) |
| 747 creditCards_.push_back(**iter); | 786 creditCards_.push_back(**iter); |
| 748 } | 787 } |
| 749 | 788 |
| 750 UpdateProfileLabels(&profiles_); | 789 UpdateProfileLabels(&profiles_); |
| 751 } | 790 } |
| 752 | 791 |
| 792 - (void)onPrefChanged:(const std::string&)prefName { |
| 793 if (prefName == prefs::kAutoFillEnabled) { |
| 794 [self setAutoFillEnabled:prefAutoFillEnabled_.GetValue()]; |
| 795 [self setAutoFillManaged:prefAutoFillEnabled_.IsManaged()]; |
| 796 [self setAutoFillManagedAndDisabled: |
| 797 prefAutoFillEnabled_.IsManaged() && !prefAutoFillEnabled_.GetValue()]; |
| 798 } else { |
| 799 NOTREACHED(); |
| 800 } |
| 801 } |
| 802 |
| 753 - (BOOL)isProfileRow:(NSInteger)row { | 803 - (BOOL)isProfileRow:(NSInteger)row { |
| 754 if (row > 0 && static_cast<size_t>(row) <= profiles_.size()) | 804 if (row > 0 && static_cast<size_t>(row) <= profiles_.size()) |
| 755 return YES; | 805 return YES; |
| 756 return NO; | 806 return NO; |
| 757 } | 807 } |
| 758 | 808 |
| 759 - (BOOL)isProfileGroupRow:(NSInteger)row { | 809 - (BOOL)isProfileGroupRow:(NSInteger)row { |
| 760 if (row == 0) | 810 if (row == 0) |
| 761 return YES; | 811 return YES; |
| 762 return NO; | 812 return NO; |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 840 if (!image) { | 890 if (!image) { |
| 841 image = rb.GetNSImageNamed(IDR_INPUT_GOOD); | 891 image = rb.GetNSImageNamed(IDR_INPUT_GOOD); |
| 842 DCHECK(image); | 892 DCHECK(image); |
| 843 return image; | 893 return image; |
| 844 } | 894 } |
| 845 | 895 |
| 846 return nil; | 896 return nil; |
| 847 } | 897 } |
| 848 | 898 |
| 849 @end | 899 @end |
| OLD | NEW |