1 /* 2 * Copyright (C) 2017 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 #pragma once 18 19 #include "config/ConfigKey.h" 20 #include "config/ConfigListener.h" 21 22 #include <aidl/android/os/IPendingIntentRef.h> 23 #include <mutex> 24 #include <string> 25 26 #include <stdio.h> 27 28 using aidl::android::os::IPendingIntentRef; 29 using std::shared_ptr; 30 31 namespace android { 32 namespace os { 33 namespace statsd { 34 35 /** 36 * Keeps track of which configurations have been set from various sources. 37 */ 38 class ConfigManager : public virtual RefBase { 39 public: 40 ConfigManager(); 41 virtual ~ConfigManager(); 42 43 /** 44 * Initialize ConfigListener by reading from disk and get updates. 45 */ 46 void Startup(); 47 48 /* 49 * No-op initializer for tests. 50 */ 51 void StartupForTest(); 52 53 /** 54 * Someone else wants to know about the configs. 55 */ 56 void AddListener(const sp<ConfigListener>& listener); 57 58 /** 59 * A configuration was added or updated. 60 * 61 * Reports this to listeners. 62 */ 63 void UpdateConfig(const ConfigKey& key, const StatsdConfig& data); 64 65 /** 66 * Sets the broadcast receiver for a configuration key. 67 */ 68 void SetConfigReceiver(const ConfigKey& key, const shared_ptr<IPendingIntentRef>& pir); 69 70 /** 71 * Returns the package name and class name representing the broadcast receiver for this config. 72 */ 73 const shared_ptr<IPendingIntentRef> GetConfigReceiver(const ConfigKey& key) const; 74 75 /** 76 * Returns all config keys registered. 77 */ 78 std::vector<ConfigKey> GetAllConfigKeys() const; 79 80 /** 81 * Erase any broadcast receiver associated with this config key. 82 */ 83 void RemoveConfigReceiver(const ConfigKey& key); 84 85 /** 86 * Erase the broadcast receiver for this config key if it is equal to the provided broadcast 87 * receiver. 88 */ 89 void RemoveConfigReceiver(const ConfigKey& key, const shared_ptr<IPendingIntentRef>& pir); 90 91 /** 92 * Sets the broadcast receiver that is notified whenever the list of active configs 93 * changes for this uid. 94 */ 95 void SetActiveConfigsChangedReceiver(const int uid, const shared_ptr<IPendingIntentRef>& pir); 96 97 /** 98 * Returns the broadcast receiver for active configs changed for this uid. 99 */ 100 101 const shared_ptr<IPendingIntentRef> GetActiveConfigsChangedReceiver(const int uid) const; 102 103 /** 104 * Erase any active configs changed broadcast receiver associated with this uid. 105 */ 106 void RemoveActiveConfigsChangedReceiver(const int uid); 107 108 /** 109 * Erase the active configs changed broadcast receiver associated with this uid if it is equal 110 * to the provided broadcast receiver. 111 */ 112 void RemoveActiveConfigsChangedReceiver(const int uid, 113 const shared_ptr<IPendingIntentRef>& pir); 114 115 /** 116 * A configuration was removed. 117 * 118 * Reports this to listeners. 119 */ 120 void RemoveConfig(const ConfigKey& key); 121 122 /** 123 * Remove all of the configs for the given uid. 124 */ 125 void RemoveConfigs(int uid); 126 127 /** 128 * Remove all of the configs from memory. 129 */ 130 void RemoveAllConfigs(); 131 132 /** 133 * Text dump of our state for debugging. 134 */ 135 void Dump(FILE* out); 136 137 private: 138 mutable std::mutex mMutex; 139 140 /** 141 * Save the configs to disk. 142 */ 143 void update_saved_configs_locked(const ConfigKey& key, 144 const std::vector<uint8_t>& buffer, 145 const int numBytes); 146 147 /** 148 * Remove saved configs from disk. 149 */ 150 void remove_saved_configs(const ConfigKey& key); 151 152 /** 153 * Maps from uid to the config keys that have been set. 154 */ 155 std::map<int, std::set<ConfigKey>> mConfigs; 156 157 /** 158 * Each config key can be subscribed by up to one receiver, specified as IPendingIntentRef. 159 */ 160 std::map<ConfigKey, shared_ptr<IPendingIntentRef>> mConfigReceivers; 161 162 /** 163 * Each uid can be subscribed by up to one receiver to notify that the list of active configs 164 * for this uid has changed. The receiver is specified as IPendingIntentRef. 165 */ 166 std::map<int, shared_ptr<IPendingIntentRef>> mActiveConfigsChangedReceivers; 167 168 /** 169 * The ConfigListeners that will be told about changes. 170 */ 171 std::vector<sp<ConfigListener>> mListeners; 172 }; 173 174 } // namespace statsd 175 } // namespace os 176 } // namespace android 177