Skip to content

Commit

Permalink
Intents: Deduplicate host conditions in ARC intent filters
Browse files Browse the repository at this point in the history
It's common for ARC intent filters to duplicate host conditions, due to
the way that conditions are specified in AndroidManifest.xml.

Deduplicating these hosts takes some one-off time during conversion from
ARC to App Service intent filters but saves space (especially since
intent filters are stored in multiple places in memory) and time (since
many operations need to compare against all hosts in the list).

On a device with a variety of Android apps installed (including Google
Maps, which has a notably large number of duplicated hosts), this change
reduced the size of the serialized PreferredApps file by ~15%.

Bug: 1251031
Change-Id: I69fda9bfce9c7e0252bd5e7984ff2b904879a593
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3195360
Reviewed-by: Maggie Cai <mxcai@chromium.org>
Commit-Queue: Tim Sergeant <tsergeant@chromium.org>
Cr-Commit-Position: refs/heads/main@{#931875}
  • Loading branch information
tgsergeant authored and Chromium LUCI CQ committed Oct 15, 2021
1 parent ffcef39 commit c7bd5eb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
7 changes: 7 additions & 0 deletions chrome/browser/apps/app_service/intent_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,13 @@ apps::mojom::IntentFilterPtr ConvertArcToAppServiceIntentFilter(
authority.host(), apps::mojom::PatternMatchType::kNone));
}
if (!host_condition_values.empty()) {
// It's common for Android apps to include duplicate host conditions, we can
// de-duplicate these to reduce time/space usage down the line.
std::sort(host_condition_values.begin(), host_condition_values.end());
host_condition_values.erase(
std::unique(host_condition_values.begin(), host_condition_values.end()),
host_condition_values.end());

auto host_condition = apps_util::MakeCondition(
apps::mojom::ConditionType::kHost, std::move(host_condition_values));
intent_filter->conditions.push_back(std::move(host_condition));
Expand Down
32 changes: 32 additions & 0 deletions chrome/browser/apps/app_service/intent_util_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,38 @@ TEST_F(IntentUtilsTest, ConvertArcIntentFilter_ConvertsSimpleGlobToPrefix) {
}
}

TEST_F(IntentUtilsTest, ConvertArcIntentFilter_DeduplicatesHosts) {
const char* kPackageName = "com.foo.bar";
const char* kPath = "/";
const char* kScheme = "https";
const char* kHost1 = "www.a.com";
const char* kHost2 = "www.b.com";

std::vector<arc::IntentFilter::AuthorityEntry> authorities;
authorities.emplace_back(kHost1, 0);
authorities.emplace_back(kHost2, 0);
authorities.emplace_back(kHost2, 0);
authorities.emplace_back(kHost1, 0);

std::vector<arc::IntentFilter::PatternMatcher> patterns;
patterns.emplace_back(kPath, arc::mojom::PatternType::PATTERN_PREFIX);

arc::IntentFilter arc_filter(kPackageName, {arc::kIntentActionView},
std::move(authorities), std::move(patterns),
{kScheme}, {});

IntentFilterPtr app_service_filter =
apps_util::ConvertArcToAppServiceIntentFilter(arc_filter);

for (auto& condition : app_service_filter->conditions) {
if (condition->condition_type == apps::mojom::ConditionType::kHost) {
ASSERT_EQ(2u, condition->condition_values.size());
ASSERT_EQ(kHost1, condition->condition_values[0]->value);
ASSERT_EQ(kHost2, condition->condition_values[1]->value);
}
}
}

#if defined(OS_CHROMEOS)
TEST_F(IntentUtilsTest, CrosapiIntentConversion) {
apps::mojom::IntentPtr original_intent =
Expand Down

0 comments on commit c7bd5eb

Please sign in to comment.