Add a time-out for connecting to SystemServer, and WebView, zygotes.

We wait for both the system server and the WebView zygotes, using
infinite loops.
This CL adds a time-out to both these loops.

Bug: 37654329
Test: Boot device, disable Chrome, enable Chrome, then start a WebView
app.

Change-Id: I74397de3caf2b02ee403195aeb3beb2320a2240d
diff --git a/core/java/android/os/ZygoteProcess.java b/core/java/android/os/ZygoteProcess.java
index 8208438..0d81bd9 100644
--- a/core/java/android/os/ZygoteProcess.java
+++ b/core/java/android/os/ZygoteProcess.java
@@ -19,6 +19,7 @@
 import android.net.LocalSocket;
 import android.net.LocalSocketAddress;
 import android.util.Log;
+import android.util.Slog;
 import com.android.internal.annotations.GuardedBy;
 import com.android.internal.os.Zygote;
 import com.android.internal.util.Preconditions;
@@ -529,4 +530,27 @@
             return (state.inputStream.readInt() == 0);
         }
     }
+
+    /**
+     * Try connecting to the Zygote over and over again until we hit a time-out.
+     * @param socketName The name of the socket to connect to.
+     */
+    public static void waitForConnectionToZygote(String socketName) {
+        for (int n = 20; n >= 0; n--) {
+            try {
+                final ZygoteState zs = ZygoteState.connect(socketName);
+                zs.close();
+                return;
+            } catch (IOException ioe) {
+                Log.w(LOG_TAG,
+                        "Got error connecting to zygote, retrying. msg= " + ioe.getMessage());
+            }
+
+            try {
+                Thread.sleep(1000);
+            } catch (InterruptedException ie) {
+            }
+        }
+        Slog.wtf(LOG_TAG, "Failed to connect to Zygote through socket " + socketName);
+    }
 }