• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "chrome/browser/search/suggestions/suggestions_source.h"
6 
7 #include <vector>
8 
9 #include "base/barrier_closure.h"
10 #include "base/base64.h"
11 #include "base/bind.h"
12 #include "base/memory/ref_counted_memory.h"
13 #include "base/strings/string16.h"
14 #include "base/strings/string_piece.h"
15 #include "base/strings/string_util.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/search/instant_io_context.h"
18 #include "chrome/browser/search/suggestions/suggestions_service.h"
19 #include "chrome/browser/search/suggestions/suggestions_service_factory.h"
20 #include "chrome/common/url_constants.h"
21 #include "net/base/escape.h"
22 #include "net/url_request/url_request.h"
23 #include "ui/gfx/codec/png_codec.h"
24 #include "ui/gfx/image/image_skia.h"
25 #include "url/gurl.h"
26 
27 namespace suggestions {
28 
29 namespace {
30 
31 const char kHtmlHeader[] =
32     "<!DOCTYPE html>\n<html>\n<head>\n<title>Suggestions</title>\n"
33     "<meta charset=\"utf-8\">\n"
34     "<style type=\"text/css\">\nli {white-space: nowrap;}\n</style>\n";
35 const char kHtmlBody[] = "</head>\n<body>\n";
36 const char kHtmlFooter[] = "</body>\n</html>\n";
37 
38 // Fills |output| with the HTML needed to display the suggestions.
RenderOutputHtml(const SuggestionsProfile & profile,const std::map<GURL,std::string> & base64_encoded_pngs,std::string * output)39 void RenderOutputHtml(const SuggestionsProfile& profile,
40                       const std::map<GURL, std::string>& base64_encoded_pngs,
41                       std::string* output) {
42   std::vector<std::string> out;
43   out.push_back(kHtmlHeader);
44   out.push_back(kHtmlBody);
45   out.push_back("<h1>Suggestions</h1>\n<ul>");
46 
47   size_t size = profile.suggestions_size();
48   for (size_t i = 0; i < size; ++i) {
49     const ChromeSuggestion& suggestion = profile.suggestions(i);
50     std::string line;
51     line += "<li><a href=\"";
52     line += net::EscapeForHTML(suggestion.url());
53     line += "\" target=\"_blank\">";
54     line += net::EscapeForHTML(suggestion.title());
55     std::map<GURL, std::string>::const_iterator it =
56         base64_encoded_pngs.find(GURL(suggestion.url()));
57     if (it != base64_encoded_pngs.end()) {
58       line += "<br><img src='";
59       line += it->second;
60       line += "'>";
61     }
62     line += "</a></li>\n";
63     out.push_back(line);
64   }
65   out.push_back("</ul>");
66   out.push_back(kHtmlFooter);
67   *output = JoinString(out, "");
68 }
69 
70 // Fills |output| with the HTML needed to display that no suggestions are
71 // available.
RenderOutputHtmlNoSuggestions(std::string * output)72 void RenderOutputHtmlNoSuggestions(std::string* output) {
73   std::vector<std::string> out;
74   out.push_back(kHtmlHeader);
75   out.push_back(kHtmlBody);
76   out.push_back("<h1>Suggestions</h1>\n");
77   out.push_back("<p>You have no suggestions.</p>\n");
78   out.push_back(kHtmlFooter);
79   *output = JoinString(out, "");
80 }
81 
82 }  // namespace
83 
SuggestionsSource(Profile * profile)84 SuggestionsSource::SuggestionsSource(Profile* profile)
85     : profile_(profile), weak_ptr_factory_(this) {}
86 
~SuggestionsSource()87 SuggestionsSource::~SuggestionsSource() {}
88 
RequestContext(const SuggestionsProfile & suggestions_profile_in,const content::URLDataSource::GotDataCallback & callback_in)89 SuggestionsSource::RequestContext::RequestContext(
90     const SuggestionsProfile& suggestions_profile_in,
91     const content::URLDataSource::GotDataCallback& callback_in)
92     : suggestions_profile(suggestions_profile_in),   // Copy.
93       callback(callback_in)  // Copy.
94 {}
95 
~RequestContext()96 SuggestionsSource::RequestContext::~RequestContext() {}
97 
GetSource() const98 std::string SuggestionsSource::GetSource() const {
99   return chrome::kChromeUISuggestionsHost;
100 }
101 
StartDataRequest(const std::string & path,int render_process_id,int render_frame_id,const content::URLDataSource::GotDataCallback & callback)102 void SuggestionsSource::StartDataRequest(
103     const std::string& path, int render_process_id, int render_frame_id,
104     const content::URLDataSource::GotDataCallback& callback) {
105   SuggestionsServiceFactory* suggestions_service_factory =
106       SuggestionsServiceFactory::GetInstance();
107 
108   SuggestionsService* suggestions_service(
109       suggestions_service_factory->GetForProfile(profile_));
110 
111   if (!suggestions_service) {
112     callback.Run(NULL);
113     return;
114   }
115 
116   suggestions_service->FetchSuggestionsData(
117       base::Bind(&SuggestionsSource::OnSuggestionsAvailable,
118                  weak_ptr_factory_.GetWeakPtr(), callback));
119 }
120 
GetMimeType(const std::string & path) const121 std::string SuggestionsSource::GetMimeType(const std::string& path) const {
122   return "text/html";
123 }
124 
MessageLoopForRequestPath(const std::string & path) const125 base::MessageLoop* SuggestionsSource::MessageLoopForRequestPath(
126     const std::string& path) const {
127   // This can be accessed from the IO thread.
128   return content::URLDataSource::MessageLoopForRequestPath(path);
129 }
130 
ShouldServiceRequest(const net::URLRequest * request) const131 bool SuggestionsSource::ShouldServiceRequest(
132     const net::URLRequest* request) const {
133   if (request->url().SchemeIs(chrome::kChromeSearchScheme))
134     return InstantIOContext::ShouldServiceRequest(request);
135   return URLDataSource::ShouldServiceRequest(request);
136 }
137 
OnSuggestionsAvailable(const content::URLDataSource::GotDataCallback & callback,const SuggestionsProfile & suggestions_profile)138 void SuggestionsSource::OnSuggestionsAvailable(
139     const content::URLDataSource::GotDataCallback& callback,
140     const SuggestionsProfile& suggestions_profile) {
141   size_t size = suggestions_profile.suggestions_size();
142   if (!size) {
143     std::string output;
144     RenderOutputHtmlNoSuggestions(&output);
145     callback.Run(base::RefCountedString::TakeString(&output));
146   } else {
147     RequestContext* context = new RequestContext(suggestions_profile, callback);
148     base::Closure barrier = BarrierClosure(
149         size, base::Bind(&SuggestionsSource::OnThumbnailsFetched,
150                          weak_ptr_factory_.GetWeakPtr(), context));
151     for (size_t i = 0; i < size; ++i) {
152       const ChromeSuggestion& suggestion = suggestions_profile.suggestions(i);
153       // Fetch the thumbnail for this URL (exercising the fetcher). After all
154       // fetches are done, including NULL callbacks for unavailable thumbnails,
155       // SuggestionsSource::OnThumbnailsFetched will be called.
156       SuggestionsService* suggestions_service(
157           SuggestionsServiceFactory::GetForProfile(profile_));
158       suggestions_service->GetPageThumbnail(
159           GURL(suggestion.url()),
160           base::Bind(&SuggestionsSource::OnThumbnailAvailable,
161                      weak_ptr_factory_.GetWeakPtr(), context, barrier));
162     }
163   }
164 }
165 
OnThumbnailsFetched(RequestContext * context)166 void SuggestionsSource::OnThumbnailsFetched(RequestContext* context) {
167   scoped_ptr<RequestContext> context_deleter(context);
168 
169   std::string output;
170   RenderOutputHtml(context->suggestions_profile, context->base64_encoded_pngs,
171                    &output);
172   context->callback.Run(base::RefCountedString::TakeString(&output));
173 }
174 
OnThumbnailAvailable(RequestContext * context,base::Closure barrier,const GURL & url,const SkBitmap * bitmap)175 void SuggestionsSource::OnThumbnailAvailable(RequestContext* context,
176                                              base::Closure barrier,
177                                              const GURL& url,
178                                              const SkBitmap* bitmap) {
179   if (bitmap) {
180     std::vector<unsigned char> output;
181     gfx::PNGCodec::EncodeBGRASkBitmap(*bitmap, false, &output);
182 
183     std::string encoded_output;
184     base::Base64Encode(std::string(output.begin(), output.end()),
185                        &encoded_output);
186     context->base64_encoded_pngs[url] = "data:image/png;base64,";
187     context->base64_encoded_pngs[url] += encoded_output;
188   }
189   barrier.Run();
190 }
191 
192 }  // namespace suggestions
193