Use multiline attribute to check for IA2_STATE_MULTILINE.
[chromium-blink-merge.git] / content / browser / frame_host / navigation_entry_screenshot_manager.cc
blob121898b07c0e8b6d416593f4fe2e6e5c413d658e
1 // Copyright 2013 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.
5 #include "content/browser/frame_host/navigation_entry_screenshot_manager.h"
7 #include "base/command_line.h"
8 #include "base/threading/worker_pool.h"
9 #include "content/browser/frame_host/navigation_controller_impl.h"
10 #include "content/browser/frame_host/navigation_entry_impl.h"
11 #include "content/browser/renderer_host/render_view_host_impl.h"
12 #include "content/public/browser/overscroll_configuration.h"
13 #include "content/public/browser/render_widget_host.h"
14 #include "content/public/browser/render_widget_host_view.h"
15 #include "content/public/common/content_switches.h"
16 #include "ui/gfx/codec/png_codec.h"
18 namespace {
20 // Minimum delay between taking screenshots.
21 const int kMinScreenshotIntervalMS = 1000;
25 namespace content {
27 // Encodes the A8 SkBitmap to grayscale PNG in a worker thread.
28 class ScreenshotData : public base::RefCountedThreadSafe<ScreenshotData> {
29 public:
30 ScreenshotData() {
33 void EncodeScreenshot(const SkBitmap& bitmap, base::Closure callback) {
34 if (!base::WorkerPool::PostTaskAndReply(FROM_HERE,
35 base::Bind(&ScreenshotData::EncodeOnWorker,
36 this,
37 bitmap),
38 callback,
39 true)) {
40 callback.Run();
44 scoped_refptr<base::RefCountedBytes> data() const { return data_; }
46 private:
47 friend class base::RefCountedThreadSafe<ScreenshotData>;
48 virtual ~ScreenshotData() {
51 void EncodeOnWorker(const SkBitmap& bitmap) {
52 DCHECK_EQ(bitmap.colorType(), kAlpha_8_SkColorType);
53 // Encode the A8 bitmap to grayscale PNG treating alpha as color intensity.
54 std::vector<unsigned char> data;
55 if (gfx::PNGCodec::EncodeA8SkBitmap(bitmap, &data))
56 data_ = new base::RefCountedBytes(data);
59 scoped_refptr<base::RefCountedBytes> data_;
61 DISALLOW_COPY_AND_ASSIGN(ScreenshotData);
64 NavigationEntryScreenshotManager::NavigationEntryScreenshotManager(
65 NavigationControllerImpl* owner)
66 : owner_(owner),
67 min_screenshot_interval_ms_(kMinScreenshotIntervalMS),
68 screenshot_factory_(this) {
72 NavigationEntryScreenshotManager::~NavigationEntryScreenshotManager() {
75 void NavigationEntryScreenshotManager::TakeScreenshot() {
76 static bool overscroll_enabled = base::CommandLine::ForCurrentProcess()->
77 GetSwitchValueASCII(switches::kOverscrollHistoryNavigation) != "0";
78 if (!overscroll_enabled)
79 return;
81 NavigationEntryImpl* entry = owner_->GetLastCommittedEntry();
82 if (!entry)
83 return;
85 if (!owner_->delegate()->CanOverscrollContent())
86 return;
88 RenderViewHost* render_view_host =
89 owner_->delegate()->GetRenderViewHost();
90 content::RenderWidgetHostView* view = render_view_host->GetView();
91 if (!view)
92 return;
94 // Make sure screenshots aren't taken too frequently.
95 base::Time now = base::Time::Now();
96 if (now - last_screenshot_time_ <
97 base::TimeDelta::FromMilliseconds(min_screenshot_interval_ms_)) {
98 return;
101 last_screenshot_time_ = now;
103 TakeScreenshotImpl(render_view_host, entry);
106 // Implemented here and not in NavigationEntry because this manager keeps track
107 // of the total number of screen shots across all entries.
108 void NavigationEntryScreenshotManager::ClearAllScreenshots() {
109 int count = owner_->GetEntryCount();
110 for (int i = 0; i < count; ++i) {
111 ClearScreenshot(owner_->GetEntryAtIndex(i));
113 DCHECK_EQ(GetScreenshotCount(), 0);
116 void NavigationEntryScreenshotManager::TakeScreenshotImpl(
117 RenderViewHost* host,
118 NavigationEntryImpl* entry) {
119 DCHECK(host && host->GetView());
120 DCHECK(entry);
121 host->CopyFromBackingStore(
122 gfx::Rect(),
123 host->GetView()->GetViewBounds().size(),
124 base::Bind(&NavigationEntryScreenshotManager::OnScreenshotTaken,
125 screenshot_factory_.GetWeakPtr(),
126 entry->GetUniqueID()),
127 kAlpha_8_SkColorType);
130 void NavigationEntryScreenshotManager::SetMinScreenshotIntervalMS(
131 int interval_ms) {
132 DCHECK_GE(interval_ms, 0);
133 min_screenshot_interval_ms_ = interval_ms;
136 void NavigationEntryScreenshotManager::OnScreenshotTaken(
137 int unique_id,
138 const SkBitmap& bitmap,
139 ReadbackResponse response) {
140 NavigationEntryImpl* entry = NULL;
141 int entry_count = owner_->GetEntryCount();
142 for (int i = 0; i < entry_count; ++i) {
143 NavigationEntryImpl* iter = owner_->GetEntryAtIndex(i);
144 if (iter->GetUniqueID() == unique_id) {
145 entry = iter;
146 break;
150 if (!entry) {
151 LOG(ERROR) << "Invalid entry with unique id: " << unique_id;
152 return;
155 if ((response != READBACK_SUCCESS) || bitmap.empty() || bitmap.isNull()) {
156 if (!ClearScreenshot(entry))
157 OnScreenshotSet(entry);
158 return;
161 scoped_refptr<ScreenshotData> screenshot = new ScreenshotData();
162 screenshot->EncodeScreenshot(
163 bitmap,
164 base::Bind(&NavigationEntryScreenshotManager::OnScreenshotEncodeComplete,
165 screenshot_factory_.GetWeakPtr(),
166 unique_id,
167 screenshot));
170 int NavigationEntryScreenshotManager::GetScreenshotCount() const {
171 int screenshot_count = 0;
172 int entry_count = owner_->GetEntryCount();
173 for (int i = 0; i < entry_count; ++i) {
174 NavigationEntryImpl* entry = owner_->GetEntryAtIndex(i);
175 if (entry->screenshot().get())
176 screenshot_count++;
178 return screenshot_count;
181 void NavigationEntryScreenshotManager::OnScreenshotEncodeComplete(
182 int unique_id,
183 scoped_refptr<ScreenshotData> screenshot) {
184 NavigationEntryImpl* entry = NULL;
185 int entry_count = owner_->GetEntryCount();
186 for (int i = 0; i < entry_count; ++i) {
187 NavigationEntryImpl* iter = owner_->GetEntryAtIndex(i);
188 if (iter->GetUniqueID() == unique_id) {
189 entry = iter;
190 break;
193 if (!entry)
194 return;
195 entry->SetScreenshotPNGData(screenshot->data());
196 OnScreenshotSet(entry);
199 void NavigationEntryScreenshotManager::OnScreenshotSet(
200 NavigationEntryImpl* entry) {
201 if (entry->screenshot().get())
202 PurgeScreenshotsIfNecessary();
205 bool NavigationEntryScreenshotManager::ClearScreenshot(
206 NavigationEntryImpl* entry) {
207 if (!entry->screenshot().get())
208 return false;
210 entry->SetScreenshotPNGData(NULL);
211 return true;
214 void NavigationEntryScreenshotManager::PurgeScreenshotsIfNecessary() {
215 // Allow only a certain number of entries to keep screenshots.
216 const int kMaxScreenshots = 10;
217 int screenshot_count = GetScreenshotCount();
218 if (screenshot_count < kMaxScreenshots)
219 return;
221 const int current = owner_->GetCurrentEntryIndex();
222 const int num_entries = owner_->GetEntryCount();
223 int available_slots = kMaxScreenshots;
224 if (owner_->GetEntryAtIndex(current)->screenshot().get()) {
225 --available_slots;
228 // Keep screenshots closer to the current navigation entry, and purge the ones
229 // that are farther away from it. So in each step, look at the entries at
230 // each offset on both the back and forward history, and start counting them
231 // to make sure that the correct number of screenshots are kept in memory.
232 // Note that it is possible for some entries to be missing screenshots (e.g.
233 // when taking the screenshot failed for some reason). So there may be a state
234 // where there are a lot of entries in the back history, but none of them has
235 // any screenshot. In such cases, keep the screenshots for |kMaxScreenshots|
236 // entries in the forward history list.
237 int back = current - 1;
238 int forward = current + 1;
239 while (available_slots > 0 && (back >= 0 || forward < num_entries)) {
240 if (back >= 0) {
241 NavigationEntryImpl* entry = owner_->GetEntryAtIndex(back);
242 if (entry->screenshot().get())
243 --available_slots;
244 --back;
247 if (available_slots > 0 && forward < num_entries) {
248 NavigationEntryImpl* entry = owner_->GetEntryAtIndex(forward);
249 if (entry->screenshot().get())
250 --available_slots;
251 ++forward;
255 // Purge any screenshot at |back| or lower indices, and |forward| or higher
256 // indices.
257 while (screenshot_count > kMaxScreenshots && back >= 0) {
258 NavigationEntryImpl* entry = owner_->GetEntryAtIndex(back);
259 if (ClearScreenshot(entry))
260 --screenshot_count;
261 --back;
264 while (screenshot_count > kMaxScreenshots && forward < num_entries) {
265 NavigationEntryImpl* entry = owner_->GetEntryAtIndex(forward);
266 if (ClearScreenshot(entry))
267 --screenshot_count;
268 ++forward;
270 CHECK_GE(screenshot_count, 0);
271 CHECK_LE(screenshot_count, kMaxScreenshots);
274 } // namespace content