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

#ifndef CHROME_BROWSER_COMMERCE_MERCHANT_VIEWER_MERCHANT_SIGNAL_DB_H_
#define CHROME_BROWSER_COMMERCE_MERCHANT_VIEWER_MERCHANT_SIGNAL_DB_H_

#include "base/android/scoped_java_ref.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/self_deleting.h"
#include "components/commerce/core/proto/merchant_signal_db_content.pb.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/leveldb_proto/public/proto_database.h"

namespace content {
class BrowserContext;
}  // namespace content

namespace merchant_signal_db {
class MerchantSignalContentProto;
}  // namespace merchant_signal_db

template <typename T>
class SessionProtoDB;

class MerchantSignalDB : public base::SelfDeleting {
 public:
  MerchantSignalDB(content::BrowserContext* browser_context,
                   base::SelfDeletingPassKey key);
  MerchantSignalDB(const MerchantSignalDB&) = delete;
  MerchantSignalDB& operator=(const MerchantSignalDB&) = delete;

  // Save signal for key.
  void Save(JNIEnv* env,
            const std::string& key,
            const int64_t jtimestamp,
            const base::android::JavaRef<jobject>& jcallback);

  // Load signal corresponding to key.
  void Load(JNIEnv* env,
            const std::string& key,
            const base::android::JavaRef<jobject>& jcallback);

  // Load signal whose keys have specific prefix.
  void LoadWithPrefix(JNIEnv* env,
                      const std::string& prefix,
                      const base::android::JavaRef<jobject>& jcallback);

  // Delete entry corresponding to key.
  void Delete(JNIEnv* env,
              const std::string& key,
              const base::android::JavaRef<jobject>& jcallback);

  // Delete all entries in the database.
  void DeleteAll(JNIEnv* env, const base::android::JavaRef<jobject>& jcallback);

  // Destroys this object.
  void Destroy(JNIEnv* env);

 private:
  ~MerchantSignalDB();

  raw_ptr<SessionProtoDB<merchant_signal_db::MerchantSignalContentProto>>
      proto_db_;
};

#endif  // CHROME_BROWSER_COMMERCE_MERCHANT_VIEWER_MERCHANT_SIGNAL_DB_H_
