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