Make USB permissions work in the new permission message system
[chromium-blink-merge.git] / content / browser / frame_host / navigation_entry_screenshot_manager.cc
blob8ad5080d3753677021c542f7ecb6f0da900f8814
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 = owner_->delegate()->GetRenderViewHost();
89 content::RenderWidgetHostView* view = render_view_host->GetView();
90 if (!view)
91 return;
93 // Make sure screenshots aren't taken too frequently.
94 base::Time now = base::Time::Now();
95 if (now - last_screenshot_time_ <
96 base::TimeDelta::FromMilliseconds(min_screenshot_interval_ms_)) {
97 return;
100 last_screenshot_time_ = now;
102 TakeScreenshotImpl(render_view_host, entry);
105 // Implemented here and not in NavigationEntry because this manager keeps track
106 // of the total number of screen shots across all entries.
107 void NavigationEntryScreenshotManager::ClearAllScreenshots() {
108 int count = owner_->GetEntryCount();
109 for (int i = 0; i < count; ++i) {
110 ClearScreenshot(owner_->GetEntryAtIndex(i));
112 DCHECK_EQ(GetScreenshotCount(), 0);
115 void NavigationEntryScreenshotManager::TakeScreenshotImpl(
116 RenderViewHost* host,
117 NavigationEntryImpl* entry) {
118 DCHECK(host && host->GetView());
119 DCHECK(entry);
120 host->CopyFromBackingStore(
121 gfx::Rect(),
122 host->GetView()->GetViewBounds().size(),
123 base::Bind(&NavigationEntryScreenshotManager::OnScreenshotTaken,
124 screenshot_factory_.GetWeakPtr(),
125 entry->GetUniqueID()),
126 kAlpha_8_SkColorType);
129 void NavigationEntryScreenshotManager::SetMinScreenshotIntervalMS(
130 int interval_ms) {
131 DCHECK_GE(interval_ms, 0);
132 min_screenshot_interval_ms_ = interval_ms;
135 void NavigationEntryScreenshotManager::OnScreenshotTaken(
136 int unique_id,
137 const SkBitmap& bitmap,
138 ReadbackResponse response) {
139 NavigationEntryImpl* entry = owner_->GetEntryWithUniqueID(unique_id);
140 if (!entry) {
141 LOG(ERROR) << "Invalid entry with unique id: " << unique_id;
142 return;
145 if ((response != READBACK_SUCCESS) || bitmap.empty() || bitmap.isNull()) {
146 if (!ClearScreenshot(entry))
147 OnScreenshotSet(entry);
148 return;
151 scoped_refptr<ScreenshotData> screenshot = new ScreenshotData();
152 screenshot->EncodeScreenshot(
153 bitmap,
154 base::Bind(&NavigationEntryScreenshotManager::OnScreenshotEncodeComplete,
155 screenshot_factory_.GetWeakPtr(),
156 unique_id,
157 screenshot));
160 int NavigationEntryScreenshotManager::GetScreenshotCount() const {
161 int screenshot_count = 0;
162 int entry_count = owner_->GetEntryCount();
163 for (int i = 0; i < entry_count; ++i) {
164 NavigationEntryImpl* entry = owner_->GetEntryAtIndex(i);
165 if (entry->screenshot().get())
166 screenshot_count++;
168 return screenshot_count;
171 void NavigationEntryScreenshotManager::OnScreenshotEncodeComplete(
172 int unique_id,
173 scoped_refptr<ScreenshotData> screenshot) {
174 NavigationEntryImpl* entry = owner_->GetEntryWithUniqueID(unique_id);
175 if (!entry)
176 return;
177 entry->SetScreenshotPNGData(screenshot->data());
178 OnScreenshotSet(entry);
181 void NavigationEntryScreenshotManager::OnScreenshotSet(
182 NavigationEntryImpl* entry) {
183 if (entry->screenshot().get())
184 PurgeScreenshotsIfNecessary();
187 bool NavigationEntryScreenshotManager::ClearScreenshot(
188 NavigationEntryImpl* entry) {
189 if (!entry->screenshot().get())
190 return false;
192 entry->SetScreenshotPNGData(NULL);
193 return true;
196 void NavigationEntryScreenshotManager::PurgeScreenshotsIfNecessary() {
197 // Allow only a certain number of entries to keep screenshots.
198 const int kMaxScreenshots = 10;
199 int screenshot_count = GetScreenshotCount();
200 if (screenshot_count < kMaxScreenshots)
201 return;
203 const int current = owner_->GetCurrentEntryIndex();
204 const int num_entries = owner_->GetEntryCount();
205 int available_slots = kMaxScreenshots;
206 if (owner_->GetEntryAtIndex(current)->screenshot().get()) {
207 --available_slots;
210 // Keep screenshots closer to the current navigation entry, and purge the ones
211 // that are farther away from it. So in each step, look at the entries at
212 // each offset on both the back and forward history, and start counting them
213 // to make sure that the correct number of screenshots are kept in memory.
214 // Note that it is possible for some entries to be missing screenshots (e.g.
215 // when taking the screenshot failed for some reason). So there may be a state
216 // where there are a lot of entries in the back history, but none of them has
217 // any screenshot. In such cases, keep the screenshots for |kMaxScreenshots|
218 // entries in the forward history list.
219 int back = current - 1;
220 int forward = current + 1;
221 while (available_slots > 0 && (back >= 0 || forward < num_entries)) {
222 if (back >= 0) {
223 NavigationEntryImpl* entry = owner_->GetEntryAtIndex(back);
224 if (entry->screenshot().get())
225 --available_slots;
226 --back;
229 if (available_slots > 0 && forward < num_entries) {
230 NavigationEntryImpl* entry = owner_->GetEntryAtIndex(forward);
231 if (entry->screenshot().get())
232 --available_slots;
233 ++forward;
237 // Purge any screenshot at |back| or lower indices, and |forward| or higher
238 // indices.
239 while (screenshot_count > kMaxScreenshots && back >= 0) {
240 NavigationEntryImpl* entry = owner_->GetEntryAtIndex(back);
241 if (ClearScreenshot(entry))
242 --screenshot_count;
243 --back;
246 while (screenshot_count > kMaxScreenshots && forward < num_entries) {
247 NavigationEntryImpl* entry = owner_->GetEntryAtIndex(forward);
248 if (ClearScreenshot(entry))
249 --screenshot_count;
250 ++forward;
252 CHECK_GE(screenshot_count, 0);
253 CHECK_LE(screenshot_count, kMaxScreenshots);
256 } // namespace content