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::Delegate
{
47 explicit UnsafeWallpaperDecoder(scoped_refptr
<WallpaperFunctionBase
> function
)
48 : function_(function
) {
51 void Start(const std::vector
<char>& image_data
) {
52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
54 // This function can only be called after user login. It is fine to use
55 // unsafe image decoder here. Before user login, a robust jpeg decoder will
57 CHECK(chromeos::LoginState::Get()->IsUserLoggedIn());
58 unsafe_image_decoder_
= new ImageDecoder(this, image_data
,
59 ImageDecoder::DEFAULT_CODEC
);
60 unsafe_image_decoder_
->set_shrink_to_fit(true);
62 scoped_refptr
<base::MessageLoopProxy
> task_runner
=
63 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI
);
64 unsafe_image_decoder_
->Start(task_runner
);
71 void OnImageDecoded(const ImageDecoder
* decoder
,
72 const SkBitmap
& decoded_image
) override
{
73 // Make the SkBitmap immutable as we won't modify it. This is important
74 // because otherwise it gets duplicated during painting, wasting memory.
75 SkBitmap
immutable(decoded_image
);
76 immutable
.setImmutable();
77 gfx::ImageSkia final_image
= gfx::ImageSkia::CreateFrom1xBitmap(immutable
);
78 final_image
.MakeThreadSafe();
79 if (cancel_flag_
.IsSet()) {
80 function_
->OnCancel();
84 function_
->OnWallpaperDecoded(final_image
);
88 void OnDecodeImageFailed(const ImageDecoder
* decoder
) override
{
90 l10n_util::GetStringUTF8(IDS_WALLPAPER_MANAGER_INVALID_WALLPAPER
));
95 scoped_refptr
<WallpaperFunctionBase
> function_
;
96 scoped_refptr
<ImageDecoder
> unsafe_image_decoder_
;
97 base::CancellationFlag cancel_flag_
;
99 DISALLOW_COPY_AND_ASSIGN(UnsafeWallpaperDecoder
);
102 WallpaperFunctionBase::UnsafeWallpaperDecoder
*
103 WallpaperFunctionBase::unsafe_wallpaper_decoder_
;
105 WallpaperFunctionBase::WallpaperFunctionBase() {
108 WallpaperFunctionBase::~WallpaperFunctionBase() {
111 void WallpaperFunctionBase::StartDecode(const std::vector
<char>& data
) {
112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
113 if (unsafe_wallpaper_decoder_
)
114 unsafe_wallpaper_decoder_
->Cancel();
115 unsafe_wallpaper_decoder_
= new UnsafeWallpaperDecoder(this);
116 unsafe_wallpaper_decoder_
->Start(data
);
119 void WallpaperFunctionBase::OnCancel() {
120 unsafe_wallpaper_decoder_
= NULL
;
121 SetError(wallpaper_api_util::kCancelWallpaperMessage
);
125 void WallpaperFunctionBase::OnFailure(const std::string
& error
) {
126 unsafe_wallpaper_decoder_
= NULL
;