| 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 "chrome/browser/browser_process.h" | 7 #include "chrome/browser/browser_process.h" |
| 8 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/thumbnails/thumbnail_service.h" | 9 #include "chrome/browser/thumbnails/thumbnail_service.h" |
| 10 #include "chrome/browser/thumbnails/thumbnail_service_factory.h" | 10 #include "chrome/browser/thumbnails/thumbnail_service_factory.h" |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 gfx::Image image = gfx::Image::CreateFrom1xBitmap(thumbnail); | 59 gfx::Image image = gfx::Image::CreateFrom1xBitmap(thumbnail); |
| 60 context.service->SetPageThumbnail(context, image); | 60 context.service->SetPageThumbnail(context, image); |
| 61 DVLOG(1) << "Thumbnail taken for " << context.url << ": " | 61 DVLOG(1) << "Thumbnail taken for " << context.url << ": " |
| 62 << context.score.ToString(); | 62 << context.score.ToString(); |
| 63 } | 63 } |
| 64 | 64 |
| 65 void ProcessCapturedBitmap(scoped_refptr<ThumbnailingContext> context, | 65 void ProcessCapturedBitmap(scoped_refptr<ThumbnailingContext> context, |
| 66 scoped_refptr<ThumbnailingAlgorithm> algorithm, | 66 scoped_refptr<ThumbnailingAlgorithm> algorithm, |
| 67 const SkBitmap& bitmap, | 67 const SkBitmap& bitmap, |
| 68 content::ReadbackResponse response) { | 68 content::ReadbackResponse response) { |
| 69 // Was the web contents destroyed before the thumbnail could be generated? |
| 70 if (!context->web_contents()) |
| 71 return; |
| 72 |
| 73 // Balance the IncrementCapturerCount() from UpdateThumbnailIfNecessary(). |
| 74 context->web_contents()->DecrementCapturerCount(); |
| 75 |
| 69 if (response != content::READBACK_SUCCESS) | 76 if (response != content::READBACK_SUCCESS) |
| 70 return; | 77 return; |
| 71 | 78 |
| 72 // On success, we must be on the UI thread (on failure because of shutdown we | 79 // On success, we must be on the UI thread (on failure because of shutdown we |
| 73 // are not on the UI thread). | 80 // are not on the UI thread). |
| 74 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 81 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 75 | 82 |
| 76 algorithm->ProcessBitmap(context, base::Bind(&UpdateThumbnail), bitmap); | 83 algorithm->ProcessBitmap(context, base::Bind(&UpdateThumbnail), bitmap); |
| 77 } | 84 } |
| 78 | 85 |
| 79 void AsyncProcessThumbnail(content::WebContents* web_contents, | 86 void AsyncProcessThumbnail(scoped_refptr<ThumbnailingContext> context, |
| 80 scoped_refptr<ThumbnailingContext> context, | |
| 81 scoped_refptr<ThumbnailingAlgorithm> algorithm) { | 87 scoped_refptr<ThumbnailingAlgorithm> algorithm) { |
| 82 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 88 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 83 RenderWidgetHost* render_widget_host = web_contents->GetRenderViewHost(); | 89 RenderWidgetHost* render_widget_host = |
| 90 context->web_contents()->GetRenderViewHost(); |
| 84 content::RenderWidgetHostView* view = render_widget_host->GetView(); | 91 content::RenderWidgetHostView* view = render_widget_host->GetView(); |
| 85 if (!view) | 92 if (!view) |
| 86 return; | 93 return; |
| 87 if (!view->IsSurfaceAvailableForCopy()) | 94 if (!view->IsSurfaceAvailableForCopy()) |
| 88 return; | 95 return; |
| 89 | 96 |
| 90 gfx::Rect copy_rect = gfx::Rect(view->GetViewBounds().size()); | 97 gfx::Rect copy_rect = gfx::Rect(view->GetViewBounds().size()); |
| 91 // Clip the pixels that will commonly hold a scrollbar, which looks bad in | 98 // Clip the pixels that will commonly hold a scrollbar, which looks bad in |
| 92 // thumbnails. | 99 // thumbnails. |
| 93 int scrollbar_size = gfx::scrollbar_size(); | 100 int scrollbar_size = gfx::scrollbar_size(); |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 | 196 |
| 190 scoped_refptr<thumbnails::ThumbnailService> thumbnail_service = | 197 scoped_refptr<thumbnails::ThumbnailService> thumbnail_service = |
| 191 ThumbnailServiceFactory::GetForProfile(profile); | 198 ThumbnailServiceFactory::GetForProfile(profile); |
| 192 | 199 |
| 193 // Skip if we don't need to update the thumbnail. | 200 // Skip if we don't need to update the thumbnail. |
| 194 if (thumbnail_service.get() == NULL || | 201 if (thumbnail_service.get() == NULL || |
| 195 !thumbnail_service->ShouldAcquirePageThumbnail(url)) { | 202 !thumbnail_service->ShouldAcquirePageThumbnail(url)) { |
| 196 return; | 203 return; |
| 197 } | 204 } |
| 198 | 205 |
| 206 web_contents->IncrementCapturerCount(gfx::Size()); |
| 207 |
| 199 scoped_refptr<thumbnails::ThumbnailingAlgorithm> algorithm( | 208 scoped_refptr<thumbnails::ThumbnailingAlgorithm> algorithm( |
| 200 thumbnail_service->GetThumbnailingAlgorithm()); | 209 thumbnail_service->GetThumbnailingAlgorithm()); |
| 201 | 210 |
| 202 scoped_refptr<ThumbnailingContext> context(new ThumbnailingContext( | 211 scoped_refptr<ThumbnailingContext> context(new ThumbnailingContext( |
| 203 web_contents, thumbnail_service.get(), load_interrupted_)); | 212 web_contents, thumbnail_service.get(), load_interrupted_)); |
| 204 AsyncProcessThumbnail(web_contents, context, algorithm); | 213 AsyncProcessThumbnail(context, algorithm); |
| 205 } | 214 } |
| 206 | 215 |
| 207 void ThumbnailTabHelper::RenderViewHostCreated( | 216 void ThumbnailTabHelper::RenderViewHostCreated( |
| 208 content::RenderViewHost* renderer) { | 217 content::RenderViewHost* renderer) { |
| 209 // NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED is really a new | 218 // NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED is really a new |
| 210 // RenderView, not RenderViewHost, and there is no good way to get | 219 // RenderView, not RenderViewHost, and there is no good way to get |
| 211 // notifications of RenderViewHosts. So just be tolerant of re-registrations. | 220 // notifications of RenderViewHosts. So just be tolerant of re-registrations. |
| 212 bool registered = registrar_.IsRegistered( | 221 bool registered = registrar_.IsRegistered( |
| 213 this, | 222 this, |
| 214 content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, | 223 content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, |
| 215 content::Source<RenderWidgetHost>(renderer)); | 224 content::Source<RenderWidgetHost>(renderer)); |
| 216 if (!registered) { | 225 if (!registered) { |
| 217 registrar_.Add( | 226 registrar_.Add( |
| 218 this, | 227 this, |
| 219 content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, | 228 content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, |
| 220 content::Source<RenderWidgetHost>(renderer)); | 229 content::Source<RenderWidgetHost>(renderer)); |
| 221 } | 230 } |
| 222 } | 231 } |
| 223 | 232 |
| 224 void ThumbnailTabHelper::WidgetHidden(RenderWidgetHost* widget) { | 233 void ThumbnailTabHelper::WidgetHidden(RenderWidgetHost* widget) { |
| 225 if (!enabled_) | 234 if (!enabled_) |
| 226 return; | 235 return; |
| 227 UpdateThumbnailIfNecessary(web_contents()); | 236 UpdateThumbnailIfNecessary(web_contents()); |
| 228 } | 237 } |
| OLD | NEW |