Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(110)

Side by Side Diff: chrome/browser/views/collected_cookies_win.cc

Issue 3108029: Display an infobar when content settings were created. (Closed)
Patch Set: updates Created 10 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/collected_cookies_win.h" 5 #include "chrome/browser/views/collected_cookies_win.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/cookies_tree_model.h" 9 #include "chrome/browser/cookies_tree_model.h"
9 #include "chrome/browser/profile.h" 10 #include "chrome/browser/profile.h"
10 #include "chrome/browser/tab_contents/tab_contents.h" 11 #include "chrome/browser/tab_contents/tab_contents.h"
11 #include "chrome/common/notification_service.h" 12 #include "chrome/common/notification_service.h"
13 #include "gfx/color_utils.h"
12 #include "grit/generated_resources.h" 14 #include "grit/generated_resources.h"
13 #include "grit/locale_settings.h" 15 #include "grit/locale_settings.h"
16 #include "grit/theme_resources.h"
17 #include "views/box_layout.h"
18 #include "views/controls/button/native_button.h"
19 #include "views/controls/image_view.h"
14 #include "views/controls/label.h" 20 #include "views/controls/label.h"
15 #include "views/controls/button/native_button.h" 21 #include "views/controls/separator.h"
16 #include "views/standard_layout.h" 22 #include "views/standard_layout.h"
23 #include "views/widget/root_view.h"
24 #include "views/widget/widget_win.h"
17 #include "views/window/window.h" 25 #include "views/window/window.h"
18 26
19 namespace browser { 27 namespace browser {
20 28
21 // Declared in browser_dialogs.h so others don't have to depend on our header. 29 // Declared in browser_dialogs.h so others don't have to depend on our header.
22 void ShowCollectedCookiesDialog(gfx::NativeWindow parent_window, 30 void ShowCollectedCookiesDialog(gfx::NativeWindow parent_window,
23 TabContents* tab_contents) { 31 TabContents* tab_contents) {
24 // Deletes itself on close. 32 // Deletes itself on close.
25 new CollectedCookiesWin(parent_window, tab_contents); 33 new CollectedCookiesWin(parent_window, tab_contents);
26 } 34 }
27 35
28 } // namespace browser 36 } // namespace browser
29 37
38 namespace {
39 // Spacing between the infobar frame and its contents.
40 const int kInfobarVerticalPadding = 3;
41 const int kInfobarHorizontalPadding = 8;
42
43 // Width of the infobar frame.
44 const int kInfobarBorderSize = 1;
45
46 // Dimensions of the tree views.
47 const int kTreeViewWidth = 400;
48 const int kTreeViewHeight = 125;
49
50 } // namespace
51
52 // A custom view that conditionally displays an infobar.
53 class InfobarView : public views::View {
54 public:
55 InfobarView() {
56 content_ = new views::View;
57 SkColor border_color = color_utils::GetSysSkColor(COLOR_3DSHADOW);
58 views::Border* border = views::Border::CreateSolidBorder(
59 kInfobarBorderSize, border_color);
60 content_->set_border(border);
61
62 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
63 info_image_ = new views::ImageView();
64 info_image_->SetImage(rb.GetBitmapNamed(IDR_INFO));
65 label_ = new views::Label();
66 }
67 virtual ~InfobarView() {}
68
69 // Update the visibility of the infobar. If |is_visible| is true, a rule for
70 // |setting| on |domain_name| was created.
71 void UpdateVisibility(bool is_visible,
72 ContentSetting setting,
73 const std::wstring& domain_name) {
74 if (!is_visible) {
75 SetVisible(false);
76 return;
77 }
78
79 std::wstring label;
80 switch (setting) {
81 case CONTENT_SETTING_BLOCK:
82 label = l10n_util::GetStringF(
83 IDS_COLLECTED_COOKIES_BLOCK_RULE_CREATED, domain_name);
84 break;
85
86 case CONTENT_SETTING_ALLOW:
87 label = l10n_util::GetStringF(
88 IDS_COLLECTED_COOKIES_ALLOW_RULE_CREATED, domain_name);
89 break;
90
91 case CONTENT_SETTING_SESSION_ONLY:
92 label = l10n_util::GetStringF(
93 IDS_COLLECTED_COOKIES_SESSION_RULE_CREATED, domain_name);
94 break;
95
96 default:
97 NOTREACHED();
98 }
99 label_->SetText(label);
100 content_->Layout();
101 SetVisible(true);
102 }
103
104 private:
105 // Initialize contents and layout.
106 void Init() {
107 AddChildView(content_);
108 content_->SetLayoutManager(
109 new views::BoxLayout(views::BoxLayout::kHorizontal,
110 kInfobarHorizontalPadding,
111 kInfobarVerticalPadding,
112 kRelatedControlSmallHorizontalSpacing));
113 content_->AddChildView(info_image_);
114 content_->AddChildView(label_);
115 UpdateVisibility(false, CONTENT_SETTING_BLOCK, std::wstring());
116 }
117
118 // views::View overrides.
119 virtual gfx::Size GetPreferredSize() {
120 if (!IsVisible())
121 return gfx::Size();
122
123 // Add space around the banner.
124 gfx::Size size(content_->GetPreferredSize());
125 size.Enlarge(0, 2 * kRelatedControlVerticalSpacing);
126 return size;
127 }
128
129 virtual void Layout() {
130 content_->SetBounds(
131 0, kRelatedControlVerticalSpacing,
132 width(), height() - kRelatedControlVerticalSpacing);
133 }
134
135 virtual void ViewHierarchyChanged(bool is_add,
136 views::View* parent,
137 views::View* child) {
138 if (is_add && child == this)
139 Init();
140 }
141
142 // Holds the info icon image and text label and renders the border.
143 views::View* content_;
144 // Info icon image.
145 views::ImageView* info_image_;
146 // The label responsible for rendering the text.
147 views::Label* label_;
148
149 DISALLOW_COPY_AND_ASSIGN(InfobarView);
150 };
30 151
31 /////////////////////////////////////////////////////////////////////////////// 152 ///////////////////////////////////////////////////////////////////////////////
32 // CollectedCookiesWin, constructor and destructor: 153 // CollectedCookiesWin, constructor and destructor:
33 154
34 CollectedCookiesWin::CollectedCookiesWin(gfx::NativeWindow parent_window, 155 CollectedCookiesWin::CollectedCookiesWin(gfx::NativeWindow parent_window,
35 TabContents* tab_contents) 156 TabContents* tab_contents)
36 : tab_contents_(tab_contents), 157 : tab_contents_(tab_contents),
37 allowed_label_(NULL), 158 allowed_label_(NULL),
38 blocked_label_(NULL), 159 blocked_label_(NULL),
39 allowed_cookies_tree_(NULL), 160 allowed_cookies_tree_(NULL),
40 blocked_cookies_tree_(NULL), 161 blocked_cookies_tree_(NULL),
41 block_allowed_button_(NULL), 162 block_allowed_button_(NULL),
42 allow_blocked_button_(NULL), 163 allow_blocked_button_(NULL),
43 for_session_blocked_button_(NULL) { 164 for_session_blocked_button_(NULL),
165 infobar_(NULL) {
44 TabSpecificContentSettings* content_settings = 166 TabSpecificContentSettings* content_settings =
45 tab_contents->GetTabSpecificContentSettings(); 167 tab_contents->GetTabSpecificContentSettings();
46 registrar_.Add(this, NotificationType::COLLECTED_COOKIES_SHOWN, 168 registrar_.Add(this, NotificationType::COLLECTED_COOKIES_SHOWN,
47 Source<TabSpecificContentSettings>(content_settings)); 169 Source<TabSpecificContentSettings>(content_settings));
48 170
49 Init(); 171 Init();
50 172
51 window_ = tab_contents_->CreateConstrainedDialog(this); 173 window_ = tab_contents_->CreateConstrainedDialog(this);
52 } 174 }
53 175
54 CollectedCookiesWin::~CollectedCookiesWin() { 176 CollectedCookiesWin::~CollectedCookiesWin() {
55 allowed_cookies_tree_->SetModel(NULL); 177 allowed_cookies_tree_->SetModel(NULL);
56 blocked_cookies_tree_->SetModel(NULL); 178 blocked_cookies_tree_->SetModel(NULL);
57 } 179 }
58 180
59 void CollectedCookiesWin::Init() { 181 void CollectedCookiesWin::Init() {
60 TabSpecificContentSettings* content_settings = 182 TabSpecificContentSettings* content_settings =
61 tab_contents_->GetTabSpecificContentSettings(); 183 tab_contents_->GetTabSpecificContentSettings();
184 HostContentSettingsMap* host_content_settings_map =
185 tab_contents_->profile()->GetHostContentSettingsMap();
62 186
63 // Allowed Cookie list. 187 // Allowed Cookie list.
64 allowed_label_ = new views::Label( 188 allowed_label_ = new views::Label(
65 l10n_util::GetString(IDS_COLLECTED_COOKIES_ALLOWED_COOKIES_LABEL)); 189 l10n_util::GetString(IDS_COLLECTED_COOKIES_ALLOWED_COOKIES_LABEL));
66 allowed_cookies_tree_model_.reset( 190 allowed_cookies_tree_model_.reset(
67 content_settings->GetAllowedCookiesTreeModel()); 191 content_settings->GetAllowedCookiesTreeModel());
68 allowed_cookies_tree_ = new views::TreeView(); 192 allowed_cookies_tree_ = new views::TreeView();
69 allowed_cookies_tree_->SetModel(allowed_cookies_tree_model_.get()); 193 allowed_cookies_tree_->SetModel(allowed_cookies_tree_model_.get());
70 allowed_cookies_tree_->SetController(this); 194 allowed_cookies_tree_->SetController(this);
71 allowed_cookies_tree_->SetRootShown(false); 195 allowed_cookies_tree_->SetRootShown(false);
72 allowed_cookies_tree_->SetEditable(false); 196 allowed_cookies_tree_->SetEditable(false);
73 allowed_cookies_tree_->set_lines_at_root(true); 197 allowed_cookies_tree_->set_lines_at_root(true);
74 allowed_cookies_tree_->set_auto_expand_children(true); 198 allowed_cookies_tree_->set_auto_expand_children(true);
75 199
76 // Blocked Cookie list. 200 // Blocked Cookie list.
77 blocked_label_ = new views::Label( 201 blocked_label_ = new views::Label(
78 l10n_util::GetString(IDS_COLLECTED_COOKIES_BLOCKED_COOKIES_LABEL)); 202 l10n_util::GetString(
203 host_content_settings_map->BlockThirdPartyCookies() ?
204 IDS_COLLECTED_COOKIES_BLOCKED_THIRD_PARTY_BLOCKING_ENABLED :
205 IDS_COLLECTED_COOKIES_BLOCKED_COOKIES_LABEL));
206 blocked_label_->SetMultiLine(true);
207 blocked_label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
79 blocked_cookies_tree_model_.reset( 208 blocked_cookies_tree_model_.reset(
80 content_settings->GetBlockedCookiesTreeModel()); 209 content_settings->GetBlockedCookiesTreeModel());
81 blocked_cookies_tree_ = new views::TreeView(); 210 blocked_cookies_tree_ = new views::TreeView();
82 blocked_cookies_tree_->SetModel(blocked_cookies_tree_model_.get()); 211 blocked_cookies_tree_->SetModel(blocked_cookies_tree_model_.get());
83 blocked_cookies_tree_->SetController(this); 212 blocked_cookies_tree_->SetController(this);
84 blocked_cookies_tree_->SetRootShown(false); 213 blocked_cookies_tree_->SetRootShown(false);
85 blocked_cookies_tree_->SetEditable(false); 214 blocked_cookies_tree_->SetEditable(false);
86 blocked_cookies_tree_->set_lines_at_root(true); 215 blocked_cookies_tree_->set_lines_at_root(true);
87 blocked_cookies_tree_->set_auto_expand_children(true); 216 blocked_cookies_tree_->set_auto_expand_children(true);
88 217
(...skipping 14 matching lines...) Expand all
103 column_set->AddPaddingColumn(0, kRelatedControlHorizontalSpacing); 232 column_set->AddPaddingColumn(0, kRelatedControlHorizontalSpacing);
104 column_set->AddColumn(GridLayout::LEADING, GridLayout::CENTER, 0, 233 column_set->AddColumn(GridLayout::LEADING, GridLayout::CENTER, 0,
105 GridLayout::USE_PREF, 0, 0); 234 GridLayout::USE_PREF, 0, 0);
106 235
107 layout->StartRow(0, single_column_layout_id); 236 layout->StartRow(0, single_column_layout_id);
108 layout->AddView(allowed_label_); 237 layout->AddView(allowed_label_);
109 238
110 layout->AddPaddingRow(0, kRelatedControlVerticalSpacing); 239 layout->AddPaddingRow(0, kRelatedControlVerticalSpacing);
111 layout->StartRow(1, single_column_layout_id); 240 layout->StartRow(1, single_column_layout_id);
112 layout->AddView( 241 layout->AddView(
113 allowed_cookies_tree_, 1, 1, GridLayout::FILL, GridLayout::FILL); 242 allowed_cookies_tree_, 1, 1, GridLayout::FILL, GridLayout::FILL,
243 kTreeViewWidth, kTreeViewHeight);
114 layout->AddPaddingRow(0, kRelatedControlVerticalSpacing); 244 layout->AddPaddingRow(0, kRelatedControlVerticalSpacing);
115 245
116 layout->StartRow(0, single_column_layout_id); 246 layout->StartRow(0, single_column_layout_id);
117 block_allowed_button_ = new views::NativeButton( 247 block_allowed_button_ = new views::NativeButton(
118 this, l10n_util::GetString(IDS_COLLECTED_COOKIES_BLOCK_BUTTON)); 248 this, l10n_util::GetString(IDS_COLLECTED_COOKIES_BLOCK_BUTTON));
119 layout->AddView( 249 layout->AddView(
120 block_allowed_button_, 1, 1, GridLayout::LEADING, GridLayout::CENTER); 250 block_allowed_button_, 1, 1, GridLayout::LEADING, GridLayout::CENTER);
121 layout->AddPaddingRow(0, kUnrelatedControlVerticalSpacing); 251 layout->AddPaddingRow(0, kUnrelatedControlVerticalSpacing);
122 252
123 layout->StartRow(0, single_column_layout_id); 253 layout->StartRow(0, single_column_layout_id);
124 layout->AddView(blocked_label_); 254 layout->AddView(
255 new views::Separator(), 1, 1, GridLayout::FILL, GridLayout::FILL);
256 layout->AddPaddingRow(0, kUnrelatedControlVerticalSpacing);
257
258 layout->StartRow(0, single_column_layout_id);
259 layout->AddView(blocked_label_, 1, 1, GridLayout::FILL, GridLayout::FILL);
125 260
126 layout->AddPaddingRow(0, kRelatedControlVerticalSpacing); 261 layout->AddPaddingRow(0, kRelatedControlVerticalSpacing);
127 layout->StartRow(1, single_column_layout_id); 262 layout->StartRow(1, single_column_layout_id);
128 layout->AddView( 263 layout->AddView(
129 blocked_cookies_tree_, 1, 1, GridLayout::FILL, GridLayout::FILL); 264 blocked_cookies_tree_, 1, 1, GridLayout::FILL, GridLayout::FILL,
265 kTreeViewWidth, kTreeViewHeight);
130 layout->AddPaddingRow(0, kRelatedControlVerticalSpacing); 266 layout->AddPaddingRow(0, kRelatedControlVerticalSpacing);
131 267
132 layout->StartRow(0, three_columns_layout_id); 268 layout->StartRow(0, three_columns_layout_id);
133 allow_blocked_button_ = new views::NativeButton( 269 allow_blocked_button_ = new views::NativeButton(
134 this, l10n_util::GetString(IDS_COLLECTED_COOKIES_ALLOW_BUTTON)); 270 this, l10n_util::GetString(IDS_COLLECTED_COOKIES_ALLOW_BUTTON));
135 layout->AddView(allow_blocked_button_); 271 layout->AddView(allow_blocked_button_);
136 for_session_blocked_button_ = new views::NativeButton( 272 for_session_blocked_button_ = new views::NativeButton(
137 this, l10n_util::GetString(IDS_COLLECTED_COOKIES_SESSION_ONLY_BUTTON)); 273 this, l10n_util::GetString(IDS_COLLECTED_COOKIES_SESSION_ONLY_BUTTON));
138 layout->AddView(for_session_blocked_button_); 274 layout->AddView(for_session_blocked_button_);
139 275
276 layout->StartRow(0, single_column_layout_id);
277 infobar_ = new InfobarView();
278 layout->AddView(infobar_, 1, 1, GridLayout::FILL, GridLayout::FILL);
279
140 EnableControls(); 280 EnableControls();
141 } 281 }
142 282
143 /////////////////////////////////////////////////////////////////////////////// 283 ///////////////////////////////////////////////////////////////////////////////
144 // ConstrainedDialogDelegate implementation. 284 // ConstrainedDialogDelegate implementation.
145 285
146 std::wstring CollectedCookiesWin::GetWindowTitle() const { 286 std::wstring CollectedCookiesWin::GetWindowTitle() const {
147 return l10n_util::GetString(IDS_COLLECTED_COOKIES_DIALOG_TITLE); 287 return l10n_util::GetString(IDS_COLLECTED_COOKIES_DIALOG_TITLE);
148 } 288 }
149 289
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 323
184 /////////////////////////////////////////////////////////////////////////////// 324 ///////////////////////////////////////////////////////////////////////////////
185 // views::View implementation. 325 // views::View implementation.
186 326
187 void CollectedCookiesWin::OnTreeViewSelectionChanged( 327 void CollectedCookiesWin::OnTreeViewSelectionChanged(
188 views::TreeView* tree_view) { 328 views::TreeView* tree_view) {
189 EnableControls(); 329 EnableControls();
190 } 330 }
191 331
192 /////////////////////////////////////////////////////////////////////////////// 332 ///////////////////////////////////////////////////////////////////////////////
193 // views::View implementation.
194
195 gfx::Size CollectedCookiesWin::GetPreferredSize() {
196 return gfx::Size(views::Window::GetLocalizedContentsSize(
197 IDS_COOKIES_DIALOG_WIDTH_CHARS,
198 IDS_COOKIES_DIALOG_HEIGHT_LINES));
199 }
200
201 ///////////////////////////////////////////////////////////////////////////////
202 // CollectedCookiesWin, private methods. 333 // CollectedCookiesWin, private methods.
203 334
204 void CollectedCookiesWin::EnableControls() { 335 void CollectedCookiesWin::EnableControls() {
205 bool enable_allowed_buttons = false; 336 bool enable_allowed_buttons = false;
206 TreeModelNode* node = allowed_cookies_tree_->GetSelectedNode(); 337 TreeModelNode* node = allowed_cookies_tree_->GetSelectedNode();
207 if (node) { 338 if (node) {
208 CookieTreeNode* cookie_node = static_cast<CookieTreeNode*>(node); 339 CookieTreeNode* cookie_node = static_cast<CookieTreeNode*>(node);
209 if (cookie_node->GetDetailedInfo().node_type == 340 if (cookie_node->GetDetailedInfo().node_type ==
210 CookieTreeNode::DetailedInfo::TYPE_ORIGIN) { 341 CookieTreeNode::DetailedInfo::TYPE_ORIGIN) {
211 enable_allowed_buttons = static_cast<CookieTreeOriginNode*>( 342 enable_allowed_buttons = static_cast<CookieTreeOriginNode*>(
(...skipping 15 matching lines...) Expand all
227 allow_blocked_button_->SetEnabled(enable_blocked_buttons); 358 allow_blocked_button_->SetEnabled(enable_blocked_buttons);
228 for_session_blocked_button_->SetEnabled(enable_blocked_buttons); 359 for_session_blocked_button_->SetEnabled(enable_blocked_buttons);
229 } 360 }
230 361
231 void CollectedCookiesWin::AddContentException(views::TreeView* tree_view, 362 void CollectedCookiesWin::AddContentException(views::TreeView* tree_view,
232 ContentSetting setting) { 363 ContentSetting setting) {
233 CookieTreeOriginNode* origin_node = 364 CookieTreeOriginNode* origin_node =
234 static_cast<CookieTreeOriginNode*>(tree_view->GetSelectedNode()); 365 static_cast<CookieTreeOriginNode*>(tree_view->GetSelectedNode());
235 origin_node->CreateContentException( 366 origin_node->CreateContentException(
236 tab_contents_->profile()->GetHostContentSettingsMap(), setting); 367 tab_contents_->profile()->GetHostContentSettingsMap(), setting);
368 infobar_->UpdateVisibility(true, setting, origin_node->GetTitle());
369 gfx::Rect bounds;
370 GetWidget()->GetBounds(&bounds, false);
371 // WidgetWin::GetBounds returns the bounds relative to the parent window,
372 // while WidgetWin::SetBounds wants screen coordinates. Do the translation
373 // here until http://crbug.com/52851 is fixed.
374 POINT topleft = {bounds.x(), bounds.y()};
375 MapWindowPoints(HWND_DESKTOP, tab_contents_->GetNativeView(), &topleft, 1);
376 gfx::Size size = GetRootView()->GetPreferredSize();
377 bounds.SetRect(topleft.x, topleft.y, size.width(), size.height());
378 GetWidget()->SetBounds(bounds);
237 } 379 }
238 380
239 /////////////////////////////////////////////////////////////////////////////// 381 ///////////////////////////////////////////////////////////////////////////////
240 // NotificationObserver implementation. 382 // NotificationObserver implementation.
241 383
242 void CollectedCookiesWin::Observe(NotificationType type, 384 void CollectedCookiesWin::Observe(NotificationType type,
243 const NotificationSource& source, 385 const NotificationSource& source,
244 const NotificationDetails& details) { 386 const NotificationDetails& details) {
245 DCHECK(type == NotificationType::COLLECTED_COOKIES_SHOWN); 387 DCHECK(type == NotificationType::COLLECTED_COOKIES_SHOWN);
246 DCHECK_EQ(Source<TabSpecificContentSettings>(source).ptr(), 388 DCHECK_EQ(Source<TabSpecificContentSettings>(source).ptr(),
247 tab_contents_->GetTabSpecificContentSettings()); 389 tab_contents_->GetTabSpecificContentSettings());
248 window_->CloseConstrainedWindow(); 390 window_->CloseConstrainedWindow();
249 } 391 }
OLDNEW
« no previous file with comments | « chrome/browser/views/collected_cookies_win.h ('k') | chrome/browser/views/options/managed_prefs_banner_view.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698