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

Side by Side Diff: chrome/browser/search/suggestions/suggestions_source.cc

Issue 423133003: [Suggestions Service] Add support for expiring the SuggestionsStore (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Test improvement: scoped_ptr for SuggestionsStore object in unittests Created 6 years, 4 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/search/suggestions/suggestions_source.h" 5 #include "chrome/browser/search/suggestions/suggestions_source.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/barrier_closure.h" 9 #include "base/barrier_closure.h"
10 #include "base/base64.h" 10 #include "base/base64.h"
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/memory/ref_counted_memory.h" 12 #include "base/memory/ref_counted_memory.h"
13 #include "base/strings/string16.h" 13 #include "base/strings/string16.h"
14 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_piece.h" 15 #include "base/strings/string_piece.h"
15 #include "base/strings/string_util.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"
16 #include "chrome/browser/profiles/profile.h" 20 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" 21 #include "chrome/browser/search/suggestions/suggestions_service_factory.h"
18 #include "chrome/common/url_constants.h" 22 #include "chrome/common/url_constants.h"
19 #include "components/suggestions/suggestions_service.h" 23 #include "components/suggestions/suggestions_service.h"
20 #include "net/base/escape.h" 24 #include "net/base/escape.h"
25 #include "ui/base/l10n/time_format.h"
21 #include "ui/gfx/codec/png_codec.h" 26 #include "ui/gfx/codec/png_codec.h"
22 #include "ui/gfx/image/image_skia.h" 27 #include "ui/gfx/image/image_skia.h"
23 #include "url/gurl.h" 28 #include "url/gurl.h"
24 29
25 namespace suggestions { 30 namespace suggestions {
26 31
27 namespace { 32 namespace {
28 33
29 const char kHtmlHeader[] = 34 const char kHtmlHeader[] =
30 "<!DOCTYPE html>\n<html>\n<head>\n<title>Suggestions</title>\n" 35 "<!DOCTYPE html>\n<html>\n<head>\n<title>Suggestions</title>\n"
31 "<meta charset=\"utf-8\">\n" 36 "<meta charset=\"utf-8\">\n"
32 "<style type=\"text/css\">\nli {white-space: nowrap;}\n</style>\n"; 37 "<style type=\"text/css\">\nli {white-space: nowrap;}\n</style>\n";
33 const char kHtmlBody[] = "</head>\n<body>\n"; 38 const char kHtmlBody[] = "</head>\n<body>\n";
34 const char kHtmlFooter[] = "</body>\n</html>\n"; 39 const char kHtmlFooter[] = "</body>\n</html>\n";
35 40
36 // Fills |output| with the HTML needed to display the suggestions. 41 // Fills |output| with the HTML needed to display the suggestions.
37 void RenderOutputHtml(const SuggestionsProfile& profile, 42 void RenderOutputHtml(const SuggestionsProfile& profile,
38 const std::map<GURL, std::string>& base64_encoded_pngs, 43 const std::map<GURL, std::string>& base64_encoded_pngs,
39 std::string* output) { 44 std::string* output) {
40 std::vector<std::string> out; 45 std::vector<std::string> out;
41 out.push_back(kHtmlHeader); 46 out.push_back(kHtmlHeader);
42 out.push_back(kHtmlBody); 47 out.push_back(kHtmlBody);
43 out.push_back("<h1>Suggestions</h1>\n<ul>"); 48 out.push_back("<h1>Suggestions</h1>\n<ul>");
44 49
50 int64 now = (base::Time::NowFromSystemTime() - base::Time::UnixEpoch())
51 .ToInternalValue();
45 size_t size = profile.suggestions_size(); 52 size_t size = profile.suggestions_size();
46 for (size_t i = 0; i < size; ++i) { 53 for (size_t i = 0; i < size; ++i) {
47 const ChromeSuggestion& suggestion = profile.suggestions(i); 54 const ChromeSuggestion& suggestion = profile.suggestions(i);
55 base::TimeDelta td = base::TimeDelta::FromMicroseconds(
manzagop (departed) 2014/08/06 19:11:18 td isn't self-explanatory. Can you find better? eg
gayane -on leave until 09-2017 2014/08/06 21:12:20 renamed as remaining_time
56 suggestion.expiry_ts() - now);
57 base::string16 formatted = ui::TimeFormat::Detailed(
manzagop (departed) 2014/08/06 19:11:18 Perhaps td_formatted so it's clear what this?
gayane -on leave until 09-2017 2014/08/06 21:12:20 renamed as remaining_time_formatted
58 ui::TimeFormat::Format::FORMAT_DURATION,
59 ui::TimeFormat::Length::LENGTH_LONG,
60 -1, td);
48 std::string line; 61 std::string line;
49 line += "<li><a href=\""; 62 line += "<li><a href=\"";
50 line += net::EscapeForHTML(suggestion.url()); 63 line += net::EscapeForHTML(suggestion.url());
51 line += "\" target=\"_blank\">"; 64 line += "\" target=\"_blank\">";
52 line += net::EscapeForHTML(suggestion.title()); 65 line += net::EscapeForHTML(suggestion.title());
53 std::map<GURL, std::string>::const_iterator it = 66 std::map<GURL, std::string>::const_iterator it =
54 base64_encoded_pngs.find(GURL(suggestion.url())); 67 base64_encoded_pngs.find(GURL(suggestion.url()));
55 if (it != base64_encoded_pngs.end()) { 68 if (it != base64_encoded_pngs.end()) {
56 line += "<br><img src='"; 69 line += "<br><img src='";
57 line += it->second; 70 line += it->second;
58 line += "'>"; 71 line += "'>";
59 } 72 }
60 line += "</a></li>\n"; 73 line += "</a> Expires in ";
74 line += base::UTF16ToUTF8(formatted);
75 line += "</li>\n";
61 out.push_back(line); 76 out.push_back(line);
62 } 77 }
63 out.push_back("</ul>"); 78 out.push_back("</ul>");
64 out.push_back(kHtmlFooter); 79 out.push_back(kHtmlFooter);
65 *output = JoinString(out, ""); 80 *output = JoinString(out, "");
66 } 81 }
67 82
68 // Fills |output| with the HTML needed to display that no suggestions are 83 // Fills |output| with the HTML needed to display that no suggestions are
69 // available. 84 // available.
70 void RenderOutputHtmlNoSuggestions(std::string* output) { 85 void RenderOutputHtmlNoSuggestions(std::string* output) {
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 std::string encoded_output; 189 std::string encoded_output;
175 base::Base64Encode(std::string(output.begin(), output.end()), 190 base::Base64Encode(std::string(output.begin(), output.end()),
176 &encoded_output); 191 &encoded_output);
177 context->base64_encoded_pngs[url] = "data:image/png;base64,"; 192 context->base64_encoded_pngs[url] = "data:image/png;base64,";
178 context->base64_encoded_pngs[url] += encoded_output; 193 context->base64_encoded_pngs[url] += encoded_output;
179 } 194 }
180 barrier.Run(); 195 barrier.Run();
181 } 196 }
182 197
183 } // namespace suggestions 198 } // namespace suggestions
OLDNEW
« no previous file with comments | « no previous file | components/suggestions/proto/suggestions.proto » ('j') | components/suggestions/suggestions_service.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698