File: fast_pair_presenter_impl.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 (599 lines) | stat: -rw-r--r-- 22,534 bytes parent folder | download | duplicates (6)
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/quick_pair/ui/fast_pair/fast_pair_presenter_impl.h"

#include <optional>
#include <string>

#include "ash/constants/ash_features.h"
#include "ash/public/cpp/new_window_delegate.h"
#include "ash/public/cpp/session/session_controller.h"
#include "ash/public/cpp/system/toast_data.h"
#include "ash/public/cpp/system_tray_client.h"
#include "ash/quick_pair/common/device.h"
#include "ash/quick_pair/common/fast_pair/fast_pair_metrics.h"
#include "ash/quick_pair/common/quick_pair_browser_delegate.h"
#include "ash/quick_pair/proto/fastpair.pb.h"
#include "ash/quick_pair/repository/fast_pair/fast_pair_image_decoder.h"
#include "ash/quick_pair/repository/fast_pair_repository.h"
#include "ash/quick_pair/ui/actions.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/system/model/system_tray_model.h"
#include "ash/system/tray/tray_popup_utils.h"
#include "ash/system/tray/tray_utils.h"
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/strings/utf_string_conversions.h"
#include "components/cross_device/logging/logging.h"
#include "components/signin/public/base/consent_level.h"
#include "components/signin/public/identity_manager/account_info.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "ui/message_center/message_center.h"

namespace {

const char kDiscoveryLearnMoreLink[] =
    "https://support.google.com/chromebook?p=fast_pair_m101";
const char kAssociateAccountLearnMoreLink[] =
    "https://support.google.com/chromebook?p=bluetooth_pairing_m101";

bool ShouldShowUserEmail(ash::LoginStatus status) {
  switch (status) {
    case ash::LoginStatus::NOT_LOGGED_IN:
    case ash::LoginStatus::LOCKED:
    case ash::LoginStatus::KIOSK_APP:
    case ash::LoginStatus::GUEST:
    case ash::LoginStatus::PUBLIC:
      return false;
    case ash::LoginStatus::USER:
    case ash::LoginStatus::CHILD:
    default:
      return true;
  }
}

}  // namespace

