| 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 <vector> | |
| 8 | |
| 9 #include "base/memory/ref_counted_memory.h" | |
| 10 #include "chrome/browser/search/instant_io_context.h" | |
| 11 #include "chrome/common/url_constants.h" | |
| 12 #include "components/favicon/core/large_icon_service.h" | |
| 13 #include "components/favicon_base/fallback_icon_style.h" | |
| 14 #include "components/favicon_base/favicon_types.h" | |
| 15 #include "components/favicon_base/large_icon_url_parser.h" | |
| 16 #include "net/url_request/url_request.h" | |
| 17 #include "third_party/skia/include/core/SkBitmap.h" | |
| 18 #include "ui/gfx/codec/png_codec.h" | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 const int kMaxLargeIconSize = 192; // Arbitrary bound to safeguard endpoint. | |
| 23 | |
| 24 } // namespace | |
| 25 | |
| 26 LargeIconSource::LargeIconSource(favicon::LargeIconService* large_icon_service) | |
| 27 : large_icon_service_(large_icon_service) {} | |
| 28 | |
| 29 LargeIconSource::~LargeIconSource() { | |
| 30 } | |
| 31 | |
| 32 std::string LargeIconSource::GetSource() const { | |
| 33 return chrome::kChromeUILargeIconHost; | |
| 34 } | |
| 35 | |
| 36 void LargeIconSource::StartDataRequest( | |
| 37 const std::string& path, | |
| 38 const content::ResourceRequestInfo::WebContentsGetter& wc_getter, | |
| 39 const content::URLDataSource::GotDataCallback& callback) { | |
| 40 if (!large_icon_service_) { | |
| 41 SendNotFoundResponse(callback); | |
| 42 return; | |
| 43 } | |
| 44 | |
| 45 LargeIconUrlParser parser; | |
| 46 bool success = parser.Parse(path); | |
| 47 if (!success || | |
| 48 parser.size_in_pixels() <= 0 || | |
| 49 parser.size_in_pixels() > kMaxLargeIconSize) { | |
| 50 SendNotFoundResponse(callback); | |
| 51 return; | |
| 52 } | |
| 53 | |
| 54 GURL url(parser.url_string()); | |
| 55 if (!url.is_valid()) { | |
| 56 SendNotFoundResponse(callback); | |
| 57 return; | |
| 58 } | |
| 59 | |
| 60 // TODO(beaudoin): Potentially allow icon to be scaled up. | |
| 61 large_icon_service_->GetLargeIconOrFallbackStyle( | |
| 62 url, | |
| 63 parser.size_in_pixels(), // Reducing this will enable scale up. | |
| 64 parser.size_in_pixels(), | |
| 65 base::Bind(&LargeIconSource::OnLargeIconDataAvailable, | |
| 66 base::Unretained(this), callback, url, | |
| 67 parser.size_in_pixels()), | |
| 68 &cancelable_task_tracker_); | |
| 69 } | |
| 70 | |
| 71 std::string LargeIconSource::GetMimeType(const std::string&) const { | |
| 72 // We need to explicitly return a mime type, otherwise if the user tries to | |
| 73 // drag the image they get no extension. | |
| 74 return "image/png"; | |
| 75 } | |
| 76 | |
| 77 bool LargeIconSource::AllowCaching() const { | |
| 78 return false; | |
| 79 } | |
| 80 | |
| 81 bool LargeIconSource::ShouldReplaceExistingSource() const { | |
| 82 // Leave the existing DataSource in place, otherwise we'll drop any pending | |
| 83 // requests on the floor. | |
| 84 return false; | |
| 85 } | |
| 86 | |
| 87 bool LargeIconSource::ShouldServiceRequest( | |
| 88 const GURL& url, | |
| 89 content::ResourceContext* resource_context, | |
| 90 int render_process_id) const { | |
| 91 if (url.SchemeIs(chrome::kChromeSearchScheme)) { | |
| 92 return InstantIOContext::ShouldServiceRequest(url, resource_context, | |
| 93 render_process_id); | |
| 94 } | |
| 95 return URLDataSource::ShouldServiceRequest(url, resource_context, | |
| 96 render_process_id); | |
| 97 } | |
| 98 | |
| 99 void LargeIconSource::OnLargeIconDataAvailable( | |
| 100 const content::URLDataSource::GotDataCallback& callback, | |
| 101 const GURL& url, | |
| 102 int size, | |
| 103 const favicon_base::LargeIconResult& result) { | |
| 104 if (result.bitmap.is_valid()) { | |
| 105 callback.Run(result.bitmap.bitmap_data.get()); | |
| 106 return; | |
| 107 } | |
| 108 | |
| 109 if (!result.fallback_icon_style) { | |
| 110 SendNotFoundResponse(callback); | |
| 111 return; | |
| 112 } | |
| 113 | |
| 114 // RenderFallbackIconBitmap() cannot draw fallback icons on Android. See | |
| 115 // crbug.com/580922 for details. Return a 1x1 bitmap so that JavaScript can | |
| 116 // detect that it needs to generate a fallback icon. | |
| 117 SkBitmap bitmap; | |
| 118 bitmap.allocN32Pixels(1, 1); | |
| 119 bitmap.eraseColor(result.fallback_icon_style->background_color); | |
| 120 std::vector<unsigned char> bitmap_data; | |
| 121 if (!gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &bitmap_data)) | |
| 122 bitmap_data.clear(); | |
| 123 | |
| 124 callback.Run(base::RefCountedBytes::TakeVector(&bitmap_data)); | |
| 125 } | |
| 126 | |
| 127 void LargeIconSource::SendNotFoundResponse( | |
| 128 const content::URLDataSource::GotDataCallback& callback) { | |
| 129 callback.Run(nullptr); | |
| 130 } | |
| OLD | NEW |