| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/ui/webui/options/chromeos/set_wallpaper_options_handler
.h" | |
| 6 | |
| 7 #include "ash/desktop_background/desktop_background_controller.h" | |
| 8 #include "ash/desktop_background/user_wallpaper_delegate.h" | |
| 9 #include "ash/shell.h" | |
| 10 #include "base/bind.h" | |
| 11 #include "base/bind_helpers.h" | |
| 12 #include "base/metrics/histogram.h" | |
| 13 #include "base/path_service.h" | |
| 14 #include "base/string_number_conversions.h" | |
| 15 #include "base/string_util.h" | |
| 16 #include "base/values.h" | |
| 17 #include "chrome/browser/chromeos/login/user_manager.h" | |
| 18 #include "chrome/browser/chromeos/login/wallpaper_manager.h" | |
| 19 #include "chrome/browser/ui/browser_finder.h" | |
| 20 #include "chrome/browser/ui/browser_window.h" | |
| 21 #include "chrome/browser/ui/chrome_select_file_policy.h" | |
| 22 #include "chrome/browser/ui/webui/options/chromeos/wallpaper_thumbnail_source.h" | |
| 23 #include "chrome/browser/ui/webui/web_ui_util.h" | |
| 24 #include "chrome/common/chrome_paths.h" | |
| 25 #include "content/public/browser/web_ui.h" | |
| 26 #include "grit/generated_resources.h" | |
| 27 #include "ui/base/l10n/l10n_util.h" | |
| 28 #include "ui/base/resource/resource_bundle.h" | |
| 29 #include "ui/views/widget/widget.h" | |
| 30 | |
| 31 namespace chromeos { | |
| 32 namespace options { | |
| 33 | |
| 34 namespace { | |
| 35 | |
| 36 // Returns info about extensions for files we support as wallpaper images. | |
| 37 ui::SelectFileDialog::FileTypeInfo GetUserImageFileTypeInfo() { | |
| 38 ui::SelectFileDialog::FileTypeInfo file_type_info; | |
| 39 file_type_info.extensions.resize(1); | |
| 40 | |
| 41 file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("jpg")); | |
| 42 file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("jpeg")); | |
| 43 | |
| 44 file_type_info.extension_description_overrides.resize(1); | |
| 45 file_type_info.extension_description_overrides[0] = | |
| 46 l10n_util::GetStringUTF16(IDS_IMAGE_FILES); | |
| 47 | |
| 48 file_type_info.support_gdata = true; | |
| 49 return file_type_info; | |
| 50 } | |
| 51 | |
| 52 } // namespace | |
| 53 | |
| 54 SetWallpaperOptionsHandler::SetWallpaperOptionsHandler() | |
| 55 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | |
| 56 } | |
| 57 | |
| 58 SetWallpaperOptionsHandler::~SetWallpaperOptionsHandler() { | |
| 59 if (select_file_dialog_.get()) | |
| 60 select_file_dialog_->ListenerDestroyed(); | |
| 61 } | |
| 62 | |
| 63 void SetWallpaperOptionsHandler::GetLocalizedValues( | |
| 64 DictionaryValue* localized_strings) { | |
| 65 DCHECK(localized_strings); | |
| 66 localized_strings->SetString("setWallpaperPage", | |
| 67 l10n_util::GetStringUTF16(IDS_OPTIONS_SET_WALLPAPER_DIALOG_TITLE)); | |
| 68 localized_strings->SetString("setWallpaperPageDescription", | |
| 69 l10n_util::GetStringUTF16(IDS_OPTIONS_SET_WALLPAPER_DIALOG_TEXT)); | |
| 70 localized_strings->SetString("setWallpaperAuthor", | |
| 71 l10n_util::GetStringUTF16(IDS_OPTIONS_SET_WALLPAPER_AUTHOR_TEXT)); | |
| 72 localized_strings->SetString("dailyWallpaperLabel", | |
| 73 l10n_util::GetStringUTF16(IDS_OPTIONS_SET_WALLPAPER_DAILY)); | |
| 74 localized_strings->SetString("customWallpaper", | |
| 75 l10n_util::GetStringUTF16(IDS_OPTIONS_CUSTOME_WALLPAPER)); | |
| 76 } | |
| 77 | |
| 78 void SetWallpaperOptionsHandler::RegisterMessages() { | |
| 79 web_ui()->RegisterMessageCallback("onSetWallpaperPageInitialized", | |
| 80 base::Bind(&SetWallpaperOptionsHandler::HandlePageInitialized, | |
| 81 base::Unretained(this))); | |
| 82 web_ui()->RegisterMessageCallback("onSetWallpaperPageShown", | |
| 83 base::Bind(&SetWallpaperOptionsHandler::HandlePageShown, | |
| 84 base::Unretained(this))); | |
| 85 web_ui()->RegisterMessageCallback("selectDefaultWallpaper", | |
| 86 base::Bind(&SetWallpaperOptionsHandler::HandleDefaultWallpaper, | |
| 87 base::Unretained(this))); | |
| 88 web_ui()->RegisterMessageCallback("selectDailyWallpaper", | |
| 89 base::Bind(&SetWallpaperOptionsHandler::HandleDailyWallpaper, | |
| 90 base::Unretained(this))); | |
| 91 web_ui()->RegisterMessageCallback("chooseWallpaper", | |
| 92 base::Bind(&SetWallpaperOptionsHandler::HandleChooseFile, | |
| 93 base::Unretained(this))); | |
| 94 web_ui()->RegisterMessageCallback("changeWallpaperLayout", | |
| 95 base::Bind(&SetWallpaperOptionsHandler::HandleLayoutChanged, | |
| 96 base::Unretained(this))); | |
| 97 } | |
| 98 | |
| 99 void SetWallpaperOptionsHandler::SetCustomWallpaperThumbnail() { | |
| 100 web_ui()->CallJavascriptFunction("SetWallpaperOptions.setCustomImage"); | |
| 101 } | |
| 102 | |
| 103 void SetWallpaperOptionsHandler::FileSelected(const FilePath& path, | |
| 104 int index, | |
| 105 void* params) { | |
| 106 UserManager* user_manager = UserManager::Get(); | |
| 107 | |
| 108 // Default wallpaper layout is CENTER_CROPPED. | |
| 109 WallpaperManager::Get()->SetUserWallpaperFromFile( | |
| 110 user_manager->GetLoggedInUser()->email(), path, ash::CENTER_CROPPED, | |
| 111 weak_factory_.GetWeakPtr()); | |
| 112 web_ui()->CallJavascriptFunction("SetWallpaperOptions.didSelectFile"); | |
| 113 } | |
| 114 | |
| 115 void SetWallpaperOptionsHandler::SendDefaultImages() { | |
| 116 ListValue images; | |
| 117 DictionaryValue* image_detail; | |
| 118 ash::WallpaperInfo image_info; | |
| 119 | |
| 120 for (int i = 0; i < ash::GetWallpaperCount(); ++i) { | |
| 121 images.Append(image_detail = new DictionaryValue()); | |
| 122 image_info = ash::GetWallpaperInfo(i); | |
| 123 } | |
| 124 | |
| 125 web_ui()->CallJavascriptFunction("SetWallpaperOptions.setDefaultImages", | |
| 126 images); | |
| 127 } | |
| 128 | |
| 129 void SetWallpaperOptionsHandler::SendLayoutOptions( | |
| 130 ash::WallpaperLayout layout) { | |
| 131 ListValue layouts; | |
| 132 DictionaryValue* entry; | |
| 133 | |
| 134 layouts.Append(entry = new DictionaryValue()); | |
| 135 entry->SetString("name", | |
| 136 l10n_util::GetStringUTF16(IDS_OPTIONS_WALLPAPER_CENTER_LAYOUT)); | |
| 137 entry->SetInteger("index", ash::CENTER); | |
| 138 | |
| 139 layouts.Append(entry = new DictionaryValue()); | |
| 140 entry->SetString("name", | |
| 141 l10n_util::GetStringUTF16(IDS_OPTIONS_WALLPAPER_CENTER_CROPPED_LAYOUT)); | |
| 142 entry->SetInteger("index", ash::CENTER_CROPPED); | |
| 143 | |
| 144 layouts.Append(entry = new DictionaryValue()); | |
| 145 entry->SetString("name", | |
| 146 l10n_util::GetStringUTF16(IDS_OPTIONS_WALLPAPER_STRETCH_LAYOUT)); | |
| 147 entry->SetInteger("index", ash::STRETCH); | |
| 148 | |
| 149 base::FundamentalValue selected_value(static_cast<int>(layout)); | |
| 150 web_ui()->CallJavascriptFunction( | |
| 151 "SetWallpaperOptions.populateWallpaperLayouts", layouts, selected_value); | |
| 152 } | |
| 153 | |
| 154 void SetWallpaperOptionsHandler::HandlePageInitialized( | |
| 155 const base::ListValue* args) { | |
| 156 DCHECK(args && args->empty()); | |
| 157 | |
| 158 SendDefaultImages(); | |
| 159 } | |
| 160 | |
| 161 void SetWallpaperOptionsHandler::HandlePageShown(const base::ListValue* args) { | |
| 162 DCHECK(args && args->empty()); | |
| 163 User::WallpaperType type; | |
| 164 int index; | |
| 165 base::Time date; | |
| 166 WallpaperManager::Get()->GetLoggedInUserWallpaperProperties( | |
| 167 &type, &index, &date); | |
| 168 if (type == User::DAILY && date != base::Time::Now().LocalMidnight()) { | |
| 169 UserManager::Get()->SaveLoggedInUserWallpaperProperties(User::DAILY, | |
| 170 index); | |
| 171 ash::Shell::GetInstance()->user_wallpaper_delegate()-> | |
| 172 InitializeWallpaper(); | |
| 173 } | |
| 174 base::StringValue image_url(GetDefaultWallpaperThumbnailURL(index)); | |
| 175 base::FundamentalValue is_daily(type == User::DAILY); | |
| 176 if (type == User::CUSTOMIZED) { | |
| 177 ash::WallpaperLayout layout = static_cast<ash::WallpaperLayout>(index); | |
| 178 SendLayoutOptions(layout); | |
| 179 web_ui()->CallJavascriptFunction("SetWallpaperOptions.setCustomImage"); | |
| 180 } else { | |
| 181 SendLayoutOptions(ash::CENTER_CROPPED); | |
| 182 web_ui()->CallJavascriptFunction("SetWallpaperOptions.setSelectedImage", | |
| 183 image_url, is_daily); | |
| 184 } | |
| 185 } | |
| 186 | |
| 187 void SetWallpaperOptionsHandler::HandleChooseFile(const ListValue* args) { | |
| 188 DCHECK(args && args->empty()); | |
| 189 select_file_dialog_ = ui::SelectFileDialog::Create( | |
| 190 this, new ChromeSelectFilePolicy(web_ui()->GetWebContents())); | |
| 191 | |
| 192 FilePath downloads_path; | |
| 193 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &downloads_path)) | |
| 194 NOTREACHED(); | |
| 195 | |
| 196 // Static so we initialize it only once. | |
| 197 CR_DEFINE_STATIC_LOCAL(ui::SelectFileDialog::FileTypeInfo, file_type_info, | |
| 198 (GetUserImageFileTypeInfo())); | |
| 199 | |
| 200 select_file_dialog_->SelectFile(ui::SelectFileDialog::SELECT_OPEN_FILE, | |
| 201 l10n_util::GetStringUTF16(IDS_DOWNLOAD_TITLE), | |
| 202 downloads_path, &file_type_info, 0, | |
| 203 FILE_PATH_LITERAL(""), | |
| 204 GetBrowserWindow(), NULL); | |
| 205 } | |
| 206 | |
| 207 void SetWallpaperOptionsHandler::HandleLayoutChanged(const ListValue* args) { | |
| 208 int selected_layout = ash::CENTER_CROPPED; | |
| 209 if (!ExtractIntegerValue(args, &selected_layout)) | |
| 210 NOTREACHED() << "Could not read wallpaper layout from JSON argument"; | |
| 211 | |
| 212 ash::WallpaperLayout layout = | |
| 213 static_cast<ash::WallpaperLayout>(selected_layout); | |
| 214 | |
| 215 UserManager::Get()->SetLoggedInUserCustomWallpaperLayout(layout); | |
| 216 } | |
| 217 | |
| 218 void SetWallpaperOptionsHandler::HandleDefaultWallpaper(const ListValue* args) { | |
| 219 std::string image_url; | |
| 220 if (!args || | |
| 221 args->GetSize() != 1 || | |
| 222 !args->GetString(0, &image_url)) | |
| 223 NOTREACHED(); | |
| 224 | |
| 225 if (image_url.empty()) | |
| 226 return; | |
| 227 | |
| 228 int user_image_index; | |
| 229 if (IsDefaultWallpaperURL(image_url, &user_image_index)) { | |
| 230 UserManager::Get()->SaveLoggedInUserWallpaperProperties(User::DEFAULT, | |
| 231 user_image_index); | |
| 232 ash::Shell::GetInstance()->user_wallpaper_delegate()->InitializeWallpaper(); | |
| 233 } | |
| 234 } | |
| 235 | |
| 236 void SetWallpaperOptionsHandler::HandleDailyWallpaper(const ListValue* args) { | |
| 237 User::WallpaperType type; | |
| 238 int index; | |
| 239 base::Time date; | |
| 240 WallpaperManager::Get()->GetLoggedInUserWallpaperProperties( | |
| 241 &type, &index, &date); | |
| 242 UserManager::Get()->SaveLoggedInUserWallpaperProperties(User::DAILY, index); | |
| 243 ash::Shell::GetInstance()->desktop_background_controller()-> | |
| 244 SetDefaultWallpaper(index); | |
| 245 base::StringValue image_url(GetDefaultWallpaperThumbnailURL(index)); | |
| 246 base::FundamentalValue is_daily(true); | |
| 247 web_ui()->CallJavascriptFunction("SetWallpaperOptions.setSelectedImage", | |
| 248 image_url, is_daily); | |
| 249 } | |
| 250 | |
| 251 gfx::NativeWindow SetWallpaperOptionsHandler::GetBrowserWindow() const { | |
| 252 Browser* browser = | |
| 253 browser::FindBrowserWithWebContents(web_ui()->GetWebContents()); | |
| 254 return browser->window()->GetNativeWindow(); | |
| 255 } | |
| 256 | |
| 257 } // namespace options | |
| 258 } // namespace chromeos | |
| OLD | NEW |