Chromium Code Reviews| 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/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(); | |
| 52 | |
| 53 int cutoff = -1; // forces two-value output for TimeFormat | |
|
manzagop (departed)
2014/08/04 19:02:59
Capitalize 'forces'.
gayane -on leave until 09-2017
2014/08/04 19:26:44
skipped because the variable is removed. If you wa
| |
| 45 size_t size = profile.suggestions_size(); | 54 size_t size = profile.suggestions_size(); |
| 46 for (size_t i = 0; i < size; ++i) { | 55 for (size_t i = 0; i < size; ++i) { |
| 47 const ChromeSuggestion& suggestion = profile.suggestions(i); | 56 const ChromeSuggestion& suggestion = profile.suggestions(i); |
| 57 | |
| 58 int64 diff = (suggestion.expiry_ts() - now); | |
|
Mathieu
2014/08/04 18:29:21
inline this expression in FromMicroseconds, below
gayane -on leave until 09-2017
2014/08/04 19:26:44
Done.
| |
| 59 base::TimeDelta td =base::TimeDelta::FromMicroseconds(diff); | |
|
Mathieu
2014/08/04 18:29:21
space after =
gayane -on leave until 09-2017
2014/08/04 19:26:44
Done.
| |
| 60 base::string16 formatted = ui::TimeFormat::Detailed( | |
| 61 ui::TimeFormat::Format::FORMAT_DURATION, | |
|
Mathieu
2014/08/04 18:29:21
indent all parameters line by 4 from the previous
gayane -on leave until 09-2017
2014/08/04 19:26:44
Done.
| |
| 62 ui::TimeFormat::Length::LENGTH_LONG, | |
| 63 cutoff, td); | |
|
Mathieu
2014/08/04 18:29:21
directly pass -1, here
gayane -on leave until 09-2017
2014/08/04 19:26:44
Done.
| |
| 48 std::string line; | 64 std::string line; |
| 49 line += "<li><a href=\""; | 65 line += "<li><a href=\""; |
| 50 line += net::EscapeForHTML(suggestion.url()); | 66 line += net::EscapeForHTML(suggestion.url()); |
| 51 line += "\" target=\"_blank\">"; | 67 line += "\" target=\"_blank\">"; |
| 52 line += net::EscapeForHTML(suggestion.title()); | 68 line += net::EscapeForHTML(suggestion.title()); |
| 53 std::map<GURL, std::string>::const_iterator it = | 69 std::map<GURL, std::string>::const_iterator it = |
| 54 base64_encoded_pngs.find(GURL(suggestion.url())); | 70 base64_encoded_pngs.find(GURL(suggestion.url())); |
| 55 if (it != base64_encoded_pngs.end()) { | 71 if (it != base64_encoded_pngs.end()) { |
| 56 line += "<br><img src='"; | 72 line += "<br><img src='"; |
| 57 line += it->second; | 73 line += it->second; |
| 58 line += "'>"; | 74 line += "'>"; |
| 59 } | 75 } |
| 60 line += "</a></li>\n"; | 76 line += "</a> Expires in "; |
| 77 line += base::UTF16ToUTF8(formatted); | |
| 78 line += "</li>\n"; | |
| 61 out.push_back(line); | 79 out.push_back(line); |
| 62 } | 80 } |
| 63 out.push_back("</ul>"); | 81 out.push_back("</ul>"); |
| 64 out.push_back(kHtmlFooter); | 82 out.push_back(kHtmlFooter); |
| 65 *output = JoinString(out, ""); | 83 *output = JoinString(out, ""); |
| 66 } | 84 } |
| 67 | 85 |
| 68 // Fills |output| with the HTML needed to display that no suggestions are | 86 // Fills |output| with the HTML needed to display that no suggestions are |
| 69 // available. | 87 // available. |
| 70 void RenderOutputHtmlNoSuggestions(std::string* output) { | 88 void RenderOutputHtmlNoSuggestions(std::string* output) { |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 174 std::string encoded_output; | 192 std::string encoded_output; |
| 175 base::Base64Encode(std::string(output.begin(), output.end()), | 193 base::Base64Encode(std::string(output.begin(), output.end()), |
| 176 &encoded_output); | 194 &encoded_output); |
| 177 context->base64_encoded_pngs[url] = "data:image/png;base64,"; | 195 context->base64_encoded_pngs[url] = "data:image/png;base64,"; |
| 178 context->base64_encoded_pngs[url] += encoded_output; | 196 context->base64_encoded_pngs[url] += encoded_output; |
| 179 } | 197 } |
| 180 barrier.Run(); | 198 barrier.Run(); |
| 181 } | 199 } |
| 182 | 200 |
| 183 } // namespace suggestions | 201 } // namespace suggestions |
| OLD | NEW |