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