blob: 8b3a4218238ccb0958b1800d1e61d0cefc2ceae4 [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
tsaichristined21aacf2019-10-07 14:47:38 -070021#include "HashableDimensionKey.h"
tsaichristine10978642019-09-10 14:12:49 -070022#include "state/StateListener.h"
23#include "state/StateTracker.h"
24
25namespace android {
26namespace os {
27namespace statsd {
28
tsaichristine7192e0a2019-12-06 02:20:00 -080029/**
30 * This class is NOT thread safe.
31 * It should only be used while StatsLogProcessor's lock is held.
32 */
tsaichristine10978642019-09-10 14:12:49 -070033class StateManager : public virtual RefBase {
34public:
35 StateManager(){};
36
37 ~StateManager(){};
38
39 // Returns a pointer to the single, shared StateManager object.
40 static StateManager& getInstance();
41
tsaichristinec876b492019-12-10 13:47:05 -080042 // Unregisters all listeners and removes all trackers from StateManager.
43 void clear();
44
tsaichristine10978642019-09-10 14:12:49 -070045 // Notifies the correct StateTracker of an event.
46 void onLogEvent(const LogEvent& event);
47
tsaichristined21aacf2019-10-07 14:47:38 -070048 // Returns true if atomId is being tracked and is associated with a state
49 // atom. StateManager notifies the correct StateTracker to register listener.
50 // If the correct StateTracker does not exist, a new StateTracker is created.
tsaichristinec876b492019-12-10 13:47:05 -080051 bool registerListener(const int32_t atomId, wp<StateListener> listener);
tsaichristine10978642019-09-10 14:12:49 -070052
53 // Notifies the correct StateTracker to unregister a listener
54 // and removes the tracker if it no longer has any listeners.
tsaichristinec876b492019-12-10 13:47:05 -080055 void unregisterListener(const int32_t atomId, wp<StateListener> listener);
tsaichristine10978642019-09-10 14:12:49 -070056
tsaichristine69000e62019-10-18 17:34:52 -070057 // Returns true if the StateTracker exists and queries for the
58 // original state value mapped to the given query key. The state value is
59 // stored and output in a FieldValue class.
60 // Returns false if the StateTracker doesn't exist.
tsaichristinec876b492019-12-10 13:47:05 -080061 bool getStateValue(const int32_t atomId, const HashableDimensionKey& queryKey,
tsaichristine69000e62019-10-18 17:34:52 -070062 FieldValue* output) const;
tsaichristine10978642019-09-10 14:12:49 -070063
tsaichristine69000e62019-10-18 17:34:52 -070064 inline int getStateTrackersCount() const {
tsaichristine10978642019-09-10 14:12:49 -070065 return mStateTrackers.size();
66 }
67
tsaichristinec876b492019-12-10 13:47:05 -080068 inline int getListenersCount(const int32_t atomId) const {
tsaichristine69000e62019-10-18 17:34:52 -070069 auto it = mStateTrackers.find(atomId);
70 if (it != mStateTrackers.end()) {
71 return it->second->getListenersCount();
tsaichristine10978642019-09-10 14:12:49 -070072 }
73 return -1;
74 }
75
76private:
tsaichristine7192e0a2019-12-06 02:20:00 -080077 mutable std::mutex mMutex;
tsaichristine10978642019-09-10 14:12:49 -070078
tsaichristine7192e0a2019-12-06 02:20:00 -080079 // Maps state atom ids to StateTrackers
80 std::unordered_map<int32_t, sp<StateTracker>> mStateTrackers;
tsaichristine10978642019-09-10 14:12:49 -070081};
82
83} // namespace statsd
84} // namespace os
85} // namespace android