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/large_icon_source.h" | 5 #include "chrome/browser/ui/webui/large_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/favicon/favicon_service_factory.h" | |
12 #include "chrome/browser/profiles/profile.h" | |
13 #include "chrome/browser/search/instant_io_context.h" | 10 #include "chrome/browser/search/instant_io_context.h" |
14 #include "chrome/common/favicon/large_icon_url_parser.h" | 11 #include "chrome/common/favicon/large_icon_url_parser.h" |
15 #include "chrome/common/url_constants.h" | 12 #include "chrome/common/url_constants.h" |
| 13 #include "components/favicon/core/fallback_icon_service.h" |
16 #include "components/favicon/core/favicon_service.h" | 14 #include "components/favicon/core/favicon_service.h" |
17 #include "components/favicon_base/fallback_icon_style.h" | 15 #include "components/favicon_base/fallback_icon_style.h" |
18 #include "grit/platform_locale_settings.h" | |
19 #include "net/url_request/url_request.h" | 16 #include "net/url_request/url_request.h" |
20 #include "third_party/skia/include/core/SkColor.h" | 17 #include "third_party/skia/include/core/SkColor.h" |
21 #include "ui/base/l10n/l10n_util.h" | |
22 | 18 |
23 namespace { | 19 namespace { |
24 | 20 |
25 int kDefaultLargeIconSize = 96; | 21 int kDefaultLargeIconSize = 96; |
26 int kMaxLargeIconSize = 192; // Arbitrary bound to safeguard endpoint. | 22 int kMaxLargeIconSize = 192; // Arbitrary bound to safeguard endpoint. |
27 | 23 |
28 } // namespace | 24 } // namespace |
29 | 25 |
30 LargeIconSource::IconRequest::IconRequest() : size(kDefaultLargeIconSize) { | 26 LargeIconSource::IconRequest::IconRequest() : size(kDefaultLargeIconSize) { |
31 } | 27 } |
32 | 28 |
33 LargeIconSource::IconRequest::IconRequest( | 29 LargeIconSource::IconRequest::IconRequest( |
34 const content::URLDataSource::GotDataCallback& callback_in, | 30 const content::URLDataSource::GotDataCallback& callback_in, |
35 const GURL& url_in, | 31 const GURL& url_in, |
36 int size_in) | 32 int size_in) |
37 : callback(callback_in), | 33 : callback(callback_in), |
38 url(url_in), | 34 url(url_in), |
39 size(size_in) { | 35 size(size_in) { |
40 } | 36 } |
41 | 37 |
42 LargeIconSource::IconRequest::~IconRequest() { | 38 LargeIconSource::IconRequest::~IconRequest() { |
43 } | 39 } |
44 | 40 |
45 LargeIconSource::LargeIconSource(Profile* profile) : profile_(profile) { | 41 LargeIconSource::LargeIconSource(FaviconService* favicon_service, |
46 std::vector<std::string> font_list; | 42 FallbackIconService* fallback_icon_service) |
47 #if defined(OS_CHROMEOS) | 43 : favicon_service_(favicon_service), |
48 font_list.push_back("Noto Sans"); | 44 fallback_icon_service_(fallback_icon_service) { |
49 #elif defined(OS_IOS) | |
50 font_list.push_back("Helvetica Neue"); | |
51 #else | |
52 font_list.push_back(l10n_util::GetStringUTF8(IDS_SANS_SERIF_FONT_FAMILY)); | |
53 #endif | |
54 fallback_icon_service_.reset(new FallbackIconService(font_list)); | |
55 } | 45 } |
56 | 46 |
57 LargeIconSource::~LargeIconSource() { | 47 LargeIconSource::~LargeIconSource() { |
58 } | 48 } |
59 | 49 |
60 std::string LargeIconSource::GetSource() const { | 50 std::string LargeIconSource::GetSource() const { |
61 return chrome::kChromeUILargeIconHost; | 51 return chrome::kChromeUILargeIconHost; |
62 } | 52 } |
63 | 53 |
64 void LargeIconSource::StartDataRequest( | 54 void LargeIconSource::StartDataRequest( |
65 const std::string& path, | 55 const std::string& path, |
66 int render_process_id, | 56 int render_process_id, |
67 int render_frame_id, | 57 int render_frame_id, |
68 const content::URLDataSource::GotDataCallback& callback) { | 58 const content::URLDataSource::GotDataCallback& callback) { |
| 59 if (!favicon_service_) { |
| 60 SendNotFoundResponse(callback); |
| 61 return; |
| 62 } |
| 63 |
69 LargeIconUrlParser parser; | 64 LargeIconUrlParser parser; |
70 bool success = parser.Parse(path); | 65 bool success = parser.Parse(path); |
71 if (!success || parser.size_in_pixels() <= 0 || | 66 if (!success || |
| 67 parser.size_in_pixels() <= 0 || |
72 parser.size_in_pixels() > kMaxLargeIconSize) { | 68 parser.size_in_pixels() > kMaxLargeIconSize) { |
73 SendNotFoundResponse(callback); | 69 SendNotFoundResponse(callback); |
74 return; | 70 return; |
75 } | 71 } |
76 | |
77 FaviconService* favicon_service = FaviconServiceFactory::GetForProfile( | |
78 profile_, ServiceAccessType::EXPLICIT_ACCESS); | |
79 if (!favicon_service) { | |
80 SendNotFoundResponse(callback); | |
81 return; | |
82 } | |
83 | 72 |
84 GURL url(parser.url_string()); | 73 GURL url(parser.url_string()); |
85 if (!url.is_valid()) { | 74 if (!url.is_valid()) { |
86 SendNotFoundResponse(callback); | 75 SendNotFoundResponse(callback); |
87 return; | 76 return; |
88 } | 77 } |
89 | 78 |
90 favicon_service->GetRawFaviconForPageURL( | 79 favicon_service_->GetRawFaviconForPageURL( |
91 url, | 80 url, |
92 favicon_base::TOUCH_ICON | favicon_base::TOUCH_PRECOMPOSED_ICON, | 81 favicon_base::TOUCH_ICON | favicon_base::TOUCH_PRECOMPOSED_ICON, |
93 parser.size_in_pixels(), | 82 parser.size_in_pixels(), |
94 base::Bind( | 83 base::Bind( |
95 &LargeIconSource::OnIconDataAvailable, | 84 &LargeIconSource::OnIconDataAvailable, |
96 base::Unretained(this), | 85 base::Unretained(this), |
97 IconRequest(callback, url, parser.size_in_pixels())), | 86 IconRequest(callback, url, parser.size_in_pixels())), |
98 &cancelable_task_tracker_); | 87 &cancelable_task_tracker_); |
99 } | 88 } |
100 | 89 |
(...skipping 19 matching lines...) Expand all Loading... |
120 void LargeIconSource::OnIconDataAvailable( | 109 void LargeIconSource::OnIconDataAvailable( |
121 const IconRequest& request, | 110 const IconRequest& request, |
122 const favicon_base::FaviconRawBitmapResult& bitmap_result) { | 111 const favicon_base::FaviconRawBitmapResult& bitmap_result) { |
123 if (!bitmap_result.is_valid()) | 112 if (!bitmap_result.is_valid()) |
124 SendFallbackIcon(request); | 113 SendFallbackIcon(request); |
125 else | 114 else |
126 request.callback.Run(bitmap_result.bitmap_data.get()); | 115 request.callback.Run(bitmap_result.bitmap_data.get()); |
127 } | 116 } |
128 | 117 |
129 void LargeIconSource::SendFallbackIcon(const IconRequest& request) { | 118 void LargeIconSource::SendFallbackIcon(const IconRequest& request) { |
| 119 if (!fallback_icon_service_) { |
| 120 SendNotFoundResponse(request.callback); |
| 121 return; |
| 122 } |
130 favicon_base::FallbackIconStyle style; | 123 favicon_base::FallbackIconStyle style; |
131 style.background_color = SkColorSetRGB(0xcc, 0xcc, 0xcc); | 124 style.background_color = SkColorSetRGB(0xcc, 0xcc, 0xcc); |
132 favicon_base::MatchFallbackIconTextColorAgainstBackgroundColor(&style); | 125 favicon_base::MatchFallbackIconTextColorAgainstBackgroundColor(&style); |
133 style.roundness = 1.0; | 126 style.roundness = 1.0; |
134 std::vector<unsigned char> bitmap_data = | 127 std::vector<unsigned char> bitmap_data = |
135 fallback_icon_service_->RenderFallbackIconBitmap( | 128 fallback_icon_service_->RenderFallbackIconBitmap( |
136 request.url, request.size, style); | 129 request.url, request.size, style); |
137 request.callback.Run(base::RefCountedBytes::TakeVector(&bitmap_data)); | 130 request.callback.Run(base::RefCountedBytes::TakeVector(&bitmap_data)); |
138 } | 131 } |
139 | 132 |
140 void LargeIconSource::SendNotFoundResponse( | 133 void LargeIconSource::SendNotFoundResponse( |
141 const content::URLDataSource::GotDataCallback& callback) { | 134 const content::URLDataSource::GotDataCallback& callback) { |
142 callback.Run(nullptr); | 135 callback.Run(nullptr); |
143 } | 136 } |
OLD | NEW |