summaryrefslogtreecommitdiff
path: root/chromium/components/password_manager/content/renderer/credential_manager_client.cc
blob: 6a6d0b2eba4e8e19bfab6b6c4092193c4c7952f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// 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_client.h"

#include <stddef.h>

#include <memory>
#include <utility>

#include "base/bind.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "components/password_manager/core/common/credential_manager_types.h"
#include "content/public/common/associated_interface_provider.h"
#include "content/public/renderer/render_frame.h"
#include "content/public/renderer/render_view.h"
#include "third_party/WebKit/public/platform/WebCredential.h"
#include "third_party/WebKit/public/platform/WebCredentialManagerError.h"
#include "third_party/WebKit/public/platform/WebFederatedCredential.h"
#include "third_party/WebKit/public/platform/WebPasswordCredential.h"
#include "third_party/WebKit/public/web/WebView.h"

namespace password_manager {

namespace {

void WebCredentialToCredentialInfo(const blink::WebCredential& credential,
                                   CredentialInfo* out) {
  out->id = credential.Id().Utf16();
  out->name = credential.GetName().Utf16();
  out->icon = credential.GetIconURL();
  if (credential.IsPasswordCredential()) {
    out->type = CredentialType::CREDENTIAL_TYPE_PASSWORD;
    out->password = static_cast<const blink::WebPasswordCredential&>(credential)
                        .Password()
                        .Utf16();
  } else {
    DCHECK(credential.IsFederatedCredential());
    out->type = CredentialType::CREDENTIAL_TYPE_FEDERATED;
    out->federation =
        static_cast<const blink::WebFederatedCredential&>(credential)
            .Provider();
  }
}

std::unique_ptr<blink::WebCredential> CredentialInfoToWebCredential(
    const CredentialInfo& info) {
  switch (info.type) {
    case CredentialType::CREDENTIAL_TYPE_FEDERATED:
      return base::MakeUnique<blink::WebFederatedCredential>(
          blink::WebString::FromUTF16(info.id), info.federation,
          blink::WebString::FromUTF16(info.name), info.icon);
    case CredentialType::CREDENTIAL_TYPE_PASSWORD:
      return base::MakeUnique<blink::WebPasswordCredential>(
          blink::WebString::FromUTF16(info.id),
          blink::WebString::FromUTF16(info.password),
          blink::WebString::FromUTF16(info.name), info.icon);
    case CredentialType::CREDENTIAL_TYPE_EMPTY:
      return nullptr;
  }

  NOTREACHED();
  return nullptr;
}

CredentialMediationRequirement GetCredentialMediationRequirementFromBlink(
    blink::WebCredentialMediationRequirement mediation) {
  switch (mediation) {
    case blink::WebCredentialMediationRequirement::kSilent:
      return CredentialMediationRequirement::kSilent;
    case blink::WebCredentialMediationRequirement::kOptional:
      return CredentialMediationRequirement::kOptional;
    case blink::WebCredentialMediationRequirement::kRequired:
      return CredentialMediationRequirement::kRequired;
  }

  NOTREACHED();
  return CredentialMediationRequirement::kOptional;
}

blink::WebCredentialManagerError GetWebCredentialManagerErrorFromMojo(
    mojom::CredentialManagerError error) {
  switch (error) {
    case mojom::CredentialManagerError::DISABLED:
      return blink::WebCredentialManagerError::
          kWebCredentialManagerDisabledError;
    case mojom::CredentialManagerError::PENDINGREQUEST:
      return blink::WebCredentialManagerError::
          kWebCredentialManagerPendingRequestError;
    case mojom::CredentialManagerError::PASSWORDSTOREUNAVAILABLE:
      return blink::WebCredentialManagerError::
          kWebCredentialManagerPasswordStoreUnavailableError;
    case mojom::CredentialManagerError::UNKNOWN:
      return blink::WebCredentialManagerError::
          kWebCredentialManagerUnknownError;
    case mojom::CredentialManagerError::SUCCESS:
      NOTREACHED();
      break;
  }

  NOTREACHED();
  return blink::WebCredentialManagerError::kWebCredentialManagerUnknownError;
}

// Takes ownership of blink::WebCredentialManagerClient::NotificationCallbacks
// pointer. When the wrapper is destroyed, if |callbacks| is still alive
// its onError() will get called.
class NotificationCallbacksWrapper {
 public:
  explicit NotificationCallbacksWrapper(
      blink::WebCredentialManagerClient::NotificationCallbacks* callbacks);

  ~NotificationCallbacksWrapper();

  void NotifySuccess();

 private:
  std::unique_ptr<blink::WebCredentialManagerClient::NotificationCallbacks>
      callbacks_;

  DISALLOW_COPY_AND_ASSIGN(NotificationCallbacksWrapper);
};

NotificationCallbacksWrapper::NotificationCallbacksWrapper(
    blink::WebCredentialManagerClient::NotificationCallbacks* callbacks)
    : callbacks_(callbacks) {}

NotificationCallbacksWrapper::~NotificationCallbacksWrapper() {
  if (callbacks_)
    callbacks_->OnError(blink::kWebCredentialManagerUnknownError);
}

void NotificationCallbacksWrapper::NotifySuccess() {
  // Call onSuccess() and reset callbacks to avoid calling onError() in
  // destructor.
  if (callbacks_) {
    callbacks_->OnSuccess();
    callbacks_.reset();
  }
}

// Takes ownership of blink::WebCredentialManagerClient::RequestCallbacks
// pointer. When the wrapper is destroied, if |callbacks| is still alive
// its onError() will get called.
class RequestCallbacksWrapper {
 public:
  explicit RequestCallbacksWrapper(
      blink::WebCredentialManagerClient::RequestCallbacks* callbacks);

