Chromium Code Reviews| 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/tab_contents/thumbnail_generator.h" | 5 #include "chrome/browser/tab_contents/thumbnail_generator.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <map> | 8 #include <map> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 25 #include "content/public/browser/render_widget_host_view.h" | 25 #include "content/public/browser/render_widget_host_view.h" |
| 26 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
| 27 #include "googleurl/src/gurl.h" | 27 #include "googleurl/src/gurl.h" |
| 28 #include "skia/ext/image_operations.h" | 28 #include "skia/ext/image_operations.h" |
| 29 #include "skia/ext/platform_canvas.h" | 29 #include "skia/ext/platform_canvas.h" |
| 30 #include "third_party/skia/include/core/SkBitmap.h" | 30 #include "third_party/skia/include/core/SkBitmap.h" |
| 31 #include "ui/gfx/color_utils.h" | 31 #include "ui/gfx/color_utils.h" |
| 32 #include "ui/gfx/rect.h" | 32 #include "ui/gfx/rect.h" |
| 33 #include "ui/gfx/skbitmap_operations.h" | 33 #include "ui/gfx/skbitmap_operations.h" |
| 34 | 34 |
| 35 #if defined(OS_WIN) | |
| 36 #include "base/win/windows_version.h" | |
| 37 #endif | |
| 38 | |
| 35 // Overview | 39 // Overview |
| 36 // -------- | 40 // -------- |
| 37 // This class provides current thumbnails for tabs. The simplest operation is | 41 // This class provides current thumbnails for tabs. The simplest operation is |
| 38 // when a request for a thumbnail comes in, to grab the backing store and make | 42 // when a request for a thumbnail comes in, to grab the backing store and make |
| 39 // a smaller version of that. Clients of the class can send such a request by | 43 // a smaller version of that. Clients of the class can send such a request by |
| 40 // AskForSnapshot(). | 44 // AskForSnapshot(). |
| 41 // | 45 // |
| 42 // The class also provides a service for updating thumbnails to be used in | 46 // The class also provides a service for updating thumbnails to be used in |
| 43 // "Most visited" section of the new tab page. The service can be started | 47 // "Most visited" section of the new tab page. The service can be started |
| 44 // by StartThumbnailing(). The current algorithm of the service is as | 48 // by StartThumbnailing(). The current algorithm of the service is as |
| (...skipping 387 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 432 } | 436 } |
| 433 } | 437 } |
| 434 | 438 |
| 435 SkBitmap clipped_bitmap; | 439 SkBitmap clipped_bitmap; |
| 436 bitmap.extractSubset(&clipped_bitmap, src_rect); | 440 bitmap.extractSubset(&clipped_bitmap, src_rect); |
| 437 return clipped_bitmap; | 441 return clipped_bitmap; |
| 438 } | 442 } |
| 439 | 443 |
| 440 void ThumbnailGenerator::UpdateThumbnailIfNecessary( | 444 void ThumbnailGenerator::UpdateThumbnailIfNecessary( |
| 441 WebContents* web_contents) { | 445 WebContents* web_contents) { |
| 442 content::RenderWidgetHostView* view = web_contents->GetRenderWidgetHostView(); | |
| 443 if (!view) | |
| 444 return; | |
| 445 bool surface_available = view->IsSurfaceAvailableForCopy(); | |
| 446 // Skip if we can't update the thumbnail. | |
| 447 if (!surface_available) | |
| 448 return; | |
| 449 // Skip if a pending entry exists. WidgetHidden can be called while navigaing | 446 // Skip if a pending entry exists. WidgetHidden can be called while navigaing |
| 450 // pages and this is not a timing when thumbnails should be generated. | 447 // pages and this is not a timing when thumbnails should be generated. |
| 451 if (web_contents->GetController().GetPendingEntry()) | 448 if (web_contents->GetController().GetPendingEntry()) |
| 452 return; | 449 return; |
| 453 const GURL& url = web_contents->GetURL(); | 450 const GURL& url = web_contents->GetURL(); |
| 454 Profile* profile = | 451 Profile* profile = |
| 455 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 452 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 456 history::TopSites* top_sites = profile->GetTopSites(); | 453 history::TopSites* top_sites = profile->GetTopSites(); |
| 457 // Skip if we don't need to update the thumbnail. | 454 // Skip if we don't need to update the thumbnail. |
| 458 if (!ShouldUpdateThumbnail(profile, top_sites, url)) | 455 if (!ShouldUpdateThumbnail(profile, top_sites, url)) |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 488 VLOG(1) << "Thumbnail taken for " << url << ": " << score.ToString(); | 485 VLOG(1) << "Thumbnail taken for " << url << ": " << score.ToString(); |
| 489 } | 486 } |
| 490 | 487 |
| 491 void ThumbnailGenerator::AsyncUpdateThumbnail( | 488 void ThumbnailGenerator::AsyncUpdateThumbnail( |
| 492 WebContents* web_contents) { | 489 WebContents* web_contents) { |
| 493 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 490 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 494 RenderWidgetHost* render_widget_host = web_contents->GetRenderViewHost(); | 491 RenderWidgetHost* render_widget_host = web_contents->GetRenderViewHost(); |
| 495 content::RenderWidgetHostView* view = render_widget_host->GetView(); | 492 content::RenderWidgetHostView* view = render_widget_host->GetView(); |
| 496 if (!view) | 493 if (!view) |
| 497 return; | 494 return; |
| 495 const bool surface_available = view->IsSurfaceAvailableForCopy(); | |
|
brettw
2012/07/12 19:20:26
I don't usually use "const" for local vars like th
mazda
2012/07/12 21:23:25
Done for |size|.
I deleted |surface_available| ins
| |
| 496 if (!surface_available) { | |
| 497 #if defined(OS_WIN) | |
| 498 // On Windows XP, neither the backing store nor the compositing surface is | |
| 499 // available in the browser when accelerated compositing is active, so ask | |
| 500 // the renderer to send a snapshot for creating the thumbnail. | |
| 501 if (base::win::GetVersion() < base::win::VERSION_VISTA) { | |
| 502 web_contents_weak_factory_.reset( | |
|
brettw
2012/07/12 19:20:26
This pattern is a bit unusual. If possible, I thin
mazda
2012/07/12 21:23:25
I changed the code to initialize |web_contents_wea
| |
| 503 new base::WeakPtrFactory<WebContents>(web_contents)); | |
| 504 const gfx::Size view_size = | |
| 505 render_widget_host->GetView()->GetViewBounds().size(); | |
| 506 AskForSnapshot(render_widget_host, | |
| 507 base::Bind(&ThumbnailGenerator::UpdateThumbnailWithBitmap, | |
| 508 weak_factory_.GetWeakPtr(), | |
| 509 web_contents_weak_factory_->GetWeakPtr()), | |
| 510 view_size, | |
| 511 view_size); | |
| 512 } | |
| 513 #endif | |
| 514 return; | |
| 515 } | |
| 498 | 516 |
| 499 const gfx::Size copy_size = | 517 const gfx::Size copy_size = |
| 500 GetCopySizeForThumbnail(view->GetViewBounds().size(), | 518 GetCopySizeForThumbnail(view->GetViewBounds().size(), |
| 501 gfx::Size(kThumbnailWidth, kThumbnailHeight)); | 519 gfx::Size(kThumbnailWidth, kThumbnailHeight)); |
| 502 skia::PlatformCanvas* temp_canvas = new skia::PlatformCanvas; | 520 skia::PlatformCanvas* temp_canvas = new skia::PlatformCanvas; |
| 503 web_contents_weak_factory_.reset( | 521 web_contents_weak_factory_.reset( |
| 504 new base::WeakPtrFactory<WebContents>(web_contents)); | 522 new base::WeakPtrFactory<WebContents>(web_contents)); |
| 505 render_widget_host->CopyFromBackingStore( | 523 render_widget_host->CopyFromBackingStore( |
| 506 gfx::Rect(), copy_size, temp_canvas, | 524 gfx::Rect(), copy_size, temp_canvas, |
| 507 base::Bind(&ThumbnailGenerator::AsyncUpdateThumbnailFinish, | 525 base::Bind(&ThumbnailGenerator::UpdateThumbnailWithCanvas, |
| 508 weak_factory_.GetWeakPtr(), | 526 weak_factory_.GetWeakPtr(), |
| 509 web_contents_weak_factory_->GetWeakPtr(), | 527 web_contents_weak_factory_->GetWeakPtr(), |
| 510 base::Owned(temp_canvas))); | 528 base::Owned(temp_canvas))); |
| 511 } | 529 } |
| 512 | 530 |
| 513 void ThumbnailGenerator::AsyncUpdateThumbnailFinish( | 531 void ThumbnailGenerator::UpdateThumbnailWithBitmap( |
| 532 base::WeakPtr<WebContents> web_contents, | |
| 533 const SkBitmap& bitmap) { | |
| 534 if (!web_contents.get()) | |
| 535 return; | |
| 536 | |
| 537 if (bitmap.isNull() || bitmap.empty()) | |
| 538 return; | |
| 539 | |
| 540 ClipResult clip_result; | |
| 541 SkBitmap thumbnail = CreateThumbnail(bitmap, | |
| 542 kThumbnailWidth, | |
| 543 kThumbnailHeight, | |
| 544 ThumbnailGenerator::kClippedThumbnail, | |
| 545 &clip_result); | |
| 546 UpdateThumbnail(web_contents.get(), thumbnail, clip_result); | |
| 547 } | |
| 548 | |
| 549 void ThumbnailGenerator::UpdateThumbnailWithCanvas( | |
| 514 base::WeakPtr<WebContents> web_contents, | 550 base::WeakPtr<WebContents> web_contents, |
| 515 skia::PlatformCanvas* temp_canvas, | 551 skia::PlatformCanvas* temp_canvas, |
| 516 bool succeeded) { | 552 bool succeeded) { |
| 517 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 553 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 518 // The weak pointer can be invalidated by the subsequent AsyncUpdateThumbnail. | 554 // The weak pointer can be invalidated by the subsequent AsyncUpdateThumbnail. |
| 519 if (!web_contents.get()) | 555 if (!web_contents.get()) |
| 520 return; | 556 return; |
| 521 | 557 |
| 522 if (!succeeded) | 558 if (!succeeded) |
| 523 return; | 559 return; |
| 524 | 560 |
| 525 SkBitmap bmp_with_scrollbars = | 561 SkBitmap bmp_with_scrollbars = |
| 526 skia::GetTopDevice(*temp_canvas)->accessBitmap(false); | 562 skia::GetTopDevice(*temp_canvas)->accessBitmap(false); |
| 527 ClipResult clip_result; | 563 UpdateThumbnailWithBitmap(web_contents, bmp_with_scrollbars); |
| 528 SkBitmap thumbnail = CreateThumbnail(bmp_with_scrollbars, | |
| 529 kThumbnailWidth, | |
| 530 kThumbnailHeight, | |
| 531 ThumbnailGenerator::kClippedThumbnail, | |
| 532 &clip_result); | |
| 533 UpdateThumbnail(web_contents.get(), thumbnail, clip_result); | |
| 534 } | 564 } |
| 535 | 565 |
| 536 bool ThumbnailGenerator::ShouldUpdateThumbnail(Profile* profile, | 566 bool ThumbnailGenerator::ShouldUpdateThumbnail(Profile* profile, |
| 537 history::TopSites* top_sites, | 567 history::TopSites* top_sites, |
| 538 const GURL& url) { | 568 const GURL& url) { |
| 539 if (!profile || !top_sites) | 569 if (!profile || !top_sites) |
| 540 return false; | 570 return false; |
| 541 // Skip if it's in the incognito mode. | 571 // Skip if it's in the incognito mode. |
| 542 if (profile->IsOffTheRecord()) | 572 if (profile->IsOffTheRecord()) |
| 543 return false; | 573 return false; |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 564 | 594 |
| 565 void ThumbnailGenerator::DidStartLoading() { | 595 void ThumbnailGenerator::DidStartLoading() { |
| 566 load_interrupted_ = false; | 596 load_interrupted_ = false; |
| 567 } | 597 } |
| 568 | 598 |
| 569 void ThumbnailGenerator::StopNavigation() { | 599 void ThumbnailGenerator::StopNavigation() { |
| 570 // This function gets called when the page loading is interrupted by the | 600 // This function gets called when the page loading is interrupted by the |
| 571 // stop button. | 601 // stop button. |
| 572 load_interrupted_ = true; | 602 load_interrupted_ = true; |
| 573 } | 603 } |
| OLD | NEW |