1/*
2 * Copyright (C) 2005 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_BPBINDER_H
18#define ANDROID_BPBINDER_H
19
20#include <binder/IBinder.h>
21#include <utils/KeyedVector.h>
22#include <utils/threads.h>
23
24// ---------------------------------------------------------------------------
25namespace android {
26
27class BpBinder : public IBinder
28{
29public:
30                        BpBinder(int32_t handle);
31
32    inline  int32_t     handle() const { return mHandle; }
33
34    virtual const String16&    getInterfaceDescriptor() const;
35    virtual bool        isBinderAlive() const;
36    virtual status_t    pingBinder();
37    virtual status_t    dump(int fd, const Vector<String16>& args);
38
39    virtual status_t    transact(   uint32_t code,
40                                    const Parcel& data,
41                                    Parcel* reply,
42                                    uint32_t flags = 0);
43
44    virtual status_t    linkToDeath(const sp<DeathRecipient>& recipient,
45                                    void* cookie = NULL,
46                                    uint32_t flags = 0);
47    virtual status_t    unlinkToDeath(  const wp<DeathRecipient>& recipient,
48                                        void* cookie = NULL,
49                                        uint32_t flags = 0,
50                                        wp<DeathRecipient>* outRecipient = NULL);
51
52    virtual void        attachObject(   const void* objectID,
53                                        void* object,
54                                        void* cleanupCookie,
55                                        object_cleanup_func func);
56    virtual void*       findObject(const void* objectID) const;
57    virtual void        detachObject(const void* objectID);
58
59    virtual BpBinder*   remoteBinder();
60
61            status_t    setConstantData(const void* data, size_t size);
62            void        sendObituary();
63
64    class ObjectManager
65    {
66    public:
67                    ObjectManager();
68                    ~ObjectManager();
69
70        void        attach( const void* objectID,
71                            void* object,
72                            void* cleanupCookie,
73                            IBinder::object_cleanup_func func);
74        void*       find(const void* objectID) const;
75        void        detach(const void* objectID);
76
77        void        kill();
78
79    private:
80                    ObjectManager(const ObjectManager&);
81        ObjectManager& operator=(const ObjectManager&);
82
83        struct entry_t
84        {
85            void* object;
86            void* cleanupCookie;
87            IBinder::object_cleanup_func func;
88        };
89
90        KeyedVector<const void*, entry_t> mObjects;
91    };
92
93protected:
94    virtual             ~BpBinder();
95    virtual void        onFirstRef();
96    virtual void        onLastStrongRef(const void* id);
97    virtual bool        onIncStrongAttempted(uint32_t flags, const void* id);
98
99private:
100    const   int32_t             mHandle;
101
102    struct Obituary {
103        wp<DeathRecipient> recipient;
104        void* cookie;
105        uint32_t flags;
106    };
107
108            void                reportOneDeath(const Obituary& obit);
109            bool                isDescriptorCached() const;
110
111    mutable Mutex               mLock;
112            volatile int32_t    mAlive;
113            volatile int32_t    mObitsSent;
114            Vector<Obituary>*   mObituaries;
115            ObjectManager       mObjects;
116            Parcel*             mConstantData;
117    mutable String16            mDescriptorCache;
118};
119
120}; // namespace android
121
122// ---------------------------------------------------------------------------
123
124#endif // ANDROID_BPBINDER_H
125