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