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 "base/string_util.h" |
| 6 #include "chrome/common/l10n_util.h" |
| 7 #include "chrome/browser/profile.h" |
| 8 #include "chrome/browser/views/password_manager_exceptions_view.h" |
| 9 #include "chrome/browser/views/standard_layout.h" |
| 10 #include "chrome/common/pref_names.h" |
| 11 #include "chrome/common/pref_service.h" |
| 12 #include "chrome/views/background.h" |
| 13 #include "chrome/views/grid_layout.h" |
| 14 #include "chrome/views/native_button.h" |
| 15 |
| 16 #include "generated_resources.h" |
| 17 |
| 18 using views::ColumnSet; |
| 19 using views::GridLayout; |
| 20 |
| 21 // We can only have one PasswordManagerExceptionsView at a time. |
| 22 static 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 } |
| 79 |
| 80 ////////////////////////////////////////////////////////////////////// |
| 81 // PasswordManagerExceptionsView |
| 82 |
| 83 // static |
| 84 void PasswordManagerExceptionsView::Show(Profile* profile) { |
| 85 DCHECK(profile); |
| 86 if (!instance_) { |
| 87 instance_ = new PasswordManagerExceptionsView(profile); |
| 88 |
| 89 // manager is owned by the dialog window, so Close() will delete it. |
| 90 views::Window::CreateChromeWindow(NULL, gfx::Rect(), instance_); |
| 91 } |
| 92 if (!instance_->window()->IsVisible()) { |
| 93 instance_->window()->Show(); |
| 94 } else { |
| 95 instance_->window()->Activate(); |
| 96 } |
| 97 } |
| 98 |
| 99 PasswordManagerExceptionsView::PasswordManagerExceptionsView(Profile* profile) |
| 100 : remove_button_(l10n_util::GetString( |
| 101 IDS_PASSWORD_MANAGER_EXCEPTIONS_VIEW_REMOVE_BUTTON)), |
| 102 remove_all_button_(l10n_util::GetString( |
| 103 IDS_PASSWORD_MANAGER_EXCEPTIONS_VIEW_REMOVE_ALL_BUTTON)), |
| 104 table_model_(profile) { |
| 105 Init(); |
| 106 } |
| 107 |
| 108 void PasswordManagerExceptionsView::SetupTable() { |
| 109 // Creates the different columns for the table. |
| 110 // The float resize values are the result of much tinkering. |
| 111 std::vector<views::TableColumn> columns; |
| 112 columns.push_back(views::TableColumn( |
| 113 IDS_PASSWORD_MANAGER_VIEW_SITE_COLUMN, |
| 114 views::TableColumn::LEFT, -1, 0.55f)); |
| 115 columns.back().sortable = true; |
| 116 table_view_ = new views::TableView(&table_model_, columns, views::TEXT_ONLY, |
| 117 true, true, true); |
| 118 // Make the table initially sorted by host. |
| 119 views::TableView::SortDescriptors sort; |
| 120 sort.push_back(views::TableView::SortDescriptor( |
| 121 IDS_PASSWORD_MANAGER_VIEW_SITE_COLUMN, true)); |
| 122 table_view_->SetSortDescriptors(sort); |
| 123 table_view_->SetObserver(this); |
| 124 } |
| 125 |
| 126 void PasswordManagerExceptionsView::SetupButtons() { |
| 127 // Tell View not to delete class stack allocated views. |
| 128 |
| 129 remove_button_.SetParentOwned(false); |
| 130 remove_button_.SetListener(this); |
| 131 remove_button_.SetEnabled(false); |
| 132 |
| 133 remove_all_button_.SetParentOwned(false); |
| 134 remove_all_button_.SetListener(this); |
| 135 } |
| 136 |
| 137 void PasswordManagerExceptionsView::Init() { |
| 138 // Configure the background and view elements (buttons, labels, table). |
| 139 SetupButtons(); |
| 140 SetupTable(); |
| 141 |
| 142 // Do the layout thing. |
| 143 const int column_set_id = 0; |
| 144 GridLayout* layout = CreatePanelGridLayout(this); |
| 145 SetLayoutManager(layout); |
| 146 |
| 147 // Design the grid. |
| 148 ColumnSet* column_set = layout->AddColumnSet(column_set_id); |
| 149 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 1, |
| 150 GridLayout::FIXED, 300, 0); |
| 151 column_set->AddPaddingColumn(0, kRelatedControlHorizontalSpacing); |
| 152 column_set->AddColumn(GridLayout::FILL, GridLayout::LEADING, 0, |
| 153 GridLayout::USE_PREF, 0, 0); |
| 154 |
| 155 // Fill the grid. |
| 156 layout->StartRow(0.05f, column_set_id); |
| 157 layout->AddView(table_view_); |
| 158 layout->AddView(&remove_button_); |
| 159 |
| 160 // Ask the database for exception data. |
| 161 table_model_.GetAllExceptionsForProfile(); |
| 162 } |
| 163 |
| 164 PasswordManagerExceptionsView::~PasswordManagerExceptionsView() { |
| 165 } |
| 166 |
| 167 void PasswordManagerExceptionsView::Layout() { |
| 168 GetLayoutManager()->Layout(this); |
| 169 |
| 170 // Manually lay out the Remove All button in the same row as |
| 171 // the close button. |
| 172 gfx::Rect parent_bounds = GetParent()->GetLocalBounds(false); |
| 173 gfx::Size prefsize = remove_all_button_.GetPreferredSize(); |
| 174 int button_y = |
| 175 parent_bounds.bottom() - prefsize.height() - kButtonVEdgeMargin; |
| 176 remove_all_button_.SetBounds(kPanelHorizMargin, button_y, prefsize.width(), |
| 177 prefsize.height()); |
| 178 } |
| 179 |
| 180 gfx::Size PasswordManagerExceptionsView::GetPreferredSize() { |
| 181 return gfx::Size(kDefaultWindowWidth, kDefaultWindowHeight); |
| 182 } |
| 183 |
| 184 void PasswordManagerExceptionsView::ViewHierarchyChanged(bool is_add, |
| 185 views::View* parent, |
| 186 views::View* child) { |
| 187 if (child == this) { |
| 188 // Add and remove the Remove All button from the ClientView's hierarchy. |
| 189 if (is_add) { |
| 190 parent->AddChildView(&remove_all_button_); |
| 191 } else { |
| 192 parent->RemoveChildView(&remove_all_button_); |
| 193 } |
| 194 } |
| 195 } |
| 196 |
| 197 void PasswordManagerExceptionsView::OnSelectionChanged() { |
| 198 bool has_selection = table_view_->SelectedRowCount() > 0; |
| 199 remove_button_.SetEnabled(has_selection); |
| 200 } |
| 201 |
| 202 int PasswordManagerExceptionsView::GetDialogButtons() const { |
| 203 return DIALOGBUTTON_CANCEL; |
| 204 } |
| 205 |
| 206 std::wstring PasswordManagerExceptionsView::GetWindowTitle() const { |
| 207 return l10n_util::GetString(IDS_PASSWORD_MANAGER_EXCEPTIONS_VIEW_TITLE); |
| 208 } |
| 209 |
| 210 void PasswordManagerExceptionsView::ButtonPressed(views::NativeButton* sender) { |
| 211 DCHECK(window()); |
| 212 // Close will result in our destruction. |
| 213 if (sender == &remove_all_button_) { |
| 214 table_model_.ForgetAndRemoveAllSignons(); |
| 215 return; |
| 216 } |
| 217 |
| 218 // The following require a selection (and only one, since table is single- |
| 219 // select only). |
| 220 views::TableSelectionIterator iter = table_view_->SelectionBegin(); |
| 221 int row = *iter; |
| 222 PasswordForm* selected = table_model_.GetPasswordFormAt(row); |
| 223 DCHECK(++iter == table_view_->SelectionEnd()); |
| 224 |
| 225 if (sender == &remove_button_) { |
| 226 table_model_.ForgetAndRemoveSignon(row); |
| 227 } else { |
| 228 NOTREACHED() << "Invalid button."; |
| 229 } |
| 230 } |
| 231 |
| 232 void PasswordManagerExceptionsView::WindowClosing() { |
| 233 // The table model will be deleted before the table view, so detach it. |
| 234 table_view_->SetModel(NULL); |
| 235 |
| 236 // Clear the static instance so the next time Show() is called, a new |
| 237 // instance is created. |
| 238 instance_ = NULL; |
| 239 } |
| 240 |
| 241 views::View* PasswordManagerExceptionsView::GetContentsView() { |
| 242 return this; |
| 243 } |
OLD | NEW |