Move BlockGuard and CloseGuard to Platform

This was causing issues on Gingerbread devices since CloseGuard was not
in that release yet. Move them out to Platform so we can filter on
release when we decide whether to instantiate or not.

Bug: 24607028
Change-Id: Iba0bbb0b878076319ace40f848aa5e307e2c3ad8
diff --git a/src/compat/java/org/conscrypt/Platform.java b/src/compat/java/org/conscrypt/Platform.java
index 9687c85..32144b2 100644
--- a/src/compat/java/org/conscrypt/Platform.java
+++ b/src/compat/java/org/conscrypt/Platform.java
@@ -18,6 +18,8 @@
 
 import android.os.Build;
 import android.util.Log;
+import dalvik.system.BlockGuard;
+import dalvik.system.CloseGuard;
 import java.io.FileDescriptor;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
@@ -359,4 +361,51 @@
         }
         return null;
     }
+
+    /*
+     * CloseGuard functions.
+     */
+
+    public static CloseGuard closeGuardGet() {
+        if (Build.VERSION.SDK_INT < 14) {
+            return null;
+        }
+
+        return CloseGuard.get();
+    }
+
+    public static void closeGuardOpen(Object guardObj, String message) {
+        if (Build.VERSION.SDK_INT < 14) {
+            return;
+        }
+
+        CloseGuard guard = (CloseGuard) guardObj;
+        guard.open(message);
+    }
+
+    public static void closeGuardClose(Object guardObj) {
+        if (Build.VERSION.SDK_INT < 14) {
+            return;
+        }
+
+        CloseGuard guard = (CloseGuard) guardObj;
+        guard.close();
+    }
+
+    public static void closeGuardWarnIfOpen(Object guardObj) {
+        if (Build.VERSION.SDK_INT < 14) {
+            return;
+        }
+
+        CloseGuard guard = (CloseGuard) guardObj;
+        guard.warnIfOpen();
+    }
+
+    /*
+     * BlockGuard functions.
+     */
+
+    public static void blockGuardOnNetwork() {
+        BlockGuard.getThreadPolicy().onNetwork();
+    }
 }
diff --git a/src/main/java/org/conscrypt/OpenSSLSocketImpl.java b/src/main/java/org/conscrypt/OpenSSLSocketImpl.java
index c25ce41..68df361 100644
--- a/src/main/java/org/conscrypt/OpenSSLSocketImpl.java
+++ b/src/main/java/org/conscrypt/OpenSSLSocketImpl.java
@@ -147,7 +147,11 @@
     private final int peerPort;
 
     private final SSLParametersImpl sslParameters;
-    private final CloseGuard guard = CloseGuard.get();
+
+    /*
+     * A CloseGuard object on Android. On other platforms, this is nothing.
+     */
+    private final Object guard = Platform.closeGuardGet();
 
     private ArrayList<HandshakeCompletedListener> listeners;
 
@@ -290,7 +294,7 @@
             final AbstractSessionContext sessionContext = sslParameters.getSessionContext();
             final long sslCtxNativePointer = sessionContext.sslCtxNativePointer;
             sslNativePointer = NativeCrypto.SSL_new(sslCtxNativePointer);
-            guard.open("close");
+            Platform.closeGuardOpen(guard, "close");
 
             boolean enableSessionCreation = getEnableSessionCreation();
             if (!enableSessionCreation) {
@@ -690,7 +694,7 @@
          */
         @Override
         public int read(byte[] buf, int offset, int byteCount) throws IOException {
-            BlockGuard.getThreadPolicy().onNetwork();
+            Platform.blockGuardOnNetwork();
 
             checkOpen();
             ArrayUtils.checkOffsetAndCount(buf.length, offset, byteCount);
@@ -757,7 +761,7 @@
          */
         @Override
         public void write(byte[] buf, int offset, int byteCount) throws IOException {
-            BlockGuard.getThreadPolicy().onNetwork();
+            Platform.blockGuardOnNetwork();
             checkOpen();
             ArrayUtils.checkOffsetAndCount(buf.length, offset, byteCount);
             if (byteCount == 0) {
@@ -1133,7 +1137,7 @@
 
     private void shutdownAndFreeSslNative() throws IOException {
         try {
-            BlockGuard.getThreadPolicy().onNetwork();
+            Platform.blockGuardOnNetwork();
             NativeCrypto.SSL_shutdown(sslNativePointer, Platform.getFileDescriptor(socket),
                     this);
         } catch (IOException ignored) {
@@ -1167,7 +1171,7 @@
         }
         NativeCrypto.SSL_free(sslNativePointer);
         sslNativePointer = 0;
-        guard.close();
+        Platform.closeGuardClose(guard);
     }
 
     @Override
@@ -1190,7 +1194,7 @@
              * reader.
              */
             if (guard != null) {
-                guard.warnIfOpen();
+                Platform.closeGuardWarnIfOpen(guard);
             }
             free();
         } finally {
diff --git a/src/platform/java/org/conscrypt/Platform.java b/src/platform/java/org/conscrypt/Platform.java
index 58c5541..e5b9f9e 100644
--- a/src/platform/java/org/conscrypt/Platform.java
+++ b/src/platform/java/org/conscrypt/Platform.java
@@ -19,12 +19,11 @@
 import static android.system.OsConstants.SOL_SOCKET;
 import static android.system.OsConstants.SO_SNDTIMEO;
 
-import org.apache.harmony.security.utils.AlgNameMapper;
-import org.apache.harmony.security.utils.AlgNameMapperSource;
-import org.conscrypt.GCMParameters;
 import android.system.ErrnoException;
 import android.system.Os;
 import android.system.StructTimeval;
+import dalvik.system.BlockGuard;
+import dalvik.system.CloseGuard;
 import java.io.FileDescriptor;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
@@ -42,6 +41,9 @@
 import javax.net.ssl.SSLParameters;
 import javax.net.ssl.SSLSocketFactory;
 import javax.net.ssl.X509TrustManager;
+import org.apache.harmony.security.utils.AlgNameMapper;
+import org.apache.harmony.security.utils.AlgNameMapperSource;
+import org.conscrypt.GCMParameters;
 
 class Platform {
     private static class NoPreloadHolder {
@@ -182,4 +184,35 @@
     public static AlgorithmParameterSpec toGCMParameterSpec(int tagLenInBits, byte[] iv) {
         return new GCMParameterSpec(tagLenInBits, iv);
     }
+
+    /*
+     * CloseGuard functions.
+     */
+
+    public static CloseGuard closeGuardGet() {
+        return CloseGuard.get();
+    }
+
+    public static void closeGuardOpen(Object guardObj, String message) {
+        CloseGuard guard = (CloseGuard) guardObj;
+        guard.open(message);
+    }
+
+    public static void closeGuardClose(Object guardObj) {
+        CloseGuard guard = (CloseGuard) guardObj;
+        guard.close();
+    }
+
+    public static void closeGuardWarnIfOpen(Object guardObj) {
+        CloseGuard guard = (CloseGuard) guardObj;
+        guard.warnIfOpen();
+    }
+
+    /*
+     * BlockGuard functions.
+     */
+
+    public static void blockGuardOnNetwork() {
+        BlockGuard.getThreadPolicy().onNetwork();
+    }
 }