Galia Peycheva | 8f04b30 | 2021-04-27 13:25:38 +0200 | [diff] [blame] | 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: |
Robert Carr | 3e2a299 | 2021-06-11 13:42:55 -0700 | [diff] [blame^] | 32 | TunnelModeEnabledReporter(); |
Galia Peycheva | 8f04b30 | 2021-04-27 13:25:38 +0200 | [diff] [blame] | 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 | |
Robert Carr | 3e2a299 | 2021-06-11 13:42:55 -0700 | [diff] [blame^] | 52 | inline void incrementTunnelModeCount() { mTunnelModeCount++; } |
| 53 | inline void decrementTunnelModeCount() { mTunnelModeCount--; } |
| 54 | |
Galia Peycheva | 8f04b30 | 2021-04-27 13:25:38 +0200 | [diff] [blame] | 55 | private: |
| 56 | mutable std::mutex mMutex; |
| 57 | struct WpHash { |
| 58 | size_t operator()(const wp<IBinder>& p) const { |
| 59 | return std::hash<IBinder*>()(p.unsafe_get()); |
| 60 | } |
| 61 | }; |
| 62 | |
Galia Peycheva | 8f04b30 | 2021-04-27 13:25:38 +0200 | [diff] [blame] | 63 | std::unordered_map<wp<IBinder>, sp<gui::ITunnelModeEnabledListener>, WpHash> mListeners |
| 64 | GUARDED_BY(mMutex); |
| 65 | bool mTunnelModeEnabled GUARDED_BY(mMutex) = false; |
Robert Carr | 3e2a299 | 2021-06-11 13:42:55 -0700 | [diff] [blame^] | 66 | uint32_t mTunnelModeCount = 0; |
Galia Peycheva | 8f04b30 | 2021-04-27 13:25:38 +0200 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | } // namespace android |