| 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 <limits> |
| 8 #include <string> |
| 9 #include <vector> |
| 10 |
| 7 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 8 #include "chrome/common/prerender_messages.h" | 12 #include "chrome/common/prerender_messages.h" |
| 9 #include "chrome/common/print_messages.h" | 13 #include "chrome/common/print_messages.h" |
| 10 #include "chrome/common/render_messages.h" | 14 #include "chrome/common/render_messages.h" |
| 11 #include "chrome/renderer/prerender/prerender_helper.h" | 15 #include "chrome/renderer/prerender/prerender_helper.h" |
| 12 #include "chrome/renderer/printing/print_web_view_helper.h" | 16 #include "chrome/renderer/printing/print_web_view_helper.h" |
| 13 #include "content/public/renderer/render_frame.h" | 17 #include "content/public/renderer/render_frame.h" |
| 14 #include "skia/ext/image_operations.h" | 18 #include "skia/ext/image_operations.h" |
| 15 #include "skia/ext/platform_canvas.h" | |
| 16 #include "third_party/WebKit/public/platform/WebImage.h" | 19 #include "third_party/WebKit/public/platform/WebImage.h" |
| 17 #include "third_party/WebKit/public/web/WebElement.h" | 20 #include "third_party/WebKit/public/web/WebElement.h" |
| 18 #include "third_party/WebKit/public/web/WebNode.h" | 21 #include "third_party/WebKit/public/web/WebNode.h" |
| 22 #include "third_party/skia/include/core/SkBitmap.h" |
| 23 #include "ui/gfx/codec/jpeg_codec.h" |
| 19 | 24 |
| 20 using blink::WebElement; | 25 using blink::WebElement; |
| 21 using blink::WebNode; | 26 using blink::WebNode; |
| 22 | 27 |
| 23 namespace { | 28 namespace { |
| 24 | 29 |
| 25 // If the source image is null or occupies less area than | 30 // If the source image is null or occupies less area than |
| 26 // |thumbnail_min_area_pixels|, we return the image unmodified. Otherwise, we | 31 // |thumbnail_min_area_pixels|, we return the image unmodified. Otherwise, we |
| 27 // scale down the image so that the width and height do not exceed | 32 // scale down the image so that the width and height do not exceed |
| 28 // |thumbnail_max_size_pixels|, preserving the original aspect ratio. | 33 // |thumbnail_max_size_pixels|, preserving the original aspect ratio. |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 WebNode context_node = render_frame()->GetContextMenuNode(); | 115 WebNode context_node = render_frame()->GetContextMenuNode(); |
| 111 SkBitmap thumbnail; | 116 SkBitmap thumbnail; |
| 112 gfx::Size original_size; | 117 gfx::Size original_size; |
| 113 if (!context_node.isNull() && context_node.isElementNode()) { | 118 if (!context_node.isNull() && context_node.isElementNode()) { |
| 114 blink::WebImage image = context_node.to<WebElement>().imageContents(); | 119 blink::WebImage image = context_node.to<WebElement>().imageContents(); |
| 115 original_size = image.size(); | 120 original_size = image.size(); |
| 116 thumbnail = Downscale(image, | 121 thumbnail = Downscale(image, |
| 117 thumbnail_min_area_pixels, | 122 thumbnail_min_area_pixels, |
| 118 thumbnail_max_size_pixels); | 123 thumbnail_max_size_pixels); |
| 119 } | 124 } |
| 125 |
| 126 SkBitmap bitmap; |
| 127 if (thumbnail.colorType() == kN32_SkColorType) |
| 128 bitmap = thumbnail; |
| 129 else |
| 130 thumbnail.copyTo(&bitmap, kN32_SkColorType); |
| 131 |
| 132 std::string thumbnail_data; |
| 133 SkAutoLockPixels lock(bitmap); |
| 134 if (bitmap.getPixels()) { |
| 135 const int kDefaultQuality = 90; |
| 136 std::vector<unsigned char> data; |
| 137 if (gfx::JPEGCodec::Encode( |
| 138 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), |
| 139 gfx::JPEGCodec::FORMAT_SkBitmap, bitmap.width(), bitmap.height(), |
| 140 static_cast<int>(bitmap.rowBytes()), kDefaultQuality, &data)) |
| 141 thumbnail_data = std::string(data.begin(), data.end()); |
| 142 } |
| 143 |
| 120 Send(new ChromeViewHostMsg_RequestThumbnailForContextNode_ACK( | 144 Send(new ChromeViewHostMsg_RequestThumbnailForContextNode_ACK( |
| 121 routing_id(), thumbnail, original_size)); | 145 routing_id(), thumbnail_data, original_size)); |
| 122 } | 146 } |
| 123 | 147 |
| 124 void ChromeRenderFrameObserver::OnPrintNodeUnderContextMenu() { | 148 void ChromeRenderFrameObserver::OnPrintNodeUnderContextMenu() { |
| 125 printing::PrintWebViewHelper* helper = | 149 printing::PrintWebViewHelper* helper = |
| 126 printing::PrintWebViewHelper::Get(render_frame()->GetRenderView()); | 150 printing::PrintWebViewHelper::Get(render_frame()->GetRenderView()); |
| 127 if (helper) | 151 if (helper) |
| 128 helper->PrintNode(render_frame()->GetContextMenuNode()); | 152 helper->PrintNode(render_frame()->GetContextMenuNode()); |
| 129 } | 153 } |
| OLD | NEW |