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_source.h" | 5 #include "chrome/browser/ui/webui/options2/chromeos/wallpaper_source.h" |
6 | 6 |
7 #include "ash/desktop_background/desktop_background_controller.h" | 7 #include "ash/desktop_background/desktop_background_controller.h" |
8 #include "ash/shell.h" | 8 #include "ash/shell.h" |
9 #include "base/debug/trace_event.h" | 9 #include "base/debug/trace_event.h" |
10 #include "base/synchronization/cancellation_flag.h" | |
11 #include "base/threading/worker_pool.h" | |
12 #include "chrome/browser/chromeos/login/user_manager.h" | 10 #include "chrome/browser/chromeos/login/user_manager.h" |
11 #include "chrome/browser/ui/webui/options2/chromeos/simple_png_encoder.h" | |
13 #include "chrome/common/url_constants.h" | 12 #include "chrome/common/url_constants.h" |
14 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
15 #include "grit/ui_resources.h" | 14 #include "grit/ui_resources.h" |
16 #include "net/base/mime_util.h" | 15 #include "net/base/mime_util.h" |
17 #include "ui/gfx/codec/png_codec.h" | |
18 #include "ui/gfx/size.h" | |
19 #include "ui/gfx/skia_util.h" | |
20 | 16 |
21 extern "C" { | 17 extern "C" { |
csilv
2012/07/23 21:35:18
Looks like we don't need to include zlib.h anymore
rkc
2012/07/24 23:01:44
Done.
| |
22 #if defined(USE_SYSTEM_ZLIB) | 18 #if defined(USE_SYSTEM_ZLIB) |
23 #include <zlib.h> | 19 #include <zlib.h> |
24 #else | 20 #else |
25 #include "third_party/zlib/zlib.h" | 21 #include "third_party/zlib/zlib.h" |
26 #endif | 22 #endif |
27 } | 23 } |
28 | 24 |
29 namespace chromeos { | 25 namespace chromeos { |
30 namespace options2 { | 26 namespace options2 { |
31 | 27 |
32 // Operation class that encodes existing in-memory image as PNG. | |
33 // It uses NO-COMPRESSION to save time. | |
34 class WallpaperImageSource::WallpaperEncodingOperation | |
35 : public base::RefCountedThreadSafe< | |
36 WallpaperImageSource::WallpaperEncodingOperation> { | |
37 public: | |
38 WallpaperEncodingOperation( | |
39 int request_id, | |
40 scoped_refptr<base::RefCountedBytes> data, | |
41 SkBitmap image) | |
42 : request_id_(request_id), | |
43 data_(data), | |
44 image_(image) { | |
45 } | |
46 | |
47 static void Run(scoped_refptr<WallpaperEncodingOperation> weo) { | |
48 weo->EncodeWallpaper(); | |
49 } | |
50 | |
51 int request_id() { | |
52 return request_id_; | |
53 } | |
54 | |
55 void EncodeWallpaper() { | |
56 if (cancel_flag_.IsSet()) | |
57 return; | |
58 TRACE_EVENT0("LOCK_SCREEN", "imageEncoding"); | |
59 SkAutoLockPixels lock_input(image_); | |
60 // Avoid compression to make things faster. | |
61 gfx::PNGCodec::EncodeWithCompressionLevel( | |
62 reinterpret_cast<unsigned char*>(image_.getAddr32(0, 0)), | |
63 gfx::PNGCodec::FORMAT_SkBitmap, | |
64 gfx::Size(image_.width(), image_.height()), | |
65 image_.width() * image_.bytesPerPixel(), | |
66 false, | |
67 std::vector<gfx::PNGCodec::Comment>(), | |
68 Z_NO_COMPRESSION, | |
69 &data_->data()); | |
70 if (cancel_flag_.IsSet()) | |
71 return; | |
72 } | |
73 | |
74 void Cancel() { | |
75 cancel_flag_.Set(); | |
76 } | |
77 | |
78 private: | |
79 friend class base::RefCountedThreadSafe< | |
80 WallpaperImageSource::WallpaperEncodingOperation>; | |
81 | |
82 ~WallpaperEncodingOperation() {} | |
83 | |
84 base::CancellationFlag cancel_flag_; | |
85 | |
86 // ID of original request. | |
87 int request_id_; | |
88 // Buffer to store encoded image. | |
89 scoped_refptr<base::RefCountedBytes> data_; | |
90 // Original image to encode. | |
91 SkBitmap image_; | |
92 | |
93 DISALLOW_COPY_AND_ASSIGN(WallpaperEncodingOperation); | |
94 }; | |
95 | |
96 WallpaperImageSource::WallpaperImageSource() | 28 WallpaperImageSource::WallpaperImageSource() |
97 : DataSource(chrome::kChromeUIWallpaperImageHost, NULL), | 29 : DataSource(chrome::kChromeUIWallpaperImageHost, NULL), |
98 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 30 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
99 } | 31 } |
100 | 32 |
101 WallpaperImageSource::~WallpaperImageSource() { | 33 WallpaperImageSource::~WallpaperImageSource() { |
102 } | 34 } |
103 | 35 |
104 void WallpaperImageSource::StartDataRequest(const std::string& email, | 36 void WallpaperImageSource::StartDataRequest(const std::string& email, |
105 bool is_incognito, | 37 bool is_incognito, |
(...skipping 30 matching lines...) Expand all Loading... | |
136 FROM_HERE, | 68 FROM_HERE, |
137 base::Bind(&WallpaperImageSource::ImageAcquired, this, image, request_id)); | 69 base::Bind(&WallpaperImageSource::ImageAcquired, this, image, request_id)); |
138 } | 70 } |
139 | 71 |
140 | 72 |
141 void WallpaperImageSource::ImageAcquired(SkBitmap image, | 73 void WallpaperImageSource::ImageAcquired(SkBitmap image, |
142 int request_id) { | 74 int request_id) { |
143 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 75 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
144 CancelPendingEncodingOperation(); | 76 CancelPendingEncodingOperation(); |
145 scoped_refptr<base::RefCountedBytes> data = new base::RefCountedBytes(); | 77 scoped_refptr<base::RefCountedBytes> data = new base::RefCountedBytes(); |
146 wallpaper_encoding_op_ = new WallpaperEncodingOperation(request_id, | 78 |
147 data, | 79 png_encoder_ = new SimplePngEncoder( |
148 image); | 80 data, |
149 base::WorkerPool::PostTaskAndReply( | 81 image, |
150 FROM_HERE, | 82 base::Bind(&WallpaperImageSource::CancelCallback, |
151 base::Bind(&WallpaperEncodingOperation::Run, wallpaper_encoding_op_), | 83 weak_ptr_factory_.GetWeakPtr(), |
84 request_id)); | |
85 | |
86 TRACE_EVENT0("LOCK_SCREEN", "imageEncoding"); | |
87 png_encoder_->Run( | |
152 base::Bind(&WallpaperImageSource::SendCurrentUserWallpaper, | 88 base::Bind(&WallpaperImageSource::SendCurrentUserWallpaper, |
153 weak_ptr_factory_.GetWeakPtr(), request_id, data), | 89 weak_ptr_factory_.GetWeakPtr(), |
154 true /* task_is_slow */); | 90 request_id)); |
155 }; | 91 }; |
156 | 92 |
157 void WallpaperImageSource::CancelPendingEncodingOperation() { | 93 void WallpaperImageSource::CancelPendingEncodingOperation() { |
158 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 94 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
159 // Set canceled flag of previous request to skip unneeded encoding. | 95 // Set canceled flag of previous request to skip unneeded encoding. |
160 if (wallpaper_encoding_op_.get()) { | 96 if (png_encoder_.get()) { |
161 wallpaper_encoding_op_->Cancel(); | 97 png_encoder_->Cancel(); |
162 SendResponse(wallpaper_encoding_op_->request_id(), NULL); | |
163 TRACE_EVENT_ASYNC_END0("SCREEN_LOCK", "GetUserWallpaper", | |
164 wallpaper_encoding_op_->request_id()); | |
165 } | 98 } |
166 | 99 |
167 // Cancel reply callback for previous request. | 100 // Cancel the callback for the previous request. |
168 weak_ptr_factory_.InvalidateWeakPtrs(); | 101 weak_ptr_factory_.InvalidateWeakPtrs(); |
169 } | 102 } |
170 | 103 |
104 void WallpaperImageSource::CancelCallback(int request_id) { | |
105 SendResponse(request_id, NULL); | |
106 TRACE_EVENT_ASYNC_END0("SCREEN_LOCK", "GetUserWallpaper", request_id); | |
107 } | |
108 | |
171 void WallpaperImageSource::SendCurrentUserWallpaper(int request_id, | 109 void WallpaperImageSource::SendCurrentUserWallpaper(int request_id, |
172 scoped_refptr<base::RefCountedBytes> data) { | 110 scoped_refptr<base::RefCountedBytes> data) { |
173 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 111 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
174 SendResponse(request_id, data); | 112 SendResponse(request_id, data); |
175 TRACE_EVENT_ASYNC_END0("SCREEN_LOCK", "GetUserWallpaper", request_id); | 113 TRACE_EVENT_ASYNC_END0("SCREEN_LOCK", "GetUserWallpaper", request_id); |
176 } | 114 } |
177 | 115 |
178 } // namespace options2 | 116 } // namespace options2 |
179 } // namespace chromeos | 117 } // namespace chromeos |
OLD | NEW |