OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/password_manager/content/renderer/credential_manager_client
.h" | 5 #include "components/password_manager/content/renderer/credential_manager_client
.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "components/password_manager/content/common/credential_manager_content_
utils.h" |
13 #include "base/logging.h" | 13 #include "components/password_manager/content/common/credential_manager_messages
.h" |
14 #include "components/password_manager/content/public/cpp/type_converters.h" | |
15 #include "components/password_manager/core/common/credential_manager_types.h" | 14 #include "components/password_manager/core/common/credential_manager_types.h" |
16 #include "content/public/common/service_registry.h" | |
17 #include "content/public/renderer/render_frame.h" | |
18 #include "content/public/renderer/render_view.h" | 15 #include "content/public/renderer/render_view.h" |
19 #include "mojo/common/url_type_converters.h" | |
20 #include "third_party/WebKit/public/platform/WebCredential.h" | 16 #include "third_party/WebKit/public/platform/WebCredential.h" |
21 #include "third_party/WebKit/public/platform/WebCredentialManagerError.h" | 17 #include "third_party/WebKit/public/platform/WebCredentialManagerError.h" |
22 #include "third_party/WebKit/public/platform/WebFederatedCredential.h" | 18 #include "third_party/WebKit/public/platform/WebFederatedCredential.h" |
23 #include "third_party/WebKit/public/platform/WebPasswordCredential.h" | 19 #include "third_party/WebKit/public/platform/WebPasswordCredential.h" |
24 #include "third_party/WebKit/public/web/WebView.h" | 20 #include "third_party/WebKit/public/web/WebView.h" |
25 | 21 |
26 namespace password_manager { | 22 namespace password_manager { |
27 | 23 |
28 namespace { | 24 namespace { |
29 | 25 |
30 blink::WebCredentialManagerError GetWebCredentialManagerErrorFromMojo( | 26 template <typename T> |
31 mojom::CredentialManagerError error) { | 27 void ClearCallbacksMapWithErrors(T* callbacks_map) { |
32 switch (error) { | 28 typename T::iterator iter(callbacks_map); |
33 case mojom::CredentialManagerError::DISABLED: | 29 while (!iter.IsAtEnd()) { |
34 return blink::WebCredentialManagerError:: | 30 iter.GetCurrentValue()->onError(blink::WebCredentialManagerUnknownError); |
35 WebCredentialManagerDisabledError; | 31 callbacks_map->Remove(iter.GetCurrentKey()); |
36 case mojom::CredentialManagerError::PENDINGREQUEST: | 32 iter.Advance(); |
37 return blink::WebCredentialManagerError:: | |
38 WebCredentialManagerPendingRequestError; | |
39 case mojom::CredentialManagerError::PASSWORDSTOREUNAVAILABLE: | |
40 return blink::WebCredentialManagerError:: | |
41 WebCredentialManagerPasswordStoreUnavailableError; | |
42 case mojom::CredentialManagerError::UNKNOWN: | |
43 return blink::WebCredentialManagerError::WebCredentialManagerUnknownError; | |
44 case mojom::CredentialManagerError::SUCCESS: | |
45 NOTREACHED(); | |
46 break; | |
47 } | |
48 | |
49 NOTREACHED(); | |
50 return blink::WebCredentialManagerError::WebCredentialManagerUnknownError; | |
51 } | |
52 | |
53 // Takes ownership of blink::WebCredentialManagerClient::NotificationCallbacks | |
54 // pointer. When the wrapper is destroyed, if |callbacks| is still alive | |
55 // its onError() will get called. | |
56 class NotificationCallbacksWrapper { | |
57 public: | |
58 explicit NotificationCallbacksWrapper( | |
59 blink::WebCredentialManagerClient::NotificationCallbacks* callbacks); | |
60 | |
61 ~NotificationCallbacksWrapper(); | |
62 | |
63 void NotifySuccess(); | |
64 | |
65 private: | |
66 std::unique_ptr<blink::WebCredentialManagerClient::NotificationCallbacks> | |
67 callbacks_; | |
68 | |
69 DISALLOW_COPY_AND_ASSIGN(NotificationCallbacksWrapper); | |
70 }; | |
71 | |
72 NotificationCallbacksWrapper::NotificationCallbacksWrapper( | |
73 blink::WebCredentialManagerClient::NotificationCallbacks* callbacks) | |
74 : callbacks_(callbacks) {} | |
75 | |
76 NotificationCallbacksWrapper::~NotificationCallbacksWrapper() { | |
77 if (callbacks_) | |
78 callbacks_->onError(blink::WebCredentialManagerUnknownError); | |
79 } | |
80 | |
81 void NotificationCallbacksWrapper::NotifySuccess() { | |
82 // Call onSuccess() and reset callbacks to avoid calling onError() in | |
83 // destructor. | |
84 if (callbacks_) { | |
85 callbacks_->onSuccess(); | |
86 callbacks_.reset(); | |
87 } | 33 } |
88 } | 34 } |
89 | 35 |
90 // Takes ownership of blink::WebCredentialManagerClient::RequestCallbacks | |
91 // pointer. When the wrapper is destroied, if |callbacks| is still alive | |
92 // its onError() will get called. | |
93 class RequestCallbacksWrapper { | |
94 public: | |
95 explicit RequestCallbacksWrapper( | |
96 blink::WebCredentialManagerClient::RequestCallbacks* callbacks); | |
97 | |
98 ~RequestCallbacksWrapper(); | |
99 | |
100 void NotifySuccess(mojom::CredentialInfoPtr info); | |
101 | |
102 void NotifyError(mojom::CredentialManagerError error); | |
103 | |
104 private: | |
105 std::unique_ptr<blink::WebCredentialManagerClient::RequestCallbacks> | |
106 callbacks_; | |
107 | |
108 DISALLOW_COPY_AND_ASSIGN(RequestCallbacksWrapper); | |
109 }; | |
110 | |
111 RequestCallbacksWrapper::RequestCallbacksWrapper( | |
112 blink::WebCredentialManagerClient::RequestCallbacks* callbacks) | |
113 : callbacks_(callbacks) {} | |
114 | |
115 RequestCallbacksWrapper::~RequestCallbacksWrapper() { | |
116 if (callbacks_) | |
117 callbacks_->onError(blink::WebCredentialManagerUnknownError); | |
118 } | |
119 | |
120 void RequestCallbacksWrapper::NotifySuccess(mojom::CredentialInfoPtr info) { | |
121 // Call onSuccess() and reset callbacks to avoid calling onError() in | |
122 // destructor. | |
123 if (callbacks_) { | |
124 std::unique_ptr<blink::WebCredential> credential = | |
125 info.To<std::unique_ptr<blink::WebCredential>>(); | |
126 callbacks_->onSuccess(std::move(credential)); | |
127 callbacks_.reset(); | |
128 } | |
129 } | |
130 | |
131 void RequestCallbacksWrapper::NotifyError(mojom::CredentialManagerError error) { | |
132 if (callbacks_) { | |
133 callbacks_->onError(GetWebCredentialManagerErrorFromMojo(error)); | |
134 callbacks_.reset(); | |
135 } | |
136 } | |
137 | |
138 void RespondToNotificationCallback( | |
139 NotificationCallbacksWrapper* callbacks_wrapper) { | |
140 callbacks_wrapper->NotifySuccess(); | |
141 } | |
142 | |
143 void RespondToRequestCallback(RequestCallbacksWrapper* callbacks_wrapper, | |
144 mojom::CredentialManagerError error, | |
145 mojom::CredentialInfoPtr info) { | |
146 if (error == mojom::CredentialManagerError::SUCCESS) { | |
147 DCHECK(!info.is_null()); | |
148 callbacks_wrapper->NotifySuccess(std::move(info)); | |
149 } else { | |
150 DCHECK(info.is_null()); | |
151 callbacks_wrapper->NotifyError(error); | |
152 } | |
153 } | |
154 | |
155 } // namespace | 36 } // namespace |
156 | 37 |
157 CredentialManagerClient::CredentialManagerClient( | 38 CredentialManagerClient::CredentialManagerClient( |
158 content::RenderView* render_view) | 39 content::RenderView* render_view) |
159 : content::RenderViewObserver(render_view) { | 40 : content::RenderViewObserver(render_view) { |
160 render_view->GetWebView()->setCredentialManagerClient(this); | 41 render_view->GetWebView()->setCredentialManagerClient(this); |
161 } | 42 } |
162 | 43 |
163 CredentialManagerClient::~CredentialManagerClient() {} | 44 CredentialManagerClient::~CredentialManagerClient() { |
| 45 ClearCallbacksMapWithErrors(&store_callbacks_); |
| 46 ClearCallbacksMapWithErrors(&require_user_mediation_callbacks_); |
| 47 ClearCallbacksMapWithErrors(&get_callbacks_); |
| 48 } |
164 | 49 |
165 // ----------------------------------------------------------------------------- | 50 // ----------------------------------------------------------------------------- |
166 // Access mojo CredentialManagerService. | 51 // Handle messages from the browser. |
| 52 |
| 53 bool CredentialManagerClient::OnMessageReceived(const IPC::Message& message) { |
| 54 bool handled = true; |
| 55 IPC_BEGIN_MESSAGE_MAP(CredentialManagerClient, message) |
| 56 IPC_MESSAGE_HANDLER(CredentialManagerMsg_AcknowledgeStore, |
| 57 OnAcknowledgeStore) |
| 58 IPC_MESSAGE_HANDLER(CredentialManagerMsg_AcknowledgeRequireUserMediation, |
| 59 OnAcknowledgeRequireUserMediation) |
| 60 IPC_MESSAGE_HANDLER(CredentialManagerMsg_SendCredential, OnSendCredential) |
| 61 IPC_MESSAGE_HANDLER(CredentialManagerMsg_RejectCredentialRequest, |
| 62 OnRejectCredentialRequest) |
| 63 IPC_MESSAGE_UNHANDLED(handled = false) |
| 64 IPC_END_MESSAGE_MAP() |
| 65 return handled; |
| 66 } |
| 67 |
| 68 void CredentialManagerClient::OnAcknowledgeStore(int request_id) { |
| 69 RespondToNotificationCallback(request_id, &store_callbacks_); |
| 70 } |
| 71 |
| 72 void CredentialManagerClient::OnAcknowledgeRequireUserMediation( |
| 73 int request_id) { |
| 74 RespondToNotificationCallback(request_id, &require_user_mediation_callbacks_); |
| 75 } |
| 76 |
| 77 void CredentialManagerClient::OnSendCredential(int request_id, |
| 78 const CredentialInfo& info) { |
| 79 RequestCallbacks* callbacks = get_callbacks_.Lookup(request_id); |
| 80 DCHECK(callbacks); |
| 81 std::unique_ptr<blink::WebCredential> credential; |
| 82 switch (info.type) { |
| 83 case CredentialType::CREDENTIAL_TYPE_FEDERATED: |
| 84 credential.reset(new blink::WebFederatedCredential( |
| 85 info.id, info.federation, info.name, info.icon)); |
| 86 break; |
| 87 case CredentialType::CREDENTIAL_TYPE_PASSWORD: |
| 88 credential.reset(new blink::WebPasswordCredential( |
| 89 info.id, info.password, info.name, info.icon)); |
| 90 break; |
| 91 case CredentialType::CREDENTIAL_TYPE_EMPTY: |
| 92 // Intentionally empty; we'll send nullptr to the onSuccess call below. |
| 93 break; |
| 94 } |
| 95 callbacks->onSuccess(std::move(credential)); |
| 96 get_callbacks_.Remove(request_id); |
| 97 } |
| 98 |
| 99 void CredentialManagerClient::OnRejectCredentialRequest( |
| 100 int request_id, |
| 101 blink::WebCredentialManagerError error) { |
| 102 RequestCallbacks* callbacks = get_callbacks_.Lookup(request_id); |
| 103 DCHECK(callbacks); |
| 104 callbacks->onError(error); |
| 105 get_callbacks_.Remove(request_id); |
| 106 } |
| 107 |
| 108 // ----------------------------------------------------------------------------- |
| 109 // Dispatch messages from the renderer to the browser. |
167 | 110 |
168 void CredentialManagerClient::dispatchStore( | 111 void CredentialManagerClient::dispatchStore( |
169 const blink::WebCredential& credential, | 112 const blink::WebCredential& credential, |
170 blink::WebCredentialManagerClient::NotificationCallbacks* callbacks) { | 113 blink::WebCredentialManagerClient::NotificationCallbacks* callbacks) { |
171 DCHECK(callbacks); | 114 int request_id = store_callbacks_.Add(callbacks); |
172 ConnectToMojoCMIfNeeded(); | 115 CredentialInfo info(WebCredentialToCredentialInfo(credential)); |
173 | 116 Send(new CredentialManagerHostMsg_Store(routing_id(), request_id, info)); |
174 mojom::CredentialInfoPtr info = mojom::CredentialInfo::From(credential); | |
175 mojo_cm_service_->Store( | |
176 std::move(info), | |
177 base::Bind(&RespondToNotificationCallback, | |
178 base::Owned(new NotificationCallbacksWrapper(callbacks)))); | |
179 } | 117 } |
180 | 118 |
181 void CredentialManagerClient::dispatchRequireUserMediation( | 119 void CredentialManagerClient::dispatchRequireUserMediation( |
182 blink::WebCredentialManagerClient::NotificationCallbacks* callbacks) { | 120 NotificationCallbacks* callbacks) { |
183 DCHECK(callbacks); | 121 int request_id = require_user_mediation_callbacks_.Add(callbacks); |
184 ConnectToMojoCMIfNeeded(); | 122 Send(new CredentialManagerHostMsg_RequireUserMediation(routing_id(), |
185 | 123 request_id)); |
186 mojo_cm_service_->RequireUserMediation( | |
187 base::Bind(&RespondToNotificationCallback, | |
188 base::Owned(new NotificationCallbacksWrapper(callbacks)))); | |
189 } | 124 } |
190 | 125 |
191 void CredentialManagerClient::dispatchGet( | 126 void CredentialManagerClient::dispatchGet( |
192 bool zero_click_only, | 127 bool zero_click_only, |
193 bool include_passwords, | 128 bool include_passwords, |
194 const blink::WebVector<blink::WebURL>& federations, | 129 const blink::WebVector<blink::WebURL>& federations, |
195 RequestCallbacks* callbacks) { | 130 RequestCallbacks* callbacks) { |
196 DCHECK(callbacks); | 131 int request_id = get_callbacks_.Add(callbacks); |
197 ConnectToMojoCMIfNeeded(); | |
198 | |
199 std::vector<GURL> federation_vector; | 132 std::vector<GURL> federation_vector; |
200 for (size_t i = 0; i < std::min(federations.size(), kMaxFederations); ++i) | 133 for (size_t i = 0; i < std::min(federations.size(), kMaxFederations); ++i) |
201 federation_vector.push_back(federations[i]); | 134 federation_vector.push_back(federations[i]); |
202 | 135 Send(new CredentialManagerHostMsg_RequestCredential( |
203 mojo_cm_service_->Get( | 136 routing_id(), request_id, zero_click_only, include_passwords, |
204 zero_click_only, include_passwords, | 137 federation_vector)); |
205 mojo::Array<mojo::String>::From(federation_vector), | |
206 base::Bind(&RespondToRequestCallback, | |
207 base::Owned(new RequestCallbacksWrapper(callbacks)))); | |
208 } | 138 } |
209 | 139 |
210 void CredentialManagerClient::ConnectToMojoCMIfNeeded() { | 140 void CredentialManagerClient::RespondToNotificationCallback( |
211 if (mojo_cm_service_) | 141 int request_id, |
212 return; | 142 CredentialManagerClient::NotificationCallbacksMap* map) { |
213 | 143 blink::WebCredentialManagerClient::NotificationCallbacks* callbacks = |
214 content::RenderFrame* main_frame = render_view()->GetMainRenderFrame(); | 144 map->Lookup(request_id); |
215 main_frame->GetServiceRegistry()->ConnectToRemoteService( | 145 DCHECK(callbacks); |
216 mojo::GetProxy(&mojo_cm_service_)); | 146 callbacks->onSuccess(); |
| 147 map->Remove(request_id); |
217 } | 148 } |
218 | 149 |
219 } // namespace password_manager | 150 } // namespace password_manager |
OLD | NEW |