OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/bind_helpers.h" | 6 #include "base/bind_helpers.h" |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 class WaitForHistoryTask : public HistoryDBTask { | 52 class WaitForHistoryTask : public HistoryDBTask { |
53 public: | 53 public: |
54 WaitForHistoryTask() {} | 54 WaitForHistoryTask() {} |
55 | 55 |
56 virtual bool RunOnDBThread(HistoryBackend* backend, | 56 virtual bool RunOnDBThread(HistoryBackend* backend, |
57 HistoryDatabase* db) OVERRIDE { | 57 HistoryDatabase* db) OVERRIDE { |
58 return true; | 58 return true; |
59 } | 59 } |
60 | 60 |
61 virtual void DoneRunOnMainThread() OVERRIDE { | 61 virtual void DoneRunOnMainThread() OVERRIDE { |
62 MessageLoop::current()->Quit(); | 62 base::MessageLoop::current()->Quit(); |
63 } | 63 } |
64 | 64 |
65 private: | 65 private: |
66 virtual ~WaitForHistoryTask() {} | 66 virtual ~WaitForHistoryTask() {} |
67 | 67 |
68 DISALLOW_COPY_AND_ASSIGN(WaitForHistoryTask); | 68 DISALLOW_COPY_AND_ASSIGN(WaitForHistoryTask); |
69 }; | 69 }; |
70 | 70 |
71 // Used for querying top sites. Either runs sequentially, or runs a nested | 71 // Used for querying top sites. Either runs sequentially, or runs a nested |
72 // nested message loop until the response is complete. The later is used when | 72 // nested message loop until the response is complete. The later is used when |
73 // TopSites is queried before it finishes loading. | 73 // TopSites is queried before it finishes loading. |
74 class TopSitesQuerier { | 74 class TopSitesQuerier { |
75 public: | 75 public: |
76 TopSitesQuerier() | 76 TopSitesQuerier() |
77 : weak_ptr_factory_(this), | 77 : weak_ptr_factory_(this), |
78 number_of_callbacks_(0), | 78 number_of_callbacks_(0), |
79 waiting_(false) {} | 79 waiting_(false) {} |
80 | 80 |
81 // Queries top sites. If |wait| is true a nested message loop is run until the | 81 // Queries top sites. If |wait| is true a nested message loop is run until the |
82 // callback is notified. | 82 // callback is notified. |
83 void QueryTopSites(TopSitesLikelyImpl* top_sites, bool wait) { | 83 void QueryTopSites(TopSitesLikelyImpl* top_sites, bool wait) { |
84 int start_number_of_callbacks = number_of_callbacks_; | 84 int start_number_of_callbacks = number_of_callbacks_; |
85 top_sites->GetMostVisitedURLs( | 85 top_sites->GetMostVisitedURLs( |
86 base::Bind(&TopSitesQuerier::OnTopSitesAvailable, | 86 base::Bind(&TopSitesQuerier::OnTopSitesAvailable, |
87 weak_ptr_factory_.GetWeakPtr())); | 87 weak_ptr_factory_.GetWeakPtr())); |
88 if (wait && start_number_of_callbacks == number_of_callbacks_) { | 88 if (wait && start_number_of_callbacks == number_of_callbacks_) { |
89 waiting_ = true; | 89 waiting_ = true; |
90 MessageLoop::current()->Run(); | 90 base::MessageLoop::current()->Run(); |
91 } | 91 } |
92 } | 92 } |
93 | 93 |
94 void CancelRequest() { | 94 void CancelRequest() { |
95 weak_ptr_factory_.InvalidateWeakPtrs(); | 95 weak_ptr_factory_.InvalidateWeakPtrs(); |
96 } | 96 } |
97 | 97 |
98 void set_urls(const MostVisitedURLList& urls) { urls_ = urls; } | 98 void set_urls(const MostVisitedURLList& urls) { urls_ = urls; } |
99 const MostVisitedURLList& urls() const { return urls_; } | 99 const MostVisitedURLList& urls() const { return urls_; } |
100 | 100 |
101 int number_of_callbacks() const { return number_of_callbacks_; } | 101 int number_of_callbacks() const { return number_of_callbacks_; } |
102 | 102 |
103 private: | 103 private: |
104 // Callback for TopSitesLikelyImpl::GetMostVisitedURLs. | 104 // Callback for TopSitesLikelyImpl::GetMostVisitedURLs. |
105 void OnTopSitesAvailable(const history::MostVisitedURLList& data) { | 105 void OnTopSitesAvailable(const history::MostVisitedURLList& data) { |
106 urls_ = data; | 106 urls_ = data; |
107 number_of_callbacks_++; | 107 number_of_callbacks_++; |
108 if (waiting_) { | 108 if (waiting_) { |
109 MessageLoop::current()->Quit(); | 109 base::MessageLoop::current()->Quit(); |
110 waiting_ = false; | 110 waiting_ = false; |
111 } | 111 } |
112 } | 112 } |
113 | 113 |
114 base::WeakPtrFactory<TopSitesQuerier> weak_ptr_factory_; | 114 base::WeakPtrFactory<TopSitesQuerier> weak_ptr_factory_; |
115 MostVisitedURLList urls_; | 115 MostVisitedURLList urls_; |
116 int number_of_callbacks_; | 116 int number_of_callbacks_; |
117 bool waiting_; | 117 bool waiting_; |
118 | 118 |
119 DISALLOW_COPY_AND_ASSIGN(TopSitesQuerier); | 119 DISALLOW_COPY_AND_ASSIGN(TopSitesQuerier); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 void RefreshTopSitesAndRecreate() { | 186 void RefreshTopSitesAndRecreate() { |
187 StartQueryForMostVisited(); | 187 StartQueryForMostVisited(); |
188 WaitForHistory(); | 188 WaitForHistory(); |
189 RecreateTopSitesAndBlock(); | 189 RecreateTopSitesAndBlock(); |
190 } | 190 } |
191 | 191 |
192 // Blocks the caller until history processes a task. This is useful if you | 192 // Blocks the caller until history processes a task. This is useful if you |
193 // need to wait until you know history has processed a task. | 193 // need to wait until you know history has processed a task. |
194 void WaitForHistory() { | 194 void WaitForHistory() { |
195 history_service()->ScheduleDBTask(new WaitForHistoryTask(), &consumer_); | 195 history_service()->ScheduleDBTask(new WaitForHistoryTask(), &consumer_); |
196 MessageLoop::current()->Run(); | 196 base::MessageLoop::current()->Run(); |
197 } | 197 } |
198 | 198 |
199 // Waits for top sites to finish processing a task. This is useful if you need | 199 // Waits for top sites to finish processing a task. This is useful if you need |
200 // to wait until top sites finishes processing a task. | 200 // to wait until top sites finishes processing a task. |
201 void WaitForTopSites() { | 201 void WaitForTopSites() { |
202 top_sites()->backend_->DoEmptyRequest( | 202 top_sites()->backend_->DoEmptyRequest( |
203 base::Bind(&TopSitesLikelyImplTest::QuitCallback, | 203 base::Bind(&TopSitesLikelyImplTest::QuitCallback, |
204 base::Unretained(this)), | 204 base::Unretained(this)), |
205 &cancelable_task_tracker_); | 205 &cancelable_task_tracker_); |
206 MessageLoop::current()->Run(); | 206 base::MessageLoop::current()->Run(); |
207 } | 207 } |
208 | 208 |
209 TopSitesLikelyImpl* top_sites() { | 209 TopSitesLikelyImpl* top_sites() { |
210 return static_cast<TopSitesLikelyImpl*>(profile_->GetTopSites()); | 210 return static_cast<TopSitesLikelyImpl*>(profile_->GetTopSites()); |
211 } | 211 } |
212 CancelableRequestConsumer* consumer() { return &consumer_; } | 212 CancelableRequestConsumer* consumer() { return &consumer_; } |
213 TestingProfile* profile() {return profile_.get();} | 213 TestingProfile* profile() {return profile_.get();} |
214 HistoryService* history_service() { | 214 HistoryService* history_service() { |
215 return HistoryServiceFactory::GetForProfile(profile_.get(), | 215 return HistoryServiceFactory::GetForProfile(profile_.get(), |
216 Profile::EXPLICIT_ACCESS); | 216 Profile::EXPLICIT_ACCESS); |
(...skipping 16 matching lines...) Expand all Loading... |
233 } | 233 } |
234 } | 234 } |
235 | 235 |
236 // Used for callbacks from history. | 236 // Used for callbacks from history. |
237 void EmptyCallback() { | 237 void EmptyCallback() { |
238 } | 238 } |
239 | 239 |
240 // Quit the current message loop when invoked. Useful when running a nested | 240 // Quit the current message loop when invoked. Useful when running a nested |
241 // message loop. | 241 // message loop. |
242 void QuitCallback() { | 242 void QuitCallback() { |
243 MessageLoop::current()->Quit(); | 243 base::MessageLoop::current()->Quit(); |
244 } | 244 } |
245 | 245 |
246 // Adds a page to history. | 246 // Adds a page to history. |
247 void AddPageToHistory(const GURL& url) { | 247 void AddPageToHistory(const GURL& url) { |
248 RedirectList redirects; | 248 RedirectList redirects; |
249 redirects.push_back(url); | 249 redirects.push_back(url); |
250 history_service()->AddPage( | 250 history_service()->AddPage( |
251 url, base::Time::Now(), static_cast<void*>(this), 0, GURL(), | 251 url, base::Time::Now(), static_cast<void*>(this), 0, GURL(), |
252 redirects, content::PAGE_TRANSITION_TYPED, history::SOURCE_BROWSED, | 252 redirects, content::PAGE_TRANSITION_TYPED, history::SOURCE_BROWSED, |
253 false); | 253 false); |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 return top_sites()->GetUpdateDelay(); | 321 return top_sites()->GetUpdateDelay(); |
322 } | 322 } |
323 | 323 |
324 bool IsTopSitesLoaded() { return top_sites()->loaded_; } | 324 bool IsTopSitesLoaded() { return top_sites()->loaded_; } |
325 | 325 |
326 bool AddPrepopulatedPages(MostVisitedURLList* urls) { | 326 bool AddPrepopulatedPages(MostVisitedURLList* urls) { |
327 return top_sites()->AddPrepopulatedPages(urls); | 327 return top_sites()->AddPrepopulatedPages(urls); |
328 } | 328 } |
329 | 329 |
330 private: | 330 private: |
331 MessageLoopForUI message_loop_; | 331 base::MessageLoopForUI message_loop_; |
332 content::TestBrowserThread ui_thread_; | 332 content::TestBrowserThread ui_thread_; |
333 content::TestBrowserThread db_thread_; | 333 content::TestBrowserThread db_thread_; |
334 scoped_ptr<TestingProfile> profile_; | 334 scoped_ptr<TestingProfile> profile_; |
335 | 335 |
336 // To cancel HistoryService tasks. | 336 // To cancel HistoryService tasks. |
337 CancelableRequestConsumer consumer_; | 337 CancelableRequestConsumer consumer_; |
338 | 338 |
339 // To cancel TopSitesBackend tasks. | 339 // To cancel TopSitesBackend tasks. |
340 CancelableTaskTracker cancelable_task_tracker_; | 340 CancelableTaskTracker cancelable_task_tracker_; |
341 | 341 |
(...skipping 917 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1259 chrome::NOTIFICATION_TOP_SITES_LOADED, | 1259 chrome::NOTIFICATION_TOP_SITES_LOADED, |
1260 content::Source<Profile>(profile())); | 1260 content::Source<Profile>(profile())); |
1261 profile()->CreateTopSites(); | 1261 profile()->CreateTopSites(); |
1262 HistoryServiceFactory::GetForProfile( | 1262 HistoryServiceFactory::GetForProfile( |
1263 profile(), Profile::EXPLICIT_ACCESS)->UnloadBackend(); | 1263 profile(), Profile::EXPLICIT_ACCESS)->UnloadBackend(); |
1264 profile()->BlockUntilHistoryProcessesPendingRequests(); | 1264 profile()->BlockUntilHistoryProcessesPendingRequests(); |
1265 observer.Wait(); | 1265 observer.Wait(); |
1266 } | 1266 } |
1267 | 1267 |
1268 } // namespace history | 1268 } // namespace history |
OLD | NEW |