// Copyright 2025 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_ANDROID_TAB_STATE_STORAGE_SERVICE_ANDROID_H_
#define CHROME_BROWSER_ANDROID_TAB_STATE_STORAGE_SERVICE_ANDROID_H_

#include "base/android/scoped_java_ref.h"
#include "chrome/browser/android/tab_android.h"
#include "chrome/browser/tab/tab_state_storage_backend.h"
#include "chrome/browser/tab/tab_state_storage_service.h"
#include "chrome/browser/tab/tab_storage_packager.h"
#include "components/keyed_service/core/keyed_service.h"

namespace tabs {

const char kTabStateStorageServiceAndroidKey[] =
    "tab_state_storage_service_android";

class TabStateStorageServiceAndroid : public base::SupportsUserData::Data {
 public:
  // ScopedBatchAndroid is a wrapper for TabStateStorageService::ScopedBatch
  // that allows it to be used across the JNI boundary. The destructor of
  // ScopedBatchAndroid will commit the batched operations.
  struct ScopedBatchAndroid {
    explicit ScopedBatchAndroid(TabStateStorageService::ScopedBatch batch);
    ~ScopedBatchAndroid();

    TabStateStorageService::ScopedBatch batch;
  };

  explicit TabStateStorageServiceAndroid(
      TabStateStorageService* tab_state_storage_service);
  ~TabStateStorageServiceAndroid() override;

  void BoostPriority(JNIEnv* env);

  void Save(JNIEnv* env, TabAndroid* tab);

  void SaveWithMetadata(JNIEnv* env,
                        const std::string& window_tag,
                        bool is_off_the_record,
                        TabAndroid* tab);

  void LoadAllData(JNIEnv* env,
                   const std::string& window_tag,
                   bool is_off_the_record,
                   const jni_zero::JavaRef<jobject>& j_loaded_data_callback);

  void CountTabsForWindow(JNIEnv* env,
                          const std::string& window_tag,
                          bool is_off_the_record,
                          const jni_zero::JavaRef<jobject>& j_count_callback);

  void ClearState(JNIEnv* env);

  void ClearWindow(JNIEnv* env, const std::string& window_tag);

  void ClearAllWindowsExcept(JNIEnv* env,
                             const std::vector<std::string>& window_tags);

  void ClearWindowWithOtrStatus(JNIEnv* env,
                                const std::string& window_tag,
                                bool is_off_the_record);
  void ClearUnusedNodesForWindow(JNIEnv* env,
                                 const std::string& window_tag,
                                 bool is_off_the_record,
                                 const TabStripCollection* collection);

  void PrintAll(JNIEnv* env);

  int64_t CreateBatch(JNIEnv* env);

  void SetKey(JNIEnv* env,
              const std::string& window_tag,
              std::vector<uint8_t> key);

  void RemoveKey(JNIEnv* env, const std::string& window_tag);

  base::android::ScopedJavaLocalRef<jbyteArray> GenerateKey(
      JNIEnv* env,
      const std::string& window_tag);

  base::android::ScopedJavaLocalRef<jobject> GetJavaObject();

 private:
  // A reference to the Java version of this class.
  base::android::ScopedJavaGlobalRef<jobject> java_obj_;

  // Not owned by this class.
  raw_ptr<TabStateStorageService> tab_state_storage_service_;
};

}  // namespace tabs

#endif  // CHROME_BROWSER_ANDROID_TAB_STATE_STORAGE_SERVICE_ANDROID_H_
