Index: components/password_manager/content/renderer/credential_manager_dispatcher.cc |
diff --git a/components/password_manager/content/renderer/credential_manager_dispatcher.cc b/components/password_manager/content/renderer/credential_manager_dispatcher.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..b4befafbfcb12415a9fb29f9ed654ea0f30478a4 |
--- /dev/null |
+++ b/components/password_manager/content/renderer/credential_manager_dispatcher.cc |
@@ -0,0 +1,132 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "components/password_manager/content/renderer/credential_manager_dispatcher.h" |
+ |
+#include "components/password_manager/content/common/credential_manager_messages.h" |
+#include "components/password_manager/content/common/credential_manager_types.h" |
+#include "content/public/renderer/render_thread.h" |
+#include "third_party/WebKit/public/platform/WebCredentialManagerError.h" |
+ |
+namespace password_manager { |
+ |
+namespace { |
+ |
+template<typename T> |
+void ClearCallbacksMapWithErrors(T* callbacks_map) { |
+ typename T::iterator iter(callbacks_map); |
+ while (!iter.IsAtEnd()) { |
+ blink::WebCredentialManagerError reason( |
+ blink::WebCredentialManagerError::ErrorTypeUnknown, |
+ "An unknown error occurred."); |
+ iter.GetCurrentValue()->onError(&reason); |
+ callbacks_map->Remove(iter.GetCurrentKey()); |
+ iter.Advance(); |
+ } |
+} |
+ |
+// Limit the size of the federations array that we pass to the browser to |
+// something reasonably sane. |
+const size_t kMaxNumFederations = 100; |
Ilya Sherman
2014/08/15 20:42:38
:)
|
+ |
+} // namespace |
+ |
+CredentialManagerDispatcher::CredentialManagerDispatcher() { |
+ routing_id_ = content::RenderThread::Get()->GenerateRoutingID(); |
+ content::RenderThread::Get()->AddRoute(routing_id_, this); |
+} |
+ |
+CredentialManagerDispatcher::~CredentialManagerDispatcher() { |
+ content::RenderThread::Get()->RemoveRoute(routing_id_); |
+ ClearCallbacksMapWithErrors(&failed_sign_in_callbacks_); |
+ ClearCallbacksMapWithErrors(&signed_in_callbacks_); |
+ ClearCallbacksMapWithErrors(&signed_out_callbacks_); |
+ ClearCallbacksMapWithErrors(&request_callbacks_); |
+} |
+ |
+// ----------------------------------------------------------------------------- |
+// Handle messages from the browser. |
+ |
+bool CredentialManagerDispatcher::OnMessageReceived( |
+ const IPC::Message& message) { |
+ bool handled = true; |
+ IPC_BEGIN_MESSAGE_MAP(CredentialManagerDispatcher, message) |
+ IPC_MESSAGE_HANDLER(CredentialManagerMsg_AcknowledgeFailedSignIn, |
+ OnFailedSignInResponse) |
+ IPC_MESSAGE_HANDLER(CredentialManagerMsg_AcknowledgeSignedIn, |
+ OnSignedInResponse) |
+ IPC_MESSAGE_HANDLER(CredentialManagerMsg_AcknowledgeSignedOut, |
+ OnSignedOutResponse) |
+ IPC_MESSAGE_HANDLER(CredentialManagerMsg_RequestResponse, OnRequestResponse) |
+ IPC_MESSAGE_UNHANDLED(handled = false) |
+ IPC_END_MESSAGE_MAP() |
+ return handled; |
+} |
+ |
+void CredentialManagerDispatcher::OnFailedSignInResponse( |
+ int request_id) { |
+ NotificationCallbacks* callbacks = |
+ failed_sign_in_callbacks_.Lookup(request_id); |
+ DCHECK(callbacks); |
+ callbacks->onSuccess(); |
+ failed_sign_in_callbacks_.Remove(request_id); |
+} |
+ |
+void CredentialManagerDispatcher::OnSignedInResponse( |
+ int request_id) { |
+ NotificationCallbacks* callbacks = |
+ signed_in_callbacks_.Lookup(request_id); |
+ DCHECK(callbacks); |
+ callbacks->onSuccess(); |
+ signed_in_callbacks_.Remove(request_id); |
+} |
+ |
+void CredentialManagerDispatcher::OnSignedOutResponse( |
+ int request_id) { |
+ NotificationCallbacks* callbacks = |
+ signed_out_callbacks_.Lookup(request_id); |
+ DCHECK(callbacks); |
+ callbacks->onSuccess(); |
+ signed_out_callbacks_.Remove(request_id); |
Ilya Sherman
2014/08/15 20:42:38
Seems like you have a bunch of very similar method
Mike West
2014/08/19 08:37:15
Extracted this out to RespondToNotificationCallbac
|
+} |
+ |
+void CredentialManagerDispatcher::OnRequestResponse( |
+ int request_id, const CredentialManagerCredentialInfo& info) { |
+ RequestCallbacks* callbacks = request_callbacks_.Lookup(request_id); |
+ DCHECK(callbacks); |
+ // TODO(mkwst): Split into local/federated credentials. |
+ blink::WebCredential credential(info.id, info.name, info.avatarURL); |
+ callbacks->onSuccess(&credential); |
+ request_callbacks_.Remove(request_id); |
+} |
+ |
+// ----------------------------------------------------------------------------- |
+// Dispatch messages from the renderer to the browser. |
+ |
+void CredentialManagerDispatcher::dispatchFailedSignIn(const blink::WebCredential& credential, NotificationCallbacks* callbacks) { |
+ int request_id = failed_sign_in_callbacks_.Add(callbacks); |
+ CredentialManagerCredentialInfo info(credential.id(), credential.name(), credential.avatarURL()); |
+ content::RenderThread::Get()->Send(new CredentialManagerHostMsg_NotifyFailedSignIn(routing_id_, request_id, info)); |
+} |
+ |
+void CredentialManagerDispatcher::dispatchSignedIn(const blink::WebCredential& credential, NotificationCallbacks* callbacks) { |
+ int request_id = signed_in_callbacks_.Add(callbacks); |
+ CredentialManagerCredentialInfo info(credential.id(), credential.name(), credential.avatarURL()); |
+ content::RenderThread::Get()->Send(new CredentialManagerHostMsg_NotifySignedIn(routing_id_, request_id, info)); |
+} |
+ |
+void CredentialManagerDispatcher::dispatchSignedOut(NotificationCallbacks* callbacks) { |
+ int request_id = signed_out_callbacks_.Add(callbacks); |
+ content::RenderThread::Get()->Send(new CredentialManagerHostMsg_NotifySignedOut(routing_id_, request_id)); |
+} |
+ |
+void CredentialManagerDispatcher::dispatchRequest(bool zeroClickOnly, const blink::WebVector<blink::WebURL>& federations, RequestCallbacks* callbacks) { |
+ int request_id = request_callbacks_.Add(callbacks); |
+ std::vector<GURL> federationVector; |
+ for (size_t i = 0; i < federations.size() && i < kMaxNumFederations; i++) |
+ federationVector.push_back(federations[i]); |
+ content::RenderThread::Get()->Send(new CredentialManagerHostMsg_Request(routing_id_, request_id, zeroClickOnly, federationVector)); |
+} |
+ |
+} // namespace password_manager |