Commit 8b3af2bb authored by huangs's avatar huangs Committed by Commit bot

[Icons NTP] Add path_index() to ParsedFallbackIconPath.

Making ParsedFallbackIconPath resemble ParsedFaviconPath, to prepare it
for chrome-search:// URL with <view_id>/<restricted_id> . Details:
- Replacing url() with url_string(). Need to update chrome://large-icon
  endpoint and test to accommodate.
- Adding path_index().

BUG=467712

Review URL: https://codereview.chromium.org/1017803003

Cr-Commit-Position: refs/heads/master@{#321615}
parent 5e83cd09
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include "net/url_request/url_request.h" #include "net/url_request/url_request.h"
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/favicon_size.h" #include "ui/gfx/favicon_size.h"
#include "url/gurl.h"
FallbackIconSource::FallbackIconSource() { FallbackIconSource::FallbackIconSource() {
std::vector<std::string> font_list; std::vector<std::string> font_list;
...@@ -47,8 +48,11 @@ void FallbackIconSource::StartDataRequest( ...@@ -47,8 +48,11 @@ void FallbackIconSource::StartDataRequest(
SendDefaultResponse(callback); SendDefaultResponse(callback);
return; return;
} }
GURL url(parsed.url_string());
GURL url(parsed.url()); if (url.is_empty() || !url.is_valid()) {
SendDefaultResponse(callback);
return;
}
std::vector<unsigned char> bitmap_data = std::vector<unsigned char> bitmap_data =
fallback_icon_service_->RenderFallbackIconBitmap( fallback_icon_service_->RenderFallbackIconBitmap(
url, parsed.size_in_pixels(), parsed.style()); url, parsed.size_in_pixels(), parsed.style());
......
...@@ -54,10 +54,15 @@ bool ParsedFallbackIconPath::Parse(const std::string& path) { ...@@ -54,10 +54,15 @@ bool ParsedFallbackIconPath::Parse(const std::string& path) {
if (!ParseSpecs(spec_str, &size_in_pixels_, &style_)) if (!ParseSpecs(spec_str, &size_in_pixels_, &style_))
return false; // Parse failed. return false; // Parse failed.
// Extract URL, which may be empty (if first slash appears at the end). // Need to store the index of the URL field, so Instant Extended can translate
std::string url_str = path.substr(slash + 1); // fallback icon URLs using advanced parameters.
url_ = GURL(url_str); // Example:
return url_str.empty() || url_.is_valid(); // Allow empty URL. // "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 // static
......
...@@ -11,7 +11,6 @@ ...@@ -11,7 +11,6 @@
#include "base/macros.h" #include "base/macros.h"
#include "components/favicon_base/fallback_icon_style.h" #include "components/favicon_base/fallback_icon_style.h"
#include "third_party/skia/include/core/SkColor.h" #include "third_party/skia/include/core/SkColor.h"
#include "url/gurl.h"
namespace chrome { namespace chrome {
...@@ -20,12 +19,14 @@ class ParsedFallbackIconPath { ...@@ -20,12 +19,14 @@ class ParsedFallbackIconPath {
ParsedFallbackIconPath(); ParsedFallbackIconPath();
~ParsedFallbackIconPath(); ~ParsedFallbackIconPath();
const GURL& url() const { return url_; } const std::string& url_string() const { return url_string_; }
int size_in_pixels() const { return size_in_pixels_; } int size_in_pixels() const { return size_in_pixels_; }
const favicon_base::FallbackIconStyle& style() const { return style_; } const favicon_base::FallbackIconStyle& style() const { return style_; }
size_t path_index() const { return path_index_; }
// Parses |path|, which should be in the format described at the top of the // Parses |path|, which should be in the format described at the top of the
// file "chrome/browser/ui/webui/fallback_icon_source.h". // file "chrome/browser/ui/webui/fallback_icon_source.h".
bool Parse(const std::string& path); bool Parse(const std::string& path);
...@@ -50,8 +51,8 @@ class ParsedFallbackIconPath { ...@@ -50,8 +51,8 @@ class ParsedFallbackIconPath {
FRIEND_TEST_ALL_PREFIXES(FallbackIconUrlParserTest, ParseSpecsFull); FRIEND_TEST_ALL_PREFIXES(FallbackIconUrlParserTest, ParseSpecsFull);
FRIEND_TEST_ALL_PREFIXES(FallbackIconUrlParserTest, ParseSpecsFailure); FRIEND_TEST_ALL_PREFIXES(FallbackIconUrlParserTest, ParseSpecsFailure);
// The page URL the fallback icon is requested for. // The page URL string the fallback icon is requested for.
GURL url_; std::string url_string_;
// The size of the requested fallback icon in pixels. // The size of the requested fallback icon in pixels.
int size_in_pixels_; int size_in_pixels_;
...@@ -59,6 +60,10 @@ class ParsedFallbackIconPath { ...@@ -59,6 +60,10 @@ class ParsedFallbackIconPath {
// Styling specifications of fallback icon. // Styling specifications of fallback icon.
favicon_base::FallbackIconStyle style_; favicon_base::FallbackIconStyle style_;
// The index of the first character (relative to the path) where the the URL
// from which the fallback icon is being requested is located.
size_t path_index_;
DISALLOW_COPY_AND_ASSIGN(ParsedFallbackIconPath); DISALLOW_COPY_AND_ASSIGN(ParsedFallbackIconPath);
}; };
......
...@@ -213,39 +213,57 @@ TEST_F(FallbackIconUrlParserTest, ParseFallbackIconPathSuccess) { ...@@ -213,39 +213,57 @@ TEST_F(FallbackIconUrlParserTest, ParseFallbackIconPathSuccess) {
{ {
chrome::ParsedFallbackIconPath parsed; chrome::ParsedFallbackIconPath parsed;
EXPECT_TRUE(parsed.Parse(specs + "/" + kTestUrlStr)); EXPECT_TRUE(parsed.Parse(specs + "/" + kTestUrlStr));
EXPECT_EQ(GURL(kTestUrlStr), parsed.url());
EXPECT_EQ(31, parsed.size_in_pixels()); EXPECT_EQ(31, parsed.size_in_pixels());
const favicon_base::FallbackIconStyle& style = parsed.style(); const favicon_base::FallbackIconStyle& style = parsed.style();
EXPECT_EQ(SkColorSetRGB(0x00, 0x00, 0x00), style.background_color); EXPECT_EQ(SkColorSetRGB(0x00, 0x00, 0x00), style.background_color);
EXPECT_EQ(SkColorSetRGB(0xFF, 0xFF, 0xFF), style.text_color); EXPECT_EQ(SkColorSetRGB(0xFF, 0xFF, 0xFF), style.text_color);
EXPECT_EQ(0.75, style.font_size_ratio); EXPECT_EQ(0.75, style.font_size_ratio);
EXPECT_EQ(0.25, style.roundness); EXPECT_EQ(0.25, style.roundness);
EXPECT_EQ(GURL(kTestUrlStr), GURL(parsed.url_string()));
EXPECT_EQ(specs.length() + 1, parsed.path_index());
} }
// Empty URL. // Empty URL.
{ {
chrome::ParsedFallbackIconPath parsed; chrome::ParsedFallbackIconPath parsed;
EXPECT_TRUE(parsed.Parse(specs + "/")); EXPECT_TRUE(parsed.Parse(specs + "/"));
EXPECT_EQ(GURL(), parsed.url());
EXPECT_EQ(31, parsed.size_in_pixels()); EXPECT_EQ(31, parsed.size_in_pixels());
const favicon_base::FallbackIconStyle& style = parsed.style(); const favicon_base::FallbackIconStyle& style = parsed.style();
EXPECT_EQ(SkColorSetRGB(0x00, 0x00, 0x00), style.background_color); EXPECT_EQ(SkColorSetRGB(0x00, 0x00, 0x00), style.background_color);
EXPECT_EQ(SkColorSetRGB(0xFF, 0xFF, 0xFF), style.text_color); EXPECT_EQ(SkColorSetRGB(0xFF, 0xFF, 0xFF), style.text_color);
EXPECT_EQ(0.75, style.font_size_ratio); EXPECT_EQ(0.75, style.font_size_ratio);
EXPECT_EQ(0.25, style.roundness); EXPECT_EQ(0.25, style.roundness);
EXPECT_EQ(GURL(), GURL(parsed.url_string()));
EXPECT_EQ(specs.length() + 1, parsed.path_index());
}
// Tolerate invalid URL.
{
chrome::ParsedFallbackIconPath parsed;
EXPECT_TRUE(parsed.Parse(specs + "/NOT A VALID URL"));
EXPECT_EQ(31, parsed.size_in_pixels());
const favicon_base::FallbackIconStyle& style = parsed.style();
EXPECT_EQ(SkColorSetRGB(0x00, 0x00, 0x00), style.background_color);
EXPECT_EQ(SkColorSetRGB(0xFF, 0xFF, 0xFF), style.text_color);
EXPECT_EQ(0.75, style.font_size_ratio);
EXPECT_EQ(0.25, style.roundness);
EXPECT_EQ("NOT A VALID URL", parsed.url_string());
EXPECT_EQ(specs.length() + 1, parsed.path_index());
} }
// Size and style are default. // Size and style are default.
{ {
std::string specs2 = ",,,,";
chrome::ParsedFallbackIconPath parsed; chrome::ParsedFallbackIconPath parsed;
EXPECT_TRUE(parsed.Parse(std::string(",,,,/") + kTestUrlStr)); EXPECT_TRUE(parsed.Parse(specs2 + "/" + kTestUrlStr));
EXPECT_EQ(GURL(kTestUrlStr), parsed.url());
EXPECT_EQ(gfx::kFaviconSize, parsed.size_in_pixels()); EXPECT_EQ(gfx::kFaviconSize, parsed.size_in_pixels());
const favicon_base::FallbackIconStyle& style = parsed.style(); const favicon_base::FallbackIconStyle& style = parsed.style();
EXPECT_EQ(kDefaultBackgroundColor, style.background_color); EXPECT_EQ(kDefaultBackgroundColor, style.background_color);
EXPECT_EQ(kDefaultTextColorLight, style.text_color); EXPECT_EQ(kDefaultTextColorLight, style.text_color);
EXPECT_EQ(kDefaultFontSizeRatio, style.font_size_ratio); EXPECT_EQ(kDefaultFontSizeRatio, style.font_size_ratio);
EXPECT_EQ(kDefaultRoundness, style.roundness); EXPECT_EQ(kDefaultRoundness, style.roundness);
EXPECT_EQ(GURL(kTestUrlStr), GURL(parsed.url_string()));
EXPECT_EQ(specs2.length() + 1, parsed.path_index());
} }
} }
...@@ -255,8 +273,6 @@ TEST_F(FallbackIconUrlParserTest, ParseFallbackIconPathFailure) { ...@@ -255,8 +273,6 @@ TEST_F(FallbackIconUrlParserTest, ParseFallbackIconPathFailure) {
"-1,000,fff,0.75,0.25/http://www.google.com/", "-1,000,fff,0.75,0.25/http://www.google.com/",
// Bad specs. // Bad specs.
"32,#junk,fff,0.75,0.25/http://www.google.com/", "32,#junk,fff,0.75,0.25/http://www.google.com/",
// Bad URL.
"32,000,fff,0.75,0.25/NOT A VALID URL",
}; };
for (size_t i = 0; i < arraysize(test_cases); ++i) { for (size_t i = 0; i < arraysize(test_cases); ++i) {
chrome::ParsedFallbackIconPath parsed; chrome::ParsedFallbackIconPath parsed;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment