// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/android/jni_string.h"
#include "base/files/file_path.h"
// NOTE: This target is transitively depended on by //chrome/browser and thus
// can't depend on it.
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/safe_browsing/advanced_protection_status_manager.h"
#include "chrome/browser/safe_browsing/advanced_protection_status_manager_factory.h"
#include "chrome/browser/signin/identity_manager_factory.h"  // nogncheck
#include "components/password_manager/core/common/password_manager_pref_names.h"
#include "components/prefs/pref_service.h"
#include "components/safe_browsing/content/browser/safe_browsing_service_interface.h"
#include "components/safe_browsing/content/common/file_type_policies.h"
#include "components/safe_browsing/core/common/hashprefix_realtime/hash_realtime_utils.h"
#include "components/safe_browsing/core/common/safe_browsing_prefs.h"
#include "content/public/browser/web_contents.h"

// Must come after all headers that specialize FromJniType() / ToJniType().
#include "chrome/browser/safe_browsing/android/jni_headers/SafeBrowsingBridge_jni.h"

using base::android::JavaRef;

namespace {

PrefService* GetPrefService(const base::android::JavaRef<jobject>& j_profile) {
  return Profile::FromJavaObject(j_profile)->GetPrefs();
}

}  // namespace

namespace safe_browsing {

static int32_t JNI_SafeBrowsingBridge_UmaValueForFile(
    JNIEnv* env,
    const JavaRef<jstring>& path) {
  base::FilePath file_path(base::android::ConvertJavaStringToUTF8(env, path));
  return safe_browsing::FileTypePolicies::GetInstance()->UmaValueForFile(
      file_path);
}

static bool JNI_SafeBrowsingBridge_GetSafeBrowsingExtendedReportingEnabled(
    JNIEnv* env,
    const JavaRef<jobject>& j_profile) {
  return safe_browsing::IsExtendedReportingEnabled(*GetPrefService(j_profile));
}

static void JNI_SafeBrowsingBridge_SetSafeBrowsingExtendedReportingEnabled(
    JNIEnv* env,
    const JavaRef<jobject>& j_profile,
    bool enabled) {
  safe_browsing::SetExtendedReportingPrefAndMetric(
      GetPrefService(j_profile), enabled,
      safe_browsing::SBER_OPTIN_SITE_ANDROID_SETTINGS);
}

static bool JNI_SafeBrowsingBridge_GetSafeBrowsingExtendedReportingManaged(
    JNIEnv* env,
    const JavaRef<jobject>& j_profile) {
  PrefService* pref_service = GetPrefService(j_profile);
  return pref_service->IsManagedPreference(
      prefs::kSafeBrowsingScoutReportingEnabled);
}

static int32_t JNI_SafeBrowsingBridge_GetSafeBrowsingState(
    JNIEnv* env,
    const JavaRef<jobject>& j_profile) {
  return static_cast<int32_t>(
      safe_browsing::GetSafeBrowsingState(*GetPrefService(j_profile)));
}

static void JNI_SafeBrowsingBridge_SetSafeBrowsingState(
    JNIEnv* env,
    const JavaRef<jobject>& j_profile,
    int32_t state) {
  return safe_browsing::SetSafeBrowsingState(
      GetPrefService(j_profile), static_cast<SafeBrowsingState>(state),
      /*is_esb_enabled_by_account_integration=*/false);
}

static void JNI_SafeBrowsingBridge_EnableSafeBrowsingSettingSetLocallyPref(
    JNIEnv* env,
    const JavaRef<jobject>& j_profile) {
  return safe_browsing::EnableSafeBrowsingSettingSetLocallyPref(
      GetPrefService(j_profile));
}

static bool JNI_SafeBrowsingBridge_IsSafeBrowsingManaged(
    JNIEnv* env,
    const JavaRef<jobject>& j_profile) {
  return safe_browsing::IsSafeBrowsingPolicyManaged(*GetPrefService(j_profile));
}

static bool JNI_SafeBrowsingBridge_IsHashRealTimeLookupEligibleInSession(
    JNIEnv* env) {
  return safe_browsing::hash_realtime_utils::
      IsHashRealTimeLookupEligibleInSession();
}

static void JNI_SafeBrowsingBridge_ReportIntent(
    JNIEnv* env,
    content::WebContents* web_contents,
    const std::string& package_name,
    const std::string& uri) {
  reinterpret_cast<SafeBrowsingServiceInterface*>(
      g_browser_process->safe_browsing_service())
      ->ReportExternalAppRedirect(web_contents, package_name, uri);
}

}  // namespace safe_browsing

DEFINE_JNI(SafeBrowsingBridge)
