- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / thumbnails / thumbnail_service_impl.cc
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 #include "url/gurl.h"
16
17 namespace {
18
19 // The thumbnail size in DIP.
20 const int kThumbnailWidth = 212;
21 const int kThumbnailHeight = 132;
22
23 // True if thumbnail retargeting feature is enabled (Finch/flags).
24 bool IsThumbnailRetargetingEnabled() {
25   if (!chrome::IsInstantExtendedAPIEnabled())
26     return false;
27
28   return CommandLine::ForCurrentProcess()->HasSwitch(
29       switches::kEnableThumbnailRetargeting);
30 }
31
32 }  // namespace
33
34 namespace thumbnails {
35
36 ThumbnailServiceImpl::ThumbnailServiceImpl(Profile* profile)
37     : top_sites_(profile->GetTopSites()),
38       use_thumbnail_retargeting_(IsThumbnailRetargetingEnabled()) {
39 }
40
41 ThumbnailServiceImpl::~ThumbnailServiceImpl() {
42 }
43
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)
48     return false;
49
50   return local_ptr->SetPageThumbnail(context.url, thumbnail, context.score);
51 }
52
53 bool ThumbnailServiceImpl::GetPageThumbnail(
54     const GURL& url,
55     bool prefix_match,
56     scoped_refptr<base::RefCountedMemory>* bytes) {
57   scoped_refptr<history::TopSites> local_ptr(top_sites_);
58   if (local_ptr.get() == NULL)
59     return false;
60
61   return local_ptr->GetPageThumbnail(url, prefix_match, bytes);
62 }
63
64 ThumbnailingAlgorithm* ThumbnailServiceImpl::GetThumbnailingAlgorithm()
65     const {
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);
70 }
71
72 bool ThumbnailServiceImpl::ShouldAcquirePageThumbnail(const GURL& url) {
73   scoped_refptr<history::TopSites> local_ptr(top_sites_);
74
75   if (local_ptr.get() == NULL)
76     return false;
77
78   // Skip if the given URL is not appropriate for history.
79   if (!HistoryService::CanAddURL(url))
80     return false;
81   // Skip if the top sites list is full, and the URL is not known.
82   if (local_ptr->IsFull() && !local_ptr->IsKnownURL(url))
83     return false;
84   // Skip if we don't have to udpate the existing thumbnail.
85   ThumbnailScore current_score;
86   if (local_ptr->GetPageThumbnailScore(url, &current_score) &&
87       !current_score.ShouldConsiderUpdating())
88     return false;
89   // Skip if we don't have to udpate the temporary thumbnail (i.e. the one
90   // not yet saved).
91   ThumbnailScore temporary_score;
92   if (local_ptr->GetTemporaryPageThumbnailScore(url, &temporary_score) &&
93       !temporary_score.ShouldConsiderUpdating())
94     return false;
95
96   return true;
97 }
98
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
102   // reference.
103   top_sites_ = NULL;
104 }
105
106 }  // namespace thumbnails