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/WebPasswordCredential.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(&store_callbacks_
);
44 ClearCallbacksMapWithErrors(&require_user_mediation_callbacks_
);
45 ClearCallbacksMapWithErrors(&get_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_AcknowledgeStore
,
56 IPC_MESSAGE_HANDLER(CredentialManagerMsg_AcknowledgeRequireUserMediation
,
57 OnAcknowledgeRequireUserMediation
)
58 IPC_MESSAGE_HANDLER(CredentialManagerMsg_SendCredential
, OnSendCredential
)
59 IPC_MESSAGE_HANDLER(CredentialManagerMsg_RejectCredentialRequest
,
60 OnRejectCredentialRequest
)
61 IPC_MESSAGE_UNHANDLED(handled
= false)
66 void CredentialManagerClient::OnAcknowledgeStore(int request_id
) {
67 RespondToNotificationCallback(request_id
, &store_callbacks_
);
70 void CredentialManagerClient::OnAcknowledgeRequireUserMediation(
72 RespondToNotificationCallback(request_id
, &require_user_mediation_callbacks_
);
75 void CredentialManagerClient::OnSendCredential(int request_id
,
76 const CredentialInfo
& info
) {
77 RequestCallbacks
* callbacks
= get_callbacks_
.Lookup(request_id
);
79 scoped_ptr
<blink::WebCredential
> credential
= nullptr;
81 case CredentialType::CREDENTIAL_TYPE_FEDERATED
:
82 credential
.reset(new blink::WebFederatedCredential(
83 info
.id
, info
.federation
, info
.name
, info
.icon
));
85 case CredentialType::CREDENTIAL_TYPE_PASSWORD
:
86 credential
.reset(new blink::WebPasswordCredential(
87 info
.id
, info
.password
, info
.name
, info
.icon
));
89 case CredentialType::CREDENTIAL_TYPE_EMPTY
:
90 // Intentionally empty; we'll send nullptr to the onSuccess call below.
93 callbacks
->onSuccess(credential
.get());
94 get_callbacks_
.Remove(request_id
);
97 void CredentialManagerClient::OnRejectCredentialRequest(
99 blink::WebCredentialManagerError::ErrorType error_type
) {
100 RequestCallbacks
* callbacks
= get_callbacks_
.Lookup(request_id
);
102 scoped_ptr
<blink::WebCredentialManagerError
> error(
103 new blink::WebCredentialManagerError(error_type
));
104 callbacks
->onError(error
.get());
105 get_callbacks_
.Remove(request_id
);
108 // -----------------------------------------------------------------------------
109 // Dispatch messages from the renderer to the browser.
111 void CredentialManagerClient::dispatchStore(
112 const blink::WebCredential
& credential
,
113 blink::WebCredentialManagerClient::NotificationCallbacks
* callbacks
) {
114 int request_id
= store_callbacks_
.Add(callbacks
);
115 CredentialInfo
info(WebCredentialToCredentialInfo(credential
));
116 Send(new CredentialManagerHostMsg_Store(routing_id(), request_id
, info
));
119 void CredentialManagerClient::dispatchRequireUserMediation(
120 NotificationCallbacks
* callbacks
) {
121 int request_id
= require_user_mediation_callbacks_
.Add(callbacks
);
122 Send(new CredentialManagerHostMsg_RequireUserMediation(routing_id(),
126 void CredentialManagerClient::dispatchGet(
128 const blink::WebVector
<blink::WebURL
>& federations
,
129 RequestCallbacks
* callbacks
) {
130 int request_id
= get_callbacks_
.Add(callbacks
);
131 std::vector
<GURL
> federation_vector
;
132 for (size_t i
= 0; i
< std::min(federations
.size(), kMaxFederations
); ++i
)
133 federation_vector
.push_back(federations
[i
]);
134 Send(new CredentialManagerHostMsg_RequestCredential(
135 routing_id(), request_id
, zeroClickOnly
, federation_vector
));
138 void CredentialManagerClient::RespondToNotificationCallback(
140 CredentialManagerClient::NotificationCallbacksMap
* map
) {
141 blink::WebCredentialManagerClient::NotificationCallbacks
* callbacks
=
142 map
->Lookup(request_id
);
144 callbacks
->onSuccess();
145 map
->Remove(request_id
);
148 } // namespace password_manager