OLD | NEW |
---|---|
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/time/time.h" | |
16 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
17 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" | 20 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" |
18 #include "chrome/common/url_constants.h" | 21 #include "chrome/common/url_constants.h" |
19 #include "components/suggestions/suggestions_service.h" | 22 #include "components/suggestions/suggestions_service.h" |
20 #include "net/base/escape.h" | 23 #include "net/base/escape.h" |
21 #include "ui/gfx/codec/png_codec.h" | 24 #include "ui/gfx/codec/png_codec.h" |
22 #include "ui/gfx/image/image_skia.h" | 25 #include "ui/gfx/image/image_skia.h" |
23 #include "url/gurl.h" | 26 #include "url/gurl.h" |
24 | 27 |
25 namespace suggestions { | 28 namespace suggestions { |
26 | 29 |
27 namespace { | 30 namespace { |
28 | 31 |
29 const char kHtmlHeader[] = | 32 const char kHtmlHeader[] = |
30 "<!DOCTYPE html>\n<html>\n<head>\n<title>Suggestions</title>\n" | 33 "<!DOCTYPE html>\n<html>\n<head>\n<title>Suggestions</title>\n" |
31 "<meta charset=\"utf-8\">\n" | 34 "<meta charset=\"utf-8\">\n" |
32 "<style type=\"text/css\">\nli {white-space: nowrap;}\n</style>\n"; | 35 "<style type=\"text/css\">\nli {white-space: nowrap;}\n</style>\n"; |
33 const char kHtmlBody[] = "</head>\n<body>\n"; | 36 const char kHtmlBody[] = "</head>\n<body>\n"; |
34 const char kHtmlFooter[] = "</body>\n</html>\n"; | 37 const char kHtmlFooter[] = "</body>\n</html>\n"; |
35 | 38 |
39 std::string FormatTimeDelta(int64 time_delta_usec){ | |
Mathieu
2014/08/04 14:39:10
I searched and found this class, which may contain
gayane -on leave until 09-2017
2014/08/04 16:34:57
With this class I can only show the two values(e.g
| |
40 int milliseconds = (time_delta_usec / Time.kMicrosecondsPerMillisecond) % | |
41 Time.KMillisecondsPerSecond; | |
42 int seconds = (time_delta_usec / Time.KMicrosecondsPerSecond) % | |
43 Time.kSecondsPerMinute; | |
44 int minutes = (time_delta_usec / Time.kMicrosecondsPerMinute) % | |
45 Time.KMinutesPerHour; | |
46 int hours = (time_delta_usec / Time.kMicrosecondsPerHour); | |
47 std::string formatted = base::StringPrintf( | |
48 "%dh %dm %ds %dms", | |
49 hours, minutes, seconds, milliseconds); | |
50 | |
51 return formatted; | |
52 } | |
53 | |
36 // Fills |output| with the HTML needed to display the suggestions. | 54 // Fills |output| with the HTML needed to display the suggestions. |
37 void RenderOutputHtml(const SuggestionsProfile& profile, | 55 void RenderOutputHtml(const SuggestionsProfile& profile, |
38 const std::map<GURL, std::string>& base64_encoded_pngs, | 56 const std::map<GURL, std::string>& base64_encoded_pngs, |
39 std::string* output) { | 57 std::string* output) { |
40 std::vector<std::string> out; | 58 std::vector<std::string> out; |
41 out.push_back(kHtmlHeader); | 59 out.push_back(kHtmlHeader); |
42 out.push_back(kHtmlBody); | 60 out.push_back(kHtmlBody); |
43 out.push_back("<h1>Suggestions</h1>\n<ul>"); | 61 out.push_back("<h1>Suggestions</h1>\n<ul>"); |
44 | 62 |
63 int64 now = (base::Time::NowFromSystemTime() - base::Time::UnixEpoch()) | |
64 .ToInternalValue(); | |
65 | |
45 size_t size = profile.suggestions_size(); | 66 size_t size = profile.suggestions_size(); |
46 for (size_t i = 0; i < size; ++i) { | 67 for (size_t i = 0; i < size; ++i) { |
47 const ChromeSuggestion& suggestion = profile.suggestions(i); | 68 const ChromeSuggestion& suggestion = profile.suggestions(i); |
69 int64 diff = (suggestion.expiry_ts()-now); | |
Mathieu
2014/08/04 14:39:10
no () around the expression, add one space each si
gayane -on leave until 09-2017
2014/08/04 16:34:57
Done.
| |
70 | |
48 std::string line; | 71 std::string line; |
49 line += "<li><a href=\""; | 72 line += "<li><a href=\""; |
50 line += net::EscapeForHTML(suggestion.url()); | 73 line += net::EscapeForHTML(suggestion.url()); |
51 line += "\" target=\"_blank\">"; | 74 line += "\" target=\"_blank\">"; |
52 line += net::EscapeForHTML(suggestion.title()); | 75 line += net::EscapeForHTML(suggestion.title()); |
53 std::map<GURL, std::string>::const_iterator it = | 76 std::map<GURL, std::string>::const_iterator it = |
54 base64_encoded_pngs.find(GURL(suggestion.url())); | 77 base64_encoded_pngs.find(GURL(suggestion.url())); |
55 if (it != base64_encoded_pngs.end()) { | 78 if (it != base64_encoded_pngs.end()) { |
56 line += "<br><img src='"; | 79 line += "<br><img src='"; |
57 line += it->second; | 80 line += it->second; |
58 line += "'>"; | 81 line += "'>"; |
59 } | 82 } |
60 line += "</a></li>\n"; | 83 line += "</a> Expires in "; |
84 line += FormatTimeDelta(diff); | |
85 | |
Mathieu
2014/08/04 14:39:10
remove extra line
gayane -on leave until 09-2017
2014/08/04 16:34:57
Done.
| |
86 line += "</li>\n"; | |
61 out.push_back(line); | 87 out.push_back(line); |
62 } | 88 } |
63 out.push_back("</ul>"); | 89 out.push_back("</ul>"); |
64 out.push_back(kHtmlFooter); | 90 out.push_back(kHtmlFooter); |
65 *output = JoinString(out, ""); | 91 *output = JoinString(out, ""); |
66 } | 92 } |
67 | 93 |
68 // Fills |output| with the HTML needed to display that no suggestions are | 94 // Fills |output| with the HTML needed to display that no suggestions are |
69 // available. | 95 // available. |
70 void RenderOutputHtmlNoSuggestions(std::string* output) { | 96 void RenderOutputHtmlNoSuggestions(std::string* output) { |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
174 std::string encoded_output; | 200 std::string encoded_output; |
175 base::Base64Encode(std::string(output.begin(), output.end()), | 201 base::Base64Encode(std::string(output.begin(), output.end()), |
176 &encoded_output); | 202 &encoded_output); |
177 context->base64_encoded_pngs[url] = "data:image/png;base64,"; | 203 context->base64_encoded_pngs[url] = "data:image/png;base64,"; |
178 context->base64_encoded_pngs[url] += encoded_output; | 204 context->base64_encoded_pngs[url] += encoded_output; |
179 } | 205 } |
180 barrier.Run(); | 206 barrier.Run(); |
181 } | 207 } |
182 | 208 |
183 } // namespace suggestions | 209 } // namespace suggestions |
OLD | NEW |