1 /*
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 <aidl/android/os/IPendingIntentRef.h>
20 #include <stdio.h>
21 
22 #include <map>
23 #include <mutex>
24 #include <set>
25 #include <string>
26 
27 #include "config/ConfigKey.h"
28 #include "config/ConfigKeyWithPackage.h"
29 #include "config/ConfigListener.h"
30 
31 using aidl::android::os::IPendingIntentRef;
32 using std::shared_ptr;
33 
34 namespace android {
35 namespace os {
36 namespace statsd {
37 
38 /**
39  * Keeps track of which configurations have been set from various sources.
40  */
41 class ConfigManager : public virtual RefBase {
42 public:
43     ConfigManager();
44     virtual ~ConfigManager();
45 
46     /**
47      * Initialize ConfigListener by reading from disk and get updates.
48      */
49     void Startup();
50 
51     /*
52      * No-op initializer for tests.
53      */
54     void StartupForTest();
55 
56     /**
57      * Someone else wants to know about the configs.
58      */
59     void AddListener(const sp<ConfigListener>& listener);
60 
61     /**
62      * A configuration was added or updated.
63      *
64      * Reports this to listeners.
65      */
66     void UpdateConfig(const ConfigKey& key, const StatsdConfig& data);
67 
68     /**
69      * Sets the broadcast receiver for a configuration key.
70      */
71     void SetConfigReceiver(const ConfigKey& key, const shared_ptr<IPendingIntentRef>& pir);
72 
73     /**
74      * Returns the package name and class name representing the broadcast receiver for this config.
75      */
76     const shared_ptr<IPendingIntentRef> GetConfigReceiver(const ConfigKey& key) const;
77 
78     /**
79      * Returns all config keys registered.
80      */
81     std::vector<ConfigKey> GetAllConfigKeys() const;
82 
83     /**
84      * Erase any broadcast receiver associated with this config key.
85      */
86     void RemoveConfigReceiver(const ConfigKey& key);
87 
88     /**
89      * Erase the broadcast receiver for this config key if it is equal to the provided broadcast
90      * receiver.
91      */
92     void RemoveConfigReceiver(const ConfigKey& key, const shared_ptr<IPendingIntentRef>& pir);
93 
94     /**
95      * Sets the broadcast receiver that is notified whenever the list of active configs
96      * changes for this uid.
97      */
98     void SetActiveConfigsChangedReceiver(const int uid, const shared_ptr<IPendingIntentRef>& pir);
99 
100     /**
101      * Returns the broadcast receiver for active configs changed for this uid.
102      */
103 
104     const shared_ptr<IPendingIntentRef> GetActiveConfigsChangedReceiver(const int uid) const;
105 
106     /**
107      * Erase any active configs changed broadcast receiver associated with this uid.
108      */
109     void RemoveActiveConfigsChangedReceiver(const int uid);
110 
111     /**
112      * Erase the active configs changed broadcast receiver associated with this uid if it is equal
113      * to the provided broadcast receiver.
114      */
115     void RemoveActiveConfigsChangedReceiver(const int uid,
116                                             const shared_ptr<IPendingIntentRef>& pir);
117 
118     /**
119      * Sets the pending intent that is notified whenever the list of restricted metrics changes
120      */
121     void SetRestrictedMetricsChangedReceiver(const string& configPackage, int64_t configId,
122                                              const int32_t callingUid,
123                                              const shared_ptr<IPendingIntentRef>& pir);
124 
125     /**
126      * Erase any restricted metrics changed pending intents associated with this config key & uid.
127      */
128     void RemoveRestrictedMetricsChangedReceiver(const string& configPackage, int64_t configId,
129                                                 const int32_t callingUid);
130 
131     /**
132      * Sends a restricted metrics broadcast for the valid config keys and delegate package
133      */
134     void SendRestrictedMetricsBroadcast(const std::set<string>& configPackages,
135                                         const int64_t configId,
136                                         const std::set<int32_t>& delegateUids,
137                                         const std::vector<int64_t>& metricIds);
138 
139     /**
140      * A configuration was removed.
141      *
142      * Reports this to listeners.
143      */
144     void RemoveConfig(const ConfigKey& key);
145 
146     /**
147      * Remove all of the configs for the given uid.
148      */
149     void RemoveConfigs(int uid);
150 
151     /**
152      * Remove all of the configs from memory.
153      */
154     void RemoveAllConfigs();
155 
156     /**
157      * Text dump of our state for debugging.
158      */
159     void Dump(FILE* out);
160 
161 private:
162     mutable std::mutex mMutex;
163 
164     /**
165      * Save the configs to disk.
166      */
167     void update_saved_configs_locked(const ConfigKey& key,
168                                      const std::vector<uint8_t>& buffer,
169                                      const int numBytes);
170 
171     /**
172      * Remove saved configs from disk.
173      */
174     void remove_saved_configs(const ConfigKey& key);
175 
176     /**
177      * Maps from uid to the config keys that have been set.
178      */
179     std::map<int, std::set<ConfigKey>> mConfigs;
180 
181     /**
182      * Each config key can be subscribed by up to one receiver, specified as IPendingIntentRef.
183      */
184     std::map<ConfigKey, shared_ptr<IPendingIntentRef>> mConfigReceivers;
185 
186     /**
187      * Each uid can be subscribed by up to one receiver to notify that the list of active configs
188      * for this uid has changed. The receiver is specified as IPendingIntentRef.
189      */
190     std::map<int, shared_ptr<IPendingIntentRef>> mActiveConfigsChangedReceivers;
191 
192     /**
193      * Each uid can subscribe up to one receiver for a particular config to receive the restricted
194      * metrics for that config. The receiver is specified as IPendingIntentRef.
195      */
196     std::map<ConfigKeyWithPackage, std::map<int32_t, shared_ptr<IPendingIntentRef>>>
197             mRestrictedMetricsChangedReceivers;
198 
199     /**
200      * The ConfigListeners that will be told about changes.
201      */
202     std::vector<sp<ConfigListener>> mListeners;
203 
204     /**
205      * Erase the restricted metrics changed pending intents associated with this config key & uid if
206      * it is equal to the provided pending intent.
207      */
208     void RemoveRestrictedMetricsChangedReceiver(const ConfigKeyWithPackage& key,
209                                                 const int32_t delegateUid,
210                                                 const shared_ptr<IPendingIntentRef>& pir);
211 };
212 
213 }  // namespace statsd
214 }  // namespace os
215 }  // namespace android
216