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

Side by Side Diff: chrome/browser/chromeos/login/wallpaper_manager.cc

Issue 12334030: New custom wallpaper picker UI (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Use FileSystem to store user custom wallpaper, so only one custom wallpaper is saved in shared dire… Created 7 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/chromeos/login/wallpaper_manager.h" 5 #include "chrome/browser/chromeos/login/wallpaper_manager.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "ash/shell.h" 9 #include "ash/shell.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after
321 321
322 void WallpaperManager::RemoveUserWallpaperInfo(const std::string& email) { 322 void WallpaperManager::RemoveUserWallpaperInfo(const std::string& email) {
323 PrefService* prefs = g_browser_process->local_state(); 323 PrefService* prefs = g_browser_process->local_state();
324 DictionaryPrefUpdate prefs_wallpapers_info_update(prefs, 324 DictionaryPrefUpdate prefs_wallpapers_info_update(prefs,
325 prefs::kUsersWallpaperInfo); 325 prefs::kUsersWallpaperInfo);
326 prefs_wallpapers_info_update->RemoveWithoutPathExpansion(email, NULL); 326 prefs_wallpapers_info_update->RemoveWithoutPathExpansion(email, NULL);
327 327
328 DeleteUserWallpapers(email); 328 DeleteUserWallpapers(email);
329 } 329 }
330 330
331 void WallpaperManager::ResizeAndSaveWallpaper(const UserImage& wallpaper, 331 std::string WallpaperManager::ResizeWallpaper(const UserImage& wallpaper,
332 const base::FilePath& path,
333 ash::WallpaperLayout layout, 332 ash::WallpaperLayout layout,
334 int preferred_width, 333 int preferred_width,
335 int preferred_height) { 334 int preferred_height) {
336 DCHECK(BrowserThread::GetBlockingPool()-> 335 DCHECK(BrowserThread::GetBlockingPool()->
337 IsRunningSequenceOnCurrentThread(sequence_token_)); 336 IsRunningSequenceOnCurrentThread(sequence_token_));
338 int width = wallpaper.image().width(); 337 int width = wallpaper.image().width();
339 int height = wallpaper.image().height(); 338 int height = wallpaper.image().height();
340 int resized_width; 339 int resized_width;
341 int resized_height; 340 int resized_height;
342 341
343 if (layout == ash::WALLPAPER_LAYOUT_CENTER_CROPPED) { 342 if (layout == ash::WALLPAPER_LAYOUT_CENTER_CROPPED) {
344 // Do not resize custom wallpaper if it is smaller than preferred size. 343 // Do not resize custom wallpaper if it is smaller than preferred size.
345 if (!(width > preferred_width && height > preferred_height)) 344 if (!(width > preferred_width && height > preferred_height))
346 return; 345 return std::string();
347 346
348 double horizontal_ratio = static_cast<double>(preferred_width) / width; 347 double horizontal_ratio = static_cast<double>(preferred_width) / width;
349 double vertical_ratio = static_cast<double>(preferred_height) / height; 348 double vertical_ratio = static_cast<double>(preferred_height) / height;
350 if (vertical_ratio > horizontal_ratio) { 349 if (vertical_ratio > horizontal_ratio) {
351 resized_width = 350 resized_width =
352 RoundPositive(static_cast<double>(width) * vertical_ratio); 351 RoundPositive(static_cast<double>(width) * vertical_ratio);
353 resized_height = preferred_height; 352 resized_height = preferred_height;
354 } else { 353 } else {
355 resized_width = preferred_width; 354 resized_width = preferred_width;
356 resized_height = 355 resized_height =
357 RoundPositive(static_cast<double>(height) * horizontal_ratio); 356 RoundPositive(static_cast<double>(height) * horizontal_ratio);
358 } 357 }
359 } else if (layout == ash::WALLPAPER_LAYOUT_STRETCH) { 358 } else if (layout == ash::WALLPAPER_LAYOUT_STRETCH) {
360 resized_width = preferred_width; 359 resized_width = preferred_width;
361 resized_height = preferred_height; 360 resized_height = preferred_height;
362 } else { 361 } else {
363 // TODO(bshe): Generates cropped custom wallpaper for CENTER layout. 362 resized_width = width;
364 if (file_util::PathExists(path)) 363 resized_height = height;
365 file_util::Delete(path, false);
366 return;
367 } 364 }
368 365
369 gfx::ImageSkia resized_image = gfx::ImageSkiaOperations::CreateResizedImage( 366 gfx::ImageSkia resized_image = gfx::ImageSkiaOperations::CreateResizedImage(
370 wallpaper.image(), 367 wallpaper.image(),
371 skia::ImageOperations::RESIZE_LANCZOS3, 368 skia::ImageOperations::RESIZE_LANCZOS3,
372 gfx::Size(resized_width, resized_height)); 369 gfx::Size(resized_width, resized_height));
373 370
374 SkBitmap image = *(resized_image.bitmap()); 371 SkBitmap image = *(resized_image.bitmap());
375 scoped_refptr<base::RefCountedBytes> data = new base::RefCountedBytes(); 372 scoped_refptr<base::RefCountedBytes> data = new base::RefCountedBytes();
376 SkAutoLockPixels lock_input(image); 373 SkAutoLockPixels lock_input(image);
377 gfx::JPEGCodec::Encode( 374 gfx::JPEGCodec::Encode(
378 reinterpret_cast<unsigned char*>(image.getAddr32(0, 0)), 375 reinterpret_cast<unsigned char*>(image.getAddr32(0, 0)),
379 gfx::JPEGCodec::FORMAT_SkBitmap, 376 gfx::JPEGCodec::FORMAT_SkBitmap,
380 image.width(), 377 image.width(),
381 image.height(), 378 image.height(),
382 image.width() * image.bytesPerPixel(), 379 image.width() * image.bytesPerPixel(),
383 kDefaultEncodingQuality, &data->data()); 380 kDefaultEncodingQuality, &data->data());
384 SaveWallpaperInternal(path, 381 return std::string(reinterpret_cast<const char*>(data->front()),
Nikita (slow) 2013/03/06 16:51:17 Is this the only way to return resized Wallpaper?
flackr 2013/03/06 18:22:24 But we don't know in advance the size of the resiz
bshe 2013/03/07 05:34:35 I changed the signature according to nikita's sugg
385 reinterpret_cast<const char*>(data->front()), 382 data->size());
386 data->size()); 383 }
384
385 void WallpaperManager::ResizeAndSaveWallpaper(const UserImage& wallpaper,
386 const base::FilePath& path,
387 ash::WallpaperLayout layout,
388 int preferred_width,
389 int preferred_height) {
390 if (layout == ash::WALLPAPER_LAYOUT_CENTER) {
391 // TODO(bshe): Generates cropped custom wallpaper for CENTER layout.
392 if (file_util::PathExists(path))
393 file_util::Delete(path, false);
394 return;
395 }
396 std::string resized_wallpaper_string =
397 ResizeWallpaper(wallpaper, layout, preferred_width, preferred_height);
398 if (!resized_wallpaper_string.empty()) {
399 SaveWallpaperInternal(path,
400 resized_wallpaper_string.c_str(),
401 resized_wallpaper_string.size());
402 }
387 } 403 }
388 404
389 void WallpaperManager::RestartTimer() { 405 void WallpaperManager::RestartTimer() {
390 timer_.Stop(); 406 timer_.Stop();
391 // Determine the next update time as the earliest local midnight after now. 407 // Determine the next update time as the earliest local midnight after now.
392 // Note that this may be more than kWallpaperUpdateIntervalSec seconds in the 408 // Note that this may be more than kWallpaperUpdateIntervalSec seconds in the
393 // future due to DST. 409 // future due to DST.
394 base::Time now = base::Time::Now(); 410 base::Time now = base::Time::Now();
395 base::TimeDelta update_interval = base::TimeDelta::FromSeconds( 411 base::TimeDelta update_interval = base::TimeDelta::FromSeconds(
396 kWallpaperUpdateIntervalSec); 412 kWallpaperUpdateIntervalSec);
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
554 gfx::ImageSkia user_wallpaper; 570 gfx::ImageSkia user_wallpaper;
555 if (GetWallpaperFromCache(email, &user_wallpaper)) { 571 if (GetWallpaperFromCache(email, &user_wallpaper)) {
556 ash::Shell::GetInstance()->desktop_background_controller()-> 572 ash::Shell::GetInstance()->desktop_background_controller()->
557 SetCustomWallpaper(user_wallpaper, info.layout); 573 SetCustomWallpaper(user_wallpaper, info.layout);
558 } else { 574 } else {
559 if (info.type == User::CUSTOMIZED) { 575 if (info.type == User::CUSTOMIZED) {
560 ash::WallpaperResolution resolution = ash::Shell::GetInstance()-> 576 ash::WallpaperResolution resolution = ash::Shell::GetInstance()->
561 desktop_background_controller()->GetAppropriateResolution(); 577 desktop_background_controller()->GetAppropriateResolution();
562 const char* sub_dir = (resolution == ash::WALLPAPER_RESOLUTION_SMALL) ? 578 const char* sub_dir = (resolution == ash::WALLPAPER_RESOLUTION_SMALL) ?
563 kSmallWallpaperSubDir : kLargeWallpaperSubDir; 579 kSmallWallpaperSubDir : kLargeWallpaperSubDir;
580 // Wallpaper is not resized when layout is ash::WALLPAPER_LAYOUT_CENTER.
581 // Original wallpaper should be used in this case.
582 // TODO(bshe): Generates cropped custom wallpaper for CENTER layout.
583 if (info.layout == ash::WALLPAPER_LAYOUT_CENTER)
584 sub_dir = kOriginalWallpaperSubDir;
564 base::FilePath wallpaper_path = GetCustomWallpaperPath(sub_dir, email, 585 base::FilePath wallpaper_path = GetCustomWallpaperPath(sub_dir, email,
565 info.file); 586 info.file);
566 if (current_wallpaper_path_ == wallpaper_path) 587 if (current_wallpaper_path_ == wallpaper_path)
567 return; 588 return;
568 current_wallpaper_path_ = wallpaper_path; 589 current_wallpaper_path_ = wallpaper_path;
569 loaded_wallpapers_++; 590 loaded_wallpapers_++;
570 591
571 task_runner_->PostTask(FROM_HERE, 592 task_runner_->PostTask(FROM_HERE,
572 base::Bind(&WallpaperManager::GetCustomWallpaperInternal, 593 base::Bind(&WallpaperManager::GetCustomWallpaperInternal,
573 base::Unretained(this), email, info, wallpaper_path, 594 base::Unretained(this), email, info, wallpaper_path,
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
652 673
653 void WallpaperManager::ClearObsoleteWallpaperPrefs() { 674 void WallpaperManager::ClearObsoleteWallpaperPrefs() {
654 PrefService* prefs = g_browser_process->local_state(); 675 PrefService* prefs = g_browser_process->local_state();
655 DictionaryPrefUpdate wallpaper_properties_pref(prefs, 676 DictionaryPrefUpdate wallpaper_properties_pref(prefs,
656 kUserWallpapersProperties); 677 kUserWallpapersProperties);
657 wallpaper_properties_pref->Clear(); 678 wallpaper_properties_pref->Clear();
658 DictionaryPrefUpdate wallpapers_pref(prefs, kUserWallpapers); 679 DictionaryPrefUpdate wallpapers_pref(prefs, kUserWallpapers);
659 wallpapers_pref->Clear(); 680 wallpapers_pref->Clear();
660 } 681 }
661 682
683 void WallpaperManager::DeleteAllExcept(const base::FilePath& path) {
684 base::FilePath dir = path.DirName();
685 if (file_util::DirectoryExists(dir)) {
686 file_util::FileEnumerator files(dir, false,
687 file_util::FileEnumerator::FILES);
688 for (base::FilePath current = files.Next(); !current.empty();
689 current = files.Next()) {
690 if (current != path)
691 file_util::Delete(current, false);
692 }
693 }
694 }
695
662 void WallpaperManager::DeleteWallpaperInList( 696 void WallpaperManager::DeleteWallpaperInList(
663 const std::vector<base::FilePath>& file_list) { 697 const std::vector<base::FilePath>& file_list) {
664 for (std::vector<base::FilePath>::const_iterator it = file_list.begin(); 698 for (std::vector<base::FilePath>::const_iterator it = file_list.begin();
665 it != file_list.end(); ++it) { 699 it != file_list.end(); ++it) {
666 base::FilePath path = *it; 700 base::FilePath path = *it;
667 // Some users may still have legacy wallpapers with png extension. We need 701 // Some users may still have legacy wallpapers with png extension. We need
668 // to delete these wallpapers too. 702 // to delete these wallpapers too.
669 if (!file_util::Delete(path, true) && 703 if (!file_util::Delete(path, true) &&
670 !file_util::Delete(path.AddExtension(".png"), false)) { 704 !file_util::Delete(path.AddExtension(".png"), false)) {
671 LOG(ERROR) << "Failed to remove user wallpaper at " << path.value(); 705 LOG(ERROR) << "Failed to remove user wallpaper at " << path.value();
(...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after
1023 GetCustomWallpaperPath(kSmallWallpaperSubDir, email, file_name); 1057 GetCustomWallpaperPath(kSmallWallpaperSubDir, email, file_name);
1024 base::FilePath large_wallpaper_path = 1058 base::FilePath large_wallpaper_path =
1025 GetCustomWallpaperPath(kLargeWallpaperSubDir, email, file_name); 1059 GetCustomWallpaperPath(kLargeWallpaperSubDir, email, file_name);
1026 1060
1027 std::vector<unsigned char> image_data = wallpaper.raw_image(); 1061 std::vector<unsigned char> image_data = wallpaper.raw_image();
1028 // Saves the original file in case that resized wallpaper is not generated 1062 // Saves the original file in case that resized wallpaper is not generated
1029 // (i.e. chrome shutdown before resized wallpaper is saved). 1063 // (i.e. chrome shutdown before resized wallpaper is saved).
1030 SaveWallpaperInternal(original_path, 1064 SaveWallpaperInternal(original_path,
1031 reinterpret_cast<char*>(&*image_data.begin()), 1065 reinterpret_cast<char*>(&*image_data.begin()),
1032 image_data.size()); 1066 image_data.size());
1067 DeleteAllExcept(original_path);
1033 1068
1034 ResizeAndSaveWallpaper(wallpaper, small_wallpaper_path, layout, 1069 ResizeAndSaveWallpaper(wallpaper, small_wallpaper_path, layout,
1035 ash::kSmallWallpaperMaxWidth, 1070 ash::kSmallWallpaperMaxWidth,
1036 ash::kSmallWallpaperMaxHeight); 1071 ash::kSmallWallpaperMaxHeight);
1072 DeleteAllExcept(small_wallpaper_path);
1037 ResizeAndSaveWallpaper(wallpaper, large_wallpaper_path, layout, 1073 ResizeAndSaveWallpaper(wallpaper, large_wallpaper_path, layout,
1038 ash::kLargeWallpaperMaxWidth, 1074 ash::kLargeWallpaperMaxWidth,
1039 ash::kLargeWallpaperMaxHeight); 1075 ash::kLargeWallpaperMaxHeight);
1076 DeleteAllExcept(large_wallpaper_path);
1040 } 1077 }
1041 1078
1042 void WallpaperManager::RecordUma(User::WallpaperType type, int index) { 1079 void WallpaperManager::RecordUma(User::WallpaperType type, int index) {
1043 UMA_HISTOGRAM_ENUMERATION("Ash.Wallpaper.Type", type, 1080 UMA_HISTOGRAM_ENUMERATION("Ash.Wallpaper.Type", type,
1044 User::WALLPAPER_TYPE_COUNT); 1081 User::WALLPAPER_TYPE_COUNT);
1045 } 1082 }
1046 1083
1047 void WallpaperManager::SaveWallpaperInternal(const base::FilePath& path, 1084 void WallpaperManager::SaveWallpaperInternal(const base::FilePath& path,
1048 const char* data, 1085 const char* data,
1049 int size) { 1086 int size) {
(...skipping 19 matching lines...) Expand all
1069 1106
1070 void WallpaperManager::SystemResumed(const base::TimeDelta& sleep_duration) { 1107 void WallpaperManager::SystemResumed(const base::TimeDelta& sleep_duration) {
1071 BatchUpdateWallpaper(); 1108 BatchUpdateWallpaper();
1072 } 1109 }
1073 1110
1074 void WallpaperManager::TimezoneChanged(const icu::TimeZone& timezone) { 1111 void WallpaperManager::TimezoneChanged(const icu::TimeZone& timezone) {
1075 RestartTimer(); 1112 RestartTimer();
1076 } 1113 }
1077 1114
1078 } // chromeos 1115 } // chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698