Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / webui / ntp / thumbnail_list_source.cc
blob69a7dfd85556cf8cef296c03a1e67a34741f8394
1 // Copyright 2013 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/ui/webui/ntp/thumbnail_list_source.h"
7 #include <string>
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.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 "net/base/escape.h"
21 #include "net/url_request/url_request.h"
23 namespace {
25 const char* html_header =
26 "<!DOCTYPE HTML>\n<html>\n<head>\n<title>TopSites Thumbnails</title>\n"
27 "<meta charset=\"utf-8\">\n"
28 "<style type=\"text/css\">\nimg.thumb {border: 1px solid black;}\n"
29 "li {white-space: nowrap;}\n</style>\n";
30 const char* html_body = "</head>\n<body>\n";
31 const char* html_footer = "</body>\n</html>\n";
33 // If |want_thumbnails| == true, then renders elements in |mvurl_list| that have
34 // thumbnails, with their thumbnails. Otherwise renders elements in |mvurl_list|
35 // that have no thumbnails.
36 void RenderMostVisitedURLList(
37 const history::MostVisitedURLList& mvurl_list,
38 const std::vector<std::string>& base64_encoded_pngs,
39 bool want_thumbnails,
40 std::vector<std::string>* out) {
41 DCHECK_EQ(mvurl_list.size(), base64_encoded_pngs.size());
42 bool doing_forced_urls = true;
43 out->push_back("<div><b>Forced URLs:</b></div>\n"
44 "<div><ul>\n");
45 for (size_t i = 0; i < mvurl_list.size(); ++i) {
46 const history::MostVisitedURL& mvurl = mvurl_list[i];
47 if (doing_forced_urls && mvurl.last_forced_time.is_null()) {
48 out->push_back("</ul></div>\n"
49 "<div><b>Non-forced URLs:</b></div>\n"
50 "<div><ul>\n");
51 doing_forced_urls = false;
53 bool has_thumbnail = !base64_encoded_pngs[i].empty();
54 if (has_thumbnail == want_thumbnails) {
55 out->push_back("<li>\n");
56 out->push_back(net::EscapeForHTML(mvurl.url.spec()) + "\n");
57 if (want_thumbnails) {
58 out->push_back("<div><img class=\"thumb\" "
59 "src=\"data:image/png;base64," +
60 base64_encoded_pngs[i] + "\"/></div>\n");
62 if (!mvurl.redirects.empty()) {
63 out->push_back("<ul>\n");
64 history::RedirectList::const_iterator jt;
65 for (jt = mvurl.redirects.begin();
66 jt != mvurl.redirects.end(); ++jt) {
67 out->push_back("<li>" + net::EscapeForHTML(jt->spec()) + "</li>\n");
69 out->push_back("</ul>\n");
71 out->push_back("</li>\n");
74 out->push_back("</ul></div>\n");
77 } // namespace
79 ThumbnailListSource::ThumbnailListSource(Profile* profile)
80 : thumbnail_service_(ThumbnailServiceFactory::GetForProfile(profile)),
81 profile_(profile),
82 weak_ptr_factory_(this) {
85 ThumbnailListSource::~ThumbnailListSource() {
88 std::string ThumbnailListSource::GetSource() const {
89 return chrome::kChromeUIThumbnailListHost;
92 void ThumbnailListSource::StartDataRequest(
93 const std::string& path,
94 int render_process_id,
95 int render_frame_id,
96 const content::URLDataSource::GotDataCallback& callback) {
97 profile_->GetTopSites()->GetMostVisitedURLs(
98 base::Bind(&ThumbnailListSource::OnMostVisitedURLsAvailable,
99 weak_ptr_factory_.GetWeakPtr(),
100 callback), true);
103 std::string ThumbnailListSource::GetMimeType(const std::string& path) const {
104 return "text/html";
107 base::MessageLoop* ThumbnailListSource::MessageLoopForRequestPath(
108 const std::string& path) const {
109 // TopSites can be accessed from the IO thread.
110 return thumbnail_service_.get() ?
111 NULL : content::URLDataSource::MessageLoopForRequestPath(path);
114 bool ThumbnailListSource::ShouldServiceRequest(
115 const net::URLRequest* request) const {
116 if (request->url().SchemeIs(chrome::kChromeSearchScheme))
117 return InstantIOContext::ShouldServiceRequest(request);
118 return URLDataSource::ShouldServiceRequest(request);
121 void ThumbnailListSource::OnMostVisitedURLsAvailable(
122 const content::URLDataSource::GotDataCallback& callback,
123 const history::MostVisitedURLList& mvurl_list) {
124 const size_t num_mv = mvurl_list.size();
125 size_t num_mv_with_thumb = 0;
127 // Encode all available thumbnails and store into |base64_encoded_pngs|.
128 std::vector<std::string> base64_encoded_pngs(num_mv);
129 for (size_t i = 0; i < num_mv; ++i) {
130 scoped_refptr<base::RefCountedMemory> data;
131 if (thumbnail_service_->GetPageThumbnail(mvurl_list[i].url, false, &data)) {
132 std::string data_str;
133 data_str.assign(reinterpret_cast<const char*>(data->front()),
134 data->size());
135 base::Base64Encode(data_str, &base64_encoded_pngs[i]);
136 ++num_mv_with_thumb;
140 // Render HTML to embed URLs and thumbnails.
141 std::vector<std::string> out;
142 out.push_back(html_header);
143 out.push_back(html_body);
144 if (num_mv_with_thumb > 0) {
145 out.push_back("<h2>TopSites URLs with Thumbnails</h2>\n");
146 RenderMostVisitedURLList(mvurl_list, base64_encoded_pngs, true, &out);
148 if (num_mv_with_thumb < num_mv) {
149 out.push_back("<h2>TopSites URLs without Thumbnails</h2>\n");
150 RenderMostVisitedURLList(mvurl_list, base64_encoded_pngs, false, &out);
152 out.push_back(html_footer);
154 std::string out_html = JoinString(out, "");
155 callback.Run(base::RefCountedString::TakeString(&out_html));