1 /*
2  * Copyright (C) 2017 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 #include <binder/IUidObserver.h>
18 
19 #include <binder/Parcel.h>
20 
21 namespace android {
22 
23 // ------------------------------------------------------------------------------------
24 
25 class BpUidObserver : public BpInterface<IUidObserver>
26 {
27 public:
BpUidObserver(const sp<IBinder> & impl)28     explicit BpUidObserver(const sp<IBinder>& impl)
29         : BpInterface<IUidObserver>(impl)
30     {
31     }
32 
onUidGone(uid_t uid,bool disabled)33     virtual void onUidGone(uid_t uid, bool disabled)
34     {
35         Parcel data, reply;
36         data.writeInterfaceToken(IUidObserver::getInterfaceDescriptor());
37         data.writeInt32((int32_t) uid);
38         data.writeInt32(disabled ? 1 : 0);
39         remote()->transact(ON_UID_GONE_TRANSACTION, data, &reply, IBinder::FLAG_ONEWAY);
40     }
41 
onUidActive(uid_t uid)42     virtual void onUidActive(uid_t uid)
43     {
44         Parcel data, reply;
45         data.writeInterfaceToken(IUidObserver::getInterfaceDescriptor());
46         data.writeInt32((int32_t) uid);
47         remote()->transact(ON_UID_ACTIVE_TRANSACTION, data, &reply, IBinder::FLAG_ONEWAY);
48     }
49 
onUidIdle(uid_t uid,bool disabled)50     virtual void onUidIdle(uid_t uid, bool disabled)
51     {
52         Parcel data, reply;
53         data.writeInterfaceToken(IUidObserver::getInterfaceDescriptor());
54         data.writeInt32((int32_t) uid);
55         data.writeInt32(disabled ? 1 : 0);
56         remote()->transact(ON_UID_IDLE_TRANSACTION, data, &reply, IBinder::FLAG_ONEWAY);
57     }
58 
onUidStateChanged(uid_t uid,int32_t procState,int64_t procStateSeq,int32_t capability)59     virtual void onUidStateChanged(uid_t uid, int32_t procState, int64_t procStateSeq,
60                                    int32_t capability) {
61         Parcel data, reply;
62         data.writeInterfaceToken(IUidObserver::getInterfaceDescriptor());
63         data.writeInt32((int32_t) uid);
64         data.writeInt32(procState);
65         data.writeInt64(procStateSeq);
66         data.writeInt32(capability);
67         remote()->transact(ON_UID_STATE_CHANGED_TRANSACTION, data, &reply, IBinder::FLAG_ONEWAY);
68     }
69 
onUidProcAdjChanged(uid_t uid,int32_t adj)70     virtual void onUidProcAdjChanged(uid_t uid, int32_t adj) {
71         Parcel data, reply;
72         data.writeInt32((int32_t)uid);
73         data.writeInt32((int32_t)adj);
74         remote()->transact(ON_UID_PROC_ADJ_CHANGED_TRANSACTION, data, &reply, IBinder::FLAG_ONEWAY);
75     }
76 };
77 
78 // ----------------------------------------------------------------------
79 
80 IMPLEMENT_META_INTERFACE(UidObserver, "android.app.IUidObserver")
81 
82 // ----------------------------------------------------------------------
83 
84 // NOLINTNEXTLINE(google-default-arguments)
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)85 status_t BnUidObserver::onTransact(
86     uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
87 {
88     switch(code) {
89         case ON_UID_GONE_TRANSACTION: {
90             CHECK_INTERFACE(IUidObserver, data, reply);
91             uid_t uid = data.readInt32();
92             bool disabled = data.readInt32() == 1;
93             onUidGone(uid, disabled);
94             return NO_ERROR;
95         } break;
96 
97         case ON_UID_ACTIVE_TRANSACTION: {
98             CHECK_INTERFACE(IUidObserver, data, reply);
99             uid_t uid = data.readInt32();
100             onUidActive(uid);
101             return NO_ERROR;
102         } break;
103 
104         case ON_UID_IDLE_TRANSACTION: {
105             CHECK_INTERFACE(IUidObserver, data, reply);
106             uid_t uid = data.readInt32();
107             bool disabled = data.readInt32() == 1;
108             onUidIdle(uid, disabled);
109             return NO_ERROR;
110         } break;
111 
112         case ON_UID_STATE_CHANGED_TRANSACTION: {
113             CHECK_INTERFACE(IUidObserver, data, reply);
114             uid_t uid = data.readInt32();
115             int32_t procState = data.readInt32();
116             int64_t procStateSeq = data.readInt64();
117             int32_t capability = data.readInt32();
118             onUidStateChanged(uid, procState, procStateSeq, capability);
119             return NO_ERROR;
120         } break;
121 
122         case ON_UID_PROC_ADJ_CHANGED_TRANSACTION: {
123             CHECK_INTERFACE(IUidObserver, data, reply);
124             uid_t uid = data.readInt32();
125             int32_t adj = data.readInt32();
126             onUidProcAdjChanged(uid, adj);
127             return NO_ERROR;
128         } break;
129 
130         default:
131             return BBinder::onTransact(code, data, reply, flags);
132     }
133 }
134 
135 } // namespace android
136