#ifndef CHROME_BROWSER_CHROMEOS_LOGIN_WALLPAPER_MANAGER_H_
#define CHROME_BROWSER_CHROMEOS_LOGIN_WALLPAPER_MANAGER_H_
#include <deque>
#include <string>
#include <vector>
#include "ash/desktop_background/desktop_background_controller.h"
#include "base/files/file_path.h"
#include "base/memory/ref_counted_memory.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/threading/sequenced_worker_pool.h"
#include "base/time/time.h"
#include "chrome/browser/chromeos/login/user.h"
#include "chrome/browser/chromeos/login/user_image.h"
#include "chrome/browser/chromeos/login/user_image_loader.h"
#include "chrome/browser/chromeos/settings/cros_settings.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "third_party/icu/source/i18n/unicode/timezone.h"
#include "ui/gfx/image/image_skia.h"
class PrefRegistrySimple;
namespace base {
class CommandLine;
class SequencedTaskRunner;
}
namespace chromeos {
struct WallpaperInfo {
std::string file;
ash::WallpaperLayout layout;
User::WallpaperType type;
base::Time date;
bool operator==(const WallpaperInfo& other) {
return (file == other.file) && (layout == other.layout) &&
(type == other.type);
}
};
class MovableOnDestroyCallback;
typedef scoped_ptr<MovableOnDestroyCallback> MovableOnDestroyCallbackHolder;
class WallpaperManagerBrowserTest;
class UserImage;
extern const char kWallpaperSequenceTokenName[];
extern const char kSmallWallpaperSuffix[];
extern const char kLargeWallpaperSuffix[];
extern const char kSmallWallpaperSubDir[];
extern const char kLargeWallpaperSubDir[];
extern const char kOriginalWallpaperSubDir[];
extern const char kThumbnailWallpaperSubDir[];
class WallpaperManager: public content::NotificationObserver {
public:
class TestApi {
public:
explicit TestApi(WallpaperManager* wallpaper_manager);
virtual ~TestApi();
base::FilePath current_wallpaper_path();
bool GetWallpaperFromCache(const std::string& user_id,
gfx::ImageSkia* image);
void SetWallpaperCache(const std::string& user_id,
const gfx::ImageSkia& image);
void ClearDisposableWallpaperCache();
private:
WallpaperManager* wallpaper_manager_;
DISALLOW_COPY_AND_ASSIGN(TestApi);
};
class Observer {
public:
virtual ~Observer() {}
virtual void OnWallpaperAnimationFinished(const std::string& user_id) = 0;
};
class PendingWallpaper : public base::RefCountedThreadSafe<PendingWallpaper> {
public:
PendingWallpaper(const base::TimeDelta delay, const std::string& user_id);
void ResetSetWallpaperImage(const gfx::ImageSkia& user_wallpaper,
const WallpaperInfo& info);
void ResetLoadWallpaper(const WallpaperInfo& info);
void ResetSetCustomWallpaper(const WallpaperInfo& info,
const base::FilePath& wallpaper_path);
void ResetSetDefaultWallpaper();
private:
friend class base::RefCountedThreadSafe<PendingWallpaper>;
~PendingWallpaper();
void SetMode(const gfx::ImageSkia& user_wallpaper,
const WallpaperInfo& info,
const base::FilePath& wallpaper_path,
const bool is_default);
void ProcessRequest();
void OnWallpaperSet();
std::string user_id_;
WallpaperInfo info_;
gfx::ImageSkia user_wallpaper_;
base::FilePath wallpaper_path_;
bool default_;
MovableOnDestroyCallbackHolder on_finish_;
base::OneShotTimer<WallpaperManager::PendingWallpaper> timer;
base::Time started_load_at_;
DISALLOW_COPY_AND_ASSIGN(PendingWallpaper);
};
static WallpaperManager* Get();
WallpaperManager();
virtual ~WallpaperManager();
void set_command_line_for_testing(base::CommandLine* command_line) {
command_line_for_testing_ = command_line;
}
void Shutdown();
static void RegisterPrefs(PrefRegistrySimple* registry);
void AddObservers();
void EnsureLoggedInUserWallpaperLoaded();
void ClearDisposableWallpaperCache();
base::FilePath GetCustomWallpaperPath(const char* sub_dir,
const std::string& user_id_hash,
const std::string& file) const;
base::FilePath GetOriginalWallpaperPathForUser(const std::string& user_id);
bool GetLoggedInUserWallpaperInfo(WallpaperInfo* info);
void InitializeWallpaper();
virtual void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) OVERRIDE;
void RemoveUserWallpaperInfo(const std::string& user_id);
bool ResizeWallpaper(const UserImage& wallpaper,
ash::WallpaperLayout layout,
int preferred_width,
int preferred_height,
scoped_refptr<base::RefCountedBytes>* output) const;
void ResizeAndSaveWallpaper(const UserImage& wallpaper,
const base::FilePath& path,
ash::WallpaperLayout layout,
int preferred_width,
int preferred_height) const;
void SetCustomWallpaper(const std::string& user_id,
const std::string& user_id_hash,
const std::string& file,
ash::WallpaperLayout layout,
User::WallpaperType type,
const UserImage& wallpaper,
bool update_wallpaper);
void SetDefaultWallpaperNow(const std::string& user_id);
void SetDefaultWallpaperDelayed(const std::string& user_id);
void InitInitialUserWallpaper(const std::string& user_id,
bool is_persistent);
void SetUserWallpaperInfo(const std::string& user_id,
const WallpaperInfo& info,
bool is_persistent);
void SetLastSelectedUser(const std::string& last_selected_user);
void SetUserWallpaperNow(const std::string& user_id);
void SetUserWallpaperDelayed(const std::string& user_id);
void SetWallpaperFromImageSkia(const std::string& user_id,
const gfx::ImageSkia& wallpaper,
ash::WallpaperLayout layout,
bool update_wallpaper);
void UpdateWallpaper(bool clear_cache);
void AddObserver(Observer* observer);
void RemoveObserver(Observer* observer);
bool IsPolicyControlled(const std::string& user_id) const;
void OnPolicySet(const std::string& policy, const std::string& user_id);
void OnPolicyCleared(const std::string& policy, const std::string& user_id);
void OnPolicyFetched(const std::string& policy,
const std::string& user_id,
scoped_ptr<std::string> data);
private:
friend class TestApi;
friend class WallpaperManagerBrowserTest;
friend class WallpaperManagerPolicyTest;
typedef std::map<std::string, gfx::ImageSkia> CustomWallpaperMap;
void SetPolicyControlledWallpaper(const std::string& user_id,
const UserImage& wallpaper);
bool GetWallpaperFromCache(const std::string& user_id,
gfx::ImageSkia* wallpaper);
int loaded_wallpapers() const { return loaded_wallpapers_; }
void CacheUsersWallpapers();
void CacheUserWallpaper(const std::string& user_id);
void ClearObsoleteWallpaperPrefs();
void DeleteAllExcept(const base::FilePath& path);
void DeleteWallpaperInList(const std::vector<base::FilePath>& file_list);
void DeleteUserWallpapers(const std::string& user_id,
const std::string& path_to_file);
void EnsureCustomWallpaperDirectories(const std::string& user_id_hash);
base::CommandLine* GetComandLine();
void InitializeRegisteredDeviceWallpaper();
void LoadWallpaper(const std::string& user_id,
const WallpaperInfo& info,
bool update_wallpaper,
MovableOnDestroyCallbackHolder on_finish);
void MoveCustomWallpapersOnWorker(const std::string& user_id,
const std::string& user_id_hash);
void MoveCustomWallpapersSuccess(const std::string& user_id,
const std::string& user_id_hash);
void MoveLoggedInUserCustomWallpaper();
void GetCustomWallpaperInternal(const std::string& user_id,
const WallpaperInfo& info,
const base::FilePath& wallpaper_path,
bool update_wallpaper,
MovableOnDestroyCallbackHolder on_finish);
bool GetUserWallpaperInfo(const std::string& user_id,
WallpaperInfo* info) const;
void OnWallpaperDecoded(const std::string& user_id,
ash::WallpaperLayout layout,
bool update_wallpaper,
MovableOnDestroyCallbackHolder on_finish,
const UserImage& wallpaper);
void ProcessCustomWallpaper(const std::string& user_id_hash,
bool persistent,
const WallpaperInfo& info,
scoped_ptr<gfx::ImageSkia> image,
const UserImage::RawImage& raw_image);
void RecordUma(User::WallpaperType type, int index) const;
void SaveCustomWallpaper(const std::string& user_id_hash,
const base::FilePath& path,
ash::WallpaperLayout layout,
const UserImage& wallpaper);
void SaveWallpaperInternal(const base::FilePath& path, const char* data,
int size) const;
void ScheduleSetUserWallpaper(const std::string& user_id, bool delayed);
void DoSetDefaultWallpaper(
const std::string& user_id,
MovableOnDestroyCallbackHolder on_finish);
void StartLoad(const std::string& user_id,
const WallpaperInfo& info,
bool update_wallpaper,
const base::FilePath& wallpaper_path,
MovableOnDestroyCallbackHolder on_finish);
void SaveLastLoadTime(const base::TimeDelta elapsed);
void NotifyAnimationFinished();
PendingWallpaper* GetPendingWallpaper(const std::string& user_id,
bool delayed);
base::TimeDelta GetWallpaperLoadDelay() const;
int loaded_wallpapers_;
base::SequencedWorkerPool::SequenceToken sequence_token_;
scoped_refptr<base::SequencedTaskRunner> task_runner_;
base::FilePath current_wallpaper_path_;
scoped_refptr<UserImageLoader> wallpaper_loader_;
WallpaperInfo current_user_wallpaper_info_;
base::CommandLine* command_line_for_testing_;
CustomWallpaperMap wallpaper_cache_;
std::string last_selected_user_;
bool should_cache_wallpaper_;
scoped_ptr<CrosSettings::ObserverSubscription>
show_user_name_on_signin_subscription_;
base::WeakPtrFactory<WallpaperManager> weak_factory_;
content::NotificationRegistrar registrar_;
ObserverList<Observer> observers_;
base::Time last_load_finished_at_;
std::deque<base::TimeDelta> last_load_times_;
PendingWallpaper* pending_inactive_;
typedef std::vector<scoped_refptr<PendingWallpaper> > PendingList;
PendingList loading_;
DISALLOW_COPY_AND_ASSIGN(WallpaperManager);
};
}
#endif