| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/views/options/cookies_view.h" | 5 #include "chrome/browser/views/options/cookies_view.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "app/gfx/canvas.h" | 9 #include "app/gfx/canvas.h" |
| 10 #include "app/gfx/color_utils.h" | 10 #include "app/gfx/color_utils.h" |
| 11 #include "app/l10n_util.h" | 11 #include "app/l10n_util.h" |
| 12 #include "app/resource_bundle.h" | |
| 13 #include "app/table_model.h" | |
| 14 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
| 15 #include "base/string_util.h" | 13 #include "base/string_util.h" |
| 16 #include "base/time_format.h" | 14 #include "base/time_format.h" |
| 15 #include "chrome/browser/cookies_table_model.h" |
| 17 #include "chrome/browser/profile.h" | 16 #include "chrome/browser/profile.h" |
| 18 #include "grit/generated_resources.h" | 17 #include "grit/generated_resources.h" |
| 19 #include "grit/locale_settings.h" | 18 #include "grit/locale_settings.h" |
| 20 #include "grit/theme_resources.h" | |
| 21 #include "net/base/cookie_monster.h" | 19 #include "net/base/cookie_monster.h" |
| 22 #include "net/url_request/url_request_context.h" | |
| 23 #include "views/border.h" | 20 #include "views/border.h" |
| 24 #include "views/grid_layout.h" | 21 #include "views/grid_layout.h" |
| 25 #include "views/controls/label.h" | 22 #include "views/controls/label.h" |
| 26 #include "views/controls/button/native_button.h" | 23 #include "views/controls/button/native_button.h" |
| 27 #include "views/controls/table/table_view.h" | 24 #include "views/controls/table/table_view.h" |
| 28 #include "views/controls/textfield/textfield.h" | 25 #include "views/controls/textfield/textfield.h" |
| 29 #include "views/standard_layout.h" | 26 #include "views/standard_layout.h" |
| 30 | 27 |
| 31 // static | 28 // static |
| 32 views::Window* CookiesView::instance_ = NULL; | 29 views::Window* CookiesView::instance_ = NULL; |
| 33 static const int kCookieInfoViewBorderSize = 1; | 30 static const int kCookieInfoViewBorderSize = 1; |
| 34 static const int kCookieInfoViewInsetSize = 3; | 31 static const int kCookieInfoViewInsetSize = 3; |
| 35 static const int kSearchFilterDelayMs = 500; | 32 static const int kSearchFilterDelayMs = 500; |
| 36 | 33 |
| 37 /////////////////////////////////////////////////////////////////////////////// | 34 /////////////////////////////////////////////////////////////////////////////// |
| 38 // CookiesTableModel | |
| 39 | |
| 40 class CookiesTableModel : public TableModel { | |
| 41 public: | |
| 42 explicit CookiesTableModel(Profile* profile); | |
| 43 virtual ~CookiesTableModel() {} | |
| 44 | |
| 45 // Returns information about the Cookie at the specified index. | |
| 46 std::string GetDomainAt(int index); | |
| 47 net::CookieMonster::CanonicalCookie& GetCookieAt(int index); | |
| 48 | |
| 49 // Remove the specified cookies from the Cookie Monster and update the view. | |
| 50 void RemoveCookies(int start_index, int remove_count); | |
| 51 void RemoveAllShownCookies(); | |
| 52 | |
| 53 // TableModel methods. | |
| 54 virtual int RowCount(); | |
| 55 virtual std::wstring GetText(int row, int column_id); | |
| 56 virtual SkBitmap GetIcon(int row); | |
| 57 virtual int CompareValues(int row1, int row2, int column_id); | |
| 58 virtual void SetObserver(TableModelObserver* observer); | |
| 59 | |
| 60 // Filter the cookies to only display matched results. | |
| 61 void UpdateSearchResults(const std::wstring& filter); | |
| 62 | |
| 63 private: | |
| 64 void LoadCookies(); | |
| 65 void DoFilter(); | |
| 66 | |
| 67 std::wstring filter_; | |
| 68 | |
| 69 // The profile from which this model sources cookies. | |
| 70 Profile* profile_; | |
| 71 | |
| 72 typedef net::CookieMonster::CookieList CookieList; | |
| 73 typedef std::vector<net::CookieMonster::CookieListPair*> CookiePtrList; | |
| 74 CookieList all_cookies_; | |
| 75 CookiePtrList shown_cookies_; | |
| 76 | |
| 77 TableModelObserver* observer_; | |
| 78 | |
| 79 DISALLOW_COPY_AND_ASSIGN(CookiesTableModel); | |
| 80 }; | |
| 81 | |
| 82 /////////////////////////////////////////////////////////////////////////////// | |
| 83 // CookiesTableModel, public: | |
| 84 | |
| 85 CookiesTableModel::CookiesTableModel(Profile* profile) | |
| 86 : profile_(profile) { | |
| 87 LoadCookies(); | |
| 88 } | |
| 89 | |
| 90 std::string CookiesTableModel::GetDomainAt(int index) { | |
| 91 DCHECK(index >= 0 && index < RowCount()); | |
| 92 return shown_cookies_.at(index)->first; | |
| 93 } | |
| 94 | |
| 95 net::CookieMonster::CanonicalCookie& CookiesTableModel::GetCookieAt( | |
| 96 int index) { | |
| 97 DCHECK(index >= 0 && index < RowCount()); | |
| 98 return shown_cookies_.at(index)->second; | |
| 99 } | |
| 100 | |
| 101 void CookiesTableModel::RemoveCookies(int start_index, int remove_count) { | |
| 102 if (remove_count <= 0) { | |
| 103 NOTREACHED(); | |
| 104 return; | |
| 105 } | |
| 106 | |
| 107 net::CookieMonster* monster = profile_->GetRequestContext()->cookie_store(); | |
| 108 | |
| 109 // We need to update the searched results list, the full cookie list, | |
| 110 // and the view. We walk through the search results list (which is what | |
| 111 // is displayed) and map these back to the full cookie list. They should | |
| 112 // be in the same sort order, and always exist, so we can just walk once. | |
| 113 // We can't delete any entries from all_cookies_ without invaliding all of | |
| 114 // our pointers after it (which are in shown_cookies), so we go backwards. | |
| 115 CookiePtrList::iterator first = shown_cookies_.begin() + start_index; | |
| 116 CookiePtrList::iterator last = first + remove_count; | |
| 117 CookieList::iterator all_it = all_cookies_.end(); | |
| 118 while (last != first) { | |
| 119 --last; | |
| 120 --all_it; | |
| 121 // Seek to the corresponding entry in all_cookies_ | |
| 122 while (&*all_it != *last) --all_it; | |
| 123 // Delete the cookie from the monster | |
| 124 monster->DeleteCookie(all_it->first, all_it->second, true); | |
| 125 all_it = all_cookies_.erase(all_it); | |
| 126 } | |
| 127 | |
| 128 // By deleting entries from all_cookies, we just possibly moved stuff around | |
| 129 // and have thus invalidated all of our pointers, so rebuild shown_cookies. | |
| 130 // We could do this all better if there was a way to mark elements of | |
| 131 // all_cookies as dead instead of deleting, but this should be fine for now. | |
| 132 DoFilter(); | |
| 133 if (observer_) | |
| 134 observer_->OnItemsRemoved(start_index, remove_count); | |
| 135 } | |
| 136 | |
| 137 void CookiesTableModel::RemoveAllShownCookies() { | |
| 138 RemoveCookies(0, RowCount()); | |
| 139 } | |
| 140 | |
| 141 /////////////////////////////////////////////////////////////////////////////// | |
| 142 // CookiesTableModel, TableModel implementation: | |
| 143 | |
| 144 int CookiesTableModel::RowCount() { | |
| 145 return static_cast<int>(shown_cookies_.size()); | |
| 146 } | |
| 147 | |
| 148 std::wstring CookiesTableModel::GetText(int row, int column_id) { | |
| 149 DCHECK(row >= 0 && row < RowCount()); | |
| 150 switch (column_id) { | |
| 151 case IDS_COOKIES_DOMAIN_COLUMN_HEADER: | |
| 152 { | |
| 153 // Domain cookies start with a trailing dot, but we will show this | |
| 154 // in the cookie details, show it without the dot in the list. | |
| 155 std::string& domain = shown_cookies_.at(row)->first; | |
| 156 std::wstring wide_domain; | |
| 157 if (!domain.empty() && domain[0] == '.') | |
| 158 wide_domain = UTF8ToWide(domain.substr(1)); | |
| 159 else | |
| 160 wide_domain = UTF8ToWide(domain); | |
| 161 // Force domain to be LTR | |
| 162 if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT) | |
| 163 l10n_util::WrapStringWithLTRFormatting(&wide_domain); | |
| 164 return wide_domain; | |
| 165 } | |
| 166 break; | |
| 167 case IDS_COOKIES_NAME_COLUMN_HEADER: { | |
| 168 std::wstring name = UTF8ToWide(shown_cookies_.at(row)->second.Name()); | |
| 169 l10n_util::AdjustStringForLocaleDirection(name, &name); | |
| 170 return name; | |
| 171 break; | |
| 172 } | |
| 173 } | |
| 174 NOTREACHED(); | |
| 175 return L""; | |
| 176 } | |
| 177 | |
| 178 SkBitmap CookiesTableModel::GetIcon(int row) { | |
| 179 static SkBitmap* icon = ResourceBundle::GetSharedInstance().GetBitmapNamed( | |
| 180 IDR_COOKIE_ICON); | |
| 181 return *icon; | |
| 182 } | |
| 183 | |
| 184 void CookiesTableModel::SetObserver(TableModelObserver* observer) { | |
| 185 observer_ = observer; | |
| 186 } | |
| 187 | |
| 188 int CookiesTableModel::CompareValues(int row1, int row2, int column_id) { | |
| 189 if (column_id == IDS_COOKIES_DOMAIN_COLUMN_HEADER) { | |
| 190 // Sort ignore the '.' prefix for domain cookies. | |
| 191 net::CookieMonster::CookieListPair* cp1 = shown_cookies_[row1]; | |
| 192 net::CookieMonster::CookieListPair* cp2 = shown_cookies_[row2]; | |
| 193 bool is1domain = !cp1->first.empty() && cp1->first[0] == '.'; | |
| 194 bool is2domain = !cp2->first.empty() && cp2->first[0] == '.'; | |
| 195 | |
| 196 // They are both either domain or host cookies, sort them normally. | |
| 197 if (is1domain == is2domain) | |
| 198 return cp1->first.compare(cp2->first); | |
| 199 | |
| 200 // One (but only one) is a domain cookie, skip the beginning '.'. | |
| 201 return is1domain ? | |
| 202 cp1->first.compare(1, cp1->first.length() - 1, cp2->first) : | |
| 203 -cp2->first.compare(1, cp2->first.length() - 1, cp1->first); | |
| 204 } | |
| 205 return TableModel::CompareValues(row1, row2, column_id); | |
| 206 } | |
| 207 | |
| 208 /////////////////////////////////////////////////////////////////////////////// | |
| 209 // CookiesTableModel, private: | |
| 210 | |
| 211 // Returns true if |cookie| matches the specified filter, where "match" is | |
| 212 // defined as the cookie's domain, name and value contains filter text | |
| 213 // somewhere. | |
| 214 static bool ContainsFilterText( | |
| 215 const std::string& domain, | |
| 216 const net::CookieMonster::CanonicalCookie& cookie, | |
| 217 const std::string& filter) { | |
| 218 return domain.find(filter) != std::string::npos || | |
| 219 cookie.Name().find(filter) != std::string::npos || | |
| 220 cookie.Value().find(filter) != std::string::npos; | |
| 221 } | |
| 222 | |
| 223 void CookiesTableModel::LoadCookies() { | |
| 224 // mmargh mmargh mmargh! | |
| 225 net::CookieMonster* cookie_monster = | |
| 226 profile_->GetRequestContext()->cookie_store(); | |
| 227 all_cookies_ = cookie_monster->GetAllCookies(); | |
| 228 DoFilter(); | |
| 229 } | |
| 230 | |
| 231 void CookiesTableModel::DoFilter() { | |
| 232 std::string utf8_filter = WideToUTF8(filter_); | |
| 233 bool has_filter = !utf8_filter.empty(); | |
| 234 | |
| 235 shown_cookies_.clear(); | |
| 236 | |
| 237 CookieList::iterator iter = all_cookies_.begin(); | |
| 238 for (; iter != all_cookies_.end(); ++iter) { | |
| 239 if (!has_filter || | |
| 240 ContainsFilterText(iter->first, iter->second, utf8_filter)) { | |
| 241 shown_cookies_.push_back(&*iter); | |
| 242 } | |
| 243 } | |
| 244 } | |
| 245 | |
| 246 void CookiesTableModel::UpdateSearchResults(const std::wstring& filter) { | |
| 247 filter_ = filter; | |
| 248 DoFilter(); | |
| 249 observer_->OnModelChanged(); | |
| 250 } | |
| 251 | |
| 252 /////////////////////////////////////////////////////////////////////////////// | |
| 253 // CookiesTableView | 35 // CookiesTableView |
| 254 // Overridden to handle Delete key presses | 36 // Overridden to handle Delete key presses |
| 255 | 37 |
| 256 class CookiesTableView : public views::TableView { | 38 class CookiesTableView : public views::TableView { |
| 257 public: | 39 public: |
| 258 CookiesTableView(CookiesTableModel* cookies_model, | 40 CookiesTableView(CookiesTableModel* cookies_model, |
| 259 std::vector<TableColumn> columns); | 41 std::vector<TableColumn> columns); |
| 260 virtual ~CookiesTableView() {} | 42 virtual ~CookiesTableView() {} |
| 261 | 43 |
| 262 // Removes the cookies associated with the selected rows in the TableView. | 44 // Removes the cookies associated with the selected rows in the TableView. |
| (...skipping 523 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 786 search_field_->SetText(EmptyWString()); | 568 search_field_->SetText(EmptyWString()); |
| 787 clear_search_button_->SetEnabled(false); | 569 clear_search_button_->SetEnabled(false); |
| 788 UpdateSearchResults(); | 570 UpdateSearchResults(); |
| 789 } | 571 } |
| 790 | 572 |
| 791 void CookiesView::UpdateForEmptyState() { | 573 void CookiesView::UpdateForEmptyState() { |
| 792 info_view_->ClearCookieDisplay(); | 574 info_view_->ClearCookieDisplay(); |
| 793 remove_button_->SetEnabled(false); | 575 remove_button_->SetEnabled(false); |
| 794 remove_all_button_->SetEnabled(false); | 576 remove_all_button_->SetEnabled(false); |
| 795 } | 577 } |
| OLD | NEW |