chromium/components/search_engines/search_engine_choice/search_engine_choice_service.cc

// 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 "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"  // nogncheck
#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))
// The choice screen should be shown if the `DefaultSearchProviderEnabled`
// policy is not set, or set to true and the
// `DefaultSearchProviderSearchURL` policy is not set.
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) {}

// Returns true if all search engine choice prefs are set.
bool IsSearchEngineChoiceCompleted(const PrefService& prefs) {}

void MarkSearchEngineChoiceCompleted(PrefService& prefs) {}

std::optional<base::Time> GetChoiceScreenCompletionTimestamp(
    PrefService& prefs) {}

// Returns true if the version is valid and can be compared to the current
// Chrome version.
bool IsValidVersionFormat(const base::Version& version) {}

// Logs the outcome of a reprompt attempt for a specific key (either a specific
// country or the wildcard).
void LogSearchRepromptKeyHistograms(RepromptResult result, bool is_wildcard) {}

NativeCallbackType;

}  // namespace

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

// static
void MarkSearchEngineChoiceCompletedForTesting(PrefService& prefs) {}

}  // namespace search_engines

#if BUILDFLAG(IS_ANDROID)
void JNI_SearchEngineChoiceService_ProcessCountryFromPlayApi(
    JNIEnv* env,
    jlong ptr_to_native_callback,
    const base::android::JavaParamRef<jstring>& j_device_country) {
  // Using base::WrapUnique ensures that the callback is deleted when this goes
  // out of scope.
  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