summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/page/scrolling/fragment_anchor.cc
blob: 0d8e0886b96dab386e691993bf0cf8fa53df4361 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright 2019 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 "third_party/blink/renderer/core/page/scrolling/fragment_anchor.h"

#include "base/metrics/histogram_macros.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/frame/web_feature.h"
#include "third_party/blink/renderer/core/html/html_document.h"
#include "third_party/blink/renderer/core/page/scrolling/element_fragment_anchor.h"
#include "third_party/blink/renderer/core/page/scrolling/text_fragment_anchor.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"

namespace blink {

FragmentAnchor* FragmentAnchor::TryCreate(const KURL& url,
                                          LocalFrame& frame,
                                          bool same_document_navigation,
                                          bool should_scroll) {
  DCHECK(frame.GetDocument());

  FragmentAnchor* anchor = nullptr;
  const bool text_fragment_identifiers_enabled =
      RuntimeEnabledFeatures::TextFragmentIdentifiersEnabled(
          frame.GetDocument());

  // The text fragment anchor will be created if we successfully parsed the
  // text directive but we only do the text matching later on.
  bool text_fragment_anchor_created = false;
  if (text_fragment_identifiers_enabled) {
    anchor = TextFragmentAnchor::TryCreateFragmentDirective(
        url, frame, same_document_navigation, should_scroll);
    text_fragment_anchor_created = anchor;
  }

  // Count cases of other delimiter candidates. We don't care about whether
  // they're followed by a text directive since they've never been used with
  // it.
  if (url.HasFragmentIdentifier()) {
    if (url.FragmentIdentifier().Find("~&~") != kNotFound) {
      UseCounter::Count(frame.GetDocument(),
                        WebFeature::kFragmentHasTildeAmpersandTilde);
    }
    if (url.FragmentIdentifier().Find("~@~") != kNotFound) {
      UseCounter::Count(frame.GetDocument(),
                        WebFeature::kFragmentHasTildeAtTilde);
    }
    if (url.FragmentIdentifier().Find("&delimiter?") != kNotFound) {
      UseCounter::Count(frame.GetDocument(),
                        WebFeature::kFragmentHasAmpersandDelimiterQuestion);
    }
  }

  // For the actual delimiter, we must determine whether to use count it at
  // the point where we strip the directive. We can't use count it at that
  // point because the DocumentLoader hasn't yet been created.
  if (frame.GetDocument()->UseCountFragmentDirective()) {
    UseCounter::Count(frame.GetDocument(),
                      WebFeature::kFragmentHasColonTildeColon);
  }

  bool element_id_anchor_found = false;
  if (!anchor) {
    anchor = ElementFragmentAnchor::TryCreate(url, frame, should_scroll);
    element_id_anchor_found = anchor;
  }

  // Track how often we have a element fragment that we can't find. Only track
  // if we didn't match a text fragment since we expect those would inflate the
  // "failed" case.
  if (IsA<HTMLDocument>(frame.GetDocument()) && url.HasFragmentIdentifier() &&
      !text_fragment_anchor_created) {
    UMA_HISTOGRAM_BOOLEAN("TextFragmentAnchor.ElementIdFragmentFound",
                          element_id_anchor_found);
  }

  return anchor;
}

}  // namespace blink