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
3 // found in the LICENSE file.
5 #include "components/history/core/browser/history_types.h"
9 #include "base/logging.h"
10 #include "base/stl_util.h"
11 #include "components/history/core/browser/page_usage_data.h"
15 // VisitRow --------------------------------------------------------------------
21 transition(ui::PAGE_TRANSITION_LINK),
25 VisitRow::VisitRow(URLID arg_url_id,
26 base::Time arg_visit_time,
27 VisitID arg_referring_visit,
28 ui::PageTransition arg_transition,
29 SegmentID arg_segment_id)
32 visit_time(arg_visit_time),
33 referring_visit(arg_referring_visit),
34 transition(arg_transition),
35 segment_id(arg_segment_id) {
38 VisitRow::~VisitRow() {
41 // QueryResults ----------------------------------------------------------------
43 QueryResults::QueryResults() : reached_beginning_(false) {
46 QueryResults::~QueryResults() {}
48 const size_t* QueryResults::MatchesForURL(const GURL& url,
49 size_t* num_matches) const {
50 URLToResultIndices::const_iterator found = url_to_results_.find(url);
51 if (found == url_to_results_.end()) {
57 // All entries in the map should have at least one index, otherwise it
58 // shouldn't be in the map.
59 DCHECK(!found->second->empty());
61 *num_matches = found->second->size();
62 return &found->second->front();
65 void QueryResults::Swap(QueryResults* other) {
66 std::swap(first_time_searched_, other->first_time_searched_);
67 std::swap(reached_beginning_, other->reached_beginning_);
68 results_.swap(other->results_);
69 url_to_results_.swap(other->url_to_results_);
72 void QueryResults::AppendURLBySwapping(URLResult* result) {
73 URLResult* new_result = new URLResult;
74 new_result->SwapResult(result);
76 results_.push_back(new_result);
77 AddURLUsageAtIndex(new_result->url(), results_.size() - 1);
80 void QueryResults::DeleteURL(const GURL& url) {
81 // Delete all instances of this URL. We re-query each time since each
82 // mutation will cause the indices to change.
83 while (const size_t* match_indices = MatchesForURL(url, NULL))
84 DeleteRange(*match_indices, *match_indices);
87 void QueryResults::DeleteRange(size_t begin, size_t end) {
88 DCHECK(begin <= end && begin < size() && end < size());
90 // First delete the pointers in the given range and store all the URLs that
91 // were modified. We will delete references to these later.
92 std::set<GURL> urls_modified;
93 for (size_t i = begin; i <= end; i++) {
94 urls_modified.insert(results_[i]->url());
97 // Now just delete that range in the vector en masse (the STL ending is
98 // exclusive, while ours is inclusive, hence the +1).
99 results_.erase(results_.begin() + begin, results_.begin() + end + 1);
101 // Delete the indicies referencing the deleted entries.
102 for (std::set<GURL>::const_iterator url = urls_modified.begin();
103 url != urls_modified.end(); ++url) {
104 URLToResultIndices::iterator found = url_to_results_.find(*url);
105 if (found == url_to_results_.end()) {
110 // Need a signed loop type since we do -- which may take us to -1.
111 for (int match = 0; match < static_cast<int>(found->second->size());
113 if (found->second[match] >= begin && found->second[match] <= end) {
114 // Remove this referece from the list.
115 found->second->erase(found->second->begin() + match);
120 // Clear out an empty lists if we just made one.
121 if (found->second->empty())
122 url_to_results_.erase(found);
125 // Shift all other indices over to account for the removed ones.
126 AdjustResultMap(end + 1, std::numeric_limits<size_t>::max(),
127 -static_cast<ptrdiff_t>(end - begin + 1));
130 void QueryResults::AddURLUsageAtIndex(const GURL& url, size_t index) {
131 URLToResultIndices::iterator found = url_to_results_.find(url);
132 if (found != url_to_results_.end()) {
133 // The URL is already in the list, so we can just append the new index.
134 found->second->push_back(index);
138 // Need to add a new entry for this URL.
139 base::StackVector<size_t, 4> new_list;
140 new_list->push_back(index);
141 url_to_results_[url] = new_list;
144 void QueryResults::AdjustResultMap(size_t begin, size_t end, ptrdiff_t delta) {
145 for (URLToResultIndices::iterator i = url_to_results_.begin();
146 i != url_to_results_.end(); ++i) {
147 for (size_t match = 0; match < i->second->size(); match++) {
148 size_t match_index = i->second[match];
149 if (match_index >= begin && match_index <= end)
150 i->second[match] += delta;
155 // QueryOptions ----------------------------------------------------------------
157 QueryOptions::QueryOptions()
159 duplicate_policy(QueryOptions::REMOVE_ALL_DUPLICATES) {
162 void QueryOptions::SetRecentDayRange(int days_ago) {
163 end_time = base::Time::Now();
164 begin_time = end_time - base::TimeDelta::FromDays(days_ago);
167 int64 QueryOptions::EffectiveBeginTime() const {
168 return begin_time.ToInternalValue();
171 int64 QueryOptions::EffectiveEndTime() const {
172 return end_time.is_null() ?
173 std::numeric_limits<int64>::max() : end_time.ToInternalValue();
176 int QueryOptions::EffectiveMaxCount() const {
177 return max_count ? max_count : std::numeric_limits<int>::max();
180 // QueryURLResult -------------------------------------------------------------
182 QueryURLResult::QueryURLResult() : success(false) {
185 QueryURLResult::~QueryURLResult() {
188 // MostVisitedURL --------------------------------------------------------------
190 MostVisitedURL::MostVisitedURL() {}
192 MostVisitedURL::MostVisitedURL(const GURL& url,
193 const base::string16& title)
198 MostVisitedURL::MostVisitedURL(const GURL& url,
199 const base::string16& title,
200 const base::Time& last_forced_time)
203 last_forced_time(last_forced_time) {
206 MostVisitedURL::~MostVisitedURL() {}
208 // FilteredURL -----------------------------------------------------------------
210 FilteredURL::FilteredURL() : score(0.0) {}
212 FilteredURL::FilteredURL(const PageUsageData& page_data)
213 : url(page_data.GetURL()),
214 title(page_data.GetTitle()),
215 score(page_data.GetScore()) {
218 FilteredURL::~FilteredURL() {}
220 // FilteredURL::ExtendedInfo ---------------------------------------------------
222 FilteredURL::ExtendedInfo::ExtendedInfo()
228 // Images ---------------------------------------------------------------------
234 // TopSitesDelta --------------------------------------------------------------
236 TopSitesDelta::TopSitesDelta() {}
238 TopSitesDelta::~TopSitesDelta() {}
240 // HistoryAddPageArgs ---------------------------------------------------------
242 HistoryAddPageArgs::HistoryAddPageArgs()
245 transition(ui::PAGE_TRANSITION_LINK),
246 visit_source(SOURCE_BROWSED),
247 did_replace_entry(false) {}
249 HistoryAddPageArgs::HistoryAddPageArgs(
252 ContextID context_id,
254 const GURL& referrer,
255 const history::RedirectList& redirects,
256 ui::PageTransition transition,
258 bool did_replace_entry)
261 context_id(context_id),
264 redirects(redirects),
265 transition(transition),
266 visit_source(source),
267 did_replace_entry(did_replace_entry) {
270 HistoryAddPageArgs::~HistoryAddPageArgs() {}
272 // ThumbnailMigration ---------------------------------------------------------
274 ThumbnailMigration::ThumbnailMigration() {}
276 ThumbnailMigration::~ThumbnailMigration() {}
278 // MostVisitedThumbnails ------------------------------------------------------
280 MostVisitedThumbnails::MostVisitedThumbnails() {}
282 MostVisitedThumbnails::~MostVisitedThumbnails() {}
284 // IconMapping ----------------------------------------------------------------
286 IconMapping::IconMapping()
287 : mapping_id(0), icon_id(0), icon_type(favicon_base::INVALID_ICON) {}
289 IconMapping::~IconMapping() {}
291 // FaviconBitmapIDSize ---------------------------------------------------------
293 FaviconBitmapIDSize::FaviconBitmapIDSize()
297 FaviconBitmapIDSize::~FaviconBitmapIDSize() {
300 // FaviconBitmap --------------------------------------------------------------
302 FaviconBitmap::FaviconBitmap()
307 FaviconBitmap::~FaviconBitmap() {
310 // ExpireHistoryArgs ----------------------------------------------------------
312 ExpireHistoryArgs::ExpireHistoryArgs() {
315 ExpireHistoryArgs::~ExpireHistoryArgs() {
318 void ExpireHistoryArgs::SetTimeRangeForOneDay(base::Time time) {
319 begin_time = time.LocalMidnight();
321 // Due to DST, leap seconds, etc., the next day at midnight may be more than
322 // 24 hours away, so add 36 hours and round back down to midnight.
323 end_time = (begin_time + base::TimeDelta::FromHours(36)).LocalMidnight();
326 } // namespace history