OLD | NEW |
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/extensions/wallpaper_private_api.h" | 5 #include "chrome/browser/chromeos/extensions/wallpaper_private_api.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "ash/shell.h" | 9 #include "ash/shell.h" |
10 #include "ash/wm/window_cycle_controller.h" | 10 #include "ash/wm/window_cycle_controller.h" |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 | 78 |
79 return file_util::PathExists(file_path) || | 79 return file_util::PathExists(file_path) || |
80 (file_util::WriteFile(file_path, data.c_str(), | 80 (file_util::WriteFile(file_path, data.c_str(), |
81 data.size()) != -1); | 81 data.size()) != -1); |
82 } | 82 } |
83 | 83 |
84 // Gets |file_name| from directory with |key|. Return false if the directory can | 84 // Gets |file_name| from directory with |key|. Return false if the directory can |
85 // not be found or failed to read file to string |data|. Note if the |file_name| | 85 // not be found or failed to read file to string |data|. Note if the |file_name| |
86 // can not be found in the directory, return true with empty |data|. It is | 86 // can not be found in the directory, return true with empty |data|. It is |
87 // expected that we may try to access file which did not saved yet. | 87 // expected that we may try to access file which did not saved yet. |
88 bool GetData(const FilePath& path, std::string* data) { | 88 bool GetData(const base::FilePath& path, std::string* data) { |
89 FilePath data_dir = path.DirName(); | 89 base::FilePath data_dir = path.DirName(); |
90 if (!file_util::DirectoryExists(data_dir) && | 90 if (!file_util::DirectoryExists(data_dir) && |
91 !file_util::CreateDirectory(data_dir)) | 91 !file_util::CreateDirectory(data_dir)) |
92 return false; | 92 return false; |
93 | 93 |
94 return !file_util::PathExists(path) || | 94 return !file_util::PathExists(path) || |
95 file_util::ReadFileToString(path, data); | 95 file_util::ReadFileToString(path, data); |
96 } | 96 } |
97 | 97 |
98 class WindowStateManager; | 98 class WindowStateManager; |
99 | 99 |
(...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
504 } | 504 } |
505 | 505 |
506 void WallpaperPrivateSetCustomWallpaperFunction::OnWallpaperDecoded( | 506 void WallpaperPrivateSetCustomWallpaperFunction::OnWallpaperDecoded( |
507 const gfx::ImageSkia& wallpaper) { | 507 const gfx::ImageSkia& wallpaper) { |
508 chromeos::WallpaperManager* wallpaper_manager = | 508 chromeos::WallpaperManager* wallpaper_manager = |
509 chromeos::WallpaperManager::Get(); | 509 chromeos::WallpaperManager::Get(); |
510 chromeos::UserImage::RawImage raw_image(image_data_.begin(), | 510 chromeos::UserImage::RawImage raw_image(image_data_.begin(), |
511 image_data_.end()); | 511 image_data_.end()); |
512 chromeos::UserImage image(wallpaper, raw_image); | 512 chromeos::UserImage image(wallpaper, raw_image); |
513 std::string file = base::Int64ToString(base::Time::Now().ToInternalValue()); | 513 std::string file = base::Int64ToString(base::Time::Now().ToInternalValue()); |
514 FilePath thumbnail_path = wallpaper_manager->GetCustomWallpaperPath( | 514 base::FilePath thumbnail_path = wallpaper_manager->GetCustomWallpaperPath( |
515 chromeos::kThumbnailWallpaperSubDir, email_, file); | 515 chromeos::kThumbnailWallpaperSubDir, email_, file); |
516 | 516 |
517 sequence_token_ = BrowserThread::GetBlockingPool()-> | 517 sequence_token_ = BrowserThread::GetBlockingPool()-> |
518 GetNamedSequenceToken(chromeos::kWallpaperSequenceTokenName); | 518 GetNamedSequenceToken(chromeos::kWallpaperSequenceTokenName); |
519 scoped_refptr<base::SequencedTaskRunner> task_runner = | 519 scoped_refptr<base::SequencedTaskRunner> task_runner = |
520 BrowserThread::GetBlockingPool()-> | 520 BrowserThread::GetBlockingPool()-> |
521 GetSequencedTaskRunnerWithShutdownBehavior(sequence_token_, | 521 GetSequencedTaskRunnerWithShutdownBehavior(sequence_token_, |
522 base::SequencedWorkerPool::BLOCK_SHUTDOWN); | 522 base::SequencedWorkerPool::BLOCK_SHUTDOWN); |
523 | 523 |
524 wallpaper.EnsureRepsForSupportedScaleFactors(); | 524 wallpaper.EnsureRepsForSupportedScaleFactors(); |
525 scoped_ptr<gfx::ImageSkia> deep_copy(wallpaper.DeepCopy()); | 525 scoped_ptr<gfx::ImageSkia> deep_copy(wallpaper.DeepCopy()); |
526 // Generates thumbnail before call api function callback. We can then request | 526 // Generates thumbnail before call api function callback. We can then request |
527 // thumbnail in the javascript callback. | 527 // thumbnail in the javascript callback. |
528 task_runner->PostTask(FROM_HERE, | 528 task_runner->PostTask(FROM_HERE, |
529 base::Bind(&WallpaperPrivateSetCustomWallpaperFunction::GenerateThumbnail, | 529 base::Bind(&WallpaperPrivateSetCustomWallpaperFunction::GenerateThumbnail, |
530 this, thumbnail_path, base::Passed(&deep_copy))); | 530 this, thumbnail_path, base::Passed(&deep_copy))); |
531 | 531 |
532 // In the new wallpaper picker UI, we do not depend on WallpaperDelegate | 532 // In the new wallpaper picker UI, we do not depend on WallpaperDelegate |
533 // to refresh thumbnail. Uses a null delegate here. | 533 // to refresh thumbnail. Uses a null delegate here. |
534 wallpaper_manager->SetCustomWallpaper(email_, file, layout_, | 534 wallpaper_manager->SetCustomWallpaper(email_, file, layout_, |
535 chromeos::User::CUSTOMIZED, | 535 chromeos::User::CUSTOMIZED, |
536 image); | 536 image); |
537 wallpaper_decoder_ = NULL; | 537 wallpaper_decoder_ = NULL; |
538 } | 538 } |
539 | 539 |
540 void WallpaperPrivateSetCustomWallpaperFunction::GenerateThumbnail( | 540 void WallpaperPrivateSetCustomWallpaperFunction::GenerateThumbnail( |
541 const FilePath& thumbnail_path, scoped_ptr<gfx::ImageSkia> image) { | 541 const base::FilePath& thumbnail_path, scoped_ptr<gfx::ImageSkia> image) { |
542 DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread( | 542 DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread( |
543 sequence_token_)); | 543 sequence_token_)); |
544 chromeos::UserImage wallpaper(*image.get()); | 544 chromeos::UserImage wallpaper(*image.get()); |
545 if (!file_util::PathExists(thumbnail_path.DirName())) | 545 if (!file_util::PathExists(thumbnail_path.DirName())) |
546 file_util::CreateDirectory(thumbnail_path.DirName()); | 546 file_util::CreateDirectory(thumbnail_path.DirName()); |
547 | 547 |
548 chromeos::WallpaperManager::Get()->ResizeAndSaveWallpaper( | 548 chromeos::WallpaperManager::Get()->ResizeAndSaveWallpaper( |
549 wallpaper, | 549 wallpaper, |
550 thumbnail_path, | 550 thumbnail_path, |
551 ash::WALLPAPER_LAYOUT_STRETCH, | 551 ash::WALLPAPER_LAYOUT_STRETCH, |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
597 bool WallpaperPrivateGetThumbnailFunction::RunImpl() { | 597 bool WallpaperPrivateGetThumbnailFunction::RunImpl() { |
598 std::string urlOrFile; | 598 std::string urlOrFile; |
599 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &urlOrFile)); | 599 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &urlOrFile)); |
600 EXTENSION_FUNCTION_VALIDATE(!urlOrFile.empty()); | 600 EXTENSION_FUNCTION_VALIDATE(!urlOrFile.empty()); |
601 | 601 |
602 std::string source; | 602 std::string source; |
603 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &source)); | 603 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &source)); |
604 EXTENSION_FUNCTION_VALIDATE(!source.empty()); | 604 EXTENSION_FUNCTION_VALIDATE(!source.empty()); |
605 | 605 |
606 std::string file_name; | 606 std::string file_name; |
607 FilePath thumbnail_path; | 607 base::FilePath thumbnail_path; |
608 std::string email = chromeos::UserManager::Get()->GetLoggedInUser()->email(); | 608 std::string email = chromeos::UserManager::Get()->GetLoggedInUser()->email(); |
609 if (source == kOnlineSource) { | 609 if (source == kOnlineSource) { |
610 file_name = GURL(urlOrFile).ExtractFileName(); | 610 file_name = GURL(urlOrFile).ExtractFileName(); |
611 CHECK(PathService::Get(chrome::DIR_CHROMEOS_WALLPAPER_THUMBNAILS, | 611 CHECK(PathService::Get(chrome::DIR_CHROMEOS_WALLPAPER_THUMBNAILS, |
612 &thumbnail_path)); | 612 &thumbnail_path)); |
613 thumbnail_path = thumbnail_path.Append(file_name); | 613 thumbnail_path = thumbnail_path.Append(file_name); |
614 } else { | 614 } else { |
615 file_name = urlOrFile; | 615 file_name = urlOrFile; |
616 thumbnail_path = chromeos::WallpaperManager::Get()-> | 616 thumbnail_path = chromeos::WallpaperManager::Get()-> |
617 GetCustomWallpaperPath(chromeos::kThumbnailWallpaperSubDir, email, | 617 GetCustomWallpaperPath(chromeos::kThumbnailWallpaperSubDir, email, |
(...skipping 25 matching lines...) Expand all Loading... |
643 } | 643 } |
644 | 644 |
645 void WallpaperPrivateGetThumbnailFunction::FileLoaded( | 645 void WallpaperPrivateGetThumbnailFunction::FileLoaded( |
646 const std::string& data) { | 646 const std::string& data) { |
647 BinaryValue* thumbnail = BinaryValue::CreateWithCopiedBuffer(data.c_str(), | 647 BinaryValue* thumbnail = BinaryValue::CreateWithCopiedBuffer(data.c_str(), |
648 data.size()); | 648 data.size()); |
649 SetResult(thumbnail); | 649 SetResult(thumbnail); |
650 SendResponse(true); | 650 SendResponse(true); |
651 } | 651 } |
652 | 652 |
653 void WallpaperPrivateGetThumbnailFunction::Get(const FilePath& path) { | 653 void WallpaperPrivateGetThumbnailFunction::Get(const base::FilePath& path) { |
654 DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread( | 654 DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread( |
655 sequence_token_)); | 655 sequence_token_)); |
656 std::string data; | 656 std::string data; |
657 if (GetData(path, &data)) { | 657 if (GetData(path, &data)) { |
658 if (data.empty()) { | 658 if (data.empty()) { |
659 BrowserThread::PostTask( | 659 BrowserThread::PostTask( |
660 BrowserThread::UI, FROM_HERE, | 660 BrowserThread::UI, FROM_HERE, |
661 base::Bind(&WallpaperPrivateGetThumbnailFunction::FileNotLoaded, this)); | 661 base::Bind(&WallpaperPrivateGetThumbnailFunction::FileNotLoaded, this)); |
662 } else { | 662 } else { |
663 BrowserThread::PostTask( | 663 BrowserThread::PostTask( |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
758 return true; | 758 return true; |
759 } | 759 } |
760 | 760 |
761 void WallpaperPrivateGetOfflineWallpaperListFunction::GetList( | 761 void WallpaperPrivateGetOfflineWallpaperListFunction::GetList( |
762 const std::string& email, | 762 const std::string& email, |
763 const std::string& source) { | 763 const std::string& source) { |
764 DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread( | 764 DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread( |
765 sequence_token_)); | 765 sequence_token_)); |
766 std::vector<std::string> file_list; | 766 std::vector<std::string> file_list; |
767 if (source == kOnlineSource) { | 767 if (source == kOnlineSource) { |
768 FilePath wallpaper_dir; | 768 base::FilePath wallpaper_dir; |
769 CHECK(PathService::Get(chrome::DIR_CHROMEOS_WALLPAPERS, &wallpaper_dir)); | 769 CHECK(PathService::Get(chrome::DIR_CHROMEOS_WALLPAPERS, &wallpaper_dir)); |
770 if (file_util::DirectoryExists(wallpaper_dir)) { | 770 if (file_util::DirectoryExists(wallpaper_dir)) { |
771 file_util::FileEnumerator files(wallpaper_dir, false, | 771 file_util::FileEnumerator files(wallpaper_dir, false, |
772 file_util::FileEnumerator::FILES); | 772 file_util::FileEnumerator::FILES); |
773 for (FilePath current = files.Next(); !current.empty(); | 773 for (base::FilePath current = files.Next(); !current.empty(); |
774 current = files.Next()) { | 774 current = files.Next()) { |
775 std::string file_name = current.BaseName().RemoveExtension().value(); | 775 std::string file_name = current.BaseName().RemoveExtension().value(); |
776 // Do not add file name of small resolution wallpaper to the list. | 776 // Do not add file name of small resolution wallpaper to the list. |
777 if (!EndsWith(file_name, chromeos::kSmallWallpaperSuffix, true)) | 777 if (!EndsWith(file_name, chromeos::kSmallWallpaperSuffix, true)) |
778 file_list.push_back(current.BaseName().value()); | 778 file_list.push_back(current.BaseName().value()); |
779 } | 779 } |
780 } | 780 } |
781 } else { | 781 } else { |
782 FilePath custom_thumbnails_dir = chromeos::WallpaperManager::Get()-> | 782 base::FilePath custom_thumbnails_dir = chromeos::WallpaperManager::Get()-> |
783 GetCustomWallpaperPath(chromeos::kThumbnailWallpaperSubDir, email, ""); | 783 GetCustomWallpaperPath(chromeos::kThumbnailWallpaperSubDir, email, ""); |
784 if (file_util::DirectoryExists(custom_thumbnails_dir)) { | 784 if (file_util::DirectoryExists(custom_thumbnails_dir)) { |
785 file_util::FileEnumerator files(custom_thumbnails_dir, false, | 785 file_util::FileEnumerator files(custom_thumbnails_dir, false, |
786 file_util::FileEnumerator::FILES); | 786 file_util::FileEnumerator::FILES); |
787 for (FilePath current = files.Next(); !current.empty(); | 787 for (base::FilePath current = files.Next(); !current.empty(); |
788 current = files.Next()) { | 788 current = files.Next()) { |
789 file_list.push_back(current.BaseName().value()); | 789 file_list.push_back(current.BaseName().value()); |
790 } | 790 } |
791 } | 791 } |
792 } | 792 } |
793 BrowserThread::PostTask( | 793 BrowserThread::PostTask( |
794 BrowserThread::UI, FROM_HERE, | 794 BrowserThread::UI, FROM_HERE, |
795 base::Bind(&WallpaperPrivateGetOfflineWallpaperListFunction::OnComplete, | 795 base::Bind(&WallpaperPrivateGetOfflineWallpaperListFunction::OnComplete, |
796 this, file_list)); | 796 this, file_list)); |
797 } | 797 } |
798 | 798 |
799 void WallpaperPrivateGetOfflineWallpaperListFunction::OnComplete( | 799 void WallpaperPrivateGetOfflineWallpaperListFunction::OnComplete( |
800 const std::vector<std::string>& file_list) { | 800 const std::vector<std::string>& file_list) { |
801 ListValue* results = new ListValue(); | 801 ListValue* results = new ListValue(); |
802 results->AppendStrings(file_list); | 802 results->AppendStrings(file_list); |
803 SetResult(results); | 803 SetResult(results); |
804 SendResponse(true); | 804 SendResponse(true); |
805 } | 805 } |
OLD | NEW |