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 #include "WpHash.h"
26 
27 namespace android {
28 
29 class Layer;
30 class SurfaceFlinger;
31 
32 class TunnelModeEnabledReporter : public IBinder::DeathRecipient {
33 public:
34     TunnelModeEnabledReporter();
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 
incrementTunnelModeCount()54     inline void incrementTunnelModeCount() { mTunnelModeCount++; }
decrementTunnelModeCount()55     inline void decrementTunnelModeCount() { mTunnelModeCount--; }
56 
57 private:
58     mutable std::mutex mMutex;
59 
60     std::unordered_map<wp<IBinder>, sp<gui::ITunnelModeEnabledListener>, WpHash> mListeners
61             GUARDED_BY(mMutex);
62     bool mTunnelModeEnabled GUARDED_BY(mMutex) = false;
63     uint32_t mTunnelModeCount = 0;
64 };
65 
66 } // namespace android
67