Chromium Code Reviews| 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) { | |
|
Peter Kasting
2014/12/11 20:11:30
Nit: {} unnecessary
jbroman
2014/12/12 00:08:57
Done.
| |
| 128 bitmap = thumbnail; | |
| 129 } else { | |
| 130 thumbnail.copyTo(&bitmap, kN32_SkColorType); | |
| 131 } | |
| 132 | |
| 133 std::string thumbnail_data; | |
| 134 { | |
| 135 SkAutoLockPixels lock(bitmap); | |
|
Peter Kasting
2014/12/11 20:11:30
Is it important that this lock be released before
jbroman
2014/12/12 00:08:57
It's not important, no. There was just no reason f
| |
| 136 if (bitmap.getPixels() != nullptr && | |
|
Peter Kasting
2014/12/11 20:11:30
Nit: "!= nullptr" unnecessary
jbroman
2014/12/12 00:08:57
Done.
I had some vague idea that Chromium style p
| |
| 137 static_cast<int>(bitmap.rowBytes()) > 0) { | |
|
Peter Kasting
2014/12/11 20:11:30
Why is this cast to int necessary?
jbroman
2014/12/12 00:08:57
The rowBytes argument to gfx::JPEGCodec::Encode is
Peter Kasting
2014/12/12 00:35:51
I see. You need some kind of cast here, then. Th
jbroman
2014/12/12 01:10:46
If this were to be negative after a cast, it would
| |
| 138 const int kDefaultQuality = 90; | |
| 139 std::vector<unsigned char> data; | |
| 140 if (gfx::JPEGCodec::Encode( | |
| 141 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), | |
| 142 gfx::JPEGCodec::FORMAT_SkBitmap, bitmap.width(), bitmap.height(), | |
| 143 static_cast<int>(bitmap.rowBytes()), kDefaultQuality, &data)) | |
| 144 thumbnail_data = std::string(data.begin(), data.end()); | |
| 145 } | |
| 146 } | |
| 147 | |
| 120 Send(new ChromeViewHostMsg_RequestThumbnailForContextNode_ACK( | 148 Send(new ChromeViewHostMsg_RequestThumbnailForContextNode_ACK( |
| 121 routing_id(), thumbnail, original_size)); | 149 routing_id(), thumbnail_data, original_size)); |
| 122 } | 150 } |
| 123 | 151 |
| 124 void ChromeRenderFrameObserver::OnPrintNodeUnderContextMenu() { | 152 void ChromeRenderFrameObserver::OnPrintNodeUnderContextMenu() { |
| 125 printing::PrintWebViewHelper* helper = | 153 printing::PrintWebViewHelper* helper = |
| 126 printing::PrintWebViewHelper::Get(render_frame()->GetRenderView()); | 154 printing::PrintWebViewHelper::Get(render_frame()->GetRenderView()); |
| 127 if (helper) | 155 if (helper) |
| 128 helper->PrintNode(render_frame()->GetContextMenuNode()); | 156 helper->PrintNode(render_frame()->GetContextMenuNode()); |
| 129 } | 157 } |
| OLD | NEW |