| 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 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 if (iterator->second->renderer == renderer) { | 368 if (iterator->second->renderer == renderer) { |
| 369 ThumbnailCallbackMap::iterator nuked = iterator; | 369 ThumbnailCallbackMap::iterator nuked = iterator; |
| 370 ++iterator; | 370 ++iterator; |
| 371 callback_map_.erase(nuked); | 371 callback_map_.erase(nuked); |
| 372 continue; | 372 continue; |
| 373 } | 373 } |
| 374 ++iterator; | 374 ++iterator; |
| 375 } | 375 } |
| 376 } | 376 } |
| 377 | 377 |
| 378 double ThumbnailGenerator::CalculateBoringScore(SkBitmap* bitmap) { | 378 double ThumbnailGenerator::CalculateBoringScore(const SkBitmap& bitmap) { |
| 379 if (bitmap->isNull() || bitmap->empty()) | 379 if (bitmap.isNull() || bitmap.empty()) |
| 380 return 1.0; | 380 return 1.0; |
| 381 int histogram[256] = {0}; | 381 int histogram[256] = {0}; |
| 382 color_utils::BuildLumaHistogram(bitmap, histogram); | 382 color_utils::BuildLumaHistogram(bitmap, histogram); |
| 383 | 383 |
| 384 int color_count = *std::max_element(histogram, histogram + 256); | 384 int color_count = *std::max_element(histogram, histogram + 256); |
| 385 int pixel_count = bitmap->width() * bitmap->height(); | 385 int pixel_count = bitmap.width() * bitmap.height(); |
| 386 return static_cast<double>(color_count) / pixel_count; | 386 return static_cast<double>(color_count) / pixel_count; |
| 387 } | 387 } |
| 388 | 388 |
| 389 SkBitmap ThumbnailGenerator::GetClippedBitmap(const SkBitmap& bitmap, | 389 SkBitmap ThumbnailGenerator::GetClippedBitmap(const SkBitmap& bitmap, |
| 390 int desired_width, | 390 int desired_width, |
| 391 int desired_height, | 391 int desired_height, |
| 392 ClipResult* clip_result) { | 392 ClipResult* clip_result) { |
| 393 const SkRect dest_rect = { 0, 0, | 393 const SkRect dest_rect = { 0, 0, |
| 394 SkIntToScalar(desired_width), | 394 SkIntToScalar(desired_width), |
| 395 SkIntToScalar(desired_height) }; | 395 SkIntToScalar(desired_height) }; |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 450 SkBitmap thumbnail = GetThumbnailForRendererWithOptions( | 450 SkBitmap thumbnail = GetThumbnailForRendererWithOptions( |
| 451 web_contents->GetRenderViewHost(), options, &clip_result); | 451 web_contents->GetRenderViewHost(), options, &clip_result); |
| 452 // Failed to generate a thumbnail. Maybe the tab is in the background? | 452 // Failed to generate a thumbnail. Maybe the tab is in the background? |
| 453 if (thumbnail.isNull()) | 453 if (thumbnail.isNull()) |
| 454 return; | 454 return; |
| 455 | 455 |
| 456 UpdateThumbnail(web_contents, thumbnail, clip_result); | 456 UpdateThumbnail(web_contents, thumbnail, clip_result); |
| 457 } | 457 } |
| 458 | 458 |
| 459 void ThumbnailGenerator::UpdateThumbnail( | 459 void ThumbnailGenerator::UpdateThumbnail( |
| 460 WebContents* web_contents, SkBitmap thumbnail, | 460 WebContents* web_contents, const SkBitmap& thumbnail, |
| 461 const ThumbnailGenerator::ClipResult& clip_result) { | 461 const ThumbnailGenerator::ClipResult& clip_result) { |
| 462 | 462 |
| 463 Profile* profile = | 463 Profile* profile = |
| 464 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 464 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 465 history::TopSites* top_sites = profile->GetTopSites(); | 465 history::TopSites* top_sites = profile->GetTopSites(); |
| 466 if (!top_sites) | 466 if (!top_sites) |
| 467 return; | 467 return; |
| 468 | 468 |
| 469 // Compute the thumbnail score. | 469 // Compute the thumbnail score. |
| 470 ThumbnailScore score; | 470 ThumbnailScore score; |
| 471 score.at_top = | 471 score.at_top = |
| 472 (web_contents->GetRenderViewHost()->GetLastScrollOffset().y() == 0); | 472 (web_contents->GetRenderViewHost()->GetLastScrollOffset().y() == 0); |
| 473 score.boring_score = ThumbnailGenerator::CalculateBoringScore(&thumbnail); | 473 score.boring_score = ThumbnailGenerator::CalculateBoringScore(thumbnail); |
| 474 score.good_clipping = | 474 score.good_clipping = |
| 475 (clip_result == ThumbnailGenerator::kTallerThanWide || | 475 (clip_result == ThumbnailGenerator::kTallerThanWide || |
| 476 clip_result == ThumbnailGenerator::kNotClipped); | 476 clip_result == ThumbnailGenerator::kNotClipped); |
| 477 score.load_completed = (!load_interrupted_ && !web_contents->IsLoading()); | 477 score.load_completed = (!load_interrupted_ && !web_contents->IsLoading()); |
| 478 | 478 |
| 479 gfx::Image image(new SkBitmap(thumbnail)); | 479 gfx::Image image(new SkBitmap(thumbnail)); |
| 480 const GURL& url = web_contents->GetURL(); | 480 const GURL& url = web_contents->GetURL(); |
| 481 top_sites->SetPageThumbnail(url, &image, score); | 481 top_sites->SetPageThumbnail(url, &image, score); |
| 482 VLOG(1) << "Thumbnail taken for " << url << ": " << score.ToString(); | 482 VLOG(1) << "Thumbnail taken for " << url << ": " << score.ToString(); |
| 483 } | 483 } |
| (...skipping 29 matching lines...) Expand all Loading... |
| 513 | 513 |
| 514 void ThumbnailGenerator::DidStartLoading() { | 514 void ThumbnailGenerator::DidStartLoading() { |
| 515 load_interrupted_ = false; | 515 load_interrupted_ = false; |
| 516 } | 516 } |
| 517 | 517 |
| 518 void ThumbnailGenerator::StopNavigation() { | 518 void ThumbnailGenerator::StopNavigation() { |
| 519 // This function gets called when the page loading is interrupted by the | 519 // This function gets called when the page loading is interrupted by the |
| 520 // stop button. | 520 // stop button. |
| 521 load_interrupted_ = true; | 521 load_interrupted_ = true; |
| 522 } | 522 } |
| OLD | NEW |