OLD | NEW |
(Empty) | |
| 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/prerender/prerender_local_predictor.h" |
| 6 |
| 7 #include "base/timer.h" |
| 8 #include "chrome/browser/prerender/prerender_manager.h" |
| 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/history/history.h" |
| 11 #include "chrome/browser/history/history_database.h" |
| 12 #include "content/public/browser/browser_thread.h" |
| 13 |
| 14 using content::BrowserThread; |
| 15 |
| 16 namespace prerender { |
| 17 |
| 18 namespace { |
| 19 |
| 20 // Task to lookup the URL for a given URLID. |
| 21 class GetURLForURLIDTask : public HistoryDBTask { |
| 22 public: |
| 23 GetURLForURLIDTask(PrerenderLocalPredictor* local_predictor, |
| 24 history::URLID url_id) |
| 25 : local_predictor_(local_predictor), |
| 26 url_id_(url_id), |
| 27 success_(false) { |
| 28 } |
| 29 virtual bool RunOnDBThread(history::HistoryBackend* backend, |
| 30 history::HistoryDatabase* db) { |
| 31 history::URLRow url_row; |
| 32 success_ = db->GetURLRow(url_id_, &url_row); |
| 33 if (success_) |
| 34 url_ = url_row.url(); |
| 35 return true; |
| 36 } |
| 37 virtual void DoneRunOnMainThread() { |
| 38 if (success_) |
| 39 local_predictor_->OnLookupURL(url_id_, url_); |
| 40 } |
| 41 private: |
| 42 PrerenderLocalPredictor* local_predictor_; |
| 43 history::URLID url_id_; |
| 44 bool success_; |
| 45 GURL url_; |
| 46 DISALLOW_COPY_AND_ASSIGN(GetURLForURLIDTask); |
| 47 }; |
| 48 |
| 49 // Task to load history from the visit database on startup. |
| 50 class GetVisitHistoryTask : public HistoryDBTask { |
| 51 public: |
| 52 GetVisitHistoryTask(PrerenderLocalPredictor* local_predictor, |
| 53 int max_visits) |
| 54 : local_predictor_(local_predictor), |
| 55 max_visits_(max_visits), |
| 56 visit_history_(new std::vector<history::BriefVisitInfo>) { |
| 57 } |
| 58 virtual bool RunOnDBThread(history::HistoryBackend* backend, |
| 59 history::HistoryDatabase* db) { |
| 60 db->GetBriefVisitInfoOfMostRecentVisits(max_visits_, visit_history_.get()); |
| 61 return true; |
| 62 } |
| 63 virtual void DoneRunOnMainThread() { |
| 64 local_predictor_->OnGetInitialVisitHistory(visit_history_.Pass()); |
| 65 } |
| 66 private: |
| 67 PrerenderLocalPredictor* local_predictor_; |
| 68 int max_visits_; |
| 69 scoped_ptr<std::vector<history::BriefVisitInfo> > visit_history_; |
| 70 DISALLOW_COPY_AND_ASSIGN(GetVisitHistoryTask); |
| 71 }; |
| 72 |
| 73 // Maximum visit history to retrieve from the visit database. |
| 74 const int kMaxVisitHistory = 100 * 1000; |
| 75 |
| 76 } // namespace |
| 77 |
| 78 PrerenderLocalPredictor::PrerenderLocalPredictor( |
| 79 PrerenderManager* prerender_manager) |
| 80 : prerender_manager_(prerender_manager), |
| 81 visit_history_initialized_(false) { |
| 82 if (MessageLoop::current()) { |
| 83 timer_.Start(FROM_HERE, |
| 84 base::TimeDelta::FromMilliseconds(kInitDelayMs), |
| 85 this, |
| 86 &PrerenderLocalPredictor::Init); |
| 87 } |
| 88 } |
| 89 |
| 90 PrerenderLocalPredictor::~PrerenderLocalPredictor() { |
| 91 HistoryService* history = GetHistoryIfExists(); |
| 92 if (history) |
| 93 history->RemoveVisitDatabaseObserver(this); |
| 94 } |
| 95 |
| 96 void PrerenderLocalPredictor::OnAddVisit(const history::BriefVisitInfo& info) { |
| 97 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 98 HistoryService* history = GetHistoryIfExists(); |
| 99 if (history) { |
| 100 history->ScheduleDBTask( |
| 101 new GetURLForURLIDTask(this, info.url_id), |
| 102 &history_db_consumer_); |
| 103 } |
| 104 } |
| 105 |
| 106 void PrerenderLocalPredictor::OnLookupURL(history::URLID url_id, |
| 107 const GURL& url) { |
| 108 } |
| 109 |
| 110 void PrerenderLocalPredictor::OnGetInitialVisitHistory( |
| 111 scoped_ptr<std::vector<history::BriefVisitInfo> > visit_history) { |
| 112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 113 DCHECK(!visit_history_initialized_); |
| 114 visit_history_.reset(visit_history.release()); |
| 115 visit_history_initialized_ = true; |
| 116 } |
| 117 |
| 118 HistoryService* PrerenderLocalPredictor::GetHistoryIfExists() const { |
| 119 Profile* profile = prerender_manager_->profile(); |
| 120 if (!profile) |
| 121 return NULL; |
| 122 return profile->GetHistoryServiceWithoutCreating(); |
| 123 } |
| 124 |
| 125 void PrerenderLocalPredictor::Init() { |
| 126 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 127 HistoryService* history = GetHistoryIfExists(); |
| 128 if (!history) { |
| 129 // TODO(tburkard): Record this somewhere (eg histogram) and/or try again |
| 130 // later. |
| 131 return; |
| 132 } |
| 133 history->ScheduleDBTask( |
| 134 new GetVisitHistoryTask(this, kMaxVisitHistory), |
| 135 &history_db_consumer_); |
| 136 history->AddVisitDatabaseObserver(this); |
| 137 } |
| 138 |
| 139 } // namespace prerender |
OLD | NEW |