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 | |
| 22 | namespace android { |
| 23 | namespace os { |
| 24 | namespace statsd { |
| 25 | |
| 26 | StateManager& StateManager::getInstance() { |
| 27 | static StateManager sStateManager; |
| 28 | return sStateManager; |
| 29 | } |
| 30 | |
| 31 | void StateManager::onLogEvent(const LogEvent& event) { |
| 32 | std::lock_guard<std::mutex> lock(mMutex); |
| 33 | if (mStateTrackers.find(event.GetTagId()) != mStateTrackers.end()) { |
| 34 | mStateTrackers[event.GetTagId()]->onLogEvent(event); |
| 35 | } |
| 36 | } |
| 37 | |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 38 | bool StateManager::registerListener(int32_t atomId, wp<StateListener> listener) { |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 39 | std::lock_guard<std::mutex> lock(mMutex); |
| 40 | |
| 41 | // Check if state tracker already exists |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 42 | if (mStateTrackers.find(atomId) == mStateTrackers.end()) { |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 43 | // Create a new state tracker iff atom is a state atom |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 44 | auto it = android::util::AtomsInfo::kStateAtomsFieldOptions.find(atomId); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 45 | if (it != android::util::AtomsInfo::kStateAtomsFieldOptions.end()) { |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 46 | mStateTrackers[atomId] = new StateTracker(atomId, it->second); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 47 | } else { |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 48 | ALOGE("StateManager cannot register listener, Atom %d is not a state atom", atomId); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 49 | return false; |
| 50 | } |
| 51 | } |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 52 | mStateTrackers[atomId]->registerListener(listener); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 53 | return true; |
| 54 | } |
| 55 | |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 56 | void StateManager::unregisterListener(int32_t atomId, wp<StateListener> listener) { |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 57 | std::unique_lock<std::mutex> lock(mMutex); |
| 58 | |
| 59 | // Hold the sp<> until the lock is released so that ~StateTracker() is |
| 60 | // not called while the lock is held. |
| 61 | sp<StateTracker> toRemove; |
| 62 | |
| 63 | // Unregister listener from correct StateTracker |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 64 | auto it = mStateTrackers.find(atomId); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 65 | if (it != mStateTrackers.end()) { |
| 66 | it->second->unregisterListener(listener); |
| 67 | |
| 68 | // Remove the StateTracker if it has no listeners |
| 69 | if (it->second->getListenersCount() == 0) { |
| 70 | toRemove = it->second; |
| 71 | mStateTrackers.erase(it); |
| 72 | } |
| 73 | } else { |
| 74 | ALOGE("StateManager cannot unregister listener, StateTracker for atom %d does not exist", |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 75 | atomId); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 76 | } |
| 77 | lock.unlock(); |
| 78 | } |
| 79 | |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 80 | bool StateManager::getStateValue(int32_t atomId, const HashableDimensionKey& key, |
| 81 | FieldValue* output) const { |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 82 | std::lock_guard<std::mutex> lock(mMutex); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 83 | |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 84 | auto it = mStateTrackers.find(atomId); |
| 85 | if (it != mStateTrackers.end()) { |
| 86 | return it->second->getStateValue(key, output); |
| 87 | } |
| 88 | return false; |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | } // namespace statsd |
| 92 | } // namespace os |
| 93 | } // namespace android |