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"); | 22 DCHECK(fallback_icon_service_); |
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 } | 23 } |
31 | 24 |
32 FallbackIconSource::~FallbackIconSource() { | 25 FallbackIconSource::~FallbackIconSource() { |
33 } | 26 } |
34 | 27 |
35 std::string FallbackIconSource::GetSource() const { | 28 std::string FallbackIconSource::GetSource() const { |
36 return chrome::kChromeUIFallbackIconHost; | 29 return chrome::kChromeUIFallbackIconHost; |
37 } | 30 } |
38 | 31 |
39 void FallbackIconSource::StartDataRequest( | 32 void FallbackIconSource::StartDataRequest( |
40 const std::string& path, | 33 const std::string& path, |
41 int render_process_id, | 34 int render_process_id, |
42 int render_frame_id, | 35 int render_frame_id, |
43 const content::URLDataSource::GotDataCallback& callback) { | 36 const content::URLDataSource::GotDataCallback& callback) { |
44 chrome::ParsedFallbackIconPath parsed; | 37 chrome::ParsedFallbackIconPath parsed; |
45 bool success = parsed.Parse(path); | 38 bool success = parsed.Parse(path); |
46 if (!success) { | 39 if (!success) { |
47 SendDefaultResponse(callback); | 40 SendDefaultResponse(callback); |
48 return; | 41 return; |
49 } | 42 } |
| 43 |
50 GURL url(parsed.url_string()); | 44 GURL url(parsed.url_string()); |
51 if (url.is_empty() || !url.is_valid()) { | 45 if (url.is_empty() || !url.is_valid()) { |
52 SendDefaultResponse(callback); | 46 SendDefaultResponse(callback); |
53 return; | 47 return; |
54 } | 48 } |
55 std::vector<unsigned char> bitmap_data = | 49 |
56 fallback_icon_service_->RenderFallbackIconBitmap( | 50 SendFallbackIconHelper( |
57 url, parsed.size_in_pixels(), parsed.style()); | 51 url, parsed.size_in_pixels(), parsed.style(), callback); |
58 callback.Run(base::RefCountedBytes::TakeVector(&bitmap_data)); | |
59 } | 52 } |
60 | 53 |
61 std::string FallbackIconSource::GetMimeType(const std::string&) const { | 54 std::string FallbackIconSource::GetMimeType(const std::string&) const { |
62 // We need to explicitly return a mime type, otherwise if the user tries to | 55 // We need to explicitly return a mime type, otherwise if the user tries to |
63 // drag the image they get no extension. | 56 // drag the image they get no extension. |
64 return "image/png"; | 57 return "image/png"; |
65 } | 58 } |
66 | 59 |
67 bool FallbackIconSource::ShouldReplaceExistingSource() const { | 60 bool FallbackIconSource::ShouldReplaceExistingSource() const { |
68 // Leave the existing DataSource in place, otherwise we'll drop any pending | 61 // Leave the existing DataSource in place, otherwise we'll drop any pending |
69 // requests on the floor. | 62 // requests on the floor. |
70 return false; | 63 return false; |
71 } | 64 } |
72 | 65 |
73 bool FallbackIconSource::ShouldServiceRequest( | 66 bool FallbackIconSource::ShouldServiceRequest( |
74 const net::URLRequest* request) const { | 67 const net::URLRequest* request) const { |
75 if (request->url().SchemeIs(chrome::kChromeSearchScheme)) | 68 if (request->url().SchemeIs(chrome::kChromeSearchScheme)) |
76 return InstantIOContext::ShouldServiceRequest(request); | 69 return InstantIOContext::ShouldServiceRequest(request); |
77 return URLDataSource::ShouldServiceRequest(request); | 70 return URLDataSource::ShouldServiceRequest(request); |
78 } | 71 } |
79 | 72 |
| 73 void FallbackIconSource::SendFallbackIconHelper( |
| 74 const GURL& url, |
| 75 int size_in_pixels, |
| 76 const favicon_base::FallbackIconStyle& style, |
| 77 const content::URLDataSource::GotDataCallback& callback) { |
| 78 std::vector<unsigned char> bitmap_data = |
| 79 fallback_icon_service_->RenderFallbackIconBitmap( |
| 80 url, size_in_pixels, style); |
| 81 callback.Run(base::RefCountedBytes::TakeVector(&bitmap_data)); |
| 82 } |
| 83 |
80 void FallbackIconSource::SendDefaultResponse( | 84 void FallbackIconSource::SendDefaultResponse( |
81 const content::URLDataSource::GotDataCallback& callback) { | 85 const content::URLDataSource::GotDataCallback& callback) { |
82 std::vector<unsigned char> bitmap_data = | 86 favicon_base::FallbackIconStyle default_style; |
83 fallback_icon_service_->RenderFallbackIconBitmap( | 87 SendFallbackIconHelper(GURL(), gfx::kFaviconSize, default_style, callback); |
84 GURL(), gfx::kFaviconSize, favicon_base::FallbackIconStyle()); | |
85 callback.Run(base::RefCountedBytes::TakeVector(&bitmap_data)); | |
86 } | 88 } |
OLD | NEW |