blob: 46bc5fbf948b8a312167ccd3a1e57cde57b69423 [file] [log] [blame]
Chong Zhanga9d45c72020-09-09 12:41:17 -07001/**
2 *
3 * Copyright 2020, The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef ANDROID_MEDIA_RESOURCE_OBSERVER_SERVICE_H
19#define ANDROID_MEDIA_RESOURCE_OBSERVER_SERVICE_H
20
21#include <map>
22
23#include <aidl/android/media/BnResourceObserverService.h>
24#include "ResourceManagerService.h"
25
26namespace android {
27
28using Status = ::ndk::ScopedAStatus;
29using ::aidl::android::media::BnResourceObserverService;
30using ::aidl::android::media::IResourceObserver;
31using ::aidl::android::media::MediaObservableFilter;
32using ::aidl::android::media::MediaObservableParcel;
33using ::aidl::android::media::MediaObservableType;
34using ::aidl::android::media::MediaObservableEvent;
35
36class ResourceObserverService : public BnResourceObserverService {
37public:
38
39 static char const *getServiceName() { return "media.resource_observer"; }
40 static std::shared_ptr<ResourceObserverService> instantiate();
41
42 virtual inline binder_status_t dump(
43 int /*fd*/, const char** /*args*/, uint32_t /*numArgs*/);
44
45 ResourceObserverService();
46 virtual ~ResourceObserverService() {}
47
48 // IResourceObserverService interface
49 Status registerObserver(const std::shared_ptr<IResourceObserver>& in_observer,
50 const std::vector<MediaObservableFilter>& in_filters) override;
51
52 Status unregisterObserver(const std::shared_ptr<IResourceObserver>& in_observer) override;
53 // ~IResourceObserverService interface
54
55 // Called by ResourceManagerService when resources are added.
56 void onResourceAdded(int uid, int pid, const ResourceList &resources);
57
58 // Called by ResourceManagerService when resources are removed.
59 void onResourceRemoved(int uid, int pid, const ResourceList &resources);
60
61private:
62 struct ObserverInfo {
63 ::ndk::SpAIBinder binder;
64 std::shared_ptr<IResourceObserver> observer;
65 std::vector<MediaObservableFilter> filters;
66 };
67 struct DeathRecipient;
68
69 // Below maps are all keyed on the observer's binder ptr value.
70 using ObserverInfoMap = std::map<uintptr_t, ObserverInfo>;
71 using SubscriberMap = std::map<uintptr_t, std::shared_ptr<IResourceObserver>>;
72
73 std::mutex mObserverLock;
74 // Binder->ObserverInfo
75 ObserverInfoMap mObserverInfoMap GUARDED_BY(mObserverLock);
76 // Observable(<type,event>)->Subscribers
77 std::map<MediaObservableFilter, SubscriberMap> mObservableToSubscribersMap
78 GUARDED_BY(mObserverLock);
79
80 ::ndk::ScopedAIBinder_DeathRecipient mDeathRecipient;
81
82 // Binder death handling.
83 static std::mutex sDeathRecipientLock;
84 static std::map<uintptr_t, std::shared_ptr<DeathRecipient>> sDeathRecipientMap
85 GUARDED_BY(sDeathRecipientLock);
86 static void BinderDiedCallback(void* cookie);
87
88 void notifyObservers(MediaObservableEvent event,
89 int uid, int pid, const ResourceList &resources);
90};
91
92// ----------------------------------------------------------------------------
93} // namespace android
94
95#endif // ANDROID_MEDIA_RESOURCE_OBSERVER_SERVICE_H