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 | |
| 17 | #pragma once |
| 18 | |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 19 | #include "binder/IBinder.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 20 | #include "config/ConfigKey.h" |
| 21 | #include "config/ConfigListener.h" |
| 22 | |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 23 | #include <map> |
David Chen | fdc123b | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 24 | #include <mutex> |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 25 | #include <string> |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 26 | |
| 27 | #include <stdio.h> |
| 28 | |
| 29 | namespace android { |
| 30 | namespace os { |
| 31 | namespace statsd { |
| 32 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 33 | /** |
| 34 | * Keeps track of which configurations have been set from various sources. |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 35 | */ |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 36 | class ConfigManager : public virtual android::RefBase { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 37 | public: |
| 38 | ConfigManager(); |
| 39 | virtual ~ConfigManager(); |
| 40 | |
| 41 | /** |
yro | 87d983c | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 42 | * Initialize ConfigListener by reading from disk and get updates. |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 43 | */ |
| 44 | void Startup(); |
| 45 | |
yro | 469cd80 | 2018-01-04 14:57:45 -0800 | [diff] [blame] | 46 | /* |
Kelly Rossmoyer | 6f37c91 | 2020-07-29 21:22:15 +0000 | [diff] [blame^] | 47 | * No-op initializer for tests. |
yro | 469cd80 | 2018-01-04 14:57:45 -0800 | [diff] [blame] | 48 | */ |
| 49 | void StartupForTest(); |
| 50 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 51 | /** |
| 52 | * Someone else wants to know about the configs. |
| 53 | */ |
| 54 | void AddListener(const sp<ConfigListener>& listener); |
| 55 | |
| 56 | /** |
| 57 | * A configuration was added or updated. |
| 58 | * |
| 59 | * Reports this to listeners. |
| 60 | */ |
| 61 | void UpdateConfig(const ConfigKey& key, const StatsdConfig& data); |
| 62 | |
| 63 | /** |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 64 | * Sets the broadcast receiver for a configuration key. |
| 65 | */ |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 66 | void SetConfigReceiver(const ConfigKey& key, const sp<IBinder>& intentSender); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 67 | |
| 68 | /** |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 69 | * Returns the package name and class name representing the broadcast receiver for this config. |
| 70 | */ |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 71 | const sp<android::IBinder> GetConfigReceiver(const ConfigKey& key) const; |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 72 | |
| 73 | /** |
| 74 | * Returns all config keys registered. |
| 75 | */ |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 76 | std::vector<ConfigKey> GetAllConfigKeys() const; |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 77 | |
| 78 | /** |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 79 | * Erase any broadcast receiver associated with this config key. |
| 80 | */ |
| 81 | void RemoveConfigReceiver(const ConfigKey& key); |
| 82 | |
| 83 | /** |
Tej Singh | 2c9ef2a | 2019-01-22 11:33:51 -0800 | [diff] [blame] | 84 | * Sets the broadcast receiver that is notified whenever the list of active configs |
| 85 | * changes for this uid. |
| 86 | */ |
| 87 | void SetActiveConfigsChangedReceiver(const int uid, const sp<IBinder>& intentSender); |
| 88 | |
| 89 | /** |
| 90 | * Returns the broadcast receiver for active configs changed for this uid. |
| 91 | */ |
| 92 | |
| 93 | const sp<IBinder> GetActiveConfigsChangedReceiver(const int uid) const; |
| 94 | |
| 95 | /** |
| 96 | * Erase any active configs changed broadcast receiver associated with this uid. |
| 97 | */ |
| 98 | void RemoveActiveConfigsChangedReceiver(const int uid); |
| 99 | |
| 100 | /** |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 101 | * A configuration was removed. |
| 102 | * |
| 103 | * Reports this to listeners. |
| 104 | */ |
| 105 | void RemoveConfig(const ConfigKey& key); |
| 106 | |
| 107 | /** |
| 108 | * Remove all of the configs for the given uid. |
| 109 | */ |
| 110 | void RemoveConfigs(int uid); |
| 111 | |
| 112 | /** |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 113 | * Remove all of the configs from memory. |
| 114 | */ |
| 115 | void RemoveAllConfigs(); |
| 116 | |
| 117 | /** |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 118 | * Text dump of our state for debugging. |
| 119 | */ |
| 120 | void Dump(FILE* out); |
| 121 | |
| 122 | private: |
David Chen | fdc123b | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 123 | mutable std::mutex mMutex; |
| 124 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 125 | /** |
| 126 | * Save the configs to disk. |
| 127 | */ |
yro | 4490765 | 2018-03-12 20:44:05 -0700 | [diff] [blame] | 128 | void update_saved_configs_locked(const ConfigKey& key, |
| 129 | const std::vector<uint8_t>& buffer, |
| 130 | const int numBytes); |
yro | 87d983c | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 131 | |
| 132 | /** |
| 133 | * Remove saved configs from disk. |
| 134 | */ |
| 135 | void remove_saved_configs(const ConfigKey& key); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 136 | |
| 137 | /** |
Yao Chen | 52b478b | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 138 | * Maps from uid to the config keys that have been set. |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 139 | */ |
Yao Chen | 52b478b | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 140 | std::map<int, std::set<ConfigKey>> mConfigs; |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 141 | |
| 142 | /** |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 143 | * Each config key can be subscribed by up to one receiver, specified as IBinder from |
| 144 | * PendingIntent. |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 145 | */ |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 146 | std::map<ConfigKey, sp<android::IBinder>> mConfigReceivers; |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 147 | |
| 148 | /** |
Tej Singh | 2c9ef2a | 2019-01-22 11:33:51 -0800 | [diff] [blame] | 149 | * Each uid can be subscribed by up to one receiver to notify that the list of active configs |
| 150 | * for this uid has changed. The receiver is specified as IBinder from PendingIntent. |
| 151 | */ |
| 152 | std::map<int, sp<android::IBinder>> mActiveConfigsChangedReceivers; |
| 153 | |
| 154 | /** |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 155 | * The ConfigListeners that will be told about changes. |
| 156 | */ |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 157 | std::vector<sp<ConfigListener>> mListeners; |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | } // namespace statsd |
| 161 | } // namespace os |
| 162 | } // namespace android |