Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(44)

Side by Side Diff: components/password_manager/content/renderer/credential_manager_client.cc

Issue 1866643002: Reland: Switch components/password_manager code from IPC messages to Mojo. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase only Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "components/password_manager/content/common/credential_manager_content_ utils.h" 12 #include "base/bind.h"
13 #include "components/password_manager/content/common/credential_manager_messages .h" 13 #include "base/logging.h"
14 #include "components/password_manager/content/public/cpp/type_converters.h"
14 #include "components/password_manager/core/common/credential_manager_types.h" 15 #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"
15 #include "content/public/renderer/render_view.h" 18 #include "content/public/renderer/render_view.h"
19 #include "mojo/common/url_type_converters.h"
16 #include "third_party/WebKit/public/platform/WebCredential.h" 20 #include "third_party/WebKit/public/platform/WebCredential.h"
17 #include "third_party/WebKit/public/platform/WebCredentialManagerError.h" 21 #include "third_party/WebKit/public/platform/WebCredentialManagerError.h"
18 #include "third_party/WebKit/public/platform/WebFederatedCredential.h" 22 #include "third_party/WebKit/public/platform/WebFederatedCredential.h"
19 #include "third_party/WebKit/public/platform/WebPasswordCredential.h" 23 #include "third_party/WebKit/public/platform/WebPasswordCredential.h"
20 #include "third_party/WebKit/public/web/WebView.h" 24 #include "third_party/WebKit/public/web/WebView.h"
21 25
22 namespace password_manager { 26 namespace password_manager {
23 27
24 namespace { 28 namespace {
25 29
26 template <typename T> 30 blink::WebCredentialManagerError GetWebCredentialManagerErrorFromMojo(
27 void ClearCallbacksMapWithErrors(T* callbacks_map) { 31 mojom::CredentialManagerError error) {
28 typename T::iterator iter(callbacks_map); 32 switch (error) {
29 while (!iter.IsAtEnd()) { 33 case mojom::CredentialManagerError::DISABLED:
30 iter.GetCurrentValue()->onError(blink::WebCredentialManagerUnknownError); 34 return blink::WebCredentialManagerError::
31 callbacks_map->Remove(iter.GetCurrentKey()); 35 WebCredentialManagerDisabledError;
32 iter.Advance(); 36 case mojom::CredentialManagerError::PENDINGREQUEST:
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 }
88 }
89
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);
33 } 152 }
34 } 153 }
35 154
36 } // namespace 155 } // namespace
37 156
38 CredentialManagerClient::CredentialManagerClient( 157 CredentialManagerClient::CredentialManagerClient(
39 content::RenderView* render_view) 158 content::RenderView* render_view)
40 : content::RenderViewObserver(render_view) { 159 : content::RenderViewObserver(render_view) {
41 render_view->GetWebView()->setCredentialManagerClient(this); 160 render_view->GetWebView()->setCredentialManagerClient(this);
42 } 161 }
43 162
44 CredentialManagerClient::~CredentialManagerClient() { 163 CredentialManagerClient::~CredentialManagerClient() {}
45 ClearCallbacksMapWithErrors(&store_callbacks_);
46 ClearCallbacksMapWithErrors(&require_user_mediation_callbacks_);
47 ClearCallbacksMapWithErrors(&get_callbacks_);
48 }
49 164
50 // ----------------------------------------------------------------------------- 165 // -----------------------------------------------------------------------------
51 // Handle messages from the browser. 166 // Access mojo CredentialManagerService.
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.
110 167
111 void CredentialManagerClient::dispatchStore( 168 void CredentialManagerClient::dispatchStore(
112 const blink::WebCredential& credential, 169 const blink::WebCredential& credential,
113 blink::WebCredentialManagerClient::NotificationCallbacks* callbacks) { 170 blink::WebCredentialManagerClient::NotificationCallbacks* callbacks) {
114 int request_id = store_callbacks_.Add(callbacks); 171 DCHECK(callbacks);
115 CredentialInfo info(WebCredentialToCredentialInfo(credential)); 172 ConnectToMojoCMIfNeeded();
116 Send(new CredentialManagerHostMsg_Store(routing_id(), request_id, info)); 173
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))));
117 } 179 }
118 180
119 void CredentialManagerClient::dispatchRequireUserMediation( 181 void CredentialManagerClient::dispatchRequireUserMediation(
120 NotificationCallbacks* callbacks) { 182 blink::WebCredentialManagerClient::NotificationCallbacks* callbacks) {
121 int request_id = require_user_mediation_callbacks_.Add(callbacks); 183 DCHECK(callbacks);
122 Send(new CredentialManagerHostMsg_RequireUserMediation(routing_id(), 184 ConnectToMojoCMIfNeeded();
123 request_id)); 185
186 mojo_cm_service_->RequireUserMediation(
187 base::Bind(&RespondToNotificationCallback,
188 base::Owned(new NotificationCallbacksWrapper(callbacks))));
124 } 189 }
125 190
126 void CredentialManagerClient::dispatchGet( 191 void CredentialManagerClient::dispatchGet(
127 bool zero_click_only, 192 bool zero_click_only,
128 bool include_passwords, 193 bool include_passwords,
129 const blink::WebVector<blink::WebURL>& federations, 194 const blink::WebVector<blink::WebURL>& federations,
130 RequestCallbacks* callbacks) { 195 RequestCallbacks* callbacks) {
131 int request_id = get_callbacks_.Add(callbacks); 196 DCHECK(callbacks);
197 ConnectToMojoCMIfNeeded();
198
132 std::vector<GURL> federation_vector; 199 std::vector<GURL> federation_vector;
133 for (size_t i = 0; i < std::min(federations.size(), kMaxFederations); ++i) 200 for (size_t i = 0; i < std::min(federations.size(), kMaxFederations); ++i)
134 federation_vector.push_back(federations[i]); 201 federation_vector.push_back(federations[i]);
135 Send(new CredentialManagerHostMsg_RequestCredential( 202
136 routing_id(), request_id, zero_click_only, include_passwords, 203 mojo_cm_service_->Get(
137 federation_vector)); 204 zero_click_only, include_passwords,
205 mojo::Array<mojo::String>::From(federation_vector),
206 base::Bind(&RespondToRequestCallback,
207 base::Owned(new RequestCallbacksWrapper(callbacks))));
138 } 208 }
139 209
140 void CredentialManagerClient::RespondToNotificationCallback( 210 void CredentialManagerClient::ConnectToMojoCMIfNeeded() {
141 int request_id, 211 if (mojo_cm_service_)
142 CredentialManagerClient::NotificationCallbacksMap* map) { 212 return;
143 blink::WebCredentialManagerClient::NotificationCallbacks* callbacks = 213
144 map->Lookup(request_id); 214 content::RenderFrame* main_frame = render_view()->GetMainRenderFrame();
145 DCHECK(callbacks); 215 main_frame->GetServiceRegistry()->ConnectToRemoteService(
146 callbacks->onSuccess(); 216 mojo::GetProxy(&mojo_cm_service_));
147 map->Remove(request_id);
148 } 217 }
149 218
150 } // namespace password_manager 219 } // namespace password_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698