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 "components/password_manager/content/public/cpp/type_converters.h" |
11 #include "components/password_manager/core/common/credential_manager_types.h" | 12 #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" |
12 #include "content/public/renderer/render_view.h" | 15 #include "content/public/renderer/render_view.h" |
| 16 #include "mojo/common/url_type_converters.h" |
13 #include "third_party/WebKit/public/platform/WebCredential.h" | 17 #include "third_party/WebKit/public/platform/WebCredential.h" |
14 #include "third_party/WebKit/public/platform/WebCredentialManagerError.h" | 18 #include "third_party/WebKit/public/platform/WebCredentialManagerError.h" |
15 #include "third_party/WebKit/public/platform/WebFederatedCredential.h" | 19 #include "third_party/WebKit/public/platform/WebFederatedCredential.h" |
16 #include "third_party/WebKit/public/platform/WebPassOwnPtr.h" | 20 #include "third_party/WebKit/public/platform/WebPassOwnPtr.h" |
17 #include "third_party/WebKit/public/platform/WebPasswordCredential.h" | 21 #include "third_party/WebKit/public/platform/WebPasswordCredential.h" |
18 #include "third_party/WebKit/public/web/WebView.h" | 22 #include "third_party/WebKit/public/web/WebView.h" |
19 | 23 |
20 namespace password_manager { | 24 namespace password_manager { |
21 | 25 |
22 namespace { | 26 namespace { |
23 | 27 |
24 template <typename T> | 28 blink::WebCredentialManagerError GetWebCredentialManagerErrorFromMojo( |
25 void ClearCallbacksMapWithErrors(T* callbacks_map) { | 29 mojom::CredentialManagerError error) { |
26 typename T::iterator iter(callbacks_map); | 30 switch (error) { |
27 while (!iter.IsAtEnd()) { | 31 case mojom::CredentialManagerError::DISABLED: |
28 iter.GetCurrentValue()->onError(blink::WebCredentialManagerUnknownError); | 32 return blink::WebCredentialManagerError:: |
29 callbacks_map->Remove(iter.GetCurrentKey()); | 33 WebCredentialManagerDisabledError; |
30 iter.Advance(); | 34 case mojom::CredentialManagerError::PENDINGREQUEST: |
| 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 } |
| 86 } |
| 87 |
| 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); |
31 } | 150 } |
32 } | 151 } |
33 | 152 |
34 } // namespace | 153 } // namespace |
35 | 154 |
36 CredentialManagerClient::CredentialManagerClient( | 155 CredentialManagerClient::CredentialManagerClient( |
37 content::RenderView* render_view) | 156 content::RenderView* render_view) |
38 : content::RenderViewObserver(render_view) { | 157 : content::RenderViewObserver(render_view) { |
39 render_view->GetWebView()->setCredentialManagerClient(this); | 158 render_view->GetWebView()->setCredentialManagerClient(this); |
40 } | 159 } |
41 | 160 |
42 CredentialManagerClient::~CredentialManagerClient() { | 161 CredentialManagerClient::~CredentialManagerClient() {} |
43 ClearCallbacksMapWithErrors(&store_callbacks_); | |
44 ClearCallbacksMapWithErrors(&require_user_mediation_callbacks_); | |
45 ClearCallbacksMapWithErrors(&get_callbacks_); | |
46 } | |
47 | 162 |
48 // ----------------------------------------------------------------------------- | 163 // ----------------------------------------------------------------------------- |
49 // Handle messages from the browser. | 164 // 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 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. | |
108 | 165 |
109 void CredentialManagerClient::dispatchStore( | 166 void CredentialManagerClient::dispatchStore( |
110 const blink::WebCredential& credential, | 167 const blink::WebCredential& credential, |
111 blink::WebCredentialManagerClient::NotificationCallbacks* callbacks) { | 168 blink::WebCredentialManagerClient::NotificationCallbacks* callbacks) { |
112 int request_id = store_callbacks_.Add(callbacks); | 169 DCHECK(callbacks); |
113 CredentialInfo info(WebCredentialToCredentialInfo(credential)); | 170 ConnectToMojoCMIfNeeded(); |
114 Send(new CredentialManagerHostMsg_Store(routing_id(), request_id, info)); | 171 |
| 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)))); |
115 } | 177 } |
116 | 178 |
117 void CredentialManagerClient::dispatchRequireUserMediation( | 179 void CredentialManagerClient::dispatchRequireUserMediation( |
118 NotificationCallbacks* callbacks) { | 180 blink::WebCredentialManagerClient::NotificationCallbacks* callbacks) { |
119 int request_id = require_user_mediation_callbacks_.Add(callbacks); | 181 DCHECK(callbacks); |
120 Send(new CredentialManagerHostMsg_RequireUserMediation(routing_id(), | 182 ConnectToMojoCMIfNeeded(); |
121 request_id)); | 183 |
| 184 mojo_cm_service_->RequireUserMediation( |
| 185 base::Bind(&RespondToNotificationCallback, |
| 186 base::Owned(new NotificationCallbacksWrapper(callbacks)))); |
122 } | 187 } |
123 | 188 |
124 void CredentialManagerClient::dispatchGet( | 189 void CredentialManagerClient::dispatchGet( |
125 bool zero_click_only, | 190 bool zero_click_only, |
126 bool include_passwords, | 191 bool include_passwords, |
127 const blink::WebVector<blink::WebURL>& federations, | 192 const blink::WebVector<blink::WebURL>& federations, |
128 RequestCallbacks* callbacks) { | 193 RequestCallbacks* callbacks) { |
129 int request_id = get_callbacks_.Add(callbacks); | 194 DCHECK(callbacks); |
| 195 ConnectToMojoCMIfNeeded(); |
| 196 |
130 std::vector<GURL> federation_vector; | 197 std::vector<GURL> federation_vector; |
131 for (size_t i = 0; i < std::min(federations.size(), kMaxFederations); ++i) | 198 for (size_t i = 0; i < std::min(federations.size(), kMaxFederations); ++i) |
132 federation_vector.push_back(federations[i]); | 199 federation_vector.push_back(federations[i]); |
133 Send(new CredentialManagerHostMsg_RequestCredential( | 200 |
134 routing_id(), request_id, zero_click_only, include_passwords, | 201 mojo_cm_service_->Get( |
135 federation_vector)); | 202 zero_click_only, include_passwords, |
| 203 mojo::Array<mojo::String>::From(federation_vector), |
| 204 base::Bind(&RespondToRequestCallback, |
| 205 base::Owned(new RequestCallbacksWrapper(callbacks)))); |
136 } | 206 } |
137 | 207 |
138 void CredentialManagerClient::RespondToNotificationCallback( | 208 void CredentialManagerClient::ConnectToMojoCMIfNeeded() { |
139 int request_id, | 209 if (mojo_cm_service_) |
140 CredentialManagerClient::NotificationCallbacksMap* map) { | 210 return; |
141 blink::WebCredentialManagerClient::NotificationCallbacks* callbacks = | 211 |
142 map->Lookup(request_id); | 212 content::RenderFrame* main_frame = render_view()->GetMainRenderFrame(); |
143 DCHECK(callbacks); | 213 main_frame->GetServiceRegistry()->ConnectToRemoteService( |
144 callbacks->onSuccess(); | 214 mojo::GetProxy(&mojo_cm_service_)); |
145 map->Remove(request_id); | |
146 } | 215 } |
147 | 216 |
148 } // namespace password_manager | 217 } // namespace password_manager |
OLD | NEW |