// 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.

#include "components/one_time_tokens/core/browser/util/expiring_subscription.h"

#include "components/one_time_tokens/core/browser/util/expiring_subscription_manager.h"

namespace one_time_tokens {

ExpiringSubscription::ExpiringSubscription() = default;

ExpiringSubscription::ExpiringSubscription(
    ExpiringSubscriptionHandle handle,
    base::WeakPtr<ExpiringSubscriptionManagerBase> manager)
    : handle_(std::move(handle)), manager_(std::move(manager)) {}

// This delegates to the assignment operator, which correctly handles the
// cancellation of any active subscription.
ExpiringSubscription::ExpiringSubscription(ExpiringSubscription&& other) {
  *this = std::move(other);
}

ExpiringSubscription::~ExpiringSubscription() {
  if (manager_) {
    manager_->Cancel(handle_);
  }
}

bool ExpiringSubscription::IsAlive() const {
  return manager_ && manager_->Exists(handle_);
}

void ExpiringSubscription::Cancel() {
  if (manager_) {
    manager_->Cancel(handle_);
  }
}

void ExpiringSubscription::SetExpirationTime(base::Time new_expiration) {
  if (manager_) {
    manager_->SetExpirationTime(handle_, new_expiration);
  }
}

base::Time ExpiringSubscription::GetExpirationTime() const {
  return manager_ ? manager_->GetExpirationTime(handle_) : base::Time();
}

ExpiringSubscription& ExpiringSubscription::operator=(
    ExpiringSubscription&& other) {
  if (manager_) {
    manager_->Cancel(handle_);
  }
  handle_ = std::move(other.handle_);
  manager_ = other.manager_;
  // Prevent that destructor of other cancels the subscription.
  other.manager_ = nullptr;
  return *this;
}

}  // namespace one_time_tokens
