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