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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/glic/glic_enabling.h"
#include "base/command_line.h"
#include "base/feature_list.h"
#include "chrome/browser/glic/glic_pref_names.h"
#include "chrome/browser/glic/glic_user_status_code.h"
#include "chrome/browser/glic/glic_user_status_fetcher.h"
#include "chrome/browser/glic/host/glic.mojom.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_attributes_storage.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/signin/public/identity_manager/account_info.h"
#include "components/signin/public/identity_manager/identity_manager.h"
namespace glic {
GlicEnabling::ProfileEnablement GlicEnabling::EnablementForProfile(
Profile* profile) {
ProfileEnablement result;
if (!IsEnabledByFlags()) {
result.feature_disabled = true;
return result;
}
if (!profile || !profile->IsRegularProfile()) {
result.not_regular_profile = true;
return result;
}
// Certain checks are bypassed if --glic-dev is passed.
auto* command_line = base::CommandLine::ForCurrentProcess();
if (!command_line->HasSwitch(::switches::kGlicDev)) {
if (!base::FeatureList::IsEnabled(features::kGlicRollout) &&
!IsEligibleForGlicTieredRollout(profile)) {
result.not_rolled_out = true;
}
signin::IdentityManager* identity_manager =
IdentityManagerFactory::GetForProfile(profile);
CHECK(identity_manager);
AccountInfo primary_account =
identity_manager->FindExtendedAccountInfoByAccountId(
identity_manager->GetPrimaryAccountId(
signin::ConsentLevel::kSignin));
// Not having a primary account is considered ineligible, as is kUnknown
// for the required account capability.
if (primary_account.IsEmpty() ||
primary_account.capabilities.can_use_model_execution_features() !=
signin::Tribool::kTrue) {
result.primary_account_not_capable = true;
}
}
if (profile->GetPrefs()->GetInteger(::prefs::kGeminiSettings) !=
static_cast<int>(glic::prefs::SettingsPolicyState::kEnabled)) {
result.disallowed_by_chrome_policy = true;
}
if (base::FeatureList::IsEnabled(features::kGlicUserStatusCheck)) {
if (auto cached_user_status =
GlicUserStatusFetcher::GetCachedUserStatus(profile);
cached_user_status.has_value()) {
switch (cached_user_status->user_status_code) {
case UserStatusCode::DISABLED_BY_ADMIN:
result.disallowed_by_remote_admin = true;
break;
case UserStatusCode::DISABLED_OTHER:
result.disallowed_by_remote_other = true;
break;
case UserStatusCode::ENABLED:
break;
case UserStatusCode::SERVER_UNAVAILABLE:
// We never cache SERVER_UNAVAILABLE.
NOTREACHED();
}
}
}
if (!HasConsentedForProfile(profile)) {
result.not_consented = true;
}
return result;
}
bool GlicEnabling::IsEnabledByFlags() {
// Check that the feature flags are enabled.
return base::FeatureList::IsEnabled(features::kGlic) &&
features::IsTabSearchMoving();
}
bool GlicEnabling::IsProfileEligible(const Profile* profile) {
// Glic is supported only in regular profiles, i.e. disable in incognito,
// guest, system profile, etc.
return IsEnabledByFlags() && profile && profile->IsRegularProfile();
}
bool GlicEnabling::IsEnabledForProfile(Profile* profile) {
return EnablementForProfile(profile).IsEnabled();
}
bool GlicEnabling::HasConsentedForProfile(Profile* profile) {
return profile->GetPrefs()->GetInteger(prefs::kGlicCompletedFre) ==
static_cast<int>(prefs::FreStatus::kCompleted);
}
bool GlicEnabling::IsEnabledAndConsentForProfile(Profile* profile) {
return EnablementForProfile(profile).IsEnabledAndConsented();
}
bool GlicEnabling::DidDismissForProfile(Profile* profile) {
return profile->GetPrefs()->GetInteger(glic::prefs::kGlicCompletedFre) ==
static_cast<int>(prefs::FreStatus::kIncomplete);
}
bool GlicEnabling::IsReadyForProfile(Profile* profile) {
return GetProfileReadyState(profile) == mojom::ProfileReadyState::kReady;
}
mojom::ProfileReadyState GlicEnabling::GetProfileReadyState(Profile* profile) {
if (!IsEnabledAndConsentForProfile(profile)) {
return mojom::ProfileReadyState::kUnknownError;
}
auto* command_line = base::CommandLine::ForCurrentProcess();
if (command_line->HasSwitch(::switches::kGlicAutomation)) {
return mojom::ProfileReadyState::kReady;
}
signin::IdentityManager* identity_manager =
IdentityManagerFactory::GetForProfile(profile);
// Check that profile is not currently paused.
CoreAccountInfo core_account_info =
identity_manager->GetPrimaryAccountInfo(signin::ConsentLevel::kSignin);
if (core_account_info.IsEmpty()) {
return mojom::ProfileReadyState::kUnknownError;
}
if (identity_manager->HasAccountWithRefreshTokenInPersistentErrorState(
core_account_info.account_id)) {
return mojom::ProfileReadyState::kSignInRequired;
}
return mojom::ProfileReadyState::kReady;
}
bool GlicEnabling::IsEligibleForGlicTieredRollout(Profile* profile) {
return base::FeatureList::IsEnabled(features::kGlicTieredRollout) &&
profile->GetPrefs()->GetBoolean(prefs::kGlicRolloutEligibility);
}
bool GlicEnabling::ShouldShowSettingsPage(Profile* profile) {
return EnablementForProfile(profile).ShouldShowSettingsPage();
}
void GlicEnabling::OnGlicSettingsPolicyChanged() {
glic::prefs::SettingsPolicyState updated_gemini_settings_value =
glic::prefs::SettingsPolicyState{
profile_->GetPrefs()->GetInteger(::prefs::kGeminiSettings)};
// If the policy changed from either not set or Disabled to Enabled, trigger a
// rpc fetch to update the possible user status change sooner.
if ((!cached_gemini_settings_value_.has_value() ||
cached_gemini_settings_value_.value() ==
glic::prefs::SettingsPolicyState::kDisabled) &&
updated_gemini_settings_value ==
(glic::prefs::SettingsPolicyState::kEnabled)) {
if (glic_user_status_fetcher_) {
glic_user_status_fetcher_->UpdateUserStatus();
}
}
// Update the stored value for the next comparison.
cached_gemini_settings_value_ = updated_gemini_settings_value;
// Update the overall enabled status as the policy has changed.
UpdateEnabledStatus();
}
GlicEnabling::GlicEnabling(Profile* profile,
ProfileAttributesStorage* profile_attributes_storage)
: profile_(profile),
profile_attributes_storage_(profile_attributes_storage) {
pref_registrar_.Init(profile_->GetPrefs());
pref_registrar_.Add(
::prefs::kGeminiSettings,
base::BindRepeating(&GlicEnabling::OnGlicSettingsPolicyChanged,
base::Unretained(this)));
pref_registrar_.Add(prefs::kGlicCompletedFre,
base::BindRepeating(&GlicEnabling::UpdateConsentStatus,
base::Unretained(this)));
if (!base::FeatureList::IsEnabled(features::kGlicRollout) &&
base::FeatureList::IsEnabled(features::kGlicTieredRollout)) {
pref_registrar_.Add(
prefs::kGlicRolloutEligibility,
base::BindRepeating(&GlicEnabling::OnTieredRolloutStatusMaybeChanged,
base::Unretained(this)));
}
signin::IdentityManager* identity_manager =
IdentityManagerFactory::GetForProfile(profile);
CHECK(identity_manager);
identity_manager_observation_.Observe(identity_manager);
if (base::FeatureList::IsEnabled(features::kGlicUserStatusCheck)) {
glic_user_status_fetcher_ = std::make_unique<GlicUserStatusFetcher>(
profile_, base::BindRepeating(&GlicEnabling::UpdateEnabledStatus,
base::Unretained(this)));
cached_gemini_settings_value_ = glic::prefs::SettingsPolicyState{
profile_->GetPrefs()->GetInteger(::prefs::kGeminiSettings)};
}
}
GlicEnabling::~GlicEnabling() = default;
bool GlicEnabling::IsAllowed() {
return IsEnabledForProfile(profile_);
}
bool GlicEnabling::HasConsented() {
return HasConsentedForProfile(profile_);
}
base::CallbackListSubscription GlicEnabling::RegisterAllowedChanged(
EnableChangedCallback callback) {
return enable_changed_callback_list_.Add(std::move(callback));
}
base::CallbackListSubscription GlicEnabling::RegisterOnConsentChanged(
ConsentChangedCallback callback) {
return consent_changed_callback_list_.Add(std::move(callback));
}
base::CallbackListSubscription GlicEnabling::RegisterOnShowSettingsPageChanged(
ShowSettingsPageChangedCallback callback) {
return show_settings_page_changed_callback_list_.Add(std::move(callback));
}
void GlicEnabling::UpdateUserStatus(
const signin::PrimaryAccountChangeEvent& event_details) {
if (!glic_user_status_fetcher_) {
return;
}
switch (event_details.GetEventTypeFor(signin::ConsentLevel::kSignin)) {
case signin::PrimaryAccountChangeEvent::Type::kSet:
if (glic_user_status_fetcher_) {
glic_user_status_fetcher_->UpdateUserStatus();
}
break;
// Ignore until primary account is set.
case signin::PrimaryAccountChangeEvent::Type::kNone:
break;
case signin::PrimaryAccountChangeEvent::Type::kCleared:
if (glic_user_status_fetcher_) {
glic_user_status_fetcher_->InvalidateCachedStatus();
glic_user_status_fetcher_->CancelUserStatusUpdateIfNeeded();
}
break;
}
}
void GlicEnabling::OnPrimaryAccountChanged(
const signin::PrimaryAccountChangeEvent& event_details) {
UpdateEnabledStatus();
UpdateUserStatus(event_details);
}
void GlicEnabling::OnExtendedAccountInfoUpdated(const AccountInfo& info) {
UpdateEnabledStatus();
}
void GlicEnabling::OnExtendedAccountInfoRemoved(const AccountInfo& info) {
UpdateEnabledStatus();
}
void GlicEnabling::OnRefreshTokensLoaded() {
UpdateEnabledStatus();
}
// It happens that when the request is sent upon sign-in, the refresh token is
// not available yet, the request would hence be cancelled. In such cases, we
// re-send the request when refresh token becomes available.
void GlicEnabling::OnRefreshTokenUpdatedForAccount(
const CoreAccountInfo& account_info) {
if (glic_user_status_fetcher_) {
glic_user_status_fetcher_->UpdateUserStatusIfNeeded();
}
}
void GlicEnabling::OnRefreshTokenRemovedForAccount(
const CoreAccountId& account_id) {
UpdateEnabledStatus();
}
void GlicEnabling::OnTieredRolloutStatusMaybeChanged() {
UpdateEnabledStatus();
}
void GlicEnabling::OnErrorStateOfRefreshTokenUpdatedForAccount(
const CoreAccountInfo& account_info,
const GoogleServiceAuthError& error,
signin_metrics::SourceForRefreshTokenOperation token_operation_source) {
// Check that the account info here is the same as the primary account, and
// ignore all events that are not about the primary account.
if (identity_manager_observation_.GetSource()->GetPrimaryAccountInfo(
signin::ConsentLevel::kSignin) != account_info) {
return;
}
UpdateEnabledStatus();
if (glic_user_status_fetcher_) {
glic_user_status_fetcher_->CancelUserStatusUpdateIfNeeded();
}
}
void GlicEnabling::OnIdentityManagerShutdown(
signin::IdentityManager* identity_manager) {
if (glic_user_status_fetcher_) {
glic_user_status_fetcher_->CancelUserStatusUpdateIfNeeded();
}
}
void GlicEnabling::UpdateEnabledStatus() {
if (ProfileAttributesEntry* entry =
profile_attributes_storage_->GetProfileAttributesWithPath(
profile_->GetPath())) {
entry->SetIsGlicEligible(IsAllowed());
}
enable_changed_callback_list_.Notify();
show_settings_page_changed_callback_list_.Notify();
}
void GlicEnabling::UpdateConsentStatus() {
consent_changed_callback_list_.Notify();
show_settings_page_changed_callback_list_.Notify();
}
} // namespace glic
|