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_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 int PathToIDR(const std::string& path) { | |
109 int idr = -1; | |
110 int index = ash::GetInvalidWallpaperIndex(); | |
111 if (ParseIndexFromPath(path, &index)) | |
112 idr = ash::GetWallpaperInfo(index).thumb_id; | |
113 return idr; | |
114 } | |
115 | |
116 // True if |path| is a custom wallpaper thumbnail URL and set |email| parsed | |
117 // from |path|. | |
118 // custom url = "custom_|email|?date" where date is current time. | |
119 bool IsCustomWallpaperPath(const std::string& path, std::string* email) { | |
120 if (!StartsWithASCII(path, kCustomWallpaperPrefix, false)) | |
121 return false; | |
122 | |
123 std::string sub_path = path.substr(strlen(kCustomWallpaperPrefix)); | |
124 *email = sub_path.substr(0, sub_path.find_first_of("?")); | |
125 return true; | |
126 } | |
127 | |
128 } // namespace | |
129 | |
130 std::string GetDefaultWallpaperThumbnailURL(int index) { | |
131 return StringPrintf("%s%s%d", chrome::kChromeUIWallpaperThumbnailURL, | |
132 kDefaultWallpaperPrefix, index); | |
133 } | |
134 | |
135 bool IsDefaultWallpaperURL(const std::string url, int* wallpaper_index) { | |
136 DCHECK(wallpaper_index); | |
137 *wallpaper_index = ash::GetInvalidWallpaperIndex(); | |
138 if (!StartsWithASCII(url, chrome::kChromeUIWallpaperThumbnailURL, false)) | |
139 return false; | |
140 std::string sub_path = url.substr(strlen( | |
141 chrome::kChromeUIWallpaperThumbnailURL)); | |
142 return ParseIndexFromPath(sub_path, wallpaper_index); | |
143 } | |
144 | |
145 WallpaperThumbnailSource::WallpaperThumbnailSource() | |
146 : DataSource(chrome::kChromeUIWallpaperThumbnailHost, NULL), | |
147 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | |
148 } | |
149 | |
150 std::string WallpaperThumbnailSource::GetMimeType(const std::string&) const { | |
151 return "images/png"; | |
152 } | |
153 | |
154 void WallpaperThumbnailSource::StartDataRequest(const std::string& full_path, | |
155 bool is_incognito, | |
156 int request_id) { | |
157 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
158 GURL url(chrome::kChromeUIWallpaperThumbnailURL + full_path); | |
159 std::string path; | |
160 ui::ScaleFactor scale_factor; | |
161 web_ui_util::ParsePathAndScale(url, &path, &scale_factor); | |
162 | |
163 CancelPendingCustomThumbnailEncodingOperation(); | |
164 content::BrowserThread::PostTask( | |
165 content::BrowserThread::UI, | |
166 FROM_HERE, | |
167 base::Bind(&WallpaperThumbnailSource::GetCurrentUserThumbnail, | |
168 this, path, scale_factor, request_id)); | |
169 } | |
170 | |
171 WallpaperThumbnailSource::~WallpaperThumbnailSource() { | |
172 } | |
173 | |
174 void WallpaperThumbnailSource::GetCurrentUserThumbnail( | |
175 const std::string& path, | |
176 ui::ScaleFactor scale_factor, | |
177 int request_id) { | |
178 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
179 std::string email; | |
180 if (IsCustomWallpaperPath(path, &email)) { | |
181 const chromeos::User* user = chromeos::UserManager::Get()->FindUser(email); | |
182 if (!user) { | |
183 content::BrowserThread::PostTask( | |
184 content::BrowserThread::IO, | |
185 FROM_HERE, | |
186 base::Bind(&WallpaperThumbnailSource::SendCurrentUserNullThumbnail, | |
187 this, request_id)); | |
188 return; | |
189 } | |
190 content::BrowserThread::PostTask( | |
191 content::BrowserThread::IO, | |
192 FROM_HERE, | |
193 base::Bind( | |
194 &WallpaperThumbnailSource::StartCustomThumbnailEncodingOperation, | |
195 this, user, scale_factor, request_id)); | |
196 return; | |
197 } | |
198 content::BrowserThread::PostTask( | |
199 content::BrowserThread::IO, | |
200 FROM_HERE, | |
201 base::Bind(&WallpaperThumbnailSource::SendCurrentUserDefaultThumbnail, | |
202 this, path, scale_factor, request_id)); | |
203 } | |
204 | |
205 void WallpaperThumbnailSource::StartCustomThumbnailEncodingOperation( | |
206 const chromeos::User* user, | |
207 ui::ScaleFactor scale_factor, | |
208 int request_id) { | |
209 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
210 CancelPendingCustomThumbnailEncodingOperation(); | |
211 scoped_refptr<base::RefCountedBytes> data = new base::RefCountedBytes(); | |
212 thumbnail_encoding_op_ = new ThumbnailEncodingOperation(request_id, | |
213 user, | |
214 scale_factor, | |
215 data); | |
216 base::WorkerPool::PostTaskAndReply( | |
217 FROM_HERE, | |
218 base::Bind(&ThumbnailEncodingOperation::Run, thumbnail_encoding_op_), | |
219 base::Bind(&WallpaperThumbnailSource::SendCurrentUserCustomThumbnail, | |
220 weak_ptr_factory_.GetWeakPtr(), data, request_id), | |
221 true /* task_is_slow */); | |
222 } | |
223 | |
224 void WallpaperThumbnailSource::CancelPendingCustomThumbnailEncodingOperation() { | |
225 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
226 if (thumbnail_encoding_op_.get()) { | |
227 thumbnail_encoding_op_->Cancel(); | |
228 SendResponse(thumbnail_encoding_op_->request_id(), NULL); | |
229 } | |
230 weak_ptr_factory_.InvalidateWeakPtrs(); | |
231 } | |
232 | |
233 void WallpaperThumbnailSource::SendCurrentUserNullThumbnail(int request_id) { | |
234 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
235 SendResponse(request_id, NULL); | |
236 } | |
237 | |
238 void WallpaperThumbnailSource::SendCurrentUserCustomThumbnail( | |
239 scoped_refptr<base::RefCountedBytes> data, | |
240 int request_id) { | |
241 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
242 SendResponse(request_id, data); | |
243 } | |
244 | |
245 void WallpaperThumbnailSource::SendCurrentUserDefaultThumbnail( | |
246 const std::string& path, | |
247 ui::ScaleFactor scale_factor, | |
248 int request_id) { | |
249 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
250 int idr = PathToIDR(path); | |
251 if (idr == -1) { | |
252 SendResponse(request_id, NULL); | |
253 return; | |
254 } | |
255 const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
256 SendResponse(request_id, | |
257 rb.LoadDataResourceBytes(idr, scale_factor)); | |
258 } | |
259 | |
260 } // namespace options | |
261 } // namespace chromeos | |
OLD | NEW |