Commit 7f46a6ca authored by Jeremy Roman's avatar Jeremy Roman Committed by Commit Bot

[extensions] Modernize wallpaper API value use a little and reduce copies.

Bug: 646113
Change-Id: Ib344bd52cf5fe266f6bf260745eeeff1ca8296da
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2233691Reviewed-by: default avatarXiaoqian Dai <xdai@chromium.org>
Commit-Queue: Jeremy Roman <jbroman@chromium.org>
Auto-Submit: Jeremy Roman <jbroman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#776645}
parent 097cd852
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
#include "base/check_op.h" #include "base/check_op.h"
#include "base/files/file_util.h" #include "base/files/file_util.h"
#include "base/lazy_instance.h" #include "base/lazy_instance.h"
#include "base/memory/ref_counted_memory.h"
#include "base/sequenced_task_runner.h" #include "base/sequenced_task_runner.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "base/values.h" #include "base/values.h"
...@@ -194,19 +193,8 @@ void WallpaperSetWallpaperFunction::OnWallpaperDecoded( ...@@ -194,19 +193,8 @@ void WallpaperSetWallpaperFunction::OnWallpaperDecoded(
// We need to generate thumbnail image anyway to make the current third party // We need to generate thumbnail image anyway to make the current third party
// wallpaper syncable through different devices. // wallpaper syncable through different devices.
image.EnsureRepsForSupportedScales(); image.EnsureRepsForSupportedScales();
scoped_refptr<base::RefCountedBytes> thumbnail_data; std::vector<uint8_t> thumbnail_data = GenerateThumbnail(
GenerateThumbnail( image, gfx::Size(kWallpaperThumbnailWidth, kWallpaperThumbnailHeight));
image, gfx::Size(kWallpaperThumbnailWidth, kWallpaperThumbnailHeight),
&thumbnail_data);
scoped_refptr<base::RefCountedBytes> original_data;
GenerateThumbnail(image, image.size(), &original_data);
std::unique_ptr<Value> original_result = Value::CreateWithCopiedBuffer(
reinterpret_cast<const char*>(original_data->front()),
original_data->size());
std::unique_ptr<Value> thumbnail_result = Value::CreateWithCopiedBuffer(
reinterpret_cast<const char*>(thumbnail_data->front()),
thumbnail_data->size());
// Inform the native Wallpaper Picker Application that the current wallpaper // Inform the native Wallpaper Picker Application that the current wallpaper
// has been modified by a third party application. // has been modified by a third party application.
...@@ -214,10 +202,11 @@ void WallpaperSetWallpaperFunction::OnWallpaperDecoded( ...@@ -214,10 +202,11 @@ void WallpaperSetWallpaperFunction::OnWallpaperDecoded(
Profile* profile = Profile::FromBrowserContext(browser_context()); Profile* profile = Profile::FromBrowserContext(browser_context());
extensions::EventRouter* event_router = extensions::EventRouter* event_router =
extensions::EventRouter::Get(profile); extensions::EventRouter::Get(profile);
std::unique_ptr<base::ListValue> event_args(new base::ListValue());
event_args->Append(original_result->CreateDeepCopy()); base::Value event_args(Value::Type::LIST);
event_args->Append(thumbnail_result->CreateDeepCopy()); event_args.Append(Value(GenerateThumbnail(image, image.size())));
event_args->AppendString( event_args.Append(Value(thumbnail_data));
event_args.Append(
extensions::api::wallpaper::ToString(params_->details.layout)); extensions::api::wallpaper::ToString(params_->details.layout));
// Setting wallpaper from right click menu in 'Files' app is a feature that // Setting wallpaper from right click menu in 'Files' app is a feature that
// was implemented in crbug.com/578935. Since 'Files' app is a built-in v1 // was implemented in crbug.com/578935. Since 'Files' app is a built-in v1
...@@ -226,21 +215,20 @@ void WallpaperSetWallpaperFunction::OnWallpaperDecoded( ...@@ -226,21 +215,20 @@ void WallpaperSetWallpaperFunction::OnWallpaperDecoded(
// and it should not appear in the wallpaper grid in the Wallpaper Picker. // and it should not appear in the wallpaper grid in the Wallpaper Picker.
// But we should not display the 'wallpaper-set-by-mesage' since it might // But we should not display the 'wallpaper-set-by-mesage' since it might
// introduce confusion as shown in crbug.com/599407. // introduce confusion as shown in crbug.com/599407.
event_args->AppendString( event_args.Append((extension()->id() == file_manager::kFileManagerAppId)
(extension()->id() == file_manager::kFileManagerAppId) ? base::StringPiece()
? std::string() : extension()->name());
: extension()->name());
std::unique_ptr<extensions::Event> event(new extensions::Event( std::unique_ptr<extensions::Event> event(new extensions::Event(
extensions::events::WALLPAPER_PRIVATE_ON_WALLPAPER_CHANGED_BY_3RD_PARTY, extensions::events::WALLPAPER_PRIVATE_ON_WALLPAPER_CHANGED_BY_3RD_PARTY,
extensions::api::wallpaper_private::OnWallpaperChangedBy3rdParty:: extensions::api::wallpaper_private::OnWallpaperChangedBy3rdParty::
kEventName, kEventName,
std::move(event_args))); base::ListValue::From(std::make_unique<Value>(std::move(event_args)))));
event_router->DispatchEventToExtension(extension_misc::kWallpaperManagerId, event_router->DispatchEventToExtension(extension_misc::kWallpaperManagerId,
std::move(event)); std::move(event));
} }
Respond(params_->details.thumbnail Respond(params_->details.thumbnail
? OneArgument(thumbnail_result->CreateDeepCopy()) ? OneArgument(std::make_unique<Value>(std::move(thumbnail_data)))
: NoArguments()); : NoArguments());
} }
......
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
#include "chrome/browser/chromeos/extensions/wallpaper_function_base.h" #include "chrome/browser/chromeos/extensions/wallpaper_function_base.h"
#include "base/memory/ref_counted_memory.h"
#include "base/metrics/histogram_macros.h" #include "base/metrics/histogram_macros.h"
#include "base/stl_util.h" #include "base/stl_util.h"
#include "base/synchronization/atomic_flag.h" #include "base/synchronization/atomic_flag.h"
...@@ -182,12 +181,12 @@ void WallpaperFunctionBase::OnFailure(const std::string& error) { ...@@ -182,12 +181,12 @@ void WallpaperFunctionBase::OnFailure(const std::string& error) {
Respond(Error(error)); Respond(Error(error));
} }
void WallpaperFunctionBase::GenerateThumbnail( std::vector<uint8_t> WallpaperFunctionBase::GenerateThumbnail(
const gfx::ImageSkia& image, const gfx::ImageSkia& image,
const gfx::Size& size, const gfx::Size& size) {
scoped_refptr<base::RefCountedBytes>* thumbnail_data_out) { std::vector<uint8_t> data_out;
*thumbnail_data_out = new base::RefCountedBytes();
gfx::JPEGCodec::Encode( gfx::JPEGCodec::Encode(
*wallpaper_api_util::ScaleAspectRatioAndCropCenter(size, image).bitmap(), *wallpaper_api_util::ScaleAspectRatioAndCropCenter(size, image).bitmap(),
90 /*quality=*/, &(*thumbnail_data_out)->data()); 90 /*quality=*/, &data_out);
return data_out;
} }
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
#include "ui/gfx/image/image_skia.h" #include "ui/gfx/image/image_skia.h"
namespace base { namespace base {
class RefCountedBytes;
class SequencedTaskRunner; class SequencedTaskRunner;
} }
...@@ -69,10 +68,8 @@ class WallpaperFunctionBase : public ExtensionFunction { ...@@ -69,10 +68,8 @@ class WallpaperFunctionBase : public ExtensionFunction {
void OnFailure(const std::string& error); void OnFailure(const std::string& error);
// Resize the image to |size|, encode it and save to |thumbnail_data_out|. // Resize the image to |size|, encode it and save to |thumbnail_data_out|.
void GenerateThumbnail( std::vector<uint8_t> GenerateThumbnail(const gfx::ImageSkia& image,
const gfx::ImageSkia& image, const gfx::Size& size);
const gfx::Size& size,
scoped_refptr<base::RefCountedBytes>* thumbnail_data_out);
private: private:
virtual void OnWallpaperDecoded(const gfx::ImageSkia& wallpaper) = 0; virtual void OnWallpaperDecoded(const gfx::ImageSkia& wallpaper) = 0;
......
...@@ -10,10 +10,10 @@ ...@@ -10,10 +10,10 @@
#include "ash/public/cpp/ash_features.h" #include "ash/public/cpp/ash_features.h"
#include "base/bind.h" #include "base/bind.h"
#include "base/command_line.h" #include "base/command_line.h"
#include "base/containers/span.h"
#include "base/files/file_enumerator.h" #include "base/files/file_enumerator.h"
#include "base/files/file_util.h" #include "base/files/file_util.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/ref_counted_memory.h"
#include "base/metrics/histogram_macros.h" #include "base/metrics/histogram_macros.h"
#include "base/path_service.h" #include "base/path_service.h"
#include "base/sequenced_task_runner.h" #include "base/sequenced_task_runner.h"
...@@ -468,13 +468,10 @@ void WallpaperPrivateSetCustomWallpaperFunction::OnWallpaperDecoded( ...@@ -468,13 +468,10 @@ void WallpaperPrivateSetCustomWallpaperFunction::OnWallpaperDecoded(
if (params->generate_thumbnail) { if (params->generate_thumbnail) {
image.EnsureRepsForSupportedScales(); image.EnsureRepsForSupportedScales();
scoped_refptr<base::RefCountedBytes> thumbnail_data; std::vector<uint8_t> thumbnail_data = GenerateThumbnail(
GenerateThumbnail( image, gfx::Size(kWallpaperThumbnailWidth, kWallpaperThumbnailHeight));
image, gfx::Size(kWallpaperThumbnailWidth, kWallpaperThumbnailHeight), Respond(
&thumbnail_data); OneArgument(std::make_unique<base::Value>(std::move(thumbnail_data))));
Respond(OneArgument(Value::CreateWithCopiedBuffer(
reinterpret_cast<const char*>(thumbnail_data->front()),
thumbnail_data->size())));
} else { } else {
Respond(NoArguments()); Respond(NoArguments());
} }
...@@ -580,8 +577,8 @@ void WallpaperPrivateGetThumbnailFunction::FileNotLoaded() { ...@@ -580,8 +577,8 @@ void WallpaperPrivateGetThumbnailFunction::FileNotLoaded() {
void WallpaperPrivateGetThumbnailFunction::FileLoaded( void WallpaperPrivateGetThumbnailFunction::FileLoaded(
const std::string& data) { const std::string& data) {
Respond( Respond(OneArgument(
OneArgument(Value::CreateWithCopiedBuffer(data.c_str(), data.size()))); std::make_unique<Value>(base::as_bytes(base::make_span(data)))));
} }
void WallpaperPrivateGetThumbnailFunction::Get(const base::FilePath& path) { void WallpaperPrivateGetThumbnailFunction::Get(const base::FilePath& path) {
...@@ -598,7 +595,7 @@ void WallpaperPrivateGetThumbnailFunction::Get(const base::FilePath& path) { ...@@ -598,7 +595,7 @@ void WallpaperPrivateGetThumbnailFunction::Get(const base::FilePath& path) {
content::GetUIThreadTaskRunner({})->PostTask( content::GetUIThreadTaskRunner({})->PostTask(
FROM_HERE, FROM_HERE,
base::BindOnce(&WallpaperPrivateGetThumbnailFunction::FileLoaded, base::BindOnce(&WallpaperPrivateGetThumbnailFunction::FileLoaded,
this, data)); this, std::move(data)));
} }
} else { } else {
content::GetUIThreadTaskRunner({})->PostTask( content::GetUIThreadTaskRunner({})->PostTask(
...@@ -864,11 +861,10 @@ WallpaperPrivateGetCurrentWallpaperThumbnailFunction::Run() { ...@@ -864,11 +861,10 @@ WallpaperPrivateGetCurrentWallpaperThumbnailFunction::Run() {
auto image = WallpaperControllerClient::Get()->GetWallpaperImage(); auto image = WallpaperControllerClient::Get()->GetWallpaperImage();
gfx::Size thumbnail_size(params->thumbnail_width, params->thumbnail_height); gfx::Size thumbnail_size(params->thumbnail_width, params->thumbnail_height);
image.EnsureRepsForSupportedScales(); image.EnsureRepsForSupportedScales();
scoped_refptr<base::RefCountedBytes> thumbnail_data; std::vector<uint8_t> thumbnail_data =
GenerateThumbnail(image, thumbnail_size, &thumbnail_data); GenerateThumbnail(image, thumbnail_size);
return RespondNow(OneArgument(std::make_unique<Value>( return RespondNow(
Value::BlobStorage(thumbnail_data->front(), OneArgument(std::make_unique<base::Value>(std::move(thumbnail_data))));
thumbnail_data->front() + thumbnail_data->size()))));
} }
void WallpaperPrivateGetCurrentWallpaperThumbnailFunction::OnWallpaperDecoded( void WallpaperPrivateGetCurrentWallpaperThumbnailFunction::OnWallpaperDecoded(
......
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