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/update_password_infobar.h" |
| 13 #include "chrome/browser/ui/passwords/manage_passwords_view_utils.h" |
| 14 #include "chrome/grit/chromium_strings.h" |
| 15 #include "chrome/grit/generated_resources.h" |
| 16 #include "components/browser_sync/browser/profile_sync_service.h" |
| 17 #include "components/infobars/core/infobar.h" |
| 18 #include "components/password_manager/core/browser/password_bubble_experiment.h" |
| 19 #include "content/public/browser/web_contents.h" |
| 20 #include "ui/base/l10n/l10n_util.h" |
| 21 |
| 22 // static |
| 23 void UpdatePasswordInfoBarDelegate::Create( |
| 24 content::WebContents* web_contents, |
| 25 scoped_ptr<password_manager::PasswordFormManager> form_to_save) { |
| 26 Profile* profile = |
| 27 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 28 sync_driver::SyncService* sync_service = |
| 29 ProfileSyncServiceFactory::GetForProfile(profile); |
| 30 bool is_smartlock_branding_enabled = |
| 31 password_bubble_experiment::IsSmartLockBrandingEnabled(sync_service); |
| 32 InfoBarService::FromWebContents(web_contents) |
| 33 ->AddInfoBar(make_scoped_ptr(new UpdatePasswordInfoBar( |
| 34 make_scoped_ptr(new UpdatePasswordInfoBarDelegate( |
| 35 web_contents, std::move(form_to_save), |
| 36 is_smartlock_branding_enabled))))); |
| 37 } |
| 38 |
| 39 UpdatePasswordInfoBarDelegate::~UpdatePasswordInfoBarDelegate() {} |
| 40 |
| 41 bool UpdatePasswordInfoBarDelegate::ShowMultipleAccounts() const { |
| 42 const password_manager::PasswordFormManager* form_manager = |
| 43 passwords_data_.form_manager(); |
| 44 bool is_password_overriden = |
| 45 form_manager ? form_manager->password_overridden() : false; |
| 46 return GetCurrentForms().size() > 1 && !is_password_overriden; |
| 47 } |
| 48 |
| 49 const std::vector<const autofill::PasswordForm*>& |
| 50 UpdatePasswordInfoBarDelegate::GetCurrentForms() const { |
| 51 return passwords_data_.GetCurrentForms(); |
| 52 } |
| 53 |
| 54 base::string16 UpdatePasswordInfoBarDelegate::GetUsernameForOneAccountCase() { |
| 55 password_manager::PasswordFormManager* form_manager = |
| 56 passwords_data_.form_manager(); |
| 57 return form_manager->pending_credentials().username_value; |
| 58 } |
| 59 |
| 60 UpdatePasswordInfoBarDelegate::UpdatePasswordInfoBarDelegate( |
| 61 content::WebContents* web_contents, |
| 62 scoped_ptr<password_manager::PasswordFormManager> form_to_update, |
| 63 bool is_smartlock_branding_enabled) |
| 64 : PasswordManagerInfoBarDelegate(), |
| 65 is_smartlock_branding_enabled_(is_smartlock_branding_enabled) { |
| 66 // TODO(crbug.com/577129): Add histograms. |
| 67 passwords_data_.set_client( |
| 68 ChromePasswordManagerClient::FromWebContents(web_contents)); |
| 69 passwords_data_.OnUpdatePassword(std::move(form_to_update)); |
| 70 branding_ = l10n_util::GetStringUTF16( |
| 71 is_smartlock_branding_enabled |
| 72 ? IDS_PASSWORD_MANAGER_SMART_LOCK_FOR_PASSWORDS |
| 73 : IDS_PASSWORD_MANAGER_TITLE_BRAND); |
| 74 } |
| 75 |
| 76 infobars::InfoBarDelegate::InfoBarIdentifier |
| 77 UpdatePasswordInfoBarDelegate::GetIdentifier() const { |
| 78 return UPDATE_PASSWORD_INFOBAR_DELEGATE; |
| 79 } |
| 80 |
| 81 base::string16 UpdatePasswordInfoBarDelegate::GetButtonLabel( |
| 82 InfoBarButton button) const { |
| 83 return l10n_util::GetStringUTF16((button == BUTTON_OK) |
| 84 ? IDS_PASSWORD_MANAGER_UPDATE_BUTTON |
| 85 : IDS_PASSWORD_MANAGER_CANCEL_BUTTON); |
| 86 } |
| 87 |
| 88 bool UpdatePasswordInfoBarDelegate::Accept() { |
| 89 UpdatePasswordInfoBar* update_password_infobar = |
| 90 static_cast<UpdatePasswordInfoBar*>(infobar()); |
| 91 password_manager::PasswordFormManager* form_manager = |
| 92 passwords_data_.form_manager(); |
| 93 if (ShowMultipleAccounts()) { |
| 94 int form_index = update_password_infobar->GetIdOfSelectedUsername(); |
| 95 DCHECK_GE(form_index, 0); |
| 96 DCHECK_LT(static_cast<size_t>(form_index), GetCurrentForms().size()); |
| 97 form_manager->Update(*GetCurrentForms()[form_index]); |
| 98 } else { |
| 99 form_manager->Update(form_manager->pending_credentials()); |
| 100 } |
| 101 return true; |
| 102 } |
| 103 |
| 104 bool UpdatePasswordInfoBarDelegate::Cancel() { |
| 105 return true; |
| 106 } |
OLD | NEW |