Commit 215f82a6 authored by xiyuan's avatar xiyuan Committed by Commit bot

Add a mojo interface for AccountId

And use it for SessionController interface.

BUG=648964

Review-Url: https://codereview.chromium.org/2617763003
Cr-Commit-Position: refs/heads/master@{#443356}
parent ca6a69d4
......@@ -70,7 +70,7 @@ void SessionController::LockScreen() {
void SessionController::SwitchActiveUser(const AccountId& account_id) {
if (client_)
client_->SwitchActiveUser(account_id.Serialize());
client_->SwitchActiveUser(account_id);
}
void SessionController::CycleActiveUser(bool next_user) {
......@@ -141,12 +141,8 @@ void SessionController::SetUserSessionOrder(
if (user_sessions_[0]->session_id != active_session_id_) {
active_session_id_ = user_sessions_[0]->session_id;
AccountId account_id(EmptyAccountId());
if (AccountId::Deserialize(user_sessions_[0]->serialized_account_id,
&account_id)) {
for (auto& observer : observers_)
observer.ActiveUserChanged(account_id);
}
for (auto& observer : observers_)
observer.ActiveUserChanged(user_sessions_[0]->account_id);
}
}
......@@ -160,12 +156,7 @@ void SessionController::SetSessionState(session_manager::SessionState state) {
}
void SessionController::AddUserSession(mojom::UserSessionPtr user_session) {
AccountId account_id(EmptyAccountId());
if (!AccountId::Deserialize(user_session->serialized_account_id,
&account_id)) {
LOG(ERROR) << "Failed to deserialize account id.";
return;
}
const AccountId account_id(user_session->account_id);
user_sessions_.push_back(std::move(user_session));
......
......@@ -93,8 +93,7 @@ class SessionControllerTest : public testing::Test {
mojom::UserSessionPtr session = mojom::UserSession::New();
session->session_id = session_id;
session->type = user_manager::USER_TYPE_REGULAR;
session->serialized_account_id =
AccountId::FromUserEmail(email).Serialize();
session->account_id = AccountId::FromUserEmail(email);
session->display_name = email;
session->display_email = email;
......
......@@ -189,7 +189,7 @@ void WmTestBase::SimulateUserLogin() {
session->session_id = 1;
session->type = user_manager::USER_TYPE_REGULAR;
const std::string email("ash.user@example.com");
session->serialized_account_id = AccountId::FromUserEmail(email).Serialize();
session->account_id = AccountId::FromUserEmail(email);
session->display_name = "Ash User";
session->display_email = email;
session_controller->UpdateUserSession(std::move(session));
......
......@@ -24,6 +24,7 @@ mojom("interfaces") {
]
public_deps = [
"//components/signin/public/interfaces",
"//skia/public/interfaces",
]
}
......@@ -4,6 +4,7 @@
module ash.mojom;
import "components/signin/public/interfaces/account_id.mojom";
import "skia/public/interfaces/bitmap.mojom";
// Matches session_manager::SessionState.
......@@ -73,7 +74,7 @@ struct UserSession {
uint32 session_id;
UserType type;
string serialized_account_id; // TODO(xiyuan): Use typemapping for AccountId.
signin.mojom.AccountId account_id;
string display_name;
string display_email;
skia.mojom.Bitmap avatar;
......@@ -139,7 +140,7 @@ interface SessionControllerClient {
// Switch to the active user with |account_id| (if the user has already signed
// in).
SwitchActiveUser(string account_id);
SwitchActiveUser(signin.mojom.AccountId account_id);
// Switch the active user to the next or previous user.
CycleActiveUser(bool next_user);
......
......@@ -53,8 +53,7 @@ ash::mojom::UserSessionPtr UserToUserSession(const User& user) {
ash::mojom::UserSessionPtr session = ash::mojom::UserSession::New();
session->session_id = GetSessionId(&user);
session->type = user.GetType();
// TODO(xiyuan): Add type map for AccountId.
session->serialized_account_id = user.GetAccountId().Serialize();
session->account_id = user.GetAccountId();
session->display_name = base::UTF16ToUTF8(user.display_name());
session->display_email = user.display_email();
......@@ -102,15 +101,7 @@ void SessionControllerClient::RequestLockScreen() {
DoLockScreen();
}
void SessionControllerClient::SwitchActiveUser(
const std::string& serialized_account_id) {
// TODO(xiyuan): Add type map for AccountId.
AccountId account_id(EmptyAccountId());
if (!AccountId::Deserialize(serialized_account_id, &account_id)) {
LOG(ERROR) << "Bad account id for SwitchActiveUser.";
return;
}
void SessionControllerClient::SwitchActiveUser(const AccountId& account_id) {
DoSwitchActiveUser(account_id);
}
......
......@@ -32,7 +32,7 @@ class SessionControllerClient
// ash::mojom::SessionControllerClient:
void RequestLockScreen() override;
void SwitchActiveUser(const std::string& serialized_account_id) override;
void SwitchActiveUser(const AccountId& account_id) override;
void CycleActiveUser(bool next_user) override;
// user_manager::UserManager::UserSessionStateObserver:
......
......@@ -24,6 +24,13 @@ class AccountId {
public:
struct EmptyAccountId;
// Creates an empty account id.
//
// Note: This constructor is public as it is required for mojo serialization
// To create an AccountId object, prefer using the static FromXXXX methods or
// the EmptyAccountId method when creating an empty account id.
AccountId();
AccountId(const AccountId& other);
// If any of the comparable AccountIds has AccountType == UNKNOWN then it
......@@ -90,7 +97,6 @@ class AccountId {
AccountId* out_account_id);
private:
AccountId();
AccountId(const std::string& id,
const std::string& user_email,
const AccountType& account_type);
......
# Copyright 2017 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.
import("//mojo/public/tools/bindings/mojom.gni")
mojom("interfaces") {
sources = [
"account_id.mojom",
]
}
per-file *.mojom=set noparent
per-file *.mojom=file://ipc/SECURITY_OWNERS
// Copyright 2017 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.
module signin.mojom;
// Matches AccountType of AccountId.
enum AccountType {
UNKNOWN,
GOOGLE,
ACTIVE_DIRECTORY,
};
// Mirror of AccountId in Mojo.
struct AccountId {
AccountType account_type;
string id;
string user_email;
};
# Copyright 2017 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.
mojom = "//components/signin/public/interfaces/account_id.mojom"
public_headers = [ "//components/signin/core/account_id/account_id.h" ]
traits_headers = [ "//components/signin/public/interfaces/account_id_traits.h" ]
public_deps = [
"//components/signin/core/account_id",
]
type_mappings = [
"signin.mojom.AccountType=AccountType",
"signin.mojom.AccountId=AccountId",
]
// Copyright 2017 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_SIGNIN_PUBLIC_INTERFACES_ACCOUNT_ID_TRAITS_H_
#define COMPONENTS_SIGNIN_PUBLIC_INTERFACES_ACCOUNT_ID_TRAITS_H_
#include <string>
#include "components/signin/core/account_id/account_id.h"
#include "components/signin/public/interfaces/account_id.mojom.h"
namespace mojo {
template <>
struct EnumTraits<signin::mojom::AccountType, AccountType> {
static signin::mojom::AccountType ToMojom(AccountType input) {
switch (input) {
case AccountType::UNKNOWN:
return signin::mojom::AccountType::UNKNOWN;
case AccountType::GOOGLE:
return signin::mojom::AccountType::GOOGLE;
case AccountType::ACTIVE_DIRECTORY:
return signin::mojom::AccountType::ACTIVE_DIRECTORY;
}
NOTREACHED();
return signin::mojom::AccountType::UNKNOWN;
}
static bool FromMojom(signin::mojom::AccountType input,
AccountType* out) {
switch (input) {
case signin::mojom::AccountType::UNKNOWN:
*out = AccountType::UNKNOWN;
return true;
case signin::mojom::AccountType::GOOGLE:
*out = AccountType::GOOGLE;
return true;
case signin::mojom::AccountType::ACTIVE_DIRECTORY:
*out = AccountType::ACTIVE_DIRECTORY;
return true;
}
NOTREACHED();
return false;
}
};
template <>
struct StructTraits<signin::mojom::AccountIdDataView, AccountId> {
static AccountType account_type(const AccountId& r) {
return r.GetAccountType();
}
static std::string id(const AccountId& r) {
switch (r.GetAccountType()) {
case AccountType::GOOGLE:
return r.GetGaiaId();
case AccountType::ACTIVE_DIRECTORY:
return r.GetObjGuid();
case AccountType::UNKNOWN:
// UNKNOWN type is used for users that have only email (e.g. in tests
// or legacy users that have not run through migration code).
// Return an empty string for such accounts.
return std::string();
}
NOTREACHED();
return std::string();
}
static std::string user_email(const AccountId& r) {
return r.GetUserEmail();
}
static bool Read(signin::mojom::AccountIdDataView data, AccountId* out) {
AccountType account_type;
std::string id;
std::string user_email;
if (!data.ReadAccountType(&account_type) ||
!data.ReadId(&id) ||
!data.ReadUserEmail(&user_email)) {
return false;
}
switch (account_type) {
case AccountType::GOOGLE:
*out = AccountId::FromUserEmailGaiaId(user_email, id);
break;
case AccountType::ACTIVE_DIRECTORY:
*out = AccountId::AdFromUserEmailObjGuid(user_email, id);
break;
case AccountType::UNKNOWN:
// UNKNOWN type is used for users that have only email (e.g. in tests
// or legacy users that have not run through migration code).
// Bail if there is no user email.
if (user_email.empty())
return false;
*out = AccountId::FromUserEmail(user_email);
break;
}
return out->is_valid();
}
};
} // namespace mojo
#endif // COMPONENTS_SIGNIN_PUBLIC_INTERFACES_ACCOUNT_ID_TRAITS_H_
......@@ -7,5 +7,6 @@ typemaps = [
"//components/content_settings/core/common/content_settings.typemap",
"//components/nacl/common/nacl.typemap",
"//components/password_manager/content/common/credential_manager.typemap",
"//components/signin/public/interfaces/account_id.typemap",
"//components/translate/content/common/translate.typemap",
]
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