| 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 "base/feature_list.h" | 7 #include "base/feature_list.h" |
| 8 #include "build/build_config.h" | 8 #include "base/metrics/histogram_macros.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" |
| 11 #include "chrome/browser/thumbnails/thumbnail_service_factory.h" | 11 #include "chrome/browser/thumbnails/thumbnail_service_factory.h" |
| 12 #include "chrome/browser/thumbnails/thumbnailing_algorithm.h" | 12 #include "chrome/browser/thumbnails/thumbnailing_algorithm.h" |
| 13 #include "chrome/common/chrome_features.h" | 13 #include "chrome/common/chrome_features.h" |
| 14 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 15 #include "content/public/browser/notification_details.h" | 15 #include "content/public/browser/notification_details.h" |
| 16 #include "content/public/browser/notification_source.h" | 16 #include "content/public/browser/notification_source.h" |
| 17 #include "content/public/browser/notification_types.h" | 17 #include "content/public/browser/notification_types.h" |
| 18 #include "content/public/browser/render_view_host.h" | 18 #include "content/public/browser/render_view_host.h" |
| 19 #include "content/public/browser/render_widget_host.h" | 19 #include "content/public/browser/render_widget_host.h" |
| 20 #include "content/public/browser/render_widget_host_view.h" | 20 #include "content/public/browser/render_widget_host_view.h" |
| 21 #include "ui/gfx/geometry/size_conversions.h" | |
| 22 #include "ui/gfx/scrollbar_size.h" | 21 #include "ui/gfx/scrollbar_size.h" |
| 23 | 22 |
| 24 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ThumbnailTabHelper); | 23 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ThumbnailTabHelper); |
| 25 | 24 |
| 26 class SkBitmap; | 25 class SkBitmap; |
| 27 | 26 |
| 28 // Overview | 27 // Overview |
| 29 // -------- | 28 // -------- |
| 30 // This class provides a service for updating thumbnails to be used in | 29 // This class provides a service for updating thumbnails to be used in |
| 31 // "Most visited" section of the new tab page. The service can be started | 30 // "Most visited" section of the new tab page. The service can be started |
| 32 // by UpdateThumbnailIfNecessary(). The current algorithm of the service is as | 31 // by UpdateThumbnailIfNecessary(). The current algorithm of the service is as |
| 33 // simple as follows: | 32 // simple as follows: |
| 34 // | 33 // |
| 35 // When a renderer is about to be hidden (this usually occurs when the | 34 // When a renderer is about to be hidden (this usually occurs when the |
| 36 // current tab is closed or another tab is clicked), update the | 35 // current tab is closed or another tab is clicked), update the |
| 37 // thumbnail for the tab rendered by the renderer, if needed. The | 36 // thumbnail for the tab rendered by the renderer, if needed. The |
| 38 // heuristics to judge whether or not to update the thumbnail is | 37 // heuristics to judge whether or not to update the thumbnail is |
| 39 // implemented in ShouldUpdateThumbnail(). | 38 // implemented in ShouldUpdateThumbnail(). |
| 40 // If features::kCaptureThumbnailOnLoadFinished is enabled, then a thumbnail | 39 // If features::kCaptureThumbnailOnLoadFinished is enabled, then a thumbnail |
| 41 // may also be captured when a page load finishes (subject to the same | 40 // may also be captured when a page load finishes (subject to the same |
| 42 // heuristics). | 41 // heuristics). |
| 43 | 42 |
| 44 using content::RenderViewHost; | |
| 45 using content::RenderWidgetHost; | |
| 46 using content::WebContents; | |
| 47 | |
| 48 using thumbnails::ThumbnailingContext; | 43 using thumbnails::ThumbnailingContext; |
| 49 using thumbnails::ThumbnailingAlgorithm; | 44 using thumbnails::ThumbnailingAlgorithm; |
| 50 | 45 |
| 51 ThumbnailTabHelper::ThumbnailTabHelper(content::WebContents* contents) | 46 ThumbnailTabHelper::ThumbnailTabHelper(content::WebContents* contents) |
| 52 : content::WebContentsObserver(contents), | 47 : content::WebContentsObserver(contents), |
| 53 capture_on_load_finished_(base::FeatureList::IsEnabled( | 48 capture_on_load_finished_(base::FeatureList::IsEnabled( |
| 54 features::kCaptureThumbnailOnLoadFinished)), | 49 features::kCaptureThumbnailOnLoadFinished)), |
| 55 load_interrupted_(false), | 50 load_interrupted_(false), |
| 56 weak_factory_(this) { | 51 weak_factory_(this) { |
| 57 // Even though we deal in RenderWidgetHosts, we only care about its | 52 // Even though we deal in RenderWidgetHosts, we only care about its |
| 58 // subclass, RenderViewHost when it is in a tab. We don't make thumbnails | 53 // subclass, RenderViewHost when it is in a tab. We don't make thumbnails |
| 59 // for RenderViewHosts that aren't in tabs, or RenderWidgetHosts that | 54 // for RenderViewHosts that aren't in tabs, or RenderWidgetHosts that |
| 60 // aren't views like select popups. | 55 // aren't views like select popups. |
| 61 registrar_.Add(this, | 56 registrar_.Add(this, |
| 62 content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED, | 57 content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED, |
| 63 content::Source<WebContents>(contents)); | 58 content::Source<content::WebContents>(contents)); |
| 64 } | 59 } |
| 65 | 60 |
| 66 ThumbnailTabHelper::~ThumbnailTabHelper() { | 61 ThumbnailTabHelper::~ThumbnailTabHelper() = default; |
| 67 } | |
| 68 | 62 |
| 69 void ThumbnailTabHelper::Observe(int type, | 63 void ThumbnailTabHelper::Observe(int type, |
| 70 const content::NotificationSource& source, | 64 const content::NotificationSource& source, |
| 71 const content::NotificationDetails& details) { | 65 const content::NotificationDetails& details) { |
| 72 switch (type) { | 66 switch (type) { |
| 73 case content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED: | 67 case content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED: |
| 74 RenderViewHostCreated(content::Details<RenderViewHost>(details).ptr()); | 68 RenderViewHostCreated( |
| 69 content::Details<content::RenderViewHost>(details).ptr()); |
| 75 break; | 70 break; |
| 76 | 71 |
| 77 case content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED: | 72 case content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED: |
| 78 if (!*content::Details<bool>(details).ptr()) | 73 if (!*content::Details<bool>(details).ptr()) |
| 79 WidgetHidden(content::Source<RenderWidgetHost>(source).ptr()); | 74 WidgetHidden(content::Source<content::RenderWidgetHost>(source).ptr()); |
| 80 break; | 75 break; |
| 81 | 76 |
| 82 default: | 77 default: |
| 83 NOTREACHED() << "Unexpected notification type: " << type; | 78 NOTREACHED() << "Unexpected notification type: " << type; |
| 84 } | 79 } |
| 85 } | 80 } |
| 86 | 81 |
| 87 void ThumbnailTabHelper::RenderViewDeleted(RenderViewHost* render_view_host) { | 82 void ThumbnailTabHelper::RenderViewDeleted( |
| 83 content::RenderViewHost* render_view_host) { |
| 88 bool registered = registrar_.IsRegistered( | 84 bool registered = registrar_.IsRegistered( |
| 89 this, content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, | 85 this, content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, |
| 90 content::Source<RenderWidgetHost>(render_view_host->GetWidget())); | 86 content::Source<content::RenderWidgetHost>( |
| 87 render_view_host->GetWidget())); |
| 91 if (registered) { | 88 if (registered) { |
| 92 registrar_.Remove( | 89 registrar_.Remove(this, |
| 93 this, content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, | 90 content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, |
| 94 content::Source<RenderWidgetHost>(render_view_host->GetWidget())); | 91 content::Source<content::RenderWidgetHost>( |
| 92 render_view_host->GetWidget())); |
| 95 } | 93 } |
| 96 } | 94 } |
| 97 | 95 |
| 98 void ThumbnailTabHelper::DidStartLoading() { | 96 void ThumbnailTabHelper::DidStartLoading() { |
| 99 load_interrupted_ = false; | 97 load_interrupted_ = false; |
| 100 } | 98 } |
| 101 | 99 |
| 102 void ThumbnailTabHelper::DidStopLoading() { | 100 void ThumbnailTabHelper::DidStopLoading() { |
| 103 if (capture_on_load_finished_) { | 101 if (capture_on_load_finished_) { |
| 104 UpdateThumbnailIfNecessary(); | 102 UpdateThumbnailIfNecessary(); |
| 105 } | 103 } |
| 106 } | 104 } |
| 107 | 105 |
| 108 void ThumbnailTabHelper::NavigationStopped() { | 106 void ThumbnailTabHelper::NavigationStopped() { |
| 109 // 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 |
| 110 // stop button. | 108 // stop button. |
| 111 load_interrupted_ = true; | 109 load_interrupted_ = true; |
| 112 } | 110 } |
| 113 | 111 |
| 114 void ThumbnailTabHelper::UpdateThumbnailIfNecessary() { | 112 void ThumbnailTabHelper::UpdateThumbnailIfNecessary() { |
| 115 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 113 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 116 // Ignore thumbnail update requests if one is already in progress. | 114 // Ignore thumbnail update requests if one is already in progress. |
| 117 if (thumbnailing_context_) { | 115 if (thumbnailing_context_) { |
| 118 return; | 116 return; |
| 119 } | 117 } |
| 120 | 118 |
| 121 // Destroying a WebContents may trigger it to be hidden, prompting a snapshot | 119 // Destroying a WebContents may trigger it to be hidden, prompting a snapshot |
| 122 // which would be unwise to attempt <http://crbug.com/130097>. If the | 120 // which would be unwise to attempt <http://crbug.com/130097>. If the |
| 123 // WebContents is in the middle of destruction, do not risk it. | 121 // WebContents is in the middle of destruction, do not risk it. |
| 124 if (!web_contents() || web_contents()->IsBeingDestroyed()) | 122 if (!web_contents() || web_contents()->IsBeingDestroyed()) { |
| 125 return; | 123 return; |
| 124 } |
| 126 // Skip if a pending entry exists. WidgetHidden can be called while navigating | 125 // Skip if a pending entry exists. WidgetHidden can be called while navigating |
| 127 // pages and this is not a time when thumbnails should be generated. | 126 // pages and this is not a time when thumbnails should be generated. |
| 128 if (web_contents()->GetController().GetPendingEntry()) | 127 if (web_contents()->GetController().GetPendingEntry()) { |
| 129 return; | 128 return; |
| 129 } |
| 130 const GURL& url = web_contents()->GetURL(); | 130 const GURL& url = web_contents()->GetURL(); |
| 131 Profile* profile = | 131 Profile* profile = |
| 132 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 132 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
| 133 | 133 |
| 134 scoped_refptr<thumbnails::ThumbnailService> thumbnail_service = | 134 scoped_refptr<thumbnails::ThumbnailService> thumbnail_service = |
| 135 ThumbnailServiceFactory::GetForProfile(profile); | 135 ThumbnailServiceFactory::GetForProfile(profile); |
| 136 | 136 |
| 137 // Skip if we don't need to update the thumbnail. | 137 // Skip if we don't need to update the thumbnail. |
| 138 if (thumbnail_service.get() == NULL || | 138 if (thumbnail_service.get() == NULL || |
| 139 !thumbnail_service->ShouldAcquirePageThumbnail(url)) { | 139 !thumbnail_service->ShouldAcquirePageThumbnail(url)) { |
| 140 return; | 140 return; |
| 141 } | 141 } |
| 142 | 142 |
| 143 AsyncProcessThumbnail(thumbnail_service); | 143 AsyncProcessThumbnail(thumbnail_service); |
| 144 } | 144 } |
| 145 | 145 |
| 146 void ThumbnailTabHelper::AsyncProcessThumbnail( | 146 void ThumbnailTabHelper::AsyncProcessThumbnail( |
| 147 scoped_refptr<thumbnails::ThumbnailService> thumbnail_service) { | 147 scoped_refptr<thumbnails::ThumbnailService> thumbnail_service) { |
| 148 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 148 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 149 RenderWidgetHost* render_widget_host = | 149 content::RenderWidgetHost* render_widget_host = |
| 150 web_contents()->GetRenderViewHost()->GetWidget(); | 150 web_contents()->GetRenderViewHost()->GetWidget(); |
| 151 content::RenderWidgetHostView* view = render_widget_host->GetView(); | 151 content::RenderWidgetHostView* view = render_widget_host->GetView(); |
| 152 if (!view || !view->IsSurfaceAvailableForCopy()) { | 152 if (!view || !view->IsSurfaceAvailableForCopy()) { |
| 153 return; | 153 return; |
| 154 } | 154 } |
| 155 | 155 |
| 156 // TODO(miu): This is the wrong size. It's the size of the view on-screen, and | 156 // TODO(miu): This is the wrong size. It's the size of the view on-screen, and |
| 157 // not the rendering size of the view. This will be replaced with the view's | 157 // not the rendering size of the view. This will be replaced with the view's |
| 158 // actual rendering size in a later change. http://crbug.com/73362 | 158 // actual rendering size in a later change. http://crbug.com/73362 |
| 159 gfx::Rect copy_rect = gfx::Rect(view->GetViewBounds().size()); | 159 gfx::Rect copy_rect = gfx::Rect(view->GetViewBounds().size()); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 174 load_interrupted_); | 174 load_interrupted_); |
| 175 | 175 |
| 176 ui::ScaleFactor scale_factor = | 176 ui::ScaleFactor scale_factor = |
| 177 ui::GetSupportedScaleFactor( | 177 ui::GetSupportedScaleFactor( |
| 178 ui::GetScaleFactorForNativeView(view->GetNativeView())); | 178 ui::GetScaleFactorForNativeView(view->GetNativeView())); |
| 179 thumbnailing_context_->clip_result = algorithm->GetCanvasCopyInfo( | 179 thumbnailing_context_->clip_result = algorithm->GetCanvasCopyInfo( |
| 180 copy_rect.size(), | 180 copy_rect.size(), |
| 181 scale_factor, | 181 scale_factor, |
| 182 ©_rect, | 182 ©_rect, |
| 183 &thumbnailing_context_->requested_copy_size); | 183 &thumbnailing_context_->requested_copy_size); |
| 184 copy_from_surface_start_time_ = base::TimeTicks::Now(); |
| 184 view->CopyFromSurface(copy_rect, thumbnailing_context_->requested_copy_size, | 185 view->CopyFromSurface(copy_rect, thumbnailing_context_->requested_copy_size, |
| 185 base::Bind(&ThumbnailTabHelper::ProcessCapturedBitmap, | 186 base::Bind(&ThumbnailTabHelper::ProcessCapturedBitmap, |
| 186 weak_factory_.GetWeakPtr(), algorithm), | 187 weak_factory_.GetWeakPtr(), algorithm), |
| 187 kN32_SkColorType); | 188 kN32_SkColorType); |
| 188 } | 189 } |
| 189 | 190 |
| 190 void ThumbnailTabHelper::ProcessCapturedBitmap( | 191 void ThumbnailTabHelper::ProcessCapturedBitmap( |
| 191 scoped_refptr<ThumbnailingAlgorithm> algorithm, | 192 scoped_refptr<ThumbnailingAlgorithm> algorithm, |
| 192 const SkBitmap& bitmap, | 193 const SkBitmap& bitmap, |
| 193 content::ReadbackResponse response) { | 194 content::ReadbackResponse response) { |
| 195 base::TimeDelta copy_from_surface_time = |
| 196 base::TimeTicks::Now() - copy_from_surface_start_time_; |
| 197 UMA_HISTOGRAM_TIMES("Thumbnails.CopyFromSurfaceTime", copy_from_surface_time); |
| 198 |
| 194 if (response == content::READBACK_SUCCESS) { | 199 if (response == content::READBACK_SUCCESS) { |
| 195 // On success, we must be on the UI thread. | 200 // On success, we must be on the UI thread. |
| 196 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 201 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 202 process_bitmap_start_time_ = base::TimeTicks::Now(); |
| 197 algorithm->ProcessBitmap(thumbnailing_context_, | 203 algorithm->ProcessBitmap(thumbnailing_context_, |
| 198 base::Bind(&ThumbnailTabHelper::UpdateThumbnail, | 204 base::Bind(&ThumbnailTabHelper::UpdateThumbnail, |
| 199 weak_factory_.GetWeakPtr()), | 205 weak_factory_.GetWeakPtr()), |
| 200 bitmap); | 206 bitmap); |
| 201 } else { | 207 } else { |
| 202 // On failure because of shutdown we are not on the UI thread, so ensure | 208 // On failure because of shutdown we are not on the UI thread, so ensure |
| 203 // that cleanup happens on that thread. | 209 // that cleanup happens on that thread. |
| 204 content::BrowserThread::PostTask( | 210 content::BrowserThread::PostTask( |
| 205 content::BrowserThread::UI, | 211 content::BrowserThread::UI, |
| 206 FROM_HERE, | 212 FROM_HERE, |
| 207 base::Bind(&ThumbnailTabHelper::CleanUpFromThumbnailGeneration, | 213 base::Bind(&ThumbnailTabHelper::CleanUpFromThumbnailGeneration, |
| 208 weak_factory_.GetWeakPtr())); | 214 weak_factory_.GetWeakPtr())); |
| 209 } | 215 } |
| 210 } | 216 } |
| 211 | 217 |
| 212 void ThumbnailTabHelper::CleanUpFromThumbnailGeneration() { | |
| 213 // Make a note that thumbnail generation is complete. | |
| 214 thumbnailing_context_ = nullptr; | |
| 215 } | |
| 216 | |
| 217 void ThumbnailTabHelper::UpdateThumbnail(const ThumbnailingContext& context, | 218 void ThumbnailTabHelper::UpdateThumbnail(const ThumbnailingContext& context, |
| 218 const SkBitmap& thumbnail) { | 219 const SkBitmap& thumbnail) { |
| 219 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 220 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 221 base::TimeDelta process_bitmap_time = |
| 222 base::TimeTicks::Now() - process_bitmap_start_time_; |
| 223 UMA_HISTOGRAM_TIMES("Thumbnails.ProcessBitmapTime", process_bitmap_time); |
| 224 |
| 220 // Feed the constructed thumbnail to the thumbnail service. | 225 // Feed the constructed thumbnail to the thumbnail service. |
| 221 gfx::Image image = gfx::Image::CreateFrom1xBitmap(thumbnail); | 226 gfx::Image image = gfx::Image::CreateFrom1xBitmap(thumbnail); |
| 222 context.service->SetPageThumbnail(context, image); | 227 context.service->SetPageThumbnail(context, image); |
| 223 DVLOG(1) << "Thumbnail taken for " << context.url << ": " | 228 DVLOG(1) << "Thumbnail taken for " << context.url << ": " |
| 224 << context.score.ToString(); | 229 << context.score.ToString(); |
| 225 | 230 |
| 226 CleanUpFromThumbnailGeneration(); | 231 CleanUpFromThumbnailGeneration(); |
| 227 } | 232 } |
| 228 | 233 |
| 229 void ThumbnailTabHelper::RenderViewHostCreated(RenderViewHost* renderer) { | 234 void ThumbnailTabHelper::CleanUpFromThumbnailGeneration() { |
| 235 // Make a note that thumbnail generation is complete. |
| 236 thumbnailing_context_ = nullptr; |
| 237 } |
| 238 |
| 239 void ThumbnailTabHelper::RenderViewHostCreated( |
| 240 content::RenderViewHost* renderer) { |
| 230 // NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED is really a new | 241 // NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED is really a new |
| 231 // RenderView, not RenderViewHost, and there is no good way to get | 242 // RenderView, not RenderViewHost, and there is no good way to get |
| 232 // notifications of RenderViewHosts. So just be tolerant of re-registrations. | 243 // notifications of RenderViewHosts. So just be tolerant of re-registrations. |
| 233 bool registered = registrar_.IsRegistered( | 244 bool registered = registrar_.IsRegistered( |
| 234 this, content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, | 245 this, content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, |
| 235 content::Source<RenderWidgetHost>(renderer->GetWidget())); | 246 content::Source<content::RenderWidgetHost>(renderer->GetWidget())); |
| 236 if (!registered) { | 247 if (!registered) { |
| 237 registrar_.Add(this, content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, | 248 registrar_.Add( |
| 238 content::Source<RenderWidgetHost>(renderer->GetWidget())); | 249 this, content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, |
| 250 content::Source<content::RenderWidgetHost>(renderer->GetWidget())); |
| 239 } | 251 } |
| 240 } | 252 } |
| 241 | 253 |
| 242 void ThumbnailTabHelper::WidgetHidden(RenderWidgetHost* widget) { | 254 void ThumbnailTabHelper::WidgetHidden(content::RenderWidgetHost* widget) { |
| 243 UpdateThumbnailIfNecessary(); | 255 UpdateThumbnailIfNecessary(); |
| 244 } | 256 } |
| OLD | NEW |