OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/exception_editor_view.h" | 5 #include "chrome/browser/views/options/exception_editor_view.h" |
6 | 6 |
7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "app/resource_bundle.h" |
8 #include "chrome/browser/host_content_settings_map.h" | 9 #include "chrome/browser/host_content_settings_map.h" |
9 #include "chrome/browser/views/options/content_exceptions_table_model.h" | 10 #include "chrome/browser/views/options/content_exceptions_table_model.h" |
10 #include "googleurl/src/url_canon.h" | 11 #include "googleurl/src/url_canon.h" |
11 #include "googleurl/src/url_parse.h" | 12 #include "googleurl/src/url_parse.h" |
| 13 #include "grit/app_resources.h" |
12 #include "grit/generated_resources.h" | 14 #include "grit/generated_resources.h" |
13 #include "net/base/net_util.h" | 15 #include "net/base/net_util.h" |
14 #include "views/grid_layout.h" | 16 #include "views/grid_layout.h" |
15 #include "views/controls/label.h" | 17 #include "views/controls/label.h" |
| 18 #include "views/controls/image_view.h" |
16 #include "views/standard_layout.h" | 19 #include "views/standard_layout.h" |
17 #include "views/window/window.h" | 20 #include "views/window/window.h" |
18 | 21 |
| 22 namespace { |
| 23 |
19 // The settings shown in the combobox if show_ask_ is false; | 24 // The settings shown in the combobox if show_ask_ is false; |
20 static const ContentSetting kNoAskSettings[] = { CONTENT_SETTING_ALLOW, | 25 const ContentSetting kNoAskSettings[] = { CONTENT_SETTING_ALLOW, |
21 CONTENT_SETTING_BLOCK }; | 26 CONTENT_SETTING_BLOCK }; |
22 | 27 |
23 // The settings shown in the combobox if show_ask_ is true; | 28 // The settings shown in the combobox if show_ask_ is true; |
24 static const ContentSetting kAskSettings[] = { CONTENT_SETTING_ALLOW, | 29 const ContentSetting kAskSettings[] = { CONTENT_SETTING_ALLOW, |
25 CONTENT_SETTING_ASK, | 30 CONTENT_SETTING_ASK, |
26 CONTENT_SETTING_BLOCK }; | 31 CONTENT_SETTING_BLOCK }; |
27 | 32 |
28 // Returns true if the host name is valid. | 33 // Returns true if the host name is valid. |
29 static bool ValidHost(const std::string& host) { | 34 bool ValidHost(const std::string& host) { |
30 if (host.empty()) | 35 if (host.empty()) |
31 return false; | 36 return false; |
32 | 37 |
33 url_canon::CanonHostInfo host_info; | 38 url_canon::CanonHostInfo host_info; |
34 return !net::CanonicalizeHost(host, &host_info).empty(); | 39 return !net::CanonicalizeHost(host, &host_info).empty(); |
35 } | 40 } |
36 | 41 |
| 42 } // namespace |
| 43 |
37 int ExceptionEditorView::ActionComboboxModel::GetItemCount() { | 44 int ExceptionEditorView::ActionComboboxModel::GetItemCount() { |
38 return show_ask_ ? arraysize(kAskSettings) : arraysize(kNoAskSettings); | 45 return show_ask_ ? arraysize(kAskSettings) : arraysize(kNoAskSettings); |
39 } | 46 } |
40 | 47 |
41 std::wstring ExceptionEditorView::ActionComboboxModel::GetItemAt(int index) { | 48 std::wstring ExceptionEditorView::ActionComboboxModel::GetItemAt(int index) { |
42 switch (setting_for_index(index)) { | 49 switch (setting_for_index(index)) { |
43 case CONTENT_SETTING_ALLOW: | 50 case CONTENT_SETTING_ALLOW: |
44 return l10n_util::GetString(IDS_EXCEPTIONS_ALLOW_BUTTON); | 51 return l10n_util::GetString(IDS_EXCEPTIONS_ALLOW_BUTTON); |
45 case CONTENT_SETTING_BLOCK: | 52 case CONTENT_SETTING_BLOCK: |
46 return l10n_util::GetString(IDS_EXCEPTIONS_BLOCK_BUTTON); | 53 return l10n_util::GetString(IDS_EXCEPTIONS_BLOCK_BUTTON); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 } | 101 } |
95 | 102 |
96 std::wstring ExceptionEditorView::GetWindowTitle() const { | 103 std::wstring ExceptionEditorView::GetWindowTitle() const { |
97 return is_new() ? l10n_util::GetString(IDS_EXCEPTION_EDITOR_NEW_TITLE) : | 104 return is_new() ? l10n_util::GetString(IDS_EXCEPTION_EDITOR_NEW_TITLE) : |
98 l10n_util::GetString(IDS_EXCEPTION_EDITOR_TITLE); | 105 l10n_util::GetString(IDS_EXCEPTION_EDITOR_TITLE); |
99 } | 106 } |
100 | 107 |
101 bool ExceptionEditorView::IsDialogButtonEnabled( | 108 bool ExceptionEditorView::IsDialogButtonEnabled( |
102 MessageBoxFlags::DialogButton button) const { | 109 MessageBoxFlags::DialogButton button) const { |
103 if (button == MessageBoxFlags::DIALOGBUTTON_OK) { | 110 if (button == MessageBoxFlags::DIALOGBUTTON_OK) { |
104 std::string new_host = UTF16ToUTF8(host_tf_->text()); | 111 return IsHostValid(UTF16ToUTF8(host_tf_->text())); |
105 if (is_new()) { | |
106 return ValidHost(new_host) && | |
107 (model_->IndexOfExceptionByHost(new_host) == -1); | |
108 } | |
109 return !new_host.empty() && | |
110 (host_ == new_host || | |
111 (ValidHost(new_host) && | |
112 model_->IndexOfExceptionByHost(new_host) == -1)); | |
113 } | 112 } |
114 return true; | 113 return true; |
115 } | 114 } |
116 | 115 |
117 bool ExceptionEditorView::Cancel() { | 116 bool ExceptionEditorView::Cancel() { |
118 return true; | 117 return true; |
119 } | 118 } |
120 | 119 |
121 bool ExceptionEditorView::Accept() { | 120 bool ExceptionEditorView::Accept() { |
122 std::string new_host = UTF16ToUTF8(host_tf_->text()); | 121 std::string new_host = UTF16ToUTF8(host_tf_->text()); |
123 ContentSetting setting = | 122 ContentSetting setting = |
124 cb_model_.setting_for_index(action_cb_->selected_item()); | 123 cb_model_.setting_for_index(action_cb_->selected_item()); |
125 delegate_->AcceptExceptionEdit(new_host, setting, index_, is_new()); | 124 delegate_->AcceptExceptionEdit(new_host, setting, index_, is_new()); |
126 return true; | 125 return true; |
127 } | 126 } |
128 | 127 |
129 views::View* ExceptionEditorView::GetContentsView() { | 128 views::View* ExceptionEditorView::GetContentsView() { |
130 return this; | 129 return this; |
131 } | 130 } |
132 | 131 |
133 void ExceptionEditorView::ContentsChanged(views::Textfield* sender, | 132 void ExceptionEditorView::ContentsChanged(views::Textfield* sender, |
134 const std::wstring& new_contents) { | 133 const std::wstring& new_contents) { |
135 GetDialogClientView()->UpdateDialogButtons(); | 134 GetDialogClientView()->UpdateDialogButtons(); |
| 135 UpdateImageView(host_iv_, IsHostValid(UTF16ToUTF8(host_tf_->text()))); |
136 } | 136 } |
137 | 137 |
138 bool ExceptionEditorView::HandleKeystroke( | 138 bool ExceptionEditorView::HandleKeystroke( |
139 views::Textfield* sender, | 139 views::Textfield* sender, |
140 const views::Textfield::Keystroke& key) { | 140 const views::Textfield::Keystroke& key) { |
141 return false; | 141 return false; |
142 } | 142 } |
143 | 143 |
144 void ExceptionEditorView::Init() { | 144 void ExceptionEditorView::Init() { |
145 using views::GridLayout; | 145 using views::GridLayout; |
146 | 146 |
147 host_tf_ = new views::Textfield(); | 147 host_tf_ = new views::Textfield(); |
148 host_tf_->SetText(UTF8ToUTF16(host_)); | 148 host_tf_->SetText(UTF8ToUTF16(host_)); |
149 host_tf_->SetController(this); | 149 host_tf_->SetController(this); |
150 | 150 |
| 151 host_iv_ = new views::ImageView; |
| 152 |
| 153 UpdateImageView(host_iv_, IsHostValid(UTF16ToUTF8(host_tf_->text()))); |
| 154 |
151 action_cb_ = new views::Combobox(&cb_model_); | 155 action_cb_ = new views::Combobox(&cb_model_); |
152 if (!is_new()) | 156 if (!is_new()) |
153 action_cb_->SetSelectedItem(cb_model_.index_for_setting(setting_)); | 157 action_cb_->SetSelectedItem(cb_model_.index_for_setting(setting_)); |
154 | 158 |
155 GridLayout* layout = CreatePanelGridLayout(this); | 159 GridLayout* layout = CreatePanelGridLayout(this); |
156 SetLayoutManager(layout); | 160 SetLayoutManager(layout); |
157 | 161 |
158 // For the Textfields. | 162 // For the Textfields. |
159 views::ColumnSet* column_set = layout->AddColumnSet(1); | 163 views::ColumnSet* column_set = layout->AddColumnSet(1); |
160 column_set->AddColumn(GridLayout::LEADING, GridLayout::CENTER, 0, | 164 column_set->AddColumn(GridLayout::LEADING, GridLayout::CENTER, 0, |
161 GridLayout::USE_PREF, 0, 0); | 165 GridLayout::USE_PREF, 0, 0); |
162 column_set->AddPaddingColumn(0, kRelatedControlHorizontalSpacing); | 166 column_set->AddPaddingColumn(0, kRelatedControlHorizontalSpacing); |
163 column_set->AddColumn(GridLayout::FILL, GridLayout::CENTER, 1, | 167 column_set->AddColumn(GridLayout::FILL, GridLayout::CENTER, 1, |
164 GridLayout::USE_PREF, 0, 0); | 168 GridLayout::USE_PREF, 0, 0); |
165 column_set->AddPaddingColumn(0, kRelatedControlHorizontalSpacing); | 169 column_set->AddPaddingColumn(0, kRelatedControlHorizontalSpacing); |
166 column_set->AddColumn(GridLayout::CENTER, GridLayout::CENTER, 0, | 170 column_set->AddColumn(GridLayout::CENTER, GridLayout::CENTER, 0, |
167 GridLayout::USE_PREF, 0, 0); | 171 GridLayout::USE_PREF, 0, 0); |
168 | 172 |
169 // Add the contents. | 173 // Add the contents. |
170 layout->StartRow(0, 1); | 174 layout->StartRow(0, 1); |
171 layout->AddView(CreateLabel(IDS_EXCEPTION_EDITOR_HOST_TITLE)); | 175 layout->AddView(CreateLabel(IDS_EXCEPTION_EDITOR_HOST_TITLE)); |
172 layout->AddView(host_tf_); | 176 layout->AddView(host_tf_); |
| 177 layout->AddView(host_iv_); |
173 | 178 |
174 layout->StartRowWithPadding(0, 1, 0, kRelatedControlVerticalSpacing); | 179 layout->StartRowWithPadding(0, 1, 0, kRelatedControlVerticalSpacing); |
175 layout->AddView(CreateLabel(IDS_EXCEPTION_EDITOR_ACTION_TITLE)); | 180 layout->AddView(CreateLabel(IDS_EXCEPTION_EDITOR_ACTION_TITLE)); |
176 layout->AddView(action_cb_); | 181 layout->AddView(action_cb_); |
177 } | 182 } |
178 | 183 |
179 views::Label* ExceptionEditorView::CreateLabel(int message_id) { | 184 views::Label* ExceptionEditorView::CreateLabel(int message_id) { |
180 views::Label* label = new views::Label(l10n_util::GetString(message_id)); | 185 views::Label* label = new views::Label(l10n_util::GetString(message_id)); |
181 label->SetHorizontalAlignment(views::Label::ALIGN_LEFT); | 186 label->SetHorizontalAlignment(views::Label::ALIGN_LEFT); |
182 return label; | 187 return label; |
183 } | 188 } |
| 189 |
| 190 bool ExceptionEditorView::IsHostValid(const std::string& host) const { |
| 191 bool is_valid_host = ValidHost(host) && |
| 192 (model_->IndexOfExceptionByHost(host) == -1); |
| 193 |
| 194 return is_new() ? is_valid_host : (!host.empty() && |
| 195 ((host_ == host) || is_valid_host)); |
| 196 } |
| 197 |
| 198 void ExceptionEditorView::UpdateImageView(views::ImageView* image_view, |
| 199 bool is_valid) { |
| 200 return image_view->SetImage( |
| 201 ResourceBundle::GetSharedInstance().GetBitmapNamed( |
| 202 is_valid ? IDR_INPUT_GOOD : IDR_INPUT_ALERT)); |
| 203 } |
OLD | NEW |