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/notification_details.h"
14 #include "content/public/browser/notification_source.h"
15 #include "content/public/browser/notification_types.h"
16 #include "content/public/browser/render_view_host.h"
17 #include "content/public/browser/render_widget_host_view.h"
18 #include "ui/gfx/color_utils.h"
19 #include "ui/gfx/size_conversions.h"
20 #include "ui/gfx/screen.h"
21 #include "ui/gfx/scrollbar_size.h"
22 #include "ui/gfx/skbitmap_operations.h"
25 #include "base/win/windows_version.h"
28 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ThumbnailTabHelper
);
34 // This class provides a service for updating thumbnails to be used in
35 // "Most visited" section of the new tab page. The service can be started
36 // by StartThumbnailing(). The current algorithm of the service is as
39 // When a renderer is about to be hidden (this usually occurs when the
40 // current tab is closed or another tab is clicked), update the
41 // thumbnail for the tab rendered by the renderer, if needed. The
42 // heuristics to judge whether or not to update the thumbnail is
43 // implemented in ShouldUpdateThumbnail().
45 using content::RenderViewHost
;
46 using content::RenderWidgetHost
;
47 using content::WebContents
;
49 using thumbnails::ClipResult
;
50 using thumbnails::ThumbnailingContext
;
51 using thumbnails::ThumbnailingAlgorithm
;
55 // Feed the constructed thumbnail to the thumbnail service.
56 void UpdateThumbnail(const ThumbnailingContext
& context
,
57 const SkBitmap
& thumbnail
) {
58 gfx::Image image
= gfx::Image::CreateFrom1xBitmap(thumbnail
);
59 context
.service
->SetPageThumbnail(context
, image
);
60 VLOG(1) << "Thumbnail taken for " << context
.url
<< ": "
61 << context
.score
.ToString();
64 void ProcessCapturedBitmap(scoped_refptr
<ThumbnailingContext
> context
,
65 scoped_refptr
<ThumbnailingAlgorithm
> algorithm
,
67 const SkBitmap
& bitmap
) {
71 // On success, we must be on the UI thread (on failure because of shutdown we
72 // are not on the UI thread).
73 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
75 algorithm
->ProcessBitmap(context
, base::Bind(&UpdateThumbnail
), bitmap
);
78 void AsyncProcessThumbnail(content::WebContents
* web_contents
,
79 scoped_refptr
<ThumbnailingContext
> context
,
80 scoped_refptr
<ThumbnailingAlgorithm
> algorithm
) {
81 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
82 RenderWidgetHost
* render_widget_host
= web_contents
->GetRenderViewHost();
83 content::RenderWidgetHostView
* view
= render_widget_host
->GetView();
86 if (!view
->IsSurfaceAvailableForCopy())
89 gfx::Rect copy_rect
= gfx::Rect(view
->GetViewBounds().size());
90 // Clip the pixels that will commonly hold a scrollbar, which looks bad in
92 int scrollbar_size
= gfx::scrollbar_size();
94 copy_rect
.Inset(0, 0, scrollbar_size
, scrollbar_size
);
96 if (copy_rect
.IsEmpty())
99 ui::ScaleFactor scale_factor
=
100 ui::GetSupportedScaleFactor(
101 ui::GetScaleFactorForNativeView(view
->GetNativeView()));
102 context
->clip_result
= algorithm
->GetCanvasCopyInfo(
106 &context
->requested_copy_size
);
107 render_widget_host
->CopyFromBackingStore(
109 context
->requested_copy_size
,
110 base::Bind(&ProcessCapturedBitmap
, context
, algorithm
),
116 ThumbnailTabHelper::ThumbnailTabHelper(content::WebContents
* contents
)
117 : content::WebContentsObserver(contents
),
119 load_interrupted_(false) {
120 // Even though we deal in RenderWidgetHosts, we only care about its
121 // subclass, RenderViewHost when it is in a tab. We don't make thumbnails
122 // for RenderViewHosts that aren't in tabs, or RenderWidgetHosts that
123 // aren't views like select popups.
125 content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED
,
126 content::Source
<WebContents
>(contents
));
129 ThumbnailTabHelper::~ThumbnailTabHelper() {
132 void ThumbnailTabHelper::Observe(int type
,
133 const content::NotificationSource
& source
,
134 const content::NotificationDetails
& details
) {
136 case content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED
:
137 RenderViewHostCreated(content::Details
<RenderViewHost
>(details
).ptr());
140 case content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED
:
141 if (!*content::Details
<bool>(details
).ptr())
142 WidgetHidden(content::Source
<RenderWidgetHost
>(source
).ptr());
146 NOTREACHED() << "Unexpected notification type: " << type
;
150 void ThumbnailTabHelper::RenderViewDeleted(
151 content::RenderViewHost
* render_view_host
) {
152 bool registered
= registrar_
.IsRegistered(
154 content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED
,
155 content::Source
<RenderWidgetHost
>(render_view_host
));
159 content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED
,
160 content::Source
<RenderWidgetHost
>(render_view_host
));
164 void ThumbnailTabHelper::DidStartLoading(
165 content::RenderViewHost
* render_view_host
) {
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());