Commit 84f4134a authored by Xinghui Lu's avatar Xinghui Lu Committed by Commit Bot

Control real time url lookup by memory size on Android

Since some Android devices are more sensitive on network and data usage,
we want to enable real time url lookup on high end devices first. Whether
a device is high end or not is based on its system memory size. The memory
size threshold is controlled by an experiment parameter.

Bug: 1014202
Change-Id: I2dd399e71b35694b08ae0024140918d5e0d390ab
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1893989
Commit-Queue: Xinghui Lu <xinghuilu@chromium.org>
Reviewed-by: default avatarVarun Khaneja <vakh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#711934}
parent e46c0cfa
...@@ -72,6 +72,7 @@ extern const base::Feature kRealTimeUrlLookupEnabled; ...@@ -72,6 +72,7 @@ extern const base::Feature kRealTimeUrlLookupEnabled;
// Controls whether the high confidence allowlist for real time URL lookup be // Controls whether the high confidence allowlist for real time URL lookup be
// fetched. // fetched.
// Note: it is not applicable on Android.
extern const base::Feature kRealTimeUrlLookupFetchAllowlist; extern const base::Feature kRealTimeUrlLookupFetchAllowlist;
// Controls whether to send sample pings of allowlist domains on // Controls whether to send sample pings of allowlist domains on
......
...@@ -14,8 +14,17 @@ ...@@ -14,8 +14,17 @@
#include "components/user_prefs/user_prefs.h" #include "components/user_prefs/user_prefs.h"
#include "content/public/browser/browser_context.h" #include "content/public/browser/browser_context.h"
#if defined(OS_ANDROID)
#include "base/metrics/field_trial_params.h"
#include "base/system/sys_info.h"
#endif
namespace safe_browsing { namespace safe_browsing {
#if defined(OS_ANDROID)
const int kDefaultMemoryThresholdMb = 4096;
#endif
// static // static
bool RealTimePolicyEngine::IsFetchAllowlistEnabled() { bool RealTimePolicyEngine::IsFetchAllowlistEnabled() {
return base::FeatureList::IsEnabled(kRealTimeUrlLookupFetchAllowlist); return base::FeatureList::IsEnabled(kRealTimeUrlLookupFetchAllowlist);
...@@ -23,7 +32,19 @@ bool RealTimePolicyEngine::IsFetchAllowlistEnabled() { ...@@ -23,7 +32,19 @@ bool RealTimePolicyEngine::IsFetchAllowlistEnabled() {
// static // static
bool RealTimePolicyEngine::IsUrlLookupEnabled() { bool RealTimePolicyEngine::IsUrlLookupEnabled() {
return base::FeatureList::IsEnabled(kRealTimeUrlLookupEnabled); if (!base::FeatureList::IsEnabled(kRealTimeUrlLookupEnabled))
return false;
#if defined(OS_ANDROID)
// On Android, performs real time URL lookup only if
// |kRealTimeUrlLookupEnabled| is enabled, and system memory is larger than
// threshold.
int memory_threshold_mb = base::GetFieldTrialParamByFeatureAsInt(
kRealTimeUrlLookupEnabled, kRealTimeUrlLookupMemoryThresholdMb,
kDefaultMemoryThresholdMb);
return base::SysInfo::AmountOfPhysicalMemoryMB() >= memory_threshold_mb;
#else
return true;
#endif
} }
// static // static
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#ifndef COMPONENTS_SAFE_BROWSING_REALTIME_POLICY_ENGINE_H_ #ifndef COMPONENTS_SAFE_BROWSING_REALTIME_POLICY_ENGINE_H_
#define COMPONENTS_SAFE_BROWSING_REALTIME_POLICY_ENGINE_H_ #define COMPONENTS_SAFE_BROWSING_REALTIME_POLICY_ENGINE_H_
#include "build/build_config.h"
#include "content/public/common/resource_type.h" #include "content/public/common/resource_type.h"
namespace content { namespace content {
...@@ -13,6 +14,14 @@ class BrowserContext; ...@@ -13,6 +14,14 @@ class BrowserContext;
namespace safe_browsing { namespace safe_browsing {
#if defined(OS_ANDROID)
// A parameter controlled by finch experiment.
// On Android, performs real time URL lookup only if |kRealTimeUrlLookupEnabled|
// is enabled, and system memory is larger than threshold.
const char kRealTimeUrlLookupMemoryThresholdMb[] =
"SafeBrowsingRealTimeUrlLookupMemoryThresholdMb";
#endif
// This class implements the logic to decide whether the real time lookup // This class implements the logic to decide whether the real time lookup
// feature is enabled for a given user/profile. // feature is enabled for a given user/profile.
class RealTimePolicyEngine { class RealTimePolicyEngine {
......
...@@ -16,6 +16,11 @@ ...@@ -16,6 +16,11 @@
#include "content/public/test/test_browser_context.h" #include "content/public/test/test_browser_context.h"
#include "testing/platform_test.h" #include "testing/platform_test.h"
#if defined(OS_ANDROID)
#include "base/strings/string_number_conversions.h"
#include "base/system/sys_info.h"
#endif
namespace safe_browsing { namespace safe_browsing {
class RealTimePolicyEngineTest : public PlatformTest { class RealTimePolicyEngineTest : public PlatformTest {
...@@ -40,24 +45,63 @@ class RealTimePolicyEngineTest : public PlatformTest { ...@@ -40,24 +45,63 @@ class RealTimePolicyEngineTest : public PlatformTest {
sync_preferences::TestingPrefServiceSyncable pref_service_; sync_preferences::TestingPrefServiceSyncable pref_service_;
}; };
#if defined(OS_ANDROID)
// Real time URL check on Android is controlled by system memory size, the
// following tests test that logic.
TEST_F(RealTimePolicyEngineTest, TEST_F(RealTimePolicyEngineTest,
TestCanPerformFullURLLookup_DisabledFetchAllowlist) { TestCanPerformFullURLLookup_DisabledFetchAllowlistWithLargeMemorySize) {
base::test::ScopedFeatureList feature_list; base::test::ScopedFeatureList feature_list;
#if !defined(OS_ANDROID) int system_memory_size = base::SysInfo::AmountOfPhysicalMemoryMB();
feature_list.InitAndDisableFeature(kRealTimeUrlLookupFetchAllowlist); int memory_size_threshold = system_memory_size - 1;
EXPECT_FALSE(CanPerformFullURLLookup());
#else
// Should not be controlled by allowlist flag on Android. // Should not be controlled by allowlist flag on Android.
feature_list.InitWithFeatures( feature_list.InitWithFeaturesAndParameters(
/* enabled_features */ {kRealTimeUrlLookupEnabled}, /* enabled_features */ {{kRealTimeUrlLookupEnabled,
{{kRealTimeUrlLookupMemoryThresholdMb,
base::NumberToString(
memory_size_threshold)}}}},
/* disabled_features */ {kRealTimeUrlLookupFetchAllowlist}); /* disabled_features */ {kRealTimeUrlLookupFetchAllowlist});
pref_service_.SetUserPref( pref_service_.SetUserPref(
unified_consent::prefs::kUrlKeyedAnonymizedDataCollectionEnabled, unified_consent::prefs::kUrlKeyedAnonymizedDataCollectionEnabled,
std::make_unique<base::Value>(true)); std::make_unique<base::Value>(true));
EXPECT_TRUE(CanPerformFullURLLookup()); EXPECT_TRUE(CanPerformFullURLLookup());
#endif
} }
TEST_F(RealTimePolicyEngineTest,
TestCanPerformFullURLLookup_DisabledFetchAllowlistWithSmallMemorySize) {
base::test::ScopedFeatureList feature_list;
int system_memory_size = base::SysInfo::AmountOfPhysicalMemoryMB();
int memory_size_threshold = system_memory_size + 1;
// Should not be controlled by allowlist flag on Android.
feature_list.InitWithFeaturesAndParameters(
/* enabled_features */ {{kRealTimeUrlLookupEnabled,
{{kRealTimeUrlLookupMemoryThresholdMb,
base::NumberToString(
memory_size_threshold)}}}},
/* disabled_features */ {kRealTimeUrlLookupFetchAllowlist});
pref_service_.SetUserPref(
unified_consent::prefs::kUrlKeyedAnonymizedDataCollectionEnabled,
std::make_unique<base::Value>(true));
EXPECT_FALSE(CanPerformFullURLLookup());
}
TEST_F(RealTimePolicyEngineTest,
TestCanPerformFullURLLookup_DisabledUrlLookupWithLargeMemorySize) {
base::test::ScopedFeatureList feature_list;
feature_list.InitWithFeaturesAndParameters(
/* enabled_features */ {},
/* disabled_features */ {kRealTimeUrlLookupFetchAllowlist,
kRealTimeUrlLookupEnabled});
EXPECT_FALSE(CanPerformFullURLLookup());
}
#else // !defined(OS_ANDROID)
TEST_F(RealTimePolicyEngineTest,
TestCanPerformFullURLLookup_DisabledFetchAllowlist) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndDisableFeature(kRealTimeUrlLookupFetchAllowlist);
EXPECT_FALSE(CanPerformFullURLLookup());
}
#endif // defined(OS_ANDROID)
TEST_F(RealTimePolicyEngineTest, TestCanPerformFullURLLookup_EnabledByPolicy) { TEST_F(RealTimePolicyEngineTest, TestCanPerformFullURLLookup_EnabledByPolicy) {
base::test::ScopedFeatureList feature_list; base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(kRealTimeUrlLookupFetchAllowlist); feature_list.InitAndEnableFeature(kRealTimeUrlLookupFetchAllowlist);
......
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