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