File: digital_identity_provider_desktop.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (374 lines) | stat: -rw-r--r-- 15,661 bytes parent folder | download | duplicates (3)
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/digital_credentials/digital_identity_provider_desktop.h"

#include <memory>
#include <variant>

#include "base/containers/span.h"
#include "base/values.h"
#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/digital_credentials/digital_identity_low_risk_origins.h"
#include "chrome/browser/net/system_network_context_manager.h"
#include "chrome/browser/ui/views/accessibility/theme_tracking_non_accessible_image_view.h"
#include "chrome/browser/ui/views/digital_credentials/digital_identity_bluetooth_manual_dialog_controller.h"
#include "chrome/browser/ui/views/digital_credentials/digital_identity_multi_step_dialog.h"
#include "chrome/browser/ui/views/digital_credentials/digital_identity_safety_interstitial_controller_desktop.h"
#include "chrome/grit/browser_resources.h"
#include "chrome/grit/generated_resources.h"
#include "components/constrained_window/constrained_window_views.h"
#include "components/qr_code_generator/bitmap_generator.h"
#include "content/public/browser/cross_device_request_info.h"
#include "content/public/browser/digital_credentials_cross_device.h"
#include "content/public/browser/digital_identity_provider.h"
#include "content/public/browser/web_contents.h"
#include "crypto/random.h"
#include "device/fido/cable/v2_constants.h"
#include "device/fido/cable/v2_handshake.h"
#include "third_party/abseil-cpp/absl/functional/overload.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/models/dialog_model.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/bubble/bubble_dialog_model_host.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/theme_tracking_animated_image_view.h"
#include "ui/views/layout/box_layout_view.h"
#include "ui/views/widget/widget.h"

namespace {

// Smaller than DistanceMetric::DISTANCE_MODAL_DIALOG_PREFERRED_WIDTH.
constexpr int kQrCodeSize = 240;
constexpr int kQrCodeMargin = 20;

using DigitalIdentityInterstitialAbortCallback =
    content::DigitalIdentityProvider::DigitalIdentityInterstitialAbortCallback;
using RequestStatusForMetrics =
    content::DigitalIdentityProvider::RequestStatusForMetrics;
using content::digital_credentials::cross_device::Error;
using content::digital_credentials::cross_device::Event;
using content::digital_credentials::cross_device::ProtocolError;
using content::digital_credentials::cross_device::RemoteError;
using content::digital_credentials::cross_device::RequestInfo;
using content::digital_credentials::cross_device::Response;
using content::digital_credentials::cross_device::SystemError;
using content::digital_credentials::cross_device::SystemEvent;
using content::digital_credentials::cross_device::Transaction;

void RunDigitalIdentityCallback(
    std::unique_ptr<DigitalIdentitySafetyInterstitialControllerDesktop>
        controller,
    content::DigitalIdentityProvider::DigitalIdentityInterstitialCallback
        callback,
    content::DigitalIdentityProvider::RequestStatusForMetrics
        status_for_metrics) {
  std::move(callback).Run(status_for_metrics);
}

std::unique_ptr<views::View> MakeQrCodeImageView(const std::string& qr_url) {
  auto qr_code = qr_code_generator::GenerateImage(
      base::as_byte_span(qr_url), qr_code_generator::ModuleStyle::kCircles,
      qr_code_generator::LocatorStyle::kRounded,
      qr_code_generator::CenterImage::kNoCenterImage,
      qr_code_generator::QuietZone::kIncluded);

  // Success is guaranteed, because `qr_url`'s size is bounded and smaller
  // than QR code limits.
  CHECK(qr_code.has_value());
  auto image_view = std::make_unique<views::ImageView>(
      ui::ImageModel::FromImageSkia(qr_code.value()));
  image_view->GetViewAccessibility().SetName(
      l10n_util::GetStringUTF16(IDS_WEB_DIGITAL_CREDENTIALS_QR_CODE_ALT_TEXT));
  image_view->SetImageSize(gfx::Size(kQrCodeSize, kQrCodeSize));
  image_view->SetCornerRadius(
      10);  // Set radius to match that used in the QR-Code Locator.
  image_view->SetBorder(views::CreateEmptyBorder(gfx::Insets(kQrCodeMargin)));
  return image_view;
}

device::cablev2::CredentialRequestType
CrossDeviceRequestTypeToCredentialRequestType(
    RequestInfo::RequestType request_type) {
  switch (request_type) {
    case RequestInfo::RequestType::kGet:
      return device::cablev2::CredentialRequestType::kPresentation;
    case RequestInfo::RequestType::kCreate:
      return device::cablev2::CredentialRequestType::kIssuance;
  }
}

}  // anonymous namespace

