Commit 57467667 authored by Xing Liu's avatar Xing Liu Committed by Commit Bot

Upboarding: Add image decoder.

This CL adds the functionality to decode image safely in a utility
process.

The decoding details is hidden behind an interface, for caller to build
unit tests effectively.

Bug: 1058534
Change-Id: Ibbcf7afa4e6d7ee97ed0baa2fcdfacaeddcdcbe6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2106768
Commit-Queue: Xing Liu <xingliu@chromium.org>
Reviewed-by: default avatarShakti Sahu <shaktisahu@chromium.org>
Reviewed-by: default avatarHesen Zhang <hesen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#751087}
parent 2f2a33d5
...@@ -11,6 +11,11 @@ group("upboarding") { ...@@ -11,6 +11,11 @@ group("upboarding") {
deps = [ "//chrome/browser/upboarding/query_tiles" ] deps = [ "//chrome/browser/upboarding/query_tiles" ]
} }
group("unit_tests") {
testonly = true
deps = [ "//chrome/browser/upboarding/query_tiles:unit_tests" ]
}
if (is_android) { if (is_android) {
java_group("java") { java_group("java") {
deps = [ "query_tiles:query_tiles_java" ] deps = [ "query_tiles:query_tiles_java" ]
......
...@@ -116,10 +116,8 @@ if (is_android) { ...@@ -116,10 +116,8 @@ if (is_android) {
} }
} }
source_set("unit_tests") { group("unit_tests") {
testonly = true testonly = true
visibility = [ "//chrome/browser/upboarding:unit_tests" ]
sources = []
deps = [ "//chrome/browser/upboarding/query_tiles/internal:unit_tests" ] deps = [ "//chrome/browser/upboarding/query_tiles/internal:unit_tests" ]
} }
...@@ -9,6 +9,8 @@ if (is_android) { ...@@ -9,6 +9,8 @@ if (is_android) {
source_set("internal") { source_set("internal") {
sources = [ sources = [
"image_decoder.cc",
"image_decoder.h",
"image_loader.cc", "image_loader.cc",
"image_loader.h", "image_loader.h",
"proto_conversion.cc", "proto_conversion.cc",
...@@ -23,6 +25,7 @@ source_set("internal") { ...@@ -23,6 +25,7 @@ source_set("internal") {
"//chrome/browser/upboarding/query_tiles:public", "//chrome/browser/upboarding/query_tiles:public",
"//chrome/browser/upboarding/query_tiles/proto", "//chrome/browser/upboarding/query_tiles/proto",
"//components/leveldb_proto", "//components/leveldb_proto",
"//services/data_decoder/public/cpp",
"//skia", "//skia",
"//url", "//url",
] ]
...@@ -30,18 +33,24 @@ source_set("internal") { ...@@ -30,18 +33,24 @@ source_set("internal") {
source_set("unit_tests") { source_set("unit_tests") {
testonly = true testonly = true
visibility = [ "//chrome/browser/upboarding/query_tiles:unit_tests" ]
sources = [ sources = [
"image_decoder_unittest.cc",
"proto_conversion_unittest.cc", "proto_conversion_unittest.cc",
"query_tile_store_unittest.cc", "query_tile_store_unittest.cc",
] ]
deps = [ deps = [
":internal",
"//base", "//base",
"//base/test:test_support",
"//chrome/browser/upboarding/query_tiles:public", "//chrome/browser/upboarding/query_tiles:public",
"//chrome/browser/upboarding/query_tiles/internal", "//chrome/browser/upboarding/query_tiles/internal",
"//chrome/browser/upboarding/query_tiles/proto", "//chrome/browser/upboarding/query_tiles/proto",
"//components/leveldb_proto", "//components/leveldb_proto",
"//components/leveldb_proto:test_support", "//components/leveldb_proto:test_support",
"//services/data_decoder/public/cpp:test_support",
"//skia", "//skia",
"//testing/gmock", "//testing/gmock",
"//testing/gtest", "//testing/gtest",
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/upboarding/query_tiles/internal/image_decoder.h"
#include <utility>
#include "services/data_decoder/public/cpp/decode_image.h"
#include "ui/gfx/geometry/size.h"
namespace upboarding {
namespace {
// The maximum size of data to be decoded.
constexpr size_t kMaximumEncodedDataSize = 10 * 1024 * 1024 /*10MB*/;
// Decodes an image in a utility process. Destroy the object will release the
// IPC connection.
class SafeImageDecoder : public ImageDecoder {
public:
SafeImageDecoder() = default;
SafeImageDecoder(const SafeImageDecoder&) = delete;
SafeImageDecoder& operator=(const SafeImageDecoder&) = delete;
~SafeImageDecoder() override = default;
private:
// ImageDecoder implementation.
void Decode(EncodedData data,
const gfx::Size& output_size,
DecodeCallback decode_callback) override {
DCHECK(decode_callback);
if (data.empty()) {
std::move(decode_callback).Run(SkBitmap());
return;
}
// Each decoding operation happens in its own process.
// TODO(xingliu): Consider to use a shared utility process.
data_decoder::DecodeImageIsolated(
std::move(data), data_decoder::mojom::ImageCodec::DEFAULT,
/*shrink_to_fit=*/true, kMaximumEncodedDataSize, output_size,
std::move(decode_callback));
}
};
} // namespace
// static
std::unique_ptr<ImageDecoder> ImageDecoder::Create() {
return std::make_unique<SafeImageDecoder>();
}
ImageDecoder::ImageDecoder() = default;
ImageDecoder::~ImageDecoder() = default;
} // namespace upboarding
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_UPBOARDING_QUERY_TILES_INTERNAL_IMAGE_DECODER_H_
#define CHROME_BROWSER_UPBOARDING_QUERY_TILES_INTERNAL_IMAGE_DECODER_H_
#include <memory>
#include <vector>
#include "base/callback.h"
namespace gfx {
class Size;
} // namespace gfx
class SkBitmap;
namespace upboarding {
// Decodes an image into bitmap, to be consumed by UI.
class ImageDecoder {
public:
using DecodeCallback = base::OnceCallback<void(const SkBitmap&)>;
using EncodedData = std::vector<uint8_t>;
static std::unique_ptr<ImageDecoder> Create();
ImageDecoder(const ImageDecoder&) = delete;
ImageDecoder& operator=(const ImageDecoder&) = delete;
virtual ~ImageDecoder();
// Decodes the image. |data| should be moved into this function.
virtual void Decode(EncodedData data,
const gfx::Size& output_size,
DecodeCallback decode_callback) = 0;
protected:
ImageDecoder();
};
} // namespace upboarding
#endif // CHROME_BROWSER_UPBOARDING_QUERY_TILES_INTERNAL_IMAGE_DECODER_H_
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/upboarding/query_tiles/internal/image_decoder.h"
#include <string>
#include <utility>
#include "base/bind.h"
#include "base/files/file_util.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/test/bind_test_util.h"
#include "base/test/task_environment.h"
#include "services/data_decoder/public/cpp/test_support/in_process_data_decoder.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/size.h"
namespace upboarding {
namespace {
const base::FilePath::CharType kTestDataDir[] =
FILE_PATH_LITERAL("chrome/test/data/image_decoding/droids.jpg");
class ImageDecoderTest : public testing::Test {
public:
ImageDecoderTest() = default;
ImageDecoderTest(const ImageDecoderTest&) = delete;
ImageDecoderTest& operator=(const ImageDecoderTest&) = delete;
~ImageDecoderTest() override = default;
void SetUp() override { image_decoder_ = ImageDecoder::Create(); }
protected:
void Decode(ImageDecoder::EncodedData data,
const gfx::Size& size,
ImageDecoder::DecodeCallback callback) {
image_decoder_->Decode(std::move(data), size, std::move(callback));
}
private:
// Needed test support objects.
base::test::TaskEnvironment task_environment_;
data_decoder::test::InProcessDataDecoder decoder_service;
std::unique_ptr<ImageDecoder> image_decoder_;
};
// Verifies empty input will result in empty output.
TEST_F(ImageDecoderTest, DecodeEmpty) {
base::RunLoop loop;
auto callback = base::BindLambdaForTesting([&](const SkBitmap& bitmap) {
EXPECT_TRUE(bitmap.empty());
loop.Quit();
});
Decode(ImageDecoder::EncodedData(), gfx::Size(1, 1), std::move(callback));
loop.Run();
}
// Decodes an image.
TEST_F(ImageDecoderTest, Decode) {
// Read in a test image.
base::FilePath data_dir;
ASSERT_TRUE(base::PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
base::FilePath file_path = data_dir.Append(base::FilePath(kTestDataDir));
std::string file_data;
ASSERT_TRUE(base::ReadFileToString(file_path, &file_data));
EXPECT_FALSE(file_data.empty());
ImageDecoder::EncodedData data(file_data.begin(), file_data.end());
EXPECT_FALSE(data.empty());
// Decode the image data.
base::RunLoop loop;
auto callback = base::BindLambdaForTesting([&](const SkBitmap& bitmap) {
EXPECT_FALSE(bitmap.empty());
loop.Quit();
});
Decode(std::move(data), gfx::Size(16, 16), std::move(callback));
loop.Run();
}
} // namespace
} // namespace upboarding
...@@ -3653,7 +3653,7 @@ test("unit_tests") { ...@@ -3653,7 +3653,7 @@ test("unit_tests") {
"//chrome/browser/media/router:unittests", "//chrome/browser/media/router:unittests",
"//chrome/browser/notifications:unit_tests", "//chrome/browser/notifications:unit_tests",
"//chrome/browser/payments:unittests", "//chrome/browser/payments:unittests",
"//chrome/browser/upboarding/query_tiles:unit_tests", "//chrome/browser/upboarding:unit_tests",
"//chrome/browser/updates/announcement_notification:unit_tests", "//chrome/browser/updates/announcement_notification:unit_tests",
"//chrome/common:test_support", "//chrome/common:test_support",
"//chrome/common/media_router:test_support", "//chrome/common/media_router:test_support",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment