blob: d55507a8e9b344e9a589e0c7e7df9d147cc13d1f [file] [log] [blame]
Galia Peycheva8f04b302021-04-27 13:25:38 +02001/*
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
25namespace android {
26
27class Layer;
28class SurfaceFlinger;
29
30class TunnelModeEnabledReporter : public IBinder::DeathRecipient {
31public:
32 TunnelModeEnabledReporter(SurfaceFlinger& flinger);
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
52private:
53 mutable std::mutex mMutex;
54 struct WpHash {
55 size_t operator()(const wp<IBinder>& p) const {
56 return std::hash<IBinder*>()(p.unsafe_get());
57 }
58 };
59
60 SurfaceFlinger& mFlinger;
61 std::unordered_map<wp<IBinder>, sp<gui::ITunnelModeEnabledListener>, WpHash> mListeners
62 GUARDED_BY(mMutex);
63 bool mTunnelModeEnabled GUARDED_BY(mMutex) = false;
64};
65
66} // namespace android