| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/views/cookie_prompt_view.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "app/l10n_util.h" | |
| 10 #include "base/message_loop.h" | |
| 11 #include "base/utf_string_conversions.h" | |
| 12 #include "chrome/browser/browser_process.h" | |
| 13 #include "chrome/browser/cookie_modal_dialog.h" | |
| 14 #include "chrome/browser/prefs/pref_service.h" | |
| 15 #include "chrome/browser/profile.h" | |
| 16 #include "chrome/browser/views/browser_dialogs.h" | |
| 17 #include "chrome/browser/views/cookie_info_view.h" | |
| 18 #include "chrome/browser/views/database_open_info_view.h" | |
| 19 #include "chrome/browser/views/generic_info_view.h" | |
| 20 #include "chrome/browser/views/local_storage_set_item_info_view.h" | |
| 21 #include "chrome/browser/views/options/content_settings_window_view.h" | |
| 22 #include "chrome/common/pref_names.h" | |
| 23 #include "gfx/canvas.h" | |
| 24 #include "gfx/color_utils.h" | |
| 25 #include "grit/generated_resources.h" | |
| 26 #include "grit/locale_settings.h" | |
| 27 #include "net/base/cookie_monster.h" | |
| 28 #include "views/controls/label.h" | |
| 29 #include "views/controls/button/native_button.h" | |
| 30 #include "views/controls/button/radio_button.h" | |
| 31 #include "views/controls/textfield/textfield.h" | |
| 32 #include "views/grid_layout.h" | |
| 33 #include "views/standard_layout.h" | |
| 34 #include "views/window/non_client_view.h" | |
| 35 | |
| 36 static const int kCookiePromptViewInsetSize = 5; | |
| 37 | |
| 38 /////////////////////////////////////////////////////////////////////////////// | |
| 39 // CookiePromptView, public: | |
| 40 | |
| 41 CookiePromptView::CookiePromptView( | |
| 42 CookiePromptModalDialog* parent, | |
| 43 gfx::NativeWindow root_window, | |
| 44 Profile* profile) | |
| 45 : remember_radio_(NULL), | |
| 46 ask_radio_(NULL), | |
| 47 allow_button_(NULL), | |
| 48 block_button_(NULL), | |
| 49 show_cookie_link_(NULL), | |
| 50 info_view_(NULL), | |
| 51 session_expire_(false), | |
| 52 expanded_view_(false), | |
| 53 signaled_(false), | |
| 54 parent_(parent), | |
| 55 root_window_(root_window), | |
| 56 profile_(profile) { | |
| 57 InitializeViewResources(); | |
| 58 expanded_view_ = profile_->GetPrefs()-> | |
| 59 GetBoolean(prefs::kCookiePromptExpanded); | |
| 60 } | |
| 61 | |
| 62 CookiePromptView::~CookiePromptView() { | |
| 63 } | |
| 64 | |
| 65 /////////////////////////////////////////////////////////////////////////////// | |
| 66 // CookiePromptView, views::View overrides: | |
| 67 | |
| 68 gfx::Size CookiePromptView::GetPreferredSize() { | |
| 69 gfx::Size client_size = views::View::GetPreferredSize(); | |
| 70 return gfx::Size(client_size.width(), | |
| 71 client_size.height() + GetExtendedViewHeight()); | |
| 72 } | |
| 73 | |
| 74 | |
| 75 void CookiePromptView::ViewHierarchyChanged(bool is_add, | |
| 76 views::View* parent, | |
| 77 views::View* child) { | |
| 78 if (is_add && child == this) | |
| 79 Init(); | |
| 80 } | |
| 81 | |
| 82 /////////////////////////////////////////////////////////////////////////////// | |
| 83 // CookiePromptView, ModalDialogDelegate implementation: | |
| 84 | |
| 85 gfx::NativeWindow CookiePromptView::GetDialogRootWindow() { | |
| 86 return root_window_; | |
| 87 } | |
| 88 | |
| 89 /////////////////////////////////////////////////////////////////////////////// | |
| 90 // CookiePromptView, views::DialogDelegate implementation: | |
| 91 | |
| 92 std::wstring CookiePromptView::GetWindowTitle() const { | |
| 93 return title_; | |
| 94 } | |
| 95 | |
| 96 void CookiePromptView::WindowClosing() { | |
| 97 if (!signaled_) | |
| 98 parent_->BlockSiteData(false); | |
| 99 parent_->CompleteDialog(); | |
| 100 } | |
| 101 | |
| 102 views::View* CookiePromptView::GetContentsView() { | |
| 103 return this; | |
| 104 } | |
| 105 | |
| 106 bool CookiePromptView::Accept() { | |
| 107 parent_->AllowSiteData(remember_radio_->checked(), session_expire_); | |
| 108 signaled_ = true; | |
| 109 return true; | |
| 110 } | |
| 111 | |
| 112 // CookieInfoViewDelegate overrides: | |
| 113 void CookiePromptView::ModifyExpireDate(bool session_expire) { | |
| 114 session_expire_ = session_expire; | |
| 115 } | |
| 116 | |
| 117 | |
| 118 /////////////////////////////////////////////////////////////////////////////// | |
| 119 // CookiePromptView, views::ButtonListener implementation: | |
| 120 | |
| 121 void CookiePromptView::ButtonPressed(views::Button* sender, | |
| 122 const views::Event& event) { | |
| 123 if (sender == allow_button_) { | |
| 124 Accept(); | |
| 125 GetWindow()->Close(); | |
| 126 } else if (sender == block_button_) { | |
| 127 parent_->BlockSiteData(remember_radio_->checked()); | |
| 128 signaled_ = true; | |
| 129 GetWindow()->Close(); | |
| 130 } | |
| 131 } | |
| 132 | |
| 133 /////////////////////////////////////////////////////////////////////////////// | |
| 134 // CookiePromptView, views::LinkController implementation: | |
| 135 void CookiePromptView::LinkActivated(views::Link* source, int event_flags) { | |
| 136 DCHECK_EQ(source, show_cookie_link_); | |
| 137 ToggleDetailsViewExpand(); | |
| 138 } | |
| 139 | |
| 140 /////////////////////////////////////////////////////////////////////////////// | |
| 141 // CookiePromptView, private: | |
| 142 | |
| 143 void CookiePromptView::Init() { | |
| 144 CookiePromptModalDialog::DialogType type = parent_->dialog_type(); | |
| 145 std::wstring display_host = UTF8ToWide(parent_->origin().host()); | |
| 146 views::Label* description_label = new views::Label(l10n_util::GetStringF( | |
| 147 type == CookiePromptModalDialog::DIALOG_TYPE_COOKIE ? | |
| 148 IDS_COOKIE_ALERT_LABEL : IDS_DATA_ALERT_LABEL, | |
| 149 display_host)); | |
| 150 int radio_group_id = 0; | |
| 151 remember_radio_ = new views::RadioButton( | |
| 152 l10n_util::GetStringF(IDS_COOKIE_ALERT_REMEMBER_RADIO, display_host), | |
| 153 radio_group_id); | |
| 154 remember_radio_->set_listener(this); | |
| 155 ask_radio_ = new views::RadioButton( | |
| 156 l10n_util::GetString(IDS_COOKIE_ALERT_ASK_RADIO), radio_group_id); | |
| 157 ask_radio_->set_listener(this); | |
| 158 allow_button_ = new views::NativeButton( | |
| 159 this, l10n_util::GetString(IDS_COOKIE_ALERT_ALLOW_BUTTON)); | |
| 160 block_button_ = new views::NativeButton( | |
| 161 this, l10n_util::GetString(IDS_COOKIE_ALERT_BLOCK_BUTTON)); | |
| 162 show_cookie_link_ = new views::Link( | |
| 163 l10n_util::GetString(IDS_COOKIE_SHOW_DETAILS_LABEL)); | |
| 164 show_cookie_link_->SetController(this); | |
| 165 | |
| 166 using views::GridLayout; | |
| 167 | |
| 168 GridLayout* layout = CreatePanelGridLayout(this); | |
| 169 layout->SetInsets(kCookiePromptViewInsetSize, kCookiePromptViewInsetSize, | |
| 170 kCookiePromptViewInsetSize, kCookiePromptViewInsetSize); | |
| 171 SetLayoutManager(layout); | |
| 172 | |
| 173 const int one_column_layout_id = 0; | |
| 174 views::ColumnSet* one_column_set = layout->AddColumnSet(one_column_layout_id); | |
| 175 one_column_set->AddPaddingColumn(0, kRelatedControlHorizontalSpacing); | |
| 176 one_column_set->AddColumn(GridLayout::LEADING, GridLayout::CENTER, 0, | |
| 177 GridLayout::USE_PREF, 0, 0); | |
| 178 one_column_set->AddPaddingColumn(0, kRelatedControlHorizontalSpacing); | |
| 179 layout->AddPaddingRow(0, kRelatedControlVerticalSpacing); | |
| 180 layout->StartRow(0, one_column_layout_id); | |
| 181 layout->AddView(description_label); | |
| 182 layout->AddPaddingRow(0, kUnrelatedControlVerticalSpacing); | |
| 183 layout->StartRow(0, one_column_layout_id); | |
| 184 layout->AddView(remember_radio_); | |
| 185 layout->AddPaddingRow(0, kRelatedControlVerticalSpacing); | |
| 186 layout->StartRow(0, one_column_layout_id); | |
| 187 layout->AddView(ask_radio_); | |
| 188 layout->AddPaddingRow(0, kRelatedControlVerticalSpacing); | |
| 189 | |
| 190 View* button_container = new View(); | |
| 191 GridLayout* button_layout = new GridLayout(button_container); | |
| 192 button_container->SetLayoutManager(button_layout); | |
| 193 const int inner_column_layout_id = 1; | |
| 194 views::ColumnSet* inner_column_set = button_layout->AddColumnSet( | |
| 195 inner_column_layout_id); | |
| 196 inner_column_set->AddColumn(GridLayout::FILL, GridLayout::CENTER, 1, | |
| 197 GridLayout::USE_PREF, 0, 0); | |
| 198 inner_column_set->AddPaddingColumn(0, kRelatedControlHorizontalSpacing); | |
| 199 inner_column_set->AddColumn(GridLayout::LEADING, GridLayout::CENTER, 0, | |
| 200 GridLayout::USE_PREF, 0, 0); | |
| 201 inner_column_set->AddPaddingColumn(0, kRelatedControlHorizontalSpacing); | |
| 202 inner_column_set->AddColumn(GridLayout::LEADING, GridLayout::CENTER, 0, | |
| 203 GridLayout::USE_PREF, 0, 0); | |
| 204 button_layout->StartRow(0, inner_column_layout_id); | |
| 205 button_layout->AddView(show_cookie_link_, 1, 1, | |
| 206 GridLayout::LEADING, GridLayout::TRAILING); | |
| 207 button_layout->AddView(allow_button_); | |
| 208 button_layout->AddView(block_button_); | |
| 209 | |
| 210 int button_column_layout_id = 2; | |
| 211 views::ColumnSet* button_column_set = | |
| 212 layout->AddColumnSet(button_column_layout_id); | |
| 213 button_column_set->AddPaddingColumn(0, kRelatedControlHorizontalSpacing); | |
| 214 button_column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 0, | |
| 215 GridLayout::USE_PREF, 0, 0); | |
| 216 button_column_set->AddPaddingColumn(0, kRelatedControlHorizontalSpacing); | |
| 217 layout->StartRow(0, one_column_layout_id); | |
| 218 layout->AddView(button_container, 1, 1, | |
| 219 GridLayout::FILL, GridLayout::CENTER); | |
| 220 layout->AddPaddingRow(0, kRelatedControlVerticalSpacing); | |
| 221 | |
| 222 layout->StartRow(0, one_column_layout_id); | |
| 223 | |
| 224 if (type == CookiePromptModalDialog::DIALOG_TYPE_COOKIE) { | |
| 225 CookieInfoView* cookie_info_view = new CookieInfoView(true); | |
| 226 cookie_info_view->set_delegate(this); | |
| 227 layout->AddView(cookie_info_view, 1, 1, GridLayout::FILL, | |
| 228 GridLayout::CENTER); | |
| 229 | |
| 230 cookie_info_view->SetCookieString(parent_->origin(), | |
| 231 parent_->cookie_line()); | |
| 232 info_view_ = cookie_info_view; | |
| 233 } else if (type == CookiePromptModalDialog::DIALOG_TYPE_LOCAL_STORAGE) { | |
| 234 LocalStorageSetItemInfoView* view = new LocalStorageSetItemInfoView(); | |
| 235 layout->AddView(view, 1, 1, GridLayout::FILL, GridLayout::CENTER); | |
| 236 view->SetFields(parent_->origin().host(), | |
| 237 parent_->local_storage_key(), | |
| 238 parent_->local_storage_value()); | |
| 239 info_view_ = view; | |
| 240 } else if (type == CookiePromptModalDialog::DIALOG_TYPE_DATABASE) { | |
| 241 DatabaseOpenInfoView* view = new DatabaseOpenInfoView(); | |
| 242 layout->AddView(view, 1, 1, GridLayout::FILL, GridLayout::CENTER); | |
| 243 view->SetFields(parent_->origin().host(), | |
| 244 parent_->database_name(), | |
| 245 parent_->display_name(), | |
| 246 parent_->estimated_size()); | |
| 247 info_view_ = view; | |
| 248 } else if (type == CookiePromptModalDialog::DIALOG_TYPE_APPCACHE) { | |
| 249 static const int kAppCacheInfoLabels[] = { | |
| 250 IDS_COOKIES_APPLICATION_CACHE_MANIFEST_LABEL | |
| 251 }; | |
| 252 GenericInfoView* view = new GenericInfoView(ARRAYSIZE(kAppCacheInfoLabels), | |
| 253 kAppCacheInfoLabels); | |
| 254 layout->AddView(view, 1, 1, GridLayout::FILL, GridLayout::CENTER); | |
| 255 view->SetValue(0, UTF8ToUTF16(parent_->appcache_manifest_url().spec())); | |
| 256 info_view_ = view; | |
| 257 } else { | |
| 258 NOTIMPLEMENTED(); | |
| 259 } | |
| 260 | |
| 261 info_view_->SetVisible(expanded_view_); | |
| 262 | |
| 263 // Set default values. | |
| 264 remember_radio_->SetChecked(true); | |
| 265 } | |
| 266 | |
| 267 int CookiePromptView::GetExtendedViewHeight() { | |
| 268 DCHECK(info_view_); | |
| 269 return expanded_view_ ? | |
| 270 0 : -info_view_->GetPreferredSize().height(); | |
| 271 } | |
| 272 | |
| 273 void CookiePromptView::ToggleDetailsViewExpand() { | |
| 274 int old_extended_height = GetExtendedViewHeight(); | |
| 275 | |
| 276 expanded_view_ = !expanded_view_; | |
| 277 profile_->GetPrefs()->SetBoolean(prefs::kCookiePromptExpanded, | |
| 278 expanded_view_); | |
| 279 | |
| 280 // We have to set the visbility before asking for the extended view height | |
| 281 // again as there is a bug in combobox that results in preferred height | |
| 282 // changing when visible and not visible. | |
| 283 info_view_->SetVisible(expanded_view_); | |
| 284 int extended_height_delta = GetExtendedViewHeight() - old_extended_height; | |
| 285 views::Window* window = GetWindow(); | |
| 286 gfx::Rect bounds = window->GetBounds(); | |
| 287 bounds.set_height(bounds.height() + extended_height_delta); | |
| 288 window->SetBounds(bounds, NULL); | |
| 289 } | |
| 290 | |
| 291 void CookiePromptView::InitializeViewResources() { | |
| 292 CookiePromptModalDialog::DialogType type = parent_->dialog_type(); | |
| 293 title_ = l10n_util::GetStringF( | |
| 294 type == CookiePromptModalDialog::DIALOG_TYPE_COOKIE ? | |
| 295 IDS_COOKIE_ALERT_TITLE : IDS_DATA_ALERT_TITLE, | |
| 296 UTF8ToWide(parent_->origin().host())); | |
| 297 } | |
| OLD | NEW |