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