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

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

Issue 1952002: AutoFill profile shouldn't be saved when cancelled during initial setup. (Closed)
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
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
472 } // namespace PreferencesWindowControllerInternal 378 } // namespace PreferencesWindowControllerInternal
473 379
474 @implementation PreferencesWindowController 380 @implementation PreferencesWindowController
475 381
476 - (id)initWithProfile:(Profile*)profile initialPage:(OptionsPage)initialPage { 382 - (id)initWithProfile:(Profile*)profile initialPage:(OptionsPage)initialPage {
477 DCHECK(profile); 383 DCHECK(profile);
478 // Use initWithWindowNibPath:: instead of initWithWindowNibName: so we 384 // Use initWithWindowNibPath:: instead of initWithWindowNibName: so we
479 // can override it in a unit test. 385 // can override it in a unit test.
480 NSString* nibPath = [mac_util::MainAppBundle() 386 NSString* nibPath = [mac_util::MainAppBundle()
481 pathForResource:@"Preferences" 387 pathForResource:@"Preferences"
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after
744 [[self window] center]; 650 [[self window] center];
745 } 651 }
746 652
747 - (void)dealloc { 653 - (void)dealloc {
748 if (syncService_) { 654 if (syncService_) {
749 syncService_->RemoveObserver(observer_.get()); 655 syncService_->RemoveObserver(observer_.get());
750 } 656 }
751 [customPagesSource_ removeObserver:self forKeyPath:@"customHomePages"]; 657 [customPagesSource_ removeObserver:self forKeyPath:@"customHomePages"];
752 [[NSNotificationCenter defaultCenter] removeObserver:self]; 658 [[NSNotificationCenter defaultCenter] removeObserver:self];
753 [self unregisterPrefObservers]; 659 [self unregisterPrefObservers];
754 personalDataManagerObserver_.reset();
755 [super dealloc]; 660 [super dealloc];
756 } 661 }
757 662
758 // Xcode 3.1.x version of Interface Builder doesn't do a lot for editing 663 // Xcode 3.1.x version of Interface Builder doesn't do a lot for editing
759 // toolbars in XIB. So the toolbar's delegate is set to the controller so it 664 // toolbars in XIB. So the toolbar's delegate is set to the controller so it
760 // can tell the toolbar what items are selectable. 665 // can tell the toolbar what items are selectable.
761 - (NSArray*)toolbarSelectableItemIdentifiers:(NSToolbar*)toolbar { 666 - (NSArray*)toolbarSelectableItemIdentifiers:(NSToolbar*)toolbar {
762 DCHECK(toolbar == toolbar_); 667 DCHECK(toolbar == toolbar_);
763 return [[toolbar_ items] valueForKey:@"itemIdentifier"]; 668 return [[toolbar_ items] valueForKey:@"itemIdentifier"];
764 } 669 }
(...skipping 480 matching lines...) Expand 10 before | Expand all | Expand 10 after
1245 [self recordUserAction:UserMetricsAction("Options_ShowAutoFillSettings")]; 1150 [self recordUserAction:UserMetricsAction("Options_ShowAutoFillSettings")];
1246 1151
1247 PersonalDataManager* personalDataManager = profile_->GetPersonalDataManager(); 1152 PersonalDataManager* personalDataManager = profile_->GetPersonalDataManager();
1248 if (!personalDataManager) { 1153 if (!personalDataManager) {
1249 // Should not reach here because button is disabled when 1154 // Should not reach here because button is disabled when
1250 // |personalDataManager| is NULL. 1155 // |personalDataManager| is NULL.
1251 NOTREACHED(); 1156 NOTREACHED();
1252 return; 1157 return;
1253 } 1158 }
1254 1159
1255 if (personalDataManager->IsDataLoaded()) { 1160 ShowAutoFillDialog(NULL, personalDataManager, profile_, NULL, NULL);
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 }
1268 } 1161 }
1269 1162
1270 // Called to import data from other browsers (Safari, Firefox, etc). 1163 // Called to import data from other browsers (Safari, Firefox, etc).
1271 - (IBAction)importData:(id)sender { 1164 - (IBAction)importData:(id)sender {
1272 UserMetrics::RecordAction(UserMetricsAction("Import_ShowDlg"), profile_); 1165 UserMetrics::RecordAction(UserMetricsAction("Import_ShowDlg"), profile_);
1273 [ImportSettingsDialogController showImportSettingsDialogForProfile:profile_]; 1166 [ImportSettingsDialogController showImportSettingsDialogForProfile:profile_];
1274 } 1167 }
1275 1168
1276 - (IBAction)resetThemeToDefault:(id)sender { 1169 - (IBAction)resetThemeToDefault:(id)sender {
1277 [self recordUserAction:UserMetricsAction("Options_ThemesReset")]; 1170 [self recordUserAction:UserMetricsAction("Options_ThemesReset")];
(...skipping 683 matching lines...) Expand 10 before | Expand all | Expand 10 after
1961 case OPTIONS_PAGE_ADVANCED: 1854 case OPTIONS_PAGE_ADVANCED:
1962 return underTheHoodView_; 1855 return underTheHoodView_;
1963 case OPTIONS_PAGE_DEFAULT: 1856 case OPTIONS_PAGE_DEFAULT:
1964 case OPTIONS_PAGE_COUNT: 1857 case OPTIONS_PAGE_COUNT:
1965 LOG(DFATAL) << "Invalid page value " << page; 1858 LOG(DFATAL) << "Invalid page value " << page;
1966 } 1859 }
1967 return basicsView_; 1860 return basicsView_;
1968 } 1861 }
1969 1862
1970 @end 1863 @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