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_.release()); |
| 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 }; |
| 74 |
| 75 HistoryService* PrerenderLocalPredictor::GetHistoryIfExists() const { |
| 76 Profile* profile = prerender_manager_->profile(); |
| 77 if (!profile) |
| 78 return NULL; |
| 79 return profile->GetHistoryServiceWithoutCreating(); |
| 80 } |
| 81 |
| 82 PrerenderLocalPredictor::PrerenderLocalPredictor( |
| 83 PrerenderManager* prerender_manager) |
| 84 : prerender_manager_(prerender_manager), |
| 85 visit_history_initialized_(false) { |
| 86 if (MessageLoop::current()) { |
| 87 timer_.Start(FROM_HERE, |
| 88 base::TimeDelta::FromMilliseconds(kInitDelayMs), |
| 89 this, |
| 90 &PrerenderLocalPredictor::Init); |
| 91 } |
| 92 } |
| 93 |
| 94 void PrerenderLocalPredictor::OnGetInitialVisitHistory( |
| 95 std::vector<history::BriefVisitInfo>* visit_history) { |
| 96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 97 DCHECK(!visit_history_initialized_); |
| 98 visit_history_.reset(visit_history); |
| 99 visit_history_initialized_ = true; |
| 100 } |
| 101 |
| 102 void PrerenderLocalPredictor::Init() { |
| 103 HistoryService* history = GetHistoryIfExists(); |
| 104 if (!history) { |
| 105 // TODO(tburkard): Record this somewhere (eg histogram) and/or try again |
| 106 // later. |
| 107 return; |
| 108 } |
| 109 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 110 history->ScheduleDBTask( |
| 111 new GetVisitHistoryTask(this, kMaxVisitHistory), |
| 112 &history_db_consumer_); |
| 113 history->AddVisitDatabaseObserver(this); |
| 114 } |
| 115 |
| 116 void PrerenderLocalPredictor::OnAddVisit(history::BriefVisitInfo info) { |
| 117 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 118 HistoryService* history = GetHistoryIfExists(); |
| 119 if (history) { |
| 120 history->ScheduleDBTask( |
| 121 new GetURLForURLIDTask(this, info.url_id), |
| 122 &history_db_consumer_); |
| 123 } |
| 124 } |
| 125 |
| 126 PrerenderLocalPredictor::~PrerenderLocalPredictor() { |
| 127 HistoryService* history = GetHistoryIfExists(); |
| 128 if (history) |
| 129 history->RemoveVisitDatabaseObserver(this); |
| 130 } |
| 131 |
| 132 void PrerenderLocalPredictor::OnLookupURL(history::URLID url_id, GURL url) { |
| 133 |
| 134 } |
| 135 |
| 136 } // namespace prerender |
OLD | NEW |