tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 18 | #include <inttypes.h> |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 19 | #include <utils/RefBase.h> |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 20 | |
Muhammad Qureshi | 7ce2256 | 2020-05-12 01:08:00 -0700 | [diff] [blame^] | 21 | #include <set> |
| 22 | #include <string> |
| 23 | #include <unordered_map> |
| 24 | |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 25 | #include "HashableDimensionKey.h" |
Muhammad Qureshi | 7ce2256 | 2020-05-12 01:08:00 -0700 | [diff] [blame^] | 26 | #include "packages/UidMap.h" |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 27 | #include "state/StateListener.h" |
| 28 | #include "state/StateTracker.h" |
| 29 | |
| 30 | namespace android { |
| 31 | namespace os { |
| 32 | namespace statsd { |
| 33 | |
tsaichristine | 7192e0a | 2019-12-06 02:20:00 -0800 | [diff] [blame] | 34 | /** |
| 35 | * This class is NOT thread safe. |
| 36 | * It should only be used while StatsLogProcessor's lock is held. |
| 37 | */ |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 38 | class StateManager : public virtual RefBase { |
| 39 | public: |
Muhammad Qureshi | 7ce2256 | 2020-05-12 01:08:00 -0700 | [diff] [blame^] | 40 | StateManager(); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 41 | |
| 42 | ~StateManager(){}; |
| 43 | |
| 44 | // Returns a pointer to the single, shared StateManager object. |
| 45 | static StateManager& getInstance(); |
| 46 | |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 47 | // Unregisters all listeners and removes all trackers from StateManager. |
| 48 | void clear(); |
| 49 | |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 50 | // Notifies the correct StateTracker of an event. |
| 51 | void onLogEvent(const LogEvent& event); |
| 52 | |
Muhammad Qureshi | bfc4bdb | 2020-04-08 06:26:49 -0700 | [diff] [blame] | 53 | // Notifies the StateTracker for the given atomId to register listener. |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 54 | // If the correct StateTracker does not exist, a new StateTracker is created. |
Muhammad Qureshi | bfc4bdb | 2020-04-08 06:26:49 -0700 | [diff] [blame] | 55 | // 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); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 58 | |
| 59 | // Notifies the correct StateTracker to unregister a listener |
| 60 | // and removes the tracker if it no longer has any listeners. |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 61 | void unregisterListener(const int32_t atomId, wp<StateListener> listener); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 62 | |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 63 | // 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. |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 67 | bool getStateValue(const int32_t atomId, const HashableDimensionKey& queryKey, |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 68 | FieldValue* output) const; |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 69 | |
Muhammad Qureshi | 7ce2256 | 2020-05-12 01:08:00 -0700 | [diff] [blame^] | 70 | // 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 | |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 75 | inline int getStateTrackersCount() const { |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 76 | return mStateTrackers.size(); |
| 77 | } |
| 78 | |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 79 | inline int getListenersCount(const int32_t atomId) const { |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 80 | auto it = mStateTrackers.find(atomId); |
| 81 | if (it != mStateTrackers.end()) { |
| 82 | return it->second->getListenersCount(); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 83 | } |
| 84 | return -1; |
| 85 | } |
| 86 | |
| 87 | private: |
tsaichristine | 7192e0a | 2019-12-06 02:20:00 -0800 | [diff] [blame] | 88 | mutable std::mutex mMutex; |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 89 | |
tsaichristine | 7192e0a | 2019-12-06 02:20:00 -0800 | [diff] [blame] | 90 | // Maps state atom ids to StateTrackers |
| 91 | std::unordered_map<int32_t, sp<StateTracker>> mStateTrackers; |
Muhammad Qureshi | 7ce2256 | 2020-05-12 01:08:00 -0700 | [diff] [blame^] | 92 | |
| 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; |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | } // namespace statsd |
| 102 | } // namespace os |
| 103 | } // namespace android |