blob: 802d22d86ac857b878f6b9f50978d4943cb1bc02 [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
Angel Aguayob398ee22021-10-14 00:39:15 +000025#include "WpHash.h"
26
Galia Peycheva8f04b302021-04-27 13:25:38 +020027namespace android {
28
29class Layer;
30class SurfaceFlinger;
31
32class TunnelModeEnabledReporter : public IBinder::DeathRecipient {
33public:
Robert Carr3e2a2992021-06-11 13:42:55 -070034 TunnelModeEnabledReporter();
Galia Peycheva8f04b302021-04-27 13:25:38 +020035
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 Carr3e2a2992021-06-11 13:42:55 -070054 inline void incrementTunnelModeCount() { mTunnelModeCount++; }
55 inline void decrementTunnelModeCount() { mTunnelModeCount--; }
56
Galia Peycheva8f04b302021-04-27 13:25:38 +020057private:
58 mutable std::mutex mMutex;
Galia Peycheva8f04b302021-04-27 13:25:38 +020059
Galia Peycheva8f04b302021-04-27 13:25:38 +020060 std::unordered_map<wp<IBinder>, sp<gui::ITunnelModeEnabledListener>, WpHash> mListeners
61 GUARDED_BY(mMutex);
62 bool mTunnelModeEnabled GUARDED_BY(mMutex) = false;
Robert Carr3e2a2992021-06-11 13:42:55 -070063 uint32_t mTunnelModeCount = 0;
Galia Peycheva8f04b302021-04-27 13:25:38 +020064};
65
66} // namespace android