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 "FeatureControl.h"
19
20 #include <string>
21 #include <vector>
22
23
24 namespace android {
25 namespace featurecontrol {
26
27 static std::function<bool(Feature)> sFeatureEnabledCb;
28
setFeatureEnabledCallback(std::function<bool (Feature)> cb)29 void setFeatureEnabledCallback(std::function<bool(Feature)> cb) {
30 sFeatureEnabledCb = cb;
31 }
32 } // namespace featurecontrol
33 } // namespace android
34
35 struct FeatureState {
FeatureStateFeatureState36 FeatureState() {
37 enabled.resize(1024, 0);
38 }
39 std::vector<bool> enabled;
40 };
41
42 static FeatureState sFeatureState;
43
44 // Call this function first to initialize the feature control.
feature_initialize()45 void feature_initialize() { }
46
47 // Get the access rules given by |name| if they exist, otherwise returns NULL
feature_is_enabled(Feature feature)48 bool feature_is_enabled(Feature feature) {
49 return android::featurecontrol::sFeatureEnabledCb ?
50 android::featurecontrol::sFeatureEnabledCb(
51 static_cast<android::featurecontrol::Feature>(feature))
52 : sFeatureState.enabled[feature];
53 }
54
feature_set_enabled_override(Feature feature,bool isEnabled)55 void feature_set_enabled_override(Feature feature, bool isEnabled) {
56 sFeatureState.enabled[feature] = isEnabled;
57 }
58
feature_reset_enabled_to_default(Feature feature)59 void feature_reset_enabled_to_default(Feature feature) {
60 sFeatureState.enabled[feature] = false;
61 }
62
63 // Set the feature if it is not user-overriden.
feature_set_if_not_overridden(Feature feature,bool enable)64 void feature_set_if_not_overridden(Feature feature, bool enable) {
65 sFeatureState.enabled[feature] = enable;
66 }
67
68 // 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)69 void feature_set_if_not_overridden_or_guest_disabled(Feature feature, bool enable) {
70 sFeatureState.enabled[feature] = enable;
71 }
72
73 // Runs applyCachedServerFeaturePatterns then
74 // asyncUpdateServerFeaturePatterns. See FeatureControl.h
75 // for more info. To be called only once on startup.
feature_update_from_server()76 void feature_update_from_server() { }
77
feature_name(Feature feature)78 const char* feature_name(Feature feature) {
79 static const std::vector<std::string>* const sFeatureNames = [] {
80 return new std::vector<std::string>{
81 #define FEATURE_CONTROL_ITEM(item) #item,
82 #include "FeatureControlDefHost.h"
83 #include "FeatureControlDefGuest.h"
84 #undef FEATURE_CONTROL_ITEM
85 };
86 }();
87
88 if (feature >= sFeatureNames->size()) {
89 return "InvalidFeature";
90 }
91
92 return (*sFeatureNames)[feature].c_str();
93 }
94