Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / chrome / browser / search / suggestions / suggestions_source.cc
blob79ea0be69f6908a7da8da8186ec7c770bfd28f27
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.
5 #include "chrome/browser/search/suggestions/suggestions_source.h"
7 #include <vector>
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_number_conversions.h"
15 #include "base/strings/string_piece.h"
16 #include "base/strings/string_util.h"
17 #include "base/strings/stringprintf.h"
18 #include "base/strings/utf_string_conversions.h"
19 #include "base/time/time.h"
20 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/browser/search/suggestions/suggestions_service_factory.h"
22 #include "chrome/common/url_constants.h"
23 #include "components/suggestions/suggestions_service.h"
24 #include "components/suggestions/suggestions_utils.h"
25 #include "net/base/escape.h"
26 #include "ui/base/l10n/time_format.h"
27 #include "ui/gfx/codec/png_codec.h"
28 #include "ui/gfx/image/image_skia.h"
29 #include "url/gurl.h"
31 namespace suggestions {
33 namespace {
35 const char kHtmlHeader[] =
36 "<!DOCTYPE html>\n<html>\n<head>\n<title>Suggestions</title>\n"
37 "<meta charset=\"utf-8\">\n"
38 "<style type=\"text/css\">\nli {white-space: nowrap;}\n</style>\n";
39 const char kHtmlBody[] = "</head>\n<body>\n";
40 const char kHtmlFooter[] = "</body>\n</html>\n";
42 // Fills |output| with the HTML needed to display the suggestions.
43 void RenderOutputHtml(const SuggestionsProfile& profile,
44 const std::map<GURL, std::string>& base64_encoded_pngs,
45 std::string* output) {
46 std::vector<std::string> out;
47 out.push_back(kHtmlHeader);
48 out.push_back(kHtmlBody);
49 out.push_back("<h1>Suggestions</h1>\n<ul>");
51 int64 now = (base::Time::NowFromSystemTime() - base::Time::UnixEpoch())
52 .ToInternalValue();
53 size_t size = profile.suggestions_size();
54 for (size_t i = 0; i < size; ++i) {
55 const ChromeSuggestion& suggestion = profile.suggestions(i);
56 base::TimeDelta remaining_time = base::TimeDelta::FromMicroseconds(
57 suggestion.expiry_ts() - now);
58 base::string16 remaining_time_formatted = ui::TimeFormat::Detailed(
59 ui::TimeFormat::Format::FORMAT_DURATION,
60 ui::TimeFormat::Length::LENGTH_LONG,
61 -1, remaining_time);
62 std::string line;
63 line += "<li><a href=\"";
64 line += net::EscapeForHTML(suggestion.url());
65 line += "\" target=\"_blank\">";
66 line += net::EscapeForHTML(suggestion.title());
67 std::map<GURL, std::string>::const_iterator it =
68 base64_encoded_pngs.find(GURL(suggestion.url()));
69 if (it != base64_encoded_pngs.end()) {
70 line += "<br><img src='";
71 line += it->second;
72 line += "'>";
74 line += "</a> Expires in ";
75 line += base::UTF16ToUTF8(remaining_time_formatted);
76 line += "</li>\n";
77 out.push_back(line);
79 out.push_back("</ul>");
80 out.push_back(kHtmlFooter);
81 *output = base::JoinString(out, base::StringPiece());
84 // Fills |output| with the HTML needed to display that no suggestions are
85 // available.
86 void RenderOutputHtmlNoSuggestions(std::string* output) {
87 std::vector<std::string> out;
88 out.push_back(kHtmlHeader);
89 out.push_back(kHtmlBody);
90 out.push_back("<h1>Suggestions</h1>\n");
91 out.push_back("<p>You have no suggestions.</p>\n");
92 out.push_back(kHtmlFooter);
93 *output = base::JoinString(out, base::StringPiece());
96 } // namespace
98 SuggestionsSource::SuggestionsSource(Profile* profile)
99 : profile_(profile), weak_ptr_factory_(this) {}
101 SuggestionsSource::~SuggestionsSource() {}
103 SuggestionsSource::RequestContext::RequestContext(
104 const SuggestionsProfile& suggestions_profile_in,
105 const content::URLDataSource::GotDataCallback& callback_in)
106 : suggestions_profile(suggestions_profile_in), // Copy.
107 callback(callback_in) // Copy.
110 SuggestionsSource::RequestContext::~RequestContext() {}
112 std::string SuggestionsSource::GetSource() const {
113 return chrome::kChromeUISuggestionsHost;
116 void SuggestionsSource::StartDataRequest(
117 const std::string& path, int render_process_id, int render_frame_id,
118 const content::URLDataSource::GotDataCallback& callback) {
119 SuggestionsService* suggestions_service =
120 SuggestionsServiceFactory::GetForProfile(profile_);
122 if (!suggestions_service) {
123 callback.Run(NULL);
124 return;
127 // Since it's a debugging page, it's fine to specify that sync state is
128 // initialized.
129 suggestions_service->FetchSuggestionsData(
130 INITIALIZED_ENABLED_HISTORY,
131 base::Bind(&SuggestionsSource::OnSuggestionsAvailable,
132 weak_ptr_factory_.GetWeakPtr(), callback));
135 std::string SuggestionsSource::GetMimeType(const std::string& path) const {
136 return "text/html";
139 base::MessageLoop* SuggestionsSource::MessageLoopForRequestPath(
140 const std::string& path) const {
141 // This can be accessed from the IO thread.
142 return content::URLDataSource::MessageLoopForRequestPath(path);
145 void SuggestionsSource::OnSuggestionsAvailable(
146 const content::URLDataSource::GotDataCallback& callback,
147 const SuggestionsProfile& suggestions_profile) {
148 size_t size = suggestions_profile.suggestions_size();
149 if (!size) {
150 std::string output;
151 RenderOutputHtmlNoSuggestions(&output);
152 callback.Run(base::RefCountedString::TakeString(&output));
153 } else {
154 RequestContext* context = new RequestContext(suggestions_profile, callback);
155 base::Closure barrier = BarrierClosure(
156 size, base::Bind(&SuggestionsSource::OnThumbnailsFetched,
157 weak_ptr_factory_.GetWeakPtr(), context));
158 for (size_t i = 0; i < size; ++i) {
159 const ChromeSuggestion& suggestion = suggestions_profile.suggestions(i);
160 // Fetch the thumbnail for this URL (exercising the fetcher). After all
161 // fetches are done, including NULL callbacks for unavailable thumbnails,
162 // SuggestionsSource::OnThumbnailsFetched will be called.
163 SuggestionsService* suggestions_service(
164 SuggestionsServiceFactory::GetForProfile(profile_));
165 suggestions_service->GetPageThumbnail(
166 GURL(suggestion.url()),
167 base::Bind(&SuggestionsSource::OnThumbnailAvailable,
168 weak_ptr_factory_.GetWeakPtr(), context, barrier));
173 void SuggestionsSource::OnThumbnailsFetched(RequestContext* context) {
174 scoped_ptr<RequestContext> context_deleter(context);
176 std::string output;
177 RenderOutputHtml(context->suggestions_profile, context->base64_encoded_pngs,
178 &output);
179 context->callback.Run(base::RefCountedString::TakeString(&output));
182 void SuggestionsSource::OnThumbnailAvailable(RequestContext* context,
183 base::Closure barrier,
184 const GURL& url,
185 const SkBitmap* bitmap) {
186 if (bitmap) {
187 std::vector<unsigned char> output;
188 gfx::PNGCodec::EncodeBGRASkBitmap(*bitmap, false, &output);
190 std::string encoded_output;
191 base::Base64Encode(std::string(output.begin(), output.end()),
192 &encoded_output);
193 context->base64_encoded_pngs[url] = "data:image/png;base64,";
194 context->base64_encoded_pngs[url] += encoded_output;
196 barrier.Run();
199 } // namespace suggestions