Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(140)

Side by Side Diff: chrome/browser/cocoa/preferences_window_controller.mm

Issue 1902003: Revert 46424 - AutoFill profile shouldn't be saved when cancelled during init... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 10 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/cocoa/preferences_window_controller.h" 5 #import "chrome/browser/cocoa/preferences_window_controller.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "app/l10n_util.h" 9 #include "app/l10n_util.h"
10 #include "app/l10n_util_mac.h" 10 #include "app/l10n_util_mac.h"
(...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after
368 368
369 // Overridden from ProfileSyncServiceObserver. 369 // Overridden from ProfileSyncServiceObserver.
370 virtual void OnStateChanged() { 370 virtual void OnStateChanged() {
371 [controller_ syncStateChanged]; 371 [controller_ syncStateChanged];
372 } 372 }
373 373
374 private: 374 private:
375 PreferencesWindowController* controller_; // weak, owns us 375 PreferencesWindowController* controller_; // weak, owns us
376 }; 376 };
377 377
378 // PersonalDataManagerObserver facilitates asynchronous loading of
379 // PersonalDataManager data before showing the auto fill settings dialog to the
380 // user. It acts as a C++-based delegate for the |PreferencesWindowController|.
381 class PersonalDataManagerObserver : public PersonalDataManager::Observer {
382 public:
383 explicit PersonalDataManagerObserver(
384 PersonalDataManager* personal_data_manager,
385 Profile* profile)
386 : personal_data_manager_(personal_data_manager),
387 profile_(profile) {
388 }
389
390 virtual ~PersonalDataManagerObserver();
391
392 // Notifies the observer that the PersonalDataManager has finished loading.
393 virtual void OnPersonalDataLoaded();
394
395 // Static method to dispatch to |ShowAutoFillDialog| method in autofill
396 // module. This is public to facilitate direct external call when the
397 // data manager has already loaded its data.
398 static void ShowAutoFillDialog(PersonalDataManager* personal_data_manager,
399 Profile* profile);
400
401 private:
402 // Utility method to remove |this| from |personal_data_manager_| as an
403 // observer.
404 void RemoveObserver();
405
406 // The object in which we are registered as an observer. We hold on to
407 // it to facilitate un-registering ourself in the destructor and in the
408 // |OnPersonalDataLoaded| method. This may be NULL.
409 // Weak reference.
410 PersonalDataManager* personal_data_manager_;
411
412 // Profile of caller. Held as weak reference. May not be NULL.
413 Profile* profile_;
414
415 private:
416 DISALLOW_COPY_AND_ASSIGN(PersonalDataManagerObserver);
417 };
418
419 // During destruction ensure that we are removed from the
420 // |personal_data_manager_| as an observer.
421 PersonalDataManagerObserver::~PersonalDataManagerObserver() {
422 RemoveObserver();
423 }
424
425 void PersonalDataManagerObserver::RemoveObserver() {
426 if (personal_data_manager_) {
427 personal_data_manager_->RemoveObserver(this);
428 }
429 }
430
431 // The data is ready so display our dialog. Recursively call
432 // |showAutoFillSettings:| to try again now knowing that the
433 // |PersonalDataManager| is ready. Once done we clear the observer
434 // (deleting |this| in the process).
435 void PersonalDataManagerObserver::OnPersonalDataLoaded() {
436 RemoveObserver();
437 PersonalDataManagerObserver::ShowAutoFillDialog(personal_data_manager_,
438 profile_);
439 }
440
441 // Dispatches request to show the autofill dialog. If there are no profiles
442 // in the |personal_data_manager| the we create a new one here. Similary with
443 // credit card info.
444 void PersonalDataManagerObserver::ShowAutoFillDialog(
445 PersonalDataManager* personal_data_manager, Profile* profile) {
446 DCHECK(profile);
447 if (!personal_data_manager)
448 return;
449
450 std::vector<AutoFillProfile*> profiles =
451 personal_data_manager->web_profiles();
452 AutoFillProfile autofill_profile(ASCIIToUTF16(""), 0);
453 if (profiles.size() == 0) {
454 string16 new_profile_name =
455 l10n_util::GetStringUTF16(IDS_AUTOFILL_NEW_ADDRESS);
456 autofill_profile.set_label(new_profile_name);
457 profiles.push_back(&autofill_profile);
458 }
459
460 std::vector<CreditCard*> credit_cards = personal_data_manager->credit_cards();
461 CreditCard credit_card(ASCIIToUTF16(""), 0);
462 if (credit_cards.size() == 0) {
463 string16 new_credit_card_name =
464 l10n_util::GetStringUTF16(IDS_AUTOFILL_NEW_CREDITCARD);
465 credit_card.set_label(new_credit_card_name);
466 credit_cards.push_back(&credit_card);
467 }
468
469 ::ShowAutoFillDialog(personal_data_manager, profiles, credit_cards, profile);
470 }
471
378 } // namespace PreferencesWindowControllerInternal 472 } // namespace PreferencesWindowControllerInternal
379 473
380 @implementation PreferencesWindowController 474 @implementation PreferencesWindowController
381 475
382 - (id)initWithProfile:(Profile*)profile initialPage:(OptionsPage)initialPage { 476 - (id)initWithProfile:(Profile*)profile initialPage:(OptionsPage)initialPage {
383 DCHECK(profile); 477 DCHECK(profile);
384 // Use initWithWindowNibPath:: instead of initWithWindowNibName: so we 478 // Use initWithWindowNibPath:: instead of initWithWindowNibName: so we
385 // can override it in a unit test. 479 // can override it in a unit test.
386 NSString* nibPath = [mac_util::MainAppBundle() 480 NSString* nibPath = [mac_util::MainAppBundle()
387 pathForResource:@"Preferences" 481 pathForResource:@"Preferences"
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after
650 [[self window] center]; 744 [[self window] center];
651 } 745 }
652 746
653 - (void)dealloc { 747 - (void)dealloc {
654 if (syncService_) { 748 if (syncService_) {
655 syncService_->RemoveObserver(observer_.get()); 749 syncService_->RemoveObserver(observer_.get());
656 } 750 }
657 [customPagesSource_ removeObserver:self forKeyPath:@"customHomePages"]; 751 [customPagesSource_ removeObserver:self forKeyPath:@"customHomePages"];
658 [[NSNotificationCenter defaultCenter] removeObserver:self]; 752 [[NSNotificationCenter defaultCenter] removeObserver:self];
659 [self unregisterPrefObservers]; 753 [self unregisterPrefObservers];
754 personalDataManagerObserver_.reset();
660 [super dealloc]; 755 [super dealloc];
661 } 756 }
662 757
663 // Xcode 3.1.x version of Interface Builder doesn't do a lot for editing 758 // Xcode 3.1.x version of Interface Builder doesn't do a lot for editing
664 // toolbars in XIB. So the toolbar's delegate is set to the controller so it 759 // toolbars in XIB. So the toolbar's delegate is set to the controller so it
665 // can tell the toolbar what items are selectable. 760 // can tell the toolbar what items are selectable.
666 - (NSArray*)toolbarSelectableItemIdentifiers:(NSToolbar*)toolbar { 761 - (NSArray*)toolbarSelectableItemIdentifiers:(NSToolbar*)toolbar {
667 DCHECK(toolbar == toolbar_); 762 DCHECK(toolbar == toolbar_);
668 return [[toolbar_ items] valueForKey:@"itemIdentifier"]; 763 return [[toolbar_ items] valueForKey:@"itemIdentifier"];
669 } 764 }
(...skipping 480 matching lines...) Expand 10 before | Expand all | Expand 10 after
1150 [self recordUserAction:UserMetricsAction("Options_ShowAutoFillSettings")]; 1245 [self recordUserAction:UserMetricsAction("Options_ShowAutoFillSettings")];
1151 1246
1152 PersonalDataManager* personalDataManager = profile_->GetPersonalDataManager(); 1247 PersonalDataManager* personalDataManager = profile_->GetPersonalDataManager();
1153 if (!personalDataManager) { 1248 if (!personalDataManager) {
1154 // Should not reach here because button is disabled when 1249 // Should not reach here because button is disabled when
1155 // |personalDataManager| is NULL. 1250 // |personalDataManager| is NULL.
1156 NOTREACHED(); 1251 NOTREACHED();
1157 return; 1252 return;
1158 } 1253 }
1159 1254
1160 ShowAutoFillDialog(NULL, personalDataManager, profile_, NULL, NULL); 1255 if (personalDataManager->IsDataLoaded()) {
1256 // |personalDataManager| data is loaded, we can proceed with the dialog.
1257 PreferencesWindowControllerInternal::
1258 PersonalDataManagerObserver::ShowAutoFillDialog(personalDataManager,
1259 profile_);
1260 } else {
1261 // |personalDataManager| data is NOT loaded, so we load it here, installing
1262 // our observer.
1263 personalDataManagerObserver_.reset(
1264 new PreferencesWindowControllerInternal::PersonalDataManagerObserver(
1265 personalDataManager, profile_));
1266 personalDataManager->SetObserver(personalDataManagerObserver_.get());
1267 }
1161 } 1268 }
1162 1269
1163 // Called to import data from other browsers (Safari, Firefox, etc). 1270 // Called to import data from other browsers (Safari, Firefox, etc).
1164 - (IBAction)importData:(id)sender { 1271 - (IBAction)importData:(id)sender {
1165 UserMetrics::RecordAction(UserMetricsAction("Import_ShowDlg"), profile_); 1272 UserMetrics::RecordAction(UserMetricsAction("Import_ShowDlg"), profile_);
1166 [ImportSettingsDialogController showImportSettingsDialogForProfile:profile_]; 1273 [ImportSettingsDialogController showImportSettingsDialogForProfile:profile_];
1167 } 1274 }
1168 1275
1169 - (IBAction)resetThemeToDefault:(id)sender { 1276 - (IBAction)resetThemeToDefault:(id)sender {
1170 [self recordUserAction:UserMetricsAction("Options_ThemesReset")]; 1277 [self recordUserAction:UserMetricsAction("Options_ThemesReset")];
(...skipping 683 matching lines...) Expand 10 before | Expand all | Expand 10 after
1854 case OPTIONS_PAGE_ADVANCED: 1961 case OPTIONS_PAGE_ADVANCED:
1855 return underTheHoodView_; 1962 return underTheHoodView_;
1856 case OPTIONS_PAGE_DEFAULT: 1963 case OPTIONS_PAGE_DEFAULT:
1857 case OPTIONS_PAGE_COUNT: 1964 case OPTIONS_PAGE_COUNT:
1858 LOG(DFATAL) << "Invalid page value " << page; 1965 LOG(DFATAL) << "Invalid page value " << page;
1859 } 1966 }
1860 return basicsView_; 1967 return basicsView_;
1861 } 1968 }
1862 1969
1863 @end 1970 @end
OLDNEW
« no previous file with comments | « chrome/browser/cocoa/preferences_window_controller.h ('k') | chrome/browser/gtk/options/content_page_gtk.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698