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 | |
| 17 | #define DEBUG false // STOPSHIP if true |
| 18 | #include "Log.h" |
| 19 | |
| 20 | #include "StateManager.h" |
| 21 | |
Muhammad Qureshi | 7ce2256 | 2020-05-12 01:08:00 -0700 | [diff] [blame^] | 22 | #include <private/android_filesystem_config.h> |
| 23 | |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 24 | namespace android { |
| 25 | namespace os { |
| 26 | namespace statsd { |
| 27 | |
Muhammad Qureshi | 7ce2256 | 2020-05-12 01:08:00 -0700 | [diff] [blame^] | 28 | StateManager::StateManager() |
| 29 | : mAllowedPkg({ |
| 30 | "com.android.systemui", |
| 31 | }) { |
| 32 | } |
| 33 | |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 34 | StateManager& StateManager::getInstance() { |
| 35 | static StateManager sStateManager; |
| 36 | return sStateManager; |
| 37 | } |
| 38 | |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 39 | void StateManager::clear() { |
| 40 | mStateTrackers.clear(); |
| 41 | } |
| 42 | |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 43 | void StateManager::onLogEvent(const LogEvent& event) { |
Muhammad Qureshi | 7ce2256 | 2020-05-12 01:08:00 -0700 | [diff] [blame^] | 44 | // Only process state events from uids in AID_* and packages that are whitelisted in |
| 45 | // mAllowedPkg. |
| 46 | // Whitelisted AIDs are AID_ROOT and all AIDs in [1000, 2000) |
| 47 | if (event.GetUid() == AID_ROOT || (event.GetUid() >= 1000 && event.GetUid() < 2000) || |
| 48 | mAllowedLogSources.find(event.GetUid()) != mAllowedLogSources.end()) { |
| 49 | if (mStateTrackers.find(event.GetTagId()) != mStateTrackers.end()) { |
| 50 | mStateTrackers[event.GetTagId()]->onLogEvent(event); |
| 51 | } |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
Muhammad Qureshi | bfc4bdb | 2020-04-08 06:26:49 -0700 | [diff] [blame] | 55 | void StateManager::registerListener(const int32_t atomId, wp<StateListener> listener) { |
tsaichristine | 7192e0a | 2019-12-06 02:20:00 -0800 | [diff] [blame] | 56 | // Check if state tracker already exists. |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 57 | if (mStateTrackers.find(atomId) == mStateTrackers.end()) { |
Muhammad Qureshi | bfc4bdb | 2020-04-08 06:26:49 -0700 | [diff] [blame] | 58 | mStateTrackers[atomId] = new StateTracker(atomId); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 59 | } |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 60 | mStateTrackers[atomId]->registerListener(listener); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 61 | } |
| 62 | |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 63 | void StateManager::unregisterListener(const int32_t atomId, wp<StateListener> listener) { |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 64 | std::unique_lock<std::mutex> lock(mMutex); |
| 65 | |
| 66 | // Hold the sp<> until the lock is released so that ~StateTracker() is |
| 67 | // not called while the lock is held. |
| 68 | sp<StateTracker> toRemove; |
| 69 | |
| 70 | // Unregister listener from correct StateTracker |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 71 | auto it = mStateTrackers.find(atomId); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 72 | if (it != mStateTrackers.end()) { |
| 73 | it->second->unregisterListener(listener); |
| 74 | |
| 75 | // Remove the StateTracker if it has no listeners |
| 76 | if (it->second->getListenersCount() == 0) { |
| 77 | toRemove = it->second; |
| 78 | mStateTrackers.erase(it); |
| 79 | } |
| 80 | } else { |
| 81 | ALOGE("StateManager cannot unregister listener, StateTracker for atom %d does not exist", |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 82 | atomId); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 83 | } |
| 84 | lock.unlock(); |
| 85 | } |
| 86 | |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 87 | bool StateManager::getStateValue(const int32_t atomId, const HashableDimensionKey& key, |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 88 | FieldValue* output) const { |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 89 | auto it = mStateTrackers.find(atomId); |
| 90 | if (it != mStateTrackers.end()) { |
| 91 | return it->second->getStateValue(key, output); |
| 92 | } |
| 93 | return false; |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Muhammad Qureshi | 7ce2256 | 2020-05-12 01:08:00 -0700 | [diff] [blame^] | 96 | void StateManager::updateLogSources(const sp<UidMap>& uidMap) { |
| 97 | mAllowedLogSources.clear(); |
| 98 | for (const auto& pkg : mAllowedPkg) { |
| 99 | auto uids = uidMap->getAppUid(pkg); |
| 100 | mAllowedLogSources.insert(uids.begin(), uids.end()); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | void StateManager::notifyAppChanged(const string& apk, const sp<UidMap>& uidMap) { |
| 105 | if (mAllowedPkg.find(apk) != mAllowedPkg.end()) { |
| 106 | updateLogSources(uidMap); |
| 107 | } |
| 108 | } |
| 109 | |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 110 | } // namespace statsd |
| 111 | } // namespace os |
| 112 | } // namespace android |