Joe Onorato | 9fc9edf | 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 | |
Yao Chen | 52b478b | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 17 | #define DEBUG false // STOPSHIP if true |
| 18 | #include "Log.h" |
| 19 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 20 | #include "config/ConfigManager.h" |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 21 | #include "storage/StorageManager.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 22 | |
Yao Chen | 52b478b | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 23 | #include "guardrail/StatsdStats.h" |
Yangster-mac | c04feba | 2018-04-02 14:37:33 -0700 | [diff] [blame] | 24 | #include "stats_log_util.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 25 | #include "stats_util.h" |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 26 | #include "stats_log_util.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 27 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 28 | #include <stdio.h> |
yro | 87d983c | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 29 | #include <vector> |
| 30 | #include "android-base/stringprintf.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 31 | |
| 32 | namespace android { |
| 33 | namespace os { |
| 34 | namespace statsd { |
| 35 | |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 36 | using std::pair; |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 37 | using std::string; |
| 38 | using std::vector; |
| 39 | |
yro | 03faf09 | 2017-12-12 00:17:50 -0800 | [diff] [blame] | 40 | #define STATS_SERVICE_DIR "/data/misc/stats-service" |
yro | 87d983c | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 41 | |
yro | 87d983c | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 42 | using android::base::StringPrintf; |
| 43 | using std::unique_ptr; |
| 44 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 45 | struct ConfigReceiverDeathCookie { |
Ruchir Rastogi | e20c53e | 2020-04-05 21:44:31 -0700 | [diff] [blame] | 46 | ConfigReceiverDeathCookie(const wp<ConfigManager>& configManager, const ConfigKey& configKey, |
| 47 | const shared_ptr<IPendingIntentRef>& pir) : |
| 48 | mConfigManager(configManager), mConfigKey(configKey), mPir(pir) { |
| 49 | } |
Jeffrey Huang | ad21374 | 2019-12-16 13:50:06 -0800 | [diff] [blame] | 50 | |
Ruchir Rastogi | e20c53e | 2020-04-05 21:44:31 -0700 | [diff] [blame] | 51 | wp<ConfigManager> mConfigManager; |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 52 | ConfigKey mConfigKey; |
| 53 | shared_ptr<IPendingIntentRef> mPir; |
Jeffrey Huang | ad21374 | 2019-12-16 13:50:06 -0800 | [diff] [blame] | 54 | }; |
| 55 | |
Ruchir Rastogi | 1497e8f | 2020-03-03 16:04:42 -0800 | [diff] [blame] | 56 | void ConfigManager::configReceiverDied(void* cookie) { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 57 | auto cookie_ = static_cast<ConfigReceiverDeathCookie*>(cookie); |
Ruchir Rastogi | e20c53e | 2020-04-05 21:44:31 -0700 | [diff] [blame] | 58 | sp<ConfigManager> thiz = cookie_->mConfigManager.promote(); |
| 59 | if (!thiz) { |
| 60 | return; |
| 61 | } |
| 62 | |
Ruchir Rastogi | 1497e8f | 2020-03-03 16:04:42 -0800 | [diff] [blame] | 63 | ConfigKey& configKey = cookie_->mConfigKey; |
| 64 | shared_ptr<IPendingIntentRef>& pir = cookie_->mPir; |
Jeffrey Huang | 47537a1 | 2020-01-06 15:35:34 -0800 | [diff] [blame] | 65 | |
Ruchir Rastogi | 1497e8f | 2020-03-03 16:04:42 -0800 | [diff] [blame] | 66 | // Erase the mapping from the config key to the config receiver (pir) if the |
| 67 | // mapping still exists. |
| 68 | lock_guard<mutex> lock(thiz->mMutex); |
| 69 | auto it = thiz->mConfigReceivers.find(configKey); |
| 70 | if (it != thiz->mConfigReceivers.end() && it->second == pir) { |
| 71 | thiz->mConfigReceivers.erase(configKey); |
Jeffrey Huang | 47537a1 | 2020-01-06 15:35:34 -0800 | [diff] [blame] | 72 | } |
Ruchir Rastogi | 1497e8f | 2020-03-03 16:04:42 -0800 | [diff] [blame] | 73 | |
| 74 | // The death recipient corresponding to this specific pir can never be |
| 75 | // triggered again, so free up resources. |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 76 | delete cookie_; |
| 77 | } |
| 78 | |
| 79 | struct ActiveConfigChangedReceiverDeathCookie { |
Ruchir Rastogi | e20c53e | 2020-04-05 21:44:31 -0700 | [diff] [blame] | 80 | ActiveConfigChangedReceiverDeathCookie(const wp<ConfigManager>& configManager, const int uid, |
| 81 | const shared_ptr<IPendingIntentRef>& pir) : |
| 82 | mConfigManager(configManager), mUid(uid), mPir(pir) { |
| 83 | } |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 84 | |
Ruchir Rastogi | e20c53e | 2020-04-05 21:44:31 -0700 | [diff] [blame] | 85 | wp<ConfigManager> mConfigManager; |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 86 | int mUid; |
| 87 | shared_ptr<IPendingIntentRef> mPir; |
Jeffrey Huang | 47537a1 | 2020-01-06 15:35:34 -0800 | [diff] [blame] | 88 | }; |
| 89 | |
Ruchir Rastogi | 1497e8f | 2020-03-03 16:04:42 -0800 | [diff] [blame] | 90 | void ConfigManager::activeConfigChangedReceiverDied(void* cookie) { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 91 | auto cookie_ = static_cast<ActiveConfigChangedReceiverDeathCookie*>(cookie); |
Ruchir Rastogi | e20c53e | 2020-04-05 21:44:31 -0700 | [diff] [blame] | 92 | sp<ConfigManager> thiz = cookie_->mConfigManager.promote(); |
| 93 | if (!thiz) { |
| 94 | return; |
| 95 | } |
| 96 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 97 | int uid = cookie_->mUid; |
Ruchir Rastogi | 1497e8f | 2020-03-03 16:04:42 -0800 | [diff] [blame] | 98 | shared_ptr<IPendingIntentRef>& pir = cookie_->mPir; |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 99 | |
Ruchir Rastogi | 1497e8f | 2020-03-03 16:04:42 -0800 | [diff] [blame] | 100 | // Erase the mapping from the config key to the active config changed |
| 101 | // receiver (pir) if the mapping still exists. |
| 102 | lock_guard<mutex> lock(thiz->mMutex); |
| 103 | auto it = thiz->mActiveConfigsChangedReceivers.find(uid); |
| 104 | if (it != thiz->mActiveConfigsChangedReceivers.end() && it->second == pir) { |
| 105 | thiz->mActiveConfigsChangedReceivers.erase(uid); |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 106 | } |
Ruchir Rastogi | 1497e8f | 2020-03-03 16:04:42 -0800 | [diff] [blame] | 107 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 108 | // The death recipient corresponding to this specific pir can never |
| 109 | // be triggered again, so free up resources. |
| 110 | delete cookie_; |
| 111 | } |
| 112 | |
Ruchir Rastogi | 1497e8f | 2020-03-03 16:04:42 -0800 | [diff] [blame] | 113 | ConfigManager::ConfigManager() : |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 114 | mConfigReceiverDeathRecipient(AIBinder_DeathRecipient_new(configReceiverDied)), |
| 115 | mActiveConfigChangedReceiverDeathRecipient( |
Ruchir Rastogi | 1497e8f | 2020-03-03 16:04:42 -0800 | [diff] [blame] | 116 | AIBinder_DeathRecipient_new(activeConfigChangedReceiverDied)) { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | ConfigManager::~ConfigManager() { |
| 120 | } |
| 121 | |
| 122 | void ConfigManager::Startup() { |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 123 | map<ConfigKey, StatsdConfig> configsFromDisk; |
| 124 | StorageManager::readConfigFromDisk(configsFromDisk); |
yro | 469cd80 | 2018-01-04 14:57:45 -0800 | [diff] [blame] | 125 | for (const auto& pair : configsFromDisk) { |
| 126 | UpdateConfig(pair.first, pair.second); |
| 127 | } |
| 128 | } |
Yao Chen | 3c0b95c | 2017-12-16 14:34:20 -0800 | [diff] [blame] | 129 | |
yro | 469cd80 | 2018-01-04 14:57:45 -0800 | [diff] [blame] | 130 | void ConfigManager::StartupForTest() { |
Kelly Rossmoyer | 6f37c91 | 2020-07-29 21:22:15 +0000 | [diff] [blame] | 131 | // No-op function to avoid reading configs from disks for tests. |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void ConfigManager::AddListener(const sp<ConfigListener>& listener) { |
David Chen | fdc123b | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 135 | lock_guard<mutex> lock(mMutex); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 136 | mListeners.push_back(listener); |
| 137 | } |
| 138 | |
| 139 | void ConfigManager::UpdateConfig(const ConfigKey& key, const StatsdConfig& config) { |
David Chen | fdc123b | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 140 | vector<sp<ConfigListener>> broadcastList; |
| 141 | { |
| 142 | lock_guard <mutex> lock(mMutex); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 143 | |
yro | 4490765 | 2018-03-12 20:44:05 -0700 | [diff] [blame] | 144 | const int numBytes = config.ByteSize(); |
| 145 | vector<uint8_t> buffer(numBytes); |
| 146 | config.SerializeToArray(&buffer[0], numBytes); |
| 147 | |
Yao Chen | 52b478b | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 148 | auto uidIt = mConfigs.find(key.GetUid()); |
| 149 | // GuardRail: Limit the number of configs per uid. |
| 150 | if (uidIt != mConfigs.end()) { |
| 151 | auto it = uidIt->second.find(key); |
| 152 | if (it == uidIt->second.end() && |
| 153 | uidIt->second.size() >= StatsdStats::kMaxConfigCountPerUid) { |
| 154 | ALOGE("ConfigManager: uid %d has exceeded the config count limit", key.GetUid()); |
| 155 | return; |
| 156 | } |
| 157 | } |
yro | 4490765 | 2018-03-12 20:44:05 -0700 | [diff] [blame] | 158 | |
Yao Chen | 52b478b | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 159 | // Check if it's a duplicate config. |
| 160 | if (uidIt != mConfigs.end() && uidIt->second.find(key) != uidIt->second.end() && |
| 161 | StorageManager::hasIdenticalConfig(key, buffer)) { |
| 162 | // This is a duplicate config. |
| 163 | ALOGI("ConfigManager This is a duplicate config %s", key.ToString().c_str()); |
| 164 | // Update saved file on disk. We still update timestamp of file when |
| 165 | // there exists a duplicate configuration to avoid garbage collection. |
| 166 | update_saved_configs_locked(key, buffer, numBytes); |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | // Update saved file on disk. |
yro | 4490765 | 2018-03-12 20:44:05 -0700 | [diff] [blame] | 171 | update_saved_configs_locked(key, buffer, numBytes); |
| 172 | |
Yao Chen | 52b478b | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 173 | // Add to set. |
| 174 | mConfigs[key.GetUid()].insert(key); |
David Chen | fdc123b | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 175 | |
Chih-Hung Hsieh | a1b644e | 2018-12-11 11:09:20 -0800 | [diff] [blame] | 176 | for (const sp<ConfigListener>& listener : mListeners) { |
David Chen | fdc123b | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 177 | broadcastList.push_back(listener); |
| 178 | } |
| 179 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 180 | |
Yangster-mac | c04feba | 2018-04-02 14:37:33 -0700 | [diff] [blame] | 181 | const int64_t timestampNs = getElapsedRealtimeNs(); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 182 | // Tell everyone |
Chih-Hung Hsieh | a1b644e | 2018-12-11 11:09:20 -0800 | [diff] [blame] | 183 | for (const sp<ConfigListener>& listener : broadcastList) { |
Yangster-mac | c04feba | 2018-04-02 14:37:33 -0700 | [diff] [blame] | 184 | listener->OnConfigUpdated(timestampNs, key, config); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
Jeffrey Huang | ad21374 | 2019-12-16 13:50:06 -0800 | [diff] [blame] | 188 | void ConfigManager::SetConfigReceiver(const ConfigKey& key, |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 189 | const shared_ptr<IPendingIntentRef>& pir) { |
David Chen | fdc123b | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 190 | lock_guard<mutex> lock(mMutex); |
Jeffrey Huang | ad21374 | 2019-12-16 13:50:06 -0800 | [diff] [blame] | 191 | mConfigReceivers[key] = pir; |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 192 | AIBinder_linkToDeath(pir->asBinder().get(), mConfigReceiverDeathRecipient.get(), |
| 193 | new ConfigReceiverDeathCookie(this, key, pir)); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | void ConfigManager::RemoveConfigReceiver(const ConfigKey& key) { |
David Chen | fdc123b | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 197 | lock_guard<mutex> lock(mMutex); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 198 | mConfigReceivers.erase(key); |
| 199 | } |
| 200 | |
Tej Singh | 2c9ef2a | 2019-01-22 11:33:51 -0800 | [diff] [blame] | 201 | void ConfigManager::SetActiveConfigsChangedReceiver(const int uid, |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 202 | const shared_ptr<IPendingIntentRef>& pir) { |
Ruchir Rastogi | 1497e8f | 2020-03-03 16:04:42 -0800 | [diff] [blame] | 203 | { |
| 204 | lock_guard<mutex> lock(mMutex); |
| 205 | mActiveConfigsChangedReceivers[uid] = pir; |
| 206 | } |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 207 | AIBinder_linkToDeath(pir->asBinder().get(), mActiveConfigChangedReceiverDeathRecipient.get(), |
| 208 | new ActiveConfigChangedReceiverDeathCookie(this, uid, pir)); |
Tej Singh | 2c9ef2a | 2019-01-22 11:33:51 -0800 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | void ConfigManager::RemoveActiveConfigsChangedReceiver(const int uid) { |
| 212 | lock_guard<mutex> lock(mMutex); |
| 213 | mActiveConfigsChangedReceivers.erase(uid); |
| 214 | } |
| 215 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 216 | void ConfigManager::RemoveConfig(const ConfigKey& key) { |
David Chen | fdc123b | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 217 | vector<sp<ConfigListener>> broadcastList; |
| 218 | { |
| 219 | lock_guard <mutex> lock(mMutex); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 220 | |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 221 | auto uid = key.GetUid(); |
| 222 | auto uidIt = mConfigs.find(uid); |
Yao Chen | 52b478b | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 223 | if (uidIt != mConfigs.end() && uidIt->second.find(key) != uidIt->second.end()) { |
David Chen | fdc123b | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 224 | // Remove from map |
Yao Chen | 52b478b | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 225 | uidIt->second.erase(key); |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 226 | |
Chih-Hung Hsieh | a1b644e | 2018-12-11 11:09:20 -0800 | [diff] [blame] | 227 | for (const sp<ConfigListener>& listener : mListeners) { |
David Chen | fdc123b | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 228 | broadcastList.push_back(listener); |
| 229 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 230 | } |
David Chen | fdc123b | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 231 | |
David Chen | fdc123b | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 232 | // Remove from disk. There can still be a lingering file on disk so we check |
| 233 | // whether or not the config was on memory. |
| 234 | remove_saved_configs(key); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 235 | } |
yro | 9c98c05 | 2017-11-19 14:33:56 -0800 | [diff] [blame] | 236 | |
Chih-Hung Hsieh | a1b644e | 2018-12-11 11:09:20 -0800 | [diff] [blame] | 237 | for (const sp<ConfigListener>& listener:broadcastList) { |
David Chen | fdc123b | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 238 | listener->OnConfigRemoved(key); |
| 239 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 240 | } |
| 241 | |
yro | 87d983c | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 242 | void ConfigManager::remove_saved_configs(const ConfigKey& key) { |
yro | f4ae56b | 2018-02-13 22:06:34 -0800 | [diff] [blame] | 243 | string suffix = StringPrintf("%d_%lld", key.GetUid(), (long long)key.GetId()); |
yro | e5f8292 | 2018-01-22 18:37:27 -0800 | [diff] [blame] | 244 | StorageManager::deleteSuffixedFiles(STATS_SERVICE_DIR, suffix.c_str()); |
yro | 87d983c | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 245 | } |
| 246 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 247 | void ConfigManager::RemoveConfigs(int uid) { |
| 248 | vector<ConfigKey> removed; |
David Chen | fdc123b | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 249 | vector<sp<ConfigListener>> broadcastList; |
| 250 | { |
| 251 | lock_guard <mutex> lock(mMutex); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 252 | |
Yao Chen | 52b478b | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 253 | auto uidIt = mConfigs.find(uid); |
| 254 | if (uidIt == mConfigs.end()) { |
| 255 | return; |
| 256 | } |
| 257 | |
| 258 | for (auto it = uidIt->second.begin(); it != uidIt->second.end(); ++it) { |
David Chen | fdc123b | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 259 | // Remove from map |
David Chen | fdc123b | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 260 | remove_saved_configs(*it); |
| 261 | removed.push_back(*it); |
Tej Singh | 2c9ef2a | 2019-01-22 11:33:51 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Yao Chen | 52b478b | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 264 | mConfigs.erase(uidIt); |
| 265 | |
Chih-Hung Hsieh | a1b644e | 2018-12-11 11:09:20 -0800 | [diff] [blame] | 266 | for (const sp<ConfigListener>& listener : mListeners) { |
David Chen | fdc123b | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 267 | broadcastList.push_back(listener); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
| 271 | // Remove separately so if they do anything in the callback they can't mess up our iteration. |
| 272 | for (auto& key : removed) { |
| 273 | // Tell everyone |
Chih-Hung Hsieh | a1b644e | 2018-12-11 11:09:20 -0800 | [diff] [blame] | 274 | for (const sp<ConfigListener>& listener:broadcastList) { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 275 | listener->OnConfigRemoved(key); |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 280 | void ConfigManager::RemoveAllConfigs() { |
| 281 | vector<ConfigKey> removed; |
David Chen | fdc123b | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 282 | vector<sp<ConfigListener>> broadcastList; |
| 283 | { |
| 284 | lock_guard <mutex> lock(mMutex); |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 285 | |
Yao Chen | 52b478b | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 286 | for (auto uidIt = mConfigs.begin(); uidIt != mConfigs.end();) { |
| 287 | for (auto it = uidIt->second.begin(); it != uidIt->second.end();) { |
| 288 | // Remove from map |
| 289 | removed.push_back(*it); |
| 290 | it = uidIt->second.erase(it); |
David Chen | fdc123b | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 291 | } |
Yao Chen | 52b478b | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 292 | uidIt = mConfigs.erase(uidIt); |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 293 | } |
David Chen | fdc123b | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 294 | |
Chih-Hung Hsieh | a1b644e | 2018-12-11 11:09:20 -0800 | [diff] [blame] | 295 | for (const sp<ConfigListener>& listener : mListeners) { |
David Chen | fdc123b | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 296 | broadcastList.push_back(listener); |
| 297 | } |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | // Remove separately so if they do anything in the callback they can't mess up our iteration. |
| 301 | for (auto& key : removed) { |
| 302 | // Tell everyone |
Chih-Hung Hsieh | a1b644e | 2018-12-11 11:09:20 -0800 | [diff] [blame] | 303 | for (const sp<ConfigListener>& listener:broadcastList) { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 304 | listener->OnConfigRemoved(key); |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
Yangster | 7c334a1 | 2017-11-22 14:24:24 -0800 | [diff] [blame] | 309 | vector<ConfigKey> ConfigManager::GetAllConfigKeys() const { |
David Chen | fdc123b | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 310 | lock_guard<mutex> lock(mMutex); |
| 311 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 312 | vector<ConfigKey> ret; |
Yao Chen | 52b478b | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 313 | for (auto uidIt = mConfigs.cbegin(); uidIt != mConfigs.cend(); ++uidIt) { |
| 314 | for (auto it = uidIt->second.cbegin(); it != uidIt->second.cend(); ++it) { |
| 315 | ret.push_back(*it); |
| 316 | } |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 317 | } |
| 318 | return ret; |
| 319 | } |
| 320 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 321 | const shared_ptr<IPendingIntentRef> ConfigManager::GetConfigReceiver(const ConfigKey& key) const { |
David Chen | fdc123b | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 322 | lock_guard<mutex> lock(mMutex); |
| 323 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 324 | auto it = mConfigReceivers.find(key); |
| 325 | if (it == mConfigReceivers.end()) { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 326 | return nullptr; |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 327 | } else { |
| 328 | return it->second; |
| 329 | } |
| 330 | } |
| 331 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 332 | const shared_ptr<IPendingIntentRef> ConfigManager::GetActiveConfigsChangedReceiver(const int uid) |
| 333 | const { |
Tej Singh | 2c9ef2a | 2019-01-22 11:33:51 -0800 | [diff] [blame] | 334 | lock_guard<mutex> lock(mMutex); |
| 335 | |
| 336 | auto it = mActiveConfigsChangedReceivers.find(uid); |
| 337 | if (it == mActiveConfigsChangedReceivers.end()) { |
| 338 | return nullptr; |
| 339 | } else { |
| 340 | return it->second; |
| 341 | } |
| 342 | } |
| 343 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 344 | void ConfigManager::Dump(FILE* out) { |
David Chen | fdc123b | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 345 | lock_guard<mutex> lock(mMutex); |
| 346 | |
Yao Chen | 52b478b | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 347 | fprintf(out, "CONFIGURATIONS\n"); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 348 | fprintf(out, " uid name\n"); |
Yao Chen | 52b478b | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 349 | for (auto uidIt = mConfigs.cbegin(); uidIt != mConfigs.cend(); ++uidIt) { |
| 350 | for (auto it = uidIt->second.cbegin(); it != uidIt->second.cend(); ++it) { |
| 351 | fprintf(out, " %6d %lld\n", it->GetUid(), (long long)it->GetId()); |
| 352 | auto receiverIt = mConfigReceivers.find(*it); |
| 353 | if (receiverIt != mConfigReceivers.end()) { |
| 354 | fprintf(out, " -> received by PendingIntent as binder\n"); |
| 355 | } |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 356 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
yro | 4490765 | 2018-03-12 20:44:05 -0700 | [diff] [blame] | 360 | void ConfigManager::update_saved_configs_locked(const ConfigKey& key, |
| 361 | const vector<uint8_t>& buffer, |
| 362 | const int numBytes) { |
yro | 9c98c05 | 2017-11-19 14:33:56 -0800 | [diff] [blame] | 363 | // If there is a pre-existing config with same key we should first delete it. |
| 364 | remove_saved_configs(key); |
| 365 | |
| 366 | // Then we save the latest config. |
yro | 4490765 | 2018-03-12 20:44:05 -0700 | [diff] [blame] | 367 | string file_name = |
| 368 | StringPrintf("%s/%ld_%d_%lld", STATS_SERVICE_DIR, time(nullptr), |
| 369 | key.GetUid(), (long long)key.GetId()); |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 370 | StorageManager::writeFile(file_name.c_str(), &buffer[0], numBytes); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 373 | } // namespace statsd |
| 374 | } // namespace os |
| 375 | } // namespace android |