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"
10 #include "ash/desktop_background/desktop_background_controller.h"
11 #include "base/files/file_util.h"
12 #include "base/lazy_instance.h"
13 #include "base/prefs/pref_service.h"
14 #include "base/strings/stringprintf.h"
15 #include "base/threading/worker_pool.h"
16 #include "chrome/browser/browser_process.h"
17 #include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/common/chrome_paths.h"
20 #include "chrome/common/extensions/extension_constants.h"
21 #include "chrome/common/pref_names.h"
22 #include "components/user_manager/user.h"
23 #include "components/user_manager/user_manager.h"
24 #include "components/wallpaper/wallpaper_layout.h"
25 #include "net/base/load_flags.h"
26 #include "net/http/http_status_code.h"
27 #include "net/url_request/url_fetcher.h"
28 #include "net/url_request/url_fetcher_delegate.h"
31 using base::BinaryValue
;
32 using content::BrowserThread
;
34 typedef base::Callback
<void(bool success
, const std::string
&)> FetchCallback
;
36 namespace set_wallpaper
= extensions::api::wallpaper::SetWallpaper
;
40 class WallpaperFetcher
: public net::URLFetcherDelegate
{
44 ~WallpaperFetcher() override
{}
46 void FetchWallpaper(const GURL
& url
, FetchCallback callback
) {
47 CancelPreviousFetch();
49 url_fetcher_
.reset(net::URLFetcher::Create(url
,
52 url_fetcher_
->SetRequestContext(
53 g_browser_process
->system_request_context());
54 url_fetcher_
->SetLoadFlags(net::LOAD_DISABLE_CACHE
);
55 url_fetcher_
->Start();
59 // URLFetcherDelegate overrides:
60 void OnURLFetchComplete(const net::URLFetcher
* source
) override
{
61 DCHECK(url_fetcher_
.get() == source
);
63 bool success
= source
->GetStatus().is_success() &&
64 source
->GetResponseCode() == net::HTTP_OK
;
67 source
->GetResponseAsString(&response
);
69 response
= base::StringPrintf(
70 "Downloading wallpaper %s failed. The response code is %d.",
71 source
->GetOriginalURL().ExtractFileName().c_str(),
72 source
->GetResponseCode());
75 callback_
.Run(success
, response
);
78 void CancelPreviousFetch() {
79 if (url_fetcher_
.get()) {
80 callback_
.Run(false, wallpaper_api_util::kCancelWallpaperMessage
);
85 scoped_ptr
<net::URLFetcher
> url_fetcher_
;
86 FetchCallback callback_
;
89 base::LazyInstance
<WallpaperFetcher
> g_wallpaper_fetcher
=
90 LAZY_INSTANCE_INITIALIZER
;
94 WallpaperSetWallpaperFunction::WallpaperSetWallpaperFunction() {
97 WallpaperSetWallpaperFunction::~WallpaperSetWallpaperFunction() {
100 bool WallpaperSetWallpaperFunction::RunAsync() {
101 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
102 params_
= set_wallpaper::Params::Create(*args_
);
103 EXTENSION_FUNCTION_VALIDATE(params_
);
105 // Gets email address and username hash while at UI thread.
106 user_id_
= user_manager::UserManager::Get()->GetLoggedInUser()->email();
108 user_manager::UserManager::Get()->GetLoggedInUser()->username_hash();
110 if (params_
->details
.data
) {
111 StartDecode(*params_
->details
.data
);
113 GURL
wallpaper_url(*params_
->details
.url
);
114 if (wallpaper_url
.is_valid()) {
115 g_wallpaper_fetcher
.Get().FetchWallpaper(
117 base::Bind(&WallpaperSetWallpaperFunction::OnWallpaperFetched
, this));
119 SetError("URL is invalid.");
126 void WallpaperSetWallpaperFunction::OnWallpaperDecoded(
127 const gfx::ImageSkia
& image
) {
128 chromeos::WallpaperManager
* wallpaper_manager
=
129 chromeos::WallpaperManager::Get();
130 base::FilePath thumbnail_path
= wallpaper_manager
->GetCustomWallpaperPath(
131 wallpaper::kThumbnailWallpaperSubDir
, user_id_hash_
,
132 params_
->details
.filename
);
134 sequence_token_
= BrowserThread::GetBlockingPool()->GetNamedSequenceToken(
135 wallpaper::kWallpaperSequenceTokenName
);
136 scoped_refptr
<base::SequencedTaskRunner
> task_runner
=
137 BrowserThread::GetBlockingPool()->
138 GetSequencedTaskRunnerWithShutdownBehavior(sequence_token_
,
139 base::SequencedWorkerPool::BLOCK_SHUTDOWN
);
140 wallpaper::WallpaperLayout layout
= wallpaper_api_util::GetLayoutEnum(
141 set_wallpaper::Params::Details::ToString(params_
->details
.layout
));
142 bool update_wallpaper
=
143 user_id_
== user_manager::UserManager::Get()->GetActiveUser()->email();
144 wallpaper_manager
->SetCustomWallpaper(user_id_
,
146 params_
->details
.filename
,
148 user_manager::User::CUSTOMIZED
,
151 unsafe_wallpaper_decoder_
= NULL
;
153 if (params_
->details
.thumbnail
) {
154 image
.EnsureRepsForSupportedScales();
155 scoped_ptr
<gfx::ImageSkia
> deep_copy(image
.DeepCopy());
156 // Generates thumbnail before call api function callback. We can then
157 // request thumbnail in the javascript callback.
158 task_runner
->PostTask(
160 base::Bind(&WallpaperSetWallpaperFunction::GenerateThumbnail
,
163 base::Passed(deep_copy
.Pass())));
165 // Save current extenion name. It will be displayed in the component
166 // wallpaper picker app. If current extension is the component wallpaper
167 // picker, set an empty string.
168 Profile
* profile
= Profile::FromBrowserContext(browser_context());
169 if (extension()->id() == extension_misc::kWallpaperManagerId
) {
170 profile
->GetPrefs()->SetString(prefs::kCurrentWallpaperAppName
,
173 profile
->GetPrefs()->SetString(prefs::kCurrentWallpaperAppName
,
174 extension()->name());
180 void WallpaperSetWallpaperFunction::GenerateThumbnail(
181 const base::FilePath
& thumbnail_path
, scoped_ptr
<gfx::ImageSkia
> image
) {
182 DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread(
184 if (!base::PathExists(thumbnail_path
.DirName()))
185 base::CreateDirectory(thumbnail_path
.DirName());
187 scoped_refptr
<base::RefCountedBytes
> data
;
188 chromeos::WallpaperManager::Get()->ResizeImage(
189 *image
, wallpaper::WALLPAPER_LAYOUT_STRETCH
,
190 wallpaper::kWallpaperThumbnailWidth
, wallpaper::kWallpaperThumbnailHeight
,
192 BrowserThread::PostTask(
193 BrowserThread::UI
, FROM_HERE
,
195 &WallpaperSetWallpaperFunction::ThumbnailGenerated
,
199 void WallpaperSetWallpaperFunction::ThumbnailGenerated(
200 base::RefCountedBytes
* data
) {
201 BinaryValue
* result
= BinaryValue::CreateWithCopiedBuffer(
202 reinterpret_cast<const char*>(data
->front()), data
->size());
207 void WallpaperSetWallpaperFunction::OnWallpaperFetched(
209 const std::string
& response
) {
211 params_
->details
.data
.reset(
212 new std::vector
<char>(response
.begin(), response
.end()));
213 StartDecode(*params_
->details
.data
);