DigitalIdentityProviderDesktop::DigitalIdentityProviderDesktop() = default;

DigitalIdentityProviderDesktop::~DigitalIdentityProviderDesktop() = default;

bool DigitalIdentityProviderDesktop::IsLastCommittedOriginLowRisk(
    content::RenderFrameHost& render_frame_host) const {
  return digital_credentials::IsLastCommittedOriginLowRisk(render_frame_host);
}

DigitalIdentityInterstitialAbortCallback
DigitalIdentityProviderDesktop::ShowDigitalIdentityInterstitial(
    content::WebContents& web_contents,
    const url::Origin& origin,
    content::DigitalIdentityInterstitialType interstitial_type,
    DigitalIdentityInterstitialCallback callback) {
  auto controller =
      std::make_unique<DigitalIdentitySafetyInterstitialControllerDesktop>();
  // Callback takes ownership of |controller|.
  return controller->ShowInterstitial(
      web_contents, origin, interstitial_type,
      base::BindOnce(&RunDigitalIdentityCallback, std::move(controller),
                     std::move(callback)));
}

void DigitalIdentityProviderDesktop::Get(content::WebContents* web_contents,
                                         const url::Origin& rp_origin,
                                         base::ValueView request,
                                         DigitalIdentityCallback callback) {
  Transact(web_contents, RequestInfo::RequestType::kGet, rp_origin, request,
           std::move(callback));
}

void DigitalIdentityProviderDesktop::Create(content::WebContents* web_contents,
                                            const url::Origin& rp_origin,
                                            base::ValueView request,
                                            DigitalIdentityCallback callback) {
  Transact(web_contents, RequestInfo::RequestType::kCreate, rp_origin, request,
           std::move(callback));
}

void DigitalIdentityProviderDesktop::Transact(
    content::WebContents* web_contents,
    RequestInfo::RequestType request_type,
    const url::Origin& rp_origin,
    base::ValueView request,
    DigitalIdentityCallback callback) {
  web_contents_ = web_contents->GetWeakPtr();
  rp_origin_ = rp_origin;
  callback_ = std::move(callback);

  RequestInfo request_info{request_type, rp_origin, request.ToValue()};

  std::array<uint8_t, device::cablev2::kQRKeySize> qr_generator_key;
  crypto::RandBytes(qr_generator_key);

  std::string qr_url = device::cablev2::qr::Encode(
      qr_generator_key,
      CrossDeviceRequestTypeToCredentialRequestType(request_type));

  transaction_ = Transaction::New(
      std::move(request_info), qr_generator_key, base::BindRepeating([]() {
        return SystemNetworkContextManager::GetInstance()->GetContext();
      }),
      base::BindRepeating(&DigitalIdentityProviderDesktop::OnEvent,
                          weak_ptr_factory_.GetWeakPtr(), std::move(qr_url)),
      base::BindOnce(&DigitalIdentityProviderDesktop::OnFinished,
                     weak_ptr_factory_.GetWeakPtr()));
}

void DigitalIdentityProviderDesktop::OnEvent(const std::string& qr_url,
                                             Event event) {
  std::visit(absl::Overload{
                 [this, qr_url](SystemEvent event) {
                   switch (event) {
                     case SystemEvent::kBluetoothNotPowered:
                       ShowBluetoothManualTurnOnDialog();
                       break;
                     case SystemEvent::kNeedPermission:
                       // The user is being asked for Bluetooth permission by
                       // the system. Nothing for Chrome UI to do.
                       break;
                     case SystemEvent::kReady:
                       bluetooth_manual_dialog_controller_.reset();
                       ShowQrCodeDialog(qr_url);
                       break;
                   }
                 },
                 [this](device::cablev2::Event event) { OnCableEvent(event); },
             },
             event);
}

