| 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 |  | 
| Angel Aguayo | b398ee2 | 2021-10-14 00:39:15 +0000 | [diff] [blame] | 25 | #include "WpHash.h" | 
|  | 26 |  | 
| Galia Peycheva | 8f04b30 | 2021-04-27 13:25:38 +0200 | [diff] [blame] | 27 | namespace android { | 
|  | 28 |  | 
|  | 29 | class Layer; | 
|  | 30 | class SurfaceFlinger; | 
|  | 31 |  | 
|  | 32 | class TunnelModeEnabledReporter : public IBinder::DeathRecipient { | 
|  | 33 | public: | 
| Robert Carr | 3e2a299 | 2021-06-11 13:42:55 -0700 | [diff] [blame] | 34 | TunnelModeEnabledReporter(); | 
| Galia Peycheva | 8f04b30 | 2021-04-27 13:25:38 +0200 | [diff] [blame] | 35 |  | 
|  | 36 | // Checks if there is a tunnel mode enabled state change and if so, dispatches the updated | 
|  | 37 | // tunnel mode enabled/disabled state to the registered listeners | 
|  | 38 | // This method performs layer stack traversals, so mStateLock must be held when calling this | 
|  | 39 | // method. | 
|  | 40 | void updateTunnelModeStatus(); | 
|  | 41 |  | 
|  | 42 | // Dispatches tunnelModeEnabled to all registered listeners | 
|  | 43 | void dispatchTunnelModeEnabled(bool tunnelModeEnabled); | 
|  | 44 |  | 
|  | 45 | // Override for IBinder::DeathRecipient | 
|  | 46 | void binderDied(const wp<IBinder>&) override; | 
|  | 47 |  | 
|  | 48 | // Registers a TunnelModeEnabled listener | 
|  | 49 | void addListener(const sp<gui::ITunnelModeEnabledListener>& listener); | 
|  | 50 |  | 
|  | 51 | // Deregisters a TunnelModeEnabled listener | 
|  | 52 | void removeListener(const sp<gui::ITunnelModeEnabledListener>& listener); | 
|  | 53 |  | 
| Robert Carr | 3e2a299 | 2021-06-11 13:42:55 -0700 | [diff] [blame] | 54 | inline void incrementTunnelModeCount() { mTunnelModeCount++; } | 
|  | 55 | inline void decrementTunnelModeCount() { mTunnelModeCount--; } | 
|  | 56 |  | 
| Galia Peycheva | 8f04b30 | 2021-04-27 13:25:38 +0200 | [diff] [blame] | 57 | private: | 
|  | 58 | mutable std::mutex mMutex; | 
| Galia Peycheva | 8f04b30 | 2021-04-27 13:25:38 +0200 | [diff] [blame] | 59 |  | 
| Galia Peycheva | 8f04b30 | 2021-04-27 13:25:38 +0200 | [diff] [blame] | 60 | std::unordered_map<wp<IBinder>, sp<gui::ITunnelModeEnabledListener>, WpHash> mListeners | 
|  | 61 | GUARDED_BY(mMutex); | 
|  | 62 | bool mTunnelModeEnabled GUARDED_BY(mMutex) = false; | 
| Robert Carr | 3e2a299 | 2021-06-11 13:42:55 -0700 | [diff] [blame] | 63 | uint32_t mTunnelModeCount = 0; | 
| Galia Peycheva | 8f04b30 | 2021-04-27 13:25:38 +0200 | [diff] [blame] | 64 | }; | 
|  | 65 |  | 
|  | 66 | } // namespace android |