OLD | NEW |
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 27 matching lines...) Expand all Loading... |
38 #include "third_party/WebKit/public/platform/WebURLRequest.h" | 38 #include "third_party/WebKit/public/platform/WebURLRequest.h" |
39 #include "third_party/WebKit/public/web/WebDataSource.h" | 39 #include "third_party/WebKit/public/web/WebDataSource.h" |
40 #include "third_party/WebKit/public/web/WebDocument.h" | 40 #include "third_party/WebKit/public/web/WebDocument.h" |
41 #include "third_party/WebKit/public/web/WebElement.h" | 41 #include "third_party/WebKit/public/web/WebElement.h" |
42 #include "third_party/WebKit/public/web/WebFrameContentDumper.h" | 42 #include "third_party/WebKit/public/web/WebFrameContentDumper.h" |
43 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 43 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
44 #include "third_party/WebKit/public/web/WebNode.h" | 44 #include "third_party/WebKit/public/web/WebNode.h" |
45 #include "third_party/WebKit/public/web/WebSecurityPolicy.h" | 45 #include "third_party/WebKit/public/web/WebSecurityPolicy.h" |
46 #include "third_party/skia/include/core/SkBitmap.h" | 46 #include "third_party/skia/include/core/SkBitmap.h" |
47 #include "ui/gfx/codec/jpeg_codec.h" | 47 #include "ui/gfx/codec/jpeg_codec.h" |
| 48 #include "ui/gfx/codec/png_codec.h" |
48 #include "ui/gfx/geometry/size_f.h" | 49 #include "ui/gfx/geometry/size_f.h" |
49 #include "url/gurl.h" | 50 #include "url/gurl.h" |
50 | 51 |
51 #if BUILDFLAG(ENABLE_PRINTING) | 52 #if BUILDFLAG(ENABLE_PRINTING) |
52 #include "components/printing/common/print_messages.h" | 53 #include "components/printing/common/print_messages.h" |
53 #include "components/printing/renderer/print_web_view_helper.h" | 54 #include "components/printing/renderer/print_web_view_helper.h" |
54 #endif | 55 #endif |
55 | 56 |
56 using blink::WebDataSource; | 57 using blink::WebDataSource; |
57 using blink::WebElement; | 58 using blink::WebElement; |
58 using blink::WebFrameContentDumper; | 59 using blink::WebFrameContentDumper; |
59 using blink::WebLocalFrame; | 60 using blink::WebLocalFrame; |
60 using blink::WebNode; | 61 using blink::WebNode; |
61 using blink::WebString; | 62 using blink::WebString; |
62 using content::RenderFrame; | 63 using content::RenderFrame; |
63 | 64 |
64 // Maximum number of characters in the document to index. | 65 // Maximum number of characters in the document to index. |
65 // Any text beyond this point will be clipped. | 66 // Any text beyond this point will be clipped. |
66 static const size_t kMaxIndexChars = 65535; | 67 static const size_t kMaxIndexChars = 65535; |
67 | 68 |
68 // Constants for UMA statistic collection. | 69 // Constants for UMA statistic collection. |
69 static const char kTranslateCaptureText[] = "Translate.CaptureText"; | 70 static const char kTranslateCaptureText[] = "Translate.CaptureText"; |
70 | 71 |
71 // For a page that auto-refreshes, we still show the bubble, if | 72 // For a page that auto-refreshes, we still show the bubble, if |
72 // the refresh delay is less than this value (in seconds). | 73 // the refresh delay is less than this value (in seconds). |
73 static const double kLocationChangeIntervalInSeconds = 10; | 74 static const double kLocationChangeIntervalInSeconds = 10; |
74 | 75 |
| 76 // For the context menu, we want to keep transparency as is instead of |
| 77 // replacing transparent pixels with black ones |
| 78 static const bool kDiscardTransparencyForContextMenu = false; |
| 79 |
75 namespace { | 80 namespace { |
76 | 81 |
77 // If the source image is null or occupies less area than | 82 // If the source image is null or occupies less area than |
78 // |thumbnail_min_area_pixels|, we return the image unmodified. Otherwise, we | 83 // |thumbnail_min_area_pixels|, we return the image unmodified. Otherwise, we |
79 // scale down the image so that the width and height do not exceed | 84 // scale down the image so that the width and height do not exceed |
80 // |thumbnail_max_size_pixels|, preserving the original aspect ratio. | 85 // |thumbnail_max_size_pixels|, preserving the original aspect ratio. |
81 SkBitmap Downscale(const blink::WebImage& image, | 86 SkBitmap Downscale(const blink::WebImage& image, |
82 int thumbnail_min_area_pixels, | 87 int thumbnail_min_area_pixels, |
83 const gfx::Size& thumbnail_max_size_pixels) { | 88 const gfx::Size& thumbnail_max_size_pixels) { |
84 if (image.IsNull()) | 89 if (image.IsNull()) |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 // to investigate what it is doing and fix (http://crbug.com/606164). | 192 // to investigate what it is doing and fix (http://crbug.com/606164). |
188 WebNode context_node = frame->ContextMenuNode(); | 193 WebNode context_node = frame->ContextMenuNode(); |
189 if (!context_node.IsNull() && context_node.IsElementNode()) { | 194 if (!context_node.IsNull() && context_node.IsElementNode()) { |
190 frame->ReloadImage(context_node); | 195 frame->ReloadImage(context_node); |
191 } | 196 } |
192 } | 197 } |
193 | 198 |
194 void ChromeRenderFrameObserver::RequestThumbnailForContextNode( | 199 void ChromeRenderFrameObserver::RequestThumbnailForContextNode( |
195 int32_t thumbnail_min_area_pixels, | 200 int32_t thumbnail_min_area_pixels, |
196 const gfx::Size& thumbnail_max_size_pixels, | 201 const gfx::Size& thumbnail_max_size_pixels, |
| 202 bool use_png_codec, |
197 const RequestThumbnailForContextNodeCallback& callback) { | 203 const RequestThumbnailForContextNodeCallback& callback) { |
198 WebNode context_node = render_frame()->GetWebFrame()->ContextMenuNode(); | 204 WebNode context_node = render_frame()->GetWebFrame()->ContextMenuNode(); |
199 SkBitmap thumbnail; | 205 SkBitmap thumbnail; |
200 gfx::Size original_size; | 206 gfx::Size original_size; |
201 if (!context_node.IsNull() && context_node.IsElementNode()) { | 207 if (!context_node.IsNull() && context_node.IsElementNode()) { |
202 blink::WebImage image = context_node.To<WebElement>().ImageContents(); | 208 blink::WebImage image = context_node.To<WebElement>().ImageContents(); |
203 original_size = image.Size(); | 209 original_size = image.Size(); |
204 thumbnail = Downscale(image, | 210 thumbnail = Downscale(image, |
205 thumbnail_min_area_pixels, | 211 thumbnail_min_area_pixels, |
206 thumbnail_max_size_pixels); | 212 thumbnail_max_size_pixels); |
207 } | 213 } |
208 | 214 |
209 SkBitmap bitmap; | 215 SkBitmap bitmap; |
210 if (thumbnail.colorType() == kN32_SkColorType) { | 216 if (thumbnail.colorType() == kN32_SkColorType) { |
211 bitmap = thumbnail; | 217 bitmap = thumbnail; |
212 } else { | 218 } else { |
213 SkImageInfo info = thumbnail.info().makeColorType(kN32_SkColorType); | 219 SkImageInfo info = thumbnail.info().makeColorType(kN32_SkColorType); |
214 if (bitmap.tryAllocPixels(info)) { | 220 if (bitmap.tryAllocPixels(info)) { |
215 thumbnail.readPixels(info, bitmap.getPixels(), bitmap.rowBytes(), 0, 0); | 221 thumbnail.readPixels(info, bitmap.getPixels(), bitmap.rowBytes(), 0, 0); |
216 } | 222 } |
217 } | 223 } |
218 | 224 |
219 std::vector<uint8_t> thumbnail_data; | 225 std::vector<uint8_t> thumbnail_data; |
220 constexpr int kDefaultQuality = 90; | 226 constexpr int kDefaultQuality = 90; |
221 std::vector<unsigned char> data; | 227 std::vector<unsigned char> data; |
222 if (gfx::JPEGCodec::Encode(bitmap, kDefaultQuality, &data)) { | 228 |
223 thumbnail_data.swap(data); | 229 if (use_png_codec) { |
| 230 if (gfx::PNGCodec::EncodeBGRASkBitmap( |
| 231 bitmap, kDiscardTransparencyForContextMenu, &data)) { |
| 232 thumbnail_data.swap(data); |
| 233 } |
| 234 } else { |
| 235 if (gfx::JPEGCodec::Encode(bitmap, kDefaultQuality, &data)) { |
| 236 thumbnail_data.swap(data); |
| 237 } |
224 } | 238 } |
225 | 239 |
226 callback.Run(thumbnail_data, original_size); | 240 callback.Run(thumbnail_data, original_size); |
227 } | 241 } |
228 | 242 |
229 void ChromeRenderFrameObserver::OnPrintNodeUnderContextMenu() { | 243 void ChromeRenderFrameObserver::OnPrintNodeUnderContextMenu() { |
230 #if BUILDFLAG(ENABLE_PRINTING) | 244 #if BUILDFLAG(ENABLE_PRINTING) |
231 printing::PrintWebViewHelper* helper = | 245 printing::PrintWebViewHelper* helper = |
232 printing::PrintWebViewHelper::Get(render_frame()); | 246 printing::PrintWebViewHelper::Get(render_frame()); |
233 if (helper) | 247 if (helper) |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
405 const service_manager::BindSourceInfo& source_info, | 419 const service_manager::BindSourceInfo& source_info, |
406 chrome::mojom::ImageContextMenuRendererRequest request) { | 420 chrome::mojom::ImageContextMenuRendererRequest request) { |
407 image_context_menu_renderer_bindings_.AddBinding(this, std::move(request)); | 421 image_context_menu_renderer_bindings_.AddBinding(this, std::move(request)); |
408 } | 422 } |
409 | 423 |
410 void ChromeRenderFrameObserver::OnThumbnailCapturerRequest( | 424 void ChromeRenderFrameObserver::OnThumbnailCapturerRequest( |
411 const service_manager::BindSourceInfo& source_info, | 425 const service_manager::BindSourceInfo& source_info, |
412 chrome::mojom::ThumbnailCapturerRequest request) { | 426 chrome::mojom::ThumbnailCapturerRequest request) { |
413 thumbnail_capturer_bindings_.AddBinding(this, std::move(request)); | 427 thumbnail_capturer_bindings_.AddBinding(this, std::move(request)); |
414 } | 428 } |
OLD | NEW |