1 // Copyright 2013 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.
5 #include "chrome/common/favicon/favicon_url_parser.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "chrome/common/favicon/favicon_types.h"
9 #include "net/url_request/url_request.h"
10 #include "ui/base/layout.h"
11 #include "ui/base/webui/web_ui_util.h"
12 #include "ui/gfx/favicon_size.h"
16 // Parameters which can be used in chrome://favicon path. See file
17 // "chrome/browser/ui/webui/favicon_source.h" for a description of
19 const char kIconURLParameter
[] = "iconurl/";
20 const char kLargestParameter
[] = "largest/";
21 const char kOriginParameter
[] = "origin/";
22 const char kSizeParameter
[] = "size/";
24 // Returns true if |search| is a substring of |path| which starts at
26 bool HasSubstringAt(const std::string
& path
,
28 const std::string
& search
) {
29 return path
.compare(start_index
, search
.length(), search
) == 0;
36 bool ParseFaviconPath(const std::string
& path
,
38 ParsedFaviconPath
* parsed
) {
39 parsed
->is_icon_url
= false;
41 parsed
->size_in_dip
= gfx::kFaviconSize
;
42 parsed
->scale_factor
= ui::SCALE_FACTOR_100P
;
43 parsed
->path_index
= -1;
48 size_t parsed_index
= 0;
49 if (HasSubstringAt(path
, parsed_index
, kLargestParameter
)) {
50 parsed_index
+= strlen(kLargestParameter
);
51 parsed
->size_in_dip
= 0;
52 } else if (HasSubstringAt(path
, parsed_index
, kSizeParameter
)) {
53 parsed_index
+= strlen(kSizeParameter
);
55 size_t slash
= path
.find("/", parsed_index
);
56 if (slash
== std::string::npos
)
59 size_t scale_delimiter
= path
.find("@", parsed_index
);
61 std::string scale_str
;
62 if (scale_delimiter
== std::string::npos
) {
63 // Support the legacy size format of 'size/aa/' where 'aa' is the desired
64 // size in DIP for the sake of not regressing the extensions which use it.
65 size_str
= path
.substr(parsed_index
, slash
- parsed_index
);
67 size_str
= path
.substr(parsed_index
, scale_delimiter
- parsed_index
);
68 scale_str
= path
.substr(scale_delimiter
+ 1,
69 slash
- scale_delimiter
- 1);
72 if (!base::StringToInt(size_str
, &parsed
->size_in_dip
))
75 if (parsed
->size_in_dip
!= (gfx::kFaviconSize
* 4) &&
76 parsed
->size_in_dip
!= (gfx::kFaviconSize
* 2)) {
77 // Only 64x64, 32x32 and 16x16 icons are supported.
78 parsed
->size_in_dip
= gfx::kFaviconSize
;
81 if (!scale_str
.empty())
82 webui::ParseScaleFactor(scale_str
, &parsed
->scale_factor
);
84 // Return the default favicon (as opposed to a resized favicon) for
85 // favicon sizes which are not cached by the favicon service.
86 // Currently the favicon service caches:
87 // - favicons of sizes "gfx::kFaviconSize * scale factor" px of type FAVICON
88 // where scale factor is one of FaviconUtil::GetFaviconScaleFactors().
89 // - the largest TOUCH_ICON / TOUCH_PRECOMPOSED_ICON
90 if (parsed
->size_in_dip
!= gfx::kFaviconSize
&&
91 icon_types
== chrome::FAVICON
)
94 parsed_index
= slash
+ 1;
97 if (HasSubstringAt(path
, parsed_index
, kIconURLParameter
)) {
98 parsed_index
+= strlen(kIconURLParameter
);
99 parsed
->is_icon_url
= true;
100 parsed
->url
= path
.substr(parsed_index
);
102 // URL requests prefixed with "origin/" are converted to a form with an
103 // empty path and a valid scheme. (e.g., example.com -->
104 // http://example.com/ or http://example.com/a --> http://example.com/)
105 if (HasSubstringAt(path
, parsed_index
, kOriginParameter
)) {
106 parsed_index
+= strlen(kOriginParameter
);
107 std::string possibly_invalid_url
= path
.substr(parsed_index
);
109 // If the URL does not specify a scheme (e.g., example.com instead of
110 // http://example.com), add "http://" as a default.
111 if (!GURL(possibly_invalid_url
).has_scheme())
112 possibly_invalid_url
= "http://" + possibly_invalid_url
;
114 // Strip the path beyond the top-level domain.
115 parsed
->url
= GURL(possibly_invalid_url
).GetOrigin().spec();
117 parsed
->url
= path
.substr(parsed_index
);
121 // The parsed index needs to be returned in order to allow Instant Extended
122 // to translate favicon URLs using advanced parameters.
124 // "chrome-search://favicon/size/16@2x/<renderer-id>/<most-visited-id>"
125 // would be translated to:
126 // "chrome-search://favicon/size/16@2x/<most-visited-item-with-given-id>".
127 parsed
->path_index
= parsed_index
;
131 } // namespace chrome