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