namespace ash {
namespace quick_pair {

// static
FastPairPresenterImpl::Factory*
    FastPairPresenterImpl::Factory::g_test_factory_ = nullptr;

// static
std::unique_ptr<FastPairPresenter> FastPairPresenterImpl::Factory::Create(
    message_center::MessageCenter* message_center) {
  if (g_test_factory_) {
    return g_test_factory_->CreateInstance(message_center);
  }

  return base::WrapUnique(new FastPairPresenterImpl(message_center));
}

// static
void FastPairPresenterImpl::Factory::SetFactoryForTesting(
    Factory* g_test_factory) {
  g_test_factory_ = g_test_factory;
}

FastPairPresenterImpl::Factory::~Factory() = default;

FastPairPresenterImpl::FastPairPresenterImpl(
    message_center::MessageCenter* message_center)
    : notification_controller_(
          std::make_unique<FastPairNotificationController>(message_center)) {}

FastPairPresenterImpl::~FastPairPresenterImpl() = default;

void FastPairPresenterImpl::ShowDiscovery(scoped_refptr<Device> device,
                                          DiscoveryCallback callback) {
  DCHECK(device);
  const auto metadata_id = device->metadata_id();
  FastPairRepository::Get()->GetDeviceMetadata(
      metadata_id, base::BindRepeating(
                       &FastPairPresenterImpl::OnDiscoveryMetadataRetrieved,
                       weak_pointer_factory_.GetWeakPtr(), device, callback));
}

void FastPairPresenterImpl::OnDiscoveryMetadataRetrieved(
    scoped_refptr<Device> device,
    DiscoveryCallback callback,
    DeviceMetadata* device_metadata,
    bool has_retryable_error) {
  if (!device_metadata) {
    return;
  }

  device->set_version(device_metadata->InferFastPairVersion());

  if (device->protocol() == Protocol::kFastPairSubsequent) {
    ShowSubsequentDiscoveryNotification(device, callback, device_metadata);
    return;
  }

  if (device->version().value() == DeviceFastPairVersion::kV1) {
    RecordFastPairDiscoveredVersion(FastPairVersion::kVersion1);
  } else {
    RecordFastPairDiscoveredVersion(FastPairVersion::kVersion2);
  }

  // If we are in guest-mode, or are missing the IdentifyManager needed to show
  // detailed user notification, show the guest notification. We don't have to
  // verify opt-in status in this case because Guests will be guaranteed to not
  // have opt-in status.
  signin::IdentityManager* identity_manager =
      QuickPairBrowserDelegate::Get()->GetIdentityManager();
  if (!identity_manager ||
      !ShouldShowUserEmail(
          Shell::Get()->session_controller()->login_status())) {
    CD_LOG(VERBOSE, Feature::FP)
        << __func__ << ": in guest mode, showing guest notification";
    ShowGuestDiscoveryNotification(device, callback, device_metadata);
    return;
  }

  // Check if the user is opted in to saving devices to their account. If the
  // user is not opted in, we will show the guest notification which does not
  // mention saving devices to the user account. This is flagged depending if
  // the Fast Pair Saved Devices is enabled and we are using a strict
  // interpretation of the opt-in status.
  if (features::IsFastPairSavedDevicesEnabled() &&
      features::IsFastPairSavedDevicesStrictOptInEnabled()) {
    FastPairRepository::Get()->CheckOptInStatus(base::BindOnce(
        &FastPairPresenterImpl::OnCheckOptInStatus,
        weak_pointer_factory_.GetWeakPtr(), device, callback, device_metadata));
    return;
  }

  // If we don't have SavedDevices flag enabled, then we can ignore the user's
  // opt in status and move forward to showing the User Discovery notification.
  ShowUserDiscoveryNotification(device, callback, device_metadata);
}

void FastPairPresenterImpl::OnCheckOptInStatus(
    scoped_refptr<Device> device,
    DiscoveryCallback callback,
    DeviceMetadata* device_metadata,
    nearby::fastpair::OptInStatus status) {
  CD_LOG(INFO, Feature::FP) << __func__;

  if (status != nearby::fastpair::OptInStatus::STATUS_OPTED_IN) {
    ShowGuestDiscoveryNotification(device, callback, device_metadata);
    return;
  }

  ShowUserDiscoveryNotification(device, callback, device_metadata);
}

void FastPairPresenterImpl::ShowSubsequentDiscoveryNotification(
    scoped_refptr<Device> device,
    DiscoveryCallback callback,
    DeviceMetadata* device_metadata) {
  if (!device_metadata) {
    return;
  }

  // Since Subsequent Pairing scenario can only happen for a signed in user
  // when a device has already been saved to their account, this should never
  // be null. We cannot get to this scenario in Guest Mode.
  signin::IdentityManager* identity_manager =
      QuickPairBrowserDelegate::Get()->GetIdentityManager();
  DCHECK(identity_manager);

  const std::string& email =
      identity_manager->GetPrimaryAccountInfo(signin::ConsentLevel::kSignin)
          .email;
  notification_controller_->ShowSubsequentDiscoveryNotification(
      base::UTF8ToUTF16(device->display_name().value()),
      base::ASCIIToUTF16(email), device_metadata->image(),
      base::BindRepeating(&FastPairPresenterImpl::OnDiscoveryClicked,
                          weak_pointer_factory_.GetWeakPtr(), callback),
      base::BindRepeating(&FastPairPresenterImpl::OnDiscoveryLearnMoreClicked,
                          weak_pointer_factory_.GetWeakPtr(), callback),
      base::BindOnce(&FastPairPresenterImpl::OnDiscoveryDismissed,
                     weak_pointer_factory_.GetWeakPtr(), device, callback));
}

void FastPairPresenterImpl::ShowGuestDiscoveryNotification(
    scoped_refptr<Device> device,
    DiscoveryCallback callback,
    DeviceMetadata* device_metadata) {
  notification_controller_->ShowGuestDiscoveryNotification(
      base::ASCIIToUTF16(device_metadata->GetDetails().name()),
      device_metadata->image(),
      base::BindRepeating(&FastPairPresenterImpl::OnDiscoveryClicked,
                          weak_pointer_factory_.GetWeakPtr(), callback),
      base::BindRepeating(&FastPairPresenterImpl::OnDiscoveryLearnMoreClicked,
                          weak_pointer_factory_.GetWeakPtr(), callback),
      base::BindOnce(&FastPairPresenterImpl::OnDiscoveryDismissed,
                     weak_pointer_factory_.GetWeakPtr(), device, callback));
}

void FastPairPresenterImpl::ShowUserDiscoveryNotification(
    scoped_refptr<Device> device,
    DiscoveryCallback callback,
    DeviceMetadata* device_metadata) {
  // Since we check this in |OnInitialDiscoveryMetadataRetrieved| to determine
  // if we should show the Guest notification, this should never be null.
  signin::IdentityManager* identity_manager =
      QuickPairBrowserDelegate::Get()->GetIdentityManager();
  DCHECK(identity_manager);

  const std::string& email =
      identity_manager->GetPrimaryAccountInfo(signin::ConsentLevel::kSignin)
          .email;
  notification_controller_->ShowUserDiscoveryNotification(
      base::ASCIIToUTF16(device_metadata->GetDetails().name()),
      base::ASCIIToUTF16(email), device_metadata->image(),
      base::BindRepeating(&FastPairPresenterImpl::OnDiscoveryClicked,
                          weak_pointer_factory_.GetWeakPtr(), callback),
      base::BindRepeating(&FastPairPresenterImpl::OnDiscoveryLearnMoreClicked,
                          weak_pointer_factory_.GetWeakPtr(), callback),
      base::BindOnce(&FastPairPresenterImpl::OnDiscoveryDismissed,
                     weak_pointer_factory_.GetWeakPtr(), device, callback));
}

void FastPairPresenterImpl::OnDiscoveryClicked(DiscoveryCallback callback) {
  callback.Run(DiscoveryAction::kPairToDevice);
}

void FastPairPresenterImpl::OnDiscoveryDismissed(
    scoped_refptr<Device> device,
    DiscoveryCallback callback,
    FastPairNotificationDismissReason dismiss_reason) {
  switch (dismiss_reason) {
    case FastPairNotificationDismissReason::kDismissedByUser:
      callback.Run(DiscoveryAction::kDismissedByUser);
      break;
    case FastPairNotificationDismissReason::kDismissedByOs:
      callback.Run(DiscoveryAction::kDismissedByOs);
      break;
    case FastPairNotificationDismissReason::kDismissedByTimeout:
      callback.Run(DiscoveryAction::kDismissedByTimeout);
      break;
    default:
      NOTREACHED();
  }
}

void FastPairPresenterImpl::OnDiscoveryLearnMoreClicked(
    DiscoveryCallback callback) {
  NewWindowDelegate::GetPrimary()->OpenUrl(
      GURL(kDiscoveryLearnMoreLink),
      NewWindowDelegate::OpenUrlFrom::kUserInteraction,
      NewWindowDelegate::Disposition::kNewForegroundTab);
  callback.Run(DiscoveryAction::kLearnMore);
}

void FastPairPresenterImpl::ShowPairing(scoped_refptr<Device> device) {
  const auto metadata_id = device->metadata_id();
  FastPairRepository::Get()->GetDeviceMetadata(
      metadata_id,
      base::BindOnce(&FastPairPresenterImpl::OnPairingMetadataRetrieved,
                     weak_pointer_factory_.GetWeakPtr(), device));
}

void FastPairPresenterImpl::OnPairingMetadataRetrieved(
    scoped_refptr<Device> device,
    DeviceMetadata* device_metadata,
    bool has_retryable_error) {
  if (!device_metadata) {
    return;
  }

  notification_controller_->ShowPairingNotification(
      base::ASCIIToUTF16(device_metadata->GetDetails().name()),
      device_metadata->image(), base::DoNothing());
}

void FastPairPresenterImpl::ShowPairingFailed(scoped_refptr<Device> device,
                                              PairingFailedCallback callback) {
  const auto metadata_id = device->metadata_id();
  FastPairRepository::Get()->GetDeviceMetadata(
      metadata_id,
      base::BindOnce(&FastPairPresenterImpl::OnPairingFailedMetadataRetrieved,
                     weak_pointer_factory_.GetWeakPtr(), device, callback));
}

void FastPairPresenterImpl::OnPairingFailedMetadataRetrieved(
    scoped_refptr<Device> device,
    PairingFailedCallback callback,
    DeviceMetadata* device_metadata,
    bool has_retryable_error) {
  if (!device_metadata) {
    return;
  }

  notification_controller_->ShowErrorNotification(
      base::ASCIIToUTF16(device_metadata->GetDetails().name()),
      device_metadata->image(),
      base::BindRepeating(&FastPairPresenterImpl::OnNavigateToSettings,
                          weak_pointer_factory_.GetWeakPtr(), callback),
      base::BindOnce(&FastPairPresenterImpl::OnPairingFailedDismissed,
                     weak_pointer_factory_.GetWeakPtr(), callback));
}

void FastPairPresenterImpl::OnNavigateToSettings(
    PairingFailedCallback callback) {
  if (TrayPopupUtils::CanOpenWebUISettings()) {
    Shell::Get()->system_tray_model()->client()->ShowBluetoothSettings();
    RecordNavigateToSettingsResult(/*success=*/true);
  } else {
    CD_LOG(WARNING, Feature::FP)
        << "Cannot open Bluetooth Settings since it's not possible "
           "to opening WebUI settings";
    RecordNavigateToSettingsResult(/*success=*/false);
  }

  callback.Run(PairingFailedAction::kNavigateToSettings);
}

void FastPairPresenterImpl::OnPairingFailedDismissed(
    PairingFailedCallback callback,
    FastPairNotificationDismissReason dismiss_reason) {
  switch (dismiss_reason) {
    case FastPairNotificationDismissReason::kDismissedByUser:
      callback.Run(PairingFailedAction::kDismissedByUser);
      break;
    case FastPairNotificationDismissReason::kDismissedByOs:
      callback.Run(PairingFailedAction::kDismissed);
      break;
    case FastPairNotificationDismissReason::kDismissedByTimeout:
      // Fast Pair Error Notifications do not have a timeout, so this is never
      // expected to be hit.
      NOTREACHED();
    default:
      NOTREACHED();
  }
}

void FastPairPresenterImpl::ShowAssociateAccount(
    scoped_refptr<Device> device,
    AssociateAccountCallback callback) {
  RecordRetroactiveSuccessFunnelFlow(
      FastPairRetroactiveSuccessFunnelEvent::kNotificationDisplayed);
  const auto metadata_id = device->metadata_id();
  FastPairRepository::Get()->GetDeviceMetadata(
      metadata_id,
      base::BindOnce(
          &FastPairPresenterImpl::OnAssociateAccountMetadataRetrieved,
          weak_pointer_factory_.GetWeakPtr(), device, callback));
}

void FastPairPresenterImpl::OnAssociateAccountMetadataRetrieved(
    scoped_refptr<Device> device,
    AssociateAccountCallback callback,
    DeviceMetadata* device_metadata,
    bool has_retryable_error) {
  CD_LOG(VERBOSE, Feature::FP) << __func__ << ": " << device;
  if (!device_metadata) {
    return;
  }

  device->set_version(device_metadata->InferFastPairVersion());

  signin::IdentityManager* identity_manager =
      QuickPairBrowserDelegate::Get()->GetIdentityManager();
  if (!identity_manager) {
    CD_LOG(ERROR, Feature::FP)
        << __func__
        << ": IdentityManager is not available for Associate Account "
           "notification.";
    return;
  }

  const std::string email =
      identity_manager->GetPrimaryAccountInfo(signin::ConsentLevel::kSignin)
          .email;
  std::u16string device_name;
  // If the name of the device has been set by the user, use that name,
  // otherwise use the OEM default name.
  if (device->display_name().has_value()) {
    device_name = base::UTF8ToUTF16(device->display_name().value());
  } else {
    device_name = base::ASCIIToUTF16(device_metadata->GetDetails().name());
  }

  notification_controller_->ShowAssociateAccount(
      device_name, base::ASCIIToUTF16(email), device_metadata->image(),
      base::BindRepeating(
          &FastPairPresenterImpl::OnAssociateAccountActionClicked,
          weak_pointer_factory_.GetWeakPtr(), callback),
      base::BindRepeating(
          &FastPairPresenterImpl::OnAssociateAccountLearnMoreClicked,
          weak_pointer_factory_.GetWeakPtr(), callback),
      base::BindOnce(&FastPairPresenterImpl::OnAssociateAccountDismissed,
                     weak_pointer_factory_.GetWeakPtr(), callback));
}

void FastPairPresenterImpl::OnAssociateAccountActionClicked(
    AssociateAccountCallback callback) {
  callback.Run(AssociateAccountAction::kAssociateAccount);
}

void FastPairPresenterImpl::OnAssociateAccountLearnMoreClicked(
    AssociateAccountCallback callback) {
  NewWindowDelegate::GetPrimary()->OpenUrl(
      GURL(kAssociateAccountLearnMoreLink),
      NewWindowDelegate::OpenUrlFrom::kUserInteraction,
      NewWindowDelegate::Disposition::kNewForegroundTab);
  callback.Run(AssociateAccountAction::kLearnMore);
}

void FastPairPresenterImpl::OnAssociateAccountDismissed(
    AssociateAccountCallback callback,
    FastPairNotificationDismissReason dismiss_reason) {
  switch (dismiss_reason) {
    case FastPairNotificationDismissReason::kDismissedByUser:
      callback.Run(AssociateAccountAction::kDismissedByUser);
      break;
    case FastPairNotificationDismissReason::kDismissedByOs:
      callback.Run(AssociateAccountAction::kDismissedByOs);
      break;
    case FastPairNotificationDismissReason::kDismissedByTimeout:
      callback.Run(AssociateAccountAction::kDismissedByTimeout);
      break;
    default:
      NOTREACHED();
  }
}

void FastPairPresenterImpl::ShowInstallCompanionApp(
    scoped_refptr<Device> device,
    CompanionAppCallback callback) {
  CHECK(features::IsFastPairPwaCompanionEnabled());

  toast_collision_avoidance_timer_.Start(
      FROM_HERE, ash::ToastData::kDefaultToastDuration,
      base::BindOnce(&FastPairPresenterImpl::ShowInstallCompanionAppDelayed,
                     weak_pointer_factory_.GetWeakPtr(), device, callback));
}

void FastPairPresenterImpl::ShowInstallCompanionAppDelayed(
    scoped_refptr<Device> device,
    CompanionAppCallback callback) {
  CHECK(features::IsFastPairPwaCompanionEnabled());

  const auto metadata_id = device->metadata_id();
  FastPairRepository::Get()->GetDeviceMetadata(
      metadata_id,
      base::BindOnce(
          &FastPairPresenterImpl::OnInstallCompanionAppMetadataRetrieved,
          weak_pointer_factory_.GetWeakPtr(), device, callback));
}

void FastPairPresenterImpl::OnInstallCompanionAppMetadataRetrieved(
    scoped_refptr<Device> device,
    CompanionAppCallback callback,
    DeviceMetadata* device_metadata,
    bool has_retryable_error) {
  CHECK(features::IsFastPairPwaCompanionEnabled());

  if (!device_metadata) {
    return;
  }

  std::u16string device_name;
  // If the name of the device has been set by the user, use that name,
  // otherwise use the OEM default name.
  if (device->display_name().has_value()) {
    device_name = base::UTF8ToUTF16(device->display_name().value());
  } else {
    device_name = base::ASCIIToUTF16(device_metadata->GetDetails().name());
  }

  notification_controller_->ShowApplicationAvailableNotification(
      device_name, device_metadata->image(),
      base::BindRepeating(&FastPairPresenterImpl::OnCompanionAppInstallClicked,
                          weak_pointer_factory_.GetWeakPtr(), callback),
      base::BindOnce(&FastPairPresenterImpl::OnCompanionAppDismissed,
                     weak_pointer_factory_.GetWeakPtr(), callback));
}

void FastPairPresenterImpl::ShowLaunchCompanionApp(
    scoped_refptr<Device> device,
    CompanionAppCallback callback) {
  CHECK(features::IsFastPairPwaCompanionEnabled());

  toast_collision_avoidance_timer_.Start(
      FROM_HERE, ash::ToastData::kDefaultToastDuration,
      base::BindOnce(&FastPairPresenterImpl::ShowLaunchCompanionAppDelayed,
                     weak_pointer_factory_.GetWeakPtr(), device, callback));
}

void FastPairPresenterImpl::ShowLaunchCompanionAppDelayed(
    scoped_refptr<Device> device,
    CompanionAppCallback callback) {
  CHECK(features::IsFastPairPwaCompanionEnabled());

  const auto metadata_id = device->metadata_id();
  FastPairRepository::Get()->GetDeviceMetadata(
      metadata_id,
      base::BindOnce(
          &FastPairPresenterImpl::OnLaunchCompanionAppMetadataRetrieved,
          weak_pointer_factory_.GetWeakPtr(), device, callback));
}

void FastPairPresenterImpl::OnLaunchCompanionAppMetadataRetrieved(
    scoped_refptr<Device> device,
    CompanionAppCallback callback,
    DeviceMetadata* device_metadata,
    bool has_retryable_error) {
  CHECK(features::IsFastPairPwaCompanionEnabled());

  if (!device_metadata) {
    return;
  }

  std::u16string device_name;
  // If the name of the device has been set by the user, use that name,
  // otherwise use the OEM default name.
  if (device->display_name().has_value()) {
    device_name = base::UTF8ToUTF16(device->display_name().value());
  } else {
    device_name = base::ASCIIToUTF16(device_metadata->GetDetails().name());
  }

  notification_controller_->ShowApplicationInstalledNotification(
      // temporarily hardcoded text in place of companion app name
      device_name, device_metadata->image(), u"the web companion",
      base::BindRepeating(&FastPairPresenterImpl::OnCompanionAppSetupClicked,
                          weak_pointer_factory_.GetWeakPtr(), callback),
      base::BindOnce(&FastPairPresenterImpl::OnCompanionAppDismissed,
                     weak_pointer_factory_.GetWeakPtr(), callback));
}

void FastPairPresenterImpl::OnCompanionAppInstallClicked(
    CompanionAppCallback callback) {
  CHECK(features::IsFastPairPwaCompanionEnabled());

  callback.Run(CompanionAppAction::kDownloadAndLaunchApp);
}

void FastPairPresenterImpl::OnCompanionAppSetupClicked(
    CompanionAppCallback callback) {
  CHECK(features::IsFastPairPwaCompanionEnabled());

  callback.Run(CompanionAppAction::kLaunchApp);
}

void FastPairPresenterImpl::OnCompanionAppDismissed(
    CompanionAppCallback callback,
    FastPairNotificationDismissReason dismiss_reason) {
  CHECK(features::IsFastPairPwaCompanionEnabled());

  switch (dismiss_reason) {
    case FastPairNotificationDismissReason::kDismissedByUser:
      callback.Run(CompanionAppAction::kDismissedByUser);
      break;
    case FastPairNotificationDismissReason::kDismissedByOs:
      [[fallthrough]];
    case FastPairNotificationDismissReason::kDismissedByTimeout:
      callback.Run(CompanionAppAction::kDismissed);
      break;
    default:
      NOTREACHED();
  }
}

void FastPairPresenterImpl::ShowPasskey(std::u16string device_name,
                                        uint32_t passkey) {
  notification_controller_->ShowPasskey(device_name, passkey);
}

void FastPairPresenterImpl::RemoveNotifications() {
  notification_controller_->RemoveNotifications();
}

void FastPairPresenterImpl::ExtendNotification() {
  notification_controller_->ExtendNotification();
}

}  // namespace quick_pair
}  // namespace ash