Skip to content

Commit

Permalink
Implement user selected wallpaper feature.
Browse files Browse the repository at this point in the history
BUG=123612, 122791
TEST=Verify if custom wallpaper is supported


Review URL: https://chromiumcodereview.appspot.com/10375010

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@137258 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
bshe@chromium.org committed May 15, 2012
1 parent 977b06c commit 2f5b483
Show file tree
Hide file tree
Showing 18 changed files with 655 additions and 61 deletions.
19 changes: 13 additions & 6 deletions ash/desktop_background/desktop_background_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,8 @@ class DesktopBackgroundController::WallpaperOperation
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;
Expand All @@ -58,7 +56,7 @@ class DesktopBackgroundController::WallpaperOperation
return wallpaper_;
}

ImageLayout image_layout() {
WallpaperLayout wallpaper_layout() {
return layout_;
}

Expand All @@ -69,12 +67,11 @@ class DesktopBackgroundController::WallpaperOperation
private:
friend class base::RefCountedThreadSafe<
DesktopBackgroundController::WallpaperOperation>;
~WallpaperOperation(){};

base::CancellationFlag cancel_flag_;

const SkBitmap* wallpaper_;
ImageLayout layout_;
WallpaperLayout layout_;
int index_;

DISALLOW_COPY_AND_ASSIGN(WallpaperOperation);
Expand All @@ -93,6 +90,7 @@ DesktopBackgroundController::~DesktopBackgroundController() {
void DesktopBackgroundController::SetDefaultWallpaper(int index) {
if (previous_index_ == index)
return;

CancelPendingWallpaperOperation();

wallpaper_op_ = new WallpaperOperation(index);
Expand All @@ -105,6 +103,15 @@ void DesktopBackgroundController::SetDefaultWallpaper(int index) {
true /* task_is_slow */);
}

void DesktopBackgroundController::SetCustomWallpaper(const SkBitmap& wallpaper,
WallpaperLayout layout) {
internal::RootWindowLayoutManager* root_window_layout =
Shell::GetInstance()->root_window_layout();
root_window_layout->SetBackgroundLayer(NULL);
internal::CreateDesktopBackground(wallpaper, layout);
desktop_background_mode_ = BACKGROUND_IMAGE;
}

void DesktopBackgroundController::CancelPendingWallpaperOperation() {
// Set canceled flag of previous request to skip unneeded loading.
if (wallpaper_op_.get())
Expand Down Expand Up @@ -148,7 +155,7 @@ void DesktopBackgroundController::SetDesktopBackgroundImageMode(
Shell::GetInstance()->root_window_layout();
root_window_layout->SetBackgroundLayer(NULL);
if(wo->wallpaper()) {
internal::CreateDesktopBackground(*wo->wallpaper(), wo->image_layout());
internal::CreateDesktopBackground(*wo->wallpaper(), wo->wallpaper_layout());
desktop_background_mode_ = BACKGROUND_IMAGE;
}
}
Expand Down
31 changes: 18 additions & 13 deletions ash/desktop_background/desktop_background_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ class UserWallpaperDelegate {
// Gets the index of user selected wallpaper.
virtual const int GetUserWallpaperIndex() = 0;

// Open the set wallpaper page in the browser.
// Opens the set wallpaper page in the browser.
virtual void OpenSetWallpaperPage() = 0;

// Returns true if user can open set wallpaper page. Only guest user returns
// false currently.
virtual bool CanOpenSetWallpaperPage() = 0;
};

// A class to listen for login and desktop background change events and set the
// corresponding default wallpaper in Aura shell.
// Loads selected desktop wallpaper from file system asynchronously and updates
// background layer if loaded successfully.
class ASH_EXPORT DesktopBackgroundController {
public:
enum BackgroundMode {
Expand All @@ -42,39 +42,44 @@ class ASH_EXPORT DesktopBackgroundController {
DesktopBackgroundController();
virtual ~DesktopBackgroundController();

// Get the desktop background mode.
// Gets the desktop background mode.
BackgroundMode desktop_background_mode() const {
return desktop_background_mode_;
}

// Load default wallpaper at |index| asynchronously and set to current
// Loads default wallpaper at |index| asynchronously and sets to current
// wallpaper after loaded.
void SetDefaultWallpaper(int index);

// Cancel the current wallpaper loading operation.
// Sets the user selected custom wallpaper. Called when user selected a file
// from file system or changed the layout of wallpaper.
void SetCustomWallpaper(const SkBitmap& wallpaper, WallpaperLayout layout);

// Cancels the current wallpaper loading operation.
void CancelPendingWallpaperOperation();

// Load logged in user wallpaper asynchronously and set to current wallpaper
// Loads logged in user wallpaper asynchronously and sets to current wallpaper
// after loaded.
void SetLoggedInUserWallpaper();

// Sets the desktop background to solid color mode and create a solid color
// Sets the desktop background to solid color mode and creates a solid color
// layout.
void SetDesktopBackgroundSolidColorMode();

private:
// An operation to asynchronously load wallpaper.
// An operation to asynchronously loads wallpaper.
class WallpaperOperation;

// Sets the desktop background to image mode and create a new background
// widget with user selected wallpaper or default wallpaper. Delete the old
// Sets the desktop background to image mode and creates a new background
// widget with user selected wallpaper or default wallpaper. Deletes the old
// widget if any.
void SetDesktopBackgroundImageMode(scoped_refptr<WallpaperOperation> wo);

// Default wallpapper loaded, set the background mode to image mode.
// Creates a new background widget and sets the background mode to image mode.
// Called after wallpaper loaded successfully.
void OnWallpaperLoadCompleted(scoped_refptr<WallpaperOperation> wo);

// Create an empty wallpaper. Some tests require a wallpaper widget is ready
// Creates an empty wallpaper. Some tests require a wallpaper widget is ready
// when running. However, the wallpaper widgets are now created asynchronously
// . If loading a real wallpaper, there are cases that these tests crash
// because the required widget is not ready. This function synchronously
Expand Down
4 changes: 2 additions & 2 deletions ash/desktop_background/desktop_background_resources.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class SkBitmap;

namespace ash {

enum ImageLayout {
enum WallpaperLayout {
CENTER,
CENTER_CROPPED,
STRETCH,
Expand All @@ -21,7 +21,7 @@ enum ImageLayout {
struct ASH_EXPORT WallpaperInfo {
int id;
int thumb_id;
ImageLayout layout;
WallpaperLayout layout;
// TODO(bshe): author member should be encoded to UTF16. We need to use i18n
// string for this member after M19.
const char* author;
Expand Down
16 changes: 9 additions & 7 deletions ash/desktop_background/desktop_background_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ static int RoundPositive(double x) {
// DesktopBackgroundView, public:

DesktopBackgroundView::DesktopBackgroundView(const SkBitmap& wallpaper,
ImageLayout layout) {
WallpaperLayout wallpaper_layout) {
wallpaper_ = wallpaper;
image_layout_ = layout;
wallpaper_layout_ = wallpaper_layout;
wallpaper_.buildMipMap(false);
}

Expand All @@ -80,7 +80,7 @@ void DesktopBackgroundView::OnPaint(gfx::Canvas* canvas) {
// streching to avoid upsampling artifacts (Note that we could tile too, but
// decided not to do this at the moment).
gfx::Rect wallpaper_rect(0, 0, wallpaper_.width(), wallpaper_.height());
if (image_layout_ == ash::CENTER_CROPPED && wallpaper_.width() > width()
if (wallpaper_layout_ == ash::CENTER_CROPPED && wallpaper_.width() > width()
&& wallpaper_.height() > height()) {
// The dimension with the smallest ratio must be cropped, the other one
// is preserved. Both are set in gfx::Size cropped_size.
Expand All @@ -105,9 +105,9 @@ void DesktopBackgroundView::OnPaint(gfx::Canvas* canvas) {
wallpaper_cropped_rect.width(), wallpaper_cropped_rect.height(),
0, 0, width(), height(),
true);
} else if (image_layout_ == ash::TILE) {
} else if (wallpaper_layout_ == ash::TILE) {
canvas->TileImageInt(wallpaper_, 0, 0, width(), height());
} else if (image_layout_ == ash::STRETCH) {
} else if (wallpaper_layout_ == ash::STRETCH) {
// This is generally not recommended as it may show artifacts.
canvas->DrawBitmapInt(wallpaper_, 0, 0, wallpaper_.width(),
wallpaper_.height(), 0, 0, width(), height(), true);
Expand All @@ -127,11 +127,13 @@ void DesktopBackgroundView::OnMouseReleased(const views::MouseEvent& event) {
Shell::GetInstance()->ShowBackgroundMenu(GetWidget(), event.location());
}

void CreateDesktopBackground(const SkBitmap& wallpaper, ImageLayout layout) {
void CreateDesktopBackground(const SkBitmap& wallpaper,
WallpaperLayout wallpaper_layout) {
views::Widget* desktop_widget = new views::Widget;
views::Widget::InitParams params(
views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
DesktopBackgroundView* view = new DesktopBackgroundView(wallpaper, layout);
DesktopBackgroundView* view = new DesktopBackgroundView(wallpaper,
wallpaper_layout);
params.delegate = view;
params.parent =
Shell::GetInstance()->GetContainer(
Expand Down
5 changes: 3 additions & 2 deletions ash/desktop_background/desktop_background_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ namespace internal {

class DesktopBackgroundView : public views::WidgetDelegateView {
public:
DesktopBackgroundView(const SkBitmap& wallpaper, ImageLayout layout);
DesktopBackgroundView(const SkBitmap& wallpaper,
WallpaperLayout wallpaper_layout);
virtual ~DesktopBackgroundView();

private:
Expand All @@ -26,7 +27,7 @@ class DesktopBackgroundView : public views::WidgetDelegateView {
virtual void OnMouseReleased(const views::MouseEvent& event) OVERRIDE;

SkBitmap wallpaper_;
ImageLayout image_layout_;
WallpaperLayout wallpaper_layout_;

DISALLOW_COPY_AND_ASSIGN(DesktopBackgroundView);
};
Expand Down
3 changes: 2 additions & 1 deletion ash/shell_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class Widget;
namespace ash {

namespace internal {
void CreateDesktopBackground(const SkBitmap& wallpaper, ImageLayout layout);
void CreateDesktopBackground(const SkBitmap& wallpaper,
WallpaperLayout wallpaper_layout);
ASH_EXPORT views::Widget* CreateStatusArea(views::View* contents);
} // namespace internal

Expand Down
12 changes: 12 additions & 0 deletions chrome/app/generated_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -13333,6 +13333,18 @@ Press any key to continue exploring.
<message name="IDS_OPTIONS_SET_WALLPAPER_RANDOM" desc="Label for option to randomly choose wallpaper at login.">
I'm feeling lucky
</message>
<message name="IDS_OPTIONS_WALLPAPER_CENTER_LAYOUT" desc="Label for option to center a customized wallpaper.">
Center
</message>
<message name="IDS_OPTIONS_WALLPAPER_CENTER_CROPPED_LAYOUT" desc="Label for option to center crop a customized wallpaper.">
Center Cropped
</message>
<message name="IDS_OPTIONS_WALLPAPER_STRETCH_LAYOUT" desc="Label for option to stretch a customized wallpaper.">
Stretch
</message>
<message name="IDS_OPTIONS_CUSTOME_WALLPAPER" desc="Label for option to choose a customized wallpaper.">
Custom...
</message>
<message name="IDS_OPTIONS_CHANGE_PICTURE" desc="In the settings tab, the text on the button to change picture for the current user.">
Change picture...
</message>
Expand Down
6 changes: 6 additions & 0 deletions chrome/browser/chromeos/login/mock_user_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,14 @@ class MockUserManager : public UserManager {
MOCK_CONST_METHOD1(GetUserDisplayEmail, std::string(const std::string&));
MOCK_METHOD2(SaveUserDefaultImageIndex, void(const std::string&, int));
MOCK_METHOD2(SaveUserImage, void(const std::string&, const SkBitmap&));
MOCK_METHOD1(SetLoggedInUserCustomWallpaperLayout,void(
ash::WallpaperLayout));
MOCK_METHOD2(SaveUserImageFromFile, void(const std::string&,
const FilePath&));
MOCK_METHOD4(SaveUserWallpaperFromFile, void(const std::string&,
const FilePath&,
ash::WallpaperLayout,
WallpaperDelegate*));
MOCK_METHOD1(SaveUserImageFromProfileImage, void(const std::string&));
MOCK_METHOD1(DownloadProfileImage, void(const std::string&));
MOCK_CONST_METHOD0(IsCurrentUserOwner, bool(void));
Expand Down
4 changes: 4 additions & 0 deletions chrome/browser/chromeos/login/user.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ void User::SetStubImage(int image_index) {
image_is_stub_ = true;
}

void User::SetWallpaperThumbnail(const SkBitmap& wallpaper_thumbnail) {
wallpaper_thumbnail_ = wallpaper_thumbnail;
}

std::string User::GetAccountName() const {
return GetUserName(email_);
}
Expand Down
7 changes: 7 additions & 0 deletions chrome/browser/chromeos/login/user.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ class User {
const SkBitmap& image() const { return image_; }
int image_index() const { return image_index_; }

// The thumbnail of user custom wallpaper.
const SkBitmap& wallpaper_thumbnail() const { return wallpaper_thumbnail_; }

// True if user image is a stub (while real image is being loaded from file).
bool image_is_stub() const { return image_is_stub_; }

Expand Down Expand Up @@ -92,6 +95,9 @@ class User {
// one of |kExternalImageIndex| or |kProfileImageIndex|.
void SetStubImage(int image_index);

// Set thumbnail of user custom wallpaper.
void SetWallpaperThumbnail(const SkBitmap& wallpaper_thumbnail);

void set_oauth_token_status(OAuthTokenStatus status) {
oauth_token_status_ = status;
}
Expand All @@ -105,6 +111,7 @@ class User {
std::string display_email_;
SkBitmap image_;
OAuthTokenStatus oauth_token_status_;
SkBitmap wallpaper_thumbnail_;

// Either index of a default image for the user, |kExternalImageIndex| or
// |kProfileImageIndex|.
Expand Down
25 changes: 19 additions & 6 deletions chrome/browser/chromeos/login/user_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "ash/desktop_background/desktop_background_resources.h"
#include "base/memory/singleton.h"
#include "chrome/browser/chromeos/login/user.h"
#include "chrome/browser/ui/webui/options2/chromeos/set_wallpaper_options_handler2.h"

class SkBitmap;
class FilePath;
Expand Down Expand Up @@ -140,7 +141,7 @@ class UserManager {
const std::string& username,
User::OAuthTokenStatus oauth_token_status) = 0;

// Save user's displayed (non-canonical) email in local state preferences.
// Saves user's displayed (non-canonical) email in local state preferences.
// Ignored If there is no such user.
virtual void SaveUserDisplayEmail(const std::string& username,
const std::string& display_email) = 0;
Expand All @@ -152,16 +153,16 @@ class UserManager {
const std::string& username) const = 0;

// Returns the index of the default wallpapers saved in local state for login
// user if it is known (was previousely set by |SaveWallpaperToLocalState|
// user if it is known (was previously set by |SaveWallpaperToLocalState|
// call). Otherwise, returns the default wallpaper index.
virtual int GetLoggedInUserWallpaperIndex() = 0;

// Set |type| and |index| to the value saved in local state for logged in
// Sets |type| and |index| to the value saved in local state for logged in
// user.
virtual void GetLoggedInUserWallpaperProperties(User::WallpaperType* type,
int* index) = 0;

// Save |type| and |index| chose by logged in user to Local State.
// Saves |type| and |index| chose by logged in user to Local State.
virtual void SaveLoggedInUserWallpaperProperties(User::WallpaperType type,
int index) = 0;

Expand All @@ -175,22 +176,34 @@ class UserManager {
virtual void SaveUserImage(const std::string& username,
const SkBitmap& image) = 0;

// Updates custom wallpaper to selected layout and saves layout to Local
// State.
virtual void SetLoggedInUserCustomWallpaperLayout(
ash::WallpaperLayout layout) = 0;

// Tries to load user image from disk; if successful, sets it for the user,
// sends LOGIN_USER_IMAGE_CHANGED notification and updates Local State.
virtual void SaveUserImageFromFile(const std::string& username,
const FilePath& path) = 0;

// Tries to load user image from disk; if successful, sets it for the user,
// and updates Local State.
virtual void SaveUserWallpaperFromFile(const std::string& username,
const FilePath& path,
ash::WallpaperLayout layout,
WallpaperDelegate* delegate) = 0;

// Sets profile image as user image for |username|, sends
// LOGIN_USER_IMAGE_CHANGED notification and updates Local State. If the user
// is not logged-in or the last |DownloadProfileImage| call has failed, a
// default grey avatar will be used until the user logs in and profile image
// is downloaded successfuly.
// is downloaded successfully.
virtual void SaveUserImageFromProfileImage(const std::string& username) = 0;

// Starts downloading the profile image for the logged-in user.
// If user's image index is |kProfileImageIndex|, newly downloaded image
// is immediately set as user's current picture.
// |reason| is an arbitraty string (used to report UMA histograms with
// |reason| is an arbitrary string (used to report UMA histograms with
// download times).
virtual void DownloadProfileImage(const std::string& reason) = 0;

Expand Down
Loading

0 comments on commit 2f5b483

Please sign in to comment.