1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "feature_control.h"
18 #include "Features.h"
19 #include "FeatureControl.h"
20
21 #include <string>
22 #include <unordered_map>
23 #include <vector>
24
25
26 namespace android {
27 namespace featurecontrol {
28
29 static std::function<bool(Feature)> sFeatureEnabledCb;
30
setFeatureEnabledCallback(std::function<bool (Feature)> cb)31 void setFeatureEnabledCallback(std::function<bool(Feature)> cb) {
32 sFeatureEnabledCb = cb;
33 }
34 } // namespace featurecontrol
35 } // namespace android
36
37 struct FeatureState {
38 std::unordered_map<Feature, bool> enabled{
39 #define FEATURE_CONTROL_ITEM(item, idx) { kFeature_##item, false },
40 #include "FeatureControlDefHost.h"
41 #include "FeatureControlDefGuest.h"
42 #undef FEATURE_CONTROL_ITEM
43 };
44 };
45
46 static FeatureState sFeatureState;
47
48 // Call this function first to initialize the feature control.
feature_initialize()49 void feature_initialize() { }
50
51 // Get the access rules given by |name| if they exist, otherwise returns NULL
feature_is_enabled(Feature feature)52 bool feature_is_enabled(Feature feature) {
53 if (android::featurecontrol::sFeatureEnabledCb)
54 return android::featurecontrol::sFeatureEnabledCb(
55 static_cast<android::featurecontrol::Feature>(feature));
56
57 return sFeatureState.enabled[feature];
58 }
59
feature_set_enabled_override(Feature feature,bool isEnabled)60 void feature_set_enabled_override(Feature feature, bool isEnabled) {
61 sFeatureState.enabled[feature] = isEnabled;
62 }
63
feature_reset_enabled_to_default(Feature feature)64 void feature_reset_enabled_to_default(Feature feature) {
65 sFeatureState.enabled[feature] = false;
66 }
67
68 // Set the feature if it is not user-overriden.
feature_set_if_not_overridden(Feature feature,bool enable)69 void feature_set_if_not_overridden(Feature feature, bool enable) {
70 sFeatureState.enabled[feature] = enable;
71 }
72
73 // Set the feature if it is not user-overriden or disabled from the guest.
feature_set_if_not_overridden_or_guest_disabled(Feature feature,bool enable)74 void feature_set_if_not_overridden_or_guest_disabled(Feature feature, bool enable) {
75 sFeatureState.enabled[feature] = enable;
76 }
77
78 // Runs applyCachedServerFeaturePatterns then
79 // asyncUpdateServerFeaturePatterns. See FeatureControl.h
80 // for more info. To be called only once on startup.
feature_update_from_server()81 void feature_update_from_server() { }
82
feature_name(Feature feature)83 const char* feature_name(Feature feature) {
84 static const std::unordered_map<Feature, std::string>* const sFeatureNames = [] {
85 return new std::unordered_map<Feature, std::string>({
86 #define FEATURE_CONTROL_ITEM(item, idx) { kFeature_##item, #item },
87 #include "FeatureControlDefHost.h"
88 #include "FeatureControlDefGuest.h"
89 #undef FEATURE_CONTROL_ITEM
90 });
91 }();
92
93 auto it = sFeatureNames->find(feature);
94 if (it == sFeatureNames->end()) {
95 return "InvalidFeature";
96 }
97
98 return it->second.c_str();
99 }
100
101
feature_from_name(const char * name)102 Feature feature_from_name(const char* name) {
103 if (name == nullptr) {
104 return kFeature_unknown;
105 }
106
107 static const std::unordered_map<std::string, Feature>* const sNameToFeature = [] {
108 return new std::unordered_map<std::string, Feature>({
109 #define FEATURE_CONTROL_ITEM(item, idx) { #item, kFeature_##item },
110 #include "FeatureControlDefHost.h"
111 #include "FeatureControlDefGuest.h"
112 #undef FEATURE_CONTROL_ITEM
113 });
114 }();
115
116 auto it = sNameToFeature->find(std::string(name));
117 if (it == sNameToFeature->end()) {
118 return kFeature_unknown;
119 }
120
121 return it->second;
122 }