1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/extensions/wallpaper_private_api.h"
#include <vector>
#include "ash/desktop_background/desktop_background_controller.h"
#include "ash/shell.h"
#include "ash/wm/mru_window_tracker.h"
#include "ash/wm/window_state.h"
#include "ash/wm/window_util.h"
#include "base/command_line.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_util.h"
#include "base/memory/scoped_ptr.h"
#include "base/path_service.h"
#include "base/prefs/pref_service.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/threading/worker_pool.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sync/profile_sync_service.h"
#include "chrome/browser/sync/profile_sync_service_factory.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/pref_names.h"
#include "chrome/grit/generated_resources.h"
#include "chromeos/chromeos_switches.h"
#include "components/user_manager/user.h"
#include "components/user_manager/user_manager.h"
#include "components/wallpaper/wallpaper_layout.h"
#include "content/public/browser/browser_thread.h"
#include "extensions/browser/event_router.h"
#include "grit/components_strings.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/webui/web_ui_util.h"
#include "ui/strings/grit/app_locale_settings.h"
#include "url/gurl.h"
using base::BinaryValue;
using content::BrowserThread;
namespace wallpaper_private = extensions::api::wallpaper_private;
namespace set_wallpaper_if_exists = wallpaper_private::SetWallpaperIfExists;
namespace set_wallpaper = wallpaper_private::SetWallpaper;
namespace set_custom_wallpaper = wallpaper_private::SetCustomWallpaper;
namespace set_custom_wallpaper_layout =
wallpaper_private::SetCustomWallpaperLayout;
namespace get_thumbnail = wallpaper_private::GetThumbnail;
namespace save_thumbnail = wallpaper_private::SaveThumbnail;
namespace get_offline_wallpaper_list =
wallpaper_private::GetOfflineWallpaperList;
namespace {
#if defined(GOOGLE_CHROME_BUILD)
const char kWallpaperManifestBaseURL[] =
"https://storage.googleapis.com/chromeos-wallpaper-public/manifest_";
#endif
bool IsOEMDefaultWallpaper() {
return base::CommandLine::ForCurrentProcess()->HasSwitch(
chromeos::switches::kDefaultWallpaperIsOem);
}
// Saves |data| as |file_name| to directory with |key|. Return false if the
// directory can not be found/created or failed to write file.
bool SaveData(int key, const std::string& file_name, const std::string& data) {
base::FilePath data_dir;
CHECK(PathService::Get(key, &data_dir));
if (!base::DirectoryExists(data_dir) &&
!base::CreateDirectory(data_dir)) {
return false;
}
base::FilePath file_path = data_dir.Append(file_name);
return base::PathExists(file_path) ||
(base::WriteFile(file_path, data.c_str(), data.size()) != -1);
}
// Gets |file_name| from directory with |key|. Return false if the directory can
// not be found or failed to read file to string |data|. Note if the |file_name|
// can not be found in the directory, return true with empty |data|. It is
// expected that we may try to access file which did not saved yet.
bool GetData(const base::FilePath& path, std::string* data) {
base::FilePath data_dir = path.DirName();
if (!base::DirectoryExists(data_dir) &&
!base::CreateDirectory(data_dir))
return false;
return !base::PathExists(path) ||
base::ReadFileToString(path, data);
}
// WindowStateManager remembers which windows have been minimized in order to
// restore them when the wallpaper viewer is hidden.
class WindowStateManager : public aura::WindowObserver {
public:
typedef std::map<std::string, std::set<aura::Window*> >
UserIDHashWindowListMap;
// Minimizes all windows except the active window.
static void MinimizeInactiveWindows(const std::string& user_id_hash);
// Unminimizes all minimized windows restoring them to their previous state.
// This should only be called after calling MinimizeInactiveWindows.
static void RestoreWindows(const std::string& user_id_hash);
private:
WindowStateManager();
virtual ~WindowStateManager();
// Store all unminimized windows except |active_window| and minimize them.
// All the windows are saved in a map and the key value is |user_id_hash|.
void BuildWindowListAndMinimizeInactiveForUser(
const std::string& user_id_hash, aura::Window* active_window);
// Unminimize all the stored windows for |user_id_hash|.
void RestoreMinimizedWindows(const std::string& user_id_hash);
// Remove the observer from |window| if |window| is no longer referenced in
// user_id_hash_window_list_map_.
void RemoveObserverIfUnreferenced(aura::Window* window);
// aura::WindowObserver overrides.
virtual void OnWindowDestroyed(aura::Window* window) override;
// Map of user id hash and associated list of minimized windows.
UserIDHashWindowListMap user_id_hash_window_list_map_;
DISALLOW_COPY_AND_ASSIGN(WindowStateManager);
};
// static
WindowStateManager* g_window_state_manager = NULL;
// static
void WindowStateManager::MinimizeInactiveWindows(
const std::string& user_id_hash) {
if (!g_window_state_manager)
g_window_state_manager = new WindowStateManager();
g_window_state_manager->BuildWindowListAndMinimizeInactiveForUser(
user_id_hash, ash::wm::GetActiveWindow());
}
// static
void WindowStateManager::RestoreWindows(const std::string& user_id_hash) {
if (!g_window_state_manager) {
DCHECK(false) << "This should only be called after calling "
<< "MinimizeInactiveWindows.";
return;
}
g_window_state_manager->RestoreMinimizedWindows(user_id_hash);
if (g_window_state_manager->user_id_hash_window_list_map_.empty()) {
delete g_window_state_manager;
g_window_state_manager = NULL;
}
}
WindowStateManager::WindowStateManager() {}
WindowStateManager::~WindowStateManager() {}
void WindowStateManager::BuildWindowListAndMinimizeInactiveForUser(
const std::string& user_id_hash, aura::Window* active_window) {
if (user_id_hash_window_list_map_.find(user_id_hash) ==
user_id_hash_window_list_map_.end()) {
user_id_hash_window_list_map_[user_id_hash] = std::set<aura::Window*>();
}
std::set<aura::Window*>* results =
&user_id_hash_window_list_map_[user_id_hash];
std::vector<aura::Window*> windows = ash::MruWindowTracker::BuildWindowList();
for (std::vector<aura::Window*>::iterator iter = windows.begin();
iter != windows.end(); ++iter) {
// Ignore active window and minimized windows.
if (*iter == active_window || ash::wm::GetWindowState(*iter)->IsMinimized())
continue;
// TODO(bshe): Add WindowStateObserver too. http://crbug.com/323252
if (!(*iter)->HasObserver(this))
(*iter)->AddObserver(this);
results->insert(*iter);
ash::wm::GetWindowState(*iter)->Minimize();
}
}
void WindowStateManager::RestoreMinimizedWindows(
const std::string& user_id_hash) {
UserIDHashWindowListMap::iterator it =
user_id_hash_window_list_map_.find(user_id_hash);
if (it == user_id_hash_window_list_map_.end()) {
DCHECK(false) << "This should only be called after calling "
<< "MinimizeInactiveWindows.";
return;
}
std::set<aura::Window*> removed_windows;
removed_windows.swap(it->second);
user_id_hash_window_list_map_.erase(it);
for (std::set<aura::Window*>::iterator iter = removed_windows.begin();
iter != removed_windows.end(); ++iter) {
ash::wm::GetWindowState(*iter)->Unminimize();
RemoveObserverIfUnreferenced(*iter);
}
}
void WindowStateManager::RemoveObserverIfUnreferenced(aura::Window* window) {
for (UserIDHashWindowListMap::iterator iter =
user_id_hash_window_list_map_.begin();
iter != user_id_hash_window_list_map_.end();
++iter) {
if (iter->second.find(window) != iter->second.end())
return;
}
// Remove observer if |window| is not observed by any users.
window->RemoveObserver(this);
}
void WindowStateManager::OnWindowDestroyed(aura::Window* window) {
for (UserIDHashWindowListMap::iterator iter =
user_id_hash_window_list_map_.begin();
iter != user_id_hash_window_list_map_.end();
++iter) {
iter->second.erase(window);
}
}
} // namespace
bool WallpaperPrivateGetStringsFunction::RunSync() {
base::DictionaryValue* dict = new base::DictionaryValue();
SetResult(dict);
#define SET_STRING(id, idr) \
dict->SetString(id, l10n_util::GetStringUTF16(idr))
SET_STRING("webFontFamily", IDS_WEB_FONT_FAMILY);
SET_STRING("webFontSize", IDS_WEB_FONT_SIZE);
SET_STRING("allCategoryLabel", IDS_WALLPAPER_MANAGER_ALL_CATEGORY_LABEL);
SET_STRING("deleteCommandLabel", IDS_WALLPAPER_MANAGER_DELETE_COMMAND_LABEL);
SET_STRING("customCategoryLabel",
IDS_WALLPAPER_MANAGER_CUSTOM_CATEGORY_LABEL);
SET_STRING("selectCustomLabel",
IDS_WALLPAPER_MANAGER_SELECT_CUSTOM_LABEL);
SET_STRING("positionLabel", IDS_WALLPAPER_MANAGER_POSITION_LABEL);
SET_STRING("colorLabel", IDS_WALLPAPER_MANAGER_COLOR_LABEL);
SET_STRING("centerCroppedLayout",
IDS_OPTIONS_WALLPAPER_CENTER_CROPPED_LAYOUT);
SET_STRING("centerLayout", IDS_OPTIONS_WALLPAPER_CENTER_LAYOUT);
SET_STRING("stretchLayout", IDS_OPTIONS_WALLPAPER_STRETCH_LAYOUT);
SET_STRING("connectionFailed", IDS_WALLPAPER_MANAGER_ACCESS_FAIL);
SET_STRING("downloadFailed", IDS_WALLPAPER_MANAGER_DOWNLOAD_FAIL);
SET_STRING("downloadCanceled", IDS_WALLPAPER_MANAGER_DOWNLOAD_CANCEL);
SET_STRING("customWallpaperWarning",
IDS_WALLPAPER_MANAGER_SHOW_CUSTOM_WALLPAPER_ON_START_WARNING);
SET_STRING("accessFileFailure", IDS_WALLPAPER_MANAGER_ACCESS_FILE_FAILURE);
SET_STRING("invalidWallpaper", IDS_WALLPAPER_MANAGER_INVALID_WALLPAPER);
SET_STRING("surpriseMeLabel", IDS_WALLPAPER_MANAGER_SURPRISE_ME_LABEL);
SET_STRING("learnMore", IDS_LEARN_MORE);
SET_STRING("currentWallpaperSetByMessage",
IDS_CURRENT_WALLPAPER_SET_BY_MESSAGE);
#undef SET_STRING
webui::SetFontAndTextDirection(dict);
chromeos::WallpaperManager* wallpaper_manager =
chromeos::WallpaperManager::Get();
wallpaper::WallpaperInfo info;
if (wallpaper_manager->GetLoggedInUserWallpaperInfo(&info))
dict->SetString("currentWallpaper", info.location);
#if defined(GOOGLE_CHROME_BUILD)
dict->SetString("manifestBaseURL", kWallpaperManifestBaseURL);
#endif
Profile* profile = Profile::FromBrowserContext(browser_context());
std::string app_name(
profile->GetPrefs()->GetString(prefs::kCurrentWallpaperAppName));
if (!app_name.empty())
dict->SetString("wallpaperAppName", app_name);
dict->SetBoolean("isOEMDefaultWallpaper", IsOEMDefaultWallpaper());
dict->SetString("canceledWallpaper",
wallpaper_api_util::kCancelWallpaperMessage);
return true;
}
bool WallpaperPrivateGetSyncSettingFunction::RunSync() {
Profile* profile = Profile::FromBrowserContext(browser_context());
ProfileSyncService* sync =
ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile);
base::DictionaryValue* dict = new base::DictionaryValue();
SetResult(dict);
dict->SetBoolean("syncThemes",
sync->GetActiveDataTypes().Has(syncer::THEMES));
return true;
}
WallpaperPrivateSetWallpaperIfExistsFunction::
WallpaperPrivateSetWallpaperIfExistsFunction() {}
WallpaperPrivateSetWallpaperIfExistsFunction::
~WallpaperPrivateSetWallpaperIfExistsFunction() {}
bool WallpaperPrivateSetWallpaperIfExistsFunction::RunAsync() {
#if !defined(USE_ATHENA)
// TODO(bshe): Support wallpaper manager in Athena, crbug.com/408734.
params = set_wallpaper_if_exists::Params::Create(*args_);
EXTENSION_FUNCTION_VALIDATE(params);
user_id_ = user_manager::UserManager::Get()->GetActiveUser()->email();
base::FilePath wallpaper_path;
base::FilePath fallback_path;
chromeos::WallpaperManager::WallpaperResolution resolution =
chromeos::WallpaperManager::Get()->GetAppropriateResolution();
std::string file_name = GURL(params->url).ExtractFileName();
CHECK(PathService::Get(chrome::DIR_CHROMEOS_WALLPAPERS,
&wallpaper_path));
fallback_path = wallpaper_path.Append(file_name);
if (params->layout != wallpaper_private::WALLPAPER_LAYOUT_STRETCH &&
resolution == chromeos::WallpaperManager::WALLPAPER_RESOLUTION_SMALL) {
file_name = base::FilePath(file_name)
.InsertBeforeExtension(wallpaper::kSmallWallpaperSuffix)
.value();
}
wallpaper_path = wallpaper_path.Append(file_name);
sequence_token_ = BrowserThread::GetBlockingPool()->GetNamedSequenceToken(
wallpaper::kWallpaperSequenceTokenName);
scoped_refptr<base::SequencedTaskRunner> task_runner =
BrowserThread::GetBlockingPool()->
GetSequencedTaskRunnerWithShutdownBehavior(sequence_token_,
base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN);
task_runner->PostTask(FROM_HERE,
base::Bind(
&WallpaperPrivateSetWallpaperIfExistsFunction::
ReadFileAndInitiateStartDecode,
this, wallpaper_path, fallback_path));
#endif
return true;
}
void WallpaperPrivateSetWallpaperIfExistsFunction::
ReadFileAndInitiateStartDecode(const base::FilePath& file_path,
const base::FilePath& fallback_path) {
DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread(
sequence_token_));
std::string data;
base::FilePath path = file_path;
if (!base::PathExists(file_path))
path = fallback_path;
if (base::PathExists(path) &&
base::ReadFileToString(path, &data)) {
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(&WallpaperPrivateSetWallpaperIfExistsFunction::StartDecode,
this, data));
return;
}
std::string error = base::StringPrintf(
"Failed to set wallpaper %s from file system.",
path.BaseName().value().c_str());
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(&WallpaperPrivateSetWallpaperIfExistsFunction::OnFileNotExists,
this, error));
}
void WallpaperPrivateSetWallpaperIfExistsFunction::OnWallpaperDecoded(
const gfx::ImageSkia& image) {
// Set unsafe_wallpaper_decoder_ to null since the decoding already finished.
unsafe_wallpaper_decoder_ = NULL;
chromeos::WallpaperManager* wallpaper_manager =
chromeos::WallpaperManager::Get();
wallpaper::WallpaperLayout layout = wallpaper_api_util::GetLayoutEnum(
wallpaper_private::ToString(params->layout));
bool update_wallpaper =
user_id_ == user_manager::UserManager::Get()->GetActiveUser()->email();
wallpaper_manager->SetWallpaperFromImageSkia(
user_id_, image, layout, update_wallpaper);
bool is_persistent = !user_manager::UserManager::Get()
->IsCurrentUserNonCryptohomeDataEphemeral();
wallpaper::WallpaperInfo info = {params->url,
layout,
user_manager::User::ONLINE,
base::Time::Now().LocalMidnight()};
wallpaper_manager->SetUserWallpaperInfo(user_id_, info, is_persistent);
SetResult(new base::FundamentalValue(true));
Profile* profile = Profile::FromBrowserContext(browser_context());
// This API is only available to the component wallpaper picker. We do not
// need to show the app's name if it is the component wallpaper picker. So set
// the pref to empty string.
profile->GetPrefs()->SetString(prefs::kCurrentWallpaperAppName,
std::string());
SendResponse(true);
}
void WallpaperPrivateSetWallpaperIfExistsFunction::OnFileNotExists(
const std::string& error) {
SetResult(new base::FundamentalValue(false));
OnFailure(error);
}
WallpaperPrivateSetWallpaperFunction::WallpaperPrivateSetWallpaperFunction() {
}
WallpaperPrivateSetWallpaperFunction::~WallpaperPrivateSetWallpaperFunction() {
}
bool WallpaperPrivateSetWallpaperFunction::RunAsync() {
params = set_wallpaper::Params::Create(*args_);
EXTENSION_FUNCTION_VALIDATE(params);
// Gets email address while at UI thread.
user_id_ = user_manager::UserManager::Get()->GetActiveUser()->email();
StartDecode(params->wallpaper);
return true;
}
void WallpaperPrivateSetWallpaperFunction::OnWallpaperDecoded(
const gfx::ImageSkia& image) {
wallpaper_ = image;
// Set unsafe_wallpaper_decoder_ to null since the decoding already finished.
unsafe_wallpaper_decoder_ = NULL;
sequence_token_ = BrowserThread::GetBlockingPool()->GetNamedSequenceToken(
wallpaper::kWallpaperSequenceTokenName);
scoped_refptr<base::SequencedTaskRunner> task_runner =
BrowserThread::GetBlockingPool()->
GetSequencedTaskRunnerWithShutdownBehavior(sequence_token_,
base::SequencedWorkerPool::BLOCK_SHUTDOWN);
task_runner->PostTask(FROM_HERE,
base::Bind(&WallpaperPrivateSetWallpaperFunction::SaveToFile, this));
}
void WallpaperPrivateSetWallpaperFunction::SaveToFile() {
DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread(
sequence_token_));
std::string file_name = GURL(params->url).ExtractFileName();
if (SaveData(chrome::DIR_CHROMEOS_WALLPAPERS, file_name, params->wallpaper)) {
wallpaper_.EnsureRepsForSupportedScales();
scoped_ptr<gfx::ImageSkia> deep_copy(wallpaper_.DeepCopy());
// ImageSkia is not RefCountedThreadSafe. Use a deep copied ImageSkia if
// post to another thread.
BrowserThread::PostTask(
BrowserThread::UI,
FROM_HERE,
base::Bind(&WallpaperPrivateSetWallpaperFunction::SetDecodedWallpaper,
this,
base::Passed(deep_copy.Pass())));
base::FilePath wallpaper_dir;
CHECK(PathService::Get(chrome::DIR_CHROMEOS_WALLPAPERS, &wallpaper_dir));
base::FilePath file_path =
wallpaper_dir.Append(file_name)
.InsertBeforeExtension(wallpaper::kSmallWallpaperSuffix);
if (base::PathExists(file_path))
return;
// Generates and saves small resolution wallpaper. Uses CENTER_CROPPED to
// maintain the aspect ratio after resize.
chromeos::WallpaperManager::Get()->ResizeAndSaveWallpaper(
wallpaper_, file_path, wallpaper::WALLPAPER_LAYOUT_CENTER_CROPPED,
wallpaper::kSmallWallpaperMaxWidth, wallpaper::kSmallWallpaperMaxHeight,
NULL);
} else {
std::string error = base::StringPrintf(
"Failed to create/write wallpaper to %s.", file_name.c_str());
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(&WallpaperPrivateSetWallpaperFunction::OnFailure,
this, error));
}
}
void WallpaperPrivateSetWallpaperFunction::SetDecodedWallpaper(
scoped_ptr<gfx::ImageSkia> image) {
chromeos::WallpaperManager* wallpaper_manager =
chromeos::WallpaperManager::Get();
wallpaper::WallpaperLayout layout = wallpaper_api_util::GetLayoutEnum(
wallpaper_private::ToString(params->layout));
bool update_wallpaper =
user_id_ == user_manager::UserManager::Get()->GetActiveUser()->email();
wallpaper_manager->SetWallpaperFromImageSkia(
user_id_, *image.get(), layout, update_wallpaper);
bool is_persistent = !user_manager::UserManager::Get()
->IsCurrentUserNonCryptohomeDataEphemeral();
wallpaper::WallpaperInfo info = {params->url,
layout,
user_manager::User::ONLINE,
base::Time::Now().LocalMidnight()};
Profile* profile = Profile::FromBrowserContext(browser_context());
// This API is only available to the component wallpaper picker. We do not
// need to show the app's name if it is the component wallpaper picker. So set
// the pref to empty string.
profile->GetPrefs()->SetString(prefs::kCurrentWallpaperAppName,
std::string());
wallpaper_manager->SetUserWallpaperInfo(user_id_, info, is_persistent);
SendResponse(true);
}
WallpaperPrivateResetWallpaperFunction::
WallpaperPrivateResetWallpaperFunction() {}
WallpaperPrivateResetWallpaperFunction::
~WallpaperPrivateResetWallpaperFunction() {}
bool WallpaperPrivateResetWallpaperFunction::RunAsync() {
chromeos::WallpaperManager* wallpaper_manager =
chromeos::WallpaperManager::Get();
user_manager::UserManager* user_manager = user_manager::UserManager::Get();
std::string user_id = user_manager->GetActiveUser()->email();
wallpaper_manager->RemoveUserWallpaperInfo(user_id);
wallpaper::WallpaperInfo info = {std::string(),
wallpaper::WALLPAPER_LAYOUT_CENTER,
user_manager::User::DEFAULT,
base::Time::Now().LocalMidnight()};
bool is_persistent =
!user_manager->IsCurrentUserNonCryptohomeDataEphemeral();
wallpaper_manager->SetUserWallpaperInfo(user_id, info, is_persistent);
wallpaper_manager->SetDefaultWallpaperNow(user_id);
Profile* profile = Profile::FromBrowserContext(browser_context());
// This API is only available to the component wallpaper picker. We do not
// need to show the app's name if it is the component wallpaper picker. So set
// the pref to empty string.
profile->GetPrefs()->SetString(prefs::kCurrentWallpaperAppName,
std::string());
return true;
}
WallpaperPrivateSetCustomWallpaperFunction::
WallpaperPrivateSetCustomWallpaperFunction() {}
WallpaperPrivateSetCustomWallpaperFunction::
~WallpaperPrivateSetCustomWallpaperFunction() {}
bool WallpaperPrivateSetCustomWallpaperFunction::RunAsync() {
params = set_custom_wallpaper::Params::Create(*args_);
EXTENSION_FUNCTION_VALIDATE(params);
// Gets email address and username hash while at UI thread.
user_id_ = user_manager::UserManager::Get()->GetActiveUser()->email();
user_id_hash_ =
user_manager::UserManager::Get()->GetActiveUser()->username_hash();
StartDecode(params->wallpaper);
return true;
}
void WallpaperPrivateSetCustomWallpaperFunction::OnWallpaperDecoded(
const gfx::ImageSkia& image) {
chromeos::WallpaperManager* wallpaper_manager =
chromeos::WallpaperManager::Get();
base::FilePath thumbnail_path = wallpaper_manager->GetCustomWallpaperPath(
wallpaper::kThumbnailWallpaperSubDir, user_id_hash_, params->file_name);
sequence_token_ = BrowserThread::GetBlockingPool()->GetNamedSequenceToken(
wallpaper::kWallpaperSequenceTokenName);
scoped_refptr<base::SequencedTaskRunner> task_runner =
BrowserThread::GetBlockingPool()->
GetSequencedTaskRunnerWithShutdownBehavior(sequence_token_,
base::SequencedWorkerPool::BLOCK_SHUTDOWN);
wallpaper::WallpaperLayout layout = wallpaper_api_util::GetLayoutEnum(
wallpaper_private::ToString(params->layout));
bool update_wallpaper =
user_id_ == user_manager::UserManager::Get()->GetActiveUser()->email();
wallpaper_manager->SetCustomWallpaper(user_id_,
user_id_hash_,
params->file_name,
layout,
user_manager::User::CUSTOMIZED,
image,
update_wallpaper);
unsafe_wallpaper_decoder_ = NULL;
Profile* profile = Profile::FromBrowserContext(browser_context());
// This API is only available to the component wallpaper picker. We do not
// need to show the app's name if it is the component wallpaper picker. So set
// the pref to empty string.
profile->GetPrefs()->SetString(prefs::kCurrentWallpaperAppName,
std::string());
if (params->generate_thumbnail) {
image.EnsureRepsForSupportedScales();
scoped_ptr<gfx::ImageSkia> deep_copy(image.DeepCopy());
// Generates thumbnail before call api function callback. We can then
// request thumbnail in the javascript callback.
task_runner->PostTask(FROM_HERE,
base::Bind(
&WallpaperPrivateSetCustomWallpaperFunction::GenerateThumbnail,
this, thumbnail_path, base::Passed(&deep_copy)));
} else {
SendResponse(true);
}
}
void WallpaperPrivateSetCustomWallpaperFunction::GenerateThumbnail(
const base::FilePath& thumbnail_path, scoped_ptr<gfx::ImageSkia> image) {
DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread(
sequence_token_));
if (!base::PathExists(thumbnail_path.DirName()))
base::CreateDirectory(thumbnail_path.DirName());
scoped_refptr<base::RefCountedBytes> data;
chromeos::WallpaperManager::Get()->ResizeImage(
*image, wallpaper::WALLPAPER_LAYOUT_STRETCH,
wallpaper::kWallpaperThumbnailWidth, wallpaper::kWallpaperThumbnailHeight,
&data, NULL);
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(
&WallpaperPrivateSetCustomWallpaperFunction::ThumbnailGenerated,
this, data));
}
void WallpaperPrivateSetCustomWallpaperFunction::ThumbnailGenerated(
base::RefCountedBytes* data) {
BinaryValue* result = BinaryValue::CreateWithCopiedBuffer(
reinterpret_cast<const char*>(data->front()), data->size());
SetResult(result);
SendResponse(true);
}
WallpaperPrivateSetCustomWallpaperLayoutFunction::
WallpaperPrivateSetCustomWallpaperLayoutFunction() {}
WallpaperPrivateSetCustomWallpaperLayoutFunction::
~WallpaperPrivateSetCustomWallpaperLayoutFunction() {}
bool WallpaperPrivateSetCustomWallpaperLayoutFunction::RunAsync() {
scoped_ptr<set_custom_wallpaper_layout::Params> params(
set_custom_wallpaper_layout::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params);
chromeos::WallpaperManager* wallpaper_manager =
chromeos::WallpaperManager::Get();
wallpaper::WallpaperInfo info;
wallpaper_manager->GetLoggedInUserWallpaperInfo(&info);
if (info.type != user_manager::User::CUSTOMIZED) {
SetError("Only custom wallpaper can change layout.");
SendResponse(false);
return false;
}
info.layout = wallpaper_api_util::GetLayoutEnum(
wallpaper_private::ToString(params->layout));
std::string email =
user_manager::UserManager::Get()->GetActiveUser()->email();
bool is_persistent = !user_manager::UserManager::Get()
->IsCurrentUserNonCryptohomeDataEphemeral();
wallpaper_manager->SetUserWallpaperInfo(email, info, is_persistent);
wallpaper_manager->UpdateWallpaper(false /* clear_cache */);
SendResponse(true);
// Gets email address while at UI thread.
return true;
}
WallpaperPrivateMinimizeInactiveWindowsFunction::
WallpaperPrivateMinimizeInactiveWindowsFunction() {
}
WallpaperPrivateMinimizeInactiveWindowsFunction::
~WallpaperPrivateMinimizeInactiveWindowsFunction() {
}
bool WallpaperPrivateMinimizeInactiveWindowsFunction::RunAsync() {
WindowStateManager::MinimizeInactiveWindows(
user_manager::UserManager::Get()->GetActiveUser()->username_hash());
return true;
}
WallpaperPrivateRestoreMinimizedWindowsFunction::
WallpaperPrivateRestoreMinimizedWindowsFunction() {
}
WallpaperPrivateRestoreMinimizedWindowsFunction::
~WallpaperPrivateRestoreMinimizedWindowsFunction() {
}
bool WallpaperPrivateRestoreMinimizedWindowsFunction::RunAsync() {
WindowStateManager::RestoreWindows(
user_manager::UserManager::Get()->GetActiveUser()->username_hash());
return true;
}
WallpaperPrivateGetThumbnailFunction::WallpaperPrivateGetThumbnailFunction() {
}
WallpaperPrivateGetThumbnailFunction::~WallpaperPrivateGetThumbnailFunction() {
}
bool WallpaperPrivateGetThumbnailFunction::RunAsync() {
scoped_ptr<get_thumbnail::Params> params(
get_thumbnail::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params);
base::FilePath thumbnail_path;
std::string email =
user_manager::UserManager::Get()->GetActiveUser()->email();
if (params->source == get_thumbnail::Params::SOURCE_ONLINE) {
std::string file_name = GURL(params->url_or_file).ExtractFileName();
CHECK(PathService::Get(chrome::DIR_CHROMEOS_WALLPAPER_THUMBNAILS,
&thumbnail_path));
thumbnail_path = thumbnail_path.Append(file_name);
} else {
if (!IsOEMDefaultWallpaper()) {
SetError("No OEM wallpaper.");
SendResponse(false);
return false;
}
// TODO(bshe): Small resolution wallpaper is used here as wallpaper
// thumbnail. We should either resize it or include a wallpaper thumbnail in
// addition to large and small wallpaper resolutions.
thumbnail_path = base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(
chromeos::switches::kDefaultWallpaperSmall);
}
sequence_token_ = BrowserThread::GetBlockingPool()->GetNamedSequenceToken(
wallpaper::kWallpaperSequenceTokenName);
scoped_refptr<base::SequencedTaskRunner> task_runner =
BrowserThread::GetBlockingPool()->
GetSequencedTaskRunnerWithShutdownBehavior(sequence_token_,
base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN);
task_runner->PostTask(FROM_HERE,
base::Bind(&WallpaperPrivateGetThumbnailFunction::Get, this,
thumbnail_path));
return true;
}
void WallpaperPrivateGetThumbnailFunction::Failure(
const std::string& file_name) {
SetError(base::StringPrintf("Failed to access wallpaper thumbnails for %s.",
file_name.c_str()));
SendResponse(false);
}
void WallpaperPrivateGetThumbnailFunction::FileNotLoaded() {
SendResponse(true);
}
void WallpaperPrivateGetThumbnailFunction::FileLoaded(
const std::string& data) {
BinaryValue* thumbnail = BinaryValue::CreateWithCopiedBuffer(data.c_str(),
data.size());
SetResult(thumbnail);
SendResponse(true);
}
void WallpaperPrivateGetThumbnailFunction::Get(const base::FilePath& path) {
DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread(
sequence_token_));
std::string data;
if (GetData(path, &data)) {
if (data.empty()) {
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(&WallpaperPrivateGetThumbnailFunction::FileNotLoaded, this));
} else {
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(&WallpaperPrivateGetThumbnailFunction::FileLoaded, this,
data));
}
} else {
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(&WallpaperPrivateGetThumbnailFunction::Failure, this,
path.BaseName().value()));
}
}
WallpaperPrivateSaveThumbnailFunction::WallpaperPrivateSaveThumbnailFunction() {
}
WallpaperPrivateSaveThumbnailFunction::
~WallpaperPrivateSaveThumbnailFunction() {}
bool WallpaperPrivateSaveThumbnailFunction::RunAsync() {
scoped_ptr<save_thumbnail::Params> params(
save_thumbnail::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params);
sequence_token_ = BrowserThread::GetBlockingPool()->GetNamedSequenceToken(
wallpaper::kWallpaperSequenceTokenName);
scoped_refptr<base::SequencedTaskRunner> task_runner =
BrowserThread::GetBlockingPool()->
GetSequencedTaskRunnerWithShutdownBehavior(sequence_token_,
base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN);
task_runner->PostTask(FROM_HERE,
base::Bind(&WallpaperPrivateSaveThumbnailFunction::Save,
this, params->data, GURL(params->url).ExtractFileName()));
return true;
}
void WallpaperPrivateSaveThumbnailFunction::Failure(
const std::string& file_name) {
SetError(base::StringPrintf("Failed to create/write thumbnail of %s.",
file_name.c_str()));
SendResponse(false);
}
void WallpaperPrivateSaveThumbnailFunction::Success() {
SendResponse(true);
}
void WallpaperPrivateSaveThumbnailFunction::Save(const std::string& data,
const std::string& file_name) {
DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread(
sequence_token_));
if (SaveData(chrome::DIR_CHROMEOS_WALLPAPER_THUMBNAILS, file_name, data)) {
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(&WallpaperPrivateSaveThumbnailFunction::Success, this));
} else {
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(&WallpaperPrivateSaveThumbnailFunction::Failure,
this, file_name));
}
}
WallpaperPrivateGetOfflineWallpaperListFunction::
WallpaperPrivateGetOfflineWallpaperListFunction() {
}
WallpaperPrivateGetOfflineWallpaperListFunction::
~WallpaperPrivateGetOfflineWallpaperListFunction() {
}
bool WallpaperPrivateGetOfflineWallpaperListFunction::RunAsync() {
sequence_token_ = BrowserThread::GetBlockingPool()->GetNamedSequenceToken(
wallpaper::kWallpaperSequenceTokenName);
scoped_refptr<base::SequencedTaskRunner> task_runner =
BrowserThread::GetBlockingPool()->
GetSequencedTaskRunnerWithShutdownBehavior(sequence_token_,
base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN);
task_runner->PostTask(FROM_HERE,
base::Bind(&WallpaperPrivateGetOfflineWallpaperListFunction::GetList,
this));
return true;
}
void WallpaperPrivateGetOfflineWallpaperListFunction::GetList() {
DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread(
sequence_token_));
std::vector<std::string> file_list;
base::FilePath wallpaper_dir;
CHECK(PathService::Get(chrome::DIR_CHROMEOS_WALLPAPERS, &wallpaper_dir));
if (base::DirectoryExists(wallpaper_dir)) {
base::FileEnumerator files(wallpaper_dir, false,
base::FileEnumerator::FILES);
for (base::FilePath current = files.Next(); !current.empty();
current = files.Next()) {
std::string file_name = current.BaseName().RemoveExtension().value();
// Do not add file name of small resolution wallpaper to the list.
if (!EndsWith(file_name, wallpaper::kSmallWallpaperSuffix, true))
file_list.push_back(current.BaseName().value());
}
}
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(&WallpaperPrivateGetOfflineWallpaperListFunction::OnComplete,
this, file_list));
}
void WallpaperPrivateGetOfflineWallpaperListFunction::OnComplete(
const std::vector<std::string>& file_list) {
base::ListValue* results = new base::ListValue();
results->AppendStrings(file_list);
SetResult(results);
SendResponse(true);
}
|