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. |
mef
2015/11/25 17:20:22
This comment is no longer true.
jbudorick
2015/12/01 15:38:23
Yeah, this first sentence is no longer accurate.
| |
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 { |
mef
2015/11/25 17:20:22
Maybe call it LocalTestServer instead? It isn't em
jbudorick
2015/12/01 15:38:23
It's not, but it is still a java endpoint for embe
| |
30 private long mNativeEmbeddedTestServer; | 35 private static final String TAG = "cr_TestServer"; |
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"; |
39 private static final long SERVICE_CONNECTION_WAIT_INTERVAL_MS = 5000; | |
34 | 40 |
35 /** Initialize the native EmbeddedTestServer object. */ | 41 private IEmbeddedTestServerImpl mImpl; |
36 public void initializeNative() { | 42 private ServiceConnection mConn = new ServiceConnection() { |
37 if (mNativeEmbeddedTestServer == 0) nativeInit(); | 43 @Override |
38 assert mNativeEmbeddedTestServer != 0; | 44 public void onServiceConnected(ComponentName name, IBinder service) { |
45 synchronized (mImplMonitor) { | |
46 mImpl = IEmbeddedTestServerImpl.Stub.asInterface(service); | |
47 mImplMonitor.notify(); | |
48 } | |
49 } | |
50 | |
51 @Override | |
52 public void onServiceDisconnected(ComponentName name) { | |
53 synchronized (mImplMonitor) { | |
54 mImpl = null; | |
55 mImplMonitor.notify(); | |
56 } | |
57 } | |
58 }; | |
59 | |
60 private final Object mImplMonitor = new Object(); | |
61 | |
62 /** | |
63 * Exception class raised on failure in the EmbeddedTestServer. | |
64 */ | |
65 public static final class EmbeddedTestServerFailure extends Error { | |
66 public EmbeddedTestServerFailure(String errorDesc) { | |
67 super(errorDesc); | |
68 } | |
69 } | |
70 | |
71 public void initializeNative(Context context) throws InterruptedException { | |
72 Intent intent = new Intent(EMBEDDED_TEST_SERVER_SERVICE); | |
73 intent.setClassName( | |
74 "org.chromium.net.test.support", "org.chromium.net.test.Embedded TestServerService"); | |
75 if (!context.bindService(intent, mConn, Context.BIND_AUTO_CREATE)) { | |
76 throw new EmbeddedTestServerFailure( | |
77 "Unable to bind to the EmbeddedTestServer service."); | |
78 } | |
79 synchronized (mImplMonitor) { | |
80 Log.i(TAG, "Waiting for EmbeddedTestServer service connection."); | |
81 while (mImpl == null) { | |
82 mImplMonitor.wait(SERVICE_CONNECTION_WAIT_INTERVAL_MS); | |
83 Log.i(TAG, "Still waiting for EmbeddedTestServer service connect ion."); | |
84 } | |
85 Log.i(TAG, "EmbeddedTestServer service connected."); | |
86 } | |
87 try { | |
88 mImpl.initializeNative(); | |
89 } catch (RemoteException e) { | |
90 Log.e(TAG, "Failed to initialize native server.", e); | |
91 throw new EmbeddedTestServerFailure("Failed to initialize native ser ver."); | |
92 } | |
39 } | 93 } |
40 | 94 |
41 /** Serve files from the provided directory. | 95 /** Serve files from the provided directory. |
42 * | 96 * |
43 * @param directory The directory from which files should be served. | 97 * @param directory The directory from which files should be served. |
44 */ | 98 */ |
45 public void serveFilesFromDirectory(File directory) { | 99 public void serveFilesFromDirectory(File directory) { |
46 nativeServeFilesFromDirectory(mNativeEmbeddedTestServer, directory.getPa th()); | 100 serveFilesFromDirectory(directory.getPath()); |
47 } | 101 } |
48 | 102 |
49 /** Serve files from the provided directory. | 103 /** Serve files from the provided directory. |
50 * | 104 * |
51 * @param directoryPath The path of the directory from which files should b e served. | 105 * @param directoryPath The path of the directory from which files should b e served. |
52 */ | 106 */ |
53 public void serveFilesFromDirectory(String directoryPath) { | 107 public void serveFilesFromDirectory(String directoryPath) { |
54 nativeServeFilesFromDirectory(mNativeEmbeddedTestServer, directoryPath); | 108 try { |
109 mImpl.serveFilesFromDirectory(directoryPath); | |
110 } catch (RemoteException e) { | |
111 throw new EmbeddedTestServerFailure( | |
112 "Failed to start serving files from " + directoryPath + ": " + e.toString()); | |
113 } | |
55 } | 114 } |
56 | 115 |
57 // TODO(svaldez): Remove once all consumers have switched to start(). | 116 // TODO(svaldez): Remove once all consumers have switched to start(). |
58 /** Wrapper for start() | 117 /** Wrapper for start() |
59 * | 118 * |
60 * start() should be used instead of this. | 119 * start() should be used instead of this. |
61 * | 120 * |
62 * @return Whether the server was successfully initialized. | 121 * @return Whether the server was successfully initialized. |
63 */ | 122 */ |
64 public boolean initializeAndWaitUntilReady() { | 123 public boolean initializeAndWaitUntilReady() { |
65 return start(); | 124 return start(); |
66 } | 125 } |
67 | 126 |
68 /** Starts the server. | 127 /** Starts the server. |
69 * | 128 * |
70 * Note that this should be called after handlers are set up, including any relevant calls | 129 * Note that this should be called after handlers are set up, including any relevant calls |
71 * serveFilesFromDirectory. | 130 * serveFilesFromDirectory. |
72 * | 131 * |
73 * @return Whether the server was successfully initialized. | 132 * @return Whether the server was successfully initialized. |
74 */ | 133 */ |
75 public boolean start() { | 134 public boolean start() { |
76 return nativeStart(mNativeEmbeddedTestServer); | 135 try { |
136 return mImpl.start(); | |
137 } catch (RemoteException e) { | |
138 throw new EmbeddedTestServerFailure("Failed to start server: " + e.t oString()); | |
mef
2015/11/25 17:20:22
I think it makes sense to include |e| as the |caus
jbudorick
2015/12/01 15:38:23
Done here and in similar cases below.
| |
139 } | |
140 } | |
141 | |
142 /** Create and initialize a server that serves files from the provided direc tory. | |
143 * | |
144 * This handles native object initialization, server configuration, and ser ver initialization. | |
145 * On returning, the server is ready for use. | |
146 * | |
147 * @param context The context in which the server is being started. | |
148 * @param directory The directory from which files should be served. | |
149 * @return The created server. | |
150 */ | |
151 public static EmbeddedTestServer createAndStartFileServer(Context context, F ile directory) | |
152 throws InterruptedException { | |
153 EmbeddedTestServer server = new EmbeddedTestServer(); | |
154 server.initializeNative(context); | |
155 server.serveFilesFromDirectory(directory); | |
156 if (!server.start()) { | |
157 throw new EmbeddedTestServerFailure( | |
158 "Failed to start serving files from " + directory.getPath()) ; | |
159 } | |
160 return server; | |
77 } | 161 } |
78 | 162 |
79 /** Get the full URL for the given relative URL. | 163 /** Get the full URL for the given relative URL. |
80 * | 164 * |
81 * @param relativeUrl The relative URL for which a full URL will be obtaine d. | 165 * @param relativeUrl The relative URL for which a full URL will be obtaine d. |
82 * @return The URL as a String. | 166 * @return The URL as a String. |
83 */ | 167 */ |
84 public String getURL(String relativeUrl) { | 168 public String getURL(String relativeUrl) { |
85 return nativeGetURL(mNativeEmbeddedTestServer, relativeUrl); | 169 try { |
170 return mImpl.getURL(relativeUrl); | |
171 } catch (RemoteException e) { | |
172 throw new EmbeddedTestServerFailure( | |
173 "Failed to get URL for " + relativeUrl + ": " + e.toString() ); | |
174 } | |
86 } | 175 } |
87 | 176 |
88 /** Shutdown the server. | 177 /** Shutdown the server. |
89 * | 178 * |
90 * @return Whether the server was successfully shut down. | 179 * @return Whether the server was successfully shut down. |
91 */ | 180 */ |
92 public boolean shutdownAndWaitUntilComplete() { | 181 public boolean shutdownAndWaitUntilComplete() { |
93 return nativeShutdownAndWaitUntilComplete(mNativeEmbeddedTestServer); | 182 try { |
183 return mImpl.shutdownAndWaitUntilComplete(); | |
184 } catch (RemoteException e) { | |
185 throw new EmbeddedTestServerFailure("Failed to shut down: " + e.toSt ring()); | |
186 } | |
94 } | 187 } |
95 | 188 |
96 /** Destroy the native EmbeddedTestServer object. */ | 189 /** Destroy the native EmbeddedTestServer object. */ |
97 public void destroy() { | 190 public void destroy() { |
98 assert mNativeEmbeddedTestServer != 0; | 191 try { |
99 nativeDestroy(mNativeEmbeddedTestServer); | 192 mImpl.destroy(); |
100 assert mNativeEmbeddedTestServer == 0; | 193 } catch (RemoteException e) { |
194 throw new EmbeddedTestServerFailure("Failed to destroy native server : " + e.toString()); | |
195 } | |
101 } | 196 } |
102 | 197 |
103 @CalledByNative | 198 /** Stop and destroy the provided server. |
104 private void setNativePtr(long nativePtr) { | 199 * |
105 assert mNativeEmbeddedTestServer == 0; | 200 * This handles stopping the server and destroying the native object. |
106 mNativeEmbeddedTestServer = nativePtr; | 201 * |
202 * @param server The server to stop and destroy. | |
203 */ | |
204 public static void stopAndDestroyServer(EmbeddedTestServer server) { | |
205 if (!server.shutdownAndWaitUntilComplete()) { | |
206 throw new EmbeddedTestServerFailure("Failed to stop server."); | |
207 } | |
208 server.destroy(); | |
107 } | 209 } |
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 } | 210 } |
OLD | NEW |