| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/thumbnails/thumbnail_tab_helper.h" | 5 #include "chrome/browser/thumbnails/thumbnail_tab_helper.h" |
| 6 | 6 |
| 7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
| 8 #include "chrome/browser/browser_process.h" | 8 #include "chrome/browser/browser_process.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/thumbnails/thumbnail_service.h" | 10 #include "chrome/browser/thumbnails/thumbnail_service.h" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 // implemented in ShouldUpdateThumbnail(). | 46 // implemented in ShouldUpdateThumbnail(). |
| 47 | 47 |
| 48 using content::RenderViewHost; | 48 using content::RenderViewHost; |
| 49 using content::RenderWidgetHost; | 49 using content::RenderWidgetHost; |
| 50 using content::WebContents; | 50 using content::WebContents; |
| 51 | 51 |
| 52 using thumbnails::ClipResult; | 52 using thumbnails::ClipResult; |
| 53 using thumbnails::ThumbnailingContext; | 53 using thumbnails::ThumbnailingContext; |
| 54 using thumbnails::ThumbnailingAlgorithm; | 54 using thumbnails::ThumbnailingAlgorithm; |
| 55 | 55 |
| 56 namespace { | |
| 57 | |
| 58 // Feed the constructed thumbnail to the thumbnail service. | |
| 59 void UpdateThumbnail(const ThumbnailingContext& context, | |
| 60 const SkBitmap& thumbnail) { | |
| 61 gfx::Image image = gfx::Image::CreateFrom1xBitmap(thumbnail); | |
| 62 context.service->SetPageThumbnail(context, image); | |
| 63 DVLOG(1) << "Thumbnail taken for " << context.url << ": " | |
| 64 << context.score.ToString(); | |
| 65 } | |
| 66 | |
| 67 void ProcessCapturedBitmap(scoped_refptr<ThumbnailingContext> context, | |
| 68 scoped_refptr<ThumbnailingAlgorithm> algorithm, | |
| 69 const SkBitmap& bitmap, | |
| 70 content::ReadbackResponse response) { | |
| 71 if (response != content::READBACK_SUCCESS) | |
| 72 return; | |
| 73 | |
| 74 // On success, we must be on the UI thread (on failure because of shutdown we | |
| 75 // are not on the UI thread). | |
| 76 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 77 | |
| 78 algorithm->ProcessBitmap(context, base::Bind(&UpdateThumbnail), bitmap); | |
| 79 } | |
| 80 | |
| 81 void AsyncProcessThumbnail(content::WebContents* web_contents, | |
| 82 scoped_refptr<ThumbnailingContext> context, | |
| 83 scoped_refptr<ThumbnailingAlgorithm> algorithm) { | |
| 84 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 85 RenderWidgetHost* render_widget_host = | |
| 86 web_contents->GetRenderViewHost()->GetWidget(); | |
| 87 content::RenderWidgetHostView* view = render_widget_host->GetView(); | |
| 88 if (!view) | |
| 89 return; | |
| 90 if (!view->IsSurfaceAvailableForCopy()) | |
| 91 return; | |
| 92 | |
| 93 gfx::Rect copy_rect = gfx::Rect(view->GetViewBounds().size()); | |
| 94 // Clip the pixels that will commonly hold a scrollbar, which looks bad in | |
| 95 // thumbnails. | |
| 96 int scrollbar_size = gfx::scrollbar_size(); | |
| 97 gfx::Size copy_size; | |
| 98 copy_rect.Inset(0, 0, scrollbar_size, scrollbar_size); | |
| 99 | |
| 100 if (copy_rect.IsEmpty()) | |
| 101 return; | |
| 102 | |
| 103 ui::ScaleFactor scale_factor = | |
| 104 ui::GetSupportedScaleFactor( | |
| 105 ui::GetScaleFactorForNativeView(view->GetNativeView())); | |
| 106 context->clip_result = algorithm->GetCanvasCopyInfo( | |
| 107 copy_rect.size(), | |
| 108 scale_factor, | |
| 109 ©_rect, | |
| 110 &context->requested_copy_size); | |
| 111 render_widget_host->CopyFromBackingStore( | |
| 112 copy_rect, | |
| 113 context->requested_copy_size, | |
| 114 base::Bind(&ProcessCapturedBitmap, context, algorithm), | |
| 115 kN32_SkColorType); | |
| 116 } | |
| 117 | |
| 118 } // namespace | |
| 119 | |
| 120 ThumbnailTabHelper::ThumbnailTabHelper(content::WebContents* contents) | 56 ThumbnailTabHelper::ThumbnailTabHelper(content::WebContents* contents) |
| 121 : content::WebContentsObserver(contents), | 57 : content::WebContentsObserver(contents), |
| 122 load_interrupted_(false) { | 58 load_interrupted_(false), |
| 59 weak_factory_(this) { |
| 123 // Even though we deal in RenderWidgetHosts, we only care about its | 60 // Even though we deal in RenderWidgetHosts, we only care about its |
| 124 // subclass, RenderViewHost when it is in a tab. We don't make thumbnails | 61 // subclass, RenderViewHost when it is in a tab. We don't make thumbnails |
| 125 // for RenderViewHosts that aren't in tabs, or RenderWidgetHosts that | 62 // for RenderViewHosts that aren't in tabs, or RenderWidgetHosts that |
| 126 // aren't views like select popups. | 63 // aren't views like select popups. |
| 127 registrar_.Add(this, | 64 registrar_.Add(this, |
| 128 content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED, | 65 content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED, |
| 129 content::Source<WebContents>(contents)); | 66 content::Source<WebContents>(contents)); |
| 130 } | 67 } |
| 131 | 68 |
| 132 ThumbnailTabHelper::~ThumbnailTabHelper() { | 69 ThumbnailTabHelper::~ThumbnailTabHelper() { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 void ThumbnailTabHelper::DidStartLoading() { | 102 void ThumbnailTabHelper::DidStartLoading() { |
| 166 load_interrupted_ = false; | 103 load_interrupted_ = false; |
| 167 } | 104 } |
| 168 | 105 |
| 169 void ThumbnailTabHelper::NavigationStopped() { | 106 void ThumbnailTabHelper::NavigationStopped() { |
| 170 // This function gets called when the page loading is interrupted by the | 107 // This function gets called when the page loading is interrupted by the |
| 171 // stop button. | 108 // stop button. |
| 172 load_interrupted_ = true; | 109 load_interrupted_ = true; |
| 173 } | 110 } |
| 174 | 111 |
| 175 void ThumbnailTabHelper::UpdateThumbnailIfNecessary( | 112 void ThumbnailTabHelper::UpdateThumbnailIfNecessary() { |
| 176 WebContents* web_contents) { | 113 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 114 // Ignore thumbnail update requests if one is already in progress. This can |
| 115 // happen at the end of thumbnail generation when |
| 116 // CleanUpFromThumbnailGeneration() calls DecrementCapturerCount(), triggering |
| 117 // a call to content::WebContentsImpl::WasHidden() which eventually calls |
| 118 // ThumbnailTabHelper::UpdateThumbnailIfNecessary(). |
| 119 if (thumbnailing_context_) { |
| 120 return; |
| 121 } |
| 122 |
| 177 // Destroying a WebContents may trigger it to be hidden, prompting a snapshot | 123 // Destroying a WebContents may trigger it to be hidden, prompting a snapshot |
| 178 // which would be unwise to attempt <http://crbug.com/130097>. If the | 124 // which would be unwise to attempt <http://crbug.com/130097>. If the |
| 179 // WebContents is in the middle of destruction, do not risk it. | 125 // WebContents is in the middle of destruction, do not risk it. |
| 180 if (!web_contents || web_contents->IsBeingDestroyed()) | 126 if (!web_contents() || web_contents()->IsBeingDestroyed()) |
| 181 return; | 127 return; |
| 182 // Skip if a pending entry exists. WidgetHidden can be called while navigating | 128 // Skip if a pending entry exists. WidgetHidden can be called while navigating |
| 183 // pages and this is not a time when thumbnails should be generated. | 129 // pages and this is not a time when thumbnails should be generated. |
| 184 if (web_contents->GetController().GetPendingEntry()) | 130 if (web_contents()->GetController().GetPendingEntry()) |
| 185 return; | 131 return; |
| 186 const GURL& url = web_contents->GetURL(); | 132 const GURL& url = web_contents()->GetURL(); |
| 187 Profile* profile = | 133 Profile* profile = |
| 188 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 134 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
| 189 | 135 |
| 190 scoped_refptr<thumbnails::ThumbnailService> thumbnail_service = | 136 scoped_refptr<thumbnails::ThumbnailService> thumbnail_service = |
| 191 ThumbnailServiceFactory::GetForProfile(profile); | 137 ThumbnailServiceFactory::GetForProfile(profile); |
| 192 | 138 |
| 193 // Skip if we don't need to update the thumbnail. | 139 // Skip if we don't need to update the thumbnail. |
| 194 if (thumbnail_service.get() == NULL || | 140 if (thumbnail_service.get() == NULL || |
| 195 !thumbnail_service->ShouldAcquirePageThumbnail(url)) { | 141 !thumbnail_service->ShouldAcquirePageThumbnail(url)) { |
| 196 return; | 142 return; |
| 197 } | 143 } |
| 198 | 144 |
| 145 // Prevent the web contents from disappearing before the async thumbnail |
| 146 // generation code executes. See https://crbug.com/530707 . |
| 147 web_contents()->IncrementCapturerCount(gfx::Size()); |
| 148 |
| 149 AsyncProcessThumbnail(thumbnail_service); |
| 150 } |
| 151 |
| 152 void ThumbnailTabHelper::AsyncProcessThumbnail( |
| 153 scoped_refptr<thumbnails::ThumbnailService> thumbnail_service) { |
| 154 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 155 RenderWidgetHost* render_widget_host = |
| 156 web_contents()->GetRenderViewHost()->GetWidget(); |
| 157 content::RenderWidgetHostView* view = render_widget_host->GetView(); |
| 158 if (!view || !view->IsSurfaceAvailableForCopy()) { |
| 159 return; |
| 160 } |
| 161 |
| 162 gfx::Rect copy_rect = gfx::Rect(view->GetViewBounds().size()); |
| 163 // Clip the pixels that will commonly hold a scrollbar, which looks bad in |
| 164 // thumbnails. |
| 165 int scrollbar_size = gfx::scrollbar_size(); |
| 166 gfx::Size copy_size; |
| 167 copy_rect.Inset(0, 0, scrollbar_size, scrollbar_size); |
| 168 |
| 169 if (copy_rect.IsEmpty()) { |
| 170 return; |
| 171 } |
| 172 |
| 199 scoped_refptr<thumbnails::ThumbnailingAlgorithm> algorithm( | 173 scoped_refptr<thumbnails::ThumbnailingAlgorithm> algorithm( |
| 200 thumbnail_service->GetThumbnailingAlgorithm()); | 174 thumbnail_service->GetThumbnailingAlgorithm()); |
| 201 | 175 |
| 202 scoped_refptr<ThumbnailingContext> context(new ThumbnailingContext( | 176 thumbnailing_context_ = new ThumbnailingContext(web_contents(), |
| 203 web_contents, thumbnail_service.get(), load_interrupted_)); | 177 thumbnail_service.get(), |
| 204 AsyncProcessThumbnail(web_contents, context, algorithm); | 178 load_interrupted_); |
| 179 |
| 180 ui::ScaleFactor scale_factor = |
| 181 ui::GetSupportedScaleFactor( |
| 182 ui::GetScaleFactorForNativeView(view->GetNativeView())); |
| 183 thumbnailing_context_->clip_result = algorithm->GetCanvasCopyInfo( |
| 184 copy_rect.size(), |
| 185 scale_factor, |
| 186 ©_rect, |
| 187 &thumbnailing_context_->requested_copy_size); |
| 188 render_widget_host->CopyFromBackingStore( |
| 189 copy_rect, |
| 190 thumbnailing_context_->requested_copy_size, |
| 191 base::Bind(&ThumbnailTabHelper::ProcessCapturedBitmap, |
| 192 weak_factory_.GetWeakPtr(), |
| 193 algorithm), |
| 194 kN32_SkColorType); |
| 195 } |
| 196 |
| 197 void ThumbnailTabHelper::ProcessCapturedBitmap( |
| 198 scoped_refptr<thumbnails::ThumbnailingAlgorithm> algorithm, |
| 199 const SkBitmap& bitmap, |
| 200 content::ReadbackResponse response) { |
| 201 if (response == content::READBACK_SUCCESS) { |
| 202 // On success, we must be on the UI thread. |
| 203 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 204 algorithm->ProcessBitmap(thumbnailing_context_, |
| 205 base::Bind(&ThumbnailTabHelper::UpdateThumbnail, |
| 206 weak_factory_.GetWeakPtr()), |
| 207 bitmap); |
| 208 } else { |
| 209 // On failure because of shutdown we are not on the UI thread, so ensure |
| 210 // that cleanup happens on that thread. |
| 211 content::BrowserThread::PostTask( |
| 212 content::BrowserThread::UI, |
| 213 FROM_HERE, |
| 214 base::Bind(&ThumbnailTabHelper::CleanUpFromThumbnailGeneration, |
| 215 weak_factory_.GetWeakPtr())); |
| 216 } |
| 217 } |
| 218 |
| 219 void ThumbnailTabHelper::CleanUpFromThumbnailGeneration() { |
| 220 if (web_contents()) { |
| 221 // Balance the call to IncrementCapturerCount() made in |
| 222 // UpdateThumbnailIfNecessary(). |
| 223 web_contents()->DecrementCapturerCount(); |
| 224 } |
| 225 |
| 226 // Make a note that thumbnail generation is complete. |
| 227 thumbnailing_context_ = nullptr; |
| 228 } |
| 229 |
| 230 void ThumbnailTabHelper::UpdateThumbnail( |
| 231 const thumbnails::ThumbnailingContext& context, |
| 232 const SkBitmap& thumbnail) { |
| 233 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 234 // Feed the constructed thumbnail to the thumbnail service. |
| 235 gfx::Image image = gfx::Image::CreateFrom1xBitmap(thumbnail); |
| 236 context.service->SetPageThumbnail(context, image); |
| 237 DVLOG(1) << "Thumbnail taken for " << context.url << ": " |
| 238 << context.score.ToString(); |
| 239 |
| 240 CleanUpFromThumbnailGeneration(); |
| 205 } | 241 } |
| 206 | 242 |
| 207 void ThumbnailTabHelper::RenderViewHostCreated( | 243 void ThumbnailTabHelper::RenderViewHostCreated( |
| 208 content::RenderViewHost* renderer) { | 244 content::RenderViewHost* renderer) { |
| 209 // NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED is really a new | 245 // NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED is really a new |
| 210 // RenderView, not RenderViewHost, and there is no good way to get | 246 // RenderView, not RenderViewHost, and there is no good way to get |
| 211 // notifications of RenderViewHosts. So just be tolerant of re-registrations. | 247 // notifications of RenderViewHosts. So just be tolerant of re-registrations. |
| 212 bool registered = registrar_.IsRegistered( | 248 bool registered = registrar_.IsRegistered( |
| 213 this, content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, | 249 this, content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, |
| 214 content::Source<RenderWidgetHost>(renderer->GetWidget())); | 250 content::Source<RenderWidgetHost>(renderer->GetWidget())); |
| 215 if (!registered) { | 251 if (!registered) { |
| 216 registrar_.Add(this, content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, | 252 registrar_.Add(this, content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, |
| 217 content::Source<RenderWidgetHost>(renderer->GetWidget())); | 253 content::Source<RenderWidgetHost>(renderer->GetWidget())); |
| 218 } | 254 } |
| 219 } | 255 } |
| 220 | 256 |
| 221 void ThumbnailTabHelper::WidgetHidden(RenderWidgetHost* widget) { | 257 void ThumbnailTabHelper::WidgetHidden(RenderWidgetHost* widget) { |
| 222 UpdateThumbnailIfNecessary(web_contents()); | 258 UpdateThumbnailIfNecessary(); |
| 223 } | 259 } |
| OLD | NEW |