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

Side by Side Diff: chrome/browser/custom_home_pages_table_model.cc

Issue 314293005: Change HistoryService::QueryURL to use CancelableTaskTracker (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase & fix style errors Created 6 years, 6 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
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 "chrome/browser/custom_home_pages_table_model.h" 5 #include "chrome/browser/custom_home_pages_table_model.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/i18n/rtl.h" 9 #include "base/i18n/rtl.h"
10 #include "base/prefs/pref_service.h" 10 #include "base/prefs/pref_service.h"
11 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/history/history_service.h"
12 #include "chrome/browser/history/history_service_factory.h" 13 #include "chrome/browser/history/history_service_factory.h"
13 #include "chrome/browser/profiles/profile.h" 14 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/browser.h" 15 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_iterator.h" 16 #include "chrome/browser/ui/browser_iterator.h"
16 #include "chrome/browser/ui/browser_list.h" 17 #include "chrome/browser/ui/browser_list.h"
17 #include "chrome/browser/ui/tabs/tab_strip_model.h" 18 #include "chrome/browser/ui/tabs/tab_strip_model.h"
18 #include "chrome/common/pref_names.h" 19 #include "chrome/common/pref_names.h"
19 #include "chrome/common/url_constants.h" 20 #include "chrome/common/url_constants.h"
20 #include "content/public/browser/web_contents.h" 21 #include "content/public/browser/web_contents.h"
21 #include "grit/generated_resources.h" 22 #include "grit/generated_resources.h"
(...skipping 26 matching lines...) Expand all
48 return false; 49 return false;
49 } 50 }
50 } 51 }
51 52
52 return true; 53 return true;
53 } 54 }
54 55
55 } // namespace 56 } // namespace
56 57
57 struct CustomHomePagesTableModel::Entry { 58 struct CustomHomePagesTableModel::Entry {
58 Entry() : title_handle(0) {} 59 Entry() : task_id(base::CancelableTaskTracker::kBadTaskId) {}
59 60
60 // URL of the page. 61 // URL of the page.
61 GURL url; 62 GURL url;
62 63
63 // Page title. If this is empty, we'll display the URL as the entry. 64 // Page title. If this is empty, we'll display the URL as the entry.
64 base::string16 title; 65 base::string16 title;
65 66
66 // If non-zero, indicates we're loading the title for the page. 67 // If not |base::CancelableTaskTracker::kBadTaskId|, indicates we're loading
67 HistoryService::Handle title_handle; 68 // the title for the page.
69 base::CancelableTaskTracker::TaskId task_id;
68 }; 70 };
69 71
70 CustomHomePagesTableModel::CustomHomePagesTableModel(Profile* profile) 72 CustomHomePagesTableModel::CustomHomePagesTableModel(Profile* profile)
71 : profile_(profile), 73 : profile_(profile),
72 observer_(NULL) { 74 observer_(NULL) {
73 } 75 }
74 76
75 CustomHomePagesTableModel::~CustomHomePagesTableModel() { 77 CustomHomePagesTableModel::~CustomHomePagesTableModel() {
76 } 78 }
77 79
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 LoadTitle(&(entries_[index])); 154 LoadTitle(&(entries_[index]));
153 if (observer_) 155 if (observer_)
154 observer_->OnItemsAdded(index, 1); 156 observer_->OnItemsAdded(index, 1);
155 } 157 }
156 158
157 void CustomHomePagesTableModel::Remove(int index) { 159 void CustomHomePagesTableModel::Remove(int index) {
158 DCHECK(index >= 0 && index < RowCount()); 160 DCHECK(index >= 0 && index < RowCount());
159 Entry* entry = &(entries_[index]); 161 Entry* entry = &(entries_[index]);
160 // Cancel any pending load requests now so we don't deref a bogus pointer when 162 // Cancel any pending load requests now so we don't deref a bogus pointer when
161 // we get the loaded notification. 163 // we get the loaded notification.
162 if (entry->title_handle) { 164 if (entry->task_id != base::CancelableTaskTracker::kBadTaskId) {
163 HistoryService* history_service = HistoryServiceFactory::GetForProfile( 165 task_tracker_.TryCancel(entry->task_id);
164 profile_, Profile::EXPLICIT_ACCESS); 166 entry->task_id = base::CancelableTaskTracker::kBadTaskId;
165 if (history_service)
166 history_service->CancelRequest(entry->title_handle);
167 } 167 }
168 entries_.erase(entries_.begin() + static_cast<size_t>(index)); 168 entries_.erase(entries_.begin() + static_cast<size_t>(index));
169 if (observer_) 169 if (observer_)
170 observer_->OnItemsRemoved(index, 1); 170 observer_->OnItemsRemoved(index, 1);
171 } 171 }
172 172
173 void CustomHomePagesTableModel::SetToCurrentlyOpenPages() { 173 void CustomHomePagesTableModel::SetToCurrentlyOpenPages() {
174 // Remove the current entries. 174 // Remove the current entries.
175 while (RowCount()) 175 while (RowCount())
176 Remove(0); 176 Remove(0);
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 } 217 }
218 218
219 void CustomHomePagesTableModel::SetObserver(ui::TableModelObserver* observer) { 219 void CustomHomePagesTableModel::SetObserver(ui::TableModelObserver* observer) {
220 observer_ = observer; 220 observer_ = observer;
221 } 221 }
222 222
223 void CustomHomePagesTableModel::LoadTitle(Entry* entry) { 223 void CustomHomePagesTableModel::LoadTitle(Entry* entry) {
224 HistoryService* history_service = HistoryServiceFactory::GetForProfile( 224 HistoryService* history_service = HistoryServiceFactory::GetForProfile(
225 profile_, Profile::EXPLICIT_ACCESS); 225 profile_, Profile::EXPLICIT_ACCESS);
226 if (history_service) { 226 if (history_service) {
227 entry->title_handle = history_service->QueryURL(entry->url, false, 227 base::CancelableTaskTracker::TaskId taskId = history_service->QueryURL(
228 &history_query_consumer_, 228 entry->url,
229 false,
229 base::Bind(&CustomHomePagesTableModel::OnGotTitle, 230 base::Bind(&CustomHomePagesTableModel::OnGotTitle,
230 base::Unretained(this))); 231 base::Unretained(this),
232 entry->url),
233 &task_tracker_);
234 if (taskId != base::CancelableTaskTracker::kBadTaskId)
blundell 2014/06/11 11:08:40 nit: this if seems unnecessary.
sdefresne 2014/06/11 16:46:25 Done.
235 entry->task_id = taskId;
231 } 236 }
232 } 237 }
233 238
234 void CustomHomePagesTableModel::OnGotTitle(HistoryService::Handle handle, 239 void CustomHomePagesTableModel::OnGotTitle(const GURL& entry_url,
235 bool found_url, 240 bool found_url,
236 const history::URLRow* row, 241 const history::URLRow& row,
237 history::VisitVector* visits) { 242 const history::VisitVector& visits) {
238 int entry_index; 243 int entry_index = 0;
239 Entry* entry = 244 Entry* entry = NULL;
240 GetEntryByLoadHandle(&Entry::title_handle, handle, &entry_index); 245 for (size_t i = 0; i < entries_.size(); ++i) {
246 if (entries_[i].url == entry_url) {
247 entry = &entries_[i];
248 entry_index = i;
249 break;
250 }
251 }
241 if (!entry) { 252 if (!entry) {
242 // The URLs changed before we were called back. 253 // The URLs changed before we were called back.
243 return; 254 return;
244 } 255 }
245 entry->title_handle = 0; 256 entry->task_id = base::CancelableTaskTracker::kBadTaskId;
246 if (found_url && !row->title().empty()) { 257 if (found_url && !row.title().empty()) {
247 entry->title = row->title(); 258 entry->title = row.title();
248 if (observer_) 259 if (observer_)
249 observer_->OnItemsChanged(static_cast<int>(entry_index), 1); 260 observer_->OnItemsChanged(static_cast<int>(entry_index), 1);
250 } 261 }
251 } 262 }
252 263
253 CustomHomePagesTableModel::Entry*
254 CustomHomePagesTableModel::GetEntryByLoadHandle(
255 CancelableRequestProvider::Handle Entry::* member,
256 CancelableRequestProvider::Handle handle,
257 int* index) {
258 for (size_t i = 0; i < entries_.size(); ++i) {
259 if (entries_[i].*member == handle) {
260 *index = static_cast<int>(i);
261 return &entries_[i];
262 }
263 }
264 return NULL;
265 }
266
267 base::string16 CustomHomePagesTableModel::FormattedURL(int row) const { 264 base::string16 CustomHomePagesTableModel::FormattedURL(int row) const {
268 std::string languages = 265 std::string languages =
269 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages); 266 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages);
270 base::string16 url = net::FormatUrl(entries_[row].url, languages); 267 base::string16 url = net::FormatUrl(entries_[row].url, languages);
271 url = base::i18n::GetDisplayStringInLTRDirectionality(url); 268 url = base::i18n::GetDisplayStringInLTRDirectionality(url);
272 return url; 269 return url;
273 } 270 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698