Commit f22cb346 authored by Henrique Ferreiro's avatar Henrique Ferreiro Committed by Commit Bot

Move IdentityGetAccountsFunction to C++ IdentityManager

Remove the usage of the IdentityManager Mojo API in favor of the C++ API
for consistency with other //chrome/browser code.

Bug: 928184
Change-Id: I263a5fd9e5e2991639430b9d1280f88486c20690
Reviewed-on: https://chromium-review.googlesource.com/c/1456058
Auto-Submit: Henrique Ferreiro <hferreiro@igalia.com>
Reviewed-by: default avatarDavid Roger <droger@chromium.org>
Commit-Queue: David Roger <droger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#629563}
parent 98ffd7d9
...@@ -4,16 +4,18 @@ ...@@ -4,16 +4,18 @@
#include "chrome/browser/extensions/api/identity/identity_get_accounts_function.h" #include "chrome/browser/extensions/api/identity/identity_get_accounts_function.h"
#include "base/bind.h" #include <memory>
#include <utility>
#include <vector>
#include "chrome/browser/extensions/api/identity/identity_api.h" #include "chrome/browser/extensions/api/identity/identity_api.h"
#include "chrome/browser/extensions/api/identity/identity_constants.h" #include "chrome/browser/extensions/api/identity/identity_constants.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/common/extensions/api/identity.h" #include "chrome/common/extensions/api/identity.h"
#include "components/signin/core/browser/account_info.h"
#include "content/public/browser/browser_context.h" #include "content/public/browser/browser_context.h"
#include "content/public/common/service_manager_connection.h" #include "services/identity/public/cpp/identity_manager.h"
#include "services/identity/public/mojom/account.mojom.h"
#include "services/identity/public/mojom/constants.mojom.h"
#include "services/service_manager/public/cpp/connector.h"
namespace extensions { namespace extensions {
...@@ -28,23 +30,14 @@ ExtensionFunction::ResponseAction IdentityGetAccountsFunction::Run() { ...@@ -28,23 +30,14 @@ ExtensionFunction::ResponseAction IdentityGetAccountsFunction::Run() {
return RespondNow(Error(identity_constants::kOffTheRecord)); return RespondNow(Error(identity_constants::kOffTheRecord));
} }
content::BrowserContext::GetConnectorFor(browser_context()) std::vector<AccountInfo> accounts =
->BindInterface(identity::mojom::kServiceName, IdentityManagerFactory::GetForProfile(
mojo::MakeRequest(&identity_manager_)); Profile::FromBrowserContext(browser_context()))
->GetAccountsWithRefreshTokens();
identity_manager_->GetAccounts(
base::BindOnce(&IdentityGetAccountsFunction::OnGotAccounts, this));
return RespondLater();
}
void IdentityGetAccountsFunction::OnGotAccounts(
std::vector<identity::mojom::AccountPtr> accounts) {
std::unique_ptr<base::ListValue> infos(new base::ListValue()); std::unique_ptr<base::ListValue> infos(new base::ListValue());
if (accounts.empty()) { if (accounts.empty()) {
Respond(OneArgument(std::move(infos))); return RespondNow(OneArgument(std::move(infos)));
return;
} }
Profile* profile = Profile::FromBrowserContext(browser_context()); Profile* profile = Profile::FromBrowserContext(browser_context());
...@@ -52,26 +45,23 @@ void IdentityGetAccountsFunction::OnGotAccounts( ...@@ -52,26 +45,23 @@ void IdentityGetAccountsFunction::OnGotAccounts(
->Get(profile) ->Get(profile)
->AreExtensionsRestrictedToPrimaryAccount(); ->AreExtensionsRestrictedToPrimaryAccount();
// If extensions are restricted to the primary account and there is no valid auto* identity_manager = IdentityManagerFactory::GetForProfile(profile);
// primary account, short-circuit out. api::identity::AccountInfo account_info;
if (primary_account_only && (!accounts[0]->state.is_primary_account || if (primary_account_only) {
!accounts[0]->state.has_refresh_token)) { // If extensions are restricted to the primary account, only return the
Respond(OneArgument(std::move(infos))); // account info when there is a valid primary account.
return; if (identity_manager->HasPrimaryAccountWithRefreshToken()) {
account_info.id = identity_manager->GetPrimaryAccountInfo().gaia;
infos->Append(account_info.ToValue());
} }
} else {
for (const auto& account : accounts) { for (const auto& account : accounts) {
api::identity::AccountInfo account_info; account_info.id = account.gaia;
account_info.id = account->info.gaia;
infos->Append(account_info.ToValue()); infos->Append(account_info.ToValue());
}
// Stop after the primary account if extensions are restricted to the
// primary account.
if (primary_account_only)
break;
} }
Respond(OneArgument(std::move(infos))); return RespondNow(OneArgument(std::move(infos)));
} }
} // namespace extensions } // namespace extensions
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
#include "extensions/browser/extension_function.h" #include "extensions/browser/extension_function.h"
#include "extensions/browser/extension_function_histogram_value.h" #include "extensions/browser/extension_function_histogram_value.h"
#include "services/identity/public/mojom/identity_manager.mojom.h"
namespace extensions { namespace extensions {
...@@ -21,13 +20,8 @@ class IdentityGetAccountsFunction : public UIThreadExtensionFunction { ...@@ -21,13 +20,8 @@ class IdentityGetAccountsFunction : public UIThreadExtensionFunction {
private: private:
~IdentityGetAccountsFunction() override; ~IdentityGetAccountsFunction() override;
// Invoked in response to IdentityManager::GetAccounts().
void OnGotAccounts(std::vector<identity::mojom::AccountPtr> accounts);
// UIThreadExtensionFunction implementation. // UIThreadExtensionFunction implementation.
ExtensionFunction::ResponseAction Run() override; ExtensionFunction::ResponseAction Run() override;
identity::mojom::IdentityManagerPtr identity_manager_;
}; };
} // namespace extensions } // namespace extensions
......
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