Commit 7aeb422e authored by zqzhang's avatar zqzhang Committed by Commit bot

Implement WebContents.downloadImage() on Java side

This CL bridges the native WebContents::DownloadImage to Java. We are planning to use the method for fetching artwork images (WIP) in MediaMetadata and show them in Android media notification.

Related CLs:
https://codereview.chromium.org/2013813002/
https://codereview.chromium.org/2015433003/
https://codereview.chromium.org/2009243002/ (most relevant one)

BUG=616411

Review-Url: https://codereview.chromium.org/2014553002
Cr-Commit-Position: refs/heads/master@{#397807}
parent 44b9e6bd
...@@ -673,6 +673,25 @@ void WebContentsAndroid::ReloadLoFiImages(JNIEnv* env, ...@@ -673,6 +673,25 @@ void WebContentsAndroid::ReloadLoFiImages(JNIEnv* env,
static_cast<WebContentsImpl*>(web_contents_)->ReloadLoFiImages(); static_cast<WebContentsImpl*>(web_contents_)->ReloadLoFiImages();
} }
int WebContentsAndroid::DownloadImage(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& obj,
const base::android::JavaParamRef<jstring>& jurl,
jboolean is_fav_icon,
jint max_bitmap_size,
jboolean bypass_cache,
const base::android::JavaParamRef<jobject>& jcallback) {
GURL url(base::android::ConvertJavaStringToUTF8(env, jurl));
return web_contents_->DownloadImage(
url, is_fav_icon, max_bitmap_size, bypass_cache,
base::Bind(&WebContentsAndroid::OnFinishDownloadImage,
weak_factory_.GetWeakPtr(),
base::Owned(new ScopedJavaGlobalRef<jobject>(
env, obj)),
base::Owned(new ScopedJavaGlobalRef<jobject>(
env, jcallback))));
}
void WebContentsAndroid::OnFinishGetContentBitmap( void WebContentsAndroid::OnFinishGetContentBitmap(
ScopedJavaGlobalRef<jobject>* obj, ScopedJavaGlobalRef<jobject>* obj,
ScopedJavaGlobalRef<jobject>* callback, ScopedJavaGlobalRef<jobject>* callback,
...@@ -689,4 +708,35 @@ void WebContentsAndroid::OnFinishGetContentBitmap( ...@@ -689,4 +708,35 @@ void WebContentsAndroid::OnFinishGetContentBitmap(
response); response);
} }
void WebContentsAndroid::OnFinishDownloadImage(
base::android::ScopedJavaGlobalRef<jobject>* obj,
base::android::ScopedJavaGlobalRef<jobject>* callback,
int id,
int http_status_code,
const GURL& url,
const std::vector<SkBitmap>& bitmaps,
const std::vector<gfx::Size>& sizes) {
JNIEnv* env = base::android::AttachCurrentThread();
ScopedJavaLocalRef<jobject> jbitmaps =
Java_WebContentsImpl_createBitmapList(env);
ScopedJavaLocalRef<jobject> jsizes =
Java_WebContentsImpl_createSizeList(env);
ScopedJavaLocalRef<jstring> jurl =
base::android::ConvertUTF8ToJavaString(env, url.spec());
for (const SkBitmap& bitmap : bitmaps) {
// WARNING: convering to java bitmaps results in duplicate memory
// allocations, which increases the chance of OOMs if DownloadImage() is
// misused.
ScopedJavaLocalRef<jobject> jbitmap = gfx::ConvertToJavaBitmap(&bitmap);
Java_WebContentsImpl_addToBitmapList(env, jbitmaps.obj(), jbitmap.obj());
}
for (const gfx::Size& size : sizes) {
Java_WebContentsImpl_createSizeAndAddToList(
env, jsizes.obj(), size.width(), size.height());
}
Java_WebContentsImpl_onDownloadImageFinished(
env, obj->obj(), callback->obj(), id,
http_status_code, jurl.obj(), jbitmaps.obj(), jsizes.obj());
}
} // namespace content } // namespace content
...@@ -190,6 +190,14 @@ class CONTENT_EXPORT WebContentsAndroid ...@@ -190,6 +190,14 @@ class CONTENT_EXPORT WebContentsAndroid
return synchronous_compositor_client_; return synchronous_compositor_client_;
} }
int DownloadImage(JNIEnv* env,
const base::android::JavaParamRef<jobject>& obj,
const base::android::JavaParamRef<jstring>& url,
jboolean is_fav_icon,
jint max_bitmap_size,
jboolean bypass_cache,
const base::android::JavaParamRef<jobject>& jcallback);
private: private:
RenderWidgetHostViewAndroid* GetRenderWidgetHostViewAndroid(); RenderWidgetHostViewAndroid* GetRenderWidgetHostViewAndroid();
...@@ -199,6 +207,15 @@ class CONTENT_EXPORT WebContentsAndroid ...@@ -199,6 +207,15 @@ class CONTENT_EXPORT WebContentsAndroid
const SkBitmap& bitmap, const SkBitmap& bitmap,
ReadbackResponse response); ReadbackResponse response);
void OnFinishDownloadImage(
base::android::ScopedJavaGlobalRef<jobject>* obj,
base::android::ScopedJavaGlobalRef<jobject>* callback,
int id,
int http_status_code,
const GURL& url,
const std::vector<SkBitmap>& bitmaps,
const std::vector<gfx::Size>& sizes);
WebContentsImpl* web_contents_; WebContentsImpl* web_contents_;
NavigationControllerAndroid navigation_controller_; NavigationControllerAndroid navigation_controller_;
base::android::ScopedJavaGlobalRef<jobject> obj_; base::android::ScopedJavaGlobalRef<jobject> obj_;
......
...@@ -197,6 +197,7 @@ android_library("content_java") { ...@@ -197,6 +197,7 @@ android_library("content_java") {
"java/src/org/chromium/content_public/browser/AccessibilitySnapshotNode.java", "java/src/org/chromium/content_public/browser/AccessibilitySnapshotNode.java",
"java/src/org/chromium/content_public/browser/ContentBitmapCallback.java", "java/src/org/chromium/content_public/browser/ContentBitmapCallback.java",
"java/src/org/chromium/content_public/browser/GestureStateListener.java", "java/src/org/chromium/content_public/browser/GestureStateListener.java",
"java/src/org/chromium/content_public/browser/ImageDownloadCallback.java",
"java/src/org/chromium/content_public/browser/JavaScriptCallback.java", "java/src/org/chromium/content_public/browser/JavaScriptCallback.java",
"java/src/org/chromium/content_public/browser/LoadUrlParams.java", "java/src/org/chromium/content_public/browser/LoadUrlParams.java",
"java/src/org/chromium/content_public/browser/NavigationController.java", "java/src/org/chromium/content_public/browser/NavigationController.java",
......
...@@ -18,12 +18,15 @@ import org.chromium.base.annotations.JNINamespace; ...@@ -18,12 +18,15 @@ import org.chromium.base.annotations.JNINamespace;
import org.chromium.content_public.browser.AccessibilitySnapshotCallback; import org.chromium.content_public.browser.AccessibilitySnapshotCallback;
import org.chromium.content_public.browser.AccessibilitySnapshotNode; import org.chromium.content_public.browser.AccessibilitySnapshotNode;
import org.chromium.content_public.browser.ContentBitmapCallback; import org.chromium.content_public.browser.ContentBitmapCallback;
import org.chromium.content_public.browser.ImageDownloadCallback;
import org.chromium.content_public.browser.JavaScriptCallback; import org.chromium.content_public.browser.JavaScriptCallback;
import org.chromium.content_public.browser.NavigationController; import org.chromium.content_public.browser.NavigationController;
import org.chromium.content_public.browser.WebContents; import org.chromium.content_public.browser.WebContents;
import org.chromium.content_public.browser.WebContentsObserver; import org.chromium.content_public.browser.WebContentsObserver;
import org.chromium.ui.accessibility.AXTextStyle; import org.chromium.ui.accessibility.AXTextStyle;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID; import java.util.UUID;
/** /**
...@@ -469,6 +472,39 @@ import java.util.UUID; ...@@ -469,6 +472,39 @@ import java.util.UUID;
nativeReloadLoFiImages(mNativeWebContentsAndroid); nativeReloadLoFiImages(mNativeWebContentsAndroid);
} }
@Override
public int downloadImage(String url, boolean isFavicon, int maxBitmapSize,
boolean bypassCache, ImageDownloadCallback callback) {
return nativeDownloadImage(mNativeWebContentsAndroid,
url, isFavicon, maxBitmapSize, bypassCache, callback);
}
@CalledByNative
private void onDownloadImageFinished(ImageDownloadCallback callback, int id, int httpStatusCode,
String imageUrl, List<Bitmap> bitmaps, List<Rect> sizes) {
callback.onFinishDownloadImage(id, httpStatusCode, imageUrl, bitmaps, sizes);
}
@CalledByNative
private static List<Bitmap> createBitmapList() {
return new ArrayList<Bitmap>();
}
@CalledByNative
private static void addToBitmapList(List<Bitmap> bitmaps, Bitmap bitmap) {
bitmaps.add(bitmap);
}
@CalledByNative
private static List<Rect> createSizeList() {
return new ArrayList<Rect>();
}
@CalledByNative
private static void createSizeAndAddToList(List<Rect> sizes, int width, int height) {
sizes.add(new Rect(0, 0, width, height));
}
// This is static to avoid exposing a public destroy method on the native side of this class. // This is static to avoid exposing a public destroy method on the native side of this class.
private static native void nativeDestroyWebContents(long webContentsAndroidPtr); private static native void nativeDestroyWebContents(long webContentsAndroidPtr);
...@@ -530,4 +566,7 @@ import java.util.UUID; ...@@ -530,4 +566,7 @@ import java.util.UUID;
float x, float y, float width, float height); float x, float y, float width, float height);
private native void nativeOnContextMenuClosed(long nativeWebContentsAndroid); private native void nativeOnContextMenuClosed(long nativeWebContentsAndroid);
private native void nativeReloadLoFiImages(long nativeWebContentsAndroid); private native void nativeReloadLoFiImages(long nativeWebContentsAndroid);
private native int nativeDownloadImage(long nativeWebContentsAndroid,
String url, boolean isFavicon, int maxBitmapSize,
boolean bypassCache, ImageDownloadCallback callback);
} }
// 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.browser;
import android.graphics.Bitmap;
import android.graphics.Rect;
import java.util.List;
/**
* Java counterpart of native ImageDownloadCallback.
*/
public interface ImageDownloadCallback {
/**
* Called when image downloading is completed.
* @param id The unique id for the download image request, which corresponds to the return value
* of {@link WebContents.DownloadImage}.
* @param httpStatusCode The HTTP status code for the download request.
* @param imageUrl The URL of the downloaded image.
* @param bitmaps The bitmaps from the download image. Note that the bitmaps in the image could
* be ignored or resized if they are larger than the size limit in {@link
* WebContente.DownloadImage}.
* @param originalImageSizes The original sizes of {@link bitmaps} prior to the resizing.
*/
void onFinishDownloadImage(int id, int httpStatusCode, String imageUrl, List<Bitmap> bitmaps,
List<Rect> originalImageSizes);
}
...@@ -361,4 +361,24 @@ public interface WebContents extends Parcelable { ...@@ -361,4 +361,24 @@ public interface WebContents extends Parcelable {
* Reloads all the Lo-Fi images in this WebContents. * Reloads all the Lo-Fi images in this WebContents.
*/ */
public void reloadLoFiImages(); public void reloadLoFiImages();
/**
* Sends a request to download the given image {@link url}.
* This method delegates the call to the downloadImage() method of native WebContents.
* @param url The URL of the image to download.
* @param isFavicon Whether the image is a favicon. If true, the cookies are not sent and not
* accepted during download.
* @param maxBitmapSize The maximum bitmap size. Bitmaps with pixel sizes larger than {@link
* max_bitmap_size} are filtered out from the bitmap results. If there are no
* bitmap results <= {@link max_bitmap_size}, the smallest bitmap is resized to
* {@link max_bitmap_size} and is the only result. A {@link max_bitmap_size} of
* 0 means unlimited.
* @param bypassCache If true, {@link url} is requested from the server even if it is present in
* the browser cache.
* @param callback The callback which will be called when the bitmaps are received from the
* renderer.
* @return The unique id of the download request
*/
public int downloadImage(String url, boolean isFavicon, int maxBitmapSize,
boolean bypassCache, ImageDownloadCallback callback);
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment