File: account_manager_facade_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 (710 lines) | stat: -rw-r--r-- 26,762 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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/account_manager_core/account_manager_facade_impl.h"

#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>

#include "base/check.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/notreached.h"
#include "base/strings/stringprintf.h"
#include "chromeos/crosapi/mojom/account_manager.mojom.h"
#include "components/account_manager_core/account.h"
#include "components/account_manager_core/account_manager_util.h"
#include "components/account_manager_core/account_upsertion_result.h"
#include "components/account_manager_core/chromeos/account_manager.h"
#include "google_apis/gaia/google_service_auth_error.h"
#include "google_apis/gaia/oauth2_access_token_consumer.h"
#include "google_apis/gaia/oauth2_access_token_fetcher.h"
#include "google_apis/gaia/oauth2_access_token_fetcher_immediate_error.h"

namespace account_manager {

namespace {

using RemoteMinVersions = crosapi::mojom::AccountManager::MethodMinVersions;

// UMA histogram names.
const char kAccountUpsertionResultStatus[] =
    "AccountManager.AccountUpsertionResultStatus";
const char kGetAccountsMojoStatus[] =
    "AccountManager.FacadeGetAccountsMojoStatus";
const char kMojoDisconnectionsAccountManagerRemote[] =
    "AccountManager.MojoDisconnections.AccountManagerRemote";
const char kMojoDisconnectionsAccountManagerObserverReceiver[] =
    "AccountManager.MojoDisconnections.AccountManagerObserverReceiver";
const char kMojoDisconnectionsAccountManagerAccessTokenFetcherRemote[] =
    "AccountManager.MojoDisconnections.AccessTokenFetcherRemote";

void UnmarshalAccounts(
    base::OnceCallback<void(const std::vector<Account>&)> callback,
    std::vector<crosapi::mojom::AccountPtr> mojo_accounts) {
  std::vector<Account> accounts;
  for (const auto& mojo_account : mojo_accounts) {
    std::optional<Account> maybe_account = FromMojoAccount(mojo_account);
    if (!maybe_account) {
      // Skip accounts we couldn't unmarshal. No logging, as it would produce
      // a lot of noise.
      continue;
    }
    accounts.emplace_back(std::move(maybe_account.value()));
  }
  std::move(callback).Run(std::move(accounts));
}

void UnmarshalPersistentError(
    base::OnceCallback<void(const GoogleServiceAuthError&)> callback,
    crosapi::mojom::GoogleServiceAuthErrorPtr mojo_error) {
  std::optional<GoogleServiceAuthError> maybe_error =
      FromMojoGoogleServiceAuthError(mojo_error);
  if (!maybe_error) {
    // Couldn't unmarshal GoogleServiceAuthError, report the account as not
    // having an error. This is safe to do, as GetPersistentErrorForAccount is
    // best-effort (there's no way to know that the token was revoked on the
    // server).
    std::move(callback).Run(GoogleServiceAuthError::AuthErrorNone());
    return;
  }
  std::move(callback).Run(maybe_error.value());
}

// Returns whether an account should be available in ARC after it's added
// in-session.
bool GetIsAvailableInArcBySource(
    AccountManagerFacade::AccountAdditionSource source) {
  switch (source) {
    // Accounts added from Ash should be available in ARC.
    case AccountManagerFacade::AccountAdditionSource::kSettingsAddAccountButton:
    case AccountManagerFacade::AccountAdditionSource::
        kAccountManagerMigrationWelcomeScreen:
    case AccountManagerFacade::AccountAdditionSource::kArc:
    case AccountManagerFacade::AccountAdditionSource::kOnboarding:
      return true;
    // Accounts added from the browser should not be available in ARC.
    case AccountManagerFacade::AccountAdditionSource::kChromeProfileCreation:
    case AccountManagerFacade::AccountAdditionSource::kOgbAddAccount:
    case AccountManagerFacade::AccountAdditionSource::
        kAvatarBubbleTurnOnSyncAddAccount:
    case AccountManagerFacade::AccountAdditionSource::
        kChromeExtensionAddAccount:
    case AccountManagerFacade::AccountAdditionSource::
        kChromeSyncPromoAddAccount:
    case AccountManagerFacade::AccountAdditionSource::
        kChromeSettingsTurnOnSyncButton:
    case AccountManagerFacade::AccountAdditionSource::kChromeMenuTurnOnSync:
    case AccountManagerFacade::AccountAdditionSource::
        kChromeSigninPromoAddAccount:
      return false;
    // These are reauthentication cases. ARC visibility shouldn't change for
    // reauthentication.
    case AccountManagerFacade::AccountAdditionSource::kContentAreaReauth:
    case AccountManagerFacade::AccountAdditionSource::
        kSettingsReauthAccountButton:
    case AccountManagerFacade::AccountAdditionSource::
        kAvatarBubbleReauthAccountButton:
    case AccountManagerFacade::AccountAdditionSource::kChromeExtensionReauth:
    case AccountManagerFacade::AccountAdditionSource::kChromeSyncPromoReauth:
    case AccountManagerFacade::AccountAdditionSource::
        kChromeOSProjectorAppReauth:
    case AccountManagerFacade::AccountAdditionSource::
        kChromeSettingsReauthAccountButton:
      NOTREACHED();
    // Unused enums that cannot be deleted.
    case AccountManagerFacade::AccountAdditionSource::kPrintPreviewDialogUnused:
      NOTREACHED();
  }
}

// Error logs the Mojo connection stats when `event` occurs.
void LogMojoConnectionStats(const std::string& event,
                            int num_remote_disconnections,
                            int num_receiver_disconnections) {
  LOG(ERROR) << base::StringPrintf(
      "%s. Number of remote disconnections: %d, "
      "number of receiver disconnections: %d",
      event.c_str(), num_remote_disconnections, num_receiver_disconnections);
}

}  // namespace

// Fetches access tokens over the Mojo remote to `AccountManager`.
class AccountManagerFacadeImpl::AccessTokenFetcher
    : public OAuth2AccessTokenFetcher {
 public:
  AccessTokenFetcher(AccountManagerFacadeImpl* account_manager_facade_impl,
                     const account_manager::AccountKey& account_key,
                     OAuth2AccessTokenConsumer* consumer)
      : OAuth2AccessTokenFetcher(consumer),
        account_manager_facade_impl_(account_manager_facade_impl),
        account_key_(account_key),
        oauth_consumer_name_(consumer->GetConsumerName()) {}

  AccessTokenFetcher(const AccessTokenFetcher&) = delete;
  AccessTokenFetcher& operator=(const AccessTokenFetcher&) = delete;

  ~AccessTokenFetcher() override {
    base::UmaHistogramCounts100(
        kMojoDisconnectionsAccountManagerAccessTokenFetcherRemote,
        num_remote_disconnections_);
  }

  // Returns a closure, which marks `this` instance as ready for use. This
  // happens when `AccountManagerFacadeImpl`'s initialization sequence is
  // complete.
  base::OnceClosure UnblockTokenRequest() {
    return base::BindOnce(&AccessTokenFetcher::UnblockTokenRequestInternal,
                          weak_factory_.GetWeakPtr());
  }

  // Returns a closure which handles Mojo connection errors tied to Account
  // Manager remote.
  base::OnceClosure AccountManagerRemoteDisconnectionClosure() {
    return base::BindOnce(
        &AccessTokenFetcher::OnAccountManagerRemoteDisconnection,
        weak_factory_.GetWeakPtr());
  }

  // OAuth2AccessTokenFetcher override:
  // Note: This implementation ignores `client_id` and `client_secret` because
  // AccountManager's Mojo API does not support overriding OAuth client id and
  // secret.
  void Start(const std::string& client_id,
             const std::string& client_secret,
             const std::vector<std::string>& scopes) override {
    DCHECK(!is_request_pending_);
    is_request_pending_ = true;
    scopes_ = scopes;
    if (!are_token_requests_allowed_) {
      return;
    }
    StartInternal();
  }

  // OAuth2AccessTokenFetcher override:
  void CancelRequest() override {
    access_token_fetcher_.reset();
    is_request_pending_ = false;
  }

 private:
  void UnblockTokenRequestInternal() {
    are_token_requests_allowed_ = true;
    if (is_request_pending_) {
      StartInternal();
    }
  }

  void StartInternal() {
    DCHECK(are_token_requests_allowed_);
    bool is_remote_connected =
        account_manager_facade_impl_->CreateAccessTokenFetcher(
            account_manager::ToMojoAccountKey(account_key_),
            oauth_consumer_name_,
            base::BindOnce(&AccessTokenFetcher::FetchAccessToken,
                           weak_factory_.GetWeakPtr()));

    if (!is_remote_connected) {
      OnAccountManagerRemoteDisconnection();
    }
  }

  void FetchAccessToken(
      mojo::PendingRemote<crosapi::mojom::AccessTokenFetcher> pending_remote) {
    access_token_fetcher_.Bind(std::move(pending_remote));
    access_token_fetcher_.set_disconnect_handler(base::BindOnce(
        &AccessTokenFetcher::OnAccessTokenFetcherRemoteDisconnection,
        weak_factory_.GetWeakPtr()));
    access_token_fetcher_->Start(
        scopes_, base::BindOnce(&AccessTokenFetcher::OnAccessTokenFetchComplete,
                                weak_factory_.GetWeakPtr()));
  }

  void OnAccessTokenFetchComplete(crosapi::mojom::AccessTokenResultPtr result) {
    DCHECK(is_request_pending_);
    is_request_pending_ = false;

    if (result->is_error()) {
      std::optional<GoogleServiceAuthError> maybe_error =
          account_manager::FromMojoGoogleServiceAuthError(result->get_error());

      if (!maybe_error.has_value()) {
        LOG(ERROR) << "Unable to parse error result of access token fetch: "
                   << result->get_error()->state;
        FireOnGetTokenFailure(
            GoogleServiceAuthError::FromUnexpectedServiceResponse(
                "Error parsing Mojo error result of access token fetch"));
      } else {
        FireOnGetTokenFailure(maybe_error.value());
      }
      return;
    }

    FireOnGetTokenSuccess(
        OAuth2AccessTokenConsumer::TokenResponse::Builder()
            .WithAccessToken(result->get_access_token_info()->access_token)
            .WithExpirationTime(
                result->get_access_token_info()->expiration_time)
            .WithIdToken(result->get_access_token_info()->id_token)
            .build());
  }

  void OnAccountManagerRemoteDisconnection() {
    FailPendingRequestWithServiceError("Mojo pipe disconnected");
  }

  void OnAccessTokenFetcherRemoteDisconnection() {
    num_remote_disconnections_++;
    LOG(ERROR) << "Access token fetcher remote disconnected";
    FailPendingRequestWithServiceError("Access token Mojo pipe disconnected");
  }

  void FailPendingRequestWithServiceError(const std::string& message) {
    if (!is_request_pending_)
      return;

    CancelRequest();
    FireOnGetTokenFailure(GoogleServiceAuthError::FromServiceError(message));
  }

  const raw_ptr<AccountManagerFacadeImpl> account_manager_facade_impl_;
  const account_manager::AccountKey account_key_;
  const std::string oauth_consumer_name_;

  bool are_token_requests_allowed_ = false;
  bool is_request_pending_ = false;
  // Number of Mojo pipe disconnections seen by `access_token_fetcher_`.
  int num_remote_disconnections_ = 0;
  std::vector<std::string> scopes_;
  mojo::Remote<crosapi::mojom::AccessTokenFetcher> access_token_fetcher_;

  base::WeakPtrFactory<AccessTokenFetcher> weak_factory_{this};
};

AccountManagerFacadeImpl::AccountManagerFacadeImpl(
    mojo::Remote<crosapi::mojom::AccountManager> account_manager_remote,
    uint32_t remote_version,
    base::WeakPtr<AccountManager> account_manager_for_tests,
    base::OnceClosure init_finished)
    : remote_version_(remote_version),
      account_manager_remote_(std::move(account_manager_remote)),
      account_manager_for_tests_(std::move(account_manager_for_tests)) {
  DCHECK(init_finished);
  initialization_callbacks_.emplace_back(std::move(init_finished));

  if (!account_manager_remote_ ||
      remote_version_ < RemoteMinVersions::kGetAccountsMinVersion) {
    LOG(WARNING) << "Found remote at: " << remote_version_
                 << ", expected: " << RemoteMinVersions::kGetAccountsMinVersion
                 << ". Account consistency will be disabled";
    FinishInitSequenceIfNotAlreadyFinished();
    return;
  }

  account_manager_remote_.set_disconnect_handler(base::BindOnce(
      &AccountManagerFacadeImpl::OnAccountManagerRemoteDisconnected,
      weak_factory_.GetWeakPtr()));
  account_manager_remote_->AddObserver(
      base::BindOnce(&AccountManagerFacadeImpl::OnReceiverReceived,
                     weak_factory_.GetWeakPtr()));
}

AccountManagerFacadeImpl::~AccountManagerFacadeImpl() {
  base::UmaHistogramCounts100(kMojoDisconnectionsAccountManagerRemote,
                              num_remote_disconnections_);
  base::UmaHistogramCounts100(kMojoDisconnectionsAccountManagerObserverReceiver,
                              num_receiver_disconnections_);
}

void AccountManagerFacadeImpl::AddObserver(Observer* observer) {
  observer_list_.AddObserver(observer);
}

void AccountManagerFacadeImpl::RemoveObserver(Observer* observer) {
  observer_list_.RemoveObserver(observer);
}

void AccountManagerFacadeImpl::GetAccounts(
    base::OnceCallback<void(const std::vector<Account>&)> callback) {
  // Record the status of the mojo connection, to get more information about
  // https://crbug.com/1287297
  FacadeMojoStatus mojo_status = FacadeMojoStatus::kOk;
  if (!account_manager_remote_)
    mojo_status = FacadeMojoStatus::kNoRemote;
  else if (remote_version_ < RemoteMinVersions::kGetAccountsMinVersion)
    mojo_status = FacadeMojoStatus::kVersionMismatch;
  else if (!is_initialized_)
    mojo_status = FacadeMojoStatus::kUninitialized;
  base::UmaHistogramEnumeration(kGetAccountsMojoStatus, mojo_status);

  if (!account_manager_remote_ ||
      remote_version_ < RemoteMinVersions::kGetAccountsMinVersion) {
    // Remote side is disconnected or doesn't support GetAccounts. Do not return
    // an empty list as that may cause Lacros to delete user profiles.
    // TODO(crbug.com/40211181): Try to reconnect, or return an error.
    return;
  }
  RunAfterInitializationSequence(
      base::BindOnce(&AccountManagerFacadeImpl::GetAccountsInternal,
                     weak_factory_.GetWeakPtr(), std::move(callback)));
}

void AccountManagerFacadeImpl::GetPersistentErrorForAccount(
    const AccountKey& account,
    base::OnceCallback<void(const GoogleServiceAuthError&)> callback) {
  if (!account_manager_remote_ ||
      remote_version_ <
          RemoteMinVersions::kGetPersistentErrorForAccountMinVersion) {
    // Remote side doesn't support GetPersistentErrorForAccount.
    std::move(callback).Run(GoogleServiceAuthError::AuthErrorNone());
    return;
  }
  RunAfterInitializationSequence(
      base::BindOnce(&AccountManagerFacadeImpl::GetPersistentErrorInternal,
                     weak_factory_.GetWeakPtr(), account, std::move(callback)));
}

void AccountManagerFacadeImpl::ShowAddAccountDialog(
    AccountAdditionSource source) {
  ShowAddAccountDialog(source, base::DoNothing());
}

void AccountManagerFacadeImpl::ShowAddAccountDialog(
    AccountAdditionSource source,
    base::OnceCallback<
        void(const account_manager::AccountUpsertionResult& result)> callback) {
  if (!account_manager_remote_) {
    LOG(WARNING) << "Account Manager remote disconnected";
    FinishUpsertAccount(
        std::move(callback),
        AccountUpsertionResult::FromStatus(
            AccountUpsertionResult::Status::kMojoRemoteDisconnected));
    return;
  }

  if (remote_version_ < RemoteMinVersions::kShowAddAccountDialogMinVersion) {
    LOG(WARNING) << "Found remote at: " << remote_version_ << ", expected: "
                 << RemoteMinVersions::kShowAddAccountDialogMinVersion
                 << " for ShowAddAccountDialog.";
    FinishUpsertAccount(
        std::move(callback),
        AccountUpsertionResult::FromStatus(
            AccountUpsertionResult::Status::kIncompatibleMojoVersions));
    return;
  }

  base::UmaHistogramEnumeration(kAccountAdditionSource, source);

  crosapi::mojom::AccountAdditionOptionsPtr options =
      crosapi::mojom::AccountAdditionOptions::New();
  options->is_available_in_arc = GetIsAvailableInArcBySource(source);
  options->show_arc_availability_picker =
      (source == AccountManagerFacade::AccountAdditionSource::kArc);

  account_manager_remote_->ShowAddAccountDialog(
      std::move(options),
      base::BindOnce(&AccountManagerFacadeImpl::OnSigninDialogActionFinished,
                     weak_factory_.GetWeakPtr(), std::move(callback)));
}

void AccountManagerFacadeImpl::ShowReauthAccountDialog(
    AccountAdditionSource source,
    const std::string& email,
    base::OnceCallback<
        void(const account_manager::AccountUpsertionResult& result)> callback) {
  if (!account_manager_remote_) {
    LOG(WARNING) << "Account Manager remote disconnected";
    FinishUpsertAccount(
        std::move(callback),
        AccountUpsertionResult::FromStatus(
            AccountUpsertionResult::Status::kMojoRemoteDisconnected));
    return;
  }

  if (remote_version_ < RemoteMinVersions::kShowReauthAccountDialogMinVersion) {
    LOG(WARNING) << "Found remote at: " << remote_version_ << ", expected: "
                 << RemoteMinVersions::kShowReauthAccountDialogMinVersion
                 << " for ShowReauthAccountDialog.";
    FinishUpsertAccount(
        std::move(callback),
        AccountUpsertionResult::FromStatus(
            AccountUpsertionResult::Status::kIncompatibleMojoVersions));
    return;
  }

  base::UmaHistogramEnumeration(kAccountAdditionSource, source);

  account_manager_remote_->ShowReauthAccountDialog(
      email,
      base::BindOnce(&AccountManagerFacadeImpl::OnSigninDialogActionFinished,
                     weak_factory_.GetWeakPtr(), std::move(callback)));
}

void AccountManagerFacadeImpl::ShowManageAccountsSettings() {
  if (!account_manager_remote_ ||
      remote_version_ <
          RemoteMinVersions::kShowManageAccountsSettingsMinVersion) {
    LOG(WARNING) << "Found remote at: " << remote_version_ << ", expected: "
                 << RemoteMinVersions::kShowManageAccountsSettingsMinVersion
                 << " for ShowManageAccountsSettings.";
    return;
  }

  account_manager_remote_->ShowManageAccountsSettings();
}

std::unique_ptr<OAuth2AccessTokenFetcher>
AccountManagerFacadeImpl::CreateAccessTokenFetcher(
    const AccountKey& account,
    OAuth2AccessTokenConsumer* consumer) {
  if (!account_manager_remote_ ||
      remote_version_ <
          RemoteMinVersions::kCreateAccessTokenFetcherMinVersion) {
    VLOG(1) << "Found remote at: " << remote_version_ << ", expected: "
            << RemoteMinVersions::kCreateAccessTokenFetcherMinVersion
            << " for CreateAccessTokenFetcher";
    return std::make_unique<OAuth2AccessTokenFetcherImmediateError>(
        consumer,
        GoogleServiceAuthError::FromServiceError("Mojo pipe disconnected"));
  }

  auto access_token_fetcher = std::make_unique<AccessTokenFetcher>(
      /*account_manager_facade_impl=*/this, account, consumer);
  RunAfterInitializationSequence(access_token_fetcher->UnblockTokenRequest());
  RunOnAccountManagerRemoteDisconnection(
      access_token_fetcher->AccountManagerRemoteDisconnectionClosure());
  return std::move(access_token_fetcher);
}

void AccountManagerFacadeImpl::ReportAuthError(
    const account_manager::AccountKey& account,
    const GoogleServiceAuthError& error) {
  if (!account_manager_remote_ ||
      remote_version_ < RemoteMinVersions::kReportAuthErrorMinVersion) {
    LOG(WARNING) << "Found remote at: " << remote_version_ << ", expected: "
                 << RemoteMinVersions::kReportAuthErrorMinVersion
                 << " for ReportAuthError.";
    return;
  }

  account_manager_remote_->ReportAuthError(ToMojoAccountKey(account),
                                           ToMojoGoogleServiceAuthError(error));
}

void AccountManagerFacadeImpl::UpsertAccountForTesting(
    const Account& account,
    const std::string& token_value) {
  CHECK(account_manager_for_tests_);
  account_manager_for_tests_->UpsertAccount(account.key, account.raw_email,
                                            token_value);
}

void AccountManagerFacadeImpl::RemoveAccountForTesting(
    const AccountKey& account) {
  CHECK(account_manager_for_tests_);
  account_manager_for_tests_->RemoveAccount(account);
}

// static
std::string AccountManagerFacadeImpl::
    GetAccountUpsertionResultStatusHistogramNameForTesting() {
  return kAccountUpsertionResultStatus;
}

// static
std::string
AccountManagerFacadeImpl::GetAccountsMojoStatusHistogramNameForTesting() {
  return kGetAccountsMojoStatus;
}

void AccountManagerFacadeImpl::OnReceiverReceived(
    mojo::PendingReceiver<AccountManagerObserver> receiver) {
  receiver_ =
      std::make_unique<mojo::Receiver<crosapi::mojom::AccountManagerObserver>>(
          this, std::move(receiver));
  // At this point (`receiver_` exists), we are subscribed to Account Manager.

  receiver_->set_disconnect_handler(base::BindOnce(
      &AccountManagerFacadeImpl::OnAccountManagerObserverReceiverDisconnected,
      weak_factory_.GetWeakPtr()));

  FinishInitSequenceIfNotAlreadyFinished();
}

void AccountManagerFacadeImpl::OnSigninDialogActionFinished(
    base::OnceCallback<
        void(const account_manager::AccountUpsertionResult& result)> callback,
    crosapi::mojom::AccountUpsertionResultPtr mojo_result) {
  std::optional<account_manager::AccountUpsertionResult> result =
      account_manager::FromMojoAccountUpsertionResult(mojo_result);
  if (!result.has_value()) {
    FinishUpsertAccount(
        std::move(callback),
        AccountUpsertionResult::FromStatus(
            AccountUpsertionResult::Status::kUnexpectedResponse));
    return;
  }
  FinishUpsertAccount(std::move(callback), result.value());
}

void AccountManagerFacadeImpl::FinishUpsertAccount(
    base::OnceCallback<
        void(const account_manager::AccountUpsertionResult& result)> callback,
    const account_manager::AccountUpsertionResult& result) {
  base::UmaHistogramEnumeration(kAccountUpsertionResultStatus, result.status());
  std::move(callback).Run(result);
}

void AccountManagerFacadeImpl::OnTokenUpserted(
    crosapi::mojom::AccountPtr account) {
  std::optional<Account> maybe_account = FromMojoAccount(account);
  if (!maybe_account) {
    LOG(WARNING) << "Can't unmarshal account of type: "
                 << account->key->account_type;
    return;
  }
  for (auto& observer : observer_list_) {
    observer.OnAccountUpserted(maybe_account.value());
  }
}

void AccountManagerFacadeImpl::OnAccountRemoved(
    crosapi::mojom::AccountPtr account) {
  std::optional<Account> maybe_account = FromMojoAccount(account);
  if (!maybe_account) {
    LOG(WARNING) << "Can't unmarshal account of type: "
                 << account->key->account_type;
    return;
  }
  for (auto& observer : observer_list_) {
    observer.OnAccountRemoved(maybe_account.value());
  }
}

void AccountManagerFacadeImpl::OnAuthErrorChanged(
    crosapi::mojom::AccountKeyPtr account,
    crosapi::mojom::GoogleServiceAuthErrorPtr error) {
  std::optional<AccountKey> maybe_account_key = FromMojoAccountKey(account);
  if (!maybe_account_key) {
    LOG(WARNING) << "Can't unmarshal account key of type: "
                 << account->account_type;
    return;
  }

  std::optional<GoogleServiceAuthError> maybe_error =
      FromMojoGoogleServiceAuthError(error);
  if (!maybe_error) {
    LOG(WARNING) << "Can't unmarshal error with state: " << error->state;
    return;
  }

  for (auto& observer : observer_list_) {
    observer.OnAuthErrorChanged(maybe_account_key.value(), maybe_error.value());
  }
}

void AccountManagerFacadeImpl::OnSigninDialogClosed() {
  for (auto& observer : observer_list_) {
    observer.OnSigninDialogClosed();
  }
}

void AccountManagerFacadeImpl::GetAccountsInternal(
    base::OnceCallback<void(const std::vector<Account>&)> callback) {
  account_manager_remote_->GetAccounts(
      base::BindOnce(&UnmarshalAccounts, std::move(callback)));
}

void AccountManagerFacadeImpl::GetPersistentErrorInternal(
    const AccountKey& account,
    base::OnceCallback<void(const GoogleServiceAuthError&)> callback) {
  account_manager_remote_->GetPersistentErrorForAccount(
      ToMojoAccountKey(account),
      base::BindOnce(&UnmarshalPersistentError, std::move(callback)));
}

bool AccountManagerFacadeImpl::CreateAccessTokenFetcher(
    crosapi::mojom::AccountKeyPtr account_key,
    const std::string& oauth_consumer_name,
    crosapi::mojom::AccountManager::CreateAccessTokenFetcherCallback callback) {
  if (!account_manager_remote_) {
    return false;
  }

  account_manager_remote_->CreateAccessTokenFetcher(
      std::move(account_key), oauth_consumer_name, std::move(callback));
  return true;
}

void AccountManagerFacadeImpl::FinishInitSequenceIfNotAlreadyFinished() {
  if (is_initialized_) {
    return;
  }

  is_initialized_ = true;
  for (auto& cb : initialization_callbacks_) {
    std::move(cb).Run();
  }
  initialization_callbacks_.clear();
}

void AccountManagerFacadeImpl::RunAfterInitializationSequence(
    base::OnceClosure closure) {
  if (!is_initialized_) {
    initialization_callbacks_.emplace_back(std::move(closure));
  } else {
    std::move(closure).Run();
  }
}

void AccountManagerFacadeImpl::RunOnAccountManagerRemoteDisconnection(
    base::OnceClosure closure) {
  if (!account_manager_remote_) {
    std::move(closure).Run();
    return;
  }
  account_manager_remote_disconnection_handlers_.emplace_back(
      std::move(closure));
}

void AccountManagerFacadeImpl::OnAccountManagerRemoteDisconnected() {
  num_remote_disconnections_++;
  LogMojoConnectionStats("Account Manager disconnected",
                         num_remote_disconnections_,
                         num_receiver_disconnections_);
  for (auto& cb : account_manager_remote_disconnection_handlers_) {
    std::move(cb).Run();
  }
  account_manager_remote_disconnection_handlers_.clear();
  account_manager_remote_.reset();
}

void AccountManagerFacadeImpl::OnAccountManagerObserverReceiverDisconnected() {
  num_receiver_disconnections_++;
  LogMojoConnectionStats("Account Manager Observer disconnected",
                         num_remote_disconnections_,
                         num_receiver_disconnections_);
}

bool AccountManagerFacadeImpl::IsInitialized() {
  return is_initialized_;
}

void AccountManagerFacadeImpl::FlushMojoForTesting() {
  if (!account_manager_remote_) {
    return;
  }
  account_manager_remote_.FlushForTesting();
}

}  // namespace account_manager