| OLD | NEW |
| 1 // Copyright (c) 2012 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/history/history_types.h" | 5 #include "components/history/core/browser/url_row.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <algorithm> |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/stl_util.h" | |
| 11 #include "chrome/browser/history/page_usage_data.h" | |
| 12 | 8 |
| 13 namespace history { | 9 namespace history { |
| 14 | 10 |
| 15 // URLRow ---------------------------------------------------------------------- | |
| 16 | |
| 17 URLRow::URLRow() { | 11 URLRow::URLRow() { |
| 18 Initialize(); | 12 Initialize(); |
| 19 } | 13 } |
| 20 | 14 |
| 21 URLRow::URLRow(const GURL& url) : url_(url) { | 15 URLRow::URLRow(const GURL& url) : url_(url) { |
| 22 // Initialize will not set the URL, so our initialization above will stay. | 16 // Initialize will not set the URL, so our initialization above will stay. |
| 23 Initialize(); | 17 Initialize(); |
| 24 } | 18 } |
| 25 | 19 |
| 26 URLRow::URLRow(const GURL& url, URLID id) : url_(url) { | 20 URLRow::URLRow(const GURL& url, URLID id) : url_(url) { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 55 } | 49 } |
| 56 | 50 |
| 57 void URLRow::Initialize() { | 51 void URLRow::Initialize() { |
| 58 id_ = 0; | 52 id_ = 0; |
| 59 visit_count_ = 0; | 53 visit_count_ = 0; |
| 60 typed_count_ = 0; | 54 typed_count_ = 0; |
| 61 last_visit_ = base::Time(); | 55 last_visit_ = base::Time(); |
| 62 hidden_ = false; | 56 hidden_ = false; |
| 63 } | 57 } |
| 64 | 58 |
| 65 // VisitRow -------------------------------------------------------------------- | |
| 66 | |
| 67 VisitRow::VisitRow() | |
| 68 : visit_id(0), | |
| 69 url_id(0), | |
| 70 referring_visit(0), | |
| 71 transition(content::PAGE_TRANSITION_LINK), | |
| 72 segment_id(0) { | |
| 73 } | |
| 74 | |
| 75 VisitRow::VisitRow(URLID arg_url_id, | |
| 76 base::Time arg_visit_time, | |
| 77 VisitID arg_referring_visit, | |
| 78 content::PageTransition arg_transition, | |
| 79 SegmentID arg_segment_id) | |
| 80 : visit_id(0), | |
| 81 url_id(arg_url_id), | |
| 82 visit_time(arg_visit_time), | |
| 83 referring_visit(arg_referring_visit), | |
| 84 transition(arg_transition), | |
| 85 segment_id(arg_segment_id) { | |
| 86 } | |
| 87 | |
| 88 VisitRow::~VisitRow() { | |
| 89 } | |
| 90 | |
| 91 // URLResult ------------------------------------------------------------------- | |
| 92 | 59 |
| 93 URLResult::URLResult() | 60 URLResult::URLResult() |
| 94 : blocked_visit_(false) { | 61 : blocked_visit_(false) { |
| 95 } | 62 } |
| 96 | 63 |
| 97 URLResult::URLResult(const GURL& url, base::Time visit_time) | 64 URLResult::URLResult(const GURL& url, base::Time visit_time) |
| 98 : URLRow(url), | 65 : URLRow(url), |
| 99 visit_time_(visit_time), | 66 visit_time_(visit_time), |
| 100 blocked_visit_(false) { | 67 blocked_visit_(false) { |
| 101 } | 68 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 119 snippet_.Swap(&other->snippet_); | 86 snippet_.Swap(&other->snippet_); |
| 120 title_match_positions_.swap(other->title_match_positions_); | 87 title_match_positions_.swap(other->title_match_positions_); |
| 121 std::swap(blocked_visit_, other->blocked_visit_); | 88 std::swap(blocked_visit_, other->blocked_visit_); |
| 122 } | 89 } |
| 123 | 90 |
| 124 // static | 91 // static |
| 125 bool URLResult::CompareVisitTime(const URLResult& lhs, const URLResult& rhs) { | 92 bool URLResult::CompareVisitTime(const URLResult& lhs, const URLResult& rhs) { |
| 126 return lhs.visit_time() > rhs.visit_time(); | 93 return lhs.visit_time() > rhs.visit_time(); |
| 127 } | 94 } |
| 128 | 95 |
| 129 // QueryResults ---------------------------------------------------------------- | |
| 130 | |
| 131 QueryResults::QueryResults() : reached_beginning_(false) { | |
| 132 } | |
| 133 | |
| 134 QueryResults::~QueryResults() {} | |
| 135 | |
| 136 const size_t* QueryResults::MatchesForURL(const GURL& url, | |
| 137 size_t* num_matches) const { | |
| 138 URLToResultIndices::const_iterator found = url_to_results_.find(url); | |
| 139 if (found == url_to_results_.end()) { | |
| 140 if (num_matches) | |
| 141 *num_matches = 0; | |
| 142 return NULL; | |
| 143 } | |
| 144 | |
| 145 // All entries in the map should have at least one index, otherwise it | |
| 146 // shouldn't be in the map. | |
| 147 DCHECK(!found->second->empty()); | |
| 148 if (num_matches) | |
| 149 *num_matches = found->second->size(); | |
| 150 return &found->second->front(); | |
| 151 } | |
| 152 | |
| 153 void QueryResults::Swap(QueryResults* other) { | |
| 154 std::swap(first_time_searched_, other->first_time_searched_); | |
| 155 std::swap(reached_beginning_, other->reached_beginning_); | |
| 156 results_.swap(other->results_); | |
| 157 url_to_results_.swap(other->url_to_results_); | |
| 158 } | |
| 159 | |
| 160 void QueryResults::AppendURLBySwapping(URLResult* result) { | |
| 161 URLResult* new_result = new URLResult; | |
| 162 new_result->SwapResult(result); | |
| 163 | |
| 164 results_.push_back(new_result); | |
| 165 AddURLUsageAtIndex(new_result->url(), results_.size() - 1); | |
| 166 } | |
| 167 | |
| 168 void QueryResults::DeleteURL(const GURL& url) { | |
| 169 // Delete all instances of this URL. We re-query each time since each | |
| 170 // mutation will cause the indices to change. | |
| 171 while (const size_t* match_indices = MatchesForURL(url, NULL)) | |
| 172 DeleteRange(*match_indices, *match_indices); | |
| 173 } | |
| 174 | |
| 175 void QueryResults::DeleteRange(size_t begin, size_t end) { | |
| 176 DCHECK(begin <= end && begin < size() && end < size()); | |
| 177 | |
| 178 // First delete the pointers in the given range and store all the URLs that | |
| 179 // were modified. We will delete references to these later. | |
| 180 std::set<GURL> urls_modified; | |
| 181 for (size_t i = begin; i <= end; i++) { | |
| 182 urls_modified.insert(results_[i]->url()); | |
| 183 } | |
| 184 | |
| 185 // Now just delete that range in the vector en masse (the STL ending is | |
| 186 // exclusive, while ours is inclusive, hence the +1). | |
| 187 results_.erase(results_.begin() + begin, results_.begin() + end + 1); | |
| 188 | |
| 189 // Delete the indicies referencing the deleted entries. | |
| 190 for (std::set<GURL>::const_iterator url = urls_modified.begin(); | |
| 191 url != urls_modified.end(); ++url) { | |
| 192 URLToResultIndices::iterator found = url_to_results_.find(*url); | |
| 193 if (found == url_to_results_.end()) { | |
| 194 NOTREACHED(); | |
| 195 continue; | |
| 196 } | |
| 197 | |
| 198 // Need a signed loop type since we do -- which may take us to -1. | |
| 199 for (int match = 0; match < static_cast<int>(found->second->size()); | |
| 200 match++) { | |
| 201 if (found->second[match] >= begin && found->second[match] <= end) { | |
| 202 // Remove this referece from the list. | |
| 203 found->second->erase(found->second->begin() + match); | |
| 204 match--; | |
| 205 } | |
| 206 } | |
| 207 | |
| 208 // Clear out an empty lists if we just made one. | |
| 209 if (found->second->empty()) | |
| 210 url_to_results_.erase(found); | |
| 211 } | |
| 212 | |
| 213 // Shift all other indices over to account for the removed ones. | |
| 214 AdjustResultMap(end + 1, std::numeric_limits<size_t>::max(), | |
| 215 -static_cast<ptrdiff_t>(end - begin + 1)); | |
| 216 } | |
| 217 | |
| 218 void QueryResults::AddURLUsageAtIndex(const GURL& url, size_t index) { | |
| 219 URLToResultIndices::iterator found = url_to_results_.find(url); | |
| 220 if (found != url_to_results_.end()) { | |
| 221 // The URL is already in the list, so we can just append the new index. | |
| 222 found->second->push_back(index); | |
| 223 return; | |
| 224 } | |
| 225 | |
| 226 // Need to add a new entry for this URL. | |
| 227 base::StackVector<size_t, 4> new_list; | |
| 228 new_list->push_back(index); | |
| 229 url_to_results_[url] = new_list; | |
| 230 } | |
| 231 | |
| 232 void QueryResults::AdjustResultMap(size_t begin, size_t end, ptrdiff_t delta) { | |
| 233 for (URLToResultIndices::iterator i = url_to_results_.begin(); | |
| 234 i != url_to_results_.end(); ++i) { | |
| 235 for (size_t match = 0; match < i->second->size(); match++) { | |
| 236 size_t match_index = i->second[match]; | |
| 237 if (match_index >= begin && match_index <= end) | |
| 238 i->second[match] += delta; | |
| 239 } | |
| 240 } | |
| 241 } | |
| 242 | |
| 243 // QueryOptions ---------------------------------------------------------------- | |
| 244 | |
| 245 QueryOptions::QueryOptions() | |
| 246 : max_count(0), | |
| 247 duplicate_policy(QueryOptions::REMOVE_ALL_DUPLICATES) { | |
| 248 } | |
| 249 | |
| 250 void QueryOptions::SetRecentDayRange(int days_ago) { | |
| 251 end_time = base::Time::Now(); | |
| 252 begin_time = end_time - base::TimeDelta::FromDays(days_ago); | |
| 253 } | |
| 254 | |
| 255 int64 QueryOptions::EffectiveBeginTime() const { | |
| 256 return begin_time.ToInternalValue(); | |
| 257 } | |
| 258 | |
| 259 int64 QueryOptions::EffectiveEndTime() const { | |
| 260 return end_time.is_null() ? | |
| 261 std::numeric_limits<int64>::max() : end_time.ToInternalValue(); | |
| 262 } | |
| 263 | |
| 264 int QueryOptions::EffectiveMaxCount() const { | |
| 265 return max_count ? max_count : std::numeric_limits<int>::max(); | |
| 266 } | |
| 267 | |
| 268 // QueryURLResult ------------------------------------------------------------- | |
| 269 | |
| 270 QueryURLResult::QueryURLResult() : success(false) { | |
| 271 } | |
| 272 | |
| 273 QueryURLResult::~QueryURLResult() { | |
| 274 } | |
| 275 | |
| 276 // KeywordSearchTermVisit ----------------------------------------------------- | |
| 277 | |
| 278 KeywordSearchTermVisit::KeywordSearchTermVisit() : visits(0) {} | |
| 279 | |
| 280 KeywordSearchTermVisit::~KeywordSearchTermVisit() {} | |
| 281 | |
| 282 // KeywordSearchTermRow -------------------------------------------------------- | |
| 283 | |
| 284 KeywordSearchTermRow::KeywordSearchTermRow() : keyword_id(0), url_id(0) {} | |
| 285 | |
| 286 KeywordSearchTermRow::~KeywordSearchTermRow() {} | |
| 287 | |
| 288 // MostVisitedURL -------------------------------------------------------------- | |
| 289 | |
| 290 MostVisitedURL::MostVisitedURL() {} | |
| 291 | |
| 292 MostVisitedURL::MostVisitedURL(const GURL& url, | |
| 293 const base::string16& title) | |
| 294 : url(url), | |
| 295 title(title) { | |
| 296 } | |
| 297 | |
| 298 MostVisitedURL::MostVisitedURL(const GURL& url, | |
| 299 const base::string16& title, | |
| 300 const base::Time& last_forced_time) | |
| 301 : url(url), | |
| 302 title(title), | |
| 303 last_forced_time(last_forced_time) { | |
| 304 } | |
| 305 | |
| 306 MostVisitedURL::~MostVisitedURL() {} | |
| 307 | |
| 308 // FilteredURL ----------------------------------------------------------------- | |
| 309 | |
| 310 FilteredURL::FilteredURL() : score(0.0) {} | |
| 311 | |
| 312 FilteredURL::FilteredURL(const PageUsageData& page_data) | |
| 313 : url(page_data.GetURL()), | |
| 314 title(page_data.GetTitle()), | |
| 315 score(page_data.GetScore()) { | |
| 316 } | |
| 317 | |
| 318 FilteredURL::~FilteredURL() {} | |
| 319 | |
| 320 // FilteredURL::ExtendedInfo --------------------------------------------------- | |
| 321 | |
| 322 FilteredURL::ExtendedInfo::ExtendedInfo() | |
| 323 : total_visits(0), | |
| 324 visits(0), | |
| 325 duration_opened(0) { | |
| 326 } | |
| 327 | |
| 328 // Images --------------------------------------------------------------------- | |
| 329 | |
| 330 Images::Images() {} | |
| 331 | |
| 332 Images::~Images() {} | |
| 333 | |
| 334 // TopSitesDelta -------------------------------------------------------------- | |
| 335 | |
| 336 TopSitesDelta::TopSitesDelta() {} | |
| 337 | |
| 338 TopSitesDelta::~TopSitesDelta() {} | |
| 339 | |
| 340 // HistoryAddPageArgs --------------------------------------------------------- | |
| 341 | |
| 342 HistoryAddPageArgs::HistoryAddPageArgs() | |
| 343 : context_id(NULL), | |
| 344 page_id(0), | |
| 345 transition(content::PAGE_TRANSITION_LINK), | |
| 346 visit_source(SOURCE_BROWSED), | |
| 347 did_replace_entry(false) {} | |
| 348 | |
| 349 HistoryAddPageArgs::HistoryAddPageArgs( | |
| 350 const GURL& url, | |
| 351 base::Time time, | |
| 352 ContextID context_id, | |
| 353 int32 page_id, | |
| 354 const GURL& referrer, | |
| 355 const history::RedirectList& redirects, | |
| 356 content::PageTransition transition, | |
| 357 VisitSource source, | |
| 358 bool did_replace_entry) | |
| 359 : url(url), | |
| 360 time(time), | |
| 361 context_id(context_id), | |
| 362 page_id(page_id), | |
| 363 referrer(referrer), | |
| 364 redirects(redirects), | |
| 365 transition(transition), | |
| 366 visit_source(source), | |
| 367 did_replace_entry(did_replace_entry) { | |
| 368 } | |
| 369 | |
| 370 HistoryAddPageArgs::~HistoryAddPageArgs() {} | |
| 371 | |
| 372 ThumbnailMigration::ThumbnailMigration() {} | |
| 373 | |
| 374 ThumbnailMigration::~ThumbnailMigration() {} | |
| 375 | |
| 376 MostVisitedThumbnails::MostVisitedThumbnails() {} | |
| 377 | |
| 378 MostVisitedThumbnails::~MostVisitedThumbnails() {} | |
| 379 | |
| 380 // Autocomplete thresholds ----------------------------------------------------- | |
| 381 | |
| 382 const int kLowQualityMatchTypedLimit = 1; | |
| 383 const int kLowQualityMatchVisitLimit = 4; | |
| 384 const int kLowQualityMatchAgeLimitInDays = 3; | |
| 385 | |
| 386 base::Time AutocompleteAgeThreshold() { | |
| 387 return (base::Time::Now() - | |
| 388 base::TimeDelta::FromDays(kLowQualityMatchAgeLimitInDays)); | |
| 389 } | |
| 390 | |
| 391 bool RowQualifiesAsSignificant(const URLRow& row, | |
| 392 const base::Time& threshold) { | |
| 393 const base::Time& real_threshold = | |
| 394 threshold.is_null() ? AutocompleteAgeThreshold() : threshold; | |
| 395 return (row.typed_count() >= kLowQualityMatchTypedLimit) || | |
| 396 (row.visit_count() >= kLowQualityMatchVisitLimit) || | |
| 397 (row.last_visit() >= real_threshold); | |
| 398 } | |
| 399 | |
| 400 // IconMapping ---------------------------------------------------------------- | |
| 401 | |
| 402 IconMapping::IconMapping() | |
| 403 : mapping_id(0), icon_id(0), icon_type(favicon_base::INVALID_ICON) {} | |
| 404 | |
| 405 IconMapping::~IconMapping() {} | |
| 406 | |
| 407 // FaviconBitmapIDSize --------------------------------------------------------- | |
| 408 | |
| 409 FaviconBitmapIDSize::FaviconBitmapIDSize() | |
| 410 : bitmap_id(0) { | |
| 411 } | |
| 412 | |
| 413 FaviconBitmapIDSize::~FaviconBitmapIDSize() { | |
| 414 } | |
| 415 | |
| 416 // FaviconBitmap -------------------------------------------------------------- | |
| 417 | |
| 418 FaviconBitmap::FaviconBitmap() | |
| 419 : bitmap_id(0), | |
| 420 icon_id(0) { | |
| 421 } | |
| 422 | |
| 423 FaviconBitmap::~FaviconBitmap() { | |
| 424 } | |
| 425 | |
| 426 // VisitDatabaseObserver ------------------------------------------------------- | |
| 427 | |
| 428 VisitDatabaseObserver::~VisitDatabaseObserver() {} | |
| 429 | |
| 430 ExpireHistoryArgs::ExpireHistoryArgs() { | |
| 431 } | |
| 432 | |
| 433 ExpireHistoryArgs::~ExpireHistoryArgs() { | |
| 434 } | |
| 435 | |
| 436 void ExpireHistoryArgs::SetTimeRangeForOneDay(base::Time time) { | |
| 437 begin_time = time.LocalMidnight(); | |
| 438 | |
| 439 // Due to DST, leap seconds, etc., the next day at midnight may be more than | |
| 440 // 24 hours away, so add 36 hours and round back down to midnight. | |
| 441 end_time = (begin_time + base::TimeDelta::FromHours(36)).LocalMidnight(); | |
| 442 } | |
| 443 | |
| 444 } // namespace history | 96 } // namespace history |
| OLD | NEW |