OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/web_contents/web_contents_screenshot_manager.h" | 5 #include "content/browser/web_contents/web_contents_screenshot_manager.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/threading/worker_pool.h" | 8 #include "base/threading/worker_pool.h" |
9 #include "content/browser/renderer_host/render_view_host_impl.h" | 9 #include "content/browser/renderer_host/render_view_host_impl.h" |
10 #include "content/browser/web_contents/navigation_controller_impl.h" | 10 #include "content/browser/web_contents/navigation_controller_impl.h" |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 unique_id, | 161 unique_id, |
162 screenshot)); | 162 screenshot)); |
163 } | 163 } |
164 | 164 |
165 int WebContentsScreenshotManager::GetScreenshotCount() const { | 165 int WebContentsScreenshotManager::GetScreenshotCount() const { |
166 int screenshot_count = 0; | 166 int screenshot_count = 0; |
167 int entry_count = owner_->GetEntryCount(); | 167 int entry_count = owner_->GetEntryCount(); |
168 for (int i = 0; i < entry_count; ++i) { | 168 for (int i = 0; i < entry_count; ++i) { |
169 NavigationEntryImpl* entry = | 169 NavigationEntryImpl* entry = |
170 NavigationEntryImpl::FromNavigationEntry(owner_->GetEntryAtIndex(i)); | 170 NavigationEntryImpl::FromNavigationEntry(owner_->GetEntryAtIndex(i)); |
171 if (entry->screenshot()) | 171 if (entry->screenshot().get()) |
172 screenshot_count++; | 172 screenshot_count++; |
173 } | 173 } |
174 return screenshot_count; | 174 return screenshot_count; |
175 } | 175 } |
176 | 176 |
177 void WebContentsScreenshotManager::OnScreenshotEncodeComplete( | 177 void WebContentsScreenshotManager::OnScreenshotEncodeComplete( |
178 int unique_id, | 178 int unique_id, |
179 scoped_refptr<ScreenshotData> screenshot) { | 179 scoped_refptr<ScreenshotData> screenshot) { |
180 NavigationEntryImpl* entry = NULL; | 180 NavigationEntryImpl* entry = NULL; |
181 int entry_count = owner_->GetEntryCount(); | 181 int entry_count = owner_->GetEntryCount(); |
182 for (int i = 0; i < entry_count; ++i) { | 182 for (int i = 0; i < entry_count; ++i) { |
183 NavigationEntry* iter = owner_->GetEntryAtIndex(i); | 183 NavigationEntry* iter = owner_->GetEntryAtIndex(i); |
184 if (iter->GetUniqueID() == unique_id) { | 184 if (iter->GetUniqueID() == unique_id) { |
185 entry = NavigationEntryImpl::FromNavigationEntry(iter); | 185 entry = NavigationEntryImpl::FromNavigationEntry(iter); |
186 break; | 186 break; |
187 } | 187 } |
188 } | 188 } |
189 if (!entry) | 189 if (!entry) |
190 return; | 190 return; |
191 entry->SetScreenshotPNGData(screenshot->data()); | 191 entry->SetScreenshotPNGData(screenshot->data()); |
192 OnScreenshotSet(entry); | 192 OnScreenshotSet(entry); |
193 } | 193 } |
194 | 194 |
195 void WebContentsScreenshotManager::OnScreenshotSet(NavigationEntryImpl* entry) { | 195 void WebContentsScreenshotManager::OnScreenshotSet(NavigationEntryImpl* entry) { |
196 PurgeScreenshotsIfNecessary(); | 196 PurgeScreenshotsIfNecessary(); |
197 } | 197 } |
198 | 198 |
199 bool WebContentsScreenshotManager::ClearScreenshot(NavigationEntryImpl* entry) { | 199 bool WebContentsScreenshotManager::ClearScreenshot(NavigationEntryImpl* entry) { |
200 if (!entry->screenshot()) | 200 if (!entry->screenshot().get()) |
201 return false; | 201 return false; |
202 | 202 |
203 entry->SetScreenshotPNGData(NULL); | 203 entry->SetScreenshotPNGData(NULL); |
204 return true; | 204 return true; |
205 } | 205 } |
206 | 206 |
207 void WebContentsScreenshotManager::PurgeScreenshotsIfNecessary() { | 207 void WebContentsScreenshotManager::PurgeScreenshotsIfNecessary() { |
208 // Allow only a certain number of entries to keep screenshots. | 208 // Allow only a certain number of entries to keep screenshots. |
209 const int kMaxScreenshots = 10; | 209 const int kMaxScreenshots = 10; |
210 int screenshot_count = GetScreenshotCount(); | 210 int screenshot_count = GetScreenshotCount(); |
211 if (screenshot_count < kMaxScreenshots) | 211 if (screenshot_count < kMaxScreenshots) |
212 return; | 212 return; |
213 | 213 |
214 const int current = owner_->GetCurrentEntryIndex(); | 214 const int current = owner_->GetCurrentEntryIndex(); |
215 const int num_entries = owner_->GetEntryCount(); | 215 const int num_entries = owner_->GetEntryCount(); |
216 int available_slots = kMaxScreenshots; | 216 int available_slots = kMaxScreenshots; |
217 if (NavigationEntryImpl::FromNavigationEntry( | 217 if (NavigationEntryImpl::FromNavigationEntry(owner_->GetEntryAtIndex(current)) |
218 owner_->GetEntryAtIndex(current))->screenshot()) { | 218 ->screenshot().get()) { |
219 --available_slots; | 219 --available_slots; |
220 } | 220 } |
221 | 221 |
222 // Keep screenshots closer to the current navigation entry, and purge the ones | 222 // Keep screenshots closer to the current navigation entry, and purge the ones |
223 // that are farther away from it. So in each step, look at the entries at | 223 // that are farther away from it. So in each step, look at the entries at |
224 // each offset on both the back and forward history, and start counting them | 224 // each offset on both the back and forward history, and start counting them |
225 // to make sure that the correct number of screenshots are kept in memory. | 225 // to make sure that the correct number of screenshots are kept in memory. |
226 // Note that it is possible for some entries to be missing screenshots (e.g. | 226 // Note that it is possible for some entries to be missing screenshots (e.g. |
227 // when taking the screenshot failed for some reason). So there may be a state | 227 // when taking the screenshot failed for some reason). So there may be a state |
228 // where there are a lot of entries in the back history, but none of them has | 228 // where there are a lot of entries in the back history, but none of them has |
229 // any screenshot. In such cases, keep the screenshots for |kMaxScreenshots| | 229 // any screenshot. In such cases, keep the screenshots for |kMaxScreenshots| |
230 // entries in the forward history list. | 230 // entries in the forward history list. |
231 int back = current - 1; | 231 int back = current - 1; |
232 int forward = current + 1; | 232 int forward = current + 1; |
233 while (available_slots > 0 && (back >= 0 || forward < num_entries)) { | 233 while (available_slots > 0 && (back >= 0 || forward < num_entries)) { |
234 if (back >= 0) { | 234 if (back >= 0) { |
235 NavigationEntryImpl* entry = NavigationEntryImpl::FromNavigationEntry( | 235 NavigationEntryImpl* entry = NavigationEntryImpl::FromNavigationEntry( |
236 owner_->GetEntryAtIndex(back)); | 236 owner_->GetEntryAtIndex(back)); |
237 if (entry->screenshot()) | 237 if (entry->screenshot().get()) |
238 --available_slots; | 238 --available_slots; |
239 --back; | 239 --back; |
240 } | 240 } |
241 | 241 |
242 if (available_slots > 0 && forward < num_entries) { | 242 if (available_slots > 0 && forward < num_entries) { |
243 NavigationEntryImpl* entry = NavigationEntryImpl::FromNavigationEntry( | 243 NavigationEntryImpl* entry = NavigationEntryImpl::FromNavigationEntry( |
244 owner_->GetEntryAtIndex(forward)); | 244 owner_->GetEntryAtIndex(forward)); |
245 if (entry->screenshot()) | 245 if (entry->screenshot().get()) |
246 --available_slots; | 246 --available_slots; |
247 ++forward; | 247 ++forward; |
248 } | 248 } |
249 } | 249 } |
250 | 250 |
251 // Purge any screenshot at |back| or lower indices, and |forward| or higher | 251 // Purge any screenshot at |back| or lower indices, and |forward| or higher |
252 // indices. | 252 // indices. |
253 while (screenshot_count > kMaxScreenshots && back >= 0) { | 253 while (screenshot_count > kMaxScreenshots && back >= 0) { |
254 NavigationEntryImpl* entry = NavigationEntryImpl::FromNavigationEntry( | 254 NavigationEntryImpl* entry = NavigationEntryImpl::FromNavigationEntry( |
255 owner_->GetEntryAtIndex(back)); | 255 owner_->GetEntryAtIndex(back)); |
256 if (ClearScreenshot(entry)) | 256 if (ClearScreenshot(entry)) |
257 --screenshot_count; | 257 --screenshot_count; |
258 --back; | 258 --back; |
259 } | 259 } |
260 | 260 |
261 while (screenshot_count > kMaxScreenshots && forward < num_entries) { | 261 while (screenshot_count > kMaxScreenshots && forward < num_entries) { |
262 NavigationEntryImpl* entry = NavigationEntryImpl::FromNavigationEntry( | 262 NavigationEntryImpl* entry = NavigationEntryImpl::FromNavigationEntry( |
263 owner_->GetEntryAtIndex(forward)); | 263 owner_->GetEntryAtIndex(forward)); |
264 if (ClearScreenshot(entry)) | 264 if (ClearScreenshot(entry)) |
265 --screenshot_count; | 265 --screenshot_count; |
266 ++forward; | 266 ++forward; |
267 } | 267 } |
268 CHECK_GE(screenshot_count, 0); | 268 CHECK_GE(screenshot_count, 0); |
269 CHECK_LE(screenshot_count, kMaxScreenshots); | 269 CHECK_LE(screenshot_count, kMaxScreenshots); |
270 } | 270 } |
271 | 271 |
272 } // namespace content | 272 } // namespace content |
OLD | NEW |