blob: d214ce31c10f4c1eda120bcb27cbe228bed69368 [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 "real_binder_wrapper.h"
18
19#include <android-base/logging.h>
20
21#include <binder/Binder.h>
22#include <binder/IBinder.h>
23#include <binder/IPCThreadState.h>
24#include <binder/IServiceManager.h>
25
26namespace android {
27
28// Class that handles binder death notifications. libbinder wants the recipient
29// to be wrapped in sp<>, so registering RealBinderWrapper as a recipient would
30// be awkward.
31class RealBinderWrapper::DeathRecipient : public IBinder::DeathRecipient {
32 public:
33 explicit DeathRecipient(const std::function<void()>& callback)
34 : callback_(std::move(callback)) {}
35 ~DeathRecipient() = default;
36
37 // IBinder::DeathRecipient:
38 void binderDied(const wp<IBinder>& who) override { callback_(); }
39
40 private:
41 // Callback to run in response to binder death.
42 std::function<void()> callback_;
43
44 DISALLOW_COPY_AND_ASSIGN(DeathRecipient);
45};
46
47RealBinderWrapper::RealBinderWrapper() = default;
48
49RealBinderWrapper::~RealBinderWrapper() = default;
50
51sp<IBinder> RealBinderWrapper::GetService(const std::string& service_name) {
52 sp<IServiceManager> service_manager = defaultServiceManager();
53 if (!service_manager.get()) {
54 LOG(ERROR) << "Unable to get service manager";
55 return sp<IBinder>();
56 }
57 sp<IBinder> binder =
58 service_manager->checkService(String16(service_name.c_str()));
59 if (!binder.get())
60 LOG(ERROR) << "Unable to get \"" << service_name << "\" service";
61 return binder;
62}
63
64bool RealBinderWrapper::RegisterService(const std::string& service_name,
65 const sp<IBinder>& binder) {
66 sp<IServiceManager> service_manager = defaultServiceManager();
67 if (!service_manager.get()) {
68 LOG(ERROR) << "Unable to get service manager";
69 return false;
70 }
71 status_t status = defaultServiceManager()->addService(
72 String16(service_name.c_str()), binder);
73 if (status != OK) {
74 LOG(ERROR) << "Failed to register \"" << service_name << "\" with service "
75 << "manager";
76 return false;
77 }
78 return true;
79}
80
81sp<BBinder> RealBinderWrapper::CreateLocalBinder() {
82 return sp<BBinder>(new BBinder());
83}
84
85bool RealBinderWrapper::RegisterForDeathNotifications(const sp<IBinder>& binder,
86 const std::function<void()>& callback) {
87 sp<DeathRecipient> recipient(new DeathRecipient(callback));
88 if (binder->linkToDeath(recipient) != OK) {
89 LOG(ERROR) << "Failed to register for death notifications on "
90 << binder.get();
91 return false;
92 }
93 death_recipients_[binder] = recipient;
94 return true;
95}
96
97bool RealBinderWrapper::UnregisterForDeathNotifications(
98 const sp<IBinder>& binder) {
99 auto it = death_recipients_.find(binder);
100 if (it == death_recipients_.end()) {
101 LOG(ERROR) << "Not registered for death notifications on " << binder.get();
102 return false;
103 }
104 if (binder->unlinkToDeath(it->second) != OK) {
105 LOG(ERROR) << "Failed to unregister for death notifications on "
106 << binder.get();
107 return false;
108 }
109 death_recipients_.erase(it);
110 return true;
111}
112
113uid_t RealBinderWrapper::GetCallingUid() {
114 return IPCThreadState::self()->getCallingUid();
115}
116
117pid_t RealBinderWrapper::GetCallingPid() {
118 return IPCThreadState::self()->getCallingPid();
119}
120
121} // namespace android