Commit d4fa44a3 authored by Kushagra Sinha's avatar Kushagra Sinha Committed by Commit Bot

Lacros: Move Mojo de/serializers to account_manager_core

Move `Account` serializes and deserializers from `AccountManagerAsh` to
//components/account_manager_core, where it can be depended on by ash
and lacros both.

Bug: 1117466
Change-Id: I4927e89be2e909888f5ccf9ac19ad00b9f007ea7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2510055
Commit-Queue: Kush Sinha <sinhak@chromium.org>
Reviewed-by: default avatarAnastasiia N <anastasiian@chromium.org>
Reviewed-by: default avatarJames Cook <jamescook@chromium.org>
Cr-Commit-Position: refs/heads/master@{#825450}
parent 3b0fcae4
......@@ -8,6 +8,7 @@
#include "base/callback.h"
#include "base/notreached.h"
#include "components/account_manager_core/account_manager_util.h"
#include "mojo/public/cpp/bindings/remote.h"
namespace crosapi {
......@@ -47,62 +48,6 @@ void AccountManagerAsh::OnAccountRemoved(
observer->OnAccountRemoved(ToMojoAccount(account));
}
// static
base::Optional<account_manager::Account> AccountManagerAsh::FromMojoAccount(
const mojom::AccountPtr& mojom_account) {
const base::Optional<account_manager::AccountType> account_type =
FromMojoAccountType(mojom_account->key->account_type);
if (!account_type.has_value())
return base::nullopt;
account_manager::Account account;
account.key.id = mojom_account->key->id;
account.key.account_type = account_type.value();
account.raw_email = mojom_account->raw_email;
return account;
}
// static
mojom::AccountPtr AccountManagerAsh::ToMojoAccount(
const account_manager::Account& account) {
mojom::AccountPtr mojom_account = mojom::Account::New();
mojom_account->key = mojom::AccountKey::New();
mojom_account->key->id = account.key.id;
mojom_account->key->account_type =
ToMojoAccountType(account.key.account_type);
mojom_account->raw_email = account.raw_email;
return mojom_account;
}
// static
base::Optional<account_manager::AccountType>
AccountManagerAsh::FromMojoAccountType(const mojom::AccountType& account_type) {
switch (account_type) {
case mojom::AccountType::kGaia:
return account_manager::AccountType::kGaia;
case mojom::AccountType::kActiveDirectory:
return account_manager::AccountType::kActiveDirectory;
default:
// Ash does not know about this new account type. Don't consider this as
// as error to preserve forwards compatibility with lacros.
LOG(WARNING) << "Unknown account type: " << account_type;
return base::nullopt;
}
}
// static
mojom::AccountType AccountManagerAsh::ToMojoAccountType(
const account_manager::AccountType& account_type) {
switch (account_type) {
case account_manager::AccountType::kGaia:
return mojom::AccountType::kGaia;
case account_manager::AccountType::kActiveDirectory:
return mojom::AccountType::kActiveDirectory;
}
}
void AccountManagerAsh::FlushMojoForTesting() {
observers_.FlushForTesting();
}
......
......@@ -39,18 +39,6 @@ class AccountManagerAsh : public mojom::AccountManager,
friend class AccountManagerAshTest;
friend class TestAccountManagerObserver;
// Following util functions are static for ease of testing.
// Returns `base::nullopt` if `mojom_account` cannot be parsed.
static base::Optional<account_manager::Account> FromMojoAccount(
const mojom::AccountPtr& mojom_account);
static mojom::AccountPtr ToMojoAccount(
const account_manager::Account& account);
// Returns `base::nullopt` if `account_type` cannot be parsed.
static base::Optional<account_manager::AccountType> FromMojoAccountType(
const mojom::AccountType& account_type);
static mojom::AccountType ToMojoAccountType(
const account_manager::AccountType& account_type);
void FlushMojoForTesting();
chromeos::AccountManager* const account_manager_;
......
......@@ -14,6 +14,7 @@
#include "base/test/task_environment.h"
#include "chromeos/crosapi/mojom/account_manager.mojom-test-utils.h"
#include "chromeos/crosapi/mojom/account_manager.mojom.h"
#include "components/account_manager_core/account_manager_util.h"
#include "components/prefs/testing_pref_service.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
......@@ -67,14 +68,13 @@ class TestAccountManagerObserver
// mojom::AccountManagerObserverInterceptorForTesting override:
void OnTokenUpserted(mojom::AccountPtr account) override {
++num_token_upserted_calls_;
last_upserted_account_ =
AccountManagerAsh::FromMojoAccount(account).value();
last_upserted_account_ = account_manager::FromMojoAccount(account).value();
}
// mojom::AccountManagerObserverInterceptorForTesting override:
void OnAccountRemoved(mojom::AccountPtr account) override {
++num_account_removed_calls_;
last_removed_account_ = AccountManagerAsh::FromMojoAccount(account).value();
last_removed_account_ = account_manager::FromMojoAccount(account).value();
}
int num_token_upserted_calls_ = 0;
......
......@@ -81,8 +81,18 @@ base::Optional<::account_manager::AccountType> FromProtoAccountType(
case chromeos::account_manager::AccountType::ACCOUNT_TYPE_UNSPECIFIED:
return base::nullopt;
case chromeos::account_manager::AccountType::ACCOUNT_TYPE_GAIA:
static_assert(
static_cast<int>(
chromeos::account_manager::AccountType::ACCOUNT_TYPE_GAIA) ==
static_cast<int>(::account_manager::AccountType::kGaia),
"Underlying enum values must match");
return ::account_manager::AccountType::kGaia;
case chromeos::account_manager::AccountType::ACCOUNT_TYPE_ACTIVE_DIRECTORY:
static_assert(static_cast<int>(chromeos::account_manager::AccountType::
ACCOUNT_TYPE_ACTIVE_DIRECTORY) ==
static_cast<int>(
::account_manager::AccountType::kActiveDirectory),
"Underlying enum values must match");
return ::account_manager::AccountType::kActiveDirectory;
}
}
......
......@@ -12,9 +12,14 @@ component("account_manager_core") {
"account.h",
"account_manager_facade.cc",
"account_manager_facade.h",
"account_manager_util.cc",
"account_manager_util.h",
]
deps = [ "//base" ]
deps = [
"//base",
"//chromeos/crosapi/mojom",
]
defines = [ "IS_ACCOUNT_MANAGER_CORE_IMPL" ]
}
include_rules = [
"+chromeos/crosapi/mojom/account_manager.mojom.h",
]
// 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.
#include "components/account_manager_core/account_manager_util.h"
#include "components/account_manager_core/account.h"
namespace account_manager {
base::Optional<account_manager::Account> FromMojoAccount(
const crosapi::mojom::AccountPtr& mojom_account) {
const base::Optional<account_manager::AccountType> account_type =
FromMojoAccountType(mojom_account->key->account_type);
if (!account_type.has_value())
return base::nullopt;
account_manager::Account account;
account.key.id = mojom_account->key->id;
account.key.account_type = account_type.value();
account.raw_email = mojom_account->raw_email;
return account;
}
crosapi::mojom::AccountPtr ToMojoAccount(
const account_manager::Account& account) {
crosapi::mojom::AccountPtr mojom_account = crosapi::mojom::Account::New();
mojom_account->key = crosapi::mojom::AccountKey::New();
mojom_account->key->id = account.key.id;
mojom_account->key->account_type =
ToMojoAccountType(account.key.account_type);
mojom_account->raw_email = account.raw_email;
return mojom_account;
}
base::Optional<account_manager::AccountType> FromMojoAccountType(
const crosapi::mojom::AccountType& account_type) {
switch (account_type) {
case crosapi::mojom::AccountType::kGaia:
static_assert(static_cast<int>(crosapi::mojom::AccountType::kGaia) ==
static_cast<int>(account_manager::AccountType::kGaia),
"Underlying enum values must match");
return account_manager::AccountType::kGaia;
case crosapi::mojom::AccountType::kActiveDirectory:
static_assert(
static_cast<int>(crosapi::mojom::AccountType::kActiveDirectory) ==
static_cast<int>(account_manager::AccountType::kActiveDirectory),
"Underlying enum values must match");
return account_manager::AccountType::kActiveDirectory;
default:
// Don't consider this as as error to preserve forwards compatibility with
// lacros.
LOG(WARNING) << "Unknown account type: " << account_type;
return base::nullopt;
}
}
crosapi::mojom::AccountType ToMojoAccountType(
const account_manager::AccountType& account_type) {
switch (account_type) {
case account_manager::AccountType::kGaia:
return crosapi::mojom::AccountType::kGaia;
case account_manager::AccountType::kActiveDirectory:
return crosapi::mojom::AccountType::kActiveDirectory;
}
}
} // namespace account_manager
// 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_ACCOUNT_MANAGER_UTIL_H_
#define COMPONENTS_ACCOUNT_MANAGER_CORE_ACCOUNT_MANAGER_UTIL_H_
#include "base/optional.h"
#include "chromeos/crosapi/mojom/account_manager.mojom.h"
#include "components/account_manager_core/account.h"
namespace account_manager {
// Returns `base::nullopt` if `mojom_account` cannot be parsed.
COMPONENT_EXPORT(ACCOUNT_MANAGER_CORE)
base::Optional<account_manager::Account> FromMojoAccount(
const crosapi::mojom::AccountPtr& mojom_account);
COMPONENT_EXPORT(ACCOUNT_MANAGER_CORE)
crosapi::mojom::AccountPtr ToMojoAccount(
const account_manager::Account& account);
// Returns `base::nullopt` if `account_type` cannot be parsed.
COMPONENT_EXPORT(ACCOUNT_MANAGER_CORE)
base::Optional<account_manager::AccountType> FromMojoAccountType(
const crosapi::mojom::AccountType& account_type);
COMPONENT_EXPORT(ACCOUNT_MANAGER_CORE)
crosapi::mojom::AccountType ToMojoAccountType(
const account_manager::AccountType& account_type);
} // namespace account_manager
#endif // COMPONENTS_ACCOUNT_MANAGER_CORE_ACCOUNT_MANAGER_UTIL_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment