Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(181)

Side by Side Diff: chrome/browser/thumbnails/thumbnail_tab_helper.cc

Issue 2859263002: ThumbnailTabHelper: Capture thumbnail when page load finishes (Closed)
Patch Set: review: comment Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/thumbnails/thumbnail_tab_helper.h ('k') | chrome/common/chrome_features.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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().
40 // If features::kCaptureThumbnailOnLoadFinished is enabled, then a thumbnail
41 // may also be captured when a page load finishes (subject to the same
42 // heuristics).
46 43
47 using content::RenderViewHost; 44 using content::RenderViewHost;
48 using content::RenderWidgetHost; 45 using content::RenderWidgetHost;
49 using content::WebContents; 46 using content::WebContents;
50 47
51 using thumbnails::ClipResult;
52 using thumbnails::ThumbnailingContext; 48 using thumbnails::ThumbnailingContext;
53 using thumbnails::ThumbnailingAlgorithm; 49 using thumbnails::ThumbnailingAlgorithm;
54 50
55 ThumbnailTabHelper::ThumbnailTabHelper(content::WebContents* contents) 51 ThumbnailTabHelper::ThumbnailTabHelper(content::WebContents* contents)
56 : content::WebContentsObserver(contents), 52 : content::WebContentsObserver(contents),
53 capture_on_load_finished_(base::FeatureList::IsEnabled(
54 features::kCaptureThumbnailOnLoadFinished)),
57 load_interrupted_(false), 55 load_interrupted_(false),
58 weak_factory_(this) { 56 weak_factory_(this) {
59 // Even though we deal in RenderWidgetHosts, we only care about its 57 // 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 58 // subclass, RenderViewHost when it is in a tab. We don't make thumbnails
61 // for RenderViewHosts that aren't in tabs, or RenderWidgetHosts that 59 // for RenderViewHosts that aren't in tabs, or RenderWidgetHosts that
62 // aren't views like select popups. 60 // aren't views like select popups.
63 registrar_.Add(this, 61 registrar_.Add(this,
64 content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED, 62 content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED,
65 content::Source<WebContents>(contents)); 63 content::Source<WebContents>(contents));
66 } 64 }
(...skipping 12 matching lines...) Expand all
79 case content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED: 77 case content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED:
80 if (!*content::Details<bool>(details).ptr()) 78 if (!*content::Details<bool>(details).ptr())
81 WidgetHidden(content::Source<RenderWidgetHost>(source).ptr()); 79 WidgetHidden(content::Source<RenderWidgetHost>(source).ptr());
82 break; 80 break;
83 81
84 default: 82 default:
85 NOTREACHED() << "Unexpected notification type: " << type; 83 NOTREACHED() << "Unexpected notification type: " << type;
86 } 84 }
87 } 85 }
88 86
89 void ThumbnailTabHelper::RenderViewDeleted( 87 void ThumbnailTabHelper::RenderViewDeleted(RenderViewHost* render_view_host) {
90 content::RenderViewHost* render_view_host) {
91 bool registered = registrar_.IsRegistered( 88 bool registered = registrar_.IsRegistered(
92 this, content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, 89 this, content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED,
93 content::Source<RenderWidgetHost>(render_view_host->GetWidget())); 90 content::Source<RenderWidgetHost>(render_view_host->GetWidget()));
94 if (registered) { 91 if (registered) {
95 registrar_.Remove( 92 registrar_.Remove(
96 this, content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, 93 this, content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED,
97 content::Source<RenderWidgetHost>(render_view_host->GetWidget())); 94 content::Source<RenderWidgetHost>(render_view_host->GetWidget()));
98 } 95 }
99 } 96 }
100 97
101 void ThumbnailTabHelper::DidStartLoading() { 98 void ThumbnailTabHelper::DidStartLoading() {
102 load_interrupted_ = false; 99 load_interrupted_ = false;
103 } 100 }
104 101
102 void ThumbnailTabHelper::DidStopLoading() {
103 if (capture_on_load_finished_) {
104 UpdateThumbnailIfNecessary();
105 }
106 }
107
105 void ThumbnailTabHelper::NavigationStopped() { 108 void ThumbnailTabHelper::NavigationStopped() {
106 // This function gets called when the page loading is interrupted by the 109 // This function gets called when the page loading is interrupted by the
107 // stop button. 110 // stop button.
108 load_interrupted_ = true; 111 load_interrupted_ = true;
109 } 112 }
110 113
111 void ThumbnailTabHelper::UpdateThumbnailIfNecessary() { 114 void ThumbnailTabHelper::UpdateThumbnailIfNecessary() {
112 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 115 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
113 // Ignore thumbnail update requests if one is already in progress. 116 // Ignore thumbnail update requests if one is already in progress.
114 if (thumbnailing_context_) { 117 if (thumbnailing_context_) {
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 gfx::Rect copy_rect = gfx::Rect(view->GetViewBounds().size()); 159 gfx::Rect copy_rect = gfx::Rect(view->GetViewBounds().size());
157 // Clip the pixels that will commonly hold a scrollbar, which looks bad in 160 // Clip the pixels that will commonly hold a scrollbar, which looks bad in
158 // thumbnails. 161 // thumbnails.
159 int scrollbar_size = gfx::scrollbar_size(); 162 int scrollbar_size = gfx::scrollbar_size();
160 copy_rect.Inset(0, 0, scrollbar_size, scrollbar_size); 163 copy_rect.Inset(0, 0, scrollbar_size, scrollbar_size);
161 164
162 if (copy_rect.IsEmpty()) { 165 if (copy_rect.IsEmpty()) {
163 return; 166 return;
164 } 167 }
165 168
166 scoped_refptr<thumbnails::ThumbnailingAlgorithm> algorithm( 169 scoped_refptr<ThumbnailingAlgorithm> algorithm(
167 thumbnail_service->GetThumbnailingAlgorithm()); 170 thumbnail_service->GetThumbnailingAlgorithm());
168 171
169 thumbnailing_context_ = new ThumbnailingContext(web_contents(), 172 thumbnailing_context_ = new ThumbnailingContext(web_contents(),
170 thumbnail_service.get(), 173 thumbnail_service.get(),
171 load_interrupted_); 174 load_interrupted_);
172 175
173 ui::ScaleFactor scale_factor = 176 ui::ScaleFactor scale_factor =
174 ui::GetSupportedScaleFactor( 177 ui::GetSupportedScaleFactor(
175 ui::GetScaleFactorForNativeView(view->GetNativeView())); 178 ui::GetScaleFactorForNativeView(view->GetNativeView()));
176 thumbnailing_context_->clip_result = algorithm->GetCanvasCopyInfo( 179 thumbnailing_context_->clip_result = algorithm->GetCanvasCopyInfo(
177 copy_rect.size(), 180 copy_rect.size(),
178 scale_factor, 181 scale_factor,
179 &copy_rect, 182 &copy_rect,
180 &thumbnailing_context_->requested_copy_size); 183 &thumbnailing_context_->requested_copy_size);
181 view->CopyFromSurface(copy_rect, thumbnailing_context_->requested_copy_size, 184 view->CopyFromSurface(copy_rect, thumbnailing_context_->requested_copy_size,
182 base::Bind(&ThumbnailTabHelper::ProcessCapturedBitmap, 185 base::Bind(&ThumbnailTabHelper::ProcessCapturedBitmap,
183 weak_factory_.GetWeakPtr(), algorithm), 186 weak_factory_.GetWeakPtr(), algorithm),
184 kN32_SkColorType); 187 kN32_SkColorType);
185 } 188 }
186 189
187 void ThumbnailTabHelper::ProcessCapturedBitmap( 190 void ThumbnailTabHelper::ProcessCapturedBitmap(
188 scoped_refptr<thumbnails::ThumbnailingAlgorithm> algorithm, 191 scoped_refptr<ThumbnailingAlgorithm> algorithm,
189 const SkBitmap& bitmap, 192 const SkBitmap& bitmap,
190 content::ReadbackResponse response) { 193 content::ReadbackResponse response) {
191 if (response == content::READBACK_SUCCESS) { 194 if (response == content::READBACK_SUCCESS) {
192 // On success, we must be on the UI thread. 195 // On success, we must be on the UI thread.
193 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 196 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
194 algorithm->ProcessBitmap(thumbnailing_context_, 197 algorithm->ProcessBitmap(thumbnailing_context_,
195 base::Bind(&ThumbnailTabHelper::UpdateThumbnail, 198 base::Bind(&ThumbnailTabHelper::UpdateThumbnail,
196 weak_factory_.GetWeakPtr()), 199 weak_factory_.GetWeakPtr()),
197 bitmap); 200 bitmap);
198 } else { 201 } else {
199 // On failure because of shutdown we are not on the UI thread, so ensure 202 // On failure because of shutdown we are not on the UI thread, so ensure
200 // that cleanup happens on that thread. 203 // that cleanup happens on that thread.
201 content::BrowserThread::PostTask( 204 content::BrowserThread::PostTask(
202 content::BrowserThread::UI, 205 content::BrowserThread::UI,
203 FROM_HERE, 206 FROM_HERE,
204 base::Bind(&ThumbnailTabHelper::CleanUpFromThumbnailGeneration, 207 base::Bind(&ThumbnailTabHelper::CleanUpFromThumbnailGeneration,
205 weak_factory_.GetWeakPtr())); 208 weak_factory_.GetWeakPtr()));
206 } 209 }
207 } 210 }
208 211
209 void ThumbnailTabHelper::CleanUpFromThumbnailGeneration() { 212 void ThumbnailTabHelper::CleanUpFromThumbnailGeneration() {
210 // Make a note that thumbnail generation is complete. 213 // Make a note that thumbnail generation is complete.
211 thumbnailing_context_ = nullptr; 214 thumbnailing_context_ = nullptr;
212 } 215 }
213 216
214 void ThumbnailTabHelper::UpdateThumbnail( 217 void ThumbnailTabHelper::UpdateThumbnail(const ThumbnailingContext& context,
215 const thumbnails::ThumbnailingContext& context, 218 const SkBitmap& thumbnail) {
216 const SkBitmap& thumbnail) {
217 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 219 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
218 // Feed the constructed thumbnail to the thumbnail service. 220 // Feed the constructed thumbnail to the thumbnail service.
219 gfx::Image image = gfx::Image::CreateFrom1xBitmap(thumbnail); 221 gfx::Image image = gfx::Image::CreateFrom1xBitmap(thumbnail);
220 context.service->SetPageThumbnail(context, image); 222 context.service->SetPageThumbnail(context, image);
221 DVLOG(1) << "Thumbnail taken for " << context.url << ": " 223 DVLOG(1) << "Thumbnail taken for " << context.url << ": "
222 << context.score.ToString(); 224 << context.score.ToString();
223 225
224 CleanUpFromThumbnailGeneration(); 226 CleanUpFromThumbnailGeneration();
225 } 227 }
226 228
227 void ThumbnailTabHelper::RenderViewHostCreated( 229 void ThumbnailTabHelper::RenderViewHostCreated(RenderViewHost* renderer) {
228 content::RenderViewHost* renderer) {
229 // NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED is really a new 230 // NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED is really a new
230 // RenderView, not RenderViewHost, and there is no good way to get 231 // RenderView, not RenderViewHost, and there is no good way to get
231 // notifications of RenderViewHosts. So just be tolerant of re-registrations. 232 // notifications of RenderViewHosts. So just be tolerant of re-registrations.
232 bool registered = registrar_.IsRegistered( 233 bool registered = registrar_.IsRegistered(
233 this, content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, 234 this, content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED,
234 content::Source<RenderWidgetHost>(renderer->GetWidget())); 235 content::Source<RenderWidgetHost>(renderer->GetWidget()));
235 if (!registered) { 236 if (!registered) {
236 registrar_.Add(this, content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, 237 registrar_.Add(this, content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED,
237 content::Source<RenderWidgetHost>(renderer->GetWidget())); 238 content::Source<RenderWidgetHost>(renderer->GetWidget()));
238 } 239 }
239 } 240 }
240 241
241 void ThumbnailTabHelper::WidgetHidden(RenderWidgetHost* widget) { 242 void ThumbnailTabHelper::WidgetHidden(RenderWidgetHost* widget) {
242 UpdateThumbnailIfNecessary(); 243 UpdateThumbnailIfNecessary();
243 } 244 }
OLDNEW
« no previous file with comments | « chrome/browser/thumbnails/thumbnail_tab_helper.h ('k') | chrome/common/chrome_features.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698