1 // Copyright 2020 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include "aemu/base/export.h"
18 #include "Features.h"
19 // #include "HWMatching.h"
20 
21 #include <functional>
22 #include <string>
23 #include <vector>
24 #include <ostream>
25 
26 namespace android {
27 namespace featurecontrol {
28 
29 // featurecontrol is used to switch on/off advanced features It loads
30 // sdk/emulator/lib/advancedFeatures.ini for default values and
31 // .android/advancedFeatures.ini for user overridden values.
32 // It is expected to be initialized at the beginning of the emulator.
33 // For easier testing, one may also want to pass the override value through
34 // command line and call setEnabledOverride. (Command line override not
35 // implemented yet)
36 //
37 // featurecontrol::isEnabled is thread safe, all other methods are not.
38 //
39 // To add new features, please (1) add it to android/data/advancedFeatures.ini
40 // and (2) add a new line to
41 // FeatureControlDef.h, in the following format:
42 // FEATURE_CONTROL_ITEM(YOUR_FEATURE_NAME)
43 
44 void initialize();
45 
46 bool isEnabled(Feature feature);
47 bool isEnabledByGuest(Feature feature);
48 AEMU_EXPORT void setEnabledOverride(Feature feature, bool isEnabled);
49 void resetEnabledToDefault(Feature feature);
50 
51 // Queries whether this feature is tied to the guest.
52 bool isGuestFeature(Feature feature);
53 
54 // returns true if the user has specified it in
55 // home directory's user-based advancedFeatures.ini.
56 bool isOverridden(Feature feature);
57 
58 // like setEnabledOverride, except it is a no-op
59 // if isOverridden(feature) == true.
60 void setIfNotOverriden(Feature feature, bool isEnabled);
61 // like setIfNotOverriden, except it is a no-op
62 // if the guest did not enable it too.
63 void setIfNotOverridenOrGuestDisabled(Feature feature, bool isEnabled);
64 
65 Feature stringToFeature(const std::string& str);
66 
67 // For hardware configurations special enough to warrant
68 // disabling or enabling features, we use the concept of
69 // "feature pattern" which consists of properties of hardware
70 // in question and a set of features to force-enable or disable.
71 
72 // applyCachedServerFeaturePatterns() queries host hardware
73 // confiruation, takes current cached patterns, and enables
74 // or disables features based on which patterns match the host.
75 // If there is no cached patterns, no action is taken.
76 void applyCachedServerFeaturePatterns();
77 // asyncUpdateServerFeaturePatterns():
78 // If the current cached feature patterns don't exist or are over 24 hours old,
79 // asyncUpdateServerFeaturePatterns() starts a download of
80 // a protobuf containing the latest feature patterns, replacing
81 // the current cached ones.
82 void asyncUpdateServerFeaturePatterns();
83 
84 // Queries the current set of features in various ways:
85 // - whether the default guest/host/server config has attempted
86 // to enable the feature.
87 // - whether the user has overriden the feature.
88 // - the resulting set of enabled features, which also accounts for
89 // programmatic setting of features.
90 std::vector<Feature> getEnabledNonOverride();
91 std::vector<Feature> getEnabledOverride();
92 std::vector<Feature> getDisabledOverride();
93 std::vector<Feature> getEnabled();
94 void writeFeaturesToStream(std::ostream& os);
95 
96 // Overrides feature_is_enabled function above to use a user-provided callback
97 // instead.
98 void setFeatureEnabledCallback(std::function<bool(Feature)> cb);
99 
100 // Customization point for the client to inject feature override calls.
101 void productFeatureOverride();
102 } // namespace android
103 } // namespace featurecontrol
104