1 // Copyright 2014 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_list_source.h"
9 #include "base/base64.h"
10 #include "base/bind.h"
11 #include "base/logging.h"
12 #include "base/memory/ref_counted_memory.h"
13 #include "base/strings/string_util.h"
14 #include "chrome/browser/history/top_sites_factory.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/search/instant_io_context.h"
17 #include "chrome/browser/thumbnails/thumbnail_service.h"
18 #include "chrome/browser/thumbnails/thumbnail_service_factory.h"
19 #include "chrome/common/url_constants.h"
20 #include "components/history/core/browser/top_sites.h"
21 #include "content/public/browser/browser_thread.h"
22 #include "net/base/escape.h"
23 #include "net/url_request/url_request.h"
25 using content::BrowserThread
;
29 const char kHtmlHeader
[] =
30 "<!DOCTYPE html>\n<html>\n<head>\n<title>TopSites Thumbnails</title>\n"
31 "<meta charset=\"utf-8\">\n"
32 "<style type=\"text/css\">\nimg.thumb {border: 1px solid black;}\n"
33 "li {white-space: nowrap;}\n</style>\n";
34 const char kHtmlBody
[] = "</head>\n<body>\n";
35 const char kHtmlFooter
[] = "</body>\n</html>\n";
37 // If |want_thumbnails| == true, then renders elements in |mvurl_list| that have
38 // thumbnails, with their thumbnails. Otherwise renders elements in |mvurl_list|
39 // that have no thumbnails.
40 void RenderMostVisitedURLList(
41 const history::MostVisitedURLList
& mvurl_list
,
42 const std::vector
<std::string
>& base64_encoded_pngs
,
44 std::vector
<std::string
>* out
) {
45 DCHECK_EQ(mvurl_list
.size(), base64_encoded_pngs
.size());
46 bool doing_forced_urls
= true;
47 out
->push_back("<div><b>Forced URLs:</b></div>\n"
49 for (size_t i
= 0; i
< mvurl_list
.size(); ++i
) {
50 const history::MostVisitedURL
& mvurl
= mvurl_list
[i
];
51 if (doing_forced_urls
&& mvurl
.last_forced_time
.is_null()) {
52 out
->push_back("</ul></div>\n"
53 "<div><b>Non-forced URLs:</b></div>\n"
55 doing_forced_urls
= false;
57 bool has_thumbnail
= !base64_encoded_pngs
[i
].empty();
58 if (has_thumbnail
== want_thumbnails
) {
59 out
->push_back("<li>\n");
60 out
->push_back(net::EscapeForHTML(mvurl
.url
.spec()) + "\n");
61 if (want_thumbnails
) {
62 out
->push_back("<div><img class=\"thumb\" "
63 "src=\"data:image/png;base64," +
64 base64_encoded_pngs
[i
] + "\"/></div>\n");
66 if (!mvurl
.redirects
.empty()) {
67 out
->push_back("<ul>\n");
68 history::RedirectList::const_iterator jt
;
69 for (jt
= mvurl
.redirects
.begin();
70 jt
!= mvurl
.redirects
.end(); ++jt
) {
71 out
->push_back("<li>" + net::EscapeForHTML(jt
->spec()) + "</li>\n");
73 out
->push_back("</ul>\n");
75 out
->push_back("</li>\n");
78 out
->push_back("</ul></div>\n");
83 ThumbnailListSource::ThumbnailListSource(Profile
* profile
)
84 : thumbnail_service_(ThumbnailServiceFactory::GetForProfile(profile
)),
85 top_sites_(TopSitesFactory::GetForProfile(profile
)),
86 weak_ptr_factory_(this) {
89 ThumbnailListSource::~ThumbnailListSource() {
92 std::string
ThumbnailListSource::GetSource() const {
93 return chrome::kChromeUIThumbnailListHost
;
96 void ThumbnailListSource::StartDataRequest(
97 const std::string
& path
,
98 int render_process_id
,
100 const content::URLDataSource::GotDataCallback
& callback
) {
101 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
107 top_sites_
->GetMostVisitedURLs(
108 base::Bind(&ThumbnailListSource::OnMostVisitedURLsAvailable
,
109 weak_ptr_factory_
.GetWeakPtr(), callback
),
113 std::string
ThumbnailListSource::GetMimeType(const std::string
& path
) const {
117 base::MessageLoop
* ThumbnailListSource::MessageLoopForRequestPath(
118 const std::string
& path
) const {
119 // TopSites can be accessed from the IO thread.
120 return thumbnail_service_
.get() ?
121 NULL
: content::URLDataSource::MessageLoopForRequestPath(path
);
124 bool ThumbnailListSource::ShouldServiceRequest(
125 const net::URLRequest
* request
) const {
126 if (request
->url().SchemeIs(chrome::kChromeSearchScheme
))
127 return InstantIOContext::ShouldServiceRequest(request
);
128 return URLDataSource::ShouldServiceRequest(request
);
131 bool ThumbnailListSource::ShouldReplaceExistingSource() const {
135 void ThumbnailListSource::OnMostVisitedURLsAvailable(
136 const content::URLDataSource::GotDataCallback
& callback
,
137 const history::MostVisitedURLList
& mvurl_list
) {
138 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
139 const size_t num_mv
= mvurl_list
.size();
140 size_t num_mv_with_thumb
= 0;
142 // Encode all available thumbnails and store into |base64_encoded_pngs|.
143 std::vector
<std::string
> base64_encoded_pngs(num_mv
);
144 for (size_t i
= 0; i
< num_mv
; ++i
) {
145 scoped_refptr
<base::RefCountedMemory
> data
;
146 if (thumbnail_service_
->GetPageThumbnail(mvurl_list
[i
].url
, false, &data
)) {
147 base::Base64Encode(std::string(data
->front_as
<char>(), data
->size()),
148 &base64_encoded_pngs
[i
]);
153 // Render HTML to embed URLs and thumbnails.
154 std::vector
<std::string
> out
;
155 out
.push_back(kHtmlHeader
);
156 out
.push_back(kHtmlBody
);
157 if (num_mv_with_thumb
> 0) {
158 out
.push_back("<h2>TopSites URLs with Thumbnails</h2>\n");
159 RenderMostVisitedURLList(mvurl_list
, base64_encoded_pngs
, true, &out
);
161 if (num_mv_with_thumb
< num_mv
) {
162 out
.push_back("<h2>TopSites URLs without Thumbnails</h2>\n");
163 RenderMostVisitedURLList(mvurl_list
, base64_encoded_pngs
, false, &out
);
165 out
.push_back(kHtmlFooter
);
167 std::string out_html
= base::JoinString(out
, base::StringPiece());
168 callback
.Run(base::RefCountedString::TakeString(&out_html
));