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