Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / thumbnails / thumbnail_tab_helper.cc
blobf8322e76f9a2d9573939dea942a32fe342b8bffb
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"
25 #if defined(OS_WIN)
26 #include "base/win/windows_version.h"
27 #endif
29 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ThumbnailTabHelper);
31 class SkBitmap;
33 // Overview
34 // --------
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
38 // simple as follows:
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;
54 namespace {
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)
70 return;
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();
85 if (!view)
86 return;
87 if (!view->IsSurfaceAvailableForCopy())
88 return;
90 gfx::Rect copy_rect = gfx::Rect(view->GetViewBounds().size());
91 // Clip the pixels that will commonly hold a scrollbar, which looks bad in
92 // thumbnails.
93 int scrollbar_size = gfx::scrollbar_size();
94 gfx::Size copy_size;
95 copy_rect.Inset(0, 0, scrollbar_size, scrollbar_size);
97 if (copy_rect.IsEmpty())
98 return;
100 ui::ScaleFactor scale_factor =
101 ui::GetSupportedScaleFactor(
102 ui::GetScaleFactorForNativeView(view->GetNativeView()));
103 context->clip_result = algorithm->GetCanvasCopyInfo(
104 copy_rect.size(),
105 scale_factor,
106 &copy_rect,
107 &context->requested_copy_size);
108 render_widget_host->CopyFromBackingStore(
109 copy_rect,
110 context->requested_copy_size,
111 base::Bind(&ProcessCapturedBitmap, context, algorithm),
112 kN32_SkColorType);
115 } // namespace
117 ThumbnailTabHelper::ThumbnailTabHelper(content::WebContents* contents)
118 : content::WebContentsObserver(contents),
119 enabled_(true),
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.
125 registrar_.Add(this,
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) {
136 switch (type) {
137 case content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED:
138 RenderViewHostCreated(content::Details<RenderViewHost>(details).ptr());
139 break;
141 case content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED:
142 if (!*content::Details<bool>(details).ptr())
143 WidgetHidden(content::Source<RenderWidgetHost>(source).ptr());
144 break;
146 default:
147 NOTREACHED() << "Unexpected notification type: " << type;
151 void ThumbnailTabHelper::RenderViewDeleted(
152 content::RenderViewHost* render_view_host) {
153 bool registered = registrar_.IsRegistered(
154 this,
155 content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED,
156 content::Source<RenderWidgetHost>(render_view_host));
157 if (registered) {
158 registrar_.Remove(
159 this,
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
171 // stop button.
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())
181 return;
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())
185 return;
186 const GURL& url = web_contents->GetURL();
187 Profile* profile =
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)) {
196 return;
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(
213 this,
214 content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED,
215 content::Source<RenderWidgetHost>(renderer));
216 if (!registered) {
217 registrar_.Add(
218 this,
219 content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED,
220 content::Source<RenderWidgetHost>(renderer));
224 void ThumbnailTabHelper::WidgetHidden(RenderWidgetHost* widget) {
225 if (!enabled_)
226 return;
227 UpdateThumbnailIfNecessary(web_contents());