blob: 16d88bf2c0bb79c32858591d7482cc6f818c0faa [file] [log] [blame]
Patricia Alfonso3b296e9f2023-09-14 15:18:141// Copyright 2023 The Chromium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROME_BROWSER_TPCD_EXPERIMENT_EXPERIMENT_MANAGER_H_
6#define CHROME_BROWSER_TPCD_EXPERIMENT_EXPERIMENT_MANAGER_H_
7
Nan Lin12bbaa82023-09-26 22:45:348#include <vector>
9
10#include "base/functional/callback_forward.h"
Nan Lin03748b42023-09-22 19:21:0311#include "base/no_destructor.h"
Nan Lin12bbaa82023-09-26 22:45:3412#include "base/sequence_checker.h"
13#include "base/thread_annotations.h"
Anton Maliev6ddb3c42023-09-26 21:08:3014#include "third_party/abseil-cpp/absl/types/optional.h"
Nan Lin03748b42023-09-22 19:21:0315
Patricia Alfonso3b296e9f2023-09-14 15:18:1416namespace tpcd::experiment {
17
Nan Lin12bbaa82023-09-26 22:45:3418// Can only be used on the main thread.
Patricia Alfonso3b296e9f2023-09-14 15:18:1419class ExperimentManager {
20 public:
Nan Lin12bbaa82023-09-26 22:45:3421 using EligibilityDecisionCallback = base::OnceCallback<void(bool)>;
22
Nan Lin03748b42023-09-22 19:21:0323 static ExperimentManager* GetInstance();
Patricia Alfonso3b296e9f2023-09-14 15:18:1424
Nan Lin12bbaa82023-09-26 22:45:3425 // Called by `EligibilityService` to tell the manager whether a profile is
26 // eligible, with a callback to complete the profile-level work required once
27 // the final decision is made. The final decision is recorded in a local state
28 // pref. If this is called after the final decision is made, the local state
29 // pref value takes precedence and the callbacks are still performed.
30 void SetClientEligibility(
31 bool is_eligible,
32 EligibilityDecisionCallback on_eligibility_decision_callback);
Patricia Alfonso3b296e9f2023-09-14 15:18:1433
Nan Lin12bbaa82023-09-26 22:45:3434 // Returns the final decision for client eligibility, if completed.
35 // `absl::nullopt` will be returned if the final decision has not been made
36 // yet.
Anton Maliev6ddb3c42023-09-26 21:08:3037 absl::optional<bool> IsClientEligible() const;
38
Nan Lin03748b42023-09-22 19:21:0339 protected:
40 ExperimentManager();
Nan Lin12bbaa82023-09-26 22:45:3441 ~ExperimentManager();
Nan Lin03748b42023-09-22 19:21:0342
Patricia Alfonso3b296e9f2023-09-14 15:18:1443 private:
Nan Lin03748b42023-09-22 19:21:0344 friend base::NoDestructor<ExperimentManager>;
Nan Lin12bbaa82023-09-26 22:45:3445 bool client_is_eligible_ GUARDED_BY_CONTEXT(sequence_checker_) = true;
46 std::vector<EligibilityDecisionCallback> callbacks_
47 GUARDED_BY_CONTEXT(sequence_checker_);
48 SEQUENCE_CHECKER(sequence_checker_);
Nan Lin03748b42023-09-22 19:21:0349
Nan Lin12bbaa82023-09-26 22:45:3450 void CaptureEligibilityInLocalStatePref();
Patricia Alfonso3b296e9f2023-09-14 15:18:1451};
52
53} // namespace tpcd::experiment
54
55#endif // CHROME_BROWSER_TPCD_EXPERIMENT_EXPERIMENT_MANAGER_H_