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