1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/extensions/wallpaper_function_base.h"
7 #include "base/synchronization/cancellation_flag.h"
8 #include "chrome/browser/image_decoder.h"
9 #include "chrome/grit/generated_resources.h"
10 #include "chromeos/login/login_state.h"
11 #include "components/wallpaper/wallpaper_layout.h"
12 #include "ui/base/l10n/l10n_util.h"
14 using content::BrowserThread
;
16 namespace wallpaper_api_util
{
19 // Keeps in sync (same order) with WallpaperLayout enum in header file.
20 const char* const kWallpaperLayoutArrays
[] = {
27 const int kWallpaperLayoutCount
= arraysize(kWallpaperLayoutArrays
);
31 const char kCancelWallpaperMessage
[] = "Set wallpaper was canceled.";
33 wallpaper::WallpaperLayout
GetLayoutEnum(const std::string
& layout
) {
34 for (int i
= 0; i
< kWallpaperLayoutCount
; i
++) {
35 if (layout
.compare(kWallpaperLayoutArrays
[i
]) == 0)
36 return static_cast<wallpaper::WallpaperLayout
>(i
);
38 // Default to use CENTER layout.
39 return wallpaper::WALLPAPER_LAYOUT_CENTER
;
42 } // namespace wallpaper_api_util
44 class WallpaperFunctionBase::UnsafeWallpaperDecoder
45 : public ImageDecoder::ImageRequest
{
47 explicit UnsafeWallpaperDecoder(scoped_refptr
<WallpaperFunctionBase
> function
)
48 : function_(function
) {}
50 void Start(const std::vector
<char>& image_data
) {
51 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
53 // This function can only be called after user login. It is fine to use
54 // unsafe image decoder here. Before user login, a robust jpeg decoder will
56 CHECK(chromeos::LoginState::Get()->IsUserLoggedIn());
57 std::string
image_data_str(image_data
.begin(), image_data
.end());
58 ImageDecoder::StartWithOptions(this, image_data_str
,
59 ImageDecoder::DEFAULT_CODEC
, true);
66 void OnImageDecoded(const SkBitmap
& decoded_image
) override
{
67 // Make the SkBitmap immutable as we won't modify it. This is important
68 // because otherwise it gets duplicated during painting, wasting memory.
69 SkBitmap
immutable(decoded_image
);
70 immutable
.setImmutable();
71 gfx::ImageSkia final_image
= gfx::ImageSkia::CreateFrom1xBitmap(immutable
);
72 final_image
.MakeThreadSafe();
73 if (cancel_flag_
.IsSet()) {
74 function_
->OnCancel();
78 function_
->OnWallpaperDecoded(final_image
);
82 void OnDecodeImageFailed() override
{
84 l10n_util::GetStringUTF8(IDS_WALLPAPER_MANAGER_INVALID_WALLPAPER
));
89 scoped_refptr
<WallpaperFunctionBase
> function_
;
90 base::CancellationFlag cancel_flag_
;
92 DISALLOW_COPY_AND_ASSIGN(UnsafeWallpaperDecoder
);
95 WallpaperFunctionBase::UnsafeWallpaperDecoder
*
96 WallpaperFunctionBase::unsafe_wallpaper_decoder_
;
98 WallpaperFunctionBase::WallpaperFunctionBase() {
101 WallpaperFunctionBase::~WallpaperFunctionBase() {
104 void WallpaperFunctionBase::StartDecode(const std::vector
<char>& data
) {
105 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
106 if (unsafe_wallpaper_decoder_
)
107 unsafe_wallpaper_decoder_
->Cancel();
108 unsafe_wallpaper_decoder_
= new UnsafeWallpaperDecoder(this);
109 unsafe_wallpaper_decoder_
->Start(data
);
112 void WallpaperFunctionBase::OnCancel() {
113 unsafe_wallpaper_decoder_
= NULL
;
114 SetError(wallpaper_api_util::kCancelWallpaperMessage
);
118 void WallpaperFunctionBase::OnFailure(const std::string
& error
) {
119 unsafe_wallpaper_decoder_
= NULL
;