Commit ac2672cc authored by Justin DeWitt's avatar Justin DeWitt Committed by Commit Bot

[EoS] Move experimental methods to make room for v1 code.

It was getting a bit cluttered in ExploreSitesBridge, so this
creates an ExploreSitesBridgeExperimental that contains all
the code required for the "experiment" version (that is still
in use behind a flag.)

Bug: None
Change-Id: Ie5575d1960ef7016a08f7b4eff56327fa15ce1d7
Reviewed-on: https://chromium-review.googlesource.com/1227216
Commit-Queue: Justin DeWitt <dewittj@chromium.org>
Reviewed-by: default avatarCathy Li <chili@chromium.org>
Cr-Commit-Position: refs/heads/master@{#591502}
parent bc379630
...@@ -41,14 +41,14 @@ public class ExperimentalExploreSitesSection { ...@@ -41,14 +41,14 @@ public class ExperimentalExploreSitesSection {
private void initialize() { private void initialize() {
mCategorySection = mExploreSection.findViewById(R.id.explore_sites_tiles); mCategorySection = mExploreSection.findViewById(R.id.explore_sites_tiles);
ExploreSitesBridge.getNtpCategories(mProfile, this::initializeTiles); ExploreSitesBridgeExperimental.getNtpCategories(mProfile, this::initializeTiles);
View moreCategoriesButton = mExploreSection.findViewById(R.id.explore_sites_more_button); View moreCategoriesButton = mExploreSection.findViewById(R.id.explore_sites_more_button);
moreCategoriesButton.setOnClickListener( moreCategoriesButton.setOnClickListener(
(View v) (View v)
-> mNavigationDelegate.navigateToSuggestionUrl( -> mNavigationDelegate.navigateToSuggestionUrl(
WindowOpenDisposition.CURRENT_TAB, WindowOpenDisposition.CURRENT_TAB,
ExploreSitesBridge.nativeGetCatalogUrl())); ExploreSitesBridgeExperimental.nativeGetCatalogUrl()));
} }
private void initializeTiles(List<ExploreSitesCategoryTile> tileList) { private void initializeTiles(List<ExploreSitesCategoryTile> tileList) {
...@@ -84,7 +84,7 @@ public class ExperimentalExploreSitesSection { ...@@ -84,7 +84,7 @@ public class ExperimentalExploreSitesSection {
(View v) (View v)
-> mNavigationDelegate.navigateToSuggestionUrl( -> mNavigationDelegate.navigateToSuggestionUrl(
WindowOpenDisposition.CURRENT_TAB, tile.getNavigationUrl())); WindowOpenDisposition.CURRENT_TAB, tile.getNavigationUrl()));
ExploreSitesBridge.getIcon( ExploreSitesBridgeExperimental.getIcon(
mProfile, tile.getIconUrl(), (Bitmap icon) -> onIconRetrieved(tileView, icon)); mProfile, tile.getIconUrl(), (Bitmap icon) -> onIconRetrieved(tileView, icon));
} }
} }
......
...@@ -4,8 +4,6 @@ ...@@ -4,8 +4,6 @@
package org.chromium.chrome.browser.explore_sites; package org.chromium.chrome.browser.explore_sites;
import android.graphics.Bitmap;
import org.chromium.base.Callback; import org.chromium.base.Callback;
import org.chromium.base.annotations.JNINamespace; import org.chromium.base.annotations.JNINamespace;
import org.chromium.chrome.browser.profiles.Profile; import org.chromium.chrome.browser.profiles.Profile;
...@@ -38,33 +36,6 @@ public class ExploreSitesBridge { ...@@ -38,33 +36,6 @@ public class ExploreSitesBridge {
nativeGetNtpCatalog(profile, result, callback); nativeGetNtpCatalog(profile, result, callback);
} }
/* These methods for Experimental Explore Sites */
/**
* Fetches a JSON string from URL, returning the parsed JSONobject in a callback.
* This will cancel any pending JSON fetches.
*/
public static void getNtpCategories(
Profile profile, final Callback<List<ExploreSitesCategoryTile>> callback) {
List<ExploreSitesCategoryTile> result = new ArrayList<>();
nativeGetNtpCategories(profile, result, callback);
}
/**
* Fetches an icon from a url and returns in a Bitmap image. The callback argument will be null
* if the operation fails.
*/
public static void getIcon(
Profile profile, final String iconUrl, final Callback<Bitmap> callback) {
nativeGetIcon(profile, iconUrl, callback);
}
private static native void nativeGetNtpCategories(Profile profile,
List<ExploreSitesCategoryTile> result,
Callback<List<ExploreSitesCategoryTile>> callback);
private static native void nativeGetIcon(
Profile profile, String iconUrl, Callback<Bitmap> callback);
public static native String nativeGetCatalogUrl();
private static native void nativeGetNtpCatalog(Profile profile, private static native void nativeGetNtpCatalog(Profile profile,
List<ExploreSitesCategory> result, Callback<List<ExploreSitesCategory>> callback); List<ExploreSitesCategory> result, Callback<List<ExploreSitesCategory>> callback);
......
// Copyright 2018 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.chrome.browser.explore_sites;
import android.graphics.Bitmap;
import org.chromium.base.Callback;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.chrome.browser.profiles.Profile;
import java.util.ArrayList;
import java.util.List;
/**
* The model and controller for a group of explore options.
*/
@JNINamespace("explore_sites")
public class ExploreSitesBridgeExperimental {
/**
* Fetches a JSON string from URL, returning the parsed JSONobject in a callback.
* This will cancel any pending JSON fetches.
*/
public static void getNtpCategories(
Profile profile, final Callback<List<ExploreSitesCategoryTile>> callback) {
List<ExploreSitesCategoryTile> result = new ArrayList<>();
nativeGetNtpCategories(profile, result, callback);
}
/**
* Fetches an icon from a url and returns in a Bitmap image. The callback argument will be null
* if the operation fails.
*/
public static void getIcon(
Profile profile, final String iconUrl, final Callback<Bitmap> callback) {
nativeGetIcon(profile, iconUrl, callback);
}
/* UX prototype methods. */
private static native void nativeGetNtpCategories(Profile profile,
List<ExploreSitesCategoryTile> result,
Callback<List<ExploreSitesCategoryTile>> callback);
public static native String nativeGetCatalogUrl();
private static native void nativeGetIcon(
Profile profile, String iconUrl, Callback<Bitmap> callback);
}
...@@ -31,7 +31,7 @@ public class ExploreSitesCategoryTile { ...@@ -31,7 +31,7 @@ public class ExploreSitesCategoryTile {
public ExploreSitesCategoryTile(String categoryName, String iconUrl, String navigationUrl) { public ExploreSitesCategoryTile(String categoryName, String iconUrl, String navigationUrl) {
mCategoryName = categoryName; mCategoryName = categoryName;
mIconUrl = iconUrl; mIconUrl = iconUrl;
mNavigationUrl = ExploreSitesBridge.nativeGetCatalogUrl() + navigationUrl; mNavigationUrl = ExploreSitesBridgeExperimental.nativeGetCatalogUrl() + navigationUrl;
} }
public String getNavigationUrl() { public String getNavigationUrl() {
......
...@@ -558,6 +558,7 @@ chrome_java_sources = [ ...@@ -558,6 +558,7 @@ chrome_java_sources = [
"java/src/org/chromium/chrome/browser/explore_sites/ExperimentalExploreSitesCategoryTileView.java", "java/src/org/chromium/chrome/browser/explore_sites/ExperimentalExploreSitesCategoryTileView.java",
"java/src/org/chromium/chrome/browser/explore_sites/ExperimentalExploreSitesSection.java", "java/src/org/chromium/chrome/browser/explore_sites/ExperimentalExploreSitesSection.java",
"java/src/org/chromium/chrome/browser/explore_sites/ExploreSitesBridge.java", "java/src/org/chromium/chrome/browser/explore_sites/ExploreSitesBridge.java",
"java/src/org/chromium/chrome/browser/explore_sites/ExploreSitesBridgeExperimental.java",
"java/src/org/chromium/chrome/browser/explore_sites/ExploreSitesCategory.java", "java/src/org/chromium/chrome/browser/explore_sites/ExploreSitesCategory.java",
"java/src/org/chromium/chrome/browser/explore_sites/ExploreSitesCategoryTile.java", "java/src/org/chromium/chrome/browser/explore_sites/ExploreSitesCategoryTile.java",
"java/src/org/chromium/chrome/browser/explore_sites/ExploreSitesPage.java", "java/src/org/chromium/chrome/browser/explore_sites/ExploreSitesPage.java",
...@@ -1817,7 +1818,7 @@ chrome_test_java_sources = [ ...@@ -1817,7 +1818,7 @@ chrome_test_java_sources = [
"javatests/src/org/chromium/chrome/browser/download/ui/DownloadHistoryAdapterTest.java", "javatests/src/org/chromium/chrome/browser/download/ui/DownloadHistoryAdapterTest.java",
"javatests/src/org/chromium/chrome/browser/download/ui/StubbedProvider.java", "javatests/src/org/chromium/chrome/browser/download/ui/StubbedProvider.java",
"javatests/src/org/chromium/chrome/browser/engagement/SiteEngagementServiceTest.java", "javatests/src/org/chromium/chrome/browser/engagement/SiteEngagementServiceTest.java",
"javatests/src/org/chromium/chrome/browser/explore_sites/ExploreSitesBridgeTest.java", "javatests/src/org/chromium/chrome/browser/explore_sites/ExploreSitesBridgeExperimentalTest.java",
"javatests/src/org/chromium/chrome/browser/externalnav/ExternalNavigationDelegateImplTest.java", "javatests/src/org/chromium/chrome/browser/externalnav/ExternalNavigationDelegateImplTest.java",
"javatests/src/org/chromium/chrome/browser/externalnav/ExternalNavigationHandlerTest.java", "javatests/src/org/chromium/chrome/browser/externalnav/ExternalNavigationHandlerTest.java",
"javatests/src/org/chromium/chrome/browser/externalnav/IntentWithGesturesHandlerTest.java", "javatests/src/org/chromium/chrome/browser/externalnav/IntentWithGesturesHandlerTest.java",
......
...@@ -27,9 +27,9 @@ import org.chromium.net.test.EmbeddedTestServer; ...@@ -27,9 +27,9 @@ import org.chromium.net.test.EmbeddedTestServer;
import java.util.concurrent.Semaphore; import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
/** Tests for {@link ExploreSitesBridge}. */ /** Tests for {@link ExploreSitesBridgeExperimental}. */
@RunWith(ChromeJUnit4ClassRunner.class) @RunWith(ChromeJUnit4ClassRunner.class)
public final class ExploreSitesBridgeTest { public final class ExploreSitesBridgeExperimentalTest {
@Rule @Rule
public final ChromeBrowserTestRule mRule = new ChromeBrowserTestRule(); public final ChromeBrowserTestRule mRule = new ChromeBrowserTestRule();
...@@ -62,14 +62,15 @@ public final class ExploreSitesBridgeTest { ...@@ -62,14 +62,15 @@ public final class ExploreSitesBridgeTest {
ThreadUtils.runOnUiThreadBlocking(new Runnable() { ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override @Override
public void run() { public void run() {
ExploreSitesBridge.getIcon(mProfile, testImageUrl, new Callback<Bitmap>() { ExploreSitesBridgeExperimental.getIcon(
@Override mProfile, testImageUrl, new Callback<Bitmap>() {
public void onResult(Bitmap icon) { @Override
Assert.assertNotNull(icon); public void onResult(Bitmap icon) {
Assert.assertTrue(expectedIcon.sameAs(icon)); Assert.assertNotNull(icon);
semaphore.release(); Assert.assertTrue(expectedIcon.sameAs(icon));
} semaphore.release();
}); }
});
} }
}); });
Assert.assertTrue(semaphore.tryAcquire(TIMEOUT_MS, TimeUnit.MILLISECONDS)); Assert.assertTrue(semaphore.tryAcquire(TIMEOUT_MS, TimeUnit.MILLISECONDS));
......
...@@ -2130,7 +2130,7 @@ jumbo_split_static_library("browser") { ...@@ -2130,7 +2130,7 @@ jumbo_split_static_library("browser") {
"android/explore_sites/catalog.cc", "android/explore_sites/catalog.cc",
"android/explore_sites/catalog.h", "android/explore_sites/catalog.h",
"android/explore_sites/explore_sites_bridge.cc", "android/explore_sites/explore_sites_bridge.cc",
"android/explore_sites/explore_sites_bridge.h", "android/explore_sites/explore_sites_bridge_experimental.cc",
"android/explore_sites/explore_sites_feature.cc", "android/explore_sites/explore_sites_feature.cc",
"android/explore_sites/explore_sites_feature.h", "android/explore_sites/explore_sites_feature.h",
"android/explore_sites/explore_sites_schema.cc", "android/explore_sites/explore_sites_schema.cc",
...@@ -4585,6 +4585,7 @@ if (is_android) { ...@@ -4585,6 +4585,7 @@ if (is_android) {
"../android/java/src/org/chromium/chrome/browser/download/service/DownloadTaskScheduler.java", "../android/java/src/org/chromium/chrome/browser/download/service/DownloadTaskScheduler.java",
"../android/java/src/org/chromium/chrome/browser/engagement/SiteEngagementService.java", "../android/java/src/org/chromium/chrome/browser/engagement/SiteEngagementService.java",
"../android/java/src/org/chromium/chrome/browser/explore_sites/ExploreSitesBridge.java", "../android/java/src/org/chromium/chrome/browser/explore_sites/ExploreSitesBridge.java",
"../android/java/src/org/chromium/chrome/browser/explore_sites/ExploreSitesBridgeExperimental.java",
"../android/java/src/org/chromium/chrome/browser/explore_sites/ExploreSitesCategoryTile.java", "../android/java/src/org/chromium/chrome/browser/explore_sites/ExploreSitesCategoryTile.java",
"../android/java/src/org/chromium/chrome/browser/favicon/FaviconHelper.java", "../android/java/src/org/chromium/chrome/browser/favicon/FaviconHelper.java",
"../android/java/src/org/chromium/chrome/browser/favicon/LargeIconBridge.java", "../android/java/src/org/chromium/chrome/browser/favicon/LargeIconBridge.java",
......
...@@ -2,8 +2,6 @@ ...@@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "chrome/browser/android/explore_sites/explore_sites_bridge.h"
#include <string> #include <string>
#include <utility> #include <utility>
...@@ -11,136 +9,14 @@ ...@@ -11,136 +9,14 @@
#include "base/android/jni_android.h" #include "base/android/jni_android.h"
#include "base/android/jni_string.h" #include "base/android/jni_string.h"
#include "base/android/scoped_java_ref.h" #include "base/android/scoped_java_ref.h"
#include "base/bind.h"
#include "base/threading/thread_task_runner_handle.h"
#include "chrome/browser/android/explore_sites/url_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_android.h"
#include "chrome/browser/search/suggestions/image_decoder_impl.h"
#include "components/image_fetcher/core/image_fetcher.h"
#include "components/image_fetcher/core/image_fetcher_impl.h"
#include "content/public/browser/storage_partition.h"
#include "jni/ExploreSitesBridge_jni.h" #include "jni/ExploreSitesBridge_jni.h"
#include "jni/ExploreSitesCategoryTile_jni.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/simple_url_loader.h"
#include "ui/gfx/android/java_bitmap.h"
#include "ui/gfx/image/image.h"
namespace explore_sites { namespace explore_sites {
using base::android::JavaParamRef; using base::android::JavaParamRef;
using base::android::JavaRef;
using base::android::ScopedJavaGlobalRef;
using base::android::ScopedJavaLocalRef;
namespace {
constexpr net::NetworkTrafficAnnotationTag kTrafficAnnotation =
net::DefineNetworkTrafficAnnotation("explore_sites_image_fetcher", R"(
semantics {
sender: "Explore Sites image fetcher"
description:
"Downloads images for explore sites usage."
trigger:
"When Explore Sites feature requires images from url."
data: "Requested image at url."
destination: GOOGLE_OWNED_SERVICE
}
policy {
cookies_allowed: YES
setting: "user"
policy_exception_justification:
"This feature is only enabled explicitly by flag."
})");
void GotNTPCategoriesFromJson(
const ScopedJavaGlobalRef<jobject>& j_callback_ref,
const ScopedJavaGlobalRef<jobject>& j_result_ref,
std::unique_ptr<NTPJsonFetcher> fetcher,
std::unique_ptr<NTPCatalog> catalog) {
JNIEnv* env = base::android::AttachCurrentThread();
if (catalog) {
for (NTPCatalog::Category category : catalog->categories) {
Java_ExploreSitesCategoryTile_createInList(
env, j_result_ref,
base::android::ConvertUTF8ToJavaString(env, category.id),
base::android::ConvertUTF8ToJavaString(env, category.icon_url.spec()),
base::android::ConvertUTF8ToJavaString(env, category.title));
}
}
base::android::RunObjectCallbackAndroid(j_callback_ref, j_result_ref);
}
void OnGetIconDone(std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher,
const ScopedJavaGlobalRef<jobject>& j_callback_obj,
const std::string& id,
const gfx::Image& image,
const image_fetcher::RequestMetadata& metadata) {
ScopedJavaLocalRef<jobject> j_bitmap;
if (!image.IsEmpty()) {
j_bitmap = gfx::ConvertToJavaBitmap(image.ToSkBitmap());
}
base::android::RunObjectCallbackAndroid(j_callback_obj, j_bitmap);
// Delete |image_fetcher| when appropriate.
base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE,
std::move(image_fetcher));
}
} // namespace
// static
void JNI_ExploreSitesBridge_GetNtpCategories(
JNIEnv* env,
const JavaParamRef<jclass>& j_caller,
const JavaParamRef<jobject>& j_profile,
const JavaParamRef<jobject>& j_result_obj,
const JavaParamRef<jobject>& j_callback_obj) {
NTPJsonFetcher* ntp_fetcher =
new NTPJsonFetcher(ProfileAndroid::FromProfileAndroid(j_profile));
ntp_fetcher->Start(base::BindOnce(
&GotNTPCategoriesFromJson, ScopedJavaGlobalRef<jobject>(j_callback_obj),
ScopedJavaGlobalRef<jobject>(j_result_obj),
base::WrapUnique(ntp_fetcher)));
}
// static
static void JNI_ExploreSitesBridge_GetIcon(
JNIEnv* env,
const JavaParamRef<jclass>& j_caller,
const JavaParamRef<jobject>& j_profile,
const JavaParamRef<jstring>& j_url,
const JavaParamRef<jobject>& j_callback_obj) {
Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile);
GURL icon_url(ConvertJavaStringToUTF8(env, j_url));
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory =
content::BrowserContext::GetDefaultStoragePartition(profile)
->GetURLLoaderFactoryForBrowserProcess();
auto image_fetcher = std::make_unique<image_fetcher::ImageFetcherImpl>(
std::make_unique<suggestions::ImageDecoderImpl>(), url_loader_factory);
// |image_fetcher| will be owned by the callback and gets destroyed at the end
// of the callback.
image_fetcher::ImageFetcher* image_fetcher_ptr = image_fetcher.get();
image_fetcher_ptr->FetchImage(
icon_url.spec(), icon_url,
base::BindOnce(&OnGetIconDone, std::move(image_fetcher),
ScopedJavaGlobalRef<jobject>(j_callback_obj)),
kTrafficAnnotation);
}
// static
ScopedJavaLocalRef<jstring> JNI_ExploreSitesBridge_GetCatalogUrl(
JNIEnv* env,
const JavaParamRef<jclass>& jcaller) {
return base::android::ConvertUTF8ToJavaString(env, GetCatalogURL().spec());
}
// static // static
static void JNI_ExploreSitesBridge_GetNtpCatalog( void JNI_ExploreSitesBridge_GetNtpCatalog(
JNIEnv* env, JNIEnv* env,
const JavaParamRef<jclass>& j_caller, const JavaParamRef<jclass>& j_caller,
const JavaParamRef<jobject>& j_profile, const JavaParamRef<jobject>& j_profile,
...@@ -150,7 +26,7 @@ static void JNI_ExploreSitesBridge_GetNtpCatalog( ...@@ -150,7 +26,7 @@ static void JNI_ExploreSitesBridge_GetNtpCatalog(
} }
// static // static
static void JNI_ExploreSitesBridge_GetEspCatalog( void JNI_ExploreSitesBridge_GetEspCatalog(
JNIEnv* env, JNIEnv* env,
const JavaParamRef<jclass>& j_caller, const JavaParamRef<jclass>& j_caller,
const JavaParamRef<jobject>& j_profile, const JavaParamRef<jobject>& j_profile,
......
// Copyright 2018 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.
#ifndef CHROME_BROWSER_ANDROID_EXPLORE_SITES_EXPLORE_SITES_BRIDGE_H_
#define CHROME_BROWSER_ANDROID_EXPLORE_SITES_EXPLORE_SITES_BRIDGE_H_
#include <memory>
#include "base/android/jni_android.h"
#include "base/android/jni_weak_ref.h"
#include "base/android/scoped_java_ref.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/android/explore_sites/ntp_json_fetcher.h"
namespace explore_sites {
/**
* Bridge between C++ and Java for fetching and decoding URLs and images.
*/
static void JNI_ExploreSitesBridge_GetNtpCategories(
JNIEnv* env,
const base::android::JavaParamRef<jclass>& j_caller,
const base::android::JavaParamRef<jobject>& j_profile,
const base::android::JavaParamRef<jobject>& j_result_obj,
const base::android::JavaParamRef<jobject>& j_callback_obj);
static void JNI_ExploreSitesBridge_GetIcon(
JNIEnv* env,
const base::android::JavaParamRef<jclass>& j_caller,
const base::android::JavaParamRef<jobject>& j_profile,
const base::android::JavaParamRef<jstring>& j_url,
const base::android::JavaParamRef<jobject>& j_callback_obj);
static base::android::ScopedJavaLocalRef<jstring>
JNI_ExploreSitesBridge_GetCatalogUrl(
JNIEnv* env,
const base::android::JavaParamRef<jclass>& jcaller);
static void JNI_ExploreSitesBridge_GetNtpCatalog(
JNIEnv* env,
const base::android::JavaParamRef<jclass>& j_caller,
const base::android::JavaParamRef<jobject>& j_profile,
const base::android::JavaParamRef<jobject>& j_result_obj,
const base::android::JavaParamRef<jobject>& j_callback_obj);
static void JNI_ExploreSitesBridge_GetEspCatalog(
JNIEnv* env,
const base::android::JavaParamRef<jclass>& j_caller,
const base::android::JavaParamRef<jobject>& j_profile,
const base::android::JavaParamRef<jobject>& j_result_obj,
const base::android::JavaParamRef<jobject>& j_callback_obj);
} // namespace explore_sites
#endif // CHROME_BROWSER_ANDROID_EXPLORE_SITES_EXPLORE_SITES_BRIDGE_H_
// Copyright 2018 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.
#include <string>
#include <utility>
#include "base/android/callback_android.h"
#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "base/android/scoped_java_ref.h"
#include "base/bind.h"
#include "base/threading/thread_task_runner_handle.h"
#include "chrome/browser/android/explore_sites/catalog.h"
#include "chrome/browser/android/explore_sites/catalog.pb.h"
#include "chrome/browser/android/explore_sites/explore_sites_service.h"
#include "chrome/browser/android/explore_sites/explore_sites_service_factory.h"
#include "chrome/browser/android/explore_sites/ntp_json_fetcher.h"
#include "chrome/browser/android/explore_sites/url_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_android.h"
#include "chrome/browser/search/suggestions/image_decoder_impl.h"
#include "components/image_fetcher/core/image_fetcher.h"
#include "components/image_fetcher/core/image_fetcher_impl.h"
#include "content/public/browser/storage_partition.h"
#include "jni/ExploreSitesBridgeExperimental_jni.h"
#include "jni/ExploreSitesCategoryTile_jni.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/simple_url_loader.h"
#include "ui/gfx/android/java_bitmap.h"
#include "ui/gfx/image/image.h"
namespace explore_sites {
using base::android::JavaParamRef;
using base::android::JavaRef;
using base::android::ScopedJavaGlobalRef;
using base::android::ScopedJavaLocalRef;
namespace {
constexpr net::NetworkTrafficAnnotationTag kTrafficAnnotation =
net::DefineNetworkTrafficAnnotation("explore_sites_image_fetcher", R"(
semantics {
sender: "Explore Sites image fetcher"
description:
"Downloads images for explore sites usage."
trigger:
"When Explore Sites feature requires images from url."
data: "Requested image at url."
destination: GOOGLE_OWNED_SERVICE
}
policy {
cookies_allowed: YES
setting: "user"
policy_exception_justification:
"This feature is only enabled explicitly by flag."
})");
void GotNTPCategoriesFromJson(
const ScopedJavaGlobalRef<jobject>& j_callback_ref,
const ScopedJavaGlobalRef<jobject>& j_result_ref,
std::unique_ptr<NTPJsonFetcher> fetcher,
std::unique_ptr<NTPCatalog> catalog) {
JNIEnv* env = base::android::AttachCurrentThread();
if (catalog) {
for (NTPCatalog::Category category : catalog->categories) {
Java_ExploreSitesCategoryTile_createInList(
env, j_result_ref,
base::android::ConvertUTF8ToJavaString(env, category.id),
base::android::ConvertUTF8ToJavaString(env, category.icon_url.spec()),
base::android::ConvertUTF8ToJavaString(env, category.title));
}
}
base::android::RunObjectCallbackAndroid(j_callback_ref, j_result_ref);
}
void OnGetIconDone(std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher,
const ScopedJavaGlobalRef<jobject>& j_callback_obj,
const std::string& id,
const gfx::Image& image,
const image_fetcher::RequestMetadata& metadata) {
ScopedJavaLocalRef<jobject> j_bitmap;
if (!image.IsEmpty()) {
j_bitmap = gfx::ConvertToJavaBitmap(image.ToSkBitmap());
}
base::android::RunObjectCallbackAndroid(j_callback_obj, j_bitmap);
// Delete |image_fetcher| when appropriate.
base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE,
std::move(image_fetcher));
}
} // namespace
// static
void JNI_ExploreSitesBridgeExperimental_GetNtpCategories(
JNIEnv* env,
const JavaParamRef<jclass>& j_caller,
const JavaParamRef<jobject>& j_profile,
const JavaParamRef<jobject>& j_result_obj,
const JavaParamRef<jobject>& j_callback_obj) {
NTPJsonFetcher* ntp_fetcher =
new NTPJsonFetcher(ProfileAndroid::FromProfileAndroid(j_profile));
ntp_fetcher->Start(base::BindOnce(
&GotNTPCategoriesFromJson, ScopedJavaGlobalRef<jobject>(j_callback_obj),
ScopedJavaGlobalRef<jobject>(j_result_obj),
base::WrapUnique(ntp_fetcher)));
}
// static
ScopedJavaLocalRef<jstring> JNI_ExploreSitesBridgeExperimental_GetCatalogUrl(
JNIEnv* env,
const JavaParamRef<jclass>& jcaller) {
return base::android::ConvertUTF8ToJavaString(env, GetCatalogURL().spec());
}
// static
static void JNI_ExploreSitesBridgeExperimental_GetIcon(
JNIEnv* env,
const JavaParamRef<jclass>& j_caller,
const JavaParamRef<jobject>& j_profile,
const JavaParamRef<jstring>& j_url,
const JavaParamRef<jobject>& j_callback_obj) {
Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile);
GURL icon_url(ConvertJavaStringToUTF8(env, j_url));
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory =
content::BrowserContext::GetDefaultStoragePartition(profile)
->GetURLLoaderFactoryForBrowserProcess();
auto image_fetcher = std::make_unique<image_fetcher::ImageFetcherImpl>(
std::make_unique<suggestions::ImageDecoderImpl>(), url_loader_factory);
// |image_fetcher| will be owned by the callback and gets destroyed at the end
// of the callback.
image_fetcher::ImageFetcher* image_fetcher_ptr = image_fetcher.get();
image_fetcher_ptr->FetchImage(
icon_url.spec(), icon_url,
base::BindOnce(&OnGetIconDone, std::move(image_fetcher),
ScopedJavaGlobalRef<jobject>(j_callback_obj)),
kTrafficAnnotation);
}
} // namespace explore_sites
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