Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / thumbnails / thumbnail_service_impl.cc
blobae061cc2b3baa5cfcf81446dcc57268831253c63
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 "base/time/time.h"
10 #include "chrome/browser/history/history_utils.h"
11 #include "chrome/browser/history/top_sites_factory.h"
12 #include "chrome/browser/thumbnails/content_based_thumbnailing_algorithm.h"
13 #include "chrome/browser/thumbnails/simple_thumbnail_crop.h"
14 #include "chrome/browser/thumbnails/thumbnailing_context.h"
15 #include "chrome/common/chrome_switches.h"
16 #include "components/search/search.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "url/gurl.h"
20 using content::BrowserThread;
22 namespace {
24 // The thumbnail size in DIP.
25 const int kThumbnailWidth = 212;
26 const int kThumbnailHeight = 142;
28 // True if thumbnail retargeting feature is enabled (Finch/flags).
29 bool IsThumbnailRetargetingEnabled() {
30 if (!search::IsInstantExtendedAPIEnabled())
31 return false;
33 return base::CommandLine::ForCurrentProcess()->HasSwitch(
34 switches::kEnableThumbnailRetargeting);
37 void AddForcedURLOnUIThread(scoped_refptr<history::TopSites> top_sites,
38 const GURL& url) {
39 DCHECK_CURRENTLY_ON(BrowserThread::UI);
41 if (top_sites)
42 top_sites->AddForcedURL(url, base::Time::Now());
45 } // namespace
47 namespace thumbnails {
49 ThumbnailServiceImpl::ThumbnailServiceImpl(Profile* profile)
50 : top_sites_(TopSitesFactory::GetForProfile(profile)),
51 use_thumbnail_retargeting_(IsThumbnailRetargetingEnabled()) {
54 ThumbnailServiceImpl::~ThumbnailServiceImpl() {
57 bool ThumbnailServiceImpl::SetPageThumbnail(const ThumbnailingContext& context,
58 const gfx::Image& thumbnail) {
59 scoped_refptr<history::TopSites> local_ptr(top_sites_);
60 if (!local_ptr)
61 return false;
63 return local_ptr->SetPageThumbnail(context.url, thumbnail, context.score);
66 bool ThumbnailServiceImpl::GetPageThumbnail(
67 const GURL& url,
68 bool prefix_match,
69 scoped_refptr<base::RefCountedMemory>* bytes) {
70 scoped_refptr<history::TopSites> local_ptr(top_sites_);
71 if (!local_ptr)
72 return false;
74 return local_ptr->GetPageThumbnail(url, prefix_match, bytes);
77 void ThumbnailServiceImpl::AddForcedURL(const GURL& url) {
78 scoped_refptr<history::TopSites> local_ptr(top_sites_);
79 if (!local_ptr)
80 return;
82 // Adding
83 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
84 base::Bind(AddForcedURLOnUIThread, local_ptr, url));
87 ThumbnailingAlgorithm* ThumbnailServiceImpl::GetThumbnailingAlgorithm()
88 const {
89 const gfx::Size thumbnail_size(kThumbnailWidth, kThumbnailHeight);
90 if (use_thumbnail_retargeting_)
91 return new ContentBasedThumbnailingAlgorithm(thumbnail_size);
92 return new SimpleThumbnailCrop(thumbnail_size);
95 bool ThumbnailServiceImpl::ShouldAcquirePageThumbnail(const GURL& url) {
96 scoped_refptr<history::TopSites> local_ptr(top_sites_);
98 if (!local_ptr)
99 return false;
101 // Skip if the given URL is not appropriate for history.
102 if (!CanAddURLToHistory(url))
103 return false;
104 // Skip if the top sites list is full, and the URL is not known.
105 if (local_ptr->IsNonForcedFull() && !local_ptr->IsKnownURL(url))
106 return false;
107 // Skip if we don't have to udpate the existing thumbnail.
108 ThumbnailScore current_score;
109 if (local_ptr->GetPageThumbnailScore(url, &current_score) &&
110 !current_score.ShouldConsiderUpdating())
111 return false;
112 // Skip if we don't have to udpate the temporary thumbnail (i.e. the one
113 // not yet saved).
114 ThumbnailScore temporary_score;
115 if (local_ptr->GetTemporaryPageThumbnailScore(url, &temporary_score) &&
116 !temporary_score.ShouldConsiderUpdating())
117 return false;
119 return true;
122 void ThumbnailServiceImpl::ShutdownOnUIThread() {
123 // Since each call uses its own scoped_refptr, we can just clear the reference
124 // here by assigning null. If another call is completed, it added its own
125 // reference.
126 top_sites_ = NULL;
129 } // namespace thumbnails