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
)
49 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI
)),
50 function_(function
) {}
52 void Start(const std::vector
<char>& image_data
) {
53 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
55 // This function can only be called after user login. It is fine to use
56 // unsafe image decoder here. Before user login, a robust jpeg decoder will
58 CHECK(chromeos::LoginState::Get()->IsUserLoggedIn());
59 std::string
image_data_str(image_data
.begin(), image_data
.end());
60 ImageDecoder::StartWithOptions(this, image_data_str
,
61 ImageDecoder::DEFAULT_CODEC
, true);
68 void OnImageDecoded(const SkBitmap
& decoded_image
) override
{
69 // Make the SkBitmap immutable as we won't modify it. This is important
70 // because otherwise it gets duplicated during painting, wasting memory.
71 SkBitmap
immutable(decoded_image
);
72 immutable
.setImmutable();
73 gfx::ImageSkia final_image
= gfx::ImageSkia::CreateFrom1xBitmap(immutable
);
74 final_image
.MakeThreadSafe();
75 if (cancel_flag_
.IsSet()) {
76 function_
->OnCancel();
80 function_
->OnWallpaperDecoded(final_image
);
84 void OnDecodeImageFailed() override
{
86 l10n_util::GetStringUTF8(IDS_WALLPAPER_MANAGER_INVALID_WALLPAPER
));
91 scoped_refptr
<WallpaperFunctionBase
> function_
;
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::vector
<char>& data
) {
107 DCHECK_CURRENTLY_ON(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
;
116 SetError(wallpaper_api_util::kCancelWallpaperMessage
);
120 void WallpaperFunctionBase::OnFailure(const std::string
& error
) {
121 unsafe_wallpaper_decoder_
= NULL
;