diff options
author | mattm@chromium.org <mattm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-22 19:39:21 +0000 |
---|---|---|
committer | mattm@chromium.org <mattm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-22 19:39:21 +0000 |
commit | 41cf8020fe10d9949772f58af8504d768edd33a4 (patch) | |
tree | a9a291307dfe802c0aeddf7c5434102778d27427 | |
parent | c535a312073f7fc4eaed7e5efa3d0ef3df2e1dce (diff) | |
download | chromium_src-41cf8020fe10d9949772f58af8504d768edd33a4.zip chromium_src-41cf8020fe10d9949772f58af8504d768edd33a4.tar.gz chromium_src-41cf8020fe10d9949772f58af8504d768edd33a4.tar.bz2 |
Gtk cookie manager part 1.
(Doesn't display cookie details, otherwise working.)
BUG=11507
TEST=All cookie manager functions should work as expected, other than viewing the cookie details.
Review URL: http://codereview.chromium.org/159187
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@21307 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/cookies_table_model.cc | 185 | ||||
-rw-r--r-- | chrome/browser/cookies_table_model.h | 58 | ||||
-rw-r--r-- | chrome/browser/gtk/options/advanced_contents_gtk.cc | 39 | ||||
-rw-r--r-- | chrome/browser/gtk/options/cookies_view.cc | 395 | ||||
-rw-r--r-- | chrome/browser/gtk/options/cookies_view.h | 103 | ||||
-rw-r--r-- | chrome/browser/views/options/cookies_view.cc | 220 | ||||
-rw-r--r-- | chrome/chrome.gyp | 4 |
7 files changed, 776 insertions, 228 deletions
diff --git a/chrome/browser/cookies_table_model.cc b/chrome/browser/cookies_table_model.cc new file mode 100644 index 0000000..8aab8e2 --- /dev/null +++ b/chrome/browser/cookies_table_model.cc @@ -0,0 +1,185 @@ +// Copyright (c) 2009 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/cookies_table_model.h" + +#include "app/l10n_util.h" +#include "app/table_model_observer.h" +#include "app/resource_bundle.h" +#include "base/string_util.h" +#include "chrome/browser/profile.h" +#include "grit/generated_resources.h" +#include "grit/theme_resources.h" +#include "net/url_request/url_request_context.h" +#include "third_party/skia/include/core/SkBitmap.h" + +/////////////////////////////////////////////////////////////////////////////// +// CookiesTableModel, public: + +CookiesTableModel::CookiesTableModel(Profile* profile) + : profile_(profile) { + LoadCookies(); +} + +std::string CookiesTableModel::GetDomainAt(int index) { + DCHECK(index >= 0 && index < RowCount()); + return shown_cookies_.at(index)->first; +} + +net::CookieMonster::CanonicalCookie& CookiesTableModel::GetCookieAt( + int index) { + DCHECK(index >= 0 && index < RowCount()); + return shown_cookies_.at(index)->second; +} + +void CookiesTableModel::RemoveCookies(int start_index, int remove_count) { + if (remove_count <= 0) { + NOTREACHED(); + return; + } + + net::CookieMonster* monster = profile_->GetRequestContext()->cookie_store(); + + // We need to update the searched results list, the full cookie list, + // and the view. We walk through the search results list (which is what + // is displayed) and map these back to the full cookie list. They should + // be in the same sort order, and always exist, so we can just walk once. + // We can't delete any entries from all_cookies_ without invaliding all of + // our pointers after it (which are in shown_cookies), so we go backwards. + CookiePtrList::iterator first = shown_cookies_.begin() + start_index; + CookiePtrList::iterator last = first + remove_count; + CookieList::iterator all_it = all_cookies_.end(); + while (last != first) { + --last; + --all_it; + // Seek to the corresponding entry in all_cookies_ + while (&*all_it != *last) --all_it; + // Delete the cookie from the monster + monster->DeleteCookie(all_it->first, all_it->second, true); + all_it = all_cookies_.erase(all_it); + } + + // By deleting entries from all_cookies, we just possibly moved stuff around + // and have thus invalidated all of our pointers, so rebuild shown_cookies. + // We could do this all better if there was a way to mark elements of + // all_cookies as dead instead of deleting, but this should be fine for now. + DoFilter(); + if (observer_) + observer_->OnItemsRemoved(start_index, remove_count); +} + +void CookiesTableModel::RemoveAllShownCookies() { + RemoveCookies(0, RowCount()); +} + +/////////////////////////////////////////////////////////////////////////////// +// CookiesTableModel, TableModel implementation: + +int CookiesTableModel::RowCount() { + return static_cast<int>(shown_cookies_.size()); +} + +std::wstring CookiesTableModel::GetText(int row, int column_id) { + DCHECK(row >= 0 && row < RowCount()); + switch (column_id) { + case IDS_COOKIES_DOMAIN_COLUMN_HEADER: + { + // Domain cookies start with a trailing dot, but we will show this + // in the cookie details, show it without the dot in the list. + std::string& domain = shown_cookies_.at(row)->first; + std::wstring wide_domain; + if (!domain.empty() && domain[0] == '.') + wide_domain = UTF8ToWide(domain.substr(1)); + else + wide_domain = UTF8ToWide(domain); + // Force domain to be LTR + if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT) + l10n_util::WrapStringWithLTRFormatting(&wide_domain); + return wide_domain; + } + break; + case IDS_COOKIES_NAME_COLUMN_HEADER: { + std::wstring name = UTF8ToWide(shown_cookies_.at(row)->second.Name()); + l10n_util::AdjustStringForLocaleDirection(name, &name); + return name; + break; + } + } + NOTREACHED(); + return L""; +} + +SkBitmap CookiesTableModel::GetIcon(int row) { + static SkBitmap* icon = ResourceBundle::GetSharedInstance().GetBitmapNamed( + IDR_COOKIE_ICON); + return *icon; +} + +void CookiesTableModel::SetObserver(TableModelObserver* observer) { + observer_ = observer; +} + +int CookiesTableModel::CompareValues(int row1, int row2, int column_id) { + if (column_id == IDS_COOKIES_DOMAIN_COLUMN_HEADER) { + // Sort ignore the '.' prefix for domain cookies. + net::CookieMonster::CookieListPair* cp1 = shown_cookies_[row1]; + net::CookieMonster::CookieListPair* cp2 = shown_cookies_[row2]; + bool is1domain = !cp1->first.empty() && cp1->first[0] == '.'; + bool is2domain = !cp2->first.empty() && cp2->first[0] == '.'; + + // They are both either domain or host cookies, sort them normally. + if (is1domain == is2domain) + return cp1->first.compare(cp2->first); + + // One (but only one) is a domain cookie, skip the beginning '.'. + return is1domain ? + cp1->first.compare(1, cp1->first.length() - 1, cp2->first) : + -cp2->first.compare(1, cp2->first.length() - 1, cp1->first); + } + return TableModel::CompareValues(row1, row2, column_id); +} + +/////////////////////////////////////////////////////////////////////////////// +// CookiesTableModel, private: + +// Returns true if |cookie| matches the specified filter, where "match" is +// defined as the cookie's domain, name and value contains filter text +// somewhere. +static bool ContainsFilterText( + const std::string& domain, + const net::CookieMonster::CanonicalCookie& cookie, + const std::string& filter) { + return domain.find(filter) != std::string::npos || + cookie.Name().find(filter) != std::string::npos || + cookie.Value().find(filter) != std::string::npos; +} + +void CookiesTableModel::LoadCookies() { + // mmargh mmargh mmargh! + net::CookieMonster* cookie_monster = + profile_->GetRequestContext()->cookie_store(); + all_cookies_ = cookie_monster->GetAllCookies(); + DoFilter(); +} + +void CookiesTableModel::DoFilter() { + std::string utf8_filter = WideToUTF8(filter_); + bool has_filter = !utf8_filter.empty(); + + shown_cookies_.clear(); + + CookieList::iterator iter = all_cookies_.begin(); + for (; iter != all_cookies_.end(); ++iter) { + if (!has_filter || + ContainsFilterText(iter->first, iter->second, utf8_filter)) { + shown_cookies_.push_back(&*iter); + } + } +} + +void CookiesTableModel::UpdateSearchResults(const std::wstring& filter) { + filter_ = filter; + DoFilter(); + observer_->OnModelChanged(); +} diff --git a/chrome/browser/cookies_table_model.h b/chrome/browser/cookies_table_model.h new file mode 100644 index 0000000..2b69849 --- /dev/null +++ b/chrome/browser/cookies_table_model.h @@ -0,0 +1,58 @@ +// Copyright (c) 2009 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_COOKIES_TABLE_MODEL_H_ +#define CHROME_BROWSER_COOKIES_TABLE_MODEL_H_ + +#include <string> +#include <vector> + +#include "app/table_model.h" +#include "net/base/cookie_monster.h" + +class Profile; + +class CookiesTableModel : public TableModel { + public: + explicit CookiesTableModel(Profile* profile); + virtual ~CookiesTableModel() {} + + // Returns information about the Cookie at the specified index. + std::string GetDomainAt(int index); + net::CookieMonster::CanonicalCookie& GetCookieAt(int index); + + // Remove the specified cookies from the Cookie Monster and update the view. + void RemoveCookies(int start_index, int remove_count); + void RemoveAllShownCookies(); + + // TableModel methods. + virtual int RowCount(); + virtual std::wstring GetText(int row, int column_id); + virtual SkBitmap GetIcon(int row); + virtual int CompareValues(int row1, int row2, int column_id); + virtual void SetObserver(TableModelObserver* observer); + + // Filter the cookies to only display matched results. + void UpdateSearchResults(const std::wstring& filter); + + private: + void LoadCookies(); + void DoFilter(); + + std::wstring filter_; + + // The profile from which this model sources cookies. + Profile* profile_; + + typedef net::CookieMonster::CookieList CookieList; + typedef std::vector<net::CookieMonster::CookieListPair*> CookiePtrList; + CookieList all_cookies_; + CookiePtrList shown_cookies_; + + TableModelObserver* observer_; + + DISALLOW_COPY_AND_ASSIGN(CookiesTableModel); +}; + +#endif // CHROME_BROWSER_COOKIES_TABLE_MODEL_H_ diff --git a/chrome/browser/gtk/options/advanced_contents_gtk.cc b/chrome/browser/gtk/options/advanced_contents_gtk.cc index 5cf7141..7aa21b28 100644 --- a/chrome/browser/gtk/options/advanced_contents_gtk.cc +++ b/chrome/browser/gtk/options/advanced_contents_gtk.cc @@ -17,6 +17,7 @@ #include "chrome/browser/download/download_manager.h" #include "chrome/browser/fonts_languages_window.h" #include "chrome/browser/gtk/gtk_chrome_link_button.h" +#include "chrome/browser/gtk/options/cookies_view.h" #include "chrome/browser/gtk/options/options_layout_gtk.h" #include "chrome/browser/net/dns_global.h" #include "chrome/browser/options_page_base.h" @@ -360,6 +361,8 @@ class PrivacySection : public OptionsPageBase { PrivacySection* options_window); static void OnCookieBehaviorChanged(GtkComboBox* combo_box, PrivacySection* privacy_section); + static void OnShowCookiesButtonClicked(GtkButton *button, + PrivacySection* privacy_section); // The widget containing the options for this section. GtkWidget* page_; @@ -448,6 +451,16 @@ PrivacySection::PrivacySection(Profile* profile) G_CALLBACK(OnLoggingChange), this); #endif + GtkWidget* cookie_description_label = gtk_label_new( + l10n_util::GetStringUTF8(IDS_OPTIONS_COOKIES_ACCEPT_LABEL).c_str()); + gtk_misc_set_alignment(GTK_MISC(cookie_description_label), 0, 0); + gtk_box_pack_start(GTK_BOX(page_), cookie_description_label, FALSE, FALSE, 0); + + GtkWidget* cookie_controls = gtk_vbox_new(FALSE, gtk_util::kControlSpacing); + gtk_box_pack_start(GTK_BOX(page_), + OptionsLayoutBuilderGtk::IndentWidget(cookie_controls), + FALSE, FALSE, 0); + cookie_behavior_combobox_ = gtk_combo_box_new_text(); gtk_combo_box_append_text( GTK_COMBO_BOX(cookie_behavior_combobox_), @@ -461,17 +474,18 @@ PrivacySection::PrivacySection(Profile* profile) l10n_util::GetStringUTF8(IDS_OPTIONS_COOKIES_BLOCK_ALL_COOKIES).c_str()); g_signal_connect(G_OBJECT(cookie_behavior_combobox_), "changed", G_CALLBACK(OnCookieBehaviorChanged), this); + gtk_box_pack_start(GTK_BOX(cookie_controls), cookie_behavior_combobox_, + FALSE, FALSE, 0); - GtkWidget* cookie_controls = gtk_util::CreateLabeledControlsGroup(NULL, - l10n_util::GetStringUTF8(IDS_OPTIONS_COOKIES_ACCEPT_LABEL).c_str(), - cookie_behavior_combobox_, - NULL); - gtk_box_pack_start(GTK_BOX(page_), cookie_controls, FALSE, FALSE, 0); - - // TODO(mattm): show cookies button - gtk_box_pack_start(GTK_BOX(page_), - gtk_label_new("TODO rest of the privacy options"), + GtkWidget* show_cookies_button = gtk_button_new_with_label( + l10n_util::GetStringUTF8(IDS_OPTIONS_COOKIES_SHOWCOOKIES).c_str()); + g_signal_connect(show_cookies_button, "clicked", + G_CALLBACK(OnShowCookiesButtonClicked), this); + // Stick it in an hbox so it doesn't expand to the whole width. + GtkWidget* button_hbox = gtk_hbox_new(FALSE, 0); + gtk_box_pack_start(GTK_BOX(button_hbox), show_cookies_button, FALSE, FALSE, 0); + gtk_box_pack_start(GTK_BOX(cookie_controls), button_hbox, FALSE, FALSE, 0); // Init member prefs so we can update the controls if prefs change. alternate_error_pages_.Init(prefs::kAlternateErrorPagesEnabled, @@ -604,6 +618,13 @@ void PrivacySection::OnCookieBehaviorChanged(GtkComboBox* combo_box, privacy_section->cookie_behavior_.SetValue(cookie_policy); } +// static +void PrivacySection::OnShowCookiesButtonClicked( + GtkButton *button, PrivacySection* privacy_section) { + privacy_section->UserMetricsRecordAction(L"Options_ShowCookies", NULL); + CookiesView::Show(privacy_section->profile()); +} + void PrivacySection::NotifyPrefChanged(const std::wstring* pref_name) { initializing_ = true; if (!pref_name || *pref_name == prefs::kAlternateErrorPagesEnabled) { diff --git a/chrome/browser/gtk/options/cookies_view.cc b/chrome/browser/gtk/options/cookies_view.cc new file mode 100644 index 0000000..a43ac18 --- /dev/null +++ b/chrome/browser/gtk/options/cookies_view.cc @@ -0,0 +1,395 @@ +// Copyright (c) 2009 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/gtk/options/cookies_view.h" + +#include "app/l10n_util.h" +#include "base/gfx/gtk_util.h" +#include "base/message_loop.h" +#include "base/string_util.h" +#include "chrome/browser/cookies_table_model.h" +#include "chrome/common/gtk_util.h" +#include "grit/generated_resources.h" +#include "third_party/skia/include/core/SkBitmap.h" + +namespace { + +// Initial size for dialog. +const int kDialogDefaultWidth = 450; +const int kDialogDefaultHeight = 450; + +// Delay after entering filter text before filtering occurs. +const int kSearchFilterDelayMs = 500; + +// Response ids for our custom buttons. +enum { + RESPONSE_REMOVE = 1, + RESPONSE_REMOVE_ALL +}; + +// Column ids for |list_store_|. +enum { + COL_ICON, + COL_SITE, + COL_COOKIE_NAME, + COL_COUNT, +}; + +// The currently open cookie manager, if any. +CookiesView* instance_ = NULL; + +// TODO(mattm): These functions are also in url_picker_dialog_gtk. Move them to +// some sort of gtk table model helper? +// Get the row number corresponding to |path|. +gint GetRowNumForPath(GtkTreePath* path) { + gint* indices = gtk_tree_path_get_indices(path); + if (!indices) { + NOTREACHED(); + return -1; + } + return indices[0]; +} + +// Get the row number corresponding to |iter|. +gint GetRowNumForIter(GtkTreeModel* model, GtkTreeIter* iter) { + GtkTreePath* path = gtk_tree_model_get_path(model, iter); + int row = GetRowNumForPath(path); + gtk_tree_path_free(path); + return row; +} + +// Get the row number in the child tree model corresponding to |sort_path| in +// the parent tree model. +gint GetTreeSortChildRowNumForPath(GtkTreeModel* sort_model, + GtkTreePath* sort_path) { + GtkTreePath *child_path = gtk_tree_model_sort_convert_path_to_child_path( + GTK_TREE_MODEL_SORT(sort_model), sort_path); + int row = GetRowNumForPath(child_path); + gtk_tree_path_free(child_path); + return row; +} + +} // namespace + +CookiesView::~CookiesView() { +} + +// static +void CookiesView::Show(Profile* profile) { + DCHECK(profile); + + // If there's already an existing editor window, activate it. + if (instance_) { + gtk_window_present(GTK_WINDOW(instance_->dialog_)); + } else { + instance_ = new CookiesView(profile); + } +} + +CookiesView::CookiesView(Profile* profile) + : profile_(profile), + filter_update_factory_(this) { + Init(); +} + +void CookiesView::Init() { + dialog_ = gtk_dialog_new_with_buttons( + l10n_util::GetStringUTF8(IDS_COOKIES_WINDOW_TITLE).c_str(), + NULL, + GTK_DIALOG_NO_SEPARATOR, + GTK_STOCK_CLOSE, + GTK_RESPONSE_CLOSE, + NULL); + + remove_button_ = gtk_dialog_add_button( + GTK_DIALOG(dialog_), + gtk_util::ConvertAcceleratorsFromWindowsStyle( + l10n_util::GetStringUTF8(IDS_COOKIES_REMOVE_LABEL)).c_str(), + RESPONSE_REMOVE); + gtk_button_box_set_child_secondary( + GTK_BUTTON_BOX(GTK_DIALOG(dialog_)->action_area), + remove_button_, + TRUE); + + remove_all_button_ = gtk_dialog_add_button( + GTK_DIALOG(dialog_), + gtk_util::ConvertAcceleratorsFromWindowsStyle( + l10n_util::GetStringUTF8(IDS_COOKIES_REMOVE_ALL_LABEL)).c_str(), + RESPONSE_REMOVE_ALL); + gtk_button_box_set_child_secondary( + GTK_BUTTON_BOX(GTK_DIALOG(dialog_)->action_area), + remove_all_button_, + TRUE); + + gtk_dialog_set_default_response(GTK_DIALOG(dialog_), GTK_RESPONSE_CLOSE); + gtk_window_set_default_size(GTK_WINDOW(dialog_), kDialogDefaultWidth, + kDialogDefaultHeight); + gtk_box_set_spacing(GTK_BOX(GTK_DIALOG(dialog_)->vbox), + gtk_util::kContentAreaSpacing); + g_signal_connect(dialog_, "response", G_CALLBACK(OnResponse), this); + g_signal_connect(dialog_, "destroy", G_CALLBACK(OnWindowDestroy), this); + + // Filtering controls. + GtkWidget* filter_hbox = gtk_hbox_new(FALSE, gtk_util::kControlSpacing); + filter_entry_ = gtk_entry_new(); + g_signal_connect(G_OBJECT(filter_entry_), "activate", + G_CALLBACK(OnFilterEntryActivated), this); + g_signal_connect(G_OBJECT(filter_entry_), "changed", + G_CALLBACK(OnFilterEntryChanged), this); + gtk_box_pack_start(GTK_BOX(filter_hbox), filter_entry_, + TRUE, TRUE, 0); + filter_clear_button_ = gtk_button_new_with_mnemonic( + gtk_util::ConvertAcceleratorsFromWindowsStyle( + l10n_util::GetStringUTF8(IDS_COOKIES_CLEAR_SEARCH_LABEL)).c_str()); + g_signal_connect(G_OBJECT(filter_clear_button_), "clicked", + G_CALLBACK(OnFilterClearButtonClicked), this); + gtk_box_pack_start(GTK_BOX(filter_hbox), filter_clear_button_, + FALSE, FALSE, 0); + + GtkWidget* filter_controls = gtk_util::CreateLabeledControlsGroup(NULL, + l10n_util::GetStringUTF8(IDS_COOKIES_SEARCH_LABEL).c_str(), filter_hbox, + NULL); + gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog_)->vbox), filter_controls, + FALSE, FALSE, 0); + + // Cookie list. + GtkWidget* cookie_list_vbox = gtk_vbox_new(FALSE, gtk_util::kControlSpacing); + gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog_)->vbox), cookie_list_vbox, + TRUE, TRUE, 0); + + GtkWidget* description_label = gtk_label_new( + l10n_util::GetStringUTF8(IDS_COOKIES_INFO_LABEL).c_str()); + GtkWidget* description_label_alignment = gtk_alignment_new( + 0.0, 0.5, 0.0, 0.0); + gtk_container_add(GTK_CONTAINER(description_label_alignment), + description_label); + gtk_box_pack_start(GTK_BOX(cookie_list_vbox), description_label_alignment, + FALSE, FALSE, 0); + + GtkWidget* scroll_window = gtk_scrolled_window_new(NULL, NULL); + gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll_window), + GTK_POLICY_AUTOMATIC, + GTK_POLICY_AUTOMATIC); + gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll_window), + GTK_SHADOW_ETCHED_IN); + gtk_box_pack_start(GTK_BOX(cookie_list_vbox), scroll_window, TRUE, TRUE, 0); + + list_store_ = gtk_list_store_new(COL_COUNT, + GDK_TYPE_PIXBUF, + G_TYPE_STRING, + G_TYPE_STRING); + list_sort_ = gtk_tree_model_sort_new_with_model(GTK_TREE_MODEL(list_store_)); + gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(list_sort_), + COL_SITE, CompareSite, this, NULL); + gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(list_sort_), + COL_COOKIE_NAME, CompareCookieName, this, + NULL); + tree_ = gtk_tree_view_new_with_model(GTK_TREE_MODEL(list_sort_)); + gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree_), TRUE); + gtk_container_add(GTK_CONTAINER(scroll_window), tree_); + + GtkTreeViewColumn* site_column = gtk_tree_view_column_new(); + GtkCellRenderer* pixbuf_renderer = gtk_cell_renderer_pixbuf_new(); + gtk_tree_view_column_pack_start(site_column, pixbuf_renderer, FALSE); + gtk_tree_view_column_add_attribute(site_column, pixbuf_renderer, "pixbuf", + COL_ICON); + GtkCellRenderer* site_renderer = gtk_cell_renderer_text_new(); + gtk_tree_view_column_pack_start(site_column, site_renderer, TRUE); + gtk_tree_view_column_add_attribute(site_column, site_renderer, "text", + COL_SITE); + gtk_tree_view_column_set_title( + site_column, l10n_util::GetStringUTF8( + IDS_COOKIES_DOMAIN_COLUMN_HEADER).c_str()); + gtk_tree_view_column_set_sort_column_id(site_column, COL_SITE); + gtk_tree_view_append_column(GTK_TREE_VIEW(tree_), site_column); + + GtkTreeViewColumn* name_column = gtk_tree_view_column_new_with_attributes( + l10n_util::GetStringUTF8( + IDS_COOKIES_NAME_COLUMN_HEADER).c_str(), + gtk_cell_renderer_text_new(), + "text", COL_COOKIE_NAME, + NULL); + gtk_tree_view_column_set_sort_column_id(name_column, COL_COOKIE_NAME); + gtk_tree_view_append_column(GTK_TREE_VIEW(tree_), name_column); + + selection_ = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_)); + gtk_tree_selection_set_mode(selection_, GTK_SELECTION_MULTIPLE); + g_signal_connect(G_OBJECT(selection_), "changed", + G_CALLBACK(OnSelectionChanged), this); + + // Initialize model. + cookies_table_model_.reset(new CookiesTableModel(profile_)); + cookies_table_model_->SetObserver(this); + OnModelChanged(); + + // Show dialog. + EnableControls(); + gtk_widget_show_all(dialog_); +} + +void CookiesView::EnableControls() { + gtk_widget_set_sensitive( + remove_button_, gtk_tree_selection_count_selected_rows(selection_) > 0); + gtk_widget_set_sensitive( + remove_all_button_, cookies_table_model_->RowCount() > 0); + + const gchar* filter_text = gtk_entry_get_text(GTK_ENTRY(filter_entry_)); + gtk_widget_set_sensitive(filter_clear_button_, filter_text && *filter_text); +} + +void CookiesView::RemoveSelectedCookies() { + GList* list = gtk_tree_selection_get_selected_rows(selection_, NULL); + std::vector<int> selected_rows( + gtk_tree_selection_count_selected_rows(selection_)); + GList* node; + size_t i; + for (i = 0, node = list; node != NULL; ++i, node = node->next) { + selected_rows[i] = GetTreeSortChildRowNumForPath( + list_sort_, static_cast<GtkTreePath*>(node->data)); + } + g_list_foreach(list, (GFunc)gtk_tree_path_free, NULL); + g_list_free(list); + + for (std::vector<int>::reverse_iterator selected = selected_rows.rbegin(); + selected != selected_rows.rend(); ++selected) { + cookies_table_model_->RemoveCookies(*selected, 1); + } +} + +void CookiesView::SetColumnValues(int row, GtkTreeIter* iter) { + SkBitmap bitmap = cookies_table_model_->GetIcon(row); + GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(&bitmap); + std::wstring site = cookies_table_model_->GetText( + row, IDS_COOKIES_DOMAIN_COLUMN_HEADER); + std::wstring name = cookies_table_model_->GetText( + row, IDS_COOKIES_NAME_COLUMN_HEADER); + gtk_list_store_set(list_store_, iter, + COL_ICON, pixbuf, + COL_SITE, WideToUTF8(site).c_str(), + COL_COOKIE_NAME, WideToUTF8(name).c_str(), + -1); + g_object_unref(pixbuf); +} + +void CookiesView::AddNodeToList(int row) { + GtkTreeIter iter; + if (row == 0) { + gtk_list_store_prepend(list_store_, &iter); + } else { + GtkTreeIter sibling; + gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store_), &sibling, + NULL, row - 1); + gtk_list_store_insert_after(list_store_, &iter, &sibling); + } + + SetColumnValues(row, &iter); +} + +void CookiesView::OnModelChanged() { + gtk_list_store_clear(list_store_); + for (int i = 0; i < cookies_table_model_->RowCount(); ++i) + AddNodeToList(i); +} + +void CookiesView::OnItemsChanged(int start, int length) { + GtkTreeIter iter; + bool rv = gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store_), + &iter, NULL, start); + for (int i = 0; i < length; ++i) { + if (!rv) { + NOTREACHED(); + return; + } + SetColumnValues(start + i, &iter); + rv = gtk_tree_model_iter_next(GTK_TREE_MODEL(list_store_), &iter); + } +} + +void CookiesView::OnItemsAdded(int start, int length) { + NOTREACHED(); +} + +void CookiesView::OnItemsRemoved(int start, int length) { + for (int i = 0; i < length; ++i) { + GtkTreeIter iter; + if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store_), &iter, + NULL, start)) { + NOTREACHED(); + return; + } + gtk_list_store_remove(list_store_, &iter); + } +} + +// static +gint CookiesView::CompareSite(GtkTreeModel* model, GtkTreeIter* a, + GtkTreeIter* b, gpointer window) { + int row1 = GetRowNumForIter(model, a); + int row2 = GetRowNumForIter(model, b); + return reinterpret_cast<CookiesView*>(window)->cookies_table_model_-> + CompareValues(row1, row2, IDS_COOKIES_DOMAIN_COLUMN_HEADER); +} + +// static +gint CookiesView::CompareCookieName(GtkTreeModel* model, GtkTreeIter* a, + GtkTreeIter* b, gpointer window) { + int row1 = GetRowNumForIter(model, a); + int row2 = GetRowNumForIter(model, b); + return reinterpret_cast<CookiesView*>(window)->cookies_table_model_-> + CompareValues(row1, row2, IDS_COOKIES_NAME_COLUMN_HEADER); +} + +// static +void CookiesView::OnResponse(GtkDialog* dialog, int response_id, + CookiesView* window) { + if (response_id == RESPONSE_REMOVE) { + window->RemoveSelectedCookies(); + } else if (response_id == RESPONSE_REMOVE_ALL) { + window->cookies_table_model_->RemoveAllShownCookies(); + } else { + gtk_widget_destroy(window->dialog_); + } +} + +// static +void CookiesView::OnWindowDestroy(GtkWidget* widget, CookiesView* window) { + instance_ = NULL; + MessageLoop::current()->DeleteSoon(FROM_HERE, window); +} + +// static +void CookiesView::OnSelectionChanged(GtkTreeSelection *selection, + CookiesView* window) { + window->EnableControls(); +} + +void CookiesView::UpdateFilterResults() { + const gchar* text = gtk_entry_get_text(GTK_ENTRY(filter_entry_)); + if (text) + cookies_table_model_->UpdateSearchResults(UTF8ToWide(text)); +} + +// static +void CookiesView::OnFilterEntryActivated(GtkEntry* entry, CookiesView* window) { + window->filter_update_factory_.RevokeAll(); + window->UpdateFilterResults(); +} + +// static +void CookiesView::OnFilterEntryChanged(GtkEditable* editable, + CookiesView* window) { + window->filter_update_factory_.RevokeAll(); + MessageLoop::current()->PostDelayedTask(FROM_HERE, + window->filter_update_factory_.NewRunnableMethod( + &CookiesView::UpdateFilterResults), kSearchFilterDelayMs); + window->EnableControls(); +} + +// static +void CookiesView::OnFilterClearButtonClicked(GtkButton* button, + CookiesView* window) { + gtk_entry_set_text(GTK_ENTRY(window->filter_entry_), ""); + window->filter_update_factory_.RevokeAll(); + window->UpdateFilterResults(); +} diff --git a/chrome/browser/gtk/options/cookies_view.h b/chrome/browser/gtk/options/cookies_view.h new file mode 100644 index 0000000..5621e58 --- /dev/null +++ b/chrome/browser/gtk/options/cookies_view.h @@ -0,0 +1,103 @@ +// Copyright (c) 2009 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_GTK_OPTIONS_COOKIES_VIEW_H_ +#define CHROME_BROWSER_GTK_OPTIONS_COOKIES_VIEW_H_ + +#include <gtk/gtk.h> + +#include "app/table_model_observer.h" +#include "base/basictypes.h" +#include "base/scoped_ptr.h" +#include "base/task.h" + +class CookiesTableModel; +class Profile; + +class CookiesView : public TableModelObserver { + public: + virtual ~CookiesView(); + + // Create (if necessary) and show the keyword window window. + static void Show(Profile* profile); + + private: + explicit CookiesView(Profile* profile); + + // Initialize the dialog contents and layout. + void Init(); + + // Set sensitivity of buttons based on selection and filter state. + void EnableControls(); + + // Remove any cookies that are currently selected. + void RemoveSelectedCookies(); + + // Set the column values for |row| of |cookies_table_model_| in the + // |list_store_| at |iter|. + void SetColumnValues(int row, GtkTreeIter* iter); + + // Add the values from |row| of |cookies_table_model_|. + void AddNodeToList(int row); + + // TableModelObserver implementation. + virtual void OnModelChanged(); + virtual void OnItemsChanged(int start, int length); + virtual void OnItemsAdded(int start, int length); + virtual void OnItemsRemoved(int start, int length); + + // List sorting callbacks. + static gint CompareSite(GtkTreeModel* model, GtkTreeIter* a, GtkTreeIter* b, + gpointer window); + static gint CompareCookieName(GtkTreeModel* model, GtkTreeIter* a, + GtkTreeIter* b, gpointer window); + + // Callback for dialog buttons. + static void OnResponse(GtkDialog* dialog, int response_id, + CookiesView* window); + + // Callback for window destruction. + static void OnWindowDestroy(GtkWidget* widget, CookiesView* window); + + // Callback for when user selects something in the table. + static void OnSelectionChanged(GtkTreeSelection *selection, + CookiesView* window); + + // Filter the list against the text in |filter_entry_|. + void UpdateFilterResults(); + + // Callbacks for user actions filtering the list. + static void OnFilterEntryActivated(GtkEntry* entry, CookiesView* window); + static void OnFilterEntryChanged(GtkEditable* editable, CookiesView* window); + static void OnFilterClearButtonClicked(GtkButton* button, + CookiesView* window); + + // Widgets of the dialog. + GtkWidget* dialog_; + GtkWidget* filter_entry_; + GtkWidget* filter_clear_button_; + GtkWidget* remove_button_; + GtkWidget* remove_all_button_; + + // The table listing the cookies. + GtkWidget* tree_; + GtkListStore* list_store_; + GtkTreeModel* list_sort_; + GtkTreeSelection* selection_; + + // The profile. + Profile* profile_; + + // The Cookies Table model + scoped_ptr<CookiesTableModel> cookies_table_model_; + + // A factory to construct Runnable Methods so that we can be called back to + // re-evaluate the model after the search query string changes. + ScopedRunnableMethodFactory<CookiesView> filter_update_factory_; + + DISALLOW_COPY_AND_ASSIGN(CookiesView); +}; + + +#endif // CHROME_BROWSER_GTK_OPTIONS_COOKIES_VIEW_H_ diff --git a/chrome/browser/views/options/cookies_view.cc b/chrome/browser/views/options/cookies_view.cc index aee97ba..fa0e711 100644 --- a/chrome/browser/views/options/cookies_view.cc +++ b/chrome/browser/views/options/cookies_view.cc @@ -9,17 +9,14 @@ #include "app/gfx/canvas.h" #include "app/gfx/color_utils.h" #include "app/l10n_util.h" -#include "app/resource_bundle.h" -#include "app/table_model.h" #include "base/message_loop.h" #include "base/string_util.h" #include "base/time_format.h" +#include "chrome/browser/cookies_table_model.h" #include "chrome/browser/profile.h" #include "grit/generated_resources.h" #include "grit/locale_settings.h" -#include "grit/theme_resources.h" #include "net/base/cookie_monster.h" -#include "net/url_request/url_request_context.h" #include "views/border.h" #include "views/grid_layout.h" #include "views/controls/label.h" @@ -35,221 +32,6 @@ static const int kCookieInfoViewInsetSize = 3; static const int kSearchFilterDelayMs = 500; /////////////////////////////////////////////////////////////////////////////// -// CookiesTableModel - -class CookiesTableModel : public TableModel { - public: - explicit CookiesTableModel(Profile* profile); - virtual ~CookiesTableModel() {} - - // Returns information about the Cookie at the specified index. - std::string GetDomainAt(int index); - net::CookieMonster::CanonicalCookie& GetCookieAt(int index); - - // Remove the specified cookies from the Cookie Monster and update the view. - void RemoveCookies(int start_index, int remove_count); - void RemoveAllShownCookies(); - - // TableModel methods. - virtual int RowCount(); - virtual std::wstring GetText(int row, int column_id); - virtual SkBitmap GetIcon(int row); - virtual int CompareValues(int row1, int row2, int column_id); - virtual void SetObserver(TableModelObserver* observer); - - // Filter the cookies to only display matched results. - void UpdateSearchResults(const std::wstring& filter); - - private: - void LoadCookies(); - void DoFilter(); - - std::wstring filter_; - - // The profile from which this model sources cookies. - Profile* profile_; - - typedef net::CookieMonster::CookieList CookieList; - typedef std::vector<net::CookieMonster::CookieListPair*> CookiePtrList; - CookieList all_cookies_; - CookiePtrList shown_cookies_; - - TableModelObserver* observer_; - - DISALLOW_COPY_AND_ASSIGN(CookiesTableModel); -}; - -/////////////////////////////////////////////////////////////////////////////// -// CookiesTableModel, public: - -CookiesTableModel::CookiesTableModel(Profile* profile) - : profile_(profile) { - LoadCookies(); -} - -std::string CookiesTableModel::GetDomainAt(int index) { - DCHECK(index >= 0 && index < RowCount()); - return shown_cookies_.at(index)->first; -} - -net::CookieMonster::CanonicalCookie& CookiesTableModel::GetCookieAt( - int index) { - DCHECK(index >= 0 && index < RowCount()); - return shown_cookies_.at(index)->second; -} - -void CookiesTableModel::RemoveCookies(int start_index, int remove_count) { - if (remove_count <= 0) { - NOTREACHED(); - return; - } - - net::CookieMonster* monster = profile_->GetRequestContext()->cookie_store(); - - // We need to update the searched results list, the full cookie list, - // and the view. We walk through the search results list (which is what - // is displayed) and map these back to the full cookie list. They should - // be in the same sort order, and always exist, so we can just walk once. - // We can't delete any entries from all_cookies_ without invaliding all of - // our pointers after it (which are in shown_cookies), so we go backwards. - CookiePtrList::iterator first = shown_cookies_.begin() + start_index; - CookiePtrList::iterator last = first + remove_count; - CookieList::iterator all_it = all_cookies_.end(); - while (last != first) { - --last; - --all_it; - // Seek to the corresponding entry in all_cookies_ - while (&*all_it != *last) --all_it; - // Delete the cookie from the monster - monster->DeleteCookie(all_it->first, all_it->second, true); - all_it = all_cookies_.erase(all_it); - } - - // By deleting entries from all_cookies, we just possibly moved stuff around - // and have thus invalidated all of our pointers, so rebuild shown_cookies. - // We could do this all better if there was a way to mark elements of - // all_cookies as dead instead of deleting, but this should be fine for now. - DoFilter(); - if (observer_) - observer_->OnItemsRemoved(start_index, remove_count); -} - -void CookiesTableModel::RemoveAllShownCookies() { - RemoveCookies(0, RowCount()); -} - -/////////////////////////////////////////////////////////////////////////////// -// CookiesTableModel, TableModel implementation: - -int CookiesTableModel::RowCount() { - return static_cast<int>(shown_cookies_.size()); -} - -std::wstring CookiesTableModel::GetText(int row, int column_id) { - DCHECK(row >= 0 && row < RowCount()); - switch (column_id) { - case IDS_COOKIES_DOMAIN_COLUMN_HEADER: - { - // Domain cookies start with a trailing dot, but we will show this - // in the cookie details, show it without the dot in the list. - std::string& domain = shown_cookies_.at(row)->first; - std::wstring wide_domain; - if (!domain.empty() && domain[0] == '.') - wide_domain = UTF8ToWide(domain.substr(1)); - else - wide_domain = UTF8ToWide(domain); - // Force domain to be LTR - if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT) - l10n_util::WrapStringWithLTRFormatting(&wide_domain); - return wide_domain; - } - break; - case IDS_COOKIES_NAME_COLUMN_HEADER: { - std::wstring name = UTF8ToWide(shown_cookies_.at(row)->second.Name()); - l10n_util::AdjustStringForLocaleDirection(name, &name); - return name; - break; - } - } - NOTREACHED(); - return L""; -} - -SkBitmap CookiesTableModel::GetIcon(int row) { - static SkBitmap* icon = ResourceBundle::GetSharedInstance().GetBitmapNamed( - IDR_COOKIE_ICON); - return *icon; -} - -void CookiesTableModel::SetObserver(TableModelObserver* observer) { - observer_ = observer; -} - -int CookiesTableModel::CompareValues(int row1, int row2, int column_id) { - if (column_id == IDS_COOKIES_DOMAIN_COLUMN_HEADER) { - // Sort ignore the '.' prefix for domain cookies. - net::CookieMonster::CookieListPair* cp1 = shown_cookies_[row1]; - net::CookieMonster::CookieListPair* cp2 = shown_cookies_[row2]; - bool is1domain = !cp1->first.empty() && cp1->first[0] == '.'; - bool is2domain = !cp2->first.empty() && cp2->first[0] == '.'; - - // They are both either domain or host cookies, sort them normally. - if (is1domain == is2domain) - return cp1->first.compare(cp2->first); - - // One (but only one) is a domain cookie, skip the beginning '.'. - return is1domain ? - cp1->first.compare(1, cp1->first.length() - 1, cp2->first) : - -cp2->first.compare(1, cp2->first.length() - 1, cp1->first); - } - return TableModel::CompareValues(row1, row2, column_id); -} - -/////////////////////////////////////////////////////////////////////////////// -// CookiesTableModel, private: - -// Returns true if |cookie| matches the specified filter, where "match" is -// defined as the cookie's domain, name and value contains filter text -// somewhere. -static bool ContainsFilterText( - const std::string& domain, - const net::CookieMonster::CanonicalCookie& cookie, - const std::string& filter) { - return domain.find(filter) != std::string::npos || - cookie.Name().find(filter) != std::string::npos || - cookie.Value().find(filter) != std::string::npos; -} - -void CookiesTableModel::LoadCookies() { - // mmargh mmargh mmargh! - net::CookieMonster* cookie_monster = - profile_->GetRequestContext()->cookie_store(); - all_cookies_ = cookie_monster->GetAllCookies(); - DoFilter(); -} - -void CookiesTableModel::DoFilter() { - std::string utf8_filter = WideToUTF8(filter_); - bool has_filter = !utf8_filter.empty(); - - shown_cookies_.clear(); - - CookieList::iterator iter = all_cookies_.begin(); - for (; iter != all_cookies_.end(); ++iter) { - if (!has_filter || - ContainsFilterText(iter->first, iter->second, utf8_filter)) { - shown_cookies_.push_back(&*iter); - } - } -} - -void CookiesTableModel::UpdateSearchResults(const std::wstring& filter) { - filter_ = filter; - DoFilter(); - observer_->OnModelChanged(); -} - -/////////////////////////////////////////////////////////////////////////////// // CookiesTableView // Overridden to handle Delete key presses diff --git a/chrome/chrome.gyp b/chrome/chrome.gyp index 4448e28..ab74ae8 100644 --- a/chrome/chrome.gyp +++ b/chrome/chrome.gyp @@ -878,6 +878,8 @@ 'browser/cocoa/web_drop_target.mm', 'browser/command_updater.cc', 'browser/command_updater.h', + 'browser/cookies_table_model.cc', + 'browser/cookies_table_model.h', 'browser/cross_site_request_manager.cc', 'browser/cross_site_request_manager.h', 'browser/defaults.cc', @@ -1123,6 +1125,8 @@ 'browser/gtk/options/advanced_page_gtk.h', 'browser/gtk/options/content_page_gtk.cc', 'browser/gtk/options/content_page_gtk.h', + 'browser/gtk/options/cookies_view.cc', + 'browser/gtk/options/cookies_view.h', 'browser/gtk/options/fonts_languages_window_gtk.cc', 'browser/gtk/options/general_page_gtk.cc', 'browser/gtk/options/general_page_gtk.h', |