Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 #include <vector> | |
| 9 | |
| 10 #include "base/base64.h" | |
| 11 #include "base/bind.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 struct MostVisitedURLWithImage{ | |
| 34 const history::MostVisitedURL* mv_url; | |
| 35 std::string base64_encoded_png; | |
| 36 | |
| 37 MostVisitedURLWithImage() : mv_url(NULL) {} | |
| 38 bool has_thumbnail() const { return !base64_encoded_png.empty(); } | |
| 39 }; | |
| 40 | |
| 41 // If |show_thumbnail| == true, then render elements in |mv_list| that have | |
|
beaudoin
2013/09/25 21:36:37
NIT: that have _a_ thumbnail
huangs
2013/09/25 21:49:26
Done.
| |
| 42 // thumbnail (along with their thumbnails). Otherwise render elements in | |
| 43 // |mv_list| that do not have a thumbnail. | |
| 44 void RenderMostVisitedURLList( | |
| 45 const std::vector<MostVisitedURLWithImage>& mv_list, | |
| 46 bool show_thumbnail, | |
| 47 std::vector<std::string>* out) { | |
| 48 out->push_back("<div><ul>\n"); | |
| 49 std::vector<MostVisitedURLWithImage>::const_iterator it; | |
| 50 for (it = mv_list.begin(); it != mv_list.end(); ++it) { | |
| 51 if (it->has_thumbnail() == show_thumbnail) { | |
| 52 const GURL& url(it->mv_url->url); | |
| 53 out->push_back("<li>\n"); | |
| 54 out->push_back(net::EscapeForHTML(url.spec()) + "\n"); | |
| 55 if (show_thumbnail) { | |
| 56 out->push_back("<div><img class=\"thumb\" " | |
| 57 "src=\"data:image/png;base64," + | |
| 58 it->base64_encoded_png + "\"/></div>\n"); | |
| 59 } | |
| 60 if (!it->mv_url->redirects.empty()) { | |
| 61 out->push_back("<ul>\n"); | |
| 62 history::RedirectList::const_iterator jt; | |
| 63 for (jt = it->mv_url->redirects.begin(); | |
| 64 jt != it->mv_url->redirects.end(); ++jt) { | |
| 65 out->push_back("<li>" + net::EscapeForHTML(jt->spec()) + "</li>\n"); | |
| 66 } | |
| 67 out->push_back("</ul>\n"); | |
| 68 } | |
| 69 out->push_back("</li>\n"); | |
| 70 } | |
| 71 } | |
| 72 out->push_back("</ul></div>\n"); | |
| 73 } | |
| 74 | |
| 75 } // namespace | |
| 76 | |
| 77 ThumbnailListSource::ThumbnailListSource(Profile* profile) | |
| 78 : thumbnail_service_(ThumbnailServiceFactory::GetForProfile(profile)), | |
| 79 profile_(profile) { | |
| 80 } | |
| 81 | |
| 82 ThumbnailListSource::~ThumbnailListSource() { | |
| 83 } | |
| 84 | |
| 85 std::string ThumbnailListSource::GetSource() const { | |
| 86 return chrome::kChromeUIThumbnailListHost; | |
| 87 } | |
| 88 | |
| 89 void ThumbnailListSource::StartDataRequest( | |
| 90 const std::string& path, | |
| 91 int render_process_id, | |
| 92 int render_view_id, | |
| 93 const content::URLDataSource::GotDataCallback& callback) { | |
| 94 profile_->GetTopSites()->GetMostVisitedURLs( | |
| 95 base::Bind(&ThumbnailListSource::OnMostVisitedURLsAvailable, | |
| 96 base::Unretained(this), | |
| 97 callback)); | |
| 98 } | |
| 99 | |
| 100 std::string ThumbnailListSource::GetMimeType(const std::string& path) const { | |
| 101 return "text/html"; | |
| 102 } | |
| 103 | |
| 104 base::MessageLoop* ThumbnailListSource::MessageLoopForRequestPath( | |
| 105 const std::string& path) const { | |
| 106 // TopSites can be accessed from the IO thread. | |
| 107 return thumbnail_service_.get() ? | |
| 108 NULL : content::URLDataSource::MessageLoopForRequestPath(path); | |
| 109 } | |
| 110 | |
| 111 bool ThumbnailListSource::ShouldServiceRequest( | |
| 112 const net::URLRequest* request) const { | |
| 113 if (request->url().SchemeIs(chrome::kChromeSearchScheme)) | |
| 114 return InstantIOContext::ShouldServiceRequest(request); | |
| 115 return URLDataSource::ShouldServiceRequest(request); | |
| 116 } | |
| 117 | |
| 118 void ThumbnailListSource::OnMostVisitedURLsAvailable( | |
| 119 const content::URLDataSource::GotDataCallback& callback, | |
| 120 const history::MostVisitedURLList& visited_list) { | |
| 121 int num_mv = visited_list.size(); | |
| 122 int num_mv_with_thumb = 0; | |
| 123 std::vector<MostVisitedURLWithImage> mv_list(num_mv); | |
| 124 | |
| 125 for (int i = 0; i < num_mv; ++i) { | |
| 126 mv_list[i].mv_url = &visited_list[i]; | |
| 127 scoped_refptr<base::RefCountedMemory> data; | |
| 128 if (thumbnail_service_->GetPageThumbnail(visited_list[i].url, false, | |
| 129 &data)) { | |
| 130 // Thumbnail found, convert it to Base64 and store in mv_list[i]. | |
| 131 std::string data_str; | |
| 132 data_str.assign(reinterpret_cast<const char*>(data->front()), | |
| 133 data->size()); | |
| 134 base::Base64Encode(data_str, &mv_list[i].base64_encoded_png); | |
| 135 ++num_mv_with_thumb; | |
| 136 } | |
| 137 } | |
| 138 | |
| 139 std::vector<std::string> out; | |
| 140 out.push_back(html_header); | |
| 141 out.push_back(html_body); | |
| 142 if (num_mv_with_thumb > 0) { | |
| 143 out.push_back("<h2>TopSites URLs with Thumbnails</h2>\n"); | |
| 144 RenderMostVisitedURLList(mv_list, true, &out); | |
| 145 } | |
| 146 if (num_mv_with_thumb < num_mv) { | |
| 147 out.push_back("<h2>TopSites URLs without Thumbnails</h2>\n"); | |
| 148 RenderMostVisitedURLList(mv_list, false, &out); | |
| 149 } | |
| 150 out.push_back(html_footer); | |
| 151 | |
| 152 std::string out_html = JoinString(out, ""); | |
| 153 callback.Run(base::RefCountedString::TakeString(&out_html)); | |
| 154 } | |
| OLD | NEW |