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 | |
21 extern "C" { | |
22 #if defined(USE_SYSTEM_ZLIB) | |
23 #include <zlib.h> | |
24 #else | |
25 #include "third_party/zlib/zlib.h" | |
26 #endif | |
27 } | |
28 | 16 |
29 namespace chromeos { | 17 namespace chromeos { |
30 namespace options2 { | 18 namespace options2 { |
31 | 19 |
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() | 20 WallpaperImageSource::WallpaperImageSource() |
97 : DataSource(chrome::kChromeUIWallpaperImageHost, NULL), | 21 : DataSource(chrome::kChromeUIWallpaperImageHost, NULL), |
98 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 22 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
99 } | 23 } |
100 | 24 |
101 WallpaperImageSource::~WallpaperImageSource() { | 25 WallpaperImageSource::~WallpaperImageSource() { |
102 } | 26 } |
103 | 27 |
104 void WallpaperImageSource::StartDataRequest(const std::string& email, | 28 void WallpaperImageSource::StartDataRequest(const std::string& email, |
105 bool is_incognito, | 29 bool is_incognito, |
(...skipping 30 matching lines...) Expand all Loading... |
136 FROM_HERE, | 60 FROM_HERE, |
137 base::Bind(&WallpaperImageSource::ImageAcquired, this, image, request_id)); | 61 base::Bind(&WallpaperImageSource::ImageAcquired, this, image, request_id)); |
138 } | 62 } |
139 | 63 |
140 | 64 |
141 void WallpaperImageSource::ImageAcquired(SkBitmap image, | 65 void WallpaperImageSource::ImageAcquired(SkBitmap image, |
142 int request_id) { | 66 int request_id) { |
143 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 67 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
144 CancelPendingEncodingOperation(); | 68 CancelPendingEncodingOperation(); |
145 scoped_refptr<base::RefCountedBytes> data = new base::RefCountedBytes(); | 69 scoped_refptr<base::RefCountedBytes> data = new base::RefCountedBytes(); |
146 wallpaper_encoding_op_ = new WallpaperEncodingOperation(request_id, | 70 |
147 data, | 71 png_encoder_ = new SimplePngEncoder( |
148 image); | 72 data, |
149 base::WorkerPool::PostTaskAndReply( | 73 image, |
150 FROM_HERE, | 74 base::Bind(&WallpaperImageSource::CancelCallback, |
151 base::Bind(&WallpaperEncodingOperation::Run, wallpaper_encoding_op_), | 75 weak_ptr_factory_.GetWeakPtr(), |
| 76 request_id)); |
| 77 |
| 78 TRACE_EVENT0("LOCK_SCREEN", "imageEncoding"); |
| 79 png_encoder_->Run( |
152 base::Bind(&WallpaperImageSource::SendCurrentUserWallpaper, | 80 base::Bind(&WallpaperImageSource::SendCurrentUserWallpaper, |
153 weak_ptr_factory_.GetWeakPtr(), request_id, data), | 81 weak_ptr_factory_.GetWeakPtr(), |
154 true /* task_is_slow */); | 82 request_id)); |
155 }; | 83 }; |
156 | 84 |
157 void WallpaperImageSource::CancelPendingEncodingOperation() { | 85 void WallpaperImageSource::CancelPendingEncodingOperation() { |
158 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 86 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
159 // Set canceled flag of previous request to skip unneeded encoding. | 87 // Set canceled flag of previous request to skip unneeded encoding. |
160 if (wallpaper_encoding_op_.get()) { | 88 if (png_encoder_.get()) { |
161 wallpaper_encoding_op_->Cancel(); | 89 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 } | 90 } |
166 | 91 |
167 // Cancel reply callback for previous request. | 92 // Cancel the callback for the previous request. |
168 weak_ptr_factory_.InvalidateWeakPtrs(); | 93 weak_ptr_factory_.InvalidateWeakPtrs(); |
169 } | 94 } |
170 | 95 |
| 96 void WallpaperImageSource::CancelCallback(int request_id) { |
| 97 SendResponse(request_id, NULL); |
| 98 TRACE_EVENT_ASYNC_END0("SCREEN_LOCK", "GetUserWallpaper", request_id); |
| 99 } |
| 100 |
171 void WallpaperImageSource::SendCurrentUserWallpaper(int request_id, | 101 void WallpaperImageSource::SendCurrentUserWallpaper(int request_id, |
172 scoped_refptr<base::RefCountedBytes> data) { | 102 scoped_refptr<base::RefCountedBytes> data) { |
173 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 103 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
174 SendResponse(request_id, data); | 104 SendResponse(request_id, data); |
175 TRACE_EVENT_ASYNC_END0("SCREEN_LOCK", "GetUserWallpaper", request_id); | 105 TRACE_EVENT_ASYNC_END0("SCREEN_LOCK", "GetUserWallpaper", request_id); |
176 } | 106 } |
177 | 107 |
178 } // namespace options2 | 108 } // namespace options2 |
179 } // namespace chromeos | 109 } // namespace chromeos |
OLD | NEW |