OLD | NEW |
| (Empty) |
1 // Copyright (c) 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. | |
4 | |
5 #include "content/browser/web_contents/web_contents_screenshot_manager.h" | |
6 | |
7 #include "base/command_line.h" | |
8 #include "base/threading/worker_pool.h" | |
9 #include "content/browser/renderer_host/render_view_host_impl.h" | |
10 #include "content/browser/web_contents/navigation_controller_impl.h" | |
11 #include "content/browser/web_contents/navigation_entry_impl.h" | |
12 #include "content/browser/web_contents/web_contents_impl.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" | |
17 | |
18 namespace { | |
19 | |
20 // Minimum delay between taking screenshots. | |
21 const int kMinScreenshotIntervalMS = 1000; | |
22 | |
23 } | |
24 | |
25 namespace content { | |
26 | |
27 // Encodes an SkBitmap to PNG data in a worker thread. | |
28 class ScreenshotData : public base::RefCountedThreadSafe<ScreenshotData> { | |
29 public: | |
30 ScreenshotData() { | |
31 } | |
32 | |
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(); | |
41 } | |
42 } | |
43 | |
44 scoped_refptr<base::RefCountedBytes> data() const { return data_; } | |
45 | |
46 private: | |
47 friend class base::RefCountedThreadSafe<ScreenshotData>; | |
48 virtual ~ScreenshotData() { | |
49 } | |
50 | |
51 void EncodeOnWorker(const SkBitmap& bitmap) { | |
52 std::vector<unsigned char> data; | |
53 if (gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, true, &data)) | |
54 data_ = new base::RefCountedBytes(data); | |
55 } | |
56 | |
57 scoped_refptr<base::RefCountedBytes> data_; | |
58 | |
59 DISALLOW_COPY_AND_ASSIGN(ScreenshotData); | |
60 }; | |
61 | |
62 WebContentsScreenshotManager::WebContentsScreenshotManager( | |
63 NavigationControllerImpl* owner) | |
64 : owner_(owner), | |
65 screenshot_factory_(this), | |
66 min_screenshot_interval_ms_(kMinScreenshotIntervalMS) { | |
67 } | |
68 | |
69 WebContentsScreenshotManager::~WebContentsScreenshotManager() { | |
70 } | |
71 | |
72 void WebContentsScreenshotManager::TakeScreenshot() { | |
73 static bool overscroll_enabled = CommandLine::ForCurrentProcess()-> | |
74 GetSwitchValueASCII(switches::kOverscrollHistoryNavigation) != "0"; | |
75 if (!overscroll_enabled) | |
76 return; | |
77 | |
78 NavigationEntryImpl* entry = | |
79 NavigationEntryImpl::FromNavigationEntry(owner_->GetLastCommittedEntry()); | |
80 if (!entry) | |
81 return; | |
82 | |
83 RenderViewHost* render_view_host = | |
84 owner_->delegate()->GetRenderViewHost(); | |
85 if (!static_cast<RenderViewHostImpl*> | |
86 (render_view_host)->overscroll_controller()) { | |
87 return; | |
88 } | |
89 content::RenderWidgetHostView* view = render_view_host->GetView(); | |
90 if (!view) | |
91 return; | |
92 | |
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; | |
98 } | |
99 | |
100 last_screenshot_time_ = now; | |
101 | |
102 TakeScreenshotImpl(render_view_host, entry); | |
103 } | |
104 | |
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 WebContentsScreenshotManager::ClearAllScreenshots() { | |
108 int count = owner_->GetEntryCount(); | |
109 for (int i = 0; i < count; ++i) { | |
110 ClearScreenshot(NavigationEntryImpl::FromNavigationEntry( | |
111 owner_->GetEntryAtIndex(i))); | |
112 } | |
113 DCHECK_EQ(GetScreenshotCount(), 0); | |
114 } | |
115 | |
116 void WebContentsScreenshotManager::TakeScreenshotImpl( | |
117 RenderViewHost* host, | |
118 NavigationEntryImpl* entry) { | |
119 DCHECK(host && host->GetView()); | |
120 DCHECK(entry); | |
121 host->CopyFromBackingStore(gfx::Rect(), | |
122 host->GetView()->GetViewBounds().size(), | |
123 base::Bind(&WebContentsScreenshotManager::OnScreenshotTaken, | |
124 screenshot_factory_.GetWeakPtr(), | |
125 entry->GetUniqueID())); | |
126 } | |
127 | |
128 void WebContentsScreenshotManager::SetMinScreenshotIntervalMS(int interval_ms) { | |
129 DCHECK_GE(interval_ms, 0); | |
130 min_screenshot_interval_ms_ = interval_ms; | |
131 } | |
132 | |
133 void WebContentsScreenshotManager::OnScreenshotTaken(int unique_id, | |
134 bool success, | |
135 const SkBitmap& bitmap) { | |
136 NavigationEntryImpl* entry = NULL; | |
137 int entry_count = owner_->GetEntryCount(); | |
138 for (int i = 0; i < entry_count; ++i) { | |
139 NavigationEntry* iter = owner_->GetEntryAtIndex(i); | |
140 if (iter->GetUniqueID() == unique_id) { | |
141 entry = NavigationEntryImpl::FromNavigationEntry(iter); | |
142 break; | |
143 } | |
144 } | |
145 | |
146 if (!entry) { | |
147 LOG(ERROR) << "Invalid entry with unique id: " << unique_id; | |
148 return; | |
149 } | |
150 | |
151 if (!success || bitmap.empty() || bitmap.isNull()) { | |
152 if (!ClearScreenshot(entry)) | |
153 OnScreenshotSet(entry); | |
154 return; | |
155 } | |
156 | |
157 scoped_refptr<ScreenshotData> screenshot = new ScreenshotData(); | |
158 screenshot->EncodeScreenshot( | |
159 bitmap, | |
160 base::Bind(&WebContentsScreenshotManager::OnScreenshotEncodeComplete, | |
161 screenshot_factory_.GetWeakPtr(), | |
162 unique_id, | |
163 screenshot)); | |
164 } | |
165 | |
166 int WebContentsScreenshotManager::GetScreenshotCount() const { | |
167 int screenshot_count = 0; | |
168 int entry_count = owner_->GetEntryCount(); | |
169 for (int i = 0; i < entry_count; ++i) { | |
170 NavigationEntryImpl* entry = | |
171 NavigationEntryImpl::FromNavigationEntry(owner_->GetEntryAtIndex(i)); | |
172 if (entry->screenshot().get()) | |
173 screenshot_count++; | |
174 } | |
175 return screenshot_count; | |
176 } | |
177 | |
178 void WebContentsScreenshotManager::OnScreenshotEncodeComplete( | |
179 int unique_id, | |
180 scoped_refptr<ScreenshotData> screenshot) { | |
181 NavigationEntryImpl* entry = NULL; | |
182 int entry_count = owner_->GetEntryCount(); | |
183 for (int i = 0; i < entry_count; ++i) { | |
184 NavigationEntry* iter = owner_->GetEntryAtIndex(i); | |
185 if (iter->GetUniqueID() == unique_id) { | |
186 entry = NavigationEntryImpl::FromNavigationEntry(iter); | |
187 break; | |
188 } | |
189 } | |
190 if (!entry) | |
191 return; | |
192 entry->SetScreenshotPNGData(screenshot->data()); | |
193 OnScreenshotSet(entry); | |
194 } | |
195 | |
196 void WebContentsScreenshotManager::OnScreenshotSet(NavigationEntryImpl* entry) { | |
197 if (entry->screenshot().get()) | |
198 PurgeScreenshotsIfNecessary(); | |
199 } | |
200 | |
201 bool WebContentsScreenshotManager::ClearScreenshot(NavigationEntryImpl* entry) { | |
202 if (!entry->screenshot().get()) | |
203 return false; | |
204 | |
205 entry->SetScreenshotPNGData(NULL); | |
206 return true; | |
207 } | |
208 | |
209 void WebContentsScreenshotManager::PurgeScreenshotsIfNecessary() { | |
210 // Allow only a certain number of entries to keep screenshots. | |
211 const int kMaxScreenshots = 10; | |
212 int screenshot_count = GetScreenshotCount(); | |
213 if (screenshot_count < kMaxScreenshots) | |
214 return; | |
215 | |
216 const int current = owner_->GetCurrentEntryIndex(); | |
217 const int num_entries = owner_->GetEntryCount(); | |
218 int available_slots = kMaxScreenshots; | |
219 if (NavigationEntryImpl::FromNavigationEntry(owner_->GetEntryAtIndex(current)) | |
220 ->screenshot().get()) { | |
221 --available_slots; | |
222 } | |
223 | |
224 // Keep screenshots closer to the current navigation entry, and purge the ones | |
225 // that are farther away from it. So in each step, look at the entries at | |
226 // each offset on both the back and forward history, and start counting them | |
227 // to make sure that the correct number of screenshots are kept in memory. | |
228 // Note that it is possible for some entries to be missing screenshots (e.g. | |
229 // when taking the screenshot failed for some reason). So there may be a state | |
230 // where there are a lot of entries in the back history, but none of them has | |
231 // any screenshot. In such cases, keep the screenshots for |kMaxScreenshots| | |
232 // entries in the forward history list. | |
233 int back = current - 1; | |
234 int forward = current + 1; | |
235 while (available_slots > 0 && (back >= 0 || forward < num_entries)) { | |
236 if (back >= 0) { | |
237 NavigationEntryImpl* entry = NavigationEntryImpl::FromNavigationEntry( | |
238 owner_->GetEntryAtIndex(back)); | |
239 if (entry->screenshot().get()) | |
240 --available_slots; | |
241 --back; | |
242 } | |
243 | |
244 if (available_slots > 0 && forward < num_entries) { | |
245 NavigationEntryImpl* entry = NavigationEntryImpl::FromNavigationEntry( | |
246 owner_->GetEntryAtIndex(forward)); | |
247 if (entry->screenshot().get()) | |
248 --available_slots; | |
249 ++forward; | |
250 } | |
251 } | |
252 | |
253 // Purge any screenshot at |back| or lower indices, and |forward| or higher | |
254 // indices. | |
255 while (screenshot_count > kMaxScreenshots && back >= 0) { | |
256 NavigationEntryImpl* entry = NavigationEntryImpl::FromNavigationEntry( | |
257 owner_->GetEntryAtIndex(back)); | |
258 if (ClearScreenshot(entry)) | |
259 --screenshot_count; | |
260 --back; | |
261 } | |
262 | |
263 while (screenshot_count > kMaxScreenshots && forward < num_entries) { | |
264 NavigationEntryImpl* entry = NavigationEntryImpl::FromNavigationEntry( | |
265 owner_->GetEntryAtIndex(forward)); | |
266 if (ClearScreenshot(entry)) | |
267 --screenshot_count; | |
268 ++forward; | |
269 } | |
270 CHECK_GE(screenshot_count, 0); | |
271 CHECK_LE(screenshot_count, kMaxScreenshots); | |
272 } | |
273 | |
274 } // namespace content | |
OLD | NEW |