| 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" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/path_service.h" | 12 #include "base/path_service.h" |
| 13 #include "base/synchronization/cancellation_flag.h" | 13 #include "base/synchronization/cancellation_flag.h" |
| 14 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/chromeos/login/user_image.h" |
| 15 #include "chrome/browser/chromeos/login/user_manager.h" | 16 #include "chrome/browser/chromeos/login/user_manager.h" |
| 16 #include "chrome/browser/chromeos/login/wallpaper_manager.h" | 17 #include "chrome/browser/chromeos/login/wallpaper_manager.h" |
| 17 #include "chrome/browser/extensions/event_router.h" | 18 #include "chrome/browser/extensions/event_router.h" |
| 18 #include "chrome/browser/image_decoder.h" | 19 #include "chrome/browser/image_decoder.h" |
| 19 #include "chrome/common/chrome_paths.h" | 20 #include "chrome/common/chrome_paths.h" |
| 20 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
| 21 #include "net/url_request/url_fetcher.h" | 22 #include "net/url_request/url_fetcher.h" |
| 22 #include "net/url_request/url_fetcher_delegate.h" | 23 #include "net/url_request/url_fetcher_delegate.h" |
| 23 #include "net/url_request/url_request_status.h" | 24 #include "net/url_request/url_request_status.h" |
| 24 #include "googleurl/src/gurl.h" | 25 #include "googleurl/src/gurl.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 39 SET_STRING("customCategoryLabel", | 40 SET_STRING("customCategoryLabel", |
| 40 IDS_WALLPAPER_MANAGER_CUSTOM_CATEGORY_LABEL); | 41 IDS_WALLPAPER_MANAGER_CUSTOM_CATEGORY_LABEL); |
| 41 SET_STRING("selectCustomLabel", | 42 SET_STRING("selectCustomLabel", |
| 42 IDS_WALLPAPER_MANAGER_SELECT_CUSTOM_LABEL); | 43 IDS_WALLPAPER_MANAGER_SELECT_CUSTOM_LABEL); |
| 43 SET_STRING("positionLabel", IDS_WALLPAPER_MANAGER_POSITION_LABEL); | 44 SET_STRING("positionLabel", IDS_WALLPAPER_MANAGER_POSITION_LABEL); |
| 44 SET_STRING("colorLabel", IDS_WALLPAPER_MANAGER_COLOR_LABEL); | 45 SET_STRING("colorLabel", IDS_WALLPAPER_MANAGER_COLOR_LABEL); |
| 45 SET_STRING("previewLabel", IDS_WALLPAPER_MANAGER_PREVIEW_LABEL); | 46 SET_STRING("previewLabel", IDS_WALLPAPER_MANAGER_PREVIEW_LABEL); |
| 46 SET_STRING("downloadingLabel", IDS_WALLPAPER_MANAGER_DOWNLOADING_LABEL); | 47 SET_STRING("downloadingLabel", IDS_WALLPAPER_MANAGER_DOWNLOADING_LABEL); |
| 47 SET_STRING("setWallpaperDaily", IDS_OPTIONS_SET_WALLPAPER_DAILY); | 48 SET_STRING("setWallpaperDaily", IDS_OPTIONS_SET_WALLPAPER_DAILY); |
| 48 SET_STRING("searchTextLabel", IDS_WALLPAPER_MANAGER_SEARCH_TEXT_LABEL); | 49 SET_STRING("searchTextLabel", IDS_WALLPAPER_MANAGER_SEARCH_TEXT_LABEL); |
| 50 SET_STRING("centerCroppedLayout", |
| 51 IDS_OPTIONS_WALLPAPER_CENTER_CROPPED_LAYOUT); |
| 52 SET_STRING("centerLayout", IDS_OPTIONS_WALLPAPER_CENTER_LAYOUT); |
| 53 SET_STRING("stretchLayout", IDS_OPTIONS_WALLPAPER_STRETCH_LAYOUT); |
| 49 #undef SET_STRING | 54 #undef SET_STRING |
| 50 | 55 |
| 51 ChromeURLDataManager::DataSource::SetFontAndTextDirection(dict); | 56 ChromeURLDataManager::DataSource::SetFontAndTextDirection(dict); |
| 52 | 57 |
| 53 return true; | 58 return true; |
| 54 } | 59 } |
| 55 | 60 |
| 56 class WallpaperSetWallpaperFunction::WallpaperDecoder | 61 class WallpaperFunctionBase::WallpaperDecoder : public ImageDecoder::Delegate { |
| 57 : public ImageDecoder::Delegate { | |
| 58 public: | 62 public: |
| 59 explicit WallpaperDecoder( | 63 explicit WallpaperDecoder(scoped_refptr<WallpaperFunctionBase> function) |
| 60 scoped_refptr<WallpaperSetWallpaperFunction> function) | |
| 61 : function_(function) { | 64 : function_(function) { |
| 62 } | 65 } |
| 63 | 66 |
| 64 void Start(const std::string& image_data) { | 67 void Start(const std::string& image_data) { |
| 65 image_decoder_ = new ImageDecoder(this, image_data, | 68 image_decoder_ = new ImageDecoder(this, image_data, |
| 66 ImageDecoder::ROBUST_JPEG_CODEC); | 69 ImageDecoder::ROBUST_JPEG_CODEC); |
| 67 image_decoder_->Start(); | 70 image_decoder_->Start(); |
| 68 } | 71 } |
| 69 | 72 |
| 70 void Cancel() { | 73 void Cancel() { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 81 } | 84 } |
| 82 function_->OnWallpaperDecoded(final_image); | 85 function_->OnWallpaperDecoded(final_image); |
| 83 delete this; | 86 delete this; |
| 84 } | 87 } |
| 85 | 88 |
| 86 virtual void OnDecodeImageFailed(const ImageDecoder* decoder) OVERRIDE { | 89 virtual void OnDecodeImageFailed(const ImageDecoder* decoder) OVERRIDE { |
| 87 if (cancel_flag_.IsSet()) { | 90 if (cancel_flag_.IsSet()) { |
| 88 delete this; | 91 delete this; |
| 89 return; | 92 return; |
| 90 } | 93 } |
| 91 function_->OnFail(); | 94 function_->OnFailure(); |
| 92 // TODO(bshe): Dispatches an encoding error event. | 95 // TODO(bshe): Dispatches an encoding error event. |
| 93 delete this; | 96 delete this; |
| 94 } | 97 } |
| 95 | 98 |
| 96 private: | 99 private: |
| 97 scoped_refptr<WallpaperSetWallpaperFunction> function_; | 100 scoped_refptr<WallpaperFunctionBase> function_; |
| 98 scoped_refptr<ImageDecoder> image_decoder_; | 101 scoped_refptr<ImageDecoder> image_decoder_; |
| 99 base::CancellationFlag cancel_flag_; | 102 base::CancellationFlag cancel_flag_; |
| 100 | 103 |
| 101 DISALLOW_COPY_AND_ASSIGN(WallpaperDecoder); | 104 DISALLOW_COPY_AND_ASSIGN(WallpaperDecoder); |
| 102 }; | 105 }; |
| 103 | 106 |
| 104 WallpaperSetWallpaperFunction::WallpaperDecoder* | 107 WallpaperFunctionBase::WallpaperDecoder* |
| 105 WallpaperSetWallpaperFunction::wallpaper_decoder_; | 108 WallpaperFunctionBase::wallpaper_decoder_; |
| 109 |
| 110 WallpaperFunctionBase::WallpaperFunctionBase() { |
| 111 } |
| 112 |
| 113 WallpaperFunctionBase::~WallpaperFunctionBase() { |
| 114 } |
| 106 | 115 |
| 107 WallpaperSetWallpaperFunction::WallpaperSetWallpaperFunction() { | 116 WallpaperSetWallpaperFunction::WallpaperSetWallpaperFunction() { |
| 108 } | 117 } |
| 109 | 118 |
| 110 WallpaperSetWallpaperFunction::~WallpaperSetWallpaperFunction() { | 119 WallpaperSetWallpaperFunction::~WallpaperSetWallpaperFunction() { |
| 111 } | 120 } |
| 112 | 121 |
| 113 bool WallpaperSetWallpaperFunction::RunImpl() { | 122 bool WallpaperSetWallpaperFunction::RunImpl() { |
| 114 BinaryValue* input = NULL; | 123 BinaryValue* input = NULL; |
| 115 if (args_ == NULL || !args_->GetBinary(0, &input)) { | 124 if (args_ == NULL || !args_->GetBinary(0, &input)) { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 140 | 149 |
| 141 void WallpaperSetWallpaperFunction::OnWallpaperDecoded( | 150 void WallpaperSetWallpaperFunction::OnWallpaperDecoded( |
| 142 const gfx::ImageSkia& wallpaper) { | 151 const gfx::ImageSkia& wallpaper) { |
| 143 wallpaper_ = wallpaper; | 152 wallpaper_ = wallpaper; |
| 144 BrowserThread::PostTask( | 153 BrowserThread::PostTask( |
| 145 BrowserThread::FILE, FROM_HERE, | 154 BrowserThread::FILE, FROM_HERE, |
| 146 base::Bind(&WallpaperSetWallpaperFunction::SaveToFile, | 155 base::Bind(&WallpaperSetWallpaperFunction::SaveToFile, |
| 147 this)); | 156 this)); |
| 148 } | 157 } |
| 149 | 158 |
| 150 void WallpaperSetWallpaperFunction::OnFail() { | 159 void WallpaperSetWallpaperFunction::OnFailure() { |
| 151 wallpaper_decoder_ = NULL; | 160 wallpaper_decoder_ = NULL; |
| 152 SendResponse(false); | 161 SendResponse(false); |
| 153 } | 162 } |
| 154 | 163 |
| 155 void WallpaperSetWallpaperFunction::SaveToFile() { | 164 void WallpaperSetWallpaperFunction::SaveToFile() { |
| 156 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 165 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 157 FilePath wallpaper_dir; | 166 FilePath wallpaper_dir; |
| 158 CHECK(PathService::Get(chrome::DIR_CHROMEOS_WALLPAPERS, &wallpaper_dir)); | 167 CHECK(PathService::Get(chrome::DIR_CHROMEOS_WALLPAPERS, &wallpaper_dir)); |
| 159 if (!file_util::DirectoryExists(wallpaper_dir) && | 168 if (!file_util::DirectoryExists(wallpaper_dir) && |
| 160 !file_util::CreateDirectory(wallpaper_dir)) { | 169 !file_util::CreateDirectory(wallpaper_dir)) { |
| 161 BrowserThread::PostTask( | 170 BrowserThread::PostTask( |
| 162 BrowserThread::UI, FROM_HERE, | 171 BrowserThread::UI, FROM_HERE, |
| 163 base::Bind(&WallpaperSetWallpaperFunction::OnFail, | 172 base::Bind(&WallpaperSetWallpaperFunction::OnFailure, |
| 164 this)); | 173 this)); |
| 165 return; | 174 return; |
| 166 } | 175 } |
| 167 FilePath file_path = wallpaper_dir.Append(file_name_); | 176 FilePath file_path = wallpaper_dir.Append(file_name_); |
| 168 if (file_util::PathExists(file_path) || | 177 if (file_util::PathExists(file_path) || |
| 169 file_util::WriteFile(file_path, image_data_.c_str(), | 178 file_util::WriteFile(file_path, image_data_.c_str(), |
| 170 image_data_.size()) != -1 ) { | 179 image_data_.size()) != -1 ) { |
| 171 BrowserThread::PostTask( | 180 BrowserThread::PostTask( |
| 172 BrowserThread::UI, FROM_HERE, | 181 BrowserThread::UI, FROM_HERE, |
| 173 base::Bind(&WallpaperSetWallpaperFunction::SetDecodedWallpaper, | 182 base::Bind(&WallpaperSetWallpaperFunction::SetDecodedWallpaper, |
| 174 this)); | 183 this)); |
| 175 } else { | 184 } else { |
| 176 BrowserThread::PostTask( | 185 BrowserThread::PostTask( |
| 177 BrowserThread::UI, FROM_HERE, | 186 BrowserThread::UI, FROM_HERE, |
| 178 base::Bind(&WallpaperSetWallpaperFunction::OnFail, | 187 base::Bind(&WallpaperSetWallpaperFunction::OnFailure, |
| 179 this)); | 188 this)); |
| 180 } | 189 } |
| 181 } | 190 } |
| 182 | 191 |
| 183 void WallpaperSetWallpaperFunction::SetDecodedWallpaper() { | 192 void WallpaperSetWallpaperFunction::SetDecodedWallpaper() { |
| 184 chromeos::WallpaperManager* wallpaper_manager = | 193 chromeos::WallpaperManager* wallpaper_manager = |
| 185 chromeos::WallpaperManager::Get(); | 194 chromeos::WallpaperManager::Get(); |
| 186 wallpaper_manager->SetWallpaperFromImageSkia(wallpaper_, layout_); | 195 wallpaper_manager->SetWallpaperFromImageSkia(wallpaper_, layout_); |
| 187 bool is_persistent = | 196 bool is_persistent = |
| 188 !chromeos::UserManager::Get()->IsCurrentUserEphemeral(); | 197 !chromeos::UserManager::Get()->IsCurrentUserEphemeral(); |
| 189 chromeos::WallpaperInfo info = { | 198 chromeos::WallpaperInfo info = { |
| 190 file_name_, | 199 file_name_, |
| 191 layout_, | 200 layout_, |
| 192 chromeos::User::ONLINE, | 201 chromeos::User::ONLINE, |
| 193 base::Time::Now().LocalMidnight() | 202 base::Time::Now().LocalMidnight() |
| 194 }; | 203 }; |
| 195 wallpaper_manager->SetUserWallpaperInfo(email_, info, is_persistent); | 204 wallpaper_manager->SetUserWallpaperInfo(email_, info, is_persistent); |
| 196 wallpaper_decoder_ = NULL; | 205 wallpaper_decoder_ = NULL; |
| 197 SendResponse(true); | 206 SendResponse(true); |
| 198 } | 207 } |
| 208 |
| 209 WallpaperSetCustomWallpaperFunction::WallpaperSetCustomWallpaperFunction() { |
| 210 } |
| 211 |
| 212 WallpaperSetCustomWallpaperFunction::~WallpaperSetCustomWallpaperFunction() { |
| 213 } |
| 214 |
| 215 bool WallpaperSetCustomWallpaperFunction::RunImpl() { |
| 216 BinaryValue* input = NULL; |
| 217 if (args_ == NULL || !args_->GetBinary(0, &input)) { |
| 218 return false; |
| 219 } |
| 220 std::string layout_string; |
| 221 if (!args_->GetString(1, &layout_string) || layout_string.empty()) { |
| 222 return false; |
| 223 } |
| 224 layout_ = ash::GetLayoutEnum(layout_string); |
| 225 |
| 226 // Gets email address while at UI thread. |
| 227 email_ = chromeos::UserManager::Get()->GetLoggedInUser().email(); |
| 228 |
| 229 image_data_.assign(input->GetBuffer(), input->GetSize()); |
| 230 if (wallpaper_decoder_) |
| 231 wallpaper_decoder_->Cancel(); |
| 232 wallpaper_decoder_ = new WallpaperDecoder(this); |
| 233 wallpaper_decoder_->Start(image_data_); |
| 234 |
| 235 return true; |
| 236 } |
| 237 |
| 238 void WallpaperSetCustomWallpaperFunction::OnWallpaperDecoded( |
| 239 const gfx::ImageSkia& wallpaper) { |
| 240 chromeos::UserImage::RawImage raw_image(image_data_.begin(), |
| 241 image_data_.end()); |
| 242 chromeos::UserImage image(wallpaper, raw_image); |
| 243 // In the new wallpaper picker UI, we do not depend on WallpaperDelegate |
| 244 // to refresh thumbnail. Uses a null delegate here. |
| 245 chromeos::WallpaperManager::Get()->SetCustomWallpaper( |
| 246 email_, layout_, chromeos::User::CUSTOMIZED, |
| 247 base::WeakPtr<chromeos::WallpaperDelegate>(), image); |
| 248 wallpaper_decoder_ = NULL; |
| 249 SendResponse(true); |
| 250 } |
| 251 |
| 252 void WallpaperSetCustomWallpaperFunction::OnFailure() { |
| 253 wallpaper_decoder_ = NULL; |
| 254 SendResponse(false); |
| 255 } |
| OLD | NEW |