Commit 89fe606f authored by Antonio Gomes's avatar Antonio Gomes Committed by Commit Bot

Develop IdentityManager::Observer equivalent to GCMS::Observer::OnGaiaCookieDeletedByUserAction

This method introduces an IdentityManager::Observer callback, namely
OnAccountsCookieDeletedByUserAction(), equivalent to the existing
GaiaCookieManagerService::Observer::OnGaiaCookieDeletedByUserAction.

In order to unit test, FakeCookieManager and CustomTestSigninClient
classes where introduced, so the the code path that triggers the
call to GCMS::Observer::OnGaiaCookieDeletedByUserAction gets executed.

Particularly about FakeCookieManager, it inherits from
network::mojom::CookieManager which is a pure virtual class, and needed
all its methods but one to be overridden / stubbed out. The method from
FakeCookieManager that needed a real (although simple) implementation
was AddCookieChangeListener().

BUG=926890

Change-Id: Icc1124eded616a8c31efc58c9e1950f08363c3b3
Reviewed-on: https://chromium-review.googlesource.com/c/1452816
Commit-Queue: Antonio Gomes <tonikitoo@igalia.com>
Reviewed-by: default avatarDavid Roger <droger@chromium.org>
Reviewed-by: default avatarColin Blundell <blundell@chromium.org>
Cr-Commit-Position: refs/heads/master@{#629139}
parent 27510944
......@@ -7,6 +7,7 @@
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/callback_forward.h"
......@@ -50,8 +51,11 @@ class TestSigninClient : public SigninClient {
// Wraps the test_url_loader_factory().
scoped_refptr<network::SharedURLLoaderFactory> GetURLLoaderFactory() override;
// Returns FakeCookieManager.
network::mojom::CookieManager* GetCookieManager() override;
void set_cookie_manager(
std::unique_ptr<network::mojom::CookieManager> cookie_manager) {
cookie_manager_ = std::move(cookie_manager);
}
network::TestURLLoaderFactory* test_url_loader_factory() {
return &test_url_loader_factory_;
......
......@@ -443,6 +443,12 @@ void IdentityManager::OnSetAccountsInCookieCompleted(
}
}
void IdentityManager::OnGaiaCookieDeletedByUserAction() {
for (auto& observer : observer_list_) {
observer.OnAccountsCookieDeletedByUserAction();
}
}
void IdentityManager::OnAccessTokenRequested(
const std::string& account_id,
const std::string& consumer_id,
......
......@@ -148,6 +148,10 @@ class IdentityManager : public SigninManagerBase::Observer,
virtual void OnSetAccountsInCookieCompleted(
const GoogleServiceAuthError& error) {}
// Called when the Gaia cookie has been deleted explicitly by a user
// action, e.g. from the settings or by an extension.
virtual void OnAccountsCookieDeletedByUserAction() {}
// Called before a batch of refresh token state changes is started.
virtual void OnStartBatchOfRefreshTokenStateChanges() {}
......@@ -482,6 +486,7 @@ class IdentityManager : public SigninManagerBase::Observer,
const GoogleServiceAuthError& error) override;
void OnSetAccountsInCookieCompleted(
const GoogleServiceAuthError& error) override;
void OnGaiaCookieDeletedByUserAction() override;
// OAuth2TokenService::DiagnosticsObserver:
void OnAccessTokenRequested(
......
......@@ -93,6 +93,43 @@ class CustomFakeProfileOAuth2TokenService
base::OnceClosure on_access_token_invalidated_callback_;
};
class FakeCookieManager : public network::mojom::CookieManager {
public:
void SetCanonicalCookie(const net::CanonicalCookie& cookie,
bool secure_source,
bool modify_http_only,
SetCanonicalCookieCallback callback) override {
std::move(callback).Run(false);
}
void GetAllCookies(GetAllCookiesCallback callback) override {}
void GetCookieList(const GURL& url,
const net::CookieOptions& cookie_options,
GetCookieListCallback callback) override {}
void DeleteCanonicalCookie(const net::CanonicalCookie& cookie,
DeleteCanonicalCookieCallback callback) override {}
void DeleteCookies(network::mojom::CookieDeletionFilterPtr filter,
DeleteCookiesCallback callback) override {}
void AddCookieChangeListener(
const GURL& url,
const std::string& name,
network::mojom::CookieChangeListenerPtr listener) override {
listener_ = std::move(listener);
}
void AddGlobalChangeListener(
network::mojom::CookieChangeListenerPtr notification_pointer) override {}
void CloneInterface(
network::mojom::CookieManagerRequest new_interface) override {}
void FlushCookieStore(FlushCookieStoreCallback callback) override {}
void SetContentSettings(
const std::vector<::ContentSettingPatternSource>& settings) override {}
void SetForceKeepSessionState() override {}
void BlockThirdPartyCookies(bool block) override {}
private:
// The observer receiving change notifications.
network::mojom::CookieChangeListenerPtr listener_;
};
class AccountTrackerServiceForTest : public AccountTrackerService {
public:
void SetAccountInfoFromUserInfo(const std::string& account_id,
......@@ -219,6 +256,9 @@ class TestIdentityManagerObserver : IdentityManager::Observer {
base::OnceClosure callback) {
on_primary_account_signin_failed_callback_ = std::move(callback);
}
void set_on_cookie_deleted_by_user_callback(base::OnceClosure callback) {
on_cookie_deleted_by_user_callback_ = std::move(callback);
}
const AccountInfo& primary_account_from_set_callback() {
return primary_account_from_set_callback_;
......@@ -368,6 +408,9 @@ class TestIdentityManagerObserver : IdentityManager::Observer {
const GoogleServiceAuthError& error) override {
error_from_set_accounts_in_cookie_completed_callback_ = error;
}
void OnAccountsCookieDeletedByUserAction() override {
std::move(on_cookie_deleted_by_user_callback_).Run();
}
void OnStartBatchOfRefreshTokenStateChanges() override {
EXPECT_FALSE(is_inside_batch_);
......@@ -395,6 +438,7 @@ class TestIdentityManagerObserver : IdentityManager::Observer {
base::OnceClosure on_primary_account_cleared_callback_;
base::OnceClosure on_primary_account_signin_failed_callback_;
base::OnceClosure on_refresh_token_updated_callback_;
base::OnceClosure on_cookie_deleted_by_user_callback_;
base::RepeatingCallback<void(const std::string&)>
on_refresh_token_removed_callback_;
base::OnceClosure on_error_state_of_refresh_token_updated_callback_;
......@@ -500,8 +544,11 @@ class IdentityManagerTest : public testing::Test {
SigninManagerBase::RegisterProfilePrefs(pref_service_.registry());
SigninManagerBase::RegisterPrefs(pref_service_.registry());
signin_client_.set_cookie_manager(std::make_unique<FakeCookieManager>());
account_tracker_.Initialize(&pref_service_, base::FilePath());
gaia_cookie_manager_service_.InitCookieListener();
RecreateSigninAndIdentityManager(
signin::AccountConsistencyMethod::kDisabled,
SigninManagerSetup::kWithAuthenticatedAccout);
......@@ -608,6 +655,13 @@ class IdentityManagerTest : public testing::Test {
consumer->OnMergeSessionFailure(error);
}
void SimulateCookieDeletedByUser(
network::mojom::CookieChangeListener* listener,
const net::CanonicalCookie& cookie) {
listener->OnCookieChange(cookie,
network::mojom::CookieChangeCause::EXPLICIT);
}
void SimulateOAuthMultiloginFinished(GaiaAuthConsumer* consumer,
const OAuthMultiloginResult& result) {
consumer->OnOAuthMultiloginFinished(result);
......@@ -2181,6 +2235,49 @@ TEST_F(IdentityManagerTest,
error);
}
TEST_F(IdentityManagerTest, CallbackSentOnAccountsCookieDeletedByUserAction) {
const char kTestAccountId[] = "account_id";
const char kTestAccountId2[] = "account_id2";
const std::vector<std::string> account_ids = {kTestAccountId,
kTestAccountId2};
// Needed to insert request in the queue.
gaia_cookie_manager_service()->SetAccountsInCookie(account_ids,
gaia::GaiaSource::kChrome);
// Sample success cookie response.
std::string data =
R"()]}'
{
"status": "OK",
"cookies":[
{
"name":"APISID",
"value":"vAlUe1",
"domain":".google.com",
"path":"/",
"isSecure":true,
"isHttpOnly":false,
"priority":"HIGH",
"maxAge":63070000
}
]
}
)";
OAuthMultiloginResult result(data);
SimulateOAuthMultiloginFinished(gaia_cookie_manager_service(), result);
base::RunLoop().RunUntilIdle();
base::RunLoop run_loop;
identity_manager_observer()->set_on_cookie_deleted_by_user_callback(
run_loop.QuitClosure());
const std::vector<net::CanonicalCookie>& cookies = result.cookies();
SimulateCookieDeletedByUser(gaia_cookie_manager_service(), cookies[0]);
run_loop.Run();
}
TEST_F(IdentityManagerTest,
BatchChangeObserversAreNotifiedOnCredentialsUpdate) {
signin_manager()->SetAuthenticatedAccountInfo(kTestGaiaId, kTestEmail);
......
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