| 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_factory.h" | 12 #include "chrome/browser/history/history_service_factory.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
| 15 #include "chrome/browser/ui/browser_iterator.h" | 15 #include "chrome/browser/ui/browser_iterator.h" |
| 16 #include "chrome/browser/ui/browser_list.h" | 16 #include "chrome/browser/ui/browser_list.h" |
| 17 #include "chrome/browser/ui/settings_window_manager.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 "chrome/grit/generated_resources.h" | 21 #include "chrome/grit/generated_resources.h" |
| 21 #include "components/history/core/browser/history_service.h" | 22 #include "components/history/core/browser/history_service.h" |
| 22 #include "components/url_formatter/url_formatter.h" | 23 #include "components/url_formatter/url_formatter.h" |
| 23 #include "content/public/browser/web_contents.h" | 24 #include "content/public/browser/web_contents.h" |
| 24 #include "ui/base/l10n/l10n_util.h" | 25 #include "ui/base/l10n/l10n_util.h" |
| 25 #include "ui/base/models/table_model_observer.h" | 26 #include "ui/base/models/table_model_observer.h" |
| 26 #include "ui/gfx/codec/png_codec.h" | 27 #include "ui/gfx/codec/png_codec.h" |
| 27 #include "url/gurl.h" | 28 #include "url/gurl.h" |
| 28 | 29 |
| 29 namespace { | 30 namespace { |
| 30 | 31 |
| 31 // Checks whether the given URL should count as one of the "current" pages. | 32 // 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 // Returns true for all pages except dev tools and settings. |
| 33 bool ShouldAddPage(const GURL& url) { | 34 bool ShouldAddPage(const GURL& url) { |
| 34 if (url.is_empty()) | 35 if (url.is_empty()) |
| 35 return false; | 36 return false; |
| 36 | 37 |
| 37 if (url.SchemeIs(content::kChromeDevToolsScheme)) | 38 if (url.SchemeIs(content::kChromeDevToolsScheme)) |
| 38 return false; | 39 return false; |
| 39 | 40 |
| 40 if (url.SchemeIs(content::kChromeUIScheme)) { | 41 if (url.SchemeIs(content::kChromeUIScheme)) { |
| 41 if (url.host() == chrome::kChromeUISettingsHost) | 42 if (url.host() == chrome::kChromeUISettingsHost || |
| 43 url.host() == chrome::kChromeUISettingsFrameHost) { |
| 42 return false; | 44 return false; |
| 45 } |
| 43 | 46 |
| 44 // For a settings page, the path will start with "/settings" not "settings" | 47 // For a settings page, the path will start with "/settings" not "settings" |
| 45 // so find() will return 1, not 0. | 48 // so find() will return 1, not 0. |
| 46 if (url.host() == chrome::kChromeUIUberHost && | 49 if (url.host() == chrome::kChromeUIUberHost && |
| 47 url.path().find(chrome::kChromeUISettingsHost) == 1) { | 50 url.path().find(chrome::kChromeUISettingsHost) == 1) { |
| 48 return false; | 51 return false; |
| 49 } | 52 } |
| 50 } | 53 } |
| 51 | 54 |
| 52 return true; | 55 return true; |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 RemoveWithoutNotification(index); | 177 RemoveWithoutNotification(index); |
| 175 if (observer_) | 178 if (observer_) |
| 176 observer_->OnItemsRemoved(index, 1); | 179 observer_->OnItemsRemoved(index, 1); |
| 177 } | 180 } |
| 178 | 181 |
| 179 void CustomHomePagesTableModel::SetToCurrentlyOpenPages() { | 182 void CustomHomePagesTableModel::SetToCurrentlyOpenPages() { |
| 180 // Remove the current entries. | 183 // Remove the current entries. |
| 181 while (RowCount()) | 184 while (RowCount()) |
| 182 RemoveWithoutNotification(0); | 185 RemoveWithoutNotification(0); |
| 183 | 186 |
| 184 // And add all tabs for all open browsers with our profile. | 187 // Add tabs from appropriate browser windows. |
| 185 int add_index = 0; | 188 int add_index = 0; |
| 186 for (chrome::BrowserIterator it; !it.done(); it.Next()) { | 189 for (chrome::BrowserIterator it; !it.done(); it.Next()) { |
| 187 Browser* browser = *it; | 190 Browser* browser = *it; |
| 188 if (browser->profile() != profile_) | 191 if (!ShouldIncludeBrowser(browser)) |
| 189 continue; // Skip incognito browsers. | 192 continue; |
| 190 | 193 |
| 191 for (int tab_index = 0; | 194 for (int tab_index = 0; |
| 192 tab_index < browser->tab_strip_model()->count(); | 195 tab_index < browser->tab_strip_model()->count(); |
| 193 ++tab_index) { | 196 ++tab_index) { |
| 194 const GURL url = | 197 const GURL url = |
| 195 browser->tab_strip_model()->GetWebContentsAt(tab_index)->GetURL(); | 198 browser->tab_strip_model()->GetWebContentsAt(tab_index)->GetURL(); |
| 196 if (ShouldAddPage(url)) | 199 if (ShouldAddPage(url)) |
| 197 AddWithoutNotification(add_index++, url); | 200 AddWithoutNotification(add_index++, url); |
| 198 } | 201 } |
| 199 } | 202 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 220 base::string16 CustomHomePagesTableModel::GetTooltip(int row) { | 223 base::string16 CustomHomePagesTableModel::GetTooltip(int row) { |
| 221 return entries_[row].title.empty() ? base::string16() : | 224 return entries_[row].title.empty() ? base::string16() : |
| 222 l10n_util::GetStringFUTF16(IDS_OPTIONS_STARTUP_PAGE_TOOLTIP, | 225 l10n_util::GetStringFUTF16(IDS_OPTIONS_STARTUP_PAGE_TOOLTIP, |
| 223 entries_[row].title, FormattedURL(row)); | 226 entries_[row].title, FormattedURL(row)); |
| 224 } | 227 } |
| 225 | 228 |
| 226 void CustomHomePagesTableModel::SetObserver(ui::TableModelObserver* observer) { | 229 void CustomHomePagesTableModel::SetObserver(ui::TableModelObserver* observer) { |
| 227 observer_ = observer; | 230 observer_ = observer; |
| 228 } | 231 } |
| 229 | 232 |
| 233 bool CustomHomePagesTableModel::ShouldIncludeBrowser(Browser* browser) { |
| 234 // Do not include incognito browsers. |
| 235 if (browser->profile() != profile_) |
| 236 return false; |
| 237 // Do not include the Settings window. |
| 238 if (chrome::SettingsWindowManager::GetInstance()->IsSettingsBrowser( |
| 239 browser)) { |
| 240 return false; |
| 241 } |
| 242 return true; |
| 243 } |
| 244 |
| 230 void CustomHomePagesTableModel::LoadTitle(Entry* entry) { | 245 void CustomHomePagesTableModel::LoadTitle(Entry* entry) { |
| 231 history::HistoryService* history_service = | 246 history::HistoryService* history_service = |
| 232 HistoryServiceFactory::GetForProfile(profile_, | 247 HistoryServiceFactory::GetForProfile(profile_, |
| 233 ServiceAccessType::EXPLICIT_ACCESS); | 248 ServiceAccessType::EXPLICIT_ACCESS); |
| 234 if (history_service) { | 249 if (history_service) { |
| 235 entry->task_id = history_service->QueryURL( | 250 entry->task_id = history_service->QueryURL( |
| 236 entry->url, | 251 entry->url, |
| 237 false, | 252 false, |
| 238 base::Bind(&CustomHomePagesTableModel::OnGotTitle, | 253 base::Bind(&CustomHomePagesTableModel::OnGotTitle, |
| 239 base::Unretained(this), | 254 base::Unretained(this), |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 } | 317 } |
| 303 } | 318 } |
| 304 | 319 |
| 305 base::string16 CustomHomePagesTableModel::FormattedURL(int row) const { | 320 base::string16 CustomHomePagesTableModel::FormattedURL(int row) const { |
| 306 std::string languages = | 321 std::string languages = |
| 307 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages); | 322 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages); |
| 308 base::string16 url = url_formatter::FormatUrl(entries_[row].url, languages); | 323 base::string16 url = url_formatter::FormatUrl(entries_[row].url, languages); |
| 309 url = base::i18n::GetDisplayStringInLTRDirectionality(url); | 324 url = base::i18n::GetDisplayStringInLTRDirectionality(url); |
| 310 return url; | 325 return url; |
| 311 } | 326 } |
| OLD | NEW |