1 /* 2 * Copyright 2021 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 #pragma once 18 19 #include <android-base/thread_annotations.h> 20 #include <android/gui/ITunnelModeEnabledListener.h> 21 #include <binder/IBinder.h> 22 23 #include <unordered_map> 24 25 namespace android { 26 27 class Layer; 28 class SurfaceFlinger; 29 30 class TunnelModeEnabledReporter : public IBinder::DeathRecipient { 31 public: 32 TunnelModeEnabledReporter(); 33 34 // Checks if there is a tunnel mode enabled state change and if so, dispatches the updated 35 // tunnel mode enabled/disabled state to the registered listeners 36 // This method performs layer stack traversals, so mStateLock must be held when calling this 37 // method. 38 void updateTunnelModeStatus(); 39 40 // Dispatches tunnelModeEnabled to all registered listeners 41 void dispatchTunnelModeEnabled(bool tunnelModeEnabled); 42 43 // Override for IBinder::DeathRecipient 44 void binderDied(const wp<IBinder>&) override; 45 46 // Registers a TunnelModeEnabled listener 47 void addListener(const sp<gui::ITunnelModeEnabledListener>& listener); 48 49 // Deregisters a TunnelModeEnabled listener 50 void removeListener(const sp<gui::ITunnelModeEnabledListener>& listener); 51 incrementTunnelModeCount()52 inline void incrementTunnelModeCount() { mTunnelModeCount++; } decrementTunnelModeCount()53 inline void decrementTunnelModeCount() { mTunnelModeCount--; } 54 55 private: 56 mutable std::mutex mMutex; 57 struct WpHash { operatorWpHash58 size_t operator()(const wp<IBinder>& p) const { 59 return std::hash<IBinder*>()(p.unsafe_get()); 60 } 61 }; 62 63 std::unordered_map<wp<IBinder>, sp<gui::ITunnelModeEnabledListener>, WpHash> mListeners 64 GUARDED_BY(mMutex); 65 bool mTunnelModeEnabled GUARDED_BY(mMutex) = false; 66 uint32_t mTunnelModeCount = 0; 67 }; 68 69 } // namespace android 70