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