blob: fabf1228a05c93c0da2b170711b76106b739ce0a [file] [log] [blame]
Kelvin Zhang0c3e2472023-07-05 16:04:15 -07001/*
2 * Copyright (C) 2015 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#include <binderwrapper/stub_binder_wrapper.h>
18
19#include <android-base/logging.h>
20
21#include <binder/Binder.h>
22#include <binder/IBinder.h>
23
24namespace android {
25
26StubBinderWrapper::StubBinderWrapper()
27 : calling_uid_(-1),
28 calling_pid_(-1) {}
29
30StubBinderWrapper::~StubBinderWrapper() = default;
31
32void StubBinderWrapper::SetBinderForService(const std::string& service_name,
33 const sp<IBinder>& binder) {
34 services_to_return_[service_name] = binder;
35}
36
37sp<IBinder> StubBinderWrapper::GetRegisteredService(
38 const std::string& service_name) const {
39 const auto it = registered_services_.find(service_name);
40 return it != registered_services_.end() ? it->second : sp<IBinder>();
41}
42
43void StubBinderWrapper::NotifyAboutBinderDeath(const sp<IBinder>& binder) {
44 const auto it = death_callbacks_.find(binder);
45 if (it != death_callbacks_.end()) it->second();
46}
47
48sp<IBinder> StubBinderWrapper::GetService(const std::string& service_name) {
49 const auto it = services_to_return_.find(service_name);
50 return it != services_to_return_.end() ? it->second : sp<IBinder>();
51}
52
53bool StubBinderWrapper::RegisterService(const std::string& service_name,
54 const sp<IBinder>& binder) {
55 registered_services_[service_name] = binder;
56 return true;
57}
58
59sp<BBinder> StubBinderWrapper::CreateLocalBinder() {
60 sp<BBinder> binder(new BBinder());
61 local_binders_.push_back(binder);
62 return binder;
63}
64
65bool StubBinderWrapper::RegisterForDeathNotifications(const sp<IBinder>& binder,
66 const std::function<void()>& callback) {
67 death_callbacks_[binder] = callback;
68 return true;
69}
70
71bool StubBinderWrapper::UnregisterForDeathNotifications(
72 const sp<IBinder>& binder) {
73 death_callbacks_.erase(binder);
74 return true;
75}
76
77uid_t StubBinderWrapper::GetCallingUid() {
78 return calling_uid_;
79}
80
81pid_t StubBinderWrapper::GetCallingPid() {
82 return calling_pid_;
83}
84
85} // namespace android