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
3 // found in the LICENSE file.
5 #include "chrome/browser/thumbnails/thumbnail_tab_helper.h"
7 #include "chrome/browser/browser_process.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/thumbnails/thumbnail_service.h"
10 #include "chrome/browser/thumbnails/thumbnail_service_factory.h"
11 #include "chrome/browser/thumbnails/thumbnailing_algorithm.h"
12 #include "chrome/browser/thumbnails/thumbnailing_context.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/browser/notification_details.h"
15 #include "content/public/browser/notification_source.h"
16 #include "content/public/browser/notification_types.h"
17 #include "content/public/browser/render_view_host.h"
18 #include "content/public/browser/render_widget_host_view.h"
19 #include "ui/gfx/color_utils.h"
20 #include "ui/gfx/geometry/size_conversions.h"
21 #include "ui/gfx/screen.h"
22 #include "ui/gfx/scrollbar_size.h"
23 #include "ui/gfx/skbitmap_operations.h"
26 #include "base/win/windows_version.h"
29 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ThumbnailTabHelper
);
35 // This class provides a service for updating thumbnails to be used in
36 // "Most visited" section of the new tab page. The service can be started
37 // by StartThumbnailing(). The current algorithm of the service is as
40 // When a renderer is about to be hidden (this usually occurs when the
41 // current tab is closed or another tab is clicked), update the
42 // thumbnail for the tab rendered by the renderer, if needed. The
43 // heuristics to judge whether or not to update the thumbnail is
44 // implemented in ShouldUpdateThumbnail().
46 using content::RenderViewHost
;
47 using content::RenderWidgetHost
;
48 using content::WebContents
;
50 using thumbnails::ClipResult
;
51 using thumbnails::ThumbnailingContext
;
52 using thumbnails::ThumbnailingAlgorithm
;
56 // Feed the constructed thumbnail to the thumbnail service.
57 void UpdateThumbnail(const ThumbnailingContext
& context
,
58 const SkBitmap
& thumbnail
) {
59 gfx::Image image
= gfx::Image::CreateFrom1xBitmap(thumbnail
);
60 context
.service
->SetPageThumbnail(context
, image
);
61 DVLOG(1) << "Thumbnail taken for " << context
.url
<< ": "
62 << context
.score
.ToString();
65 void ProcessCapturedBitmap(scoped_refptr
<ThumbnailingContext
> context
,
66 scoped_refptr
<ThumbnailingAlgorithm
> algorithm
,
67 const SkBitmap
& bitmap
,
68 content::ReadbackResponse response
) {
69 if (response
!= content::READBACK_SUCCESS
)
72 // On success, we must be on the UI thread (on failure because of shutdown we
73 // are not on the UI thread).
74 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
76 algorithm
->ProcessBitmap(context
, base::Bind(&UpdateThumbnail
), bitmap
);
79 void AsyncProcessThumbnail(content::WebContents
* web_contents
,
80 scoped_refptr
<ThumbnailingContext
> context
,
81 scoped_refptr
<ThumbnailingAlgorithm
> algorithm
) {
82 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
83 RenderWidgetHost
* render_widget_host
= web_contents
->GetRenderViewHost();
84 content::RenderWidgetHostView
* view
= render_widget_host
->GetView();
87 if (!view
->IsSurfaceAvailableForCopy())
90 gfx::Rect copy_rect
= gfx::Rect(view
->GetViewBounds().size());
91 // Clip the pixels that will commonly hold a scrollbar, which looks bad in
93 int scrollbar_size
= gfx::scrollbar_size();
95 copy_rect
.Inset(0, 0, scrollbar_size
, scrollbar_size
);
97 if (copy_rect
.IsEmpty())
100 ui::ScaleFactor scale_factor
=
101 ui::GetSupportedScaleFactor(
102 ui::GetScaleFactorForNativeView(view
->GetNativeView()));
103 context
->clip_result
= algorithm
->GetCanvasCopyInfo(
107 &context
->requested_copy_size
);
108 render_widget_host
->CopyFromBackingStore(
110 context
->requested_copy_size
,
111 base::Bind(&ProcessCapturedBitmap
, context
, algorithm
),
117 ThumbnailTabHelper::ThumbnailTabHelper(content::WebContents
* contents
)
118 : content::WebContentsObserver(contents
),
120 load_interrupted_(false) {
121 // Even though we deal in RenderWidgetHosts, we only care about its
122 // subclass, RenderViewHost when it is in a tab. We don't make thumbnails
123 // for RenderViewHosts that aren't in tabs, or RenderWidgetHosts that
124 // aren't views like select popups.
126 content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED
,
127 content::Source
<WebContents
>(contents
));
130 ThumbnailTabHelper::~ThumbnailTabHelper() {
133 void ThumbnailTabHelper::Observe(int type
,
134 const content::NotificationSource
& source
,
135 const content::NotificationDetails
& details
) {
137 case content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED
:
138 RenderViewHostCreated(content::Details
<RenderViewHost
>(details
).ptr());
141 case content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED
:
142 if (!*content::Details
<bool>(details
).ptr())
143 WidgetHidden(content::Source
<RenderWidgetHost
>(source
).ptr());
147 NOTREACHED() << "Unexpected notification type: " << type
;
151 void ThumbnailTabHelper::RenderViewDeleted(
152 content::RenderViewHost
* render_view_host
) {
153 bool registered
= registrar_
.IsRegistered(
155 content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED
,
156 content::Source
<RenderWidgetHost
>(render_view_host
));
160 content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED
,
161 content::Source
<RenderWidgetHost
>(render_view_host
));
165 void ThumbnailTabHelper::DidStartLoading() {
166 load_interrupted_
= false;
169 void ThumbnailTabHelper::NavigationStopped() {
170 // This function gets called when the page loading is interrupted by the
172 load_interrupted_
= true;
175 void ThumbnailTabHelper::UpdateThumbnailIfNecessary(
176 WebContents
* web_contents
) {
177 // 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
179 // WebContents is in the middle of destruction, do not risk it.
180 if (!web_contents
|| web_contents
->IsBeingDestroyed())
182 // 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.
184 if (web_contents
->GetController().GetPendingEntry())
186 const GURL
& url
= web_contents
->GetURL();
188 Profile::FromBrowserContext(web_contents
->GetBrowserContext());
190 scoped_refptr
<thumbnails::ThumbnailService
> thumbnail_service
=
191 ThumbnailServiceFactory::GetForProfile(profile
);
193 // Skip if we don't need to update the thumbnail.
194 if (thumbnail_service
.get() == NULL
||
195 !thumbnail_service
->ShouldAcquirePageThumbnail(url
)) {
199 scoped_refptr
<thumbnails::ThumbnailingAlgorithm
> algorithm(
200 thumbnail_service
->GetThumbnailingAlgorithm());
202 scoped_refptr
<ThumbnailingContext
> context(new ThumbnailingContext(
203 web_contents
, thumbnail_service
.get(), load_interrupted_
));
204 AsyncProcessThumbnail(web_contents
, context
, algorithm
);
207 void ThumbnailTabHelper::RenderViewHostCreated(
208 content::RenderViewHost
* renderer
) {
209 // NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED is really a new
210 // RenderView, not RenderViewHost, and there is no good way to get
211 // notifications of RenderViewHosts. So just be tolerant of re-registrations.
212 bool registered
= registrar_
.IsRegistered(
214 content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED
,
215 content::Source
<RenderWidgetHost
>(renderer
));
219 content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED
,
220 content::Source
<RenderWidgetHost
>(renderer
));
224 void ThumbnailTabHelper::WidgetHidden(RenderWidgetHost
* widget
) {
227 UpdateThumbnailIfNecessary(web_contents());