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_) |
| 96 return; |
| 97 previous_index_ = image_index; |
| 98 |
| 99 content::NotificationService::current()->Notify( |
| 100 chrome::NOTIFICATION_DESKTOP_BACKGROUND_CHANGED, |
| 101 content::Source<SetWallpaperOptionsHandler>(this), |
| 102 content::Details<int>(&image_index)); |
| 103 |
| 104 user_manager->SaveWallpaperToLocalState(user.email(), image_index); |
| 105 } |
| 106 |
| 107 gfx::NativeWindow SetWallpaperOptionsHandler::GetBrowserWindow() const { |
| 108 Browser* browser = |
| 109 BrowserList::FindBrowserWithProfile(Profile::FromWebUI(web_ui())); |
| 110 if (!browser) |
| 111 return NULL; |
| 112 return browser->window()->GetNativeHandle(); |
| 113 } |
| 114 |
| 115 } // namespace options2 |
| 116 } // namespace chromeos |
OLD | NEW |