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