OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "components/autofill/content/browser/content_autofill_driver.h" | 5 #include "components/autofill/content/browser/content_autofill_driver.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/threading/sequenced_worker_pool.h" | 10 #include "base/threading/sequenced_worker_pool.h" |
11 #include "components/autofill/content/browser/content_autofill_driver_factory.h" | |
11 #include "components/autofill/content/common/autofill_messages.h" | 12 #include "components/autofill/content/common/autofill_messages.h" |
12 #include "components/autofill/core/browser/autofill_client.h" | 13 #include "components/autofill/core/browser/autofill_client.h" |
13 #include "components/autofill/core/browser/autofill_external_delegate.h" | 14 #include "components/autofill/core/browser/autofill_external_delegate.h" |
14 #include "components/autofill/core/browser/autofill_manager.h" | 15 #include "components/autofill/core/browser/autofill_manager.h" |
15 #include "components/autofill/core/browser/form_structure.h" | 16 #include "components/autofill/core/browser/form_structure.h" |
16 #include "components/autofill/core/common/autofill_switches.h" | 17 #include "components/autofill/core/common/autofill_switches.h" |
17 #include "content/public/browser/browser_context.h" | 18 #include "content/public/browser/browser_context.h" |
18 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
19 #include "content/public/browser/navigation_controller.h" | 20 #include "content/public/browser/navigation_controller.h" |
20 #include "content/public/browser/navigation_details.h" | 21 #include "content/public/browser/navigation_details.h" |
21 #include "content/public/browser/render_frame_host.h" | 22 #include "content/public/browser/render_frame_host.h" |
22 #include "content/public/browser/render_view_host.h" | 23 #include "content/public/browser/render_view_host.h" |
23 #include "content/public/browser/render_widget_host_view.h" | 24 #include "content/public/browser/render_widget_host_view.h" |
24 #include "content/public/browser/site_instance.h" | 25 #include "content/public/browser/site_instance.h" |
25 #include "content/public/browser/storage_partition.h" | 26 #include "content/public/browser/storage_partition.h" |
26 #include "ipc/ipc_message_macros.h" | 27 #include "content/public/browser/web_contents.h" |
28 #include "mojo/common/common_type_converters.h" | |
27 #include "services/shell/public/cpp/interface_provider.h" | 29 #include "services/shell/public/cpp/interface_provider.h" |
28 #include "ui/gfx/geometry/size_f.h" | 30 #include "ui/gfx/geometry/size_f.h" |
29 | 31 |
30 namespace autofill { | 32 namespace autofill { |
31 | 33 |
32 ContentAutofillDriver::ContentAutofillDriver( | 34 ContentAutofillDriver::ContentAutofillDriver( |
33 content::RenderFrameHost* render_frame_host, | 35 content::RenderFrameHost* render_frame_host, |
34 AutofillClient* client, | 36 AutofillClient* client, |
35 const std::string& app_locale, | 37 const std::string& app_locale, |
36 AutofillManager::AutofillDownloadManagerState enable_download_manager) | 38 AutofillManager::AutofillDownloadManagerState enable_download_manager) |
37 : render_frame_host_(render_frame_host), | 39 : render_frame_host_(render_frame_host), |
38 client_(client), | 40 client_(client), |
39 autofill_manager_(new AutofillManager(this, | 41 autofill_manager_(new AutofillManager(this, |
40 client, | 42 client, |
41 app_locale, | 43 app_locale, |
42 enable_download_manager)), | 44 enable_download_manager)), |
43 autofill_external_delegate_(autofill_manager_.get(), this) { | 45 autofill_external_delegate_(autofill_manager_.get(), this), |
46 binding_(this) { | |
44 autofill_manager_->SetExternalDelegate(&autofill_external_delegate_); | 47 autofill_manager_->SetExternalDelegate(&autofill_external_delegate_); |
45 } | 48 } |
46 | 49 |
47 ContentAutofillDriver::~ContentAutofillDriver() {} | 50 ContentAutofillDriver::~ContentAutofillDriver() {} |
48 | 51 |
52 // static | |
53 ContentAutofillDriver* ContentAutofillDriver::GetForRenderFrameHost( | |
54 content::RenderFrameHost* render_frame_host) { | |
55 ContentAutofillDriverFactory* factory = | |
56 ContentAutofillDriverFactory::FromWebContents( | |
57 content::WebContents::FromRenderFrameHost(render_frame_host)); | |
58 return factory ? factory->DriverForFrame(render_frame_host) : nullptr; | |
59 } | |
60 | |
49 void ContentAutofillDriver::BindRequest(mojom::AutofillDriverRequest request) { | 61 void ContentAutofillDriver::BindRequest(mojom::AutofillDriverRequest request) { |
50 bindings_.AddBinding(this, std::move(request)); | 62 binding_.Bind(std::move(request)); |
51 } | 63 } |
52 | 64 |
53 bool ContentAutofillDriver::IsOffTheRecord() const { | 65 bool ContentAutofillDriver::IsOffTheRecord() const { |
54 return render_frame_host_->GetSiteInstance() | 66 return render_frame_host_->GetSiteInstance() |
55 ->GetBrowserContext() | 67 ->GetBrowserContext() |
56 ->IsOffTheRecord(); | 68 ->IsOffTheRecord(); |
57 } | 69 } |
58 | 70 |
59 net::URLRequestContextGetter* ContentAutofillDriver::GetURLRequestContext() { | 71 net::URLRequestContextGetter* ContentAutofillDriver::GetURLRequestContext() { |
60 return content::BrowserContext::GetDefaultStoragePartition( | 72 return content::BrowserContext::GetDefaultStoragePartition( |
61 render_frame_host_->GetSiteInstance()->GetBrowserContext())-> | 73 render_frame_host_->GetSiteInstance()->GetBrowserContext())-> |
62 GetURLRequestContext(); | 74 GetURLRequestContext(); |
63 } | 75 } |
64 | 76 |
65 base::SequencedWorkerPool* ContentAutofillDriver::GetBlockingPool() { | 77 base::SequencedWorkerPool* ContentAutofillDriver::GetBlockingPool() { |
66 return content::BrowserThread::GetBlockingPool(); | 78 return content::BrowserThread::GetBlockingPool(); |
67 } | 79 } |
68 | 80 |
69 bool ContentAutofillDriver::RendererIsAvailable() { | 81 bool ContentAutofillDriver::RendererIsAvailable() { |
70 return render_frame_host_->GetRenderViewHost() != NULL; | 82 return render_frame_host_->GetRenderViewHost() != NULL; |
71 } | 83 } |
72 | 84 |
73 void ContentAutofillDriver::SendFormDataToRenderer( | 85 void ContentAutofillDriver::SendFormDataToRenderer( |
74 int query_id, | 86 int query_id, |
75 RendererFormDataAction action, | 87 RendererFormDataAction action, |
76 const FormData& data) { | 88 const FormData& data) { |
77 if (!RendererIsAvailable()) | 89 if (!RendererIsAvailable()) |
78 return; | 90 return; |
91 | |
79 switch (action) { | 92 switch (action) { |
80 case FORM_DATA_ACTION_FILL: | 93 case FORM_DATA_ACTION_FILL: |
81 render_frame_host_->Send(new AutofillMsg_FillForm( | 94 GetAutofillAgent()->FillForm(query_id, data); |
82 render_frame_host_->GetRoutingID(), query_id, data)); | |
83 break; | 95 break; |
84 case FORM_DATA_ACTION_PREVIEW: | 96 case FORM_DATA_ACTION_PREVIEW: |
85 render_frame_host_->Send(new AutofillMsg_PreviewForm( | 97 GetAutofillAgent()->PreviewForm(query_id, data); |
86 render_frame_host_->GetRoutingID(), query_id, data)); | |
87 break; | 98 break; |
88 } | 99 } |
89 } | 100 } |
90 | 101 |
91 void ContentAutofillDriver::PropagateAutofillPredictions( | 102 void ContentAutofillDriver::PropagateAutofillPredictions( |
92 const std::vector<FormStructure*>& forms) { | 103 const std::vector<FormStructure*>& forms) { |
93 autofill_manager_->client()->PropagateAutofillPredictions(render_frame_host_, | 104 autofill_manager_->client()->PropagateAutofillPredictions(render_frame_host_, |
94 forms); | 105 forms); |
95 } | 106 } |
96 | 107 |
97 void ContentAutofillDriver::SendAutofillTypePredictionsToRenderer( | 108 void ContentAutofillDriver::SendAutofillTypePredictionsToRenderer( |
98 const std::vector<FormStructure*>& forms) { | 109 const std::vector<FormStructure*>& forms) { |
99 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( | 110 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( |
100 switches::kShowAutofillTypePredictions)) | 111 switches::kShowAutofillTypePredictions)) |
101 return; | 112 return; |
102 | 113 |
103 if (!RendererIsAvailable()) | 114 if (!RendererIsAvailable()) |
104 return; | 115 return; |
105 | 116 |
106 std::vector<FormDataPredictions> type_predictions = | 117 std::vector<FormDataPredictions> type_predictions = |
107 FormStructure::GetFieldTypePredictions(forms); | 118 FormStructure::GetFieldTypePredictions(forms); |
108 render_frame_host_->Send(new AutofillMsg_FieldTypePredictionsAvailable( | 119 GetAutofillAgent()->FieldTypePredictionsAvailable( |
109 render_frame_host_->GetRoutingID(), type_predictions)); | 120 std::move(type_predictions)); |
110 } | 121 } |
111 | 122 |
112 void ContentAutofillDriver::RendererShouldAcceptDataListSuggestion( | 123 void ContentAutofillDriver::RendererShouldAcceptDataListSuggestion( |
113 const base::string16& value) { | 124 const base::string16& value) { |
114 if (!RendererIsAvailable()) | 125 if (!RendererIsAvailable()) |
115 return; | 126 return; |
116 render_frame_host_->Send(new AutofillMsg_AcceptDataListSuggestion( | 127 GetAutofillAgent()->AcceptDataListSuggestion(mojo::String::From(value)); |
117 render_frame_host_->GetRoutingID(), value)); | |
118 } | 128 } |
119 | 129 |
120 void ContentAutofillDriver::RendererShouldClearFilledForm() { | 130 void ContentAutofillDriver::RendererShouldClearFilledForm() { |
121 if (!RendererIsAvailable()) | 131 if (!RendererIsAvailable()) |
122 return; | 132 return; |
123 render_frame_host_->Send( | 133 GetAutofillAgent()->ClearForm(); |
124 new AutofillMsg_ClearForm(render_frame_host_->GetRoutingID())); | |
125 } | 134 } |
126 | 135 |
127 void ContentAutofillDriver::RendererShouldClearPreviewedForm() { | 136 void ContentAutofillDriver::RendererShouldClearPreviewedForm() { |
128 if (!RendererIsAvailable()) | 137 if (!RendererIsAvailable()) |
129 return; | 138 return; |
130 render_frame_host_->Send( | 139 GetAutofillAgent()->ClearPreviewedForm(); |
131 new AutofillMsg_ClearPreviewedForm(render_frame_host_->GetRoutingID())); | |
132 } | 140 } |
133 | 141 |
134 void ContentAutofillDriver::RendererShouldFillFieldWithValue( | 142 void ContentAutofillDriver::RendererShouldFillFieldWithValue( |
135 const base::string16& value) { | 143 const base::string16& value) { |
136 if (!RendererIsAvailable()) | 144 if (!RendererIsAvailable()) |
137 return; | 145 return; |
138 render_frame_host_->Send(new AutofillMsg_FillFieldWithValue( | 146 GetAutofillAgent()->FillFieldWithValue(mojo::String::From(value)); |
139 render_frame_host_->GetRoutingID(), value)); | |
140 } | 147 } |
141 | 148 |
142 void ContentAutofillDriver::RendererShouldPreviewFieldWithValue( | 149 void ContentAutofillDriver::RendererShouldPreviewFieldWithValue( |
143 const base::string16& value) { | 150 const base::string16& value) { |
144 if (!RendererIsAvailable()) | 151 if (!RendererIsAvailable()) |
145 return; | 152 return; |
146 render_frame_host_->Send(new AutofillMsg_PreviewFieldWithValue( | 153 GetAutofillAgent()->PreviewFieldWithValue(mojo::String::From(value)); |
147 render_frame_host_->GetRoutingID(), value)); | |
148 } | 154 } |
149 | 155 |
150 void ContentAutofillDriver::PopupHidden() { | 156 void ContentAutofillDriver::PopupHidden() { |
151 // If the unmask prompt is showing, keep showing the preview. The preview | 157 // If the unmask prompt is showing, keep showing the preview. The preview |
152 // will be cleared when the prompt closes. | 158 // will be cleared when the prompt closes. |
153 if (!autofill_manager_->IsShowingUnmaskPrompt()) | 159 if (!autofill_manager_->IsShowingUnmaskPrompt()) |
154 RendererShouldClearPreviewedForm(); | 160 RendererShouldClearPreviewedForm(); |
155 } | 161 } |
156 | 162 |
157 gfx::RectF ContentAutofillDriver::TransformBoundingBoxToViewportCoordinates( | 163 gfx::RectF ContentAutofillDriver::TransformBoundingBoxToViewportCoordinates( |
158 const gfx::RectF& bounding_box) { | 164 const gfx::RectF& bounding_box) { |
159 gfx::Point orig_point(bounding_box.x(), bounding_box.y()); | 165 gfx::Point orig_point(bounding_box.x(), bounding_box.y()); |
160 gfx::Point transformed_point; | 166 gfx::Point transformed_point; |
161 transformed_point = | 167 transformed_point = |
162 render_frame_host_->GetView()->TransformPointToRootCoordSpace(orig_point); | 168 render_frame_host_->GetView()->TransformPointToRootCoordSpace(orig_point); |
163 | 169 |
164 gfx::RectF new_box; | 170 gfx::RectF new_box; |
165 new_box.SetRect(transformed_point.x(), transformed_point.y(), | 171 new_box.SetRect(transformed_point.x(), transformed_point.y(), |
166 bounding_box.width(), bounding_box.height()); | 172 bounding_box.width(), bounding_box.height()); |
167 return new_box; | 173 return new_box; |
168 } | 174 } |
169 | 175 |
170 // mojom::AutofillDriver: | 176 // mojom::AutofillDriver: |
171 void ContentAutofillDriver::FirstUserGestureObserved() { | 177 void ContentAutofillDriver::FirstUserGestureObserved() { |
172 client_->OnFirstUserGestureObserved(); | 178 client_->OnFirstUserGestureObserved(); |
173 } | 179 } |
174 | 180 |
175 bool ContentAutofillDriver::HandleMessage(const IPC::Message& message) { | 181 void ContentAutofillDriver::FormsSeen(mojo::Array<FormData> forms, |
176 bool handled = true; | 182 const base::TimeTicks& timestamp) { |
177 IPC_BEGIN_MESSAGE_MAP(ContentAutofillDriver, message) | 183 autofill_manager_->OnFormsSeen(forms.storage(), timestamp); |
178 IPC_MESSAGE_FORWARD(AutofillHostMsg_FormsSeen, | 184 } |
179 autofill_manager_.get(), | 185 |
180 AutofillManager::OnFormsSeen) | 186 void ContentAutofillDriver::WillSubmitForm(const FormData& form, |
181 IPC_MESSAGE_FORWARD(AutofillHostMsg_WillSubmitForm, autofill_manager_.get(), | 187 const base::TimeTicks& timestamp) { |
182 AutofillManager::OnWillSubmitForm) | 188 autofill_manager_->OnWillSubmitForm(form, timestamp); |
183 IPC_MESSAGE_FORWARD(AutofillHostMsg_FormSubmitted, | 189 } |
184 autofill_manager_.get(), | 190 |
185 AutofillManager::OnFormSubmitted) | 191 void ContentAutofillDriver::FormSubmitted(const FormData& form) { |
186 IPC_MESSAGE_FORWARD(AutofillHostMsg_TextFieldDidChange, | 192 autofill_manager_->OnFormSubmitted(form); |
187 autofill_manager_.get(), | 193 } |
188 AutofillManager::OnTextFieldDidChange) | 194 |
189 IPC_MESSAGE_FORWARD(AutofillHostMsg_FocusNoLongerOnForm, | 195 void ContentAutofillDriver::TextFieldDidChange( |
190 autofill_manager_.get(), | 196 const FormData& form, |
191 AutofillManager::OnFocusNoLongerOnForm) | 197 const FormFieldData& field, |
192 IPC_MESSAGE_FORWARD(AutofillHostMsg_QueryFormFieldAutofill, | 198 const base::TimeTicks& timestamp) { |
193 autofill_manager_.get(), | 199 autofill_manager_->OnTextFieldDidChange(form, field, timestamp); |
194 AutofillManager::OnQueryFormFieldAutofill) | 200 } |
195 IPC_MESSAGE_FORWARD(AutofillHostMsg_DidPreviewAutofillFormData, | 201 |
196 autofill_manager_.get(), | 202 void ContentAutofillDriver::QueryFormFieldAutofill( |
197 AutofillManager::OnDidPreviewAutofillFormData) | 203 int32_t id, |
198 IPC_MESSAGE_FORWARD(AutofillHostMsg_PingAck, | 204 const FormData& form, |
199 &autofill_external_delegate_, | 205 const FormFieldData& field, |
200 AutofillExternalDelegate::OnPingAck) | 206 const gfx::RectF& bounding_box) { |
201 IPC_MESSAGE_FORWARD(AutofillHostMsg_DidFillAutofillFormData, | 207 autofill_manager_->OnQueryFormFieldAutofill(id, form, field, bounding_box); |
202 autofill_manager_.get(), | 208 } |
203 AutofillManager::OnDidFillAutofillFormData) | 209 |
204 IPC_MESSAGE_FORWARD(AutofillHostMsg_DidEndTextFieldEditing, | 210 void ContentAutofillDriver::HidePopup() { |
205 autofill_manager_.get(), | 211 autofill_manager_->OnHidePopup(); |
206 AutofillManager::OnDidEndTextFieldEditing) | 212 } |
207 IPC_MESSAGE_FORWARD(AutofillHostMsg_HidePopup, | 213 |
208 autofill_manager_.get(), | 214 void ContentAutofillDriver::PingAck() { |
209 AutofillManager::OnHidePopup) | 215 autofill_external_delegate_.OnPingAck(); |
210 IPC_MESSAGE_FORWARD(AutofillHostMsg_SetDataList, | 216 } |
211 autofill_manager_.get(), | 217 |
212 AutofillManager::OnSetDataList) | 218 void ContentAutofillDriver::FocusNoLongerOnForm() { |
213 IPC_MESSAGE_UNHANDLED(handled = false) | 219 autofill_manager_->OnFocusNoLongerOnForm(); |
214 IPC_END_MESSAGE_MAP() | 220 } |
215 return handled; | 221 |
222 void ContentAutofillDriver::DidFillAutofillFormData( | |
223 const FormData& form, | |
224 const base::TimeTicks& timestamp) { | |
225 autofill_manager_->OnDidFillAutofillFormData(form, timestamp); | |
226 } | |
227 | |
228 void ContentAutofillDriver::DidPreviewAutofillFormData() { | |
229 autofill_manager_->OnDidPreviewAutofillFormData(); | |
230 } | |
231 | |
232 void ContentAutofillDriver::DidEndTextFieldEditing() { | |
233 autofill_manager_->OnDidEndTextFieldEditing(); | |
234 } | |
235 | |
236 void ContentAutofillDriver::SetDataList(mojo::Array<mojo::String> values, | |
237 mojo::Array<mojo::String> labels) { | |
238 autofill_manager_->OnSetDataList(values.To<std::vector<base::string16>>(), | |
239 labels.To<std::vector<base::string16>>()); | |
216 } | 240 } |
217 | 241 |
218 void ContentAutofillDriver::DidNavigateFrame( | 242 void ContentAutofillDriver::DidNavigateFrame( |
219 const content::LoadCommittedDetails& details, | 243 const content::LoadCommittedDetails& details, |
220 const content::FrameNavigateParams& params) { | 244 const content::FrameNavigateParams& params) { |
221 if (details.is_navigation_to_different_page()) | 245 if (details.is_navigation_to_different_page()) |
222 autofill_manager_->Reset(); | 246 autofill_manager_->Reset(); |
223 } | 247 } |
224 | 248 |
225 void ContentAutofillDriver::SetAutofillManager( | 249 void ContentAutofillDriver::SetAutofillManager( |
226 std::unique_ptr<AutofillManager> manager) { | 250 std::unique_ptr<AutofillManager> manager) { |
227 autofill_manager_ = std::move(manager); | 251 autofill_manager_ = std::move(manager); |
228 autofill_manager_->SetExternalDelegate(&autofill_external_delegate_); | 252 autofill_manager_->SetExternalDelegate(&autofill_external_delegate_); |
229 } | 253 } |
230 | 254 |
231 void ContentAutofillDriver::NotifyFirstUserGestureObservedInTab() { | 255 void ContentAutofillDriver::NotifyFirstUserGestureObservedInTab() { |
232 ConnectToMojoAutofillAgentIfNeeded(); | 256 GetAutofillAgent()->FirstUserGestureObservedInTab(); |
233 mojo_autofill_agent_->FirstUserGestureObservedInTab(); | |
234 } | 257 } |
235 | 258 |
236 void ContentAutofillDriver::ConnectToMojoAutofillAgentIfNeeded() { | 259 const mojom::AutofillAgentPtr& ContentAutofillDriver::GetAutofillAgent() { |
237 if (mojo_autofill_agent_) | 260 if (!mojo_autofill_agent_) { |
238 return; | 261 render_frame_host_->GetRemoteInterfaces()->GetInterface( |
262 mojo::GetProxy(&mojo_autofill_agent_)); | |
dcheng
2016/07/12 14:22:12
I guess we do this lazy binding to avoid connectin
leonhsl(Using Gerrit)
2016/07/13 03:48:30
Done.
| |
263 } | |
239 | 264 |
240 render_frame_host_->GetRemoteInterfaces()->GetInterface( | 265 return mojo_autofill_agent_; |
241 &mojo_autofill_agent_); | |
242 } | 266 } |
243 | 267 |
244 } // namespace autofill | 268 } // namespace autofill |
OLD | NEW |