OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/password_manager/update_password_infobar_delegate.h" |
| 6 |
| 7 #include "base/numerics/safe_conversions.h" |
| 8 #include "chrome/browser/infobars/infobar_service.h" |
| 9 #include "chrome/browser/password_manager/chrome_password_manager_client.h" |
| 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 12 #include "chrome/browser/ui/android/infobars/auto_signin_first_run_infobar.h" |
| 13 #include "chrome/browser/ui/android/infobars/update_password_infobar.h" |
| 14 #include "chrome/browser/ui/passwords/manage_passwords_view_utils.h" |
| 15 #include "chrome/grit/chromium_strings.h" |
| 16 #include "chrome/grit/generated_resources.h" |
| 17 #include "components/browser_sync/browser/profile_sync_service.h" |
| 18 #include "components/infobars/core/infobar.h" |
| 19 #include "components/password_manager/core/browser/password_bubble_experiment.h" |
| 20 #include "content/public/browser/web_contents.h" |
| 21 #include "ui/base/l10n/l10n_util.h" |
| 22 |
| 23 // static |
| 24 void UpdatePasswordInfoBarDelegate::Create( |
| 25 content::WebContents* web_contents, |
| 26 scoped_ptr<password_manager::PasswordFormManager> form_to_save) { |
| 27 Profile* profile = |
| 28 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 29 sync_driver::SyncService* sync_service = |
| 30 ProfileSyncServiceFactory::GetForProfile(profile); |
| 31 bool is_smartlock_branding_enabled = |
| 32 password_bubble_experiment::IsSmartLockBrandingEnabled(sync_service); |
| 33 InfoBarService::FromWebContents(web_contents) |
| 34 ->AddInfoBar(make_scoped_ptr(new UpdatePasswordInfoBar( |
| 35 make_scoped_ptr(new UpdatePasswordInfoBarDelegate( |
| 36 web_contents, std::move(form_to_save), |
| 37 is_smartlock_branding_enabled))))); |
| 38 } |
| 39 |
| 40 UpdatePasswordInfoBarDelegate::UpdatePasswordInfoBarDelegate( |
| 41 content::WebContents* web_contents, |
| 42 scoped_ptr<password_manager::PasswordFormManager> form_to_update, |
| 43 bool is_smartlock_branding_enabled) |
| 44 : PasswordManagerInfoBarDelegate(), |
| 45 is_smartlock_branding_enabled_(is_smartlock_branding_enabled) { |
| 46 // TODO(crbug.com/577129): Add histograms. |
| 47 passwords_data_.set_client( |
| 48 ChromePasswordManagerClient::FromWebContents(web_contents)); |
| 49 passwords_data_.OnUpdatePassword(std::move(form_to_update)); |
| 50 if (is_smartlock_branding_enabled) { |
| 51 base::string16 branding_ = l10n_util::GetStringUTF16( |
| 52 IDS_PASSWORD_MANAGER_SMART_LOCK_FOR_PASSWORDS); |
| 53 } else { |
| 54 branding_ = l10n_util::GetStringUTF16(IDS_PASSWORD_MANAGER_TITLE_BRAND); |
| 55 } |
| 56 } |
| 57 |
| 58 UpdatePasswordInfoBarDelegate::~UpdatePasswordInfoBarDelegate() {} |
| 59 |
| 60 bool UpdatePasswordInfoBarDelegate::ShowMultipleAccounts() const { |
| 61 const password_manager::PasswordFormManager* form_manager = |
| 62 passwords_data_.form_manager(); |
| 63 bool is_password_overriden = |
| 64 form_manager ? form_manager->password_overridden() : false; |
| 65 return GetCurrentForms().size() > 1 && !is_password_overriden; |
| 66 } |
| 67 |
| 68 base::string16 UpdatePasswordInfoBarDelegate::GetUsernameForOneAccountCase() { |
| 69 password_manager::PasswordFormManager* form_manager = |
| 70 passwords_data_.form_manager(); |
| 71 return form_manager->pending_credentials().username_value; |
| 72 } |
| 73 |
| 74 void UpdatePasswordInfoBarDelegate::UpdatePassword(int form_index) { |
| 75 if (ShowMultipleAccounts()) |
| 76 UpdatePasswordWithCurrentForm(form_index); |
| 77 else |
| 78 UpdatePasswordWithPending(); |
| 79 } |
| 80 |
| 81 infobars::InfoBarDelegate::InfoBarIdentifier |
| 82 UpdatePasswordInfoBarDelegate::GetIdentifier() const { |
| 83 return UPDATE_PASSWORD_INFOBAR_DELEGATE; |
| 84 } |
| 85 |
| 86 base::string16 UpdatePasswordInfoBarDelegate::GetButtonLabel( |
| 87 InfoBarButton button) const { |
| 88 return l10n_util::GetStringUTF16((button == BUTTON_OK) |
| 89 ? IDS_PASSWORD_MANAGER_UPDATE_BUTTON |
| 90 : IDS_PASSWORD_MANAGER_CANCEL_BUTTON); |
| 91 } |
| 92 |
| 93 bool UpdatePasswordInfoBarDelegate::Accept() { |
| 94 return true; |
| 95 } |
| 96 |
| 97 bool UpdatePasswordInfoBarDelegate::Cancel() { |
| 98 return true; |
| 99 } |
| 100 |
| 101 void UpdatePasswordInfoBarDelegate::UpdatePasswordWithCurrentForm( |
| 102 int form_index) { |
| 103 CHECK_LT(base::checked_cast<unsigned int>(form_index), |
| 104 GetCurrentForms().size()); |
| 105 password_manager::PasswordFormManager* form_manager = |
| 106 passwords_data_.form_manager(); |
| 107 form_manager->Update(*GetCurrentForms()[form_index]); |
| 108 } |
| 109 |
| 110 void UpdatePasswordInfoBarDelegate::UpdatePasswordWithPending() { |
| 111 password_manager::PasswordFormManager* form_manager = |
| 112 passwords_data_.form_manager(); |
| 113 form_manager->Update(form_manager->pending_credentials()); |
| 114 } |
OLD | NEW |