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.
5 #include "chrome/browser/thumbnails/thumbnail_service_impl.h"
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"
19 // The thumbnail size in DIP.
20 const int kThumbnailWidth = 212;
21 const int kThumbnailHeight = 132;
23 // True if thumbnail retargeting feature is enabled (Finch/flags).
24 bool IsThumbnailRetargetingEnabled() {
25 if (!chrome::IsInstantExtendedAPIEnabled())
28 return CommandLine::ForCurrentProcess()->HasSwitch(
29 switches::kEnableThumbnailRetargeting);
34 namespace thumbnails {
36 ThumbnailServiceImpl::ThumbnailServiceImpl(Profile* profile)
37 : top_sites_(profile->GetTopSites()),
38 use_thumbnail_retargeting_(IsThumbnailRetargetingEnabled()) {
41 ThumbnailServiceImpl::~ThumbnailServiceImpl() {
44 bool ThumbnailServiceImpl::SetPageThumbnail(const ThumbnailingContext& context,
45 const gfx::Image& thumbnail) {
46 scoped_refptr<history::TopSites> local_ptr(top_sites_);
47 if (local_ptr.get() == NULL)
50 return local_ptr->SetPageThumbnail(context.url, thumbnail, context.score);
53 bool ThumbnailServiceImpl::GetPageThumbnail(
56 scoped_refptr<base::RefCountedMemory>* bytes) {
57 scoped_refptr<history::TopSites> local_ptr(top_sites_);
58 if (local_ptr.get() == NULL)
61 return local_ptr->GetPageThumbnail(url, prefix_match, bytes);
64 ThumbnailingAlgorithm* ThumbnailServiceImpl::GetThumbnailingAlgorithm()
66 const gfx::Size thumbnail_size(kThumbnailWidth, kThumbnailHeight);
67 if (use_thumbnail_retargeting_)
68 return new ContentBasedThumbnailingAlgorithm(thumbnail_size);
69 return new SimpleThumbnailCrop(thumbnail_size);
72 bool ThumbnailServiceImpl::ShouldAcquirePageThumbnail(const GURL& url) {
73 scoped_refptr<history::TopSites> local_ptr(top_sites_);
75 if (local_ptr.get() == NULL)
78 // Skip if the given URL is not appropriate for history.
79 if (!HistoryService::CanAddURL(url))
81 // Skip if the top sites list is full, and the URL is not known.
82 if (local_ptr->IsFull() && !local_ptr->IsKnownURL(url))
84 // Skip if we don't have to udpate the existing thumbnail.
85 ThumbnailScore current_score;
86 if (local_ptr->GetPageThumbnailScore(url, ¤t_score) &&
87 !current_score.ShouldConsiderUpdating())
89 // Skip if we don't have to udpate the temporary thumbnail (i.e. the one
91 ThumbnailScore temporary_score;
92 if (local_ptr->GetTemporaryPageThumbnailScore(url, &temporary_score) &&
93 !temporary_score.ShouldConsiderUpdating())
99 void ThumbnailServiceImpl::ShutdownOnUIThread() {
100 // Since each call uses its own scoped_refptr, we can just clear the reference
101 // here by assigning null. If another call is completed, it added its own
106 } // namespace thumbnails