1 // Copyright 2016 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 org.chromium.base.annotations.CalledByNative; 8 import org.chromium.base.annotations.JNINamespace; 9 import org.chromium.mojo.system.Core; 10 import org.chromium.mojo.system.Handle; 11 import org.chromium.mojo.system.MojoResult; 12 import org.chromium.mojo.system.Watcher; 13 14 @JNINamespace("mojo::android") 15 class WatcherImpl implements Watcher { 16 private long mImplPtr = nativeCreateWatcher(); 17 private Callback mCallback; 18 19 @Override start(Handle handle, Core.HandleSignals signals, Callback callback)20 public int start(Handle handle, Core.HandleSignals signals, Callback callback) { 21 if (mImplPtr == 0) { 22 return MojoResult.INVALID_ARGUMENT; 23 } 24 if (!(handle instanceof HandleBase)) { 25 return MojoResult.INVALID_ARGUMENT; 26 } 27 int result = 28 nativeStart(mImplPtr, ((HandleBase) handle).getMojoHandle(), signals.getFlags()); 29 if (result == MojoResult.OK) mCallback = callback; 30 return result; 31 } 32 33 @Override cancel()34 public void cancel() { 35 if (mImplPtr == 0) { 36 return; 37 } 38 mCallback = null; 39 nativeCancel(mImplPtr); 40 } 41 42 @Override destroy()43 public void destroy() { 44 if (mImplPtr == 0) { 45 return; 46 } 47 nativeDelete(mImplPtr); 48 mImplPtr = 0; 49 } 50 51 @CalledByNative onHandleReady(int result)52 private void onHandleReady(int result) { 53 mCallback.onResult(result); 54 } 55 nativeCreateWatcher()56 private native long nativeCreateWatcher(); 57 nativeStart(long implPtr, int mojoHandle, int flags)58 private native int nativeStart(long implPtr, int mojoHandle, int flags); 59 nativeCancel(long implPtr)60 private native void nativeCancel(long implPtr); 61 nativeDelete(long implPtr)62 private native void nativeDelete(long implPtr); 63 } 64