Commit c2afb9b8 authored by James Cook's avatar James Cook Committed by Commit Bot

chromeos: Migrate DriveFS off the mojo Identity Service

The code in //chromeos/components/drivefs runs in the browser process
on the UI thread. There aren't any plans to move it out of process.
It can directly use the C++ IdentityManager instead of using the mojo
Identity Service. This will eliminate the last client of the Identity
Service, making it easier to refactor or delete it.

Convert DriveFsAuth to use PrimaryAccountAccessTokenFetcher, which
automatically handles waiting for / getting information about the
primary account.

Migrate the tests to use IdentityTestEnvironment, specifically the
helpers for access token requests. This requires rewriting tests
that used GMock on the Identity Service mojo API, but ends up being
less code overall.

Test: rewrite the chromeos_components_unittests
Test: Google Drive still works in File Manager to read / write / copy
      files.
Bug: 1054673

Change-Id: If83be823ee2ab1936289e21b2c50451004557c17
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2063419
Commit-Queue: James Cook <jamescook@chromium.org>
Reviewed-by: default avatarAustin Tankiang <austinct@chromium.org>
Reviewed-by: default avatarColin Blundell <blundell@chromium.org>
Cr-Commit-Position: refs/heads/master@{#744097}
parent 60e93eb9
...@@ -48,6 +48,7 @@ ...@@ -48,6 +48,7 @@
#include "components/metrics/metrics_pref_names.h" #include "components/metrics/metrics_pref_names.h"
#include "components/prefs/pref_change_registrar.h" #include "components/prefs/pref_change_registrar.h"
#include "components/prefs/pref_service.h" #include "components/prefs/pref_service.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/user_manager/user.h" #include "components/user_manager/user.h"
#include "components/version_info/version_info.h" #include "components/version_info/version_info.h"
#include "content/public/browser/browser_context.h" #include "content/public/browser/browser_context.h"
...@@ -60,7 +61,6 @@ ...@@ -60,7 +61,6 @@
#include "mojo/public/cpp/bindings/pending_remote.h" #include "mojo/public/cpp/bindings/pending_remote.h"
#include "net/traffic_annotation/network_traffic_annotation.h" #include "net/traffic_annotation/network_traffic_annotation.h"
#include "services/device/public/mojom/wake_lock_provider.mojom.h" #include "services/device/public/mojom/wake_lock_provider.mojom.h"
#include "services/identity/public/mojom/identity_service.mojom.h"
#include "services/network/public/cpp/network_connection_tracker.h" #include "services/network/public/cpp/network_connection_tracker.h"
#include "services/network/public/cpp/shared_url_loader_factory.h" #include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/service_manager/public/cpp/connector.h" #include "services/service_manager/public/cpp/connector.h"
...@@ -456,12 +456,8 @@ class DriveIntegrationService::DriveFsHolder ...@@ -456,12 +456,8 @@ class DriveIntegrationService::DriveFsHolder
return profile_->GetURLLoaderFactory(); return profile_->GetURLLoaderFactory();
} }
void BindIdentityAccessor( signin::IdentityManager* GetIdentityManager() override {
mojo::PendingReceiver<identity::mojom::IdentityAccessor> receiver) return IdentityManagerFactory::GetForProfile(profile_);
override {
auto* service = profile_->GetIdentityService();
if (service)
service->BindIdentityAccessor(std::move(receiver));
} }
const AccountId& GetAccountId() override { const AccountId& GetAccountId() override {
......
...@@ -30,12 +30,12 @@ component("drivefs") { ...@@ -30,12 +30,12 @@ component("drivefs") {
"//chromeos/disks", "//chromeos/disks",
"//components/account_id", "//components/account_id",
"//components/drive", "//components/drive",
"//components/signin/public/identity_manager",
"//dbus", "//dbus",
"//google_apis", "//google_apis",
"//mojo/public/cpp/bindings", "//mojo/public/cpp/bindings",
"//mojo/public/cpp/platform", "//mojo/public/cpp/platform",
"//net", "//net",
"//services/identity/public/mojom",
"//services/network/public/cpp:cpp", "//services/network/public/cpp:cpp",
] ]
defines = [ "IS_DRIVEFS_IMPL" ] defines = [ "IS_DRIVEFS_IMPL" ]
...@@ -77,10 +77,11 @@ source_set("unit_tests") { ...@@ -77,10 +77,11 @@ source_set("unit_tests") {
"//components/account_id", "//components/account_id",
"//components/drive", "//components/drive",
"//components/invalidation/impl:test_support", "//components/invalidation/impl:test_support",
"//components/signin/public/identity_manager",
"//components/signin/public/identity_manager:test_support",
"//mojo/public/cpp/bindings", "//mojo/public/cpp/bindings",
"//net", "//net",
"//net:test_support", "//net:test_support",
"//services/identity/public/mojom",
"//services/network:test_support", "//services/network:test_support",
"//services/network/public/cpp:cpp", "//services/network/public/cpp:cpp",
"//testing/gmock", "//testing/gmock",
......
include_rules = [ include_rules = [
"+components/drive", "+components/drive",
"+components/invalidation/impl/fake_invalidation_service.h", "+components/invalidation/impl/fake_invalidation_service.h",
"+components/signin",
"+mojo/public", "+mojo/public",
"+services/identity/public",
] ]
...@@ -6,6 +6,11 @@ ...@@ -6,6 +6,11 @@
#include "base/bind.h" #include "base/bind.h"
#include "components/account_id/account_id.h" #include "components/account_id/account_id.h"
#include "components/signin/public/identity_manager/access_token_info.h"
#include "components/signin/public/identity_manager/consent_level.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/signin/public/identity_manager/primary_account_access_token_fetcher.h"
#include "google_apis/gaia/google_service_auth_error.h"
#include "services/network/public/cpp/shared_url_loader_factory.h" #include "services/network/public/cpp/shared_url_loader_factory.h"
namespace drivefs { namespace drivefs {
...@@ -23,7 +28,7 @@ DriveFsAuth::DriveFsAuth(const base::Clock* clock, ...@@ -23,7 +28,7 @@ DriveFsAuth::DriveFsAuth(const base::Clock* clock,
timer_(std::move(timer)), timer_(std::move(timer)),
delegate_(delegate) {} delegate_(delegate) {}
DriveFsAuth::~DriveFsAuth() {} DriveFsAuth::~DriveFsAuth() = default;
base::Optional<std::string> DriveFsAuth::GetCachedAccessToken() { base::Optional<std::string> DriveFsAuth::GetCachedAccessToken() {
const auto& token = GetOrResetCachedToken(true); const auto& token = GetOrResetCachedToken(true);
...@@ -48,35 +53,32 @@ void DriveFsAuth::GetAccessToken( ...@@ -48,35 +53,32 @@ void DriveFsAuth::GetAccessToken(
return; return;
} }
signin::IdentityManager* identity_manager = delegate_->GetIdentityManager();
if (!identity_manager) {
std::move(callback).Run(mojom::AccessTokenStatus::kAuthError, "");
return;
}
get_access_token_callback_ = std::move(callback); get_access_token_callback_ = std::move(callback);
timer_->Start(FROM_HERE, base::TimeDelta::FromSeconds(30), // Timer is cancelled when it is destroyed, so use base::Unretained().
base::BindOnce(&DriveFsAuth::AuthTimeout, timer_->Start(
weak_ptr_factory_.GetWeakPtr())); FROM_HERE, base::TimeDelta::FromSeconds(30),
GetIdentityAccessor()->GetUnconsentedPrimaryAccountWhenAvailable( base::BindOnce(&DriveFsAuth::AuthTimeout, base::Unretained(this)));
base::BindOnce(&DriveFsAuth::AccountReady, std::set<std::string> scopes({"https://www.googleapis.com/auth/drive"});
weak_ptr_factory_.GetWeakPtr())); access_token_fetcher_ =
} std::make_unique<signin::PrimaryAccountAccessTokenFetcher>(
kIdentityConsumerId, identity_manager, scopes,
void DriveFsAuth::AccountReady(const CoreAccountId& account_id, base::BindOnce(&DriveFsAuth::GotChromeAccessToken,
const std::string& gaia, base::Unretained(this)),
const std::string& email, signin::PrimaryAccountAccessTokenFetcher::Mode::kWaitUntilAvailable,
const identity::AccountState& state) { signin::ConsentLevel::kNotRequired);
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
weak_ptr_factory_.InvalidateWeakPtrs();
timer_->Stop();
GetIdentityAccessor()->GetAccessToken(
account_id, {"https://www.googleapis.com/auth/drive"},
kIdentityConsumerId,
base::BindOnce(&DriveFsAuth::GotChromeAccessToken,
base::Unretained(this)));
} }
void DriveFsAuth::GotChromeAccessToken( void DriveFsAuth::GotChromeAccessToken(
const base::Optional<std::string>& access_token, GoogleServiceAuthError error,
base::Time expiration_time, signin::AccessTokenInfo access_token_info) {
const GoogleServiceAuthError& error) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!access_token) { timer_->Stop();
if (error.state() != GoogleServiceAuthError::NONE) {
std::move(get_access_token_callback_) std::move(get_access_token_callback_)
.Run(error.IsPersistentError() .Run(error.IsPersistentError()
? mojom::AccessTokenStatus::kAuthError ? mojom::AccessTokenStatus::kAuthError
...@@ -84,9 +86,9 @@ void DriveFsAuth::GotChromeAccessToken( ...@@ -84,9 +86,9 @@ void DriveFsAuth::GotChromeAccessToken(
""); "");
return; return;
} }
UpdateCachedToken(*access_token, expiration_time); UpdateCachedToken(access_token_info.token, access_token_info.expiration_time);
std::move(get_access_token_callback_) std::move(get_access_token_callback_)
.Run(mojom::AccessTokenStatus::kSuccess, *access_token); .Run(mojom::AccessTokenStatus::kSuccess, access_token_info.token);
} }
const std::string& DriveFsAuth::GetOrResetCachedToken(bool use_cached) { const std::string& DriveFsAuth::GetOrResetCachedToken(bool use_cached) {
...@@ -104,18 +106,8 @@ void DriveFsAuth::UpdateCachedToken(const std::string& token, ...@@ -104,18 +106,8 @@ void DriveFsAuth::UpdateCachedToken(const std::string& token,
void DriveFsAuth::AuthTimeout() { void DriveFsAuth::AuthTimeout() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
weak_ptr_factory_.InvalidateWeakPtrs();
std::move(get_access_token_callback_) std::move(get_access_token_callback_)
.Run(mojom::AccessTokenStatus::kAuthError, ""); .Run(mojom::AccessTokenStatus::kAuthError, "");
} }
identity::mojom::IdentityAccessor* DriveFsAuth::GetIdentityAccessor() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!identity_accessor_) {
delegate_->BindIdentityAccessor(
identity_accessor_.BindNewPipeAndPassReceiver());
}
return identity_accessor_.get();
}
} // namespace drivefs } // namespace drivefs
...@@ -11,20 +11,23 @@ ...@@ -11,20 +11,23 @@
#include "base/component_export.h" #include "base/component_export.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/time/clock.h" #include "base/time/clock.h"
#include "base/timer/timer.h" #include "base/timer/timer.h"
#include "chromeos/components/drivefs/mojom/drivefs.mojom.h" #include "chromeos/components/drivefs/mojom/drivefs.mojom.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "services/identity/public/mojom/identity_accessor.mojom.h"
class AccountId; class AccountId;
class GoogleServiceAuthError;
namespace network { namespace network {
class SharedURLLoaderFactory; class SharedURLLoaderFactory;
} // namespace network } // namespace network
namespace signin {
struct AccessTokenInfo;
class IdentityManager;
class PrimaryAccountAccessTokenFetcher;
} // namespace signin
namespace drivefs { namespace drivefs {
class COMPONENT_EXPORT(DRIVEFS) DriveFsAuth { class COMPONENT_EXPORT(DRIVEFS) DriveFsAuth {
...@@ -36,8 +39,7 @@ class COMPONENT_EXPORT(DRIVEFS) DriveFsAuth { ...@@ -36,8 +39,7 @@ class COMPONENT_EXPORT(DRIVEFS) DriveFsAuth {
virtual scoped_refptr<network::SharedURLLoaderFactory> virtual scoped_refptr<network::SharedURLLoaderFactory>
GetURLLoaderFactory() = 0; GetURLLoaderFactory() = 0;
virtual void BindIdentityAccessor( virtual signin::IdentityManager* GetIdentityManager() = 0;
mojo::PendingReceiver<identity::mojom::IdentityAccessor> receiver) = 0;
virtual const AccountId& GetAccountId() = 0; virtual const AccountId& GetAccountId() = 0;
virtual std::string GetObfuscatedAccountId() = 0; virtual std::string GetObfuscatedAccountId() = 0;
virtual bool IsMetricsCollectionEnabled() = 0; virtual bool IsMetricsCollectionEnabled() = 0;
...@@ -71,14 +73,8 @@ class COMPONENT_EXPORT(DRIVEFS) DriveFsAuth { ...@@ -71,14 +73,8 @@ class COMPONENT_EXPORT(DRIVEFS) DriveFsAuth {
mojom::DriveFsDelegate::GetAccessTokenCallback callback); mojom::DriveFsDelegate::GetAccessTokenCallback callback);
private: private:
void AccountReady(const CoreAccountId& account_id, void GotChromeAccessToken(GoogleServiceAuthError error,
const std::string& gaia, signin::AccessTokenInfo access_token_info);
const std::string& email,
const identity::AccountState& state);
void GotChromeAccessToken(const base::Optional<std::string>& access_token,
base::Time expiration_time,
const GoogleServiceAuthError& error);
const std::string& GetOrResetCachedToken(bool use_cached); const std::string& GetOrResetCachedToken(bool use_cached);
...@@ -86,16 +82,14 @@ class COMPONENT_EXPORT(DRIVEFS) DriveFsAuth { ...@@ -86,16 +82,14 @@ class COMPONENT_EXPORT(DRIVEFS) DriveFsAuth {
void AuthTimeout(); void AuthTimeout();
identity::mojom::IdentityAccessor* GetIdentityAccessor();
SEQUENCE_CHECKER(sequence_checker_); SEQUENCE_CHECKER(sequence_checker_);
const base::Clock* const clock_; const base::Clock* const clock_;
const base::FilePath profile_path_; const base::FilePath profile_path_;
const std::unique_ptr<base::OneShotTimer> timer_; const std::unique_ptr<base::OneShotTimer> timer_;
Delegate* const delegate_; Delegate* const delegate_;
// The connection to the identity service. Access via |GetIdentityAccessor()|. std::unique_ptr<signin::PrimaryAccountAccessTokenFetcher>
mojo::Remote<identity::mojom::IdentityAccessor> identity_accessor_; access_token_fetcher_;
// Pending callback for an in-flight GetAccessToken request. // Pending callback for an in-flight GetAccessToken request.
mojom::DriveFsDelegate::GetAccessTokenCallback get_access_token_callback_; mojom::DriveFsDelegate::GetAccessTokenCallback get_access_token_callback_;
...@@ -103,7 +97,6 @@ class COMPONENT_EXPORT(DRIVEFS) DriveFsAuth { ...@@ -103,7 +97,6 @@ class COMPONENT_EXPORT(DRIVEFS) DriveFsAuth {
std::string last_token_; std::string last_token_;
base::Time last_token_expiry_; base::Time last_token_expiry_;
base::WeakPtrFactory<DriveFsAuth> weak_ptr_factory_{this};
DISALLOW_COPY_AND_ASSIGN(DriveFsAuth); DISALLOW_COPY_AND_ASSIGN(DriveFsAuth);
}; };
......
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#include "mojo/public/cpp/bindings/remote.h" #include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h" #include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "net/base/mime_util.h" #include "net/base/mime_util.h"
#include "url/gurl.h"
namespace drivefs { namespace drivefs {
namespace { namespace {
......
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