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" |
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, int index, | |
flackr
2012/05/09 21:26:16
One parameter per line when wrapping decl.
bshe
2012/05/10 16:10:26
Done.
| |
99 void* params) { | |
100 UserManager* user_manager = UserManager::Get(); | |
101 | |
102 // Default wallpaper layout is CENTER_CROPPED. | |
103 user_manager->SaveUserWallpaperFromFile( | |
104 user_manager->GetLoggedInUser().email(), path, ash::CENTER_CROPPED, this); | |
105 web_ui()->CallJavascriptFunction("SetWallpaperOptions.didSelectFile"); | |
66 } | 106 } |
67 | 107 |
68 void SetWallpaperOptionsHandler::SendDefaultImages() { | 108 void SetWallpaperOptionsHandler::SendDefaultImages() { |
69 ListValue images; | 109 ListValue images; |
70 DictionaryValue* image_detail; | 110 DictionaryValue* image_detail; |
71 ash::WallpaperInfo image_info; | 111 ash::WallpaperInfo image_info; |
72 | 112 |
73 for (int i = 0; i < ash::GetWallpaperCount(); ++i) { | 113 for (int i = 0; i < ash::GetWallpaperCount(); ++i) { |
74 images.Append(image_detail = new DictionaryValue()); | 114 images.Append(image_detail = new DictionaryValue()); |
75 image_info = ash::GetWallpaperInfo(i); | 115 image_info = ash::GetWallpaperInfo(i); |
76 image_detail->SetString("url", GetDefaultWallpaperThumbnailURL(i)); | 116 image_detail->SetString("url", GetDefaultWallpaperThumbnailURL(i)); |
77 image_detail->SetString("author", image_info.author); | 117 image_detail->SetString("author", image_info.author); |
78 image_detail->SetString("website", image_info.website); | 118 image_detail->SetString("website", image_info.website); |
79 } | 119 } |
80 | 120 |
81 web_ui()->CallJavascriptFunction("SetWallpaperOptions.setDefaultImages", | 121 web_ui()->CallJavascriptFunction("SetWallpaperOptions.setDefaultImages", |
82 images); | 122 images); |
83 } | 123 } |
84 | 124 |
125 void SetWallpaperOptionsHandler::SendLayoutOptions( | |
126 ash::WallpaperLayout layout) { | |
127 ListValue layouts; | |
128 DictionaryValue* entry; | |
129 | |
130 layouts.Append(entry = new DictionaryValue()); | |
131 entry->SetString("name", | |
132 l10n_util::GetStringUTF16(IDS_OPTIONS_WALLPAPER_CENTER_LAYOUT)); | |
133 entry->SetInteger("index", ash::CENTER); | |
134 | |
135 layouts.Append(entry = new DictionaryValue()); | |
136 entry->SetString("name", | |
137 l10n_util::GetStringUTF16(IDS_OPTIONS_WALLPAPER_CENTER_CROPPED_LAYOUT)); | |
138 entry->SetInteger("index", ash::CENTER_CROPPED); | |
139 | |
140 layouts.Append(entry = new DictionaryValue()); | |
141 entry->SetString("name", | |
142 l10n_util::GetStringUTF16(IDS_OPTIONS_WALLPAPER_STRETCH_LAYOUT)); | |
143 entry->SetInteger("index", ash::STRETCH); | |
144 | |
145 int index = layout; | |
flackr
2012/05/09 21:26:16
Should be able to use:
FundamentalValue selected_v
bshe
2012/05/10 16:10:26
Done.
| |
146 scoped_ptr<Value> selected_value(Value::CreateIntegerValue(index)); | |
147 | |
148 web_ui()->CallJavascriptFunction( | |
149 "SetWallpaperOptions.populateWallpaperLayouts", layouts, *selected_value); | |
150 } | |
151 | |
85 void SetWallpaperOptionsHandler::HandlePageInitialized( | 152 void SetWallpaperOptionsHandler::HandlePageInitialized( |
86 const base::ListValue* args) { | 153 const base::ListValue* args) { |
87 DCHECK(args && args->empty()); | 154 DCHECK(args && args->empty()); |
88 | 155 |
89 SendDefaultImages(); | 156 SendDefaultImages(); |
90 } | 157 } |
91 | 158 |
92 void SetWallpaperOptionsHandler::HandlePageShown(const base::ListValue* args) { | 159 void SetWallpaperOptionsHandler::HandlePageShown(const base::ListValue* args) { |
93 DCHECK(args && args->empty()); | 160 DCHECK(args && args->empty()); |
94 User::WallpaperType type; | 161 User::WallpaperType type; |
95 int index; | 162 int index; |
96 UserManager::Get()->GetLoggedInUserWallpaperProperties(type, index); | 163 UserManager::Get()->GetLoggedInUserWallpaperProperties(type, index); |
97 base::StringValue image_url(GetDefaultWallpaperThumbnailURL(index)); | 164 base::StringValue image_url(GetDefaultWallpaperThumbnailURL(index)); |
98 base::FundamentalValue is_random(type == User::RANDOM); | 165 base::FundamentalValue is_random(type == User::RANDOM); |
99 web_ui()->CallJavascriptFunction("SetWallpaperOptions.setSelectedImage", | 166 if (type == User::CUSTOMIZED) { |
100 image_url, is_random); | 167 ash::WallpaperLayout layout = static_cast<ash::WallpaperLayout>(index); |
168 SendLayoutOptions(layout); | |
169 web_ui()->CallJavascriptFunction("SetWallpaperOptions.setCustomImage"); | |
170 } else { | |
171 SendLayoutOptions(ash::CENTER_CROPPED); | |
172 web_ui()->CallJavascriptFunction("SetWallpaperOptions.setSelectedImage", | |
173 image_url, is_random); | |
174 } | |
175 } | |
176 | |
177 void SetWallpaperOptionsHandler::HandleChooseFile(const ListValue* args) { | |
178 DCHECK(args && args->empty()); | |
179 if (!select_file_dialog_.get()) | |
180 select_file_dialog_ = SelectFileDialog::Create(this); | |
181 | |
182 FilePath downloads_path; | |
183 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &downloads_path)) { | |
184 NOTREACHED(); | |
185 return; | |
flackr
2012/05/09 21:26:16
Either we expect this case or don't return for NOT
bshe
2012/05/10 16:10:26
Done.
| |
186 } | |
187 | |
188 // Static so we initialize it only once. | |
189 CR_DEFINE_STATIC_LOCAL(SelectFileDialog::FileTypeInfo, file_type_info, | |
190 (GetUserImageFileTypeInfo())); | |
191 | |
192 select_file_dialog_->SelectFile(SelectFileDialog::SELECT_OPEN_FILE, | |
193 l10n_util::GetStringUTF16(IDS_DOWNLOAD_TITLE), | |
194 downloads_path, &file_type_info, 0, | |
195 FILE_PATH_LITERAL(""), | |
196 web_ui()->GetWebContents(), | |
197 GetBrowserWindow(), NULL); | |
198 } | |
199 | |
200 void SetWallpaperOptionsHandler::HandleLayoutChanged(const ListValue* args) { | |
201 int selected_layout = ash::CENTER_CROPPED; | |
202 if (!ExtractIntegerValue(args, &selected_layout)) | |
203 NOTREACHED() << "Could not read wallpapper layout from JSON argument"; | |
204 | |
205 ash::WallpaperLayout layout = | |
206 static_cast<ash::WallpaperLayout>(selected_layout); | |
207 | |
208 UserManager::Get()->SetLoggedInUserCustomWallpaperLayout(layout); | |
101 } | 209 } |
102 | 210 |
103 void SetWallpaperOptionsHandler::HandleDefaultWallpaper(const ListValue* args) { | 211 void SetWallpaperOptionsHandler::HandleDefaultWallpaper(const ListValue* args) { |
104 std::string image_url; | 212 std::string image_url; |
105 if (!args || | 213 if (!args || |
106 args->GetSize() != 1 || | 214 args->GetSize() != 1 || |
107 !args->GetString(0, &image_url)) | 215 !args->GetString(0, &image_url)) |
108 NOTREACHED(); | 216 NOTREACHED(); |
109 | 217 |
110 if (image_url.empty()) | 218 if (image_url.empty()) |
(...skipping 22 matching lines...) Expand all Loading... | |
133 gfx::NativeWindow SetWallpaperOptionsHandler::GetBrowserWindow() const { | 241 gfx::NativeWindow SetWallpaperOptionsHandler::GetBrowserWindow() const { |
134 Browser* browser = | 242 Browser* browser = |
135 BrowserList::FindBrowserWithProfile(Profile::FromWebUI(web_ui())); | 243 BrowserList::FindBrowserWithProfile(Profile::FromWebUI(web_ui())); |
136 if (!browser) | 244 if (!browser) |
137 return NULL; | 245 return NULL; |
138 return browser->window()->GetNativeHandle(); | 246 return browser->window()->GetNativeHandle(); |
139 } | 247 } |
140 | 248 |
141 } // namespace options2 | 249 } // namespace options2 |
142 } // namespace chromeos | 250 } // namespace chromeos |
OLD | NEW |