diff options
author | bshe@chromium.org <bshe@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-04-24 02:05:55 +0000 |
---|---|---|
committer | bshe@chromium.org <bshe@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-04-24 02:05:55 +0000 |
commit | 6e8ac9af5c73f256555d2e3b2782ad82e44350c9 (patch) | |
tree | 52ac65bea38c7210d52b9966c1a4bd2891d2fe9f /chrome | |
parent | 3341c653947c5343c74b5f4b3987dffdd11b1532 (diff) | |
download | chromium_src-6e8ac9af5c73f256555d2e3b2782ad82e44350c9.zip chromium_src-6e8ac9af5c73f256555d2e3b2782ad82e44350c9.tar.gz chromium_src-6e8ac9af5c73f256555d2e3b2782ad82e44350c9.tar.bz2 |
Replace the index mapping of wallpaper picker UI and hard coded wallpaper index in C++
In this CL, we replace the old index system with a smarter one.
Its basically the same index system used for default user profile images.
BUG=124035
TEST=
Review URL: http://codereview.chromium.org/10021066
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@133602 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
8 files changed, 195 insertions, 28 deletions
diff --git a/chrome/browser/resources/options2/chromeos/set_wallpaper_options.js b/chrome/browser/resources/options2/chromeos/set_wallpaper_options.js index ed621c0..3910569 100644 --- a/chrome/browser/resources/options2/chromeos/set_wallpaper_options.js +++ b/chrome/browser/resources/options2/chromeos/set_wallpaper_options.js @@ -77,20 +77,33 @@ cr.define('options', function() { }, /** + * Set attributions of wallpaper with given URL. + * @param {string} url URL of the selected wallpaper. + * @private + */ + setWallpaperAttribution_: function(url) { + for (var i = 0; i < this.wallpapers_.length; i++) { + if (this.wallpapers_[i].url == url) { + $('author-name').textContent = this.wallpapers_[i].author; + $('author-website').textContent = this.wallpapers_[i].website; + return; + } + } + $('author-name').textContent = ''; + $('author-website').textContent = ''; + }, + + /** * Handles image selection change. * @private */ handleImageSelected_: function() { var wallpaperGrid = $('wallpaper-grid'); - var index = wallpaperGrid.selectionModel.selectedIndex; - $('author-name').innerText = this.wallpapers_[index].author; - $('author-website').innerText = this.wallpapers_[index].website; - - // Ignore deselection, selection change caused by program itself and - // selection of one of the action buttons. - if (index != -1 && + var url = wallpaperGrid.selectedItemUrl; + if (url && !wallpaperGrid.inProgramSelection) { - chrome.send('selectWallpaper', [index.toString()]); + this.setWallpaperAttribution_(url); + chrome.send('selectWallpaper', [url]); } }, @@ -105,15 +118,13 @@ cr.define('options', function() { }, /** - * Selects user image with the given index. - * @param {int} index index of the image to select. + * Selects corresponding wallpaper thumbnail with the given URL. + * @param {string} url URL of the wallpaper thumbnail to select. * @private */ - setSelectedImage_: function(index) { - var wallpaperGrid = $('wallpaper-grid'); - wallpaperGrid.selectedItemIndex = index; - $('author-name').innerText = this.wallpapers_[index].author; - $('author-website').innerText = this.wallpapers_[index].website; + setSelectedImage_: function(url) { + $('wallpaper-grid').selectedItemUrl = url; + this.setWallpaperAttribution_(url); }, /** diff --git a/chrome/browser/ui/webui/options2/chromeos/set_wallpaper_options_handler2.cc b/chrome/browser/ui/webui/options2/chromeos/set_wallpaper_options_handler2.cc index 8fda99b..40056a4 100644 --- a/chrome/browser/ui/webui/options2/chromeos/set_wallpaper_options_handler2.cc +++ b/chrome/browser/ui/webui/options2/chromeos/set_wallpaper_options_handler2.cc @@ -18,7 +18,8 @@ #include "chrome/browser/profiles/profile.h" #include "chrome/browser/ui/browser_list.h" #include "chrome/browser/ui/browser_window.h" -#include "chrome/browser/ui/webui/web_ui_util.h" +#include "chrome/browser/ui/views/window.h" +#include "chrome/browser/ui/webui/options2/chromeos/wallpaper_thumbnail_source2.h" #include "chrome/common/chrome_notification_types.h" #include "content/public/browser/notification_service.h" #include "content/public/browser/web_ui.h" @@ -68,8 +69,7 @@ void SetWallpaperOptionsHandler::SendDefaultImages() { for (int i = 0; i < ash::GetWallpaperCount(); ++i) { images.Append(image_detail = new DictionaryValue()); image_info = ash::GetWallpaperInfo(i); - image_detail->SetString("url", web_ui_util::GetImageDataUrl( - ash::GetWallpaperThumbnail(i))); + image_detail->SetString("url", GetDefaultWallpaperThumbnailURL(i)); image_detail->SetString("author", image_info.author); image_detail->SetString("website", image_info.website); } @@ -88,24 +88,27 @@ void SetWallpaperOptionsHandler::HandlePageInitialized( void SetWallpaperOptionsHandler::HandlePageShown(const base::ListValue* args) { DCHECK(args && args->empty()); int index = chromeos::UserManager::Get()->GetUserWallpaperIndex(); - base::FundamentalValue index_value(index); + base::StringValue image_url(GetDefaultWallpaperThumbnailURL(index)); web_ui()->CallJavascriptFunction("SetWallpaperOptions.setSelectedImage", - index_value); + image_url); } void SetWallpaperOptionsHandler::HandleSelectImage(const ListValue* args) { - std::string image_index_string; - int image_index; + std::string image_url; if (!args || args->GetSize() != 1 || - !args->GetString(0, &image_index_string) || - !base::StringToInt(image_index_string, &image_index) || - image_index < 0 || image_index >= ash::GetWallpaperCount()) + !args->GetString(0, &image_url)) NOTREACHED(); - UserManager::Get()->SaveUserWallpaperIndex(image_index); - ash::Shell::GetInstance()->desktop_background_controller()-> - SetDesktopBackgroundImageMode(); + if (image_url.empty()) + return; + + int user_image_index; + if (IsDefaultWallpaperURL(image_url, &user_image_index)) { + UserManager::Get()->SaveUserWallpaperIndex(user_image_index); + ash::Shell::GetInstance()->desktop_background_controller()-> + SetDesktopBackgroundImageMode(); + } } gfx::NativeWindow SetWallpaperOptionsHandler::GetBrowserWindow() const { diff --git a/chrome/browser/ui/webui/options2/chromeos/wallpaper_thumbnail_source2.cc b/chrome/browser/ui/webui/options2/chromeos/wallpaper_thumbnail_source2.cc new file mode 100644 index 0000000..4fca927 --- /dev/null +++ b/chrome/browser/ui/webui/options2/chromeos/wallpaper_thumbnail_source2.cc @@ -0,0 +1,93 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/ui/webui/options2/chromeos/wallpaper_thumbnail_source2.h" + +#include "ash/desktop_background/desktop_background_resources.h" +#include "base/memory/singleton.h" +#include "base/string_number_conversions.h" +#include "base/string_piece.h" +#include "base/string_util.h" +#include "base/stringprintf.h" +#include "base/threading/thread_restrictions.h" +#include "chrome/browser/io_thread.h" +#include "chrome/browser/ui/webui/chrome_url_data_manager.h" +#include "chrome/common/url_constants.h" +#include "content/public/browser/browser_thread.h" +#include "grit/ui_resources.h" +#include "net/base/mime_util.h" +#include "ui/base/resource/resource_bundle.h" +#include "ui/gfx/codec/png_codec.h" + +namespace chromeos { +namespace options2 { + +namespace { + +const char kDefaultWallpaperPrefix[] = "default_"; + +// Parse an integer from |path| and save it to |index|. For example, deafult_20 +// will set |index| to 20. +// |path| and |index| must not be NULL. +bool ParseIndexFromPath(const std::string& path, int* index) { + // TODO(bshe): We should probably save a string instead of index for + // extensibility. Remove this function when we migrate to string preference. + DCHECK(index); + if (!StartsWithASCII(path, kDefaultWallpaperPrefix, false)) + return false; + return base::StringToInt(base::StringPiece(path.begin() + + strlen(kDefaultWallpaperPrefix), path.end()), index); +} + +// Convert |path| to corresponding IDR. Return -1 if the path is invalid. +// |path| must not be NULL. +int PathToIDR(const std::string& path) { + int idr = -1; + int index = ash::GetInvalidWallpaperIndex(); + if (ParseIndexFromPath(path, &index)) + idr = ash::GetWallpaperInfo(index).thumb_id; + return idr; +} + +} // namespace + +std::string GetDefaultWallpaperThumbnailURL(int index) { + return StringPrintf("%s%s%d", chrome::kChromeUIWallpaperThumbnailURL, + kDefaultWallpaperPrefix, index); +} + +bool IsDefaultWallpaperURL(const std::string url, int* wallpaper_index) { + DCHECK(wallpaper_index); + *wallpaper_index = ash::GetInvalidWallpaperIndex(); + if (!StartsWithASCII(url, chrome::kChromeUIWallpaperThumbnailURL, false)) + return false; + std::string sub_path = url.substr(strlen( + chrome::kChromeUIWallpaperThumbnailURL)); + return ParseIndexFromPath(sub_path, wallpaper_index); +} + +WallpaperThumbnailSource::WallpaperThumbnailSource() + : DataSource(chrome::kChromeUIWallpaperThumbnailHost, NULL) { +} + +WallpaperThumbnailSource::~WallpaperThumbnailSource() { +} + +void WallpaperThumbnailSource::StartDataRequest(const std::string& path, + bool is_incognito, + int request_id) { + int idr = PathToIDR(path); + if (idr != -1) { + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); + const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); + SendResponse(request_id, rb.LoadDataResourceBytes(idr)); + } +} + +std::string WallpaperThumbnailSource::GetMimeType(const std::string&) const { + return "images/png"; +} + +} // namespace options2 +} // namespace chromeos diff --git a/chrome/browser/ui/webui/options2/chromeos/wallpaper_thumbnail_source2.h b/chrome/browser/ui/webui/options2/chromeos/wallpaper_thumbnail_source2.h new file mode 100644 index 0000000..a639a538 --- /dev/null +++ b/chrome/browser/ui/webui/options2/chromeos/wallpaper_thumbnail_source2.h @@ -0,0 +1,48 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_UI_WEBUI_OPTIONS2_CHROMEOS_WALLPAPER_THUMBNAIL_SOURCE2_H_ +#define CHROME_BROWSER_UI_WEBUI_OPTIONS2_CHROMEOS_WALLPAPER_THUMBNAIL_SOURCE2_H_ +#pragma once + +#include <string> + +#include "base/compiler_specific.h" +#include "chrome/browser/ui/webui/chrome_url_data_manager.h" + +namespace chromeos { +namespace options2 { + +// Returns a string consisting of the prefix specified and the index of the +// image. For example: chrome://wallpaper/default_2. +std::string GetDefaultWallpaperThumbnailURL(int index); + +// Checks if the given URL points to one of the default wallpapers. If it is, +// returns true and sets |wallpaper_index| to the corresponding index parsed +// from |url|. For example: chrome://wallpaper/default_2 will set +// |wallpaper_index| to 2. If not a default wallpaper url, returns false. +// |url| and |wallpaper_index| must not be NULL. +bool IsDefaultWallpaperURL(const std::string url, int* wallpaper_index); + +// A DataSource for chrome://wallpaper/ URLs. +class WallpaperThumbnailSource : public ChromeURLDataManager::DataSource { + public: + WallpaperThumbnailSource(); + + // ChromeURLDataManager::DataSource implementation. + virtual void StartDataRequest(const std::string& path, + bool is_incognito, + int request_id) OVERRIDE; + virtual std::string GetMimeType(const std::string&) const OVERRIDE; + + private: + virtual ~WallpaperThumbnailSource(); + + DISALLOW_COPY_AND_ASSIGN(WallpaperThumbnailSource); +}; + +} // namespace options2 +} // namespace chromeos + +#endif // CHROME_BROWSER_UI_WEBUI_OPTIONS2_CHROMEOS_WALLPAPER_THUMBNAIL_SOURCE2_H_ diff --git a/chrome/browser/ui/webui/options2/options_ui2.cc b/chrome/browser/ui/webui/options2/options_ui2.cc index a0adc59..d733219 100644 --- a/chrome/browser/ui/webui/options2/options_ui2.cc +++ b/chrome/browser/ui/webui/options2/options_ui2.cc @@ -75,6 +75,7 @@ #include "chrome/browser/ui/webui/options2/chromeos/proxy_handler2.h" #include "chrome/browser/ui/webui/options2/chromeos/stats_options_handler2.h" #include "chrome/browser/ui/webui/options2/chromeos/user_image_source2.h" +#include "chrome/browser/ui/webui/options2/chromeos/wallpaper_thumbnail_source2.h" #if defined(USE_VIRTUAL_KEYBOARD) #include "chrome/browser/ui/webui/options2/chromeos/virtual_keyboard_manager_handler2.h" #endif @@ -309,6 +310,11 @@ OptionsUI::OptionsUI(content::WebUI* web_ui) chromeos::options2::UserImageSource* user_image_source = new chromeos::options2::UserImageSource(); profile->GetChromeURLDataManager()->AddDataSource(user_image_source); + // Set up the chrome://wallpaper/ source. + chromeos::options2::WallpaperThumbnailSource* wallpaper_thumbnail_source = + new chromeos::options2::WallpaperThumbnailSource(); + profile->GetChromeURLDataManager()-> + AddDataSource(wallpaper_thumbnail_source); pointer_device_observer_.reset( new chromeos::system::PointerDeviceObserver()); diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index 9f06cad..8a98f01 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -3764,6 +3764,8 @@ 'browser/ui/webui/options2/chromeos/user_image_source2.h', 'browser/ui/webui/options2/chromeos/virtual_keyboard_manager_handler2.cc', 'browser/ui/webui/options2/chromeos/virtual_keyboard_manager_handler2.h', + 'browser/ui/webui/options2/chromeos/wallpaper_thumbnail_source2.cc', + 'browser/ui/webui/options2/chromeos/wallpaper_thumbnail_source2.h', 'browser/ui/webui/options2/clear_browser_data_handler2.cc', 'browser/ui/webui/options2/clear_browser_data_handler2.h', 'browser/ui/webui/options2/content_settings_handler2.cc', diff --git a/chrome/common/url_constants.cc b/chrome/common/url_constants.cc index c582272..38e7019 100644 --- a/chrome/common/url_constants.cc +++ b/chrome/common/url_constants.cc @@ -88,6 +88,7 @@ const char kChromeUISlideshowURL[] = "chrome://slideshow/"; const char kChromeUISystemInfoURL[] = "chrome://system/"; const char kChromeUITermsOemURL[] = "chrome://terms/oem"; const char kChromeUIUserImageURL[] = "chrome://userimage/"; +const char kChromeUIWallpaperThumbnailURL[] = "chrome://wallpaper/"; #endif #if defined(USE_ASH) @@ -214,6 +215,7 @@ const char kChromeUISimUnlockHost[] = "sim-unlock"; const char kChromeUISlideshowHost[] = "slideshow"; const char kChromeUISystemInfoHost[] = "system"; const char kChromeUIUserImageHost[] = "userimage"; +const char kChromeUIWallpaperThumbnailHost[] = "wallpaper"; const char kChromeUIMenu[] = "menu"; const char kChromeUINetworkMenu[] = "network-menu"; diff --git a/chrome/common/url_constants.h b/chrome/common/url_constants.h index fbbd90e..83c852f 100644 --- a/chrome/common/url_constants.h +++ b/chrome/common/url_constants.h @@ -87,6 +87,7 @@ extern const char kChromeUISlideshowURL[]; extern const char kChromeUISystemInfoURL[]; extern const char kChromeUITermsOemURL[]; extern const char kChromeUIUserImageURL[]; +extern const char kChromeUIWallpaperThumbnailURL[]; #endif #if defined(USE_ASH) @@ -211,6 +212,7 @@ extern const char kChromeUISimUnlockHost[]; extern const char kChromeUISlideshowHost[]; extern const char kChromeUISystemInfoHost[]; extern const char kChromeUIUserImageHost[]; +extern const char kChromeUIWallpaperThumbnailHost[]; extern const char kChromeUIMenu[]; extern const char kChromeUINetworkMenu[]; |