| 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 <map> | 7 #include <map> |
| 8 #include <memory> |
| 8 #include <set> | 9 #include <set> |
| 9 #include <string> | 10 #include <string> |
| 10 #include <utility> | 11 #include <utility> |
| 11 #include <vector> | 12 #include <vector> |
| 12 | 13 |
| 13 #include "ash/desktop_background/desktop_background_controller.h" | 14 #include "ash/desktop_background/desktop_background_controller.h" |
| 14 #include "ash/shell.h" | 15 #include "ash/shell.h" |
| 15 #include "ash/wm/mru_window_tracker.h" | 16 #include "ash/wm/mru_window_tracker.h" |
| 16 #include "ash/wm/window_state.h" | 17 #include "ash/wm/window_state.h" |
| 17 #include "ash/wm/window_util.h" | 18 #include "ash/wm/window_util.h" |
| 18 #include "base/command_line.h" | 19 #include "base/command_line.h" |
| 19 #include "base/files/file_enumerator.h" | 20 #include "base/files/file_enumerator.h" |
| 20 #include "base/files/file_util.h" | 21 #include "base/files/file_util.h" |
| 21 #include "base/macros.h" | 22 #include "base/macros.h" |
| 22 #include "base/memory/ref_counted_memory.h" | 23 #include "base/memory/ref_counted_memory.h" |
| 23 #include "base/memory/scoped_ptr.h" | |
| 24 #include "base/path_service.h" | 24 #include "base/path_service.h" |
| 25 #include "base/strings/string_number_conversions.h" | 25 #include "base/strings/string_number_conversions.h" |
| 26 #include "base/strings/stringprintf.h" | 26 #include "base/strings/stringprintf.h" |
| 27 #include "base/threading/worker_pool.h" | 27 #include "base/threading/worker_pool.h" |
| 28 #include "chrome/browser/browser_process.h" | 28 #include "chrome/browser/browser_process.h" |
| 29 #include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h" | 29 #include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h" |
| 30 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 30 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
| 31 #include "chrome/browser/profiles/profile.h" | 31 #include "chrome/browser/profiles/profile.h" |
| 32 #include "chrome/browser/sync/profile_sync_service_factory.h" | 32 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 33 #include "chrome/common/chrome_paths.h" | 33 #include "chrome/common/chrome_paths.h" |
| (...skipping 454 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 488 task_runner->PostTask(FROM_HERE, | 488 task_runner->PostTask(FROM_HERE, |
| 489 base::Bind(&WallpaperPrivateSetWallpaperFunction::SaveToFile, this)); | 489 base::Bind(&WallpaperPrivateSetWallpaperFunction::SaveToFile, this)); |
| 490 } | 490 } |
| 491 | 491 |
| 492 void WallpaperPrivateSetWallpaperFunction::SaveToFile() { | 492 void WallpaperPrivateSetWallpaperFunction::SaveToFile() { |
| 493 DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread( | 493 DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread( |
| 494 sequence_token_)); | 494 sequence_token_)); |
| 495 std::string file_name = GURL(params->url).ExtractFileName(); | 495 std::string file_name = GURL(params->url).ExtractFileName(); |
| 496 if (SaveData(chrome::DIR_CHROMEOS_WALLPAPERS, file_name, params->wallpaper)) { | 496 if (SaveData(chrome::DIR_CHROMEOS_WALLPAPERS, file_name, params->wallpaper)) { |
| 497 wallpaper_.EnsureRepsForSupportedScales(); | 497 wallpaper_.EnsureRepsForSupportedScales(); |
| 498 scoped_ptr<gfx::ImageSkia> deep_copy(wallpaper_.DeepCopy()); | 498 std::unique_ptr<gfx::ImageSkia> deep_copy(wallpaper_.DeepCopy()); |
| 499 // ImageSkia is not RefCountedThreadSafe. Use a deep copied ImageSkia if | 499 // ImageSkia is not RefCountedThreadSafe. Use a deep copied ImageSkia if |
| 500 // post to another thread. | 500 // post to another thread. |
| 501 BrowserThread::PostTask( | 501 BrowserThread::PostTask( |
| 502 BrowserThread::UI, FROM_HERE, | 502 BrowserThread::UI, FROM_HERE, |
| 503 base::Bind(&WallpaperPrivateSetWallpaperFunction::SetDecodedWallpaper, | 503 base::Bind(&WallpaperPrivateSetWallpaperFunction::SetDecodedWallpaper, |
| 504 this, base::Passed(std::move(deep_copy)))); | 504 this, base::Passed(std::move(deep_copy)))); |
| 505 | 505 |
| 506 base::FilePath wallpaper_dir; | 506 base::FilePath wallpaper_dir; |
| 507 CHECK(PathService::Get(chrome::DIR_CHROMEOS_WALLPAPERS, &wallpaper_dir)); | 507 CHECK(PathService::Get(chrome::DIR_CHROMEOS_WALLPAPERS, &wallpaper_dir)); |
| 508 base::FilePath file_path = | 508 base::FilePath file_path = |
| (...skipping 11 matching lines...) Expand all Loading... |
| 520 std::string error = base::StringPrintf( | 520 std::string error = base::StringPrintf( |
| 521 "Failed to create/write wallpaper to %s.", file_name.c_str()); | 521 "Failed to create/write wallpaper to %s.", file_name.c_str()); |
| 522 BrowserThread::PostTask( | 522 BrowserThread::PostTask( |
| 523 BrowserThread::UI, FROM_HERE, | 523 BrowserThread::UI, FROM_HERE, |
| 524 base::Bind(&WallpaperPrivateSetWallpaperFunction::OnFailure, | 524 base::Bind(&WallpaperPrivateSetWallpaperFunction::OnFailure, |
| 525 this, error)); | 525 this, error)); |
| 526 } | 526 } |
| 527 } | 527 } |
| 528 | 528 |
| 529 void WallpaperPrivateSetWallpaperFunction::SetDecodedWallpaper( | 529 void WallpaperPrivateSetWallpaperFunction::SetDecodedWallpaper( |
| 530 scoped_ptr<gfx::ImageSkia> image) { | 530 std::unique_ptr<gfx::ImageSkia> image) { |
| 531 chromeos::WallpaperManager* wallpaper_manager = | 531 chromeos::WallpaperManager* wallpaper_manager = |
| 532 chromeos::WallpaperManager::Get(); | 532 chromeos::WallpaperManager::Get(); |
| 533 | 533 |
| 534 wallpaper::WallpaperLayout layout = wallpaper_api_util::GetLayoutEnum( | 534 wallpaper::WallpaperLayout layout = wallpaper_api_util::GetLayoutEnum( |
| 535 wallpaper_base::ToString(params->layout)); | 535 wallpaper_base::ToString(params->layout)); |
| 536 | 536 |
| 537 bool update_wallpaper = | 537 bool update_wallpaper = |
| 538 account_id_ == | 538 account_id_ == |
| 539 user_manager::UserManager::Get()->GetActiveUser()->GetAccountId(); | 539 user_manager::UserManager::Get()->GetActiveUser()->GetAccountId(); |
| 540 wallpaper_manager->SetWallpaperFromImageSkia(account_id_, *image.get(), | 540 wallpaper_manager->SetWallpaperFromImageSkia(account_id_, *image.get(), |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 638 unsafe_wallpaper_decoder_ = NULL; | 638 unsafe_wallpaper_decoder_ = NULL; |
| 639 | 639 |
| 640 Profile* profile = Profile::FromBrowserContext(browser_context()); | 640 Profile* profile = Profile::FromBrowserContext(browser_context()); |
| 641 // TODO(xdai): This preference is unused now. For compatiblity concern, we | 641 // TODO(xdai): This preference is unused now. For compatiblity concern, we |
| 642 // need to keep it until it's safe to clean it up. | 642 // need to keep it until it's safe to clean it up. |
| 643 profile->GetPrefs()->SetString(prefs::kCurrentWallpaperAppName, | 643 profile->GetPrefs()->SetString(prefs::kCurrentWallpaperAppName, |
| 644 std::string()); | 644 std::string()); |
| 645 | 645 |
| 646 if (params->generate_thumbnail) { | 646 if (params->generate_thumbnail) { |
| 647 image.EnsureRepsForSupportedScales(); | 647 image.EnsureRepsForSupportedScales(); |
| 648 scoped_ptr<gfx::ImageSkia> deep_copy(image.DeepCopy()); | 648 std::unique_ptr<gfx::ImageSkia> deep_copy(image.DeepCopy()); |
| 649 // Generates thumbnail before call api function callback. We can then | 649 // Generates thumbnail before call api function callback. We can then |
| 650 // request thumbnail in the javascript callback. | 650 // request thumbnail in the javascript callback. |
| 651 task_runner->PostTask(FROM_HERE, | 651 task_runner->PostTask(FROM_HERE, |
| 652 base::Bind( | 652 base::Bind( |
| 653 &WallpaperPrivateSetCustomWallpaperFunction::GenerateThumbnail, | 653 &WallpaperPrivateSetCustomWallpaperFunction::GenerateThumbnail, |
| 654 this, thumbnail_path, base::Passed(&deep_copy))); | 654 this, thumbnail_path, base::Passed(&deep_copy))); |
| 655 } else { | 655 } else { |
| 656 SendResponse(true); | 656 SendResponse(true); |
| 657 } | 657 } |
| 658 } | 658 } |
| 659 | 659 |
| 660 void WallpaperPrivateSetCustomWallpaperFunction::GenerateThumbnail( | 660 void WallpaperPrivateSetCustomWallpaperFunction::GenerateThumbnail( |
| 661 const base::FilePath& thumbnail_path, scoped_ptr<gfx::ImageSkia> image) { | 661 const base::FilePath& thumbnail_path, |
| 662 std::unique_ptr<gfx::ImageSkia> image) { |
| 662 DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread( | 663 DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread( |
| 663 sequence_token_)); | 664 sequence_token_)); |
| 664 if (!base::PathExists(thumbnail_path.DirName())) | 665 if (!base::PathExists(thumbnail_path.DirName())) |
| 665 base::CreateDirectory(thumbnail_path.DirName()); | 666 base::CreateDirectory(thumbnail_path.DirName()); |
| 666 | 667 |
| 667 scoped_refptr<base::RefCountedBytes> data; | 668 scoped_refptr<base::RefCountedBytes> data; |
| 668 chromeos::WallpaperManager::Get()->ResizeImage( | 669 chromeos::WallpaperManager::Get()->ResizeImage( |
| 669 *image, wallpaper::WALLPAPER_LAYOUT_STRETCH, | 670 *image, wallpaper::WALLPAPER_LAYOUT_STRETCH, |
| 670 wallpaper::kWallpaperThumbnailWidth, wallpaper::kWallpaperThumbnailHeight, | 671 wallpaper::kWallpaperThumbnailWidth, wallpaper::kWallpaperThumbnailHeight, |
| 671 &data, NULL); | 672 &data, NULL); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 684 SendResponse(true); | 685 SendResponse(true); |
| 685 } | 686 } |
| 686 | 687 |
| 687 WallpaperPrivateSetCustomWallpaperLayoutFunction:: | 688 WallpaperPrivateSetCustomWallpaperLayoutFunction:: |
| 688 WallpaperPrivateSetCustomWallpaperLayoutFunction() {} | 689 WallpaperPrivateSetCustomWallpaperLayoutFunction() {} |
| 689 | 690 |
| 690 WallpaperPrivateSetCustomWallpaperLayoutFunction:: | 691 WallpaperPrivateSetCustomWallpaperLayoutFunction:: |
| 691 ~WallpaperPrivateSetCustomWallpaperLayoutFunction() {} | 692 ~WallpaperPrivateSetCustomWallpaperLayoutFunction() {} |
| 692 | 693 |
| 693 bool WallpaperPrivateSetCustomWallpaperLayoutFunction::RunAsync() { | 694 bool WallpaperPrivateSetCustomWallpaperLayoutFunction::RunAsync() { |
| 694 scoped_ptr<set_custom_wallpaper_layout::Params> params( | 695 std::unique_ptr<set_custom_wallpaper_layout::Params> params( |
| 695 set_custom_wallpaper_layout::Params::Create(*args_)); | 696 set_custom_wallpaper_layout::Params::Create(*args_)); |
| 696 EXTENSION_FUNCTION_VALIDATE(params); | 697 EXTENSION_FUNCTION_VALIDATE(params); |
| 697 | 698 |
| 698 chromeos::WallpaperManager* wallpaper_manager = | 699 chromeos::WallpaperManager* wallpaper_manager = |
| 699 chromeos::WallpaperManager::Get(); | 700 chromeos::WallpaperManager::Get(); |
| 700 wallpaper::WallpaperInfo info; | 701 wallpaper::WallpaperInfo info; |
| 701 wallpaper_manager->GetLoggedInUserWallpaperInfo(&info); | 702 wallpaper_manager->GetLoggedInUserWallpaperInfo(&info); |
| 702 if (info.type != user_manager::User::CUSTOMIZED) { | 703 if (info.type != user_manager::User::CUSTOMIZED) { |
| 703 SetError("Only custom wallpaper can change layout."); | 704 SetError("Only custom wallpaper can change layout."); |
| 704 SendResponse(false); | 705 SendResponse(false); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 747 return true; | 748 return true; |
| 748 } | 749 } |
| 749 | 750 |
| 750 WallpaperPrivateGetThumbnailFunction::WallpaperPrivateGetThumbnailFunction() { | 751 WallpaperPrivateGetThumbnailFunction::WallpaperPrivateGetThumbnailFunction() { |
| 751 } | 752 } |
| 752 | 753 |
| 753 WallpaperPrivateGetThumbnailFunction::~WallpaperPrivateGetThumbnailFunction() { | 754 WallpaperPrivateGetThumbnailFunction::~WallpaperPrivateGetThumbnailFunction() { |
| 754 } | 755 } |
| 755 | 756 |
| 756 bool WallpaperPrivateGetThumbnailFunction::RunAsync() { | 757 bool WallpaperPrivateGetThumbnailFunction::RunAsync() { |
| 757 scoped_ptr<get_thumbnail::Params> params( | 758 std::unique_ptr<get_thumbnail::Params> params( |
| 758 get_thumbnail::Params::Create(*args_)); | 759 get_thumbnail::Params::Create(*args_)); |
| 759 EXTENSION_FUNCTION_VALIDATE(params); | 760 EXTENSION_FUNCTION_VALIDATE(params); |
| 760 | 761 |
| 761 base::FilePath thumbnail_path; | 762 base::FilePath thumbnail_path; |
| 762 if (params->source == wallpaper_private::WALLPAPER_SOURCE_ONLINE) { | 763 if (params->source == wallpaper_private::WALLPAPER_SOURCE_ONLINE) { |
| 763 std::string file_name = GURL(params->url_or_file).ExtractFileName(); | 764 std::string file_name = GURL(params->url_or_file).ExtractFileName(); |
| 764 CHECK(PathService::Get(chrome::DIR_CHROMEOS_WALLPAPER_THUMBNAILS, | 765 CHECK(PathService::Get(chrome::DIR_CHROMEOS_WALLPAPER_THUMBNAILS, |
| 765 &thumbnail_path)); | 766 &thumbnail_path)); |
| 766 thumbnail_path = thumbnail_path.Append(file_name); | 767 thumbnail_path = thumbnail_path.Append(file_name); |
| 767 } else { | 768 } else { |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 833 } | 834 } |
| 834 } | 835 } |
| 835 | 836 |
| 836 WallpaperPrivateSaveThumbnailFunction::WallpaperPrivateSaveThumbnailFunction() { | 837 WallpaperPrivateSaveThumbnailFunction::WallpaperPrivateSaveThumbnailFunction() { |
| 837 } | 838 } |
| 838 | 839 |
| 839 WallpaperPrivateSaveThumbnailFunction:: | 840 WallpaperPrivateSaveThumbnailFunction:: |
| 840 ~WallpaperPrivateSaveThumbnailFunction() {} | 841 ~WallpaperPrivateSaveThumbnailFunction() {} |
| 841 | 842 |
| 842 bool WallpaperPrivateSaveThumbnailFunction::RunAsync() { | 843 bool WallpaperPrivateSaveThumbnailFunction::RunAsync() { |
| 843 scoped_ptr<save_thumbnail::Params> params( | 844 std::unique_ptr<save_thumbnail::Params> params( |
| 844 save_thumbnail::Params::Create(*args_)); | 845 save_thumbnail::Params::Create(*args_)); |
| 845 EXTENSION_FUNCTION_VALIDATE(params); | 846 EXTENSION_FUNCTION_VALIDATE(params); |
| 846 | 847 |
| 847 sequence_token_ = BrowserThread::GetBlockingPool()->GetNamedSequenceToken( | 848 sequence_token_ = BrowserThread::GetBlockingPool()->GetNamedSequenceToken( |
| 848 wallpaper::kWallpaperSequenceTokenName); | 849 wallpaper::kWallpaperSequenceTokenName); |
| 849 scoped_refptr<base::SequencedTaskRunner> task_runner = | 850 scoped_refptr<base::SequencedTaskRunner> task_runner = |
| 850 BrowserThread::GetBlockingPool()-> | 851 BrowserThread::GetBlockingPool()-> |
| 851 GetSequencedTaskRunnerWithShutdownBehavior(sequence_token_, | 852 GetSequencedTaskRunnerWithShutdownBehavior(sequence_token_, |
| 852 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); | 853 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); |
| 853 | 854 |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 930 this, file_list)); | 931 this, file_list)); |
| 931 } | 932 } |
| 932 | 933 |
| 933 void WallpaperPrivateGetOfflineWallpaperListFunction::OnComplete( | 934 void WallpaperPrivateGetOfflineWallpaperListFunction::OnComplete( |
| 934 const std::vector<std::string>& file_list) { | 935 const std::vector<std::string>& file_list) { |
| 935 base::ListValue* results = new base::ListValue(); | 936 base::ListValue* results = new base::ListValue(); |
| 936 results->AppendStrings(file_list); | 937 results->AppendStrings(file_list); |
| 937 SetResult(results); | 938 SetResult(results); |
| 938 SendResponse(true); | 939 SendResponse(true); |
| 939 } | 940 } |
| OLD | NEW |