Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(559)

Side by Side Diff: chrome/browser/ui/webui/options2/chromeos/wallpaper_thumbnail_source2.cc

Issue 10021066: Replace the index mapping of wallpaper picker UI and hard coded wallpaper index in C++ (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merge to trunk Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 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/wallpaper_thumbnail_source2. h"
6
7 #include "ash/desktop_background/desktop_background_resources.h"
8 #include "base/memory/singleton.h"
9 #include "base/string_number_conversions.h"
10 #include "base/string_piece.h"
11 #include "base/string_util.h"
12 #include "base/stringprintf.h"
13 #include "base/threading/thread_restrictions.h"
14 #include "chrome/browser/io_thread.h"
15 #include "chrome/browser/ui/webui/chrome_url_data_manager.h"
16 #include "chrome/common/url_constants.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "grit/ui_resources.h"
19 #include "net/base/mime_util.h"
20 #include "ui/base/resource/resource_bundle.h"
21 #include "ui/gfx/codec/png_codec.h"
22
23 namespace chromeos {
24 namespace options2 {
25
26 namespace {
27
28 const char kDefaultWallpaperPrefix[] = "default_";
29
30 // Parse an integer from |path| and save it to |index|. For example, deafult_20
31 // will set |index| to 20.
32 // |path| and |index| must not be NULL.
33 bool ParseIndexFromPath(const std::string& path, int* index) {
34 // TODO(bshe): We should probably save a string instead of index for
35 // extensibility. Remove this function when we migrate to string preference.
36 DCHECK(index);
37 if (!StartsWithASCII(path, kDefaultWallpaperPrefix, false))
38 return false;
39 return base::StringToInt(base::StringPiece(path.begin() +
40 strlen(kDefaultWallpaperPrefix), path.end()), index);
41 }
42
43 // Convert |path| to corresponding IDR. Return -1 if the path is invalid.
44 // |path| must not be NULL.
45 int PathToIDR(const std::string& path) {
46 int idr = -1;
47 int index = ash::GetInvalidWallpaperIndex();
48 if (ParseIndexFromPath(path, &index))
49 idr = ash::GetWallpaperInfo(index).thumb_id;
50 return idr;
51 }
52
53 } // namespace
54
55 std::string GetDefaultWallpaperThumbnailURL(int index) {
56 return StringPrintf("%s%s%d", chrome::kChromeUIWallpaperThumbnailURL,
57 kDefaultWallpaperPrefix, index);
58 }
59
60 bool IsDefaultWallpaperURL(const std::string url, int* wallpaper_index) {
61 DCHECK(wallpaper_index);
62 *wallpaper_index = ash::GetInvalidWallpaperIndex();
63 if (!StartsWithASCII(url, chrome::kChromeUIWallpaperThumbnailURL, false))
64 return false;
65 std::string sub_path = url.substr(strlen(
66 chrome::kChromeUIWallpaperThumbnailURL));
67 return ParseIndexFromPath(sub_path, wallpaper_index);
68 }
69
70 WallpaperThumbnailSource::WallpaperThumbnailSource()
71 : DataSource(chrome::kChromeUIWallpaperThumbnailHost, NULL) {
72 }
73
74 WallpaperThumbnailSource::~WallpaperThumbnailSource() {
75 }
76
77 void WallpaperThumbnailSource::StartDataRequest(const std::string& path,
78 bool is_incognito,
79 int request_id) {
80 int idr = PathToIDR(path);
81 if (idr != -1) {
82 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
83 const ResourceBundle& rb = ResourceBundle::GetSharedInstance();
84 SendResponse(request_id, rb.LoadDataResourceBytes(idr));
85 }
86 }
87
88 std::string WallpaperThumbnailSource::GetMimeType(const std::string&) const {
89 return "images/png";
90 }
91
92 } // namespace options2
93 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698