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