blob: 0d60072c2bfa994190a42e22dea822a527ad5d0c [file] [log] [blame]
// Copyright 2022 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef MISSIVE_UTIL_REFCOUNTED_CLOSURE_LIST_H_
#define MISSIVE_UTIL_REFCOUNTED_CLOSURE_LIST_H_
#include <vector>
#include <base/functional/callback.h>
#include <base/memory/ref_counted.h>
#include <base/memory/ref_counted_delete_on_sequence.h>
#include <base/memory/scoped_refptr.h>
#include <base/task/sequenced_task_runner.h>
#include <base/thread_annotations.h>
namespace reporting {
// Thread-safe refcounted closure class, combining `BarrierClosure` and
// `OnceClosureList`, but allowing to take and drop references at arbitrary
// time. When the last reference is dropped, `OnceClosureList` is notified.
class RefCountedClosureList
: public base::RefCountedDeleteOnSequence<RefCountedClosureList> {
public:
explicit RefCountedClosureList(
scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner);
RefCountedClosureList(const RefCountedClosureList&) = delete;
RefCountedClosureList& operator=(const RefCountedClosureList&) = delete;
// Registers completion notification callback.
void RegisterCompletionCallback(base::OnceClosure callback);
private:
friend class base::RefCountedDeleteOnSequence<RefCountedClosureList>;
friend class base::DeleteHelper<RefCountedClosureList>;
~RefCountedClosureList();
scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_;
SEQUENCE_CHECKER(sequence_checker_);
// List of all completion closures (protected by |sequenced_task_runner_|).
std::vector<base::OnceClosure> callbacks_
GUARDED_BY_CONTEXT(sequence_checker_);
};
} // namespace reporting
#endif // MISSIVE_UTIL_REFCOUNTED_CLOSURE_LIST_H_