history_backend.cc revision a93a17c8d99d686bd4a1511e5504e5e6cc9fcadf
1// Copyright (c) 2012 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 "chrome/browser/history/history_backend.h" 6 7#include <algorithm> 8#include <functional> 9#include <list> 10#include <map> 11#include <set> 12#include <vector> 13 14#include "base/basictypes.h" 15#include "base/bind.h" 16#include "base/compiler_specific.h" 17#include "base/file_util.h" 18#include "base/memory/scoped_ptr.h" 19#include "base/memory/scoped_vector.h" 20#include "base/message_loop.h" 21#include "base/metrics/histogram.h" 22#include "base/rand_util.h" 23#include "base/string_util.h" 24#include "base/time.h" 25#include "base/utf_string_conversions.h" 26#include "chrome/browser/autocomplete/history_url_provider.h" 27#include "chrome/browser/bookmarks/bookmark_service.h" 28#include "chrome/browser/favicon/imported_favicon_usage.h" 29#include "chrome/browser/history/download_row.h" 30#include "chrome/browser/history/history_db_task.h" 31#include "chrome/browser/history/history_notifications.h" 32#include "chrome/browser/history/history_publisher.h" 33#include "chrome/browser/history/in_memory_history_backend.h" 34#include "chrome/browser/history/page_usage_data.h" 35#include "chrome/browser/history/select_favicon_frames.h" 36#include "chrome/browser/history/top_sites.h" 37#include "chrome/browser/history/typed_url_syncable_service.h" 38#include "chrome/browser/history/visit_filter.h" 39#include "chrome/common/chrome_constants.h" 40#include "chrome/common/chrome_notification_types.h" 41#include "chrome/common/url_constants.h" 42#include "googleurl/src/gurl.h" 43#include "grit/chromium_strings.h" 44#include "grit/generated_resources.h" 45#include "net/base/registry_controlled_domains/registry_controlled_domain.h" 46#include "sql/error_delegate_util.h" 47 48#if defined(OS_ANDROID) 49#include "chrome/browser/history/android/android_provider_backend.h" 50#endif 51 52using base::Time; 53using base::TimeDelta; 54using base::TimeTicks; 55 56/* The HistoryBackend consists of a number of components: 57 58 HistoryDatabase (stores past 3 months of history) 59 URLDatabase (stores a list of URLs) 60 DownloadDatabase (stores a list of downloads) 61 VisitDatabase (stores a list of visits for the URLs) 62 VisitSegmentDatabase (stores groups of URLs for the most visited view). 63 64 ArchivedDatabase (stores history older than 3 months) 65 URLDatabase (stores a list of URLs) 66 DownloadDatabase (stores a list of downloads) 67 VisitDatabase (stores a list of visits for the URLs) 68 69 (this does not store visit segments as they expire after 3 mos.) 70 71 TextDatabaseManager (manages multiple text database for different times) 72 TextDatabase (represents a single month of full-text index). 73 ...more TextDatabase objects... 74 75 ExpireHistoryBackend (manages moving things from HistoryDatabase to 76 the ArchivedDatabase and deleting) 77*/ 78 79namespace history { 80 81// How long we keep segment data for in days. Currently 3 months. 82// This value needs to be greater or equal to 83// MostVisitedModel::kMostVisitedScope but we don't want to introduce a direct 84// dependency between MostVisitedModel and the history backend. 85static const int kSegmentDataRetention = 90; 86 87// How long we'll wait to do a commit, so that things are batched together. 88static const int kCommitIntervalSeconds = 10; 89 90// The amount of time before we re-fetch the favicon. 91static const int kFaviconRefetchDays = 7; 92 93// GetSessionTabs returns all open tabs, or tabs closed kSessionCloseTimeWindow 94// seconds ago. 95static const int kSessionCloseTimeWindowSecs = 10; 96 97// The maximum number of items we'll allow in the redirect list before 98// deleting some. 99static const int kMaxRedirectCount = 32; 100 101// The number of days old a history entry can be before it is considered "old" 102// and is archived. 103static const int kArchiveDaysThreshold = 90; 104 105// Converts from PageUsageData to MostVisitedURL. |redirects| is a 106// list of redirects for this URL. Empty list means no redirects. 107MostVisitedURL MakeMostVisitedURL(const PageUsageData& page_data, 108 const RedirectList& redirects) { 109 MostVisitedURL mv; 110 mv.url = page_data.GetURL(); 111 mv.title = page_data.GetTitle(); 112 if (redirects.empty()) { 113 // Redirects must contain at least the target url. 114 mv.redirects.push_back(mv.url); 115 } else { 116 mv.redirects = redirects; 117 if (mv.redirects[mv.redirects.size() - 1] != mv.url) { 118 // The last url must be the target url. 119 mv.redirects.push_back(mv.url); 120 } 121 } 122 return mv; 123} 124 125// This task is run on a timer so that commits happen at regular intervals 126// so they are batched together. The important thing about this class is that 127// it supports canceling of the task so the reference to the backend will be 128// freed. The problem is that when history is shutting down, there is likely 129// to be one of these commits still pending and holding a reference. 130// 131// The backend can call Cancel to have this task release the reference. The 132// task will still run (if we ever get to processing the event before 133// shutdown), but it will not do anything. 134// 135// Note that this is a refcounted object and is not a task in itself. It should 136// be assigned to a RunnableMethod. 137// 138// TODO(brettw): bug 1165182: This should be replaced with a 139// base::WeakPtrFactory which will handle everything automatically (like we do 140// in ExpireHistoryBackend). 141class CommitLaterTask : public base::RefCounted<CommitLaterTask> { 142 public: 143 explicit CommitLaterTask(HistoryBackend* history_backend) 144 : history_backend_(history_backend) { 145 } 146 147 // The backend will call this function if it is being destroyed so that we 148 // release our reference. 149 void Cancel() { 150 history_backend_ = NULL; 151 } 152 153 void RunCommit() { 154 if (history_backend_) 155 history_backend_->Commit(); 156 } 157 158 private: 159 friend class base::RefCounted<CommitLaterTask>; 160 161 ~CommitLaterTask() {} 162 163 scoped_refptr<HistoryBackend> history_backend_; 164}; 165 166// Handles querying first the main database, then the full text database if that 167// fails. It will optionally keep track of all URLs seen so duplicates can be 168// eliminated. This is used by the querying sub-functions. 169// 170// TODO(brettw): This class may be able to be simplified or eliminated. After 171// this was written, QueryResults can efficiently look up by URL, so the need 172// for this extra set of previously queried URLs is less important. 173class HistoryBackend::URLQuerier { 174 public: 175 URLQuerier(URLDatabase* main_db, URLDatabase* archived_db, bool track_unique) 176 : main_db_(main_db), 177 archived_db_(archived_db), 178 track_unique_(track_unique) { 179 } 180 181 // When we're tracking unique URLs, returns true if this URL has been 182 // previously queried. Only call when tracking unique URLs. 183 bool HasURL(const GURL& url) { 184 DCHECK(track_unique_); 185 return unique_urls_.find(url) != unique_urls_.end(); 186 } 187 188 bool GetRowForURL(const GURL& url, URLRow* row) { 189 if (!main_db_->GetRowForURL(url, row)) { 190 if (!archived_db_ || !archived_db_->GetRowForURL(url, row)) { 191 // This row is neither in the main nor the archived DB. 192 return false; 193 } 194 } 195 196 if (track_unique_) 197 unique_urls_.insert(url); 198 return true; 199 } 200 201 private: 202 URLDatabase* main_db_; // Guaranteed non-NULL. 203 URLDatabase* archived_db_; // Possibly NULL. 204 205 bool track_unique_; 206 207 // When track_unique_ is set, this is updated with every URL seen so far. 208 std::set<GURL> unique_urls_; 209 210 DISALLOW_COPY_AND_ASSIGN(URLQuerier); 211}; 212 213// KillHistoryDatabaseErrorDelegate ------------------------------------------- 214 215class KillHistoryDatabaseErrorDelegate : public sql::ErrorDelegate { 216 public: 217 explicit KillHistoryDatabaseErrorDelegate(HistoryBackend* backend) 218 : backend_(backend), 219 scheduled_killing_database_(false) { 220 } 221 222 // sql::ErrorDelegate implementation. 223 virtual int OnError(int error, 224 sql::Connection* connection, 225 sql::Statement* stmt) OVERRIDE { 226 // Do not schedule killing database more than once. If the first time 227 // failed, it is unlikely that a second time will be successful. 228 if (!scheduled_killing_database_ && sql::IsErrorCatastrophic(error)) { 229 scheduled_killing_database_ = true; 230 231 // Don't just do the close/delete here, as we are being called by |db| and 232 // that seems dangerous. 233 MessageLoop::current()->PostTask( 234 FROM_HERE, 235 base::Bind(&HistoryBackend::KillHistoryDatabase, backend_)); 236 } 237 238 return error; 239 } 240 241 // Returns true if the delegate has previously scheduled killing the database. 242 bool scheduled_killing_database() const { 243 return scheduled_killing_database_; 244 } 245 246 private: 247 // Do not increment the count on |HistoryBackend| as that would create a 248 // circular reference (HistoryBackend -> HistoryDatabase -> Connection -> 249 // ErrorDelegate -> HistoryBackend). 250 HistoryBackend* backend_; 251 252 // True if the backend has previously scheduled killing the history database. 253 bool scheduled_killing_database_; 254 255 DISALLOW_COPY_AND_ASSIGN(KillHistoryDatabaseErrorDelegate); 256}; 257 258// HistoryBackend -------------------------------------------------------------- 259 260HistoryBackend::HistoryBackend(const base::FilePath& history_dir, 261 int id, 262 Delegate* delegate, 263 BookmarkService* bookmark_service) 264 : delegate_(delegate), 265 id_(id), 266 history_dir_(history_dir), 267 expirer_(this, bookmark_service), 268 recent_redirects_(kMaxRedirectCount), 269 backend_destroy_message_loop_(NULL), 270 segment_queried_(false), 271 bookmark_service_(bookmark_service) { 272} 273 274HistoryBackend::~HistoryBackend() { 275 DCHECK(!scheduled_commit_) << "Deleting without cleanup"; 276 ReleaseDBTasks(); 277 278#if defined(OS_ANDROID) 279 // Release AndroidProviderBackend before other objects. 280 android_provider_backend_.reset(); 281#endif 282 283 // First close the databases before optionally running the "destroy" task. 284 CloseAllDatabases(); 285 286 if (!backend_destroy_task_.is_null()) { 287 // Notify an interested party (typically a unit test) that we're done. 288 DCHECK(backend_destroy_message_loop_); 289 backend_destroy_message_loop_->PostTask(FROM_HERE, backend_destroy_task_); 290 } 291 292#if defined(OS_ANDROID) 293 file_util::Delete(GetAndroidCacheFileName(), false); 294#endif 295} 296 297void HistoryBackend::Init(const std::string& languages, bool force_fail) { 298 if (!force_fail) 299 InitImpl(languages); 300 delegate_->DBLoaded(id_); 301 typed_url_syncable_service_.reset(new TypedUrlSyncableService(this)); 302} 303 304void HistoryBackend::SetOnBackendDestroyTask(MessageLoop* message_loop, 305 const base::Closure& task) { 306 if (!backend_destroy_task_.is_null()) 307 DLOG(WARNING) << "Setting more than one destroy task, overriding"; 308 backend_destroy_message_loop_ = message_loop; 309 backend_destroy_task_ = task; 310} 311 312void HistoryBackend::Closing() { 313 // Any scheduled commit will have a reference to us, we must make it 314 // release that reference before we can be destroyed. 315 CancelScheduledCommit(); 316 317 // Release our reference to the delegate, this reference will be keeping the 318 // history service alive. 319 delegate_.reset(); 320} 321 322void HistoryBackend::NotifyRenderProcessHostDestruction(const void* host) { 323 tracker_.NotifyRenderProcessHostDestruction(host); 324} 325 326base::FilePath HistoryBackend::GetThumbnailFileName() const { 327 return history_dir_.Append(chrome::kThumbnailsFilename); 328} 329 330base::FilePath HistoryBackend::GetFaviconsFileName() const { 331 return history_dir_.Append(chrome::kFaviconsFilename); 332} 333 334base::FilePath HistoryBackend::GetArchivedFileName() const { 335 return history_dir_.Append(chrome::kArchivedHistoryFilename); 336} 337 338#if defined(OS_ANDROID) 339base::FilePath HistoryBackend::GetAndroidCacheFileName() const { 340 return history_dir_.Append(chrome::kAndroidCacheFilename); 341} 342#endif 343 344SegmentID HistoryBackend::GetLastSegmentID(VisitID from_visit) { 345 // Set is used to detect referrer loops. Should not happen, but can 346 // if the database is corrupt. 347 std::set<VisitID> visit_set; 348 VisitID visit_id = from_visit; 349 while (visit_id) { 350 VisitRow row; 351 if (!db_->GetRowForVisit(visit_id, &row)) 352 return 0; 353 if (row.segment_id) 354 return row.segment_id; // Found a visit in this change with a segment. 355 356 // Check the referrer of this visit, if any. 357 visit_id = row.referring_visit; 358 359 if (visit_set.find(visit_id) != visit_set.end()) { 360 NOTREACHED() << "Loop in referer chain, giving up"; 361 break; 362 } 363 visit_set.insert(visit_id); 364 } 365 return 0; 366} 367 368SegmentID HistoryBackend::UpdateSegments( 369 const GURL& url, 370 VisitID from_visit, 371 VisitID visit_id, 372 content::PageTransition transition_type, 373 const Time ts) { 374 if (!db_) 375 return 0; 376 377 // We only consider main frames. 378 if (!content::PageTransitionIsMainFrame(transition_type)) 379 return 0; 380 381 SegmentID segment_id = 0; 382 content::PageTransition t = 383 content::PageTransitionStripQualifier(transition_type); 384 385 // Are we at the beginning of a new segment? 386 // Note that navigating to an existing entry (with back/forward) reuses the 387 // same transition type. We are not adding it as a new segment in that case 388 // because if this was the target of a redirect, we might end up with 389 // 2 entries for the same final URL. Ex: User types google.net, gets 390 // redirected to google.com. A segment is created for google.net. On 391 // google.com users navigates through a link, then press back. That last 392 // navigation is for the entry google.com transition typed. We end up adding 393 // a segment for that one as well. So we end up with google.net and google.com 394 // in the segement table, showing as 2 entries in the NTP. 395 // Note also that we should still be updating the visit count for that segment 396 // which we are not doing now. It should be addressed when 397 // http://crbug.com/96860 is fixed. 398 if ((t == content::PAGE_TRANSITION_TYPED || 399 t == content::PAGE_TRANSITION_AUTO_BOOKMARK) && 400 (transition_type & content::PAGE_TRANSITION_FORWARD_BACK) == 0) { 401 // If so, create or get the segment. 402 std::string segment_name = db_->ComputeSegmentName(url); 403 URLID url_id = db_->GetRowForURL(url, NULL); 404 if (!url_id) 405 return 0; 406 407 if (!(segment_id = db_->GetSegmentNamed(segment_name))) { 408 if (!(segment_id = db_->CreateSegment(url_id, segment_name))) { 409 NOTREACHED(); 410 return 0; 411 } 412 } else { 413 // Note: if we update an existing segment, we update the url used to 414 // represent that segment in order to minimize stale most visited 415 // images. 416 db_->UpdateSegmentRepresentationURL(segment_id, url_id); 417 } 418 } else { 419 // Note: it is possible there is no segment ID set for this visit chain. 420 // This can happen if the initial navigation wasn't AUTO_BOOKMARK or 421 // TYPED. (For example GENERATED). In this case this visit doesn't count 422 // toward any segment. 423 if (!(segment_id = GetLastSegmentID(from_visit))) 424 return 0; 425 } 426 427 // Set the segment in the visit. 428 if (!db_->SetSegmentID(visit_id, segment_id)) { 429 NOTREACHED(); 430 return 0; 431 } 432 433 // Finally, increase the counter for that segment / day. 434 if (!db_->IncreaseSegmentVisitCount(segment_id, ts, 1)) { 435 NOTREACHED(); 436 return 0; 437 } 438 return segment_id; 439} 440 441void HistoryBackend::UpdateWithPageEndTime(const void* host, 442 int32 page_id, 443 const GURL& url, 444 Time end_ts) { 445 // Will be filled with the URL ID and the visit ID of the last addition. 446 VisitID visit_id = tracker_.GetLastVisit(host, page_id, url); 447 UpdateVisitDuration(visit_id, end_ts); 448} 449 450void HistoryBackend::UpdateVisitDuration(VisitID visit_id, const Time end_ts) { 451 if (!db_) 452 return; 453 454 // Get the starting visit_time for visit_id. 455 VisitRow visit_row; 456 if (db_->GetRowForVisit(visit_id, &visit_row)) { 457 // We should never have a negative duration time even when time is skewed. 458 visit_row.visit_duration = end_ts > visit_row.visit_time ? 459 end_ts - visit_row.visit_time : TimeDelta::FromMicroseconds(0); 460 db_->UpdateVisitRow(visit_row); 461 } 462} 463 464void HistoryBackend::AddPage(const HistoryAddPageArgs& request) { 465 if (!db_) 466 return; 467 468 // Will be filled with the URL ID and the visit ID of the last addition. 469 std::pair<URLID, VisitID> last_ids(0, tracker_.GetLastVisit( 470 request.id_scope, request.page_id, request.referrer)); 471 472 VisitID from_visit_id = last_ids.second; 473 474 // If a redirect chain is given, we expect the last item in that chain to be 475 // the final URL. 476 DCHECK(request.redirects.empty() || 477 request.redirects.back() == request.url); 478 479 // If the user is adding older history, we need to make sure our times 480 // are correct. 481 if (request.time < first_recorded_time_) 482 first_recorded_time_ = request.time; 483 484 content::PageTransition request_transition = request.transition; 485 content::PageTransition stripped_transition = 486 content::PageTransitionStripQualifier(request_transition); 487 bool is_keyword_generated = 488 (stripped_transition == content::PAGE_TRANSITION_KEYWORD_GENERATED); 489 490 // If the user is navigating to a not-previously-typed intranet hostname, 491 // change the transition to TYPED so that the omnibox will learn that this is 492 // a known host. 493 bool has_redirects = request.redirects.size() > 1; 494 if (content::PageTransitionIsMainFrame(request_transition) && 495 (stripped_transition != content::PAGE_TRANSITION_TYPED) && 496 !is_keyword_generated) { 497 const GURL& origin_url(has_redirects ? 498 request.redirects[0] : request.url); 499 if (origin_url.SchemeIs(chrome::kHttpScheme) || 500 origin_url.SchemeIs(chrome::kHttpsScheme) || 501 origin_url.SchemeIs(chrome::kFtpScheme)) { 502 std::string host(origin_url.host()); 503 size_t registry_length = 504 net::registry_controlled_domains::GetRegistryLength( 505 host, 506 net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES, 507 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES); 508 if (registry_length == 0 && !db_->IsTypedHost(host)) { 509 stripped_transition = content::PAGE_TRANSITION_TYPED; 510 request_transition = 511 content::PageTransitionFromInt( 512 stripped_transition | 513 content::PageTransitionGetQualifier(request_transition)); 514 } 515 } 516 } 517 518 if (!has_redirects) { 519 // The single entry is both a chain start and end. 520 content::PageTransition t = content::PageTransitionFromInt( 521 request_transition | 522 content::PAGE_TRANSITION_CHAIN_START | 523 content::PAGE_TRANSITION_CHAIN_END); 524 525 // No redirect case (one element means just the page itself). 526 last_ids = AddPageVisit(request.url, request.time, 527 last_ids.second, t, request.visit_source); 528 529 // Update the segment for this visit. KEYWORD_GENERATED visits should not 530 // result in changing most visited, so we don't update segments (most 531 // visited db). 532 if (!is_keyword_generated) { 533 UpdateSegments(request.url, from_visit_id, last_ids.second, t, 534 request.time); 535 536 // Update the referrer's duration. 537 UpdateVisitDuration(from_visit_id, request.time); 538 } 539 } else { 540 // Redirect case. Add the redirect chain. 541 542 content::PageTransition redirect_info = 543 content::PAGE_TRANSITION_CHAIN_START; 544 545 RedirectList redirects = request.redirects; 546 if (redirects[0].SchemeIs(chrome::kAboutScheme)) { 547 // When the redirect source + referrer is "about" we skip it. This 548 // happens when a page opens a new frame/window to about:blank and then 549 // script sets the URL to somewhere else (used to hide the referrer). It 550 // would be nice to keep all these redirects properly but we don't ever 551 // see the initial about:blank load, so we don't know where the 552 // subsequent client redirect came from. 553 // 554 // In this case, we just don't bother hooking up the source of the 555 // redirects, so we remove it. 556 redirects.erase(redirects.begin()); 557 } else if (request_transition & content::PAGE_TRANSITION_CLIENT_REDIRECT) { 558 redirect_info = content::PAGE_TRANSITION_CLIENT_REDIRECT; 559 // The first entry in the redirect chain initiated a client redirect. 560 // We don't add this to the database since the referrer is already 561 // there, so we skip over it but change the transition type of the first 562 // transition to client redirect. 563 // 564 // The referrer is invalid when restoring a session that features an 565 // https tab that redirects to a different host or to http. In this 566 // case we don't need to reconnect the new redirect with the existing 567 // chain. 568 if (request.referrer.is_valid()) { 569 DCHECK(request.referrer == redirects[0]); 570 redirects.erase(redirects.begin()); 571 572 // If the navigation entry for this visit has replaced that for the 573 // first visit, remove the CHAIN_END marker from the first visit. This 574 // can be called a lot, for example, the page cycler, and most of the 575 // time we won't have changed anything. 576 VisitRow visit_row; 577 if (request.did_replace_entry && 578 db_->GetRowForVisit(last_ids.second, &visit_row) && 579 visit_row.transition & content::PAGE_TRANSITION_CHAIN_END) { 580 visit_row.transition = content::PageTransitionFromInt( 581 visit_row.transition & ~content::PAGE_TRANSITION_CHAIN_END); 582 db_->UpdateVisitRow(visit_row); 583 } 584 } 585 } 586 587 for (size_t redirect_index = 0; redirect_index < redirects.size(); 588 redirect_index++) { 589 content::PageTransition t = 590 content::PageTransitionFromInt(stripped_transition | redirect_info); 591 592 // If this is the last transition, add a CHAIN_END marker 593 if (redirect_index == (redirects.size() - 1)) { 594 t = content::PageTransitionFromInt( 595 t | content::PAGE_TRANSITION_CHAIN_END); 596 } 597 598 // Record all redirect visits with the same timestamp. We don't display 599 // them anyway, and if we ever decide to, we can reconstruct their order 600 // from the redirect chain. 601 last_ids = AddPageVisit(redirects[redirect_index], 602 request.time, last_ids.second, 603 t, request.visit_source); 604 if (t & content::PAGE_TRANSITION_CHAIN_START) { 605 // Update the segment for this visit. 606 UpdateSegments(redirects[redirect_index], 607 from_visit_id, last_ids.second, t, request.time); 608 609 // Update the visit_details for this visit. 610 UpdateVisitDuration(from_visit_id, request.time); 611 } 612 613 // Subsequent transitions in the redirect list must all be server 614 // redirects. 615 redirect_info = content::PAGE_TRANSITION_SERVER_REDIRECT; 616 } 617 618 // Last, save this redirect chain for later so we can set titles & favicons 619 // on the redirected pages properly. It is indexed by the destination page. 620 recent_redirects_.Put(request.url, redirects); 621 } 622 623 // TODO(brettw) bug 1140015: Add an "add page" notification so the history 624 // views can keep in sync. 625 626 // Add the last visit to the tracker so we can get outgoing transitions. 627 // TODO(evanm): Due to http://b/1194536 we lose the referrers of a subframe 628 // navigation anyway, so last_visit_id is always zero for them. But adding 629 // them here confuses main frame history, so we skip them for now. 630 if (stripped_transition != content::PAGE_TRANSITION_AUTO_SUBFRAME && 631 stripped_transition != content::PAGE_TRANSITION_MANUAL_SUBFRAME && 632 !is_keyword_generated) { 633 tracker_.AddVisit(request.id_scope, request.page_id, request.url, 634 last_ids.second); 635 } 636 637 if (text_database_) { 638 text_database_->AddPageURL(request.url, last_ids.first, last_ids.second, 639 request.time); 640 } 641 642 ScheduleCommit(); 643} 644 645void HistoryBackend::InitImpl(const std::string& languages) { 646 DCHECK(!db_) << "Initializing HistoryBackend twice"; 647 // In the rare case where the db fails to initialize a dialog may get shown 648 // the blocks the caller, yet allows other messages through. For this reason 649 // we only set db_ to the created database if creation is successful. That 650 // way other methods won't do anything as db_ is still NULL. 651 652 TimeTicks beginning_time = TimeTicks::Now(); 653 654 // Compute the file names. Note that the index file can be removed when the 655 // text db manager is finished being hooked up. 656 base::FilePath history_name = history_dir_.Append(chrome::kHistoryFilename); 657 base::FilePath thumbnail_name = GetThumbnailFileName(); 658 base::FilePath archived_name = GetArchivedFileName(); 659 660 // History database. 661 db_.reset(new HistoryDatabase()); 662 663 // |HistoryDatabase::Init| takes ownership of |error_delegate|. 664 KillHistoryDatabaseErrorDelegate* error_delegate = 665 new KillHistoryDatabaseErrorDelegate(this); 666 667 sql::InitStatus status = db_->Init(history_name, error_delegate); 668 switch (status) { 669 case sql::INIT_OK: 670 break; 671 case sql::INIT_FAILURE: { 672 // A NULL db_ will cause all calls on this object to notice this error 673 // and to not continue. If the error delegate scheduled killing the 674 // database, the task it posted has not executed yet. Try killing the 675 // database now before we close it. 676 bool kill_database = error_delegate->scheduled_killing_database(); 677 if (kill_database) 678 KillHistoryDatabase(); 679 UMA_HISTOGRAM_BOOLEAN("History.AttemptedToFixProfileError", 680 kill_database); 681 delegate_->NotifyProfileError(id_, status); 682 db_.reset(); 683 return; 684 } 685 default: 686 NOTREACHED(); 687 } 688 689 // Fill the in-memory database and send it back to the history service on the 690 // main thread. 691 InMemoryHistoryBackend* mem_backend = new InMemoryHistoryBackend; 692 if (mem_backend->Init(history_name, db_.get())) 693 delegate_->SetInMemoryBackend(id_, mem_backend); // Takes ownership of 694 // pointer. 695 else 696 delete mem_backend; // Error case, run without the in-memory DB. 697 db_->BeginExclusiveMode(); // Must be after the mem backend read the data. 698 699 // Create the history publisher which needs to be passed on to the text and 700 // thumbnail databases for publishing history. 701 history_publisher_.reset(new HistoryPublisher()); 702 if (!history_publisher_->Init()) { 703 // The init may fail when there are no indexers wanting our history. 704 // Hence no need to log the failure. 705 history_publisher_.reset(); 706 } 707 708 // Full-text database. This has to be first so we can pass it to the 709 // HistoryDatabase for migration. 710 text_database_.reset(new TextDatabaseManager(history_dir_, 711 db_.get(), db_.get())); 712 if (!text_database_->Init(history_publisher_.get())) { 713 LOG(WARNING) << "Text database initialization failed, running without it."; 714 text_database_.reset(); 715 } 716 if (db_->needs_version_17_migration()) { 717 // See needs_version_17_migration() decl for more. In this case, we want 718 // to erase all the text database files. This must be done after the text 719 // database manager has been initialized, since it knows about all the 720 // files it manages. 721 text_database_->DeleteAll(); 722 } 723 724 // Thumbnail database. 725 thumbnail_db_.reset(new ThumbnailDatabase()); 726 if (!db_->GetNeedsThumbnailMigration()) { 727 // No convertion needed - use new filename right away. 728 thumbnail_name = GetFaviconsFileName(); 729 } 730 if (thumbnail_db_->Init(thumbnail_name, 731 history_publisher_.get(), 732 db_.get()) != sql::INIT_OK) { 733 // Unlike the main database, we don't error out when the database is too 734 // new because this error is much less severe. Generally, this shouldn't 735 // happen since the thumbnail and main datbase versions should be in sync. 736 // We'll just continue without thumbnails & favicons in this case or any 737 // other error. 738 LOG(WARNING) << "Could not initialize the thumbnail database."; 739 thumbnail_db_.reset(); 740 } 741 742 if (db_->GetNeedsThumbnailMigration()) { 743 VLOG(1) << "Starting TopSites migration"; 744 delegate_->StartTopSitesMigration(id_); 745 } 746 747 // Archived database. 748 if (db_->needs_version_17_migration()) { 749 // See needs_version_17_migration() decl for more. In this case, we want 750 // to delete the archived database and need to do so before we try to 751 // open the file. We can ignore any error (maybe the file doesn't exist). 752 file_util::Delete(archived_name, false); 753 } 754 archived_db_.reset(new ArchivedDatabase()); 755 if (!archived_db_->Init(archived_name)) { 756 LOG(WARNING) << "Could not initialize the archived database."; 757 archived_db_.reset(); 758 } 759 760 // Generate the history and thumbnail database metrics only after performing 761 // any migration work. 762 if (base::RandInt(1, 100) == 50) { 763 // Only do this computation sometimes since it can be expensive. 764 db_->ComputeDatabaseMetrics(history_name); 765 thumbnail_db_->ComputeDatabaseMetrics(); 766 } 767 768 // Tell the expiration module about all the nice databases we made. This must 769 // happen before db_->Init() is called since the callback ForceArchiveHistory 770 // may need to expire stuff. 771 // 772 // *sigh*, this can all be cleaned up when that migration code is removed. 773 // The main DB initialization should intuitively be first (not that it 774 // actually matters) and the expirer should be set last. 775 expirer_.SetDatabases(db_.get(), archived_db_.get(), 776 thumbnail_db_.get(), text_database_.get()); 777 778 // Open the long-running transaction. 779 db_->BeginTransaction(); 780 if (thumbnail_db_) 781 thumbnail_db_->BeginTransaction(); 782 if (archived_db_) 783 archived_db_->BeginTransaction(); 784 if (text_database_) 785 text_database_->BeginTransaction(); 786 787 // Get the first item in our database. 788 db_->GetStartDate(&first_recorded_time_); 789 790 // Start expiring old stuff. 791 expirer_.StartArchivingOldStuff(TimeDelta::FromDays(kArchiveDaysThreshold)); 792 793#if defined(OS_ANDROID) 794 if (thumbnail_db_) { 795 android_provider_backend_.reset(new AndroidProviderBackend( 796 GetAndroidCacheFileName(), db_.get(), thumbnail_db_.get(), 797 bookmark_service_, delegate_.get())); 798 } 799#endif 800 801 HISTOGRAM_TIMES("History.InitTime", 802 TimeTicks::Now() - beginning_time); 803} 804 805void HistoryBackend::CloseAllDatabases() { 806 if (db_) { 807 // Commit the long-running transaction. 808 db_->CommitTransaction(); 809 db_.reset(); 810 } 811 if (thumbnail_db_) { 812 thumbnail_db_->CommitTransaction(); 813 thumbnail_db_.reset(); 814 } 815 if (archived_db_) { 816 archived_db_->CommitTransaction(); 817 archived_db_.reset(); 818 } 819 if (text_database_) { 820 text_database_->CommitTransaction(); 821 text_database_.reset(); 822 } 823} 824 825std::pair<URLID, VisitID> HistoryBackend::AddPageVisit( 826 const GURL& url, 827 Time time, 828 VisitID referring_visit, 829 content::PageTransition transition, 830 VisitSource visit_source) { 831 // Top-level frame navigations are visible, everything else is hidden 832 bool new_hidden = !content::PageTransitionIsMainFrame(transition); 833 834 // NOTE: This code must stay in sync with 835 // ExpireHistoryBackend::ExpireURLsForVisits(). 836 // TODO(pkasting): http://b/1148304 We shouldn't be marking so many URLs as 837 // typed, which would eliminate the need for this code. 838 int typed_increment = 0; 839 content::PageTransition transition_type = 840 content::PageTransitionStripQualifier(transition); 841 if ((transition_type == content::PAGE_TRANSITION_TYPED && 842 !content::PageTransitionIsRedirect(transition)) || 843 transition_type == content::PAGE_TRANSITION_KEYWORD_GENERATED) 844 typed_increment = 1; 845 846 // See if this URL is already in the DB. 847 URLRow url_info(url); 848 URLID url_id = db_->GetRowForURL(url, &url_info); 849 if (url_id) { 850 // Update of an existing row. 851 if (content::PageTransitionStripQualifier(transition) != 852 content::PAGE_TRANSITION_RELOAD) 853 url_info.set_visit_count(url_info.visit_count() + 1); 854 if (typed_increment) 855 url_info.set_typed_count(url_info.typed_count() + typed_increment); 856 if (url_info.last_visit() < time) 857 url_info.set_last_visit(time); 858 859 // Only allow un-hiding of pages, never hiding. 860 if (!new_hidden) 861 url_info.set_hidden(false); 862 863 db_->UpdateURLRow(url_id, url_info); 864 } else { 865 // Addition of a new row. 866 url_info.set_visit_count(1); 867 url_info.set_typed_count(typed_increment); 868 url_info.set_last_visit(time); 869 url_info.set_hidden(new_hidden); 870 871 url_id = db_->AddURL(url_info); 872 if (!url_id) { 873 NOTREACHED() << "Adding URL failed."; 874 return std::make_pair(0, 0); 875 } 876 url_info.id_ = url_id; 877 878 // We don't actually add the URL to the full text index at this point. It 879 // might be nice to do this so that even if we get no title or body, the 880 // user can search for URL components and get the page. 881 // 882 // However, in most cases, we'll get at least a title and usually contents, 883 // and this add will be redundant, slowing everything down. As a result, 884 // we ignore this edge case. 885 } 886 887 // Add the visit with the time to the database. 888 VisitRow visit_info(url_id, time, referring_visit, transition, 0); 889 VisitID visit_id = db_->AddVisit(&visit_info, visit_source); 890 NotifyVisitObservers(visit_info); 891 892 if (visit_info.visit_time < first_recorded_time_) 893 first_recorded_time_ = visit_info.visit_time; 894 895 // Broadcast a notification of the visit. 896 if (visit_id) { 897 if (typed_url_syncable_service_.get()) 898 typed_url_syncable_service_->OnUrlVisited(transition, &url_info); 899 900 URLVisitedDetails* details = new URLVisitedDetails; 901 details->transition = transition; 902 details->row = url_info; 903 // TODO(meelapshah) Disabled due to potential PageCycler regression. 904 // Re-enable this. 905 // GetMostRecentRedirectsTo(url, &details->redirects); 906 BroadcastNotifications(chrome::NOTIFICATION_HISTORY_URL_VISITED, details); 907 } else { 908 VLOG(0) << "Failed to build visit insert statement: " 909 << "url_id = " << url_id; 910 } 911 912 return std::make_pair(url_id, visit_id); 913} 914 915void HistoryBackend::AddPagesWithDetails(const URLRows& urls, 916 VisitSource visit_source) { 917 if (!db_) 918 return; 919 920 scoped_ptr<URLsModifiedDetails> modified(new URLsModifiedDetails); 921 for (URLRows::const_iterator i = urls.begin(); i != urls.end(); ++i) { 922 DCHECK(!i->last_visit().is_null()); 923 924 // We will add to either the archived database or the main one depending on 925 // the date of the added visit. 926 URLDatabase* url_database; 927 VisitDatabase* visit_database; 928 if (IsExpiredVisitTime(i->last_visit())) { 929 if (!archived_db_) 930 return; // No archived database to save it to, just forget this. 931 url_database = archived_db_.get(); 932 visit_database = archived_db_.get(); 933 } else { 934 url_database = db_.get(); 935 visit_database = db_.get(); 936 } 937 938 URLRow existing_url; 939 URLID url_id = url_database->GetRowForURL(i->url(), &existing_url); 940 if (!url_id) { 941 // Add the page if it doesn't exist. 942 url_id = url_database->AddURL(*i); 943 if (!url_id) { 944 NOTREACHED() << "Could not add row to DB"; 945 return; 946 } 947 948 if (i->typed_count() > 0) { 949 modified->changed_urls.push_back(*i); 950 modified->changed_urls.back().set_id(url_id); // *i likely has |id_| 0. 951 } 952 } 953 954 // Add the page to the full text index. This function is also used for 955 // importing. Even though we don't have page contents, we can at least 956 // add the title and URL to the index so they can be searched. We don't 957 // bother to delete any already-existing FTS entries for the URL, since 958 // this is normally called on import. 959 // 960 // If you ever import *after* first run (selecting import from the menu), 961 // then these additional entries will "shadow" the originals when querying 962 // for the most recent match only, and the user won't get snippets. This is 963 // a very minor issue, and fixing it will make import slower, so we don't 964 // bother. 965 bool has_indexed = false; 966 if (text_database_) { 967 // We do not have to make it update the visit database, below, we will 968 // create the visit entry with the indexed flag set. 969 has_indexed = text_database_->AddPageData(i->url(), url_id, 0, 970 i->last_visit(), 971 i->title(), string16()); 972 } 973 974 // Sync code manages the visits itself. 975 if (visit_source != SOURCE_SYNCED) { 976 // Make up a visit to correspond to the last visit to the page. 977 VisitRow visit_info(url_id, i->last_visit(), 0, 978 content::PageTransitionFromInt( 979 content::PAGE_TRANSITION_LINK | 980 content::PAGE_TRANSITION_CHAIN_START | 981 content::PAGE_TRANSITION_CHAIN_END), 0); 982 visit_info.is_indexed = has_indexed; 983 if (!visit_database->AddVisit(&visit_info, visit_source)) { 984 NOTREACHED() << "Adding visit failed."; 985 return; 986 } 987 NotifyVisitObservers(visit_info); 988 989 if (visit_info.visit_time < first_recorded_time_) 990 first_recorded_time_ = visit_info.visit_time; 991 } 992 } 993 994 if (typed_url_syncable_service_.get()) 995 typed_url_syncable_service_->OnUrlsModified(&modified->changed_urls); 996 997 // Broadcast a notification for typed URLs that have been modified. This 998 // will be picked up by the in-memory URL database on the main thread. 999 // 1000 // TODO(brettw) bug 1140015: Add an "add page" notification so the history 1001 // views can keep in sync. 1002 BroadcastNotifications(chrome::NOTIFICATION_HISTORY_URLS_MODIFIED, 1003 modified.release()); 1004 1005 ScheduleCommit(); 1006} 1007 1008bool HistoryBackend::IsExpiredVisitTime(const base::Time& time) { 1009 return time < expirer_.GetCurrentArchiveTime(); 1010} 1011 1012void HistoryBackend::SetPageTitle(const GURL& url, 1013 const string16& title) { 1014 if (!db_) 1015 return; 1016 1017 // Update the full text index. 1018 if (text_database_) 1019 text_database_->AddPageTitle(url, title); 1020 1021 // Search for recent redirects which should get the same title. We make a 1022 // dummy list containing the exact URL visited if there are no redirects so 1023 // the processing below can be the same. 1024 history::RedirectList dummy_list; 1025 history::RedirectList* redirects; 1026 RedirectCache::iterator iter = recent_redirects_.Get(url); 1027 if (iter != recent_redirects_.end()) { 1028 redirects = &iter->second; 1029 1030 // This redirect chain should have the destination URL as the last item. 1031 DCHECK(!redirects->empty()); 1032 DCHECK(redirects->back() == url); 1033 } else { 1034 // No redirect chain stored, make up one containing the URL we want so we 1035 // can use the same logic below. 1036 dummy_list.push_back(url); 1037 redirects = &dummy_list; 1038 } 1039 1040 scoped_ptr<URLsModifiedDetails> details(new URLsModifiedDetails); 1041 for (size_t i = 0; i < redirects->size(); i++) { 1042 URLRow row; 1043 URLID row_id = db_->GetRowForURL(redirects->at(i), &row); 1044 if (row_id && row.title() != title) { 1045 row.set_title(title); 1046 db_->UpdateURLRow(row_id, row); 1047 details->changed_urls.push_back(row); 1048 } 1049 } 1050 1051 // Broadcast notifications for any URLs that have changed. This will 1052 // update the in-memory database and the InMemoryURLIndex. 1053 if (!details->changed_urls.empty()) { 1054 if (typed_url_syncable_service_.get()) 1055 typed_url_syncable_service_->OnUrlsModified(&details->changed_urls); 1056 BroadcastNotifications(chrome::NOTIFICATION_HISTORY_URLS_MODIFIED, 1057 details.release()); 1058 ScheduleCommit(); 1059 } 1060} 1061 1062void HistoryBackend::AddPageNoVisitForBookmark(const GURL& url, 1063 const string16& title) { 1064 if (!db_) 1065 return; 1066 1067 URLRow url_info(url); 1068 URLID url_id = db_->GetRowForURL(url, &url_info); 1069 if (url_id) { 1070 // URL is already known, nothing to do. 1071 return; 1072 } 1073 1074 if (!title.empty()) { 1075 url_info.set_title(title); 1076 } else { 1077 url_info.set_title(UTF8ToUTF16(url.spec())); 1078 } 1079 1080 url_info.set_last_visit(Time::Now()); 1081 // Mark the page hidden. If the user types it in, it'll unhide. 1082 url_info.set_hidden(true); 1083 1084 db_->AddURL(url_info); 1085} 1086 1087void HistoryBackend::IterateURLs( 1088 const scoped_refptr<components::VisitedLinkDelegate::URLEnumerator>& 1089 iterator) { 1090 if (db_) { 1091 HistoryDatabase::URLEnumerator e; 1092 if (db_->InitURLEnumeratorForEverything(&e)) { 1093 URLRow info; 1094 while (e.GetNextURL(&info)) { 1095 iterator->OnURL(info.url()); 1096 } 1097 iterator->OnComplete(true); // Success. 1098 return; 1099 } 1100 } 1101 iterator->OnComplete(false); // Failure. 1102} 1103 1104bool HistoryBackend::GetAllTypedURLs(URLRows* urls) { 1105 if (db_) 1106 return db_->GetAllTypedUrls(urls); 1107 return false; 1108} 1109 1110bool HistoryBackend::GetVisitsForURL(URLID id, VisitVector* visits) { 1111 if (db_) 1112 return db_->GetVisitsForURL(id, visits); 1113 return false; 1114} 1115 1116bool HistoryBackend::GetMostRecentVisitsForURL(URLID id, 1117 int max_visits, 1118 VisitVector* visits) { 1119 if (db_) 1120 return db_->GetMostRecentVisitsForURL(id, max_visits, visits); 1121 return false; 1122} 1123 1124bool HistoryBackend::UpdateURL(URLID id, const history::URLRow& url) { 1125 if (db_) 1126 return db_->UpdateURLRow(id, url); 1127 return false; 1128} 1129 1130bool HistoryBackend::AddVisits(const GURL& url, 1131 const std::vector<VisitInfo>& visits, 1132 VisitSource visit_source) { 1133 if (db_) { 1134 for (std::vector<VisitInfo>::const_iterator visit = visits.begin(); 1135 visit != visits.end(); ++visit) { 1136 if (!AddPageVisit( 1137 url, visit->first, 0, visit->second, visit_source).first) { 1138 return false; 1139 } 1140 } 1141 ScheduleCommit(); 1142 return true; 1143 } 1144 return false; 1145} 1146 1147bool HistoryBackend::RemoveVisits(const VisitVector& visits) { 1148 if (!db_) 1149 return false; 1150 1151 expirer_.ExpireVisits(visits); 1152 ScheduleCommit(); 1153 return true; 1154} 1155 1156bool HistoryBackend::GetVisitsSource(const VisitVector& visits, 1157 VisitSourceMap* sources) { 1158 if (!db_) 1159 return false; 1160 1161 db_->GetVisitsSource(visits, sources); 1162 return true; 1163} 1164 1165bool HistoryBackend::GetURL(const GURL& url, history::URLRow* url_row) { 1166 if (db_) 1167 return db_->GetRowForURL(url, url_row) != 0; 1168 return false; 1169} 1170 1171void HistoryBackend::QueryURL(scoped_refptr<QueryURLRequest> request, 1172 const GURL& url, 1173 bool want_visits) { 1174 if (request->canceled()) 1175 return; 1176 1177 bool success = false; 1178 URLRow* row = &request->value.a; 1179 VisitVector* visits = &request->value.b; 1180 if (db_) { 1181 if (db_->GetRowForURL(url, row)) { 1182 // Have a row. 1183 success = true; 1184 1185 // Optionally query the visits. 1186 if (want_visits) 1187 db_->GetVisitsForURL(row->id(), visits); 1188 } 1189 } 1190 request->ForwardResult(request->handle(), success, row, visits); 1191} 1192 1193TypedUrlSyncableService* HistoryBackend::GetTypedUrlSyncableService() const { 1194 return typed_url_syncable_service_.get(); 1195} 1196 1197// Segment usage --------------------------------------------------------------- 1198 1199void HistoryBackend::DeleteOldSegmentData() { 1200 if (db_) 1201 db_->DeleteSegmentData(Time::Now() - 1202 TimeDelta::FromDays(kSegmentDataRetention)); 1203} 1204 1205void HistoryBackend::QuerySegmentUsage( 1206 scoped_refptr<QuerySegmentUsageRequest> request, 1207 const Time from_time, 1208 int max_result_count) { 1209 if (request->canceled()) 1210 return; 1211 1212 if (db_) { 1213 db_->QuerySegmentUsage(from_time, max_result_count, &request->value.get()); 1214 1215 // If this is the first time we query segments, invoke 1216 // DeleteOldSegmentData asynchronously. We do this to cleanup old 1217 // entries. 1218 if (!segment_queried_) { 1219 segment_queried_ = true; 1220 MessageLoop::current()->PostTask( 1221 FROM_HERE, 1222 base::Bind(&HistoryBackend::DeleteOldSegmentData, this)); 1223 } 1224 } 1225 request->ForwardResult(request->handle(), &request->value.get()); 1226} 1227 1228void HistoryBackend::IncreaseSegmentDuration(const GURL& url, 1229 base::Time time, 1230 base::TimeDelta delta) { 1231 if (!db_) 1232 return; 1233 1234 const std::string segment_name(VisitSegmentDatabase::ComputeSegmentName(url)); 1235 SegmentID segment_id = db_->GetSegmentNamed(segment_name); 1236 if (!segment_id) { 1237 URLID url_id = db_->GetRowForURL(url, NULL); 1238 if (!url_id) 1239 return; 1240 segment_id = db_->CreateSegment(url_id, segment_name); 1241 if (!segment_id) 1242 return; 1243 } 1244 SegmentDurationID duration_id; 1245 base::TimeDelta total_delta; 1246 if (!db_->GetSegmentDuration(segment_id, time, &duration_id, 1247 &total_delta)) { 1248 db_->CreateSegmentDuration(segment_id, time, delta); 1249 return; 1250 } 1251 total_delta += delta; 1252 db_->SetSegmentDuration(duration_id, total_delta); 1253} 1254 1255void HistoryBackend::QuerySegmentDuration( 1256 scoped_refptr<QuerySegmentUsageRequest> request, 1257 const base::Time from_time, 1258 int max_result_count) { 1259 if (request->canceled()) 1260 return; 1261 1262 if (db_) { 1263 db_->QuerySegmentDuration(from_time, max_result_count, 1264 &request->value.get()); 1265 } 1266 request->ForwardResult(request->handle(), &request->value.get()); 1267} 1268 1269// Keyword visits -------------------------------------------------------------- 1270 1271void HistoryBackend::SetKeywordSearchTermsForURL(const GURL& url, 1272 TemplateURLID keyword_id, 1273 const string16& term) { 1274 if (!db_) 1275 return; 1276 1277 // Get the ID for this URL. 1278 URLRow url_row; 1279 if (!db_->GetRowForURL(url, &url_row)) { 1280 // There is a small possibility the url was deleted before the keyword 1281 // was added. Ignore the request. 1282 return; 1283 } 1284 1285 db_->SetKeywordSearchTermsForURL(url_row.id(), keyword_id, term); 1286 1287 // details is deleted by BroadcastNotifications. 1288 KeywordSearchTermDetails* details = new KeywordSearchTermDetails; 1289 details->url = url; 1290 details->keyword_id = keyword_id; 1291 details->term = term; 1292 BroadcastNotifications( 1293 chrome::NOTIFICATION_HISTORY_KEYWORD_SEARCH_TERM_UPDATED, details); 1294 ScheduleCommit(); 1295} 1296 1297void HistoryBackend::DeleteAllSearchTermsForKeyword( 1298 TemplateURLID keyword_id) { 1299 if (!db_) 1300 return; 1301 1302 db_->DeleteAllSearchTermsForKeyword(keyword_id); 1303 // TODO(sky): bug 1168470. Need to move from archive dbs too. 1304 ScheduleCommit(); 1305} 1306 1307void HistoryBackend::GetMostRecentKeywordSearchTerms( 1308 scoped_refptr<GetMostRecentKeywordSearchTermsRequest> request, 1309 TemplateURLID keyword_id, 1310 const string16& prefix, 1311 int max_count) { 1312 if (request->canceled()) 1313 return; 1314 1315 if (db_) { 1316 db_->GetMostRecentKeywordSearchTerms(keyword_id, prefix, max_count, 1317 &(request->value)); 1318 } 1319 request->ForwardResult(request->handle(), &request->value); 1320} 1321 1322// Downloads ------------------------------------------------------------------- 1323 1324void HistoryBackend::GetNextDownloadId(int* id) { 1325 if (db_) 1326 *id = db_->next_download_id(); 1327} 1328 1329// Get all the download entries from the database. 1330void HistoryBackend::QueryDownloads(std::vector<DownloadRow>* rows) { 1331 if (db_) 1332 db_->QueryDownloads(rows); 1333} 1334 1335// Update a particular download entry. 1336void HistoryBackend::UpdateDownload(const history::DownloadRow& data) { 1337 if (!db_) 1338 return; 1339 db_->UpdateDownload(data); 1340 ScheduleCommit(); 1341} 1342 1343void HistoryBackend::CreateDownload(const history::DownloadRow& history_info, 1344 int64* db_handle) { 1345 if (!db_) 1346 return; 1347 *db_handle = db_->CreateDownload(history_info); 1348 ScheduleCommit(); 1349} 1350 1351void HistoryBackend::RemoveDownloads(const std::set<int64>& handles) { 1352 if (!db_) 1353 return; 1354 int downloads_count_before = db_->CountDownloads(); 1355 base::TimeTicks started_removing = base::TimeTicks::Now(); 1356 // HistoryBackend uses a long-running Transaction that is committed 1357 // periodically, so this loop doesn't actually hit the disk too hard. 1358 for (std::set<int64>::const_iterator it = handles.begin(); 1359 it != handles.end(); ++it) { 1360 db_->RemoveDownload(*it); 1361 } 1362 base::TimeTicks finished_removing = base::TimeTicks::Now(); 1363 int downloads_count_after = db_->CountDownloads(); 1364 int num_downloads_deleted = downloads_count_before - downloads_count_after; 1365 if (num_downloads_deleted >= 0) { 1366 UMA_HISTOGRAM_COUNTS("Download.DatabaseRemoveDownloadsCount", 1367 num_downloads_deleted); 1368 base::TimeDelta micros = (1000 * (finished_removing - started_removing)); 1369 UMA_HISTOGRAM_TIMES("Download.DatabaseRemoveDownloadsTime", micros); 1370 if (num_downloads_deleted > 0) { 1371 UMA_HISTOGRAM_TIMES("Download.DatabaseRemoveDownloadsTimePerRecord", 1372 (1000 * micros) / num_downloads_deleted); 1373 } 1374 } 1375 int num_downloads_not_deleted = handles.size() - num_downloads_deleted; 1376 if (num_downloads_not_deleted >= 0) { 1377 UMA_HISTOGRAM_COUNTS("Download.DatabaseRemoveDownloadsCountNotRemoved", 1378 num_downloads_not_deleted); 1379 } 1380 ScheduleCommit(); 1381} 1382 1383void HistoryBackend::QueryHistory(scoped_refptr<QueryHistoryRequest> request, 1384 const string16& text_query, 1385 const QueryOptions& options) { 1386 if (request->canceled()) 1387 return; 1388 1389 TimeTicks beginning_time = TimeTicks::Now(); 1390 1391 if (db_) { 1392 if (text_query.empty()) { 1393 // Basic history query for the main database. 1394 QueryHistoryBasic(db_.get(), db_.get(), options, &request->value); 1395 1396 // Now query the archived database. This is a bit tricky because we don't 1397 // want to query it if the queried time range isn't going to find anything 1398 // in it. 1399 // TODO(brettw) bug 1171036: do blimpie querying for the archived database 1400 // as well. 1401 // if (archived_db_.get() && 1402 // expirer_.GetCurrentArchiveTime() - TimeDelta::FromDays(7)) { 1403 } else { 1404 // Full text history query. 1405 QueryHistoryFTS(text_query, options, &request->value); 1406 } 1407 } 1408 1409 request->ForwardResult(request->handle(), &request->value); 1410 1411 UMA_HISTOGRAM_TIMES("History.QueryHistory", 1412 TimeTicks::Now() - beginning_time); 1413} 1414 1415// Basic time-based querying of history. 1416void HistoryBackend::QueryHistoryBasic(URLDatabase* url_db, 1417 VisitDatabase* visit_db, 1418 const QueryOptions& options, 1419 QueryResults* result) { 1420 // First get all visits. 1421 VisitVector visits; 1422 bool has_more_results = visit_db->GetVisibleVisitsInRange(options, &visits); 1423 DCHECK(static_cast<int>(visits.size()) <= options.EffectiveMaxCount()); 1424 1425 // Now add them and the URL rows to the results. 1426 URLResult url_result; 1427 for (size_t i = 0; i < visits.size(); i++) { 1428 const VisitRow visit = visits[i]; 1429 1430 // Add a result row for this visit, get the URL info from the DB. 1431 if (!url_db->GetURLRow(visit.url_id, &url_result)) { 1432 VLOG(0) << "Failed to get id " << visit.url_id 1433 << " from history.urls."; 1434 continue; // DB out of sync and URL doesn't exist, try to recover. 1435 } 1436 1437 if (!url_result.url().is_valid()) { 1438 VLOG(0) << "Got invalid URL from history.urls with id " 1439 << visit.url_id << ": " 1440 << url_result.url().possibly_invalid_spec(); 1441 continue; // Don't report invalid URLs in case of corruption. 1442 } 1443 1444 // The archived database may be out of sync with respect to starring, 1445 // titles, last visit date, etc. Therefore, we query the main DB if the 1446 // current URL database is not the main one. 1447 if (url_db == db_.get()) { 1448 // Currently querying the archived DB, update with the main database to 1449 // catch any interesting stuff. This will update it if it exists in the 1450 // main DB, and do nothing otherwise. 1451 db_->GetRowForURL(url_result.url(), &url_result); 1452 } 1453 1454 url_result.set_visit_time(visit.visit_time); 1455 1456 // Set whether the visit was blocked for a managed user by looking at the 1457 // transition type. 1458 url_result.set_blocked_visit( 1459 (visit.transition & content::PAGE_TRANSITION_BLOCKED) != 0); 1460 1461 // We don't set any of the query-specific parts of the URLResult, since 1462 // snippets and stuff don't apply to basic querying. 1463 result->AppendURLBySwapping(&url_result); 1464 } 1465 1466 if (!has_more_results && options.begin_time <= first_recorded_time_) 1467 result->set_reached_beginning(true); 1468} 1469 1470void HistoryBackend::QueryHistoryFTS(const string16& text_query, 1471 const QueryOptions& options, 1472 QueryResults* result) { 1473 if (!text_database_) 1474 return; 1475 1476 // Full text query, first get all the FTS results in the time range. 1477 std::vector<TextDatabase::Match> fts_matches; 1478 Time first_time_searched; 1479 text_database_->GetTextMatches(text_query, options, 1480 &fts_matches, &first_time_searched); 1481 1482 URLQuerier querier(db_.get(), archived_db_.get(), true); 1483 1484 // Now get the row and visit information for each one. 1485 URLResult url_result; // Declare outside loop to prevent re-construction. 1486 for (size_t i = 0; i < fts_matches.size(); i++) { 1487 if (options.max_count != 0 && 1488 static_cast<int>(result->size()) >= options.max_count) 1489 break; // Got too many items. 1490 1491 // Get the URL, querying the main and archived databases as necessary. If 1492 // this is not found, the history and full text search databases are out 1493 // of sync and we give up with this result. 1494 if (!querier.GetRowForURL(fts_matches[i].url, &url_result)) 1495 continue; 1496 1497 if (!url_result.url().is_valid()) 1498 continue; // Don't report invalid URLs in case of corruption. 1499 1500 // Copy over the FTS stuff that the URLDatabase doesn't know about. 1501 // We do this with swap() to avoid copying, since we know we don't 1502 // need the original any more. Note that we override the title with the 1503 // one from FTS, since that will match the title_match_positions (the 1504 // FTS title and the history DB title may differ). 1505 url_result.set_title(fts_matches[i].title); 1506 url_result.title_match_positions_.swap( 1507 fts_matches[i].title_match_positions); 1508 url_result.snippet_.Swap(&fts_matches[i].snippet); 1509 1510 // The visit time also comes from the full text search database. Since it 1511 // has the time, we can avoid an extra query of the visits table. 1512 url_result.set_visit_time(fts_matches[i].time); 1513 1514 // Add it to the vector, this will clear our |url_row| object as a 1515 // result of the swap. 1516 result->AppendURLBySwapping(&url_result); 1517 } 1518 1519 if (first_time_searched <= first_recorded_time_) 1520 result->set_reached_beginning(true); 1521} 1522 1523// Frontend to GetMostRecentRedirectsFrom from the history thread. 1524void HistoryBackend::QueryRedirectsFrom( 1525 scoped_refptr<QueryRedirectsRequest> request, 1526 const GURL& url) { 1527 if (request->canceled()) 1528 return; 1529 bool success = GetMostRecentRedirectsFrom(url, &request->value); 1530 request->ForwardResult(request->handle(), url, success, &request->value); 1531} 1532 1533void HistoryBackend::QueryRedirectsTo( 1534 scoped_refptr<QueryRedirectsRequest> request, 1535 const GURL& url) { 1536 if (request->canceled()) 1537 return; 1538 bool success = GetMostRecentRedirectsTo(url, &request->value); 1539 request->ForwardResult(request->handle(), url, success, &request->value); 1540} 1541 1542void HistoryBackend::GetVisibleVisitCountToHost( 1543 scoped_refptr<GetVisibleVisitCountToHostRequest> request, 1544 const GURL& url) { 1545 if (request->canceled()) 1546 return; 1547 int count = 0; 1548 Time first_visit; 1549 const bool success = db_.get() && 1550 db_->GetVisibleVisitCountToHost(url, &count, &first_visit); 1551 request->ForwardResult(request->handle(), success, count, first_visit); 1552} 1553 1554void HistoryBackend::QueryTopURLsAndRedirects( 1555 scoped_refptr<QueryTopURLsAndRedirectsRequest> request, 1556 int result_count) { 1557 if (request->canceled()) 1558 return; 1559 1560 if (!db_) { 1561 request->ForwardResult(request->handle(), false, NULL, NULL); 1562 return; 1563 } 1564 1565 std::vector<GURL>* top_urls = &request->value.a; 1566 history::RedirectMap* redirects = &request->value.b; 1567 1568 ScopedVector<PageUsageData> data; 1569 db_->QuerySegmentUsage(base::Time::Now() - base::TimeDelta::FromDays(90), 1570 result_count, &data.get()); 1571 1572 for (size_t i = 0; i < data.size(); ++i) { 1573 top_urls->push_back(data[i]->GetURL()); 1574 RefCountedVector<GURL>* list = new RefCountedVector<GURL>; 1575 GetMostRecentRedirectsFrom(top_urls->back(), &list->data); 1576 (*redirects)[top_urls->back()] = list; 1577 } 1578 1579 request->ForwardResult(request->handle(), true, top_urls, redirects); 1580} 1581 1582// Will replace QueryTopURLsAndRedirectsRequest. 1583void HistoryBackend::QueryMostVisitedURLs( 1584 scoped_refptr<QueryMostVisitedURLsRequest> request, 1585 int result_count, 1586 int days_back) { 1587 if (request->canceled()) 1588 return; 1589 1590 if (!db_) { 1591 // No History Database - return an empty list. 1592 request->ForwardResult(request->handle(), MostVisitedURLList()); 1593 return; 1594 } 1595 1596 MostVisitedURLList* result = &request->value; 1597 QueryMostVisitedURLsImpl(result_count, days_back, result); 1598 request->ForwardResult(request->handle(), *result); 1599} 1600 1601void HistoryBackend::QueryFilteredURLs( 1602 scoped_refptr<QueryFilteredURLsRequest> request, 1603 int result_count, 1604 const history::VisitFilter& filter, 1605 bool extended_info) { 1606 if (request->canceled()) 1607 return; 1608 1609 base::Time request_start = base::Time::Now(); 1610 1611 if (!db_) { 1612 // No History Database - return an empty list. 1613 request->ForwardResult(request->handle(), FilteredURLList()); 1614 return; 1615 } 1616 1617 VisitVector visits; 1618 db_->GetDirectVisitsDuringTimes(filter, 0, &visits); 1619 1620 std::map<URLID, double> score_map; 1621 for (size_t i = 0; i < visits.size(); ++i) { 1622 score_map[visits[i].url_id] += filter.GetVisitScore(visits[i]); 1623 } 1624 1625 // TODO(georgey): experiment with visit_segment database granularity (it is 1626 // currently 24 hours) to use it directly instead of using visits database, 1627 // which is considerably slower. 1628 ScopedVector<PageUsageData> data; 1629 data.reserve(score_map.size()); 1630 for (std::map<URLID, double>::iterator it = score_map.begin(); 1631 it != score_map.end(); ++it) { 1632 PageUsageData* pud = new PageUsageData(it->first); 1633 pud->SetScore(it->second); 1634 data.push_back(pud); 1635 } 1636 1637 // Limit to the top |result_count| results. 1638 std::sort(data.begin(), data.end(), PageUsageData::Predicate); 1639 if (result_count && implicit_cast<int>(data.size()) > result_count) 1640 data.resize(result_count); 1641 1642 for (size_t i = 0; i < data.size(); ++i) { 1643 URLRow info; 1644 if (db_->GetURLRow(data[i]->GetID(), &info)) { 1645 data[i]->SetURL(info.url()); 1646 data[i]->SetTitle(info.title()); 1647 } 1648 } 1649 1650 FilteredURLList& result = request->value; 1651 for (size_t i = 0; i < data.size(); ++i) { 1652 PageUsageData* current_data = data[i]; 1653 FilteredURL url(*current_data); 1654 1655 if (extended_info) { 1656 VisitVector visits; 1657 db_->GetVisitsForURL(current_data->GetID(), &visits); 1658 if (visits.size() > 0) { 1659 url.extended_info.total_visits = visits.size(); 1660 for (size_t i = 0; i < visits.size(); ++i) { 1661 url.extended_info.duration_opened += 1662 visits[i].visit_duration.InSeconds(); 1663 if (visits[i].visit_time > url.extended_info.last_visit_time) { 1664 url.extended_info.last_visit_time = visits[i].visit_time; 1665 } 1666 } 1667 // TODO(macourteau): implement the url.extended_info.visits stat. 1668 } 1669 } 1670 result.push_back(url); 1671 } 1672 1673 int delta_time = std::max(1, std::min(999, 1674 static_cast<int>((base::Time::Now() - request_start).InMilliseconds()))); 1675 STATIC_HISTOGRAM_POINTER_BLOCK( 1676 "NewTabPage.SuggestedSitesLoadTime", 1677 Add(delta_time), 1678 base::LinearHistogram::FactoryGet("NewTabPage.SuggestedSitesLoadTime", 1679 1, 1000, 100, base::Histogram::kUmaTargetedHistogramFlag)); 1680 1681 request->ForwardResult(request->handle(), result); 1682} 1683 1684void HistoryBackend::QueryMostVisitedURLsImpl(int result_count, 1685 int days_back, 1686 MostVisitedURLList* result) { 1687 if (!db_) 1688 return; 1689 1690 ScopedVector<PageUsageData> data; 1691 db_->QuerySegmentUsage(base::Time::Now() - 1692 base::TimeDelta::FromDays(days_back), 1693 result_count, &data.get()); 1694 1695 for (size_t i = 0; i < data.size(); ++i) { 1696 PageUsageData* current_data = data[i]; 1697 RedirectList redirects; 1698 GetMostRecentRedirectsFrom(current_data->GetURL(), &redirects); 1699 MostVisitedURL url = MakeMostVisitedURL(*current_data, redirects); 1700 result->push_back(url); 1701 } 1702} 1703 1704void HistoryBackend::GetRedirectsFromSpecificVisit( 1705 VisitID cur_visit, history::RedirectList* redirects) { 1706 // Follow any redirects from the given visit and add them to the list. 1707 // It *should* be impossible to get a circular chain here, but we check 1708 // just in case to avoid infinite loops. 1709 GURL cur_url; 1710 std::set<VisitID> visit_set; 1711 visit_set.insert(cur_visit); 1712 while (db_->GetRedirectFromVisit(cur_visit, &cur_visit, &cur_url)) { 1713 if (visit_set.find(cur_visit) != visit_set.end()) { 1714 NOTREACHED() << "Loop in visit chain, giving up"; 1715 return; 1716 } 1717 visit_set.insert(cur_visit); 1718 redirects->push_back(cur_url); 1719 } 1720} 1721 1722void HistoryBackend::GetRedirectsToSpecificVisit( 1723 VisitID cur_visit, 1724 history::RedirectList* redirects) { 1725 // Follow redirects going to cur_visit. These are added to |redirects| in 1726 // the order they are found. If a redirect chain looks like A -> B -> C and 1727 // |cur_visit| = C, redirects will be {B, A} in that order. 1728 if (!db_) 1729 return; 1730 1731 GURL cur_url; 1732 std::set<VisitID> visit_set; 1733 visit_set.insert(cur_visit); 1734 while (db_->GetRedirectToVisit(cur_visit, &cur_visit, &cur_url)) { 1735 if (visit_set.find(cur_visit) != visit_set.end()) { 1736 NOTREACHED() << "Loop in visit chain, giving up"; 1737 return; 1738 } 1739 visit_set.insert(cur_visit); 1740 redirects->push_back(cur_url); 1741 } 1742} 1743 1744bool HistoryBackend::GetMostRecentRedirectsFrom( 1745 const GURL& from_url, 1746 history::RedirectList* redirects) { 1747 redirects->clear(); 1748 if (!db_) 1749 return false; 1750 1751 URLID from_url_id = db_->GetRowForURL(from_url, NULL); 1752 VisitID cur_visit = db_->GetMostRecentVisitForURL(from_url_id, NULL); 1753 if (!cur_visit) 1754 return false; // No visits for URL. 1755 1756 GetRedirectsFromSpecificVisit(cur_visit, redirects); 1757 return true; 1758} 1759 1760bool HistoryBackend::GetMostRecentRedirectsTo( 1761 const GURL& to_url, 1762 history::RedirectList* redirects) { 1763 redirects->clear(); 1764 if (!db_) 1765 return false; 1766 1767 URLID to_url_id = db_->GetRowForURL(to_url, NULL); 1768 VisitID cur_visit = db_->GetMostRecentVisitForURL(to_url_id, NULL); 1769 if (!cur_visit) 1770 return false; // No visits for URL. 1771 1772 GetRedirectsToSpecificVisit(cur_visit, redirects); 1773 return true; 1774} 1775 1776void HistoryBackend::ScheduleAutocomplete(HistoryURLProvider* provider, 1777 HistoryURLProviderParams* params) { 1778 // ExecuteWithDB should handle the NULL database case. 1779 provider->ExecuteWithDB(this, db_.get(), params); 1780} 1781 1782void HistoryBackend::SetPageContents(const GURL& url, 1783 const string16& contents) { 1784 // This is histogrammed in the text database manager. 1785 if (!text_database_) 1786 return; 1787 text_database_->AddPageContents(url, contents); 1788} 1789 1790void HistoryBackend::SetPageThumbnail( 1791 const GURL& url, 1792 const gfx::Image* thumbnail, 1793 const ThumbnailScore& score) { 1794 if (!db_ || !thumbnail_db_) 1795 return; 1796 1797 URLRow url_row; 1798 URLID url_id = db_->GetRowForURL(url, &url_row); 1799 if (url_id) { 1800 thumbnail_db_->SetPageThumbnail(url, url_id, thumbnail, score, 1801 url_row.last_visit()); 1802 } 1803 1804 ScheduleCommit(); 1805} 1806 1807void HistoryBackend::GetPageThumbnail( 1808 scoped_refptr<GetPageThumbnailRequest> request, 1809 const GURL& page_url) { 1810 if (request->canceled()) 1811 return; 1812 1813 scoped_refptr<base::RefCountedBytes> data; 1814 GetPageThumbnailDirectly(page_url, &data); 1815 1816 request->ForwardResult(request->handle(), data); 1817} 1818 1819void HistoryBackend::GetPageThumbnailDirectly( 1820 const GURL& page_url, 1821 scoped_refptr<base::RefCountedBytes>* data) { 1822 if (thumbnail_db_) { 1823 *data = new base::RefCountedBytes; 1824 1825 // Time the result. 1826 TimeTicks beginning_time = TimeTicks::Now(); 1827 1828 history::RedirectList redirects; 1829 URLID url_id; 1830 bool success = false; 1831 1832 // If there are some redirects, try to get a thumbnail from the last 1833 // redirect destination. 1834 if (GetMostRecentRedirectsFrom(page_url, &redirects) && 1835 !redirects.empty()) { 1836 if ((url_id = db_->GetRowForURL(redirects.back(), NULL))) 1837 success = thumbnail_db_->GetPageThumbnail(url_id, &(*data)->data()); 1838 } 1839 1840 // If we don't have a thumbnail from redirects, try the URL directly. 1841 if (!success) { 1842 if ((url_id = db_->GetRowForURL(page_url, NULL))) 1843 success = thumbnail_db_->GetPageThumbnail(url_id, &(*data)->data()); 1844 } 1845 1846 // In this rare case, we start to mine the older redirect sessions 1847 // from the visit table to try to find a thumbnail. 1848 if (!success) { 1849 success = GetThumbnailFromOlderRedirect(page_url, &(*data)->data()); 1850 } 1851 1852 if (!success) 1853 *data = NULL; // This will tell the callback there was an error. 1854 1855 UMA_HISTOGRAM_TIMES("History.GetPageThumbnail", 1856 TimeTicks::Now() - beginning_time); 1857 } 1858} 1859 1860void HistoryBackend::MigrateThumbnailsDatabase() { 1861 // If there is no History DB, we can't record that the migration was done. 1862 // It will be recorded on the next run. 1863 if (db_) { 1864 // If there is no thumbnail DB, we can still record a successful migration. 1865 if (thumbnail_db_) { 1866 thumbnail_db_->RenameAndDropThumbnails(GetThumbnailFileName(), 1867 GetFaviconsFileName()); 1868 } 1869 db_->ThumbnailMigrationDone(); 1870 } 1871} 1872 1873bool HistoryBackend::GetThumbnailFromOlderRedirect( 1874 const GURL& page_url, 1875 std::vector<unsigned char>* data) { 1876 // Look at a few previous visit sessions. 1877 VisitVector older_sessions; 1878 URLID page_url_id = db_->GetRowForURL(page_url, NULL); 1879 static const int kVisitsToSearchForThumbnail = 4; 1880 db_->GetMostRecentVisitsForURL( 1881 page_url_id, kVisitsToSearchForThumbnail, &older_sessions); 1882 1883 // Iterate across all those previous visits, and see if any of the 1884 // final destinations of those redirect chains have a good thumbnail 1885 // for us. 1886 bool success = false; 1887 for (VisitVector::const_iterator it = older_sessions.begin(); 1888 !success && it != older_sessions.end(); ++it) { 1889 history::RedirectList redirects; 1890 if (it->visit_id) { 1891 GetRedirectsFromSpecificVisit(it->visit_id, &redirects); 1892 1893 if (!redirects.empty()) { 1894 URLID url_id; 1895 if ((url_id = db_->GetRowForURL(redirects.back(), NULL))) 1896 success = thumbnail_db_->GetPageThumbnail(url_id, data); 1897 } 1898 } 1899 } 1900 1901 return success; 1902} 1903 1904void HistoryBackend::GetFavicons( 1905 const std::vector<GURL>& icon_urls, 1906 int icon_types, 1907 int desired_size_in_dip, 1908 const std::vector<ui::ScaleFactor>& desired_scale_factors, 1909 std::vector<FaviconBitmapResult>* bitmap_results) { 1910 UpdateFaviconMappingsAndFetchImpl(NULL, icon_urls, icon_types, 1911 desired_size_in_dip, desired_scale_factors, 1912 bitmap_results); 1913} 1914 1915void HistoryBackend::GetFaviconsForURL( 1916 const GURL& page_url, 1917 int icon_types, 1918 int desired_size_in_dip, 1919 const std::vector<ui::ScaleFactor>& desired_scale_factors, 1920 std::vector<FaviconBitmapResult>* bitmap_results) { 1921 DCHECK(bitmap_results); 1922 GetFaviconsFromDB(page_url, icon_types, desired_size_in_dip, 1923 desired_scale_factors, bitmap_results); 1924} 1925 1926void HistoryBackend::GetFaviconForID( 1927 FaviconID favicon_id, 1928 int desired_size_in_dip, 1929 ui::ScaleFactor desired_scale_factor, 1930 std::vector<FaviconBitmapResult>* bitmap_results) { 1931 std::vector<FaviconID> favicon_ids; 1932 favicon_ids.push_back(favicon_id); 1933 std::vector<ui::ScaleFactor> desired_scale_factors; 1934 desired_scale_factors.push_back(desired_scale_factor); 1935 1936 // Get results from DB. 1937 GetFaviconBitmapResultsForBestMatch(favicon_ids, 1938 desired_size_in_dip, 1939 desired_scale_factors, 1940 bitmap_results); 1941} 1942 1943void HistoryBackend::UpdateFaviconMappingsAndFetch( 1944 const GURL& page_url, 1945 const std::vector<GURL>& icon_urls, 1946 int icon_types, 1947 int desired_size_in_dip, 1948 const std::vector<ui::ScaleFactor>& desired_scale_factors, 1949 std::vector<FaviconBitmapResult>* bitmap_results) { 1950 UpdateFaviconMappingsAndFetchImpl(&page_url, icon_urls, icon_types, 1951 desired_size_in_dip, desired_scale_factors, 1952 bitmap_results); 1953} 1954 1955void HistoryBackend::MergeFavicon( 1956 const GURL& page_url, 1957 const GURL& icon_url, 1958 history::IconType icon_type, 1959 scoped_refptr<base::RefCountedMemory> bitmap_data, 1960 const gfx::Size& pixel_size) { 1961 if (!thumbnail_db_ || !db_) 1962 return; 1963 1964 FaviconID favicon_id = thumbnail_db_->GetFaviconIDForFaviconURL(icon_url, 1965 icon_type, NULL); 1966 1967 if (!favicon_id) { 1968 // There is no favicon at |icon_url|, create it. 1969 favicon_id = thumbnail_db_->AddFavicon(icon_url, icon_type, 1970 GetDefaultFaviconSizes()); 1971 } 1972 1973 std::vector<FaviconBitmapIDSize> bitmap_id_sizes; 1974 thumbnail_db_->GetFaviconBitmapIDSizes(favicon_id, &bitmap_id_sizes); 1975 1976 // If there is already a favicon bitmap of |pixel_size| at |icon_url|, 1977 // replace it. 1978 bool bitmap_identical = false; 1979 bool replaced_bitmap = false; 1980 for (size_t i = 0; i < bitmap_id_sizes.size(); ++i) { 1981 if (bitmap_id_sizes[i].pixel_size == pixel_size) { 1982 if (IsFaviconBitmapDataEqual(bitmap_id_sizes[i].bitmap_id, bitmap_data)) { 1983 thumbnail_db_->SetFaviconBitmapLastUpdateTime( 1984 bitmap_id_sizes[i].bitmap_id, base::Time::Now()); 1985 bitmap_identical = true; 1986 } else { 1987 thumbnail_db_->SetFaviconBitmap(bitmap_id_sizes[i].bitmap_id, 1988 bitmap_data, base::Time::Now()); 1989 replaced_bitmap = true; 1990 } 1991 break; 1992 } 1993 } 1994 1995 // Create a vector of the pixel sizes of the favicon bitmaps currently at 1996 // |icon_url|. 1997 std::vector<gfx::Size> favicon_sizes; 1998 for (size_t i = 0; i < bitmap_id_sizes.size(); ++i) 1999 favicon_sizes.push_back(bitmap_id_sizes[i].pixel_size); 2000 2001 if (!replaced_bitmap && !bitmap_identical) { 2002 // Set the preexisting favicon bitmaps as expired as the preexisting favicon 2003 // bitmaps are not consistent with the merged in data. 2004 thumbnail_db_->SetFaviconOutOfDate(favicon_id); 2005 2006 // Delete an arbitrary favicon bitmap to avoid going over the limit of 2007 // |kMaxFaviconBitmapsPerIconURL|. 2008 if (bitmap_id_sizes.size() >= kMaxFaviconBitmapsPerIconURL) { 2009 thumbnail_db_->DeleteFaviconBitmap(bitmap_id_sizes[0].bitmap_id); 2010 favicon_sizes.erase(favicon_sizes.begin()); 2011 } 2012 thumbnail_db_->AddFaviconBitmap(favicon_id, bitmap_data, base::Time::Now(), 2013 pixel_size); 2014 favicon_sizes.push_back(pixel_size); 2015 } 2016 2017 // A site may have changed the favicons that it uses for |page_url|. 2018 // Example Scenario: 2019 // page_url = news.google.com 2020 // Intial State: www.google.com/favicon.ico 16x16, 32x32 2021 // MergeFavicon(news.google.com, news.google.com/news_specific.ico, ..., 2022 // ..., 16x16) 2023 // 2024 // Difficulties: 2025 // 1. Sync requires that a call to GetFaviconsForURL() returns the 2026 // |bitmap_data| passed into MergeFavicon(). 2027 // - It is invalid for the 16x16 bitmap for www.google.com/favicon.ico to 2028 // stay mapped to news.google.com because it would be unclear which 16x16 2029 // bitmap should be returned via GetFaviconsForURL(). 2030 // 2031 // 2. www.google.com/favicon.ico may be mapped to more than just 2032 // news.google.com (eg www.google.com). 2033 // - The 16x16 bitmap cannot be deleted from www.google.com/favicon.ico 2034 // 2035 // To resolve these problems, we copy all of the favicon bitmaps previously 2036 // mapped to news.google.com (|page_url|) and add them to the favicon at 2037 // news.google.com/news_specific.ico (|icon_url|). The favicon sizes for 2038 // |icon_url| are set to default to indicate that |icon_url| has incomplete 2039 // / incorrect data. 2040 // Difficlty 1: All but news.google.com/news_specific.ico are unmapped from 2041 // news.google.com 2042 // Difficulty 2: The favicon bitmaps for www.google.com/favicon.ico are not 2043 // modified. 2044 2045 std::vector<IconMapping> icon_mappings; 2046 thumbnail_db_->GetIconMappingsForPageURL(page_url, icon_type, &icon_mappings); 2047 2048 // Copy the favicon bitmaps mapped to |page_url| to the favicon at |icon_url| 2049 // till the limit of |kMaxFaviconBitmapsPerIconURL| is reached. 2050 for (size_t i = 0; i < icon_mappings.size(); ++i) { 2051 if (favicon_sizes.size() >= kMaxFaviconBitmapsPerIconURL) 2052 break; 2053 2054 if (icon_mappings[i].icon_url == icon_url) 2055 continue; 2056 2057 std::vector<FaviconBitmap> bitmaps_to_copy; 2058 thumbnail_db_->GetFaviconBitmaps(icon_mappings[i].icon_id, 2059 &bitmaps_to_copy); 2060 for (size_t j = 0; j < bitmaps_to_copy.size(); ++j) { 2061 // Do not add a favicon bitmap at a pixel size for which there is already 2062 // a favicon bitmap mapped to |icon_url|. The one there is more correct 2063 // and having multiple equally sized favicon bitmaps for |page_url| is 2064 // ambiguous in terms of GetFaviconsForURL(). 2065 std::vector<gfx::Size>::iterator it = std::find(favicon_sizes.begin(), 2066 favicon_sizes.end(), bitmaps_to_copy[j].pixel_size); 2067 if (it != favicon_sizes.end()) 2068 continue; 2069 2070 // Add the favicon bitmap as expired as it is not consistent with the 2071 // merged in data. 2072 thumbnail_db_->AddFaviconBitmap(favicon_id, 2073 bitmaps_to_copy[j].bitmap_data, base::Time(), 2074 bitmaps_to_copy[j].pixel_size); 2075 favicon_sizes.push_back(bitmaps_to_copy[j].pixel_size); 2076 2077 if (favicon_sizes.size() >= kMaxFaviconBitmapsPerIconURL) 2078 break; 2079 } 2080 } 2081 2082 // Update the favicon mappings such that only |icon_url| is mapped to 2083 // |page_url|. 2084 bool mapping_changed = false; 2085 if (icon_mappings.size() != 1 || icon_mappings[0].icon_url != icon_url) { 2086 std::vector<FaviconID> favicon_ids; 2087 favicon_ids.push_back(favicon_id); 2088 SetFaviconMappingsForPageAndRedirects(page_url, icon_type, favicon_ids); 2089 mapping_changed = true; 2090 } 2091 2092 if (mapping_changed || !bitmap_identical) 2093 SendFaviconChangedNotificationForPageAndRedirects(page_url); 2094 ScheduleCommit(); 2095} 2096 2097void HistoryBackend::SetFavicons( 2098 const GURL& page_url, 2099 IconType icon_type, 2100 const std::vector<FaviconBitmapData>& favicon_bitmap_data) { 2101 if (!thumbnail_db_ || !db_) 2102 return; 2103 2104 DCHECK(ValidateSetFaviconsParams(favicon_bitmap_data)); 2105 2106 // Build map of FaviconBitmapData for each icon url. 2107 typedef std::map<GURL, std::vector<FaviconBitmapData> > 2108 BitmapDataByIconURL; 2109 BitmapDataByIconURL grouped_by_icon_url; 2110 for (size_t i = 0; i < favicon_bitmap_data.size(); ++i) { 2111 const GURL& icon_url = favicon_bitmap_data[i].icon_url; 2112 grouped_by_icon_url[icon_url].push_back(favicon_bitmap_data[i]); 2113 } 2114 2115 // Track whether the method modifies or creates any favicon bitmaps, favicons 2116 // or icon mappings. 2117 bool data_modified = false; 2118 2119 std::vector<FaviconID> icon_ids; 2120 for (BitmapDataByIconURL::const_iterator it = grouped_by_icon_url.begin(); 2121 it != grouped_by_icon_url.end(); ++it) { 2122 const GURL& icon_url = it->first; 2123 FaviconID icon_id = 2124 thumbnail_db_->GetFaviconIDForFaviconURL(icon_url, icon_type, NULL); 2125 2126 if (!icon_id) { 2127 // TODO(pkotwicz): Remove the favicon sizes attribute from 2128 // ThumbnailDatabase::AddFavicon(). 2129 icon_id = thumbnail_db_->AddFavicon(icon_url, icon_type, 2130 GetDefaultFaviconSizes()); 2131 data_modified = true; 2132 } 2133 icon_ids.push_back(icon_id); 2134 2135 if (!data_modified) 2136 SetFaviconBitmaps(icon_id, it->second, &data_modified); 2137 else 2138 SetFaviconBitmaps(icon_id, it->second, NULL); 2139 } 2140 2141 data_modified |= 2142 SetFaviconMappingsForPageAndRedirects(page_url, icon_type, icon_ids); 2143 2144 if (data_modified) { 2145 // Send notification to the UI as an icon mapping, favicon, or favicon 2146 // bitmap was changed by this function. 2147 SendFaviconChangedNotificationForPageAndRedirects(page_url); 2148 } 2149 ScheduleCommit(); 2150} 2151 2152void HistoryBackend::SetFaviconsOutOfDateForPage(const GURL& page_url) { 2153 std::vector<IconMapping> icon_mappings; 2154 2155 if (!thumbnail_db_ || 2156 !thumbnail_db_->GetIconMappingsForPageURL(page_url, 2157 &icon_mappings)) 2158 return; 2159 2160 for (std::vector<IconMapping>::iterator m = icon_mappings.begin(); 2161 m != icon_mappings.end(); ++m) { 2162 thumbnail_db_->SetFaviconOutOfDate(m->icon_id); 2163 } 2164 ScheduleCommit(); 2165} 2166 2167void HistoryBackend::CloneFavicons(const GURL& old_page_url, 2168 const GURL& new_page_url) { 2169 if (!thumbnail_db_) 2170 return; 2171 2172 // Prevent cross-domain cloning. 2173 if (old_page_url.GetOrigin() != new_page_url.GetOrigin()) 2174 return; 2175 2176 thumbnail_db_->CloneIconMappings(old_page_url, new_page_url); 2177 ScheduleCommit(); 2178} 2179 2180void HistoryBackend::SetImportedFavicons( 2181 const std::vector<ImportedFaviconUsage>& favicon_usage) { 2182 if (!db_ || !thumbnail_db_) 2183 return; 2184 2185 Time now = Time::Now(); 2186 2187 // Track all URLs that had their favicons set or updated. 2188 std::set<GURL> favicons_changed; 2189 2190 for (size_t i = 0; i < favicon_usage.size(); i++) { 2191 FaviconID favicon_id = thumbnail_db_->GetFaviconIDForFaviconURL( 2192 favicon_usage[i].favicon_url, history::FAVICON, NULL); 2193 if (!favicon_id) { 2194 // This favicon doesn't exist yet, so we create it using the given data. 2195 // TODO(pkotwicz): Pass in real pixel size. 2196 favicon_id = thumbnail_db_->AddFavicon( 2197 favicon_usage[i].favicon_url, 2198 history::FAVICON, 2199 GetDefaultFaviconSizes(), 2200 new base::RefCountedBytes(favicon_usage[i].png_data), 2201 now, 2202 gfx::Size()); 2203 } 2204 2205 // Save the mapping from all the URLs to the favicon. 2206 BookmarkService* bookmark_service = GetBookmarkService(); 2207 for (std::set<GURL>::const_iterator url = favicon_usage[i].urls.begin(); 2208 url != favicon_usage[i].urls.end(); ++url) { 2209 URLRow url_row; 2210 if (!db_->GetRowForURL(*url, &url_row)) { 2211 // If the URL is present as a bookmark, add the url in history to 2212 // save the favicon mapping. This will match with what history db does 2213 // for regular bookmarked URLs with favicons - when history db is 2214 // cleaned, we keep an entry in the db with 0 visits as long as that 2215 // url is bookmarked. 2216 if (bookmark_service && bookmark_service_->IsBookmarked(*url)) { 2217 URLRow url_info(*url); 2218 url_info.set_visit_count(0); 2219 url_info.set_typed_count(0); 2220 url_info.set_last_visit(base::Time()); 2221 url_info.set_hidden(false); 2222 db_->AddURL(url_info); 2223 thumbnail_db_->AddIconMapping(*url, favicon_id); 2224 favicons_changed.insert(*url); 2225 } 2226 } else { 2227 if (!thumbnail_db_->GetIconMappingsForPageURL(*url, FAVICON, NULL)) { 2228 // URL is present in history, update the favicon *only* if it is not 2229 // set already. 2230 thumbnail_db_->AddIconMapping(*url, favicon_id); 2231 favicons_changed.insert(*url); 2232 } 2233 } 2234 } 2235 } 2236 2237 if (!favicons_changed.empty()) { 2238 // Send the notification about the changed favicon URLs. 2239 FaviconChangeDetails* changed_details = new FaviconChangeDetails; 2240 changed_details->urls.swap(favicons_changed); 2241 BroadcastNotifications(chrome::NOTIFICATION_FAVICON_CHANGED, 2242 changed_details); 2243 } 2244} 2245 2246void HistoryBackend::UpdateFaviconMappingsAndFetchImpl( 2247 const GURL* page_url, 2248 const std::vector<GURL>& icon_urls, 2249 int icon_types, 2250 int desired_size_in_dip, 2251 const std::vector<ui::ScaleFactor>& desired_scale_factors, 2252 std::vector<FaviconBitmapResult>* bitmap_results) { 2253 // If |page_url| is specified, |icon_types| must be either a single icon 2254 // type or icon types which are equivalent. 2255 DCHECK(!page_url || 2256 icon_types == FAVICON || 2257 icon_types == TOUCH_ICON || 2258 icon_types == TOUCH_PRECOMPOSED_ICON || 2259 icon_types == (TOUCH_ICON | TOUCH_PRECOMPOSED_ICON)); 2260 bitmap_results->clear(); 2261 2262 if (!thumbnail_db_) { 2263 return; 2264 } 2265 2266 std::vector<FaviconID> favicon_ids; 2267 2268 // The icon type for which the mappings will the updated and data will be 2269 // returned. 2270 IconType selected_icon_type = INVALID_ICON; 2271 2272 for (size_t i = 0; i < icon_urls.size(); ++i) { 2273 const GURL& icon_url = icon_urls[i]; 2274 IconType icon_type_out; 2275 const FaviconID favicon_id = thumbnail_db_->GetFaviconIDForFaviconURL( 2276 icon_url, icon_types, &icon_type_out); 2277 2278 if (favicon_id) { 2279 // Return and update icon mappings only for the largest icon type. As 2280 // |icon_urls| is not sorted in terms of icon type, clear |favicon_ids| 2281 // if an |icon_url| with a larger icon type is found. 2282 if (icon_type_out > selected_icon_type) { 2283 selected_icon_type = icon_type_out; 2284 favicon_ids.clear(); 2285 } 2286 if (icon_type_out == selected_icon_type) 2287 favicon_ids.push_back(favicon_id); 2288 } 2289 } 2290 2291 if (page_url && !favicon_ids.empty()) { 2292 bool mappings_updated = 2293 SetFaviconMappingsForPageAndRedirects(*page_url, selected_icon_type, 2294 favicon_ids); 2295 if (mappings_updated) { 2296 SendFaviconChangedNotificationForPageAndRedirects(*page_url); 2297 ScheduleCommit(); 2298 } 2299 } 2300 2301 GetFaviconBitmapResultsForBestMatch(favicon_ids, desired_size_in_dip, 2302 desired_scale_factors, bitmap_results); 2303} 2304 2305void HistoryBackend::SetFaviconBitmaps( 2306 FaviconID icon_id, 2307 const std::vector<FaviconBitmapData>& favicon_bitmap_data, 2308 bool* favicon_bitmaps_changed) { 2309 if (favicon_bitmaps_changed) 2310 *favicon_bitmaps_changed = false; 2311 2312 std::vector<FaviconBitmapIDSize> bitmap_id_sizes; 2313 thumbnail_db_->GetFaviconBitmapIDSizes(icon_id, &bitmap_id_sizes); 2314 2315 std::vector<FaviconBitmapData> to_add = favicon_bitmap_data; 2316 2317 for (size_t i = 0; i < bitmap_id_sizes.size(); ++i) { 2318 const gfx::Size& pixel_size = bitmap_id_sizes[i].pixel_size; 2319 std::vector<FaviconBitmapData>::iterator match_it = to_add.end(); 2320 for (std::vector<FaviconBitmapData>::iterator it = to_add.begin(); 2321 it != to_add.end(); ++it) { 2322 if (it->pixel_size == pixel_size) { 2323 match_it = it; 2324 break; 2325 } 2326 } 2327 2328 FaviconBitmapID bitmap_id = bitmap_id_sizes[i].bitmap_id; 2329 if (match_it == to_add.end()) { 2330 thumbnail_db_->DeleteFaviconBitmap(bitmap_id); 2331 2332 if (favicon_bitmaps_changed) 2333 *favicon_bitmaps_changed = true; 2334 } else { 2335 if (favicon_bitmaps_changed && 2336 !*favicon_bitmaps_changed && 2337 IsFaviconBitmapDataEqual(bitmap_id, match_it->bitmap_data)) { 2338 thumbnail_db_->SetFaviconBitmapLastUpdateTime( 2339 bitmap_id, base::Time::Now()); 2340 } else { 2341 thumbnail_db_->SetFaviconBitmap(bitmap_id, match_it->bitmap_data, 2342 base::Time::Now()); 2343 2344 if (favicon_bitmaps_changed) 2345 *favicon_bitmaps_changed = true; 2346 } 2347 to_add.erase(match_it); 2348 } 2349 } 2350 2351 for (size_t i = 0; i < to_add.size(); ++i) { 2352 thumbnail_db_->AddFaviconBitmap(icon_id, to_add[i].bitmap_data, 2353 base::Time::Now(), to_add[i].pixel_size); 2354 2355 if (favicon_bitmaps_changed) 2356 *favicon_bitmaps_changed = true; 2357 } 2358} 2359 2360bool HistoryBackend::ValidateSetFaviconsParams( 2361 const std::vector<FaviconBitmapData>& favicon_bitmap_data) const { 2362 typedef std::map<GURL, size_t> BitmapsPerIconURL; 2363 BitmapsPerIconURL num_bitmaps_per_icon_url; 2364 for (size_t i = 0; i < favicon_bitmap_data.size(); ++i) { 2365 if (!favicon_bitmap_data[i].bitmap_data) 2366 return false; 2367 2368 const GURL& icon_url = favicon_bitmap_data[i].icon_url; 2369 if (!num_bitmaps_per_icon_url.count(icon_url)) 2370 num_bitmaps_per_icon_url[icon_url] = 1u; 2371 else 2372 ++num_bitmaps_per_icon_url[icon_url]; 2373 } 2374 2375 if (num_bitmaps_per_icon_url.size() > kMaxFaviconsPerPage) 2376 return false; 2377 2378 for (BitmapsPerIconURL::const_iterator it = num_bitmaps_per_icon_url.begin(); 2379 it != num_bitmaps_per_icon_url.end(); ++it) { 2380 if (it->second > kMaxFaviconBitmapsPerIconURL) 2381 return false; 2382 } 2383 return true; 2384} 2385 2386bool HistoryBackend::IsFaviconBitmapDataEqual( 2387 FaviconBitmapID bitmap_id, 2388 const scoped_refptr<base::RefCountedMemory>& new_bitmap_data) { 2389 if (!new_bitmap_data) 2390 return false; 2391 2392 scoped_refptr<base::RefCountedMemory> original_bitmap_data; 2393 thumbnail_db_->GetFaviconBitmap(bitmap_id, 2394 NULL, 2395 &original_bitmap_data, 2396 NULL); 2397 return new_bitmap_data->Equals(original_bitmap_data); 2398} 2399 2400bool HistoryBackend::GetFaviconsFromDB( 2401 const GURL& page_url, 2402 int icon_types, 2403 int desired_size_in_dip, 2404 const std::vector<ui::ScaleFactor>& desired_scale_factors, 2405 std::vector<FaviconBitmapResult>* favicon_bitmap_results) { 2406 DCHECK(favicon_bitmap_results); 2407 favicon_bitmap_results->clear(); 2408 2409 if (!db_ || !thumbnail_db_) 2410 return false; 2411 2412 // Time the query. 2413 TimeTicks beginning_time = TimeTicks::Now(); 2414 2415 // Get FaviconIDs for |page_url| and one of |icon_types|. 2416 std::vector<IconMapping> icon_mappings; 2417 thumbnail_db_->GetIconMappingsForPageURL(page_url, icon_types, 2418 &icon_mappings); 2419 std::vector<FaviconID> favicon_ids; 2420 for (size_t i = 0; i < icon_mappings.size(); ++i) 2421 favicon_ids.push_back(icon_mappings[i].icon_id); 2422 2423 // Populate |favicon_bitmap_results| and |icon_url_sizes|. 2424 bool success = GetFaviconBitmapResultsForBestMatch(favicon_ids, 2425 desired_size_in_dip, desired_scale_factors, favicon_bitmap_results); 2426 UMA_HISTOGRAM_TIMES("History.GetFavIconFromDB", // historical name 2427 TimeTicks::Now() - beginning_time); 2428 return success && !favicon_bitmap_results->empty(); 2429} 2430 2431bool HistoryBackend::GetFaviconBitmapResultsForBestMatch( 2432 const std::vector<FaviconID>& candidate_favicon_ids, 2433 int desired_size_in_dip, 2434 const std::vector<ui::ScaleFactor>& desired_scale_factors, 2435 std::vector<FaviconBitmapResult>* favicon_bitmap_results) { 2436 favicon_bitmap_results->clear(); 2437 2438 if (candidate_favicon_ids.empty()) 2439 return true; 2440 2441 // Find the FaviconID and the FaviconBitmapIDs which best match 2442 // |desired_size_in_dip| and |desired_scale_factors|. 2443 // TODO(pkotwicz): Select bitmap results from multiple favicons once 2444 // content::FaviconStatus supports multiple icon URLs. 2445 FaviconID best_favicon_id = 0; 2446 std::vector<FaviconBitmapID> best_bitmap_ids; 2447 float highest_score = kSelectFaviconFramesInvalidScore; 2448 for (size_t i = 0; i < candidate_favicon_ids.size(); ++i) { 2449 std::vector<FaviconBitmapIDSize> bitmap_id_sizes; 2450 thumbnail_db_->GetFaviconBitmapIDSizes(candidate_favicon_ids[i], 2451 &bitmap_id_sizes); 2452 2453 // Build vector of gfx::Size from |bitmap_id_sizes|. 2454 std::vector<gfx::Size> sizes; 2455 for (size_t j = 0; j < bitmap_id_sizes.size(); ++j) 2456 sizes.push_back(bitmap_id_sizes[j].pixel_size); 2457 2458 std::vector<size_t> candidate_bitmap_indices; 2459 float score = 0; 2460 SelectFaviconFrameIndices(sizes, 2461 desired_scale_factors, 2462 desired_size_in_dip, 2463 &candidate_bitmap_indices, 2464 &score); 2465 if (score > highest_score) { 2466 highest_score = score; 2467 best_favicon_id = candidate_favicon_ids[i], 2468 best_bitmap_ids.clear(); 2469 for (size_t j = 0; j < candidate_bitmap_indices.size(); ++j) { 2470 size_t candidate_index = candidate_bitmap_indices[j]; 2471 best_bitmap_ids.push_back( 2472 bitmap_id_sizes[candidate_index].bitmap_id); 2473 } 2474 } 2475 } 2476 2477 // Construct FaviconBitmapResults from |best_favicon_id| and 2478 // |best_bitmap_ids|. 2479 GURL icon_url; 2480 IconType icon_type; 2481 if (!thumbnail_db_->GetFaviconHeader(best_favicon_id, &icon_url, 2482 &icon_type, NULL)) { 2483 return false; 2484 } 2485 2486 for (size_t i = 0; i < best_bitmap_ids.size(); ++i) { 2487 base::Time last_updated; 2488 FaviconBitmapResult bitmap_result; 2489 bitmap_result.icon_url = icon_url; 2490 bitmap_result.icon_type = icon_type; 2491 if (!thumbnail_db_->GetFaviconBitmap(best_bitmap_ids[i], 2492 &last_updated, 2493 &bitmap_result.bitmap_data, 2494 &bitmap_result.pixel_size)) { 2495 return false; 2496 } 2497 2498 bitmap_result.expired = (Time::Now() - last_updated) > 2499 TimeDelta::FromDays(kFaviconRefetchDays); 2500 if (bitmap_result.is_valid()) 2501 favicon_bitmap_results->push_back(bitmap_result); 2502 } 2503 return true; 2504} 2505 2506bool HistoryBackend::SetFaviconMappingsForPageAndRedirects( 2507 const GURL& page_url, 2508 IconType icon_type, 2509 const std::vector<FaviconID>& icon_ids) { 2510 if (!thumbnail_db_) 2511 return false; 2512 2513 // Find all the pages whose favicons we should set, we want to set it for 2514 // all the pages in the redirect chain if it redirected. 2515 history::RedirectList redirects; 2516 GetCachedRecentRedirects(page_url, &redirects); 2517 2518 bool mappings_changed = false; 2519 2520 // Save page <-> favicon associations. 2521 for (history::RedirectList::const_iterator i(redirects.begin()); 2522 i != redirects.end(); ++i) { 2523 mappings_changed |= SetFaviconMappingsForPage(*i, icon_type, icon_ids); 2524 } 2525 return mappings_changed; 2526} 2527 2528bool HistoryBackend::SetFaviconMappingsForPage( 2529 const GURL& page_url, 2530 IconType icon_type, 2531 const std::vector<FaviconID>& icon_ids) { 2532 DCHECK_LE(icon_ids.size(), kMaxFaviconsPerPage); 2533 bool mappings_changed = false; 2534 2535 // Two icon types are considered 'equivalent' if one of the icon types is 2536 // TOUCH_ICON and the other is TOUCH_PRECOMPOSED_ICON. 2537 // 2538 // Sets the icon mappings from |page_url| for |icon_type| to the favicons 2539 // with |icon_ids|. Mappings for |page_url| to favicons of type |icon_type| 2540 // whose FaviconID is not in |icon_ids| are removed. All icon mappings for 2541 // |page_url| to favicons of a type equivalent to |icon_type| are removed. 2542 // Remove any favicons which are orphaned as a result of the removal of the 2543 // icon mappings. 2544 2545 std::vector<FaviconID> unmapped_icon_ids = icon_ids; 2546 2547 std::vector<IconMapping> icon_mappings; 2548 thumbnail_db_->GetIconMappingsForPageURL(page_url, &icon_mappings); 2549 2550 for (std::vector<IconMapping>::iterator m = icon_mappings.begin(); 2551 m != icon_mappings.end(); ++m) { 2552 std::vector<FaviconID>::iterator icon_id_it = std::find( 2553 unmapped_icon_ids.begin(), unmapped_icon_ids.end(), m->icon_id); 2554 2555 // If the icon mapping already exists, avoid removing it and adding it back. 2556 if (icon_id_it != unmapped_icon_ids.end()) { 2557 unmapped_icon_ids.erase(icon_id_it); 2558 continue; 2559 } 2560 2561 if ((icon_type == TOUCH_ICON && m->icon_type == TOUCH_PRECOMPOSED_ICON) || 2562 (icon_type == TOUCH_PRECOMPOSED_ICON && m->icon_type == TOUCH_ICON) || 2563 (icon_type == m->icon_type)) { 2564 thumbnail_db_->DeleteIconMapping(m->mapping_id); 2565 2566 // Removing the icon mapping may have orphaned the associated favicon so 2567 // we must recheck it. This is not super fast, but this case will get 2568 // triggered rarely, since normally a page will always map to the same 2569 // favicon IDs. It will mostly happen for favicons we import. 2570 if (!thumbnail_db_->HasMappingFor(m->icon_id)) 2571 thumbnail_db_->DeleteFavicon(m->icon_id); 2572 mappings_changed = true; 2573 } 2574 } 2575 2576 for (size_t i = 0; i < unmapped_icon_ids.size(); ++i) { 2577 thumbnail_db_->AddIconMapping(page_url, unmapped_icon_ids[i]); 2578 mappings_changed = true; 2579 } 2580 return mappings_changed; 2581} 2582 2583void HistoryBackend::GetCachedRecentRedirects( 2584 const GURL& page_url, 2585 history::RedirectList* redirect_list) { 2586 RedirectCache::iterator iter = recent_redirects_.Get(page_url); 2587 if (iter != recent_redirects_.end()) { 2588 *redirect_list = iter->second; 2589 2590 // The redirect chain should have the destination URL as the last item. 2591 DCHECK(!redirect_list->empty()); 2592 DCHECK(redirect_list->back() == page_url); 2593 } else { 2594 // No known redirects, construct mock redirect chain containing |page_url|. 2595 redirect_list->push_back(page_url); 2596 } 2597} 2598 2599void HistoryBackend::SendFaviconChangedNotificationForPageAndRedirects( 2600 const GURL& page_url) { 2601 history::RedirectList redirect_list; 2602 GetCachedRecentRedirects(page_url, &redirect_list); 2603 2604 FaviconChangeDetails* changed_details = new FaviconChangeDetails; 2605 for (size_t i = 0; i < redirect_list.size(); ++i) 2606 changed_details->urls.insert(redirect_list[i]); 2607 2608 BroadcastNotifications(chrome::NOTIFICATION_FAVICON_CHANGED, 2609 changed_details); 2610} 2611 2612void HistoryBackend::Commit() { 2613 if (!db_) 2614 return; 2615 2616 // Note that a commit may not actually have been scheduled if a caller 2617 // explicitly calls this instead of using ScheduleCommit. Likewise, we 2618 // may reset the flag written by a pending commit. But this is OK! It 2619 // will merely cause extra commits (which is kind of the idea). We 2620 // could optimize more for this case (we may get two extra commits in 2621 // some cases) but it hasn't been important yet. 2622 CancelScheduledCommit(); 2623 2624 db_->CommitTransaction(); 2625 DCHECK(db_->transaction_nesting() == 0) << "Somebody left a transaction open"; 2626 db_->BeginTransaction(); 2627 2628 if (thumbnail_db_) { 2629 thumbnail_db_->CommitTransaction(); 2630 DCHECK(thumbnail_db_->transaction_nesting() == 0) << 2631 "Somebody left a transaction open"; 2632 thumbnail_db_->BeginTransaction(); 2633 } 2634 2635 if (archived_db_) { 2636 archived_db_->CommitTransaction(); 2637 archived_db_->BeginTransaction(); 2638 } 2639 2640 if (text_database_) { 2641 text_database_->CommitTransaction(); 2642 text_database_->BeginTransaction(); 2643 } 2644} 2645 2646void HistoryBackend::ScheduleCommit() { 2647 if (scheduled_commit_) 2648 return; 2649 scheduled_commit_ = new CommitLaterTask(this); 2650 MessageLoop::current()->PostDelayedTask( 2651 FROM_HERE, 2652 base::Bind(&CommitLaterTask::RunCommit, scheduled_commit_.get()), 2653 base::TimeDelta::FromSeconds(kCommitIntervalSeconds)); 2654} 2655 2656void HistoryBackend::CancelScheduledCommit() { 2657 if (scheduled_commit_) { 2658 scheduled_commit_->Cancel(); 2659 scheduled_commit_ = NULL; 2660 } 2661} 2662 2663void HistoryBackend::ProcessDBTaskImpl() { 2664 if (!db_) { 2665 // db went away, release all the refs. 2666 ReleaseDBTasks(); 2667 return; 2668 } 2669 2670 // Remove any canceled tasks. 2671 while (!db_task_requests_.empty() && db_task_requests_.front()->canceled()) { 2672 db_task_requests_.front()->Release(); 2673 db_task_requests_.pop_front(); 2674 } 2675 if (db_task_requests_.empty()) 2676 return; 2677 2678 // Run the first task. 2679 HistoryDBTaskRequest* request = db_task_requests_.front(); 2680 db_task_requests_.pop_front(); 2681 if (request->value->RunOnDBThread(this, db_.get())) { 2682 // The task is done. Notify the callback. 2683 request->ForwardResult(); 2684 // We AddRef'd the request before adding, need to release it now. 2685 request->Release(); 2686 } else { 2687 // Tasks wants to run some more. Schedule it at the end of current tasks. 2688 db_task_requests_.push_back(request); 2689 // And process it after an invoke later. 2690 MessageLoop::current()->PostTask( 2691 FROM_HERE, base::Bind(&HistoryBackend::ProcessDBTaskImpl, this)); 2692 } 2693} 2694 2695void HistoryBackend::ReleaseDBTasks() { 2696 for (std::list<HistoryDBTaskRequest*>::iterator i = 2697 db_task_requests_.begin(); i != db_task_requests_.end(); ++i) { 2698 (*i)->Release(); 2699 } 2700 db_task_requests_.clear(); 2701} 2702 2703//////////////////////////////////////////////////////////////////////////////// 2704// 2705// Generic operations 2706// 2707//////////////////////////////////////////////////////////////////////////////// 2708 2709void HistoryBackend::DeleteURLs(const std::vector<GURL>& urls) { 2710 expirer_.DeleteURLs(urls); 2711 2712 db_->GetStartDate(&first_recorded_time_); 2713 // Force a commit, if the user is deleting something for privacy reasons, we 2714 // want to get it on disk ASAP. 2715 Commit(); 2716} 2717 2718void HistoryBackend::DeleteURL(const GURL& url) { 2719 expirer_.DeleteURL(url); 2720 2721 db_->GetStartDate(&first_recorded_time_); 2722 // Force a commit, if the user is deleting something for privacy reasons, we 2723 // want to get it on disk ASAP. 2724 Commit(); 2725} 2726 2727void HistoryBackend::ExpireHistoryBetween( 2728 const std::set<GURL>& restrict_urls, 2729 Time begin_time, 2730 Time end_time) { 2731 if (db_) { 2732 if (begin_time.is_null() && (end_time.is_null() || end_time.is_max()) && 2733 restrict_urls.empty()) { 2734 // Special case deleting all history so it can be faster and to reduce the 2735 // possibility of an information leak. 2736 DeleteAllHistory(); 2737 } else { 2738 // Clearing parts of history, have the expirer do the depend 2739 expirer_.ExpireHistoryBetween(restrict_urls, begin_time, end_time); 2740 2741 // Force a commit, if the user is deleting something for privacy reasons, 2742 // we want to get it on disk ASAP. 2743 Commit(); 2744 } 2745 } 2746 2747 if (begin_time <= first_recorded_time_) 2748 db_->GetStartDate(&first_recorded_time_); 2749} 2750 2751void HistoryBackend::ExpireHistoryForTimes( 2752 const std::set<base::Time>& times, 2753 base::Time begin_time, base::Time end_time) { 2754 if (times.empty() || !db_) 2755 return; 2756 2757 DCHECK(*times.begin() >= begin_time) 2758 << "Min time is before begin time: " 2759 << times.begin()->ToJsTime() << " v.s. " << begin_time.ToJsTime(); 2760 DCHECK(*times.rbegin() < end_time) 2761 << "Max time is after end time: " 2762 << times.rbegin()->ToJsTime() << " v.s. " << end_time.ToJsTime(); 2763 2764 history::QueryOptions options; 2765 options.begin_time = begin_time; 2766 options.end_time = end_time; 2767 options.duplicate_policy = QueryOptions::KEEP_ALL_DUPLICATES; 2768 QueryResults results; 2769 QueryHistoryBasic(db_.get(), db_.get(), options, &results); 2770 2771 // 1st pass: find URLs that are visited at one of |times|. 2772 std::set<GURL> urls; 2773 for (size_t i = 0; i < results.size(); ++i) { 2774 if (times.count(results[i].visit_time()) > 0) 2775 urls.insert(results[i].url()); 2776 } 2777 if (urls.empty()) 2778 return; 2779 2780 // 2nd pass: collect all visit times of those URLs. 2781 std::vector<base::Time> times_to_expire; 2782 for (size_t i = 0; i < results.size(); ++i) { 2783 if (urls.count(results[i].url())) 2784 times_to_expire.push_back(results[i].visit_time()); 2785 } 2786 2787 // Put the times in reverse chronological order and remove 2788 // duplicates (for expirer_.ExpireHistoryForTimes()). 2789 std::sort(times_to_expire.begin(), times_to_expire.end(), 2790 std::greater<base::Time>()); 2791 times_to_expire.erase( 2792 std::unique(times_to_expire.begin(), times_to_expire.end()), 2793 times_to_expire.end()); 2794 2795 // Expires by times and commit. 2796 DCHECK(!times_to_expire.empty()); 2797 expirer_.ExpireHistoryForTimes(times_to_expire); 2798 Commit(); 2799 2800 DCHECK(times_to_expire.back() >= first_recorded_time_); 2801 // Update |first_recorded_time_| if we expired it. 2802 if (times_to_expire.back() == first_recorded_time_) 2803 db_->GetStartDate(&first_recorded_time_); 2804} 2805 2806void HistoryBackend::ExpireHistory( 2807 const std::vector<history::ExpireHistoryArgs>& expire_list) { 2808 if (db_) { 2809 bool update_first_recorded_time = false; 2810 2811 for (std::vector<history::ExpireHistoryArgs>::const_iterator it = 2812 expire_list.begin(); it != expire_list.end(); ++it) { 2813 expirer_.ExpireHistoryBetween(it->urls, it->begin_time, it->end_time); 2814 2815 if (it->begin_time < first_recorded_time_) 2816 update_first_recorded_time = true; 2817 } 2818 Commit(); 2819 2820 // Update |first_recorded_time_| if any deletion might have affected it. 2821 if (update_first_recorded_time) 2822 db_->GetStartDate(&first_recorded_time_); 2823 } 2824} 2825 2826void HistoryBackend::URLsNoLongerBookmarked(const std::set<GURL>& urls) { 2827 if (!db_) 2828 return; 2829 2830 for (std::set<GURL>::const_iterator i = urls.begin(); i != urls.end(); ++i) { 2831 URLRow url_row; 2832 if (!db_->GetRowForURL(*i, &url_row)) 2833 continue; // The URL isn't in the db; nothing to do. 2834 2835 VisitVector visits; 2836 db_->GetVisitsForURL(url_row.id(), &visits); 2837 2838 if (visits.empty()) 2839 expirer_.DeleteURL(*i); // There are no more visits; nuke the URL. 2840 } 2841} 2842 2843void HistoryBackend::KillHistoryDatabase() { 2844 if (!db_) 2845 return; 2846 2847 // Rollback transaction because Raze() cannot be called from within a 2848 // transaction. 2849 db_->RollbackTransaction(); 2850 bool success = db_->Raze(); 2851 UMA_HISTOGRAM_BOOLEAN("History.KillHistoryDatabaseResult", success); 2852 2853#if defined(OS_ANDROID) 2854 // Release AndroidProviderBackend before other objects. 2855 android_provider_backend_.reset(); 2856#endif 2857 2858 // The expirer keeps tabs on the active databases. Tell it about the 2859 // databases which will be closed. 2860 expirer_.SetDatabases(NULL, NULL, NULL, NULL); 2861 2862 // Reopen a new transaction for |db_| for the sake of CloseAllDatabases(). 2863 db_->BeginTransaction(); 2864 CloseAllDatabases(); 2865} 2866 2867void HistoryBackend::ProcessDBTask( 2868 scoped_refptr<HistoryDBTaskRequest> request) { 2869 DCHECK(request); 2870 if (request->canceled()) 2871 return; 2872 2873 bool task_scheduled = !db_task_requests_.empty(); 2874 // Make sure we up the refcount of the request. ProcessDBTaskImpl will 2875 // release when done with the task. 2876 request->AddRef(); 2877 db_task_requests_.push_back(request.get()); 2878 if (!task_scheduled) { 2879 // No other tasks are scheduled. Process request now. 2880 ProcessDBTaskImpl(); 2881 } 2882} 2883 2884void HistoryBackend::BroadcastNotifications( 2885 int type, 2886 HistoryDetails* details_deleted) { 2887 // |delegate_| may be NULL if |this| is in the process of closing (closed by 2888 // HistoryService -> HistoryBackend::Closing(). 2889 if (delegate_) 2890 delegate_->BroadcastNotifications(type, details_deleted); 2891 else 2892 delete details_deleted; 2893} 2894 2895void HistoryBackend::NotifySyncURLsDeleted(bool all_history, 2896 bool archived, 2897 URLRows* rows) { 2898 if (typed_url_syncable_service_.get()) 2899 typed_url_syncable_service_->OnUrlsDeleted(all_history, archived, rows); 2900} 2901 2902// Deleting -------------------------------------------------------------------- 2903 2904void HistoryBackend::DeleteAllHistory() { 2905 // Our approach to deleting all history is: 2906 // 1. Copy the bookmarks and their dependencies to new tables with temporary 2907 // names. 2908 // 2. Delete the original tables. Since tables can not share pages, we know 2909 // that any data we don't want to keep is now in an unused page. 2910 // 3. Renaming the temporary tables to match the original. 2911 // 4. Vacuuming the database to delete the unused pages. 2912 // 2913 // Since we are likely to have very few bookmarks and their dependencies 2914 // compared to all history, this is also much faster than just deleting from 2915 // the original tables directly. 2916 2917 // Get the bookmarked URLs. 2918 std::vector<BookmarkService::URLAndTitle> starred_urls; 2919 BookmarkService* bookmark_service = GetBookmarkService(); 2920 if (bookmark_service) 2921 bookmark_service_->GetBookmarks(&starred_urls); 2922 2923 URLRows kept_urls; 2924 for (size_t i = 0; i < starred_urls.size(); i++) { 2925 URLRow row; 2926 if (!db_->GetRowForURL(starred_urls[i].url, &row)) 2927 continue; 2928 2929 // Clear the last visit time so when we write these rows they are "clean." 2930 row.set_last_visit(Time()); 2931 row.set_visit_count(0); 2932 row.set_typed_count(0); 2933 kept_urls.push_back(row); 2934 } 2935 2936 // Clear thumbnail and favicon history. The favicons for the given URLs will 2937 // be kept. 2938 if (!ClearAllThumbnailHistory(&kept_urls)) { 2939 LOG(ERROR) << "Thumbnail history could not be cleared"; 2940 // We continue in this error case. If the user wants to delete their 2941 // history, we should delete as much as we can. 2942 } 2943 2944 // ClearAllMainHistory will change the IDs of the URLs in kept_urls. Therfore, 2945 // we clear the list afterwards to make sure nobody uses this invalid data. 2946 if (!ClearAllMainHistory(kept_urls)) 2947 LOG(ERROR) << "Main history could not be cleared"; 2948 kept_urls.clear(); 2949 2950 // Delete FTS files & archived history. 2951 if (text_database_) { 2952 // We assume that the text database has one transaction on them that we need 2953 // to close & restart (the long-running history transaction). 2954 text_database_->CommitTransaction(); 2955 text_database_->DeleteAll(); 2956 text_database_->BeginTransaction(); 2957 } 2958 2959 if (archived_db_) { 2960 // Close the database and delete the file. 2961 archived_db_.reset(); 2962 base::FilePath archived_file_name = GetArchivedFileName(); 2963 file_util::Delete(archived_file_name, false); 2964 2965 // Now re-initialize the database (which may fail). 2966 archived_db_.reset(new ArchivedDatabase()); 2967 if (!archived_db_->Init(archived_file_name)) { 2968 LOG(WARNING) << "Could not initialize the archived database."; 2969 archived_db_.reset(); 2970 } else { 2971 // Open our long-running transaction on this database. 2972 archived_db_->BeginTransaction(); 2973 } 2974 } 2975 2976 db_->GetStartDate(&first_recorded_time_); 2977 2978 // Send out the notfication that history is cleared. The in-memory datdabase 2979 // will pick this up and clear itself. 2980 URLsDeletedDetails* details = new URLsDeletedDetails; 2981 details->all_history = true; 2982 NotifySyncURLsDeleted(true, false, NULL); 2983 BroadcastNotifications(chrome::NOTIFICATION_HISTORY_URLS_DELETED, details); 2984} 2985 2986bool HistoryBackend::ClearAllThumbnailHistory(URLRows* kept_urls) { 2987 if (!thumbnail_db_) { 2988 // When we have no reference to the thumbnail database, maybe there was an 2989 // error opening it. In this case, we just try to blow it away to try to 2990 // fix the error if it exists. This may fail, in which case either the 2991 // file doesn't exist or there's no more we can do. 2992 file_util::Delete(GetThumbnailFileName(), false); 2993 return true; 2994 } 2995 2996 // Create duplicate icon_mapping, favicon, and favicon_bitmaps tables, this 2997 // is where the favicons we want to keep will be stored. 2998 if (!thumbnail_db_->InitTemporaryTables()) 2999 return false; 3000 3001 // This maps existing favicon IDs to the ones in the temporary table. 3002 typedef std::map<FaviconID, FaviconID> FaviconMap; 3003 FaviconMap copied_favicons; 3004 3005 // Copy all unique favicons to the temporary table, and update all the 3006 // URLs to have the new IDs. 3007 for (URLRows::iterator i = kept_urls->begin(); i != kept_urls->end(); ++i) { 3008 std::vector<IconMapping> icon_mappings; 3009 if (!thumbnail_db_->GetIconMappingsForPageURL(i->url(), &icon_mappings)) 3010 continue; 3011 3012 for (std::vector<IconMapping>::iterator m = icon_mappings.begin(); 3013 m != icon_mappings.end(); ++m) { 3014 FaviconID old_id = m->icon_id; 3015 FaviconID new_id; 3016 FaviconMap::const_iterator found = copied_favicons.find(old_id); 3017 if (found == copied_favicons.end()) { 3018 new_id = thumbnail_db_->CopyFaviconAndFaviconBitmapsToTemporaryTables( 3019 old_id); 3020 copied_favicons[old_id] = new_id; 3021 } else { 3022 // We already encountered a URL that used this favicon, use the ID we 3023 // previously got. 3024 new_id = found->second; 3025 } 3026 // Add Icon mapping, and we don't care wheteher it suceeded or not. 3027 thumbnail_db_->AddToTemporaryIconMappingTable(i->url(), new_id); 3028 } 3029 } 3030#if defined(OS_ANDROID) 3031 // TODO (michaelbai): Add the unit test once AndroidProviderBackend is 3032 // avaliable in HistoryBackend. 3033 db_->ClearAndroidURLRows(); 3034#endif 3035 3036 // Drop original favicon_bitmaps, favicons, and icon mapping tables and 3037 // replace them with the duplicate tables. Recreate the other tables. This 3038 // will make the database consistent again. 3039 thumbnail_db_->CommitTemporaryTables(); 3040 3041 thumbnail_db_->RecreateThumbnailTable(); 3042 3043 // Vacuum to remove all the pages associated with the dropped tables. There 3044 // must be no transaction open on the table when we do this. We assume that 3045 // our long-running transaction is open, so we complete it and start it again. 3046 DCHECK(thumbnail_db_->transaction_nesting() == 1); 3047 thumbnail_db_->CommitTransaction(); 3048 thumbnail_db_->Vacuum(); 3049 thumbnail_db_->BeginTransaction(); 3050 return true; 3051} 3052 3053bool HistoryBackend::ClearAllMainHistory(const URLRows& kept_urls) { 3054 // Create the duplicate URL table. We will copy the kept URLs into this. 3055 if (!db_->CreateTemporaryURLTable()) 3056 return false; 3057 3058 // Insert the URLs into the temporary table, we need to keep a map of changed 3059 // IDs since the ID will be different in the new table. 3060 typedef std::map<URLID, URLID> URLIDMap; 3061 URLIDMap old_to_new; // Maps original ID to new one. 3062 for (URLRows::const_iterator i = kept_urls.begin(); i != kept_urls.end(); 3063 ++i) { 3064 URLID new_id = db_->AddTemporaryURL(*i); 3065 old_to_new[i->id()] = new_id; 3066 } 3067 3068 // Replace the original URL table with the temporary one. 3069 if (!db_->CommitTemporaryURLTable()) 3070 return false; 3071 3072 // Delete the old tables and recreate them empty. 3073 db_->RecreateAllTablesButURL(); 3074 3075 // Vacuum to reclaim the space from the dropped tables. This must be done 3076 // when there is no transaction open, and we assume that our long-running 3077 // transaction is currently open. 3078 db_->CommitTransaction(); 3079 db_->Vacuum(); 3080 db_->BeginTransaction(); 3081 db_->GetStartDate(&first_recorded_time_); 3082 3083 return true; 3084} 3085 3086BookmarkService* HistoryBackend::GetBookmarkService() { 3087 if (bookmark_service_) 3088 bookmark_service_->BlockTillLoaded(); 3089 return bookmark_service_; 3090} 3091 3092void HistoryBackend::NotifyVisitObservers(const VisitRow& visit) { 3093 BriefVisitInfo info; 3094 info.url_id = visit.url_id; 3095 info.time = visit.visit_time; 3096 info.transition = visit.transition; 3097 // If we don't have a delegate yet during setup or shutdown, we will drop 3098 // these notifications. 3099 if (delegate_) 3100 delegate_->NotifyVisitDBObserversOnAddVisit(info); 3101} 3102 3103} // namespace history 3104