1 // Copyright 2014 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package org.chromium.mojo.system.impl; 6 7 import android.util.Log; 8 9 import org.chromium.mojo.system.Core; 10 import org.chromium.mojo.system.Core.WaitFlags; 11 import org.chromium.mojo.system.Handle; 12 import org.chromium.mojo.system.UntypedHandle; 13 14 /** 15 * Implementation of {@link Handle}. 16 */ 17 abstract class HandleBase implements Handle { 18 19 private static final String TAG = "HandleImpl"; 20 21 /** 22 * The pointer to the scoped handle owned by this object. 23 */ 24 private int mMojoHandle; 25 26 /** 27 * The core implementation. Will be used to delegate all behavior. 28 */ 29 protected CoreImpl mCore; 30 31 /** 32 * Base constructor. Takes ownership of the passed handle. 33 */ HandleBase(CoreImpl core, int mojoHandle)34 HandleBase(CoreImpl core, int mojoHandle) { 35 mCore = core; 36 mMojoHandle = mojoHandle; 37 } 38 39 /** 40 * Constructor for transforming {@link HandleBase} into a specific one. It is used to transform 41 * an {@link UntypedHandle} into a typed one, or any handle into an {@link UntypedHandle}. 42 */ HandleBase(HandleBase other)43 protected HandleBase(HandleBase other) { 44 mCore = other.mCore; 45 HandleBase otherAsHandleImpl = other; 46 int mojoHandle = otherAsHandleImpl.mMojoHandle; 47 otherAsHandleImpl.mMojoHandle = CoreImpl.INVALID_HANDLE; 48 mMojoHandle = mojoHandle; 49 } 50 51 /** 52 * @see org.chromium.mojo.system.Handle#close() 53 */ 54 @Override close()55 public void close() { 56 if (mMojoHandle != CoreImpl.INVALID_HANDLE) { 57 // After a close, the handle is invalid whether the close succeed or not. 58 int handle = mMojoHandle; 59 mMojoHandle = CoreImpl.INVALID_HANDLE; 60 mCore.close(handle); 61 } 62 } 63 64 /** 65 * @see org.chromium.mojo.system.Handle#wait(WaitFlags, long) 66 */ 67 @Override wait(WaitFlags flags, long deadline)68 public int wait(WaitFlags flags, long deadline) { 69 return mCore.wait(this, flags, deadline); 70 } 71 72 /** 73 * @see org.chromium.mojo.system.Handle#isValid() 74 */ 75 @Override isValid()76 public boolean isValid() { 77 return mMojoHandle != CoreImpl.INVALID_HANDLE; 78 } 79 80 /** 81 * @see org.chromium.mojo.system.Handle#toUntypedHandle() 82 */ 83 @Override toUntypedHandle()84 public UntypedHandle toUntypedHandle() { 85 return new UntypedHandleImpl(this); 86 } 87 88 /** 89 * @see org.chromium.mojo.system.Handle#getCore() 90 */ 91 @Override getCore()92 public Core getCore() { 93 return mCore; 94 } 95 96 /** 97 * Getter for the native scoped handle. 98 * 99 * @return the native scoped handle. 100 */ getMojoHandle()101 int getMojoHandle() { 102 return mMojoHandle; 103 } 104 105 /** 106 * invalidate the handle. The caller must ensures that the handle does not leak. 107 */ invalidateHandle()108 void invalidateHandle() { 109 mMojoHandle = CoreImpl.INVALID_HANDLE; 110 } 111 112 /** 113 * Close the handle if it is valid. Necessary because we cannot let handle leak, and we cannot 114 * ensure that every handle will be manually closed. 115 * 116 * @see java.lang.Object#finalize() 117 */ 118 @Override finalize()119 protected final void finalize() throws Throwable { 120 if (isValid()) { 121 // This should not happen, as the user of this class should close the handle. Adding a 122 // warning. 123 Log.w(TAG, "Handle was not closed."); 124 // Ignore result at this point. 125 mCore.closeWithResult(mMojoHandle); 126 } 127 super.finalize(); 128 } 129 130 } 131