Skip to content

Commit 3ef03e9

Browse files
phillooooChromium LUCI CQ
authored andcommitted
DPWA: use ParseURL for manifest id parsing logic
Use ParseURL for manifest id parsing logic. This allows id to be both specified as a full url or a string as path to app's origin. This allows a more consistent spec processing definition, see in spec pull request w3c/manifest#988 (comment). The processed manifest id is still passed as a normalized relative url path to WebAppProvider system to keep the manifest_id saved in sync system the same and backward-compatible since changing the manifest_id format in sync system will make new apps not syncable to previous versions of Chromium. Bug: 1182363 Change-Id: I65a7ca615bae3ee504b605abba954f60dc5d2c31 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3042714 Commit-Queue: Phillis Tang <[email protected]> Reviewed-by: Daniel Murphy <[email protected]> Cr-Commit-Position: refs/heads/master@{#904128}
1 parent cd3e88c commit 3ef03e9

File tree

2 files changed

+54
-13
lines changed

2 files changed

+54
-13
lines changed

third_party/blink/renderer/modules/manifest/manifest_parser.cc

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -340,23 +340,27 @@ String ManifestParser::ParseId(const JSONObject* object,
340340
return String();
341341
}
342342

343-
absl::optional<String> id = ParseString(object, "id", NoTrim);
344-
if (id.has_value()) {
343+
if (!start_url.IsValid()) {
344+
ManifestUmaUtil::ParseIdResult(
345+
ManifestUmaUtil::ParseIdResultType::kInvalidStartUrl);
346+
return String();
347+
}
348+
KURL start_url_origin = KURL(SecurityOrigin::Create(start_url)->ToString());
349+
350+
KURL id = ParseURL(object, "id", start_url_origin,
351+
ParseURLRestrictions::kSameOriginOnly);
352+
if (id.IsValid()) {
345353
ManifestUmaUtil::ParseIdResult(
346354
ManifestUmaUtil::ParseIdResultType::kSucceed);
347-
return *id;
348355
} else {
349-
// If id is not specified, sets to start_url with origin stripped.
350-
if (start_url.IsValid()) {
351-
ManifestUmaUtil::ParseIdResult(
352-
ManifestUmaUtil::ParseIdResultType::kDefaultToStartUrl);
353-
return start_url.GetString().Substring(start_url.PathStart() + 1);
354-
} else {
355-
ManifestUmaUtil::ParseIdResult(
356-
ManifestUmaUtil::ParseIdResultType::kInvalidStartUrl);
357-
return String();
358-
}
356+
// If id is not specified, sets to start_url
357+
ManifestUmaUtil::ParseIdResult(
358+
ManifestUmaUtil::ParseIdResultType::kDefaultToStartUrl);
359+
id = start_url;
359360
}
361+
// TODO(https://crbug.com/1231765): rename the field to relative_id to reflect
362+
// the actual value.
363+
return id.GetString().Substring(id.PathStart() + 1);
360364
}
361365

362366
KURL ManifestParser::ParseStartURL(const JSONObject* object) {

third_party/blink/renderer/modules/manifest/manifest_parser_unittest.cc

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,13 +274,50 @@ TEST_F(ManifestParserTest, IdParseRules) {
274274
ASSERT_EQ(0u, GetErrorCount());
275275
EXPECT_EQ("start?query=a", manifest->id);
276276
}
277+
// Invalid type.
278+
{
279+
auto& manifest =
280+
ParseManifest("{\"start_url\": \"/start?query=a\", \"id\": 1}");
281+
ASSERT_EQ(1u, GetErrorCount());
282+
EXPECT_EQ("start?query=a", manifest->id);
283+
}
277284
// Empty string.
278285
{
279286
auto& manifest =
280287
ParseManifest("{ \"start_url\": \"/start?query=a\", \"id\": \"\" }");
281288
ASSERT_EQ(0u, GetErrorCount());
282289
EXPECT_EQ("", manifest->id);
283290
}
291+
// Full url.
292+
{
293+
auto& manifest = ParseManifest(
294+
"{ \"start_url\": \"/start?query=a\", \"id\": \"http://foo.com/foo\" "
295+
"}");
296+
ASSERT_EQ(0u, GetErrorCount());
297+
EXPECT_EQ("foo", manifest->id);
298+
}
299+
// Full url with different origin.
300+
{
301+
auto& manifest = ParseManifest(
302+
"{ \"start_url\": \"/start?query=a\", \"id\": "
303+
"\"http://another.com/foo\" }");
304+
ASSERT_EQ(1u, GetErrorCount());
305+
EXPECT_EQ("start?query=a", manifest->id);
306+
}
307+
// Relative path
308+
{
309+
auto& manifest =
310+
ParseManifest("{ \"start_url\": \"/start?query=a\", \"id\": \".\" }");
311+
ASSERT_EQ(0u, GetErrorCount());
312+
EXPECT_EQ("", manifest->id);
313+
}
314+
// Absolute path
315+
{
316+
auto& manifest =
317+
ParseManifest("{ \"start_url\": \"/start?query=a\", \"id\": \"/\" }");
318+
ASSERT_EQ(0u, GetErrorCount());
319+
EXPECT_EQ("", manifest->id);
320+
}
284321
// Smoke test.
285322
{
286323
auto& manifest =

0 commit comments

Comments
 (0)