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
|
// 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/regional_capabilities/regional_capabilities_service.h"
#include <optional>
#include <vector>
#include "base/callback_list.h"
#include "base/check_is_test.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/metrics/histogram_functions.h"
#include "components/country_codes/country_codes.h"
#include "components/prefs/pref_service.h"
#include "components/regional_capabilities/regional_capabilities_country_id.h"
#include "components/regional_capabilities/regional_capabilities_metrics.h"
#include "components/regional_capabilities/regional_capabilities_prefs.h"
#include "components/regional_capabilities/regional_capabilities_switches.h"
#include "components/regional_capabilities/regional_capabilities_utils.h"
#include "third_party/search_engines_data/resources/definitions/prepopulated_engines.h"
#if BUILDFLAG(IS_ANDROID)
#include "base/android/scoped_java_ref.h"
// Must come after all headers that specialize FromJniType() / ToJniType().
#include "components/regional_capabilities/android/jni_headers/RegionalCapabilitiesService_jni.h"
#endif
using ::country_codes::CountryId;
namespace regional_capabilities {
namespace {
constexpr char kUnknownCountryIdStored[] =
"Search.ChoiceDebug.UnknownCountryIdStored";
// LINT.IfChange(UnknownCountryIdStored)
enum class UnknownCountryIdStored {
kValidCountryId = 0,
kDontClearInvalidCountry = 1,
kClearedPref = 2,
kMaxValue = kClearedPref,
};
// LINT.ThenChange(/tools/metrics/histograms/metadata/search/enums.xml:UnknownCountryIdStored)
// Helper to make it possible to check for the synchronous completion of the
// `RegionalCapabilitiesService::Client::FetchCountryId()` call.
class ScopedCountryIdReceiver {
public:
base::OnceCallback<void(CountryId)> GetCaptureCallback() {
return base::BindOnce(
[](base::WeakPtr<ScopedCountryIdReceiver> receiver,
CountryId country_id) {
if (receiver) {
receiver->received_country_ = country_id;
}
},
weak_ptr_factory_.GetWeakPtr());
}
std::optional<CountryId> received_country() { return received_country_; }
private:
std::optional<CountryId> received_country_;
base::WeakPtrFactory<ScopedCountryIdReceiver> weak_ptr_factory_{this};
};
// Returns a callback that dispatches the incoming value to `callback1` and
// `callback2`. Always forwards the incoming value to each of them, provided
// they're not null.
RegionalCapabilitiesService::Client::CountryIdCallback DispatchCountryId(
RegionalCapabilitiesService::Client::CountryIdCallback callback1,
RegionalCapabilitiesService::Client::CountryIdCallback callback2) {
return base::BindOnce(
[](RegionalCapabilitiesService::Client::CountryIdCallback cb1,
RegionalCapabilitiesService::Client::CountryIdCallback cb2,
CountryId incoming_country_id) {
if (cb1) {
std::move(cb1).Run(incoming_country_id);
}
if (cb2) {
std::move(cb2).Run(incoming_country_id);
}
},
std::move(callback1), std::move(callback2));
}
CountryId SelectCountryId(std::optional<CountryId> persisted_country,
CountryId current_country) {
if (!persisted_country.has_value() && !current_country.IsValid()) {
RecordLoadedCountrySource(LoadedCountrySource::kNoneAvailable);
return CountryId();
}
if (!persisted_country.has_value()) {
// We deliberately don't check `persisted_country` validity here. This
// means it's still possible below that we might end up returning an
// invalid country in the case where we obtained that invalid value
// from the preferences. crbug.com/399878483 and crbug.com/399879272
// should contribute to getting rid of that issue.
CHECK(current_country.IsValid());
RecordLoadedCountrySource(LoadedCountrySource::kCurrentOnly);
return current_country;
}
LoadedCountrySource loaded_country_source;
if (!current_country.IsValid()) {
CHECK(persisted_country.has_value());
loaded_country_source = LoadedCountrySource::kPersistedOnly;
} else if (current_country == persisted_country.value()) {
loaded_country_source = LoadedCountrySource::kBothMatch;
} else {
loaded_country_source = LoadedCountrySource::kPersistedPreferred;
}
RecordLoadedCountrySource(loaded_country_source);
return persisted_country.value();
}
} // namespace
RegionalCapabilitiesService::RegionalCapabilitiesService(
PrefService& profile_prefs,
std::unique_ptr<Client> regional_capabilities_client)
: profile_prefs_(profile_prefs),
client_(std::move(regional_capabilities_client)) {
CHECK(client_);
}
RegionalCapabilitiesService::~RegionalCapabilitiesService() {
#if BUILDFLAG(IS_ANDROID)
DestroyJavaObject();
#endif
}
CountryId RegionalCapabilitiesService::GetCountryIdInternal() {
std::optional<SearchEngineCountryOverride> country_override =
GetSearchEngineCountryOverride();
if (country_override.has_value()) {
if (std::holds_alternative<CountryId>(country_override.value())) {
return std::get<CountryId>(country_override.value());
}
return CountryId();
}
if (!country_id_cache_.has_value()) {
InitializeCountryIdCache();
}
return country_id_cache_.value();
}
CountryIdHolder RegionalCapabilitiesService::GetCountryId() {
return CountryIdHolder(GetCountryIdInternal());
}
std::vector<const TemplateURLPrepopulateData::PrepopulatedEngine*>
RegionalCapabilitiesService::GetRegionalPrepopulatedEngines() {
if (HasSearchEngineCountryListOverride()) {
auto country_override = std::get<SearchEngineCountryListOverride>(
GetSearchEngineCountryOverride().value());
switch (country_override) {
case SearchEngineCountryListOverride::kEeaAll:
return GetAllEeaRegionPrepopulatedEngines();
case SearchEngineCountryListOverride::kEeaDefault:
return GetDefaultPrepopulatedEngines();
}
}
return GetPrepopulatedEngines(GetCountryIdInternal(), profile_prefs_.get());
}
bool RegionalCapabilitiesService::IsInEeaCountry() {
return regional_capabilities::IsEeaCountry(GetCountryIdInternal());
}
void RegionalCapabilitiesService::InitializeCountryIdCache() {
std::optional<CountryId> persisted_country_id = GetPersistedCountryId();
// Fetches the device country using `Client::FetchCountryId()`. Upon
// completion, makes it available through `country_id_receiver` and also
// forwards it to `completion_callback`.
ScopedCountryIdReceiver country_id_receiver;
client_->FetchCountryId(DispatchCountryId(
// Callback to a weak_ptr, and like `country_id_receiver`, scoped to the
// function only.
country_id_receiver.GetCaptureCallback(),
// Callback scoped to the lifetime of the service.
base::BindOnce(&RegionalCapabilitiesService::TrySetPersistedCountryId,
weak_ptr_factory_.GetWeakPtr())));
CountryId current_device_country =
country_id_receiver.received_country().value_or(CountryId());
bool is_device_country_from_fallback = false;
if (!current_device_country.IsValid()) {
// The initialization failed or did not complete synchronously. Use the
// fallback value and don't persist it. If the fetch completes later, the
// persisted country will be picked up at the next startup.
current_device_country = client_->GetFallbackCountryId();
is_device_country_from_fallback = true;
}
RecordVariationsCountryMatching(client_->GetVariationsLatestCountryId(),
persisted_country_id.value_or(CountryId()),
current_device_country,
is_device_country_from_fallback);
country_id_cache_ =
SelectCountryId(persisted_country_id, current_device_country);
}
void RegionalCapabilitiesService::ClearCountryIdCacheForTesting() {
CHECK_IS_TEST();
country_id_cache_.reset();
}
std::optional<CountryId> RegionalCapabilitiesService::GetPersistedCountryId() {
if (!profile_prefs_->HasPrefPath(prefs::kCountryIDAtInstall)) {
return std::nullopt;
}
CountryId persisted_country_id = CountryId::Deserialize(
profile_prefs_->GetInteger(prefs::kCountryIDAtInstall));
// Check and report on the validity of the initially persisted value.
if (persisted_country_id.IsValid()) {
base::UmaHistogramEnumeration(kUnknownCountryIdStored,
UnknownCountryIdStored::kValidCountryId);
return persisted_country_id;
}
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX)
if (base::FeatureList::IsEnabled(switches::kClearPrefForUnknownCountry)) {
profile_prefs_->ClearPref(prefs::kCountryIDAtInstall);
base::UmaHistogramEnumeration(kUnknownCountryIdStored,
UnknownCountryIdStored::kClearedPref);
return std::nullopt;
}
#endif
base::UmaHistogramEnumeration(
kUnknownCountryIdStored,
UnknownCountryIdStored::kDontClearInvalidCountry);
return persisted_country_id;
}
void RegionalCapabilitiesService::TrySetPersistedCountryId(
CountryId country_id) {
if (!country_id.IsValid()) {
return;
}
if (profile_prefs_->HasPrefPath(prefs::kCountryIDAtInstall)) {
// Deliberately do not override the current value. This would be a
// dedicated feature like `kDynamicProfileCountryMetrics` for example.
return;
}
profile_prefs_->SetInteger(prefs::kCountryIDAtInstall,
country_id.Serialize());
}
#if BUILDFLAG(IS_ANDROID)
base::android::ScopedJavaLocalRef<jobject>
RegionalCapabilitiesService::GetJavaObject() {
if (!java_ref_) {
java_ref_.Reset(Java_RegionalCapabilitiesService_Constructor(
jni_zero::AttachCurrentThread(), reinterpret_cast<intptr_t>(this)));
}
return base::android::ScopedJavaLocalRef<jobject>(java_ref_);
}
void RegionalCapabilitiesService::DestroyJavaObject() {
if (java_ref_) {
Java_RegionalCapabilitiesService_destroy(jni_zero::AttachCurrentThread(),
java_ref_);
java_ref_.Reset();
}
}
jboolean RegionalCapabilitiesService::IsInEeaCountry(JNIEnv* env) {
return IsInEeaCountry();
}
#endif
} // namespace regional_capabilities
|