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 DCHECK(index); | |
32 if (!StartsWithASCII(path, kDefaultWallpaperPrefix, false)) | |
33 return false; | |
34 return base::StringToInt(base::StringPiece(path.begin() + | |
35 strlen(kDefaultWallpaperPrefix), path.end()), index); | |
36 } | |
37 | |
38 int PathToIDR(const std::string& path) { | |
39 int idr = -1; | |
40 int index = ash::GetInvalidWallpaperIndex(); | |
41 if (ParseIndexFromPath(path, &index)) | |
42 idr = ash::GetWallpaperInfo(index).thumb_id; | |
43 return idr; | |
44 } | |
45 | |
46 } // namespace | |
47 | |
48 std::string GetDefaultWallpaperThumbnailURL(int index) { | |
49 return StringPrintf("%s%s%d", chrome::kChromeUIWallpaperThumbnailURL, | |
50 kDefaultWallpaperPrefix, index); | |
51 } | |
52 | |
53 bool IsDefaultWallpaperURL(const std::string url, int* wallpaper_index) { | |
54 DCHECK(wallpaper_index); | |
55 *wallpaper_index = ash::GetInvalidWallpaperIndex(); | |
56 if (!StartsWithASCII(url, chrome::kChromeUIWallpaperThumbnailURL, false)) | |
57 return false; | |
58 std::string sub_path = url.substr(strlen( | |
flackr
2012/04/19 20:13:07
I'm fairly certain StringPiece avoids constructing
bshe
2012/04/19 21:05:25
It seems the StartsWithASCII function use std:stri
flackr
2012/04/20 00:16:50
bool BasicStringPiece::starts_with should work :).
bshe
2012/04/20 15:33:00
As discussed, StartsWithASCII does a case insensit
| |
59 chrome::kChromeUIWallpaperThumbnailURL)); | |
60 return ParseIndexFromPath(sub_path, wallpaper_index); | |
61 } | |
62 | |
63 WallpaperThumbnailSource::WallpaperThumbnailSource() | |
64 : DataSource(chrome::kChromeUIWallpaperThumbnailHost, NULL) { | |
65 } | |
66 | |
67 WallpaperThumbnailSource::~WallpaperThumbnailSource() { | |
68 } | |
69 | |
70 void WallpaperThumbnailSource::StartDataRequest(const std::string& path, | |
71 bool is_incognito, | |
72 int request_id) { | |
73 int idr = PathToIDR(path); | |
74 if (idr != -1) { | |
75 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
76 const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
77 SendResponse(request_id, rb.LoadDataResourceBytes(idr)); | |
78 } | |
79 } | |
80 | |
81 std::string WallpaperThumbnailSource::GetMimeType(const std::string&) const { | |
82 return "images/png"; | |
83 } | |
84 | |
85 } // namespace options2 | |
86 } // namespace chromeos | |
OLD | NEW |