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 "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 Loading... | |
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 Loading... | |
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 Loading... | |
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 entry->task_id = 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_); | |
231 } | 234 } |
232 } | 235 } |
233 | 236 |
234 void CustomHomePagesTableModel::OnGotTitle(HistoryService::Handle handle, | 237 void CustomHomePagesTableModel::OnGotTitle(const GURL& entry_url, |
235 bool found_url, | 238 bool found_url, |
236 const history::URLRow* row, | 239 const history::URLRow& row, |
237 history::VisitVector* visits) { | 240 const history::VisitVector& visits) { |
238 int entry_index; | 241 int entry_index = 0; |
239 Entry* entry = | 242 Entry* entry = NULL; |
240 GetEntryByLoadHandle(&Entry::title_handle, handle, &entry_index); | 243 for (size_t i = 0; i < entries_.size(); ++i) { |
244 if (entries_[i].url == entry_url) { | |
245 entry = &entries_[i]; | |
246 entry_index = i; | |
247 break; | |
248 } | |
249 } | |
241 if (!entry) { | 250 if (!entry) { |
242 // The URLs changed before we were called back. | 251 // The URLs changed before we were called back. |
243 return; | 252 return; |
244 } | 253 } |
245 entry->title_handle = 0; | 254 entry->task_id = base::CancelableTaskTracker::kBadTaskId; |
246 if (found_url && !row->title().empty()) { | 255 if (found_url && !row.title().empty()) { |
247 entry->title = row->title(); | 256 entry->title = row.title(); |
248 if (observer_) | 257 if (observer_) |
249 observer_->OnItemsChanged(static_cast<int>(entry_index), 1); | 258 observer_->OnItemsChanged(static_cast<int>(entry_index), 1); |
Scott Hess - ex-Googler
2014/06/12 18:04:35
This case seems wrong ... but assigning a size_t t
| |
250 } | 259 } |
251 } | 260 } |
252 | 261 |
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 { | 262 base::string16 CustomHomePagesTableModel::FormattedURL(int row) const { |
268 std::string languages = | 263 std::string languages = |
269 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages); | 264 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages); |
270 base::string16 url = net::FormatUrl(entries_[row].url, languages); | 265 base::string16 url = net::FormatUrl(entries_[row].url, languages); |
271 url = base::i18n::GetDisplayStringInLTRDirectionality(url); | 266 url = base::i18n::GetDisplayStringInLTRDirectionality(url); |
272 return url; | 267 return url; |
273 } | 268 } |
OLD | NEW |