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

Unified Diff: ash/desktop_background/desktop_background_controller.cc

Issue 215293003: Move all wallpaper file loading and decoding from DesktopBackgroundController to WallpaperManager. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix windows build. Created 6 years, 9 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 7673f29aa6a48d9439e9397c4663105e09f001b3..e22e2647a31c5d686aca903e9eef9f9c585bb1d0 100644
--- a/ash/desktop_background/desktop_background_controller.cc
+++ b/ash/desktop_background/desktop_background_controller.cc
@@ -52,120 +52,14 @@ 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);
}
@@ -208,58 +102,13 @@ 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);
+void DesktopBackgroundController::SetWallpaper(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";
- 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 */);
- return true;
-}
-
-void DesktopBackgroundController::SetCustomWallpaper(
- const gfx::ImageSkia& image,
- WallpaperLayout layout) {
- VLOG(1) << "SetCustomWallpaper: image_id="
- << WallpaperResizer::GetImageId(image) << " layout=" << layout;
- CancelDefaultWallpaperLoader();
-
- if (CustomWallpaperIsAlreadyLoaded(image)) {
- VLOG(1) << "Custom wallpaper is already loaded";
+ if (WallpaperIsAlreadyLoaded(image)) {
bshe 2014/04/01 15:32:56 should we check if layout is the same in the case?
Alexander Alekseev 2014/04/03 19:15:44 Done.
+ VLOG(1) << "Wallpaper is already loaded";
return;
}
@@ -267,23 +116,11 @@ void DesktopBackgroundController::SetCustomWallpaper(
image, 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();
-}
-
void DesktopBackgroundController::CreateEmptyWallpaper() {
current_wallpaper_.reset(NULL);
SetDesktopBackgroundImageMode();
@@ -328,19 +165,7 @@ 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);
-}
-
-bool DesktopBackgroundController::CustomWallpaperIsAlreadyLoaded(
+bool DesktopBackgroundController::WallpaperIsAlreadyLoaded(
const gfx::ImageSkia& image) const {
return current_wallpaper_.get() &&
(WallpaperResizer::GetImageId(image) ==
@@ -352,22 +177,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) {
internal::DesktopBackgroundWidgetController* component = NULL;
@@ -445,8 +254,6 @@ 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 */);
}

Powered by Google App Engine
This is Rietveld 408576698