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 "base/bind.h" | |
8 #include "base/bind_helpers.h" | |
9 #include "base/metrics/histogram.h" | |
10 #include "base/path_service.h" | |
11 #include "base/string_util.h" | |
12 #include "base/values.h" | |
13 #include "chrome/browser/chromeos/login/user_manager.h" | |
14 #include "chrome/browser/profiles/profile.h" | |
15 #include "chrome/browser/ui/browser_list.h" | |
16 #include "chrome/browser/ui/browser_window.h" | |
17 #include "chrome/browser/ui/views/window.h" | |
18 #include "chrome/browser/ui/webui/web_ui_util.h" | |
19 #include "chrome/common/chrome_paths.h" | |
20 #include "chrome/common/chrome_notification_types.h" | |
21 #include "content/public/browser/notification_service.h" | |
22 #include "content/public/browser/web_ui.h" | |
23 #include "grit/generated_resources.h" | |
24 #include "third_party/skia/include/core/SkBitmap.h" | |
25 #include "ui/base/l10n/l10n_util.h" | |
26 #include "ui/base/resource/resource_bundle.h" | |
27 #include "ui/views/widget/widget.h" | |
28 | |
29 #if defined(USE_ASH) | |
30 #include "ash/desktop_background/desktop_background_resources.h" | |
31 #endif | |
32 | |
33 namespace chromeos { | |
34 namespace options2 { | |
35 | |
36 SetWallpaperOptionsHandler::SetWallpaperOptionsHandler() | |
37 : previous_index_(-1), | |
38 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | |
39 } | |
40 | |
41 SetWallpaperOptionsHandler::~SetWallpaperOptionsHandler() { | |
42 } | |
43 | |
44 void SetWallpaperOptionsHandler::GetLocalizedValues( | |
45 DictionaryValue* localized_strings) { | |
46 DCHECK(localized_strings); | |
47 localized_strings->SetString("setWallpaperPage", | |
48 l10n_util::GetStringUTF16(IDS_OPTIONS_SET_WALLPAPER_DIALOG_TITLE)); | |
49 localized_strings->SetString("setWallpaperPageDescription", | |
50 l10n_util::GetStringUTF16(IDS_OPTIONS_SET_WALLPAPER_DIALOG_TEXT)); | |
51 } | |
52 | |
53 void SetWallpaperOptionsHandler::RegisterMessages() { | |
54 web_ui()->RegisterMessageCallback("onSetWallpaperPageInitialized", | |
55 base::Bind(&SetWallpaperOptionsHandler::HandlePageInitialized, | |
56 base::Unretained(this))); | |
57 web_ui()->RegisterMessageCallback("selectWallpaper", | |
58 base::Bind(&SetWallpaperOptionsHandler::HandleSelectImage, | |
59 base::Unretained(this))); | |
60 } | |
61 | |
62 void SetWallpaperOptionsHandler::SendDefaultImages() { | |
63 ListValue image_urls; | |
64 | |
65 for (int i = 0; i < ash::kDefaultWallpaperCount; ++i) { | |
66 image_urls.Append(Value::CreateStringValue( | |
67 web_ui_util::GetImageDataUrl(ash::GetDefaultWallpaperThumbnail(i)))); | |
68 } | |
69 | |
70 web_ui()->CallJavascriptFunction("SetWallpaperOptions.setDefaultImages", | |
71 image_urls); | |
72 } | |
73 | |
74 void SetWallpaperOptionsHandler::HandlePageInitialized( | |
75 const base::ListValue* args) { | |
76 DCHECK(args && args->empty()); | |
77 | |
78 SendDefaultImages(); | |
79 } | |
80 | |
81 void SetWallpaperOptionsHandler::HandleSelectImage(const ListValue* args) { | |
82 std::string image_url; | |
83 if (!args || | |
84 args->GetSize() != 1 || | |
85 !args->GetString(0, &image_url)) { | |
86 NOTREACHED(); | |
87 return; | |
88 } | |
89 DCHECK(!image_url.empty()); | |
90 | |
91 UserManager* user_manager = UserManager::Get(); | |
92 const User& user = user_manager->logged_in_user(); | |
93 int image_index = atoi(image_url.c_str()); | |
94 | |
95 if (image_index == previous_index_) | |
flackr
2012/03/07 20:50:28
We should probably guard against this in WebUI and
bshe
2012/03/07 23:48:35
It seems it's already done in webui. It listens to
| |
96 return; | |
97 previous_index_ = image_index; | |
98 | |
99 user_manager->SaveWallpaperDefaultIndex(user.email(), image_index); | |
100 } | |
101 | |
102 gfx::NativeWindow SetWallpaperOptionsHandler::GetBrowserWindow() const { | |
103 Browser* browser = | |
104 BrowserList::FindBrowserWithProfile(Profile::FromWebUI(web_ui())); | |
105 if (!browser) | |
106 return NULL; | |
107 return browser->window()->GetNativeHandle(); | |
108 } | |
109 | |
110 } // namespace options2 | |
111 } // namespace chromeos | |
OLD | NEW |