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

Lacros: Add AccountManagerObserver interface

Add a Mojo interface - AccountManagerObserver - for ash-chrome to send
account update notifications to lacros-chrome.

Bug: 1117478
Test: unit_tests --gtest_filter="*AccountManagerAshTest*"
Change-Id: I986676d3a635bdc3e50abd9d7489ea0645c2286d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2453259
Commit-Queue: Kush Sinha <sinhak@chromium.org>
Reviewed-by: default avatarJorge Lucangeli Obes <jorgelo@chromium.org>
Reviewed-by: default avatarJames Cook <jamescook@chromium.org>
Cr-Commit-Position: refs/heads/master@{#815175}
parent 46d69c13
......@@ -6,7 +6,9 @@
#include <utility>
#include "base/callback.h"
#include "chromeos/components/account_manager/account_manager.h"
#include "mojo/public/cpp/bindings/remote.h"
namespace crosapi {
......@@ -23,4 +25,11 @@ void AccountManagerAsh::IsInitialized(IsInitializedCallback callback) {
std::move(callback).Run(account_manager_->IsInitialized());
}
void AccountManagerAsh::AddObserver(AddObserverCallback callback) {
mojo::Remote<mojom::AccountManagerObserver> remote;
auto receiver = remote.BindNewPipeAndPassReceiver();
observers_.Add(std::move(remote));
std::move(callback).Run(std::move(receiver));
}
} // namespace crosapi
......@@ -8,6 +8,7 @@
#include "chromeos/crosapi/mojom/account_manager.mojom.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote_set.h"
namespace chromeos {
class AccountManager;
......@@ -28,10 +29,14 @@ class AccountManagerAsh : public mojom::AccountManager {
// crosapi::mojom::AccountManager:
void IsInitialized(IsInitializedCallback callback) override;
void AddObserver(AddObserverCallback callback) override;
private:
friend class AccountManagerAshTest;
chromeos::AccountManager* const account_manager_;
mojo::Receiver<mojom::AccountManager> receiver_;
mojo::RemoteSet<mojom::AccountManagerObserver> observers_;
};
} // namespace crosapi
......
......@@ -4,6 +4,7 @@
#include "chrome/browser/chromeos/crosapi/account_manager_ash.h"
#include <cstddef>
#include <memory>
#include "base/run_loop.h"
......@@ -11,6 +12,8 @@
#include "base/test/task_environment.h"
#include "chromeos/components/account_manager/account_manager.h"
#include "chromeos/crosapi/mojom/account_manager.mojom-test-utils.h"
#include "chromeos/crosapi/mojom/account_manager.mojom.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/test/test_url_loader_factory.h"
......@@ -34,6 +37,8 @@ class AccountManagerAshTest : public ::testing::Test {
account_manager_ash_.get());
}
void RunAllPendingTasks() { task_environment_.RunUntilIdle(); }
// Returns |true| if initialization was successful.
bool InitializeAccountManager() {
base::RunLoop run_loop;
......@@ -43,6 +48,8 @@ class AccountManagerAshTest : public ::testing::Test {
return account_manager_.IsInitialized();
}
size_t GetNumObservers() { return account_manager_ash_->observers_.size(); }
mojom::AccountManagerAsyncWaiter* account_manager_async_waiter() {
return account_manager_async_waiter_.get();
}
......@@ -75,4 +82,18 @@ TEST_F(AccountManagerAshTest,
EXPECT_TRUE(is_initialized);
}
// Test that lacros remotes do not leak.
TEST_F(AccountManagerAshTest,
LacrosRemotesAreAutomaticallyRemovedOnConnectionClose) {
EXPECT_EQ(0, GetNumObservers());
{
mojo::PendingReceiver<mojom::AccountManagerObserver> receiver;
account_manager_async_waiter()->AddObserver(&receiver);
EXPECT_EQ(1, GetNumObservers());
}
// Wait for the disconnect handler to be called.
RunAllPendingTasks();
EXPECT_EQ(0, GetNumObservers());
}
} // namespace crosapi
......@@ -4,6 +4,58 @@
module crosapi.mojom;
// Types of accounts which can be stored in Account Manager.
// This must be kept in sync with
// //chromeos/components/account_manager/tokens.proto
[Stable, Extensible]
enum AccountType {
kUnspecified = 0,
kGaia = 1,
kActiveDirectory = 2,
};
// Uniquely identifies an account in Account Manager.
[Stable]
struct AccountKey {
// |id| is obfuscated Gaia id for Gaia accounts.
// |id| is the object GUID for Microsoft Active Directory accounts.
string id@0;
// Type of the account - such as Gaia, or Microsoft Active Directory.
AccountType account_type@1;
};
// Information about an account in Account Manager.
[Stable]
struct Account {
// A unique identifier for this account.
AccountKey key@0;
// The raw, un-canonicalized email id (The.Rock@gmail.com, as opposed to
// therock@gmail.com) for this account.
string raw_email@1;
};
// Interface for observers of Chrome OS Account Manager.
// This interface is implemented by lacros-chrome, and is used by ash-chrome to
// send account update notifications.
[Stable]
interface AccountManagerObserver {
// Called when the token for |account| is updated/inserted.
// Note: Observers which register with |AccountManager| before its
// initialization is complete will get notified when |AccountManager| is fully
// initialized.
// Note: Observers which register with |AccountManager| after its
// initialization is complete will not get an immediate
// notification-on-registration.
OnTokenUpserted@0(Account account);
// Called when an account has been removed from Account Manager.
// Note: Observers that may have cached access tokens for |account| must clear
// their cache entry for this |account| on receiving this callback.
OnAccountRemoved@1(Account account);
};
// Interface for Chrome OS Account Manager.
// Chrome OS Account Manager is the source of truth for accounts on Chrome OS -
// including ARC, and Chrome content area. It supports Google accounts and
......@@ -12,9 +64,21 @@ module crosapi.mojom;
// query accounts residing in the Chrome OS Account Manager.
// ARC++ uses components/arc/mojom/auth.mojom to talk to the Chrome OS Account
// Manager.
//
// Next version: 2
// Next method id: 2
[Stable]
interface AccountManager {
// Returns |true| if Chrome OS Account Manager has been fully initialized, and
// |false| otherwise.
IsInitialized@0() => (bool is_initialized);
// Creates and returns a new receiver for |AccountManagerObserver|.
// This API is supposed to be called by lacros-chrome, fairly early during its
// initialization, to receive updates related to accounts stored in Account
// Manager.
// The connection, and the corresponding remote, is destroyed when |receiver|
// is destroyed. This will happen automatically when lacros is shut down.
[MinVersion = 1]
AddObserver@1() => (pending_receiver<AccountManagerObserver> receiver);
};
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