1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/frame_host/navigation_entry_screenshot_manager.h"
#include "base/command_line.h"
#include "base/threading/worker_pool.h"
#include "content/browser/frame_host/navigation_controller_impl.h"
#include "content/browser/frame_host/navigation_entry_impl.h"
#include "content/browser/renderer_host/render_view_host_impl.h"
#include "content/public/browser/render_widget_host.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/common/content_switches.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkPaint.h"
#include "third_party/skia/include/effects/SkLumaColorFilter.h"
#include "ui/gfx/codec/png_codec.h"
namespace {
// Minimum delay between taking screenshots.
const int kMinScreenshotIntervalMS = 1000;
}
namespace content {
// Converts SkBitmap to grayscale and encodes to PNG data in a worker thread.
class ScreenshotData : public base::RefCountedThreadSafe<ScreenshotData> {
public:
ScreenshotData() {
}
void EncodeScreenshot(const SkBitmap& bitmap, base::Closure callback) {
if (!base::WorkerPool::PostTaskAndReply(FROM_HERE,
base::Bind(&ScreenshotData::EncodeOnWorker,
this,
bitmap),
callback,
true)) {
callback.Run();
}
}
scoped_refptr<base::RefCountedBytes> data() const { return data_; }
private:
friend class base::RefCountedThreadSafe<ScreenshotData>;
virtual ~ScreenshotData() {
}
void EncodeOnWorker(const SkBitmap& bitmap) {
std::vector<unsigned char> data;
// Paint |bitmap| to a kA8_Config SkBitmap
SkBitmap a8Bitmap;
a8Bitmap.setConfig(SkBitmap::kA8_Config,
bitmap.width(),
bitmap.height(),
0);
a8Bitmap.allocPixels();
SkCanvas canvas(a8Bitmap);
SkPaint paint;
SkColorFilter* filter = SkLumaColorFilter::Create();
paint.setColorFilter(filter);
filter->unref();
canvas.drawBitmap(bitmap, SkIntToScalar(0), SkIntToScalar(0), &paint);
// Encode the a8Bitmap to grayscale PNG treating alpha as color intensity
if (gfx::PNGCodec::EncodeA8SkBitmap(a8Bitmap, &data))
data_ = new base::RefCountedBytes(data);
}
scoped_refptr<base::RefCountedBytes> data_;
DISALLOW_COPY_AND_ASSIGN(ScreenshotData);
};
NavigationEntryScreenshotManager::NavigationEntryScreenshotManager(
NavigationControllerImpl* owner)
: owner_(owner),
screenshot_factory_(this),
min_screenshot_interval_ms_(kMinScreenshotIntervalMS) {
}
NavigationEntryScreenshotManager::~NavigationEntryScreenshotManager() {
}
void NavigationEntryScreenshotManager::TakeScreenshot() {
static bool overscroll_enabled = CommandLine::ForCurrentProcess()->
GetSwitchValueASCII(switches::kOverscrollHistoryNavigation) != "0";
if (!overscroll_enabled)
return;
NavigationEntryImpl* entry =
NavigationEntryImpl::FromNavigationEntry(owner_->GetLastCommittedEntry());
if (!entry)
return;
RenderViewHost* render_view_host =
owner_->delegate()->GetRenderViewHost();
if (!static_cast<RenderViewHostImpl*>
(render_view_host)->overscroll_controller()) {
return;
}
content::RenderWidgetHostView* view = render_view_host->GetView();
if (!view)
return;
// Make sure screenshots aren't taken too frequently.
base::Time now = base::Time::Now();
if (now - last_screenshot_time_ <
base::TimeDelta::FromMilliseconds(min_screenshot_interval_ms_)) {
return;
}
last_screenshot_time_ = now;
TakeScreenshotImpl(render_view_host, entry);
}
// Implemented here and not in NavigationEntry because this manager keeps track
// of the total number of screen shots across all entries.
void NavigationEntryScreenshotManager::ClearAllScreenshots() {
int count = owner_->GetEntryCount();
for (int i = 0; i < count; ++i) {
ClearScreenshot(NavigationEntryImpl::FromNavigationEntry(
owner_->GetEntryAtIndex(i)));
}
DCHECK_EQ(GetScreenshotCount(), 0);
}
void NavigationEntryScreenshotManager::TakeScreenshotImpl(
RenderViewHost* host,
NavigationEntryImpl* entry) {
DCHECK(host && host->GetView());
DCHECK(entry);
SkBitmap::Config preferred_format = host->PreferredReadbackFormat();
host->CopyFromBackingStore(
gfx::Rect(),
host->GetView()->GetViewBounds().size(),
base::Bind(&NavigationEntryScreenshotManager::OnScreenshotTaken,
screenshot_factory_.GetWeakPtr(),
entry->GetUniqueID()),
preferred_format);
}
void NavigationEntryScreenshotManager::SetMinScreenshotIntervalMS(
int interval_ms) {
DCHECK_GE(interval_ms, 0);
min_screenshot_interval_ms_ = interval_ms;
}
void NavigationEntryScreenshotManager::OnScreenshotTaken(int unique_id,
bool success,
const SkBitmap& bitmap) {
NavigationEntryImpl* entry = NULL;
int entry_count = owner_->GetEntryCount();
for (int i = 0; i < entry_count; ++i) {
NavigationEntry* iter = owner_->GetEntryAtIndex(i);
if (iter->GetUniqueID() == unique_id) {
entry = NavigationEntryImpl::FromNavigationEntry(iter);
break;
}
}
if (!entry) {
LOG(ERROR) << "Invalid entry with unique id: " << unique_id;
return;
}
if (!success || bitmap.empty() || bitmap.isNull()) {
if (!ClearScreenshot(entry))
OnScreenshotSet(entry);
return;
}
scoped_refptr<ScreenshotData> screenshot = new ScreenshotData();
screenshot->EncodeScreenshot(
bitmap,
base::Bind(&NavigationEntryScreenshotManager::OnScreenshotEncodeComplete,
screenshot_factory_.GetWeakPtr(),
unique_id,
screenshot));
}
int NavigationEntryScreenshotManager::GetScreenshotCount() const {
int screenshot_count = 0;
int entry_count = owner_->GetEntryCount();
for (int i = 0; i < entry_count; ++i) {
NavigationEntryImpl* entry =
NavigationEntryImpl::FromNavigationEntry(owner_->GetEntryAtIndex(i));
if (entry->screenshot().get())
screenshot_count++;
}
return screenshot_count;
}
void NavigationEntryScreenshotManager::OnScreenshotEncodeComplete(
int unique_id,
scoped_refptr<ScreenshotData> screenshot) {
NavigationEntryImpl* entry = NULL;
int entry_count = owner_->GetEntryCount();
for (int i = 0; i < entry_count; ++i) {
NavigationEntry* iter = owner_->GetEntryAtIndex(i);
if (iter->GetUniqueID() == unique_id) {
entry = NavigationEntryImpl::FromNavigationEntry(iter);
break;
}
}
if (!entry)
return;
entry->SetScreenshotPNGData(screenshot->data());
OnScreenshotSet(entry);
}
void NavigationEntryScreenshotManager::OnScreenshotSet(
NavigationEntryImpl* entry) {
if (entry->screenshot().get())
PurgeScreenshotsIfNecessary();
}
bool NavigationEntryScreenshotManager::ClearScreenshot(
NavigationEntryImpl* entry) {
if (!entry->screenshot().get())
return false;
entry->SetScreenshotPNGData(NULL);
return true;
}
void NavigationEntryScreenshotManager::PurgeScreenshotsIfNecessary() {
// Allow only a certain number of entries to keep screenshots.
const int kMaxScreenshots = 10;
int screenshot_count = GetScreenshotCount();
if (screenshot_count < kMaxScreenshots)
return;
const int current = owner_->GetCurrentEntryIndex();
const int num_entries = owner_->GetEntryCount();
int available_slots = kMaxScreenshots;
if (NavigationEntryImpl::FromNavigationEntry(owner_->GetEntryAtIndex(current))
->screenshot().get()) {
--available_slots;
}
// Keep screenshots closer to the current navigation entry, and purge the ones
// that are farther away from it. So in each step, look at the entries at
// each offset on both the back and forward history, and start counting them
// to make sure that the correct number of screenshots are kept in memory.
// Note that it is possible for some entries to be missing screenshots (e.g.
// when taking the screenshot failed for some reason). So there may be a state
// where there are a lot of entries in the back history, but none of them has
// any screenshot. In such cases, keep the screenshots for |kMaxScreenshots|
// entries in the forward history list.
int back = current - 1;
int forward = current + 1;
while (available_slots > 0 && (back >= 0 || forward < num_entries)) {
if (back >= 0) {
NavigationEntryImpl* entry = NavigationEntryImpl::FromNavigationEntry(
owner_->GetEntryAtIndex(back));
if (entry->screenshot().get())
--available_slots;
--back;
}
if (available_slots > 0 && forward < num_entries) {
NavigationEntryImpl* entry = NavigationEntryImpl::FromNavigationEntry(
owner_->GetEntryAtIndex(forward));
if (entry->screenshot().get())
--available_slots;
++forward;
}
}
// Purge any screenshot at |back| or lower indices, and |forward| or higher
// indices.
while (screenshot_count > kMaxScreenshots && back >= 0) {
NavigationEntryImpl* entry = NavigationEntryImpl::FromNavigationEntry(
owner_->GetEntryAtIndex(back));
if (ClearScreenshot(entry))
--screenshot_count;
--back;
}
while (screenshot_count > kMaxScreenshots && forward < num_entries) {
NavigationEntryImpl* entry = NavigationEntryImpl::FromNavigationEntry(
owner_->GetEntryAtIndex(forward));
if (ClearScreenshot(entry))
--screenshot_count;
++forward;
}
CHECK_GE(screenshot_count, 0);
CHECK_LE(screenshot_count, kMaxScreenshots);
}
} // namespace content
|