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/large_icon_source.h" | |
6 | |
7 #include <string> | |
8 #include <vector> | |
9 | |
10 #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" | |
14 #include "chrome/common/favicon/large_icon_url_parser.h" | |
15 #include "chrome/common/url_constants.h" | |
16 #include "components/favicon/core/browser/favicon_service.h" | |
17 #include "components/favicon_base/fallback_icon_style.h" | |
18 #include "grit/platform_locale_settings.h" | |
19 #include "net/url_request/url_request.h" | |
20 #include "third_party/skia/include/core/SkColor.h" | |
21 #include "ui/base/l10n/l10n_util.h" | |
22 | |
23 namespace { | |
24 | |
25 int kDefaultLargeIconSize = 96; | |
26 int kMaxLargeIconSize = 192; // Arbitrary bound to safegard endpoint. | |
James Hawkins
2015/03/19 18:47:31
safeguard
| |
27 | |
28 } // namespace | |
29 | |
30 LargeIconSource::IconRequest::IconRequest() : size(kDefaultLargeIconSize) { | |
31 } | |
32 | |
33 LargeIconSource::IconRequest::IconRequest( | |
34 const content::URLDataSource::GotDataCallback& callback_in, | |
35 const GURL& url_in, | |
36 int size_in) | |
37 : callback(callback_in), | |
38 url(url_in), | |
39 size(size_in) { | |
40 } | |
41 | |
42 LargeIconSource::IconRequest::~IconRequest() { | |
43 } | |
44 | |
45 LargeIconSource::LargeIconSource(Profile* profile) : profile_(profile) { | |
46 std::vector<std::string> font_list; | |
47 #if defined(OS_CHROMEOS) | |
48 font_list.push_back("Noto Sans"); | |
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( | |
55 new favicon_base::FallbackIconService(font_list)); | |
56 } | |
57 | |
58 LargeIconSource::~LargeIconSource() { | |
59 } | |
60 | |
61 std::string LargeIconSource::GetSource() const { | |
62 return chrome::kChromeUILargeIconHost; | |
63 } | |
64 | |
65 void LargeIconSource::StartDataRequest( | |
66 const std::string& path, | |
67 int render_process_id, | |
68 int render_frame_id, | |
69 const content::URLDataSource::GotDataCallback& callback) { | |
70 LargeIconUrlParser parser; | |
71 bool success = parser.Parse(path); | |
72 if (!success || parser.size_in_pixels() <= 0 || | |
73 parser.size_in_pixels() > kMaxLargeIconSize) { | |
74 SendNotFoundResponse(callback); | |
75 return; | |
76 } | |
77 | |
78 FaviconService* favicon_service = FaviconServiceFactory::GetForProfile( | |
79 profile_, ServiceAccessType::EXPLICIT_ACCESS); | |
80 if (!favicon_service) { | |
81 SendNotFoundResponse(callback); | |
82 return; | |
83 } | |
84 | |
85 GURL url(parser.url_string()); | |
86 if (!url.is_valid()) { | |
87 SendNotFoundResponse(callback); | |
88 return; | |
89 } | |
90 | |
91 favicon_service->GetRawFaviconForPageURL( | |
92 url, | |
93 favicon_base::TOUCH_ICON | favicon_base::TOUCH_PRECOMPOSED_ICON, | |
94 parser.size_in_pixels(), | |
95 base::Bind( | |
96 &LargeIconSource::OnIconDataAvailable, | |
97 base::Unretained(this), | |
98 IconRequest(callback, url, parser.size_in_pixels())), | |
99 &cancelable_task_tracker_); | |
100 } | |
101 | |
102 std::string LargeIconSource::GetMimeType(const std::string&) const { | |
103 // We need to explicitly return a mime type, otherwise if the user tries to | |
104 // drag the image they get no extension. | |
105 return "image/png"; | |
106 } | |
107 | |
108 bool LargeIconSource::ShouldReplaceExistingSource() const { | |
109 // Leave the existing DataSource in place, otherwise we'll drop any pending | |
110 // requests on the floor. | |
111 return false; | |
112 } | |
113 | |
114 bool LargeIconSource::ShouldServiceRequest( | |
115 const net::URLRequest* request) const { | |
116 if (request->url().SchemeIs(chrome::kChromeSearchScheme)) | |
117 return InstantIOContext::ShouldServiceRequest(request); | |
118 return URLDataSource::ShouldServiceRequest(request); | |
119 } | |
120 | |
121 void LargeIconSource::OnIconDataAvailable( | |
122 const IconRequest& request, | |
123 const favicon_base::FaviconRawBitmapResult& bitmap_result) { | |
124 if (!bitmap_result.is_valid()) | |
125 SendFallbackIcon(request); | |
126 else | |
127 request.callback.Run(bitmap_result.bitmap_data.get()); | |
128 } | |
129 | |
130 void LargeIconSource::SendFallbackIcon(const IconRequest& request) { | |
131 favicon_base::FallbackIconStyle style; | |
132 style.background_color = SkColorSetRGB(0xcc, 0xcc, 0xcc); | |
133 favicon_base::MatchFallbackIconTextColorAgainstBackgroundColor(&style); | |
134 style.roundness = 1.0; | |
135 std::vector<unsigned char> bitmap_data = | |
136 fallback_icon_service_->RenderFallbackIconBitmap( | |
137 request.url, request.size, style); | |
138 request.callback.Run(base::RefCountedBytes::TakeVector(&bitmap_data)); | |
139 } | |
140 | |
141 void LargeIconSource::SendNotFoundResponse( | |
142 const content::URLDataSource::GotDataCallback& callback) { | |
143 callback.Run(nullptr); | |
144 } | |
OLD | NEW |