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" |
13 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
14 #import "chrome/browser/cocoa/disclosure_view_controller.h" | 15 #import "chrome/browser/cocoa/disclosure_view_controller.h" |
15 #import "chrome/browser/cocoa/section_separator_view.h" | 16 #import "chrome/browser/cocoa/section_separator_view.h" |
16 #import "chrome/browser/cocoa/window_size_autosaver.h" | 17 #import "chrome/browser/cocoa/window_size_autosaver.h" |
17 #include "chrome/browser/pref_service.h" | 18 #include "chrome/browser/pref_service.h" |
18 #include "chrome/browser/profile.h" | 19 #include "chrome/browser/profile.h" |
19 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
20 #include "grit/generated_resources.h" | 21 #include "grit/generated_resources.h" |
21 | 22 |
| 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 |
22 @interface AutoFillDialogController (PrivateMethods) | 97 @interface AutoFillDialogController (PrivateMethods) |
23 - (void)runModalDialog; | 98 - (void)runModalDialog; |
24 - (void)installChildViews; | 99 - (void)installChildViews; |
25 @end | 100 @end |
26 | 101 |
27 @implementation AutoFillDialogController | 102 @implementation AutoFillDialogController |
28 | 103 |
29 @synthesize auxiliaryEnabled = auxiliaryEnabled_; | 104 @synthesize auxiliaryEnabled = auxiliaryEnabled_; |
30 | 105 |
31 + (void)showAutoFillDialogWithObserver:(AutoFillDialogObserver*)observer | 106 + (void)showAutoFillDialogWithObserver:(AutoFillDialogObserver*)observer |
32 autoFillProfiles:(const std::vector<AutoFillProfile*>&)profiles | 107 profile:(Profile*)profile |
33 creditCards:(const std::vector<CreditCard*>&)creditCards | 108 importedProfile:(AutoFillProfile*) importedProfile |
34 profile:(Profile*)profile { | 109 importedCreditCard:(CreditCard*) importedCreditCard { |
35 AutoFillDialogController* controller = | 110 AutoFillDialogController* controller = |
36 [AutoFillDialogController controllerWithObserver:observer | 111 [AutoFillDialogController controllerWithObserver:observer |
37 autoFillProfiles:profiles | 112 profile:profile |
38 creditCards:creditCards | 113 importedProfile:importedProfile |
39 profile:profile]; | 114 importedCreditCard:importedCreditCard]; |
40 | 115 |
41 // Only run modal dialog if it is not already being shown. | 116 // Only run modal dialog if it is not already being shown. |
42 if (![controller isWindowLoaded]) { | 117 if (![controller isWindowLoaded]) { |
43 [controller runModalDialog]; | 118 [controller runModalDialog]; |
44 } | 119 } |
45 } | 120 } |
46 | 121 |
47 - (void)awakeFromNib { | 122 - (void)awakeFromNib { |
48 [addressSectionBox_ setShowTopLine:FALSE]; | 123 [addressSectionBox_ setShowTopLine:FALSE]; |
49 [self installChildViews]; | 124 |
| 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 } |
50 } | 141 } |
51 | 142 |
52 // NSWindow Delegate callback. When the window closes the controller can | 143 // NSWindow Delegate callback. When the window closes the controller can |
53 // be released. | 144 // be released. |
54 - (void)windowWillClose:(NSNotification *)notification { | 145 - (void)windowWillClose:(NSNotification *)notification { |
55 // Force views to go away so they properly remove their observations. | 146 // Force views to go away so they properly remove their observations. |
56 addressFormViewControllers_.reset(); | 147 addressFormViewControllers_.reset(); |
57 creditCardFormViewControllers_.reset(); | 148 creditCardFormViewControllers_.reset(); |
58 [self autorelease]; | 149 [self autorelease]; |
59 } | 150 } |
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
358 | 449 |
359 defaultCreditCardLabel_.reset([label copy]); | 450 defaultCreditCardLabel_.reset([label copy]); |
360 return; | 451 return; |
361 } | 452 } |
362 | 453 |
363 @end | 454 @end |
364 | 455 |
365 @implementation AutoFillDialogController (ExposedForUnitTests) | 456 @implementation AutoFillDialogController (ExposedForUnitTests) |
366 | 457 |
367 + (AutoFillDialogController*)controllerWithObserver: | 458 + (AutoFillDialogController*)controllerWithObserver: |
368 (AutoFillDialogObserver*)observer | 459 (AutoFillDialogObserver*)observer |
369 autoFillProfiles:(const std::vector<AutoFillProfile*>&)profiles | 460 profile:(Profile*)profile |
370 creditCards:(const std::vector<CreditCard*>&)creditCards | 461 importedProfile:(AutoFillProfile*)importedProfile |
371 profile:(Profile*)profile { | 462 importedCreditCard:(CreditCard*)importedCreditCard { |
372 | 463 |
373 // Deallocation is done upon window close. See |windowWillClose:|. | 464 // Deallocation is done upon window close. See |windowWillClose:|. |
374 AutoFillDialogController* controller = | 465 AutoFillDialogController* controller = |
375 [[self alloc] initWithObserver:observer | 466 [[self alloc] initWithObserver:observer |
376 autoFillProfiles:profiles | 467 profile:profile |
377 creditCards:creditCards | 468 importedProfile:importedProfile |
378 profile:profile]; | 469 importedCreditCard:importedCreditCard]; |
379 return controller; | 470 return controller; |
380 } | 471 } |
381 | 472 |
382 | 473 |
383 // This is the designated initializer for this class. | 474 // This is the designated initializer for this class. |
384 // |profiles| are non-retained immutable list of autofill profiles. | 475 // |profiles| are non-retained immutable list of autofill profiles. |
385 // |creditCards| are non-retained immutable list of credit card info. | 476 // |creditCards| are non-retained immutable list of credit card info. |
386 - (id)initWithObserver:(AutoFillDialogObserver*)observer | 477 - (id)initWithObserver:(AutoFillDialogObserver*)observer |
387 autoFillProfiles:(const std::vector<AutoFillProfile*>&)profiles | 478 profile:(Profile*)profile |
388 creditCards:(const std::vector<CreditCard*>&)creditCards | 479 importedProfile:(AutoFillProfile*)importedProfile |
389 profile:(Profile*)profile { | 480 importedCreditCard:(CreditCard*)importedCreditCard { |
390 CHECK(profile); | 481 CHECK(profile); |
391 // Use initWithWindowNibPath: instead of initWithWindowNibName: so we | 482 // Use initWithWindowNibPath: instead of initWithWindowNibName: so we |
392 // can override it in a unit test. | 483 // can override it in a unit test. |
393 NSString* nibpath = [mac_util::MainAppBundle() | 484 NSString* nibpath = [mac_util::MainAppBundle() |
394 pathForResource:@"AutoFillDialog" | 485 pathForResource:@"AutoFillDialog" |
395 ofType:@"nib"]; | 486 ofType:@"nib"]; |
396 if ((self = [super initWithWindowNibPath:nibpath owner:self])) { | 487 if ((self = [super initWithWindowNibPath:nibpath owner:self])) { |
397 observer_ = observer; | 488 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 | |
409 profile_ = profile; | 489 profile_ = profile; |
| 490 importedProfile_ = importedProfile; |
| 491 importedCreditCard_ = importedCreditCard; |
410 | 492 |
411 // Use property here to trigger KVO binding. | 493 // Use property here to trigger KVO binding. |
412 [self setAuxiliaryEnabled:profile_->GetPrefs()->GetBoolean( | 494 [self setAuxiliaryEnabled:profile_->GetPrefs()->GetBoolean( |
413 prefs::kAutoFillAuxiliaryProfilesEnabled)]; | 495 prefs::kAutoFillAuxiliaryProfilesEnabled)]; |
414 | 496 |
415 // Do not use [NSMutableArray array] here; we need predictable destruction | 497 // Do not use [NSMutableArray array] here; we need predictable destruction |
416 // which will be prevented by having a reference held by an autorelease | 498 // which will be prevented by having a reference held by an autorelease |
417 // pool. | 499 // pool. |
418 | 500 |
419 // Initialize array of sub-controllers. | 501 // Initialize array of sub-controllers. |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
513 } | 595 } |
514 | 596 |
515 // During initialization the default accessors were returning faulty values | 597 // During initialization the default accessors were returning faulty values |
516 // since the controller arrays weren't set up. Poke our observers. | 598 // since the controller arrays weren't set up. Poke our observers. |
517 [self willChangeValueForKey:@"defaultAddressLabel"]; | 599 [self willChangeValueForKey:@"defaultAddressLabel"]; |
518 [self didChangeValueForKey:@"defaultAddressLabel"]; | 600 [self didChangeValueForKey:@"defaultAddressLabel"]; |
519 [self willChangeValueForKey:@"defaultCreditCardLabel"]; | 601 [self willChangeValueForKey:@"defaultCreditCardLabel"]; |
520 [self didChangeValueForKey:@"defaultCreditCardLabel"]; | 602 [self didChangeValueForKey:@"defaultCreditCardLabel"]; |
521 } | 603 } |
522 | 604 |
| 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 |
523 @end | 631 @end |
OLD | NEW |