blob: a104bff32306c38f5c1fc4ef05ab2f19976ec191 [file] [log] [blame]
Daniel Eratbe43a392015-09-08 13:20:02 -06001/*
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
Daniel Erat7cba9db2015-10-16 09:04:33 -060020#include <sys/types.h>
21
Daniel Eratbe43a392015-09-08 13:20:02 -060022#include <string>
23
24#include <base/callback.h>
25#include <utils/StrongPointer.h>
26
27namespace android {
28
29class BBinder;
30class IBinder;
31
32// Wraps libbinder to make it testable.
Alex Vakulenko08c98912016-01-04 12:22:05 -080033// NOTE: Static methods of this class are not thread-safe.
Daniel Eratbe43a392015-09-08 13:20:02 -060034class 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
Alex Vakulenko08c98912016-01-04 12:22:05 -080054 // 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
Daniel Eratbe43a392015-09-08 13:20:02 -060058 // 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(
72 const sp<IBinder>& binder,
Christopher Wiley09910f42016-04-12 09:23:40 -070073 const ::base::Closure& callback) = 0;
Daniel Eratbe43a392015-09-08 13:20:02 -060074
75 // Unregisters the callback, if any, for |binder|.
76 virtual bool UnregisterForDeathNotifications(const sp<IBinder>& binder) = 0;
77
Daniel Erat7cba9db2015-10-16 09:04:33 -060078 // When called while in a transaction, returns the caller's UID or PID.
79 virtual uid_t GetCallingUid() = 0;
80 virtual pid_t GetCallingPid() = 0;
81
Daniel Eratbe43a392015-09-08 13:20:02 -060082 private:
83 static BinderWrapper* instance_;
84};
85
86} // namespace android
87
88#endif // SYSTEM_CORE_INCLUDE_BINDERWRAPPER_BINDER_WRAPPER_H_