void DigitalIdentityProviderDesktop::OnCableEvent(
    device::cablev2::Event event) {
  switch (event) {
    case device::cablev2::Event::kPhoneConnected:
    case device::cablev2::Event::kBLEAdvertReceived:
      ShowConnectingToPhoneDialog();
      if (!cable_connecting_dialog_timer_.IsRunning()) {
        cable_connecting_dialog_timer_.Start(
            FROM_HERE, base::Milliseconds(2500),
            base::BindOnce(
                &DigitalIdentityProviderDesktop::OnCableConnectingTimerComplete,
                weak_ptr_factory_.GetWeakPtr()));
      }
      break;
    case device::cablev2::Event::kReady:
      // If we are ready before the timer fires, don't show the next dialog
      // directly to make sure the "connecting to phone" dialog is visible for
      // long enough time to avoid flashing the UI. Otherwise, show the next
      // dialog directly.
      if (cable_connecting_dialog_timer_.IsRunning()) {
        cable_connecting_ready_to_advance_ = true;
      } else {
        ShowContinueStepsOnThePhoneDialog();
      }
      break;
  }
}

void DigitalIdentityProviderDesktop::OnFinished(
    base::expected<Response, Error> result) {
  if (result.has_value()) {
    std::move(callback_).Run(std::move(result.value().value()));
    return;
  }

  std::visit(
      absl::Overload{
          [this](SystemError error) {
            EndRequestWithError(RequestStatusForMetrics::kErrorOther);
          },
          [this](ProtocolError error) {
            EndRequestWithError(RequestStatusForMetrics::kErrorOther);
          },
          [this](RemoteError error) {
            switch (error) {
              case RemoteError::kNoCredential:
                EndRequestWithError(
                    RequestStatusForMetrics::kErrorNoCredential);
                break;
              case RemoteError::kUserCanceled:
                EndRequestWithError(
                    RequestStatusForMetrics::kErrorUserDeclined);
                break;
              case RemoteError::kDeviceAborted:
                EndRequestWithError(RequestStatusForMetrics::kErrorAborted);
                break;
              case RemoteError::kOther:
                EndRequestWithError(RequestStatusForMetrics::kErrorOther);
                break;
            }
          }},
      result.error());
}

DigitalIdentityMultiStepDialog*
DigitalIdentityProviderDesktop::EnsureDialogCreated() {
  if (!dialog_) {
    dialog_ = std::make_unique<DigitalIdentityMultiStepDialog>(web_contents_);
  }
  return dialog_.get();
}

void DigitalIdentityProviderDesktop::ShowQrCodeDialog(
    const std::string& qr_url) {
  std::u16string dialog_title =
      l10n_util::GetStringUTF16(IDS_WEB_DIGITAL_CREDENTIALS_QR_TITLE);
  std::u16string dialog_body =
      l10n_util::GetStringUTF16(IDS_WEB_DIGITAL_CREDENTIALS_QR_BODY);
  EnsureDialogCreated()->TryShow(
      /*accept_button=*/std::nullopt, base::OnceClosure(),
      /*cancel_button=*/
      ui::DialogModel::Button::Params()
          .SetLabel(l10n_util::GetStringUTF16(
              IDS_WEB_DIGITAL_CREDENTIALS_FLOW_CANCEL_BUTTON_TEXT))
          .SetStyle(ui::ButtonStyle::kDefault),
      base::BindOnce(&DigitalIdentityProviderDesktop::OnCanceled,
                     weak_ptr_factory_.GetWeakPtr()),
      dialog_title, dialog_body, MakeQrCodeImageView(qr_url),
      /*show_progress_bar=*/false);
}

void DigitalIdentityProviderDesktop::ShowBluetoothManualTurnOnDialog() {
  bluetooth_manual_dialog_controller_ =
      std::make_unique<DigitalIdentityBluetoothManualDialogController>(
          EnsureDialogCreated());
  bluetooth_manual_dialog_controller_->Show(
      base::BindOnce(
          &DigitalIdentityProviderDesktop::OnUserRequestedBluetoothPowerOn,
          weak_ptr_factory_.GetWeakPtr()),
      base::BindOnce(&DigitalIdentityProviderDesktop::OnCanceled,
                     weak_ptr_factory_.GetWeakPtr()));
}

