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 "components/password_manager/content/browser/content_password_manager_driver.h"
6
7#include "components/autofill/content/browser/content_autofill_driver.h"
8#include "components/autofill/content/common/autofill_messages.h"
9#include "components/autofill/core/common/form_data.h"
10#include "components/autofill/core/common/password_form.h"
11#include "components/password_manager/core/browser/password_manager_client.h"
12#include "content/public/browser/browser_context.h"
13#include "content/public/browser/navigation_details.h"
14#include "content/public/browser/navigation_entry.h"
15#include "content/public/browser/render_view_host.h"
16#include "content/public/browser/web_contents.h"
17#include "content/public/common/ssl_status.h"
18#include "ipc/ipc_message_macros.h"
19#include "net/cert/cert_status_flags.h"
20
21namespace password_manager {
22
23ContentPasswordManagerDriver::ContentPasswordManagerDriver(
24    content::WebContents* web_contents,
25    PasswordManagerClient* client,
26    autofill::AutofillClient* autofill_client)
27    : WebContentsObserver(web_contents),
28      password_manager_(client),
29      password_generation_manager_(client),
30      password_autofill_manager_(client, autofill_client) {
31  DCHECK(web_contents);
32}
33
34ContentPasswordManagerDriver::~ContentPasswordManagerDriver() {}
35
36void ContentPasswordManagerDriver::FillPasswordForm(
37    const autofill::PasswordFormFillData& form_data) {
38  DCHECK(web_contents());
39  web_contents()->GetRenderViewHost()->Send(new AutofillMsg_FillPasswordForm(
40      web_contents()->GetRenderViewHost()->GetRoutingID(), form_data));
41}
42
43void ContentPasswordManagerDriver::AllowPasswordGenerationForForm(
44    const autofill::PasswordForm& form) {
45  content::RenderViewHost* host = web_contents()->GetRenderViewHost();
46  host->Send(new AutofillMsg_FormNotBlacklisted(host->GetRoutingID(), form));
47}
48
49void ContentPasswordManagerDriver::AccountCreationFormsFound(
50    const std::vector<autofill::FormData>& forms) {
51  content::RenderViewHost* host = web_contents()->GetRenderViewHost();
52  host->Send(new AutofillMsg_AccountCreationFormsDetected(host->GetRoutingID(),
53                                                          forms));
54}
55
56void ContentPasswordManagerDriver::FillSuggestion(
57    const base::string16& username,
58    const base::string16& password) {
59  content::RenderViewHost* host = web_contents()->GetRenderViewHost();
60  host->Send(
61      new AutofillMsg_FillPasswordSuggestion(host->GetRoutingID(),
62                                             username,
63                                             password));
64}
65
66void ContentPasswordManagerDriver::PreviewSuggestion(
67    const base::string16& username,
68    const base::string16& password) {
69  content::RenderViewHost* host = web_contents()->GetRenderViewHost();
70  host->Send(
71      new AutofillMsg_PreviewPasswordSuggestion(host->GetRoutingID(),
72                                                username,
73                                                password));
74}
75
76void ContentPasswordManagerDriver::ClearPreviewedForm() {
77  content::RenderViewHost* host = web_contents()->GetRenderViewHost();
78  host->Send(
79      new AutofillMsg_ClearPreviewedForm(host->GetRoutingID()));
80}
81
82bool ContentPasswordManagerDriver::DidLastPageLoadEncounterSSLErrors() {
83  DCHECK(web_contents());
84  // TODO(vabr): This is a wrong entry to look at for HTTP basic auth,
85  // http://crbug.com/388246.
86  content::NavigationEntry* entry =
87      web_contents()->GetController().GetLastCommittedEntry();
88  if (!entry) {
89    return false;
90  }
91
92  return net::IsCertStatusError(entry->GetSSL().cert_status);
93}
94
95bool ContentPasswordManagerDriver::IsOffTheRecord() {
96  DCHECK(web_contents());
97  return web_contents()->GetBrowserContext()->IsOffTheRecord();
98}
99
100PasswordGenerationManager*
101ContentPasswordManagerDriver::GetPasswordGenerationManager() {
102  return &password_generation_manager_;
103}
104
105PasswordManager* ContentPasswordManagerDriver::GetPasswordManager() {
106  return &password_manager_;
107}
108
109PasswordAutofillManager*
110ContentPasswordManagerDriver::GetPasswordAutofillManager() {
111  return &password_autofill_manager_;
112}
113
114void ContentPasswordManagerDriver::DidNavigateMainFrame(
115    const content::LoadCommittedDetails& details,
116    const content::FrameNavigateParams& params) {
117  password_manager_.DidNavigateMainFrame(details.is_in_page);
118}
119
120bool ContentPasswordManagerDriver::OnMessageReceived(
121    const IPC::Message& message) {
122  bool handled = true;
123  IPC_BEGIN_MESSAGE_MAP(PasswordManager, message)
124  IPC_MESSAGE_FORWARD(AutofillHostMsg_PasswordFormsParsed,
125                      &password_manager_,
126                      PasswordManager::OnPasswordFormsParsed)
127  IPC_MESSAGE_FORWARD(AutofillHostMsg_PasswordFormsRendered,
128                      &password_manager_,
129                      PasswordManager::OnPasswordFormsRendered)
130  IPC_MESSAGE_FORWARD(AutofillHostMsg_PasswordFormSubmitted,
131                      &password_manager_,
132                      PasswordManager::OnPasswordFormSubmitted)
133  IPC_MESSAGE_FORWARD(AutofillHostMsg_ShowPasswordSuggestions,
134                      &password_autofill_manager_,
135                      PasswordAutofillManager::OnShowPasswordSuggestions)
136  IPC_MESSAGE_FORWARD(AutofillHostMsg_AddPasswordFormMapping,
137                      &password_autofill_manager_,
138                      PasswordAutofillManager::OnAddPasswordFormMapping)
139  IPC_MESSAGE_FORWARD(AutofillHostMsg_RecordSavePasswordProgress,
140                      password_manager_.client(),
141                      PasswordManagerClient::LogSavePasswordProgress)
142  IPC_MESSAGE_UNHANDLED(handled = false)
143  IPC_END_MESSAGE_MAP()
144
145  return handled;
146}
147
148autofill::AutofillManager* ContentPasswordManagerDriver::GetAutofillManager() {
149  autofill::ContentAutofillDriver* driver =
150      autofill::ContentAutofillDriver::FromWebContents(web_contents());
151  return driver ? driver->autofill_manager() : NULL;
152}
153
154}  // namespace password_manager
155