Chromium Code Reviews| 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 return false; | |
| 62 email = path.substr(strlen(kCustomWallpaperPrefix), | |
| 63 path.find_first_of("?") - strlen(kCustomWallpaperPrefix)); | |
|
flackr
2012/05/04 19:06:17
What if there is no "?"?
bshe
2012/05/08 22:22:18
If the path can pass the first StartsWithASCII che
| |
| 64 return true; | |
| 65 } | |
| 66 | |
| 54 } // namespace | 67 } // namespace |
| 55 | 68 |
| 56 std::string GetDefaultWallpaperThumbnailURL(int index) { | 69 std::string GetDefaultWallpaperThumbnailURL(int index) { |
| 57 return StringPrintf("%s%s%d", chrome::kChromeUIWallpaperThumbnailURL, | 70 return StringPrintf("%s%s%d", chrome::kChromeUIWallpaperThumbnailURL, |
| 58 kDefaultWallpaperPrefix, index); | 71 kDefaultWallpaperPrefix, index); |
| 59 } | 72 } |
| 60 | 73 |
| 61 bool IsDefaultWallpaperURL(const std::string url, int* wallpaper_index) { | 74 bool IsDefaultWallpaperURL(const std::string url, int* wallpaper_index) { |
| 62 DCHECK(wallpaper_index); | 75 DCHECK(wallpaper_index); |
| 63 *wallpaper_index = ash::GetInvalidWallpaperIndex(); | 76 *wallpaper_index = ash::GetInvalidWallpaperIndex(); |
| 64 if (!StartsWithASCII(url, chrome::kChromeUIWallpaperThumbnailURL, false)) | 77 if (!StartsWithASCII(url, chrome::kChromeUIWallpaperThumbnailURL, false)) |
| 65 return false; | 78 return false; |
| 66 std::string sub_path = url.substr(strlen( | 79 std::string sub_path = url.substr(strlen( |
| 67 chrome::kChromeUIWallpaperThumbnailURL)); | 80 chrome::kChromeUIWallpaperThumbnailURL)); |
| 68 return ParseIndexFromPath(sub_path, wallpaper_index); | 81 return ParseIndexFromPath(sub_path, wallpaper_index); |
| 69 } | 82 } |
| 70 | 83 |
| 71 WallpaperThumbnailSource::WallpaperThumbnailSource() | 84 WallpaperThumbnailSource::WallpaperThumbnailSource() |
| 72 : DataSource(chrome::kChromeUIWallpaperThumbnailHost, NULL) { | 85 : DataSource(chrome::kChromeUIWallpaperThumbnailHost, NULL) { |
| 73 } | 86 } |
| 74 | 87 |
| 75 WallpaperThumbnailSource::~WallpaperThumbnailSource() { | 88 WallpaperThumbnailSource::~WallpaperThumbnailSource() { |
| 76 } | 89 } |
| 77 | 90 |
| 78 void WallpaperThumbnailSource::StartDataRequest(const std::string& path, | 91 void WallpaperThumbnailSource::StartDataRequest(const std::string& path, |
| 79 bool is_incognito, | 92 bool is_incognito, |
| 80 int request_id) { | 93 int request_id) { |
| 94 std::string email; | |
| 95 if (IsCustomWallpaperPath(path, email)) { | |
| 96 const chromeos::User* user = chromeos::UserManager::Get()->FindUser(email); | |
| 97 if (user) { | |
| 98 std::vector<unsigned char> data; | |
| 99 gfx::PNGCodec::EncodeBGRASkBitmap(user->wallpaper_thumb(), false, &data); | |
| 100 SendResponse(request_id, new base::RefCountedBytes(data)); | |
| 101 return; | |
| 102 } | |
| 103 } | |
| 81 int idr = PathToIDR(path); | 104 int idr = PathToIDR(path); |
| 82 if (idr != -1) { | 105 if (idr != -1) { |
| 83 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 106 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 84 const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 107 const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 85 SendResponse(request_id, rb.LoadDataResourceBytes(idr)); | 108 SendResponse(request_id, rb.LoadDataResourceBytes(idr)); |
| 86 } | 109 } |
| 87 } | 110 } |
| 88 | 111 |
| 89 std::string WallpaperThumbnailSource::GetMimeType(const std::string&) const { | 112 std::string WallpaperThumbnailSource::GetMimeType(const std::string&) const { |
| 90 return "images/png"; | 113 return "images/png"; |
| 91 } | 114 } |
| 92 | 115 |
| 93 } // namespace options2 | 116 } // namespace options2 |
| 94 } // namespace chromeos | 117 } // namespace chromeos |
| OLD | NEW |