Commit c1682928 authored by Scott Violet's avatar Scott Violet Committed by Commit Bot

weblayer: wires up Profile.getCachedFaviconForPageUri for Java

BUG=1076463
TEST=covered by tests

Change-Id: I03eed9dd1a307d92a2a7b92c59ab045b851e03a8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2358052
Commit-Queue: Scott Violet <sky@chromium.org>
Reviewed-by: default avatarEvan Stade <estade@chromium.org>
Cr-Commit-Position: refs/heads/master@{#798919}
parent 25977c3a
......@@ -5,6 +5,7 @@
package org.chromium.weblayer.test;
import android.graphics.Bitmap;
import android.net.Uri;
import androidx.test.filters.SmallTest;
......@@ -46,8 +47,21 @@ public class FaviconFetcherTest {
}
});
});
mActivityTestRule.navigateAndWait(
mActivityTestRule.getTestDataURL("simple_page_with_favicon.html"));
String url = mActivityTestRule.getTestDataURL("simple_page_with_favicon.html");
mActivityTestRule.navigateAndWait(url);
callbackHelper.waitForFirst();
// Verify the favicon can get obtained from the Profile.
final CallbackHelper downloadCallbackHelper = new CallbackHelper();
TestThreadUtils.runOnUiThreadBlocking(() -> {
mActivity.getBrowser().getProfile().getCachedFaviconForPageUri(
Uri.parse(url), (Bitmap bitmap) -> {
Assert.assertTrue(bitmap != null);
Assert.assertTrue(bitmap.getWidth() > 0);
Assert.assertTrue(bitmap.getHeight() > 0);
downloadCallbackHelper.notifyCalled();
});
});
downloadCallbackHelper.waitForFirst();
}
}
......@@ -5,6 +5,7 @@
package org.chromium.weblayer_private;
import android.content.Intent;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.webkit.ValueCallback;
......@@ -206,6 +207,16 @@ public final class ProfileImpl extends IProfile.Stub implements BrowserContextHa
ProfileImplJni.get().prepareForPossibleCrossOriginNavigation(mNativeProfile);
}
@Override
public void getCachedFaviconForPageUri(@NonNull String uri, @NonNull IObjectWrapper callback) {
StrictModeWorkaround.apply();
checkNotDestroyed();
ValueCallback<Bitmap> valueCallback =
(ValueCallback<Bitmap>) ObjectWrapper.unwrap(callback, ValueCallback.class);
Callback<Bitmap> baseCallback = valueCallback::onReceiveValue;
ProfileImplJni.get().getCachedFaviconForPageUrl(mNativeProfile, uri, baseCallback);
}
void checkNotDestroyed() {
if (!mBeingDeleted) return;
throw new IllegalArgumentException("Profile being destroyed: " + mName);
......@@ -274,5 +285,7 @@ public final class ProfileImpl extends IProfile.Stub implements BrowserContextHa
void removeBrowserPersistenceStorage(
long nativeProfileImpl, String[] ids, Callback<Boolean> callback);
void prepareForPossibleCrossOriginNavigation(long nativeProfileImpl);
void getCachedFaviconForPageUrl(
long nativeProfileImpl, String url, Callback<Bitmap> callback);
}
}
......@@ -36,4 +36,8 @@ interface IProfile {
void removeBrowserPersistenceStorage(in String[] ids,
in IObjectWrapper resultCallback) = 10;
void prepareForPossibleCrossOriginNavigation() = 11;
// Added in Version 86.
void getCachedFaviconForPageUri(in String uri,
in IObjectWrapper resultCallback) = 12;
}
......@@ -29,6 +29,8 @@
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/storage_partition.h"
#include "services/network/public/mojom/network_context.mojom.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"
#include "weblayer/browser/android/metrics/weblayer_metrics_service_client.h"
#include "weblayer/browser/browser_context_impl.h"
#include "weblayer/browser/browser_impl.h"
......@@ -47,6 +49,7 @@
#include "base/android/scoped_java_ref.h"
#include "components/safe_browsing/core/common/safe_browsing_prefs.h"
#include "components/unified_consent/pref_names.h"
#include "ui/gfx/android/java_bitmap.h"
#include "weblayer/browser/browser_process.h"
#include "weblayer/browser/java/jni/ProfileImpl_jni.h"
#include "weblayer/browser/safe_browsing/safe_browsing_service.h"
......@@ -114,6 +117,14 @@ void OnDidRemoveBrowserPersistenceStorage(
base::android::RunBooleanCallbackAndroid(callback, result);
}
void OnDidGetCachedFaviconForPageUrl(
const base::android::ScopedJavaGlobalRef<jobject>& callback,
gfx::Image image) {
SkBitmap favicon = image.AsImageSkia().GetRepresentation(1.0f).GetBitmap();
base::android::RunObjectCallbackAndroid(
callback, favicon.empty() ? nullptr : gfx::ConvertToJavaBitmap(&favicon));
}
#endif // OS_ANDROID
} // namespace
......@@ -549,6 +560,16 @@ void ProfileImpl::PrepareForPossibleCrossOriginNavigation(JNIEnv* env) {
PrepareForPossibleCrossOriginNavigation();
}
void ProfileImpl::GetCachedFaviconForPageUrl(
JNIEnv* env,
const base::android::JavaRef<jstring>& j_page_url,
const base::android::JavaRef<jobject>& j_callback) {
GetCachedFaviconForPageUrl(
GURL(base::android::ConvertJavaStringToUTF8(j_page_url)),
base::BindOnce(&OnDidGetCachedFaviconForPageUrl,
base::android::ScopedJavaGlobalRef<jobject>(j_callback)));
}
#endif // OS_ANDROID
base::FilePath ProfileImpl::GetBrowserPersisterDataBaseDir() const {
......
......@@ -127,6 +127,10 @@ class ProfileImpl : public Profile {
const base::android::JavaRef<jobjectArray>& j_ids,
const base::android::JavaRef<jobject>& j_callback);
void PrepareForPossibleCrossOriginNavigation(JNIEnv* env);
void GetCachedFaviconForPageUrl(
JNIEnv* env,
const base::android::JavaRef<jstring>& j_page_url,
const base::android::JavaRef<jobject>& j_callback);
#endif
const base::FilePath& download_directory() { return download_directory_; }
......
......@@ -5,6 +5,7 @@
package org.chromium.weblayer;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.RemoteException;
import android.webkit.ValueCallback;
......@@ -337,6 +338,28 @@ public class Profile {
}
}
/**
* Returns the previously downloaded favicon for {@link uri}.
*
* @param uri The uri to get the favicon for.
* @param callback The callback that is notified of the bitmap. The bitmap passed to the
* callback will be null if one is not available.
*
* @since 86
*/
public void getCachedFaviconForPageUri(@NonNull Uri uri, @NonNull Callback<Bitmap> callback) {
ThreadCheck.ensureOnUiThread();
if (WebLayer.getSupportedMajorVersionInternal() < 86) {
throw new UnsupportedOperationException();
}
try {
mImpl.getCachedFaviconForPageUri(
uri.toString(), ObjectWrapper.wrap((ValueCallback<Bitmap>) callback::onResult));
} catch (RemoteException e) {
throw new APICallException(e);
}
}
static final class DownloadCallbackClientImpl extends IDownloadCallbackClient.Stub {
private final DownloadCallback mCallback;
......
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