diff options
author | bshe@chromium.org <bshe@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-11-20 17:17:38 +0000 |
---|---|---|
committer | bshe@chromium.org <bshe@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-11-20 17:17:38 +0000 |
commit | 0c3461ab36efb7744df197f7029d66c3170a61f3 (patch) | |
tree | 5c11004cd470a1f028548e9139738d9cd7c528fb /ash/desktop_background/desktop_background_controller.cc | |
parent | 2dca2dc97fd303df0c6a1fb11828472ef1c00428 (diff) | |
download | chromium_src-0c3461ab36efb7744df197f7029d66c3170a61f3.zip chromium_src-0c3461ab36efb7744df197f7029d66c3170a61f3.tar.gz chromium_src-0c3461ab36efb7744df197f7029d66c3170a61f3.tar.bz2 |
Remove use of index in wallpaper picker code and some refactor
BUG=141431
Review URL: https://codereview.chromium.org/11415015
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@168818 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ash/desktop_background/desktop_background_controller.cc')
-rw-r--r-- | ash/desktop_background/desktop_background_controller.cc | 168 |
1 files changed, 75 insertions, 93 deletions
diff --git a/ash/desktop_background/desktop_background_controller.cc b/ash/desktop_background/desktop_background_controller.cc index 8575c1e..9639196 100644 --- a/ash/desktop_background/desktop_background_controller.cc +++ b/ash/desktop_background/desktop_background_controller.cc @@ -18,6 +18,7 @@ #include "base/synchronization/cancellation_flag.h" #include "base/threading/worker_pool.h" #include "content/public/browser/browser_thread.h" +#include "grit/ash_wallpaper_resources.h" #include "ui/aura/root_window.h" #include "ui/aura/window.h" #include "ui/base/resource/resource_bundle.h" @@ -43,6 +44,24 @@ internal::RootWindowLayoutManager* GetRootWindowLayoutManager( } } // namespace +#if defined(GOOGLE_CHROME_BUILD) +const WallpaperInfo kDefaultLargeWallpaper = + { IDR_AURA_WALLPAPERS_2_LANDSCAPE8_LARGE, WALLPAPER_LAYOUT_CENTER_CROPPED }; +const WallpaperInfo kDefaultSmallWallpaper = + { IDR_AURA_WALLPAPERS_2_LANDSCAPE8_SMALL, WALLPAPER_LAYOUT_CENTER }; +const WallpaperInfo kGuestLargeWallpaper = + { IDR_AURA_WALLPAPERS_2_LANDSCAPE7_LARGE, WALLPAPER_LAYOUT_CENTER_CROPPED }; +const WallpaperInfo kGuestSmallWallpaper = + { IDR_AURA_WALLPAPERS_2_LANDSCAPE7_SMALL, WALLPAPER_LAYOUT_CENTER }; +#else +const WallpaperInfo kDefaultLargeWallpaper = + { IDR_AURA_WALLPAPERS_5_GRADIENT5_LARGE, WALLPAPER_LAYOUT_TILE }; +const WallpaperInfo kDefaultSmallWallpaper = + { IDR_AURA_WALLPAPERS_5_GRADIENT5_SMALL, WALLPAPER_LAYOUT_TILE }; +const WallpaperInfo kGuestLargeWallpaper = kDefaultLargeWallpaper; +const WallpaperInfo kGuestSmallWallpaper = kDefaultSmallWallpaper; +#endif + const int kSmallWallpaperMaxWidth = 1366; const int kSmallWallpaperMaxHeight = 800; const int kLargeWallpaperMaxWidth = 2560; @@ -50,52 +69,40 @@ const int kLargeWallpaperMaxHeight = 1700; // Stores the current wallpaper data. struct DesktopBackgroundController::WallpaperData { - WallpaperData(int index, WallpaperResolution resolution) - : is_default_wallpaper(true), - wallpaper_index(index), - wallpaper_layout(GetWallpaperViewInfo(index, resolution).layout), + explicit WallpaperData(const WallpaperInfo& info) + : wallpaper_info(info), wallpaper_image(*(ui::ResourceBundle::GetSharedInstance().GetImageNamed( - GetWallpaperViewInfo(index, resolution).id).ToImageSkia())) { + info.idr).ToImageSkia())) { } - WallpaperData(WallpaperLayout layout, const gfx::ImageSkia& image) - : is_default_wallpaper(false), - wallpaper_index(-1), - wallpaper_layout(layout), + WallpaperData(const WallpaperInfo& info, const gfx::ImageSkia& image) + : wallpaper_info(info), wallpaper_image(image) { } - const bool is_default_wallpaper; - const int wallpaper_index; - const WallpaperLayout wallpaper_layout; + const WallpaperInfo wallpaper_info; const gfx::ImageSkia wallpaper_image; }; -// DesktopBackgroundController::WallpaperOperation wraps background wallpaper +// DesktopBackgroundController::WallpaperLoader wraps background wallpaper // loading. -class DesktopBackgroundController::WallpaperOperation +class DesktopBackgroundController::WallpaperLoader : public base::RefCountedThreadSafe< - DesktopBackgroundController::WallpaperOperation> { + DesktopBackgroundController::WallpaperLoader> { public: - WallpaperOperation(int index, WallpaperResolution resolution) - : index_(index), - resolution_(resolution) { - } - - static void Run(scoped_refptr<WallpaperOperation> wo) { - wo->LoadingWallpaper(); + explicit WallpaperLoader(const WallpaperInfo& info) + : info_(info) { } - void LoadingWallpaper() { - if (cancel_flag_.IsSet()) - return; - wallpaper_data_.reset(new WallpaperData(index_, resolution_)); + static void LoadOnWorkerPoolThread(scoped_refptr<WallpaperLoader> wl) { + DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); + wl->LoadingWallpaper(); } void Cancel() { cancel_flag_.Set(); } - int index() const { - return index_; + int idr() const { + return info_.idr; } WallpaperData* ReleaseWallpaperData() { @@ -104,19 +111,23 @@ class DesktopBackgroundController::WallpaperOperation private: friend class base::RefCountedThreadSafe< - DesktopBackgroundController::WallpaperOperation>; + DesktopBackgroundController::WallpaperLoader>; - ~WallpaperOperation() {} + void LoadingWallpaper() { + if (cancel_flag_.IsSet()) + return; + wallpaper_data_.reset(new WallpaperData(info_)); + } + + ~WallpaperLoader() {} base::CancellationFlag cancel_flag_; scoped_ptr<WallpaperData> wallpaper_data_; - const int index_; - - const WallpaperResolution resolution_; + const WallpaperInfo info_; - DISALLOW_COPY_AND_ASSIGN(WallpaperOperation); + DISALLOW_COPY_AND_ASSIGN(WallpaperLoader); }; DesktopBackgroundController::DesktopBackgroundController() @@ -148,8 +159,8 @@ void DesktopBackgroundController::RemoveObserver( WallpaperLayout DesktopBackgroundController::GetWallpaperLayout() const { if (current_wallpaper_.get()) - return current_wallpaper_->wallpaper_layout; - return CENTER_CROPPED; + return current_wallpaper_->wallpaper_info.layout; + return WALLPAPER_LAYOUT_CENTER_CROPPED; } gfx::ImageSkia DesktopBackgroundController::GetCurrentWallpaperImage() { @@ -158,6 +169,15 @@ gfx::ImageSkia DesktopBackgroundController::GetCurrentWallpaperImage() { return GetWallpaper(); } +int DesktopBackgroundController::GetWallpaperIDR() const { + if (wallpaper_loader_.get()) + return wallpaper_loader_->idr(); + else if (current_wallpaper_.get()) + return current_wallpaper_->wallpaper_info.idr; + else + return -1; +} + void DesktopBackgroundController::OnRootWindowAdded( aura::RootWindow* root_window) { // Handle resolution change for "built-in" images." @@ -168,69 +188,30 @@ void DesktopBackgroundController::OnRootWindowAdded( // small screen. if (kSmallWallpaperMaxWidth < root_window_size.width() || kSmallWallpaperMaxHeight < root_window_size.height()) { - if (current_wallpaper_->is_default_wallpaper) { - ReloadDefaultWallpaper(); - } else { - ash::Shell::GetInstance()->user_wallpaper_delegate()-> - UpdateWallpaper(); - } + current_wallpaper_.reset(NULL); + ash::Shell::GetInstance()->user_wallpaper_delegate()-> + UpdateWallpaper(); } } InstallDesktopController(root_window); } -void DesktopBackgroundController::CacheDefaultWallpaper(int index) { - DCHECK(index >= 0); - - WallpaperResolution resolution = GetAppropriateResolution(); - scoped_refptr<WallpaperOperation> wallpaper_op = - new WallpaperOperation(index, resolution); - base::WorkerPool::PostTask( - FROM_HERE, - base::Bind(&WallpaperOperation::Run, wallpaper_op), - true); -} - -void DesktopBackgroundController::SetDefaultWallpaper(int index) { - // We should not change background when index is invalid. For instance, at - // login screen or stub_user login. - if (index == GetInvalidWallpaperIndex()) { - CreateEmptyWallpaper(); - return; - } else if (index == GetSolidColorIndex()) { - SetDesktopBackgroundSolidColorMode(kLoginWallpaperColor); - return; - } - - // Prevents loading of the same wallpaper as the currently loading/loaded - // one. - if ((wallpaper_op_.get() && wallpaper_op_->index() == index) || - (current_wallpaper_.get() && - current_wallpaper_->wallpaper_index == index)) { - return; - } +void DesktopBackgroundController::SetDefaultWallpaper( + const WallpaperInfo& info) { + DCHECK_NE(GetWallpaperIDR(), info.idr); CancelPendingWallpaperOperation(); - - WallpaperResolution resolution = GetAppropriateResolution(); - - wallpaper_op_ = new WallpaperOperation(index, resolution); + wallpaper_loader_ = new WallpaperLoader(info); base::WorkerPool::PostTaskAndReply( FROM_HERE, - base::Bind(&WallpaperOperation::Run, wallpaper_op_), + base::Bind(&WallpaperLoader::LoadOnWorkerPoolThread, wallpaper_loader_), base::Bind(&DesktopBackgroundController::OnWallpaperLoadCompleted, weak_ptr_factory_.GetWeakPtr(), - wallpaper_op_), + wallpaper_loader_), true /* task_is_slow */); } -void DesktopBackgroundController::ReloadDefaultWallpaper() { - int index = current_wallpaper_->wallpaper_index; - current_wallpaper_.reset(NULL); - SetDefaultWallpaper(index); -} - void DesktopBackgroundController::SetCustomWallpaper( const gfx::ImageSkia& wallpaper, WallpaperLayout layout) { @@ -240,7 +221,8 @@ void DesktopBackgroundController::SetCustomWallpaper( return; } - current_wallpaper_.reset(new WallpaperData(layout, wallpaper)); + WallpaperInfo info = { -1, layout }; + current_wallpaper_.reset(new WallpaperData(info, wallpaper)); FOR_EACH_OBSERVER(DesktopBackgroundControllerObserver, observers_, OnWallpaperDataChanged()); SetDesktopBackgroundImageMode(); @@ -248,8 +230,8 @@ void DesktopBackgroundController::SetCustomWallpaper( void DesktopBackgroundController::CancelPendingWallpaperOperation() { // Set canceled flag of previous request to skip unneeded loading. - if (wallpaper_op_.get()) - wallpaper_op_->Cancel(); + if (wallpaper_loader_.get()) + wallpaper_loader_->Cancel(); // Cancel reply callback for previous request. weak_ptr_factory_.InvalidateWeakPtrs(); @@ -270,14 +252,14 @@ void DesktopBackgroundController::CreateEmptyWallpaper() { WallpaperResolution DesktopBackgroundController::GetAppropriateResolution() { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); - WallpaperResolution resolution = SMALL; + WallpaperResolution resolution = WALLPAPER_RESOLUTION_SMALL; Shell::RootWindowList root_windows = Shell::GetAllRootWindows(); for (Shell::RootWindowList::iterator iter = root_windows.begin(); iter != root_windows.end(); ++iter) { gfx::Size root_window_size = (*iter)->GetHostSize(); if (root_window_size.width() > kSmallWallpaperMaxWidth || root_window_size.height() > kSmallWallpaperMaxHeight) { - resolution = LARGE; + resolution = WALLPAPER_RESOLUTION_LARGE; } } return resolution; @@ -312,15 +294,15 @@ void DesktopBackgroundController::SetDesktopBackgroundImageMode() { } void DesktopBackgroundController::OnWallpaperLoadCompleted( - scoped_refptr<WallpaperOperation> wo) { - current_wallpaper_.reset(wo->ReleaseWallpaperData()); + scoped_refptr<WallpaperLoader> wl) { + current_wallpaper_.reset(wl->ReleaseWallpaperData()); FOR_EACH_OBSERVER(DesktopBackgroundControllerObserver, observers_, OnWallpaperDataChanged()); SetDesktopBackgroundImageMode(); - DCHECK(wo.get() == wallpaper_op_.get()); - wallpaper_op_ = NULL; + DCHECK(wl.get() == wallpaper_loader_.get()); + wallpaper_loader_ = NULL; } void DesktopBackgroundController::NotifyAnimationFinished() { |