blob: 2095173e8959fafef92eb06c664256b98990b11b [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
19#include "config/ConfigKey.h"
20#include "config/ConfigListener.h"
21
Jeffrey Huangad213742019-12-16 13:50:06 -080022#include <android/os/IPendingIntentRef.h>
David Chenfdc123b2018-02-09 17:21:48 -080023#include <mutex>
Joe Onorato9fc9edf2017-10-15 20:08:52 -070024#include <string>
Joe Onorato9fc9edf2017-10-15 20:08:52 -070025
26#include <stdio.h>
27
28namespace android {
29namespace os {
30namespace statsd {
31
Joe Onorato9fc9edf2017-10-15 20:08:52 -070032/**
33 * Keeps track of which configurations have been set from various sources.
Joe Onorato9fc9edf2017-10-15 20:08:52 -070034 */
Yao Chenf09569f2017-12-13 17:00:51 -080035class ConfigManager : public virtual android::RefBase {
Joe Onorato9fc9edf2017-10-15 20:08:52 -070036public:
37 ConfigManager();
38 virtual ~ConfigManager();
39
40 /**
yro87d983c2017-11-14 21:31:43 -080041 * Initialize ConfigListener by reading from disk and get updates.
Joe Onorato9fc9edf2017-10-15 20:08:52 -070042 */
43 void Startup();
44
yro469cd802018-01-04 14:57:45 -080045 /*
46 * Dummy initializer for tests.
47 */
48 void StartupForTest();
49
Joe Onorato9fc9edf2017-10-15 20:08:52 -070050 /**
51 * Someone else wants to know about the configs.
52 */
53 void AddListener(const sp<ConfigListener>& listener);
54
55 /**
56 * A configuration was added or updated.
57 *
58 * Reports this to listeners.
59 */
60 void UpdateConfig(const ConfigKey& key, const StatsdConfig& data);
61
62 /**
David Chenadaf8b32017-11-03 15:42:08 -070063 * Sets the broadcast receiver for a configuration key.
64 */
Jeffrey Huangad213742019-12-16 13:50:06 -080065 void SetConfigReceiver(const ConfigKey& key, const sp<IPendingIntentRef>& pir);
David Chenadaf8b32017-11-03 15:42:08 -070066
67 /**
David Chen1d7b0cd2017-11-15 14:20:04 -080068 * Returns the package name and class name representing the broadcast receiver for this config.
69 */
Jeffrey Huangad213742019-12-16 13:50:06 -080070 const sp<IPendingIntentRef> GetConfigReceiver(const ConfigKey& key) const;
David Chen1d7b0cd2017-11-15 14:20:04 -080071
72 /**
73 * Returns all config keys registered.
74 */
Yao Chenf09569f2017-12-13 17:00:51 -080075 std::vector<ConfigKey> GetAllConfigKeys() const;
David Chen1d7b0cd2017-11-15 14:20:04 -080076
77 /**
David Chenadaf8b32017-11-03 15:42:08 -070078 * Erase any broadcast receiver associated with this config key.
79 */
80 void RemoveConfigReceiver(const ConfigKey& key);
81
82 /**
Tej Singh2c9ef2a2019-01-22 11:33:51 -080083 * Sets the broadcast receiver that is notified whenever the list of active configs
84 * changes for this uid.
85 */
Jeffrey Huang47537a12020-01-06 15:35:34 -080086 void SetActiveConfigsChangedReceiver(const int uid, const sp<IPendingIntentRef>& pir);
Tej Singh2c9ef2a2019-01-22 11:33:51 -080087
88 /**
89 * Returns the broadcast receiver for active configs changed for this uid.
90 */
91
Jeffrey Huang47537a12020-01-06 15:35:34 -080092 const sp<IPendingIntentRef> GetActiveConfigsChangedReceiver(const int uid) const;
Tej Singh2c9ef2a2019-01-22 11:33:51 -080093
94 /**
95 * Erase any active configs changed broadcast receiver associated with this uid.
96 */
97 void RemoveActiveConfigsChangedReceiver(const int uid);
98
99 /**
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700100 * A configuration was removed.
101 *
102 * Reports this to listeners.
103 */
104 void RemoveConfig(const ConfigKey& key);
105
106 /**
107 * Remove all of the configs for the given uid.
108 */
109 void RemoveConfigs(int uid);
110
111 /**
yro74fed972017-11-27 14:42:42 -0800112 * Remove all of the configs from memory.
113 */
114 void RemoveAllConfigs();
115
116 /**
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700117 * Text dump of our state for debugging.
118 */
119 void Dump(FILE* out);
120
121private:
David Chenfdc123b2018-02-09 17:21:48 -0800122 mutable std::mutex mMutex;
123
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700124 /**
125 * Save the configs to disk.
126 */
yro44907652018-03-12 20:44:05 -0700127 void update_saved_configs_locked(const ConfigKey& key,
128 const std::vector<uint8_t>& buffer,
129 const int numBytes);
yro87d983c2017-11-14 21:31:43 -0800130
131 /**
132 * Remove saved configs from disk.
133 */
134 void remove_saved_configs(const ConfigKey& key);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700135
136 /**
Yao Chen52b478b2018-03-27 10:59:45 -0700137 * Maps from uid to the config keys that have been set.
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700138 */
Yao Chen52b478b2018-03-27 10:59:45 -0700139 std::map<int, std::set<ConfigKey>> mConfigs;
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700140
141 /**
Jeffrey Huangad213742019-12-16 13:50:06 -0800142 * Each config key can be subscribed by up to one receiver, specified as IPendingIntentRef.
David Chenadaf8b32017-11-03 15:42:08 -0700143 */
Jeffrey Huangad213742019-12-16 13:50:06 -0800144 std::map<ConfigKey, sp<IPendingIntentRef>> mConfigReceivers;
David Chenadaf8b32017-11-03 15:42:08 -0700145
146 /**
Tej Singh2c9ef2a2019-01-22 11:33:51 -0800147 * Each uid can be subscribed by up to one receiver to notify that the list of active configs
Jeffrey Huang47537a12020-01-06 15:35:34 -0800148 * for this uid has changed. The receiver is specified as IPendingIntentRef.
Tej Singh2c9ef2a2019-01-22 11:33:51 -0800149 */
Jeffrey Huang47537a12020-01-06 15:35:34 -0800150 std::map<int, sp<IPendingIntentRef>> mActiveConfigsChangedReceivers;
Tej Singh2c9ef2a2019-01-22 11:33:51 -0800151
152 /**
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700153 * The ConfigListeners that will be told about changes.
154 */
Yao Chenf09569f2017-12-13 17:00:51 -0800155 std::vector<sp<ConfigListener>> mListeners;
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700156};
157
158} // namespace statsd
159} // namespace os
160} // namespace android