| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/webui/fallback_icon_source.h" | 5 #include "chrome/browser/ui/webui/fallback_icon_source.h" |
| 6 | 6 |
| 7 #include <string> | |
| 8 #include <vector> | 7 #include <vector> |
| 9 | 8 |
| 10 #include "base/memory/ref_counted_memory.h" | 9 #include "base/memory/ref_counted_memory.h" |
| 11 #include "chrome/browser/search/instant_io_context.h" | 10 #include "chrome/browser/search/instant_io_context.h" |
| 12 #include "chrome/common/favicon/fallback_icon_url_parser.h" | 11 #include "chrome/common/favicon/fallback_icon_url_parser.h" |
| 13 #include "chrome/common/url_constants.h" | 12 #include "chrome/common/url_constants.h" |
| 14 #include "grit/platform_locale_settings.h" | 13 #include "components/favicon/core/fallback_icon_service.h" |
| 14 #include "components/keyed_service/core/service_access_type.h" |
| 15 #include "net/url_request/url_request.h" | 15 #include "net/url_request/url_request.h" |
| 16 #include "ui/base/l10n/l10n_util.h" | |
| 17 #include "ui/gfx/favicon_size.h" | 16 #include "ui/gfx/favicon_size.h" |
| 18 #include "url/gurl.h" | 17 #include "url/gurl.h" |
| 19 | 18 |
| 20 FallbackIconSource::FallbackIconSource() { | 19 FallbackIconSource::FallbackIconSource( |
| 21 std::vector<std::string> font_list; | 20 FallbackIconService* fallback_icon_service) |
| 22 #if defined(OS_CHROMEOS) | 21 : fallback_icon_service_(fallback_icon_service) { |
| 23 font_list.push_back("Noto Sans"); | |
| 24 #elif defined(OS_IOS) | |
| 25 font_list.push_back("Helvetica Neue"); | |
| 26 #else | |
| 27 font_list.push_back(l10n_util::GetStringUTF8(IDS_SANS_SERIF_FONT_FAMILY)); | |
| 28 #endif | |
| 29 fallback_icon_service_.reset(new FallbackIconService(font_list)); | |
| 30 } | 22 } |
| 31 | 23 |
| 32 FallbackIconSource::~FallbackIconSource() { | 24 FallbackIconSource::~FallbackIconSource() { |
| 33 } | 25 } |
| 34 | 26 |
| 35 std::string FallbackIconSource::GetSource() const { | 27 std::string FallbackIconSource::GetSource() const { |
| 36 return chrome::kChromeUIFallbackIconHost; | 28 return chrome::kChromeUIFallbackIconHost; |
| 37 } | 29 } |
| 38 | 30 |
| 39 void FallbackIconSource::StartDataRequest( | 31 void FallbackIconSource::StartDataRequest( |
| 40 const std::string& path, | 32 const std::string& path, |
| 41 int render_process_id, | 33 int render_process_id, |
| 42 int render_frame_id, | 34 int render_frame_id, |
| 43 const content::URLDataSource::GotDataCallback& callback) { | 35 const content::URLDataSource::GotDataCallback& callback) { |
| 44 chrome::ParsedFallbackIconPath parsed; | 36 chrome::ParsedFallbackIconPath parsed; |
| 45 bool success = parsed.Parse(path); | 37 bool success = parsed.Parse(path); |
| 46 if (!success) { | 38 if (!success) { |
| 47 SendDefaultResponse(callback); | 39 SendDefaultResponse(callback); |
| 48 return; | 40 return; |
| 49 } | 41 } |
| 42 |
| 50 GURL url(parsed.url_string()); | 43 GURL url(parsed.url_string()); |
| 51 if (url.is_empty() || !url.is_valid()) { | 44 if (url.is_empty() || !url.is_valid()) { |
| 52 SendDefaultResponse(callback); | 45 SendDefaultResponse(callback); |
| 53 return; | 46 return; |
| 54 } | 47 } |
| 55 std::vector<unsigned char> bitmap_data = | 48 |
| 56 fallback_icon_service_->RenderFallbackIconBitmap( | 49 SendFallbackIconHelper( |
| 57 url, parsed.size_in_pixels(), parsed.style()); | 50 url, parsed.size_in_pixels(), parsed.style(), callback); |
| 58 callback.Run(base::RefCountedBytes::TakeVector(&bitmap_data)); | |
| 59 } | 51 } |
| 60 | 52 |
| 61 std::string FallbackIconSource::GetMimeType(const std::string&) const { | 53 std::string FallbackIconSource::GetMimeType(const std::string&) const { |
| 62 // We need to explicitly return a mime type, otherwise if the user tries to | 54 // We need to explicitly return a mime type, otherwise if the user tries to |
| 63 // drag the image they get no extension. | 55 // drag the image they get no extension. |
| 64 return "image/png"; | 56 return "image/png"; |
| 65 } | 57 } |
| 66 | 58 |
| 67 bool FallbackIconSource::ShouldReplaceExistingSource() const { | 59 bool FallbackIconSource::ShouldReplaceExistingSource() const { |
| 68 // Leave the existing DataSource in place, otherwise we'll drop any pending | 60 // Leave the existing DataSource in place, otherwise we'll drop any pending |
| 69 // requests on the floor. | 61 // requests on the floor. |
| 70 return false; | 62 return false; |
| 71 } | 63 } |
| 72 | 64 |
| 73 bool FallbackIconSource::ShouldServiceRequest( | 65 bool FallbackIconSource::ShouldServiceRequest( |
| 74 const net::URLRequest* request) const { | 66 const net::URLRequest* request) const { |
| 75 if (request->url().SchemeIs(chrome::kChromeSearchScheme)) | 67 if (request->url().SchemeIs(chrome::kChromeSearchScheme)) |
| 76 return InstantIOContext::ShouldServiceRequest(request); | 68 return InstantIOContext::ShouldServiceRequest(request); |
| 77 return URLDataSource::ShouldServiceRequest(request); | 69 return URLDataSource::ShouldServiceRequest(request); |
| 78 } | 70 } |
| 79 | 71 |
| 72 void FallbackIconSource::SendFallbackIconHelper( |
| 73 const GURL& url, |
| 74 int size_in_pixels, |
| 75 const favicon_base::FallbackIconStyle& style, |
| 76 const content::URLDataSource::GotDataCallback& callback) { |
| 77 if (!fallback_icon_service_) { |
| 78 SendNotFoundResponse(callback); |
| 79 return; |
| 80 } |
| 81 std::vector<unsigned char> bitmap_data = |
| 82 fallback_icon_service_->RenderFallbackIconBitmap( |
| 83 url, size_in_pixels, style); |
| 84 callback.Run(base::RefCountedBytes::TakeVector(&bitmap_data)); |
| 85 } |
| 86 |
| 80 void FallbackIconSource::SendDefaultResponse( | 87 void FallbackIconSource::SendDefaultResponse( |
| 81 const content::URLDataSource::GotDataCallback& callback) { | 88 const content::URLDataSource::GotDataCallback& callback) { |
| 82 std::vector<unsigned char> bitmap_data = | 89 favicon_base::FallbackIconStyle default_style; |
| 83 fallback_icon_service_->RenderFallbackIconBitmap( | 90 SendFallbackIconHelper(GURL(), gfx::kFaviconSize, default_style, callback); |
| 84 GURL(), gfx::kFaviconSize, favicon_base::FallbackIconStyle()); | |
| 85 callback.Run(base::RefCountedBytes::TakeVector(&bitmap_data)); | |
| 86 } | 91 } |
| 92 |
| 93 void FallbackIconSource::SendNotFoundResponse( |
| 94 const content::URLDataSource::GotDataCallback& callback) { |
| 95 callback.Run(nullptr); |
| 96 } |
| OLD | NEW |