OLD | NEW |
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/ui/autofill/tab_autofill_manager_delegate.h" | 5 #include "chrome/browser/ui/autofill/chrome_autofill_client.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
9 #include "chrome/browser/autofill/autofill_cc_infobar_delegate.h" | 9 #include "chrome/browser/autofill/autofill_cc_infobar_delegate.h" |
10 #include "chrome/browser/autofill/personal_data_manager_factory.h" | 10 #include "chrome/browser/autofill/personal_data_manager_factory.h" |
11 #include "chrome/browser/infobars/infobar_service.h" | 11 #include "chrome/browser/infobars/infobar_service.h" |
12 #include "chrome/browser/password_manager/chrome_password_manager_client.h" | 12 #include "chrome/browser/password_manager/chrome_password_manager_client.h" |
13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/browser/ui/autofill/autofill_dialog_controller.h" | 14 #include "chrome/browser/ui/autofill/autofill_dialog_controller.h" |
15 #include "chrome/browser/ui/autofill/autofill_popup_controller_impl.h" | 15 #include "chrome/browser/ui/autofill/autofill_popup_controller_impl.h" |
16 #include "chrome/browser/ui/browser.h" | 16 #include "chrome/browser/ui/browser.h" |
17 #include "chrome/browser/ui/browser_finder.h" | 17 #include "chrome/browser/ui/browser_finder.h" |
18 #include "chrome/browser/ui/browser_window.h" | 18 #include "chrome/browser/ui/browser_window.h" |
19 #include "chrome/browser/ui/chrome_pages.h" | 19 #include "chrome/browser/ui/chrome_pages.h" |
20 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h" | 20 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h" |
21 #include "chrome/browser/webdata/web_data_service_factory.h" | 21 #include "chrome/browser/webdata/web_data_service_factory.h" |
22 #include "chrome/common/url_constants.h" | 22 #include "chrome/common/url_constants.h" |
23 #include "components/autofill/content/browser/content_autofill_driver.h" | 23 #include "components/autofill/content/browser/content_autofill_driver.h" |
24 #include "components/autofill/content/common/autofill_messages.h" | 24 #include "components/autofill/content/common/autofill_messages.h" |
25 #include "components/autofill/core/common/autofill_pref_names.h" | 25 #include "components/autofill/core/common/autofill_pref_names.h" |
26 #include "content/public/browser/render_view_host.h" | 26 #include "content/public/browser/render_view_host.h" |
27 #include "ui/gfx/rect.h" | 27 #include "ui/gfx/rect.h" |
28 | 28 |
29 #if defined(OS_ANDROID) | 29 #if defined(OS_ANDROID) |
30 #include "chrome/browser/ui/android/autofill/autofill_logger_android.h" | 30 #include "chrome/browser/ui/android/autofill/autofill_logger_android.h" |
31 #endif | 31 #endif |
32 | 32 |
33 DEFINE_WEB_CONTENTS_USER_DATA_KEY(autofill::TabAutofillManagerDelegate); | 33 DEFINE_WEB_CONTENTS_USER_DATA_KEY(autofill::ChromeAutofillClient); |
34 | 34 |
35 namespace autofill { | 35 namespace autofill { |
36 | 36 |
37 TabAutofillManagerDelegate::TabAutofillManagerDelegate( | 37 ChromeAutofillClient::ChromeAutofillClient(content::WebContents* web_contents) |
38 content::WebContents* web_contents) | 38 : content::WebContentsObserver(web_contents), web_contents_(web_contents) { |
39 : content::WebContentsObserver(web_contents), | |
40 web_contents_(web_contents) { | |
41 DCHECK(web_contents); | 39 DCHECK(web_contents); |
42 } | 40 } |
43 | 41 |
44 TabAutofillManagerDelegate::~TabAutofillManagerDelegate() { | 42 ChromeAutofillClient::~ChromeAutofillClient() { |
45 // NOTE: It is too late to clean up the autofill popup; that cleanup process | 43 // NOTE: It is too late to clean up the autofill popup; that cleanup process |
46 // requires that the WebContents instance still be valid and it is not at | 44 // requires that the WebContents instance still be valid and it is not at |
47 // this point (in particular, the WebContentsImpl destructor has already | 45 // this point (in particular, the WebContentsImpl destructor has already |
48 // finished running and we are now in the base class destructor). | 46 // finished running and we are now in the base class destructor). |
49 DCHECK(!popup_controller_); | 47 DCHECK(!popup_controller_); |
50 } | 48 } |
51 | 49 |
52 void TabAutofillManagerDelegate::TabActivated() { | 50 void ChromeAutofillClient::TabActivated() { |
53 if (dialog_controller_.get()) | 51 if (dialog_controller_.get()) |
54 dialog_controller_->TabActivated(); | 52 dialog_controller_->TabActivated(); |
55 } | 53 } |
56 | 54 |
57 PersonalDataManager* TabAutofillManagerDelegate::GetPersonalDataManager() { | 55 PersonalDataManager* ChromeAutofillClient::GetPersonalDataManager() { |
58 Profile* profile = | 56 Profile* profile = |
59 Profile::FromBrowserContext(web_contents_->GetBrowserContext()); | 57 Profile::FromBrowserContext(web_contents_->GetBrowserContext()); |
60 return PersonalDataManagerFactory::GetForProfile( | 58 return PersonalDataManagerFactory::GetForProfile( |
61 profile->GetOriginalProfile()); | 59 profile->GetOriginalProfile()); |
62 } | 60 } |
63 | 61 |
64 scoped_refptr<AutofillWebDataService> | 62 scoped_refptr<AutofillWebDataService> ChromeAutofillClient::GetDatabase() { |
65 TabAutofillManagerDelegate::GetDatabase() { | |
66 Profile* profile = | 63 Profile* profile = |
67 Profile::FromBrowserContext(web_contents_->GetBrowserContext()); | 64 Profile::FromBrowserContext(web_contents_->GetBrowserContext()); |
68 return WebDataServiceFactory::GetAutofillWebDataForProfile( | 65 return WebDataServiceFactory::GetAutofillWebDataForProfile( |
69 profile, Profile::EXPLICIT_ACCESS); | 66 profile, Profile::EXPLICIT_ACCESS); |
70 } | 67 } |
71 | 68 |
72 PrefService* TabAutofillManagerDelegate::GetPrefs() { | 69 PrefService* ChromeAutofillClient::GetPrefs() { |
73 return Profile::FromBrowserContext(web_contents_->GetBrowserContext())-> | 70 return Profile::FromBrowserContext(web_contents_->GetBrowserContext()) |
74 GetPrefs(); | 71 ->GetPrefs(); |
75 } | 72 } |
76 | 73 |
77 void TabAutofillManagerDelegate::ShowAutofillSettings() { | 74 void ChromeAutofillClient::ShowAutofillSettings() { |
78 #if defined(OS_ANDROID) | 75 #if defined(OS_ANDROID) |
79 NOTIMPLEMENTED(); | 76 NOTIMPLEMENTED(); |
80 #else | 77 #else |
81 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_); | 78 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_); |
82 if (browser) | 79 if (browser) |
83 chrome::ShowSettingsSubPage(browser, chrome::kAutofillSubPage); | 80 chrome::ShowSettingsSubPage(browser, chrome::kAutofillSubPage); |
84 #endif // #if defined(OS_ANDROID) | 81 #endif // #if defined(OS_ANDROID) |
85 } | 82 } |
86 | 83 |
87 void TabAutofillManagerDelegate::ConfirmSaveCreditCard( | 84 void ChromeAutofillClient::ConfirmSaveCreditCard( |
88 const AutofillMetrics& metric_logger, | 85 const AutofillMetrics& metric_logger, |
89 const base::Closure& save_card_callback) { | 86 const base::Closure& save_card_callback) { |
90 InfoBarService* infobar_service = | 87 InfoBarService* infobar_service = |
91 InfoBarService::FromWebContents(web_contents_); | 88 InfoBarService::FromWebContents(web_contents_); |
92 AutofillCCInfoBarDelegate::Create( | 89 AutofillCCInfoBarDelegate::Create( |
93 infobar_service, &metric_logger, save_card_callback); | 90 infobar_service, &metric_logger, save_card_callback); |
94 } | 91 } |
95 | 92 |
96 void TabAutofillManagerDelegate::ShowRequestAutocompleteDialog( | 93 void ChromeAutofillClient::ShowRequestAutocompleteDialog( |
97 const FormData& form, | 94 const FormData& form, |
98 const GURL& source_url, | 95 const GURL& source_url, |
99 const ResultCallback& callback) { | 96 const ResultCallback& callback) { |
100 HideRequestAutocompleteDialog(); | 97 HideRequestAutocompleteDialog(); |
101 | 98 |
102 dialog_controller_ = AutofillDialogController::Create(web_contents_, | 99 dialog_controller_ = AutofillDialogController::Create( |
103 form, | 100 web_contents_, form, source_url, callback); |
104 source_url, | |
105 callback); | |
106 if (dialog_controller_) { | 101 if (dialog_controller_) { |
107 dialog_controller_->Show(); | 102 dialog_controller_->Show(); |
108 } else { | 103 } else { |
109 callback.Run(AutofillManagerDelegate::AutocompleteResultErrorDisabled, | 104 callback.Run(AutofillClient::AutocompleteResultErrorDisabled, |
110 base::string16(), | 105 base::string16(), |
111 NULL); | 106 NULL); |
112 NOTIMPLEMENTED(); | 107 NOTIMPLEMENTED(); |
113 } | 108 } |
114 } | 109 } |
115 | 110 |
116 void TabAutofillManagerDelegate::ShowAutofillPopup( | 111 void ChromeAutofillClient::ShowAutofillPopup( |
117 const gfx::RectF& element_bounds, | 112 const gfx::RectF& element_bounds, |
118 base::i18n::TextDirection text_direction, | 113 base::i18n::TextDirection text_direction, |
119 const std::vector<base::string16>& values, | 114 const std::vector<base::string16>& values, |
120 const std::vector<base::string16>& labels, | 115 const std::vector<base::string16>& labels, |
121 const std::vector<base::string16>& icons, | 116 const std::vector<base::string16>& icons, |
122 const std::vector<int>& identifiers, | 117 const std::vector<int>& identifiers, |
123 base::WeakPtr<AutofillPopupDelegate> delegate) { | 118 base::WeakPtr<AutofillPopupDelegate> delegate) { |
124 // Convert element_bounds to be in screen space. | 119 // Convert element_bounds to be in screen space. |
125 gfx::Rect client_area = web_contents_->GetContainerBounds(); | 120 gfx::Rect client_area = web_contents_->GetContainerBounds(); |
126 gfx::RectF element_bounds_in_screen_space = | 121 gfx::RectF element_bounds_in_screen_space = |
127 element_bounds + client_area.OffsetFromOrigin(); | 122 element_bounds + client_area.OffsetFromOrigin(); |
128 | 123 |
129 // Will delete or reuse the old |popup_controller_|. | 124 // Will delete or reuse the old |popup_controller_|. |
130 popup_controller_ = AutofillPopupControllerImpl::GetOrCreate( | 125 popup_controller_ = |
131 popup_controller_, | 126 AutofillPopupControllerImpl::GetOrCreate(popup_controller_, |
132 delegate, | 127 delegate, |
133 web_contents(), | 128 web_contents(), |
134 web_contents()->GetNativeView(), | 129 web_contents()->GetNativeView(), |
135 element_bounds_in_screen_space, | 130 element_bounds_in_screen_space, |
136 text_direction); | 131 text_direction); |
137 | 132 |
138 popup_controller_->Show(values, labels, icons, identifiers); | 133 popup_controller_->Show(values, labels, icons, identifiers); |
139 } | 134 } |
140 | 135 |
141 void TabAutofillManagerDelegate::UpdateAutofillPopupDataListValues( | 136 void ChromeAutofillClient::UpdateAutofillPopupDataListValues( |
142 const std::vector<base::string16>& values, | 137 const std::vector<base::string16>& values, |
143 const std::vector<base::string16>& labels) { | 138 const std::vector<base::string16>& labels) { |
144 if (popup_controller_.get()) | 139 if (popup_controller_.get()) |
145 popup_controller_->UpdateDataListValues(values, labels); | 140 popup_controller_->UpdateDataListValues(values, labels); |
146 } | 141 } |
147 | 142 |
148 void TabAutofillManagerDelegate::HideAutofillPopup() { | 143 void ChromeAutofillClient::HideAutofillPopup() { |
149 if (popup_controller_.get()) | 144 if (popup_controller_.get()) |
150 popup_controller_->Hide(); | 145 popup_controller_->Hide(); |
151 | 146 |
152 // Password generation popups behave in the same fashion and should also | 147 // Password generation popups behave in the same fashion and should also |
153 // be hidden. | 148 // be hidden. |
154 ChromePasswordManagerClient* password_client = | 149 ChromePasswordManagerClient* password_client = |
155 ChromePasswordManagerClient::FromWebContents(web_contents_); | 150 ChromePasswordManagerClient::FromWebContents(web_contents_); |
156 if (password_client) | 151 if (password_client) |
157 password_client->HidePasswordGenerationPopup(); | 152 password_client->HidePasswordGenerationPopup(); |
158 } | 153 } |
159 | 154 |
160 bool TabAutofillManagerDelegate::IsAutocompleteEnabled() { | 155 bool ChromeAutofillClient::IsAutocompleteEnabled() { |
161 // For browser, Autocomplete is always enabled as part of Autofill. | 156 // For browser, Autocomplete is always enabled as part of Autofill. |
162 return GetPrefs()->GetBoolean(prefs::kAutofillEnabled); | 157 return GetPrefs()->GetBoolean(prefs::kAutofillEnabled); |
163 } | 158 } |
164 | 159 |
165 void TabAutofillManagerDelegate::HideRequestAutocompleteDialog() { | 160 void ChromeAutofillClient::HideRequestAutocompleteDialog() { |
166 if (dialog_controller_.get()) | 161 if (dialog_controller_.get()) |
167 dialog_controller_->Hide(); | 162 dialog_controller_->Hide(); |
168 } | 163 } |
169 | 164 |
170 void TabAutofillManagerDelegate::WebContentsDestroyed() { | 165 void ChromeAutofillClient::WebContentsDestroyed() { |
171 HideAutofillPopup(); | 166 HideAutofillPopup(); |
172 } | 167 } |
173 | 168 |
174 void TabAutofillManagerDelegate::DetectAccountCreationForms( | 169 void ChromeAutofillClient::DetectAccountCreationForms( |
175 const std::vector<autofill::FormStructure*>& forms) { | 170 const std::vector<autofill::FormStructure*>& forms) { |
176 password_manager::PasswordGenerationManager* manager = | 171 password_manager::PasswordGenerationManager* manager = |
177 ChromePasswordManagerClient::GetGenerationManagerFromWebContents( | 172 ChromePasswordManagerClient::GetGenerationManagerFromWebContents( |
178 web_contents_); | 173 web_contents_); |
179 if (manager) | 174 if (manager) |
180 manager->DetectAccountCreationForms(forms); | 175 manager->DetectAccountCreationForms(forms); |
181 } | 176 } |
182 | 177 |
183 void TabAutofillManagerDelegate::DidFillOrPreviewField( | 178 void ChromeAutofillClient::DidFillOrPreviewField( |
184 const base::string16& autofilled_value, | 179 const base::string16& autofilled_value, |
185 const base::string16& profile_full_name) { | 180 const base::string16& profile_full_name) { |
186 #if defined(OS_ANDROID) | 181 #if defined(OS_ANDROID) |
187 AutofillLoggerAndroid::DidFillOrPreviewField( | 182 AutofillLoggerAndroid::DidFillOrPreviewField(autofilled_value, |
188 autofilled_value, profile_full_name); | 183 profile_full_name); |
189 #endif // defined(OS_ANDROID) | 184 #endif // defined(OS_ANDROID) |
190 } | 185 } |
191 | 186 |
192 } // namespace autofill | 187 } // namespace autofill |
OLD | NEW |