blob: a0c1c1cb16f8c1e8da16793eff00c0c4f093c558 [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
Yao Chenf09569f2017-12-13 17:00:51 -080034// Util function to build a hard coded config with test metrics.
Yangster-mac756cd482017-11-21 21:58:44 -080035StatsdConfig build_fake_config();
36
Joe Onorato9fc9edf2017-10-15 20:08:52 -070037/**
38 * Keeps track of which configurations have been set from various sources.
39 *
40 * TODO: Store the configs persistently too.
41 * TODO: Dump method for debugging.
42 */
Yao Chenf09569f2017-12-13 17:00:51 -080043class ConfigManager : public virtual android::RefBase {
Joe Onorato9fc9edf2017-10-15 20:08:52 -070044public:
45 ConfigManager();
46 virtual ~ConfigManager();
47
48 /**
yro87d983c2017-11-14 21:31:43 -080049 * Initialize ConfigListener by reading from disk and get updates.
Joe Onorato9fc9edf2017-10-15 20:08:52 -070050 */
51 void Startup();
52
yro469cd802018-01-04 14:57:45 -080053 /*
54 * Dummy initializer for tests.
55 */
56 void StartupForTest();
57
Joe Onorato9fc9edf2017-10-15 20:08:52 -070058 /**
59 * Someone else wants to know about the configs.
60 */
61 void AddListener(const sp<ConfigListener>& listener);
62
63 /**
64 * A configuration was added or updated.
65 *
66 * Reports this to listeners.
67 */
68 void UpdateConfig(const ConfigKey& key, const StatsdConfig& data);
69
70 /**
David Chenadaf8b32017-11-03 15:42:08 -070071 * Sets the broadcast receiver for a configuration key.
72 */
David Chen661f7912018-01-22 17:46:24 -080073 void SetConfigReceiver(const ConfigKey& key, const sp<IBinder>& intentSender);
David Chenadaf8b32017-11-03 15:42:08 -070074
75 /**
David Chen1d7b0cd2017-11-15 14:20:04 -080076 * Returns the package name and class name representing the broadcast receiver for this config.
77 */
David Chen661f7912018-01-22 17:46:24 -080078 const sp<android::IBinder> GetConfigReceiver(const ConfigKey& key) const;
David Chen1d7b0cd2017-11-15 14:20:04 -080079
80 /**
81 * Returns all config keys registered.
82 */
Yao Chenf09569f2017-12-13 17:00:51 -080083 std::vector<ConfigKey> GetAllConfigKeys() const;
David Chen1d7b0cd2017-11-15 14:20:04 -080084
85 /**
David Chenadaf8b32017-11-03 15:42:08 -070086 * Erase any broadcast receiver associated with this config key.
87 */
88 void RemoveConfigReceiver(const ConfigKey& key);
89
90 /**
Joe Onorato9fc9edf2017-10-15 20:08:52 -070091 * A configuration was removed.
92 *
93 * Reports this to listeners.
94 */
95 void RemoveConfig(const ConfigKey& key);
96
97 /**
98 * Remove all of the configs for the given uid.
99 */
100 void RemoveConfigs(int uid);
101
102 /**
yro74fed972017-11-27 14:42:42 -0800103 * Remove all of the configs from memory.
104 */
105 void RemoveAllConfigs();
106
107 /**
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700108 * Text dump of our state for debugging.
109 */
110 void Dump(FILE* out);
111
112private:
David Chenfdc123b2018-02-09 17:21:48 -0800113 mutable std::mutex mMutex;
114
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700115 /**
116 * Save the configs to disk.
117 */
David Chenfdc123b2018-02-09 17:21:48 -0800118 void update_saved_configs_locked(const ConfigKey& key, const StatsdConfig& config);
yro87d983c2017-11-14 21:31:43 -0800119
120 /**
121 * Remove saved configs from disk.
122 */
123 void remove_saved_configs(const ConfigKey& key);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700124
125 /**
David Chenadaf8b32017-11-03 15:42:08 -0700126 * The Configs that have been set. Each config should
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700127 */
Yao Chenf09569f2017-12-13 17:00:51 -0800128 std::set<ConfigKey> mConfigs;
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700129
130 /**
David Chen661f7912018-01-22 17:46:24 -0800131 * Each config key can be subscribed by up to one receiver, specified as IBinder from
132 * PendingIntent.
David Chenadaf8b32017-11-03 15:42:08 -0700133 */
David Chen661f7912018-01-22 17:46:24 -0800134 std::map<ConfigKey, sp<android::IBinder>> mConfigReceivers;
David Chenadaf8b32017-11-03 15:42:08 -0700135
136 /**
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700137 * The ConfigListeners that will be told about changes.
138 */
Yao Chenf09569f2017-12-13 17:00:51 -0800139 std::vector<sp<ConfigListener>> mListeners;
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700140};
141
142} // namespace statsd
143} // namespace os
144} // namespace android