  ~RequestCallbacksWrapper();

  void NotifySuccess(const CredentialInfo& info);

  void NotifyError(mojom::CredentialManagerError error);

 private:
  std::unique_ptr<blink::WebCredentialManagerClient::RequestCallbacks>
      callbacks_;

  DISALLOW_COPY_AND_ASSIGN(RequestCallbacksWrapper);
};

RequestCallbacksWrapper::RequestCallbacksWrapper(
    blink::WebCredentialManagerClient::RequestCallbacks* callbacks)
    : callbacks_(callbacks) {}

RequestCallbacksWrapper::~RequestCallbacksWrapper() {
  if (callbacks_)
    callbacks_->OnError(blink::kWebCredentialManagerUnknownError);
}

void RequestCallbacksWrapper::NotifySuccess(const CredentialInfo& info) {
  // Call onSuccess() and reset callbacks to avoid calling onError() in
  // destructor.
  if (callbacks_) {
    callbacks_->OnSuccess(CredentialInfoToWebCredential(info));
    callbacks_.reset();
  }
}

void RequestCallbacksWrapper::NotifyError(mojom::CredentialManagerError error) {
  if (callbacks_) {
    callbacks_->OnError(GetWebCredentialManagerErrorFromMojo(error));
    callbacks_.reset();
  }
}

void RespondToNotificationCallback(
    NotificationCallbacksWrapper* callbacks_wrapper) {
  callbacks_wrapper->NotifySuccess();
}

void RespondToRequestCallback(RequestCallbacksWrapper* callbacks_wrapper,
                              mojom::CredentialManagerError error,
                              const base::Optional<CredentialInfo>& info) {
  if (error == mojom::CredentialManagerError::SUCCESS) {
    DCHECK(info);
    callbacks_wrapper->NotifySuccess(*info);
  } else {
    DCHECK(!info);
    callbacks_wrapper->NotifyError(error);
  }
}

}  // namespace

CredentialManagerClient::CredentialManagerClient(
    content::RenderView* render_view)
    : content::RenderViewObserver(render_view) {
  render_view->GetWebView()->SetCredentialManagerClient(this);
}

CredentialManagerClient::~CredentialManagerClient() {}

// -----------------------------------------------------------------------------
// Access mojo CredentialManagerService.

void CredentialManagerClient::DispatchStore(
    const blink::WebCredential& credential,
    blink::WebCredentialManagerClient::NotificationCallbacks* callbacks) {
  DCHECK(callbacks);
  ConnectToMojoCMIfNeeded();

  CredentialInfo info;
  WebCredentialToCredentialInfo(credential, &info);
  mojo_cm_service_->Store(
      info,
      base::Bind(&RespondToNotificationCallback,
                 base::Owned(new NotificationCallbacksWrapper(callbacks))));
}

void CredentialManagerClient::DispatchPreventSilentAccess(
    blink::WebCredentialManagerClient::NotificationCallbacks* callbacks) {
  DCHECK(callbacks);
  ConnectToMojoCMIfNeeded();

  mojo_cm_service_->PreventSilentAccess(
      base::Bind(&RespondToNotificationCallback,
                 base::Owned(new NotificationCallbacksWrapper(callbacks))));
}

void CredentialManagerClient::DispatchGet(
    blink::WebCredentialMediationRequirement mediation,
    bool include_passwords,
    const blink::WebVector<blink::WebURL>& federations,
    RequestCallbacks* callbacks) {
  DCHECK(callbacks);
  ConnectToMojoCMIfNeeded();

  std::vector<GURL> federation_vector;
  for (size_t i = 0; i < std::min(federations.size(), kMaxFederations); ++i)
    federation_vector.push_back(federations[i]);

  mojo_cm_service_->Get(
      GetCredentialMediationRequirementFromBlink(mediation), include_passwords,
      federation_vector,
      base::Bind(&RespondToRequestCallback,
                 base::Owned(new RequestCallbacksWrapper(callbacks))));
}

void CredentialManagerClient::ConnectToMojoCMIfNeeded() {
  if (mojo_cm_service_)
    return;

  content::RenderFrame* main_frame = render_view()->GetMainRenderFrame();
  main_frame->GetRemoteAssociatedInterfaces()->GetInterface(&mojo_cm_service_);

  // The remote end of the pipe will be forcibly closed by the browser side
  // after each main frame navigation. Set up an error handler to reset the
  // local end so that there will be an attempt at reestablishing the connection
  // at the next call to the API.
  mojo_cm_service_.set_connection_error_handler(base::Bind(
      &CredentialManagerClient::OnMojoConnectionError, base::Unretained(this)));
}

void CredentialManagerClient::OnMojoConnectionError() {
  mojo_cm_service_.reset();
}

void CredentialManagerClient::OnDestruct() {
  delete this;
}

}  // namespace password_manager