blob: bef057f964096be5e7c92b9eba7c51546b01cadb [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
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080022#include <aidl/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
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080028using aidl::android::os::IPendingIntentRef;
29using std::shared_ptr;
30
Joe Onorato9fc9edf2017-10-15 20:08:52 -070031namespace android {
32namespace os {
33namespace statsd {
34
Joe Onorato9fc9edf2017-10-15 20:08:52 -070035/**
36 * Keeps track of which configurations have been set from various sources.
Joe Onorato9fc9edf2017-10-15 20:08:52 -070037 */
Yao Chenf09569f2017-12-13 17:00:51 -080038class ConfigManager : public virtual android::RefBase {
Joe Onorato9fc9edf2017-10-15 20:08:52 -070039public:
40 ConfigManager();
41 virtual ~ConfigManager();
42
43 /**
yro87d983c2017-11-14 21:31:43 -080044 * Initialize ConfigListener by reading from disk and get updates.
Joe Onorato9fc9edf2017-10-15 20:08:52 -070045 */
46 void Startup();
47
yro469cd802018-01-04 14:57:45 -080048 /*
Kelly Rossmoyer6f37c912020-07-29 21:22:15 +000049 * No-op initializer for tests.
yro469cd802018-01-04 14:57:45 -080050 */
51 void StartupForTest();
52
Joe Onorato9fc9edf2017-10-15 20:08:52 -070053 /**
54 * Someone else wants to know about the configs.
55 */
56 void AddListener(const sp<ConfigListener>& listener);
57
58 /**
59 * A configuration was added or updated.
60 *
61 * Reports this to listeners.
62 */
63 void UpdateConfig(const ConfigKey& key, const StatsdConfig& data);
64
65 /**
David Chenadaf8b32017-11-03 15:42:08 -070066 * Sets the broadcast receiver for a configuration key.
67 */
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080068 void SetConfigReceiver(const ConfigKey& key, const shared_ptr<IPendingIntentRef>& pir);
David Chenadaf8b32017-11-03 15:42:08 -070069
70 /**
David Chen1d7b0cd2017-11-15 14:20:04 -080071 * Returns the package name and class name representing the broadcast receiver for this config.
72 */
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080073 const shared_ptr<IPendingIntentRef> GetConfigReceiver(const ConfigKey& key) const;
David Chen1d7b0cd2017-11-15 14:20:04 -080074
75 /**
76 * Returns all config keys registered.
77 */
Yao Chenf09569f2017-12-13 17:00:51 -080078 std::vector<ConfigKey> GetAllConfigKeys() const;
David Chen1d7b0cd2017-11-15 14:20:04 -080079
80 /**
David Chenadaf8b32017-11-03 15:42:08 -070081 * Erase any broadcast receiver associated with this config key.
82 */
83 void RemoveConfigReceiver(const ConfigKey& key);
84
85 /**
Tej Singh2c9ef2a2019-01-22 11:33:51 -080086 * Sets the broadcast receiver that is notified whenever the list of active configs
87 * changes for this uid.
88 */
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080089 void SetActiveConfigsChangedReceiver(const int uid, const shared_ptr<IPendingIntentRef>& pir);
Tej Singh2c9ef2a2019-01-22 11:33:51 -080090
91 /**
92 * Returns the broadcast receiver for active configs changed for this uid.
93 */
94
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080095 const shared_ptr<IPendingIntentRef> GetActiveConfigsChangedReceiver(const int uid) const;
Tej Singh2c9ef2a2019-01-22 11:33:51 -080096
97 /**
98 * Erase any active configs changed broadcast receiver associated with this uid.
99 */
100 void RemoveActiveConfigsChangedReceiver(const int uid);
101
102 /**
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700103 * A configuration was removed.
104 *
105 * Reports this to listeners.
106 */
107 void RemoveConfig(const ConfigKey& key);
108
109 /**
110 * Remove all of the configs for the given uid.
111 */
112 void RemoveConfigs(int uid);
113
114 /**
yro74fed972017-11-27 14:42:42 -0800115 * Remove all of the configs from memory.
116 */
117 void RemoveAllConfigs();
118
119 /**
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700120 * Text dump of our state for debugging.
121 */
122 void Dump(FILE* out);
123
124private:
David Chenfdc123b2018-02-09 17:21:48 -0800125 mutable std::mutex mMutex;
126
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700127 /**
128 * Save the configs to disk.
129 */
yro44907652018-03-12 20:44:05 -0700130 void update_saved_configs_locked(const ConfigKey& key,
131 const std::vector<uint8_t>& buffer,
132 const int numBytes);
yro87d983c2017-11-14 21:31:43 -0800133
134 /**
135 * Remove saved configs from disk.
136 */
137 void remove_saved_configs(const ConfigKey& key);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700138
139 /**
Yao Chen52b478b2018-03-27 10:59:45 -0700140 * Maps from uid to the config keys that have been set.
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700141 */
Yao Chen52b478b2018-03-27 10:59:45 -0700142 std::map<int, std::set<ConfigKey>> mConfigs;
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700143
144 /**
Jeffrey Huangad213742019-12-16 13:50:06 -0800145 * Each config key can be subscribed by up to one receiver, specified as IPendingIntentRef.
David Chenadaf8b32017-11-03 15:42:08 -0700146 */
Ruchir Rastogie449b0c2020-02-10 17:40:09 -0800147 std::map<ConfigKey, shared_ptr<IPendingIntentRef>> 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
Jeffrey Huang47537a12020-01-06 15:35:34 -0800151 * for this uid has changed. The receiver is specified as IPendingIntentRef.
Tej Singh2c9ef2a2019-01-22 11:33:51 -0800152 */
Ruchir Rastogie449b0c2020-02-10 17:40:09 -0800153 std::map<int, shared_ptr<IPendingIntentRef>> mActiveConfigsChangedReceivers;
Tej Singh2c9ef2a2019-01-22 11:33:51 -0800154
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;
Ruchir Rastogie449b0c2020-02-10 17:40:09 -0800159
160 // Death recipients that are triggered when the host process holding an
161 // IPendingIntentRef dies.
162 ::ndk::ScopedAIBinder_DeathRecipient mConfigReceiverDeathRecipient;
163 ::ndk::ScopedAIBinder_DeathRecipient mActiveConfigChangedReceiverDeathRecipient;
Ruchir Rastogi1497e8f2020-03-03 16:04:42 -0800164
165 /**
166 * Death recipient callback that is called when a config receiver dies.
167 * The cookie is a pointer to a ConfigReceiverDeathCookie.
168 */
169 static void configReceiverDied(void* cookie);
170
171 /**
172 * Death recipient callback that is called when an active config changed
173 * receiver dies. The cookie is a pointer to an
174 * ActiveConfigChangedReceiverDeathCookie.
175 */
176 static void activeConfigChangedReceiverDied(void* cookie);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700177};
178
179} // namespace statsd
180} // namespace os
181} // namespace android