Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(238)

Unified Diff: content/public/android/java/src/org/chromium/content_public/common/ResourceRequestBody.java

Issue 2038233002: Using ResourceRequestBody as the type of HTTP body outside of //content. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@make-resource-request-body-public
Patch Set: Addressed CR feedback from creis@. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/public/android/java/src/org/chromium/content_public/common/ResourceRequestBody.java
diff --git a/content/public/android/java/src/org/chromium/content_public/common/ResourceRequestBody.java b/content/public/android/java/src/org/chromium/content_public/common/ResourceRequestBody.java
new file mode 100644
index 0000000000000000000000000000000000000000..1978aa1661eef1ef4ad9e65c4947903d61b5d684
--- /dev/null
+++ b/content/public/android/java/src/org/chromium/content_public/common/ResourceRequestBody.java
@@ -0,0 +1,76 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.content_public.common;
+
+import org.chromium.base.annotations.CalledByNative;
+import org.chromium.base.annotations.JNINamespace;
+
+/**
+ * Wrapper around the native content::ResourceRequestBody.
+ * Takes care of managing the ref-count of the native counterpart.
+ */
+@JNINamespace("content")
+public final class ResourceRequestBody {
+ // Pointer to the native content:ResourceRequestBody.
+ // The Java object owns 1 ref-count to the native object.
+ private long mNativePtr;
+
+ // ResourceRequestBody Java objects can only be constructed by
+ // - ResourceRequestBody::create(byte[]) (called from Java code)
+ // - ResourceRequestBody::create(long) (called from native code)
+ private ResourceRequestBody(long nativePtr) {
+ mNativePtr = nativePtr;
+ }
+
+ protected void finalize() {
+ // content::ResourceRequestBody derives from a thread-safe
+ // base::RefCountedThreadSafe<ResourceRequestBody>, so it should
+ // be fine to rely on a finalizer (despite the warning from
+ // jni_generator/SampleForTests.java).
+ nativeReleaseResourceRequestBodyRefCount(mNativePtr);
+ }
+
+ /**
+ * Releases refcount to the ptr.
+ * @param ptr native pointer to ResourceRequestBody
+ */
+ private static native void nativeReleaseResourceRequestBodyRefCount(long ptr);
+
+ /**
+ * Used by native code to construct ResourceRequestBody.
+ * Caller should make sure nativePtr has 1 extra ref-count.
+ * @param nativePtr native pointer to ResourceRequestBody
+ */
+ @CalledByNative
+ private static ResourceRequestBody create(long nativePtr) {
+ return new ResourceRequestBody(nativePtr);
+ }
+
+ /**
+ * Used by native code to get the wrapped ResourceRequestBody pointer.
+ * Caller should secure an extra ref-count by wrapping the returned
+ * value in scoped_refptr.
+ */
+ @CalledByNative
+ private long getNativePtr() {
+ return mNativePtr;
+ }
+
+ /**
+ * Creates an instance wrapping a copy of the specified byte array.
+ * @param body the HTTP body
+ */
+ public static ResourceRequestBody create(byte[] body) {
+ long nativePtr = nativeCreateResourceRequestBodyFromBytes(body);
+ return new ResourceRequestBody(nativePtr);
+ }
+
+ /**
+ * Equivalent of the native content::ResourceRequestBody::CreateFromBytes.
+ * The returned pointer has 1 extra ref-count.
+ * @param nativePtr native pointer to ResourceRequestBody
+ */
+ private static native long nativeCreateResourceRequestBodyFromBytes(byte[] body);
+}

Powered by Google App Engine
This is Rietveld 408576698