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 #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 <string>
23 
24 #include <base/callback.h>
25 #include <utils/StrongPointer.h>
26 
27 namespace android {
28 
29 class BBinder;
30 class IBinder;
31 
32 // Wraps libbinder to make it testable.
33 // NOTE: Static methods of this class are not thread-safe.
34 class BinderWrapper {
35  public:
~BinderWrapper()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(
72       const sp<IBinder>& binder,
73       const ::base::Closure& callback) = 0;
74 
75   // Unregisters the callback, if any, for |binder|.
76   virtual bool UnregisterForDeathNotifications(const sp<IBinder>& binder) = 0;
77 
78   // 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 
82  private:
83   static BinderWrapper* instance_;
84 };
85 
86 }  // namespace android
87 
88 #endif  // SYSTEM_CORE_INCLUDE_BINDERWRAPPER_BINDER_WRAPPER_H_
89