| 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/options/chromeos/wallpaper_thumbnail_source.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/synchronization/cancellation_flag.h" | |
| 15 #include "base/threading/thread_restrictions.h" | |
| 16 #include "base/threading/worker_pool.h" | |
| 17 #include "chrome/browser/chromeos/login/user_manager.h" | |
| 18 #include "chrome/browser/chromeos/login/wallpaper_manager.h" | |
| 19 #include "chrome/browser/io_thread.h" | |
| 20 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" | |
| 21 #include "chrome/browser/ui/webui/web_ui_util.h" | |
| 22 #include "chrome/common/url_constants.h" | |
| 23 #include "content/public/browser/browser_thread.h" | |
| 24 #include "grit/ui_resources.h" | |
| 25 #include "net/base/mime_util.h" | |
| 26 #include "ui/base/layout.h" | |
| 27 #include "ui/base/resource/resource_bundle.h" | |
| 28 #include "ui/gfx/codec/png_codec.h" | |
| 29 | |
| 30 namespace chromeos { | |
| 31 namespace options { | |
| 32 | |
| 33 class WallpaperThumbnailSource::ThumbnailEncodingOperation | |
| 34 : public base::RefCountedThreadSafe< | |
| 35 WallpaperThumbnailSource::ThumbnailEncodingOperation> { | |
| 36 public: | |
| 37 ThumbnailEncodingOperation(int request_id, | |
| 38 const chromeos::User* user, | |
| 39 ui::ScaleFactor scale_factor, | |
| 40 scoped_refptr<base::RefCountedBytes> data) | |
| 41 : request_id_(request_id), | |
| 42 user_(user), | |
| 43 scale_factor_(scale_factor), | |
| 44 data_(data) { | |
| 45 } | |
| 46 | |
| 47 int request_id() { | |
| 48 return request_id_; | |
| 49 } | |
| 50 | |
| 51 static void Run(scoped_refptr<ThumbnailEncodingOperation> teo) { | |
| 52 teo->EncodeThumbnail(); | |
| 53 } | |
| 54 | |
| 55 void EncodeThumbnail() { | |
| 56 if (cancel_flag_.IsSet()) | |
| 57 return; | |
| 58 gfx::ImageSkia image = | |
| 59 WallpaperManager::Get()->GetCustomWallpaperThumbnail(user_->email()); | |
| 60 gfx::PNGCodec::EncodeBGRASkBitmap( | |
| 61 image.GetRepresentation(scale_factor_).sk_bitmap(), | |
| 62 false, &data_->data()); | |
| 63 } | |
| 64 | |
| 65 void Cancel() { | |
| 66 cancel_flag_.Set(); | |
| 67 } | |
| 68 | |
| 69 private: | |
| 70 friend class base::RefCountedThreadSafe< | |
| 71 WallpaperThumbnailSource::ThumbnailEncodingOperation>; | |
| 72 | |
| 73 ~ThumbnailEncodingOperation() {} | |
| 74 | |
| 75 base::CancellationFlag cancel_flag_; | |
| 76 | |
| 77 int request_id_; | |
| 78 | |
| 79 const chromeos::User* user_; | |
| 80 | |
| 81 ui::ScaleFactor scale_factor_; | |
| 82 | |
| 83 scoped_refptr<base::RefCountedBytes> data_; | |
| 84 | |
| 85 DISALLOW_COPY_AND_ASSIGN(ThumbnailEncodingOperation); | |
| 86 }; | |
| 87 | |
| 88 namespace { | |
| 89 | |
| 90 const char kDefaultWallpaperPrefix[] = "default_"; | |
| 91 const char kCustomWallpaperPrefix[] = "custom_"; | |
| 92 | |
| 93 // Parse an integer from |path| and save it to |index|. For example, default_20 | |
| 94 // will set |index| to 20. | |
| 95 // |path| and |index| must not be NULL. | |
| 96 bool ParseIndexFromPath(const std::string& path, int* index) { | |
| 97 // TODO(bshe): We should probably save a string instead of index for | |
| 98 // extensibility. Remove this function when we migrate to string preference. | |
| 99 DCHECK(index); | |
| 100 if (!StartsWithASCII(path, kDefaultWallpaperPrefix, false)) | |
| 101 return false; | |
| 102 return base::StringToInt(base::StringPiece(path.begin() + | |
| 103 strlen(kDefaultWallpaperPrefix), path.end()), index); | |
| 104 } | |
| 105 | |
| 106 // Convert |path| to corresponding IDR. Return -1 if the path is invalid. | |
| 107 // |path| must not be NULL. | |
| 108 // TODO(bshe): This is only used in old wallpaper picker. Remove the whole | |
| 109 // file. | |
| 110 int PathToIDR(const std::string& path) { | |
| 111 return -1; | |
| 112 } | |
| 113 | |
| 114 // True if |path| is a custom wallpaper thumbnail URL and set |email| parsed | |
| 115 // from |path|. | |
| 116 // custom url = "custom_|email|?date" where date is current time. | |
| 117 bool IsCustomWallpaperPath(const std::string& path, std::string* email) { | |
| 118 if (!StartsWithASCII(path, kCustomWallpaperPrefix, false)) | |
| 119 return false; | |
| 120 | |
| 121 std::string sub_path = path.substr(strlen(kCustomWallpaperPrefix)); | |
| 122 *email = sub_path.substr(0, sub_path.find_first_of("?")); | |
| 123 return true; | |
| 124 } | |
| 125 | |
| 126 } // namespace | |
| 127 | |
| 128 std::string GetDefaultWallpaperThumbnailURL(int index) { | |
| 129 return StringPrintf("%s%s%d", chrome::kChromeUIWallpaperThumbnailURL, | |
| 130 kDefaultWallpaperPrefix, index); | |
| 131 } | |
| 132 | |
| 133 bool IsDefaultWallpaperURL(const std::string url, int* wallpaper_index) { | |
| 134 DCHECK(wallpaper_index); | |
| 135 *wallpaper_index = ash::GetInvalidWallpaperIndex(); | |
| 136 if (!StartsWithASCII(url, chrome::kChromeUIWallpaperThumbnailURL, false)) | |
| 137 return false; | |
| 138 std::string sub_path = url.substr(strlen( | |
| 139 chrome::kChromeUIWallpaperThumbnailURL)); | |
| 140 return ParseIndexFromPath(sub_path, wallpaper_index); | |
| 141 } | |
| 142 | |
| 143 WallpaperThumbnailSource::WallpaperThumbnailSource() | |
| 144 : DataSource(chrome::kChromeUIWallpaperThumbnailHost, NULL), | |
| 145 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | |
| 146 } | |
| 147 | |
| 148 std::string WallpaperThumbnailSource::GetMimeType(const std::string&) const { | |
| 149 return "images/png"; | |
| 150 } | |
| 151 | |
| 152 void WallpaperThumbnailSource::StartDataRequest(const std::string& full_path, | |
| 153 bool is_incognito, | |
| 154 int request_id) { | |
| 155 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
| 156 GURL url(chrome::kChromeUIWallpaperThumbnailURL + full_path); | |
| 157 std::string path; | |
| 158 ui::ScaleFactor scale_factor; | |
| 159 web_ui_util::ParsePathAndScale(url, &path, &scale_factor); | |
| 160 | |
| 161 CancelPendingCustomThumbnailEncodingOperation(); | |
| 162 content::BrowserThread::PostTask( | |
| 163 content::BrowserThread::UI, | |
| 164 FROM_HERE, | |
| 165 base::Bind(&WallpaperThumbnailSource::GetCurrentUserThumbnail, | |
| 166 this, path, scale_factor, request_id)); | |
| 167 } | |
| 168 | |
| 169 WallpaperThumbnailSource::~WallpaperThumbnailSource() { | |
| 170 } | |
| 171 | |
| 172 void WallpaperThumbnailSource::GetCurrentUserThumbnail( | |
| 173 const std::string& path, | |
| 174 ui::ScaleFactor scale_factor, | |
| 175 int request_id) { | |
| 176 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 177 std::string email; | |
| 178 if (IsCustomWallpaperPath(path, &email)) { | |
| 179 const chromeos::User* user = chromeos::UserManager::Get()->FindUser(email); | |
| 180 if (!user) { | |
| 181 content::BrowserThread::PostTask( | |
| 182 content::BrowserThread::IO, | |
| 183 FROM_HERE, | |
| 184 base::Bind(&WallpaperThumbnailSource::SendCurrentUserNullThumbnail, | |
| 185 this, request_id)); | |
| 186 return; | |
| 187 } | |
| 188 content::BrowserThread::PostTask( | |
| 189 content::BrowserThread::IO, | |
| 190 FROM_HERE, | |
| 191 base::Bind( | |
| 192 &WallpaperThumbnailSource::StartCustomThumbnailEncodingOperation, | |
| 193 this, user, scale_factor, request_id)); | |
| 194 return; | |
| 195 } | |
| 196 content::BrowserThread::PostTask( | |
| 197 content::BrowserThread::IO, | |
| 198 FROM_HERE, | |
| 199 base::Bind(&WallpaperThumbnailSource::SendCurrentUserDefaultThumbnail, | |
| 200 this, path, scale_factor, request_id)); | |
| 201 } | |
| 202 | |
| 203 void WallpaperThumbnailSource::StartCustomThumbnailEncodingOperation( | |
| 204 const chromeos::User* user, | |
| 205 ui::ScaleFactor scale_factor, | |
| 206 int request_id) { | |
| 207 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
| 208 CancelPendingCustomThumbnailEncodingOperation(); | |
| 209 scoped_refptr<base::RefCountedBytes> data = new base::RefCountedBytes(); | |
| 210 thumbnail_encoding_op_ = new ThumbnailEncodingOperation(request_id, | |
| 211 user, | |
| 212 scale_factor, | |
| 213 data); | |
| 214 base::WorkerPool::PostTaskAndReply( | |
| 215 FROM_HERE, | |
| 216 base::Bind(&ThumbnailEncodingOperation::Run, thumbnail_encoding_op_), | |
| 217 base::Bind(&WallpaperThumbnailSource::SendCurrentUserCustomThumbnail, | |
| 218 weak_ptr_factory_.GetWeakPtr(), data, request_id), | |
| 219 true /* task_is_slow */); | |
| 220 } | |
| 221 | |
| 222 void WallpaperThumbnailSource::CancelPendingCustomThumbnailEncodingOperation() { | |
| 223 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
| 224 if (thumbnail_encoding_op_.get()) { | |
| 225 thumbnail_encoding_op_->Cancel(); | |
| 226 SendResponse(thumbnail_encoding_op_->request_id(), NULL); | |
| 227 } | |
| 228 weak_ptr_factory_.InvalidateWeakPtrs(); | |
| 229 } | |
| 230 | |
| 231 void WallpaperThumbnailSource::SendCurrentUserNullThumbnail(int request_id) { | |
| 232 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
| 233 SendResponse(request_id, NULL); | |
| 234 } | |
| 235 | |
| 236 void WallpaperThumbnailSource::SendCurrentUserCustomThumbnail( | |
| 237 scoped_refptr<base::RefCountedBytes> data, | |
| 238 int request_id) { | |
| 239 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
| 240 SendResponse(request_id, data); | |
| 241 } | |
| 242 | |
| 243 void WallpaperThumbnailSource::SendCurrentUserDefaultThumbnail( | |
| 244 const std::string& path, | |
| 245 ui::ScaleFactor scale_factor, | |
| 246 int request_id) { | |
| 247 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
| 248 int idr = PathToIDR(path); | |
| 249 if (idr == -1) { | |
| 250 SendResponse(request_id, NULL); | |
| 251 return; | |
| 252 } | |
| 253 const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
| 254 SendResponse(request_id, | |
| 255 rb.LoadDataResourceBytesForScale(idr, scale_factor)); | |
| 256 } | |
| 257 | |
| 258 } // namespace options | |
| 259 } // namespace chromeos | |
| OLD | NEW |