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