blob: 1fdec316e4992dac402551fd3593d18e147868ed [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>
David Chenfdc123b2018-02-09 17:21:48 -080024#include <mutex>
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
Joe Onorato9fc9edf2017-10-15 20:08:52 -070033/**
34 * Keeps track of which configurations have been set from various sources.
Joe Onorato9fc9edf2017-10-15 20:08:52 -070035 */
Yao Chenf09569f2017-12-13 17:00:51 -080036class ConfigManager : public virtual android::RefBase {
Joe Onorato9fc9edf2017-10-15 20:08:52 -070037public:
38 ConfigManager();
39 virtual ~ConfigManager();
40
41 /**
yro87d983c2017-11-14 21:31:43 -080042 * Initialize ConfigListener by reading from disk and get updates.
Joe Onorato9fc9edf2017-10-15 20:08:52 -070043 */
44 void Startup();
45
yro469cd802018-01-04 14:57:45 -080046 /*
Kelly Rossmoyer6f37c912020-07-29 21:22:15 +000047 * No-op initializer for tests.
yro469cd802018-01-04 14:57:45 -080048 */
49 void StartupForTest();
50
Joe Onorato9fc9edf2017-10-15 20:08:52 -070051 /**
52 * Someone else wants to know about the configs.
53 */
54 void AddListener(const sp<ConfigListener>& listener);
55
56 /**
57 * A configuration was added or updated.
58 *
59 * Reports this to listeners.
60 */
61 void UpdateConfig(const ConfigKey& key, const StatsdConfig& data);
62
63 /**
David Chenadaf8b32017-11-03 15:42:08 -070064 * Sets the broadcast receiver for a configuration key.
65 */
David Chen661f7912018-01-22 17:46:24 -080066 void SetConfigReceiver(const ConfigKey& key, const sp<IBinder>& intentSender);
David Chenadaf8b32017-11-03 15:42:08 -070067
68 /**
David Chen1d7b0cd2017-11-15 14:20:04 -080069 * Returns the package name and class name representing the broadcast receiver for this config.
70 */
David Chen661f7912018-01-22 17:46:24 -080071 const sp<android::IBinder> GetConfigReceiver(const ConfigKey& key) const;
David Chen1d7b0cd2017-11-15 14:20:04 -080072
73 /**
74 * Returns all config keys registered.
75 */
Yao Chenf09569f2017-12-13 17:00:51 -080076 std::vector<ConfigKey> GetAllConfigKeys() const;
David Chen1d7b0cd2017-11-15 14:20:04 -080077
78 /**
David Chenadaf8b32017-11-03 15:42:08 -070079 * Erase any broadcast receiver associated with this config key.
80 */
81 void RemoveConfigReceiver(const ConfigKey& key);
82
83 /**
Tej Singh2c9ef2a2019-01-22 11:33:51 -080084 * Sets the broadcast receiver that is notified whenever the list of active configs
85 * changes for this uid.
86 */
87 void SetActiveConfigsChangedReceiver(const int uid, const sp<IBinder>& intentSender);
88
89 /**
90 * Returns the broadcast receiver for active configs changed for this uid.
91 */
92
93 const sp<IBinder> GetActiveConfigsChangedReceiver(const int uid) const;
94
95 /**
96 * Erase any active configs changed broadcast receiver associated with this uid.
97 */
98 void RemoveActiveConfigsChangedReceiver(const int uid);
99
100 /**
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700101 * A configuration was removed.
102 *
103 * Reports this to listeners.
104 */
105 void RemoveConfig(const ConfigKey& key);
106
107 /**
108 * Remove all of the configs for the given uid.
109 */
110 void RemoveConfigs(int uid);
111
112 /**
yro74fed972017-11-27 14:42:42 -0800113 * Remove all of the configs from memory.
114 */
115 void RemoveAllConfigs();
116
117 /**
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700118 * Text dump of our state for debugging.
119 */
120 void Dump(FILE* out);
121
122private:
David Chenfdc123b2018-02-09 17:21:48 -0800123 mutable std::mutex mMutex;
124
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700125 /**
126 * Save the configs to disk.
127 */
yro44907652018-03-12 20:44:05 -0700128 void update_saved_configs_locked(const ConfigKey& key,
129 const std::vector<uint8_t>& buffer,
130 const int numBytes);
yro87d983c2017-11-14 21:31:43 -0800131
132 /**
133 * Remove saved configs from disk.
134 */
135 void remove_saved_configs(const ConfigKey& key);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700136
137 /**
Yao Chen52b478b2018-03-27 10:59:45 -0700138 * Maps from uid to the config keys that have been set.
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700139 */
Yao Chen52b478b2018-03-27 10:59:45 -0700140 std::map<int, std::set<ConfigKey>> mConfigs;
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700141
142 /**
David Chen661f7912018-01-22 17:46:24 -0800143 * Each config key can be subscribed by up to one receiver, specified as IBinder from
144 * PendingIntent.
David Chenadaf8b32017-11-03 15:42:08 -0700145 */
David Chen661f7912018-01-22 17:46:24 -0800146 std::map<ConfigKey, sp<android::IBinder>> mConfigReceivers;
David Chenadaf8b32017-11-03 15:42:08 -0700147
148 /**
Tej Singh2c9ef2a2019-01-22 11:33:51 -0800149 * Each uid can be subscribed by up to one receiver to notify that the list of active configs
150 * for this uid has changed. The receiver is specified as IBinder from PendingIntent.
151 */
152 std::map<int, sp<android::IBinder>> mActiveConfigsChangedReceivers;
153
154 /**
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700155 * The ConfigListeners that will be told about changes.
156 */
Yao Chenf09569f2017-12-13 17:00:51 -0800157 std::vector<sp<ConfigListener>> mListeners;
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700158};
159
160} // namespace statsd
161} // namespace os
162} // namespace android