Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(358)

Side by Side Diff: chrome/browser/ui/webui/options2/chromeos/set_wallpaper_options_handler2.cc

Issue 10375010: Implement user selected wallpaper feature. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merge Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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/ui/webui/options2/chromeos/set_wallpaper_options_handle r2.h" 5 #include "chrome/browser/ui/webui/options2/chromeos/set_wallpaper_options_handle r2.h"
6 6
7 #include "ash/desktop_background/desktop_background_controller.h" 7 #include "ash/desktop_background/desktop_background_controller.h"
8 #include "ash/desktop_background/desktop_background_resources.h"
9 #include "ash/shell.h" 8 #include "ash/shell.h"
10 #include "base/bind.h" 9 #include "base/bind.h"
11 #include "base/bind_helpers.h" 10 #include "base/bind_helpers.h"
12 #include "base/metrics/histogram.h" 11 #include "base/metrics/histogram.h"
13 #include "base/path_service.h" 12 #include "base/path_service.h"
14 #include "base/string_number_conversions.h" 13 #include "base/string_number_conversions.h"
15 #include "base/string_util.h" 14 #include "base/string_util.h"
16 #include "base/values.h" 15 #include "base/values.h"
17 #include "chrome/browser/chromeos/login/user_manager.h" 16 #include "chrome/browser/chromeos/login/user_manager.h"
18 #include "chrome/browser/profiles/profile.h" 17 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/ui/browser_list.h" 18 #include "chrome/browser/ui/browser_list.h"
20 #include "chrome/browser/ui/browser_window.h" 19 #include "chrome/browser/ui/browser_window.h"
21 #include "chrome/browser/ui/webui/options2/chromeos/wallpaper_thumbnail_source2. h" 20 #include "chrome/browser/ui/webui/options2/chromeos/wallpaper_thumbnail_source2. h"
22 #include "chrome/common/chrome_notification_types.h" 21 #include "chrome/browser/ui/webui/web_ui_util.h"
23 #include "content/public/browser/notification_service.h" 22 #include "chrome/common/chrome_paths.h"
24 #include "content/public/browser/web_ui.h" 23 #include "content/public/browser/web_ui.h"
25 #include "grit/generated_resources.h" 24 #include "grit/generated_resources.h"
26 #include "ui/base/l10n/l10n_util.h" 25 #include "ui/base/l10n/l10n_util.h"
27 #include "ui/base/resource/resource_bundle.h" 26 #include "ui/base/resource/resource_bundle.h"
28 #include "ui/views/widget/widget.h" 27 #include "ui/views/widget/widget.h"
29 28
30 namespace chromeos { 29 namespace chromeos {
31 namespace options2 { 30 namespace options2 {
32 31
32 namespace {
33
34 // Returns info about extensions for files we support as wallpaper images.
35 SelectFileDialog::FileTypeInfo GetUserImageFileTypeInfo() {
36 SelectFileDialog::FileTypeInfo file_type_info;
37 file_type_info.extensions.resize(3);
38
39 file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("jpg"));
40 file_type_info.extensions[1].push_back(FILE_PATH_LITERAL("jpeg"));
41
42 file_type_info.extensions[2].push_back(FILE_PATH_LITERAL("png"));
43
44 return file_type_info;
45 }
46
47 } // namespace
48
33 SetWallpaperOptionsHandler::SetWallpaperOptionsHandler() 49 SetWallpaperOptionsHandler::SetWallpaperOptionsHandler()
34 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { 50 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
35 } 51 }
36 52
37 SetWallpaperOptionsHandler::~SetWallpaperOptionsHandler() { 53 SetWallpaperOptionsHandler::~SetWallpaperOptionsHandler() {
54 if (select_file_dialog_.get())
55 select_file_dialog_->ListenerDestroyed();
38 } 56 }
39 57
40 void SetWallpaperOptionsHandler::GetLocalizedValues( 58 void SetWallpaperOptionsHandler::GetLocalizedValues(
41 DictionaryValue* localized_strings) { 59 DictionaryValue* localized_strings) {
42 DCHECK(localized_strings); 60 DCHECK(localized_strings);
43 localized_strings->SetString("setWallpaperPage", 61 localized_strings->SetString("setWallpaperPage",
44 l10n_util::GetStringUTF16(IDS_OPTIONS_SET_WALLPAPER_DIALOG_TITLE)); 62 l10n_util::GetStringUTF16(IDS_OPTIONS_SET_WALLPAPER_DIALOG_TITLE));
45 localized_strings->SetString("setWallpaperPageDescription", 63 localized_strings->SetString("setWallpaperPageDescription",
46 l10n_util::GetStringUTF16(IDS_OPTIONS_SET_WALLPAPER_DIALOG_TEXT)); 64 l10n_util::GetStringUTF16(IDS_OPTIONS_SET_WALLPAPER_DIALOG_TEXT));
47 localized_strings->SetString("setWallpaperAuthor", 65 localized_strings->SetString("setWallpaperAuthor",
48 l10n_util::GetStringUTF16(IDS_OPTIONS_SET_WALLPAPER_AUTHOR_TEXT)); 66 l10n_util::GetStringUTF16(IDS_OPTIONS_SET_WALLPAPER_AUTHOR_TEXT));
49 localized_strings->SetString("randomCheckbox", 67 localized_strings->SetString("randomCheckbox",
50 l10n_util::GetStringUTF16(IDS_OPTIONS_SET_WALLPAPER_RANDOM)); 68 l10n_util::GetStringUTF16(IDS_OPTIONS_SET_WALLPAPER_RANDOM));
69 localized_strings->SetString("customWallpaper",
70 l10n_util::GetStringUTF16(IDS_OPTIONS_CUSTOME_WALLPAPER));
51 } 71 }
52 72
53 void SetWallpaperOptionsHandler::RegisterMessages() { 73 void SetWallpaperOptionsHandler::RegisterMessages() {
54 web_ui()->RegisterMessageCallback("onSetWallpaperPageInitialized", 74 web_ui()->RegisterMessageCallback("onSetWallpaperPageInitialized",
55 base::Bind(&SetWallpaperOptionsHandler::HandlePageInitialized, 75 base::Bind(&SetWallpaperOptionsHandler::HandlePageInitialized,
56 base::Unretained(this))); 76 base::Unretained(this)));
57 web_ui()->RegisterMessageCallback("onSetWallpaperPageShown", 77 web_ui()->RegisterMessageCallback("onSetWallpaperPageShown",
58 base::Bind(&SetWallpaperOptionsHandler::HandlePageShown, 78 base::Bind(&SetWallpaperOptionsHandler::HandlePageShown,
59 base::Unretained(this))); 79 base::Unretained(this)));
60 web_ui()->RegisterMessageCallback("selectDefaultWallpaper", 80 web_ui()->RegisterMessageCallback("selectDefaultWallpaper",
61 base::Bind(&SetWallpaperOptionsHandler::HandleDefaultWallpaper, 81 base::Bind(&SetWallpaperOptionsHandler::HandleDefaultWallpaper,
62 base::Unretained(this))); 82 base::Unretained(this)));
63 web_ui()->RegisterMessageCallback("selectRandomWallpaper", 83 web_ui()->RegisterMessageCallback("selectRandomWallpaper",
64 base::Bind(&SetWallpaperOptionsHandler::HandleRandomWallpaper, 84 base::Bind(&SetWallpaperOptionsHandler::HandleRandomWallpaper,
65 base::Unretained(this))); 85 base::Unretained(this)));
86 web_ui()->RegisterMessageCallback("chooseWallpaper",
87 base::Bind(&SetWallpaperOptionsHandler::HandleChooseFile,
88 base::Unretained(this)));
89 web_ui()->RegisterMessageCallback("changeWallpaperLayout",
90 base::Bind(&SetWallpaperOptionsHandler::HandleLayoutChanged,
91 base::Unretained(this)));
92 }
93
94 void SetWallpaperOptionsHandler::SetCustomWallpaperThumbnail() {
95 web_ui()->CallJavascriptFunction("SetWallpaperOptions.setCustomImage");
96 }
97
98 void SetWallpaperOptionsHandler::FileSelected(const FilePath& path,
99 int index,
100 void* params) {
101 UserManager* user_manager = UserManager::Get();
102
103 // Default wallpaper layout is CENTER_CROPPED.
104 user_manager->SaveUserWallpaperFromFile(
105 user_manager->GetLoggedInUser().email(), path, ash::CENTER_CROPPED, this);
106 web_ui()->CallJavascriptFunction("SetWallpaperOptions.didSelectFile");
66 } 107 }
67 108
68 void SetWallpaperOptionsHandler::SendDefaultImages() { 109 void SetWallpaperOptionsHandler::SendDefaultImages() {
69 ListValue images; 110 ListValue images;
70 DictionaryValue* image_detail; 111 DictionaryValue* image_detail;
71 ash::WallpaperInfo image_info; 112 ash::WallpaperInfo image_info;
72 113
73 for (int i = 0; i < ash::GetWallpaperCount(); ++i) { 114 for (int i = 0; i < ash::GetWallpaperCount(); ++i) {
74 images.Append(image_detail = new DictionaryValue()); 115 images.Append(image_detail = new DictionaryValue());
75 image_info = ash::GetWallpaperInfo(i); 116 image_info = ash::GetWallpaperInfo(i);
76 image_detail->SetString("url", GetDefaultWallpaperThumbnailURL(i)); 117 image_detail->SetString("url", GetDefaultWallpaperThumbnailURL(i));
77 image_detail->SetString("author", image_info.author); 118 image_detail->SetString("author", image_info.author);
78 image_detail->SetString("website", image_info.website); 119 image_detail->SetString("website", image_info.website);
79 } 120 }
80 121
81 web_ui()->CallJavascriptFunction("SetWallpaperOptions.setDefaultImages", 122 web_ui()->CallJavascriptFunction("SetWallpaperOptions.setDefaultImages",
82 images); 123 images);
83 } 124 }
84 125
126 void SetWallpaperOptionsHandler::SendLayoutOptions(
127 ash::WallpaperLayout layout) {
128 ListValue layouts;
129 DictionaryValue* entry;
130
131 layouts.Append(entry = new DictionaryValue());
132 entry->SetString("name",
133 l10n_util::GetStringUTF16(IDS_OPTIONS_WALLPAPER_CENTER_LAYOUT));
134 entry->SetInteger("index", ash::CENTER);
135
136 layouts.Append(entry = new DictionaryValue());
137 entry->SetString("name",
138 l10n_util::GetStringUTF16(IDS_OPTIONS_WALLPAPER_CENTER_CROPPED_LAYOUT));
139 entry->SetInteger("index", ash::CENTER_CROPPED);
140
141 layouts.Append(entry = new DictionaryValue());
142 entry->SetString("name",
143 l10n_util::GetStringUTF16(IDS_OPTIONS_WALLPAPER_STRETCH_LAYOUT));
144 entry->SetInteger("index", ash::STRETCH);
145
146 base::FundamentalValue selected_value(static_cast<int>(layout));
147 web_ui()->CallJavascriptFunction(
148 "SetWallpaperOptions.populateWallpaperLayouts", layouts, selected_value);
149 }
150
85 void SetWallpaperOptionsHandler::HandlePageInitialized( 151 void SetWallpaperOptionsHandler::HandlePageInitialized(
86 const base::ListValue* args) { 152 const base::ListValue* args) {
87 DCHECK(args && args->empty()); 153 DCHECK(args && args->empty());
88 154
89 SendDefaultImages(); 155 SendDefaultImages();
90 } 156 }
91 157
92 void SetWallpaperOptionsHandler::HandlePageShown(const base::ListValue* args) { 158 void SetWallpaperOptionsHandler::HandlePageShown(const base::ListValue* args) {
93 DCHECK(args && args->empty()); 159 DCHECK(args && args->empty());
94 User::WallpaperType type; 160 User::WallpaperType type;
95 int index; 161 int index;
96 UserManager::Get()->GetLoggedInUserWallpaperProperties(&type, &index); 162 UserManager::Get()->GetLoggedInUserWallpaperProperties(&type, &index);
97 base::StringValue image_url(GetDefaultWallpaperThumbnailURL(index)); 163 base::StringValue image_url(GetDefaultWallpaperThumbnailURL(index));
98 base::FundamentalValue is_random(type == User::RANDOM); 164 base::FundamentalValue is_random(type == User::RANDOM);
99 web_ui()->CallJavascriptFunction("SetWallpaperOptions.setSelectedImage", 165 if (type == User::CUSTOMIZED) {
100 image_url, is_random); 166 ash::WallpaperLayout layout = static_cast<ash::WallpaperLayout>(index);
167 SendLayoutOptions(layout);
168 web_ui()->CallJavascriptFunction("SetWallpaperOptions.setCustomImage");
169 } else {
170 SendLayoutOptions(ash::CENTER_CROPPED);
171 web_ui()->CallJavascriptFunction("SetWallpaperOptions.setSelectedImage",
172 image_url, is_random);
173 }
174 }
175
176 void SetWallpaperOptionsHandler::HandleChooseFile(const ListValue* args) {
177 DCHECK(args && args->empty());
178 if (!select_file_dialog_.get())
179 select_file_dialog_ = SelectFileDialog::Create(this);
180
181 FilePath downloads_path;
182 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &downloads_path))
183 NOTREACHED();
184
185 // Static so we initialize it only once.
186 CR_DEFINE_STATIC_LOCAL(SelectFileDialog::FileTypeInfo, file_type_info,
187 (GetUserImageFileTypeInfo()));
188
189 select_file_dialog_->SelectFile(SelectFileDialog::SELECT_OPEN_FILE,
190 l10n_util::GetStringUTF16(IDS_DOWNLOAD_TITLE),
191 downloads_path, &file_type_info, 0,
192 FILE_PATH_LITERAL(""),
193 web_ui()->GetWebContents(),
194 GetBrowserWindow(), NULL);
195 }
196
197 void SetWallpaperOptionsHandler::HandleLayoutChanged(const ListValue* args) {
198 int selected_layout = ash::CENTER_CROPPED;
199 if (!ExtractIntegerValue(args, &selected_layout))
200 NOTREACHED() << "Could not read wallpaper layout from JSON argument";
201
202 ash::WallpaperLayout layout =
203 static_cast<ash::WallpaperLayout>(selected_layout);
204
205 UserManager::Get()->SetLoggedInUserCustomWallpaperLayout(layout);
101 } 206 }
102 207
103 void SetWallpaperOptionsHandler::HandleDefaultWallpaper(const ListValue* args) { 208 void SetWallpaperOptionsHandler::HandleDefaultWallpaper(const ListValue* args) {
104 std::string image_url; 209 std::string image_url;
105 if (!args || 210 if (!args ||
106 args->GetSize() != 1 || 211 args->GetSize() != 1 ||
107 !args->GetString(0, &image_url)) 212 !args->GetString(0, &image_url))
108 NOTREACHED(); 213 NOTREACHED();
109 214
110 if (image_url.empty()) 215 if (image_url.empty())
(...skipping 22 matching lines...) Expand all
133 gfx::NativeWindow SetWallpaperOptionsHandler::GetBrowserWindow() const { 238 gfx::NativeWindow SetWallpaperOptionsHandler::GetBrowserWindow() const {
134 Browser* browser = 239 Browser* browser =
135 BrowserList::FindBrowserWithProfile(Profile::FromWebUI(web_ui())); 240 BrowserList::FindBrowserWithProfile(Profile::FromWebUI(web_ui()));
136 if (!browser) 241 if (!browser)
137 return NULL; 242 return NULL;
138 return browser->window()->GetNativeHandle(); 243 return browser->window()->GetNativeHandle();
139 } 244 }
140 245
141 } // namespace options2 246 } // namespace options2
142 } // namespace chromeos 247 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698