#include "components/search_engines/search_engine_choice/search_engine_choice_service.h"
#include <inttypes.h>
#include <memory>
#include <optional>
#include "base/callback_list.h"
#include "base/check_deref.h"
#include "base/check_is_test.h"
#include "base/command_line.h"
#include "base/debug/dump_without_crashing.h"
#include "base/json/json_reader.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/histogram_functions.h"
#include "base/no_destructor.h"
#include "base/strings/stringprintf.h"
#include "base/threading/thread_restrictions.h"
#include "base/time/time.h"
#include "base/version.h"
#include "build/chromeos_buildflags.h"
#include "components/country_codes/country_codes.h"
#include "components/crash/core/common/crash_key.h"
#include "components/policy/core/common/policy_service.h"
#include "components/policy/policy_constants.h"
#include "components/prefs/pref_service.h"
#include "components/search_engines/eea_countries_ids.h"
#include "components/search_engines/search_engine_choice/search_engine_choice_metrics_service_accessor.h"
#include "components/search_engines/search_engine_choice/search_engine_choice_utils.h"
#include "components/search_engines/search_engine_type.h"
#include "components/search_engines/search_engines_pref_names.h"
#include "components/search_engines/search_engines_switches.h"
#include "components/search_engines/template_url_prepopulate_data.h"
#include "components/search_engines/template_url_service.h"
#include "components/signin/public/base/signin_switches.h"
#include "components/version_info/version_info.h"
#if !BUILDFLAG(IS_FUCHSIA)
#include "components/variations/service/variations_service.h"
#endif
#if BUILDFLAG(IS_ANDROID)
#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "components/search_engines/android/jni_headers/SearchEngineChoiceService_jni.h"
#endif
namespace search_engines {
namespace {
#if !(BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_FUCHSIA) || \
BUILDFLAG(CHROME_FOR_TESTING))
bool IsSearchEngineChoiceScreenAllowedByPolicy(
const policy::PolicyService& policy_service) { … }
bool IsSetOrBlockedByPolicy(const TemplateURL* default_search_engine) { … }
bool IsDefaultSearchProviderSetOrBlockedByPolicy(
const TemplateURLService& template_url_service) { … }
#endif
SearchEngineType GetDefaultSearchEngineType(
TemplateURLService& template_url_service) { … }
bool IsSearchEngineChoiceCompleted(const PrefService& prefs) { … }
void MarkSearchEngineChoiceCompleted(PrefService& prefs) { … }
std::optional<base::Time> GetChoiceScreenCompletionTimestamp(
PrefService& prefs) { … }
bool IsValidVersionFormat(const base::Version& version) { … }
void LogSearchRepromptKeyHistograms(RepromptResult result, bool is_wildcard) { … }
NativeCallbackType;
}
SearchEngineChoiceService::SearchEngineChoiceService(PrefService& profile_prefs,
PrefService* local_state,
int variations_country_id)
: … { … }
SearchEngineChoiceService::SearchEngineChoiceService(
PrefService& profile_prefs,
PrefService* local_state,
variations::VariationsService* variations_service)
: … { … }
SearchEngineChoiceService::~SearchEngineChoiceService() = default;
bool SearchEngineChoiceService::ShouldShowUpdatedSettings() { … }
SearchEngineChoiceScreenConditions
SearchEngineChoiceService::GetStaticChoiceScreenConditions(
const policy::PolicyService& policy_service,
bool is_regular_profile,
const TemplateURLService& template_url_service) { … }
SearchEngineChoiceScreenConditions
SearchEngineChoiceService::GetDynamicChoiceScreenConditions(
const TemplateURLService& template_url_service) { … }
int SearchEngineChoiceService::GetCountryId() { … }
void SearchEngineChoiceService::RecordChoiceMade(
ChoiceMadeLocation choice_location,
TemplateURLService* template_url_service) { … }
void SearchEngineChoiceService::MaybeRecordChoiceScreenDisplayState(
const ChoiceScreenDisplayState& display_state,
bool is_from_cached_state) { … }
void SearchEngineChoiceService::PreprocessPrefsForReprompt() { … }
void SearchEngineChoiceService::ProcessPendingChoiceScreenDisplayState(
PrefService* local_state) { … }
int SearchEngineChoiceService::GetCountryIdInternal() { … }
void SearchEngineChoiceService::ClearCountryIdCacheForTesting() { … }
#if BUILDFLAG(IS_ANDROID)
void SearchEngineChoiceService::ProcessGetCountryResponseFromPlayApi(
int country_id) {
profile_prefs_->SetInteger(country_codes::kCountryIDAtInstall, country_id);
}
#endif
void MarkSearchEngineChoiceCompletedForTesting(PrefService& prefs) { … }
}
#if BUILDFLAG(IS_ANDROID)
void JNI_SearchEngineChoiceService_ProcessCountryFromPlayApi(
JNIEnv* env,
jlong ptr_to_native_callback,
const base::android::JavaParamRef<jstring>& j_device_country) {
std::unique_ptr<search_engines::NativeCallbackType> heap_callback =
base::WrapUnique(reinterpret_cast<search_engines::NativeCallbackType*>(
ptr_to_native_callback));
CHECK(heap_callback);
if (!j_device_country) {
return;
}
std::string device_country =
base::android::ConvertJavaStringToUTF8(env, j_device_country);
int device_country_id =
country_codes::CountryStringToCountryID(device_country);
if (device_country_id == country_codes::kCountryIDUnknown) {
return;
}
std::move(*heap_callback).Run(device_country_id);
}
#endif