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
|
// 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.
#include "components/omnibox/browser/aim_eligibility_service.h"
#include <memory>
#include <string>
#include "base/base64.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/memory/scoped_refptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/strcat.h"
#include "base/strings/string_util.h"
#include "components/omnibox/browser/aim_eligibility_service_observer.h"
#include "components/omnibox/browser/omnibox_prefs.h"
#include "components/omnibox/common/omnibox_feature_configs.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/search/search.h"
#include "components/search_engines/template_url_service.h"
#include "net/base/load_flags.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/simple_url_loader.h"
#include "third_party/omnibox_proto/aim_eligibility_response.pb.h"
#include "url/gurl.h"
namespace {
// If disabled, AIM is completely turned off (kill switch).
BASE_FEATURE(kAimEnabled, "AimEnabled", base::FEATURE_ENABLED_BY_DEFAULT);
// If enabled, uses the server response for AIM eligibility for all locales.
BASE_FEATURE(kAimServerEligibilityEnabled,
"AimServerEligibilityEnabled",
base::FEATURE_DISABLED_BY_DEFAULT);
// If enabled, uses the server response for AIM eligibility for English locales.
// Has no effect if kAimServerEligibilityEnabled is enabled.
BASE_FEATURE(kAimServerEligibilityEnabledEn,
"AimServerEligibilityEnabledEn",
base::FEATURE_DISABLED_BY_DEFAULT);
// For recording UMA metrics. These aren't strictly omnibox-only, but omnibox is
// a major consumer of `AimEligibilityService`, and the few metrics here don't
// warrant creating a new metric namespace.
// The status of the server request. See `ServerRequestStatus`.
static constexpr char kUmaServerRequestStatusHistogramName[] =
"Omnibox.AimEligibility.ServerRequestStatus";
// Which AIM features were eligible according to the server request.
static constexpr char kUmaServerEligibilityHistogramPrefix[] =
"Omnibox.AimEligibility.ServerEligibility.";
// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
// LINT.IfChange(ServerAimEligibilityRequestStatus)
enum class ServerRequestStatus {
kSent = 0,
kErrorResponse = 1,
kFailedToParse = 2,
kSuccess = 3,
kMaxValue = kSuccess,
};
// LINT.ThenChange(//tools/metrics/histograms/metadata/omnibox/enums.xml:ServerAimEligibilityRequestStatus)
static constexpr char kRequestPath[] = "/async/folae";
static constexpr char kRequestQuery[] = "async=_fmt:pb";
// Returns the request URL or an empty GURL if a valid URL cannot be created;
// e.g., Google is not the default search provider.
GURL GetRequestUrl(const TemplateURLService& template_url_service) {
if (!search::DefaultSearchProviderIsGoogle(&template_url_service)) {
return GURL();
}
GURL base_gurl(template_url_service.search_terms_data().GoogleBaseURLValue());
if (!base_gurl.is_valid()) {
return GURL();
}
GURL::Replacements replacements;
replacements.SetPathStr(kRequestPath);
replacements.SetQueryStr(kRequestQuery);
return base_gurl.ReplaceComponents(replacements);
}
const net::NetworkTrafficAnnotationTag kRequestTrafficAnnotation =
net::DefineNetworkTrafficAnnotation("aim_eligibility_fetch", R"(
semantics {
sender: "Chrome AI Mode Eligibility Service"
description:
"Retrieves the set of AI Mode features the client is eligible for "
"from the server."
trigger:
"Requests are made on startup, when user's profile state changes, "
"and periodically while Chrome is running."
user_data {
type: NONE
}
data:
"No request body is sent; this is a GET request with no query params."
destination: GOOGLE_OWNED_SERVICE
internal {
contacts { email: "chrome-desktop-search@google.com" }
}
last_reviewed: "2025-08-06"
}
policy {
cookies_allowed: YES
cookies_store: "user"
setting: "Coupled to Google default search."
policy_exception_justification:
"Not gated by policy. Setting AIModeSetting to '1' prevents the "
"response from being used. But Google Chrome still makes the "
"requests and saves the response to disk so that it's available when "
"the policy is unset."
})");
} // namespace
// static
void AimEligibilityService::RegisterProfilePrefs(PrefRegistrySimple* registry) {
registry->RegisterStringPref(kResponsePrefName, "");
}
AimEligibilityService::AimEligibilityService(
PrefService& pref_service,
TemplateURLService& template_url_service,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory)
: pref_service_(pref_service),
template_url_service_(template_url_service),
url_loader_factory_(url_loader_factory) {
ReadFromPref();
// TODO(crbug.com/436898763): Call `StartServerEligibilityRequest()` to
// refresh the server response when user sign-in state changes.
StartServerEligibilityRequest();
}
AimEligibilityService::~AimEligibilityService() = default;
bool AimEligibilityService::IsCountry(const std::string& country) const {
// Country codes are in lowercase ISO 3166-1 alpha-2 format; e.g., us, br, in.
// See components/variations/service/variations_service.h
return GetCountryCode() == country;
}
bool AimEligibilityService::IsLanguage(const std::string& language) const {
// Locale follows BCP 47 format; e.g., en-US, fr-FR, ja-JP.
// See ui/base/l10n/l10n_util.h
return base::StartsWith(GetLocale(), language, base::CompareCase::SENSITIVE);
}
void AimEligibilityService::AddObserver(
AimEligibilityServiceObserver* observer) {
observers_.AddObserver(observer);
}
void AimEligibilityService::RemoveObserver(
AimEligibilityServiceObserver* observer) {
observers_.RemoveObserver(observer);
}
bool AimEligibilityService::IsServerEligibilityEnabled() const {
return base::FeatureList::IsEnabled(kAimServerEligibilityEnabled) ||
(base::FeatureList::IsEnabled(kAimServerEligibilityEnabledEn) &&
IsLanguage("en"));
}
bool AimEligibilityService::IsAimLocallyEligible() const {
// Kill switch: If AIM is completely disabled, return false.
if (!base::FeatureList::IsEnabled(kAimEnabled)) {
return false;
}
// Always check Google DSE and Policy requirements.
if (!search::DefaultSearchProviderIsGoogle(&template_url_service_.get()) ||
!omnibox::IsAimAllowedByPolicy(&pref_service_.get())) {
return false;
}
return true;
}
bool AimEligibilityService::IsAimEligible() const {
// Check local eligibility first.
if (!IsAimLocallyEligible()) {
return false;
}
// Conditionally check server response eligibility requirement.
if (IsServerEligibilityEnabled()) {
return most_recent_response_.is_eligible();
}
return true;
}
void AimEligibilityService::NotifyObservers() const {
for (auto& observer : observers_) {
observer.OnAimEligibilityChanged();
}
}
bool AimEligibilityService::ParseResponseString(
const std::string& response_string) {
// Parse into a temporary variable 1st so that if parsing fails,
// `most_recent_response_` isn't cleared.
omnibox::AimEligibilityResponse response_proto;
if (!response_proto.ParseFromString(response_string)) {
return false;
}
most_recent_response_ = response_proto;
return true;
}
void AimEligibilityService::WriteToPref(
const std::string& response_string) const {
// Nothing should be written to the prefs if AIM is disabled.
CHECK(base::FeatureList::IsEnabled(kAimEnabled));
// Do not write to the prefs if server eligibility checking is not enabled.
if (!IsServerEligibilityEnabled()) {
return;
}
pref_service_->SetString(kResponsePrefName,
base::Base64Encode(response_string));
}
void AimEligibilityService::ReadFromPref() {
const std::string& read_string = pref_service_->GetString(kResponsePrefName);
std::string decoded;
if (base::Base64Decode(read_string, &decoded)) {
ParseResponseString(decoded);
}
}
void AimEligibilityService::StartServerEligibilityRequest() {
// Don't make server requests if AIM is disabled.
if (!base::FeatureList::IsEnabled(kAimEnabled)) {
return;
}
// URLLoaderFactory may be null in tests.
if (!url_loader_factory_) {
return;
}
// Request URL may be invalid.
GURL request_url = GetRequestUrl(template_url_service_.get());
if (!request_url.is_valid()) {
return;
}
std::unique_ptr<network::ResourceRequest> request =
std::make_unique<network::ResourceRequest>();
request->url = request_url;
request->credentials_mode = network::mojom::CredentialsMode::kInclude;
request->load_flags = net::LOAD_DO_NOT_SAVE_COOKIES;
// Set the SiteForCookies to the request URL's site to avoid cookie blocking.
request->site_for_cookies = net::SiteForCookies::FromUrl(request->url);
std::unique_ptr<network::SimpleURLLoader> loader =
network::SimpleURLLoader::Create(std::move(request),
kRequestTrafficAnnotation);
base::UmaHistogramEnumeration(kUmaServerRequestStatusHistogramName,
ServerRequestStatus::kSent);
loader->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
url_loader_factory_.get(),
base::BindOnce(&AimEligibilityService::OnServerEligibilityResponse,
weak_factory_.GetWeakPtr(), std::move(loader)));
}
void AimEligibilityService::OnServerEligibilityResponse(
std::unique_ptr<network::SimpleURLLoader> loader,
std::unique_ptr<std::string> response_string) {
// TODO(crbug.com/436900259): Add UMA metrics for whether the response
// returned 200, was parsed successfully, and which features were eligible.
// This will let us know how watered down UMA and finch are compared due to
// mismatched server eligibility criteria and estimate the actual population
// size.
if (!response_string) {
base::UmaHistogramEnumeration(kUmaServerRequestStatusHistogramName,
ServerRequestStatus::kErrorResponse);
return;
}
if (!ParseResponseString(*response_string)) {
base::UmaHistogramEnumeration(kUmaServerRequestStatusHistogramName,
ServerRequestStatus::kFailedToParse);
return;
}
base::UmaHistogramEnumeration(kUmaServerRequestStatusHistogramName,
ServerRequestStatus::kSuccess);
base::UmaHistogramBoolean(
base::StrCat({kUmaServerEligibilityHistogramPrefix, "is_eligible"}),
most_recent_response_.is_eligible());
WriteToPref(*response_string);
NotifyObservers();
}
|