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_thread.h"
10 #include "content/public/renderer/render_view.h"
11 #include "third_party/WebKit/public/platform/WebCredential.h"
12 #include "third_party/WebKit/public/platform/WebCredentialManagerError.h"
13 #include "third_party/WebKit/public/web/WebView.h"
15 namespace password_manager
{
20 void ClearCallbacksMapWithErrors(T
* callbacks_map
) {
21 typename
T::iterator
iter(callbacks_map
);
22 while (!iter
.IsAtEnd()) {
23 blink::WebCredentialManagerError
reason(
24 blink::WebCredentialManagerError::ErrorTypeUnknown
,
25 "An unknown error occurred.");
26 iter
.GetCurrentValue()->onError(&reason
);
27 callbacks_map
->Remove(iter
.GetCurrentKey());
34 CredentialManagerClient::CredentialManagerClient()
35 : routing_id_(MSG_ROUTING_NONE
),
36 render_thread_(content::RenderThread::Get()) {
37 DCHECK(render_thread_
);
40 CredentialManagerClient::~CredentialManagerClient() {
41 DisconnectFromRenderThread();
43 ClearCallbacksMapWithErrors(&failed_sign_in_callbacks_
);
44 ClearCallbacksMapWithErrors(&signed_in_callbacks_
);
45 ClearCallbacksMapWithErrors(&signed_out_callbacks_
);
46 ClearCallbacksMapWithErrors(&request_callbacks_
);
49 void CredentialManagerClient::OnRenderViewCreated(
50 content::RenderView
* render_view
) {
51 render_view
->GetWebView()->setCredentialManagerClient(this);
54 void CredentialManagerClient::OnRenderProcessShutdown() {
55 DisconnectFromRenderThread();
58 int CredentialManagerClient::GetRoutingID() {
59 if (render_thread_
&& routing_id_
== MSG_ROUTING_NONE
) {
60 routing_id_
= render_thread_
->GenerateRoutingID();
61 render_thread_
->AddRoute(routing_id_
, this);
66 void CredentialManagerClient::DisconnectFromRenderThread() {
67 if (render_thread_
&& routing_id_
!= MSG_ROUTING_NONE
)
68 render_thread_
->RemoveRoute(routing_id_
);
69 render_thread_
= NULL
;
70 routing_id_
= MSG_ROUTING_NONE
;
73 // -----------------------------------------------------------------------------
74 // Handle messages from the browser.
76 bool CredentialManagerClient::OnMessageReceived(const IPC::Message
& message
) {
78 IPC_BEGIN_MESSAGE_MAP(CredentialManagerClient
, message
)
79 IPC_MESSAGE_HANDLER(CredentialManagerMsg_AcknowledgeFailedSignIn
,
80 OnAcknowledgeFailedSignIn
)
81 IPC_MESSAGE_HANDLER(CredentialManagerMsg_AcknowledgeSignedIn
,
82 OnAcknowledgeSignedIn
)
83 IPC_MESSAGE_HANDLER(CredentialManagerMsg_AcknowledgeSignedOut
,
84 OnAcknowledgeSignedOut
)
85 IPC_MESSAGE_HANDLER(CredentialManagerMsg_SendCredential
, OnSendCredential
)
86 IPC_MESSAGE_UNHANDLED(handled
= false)
91 void CredentialManagerClient::OnAcknowledgeFailedSignIn(int request_id
) {
92 RespondToNotificationCallback(request_id
, &failed_sign_in_callbacks_
);
95 void CredentialManagerClient::OnAcknowledgeSignedIn(int request_id
) {
96 RespondToNotificationCallback(request_id
, &signed_in_callbacks_
);
99 void CredentialManagerClient::OnAcknowledgeSignedOut(int request_id
) {
100 RespondToNotificationCallback(request_id
, &signed_out_callbacks_
);
103 void CredentialManagerClient::OnSendCredential(int request_id
,
104 const CredentialInfo
& info
) {
105 RequestCallbacks
* callbacks
= request_callbacks_
.Lookup(request_id
);
107 // TODO(mkwst): Split into local/federated credentials.
108 blink::WebCredential
credential(info
.id
, info
.name
, info
.avatar
);
109 callbacks
->onSuccess(&credential
);
110 request_callbacks_
.Remove(request_id
);
113 // -----------------------------------------------------------------------------
114 // Dispatch messages from the renderer to the browser.
116 void CredentialManagerClient::dispatchFailedSignIn(
117 const blink::WebCredential
& credential
,
118 blink::WebCredentialManagerClient::NotificationCallbacks
* callbacks
) {
119 int request_id
= failed_sign_in_callbacks_
.Add(callbacks
);
121 credential
.id(), credential
.name(), credential
.avatarURL());
122 if (render_thread_
) {
123 render_thread_
->Send(new CredentialManagerHostMsg_NotifyFailedSignIn(
124 GetRoutingID(), request_id
, info
));
128 void CredentialManagerClient::dispatchSignedIn(
129 const blink::WebCredential
& credential
,
130 blink::WebCredentialManagerClient::NotificationCallbacks
* callbacks
) {
131 int request_id
= signed_in_callbacks_
.Add(callbacks
);
133 credential
.id(), credential
.name(), credential
.avatarURL());
134 if (render_thread_
) {
135 render_thread_
->Send(new CredentialManagerHostMsg_NotifySignedIn(
136 GetRoutingID(), request_id
, info
));
140 void CredentialManagerClient::dispatchSignedOut(
141 NotificationCallbacks
* callbacks
) {
142 int request_id
= signed_out_callbacks_
.Add(callbacks
);
143 if (render_thread_
) {
144 render_thread_
->Send(new CredentialManagerHostMsg_NotifySignedOut(
145 GetRoutingID(), request_id
));
149 void CredentialManagerClient::dispatchRequest(
151 const blink::WebVector
<blink::WebURL
>& federations
,
152 RequestCallbacks
* callbacks
) {
153 int request_id
= request_callbacks_
.Add(callbacks
);
154 std::vector
<GURL
> federation_vector
;
155 for (size_t i
= 0; i
< std::min(federations
.size(), kMaxFederations
); ++i
)
156 federation_vector
.push_back(federations
[i
]);
157 if (render_thread_
) {
158 render_thread_
->Send(new CredentialManagerHostMsg_RequestCredential(
159 GetRoutingID(), request_id
, zeroClickOnly
, federation_vector
));
163 void CredentialManagerClient::RespondToNotificationCallback(
165 CredentialManagerClient::NotificationCallbacksMap
* map
) {
166 blink::WebCredentialManagerClient::NotificationCallbacks
* callbacks
=
167 map
->Lookup(request_id
);
169 callbacks
->onSuccess();
170 map
->Remove(request_id
);
173 } // namespace password_manager