| 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/content_settings_window_view.h" | 5 #include "chrome/browser/views/options/content_settings_window_view.h" |
| 6 | 6 |
| 7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "app/resource_bundle.h" | 8 #include "app/resource_bundle.h" |
| 9 #include "base/stl_util-inl.h" |
| 9 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
| 10 #include "chrome/browser/pref_service.h" | 11 #include "chrome/browser/pref_service.h" |
| 11 #include "chrome/browser/profile.h" | 12 #include "chrome/browser/profile.h" |
| 12 #include "chrome/browser/views/options/advanced_page_view.h" | 13 #include "chrome/browser/views/options/advanced_page_view.h" |
| 13 #include "chrome/browser/views/options/content_filter_page_view.h" | 14 #include "chrome/browser/views/options/content_filter_page_view.h" |
| 14 #include "chrome/browser/views/options/cookie_filter_page_view.h" | 15 #include "chrome/browser/views/options/cookie_filter_page_view.h" |
| 15 #include "chrome/browser/views/options/general_page_view.h" | 16 #include "chrome/browser/views/options/general_page_view.h" |
| 16 #include "chrome/browser/views/options/plugin_filter_page_view.h" | 17 #include "chrome/browser/views/options/plugin_filter_page_view.h" |
| 17 #include "chrome/common/chrome_constants.h" | 18 #include "chrome/common/chrome_constants.h" |
| 18 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
| 19 #include "grit/chromium_strings.h" | 20 #include "grit/chromium_strings.h" |
| 20 #include "grit/generated_resources.h" | 21 #include "grit/generated_resources.h" |
| 21 #include "grit/locale_settings.h" | 22 #include "grit/locale_settings.h" |
| 22 #include "views/controls/tabbed_pane/tabbed_pane.h" | 23 #include "views/controls/label.h" |
| 23 #include "views/widget/root_view.h" | 24 #include "views/widget/root_view.h" |
| 24 #include "views/window/dialog_delegate.h" | 25 #include "views/window/dialog_delegate.h" |
| 25 #include "views/window/window.h" | 26 #include "views/window/window.h" |
| 26 | 27 |
| 27 static ContentSettingsWindowView* instance_ = NULL; | 28 static ContentSettingsWindowView* instance_ = NULL; |
| 28 // Content setting dialog bounds padding. | 29 // Content setting dialog bounds padding. |
| 29 static const int kDialogPadding = 7; | 30 const int kDialogPadding = 7; |
| 31 // Width of the page selector lisbox. |
| 32 const int kListboxWidth = 100; |
| 30 | 33 |
| 31 namespace browser { | 34 namespace browser { |
| 32 | 35 |
| 33 // Declared in browser_dialogs.h so others don't have to depend on our header. | 36 // Declared in browser_dialogs.h so others don't have to depend on our header. |
| 34 void ShowContentSettingsWindow(gfx::NativeWindow parent_window, | 37 void ShowContentSettingsWindow(gfx::NativeWindow parent_window, |
| 35 ContentSettingsType content_type, | 38 ContentSettingsType content_type, |
| 36 Profile* profile) { | 39 Profile* profile) { |
| 37 DCHECK(profile); | 40 DCHECK(profile); |
| 38 // If there's already an existing options window, activate it and switch to | 41 // If there's already an existing options window, activate it and switch to |
| 39 // the specified page. | 42 // the specified page. |
| 40 // TODO(beng): note this is not multi-simultaneous-profile-safe. When we care | 43 // TODO(beng): note this is not multi-simultaneous-profile-safe. When we care |
| 41 // about this case this will have to be fixed. | 44 // about this case this will have to be fixed. |
| 42 if (!instance_) { | 45 if (!instance_) { |
| 43 instance_ = new ContentSettingsWindowView(profile); | 46 instance_ = new ContentSettingsWindowView(profile); |
| 44 views::Window::CreateChromeWindow(parent_window, gfx::Rect(), instance_); | 47 views::Window::CreateChromeWindow(parent_window, gfx::Rect(), instance_); |
| 45 } | 48 } |
| 46 instance_->ShowContentSettingsTab(content_type); | 49 instance_->ShowContentSettingsTab(content_type); |
| 47 } | 50 } |
| 48 | 51 |
| 49 } // namespace browser | 52 } // namespace browser |
| 50 | 53 |
| 51 ContentSettingsWindowView::ContentSettingsWindowView(Profile* profile) | 54 ContentSettingsWindowView::ContentSettingsWindowView(Profile* profile) |
| 52 // Always show preferences for the original profile. Most state when off | 55 // Always show preferences for the original profile. Most state when off |
| 53 // the record comes from the original profile, but we explicitly use | 56 // the record comes from the original profile, but we explicitly use |
| 54 // the original profile to avoid potential problems. | 57 // the original profile to avoid potential problems. |
| 55 : tabs_(NULL), | 58 : profile_(profile->GetOriginalProfile()), |
| 56 profile_(profile->GetOriginalProfile()) { | 59 label_(NULL), |
| 60 listbox_(NULL), |
| 61 current_page_(0) { |
| 57 // We don't need to observe changes in this value. | 62 // We don't need to observe changes in this value. |
| 58 last_selected_page_.Init(prefs::kContentSettingsWindowLastTabIndex, | 63 last_selected_page_.Init(prefs::kContentSettingsWindowLastTabIndex, |
| 59 profile->GetPrefs(), NULL); | 64 profile->GetPrefs(), NULL); |
| 60 } | 65 } |
| 61 | 66 |
| 62 ContentSettingsWindowView::~ContentSettingsWindowView() { | 67 ContentSettingsWindowView::~ContentSettingsWindowView() { |
| 68 STLDeleteElements(&pages_); |
| 63 } | 69 } |
| 64 | 70 |
| 65 void ContentSettingsWindowView::ShowContentSettingsTab( | 71 void ContentSettingsWindowView::ShowContentSettingsTab( |
| 66 ContentSettingsType page) { | 72 ContentSettingsType page) { |
| 67 // This will show invisible windows and bring visible windows to the front. | 73 // This will show invisible windows and bring visible windows to the front. |
| 68 window()->Show(); | 74 window()->Show(); |
| 69 | 75 |
| 70 if (page == CONTENT_SETTINGS_TYPE_DEFAULT) { | 76 if (page == CONTENT_SETTINGS_TYPE_DEFAULT) { |
| 71 // Remember the last visited page from local state. | 77 // Remember the last visited page from local state. |
| 72 page = static_cast<ContentSettingsType>(last_selected_page_.GetValue()); | 78 page = static_cast<ContentSettingsType>(last_selected_page_.GetValue()); |
| 73 if (page == CONTENT_SETTINGS_TYPE_DEFAULT) | 79 if (page == CONTENT_SETTINGS_TYPE_DEFAULT) |
| 74 page = CONTENT_SETTINGS_TYPE_COOKIES; | 80 page = CONTENT_SETTINGS_TYPE_COOKIES; |
| 75 } | 81 } |
| 76 // If the page number is out of bounds, reset to the first tab. | 82 // If the page number is out of bounds, reset to the first tab. |
| 77 if (page < 0 || page >= tabs_->GetTabCount()) | 83 if (page < 0 || page >= listbox_->GetRowCount()) |
| 78 page = CONTENT_SETTINGS_TYPE_COOKIES; | 84 page = CONTENT_SETTINGS_TYPE_COOKIES; |
| 79 | 85 |
| 80 tabs_->SelectTabAt(static_cast<int>(page)); | 86 listbox_->SelectRow(static_cast<int>(page)); |
| 87 ShowSettingsPage(listbox_->SelectedRow()); |
| 81 } | 88 } |
| 82 | 89 |
| 83 /////////////////////////////////////////////////////////////////////////////// | 90 /////////////////////////////////////////////////////////////////////////////// |
| 84 // ContentSettingsWindowView, views::DialogDelegate implementation: | 91 // ContentSettingsWindowView, views::DialogDelegate implementation: |
| 85 | 92 |
| 86 std::wstring ContentSettingsWindowView::GetWindowTitle() const { | 93 std::wstring ContentSettingsWindowView::GetWindowTitle() const { |
| 87 return l10n_util::GetString(IDS_CONTENT_SETTINGS_TITLE); | 94 return l10n_util::GetString(IDS_CONTENT_SETTINGS_TITLE); |
| 88 } | 95 } |
| 89 | 96 |
| 90 void ContentSettingsWindowView::WindowClosing() { | 97 void ContentSettingsWindowView::WindowClosing() { |
| 91 instance_ = NULL; | 98 instance_ = NULL; |
| 92 } | 99 } |
| 93 | 100 |
| 94 bool ContentSettingsWindowView::Cancel() { | 101 bool ContentSettingsWindowView::Cancel() { |
| 95 return GetCurrentContentSettingsTabView()->CanClose(); | 102 return GetCurrentContentSettingsTabView()->CanClose(); |
| 96 } | 103 } |
| 97 | 104 |
| 98 views::View* ContentSettingsWindowView::GetContentsView() { | 105 views::View* ContentSettingsWindowView::GetContentsView() { |
| 99 return this; | 106 return this; |
| 100 } | 107 } |
| 101 | 108 |
| 102 /////////////////////////////////////////////////////////////////////////////// | 109 /////////////////////////////////////////////////////////////////////////////// |
| 103 // ContentSettingsWindowView, views::TabbedPane::Listener implementation: | 110 // ContentSettingsWindowView, views::Listbox::Listener implementation: |
| 104 | 111 |
| 105 void ContentSettingsWindowView::TabSelectedAt(int index) { | 112 void ContentSettingsWindowView::ListboxSelectionChanged( |
| 106 last_selected_page_.SetValue(index); | 113 views::Listbox* sender) { |
| 114 DCHECK_EQ(listbox_, sender); |
| 115 ShowSettingsPage(listbox_->SelectedRow()); |
| 116 last_selected_page_.SetValue(current_page_); |
| 107 } | 117 } |
| 108 | 118 |
| 109 /////////////////////////////////////////////////////////////////////////////// | 119 /////////////////////////////////////////////////////////////////////////////// |
| 110 // ContentSettingsWindowView, views::View overrides: | 120 // ContentSettingsWindowView, views::View overrides: |
| 111 | 121 |
| 112 void ContentSettingsWindowView::Layout() { | 122 void ContentSettingsWindowView::Layout() { |
| 113 tabs_->SetBounds(kDialogPadding, kDialogPadding, | 123 label_->SetBounds(kDialogPadding, |
| 114 width() - (2 * kDialogPadding), | 124 kDialogPadding, |
| 115 height() - (2 * kDialogPadding)); | 125 kListboxWidth, |
| 126 label_->GetPreferredSize().height()); |
| 127 |
| 128 listbox_->SetBounds(kDialogPadding, |
| 129 2 * kDialogPadding + label_->height(), |
| 130 kListboxWidth, |
| 131 height() - (3 * kDialogPadding) - label_->height()); |
| 132 |
| 133 if (pages_[current_page_]->GetParent()) { |
| 134 pages_[current_page_]->SetBounds( |
| 135 2 * kDialogPadding + kListboxWidth, |
| 136 2 * kDialogPadding + label_->height(), |
| 137 width() - (3 * kDialogPadding) - kListboxWidth, |
| 138 height() - (2 * kDialogPadding)); |
| 139 } |
| 116 } | 140 } |
| 117 | 141 |
| 118 gfx::Size ContentSettingsWindowView::GetPreferredSize() { | 142 gfx::Size ContentSettingsWindowView::GetPreferredSize() { |
| 119 return gfx::Size(views::Window::GetLocalizedContentsSize( | 143 return gfx::Size(views::Window::GetLocalizedContentsSize( |
| 120 IDS_CONTENT_SETTINGS_DIALOG_WIDTH_CHARS, | 144 IDS_CONTENT_SETTINGS_DIALOG_WIDTH_CHARS, |
| 121 IDS_CONTENT_SETTINGS_DIALOG_HEIGHT_LINES)); | 145 IDS_CONTENT_SETTINGS_DIALOG_HEIGHT_LINES)); |
| 122 } | 146 } |
| 123 | 147 |
| 124 void ContentSettingsWindowView::ViewHierarchyChanged(bool is_add, | 148 void ContentSettingsWindowView::ViewHierarchyChanged(bool is_add, |
| 125 views::View* parent, | 149 views::View* parent, |
| 126 views::View* child) { | 150 views::View* child) { |
| 127 // Can't init before we're inserted into a Container, because we require a | 151 // Can't init before we're inserted into a Container, because we require a |
| 128 // HWND to parent native child controls to. | 152 // HWND to parent native child controls to. |
| 129 if (is_add && child == this) | 153 if (is_add && child == this) |
| 130 Init(); | 154 Init(); |
| 131 } | 155 } |
| 132 | 156 |
| 133 /////////////////////////////////////////////////////////////////////////////// | 157 /////////////////////////////////////////////////////////////////////////////// |
| 134 // ContentSettingsWindowView, private: | 158 // ContentSettingsWindowView, private: |
| 135 | 159 |
| 136 void ContentSettingsWindowView::Init() { | 160 void ContentSettingsWindowView::Init() { |
| 137 // Make sure we don't leak memory by calling this twice. | 161 // Make sure we don't leak memory by calling this twice. |
| 138 DCHECK(!tabs_); | 162 DCHECK(!listbox_); |
| 139 tabs_ = new views::TabbedPane; | |
| 140 tabs_->SetListener(this); | |
| 141 AddChildView(tabs_); | |
| 142 int tab_index = 0; | |
| 143 | 163 |
| 144 CookieFilterPageView* cookie_page = new CookieFilterPageView(profile_); | 164 label_ = new views::Label( |
| 145 tabs_->AddTabAtIndex(tab_index++, | 165 l10n_util::GetStringUTF16(IDS_CONTENT_SETTINGS_FEATURES_LABEL)); |
| 146 l10n_util::GetString(IDS_COOKIES_TAB_LABEL), | 166 label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); |
| 147 cookie_page, false); | 167 AddChildView(label_); |
| 148 | 168 |
| 149 ContentFilterPageView* image_page = | 169 pages_.push_back(new CookieFilterPageView(profile_)); |
| 150 new ContentFilterPageView(profile_, CONTENT_SETTINGS_TYPE_IMAGES); | 170 pages_.push_back( |
| 151 tabs_->AddTabAtIndex(tab_index++, | 171 new ContentFilterPageView(profile_, CONTENT_SETTINGS_TYPE_IMAGES)); |
| 152 l10n_util::GetString(IDS_IMAGES_TAB_LABEL), | 172 pages_.push_back( |
| 153 image_page, false); | 173 new ContentFilterPageView(profile_, CONTENT_SETTINGS_TYPE_JAVASCRIPT)); |
| 174 pages_.push_back(new PluginFilterPageView(profile_)); |
| 175 pages_.push_back( |
| 176 new ContentFilterPageView(profile_, CONTENT_SETTINGS_TYPE_POPUPS)); |
| 177 pages_.push_back( |
| 178 new ContentFilterPageView(profile_, CONTENT_SETTINGS_TYPE_GEOLOCATION)); |
| 179 for (size_t i = 0; i < pages_.size(); ++i) { |
| 180 pages_[i]->set_parent_owned(false); |
| 181 } |
| 182 DCHECK_EQ(static_cast<int>(pages_.size()), CONTENT_SETTINGS_NUM_TYPES); |
| 154 | 183 |
| 155 ContentFilterPageView* javascript_page = | 184 std::vector<string16> strings; |
| 156 new ContentFilterPageView(profile_, CONTENT_SETTINGS_TYPE_JAVASCRIPT); | 185 strings.push_back(l10n_util::GetStringUTF16(IDS_COOKIES_TAB_LABEL)); |
| 157 tabs_->AddTabAtIndex(tab_index++, | 186 strings.push_back(l10n_util::GetStringUTF16(IDS_IMAGES_TAB_LABEL)); |
| 158 l10n_util::GetString(IDS_JAVASCRIPT_TAB_LABEL), | 187 strings.push_back(l10n_util::GetStringUTF16(IDS_JAVASCRIPT_TAB_LABEL)); |
| 159 javascript_page, false); | 188 strings.push_back(l10n_util::GetStringUTF16(IDS_PLUGIN_TAB_LABEL)); |
| 189 strings.push_back(l10n_util::GetStringUTF16(IDS_POPUP_TAB_LABEL)); |
| 190 strings.push_back(l10n_util::GetStringUTF16(IDS_GEOLOCATION_TAB_LABEL)); |
| 191 listbox_ = new views::Listbox(strings, this); |
| 192 AddChildView(listbox_); |
| 193 CHECK_EQ(strings.size(), pages_.size()); |
| 194 } |
| 160 | 195 |
| 161 PluginFilterPageView* plugin_page = new PluginFilterPageView(profile_); | 196 void ContentSettingsWindowView::ShowSettingsPage(int page) { |
| 162 tabs_->AddTabAtIndex(tab_index++, | 197 if (pages_[current_page_]->GetParent()) |
| 163 l10n_util::GetString(IDS_PLUGIN_TAB_LABEL), | 198 RemoveChildView(pages_[current_page_]); |
| 164 plugin_page, false); | 199 current_page_ = page; |
| 165 | 200 AddChildView(pages_[current_page_]); |
| 166 ContentFilterPageView* popup_page = | 201 Layout(); |
| 167 new ContentFilterPageView(profile_, CONTENT_SETTINGS_TYPE_POPUPS); | 202 SchedulePaint(); |
| 168 tabs_->AddTabAtIndex(tab_index++, | |
| 169 l10n_util::GetString(IDS_POPUP_TAB_LABEL), | |
| 170 popup_page, false); | |
| 171 | |
| 172 ContentFilterPageView* geolocation_page = | |
| 173 new ContentFilterPageView(profile_, CONTENT_SETTINGS_TYPE_GEOLOCATION); | |
| 174 tabs_->AddTabAtIndex(tab_index++, | |
| 175 l10n_util::GetString(IDS_GEOLOCATION_TAB_LABEL), | |
| 176 geolocation_page, false); | |
| 177 | |
| 178 DCHECK_EQ(tabs_->GetTabCount(), CONTENT_SETTINGS_NUM_TYPES); | |
| 179 } | 203 } |
| 180 | 204 |
| 181 const OptionsPageView* | 205 const OptionsPageView* |
| 182 ContentSettingsWindowView::GetCurrentContentSettingsTabView() const { | 206 ContentSettingsWindowView::GetCurrentContentSettingsTabView() const { |
| 183 return static_cast<OptionsPageView*>(tabs_->GetSelectedTab()); | 207 return static_cast<OptionsPageView*>(pages_[current_page_]); |
| 184 } | 208 } |
| OLD | NEW |