1513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch// Copyright (c) 2010 The Chromium Authors. All rights reserved. 2513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch// Use of this source code is governed by a BSD-style license that can be 3513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch// found in the LICENSE file. 4513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch 5513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch#include "chrome/browser/history/top_sites_backend.h" 6513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch 7513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch#include "base/file_path.h" 8513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch#include "base/file_util.h" 9513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch#include "chrome/browser/history/top_sites_database.h" 10dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include "content/browser/browser_thread.h" 11513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch 12513209b27ff55e2841eac0e4120199c23acce758Ben Murdochnamespace history { 13513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch 14513209b27ff55e2841eac0e4120199c23acce758Ben MurdochTopSitesBackend::TopSitesBackend() 15513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch : db_(new TopSitesDatabase()) { 16513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch} 17513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch 18513209b27ff55e2841eac0e4120199c23acce758Ben Murdochvoid TopSitesBackend::Init(const FilePath& path) { 19513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch db_path_ = path; 20513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch BrowserThread::PostTask( 21513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch BrowserThread::DB, FROM_HERE, NewRunnableMethod( 22513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch this, &TopSitesBackend::InitDBOnDBThread, path)); 23513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch} 24513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch 25513209b27ff55e2841eac0e4120199c23acce758Ben Murdochvoid TopSitesBackend::Shutdown() { 26513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch BrowserThread::PostTask( 27513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch BrowserThread::DB, FROM_HERE, NewRunnableMethod( 28513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch this, &TopSitesBackend::ShutdownDBOnDBThread)); 29513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch} 30513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch 31513209b27ff55e2841eac0e4120199c23acce758Ben MurdochTopSitesBackend::Handle TopSitesBackend::GetMostVisitedThumbnails( 32513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch CancelableRequestConsumerBase* consumer, 33513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch GetMostVisitedThumbnailsCallback* callback) { 34513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch GetMostVisitedThumbnailsRequest* request = 35513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch new GetMostVisitedThumbnailsRequest(callback); 36513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch request->value = new MostVisitedThumbnails; 37513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch AddRequest(request, consumer); 38513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch BrowserThread::PostTask( 39513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch BrowserThread::DB, FROM_HERE, NewRunnableMethod( 40513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch this, 41513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch &TopSitesBackend::GetMostVisitedThumbnailsOnDBThread, 42513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch scoped_refptr<GetMostVisitedThumbnailsRequest>(request))); 43513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch return request->handle(); 44513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch} 45513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch 46513209b27ff55e2841eac0e4120199c23acce758Ben Murdochvoid TopSitesBackend::UpdateTopSites(const TopSitesDelta& delta) { 47513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch BrowserThread::PostTask( 48513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch BrowserThread::DB, FROM_HERE, NewRunnableMethod( 49513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch this, &TopSitesBackend::UpdateTopSitesOnDBThread, delta)); 50513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch} 51513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch 52513209b27ff55e2841eac0e4120199c23acce758Ben Murdochvoid TopSitesBackend::SetPageThumbnail(const MostVisitedURL& url, 53513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch int url_rank, 54513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch const Images& thumbnail) { 55513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch BrowserThread::PostTask( 56513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch BrowserThread::DB, FROM_HERE, NewRunnableMethod( 57513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch this, &TopSitesBackend::SetPageThumbnailOnDBThread, url, 58513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch url_rank, thumbnail)); 59513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch} 60513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch 61513209b27ff55e2841eac0e4120199c23acce758Ben Murdochvoid TopSitesBackend::ResetDatabase() { 62513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch BrowserThread::PostTask( 63513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch BrowserThread::DB, FROM_HERE, NewRunnableMethod( 64513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch this, &TopSitesBackend::ResetDatabaseOnDBThread, db_path_)); 65513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch} 66513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch 67513209b27ff55e2841eac0e4120199c23acce758Ben MurdochTopSitesBackend::Handle TopSitesBackend::DoEmptyRequest( 68513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch CancelableRequestConsumerBase* consumer, 69513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch EmptyRequestCallback* callback) { 70513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch EmptyRequestRequest* request = new EmptyRequestRequest(callback); 71513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch AddRequest(request, consumer); 72513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch BrowserThread::PostTask( 73513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch BrowserThread::DB, FROM_HERE, NewRunnableMethod( 74513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch this, 75513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch &TopSitesBackend::DoEmptyRequestOnDBThread, 76513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch scoped_refptr<EmptyRequestRequest>(request))); 77513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch return request->handle(); 78513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch} 79513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch 80513209b27ff55e2841eac0e4120199c23acce758Ben MurdochTopSitesBackend::~TopSitesBackend() { 81513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch DCHECK(!db_.get()); // Shutdown should have happened first (which results in 82513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch // nulling out db). 83513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch} 84513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch 85513209b27ff55e2841eac0e4120199c23acce758Ben Murdochvoid TopSitesBackend::InitDBOnDBThread(const FilePath& path) { 86513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); 87513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch if (!db_->Init(path)) { 88513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch NOTREACHED() << "Failed to initialize database."; 89513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch db_.reset(); 90513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch } 91513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch} 92513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch 93513209b27ff55e2841eac0e4120199c23acce758Ben Murdochvoid TopSitesBackend::ShutdownDBOnDBThread() { 94513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); 95513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch db_.reset(); 96513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch} 97513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch 98513209b27ff55e2841eac0e4120199c23acce758Ben Murdochvoid TopSitesBackend::GetMostVisitedThumbnailsOnDBThread( 99513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch scoped_refptr<GetMostVisitedThumbnailsRequest> request) { 100513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); 101513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch 102513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch if (request->canceled()) 103513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch return; 104513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch 105513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch bool may_need_history_migration = false; 106513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch if (db_.get()) { 107513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch db_->GetPageThumbnails(&(request->value->most_visited), 108513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch &(request->value->url_to_images_map)); 109513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch may_need_history_migration = db_->may_need_history_migration(); 110513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch } 111513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch request->ForwardResult(GetMostVisitedThumbnailsRequest::TupleType( 112513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch request->handle(), 113513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch request->value, 114513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch may_need_history_migration)); 115513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch} 116513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch 117513209b27ff55e2841eac0e4120199c23acce758Ben Murdochvoid TopSitesBackend::UpdateTopSitesOnDBThread(const TopSitesDelta& delta) { 118513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch if (!db_.get()) 119513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch return; 120513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch 121513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch for (size_t i = 0; i < delta.deleted.size(); ++i) 122513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch db_->RemoveURL(delta.deleted[i]); 123513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch 124513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch for (size_t i = 0; i < delta.added.size(); ++i) 125513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch db_->SetPageThumbnail(delta.added[i].url, delta.added[i].rank, Images()); 126513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch 127513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch for (size_t i = 0; i < delta.moved.size(); ++i) 128513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch db_->UpdatePageRank(delta.moved[i].url, delta.moved[i].rank); 129513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch} 130513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch 131513209b27ff55e2841eac0e4120199c23acce758Ben Murdochvoid TopSitesBackend::SetPageThumbnailOnDBThread(const MostVisitedURL& url, 132513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch int url_rank, 133513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch const Images& thumbnail) { 134513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch if (!db_.get()) 135513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch return; 136513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch 137513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch db_->SetPageThumbnail(url, url_rank, thumbnail); 138513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch} 139513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch 140513209b27ff55e2841eac0e4120199c23acce758Ben Murdochvoid TopSitesBackend::ResetDatabaseOnDBThread(const FilePath& file_path) { 141513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); 142513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch db_.reset(NULL); 143513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch file_util::Delete(db_path_, false); 144513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch db_.reset(new TopSitesDatabase()); 145513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch InitDBOnDBThread(db_path_); 146513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch} 147513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch 148513209b27ff55e2841eac0e4120199c23acce758Ben Murdochvoid TopSitesBackend::DoEmptyRequestOnDBThread( 149513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch scoped_refptr<EmptyRequestRequest> request) { 150513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch request->ForwardResult(EmptyRequestRequest::TupleType(request->handle())); 151513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch} 152513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch 153513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch} // namespace history 154