OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/frame_host/web_contents_screenshot_manager.h" | 5 #include "content/browser/frame_host/navigation_entry_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/frame_host/navigation_controller_impl.h" | 9 #include "content/browser/frame_host/navigation_controller_impl.h" |
10 #include "content/browser/frame_host/navigation_entry_impl.h" | 10 #include "content/browser/frame_host/navigation_entry_impl.h" |
11 #include "content/browser/renderer_host/render_view_host_impl.h" | 11 #include "content/browser/renderer_host/render_view_host_impl.h" |
12 #include "content/public/browser/render_widget_host.h" | 12 #include "content/public/browser/render_widget_host.h" |
13 #include "content/public/browser/render_widget_host_view.h" | 13 #include "content/public/browser/render_widget_host_view.h" |
14 #include "content/public/common/content_switches.h" | 14 #include "content/public/common/content_switches.h" |
15 #include "ui/gfx/codec/png_codec.h" | 15 #include "ui/gfx/codec/png_codec.h" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 std::vector<unsigned char> data; | 51 std::vector<unsigned char> data; |
52 if (gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, true, &data)) | 52 if (gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, true, &data)) |
53 data_ = new base::RefCountedBytes(data); | 53 data_ = new base::RefCountedBytes(data); |
54 } | 54 } |
55 | 55 |
56 scoped_refptr<base::RefCountedBytes> data_; | 56 scoped_refptr<base::RefCountedBytes> data_; |
57 | 57 |
58 DISALLOW_COPY_AND_ASSIGN(ScreenshotData); | 58 DISALLOW_COPY_AND_ASSIGN(ScreenshotData); |
59 }; | 59 }; |
60 | 60 |
61 WebContentsScreenshotManager::WebContentsScreenshotManager( | 61 NavigationEntryScreenshotManager::NavigationEntryScreenshotManager( |
62 NavigationControllerImpl* owner) | 62 NavigationControllerImpl* owner) |
63 : owner_(owner), | 63 : owner_(owner), |
64 screenshot_factory_(this), | 64 screenshot_factory_(this), |
65 min_screenshot_interval_ms_(kMinScreenshotIntervalMS) { | 65 min_screenshot_interval_ms_(kMinScreenshotIntervalMS) { |
66 } | 66 } |
67 | 67 |
68 WebContentsScreenshotManager::~WebContentsScreenshotManager() { | 68 NavigationEntryScreenshotManager::~NavigationEntryScreenshotManager() { |
69 } | 69 } |
70 | 70 |
71 void WebContentsScreenshotManager::TakeScreenshot() { | 71 void NavigationEntryScreenshotManager::TakeScreenshot() { |
72 static bool overscroll_enabled = CommandLine::ForCurrentProcess()-> | 72 static bool overscroll_enabled = CommandLine::ForCurrentProcess()-> |
73 GetSwitchValueASCII(switches::kOverscrollHistoryNavigation) != "0"; | 73 GetSwitchValueASCII(switches::kOverscrollHistoryNavigation) != "0"; |
74 if (!overscroll_enabled) | 74 if (!overscroll_enabled) |
75 return; | 75 return; |
76 | 76 |
77 NavigationEntryImpl* entry = | 77 NavigationEntryImpl* entry = |
78 NavigationEntryImpl::FromNavigationEntry(owner_->GetLastCommittedEntry()); | 78 NavigationEntryImpl::FromNavigationEntry(owner_->GetLastCommittedEntry()); |
79 if (!entry) | 79 if (!entry) |
80 return; | 80 return; |
81 | 81 |
(...skipping 14 matching lines...) Expand all Loading... |
96 return; | 96 return; |
97 } | 97 } |
98 | 98 |
99 last_screenshot_time_ = now; | 99 last_screenshot_time_ = now; |
100 | 100 |
101 TakeScreenshotImpl(render_view_host, entry); | 101 TakeScreenshotImpl(render_view_host, entry); |
102 } | 102 } |
103 | 103 |
104 // Implemented here and not in NavigationEntry because this manager keeps track | 104 // Implemented here and not in NavigationEntry because this manager keeps track |
105 // of the total number of screen shots across all entries. | 105 // of the total number of screen shots across all entries. |
106 void WebContentsScreenshotManager::ClearAllScreenshots() { | 106 void NavigationEntryScreenshotManager::ClearAllScreenshots() { |
107 int count = owner_->GetEntryCount(); | 107 int count = owner_->GetEntryCount(); |
108 for (int i = 0; i < count; ++i) { | 108 for (int i = 0; i < count; ++i) { |
109 ClearScreenshot(NavigationEntryImpl::FromNavigationEntry( | 109 ClearScreenshot(NavigationEntryImpl::FromNavigationEntry( |
110 owner_->GetEntryAtIndex(i))); | 110 owner_->GetEntryAtIndex(i))); |
111 } | 111 } |
112 DCHECK_EQ(GetScreenshotCount(), 0); | 112 DCHECK_EQ(GetScreenshotCount(), 0); |
113 } | 113 } |
114 | 114 |
115 void WebContentsScreenshotManager::TakeScreenshotImpl( | 115 void NavigationEntryScreenshotManager::TakeScreenshotImpl( |
116 RenderViewHost* host, | 116 RenderViewHost* host, |
117 NavigationEntryImpl* entry) { | 117 NavigationEntryImpl* entry) { |
118 DCHECK(host && host->GetView()); | 118 DCHECK(host && host->GetView()); |
119 DCHECK(entry); | 119 DCHECK(entry); |
120 host->CopyFromBackingStore(gfx::Rect(), | 120 host->CopyFromBackingStore(gfx::Rect(), |
121 host->GetView()->GetViewBounds().size(), | 121 host->GetView()->GetViewBounds().size(), |
122 base::Bind(&WebContentsScreenshotManager::OnScreenshotTaken, | 122 base::Bind(&NavigationEntryScreenshotManager::OnScreenshotTaken, |
123 screenshot_factory_.GetWeakPtr(), | 123 screenshot_factory_.GetWeakPtr(), |
124 entry->GetUniqueID())); | 124 entry->GetUniqueID())); |
125 } | 125 } |
126 | 126 |
127 void WebContentsScreenshotManager::SetMinScreenshotIntervalMS(int interval_ms) { | 127 void NavigationEntryScreenshotManager::SetMinScreenshotIntervalMS( |
| 128 int interval_ms) { |
128 DCHECK_GE(interval_ms, 0); | 129 DCHECK_GE(interval_ms, 0); |
129 min_screenshot_interval_ms_ = interval_ms; | 130 min_screenshot_interval_ms_ = interval_ms; |
130 } | 131 } |
131 | 132 |
132 void WebContentsScreenshotManager::OnScreenshotTaken(int unique_id, | 133 void NavigationEntryScreenshotManager::OnScreenshotTaken(int unique_id, |
133 bool success, | 134 bool success, |
134 const SkBitmap& bitmap) { | 135 const SkBitmap& bitmap) { |
135 NavigationEntryImpl* entry = NULL; | 136 NavigationEntryImpl* entry = NULL; |
136 int entry_count = owner_->GetEntryCount(); | 137 int entry_count = owner_->GetEntryCount(); |
137 for (int i = 0; i < entry_count; ++i) { | 138 for (int i = 0; i < entry_count; ++i) { |
138 NavigationEntry* iter = owner_->GetEntryAtIndex(i); | 139 NavigationEntry* iter = owner_->GetEntryAtIndex(i); |
139 if (iter->GetUniqueID() == unique_id) { | 140 if (iter->GetUniqueID() == unique_id) { |
140 entry = NavigationEntryImpl::FromNavigationEntry(iter); | 141 entry = NavigationEntryImpl::FromNavigationEntry(iter); |
141 break; | 142 break; |
142 } | 143 } |
143 } | 144 } |
144 | 145 |
145 if (!entry) { | 146 if (!entry) { |
146 LOG(ERROR) << "Invalid entry with unique id: " << unique_id; | 147 LOG(ERROR) << "Invalid entry with unique id: " << unique_id; |
147 return; | 148 return; |
148 } | 149 } |
149 | 150 |
150 if (!success || bitmap.empty() || bitmap.isNull()) { | 151 if (!success || bitmap.empty() || bitmap.isNull()) { |
151 if (!ClearScreenshot(entry)) | 152 if (!ClearScreenshot(entry)) |
152 OnScreenshotSet(entry); | 153 OnScreenshotSet(entry); |
153 return; | 154 return; |
154 } | 155 } |
155 | 156 |
156 scoped_refptr<ScreenshotData> screenshot = new ScreenshotData(); | 157 scoped_refptr<ScreenshotData> screenshot = new ScreenshotData(); |
157 screenshot->EncodeScreenshot( | 158 screenshot->EncodeScreenshot( |
158 bitmap, | 159 bitmap, |
159 base::Bind(&WebContentsScreenshotManager::OnScreenshotEncodeComplete, | 160 base::Bind(&NavigationEntryScreenshotManager::OnScreenshotEncodeComplete, |
160 screenshot_factory_.GetWeakPtr(), | 161 screenshot_factory_.GetWeakPtr(), |
161 unique_id, | 162 unique_id, |
162 screenshot)); | 163 screenshot)); |
163 } | 164 } |
164 | 165 |
165 int WebContentsScreenshotManager::GetScreenshotCount() const { | 166 int NavigationEntryScreenshotManager::GetScreenshotCount() const { |
166 int screenshot_count = 0; | 167 int screenshot_count = 0; |
167 int entry_count = owner_->GetEntryCount(); | 168 int entry_count = owner_->GetEntryCount(); |
168 for (int i = 0; i < entry_count; ++i) { | 169 for (int i = 0; i < entry_count; ++i) { |
169 NavigationEntryImpl* entry = | 170 NavigationEntryImpl* entry = |
170 NavigationEntryImpl::FromNavigationEntry(owner_->GetEntryAtIndex(i)); | 171 NavigationEntryImpl::FromNavigationEntry(owner_->GetEntryAtIndex(i)); |
171 if (entry->screenshot().get()) | 172 if (entry->screenshot().get()) |
172 screenshot_count++; | 173 screenshot_count++; |
173 } | 174 } |
174 return screenshot_count; | 175 return screenshot_count; |
175 } | 176 } |
176 | 177 |
177 void WebContentsScreenshotManager::OnScreenshotEncodeComplete( | 178 void NavigationEntryScreenshotManager::OnScreenshotEncodeComplete( |
178 int unique_id, | 179 int unique_id, |
179 scoped_refptr<ScreenshotData> screenshot) { | 180 scoped_refptr<ScreenshotData> screenshot) { |
180 NavigationEntryImpl* entry = NULL; | 181 NavigationEntryImpl* entry = NULL; |
181 int entry_count = owner_->GetEntryCount(); | 182 int entry_count = owner_->GetEntryCount(); |
182 for (int i = 0; i < entry_count; ++i) { | 183 for (int i = 0; i < entry_count; ++i) { |
183 NavigationEntry* iter = owner_->GetEntryAtIndex(i); | 184 NavigationEntry* iter = owner_->GetEntryAtIndex(i); |
184 if (iter->GetUniqueID() == unique_id) { | 185 if (iter->GetUniqueID() == unique_id) { |
185 entry = NavigationEntryImpl::FromNavigationEntry(iter); | 186 entry = NavigationEntryImpl::FromNavigationEntry(iter); |
186 break; | 187 break; |
187 } | 188 } |
188 } | 189 } |
189 if (!entry) | 190 if (!entry) |
190 return; | 191 return; |
191 entry->SetScreenshotPNGData(screenshot->data()); | 192 entry->SetScreenshotPNGData(screenshot->data()); |
192 OnScreenshotSet(entry); | 193 OnScreenshotSet(entry); |
193 } | 194 } |
194 | 195 |
195 void WebContentsScreenshotManager::OnScreenshotSet(NavigationEntryImpl* entry) { | 196 void NavigationEntryScreenshotManager::OnScreenshotSet( |
| 197 NavigationEntryImpl* entry) { |
196 if (entry->screenshot().get()) | 198 if (entry->screenshot().get()) |
197 PurgeScreenshotsIfNecessary(); | 199 PurgeScreenshotsIfNecessary(); |
198 } | 200 } |
199 | 201 |
200 bool WebContentsScreenshotManager::ClearScreenshot(NavigationEntryImpl* entry) { | 202 bool NavigationEntryScreenshotManager::ClearScreenshot( |
| 203 NavigationEntryImpl* entry) { |
201 if (!entry->screenshot().get()) | 204 if (!entry->screenshot().get()) |
202 return false; | 205 return false; |
203 | 206 |
204 entry->SetScreenshotPNGData(NULL); | 207 entry->SetScreenshotPNGData(NULL); |
205 return true; | 208 return true; |
206 } | 209 } |
207 | 210 |
208 void WebContentsScreenshotManager::PurgeScreenshotsIfNecessary() { | 211 void NavigationEntryScreenshotManager::PurgeScreenshotsIfNecessary() { |
209 // Allow only a certain number of entries to keep screenshots. | 212 // Allow only a certain number of entries to keep screenshots. |
210 const int kMaxScreenshots = 10; | 213 const int kMaxScreenshots = 10; |
211 int screenshot_count = GetScreenshotCount(); | 214 int screenshot_count = GetScreenshotCount(); |
212 if (screenshot_count < kMaxScreenshots) | 215 if (screenshot_count < kMaxScreenshots) |
213 return; | 216 return; |
214 | 217 |
215 const int current = owner_->GetCurrentEntryIndex(); | 218 const int current = owner_->GetCurrentEntryIndex(); |
216 const int num_entries = owner_->GetEntryCount(); | 219 const int num_entries = owner_->GetEntryCount(); |
217 int available_slots = kMaxScreenshots; | 220 int available_slots = kMaxScreenshots; |
218 if (NavigationEntryImpl::FromNavigationEntry(owner_->GetEntryAtIndex(current)) | 221 if (NavigationEntryImpl::FromNavigationEntry(owner_->GetEntryAtIndex(current)) |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 owner_->GetEntryAtIndex(forward)); | 267 owner_->GetEntryAtIndex(forward)); |
265 if (ClearScreenshot(entry)) | 268 if (ClearScreenshot(entry)) |
266 --screenshot_count; | 269 --screenshot_count; |
267 ++forward; | 270 ++forward; |
268 } | 271 } |
269 CHECK_GE(screenshot_count, 0); | 272 CHECK_GE(screenshot_count, 0); |
270 CHECK_LE(screenshot_count, kMaxScreenshots); | 273 CHECK_LE(screenshot_count, kMaxScreenshots); |
271 } | 274 } |
272 | 275 |
273 } // namespace content | 276 } // namespace content |
OLD | NEW |