blob: f137036f288d38348016c64f35a618f2a9f5a751 [file] [log] [blame]
// Copyright 2020 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.
#ifndef COMPONENTS_ACCOUNT_MANAGER_CORE_CHROMEOS_ACCOUNT_MANAGER_MOJO_SERVICE_H_
#define COMPONENTS_ACCOUNT_MANAGER_CORE_CHROMEOS_ACCOUNT_MANAGER_MOJO_SERVICE_H_
#include <memory>
#include <vector>
#include "base/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "chromeos/crosapi/mojom/account_manager.mojom.h"
#include "components/account_manager_core/account.h"
#include "components/account_manager_core/account_addition_result.h"
#include "components/account_manager_core/chromeos/access_token_fetcher.h"
#include "components/account_manager_core/chromeos/account_manager.h"
#include "components/account_manager_core/chromeos/account_manager_ui.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/receiver_set.h"
#include "mojo/public/cpp/bindings/remote_set.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
namespace chromeos {
class SigninHelper;
} // namespace chromeos
namespace crosapi {
// Implements the |crosapi::mojom::AccountManager| interface in ash-chrome.
// It enables lacros-chrome to interact with accounts stored in the Chrome OS
// Account Manager.
class COMPONENT_EXPORT(ACCOUNT_MANAGER_CORE) AccountManagerMojoService
: public mojom::AccountManager,
public account_manager::AccountManager::Observer {
public:
explicit AccountManagerMojoService(
account_manager::AccountManager* account_manager);
AccountManagerMojoService(const AccountManagerMojoService&) = delete;
AccountManagerMojoService& operator=(const AccountManagerMojoService&) =
delete;
~AccountManagerMojoService() override;
void BindReceiver(mojo::PendingReceiver<mojom::AccountManager> receiver);
void SetAccountManagerUI(
std::unique_ptr<account_manager::AccountManagerUI> account_manager_ui);
void OnAccountAdditionFinishedForTesting(
const account_manager::AccountAdditionResult& result);
// crosapi::mojom::AccountManager:
void IsInitialized(IsInitializedCallback callback) override;
void AddObserver(AddObserverCallback callback) override;
void GetAccounts(GetAccountsCallback callback) override;
void GetPersistentErrorForAccount(
mojom::AccountKeyPtr mojo_account_key,
GetPersistentErrorForAccountCallback callback) override;
void ShowAddAccountDialog(mojom::AccountAdditionOptionsPtr options,
ShowAddAccountDialogCallback callback) override;
void ShowReauthAccountDialog(const std::string& email,
base::OnceClosure closure) override;
void ShowManageAccountsSettings() override;
void CreateAccessTokenFetcher(
mojom::AccountKeyPtr mojo_account_key,
const std::string& oauth_consumer_name,
CreateAccessTokenFetcherCallback callback) override;
void ReportAuthError(mojom::AccountKeyPtr account,
mojom::GoogleServiceAuthErrorPtr error) override;
// account_manager::AccountManager::Observer:
void OnTokenUpserted(const account_manager::Account& account) override;
void OnAccountRemoved(const account_manager::Account& account) override;
private:
friend class AccountManagerMojoServiceTest;
friend class TestAccountManagerObserver;
friend class AccountManagerFacadeAshTest;
friend class chromeos::SigninHelper;
// This method is called by `chromeos::SigninHelper` which passes `AccountKey`
// of account that was added.
void OnAccountAdditionFinished(
const account_manager::AccountAdditionResult& result);
// A callback for `AccountManagerUI::ShowAccountAdditionDialog`.
void OnAddAccountDialogClosed();
void FinishAddAccount(const account_manager::AccountAdditionResult& result);
// Deletes `request` from `pending_access_token_requests_`, if present.
void DeletePendingAccessTokenFetchRequest(AccessTokenFetcher* request);
void FlushMojoForTesting();
int GetNumPendingAccessTokenRequests() const;
ShowAddAccountDialogCallback account_addition_callback_;
bool account_addition_in_progress_ = false;
const raw_ptr<account_manager::AccountManager> account_manager_;
std::unique_ptr<account_manager::AccountManagerUI> account_manager_ui_;
std::vector<std::unique_ptr<AccessTokenFetcher>>
pending_access_token_requests_;
// Don't add new members below this. `receivers_` and `observers_` should be
// destroyed as soon as `this` is getting destroyed so that we don't deal
// with message handling on a partially destroyed object.
mojo::ReceiverSet<mojom::AccountManager> receivers_;
mojo::RemoteSet<mojom::AccountManagerObserver> observers_;
base::WeakPtrFactory<AccountManagerMojoService> weak_ptr_factory_{this};
};
} // namespace crosapi
#endif // COMPONENTS_ACCOUNT_MANAGER_CORE_CHROMEOS_ACCOUNT_MANAGER_MOJO_SERVICE_H_