OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/utility/image_decoder_impl.h" | 5 #include <memory> |
6 | |
7 #include <vector> | 6 #include <vector> |
8 | 7 |
9 #include "base/bind.h" | 8 #include "base/bind.h" |
10 #include "ipc/ipc_channel.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/message_loop/message_loop.h" |
| 11 #include "gin/array_buffer.h" |
| 12 #include "gin/public/isolate_holder.h" |
| 13 #include "services/image_decoder/image_decoder_impl.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "third_party/WebKit/public/platform/scheduler/utility/webthread_impl_fo
r_utility_thread.h" |
| 16 #include "third_party/WebKit/public/web/WebKit.h" |
12 #include "third_party/skia/include/core/SkBitmap.h" | 17 #include "third_party/skia/include/core/SkBitmap.h" |
13 #include "ui/gfx/codec/jpeg_codec.h" | 18 #include "ui/gfx/codec/jpeg_codec.h" |
14 | 19 |
15 namespace mojom { | 20 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
| 21 #include "gin/v8_initializer.h" |
| 22 #endif |
| 23 |
| 24 namespace image_decoder { |
16 | 25 |
17 namespace { | 26 namespace { |
18 | 27 |
| 28 const int64_t kTestMaxImageSize = 128 * 1024; |
| 29 |
19 bool CreateJPEGImage(int width, | 30 bool CreateJPEGImage(int width, |
20 int height, | 31 int height, |
21 SkColor color, | 32 SkColor color, |
22 std::vector<unsigned char>* output) { | 33 std::vector<unsigned char>* output) { |
23 SkBitmap bitmap; | 34 SkBitmap bitmap; |
24 bitmap.allocN32Pixels(width, height); | 35 bitmap.allocN32Pixels(width, height); |
25 bitmap.eraseColor(color); | 36 bitmap.eraseColor(color); |
26 | 37 |
27 const int kQuality = 50; | 38 const int kQuality = 50; |
28 if (!gfx::JPEGCodec::Encode( | 39 if (!gfx::JPEGCodec::Encode( |
29 static_cast<const unsigned char*>(bitmap.getPixels()), | 40 static_cast<const unsigned char*>(bitmap.getPixels()), |
30 gfx::JPEGCodec::FORMAT_SkBitmap, width, height, | 41 gfx::JPEGCodec::FORMAT_SkBitmap, width, height, |
31 static_cast<int>(bitmap.rowBytes()), kQuality, output)) { | 42 static_cast<int>(bitmap.rowBytes()), kQuality, output)) { |
32 LOG(ERROR) << "Unable to encode " << width << "x" << height << " bitmap"; | 43 LOG(ERROR) << "Unable to encode " << width << "x" << height << " bitmap"; |
33 return false; | 44 return false; |
34 } | 45 } |
35 return true; | 46 return true; |
36 } | 47 } |
37 | 48 |
38 class Request { | 49 class Request { |
39 public: | 50 public: |
40 explicit Request(ImageDecoderImpl* decoder) : decoder_(decoder) {} | 51 explicit Request(ImageDecoderImpl* decoder) : decoder_(decoder) {} |
41 | 52 |
42 void DecodeImage(const std::vector<unsigned char>& image, bool shrink) { | 53 void DecodeImage(const std::vector<unsigned char>& image, bool shrink) { |
43 decoder_->DecodeImage( | 54 decoder_->DecodeImage( |
44 image, ImageCodec::DEFAULT, shrink, | 55 image, mojom::ImageCodec::DEFAULT, shrink, kTestMaxImageSize, |
45 base::Bind(&Request::OnRequestDone, base::Unretained(this))); | 56 base::Bind(&Request::OnRequestDone, base::Unretained(this))); |
46 } | 57 } |
47 | 58 |
48 const SkBitmap& bitmap() const { return bitmap_; } | 59 const SkBitmap& bitmap() const { return bitmap_; } |
49 | 60 |
50 private: | 61 private: |
51 void OnRequestDone(const SkBitmap& result_image) { bitmap_ = result_image; } | 62 void OnRequestDone(const SkBitmap& result_image) { bitmap_ = result_image; } |
52 | 63 |
53 ImageDecoderImpl* decoder_; | 64 ImageDecoderImpl* decoder_; |
54 SkBitmap bitmap_; | 65 SkBitmap bitmap_; |
55 }; | 66 }; |
56 | 67 |
| 68 // We need to ensure that Blink and V8 are initialized in order to use content's |
| 69 // image decoding call. |
| 70 class BlinkInitializer : public blink::Platform { |
| 71 public: |
| 72 BlinkInitializer() |
| 73 : main_thread_(new blink::scheduler::WebThreadImplForUtilityThread()) { |
| 74 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
| 75 gin::V8Initializer::LoadV8Snapshot(); |
| 76 gin::V8Initializer::LoadV8Natives(); |
| 77 #endif |
| 78 |
| 79 blink::initialize(this); |
| 80 } |
| 81 |
| 82 ~BlinkInitializer() override {} |
| 83 |
| 84 private: |
| 85 std::unique_ptr<blink::scheduler::WebThreadImplForUtilityThread> main_thread_; |
| 86 |
| 87 DISALLOW_COPY_AND_ASSIGN(BlinkInitializer); |
| 88 }; |
| 89 |
| 90 base::LazyInstance<BlinkInitializer>::Leaky g_blink_initializer = |
| 91 LAZY_INSTANCE_INITIALIZER; |
| 92 |
| 93 class ImageDecoderImplTest : public testing::Test { |
| 94 public: |
| 95 ImageDecoderImplTest() : decoder_(nullptr) {} |
| 96 ~ImageDecoderImplTest() override {} |
| 97 |
| 98 void SetUp() override { g_blink_initializer.Get(); } |
| 99 |
| 100 protected: |
| 101 ImageDecoderImpl* decoder() { return &decoder_; } |
| 102 |
| 103 private: |
| 104 base::MessageLoop message_loop_; |
| 105 ImageDecoderImpl decoder_; |
| 106 }; |
| 107 |
57 } // namespace | 108 } // namespace |
58 | 109 |
59 // Test that DecodeImage() doesn't return image message > (max message size) | 110 // Test that DecodeImage() doesn't return image message > (max message size) |
60 TEST(ImageDecoderImplTest, DecodeImageSizeLimit) { | 111 TEST_F(ImageDecoderImplTest, DecodeImageSizeLimit) { |
61 // Using actual limit generates 14000 x 9400 images, which causes the test to | 112 // Approx max height for 3:2 image that will fit in the allotted space. |
62 // timeout. We test with a smaller limit for efficiency. | 113 // 1.5 for width/height ratio, 4 for bytes/pixel. |
63 const size_t kTestMessageSize = IPC::Channel::kMaximumMessageSize / 1024; | 114 int max_height_for_msg = sqrt(kTestMaxImageSize / (1.5 * 4)); |
64 | |
65 ImageDecoderImpl decoder(kTestMessageSize); | |
66 | |
67 // Approx max height for 3:2 image that will fit in IPC message; | |
68 // 1.5 for width/height ratio, 4 for bytes/pixel | |
69 int max_height_for_msg = sqrt(kTestMessageSize / (1.5 * 4)); | |
70 int base_msg_size = sizeof(skia::mojom::Bitmap::Data_); | 115 int base_msg_size = sizeof(skia::mojom::Bitmap::Data_); |
71 | 116 |
72 // Sizes which should trigger dimension-halving 0, 1 and 2 times | 117 // Sizes which should trigger dimension-halving 0, 1 and 2 times |
73 int heights[] = {max_height_for_msg - 10, | 118 int heights[] = {max_height_for_msg - 10, |
74 max_height_for_msg + 10, | 119 max_height_for_msg + 10, |
75 2 * max_height_for_msg + 10}; | 120 2 * max_height_for_msg + 10}; |
76 int widths[] = {heights[0] * 3 / 2, heights[1] * 3 / 2, heights[2] * 3 / 2}; | 121 int widths[] = {heights[0] * 3 / 2, heights[1] * 3 / 2, heights[2] * 3 / 2}; |
77 for (size_t i = 0; i < arraysize(heights); i++) { | 122 for (size_t i = 0; i < arraysize(heights); i++) { |
78 std::vector<unsigned char> jpg; | 123 std::vector<unsigned char> jpg; |
79 ASSERT_TRUE(CreateJPEGImage(widths[i], heights[i], SK_ColorRED, &jpg)); | 124 ASSERT_TRUE(CreateJPEGImage(widths[i], heights[i], SK_ColorRED, &jpg)); |
80 | 125 |
81 Request request(&decoder); | 126 Request request(decoder()); |
82 request.DecodeImage(jpg, true); | 127 request.DecodeImage(jpg, true); |
83 ASSERT_FALSE(request.bitmap().isNull()); | 128 ASSERT_FALSE(request.bitmap().isNull()); |
84 | 129 |
85 // Check that image has been shrunk appropriately | 130 // Check that image has been shrunk appropriately |
86 EXPECT_LT(request.bitmap().computeSize64() + base_msg_size, | 131 EXPECT_LT(request.bitmap().computeSize64() + base_msg_size, |
87 static_cast<int64_t>(kTestMessageSize)); | 132 kTestMaxImageSize); |
88 // Android does its own image shrinking for memory conservation deeper in | 133 // Android does its own image shrinking for memory conservation deeper in |
89 // the decode, so more specific tests here won't work. | 134 // the decode, so more specific tests here won't work. |
90 #if !defined(OS_ANDROID) | 135 #if !defined(OS_ANDROID) |
91 EXPECT_EQ(widths[i] >> i, request.bitmap().width()); | 136 EXPECT_EQ(widths[i] >> i, request.bitmap().width()); |
92 EXPECT_EQ(heights[i] >> i, request.bitmap().height()); | 137 EXPECT_EQ(heights[i] >> i, request.bitmap().height()); |
93 | 138 |
94 // Check that if resize not requested and image exceeds IPC size limit, | 139 // Check that if resize not requested and image exceeds IPC size limit, |
95 // an empty image is returned | 140 // an empty image is returned |
96 if (heights[i] > max_height_for_msg) { | 141 if (heights[i] > max_height_for_msg) { |
97 Request request(&decoder); | 142 Request request(decoder()); |
98 request.DecodeImage(jpg, false); | 143 request.DecodeImage(jpg, false); |
99 EXPECT_TRUE(request.bitmap().isNull()); | 144 EXPECT_TRUE(request.bitmap().isNull()); |
100 } | 145 } |
101 #endif | 146 #endif |
102 } | 147 } |
103 } | 148 } |
104 | 149 |
105 TEST(ImageDecoderImplTest, DecodeImageFailed) { | 150 TEST_F(ImageDecoderImplTest, DecodeImageFailed) { |
106 ImageDecoderImpl decoder(IPC::Channel::kMaximumMessageSize); | |
107 | |
108 // The "jpeg" is just some "random" data; | 151 // The "jpeg" is just some "random" data; |
109 const char kRandomData[] = "u gycfy7xdjkhfgui bdui "; | 152 const char kRandomData[] = "u gycfy7xdjkhfgui bdui "; |
110 std::vector<unsigned char> jpg(kRandomData, | 153 std::vector<unsigned char> jpg(kRandomData, |
111 kRandomData + sizeof(kRandomData)); | 154 kRandomData + sizeof(kRandomData)); |
112 | 155 |
113 Request request(&decoder); | 156 Request request(decoder()); |
114 request.DecodeImage(jpg, false); | 157 request.DecodeImage(jpg, false); |
115 EXPECT_TRUE(request.bitmap().isNull()); | 158 EXPECT_TRUE(request.bitmap().isNull()); |
116 } | 159 } |
117 | 160 |
118 } // namespace mojom | 161 } // namespace image_decoder |
OLD | NEW |