OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/text_database_manager.h" | 5 #include "chrome/browser/history/text_database_manager.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
(...skipping 14 matching lines...) Expand all Loading... |
26 // The number of database files we will be attached to at once. | 26 // The number of database files we will be attached to at once. |
27 const int kCacheDBSize = 5; | 27 const int kCacheDBSize = 5; |
28 | 28 |
29 std::string ConvertStringForIndexer(const string16& input) { | 29 std::string ConvertStringForIndexer(const string16& input) { |
30 // TODO(evanm): other transformations here? | 30 // TODO(evanm): other transformations here? |
31 return UTF16ToUTF8(CollapseWhitespace(input, false)); | 31 return UTF16ToUTF8(CollapseWhitespace(input, false)); |
32 } | 32 } |
33 | 33 |
34 // Data older than this will be committed to the full text index even if we | 34 // Data older than this will be committed to the full text index even if we |
35 // haven't gotten a title and/or body. | 35 // haven't gotten a title and/or body. |
36 const int kExpirationSeconds = 20; | 36 const int kExpirationSec = 20; |
37 | 37 |
38 } // namespace | 38 } // namespace |
39 | 39 |
40 // TextDatabaseManager::ChangeSet ---------------------------------------------- | 40 // TextDatabaseManager::ChangeSet ---------------------------------------------- |
41 | 41 |
42 TextDatabaseManager::ChangeSet::ChangeSet() {} | 42 TextDatabaseManager::ChangeSet::ChangeSet() {} |
43 | 43 |
44 TextDatabaseManager::ChangeSet::~ChangeSet() {} | 44 TextDatabaseManager::ChangeSet::~ChangeSet() {} |
45 | 45 |
46 // TextDatabaseManager::PageInfo ----------------------------------------------- | 46 // TextDatabaseManager::PageInfo ----------------------------------------------- |
(...skipping 17 matching lines...) Expand all Loading... |
64 } | 64 } |
65 | 65 |
66 void TextDatabaseManager::PageInfo::set_body(const string16& bdy) { | 66 void TextDatabaseManager::PageInfo::set_body(const string16& bdy) { |
67 if (bdy.empty()) // Make the body nonempty when we set it for EverybodySet. | 67 if (bdy.empty()) // Make the body nonempty when we set it for EverybodySet. |
68 body_ = ASCIIToUTF16(" "); | 68 body_ = ASCIIToUTF16(" "); |
69 else | 69 else |
70 body_ = bdy; | 70 body_ = bdy; |
71 } | 71 } |
72 | 72 |
73 bool TextDatabaseManager::PageInfo::Expired(TimeTicks now) const { | 73 bool TextDatabaseManager::PageInfo::Expired(TimeTicks now) const { |
74 return now - added_time_ > base::TimeDelta::FromSeconds(kExpirationSeconds); | 74 return now - added_time_ > TimeDelta::FromSeconds(kExpirationSec); |
75 } | 75 } |
76 | 76 |
77 // TextDatabaseManager --------------------------------------------------------- | 77 // TextDatabaseManager --------------------------------------------------------- |
78 | 78 |
79 TextDatabaseManager::TextDatabaseManager(const FilePath& dir, | 79 TextDatabaseManager::TextDatabaseManager(const FilePath& dir, |
80 URLDatabase* url_database, | 80 URLDatabase* url_database, |
81 VisitDatabase* visit_database) | 81 VisitDatabase* visit_database) |
82 : dir_(dir), | 82 : dir_(dir), |
83 url_database_(url_database), | 83 url_database_(url_database), |
84 visit_database_(visit_database), | 84 visit_database_(visit_database), |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 if (!visit_database_->GetMostRecentVisitForURL(url_row.id(), &visit)) | 201 if (!visit_database_->GetMostRecentVisitForURL(url_row.id(), &visit)) |
202 return; // No recent visit, give up. | 202 return; // No recent visit, give up. |
203 | 203 |
204 if (visit.is_indexed) { | 204 if (visit.is_indexed) { |
205 // If this page was already indexed, we could have a body that came in | 205 // If this page was already indexed, we could have a body that came in |
206 // first and we don't want to overwrite it. We could go query for the | 206 // first and we don't want to overwrite it. We could go query for the |
207 // current body, or have a special setter for only the title, but this is | 207 // current body, or have a special setter for only the title, but this is |
208 // not worth it for this edge case. | 208 // not worth it for this edge case. |
209 // | 209 // |
210 // It will be almost impossible for the title to take longer than | 210 // It will be almost impossible for the title to take longer than |
211 // kExpirationSeconds yet we got a body in less than that time, since | 211 // kExpirationSec yet we got a body in less than that time, since the |
212 // the title should always come in first. | 212 // title should always come in first. |
213 return; | 213 return; |
214 } | 214 } |
215 | 215 |
216 AddPageData(url, url_row.id(), visit.visit_id, visit.visit_time, | 216 AddPageData(url, url_row.id(), visit.visit_id, visit.visit_time, |
217 title, string16()); | 217 title, string16()); |
218 return; // We don't know about this page, give up. | 218 return; // We don't know about this page, give up. |
219 } | 219 } |
220 | 220 |
221 PageInfo& info = found->second; | 221 PageInfo& info = found->second; |
222 if (info.has_body()) { | 222 if (info.has_body()) { |
223 // This info is complete, write to the database. | 223 // This info is complete, write to the database. |
224 AddPageData(url, info.url_id(), info.visit_id(), info.visit_time(), | 224 AddPageData(url, info.url_id(), info.visit_id(), info.visit_time(), |
225 title, info.body()); | 225 title, info.body()); |
226 recent_changes_.Erase(found); | 226 recent_changes_.Erase(found); |
227 return; | 227 return; |
228 } | 228 } |
229 | 229 |
230 info.set_title(title); | 230 info.set_title(title); |
231 } | 231 } |
232 | 232 |
233 void TextDatabaseManager::AddPageContents(const GURL& url, | 233 void TextDatabaseManager::AddPageContents(const GURL& url, |
234 const string16& body) { | 234 const string16& body) { |
235 RecentChangeList::iterator found = recent_changes_.Peek(url); | 235 RecentChangeList::iterator found = recent_changes_.Peek(url); |
236 if (found == recent_changes_.end()) { | 236 if (found == recent_changes_.end()) { |
237 // This page is not in our cache of recent pages. This means that the page | 237 // This page is not in our cache of recent pages. This means that the page |
238 // took more than kExpirationSeconds to load. Often, this will be the result | 238 // took more than kExpirationSec to load. Often, this will be the result of |
239 // of a very slow iframe or other resource on the page that makes us think | 239 // a very slow iframe or other resource on the page that makes us think its |
240 // its still loading. | 240 // still loading. |
241 // | 241 // |
242 // As a fallback, set the most recent visit's contents using the input, and | 242 // As a fallback, set the most recent visit's contents using the input, and |
243 // use the last set title in the URL table as the title to index. | 243 // use the last set title in the URL table as the title to index. |
244 URLRow url_row; | 244 URLRow url_row; |
245 if (!url_database_->GetRowForURL(url, &url_row)) | 245 if (!url_database_->GetRowForURL(url, &url_row)) |
246 return; // URL is unknown, give up. | 246 return; // URL is unknown, give up. |
247 VisitRow visit; | 247 VisitRow visit; |
248 if (!visit_database_->GetMostRecentVisitForURL(url_row.id(), &visit)) | 248 if (!visit_database_->GetMostRecentVisitForURL(url_row.id(), &visit)) |
249 return; // No recent visit, give up. | 249 return; // No recent visit, give up. |
250 | 250 |
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
531 bool create_if_necessary) { | 531 bool create_if_necessary) { |
532 return GetDB(TimeToID(time), create_if_necessary); | 532 return GetDB(TimeToID(time), create_if_necessary); |
533 } | 533 } |
534 | 534 |
535 void TextDatabaseManager::ScheduleFlushOldChanges() { | 535 void TextDatabaseManager::ScheduleFlushOldChanges() { |
536 weak_factory_.InvalidateWeakPtrs(); | 536 weak_factory_.InvalidateWeakPtrs(); |
537 MessageLoop::current()->PostDelayedTask( | 537 MessageLoop::current()->PostDelayedTask( |
538 FROM_HERE, | 538 FROM_HERE, |
539 base::Bind(&TextDatabaseManager::FlushOldChanges, | 539 base::Bind(&TextDatabaseManager::FlushOldChanges, |
540 weak_factory_.GetWeakPtr()), | 540 weak_factory_.GetWeakPtr()), |
541 base::TimeDelta::FromSeconds(kExpirationSeconds)); | 541 kExpirationSec * Time::kMillisecondsPerSecond); |
542 } | 542 } |
543 | 543 |
544 void TextDatabaseManager::FlushOldChanges() { | 544 void TextDatabaseManager::FlushOldChanges() { |
545 FlushOldChangesForTime(TimeTicks::Now()); | 545 FlushOldChangesForTime(TimeTicks::Now()); |
546 } | 546 } |
547 | 547 |
548 void TextDatabaseManager::FlushOldChangesForTime(TimeTicks now) { | 548 void TextDatabaseManager::FlushOldChangesForTime(TimeTicks now) { |
549 // The end of the list is the oldest, so we just start from there committing | 549 // The end of the list is the oldest, so we just start from there committing |
550 // things until we get something too new. | 550 // things until we get something too new. |
551 RecentChangeList::reverse_iterator i = recent_changes_.rbegin(); | 551 RecentChangeList::reverse_iterator i = recent_changes_.rbegin(); |
552 while (i != recent_changes_.rend() && i->second.Expired(now)) { | 552 while (i != recent_changes_.rend() && i->second.Expired(now)) { |
553 AddPageData(i->first, i->second.url_id(), i->second.visit_id(), | 553 AddPageData(i->first, i->second.url_id(), i->second.visit_id(), |
554 i->second.visit_time(), i->second.title(), i->second.body()); | 554 i->second.visit_time(), i->second.title(), i->second.body()); |
555 i = recent_changes_.Erase(i); | 555 i = recent_changes_.Erase(i); |
556 } | 556 } |
557 | 557 |
558 ScheduleFlushOldChanges(); | 558 ScheduleFlushOldChanges(); |
559 } | 559 } |
560 | 560 |
561 } // namespace history | 561 } // namespace history |
OLD | NEW |