blob: 18c404c29c4e2055ccc983cbd5bc63f1c01fb791 [file] [log] [blame]
tsaichristine10978642019-09-10 14:12:49 -07001/*
2 * Copyright (C) 2019, 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#pragma once
17
tsaichristined21aacf2019-10-07 14:47:38 -070018#include <inttypes.h>
tsaichristine10978642019-09-10 14:12:49 -070019#include <utils/RefBase.h>
tsaichristine10978642019-09-10 14:12:49 -070020
Muhammad Qureshi7ce22562020-05-12 01:08:00 -070021#include <set>
22#include <string>
23#include <unordered_map>
24
tsaichristined21aacf2019-10-07 14:47:38 -070025#include "HashableDimensionKey.h"
Muhammad Qureshi7ce22562020-05-12 01:08:00 -070026#include "packages/UidMap.h"
tsaichristine10978642019-09-10 14:12:49 -070027#include "state/StateListener.h"
28#include "state/StateTracker.h"
29
30namespace android {
31namespace os {
32namespace statsd {
33
tsaichristine7192e0a2019-12-06 02:20:00 -080034/**
35 * This class is NOT thread safe.
36 * It should only be used while StatsLogProcessor's lock is held.
37 */
tsaichristine10978642019-09-10 14:12:49 -070038class StateManager : public virtual RefBase {
39public:
Muhammad Qureshi7ce22562020-05-12 01:08:00 -070040 StateManager();
tsaichristine10978642019-09-10 14:12:49 -070041
42 ~StateManager(){};
43
44 // Returns a pointer to the single, shared StateManager object.
45 static StateManager& getInstance();
46
tsaichristinec876b492019-12-10 13:47:05 -080047 // Unregisters all listeners and removes all trackers from StateManager.
48 void clear();
49
tsaichristine10978642019-09-10 14:12:49 -070050 // Notifies the correct StateTracker of an event.
51 void onLogEvent(const LogEvent& event);
52
Muhammad Qureshibfc4bdb2020-04-08 06:26:49 -070053 // Notifies the StateTracker for the given atomId to register listener.
tsaichristined21aacf2019-10-07 14:47:38 -070054 // If the correct StateTracker does not exist, a new StateTracker is created.
Muhammad Qureshibfc4bdb2020-04-08 06:26:49 -070055 // Note: StateTrackers can be created for non-state atoms. They are essentially empty and
56 // do not perform any actions.
57 void registerListener(const int32_t atomId, wp<StateListener> listener);
tsaichristine10978642019-09-10 14:12:49 -070058
59 // Notifies the correct StateTracker to unregister a listener
60 // and removes the tracker if it no longer has any listeners.
tsaichristinec876b492019-12-10 13:47:05 -080061 void unregisterListener(const int32_t atomId, wp<StateListener> listener);
tsaichristine10978642019-09-10 14:12:49 -070062
tsaichristine69000e62019-10-18 17:34:52 -070063 // Returns true if the StateTracker exists and queries for the
64 // original state value mapped to the given query key. The state value is
65 // stored and output in a FieldValue class.
66 // Returns false if the StateTracker doesn't exist.
tsaichristinec876b492019-12-10 13:47:05 -080067 bool getStateValue(const int32_t atomId, const HashableDimensionKey& queryKey,
tsaichristine69000e62019-10-18 17:34:52 -070068 FieldValue* output) const;
tsaichristine10978642019-09-10 14:12:49 -070069
Muhammad Qureshi7ce22562020-05-12 01:08:00 -070070 // Updates mAllowedLogSources with the latest uids for the packages that are allowed to log.
71 void updateLogSources(const sp<UidMap>& uidMap);
72
73 void notifyAppChanged(const string& apk, const sp<UidMap>& uidMap);
74
tsaichristine69000e62019-10-18 17:34:52 -070075 inline int getStateTrackersCount() const {
tsaichristine10978642019-09-10 14:12:49 -070076 return mStateTrackers.size();
77 }
78
tsaichristinec876b492019-12-10 13:47:05 -080079 inline int getListenersCount(const int32_t atomId) const {
tsaichristine69000e62019-10-18 17:34:52 -070080 auto it = mStateTrackers.find(atomId);
81 if (it != mStateTrackers.end()) {
82 return it->second->getListenersCount();
tsaichristine10978642019-09-10 14:12:49 -070083 }
84 return -1;
85 }
86
87private:
tsaichristine7192e0a2019-12-06 02:20:00 -080088 mutable std::mutex mMutex;
tsaichristine10978642019-09-10 14:12:49 -070089
tsaichristine7192e0a2019-12-06 02:20:00 -080090 // Maps state atom ids to StateTrackers
91 std::unordered_map<int32_t, sp<StateTracker>> mStateTrackers;
Muhammad Qureshi7ce22562020-05-12 01:08:00 -070092
93 // The package names that can log state events.
94 const std::set<std::string> mAllowedPkg;
95
96 // The combined uid sources (after translating pkg name to uid).
97 // State events from uids that are not in the list will be ignored to avoid state pollution.
98 std::set<int32_t> mAllowedLogSources;
tsaichristine10978642019-09-10 14:12:49 -070099};
100
101} // namespace statsd
102} // namespace os
103} // namespace android