OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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. |
| 4 |
| 5 #include "ios/chrome/browser/ui/history/history_util.h" |
| 6 |
| 7 #include <map> |
| 8 |
| 9 #include "base/i18n/time_formatting.h" |
| 10 #include "base/time/time.h" |
| 11 #include "components/strings/grit/components_strings.h" |
| 12 #include "ios/chrome/browser/ui/history/history_entry.h" |
| 13 #include "ui/base/l10n/l10n_util.h" |
| 14 #include "ui/base/l10n/time_format.h" |
| 15 #include "url/gurl.h" |
| 16 |
| 17 namespace history { |
| 18 |
| 19 base::string16 GetRelativeDateLocalized(const base::Time& visit_time) { |
| 20 base::Time midnight = base::Time::Now().LocalMidnight(); |
| 21 base::string16 date_str = ui::TimeFormat::RelativeDate(visit_time, &midnight); |
| 22 if (date_str.empty()) { |
| 23 date_str = base::TimeFormatFriendlyDate(visit_time); |
| 24 } else { |
| 25 date_str = l10n_util::GetStringFUTF16( |
| 26 IDS_HISTORY_DATE_WITH_RELATIVE_TIME, date_str, |
| 27 base::TimeFormatFriendlyDate(visit_time)); |
| 28 } |
| 29 return date_str; |
| 30 } |
| 31 |
| 32 void MergeDuplicateHistoryEntries(std::vector<history::HistoryEntry>* entries) { |
| 33 std::vector<history::HistoryEntry> new_entries; |
| 34 // Pre-reserve the size of the new vector. Since we're working with pointers |
| 35 // later on not doing this could lead to the vector being resized and to |
| 36 // pointers to invalid locations. |
| 37 new_entries.reserve(entries->size()); |
| 38 // Maps a URL to the most recent entry on a particular day. |
| 39 std::map<GURL, history::HistoryEntry*> current_day_entries; |
| 40 |
| 41 // Keeps track of the day that |current_day_urls| is holding the URLs for, |
| 42 // in order to handle removing per-day duplicates. |
| 43 base::Time current_day_midnight; |
| 44 |
| 45 std::sort(entries->begin(), entries->end(), |
| 46 history::HistoryEntry::SortByTimeDescending); |
| 47 |
| 48 for (const history::HistoryEntry& entry : *entries) { |
| 49 // Reset the list of found URLs when a visit from a new day is encountered. |
| 50 if (current_day_midnight != entry.time.LocalMidnight()) { |
| 51 current_day_entries.clear(); |
| 52 current_day_midnight = entry.time.LocalMidnight(); |
| 53 } |
| 54 |
| 55 // Keep this visit if it's the first visit to this URL on the current day. |
| 56 if (current_day_entries.count(entry.url) == 0) { |
| 57 new_entries.push_back(entry); |
| 58 current_day_entries[entry.url] = &new_entries.back(); |
| 59 } else { |
| 60 // Keep track of the timestamps of all visits to the URL on the same day. |
| 61 history::HistoryEntry* combined_entry = current_day_entries[entry.url]; |
| 62 combined_entry->all_timestamps.insert(entry.all_timestamps.begin(), |
| 63 entry.all_timestamps.end()); |
| 64 |
| 65 if (combined_entry->entry_type != entry.entry_type) { |
| 66 combined_entry->entry_type = history::HistoryEntry::COMBINED_ENTRY; |
| 67 } |
| 68 } |
| 69 } |
| 70 entries->swap(new_entries); |
| 71 } |
| 72 } // namespace history |
OLD | NEW |