diff options
author | ian@chromium.org <ian@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-11-25 16:31:50 +0000 |
---|---|---|
committer | ian@chromium.org <ian@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-11-25 16:31:50 +0000 |
commit | 7daa3e9d198fb002767cfd590559149ca375aeec (patch) | |
tree | f8385c345127f6f4b2d679c30b65a74c11512bcf | |
parent | 752e451f811b4b20ea7049051be44f28fe4d7581 (diff) | |
download | chromium_src-7daa3e9d198fb002767cfd590559149ca375aeec.zip chromium_src-7daa3e9d198fb002767cfd590559149ca375aeec.tar.gz chromium_src-7daa3e9d198fb002767cfd590559149ca375aeec.tar.bz2 |
Adds back the ability to filter cookies by origin in the cookies options view.
BUG=27657
TEST=Typing "google" in the search field pulls up origins containing "google" in their name
Review URL: http://codereview.chromium.org/435024
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@33069 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/cookies_tree_model.cc | 29 | ||||
-rw-r--r-- | chrome/browser/cookies_tree_model.h | 4 | ||||
-rw-r--r-- | chrome/browser/views/options/cookies_view.cc | 70 | ||||
-rw-r--r-- | chrome/browser/views/options/cookies_view.h | 26 |
4 files changed, 109 insertions, 20 deletions
diff --git a/chrome/browser/cookies_tree_model.cc b/chrome/browser/cookies_tree_model.cc index 027224a..29c23d1 100644 --- a/chrome/browser/cookies_tree_model.cc +++ b/chrome/browser/cookies_tree_model.cc @@ -224,6 +224,10 @@ int CookiesTreeModel::GetIconIndex(TreeModelNode* node) { } void CookiesTreeModel::LoadCookies() { + LoadCookiesWithFilter(L""); +} + +void CookiesTreeModel::LoadCookiesWithFilter(const std::wstring& filter) { // mmargh mmargh mmargh! // Since we are running on the UI thread don't call GetURLRequestContext(). @@ -236,11 +240,14 @@ void CookiesTreeModel::LoadCookies() { it != all_cookies_.end(); ++it) { // Get the origin cookie - CookieTreeOriginNode* origin = - root->GetOrCreateOriginNode(UTF8ToWide(it->first)); - CookieTreeCookiesNode* cookies_node = origin->GetOrCreateCookiesNode(); - CookieTreeCookieNode* new_cookie = new CookieTreeCookieNode(&*it); - cookies_node->AddCookieNode(new_cookie); + if (!filter.size() || + (UTF8ToWide(it->first).find(filter) != std::wstring::npos)) { + CookieTreeOriginNode* origin = + root->GetOrCreateOriginNode(UTF8ToWide(it->first)); + CookieTreeCookiesNode* cookies_node = origin->GetOrCreateCookiesNode(); + CookieTreeCookieNode* new_cookie = new CookieTreeCookieNode(&*it); + cookies_node->AddCookieNode(new_cookie); + } } } @@ -263,9 +270,8 @@ void CookiesTreeModel::DeleteAllCookies() { CookieTreeNode* root = GetRoot(); root->DeleteStoredObjects(); int num_children = root->GetChildCount(); - for (int i = num_children - 1; i >= 0; --i) { + for (int i = num_children - 1; i >= 0; --i) delete Remove(root, i); - } LoadCookies(); NotifyObserverTreeNodeChanged(root); } @@ -277,3 +283,12 @@ void CookiesTreeModel::DeleteCookieNode(CookieTreeNode* cookie_node) { int cookie_node_index = parent_node->IndexOfChild(cookie_node); delete Remove(parent_node, cookie_node_index); } + +void CookiesTreeModel::UpdateSearchResults(const std::wstring& filter) { + CookieTreeNode* root = GetRoot(); + int num_children = root->GetChildCount(); + for (int i = num_children - 1; i >= 0; --i) + delete Remove(root, i); + LoadCookiesWithFilter(filter); + NotifyObserverTreeNodeChanged(root); +} diff --git a/chrome/browser/cookies_tree_model.h b/chrome/browser/cookies_tree_model.h index 16606dc..11b7f8a 100644 --- a/chrome/browser/cookies_tree_model.h +++ b/chrome/browser/cookies_tree_model.h @@ -186,6 +186,9 @@ class CookiesTreeModel : public TreeNodeModel<CookieTreeNode> { void DeleteAllCookies(); void DeleteCookieNode(CookieTreeNode* cookie_node); + // Filter the origins to only display matched results. + void UpdateSearchResults(const std::wstring& filter); + private: enum CookieIconIndex { ORIGIN = 0, @@ -195,6 +198,7 @@ class CookiesTreeModel : public TreeNodeModel<CookieTreeNode> { typedef std::vector<net::CookieMonster::CookieListPair*> CookiePtrList; void LoadCookies(); + void LoadCookiesWithFilter(const std::wstring& filter); // The profile from which this model sources cookies. Profile* profile_; diff --git a/chrome/browser/views/options/cookies_view.cc b/chrome/browser/views/options/cookies_view.cc index b287582..1388c82 100644 --- a/chrome/browser/views/options/cookies_view.cc +++ b/chrome/browser/views/options/cookies_view.cc @@ -29,6 +29,7 @@ views::Window* CookiesView::instance_ = NULL; static const int kCookieInfoViewBorderSize = 1; static const int kCookieInfoViewInsetSize = 3; +static const int kSearchFilterDelayMs = 500; /////////////////////////////////////////////////////////////////////////////// @@ -44,15 +45,11 @@ class CookiesTreeView : public views::TreeView { void RemoveSelectedItems(); private: - // Our model, as a CookiesTreeModel. - CookiesTreeModel* cookies_model_; - DISALLOW_COPY_AND_ASSIGN(CookiesTreeView); }; -CookiesTreeView::CookiesTreeView(CookiesTreeModel* cookies_model) - : cookies_model_(cookies_model) { - SetModel(cookies_model_); +CookiesTreeView::CookiesTreeView(CookiesTreeModel* cookies_model) { + SetModel(cookies_model); SetRootShown(false); SetEditable(false); } @@ -60,8 +57,8 @@ CookiesTreeView::CookiesTreeView(CookiesTreeModel* cookies_model) void CookiesTreeView::RemoveSelectedItems() { TreeModelNode* selected_node = GetSelectedNode(); if (selected_node) { - cookies_model_->DeleteCookieNode(static_cast<CookieTreeCookieNode*>( - GetSelectedNode())); + static_cast<CookiesTreeModel*>(model())->DeleteCookieNode( + static_cast<CookieTreeCookieNode*>(GetSelectedNode())); } } @@ -286,10 +283,35 @@ void CookiesView::ButtonPressed( } else if (sender == remove_all_button_) { cookies_tree_model_->DeleteAllCookies(); UpdateForEmptyState(); + } else if (sender == clear_search_button_) { + ResetSearchQuery(); } } /////////////////////////////////////////////////////////////////////////////// +// CookiesView, views::Textfield::Controller implementation: + +void CookiesView::ContentsChanged(views::Textfield* sender, + const std::wstring& new_contents) { + clear_search_button_->SetEnabled(!search_field_->text().empty()); + search_update_factory_.RevokeAll(); + MessageLoop::current()->PostDelayedTask(FROM_HERE, + search_update_factory_.NewRunnableMethod( + &CookiesView::UpdateSearchResults), kSearchFilterDelayMs); +} + +bool CookiesView::HandleKeystroke(views::Textfield* sender, + const views::Textfield::Keystroke& key) { + if (key.GetKeyboardCode() == base::VKEY_ESCAPE) { + ResetSearchQuery(); + } else if (key.GetKeyboardCode() == base::VKEY_RETURN) { + search_update_factory_.RevokeAll(); + UpdateSearchResults(); + } + return false; +} + +/////////////////////////////////////////////////////////////////////////////// // CookiesView, views::DialogDelegate implementation: std::wstring CookiesView::GetWindowTitle() const { @@ -364,19 +386,33 @@ void CookiesView::OnTreeViewKeyDown(base::KeyboardCode keycode) { CookiesView::CookiesView(Profile* profile) : + search_label_(NULL), + search_field_(NULL), + clear_search_button_(NULL), description_label_(NULL), cookies_tree_(NULL), info_view_(NULL), remove_button_(NULL), remove_all_button_(NULL), - profile_(profile) { + profile_(profile), + ALLOW_THIS_IN_INITIALIZER_LIST(search_update_factory_(this)) { } -views::View* CookiesView::GetInitiallyFocusedView() { - return cookies_tree_; + +void CookiesView::UpdateSearchResults() { + cookies_tree_model_->UpdateSearchResults(search_field_->text()); + remove_all_button_->SetEnabled(cookies_tree_model_->GetRoot()-> + GetTotalNodeCount() > 1); } void CookiesView::Init() { + search_label_ = new views::Label( + l10n_util::GetString(IDS_COOKIES_SEARCH_LABEL)); + search_field_ = new views::Textfield; + search_field_->SetController(this); + clear_search_button_ = new views::NativeButton( + this, l10n_util::GetString(IDS_COOKIES_CLEAR_SEARCH_LABEL)); + clear_search_button_->SetEnabled(false); description_label_ = new views::Label( l10n_util::GetString(IDS_COOKIES_INFO_LABEL)); description_label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); @@ -410,6 +446,12 @@ void CookiesView::Init() { column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 1, GridLayout::USE_PREF, 0, 0); + layout->StartRow(0, five_column_layout_id); + layout->AddView(search_label_); + layout->AddView(search_field_); + layout->AddView(clear_search_button_); + layout->AddPaddingRow(0, kUnrelatedControlVerticalSpacing); + layout->StartRow(0, single_column_layout_id); layout->AddView(description_label_); @@ -433,6 +475,12 @@ void CookiesView::Init() { UpdateForEmptyState(); } +void CookiesView::ResetSearchQuery() { + search_field_->SetText(EmptyWString()); + clear_search_button_->SetEnabled(false); + UpdateSearchResults(); +} + void CookiesView::UpdateForEmptyState() { info_view_->ClearCookieDisplay(); remove_button_->SetEnabled(false); diff --git a/chrome/browser/views/options/cookies_view.h b/chrome/browser/views/options/cookies_view.h index 7932b45..d7c1b23 100644 --- a/chrome/browser/views/options/cookies_view.h +++ b/chrome/browser/views/options/cookies_view.h @@ -34,13 +34,17 @@ class Timer; class CookiesView : public views::View, public views::DialogDelegate, public views::ButtonListener, - public views::TreeViewController { + public views::TreeViewController, + public views::Textfield::Controller { public: // Show the Cookies Window, creating one if necessary. static void ShowCookiesWindow(Profile* profile); virtual ~CookiesView(); + // Updates the display to show only the search results. + void UpdateSearchResults(); + // views::ButtonListener implementation. virtual void ButtonPressed(views::Button* sender, const views::Event& event); @@ -50,11 +54,19 @@ class CookiesView : public views::View, // views::TreeViewController implementation. virtual void OnTreeViewKeyDown(base::KeyboardCode keycode); + // views::Textfield::Controller implementation. + virtual void ContentsChanged(views::Textfield* sender, + const std::wstring& new_contents); + virtual bool HandleKeystroke(views::Textfield* sender, + const views::Textfield::Keystroke& key); + // views::WindowDelegate implementation. virtual int GetDialogButtons() const { return MessageBoxFlags::DIALOGBUTTON_CANCEL; } - virtual views::View* GetInitiallyFocusedView(); + virtual views::View* GetInitiallyFocusedView() { + return search_field_; + } virtual bool CanResize() const { return true; } virtual std::wstring GetWindowTitle() const; @@ -78,10 +90,16 @@ class CookiesView : public views::View, // Initialize the dialog contents and layout. void Init(); + // Resets the display to what it would be if there were no search query. + void ResetSearchQuery(); + // Update the UI when there are no cookies. void UpdateForEmptyState(); // Assorted dialog controls + views::Label* search_label_; + views::Textfield* search_field_; + views::NativeButton* clear_search_button_; views::Label* description_label_; CookiesTreeView* cookies_tree_; CookieInfoView* info_view_; @@ -94,6 +112,10 @@ class CookiesView : public views::View, // The Profile for which Cookies are displayed Profile* profile_; + // 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> search_update_factory_; + // Our containing window. If this is non-NULL there is a visible Cookies // window somewhere. static views::Window* instance_; |