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