e6941583c999ac3428664b53f6ac5063c5f128d5
[platform/framework/web/crosswalk.git] / src / components / password_manager / content / renderer / credential_manager_client.cc
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_view.h"
10 #include "third_party/WebKit/public/platform/WebCredential.h"
11 #include "third_party/WebKit/public/platform/WebCredentialManagerError.h"
12 #include "third_party/WebKit/public/platform/WebFederatedCredential.h"
13 #include "third_party/WebKit/public/platform/WebLocalCredential.h"
14 #include "third_party/WebKit/public/web/WebView.h"
15
16 namespace password_manager {
17
18 namespace {
19
20 template <typename T>
21 void ClearCallbacksMapWithErrors(T* callbacks_map) {
22   typename T::iterator iter(callbacks_map);
23   while (!iter.IsAtEnd()) {
24     blink::WebCredentialManagerError reason(
25         blink::WebCredentialManagerError::ErrorTypeUnknown);
26     iter.GetCurrentValue()->onError(&reason);
27     callbacks_map->Remove(iter.GetCurrentKey());
28     iter.Advance();
29   }
30 }
31
32 }  // namespace
33
34 CredentialManagerClient::CredentialManagerClient(
35     content::RenderView* render_view)
36     : content::RenderViewObserver(render_view) {
37   render_view->GetWebView()->setCredentialManagerClient(this);
38 }
39
40 CredentialManagerClient::~CredentialManagerClient() {
41   ClearCallbacksMapWithErrors(&failed_sign_in_callbacks_);
42   ClearCallbacksMapWithErrors(&signed_in_callbacks_);
43   ClearCallbacksMapWithErrors(&signed_out_callbacks_);
44   ClearCallbacksMapWithErrors(&request_callbacks_);
45 }
46
47 // -----------------------------------------------------------------------------
48 // Handle messages from the browser.
49
50 bool CredentialManagerClient::OnMessageReceived(const IPC::Message& message) {
51   bool handled = true;
52   IPC_BEGIN_MESSAGE_MAP(CredentialManagerClient, message)
53     IPC_MESSAGE_HANDLER(CredentialManagerMsg_AcknowledgeFailedSignIn,
54                         OnAcknowledgeFailedSignIn)
55     IPC_MESSAGE_HANDLER(CredentialManagerMsg_AcknowledgeSignedIn,
56                         OnAcknowledgeSignedIn)
57     IPC_MESSAGE_HANDLER(CredentialManagerMsg_AcknowledgeSignedOut,
58                         OnAcknowledgeSignedOut)
59     IPC_MESSAGE_HANDLER(CredentialManagerMsg_SendCredential, OnSendCredential)
60     IPC_MESSAGE_HANDLER(CredentialManagerMsg_RejectCredentialRequest,
61                         OnRejectCredentialRequest)
62     IPC_MESSAGE_UNHANDLED(handled = false)
63   IPC_END_MESSAGE_MAP()
64   return handled;
65 }
66
67 void CredentialManagerClient::OnAcknowledgeFailedSignIn(int request_id) {
68   RespondToNotificationCallback(request_id, &failed_sign_in_callbacks_);
69 }
70
71 void CredentialManagerClient::OnAcknowledgeSignedIn(int request_id) {
72   RespondToNotificationCallback(request_id, &signed_in_callbacks_);
73 }
74
75 void CredentialManagerClient::OnAcknowledgeSignedOut(int request_id) {
76   RespondToNotificationCallback(request_id, &signed_out_callbacks_);
77 }
78
79 void CredentialManagerClient::OnSendCredential(int request_id,
80                                                const CredentialInfo& info) {
81   RequestCallbacks* callbacks = request_callbacks_.Lookup(request_id);
82   DCHECK(callbacks);
83   scoped_ptr<blink::WebCredential> credential = nullptr;
84   switch (info.type) {
85   case CREDENTIAL_TYPE_FEDERATED:
86     credential.reset(new blink::WebFederatedCredential(
87         info.id, info.name, info.avatar, info.federation));
88     break;
89   case CREDENTIAL_TYPE_LOCAL:
90     credential.reset(new blink::WebLocalCredential(info.id, info.name,
91                                                    info.avatar, info.password));
92     break;
93   case CREDENTIAL_TYPE_EMPTY:
94     // Intentionally empty; we'll send nullptr to the onSuccess call below.
95     break;
96   }
97   callbacks->onSuccess(credential.get());
98   request_callbacks_.Remove(request_id);
99 }
100
101 void CredentialManagerClient::OnRejectCredentialRequest(
102     int request_id,
103     blink::WebCredentialManagerError::ErrorType error_type) {
104   RequestCallbacks* callbacks = request_callbacks_.Lookup(request_id);
105   DCHECK(callbacks);
106   scoped_ptr<blink::WebCredentialManagerError> error(
107       new blink::WebCredentialManagerError(error_type));
108   callbacks->onError(error.get());
109   request_callbacks_.Remove(request_id);
110 }
111
112 // -----------------------------------------------------------------------------
113 // Dispatch messages from the renderer to the browser.
114
115 void CredentialManagerClient::dispatchFailedSignIn(
116     const blink::WebCredential& credential,
117     blink::WebCredentialManagerClient::NotificationCallbacks* callbacks) {
118   int request_id = failed_sign_in_callbacks_.Add(callbacks);
119   CredentialInfo info(credential);
120   Send(new CredentialManagerHostMsg_NotifyFailedSignIn(
121       routing_id(), request_id, info));
122 }
123
124 void CredentialManagerClient::dispatchSignedIn(
125     const blink::WebCredential& credential,
126     blink::WebCredentialManagerClient::NotificationCallbacks* callbacks) {
127   int request_id = signed_in_callbacks_.Add(callbacks);
128   CredentialInfo info(credential);
129   Send(new CredentialManagerHostMsg_NotifySignedIn(
130       routing_id(), request_id, info));
131 }
132
133 void CredentialManagerClient::dispatchSignedOut(
134     NotificationCallbacks* callbacks) {
135   int request_id = signed_out_callbacks_.Add(callbacks);
136   Send(new CredentialManagerHostMsg_NotifySignedOut(routing_id(), request_id));
137 }
138
139 void CredentialManagerClient::dispatchRequest(
140     bool zeroClickOnly,
141     const blink::WebVector<blink::WebURL>& federations,
142     RequestCallbacks* callbacks) {
143   int request_id = request_callbacks_.Add(callbacks);
144   std::vector<GURL> federation_vector;
145   for (size_t i = 0; i < std::min(federations.size(), kMaxFederations); ++i)
146     federation_vector.push_back(federations[i]);
147   Send(new CredentialManagerHostMsg_RequestCredential(
148       routing_id(), request_id, zeroClickOnly, federation_vector));
149 }
150
151 void CredentialManagerClient::RespondToNotificationCallback(
152     int request_id,
153     CredentialManagerClient::NotificationCallbacksMap* map) {
154   blink::WebCredentialManagerClient::NotificationCallbacks* callbacks =
155       map->Lookup(request_id);
156   DCHECK(callbacks);
157   callbacks->onSuccess();
158   map->Remove(request_id);
159 }
160
161 }  // namespace password_manager