OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "modules/document_metadata/CopylessPasteExtractor.h" |
| 6 |
| 7 #include "core/HTMLNames.h" |
| 8 #include "core/dom/Document.h" |
| 9 #include "core/dom/ElementTraversal.h" |
| 10 #include "core/frame/LocalFrame.h" |
| 11 #include "core/html/HTMLElement.h" |
| 12 #include "platform/Histogram.h" |
| 13 #include "platform/instrumentation/tracing/TraceEvent.h" |
| 14 #include "wtf/text/StringBuilder.h" |
| 15 |
| 16 namespace blink { |
| 17 |
| 18 namespace { |
| 19 |
| 20 String extractMetadata(Element& root) { |
| 21 StringBuilder result; |
| 22 result.append("["); |
| 23 bool multiple = false; |
| 24 for (Element& element : ElementTraversal::descendantsOf(root)) { |
| 25 if (element.hasTagName(HTMLNames::scriptTag) && |
| 26 element.getAttribute(HTMLNames::typeAttr) == "application/ld+json") { |
| 27 if (multiple) { |
| 28 result.append(","); |
| 29 } |
| 30 result.append(element.textContent()); |
| 31 multiple = true; |
| 32 } |
| 33 } |
| 34 result.append("]"); |
| 35 return result.toString(); |
| 36 } |
| 37 |
| 38 } // namespace |
| 39 |
| 40 String CopylessPasteExtractor::extract(Document& document) { |
| 41 TRACE_EVENT0("blink", "CopylessPasteExtractor::extract"); |
| 42 |
| 43 if (!document.frame() || !document.frame()->isMainFrame()) |
| 44 return emptyString; |
| 45 |
| 46 DCHECK(document.hasFinishedParsing()); |
| 47 |
| 48 Element* html = document.documentElement(); |
| 49 if (!html) |
| 50 return emptyString; |
| 51 |
| 52 double startTime = monotonicallyIncreasingTime(); |
| 53 |
| 54 // Traverse the DOM tree and extract the metadata. |
| 55 String result = extractMetadata(*html); |
| 56 |
| 57 double elapsedTime = monotonicallyIncreasingTime() - startTime; |
| 58 |
| 59 DEFINE_STATIC_LOCAL(CustomCountHistogram, extractionHistogram, |
| 60 ("CopylessPaste.ExtractionUs", 1, 1000000, 50)); |
| 61 extractionHistogram.count(static_cast<int>(1e6 * elapsedTime)); |
| 62 return result; |
| 63 } |
| 64 |
| 65 } // namespace blink |
OLD | NEW |