Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(407)

Side by Side Diff: chrome/browser/history/top_sites_unittest.cc

Issue 14113053: chrome: Use base::MessageLoop. (Part 3) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)), 77 : ALLOW_THIS_IN_INITIALIZER_LIST(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(TopSites* top_sites, bool wait) { 83 void QueryTopSites(TopSites* 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 TopSites::GetMostVisitedURLs. 104 // Callback for TopSites::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
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(&TopSitesTest::QuitCallback, base::Unretained(this)), 203 base::Bind(&TopSitesTest::QuitCallback, base::Unretained(this)),
204 &cancelable_task_tracker_); 204 &cancelable_task_tracker_);
205 MessageLoop::current()->Run(); 205 base::MessageLoop::current()->Run();
206 } 206 }
207 207
208 TopSites* top_sites() { return profile_->GetTopSites(); } 208 TopSites* top_sites() { return profile_->GetTopSites(); }
209 CancelableRequestConsumer* consumer() { return &consumer_; } 209 CancelableRequestConsumer* consumer() { return &consumer_; }
210 TestingProfile* profile() {return profile_.get();} 210 TestingProfile* profile() {return profile_.get();}
211 HistoryService* history_service() { 211 HistoryService* history_service() {
212 return HistoryServiceFactory::GetForProfile(profile_.get(), 212 return HistoryServiceFactory::GetForProfile(profile_.get(),
213 Profile::EXPLICIT_ACCESS); 213 Profile::EXPLICIT_ACCESS);
214 } 214 }
215 215
(...skipping 14 matching lines...) Expand all
230 } 230 }
231 } 231 }
232 232
233 // Used for callbacks from history. 233 // Used for callbacks from history.
234 void EmptyCallback() { 234 void EmptyCallback() {
235 } 235 }
236 236
237 // Quit the current message loop when invoked. Useful when running a nested 237 // Quit the current message loop when invoked. Useful when running a nested
238 // message loop. 238 // message loop.
239 void QuitCallback() { 239 void QuitCallback() {
240 MessageLoop::current()->Quit(); 240 base::MessageLoop::current()->Quit();
241 } 241 }
242 242
243 // Adds a page to history. 243 // Adds a page to history.
244 void AddPageToHistory(const GURL& url) { 244 void AddPageToHistory(const GURL& url) {
245 RedirectList redirects; 245 RedirectList redirects;
246 redirects.push_back(url); 246 redirects.push_back(url);
247 history_service()->AddPage( 247 history_service()->AddPage(
248 url, base::Time::Now(), static_cast<void*>(this), 0, GURL(), 248 url, base::Time::Now(), static_cast<void*>(this), 0, GURL(),
249 redirects, content::PAGE_TRANSITION_TYPED, history::SOURCE_BROWSED, 249 redirects, content::PAGE_TRANSITION_TYPED, history::SOURCE_BROWSED,
250 false); 250 false);
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 return top_sites()->GetUpdateDelay(); 318 return top_sites()->GetUpdateDelay();
319 } 319 }
320 320
321 bool IsTopSitesLoaded() { return top_sites()->loaded_; } 321 bool IsTopSitesLoaded() { return top_sites()->loaded_; }
322 322
323 bool AddPrepopulatedPages(MostVisitedURLList* urls) { 323 bool AddPrepopulatedPages(MostVisitedURLList* urls) {
324 return top_sites()->AddPrepopulatedPages(urls); 324 return top_sites()->AddPrepopulatedPages(urls);
325 } 325 }
326 326
327 private: 327 private:
328 MessageLoopForUI message_loop_; 328 base::MessageLoopForUI message_loop_;
329 content::TestBrowserThread ui_thread_; 329 content::TestBrowserThread ui_thread_;
330 content::TestBrowserThread db_thread_; 330 content::TestBrowserThread db_thread_;
331 scoped_ptr<TestingProfile> profile_; 331 scoped_ptr<TestingProfile> profile_;
332 332
333 // To cancel HistoryService tasks. 333 // To cancel HistoryService tasks.
334 CancelableRequestConsumer consumer_; 334 CancelableRequestConsumer consumer_;
335 335
336 // To cancel TopSitesBackend tasks. 336 // To cancel TopSitesBackend tasks.
337 CancelableTaskTracker cancelable_task_tracker_; 337 CancelableTaskTracker cancelable_task_tracker_;
338 338
(...skipping 917 matching lines...) Expand 10 before | Expand all | Expand 10 after
1256 chrome::NOTIFICATION_TOP_SITES_LOADED, 1256 chrome::NOTIFICATION_TOP_SITES_LOADED,
1257 content::Source<Profile>(profile())); 1257 content::Source<Profile>(profile()));
1258 profile()->CreateTopSites(); 1258 profile()->CreateTopSites();
1259 HistoryServiceFactory::GetForProfile( 1259 HistoryServiceFactory::GetForProfile(
1260 profile(), Profile::EXPLICIT_ACCESS)->UnloadBackend(); 1260 profile(), Profile::EXPLICIT_ACCESS)->UnloadBackend();
1261 profile()->BlockUntilHistoryProcessesPendingRequests(); 1261 profile()->BlockUntilHistoryProcessesPendingRequests();
1262 observer.Wait(); 1262 observer.Wait();
1263 } 1263 }
1264 1264
1265 } // namespace history 1265 } // namespace history
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698