Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/password_manager/content/renderer/credential_manager_dispat cher.h" | |
| 6 | |
| 7 #include "components/password_manager/content/common/credential_manager_messages .h" | |
| 8 #include "components/password_manager/content/common/credential_manager_types.h" | |
| 9 #include "content/public/renderer/render_thread.h" | |
| 10 #include "third_party/WebKit/public/platform/WebCredentialManagerError.h" | |
| 11 | |
| 12 namespace password_manager { | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 template <typename T> | |
| 17 void ClearCallbacksMapWithErrors(T* callbacks_map) { | |
| 18 typename T::iterator iter(callbacks_map); | |
| 19 while (!iter.IsAtEnd()) { | |
| 20 blink::WebCredentialManagerError reason( | |
| 21 blink::WebCredentialManagerError::ErrorTypeUnknown, | |
| 22 "An unknown error occurred."); | |
|
Ilya Sherman
2014/08/19 23:55:52
Is there a plan to add more error types? If not,
Mike West
2014/08/20 13:17:19
I put it in for flexibility as the API develops; i
| |
| 23 iter.GetCurrentValue()->onError(&reason); | |
| 24 callbacks_map->Remove(iter.GetCurrentKey()); | |
| 25 iter.Advance(); | |
| 26 } | |
| 27 } | |
| 28 | |
| 29 // Limit the size of the federations array that we pass to the browser to | |
| 30 // something reasonably sane. | |
|
palmer
2014/08/19 18:28:44
Does the browser enforce this limit upon receipt?
Mike West
2014/08/20 13:17:19
Makes sense, moving this to 'content/common/creden
| |
| 31 const size_t kMaxNumFederations = 100; | |
|
palmer
2014/08/19 18:28:44
Could also be declared static?
Ilya Sherman
2014/08/19 23:55:52
I'm pretty sure that 'static' would have no effect
| |
| 32 | |
| 33 void RespondToNotificationCallback( | |
| 34 int request_id, | |
| 35 CredentialManagerDispatcher::NotificationCallbacksMap* map) { | |
| 36 blink::WebCredentialManager::NotificationCallbacks* callbacks = | |
| 37 map->Lookup(request_id); | |
| 38 DCHECK(callbacks); | |
|
Ilya Sherman
2014/08/19 23:55:53
nit: This seems redundant with the call on the lin
Mike West
2014/08/20 13:17:19
Sure, but it's a nicer error message if it blows u
| |
| 39 callbacks->onSuccess(); | |
| 40 map->Remove(request_id); | |
| 41 } | |
| 42 | |
| 43 } // namespace | |
| 44 | |
| 45 CredentialManagerDispatcher::CredentialManagerDispatcher() | |
| 46 : routing_id_(MSG_ROUTING_NONE) | |
| 47 , render_thread_(content::RenderThread::Get()) { | |
| 48 DCHECK(render_thread_); | |
| 49 } | |
| 50 | |
| 51 CredentialManagerDispatcher::~CredentialManagerDispatcher() { | |
| 52 DisconnectFromRenderThread(); | |
| 53 ClearCallbackErrorMaps(); | |
|
Ilya Sherman
2014/08/19 23:55:52
nit: Perhaps "ClearCallbackMapsWithErrors"?
Mike West
2014/08/20 13:17:19
Dropping this. Earlier, I needed to override this
| |
| 54 } | |
| 55 | |
| 56 void CredentialManagerDispatcher::OnRenderProcessShutdown() { | |
| 57 DisconnectFromRenderThread(); | |
| 58 } | |
| 59 | |
| 60 void CredentialManagerDispatcher::ClearCallbackErrorMaps() { | |
| 61 ClearCallbacksMapWithErrors(&failed_sign_in_callbacks_); | |
| 62 ClearCallbacksMapWithErrors(&signed_in_callbacks_); | |
| 63 ClearCallbacksMapWithErrors(&signed_out_callbacks_); | |
| 64 ClearCallbacksMapWithErrors(&request_callbacks_); | |
| 65 } | |
| 66 | |
| 67 int CredentialManagerDispatcher::GetRoutingID() { | |
| 68 if (render_thread_ && routing_id_ == MSG_ROUTING_NONE) { | |
| 69 routing_id_ = render_thread_->GenerateRoutingID(); | |
| 70 render_thread_->AddRoute(routing_id_, this); | |
| 71 } | |
| 72 return routing_id_; | |
| 73 } | |
| 74 | |
| 75 void CredentialManagerDispatcher::DisconnectFromRenderThread() { | |
| 76 if (render_thread_ && routing_id_ != MSG_ROUTING_NONE) | |
| 77 render_thread_->RemoveRoute(GetRoutingID()); | |
|
Ilya Sherman
2014/08/19 23:55:52
nit: Why do you call GetRoutingID() here, rather t
Mike West
2014/08/20 13:17:19
Done.
| |
| 78 render_thread_ = NULL; | |
| 79 routing_id_ = MSG_ROUTING_NONE; | |
| 80 } | |
| 81 | |
| 82 // ----------------------------------------------------------------------------- | |
| 83 // Handle messages from the browser. | |
| 84 | |
| 85 bool CredentialManagerDispatcher::OnMessageReceived( | |
| 86 const IPC::Message& message) { | |
| 87 bool handled = true; | |
| 88 IPC_BEGIN_MESSAGE_MAP(CredentialManagerDispatcher, message) | |
| 89 IPC_MESSAGE_HANDLER(CredentialManagerMsg_AcknowledgeFailedSignIn, | |
| 90 OnFailedSignInResponse) | |
|
Ilya Sherman
2014/08/19 23:55:52
nit: Why not have the method names match the IPC n
Mike West
2014/08/20 13:17:19
Done.
| |
| 91 IPC_MESSAGE_HANDLER(CredentialManagerMsg_AcknowledgeSignedIn, | |
| 92 OnSignedInResponse) | |
| 93 IPC_MESSAGE_HANDLER(CredentialManagerMsg_AcknowledgeSignedOut, | |
| 94 OnSignedOutResponse) | |
| 95 IPC_MESSAGE_HANDLER(CredentialManagerMsg_SendCredentials, OnSendCredentials) | |
| 96 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 97 IPC_END_MESSAGE_MAP() | |
| 98 return handled; | |
| 99 } | |
| 100 | |
| 101 void CredentialManagerDispatcher::OnFailedSignInResponse(int request_id) { | |
| 102 RespondToNotificationCallback(request_id, &failed_sign_in_callbacks_); | |
| 103 } | |
| 104 | |
| 105 void CredentialManagerDispatcher::OnSignedInResponse(int request_id) { | |
| 106 RespondToNotificationCallback(request_id, &signed_in_callbacks_); | |
| 107 } | |
| 108 | |
| 109 void CredentialManagerDispatcher::OnSignedOutResponse(int request_id) { | |
| 110 RespondToNotificationCallback(request_id, &signed_out_callbacks_); | |
| 111 } | |
| 112 | |
| 113 void CredentialManagerDispatcher::OnSendCredentials( | |
| 114 int request_id, | |
| 115 const CredentialInfo& info) { | |
| 116 RequestCallbacks* callbacks = request_callbacks_.Lookup(request_id); | |
| 117 DCHECK(callbacks); | |
| 118 // TODO(mkwst): Split into local/federated credentials. | |
| 119 blink::WebCredential credential(info.id, info.name, info.avatarURL); | |
| 120 callbacks->onSuccess(&credential); | |
| 121 request_callbacks_.Remove(request_id); | |
| 122 } | |
| 123 | |
| 124 // ----------------------------------------------------------------------------- | |
| 125 // Dispatch messages from the renderer to the browser. | |
| 126 | |
| 127 void CredentialManagerDispatcher::dispatchFailedSignIn( | |
| 128 const blink::WebCredential& credential, | |
| 129 blink::WebCredentialManager::NotificationCallbacks* callbacks) { | |
| 130 int request_id = failed_sign_in_callbacks_.Add(callbacks); | |
| 131 CredentialInfo info( | |
| 132 credential.id(), credential.name(), credential.avatarURL()); | |
| 133 if (render_thread_) | |
|
Ilya Sherman
2014/08/19 23:55:52
nit: Please add curly braces, since the body spans
Mike West
2014/08/20 13:17:19
Done.
| |
| 134 render_thread_->Send(new CredentialManagerHostMsg_NotifyFailedSignIn( | |
| 135 GetRoutingID(), request_id, info)); | |
| 136 } | |
| 137 | |
| 138 void CredentialManagerDispatcher::dispatchSignedIn( | |
| 139 const blink::WebCredential& credential, | |
| 140 blink::WebCredentialManager::NotificationCallbacks* callbacks) { | |
| 141 int request_id = signed_in_callbacks_.Add(callbacks); | |
| 142 CredentialInfo info( | |
| 143 credential.id(), credential.name(), credential.avatarURL()); | |
| 144 if (render_thread_) | |
| 145 render_thread_->Send(new CredentialManagerHostMsg_NotifySignedIn( | |
| 146 GetRoutingID(), request_id, info)); | |
| 147 } | |
| 148 | |
| 149 void CredentialManagerDispatcher::dispatchSignedOut( | |
| 150 NotificationCallbacks* callbacks) { | |
| 151 int request_id = signed_out_callbacks_.Add(callbacks); | |
| 152 if (render_thread_) | |
| 153 render_thread_->Send( | |
| 154 new CredentialManagerHostMsg_NotifySignedOut(GetRoutingID(), | |
| 155 request_id)); | |
| 156 } | |
| 157 | |
| 158 void CredentialManagerDispatcher::dispatchRequest( | |
| 159 bool zeroClickOnly, | |
| 160 const blink::WebVector<blink::WebURL>& federations, | |
| 161 RequestCallbacks* callbacks) { | |
| 162 int request_id = request_callbacks_.Add(callbacks); | |
| 163 std::vector<GURL> federationVector; | |
|
Ilya Sherman
2014/08/19 23:55:52
nit: hacker_case
Mike West
2014/08/20 13:17:19
Done.
| |
| 164 for (size_t i = 0; i < federations.size() && i < kMaxNumFederations; i++) | |
|
Ilya Sherman
2014/08/19 23:55:52
Optional nit: I'd use std::min rather than having
Ilya Sherman
2014/08/19 23:55:52
nit: ++i
Mike West
2014/08/20 13:17:19
Done.
Mike West
2014/08/20 13:17:19
An excellent idea!
| |
| 165 federationVector.push_back(federations[i]); | |
| 166 if (render_thread_) | |
| 167 render_thread_->Send(new CredentialManagerHostMsg_RequestCredentials( | |
| 168 GetRoutingID(), request_id, zeroClickOnly, federationVector)); | |
| 169 } | |
| 170 | |
| 171 } // namespace password_manager | |
| OLD | NEW |