File: content_credential_manager_dispatcher.cc

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (199 lines) | stat: -rw-r--r-- 7,890 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/password_manager/content/browser/content_credential_manager_dispatcher.h"

#include "base/bind.h"
#include "base/memory/scoped_vector.h"
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "components/autofill/core/common/password_form.h"
#include "components/password_manager/content/browser/content_password_manager_driver.h"
#include "components/password_manager/content/browser/content_password_manager_driver_factory.h"
#include "components/password_manager/content/browser/credential_manager_password_form_manager.h"
#include "components/password_manager/content/common/credential_manager_messages.h"
#include "components/password_manager/content/common/credential_manager_types.h"
#include "components/password_manager/core/browser/password_manager_client.h"
#include "components/password_manager/core/browser/password_store.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "ipc/ipc_message_macros.h"

namespace password_manager {

struct ContentCredentialManagerDispatcher::PendingRequestParameters {
  PendingRequestParameters(int request_id,
                           bool request_zero_click_only,
                           GURL request_origin,
                           const std::vector<GURL>& request_federations)
      : id(request_id),
        zero_click_only(request_zero_click_only),
        origin(request_origin),
        federations(request_federations) {}

  int id;
  bool zero_click_only;
  GURL origin;
  std::vector<GURL> federations;
};

ContentCredentialManagerDispatcher::ContentCredentialManagerDispatcher(
    content::WebContents* web_contents,
    PasswordManagerClient* client)
    : WebContentsObserver(web_contents), client_(client) {
  DCHECK(web_contents);
}

ContentCredentialManagerDispatcher::~ContentCredentialManagerDispatcher() {}

bool ContentCredentialManagerDispatcher::OnMessageReceived(
    const IPC::Message& message) {
  bool handled = true;
  IPC_BEGIN_MESSAGE_MAP(ContentCredentialManagerDispatcher, message)
    IPC_MESSAGE_HANDLER(CredentialManagerHostMsg_NotifyFailedSignIn,
                        OnNotifyFailedSignIn);
    IPC_MESSAGE_HANDLER(CredentialManagerHostMsg_NotifySignedIn,
                        OnNotifySignedIn);
    IPC_MESSAGE_HANDLER(CredentialManagerHostMsg_NotifySignedOut,
                        OnNotifySignedOut);
    IPC_MESSAGE_HANDLER(CredentialManagerHostMsg_RequestCredential,
                        OnRequestCredential);
    IPC_MESSAGE_UNHANDLED(handled = false)
  IPC_END_MESSAGE_MAP()
  return handled;
}

void ContentCredentialManagerDispatcher::OnNotifyFailedSignIn(
    int request_id, const CredentialInfo&) {
  DCHECK(request_id);
  // TODO(mkwst): This is a stub.
  web_contents()->GetRenderViewHost()->Send(
      new CredentialManagerMsg_AcknowledgeFailedSignIn(
          web_contents()->GetRenderViewHost()->GetRoutingID(), request_id));
}

void ContentCredentialManagerDispatcher::OnNotifySignedIn(
    int request_id,
    const password_manager::CredentialInfo& credential) {
  DCHECK(request_id);
  scoped_ptr<autofill::PasswordForm> form(
      CreatePasswordFormFromCredentialInfo(credential,
          web_contents()->GetLastCommittedURL().GetOrigin()));

  // TODO(mkwst): This is a stub; we should be checking the PasswordStore to
  // determine whether or not the credential exists, and calling UpdateLogin
  // accordingly.
  form_manager_.reset(
      new CredentialManagerPasswordFormManager(client_, GetDriver(), *form,
                                               this));

  web_contents()->GetRenderViewHost()->Send(
      new CredentialManagerMsg_AcknowledgeSignedIn(
          web_contents()->GetRenderViewHost()->GetRoutingID(), request_id));
}

void ContentCredentialManagerDispatcher::OnProvisionalSaveComplete() {
  DCHECK(form_manager_);
  client_->PromptUserToSavePassword(form_manager_.Pass());
}

void ContentCredentialManagerDispatcher::OnNotifySignedOut(int request_id) {
  DCHECK(request_id);
  // TODO(mkwst): This is a stub.
  web_contents()->GetRenderViewHost()->Send(
      new CredentialManagerMsg_AcknowledgeSignedOut(
          web_contents()->GetRenderViewHost()->GetRoutingID(), request_id));
}

void ContentCredentialManagerDispatcher::OnRequestCredential(
    int request_id,
    bool zero_click_only,
    const std::vector<GURL>& federations) {
  DCHECK(request_id);
  PasswordStore* store = GetPasswordStore();
  if (pending_request_ || !store) {
    web_contents()->GetRenderViewHost()->Send(
        new CredentialManagerMsg_RejectCredentialRequest(
            web_contents()->GetRenderViewHost()->GetRoutingID(), request_id,
            pending_request_
                ? blink::WebCredentialManagerError::ErrorTypePendingRequest
                : blink::WebCredentialManagerError::
                      ErrorTypePasswordStoreUnavailable));
    return;
  }

  pending_request_.reset(new PendingRequestParameters(
      request_id, zero_click_only,
      web_contents()->GetLastCommittedURL().GetOrigin(), federations));

  store->GetAutofillableLogins(this);
}

void ContentCredentialManagerDispatcher::OnGetPasswordStoreResults(
    const std::vector<autofill::PasswordForm*>& results) {
  DCHECK(pending_request_);

  std::set<std::string> federations;
  for (const GURL& origin : pending_request_->federations)
    federations.insert(origin.spec());

  // We own the PasswordForm instances, so we're responsible for cleaning
  // up the instances we don't add to |local_results| or |federated_results|.
  // We'll dump them into a ScopedVector and allow it to delete the
  // PasswordForms upon destruction.
  std::vector<autofill::PasswordForm*> local_results;
  std::vector<autofill::PasswordForm*> federated_results;
  ScopedVector<autofill::PasswordForm> discarded_results;
  for (autofill::PasswordForm* form : results) {
    // TODO(mkwst): Extend this filter to include federations.
    if (form->origin == pending_request_->origin)
      local_results.push_back(form);
    else if (federations.count(form->origin.spec()) != 0)
      federated_results.push_back(form);
    else
      discarded_results.push_back(form);
  }

  if ((local_results.empty() && federated_results.empty()) ||
      web_contents()->GetLastCommittedURL().GetOrigin() !=
          pending_request_->origin) {
    SendCredential(pending_request_->id, CredentialInfo());
    return;
  }

  if (!client_->PromptUserToChooseCredentials(
          local_results,
          federated_results,
          base::Bind(&ContentCredentialManagerDispatcher::SendCredential,
                     base::Unretained(this), pending_request_->id))) {
    SendCredential(pending_request_->id, CredentialInfo());
  }
}

PasswordStore* ContentCredentialManagerDispatcher::GetPasswordStore() {
  return client_ ? client_->GetPasswordStore() : nullptr;
}

base::WeakPtr<PasswordManagerDriver>
ContentCredentialManagerDispatcher::GetDriver() {
  ContentPasswordManagerDriverFactory* driver_factory =
        ContentPasswordManagerDriverFactory::FromWebContents(web_contents());
  DCHECK(driver_factory);
  PasswordManagerDriver* driver =
      driver_factory->GetDriverForFrame(web_contents()->GetMainFrame());
  return driver->AsWeakPtr();
}

void ContentCredentialManagerDispatcher::SendCredential(
    int request_id, const CredentialInfo& info) {
  DCHECK(pending_request_);
  DCHECK_EQ(pending_request_->id, request_id);
  web_contents()->GetRenderViewHost()->Send(
      new CredentialManagerMsg_SendCredential(
          web_contents()->GetRenderViewHost()->GetRoutingID(),
          pending_request_->id, info));
  pending_request_.reset();
}

}  // namespace password_manager