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