OLD | NEW |
(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 bool ParseIndexFromPath(const std::string& path, int* index) { |
| 31 // TODO(bshe): We should probably save a string instead of index for |
| 32 // extensibility. Remove this function when we migrate to string preference. |
| 33 DCHECK(index); |
| 34 if (!StartsWithASCII(path, kDefaultWallpaperPrefix, false)) |
| 35 return false; |
| 36 return base::StringToInt(base::StringPiece(path.begin() + |
| 37 strlen(kDefaultWallpaperPrefix), path.end()), index); |
| 38 } |
| 39 |
| 40 int PathToIDR(const std::string& path) { |
| 41 int idr = -1; |
| 42 int index = ash::GetInvalidWallpaperIndex(); |
| 43 if (ParseIndexFromPath(path, &index)) |
| 44 idr = ash::GetWallpaperInfo(index).thumb_id; |
| 45 return idr; |
| 46 } |
| 47 |
| 48 } // namespace |
| 49 |
| 50 std::string GetDefaultWallpaperThumbnailURL(int index) { |
| 51 return StringPrintf("%s%s%d", chrome::kChromeUIWallpaperThumbnailURL, |
| 52 kDefaultWallpaperPrefix, index); |
| 53 } |
| 54 |
| 55 bool IsDefaultWallpaperURL(const std::string url, int* wallpaper_index) { |
| 56 DCHECK(wallpaper_index); |
| 57 *wallpaper_index = ash::GetInvalidWallpaperIndex(); |
| 58 if (!StartsWithASCII(url, chrome::kChromeUIWallpaperThumbnailURL, false)) |
| 59 return false; |
| 60 std::string sub_path = url.substr(strlen( |
| 61 chrome::kChromeUIWallpaperThumbnailURL)); |
| 62 return ParseIndexFromPath(sub_path, wallpaper_index); |
| 63 } |
| 64 |
| 65 WallpaperThumbnailSource::WallpaperThumbnailSource() |
| 66 : DataSource(chrome::kChromeUIWallpaperThumbnailHost, NULL) { |
| 67 } |
| 68 |
| 69 WallpaperThumbnailSource::~WallpaperThumbnailSource() { |
| 70 } |
| 71 |
| 72 void WallpaperThumbnailSource::StartDataRequest(const std::string& path, |
| 73 bool is_incognito, |
| 74 int request_id) { |
| 75 int idr = PathToIDR(path); |
| 76 if (idr != -1) { |
| 77 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 78 const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 79 SendResponse(request_id, rb.LoadDataResourceBytes(idr)); |
| 80 } |
| 81 } |
| 82 |
| 83 std::string WallpaperThumbnailSource::GetMimeType(const std::string&) const { |
| 84 return "images/png"; |
| 85 } |
| 86 |
| 87 } // namespace options2 |
| 88 } // namespace chromeos |
OLD | NEW |