void DigitalIdentityProviderDesktop::OnUserRequestedBluetoothPowerOn() {
  transaction_->PowerBluetoothAdapter();
}

void DigitalIdentityProviderDesktop::ShowConnectingToPhoneDialog() {
  // Ensure the dialog is created to have access to GetBackgroundColor().
  EnsureDialogCreated();
  std::u16string title_text = l10n_util::GetStringUTF16(
      IDS_WEB_DIGITAL_CREDENTIALS_CABLEV2_CONNECTING_TITLE);
  auto illustration = std::make_unique<views::ThemeTrackingAnimatedImageView>(
      IDR_WEBAUTHN_HYBRID_CONNECTING_LIGHT, IDR_WEBAUTHN_HYBRID_CONNECTING_DARK,
      base::BindRepeating(&DigitalIdentityMultiStepDialog::GetBackgroundColor,
                          base::Unretained(dialog_.get())));

  EnsureDialogCreated()->TryShow(
      /*accept_button=*/std::nullopt, base::OnceClosure(),
      /*cancel_button=*/
      ui::DialogModel::Button::Params()
          .SetLabel(l10n_util::GetStringUTF16(
              IDS_WEB_DIGITAL_CREDENTIALS_FLOW_CANCEL_BUTTON_TEXT))
          .SetStyle(ui::ButtonStyle::kDefault),
      base::BindOnce(&DigitalIdentityProviderDesktop::OnCanceled,
                     weak_ptr_factory_.GetWeakPtr()),
      /*dialog_title=*/u"", /*dialog_body=*/u"",
      DigitalIdentityMultiStepDialog::CreateHeaderView(
          std::move(title_text), /*body_text=*/u"", std::move(illustration)),
      /*show_progress_bar=*/true);
}

void DigitalIdentityProviderDesktop::ShowContinueStepsOnThePhoneDialog() {
  // Ensure the dialog is created to have access to GetBackgroundColor().
  EnsureDialogCreated();
  std::u16string title_text = l10n_util::GetStringUTF16(
      IDS_WEB_DIGITAL_CREDENTIALS_CABLEV2_CONNECTED_TITLE);
  auto illustration = std::make_unique<ThemeTrackingNonAccessibleImageView>(
      ui::ImageModel::FromVectorIcon(kPasskeyPhoneIcon),
      ui::ImageModel::FromVectorIcon(kPasskeyPhoneDarkIcon),
      base::BindRepeating(&DigitalIdentityMultiStepDialog::GetBackgroundColor,
                          base::Unretained(dialog_.get())));

  EnsureDialogCreated()->TryShow(
      /*accept_button=*/std::nullopt, base::OnceClosure(),
      /*cancel_button=*/
      ui::DialogModel::Button::Params()
          .SetLabel(l10n_util::GetStringUTF16(
              IDS_WEB_DIGITAL_CREDENTIALS_FLOW_CANCEL_BUTTON_TEXT))
          .SetStyle(ui::ButtonStyle::kDefault),
      base::BindOnce(&DigitalIdentityProviderDesktop::OnCanceled,
                     weak_ptr_factory_.GetWeakPtr()),
      /*dialog_title=*/u"", /*dialog_body=*/u"",
      DigitalIdentityMultiStepDialog::CreateHeaderView(
          std::move(title_text), /*body_text=*/u"", std::move(illustration)),
      /*show_progress_bar=*/true);
}

void DigitalIdentityProviderDesktop::OnCableConnectingTimerComplete() {
  if (cable_connecting_ready_to_advance_) {
    ShowContinueStepsOnThePhoneDialog();
  }
}

void DigitalIdentityProviderDesktop::OnCanceled() {
  EndRequestWithError(RequestStatusForMetrics::kErrorOther);
}

void DigitalIdentityProviderDesktop::EndRequestWithError(
    RequestStatusForMetrics status) {
  if (callback_.is_null()) {
    return;
  }

  bluetooth_manual_dialog_controller_.reset();
  dialog_.reset();

  std::move(callback_).Run(base::unexpected(status));
}