Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(389)

Side by Side Diff: chrome/browser/ui/webui/options2/chromeos/wallpaper_source.cc

Issue 10837331: Options: s/options2/options/. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: wut Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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_source.h"
6
7 #include "ash/desktop_background/desktop_background_controller.h"
8 #include "ash/shell.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"
13 #include "chrome/common/url_constants.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "grit/ui_resources.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 }
28
29 namespace chromeos {
30 namespace options {
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
61 if (!image_.readyToDraw())
62 return;
63
64 // Avoid compression to make things faster.
65 gfx::PNGCodec::EncodeWithCompressionLevel(
66 reinterpret_cast<unsigned char*>(image_.getAddr32(0, 0)),
67 gfx::PNGCodec::FORMAT_SkBitmap,
68 gfx::Size(image_.width(), image_.height()),
69 image_.width() * image_.bytesPerPixel(),
70 false,
71 std::vector<gfx::PNGCodec::Comment>(),
72 Z_NO_COMPRESSION,
73 &data_->data());
74 if (cancel_flag_.IsSet())
75 return;
76 }
77
78 void Cancel() {
79 cancel_flag_.Set();
80 }
81
82 private:
83 friend class base::RefCountedThreadSafe<
84 WallpaperImageSource::WallpaperEncodingOperation>;
85
86 ~WallpaperEncodingOperation() {}
87
88 base::CancellationFlag cancel_flag_;
89
90 // ID of original request.
91 int request_id_;
92 // Buffer to store encoded image.
93 scoped_refptr<base::RefCountedBytes> data_;
94 // Original image to encode.
95 SkBitmap image_;
96
97 DISALLOW_COPY_AND_ASSIGN(WallpaperEncodingOperation);
98 };
99
100 WallpaperImageSource::WallpaperImageSource()
101 : DataSource(chrome::kChromeUIWallpaperImageHost, NULL),
102 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
103 }
104
105 WallpaperImageSource::~WallpaperImageSource() {
106 }
107
108 void WallpaperImageSource::StartDataRequest(const std::string& email,
109 bool is_incognito,
110 int request_id) {
111 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
112 TRACE_EVENT_ASYNC_BEGIN0("SCREEN_LOCK", "GetUserWallpaperDataRequest",
113 request_id);
114 CancelPendingEncodingOperation();
115 content::BrowserThread::PostTask(
116 content::BrowserThread::UI,
117 FROM_HERE,
118 base::Bind(&WallpaperImageSource::GetCurrentUserWallpaper, this,
119 request_id));
120 }
121
122 std::string WallpaperImageSource::GetMimeType(const std::string&) const {
123 return "images/png";
124 }
125
126 // Get current background image and store it to |data|.
127 void WallpaperImageSource::GetCurrentUserWallpaper(int request_id) {
128 SkBitmap image;
129 TRACE_EVENT0("LOCK_SCREEN", "GetCurrentUserWallpaper");
130 if (chromeos::UserManager::Get()->IsUserLoggedIn()) {
131 // TODO(sad|bshe): It maybe necessary to include the scale factor in the
132 // request (as is done for user-image and wallpaper-thumbnails).
133 SkBitmap wallpaper;
134 gfx::ImageSkia wallpaper_skia = ash::Shell::GetInstance()->
135 desktop_background_controller()->GetCurrentWallpaperImage();
136 if (!wallpaper_skia.empty())
137 wallpaper = *wallpaper_skia.bitmap();
138 SkBitmap copy;
139 if (wallpaper.deepCopyTo(&copy, wallpaper.config()))
140 image = copy;
141 }
142 content::BrowserThread::PostTask(
143 content::BrowserThread::IO,
144 FROM_HERE,
145 base::Bind(&WallpaperImageSource::ImageAcquired, this, image, request_id));
146 }
147
148
149 void WallpaperImageSource::ImageAcquired(SkBitmap image,
150 int request_id) {
151 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
152 CancelPendingEncodingOperation();
153 scoped_refptr<base::RefCountedBytes> data = new base::RefCountedBytes();
154 wallpaper_encoding_op_ = new WallpaperEncodingOperation(request_id,
155 data,
156 image);
157 base::WorkerPool::PostTaskAndReply(
158 FROM_HERE,
159 base::Bind(&WallpaperEncodingOperation::Run, wallpaper_encoding_op_),
160 base::Bind(&WallpaperImageSource::SendCurrentUserWallpaper,
161 weak_ptr_factory_.GetWeakPtr(), request_id, data),
162 true /* task_is_slow */);
163 };
164
165 void WallpaperImageSource::CancelPendingEncodingOperation() {
166 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
167 // Set canceled flag of previous request to skip unneeded encoding.
168 if (wallpaper_encoding_op_.get()) {
169 wallpaper_encoding_op_->Cancel();
170 SendResponse(wallpaper_encoding_op_->request_id(), NULL);
171 TRACE_EVENT_ASYNC_END0("SCREEN_LOCK", "GetUserWallpaper",
172 wallpaper_encoding_op_->request_id());
173 }
174
175 // Cancel reply callback for previous request.
176 weak_ptr_factory_.InvalidateWeakPtrs();
177 }
178
179 void WallpaperImageSource::SendCurrentUserWallpaper(int request_id,
180 scoped_refptr<base::RefCountedBytes> data) {
181 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
182 SendResponse(request_id, data);
183 TRACE_EVENT_ASYNC_END0("SCREEN_LOCK", "GetUserWallpaper", request_id);
184 }
185
186 } // namespace options
187 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698