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 android::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 * Dummy 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 * Sets the broadcast receiver that is notified whenever the list of active configs 87 * changes for this uid. 88 */ 89 void SetActiveConfigsChangedReceiver(const int uid, const shared_ptr<IPendingIntentRef>& pir); 90 91 /** 92 * Returns the broadcast receiver for active configs changed for this uid. 93 */ 94 95 const shared_ptr<IPendingIntentRef> GetActiveConfigsChangedReceiver(const int uid) const; 96 97 /** 98 * Erase any active configs changed broadcast receiver associated with this uid. 99 */ 100 void RemoveActiveConfigsChangedReceiver(const int uid); 101 102 /** 103 * A configuration was removed. 104 * 105 * Reports this to listeners. 106 */ 107 void RemoveConfig(const ConfigKey& key); 108 109 /** 110 * Remove all of the configs for the given uid. 111 */ 112 void RemoveConfigs(int uid); 113 114 /** 115 * Remove all of the configs from memory. 116 */ 117 void RemoveAllConfigs(); 118 119 /** 120 * Text dump of our state for debugging. 121 */ 122 void Dump(FILE* out); 123 124 private: 125 mutable std::mutex mMutex; 126 127 /** 128 * Save the configs to disk. 129 */ 130 void update_saved_configs_locked(const ConfigKey& key, 131 const std::vector<uint8_t>& buffer, 132 const int numBytes); 133 134 /** 135 * Remove saved configs from disk. 136 */ 137 void remove_saved_configs(const ConfigKey& key); 138 139 /** 140 * Maps from uid to the config keys that have been set. 141 */ 142 std::map<int, std::set<ConfigKey>> mConfigs; 143 144 /** 145 * Each config key can be subscribed by up to one receiver, specified as IPendingIntentRef. 146 */ 147 std::map<ConfigKey, shared_ptr<IPendingIntentRef>> 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 IPendingIntentRef. 152 */ 153 std::map<int, shared_ptr<IPendingIntentRef>> mActiveConfigsChangedReceivers; 154 155 /** 156 * The ConfigListeners that will be told about changes. 157 */ 158 std::vector<sp<ConfigListener>> mListeners; 159 160 // Death recipients that are triggered when the host process holding an 161 // IPendingIntentRef dies. 162 ::ndk::ScopedAIBinder_DeathRecipient mConfigReceiverDeathRecipient; 163 ::ndk::ScopedAIBinder_DeathRecipient mActiveConfigChangedReceiverDeathRecipient; 164 165 /** 166 * Death recipient callback that is called when a config receiver dies. 167 * The cookie is a pointer to a ConfigReceiverDeathCookie. 168 */ 169 static void configReceiverDied(void* cookie); 170 171 /** 172 * Death recipient callback that is called when an active config changed 173 * receiver dies. The cookie is a pointer to an 174 * ActiveConfigChangedReceiverDeathCookie. 175 */ 176 static void activeConfigChangedReceiverDied(void* cookie); 177 }; 178 179 } // namespace statsd 180 } // namespace os 181 } // namespace android 182