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 "base/mac_util.h" | 7 #include "base/mac_util.h" |
8 #include "base/sys_string_conversions.h" | 8 #include "base/sys_string_conversions.h" |
9 #import "chrome/browser/autofill/autofill_address_model_mac.h" | 9 #import "chrome/browser/autofill/autofill_address_model_mac.h" |
10 #import "chrome/browser/autofill/autofill_address_view_controller_mac.h" | 10 #import "chrome/browser/autofill/autofill_address_view_controller_mac.h" |
11 #import "chrome/browser/autofill/autofill_credit_card_model_mac.h" | 11 #import "chrome/browser/autofill/autofill_credit_card_model_mac.h" |
12 #import "chrome/browser/autofill/autofill_credit_card_view_controller_mac.h" | 12 #import "chrome/browser/autofill/autofill_credit_card_view_controller_mac.h" |
13 #import "chrome/browser/autofill/personal_data_manager.h" | |
14 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
15 #import "chrome/browser/cocoa/disclosure_view_controller.h" | 14 #import "chrome/browser/cocoa/disclosure_view_controller.h" |
16 #import "chrome/browser/cocoa/section_separator_view.h" | 15 #import "chrome/browser/cocoa/section_separator_view.h" |
17 #import "chrome/browser/cocoa/window_size_autosaver.h" | 16 #import "chrome/browser/cocoa/window_size_autosaver.h" |
18 #include "chrome/browser/pref_service.h" | 17 #include "chrome/browser/pref_service.h" |
19 #include "chrome/browser/profile.h" | 18 #include "chrome/browser/profile.h" |
20 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
21 #include "grit/generated_resources.h" | 20 #include "grit/generated_resources.h" |
22 | 21 |
23 // Private interface. | |
24 @interface AutoFillDialogController (PrivateAPI) | |
25 // Asyncronous handler for when PersonalDataManager data loads. The | |
26 // personal data manager notifies the dialog with this method when the | |
27 // data loading is complete and ready to be used. | |
28 - (void)onPersonalDataLoaded:(const std::vector<AutoFillProfile*>&)profiles | |
29 creditCards:(const std::vector<CreditCard*>&)creditCards; | |
30 @end | |
31 | |
32 namespace AutoFillDialogControllerInternal { | |
33 | |
34 // PersonalDataManagerObserver facilitates asynchronous loading of | |
35 // PersonalDataManager data before showing the AutoFill settings data to the | |
36 // user. It acts as a C++-based delegate for the |AutoFillDialogController|. | |
37 class PersonalDataManagerObserver : public PersonalDataManager::Observer { | |
38 public: | |
39 explicit PersonalDataManagerObserver( | |
40 AutoFillDialogController* controller, | |
41 PersonalDataManager* personal_data_manager, | |
42 Profile* profile) | |
43 : controller_(controller), | |
44 personal_data_manager_(personal_data_manager), | |
45 profile_(profile) { | |
46 } | |
47 | |
48 virtual ~PersonalDataManagerObserver(); | |
49 | |
50 // Notifies the observer that the PersonalDataManager has finished loading. | |
51 virtual void OnPersonalDataLoaded(); | |
52 | |
53 private: | |
54 // Utility method to remove |this| from |personal_data_manager_| as an | |
55 // observer. | |
56 void RemoveObserver(); | |
57 | |
58 // The dialog controller to be notified when the data loading completes. | |
59 // Weak reference. | |
60 AutoFillDialogController* controller_; | |
61 | |
62 // The object in which we are registered as an observer. We hold on to | |
63 // it to facilitate un-registering ourself in the destructor and in the | |
64 // |OnPersonalDataLoaded| method. This may be NULL. | |
65 // Weak reference. | |
66 PersonalDataManager* personal_data_manager_; | |
67 | |
68 // Profile of caller. Held as weak reference. May not be NULL. | |
69 Profile* profile_; | |
70 | |
71 private: | |
72 DISALLOW_COPY_AND_ASSIGN(PersonalDataManagerObserver); | |
73 }; | |
74 | |
75 // During destruction ensure that we are removed from the | |
76 // |personal_data_manager_| as an observer. | |
77 PersonalDataManagerObserver::~PersonalDataManagerObserver() { | |
78 RemoveObserver(); | |
79 } | |
80 | |
81 void PersonalDataManagerObserver::RemoveObserver() { | |
82 if (personal_data_manager_) { | |
83 personal_data_manager_->RemoveObserver(this); | |
84 } | |
85 } | |
86 | |
87 // The data is ready so display our data. Notify the dialog controller that | |
88 // the data is ready. Once done we clear the observer. | |
89 void PersonalDataManagerObserver::OnPersonalDataLoaded() { | |
90 RemoveObserver(); | |
91 [controller_ onPersonalDataLoaded:personal_data_manager_->web_profiles() | |
92 creditCards:personal_data_manager_->credit_cards()]; | |
93 } | |
94 | |
95 } // namespace AutoFillDialogControllerInternal | |
96 | |
97 @interface AutoFillDialogController (PrivateMethods) | 22 @interface AutoFillDialogController (PrivateMethods) |
98 - (void)runModalDialog; | 23 - (void)runModalDialog; |
99 - (void)installChildViews; | 24 - (void)installChildViews; |
100 @end | 25 @end |
101 | 26 |
102 @implementation AutoFillDialogController | 27 @implementation AutoFillDialogController |
103 | 28 |
104 @synthesize auxiliaryEnabled = auxiliaryEnabled_; | 29 @synthesize auxiliaryEnabled = auxiliaryEnabled_; |
105 | 30 |
106 + (void)showAutoFillDialogWithObserver:(AutoFillDialogObserver*)observer | 31 + (void)showAutoFillDialogWithObserver:(AutoFillDialogObserver*)observer |
107 profile:(Profile*)profile | 32 autoFillProfiles:(const std::vector<AutoFillProfile*>&)profiles |
108 importedProfile:(AutoFillProfile*) importedProfile | 33 creditCards:(const std::vector<CreditCard*>&)creditCards |
109 importedCreditCard:(CreditCard*) importedCreditCard { | 34 profile:(Profile*)profile { |
110 AutoFillDialogController* controller = | 35 AutoFillDialogController* controller = |
111 [AutoFillDialogController controllerWithObserver:observer | 36 [AutoFillDialogController controllerWithObserver:observer |
112 profile:profile | 37 autoFillProfiles:profiles |
113 importedProfile:importedProfile | 38 creditCards:creditCards |
114 importedCreditCard:importedCreditCard]; | 39 profile:profile]; |
115 | 40 |
116 // Only run modal dialog if it is not already being shown. | 41 // Only run modal dialog if it is not already being shown. |
117 if (![controller isWindowLoaded]) { | 42 if (![controller isWindowLoaded]) { |
118 [controller runModalDialog]; | 43 [controller runModalDialog]; |
119 } | 44 } |
120 } | 45 } |
121 | 46 |
122 - (void)awakeFromNib { | 47 - (void)awakeFromNib { |
123 [addressSectionBox_ setShowTopLine:FALSE]; | 48 [addressSectionBox_ setShowTopLine:FALSE]; |
124 | 49 [self installChildViews]; |
125 PersonalDataManager* personal_data_manager = | |
126 profile_->GetPersonalDataManager(); | |
127 DCHECK(personal_data_manager); | |
128 | |
129 if (personal_data_manager->IsDataLoaded()) { | |
130 // |personalDataManager| data is loaded, we can proceed with the contents. | |
131 [self onPersonalDataLoaded:personal_data_manager->web_profiles() | |
132 creditCards:personal_data_manager->credit_cards()]; | |
133 } else { | |
134 // |personalDataManager| data is NOT loaded, so we load it here, installing | |
135 // our observer. | |
136 personalDataManagerObserver_.reset( | |
137 new AutoFillDialogControllerInternal::PersonalDataManagerObserver( | |
138 self, personal_data_manager, profile_)); | |
139 personal_data_manager->SetObserver(personalDataManagerObserver_.get()); | |
140 } | |
141 } | 50 } |
142 | 51 |
143 // NSWindow Delegate callback. When the window closes the controller can | 52 // NSWindow Delegate callback. When the window closes the controller can |
144 // be released. | 53 // be released. |
145 - (void)windowWillClose:(NSNotification *)notification { | 54 - (void)windowWillClose:(NSNotification *)notification { |
146 // Force views to go away so they properly remove their observations. | 55 // Force views to go away so they properly remove their observations. |
147 addressFormViewControllers_.reset(); | 56 addressFormViewControllers_.reset(); |
148 creditCardFormViewControllers_.reset(); | 57 creditCardFormViewControllers_.reset(); |
149 [self autorelease]; | 58 [self autorelease]; |
150 } | 59 } |
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
449 | 358 |
450 defaultCreditCardLabel_.reset([label copy]); | 359 defaultCreditCardLabel_.reset([label copy]); |
451 return; | 360 return; |
452 } | 361 } |
453 | 362 |
454 @end | 363 @end |
455 | 364 |
456 @implementation AutoFillDialogController (ExposedForUnitTests) | 365 @implementation AutoFillDialogController (ExposedForUnitTests) |
457 | 366 |
458 + (AutoFillDialogController*)controllerWithObserver: | 367 + (AutoFillDialogController*)controllerWithObserver: |
459 (AutoFillDialogObserver*)observer | 368 (AutoFillDialogObserver*)observer |
460 profile:(Profile*)profile | 369 autoFillProfiles:(const std::vector<AutoFillProfile*>&)profiles |
461 importedProfile:(AutoFillProfile*)importedProfile | 370 creditCards:(const std::vector<CreditCard*>&)creditCards |
462 importedCreditCard:(CreditCard*)importedCreditCard { | 371 profile:(Profile*)profile { |
463 | 372 |
464 // Deallocation is done upon window close. See |windowWillClose:|. | 373 // Deallocation is done upon window close. See |windowWillClose:|. |
465 AutoFillDialogController* controller = | 374 AutoFillDialogController* controller = |
466 [[self alloc] initWithObserver:observer | 375 [[self alloc] initWithObserver:observer |
467 profile:profile | 376 autoFillProfiles:profiles |
468 importedProfile:importedProfile | 377 creditCards:creditCards |
469 importedCreditCard:importedCreditCard]; | 378 profile:profile]; |
470 return controller; | 379 return controller; |
471 } | 380 } |
472 | 381 |
473 | 382 |
474 // This is the designated initializer for this class. | 383 // This is the designated initializer for this class. |
475 // |profiles| are non-retained immutable list of autofill profiles. | 384 // |profiles| are non-retained immutable list of autofill profiles. |
476 // |creditCards| are non-retained immutable list of credit card info. | 385 // |creditCards| are non-retained immutable list of credit card info. |
477 - (id)initWithObserver:(AutoFillDialogObserver*)observer | 386 - (id)initWithObserver:(AutoFillDialogObserver*)observer |
478 profile:(Profile*)profile | 387 autoFillProfiles:(const std::vector<AutoFillProfile*>&)profiles |
479 importedProfile:(AutoFillProfile*)importedProfile | 388 creditCards:(const std::vector<CreditCard*>&)creditCards |
480 importedCreditCard:(CreditCard*)importedCreditCard { | 389 profile:(Profile*)profile { |
481 CHECK(profile); | 390 CHECK(profile); |
482 // Use initWithWindowNibPath: instead of initWithWindowNibName: so we | 391 // Use initWithWindowNibPath: instead of initWithWindowNibName: so we |
483 // can override it in a unit test. | 392 // can override it in a unit test. |
484 NSString* nibpath = [mac_util::MainAppBundle() | 393 NSString* nibpath = [mac_util::MainAppBundle() |
485 pathForResource:@"AutoFillDialog" | 394 pathForResource:@"AutoFillDialog" |
486 ofType:@"nib"]; | 395 ofType:@"nib"]; |
487 if ((self = [super initWithWindowNibPath:nibpath owner:self])) { | 396 if ((self = [super initWithWindowNibPath:nibpath owner:self])) { |
488 observer_ = observer; | 397 observer_ = observer; |
| 398 |
| 399 // Make local copy of |profiles|. |
| 400 std::vector<AutoFillProfile*>::const_iterator i; |
| 401 for (i = profiles.begin(); i != profiles.end(); ++i) |
| 402 profiles_.push_back(**i); |
| 403 |
| 404 // Make local copy of |creditCards|. |
| 405 std::vector<CreditCard*>::const_iterator j; |
| 406 for (j = creditCards.begin(); j != creditCards.end(); ++j) |
| 407 creditCards_.push_back(**j); |
| 408 |
489 profile_ = profile; | 409 profile_ = profile; |
490 importedProfile_ = importedProfile; | |
491 importedCreditCard_ = importedCreditCard; | |
492 | 410 |
493 // Use property here to trigger KVO binding. | 411 // Use property here to trigger KVO binding. |
494 [self setAuxiliaryEnabled:profile_->GetPrefs()->GetBoolean( | 412 [self setAuxiliaryEnabled:profile_->GetPrefs()->GetBoolean( |
495 prefs::kAutoFillAuxiliaryProfilesEnabled)]; | 413 prefs::kAutoFillAuxiliaryProfilesEnabled)]; |
496 | 414 |
497 // Do not use [NSMutableArray array] here; we need predictable destruction | 415 // Do not use [NSMutableArray array] here; we need predictable destruction |
498 // which will be prevented by having a reference held by an autorelease | 416 // which will be prevented by having a reference held by an autorelease |
499 // pool. | 417 // pool. |
500 | 418 |
501 // Initialize array of sub-controllers. | 419 // Initialize array of sub-controllers. |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
595 } | 513 } |
596 | 514 |
597 // During initialization the default accessors were returning faulty values | 515 // During initialization the default accessors were returning faulty values |
598 // since the controller arrays weren't set up. Poke our observers. | 516 // since the controller arrays weren't set up. Poke our observers. |
599 [self willChangeValueForKey:@"defaultAddressLabel"]; | 517 [self willChangeValueForKey:@"defaultAddressLabel"]; |
600 [self didChangeValueForKey:@"defaultAddressLabel"]; | 518 [self didChangeValueForKey:@"defaultAddressLabel"]; |
601 [self willChangeValueForKey:@"defaultCreditCardLabel"]; | 519 [self willChangeValueForKey:@"defaultCreditCardLabel"]; |
602 [self didChangeValueForKey:@"defaultCreditCardLabel"]; | 520 [self didChangeValueForKey:@"defaultCreditCardLabel"]; |
603 } | 521 } |
604 | 522 |
605 - (void)onPersonalDataLoaded:(const std::vector<AutoFillProfile*>&)profiles | |
606 creditCards:(const std::vector<CreditCard*>&)creditCards { | |
607 if (importedProfile_) { | |
608 profiles_.push_back(*importedProfile_); | |
609 } | |
610 | |
611 if (importedCreditCard_) { | |
612 creditCards_.push_back(*importedCreditCard_); | |
613 } | |
614 | |
615 // If we're not using imported data then use the data fetch from the web db. | |
616 if (!importedProfile_ && !importedCreditCard_) { | |
617 // Make local copy of |profiles|. | |
618 for (std::vector<AutoFillProfile*>::const_iterator iter = profiles.begin(); | |
619 iter != profiles.end(); ++iter) | |
620 profiles_.push_back(**iter); | |
621 | |
622 // Make local copy of |creditCards|. | |
623 for (std::vector<CreditCard*>::const_iterator iter = creditCards.begin(); | |
624 iter != creditCards.end(); ++iter) | |
625 creditCards_.push_back(**iter); | |
626 } | |
627 | |
628 [self installChildViews]; | |
629 } | |
630 | |
631 @end | 523 @end |
OLD | NEW |