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_source2.h" | |
6 | |
7 #include "ash/desktop_background/desktop_background_controller.h" | |
8 #include "ash/shell.h" | |
9 #include "base/synchronization/cancellation_flag.h" | |
10 #include "base/threading/worker_pool.h" | |
11 #include "chrome/browser/chromeos/login/user_manager.h" | |
12 #include "chrome/common/url_constants.h" | |
13 #include "content/public/browser/browser_thread.h" | |
14 #include "grit/ui_resources.h" | |
15 #include "net/base/mime_util.h" | |
16 #include "ui/gfx/codec/png_codec.h" | |
17 #include "ui/gfx/size.h" | |
18 #include "ui/gfx/skia_util.h" | |
19 | |
20 extern "C" { | |
21 #if defined(USE_SYSTEM_ZLIB) | |
22 #include <zlib.h> | |
23 #else | |
24 #include "third_party/zlib/zlib.h" | |
25 #endif | |
26 } | |
27 | |
28 namespace chromeos { | |
29 namespace options2 { | |
30 | |
31 // Operation class that encodes existing in-memory image as PNG. | |
32 // It uses NO-COMPRESSION to save time. | |
33 class WallpaperImageSource::WallpaperEncodingOperation | |
34 : public base::RefCountedThreadSafe< | |
35 WallpaperImageSource::WallpaperEncodingOperation> { | |
36 public: | |
37 WallpaperEncodingOperation( | |
38 int request_id, | |
39 scoped_refptr<base::RefCountedBytes> data, | |
40 SkBitmap image) | |
41 : request_id_(request_id), | |
42 data_(data), | |
43 image_(image) { | |
44 } | |
45 | |
46 static void Run(scoped_refptr<WallpaperEncodingOperation> weo) { | |
47 weo->EncodeWallpaper(); | |
48 } | |
49 | |
50 int request_id() { | |
51 return request_id_; | |
52 } | |
53 | |
54 void EncodeWallpaper() { | |
55 if (cancel_flag_.IsSet()) | |
56 return; | |
57 SkAutoLockPixels lock_input(image_); | |
58 // Avoid compression to make things faster. | |
59 gfx::PNGCodec::EncodeWithCompressionLevel( | |
60 reinterpret_cast<unsigned char*>(image_.getAddr32(0, 0)), | |
61 gfx::PNGCodec::FORMAT_SkBitmap, | |
62 gfx::Size(image_.width(), image_.height()), | |
63 image_.width() * image_.bytesPerPixel(), | |
64 false, | |
65 std::vector<gfx::PNGCodec::Comment>(), | |
66 Z_NO_COMPRESSION, | |
67 &data_->data()); | |
68 if (cancel_flag_.IsSet()) | |
69 return; | |
70 } | |
71 | |
72 void Cancel() { | |
73 cancel_flag_.Set(); | |
74 } | |
75 | |
76 private: | |
77 friend class base::RefCountedThreadSafe< | |
78 WallpaperImageSource::WallpaperEncodingOperation>; | |
79 | |
80 ~WallpaperEncodingOperation() {} | |
81 | |
82 base::CancellationFlag cancel_flag_; | |
83 | |
84 // ID of original request. | |
85 int request_id_; | |
86 // Buffer to store encoded image. | |
87 scoped_refptr<base::RefCountedBytes> data_; | |
88 // Original image to encode. | |
89 SkBitmap image_; | |
90 | |
91 DISALLOW_COPY_AND_ASSIGN(WallpaperEncodingOperation); | |
92 }; | |
93 | |
94 WallpaperImageSource::WallpaperImageSource() | |
95 : DataSource(chrome::kChromeUIWallpaperImageHost, NULL), | |
96 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | |
97 } | |
98 | |
99 WallpaperImageSource::~WallpaperImageSource() { | |
100 } | |
101 | |
102 void WallpaperImageSource::StartDataRequest(const std::string& email, | |
103 bool is_incognito, | |
104 int request_id) { | |
105 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
106 CancelPendingEncodingOperation(); | |
107 content::BrowserThread::PostTask( | |
108 content::BrowserThread::UI, | |
109 FROM_HERE, | |
110 base::Bind(&WallpaperImageSource::GetCurrentUserWallpaper, this, | |
111 request_id)); | |
112 } | |
113 | |
114 std::string WallpaperImageSource::GetMimeType(const std::string&) const { | |
115 return "images/png"; | |
116 } | |
117 | |
118 // Get current background image and store it to |data|. | |
119 void WallpaperImageSource::GetCurrentUserWallpaper(int request_id) { | |
120 SkBitmap image; | |
121 if (chromeos::UserManager::Get()->IsUserLoggedIn()) { | |
122 SkBitmap wallpaper = ash::Shell::GetInstance()-> | |
123 desktop_background_controller()-> | |
124 GetCurrentWallpaperImage(); | |
125 SkBitmap copy; | |
126 if (wallpaper.deepCopyTo(©, wallpaper.config())) | |
127 image = copy; | |
128 } | |
129 content::BrowserThread::PostTask( | |
130 content::BrowserThread::IO, | |
131 FROM_HERE, | |
132 base::Bind(&WallpaperImageSource::ImageAcquired, this, image, request_id)); | |
133 } | |
134 | |
135 | |
136 void WallpaperImageSource::ImageAcquired(SkBitmap image, | |
137 int request_id) { | |
138 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
139 CancelPendingEncodingOperation(); | |
140 scoped_refptr<base::RefCountedBytes> data = new base::RefCountedBytes(); | |
141 wallpaper_encoding_op_ = new WallpaperEncodingOperation(request_id, | |
142 data, | |
143 image); | |
144 base::WorkerPool::PostTaskAndReply( | |
145 FROM_HERE, | |
146 base::Bind(&WallpaperEncodingOperation::Run, wallpaper_encoding_op_), | |
147 base::Bind(&WallpaperImageSource::SendCurrentUserWallpaper, | |
148 weak_ptr_factory_.GetWeakPtr(), request_id, data), | |
149 true /* task_is_slow */); | |
150 }; | |
151 | |
152 void WallpaperImageSource::CancelPendingEncodingOperation() { | |
153 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
154 // Set canceled flag of previous request to skip unneeded encoding. | |
155 if (wallpaper_encoding_op_.get()) { | |
156 wallpaper_encoding_op_->Cancel(); | |
157 SendResponse(wallpaper_encoding_op_->request_id(), NULL); | |
158 } | |
159 | |
160 // Cancel reply callback for previous request. | |
161 weak_ptr_factory_.InvalidateWeakPtrs(); | |
162 } | |
163 | |
164 void WallpaperImageSource::SendCurrentUserWallpaper(int request_id, | |
165 scoped_refptr<base::RefCountedBytes> data) { | |
166 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
167 SendResponse(request_id, data); | |
168 } | |
169 | |
170 } // namespace options2 | |
171 } // namespace chromeos | |
OLD | NEW |