1 /* 2 * Copyright (C) 2006 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 package android.os; 18 19 20 /** 21 * Native implementation of the service manager. Most clients will only 22 * care about getDefault() and possibly asInterface(). 23 * @hide 24 */ 25 public abstract class ServiceManagerNative extends Binder implements IServiceManager 26 { 27 /** 28 * Cast a Binder object into a service manager interface, generating 29 * a proxy if needed. 30 */ 31 static public IServiceManager asInterface(IBinder obj) 32 { 33 if (obj == null) { 34 return null; 35 } 36 IServiceManager in = 37 (IServiceManager)obj.queryLocalInterface(descriptor); 38 if (in != null) { 39 return in; 40 } 41 42 return new ServiceManagerProxy(obj); 43 } 44 45 public ServiceManagerNative() 46 { 47 attachInterface(this, descriptor); 48 } 49 50 public boolean onTransact(int code, Parcel data, Parcel reply, int flags) 51 { 52 try { 53 switch (code) { 54 case IServiceManager.GET_SERVICE_TRANSACTION: { 55 data.enforceInterface(IServiceManager.descriptor); 56 String name = data.readString(); 57 IBinder service = getService(name); 58 reply.writeStrongBinder(service); 59 return true; 60 } 61 62 case IServiceManager.CHECK_SERVICE_TRANSACTION: { 63 data.enforceInterface(IServiceManager.descriptor); 64 String name = data.readString(); 65 IBinder service = checkService(name); 66 reply.writeStrongBinder(service); 67 return true; 68 } 69 70 case IServiceManager.ADD_SERVICE_TRANSACTION: { 71 data.enforceInterface(IServiceManager.descriptor); 72 String name = data.readString(); 73 IBinder service = data.readStrongBinder(); 74 addService(name, service); 75 return true; 76 } 77 78 case IServiceManager.LIST_SERVICES_TRANSACTION: { 79 data.enforceInterface(IServiceManager.descriptor); 80 String[] list = listServices(); 81 reply.writeStringArray(list); 82 return true; 83 } 84 85 case IServiceManager.SET_PERMISSION_CONTROLLER_TRANSACTION: { 86 data.enforceInterface(IServiceManager.descriptor); 87 IPermissionController controller 88 = IPermissionController.Stub.asInterface( 89 data.readStrongBinder()); 90 setPermissionController(controller); 91 return true; 92 } 93 } 94 } catch (RemoteException e) { 95 } 96 97 return false; 98 } 99 100 public IBinder asBinder() 101 { 102 return this; 103 } 104 } 105 106 class ServiceManagerProxy implements IServiceManager { 107 public ServiceManagerProxy(IBinder remote) { 108 mRemote = remote; 109 } 110 111 public IBinder asBinder() { 112 return mRemote; 113 } 114 115 public IBinder getService(String name) throws RemoteException { 116 Parcel data = Parcel.obtain(); 117 Parcel reply = Parcel.obtain(); 118 data.writeInterfaceToken(IServiceManager.descriptor); 119 data.writeString(name); 120 mRemote.transact(GET_SERVICE_TRANSACTION, data, reply, 0); 121 IBinder binder = reply.readStrongBinder(); 122 reply.recycle(); 123 data.recycle(); 124 return binder; 125 } 126 127 public IBinder checkService(String name) throws RemoteException { 128 Parcel data = Parcel.obtain(); 129 Parcel reply = Parcel.obtain(); 130 data.writeInterfaceToken(IServiceManager.descriptor); 131 data.writeString(name); 132 mRemote.transact(CHECK_SERVICE_TRANSACTION, data, reply, 0); 133 IBinder binder = reply.readStrongBinder(); 134 reply.recycle(); 135 data.recycle(); 136 return binder; 137 } 138 139 public void addService(String name, IBinder service) 140 throws RemoteException { 141 Parcel data = Parcel.obtain(); 142 Parcel reply = Parcel.obtain(); 143 data.writeInterfaceToken(IServiceManager.descriptor); 144 data.writeString(name); 145 data.writeStrongBinder(service); 146 mRemote.transact(ADD_SERVICE_TRANSACTION, data, reply, 0); 147 reply.recycle(); 148 data.recycle(); 149 } 150 151 public String[] listServices() throws RemoteException { 152 Parcel data = Parcel.obtain(); 153 Parcel reply = Parcel.obtain(); 154 data.writeInterfaceToken(IServiceManager.descriptor); 155 mRemote.transact(LIST_SERVICES_TRANSACTION, data, reply, 0); 156 String[] list = reply.readStringArray(); 157 reply.recycle(); 158 data.recycle(); 159 return list; 160 } 161 162 public void setPermissionController(IPermissionController controller) 163 throws RemoteException { 164 Parcel data = Parcel.obtain(); 165 Parcel reply = Parcel.obtain(); 166 data.writeInterfaceToken(IServiceManager.descriptor); 167 data.writeStrongBinder(controller.asBinder()); 168 mRemote.transact(SET_PERMISSION_CONTROLLER_TRANSACTION, data, reply, 0); 169 reply.recycle(); 170 data.recycle(); 171 } 172 173 private IBinder mRemote; 174 } 175