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