blob: 607c18bce8206e85b1b4dcd7d0a71fc14cbc13c6 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
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_IBINDER_H
18#define ANDROID_IBINDER_H
19
Christopher Wiley3eab1b42015-12-04 14:15:27 -080020#include <cstdlib> // Defines types needed for linux/binder.h
21#include <linux/binder.h> // Needed for B_PACK_CHARS
22
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023#include <utils/Errors.h>
24#include <utils/RefBase.h>
25#include <utils/String16.h>
26#include <utils/Vector.h>
27
Christopher Wiley3eab1b42015-12-04 14:15:27 -080028#ifndef B_PACK_CHARS
29#error "linux/binder.h no longer defines B_PACK_CHARS"
30#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031
32// ---------------------------------------------------------------------------
33namespace android {
34
35class BBinder;
36class BpBinder;
37class IInterface;
38class Parcel;
39
40/**
41 * Base class and low-level protocol for a remotable object.
42 * You can derive from this class to create an object for which other
43 * processes can hold references to it. Communication between processes
44 * (method calls, property get and set) is down through a low-level
45 * protocol implemented on top of the transact() API.
46 */
47class IBinder : public virtual RefBase
48{
49public:
50 enum {
51 FIRST_CALL_TRANSACTION = 0x00000001,
52 LAST_CALL_TRANSACTION = 0x00ffffff,
53
54 PING_TRANSACTION = B_PACK_CHARS('_','P','N','G'),
55 DUMP_TRANSACTION = B_PACK_CHARS('_','D','M','P'),
56 INTERFACE_TRANSACTION = B_PACK_CHARS('_', 'N', 'T', 'F'),
Dianne Hackborn555f89d2012-05-08 18:54:22 -070057 SYSPROPS_TRANSACTION = B_PACK_CHARS('_', 'S', 'P', 'R'),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -080059 // Corresponds to TF_ONE_WAY -- an asynchronous call.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060 FLAG_ONEWAY = 0x00000001
61 };
62
Mathias Agopian83c04462009-05-22 19:00:22 -070063 IBinder();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064
65 /**
66 * Check if this IBinder implements the interface named by
67 * @a descriptor. If it does, the base pointer to it is returned,
68 * which you can safely static_cast<> to the concrete C++ interface.
69 */
70 virtual sp<IInterface> queryLocalInterface(const String16& descriptor);
71
72 /**
73 * Return the canonical name of the interface provided by this IBinder
74 * object.
75 */
Mathias Agopian83c04462009-05-22 19:00:22 -070076 virtual const String16& getInterfaceDescriptor() const = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077
78 virtual bool isBinderAlive() const = 0;
79 virtual status_t pingBinder() = 0;
80 virtual status_t dump(int fd, const Vector<String16>& args) = 0;
81
82 virtual status_t transact( uint32_t code,
83 const Parcel& data,
84 Parcel* reply,
85 uint32_t flags = 0) = 0;
86
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 class DeathRecipient : public virtual RefBase
88 {
89 public:
90 virtual void binderDied(const wp<IBinder>& who) = 0;
91 };
92
93 /**
94 * Register the @a recipient for a notification if this binder
95 * goes away. If this binder object unexpectedly goes away
96 * (typically because its hosting process has been killed),
Christopher Tate71f64dd2011-02-17 13:00:38 -080097 * then DeathRecipient::binderDied() will be called with a reference
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080098 * to this.
99 *
100 * The @a cookie is optional -- if non-NULL, it should be a
101 * memory address that you own (that is, you know it is unique).
102 *
103 * @note You will only receive death notifications for remote binders,
104 * as local binders by definition can't die without you dying as well.
105 * Trying to use this function on a local binder will result in an
106 * INVALID_OPERATION code being returned and nothing happening.
107 *
108 * @note This link always holds a weak reference to its recipient.
109 *
110 * @note You will only receive a weak reference to the dead
111 * binder. You should not try to promote this to a strong reference.
112 * (Nor should you need to, as there is nothing useful you can
113 * directly do with it now that it has passed on.)
114 */
115 virtual status_t linkToDeath(const sp<DeathRecipient>& recipient,
116 void* cookie = NULL,
117 uint32_t flags = 0) = 0;
118
119 /**
120 * Remove a previously registered death notification.
121 * The @a recipient will no longer be called if this object
122 * dies. The @a cookie is optional. If non-NULL, you can
123 * supply a NULL @a recipient, and the recipient previously
124 * added with that cookie will be unlinked.
125 */
126 virtual status_t unlinkToDeath( const wp<DeathRecipient>& recipient,
127 void* cookie = NULL,
128 uint32_t flags = 0,
129 wp<DeathRecipient>* outRecipient = NULL) = 0;
130
131 virtual bool checkSubclass(const void* subclassID) const;
132
133 typedef void (*object_cleanup_func)(const void* id, void* obj, void* cleanupCookie);
134
135 virtual void attachObject( const void* objectID,
136 void* object,
137 void* cleanupCookie,
138 object_cleanup_func func) = 0;
139 virtual void* findObject(const void* objectID) const = 0;
140 virtual void detachObject(const void* objectID) = 0;
141
142 virtual BBinder* localBinder();
143 virtual BpBinder* remoteBinder();
144
145protected:
Mathias Agopian83c04462009-05-22 19:00:22 -0700146 virtual ~IBinder();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800147
148private:
149};
150
151}; // namespace android
152
153// ---------------------------------------------------------------------------
154
155#endif // ANDROID_IBINDER_H