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_api.h"
7 #include "base/file_util.h"
8 #include "base/path_service.h"
9 #include "base/threading/worker_pool.h"
10 #include "chrome/browser/chromeos/login/user.h"
11 #include "chrome/browser/chromeos/login/user_manager.h"
12 #include "chrome/browser/chromeos/login/wallpaper_manager.h"
13 #include "chrome/common/chrome_paths.h"
15 using base::BinaryValue
;
16 using content::BrowserThread
;
18 WallpaperSetWallpaperFunction::WallpaperSetWallpaperFunction() {
21 WallpaperSetWallpaperFunction::~WallpaperSetWallpaperFunction() {
24 bool WallpaperSetWallpaperFunction::RunImpl() {
25 DictionaryValue
* details
= NULL
;
26 EXTENSION_FUNCTION_VALIDATE(args_
->GetDictionary(0, &details
));
28 BinaryValue
* input
= NULL
;
29 details
->GetBinary("wallpaperData", &input
);
31 std::string layout_string
;
32 details
->GetString("layout", &layout_string
);
33 EXTENSION_FUNCTION_VALIDATE(!layout_string
.empty());
34 layout_
= wallpaper_api_util::GetLayoutEnum(layout_string
);
36 details
->GetBoolean("thumbnail", &generate_thumbnail_
);
37 details
->GetString("name", &file_name_
);
39 EXTENSION_FUNCTION_VALIDATE(!file_name_
.empty());
41 // Gets email address while at UI thread.
42 email_
= chromeos::UserManager::Get()->GetLoggedInUser()->email();
44 image_data_
.assign(input
->GetBuffer(), input
->GetSize());
45 StartDecode(image_data_
);
50 void WallpaperSetWallpaperFunction::OnWallpaperDecoded(
51 const gfx::ImageSkia
& wallpaper
) {
52 chromeos::WallpaperManager
* wallpaper_manager
=
53 chromeos::WallpaperManager::Get();
54 chromeos::UserImage::RawImage
raw_image(image_data_
.begin(),
56 chromeos::UserImage
image(wallpaper
, raw_image
);
57 base::FilePath thumbnail_path
= wallpaper_manager
->GetCustomWallpaperPath(
58 chromeos::kThumbnailWallpaperSubDir
, email_
, file_name_
);
60 sequence_token_
= BrowserThread::GetBlockingPool()->
61 GetNamedSequenceToken(chromeos::kWallpaperSequenceTokenName
);
62 scoped_refptr
<base::SequencedTaskRunner
> task_runner
=
63 BrowserThread::GetBlockingPool()->
64 GetSequencedTaskRunnerWithShutdownBehavior(sequence_token_
,
65 base::SequencedWorkerPool::BLOCK_SHUTDOWN
);
67 // In the new wallpaper picker UI, we do not depend on WallpaperDelegate
68 // to refresh thumbnail. Uses a null delegate here.
69 wallpaper_manager
->SetCustomWallpaper(email_
, file_name_
, layout_
,
70 chromeos::User::CUSTOMIZED
,
72 unsafe_wallpaper_decoder_
= NULL
;
74 if (generate_thumbnail_
) {
75 wallpaper
.EnsureRepsForSupportedScaleFactors();
76 scoped_ptr
<gfx::ImageSkia
> deep_copy(wallpaper
.DeepCopy());
77 // Generates thumbnail before call api function callback. We can then
78 // request thumbnail in the javascript callback.
79 task_runner
->PostTask(FROM_HERE
,
81 &WallpaperSetWallpaperFunction::GenerateThumbnail
,
82 this, thumbnail_path
, base::Passed(&deep_copy
)));
88 void WallpaperSetWallpaperFunction::GenerateThumbnail(
89 const base::FilePath
& thumbnail_path
, scoped_ptr
<gfx::ImageSkia
> image
) {
90 DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread(
92 chromeos::UserImage
wallpaper(*image
.get());
93 if (!base::PathExists(thumbnail_path
.DirName()))
94 file_util::CreateDirectory(thumbnail_path
.DirName());
96 scoped_refptr
<base::RefCountedBytes
> data
;
97 chromeos::WallpaperManager::Get()->ResizeWallpaper(
99 ash::WALLPAPER_LAYOUT_STRETCH
,
100 ash::kWallpaperThumbnailWidth
,
101 ash::kWallpaperThumbnailHeight
,
103 BrowserThread::PostTask(
104 BrowserThread::UI
, FROM_HERE
,
106 &WallpaperSetWallpaperFunction::ThumbnailGenerated
,
110 void WallpaperSetWallpaperFunction::ThumbnailGenerated(
111 base::RefCountedBytes
* data
) {
112 BinaryValue
* result
= BinaryValue::CreateWithCopiedBuffer(
113 reinterpret_cast<const char*>(data
->front()), data
->size());