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

#include "chrome/credential_provider/gaiacp/gcp_crash_reporter_client.h"

#include <utility>

#include "base/check.h"
#include "base/file_version_info.h"
#include "base/notreached.h"
#include "base/strings/string_util.h"
#include "chrome/credential_provider/gaiacp/gcp_crash_reporting_utils.h"

namespace credential_provider {

GcpCrashReporterClient::GcpCrashReporterClient(
    base::FilePath crash_dump_location)
    : crash_dump_location_(std::move(crash_dump_location)) {
  CHECK(!crash_dump_location_.empty());
}

GcpCrashReporterClient::~GcpCrashReporterClient() = default;

base::FilePath GcpCrashReporterClient::GetPathForFileVersionInfo(
    const std::wstring& exe_path) {
  return base::FilePath(exe_path);
}

bool GcpCrashReporterClient::GetAlternativeCrashDumpLocation(
    std::wstring* crash_dir) {
  return false;
}

void GcpCrashReporterClient::GetProductNameAndVersion(
    const std::wstring& exe_path,
    std::wstring* product_name,
    std::wstring* version,
    std::wstring* special_build,
    std::wstring* channel_name) {
  DCHECK(product_name);
  DCHECK(version);
  DCHECK(special_build);
  DCHECK(channel_name);
  // Report crashes under the same product name as the browser. This string
  // MUST match server-side configuration.
  *product_name = L"Chrome";
  special_build->clear();
  channel_name->clear();
  std::unique_ptr<FileVersionInfo> version_info(
      FileVersionInfo::CreateFileVersionInfo(
          GetPathForFileVersionInfo(exe_path)));

  if (version_info) {
    *version = base::AsWString(version_info->product_version());
    *special_build = base::AsWString(version_info->special_build());
  } else {
    *version = L"0.0.0.0-devel";
  }
}

bool GcpCrashReporterClient::GetShouldDumpLargerDumps() {
  return false;
}

bool GcpCrashReporterClient::GetCrashDumpLocation(std::wstring* crash_dir) {
  *crash_dir = crash_dump_location_.value();
  return true;
}

bool GcpCrashReporterClient::IsRunningUnattended() {
  return false;
}

bool GcpCrashReporterClient::GetCollectStatsConsent() {
  return GetGCPWCollectStatsConsent();
}

bool GcpCrashReporterClient::EnableBreakpadForProcess(
    const std::string& process_type) {
  // This function is only called on Linux which the GCPW does not support.
  NOTREACHED();
}

}  // namespace credential_provider
