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> |
| 22 | #include <binder/IServiceManager.h> |
| 23 | |
| 24 | namespace android { |
| 25 | |
| 26 | // Class that handles binder death notifications. libbinder wants the recipient |
| 27 | // to be wrapped in sp<>, so registering RealBinderWrapper as a recipient would |
| 28 | // be awkward. |
| 29 | class RealBinderWrapper::DeathRecipient : public IBinder::DeathRecipient { |
| 30 | public: |
| 31 | explicit DeathRecipient(const base::Closure& callback) |
| 32 | : callback_(callback) {} |
| 33 | ~DeathRecipient() = default; |
| 34 | |
| 35 | // IBinder::DeathRecipient: |
| 36 | void binderDied(const wp<IBinder>& who) override { |
| 37 | callback_.Run(); |
| 38 | } |
| 39 | |
| 40 | private: |
| 41 | // Callback to run in response to binder death. |
| 42 | base::Closure callback_; |
| 43 | |
| 44 | DISALLOW_COPY_AND_ASSIGN(DeathRecipient); |
| 45 | }; |
| 46 | |
| 47 | RealBinderWrapper::RealBinderWrapper() = default; |
| 48 | |
| 49 | RealBinderWrapper::~RealBinderWrapper() = default; |
| 50 | |
| 51 | sp<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 | |
| 64 | bool 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 | |
| 81 | sp<BBinder> RealBinderWrapper::CreateLocalBinder() { |
| 82 | return sp<BBinder>(new BBinder()); |
| 83 | } |
| 84 | |
| 85 | bool RealBinderWrapper::RegisterForDeathNotifications( |
| 86 | const sp<IBinder>& binder, |
| 87 | const base::Closure& callback) { |
| 88 | sp<DeathRecipient> recipient(new DeathRecipient(callback)); |
| 89 | if (binder->linkToDeath(recipient) != OK) { |
| 90 | LOG(ERROR) << "Failed to register for death notifications on " |
| 91 | << binder.get(); |
| 92 | return false; |
| 93 | } |
| 94 | death_recipients_[binder] = recipient; |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | bool RealBinderWrapper::UnregisterForDeathNotifications( |
| 99 | const sp<IBinder>& binder) { |
| 100 | auto it = death_recipients_.find(binder); |
| 101 | if (it == death_recipients_.end()) { |
| 102 | LOG(ERROR) << "Not registered for death notifications on " << binder.get(); |
| 103 | return false; |
| 104 | } |
| 105 | if (binder->unlinkToDeath(it->second) != OK) { |
| 106 | LOG(ERROR) << "Failed to unregister for death notifications on " |
| 107 | << binder.get(); |
| 108 | return false; |
| 109 | } |
| 110 | death_recipients_.erase(it); |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | } // namespace android |