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