Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(212)

Side by Side Diff: chrome/common/favicon/fallback_icon_url_parser.cc

Issue 835903005: [Favicon] Add new fallback icon rendering flow. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Adding new host chrome://fallback-icon. Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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 "chrome/common/favicon/fallback_icon_url_parser.h"
6
7 #include "base/strings/string_number_conversions.h"
8 #include "ui/base/webui/web_ui_util.h"
9 #include "ui/gfx/favicon_size.h"
10
11 namespace {
12
13 // Parameters which can be used in chrome://favicon path. See file
14 // "fallback_icon_url_parser.h" for a description of what each does.
15 const char kSizeParameter[] = "size/";
16 const char kStyleParameter[] = "style/";
17
18 // Returns true if |search| is a substring of |path| which starts at
19 // |start_index|.
20 bool HasSubstringAt(const std::string& path,
21 size_t start_index,
22 const std::string& search) {
23 return path.compare(start_index, search.length(), search) == 0;
24 }
25
26 } // namespace
27
28 namespace chrome {
29
30 bool ParseFallbackIconPath(const std::string& path,
31 ParsedFallbackIconPath* parsed) {
pkotwicz 2015/01/21 19:44:15 Can you move the parsing logic to FallbackIconSour
huangs 2015/01/22 01:13:27 Per other comment, I think size should be a separa
huangs 2015/01/22 22:51:39 Moved to ParseFallbackIconPath(), per discussion.
32 DCHECK(parsed);
33 parsed->url = "";
34 parsed->size_in_dip = gfx::kFaviconSize;
35 parsed->device_scale_factor = 1.0f;
36 parsed->style_builder.Reset();
37
38 if (path.empty())
39 return false;
40
41 size_t parsed_index = 0;
42 if (HasSubstringAt(path, parsed_index, kSizeParameter)) {
43 parsed_index += strlen(kSizeParameter);
44 size_t slash = path.find("/", parsed_index);
45 if (slash == std::string::npos)
46 return false;
47 size_t scale_delimiter = path.find("@", parsed_index);
48 std::string size_str;
49 std::string scale_str;
50 if (scale_delimiter == std::string::npos) {
51 // Support the legacy size format of 'size/aa/' where 'aa' is the desired
52 // size in DIP for the sake of not regressing the extensions which use it.
53 size_str = path.substr(parsed_index, slash - parsed_index);
54 } else {
55 size_str = path.substr(parsed_index, scale_delimiter - parsed_index);
56 scale_str = path.substr(scale_delimiter + 1,
57 slash - scale_delimiter - 1);
58 }
59
60 if (!base::StringToInt(size_str, &parsed->size_in_dip))
61 return false;
62 if (!scale_str.empty())
63 webui::ParseScaleFactor(scale_str, &parsed->device_scale_factor);
64 parsed_index = slash + 1;
65 }
66
67 if (HasSubstringAt(path, parsed_index, kStyleParameter)) {
68 parsed_index += strlen(kStyleParameter);
69 size_t slash = path.find("/", parsed_index);
70 if (slash == std::string::npos)
71 return false;
72 std::string fallback_str = path.substr(parsed_index, slash - parsed_index);
73 if (!parsed->style_builder.Parse(fallback_str))
74 return false; // Parse failed.
75 parsed_index = slash + 1;
76 }
77
78 // Extract URL.
79 parsed->url = path.substr(parsed_index);
80 return true;
81 }
82
83 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698