| 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" | |
| 13 #include "chrome/browser/history/history_service_factory.h" | 12 #include "chrome/browser/history/history_service_factory.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
| 16 #include "chrome/browser/ui/browser_iterator.h" | 15 #include "chrome/browser/ui/browser_iterator.h" |
| 17 #include "chrome/browser/ui/browser_list.h" | 16 #include "chrome/browser/ui/browser_list.h" |
| 18 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 17 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 19 #include "chrome/common/pref_names.h" | 18 #include "chrome/common/pref_names.h" |
| 20 #include "chrome/common/url_constants.h" | 19 #include "chrome/common/url_constants.h" |
| 21 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
| 22 #include "grit/generated_resources.h" | 21 #include "grit/generated_resources.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 49 return false; | 48 return false; |
| 50 } | 49 } |
| 51 } | 50 } |
| 52 | 51 |
| 53 return true; | 52 return true; |
| 54 } | 53 } |
| 55 | 54 |
| 56 } // namespace | 55 } // namespace |
| 57 | 56 |
| 58 struct CustomHomePagesTableModel::Entry { | 57 struct CustomHomePagesTableModel::Entry { |
| 59 Entry() : task_id(base::CancelableTaskTracker::kBadTaskId) {} | 58 Entry() : title_handle(0) {} |
| 60 | 59 |
| 61 // URL of the page. | 60 // URL of the page. |
| 62 GURL url; | 61 GURL url; |
| 63 | 62 |
| 64 // Page title. If this is empty, we'll display the URL as the entry. | 63 // Page title. If this is empty, we'll display the URL as the entry. |
| 65 base::string16 title; | 64 base::string16 title; |
| 66 | 65 |
| 67 // If not |base::CancelableTaskTracker::kBadTaskId|, indicates we're loading | 66 // If non-zero, indicates we're loading the title for the page. |
| 68 // the title for the page. | 67 HistoryService::Handle title_handle; |
| 69 base::CancelableTaskTracker::TaskId task_id; | |
| 70 }; | 68 }; |
| 71 | 69 |
| 72 CustomHomePagesTableModel::CustomHomePagesTableModel(Profile* profile) | 70 CustomHomePagesTableModel::CustomHomePagesTableModel(Profile* profile) |
| 73 : profile_(profile), | 71 : profile_(profile), |
| 74 observer_(NULL) { | 72 observer_(NULL) { |
| 75 } | 73 } |
| 76 | 74 |
| 77 CustomHomePagesTableModel::~CustomHomePagesTableModel() { | 75 CustomHomePagesTableModel::~CustomHomePagesTableModel() { |
| 78 } | 76 } |
| 79 | 77 |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 LoadTitle(&(entries_[index])); | 152 LoadTitle(&(entries_[index])); |
| 155 if (observer_) | 153 if (observer_) |
| 156 observer_->OnItemsAdded(index, 1); | 154 observer_->OnItemsAdded(index, 1); |
| 157 } | 155 } |
| 158 | 156 |
| 159 void CustomHomePagesTableModel::Remove(int index) { | 157 void CustomHomePagesTableModel::Remove(int index) { |
| 160 DCHECK(index >= 0 && index < RowCount()); | 158 DCHECK(index >= 0 && index < RowCount()); |
| 161 Entry* entry = &(entries_[index]); | 159 Entry* entry = &(entries_[index]); |
| 162 // Cancel any pending load requests now so we don't deref a bogus pointer when | 160 // Cancel any pending load requests now so we don't deref a bogus pointer when |
| 163 // we get the loaded notification. | 161 // we get the loaded notification. |
| 164 if (entry->task_id != base::CancelableTaskTracker::kBadTaskId) { | 162 if (entry->title_handle) { |
| 165 task_tracker_.TryCancel(entry->task_id); | 163 HistoryService* history_service = HistoryServiceFactory::GetForProfile( |
| 166 entry->task_id = base::CancelableTaskTracker::kBadTaskId; | 164 profile_, Profile::EXPLICIT_ACCESS); |
| 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->task_id = history_service->QueryURL( | 227 entry->title_handle = history_service->QueryURL(entry->url, false, |
| 228 entry->url, | 228 &history_query_consumer_, |
| 229 false, | |
| 230 base::Bind(&CustomHomePagesTableModel::OnGotTitle, | 229 base::Bind(&CustomHomePagesTableModel::OnGotTitle, |
| 231 base::Unretained(this), | 230 base::Unretained(this))); |
| 232 entry->url), | |
| 233 &task_tracker_); | |
| 234 } | 231 } |
| 235 } | 232 } |
| 236 | 233 |
| 237 void CustomHomePagesTableModel::OnGotTitle(const GURL& entry_url, | 234 void CustomHomePagesTableModel::OnGotTitle(HistoryService::Handle handle, |
| 238 bool found_url, | 235 bool found_url, |
| 239 const history::URLRow& row, | 236 const history::URLRow* row, |
| 240 const history::VisitVector& visits) { | 237 history::VisitVector* visits) { |
| 241 int entry_index = 0; | 238 int entry_index; |
| 242 Entry* entry = NULL; | 239 Entry* entry = |
| 243 for (size_t i = 0; i < entries_.size(); ++i) { | 240 GetEntryByLoadHandle(&Entry::title_handle, handle, &entry_index); |
| 244 if (entries_[i].url == entry_url) { | |
| 245 entry = &entries_[i]; | |
| 246 entry_index = i; | |
| 247 break; | |
| 248 } | |
| 249 } | |
| 250 if (!entry) { | 241 if (!entry) { |
| 251 // The URLs changed before we were called back. | 242 // The URLs changed before we were called back. |
| 252 return; | 243 return; |
| 253 } | 244 } |
| 254 entry->task_id = base::CancelableTaskTracker::kBadTaskId; | 245 entry->title_handle = 0; |
| 255 if (found_url && !row.title().empty()) { | 246 if (found_url && !row->title().empty()) { |
| 256 entry->title = row.title(); | 247 entry->title = row->title(); |
| 257 if (observer_) | 248 if (observer_) |
| 258 observer_->OnItemsChanged(static_cast<int>(entry_index), 1); | 249 observer_->OnItemsChanged(static_cast<int>(entry_index), 1); |
| 259 } | 250 } |
| 260 } | 251 } |
| 261 | 252 |
| 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 |
| 262 base::string16 CustomHomePagesTableModel::FormattedURL(int row) const { | 267 base::string16 CustomHomePagesTableModel::FormattedURL(int row) const { |
| 263 std::string languages = | 268 std::string languages = |
| 264 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages); | 269 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages); |
| 265 base::string16 url = net::FormatUrl(entries_[row].url, languages); | 270 base::string16 url = net::FormatUrl(entries_[row].url, languages); |
| 266 url = base::i18n::GetDisplayStringInLTRDirectionality(url); | 271 url = base::i18n::GetDisplayStringInLTRDirectionality(url); |
| 267 return url; | 272 return url; |
| 268 } | 273 } |
| OLD | NEW |