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/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 409 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
420 private: | 420 private: |
421 PreferencesWindowController* controller_; // weak, owns us | 421 PreferencesWindowController* controller_; // weak, owns us |
422 }; | 422 }; |
423 | 423 |
424 // PersonalDataManagerObserver facilitates asynchronous loading of | 424 // PersonalDataManagerObserver facilitates asynchronous loading of |
425 // PersonalDataManager data before showing the auto fill settings dialog to the | 425 // PersonalDataManager data before showing the auto fill settings dialog to the |
426 // user. It acts as a C++-based delegate for the |PreferencesWindowController|. | 426 // user. It acts as a C++-based delegate for the |PreferencesWindowController|. |
427 class PersonalDataManagerObserver : public PersonalDataManager::Observer { | 427 class PersonalDataManagerObserver : public PersonalDataManager::Observer { |
428 public: | 428 public: |
429 explicit PersonalDataManagerObserver( | 429 explicit PersonalDataManagerObserver( |
430 PersonalDataManager* personal_data_manager) | 430 PersonalDataManager* personal_data_manager, |
431 : personal_data_manager_(personal_data_manager) { | 431 Profile* profile) |
| 432 : personal_data_manager_(personal_data_manager), |
| 433 profile_(profile) { |
432 } | 434 } |
433 | 435 |
434 virtual ~PersonalDataManagerObserver(); | 436 virtual ~PersonalDataManagerObserver(); |
435 | 437 |
436 // Notifies the observer that the PersonalDataManager has finished loading. | 438 // Notifies the observer that the PersonalDataManager has finished loading. |
437 virtual void OnPersonalDataLoaded(); | 439 virtual void OnPersonalDataLoaded(); |
438 | 440 |
439 // Static method to dispatch to |ShowAutoFillDialog| method in autofill | 441 // Static method to dispatch to |ShowAutoFillDialog| method in autofill |
440 // module. This is public to facilitate direct external call when the | 442 // module. This is public to facilitate direct external call when the |
441 // data manager has already loaded its data. | 443 // data manager has already loaded its data. |
442 static void ShowAutoFillDialog(PersonalDataManager* personal_data_manager); | 444 static void ShowAutoFillDialog(PersonalDataManager* personal_data_manager, |
| 445 Profile* profile); |
443 | 446 |
444 private: | 447 private: |
445 // Utility method to remove |this| from |personal_data_manager_| as an | 448 // Utility method to remove |this| from |personal_data_manager_| as an |
446 // observer. | 449 // observer. |
447 void RemoveObserver(); | 450 void RemoveObserver(); |
448 | 451 |
449 // The object in which we are registered as an observer. We hold on to | 452 // The object in which we are registered as an observer. We hold on to |
450 // it to facilitate un-registering ourself in the destructor and in the | 453 // it to facilitate un-registering ourself in the destructor and in the |
451 // |OnPersonalDataLoaded| method. This may be NULL. | 454 // |OnPersonalDataLoaded| method. This may be NULL. |
452 // Weak reference. | 455 // Weak reference. |
453 PersonalDataManager* personal_data_manager_; | 456 PersonalDataManager* personal_data_manager_; |
454 | 457 |
| 458 // Profile of caller. Held as weak reference. May not be NULL. |
| 459 Profile* profile_; |
| 460 |
455 private: | 461 private: |
456 DISALLOW_COPY_AND_ASSIGN(PersonalDataManagerObserver); | 462 DISALLOW_COPY_AND_ASSIGN(PersonalDataManagerObserver); |
457 }; | 463 }; |
458 | 464 |
459 // During destruction ensure that we are removed from the | 465 // During destruction ensure that we are removed from the |
460 // |personal_data_manager_| as an observer. | 466 // |personal_data_manager_| as an observer. |
461 PersonalDataManagerObserver::~PersonalDataManagerObserver() { | 467 PersonalDataManagerObserver::~PersonalDataManagerObserver() { |
462 RemoveObserver(); | 468 RemoveObserver(); |
463 } | 469 } |
464 | 470 |
465 void PersonalDataManagerObserver::RemoveObserver() { | 471 void PersonalDataManagerObserver::RemoveObserver() { |
466 if (personal_data_manager_) { | 472 if (personal_data_manager_) { |
467 personal_data_manager_->RemoveObserver(this); | 473 personal_data_manager_->RemoveObserver(this); |
468 } | 474 } |
469 } | 475 } |
470 | 476 |
471 // The data is ready so display our dialog. Recursively call | 477 // The data is ready so display our dialog. Recursively call |
472 // |showAutoFillSettings:| to try again now knowing that the | 478 // |showAutoFillSettings:| to try again now knowing that the |
473 // |PersonalDataManager| is ready. Once done we clear the observer | 479 // |PersonalDataManager| is ready. Once done we clear the observer |
474 // (deleting |this| in the process). | 480 // (deleting |this| in the process). |
475 void PersonalDataManagerObserver::OnPersonalDataLoaded() { | 481 void PersonalDataManagerObserver::OnPersonalDataLoaded() { |
476 RemoveObserver(); | 482 RemoveObserver(); |
477 PersonalDataManagerObserver::ShowAutoFillDialog(personal_data_manager_); | 483 PersonalDataManagerObserver::ShowAutoFillDialog(personal_data_manager_, |
| 484 profile_); |
478 } | 485 } |
479 | 486 |
480 // Dispatches request to show the autofill dialog. If there are no profiles | 487 // Dispatches request to show the autofill dialog. If there are no profiles |
481 // in the |personal_data_manager| the we create a new one here. Similary with | 488 // in the |personal_data_manager| the we create a new one here. Similary with |
482 // credit card info. | 489 // credit card info. |
483 void PersonalDataManagerObserver::ShowAutoFillDialog( | 490 void PersonalDataManagerObserver::ShowAutoFillDialog( |
484 PersonalDataManager* personal_data_manager) { | 491 PersonalDataManager* personal_data_manager, Profile* profile) { |
| 492 DCHECK(profile); |
485 if (!personal_data_manager) | 493 if (!personal_data_manager) |
486 return; | 494 return; |
487 | 495 |
488 std::vector<AutoFillProfile*> profiles = personal_data_manager->profiles(); | 496 std::vector<AutoFillProfile*> profiles = personal_data_manager->profiles(); |
489 AutoFillProfile profile(ASCIIToUTF16(""), 0); | 497 AutoFillProfile autofill_profile(ASCIIToUTF16(""), 0); |
490 if (profiles.size() == 0) { | 498 if (profiles.size() == 0) { |
491 string16 new_profile_name = | 499 string16 new_profile_name = |
492 l10n_util::GetStringUTF16(IDS_AUTOFILL_NEW_ADDRESS); | 500 l10n_util::GetStringUTF16(IDS_AUTOFILL_NEW_ADDRESS); |
493 profile.set_label(new_profile_name); | 501 autofill_profile.set_label(new_profile_name); |
494 profiles.push_back(&profile); | 502 profiles.push_back(&autofill_profile); |
495 } | 503 } |
496 | 504 |
497 std::vector<CreditCard*> credit_cards = personal_data_manager->credit_cards(); | 505 std::vector<CreditCard*> credit_cards = personal_data_manager->credit_cards(); |
498 CreditCard credit_card(ASCIIToUTF16(""), 0); | 506 CreditCard credit_card(ASCIIToUTF16(""), 0); |
499 if (credit_cards.size() == 0) { | 507 if (credit_cards.size() == 0) { |
500 string16 new_credit_card_name = | 508 string16 new_credit_card_name = |
501 l10n_util::GetStringUTF16(IDS_AUTOFILL_NEW_CREDITCARD); | 509 l10n_util::GetStringUTF16(IDS_AUTOFILL_NEW_CREDITCARD); |
502 credit_card.set_label(new_credit_card_name); | 510 credit_card.set_label(new_credit_card_name); |
503 credit_cards.push_back(&credit_card); | 511 credit_cards.push_back(&credit_card); |
504 } | 512 } |
505 | 513 |
506 ::ShowAutoFillDialog(personal_data_manager, profiles, credit_cards); | 514 ::ShowAutoFillDialog(personal_data_manager, profiles, credit_cards, profile); |
507 } | 515 } |
508 | 516 |
509 | 517 |
510 @implementation PreferencesWindowController | 518 @implementation PreferencesWindowController |
511 | 519 |
512 - (id)initWithProfile:(Profile*)profile initialPage:(OptionsPage)initialPage { | 520 - (id)initWithProfile:(Profile*)profile initialPage:(OptionsPage)initialPage { |
513 DCHECK(profile); | 521 DCHECK(profile); |
514 // Use initWithWindowNibPath:: instead of initWithWindowNibName: so we | 522 // Use initWithWindowNibPath:: instead of initWithWindowNibName: so we |
515 // can override it in a unit test. | 523 // can override it in a unit test. |
516 NSString* nibPath = [mac_util::MainAppBundle() | 524 NSString* nibPath = [mac_util::MainAppBundle() |
(...skipping 743 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1260 PersonalDataManager* personalDataManager = profile_->GetPersonalDataManager(); | 1268 PersonalDataManager* personalDataManager = profile_->GetPersonalDataManager(); |
1261 if (!personalDataManager) { | 1269 if (!personalDataManager) { |
1262 // Should not reach here because button is disabled when | 1270 // Should not reach here because button is disabled when |
1263 // |personalDataManager| is NULL. | 1271 // |personalDataManager| is NULL. |
1264 NOTREACHED(); | 1272 NOTREACHED(); |
1265 return; | 1273 return; |
1266 } | 1274 } |
1267 | 1275 |
1268 if (personalDataManager->IsDataLoaded()) { | 1276 if (personalDataManager->IsDataLoaded()) { |
1269 // |personalDataManager| data is loaded, we can proceed with the dialog. | 1277 // |personalDataManager| data is loaded, we can proceed with the dialog. |
1270 PersonalDataManagerObserver::ShowAutoFillDialog(personalDataManager); | 1278 PersonalDataManagerObserver::ShowAutoFillDialog(personalDataManager, |
| 1279 profile_); |
1271 } else { | 1280 } else { |
1272 // |personalDataManager| data is NOT loaded, so we load it here, installing | 1281 // |personalDataManager| data is NOT loaded, so we load it here, installing |
1273 // our observer. | 1282 // our observer. |
1274 personalDataManagerObserver_.reset( | 1283 personalDataManagerObserver_.reset( |
1275 new PersonalDataManagerObserver(personalDataManager)); | 1284 new PersonalDataManagerObserver(personalDataManager, profile_)); |
1276 personalDataManager->SetObserver(personalDataManagerObserver_.get()); | 1285 personalDataManager->SetObserver(personalDataManagerObserver_.get()); |
1277 } | 1286 } |
1278 } | 1287 } |
1279 | 1288 |
1280 // Called to import data from other browsers (Safari, Firefox, etc). | 1289 // Called to import data from other browsers (Safari, Firefox, etc). |
1281 - (IBAction)importData:(id)sender { | 1290 - (IBAction)importData:(id)sender { |
1282 UserMetrics::RecordAction("Import_ShowDlg", profile_); | 1291 UserMetrics::RecordAction("Import_ShowDlg", profile_); |
1283 [ImportSettingsDialogController showImportSettingsDialogForProfile:profile_]; | 1292 [ImportSettingsDialogController showImportSettingsDialogForProfile:profile_]; |
1284 } | 1293 } |
1285 | 1294 |
(...skipping 620 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1906 case OPTIONS_PAGE_ADVANCED: | 1915 case OPTIONS_PAGE_ADVANCED: |
1907 return underTheHoodView_; | 1916 return underTheHoodView_; |
1908 case OPTIONS_PAGE_DEFAULT: | 1917 case OPTIONS_PAGE_DEFAULT: |
1909 case OPTIONS_PAGE_COUNT: | 1918 case OPTIONS_PAGE_COUNT: |
1910 LOG(DFATAL) << "Invalid page value " << page; | 1919 LOG(DFATAL) << "Invalid page value " << page; |
1911 } | 1920 } |
1912 return basicsView_; | 1921 return basicsView_; |
1913 } | 1922 } |
1914 | 1923 |
1915 @end | 1924 @end |
OLD | NEW |