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

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

Issue 1762603002: Switch components/password_manager code from IPC messages to Mojo. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Impl done, unit_tests and browser_tests not ready yet Created 4 years, 9 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 "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 DCHECK(error != mojom::CredentialManagerError::SUCCESS);
vabr (Chromium) 2016/03/09 16:26:30 nit: DCHECK_NE(mojom::CredentialManagerError::SUCC
leonhsl(Using Gerrit) 2016/03/10 06:42:42 Done. Moved the check into the switch sentence bel
27 while (!iter.IsAtEnd()) { 32 switch (error) {
28 iter.GetCurrentValue()->onError(blink::WebCredentialManagerUnknownError); 33 case mojom::CredentialManagerError::DISABLED:
29 callbacks_map->Remove(iter.GetCurrentKey()); 34 return blink::WebCredentialManagerError::
30 iter.Advance(); 35 WebCredentialManagerDisabledError;
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 default:
vabr (Chromium) 2016/03/09 16:26:29 Please drop the default clause if possible to have
leonhsl(Using Gerrit) 2016/03/10 06:42:42 Done.
45 NOTREACHED();
46 }
47
48 return blink::WebCredentialManagerError::WebCredentialManagerUnknownError;
49 }
50
51 // Takes ownership of blink::WebCredentialManagerClient::NotificationCallbacks
52 // pointer. When the wrapper is destroied, if callbacks is still alive
vabr (Chromium) 2016/03/09 16:26:29 typo: destroyed Also below.
vabr (Chromium) 2016/03/09 16:26:30 nit: Add || around "callbacks" to signal you mean
leonhsl(Using Gerrit) 2016/03/10 06:42:42 Done.
leonhsl(Using Gerrit) 2016/03/10 06:42:42 Done.
53 // its onError() will get called.
54 class NotificationCallbacksWrapper {
55 public:
56 explicit NotificationCallbacksWrapper(
57 blink::WebCredentialManagerClient::NotificationCallbacks* callbacks)
58 : callbacks_(callbacks){};
vabr (Chromium) 2016/03/09 16:26:29 nit: There should be a space between {}, did you r
leonhsl(Using Gerrit) 2016/03/10 06:42:42 Done. Yes I did run 'git cl format', eh, but here
59 ~NotificationCallbacksWrapper() {
vabr (Chromium) 2016/03/09 16:26:29 Please do not inline methods, the style forbids it
vabr (Chromium) 2016/03/09 16:26:30 nit: blank line above
leonhsl(Using Gerrit) 2016/03/10 06:42:42 Done.
leonhsl(Using Gerrit) 2016/03/10 06:42:42 Done.
60 // Force to call onError() for hanged callbacks.
vabr (Chromium) 2016/03/09 16:26:29 nit: Just drop the comment, the code is clear with
leonhsl(Using Gerrit) 2016/03/10 06:42:42 Done.
61 if (callbacks_)
62 callbacks_->onError(blink::WebCredentialManagerUnknownError);
63 }
64
65 void NotifySuccess() {
66 // Call onSuccess() and reset callbacks to avoid calling onError() in
67 // destructor.
68 if (callbacks_) {
69 callbacks_->onSuccess();
70 callbacks_.reset();
71 }
72 }
73
74 private:
75 scoped_ptr<blink::WebCredentialManagerClient::NotificationCallbacks>
76 callbacks_;
77 };
vabr (Chromium) 2016/03/09 16:26:30 Did you consider DISALLOW_COPY_AND_ASSIGN for the
leonhsl(Using Gerrit) 2016/03/10 06:42:42 Done. Will keep in mind!
78
79 // Takes ownership of blink::WebCredentialManagerClient::RequestCallbacks
80 // pointer. When the wrapper is destroied, if callbacks is still alive
81 // its onError() will get called.
82 class RequestCallbacksWrapper {
83 public:
84 explicit RequestCallbacksWrapper(
85 blink::WebCredentialManagerClient::RequestCallbacks* callbacks)
86 : callbacks_(callbacks) {}
87 ~RequestCallbacksWrapper() {
vabr (Chromium) 2016/03/09 16:26:30 nit: blank line above
leonhsl(Using Gerrit) 2016/03/10 06:42:42 Done.
88 // Force to call onError() for hanged callbacks.
vabr (Chromium) 2016/03/09 16:26:30 nit: Drop the comment.
leonhsl(Using Gerrit) 2016/03/10 06:42:42 Done.
89 if (callbacks_)
90 callbacks_->onError(blink::WebCredentialManagerUnknownError);
91 }
92
93 void NotifySuccess(mojom::CredentialInfoPtr info) {
94 // Call onSuccess() and reset callbacks to avoid calling onError() in
95 // destructor.
96 if (callbacks_) {
97 BlinkWebCredentialPtr credential = info.To<BlinkWebCredentialPtr>();
98 callbacks_->onSuccess(adoptWebPtr(credential.release()));
99 callbacks_.reset();
100 }
101 }
102 void NotifyError(mojom::CredentialManagerError error) {
103 if (callbacks_) {
104 callbacks_->onError(GetWebCredentialManagerErrorFromMojo(error));
105 callbacks_.reset();
106 }
107 }
108
109 private:
110 scoped_ptr<blink::WebCredentialManagerClient::RequestCallbacks> callbacks_;
111 };
112
113 void RespondToNotificationCallback(
114 scoped_ptr<NotificationCallbacksWrapper> callbacks_wrapper) {
115 callbacks_wrapper->NotifySuccess();
116 }
117
118 void RespondToRequestCallback(
119 scoped_ptr<RequestCallbacksWrapper> callbacks_wrapper,
120 mojom::CredentialManagerError error,
121 mojom::CredentialInfoPtr info) {
122 if (error == mojom::CredentialManagerError::SUCCESS) {
123 DCHECK(!info.is_null());
124 callbacks_wrapper->NotifySuccess(std::move(info));
125 } else {
126 DCHECK(info.is_null());
127 callbacks_wrapper->NotifyError(error);
31 } 128 }
32 } 129 }
33 130
34 } // namespace 131 } // namespace
35 132
36 CredentialManagerClient::CredentialManagerClient( 133 CredentialManagerClient::CredentialManagerClient(
37 content::RenderView* render_view) 134 content::RenderView* render_view)
38 : content::RenderViewObserver(render_view) { 135 : content::RenderViewObserver(render_view) {
39 render_view->GetWebView()->setCredentialManagerClient(this); 136 render_view->GetWebView()->setCredentialManagerClient(this);
40 } 137 }
41 138
42 CredentialManagerClient::~CredentialManagerClient() { 139 CredentialManagerClient::~CredentialManagerClient() {}
43 ClearCallbacksMapWithErrors(&store_callbacks_);
44 ClearCallbacksMapWithErrors(&require_user_mediation_callbacks_);
45 ClearCallbacksMapWithErrors(&get_callbacks_);
46 }
47 140
48 // ----------------------------------------------------------------------------- 141 // -----------------------------------------------------------------------------
49 // Handle messages from the browser. 142 // 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 143
109 void CredentialManagerClient::dispatchStore( 144 void CredentialManagerClient::dispatchStore(
110 const blink::WebCredential& credential, 145 const blink::WebCredential& credential,
111 blink::WebCredentialManagerClient::NotificationCallbacks* callbacks) { 146 blink::WebCredentialManagerClient::NotificationCallbacks* callbacks) {
112 int request_id = store_callbacks_.Add(callbacks); 147 DCHECK(callbacks);
113 CredentialInfo info(WebCredentialToCredentialInfo(credential)); 148 ConnectToMojoCMIfNeeded();
114 Send(new CredentialManagerHostMsg_Store(routing_id(), request_id, info)); 149
150 mojom::CredentialInfoPtr info = mojom::CredentialInfo::From(credential);
151 mojo_cm_service_->Store(
152 std::move(info),
153 base::Bind(&RespondToNotificationCallback,
154 base::Passed(make_scoped_ptr(
155 new NotificationCallbacksWrapper(callbacks)))));
115 } 156 }
116 157
117 void CredentialManagerClient::dispatchRequireUserMediation( 158 void CredentialManagerClient::dispatchRequireUserMediation(
118 NotificationCallbacks* callbacks) { 159 blink::WebCredentialManagerClient::NotificationCallbacks* callbacks) {
119 int request_id = require_user_mediation_callbacks_.Add(callbacks); 160 DCHECK(callbacks);
120 Send(new CredentialManagerHostMsg_RequireUserMediation(routing_id(), 161 ConnectToMojoCMIfNeeded();
121 request_id)); 162
163 mojo_cm_service_->RequireUserMediation(base::Bind(
164 &RespondToNotificationCallback,
165 base::Passed(
166 make_scoped_ptr(new NotificationCallbacksWrapper(callbacks)))));
122 } 167 }
123 168
124 void CredentialManagerClient::dispatchGet( 169 void CredentialManagerClient::dispatchGet(
125 bool zero_click_only, 170 bool zero_click_only,
126 bool include_passwords, 171 bool include_passwords,
127 const blink::WebVector<blink::WebURL>& federations, 172 const blink::WebVector<blink::WebURL>& federations,
128 RequestCallbacks* callbacks) { 173 RequestCallbacks* callbacks) {
129 int request_id = get_callbacks_.Add(callbacks); 174 DCHECK(callbacks);
175 ConnectToMojoCMIfNeeded();
176
130 std::vector<GURL> federation_vector; 177 std::vector<GURL> federation_vector;
131 for (size_t i = 0; i < std::min(federations.size(), kMaxFederations); ++i) 178 for (size_t i = 0; i < std::min(federations.size(), kMaxFederations); ++i)
132 federation_vector.push_back(federations[i]); 179 federation_vector.push_back(federations[i]);
133 Send(new CredentialManagerHostMsg_RequestCredential( 180
134 routing_id(), request_id, zero_click_only, include_passwords, 181 mojo_cm_service_->Get(
135 federation_vector)); 182 zero_click_only, include_passwords,
183 mojo::Array<mojo::String>::From(federation_vector),
184 base::Bind(&RespondToRequestCallback,
185 base::Passed(
186 make_scoped_ptr(new RequestCallbacksWrapper(callbacks)))));
136 } 187 }
137 188
138 void CredentialManagerClient::RespondToNotificationCallback( 189 void CredentialManagerClient::ConnectToMojoCMIfNeeded() {
139 int request_id, 190 if (mojo_cm_service_)
140 CredentialManagerClient::NotificationCallbacksMap* map) { 191 return;
141 blink::WebCredentialManagerClient::NotificationCallbacks* callbacks = 192
142 map->Lookup(request_id); 193 // Connect via the service registry of the main render frame.
143 DCHECK(callbacks); 194 content::RenderFrame* main_frame = render_view()->GetMainRenderFrame();
144 callbacks->onSuccess(); 195 main_frame->GetServiceRegistry()->ConnectToRemoteService(
145 map->Remove(request_id); 196 mojo::GetProxy(&mojo_cm_service_));
146 } 197 }
147 198
148 } // namespace password_manager 199 } // namespace password_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698