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 <base/logging.h>
20 #include <binder/Binder.h>
21 #include <binder/IBinder.h>
22
23 namespace android {
24
StubBinderWrapper()25 StubBinderWrapper::StubBinderWrapper()
26 : calling_uid_(-1),
27 calling_pid_(-1) {}
28
29 StubBinderWrapper::~StubBinderWrapper() = default;
30
SetBinderForService(const std::string & service_name,const sp<IBinder> & binder)31 void StubBinderWrapper::SetBinderForService(const std::string& service_name,
32 const sp<IBinder>& binder) {
33 services_to_return_[service_name] = binder;
34 }
35
GetRegisteredService(const std::string & service_name) const36 sp<IBinder> StubBinderWrapper::GetRegisteredService(
37 const std::string& service_name) const {
38 const auto it = registered_services_.find(service_name);
39 return it != registered_services_.end() ? it->second : sp<IBinder>();
40 }
41
NotifyAboutBinderDeath(const sp<IBinder> & binder)42 void StubBinderWrapper::NotifyAboutBinderDeath(const sp<IBinder>& binder) {
43 const auto it = death_callbacks_.find(binder);
44 if (it != death_callbacks_.end())
45 it->second.Run();
46 }
47
GetService(const std::string & service_name)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
RegisterService(const std::string & service_name,const sp<IBinder> & binder)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
CreateLocalBinder()59 sp<BBinder> StubBinderWrapper::CreateLocalBinder() {
60 sp<BBinder> binder(new BBinder());
61 local_binders_.push_back(binder);
62 return binder;
63 }
64
RegisterForDeathNotifications(const sp<IBinder> & binder,const::base::Closure & callback)65 bool StubBinderWrapper::RegisterForDeathNotifications(
66 const sp<IBinder>& binder,
67 const ::base::Closure& callback) {
68 death_callbacks_[binder] = callback;
69 return true;
70 }
71
UnregisterForDeathNotifications(const sp<IBinder> & binder)72 bool StubBinderWrapper::UnregisterForDeathNotifications(
73 const sp<IBinder>& binder) {
74 death_callbacks_.erase(binder);
75 return true;
76 }
77
GetCallingUid()78 uid_t StubBinderWrapper::GetCallingUid() {
79 return calling_uid_;
80 }
81
GetCallingPid()82 pid_t StubBinderWrapper::GetCallingPid() {
83 return calling_pid_;
84 }
85
86 } // namespace android
87