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_content_utils.h"
8 #include "components/password_manager/content/common/credential_manager_messages.h"
9 #include "components/password_manager/core/common/credential_manager_types.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/platform/WebFederatedCredential.h"
14 #include "third_party/WebKit/public/platform/WebLocalCredential.h"
15 #include "third_party/WebKit/public/web/WebView.h"
17 namespace password_manager
{
22 void ClearCallbacksMapWithErrors(T
* callbacks_map
) {
23 typename
T::iterator
iter(callbacks_map
);
24 while (!iter
.IsAtEnd()) {
25 blink::WebCredentialManagerError
reason(
26 blink::WebCredentialManagerError::ErrorTypeUnknown
);
27 iter
.GetCurrentValue()->onError(&reason
);
28 callbacks_map
->Remove(iter
.GetCurrentKey());
35 CredentialManagerClient::CredentialManagerClient(
36 content::RenderView
* render_view
)
37 : content::RenderViewObserver(render_view
) {
38 render_view
->GetWebView()->setCredentialManagerClient(this);
41 CredentialManagerClient::~CredentialManagerClient() {
42 ClearCallbacksMapWithErrors(&failed_sign_in_callbacks_
);
43 ClearCallbacksMapWithErrors(&signed_in_callbacks_
);
44 ClearCallbacksMapWithErrors(&signed_out_callbacks_
);
45 ClearCallbacksMapWithErrors(&request_callbacks_
);
48 // -----------------------------------------------------------------------------
49 // Handle messages from the browser.
51 bool CredentialManagerClient::OnMessageReceived(const IPC::Message
& message
) {
53 IPC_BEGIN_MESSAGE_MAP(CredentialManagerClient
, message
)
54 IPC_MESSAGE_HANDLER(CredentialManagerMsg_AcknowledgeFailedSignIn
,
55 OnAcknowledgeFailedSignIn
)
56 IPC_MESSAGE_HANDLER(CredentialManagerMsg_AcknowledgeSignedIn
,
57 OnAcknowledgeSignedIn
)
58 IPC_MESSAGE_HANDLER(CredentialManagerMsg_AcknowledgeSignedOut
,
59 OnAcknowledgeSignedOut
)
60 IPC_MESSAGE_HANDLER(CredentialManagerMsg_SendCredential
, OnSendCredential
)
61 IPC_MESSAGE_HANDLER(CredentialManagerMsg_RejectCredentialRequest
,
62 OnRejectCredentialRequest
)
63 IPC_MESSAGE_UNHANDLED(handled
= false)
68 void CredentialManagerClient::OnAcknowledgeFailedSignIn(int request_id
) {
69 RespondToNotificationCallback(request_id
, &failed_sign_in_callbacks_
);
72 void CredentialManagerClient::OnAcknowledgeSignedIn(int request_id
) {
73 RespondToNotificationCallback(request_id
, &signed_in_callbacks_
);
76 void CredentialManagerClient::OnAcknowledgeSignedOut(int request_id
) {
77 RespondToNotificationCallback(request_id
, &signed_out_callbacks_
);
80 void CredentialManagerClient::OnSendCredential(int request_id
,
81 const CredentialInfo
& info
) {
82 RequestCallbacks
* callbacks
= request_callbacks_
.Lookup(request_id
);
84 scoped_ptr
<blink::WebCredential
> credential
= nullptr;
86 case CredentialType::CREDENTIAL_TYPE_FEDERATED
:
87 credential
.reset(new blink::WebFederatedCredential(
88 info
.id
, info
.name
, info
.avatar
, info
.federation
));
90 case CredentialType::CREDENTIAL_TYPE_LOCAL
:
91 credential
.reset(new blink::WebLocalCredential(
92 info
.id
, info
.name
, info
.avatar
, info
.password
));
94 case CredentialType::CREDENTIAL_TYPE_EMPTY
:
95 // Intentionally empty; we'll send nullptr to the onSuccess call below.
98 callbacks
->onSuccess(credential
.get());
99 request_callbacks_
.Remove(request_id
);
102 void CredentialManagerClient::OnRejectCredentialRequest(
104 blink::WebCredentialManagerError::ErrorType error_type
) {
105 RequestCallbacks
* callbacks
= request_callbacks_
.Lookup(request_id
);
107 scoped_ptr
<blink::WebCredentialManagerError
> error(
108 new blink::WebCredentialManagerError(error_type
));
109 callbacks
->onError(error
.get());
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
);
120 CredentialInfo
info(WebCredentialToCredentialInfo(credential
));
121 Send(new CredentialManagerHostMsg_NotifyFailedSignIn(
122 routing_id(), request_id
, info
));
125 void CredentialManagerClient::dispatchSignedIn(
126 const blink::WebCredential
& credential
,
127 blink::WebCredentialManagerClient::NotificationCallbacks
* callbacks
) {
128 int request_id
= signed_in_callbacks_
.Add(callbacks
);
129 CredentialInfo
info(WebCredentialToCredentialInfo(credential
));
130 Send(new CredentialManagerHostMsg_NotifySignedIn(
131 routing_id(), request_id
, info
));
134 void CredentialManagerClient::dispatchSignedOut(
135 NotificationCallbacks
* callbacks
) {
136 int request_id
= signed_out_callbacks_
.Add(callbacks
);
137 Send(new CredentialManagerHostMsg_NotifySignedOut(routing_id(), request_id
));
140 void CredentialManagerClient::dispatchRequest(
142 const blink::WebVector
<blink::WebURL
>& federations
,
143 RequestCallbacks
* callbacks
) {
144 int request_id
= request_callbacks_
.Add(callbacks
);
145 std::vector
<GURL
> federation_vector
;
146 for (size_t i
= 0; i
< std::min(federations
.size(), kMaxFederations
); ++i
)
147 federation_vector
.push_back(federations
[i
]);
148 Send(new CredentialManagerHostMsg_RequestCredential(
149 routing_id(), request_id
, zeroClickOnly
, federation_vector
));
152 void CredentialManagerClient::RespondToNotificationCallback(
154 CredentialManagerClient::NotificationCallbacksMap
* map
) {
155 blink::WebCredentialManagerClient::NotificationCallbacks
* callbacks
=
156 map
->Lookup(request_id
);
158 callbacks
->onSuccess();
159 map
->Remove(request_id
);
162 } // namespace password_manager