1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/arc/wallpaper/arc_wallpaper_service.h"
#include <stdlib.h>
#include <string>
#include <utility>
#include "ash/public/cpp/wallpaper/wallpaper_controller.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/singleton.h"
#include "base/task/thread_pool.h"
#include "chrome/browser/ui/ash/wallpaper/wallpaper_controller_client_impl.h"
#include "chromeos/ash/experiences/arc/arc_browser_context_keyed_service_factory_base.h"
#include "chromeos/ash/experiences/arc/session/arc_bridge_service.h"
#include "components/account_id/account_id.h"
#include "components/user_manager/user.h"
#include "components/user_manager/user_manager.h"
#include "content/public/browser/browser_thread.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_util.h"
using user_manager::UserManager;
namespace arc {
namespace {
constexpr char kAndroidWallpaperFilename[] = "android.jpg";
std::vector<uint8_t> EncodeImagePng(const gfx::ImageSkia& image) {
std::optional<std::vector<uint8_t>> result =
gfx::PNGCodec::FastEncodeBGRASkBitmap(*image.bitmap(),
/*discard_transparency=*/true);
return result.value_or(std::vector<uint8_t>());
}
// Singleton factory for ArcWallpaperService.
class ArcWallpaperServiceFactory
: public internal::ArcBrowserContextKeyedServiceFactoryBase<
ArcWallpaperService,
ArcWallpaperServiceFactory> {
public:
// Factory name used by ArcBrowserContextKeyedServiceFactoryBase.
static constexpr const char* kName = "ArcWallpaperServiceFactory";
static ArcWallpaperServiceFactory* GetInstance() {
return base::Singleton<ArcWallpaperServiceFactory>::get();
}
private:
friend base::DefaultSingletonTraits<ArcWallpaperServiceFactory>;
ArcWallpaperServiceFactory() = default;
~ArcWallpaperServiceFactory() override = default;
};
class DecodeRequestSenderImpl
: public ArcWallpaperService::DecodeRequestSender {
public:
void SendDecodeRequest(ImageDecoder::ImageRequest* request,
const std::vector<uint8_t>& data) override {
ImageDecoder::StartWithOptions(request, data, ImageDecoder::DEFAULT_CODEC,
true, gfx::Size());
}
};
} // namespace
class ArcWallpaperService::DecodeRequest : public ImageDecoder::ImageRequest {
public:
DecodeRequest(ArcWallpaperService* service, int32_t android_id)
: service_(service), android_id_(android_id) {}
DecodeRequest(const DecodeRequest&) = delete;
DecodeRequest& operator=(const DecodeRequest&) = delete;
~DecodeRequest() override = default;
// ImageDecoder::ImageRequest overrides.
void OnImageDecoded(const SkBitmap& bitmap) override {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
// Make the SkBitmap immutable as we won't modify it. This is important
// because otherwise it gets duplicated during painting, wasting memory.
SkBitmap immutable_bitmap(bitmap);
immutable_bitmap.setImmutable();
gfx::ImageSkia image = gfx::ImageSkia::CreateFrom1xBitmap(immutable_bitmap);
image.MakeThreadSafe();
service_->OnWallpaperDecoded(image, android_id_);
}
void OnDecodeImageFailed() override {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
DLOG(ERROR) << "Failed to decode wallpaper image.";
service_->NotifyWallpaperChangedAndReset(android_id_);
}
private:
// ArcWallpaperService owns DecodeRequest, so it will outlive this.
const raw_ptr<ArcWallpaperService> service_;
const int32_t android_id_;
};
ArcWallpaperService::DecodeRequestSender::~DecodeRequestSender() = default;
void ArcWallpaperService::SetDecodeRequestSenderForTesting(
std::unique_ptr<DecodeRequestSender> sender) {
decode_request_sender_ = std::move(sender);
}
// static
ArcWallpaperService* ArcWallpaperService::GetForBrowserContext(
content::BrowserContext* context) {
return ArcWallpaperServiceFactory::GetForBrowserContext(context);
}
ArcWallpaperService::ArcWallpaperService(content::BrowserContext* context,
ArcBridgeService* bridge_service)
: arc_bridge_service_(bridge_service),
decode_request_sender_(std::make_unique<DecodeRequestSenderImpl>()) {
arc_bridge_service_->wallpaper()->SetHost(this);
}
ArcWallpaperService::~ArcWallpaperService() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
arc_bridge_service_->wallpaper()->SetHost(nullptr);
}
void ArcWallpaperService::SetWallpaper(const std::vector<uint8_t>& data,
int32_t wallpaper_id) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (wallpaper_id == 0)
wallpaper_id = -1;
// Previous request will be cancelled at destructor of
// ImageDecoder::ImageRequest.
decode_request_ = std::make_unique<DecodeRequest>(this, wallpaper_id);
DCHECK(decode_request_sender_);
decode_request_sender_->SendDecodeRequest(decode_request_.get(), data);
}
void ArcWallpaperService::SetDefaultWallpaper() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
// Previous request will be cancelled at destructor of
// ImageDecoder::ImageRequest.
decode_request_.reset();
const user_manager::User* const primary_user =
UserManager::Get()->GetPrimaryUser();
ash::WallpaperController::Get()->SetDefaultWallpaper(
primary_user->GetAccountId(),
primary_user->is_active() /*show_wallpaper=*/, base::DoNothing());
}
void ArcWallpaperService::GetWallpaper(GetWallpaperCallback callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
gfx::ImageSkia image = ash::WallpaperController::Get()->GetWallpaperImage();
if (!image.isNull())
image.SetReadOnly();
base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE, {base::MayBlock(), base::TaskPriority::BEST_EFFORT},
base::BindOnce(&EncodeImagePng, image), std::move(callback));
}
void ArcWallpaperService::OnWallpaperDecoded(const gfx::ImageSkia& image,
int32_t android_id) {
const AccountId account_id =
UserManager::Get()->GetPrimaryUser()->GetAccountId();
const bool result =
WallpaperControllerClientImpl::Get()->SetThirdPartyWallpaper(
account_id, kAndroidWallpaperFilename,
ash::WALLPAPER_LAYOUT_CENTER_CROPPED, image);
// Notify the Android side whether the request is going through or not.
if (result)
NotifyWallpaperChanged(android_id);
else
NotifyWallpaperChangedAndReset(android_id);
}
void ArcWallpaperService::NotifyWallpaperChanged(int android_id) {
mojom::WallpaperInstance* const wallpaper_instance =
ARC_GET_INSTANCE_FOR_METHOD(arc_bridge_service_->wallpaper(),
OnWallpaperChanged);
if (wallpaper_instance == nullptr)
return;
wallpaper_instance->OnWallpaperChanged(android_id);
}
void ArcWallpaperService::NotifyWallpaperChangedAndReset(int32_t android_id) {
// Invoke NotifyWallpaperChanged so that setWallpaper completes in Android
// side.
NotifyWallpaperChanged(android_id);
// Invoke NotifyWallpaperChanged with -1 so that Android side regards the
// wallpaper of |android_id_| is no longer used at Chrome side.
NotifyWallpaperChanged(-1);
}
// static
void ArcWallpaperService::EnsureFactoryBuilt() {
ArcWallpaperServiceFactory::GetInstance();
}
} // namespace arc
|