OLD | NEW |
---|---|
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" |
21 #include "chrome/browser/ui/webui/web_ui_util.h" | |
22 #include "chrome/common/chrome_notification_types.h" | 22 #include "chrome/common/chrome_notification_types.h" |
23 #include "chrome/common/chrome_paths.h" | |
23 #include "content/public/browser/notification_service.h" | 24 #include "content/public/browser/notification_service.h" |
24 #include "content/public/browser/web_ui.h" | 25 #include "content/public/browser/web_ui.h" |
25 #include "grit/generated_resources.h" | 26 #include "grit/generated_resources.h" |
26 #include "ui/base/l10n/l10n_util.h" | 27 #include "ui/base/l10n/l10n_util.h" |
27 #include "ui/base/resource/resource_bundle.h" | 28 #include "ui/base/resource/resource_bundle.h" |
28 #include "ui/views/widget/widget.h" | 29 #include "ui/views/widget/widget.h" |
29 | 30 |
30 namespace chromeos { | 31 namespace chromeos { |
31 namespace options2 { | 32 namespace options2 { |
32 | 33 |
34 namespace { | |
35 | |
36 // Returns info about extensions for files we support as wallpaper images. | |
37 SelectFileDialog::FileTypeInfo GetUserImageFileTypeInfo() { | |
38 SelectFileDialog::FileTypeInfo file_type_info; | |
39 file_type_info.extensions.resize(3); | |
40 | |
41 file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("jpg")); | |
42 file_type_info.extensions[1].push_back(FILE_PATH_LITERAL("jpeg")); | |
43 | |
44 file_type_info.extensions[2].push_back(FILE_PATH_LITERAL("png")); | |
45 | |
46 return file_type_info; | |
47 } | |
48 | |
49 } // namespace | |
50 | |
33 SetWallpaperOptionsHandler::SetWallpaperOptionsHandler() | 51 SetWallpaperOptionsHandler::SetWallpaperOptionsHandler() |
34 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 52 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
53 registrar_.Add(this, | |
54 chrome::NOTIFICATION_LOGIN_USER_WALLPAPER_THUMBNAIL_UPDATED, | |
55 content::NotificationService::AllSources()); | |
35 } | 56 } |
36 | 57 |
37 SetWallpaperOptionsHandler::~SetWallpaperOptionsHandler() { | 58 SetWallpaperOptionsHandler::~SetWallpaperOptionsHandler() { |
59 if (select_file_dialog_.get()) | |
flackr
2012/05/04 19:06:17
Only indent 2
bshe
2012/05/08 22:22:18
Done.
| |
60 select_file_dialog_->ListenerDestroyed(); | |
38 } | 61 } |
39 | 62 |
40 void SetWallpaperOptionsHandler::GetLocalizedValues( | 63 void SetWallpaperOptionsHandler::GetLocalizedValues( |
41 DictionaryValue* localized_strings) { | 64 DictionaryValue* localized_strings) { |
42 DCHECK(localized_strings); | 65 DCHECK(localized_strings); |
43 localized_strings->SetString("setWallpaperPage", | 66 localized_strings->SetString("setWallpaperPage", |
44 l10n_util::GetStringUTF16(IDS_OPTIONS_SET_WALLPAPER_DIALOG_TITLE)); | 67 l10n_util::GetStringUTF16(IDS_OPTIONS_SET_WALLPAPER_DIALOG_TITLE)); |
45 localized_strings->SetString("setWallpaperPageDescription", | 68 localized_strings->SetString("setWallpaperPageDescription", |
46 l10n_util::GetStringUTF16(IDS_OPTIONS_SET_WALLPAPER_DIALOG_TEXT)); | 69 l10n_util::GetStringUTF16(IDS_OPTIONS_SET_WALLPAPER_DIALOG_TEXT)); |
47 localized_strings->SetString("setWallpaperAuthor", | 70 localized_strings->SetString("setWallpaperAuthor", |
48 l10n_util::GetStringUTF16(IDS_OPTIONS_SET_WALLPAPER_AUTHOR_TEXT)); | 71 l10n_util::GetStringUTF16(IDS_OPTIONS_SET_WALLPAPER_AUTHOR_TEXT)); |
49 localized_strings->SetString("randomCheckbox", | 72 localized_strings->SetString("randomCheckbox", |
50 l10n_util::GetStringUTF16(IDS_OPTIONS_SET_WALLPAPER_RANDOM)); | 73 l10n_util::GetStringUTF16(IDS_OPTIONS_SET_WALLPAPER_RANDOM)); |
74 localized_strings->SetString("customeWallpaper", | |
75 l10n_util::GetStringUTF16(IDS_OPTIONS_CUSTOME_WALLPAPER)); | |
51 } | 76 } |
52 | 77 |
53 void SetWallpaperOptionsHandler::RegisterMessages() { | 78 void SetWallpaperOptionsHandler::RegisterMessages() { |
54 web_ui()->RegisterMessageCallback("onSetWallpaperPageInitialized", | 79 web_ui()->RegisterMessageCallback("onSetWallpaperPageInitialized", |
55 base::Bind(&SetWallpaperOptionsHandler::HandlePageInitialized, | 80 base::Bind(&SetWallpaperOptionsHandler::HandlePageInitialized, |
56 base::Unretained(this))); | 81 base::Unretained(this))); |
57 web_ui()->RegisterMessageCallback("onSetWallpaperPageShown", | 82 web_ui()->RegisterMessageCallback("onSetWallpaperPageShown", |
58 base::Bind(&SetWallpaperOptionsHandler::HandlePageShown, | 83 base::Bind(&SetWallpaperOptionsHandler::HandlePageShown, |
59 base::Unretained(this))); | 84 base::Unretained(this))); |
60 web_ui()->RegisterMessageCallback("selectDefaultWallpaper", | 85 web_ui()->RegisterMessageCallback("selectDefaultWallpaper", |
61 base::Bind(&SetWallpaperOptionsHandler::HandleDefaultWallpaper, | 86 base::Bind(&SetWallpaperOptionsHandler::HandleDefaultWallpaper, |
62 base::Unretained(this))); | 87 base::Unretained(this))); |
63 web_ui()->RegisterMessageCallback("selectRandomWallpaper", | 88 web_ui()->RegisterMessageCallback("selectRandomWallpaper", |
64 base::Bind(&SetWallpaperOptionsHandler::HandleRandomWallpaper, | 89 base::Bind(&SetWallpaperOptionsHandler::HandleRandomWallpaper, |
65 base::Unretained(this))); | 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))); | |
66 } | 97 } |
67 | 98 |
68 void SetWallpaperOptionsHandler::SendDefaultImages() { | 99 void SetWallpaperOptionsHandler::SendDefaultImages() { |
69 ListValue images; | 100 ListValue images; |
70 DictionaryValue* image_detail; | 101 DictionaryValue* image_detail; |
71 ash::WallpaperInfo image_info; | 102 ash::WallpaperInfo image_info; |
72 | 103 |
73 for (int i = 0; i < ash::GetWallpaperCount(); ++i) { | 104 for (int i = 0; i < ash::GetWallpaperCount(); ++i) { |
74 images.Append(image_detail = new DictionaryValue()); | 105 images.Append(image_detail = new DictionaryValue()); |
75 image_info = ash::GetWallpaperInfo(i); | 106 image_info = ash::GetWallpaperInfo(i); |
76 image_detail->SetString("url", GetDefaultWallpaperThumbnailURL(i)); | 107 image_detail->SetString("url", GetDefaultWallpaperThumbnailURL(i)); |
77 image_detail->SetString("author", image_info.author); | 108 image_detail->SetString("author", image_info.author); |
78 image_detail->SetString("website", image_info.website); | 109 image_detail->SetString("website", image_info.website); |
79 } | 110 } |
80 | 111 |
81 web_ui()->CallJavascriptFunction("SetWallpaperOptions.setDefaultImages", | 112 web_ui()->CallJavascriptFunction("SetWallpaperOptions.setDefaultImages", |
82 images); | 113 images); |
83 } | 114 } |
84 | 115 |
116 void SetWallpaperOptionsHandler::SendLayoutOptions( | |
117 ash::WallpaperLayout layout) { | |
118 ListValue layouts; | |
119 DictionaryValue* entry; | |
120 | |
121 layouts.Append(entry = new DictionaryValue()); | |
122 entry->SetString("name", | |
123 l10n_util::GetStringUTF16(IDS_OPTIONS_WALLPAPER_CENTER_LAYOUT)); | |
124 entry->SetInteger("index", ash::CENTER); | |
125 | |
126 layouts.Append(entry = new DictionaryValue()); | |
127 entry->SetString("name", | |
128 l10n_util::GetStringUTF16(IDS_OPTIONS_WALLPAPER_CENTER_CROPPED_LAYOUT)); | |
129 entry->SetInteger("index", ash::CENTER_CROPPED); | |
130 | |
131 layouts.Append(entry = new DictionaryValue()); | |
132 entry->SetString("name", | |
133 l10n_util::GetStringUTF16(IDS_OPTIONS_WALLPAPER_STRETCH_LAYOUT)); | |
134 entry->SetInteger("index", ash::STRETCH); | |
135 | |
136 int index = layout; | |
137 scoped_ptr<Value> selected_value(Value::CreateIntegerValue(index)); | |
138 | |
139 web_ui()->CallJavascriptFunction( | |
140 "SetWallpaperOptions.populateWallpaperLayout", layouts, *selected_value); | |
141 } | |
142 | |
85 void SetWallpaperOptionsHandler::HandlePageInitialized( | 143 void SetWallpaperOptionsHandler::HandlePageInitialized( |
86 const base::ListValue* args) { | 144 const base::ListValue* args) { |
87 DCHECK(args && args->empty()); | 145 DCHECK(args && args->empty()); |
88 | 146 |
89 SendDefaultImages(); | 147 SendDefaultImages(); |
90 } | 148 } |
91 | 149 |
92 void SetWallpaperOptionsHandler::HandlePageShown(const base::ListValue* args) { | 150 void SetWallpaperOptionsHandler::HandlePageShown(const base::ListValue* args) { |
93 DCHECK(args && args->empty()); | 151 DCHECK(args && args->empty()); |
94 User::WallpaperType type; | 152 User::WallpaperType type; |
95 int index; | 153 int index; |
96 UserManager::Get()->GetLoggedInUserWallpaperProperties(type, index); | 154 UserManager::Get()->GetLoggedInUserWallpaperProperties(type, index); |
97 //int index = chromeos::UserManager::Get()->GetUserWallpaperIndex(); | |
98 base::StringValue image_url(GetDefaultWallpaperThumbnailURL(index)); | 155 base::StringValue image_url(GetDefaultWallpaperThumbnailURL(index)); |
99 base::FundamentalValue is_random(type == User::RANDOM); | 156 base::FundamentalValue is_random(type == User::RANDOM); |
100 web_ui()->CallJavascriptFunction("SetWallpaperOptions.setSelectedImage", | 157 if (type == User::CUSTOMIZED) { |
101 image_url, is_random); | 158 ash::WallpaperLayout layout = static_cast<ash::WallpaperLayout>(index); |
159 SendLayoutOptions(layout); | |
160 web_ui()->CallJavascriptFunction("SetWallpaperOptions.setCustomImage"); | |
161 } else { | |
162 SendLayoutOptions(ash::CENTER_CROPPED); | |
163 web_ui()->CallJavascriptFunction("SetWallpaperOptions.setSelectedImage", | |
164 image_url, is_random); | |
165 } | |
166 } | |
167 | |
168 void SetWallpaperOptionsHandler::HandleChooseFile(const ListValue* args) { | |
169 DCHECK(args && args->empty()); | |
170 if (!select_file_dialog_.get()) | |
171 select_file_dialog_ = SelectFileDialog::Create(this); | |
172 | |
173 FilePath downloads_path; | |
174 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &downloads_path)) { | |
175 NOTREACHED(); | |
176 return; | |
177 } | |
178 | |
179 // Static so we initialize it only once. | |
180 CR_DEFINE_STATIC_LOCAL(SelectFileDialog::FileTypeInfo, file_type_info, | |
181 (GetUserImageFileTypeInfo())); | |
182 | |
183 select_file_dialog_->SelectFile( | |
184 SelectFileDialog::SELECT_OPEN_FILE, | |
185 l10n_util::GetStringUTF16(IDS_DOWNLOAD_TITLE), | |
186 downloads_path, | |
187 &file_type_info, | |
188 0, | |
189 FILE_PATH_LITERAL(""), | |
190 web_ui()->GetWebContents(), | |
191 GetBrowserWindow(), | |
192 NULL); | |
193 } | |
194 | |
195 void SetWallpaperOptionsHandler::FileSelected(const FilePath& path, | |
196 int index, | |
197 void* params) { | |
198 UserManager* user_manager = UserManager::Get(); | |
199 | |
200 // Default wallpaper layout is CENTER_CROPPED. | |
201 user_manager->SaveUserWallpaperFromFile( | |
202 user_manager->GetLoggedInUser().email(), path, ash::CENTER_CROPPED); | |
203 web_ui()->CallJavascriptFunction("SetWallpaperOptions.didSelectFile"); | |
204 } | |
205 | |
206 void SetWallpaperOptionsHandler::HandleLayoutChanged(const ListValue* args) { | |
207 int selected_layout = -1; | |
208 if (!ExtractIntegerValue(args, &selected_layout)) { | |
209 NOTREACHED(); | |
210 return; | |
211 } | |
212 ash::WallpaperLayout layout = | |
213 static_cast<ash::WallpaperLayout>(selected_layout); | |
214 | |
215 UserManager::Get()->SetLoggedInUserCustomWallpaperLayout(layout); | |
102 } | 216 } |
103 | 217 |
104 void SetWallpaperOptionsHandler::HandleDefaultWallpaper(const ListValue* args) { | 218 void SetWallpaperOptionsHandler::HandleDefaultWallpaper(const ListValue* args) { |
105 std::string image_url; | 219 std::string image_url; |
106 if (!args || | 220 if (!args || |
107 args->GetSize() != 1 || | 221 args->GetSize() != 1 || |
108 !args->GetString(0, &image_url)) | 222 !args->GetString(0, &image_url)) |
109 NOTREACHED(); | 223 NOTREACHED(); |
110 | 224 |
111 if (image_url.empty()) | 225 if (image_url.empty()) |
(...skipping 12 matching lines...) Expand all Loading... | |
124 int index = ash::GetRandomWallpaperIndex(); | 238 int index = ash::GetRandomWallpaperIndex(); |
125 UserManager::Get()->SaveLoggedInUserWallpaperProperties(User::RANDOM, index); | 239 UserManager::Get()->SaveLoggedInUserWallpaperProperties(User::RANDOM, index); |
126 ash::Shell::GetInstance()->desktop_background_controller()-> | 240 ash::Shell::GetInstance()->desktop_background_controller()-> |
127 SetDefaultWallpaper(index); | 241 SetDefaultWallpaper(index); |
128 base::StringValue image_url(GetDefaultWallpaperThumbnailURL(index)); | 242 base::StringValue image_url(GetDefaultWallpaperThumbnailURL(index)); |
129 base::FundamentalValue is_random(true); | 243 base::FundamentalValue is_random(true); |
130 web_ui()->CallJavascriptFunction("SetWallpaperOptions.setSelectedImage", | 244 web_ui()->CallJavascriptFunction("SetWallpaperOptions.setSelectedImage", |
131 image_url, is_random); | 245 image_url, is_random); |
132 } | 246 } |
133 | 247 |
248 void SetWallpaperOptionsHandler::SetCustomWallpaperThumb() { | |
249 web_ui()->CallJavascriptFunction("SetWallpaperOptions.setCustomImage"); | |
250 } | |
251 | |
252 void SetWallpaperOptionsHandler::Observe( | |
253 int type, | |
254 const content::NotificationSource& source, | |
255 const content::NotificationDetails& details) { | |
256 OptionsPageUIHandler::Observe(type, source, details); | |
257 if (type == chrome::NOTIFICATION_LOGIN_USER_WALLPAPER_THUMBNAIL_UPDATED) { | |
258 // User custom wallpaper thumbnail has been updated. | |
259 //SetCustomWallpaperThumb(); | |
flackr
2012/05/04 19:06:17
Remove commented code.
bshe
2012/05/08 22:22:18
Done.
| |
260 web_ui()->CallJavascriptFunction("SetWallpaperOptions.setCustomImage"); | |
261 } | |
262 } | |
263 | |
264 | |
134 gfx::NativeWindow SetWallpaperOptionsHandler::GetBrowserWindow() const { | 265 gfx::NativeWindow SetWallpaperOptionsHandler::GetBrowserWindow() const { |
135 Browser* browser = | 266 Browser* browser = |
136 BrowserList::FindBrowserWithProfile(Profile::FromWebUI(web_ui())); | 267 BrowserList::FindBrowserWithProfile(Profile::FromWebUI(web_ui())); |
137 if (!browser) | 268 if (!browser) |
138 return NULL; | 269 return NULL; |
139 return browser->window()->GetNativeHandle(); | 270 return browser->window()->GetNativeHandle(); |
140 } | 271 } |
141 | 272 |
142 } // namespace options2 | 273 } // namespace options2 |
143 } // namespace chromeos | 274 } // namespace chromeos |
OLD | NEW |