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 "chromeos/login/login_state.h"
10 #include "grit/generated_resources.h"
11 #include "ui/base/l10n/l10n_util.h"
13 using content::BrowserThread
;
15 namespace wallpaper_api_util
{
18 // Keeps in sync (same order) with WallpaperLayout enum in header file.
19 const char* kWallpaperLayoutArrays
[] = {
26 const int kWallpaperLayoutCount
= arraysize(kWallpaperLayoutArrays
);
30 ash::WallpaperLayout
GetLayoutEnum(const std::string
& layout
) {
31 for (int i
= 0; i
< kWallpaperLayoutCount
; i
++) {
32 if (layout
.compare(kWallpaperLayoutArrays
[i
]) == 0)
33 return static_cast<ash::WallpaperLayout
>(i
);
35 // Default to use CENTER layout.
36 return ash::WALLPAPER_LAYOUT_CENTER
;
39 } // namespace wallpaper_api_util
41 class WallpaperFunctionBase::UnsafeWallpaperDecoder
42 : public ImageDecoder::Delegate
{
44 explicit UnsafeWallpaperDecoder(scoped_refptr
<WallpaperFunctionBase
> function
)
45 : function_(function
) {
48 void Start(const std::string
& image_data
) {
49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
51 // This function can only be called after user login. It is fine to use
52 // unsafe image decoder here. Before user login, a robust jpeg decoder will
54 CHECK(chromeos::LoginState::Get()->IsUserLoggedIn());
55 unsafe_image_decoder_
= new ImageDecoder(this, image_data
,
56 ImageDecoder::DEFAULT_CODEC
);
57 scoped_refptr
<base::MessageLoopProxy
> task_runner
=
58 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI
);
59 unsafe_image_decoder_
->Start(task_runner
);
66 virtual void OnImageDecoded(const ImageDecoder
* decoder
,
67 const SkBitmap
& decoded_image
) OVERRIDE
{
68 // Make the SkBitmap immutable as we won't modify it. This is important
69 // because otherwise it gets duplicated during painting, wasting memory.
70 SkBitmap
immutable(decoded_image
);
71 immutable
.setImmutable();
72 gfx::ImageSkia final_image
= gfx::ImageSkia::CreateFrom1xBitmap(immutable
);
73 final_image
.MakeThreadSafe();
74 if (cancel_flag_
.IsSet()) {
75 function_
->OnCancel();
79 function_
->OnWallpaperDecoded(final_image
);
83 virtual void OnDecodeImageFailed(const ImageDecoder
* decoder
) OVERRIDE
{
85 l10n_util::GetStringUTF8(IDS_WALLPAPER_MANAGER_INVALID_WALLPAPER
));
90 scoped_refptr
<WallpaperFunctionBase
> function_
;
91 scoped_refptr
<ImageDecoder
> unsafe_image_decoder_
;
92 base::CancellationFlag cancel_flag_
;
94 DISALLOW_COPY_AND_ASSIGN(UnsafeWallpaperDecoder
);
97 WallpaperFunctionBase::UnsafeWallpaperDecoder
*
98 WallpaperFunctionBase::unsafe_wallpaper_decoder_
;
100 WallpaperFunctionBase::WallpaperFunctionBase() {
103 WallpaperFunctionBase::~WallpaperFunctionBase() {
106 void WallpaperFunctionBase::StartDecode(const std::string
& data
) {
107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
108 if (unsafe_wallpaper_decoder_
)
109 unsafe_wallpaper_decoder_
->Cancel();
110 unsafe_wallpaper_decoder_
= new UnsafeWallpaperDecoder(this);
111 unsafe_wallpaper_decoder_
->Start(data
);
114 void WallpaperFunctionBase::OnCancel() {
115 unsafe_wallpaper_decoder_
= NULL
;
119 void WallpaperFunctionBase::OnFailure(const std::string
& error
) {
120 unsafe_wallpaper_decoder_
= NULL
;