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