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