Commit bba5785a authored by Carlos Knippschild's avatar Carlos Knippschild Committed by Commit Bot

Make Prefetch flag and configuration accessible from Java.

Expose to Java Offline Prefetch flag and configurations to allow the
upcoming work on Android UI and settings to access them.

Bug: 850588
Change-Id: I697e944b57ebae57df73def9bffc8059b13eaa5d
Reviewed-on: https://chromium-review.googlesource.com/1154144Reviewed-by: default avatarJustin DeWitt <dewittj@chromium.org>
Reviewed-by: default avatarShakti Sahu <shaktisahu@chromium.org>
Commit-Queue: Carlos Knippschild <carlosk@chromium.org>
Cr-Commit-Position: refs/heads/master@{#579599}
parent e8c7ab52
...@@ -247,6 +247,7 @@ public abstract class ChromeFeatureList { ...@@ -247,6 +247,7 @@ public abstract class ChromeFeatureList {
"OfflinePagesDescriptiveFailStatus"; "OfflinePagesDescriptiveFailStatus";
public static final String OFFLINE_PAGES_DESCRIPTIVE_PENDING_STATUS = public static final String OFFLINE_PAGES_DESCRIPTIVE_PENDING_STATUS =
"OfflinePagesDescriptivePendingStatus"; "OfflinePagesDescriptivePendingStatus";
public static final String OFFLINE_PAGES_PREFETCHING = "OfflinePagesPrefetching";
public static final String OMNIBOX_HIDE_SCHEME_DOMAIN_IN_STEADY_STATE = public static final String OMNIBOX_HIDE_SCHEME_DOMAIN_IN_STEADY_STATE =
"OmniboxUIExperimentHideSteadyStateUrlSchemeAndSubdomains"; "OmniboxUIExperimentHideSteadyStateUrlSchemeAndSubdomains";
public static final String OMNIBOX_SPARE_RENDERER = "OmniboxSpareRenderer"; public static final String OMNIBOX_SPARE_RENDERER = "OmniboxSpareRenderer";
......
// 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.offlinepages.prefetch;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.chrome.browser.ChromeFeatureList;
import org.chromium.chrome.browser.profiles.Profile;
/**
* Allows the querying and setting of Offline Prefetch related configurations.
*/
@JNINamespace("offline_pages::android")
public class PrefetchConfiguration {
/**
* Returns true if the Offline Prefetch feature flag is enabled. This can be used to determine
* if any evidence of this feature should be presented to the user (like its respective user
* settings entries).
*/
public static boolean isPrefetchingFlagEnabled() {
return ChromeFeatureList.isEnabled(ChromeFeatureList.OFFLINE_PAGES_PREFETCHING);
}
/**
* Returns true if Offline Prefetch is allowed to run, requiring both the feature flag and the
* user setting to be true. If the current browser Profile is null this method returns false.
*/
public static boolean isPrefetchingEnabled() {
return nativeIsPrefetchingEnabled(Profile.getLastUsedProfile());
}
/**
* Sets the value of the user controlled setting that controls whether Offline Prefetch is
* enabled or disabled. If the current browser Profile is null the setting will not be changed.
*/
public static void setPrefetchingEnabledInSettings(boolean enabled) {
nativeSetPrefetchingEnabledInSettings(Profile.getLastUsedProfile(), enabled);
}
private static native boolean nativeIsPrefetchingEnabled(Profile profile);
private static native void nativeSetPrefetchingEnabledInSettings(
Profile profile, boolean enabled);
}
...@@ -922,6 +922,7 @@ chrome_java_sources = [ ...@@ -922,6 +922,7 @@ chrome_java_sources = [
"java/src/org/chromium/chrome/browser/offlinepages/prefetch/OfflineNotificationBackgroundTask.java", "java/src/org/chromium/chrome/browser/offlinepages/prefetch/OfflineNotificationBackgroundTask.java",
"java/src/org/chromium/chrome/browser/offlinepages/prefetch/PrefetchBackgroundTask.java", "java/src/org/chromium/chrome/browser/offlinepages/prefetch/PrefetchBackgroundTask.java",
"java/src/org/chromium/chrome/browser/offlinepages/prefetch/PrefetchBackgroundTaskScheduler.java", "java/src/org/chromium/chrome/browser/offlinepages/prefetch/PrefetchBackgroundTaskScheduler.java",
"java/src/org/chromium/chrome/browser/offlinepages/prefetch/PrefetchConfiguration.java",
"java/src/org/chromium/chrome/browser/offlinepages/prefetch/PrefetchPrefs.java", "java/src/org/chromium/chrome/browser/offlinepages/prefetch/PrefetchPrefs.java",
"java/src/org/chromium/chrome/browser/offlinepages/prefetch/PrefetchedPagesNotifier.java", "java/src/org/chromium/chrome/browser/offlinepages/prefetch/PrefetchedPagesNotifier.java",
"java/src/org/chromium/chrome/browser/omaha/ExponentialBackoffScheduler.java", "java/src/org/chromium/chrome/browser/omaha/ExponentialBackoffScheduler.java",
...@@ -1871,6 +1872,7 @@ chrome_test_java_sources = [ ...@@ -1871,6 +1872,7 @@ chrome_test_java_sources = [
"javatests/src/org/chromium/chrome/browser/offlinepages/RecentTabsTest.java", "javatests/src/org/chromium/chrome/browser/offlinepages/RecentTabsTest.java",
"javatests/src/org/chromium/chrome/browser/offlinepages/indicator/OfflineIndicatorControllerTest.java", "javatests/src/org/chromium/chrome/browser/offlinepages/indicator/OfflineIndicatorControllerTest.java",
"javatests/src/org/chromium/chrome/browser/offlinepages/prefetch/PrefetchBackgroundTaskTest.java", "javatests/src/org/chromium/chrome/browser/offlinepages/prefetch/PrefetchBackgroundTaskTest.java",
"javatests/src/org/chromium/chrome/browser/offlinepages/prefetch/PrefetchConfigurationTest.java",
"javatests/src/org/chromium/chrome/browser/offlinepages/prefetch/PrefetchFlowTest.java", "javatests/src/org/chromium/chrome/browser/offlinepages/prefetch/PrefetchFlowTest.java",
"javatests/src/org/chromium/chrome/browser/offlinepages/prefetch/TestOfflinePageService.java", "javatests/src/org/chromium/chrome/browser/offlinepages/prefetch/TestOfflinePageService.java",
"javatests/src/org/chromium/chrome/browser/offlinepages/prefetch/TestSuggestionsService.java", "javatests/src/org/chromium/chrome/browser/offlinepages/prefetch/TestSuggestionsService.java",
......
// 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.offlinepages.prefetch;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import android.support.test.filters.MediumTest;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.ThreadUtils;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.Feature;
import org.chromium.chrome.browser.ChromeActivity;
import org.chromium.chrome.browser.ChromeFeatureList;
import org.chromium.chrome.browser.ChromeSwitches;
import org.chromium.chrome.test.ChromeActivityTestRule;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.chrome.test.util.browser.Features;
import java.util.concurrent.atomic.AtomicBoolean;
/** Integration tests for {@link PrefetchConfiguration}. */
@CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE})
@RunWith(ChromeJUnit4ClassRunner.class)
public class PrefetchConfigurationTest {
@Rule
public ChromeActivityTestRule<ChromeActivity> mActivityTestRule =
new ChromeActivityTestRule<>(ChromeActivity.class);
@Before
public void setUp() throws Exception {
// Start Chrome.
mActivityTestRule.startMainActivityOnBlankPage();
}
@Test
@MediumTest
@Feature("OfflinePrefetch")
@Features.EnableFeatures(ChromeFeatureList.OFFLINE_PAGES_PREFETCHING)
public void testWithPrefetchingFeatureEnabled() {
AtomicBoolean isFlagEnabled = new AtomicBoolean();
AtomicBoolean isEnabled = new AtomicBoolean();
ThreadUtils.runOnUiThreadBlocking(() -> {
isFlagEnabled.set(PrefetchConfiguration.isPrefetchingFlagEnabled());
isEnabled.set(PrefetchConfiguration.isPrefetchingEnabled());
});
assertTrue(isFlagEnabled.get());
assertTrue(isEnabled.get());
ThreadUtils.runOnUiThreadBlocking(() -> {
// Disable prefetching user setting.
PrefetchConfiguration.setPrefetchingEnabledInSettings(false);
isFlagEnabled.set(PrefetchConfiguration.isPrefetchingFlagEnabled());
isEnabled.set(PrefetchConfiguration.isPrefetchingEnabled());
});
assertTrue(isFlagEnabled.get());
assertFalse(isEnabled.get());
ThreadUtils.runOnUiThreadBlocking(() -> {
// Re-enable prefetching user setting.
PrefetchConfiguration.setPrefetchingEnabledInSettings(true);
isFlagEnabled.set(PrefetchConfiguration.isPrefetchingFlagEnabled());
isEnabled.set(PrefetchConfiguration.isPrefetchingEnabled());
});
assertTrue(isFlagEnabled.get());
assertTrue(isEnabled.get());
}
@Test
@MediumTest
@Feature("OfflinePrefetch")
@Features.DisableFeatures(ChromeFeatureList.OFFLINE_PAGES_PREFETCHING)
public void testWithPrefetchingFeatureDisabled() {
AtomicBoolean isFlagEnabled = new AtomicBoolean();
AtomicBoolean isEnabled = new AtomicBoolean();
ThreadUtils.runOnUiThreadBlocking(() -> {
isFlagEnabled.set(PrefetchConfiguration.isPrefetchingFlagEnabled());
isEnabled.set(PrefetchConfiguration.isPrefetchingEnabled());
});
assertFalse(isFlagEnabled.get());
assertFalse(isEnabled.get());
ThreadUtils.runOnUiThreadBlocking(() -> {
// Disable prefetching user setting.
PrefetchConfiguration.setPrefetchingEnabledInSettings(false);
isFlagEnabled.set(PrefetchConfiguration.isPrefetchingFlagEnabled());
isEnabled.set(PrefetchConfiguration.isPrefetchingEnabled());
});
assertFalse(isFlagEnabled.get());
assertFalse(isEnabled.get());
ThreadUtils.runOnUiThreadBlocking(() -> {
// Re-enable prefetching user setting.
PrefetchConfiguration.setPrefetchingEnabledInSettings(true);
isFlagEnabled.set(PrefetchConfiguration.isPrefetchingFlagEnabled());
isEnabled.set(PrefetchConfiguration.isPrefetchingEnabled());
});
assertFalse(isFlagEnabled.get());
assertFalse(isEnabled.get());
}
}
...@@ -3959,6 +3959,7 @@ jumbo_split_static_library("browser") { ...@@ -3959,6 +3959,7 @@ jumbo_split_static_library("browser") {
"offline_pages/android/prefetch_background_task_android.cc", "offline_pages/android/prefetch_background_task_android.cc",
"offline_pages/android/prefetch_background_task_android.h", "offline_pages/android/prefetch_background_task_android.h",
"offline_pages/android/prefetch_background_task_scheduler_android.cc", "offline_pages/android/prefetch_background_task_scheduler_android.cc",
"offline_pages/android/prefetch_configuration_impl_android.cc",
"offline_pages/android/prefetched_pages_notifier_android.cc", "offline_pages/android/prefetched_pages_notifier_android.cc",
"offline_pages/android/request_coordinator_factory.cc", "offline_pages/android/request_coordinator_factory.cc",
] ]
...@@ -4522,6 +4523,7 @@ if (is_android) { ...@@ -4522,6 +4523,7 @@ if (is_android) {
"../android/java/src/org/chromium/chrome/browser/offlinepages/prefetch/OfflineNotificationBackgroundTask.java", "../android/java/src/org/chromium/chrome/browser/offlinepages/prefetch/OfflineNotificationBackgroundTask.java",
"../android/java/src/org/chromium/chrome/browser/offlinepages/prefetch/PrefetchBackgroundTask.java", "../android/java/src/org/chromium/chrome/browser/offlinepages/prefetch/PrefetchBackgroundTask.java",
"../android/java/src/org/chromium/chrome/browser/offlinepages/prefetch/PrefetchBackgroundTaskScheduler.java", "../android/java/src/org/chromium/chrome/browser/offlinepages/prefetch/PrefetchBackgroundTaskScheduler.java",
"../android/java/src/org/chromium/chrome/browser/offlinepages/prefetch/PrefetchConfiguration.java",
"../android/java/src/org/chromium/chrome/browser/offlinepages/prefetch/PrefetchedPagesNotifier.java", "../android/java/src/org/chromium/chrome/browser/offlinepages/prefetch/PrefetchedPagesNotifier.java",
"../android/java/src/org/chromium/chrome/browser/omnibox/AnswersImage.java", "../android/java/src/org/chromium/chrome/browser/omnibox/AnswersImage.java",
"../android/java/src/org/chromium/chrome/browser/omnibox/AutocompleteController.java", "../android/java/src/org/chromium/chrome/browser/omnibox/AutocompleteController.java",
......
...@@ -159,6 +159,7 @@ const base::Feature* kFeaturesExposedToJava[] = { ...@@ -159,6 +159,7 @@ const base::Feature* kFeaturesExposedToJava[] = {
&offline_pages::kOfflinePagesDescriptiveFailStatusFeature, &offline_pages::kOfflinePagesDescriptiveFailStatusFeature,
&offline_pages::kOfflinePagesDescriptivePendingStatusFeature, &offline_pages::kOfflinePagesDescriptivePendingStatusFeature,
&offline_pages::kOfflinePagesSharingFeature, &offline_pages::kOfflinePagesSharingFeature,
&offline_pages::kPrefetchingOfflinePagesFeature,
&omnibox::kUIExperimentHideSteadyStateUrlSchemeAndSubdomains, &omnibox::kUIExperimentHideSteadyStateUrlSchemeAndSubdomains,
&password_manager::features::kPasswordExport, &password_manager::features::kPasswordExport,
&password_manager::features::kPasswordSearchMobile, &password_manager::features::kPasswordSearchMobile,
......
// 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 "base/android/jni_android.h"
#include "chrome/browser/offline_pages/prefetch/prefetch_configuration_impl.h"
#include "chrome/browser/offline_pages/prefetch/prefetch_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_android.h"
#include "components/offline_pages/core/prefetch/prefetch_service.h"
#include "jni/PrefetchConfiguration_jni.h"
using base::android::JavaParamRef;
// These functions fulfill the Java to native link between
// PrefetchConfiguration.java and PrefetchConfigurationImpl.
namespace offline_pages {
namespace android {
namespace {
PrefetchConfigurationImpl* PrefetchConfigurationImplFromJProfile(
const JavaParamRef<jobject>& jprofile) {
Profile* profile = ProfileAndroid::FromProfileAndroid(jprofile);
if (!profile)
return nullptr;
PrefetchService* prefetch_service =
PrefetchServiceFactory::GetForBrowserContext(profile);
if (!prefetch_service)
return nullptr;
// The cast below is safe because PrefetchConfigurationImpl is Chrome's
// implementation of PrefetchConfiguration.
return static_cast<PrefetchConfigurationImpl*>(
prefetch_service->GetPrefetchConfiguration());
}
} // namespace
JNI_EXPORT jboolean JNI_PrefetchConfiguration_IsPrefetchingEnabled(
JNIEnv* env,
const JavaParamRef<jclass>& jcaller,
const JavaParamRef<jobject>& jprofile) {
PrefetchConfigurationImpl* config_impl =
PrefetchConfigurationImplFromJProfile(jprofile);
if (config_impl)
return static_cast<jboolean>(config_impl->IsPrefetchingEnabled());
return false;
}
JNI_EXPORT void JNI_PrefetchConfiguration_SetPrefetchingEnabledInSettings(
JNIEnv* env,
const JavaParamRef<jclass>& jcaller,
const JavaParamRef<jobject>& jprofile,
jboolean enabled) {
PrefetchConfigurationImpl* config_impl =
PrefetchConfigurationImplFromJProfile(jprofile);
if (config_impl)
config_impl->SetPrefetchingEnabledInSettings(enabled);
}
} // namespace android
} // namespace offline_pages
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