Commit 43407d9d authored by Maggie Cai's avatar Maggie Cai Committed by Commit Bot

[IntentHandling] Support mime type matching for only main type.

There are mime type that only contain the main type or single *. They
are treated as wildcard matching as well, e.g. image, *. This CL
supports this.

BUG=1092784

Change-Id: I0f3bdd81eb1f3bbc29f94e0b22206e3886dd5abf
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2269443
Commit-Queue: Maggie Cai <mxcai@chromium.org>
Reviewed-by: default avatarDominick Ng <dominickn@chromium.org>
Cr-Commit-Position: refs/heads/master@{#783818}
parent e22d5d22
......@@ -55,12 +55,18 @@ bool MimeTypeMatched(const std::string& mime_type1,
base::SplitString(mime_type2, kMimeTypeSeparator, base::TRIM_WHITESPACE,
base::SPLIT_WANT_NONEMPTY);
const size_t kMimeTypeComponentSize = 2;
if (components1.size() != kMimeTypeComponentSize ||
components2.size() != kMimeTypeComponentSize) {
constexpr size_t kMimeTypeComponentSize = 2;
if (components1.size() > kMimeTypeComponentSize ||
components2.size() > kMimeTypeComponentSize || components1.size() == 0 ||
components2.size() == 0) {
return false;
}
// For strings only contain the main mime type (i.e. no "/").
if (components1.size() == 1 || components2.size() == 1) {
return ComponentMatched(components1[0], components2[0]);
}
// Both intent and intent filter can use wildcard for mime type.
for (size_t i = 0; i < kMimeTypeComponentSize; i++) {
if (!ComponentMatched(components1[i], components2[i])) {
......
......@@ -226,11 +226,15 @@ TEST_F(IntentUtilTest, MimeTypeMatch) {
std::string mime_type2 = "image/jpeg";
std::string mime_type_sub_wildcard = "text/*";
std::string mime_type_all_wildcard = "*/*";
std::string mime_type_only_main_type = "text";
std::string mime_type_only_star = "*";
auto intent1 = CreateShareIntent(mime_type1);
auto intent2 = CreateShareIntent(mime_type2);
auto intent_sub_wildcard = CreateShareIntent(mime_type_sub_wildcard);
auto intent_all_wildcard = CreateShareIntent(mime_type_all_wildcard);
auto intent_only_main_type = CreateShareIntent(mime_type_only_main_type);
auto intent_only_star = CreateShareIntent(mime_type_only_star);
auto filter1 = CreateIntentFilterForShareTarget(mime_type1);
......@@ -238,6 +242,8 @@ TEST_F(IntentUtilTest, MimeTypeMatch) {
EXPECT_FALSE(apps_util::IntentMatchesFilter(intent2, filter1));
EXPECT_TRUE(apps_util::IntentMatchesFilter(intent_sub_wildcard, filter1));
EXPECT_TRUE(apps_util::IntentMatchesFilter(intent_all_wildcard, filter1));
EXPECT_TRUE(apps_util::IntentMatchesFilter(intent_only_main_type, filter1));
EXPECT_TRUE(apps_util::IntentMatchesFilter(intent_only_star, filter1));
auto filter2 = CreateIntentFilterForShareTarget(mime_type2);
......@@ -245,6 +251,8 @@ TEST_F(IntentUtilTest, MimeTypeMatch) {
EXPECT_TRUE(apps_util::IntentMatchesFilter(intent2, filter2));
EXPECT_FALSE(apps_util::IntentMatchesFilter(intent_sub_wildcard, filter2));
EXPECT_TRUE(apps_util::IntentMatchesFilter(intent_all_wildcard, filter2));
EXPECT_FALSE(apps_util::IntentMatchesFilter(intent_only_main_type, filter2));
EXPECT_TRUE(apps_util::IntentMatchesFilter(intent_only_star, filter2));
auto filter_sub_wildcard =
CreateIntentFilterForShareTarget(mime_type_sub_wildcard);
......@@ -255,6 +263,10 @@ TEST_F(IntentUtilTest, MimeTypeMatch) {
apps_util::IntentMatchesFilter(intent_sub_wildcard, filter_sub_wildcard));
EXPECT_TRUE(
apps_util::IntentMatchesFilter(intent_all_wildcard, filter_sub_wildcard));
EXPECT_TRUE(apps_util::IntentMatchesFilter(intent_only_main_type,
filter_sub_wildcard));
EXPECT_TRUE(
apps_util::IntentMatchesFilter(intent_only_star, filter_sub_wildcard));
auto filter_all_wildcard =
CreateIntentFilterForShareTarget(mime_type_all_wildcard);
......@@ -265,4 +277,35 @@ TEST_F(IntentUtilTest, MimeTypeMatch) {
apps_util::IntentMatchesFilter(intent_sub_wildcard, filter_all_wildcard));
EXPECT_TRUE(
apps_util::IntentMatchesFilter(intent_all_wildcard, filter_all_wildcard));
EXPECT_TRUE(apps_util::IntentMatchesFilter(intent_only_main_type,
filter_all_wildcard));
EXPECT_TRUE(
apps_util::IntentMatchesFilter(intent_only_star, filter_all_wildcard));
auto filter_only_main_type =
CreateIntentFilterForShareTarget(mime_type_only_main_type);
EXPECT_TRUE(apps_util::IntentMatchesFilter(intent1, filter_only_main_type));
EXPECT_FALSE(apps_util::IntentMatchesFilter(intent2, filter_only_main_type));
EXPECT_TRUE(apps_util::IntentMatchesFilter(intent_sub_wildcard,
filter_only_main_type));
EXPECT_TRUE(apps_util::IntentMatchesFilter(intent_all_wildcard,
filter_only_main_type));
EXPECT_TRUE(apps_util::IntentMatchesFilter(intent_only_main_type,
filter_only_main_type));
EXPECT_TRUE(
apps_util::IntentMatchesFilter(intent_only_star, filter_only_main_type));
auto filter_only_star = CreateIntentFilterForShareTarget(mime_type_only_star);
EXPECT_TRUE(apps_util::IntentMatchesFilter(intent1, filter_only_star));
EXPECT_TRUE(apps_util::IntentMatchesFilter(intent2, filter_only_star));
EXPECT_TRUE(
apps_util::IntentMatchesFilter(intent_sub_wildcard, filter_only_star));
EXPECT_TRUE(
apps_util::IntentMatchesFilter(intent_all_wildcard, filter_only_star));
EXPECT_TRUE(
apps_util::IntentMatchesFilter(intent_only_main_type, filter_only_star));
EXPECT_TRUE(
apps_util::IntentMatchesFilter(intent_only_star, filter_only_star));
}
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