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

#ifndef COMPONENTS_UNEXPORTABLE_KEYS_SERVICE_ERROR_H_
#define COMPONENTS_UNEXPORTABLE_KEYS_SERVICE_ERROR_H_

#include <stdint.h>

#include "base/component_export.h"
#include "base/types/expected.h"

namespace unexportable_keys {

// Various errors returned by this component.
// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
// LINT.IfChange(ServiceError)
enum class ServiceError : uint8_t {
  // Reserved for histograms.
  // kNone = 0
  // crypto:: operation returned an error.
  kCryptoApiFailed = 1,
  // Provided key ID is unknown and doesn't correspond to any key.
  kKeyNotFound = 2,
  // Newly generated key is the same as the existing one (should be extremely
  // rare).
  kKeyCollision = 3,
  // Unexportable key provider is not available on this platform.
  kNoKeyProvider = 4,
  // None of the requested algorithms are supported by the key provider.
  kAlgorithmNotSupported = 5,
  // The key object hasn't been created yet. Try again later.
  kKeyNotReady = 6,
  // The returned signature did not verify with the corresponding public key.
  kVerifySignatureFailed = 7,
  // The operation is not supported by the key provider.
  kOperationNotSupported = 8,
  // The operation was cancelled.
  kOperationCancelled = 9,

  kMaxValue = kOperationCancelled
};
// LINT.ThenChange(
//     /components/unexportable_keys/mojom/unexportable_key_service.mojom:ServiceError,
//     /tools/metrics/histograms/metadata/net/enums.xml:UnexportableKeyServiceResult
// )

// Fake `ServiceError` value that can be used for metrics to signify that no
// error has occurred.
constexpr ServiceError kNoServiceErrorForMetrics = static_cast<ServiceError>(0);

// Return value for methods which perform unexportable keys operations that may
// fail. Either contains a `ServiceError` or a result value of arbitrary type.
template <class Result>
using ServiceErrorOr = base::expected<Result, ServiceError>;

// Returns whether a given `error` is persistent.
COMPONENT_EXPORT(UNEXPORTABLE_KEYS) bool IsPersistentError(ServiceError error);

}  // namespace unexportable_keys

#endif  // COMPONENTS_UNEXPORTABLE_KEYS_SERVICE_ERROR_H_
