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; 6 7 import org.chromium.mojo.system.Core; 8 import org.chromium.mojo.system.Core.WaitResult; 9 import org.chromium.mojo.system.DataPipe; 10 import org.chromium.mojo.system.DataPipe.ConsumerHandle; 11 import org.chromium.mojo.system.DataPipe.ProducerHandle; 12 import org.chromium.mojo.system.Handle; 13 import org.chromium.mojo.system.MessagePipeHandle; 14 import org.chromium.mojo.system.MojoResult; 15 import org.chromium.mojo.system.ResultAnd; 16 import org.chromium.mojo.system.SharedBufferHandle; 17 import org.chromium.mojo.system.UntypedHandle; 18 import org.chromium.mojo.system.impl.CoreImpl; 19 20 import java.nio.ByteBuffer; 21 import java.util.List; 22 23 /** 24 * A mock handle, that does nothing. 25 */ 26 public class HandleMock implements UntypedHandle, MessagePipeHandle, 27 ProducerHandle, ConsumerHandle, SharedBufferHandle { 28 29 /** 30 * @see Handle#close() 31 */ 32 @Override close()33 public void close() { 34 // Do nothing. 35 } 36 37 /** 38 * @see Handle#wait(Core.HandleSignals, long) 39 */ 40 @Override wait(Core.HandleSignals signals, long deadline)41 public WaitResult wait(Core.HandleSignals signals, long deadline) { 42 // Do nothing. 43 WaitResult result = new WaitResult(); 44 result.setMojoResult(MojoResult.OK); 45 return result; 46 } 47 48 /** 49 * @see Handle#isValid() 50 */ 51 @Override isValid()52 public boolean isValid() { 53 return true; 54 } 55 56 /** 57 * @see Handle#toUntypedHandle() 58 */ 59 @Override toUntypedHandle()60 public UntypedHandle toUntypedHandle() { 61 return this; 62 } 63 64 /** 65 * @see org.chromium.mojo.system.Handle#getCore() 66 */ 67 @Override getCore()68 public Core getCore() { 69 return CoreImpl.getInstance(); 70 } 71 72 /** 73 * @see org.chromium.mojo.system.UntypedHandle#pass() 74 */ 75 @Override pass()76 public HandleMock pass() { 77 return this; 78 } 79 80 /** 81 * @see Handle#releaseNativeHandle() 82 */ 83 @Override releaseNativeHandle()84 public int releaseNativeHandle() { 85 return 0; 86 } 87 88 /** 89 * @see ConsumerHandle#discardData(int, DataPipe.ReadFlags) 90 */ 91 @Override discardData(int numBytes, DataPipe.ReadFlags flags)92 public int discardData(int numBytes, DataPipe.ReadFlags flags) { 93 // Do nothing. 94 return 0; 95 } 96 97 /** 98 * @see ConsumerHandle#readData(java.nio.ByteBuffer, DataPipe.ReadFlags) 99 */ 100 @Override readData(ByteBuffer elements, DataPipe.ReadFlags flags)101 public ResultAnd<Integer> readData(ByteBuffer elements, DataPipe.ReadFlags flags) { 102 // Do nothing. 103 return new ResultAnd<Integer>(MojoResult.OK, 0); 104 } 105 106 /** 107 * @see ConsumerHandle#beginReadData(int, DataPipe.ReadFlags) 108 */ 109 @Override beginReadData(int numBytes, DataPipe.ReadFlags flags)110 public ByteBuffer beginReadData(int numBytes, 111 DataPipe.ReadFlags flags) { 112 // Do nothing. 113 return null; 114 } 115 116 /** 117 * @see ConsumerHandle#endReadData(int) 118 */ 119 @Override endReadData(int numBytesRead)120 public void endReadData(int numBytesRead) { 121 // Do nothing. 122 } 123 124 /** 125 * @see ProducerHandle#writeData(java.nio.ByteBuffer, DataPipe.WriteFlags) 126 */ 127 @Override writeData(ByteBuffer elements, DataPipe.WriteFlags flags)128 public ResultAnd<Integer> writeData(ByteBuffer elements, DataPipe.WriteFlags flags) { 129 // Do nothing. 130 return new ResultAnd<Integer>(MojoResult.OK, 0); 131 } 132 133 /** 134 * @see ProducerHandle#beginWriteData(int, DataPipe.WriteFlags) 135 */ 136 @Override beginWriteData(int numBytes, DataPipe.WriteFlags flags)137 public ByteBuffer beginWriteData(int numBytes, 138 DataPipe.WriteFlags flags) { 139 // Do nothing. 140 return null; 141 } 142 143 /** 144 * @see ProducerHandle#endWriteData(int) 145 */ 146 @Override endWriteData(int numBytesWritten)147 public void endWriteData(int numBytesWritten) { 148 // Do nothing. 149 } 150 151 /** 152 * @see MessagePipeHandle#writeMessage(java.nio.ByteBuffer, java.util.List, 153 * MessagePipeHandle.WriteFlags) 154 */ 155 @Override writeMessage(ByteBuffer bytes, List<? extends Handle> handles, WriteFlags flags)156 public void writeMessage(ByteBuffer bytes, List<? extends Handle> handles, 157 WriteFlags flags) { 158 // Do nothing. 159 } 160 161 /** 162 * @see MessagePipeHandle#readMessage(java.nio.ByteBuffer, int, MessagePipeHandle.ReadFlags) 163 */ 164 @Override readMessage( ByteBuffer bytes, int maxNumberOfHandles, ReadFlags flags)165 public ResultAnd<ReadMessageResult> readMessage( 166 ByteBuffer bytes, int maxNumberOfHandles, ReadFlags flags) { 167 // Do nothing. 168 return new ResultAnd<ReadMessageResult>(MojoResult.OK, new ReadMessageResult()); 169 } 170 171 /** 172 * @see UntypedHandle#toMessagePipeHandle() 173 */ 174 @Override toMessagePipeHandle()175 public MessagePipeHandle toMessagePipeHandle() { 176 return this; 177 } 178 179 /** 180 * @see UntypedHandle#toDataPipeConsumerHandle() 181 */ 182 @Override toDataPipeConsumerHandle()183 public ConsumerHandle toDataPipeConsumerHandle() { 184 return this; 185 } 186 187 /** 188 * @see UntypedHandle#toDataPipeProducerHandle() 189 */ 190 @Override toDataPipeProducerHandle()191 public ProducerHandle toDataPipeProducerHandle() { 192 return this; 193 } 194 195 /** 196 * @see UntypedHandle#toSharedBufferHandle() 197 */ 198 @Override toSharedBufferHandle()199 public SharedBufferHandle toSharedBufferHandle() { 200 return this; 201 } 202 203 /** 204 * @see SharedBufferHandle#duplicate(SharedBufferHandle.DuplicateOptions) 205 */ 206 @Override duplicate(DuplicateOptions options)207 public SharedBufferHandle duplicate(DuplicateOptions options) { 208 // Do nothing. 209 return null; 210 } 211 212 /** 213 * @see SharedBufferHandle#map(long, long, SharedBufferHandle.MapFlags) 214 */ 215 @Override map(long offset, long numBytes, MapFlags flags)216 public ByteBuffer map(long offset, long numBytes, MapFlags flags) { 217 // Do nothing. 218 return null; 219 } 220 221 /** 222 * @see SharedBufferHandle#unmap(java.nio.ByteBuffer) 223 */ 224 @Override unmap(ByteBuffer buffer)225 public void unmap(ByteBuffer buffer) { 226 // Do nothing. 227 } 228 229 } 230