diff options
author | mattm@chromium.org <mattm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-12 00:19:05 +0000 |
---|---|---|
committer | mattm@chromium.org <mattm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-12 00:19:05 +0000 |
commit | 7fff7901c3813f7847e0c46f5eff8a427e3ba6b3 (patch) | |
tree | 6139867a31596478126841cc056547b8a1b8968f /chrome | |
parent | d1a79af22766b274e904c997f05813b9bf7ef28d (diff) | |
download | chromium_src-7fff7901c3813f7847e0c46f5eff8a427e3ba6b3.zip chromium_src-7fff7901c3813f7847e0c46f5eff8a427e3ba6b3.tar.gz chromium_src-7fff7901c3813f7847e0c46f5eff8a427e3ba6b3.tar.bz2 |
Split CustomHomePagesTableModel into its own file so it can be shared with the Gtk UI.
BUG=43802
TEST=manual
Review URL: http://codereview.chromium.org/2061003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@46993 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/browser/custom_home_pages_table_model.cc | 197 | ||||
-rw-r--r-- | chrome/browser/custom_home_pages_table_model.h | 120 | ||||
-rw-r--r-- | chrome/browser/views/options/general_page_view.cc | 291 | ||||
-rw-r--r-- | chrome/chrome_browser.gypi | 2 |
4 files changed, 320 insertions, 290 deletions
diff --git a/chrome/browser/custom_home_pages_table_model.cc b/chrome/browser/custom_home_pages_table_model.cc new file mode 100644 index 0000000..7b4675f --- /dev/null +++ b/chrome/browser/custom_home_pages_table_model.cc @@ -0,0 +1,197 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/custom_home_pages_table_model.h" + +#include "app/l10n_util.h" +#include "app/resource_bundle.h" +#include "app/table_model_observer.h" +#include "base/i18n/rtl.h" +#include "chrome/browser/pref_service.h" +#include "chrome/browser/profile.h" +#include "chrome/common/pref_names.h" +#include "gfx/codec/png_codec.h" +#include "grit/app_resources.h" +#include "grit/generated_resources.h" +#include "net/base/net_util.h" + +// static +SkBitmap CustomHomePagesTableModel::default_favicon_; + +CustomHomePagesTableModel::CustomHomePagesTableModel(Profile* profile) + : profile_(profile), + observer_(NULL) { + InitClass(); +} + +void CustomHomePagesTableModel::SetURLs(const std::vector<GURL>& urls) { + entries_.resize(urls.size()); + for (size_t i = 0; i < urls.size(); ++i) { + entries_[i].url = urls[i]; + LoadTitleAndFavIcon(&(entries_[i])); + } + // Complete change, so tell the view to just rebuild itself. + if (observer_) + observer_->OnModelChanged(); +} + +void CustomHomePagesTableModel::Add(int index, const GURL& url) { + DCHECK(index >= 0 && index <= RowCount()); + entries_.insert(entries_.begin() + static_cast<size_t>(index), Entry()); + entries_[index].url = url; + if (observer_) + observer_->OnItemsAdded(index, 1); +} + +void CustomHomePagesTableModel::Remove(int index) { + DCHECK(index >= 0 && index < RowCount()); + Entry* entry = &(entries_[index]); + // Cancel any pending load requests now so we don't deref a bogus pointer when + // we get the loaded notification. + if (entry->title_handle) { + HistoryService* history_service = + profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); + if (history_service) + history_service->CancelRequest(entry->title_handle); + } + if (entry->fav_icon_handle) { + FaviconService* favicon_service = + profile_->GetFaviconService(Profile::EXPLICIT_ACCESS); + if (favicon_service) + favicon_service->CancelRequest(entry->fav_icon_handle); + } + entries_.erase(entries_.begin() + static_cast<size_t>(index)); + if (observer_) + observer_->OnItemsRemoved(index, 1); +} + +std::vector<GURL> CustomHomePagesTableModel::GetURLs() { + std::vector<GURL> urls(entries_.size()); + for (size_t i = 0; i < entries_.size(); ++i) + urls[i] = entries_[i].url; + return urls; +} + +int CustomHomePagesTableModel::RowCount() { + return static_cast<int>(entries_.size()); +} + +std::wstring CustomHomePagesTableModel::GetText(int row, int column_id) { + DCHECK(column_id == 0); + DCHECK(row >= 0 && row < RowCount()); + return entries_[row].title.empty() ? FormattedURL(row) : entries_[row].title; +} + +SkBitmap CustomHomePagesTableModel::GetIcon(int row) { + DCHECK(row >= 0 && row < RowCount()); + return entries_[row].icon.isNull() ? default_favicon_ : entries_[row].icon; +} + +std::wstring CustomHomePagesTableModel::GetTooltip(int row) { + return entries_[row].title.empty() ? std::wstring() : + l10n_util::GetStringF(IDS_OPTIONS_STARTUP_PAGE_TOOLTIP, + entries_[row].title, FormattedURL(row)); +} + +void CustomHomePagesTableModel::SetObserver(TableModelObserver* observer) { + observer_ = observer; +} + +void CustomHomePagesTableModel::InitClass() { + static bool initialized = false; + if (!initialized) { + ResourceBundle& rb = ResourceBundle::GetSharedInstance(); + default_favicon_ = *rb.GetBitmapNamed(IDR_DEFAULT_FAVICON); + initialized = true; + } +} + +void CustomHomePagesTableModel::LoadTitleAndFavIcon(Entry* entry) { + HistoryService* history_service = + profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); + if (history_service) { + entry->title_handle = history_service->QueryURL(entry->url, false, + &query_consumer_, + NewCallback(this, &CustomHomePagesTableModel::OnGotTitle)); + } + FaviconService* favicon_service = + profile_->GetFaviconService(Profile::EXPLICIT_ACCESS); + if (favicon_service) { + entry->fav_icon_handle = favicon_service->GetFaviconForURL(entry->url, + &query_consumer_, + NewCallback(this, &CustomHomePagesTableModel::OnGotFavIcon)); + } +} + +void CustomHomePagesTableModel::OnGotTitle(HistoryService::Handle handle, + bool found_url, + const history::URLRow* row, + history::VisitVector* visits) { + int entry_index; + Entry* entry = + GetEntryByLoadHandle(&Entry::title_handle, handle, &entry_index); + if (!entry) { + // The URLs changed before we were called back. + return; + } + entry->title_handle = 0; + if (found_url && !row->title().empty()) { + entry->title = row->title(); + if (observer_) + observer_->OnItemsChanged(static_cast<int>(entry_index), 1); + } +} + +void CustomHomePagesTableModel::OnGotFavIcon( + FaviconService::Handle handle, + bool know_fav_icon, + scoped_refptr<RefCountedMemory> image_data, + bool is_expired, + GURL icon_url) { + int entry_index; + Entry* entry = + GetEntryByLoadHandle(&Entry::fav_icon_handle, handle, &entry_index); + if (!entry) { + // The URLs changed before we were called back. + return; + } + entry->fav_icon_handle = 0; + if (know_fav_icon && image_data.get() && image_data->size()) { + int width, height; + std::vector<unsigned char> decoded_data; + if (gfx::PNGCodec::Decode(image_data->front(), + image_data->size(), + gfx::PNGCodec::FORMAT_BGRA, &decoded_data, + &width, &height)) { + entry->icon.setConfig(SkBitmap::kARGB_8888_Config, width, height); + entry->icon.allocPixels(); + memcpy(entry->icon.getPixels(), &decoded_data.front(), + width * height * 4); + if (observer_) + observer_->OnItemsChanged(static_cast<int>(entry_index), 1); + } + } +} + +CustomHomePagesTableModel::Entry* + CustomHomePagesTableModel::GetEntryByLoadHandle( + CancelableRequestProvider::Handle Entry::* member, + CancelableRequestProvider::Handle handle, + int* index) { + for (size_t i = 0; i < entries_.size(); ++i) { + if (entries_[i].*member == handle) { + *index = static_cast<int>(i); + return &entries_[i]; + } + } + return NULL; +} + +std::wstring CustomHomePagesTableModel::FormattedURL(int row) const { + std::wstring languages = + profile_->GetPrefs()->GetString(prefs::kAcceptLanguages); + std::wstring url(net::FormatUrl(entries_[row].url, languages)); + base::i18n::GetDisplayStringInLTRDirectionality(&url); + return url; +} diff --git a/chrome/browser/custom_home_pages_table_model.h b/chrome/browser/custom_home_pages_table_model.h new file mode 100644 index 0000000..dd2e1aa --- /dev/null +++ b/chrome/browser/custom_home_pages_table_model.h @@ -0,0 +1,120 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_CUSTOM_HOME_PAGES_TABLE_MODEL_H_ +#define CHROME_BROWSER_CUSTOM_HOME_PAGES_TABLE_MODEL_H_ + +#include <string> +#include <vector> + +#include "app/table_model.h" +#include "chrome/browser/history/history.h" +#include "chrome/browser/favicon_service.h" +#include "googleurl/src/gurl.h" +#include "third_party/skia/include/core/SkBitmap.h" + +class Profile; +class TableModelObserver; + +// CustomHomePagesTableModel is the model for the TableView showing the list +// of pages the user wants opened on startup. + +class CustomHomePagesTableModel : public TableModel { + public: + explicit CustomHomePagesTableModel(Profile* profile); + virtual ~CustomHomePagesTableModel() {} + + // Sets the set of urls that this model contains. + void SetURLs(const std::vector<GURL>& urls); + + // Adds an entry at the specified index. + void Add(int index, const GURL& url); + + // Removes the entry at the specified index. + void Remove(int index); + + // Returns the set of urls this model contains. + std::vector<GURL> GetURLs(); + + // TableModel overrides: + virtual int RowCount(); + virtual std::wstring GetText(int row, int column_id); + virtual SkBitmap GetIcon(int row); + virtual std::wstring GetTooltip(int row); + virtual void SetObserver(TableModelObserver* observer); + + private: + // Each item in the model is represented as an Entry. Entry stores the URL, + // title, and favicon of the page. + struct Entry { + Entry() : title_handle(0), fav_icon_handle(0) {} + + // URL of the page. + GURL url; + + // Page title. If this is empty, we'll display the URL as the entry. + std::wstring title; + + // Icon for the page. + SkBitmap icon; + + // If non-zero, indicates we're loading the title for the page. + HistoryService::Handle title_handle; + + // If non-zero, indicates we're loading the favicon for the page. + FaviconService::Handle fav_icon_handle; + }; + + static void InitClass(); + + // Loads the title and favicon for the specified entry. + void LoadTitleAndFavIcon(Entry* entry); + + // Callback from history service. Updates the title of the Entry whose + // |title_handle| matches |handle| and notifies the observer of the change. + void OnGotTitle(HistoryService::Handle handle, + bool found_url, + const history::URLRow* row, + history::VisitVector* visits); + + // Callback from history service. Updates the icon of the Entry whose + // |fav_icon_handle| matches |handle| and notifies the observer of the change. + void OnGotFavIcon(FaviconService::Handle handle, + bool know_fav_icon, + scoped_refptr<RefCountedMemory> image_data, + bool is_expired, + GURL icon_url); + + // Returns the entry whose |member| matches |handle| and sets |entry_index| to + // the index of the entry. + Entry* GetEntryByLoadHandle(CancelableRequestProvider::Handle Entry::* member, + CancelableRequestProvider::Handle handle, + int* entry_index); + + // Returns the entry whose |fav_icon_handle| matches |handle| and sets + // |entry_index| to the index of the entry. + Entry* GetEntryByFavIconHandle(FaviconService::Handle handle, + int* entry_index); + + // Returns the URL for a particular row, formatted for display to the user. + std::wstring FormattedURL(int row) const; + + // Set of entries we're showing. + std::vector<Entry> entries_; + + // Default icon to show when one can't be found for the URL. + static SkBitmap default_favicon_; + + // Profile used to load titles and icons. + Profile* profile_; + + TableModelObserver* observer_; + + // Used in loading titles and favicons. + CancelableRequestConsumer query_consumer_; + + DISALLOW_COPY_AND_ASSIGN(CustomHomePagesTableModel); +}; + +#endif // CHROME_BROWSER_CUSTOM_HOME_PAGES_TABLE_MODEL_H_ diff --git a/chrome/browser/views/options/general_page_view.cc b/chrome/browser/views/options/general_page_view.cc index d1db4ed..b287d81 100644 --- a/chrome/browser/views/options/general_page_view.cc +++ b/chrome/browser/views/options/general_page_view.cc @@ -6,15 +6,13 @@ #include "app/combobox_model.h" #include "app/l10n_util.h" -#include "app/resource_bundle.h" #include "base/callback.h" #include "base/message_loop.h" #include "base/string_util.h" #include "chrome/browser/browser.h" #include "chrome/browser/browser_list.h" +#include "chrome/browser/custom_home_pages_table_model.h" #include "chrome/browser/dom_ui/new_tab_ui.h" -#include "chrome/browser/favicon_service.h" -#include "chrome/browser/history/history.h" #include "chrome/browser/net/url_fixer_upper.h" #include "chrome/browser/pref_service.h" #include "chrome/browser/profile.h" @@ -28,12 +26,8 @@ #include "chrome/common/pref_names.h" #include "chrome/common/url_constants.h" #include "chrome/installer/util/browser_distribution.h" -#include "gfx/codec/png_codec.h" -#include "grit/app_resources.h" #include "grit/chromium_strings.h" #include "grit/generated_resources.h" -#include "net/base/net_util.h" -#include "third_party/skia/include/core/SkBitmap.h" #include "views/controls/button/radio_button.h" #include "views/controls/label.h" #include "views/controls/table/table_view.h" @@ -54,289 +48,6 @@ std::wstring GetNewTabUIURLString() { } /////////////////////////////////////////////////////////////////////////////// -// CustomHomePagesTableModel - -// CustomHomePagesTableModel is the model for the TableView showing the list -// of pages the user wants opened on startup. - -class CustomHomePagesTableModel : public TableModel { - public: - explicit CustomHomePagesTableModel(Profile* profile); - virtual ~CustomHomePagesTableModel() {} - - // Sets the set of urls that this model contains. - void SetURLs(const std::vector<GURL>& urls); - - // Adds an entry at the specified index. - void Add(int index, const GURL& url); - - // Removes the entry at the specified index. - void Remove(int index); - - // Returns the set of urls this model contains. - std::vector<GURL> GetURLs(); - - // TableModel overrides: - virtual int RowCount(); - virtual std::wstring GetText(int row, int column_id); - virtual SkBitmap GetIcon(int row); - virtual std::wstring GetTooltip(int row); - virtual void SetObserver(TableModelObserver* observer); - - private: - // Each item in the model is represented as an Entry. Entry stores the URL, - // title, and favicon of the page. - struct Entry { - Entry() : title_handle(0), fav_icon_handle(0) {} - - // URL of the page. - GURL url; - - // Page title. If this is empty, we'll display the URL as the entry. - std::wstring title; - - // Icon for the page. - SkBitmap icon; - - // If non-zero, indicates we're loading the title for the page. - HistoryService::Handle title_handle; - - // If non-zero, indicates we're loading the favicon for the page. - FaviconService::Handle fav_icon_handle; - }; - - static void InitClass(); - - // Loads the title and favicon for the specified entry. - void LoadTitleAndFavIcon(Entry* entry); - - // Callback from history service. Updates the title of the Entry whose - // |title_handle| matches |handle| and notifies the observer of the change. - void OnGotTitle(HistoryService::Handle handle, - bool found_url, - const history::URLRow* row, - history::VisitVector* visits); - - // Callback from history service. Updates the icon of the Entry whose - // |fav_icon_handle| matches |handle| and notifies the observer of the change. - void OnGotFavIcon(FaviconService::Handle handle, - bool know_fav_icon, - scoped_refptr<RefCountedMemory> image_data, - bool is_expired, - GURL icon_url); - - // Returns the entry whose |member| matches |handle| and sets |entry_index| to - // the index of the entry. - Entry* GetEntryByLoadHandle(CancelableRequestProvider::Handle Entry::* member, - CancelableRequestProvider::Handle handle, - int* entry_index); - - // Returns the entry whose |fav_icon_handle| matches |handle| and sets - // |entry_index| to the index of the entry. - Entry* GetEntryByFavIconHandle(FaviconService::Handle handle, - int* entry_index); - - // Returns the URL for a particular row, formatted for display to the user. - std::wstring FormattedURL(int row) const; - - // Set of entries we're showing. - std::vector<Entry> entries_; - - // Default icon to show when one can't be found for the URL. - static SkBitmap default_favicon_; - - // Profile used to load titles and icons. - Profile* profile_; - - TableModelObserver* observer_; - - // Used in loading titles and favicons. - CancelableRequestConsumer query_consumer_; - - DISALLOW_COPY_AND_ASSIGN(CustomHomePagesTableModel); -}; - -// static -SkBitmap CustomHomePagesTableModel::default_favicon_; - -CustomHomePagesTableModel::CustomHomePagesTableModel(Profile* profile) - : profile_(profile), - observer_(NULL) { - InitClass(); -} - -void CustomHomePagesTableModel::SetURLs(const std::vector<GURL>& urls) { - entries_.resize(urls.size()); - for (size_t i = 0; i < urls.size(); ++i) { - entries_[i].url = urls[i]; - LoadTitleAndFavIcon(&(entries_[i])); - } - // Complete change, so tell the view to just rebuild itself. - if (observer_) - observer_->OnModelChanged(); -} - -void CustomHomePagesTableModel::Add(int index, const GURL& url) { - DCHECK(index >= 0 && index <= RowCount()); - entries_.insert(entries_.begin() + static_cast<size_t>(index), Entry()); - entries_[index].url = url; - if (observer_) - observer_->OnItemsAdded(index, 1); -} - -void CustomHomePagesTableModel::Remove(int index) { - DCHECK(index >= 0 && index < RowCount()); - Entry* entry = &(entries_[index]); - // Cancel any pending load requests now so we don't deref a bogus pointer when - // we get the loaded notification. - if (entry->title_handle) { - HistoryService* history_service = - profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); - if (history_service) - history_service->CancelRequest(entry->title_handle); - } - if (entry->fav_icon_handle) { - FaviconService* favicon_service = - profile_->GetFaviconService(Profile::EXPLICIT_ACCESS); - if (favicon_service) - favicon_service->CancelRequest(entry->fav_icon_handle); - } - entries_.erase(entries_.begin() + static_cast<size_t>(index)); - if (observer_) - observer_->OnItemsRemoved(index, 1); -} - -std::vector<GURL> CustomHomePagesTableModel::GetURLs() { - std::vector<GURL> urls(entries_.size()); - for (size_t i = 0; i < entries_.size(); ++i) - urls[i] = entries_[i].url; - return urls; -} - -int CustomHomePagesTableModel::RowCount() { - return static_cast<int>(entries_.size()); -} - -std::wstring CustomHomePagesTableModel::GetText(int row, int column_id) { - DCHECK(column_id == 0); - DCHECK(row >= 0 && row < RowCount()); - return entries_[row].title.empty() ? FormattedURL(row) : entries_[row].title; -} - -SkBitmap CustomHomePagesTableModel::GetIcon(int row) { - DCHECK(row >= 0 && row < RowCount()); - return entries_[row].icon.isNull() ? default_favicon_ : entries_[row].icon; -} - -std::wstring CustomHomePagesTableModel::GetTooltip(int row) { - return entries_[row].title.empty() ? std::wstring() : - l10n_util::GetStringF(IDS_OPTIONS_STARTUP_PAGE_TOOLTIP, - entries_[row].title, FormattedURL(row)); -} - -void CustomHomePagesTableModel::SetObserver(TableModelObserver* observer) { - observer_ = observer; -} - -void CustomHomePagesTableModel::InitClass() { - static bool initialized = false; - if (!initialized) { - ResourceBundle& rb = ResourceBundle::GetSharedInstance(); - default_favicon_ = *rb.GetBitmapNamed(IDR_DEFAULT_FAVICON); - initialized = true; - } -} - -void CustomHomePagesTableModel::LoadTitleAndFavIcon(Entry* entry) { - HistoryService* history_service = - profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); - if (history_service) { - entry->title_handle = history_service->QueryURL(entry->url, false, - &query_consumer_, - NewCallback(this, &CustomHomePagesTableModel::OnGotTitle)); - } - FaviconService* favicon_service = - profile_->GetFaviconService(Profile::EXPLICIT_ACCESS); - if (favicon_service) { - entry->fav_icon_handle = favicon_service->GetFaviconForURL(entry->url, - &query_consumer_, - NewCallback(this, &CustomHomePagesTableModel::OnGotFavIcon)); - } -} - -void CustomHomePagesTableModel::OnGotTitle(HistoryService::Handle handle, - bool found_url, - const history::URLRow* row, - history::VisitVector* visits) { - int entry_index; - Entry* entry = - GetEntryByLoadHandle(&Entry::title_handle, handle, &entry_index); - if (!entry) { - // The URLs changed before we were called back. - return; - } - entry->title_handle = 0; - if (found_url && !row->title().empty()) { - entry->title = row->title(); - if (observer_) - observer_->OnItemsChanged(static_cast<int>(entry_index), 1); - } -} - -void CustomHomePagesTableModel::OnGotFavIcon( - FaviconService::Handle handle, - bool know_fav_icon, - scoped_refptr<RefCountedMemory> image_data, - bool is_expired, - GURL icon_url) { - int entry_index; - Entry* entry = - GetEntryByLoadHandle(&Entry::fav_icon_handle, handle, &entry_index); - if (!entry) { - // The URLs changed before we were called back. - return; - } - entry->fav_icon_handle = 0; - if (know_fav_icon && image_data.get() && image_data->size()) { - int width, height; - std::vector<unsigned char> decoded_data; - if (gfx::PNGCodec::Decode(image_data->front(), - image_data->size(), - gfx::PNGCodec::FORMAT_BGRA, &decoded_data, - &width, &height)) { - entry->icon.setConfig(SkBitmap::kARGB_8888_Config, width, height); - entry->icon.allocPixels(); - memcpy(entry->icon.getPixels(), &decoded_data.front(), - width * height * 4); - if (observer_) - observer_->OnItemsChanged(static_cast<int>(entry_index), 1); - } - } -} - -CustomHomePagesTableModel::Entry* - CustomHomePagesTableModel::GetEntryByLoadHandle( - CancelableRequestProvider::Handle Entry::* member, - CancelableRequestProvider::Handle handle, - int* index) { - for (size_t i = 0; i < entries_.size(); ++i) { - if (entries_[i].*member == handle) { - *index = static_cast<int>(i); - return &entries_[i]; - } - } - return NULL; -} - -std::wstring CustomHomePagesTableModel::FormattedURL(int row) const { - std::wstring languages = - profile_->GetPrefs()->GetString(prefs::kAcceptLanguages); - std::wstring url(net::FormatUrl(entries_[row].url, languages)); - base::i18n::GetDisplayStringInLTRDirectionality(&url); - return url; -} - -/////////////////////////////////////////////////////////////////////////////// // SearchEngineListModel class SearchEngineListModel : public ComboboxModel, diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index d2a213e..2252892 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -862,6 +862,8 @@ 'browser/cookies_tree_model.h', 'browser/cross_site_request_manager.cc', 'browser/cross_site_request_manager.h', + 'browser/custom_home_pages_table_model.cc', + 'browser/custom_home_pages_table_model.h', 'browser/defaults.cc', 'browser/defaults.h', 'browser/diagnostics/diagnostics_main.cc', |