Chromium Code Reviews| 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 NOTREACHED(); | |
| 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 | |
| 60 ~NotificationCallbacksWrapper(); | |
| 61 | |
| 62 void NotifySuccess(); | |
| 63 | |
| 64 private: | |
| 65 scoped_ptr<blink::WebCredentialManagerClient::NotificationCallbacks> | |
| 66 callbacks_; | |
| 67 | |
| 68 DISALLOW_COPY_AND_ASSIGN(NotificationCallbacksWrapper); | |
| 69 }; | |
| 70 | |
| 71 NotificationCallbacksWrapper::NotificationCallbacksWrapper( | |
| 72 blink::WebCredentialManagerClient::NotificationCallbacks* callbacks) | |
| 73 : callbacks_(callbacks) {} | |
| 74 | |
| 75 NotificationCallbacksWrapper::~NotificationCallbacksWrapper() { | |
| 76 if (callbacks_) | |
| 77 callbacks_->onError(blink::WebCredentialManagerUnknownError); | |
| 78 } | |
| 79 | |
| 80 void NotificationCallbacksWrapper::NotifySuccess() { | |
| 81 // Call onSuccess() and reset callbacks to avoid calling onError() in | |
| 82 // destructor. | |
| 83 if (callbacks_) { | |
| 84 callbacks_->onSuccess(); | |
| 85 callbacks_.reset(); | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 // Takes ownership of blink::WebCredentialManagerClient::RequestCallbacks | |
| 90 // pointer. When the wrapper is destroied, if |callbacks| is still alive | |
| 91 // its onError() will get called. | |
| 92 class RequestCallbacksWrapper { | |
| 93 public: | |
| 94 explicit RequestCallbacksWrapper( | |
| 95 blink::WebCredentialManagerClient::RequestCallbacks* callbacks); | |
| 96 | |
| 97 ~RequestCallbacksWrapper(); | |
| 98 | |
| 99 void NotifySuccess(mojom::CredentialInfoPtr info); | |
| 100 | |
| 101 void NotifyError(mojom::CredentialManagerError error); | |
| 102 | |
| 103 private: | |
| 104 scoped_ptr<blink::WebCredentialManagerClient::RequestCallbacks> 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 scoped_ptr<blink::WebCredential> credential = | |
| 123 info.To<scoped_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 scoped_ptr<NotificationCallbacksWrapper> callbacks_wrapper) { | |
| 138 callbacks_wrapper->NotifySuccess(); | |
| 139 } | |
| 140 | |
| 141 void RespondToRequestCallback( | |
| 142 scoped_ptr<RequestCallbacksWrapper> callbacks_wrapper, | |
| 143 mojom::CredentialManagerError error, | |
| 144 mojom::CredentialInfoPtr info) { | |
| 145 if (error == mojom::CredentialManagerError::SUCCESS) { | |
| 146 DCHECK(!info.is_null()); | |
| 147 callbacks_wrapper->NotifySuccess(std::move(info)); | |
| 148 } else { | |
| 149 DCHECK(info.is_null()); | |
| 150 callbacks_wrapper->NotifyError(error); | |
| 31 } | 151 } |
| 32 } | 152 } |
| 33 | 153 |
| 34 } // namespace | 154 } // namespace |
| 35 | 155 |
| 36 CredentialManagerClient::CredentialManagerClient( | 156 CredentialManagerClient::CredentialManagerClient( |
| 37 content::RenderView* render_view) | 157 content::RenderView* render_view) |
| 38 : content::RenderViewObserver(render_view) { | 158 : content::RenderViewObserver(render_view) { |
| 39 render_view->GetWebView()->setCredentialManagerClient(this); | 159 render_view->GetWebView()->setCredentialManagerClient(this); |
| 40 } | 160 } |
| 41 | 161 |
| 42 CredentialManagerClient::~CredentialManagerClient() { | 162 CredentialManagerClient::~CredentialManagerClient() {} |
| 43 ClearCallbacksMapWithErrors(&store_callbacks_); | |
| 44 ClearCallbacksMapWithErrors(&require_user_mediation_callbacks_); | |
| 45 ClearCallbacksMapWithErrors(&get_callbacks_); | |
| 46 } | |
| 47 | 163 |
| 48 // ----------------------------------------------------------------------------- | 164 // ----------------------------------------------------------------------------- |
| 49 // Handle messages from the browser. | 165 // 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 | 166 |
| 109 void CredentialManagerClient::dispatchStore( | 167 void CredentialManagerClient::dispatchStore( |
| 110 const blink::WebCredential& credential, | 168 const blink::WebCredential& credential, |
| 111 blink::WebCredentialManagerClient::NotificationCallbacks* callbacks) { | 169 blink::WebCredentialManagerClient::NotificationCallbacks* callbacks) { |
| 112 int request_id = store_callbacks_.Add(callbacks); | 170 DCHECK(callbacks); |
| 113 CredentialInfo info(WebCredentialToCredentialInfo(credential)); | 171 ConnectToMojoCMIfNeeded(); |
| 114 Send(new CredentialManagerHostMsg_Store(routing_id(), request_id, info)); | 172 |
| 173 mojom::CredentialInfoPtr info = mojom::CredentialInfo::From(credential); | |
| 174 mojo_cm_service_->Store( | |
| 175 std::move(info), | |
| 176 base::Bind(&RespondToNotificationCallback, | |
| 177 base::Passed(make_scoped_ptr( | |
|
Anand Mistry (off Chromium)
2016/03/18 01:24:13
base::Owned does what you need.
leonhsl(Using Gerrit)
2016/03/18 10:02:16
Done.
| |
| 178 new NotificationCallbacksWrapper(callbacks))))); | |
| 115 } | 179 } |
| 116 | 180 |
| 117 void CredentialManagerClient::dispatchRequireUserMediation( | 181 void CredentialManagerClient::dispatchRequireUserMediation( |
| 118 NotificationCallbacks* callbacks) { | 182 blink::WebCredentialManagerClient::NotificationCallbacks* callbacks) { |
| 119 int request_id = require_user_mediation_callbacks_.Add(callbacks); | 183 DCHECK(callbacks); |
| 120 Send(new CredentialManagerHostMsg_RequireUserMediation(routing_id(), | 184 ConnectToMojoCMIfNeeded(); |
| 121 request_id)); | 185 |
| 186 mojo_cm_service_->RequireUserMediation(base::Bind( | |
| 187 &RespondToNotificationCallback, | |
| 188 base::Passed( | |
| 189 make_scoped_ptr(new NotificationCallbacksWrapper(callbacks))))); | |
| 122 } | 190 } |
| 123 | 191 |
| 124 void CredentialManagerClient::dispatchGet( | 192 void CredentialManagerClient::dispatchGet( |
| 125 bool zero_click_only, | 193 bool zero_click_only, |
| 126 bool include_passwords, | 194 bool include_passwords, |
| 127 const blink::WebVector<blink::WebURL>& federations, | 195 const blink::WebVector<blink::WebURL>& federations, |
| 128 RequestCallbacks* callbacks) { | 196 RequestCallbacks* callbacks) { |
| 129 int request_id = get_callbacks_.Add(callbacks); | 197 DCHECK(callbacks); |
| 198 ConnectToMojoCMIfNeeded(); | |
| 199 | |
| 130 std::vector<GURL> federation_vector; | 200 std::vector<GURL> federation_vector; |
| 131 for (size_t i = 0; i < std::min(federations.size(), kMaxFederations); ++i) | 201 for (size_t i = 0; i < std::min(federations.size(), kMaxFederations); ++i) |
| 132 federation_vector.push_back(federations[i]); | 202 federation_vector.push_back(federations[i]); |
| 133 Send(new CredentialManagerHostMsg_RequestCredential( | 203 |
| 134 routing_id(), request_id, zero_click_only, include_passwords, | 204 mojo_cm_service_->Get( |
| 135 federation_vector)); | 205 zero_click_only, include_passwords, |
| 206 mojo::Array<mojo::String>::From(federation_vector), | |
| 207 base::Bind(&RespondToRequestCallback, | |
| 208 base::Passed( | |
| 209 make_scoped_ptr(new RequestCallbacksWrapper(callbacks))))); | |
| 136 } | 210 } |
| 137 | 211 |
| 138 void CredentialManagerClient::RespondToNotificationCallback( | 212 void CredentialManagerClient::ConnectToMojoCMIfNeeded() { |
| 139 int request_id, | 213 if (mojo_cm_service_) |
| 140 CredentialManagerClient::NotificationCallbacksMap* map) { | 214 return; |
| 141 blink::WebCredentialManagerClient::NotificationCallbacks* callbacks = | 215 |
| 142 map->Lookup(request_id); | 216 // Connect via the service registry of the main render frame. |
| 143 DCHECK(callbacks); | 217 content::RenderFrame* main_frame = render_view()->GetMainRenderFrame(); |
| 144 callbacks->onSuccess(); | 218 main_frame->GetServiceRegistry()->ConnectToRemoteService( |
| 145 map->Remove(request_id); | 219 mojo::GetProxy(&mojo_cm_service_)); |
| 146 } | 220 } |
| 147 | 221 |
| 148 } // namespace password_manager | 222 } // namespace password_manager |
| OLD | NEW |