Chromium Code Reviews| 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 "build/build_config.h" | 8 #include "build/build_config.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" |
| 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/browser/thumbnails/thumbnailing_context.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/color_utils.h" | |
| 22 #include "ui/gfx/geometry/size_conversions.h" | 21 #include "ui/gfx/geometry/size_conversions.h" |
| 23 #include "ui/gfx/scrollbar_size.h" | 22 #include "ui/gfx/scrollbar_size.h" |
| 24 #include "ui/gfx/skbitmap_operations.h" | |
| 25 | |
| 26 #if defined(OS_WIN) | |
| 27 #include "base/win/windows_version.h" | |
| 28 #endif | |
| 29 | 23 |
| 30 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ThumbnailTabHelper); | 24 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ThumbnailTabHelper); |
| 31 | 25 |
| 32 class SkBitmap; | 26 class SkBitmap; |
| 33 | 27 |
| 34 // Overview | 28 // Overview |
| 35 // -------- | 29 // -------- |
| 36 // This class provides a service for updating thumbnails to be used in | 30 // This class provides a service for updating thumbnails to be used in |
| 37 // "Most visited" section of the new tab page. The service can be started | 31 // "Most visited" section of the new tab page. The service can be started |
| 38 // by StartThumbnailing(). The current algorithm of the service is as | 32 // by UpdateThumbnailIfNecessary(). The current algorithm of the service is as |
| 39 // simple as follows: | 33 // simple as follows: |
| 40 // | 34 // |
| 41 // When a renderer is about to be hidden (this usually occurs when the | 35 // When a renderer is about to be hidden (this usually occurs when the |
|
Lei Zhang
2017/05/05 21:32:01
Can you mention the new behavior in a separate par
Marc Treib
2017/05/08 08:30:58
Done.
| |
| 42 // current tab is closed or another tab is clicked), update the | 36 // current tab is closed or another tab is clicked), update the |
| 43 // thumbnail for the tab rendered by the renderer, if needed. The | 37 // thumbnail for the tab rendered by the renderer, if needed. The |
| 44 // heuristics to judge whether or not to update the thumbnail is | 38 // heuristics to judge whether or not to update the thumbnail is |
| 45 // implemented in ShouldUpdateThumbnail(). | 39 // implemented in ShouldUpdateThumbnail(). |
| 46 | 40 |
| 47 using content::RenderViewHost; | 41 using content::RenderViewHost; |
| 48 using content::RenderWidgetHost; | 42 using content::RenderWidgetHost; |
| 49 using content::WebContents; | 43 using content::WebContents; |
| 50 | 44 |
| 51 using thumbnails::ClipResult; | |
| 52 using thumbnails::ThumbnailingContext; | 45 using thumbnails::ThumbnailingContext; |
| 53 using thumbnails::ThumbnailingAlgorithm; | 46 using thumbnails::ThumbnailingAlgorithm; |
| 54 | 47 |
| 55 ThumbnailTabHelper::ThumbnailTabHelper(content::WebContents* contents) | 48 ThumbnailTabHelper::ThumbnailTabHelper(content::WebContents* contents) |
| 56 : content::WebContentsObserver(contents), | 49 : content::WebContentsObserver(contents), |
| 50 capture_on_load_finished_(base::FeatureList::IsEnabled( | |
| 51 features::kCaptureThumbnailOnLoadFinished)), | |
| 57 load_interrupted_(false), | 52 load_interrupted_(false), |
| 58 weak_factory_(this) { | 53 weak_factory_(this) { |
| 59 // Even though we deal in RenderWidgetHosts, we only care about its | 54 // Even though we deal in RenderWidgetHosts, we only care about its |
| 60 // subclass, RenderViewHost when it is in a tab. We don't make thumbnails | 55 // subclass, RenderViewHost when it is in a tab. We don't make thumbnails |
| 61 // for RenderViewHosts that aren't in tabs, or RenderWidgetHosts that | 56 // for RenderViewHosts that aren't in tabs, or RenderWidgetHosts that |
| 62 // aren't views like select popups. | 57 // aren't views like select popups. |
| 63 registrar_.Add(this, | 58 registrar_.Add(this, |
| 64 content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED, | 59 content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED, |
| 65 content::Source<WebContents>(contents)); | 60 content::Source<WebContents>(contents)); |
| 66 } | 61 } |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 79 case content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED: | 74 case content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED: |
| 80 if (!*content::Details<bool>(details).ptr()) | 75 if (!*content::Details<bool>(details).ptr()) |
| 81 WidgetHidden(content::Source<RenderWidgetHost>(source).ptr()); | 76 WidgetHidden(content::Source<RenderWidgetHost>(source).ptr()); |
| 82 break; | 77 break; |
| 83 | 78 |
| 84 default: | 79 default: |
| 85 NOTREACHED() << "Unexpected notification type: " << type; | 80 NOTREACHED() << "Unexpected notification type: " << type; |
| 86 } | 81 } |
| 87 } | 82 } |
| 88 | 83 |
| 89 void ThumbnailTabHelper::RenderViewDeleted( | 84 void ThumbnailTabHelper::RenderViewDeleted(RenderViewHost* render_view_host) { |
| 90 content::RenderViewHost* render_view_host) { | |
| 91 bool registered = registrar_.IsRegistered( | 85 bool registered = registrar_.IsRegistered( |
| 92 this, content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, | 86 this, content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, |
| 93 content::Source<RenderWidgetHost>(render_view_host->GetWidget())); | 87 content::Source<RenderWidgetHost>(render_view_host->GetWidget())); |
| 94 if (registered) { | 88 if (registered) { |
| 95 registrar_.Remove( | 89 registrar_.Remove( |
| 96 this, content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, | 90 this, content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, |
| 97 content::Source<RenderWidgetHost>(render_view_host->GetWidget())); | 91 content::Source<RenderWidgetHost>(render_view_host->GetWidget())); |
| 98 } | 92 } |
| 99 } | 93 } |
| 100 | 94 |
| 101 void ThumbnailTabHelper::DidStartLoading() { | 95 void ThumbnailTabHelper::DidStartLoading() { |
| 102 load_interrupted_ = false; | 96 load_interrupted_ = false; |
| 103 } | 97 } |
| 104 | 98 |
| 99 void ThumbnailTabHelper::DidStopLoading() { | |
| 100 if (capture_on_load_finished_) { | |
| 101 UpdateThumbnailIfNecessary(); | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 void ThumbnailTabHelper::NavigationStopped() { | 105 void ThumbnailTabHelper::NavigationStopped() { |
| 106 // This function gets called when the page loading is interrupted by the | 106 // This function gets called when the page loading is interrupted by the |
| 107 // stop button. | 107 // stop button. |
| 108 load_interrupted_ = true; | 108 load_interrupted_ = true; |
| 109 } | 109 } |
| 110 | 110 |
| 111 void ThumbnailTabHelper::UpdateThumbnailIfNecessary() { | 111 void ThumbnailTabHelper::UpdateThumbnailIfNecessary() { |
| 112 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 112 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 113 // Ignore thumbnail update requests if one is already in progress. | 113 // Ignore thumbnail update requests if one is already in progress. |
| 114 if (thumbnailing_context_) { | 114 if (thumbnailing_context_) { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 156 gfx::Rect copy_rect = gfx::Rect(view->GetViewBounds().size()); | 156 gfx::Rect copy_rect = gfx::Rect(view->GetViewBounds().size()); |
| 157 // Clip the pixels that will commonly hold a scrollbar, which looks bad in | 157 // Clip the pixels that will commonly hold a scrollbar, which looks bad in |
| 158 // thumbnails. | 158 // thumbnails. |
| 159 int scrollbar_size = gfx::scrollbar_size(); | 159 int scrollbar_size = gfx::scrollbar_size(); |
| 160 copy_rect.Inset(0, 0, scrollbar_size, scrollbar_size); | 160 copy_rect.Inset(0, 0, scrollbar_size, scrollbar_size); |
| 161 | 161 |
| 162 if (copy_rect.IsEmpty()) { | 162 if (copy_rect.IsEmpty()) { |
| 163 return; | 163 return; |
| 164 } | 164 } |
| 165 | 165 |
| 166 scoped_refptr<thumbnails::ThumbnailingAlgorithm> algorithm( | 166 scoped_refptr<ThumbnailingAlgorithm> algorithm( |
| 167 thumbnail_service->GetThumbnailingAlgorithm()); | 167 thumbnail_service->GetThumbnailingAlgorithm()); |
| 168 | 168 |
| 169 thumbnailing_context_ = new ThumbnailingContext(web_contents(), | 169 thumbnailing_context_ = new ThumbnailingContext(web_contents(), |
| 170 thumbnail_service.get(), | 170 thumbnail_service.get(), |
| 171 load_interrupted_); | 171 load_interrupted_); |
| 172 | 172 |
| 173 ui::ScaleFactor scale_factor = | 173 ui::ScaleFactor scale_factor = |
| 174 ui::GetSupportedScaleFactor( | 174 ui::GetSupportedScaleFactor( |
| 175 ui::GetScaleFactorForNativeView(view->GetNativeView())); | 175 ui::GetScaleFactorForNativeView(view->GetNativeView())); |
| 176 thumbnailing_context_->clip_result = algorithm->GetCanvasCopyInfo( | 176 thumbnailing_context_->clip_result = algorithm->GetCanvasCopyInfo( |
| 177 copy_rect.size(), | 177 copy_rect.size(), |
| 178 scale_factor, | 178 scale_factor, |
| 179 ©_rect, | 179 ©_rect, |
| 180 &thumbnailing_context_->requested_copy_size); | 180 &thumbnailing_context_->requested_copy_size); |
| 181 view->CopyFromSurface(copy_rect, thumbnailing_context_->requested_copy_size, | 181 view->CopyFromSurface(copy_rect, thumbnailing_context_->requested_copy_size, |
| 182 base::Bind(&ThumbnailTabHelper::ProcessCapturedBitmap, | 182 base::Bind(&ThumbnailTabHelper::ProcessCapturedBitmap, |
| 183 weak_factory_.GetWeakPtr(), algorithm), | 183 weak_factory_.GetWeakPtr(), algorithm), |
| 184 kN32_SkColorType); | 184 kN32_SkColorType); |
| 185 } | 185 } |
| 186 | 186 |
| 187 void ThumbnailTabHelper::ProcessCapturedBitmap( | 187 void ThumbnailTabHelper::ProcessCapturedBitmap( |
| 188 scoped_refptr<thumbnails::ThumbnailingAlgorithm> algorithm, | 188 scoped_refptr<ThumbnailingAlgorithm> algorithm, |
| 189 const SkBitmap& bitmap, | 189 const SkBitmap& bitmap, |
| 190 content::ReadbackResponse response) { | 190 content::ReadbackResponse response) { |
| 191 if (response == content::READBACK_SUCCESS) { | 191 if (response == content::READBACK_SUCCESS) { |
| 192 // On success, we must be on the UI thread. | 192 // On success, we must be on the UI thread. |
| 193 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 193 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 194 algorithm->ProcessBitmap(thumbnailing_context_, | 194 algorithm->ProcessBitmap(thumbnailing_context_, |
| 195 base::Bind(&ThumbnailTabHelper::UpdateThumbnail, | 195 base::Bind(&ThumbnailTabHelper::UpdateThumbnail, |
| 196 weak_factory_.GetWeakPtr()), | 196 weak_factory_.GetWeakPtr()), |
| 197 bitmap); | 197 bitmap); |
| 198 } else { | 198 } else { |
| 199 // On failure because of shutdown we are not on the UI thread, so ensure | 199 // On failure because of shutdown we are not on the UI thread, so ensure |
| 200 // that cleanup happens on that thread. | 200 // that cleanup happens on that thread. |
| 201 content::BrowserThread::PostTask( | 201 content::BrowserThread::PostTask( |
| 202 content::BrowserThread::UI, | 202 content::BrowserThread::UI, |
| 203 FROM_HERE, | 203 FROM_HERE, |
| 204 base::Bind(&ThumbnailTabHelper::CleanUpFromThumbnailGeneration, | 204 base::Bind(&ThumbnailTabHelper::CleanUpFromThumbnailGeneration, |
| 205 weak_factory_.GetWeakPtr())); | 205 weak_factory_.GetWeakPtr())); |
| 206 } | 206 } |
| 207 } | 207 } |
| 208 | 208 |
| 209 void ThumbnailTabHelper::CleanUpFromThumbnailGeneration() { | 209 void ThumbnailTabHelper::CleanUpFromThumbnailGeneration() { |
| 210 // Make a note that thumbnail generation is complete. | 210 // Make a note that thumbnail generation is complete. |
| 211 thumbnailing_context_ = nullptr; | 211 thumbnailing_context_ = nullptr; |
| 212 } | 212 } |
| 213 | 213 |
| 214 void ThumbnailTabHelper::UpdateThumbnail( | 214 void ThumbnailTabHelper::UpdateThumbnail(const ThumbnailingContext& context, |
| 215 const thumbnails::ThumbnailingContext& context, | 215 const SkBitmap& thumbnail) { |
| 216 const SkBitmap& thumbnail) { | |
| 217 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 216 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 218 // Feed the constructed thumbnail to the thumbnail service. | 217 // Feed the constructed thumbnail to the thumbnail service. |
| 219 gfx::Image image = gfx::Image::CreateFrom1xBitmap(thumbnail); | 218 gfx::Image image = gfx::Image::CreateFrom1xBitmap(thumbnail); |
| 220 context.service->SetPageThumbnail(context, image); | 219 context.service->SetPageThumbnail(context, image); |
| 221 DVLOG(1) << "Thumbnail taken for " << context.url << ": " | 220 DVLOG(1) << "Thumbnail taken for " << context.url << ": " |
| 222 << context.score.ToString(); | 221 << context.score.ToString(); |
| 223 | 222 |
| 224 CleanUpFromThumbnailGeneration(); | 223 CleanUpFromThumbnailGeneration(); |
| 225 } | 224 } |
| 226 | 225 |
| 227 void ThumbnailTabHelper::RenderViewHostCreated( | 226 void ThumbnailTabHelper::RenderViewHostCreated(RenderViewHost* renderer) { |
| 228 content::RenderViewHost* renderer) { | |
| 229 // NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED is really a new | 227 // NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED is really a new |
| 230 // RenderView, not RenderViewHost, and there is no good way to get | 228 // RenderView, not RenderViewHost, and there is no good way to get |
| 231 // notifications of RenderViewHosts. So just be tolerant of re-registrations. | 229 // notifications of RenderViewHosts. So just be tolerant of re-registrations. |
| 232 bool registered = registrar_.IsRegistered( | 230 bool registered = registrar_.IsRegistered( |
| 233 this, content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, | 231 this, content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, |
| 234 content::Source<RenderWidgetHost>(renderer->GetWidget())); | 232 content::Source<RenderWidgetHost>(renderer->GetWidget())); |
| 235 if (!registered) { | 233 if (!registered) { |
| 236 registrar_.Add(this, content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, | 234 registrar_.Add(this, content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, |
| 237 content::Source<RenderWidgetHost>(renderer->GetWidget())); | 235 content::Source<RenderWidgetHost>(renderer->GetWidget())); |
| 238 } | 236 } |
| 239 } | 237 } |
| 240 | 238 |
| 241 void ThumbnailTabHelper::WidgetHidden(RenderWidgetHost* widget) { | 239 void ThumbnailTabHelper::WidgetHidden(RenderWidgetHost* widget) { |
| 242 UpdateThumbnailIfNecessary(); | 240 UpdateThumbnailIfNecessary(); |
| 243 } | 241 } |
| OLD | NEW |