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