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