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

Side by Side Diff: components/autofill/core/browser/autofill_cc_infobar_delegate.cc

Issue 710453002: [Autofill] Componentize AutofillCCInfoBarDelegate. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove infobar if its associated page content is destroyed. Created 5 years, 11 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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #include "chrome/browser/autofill/autofill_cc_infobar_delegate.h" 5 #include "components/autofill/core/browser/autofill_cc_infobar_delegate.h"
6
7 #include <map>
6 8
7 #include "base/logging.h" 9 #include "base/logging.h"
8 #include "chrome/browser/infobars/infobar_service.h" 10 #include "components/autofill/core/browser/autofill_driver.h"
9 #include "chrome/grit/generated_resources.h"
10 #include "chrome/grit/google_chrome_strings.h"
11 #include "components/autofill/core/browser/credit_card.h" 11 #include "components/autofill/core/browser/credit_card.h"
12 #include "components/autofill/core/browser/personal_data_manager.h" 12 #include "components/autofill/core/browser/personal_data_manager.h"
13 #include "components/autofill/core/common/autofill_constants.h" 13 #include "components/autofill/core/common/autofill_constants.h"
14 #include "components/infobars/core/infobar.h" 14 #include "components/infobars/core/infobar.h"
15 #include "content/public/browser/page_navigator.h" 15 #include "components/infobars/core/infobar_manager.h"
16 #include "content/public/browser/web_contents.h" 16 #include "grit/components_scaled_resources.h"
17 #include "content/public/browser/web_contents_delegate.h"
18 #include "grit/components_strings.h" 17 #include "grit/components_strings.h"
19 #include "grit/theme_resources.h"
20 #include "ui/base/l10n/l10n_util.h" 18 #include "ui/base/l10n/l10n_util.h"
21 19
22 namespace autofill { 20 namespace autofill {
23 21
22 namespace {
23
24 std::map<AutofillDriver*, AutofillCCInfoBarDelegate*> driver_to_delegate_map;
25
26 void AddPairToMap(AutofillDriver* driver, AutofillCCInfoBarDelegate* delegate) {
27 std::map<AutofillDriver*, AutofillCCInfoBarDelegate*>::iterator it =
28 driver_to_delegate_map.find(driver);
29 if (it == driver_to_delegate_map.end()) {
30 driver_to_delegate_map.insert(
31 std::pair<AutofillDriver*, AutofillCCInfoBarDelegate*>(driver,
32 delegate));
33 } else {
34 it->second = delegate;
35 }
36 }
37
38 void RemovePairFromMap(AutofillDriver* driver,
39 AutofillCCInfoBarDelegate* delegate) {
40 std::map<AutofillDriver*, AutofillCCInfoBarDelegate*>::iterator it =
41 driver_to_delegate_map.find(driver);
42 if (it != driver_to_delegate_map.end())
43 driver_to_delegate_map.erase(it);
44 }
Ilya Sherman 2015/01/06 02:54:59 This all seems needlessly complex. I think we sho
Peter Kasting 2015/01/06 03:01:50 (4) Provide LinkClicked as a method on the client
Pritam Nikam 2015/01/06 12:47:42 Done.
45
46 } // namespace
47
24 // static 48 // static
25 void AutofillCCInfoBarDelegate::Create( 49 void AutofillCCInfoBarDelegate::Create(
26 InfoBarService* infobar_service, 50 infobars::InfoBarManager* infobar_manager,
51 AutofillDriver* autofill_driver,
27 const base::Closure& save_card_callback) { 52 const base::Closure& save_card_callback) {
28 infobar_service->AddInfoBar( 53 infobar_manager->AddInfoBar(
29 infobar_service->CreateConfirmInfoBar(scoped_ptr<ConfirmInfoBarDelegate>( 54 infobar_manager->CreateConfirmInfoBar(scoped_ptr<ConfirmInfoBarDelegate>(
30 new AutofillCCInfoBarDelegate(save_card_callback)))); 55 new AutofillCCInfoBarDelegate(autofill_driver, save_card_callback))));
56 }
57
58 // static
59 AutofillCCInfoBarDelegate* AutofillCCInfoBarDelegate::FromAutofillDriver(
60 AutofillDriver* autofill_driver) {
61 std::map<AutofillDriver*, AutofillCCInfoBarDelegate*>::iterator it =
62 driver_to_delegate_map.find(autofill_driver);
63 if (it != driver_to_delegate_map.end())
64 return it->second;
65
66 return nullptr;
67 }
68
69 void AutofillCCInfoBarDelegate::CloseInfoBar() {
70 RemovePairFromMap(autofill_driver_, this);
71 infobar()->Hide(true);
31 } 72 }
32 73
33 AutofillCCInfoBarDelegate::AutofillCCInfoBarDelegate( 74 AutofillCCInfoBarDelegate::AutofillCCInfoBarDelegate(
75 AutofillDriver* autofill_driver,
34 const base::Closure& save_card_callback) 76 const base::Closure& save_card_callback)
35 : ConfirmInfoBarDelegate(), 77 : ConfirmInfoBarDelegate(),
78 autofill_driver_(autofill_driver),
36 save_card_callback_(save_card_callback), 79 save_card_callback_(save_card_callback),
37 had_user_interaction_(false) { 80 had_user_interaction_(false) {
81 AddPairToMap(autofill_driver_, this);
38 AutofillMetrics::LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_SHOWN); 82 AutofillMetrics::LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_SHOWN);
39 } 83 }
40 84
41 AutofillCCInfoBarDelegate::~AutofillCCInfoBarDelegate() { 85 AutofillCCInfoBarDelegate::~AutofillCCInfoBarDelegate() {
42 if (!had_user_interaction_) 86 if (!had_user_interaction_)
43 LogUserAction(AutofillMetrics::INFOBAR_IGNORED); 87 LogUserAction(AutofillMetrics::INFOBAR_IGNORED);
44 } 88 }
45 89
46 void AutofillCCInfoBarDelegate::LogUserAction( 90 void AutofillCCInfoBarDelegate::LogUserAction(
47 AutofillMetrics::InfoBarMetric user_action) { 91 AutofillMetrics::InfoBarMetric user_action) {
48 DCHECK(!had_user_interaction_); 92 DCHECK(!had_user_interaction_);
49 93
50 AutofillMetrics::LogCreditCardInfoBarMetric(user_action); 94 AutofillMetrics::LogCreditCardInfoBarMetric(user_action);
51 had_user_interaction_ = true; 95 had_user_interaction_ = true;
52 } 96 }
53 97
54 void AutofillCCInfoBarDelegate::InfoBarDismissed() { 98 void AutofillCCInfoBarDelegate::InfoBarDismissed() {
99 RemovePairFromMap(autofill_driver_, this);
55 LogUserAction(AutofillMetrics::INFOBAR_DENIED); 100 LogUserAction(AutofillMetrics::INFOBAR_DENIED);
56 } 101 }
57 102
58 int AutofillCCInfoBarDelegate::GetIconID() const { 103 int AutofillCCInfoBarDelegate::GetIconID() const {
59 return IDR_INFOBAR_AUTOFILL_CC; 104 return IDR_INFOBAR_AUTOFILL_CC;
60 } 105 }
61 106
62 infobars::InfoBarDelegate::Type AutofillCCInfoBarDelegate::GetInfoBarType() 107 infobars::InfoBarDelegate::Type AutofillCCInfoBarDelegate::GetInfoBarType()
63 const { 108 const {
64 return PAGE_ACTION_TYPE; 109 return PAGE_ACTION_TYPE;
(...skipping 11 matching lines...) Expand all
76 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_INFOBAR_TEXT); 121 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_INFOBAR_TEXT);
77 } 122 }
78 123
79 base::string16 AutofillCCInfoBarDelegate::GetButtonLabel( 124 base::string16 AutofillCCInfoBarDelegate::GetButtonLabel(
80 InfoBarButton button) const { 125 InfoBarButton button) const {
81 return l10n_util::GetStringUTF16((button == BUTTON_OK) ? 126 return l10n_util::GetStringUTF16((button == BUTTON_OK) ?
82 IDS_AUTOFILL_CC_INFOBAR_ACCEPT : IDS_AUTOFILL_CC_INFOBAR_DENY); 127 IDS_AUTOFILL_CC_INFOBAR_ACCEPT : IDS_AUTOFILL_CC_INFOBAR_DENY);
83 } 128 }
84 129
85 bool AutofillCCInfoBarDelegate::Accept() { 130 bool AutofillCCInfoBarDelegate::Accept() {
131 RemovePairFromMap(autofill_driver_, this);
86 save_card_callback_.Run(); 132 save_card_callback_.Run();
87 save_card_callback_.Reset(); 133 save_card_callback_.Reset();
88 LogUserAction(AutofillMetrics::INFOBAR_ACCEPTED); 134 LogUserAction(AutofillMetrics::INFOBAR_ACCEPTED);
89 return true; 135 return true;
90 } 136 }
91 137
92 bool AutofillCCInfoBarDelegate::Cancel() { 138 bool AutofillCCInfoBarDelegate::Cancel() {
139 RemovePairFromMap(autofill_driver_, this);
93 LogUserAction(AutofillMetrics::INFOBAR_DENIED); 140 LogUserAction(AutofillMetrics::INFOBAR_DENIED);
94 return true; 141 return true;
95 } 142 }
96 143
97 base::string16 AutofillCCInfoBarDelegate::GetLinkText() const { 144 base::string16 AutofillCCInfoBarDelegate::GetLinkText() const {
98 return l10n_util::GetStringUTF16(IDS_LEARN_MORE); 145 return l10n_util::GetStringUTF16(IDS_LEARN_MORE);
99 } 146 }
100 147
101 bool AutofillCCInfoBarDelegate::LinkClicked(WindowOpenDisposition disposition) { 148 bool AutofillCCInfoBarDelegate::LinkClicked(WindowOpenDisposition disposition) {
102 InfoBarService::WebContentsFromInfoBar(infobar())->OpenURL( 149 if (autofill_driver_) {
103 content::OpenURLParams( 150 autofill_driver_->LinkClicked(
104 GURL(autofill::kHelpURL), content::Referrer(), 151 GURL(autofill::kHelpURL),
105 (disposition == CURRENT_TAB) ? NEW_FOREGROUND_TAB : disposition, 152 (disposition == CURRENT_TAB) ? NEW_FOREGROUND_TAB : disposition);
106 ui::PAGE_TRANSITION_LINK, false)); 153 }
154
107 return false; 155 return false;
108 } 156 }
109 157
110 } // namespace autofill 158 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698