1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_GLIC_HOST_AUTH_CONTROLLER_H_
#define CHROME_BROWSER_GLIC_HOST_AUTH_CONTROLLER_H_
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_observation.h"
#include "base/time/time.h"
#include "chrome/browser/glic/host/glic.mojom.h"
#include "components/signin/public/identity_manager/identity_manager.h"
class Profile;
namespace glic {
class GlicCookieSynchronizer;
// Decides when to refresh sign-in cookies for the webview.
class AuthController : public signin::IdentityManager::Observer {
public:
AuthController(Profile* profile,
signin::IdentityManager* identity_manager,
bool use_for_fre);
~AuthController() override;
// Called before the glic web client is loaded. Tries to sync cookies to the
// webview partition, and then calls `callback`. Informs the caller whether
// cookies could be synced.
void CheckAuthBeforeLoad(
base::OnceCallback<void(mojom::PrepareForClientResult)> callback);
// Called before the glic window is shown. Returns true if the glic window
// should be shown. Returns false if the login page is shown instead, in which
// case the glic window should not be shown.
bool CheckAuthBeforeShowSync(base::OnceClosure after_signin);
// Sync cookies, even if it appears as though a sync is not required.
void ForceSyncCookies(base::OnceCallback<void(bool)> callback);
// Show the sign-in page. `after_signin` will be called after the user has
// signed in. It will not be called if the user cancels the sign-in, or the
// sign-in doesn't happen before:
// * ShowReauthForAccount is called again
// * CheckAuthBeforeShow is called
// * The AuthController is destroyed
// * Too much time has passed (5 minutes).
// * OnGlicWindowOpened is called.
// TODO(crbug.com/406529330): Track sign-in flow correctly.
void ShowReauthForAccount(base::OnceClosure after_signin);
void OnGlicWindowOpened();
bool RequiresSignIn() const;
void SetCookieSynchronizerForTesting(
std::unique_ptr<GlicCookieSynchronizer> synchronizer);
GlicCookieSynchronizer* GetCookieSynchronizerForTesting() const {
return cookie_synchronizer_.get();
}
// signin::IdentityManager::Observer implementation.
void OnErrorStateOfRefreshTokenUpdatedForAccount(
const CoreAccountInfo& account_info,
const GoogleServiceAuthError& error,
signin_metrics::SourceForRefreshTokenOperation token_operation_source)
override;
void OnPrimaryAccountChanged(
const signin::PrimaryAccountChangeEvent& event_details) override;
void OnRefreshTokenUpdatedForAccount(
const CoreAccountInfo& account_info) override;
private:
enum class TokenState {
kUnknownError,
kRequiresSignIn,
kOk,
};
TokenState GetTokenState() const;
base::WeakPtr<AuthController> GetWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
void SyncCookiesIfRequired(base::OnceCallback<void(bool)> callback);
void CookieSyncDone(base::OnceCallback<void(bool)> callback,
bool sync_success);
void CookieSyncBeforeLoadDone(
base::OnceCallback<void(mojom::PrepareForClientResult)> callback,
bool sync_success);
raw_ptr<Profile> profile_;
base::OnceClosure after_signin_callback_;
base::TimeTicks after_signin_callback_expiration_time_;
raw_ptr<signin::IdentityManager> identity_manager_;
std::optional<base::TimeTicks> last_cookie_sync_time_;
std::unique_ptr<GlicCookieSynchronizer> cookie_synchronizer_;
base::ScopedObservation<signin::IdentityManager,
signin::IdentityManager::Observer>
observation_;
base::WeakPtrFactory<AuthController> weak_ptr_factory_{this};
};
} // namespace glic
#endif // CHROME_BROWSER_GLIC_HOST_AUTH_CONTROLLER_H_
|