chromium/content/services/auction_worklet/context_recycler.h

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

#ifndef CONTENT_SERVICES_AUCTION_WORKLET_CONTEXT_RECYCLER_H_
#define CONTENT_SERVICES_AUCTION_WORKLET_CONTEXT_RECYCLER_H_

#include <memory>
#include <vector>

#include "base/memory/raw_ptr.h"
#include "base/memory/raw_ref.h"
#include "content/common/content_export.h"
#include "content/services/auction_worklet/auction_v8_helper.h"
#include "content/services/auction_worklet/lazy_filler.h"
#include "content/services/auction_worklet/public/mojom/auction_shared_storage_host.mojom-forward.h"
#include "v8/include/v8-context.h"
#include "v8/include/v8-forward.h"

namespace auction_worklet {

namespace mojom {
class AuctionSharedStorageHost;
}  // namespace mojom

class AuctionV8Logger;
class ForDebuggingOnlyBindings;
class PrivateAggregationBindings;
class RealTimeReportingBindings;
class RegisterAdBeaconBindings;
class RegisterAdMacroBindings;
class ReportBindings;
class SetBidBindings;
class SetPriorityBindings;
class SetPrioritySignalsOverrideBindings;
class SharedStorageBindings;
class AuctionConfigLazyFiller;
class BiddingBrowserSignalsLazyFiller;
class InterestGroupLazyFiller;
class SellerBrowserSignalsLazyFiller;

// Base class for bindings used with contexts used with ContextRecycler.
// The expected lifecycle is:
// 1) AttachToContext()
// 2) Use by script
// 3) Reset()
// 4) Use by script
// 5) Reset()
// etc.
class Bindings {};

// Base class for helper for lazily filling in objects, with lifetime tied to
// ContextRecycler. The connection to ContextRecycler is needed in case the
// parent object is (inappropriately) saved between reuses, to avoid dangling
// points to input data (in that case we return overly fresh data, but that's
// safe, and it doesn't seem worth the effort to aid in misuse).
//
// In addition to the basic LazyFillter pattern, implementors must also
// implement Reset(), which adjusts state for recycling.
//
// Implementations must be careful of `this` being in a recycled state, and
// re-check that the fields themselves are still present on access (values may
// have changed between defining an attribute and the invocation of the
// attribute's callback).
//
// Users should get PersistedLazyFiller from ContextRecycler and use it to
// populate objects only after ContextRecyclerScope is active.
class PersistedLazyFiller : public LazyFiller {};

// This helps manage the state of bindings on a context should we chose to
// recycle it, by calling Reset() after the current usage is done, to prepare
// for the next. Context is accessed via ContextRecyclerScope.
class CONTENT_EXPORT ContextRecycler {};

// Helper to enter a context scope on creation and reset all bindings
// on destruction.
class CONTENT_EXPORT ContextRecyclerScope {};

}  // namespace auction_worklet

#endif  // CONTENT_SERVICES_AUCTION_WORKLET_CONTEXT_RECYCLER_H_