summaryrefslogtreecommitdiff
path: root/chromium/components/history/core/browser/history_types.cc
blob: aae0d2212673819b5ffacdbfe489eecbc31d9c48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/history/core/browser/history_types.h"

#include <limits>

#include "base/logging.h"
#include "base/stl_util.h"
#include "components/history/core/browser/page_usage_data.h"

namespace history {

// VisitRow --------------------------------------------------------------------

VisitRow::VisitRow() {}

VisitRow::VisitRow(URLID arg_url_id,
                   base::Time arg_visit_time,
                   VisitID arg_referring_visit,
                   ui::PageTransition arg_transition,
                   SegmentID arg_segment_id,
                   bool arg_incremented_omnibox_typed_score)
    : url_id(arg_url_id),
      visit_time(arg_visit_time),
      referring_visit(arg_referring_visit),
      transition(arg_transition),
      segment_id(arg_segment_id),
      incremented_omnibox_typed_score(arg_incremented_omnibox_typed_score) {}

VisitRow::~VisitRow() {
}

// QueryResults ----------------------------------------------------------------

QueryResults::QueryResults() : reached_beginning_(false) {
}

QueryResults::~QueryResults() {}

const size_t* QueryResults::MatchesForURL(const GURL& url,
                                          size_t* num_matches) const {
  URLToResultIndices::const_iterator found = url_to_results_.find(url);
  if (found == url_to_results_.end()) {
    if (num_matches)
      *num_matches = 0;
    return nullptr;
  }

  // All entries in the map should have at least one index, otherwise it
  // shouldn't be in the map.
  DCHECK(!found->second->empty());
  if (num_matches)
    *num_matches = found->second->size();
  return &found->second->front();
}

void QueryResults::Swap(QueryResults* other) {
  std::swap(reached_beginning_, other->reached_beginning_);
  results_.swap(other->results_);
  url_to_results_.swap(other->url_to_results_);
}

void QueryResults::SetURLResults(std::vector<URLResult>&& results) {
  results_ = std::move(results);

  // Recreate the map for the results_ has been replaced.
  url_to_results_.clear();
  for(size_t i = 0; i < results_.size(); ++i)
    AddURLUsageAtIndex(results_[i].url(), i);
}

void QueryResults::DeleteURL(const GURL& url) {
  // Delete all instances of this URL. We re-query each time since each
  // mutation will cause the indices to change.
  while (const size_t* match_indices = MatchesForURL(url, nullptr))
    DeleteRange(*match_indices, *match_indices);
}

void QueryResults::DeleteRange(size_t begin, size_t end) {
  DCHECK(begin <= end && begin < size() && end < size());

  // First delete the pointers in the given range and store all the URLs that
  // were modified. We will delete references to these later.
  std::set<GURL> urls_modified;
  for (size_t i = begin; i <= end; i++) {
    urls_modified.insert(results_[i].url());
  }

  // Now just delete that range in the vector en masse (the STL ending is
  // exclusive, while ours is inclusive, hence the +1).
  results_.erase(results_.begin() + begin, results_.begin() + end + 1);

  // Delete the indicies referencing the deleted entries.
  for (const auto& url : urls_modified) {
    URLToResultIndices::iterator found = url_to_results_.find(url);
    if (found == url_to_results_.end()) {
      NOTREACHED();
      continue;
    }

    // Need a signed loop type since we do -- which may take us to -1.
    for (int match = 0; match < static_cast<int>(found->second->size());
         match++) {
      if (found->second[match] >= begin && found->second[match] <= end) {
        // Remove this referece from the list.
        found->second->erase(found->second->begin() + match);
        match--;
      }
    }

    // Clear out an empty lists if we just made one.
    if (found->second->empty())
      url_to_results_.erase(found);
  }

  // Shift all other indices over to account for the removed ones.
  AdjustResultMap(end + 1, std::numeric_limits<size_t>::max(),
                  -static_cast<ptrdiff_t>(end - begin + 1));
}

void QueryResults::AddURLUsageAtIndex(const GURL& url, size_t index) {
  URLToResultIndices::iterator found = url_to_results_.find(url);
  if (found != url_to_results_.end()) {
    // The URL is already in the list, so we can just append the new index.
    found->second->push_back(index);
    return;
  }

  // Need to add a new entry for this URL.
  base::StackVector<size_t, 4> new_list;
  new_list->push_back(index);
  url_to_results_[url] = new_list;
}

void QueryResults::AdjustResultMap(size_t begin, size_t end, ptrdiff_t delta) {
  for (URLToResultIndices::iterator i = url_to_results_.begin();
       i != url_to_results_.end(); ++i) {
    for (size_t match = 0; match < i->second->size(); match++) {
      size_t match_index = i->second[match];
      if (match_index >= begin && match_index <= end)
        i->second[match] += delta;
    }
  }
}

// QueryOptions ----------------------------------------------------------------

QueryOptions::QueryOptions() {}

void QueryOptions::SetRecentDayRange(int days_ago) {
  end_time = base::Time::Now();
  begin_time = end_time - base::TimeDelta::FromDays(days_ago);
}

int64_t QueryOptions::EffectiveBeginTime() const {
  return begin_time.ToInternalValue();
}

int64_t QueryOptions::EffectiveEndTime() const {
  return end_time.is_null() ? std::numeric_limits<int64_t>::max()
                            : end_time.ToInternalValue();
}

int QueryOptions::EffectiveMaxCount() const {
  return max_count ? max_count : std::numeric_limits<int>::max();
}

// QueryURLResult -------------------------------------------------------------

QueryURLResult::QueryURLResult() {}

QueryURLResult::~QueryURLResult() {
}

// MostVisitedURL --------------------------------------------------------------

MostVisitedURL::MostVisitedURL() {}

MostVisitedURL::MostVisitedURL(const GURL& url,
                               const base::string16& title,
                               base::Time last_forced_time)
    : url(url), title(title), last_forced_time(last_forced_time) {}

MostVisitedURL::MostVisitedURL(const GURL& url,
                               const base::string16& title,
                               const RedirectList& preceding_redirects)
    : url(url), title(title) {
  InitRedirects(preceding_redirects);
}

MostVisitedURL::MostVisitedURL(const MostVisitedURL& other) = default;

MostVisitedURL::MostVisitedURL(MostVisitedURL&& other) noexcept = default;

MostVisitedURL::~MostVisitedURL() {}

void MostVisitedURL::InitRedirects(const RedirectList& redirects_from) {
  redirects.clear();

  if (redirects_from.empty()) {
    // Redirects must contain at least the target URL.
    redirects.push_back(url);
  } else {
    redirects = redirects_from;
    if (redirects.back() != url) {
      // The last url must be the target URL.
      redirects.push_back(url);
    }
  }
}

MostVisitedURL& MostVisitedURL::operator=(const MostVisitedURL&) = default;

// FilteredURL -----------------------------------------------------------------

FilteredURL::FilteredURL() {}

FilteredURL::FilteredURL(const PageUsageData& page_data)
    : url(page_data.GetURL()),
      title(page_data.GetTitle()),
      score(page_data.GetScore()) {
}

FilteredURL::FilteredURL(FilteredURL&& other) noexcept = default;

FilteredURL::~FilteredURL() {}

// FilteredURL::ExtendedInfo ---------------------------------------------------

FilteredURL::ExtendedInfo::ExtendedInfo() = default;

// Images ---------------------------------------------------------------------

Images::Images() {}

Images::Images(const Images& other) = default;

Images::~Images() {}

// TopSitesDelta --------------------------------------------------------------

TopSitesDelta::TopSitesDelta() {}

TopSitesDelta::TopSitesDelta(const TopSitesDelta& other) = default;

TopSitesDelta::~TopSitesDelta() {}

// HistoryAddPageArgs ---------------------------------------------------------

HistoryAddPageArgs::HistoryAddPageArgs()
    : HistoryAddPageArgs(GURL(),
                         base::Time(),
                         nullptr,
                         0,
                         GURL(),
                         RedirectList(),
                         ui::PAGE_TRANSITION_LINK,
                         false,
                         SOURCE_BROWSED,
                         false,
                         true,
                         base::nullopt) {}

HistoryAddPageArgs::HistoryAddPageArgs(const GURL& url,
                                       base::Time time,
                                       ContextID context_id,
                                       int nav_entry_id,
                                       const GURL& referrer,
                                       const RedirectList& redirects,
                                       ui::PageTransition transition,
                                       bool hidden,
                                       VisitSource source,
                                       bool did_replace_entry,
                                       bool consider_for_ntp_most_visited,
                                       base::Optional<base::string16> title)
    : url(url),
      time(time),
      context_id(context_id),
      nav_entry_id(nav_entry_id),
      referrer(referrer),
      redirects(redirects),
      transition(transition),
      hidden(hidden),
      visit_source(source),
      did_replace_entry(did_replace_entry),
      consider_for_ntp_most_visited(consider_for_ntp_most_visited),
      title(title) {}

HistoryAddPageArgs::HistoryAddPageArgs(const HistoryAddPageArgs& other) =
    default;

HistoryAddPageArgs::~HistoryAddPageArgs() {}

// ThumbnailMigration ---------------------------------------------------------

ThumbnailMigration::ThumbnailMigration() {}

ThumbnailMigration::~ThumbnailMigration() {}

// MostVisitedThumbnails ------------------------------------------------------

MostVisitedThumbnails::MostVisitedThumbnails() {}

MostVisitedThumbnails::~MostVisitedThumbnails() {}

// IconMapping ----------------------------------------------------------------

IconMapping::IconMapping() {}
IconMapping::IconMapping(const IconMapping&) = default;
IconMapping::IconMapping(IconMapping&&) noexcept = default;

IconMapping::~IconMapping() {}

IconMapping& IconMapping::operator=(const IconMapping&) = default;

// FaviconBitmapIDSize ---------------------------------------------------------

FaviconBitmapIDSize::FaviconBitmapIDSize() {}

FaviconBitmapIDSize::~FaviconBitmapIDSize() {}

// IconMappingsForExpiry ------------------------------------------------------

IconMappingsForExpiry::IconMappingsForExpiry() {}

IconMappingsForExpiry::IconMappingsForExpiry(
    const IconMappingsForExpiry& other) = default;

IconMappingsForExpiry::~IconMappingsForExpiry() {}

// FaviconBitmap --------------------------------------------------------------

FaviconBitmap::FaviconBitmap() {}

FaviconBitmap::FaviconBitmap(const FaviconBitmap& other) = default;

FaviconBitmap::~FaviconBitmap() {}

// ExpireHistoryArgs ----------------------------------------------------------

ExpireHistoryArgs::ExpireHistoryArgs() {
}

ExpireHistoryArgs::ExpireHistoryArgs(const ExpireHistoryArgs& other) = default;

ExpireHistoryArgs::~ExpireHistoryArgs() {
}

void ExpireHistoryArgs::SetTimeRangeForOneDay(base::Time time) {
  begin_time = time.LocalMidnight();

  // Due to DST, leap seconds, etc., the next day at midnight may be more than
  // 24 hours away, so add 36 hours and round back down to midnight.
  end_time = (begin_time + base::TimeDelta::FromHours(36)).LocalMidnight();
}

// DeletionTimeRange ----------------------------------------------------------

DeletionTimeRange DeletionTimeRange::Invalid() {
  return DeletionTimeRange();
}

DeletionTimeRange DeletionTimeRange::AllTime() {
  return DeletionTimeRange(base::Time(), base::Time::Max());
}

bool DeletionTimeRange::IsValid() const {
  return end_.is_null() || begin_ <= end_;
}

bool DeletionTimeRange::IsAllTime() const {
  return begin_.is_null() && (end_.is_null() || end_.is_max());
}

// DeletionInfo
// ----------------------------------------------------------

// static
DeletionInfo DeletionInfo::ForAllHistory() {
  return DeletionInfo(DeletionTimeRange::AllTime(), false, {}, {},
                      base::nullopt);
}

// static
DeletionInfo DeletionInfo::ForUrls(URLRows deleted_rows,
                                   std::set<GURL> favicon_urls) {
  return DeletionInfo(DeletionTimeRange::Invalid(), false,
                      std::move(deleted_rows), std::move(favicon_urls),
                      base::nullopt);
}

DeletionInfo::DeletionInfo(const DeletionTimeRange& time_range,
                           bool is_from_expiration,
                           URLRows deleted_rows,
                           std::set<GURL> favicon_urls,
                           base::Optional<std::set<GURL>> restrict_urls)
    : time_range_(time_range),
      is_from_expiration_(is_from_expiration),
      deleted_rows_(std::move(deleted_rows)),
      favicon_urls_(std::move(favicon_urls)),
      restrict_urls_(std::move(restrict_urls)) {
  // If time_range is all time or invalid, restrict_urls should be empty.
  DCHECK(!time_range_.IsAllTime() || !restrict_urls_.has_value());
  DCHECK(time_range_.IsValid() || !restrict_urls_.has_value());
  // If restrict_urls_ is defined, it should be non-empty.
  DCHECK(!restrict_urls_.has_value() || !restrict_urls_->empty());
};

DeletionInfo::~DeletionInfo() = default;

DeletionInfo::DeletionInfo(DeletionInfo&& other) noexcept = default;

DeletionInfo& DeletionInfo::operator=(DeletionInfo&& rhs) noexcept = default;

}  // namespace history