Index: chrome/browser/chromeos/login/wallpaper_manager.cc |
diff --git a/chrome/browser/chromeos/login/wallpaper_manager.cc b/chrome/browser/chromeos/login/wallpaper_manager.cc |
index a036ca9064fdbf43a1a63d07eba4e94046c9fc0f..cea5c993553118c5497b8ae82ca4aac2c4caaad3 100644 |
--- a/chrome/browser/chromeos/login/wallpaper_manager.cc |
+++ b/chrome/browser/chromeos/login/wallpaper_manager.cc |
@@ -7,6 +7,8 @@ |
#include <numeric> |
#include <vector> |
+#include "ash/ash_switches.h" |
+#include "ash/desktop_background/desktop_background_controller.h" |
#include "ash/shell.h" |
#include "base/command_line.h" |
#include "base/debug/trace_event.h" |
@@ -112,6 +114,21 @@ bool MoveCustomWallpaperDirectory(const char* sub_dir, |
return false; |
} |
+void CheckCustomizedWallpaperFilesExist( |
+ const base::FilePath& downloaded_file, |
+ const chromeos::WallpaperManager::CustomizedWallpaperRescaledFiles* |
+ rescaled_files, |
+ chromeos::WallpaperManager::CustomizedWallpaperFilesExist* exist) { |
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
+ DCHECK(rescaled_files); |
+ DCHECK(exist); |
+ exist->dowloaded = base::PathExists(downloaded_file); |
+ exist->rescaled_original = |
+ base::PathExists(rescaled_files->path_rescaled_original); |
+ exist->rescaled_small = base::PathExists(rescaled_files->path_rescaled_small); |
+ exist->rescaled_large = base::PathExists(rescaled_files->path_rescaled_large); |
+} |
+ |
} // namespace |
namespace chromeos { |
Daniel Erat
2014/03/25 21:58:29
nit: put the above anonymous namespace within the
Alexander Alekseev
2014/03/27 00:28:37
Done.
|
@@ -121,6 +138,9 @@ const char kWallpaperSequenceTokenName[] = "wallpaper-sequence"; |
const char kSmallWallpaperSuffix[] = "_small"; |
const char kLargeWallpaperSuffix[] = "_large"; |
+// This is used for customized default wallpaper only. |
+const char kOriginalWallpaperSuffix[] = "_original"; |
+ |
const char kSmallWallpaperSubDir[] = "small"; |
const char kLargeWallpaperSubDir[] = "large"; |
const char kOriginalWallpaperSubDir[] = "original"; |
@@ -266,6 +286,27 @@ void WallpaperManager::PendingWallpaper::OnWallpaperSet() { |
// WallpaperManager, public: --------------------------------------------------- |
+WallpaperManager::CustomizedWallpaperRescaledFiles:: |
+ CustomizedWallpaperRescaledFiles( |
+ const base::FilePath& path_rescaled_original, |
+ const base::FilePath& path_rescaled_small, |
+ const base::FilePath& path_rescaled_large) |
+ : path_rescaled_original(path_rescaled_original), |
+ path_rescaled_small(path_rescaled_small), |
+ path_rescaled_large(path_rescaled_large) { |
+} |
+ |
+WallpaperManager::CustomizedWallpaperFilesExist::CustomizedWallpaperFilesExist() |
+ : dowloaded(false), |
+ rescaled_original(false), |
+ rescaled_small(false), |
+ rescaled_large(false) { |
+} |
+ |
+bool WallpaperManager::CustomizedWallpaperFilesExist::AllRescaledExist() const { |
+ return rescaled_original && rescaled_small && rescaled_large; |
+} |
+ |
// TestApi. For testing purpose |
WallpaperManager::TestApi::TestApi(WallpaperManager* wallpaper_manager) |
: wallpaper_manager_(wallpaper_manager) { |
@@ -306,6 +347,8 @@ WallpaperManager::WallpaperManager() |
should_cache_wallpaper_(false), |
weak_factory_(this), |
pending_inactive_(NULL) { |
+ SetDefaultWallpaperPathFromCommandLine( |
+ base::CommandLine::ForCurrentProcess()); |
registrar_.Add(this, |
chrome::NOTIFICATION_LOGIN_USER_CHANGED, |
content::NotificationService::AllSources()); |
@@ -432,6 +475,13 @@ void WallpaperManager::InitializeWallpaper() { |
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
UserManager* user_manager = UserManager::Get(); |
+ // Apply device customization. |
+ if (ShouldUseCustomizedDefaultWallpaper()) { |
+ SetDefaultWallpaperPath( |
+ GetCustomizedWallpaperDefaultRescaledFileName(kSmallWallpaperSuffix), |
+ GetCustomizedWallpaperDefaultRescaledFileName(kLargeWallpaperSuffix)); |
+ } |
+ |
CommandLine* command_line = GetComandLine(); |
if (command_line->HasSwitch(chromeos::switches::kGuestSession)) { |
// Guest wallpaper should be initialized when guest login. |
@@ -570,7 +620,7 @@ bool WallpaperManager::ResizeWallpaper( |
return true; |
} |
-void WallpaperManager::ResizeAndSaveWallpaper(const UserImage& wallpaper, |
+bool WallpaperManager::ResizeAndSaveWallpaper(const UserImage& wallpaper, |
const base::FilePath& path, |
ash::WallpaperLayout layout, |
int preferred_width, |
@@ -579,15 +629,15 @@ void WallpaperManager::ResizeAndSaveWallpaper(const UserImage& wallpaper, |
// TODO(bshe): Generates cropped custom wallpaper for CENTER layout. |
if (base::PathExists(path)) |
base::DeleteFile(path, false); |
- return; |
+ return false; |
} |
scoped_refptr<base::RefCountedBytes> data; |
if (ResizeWallpaper(wallpaper, layout, preferred_width, preferred_height, |
&data)) { |
- SaveWallpaperInternal(path, |
- reinterpret_cast<const char*>(data->front()), |
- data->size()); |
+ return SaveWallpaperInternal( |
+ path, reinterpret_cast<const char*>(data->front()), data->size()); |
} |
+ return false; |
} |
bool WallpaperManager::IsPolicyControlled(const std::string& user_id) const { |
@@ -742,9 +792,22 @@ void WallpaperManager::DoSetDefaultWallpaper( |
// up the tests. |
if (!ash::Shell::HasInstance()) |
return; |
- if (ash::Shell::GetInstance()->desktop_background_controller()-> |
- SetDefaultWallpaper(UserManager::Get()->IsLoggedInAsGuest())) |
- loaded_wallpapers_++; |
+ |
+ const bool is_guest = UserManager::Get()->IsLoggedInAsGuest(); |
+ |
+ if (is_guest) { |
+ loaded_wallpapers_ += |
+ ash::Shell::GetInstance() |
+ ->desktop_background_controller() |
+ ->SetDefaultWallpaper(guest_default_small_wallpaper_file_, |
+ guest_default_large_wallpaper_file_); |
+ } else { |
+ loaded_wallpapers_ += |
+ ash::Shell::GetInstance() |
+ ->desktop_background_controller() |
+ ->SetDefaultWallpaper(default_small_wallpaper_file_, |
+ default_large_wallpaper_file_); |
+ } |
} |
void WallpaperManager::InitInitialUserWallpaper(const std::string& user_id, |
@@ -1070,6 +1133,12 @@ void WallpaperManager::EnsureCustomWallpaperDirectories( |
base::CreateDirectory(dir); |
} |
+void WallpaperManager::set_command_line_for_testing( |
+ base::CommandLine* command_line) { |
+ command_line_for_testing_ = command_line; |
+ SetDefaultWallpaperPathFromCommandLine(command_line); |
+} |
+ |
CommandLine* WallpaperManager::GetComandLine() { |
CommandLine* command_line = command_line_for_testing_ ? |
command_line_for_testing_ : CommandLine::ForCurrentProcess(); |
@@ -1378,11 +1447,11 @@ void WallpaperManager::RecordUma(User::WallpaperType type, int index) const { |
User::WALLPAPER_TYPE_COUNT); |
} |
-void WallpaperManager::SaveWallpaperInternal(const base::FilePath& path, |
+bool WallpaperManager::SaveWallpaperInternal(const base::FilePath& path, |
const char* data, |
int size) const { |
int written_bytes = base::WriteFile(path, data, size); |
- DCHECK(written_bytes == size); |
+ return written_bytes == size; |
} |
void WallpaperManager::StartLoad(const std::string& user_id, |
@@ -1455,4 +1524,208 @@ WallpaperManager::PendingWallpaper* WallpaperManager::GetPendingWallpaper( |
return pending_inactive_; |
} |
+// static |
+base::FilePath WallpaperManager::GetCustomizedWallpaperDefaultRescaledFileName( |
+ const char* suffix) { |
+ const base::FilePath default_downloaded_file_name = |
+ ServicesCustomizationDocument::GetCustomizedWallpaperDownloadedFileName(); |
+ const base::FilePath default_cache_dir = |
+ ServicesCustomizationDocument::GetCustomizedWallpaperCacheDir(); |
+ if (default_downloaded_file_name.empty() || default_cache_dir.empty()) |
+ return base::FilePath(); |
+ return default_cache_dir.Append( |
+ default_downloaded_file_name.BaseName().value() + suffix); |
+} |
+ |
+void WallpaperManager::SetCustomizedDefaultWallpaper( |
+ const GURL& wallpaper_url, |
+ const base::FilePath& downloaded_file, |
+ const base::FilePath& resized_directory) { |
+ // Should fail if this ever happens in tests. |
+ DCHECK(wallpaper_url.is_valid() || wallpaper_url.is_empty()); |
+ if (!wallpaper_url.is_valid()) { |
+ if (!wallpaper_url.is_empty()) { |
+ LOG(WARNING) << "Invalid Customized Wallpaper URL."; |
+ } |
+ return; |
+ } |
+ std::string downloaded_file_name = downloaded_file.BaseName().value(); |
+ scoped_ptr<CustomizedWallpaperRescaledFiles> rescaled_files( |
+ new CustomizedWallpaperRescaledFiles( |
+ resized_directory.Append(downloaded_file_name + |
+ kOriginalWallpaperSuffix), |
+ resized_directory.Append(downloaded_file_name + |
+ kSmallWallpaperSuffix), |
+ resized_directory.Append(downloaded_file_name + |
+ kLargeWallpaperSuffix))); |
+ scoped_ptr<CustomizedWallpaperFilesExist> exist( |
+ new CustomizedWallpaperFilesExist); |
+ |
+ base::Closure check_file_exists = |
+ base::Bind(&CheckCustomizedWallpaperFilesExist, |
+ downloaded_file, |
+ base::Unretained(rescaled_files.get()), |
+ base::Unretained(exist.get())); |
+ base::Closure on_checked_closure = |
+ base::Bind(&WallpaperManager::SetCustomizedDefaultWallpaperAfterCheck, |
+ weak_factory_.GetWeakPtr(), |
+ wallpaper_url, |
+ downloaded_file, |
+ base::Passed(rescaled_files.Pass()), |
+ base::Passed(exist.Pass())); |
+ if (!content::BrowserThread::PostBlockingPoolTaskAndReply( |
+ FROM_HERE, check_file_exists, on_checked_closure)) { |
+ LOG(WARNING) << "Failed to start check CheckCustomizedWallpaperFilesExist."; |
+ } |
+} |
+ |
+void WallpaperManager::ResizeCustomizedDefaultWallpaper( |
+ scoped_ptr<gfx::ImageSkia> image, |
+ const UserImage::RawImage& raw_image, |
+ const CustomizedWallpaperRescaledFiles* rescaled_files, |
+ bool* success) { |
+ DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread( |
+ sequence_token_)); |
+ UserImage wallpaper(*image.get(), raw_image); |
+ |
+ *success = false; |
+ |
+ // Re-encode orginal file to jpeg format and save the result for debug |
Daniel Erat
2014/03/25 21:58:29
i don't understand the "for debug" here. how likel
Alexander Alekseev
2014/03/27 00:28:37
No, I never compare the results. It just seems use
|
+ // (if downloaded file was corrupted, path_rescaled_original will be |
+ // different from the downloaded). |
+ *success |= ResizeAndSaveWallpaper(wallpaper, |
Daniel Erat
2014/03/25 21:58:29
shouldn't all of the resizes need to succeed for t
Alexander Alekseev
2014/03/27 00:28:37
Done.
|
+ rescaled_files->path_rescaled_original, |
+ ash::WALLPAPER_LAYOUT_STRETCH, |
+ wallpaper.image().width(), |
+ wallpaper.image().height()); |
+ |
+ *success |= ResizeAndSaveWallpaper(wallpaper, |
+ rescaled_files->path_rescaled_small, |
+ ash::WALLPAPER_LAYOUT_STRETCH, |
+ ash::kSmallWallpaperMaxWidth, |
+ ash::kSmallWallpaperMaxHeight); |
+ |
+ *success |= ResizeAndSaveWallpaper(wallpaper, |
+ rescaled_files->path_rescaled_large, |
+ ash::WALLPAPER_LAYOUT_STRETCH, |
+ ash::kLargeWallpaperMaxWidth, |
+ ash::kLargeWallpaperMaxHeight); |
+} |
+ |
+void WallpaperManager::OnCustomizedDefaultWallpaperResized( |
+ const GURL& wallpaper_url, |
+ scoped_ptr<CustomizedWallpaperRescaledFiles> rescaled_files, |
+ scoped_ptr<bool> success) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ DCHECK(rescaled_files.get()); |
+ DCHECK(success.get()); |
+ if (!*success) { |
+ LOG(WARNING) << "Failed to save Resized Customized Default Wallpaper"; |
+ return; |
+ } |
+ PrefService* prefService = g_browser_process->local_state(); |
Daniel Erat
2014/03/25 21:58:29
s/prefService/pref_service/
Alexander Alekseev
2014/03/27 00:28:37
Done.
|
+ prefService->SetString(prefs::kCustomizationDefaultWallpaperURL, |
+ wallpaper_url.spec()); |
+ SetDefaultWallpaperPath(rescaled_files->path_rescaled_small, |
+ rescaled_files->path_rescaled_large); |
+ VLOG(1) << "Customized Default Wallpaper applied."; |
+} |
+ |
+void WallpaperManager::OnCustomizedDefaultWallpaperDecoded( |
+ const GURL& wallpaper_url, |
+ scoped_ptr<CustomizedWallpaperRescaledFiles> rescaled_files, |
+ const UserImage& wallpaper) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ |
+ // If decoded wallpaper is empty, we have probably failed to decode the file. |
+ if (wallpaper.image().isNull()) { |
+ LOG(WARNING) << "Failed to decode customized wallpaper."; |
+ return; |
+ } |
+ |
+ wallpaper.image().EnsureRepsForSupportedScales(); |
+ scoped_ptr<gfx::ImageSkia> deep_copy(wallpaper.image().DeepCopy()); |
+ |
+ scoped_ptr<bool> success(new bool(false)); |
+ |
+ // TODO(bshe): This may break if RawImage becomes RefCountedMemory. |
+ base::Closure resize_closure = |
+ base::Bind(&WallpaperManager::ResizeCustomizedDefaultWallpaper, |
+ base::Unretained(this), |
+ base::Passed(&deep_copy), |
+ wallpaper.raw_image(), |
+ base::Unretained(rescaled_files.get()), |
+ base::Unretained(success.get())); |
+ base::Closure on_resized_closure = |
+ base::Bind(&WallpaperManager::OnCustomizedDefaultWallpaperResized, |
+ weak_factory_.GetWeakPtr(), |
+ wallpaper_url, |
+ base::Passed(rescaled_files.Pass()), |
+ base::Passed(success.Pass())); |
+ |
+ if (!task_runner_->PostTaskAndReply( |
+ FROM_HERE, resize_closure, on_resized_closure)) { |
+ LOG(WARNING) << "Failed to start Customized Wallpaper resize."; |
+ } |
+} |
+ |
+void WallpaperManager::SetCustomizedDefaultWallpaperAfterCheck( |
+ const GURL& wallpaper_url, |
+ const base::FilePath& downloaded_file, |
+ scoped_ptr<CustomizedWallpaperRescaledFiles> rescaled_files, |
+ scoped_ptr<CustomizedWallpaperFilesExist> exist) { |
+ PrefService* prefService = g_browser_process->local_state(); |
Daniel Erat
2014/03/25 21:58:29
s/prefService/pref_service/
Alexander Alekseev
2014/03/27 00:28:37
Done.
|
+ |
+ std::string current_url = |
+ prefService->GetString(prefs::kCustomizationDefaultWallpaperURL); |
+ if ((current_url != wallpaper_url.spec()) || (!exist->AllRescaledExist())) { |
Daniel Erat
2014/03/25 21:58:29
nit: remove unnecessary parentheses
Alexander Alekseev
2014/03/27 00:28:37
Done.
|
+ DCHECK(exist->dowloaded); |
+ // Need rescale |
+ wallpaper_loader_->Start( |
+ downloaded_file.value(), |
+ 0, // Do not crop. |
+ base::Bind(&WallpaperManager::OnCustomizedDefaultWallpaperDecoded, |
+ weak_factory_.GetWeakPtr(), |
+ wallpaper_url, |
+ base::Passed(rescaled_files.Pass()))); |
+ } else { |
+ SetDefaultWallpaperPath(rescaled_files->path_rescaled_small, |
+ rescaled_files->path_rescaled_large); |
+ } |
+} |
+ |
+// static |
+bool WallpaperManager::ShouldUseCustomizedDefaultWallpaper() { |
+ PrefService* prefService = g_browser_process->local_state(); |
Daniel Erat
2014/03/25 21:58:29
s/prefService/pref_service/
Alexander Alekseev
2014/03/27 00:28:37
Done.
|
+ |
+ return !(prefService->FindPreference(prefs::kCustomizationDefaultWallpaperURL) |
+ ->IsDefaultValue()); |
+} |
+ |
+void WallpaperManager::SetDefaultWallpaperPathFromCommandLine( |
+ base::CommandLine* command_line) { |
+ default_small_wallpaper_file_ = command_line->GetSwitchValuePath( |
+ ash::switches::kAshDefaultWallpaperSmall); |
+ default_large_wallpaper_file_ = command_line->GetSwitchValuePath( |
+ ash::switches::kAshDefaultWallpaperLarge); |
+ guest_default_small_wallpaper_file_ = |
+ command_line->GetSwitchValuePath(ash::switches::kAshGuestWallpaperSmall); |
+ guest_default_large_wallpaper_file_ = |
+ command_line->GetSwitchValuePath(ash::switches::kAshGuestWallpaperLarge); |
+} |
+ |
+void WallpaperManager::SetDefaultWallpaperPath( |
+ const base::FilePath& default_small_wallpaper_file, |
+ const base::FilePath& default_large_wallpaper_file) { |
+ ash::DesktopBackgroundController* dbc = |
+ ash::Shell::GetInstance()->desktop_background_controller(); |
+ const bool need_reset = dbc->IsUsingDefaultWallpaper(); |
Daniel Erat
2014/03/25 21:58:29
nit: you can just inline this below now:
if (db
Alexander Alekseev
2014/03/27 00:28:37
Done.
|
+ default_small_wallpaper_file_ = default_small_wallpaper_file; |
+ default_large_wallpaper_file_ = default_large_wallpaper_file; |
+ if (need_reset) { |
+ dbc->SetDefaultWallpaper(default_small_wallpaper_file, |
+ default_large_wallpaper_file); |
+ } |
+} |
+ |
} // namespace chromeos |