blob: a2b2a0ce43d5b86b91ec508862958efeb26cd862 [file] [log] [blame]
Joe Onorato9fc9edf2017-10-15 20:08:52 -07001/*
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 Chen661f7912018-01-22 17:46:24 -080019#include "binder/IBinder.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070020#include "config/ConfigKey.h"
21#include "config/ConfigListener.h"
22
Yao Chenf09569f2017-12-13 17:00:51 -080023#include <map>
24#include <set>
Joe Onorato9fc9edf2017-10-15 20:08:52 -070025#include <string>
Joe Onorato9fc9edf2017-10-15 20:08:52 -070026
27#include <stdio.h>
28
29namespace android {
30namespace os {
31namespace statsd {
32
Yao Chenf09569f2017-12-13 17:00:51 -080033// Util function to build a hard coded config with test metrics.
Yangster-mac756cd482017-11-21 21:58:44 -080034StatsdConfig build_fake_config();
35
Joe Onorato9fc9edf2017-10-15 20:08:52 -070036/**
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 Chenf09569f2017-12-13 17:00:51 -080042class ConfigManager : public virtual android::RefBase {
Joe Onorato9fc9edf2017-10-15 20:08:52 -070043public:
44 ConfigManager();
45 virtual ~ConfigManager();
46
47 /**
yro87d983c2017-11-14 21:31:43 -080048 * Initialize ConfigListener by reading from disk and get updates.
Joe Onorato9fc9edf2017-10-15 20:08:52 -070049 */
50 void Startup();
51
yro469cd802018-01-04 14:57:45 -080052 /*
53 * Dummy initializer for tests.
54 */
55 void StartupForTest();
56
Joe Onorato9fc9edf2017-10-15 20:08:52 -070057 /**
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 Chenadaf8b32017-11-03 15:42:08 -070070 * Sets the broadcast receiver for a configuration key.
71 */
David Chen661f7912018-01-22 17:46:24 -080072 void SetConfigReceiver(const ConfigKey& key, const sp<IBinder>& intentSender);
David Chenadaf8b32017-11-03 15:42:08 -070073
74 /**
David Chen1d7b0cd2017-11-15 14:20:04 -080075 * Returns the package name and class name representing the broadcast receiver for this config.
76 */
David Chen661f7912018-01-22 17:46:24 -080077 const sp<android::IBinder> GetConfigReceiver(const ConfigKey& key) const;
David Chen1d7b0cd2017-11-15 14:20:04 -080078
79 /**
80 * Returns all config keys registered.
81 */
Yao Chenf09569f2017-12-13 17:00:51 -080082 std::vector<ConfigKey> GetAllConfigKeys() const;
David Chen1d7b0cd2017-11-15 14:20:04 -080083
84 /**
David Chenadaf8b32017-11-03 15:42:08 -070085 * Erase any broadcast receiver associated with this config key.
86 */
87 void RemoveConfigReceiver(const ConfigKey& key);
88
89 /**
Joe Onorato9fc9edf2017-10-15 20:08:52 -070090 * 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 /**
yro74fed972017-11-27 14:42:42 -0800102 * Remove all of the configs from memory.
103 */
104 void RemoveAllConfigs();
105
106 /**
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700107 * Text dump of our state for debugging.
108 */
109 void Dump(FILE* out);
110
111private:
112 /**
113 * Save the configs to disk.
114 */
yro87d983c2017-11-14 21:31:43 -0800115 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 Onorato9fc9edf2017-10-15 20:08:52 -0700121
122 /**
David Chenadaf8b32017-11-03 15:42:08 -0700123 * The Configs that have been set. Each config should
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700124 */
Yao Chenf09569f2017-12-13 17:00:51 -0800125 std::set<ConfigKey> mConfigs;
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700126
127 /**
David Chen661f7912018-01-22 17:46:24 -0800128 * Each config key can be subscribed by up to one receiver, specified as IBinder from
129 * PendingIntent.
David Chenadaf8b32017-11-03 15:42:08 -0700130 */
David Chen661f7912018-01-22 17:46:24 -0800131 std::map<ConfigKey, sp<android::IBinder>> mConfigReceivers;
David Chenadaf8b32017-11-03 15:42:08 -0700132
133 /**
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700134 * The ConfigListeners that will be told about changes.
135 */
Yao Chenf09569f2017-12-13 17:00:51 -0800136 std::vector<sp<ConfigListener>> mListeners;
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700137};
138
139} // namespace statsd
140} // namespace os
141} // namespace android