Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 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 | |
satayev | 134c97a | 2022-02-09 12:45:25 +0000 | [diff] [blame] | 17 | #define STATSD_DEBUG false // STOPSHIP if true |
Yao Chen | 341db89 | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 18 | #include "Log.h" |
| 19 | |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 20 | #include "config/ConfigManager.h" |
yro | 1528e0f | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 21 | #include "storage/StorageManager.h" |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 22 | |
Yao Chen | 341db89 | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 23 | #include "guardrail/StatsdStats.h" |
Yangster-mac | e9b52eb | 2018-04-02 14:37:33 -0700 | [diff] [blame] | 24 | #include "stats_log_util.h" |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 25 | #include "stats_util.h" |
Yangster-mac | f50f12b | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 26 | #include "stats_log_util.h" |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 27 | |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 28 | #include <stdio.h> |
yro | d3a2436 | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 29 | #include <vector> |
| 30 | #include "android-base/stringprintf.h" |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 31 | |
| 32 | namespace android { |
| 33 | namespace os { |
| 34 | namespace statsd { |
| 35 | |
Yao Chen | a277da3 | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 36 | using std::string; |
| 37 | using std::vector; |
| 38 | |
Tej Singh | 0740768 | 2023-02-15 02:09:54 -0800 | [diff] [blame] | 39 | using Status = ::ndk::ScopedAStatus; |
| 40 | |
yro | 0684915 | 2017-12-12 00:17:50 -0800 | [diff] [blame] | 41 | #define STATS_SERVICE_DIR "/data/misc/stats-service" |
yro | d3a2436 | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 42 | |
yro | d3a2436 | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 43 | using android::base::StringPrintf; |
yro | d3a2436 | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 44 | |
Tej Singh | 81e38f4 | 2021-10-19 23:40:07 -0700 | [diff] [blame] | 45 | ConfigManager::ConfigManager() { |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | ConfigManager::~ConfigManager() { |
| 49 | } |
| 50 | |
| 51 | void ConfigManager::Startup() { |
Yao Chen | a277da3 | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 52 | map<ConfigKey, StatsdConfig> configsFromDisk; |
| 53 | StorageManager::readConfigFromDisk(configsFromDisk); |
Vova Sharaienko | 8d42bc3 | 2024-01-04 19:08:27 +0000 | [diff] [blame] | 54 | for (const auto& config : configsFromDisk) { |
| 55 | UpdateConfig(config.first, config.second); |
yro | 1d5bb08 | 2018-01-04 14:57:45 -0800 | [diff] [blame] | 56 | } |
| 57 | } |
Yao Chen | 0cf6189 | 2017-12-16 14:34:20 -0800 | [diff] [blame] | 58 | |
yro | 1d5bb08 | 2018-01-04 14:57:45 -0800 | [diff] [blame] | 59 | void ConfigManager::StartupForTest() { |
Kelly Rossmoyer | a685f90 | 2020-07-29 21:22:15 +0000 | [diff] [blame] | 60 | // No-op function to avoid reading configs from disks for tests. |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void ConfigManager::AddListener(const sp<ConfigListener>& listener) { |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 64 | lock_guard<mutex> lock(mMutex); |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 65 | mListeners.push_back(listener); |
| 66 | } |
| 67 | |
| 68 | void ConfigManager::UpdateConfig(const ConfigKey& key, const StatsdConfig& config) { |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 69 | vector<sp<ConfigListener>> broadcastList; |
| 70 | { |
| 71 | lock_guard <mutex> lock(mMutex); |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 72 | |
yro | 2918b55 | 2018-03-12 20:44:05 -0700 | [diff] [blame] | 73 | const int numBytes = config.ByteSize(); |
| 74 | vector<uint8_t> buffer(numBytes); |
Muhammad Qureshi | f4951fb | 2022-03-29 07:00:28 -0700 | [diff] [blame] | 75 | config.SerializeToArray(buffer.data(), numBytes); |
yro | 2918b55 | 2018-03-12 20:44:05 -0700 | [diff] [blame] | 76 | |
Yao Chen | 341db89 | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 77 | auto uidIt = mConfigs.find(key.GetUid()); |
| 78 | // GuardRail: Limit the number of configs per uid. |
| 79 | if (uidIt != mConfigs.end()) { |
| 80 | auto it = uidIt->second.find(key); |
| 81 | if (it == uidIt->second.end() && |
| 82 | uidIt->second.size() >= StatsdStats::kMaxConfigCountPerUid) { |
| 83 | ALOGE("ConfigManager: uid %d has exceeded the config count limit", key.GetUid()); |
| 84 | return; |
| 85 | } |
| 86 | } |
yro | 2918b55 | 2018-03-12 20:44:05 -0700 | [diff] [blame] | 87 | |
Yao Chen | 341db89 | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 88 | // Check if it's a duplicate config. |
| 89 | if (uidIt != mConfigs.end() && uidIt->second.find(key) != uidIt->second.end() && |
| 90 | StorageManager::hasIdenticalConfig(key, buffer)) { |
| 91 | // This is a duplicate config. |
| 92 | ALOGI("ConfigManager This is a duplicate config %s", key.ToString().c_str()); |
| 93 | // Update saved file on disk. We still update timestamp of file when |
| 94 | // there exists a duplicate configuration to avoid garbage collection. |
| 95 | update_saved_configs_locked(key, buffer, numBytes); |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | // Update saved file on disk. |
yro | 2918b55 | 2018-03-12 20:44:05 -0700 | [diff] [blame] | 100 | update_saved_configs_locked(key, buffer, numBytes); |
| 101 | |
Yao Chen | 341db89 | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 102 | // Add to set. |
| 103 | mConfigs[key.GetUid()].insert(key); |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 104 | |
Vova Sharaienko | d9a033d | 2022-10-27 23:19:37 +0000 | [diff] [blame] | 105 | broadcastList = mListeners; |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 106 | } |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 107 | |
Yangster-mac | e9b52eb | 2018-04-02 14:37:33 -0700 | [diff] [blame] | 108 | const int64_t timestampNs = getElapsedRealtimeNs(); |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 109 | // Tell everyone |
Chih-Hung Hsieh | fa35ea8 | 2018-12-11 11:09:20 -0800 | [diff] [blame] | 110 | for (const sp<ConfigListener>& listener : broadcastList) { |
Yangster-mac | e9b52eb | 2018-04-02 14:37:33 -0700 | [diff] [blame] | 111 | listener->OnConfigUpdated(timestampNs, key, config); |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
Jeffrey Huang | d0c3d34 | 2019-12-16 13:50:06 -0800 | [diff] [blame] | 115 | void ConfigManager::SetConfigReceiver(const ConfigKey& key, |
Ruchir Rastogi | d36dd0b | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 116 | const shared_ptr<IPendingIntentRef>& pir) { |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 117 | lock_guard<mutex> lock(mMutex); |
Jeffrey Huang | d0c3d34 | 2019-12-16 13:50:06 -0800 | [diff] [blame] | 118 | mConfigReceivers[key] = pir; |
David Chen | ec84d13 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void ConfigManager::RemoveConfigReceiver(const ConfigKey& key) { |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 122 | lock_guard<mutex> lock(mMutex); |
David Chen | ec84d13 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 123 | mConfigReceivers.erase(key); |
| 124 | } |
| 125 | |
Tej Singh | 81e38f4 | 2021-10-19 23:40:07 -0700 | [diff] [blame] | 126 | void ConfigManager::RemoveConfigReceiver(const ConfigKey& key, |
| 127 | const shared_ptr<IPendingIntentRef>& pir) { |
| 128 | lock_guard<mutex> lock(mMutex); |
| 129 | auto it = mConfigReceivers.find(key); |
| 130 | if (it != mConfigReceivers.end() && it->second == pir) { |
| 131 | mConfigReceivers.erase(key); |
| 132 | } |
| 133 | } |
| 134 | |
Tej Singh | fad59e3 | 2019-01-22 11:33:51 -0800 | [diff] [blame] | 135 | void ConfigManager::SetActiveConfigsChangedReceiver(const int uid, |
Ruchir Rastogi | d36dd0b | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 136 | const shared_ptr<IPendingIntentRef>& pir) { |
Tej Singh | 81e38f4 | 2021-10-19 23:40:07 -0700 | [diff] [blame] | 137 | lock_guard<mutex> lock(mMutex); |
| 138 | mActiveConfigsChangedReceivers[uid] = pir; |
Tej Singh | fad59e3 | 2019-01-22 11:33:51 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | void ConfigManager::RemoveActiveConfigsChangedReceiver(const int uid) { |
| 142 | lock_guard<mutex> lock(mMutex); |
| 143 | mActiveConfigsChangedReceivers.erase(uid); |
| 144 | } |
| 145 | |
Tej Singh | 81e38f4 | 2021-10-19 23:40:07 -0700 | [diff] [blame] | 146 | void ConfigManager::RemoveActiveConfigsChangedReceiver(const int uid, |
| 147 | const shared_ptr<IPendingIntentRef>& pir) { |
| 148 | lock_guard<mutex> lock(mMutex); |
| 149 | auto it = mActiveConfigsChangedReceivers.find(uid); |
| 150 | if (it != mActiveConfigsChangedReceivers.end() && it->second == pir) { |
| 151 | mActiveConfigsChangedReceivers.erase(uid); |
| 152 | } |
| 153 | } |
| 154 | |
Tej Singh | 0740768 | 2023-02-15 02:09:54 -0800 | [diff] [blame] | 155 | void ConfigManager::SetRestrictedMetricsChangedReceiver(const string& configPackage, |
| 156 | const int64_t configId, |
| 157 | const int32_t callingUid, |
| 158 | const shared_ptr<IPendingIntentRef>& pir) { |
| 159 | lock_guard<mutex> lock(mMutex); |
| 160 | ConfigKeyWithPackage configKey(configPackage, configId); |
| 161 | mRestrictedMetricsChangedReceivers[configKey][callingUid] = pir; |
| 162 | } |
| 163 | |
| 164 | void ConfigManager::RemoveRestrictedMetricsChangedReceiver(const string& configPackage, |
| 165 | const int64_t configId, |
| 166 | const int32_t callingUid) { |
| 167 | lock_guard<mutex> lock(mMutex); |
| 168 | ConfigKeyWithPackage configKey(configPackage, configId); |
| 169 | const auto& it = mRestrictedMetricsChangedReceivers.find(configKey); |
| 170 | if (it != mRestrictedMetricsChangedReceivers.end()) { |
| 171 | it->second.erase(callingUid); |
| 172 | if (it->second.empty()) { |
| 173 | mRestrictedMetricsChangedReceivers.erase(it); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
Tej Singh | d6efe1f | 2023-03-16 23:56:37 -0700 | [diff] [blame] | 178 | void ConfigManager::RemoveRestrictedMetricsChangedReceiver( |
| 179 | const ConfigKeyWithPackage& key, const int32_t delegateUid, |
| 180 | const shared_ptr<IPendingIntentRef>& pir) { |
| 181 | lock_guard<mutex> lock(mMutex); |
| 182 | const auto& it = mRestrictedMetricsChangedReceivers.find(key); |
| 183 | if (it != mRestrictedMetricsChangedReceivers.end()) { |
| 184 | const auto& pirIt = it->second.find(delegateUid); |
| 185 | if (pirIt != it->second.end() && pirIt->second == pir) { |
| 186 | it->second.erase(delegateUid); |
| 187 | if (it->second.empty()) { |
| 188 | mRestrictedMetricsChangedReceivers.erase(it); |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
Tej Singh | 0740768 | 2023-02-15 02:09:54 -0800 | [diff] [blame] | 194 | void ConfigManager::SendRestrictedMetricsBroadcast(const set<string>& configPackages, |
| 195 | const int64_t configId, |
| 196 | const set<int32_t>& delegateUids, |
| 197 | const vector<int64_t>& metricIds) { |
Tej Singh | d6efe1f | 2023-03-16 23:56:37 -0700 | [diff] [blame] | 198 | map<ConfigKeyWithPackage, map<int32_t, shared_ptr<IPendingIntentRef>>> intentsToSend; |
Tej Singh | 0740768 | 2023-02-15 02:09:54 -0800 | [diff] [blame] | 199 | { |
| 200 | lock_guard<mutex> lock(mMutex); |
| 201 | // Invoke the pending intent for all matching configs, as long as the listening delegates |
| 202 | // match the allowed delegate uids specified by the config. |
| 203 | for (const string& configPackage : configPackages) { |
| 204 | ConfigKeyWithPackage key(configPackage, configId); |
| 205 | const auto& it = mRestrictedMetricsChangedReceivers.find(key); |
| 206 | if (it != mRestrictedMetricsChangedReceivers.end()) { |
| 207 | for (const auto& [delegateUid, pir] : it->second) { |
| 208 | if (delegateUids.find(delegateUid) != delegateUids.end()) { |
Tej Singh | d6efe1f | 2023-03-16 23:56:37 -0700 | [diff] [blame] | 209 | intentsToSend[key][delegateUid] = pir; |
Tej Singh | 0740768 | 2023-02-15 02:09:54 -0800 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | // Invoke the pending intents without holding the lock. |
Tej Singh | d6efe1f | 2023-03-16 23:56:37 -0700 | [diff] [blame] | 217 | for (const auto& [key, innerMap] : intentsToSend) { |
| 218 | for (const auto& [delegateUid, pir] : innerMap) { |
| 219 | Status status = pir->sendRestrictedMetricsChangedBroadcast(metricIds); |
| 220 | if (status.isOk()) { |
| 221 | VLOG("ConfigManager::SendRestrictedMetricsBroadcast succeeded"); |
| 222 | } |
| 223 | if (status.getExceptionCode() == EX_TRANSACTION_FAILED && |
| 224 | status.getStatus() == STATUS_DEAD_OBJECT) { |
| 225 | // Must also be called without the lock, since remove will acquire the lock. |
| 226 | RemoveRestrictedMetricsChangedReceiver(key, delegateUid, pir); |
| 227 | } |
Tej Singh | 0740768 | 2023-02-15 02:09:54 -0800 | [diff] [blame] | 228 | } |
Tej Singh | 0740768 | 2023-02-15 02:09:54 -0800 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 232 | void ConfigManager::RemoveConfig(const ConfigKey& key) { |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 233 | vector<sp<ConfigListener>> broadcastList; |
| 234 | { |
| 235 | lock_guard <mutex> lock(mMutex); |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 236 | |
Tej Singh | a783d52 | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 237 | auto uid = key.GetUid(); |
| 238 | auto uidIt = mConfigs.find(uid); |
Yao Chen | 341db89 | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 239 | if (uidIt != mConfigs.end() && uidIt->second.find(key) != uidIt->second.end()) { |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 240 | // Remove from map |
Yao Chen | 341db89 | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 241 | uidIt->second.erase(key); |
Tej Singh | a783d52 | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 242 | |
Vova Sharaienko | d9a033d | 2022-10-27 23:19:37 +0000 | [diff] [blame] | 243 | broadcastList = mListeners; |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 244 | } |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 245 | |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 246 | // Remove from disk. There can still be a lingering file on disk so we check |
| 247 | // whether or not the config was on memory. |
| 248 | remove_saved_configs(key); |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 249 | } |
yro | 9bb4f7d | 2017-11-19 14:33:56 -0800 | [diff] [blame] | 250 | |
Chih-Hung Hsieh | fa35ea8 | 2018-12-11 11:09:20 -0800 | [diff] [blame] | 251 | for (const sp<ConfigListener>& listener:broadcastList) { |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 252 | listener->OnConfigRemoved(key); |
| 253 | } |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 254 | } |
| 255 | |
yro | d3a2436 | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 256 | void ConfigManager::remove_saved_configs(const ConfigKey& key) { |
yro | 0bd9aa1 | 2018-02-13 22:06:34 -0800 | [diff] [blame] | 257 | string suffix = StringPrintf("%d_%lld", key.GetUid(), (long long)key.GetId()); |
yro | a8c4ebf | 2018-01-22 18:37:27 -0800 | [diff] [blame] | 258 | StorageManager::deleteSuffixedFiles(STATS_SERVICE_DIR, suffix.c_str()); |
yro | d3a2436 | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 259 | } |
| 260 | |
Tej Singh | 0740768 | 2023-02-15 02:09:54 -0800 | [diff] [blame] | 261 | // TODO(b/xxx): consider removing all receivers associated with this uid. |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 262 | void ConfigManager::RemoveConfigs(int uid) { |
| 263 | vector<ConfigKey> removed; |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 264 | vector<sp<ConfigListener>> broadcastList; |
| 265 | { |
| 266 | lock_guard <mutex> lock(mMutex); |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 267 | |
Yao Chen | 341db89 | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 268 | auto uidIt = mConfigs.find(uid); |
| 269 | if (uidIt == mConfigs.end()) { |
| 270 | return; |
| 271 | } |
| 272 | |
| 273 | for (auto it = uidIt->second.begin(); it != uidIt->second.end(); ++it) { |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 274 | // Remove from map |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 275 | remove_saved_configs(*it); |
| 276 | removed.push_back(*it); |
Tej Singh | fad59e3 | 2019-01-22 11:33:51 -0800 | [diff] [blame] | 277 | } |
| 278 | |
Yao Chen | 341db89 | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 279 | mConfigs.erase(uidIt); |
| 280 | |
Vova Sharaienko | d9a033d | 2022-10-27 23:19:37 +0000 | [diff] [blame] | 281 | broadcastList = mListeners; |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | // Remove separately so if they do anything in the callback they can't mess up our iteration. |
| 285 | for (auto& key : removed) { |
| 286 | // Tell everyone |
Chih-Hung Hsieh | fa35ea8 | 2018-12-11 11:09:20 -0800 | [diff] [blame] | 287 | for (const sp<ConfigListener>& listener:broadcastList) { |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 288 | listener->OnConfigRemoved(key); |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
yro | 11e2da5 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 293 | void ConfigManager::RemoveAllConfigs() { |
| 294 | vector<ConfigKey> removed; |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 295 | vector<sp<ConfigListener>> broadcastList; |
| 296 | { |
| 297 | lock_guard <mutex> lock(mMutex); |
yro | 11e2da5 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 298 | |
Yao Chen | 341db89 | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 299 | for (auto uidIt = mConfigs.begin(); uidIt != mConfigs.end();) { |
| 300 | for (auto it = uidIt->second.begin(); it != uidIt->second.end();) { |
| 301 | // Remove from map |
| 302 | removed.push_back(*it); |
| 303 | it = uidIt->second.erase(it); |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 304 | } |
Yao Chen | 341db89 | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 305 | uidIt = mConfigs.erase(uidIt); |
yro | 11e2da5 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 306 | } |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 307 | |
Vova Sharaienko | d9a033d | 2022-10-27 23:19:37 +0000 | [diff] [blame] | 308 | broadcastList = mListeners; |
yro | 11e2da5 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | // Remove separately so if they do anything in the callback they can't mess up our iteration. |
| 312 | for (auto& key : removed) { |
| 313 | // Tell everyone |
Chih-Hung Hsieh | fa35ea8 | 2018-12-11 11:09:20 -0800 | [diff] [blame] | 314 | for (const sp<ConfigListener>& listener:broadcastList) { |
yro | 11e2da5 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 315 | listener->OnConfigRemoved(key); |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
Yangster | d1df8ed | 2017-11-22 14:24:24 -0800 | [diff] [blame] | 320 | vector<ConfigKey> ConfigManager::GetAllConfigKeys() const { |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 321 | lock_guard<mutex> lock(mMutex); |
| 322 | |
David Chen | dd4e803 | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 323 | vector<ConfigKey> ret; |
Yao Chen | 341db89 | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 324 | for (auto uidIt = mConfigs.cbegin(); uidIt != mConfigs.cend(); ++uidIt) { |
| 325 | for (auto it = uidIt->second.cbegin(); it != uidIt->second.cend(); ++it) { |
| 326 | ret.push_back(*it); |
| 327 | } |
David Chen | dd4e803 | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 328 | } |
| 329 | return ret; |
| 330 | } |
| 331 | |
Ruchir Rastogi | d36dd0b | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 332 | const shared_ptr<IPendingIntentRef> ConfigManager::GetConfigReceiver(const ConfigKey& key) const { |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 333 | lock_guard<mutex> lock(mMutex); |
| 334 | |
David Chen | dd4e803 | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 335 | auto it = mConfigReceivers.find(key); |
| 336 | if (it == mConfigReceivers.end()) { |
David Chen | 29ada1e | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 337 | return nullptr; |
David Chen | dd4e803 | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 338 | } else { |
| 339 | return it->second; |
| 340 | } |
| 341 | } |
| 342 | |
Ruchir Rastogi | d36dd0b | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 343 | const shared_ptr<IPendingIntentRef> ConfigManager::GetActiveConfigsChangedReceiver(const int uid) |
| 344 | const { |
Tej Singh | fad59e3 | 2019-01-22 11:33:51 -0800 | [diff] [blame] | 345 | lock_guard<mutex> lock(mMutex); |
| 346 | |
| 347 | auto it = mActiveConfigsChangedReceivers.find(uid); |
| 348 | if (it == mActiveConfigsChangedReceivers.end()) { |
| 349 | return nullptr; |
| 350 | } else { |
| 351 | return it->second; |
| 352 | } |
| 353 | } |
| 354 | |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 355 | void ConfigManager::Dump(FILE* out) { |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 356 | lock_guard<mutex> lock(mMutex); |
| 357 | |
Yao Chen | 341db89 | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 358 | fprintf(out, "CONFIGURATIONS\n"); |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 359 | fprintf(out, " uid name\n"); |
Yao Chen | 341db89 | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 360 | for (auto uidIt = mConfigs.cbegin(); uidIt != mConfigs.cend(); ++uidIt) { |
| 361 | for (auto it = uidIt->second.cbegin(); it != uidIt->second.cend(); ++it) { |
| 362 | fprintf(out, " %6d %lld\n", it->GetUid(), (long long)it->GetId()); |
| 363 | auto receiverIt = mConfigReceivers.find(*it); |
| 364 | if (receiverIt != mConfigReceivers.end()) { |
| 365 | fprintf(out, " -> received by PendingIntent as binder\n"); |
| 366 | } |
David Chen | dd4e803 | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 367 | } |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | |
yro | 2918b55 | 2018-03-12 20:44:05 -0700 | [diff] [blame] | 371 | void ConfigManager::update_saved_configs_locked(const ConfigKey& key, |
| 372 | const vector<uint8_t>& buffer, |
| 373 | const int numBytes) { |
yro | 9bb4f7d | 2017-11-19 14:33:56 -0800 | [diff] [blame] | 374 | // If there is a pre-existing config with same key we should first delete it. |
| 375 | remove_saved_configs(key); |
| 376 | |
| 377 | // Then we save the latest config. |
yro | 2918b55 | 2018-03-12 20:44:05 -0700 | [diff] [blame] | 378 | string file_name = |
| 379 | StringPrintf("%s/%ld_%d_%lld", STATS_SERVICE_DIR, time(nullptr), |
| 380 | key.GetUid(), (long long)key.GetId()); |
yro | 1528e0f | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 381 | StorageManager::writeFile(file_name.c_str(), &buffer[0], numBytes); |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 384 | } // namespace statsd |
| 385 | } // namespace os |
| 386 | } // namespace android |