Add JSON-LD id field to entity in schema.org extractor

Bug: 1065555
Change-Id: I45b4a8b788ea585f4fc6a655e435c9b54573b7d0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2127433
Commit-Queue: Sam Bowen <sgbowen@google.com>
Reviewed-by: Becca Hughes <beccahughes@chromium.org>
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Cr-Commit-Position: refs/heads/master@{#755066}
diff --git a/components/schema_org/common/improved_metadata.mojom b/components/schema_org/common/improved_metadata.mojom
index fd546a4c..576174d7 100644
--- a/components/schema_org/common/improved_metadata.mojom
+++ b/components/schema_org/common/improved_metadata.mojom
@@ -36,6 +36,11 @@
 // Tree structure of entities is possible.
 // Ref: https://developers.google.com/schemas/formats/json-ld
 struct Entity {
-  string type;  // Correspond to the "@type" key, defined in JSON-LD.
+  // Corresponds to the "@type" key, defined in JSON-LD.
+  // See: https://w3c.github.io/json-ld-syntax/#specifying-the-type
+  string type;
+  // Corresponds to the "@id" key, defined in JSON-LD.
+  // See: https://w3c.github.io/json-ld-syntax/#node-identifiers
+  string id;
   array<Property> properties;
 };
diff --git a/components/schema_org/extractor.cc b/components/schema_org/extractor.cc
index 59b8465..2e04d3e 100644
--- a/components/schema_org/extractor.cc
+++ b/components/schema_org/extractor.cc
@@ -43,6 +43,7 @@
 constexpr size_t kMaxRepeatedSize = 100;
 
 constexpr char kJSONLDKeyType[] = "@type";
+constexpr char kJSONLDKeyId[] = "@id";
 
 using improved::mojom::Entity;
 using improved::mojom::EntityPtr;
@@ -191,6 +192,11 @@
   }
   entity->type = type;
 
+  std::string id;
+  if (val.GetString(kJSONLDKeyId, &id)) {
+    entity->id = id;
+  }
+
   for (const auto& entry : val.DictItems()) {
     if (entity->properties.size() >= kMaxNumFields) {
       break;
diff --git a/components/schema_org/extractor_unittest.cc b/components/schema_org/extractor_unittest.cc
index d75edcd..9b9a826d 100644
--- a/components/schema_org/extractor_unittest.cc
+++ b/components/schema_org/extractor_unittest.cc
@@ -133,12 +133,13 @@
 }
 
 TEST_F(SchemaOrgExtractorTest, Basic) {
-  EntityPtr extracted =
-      Extract("{\"@type\": \"VideoObject\", \"name\": \"a video!\"}");
+  EntityPtr extracted = Extract(
+      "{\"@type\": \"VideoObject\", \"@id\": \"1\", \"name\": \"a video!\"}");
   ASSERT_FALSE(extracted.is_null());
 
   EntityPtr expected = Entity::New();
   expected->type = "VideoObject";
+  expected->id = "1";
   expected->properties.push_back(CreateStringProperty("name", "a video!"));
 
   EXPECT_EQ(expected, extracted);