Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(75)

Unified Diff: ash/desktop_background/desktop_background_controller.cc

Issue 10207030: Asynchronously load wallpapers when user pod is selected. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Nits Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ash/desktop_background/desktop_background_controller.cc
diff --git a/ash/desktop_background/desktop_background_controller.cc b/ash/desktop_background/desktop_background_controller.cc
index e34515878cf112433e4ddc325a1a7422ba1eb3a6..f95c32e9391c4da59e398b819e4a4ba03b71ab04 100644
--- a/ash/desktop_background/desktop_background_controller.cc
+++ b/ash/desktop_background/desktop_background_controller.cc
@@ -9,7 +9,10 @@
#include "ash/shell_factory.h"
#include "ash/shell_window_ids.h"
#include "ash/wm/root_window_layout_manager.h"
+#include "base/bind.h"
#include "base/logging.h"
+#include "base/synchronization/cancellation_flag.h"
+#include "base/threading/worker_pool.h"
#include "grit/ui_resources.h"
#include "ui/aura/window.h"
#include "ui/base/resource/resource_bundle.h"
@@ -19,26 +22,109 @@
namespace ash {
-DesktopBackgroundController::DesktopBackgroundController() :
- desktop_background_mode_(BACKGROUND_IMAGE) {
+// DesktopBackgroundController::WallpaperOperation wraps background wallpaper
+// loading.
+class DesktopBackgroundController::WallpaperOperation
+ : public base::RefCountedThreadSafe<
+ DesktopBackgroundController::WallpaperOperation> {
+ public:
+ WallpaperOperation(int index)
+ : wallpaper_(NULL),
+ layout_(CENTER_CROPPED),
+ index_(index) {
+ }
+
+ static void Run(scoped_refptr<WallpaperOperation> wo) {
+ wo->LoadingWallpaper();
+ }
+
+ void LoadingWallpaper() {
+ if (cancel_flag_.IsSet())
+ return;
+
+ wallpaper_ = ui::ResourceBundle::GetSharedInstance().GetImageNamed(
+ GetWallpaperInfo(index_).id).ToSkBitmap();
+
+ if (cancel_flag_.IsSet())
+ return;
+ layout_ = GetWallpaperInfo(index_).layout;
+ }
+
+ void Cancel() {
+ cancel_flag_.Set();
+ }
+
+ const SkBitmap* wallpaper() {
+ return wallpaper_;
+ }
+
+ ImageLayout image_layout() {
+ return layout_;
+ }
+
+ int index() {
+ return index_;
+ }
+
+ private:
+ friend class base::RefCountedThreadSafe<
+ DesktopBackgroundController::WallpaperOperation>;
+ ~WallpaperOperation(){};
+
+ base::CancellationFlag cancel_flag_;
+
+ const SkBitmap* wallpaper_;
+ ImageLayout layout_;
+ int index_;
+
+ DISALLOW_COPY_AND_ASSIGN(WallpaperOperation);
+};
+
+DesktopBackgroundController::DesktopBackgroundController()
+ : desktop_background_mode_(BACKGROUND_IMAGE),
+ previous_index_(-1),
+ weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
}
DesktopBackgroundController::~DesktopBackgroundController() {
+ CancelPendingWallpaperOperation();
}
-void DesktopBackgroundController::SetDesktopBackgroundImageMode() {
- internal::RootWindowLayoutManager* root_window_layout =
- Shell::GetInstance()->root_window_layout();
+void DesktopBackgroundController::SetDefaultWallpaper(int index) {
+ if (previous_index_ == index)
+ return;
+ CancelPendingWallpaperOperation();
+
+ wallpaper_op_ = new WallpaperOperation(index);
+ base::WorkerPool::PostTaskAndReply(
+ FROM_HERE,
+ base::Bind(&WallpaperOperation::Run, wallpaper_op_),
+ base::Bind(&DesktopBackgroundController::OnWallpaperLoadCompleted,
+ weak_ptr_factory_.GetWeakPtr(),
+ wallpaper_op_),
+ true /* task_is_slow */);
+}
+
+void DesktopBackgroundController::CancelPendingWallpaperOperation() {
+ // Set canceled flag of previous request to skip unneeded loading.
+ if (wallpaper_op_.get())
+ wallpaper_op_->Cancel();
+
+ // Cancel reply callback for previous request.
+ weak_ptr_factory_.InvalidateWeakPtrs();
+}
+
+void DesktopBackgroundController::SetLoggedInUserWallpaper() {
int index = Shell::GetInstance()->user_wallpaper_delegate()->
GetUserWallpaperIndex();
// We should not change background when index is invalid. For instance, at
- // login screen.
- if (index == ash::GetInvalidWallpaperIndex())
+ // login screen or stub_user login.
+ if (index == ash::GetInvalidWallpaperIndex()) {
+ CreateEmptyWallpaper();
return;
- root_window_layout->SetBackgroundLayer(NULL);
- internal::CreateDesktopBackground(GetWallpaper(index),
- GetWallpaperInfo(index).layout);
- desktop_background_mode_ = BACKGROUND_IMAGE;
+ }
+
+ SetDefaultWallpaper(index);
}
void DesktopBackgroundController::SetDesktopBackgroundSolidColorMode() {
@@ -56,4 +142,30 @@ void DesktopBackgroundController::SetDesktopBackgroundSolidColorMode() {
desktop_background_mode_ = BACKGROUND_SOLID_COLOR;
}
+void DesktopBackgroundController::SetDesktopBackgroundImageMode(
+ scoped_refptr<WallpaperOperation> wo) {
+ internal::RootWindowLayoutManager* root_window_layout =
+ Shell::GetInstance()->root_window_layout();
+ root_window_layout->SetBackgroundLayer(NULL);
+ if(wo->wallpaper()) {
+ internal::CreateDesktopBackground(*wo->wallpaper(), wo->image_layout());
+ desktop_background_mode_ = BACKGROUND_IMAGE;
+ }
+}
+
+void DesktopBackgroundController::OnWallpaperLoadCompleted(
+ scoped_refptr<WallpaperOperation> wo) {
+ SetDesktopBackgroundImageMode(wo);
+ previous_index_ = wo->index();
+
+ DCHECK(wo.get() == wallpaper_op_.get());
+ wallpaper_op_ = NULL;
+}
+
+void DesktopBackgroundController::CreateEmptyWallpaper() {
+ SkBitmap dummy;
+ internal::CreateDesktopBackground(dummy, CENTER);
+ desktop_background_mode_ = BACKGROUND_IMAGE;
+}
+
} // namespace ash
« no previous file with comments | « ash/desktop_background/desktop_background_controller.h ('k') | ash/desktop_background/desktop_background_resources.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698