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/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 timedelta){ | |
|
manzagop (departed)
2014/07/31 15:31:49
Include the unit in the name, eg time_delta_usec i
gayane -on leave until 09-2017
2014/08/04 13:46:29
Done.
| |
| 40 | |
|
manzagop (departed)
2014/07/31 15:31:49
No need for this empty line.
gayane -on leave until 09-2017
2014/08/04 13:46:30
Done.
| |
| 41 timedelta /= 1000; | |
|
manzagop (departed)
2014/07/31 15:31:49
I'd argue the code will be more readable / less er
gayane -on leave until 09-2017
2014/08/04 13:46:29
Done.
| |
| 42 int milliseconds = timedelta % 1000; | |
|
manzagop (departed)
2014/07/31 15:31:49
int64?
gayane -on leave until 09-2017
2014/08/04 13:46:30
Isn't it always going to be less then 1000?
| |
| 43 timedelta /= 1000; | |
| 44 int seconds = timedelta%60; | |
|
manzagop (departed)
2014/07/31 15:31:49
Put space around binary operators (timedelta % 60)
gayane -on leave until 09-2017
2014/08/04 13:46:30
Done.
| |
| 45 timedelta /= 60; | |
| 46 int minutes = timedelta%60; | |
| 47 timedelta /= 60; | |
| 48 int hours = timedelta; | |
| 49 std::string formatted = base::StringPrintf("%dh %dm %ds %dms", | |
| 50 hours, minutes, seconds, milliseconds); | |
|
manzagop (departed)
2014/07/31 15:31:49
Fixup the indentation as per: http://google-styleg
gayane -on leave until 09-2017
2014/08/04 13:46:30
Done.
| |
| 51 | |
| 52 return formatted; | |
| 53 } | |
| 54 | |
| 36 // Fills |output| with the HTML needed to display the suggestions. | 55 // Fills |output| with the HTML needed to display the suggestions. |
| 37 void RenderOutputHtml(const SuggestionsProfile& profile, | 56 void RenderOutputHtml(const SuggestionsProfile& profile, |
| 38 const std::map<GURL, std::string>& base64_encoded_pngs, | 57 const std::map<GURL, std::string>& base64_encoded_pngs, |
| 39 std::string* output) { | 58 std::string* output) { |
| 40 std::vector<std::string> out; | 59 std::vector<std::string> out; |
| 41 out.push_back(kHtmlHeader); | 60 out.push_back(kHtmlHeader); |
| 42 out.push_back(kHtmlBody); | 61 out.push_back(kHtmlBody); |
| 43 out.push_back("<h1>Suggestions</h1>\n<ul>"); | 62 out.push_back("<h1>Suggestions</h1>\n<ul>"); |
| 44 | 63 |
| 64 int64 now = (base::Time::NowFromSystemTime() | |
| 65 -base::Time::UnixEpoch()).ToInternalValue(); | |
|
manzagop (departed)
2014/07/31 15:31:49
Indentation. I would go with:
int64 now = (Time::N
gayane -on leave until 09-2017
2014/08/04 13:46:30
I am not sure I did this right.
Should it be 4 spa
| |
| 66 | |
| 45 size_t size = profile.suggestions_size(); | 67 size_t size = profile.suggestions_size(); |
| 46 for (size_t i = 0; i < size; ++i) { | 68 for (size_t i = 0; i < size; ++i) { |
| 69 | |
|
manzagop (departed)
2014/07/31 15:31:49
Remove empty line: http://google-styleguide.google
gayane -on leave until 09-2017
2014/08/04 13:46:30
Done.
| |
| 47 const ChromeSuggestion& suggestion = profile.suggestions(i); | 70 const ChromeSuggestion& suggestion = profile.suggestions(i); |
| 71 int64 diff = (suggestion.expiry_ts()-now); | |
| 72 | |
| 48 std::string line; | 73 std::string line; |
| 49 line += "<li><a href=\""; | 74 line += "<li><a href=\""; |
| 50 line += net::EscapeForHTML(suggestion.url()); | 75 line += net::EscapeForHTML(suggestion.url()); |
| 51 line += "\" target=\"_blank\">"; | 76 line += "\" target=\"_blank\">"; |
| 52 line += net::EscapeForHTML(suggestion.title()); | 77 line += net::EscapeForHTML(suggestion.title()); |
| 53 std::map<GURL, std::string>::const_iterator it = | 78 std::map<GURL, std::string>::const_iterator it = |
| 54 base64_encoded_pngs.find(GURL(suggestion.url())); | 79 base64_encoded_pngs.find(GURL(suggestion.url())); |
| 55 if (it != base64_encoded_pngs.end()) { | 80 if (it != base64_encoded_pngs.end()) { |
| 56 line += "<br><img src='"; | 81 line += "<br><img src='"; |
| 57 line += it->second; | 82 line += it->second; |
| 58 line += "'>"; | 83 line += "'>"; |
| 59 } | 84 } |
| 60 line += "</a></li>\n"; | 85 line += "</a> Expires in "; |
| 86 line += FormatTimeDelta(diff); | |
| 87 | |
| 88 line += "</li>\n"; | |
| 61 out.push_back(line); | 89 out.push_back(line); |
| 62 } | 90 } |
| 63 out.push_back("</ul>"); | 91 out.push_back("</ul>"); |
| 64 out.push_back(kHtmlFooter); | 92 out.push_back(kHtmlFooter); |
| 65 *output = JoinString(out, ""); | 93 *output = JoinString(out, ""); |
| 66 } | 94 } |
| 67 | 95 |
| 68 // Fills |output| with the HTML needed to display that no suggestions are | 96 // Fills |output| with the HTML needed to display that no suggestions are |
| 69 // available. | 97 // available. |
| 70 void RenderOutputHtmlNoSuggestions(std::string* output) { | 98 void RenderOutputHtmlNoSuggestions(std::string* output) { |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 174 std::string encoded_output; | 202 std::string encoded_output; |
| 175 base::Base64Encode(std::string(output.begin(), output.end()), | 203 base::Base64Encode(std::string(output.begin(), output.end()), |
| 176 &encoded_output); | 204 &encoded_output); |
| 177 context->base64_encoded_pngs[url] = "data:image/png;base64,"; | 205 context->base64_encoded_pngs[url] = "data:image/png;base64,"; |
| 178 context->base64_encoded_pngs[url] += encoded_output; | 206 context->base64_encoded_pngs[url] += encoded_output; |
| 179 } | 207 } |
| 180 barrier.Run(); | 208 barrier.Run(); |
| 181 } | 209 } |
| 182 | 210 |
| 183 } // namespace suggestions | 211 } // namespace suggestions |
| OLD | NEW |