OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/extensions/wallpaper_private_api.h" | 5 #include "chrome/browser/chromeos/extensions/wallpaper_private_api.h" |
6 | 6 |
7 #include "ash/desktop_background/desktop_background_controller.h" | 7 #include "ash/desktop_background/desktop_background_controller.h" |
8 #include "ash/shell.h" | 8 #include "ash/shell.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 | 94 |
95 void Cancel() { | 95 void Cancel() { |
96 cancel_flag_.Set(); | 96 cancel_flag_.Set(); |
97 } | 97 } |
98 | 98 |
99 virtual void OnImageDecoded(const ImageDecoder* decoder, | 99 virtual void OnImageDecoded(const ImageDecoder* decoder, |
100 const SkBitmap& decoded_image) OVERRIDE { | 100 const SkBitmap& decoded_image) OVERRIDE { |
101 gfx::ImageSkia final_image(decoded_image); | 101 gfx::ImageSkia final_image(decoded_image); |
102 final_image.MakeThreadSafe(); | 102 final_image.MakeThreadSafe(); |
103 if (cancel_flag_.IsSet()) { | 103 if (cancel_flag_.IsSet()) { |
104 function_->OnFailureOrCancel(); | 104 function_->OnFailureOrCancel(""); |
105 delete this; | 105 delete this; |
106 return; | 106 return; |
107 } | 107 } |
108 function_->OnWallpaperDecoded(final_image); | 108 function_->OnWallpaperDecoded(final_image); |
109 delete this; | 109 delete this; |
110 } | 110 } |
111 | 111 |
112 virtual void OnDecodeImageFailed(const ImageDecoder* decoder) OVERRIDE { | 112 virtual void OnDecodeImageFailed(const ImageDecoder* decoder) OVERRIDE { |
113 function_->OnFailureOrCancel(); | 113 function_->OnFailureOrCancel( |
114 // TODO(bshe): Dispatches an encoding error event. | 114 l10n_util::GetStringUTF8(IDS_WALLPAPER_MANAGER_INVALID_WALLPAPER)); |
115 delete this; | 115 delete this; |
116 } | 116 } |
117 | 117 |
118 private: | 118 private: |
119 scoped_refptr<WallpaperFunctionBase> function_; | 119 scoped_refptr<WallpaperFunctionBase> function_; |
120 scoped_refptr<ImageDecoder> image_decoder_; | 120 scoped_refptr<ImageDecoder> image_decoder_; |
121 base::CancellationFlag cancel_flag_; | 121 base::CancellationFlag cancel_flag_; |
122 | 122 |
123 DISALLOW_COPY_AND_ASSIGN(WallpaperDecoder); | 123 DISALLOW_COPY_AND_ASSIGN(WallpaperDecoder); |
124 }; | 124 }; |
125 | 125 |
126 WallpaperFunctionBase::WallpaperDecoder* | 126 WallpaperFunctionBase::WallpaperDecoder* |
127 WallpaperFunctionBase::wallpaper_decoder_; | 127 WallpaperFunctionBase::wallpaper_decoder_; |
128 | 128 |
129 WallpaperFunctionBase::WallpaperFunctionBase() { | 129 WallpaperFunctionBase::WallpaperFunctionBase() { |
130 } | 130 } |
131 | 131 |
132 WallpaperFunctionBase::~WallpaperFunctionBase() { | 132 WallpaperFunctionBase::~WallpaperFunctionBase() { |
133 } | 133 } |
134 | 134 |
| 135 void WallpaperFunctionBase::OnFailureOrCancel(const std::string& error) { |
| 136 wallpaper_decoder_ = NULL; |
| 137 if (!error.empty()) |
| 138 SetError(error); |
| 139 SendResponse(false); |
| 140 } |
| 141 |
135 WallpaperSetWallpaperFunction::WallpaperSetWallpaperFunction() { | 142 WallpaperSetWallpaperFunction::WallpaperSetWallpaperFunction() { |
136 } | 143 } |
137 | 144 |
138 WallpaperSetWallpaperFunction::~WallpaperSetWallpaperFunction() { | 145 WallpaperSetWallpaperFunction::~WallpaperSetWallpaperFunction() { |
139 } | 146 } |
140 | 147 |
141 bool WallpaperSetWallpaperFunction::RunImpl() { | 148 bool WallpaperSetWallpaperFunction::RunImpl() { |
142 BinaryValue* input = NULL; | 149 BinaryValue* input = NULL; |
143 if (args_ == NULL || !args_->GetBinary(0, &input)) { | 150 if (args_ == NULL || !args_->GetBinary(0, &input)) { |
144 return false; | 151 return false; |
(...skipping 22 matching lines...) Expand all Loading... |
167 const gfx::ImageSkia& wallpaper) { | 174 const gfx::ImageSkia& wallpaper) { |
168 wallpaper_ = wallpaper; | 175 wallpaper_ = wallpaper; |
169 // Set wallpaper_decoder_ to null since the decoding already finished. | 176 // Set wallpaper_decoder_ to null since the decoding already finished. |
170 wallpaper_decoder_ = NULL; | 177 wallpaper_decoder_ = NULL; |
171 BrowserThread::PostTask( | 178 BrowserThread::PostTask( |
172 BrowserThread::FILE, FROM_HERE, | 179 BrowserThread::FILE, FROM_HERE, |
173 base::Bind(&WallpaperSetWallpaperFunction::SaveToFile, | 180 base::Bind(&WallpaperSetWallpaperFunction::SaveToFile, |
174 this)); | 181 this)); |
175 } | 182 } |
176 | 183 |
177 void WallpaperSetWallpaperFunction::OnFailureOrCancel() { | |
178 wallpaper_decoder_ = NULL; | |
179 SendResponse(false); | |
180 } | |
181 | |
182 void WallpaperSetWallpaperFunction::SaveToFile() { | 184 void WallpaperSetWallpaperFunction::SaveToFile() { |
183 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
184 FilePath wallpaper_dir; | 186 FilePath wallpaper_dir; |
185 CHECK(PathService::Get(chrome::DIR_CHROMEOS_WALLPAPERS, &wallpaper_dir)); | 187 CHECK(PathService::Get(chrome::DIR_CHROMEOS_WALLPAPERS, &wallpaper_dir)); |
186 if (!file_util::DirectoryExists(wallpaper_dir) && | 188 if (!file_util::DirectoryExists(wallpaper_dir) && |
187 !file_util::CreateDirectory(wallpaper_dir)) { | 189 !file_util::CreateDirectory(wallpaper_dir)) { |
188 BrowserThread::PostTask( | 190 BrowserThread::PostTask( |
189 BrowserThread::UI, FROM_HERE, | 191 BrowserThread::UI, FROM_HERE, |
190 base::Bind(&WallpaperSetWallpaperFunction::OnFailureOrCancel, | 192 base::Bind(&WallpaperSetWallpaperFunction::OnFailureOrCancel, |
191 this)); | 193 this, "")); |
| 194 LOG(ERROR) << "Failed to create wallpaper directory."; |
192 return; | 195 return; |
193 } | 196 } |
194 std::string file_name = GURL(url_).ExtractFileName(); | 197 std::string file_name = GURL(url_).ExtractFileName(); |
195 FilePath file_path = wallpaper_dir.Append(file_name); | 198 FilePath file_path = wallpaper_dir.Append(file_name); |
196 if (file_util::PathExists(file_path) || | 199 if (file_util::PathExists(file_path) || |
197 file_util::WriteFile(file_path, image_data_.c_str(), | 200 file_util::WriteFile(file_path, image_data_.c_str(), |
198 image_data_.size()) != -1 ) { | 201 image_data_.size()) != -1 ) { |
199 BrowserThread::PostTask( | 202 BrowserThread::PostTask( |
200 BrowserThread::UI, FROM_HERE, | 203 BrowserThread::UI, FROM_HERE, |
201 base::Bind(&WallpaperSetWallpaperFunction::SetDecodedWallpaper, | 204 base::Bind(&WallpaperSetWallpaperFunction::SetDecodedWallpaper, |
202 this)); | 205 this)); |
203 chromeos::UserImage wallpaper(wallpaper_); | 206 chromeos::UserImage wallpaper(wallpaper_); |
204 | 207 |
205 // Generates and saves small resolution wallpaper. Uses CENTER_CROPPED to | 208 // Generates and saves small resolution wallpaper. Uses CENTER_CROPPED to |
206 // maintain the aspect ratio after resize. | 209 // maintain the aspect ratio after resize. |
207 chromeos::WallpaperManager::Get()->ResizeAndSaveWallpaper( | 210 chromeos::WallpaperManager::Get()->ResizeAndSaveWallpaper( |
208 wallpaper, | 211 wallpaper, |
209 file_path.InsertBeforeExtension(chromeos::kSmallWallpaperSuffix), | 212 file_path.InsertBeforeExtension(chromeos::kSmallWallpaperSuffix), |
210 ash::CENTER_CROPPED, | 213 ash::CENTER_CROPPED, |
211 ash::kSmallWallpaperMaxWidth, | 214 ash::kSmallWallpaperMaxWidth, |
212 ash::kSmallWallpaperMaxHeight); | 215 ash::kSmallWallpaperMaxHeight); |
213 } else { | 216 } else { |
214 BrowserThread::PostTask( | 217 BrowserThread::PostTask( |
215 BrowserThread::UI, FROM_HERE, | 218 BrowserThread::UI, FROM_HERE, |
216 base::Bind(&WallpaperSetWallpaperFunction::OnFailureOrCancel, | 219 base::Bind(&WallpaperSetWallpaperFunction::OnFailureOrCancel, |
217 this)); | 220 this, "")); |
| 221 LOG(ERROR) << "Failed to save downloaded wallpaper."; |
218 } | 222 } |
219 } | 223 } |
220 | 224 |
221 void WallpaperSetWallpaperFunction::SetDecodedWallpaper() { | 225 void WallpaperSetWallpaperFunction::SetDecodedWallpaper() { |
222 chromeos::WallpaperManager* wallpaper_manager = | 226 chromeos::WallpaperManager* wallpaper_manager = |
223 chromeos::WallpaperManager::Get(); | 227 chromeos::WallpaperManager::Get(); |
224 wallpaper_manager->SetWallpaperFromImageSkia(wallpaper_, layout_); | 228 wallpaper_manager->SetWallpaperFromImageSkia(wallpaper_, layout_); |
225 bool is_persistent = | 229 bool is_persistent = |
226 !chromeos::UserManager::Get()->IsCurrentUserEphemeral(); | 230 !chromeos::UserManager::Get()->IsCurrentUserEphemeral(); |
227 chromeos::WallpaperInfo info = { | 231 chromeos::WallpaperInfo info = { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 image_data_.end()); | 273 image_data_.end()); |
270 chromeos::UserImage image(wallpaper, raw_image); | 274 chromeos::UserImage image(wallpaper, raw_image); |
271 // In the new wallpaper picker UI, we do not depend on WallpaperDelegate | 275 // In the new wallpaper picker UI, we do not depend on WallpaperDelegate |
272 // to refresh thumbnail. Uses a null delegate here. | 276 // to refresh thumbnail. Uses a null delegate here. |
273 chromeos::WallpaperManager::Get()->SetCustomWallpaper( | 277 chromeos::WallpaperManager::Get()->SetCustomWallpaper( |
274 email_, layout_, chromeos::User::CUSTOMIZED, | 278 email_, layout_, chromeos::User::CUSTOMIZED, |
275 base::WeakPtr<chromeos::WallpaperDelegate>(), image); | 279 base::WeakPtr<chromeos::WallpaperDelegate>(), image); |
276 wallpaper_decoder_ = NULL; | 280 wallpaper_decoder_ = NULL; |
277 SendResponse(true); | 281 SendResponse(true); |
278 } | 282 } |
279 | |
280 void WallpaperSetCustomWallpaperFunction::OnFailureOrCancel() { | |
281 wallpaper_decoder_ = NULL; | |
282 SendResponse(false); | |
283 } | |
OLD | NEW |