blob: 52dcacd58354a2ea9009b68696333d1f094e1608 [file] [log] [blame]
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/memory/weak_ptr.h"
namespace content {
// Define a templated type to track references to a parameterized target type.
// The target type must provide AddHandleRef() methods and DropHandleRef()
// methods that will be called when the CacheStorageRef<> object is created
// and destroyed.
// Note, the reference being tracked by CacheStorageRef is intended to
// represent high level references from DOM objects exposed to javascript
// and does not correspond to typical smart pointer reference counts.
// Internally this class uses a WeakPtr<> to the target and cache_storage
// must manager the target life cycle through other mechanisms.
// Currently this is only used to track references to CacheStorageCache
// target objects, but will soon be used to track references to CacheStorage
// targets as well.
template <typename TargetType>
class CacheStorageRef {
CacheStorageRef() = default;
explicit CacheStorageRef(base::WeakPtr<TargetType> target)
: target_(std::move(target)) {
CacheStorageRef(CacheStorageRef&& rhs) noexcept
: target_(std::move(rhs.target_)) {}
CacheStorageRef& operator=(CacheStorageRef&& rhs) {
if (target_)
target_ = std::move(rhs.target_);
return *this;
~CacheStorageRef() {
if (target_)
TargetType* value() const { return target_.get(); }
CacheStorageRef Clone() const { return CacheStorageRef(target_); }
base::WeakPtr<TargetType> target_;
} // namespace content