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