This source file includes following definitions.
- HasSubstringAt
- ParseFaviconPath
#include "chrome/common/favicon/favicon_url_parser.h"
#include "base/strings/string_number_conversions.h"
#include "chrome/common/favicon/favicon_types.h"
#include "net/url_request/url_request.h"
#include "ui/base/layout.h"
#include "ui/base/webui/web_ui_util.h"
#include "ui/gfx/favicon_size.h"
namespace {
const char kIconURLParameter[] = "iconurl/";
const char kLargestParameter[] = "largest/";
const char kOriginParameter[] = "origin/";
const char kSizeParameter[] = "size/";
bool HasSubstringAt(const std::string& path,
size_t start_index,
const std::string& search) {
return path.compare(start_index, search.length(), search) == 0;
}
}
namespace chrome {
bool ParseFaviconPath(const std::string& path,
int icon_types,
ParsedFaviconPath* parsed) {
parsed->is_icon_url = false;
parsed->url = "";
parsed->size_in_dip = gfx::kFaviconSize;
parsed->scale_factor = ui::SCALE_FACTOR_100P;
parsed->path_index = -1;
if (path.empty())
return false;
size_t parsed_index = 0;
if (HasSubstringAt(path, parsed_index, kLargestParameter)) {
parsed_index += strlen(kLargestParameter);
parsed->size_in_dip = 0;
} else if (HasSubstringAt(path, parsed_index, kSizeParameter)) {
parsed_index += strlen(kSizeParameter);
size_t slash = path.find("/", parsed_index);
if (slash == std::string::npos)
return false;
size_t scale_delimiter = path.find("@", parsed_index);
std::string size_str;
std::string scale_str;
if (scale_delimiter == std::string::npos) {
size_str = path.substr(parsed_index, slash - parsed_index);
} else {
size_str = path.substr(parsed_index, scale_delimiter - parsed_index);
scale_str = path.substr(scale_delimiter + 1,
slash - scale_delimiter - 1);
}
if (!base::StringToInt(size_str, &parsed->size_in_dip))
return false;
if (parsed->size_in_dip != (gfx::kFaviconSize * 4) &&
parsed->size_in_dip != (gfx::kFaviconSize * 2)) {
parsed->size_in_dip = gfx::kFaviconSize;
}
if (!scale_str.empty())
webui::ParseScaleFactor(scale_str, &parsed->scale_factor);
if (parsed->size_in_dip != gfx::kFaviconSize &&
icon_types == chrome::FAVICON)
return false;
parsed_index = slash + 1;
}
if (HasSubstringAt(path, parsed_index, kIconURLParameter)) {
parsed_index += strlen(kIconURLParameter);
parsed->is_icon_url = true;
parsed->url = path.substr(parsed_index);
} else {
if (HasSubstringAt(path, parsed_index, kOriginParameter)) {
parsed_index += strlen(kOriginParameter);
std::string possibly_invalid_url = path.substr(parsed_index);
if (!GURL(possibly_invalid_url).has_scheme())
possibly_invalid_url = "http://" + possibly_invalid_url;
parsed->url = GURL(possibly_invalid_url).GetOrigin().spec();
} else {
parsed->url = path.substr(parsed_index);
}
}
parsed->path_index = parsed_index;
return true;
}
}