blob: fc5708b6288148042afbfa10af2a13660277eefe [file] [log] [blame]
Kelvin Zhang0c3e2472023-07-05 16:04:15 -07001/*
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#ifndef SYSTEM_CORE_INCLUDE_BINDERWRAPPER_BINDER_WRAPPER_H_
18#define SYSTEM_CORE_INCLUDE_BINDERWRAPPER_BINDER_WRAPPER_H_
19
20#include <sys/types.h>
21
22#include <functional>
23#include <string>
24
25#include <utils/StrongPointer.h>
26
27namespace android {
28
29class BBinder;
30class IBinder;
31
32// Wraps libbinder to make it testable.
33// NOTE: Static methods of this class are not thread-safe.
34class BinderWrapper {
35 public:
36 virtual ~BinderWrapper() {}
37
38 // Creates and initializes the singleton (using a wrapper that communicates
39 // with the real binder system).
40 static void Create();
41
42 // Initializes |wrapper| as the singleton, taking ownership of it. Tests that
43 // want to inject their own wrappers should call this instead of Create().
44 static void InitForTesting(BinderWrapper* wrapper);
45
46 // Destroys the singleton. Must be called before calling Create() or
47 // InitForTesting() a second time.
48 static void Destroy();
49
50 // Returns the singleton instance previously created by Create() or set by
51 // InitForTesting().
52 static BinderWrapper* Get();
53
54 // Returns the singleton instance if it was previously created by Create() or
55 // set by InitForTesting(), or creates a new one by calling Create().
56 static BinderWrapper* GetOrCreateInstance();
57
58 // Gets the binder for communicating with the service identified by
59 // |service_name|, returning null immediately if it doesn't exist.
60 virtual sp<IBinder> GetService(const std::string& service_name) = 0;
61
62 // Registers |binder| as |service_name| with the service manager.
63 virtual bool RegisterService(const std::string& service_name,
64 const sp<IBinder>& binder) = 0;
65
66 // Creates a local binder object.
67 virtual sp<BBinder> CreateLocalBinder() = 0;
68
69 // Registers |callback| to be invoked when |binder| dies. If another callback
70 // is currently registered for |binder|, it will be replaced.
71 virtual bool RegisterForDeathNotifications(const sp<IBinder>& binder,
72 const std::function<void()>&) = 0;
73
74 // Unregisters the callback, if any, for |binder|.
75 virtual bool UnregisterForDeathNotifications(const sp<IBinder>& binder) = 0;
76
77 // When called while in a transaction, returns the caller's UID or PID.
78 virtual uid_t GetCallingUid() = 0;
79 virtual pid_t GetCallingPid() = 0;
80
81 private:
82 static BinderWrapper* instance_;
83};
84
85} // namespace android
86
87#endif // SYSTEM_CORE_INCLUDE_BINDERWRAPPER_BINDER_WRAPPER_H_