1 /* 2 * Copyright 2020 The gRPC Authors 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 io.grpc.binder.internal; 18 19 import android.os.Binder; 20 import android.os.Parcel; 21 import io.grpc.Internal; 22 import java.util.logging.Level; 23 import java.util.logging.Logger; 24 import javax.annotation.Nullable; 25 26 /** 27 * An extension of {@link Binder} which delegates all transactions to an internal handler, and only 28 * supports one way transactions. 29 * 30 * <p>Since Binder objects can be anchored forever by a misbehaved remote process, any references 31 * they hold can lead to significant memory leaks. To prevent that, the {@link #detach} method 32 * clears the reference to the delegate handler, ensuring that only this simple class is leaked. 33 * Once detached, this binder returns false from any future transactions (and pings). 34 * 35 * <p>Since two-way transactions block the calling thread on a remote process, this class only 36 * supports one-way calls. 37 */ 38 @Internal 39 public final class LeakSafeOneWayBinder extends Binder { 40 41 private static final Logger logger = Logger.getLogger(LeakSafeOneWayBinder.class.getName()); 42 43 @Internal 44 public interface TransactionHandler { handleTransaction(int code, Parcel data)45 boolean handleTransaction(int code, Parcel data); 46 } 47 48 @Nullable private TransactionHandler handler; 49 LeakSafeOneWayBinder(TransactionHandler handler)50 public LeakSafeOneWayBinder(TransactionHandler handler) { 51 this.handler = handler; 52 } 53 detach()54 public void detach() { 55 handler = null; 56 } 57 58 @Override onTransact(int code, Parcel parcel, Parcel reply, int flags)59 protected boolean onTransact(int code, Parcel parcel, Parcel reply, int flags) { 60 TransactionHandler handler = this.handler; 61 if (handler != null) { 62 try { 63 return handler.handleTransaction(code, parcel); 64 } catch (RuntimeException re) { 65 logger.log(Level.WARNING, "failure sending transaction " + code, re); 66 return false; 67 } 68 } else { 69 return false; 70 } 71 } 72 73 @Override pingBinder()74 public boolean pingBinder() { 75 return handler != null; 76 } 77 } 78