Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / password_manager / content / browser / content_password_manager_driver.cc
blob83239f480de44a2f013c5dd0a63bf809d0e65733
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.
5 #include "components/password_manager/content/browser/content_password_manager_driver.h"
7 #include "components/autofill/content/common/autofill_messages.h"
8 #include "components/autofill/core/common/form_data.h"
9 #include "components/autofill/core/common/password_form.h"
10 #include "components/password_manager/content/browser/bad_message.h"
11 #include "components/password_manager/content/browser/content_password_manager_driver_factory.h"
12 #include "components/password_manager/core/browser/password_manager_client.h"
13 #include "content/public/browser/browser_context.h"
14 #include "content/public/browser/child_process_security_policy.h"
15 #include "content/public/browser/navigation_details.h"
16 #include "content/public/browser/navigation_entry.h"
17 #include "content/public/browser/render_frame_host.h"
18 #include "content/public/browser/render_process_host.h"
19 #include "content/public/browser/render_view_host.h"
20 #include "content/public/browser/site_instance.h"
21 #include "content/public/browser/web_contents.h"
22 #include "content/public/common/ssl_status.h"
23 #include "ipc/ipc_message_macros.h"
24 #include "net/cert/cert_status_flags.h"
26 namespace password_manager {
28 ContentPasswordManagerDriver::ContentPasswordManagerDriver(
29 content::RenderFrameHost* render_frame_host,
30 PasswordManagerClient* client,
31 autofill::AutofillClient* autofill_client)
32 : render_frame_host_(render_frame_host),
33 client_(client),
34 password_generation_manager_(client, this),
35 password_autofill_manager_(this, autofill_client),
36 next_free_key_(0) {
39 ContentPasswordManagerDriver::~ContentPasswordManagerDriver() {
42 // static
43 ContentPasswordManagerDriver*
44 ContentPasswordManagerDriver::GetForRenderFrameHost(
45 content::RenderFrameHost* render_frame_host) {
46 ContentPasswordManagerDriverFactory* factory =
47 ContentPasswordManagerDriverFactory::FromWebContents(
48 content::WebContents::FromRenderFrameHost(render_frame_host));
49 return factory ? factory->GetDriverForFrame(render_frame_host) : nullptr;
52 void ContentPasswordManagerDriver::FillPasswordForm(
53 const autofill::PasswordFormFillData& form_data) {
54 const int key = next_free_key_++;
55 password_autofill_manager_.OnAddPasswordFormMapping(key, form_data);
56 render_frame_host_->Send(new AutofillMsg_FillPasswordForm(
57 render_frame_host_->GetRoutingID(), key, form_data));
60 void ContentPasswordManagerDriver::AllowPasswordGenerationForForm(
61 const autofill::PasswordForm& form) {
62 if (!GetPasswordGenerationManager()->IsGenerationEnabled())
63 return;
64 content::RenderFrameHost* host = render_frame_host_;
65 host->Send(new AutofillMsg_FormNotBlacklisted(host->GetRoutingID(), form));
68 void ContentPasswordManagerDriver::AccountCreationFormsFound(
69 const std::vector<autofill::FormData>& forms) {
70 content::RenderFrameHost* host = render_frame_host_;
71 host->Send(new AutofillMsg_AccountCreationFormsDetected(host->GetRoutingID(),
72 forms));
75 void ContentPasswordManagerDriver::AutofillDataReceived(
76 const std::map<autofill::FormData,
77 autofill::PasswordFormFieldPredictionMap>& predictions) {
78 content::RenderFrameHost* host = render_frame_host_;
79 host->Send(new AutofillMsg_AutofillUsernameAndPasswordDataReceived(
80 host->GetRoutingID(),
81 predictions));
84 void ContentPasswordManagerDriver::GeneratedPasswordAccepted(
85 const base::string16& password) {
86 content::RenderFrameHost* host = render_frame_host_;
87 host->Send(new AutofillMsg_GeneratedPasswordAccepted(host->GetRoutingID(),
88 password));
91 void ContentPasswordManagerDriver::FillSuggestion(
92 const base::string16& username,
93 const base::string16& password) {
94 content::RenderFrameHost* host = render_frame_host_;
95 host->Send(new AutofillMsg_FillPasswordSuggestion(host->GetRoutingID(),
96 username, password));
99 void ContentPasswordManagerDriver::PreviewSuggestion(
100 const base::string16& username,
101 const base::string16& password) {
102 content::RenderFrameHost* host = render_frame_host_;
103 host->Send(new AutofillMsg_PreviewPasswordSuggestion(host->GetRoutingID(),
104 username, password));
107 void ContentPasswordManagerDriver::ClearPreviewedForm() {
108 content::RenderFrameHost* host = render_frame_host_;
109 host->Send(new AutofillMsg_ClearPreviewedForm(host->GetRoutingID()));
112 void ContentPasswordManagerDriver::ForceSavePassword() {
113 content::RenderFrameHost* host = render_frame_host_;
114 host->Send(new AutofillMsg_FindFocusedPasswordForm(host->GetRoutingID()));
117 PasswordGenerationManager*
118 ContentPasswordManagerDriver::GetPasswordGenerationManager() {
119 return &password_generation_manager_;
122 PasswordManager* ContentPasswordManagerDriver::GetPasswordManager() {
123 return client_->GetPasswordManager();
126 PasswordAutofillManager*
127 ContentPasswordManagerDriver::GetPasswordAutofillManager() {
128 return &password_autofill_manager_;
131 bool ContentPasswordManagerDriver::HandleMessage(const IPC::Message& message) {
132 bool handled = true;
133 IPC_BEGIN_MESSAGE_MAP(ContentPasswordManagerDriver, message)
134 IPC_MESSAGE_HANDLER(AutofillHostMsg_PasswordFormsParsed,
135 OnPasswordFormsParsed)
136 IPC_MESSAGE_HANDLER(AutofillHostMsg_PasswordFormsRendered,
137 OnPasswordFormsRendered)
138 IPC_MESSAGE_HANDLER(AutofillHostMsg_PasswordFormSubmitted,
139 OnPasswordFormSubmitted)
140 IPC_MESSAGE_HANDLER(AutofillHostMsg_InPageNavigation, OnInPageNavigation)
141 IPC_MESSAGE_HANDLER(AutofillHostMsg_PasswordNoLongerGenerated,
142 OnPasswordNoLongerGenerated)
143 IPC_MESSAGE_HANDLER(AutofillHostMsg_FocusedPasswordFormFound,
144 OnFocusedPasswordFormFound)
145 IPC_MESSAGE_FORWARD(AutofillHostMsg_ShowPasswordSuggestions,
146 &password_autofill_manager_,
147 PasswordAutofillManager::OnShowPasswordSuggestions)
148 IPC_MESSAGE_FORWARD(AutofillHostMsg_RecordSavePasswordProgress, client_,
149 PasswordManagerClient::LogSavePasswordProgress)
150 IPC_MESSAGE_UNHANDLED(handled = false)
151 IPC_END_MESSAGE_MAP()
152 return handled;
155 void ContentPasswordManagerDriver::OnPasswordFormsParsed(
156 const std::vector<autofill::PasswordForm>& forms) {
157 for (const auto& form : forms)
158 if (!CheckChildProcessSecurityPolicy(
159 form.origin, BadMessageReason::CPMD_BAD_ORIGIN_FORMS_PARSED))
160 return;
162 OnPasswordFormsParsedNoRenderCheck(forms);
165 void ContentPasswordManagerDriver::OnPasswordFormsParsedNoRenderCheck(
166 const std::vector<autofill::PasswordForm>& forms) {
167 GetPasswordManager()->OnPasswordFormsParsed(this, forms);
170 void ContentPasswordManagerDriver::OnPasswordFormsRendered(
171 const std::vector<autofill::PasswordForm>& visible_forms,
172 bool did_stop_loading) {
173 for (const auto& form : visible_forms)
174 if (!CheckChildProcessSecurityPolicy(
175 form.origin, BadMessageReason::CPMD_BAD_ORIGIN_FORMS_RENDERED))
176 return;
177 GetPasswordManager()->OnPasswordFormsRendered(this, visible_forms,
178 did_stop_loading);
181 void ContentPasswordManagerDriver::OnPasswordFormSubmitted(
182 const autofill::PasswordForm& password_form) {
183 if (!CheckChildProcessSecurityPolicy(
184 password_form.origin,
185 BadMessageReason::CPMD_BAD_ORIGIN_FORM_SUBMITTED))
186 return;
187 GetPasswordManager()->OnPasswordFormSubmitted(this, password_form);
190 void ContentPasswordManagerDriver::OnFocusedPasswordFormFound(
191 const autofill::PasswordForm& password_form) {
192 if (!CheckChildProcessSecurityPolicy(
193 password_form.origin,
194 BadMessageReason::CPMD_BAD_ORIGIN_FOCUSED_PASSWORD_FORM_FOUND))
195 return;
196 GetPasswordManager()->OnPasswordFormForceSaveRequested(this, password_form);
199 void ContentPasswordManagerDriver::DidNavigateFrame(
200 const content::LoadCommittedDetails& details,
201 const content::FrameNavigateParams& params) {
202 // Clear page specific data after main frame navigation.
203 if (!render_frame_host_->GetParent() && !details.is_in_page) {
204 GetPasswordManager()->DidNavigateMainFrame();
205 GetPasswordAutofillManager()->DidNavigateMainFrame();
209 void ContentPasswordManagerDriver::OnInPageNavigation(
210 const autofill::PasswordForm& password_form) {
211 if (!CheckChildProcessSecurityPolicy(
212 password_form.origin,
213 BadMessageReason::CPMD_BAD_ORIGIN_IN_PAGE_NAVIGATION))
214 return;
215 GetPasswordManager()->OnInPageNavigation(this, password_form);
218 void ContentPasswordManagerDriver::OnPasswordNoLongerGenerated(
219 const autofill::PasswordForm& password_form) {
220 if (!CheckChildProcessSecurityPolicy(
221 password_form.origin,
222 BadMessageReason::CPMD_BAD_ORIGIN_PASSWORD_NO_LONGER_GENERATED))
223 return;
224 GetPasswordManager()->SetHasGeneratedPasswordForForm(this, password_form,
225 false);
228 bool ContentPasswordManagerDriver::CheckChildProcessSecurityPolicy(
229 const GURL& url,
230 BadMessageReason reason) {
231 content::ChildProcessSecurityPolicy* policy =
232 content::ChildProcessSecurityPolicy::GetInstance();
233 if (!policy->CanAccessDataForOrigin(render_frame_host_->GetProcess()->GetID(),
234 url)) {
235 bad_message::ReceivedBadMessage(render_frame_host_->GetProcess(), reason);
236 return false;
239 return true;
242 } // namespace password_manager