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. 4 5#include "chrome/browser/thumbnails/thumbnail_service_impl.h" 6 7#include "base/command_line.h" 8#include "base/memory/ref_counted_memory.h" 9#include "chrome/browser/history/history_service.h" 10#include "chrome/browser/search/search.h" 11#include "chrome/browser/thumbnails/content_based_thumbnailing_algorithm.h" 12#include "chrome/browser/thumbnails/simple_thumbnail_crop.h" 13#include "chrome/browser/thumbnails/thumbnailing_context.h" 14#include "chrome/common/chrome_switches.h" 15 16namespace { 17 18// The thumbnail size in DIP. 19const int kThumbnailWidth = 212; 20const int kThumbnailHeight = 132; 21 22// True if thumbnail retargeting feature is enabled (Finch/flags). 23bool IsThumbnailRetargetingEnabled() { 24 if (!chrome::IsInstantExtendedAPIEnabled()) 25 return false; 26 27 return CommandLine::ForCurrentProcess()->HasSwitch( 28 switches::kEnableThumbnailRetargeting); 29} 30 31} 32 33namespace thumbnails { 34 35ThumbnailServiceImpl::ThumbnailServiceImpl(Profile* profile) 36 : top_sites_(profile->GetTopSites()), 37 use_thumbnail_retargeting_(IsThumbnailRetargetingEnabled()){ 38} 39 40ThumbnailServiceImpl::~ThumbnailServiceImpl() { 41} 42 43bool ThumbnailServiceImpl::SetPageThumbnail(const ThumbnailingContext& context, 44 const gfx::Image& thumbnail) { 45 scoped_refptr<history::TopSites> local_ptr(top_sites_); 46 if (local_ptr.get() == NULL) 47 return false; 48 49 return local_ptr->SetPageThumbnail(context.url, thumbnail, context.score); 50} 51 52bool ThumbnailServiceImpl::GetPageThumbnail( 53 const GURL& url, 54 scoped_refptr<base::RefCountedMemory>* bytes) { 55 scoped_refptr<history::TopSites> local_ptr(top_sites_); 56 if (local_ptr.get() == NULL) 57 return false; 58 59 return local_ptr->GetPageThumbnail(url, bytes); 60} 61 62ThumbnailingAlgorithm* ThumbnailServiceImpl::GetThumbnailingAlgorithm() 63 const { 64 const gfx::Size thumbnail_size(kThumbnailWidth, kThumbnailHeight); 65 if (use_thumbnail_retargeting_) 66 return new ContentBasedThumbnailingAlgorithm(thumbnail_size); 67 return new SimpleThumbnailCrop(thumbnail_size); 68} 69 70bool ThumbnailServiceImpl::ShouldAcquirePageThumbnail(const GURL& url) { 71 scoped_refptr<history::TopSites> local_ptr(top_sites_); 72 73 if (local_ptr.get() == NULL) 74 return false; 75 76 // Skip if the given URL is not appropriate for history. 77 if (!HistoryService::CanAddURL(url)) 78 return false; 79 // Skip if the top sites list is full, and the URL is not known. 80 if (local_ptr->IsFull() && !local_ptr->IsKnownURL(url)) 81 return false; 82 // Skip if we don't have to udpate the existing thumbnail. 83 ThumbnailScore current_score; 84 if (local_ptr->GetPageThumbnailScore(url, ¤t_score) && 85 !current_score.ShouldConsiderUpdating()) 86 return false; 87 // Skip if we don't have to udpate the temporary thumbnail (i.e. the one 88 // not yet saved). 89 ThumbnailScore temporary_score; 90 if (local_ptr->GetTemporaryPageThumbnailScore(url, &temporary_score) && 91 !temporary_score.ShouldConsiderUpdating()) 92 return false; 93 94 return true; 95} 96 97void ThumbnailServiceImpl::ShutdownOnUIThread() { 98 // Since each call uses its own scoped_refptr, we can just clear the reference 99 // here by assigning null. If another call is completed, it added its own 100 // reference. 101 top_sites_ = NULL; 102} 103 104} // namespace thumbnails 105