Chromium Code Reviews| Index: third_party/WebKit/Source/modules/document_metadata/CopylessPasteExtractor.cpp |
| diff --git a/third_party/WebKit/Source/modules/document_metadata/CopylessPasteExtractor.cpp b/third_party/WebKit/Source/modules/document_metadata/CopylessPasteExtractor.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6c3d28c0bdca950a58155c1dc2f00a1453193d71 |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/modules/document_metadata/CopylessPasteExtractor.cpp |
| @@ -0,0 +1,65 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "modules/document_metadata/CopylessPasteExtractor.h" |
| + |
| +#include "core/HTMLNames.h" |
| +#include "core/dom/Document.h" |
| +#include "core/dom/ElementTraversal.h" |
| +#include "core/frame/LocalFrame.h" |
| +#include "core/html/HTMLElement.h" |
| +#include "platform/Histogram.h" |
| +#include "platform/instrumentation/tracing/TraceEvent.h" |
| +#include "wtf/text/StringBuilder.h" |
| + |
| +namespace blink { |
| + |
| +namespace { |
| + |
| +String extractMetadata(Element& root) { |
| + StringBuilder result; |
| + result.append("["); |
| + bool multiple = false; |
| + for (Element& element : ElementTraversal::descendantsOf(root)) { |
| + if (element.hasTagName(HTMLNames::scriptTag) && |
| + element.getAttribute(HTMLNames::typeAttr) == "application/ld+json") { |
| + if (multiple) { |
| + result.append(","); |
| + } |
| + result.append(element.textContent()); |
| + multiple = true; |
| + } |
| + } |
| + result.append("]"); |
| + return result.toString(); |
| +} |
| + |
| +} // namespace |
| + |
| +String CopylessPasteExtractor::extract(Document& document) { |
| + TRACE_EVENT0("blink", "CopylessPasteExtractor::extract"); |
| + |
| + if (!document.frame() || !document.frame()->isMainFrame()) |
| + return emptyString; |
| + |
| + DCHECK(document.hasFinishedParsing()); |
| + |
| + Element* html = document.documentElement(); |
| + if (!html) |
| + return emptyString; |
| + |
| + double startTime = monotonicallyIncreasingTime(); |
| + |
| + // Traverse the DOM tree and extract the metadata. |
| + String result = extractMetadata(*html); |
| + |
| + double elapsedTime = monotonicallyIncreasingTime() - startTime; |
| + |
| + DEFINE_STATIC_LOCAL(CustomCountHistogram, extractionHistogram, |
| + ("CopylessPaste.ExtractionUs", 1, 1000000, 50)); |
|
haraken
2017/02/16 02:33:35
Have you already added the histogram to histograms
wychen
2017/02/16 17:29:30
Oops. Thanks for your eagle eyes!
|
| + extractionHistogram.count(static_cast<int>(1e6 * elapsedTime)); |
| + return result; |
| +} |
| + |
| +} // namespace blink |