1 // Copyright 2017 The Chromium Authors. All rights reserved.
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 COMPONENTS_FEATURE_ENGAGEMENT_INTERNAL_CONDITION_VALIDATOR_H_
6 #define COMPONENTS_FEATURE_ENGAGEMENT_INTERNAL_CONDITION_VALIDATOR_H_
7 
8 #include <stdint.h>
9 
10 #include <ostream>
11 #include <string>
12 #include <vector>
13 
14 #include "base/macros.h"
15 #include "components/feature_engagement/public/feature_list.h"
16 
17 namespace base {
18 struct Feature;
19 }  // namespace base
20 
21 namespace feature_engagement {
22 struct FeatureConfig;
23 class AvailabilityModel;
24 class DisplayLockController;
25 class EventModel;
26 
27 // A ConditionValidator checks the requred conditions for a given feature,
28 // and checks if all conditions are met.
29 class ConditionValidator {
30  public:
31   // The Result struct is used to categorize everything that could have the
32   // wrong state. By returning an instance of this where every value is true
33   // from MeetsConditions(...), it can be assumed that in-product help will
34   // be displayed.
35   struct Result {
36     explicit Result(bool initial_values);
37     Result(const Result& other);
38 
39     // Whether the event model was ready.
40     bool event_model_ready_ok;
41 
42     // Whether no other in-product helps were shown at the time.
43     bool currently_showing_ok;
44 
45     // Whether the feature is enabled.
46     bool feature_enabled_ok;
47 
48     // Whether the feature configuration was valid.
49     bool config_ok;
50 
51     // Whether the used precondition was met.
52     bool used_ok;
53 
54     // Whether the trigger precondition was met.
55     bool trigger_ok;
56 
57     // Whether the other preconditions were met.
58     bool preconditions_ok;
59 
60     // Whether the session rate precondition was met.
61     bool session_rate_ok;
62 
63     // Whether the availability model was ready.
64     bool availability_model_ready_ok;
65 
66     // Whether the availability precondition was met.
67     bool availability_ok;
68 
69     // Whether there are currently held display locks.
70     bool display_lock_ok;
71 
72     // Returns true if this result object has no errors, i.e. no values that
73     // are false.
74     bool NoErrors() const;
75   };
76 
77   virtual ~ConditionValidator() = default;
78 
79   // Returns a Result object that describes whether each condition has been met.
80   virtual Result MeetsConditions(
81       const base::Feature& feature,
82       const FeatureConfig& config,
83       const EventModel& event_model,
84       const AvailabilityModel& availability_model,
85       const DisplayLockController& display_lock_controller,
86       uint32_t current_day) const = 0;
87 
88   // Must be called to notify that the |feature| is currently showing.
89   virtual void NotifyIsShowing(
90       const base::Feature& feature,
91       const FeatureConfig& config,
92       const std::vector<std::string>& all_feature_names) = 0;
93 
94   // Must be called to notify that the |feature| is no longer showing.
95   virtual void NotifyDismissed(const base::Feature& feature) = 0;
96 
97  protected:
98   ConditionValidator() = default;
99 
100  private:
101   DISALLOW_COPY_AND_ASSIGN(ConditionValidator);
102 };
103 
104 std::ostream& operator<<(std::ostream& os,
105                          const ConditionValidator::Result& result);
106 
107 }  // namespace feature_engagement
108 
109 #endif  // COMPONENTS_FEATURE_ENGAGEMENT_INTERNAL_CONDITION_VALIDATOR_H_
110