Commit d31574bb authored by Shakti Sahu's avatar Shakti Sahu Committed by Commit Bot

Moved thumbnail generation code from chrome/browser/android to chrome/browser

This will be required for supplying thumbnails to download OfflineContentProvider.
Since the invoking code lives in chrome/browser, we are moving the thumbnail
generation part to platform-independent code as well

Bug: 855330
Change-Id: Ic397e50faa511c83f1241d4c961c5e888ea8d272
Reviewed-on: https://chromium-review.googlesource.com/1111652Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Commit-Queue: Shakti Sahu <shaktisahu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#570252}
parent 87ed5723
...@@ -436,6 +436,8 @@ jumbo_split_static_library("browser") { ...@@ -436,6 +436,8 @@ jumbo_split_static_library("browser") {
"download/download_ui_controller.cc", "download/download_ui_controller.cc",
"download/download_ui_controller.h", "download/download_ui_controller.h",
"download/drag_download_item.h", "download/drag_download_item.h",
"download/image_thumbnail_request.cc",
"download/image_thumbnail_request.h",
"download/save_package_file_picker.cc", "download/save_package_file_picker.cc",
"download/save_package_file_picker.h", "download/save_package_file_picker.h",
"download/trusted_sources_manager.cc", "download/trusted_sources_manager.cc",
......
...@@ -7,137 +7,15 @@ ...@@ -7,137 +7,15 @@
#include <memory> #include <memory>
#include "base/android/jni_string.h" #include "base/android/jni_string.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/memory/weak_ptr.h"
#include "base/task_scheduler/post_task.h"
#include "base/threading/thread_restrictions.h" #include "base/threading/thread_restrictions.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "jni/ThumbnailGenerator_jni.h" #include "jni/ThumbnailGenerator_jni.h"
#include "skia/ext/image_operations.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/android/java_bitmap.h" #include "ui/gfx/android/java_bitmap.h"
#include "ui/gfx/skbitmap_operations.h"
class SkBitmap; class SkBitmap;
using base::android::JavaParamRef; using base::android::JavaParamRef;
namespace {
// Ignore image files that are too large to avoid long delays.
const int64_t kMaxImageSize = 10 * 1024 * 1024; // 10 MB
std::string LoadImageData(const base::FilePath& path) {
base::AssertBlockingAllowed();
// Confirm that the file's size is within our threshold.
int64_t file_size;
if (!base::GetFileSize(path, &file_size) || file_size > kMaxImageSize) {
LOG(ERROR) << "Unexpected file size: " << path.MaybeAsASCII() << ", "
<< file_size;
return std::string();
}
std::string data;
bool success = base::ReadFileToString(path, &data);
// Make sure the file isn't empty.
if (!success || data.empty()) {
LOG(ERROR) << "Failed to read file: " << path.MaybeAsASCII();
return std::string();
}
return data;
}
SkBitmap ScaleDownBitmap(int icon_size, const SkBitmap& decoded_image) {
DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
if (decoded_image.drawsNothing())
return decoded_image;
// Shrink the image down so that its smallest dimension is equal to or
// smaller than the requested size.
int min_dimension = std::min(decoded_image.width(), decoded_image.height());
if (min_dimension <= icon_size)
return decoded_image;
uint64_t width = static_cast<uint64_t>(decoded_image.width());
uint64_t height = static_cast<uint64_t>(decoded_image.height());
return skia::ImageOperations::Resize(
decoded_image, skia::ImageOperations::RESIZE_BEST,
width * icon_size / min_dimension, height * icon_size / min_dimension);
}
class ImageThumbnailRequest : public ImageDecoder::ImageRequest {
public:
ImageThumbnailRequest(int icon_size,
base::OnceCallback<void(const SkBitmap&)> callback)
: icon_size_(icon_size),
callback_(std::move(callback)),
weak_ptr_factory_(this) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
}
~ImageThumbnailRequest() override {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
}
void Start(const base::FilePath& path) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
base::PostTaskWithTraitsAndReplyWithResult(
FROM_HERE, {base::MayBlock(), base::TaskPriority::USER_BLOCKING},
base::BindOnce(&LoadImageData, path),
base::BindOnce(&ImageThumbnailRequest::OnLoadComplete,
weak_ptr_factory_.GetWeakPtr()));
}
private:
// ImageDecoder::ImageRequest implementation.
void OnImageDecoded(const SkBitmap& decoded_image) override {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
base::PostTaskWithTraitsAndReplyWithResult(
FROM_HERE, {base::MayBlock(), base::TaskPriority::USER_BLOCKING},
base::BindOnce(&ScaleDownBitmap, icon_size_, decoded_image),
base::BindOnce(&ImageThumbnailRequest::FinishRequest,
weak_ptr_factory_.GetWeakPtr()));
}
void OnDecodeImageFailed() override {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
LOG(ERROR) << "Failed to decode image.";
FinishRequest(SkBitmap());
}
void OnLoadComplete(const std::string& data) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (data.empty()) {
FinishRequest(SkBitmap());
return;
}
ImageDecoder::Start(this, data);
}
void FinishRequest(const SkBitmap& thumbnail) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE,
base::BindOnce(std::move(callback_), thumbnail));
delete this;
}
const int icon_size_;
base::OnceCallback<void(const SkBitmap&)> callback_;
base::WeakPtrFactory<ImageThumbnailRequest> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(ImageThumbnailRequest);
};
} // namespace
ThumbnailGenerator::ThumbnailGenerator(const JavaParamRef<jobject>& jobj) ThumbnailGenerator::ThumbnailGenerator(const JavaParamRef<jobject>& jobj)
: java_delegate_(jobj), weak_factory_(this) { : java_delegate_(jobj), weak_factory_(this) {
DCHECK(!jobj.is_null()); DCHECK(!jobj.is_null());
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
#include "base/android/jni_android.h" #include "base/android/jni_android.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "chrome/browser/image_decoder.h" #include "chrome/browser/download/image_thumbnail_request.h"
// Kicks off asynchronous pipelines for creating thumbnails for local files. // Kicks off asynchronous pipelines for creating thumbnails for local files.
// The native-side ThumbnailGenerator is owned by the Java-side and can be // The native-side ThumbnailGenerator is owned by the Java-side and can be
......
// Copyright 2018 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/download/image_thumbnail_request.h"
#include <memory>
#include "base/files/file_util.h"
#include "base/task_scheduler/post_task.h"
#include "base/threading/thread_restrictions.h"
#include "content/public/browser/browser_thread.h"
#include "skia/ext/image_operations.h"
namespace {
// Ignore image files that are too large to avoid long delays.
const int64_t kMaxImageSize = 10 * 1024 * 1024; // 10 MB
std::string LoadImageData(const base::FilePath& path) {
base::AssertBlockingAllowed();
// Confirm that the file's size is within our threshold.
int64_t file_size;
if (!base::GetFileSize(path, &file_size) || file_size > kMaxImageSize) {
LOG(ERROR) << "Unexpected file size: " << path.MaybeAsASCII() << ", "
<< file_size;
return std::string();
}
std::string data;
bool success = base::ReadFileToString(path, &data);
// Make sure the file isn't empty.
if (!success || data.empty()) {
LOG(ERROR) << "Failed to read file: " << path.MaybeAsASCII();
return std::string();
}
return data;
}
SkBitmap ScaleDownBitmap(int icon_size, const SkBitmap& decoded_image) {
DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
if (decoded_image.drawsNothing())
return decoded_image;
// Shrink the image down so that its smallest dimension is equal to or
// smaller than the requested size.
int min_dimension = std::min(decoded_image.width(), decoded_image.height());
if (min_dimension <= icon_size)
return decoded_image;
uint64_t width = static_cast<uint64_t>(decoded_image.width());
uint64_t height = static_cast<uint64_t>(decoded_image.height());
return skia::ImageOperations::Resize(
decoded_image, skia::ImageOperations::RESIZE_BEST,
width * icon_size / min_dimension, height * icon_size / min_dimension);
}
} // namespace
ImageThumbnailRequest::ImageThumbnailRequest(
int icon_size,
base::OnceCallback<void(const SkBitmap&)> callback)
: icon_size_(icon_size),
callback_(std::move(callback)),
weak_ptr_factory_(this) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
}
ImageThumbnailRequest::~ImageThumbnailRequest() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
}
void ImageThumbnailRequest::Start(const base::FilePath& path) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
base::PostTaskWithTraitsAndReplyWithResult(
FROM_HERE, {base::MayBlock(), base::TaskPriority::USER_BLOCKING},
base::BindOnce(&LoadImageData, path),
base::BindOnce(&ImageThumbnailRequest::OnLoadComplete,
weak_ptr_factory_.GetWeakPtr()));
}
void ImageThumbnailRequest::OnImageDecoded(const SkBitmap& decoded_image) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
base::PostTaskWithTraitsAndReplyWithResult(
FROM_HERE, {base::MayBlock(), base::TaskPriority::USER_BLOCKING},
base::BindOnce(&ScaleDownBitmap, icon_size_, decoded_image),
base::BindOnce(&ImageThumbnailRequest::FinishRequest,
weak_ptr_factory_.GetWeakPtr()));
}
void ImageThumbnailRequest::OnDecodeImageFailed() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
LOG(ERROR) << "Failed to decode image.";
FinishRequest(SkBitmap());
}
void ImageThumbnailRequest::OnLoadComplete(const std::string& data) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (data.empty()) {
FinishRequest(SkBitmap());
return;
}
ImageDecoder::Start(this, data);
}
void ImageThumbnailRequest::FinishRequest(const SkBitmap& thumbnail) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE,
base::BindOnce(std::move(callback_), thumbnail));
delete this;
}
// Copyright 2018 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_DOWNLOAD_IMAGE_THUMBNAIL_REQUEST_H_
#define CHROME_BROWSER_DOWNLOAD_IMAGE_THUMBNAIL_REQUEST_H_
#include <string>
#include "base/files/file_path.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/image_decoder.h"
// Helper class to generate thumbnail for a given local image file with a given
// max size. Must be invoked on the browser thread.
class ImageThumbnailRequest : public ImageDecoder::ImageRequest {
public:
ImageThumbnailRequest(int icon_size,
base::OnceCallback<void(const SkBitmap&)> callback);
~ImageThumbnailRequest() override;
// Kicks off an asynchronous process to retrieve the thumbnail for the file
// located at |file_path| with a max size of |icon_size_| in each dimension.
// Invokes the |callback_| method when finished.
void Start(const base::FilePath& path);
private:
// ImageDecoder::ImageRequest implementation.
void OnImageDecoded(const SkBitmap& decoded_image) override;
void OnDecodeImageFailed() override;
void OnLoadComplete(const std::string& data);
void FinishRequest(const SkBitmap& thumbnail);
const int icon_size_;
base::OnceCallback<void(const SkBitmap&)> callback_;
base::WeakPtrFactory<ImageThumbnailRequest> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(ImageThumbnailRequest);
};
#endif // CHROME_BROWSER_DOWNLOAD_IMAGE_THUMBNAIL_REQUEST_H_
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