diff options
Diffstat (limited to 'ash/desktop_background/desktop_background_controller.cc')
-rw-r--r-- | ash/desktop_background/desktop_background_controller.cc | 299 |
1 files changed, 61 insertions, 238 deletions
diff --git a/ash/desktop_background/desktop_background_controller.cc b/ash/desktop_background/desktop_background_controller.cc index 7305586..fe111c0 100644 --- a/ash/desktop_background/desktop_background_controller.cc +++ b/ash/desktop_background/desktop_background_controller.cc @@ -24,7 +24,6 @@ #include "base/synchronization/cancellation_flag.h" #include "base/threading/worker_pool.h" #include "content/public/browser/browser_thread.h" -#include "grit/ash_resources.h" #include "ui/aura/window.h" #include "ui/aura/window_event_dispatcher.h" #include "ui/compositor/layer.h" @@ -44,127 +43,14 @@ const int kWallpaperReloadDelayMs = 2000; } // namespace -const int kSmallWallpaperMaxWidth = 1366; -const int kSmallWallpaperMaxHeight = 800; -const int kLargeWallpaperMaxWidth = 2560; -const int kLargeWallpaperMaxHeight = 1700; -const int kWallpaperThumbnailWidth = 108; -const int kWallpaperThumbnailHeight = 68; - -// DesktopBackgroundController::WallpaperLoader wraps background wallpaper -// loading. -class DesktopBackgroundController::WallpaperLoader - : public base::RefCountedThreadSafe< - DesktopBackgroundController::WallpaperLoader> { - public: - // If set, |file_path| must be a trusted (i.e. read-only, - // non-user-controlled) file containing a JPEG image. - WallpaperLoader(const base::FilePath& file_path, - WallpaperLayout file_layout, - int resource_id, - WallpaperLayout resource_layout) - : file_path_(file_path), - file_layout_(file_layout), - resource_id_(resource_id), - resource_layout_(resource_layout) { - } - - void LoadOnWorkerPoolThread() { - DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); - if (cancel_flag_.IsSet()) - return; - - if (!file_path_.empty()) { - VLOG(1) << "Loading " << file_path_.value(); - file_bitmap_ = LoadSkBitmapFromJPEGFile(file_path_); - } - - if (cancel_flag_.IsSet()) - return; - - if (file_bitmap_) { - gfx::ImageSkia image = gfx::ImageSkia::CreateFrom1xBitmap(*file_bitmap_); - wallpaper_resizer_.reset(new WallpaperResizer( - image, GetMaxDisplaySizeInNative(), file_layout_)); - } else { - wallpaper_resizer_.reset(new WallpaperResizer( - resource_id_, GetMaxDisplaySizeInNative(), resource_layout_)); - } - } - - const base::FilePath& file_path() const { return file_path_; } - int resource_id() const { return resource_id_; } - - void Cancel() { - cancel_flag_.Set(); - } - - bool IsCanceled() { - return cancel_flag_.IsSet(); - } - - WallpaperResizer* ReleaseWallpaperResizer() { - return wallpaper_resizer_.release(); - } - - private: - friend class base::RefCountedThreadSafe< - DesktopBackgroundController::WallpaperLoader>; - - // Loads a JPEG image from |path|, a trusted file -- note that the image - // is not loaded in a sandboxed process. Returns an empty pointer on - // error. - static scoped_ptr<SkBitmap> LoadSkBitmapFromJPEGFile( - const base::FilePath& path) { - std::string data; - if (!base::ReadFileToString(path, &data)) { - LOG(ERROR) << "Unable to read data from " << path.value(); - return scoped_ptr<SkBitmap>(); - } - - scoped_ptr<SkBitmap> bitmap(gfx::JPEGCodec::Decode( - reinterpret_cast<const unsigned char*>(data.data()), data.size())); - if (!bitmap) - LOG(ERROR) << "Unable to decode JPEG data from " << path.value(); - return bitmap.Pass(); - } - - ~WallpaperLoader() {} - - base::CancellationFlag cancel_flag_; - - // Bitmap loaded from |file_path_|. - scoped_ptr<SkBitmap> file_bitmap_; - - scoped_ptr<WallpaperResizer> wallpaper_resizer_; - - // Path to a trusted JPEG file. - base::FilePath file_path_; - - // Layout to be used when displaying the image from |file_path_|. - WallpaperLayout file_layout_; - - // ID of an image resource to use if |file_path_| is empty or unloadable. - int resource_id_; - - // Layout to be used when displaying |resource_id_|. - WallpaperLayout resource_layout_; - - DISALLOW_COPY_AND_ASSIGN(WallpaperLoader); -}; - DesktopBackgroundController::DesktopBackgroundController() - : command_line_for_testing_(NULL), - locked_(false), + : locked_(false), desktop_background_mode_(BACKGROUND_NONE), - current_default_wallpaper_resource_id_(-1), - weak_ptr_factory_(this), wallpaper_reload_delay_(kWallpaperReloadDelayMs) { Shell::GetInstance()->display_controller()->AddObserver(this); } DesktopBackgroundController::~DesktopBackgroundController() { - CancelDefaultWallpaperLoader(); Shell::GetInstance()->display_controller()->RemoveObserver(this); } @@ -207,80 +93,44 @@ void DesktopBackgroundController::OnRootWindowAdded(aura::Window* root_window) { InstallDesktopController(root_window); } -bool DesktopBackgroundController::SetDefaultWallpaper(bool is_guest) { - VLOG(1) << "SetDefaultWallpaper: is_guest=" << is_guest; - const bool use_large = - GetAppropriateResolution() == WALLPAPER_RESOLUTION_LARGE; - - base::FilePath file_path; - WallpaperLayout file_layout = use_large ? WALLPAPER_LAYOUT_CENTER_CROPPED : - WALLPAPER_LAYOUT_CENTER; - int resource_id = use_large ? IDR_AURA_WALLPAPER_DEFAULT_LARGE : - IDR_AURA_WALLPAPER_DEFAULT_SMALL; - WallpaperLayout resource_layout = WALLPAPER_LAYOUT_TILE; - - CommandLine* command_line = command_line_for_testing_ ? - command_line_for_testing_ : CommandLine::ForCurrentProcess(); - const char* switch_name = NULL; - if (is_guest) { - switch_name = use_large ? switches::kAshGuestWallpaperLarge : - switches::kAshGuestWallpaperSmall; - } else { - switch_name = use_large ? switches::kAshDefaultWallpaperLarge : - switches::kAshDefaultWallpaperSmall; - } - file_path = command_line->GetSwitchValuePath(switch_name); +bool DesktopBackgroundController::SetWallpaperImage(const gfx::ImageSkia& image, + WallpaperLayout layout) { + VLOG(1) << "SetWallpaper: image_id=" << WallpaperResizer::GetImageId(image) + << " layout=" << layout; - if (DefaultWallpaperIsAlreadyLoadingOrLoaded(file_path, resource_id)) { - VLOG(1) << "Default wallpaper is already loading or loaded"; + if (WallpaperIsAlreadyLoaded(&image, kInvalidResourceID, layout)) { + VLOG(1) << "Wallpaper is already loaded"; return false; } - CancelDefaultWallpaperLoader(); - default_wallpaper_loader_ = new WallpaperLoader( - file_path, file_layout, resource_id, resource_layout); - base::WorkerPool::PostTaskAndReply( - FROM_HERE, - base::Bind(&WallpaperLoader::LoadOnWorkerPoolThread, - default_wallpaper_loader_), - base::Bind(&DesktopBackgroundController::OnDefaultWallpaperLoadCompleted, - weak_ptr_factory_.GetWeakPtr(), - default_wallpaper_loader_), - true /* task_is_slow */); + current_wallpaper_.reset( + new WallpaperResizer(image, GetMaxDisplaySizeInNative(), layout)); + current_wallpaper_->StartResize(); + + FOR_EACH_OBSERVER(DesktopBackgroundControllerObserver, + observers_, + OnWallpaperDataChanged()); + SetDesktopBackgroundImageMode(); return true; } -void DesktopBackgroundController::SetCustomWallpaper( - const gfx::ImageSkia& image, - WallpaperLayout layout) { - VLOG(1) << "SetCustomWallpaper: image_id=" - << WallpaperResizer::GetImageId(image) << " layout=" << layout; - CancelDefaultWallpaperLoader(); +bool DesktopBackgroundController::SetWallpaperResource(int resource_id, + WallpaperLayout layout) { + VLOG(1) << "SetWallpaper: resource_id=" << resource_id + << " layout=" << layout; - if (CustomWallpaperIsAlreadyLoaded(image)) { - VLOG(1) << "Custom wallpaper is already loaded"; - return; + if (WallpaperIsAlreadyLoaded(NULL, resource_id, layout)) { + VLOG(1) << "Wallpaper is already loaded"; + return false; } - - current_wallpaper_.reset(new WallpaperResizer( - image, GetMaxDisplaySizeInNative(), layout)); + current_wallpaper_.reset( + new WallpaperResizer(resource_id, GetMaxDisplaySizeInNative(), layout)); current_wallpaper_->StartResize(); - current_default_wallpaper_path_ = base::FilePath(); - current_default_wallpaper_resource_id_ = -1; - FOR_EACH_OBSERVER(DesktopBackgroundControllerObserver, observers_, OnWallpaperDataChanged()); SetDesktopBackgroundImageMode(); -} - -void DesktopBackgroundController::CancelDefaultWallpaperLoader() { - // Set canceled flag of previous request to skip unneeded loading. - if (default_wallpaper_loader_.get()) - default_wallpaper_loader_->Cancel(); - - // Cancel reply callback for previous request. - weak_ptr_factory_.InvalidateWeakPtrs(); + return true; } void DesktopBackgroundController::CreateEmptyWallpaper() { @@ -288,14 +138,6 @@ void DesktopBackgroundController::CreateEmptyWallpaper() { SetDesktopBackgroundImageMode(); } -WallpaperResolution DesktopBackgroundController::GetAppropriateResolution() { - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); - gfx::Size size = GetMaxDisplaySizeInNative(); - return (size.width() > kSmallWallpaperMaxWidth || - size.height() > kSmallWallpaperMaxHeight) ? - WALLPAPER_RESOLUTION_LARGE : WALLPAPER_RESOLUTION_SMALL; -} - bool DesktopBackgroundController::MoveDesktopToLockedContainer() { if (locked_) return false; @@ -327,23 +169,44 @@ void DesktopBackgroundController::OnDisplayConfigurationChanged() { } } -bool DesktopBackgroundController::DefaultWallpaperIsAlreadyLoadingOrLoaded( - const base::FilePath& image_file, - int image_resource_id) const { - return (default_wallpaper_loader_.get() && - !default_wallpaper_loader_->IsCanceled() && - default_wallpaper_loader_->file_path() == image_file && - default_wallpaper_loader_->resource_id() == image_resource_id) || - (current_wallpaper_.get() && - current_default_wallpaper_path_ == image_file && - current_default_wallpaper_resource_id_ == image_resource_id); +// static +gfx::Size DesktopBackgroundController::GetMaxDisplaySizeInNative() { + int width = 0; + int height = 0; + std::vector<gfx::Display> displays = Shell::GetScreen()->GetAllDisplays(); + DisplayManager* display_manager = Shell::GetInstance()->display_manager(); + + for (std::vector<gfx::Display>::iterator iter = displays.begin(); + iter != displays.end(); ++iter) { + // Don't use size_in_pixel because we want to use the native pixel size. + gfx::Size size_in_pixel = + display_manager->GetDisplayInfo(iter->id()).bounds_in_native().size(); + if (iter->rotation() == gfx::Display::ROTATE_90 || + iter->rotation() == gfx::Display::ROTATE_270) { + size_in_pixel = gfx::Size(size_in_pixel.height(), size_in_pixel.width()); + } + width = std::max(size_in_pixel.width(), width); + height = std::max(size_in_pixel.height(), height); + } + return gfx::Size(width, height); } -bool DesktopBackgroundController::CustomWallpaperIsAlreadyLoaded( - const gfx::ImageSkia& image) const { - return current_wallpaper_.get() && - (WallpaperResizer::GetImageId(image) == - current_wallpaper_->original_image_id()); +bool DesktopBackgroundController::WallpaperIsAlreadyLoaded( + const gfx::ImageSkia* image, + int resource_id, + WallpaperLayout layout) const { + if (!current_wallpaper_.get()) + return false; + + if (layout != current_wallpaper_->layout()) + return false; + + if (image) { + return WallpaperResizer::GetImageId(*image) == + current_wallpaper_->original_image_id(); + } + + return current_wallpaper_->resource_id() == resource_id; } void DesktopBackgroundController::SetDesktopBackgroundImageMode() { @@ -351,22 +214,6 @@ void DesktopBackgroundController::SetDesktopBackgroundImageMode() { InstallDesktopControllerForAllWindows(); } -void DesktopBackgroundController::OnDefaultWallpaperLoadCompleted( - scoped_refptr<WallpaperLoader> loader) { - VLOG(1) << "OnDefaultWallpaperLoadCompleted"; - current_wallpaper_.reset(loader->ReleaseWallpaperResizer()); - current_wallpaper_->StartResize(); - current_default_wallpaper_path_ = loader->file_path(); - current_default_wallpaper_resource_id_ = loader->resource_id(); - FOR_EACH_OBSERVER(DesktopBackgroundControllerObserver, observers_, - OnWallpaperDataChanged()); - - SetDesktopBackgroundImageMode(); - - DCHECK(loader.get() == default_wallpaper_loader_.get()); - default_wallpaper_loader_ = NULL; -} - void DesktopBackgroundController::InstallDesktopController( aura::Window* root_window) { DesktopBackgroundWidgetController* component = NULL; @@ -443,32 +290,8 @@ int DesktopBackgroundController::GetBackgroundContainerId(bool locked) { void DesktopBackgroundController::UpdateWallpaper() { current_wallpaper_.reset(NULL); - current_default_wallpaper_path_ = base::FilePath(); - current_default_wallpaper_resource_id_ = -1; ash::Shell::GetInstance()->user_wallpaper_delegate()-> UpdateWallpaper(true /* clear cache */); } -// static -gfx::Size DesktopBackgroundController::GetMaxDisplaySizeInNative() { - int width = 0; - int height = 0; - std::vector<gfx::Display> displays = Shell::GetScreen()->GetAllDisplays(); - DisplayManager* display_manager = Shell::GetInstance()->display_manager(); - - for (std::vector<gfx::Display>::iterator iter = displays.begin(); - iter != displays.end(); ++iter) { - // Don't use size_in_pixel because we want to use the native pixel size. - gfx::Size size_in_pixel = - display_manager->GetDisplayInfo(iter->id()).bounds_in_native().size(); - if (iter->rotation() == gfx::Display::ROTATE_90 || - iter->rotation() == gfx::Display::ROTATE_270) { - size_in_pixel = gfx::Size(size_in_pixel.height(), size_in_pixel.width()); - } - width = std::max(size_in_pixel.width(), width); - height = std::max(size_in_pixel.height(), height); - } - return gfx::Size(width, height); -} - } // namespace ash |