| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.net.test; | 5 package org.chromium.net.test; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.os.Build; | 8 import android.os.Build; |
| 9 import android.os.Handler; | 9 import android.os.Handler; |
| 10 import android.os.HandlerThread; | 10 import android.os.HandlerThread; |
| 11 | 11 |
| 12 import org.chromium.base.ContextUtils; | 12 import org.chromium.base.ContextUtils; |
| 13 import org.chromium.base.Log; | 13 import org.chromium.base.Log; |
| 14 import org.chromium.base.annotations.CalledByNative; | 14 import org.chromium.base.annotations.CalledByNative; |
| 15 import org.chromium.base.annotations.JNINamespace; | 15 import org.chromium.base.annotations.JNINamespace; |
| 16 import org.chromium.base.library_loader.LibraryLoader; | 16 import org.chromium.base.library_loader.LibraryLoader; |
| 17 import org.chromium.base.library_loader.LibraryProcessType; | 17 import org.chromium.base.library_loader.LibraryProcessType; |
| 18 import org.chromium.base.library_loader.ProcessInitException; | 18 import org.chromium.base.library_loader.ProcessInitException; |
| 19 import org.chromium.base.test.util.UrlUtils; | 19 import org.chromium.base.test.util.UrlUtils; |
| 20 | 20 |
| 21 import java.util.concurrent.Callable; | 21 import java.util.concurrent.Callable; |
| 22 import java.util.concurrent.ExecutionException; | 22 import java.util.concurrent.ExecutionException; |
| 23 import java.util.concurrent.FutureTask; | 23 import java.util.concurrent.FutureTask; |
| 24 import java.util.concurrent.atomic.AtomicInteger; | 24 import java.util.concurrent.atomic.AtomicInteger; |
| 25 | 25 |
| 26 /** | 26 /** |
| 27 * Java bindings for running a net::test_server::EmbeddedTestServer. | 27 * Java bindings for running a net::test_server::EmbeddedTestServer. |
| 28 * | 28 * |
| 29 * This should not be used directly. Use {@link EmbeddedTestServer} instead. | 29 * This should not be used directly. Use {@link AwEmbeddedTestServer} instead. |
| 30 */ | 30 */ |
| 31 @JNINamespace("net::test_server") | 31 @JNINamespace("net::test_server") |
| 32 public class EmbeddedTestServerImpl extends IEmbeddedTestServerImpl.Stub { | 32 public class AwEmbeddedTestServerImpl extends IAwEmbeddedTestServerImpl.Stub { |
| 33 private static final String TAG = "cr_TestServer"; | 33 private static final String TAG = "cr_TestServer"; |
| 34 | 34 |
| 35 private static AtomicInteger sCount = new AtomicInteger(); | 35 private static AtomicInteger sCount = new AtomicInteger(); |
| 36 | 36 |
| 37 private final Context mContext; | 37 private final Context mContext; |
| 38 private Handler mHandler; | 38 private Handler mHandler; |
| 39 private HandlerThread mHandlerThread; | 39 private HandlerThread mHandlerThread; |
| 40 private long mNativeEmbeddedTestServer; | 40 private long mNativeAwEmbeddedTestServer; |
| 41 | 41 |
| 42 /** Create an uninitialized EmbeddedTestServer. */ | 42 /** Create an uninitialized EmbeddedTestServer. */ |
| 43 public EmbeddedTestServerImpl(Context context) { | 43 public AwEmbeddedTestServerImpl(Context context) { |
| 44 mContext = context; | 44 mContext = context; |
| 45 } | 45 } |
| 46 | 46 |
| 47 private <V> V runOnHandlerThread(Callable<V> c) { | 47 private <V> V runOnHandlerThread(Callable<V> c) { |
| 48 FutureTask<V> t = new FutureTask<>(c); | 48 FutureTask<V> t = new FutureTask<>(c); |
| 49 mHandler.post(t); | 49 mHandler.post(t); |
| 50 try { | 50 try { |
| 51 return t.get(); | 51 return t.get(); |
| 52 } catch (ExecutionException e) { | 52 } catch (ExecutionException e) { |
| 53 Log.e(TAG, "Exception raised from native EmbeddedTestServer", e); | 53 Log.e(TAG, "Exception raised from native EmbeddedTestServer", e); |
| 54 } catch (InterruptedException e) { | 54 } catch (InterruptedException e) { |
| 55 Log.e(TAG, "Interrupted while waiting for native EmbeddedTestServer"
, e); | 55 Log.e(TAG, "Interrupted while waiting for native EmbeddedTestServer"
, e); |
| 56 } | 56 } |
| 57 return null; | 57 return null; |
| 58 } | 58 } |
| 59 | 59 |
| 60 /** Initialize the native EmbeddedTestServer object. | 60 /** Initialize the native EmbeddedTestServer object. |
| 61 * | 61 * |
| 62 * @return Whether the native object was successfully initialized. | 62 * @return Whether the native object was successfully initialized. |
| 63 */ | 63 */ |
| 64 @Override | 64 @Override |
| 65 public boolean initializeNative() { | 65 public boolean initializeNative() { |
| 66 // This is necessary as EmbeddedTestServerImpl is in a different process
than the tests | 66 // This is necessary as AwEmbeddedTestServerImpl is in a different proce
ss than the tests |
| 67 // using it, so it needs to initialize its own application context. | 67 // using it, so it needs to initialize its own application context. |
| 68 ContextUtils.initApplicationContext(mContext.getApplicationContext()); | 68 ContextUtils.initApplicationContext(mContext.getApplicationContext()); |
| 69 try { | 69 try { |
| 70 LibraryLoader.get(LibraryProcessType.PROCESS_BROWSER).ensureInitiali
zed(); | 70 LibraryLoader.get(LibraryProcessType.PROCESS_BROWSER).ensureInitiali
zed(); |
| 71 } catch (ProcessInitException e) { | 71 } catch (ProcessInitException e) { |
| 72 Log.e(TAG, "Failed to load native libraries.", e); | 72 Log.e(TAG, "Failed to load native libraries.", e); |
| 73 return false; | 73 return false; |
| 74 } | 74 } |
| 75 | 75 |
| 76 mHandlerThread = new HandlerThread("EmbeddedTestServer" + sCount.getAndI
ncrement()); | 76 mHandlerThread = new HandlerThread("EmbeddedTestServer" + sCount.getAndI
ncrement()); |
| 77 mHandlerThread.start(); | 77 mHandlerThread.start(); |
| 78 mHandler = new Handler(mHandlerThread.getLooper()); | 78 mHandler = new Handler(mHandlerThread.getLooper()); |
| 79 | 79 |
| 80 runOnHandlerThread(new Callable<Void>() { | 80 runOnHandlerThread(new Callable<Void>() { |
| 81 @Override | 81 @Override |
| 82 public Void call() { | 82 public Void call() { |
| 83 if (mNativeEmbeddedTestServer == 0) nativeInit(UrlUtils.getIsola
tedTestRoot()); | 83 if (mNativeAwEmbeddedTestServer == 0) nativeInit(UrlUtils.getIso
latedTestRoot()); |
| 84 assert mNativeEmbeddedTestServer != 0; | 84 assert mNativeAwEmbeddedTestServer != 0; |
| 85 return null; | 85 return null; |
| 86 } | 86 } |
| 87 }); | 87 }); |
| 88 return true; | 88 return true; |
| 89 } | 89 } |
| 90 | 90 |
| 91 /** Starts the server. | 91 /** Starts the server. |
| 92 * | 92 * |
| 93 * Note that this should be called after handlers are set up, including any
relevant calls | 93 * Note that this should be called after handlers are set up, including any
relevant calls |
| 94 * serveFilesFromDirectory. | 94 * serveFilesFromDirectory. |
| 95 * | 95 * |
| 96 * @return Whether the server was successfully started. | 96 * @return Whether the server was successfully started. |
| 97 */ | 97 */ |
| 98 @Override | 98 @Override |
| 99 public boolean start() { | 99 public boolean start() { |
| 100 return runOnHandlerThread(new Callable<Boolean>() { | 100 return runOnHandlerThread(new Callable<Boolean>() { |
| 101 @Override | 101 @Override |
| 102 public Boolean call() { | 102 public Boolean call() { |
| 103 return nativeStart(mNativeEmbeddedTestServer); | 103 return nativeStart(mNativeAwEmbeddedTestServer); |
| 104 } | 104 } |
| 105 }); | 105 }); |
| 106 } | 106 } |
| 107 | 107 |
| 108 /** Add the default handlers and serve files from the provided directory rel
ative to the | 108 /** Add the default handlers and serve files from the provided directory rel
ative to the |
| 109 * external storage directory. | 109 * external storage directory. |
| 110 * | 110 * |
| 111 * @param directoryPath The path of the directory from which files should b
e served, relative | 111 * @param directoryPath The path of the directory from which files should b
e served, relative |
| 112 * to the external storage directory. | 112 * to the external storage directory. |
| 113 */ | 113 */ |
| 114 @Override | 114 @Override |
| 115 public void addDefaultHandlers(final String directoryPath) { | 115 public void addDefaultHandlers(final String directoryPath) { |
| 116 runOnHandlerThread(new Callable<Void>() { | 116 runOnHandlerThread(new Callable<Void>() { |
| 117 @Override | 117 @Override |
| 118 public Void call() { | 118 public Void call() { |
| 119 nativeAddDefaultHandlers(mNativeEmbeddedTestServer, directoryPat
h); | 119 nativeAddDefaultHandlers(mNativeAwEmbeddedTestServer, directoryP
ath); |
| 120 return null; | 120 return null; |
| 121 } | 121 } |
| 122 }); | 122 }); |
| 123 } |
| 124 |
| 125 @Override |
| 126 public long[] getCustomHandlers() { |
| 127 return runOnHandlerThread(new Callable<long[]>() { |
| 128 @Override |
| 129 public long[] call() { |
| 130 return nativeGetCustomHandlers(mNativeAwEmbeddedTestServer); |
| 131 } |
| 132 }); |
| 133 } |
| 134 |
| 135 /** Register multiple request handlers. |
| 136 * Handlers must be registered before starting the server. |
| 137 * |
| 138 * @param handlers The pointers of handlers to be registered. |
| 139 */ |
| 140 @Override |
| 141 public void registerRequestHandlers(final long[] handlers) { |
| 142 runOnHandlerThread(new Callable<Void>() { |
| 143 @Override |
| 144 public Void call() { |
| 145 for (int i = 0; i < handlers.length; i++) { |
| 146 nativeRegisterRequestHandler(mNativeAwEmbeddedTestServer, ha
ndlers[i]); |
| 147 } |
| 148 return null; |
| 149 } |
| 150 }); |
| 123 } | 151 } |
| 124 | 152 |
| 125 /** Serve files from the provided directory. | 153 /** Serve files from the provided directory. |
| 126 * | 154 * |
| 127 * @param directoryPath The path of the directory from which files should b
e served. | 155 * @param directoryPath The path of the directory from which files should b
e served. |
| 128 */ | 156 */ |
| 129 @Override | 157 @Override |
| 130 public void serveFilesFromDirectory(final String directoryPath) { | 158 public void serveFilesFromDirectory(final String directoryPath) { |
| 131 runOnHandlerThread(new Callable<Void>() { | 159 runOnHandlerThread(new Callable<Void>() { |
| 132 @Override | 160 @Override |
| 133 public Void call() { | 161 public Void call() { |
| 134 nativeServeFilesFromDirectory(mNativeEmbeddedTestServer, directo
ryPath); | 162 nativeServeFilesFromDirectory(mNativeAwEmbeddedTestServer, direc
toryPath); |
| 135 return null; | 163 return null; |
| 136 } | 164 } |
| 137 }); | 165 }); |
| 138 } | 166 } |
| 139 | 167 |
| 140 /** Get the full URL for the given relative URL. | 168 /** Get the full URL for the given relative URL. |
| 141 * | 169 * |
| 142 * @param relativeUrl The relative URL for which a full URL should be retur
ned. | 170 * @param relativeUrl The relative URL for which a full URL should be retur
ned. |
| 143 * @return The URL as a String. | 171 * @return The URL as a String. |
| 144 */ | 172 */ |
| 145 @Override | 173 @Override |
| 146 public String getURL(final String relativeUrl) { | 174 public String getURL(final String relativeUrl) { |
| 147 return runOnHandlerThread(new Callable<String>() { | 175 return runOnHandlerThread(new Callable<String>() { |
| 148 @Override | 176 @Override |
| 149 public String call() { | 177 public String call() { |
| 150 return nativeGetURL(mNativeEmbeddedTestServer, relativeUrl); | 178 return nativeGetURL(mNativeAwEmbeddedTestServer, relativeUrl); |
| 151 } | 179 } |
| 152 }); | 180 }); |
| 153 } | 181 } |
| 154 | 182 |
| 155 /** Shut down the server. | 183 /** Shut down the server. |
| 156 * | 184 * |
| 157 * @return Whether the server was successfully shut down. | 185 * @return Whether the server was successfully shut down. |
| 158 */ | 186 */ |
| 159 @Override | 187 @Override |
| 160 public boolean shutdownAndWaitUntilComplete() { | 188 public boolean shutdownAndWaitUntilComplete() { |
| 161 return runOnHandlerThread(new Callable<Boolean>() { | 189 return runOnHandlerThread(new Callable<Boolean>() { |
| 162 @Override | 190 @Override |
| 163 public Boolean call() { | 191 public Boolean call() { |
| 164 return nativeShutdownAndWaitUntilComplete(mNativeEmbeddedTestSer
ver); | 192 return nativeShutdownAndWaitUntilComplete(mNativeAwEmbeddedTestS
erver); |
| 165 } | 193 } |
| 166 }); | 194 }); |
| 167 } | 195 } |
| 168 | 196 |
| 169 /** Destroy the native EmbeddedTestServer object. */ | 197 /** Destroy the native EmbeddedTestServer object. */ |
| 170 @Override | 198 @Override |
| 171 public void destroy() { | 199 public void destroy() { |
| 172 runOnHandlerThread(new Callable<Void>() { | 200 runOnHandlerThread(new Callable<Void>() { |
| 173 @Override | 201 @Override |
| 174 public Void call() { | 202 public Void call() { |
| 175 assert mNativeEmbeddedTestServer != 0; | 203 assert mNativeAwEmbeddedTestServer != 0; |
| 176 nativeDestroy(mNativeEmbeddedTestServer); | 204 nativeDestroy(mNativeAwEmbeddedTestServer); |
| 177 assert mNativeEmbeddedTestServer == 0; | 205 assert mNativeAwEmbeddedTestServer == 0; |
| 178 return null; | 206 return null; |
| 179 } | 207 } |
| 180 }); | 208 }); |
| 181 | 209 |
| 182 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { | 210 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { |
| 183 mHandlerThread.quitSafely(); | 211 mHandlerThread.quitSafely(); |
| 184 } else { | 212 } else { |
| 185 runOnHandlerThread(new Callable<Void>() { | 213 runOnHandlerThread(new Callable<Void>() { |
| 186 @Override | 214 @Override |
| 187 public Void call() { | 215 public Void call() { |
| 188 mHandlerThread.quit(); | 216 mHandlerThread.quit(); |
| 189 return null; | 217 return null; |
| 190 } | 218 } |
| 191 }); | 219 }); |
| 192 } | 220 } |
| 193 | 221 |
| 194 try { | 222 try { |
| 195 mHandlerThread.join(); | 223 mHandlerThread.join(); |
| 196 } catch (InterruptedException e) { | 224 } catch (InterruptedException e) { |
| 197 } | 225 } |
| 198 } | 226 } |
| 199 | 227 |
| 200 @CalledByNative | 228 @CalledByNative |
| 201 private void setNativePtr(long nativePtr) { | 229 private void setNativePtr(long nativePtr) { |
| 202 assert mNativeEmbeddedTestServer == 0; | 230 assert mNativeAwEmbeddedTestServer == 0; |
| 203 mNativeEmbeddedTestServer = nativePtr; | 231 mNativeAwEmbeddedTestServer = nativePtr; |
| 204 } | 232 } |
| 205 | 233 |
| 206 @CalledByNative | 234 @CalledByNative |
| 207 private void clearNativePtr() { | 235 private void clearNativePtr() { |
| 208 assert mNativeEmbeddedTestServer != 0; | 236 assert mNativeAwEmbeddedTestServer != 0; |
| 209 mNativeEmbeddedTestServer = 0; | 237 mNativeAwEmbeddedTestServer = 0; |
| 210 } | 238 } |
| 211 | 239 |
| 212 private native void nativeInit(String testDataDir); | 240 private native void nativeInit(String testDataDir); |
| 213 private native void nativeDestroy(long nativeEmbeddedTestServerAndroid); | 241 private native void nativeDestroy(long nativeAwEmbeddedTestServerAndroid); |
| 214 private native boolean nativeStart(long nativeEmbeddedTestServerAndroid); | 242 private native boolean nativeStart(long nativeAwEmbeddedTestServerAndroid); |
| 215 private native boolean nativeShutdownAndWaitUntilComplete(long nativeEmbedde
dTestServerAndroid); | 243 private native boolean nativeShutdownAndWaitUntilComplete( |
| 216 private native String nativeGetURL(long nativeEmbeddedTestServerAndroid, Str
ing relativeUrl); | 244 long nativeAwEmbeddedTestServerAndroid); |
| 245 private native String nativeGetURL(long nativeAwEmbeddedTestServerAndroid, S
tring relativeUrl); |
| 217 private native void nativeAddDefaultHandlers( | 246 private native void nativeAddDefaultHandlers( |
| 218 long nativeEmbeddedTestServerAndroid, String directoryPath); | 247 long nativeAwEmbeddedTestServerAndroid, String directoryPath); |
| 248 private native long[] nativeGetCustomHandlers(long nativeAwEmbeddedTestServe
rAndroid); |
| 249 private native void nativeRegisterRequestHandler( |
| 250 long nativeAwEmbeddedTestServerAndroid, long handler); |
| 219 private native void nativeServeFilesFromDirectory( | 251 private native void nativeServeFilesFromDirectory( |
| 220 long nativeEmbeddedTestServerAndroid, String directoryPath); | 252 long nativeAwEmbeddedTestServerAndroid, String directoryPath); |
| 221 } | 253 } |
| OLD | NEW |