File: web_page_metadata_extraction.cc

package info (click to toggle)
chromium 135.0.7049.95-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,959,392 kB
  • sloc: cpp: 34,198,526; ansic: 7,100,035; javascript: 3,985,800; python: 1,395,489; asm: 896,754; xml: 722,891; pascal: 180,504; sh: 94,909; perl: 88,388; objc: 79,739; sql: 53,020; cs: 41,358; fortran: 24,137; makefile: 22,501; php: 13,699; tcl: 10,142; yacc: 8,822; ruby: 7,350; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; awk: 197; sed: 36
file content (122 lines) | stat: -rw-r--r-- 4,391 bytes parent folder | download | duplicates (3)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/webapps/renderer/web_page_metadata_extraction.h"

#include <stddef.h>

#include <string>
#include <vector>

#include "components/webapps/common/web_page_metadata.mojom.h"
#include "third_party/blink/public/platform/web_icon_sizes_parser.h"
#include "third_party/blink/public/platform/web_string.h"
#include "third_party/blink/public/platform/web_url.h"
#include "third_party/blink/public/web/web_document.h"
#include "third_party/blink/public/web/web_element.h"
#include "third_party/blink/public/web/web_local_frame.h"
#include "third_party/blink/public/web/web_node.h"
#include "url/gurl.h"

using blink::WebDocument;
using blink::WebElement;
using blink::WebLocalFrame;
using blink::WebNode;
using blink::WebString;

namespace webapps {
namespace {

void AddInstallIcon(const WebElement& link,
                    std::vector<mojom::WebPageIconInfoPtr>* icons) {
  WebString href = link.GetAttribute("href");
  if (href.IsNull() || href.IsEmpty())
    return;

  // Get complete url.
  GURL url = link.GetDocument().CompleteURL(href);
  if (!url.is_valid())
    return;

  mojom::WebPageIconInfoPtr icon_info(mojom::WebPageIconInfo::New());
  if (link.HasAttribute("sizes")) {
    std::vector<gfx::Size> icon_sizes =
        blink::WebIconSizesParser::ParseIconSizes(link.GetAttribute("sizes"));
    if (icon_sizes.size() == 1 && icon_sizes[0].width() != 0 &&
        icon_sizes[0].height() == icon_sizes[0].width()) {
      icon_info->square_size_px = icon_sizes[0].width();
    }
  }
  icon_info->url = url;
  icons->push_back(std::move(icon_info));
}

}  // namespace

mojom::WebPageMetadataPtr ExtractWebPageMetadata(WebLocalFrame* frame) {
  mojom::WebPageMetadataPtr metadata(mojom::WebPageMetadata::New());

  WebDocument document = frame->GetDocument();
  if (document.IsNull())
    return metadata;

  WebElement head = document.Head();
  if (head.IsNull()) {
    return metadata;
  }

  metadata->title = document.Title().Utf16();

  GURL document_url = document.Url();
  for (WebNode child = head.FirstChild(); !child.IsNull();
       child = child.NextSibling()) {
    if (!child.IsElementNode())
      continue;
    WebElement elem = child.To<WebElement>();

    if (elem.HasHTMLTagName("link")) {
      std::string rel = elem.GetAttribute("rel").Utf8();
      // "rel" attribute may use either "icon" or "shortcut icon".
      // see also
      //   <http://en.wikipedia.org/wiki/Favicon>
      //   <http://dev.w3.org/html5/spec/Overview.html#rel-icon>
      //
      // Bookmark apps also support "apple-touch-icon" and
      // "apple-touch-icon-precomposed".
      if (base::EqualsCaseInsensitiveASCII(rel, "icon") ||
          base::EqualsCaseInsensitiveASCII(rel, "shortcut icon") ||
          base::EqualsCaseInsensitiveASCII(rel, "apple-touch-icon") ||
          base::EqualsCaseInsensitiveASCII(rel,
                                           "apple-touch-icon-precomposed")) {
        AddInstallIcon(elem, &metadata->icons);
      }
    } else if (elem.HasHTMLTagName("meta") && elem.HasAttribute("name")) {
      std::string name = elem.GetAttribute("name").Utf8();
      WebString content = elem.GetAttribute("content");
      if (name == "application-name") {
        metadata->application_name = content.Utf16();
      } else if (name == "description") {
        metadata->description = content.Utf16();
      } else if (name == "application-url") {
        std::string url = content.Utf8();
        metadata->application_url =
            document_url.is_valid() ? document_url.Resolve(url) : GURL(url);
        if (!metadata->application_url.is_valid())
          metadata->application_url = GURL();
      } else if (name == "mobile-web-app-capable" &&
                 base::EqualsCaseInsensitiveASCII(content.Utf16(), "yes")) {
        metadata->mobile_capable = mojom::WebPageMobileCapable::ENABLED;
      } else if (name == "apple-mobile-web-app-capable" &&
                 base::EqualsCaseInsensitiveASCII(content.Utf16(), "yes") &&
                 metadata->mobile_capable ==
                     mojom::WebPageMobileCapable::UNSPECIFIED) {
        metadata->mobile_capable = mojom::WebPageMobileCapable::ENABLED_APPLE;
      }
    }
  }

  return metadata;
}

}  // namespace webapps