Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(437)

Side by Side Diff: chrome/renderer/chrome_render_frame_observer.cc

Issue 2943363003: Fixing transparent pixels appearing black when rendered for the context menu. (Closed)
Patch Set: Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/renderer/chrome_render_frame_observer.h" 5 #include "chrome/renderer/chrome_render_frame_observer.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <string.h> 8 #include <string.h>
9 9
10 #include <limits> 10 #include <limits>
(...skipping 25 matching lines...) Expand all
36 #include "third_party/WebKit/public/platform/WebURLRequest.h" 36 #include "third_party/WebKit/public/platform/WebURLRequest.h"
37 #include "third_party/WebKit/public/web/WebDataSource.h" 37 #include "third_party/WebKit/public/web/WebDataSource.h"
38 #include "third_party/WebKit/public/web/WebDocument.h" 38 #include "third_party/WebKit/public/web/WebDocument.h"
39 #include "third_party/WebKit/public/web/WebElement.h" 39 #include "third_party/WebKit/public/web/WebElement.h"
40 #include "third_party/WebKit/public/web/WebFrameContentDumper.h" 40 #include "third_party/WebKit/public/web/WebFrameContentDumper.h"
41 #include "third_party/WebKit/public/web/WebLocalFrame.h" 41 #include "third_party/WebKit/public/web/WebLocalFrame.h"
42 #include "third_party/WebKit/public/web/WebNode.h" 42 #include "third_party/WebKit/public/web/WebNode.h"
43 #include "third_party/WebKit/public/web/WebSecurityPolicy.h" 43 #include "third_party/WebKit/public/web/WebSecurityPolicy.h"
44 #include "third_party/skia/include/core/SkBitmap.h" 44 #include "third_party/skia/include/core/SkBitmap.h"
45 #include "ui/gfx/codec/jpeg_codec.h" 45 #include "ui/gfx/codec/jpeg_codec.h"
46 #include "ui/gfx/codec/png_codec.h"
46 #include "ui/gfx/geometry/size_f.h" 47 #include "ui/gfx/geometry/size_f.h"
47 #include "url/gurl.h" 48 #include "url/gurl.h"
48 49
49 #if BUILDFLAG(ENABLE_PRINTING) 50 #if BUILDFLAG(ENABLE_PRINTING)
50 #include "components/printing/common/print_messages.h" 51 #include "components/printing/common/print_messages.h"
51 #include "components/printing/renderer/print_web_view_helper.h" 52 #include "components/printing/renderer/print_web_view_helper.h"
52 #endif 53 #endif
53 54
54 using blink::WebDataSource; 55 using blink::WebDataSource;
55 using blink::WebElement; 56 using blink::WebElement;
56 using blink::WebFrameContentDumper; 57 using blink::WebFrameContentDumper;
57 using blink::WebLocalFrame; 58 using blink::WebLocalFrame;
58 using blink::WebNode; 59 using blink::WebNode;
59 using blink::WebString; 60 using blink::WebString;
60 using content::RenderFrame; 61 using content::RenderFrame;
61 62
62 // Maximum number of characters in the document to index. 63 // Maximum number of characters in the document to index.
63 // Any text beyond this point will be clipped. 64 // Any text beyond this point will be clipped.
64 static const size_t kMaxIndexChars = 65535; 65 static const size_t kMaxIndexChars = 65535;
65 66
66 // Constants for UMA statistic collection. 67 // Constants for UMA statistic collection.
67 static const char kTranslateCaptureText[] = "Translate.CaptureText"; 68 static const char kTranslateCaptureText[] = "Translate.CaptureText";
68 69
69 // For a page that auto-refreshes, we still show the bubble, if 70 // For a page that auto-refreshes, we still show the bubble, if
70 // the refresh delay is less than this value (in seconds). 71 // the refresh delay is less than this value (in seconds).
71 static const double kLocationChangeIntervalInSeconds = 10; 72 static const double kLocationChangeIntervalInSeconds = 10;
72 73
74 // For the context menu, we want to keep transparency as is instead of
75 // replacing transparent pixels with black ones
76 static const bool kDiscardTransparencyForContextMenu = false;
msarett1 2017/06/21 04:18:15 I'm glad this is false. "true" is fairly awkward
Daniel Park 2017/06/22 01:11:35 I'm a little unsure of how to proceed with regards
77
73 namespace { 78 namespace {
74 79
75 // If the source image is null or occupies less area than 80 // If the source image is null or occupies less area than
76 // |thumbnail_min_area_pixels|, we return the image unmodified. Otherwise, we 81 // |thumbnail_min_area_pixels|, we return the image unmodified. Otherwise, we
77 // scale down the image so that the width and height do not exceed 82 // scale down the image so that the width and height do not exceed
78 // |thumbnail_max_size_pixels|, preserving the original aspect ratio. 83 // |thumbnail_max_size_pixels|, preserving the original aspect ratio.
79 SkBitmap Downscale(const blink::WebImage& image, 84 SkBitmap Downscale(const blink::WebImage& image,
80 int thumbnail_min_area_pixels, 85 int thumbnail_min_area_pixels,
81 const gfx::Size& thumbnail_max_size_pixels) { 86 const gfx::Size& thumbnail_max_size_pixels) {
82 if (image.IsNull()) 87 if (image.IsNull())
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 // to investigate what it is doing and fix (http://crbug.com/606164). 188 // to investigate what it is doing and fix (http://crbug.com/606164).
184 WebNode context_node = frame->ContextMenuNode(); 189 WebNode context_node = frame->ContextMenuNode();
185 if (!context_node.IsNull() && context_node.IsElementNode()) { 190 if (!context_node.IsNull() && context_node.IsElementNode()) {
186 frame->ReloadImage(context_node); 191 frame->ReloadImage(context_node);
187 } 192 }
188 } 193 }
189 194
190 void ChromeRenderFrameObserver::RequestThumbnailForContextNode( 195 void ChromeRenderFrameObserver::RequestThumbnailForContextNode(
191 int32_t thumbnail_min_area_pixels, 196 int32_t thumbnail_min_area_pixels,
192 const gfx::Size& thumbnail_max_size_pixels, 197 const gfx::Size& thumbnail_max_size_pixels,
198 bool use_png_codec,
193 const RequestThumbnailForContextNodeCallback& callback) { 199 const RequestThumbnailForContextNodeCallback& callback) {
194 WebNode context_node = render_frame()->GetWebFrame()->ContextMenuNode(); 200 WebNode context_node = render_frame()->GetWebFrame()->ContextMenuNode();
195 SkBitmap thumbnail; 201 SkBitmap thumbnail;
196 gfx::Size original_size; 202 gfx::Size original_size;
197 if (!context_node.IsNull() && context_node.IsElementNode()) { 203 if (!context_node.IsNull() && context_node.IsElementNode()) {
198 blink::WebImage image = context_node.To<WebElement>().ImageContents(); 204 blink::WebImage image = context_node.To<WebElement>().ImageContents();
199 original_size = image.Size(); 205 original_size = image.Size();
200 thumbnail = Downscale(image, 206 thumbnail = Downscale(image,
201 thumbnail_min_area_pixels, 207 thumbnail_min_area_pixels,
202 thumbnail_max_size_pixels); 208 thumbnail_max_size_pixels);
203 } 209 }
204 210
205 SkBitmap bitmap; 211 SkBitmap bitmap;
206 if (thumbnail.colorType() == kN32_SkColorType) { 212 if (thumbnail.colorType() == kN32_SkColorType) {
207 bitmap = thumbnail; 213 bitmap = thumbnail;
208 } else { 214 } else {
209 SkImageInfo info = thumbnail.info().makeColorType(kN32_SkColorType); 215 SkImageInfo info = thumbnail.info().makeColorType(kN32_SkColorType);
210 if (bitmap.tryAllocPixels(info)) { 216 if (bitmap.tryAllocPixels(info)) {
211 thumbnail.readPixels(info, bitmap.getPixels(), bitmap.rowBytes(), 0, 0); 217 thumbnail.readPixels(info, bitmap.getPixels(), bitmap.rowBytes(), 0, 0);
212 } 218 }
213 } 219 }
214 220
215 std::vector<uint8_t> thumbnail_data; 221 std::vector<uint8_t> thumbnail_data;
216 constexpr int kDefaultQuality = 90; 222 constexpr int kDefaultQuality = 90;
217 std::vector<unsigned char> data; 223 std::vector<unsigned char> data;
218 if (gfx::JPEGCodec::Encode(bitmap, kDefaultQuality, &data)) { 224
219 thumbnail_data.swap(data); 225 if (use_png_codec) {
226 if (gfx::PNGCodec::EncodeBGRASkBitmap(
227 bitmap, kDiscardTransparencyForContextMenu, &data)) {
228 thumbnail_data.swap(data);
229 }
230 } else {
231 if (gfx::JPEGCodec::Encode(bitmap, kDefaultQuality, &data)) {
232 thumbnail_data.swap(data);
233 }
220 } 234 }
221 235
222 callback.Run(thumbnail_data, original_size); 236 callback.Run(thumbnail_data, original_size);
223 } 237 }
224 238
225 void ChromeRenderFrameObserver::OnPrintNodeUnderContextMenu() { 239 void ChromeRenderFrameObserver::OnPrintNodeUnderContextMenu() {
226 #if BUILDFLAG(ENABLE_PRINTING) 240 #if BUILDFLAG(ENABLE_PRINTING)
227 printing::PrintWebViewHelper* helper = 241 printing::PrintWebViewHelper* helper =
228 printing::PrintWebViewHelper::Get(render_frame()); 242 printing::PrintWebViewHelper::Get(render_frame());
229 if (helper) 243 if (helper)
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
360 const service_manager::BindSourceInfo& source_info, 374 const service_manager::BindSourceInfo& source_info,
361 chrome::mojom::ImageContextMenuRendererRequest request) { 375 chrome::mojom::ImageContextMenuRendererRequest request) {
362 image_context_menu_renderer_bindings_.AddBinding(this, std::move(request)); 376 image_context_menu_renderer_bindings_.AddBinding(this, std::move(request));
363 } 377 }
364 378
365 void ChromeRenderFrameObserver::OnThumbnailCapturerRequest( 379 void ChromeRenderFrameObserver::OnThumbnailCapturerRequest(
366 const service_manager::BindSourceInfo& source_info, 380 const service_manager::BindSourceInfo& source_info,
367 chrome::mojom::ThumbnailCapturerRequest request) { 381 chrome::mojom::ThumbnailCapturerRequest request) {
368 thumbnail_capturer_bindings_.AddBinding(this, std::move(request)); 382 thumbnail_capturer_bindings_.AddBinding(this, std::move(request));
369 } 383 }
OLDNEW
« chrome/common/thumbnail_capturer.mojom ('K') | « chrome/renderer/chrome_render_frame_observer.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698