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/metrics/histogram_macros.h"
8 #include "base/synchronization/cancellation_flag.h"
9 #include "chrome/browser/image_decoder.h"
10 #include "chrome/grit/generated_resources.h"
11 #include "chromeos/login/login_state.h"
12 #include "components/wallpaper/wallpaper_layout.h"
13 #include "ui/base/l10n/l10n_util.h"
15 using content::BrowserThread
;
17 namespace wallpaper_api_util
{
20 // Keeps in sync (same order) with WallpaperLayout enum in header file.
21 const char* const kWallpaperLayoutArrays
[] = {
28 const int kWallpaperLayoutCount
= arraysize(kWallpaperLayoutArrays
);
32 const char kCancelWallpaperMessage
[] = "Set wallpaper was canceled.";
34 wallpaper::WallpaperLayout
GetLayoutEnum(const std::string
& layout
) {
35 for (int i
= 0; i
< kWallpaperLayoutCount
; i
++) {
36 if (layout
.compare(kWallpaperLayoutArrays
[i
]) == 0)
37 return static_cast<wallpaper::WallpaperLayout
>(i
);
39 // Default to use CENTER layout.
40 return wallpaper::WALLPAPER_LAYOUT_CENTER
;
43 void RecordCustomWallpaperLayout(const wallpaper::WallpaperLayout
& layout
) {
44 UMA_HISTOGRAM_ENUMERATION("Ash.Wallpaper.CustomLayout", layout
,
45 wallpaper::NUM_WALLPAPER_LAYOUT
);
48 } // namespace wallpaper_api_util
50 class WallpaperFunctionBase::UnsafeWallpaperDecoder
51 : public ImageDecoder::ImageRequest
{
53 explicit UnsafeWallpaperDecoder(scoped_refptr
<WallpaperFunctionBase
> function
)
54 : function_(function
) {}
56 void Start(const std::vector
<char>& image_data
) {
57 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
59 // This function can only be called after user login. It is fine to use
60 // unsafe image decoder here. Before user login, a robust jpeg decoder will
62 CHECK(chromeos::LoginState::Get()->IsUserLoggedIn());
63 std::string
image_data_str(image_data
.begin(), image_data
.end());
64 ImageDecoder::StartWithOptions(this, image_data_str
,
65 ImageDecoder::DEFAULT_CODEC
, true);
72 void OnImageDecoded(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() override
{
90 l10n_util::GetStringUTF8(IDS_WALLPAPER_MANAGER_INVALID_WALLPAPER
));
95 scoped_refptr
<WallpaperFunctionBase
> function_
;
96 base::CancellationFlag cancel_flag_
;
98 DISALLOW_COPY_AND_ASSIGN(UnsafeWallpaperDecoder
);
101 WallpaperFunctionBase::UnsafeWallpaperDecoder
*
102 WallpaperFunctionBase::unsafe_wallpaper_decoder_
;
104 WallpaperFunctionBase::WallpaperFunctionBase() {
107 WallpaperFunctionBase::~WallpaperFunctionBase() {
110 void WallpaperFunctionBase::StartDecode(const std::vector
<char>& data
) {
111 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
112 if (unsafe_wallpaper_decoder_
)
113 unsafe_wallpaper_decoder_
->Cancel();
114 unsafe_wallpaper_decoder_
= new UnsafeWallpaperDecoder(this);
115 unsafe_wallpaper_decoder_
->Start(data
);
118 void WallpaperFunctionBase::OnCancel() {
119 unsafe_wallpaper_decoder_
= NULL
;
120 SetError(wallpaper_api_util::kCancelWallpaperMessage
);
124 void WallpaperFunctionBase::OnFailure(const std::string
& error
) {
125 unsafe_wallpaper_decoder_
= NULL
;