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 "base/feature_list.h" |
8 #include "build/build_config.h" | 8 #include "base/metrics/histogram_macros.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/common/chrome_features.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/geometry/size_conversions.h" | |
22 #include "ui/gfx/scrollbar_size.h" | 21 #include "ui/gfx/scrollbar_size.h" |
23 | 22 |
24 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ThumbnailTabHelper); | 23 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ThumbnailTabHelper); |
25 | 24 |
26 class SkBitmap; | 25 class SkBitmap; |
27 | 26 |
28 // Overview | 27 // Overview |
29 // -------- | 28 // -------- |
30 // This class provides a service for updating thumbnails to be used in | 29 // This class provides a service for updating thumbnails to be used in |
31 // "Most visited" section of the new tab page. The service can be started | 30 // "Most visited" section of the new tab page. The service can be started |
32 // by UpdateThumbnailIfNecessary(). The current algorithm of the service is as | 31 // by UpdateThumbnailIfNecessary(). The current algorithm of the service is as |
33 // simple as follows: | 32 // simple as follows: |
34 // | 33 // |
35 // When a renderer is about to be hidden (this usually occurs when the | 34 // When a renderer is about to be hidden (this usually occurs when the |
36 // current tab is closed or another tab is clicked), update the | 35 // current tab is closed or another tab is clicked), update the |
37 // thumbnail for the tab rendered by the renderer, if needed. The | 36 // thumbnail for the tab rendered by the renderer, if needed. The |
38 // heuristics to judge whether or not to update the thumbnail is | 37 // heuristics to judge whether or not to update the thumbnail is |
39 // implemented in ShouldUpdateThumbnail(). | 38 // implemented in ShouldUpdateThumbnail(). |
40 // If features::kCaptureThumbnailOnLoadFinished is enabled, then a thumbnail | 39 // If features::kCaptureThumbnailOnLoadFinished is enabled, then a thumbnail |
41 // may also be captured when a page load finishes (subject to the same | 40 // may also be captured when a page load finishes (subject to the same |
42 // heuristics). | 41 // heuristics). |
43 | 42 |
44 using content::RenderViewHost; | 43 using content::RenderViewHost; |
45 using content::RenderWidgetHost; | 44 using content::RenderWidgetHost; |
46 using content::WebContents; | 45 using content::WebContents; |
47 | 46 |
48 using thumbnails::ThumbnailingContext; | 47 using thumbnails::ThumbnailingContext; |
49 using thumbnails::ThumbnailingAlgorithm; | 48 using thumbnails::ThumbnailingAlgorithm; |
50 | 49 |
51 ThumbnailTabHelper::ThumbnailTabHelper(content::WebContents* contents) | 50 ThumbnailTabHelper::ThumbnailTabHelper(WebContents* contents) |
jochen (gone - plz use gerrit)
2017/05/16 11:15:17
using content::Something should be the exception,
Marc Treib
2017/05/16 11:48:37
Done. (For all three "using content::"s)
| |
52 : content::WebContentsObserver(contents), | 51 : content::WebContentsObserver(contents), |
53 capture_on_load_finished_(base::FeatureList::IsEnabled( | 52 capture_on_load_finished_(base::FeatureList::IsEnabled( |
54 features::kCaptureThumbnailOnLoadFinished)), | 53 features::kCaptureThumbnailOnLoadFinished)), |
55 load_interrupted_(false), | 54 load_interrupted_(false), |
56 weak_factory_(this) { | 55 weak_factory_(this) { |
57 // Even though we deal in RenderWidgetHosts, we only care about its | 56 // Even though we deal in RenderWidgetHosts, we only care about its |
58 // subclass, RenderViewHost when it is in a tab. We don't make thumbnails | 57 // subclass, RenderViewHost when it is in a tab. We don't make thumbnails |
59 // for RenderViewHosts that aren't in tabs, or RenderWidgetHosts that | 58 // for RenderViewHosts that aren't in tabs, or RenderWidgetHosts that |
60 // aren't views like select popups. | 59 // aren't views like select popups. |
61 registrar_.Add(this, | 60 registrar_.Add(this, |
62 content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED, | 61 content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED, |
63 content::Source<WebContents>(contents)); | 62 content::Source<WebContents>(contents)); |
64 } | 63 } |
65 | 64 |
66 ThumbnailTabHelper::~ThumbnailTabHelper() { | 65 ThumbnailTabHelper::~ThumbnailTabHelper() = default; |
67 } | |
68 | 66 |
69 void ThumbnailTabHelper::Observe(int type, | 67 void ThumbnailTabHelper::Observe(int type, |
70 const content::NotificationSource& source, | 68 const content::NotificationSource& source, |
71 const content::NotificationDetails& details) { | 69 const content::NotificationDetails& details) { |
72 switch (type) { | 70 switch (type) { |
73 case content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED: | 71 case content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED: |
74 RenderViewHostCreated(content::Details<RenderViewHost>(details).ptr()); | 72 RenderViewHostCreated(content::Details<RenderViewHost>(details).ptr()); |
75 break; | 73 break; |
76 | 74 |
77 case content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED: | 75 case content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED: |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
114 void ThumbnailTabHelper::UpdateThumbnailIfNecessary() { | 112 void ThumbnailTabHelper::UpdateThumbnailIfNecessary() { |
115 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 113 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
116 // Ignore thumbnail update requests if one is already in progress. | 114 // Ignore thumbnail update requests if one is already in progress. |
117 if (thumbnailing_context_) { | 115 if (thumbnailing_context_) { |
118 return; | 116 return; |
119 } | 117 } |
120 | 118 |
121 // Destroying a WebContents may trigger it to be hidden, prompting a snapshot | 119 // Destroying a WebContents may trigger it to be hidden, prompting a snapshot |
122 // which would be unwise to attempt <http://crbug.com/130097>. If the | 120 // which would be unwise to attempt <http://crbug.com/130097>. If the |
123 // WebContents is in the middle of destruction, do not risk it. | 121 // WebContents is in the middle of destruction, do not risk it. |
124 if (!web_contents() || web_contents()->IsBeingDestroyed()) | 122 if (!web_contents() || web_contents()->IsBeingDestroyed()) { |
jochen (gone - plz use gerrit)
2017/05/16 11:15:17
why this change?
if (single line)
single line;
Marc Treib
2017/05/16 11:48:37
For local consistency, since there are a few other
| |
125 return; | 123 return; |
124 } | |
126 // Skip if a pending entry exists. WidgetHidden can be called while navigating | 125 // Skip if a pending entry exists. WidgetHidden can be called while navigating |
127 // pages and this is not a time when thumbnails should be generated. | 126 // pages and this is not a time when thumbnails should be generated. |
128 if (web_contents()->GetController().GetPendingEntry()) | 127 if (web_contents()->GetController().GetPendingEntry()) { |
129 return; | 128 return; |
129 } | |
130 const GURL& url = web_contents()->GetURL(); | 130 const GURL& url = web_contents()->GetURL(); |
131 Profile* profile = | 131 Profile* profile = |
132 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 132 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
133 | 133 |
134 scoped_refptr<thumbnails::ThumbnailService> thumbnail_service = | 134 scoped_refptr<thumbnails::ThumbnailService> thumbnail_service = |
135 ThumbnailServiceFactory::GetForProfile(profile); | 135 ThumbnailServiceFactory::GetForProfile(profile); |
136 | 136 |
137 // Skip if we don't need to update the thumbnail. | 137 // Skip if we don't need to update the thumbnail. |
138 if (thumbnail_service.get() == NULL || | 138 if (thumbnail_service.get() == NULL || |
139 !thumbnail_service->ShouldAcquirePageThumbnail(url)) { | 139 !thumbnail_service->ShouldAcquirePageThumbnail(url)) { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
174 load_interrupted_); | 174 load_interrupted_); |
175 | 175 |
176 ui::ScaleFactor scale_factor = | 176 ui::ScaleFactor scale_factor = |
177 ui::GetSupportedScaleFactor( | 177 ui::GetSupportedScaleFactor( |
178 ui::GetScaleFactorForNativeView(view->GetNativeView())); | 178 ui::GetScaleFactorForNativeView(view->GetNativeView())); |
179 thumbnailing_context_->clip_result = algorithm->GetCanvasCopyInfo( | 179 thumbnailing_context_->clip_result = algorithm->GetCanvasCopyInfo( |
180 copy_rect.size(), | 180 copy_rect.size(), |
181 scale_factor, | 181 scale_factor, |
182 ©_rect, | 182 ©_rect, |
183 &thumbnailing_context_->requested_copy_size); | 183 &thumbnailing_context_->requested_copy_size); |
184 copy_from_surface_start_time_ = base::TimeTicks::Now(); | |
184 view->CopyFromSurface(copy_rect, thumbnailing_context_->requested_copy_size, | 185 view->CopyFromSurface(copy_rect, thumbnailing_context_->requested_copy_size, |
185 base::Bind(&ThumbnailTabHelper::ProcessCapturedBitmap, | 186 base::Bind(&ThumbnailTabHelper::ProcessCapturedBitmap, |
186 weak_factory_.GetWeakPtr(), algorithm), | 187 weak_factory_.GetWeakPtr(), algorithm), |
187 kN32_SkColorType); | 188 kN32_SkColorType); |
188 } | 189 } |
189 | 190 |
190 void ThumbnailTabHelper::ProcessCapturedBitmap( | 191 void ThumbnailTabHelper::ProcessCapturedBitmap( |
191 scoped_refptr<ThumbnailingAlgorithm> algorithm, | 192 scoped_refptr<ThumbnailingAlgorithm> algorithm, |
192 const SkBitmap& bitmap, | 193 const SkBitmap& bitmap, |
193 content::ReadbackResponse response) { | 194 content::ReadbackResponse response) { |
195 base::TimeDelta copy_from_surface_time = | |
196 base::TimeTicks::Now() - copy_from_surface_start_time_; | |
197 UMA_HISTOGRAM_TIMES("Thumbnails.CopyFromSurfaceTime", copy_from_surface_time); | |
198 | |
194 if (response == content::READBACK_SUCCESS) { | 199 if (response == content::READBACK_SUCCESS) { |
195 // On success, we must be on the UI thread. | 200 // On success, we must be on the UI thread. |
196 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 201 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
202 process_bitmap_start_time_ = base::TimeTicks::Now(); | |
197 algorithm->ProcessBitmap(thumbnailing_context_, | 203 algorithm->ProcessBitmap(thumbnailing_context_, |
198 base::Bind(&ThumbnailTabHelper::UpdateThumbnail, | 204 base::Bind(&ThumbnailTabHelper::UpdateThumbnail, |
199 weak_factory_.GetWeakPtr()), | 205 weak_factory_.GetWeakPtr()), |
200 bitmap); | 206 bitmap); |
201 } else { | 207 } else { |
202 // On failure because of shutdown we are not on the UI thread, so ensure | 208 // On failure because of shutdown we are not on the UI thread, so ensure |
203 // that cleanup happens on that thread. | 209 // that cleanup happens on that thread. |
204 content::BrowserThread::PostTask( | 210 content::BrowserThread::PostTask( |
205 content::BrowserThread::UI, | 211 content::BrowserThread::UI, |
206 FROM_HERE, | 212 FROM_HERE, |
207 base::Bind(&ThumbnailTabHelper::CleanUpFromThumbnailGeneration, | 213 base::Bind(&ThumbnailTabHelper::CleanUpFromThumbnailGeneration, |
208 weak_factory_.GetWeakPtr())); | 214 weak_factory_.GetWeakPtr())); |
209 } | 215 } |
210 } | 216 } |
211 | 217 |
212 void ThumbnailTabHelper::CleanUpFromThumbnailGeneration() { | |
Marc Treib
2017/05/15 10:16:48
Moved to match the order in the header.
| |
213 // Make a note that thumbnail generation is complete. | |
214 thumbnailing_context_ = nullptr; | |
215 } | |
216 | |
217 void ThumbnailTabHelper::UpdateThumbnail(const ThumbnailingContext& context, | 218 void ThumbnailTabHelper::UpdateThumbnail(const ThumbnailingContext& context, |
218 const SkBitmap& thumbnail) { | 219 const SkBitmap& thumbnail) { |
219 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 220 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
221 base::TimeDelta process_bitmap_time = | |
222 base::TimeTicks::Now() - process_bitmap_start_time_; | |
223 UMA_HISTOGRAM_TIMES("Thumbnails.ProcessBitmapTime", process_bitmap_time); | |
224 | |
220 // Feed the constructed thumbnail to the thumbnail service. | 225 // Feed the constructed thumbnail to the thumbnail service. |
221 gfx::Image image = gfx::Image::CreateFrom1xBitmap(thumbnail); | 226 gfx::Image image = gfx::Image::CreateFrom1xBitmap(thumbnail); |
222 context.service->SetPageThumbnail(context, image); | 227 context.service->SetPageThumbnail(context, image); |
223 DVLOG(1) << "Thumbnail taken for " << context.url << ": " | 228 DVLOG(1) << "Thumbnail taken for " << context.url << ": " |
224 << context.score.ToString(); | 229 << context.score.ToString(); |
225 | 230 |
226 CleanUpFromThumbnailGeneration(); | 231 CleanUpFromThumbnailGeneration(); |
227 } | 232 } |
228 | 233 |
234 void ThumbnailTabHelper::CleanUpFromThumbnailGeneration() { | |
235 // Make a note that thumbnail generation is complete. | |
236 thumbnailing_context_ = nullptr; | |
237 } | |
238 | |
229 void ThumbnailTabHelper::RenderViewHostCreated(RenderViewHost* renderer) { | 239 void ThumbnailTabHelper::RenderViewHostCreated(RenderViewHost* renderer) { |
230 // NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED is really a new | 240 // NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED is really a new |
231 // RenderView, not RenderViewHost, and there is no good way to get | 241 // RenderView, not RenderViewHost, and there is no good way to get |
232 // notifications of RenderViewHosts. So just be tolerant of re-registrations. | 242 // notifications of RenderViewHosts. So just be tolerant of re-registrations. |
233 bool registered = registrar_.IsRegistered( | 243 bool registered = registrar_.IsRegistered( |
234 this, content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, | 244 this, content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, |
235 content::Source<RenderWidgetHost>(renderer->GetWidget())); | 245 content::Source<RenderWidgetHost>(renderer->GetWidget())); |
236 if (!registered) { | 246 if (!registered) { |
237 registrar_.Add(this, content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, | 247 registrar_.Add(this, content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, |
238 content::Source<RenderWidgetHost>(renderer->GetWidget())); | 248 content::Source<RenderWidgetHost>(renderer->GetWidget())); |
239 } | 249 } |
240 } | 250 } |
241 | 251 |
242 void ThumbnailTabHelper::WidgetHidden(RenderWidgetHost* widget) { | 252 void ThumbnailTabHelper::WidgetHidden(RenderWidgetHost* widget) { |
243 UpdateThumbnailIfNecessary(); | 253 UpdateThumbnailIfNecessary(); |
244 } | 254 } |
OLD | NEW |