blob: c064a519f597b0eb5f4407d33209338deefe0c81 [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>
Yao Chenf09569f2017-12-13 17:00:51 -080025#include <set>
Joe Onorato9fc9edf2017-10-15 20:08:52 -070026#include <string>
Joe Onorato9fc9edf2017-10-15 20:08:52 -070027
28#include <stdio.h>
29
30namespace android {
31namespace os {
32namespace statsd {
33
Joe Onorato9fc9edf2017-10-15 20:08:52 -070034/**
35 * Keeps track of which configurations have been set from various sources.
Joe Onorato9fc9edf2017-10-15 20:08:52 -070036 */
Yao Chenf09569f2017-12-13 17:00:51 -080037class ConfigManager : public virtual android::RefBase {
Joe Onorato9fc9edf2017-10-15 20:08:52 -070038public:
39 ConfigManager();
40 virtual ~ConfigManager();
41
42 /**
yro87d983c2017-11-14 21:31:43 -080043 * Initialize ConfigListener by reading from disk and get updates.
Joe Onorato9fc9edf2017-10-15 20:08:52 -070044 */
45 void Startup();
46
yro469cd802018-01-04 14:57:45 -080047 /*
48 * Dummy initializer for tests.
49 */
50 void StartupForTest();
51
Joe Onorato9fc9edf2017-10-15 20:08:52 -070052 /**
53 * Someone else wants to know about the configs.
54 */
55 void AddListener(const sp<ConfigListener>& listener);
56
57 /**
58 * A configuration was added or updated.
59 *
60 * Reports this to listeners.
61 */
62 void UpdateConfig(const ConfigKey& key, const StatsdConfig& data);
63
64 /**
David Chenadaf8b32017-11-03 15:42:08 -070065 * Sets the broadcast receiver for a configuration key.
66 */
David Chen661f7912018-01-22 17:46:24 -080067 void SetConfigReceiver(const ConfigKey& key, const sp<IBinder>& intentSender);
David Chenadaf8b32017-11-03 15:42:08 -070068
69 /**
David Chen1d7b0cd2017-11-15 14:20:04 -080070 * Returns the package name and class name representing the broadcast receiver for this config.
71 */
David Chen661f7912018-01-22 17:46:24 -080072 const sp<android::IBinder> GetConfigReceiver(const ConfigKey& key) const;
David Chen1d7b0cd2017-11-15 14:20:04 -080073
74 /**
75 * Returns all config keys registered.
76 */
Yao Chenf09569f2017-12-13 17:00:51 -080077 std::vector<ConfigKey> GetAllConfigKeys() const;
David Chen1d7b0cd2017-11-15 14:20:04 -080078
79 /**
David Chenadaf8b32017-11-03 15:42:08 -070080 * Erase any broadcast receiver associated with this config key.
81 */
82 void RemoveConfigReceiver(const ConfigKey& key);
83
84 /**
Tej Singh2c9ef2a2019-01-22 11:33:51 -080085 * Sets the broadcast receiver that is notified whenever the list of active configs
86 * changes for this uid.
87 */
88 void SetActiveConfigsChangedReceiver(const int uid, const sp<IBinder>& intentSender);
89
90 /**
91 * Returns the broadcast receiver for active configs changed for this uid.
92 */
93
94 const sp<IBinder> GetActiveConfigsChangedReceiver(const int uid) const;
95
96 /**
97 * Erase any active configs changed broadcast receiver associated with this uid.
98 */
99 void RemoveActiveConfigsChangedReceiver(const int uid);
100
101 /**
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700102 * A configuration was removed.
103 *
104 * Reports this to listeners.
105 */
106 void RemoveConfig(const ConfigKey& key);
107
108 /**
109 * Remove all of the configs for the given uid.
110 */
111 void RemoveConfigs(int uid);
112
113 /**
yro74fed972017-11-27 14:42:42 -0800114 * Remove all of the configs from memory.
115 */
116 void RemoveAllConfigs();
117
118 /**
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700119 * Text dump of our state for debugging.
120 */
121 void Dump(FILE* out);
122
123private:
David Chenfdc123b2018-02-09 17:21:48 -0800124 mutable std::mutex mMutex;
125
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700126 /**
127 * Save the configs to disk.
128 */
yro44907652018-03-12 20:44:05 -0700129 void update_saved_configs_locked(const ConfigKey& key,
130 const std::vector<uint8_t>& buffer,
131 const int numBytes);
yro87d983c2017-11-14 21:31:43 -0800132
133 /**
134 * Remove saved configs from disk.
135 */
136 void remove_saved_configs(const ConfigKey& key);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700137
138 /**
Yao Chen52b478b2018-03-27 10:59:45 -0700139 * Maps from uid to the config keys that have been set.
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700140 */
Yao Chen52b478b2018-03-27 10:59:45 -0700141 std::map<int, std::set<ConfigKey>> mConfigs;
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700142
143 /**
David Chen661f7912018-01-22 17:46:24 -0800144 * Each config key can be subscribed by up to one receiver, specified as IBinder from
145 * PendingIntent.
David Chenadaf8b32017-11-03 15:42:08 -0700146 */
David Chen661f7912018-01-22 17:46:24 -0800147 std::map<ConfigKey, sp<android::IBinder>> mConfigReceivers;
David Chenadaf8b32017-11-03 15:42:08 -0700148
149 /**
Tej Singh2c9ef2a2019-01-22 11:33:51 -0800150 * Each uid can be subscribed by up to one receiver to notify that the list of active configs
151 * for this uid has changed. The receiver is specified as IBinder from PendingIntent.
152 */
153 std::map<int, sp<android::IBinder>> mActiveConfigsChangedReceivers;
154
155 /**
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700156 * The ConfigListeners that will be told about changes.
157 */
Yao Chenf09569f2017-12-13 17:00:51 -0800158 std::vector<sp<ConfigListener>> mListeners;
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700159};
160
161} // namespace statsd
162} // namespace os
163} // namespace android