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/renderer/credential_manager_client.h"
7 #include "components/password_manager/content/common/credential_manager_messages.h"
8 #include "components/password_manager/content/common/credential_manager_types.h"
9 #include "content/public/renderer/render_view.h"
10 #include "third_party/WebKit/public/platform/WebCredential.h"
11 #include "third_party/WebKit/public/platform/WebCredentialManagerError.h"
12 #include "third_party/WebKit/public/platform/WebFederatedCredential.h"
13 #include "third_party/WebKit/public/platform/WebLocalCredential.h"
14 #include "third_party/WebKit/public/web/WebView.h"
16 namespace password_manager {
21 void ClearCallbacksMapWithErrors(T* callbacks_map) {
22 typename T::iterator iter(callbacks_map);
23 while (!iter.IsAtEnd()) {
24 blink::WebCredentialManagerError reason(
25 blink::WebCredentialManagerError::ErrorTypeUnknown);
26 iter.GetCurrentValue()->onError(&reason);
27 callbacks_map->Remove(iter.GetCurrentKey());
34 CredentialManagerClient::CredentialManagerClient(
35 content::RenderView* render_view)
36 : content::RenderViewObserver(render_view) {
37 render_view->GetWebView()->setCredentialManagerClient(this);
40 CredentialManagerClient::~CredentialManagerClient() {
41 ClearCallbacksMapWithErrors(&failed_sign_in_callbacks_);
42 ClearCallbacksMapWithErrors(&signed_in_callbacks_);
43 ClearCallbacksMapWithErrors(&signed_out_callbacks_);
44 ClearCallbacksMapWithErrors(&request_callbacks_);
47 // -----------------------------------------------------------------------------
48 // Handle messages from the browser.
50 bool CredentialManagerClient::OnMessageReceived(const IPC::Message& message) {
52 IPC_BEGIN_MESSAGE_MAP(CredentialManagerClient, message)
53 IPC_MESSAGE_HANDLER(CredentialManagerMsg_AcknowledgeFailedSignIn,
54 OnAcknowledgeFailedSignIn)
55 IPC_MESSAGE_HANDLER(CredentialManagerMsg_AcknowledgeSignedIn,
56 OnAcknowledgeSignedIn)
57 IPC_MESSAGE_HANDLER(CredentialManagerMsg_AcknowledgeSignedOut,
58 OnAcknowledgeSignedOut)
59 IPC_MESSAGE_HANDLER(CredentialManagerMsg_SendCredential, OnSendCredential)
60 IPC_MESSAGE_HANDLER(CredentialManagerMsg_RejectCredentialRequest,
61 OnRejectCredentialRequest)
62 IPC_MESSAGE_UNHANDLED(handled = false)
67 void CredentialManagerClient::OnAcknowledgeFailedSignIn(int request_id) {
68 RespondToNotificationCallback(request_id, &failed_sign_in_callbacks_);
71 void CredentialManagerClient::OnAcknowledgeSignedIn(int request_id) {
72 RespondToNotificationCallback(request_id, &signed_in_callbacks_);
75 void CredentialManagerClient::OnAcknowledgeSignedOut(int request_id) {
76 RespondToNotificationCallback(request_id, &signed_out_callbacks_);
79 void CredentialManagerClient::OnSendCredential(int request_id,
80 const CredentialInfo& info) {
81 RequestCallbacks* callbacks = request_callbacks_.Lookup(request_id);
83 scoped_ptr<blink::WebCredential> credential = nullptr;
85 case CREDENTIAL_TYPE_FEDERATED:
86 credential.reset(new blink::WebFederatedCredential(
87 info.id, info.name, info.avatar, info.federation));
89 case CREDENTIAL_TYPE_LOCAL:
90 credential.reset(new blink::WebLocalCredential(info.id, info.name,
91 info.avatar, info.password));
93 case CREDENTIAL_TYPE_EMPTY:
94 // Intentionally empty; we'll send nullptr to the onSuccess call below.
97 callbacks->onSuccess(credential.get());
98 request_callbacks_.Remove(request_id);
101 void CredentialManagerClient::OnRejectCredentialRequest(
103 blink::WebCredentialManagerError::ErrorType error_type) {
104 RequestCallbacks* callbacks = request_callbacks_.Lookup(request_id);
106 scoped_ptr<blink::WebCredentialManagerError> error(
107 new blink::WebCredentialManagerError(error_type));
108 callbacks->onError(error.get());
109 request_callbacks_.Remove(request_id);
112 // -----------------------------------------------------------------------------
113 // Dispatch messages from the renderer to the browser.
115 void CredentialManagerClient::dispatchFailedSignIn(
116 const blink::WebCredential& credential,
117 blink::WebCredentialManagerClient::NotificationCallbacks* callbacks) {
118 int request_id = failed_sign_in_callbacks_.Add(callbacks);
119 CredentialInfo info(credential);
120 Send(new CredentialManagerHostMsg_NotifyFailedSignIn(
121 routing_id(), request_id, info));
124 void CredentialManagerClient::dispatchSignedIn(
125 const blink::WebCredential& credential,
126 blink::WebCredentialManagerClient::NotificationCallbacks* callbacks) {
127 int request_id = signed_in_callbacks_.Add(callbacks);
128 CredentialInfo info(credential);
129 Send(new CredentialManagerHostMsg_NotifySignedIn(
130 routing_id(), request_id, info));
133 void CredentialManagerClient::dispatchSignedOut(
134 NotificationCallbacks* callbacks) {
135 int request_id = signed_out_callbacks_.Add(callbacks);
136 Send(new CredentialManagerHostMsg_NotifySignedOut(routing_id(), request_id));
139 void CredentialManagerClient::dispatchRequest(
141 const blink::WebVector<blink::WebURL>& federations,
142 RequestCallbacks* callbacks) {
143 int request_id = request_callbacks_.Add(callbacks);
144 std::vector<GURL> federation_vector;
145 for (size_t i = 0; i < std::min(federations.size(), kMaxFederations); ++i)
146 federation_vector.push_back(federations[i]);
147 Send(new CredentialManagerHostMsg_RequestCredential(
148 routing_id(), request_id, zeroClickOnly, federation_vector));
151 void CredentialManagerClient::RespondToNotificationCallback(
153 CredentialManagerClient::NotificationCallbacksMap* map) {
154 blink::WebCredentialManagerClient::NotificationCallbacks* callbacks =
155 map->Lookup(request_id);
157 callbacks->onSuccess();
158 map->Remove(request_id);
161 } // namespace password_manager