summaryrefslogtreecommitdiffstats
path: root/components/favicon_base/fallback_icon_url_parser.cc
blob: 8855695bfdfb88dd3211a847c6b3fbc1859c1f58 (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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2015 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 "components/favicon_base/fallback_icon_url_parser.h"

#include <algorithm>

#include "base/logging.h"
#include "base/macros.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "third_party/skia/include/utils/SkParse.h"
#include "ui/gfx/favicon_size.h"

namespace {

// List of sizes corresponding to RGB, ARGB, RRGGBB, AARRGGBB.
const size_t kValidHexColorSizes[] = {3, 4, 6, 8};

// Returns whether |color_str| is a valid CSS color in hex format if we prepend
// '#', i.e., whether |color_str| matches /^[0-9A-Fa-f]{3,4,6,8}$/.
bool IsHexColorString(const std::string& color_str) {
  size_t len = color_str.length();
  const size_t* end = kValidHexColorSizes + arraysize(kValidHexColorSizes);
  if (std::find(kValidHexColorSizes, end, len) == end)
    return false;
  for (auto ch : color_str) {
    if (!base::IsHexDigit(ch))
      return false;
  }
  return true;
}

}  // namespace

namespace chrome {

ParsedFallbackIconPath::ParsedFallbackIconPath()
    : size_in_pixels_(gfx::kFaviconSize) {
}

ParsedFallbackIconPath::~ParsedFallbackIconPath() {
}

bool ParsedFallbackIconPath::Parse(const std::string& path) {
  if (path.empty())
    return false;

  size_t slash = path.find("/", 0);
  if (slash == std::string::npos)
    return false;
  std::string spec_str = path.substr(0, slash);
  if (!ParseSpecs(spec_str, &size_in_pixels_, &style_))
    return false;  // Parse failed.

  // Need to store the index of the URL field, so Instant Extended can translate
  // fallback icon URLs using advanced parameters.
  // Example:
  //   "chrome-search://fallback-icon/48/<renderer-id>/<most-visited-id>"
  // would be translated to:
  //   "chrome-search://fallback-icon/48/<most-visited-item-with-given-id>".
  path_index_ = slash + 1;
  url_string_ = path.substr(path_index_);
  return true;
}

// static
bool ParsedFallbackIconPath::ParseSpecs(
    const std::string& specs_str,
    int *size,
    favicon_base::FallbackIconStyle* style) {
  DCHECK(size);
  DCHECK(style);

  std::vector<std::string> tokens = base::SplitString(
      specs_str, ",", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
  if (tokens.size() != 5)  // Force "," for empty fields.
    return false;

  *size = gfx::kFaviconSize;
  if (!tokens[0].empty() && !base::StringToInt(tokens[0], size))
    return false;
  if (*size <= 0)
    return false;

  if (!tokens[1].empty() && !ParseColor(tokens[1], &style->background_color))
    return false;

  if (tokens[2].empty())
    favicon_base::MatchFallbackIconTextColorAgainstBackgroundColor(style);
  else if (!ParseColor(tokens[2], &style->text_color))
    return false;

  if (!tokens[3].empty() &&
      !base::StringToDouble(tokens[3], &style->font_size_ratio))
    return false;

  if (!tokens[4].empty() && !base::StringToDouble(tokens[4], &style->roundness))
    return false;

  return favicon_base::ValidateFallbackIconStyle(*style);
}

// static
bool ParsedFallbackIconPath::ParseColor(const std::string& color_str,
                                        SkColor* color) {
  DCHECK(color);
  // Exclude the empty case. Also disallow the '#' prefix, since we want color
  // to be part of an URL, but in URL '#' is used for ref fragment.
  if (color_str.empty() || color_str[0] == '#')
    return false;

  // If a valid color hex string is given, prepend '#' and parse (always works).
  // This is unambiguous since named color never only use leters 'a' to 'f'.
  if (IsHexColorString(color_str)) {
    // Default alpha to 0xFF since FindColor() preserves unspecified alpha.
    *color = SK_ColorWHITE;
    // Need temp variable to avoid use-after-free of returned pointer.
    std::string color_str_with_hash = "#" + color_str;
    const char* end = SkParse::FindColor(color_str_with_hash.c_str(), color);
    DCHECK(end && !*end);  // Call should succeed and consume string.
    return true;
  }

  // Default alpha to 0xFF.
  SkColor temp_color = SK_ColorWHITE;
  const char* end = SkParse::FindColor(color_str.c_str(), &temp_color);
  if (end && !*end) {  // Successful if call succeeds and string is consumed.
    *color = temp_color;
    return true;
  }
  return false;
}

}  // namespace chrome