| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/views/password_manager_exceptions_view.h" | |
| 6 | |
| 7 #include "base/string_util.h" | |
| 8 #include "chrome/common/l10n_util.h" | |
| 9 #include "chrome/browser/profile.h" | |
| 10 #include "chrome/browser/views/standard_layout.h" | |
| 11 #include "chrome/common/pref_names.h" | |
| 12 #include "chrome/common/pref_service.h" | |
| 13 #include "chrome/views/background.h" | |
| 14 #include "chrome/views/controls/button/native_button.h" | |
| 15 #include "chrome/views/grid_layout.h" | |
| 16 #include "grit/generated_resources.h" | |
| 17 | |
| 18 using views::ColumnSet; | |
| 19 using views::GridLayout; | |
| 20 | |
| 21 // We can only have one PasswordManagerExceptionsView at a time. | |
| 22 PasswordManagerExceptionsView* PasswordManagerExceptionsView::instance_ = NULL; | |
| 23 | |
| 24 static const int kDefaultWindowWidth = 530; | |
| 25 static const int kDefaultWindowHeight = 240; | |
| 26 | |
| 27 //////////////////////////////////////////////////////////////////// | |
| 28 // PasswordManagerExceptionsTableModel | |
| 29 PasswordManagerExceptionsTableModel::PasswordManagerExceptionsTableModel( | |
| 30 Profile* profile) : PasswordManagerTableModel(profile) { | |
| 31 } | |
| 32 | |
| 33 PasswordManagerExceptionsTableModel::~PasswordManagerExceptionsTableModel() { | |
| 34 } | |
| 35 | |
| 36 std::wstring PasswordManagerExceptionsTableModel::GetText(int row, int col_id) { | |
| 37 DCHECK_EQ(col_id, IDS_PASSWORD_MANAGER_VIEW_SITE_COLUMN); | |
| 38 return PasswordManagerTableModel::GetText(row, col_id); | |
| 39 } | |
| 40 | |
| 41 int PasswordManagerExceptionsTableModel::CompareValues(int row1, int row2, | |
| 42 int col_id) { | |
| 43 DCHECK_EQ(col_id, IDS_PASSWORD_MANAGER_VIEW_SITE_COLUMN); | |
| 44 return PasswordManagerTableModel::CompareValues(row1, row2, col_id); | |
| 45 } | |
| 46 | |
| 47 void PasswordManagerExceptionsTableModel::GetAllExceptionsForProfile() { | |
| 48 DCHECK(!pending_login_query_); | |
| 49 pending_login_query_ = web_data_service()->GetAllLogins(this); | |
| 50 } | |
| 51 | |
| 52 void PasswordManagerExceptionsTableModel::OnWebDataServiceRequestDone( | |
| 53 WebDataService::Handle h, | |
| 54 const WDTypedResult* result) { | |
| 55 DCHECK_EQ(pending_login_query_, h); | |
| 56 pending_login_query_ = NULL; | |
| 57 | |
| 58 if (!result) | |
| 59 return; | |
| 60 | |
| 61 DCHECK(result->GetType() == PASSWORD_RESULT); | |
| 62 | |
| 63 // Get the result from the database into a useable form. | |
| 64 const WDResult<std::vector<PasswordForm*> >* r = | |
| 65 static_cast<const WDResult<std::vector<PasswordForm*> >*>(result); | |
| 66 std::vector<PasswordForm*> rows = r->GetValue(); | |
| 67 STLDeleteElements<PasswordRows>(&saved_signons_); | |
| 68 std::wstring languages = | |
| 69 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages); | |
| 70 for (size_t i = 0; i < rows.size(); ++i) { | |
| 71 if (rows[i]->blacklisted_by_user) { | |
| 72 saved_signons_.push_back(new PasswordRow( | |
| 73 gfx::SortedDisplayURL(rows[i]->origin, languages), rows[i])); | |
| 74 } | |
| 75 } | |
| 76 if (observer_) | |
| 77 observer_->OnModelChanged(); | |
| 78 if (row_count_observer_) | |
| 79 row_count_observer_->OnRowCountChanged(RowCount()); | |
| 80 } | |
| 81 | |
| 82 ////////////////////////////////////////////////////////////////////// | |
| 83 // PasswordManagerExceptionsView | |
| 84 | |
| 85 // static | |
| 86 void PasswordManagerExceptionsView::Show(Profile* profile) { | |
| 87 DCHECK(profile); | |
| 88 if (!instance_) { | |
| 89 instance_ = new PasswordManagerExceptionsView(profile); | |
| 90 | |
| 91 // manager is owned by the dialog window, so Close() will delete it. | |
| 92 views::Window::CreateChromeWindow(NULL, gfx::Rect(), instance_); | |
| 93 } | |
| 94 if (!instance_->window()->IsVisible()) { | |
| 95 instance_->window()->Show(); | |
| 96 } else { | |
| 97 instance_->window()->Activate(); | |
| 98 } | |
| 99 } | |
| 100 | |
| 101 PasswordManagerExceptionsView::PasswordManagerExceptionsView(Profile* profile) | |
| 102 : remove_button_(this, l10n_util::GetString( | |
| 103 IDS_PASSWORD_MANAGER_EXCEPTIONS_VIEW_REMOVE_BUTTON)), | |
| 104 remove_all_button_(this, l10n_util::GetString( | |
| 105 IDS_PASSWORD_MANAGER_EXCEPTIONS_VIEW_REMOVE_ALL_BUTTON)), | |
| 106 table_model_(profile) { | |
| 107 Init(); | |
| 108 } | |
| 109 | |
| 110 void PasswordManagerExceptionsView::SetupTable() { | |
| 111 // Tell the table model we are concern about how many rows it has. | |
| 112 table_model_.set_row_count_observer(this); | |
| 113 | |
| 114 // Creates the different columns for the table. | |
| 115 // The float resize values are the result of much tinkering. | |
| 116 std::vector<views::TableColumn> columns; | |
| 117 columns.push_back(views::TableColumn( | |
| 118 IDS_PASSWORD_MANAGER_VIEW_SITE_COLUMN, | |
| 119 views::TableColumn::LEFT, -1, 0.55f)); | |
| 120 columns.back().sortable = true; | |
| 121 table_view_ = new views::TableView(&table_model_, columns, views::TEXT_ONLY, | |
| 122 true, true, true); | |
| 123 // Make the table initially sorted by host. | |
| 124 views::TableView::SortDescriptors sort; | |
| 125 sort.push_back(views::TableView::SortDescriptor( | |
| 126 IDS_PASSWORD_MANAGER_VIEW_SITE_COLUMN, true)); | |
| 127 table_view_->SetSortDescriptors(sort); | |
| 128 table_view_->SetObserver(this); | |
| 129 } | |
| 130 | |
| 131 void PasswordManagerExceptionsView::SetupButtons() { | |
| 132 // Tell View not to delete class stack allocated views. | |
| 133 | |
| 134 remove_button_.SetParentOwned(false); | |
| 135 remove_button_.SetEnabled(false); | |
| 136 | |
| 137 remove_all_button_.SetParentOwned(false); | |
| 138 } | |
| 139 | |
| 140 void PasswordManagerExceptionsView::Init() { | |
| 141 // Configure the background and view elements (buttons, labels, table). | |
| 142 SetupButtons(); | |
| 143 SetupTable(); | |
| 144 | |
| 145 // Do the layout thing. | |
| 146 const int column_set_id = 0; | |
| 147 GridLayout* layout = CreatePanelGridLayout(this); | |
| 148 SetLayoutManager(layout); | |
| 149 | |
| 150 // Design the grid. | |
| 151 ColumnSet* column_set = layout->AddColumnSet(column_set_id); | |
| 152 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 1, | |
| 153 GridLayout::FIXED, 300, 0); | |
| 154 column_set->AddPaddingColumn(0, kRelatedControlHorizontalSpacing); | |
| 155 column_set->AddColumn(GridLayout::FILL, GridLayout::LEADING, 0, | |
| 156 GridLayout::USE_PREF, 0, 0); | |
| 157 | |
| 158 // Fill the grid. | |
| 159 layout->StartRow(0.05f, column_set_id); | |
| 160 layout->AddView(table_view_); | |
| 161 layout->AddView(&remove_button_); | |
| 162 | |
| 163 // Ask the database for exception data. | |
| 164 table_model_.GetAllExceptionsForProfile(); | |
| 165 } | |
| 166 | |
| 167 PasswordManagerExceptionsView::~PasswordManagerExceptionsView() { | |
| 168 } | |
| 169 | |
| 170 void PasswordManagerExceptionsView::Layout() { | |
| 171 GetLayoutManager()->Layout(this); | |
| 172 | |
| 173 // Manually lay out the Remove All button in the same row as | |
| 174 // the close button. | |
| 175 gfx::Rect parent_bounds = GetParent()->GetLocalBounds(false); | |
| 176 gfx::Size prefsize = remove_all_button_.GetPreferredSize(); | |
| 177 int button_y = | |
| 178 parent_bounds.bottom() - prefsize.height() - kButtonVEdgeMargin; | |
| 179 remove_all_button_.SetBounds(kPanelHorizMargin, button_y, prefsize.width(), | |
| 180 prefsize.height()); | |
| 181 } | |
| 182 | |
| 183 gfx::Size PasswordManagerExceptionsView::GetPreferredSize() { | |
| 184 return gfx::Size(kDefaultWindowWidth, kDefaultWindowHeight); | |
| 185 } | |
| 186 | |
| 187 void PasswordManagerExceptionsView::ViewHierarchyChanged(bool is_add, | |
| 188 views::View* parent, | |
| 189 views::View* child) { | |
| 190 if (child == this) { | |
| 191 // Add and remove the Remove All button from the ClientView's hierarchy. | |
| 192 if (is_add) { | |
| 193 parent->AddChildView(&remove_all_button_); | |
| 194 } else { | |
| 195 parent->RemoveChildView(&remove_all_button_); | |
| 196 } | |
| 197 } | |
| 198 } | |
| 199 | |
| 200 void PasswordManagerExceptionsView::OnSelectionChanged() { | |
| 201 bool has_selection = table_view_->SelectedRowCount() > 0; | |
| 202 remove_button_.SetEnabled(has_selection); | |
| 203 } | |
| 204 | |
| 205 int PasswordManagerExceptionsView::GetDialogButtons() const { | |
| 206 return DIALOGBUTTON_CANCEL; | |
| 207 } | |
| 208 | |
| 209 std::wstring PasswordManagerExceptionsView::GetWindowTitle() const { | |
| 210 return l10n_util::GetString(IDS_PASSWORD_MANAGER_EXCEPTIONS_VIEW_TITLE); | |
| 211 } | |
| 212 | |
| 213 void PasswordManagerExceptionsView::ButtonPressed(views::Button* sender) { | |
| 214 DCHECK(window()); | |
| 215 // Close will result in our destruction. | |
| 216 if (sender == &remove_all_button_) { | |
| 217 table_model_.ForgetAndRemoveAllSignons(); | |
| 218 return; | |
| 219 } | |
| 220 | |
| 221 // The following require a selection (and only one, since table is single- | |
| 222 // select only). | |
| 223 views::TableSelectionIterator iter = table_view_->SelectionBegin(); | |
| 224 int row = *iter; | |
| 225 PasswordForm* selected = table_model_.GetPasswordFormAt(row); | |
| 226 DCHECK(++iter == table_view_->SelectionEnd()); | |
| 227 | |
| 228 if (sender == &remove_button_) { | |
| 229 table_model_.ForgetAndRemoveSignon(row); | |
| 230 } else { | |
| 231 NOTREACHED() << "Invalid button."; | |
| 232 } | |
| 233 } | |
| 234 | |
| 235 void PasswordManagerExceptionsView::WindowClosing() { | |
| 236 // The table model will be deleted before the table view, so detach it. | |
| 237 table_view_->SetModel(NULL); | |
| 238 | |
| 239 // Clear the static instance so the next time Show() is called, a new | |
| 240 // instance is created. | |
| 241 instance_ = NULL; | |
| 242 } | |
| 243 | |
| 244 views::View* PasswordManagerExceptionsView::GetContentsView() { | |
| 245 return this; | |
| 246 } | |
| 247 | |
| 248 void PasswordManagerExceptionsView::OnRowCountChanged(size_t rows) { | |
| 249 remove_all_button_.SetEnabled(rows > 0); | |
| 250 } | |
| OLD | NEW |