Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 org.chromium.base.annotations.CalledByNative; | 7 import android.content.ComponentName; |
| 8 import org.chromium.base.annotations.JNINamespace; | 8 import android.content.Context; |
| 9 import android.content.Intent; | |
| 10 import android.content.ServiceConnection; | |
| 11 import android.os.IBinder; | |
| 12 import android.os.RemoteException; | |
| 13 | |
| 14 import org.chromium.base.Log; | |
| 9 | 15 |
| 10 import java.io.File; | 16 import java.io.File; |
| 11 | 17 |
| 12 /** | 18 /** |
| 13 * Java bindings for the native embedded test server. | 19 * Java bindings for the native embedded test server. |
| 14 * | 20 * |
| 15 * An example use: | 21 * An example use: |
| 16 * EmbeddedTestServer s = new EmbeddedTestServer(); | 22 * EmbeddedTestServer s = new EmbeddedTestServer(); |
| 17 * s.initializeNative(); | 23 * s.initializeNative(); |
| 18 * s.serveFilesFromDirectory("/path/to/my/directory"); | 24 * s.serveFilesFromDirectory("/path/to/my/directory"); |
| 19 * if (!s.start()) { | 25 * if (!s.start()) { |
| 20 * throw new SomeKindOfException("Unable to initialize EmbeddedTestServer. "); | 26 * throw new SomeKindOfException("Unable to initialize EmbeddedTestServer. "); |
| 21 * } | 27 * } |
| 22 * | 28 * |
| 23 * // serve requests... | 29 * // serve requests... |
| 24 * | 30 * |
| 25 * s.shutdownAndWait(); | 31 * s.shutdownAndWait(); |
| 26 * s.destroy(); | 32 * s.destroy(); |
| 27 */ | 33 */ |
| 28 @JNINamespace("net::test_server") | |
| 29 public class EmbeddedTestServer { | 34 public class EmbeddedTestServer { |
| 30 private long mNativeEmbeddedTestServer; | 35 private static final String TAG = "cr_net_test"; |
| 31 | 36 |
| 32 /** Create an uninitialized EmbeddedTestServer. */ | 37 private static final String EMBEDDED_TEST_SERVER_SERVICE = |
| 33 public EmbeddedTestServer() {} | 38 "org.chromium.net.test.EMBEDDED_TEST_SERVER_SERVICE"; |
| 34 | 39 |
| 35 /** Initialize the native EmbeddedTestServer object. */ | 40 private IEmbeddedTestServerImpl mImpl; |
| 36 public void initializeNative() { | 41 private ServiceConnection mConn = new ServiceConnection() { |
| 37 if (mNativeEmbeddedTestServer == 0) nativeInit(); | 42 @Override |
| 38 assert mNativeEmbeddedTestServer != 0; | 43 public void onServiceConnected(ComponentName name, IBinder service) { |
| 44 synchronized (mImplMonitor) { | |
| 45 mImpl = IEmbeddedTestServerImpl.Stub.asInterface(service); | |
| 46 mImplMonitor.notify(); | |
| 47 } | |
| 48 } | |
| 49 | |
| 50 @Override | |
| 51 public void onServiceDisconnected(ComponentName name) { | |
| 52 synchronized (mImplMonitor) { | |
| 53 mImpl = null; | |
| 54 mImplMonitor.notify(); | |
| 55 } | |
| 56 } | |
| 57 }; | |
| 58 | |
| 59 private final Object mImplMonitor = new Object(); | |
| 60 | |
| 61 /** | |
| 62 * Exception class raised on failure in the EmbeddedTestServer. | |
| 63 */ | |
| 64 public static final class EmbeddedTestServerFailure extends Error { | |
| 65 public EmbeddedTestServerFailure(String errorDesc) { | |
| 66 super(errorDesc); | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 public void initializeNative(Context context) throws InterruptedException { | |
| 71 Intent intent = new Intent(EMBEDDED_TEST_SERVER_SERVICE); | |
| 72 intent.setClassName( | |
| 73 "org.chromium.net.test.support", "org.chromium.net.test.Embedded TestServerService"); | |
| 74 if (!context.bindService(intent, mConn, Context.BIND_AUTO_CREATE)) { | |
| 75 throw new EmbeddedTestServerFailure( | |
| 76 "Unable to bind to the EmbeddedTestServer service."); | |
| 77 } | |
| 78 synchronized (mImplMonitor) { | |
|
Yaron
2015/11/24 19:06:28
Nit: add logs around this so we can see in logcat
jbudorick
2015/11/25 02:43:40
Done.
| |
| 79 // TODO(jbudorick): Add a fatal timeout here. | |
|
jbudorick
2015/11/25 02:43:40
TODO removed and somewhat addressed.
Yaron
2015/11/25 16:04:54
"somewhat" :)
| |
| 80 while (mImpl == null) { | |
| 81 mImplMonitor.wait(); | |
| 82 } | |
| 83 } | |
| 84 try { | |
| 85 mImpl.initializeNative(); | |
| 86 } catch (RemoteException e) { | |
| 87 Log.e(TAG, "Failed to initialize native server.", e); | |
| 88 throw new EmbeddedTestServerFailure("Failed to initialize native ser ver."); | |
| 89 } | |
| 39 } | 90 } |
| 40 | 91 |
| 41 /** Serve files from the provided directory. | 92 /** Serve files from the provided directory. |
| 42 * | 93 * |
| 43 * @param directory The directory from which files should be served. | 94 * @param directory The directory from which files should be served. |
| 44 */ | 95 */ |
| 45 public void serveFilesFromDirectory(File directory) { | 96 public void serveFilesFromDirectory(File directory) { |
| 46 nativeServeFilesFromDirectory(mNativeEmbeddedTestServer, directory.getPa th()); | 97 serveFilesFromDirectory(directory.getPath()); |
| 47 } | 98 } |
| 48 | 99 |
| 49 /** Serve files from the provided directory. | 100 /** Serve files from the provided directory. |
| 50 * | 101 * |
| 51 * @param directoryPath The path of the directory from which files should b e served. | 102 * @param directoryPath The path of the directory from which files should b e served. |
| 52 */ | 103 */ |
| 53 public void serveFilesFromDirectory(String directoryPath) { | 104 public void serveFilesFromDirectory(String directoryPath) { |
| 54 nativeServeFilesFromDirectory(mNativeEmbeddedTestServer, directoryPath); | 105 try { |
| 106 mImpl.serveFilesFromDirectory(directoryPath); | |
| 107 } catch (RemoteException e) { | |
| 108 throw new EmbeddedTestServerFailure( | |
| 109 "Failed to start serving files from " + directoryPath + ": " + e.toString()); | |
| 110 } | |
| 55 } | 111 } |
| 56 | 112 |
| 57 // TODO(svaldez): Remove once all consumers have switched to start(). | 113 // TODO(svaldez): Remove once all consumers have switched to start(). |
| 58 /** Wrapper for start() | 114 /** Wrapper for start() |
| 59 * | 115 * |
| 60 * start() should be used instead of this. | 116 * start() should be used instead of this. |
| 61 * | 117 * |
| 62 * @return Whether the server was successfully initialized. | 118 * @return Whether the server was successfully initialized. |
| 63 */ | 119 */ |
| 64 public boolean initializeAndWaitUntilReady() { | 120 public boolean initializeAndWaitUntilReady() { |
| 65 return start(); | 121 return start(); |
| 66 } | 122 } |
| 67 | 123 |
| 68 /** Starts the server. | 124 /** Starts the server. |
| 69 * | 125 * |
| 70 * Note that this should be called after handlers are set up, including any relevant calls | 126 * Note that this should be called after handlers are set up, including any relevant calls |
| 71 * serveFilesFromDirectory. | 127 * serveFilesFromDirectory. |
| 72 * | 128 * |
| 73 * @return Whether the server was successfully initialized. | 129 * @return Whether the server was successfully initialized. |
| 74 */ | 130 */ |
| 75 public boolean start() { | 131 public boolean start() { |
| 76 return nativeStart(mNativeEmbeddedTestServer); | 132 try { |
| 133 return mImpl.start(); | |
| 134 } catch (RemoteException e) { | |
| 135 throw new EmbeddedTestServerFailure("Failed to start server: " + e.t oString()); | |
| 136 } | |
| 137 } | |
| 138 | |
| 139 /** Create and initialize a server that serves files from the provided direc tory. | |
| 140 * | |
| 141 * This handles native object initialization, server configuration, and ser ver initialization. | |
| 142 * On returning, the server is ready for use. | |
| 143 * | |
| 144 * @param context The context in which the server is being started. | |
| 145 * @param directory The directory from which files should be served. | |
| 146 * @return The created server. | |
| 147 */ | |
| 148 public static EmbeddedTestServer createAndStartFileServer(Context context, F ile directory) | |
| 149 throws InterruptedException { | |
| 150 EmbeddedTestServer server = new EmbeddedTestServer(); | |
| 151 server.initializeNative(context); | |
| 152 server.serveFilesFromDirectory(directory); | |
| 153 if (!server.start()) { | |
| 154 throw new EmbeddedTestServerFailure( | |
| 155 "Failed to start serving files from " + directory.getPath()) ; | |
| 156 } | |
| 157 return server; | |
| 77 } | 158 } |
| 78 | 159 |
| 79 /** Get the full URL for the given relative URL. | 160 /** Get the full URL for the given relative URL. |
| 80 * | 161 * |
| 81 * @param relativeUrl The relative URL for which a full URL will be obtaine d. | 162 * @param relativeUrl The relative URL for which a full URL will be obtaine d. |
| 82 * @return The URL as a String. | 163 * @return The URL as a String. |
| 83 */ | 164 */ |
| 84 public String getURL(String relativeUrl) { | 165 public String getURL(String relativeUrl) { |
| 85 return nativeGetURL(mNativeEmbeddedTestServer, relativeUrl); | 166 try { |
| 167 return mImpl.getURL(relativeUrl); | |
| 168 } catch (RemoteException e) { | |
| 169 throw new EmbeddedTestServerFailure( | |
| 170 "Failed to get URL for " + relativeUrl + ": " + e.toString() ); | |
| 171 } | |
| 86 } | 172 } |
| 87 | 173 |
| 88 /** Shutdown the server. | 174 /** Shutdown the server. |
| 89 * | 175 * |
| 90 * @return Whether the server was successfully shut down. | 176 * @return Whether the server was successfully shut down. |
| 91 */ | 177 */ |
| 92 public boolean shutdownAndWaitUntilComplete() { | 178 public boolean shutdownAndWaitUntilComplete() { |
| 93 return nativeShutdownAndWaitUntilComplete(mNativeEmbeddedTestServer); | 179 try { |
| 180 return mImpl.shutdownAndWaitUntilComplete(); | |
| 181 } catch (RemoteException e) { | |
| 182 throw new EmbeddedTestServerFailure("Failed to shut down: " + e.toSt ring()); | |
| 183 } | |
| 94 } | 184 } |
| 95 | 185 |
| 96 /** Destroy the native EmbeddedTestServer object. */ | 186 /** Destroy the native EmbeddedTestServer object. */ |
| 97 public void destroy() { | 187 public void destroy() { |
| 98 assert mNativeEmbeddedTestServer != 0; | 188 try { |
| 99 nativeDestroy(mNativeEmbeddedTestServer); | 189 mImpl.destroy(); |
| 100 assert mNativeEmbeddedTestServer == 0; | 190 } catch (RemoteException e) { |
| 191 throw new EmbeddedTestServerFailure("Failed to destroy native server : " + e.toString()); | |
| 192 } | |
| 101 } | 193 } |
| 102 | 194 |
| 103 @CalledByNative | 195 /** Stop and destroy the provided server. |
| 104 private void setNativePtr(long nativePtr) { | 196 * |
| 105 assert mNativeEmbeddedTestServer == 0; | 197 * This handles stopping the server and destroying the native object. |
| 106 mNativeEmbeddedTestServer = nativePtr; | 198 * |
| 199 * @param server The server to stop and destroy. | |
| 200 */ | |
| 201 public static void stopAndDestroyServer(EmbeddedTestServer server) { | |
| 202 if (!server.shutdownAndWaitUntilComplete()) { | |
| 203 throw new EmbeddedTestServerFailure("Failed to stop server."); | |
| 204 } | |
| 205 server.destroy(); | |
| 107 } | 206 } |
| 108 | |
| 109 @CalledByNative | |
| 110 private void clearNativePtr() { | |
| 111 assert mNativeEmbeddedTestServer != 0; | |
| 112 mNativeEmbeddedTestServer = 0; | |
| 113 } | |
| 114 | |
| 115 private native void nativeInit(); | |
| 116 private native void nativeDestroy(long nativeEmbeddedTestServerAndroid); | |
| 117 private native boolean nativeStart(long nativeEmbeddedTestServerAndroid); | |
| 118 private native boolean nativeShutdownAndWaitUntilComplete(long nativeEmbedde dTestServerAndroid); | |
| 119 private native String nativeGetURL(long nativeEmbeddedTestServerAndroid, Str ing relativeUrl); | |
| 120 private native void nativeServeFilesFromDirectory( | |
| 121 long nativeEmbeddedTestServerAndroid, String directoryPath); | |
| 122 } | 207 } |
| OLD | NEW |