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/wallpaper_thumbnail_source2.
h" | |
6 | |
7 #include "ash/desktop_background/desktop_background_resources.h" | |
8 #include "base/memory/ref_counted_memory.h" | |
9 #include "base/memory/singleton.h" | |
10 #include "base/string_number_conversions.h" | |
11 #include "base/string_piece.h" | |
12 #include "base/string_util.h" | |
13 #include "base/stringprintf.h" | |
14 #include "base/threading/thread_restrictions.h" | |
15 #include "chrome/browser/chromeos/login/user_manager.h" | |
16 #include "chrome/browser/io_thread.h" | |
17 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" | |
18 #include "chrome/common/url_constants.h" | |
19 #include "content/public/browser/browser_thread.h" | |
20 #include "grit/ui_resources.h" | |
21 #include "net/base/mime_util.h" | |
22 #include "ui/base/layout.h" | |
23 #include "ui/base/resource/resource_bundle.h" | |
24 #include "ui/gfx/codec/png_codec.h" | |
25 | |
26 namespace chromeos { | |
27 namespace options2 { | |
28 | |
29 namespace { | |
30 | |
31 const char kDefaultWallpaperPrefix[] = "default_"; | |
32 const char kCustomWallpaperPrefix[] = "custom_"; | |
33 | |
34 // Parse an integer from |path| and save it to |index|. For example, default_20 | |
35 // will set |index| to 20. | |
36 // |path| and |index| must not be NULL. | |
37 bool ParseIndexFromPath(const std::string& path, int* index) { | |
38 // TODO(bshe): We should probably save a string instead of index for | |
39 // extensibility. Remove this function when we migrate to string preference. | |
40 DCHECK(index); | |
41 if (!StartsWithASCII(path, kDefaultWallpaperPrefix, false)) | |
42 return false; | |
43 return base::StringToInt(base::StringPiece(path.begin() + | |
44 strlen(kDefaultWallpaperPrefix), path.end()), index); | |
45 } | |
46 | |
47 // Convert |path| to corresponding IDR. Return -1 if the path is invalid. | |
48 // |path| must not be NULL. | |
49 int PathToIDR(const std::string& path) { | |
50 int idr = -1; | |
51 int index = ash::GetInvalidWallpaperIndex(); | |
52 if (ParseIndexFromPath(path, &index)) | |
53 idr = ash::GetWallpaperInfo(index).thumb_id; | |
54 return idr; | |
55 } | |
56 | |
57 // True if |path| is a custom wallpaper thumbnail URL and set |email| parsed | |
58 // from |path|. | |
59 // custom url = "custom_|email|?date" where date is current time. | |
60 bool IsCustomWallpaperPath(const std::string& path, std::string* email) { | |
61 if (!StartsWithASCII(path, kCustomWallpaperPrefix, false)) | |
62 return false; | |
63 | |
64 std::string sub_path = path.substr(strlen(kCustomWallpaperPrefix)); | |
65 *email = sub_path.substr(0, sub_path.find_first_of("?")); | |
66 return true; | |
67 } | |
68 | |
69 } // namespace | |
70 | |
71 std::string GetDefaultWallpaperThumbnailURL(int index) { | |
72 return StringPrintf("%s%s%d", chrome::kChromeUIWallpaperThumbnailURL, | |
73 kDefaultWallpaperPrefix, index); | |
74 } | |
75 | |
76 bool IsDefaultWallpaperURL(const std::string url, int* wallpaper_index) { | |
77 DCHECK(wallpaper_index); | |
78 *wallpaper_index = ash::GetInvalidWallpaperIndex(); | |
79 if (!StartsWithASCII(url, chrome::kChromeUIWallpaperThumbnailURL, false)) | |
80 return false; | |
81 std::string sub_path = url.substr(strlen( | |
82 chrome::kChromeUIWallpaperThumbnailURL)); | |
83 return ParseIndexFromPath(sub_path, wallpaper_index); | |
84 } | |
85 | |
86 WallpaperThumbnailSource::WallpaperThumbnailSource() | |
87 : DataSource(chrome::kChromeUIWallpaperThumbnailHost, NULL) { | |
88 } | |
89 | |
90 WallpaperThumbnailSource::~WallpaperThumbnailSource() { | |
91 } | |
92 | |
93 void WallpaperThumbnailSource::StartDataRequest(const std::string& path, | |
94 bool is_incognito, | |
95 int request_id) { | |
96 std::string email; | |
97 if (IsCustomWallpaperPath(path, &email)) { | |
98 const chromeos::User* user = chromeos::UserManager::Get()->FindUser(email); | |
99 if (user) { | |
100 std::vector<unsigned char> data; | |
101 gfx::PNGCodec::EncodeBGRASkBitmap(user->wallpaper_thumbnail(), | |
102 false, &data); | |
103 SendResponse(request_id, new base::RefCountedBytes(data)); | |
104 return; | |
105 } | |
106 } | |
107 int idr = PathToIDR(path); | |
108 if (idr != -1) { | |
109 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
110 const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
111 SendResponse(request_id, rb.LoadDataResourceBytes(idr, | |
112 ui::SCALE_FACTOR_100P)); | |
113 } | |
114 } | |
115 | |
116 std::string WallpaperThumbnailSource::GetMimeType(const std::string&) const { | |
117 return "images/png"; | |
118 } | |
119 | |
120 } // namespace options2 | |
121 } // namespace chromeos | |
OLD | NEW |