Daniel Erat | be43a39 | 2015-09-08 13:20:02 -0600 | [diff] [blame] | 1 | /* |
| 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 Erat | 7cba9db | 2015-10-16 09:04:33 -0600 | [diff] [blame] | 22 | #include <binder/IPCThreadState.h> |
Daniel Erat | be43a39 | 2015-09-08 13:20:02 -0600 | [diff] [blame] | 23 | #include <binder/IServiceManager.h> |
| 24 | |
| 25 | namespace 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. |
| 30 | class RealBinderWrapper::DeathRecipient : public IBinder::DeathRecipient { |
| 31 | public: |
Christopher Wiley | 09910f4 | 2016-04-12 09:23:40 -0700 | [diff] [blame] | 32 | explicit DeathRecipient(const ::base::Closure& callback) |
Daniel Erat | be43a39 | 2015-09-08 13:20:02 -0600 | [diff] [blame] | 33 | : 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 Wiley | 09910f4 | 2016-04-12 09:23:40 -0700 | [diff] [blame] | 43 | ::base::Closure callback_; |
Daniel Erat | be43a39 | 2015-09-08 13:20:02 -0600 | [diff] [blame] | 44 | |
| 45 | DISALLOW_COPY_AND_ASSIGN(DeathRecipient); |
| 46 | }; |
| 47 | |
| 48 | RealBinderWrapper::RealBinderWrapper() = default; |
| 49 | |
| 50 | RealBinderWrapper::~RealBinderWrapper() = default; |
| 51 | |
| 52 | sp<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 | |
| 65 | bool 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 | |
| 82 | sp<BBinder> RealBinderWrapper::CreateLocalBinder() { |
| 83 | return sp<BBinder>(new BBinder()); |
| 84 | } |
| 85 | |
| 86 | bool RealBinderWrapper::RegisterForDeathNotifications( |
| 87 | const sp<IBinder>& binder, |
Christopher Wiley | 09910f4 | 2016-04-12 09:23:40 -0700 | [diff] [blame] | 88 | const ::base::Closure& callback) { |
Daniel Erat | be43a39 | 2015-09-08 13:20:02 -0600 | [diff] [blame] | 89 | 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 | |
| 99 | bool 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 Erat | 7cba9db | 2015-10-16 09:04:33 -0600 | [diff] [blame] | 115 | uid_t RealBinderWrapper::GetCallingUid() { |
| 116 | return IPCThreadState::self()->getCallingUid(); |
| 117 | } |
| 118 | |
| 119 | pid_t RealBinderWrapper::GetCallingPid() { |
| 120 | return IPCThreadState::self()->getCallingPid(); |
| 121 | } |
| 122 | |
Daniel Erat | be43a39 | 2015-09-08 13:20:02 -0600 | [diff] [blame] | 123 | } // namespace android |