• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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 ANDROID_HARDWARE_IBINDER_H
18 #define ANDROID_HARDWARE_IBINDER_H
19 
20 #include <functional>
21 
22 #include <utils/Errors.h>
23 #include <utils/RefBase.h>
24 #include <utils/String16.h>
25 #include <utils/Vector.h>
26 
27 // ---------------------------------------------------------------------------
28 namespace android {
29 namespace hardware {
30 
31 class BHwBinder;
32 class BpHwBinder;
33 class IInterface;
34 class Parcel;
35 
36 /**
37  * Base class and low-level protocol for a remotable object.
38  * You can derive from this class to create an object for which other
39  * processes can hold references to it.  Communication between processes
40  * (method calls, property get and set) is down through a low-level
41  * protocol implemented on top of the transact() API.
42  */
43 class IBinder : public virtual RefBase
44 {
45 public:
46     using TransactCallback = std::function<void(Parcel&)>;
47 
48     enum {
49         // Corresponds to TF_ONE_WAY -- an asynchronous call.
50         FLAG_ONEWAY             = 0x00000001
51     };
52 
53                           IBinder();
54 
55     virtual status_t        transact(   uint32_t code,
56                                         const Parcel& data,
57                                         Parcel* reply,
58                                         uint32_t flags = 0,
59                                         TransactCallback callback = nullptr) = 0;
60 
61     class DeathRecipient : public virtual RefBase
62     {
63     public:
64         virtual void binderDied(const wp<IBinder>& who) = 0;
65     };
66 
67     /**
68      * Register the @a recipient for a notification if this binder
69      * goes away.  If this binder object unexpectedly goes away
70      * (typically because its hosting process has been killed),
71      * then DeathRecipient::binderDied() will be called with a reference
72      * to this.
73      *
74      * The @a cookie is optional -- if non-NULL, it should be a
75      * memory address that you own (that is, you know it is unique).
76      *
77      * @note You will only receive death notifications for remote binders,
78      * as local binders by definition can't die without you dying as well.
79      * Trying to use this function on a local binder will result in an
80      * INVALID_OPERATION code being returned and nothing happening.
81      *
82      * @note This link always holds a weak reference to its recipient.
83      *
84      * @note You will only receive a weak reference to the dead
85      * binder.  You should not try to promote this to a strong reference.
86      * (Nor should you need to, as there is nothing useful you can
87      * directly do with it now that it has passed on.)
88      */
89     virtual status_t        linkToDeath(const sp<DeathRecipient>& recipient,
90                                         void* cookie = NULL,
91                                         uint32_t flags = 0) = 0;
92 
93     /**
94      * Remove a previously registered death notification.
95      * The @a recipient will no longer be called if this object
96      * dies.  The @a cookie is optional.  If non-NULL, you can
97      * supply a NULL @a recipient, and the recipient previously
98      * added with that cookie will be unlinked.
99      */
100     virtual status_t        unlinkToDeath(  const wp<DeathRecipient>& recipient,
101                                             void* cookie = NULL,
102                                             uint32_t flags = 0,
103                                             wp<DeathRecipient>* outRecipient = NULL) = 0;
104 
105     virtual bool            checkSubclass(const void* subclassID) const;
106 
107     typedef void (*object_cleanup_func)(const void* id, void* obj, void* cleanupCookie);
108 
109     virtual void            attachObject(   const void* objectID,
110                                             void* object,
111                                             void* cleanupCookie,
112                                             object_cleanup_func func) = 0;
113     virtual void*           findObject(const void* objectID) const = 0;
114     virtual void            detachObject(const void* objectID) = 0;
115 
116     virtual BHwBinder*        localBinder();
117     virtual BpHwBinder*       remoteBinder();
118 
119 protected:
120     virtual          ~IBinder();
121 
122 private:
123 };
124 
125 }; // namespace hardware
126 }; // namespace android
127 
128 // ---------------------------------------------------------------------------
129 
130 #endif // ANDROID_HARDWARE_IBINDER_H
131