Chromium Code Reviews| Index: chrome/browser/ui/webui/ntp/thumbnail_list_source.cc |
| diff --git a/chrome/browser/ui/webui/ntp/thumbnail_list_source.cc b/chrome/browser/ui/webui/ntp/thumbnail_list_source.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c29355334df3462e532c5ca1542f7432a7183357 |
| --- /dev/null |
| +++ b/chrome/browser/ui/webui/ntp/thumbnail_list_source.cc |
| @@ -0,0 +1,154 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/ui/webui/ntp/thumbnail_list_source.h" |
| + |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "base/base64.h" |
| +#include "base/bind.h" |
| +#include "base/memory/ref_counted_memory.h" |
| +#include "base/strings/string_util.h" |
| +#include "chrome/browser/history/top_sites.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/search/instant_io_context.h" |
| +#include "chrome/browser/thumbnails/thumbnail_service.h" |
| +#include "chrome/browser/thumbnails/thumbnail_service_factory.h" |
| +#include "chrome/common/url_constants.h" |
| +#include "net/base/escape.h" |
| +#include "net/url_request/url_request.h" |
| + |
| +namespace { |
| + |
| +const char* html_header = |
| + "<!DOCTYPE HTML>\n<html>\n<head>\n<title>TopSites Thumbnails</title>\n" |
| + "<meta charset=\"utf-8\">\n" |
| + "<style type=\"text/css\">\nimg.thumb {border: 1px solid black;}\n" |
| + "li {white-space: nowrap;}\n</style>\n"; |
| +const char* html_body = "</head>\n<body>\n"; |
| +const char* html_footer = "</body>\n</html>\n"; |
| + |
| +struct MostVisitedURLWithImage{ |
| + const history::MostVisitedURL* mv_url; |
|
sky
2013/09/30 20:46:16
Can you copy the url here. No lifetime issues to w
huangs
2013/09/30 22:07:11
I was being silly in trying to wrap history::MostV
|
| + std::string base64_encoded_png; |
| + |
| + MostVisitedURLWithImage() : mv_url(NULL) {} |
|
sky
2013/09/30 20:46:16
nit: functions before members with structs too.
huangs
2013/09/30 22:07:11
Done.
|
| + bool has_thumbnail() const { return !base64_encoded_png.empty(); } |
| +}; |
| + |
| +// If |show_thumbnail| == true, then render elements in |mv_list| that have a |
| +// thumbnail (along with their thumbnails). Otherwise render elements in |
| +// |mv_list| that do not have a thumbnail. |
| +void RenderMostVisitedURLList( |
| + const std::vector<MostVisitedURLWithImage>& mv_list, |
| + bool show_thumbnail, |
| + std::vector<std::string>* out) { |
| + out->push_back("<div><ul>\n"); |
| + std::vector<MostVisitedURLWithImage>::const_iterator it; |
| + for (it = mv_list.begin(); it != mv_list.end(); ++it) { |
| + if (it->has_thumbnail() == show_thumbnail) { |
| + const GURL& url(it->mv_url->url); |
| + out->push_back("<li>\n"); |
| + out->push_back(net::EscapeForHTML(url.spec()) + "\n"); |
| + if (show_thumbnail) { |
| + out->push_back("<div><img class=\"thumb\" " |
| + "src=\"data:image/png;base64," + |
| + it->base64_encoded_png + "\"/></div>\n"); |
| + } |
| + if (!it->mv_url->redirects.empty()) { |
| + out->push_back("<ul>\n"); |
| + history::RedirectList::const_iterator jt; |
| + for (jt = it->mv_url->redirects.begin(); |
| + jt != it->mv_url->redirects.end(); ++jt) { |
| + out->push_back("<li>" + net::EscapeForHTML(jt->spec()) + "</li>\n"); |
| + } |
| + out->push_back("</ul>\n"); |
| + } |
| + out->push_back("</li>\n"); |
| + } |
| + } |
| + out->push_back("</ul></div>\n"); |
| +} |
| + |
| +} // namespace |
| + |
| +ThumbnailListSource::ThumbnailListSource(Profile* profile) |
| + : thumbnail_service_(ThumbnailServiceFactory::GetForProfile(profile)), |
| + profile_(profile) { |
| +} |
| + |
| +ThumbnailListSource::~ThumbnailListSource() { |
| +} |
| + |
| +std::string ThumbnailListSource::GetSource() const { |
| + return chrome::kChromeUIThumbnailListHost; |
| +} |
| + |
| +void ThumbnailListSource::StartDataRequest( |
| + const std::string& path, |
| + int render_process_id, |
| + int render_view_id, |
| + const content::URLDataSource::GotDataCallback& callback) { |
| + profile_->GetTopSites()->GetMostVisitedURLs( |
| + base::Bind(&ThumbnailListSource::OnMostVisitedURLsAvailable, |
| + base::Unretained(this), |
|
sky
2013/09/30 20:46:16
Why the unretained here?
huangs
2013/09/30 22:07:11
Changed to weak_ptr_factory_.GetWeakPtr(), emulati
|
| + callback)); |
| +} |
| + |
| +std::string ThumbnailListSource::GetMimeType(const std::string& path) const { |
| + return "text/html"; |
| +} |
| + |
| +base::MessageLoop* ThumbnailListSource::MessageLoopForRequestPath( |
| + const std::string& path) const { |
| + // TopSites can be accessed from the IO thread. |
| + return thumbnail_service_.get() ? |
| + NULL : content::URLDataSource::MessageLoopForRequestPath(path); |
| +} |
| + |
| +bool ThumbnailListSource::ShouldServiceRequest( |
| + const net::URLRequest* request) const { |
| + if (request->url().SchemeIs(chrome::kChromeSearchScheme)) |
| + return InstantIOContext::ShouldServiceRequest(request); |
| + return URLDataSource::ShouldServiceRequest(request); |
| +} |
| + |
| +void ThumbnailListSource::OnMostVisitedURLsAvailable( |
| + const content::URLDataSource::GotDataCallback& callback, |
| + const history::MostVisitedURLList& visited_list) { |
| + int num_mv = visited_list.size(); |
|
sky
2013/09/30 20:46:16
nit: const
huangs
2013/09/30 22:07:11
Done.
|
| + int num_mv_with_thumb = 0; |
| + std::vector<MostVisitedURLWithImage> mv_list(num_mv); |
| + |
| + for (int i = 0; i < num_mv; ++i) { |
| + mv_list[i].mv_url = &visited_list[i]; |
| + scoped_refptr<base::RefCountedMemory> data; |
| + if (thumbnail_service_->GetPageThumbnail(visited_list[i].url, false, |
| + &data)) { |
| + // Thumbnail found, convert it to Base64 and store in mv_list[i]. |
| + std::string data_str; |
| + data_str.assign(reinterpret_cast<const char*>(data->front()), |
| + data->size()); |
| + base::Base64Encode(data_str, &mv_list[i].base64_encoded_png); |
| + ++num_mv_with_thumb; |
| + } |
| + } |
| + |
| + std::vector<std::string> out; |
| + out.push_back(html_header); |
| + out.push_back(html_body); |
| + if (num_mv_with_thumb > 0) { |
| + out.push_back("<h2>TopSites URLs with Thumbnails</h2>\n"); |
| + RenderMostVisitedURLList(mv_list, true, &out); |
| + } |
| + if (num_mv_with_thumb < num_mv) { |
| + out.push_back("<h2>TopSites URLs without Thumbnails</h2>\n"); |
| + RenderMostVisitedURLList(mv_list, false, &out); |
| + } |
| + out.push_back(html_footer); |
| + |
| + std::string out_html = JoinString(out, ""); |
| + callback.Run(base::RefCountedString::TakeString(&out_html)); |
| +} |