- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / autofill / tab_autofill_manager_delegate.cc
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/ui/autofill/tab_autofill_manager_delegate.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/password_generation_manager.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/common/url_constants.h"
22 #include "components/autofill/content/browser/autofill_driver_impl.h"
23 #include "components/autofill/core/common/autofill_messages.h"
24 #include "components/autofill/core/common/autofill_pref_names.h"
25 #include "content/public/browser/render_view_host.h"
26 #include "content/public/browser/web_contents_view.h"
27 #include "ui/gfx/rect.h"
28
29 DEFINE_WEB_CONTENTS_USER_DATA_KEY(autofill::TabAutofillManagerDelegate);
30
31 namespace autofill {
32
33 TabAutofillManagerDelegate::TabAutofillManagerDelegate(
34     content::WebContents* web_contents)
35     : content::WebContentsObserver(web_contents),
36       web_contents_(web_contents) {
37   DCHECK(web_contents);
38 }
39
40 TabAutofillManagerDelegate::~TabAutofillManagerDelegate() {
41   // NOTE: It is too late to clean up the autofill popup; that cleanup process
42   // requires that the WebContents instance still be valid and it is not at
43   // this point (in particular, the WebContentsImpl destructor has already
44   // finished running and we are now in the base class destructor).
45   DCHECK(!popup_controller_);
46 }
47
48 void TabAutofillManagerDelegate::TabActivated(int reason) {
49   if (reason != TabStripModelObserver::CHANGE_REASON_USER_GESTURE)
50     return;
51
52   if (dialog_controller_.get())
53     dialog_controller_->TabActivated();
54 }
55
56 PersonalDataManager* TabAutofillManagerDelegate::GetPersonalDataManager() {
57   Profile* profile =
58       Profile::FromBrowserContext(web_contents_->GetBrowserContext());
59   return PersonalDataManagerFactory::GetForProfile(
60       profile->GetOriginalProfile());
61 }
62
63 PrefService* TabAutofillManagerDelegate::GetPrefs() {
64   return Profile::FromBrowserContext(web_contents_->GetBrowserContext())->
65       GetPrefs();
66 }
67
68 void TabAutofillManagerDelegate::ShowAutofillSettings() {
69 #if defined(OS_ANDROID)
70   NOTIMPLEMENTED();
71 #else
72   Browser* browser = chrome::FindBrowserWithWebContents(web_contents_);
73   if (browser)
74     chrome::ShowSettingsSubPage(browser, chrome::kAutofillSubPage);
75 #endif  // #if defined(OS_ANDROID)
76 }
77
78 void TabAutofillManagerDelegate::ConfirmSaveCreditCard(
79     const AutofillMetrics& metric_logger,
80     const CreditCard& credit_card,
81     const base::Closure& save_card_callback) {
82   InfoBarService* infobar_service =
83       InfoBarService::FromWebContents(web_contents_);
84   AutofillCCInfoBarDelegate::Create(
85       infobar_service, &metric_logger, save_card_callback);
86 }
87
88 void TabAutofillManagerDelegate::ShowRequestAutocompleteDialog(
89     const FormData& form,
90     const GURL& source_url,
91     const base::Callback<void(const FormStructure*)>& callback) {
92   HideRequestAutocompleteDialog();
93
94   dialog_controller_ = AutofillDialogController::Create(web_contents_,
95                                                         form,
96                                                         source_url,
97                                                         callback);
98   if (dialog_controller_) {
99     dialog_controller_->Show();
100   } else {
101     callback.Run(NULL);
102     NOTIMPLEMENTED();
103   }
104 }
105
106 void TabAutofillManagerDelegate::ShowAutofillPopup(
107     const gfx::RectF& element_bounds,
108     base::i18n::TextDirection text_direction,
109     const std::vector<string16>& values,
110     const std::vector<string16>& labels,
111     const std::vector<string16>& icons,
112     const std::vector<int>& identifiers,
113     base::WeakPtr<AutofillPopupDelegate> delegate) {
114   // Convert element_bounds to be in screen space.
115   gfx::Rect client_area;
116   web_contents_->GetView()->GetContainerBounds(&client_area);
117   gfx::RectF element_bounds_in_screen_space =
118       element_bounds + client_area.OffsetFromOrigin();
119
120   // Will delete or reuse the old |popup_controller_|.
121   popup_controller_ = AutofillPopupControllerImpl::GetOrCreate(
122       popup_controller_,
123       delegate,
124       web_contents(),
125       web_contents()->GetView()->GetNativeView(),
126       element_bounds_in_screen_space,
127       text_direction);
128
129   popup_controller_->Show(values, labels, icons, identifiers);
130 }
131
132 void TabAutofillManagerDelegate::UpdateAutofillPopupDataListValues(
133     const std::vector<base::string16>& values,
134     const std::vector<base::string16>& labels) {
135   if (popup_controller_.get())
136     popup_controller_->UpdateDataListValues(values, labels);
137 }
138
139 void TabAutofillManagerDelegate::HideAutofillPopup() {
140   if (popup_controller_.get())
141     popup_controller_->Hide();
142 }
143
144 bool TabAutofillManagerDelegate::IsAutocompleteEnabled() {
145   // For browser, Autocomplete is always enabled as part of Autofill.
146   return GetPrefs()->GetBoolean(prefs::kAutofillEnabled);
147 }
148
149 void TabAutofillManagerDelegate::HideRequestAutocompleteDialog() {
150   if (dialog_controller_.get())
151     dialog_controller_->Hide();
152 }
153
154 void TabAutofillManagerDelegate::WasShown() {
155   content::RenderViewHost* host = web_contents()->GetRenderViewHost();
156   if (!host)
157     return;
158   host->Send(new AutofillMsg_PageShown(host->GetRoutingID()));
159 }
160
161 void TabAutofillManagerDelegate::DidNavigateMainFrame(
162     const content::LoadCommittedDetails& details,
163     const content::FrameNavigateParams& params) {
164   if (!dialog_controller_.get())
165     return;
166
167   HideRequestAutocompleteDialog();
168 }
169
170 void TabAutofillManagerDelegate::WebContentsDestroyed(
171     content::WebContents* web_contents) {
172   HideAutofillPopup();
173 }
174
175 void TabAutofillManagerDelegate::DetectAccountCreationForms(
176     const std::vector<autofill::FormStructure*>& forms) {
177   PasswordGenerationManager* manager =
178       PasswordGenerationManager::FromWebContents(web_contents_);
179   if (manager)
180     manager->DetectAccountCreationForms(forms);
181 }
182
183 }  // namespace autofill