Home | History | Annotate | Download | only in thumbnails
      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 "base/time/time.h"
     10 #include "chrome/browser/history/history_service.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 #include "components/search/search.h"
     16 #include "url/gurl.h"
     17 
     18 using content::BrowserThread;
     19 
     20 namespace {
     21 
     22 // The thumbnail size in DIP.
     23 const int kThumbnailWidth = 212;
     24 const int kThumbnailHeight = 142;
     25 
     26 // True if thumbnail retargeting feature is enabled (Finch/flags).
     27 bool IsThumbnailRetargetingEnabled() {
     28   if (!chrome::IsInstantExtendedAPIEnabled())
     29     return false;
     30 
     31   return CommandLine::ForCurrentProcess()->HasSwitch(
     32       switches::kEnableThumbnailRetargeting);
     33 }
     34 
     35 void AddForcedURLOnUIThread(scoped_refptr<history::TopSites> top_sites,
     36                             const GURL& url) {
     37   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     38 
     39   if (top_sites.get() != NULL)
     40     top_sites->AddForcedURL(url, base::Time::Now());
     41 }
     42 
     43 }  // namespace
     44 
     45 namespace thumbnails {
     46 
     47 ThumbnailServiceImpl::ThumbnailServiceImpl(Profile* profile)
     48     : top_sites_(profile->GetTopSites()),
     49       use_thumbnail_retargeting_(IsThumbnailRetargetingEnabled()) {
     50 }
     51 
     52 ThumbnailServiceImpl::~ThumbnailServiceImpl() {
     53 }
     54 
     55 bool ThumbnailServiceImpl::SetPageThumbnail(const ThumbnailingContext& context,
     56                                             const gfx::Image& thumbnail) {
     57   scoped_refptr<history::TopSites> local_ptr(top_sites_);
     58   if (local_ptr.get() == NULL)
     59     return false;
     60 
     61   return local_ptr->SetPageThumbnail(context.url, thumbnail, context.score);
     62 }
     63 
     64 bool ThumbnailServiceImpl::GetPageThumbnail(
     65     const GURL& url,
     66     bool prefix_match,
     67     scoped_refptr<base::RefCountedMemory>* bytes) {
     68   scoped_refptr<history::TopSites> local_ptr(top_sites_);
     69   if (local_ptr.get() == NULL)
     70     return false;
     71 
     72   return local_ptr->GetPageThumbnail(url, prefix_match, bytes);
     73 }
     74 
     75 void ThumbnailServiceImpl::AddForcedURL(const GURL& url) {
     76   scoped_refptr<history::TopSites> local_ptr(top_sites_);
     77   if (local_ptr.get() == NULL)
     78     return;
     79 
     80   // Adding
     81   BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
     82                           base::Bind(AddForcedURLOnUIThread, local_ptr, url));
     83 }
     84 
     85 ThumbnailingAlgorithm* ThumbnailServiceImpl::GetThumbnailingAlgorithm()
     86     const {
     87   const gfx::Size thumbnail_size(kThumbnailWidth, kThumbnailHeight);
     88   if (use_thumbnail_retargeting_)
     89     return new ContentBasedThumbnailingAlgorithm(thumbnail_size);
     90   return new SimpleThumbnailCrop(thumbnail_size);
     91 }
     92 
     93 bool ThumbnailServiceImpl::ShouldAcquirePageThumbnail(const GURL& url) {
     94   scoped_refptr<history::TopSites> local_ptr(top_sites_);
     95 
     96   if (local_ptr.get() == NULL)
     97     return false;
     98 
     99   // Skip if the given URL is not appropriate for history.
    100   if (!HistoryService::CanAddURL(url))
    101     return false;
    102   // Skip if the top sites list is full, and the URL is not known.
    103   if (local_ptr->IsNonForcedFull() && !local_ptr->IsKnownURL(url))
    104     return false;
    105   // Skip if we don't have to udpate the existing thumbnail.
    106   ThumbnailScore current_score;
    107   if (local_ptr->GetPageThumbnailScore(url, &current_score) &&
    108       !current_score.ShouldConsiderUpdating())
    109     return false;
    110   // Skip if we don't have to udpate the temporary thumbnail (i.e. the one
    111   // not yet saved).
    112   ThumbnailScore temporary_score;
    113   if (local_ptr->GetTemporaryPageThumbnailScore(url, &temporary_score) &&
    114       !temporary_score.ShouldConsiderUpdating())
    115     return false;
    116 
    117   return true;
    118 }
    119 
    120 void ThumbnailServiceImpl::ShutdownOnUIThread() {
    121   // Since each call uses its own scoped_refptr, we can just clear the reference
    122   // here by assigning null. If another call is completed, it added its own
    123   // reference.
    124   top_sites_ = NULL;
    125 }
    126 
    127 }  // namespace thumbnails
    128