| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "modules/document_metadata/CopylessPasteExtractor.h" | 5 #include "modules/document_metadata/CopylessPasteExtractor.h" |
| 6 | 6 |
| 7 #include "core/HTMLNames.h" | 7 #include "core/HTMLNames.h" |
| 8 #include "core/dom/Document.h" | 8 #include "core/dom/Document.h" |
| 9 #include "core/dom/ElementTraversal.h" | 9 #include "core/dom/ElementTraversal.h" |
| 10 #include "core/frame/LocalFrame.h" | 10 #include "core/frame/LocalFrame.h" |
| 11 #include "core/html/HTMLElement.h" | 11 #include "core/html/HTMLElement.h" |
| 12 #include "platform/Histogram.h" | 12 #include "platform/Histogram.h" |
| 13 #include "platform/instrumentation/tracing/TraceEvent.h" | 13 #include "platform/instrumentation/tracing/TraceEvent.h" |
| 14 #include "public/platform/InterfaceProvider.h" |
| 15 #include "public/platform/Platform.h" |
| 16 #include "public/platform/modules/document_metadata/copyless_paste.mojom-blink.h
" |
| 14 #include "wtf/text/StringBuilder.h" | 17 #include "wtf/text/StringBuilder.h" |
| 15 | 18 |
| 16 namespace blink { | 19 namespace blink { |
| 17 | 20 |
| 18 namespace { | 21 namespace { |
| 19 | 22 |
| 20 String extractMetadata(Element& root) { | 23 String extractMetadata(Element& root) { |
| 21 StringBuilder result; | 24 StringBuilder result; |
| 22 result.append("["); | 25 result.append("["); |
| 23 bool multiple = false; | 26 bool multiple = false; |
| 24 for (Element& element : ElementTraversal::descendantsOf(root)) { | 27 for (Element& element : ElementTraversal::descendantsOf(root)) { |
| 25 if (element.hasTagName(HTMLNames::scriptTag) && | 28 if (element.hasTagName(HTMLNames::scriptTag) && |
| 26 element.getAttribute(HTMLNames::typeAttr) == "application/ld+json") { | 29 element.getAttribute(HTMLNames::typeAttr) == "application/ld+json") { |
| 27 if (multiple) { | 30 if (multiple) { |
| 28 result.append(","); | 31 result.append(","); |
| 29 } | 32 } |
| 30 result.append(element.textContent()); | 33 result.append(element.textContent()); |
| 31 multiple = true; | 34 multiple = true; |
| 32 } | 35 } |
| 33 } | 36 } |
| 34 result.append("]"); | 37 result.append("]"); |
| 35 return result.toString(); | 38 return result.toString(); |
| 36 } | 39 } |
| 37 | 40 |
| 41 void sendToIcing(Document& document, String extraction) { |
| 42 mojom::blink::CopylessPastePtr servicePtr; |
| 43 Platform::current()->interfaceProvider()->getInterface( |
| 44 mojo::MakeRequest(&servicePtr)); |
| 45 DCHECK(servicePtr); |
| 46 if (!servicePtr.is_bound()) |
| 47 return; |
| 48 servicePtr->ReportEntityJsonLd(document.url().getString(), extraction); |
| 49 } |
| 50 |
| 38 } // namespace | 51 } // namespace |
| 39 | 52 |
| 40 String CopylessPasteExtractor::extract(Document& document) { | 53 String CopylessPasteExtractor::metadata(Document& document) { |
| 41 TRACE_EVENT0("blink", "CopylessPasteExtractor::extract"); | 54 TRACE_EVENT0("blink", "CopylessPasteExtractor::extract"); |
| 42 | 55 |
| 43 if (!document.frame() || !document.frame()->isMainFrame()) | 56 if (!document.frame() || !document.frame()->isMainFrame()) |
| 44 return emptyString; | 57 return emptyString; |
| 45 | 58 |
| 46 DCHECK(document.hasFinishedParsing()); | 59 DCHECK(document.hasFinishedParsing()); |
| 47 | 60 |
| 48 Element* html = document.documentElement(); | 61 Element* html = document.documentElement(); |
| 49 if (!html) | 62 if (!html) |
| 50 return emptyString; | 63 return emptyString; |
| 51 | 64 |
| 52 double startTime = monotonicallyIncreasingTime(); | 65 double startTime = monotonicallyIncreasingTime(); |
| 53 | 66 |
| 54 // Traverse the DOM tree and extract the metadata. | 67 // Traverse the DOM tree and extract the metadata. |
| 55 String result = extractMetadata(*html); | 68 String result = extractMetadata(*html); |
| 56 | 69 |
| 57 double elapsedTime = monotonicallyIncreasingTime() - startTime; | 70 double elapsedTime = monotonicallyIncreasingTime() - startTime; |
| 58 | 71 |
| 59 DEFINE_STATIC_LOCAL(CustomCountHistogram, extractionHistogram, | 72 DEFINE_STATIC_LOCAL(CustomCountHistogram, extractionHistogram, |
| 60 ("CopylessPaste.ExtractionUs", 1, 1000000, 50)); | 73 ("CopylessPaste.ExtractionUs", 1, 1000000, 50)); |
| 61 extractionHistogram.count(static_cast<int>(1e6 * elapsedTime)); | 74 extractionHistogram.count(static_cast<int>(1e6 * elapsedTime)); |
| 75 |
| 62 return result; | 76 return result; |
| 63 } | 77 } |
| 64 | 78 |
| 79 void CopylessPasteExtractor::extract(Document& document) { |
| 80 String result = metadata(document); |
| 81 if (result != "[]") { |
| 82 sendToIcing(document, result); |
| 83 } |
| 84 } |
| 85 |
| 65 } // namespace blink | 86 } // namespace blink |
| OLD | NEW |