Chromium Code Reviews| 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 "ash/desktop_background/desktop_background_controller.h" | 5 #include "ash/desktop_background/desktop_background_controller.h" |
| 6 | 6 |
| 7 #include "ash/ash_switches.h" | 7 #include "ash/ash_switches.h" |
| 8 #include "ash/desktop_background/desktop_background_controller_observer.h" | 8 #include "ash/desktop_background/desktop_background_controller_observer.h" |
| 9 #include "ash/desktop_background/desktop_background_view.h" | 9 #include "ash/desktop_background/desktop_background_view.h" |
| 10 #include "ash/desktop_background/desktop_background_widget_controller.h" | 10 #include "ash/desktop_background/desktop_background_widget_controller.h" |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 63 WallpaperLoader(const base::FilePath& file_path, | 63 WallpaperLoader(const base::FilePath& file_path, |
| 64 WallpaperLayout file_layout, | 64 WallpaperLayout file_layout, |
| 65 int resource_id, | 65 int resource_id, |
| 66 WallpaperLayout resource_layout) | 66 WallpaperLayout resource_layout) |
| 67 : file_path_(file_path), | 67 : file_path_(file_path), |
| 68 file_layout_(file_layout), | 68 file_layout_(file_layout), |
| 69 resource_id_(resource_id), | 69 resource_id_(resource_id), |
| 70 resource_layout_(resource_layout) { | 70 resource_layout_(resource_layout) { |
| 71 } | 71 } |
| 72 | 72 |
| 73 static void LoadOnWorkerPoolThread(scoped_refptr<WallpaperLoader> loader) { | 73 void LoadOnWorkerPoolThread() { |
|
Daniel Erat
2013/12/26 20:52:00
i don't know why this was a static method; making
| |
| 74 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); | 74 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 75 loader->LoadWallpaper(); | 75 if (cancel_flag_.IsSet()) |
| 76 return; | |
| 77 | |
| 78 if (!file_path_.empty()) | |
| 79 file_bitmap_ = LoadSkBitmapFromJPEGFile(file_path_); | |
| 80 | |
| 81 if (cancel_flag_.IsSet()) | |
| 82 return; | |
| 83 | |
| 84 if (file_bitmap_) { | |
| 85 gfx::ImageSkia image = gfx::ImageSkia::CreateFrom1xBitmap(*file_bitmap_); | |
| 86 wallpaper_resizer_.reset(new WallpaperResizer( | |
| 87 image, GetMaxDisplaySizeInNative(), file_layout_)); | |
| 88 } else { | |
| 89 wallpaper_resizer_.reset(new WallpaperResizer( | |
| 90 resource_id_, GetMaxDisplaySizeInNative(), resource_layout_)); | |
| 91 } | |
| 76 } | 92 } |
| 77 | 93 |
| 78 const base::FilePath& file_path() const { return file_path_; } | 94 const base::FilePath& file_path() const { return file_path_; } |
| 79 int resource_id() const { return resource_id_; } | 95 int resource_id() const { return resource_id_; } |
| 80 | 96 |
| 81 void Cancel() { | 97 void Cancel() { |
| 82 cancel_flag_.Set(); | 98 cancel_flag_.Set(); |
| 83 } | 99 } |
| 84 | 100 |
| 101 bool IsCanceled() { | |
| 102 return cancel_flag_.IsSet(); | |
| 103 } | |
| 104 | |
| 85 WallpaperResizer* ReleaseWallpaperResizer() { | 105 WallpaperResizer* ReleaseWallpaperResizer() { |
| 86 return wallpaper_resizer_.release(); | 106 return wallpaper_resizer_.release(); |
| 87 } | 107 } |
| 88 | 108 |
| 89 private: | 109 private: |
| 90 friend class base::RefCountedThreadSafe< | 110 friend class base::RefCountedThreadSafe< |
| 91 DesktopBackgroundController::WallpaperLoader>; | 111 DesktopBackgroundController::WallpaperLoader>; |
| 92 | 112 |
| 93 // Loads a JPEG image from |path|, a trusted file -- note that the image | 113 // Loads a JPEG image from |path|, a trusted file -- note that the image |
| 94 // is not loaded in a sandboxed process. Returns an empty pointer on | 114 // is not loaded in a sandboxed process. Returns an empty pointer on |
| 95 // error. | 115 // error. |
| 96 static scoped_ptr<SkBitmap> LoadSkBitmapFromJPEGFile( | 116 static scoped_ptr<SkBitmap> LoadSkBitmapFromJPEGFile( |
| 97 const base::FilePath& path) { | 117 const base::FilePath& path) { |
| 98 std::string data; | 118 std::string data; |
| 99 if (!base::ReadFileToString(path, &data)) { | 119 if (!base::ReadFileToString(path, &data)) { |
| 100 LOG(ERROR) << "Unable to read data from " << path.value(); | 120 LOG(ERROR) << "Unable to read data from " << path.value(); |
| 101 return scoped_ptr<SkBitmap>(); | 121 return scoped_ptr<SkBitmap>(); |
| 102 } | 122 } |
| 103 | 123 |
| 104 scoped_ptr<SkBitmap> bitmap(gfx::JPEGCodec::Decode( | 124 scoped_ptr<SkBitmap> bitmap(gfx::JPEGCodec::Decode( |
| 105 reinterpret_cast<const unsigned char*>(data.data()), data.size())); | 125 reinterpret_cast<const unsigned char*>(data.data()), data.size())); |
| 106 if (!bitmap) | 126 if (!bitmap) |
| 107 LOG(ERROR) << "Unable to decode JPEG data from " << path.value(); | 127 LOG(ERROR) << "Unable to decode JPEG data from " << path.value(); |
| 108 return bitmap.Pass(); | 128 return bitmap.Pass(); |
| 109 } | 129 } |
| 110 | 130 |
| 111 void LoadWallpaper() { | |
| 112 if (cancel_flag_.IsSet()) | |
| 113 return; | |
| 114 | |
| 115 if (!file_path_.empty()) | |
| 116 file_bitmap_ = LoadSkBitmapFromJPEGFile(file_path_); | |
| 117 | |
| 118 if (cancel_flag_.IsSet()) | |
| 119 return; | |
| 120 | |
| 121 if (file_bitmap_) { | |
| 122 gfx::ImageSkia image = gfx::ImageSkia::CreateFrom1xBitmap(*file_bitmap_); | |
| 123 wallpaper_resizer_.reset(new WallpaperResizer( | |
| 124 image, GetMaxDisplaySizeInNative(), file_layout_)); | |
| 125 } else { | |
| 126 wallpaper_resizer_.reset(new WallpaperResizer( | |
| 127 resource_id_, GetMaxDisplaySizeInNative(), resource_layout_)); | |
| 128 } | |
| 129 } | |
| 130 | |
| 131 ~WallpaperLoader() {} | 131 ~WallpaperLoader() {} |
| 132 | 132 |
| 133 base::CancellationFlag cancel_flag_; | 133 base::CancellationFlag cancel_flag_; |
| 134 | 134 |
| 135 // Bitmap loaded from |file_path_|. | 135 // Bitmap loaded from |file_path_|. |
| 136 scoped_ptr<SkBitmap> file_bitmap_; | 136 scoped_ptr<SkBitmap> file_bitmap_; |
| 137 | 137 |
| 138 scoped_ptr<WallpaperResizer> wallpaper_resizer_; | 138 scoped_ptr<WallpaperResizer> wallpaper_resizer_; |
| 139 | 139 |
| 140 // Path to a trusted JPEG file. | 140 // Path to a trusted JPEG file. |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 156 : command_line_for_testing_(NULL), | 156 : command_line_for_testing_(NULL), |
| 157 locked_(false), | 157 locked_(false), |
| 158 desktop_background_mode_(BACKGROUND_NONE), | 158 desktop_background_mode_(BACKGROUND_NONE), |
| 159 current_default_wallpaper_resource_id_(-1), | 159 current_default_wallpaper_resource_id_(-1), |
| 160 weak_ptr_factory_(this), | 160 weak_ptr_factory_(this), |
| 161 wallpaper_reload_delay_(kWallpaperReloadDelayMs) { | 161 wallpaper_reload_delay_(kWallpaperReloadDelayMs) { |
| 162 Shell::GetInstance()->display_controller()->AddObserver(this); | 162 Shell::GetInstance()->display_controller()->AddObserver(this); |
| 163 } | 163 } |
| 164 | 164 |
| 165 DesktopBackgroundController::~DesktopBackgroundController() { | 165 DesktopBackgroundController::~DesktopBackgroundController() { |
| 166 CancelPendingWallpaperOperation(); | 166 CancelDefaultWallpaperLoader(); |
| 167 Shell::GetInstance()->display_controller()->RemoveObserver(this); | 167 Shell::GetInstance()->display_controller()->RemoveObserver(this); |
| 168 } | 168 } |
| 169 | 169 |
| 170 gfx::ImageSkia DesktopBackgroundController::GetWallpaper() const { | 170 gfx::ImageSkia DesktopBackgroundController::GetWallpaper() const { |
| 171 if (current_wallpaper_) | 171 if (current_wallpaper_) |
| 172 return current_wallpaper_->image(); | 172 return current_wallpaper_->image(); |
| 173 return gfx::ImageSkia(); | 173 return gfx::ImageSkia(); |
| 174 } | 174 } |
| 175 | 175 |
| 176 void DesktopBackgroundController::AddObserver( | 176 void DesktopBackgroundController::AddObserver( |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 225 switches::kAshGuestWallpaperSmall; | 225 switches::kAshGuestWallpaperSmall; |
| 226 } else { | 226 } else { |
| 227 switch_name = use_large ? switches::kAshDefaultWallpaperLarge : | 227 switch_name = use_large ? switches::kAshDefaultWallpaperLarge : |
| 228 switches::kAshDefaultWallpaperSmall; | 228 switches::kAshDefaultWallpaperSmall; |
| 229 } | 229 } |
| 230 file_path = command_line->GetSwitchValuePath(switch_name); | 230 file_path = command_line->GetSwitchValuePath(switch_name); |
| 231 | 231 |
| 232 if (DefaultWallpaperIsAlreadyLoadingOrLoaded(file_path, resource_id)) | 232 if (DefaultWallpaperIsAlreadyLoadingOrLoaded(file_path, resource_id)) |
| 233 return false; | 233 return false; |
| 234 | 234 |
| 235 CancelPendingWallpaperOperation(); | 235 CancelDefaultWallpaperLoader(); |
| 236 wallpaper_loader_ = new WallpaperLoader( | 236 default_wallpaper_loader_ = new WallpaperLoader( |
| 237 file_path, file_layout, resource_id, resource_layout); | 237 file_path, file_layout, resource_id, resource_layout); |
| 238 base::WorkerPool::PostTaskAndReply( | 238 base::WorkerPool::PostTaskAndReply( |
| 239 FROM_HERE, | 239 FROM_HERE, |
| 240 base::Bind(&WallpaperLoader::LoadOnWorkerPoolThread, wallpaper_loader_), | 240 base::Bind(&WallpaperLoader::LoadOnWorkerPoolThread, |
| 241 default_wallpaper_loader_), | |
| 241 base::Bind(&DesktopBackgroundController::OnDefaultWallpaperLoadCompleted, | 242 base::Bind(&DesktopBackgroundController::OnDefaultWallpaperLoadCompleted, |
| 242 weak_ptr_factory_.GetWeakPtr(), | 243 weak_ptr_factory_.GetWeakPtr(), |
| 243 wallpaper_loader_), | 244 default_wallpaper_loader_), |
| 244 true /* task_is_slow */); | 245 true /* task_is_slow */); |
| 245 return true; | 246 return true; |
| 246 } | 247 } |
| 247 | 248 |
| 248 void DesktopBackgroundController::SetCustomWallpaper( | 249 void DesktopBackgroundController::SetCustomWallpaper( |
| 249 const gfx::ImageSkia& image, | 250 const gfx::ImageSkia& image, |
| 250 WallpaperLayout layout) { | 251 WallpaperLayout layout) { |
| 251 CancelPendingWallpaperOperation(); | 252 CancelDefaultWallpaperLoader(); |
| 253 | |
| 252 if (CustomWallpaperIsAlreadyLoaded(image)) | 254 if (CustomWallpaperIsAlreadyLoaded(image)) |
| 253 return; | 255 return; |
| 254 | 256 |
| 255 current_wallpaper_.reset(new WallpaperResizer( | 257 current_wallpaper_.reset(new WallpaperResizer( |
| 256 image, GetMaxDisplaySizeInNative(), layout)); | 258 image, GetMaxDisplaySizeInNative(), layout)); |
| 257 current_wallpaper_->StartResize(); | 259 current_wallpaper_->StartResize(); |
| 258 | 260 |
| 259 current_default_wallpaper_path_ = base::FilePath(); | 261 current_default_wallpaper_path_ = base::FilePath(); |
| 260 current_default_wallpaper_resource_id_ = -1; | 262 current_default_wallpaper_resource_id_ = -1; |
| 261 | 263 |
| 262 FOR_EACH_OBSERVER(DesktopBackgroundControllerObserver, observers_, | 264 FOR_EACH_OBSERVER(DesktopBackgroundControllerObserver, observers_, |
| 263 OnWallpaperDataChanged()); | 265 OnWallpaperDataChanged()); |
| 264 SetDesktopBackgroundImageMode(); | 266 SetDesktopBackgroundImageMode(); |
| 265 } | 267 } |
| 266 | 268 |
| 267 void DesktopBackgroundController::CancelPendingWallpaperOperation() { | 269 void DesktopBackgroundController::CancelDefaultWallpaperLoader() { |
| 268 // Set canceled flag of previous request to skip unneeded loading. | 270 // Set canceled flag of previous request to skip unneeded loading. |
| 269 if (wallpaper_loader_.get()) | 271 if (default_wallpaper_loader_.get()) |
| 270 wallpaper_loader_->Cancel(); | 272 default_wallpaper_loader_->Cancel(); |
| 271 | 273 |
| 272 // Cancel reply callback for previous request. | 274 // Cancel reply callback for previous request. |
| 273 weak_ptr_factory_.InvalidateWeakPtrs(); | 275 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 274 } | 276 } |
| 275 | 277 |
| 276 void DesktopBackgroundController::CreateEmptyWallpaper() { | 278 void DesktopBackgroundController::CreateEmptyWallpaper() { |
| 277 current_wallpaper_.reset(NULL); | 279 current_wallpaper_.reset(NULL); |
| 278 SetDesktopBackgroundImageMode(); | 280 SetDesktopBackgroundImageMode(); |
| 279 } | 281 } |
| 280 | 282 |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 311 timer_.Stop(); | 313 timer_.Stop(); |
| 312 timer_.Start(FROM_HERE, | 314 timer_.Start(FROM_HERE, |
| 313 base::TimeDelta::FromMilliseconds(wallpaper_reload_delay_), | 315 base::TimeDelta::FromMilliseconds(wallpaper_reload_delay_), |
| 314 this, | 316 this, |
| 315 &DesktopBackgroundController::UpdateWallpaper); | 317 &DesktopBackgroundController::UpdateWallpaper); |
| 316 } | 318 } |
| 317 } | 319 } |
| 318 } | 320 } |
| 319 | 321 |
| 320 bool DesktopBackgroundController::DefaultWallpaperIsAlreadyLoadingOrLoaded( | 322 bool DesktopBackgroundController::DefaultWallpaperIsAlreadyLoadingOrLoaded( |
| 321 const base::FilePath& image_file, int image_resource_id) const { | 323 const base::FilePath& image_file, |
| 322 return (wallpaper_loader_.get() && | 324 int image_resource_id) const { |
| 323 wallpaper_loader_->file_path() == image_file && | 325 return (default_wallpaper_loader_.get() && |
| 324 wallpaper_loader_->resource_id() == image_resource_id) || | 326 !default_wallpaper_loader_->IsCanceled() && |
|
Daniel Erat
2013/12/26 20:52:00
this is the fix
| |
| 327 default_wallpaper_loader_->file_path() == image_file && | |
| 328 default_wallpaper_loader_->resource_id() == image_resource_id) || | |
| 325 (current_wallpaper_.get() && | 329 (current_wallpaper_.get() && |
| 326 current_default_wallpaper_path_ == image_file && | 330 current_default_wallpaper_path_ == image_file && |
| 327 current_default_wallpaper_resource_id_ == image_resource_id); | 331 current_default_wallpaper_resource_id_ == image_resource_id); |
| 328 } | 332 } |
| 329 | 333 |
| 330 bool DesktopBackgroundController::CustomWallpaperIsAlreadyLoaded( | 334 bool DesktopBackgroundController::CustomWallpaperIsAlreadyLoaded( |
| 331 const gfx::ImageSkia& image) const { | 335 const gfx::ImageSkia& image) const { |
| 332 return current_wallpaper_.get() && | 336 return current_wallpaper_.get() && |
| 333 (WallpaperResizer::GetImageId(image) == | 337 (WallpaperResizer::GetImageId(image) == |
| 334 current_wallpaper_->original_image_id()); | 338 current_wallpaper_->original_image_id()); |
| 335 } | 339 } |
| 336 | 340 |
| 337 void DesktopBackgroundController::SetDesktopBackgroundImageMode() { | 341 void DesktopBackgroundController::SetDesktopBackgroundImageMode() { |
| 338 desktop_background_mode_ = BACKGROUND_IMAGE; | 342 desktop_background_mode_ = BACKGROUND_IMAGE; |
| 339 InstallDesktopControllerForAllWindows(); | 343 InstallDesktopControllerForAllWindows(); |
| 340 } | 344 } |
| 341 | 345 |
| 342 void DesktopBackgroundController::OnDefaultWallpaperLoadCompleted( | 346 void DesktopBackgroundController::OnDefaultWallpaperLoadCompleted( |
| 343 scoped_refptr<WallpaperLoader> loader) { | 347 scoped_refptr<WallpaperLoader> loader) { |
| 344 current_wallpaper_.reset(loader->ReleaseWallpaperResizer()); | 348 current_wallpaper_.reset(loader->ReleaseWallpaperResizer()); |
| 345 current_wallpaper_->StartResize(); | 349 current_wallpaper_->StartResize(); |
| 346 current_default_wallpaper_path_ = loader->file_path(); | 350 current_default_wallpaper_path_ = loader->file_path(); |
| 347 current_default_wallpaper_resource_id_ = loader->resource_id(); | 351 current_default_wallpaper_resource_id_ = loader->resource_id(); |
| 348 FOR_EACH_OBSERVER(DesktopBackgroundControllerObserver, observers_, | 352 FOR_EACH_OBSERVER(DesktopBackgroundControllerObserver, observers_, |
| 349 OnWallpaperDataChanged()); | 353 OnWallpaperDataChanged()); |
| 350 | 354 |
| 351 SetDesktopBackgroundImageMode(); | 355 SetDesktopBackgroundImageMode(); |
| 352 | 356 |
| 353 DCHECK(loader.get() == wallpaper_loader_.get()); | 357 DCHECK(loader.get() == default_wallpaper_loader_.get()); |
| 354 wallpaper_loader_ = NULL; | 358 default_wallpaper_loader_ = NULL; |
| 355 } | 359 } |
| 356 | 360 |
| 357 void DesktopBackgroundController::InstallDesktopController( | 361 void DesktopBackgroundController::InstallDesktopController( |
| 358 aura::Window* root_window) { | 362 aura::Window* root_window) { |
| 359 internal::DesktopBackgroundWidgetController* component = NULL; | 363 internal::DesktopBackgroundWidgetController* component = NULL; |
| 360 int container_id = GetBackgroundContainerId(locked_); | 364 int container_id = GetBackgroundContainerId(locked_); |
| 361 | 365 |
| 362 switch (desktop_background_mode_) { | 366 switch (desktop_background_mode_) { |
| 363 case BACKGROUND_IMAGE: { | 367 case BACKGROUND_IMAGE: { |
| 364 views::Widget* widget = internal::CreateDesktopBackground(root_window, | 368 views::Widget* widget = internal::CreateDesktopBackground(root_window, |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 454 iter->rotation() == gfx::Display::ROTATE_270) { | 458 iter->rotation() == gfx::Display::ROTATE_270) { |
| 455 size_in_pixel = gfx::Size(size_in_pixel.height(), size_in_pixel.width()); | 459 size_in_pixel = gfx::Size(size_in_pixel.height(), size_in_pixel.width()); |
| 456 } | 460 } |
| 457 width = std::max(size_in_pixel.width(), width); | 461 width = std::max(size_in_pixel.width(), width); |
| 458 height = std::max(size_in_pixel.height(), height); | 462 height = std::max(size_in_pixel.height(), height); |
| 459 } | 463 } |
| 460 return gfx::Size(width, height); | 464 return gfx::Size(width, height); |
| 461 } | 465 } |
| 462 | 466 |
| 463 } // namespace ash | 467 } // namespace ash |
| OLD | NEW |