Chromium Code Reviews| 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/big_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.h" | |
| 12 #include "chrome/browser/favicon/favicon_service_factory.h" | |
| 13 #include "chrome/browser/profiles/profile.h" | |
| 14 #include "chrome/browser/search/instant_io_context.h" | |
| 15 #include "chrome/common/favicon/big_icon_url_parser.h" | |
| 16 #include "chrome/common/url_constants.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 #include "ui/gfx/favicon_size.h" | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 int kFallbackIconSize = 96; | |
| 27 | |
| 28 } // namespace | |
| 29 | |
| 30 BigIconSource::IconRequest::IconRequest() : size(48) { | |
| 31 } | |
| 32 | |
| 33 BigIconSource::IconRequest::IconRequest( | |
| 34 const content::URLDataSource::GotDataCallback& callback_in, | |
| 35 const GURL& path_in, | |
| 36 int size_in) | |
| 37 : callback(callback_in), | |
| 38 path(path_in), | |
| 39 size(size_in) { | |
| 40 } | |
| 41 | |
| 42 BigIconSource::IconRequest::~IconRequest() { | |
| 43 } | |
| 44 | |
| 45 BigIconSource::BigIconSource(Profile* profile) | |
| 46 : profile_(profile), | |
| 47 render_fallback_on_failure_(true) { | |
| 48 std::vector<std::string> font_list; | |
| 49 #if defined(OS_CHROMEOS) | |
| 50 font_list.push_back("Noto Sans"); | |
| 51 #elif defined(OS_IOS) | |
| 52 font_list.push_back("Helvetica Neue"); | |
| 53 #else | |
| 54 font_list.push_back(l10n_util::GetStringUTF8(IDS_SANS_SERIF_FONT_FAMILY)); | |
| 55 #endif | |
| 56 fallback_icon_service_.reset( | |
| 57 new favicon_base::FallbackIconService(font_list)); | |
| 58 } | |
| 59 | |
| 60 BigIconSource::~BigIconSource() { | |
| 61 } | |
| 62 | |
| 63 std::string BigIconSource::GetSource() const { | |
| 64 return chrome::kChromeUIBigIconHost; | |
| 65 } | |
| 66 | |
| 67 void BigIconSource::StartDataRequest( | |
| 68 const std::string& path, | |
| 69 int render_process_id, | |
| 70 int render_frame_id, | |
| 71 const content::URLDataSource::GotDataCallback& callback) { | |
| 72 chrome::BigIconUrlParser parser; | |
| 73 bool success = parser.Parse(path); | |
| 74 if (!success) { | |
| 75 SendDefaultResponse(callback); | |
| 76 return; | |
| 77 } | |
| 78 | |
| 79 FaviconService* favicon_service = FaviconServiceFactory::GetForProfile( | |
| 80 profile_, ServiceAccessType::EXPLICIT_ACCESS); | |
| 81 if (!favicon_service) { | |
| 82 SendDefaultResponse(callback); | |
| 83 return; | |
| 84 } | |
| 85 | |
| 86 GURL url(parser.url_string()); | |
| 87 if (!url.is_valid()) { | |
| 88 SendDefaultResponse(callback); | |
| 89 return; | |
| 90 } | |
| 91 favicon_service->HackGetBigIcon( | |
| 92 url, | |
| 93 parser.size_in_pixels(), | |
| 94 base::Bind( | |
| 95 &BigIconSource::OnBigIconDataAvailable, | |
| 96 base::Unretained(this), | |
| 97 IconRequest(callback, url, parser.size_in_pixels())), | |
| 98 &cancelable_task_tracker_); | |
| 99 } | |
| 100 | |
| 101 std::string BigIconSource::GetMimeType(const std::string&) const { | |
| 102 // We need to explicitly return a mime type, otherwise if the user tries to | |
| 103 // drag the image they get no extension. | |
| 104 return "image/png"; | |
| 105 } | |
| 106 | |
| 107 bool BigIconSource::ShouldReplaceExistingSource() const { | |
| 108 // Leave the existing DataSource in place, otherwise we'll drop any pending | |
| 109 // requests on the floor. | |
| 110 return false; | |
| 111 } | |
| 112 | |
| 113 bool BigIconSource::ShouldServiceRequest( | |
| 114 const net::URLRequest* request) const { | |
| 115 if (request->url().SchemeIs(chrome::kChromeSearchScheme)) | |
| 116 return InstantIOContext::ShouldServiceRequest(request); | |
| 117 return URLDataSource::ShouldServiceRequest(request); | |
| 118 } | |
| 119 | |
| 120 void BigIconSource::OnBigIconDataAvailable( | |
| 121 IconRequest request, | |
| 122 const favicon_base::FaviconRawBitmapResult& bitmap_result) { | |
| 123 if (bitmap_result.is_valid()) { | |
| 124 // TODO: Resize the big icon to request.size. | |
| 125 // Need to do so here because otherwise NTP can deduce by size whether user | |
| 126 // has visited a site! | |
|
Roger McFarlane (Chromium)
2015/03/16 19:00:59
assuming:
(1) non-privileged access is allowed fr
huangs
2015/03/17 01:43:52
Yup! Raw bitmap sizes are pretty much arbitrary.
| |
| 127 | |
| 128 // Forward the data along to the networking system. | |
| 129 request.callback.Run(bitmap_result.bitmap_data.get()); | |
| 130 } else if (render_fallback_on_failure_) { | |
| 131 SendFallbackIcon(request.path, request.callback); | |
| 132 } else { | |
| 133 SendDefaultResponse(request.callback); | |
| 134 } | |
| 135 } | |
| 136 | |
| 137 void BigIconSource::SendFallbackIcon( | |
| 138 const GURL& url, | |
| 139 const content::URLDataSource::GotDataCallback& callback) { | |
| 140 favicon_base::FallbackIconStyle style; | |
| 141 style.background_color = SkColorSetRGB(0xcc, 0xcc, 0xcc); | |
| 142 favicon_base::MatchFallbackIconTextColorAgainstBackgroundColor(&style); | |
| 143 style.roundness = 1.0; | |
| 144 std::vector<unsigned char> bitmap_data = | |
| 145 fallback_icon_service_->RenderFallbackIconBitmap( | |
| 146 url, kFallbackIconSize, style); | |
| 147 callback.Run(base::RefCountedBytes::TakeVector(&bitmap_data)); | |
| 148 } | |
| 149 | |
| 150 void BigIconSource::SendDefaultResponse( | |
|
Roger McFarlane (Chromium)
2015/03/16 19:00:59
is this really a default? or is it an error respon
huangs
2015/03/17 01:43:52
Renamed.
| |
| 151 const content::URLDataSource::GotDataCallback& callback) { | |
| 152 callback.Run(nullptr); | |
| 153 } | |
| OLD | NEW |