• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1  // Copyright (c) 2010 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/top_sites_backend.h"
6  
7  #include "base/file_path.h"
8  #include "base/file_util.h"
9  #include "chrome/browser/history/top_sites_database.h"
10  #include "content/browser/browser_thread.h"
11  
12  namespace history {
13  
TopSitesBackend()14  TopSitesBackend::TopSitesBackend()
15      : db_(new TopSitesDatabase()) {
16  }
17  
Init(const FilePath & path)18  void TopSitesBackend::Init(const FilePath& path) {
19    db_path_ = path;
20    BrowserThread::PostTask(
21        BrowserThread::DB, FROM_HERE, NewRunnableMethod(
22            this, &TopSitesBackend::InitDBOnDBThread, path));
23  }
24  
Shutdown()25  void TopSitesBackend::Shutdown() {
26    BrowserThread::PostTask(
27        BrowserThread::DB, FROM_HERE, NewRunnableMethod(
28            this, &TopSitesBackend::ShutdownDBOnDBThread));
29  }
30  
GetMostVisitedThumbnails(CancelableRequestConsumerBase * consumer,GetMostVisitedThumbnailsCallback * callback)31  TopSitesBackend::Handle TopSitesBackend::GetMostVisitedThumbnails(
32      CancelableRequestConsumerBase* consumer,
33      GetMostVisitedThumbnailsCallback* callback) {
34    GetMostVisitedThumbnailsRequest* request =
35        new GetMostVisitedThumbnailsRequest(callback);
36    request->value = new MostVisitedThumbnails;
37    AddRequest(request, consumer);
38    BrowserThread::PostTask(
39        BrowserThread::DB, FROM_HERE, NewRunnableMethod(
40            this,
41            &TopSitesBackend::GetMostVisitedThumbnailsOnDBThread,
42            scoped_refptr<GetMostVisitedThumbnailsRequest>(request)));
43    return request->handle();
44  }
45  
UpdateTopSites(const TopSitesDelta & delta)46  void TopSitesBackend::UpdateTopSites(const TopSitesDelta& delta) {
47    BrowserThread::PostTask(
48        BrowserThread::DB, FROM_HERE, NewRunnableMethod(
49            this, &TopSitesBackend::UpdateTopSitesOnDBThread, delta));
50  }
51  
SetPageThumbnail(const MostVisitedURL & url,int url_rank,const Images & thumbnail)52  void TopSitesBackend::SetPageThumbnail(const MostVisitedURL& url,
53                                         int url_rank,
54                                         const Images& thumbnail) {
55    BrowserThread::PostTask(
56        BrowserThread::DB, FROM_HERE, NewRunnableMethod(
57            this, &TopSitesBackend::SetPageThumbnailOnDBThread, url,
58            url_rank, thumbnail));
59  }
60  
ResetDatabase()61  void TopSitesBackend::ResetDatabase() {
62    BrowserThread::PostTask(
63        BrowserThread::DB, FROM_HERE, NewRunnableMethod(
64            this, &TopSitesBackend::ResetDatabaseOnDBThread, db_path_));
65  }
66  
DoEmptyRequest(CancelableRequestConsumerBase * consumer,EmptyRequestCallback * callback)67  TopSitesBackend::Handle TopSitesBackend::DoEmptyRequest(
68      CancelableRequestConsumerBase* consumer,
69      EmptyRequestCallback* callback) {
70    EmptyRequestRequest* request = new EmptyRequestRequest(callback);
71    AddRequest(request, consumer);
72    BrowserThread::PostTask(
73        BrowserThread::DB, FROM_HERE, NewRunnableMethod(
74            this,
75            &TopSitesBackend::DoEmptyRequestOnDBThread,
76            scoped_refptr<EmptyRequestRequest>(request)));
77    return request->handle();
78  }
79  
~TopSitesBackend()80  TopSitesBackend::~TopSitesBackend() {
81    DCHECK(!db_.get());  // Shutdown should have happened first (which results in
82                         // nulling out db).
83  }
84  
InitDBOnDBThread(const FilePath & path)85  void TopSitesBackend::InitDBOnDBThread(const FilePath& path) {
86    DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
87    if (!db_->Init(path)) {
88      NOTREACHED() << "Failed to initialize database.";
89      db_.reset();
90    }
91  }
92  
ShutdownDBOnDBThread()93  void TopSitesBackend::ShutdownDBOnDBThread() {
94    DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
95    db_.reset();
96  }
97  
GetMostVisitedThumbnailsOnDBThread(scoped_refptr<GetMostVisitedThumbnailsRequest> request)98  void TopSitesBackend::GetMostVisitedThumbnailsOnDBThread(
99      scoped_refptr<GetMostVisitedThumbnailsRequest> request) {
100    DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
101  
102    if (request->canceled())
103      return;
104  
105    bool may_need_history_migration = false;
106    if (db_.get()) {
107      db_->GetPageThumbnails(&(request->value->most_visited),
108                             &(request->value->url_to_images_map));
109      may_need_history_migration = db_->may_need_history_migration();
110    }
111    request->ForwardResult(GetMostVisitedThumbnailsRequest::TupleType(
112                               request->handle(),
113                               request->value,
114                               may_need_history_migration));
115  }
116  
UpdateTopSitesOnDBThread(const TopSitesDelta & delta)117  void TopSitesBackend::UpdateTopSitesOnDBThread(const TopSitesDelta& delta) {
118    if (!db_.get())
119      return;
120  
121    for (size_t i = 0; i < delta.deleted.size(); ++i)
122      db_->RemoveURL(delta.deleted[i]);
123  
124    for (size_t i = 0; i < delta.added.size(); ++i)
125      db_->SetPageThumbnail(delta.added[i].url, delta.added[i].rank, Images());
126  
127    for (size_t i = 0; i < delta.moved.size(); ++i)
128      db_->UpdatePageRank(delta.moved[i].url, delta.moved[i].rank);
129  }
130  
SetPageThumbnailOnDBThread(const MostVisitedURL & url,int url_rank,const Images & thumbnail)131  void TopSitesBackend::SetPageThumbnailOnDBThread(const MostVisitedURL& url,
132                                                   int url_rank,
133                                                   const Images& thumbnail) {
134    if (!db_.get())
135      return;
136  
137    db_->SetPageThumbnail(url, url_rank, thumbnail);
138  }
139  
ResetDatabaseOnDBThread(const FilePath & file_path)140  void TopSitesBackend::ResetDatabaseOnDBThread(const FilePath& file_path) {
141    DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
142    db_.reset(NULL);
143    file_util::Delete(db_path_, false);
144    db_.reset(new TopSitesDatabase());
145    InitDBOnDBThread(db_path_);
146  }
147  
DoEmptyRequestOnDBThread(scoped_refptr<EmptyRequestRequest> request)148  void TopSitesBackend::DoEmptyRequestOnDBThread(
149      scoped_refptr<EmptyRequestRequest> request) {
150    request->ForwardResult(EmptyRequestRequest::TupleType(request->handle()));
151  }
152  
153  }  // namespace history
154