Patricia Alfonso | 3b296e9f | 2023-09-14 15:18:14 | [diff] [blame] | 1 | // 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 Lin | 12bbaa8 | 2023-09-26 22:45:34 | [diff] [blame^] | 8 | #include <vector> |
| 9 | |
| 10 | #include "base/functional/callback_forward.h" |
Nan Lin | 03748b4 | 2023-09-22 19:21:03 | [diff] [blame] | 11 | #include "base/no_destructor.h" |
Nan Lin | 12bbaa8 | 2023-09-26 22:45:34 | [diff] [blame^] | 12 | #include "base/sequence_checker.h" |
| 13 | #include "base/thread_annotations.h" |
Anton Maliev | 6ddb3c4 | 2023-09-26 21:08:30 | [diff] [blame] | 14 | #include "third_party/abseil-cpp/absl/types/optional.h" |
Nan Lin | 03748b4 | 2023-09-22 19:21:03 | [diff] [blame] | 15 | |
Patricia Alfonso | 3b296e9f | 2023-09-14 15:18:14 | [diff] [blame] | 16 | namespace tpcd::experiment { |
| 17 | |
Nan Lin | 12bbaa8 | 2023-09-26 22:45:34 | [diff] [blame^] | 18 | // Can only be used on the main thread. |
Patricia Alfonso | 3b296e9f | 2023-09-14 15:18:14 | [diff] [blame] | 19 | class ExperimentManager { |
| 20 | public: |
Nan Lin | 12bbaa8 | 2023-09-26 22:45:34 | [diff] [blame^] | 21 | using EligibilityDecisionCallback = base::OnceCallback<void(bool)>; |
| 22 | |
Nan Lin | 03748b4 | 2023-09-22 19:21:03 | [diff] [blame] | 23 | static ExperimentManager* GetInstance(); |
Patricia Alfonso | 3b296e9f | 2023-09-14 15:18:14 | [diff] [blame] | 24 | |
Nan Lin | 12bbaa8 | 2023-09-26 22:45:34 | [diff] [blame^] | 25 | // 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 Alfonso | 3b296e9f | 2023-09-14 15:18:14 | [diff] [blame] | 33 | |
Nan Lin | 12bbaa8 | 2023-09-26 22:45:34 | [diff] [blame^] | 34 | // 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 Maliev | 6ddb3c4 | 2023-09-26 21:08:30 | [diff] [blame] | 37 | absl::optional<bool> IsClientEligible() const; |
| 38 | |
Nan Lin | 03748b4 | 2023-09-22 19:21:03 | [diff] [blame] | 39 | protected: |
| 40 | ExperimentManager(); |
Nan Lin | 12bbaa8 | 2023-09-26 22:45:34 | [diff] [blame^] | 41 | ~ExperimentManager(); |
Nan Lin | 03748b4 | 2023-09-22 19:21:03 | [diff] [blame] | 42 | |
Patricia Alfonso | 3b296e9f | 2023-09-14 15:18:14 | [diff] [blame] | 43 | private: |
Nan Lin | 03748b4 | 2023-09-22 19:21:03 | [diff] [blame] | 44 | friend base::NoDestructor<ExperimentManager>; |
Nan Lin | 12bbaa8 | 2023-09-26 22:45:34 | [diff] [blame^] | 45 | 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 Lin | 03748b4 | 2023-09-22 19:21:03 | [diff] [blame] | 49 | |
Nan Lin | 12bbaa8 | 2023-09-26 22:45:34 | [diff] [blame^] | 50 | void CaptureEligibilityInLocalStatePref(); |
Patricia Alfonso | 3b296e9f | 2023-09-14 15:18:14 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | } // namespace tpcd::experiment |
| 54 | |
| 55 | #endif // CHROME_BROWSER_TPCD_EXPERIMENT_EXPERIMENT_MANAGER_H_ |