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_client .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 "content/public/renderer/render_view.h" | |
| 11 #include "third_party/WebKit/public/platform/WebCredential.h" | |
| 12 #include "third_party/WebKit/public/platform/WebCredentialManagerError.h" | |
| 13 #include "third_party/WebKit/public/web/WebView.h" | |
| 14 | |
| 15 namespace password_manager { | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 template <typename T> | |
| 20 void ClearCallbacksMapWithErrors(T* callbacks_map) { | |
| 21 typename T::iterator iter(callbacks_map); | |
| 22 while (!iter.IsAtEnd()) { | |
| 23 blink::WebCredentialManagerError reason( | |
| 24 blink::WebCredentialManagerError::ErrorTypeUnknown, | |
| 25 "An unknown error occurred."); | |
| 26 iter.GetCurrentValue()->onError(&reason); | |
| 27 callbacks_map->Remove(iter.GetCurrentKey()); | |
| 28 iter.Advance(); | |
| 29 } | |
| 30 } | |
| 31 | |
| 32 } // namespace | |
| 33 | |
| 34 CredentialManagerClient::CredentialManagerClient() | |
| 35 : routing_id_(MSG_ROUTING_NONE) | |
| 36 , render_thread_(content::RenderThread::Get()) { | |
| 37 DCHECK(render_thread_); | |
| 38 } | |
| 39 | |
| 40 CredentialManagerClient::~CredentialManagerClient() { | |
| 41 DisconnectFromRenderThread(); | |
| 42 | |
| 43 ClearCallbacksMapWithErrors(&failed_sign_in_callbacks_); | |
| 44 ClearCallbacksMapWithErrors(&signed_in_callbacks_); | |
| 45 ClearCallbacksMapWithErrors(&signed_out_callbacks_); | |
| 46 ClearCallbacksMapWithErrors(&request_callbacks_); | |
| 47 } | |
| 48 | |
| 49 void CredentialManagerClient::OnRenderViewCreated( | |
| 50 content::RenderView* render_view) { | |
| 51 render_view->GetWebView()->setCredentialManagerClient(this); | |
| 52 } | |
| 53 | |
| 54 void CredentialManagerClient::OnRenderProcessShutdown() { | |
| 55 DisconnectFromRenderThread(); | |
| 56 } | |
| 57 | |
| 58 int CredentialManagerClient::GetRoutingID() { | |
| 59 if (render_thread_ && routing_id_ == MSG_ROUTING_NONE) { | |
| 60 routing_id_ = render_thread_->GenerateRoutingID(); | |
| 61 render_thread_->AddRoute(routing_id_, this); | |
| 62 } | |
| 63 return routing_id_; | |
| 64 } | |
| 65 | |
| 66 void CredentialManagerClient::DisconnectFromRenderThread() { | |
| 67 if (render_thread_ && routing_id_ != MSG_ROUTING_NONE) | |
| 68 render_thread_->RemoveRoute(routing_id_); | |
| 69 render_thread_ = NULL; | |
| 70 routing_id_ = MSG_ROUTING_NONE; | |
| 71 } | |
| 72 | |
| 73 // ----------------------------------------------------------------------------- | |
| 74 // Handle messages from the browser. | |
| 75 | |
| 76 bool CredentialManagerClient::OnMessageReceived( | |
| 77 const IPC::Message& message) { | |
| 78 bool handled = true; | |
| 79 IPC_BEGIN_MESSAGE_MAP(CredentialManagerClient, message) | |
| 80 IPC_MESSAGE_HANDLER(CredentialManagerMsg_AcknowledgeFailedSignIn, | |
| 81 OnAcknowledgeFailedSignIn) | |
| 82 IPC_MESSAGE_HANDLER(CredentialManagerMsg_AcknowledgeSignedIn, | |
| 83 OnAcknowledgeSignedIn) | |
| 84 IPC_MESSAGE_HANDLER(CredentialManagerMsg_AcknowledgeSignedOut, | |
| 85 OnAcknowledgeSignedOut) | |
| 86 IPC_MESSAGE_HANDLER(CredentialManagerMsg_SendCredential, OnSendCredential) | |
| 87 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 88 IPC_END_MESSAGE_MAP() | |
| 89 return handled; | |
| 90 } | |
| 91 | |
| 92 void CredentialManagerClient::OnAcknowledgeFailedSignIn(int request_id) { | |
| 93 RespondToNotificationCallback(request_id, &failed_sign_in_callbacks_); | |
| 94 } | |
| 95 | |
| 96 void CredentialManagerClient::OnAcknowledgeSignedIn(int request_id) { | |
| 97 RespondToNotificationCallback(request_id, &signed_in_callbacks_); | |
| 98 } | |
| 99 | |
| 100 void CredentialManagerClient::OnAcknowledgeSignedOut(int request_id) { | |
| 101 RespondToNotificationCallback(request_id, &signed_out_callbacks_); | |
| 102 } | |
| 103 | |
| 104 void CredentialManagerClient::OnSendCredential( | |
| 105 int request_id, | |
| 106 const CredentialInfo& info) { | |
| 107 RequestCallbacks* callbacks = request_callbacks_.Lookup(request_id); | |
| 108 DCHECK(callbacks); | |
| 109 // TODO(mkwst): Split into local/federated credentials. | |
| 110 blink::WebCredential credential(info.id, info.name, info.avatar); | |
| 111 callbacks->onSuccess(&credential); | |
| 112 request_callbacks_.Remove(request_id); | |
| 113 } | |
| 114 | |
| 115 // ----------------------------------------------------------------------------- | |
| 116 // Dispatch messages from the renderer to the browser. | |
| 117 | |
| 118 void CredentialManagerClient::dispatchFailedSignIn( | |
| 119 const blink::WebCredential& credential, | |
| 120 blink::WebCredentialManagerClient::NotificationCallbacks* callbacks) { | |
| 121 int request_id = failed_sign_in_callbacks_.Add(callbacks); | |
| 122 CredentialInfo info( | |
| 123 credential.id(), credential.name(), credential.avatarURL()); | |
| 124 if (render_thread_) { | |
| 125 render_thread_->Send(new CredentialManagerHostMsg_NotifyFailedSignIn( | |
| 126 GetRoutingID(), request_id, info)); | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 void CredentialManagerClient::dispatchSignedIn( | |
| 131 const blink::WebCredential& credential, | |
| 132 blink::WebCredentialManagerClient::NotificationCallbacks* callbacks) { | |
| 133 int request_id = signed_in_callbacks_.Add(callbacks); | |
| 134 CredentialInfo info( | |
| 135 credential.id(), credential.name(), credential.avatarURL()); | |
| 136 if (render_thread_) | |
| 137 render_thread_->Send(new CredentialManagerHostMsg_NotifySignedIn( | |
| 138 GetRoutingID(), request_id, info)); | |
| 139 } | |
| 140 | |
| 141 void CredentialManagerClient::dispatchSignedOut( | |
| 142 NotificationCallbacks* callbacks) { | |
| 143 int request_id = signed_out_callbacks_.Add(callbacks); | |
| 144 if (render_thread_) | |
| 145 render_thread_->Send( | |
| 146 new CredentialManagerHostMsg_NotifySignedOut(GetRoutingID(), | |
| 147 request_id)); | |
| 148 } | |
| 149 | |
| 150 void CredentialManagerClient::dispatchRequest( | |
| 151 bool zeroClickOnly, | |
| 152 const blink::WebVector<blink::WebURL>& federations, | |
| 153 RequestCallbacks* callbacks) { | |
| 154 int request_id = request_callbacks_.Add(callbacks); | |
| 155 std::vector<GURL> federation_vector; | |
| 156 for (size_t i = 0; i < std::min(federations.size(), kMaxFederations); ++i) | |
| 157 federation_vector.push_back(federations[i]); | |
| 158 if (render_thread_) | |
|
Ilya Sherman
2014/08/21 00:11:21
nit: Please add curly braces, because the body of
Mike West
2014/08/21 10:20:47
Done.
| |
| 159 render_thread_->Send(new CredentialManagerHostMsg_RequestCredential( | |
| 160 GetRoutingID(), request_id, zeroClickOnly, federation_vector)); | |
| 161 } | |
| 162 | |
| 163 void CredentialManagerClient::RespondToNotificationCallback( | |
| 164 int request_id, | |
| 165 CredentialManagerClient::NotificationCallbacksMap* map) { | |
| 166 blink::WebCredentialManagerClient::NotificationCallbacks* callbacks = | |
| 167 map->Lookup(request_id); | |
| 168 DCHECK(callbacks); | |
| 169 callbacks->onSuccess(); | |
| 170 map->Remove(request_id); | |
| 171 } | |
| 172 | |
| 173 | |
| 174 } // namespace password_manager | |
| OLD | NEW |