[M73 Dev][EFL] Disable VizDisplayCompositor for EFL port
[platform/framework/web/chromium-efl.git] / components / favicon_base / favicon_url_parser.cc
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.
4
5 #include "components/favicon_base/favicon_url_parser.h"
6
7 #include "base/strings/string_number_conversions.h"
8 #include "components/favicon_base/favicon_types.h"
9 #include "net/url_request/url_request.h"
10 #include "ui/base/webui/web_ui_util.h"
11 #include "ui/gfx/favicon_size.h"
12
13 namespace {
14
15 // Parameters which can be used in chrome://favicon path. See file
16 // "chrome/browser/ui/webui/favicon_source.h" for a description of
17 // what each does.
18 const char kIconURLParameter[] = "iconurl/";
19 const char kSizeParameter[] = "size/";
20
21 // Returns true if |search| is a substring of |path| which starts at
22 // |start_index|.
23 bool HasSubstringAt(const std::string& path,
24                     size_t start_index,
25                     const std::string& search) {
26   return path.compare(start_index, search.length(), search) == 0;
27 }
28
29 }  // namespace
30
31 namespace chrome {
32
33 bool ParseFaviconPath(const std::string& path,
34                       ParsedFaviconPath* parsed) {
35   parsed->is_icon_url = false;
36   parsed->url = "";
37   parsed->size_in_dip = gfx::kFaviconSize;
38   parsed->device_scale_factor = 1.0f;
39   parsed->path_index = std::string::npos;
40
41   if (path.empty())
42     return false;
43
44   size_t parsed_index = 0;
45   if (HasSubstringAt(path, parsed_index, kSizeParameter)) {
46     parsed_index += strlen(kSizeParameter);
47
48     size_t slash = path.find("/", parsed_index);
49     if (slash == std::string::npos)
50       return false;
51
52     size_t scale_delimiter = path.find("@", parsed_index);
53     std::string size_str;
54     std::string scale_str;
55     if (scale_delimiter == std::string::npos) {
56       // Support the legacy size format of 'size/aa/' where 'aa' is the desired
57       // size in DIP for the sake of not regressing the extensions which use it.
58       size_str = path.substr(parsed_index, slash - parsed_index);
59     } else {
60       size_str = path.substr(parsed_index, scale_delimiter - parsed_index);
61       scale_str = path.substr(scale_delimiter + 1,
62                               slash - scale_delimiter - 1);
63     }
64
65     if (!base::StringToInt(size_str, &parsed->size_in_dip))
66       return false;
67
68     if (!scale_str.empty())
69       webui::ParseScaleFactor(scale_str, &parsed->device_scale_factor);
70
71     parsed_index = slash + 1;
72   }
73
74   if (HasSubstringAt(path, parsed_index, kIconURLParameter)) {
75     parsed_index += strlen(kIconURLParameter);
76     parsed->is_icon_url = true;
77     parsed->url = path.substr(parsed_index);
78   } else {
79     parsed->url = path.substr(parsed_index);
80   }
81
82   // The parsed index needs to be returned in order to allow Instant Extended
83   // to translate favicon URLs using advanced parameters.
84   // Example:
85   //   "chrome-search://favicon/size/16@2x/<renderer-id>/<most-visited-id>"
86   // would be translated to:
87   //   "chrome-search://favicon/size/16@2x/<most-visited-item-with-given-id>".
88   parsed->path_index = parsed_index;
89   return true;
90 }
91
92 }  // namespace chrome