Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(263)

Side by Side Diff: chrome/browser/ui/webui/ntp/thumbnail_list_source.cc

Issue 24632002: Implementing chrome://thumbnails page to view URLs in TopSites and cached thumbnails. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Trying to fix Linux compile by reordering initializing order of weak_ptr_factory_. Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/ui/webui/ntp/thumbnail_list_source.h ('k') | chrome/chrome_browser_ui.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 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.
4
5 #include "chrome/browser/ui/webui/ntp/thumbnail_list_source.h"
6
7 #include <string>
8
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"
22
23 namespace {
24
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";
32
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 out->push_back("<div><ul>\n");
43 for (size_t i = 0; i < mvurl_list.size(); ++i) {
44 const history::MostVisitedURL& mvurl = mvurl_list[i];
45 bool has_thumbnail = !base64_encoded_pngs[i].empty();
46 if (has_thumbnail == want_thumbnails) {
47 out->push_back("<li>\n");
48 out->push_back(net::EscapeForHTML(mvurl.url.spec()) + "\n");
49 if (want_thumbnails) {
50 out->push_back("<div><img class=\"thumb\" "
51 "src=\"data:image/png;base64," +
52 base64_encoded_pngs[i] + "\"/></div>\n");
53 }
54 if (!mvurl.redirects.empty()) {
55 out->push_back("<ul>\n");
56 history::RedirectList::const_iterator jt;
57 for (jt = mvurl.redirects.begin();
58 jt != mvurl.redirects.end(); ++jt) {
59 out->push_back("<li>" + net::EscapeForHTML(jt->spec()) + "</li>\n");
60 }
61 out->push_back("</ul>\n");
62 }
63 out->push_back("</li>\n");
64 }
65 }
66 out->push_back("</ul></div>\n");
67 }
68
69 } // namespace
70
71 ThumbnailListSource::ThumbnailListSource(Profile* profile)
72 : thumbnail_service_(ThumbnailServiceFactory::GetForProfile(profile)),
73 profile_(profile),
74 weak_ptr_factory_(this) {
75 }
76
77 ThumbnailListSource::~ThumbnailListSource() {
78 }
79
80 std::string ThumbnailListSource::GetSource() const {
81 return chrome::kChromeUIThumbnailListHost;
82 }
83
84 void ThumbnailListSource::StartDataRequest(
85 const std::string& path,
86 int render_process_id,
87 int render_view_id,
88 const content::URLDataSource::GotDataCallback& callback) {
89 profile_->GetTopSites()->GetMostVisitedURLs(
90 base::Bind(&ThumbnailListSource::OnMostVisitedURLsAvailable,
91 weak_ptr_factory_.GetWeakPtr(),
92 callback));
93 }
94
95 std::string ThumbnailListSource::GetMimeType(const std::string& path) const {
96 return "text/html";
97 }
98
99 base::MessageLoop* ThumbnailListSource::MessageLoopForRequestPath(
100 const std::string& path) const {
101 // TopSites can be accessed from the IO thread.
102 return thumbnail_service_.get() ?
103 NULL : content::URLDataSource::MessageLoopForRequestPath(path);
104 }
105
106 bool ThumbnailListSource::ShouldServiceRequest(
107 const net::URLRequest* request) const {
108 if (request->url().SchemeIs(chrome::kChromeSearchScheme))
109 return InstantIOContext::ShouldServiceRequest(request);
110 return URLDataSource::ShouldServiceRequest(request);
111 }
112
113 void ThumbnailListSource::OnMostVisitedURLsAvailable(
114 const content::URLDataSource::GotDataCallback& callback,
115 const history::MostVisitedURLList& mvurl_list) {
116 const size_t num_mv = mvurl_list.size();
117 size_t num_mv_with_thumb = 0;
118
119 // Encode all available thumbnails and store into |base64_encoded_pngs|.
120 std::vector<std::string> base64_encoded_pngs(num_mv);
121 for (size_t i = 0; i < num_mv; ++i) {
122 scoped_refptr<base::RefCountedMemory> data;
123 if (thumbnail_service_->GetPageThumbnail(mvurl_list[i].url, false, &data)) {
124 std::string data_str;
125 data_str.assign(reinterpret_cast<const char*>(data->front()),
126 data->size());
127 base::Base64Encode(data_str, &base64_encoded_pngs[i]);
128 ++num_mv_with_thumb;
129 }
130 }
131
132 // Render HTML to embed URLs and thumbnails.
133 std::vector<std::string> out;
134 out.push_back(html_header);
135 out.push_back(html_body);
136 if (num_mv_with_thumb > 0) {
137 out.push_back("<h2>TopSites URLs with Thumbnails</h2>\n");
138 RenderMostVisitedURLList(mvurl_list, base64_encoded_pngs, true, &out);
139 }
140 if (num_mv_with_thumb < num_mv) {
141 out.push_back("<h2>TopSites URLs without Thumbnails</h2>\n");
142 RenderMostVisitedURLList(mvurl_list, base64_encoded_pngs, false, &out);
143 }
144 out.push_back(html_footer);
145
146 std::string out_html = JoinString(out, "");
147 callback.Run(base::RefCountedString::TakeString(&out_html));
148 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/ntp/thumbnail_list_source.h ('k') | chrome/chrome_browser_ui.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698