Disabling NativeViewAcccessibilityWinTest.RetrieveAllAlerts.
[chromium-blink-merge.git] / chrome / browser / custom_home_pages_table_model.cc
blob240c6193e9781924ab119ef0d52a668f7cae7d4d
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
3 // found in the LICENSE file.
5 #include "chrome/browser/custom_home_pages_table_model.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/i18n/rtl.h"
10 #include "base/prefs/pref_service.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"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/browser_iterator.h"
17 #include "chrome/browser/ui/browser_list.h"
18 #include "chrome/browser/ui/tabs/tab_strip_model.h"
19 #include "chrome/common/pref_names.h"
20 #include "chrome/common/url_constants.h"
21 #include "chrome/grit/generated_resources.h"
22 #include "content/public/browser/web_contents.h"
23 #include "net/base/net_util.h"
24 #include "ui/base/l10n/l10n_util.h"
25 #include "ui/base/models/table_model_observer.h"
26 #include "ui/gfx/codec/png_codec.h"
27 #include "url/gurl.h"
29 namespace {
31 // Checks whether the given URL should count as one of the "current" pages.
32 // Returns true for all pages except dev tools and settings.
33 bool ShouldAddPage(const GURL& url) {
34 if (url.is_empty())
35 return false;
37 if (url.SchemeIs(content::kChromeDevToolsScheme))
38 return false;
40 if (url.SchemeIs(content::kChromeUIScheme)) {
41 if (url.host() == chrome::kChromeUISettingsHost)
42 return false;
44 // For a settings page, the path will start with "/settings" not "settings"
45 // so find() will return 1, not 0.
46 if (url.host() == chrome::kChromeUIUberHost &&
47 url.path().find(chrome::kChromeUISettingsHost) == 1) {
48 return false;
52 return true;
55 } // namespace
57 struct CustomHomePagesTableModel::Entry {
58 Entry() : task_id(base::CancelableTaskTracker::kBadTaskId) {}
60 // URL of the page.
61 GURL url;
63 // Page title. If this is empty, we'll display the URL as the entry.
64 base::string16 title;
66 // If not |base::CancelableTaskTracker::kBadTaskId|, indicates we're loading
67 // the title for the page.
68 base::CancelableTaskTracker::TaskId task_id;
71 CustomHomePagesTableModel::CustomHomePagesTableModel(Profile* profile)
72 : profile_(profile),
73 observer_(NULL),
74 num_outstanding_title_lookups_(0) {
77 CustomHomePagesTableModel::~CustomHomePagesTableModel() {
80 void CustomHomePagesTableModel::SetURLs(const std::vector<GURL>& urls) {
81 entries_.resize(urls.size());
82 for (size_t i = 0; i < urls.size(); ++i) {
83 entries_[i].url = urls[i];
84 entries_[i].title.erase();
86 LoadAllTitles();
89 /**
90 * Move a number of existing entries to a new position, reordering the table.
92 * We determine the range of elements affected by the move, save the moved
93 * elements, compact the remaining ones, and re-insert moved elements.
94 * Expects |index_list| to be ordered ascending.
96 void CustomHomePagesTableModel::MoveURLs(int insert_before,
97 const std::vector<int>& index_list) {
98 if (index_list.empty()) return;
99 DCHECK(insert_before >= 0 && insert_before <= RowCount());
101 // The range of elements that needs to be reshuffled is [ |first|, |last| ).
102 int first = std::min(insert_before, index_list.front());
103 int last = std::max(insert_before, index_list.back() + 1);
105 // Save the dragged elements. Also, adjust insertion point if it is before a
106 // dragged element.
107 std::vector<Entry> moved_entries;
108 for (size_t i = 0; i < index_list.size(); ++i) {
109 moved_entries.push_back(entries_[index_list[i]]);
110 if (index_list[i] == insert_before)
111 insert_before++;
114 // Compact the range between beginning and insertion point, moving downwards.
115 size_t skip_count = 0;
116 for (int i = first; i < insert_before; ++i) {
117 if (skip_count < index_list.size() && index_list[skip_count] == i)
118 skip_count++;
119 else
120 entries_[i - skip_count] = entries_[i];
123 // Moving items down created a gap. We start compacting up after it.
124 first = insert_before;
125 insert_before -= skip_count;
127 // Now compact up for elements after the insertion point.
128 skip_count = 0;
129 for (int i = last - 1; i >= first; --i) {
130 if (skip_count < index_list.size() &&
131 index_list[index_list.size() - skip_count - 1] == i) {
132 skip_count++;
133 } else {
134 entries_[i + skip_count] = entries_[i];
138 // Insert moved elements.
139 std::copy(moved_entries.begin(), moved_entries.end(),
140 entries_.begin() + insert_before);
142 // Possibly large change, so tell the view to just rebuild itself.
143 if (observer_)
144 observer_->OnModelChanged();
147 void CustomHomePagesTableModel::AddWithoutNotification(
148 int index, const GURL& url) {
149 DCHECK(index >= 0 && index <= RowCount());
150 entries_.insert(entries_.begin() + static_cast<size_t>(index), Entry());
151 entries_[index].url = url;
154 void CustomHomePagesTableModel::Add(int index, const GURL& url) {
155 AddWithoutNotification(index, url);
156 LoadTitle(&(entries_[index]));
157 if (observer_)
158 observer_->OnItemsAdded(index, 1);
161 void CustomHomePagesTableModel::RemoveWithoutNotification(int index) {
162 DCHECK(index >= 0 && index < RowCount());
163 Entry* entry = &(entries_[index]);
164 // Cancel any pending load requests now so we don't deref a bogus pointer when
165 // we get the loaded notification.
166 if (entry->task_id != base::CancelableTaskTracker::kBadTaskId) {
167 task_tracker_.TryCancel(entry->task_id);
168 entry->task_id = base::CancelableTaskTracker::kBadTaskId;
170 entries_.erase(entries_.begin() + static_cast<size_t>(index));
173 void CustomHomePagesTableModel::Remove(int index) {
174 RemoveWithoutNotification(index);
175 if (observer_)
176 observer_->OnItemsRemoved(index, 1);
179 void CustomHomePagesTableModel::SetToCurrentlyOpenPages() {
180 // Remove the current entries.
181 while (RowCount())
182 RemoveWithoutNotification(0);
184 // And add all tabs for all open browsers with our profile.
185 int add_index = 0;
186 for (chrome::BrowserIterator it; !it.done(); it.Next()) {
187 Browser* browser = *it;
188 if (browser->profile() != profile_)
189 continue; // Skip incognito browsers.
191 for (int tab_index = 0;
192 tab_index < browser->tab_strip_model()->count();
193 ++tab_index) {
194 const GURL url =
195 browser->tab_strip_model()->GetWebContentsAt(tab_index)->GetURL();
196 if (ShouldAddPage(url))
197 AddWithoutNotification(add_index++, url);
200 LoadAllTitles();
203 std::vector<GURL> CustomHomePagesTableModel::GetURLs() {
204 std::vector<GURL> urls(entries_.size());
205 for (size_t i = 0; i < entries_.size(); ++i)
206 urls[i] = entries_[i].url;
207 return urls;
210 int CustomHomePagesTableModel::RowCount() {
211 return static_cast<int>(entries_.size());
214 base::string16 CustomHomePagesTableModel::GetText(int row, int column_id) {
215 DCHECK(column_id == 0);
216 DCHECK(row >= 0 && row < RowCount());
217 return entries_[row].title.empty() ? FormattedURL(row) : entries_[row].title;
220 base::string16 CustomHomePagesTableModel::GetTooltip(int row) {
221 return entries_[row].title.empty() ? base::string16() :
222 l10n_util::GetStringFUTF16(IDS_OPTIONS_STARTUP_PAGE_TOOLTIP,
223 entries_[row].title, FormattedURL(row));
226 void CustomHomePagesTableModel::SetObserver(ui::TableModelObserver* observer) {
227 observer_ = observer;
230 void CustomHomePagesTableModel::LoadTitle(Entry* entry) {
231 HistoryService* history_service = HistoryServiceFactory::GetForProfile(
232 profile_, ServiceAccessType::EXPLICIT_ACCESS);
233 if (history_service) {
234 entry->task_id = history_service->QueryURL(
235 entry->url,
236 false,
237 base::Bind(&CustomHomePagesTableModel::OnGotTitle,
238 base::Unretained(this),
239 entry->url,
240 false),
241 &task_tracker_);
245 void CustomHomePagesTableModel::LoadAllTitles() {
246 HistoryService* history_service = HistoryServiceFactory::GetForProfile(
247 profile_, ServiceAccessType::EXPLICIT_ACCESS);
248 // It's possible for multiple LoadAllTitles() queries to be inflight we want
249 // to make sure everything is resolved before updating the observer or we risk
250 // getting rendering glitches.
251 num_outstanding_title_lookups_ += entries_.size();
252 for (Entry& entry : entries_) {
253 if (history_service) {
254 entry.task_id = history_service->QueryURL(
255 entry.url,
256 false,
257 base::Bind(&CustomHomePagesTableModel::OnGotOneOfManyTitles,
258 base::Unretained(this),
259 entry.url),
260 &task_tracker_);
263 if (entries_.empty())
264 observer_->OnModelChanged();
267 void CustomHomePagesTableModel::OnGotOneOfManyTitles(const GURL& entry_url,
268 bool found_url,
269 const history::URLRow& row,
270 const history::VisitVector& visits) {
271 OnGotTitle(entry_url, false, found_url, row, visits);
272 DCHECK_GE(num_outstanding_title_lookups_, 1);
273 if (--num_outstanding_title_lookups_ == 0 && observer_)
274 observer_->OnModelChanged();
277 void CustomHomePagesTableModel::OnGotTitle(const GURL& entry_url,
278 bool observable,
279 bool found_url,
280 const history::URLRow& row,
281 const history::VisitVector& visits) {
282 Entry* entry = NULL;
283 size_t entry_index = 0;
284 for (size_t i = 0; i < entries_.size(); ++i) {
285 if (entries_[i].url == entry_url) {
286 entry = &entries_[i];
287 entry_index = i;
288 break;
291 if (!entry) {
292 // The URLs changed before we were called back.
293 return;
295 entry->task_id = base::CancelableTaskTracker::kBadTaskId;
296 if (found_url && !row.title().empty()) {
297 entry->title = row.title();
298 if (observer_ && observable)
299 observer_->OnItemsChanged(static_cast<int>(entry_index), 1);
303 base::string16 CustomHomePagesTableModel::FormattedURL(int row) const {
304 std::string languages =
305 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages);
306 base::string16 url = net::FormatUrl(entries_[row].url, languages);
307 url = base::i18n::GetDisplayStringInLTRDirectionality(url);
308 return url;