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

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: reviews 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 bool WallpaperManager::ResizeWallpaper(
332 const base::FilePath& path, 332 const UserImage& wallpaper,
333 ash::WallpaperLayout layout, 333 ash::WallpaperLayout layout,
334 int preferred_width, 334 int preferred_width,
335 int preferred_height) { 335 int preferred_height,
336 scoped_refptr<base::RefCountedBytes>* output) {
336 DCHECK(BrowserThread::GetBlockingPool()-> 337 DCHECK(BrowserThread::GetBlockingPool()->
337 IsRunningSequenceOnCurrentThread(sequence_token_)); 338 IsRunningSequenceOnCurrentThread(sequence_token_));
338 int width = wallpaper.image().width(); 339 int width = wallpaper.image().width();
339 int height = wallpaper.image().height(); 340 int height = wallpaper.image().height();
340 int resized_width; 341 int resized_width;
341 int resized_height; 342 int resized_height;
343 *output = new base::RefCountedBytes();
342 344
343 if (layout == ash::WALLPAPER_LAYOUT_CENTER_CROPPED) { 345 if (layout == ash::WALLPAPER_LAYOUT_CENTER_CROPPED) {
344 // Do not resize custom wallpaper if it is smaller than preferred size. 346 // Do not resize custom wallpaper if it is smaller than preferred size.
345 if (!(width > preferred_width && height > preferred_height)) 347 if (!(width > preferred_width && height > preferred_height))
346 return; 348 return false;
347 349
348 double horizontal_ratio = static_cast<double>(preferred_width) / width; 350 double horizontal_ratio = static_cast<double>(preferred_width) / width;
349 double vertical_ratio = static_cast<double>(preferred_height) / height; 351 double vertical_ratio = static_cast<double>(preferred_height) / height;
350 if (vertical_ratio > horizontal_ratio) { 352 if (vertical_ratio > horizontal_ratio) {
351 resized_width = 353 resized_width =
352 RoundPositive(static_cast<double>(width) * vertical_ratio); 354 RoundPositive(static_cast<double>(width) * vertical_ratio);
353 resized_height = preferred_height; 355 resized_height = preferred_height;
354 } else { 356 } else {
355 resized_width = preferred_width; 357 resized_width = preferred_width;
356 resized_height = 358 resized_height =
357 RoundPositive(static_cast<double>(height) * horizontal_ratio); 359 RoundPositive(static_cast<double>(height) * horizontal_ratio);
358 } 360 }
359 } else if (layout == ash::WALLPAPER_LAYOUT_STRETCH) { 361 } else if (layout == ash::WALLPAPER_LAYOUT_STRETCH) {
360 resized_width = preferred_width; 362 resized_width = preferred_width;
361 resized_height = preferred_height; 363 resized_height = preferred_height;
362 } else { 364 } else {
363 // TODO(bshe): Generates cropped custom wallpaper for CENTER layout. 365 resized_width = width;
364 if (file_util::PathExists(path)) 366 resized_height = height;
365 file_util::Delete(path, false);
366 return;
367 } 367 }
368 368
369 gfx::ImageSkia resized_image = gfx::ImageSkiaOperations::CreateResizedImage( 369 gfx::ImageSkia resized_image = gfx::ImageSkiaOperations::CreateResizedImage(
370 wallpaper.image(), 370 wallpaper.image(),
371 skia::ImageOperations::RESIZE_LANCZOS3, 371 skia::ImageOperations::RESIZE_LANCZOS3,
372 gfx::Size(resized_width, resized_height)); 372 gfx::Size(resized_width, resized_height));
373 373
374 SkBitmap image = *(resized_image.bitmap()); 374 SkBitmap image = *(resized_image.bitmap());
375 scoped_refptr<base::RefCountedBytes> data = new base::RefCountedBytes();
376 SkAutoLockPixels lock_input(image); 375 SkAutoLockPixels lock_input(image);
377 gfx::JPEGCodec::Encode( 376 gfx::JPEGCodec::Encode(
378 reinterpret_cast<unsigned char*>(image.getAddr32(0, 0)), 377 reinterpret_cast<unsigned char*>(image.getAddr32(0, 0)),
379 gfx::JPEGCodec::FORMAT_SkBitmap, 378 gfx::JPEGCodec::FORMAT_SkBitmap,
380 image.width(), 379 image.width(),
381 image.height(), 380 image.height(),
382 image.width() * image.bytesPerPixel(), 381 image.width() * image.bytesPerPixel(),
383 kDefaultEncodingQuality, &data->data()); 382 kDefaultEncodingQuality, &(*output)->data());
384 SaveWallpaperInternal(path, 383 return true;
385 reinterpret_cast<const char*>(data->front()), 384 }
386 data->size()); 385
386 void WallpaperManager::ResizeAndSaveWallpaper(const UserImage& wallpaper,
387 const base::FilePath& path,
388 ash::WallpaperLayout layout,
389 int preferred_width,
390 int preferred_height) {
391 if (layout == ash::WALLPAPER_LAYOUT_CENTER) {
392 // TODO(bshe): Generates cropped custom wallpaper for CENTER layout.
393 if (file_util::PathExists(path))
394 file_util::Delete(path, false);
395 return;
396 }
397 scoped_refptr<base::RefCountedBytes> data;
398 if (ResizeWallpaper(wallpaper, layout, preferred_width, preferred_height,
399 &data)) {
flackr 2013/03/07 14:46:47 If the image is smaller than the preferred width a
bshe 2013/03/07 16:13:05 If it is smaller than preferred size, we wont try
400 SaveWallpaperInternal(path,
401 reinterpret_cast<const char*>(data->front()),
402 data->size());
403 }
387 } 404 }
388 405
389 void WallpaperManager::RestartTimer() { 406 void WallpaperManager::RestartTimer() {
390 timer_.Stop(); 407 timer_.Stop();
391 // Determine the next update time as the earliest local midnight after now. 408 // Determine the next update time as the earliest local midnight after now.
392 // Note that this may be more than kWallpaperUpdateIntervalSec seconds in the 409 // Note that this may be more than kWallpaperUpdateIntervalSec seconds in the
393 // future due to DST. 410 // future due to DST.
394 base::Time now = base::Time::Now(); 411 base::Time now = base::Time::Now();
395 base::TimeDelta update_interval = base::TimeDelta::FromSeconds( 412 base::TimeDelta update_interval = base::TimeDelta::FromSeconds(
396 kWallpaperUpdateIntervalSec); 413 kWallpaperUpdateIntervalSec);
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
554 gfx::ImageSkia user_wallpaper; 571 gfx::ImageSkia user_wallpaper;
555 if (GetWallpaperFromCache(email, &user_wallpaper)) { 572 if (GetWallpaperFromCache(email, &user_wallpaper)) {
556 ash::Shell::GetInstance()->desktop_background_controller()-> 573 ash::Shell::GetInstance()->desktop_background_controller()->
557 SetCustomWallpaper(user_wallpaper, info.layout); 574 SetCustomWallpaper(user_wallpaper, info.layout);
558 } else { 575 } else {
559 if (info.type == User::CUSTOMIZED) { 576 if (info.type == User::CUSTOMIZED) {
560 ash::WallpaperResolution resolution = ash::Shell::GetInstance()-> 577 ash::WallpaperResolution resolution = ash::Shell::GetInstance()->
561 desktop_background_controller()->GetAppropriateResolution(); 578 desktop_background_controller()->GetAppropriateResolution();
562 const char* sub_dir = (resolution == ash::WALLPAPER_RESOLUTION_SMALL) ? 579 const char* sub_dir = (resolution == ash::WALLPAPER_RESOLUTION_SMALL) ?
563 kSmallWallpaperSubDir : kLargeWallpaperSubDir; 580 kSmallWallpaperSubDir : kLargeWallpaperSubDir;
581 // Wallpaper is not resized when layout is ash::WALLPAPER_LAYOUT_CENTER.
582 // Original wallpaper should be used in this case.
583 // TODO(bshe): Generates cropped custom wallpaper for CENTER layout.
584 if (info.layout == ash::WALLPAPER_LAYOUT_CENTER)
585 sub_dir = kOriginalWallpaperSubDir;
564 base::FilePath wallpaper_path = GetCustomWallpaperPath(sub_dir, email, 586 base::FilePath wallpaper_path = GetCustomWallpaperPath(sub_dir, email,
565 info.file); 587 info.file);
566 if (current_wallpaper_path_ == wallpaper_path) 588 if (current_wallpaper_path_ == wallpaper_path)
567 return; 589 return;
568 current_wallpaper_path_ = wallpaper_path; 590 current_wallpaper_path_ = wallpaper_path;
569 loaded_wallpapers_++; 591 loaded_wallpapers_++;
570 592
571 task_runner_->PostTask(FROM_HERE, 593 task_runner_->PostTask(FROM_HERE,
572 base::Bind(&WallpaperManager::GetCustomWallpaperInternal, 594 base::Bind(&WallpaperManager::GetCustomWallpaperInternal,
573 base::Unretained(this), email, info, wallpaper_path, 595 base::Unretained(this), email, info, wallpaper_path,
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
652 674
653 void WallpaperManager::ClearObsoleteWallpaperPrefs() { 675 void WallpaperManager::ClearObsoleteWallpaperPrefs() {
654 PrefService* prefs = g_browser_process->local_state(); 676 PrefService* prefs = g_browser_process->local_state();
655 DictionaryPrefUpdate wallpaper_properties_pref(prefs, 677 DictionaryPrefUpdate wallpaper_properties_pref(prefs,
656 kUserWallpapersProperties); 678 kUserWallpapersProperties);
657 wallpaper_properties_pref->Clear(); 679 wallpaper_properties_pref->Clear();
658 DictionaryPrefUpdate wallpapers_pref(prefs, kUserWallpapers); 680 DictionaryPrefUpdate wallpapers_pref(prefs, kUserWallpapers);
659 wallpapers_pref->Clear(); 681 wallpapers_pref->Clear();
660 } 682 }
661 683
684 void WallpaperManager::DeleteAllExcept(const base::FilePath& path) {
685 base::FilePath dir = path.DirName();
686 if (file_util::DirectoryExists(dir)) {
687 file_util::FileEnumerator files(dir, false,
688 file_util::FileEnumerator::FILES);
689 for (base::FilePath current = files.Next(); !current.empty();
690 current = files.Next()) {
691 if (current != path)
692 file_util::Delete(current, false);
693 }
694 }
695 }
696
662 void WallpaperManager::DeleteWallpaperInList( 697 void WallpaperManager::DeleteWallpaperInList(
663 const std::vector<base::FilePath>& file_list) { 698 const std::vector<base::FilePath>& file_list) {
664 for (std::vector<base::FilePath>::const_iterator it = file_list.begin(); 699 for (std::vector<base::FilePath>::const_iterator it = file_list.begin();
665 it != file_list.end(); ++it) { 700 it != file_list.end(); ++it) {
666 base::FilePath path = *it; 701 base::FilePath path = *it;
667 // Some users may still have legacy wallpapers with png extension. We need 702 // Some users may still have legacy wallpapers with png extension. We need
668 // to delete these wallpapers too. 703 // to delete these wallpapers too.
669 if (!file_util::Delete(path, true) && 704 if (!file_util::Delete(path, true) &&
670 !file_util::Delete(path.AddExtension(".png"), false)) { 705 !file_util::Delete(path.AddExtension(".png"), false)) {
671 LOG(ERROR) << "Failed to remove user wallpaper at " << path.value(); 706 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); 1058 GetCustomWallpaperPath(kSmallWallpaperSubDir, email, file_name);
1024 base::FilePath large_wallpaper_path = 1059 base::FilePath large_wallpaper_path =
1025 GetCustomWallpaperPath(kLargeWallpaperSubDir, email, file_name); 1060 GetCustomWallpaperPath(kLargeWallpaperSubDir, email, file_name);
1026 1061
1027 std::vector<unsigned char> image_data = wallpaper.raw_image(); 1062 std::vector<unsigned char> image_data = wallpaper.raw_image();
1028 // Saves the original file in case that resized wallpaper is not generated 1063 // Saves the original file in case that resized wallpaper is not generated
1029 // (i.e. chrome shutdown before resized wallpaper is saved). 1064 // (i.e. chrome shutdown before resized wallpaper is saved).
1030 SaveWallpaperInternal(original_path, 1065 SaveWallpaperInternal(original_path,
1031 reinterpret_cast<char*>(&*image_data.begin()), 1066 reinterpret_cast<char*>(&*image_data.begin()),
1032 image_data.size()); 1067 image_data.size());
1068 DeleteAllExcept(original_path);
1033 1069
1034 ResizeAndSaveWallpaper(wallpaper, small_wallpaper_path, layout, 1070 ResizeAndSaveWallpaper(wallpaper, small_wallpaper_path, layout,
1035 ash::kSmallWallpaperMaxWidth, 1071 ash::kSmallWallpaperMaxWidth,
1036 ash::kSmallWallpaperMaxHeight); 1072 ash::kSmallWallpaperMaxHeight);
1073 DeleteAllExcept(small_wallpaper_path);
1037 ResizeAndSaveWallpaper(wallpaper, large_wallpaper_path, layout, 1074 ResizeAndSaveWallpaper(wallpaper, large_wallpaper_path, layout,
1038 ash::kLargeWallpaperMaxWidth, 1075 ash::kLargeWallpaperMaxWidth,
1039 ash::kLargeWallpaperMaxHeight); 1076 ash::kLargeWallpaperMaxHeight);
1077 DeleteAllExcept(large_wallpaper_path);
1040 } 1078 }
1041 1079
1042 void WallpaperManager::RecordUma(User::WallpaperType type, int index) { 1080 void WallpaperManager::RecordUma(User::WallpaperType type, int index) {
1043 UMA_HISTOGRAM_ENUMERATION("Ash.Wallpaper.Type", type, 1081 UMA_HISTOGRAM_ENUMERATION("Ash.Wallpaper.Type", type,
1044 User::WALLPAPER_TYPE_COUNT); 1082 User::WALLPAPER_TYPE_COUNT);
1045 } 1083 }
1046 1084
1047 void WallpaperManager::SaveWallpaperInternal(const base::FilePath& path, 1085 void WallpaperManager::SaveWallpaperInternal(const base::FilePath& path,
1048 const char* data, 1086 const char* data,
1049 int size) { 1087 int size) {
(...skipping 19 matching lines...) Expand all
1069 1107
1070 void WallpaperManager::SystemResumed(const base::TimeDelta& sleep_duration) { 1108 void WallpaperManager::SystemResumed(const base::TimeDelta& sleep_duration) {
1071 BatchUpdateWallpaper(); 1109 BatchUpdateWallpaper();
1072 } 1110 }
1073 1111
1074 void WallpaperManager::TimezoneChanged(const icu::TimeZone& timezone) { 1112 void WallpaperManager::TimezoneChanged(const icu::TimeZone& timezone) {
1075 RestartTimer(); 1113 RestartTimer();
1076 } 1114 }
1077 1115
1078 } // chromeos 1116 } // chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698