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."); | |
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. | |
31 const size_t kMaxNumFederations = 100; | |
Ilya Sherman
2014/08/15 20:42:38
:)
| |
32 | |
33 } // namespace | |
34 | |
35 CredentialManagerDispatcher::CredentialManagerDispatcher() { | |
36 routing_id_ = content::RenderThread::Get()->GenerateRoutingID(); | |
37 content::RenderThread::Get()->AddRoute(routing_id_, this); | |
38 } | |
39 | |
40 CredentialManagerDispatcher::~CredentialManagerDispatcher() { | |
41 content::RenderThread::Get()->RemoveRoute(routing_id_); | |
42 ClearCallbacksMapWithErrors(&failed_sign_in_callbacks_); | |
43 ClearCallbacksMapWithErrors(&signed_in_callbacks_); | |
44 ClearCallbacksMapWithErrors(&signed_out_callbacks_); | |
45 ClearCallbacksMapWithErrors(&request_callbacks_); | |
46 } | |
47 | |
48 // ----------------------------------------------------------------------------- | |
49 // Handle messages from the browser. | |
50 | |
51 bool CredentialManagerDispatcher::OnMessageReceived( | |
52 const IPC::Message& message) { | |
53 bool handled = true; | |
54 IPC_BEGIN_MESSAGE_MAP(CredentialManagerDispatcher, message) | |
55 IPC_MESSAGE_HANDLER(CredentialManagerMsg_AcknowledgeFailedSignIn, | |
56 OnFailedSignInResponse) | |
57 IPC_MESSAGE_HANDLER(CredentialManagerMsg_AcknowledgeSignedIn, | |
58 OnSignedInResponse) | |
59 IPC_MESSAGE_HANDLER(CredentialManagerMsg_AcknowledgeSignedOut, | |
60 OnSignedOutResponse) | |
61 IPC_MESSAGE_HANDLER(CredentialManagerMsg_RequestResponse, OnRequestResponse) | |
62 IPC_MESSAGE_UNHANDLED(handled = false) | |
63 IPC_END_MESSAGE_MAP() | |
64 return handled; | |
65 } | |
66 | |
67 void CredentialManagerDispatcher::OnFailedSignInResponse( | |
68 int request_id) { | |
69 NotificationCallbacks* callbacks = | |
70 failed_sign_in_callbacks_.Lookup(request_id); | |
71 DCHECK(callbacks); | |
72 callbacks->onSuccess(); | |
73 failed_sign_in_callbacks_.Remove(request_id); | |
74 } | |
75 | |
76 void CredentialManagerDispatcher::OnSignedInResponse( | |
77 int request_id) { | |
78 NotificationCallbacks* callbacks = | |
79 signed_in_callbacks_.Lookup(request_id); | |
80 DCHECK(callbacks); | |
81 callbacks->onSuccess(); | |
82 signed_in_callbacks_.Remove(request_id); | |
83 } | |
84 | |
85 void CredentialManagerDispatcher::OnSignedOutResponse( | |
86 int request_id) { | |
87 NotificationCallbacks* callbacks = | |
88 signed_out_callbacks_.Lookup(request_id); | |
89 DCHECK(callbacks); | |
90 callbacks->onSuccess(); | |
91 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
| |
92 } | |
93 | |
94 void CredentialManagerDispatcher::OnRequestResponse( | |
95 int request_id, const CredentialManagerCredentialInfo& info) { | |
96 RequestCallbacks* callbacks = request_callbacks_.Lookup(request_id); | |
97 DCHECK(callbacks); | |
98 // TODO(mkwst): Split into local/federated credentials. | |
99 blink::WebCredential credential(info.id, info.name, info.avatarURL); | |
100 callbacks->onSuccess(&credential); | |
101 request_callbacks_.Remove(request_id); | |
102 } | |
103 | |
104 // ----------------------------------------------------------------------------- | |
105 // Dispatch messages from the renderer to the browser. | |
106 | |
107 void CredentialManagerDispatcher::dispatchFailedSignIn(const blink::WebCredentia l& credential, NotificationCallbacks* callbacks) { | |
108 int request_id = failed_sign_in_callbacks_.Add(callbacks); | |
109 CredentialManagerCredentialInfo info(credential.id(), credential.name(), crede ntial.avatarURL()); | |
110 content::RenderThread::Get()->Send(new CredentialManagerHostMsg_NotifyFailedSi gnIn(routing_id_, request_id, info)); | |
111 } | |
112 | |
113 void CredentialManagerDispatcher::dispatchSignedIn(const blink::WebCredential& c redential, NotificationCallbacks* callbacks) { | |
114 int request_id = signed_in_callbacks_.Add(callbacks); | |
115 CredentialManagerCredentialInfo info(credential.id(), credential.name(), crede ntial.avatarURL()); | |
116 content::RenderThread::Get()->Send(new CredentialManagerHostMsg_NotifySignedIn (routing_id_, request_id, info)); | |
117 } | |
118 | |
119 void CredentialManagerDispatcher::dispatchSignedOut(NotificationCallbacks* callb acks) { | |
120 int request_id = signed_out_callbacks_.Add(callbacks); | |
121 content::RenderThread::Get()->Send(new CredentialManagerHostMsg_NotifySignedOu t(routing_id_, request_id)); | |
122 } | |
123 | |
124 void CredentialManagerDispatcher::dispatchRequest(bool zeroClickOnly, const blin k::WebVector<blink::WebURL>& federations, RequestCallbacks* callbacks) { | |
125 int request_id = request_callbacks_.Add(callbacks); | |
126 std::vector<GURL> federationVector; | |
127 for (size_t i = 0; i < federations.size() && i < kMaxNumFederations; i++) | |
128 federationVector.push_back(federations[i]); | |
129 content::RenderThread::Get()->Send(new CredentialManagerHostMsg_Request(routin g_id_, request_id, zeroClickOnly, federationVector)); | |
130 } | |
131 | |
132 } // namespace password_manager | |
OLD | NEW |