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 THIRD_PARTY_BLINK_PUBLIC_COMMON_FEATURE_POLICY_POLICY_VALUE_H_
6 #define THIRD_PARTY_BLINK_PUBLIC_COMMON_FEATURE_POLICY_POLICY_VALUE_H_
7 
8 #include "base/macros.h"
9 #include "third_party/blink/public/common/common_export.h"
10 #include "third_party/blink/public/mojom/feature_policy/policy_value.mojom-shared.h"
11 
12 namespace blink {
13 
14 // ListValue (PolicyValue)
15 // ----------------------
16 // PolicyValue is a union of types (int / double / set<int> / bool ) that can be
17 // used to specify the parameter of a policy.
18 
19 // TODO(crbug.com/1119481): Add the following types: enum, inc/dec int, inc
20 // double, set.
21 class BLINK_COMMON_EXPORT PolicyValue {
22  public:
23   PolicyValue();
24 
25   static PolicyValue CreateBool(bool);
26   static PolicyValue CreateDecDouble(double);
27   static PolicyValue CreateEnum(int32_t);
28 
29   // A 'max' PolicyValue is the most permissive value for the policy.
30   static PolicyValue CreateMaxPolicyValue(mojom::PolicyValueType type);
31   // A 'min' PolicyValue is the most restrictive value for the policy.
32   static PolicyValue CreateMinPolicyValue(mojom::PolicyValueType type);
33 
Type()34   mojom::PolicyValueType Type() const { return type_; }
SetType(mojom::PolicyValueType type)35   void SetType(mojom::PolicyValueType type) { type_ = type; }
36 
37   // PolicyValue getters.
38   // Note the getters also DCHECKs that the type is correct.
39   bool BoolValue() const;
40   double DoubleValue() const;
41   int32_t IntValue() const;
42 
43   // PolicyValue setters.
44   // Note the getters also DCHECKs that the type is correct.
45   void SetBoolValue(bool bool_value);
46   void SetDoubleValue(double double_value);
47   void SetIntValue(int32_t int_value);
48 
49   // Operater overrides
50   PolicyValue& operator=(const PolicyValue& rhs);
51 
52   void SetToMax();
53   void SetToMin();
54 
55   // Test whether this policy value is compatible with required policy value.
56   // Note: a.IsCompatibleWith(b) == true does not necessary indicate
57   // b.IsCompatibleWith(a) == false, because not all policy value types support
58   // strictness comparison, e.g. enum.
59   bool IsCompatibleWith(const PolicyValue& required) const;
60 
61  private:
62   explicit PolicyValue(bool bool_value);
63   PolicyValue(int32_t int_value, mojom::PolicyValueType type);
64   PolicyValue(double double_value, mojom::PolicyValueType type);
65 
66   mojom::PolicyValueType type_;
67   bool bool_value_ = false;
68   double double_value_ = 0.0;
69   int32_t int_value_ = -1;
70 };
71 
72 bool BLINK_COMMON_EXPORT operator==(const PolicyValue& lhs,
73                                     const PolicyValue& rhs);
74 bool BLINK_COMMON_EXPORT operator!=(const PolicyValue& lhs,
75                                     const PolicyValue& rhs);
76 }  // namespace blink
77 
78 #endif  // THIRD_PARTY_BLINK_PUBLIC_COMMON_FEATURE_POLICY_POLICY_VALUE_H_
79