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(TopSitesImpl* top_sites, bool wait) { | 83 void QueryTopSites(TopSitesImpl* 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 TopSitesImpl::GetMostVisitedURLs. | 104 // Callback for TopSitesImpl::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(&TopSitesImplTest::QuitCallback, base::Unretained(this)), | 203 base::Bind(&TopSitesImplTest::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 TopSitesImpl* top_sites() { | 208 TopSitesImpl* top_sites() { |
209 return static_cast<TopSitesImpl*>(profile_->GetTopSites()); | 209 return static_cast<TopSitesImpl*>(profile_->GetTopSites()); |
210 } | 210 } |
211 CancelableRequestConsumer* consumer() { return &consumer_; } | 211 CancelableRequestConsumer* consumer() { return &consumer_; } |
212 TestingProfile* profile() {return profile_.get();} | 212 TestingProfile* profile() {return profile_.get();} |
213 HistoryService* history_service() { | 213 HistoryService* history_service() { |
214 return HistoryServiceFactory::GetForProfile(profile_.get(), | 214 return HistoryServiceFactory::GetForProfile(profile_.get(), |
215 Profile::EXPLICIT_ACCESS); | 215 Profile::EXPLICIT_ACCESS); |
(...skipping 16 matching lines...) Expand all Loading... |
232 } | 232 } |
233 } | 233 } |
234 | 234 |
235 // Used for callbacks from history. | 235 // Used for callbacks from history. |
236 void EmptyCallback() { | 236 void EmptyCallback() { |
237 } | 237 } |
238 | 238 |
239 // Quit the current message loop when invoked. Useful when running a nested | 239 // Quit the current message loop when invoked. Useful when running a nested |
240 // message loop. | 240 // message loop. |
241 void QuitCallback() { | 241 void QuitCallback() { |
242 MessageLoop::current()->Quit(); | 242 base::MessageLoop::current()->Quit(); |
243 } | 243 } |
244 | 244 |
245 // Adds a page to history. | 245 // Adds a page to history. |
246 void AddPageToHistory(const GURL& url) { | 246 void AddPageToHistory(const GURL& url) { |
247 RedirectList redirects; | 247 RedirectList redirects; |
248 redirects.push_back(url); | 248 redirects.push_back(url); |
249 history_service()->AddPage( | 249 history_service()->AddPage( |
250 url, base::Time::Now(), static_cast<void*>(this), 0, GURL(), | 250 url, base::Time::Now(), static_cast<void*>(this), 0, GURL(), |
251 redirects, content::PAGE_TRANSITION_TYPED, history::SOURCE_BROWSED, | 251 redirects, content::PAGE_TRANSITION_TYPED, history::SOURCE_BROWSED, |
252 false); | 252 false); |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 return top_sites()->GetUpdateDelay(); | 320 return top_sites()->GetUpdateDelay(); |
321 } | 321 } |
322 | 322 |
323 bool IsTopSitesLoaded() { return top_sites()->loaded_; } | 323 bool IsTopSitesLoaded() { return top_sites()->loaded_; } |
324 | 324 |
325 bool AddPrepopulatedPages(MostVisitedURLList* urls) { | 325 bool AddPrepopulatedPages(MostVisitedURLList* urls) { |
326 return top_sites()->AddPrepopulatedPages(urls); | 326 return top_sites()->AddPrepopulatedPages(urls); |
327 } | 327 } |
328 | 328 |
329 private: | 329 private: |
330 MessageLoopForUI message_loop_; | 330 base::MessageLoopForUI message_loop_; |
331 content::TestBrowserThread ui_thread_; | 331 content::TestBrowserThread ui_thread_; |
332 content::TestBrowserThread db_thread_; | 332 content::TestBrowserThread db_thread_; |
333 scoped_ptr<TestingProfile> profile_; | 333 scoped_ptr<TestingProfile> profile_; |
334 | 334 |
335 // To cancel HistoryService tasks. | 335 // To cancel HistoryService tasks. |
336 CancelableRequestConsumer consumer_; | 336 CancelableRequestConsumer consumer_; |
337 | 337 |
338 // To cancel TopSitesBackend tasks. | 338 // To cancel TopSitesBackend tasks. |
339 CancelableTaskTracker cancelable_task_tracker_; | 339 CancelableTaskTracker cancelable_task_tracker_; |
340 | 340 |
(...skipping 917 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1258 chrome::NOTIFICATION_TOP_SITES_LOADED, | 1258 chrome::NOTIFICATION_TOP_SITES_LOADED, |
1259 content::Source<Profile>(profile())); | 1259 content::Source<Profile>(profile())); |
1260 profile()->CreateTopSites(); | 1260 profile()->CreateTopSites(); |
1261 HistoryServiceFactory::GetForProfile( | 1261 HistoryServiceFactory::GetForProfile( |
1262 profile(), Profile::EXPLICIT_ACCESS)->UnloadBackend(); | 1262 profile(), Profile::EXPLICIT_ACCESS)->UnloadBackend(); |
1263 profile()->BlockUntilHistoryProcessesPendingRequests(); | 1263 profile()->BlockUntilHistoryProcessesPendingRequests(); |
1264 observer.Wait(); | 1264 observer.Wait(); |
1265 } | 1265 } |
1266 | 1266 |
1267 } // namespace history | 1267 } // namespace history |
OLD | NEW |