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

Side by Side Diff: chrome/browser/cocoa/collected_cookies_mac.mm

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 #import "chrome/browser/cocoa/collected_cookies_mac.h" 5 #import "chrome/browser/cocoa/collected_cookies_mac.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "app/l10n_util_mac.h" 9 #include "app/l10n_util_mac.h"
10 #include "app/resource_bundle.h" 10 #include "app/resource_bundle.h"
11 #import "base/mac_util.h" 11 #import "base/mac_util.h"
12 #include "base/sys_string_conversions.h" 12 #include "base/sys_string_conversions.h"
13 #include "chrome/browser/cocoa/content_settings_dialog_controller.h" 13 #import "chrome/browser/cocoa/vertical_gradient_view.h"
14 #include "chrome/browser/profile.h" 14 #include "chrome/browser/profile.h"
15 #include "chrome/browser/tab_contents/tab_contents.h" 15 #include "chrome/browser/tab_contents/tab_contents.h"
16 #include "chrome/common/notification_service.h" 16 #include "chrome/common/notification_service.h"
17 #include "grit/generated_resources.h" 17 #include "grit/generated_resources.h"
18 #include "grit/theme_resources.h" 18 #include "grit/theme_resources.h"
19 #include "skia/ext/skia_utils_mac.h" 19 #include "skia/ext/skia_utils_mac.h"
20 #include "third_party/apple/ImageAndTextCell.h" 20 #include "third_party/apple/ImageAndTextCell.h"
21 #include "third_party/skia/include/core/SkBitmap.h" 21 #include "third_party/skia/include/core/SkBitmap.h"
22 #import "third_party/GTM/AppKit/GTMNSAnimation+Duration.h"
23 #import "third_party/GTM/AppKit/GTMUILocalizerAndLayoutTweaker.h"
22 24
23 static const CGFloat kMinCollectedCookiesViewHeight = 116; 25 namespace {
26 // Colors for the infobar.
27 const double kBannerGradientColorTop[3] =
28 {255.0 / 255.0, 242.0 / 255.0, 183.0 / 255.0};
29 const double kBannerGradientColorBottom[3] =
30 {250.0 / 255.0, 230.0 / 255.0, 145.0 / 255.0};
31 const double kBannerStrokeColor = 135.0 / 255.0;
32
33 // Minimal height for the collected cookies dialog.
34 const CGFloat kMinCollectedCookiesViewHeight = 116;
35 } // namespace
24 36
25 #pragma mark Bridge between the constrained window delegate and the sheet 37 #pragma mark Bridge between the constrained window delegate and the sheet
26 38
27 // The delegate used to forward the events from the sheet to the constrained 39 // The delegate used to forward the events from the sheet to the constrained
28 // window delegate. 40 // window delegate.
29 @interface CollectedCookiesSheetBridge : NSObject { 41 @interface CollectedCookiesSheetBridge : NSObject {
30 CollectedCookiesMac* collectedCookies_; // weak 42 CollectedCookiesMac* collectedCookies_; // weak
31 } 43 }
32 - (id)initWithCollectedCookiesMac:(CollectedCookiesMac*)collectedCookies; 44 - (id)initWithCollectedCookiesMac:(CollectedCookiesMac*)collectedCookies;
33 - (void)sheetDidEnd:(NSWindow*)sheet 45 - (void)sheetDidEnd:(NSWindow*)sheet
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 } 106 }
95 107
96 void CollectedCookiesMac::OnSheetDidEnd(NSWindow* sheet) { 108 void CollectedCookiesMac::OnSheetDidEnd(NSWindow* sheet) {
97 [sheet orderOut:sheet_controller_]; 109 [sheet orderOut:sheet_controller_];
98 if (window_) 110 if (window_)
99 window_->CloseConstrainedWindow(); 111 window_->CloseConstrainedWindow();
100 } 112 }
101 113
102 #pragma mark Window Controller 114 #pragma mark Window Controller
103 115
116 @interface CollectedCookiesWindowController(Private)
117 -(void)showInfoBarForDomain:(const string16&)domain
118 setting:(ContentSetting)setting;
119 -(void)showInfoBarForMultipleDomainsAndSetting:(ContentSetting)setting;
120 -(void)animateInfoBar;
121 @end
122
104 @implementation CollectedCookiesWindowController 123 @implementation CollectedCookiesWindowController
105 124
106 @synthesize allowedCookiesButtonsEnabled = 125 @synthesize allowedCookiesButtonsEnabled =
107 allowedCookiesButtonsEnabled_; 126 allowedCookiesButtonsEnabled_;
108 @synthesize blockedCookiesButtonsEnabled = 127 @synthesize blockedCookiesButtonsEnabled =
109 blockedCookiesButtonsEnabled_; 128 blockedCookiesButtonsEnabled_;
110 129
111 @synthesize allowedTreeController = allowedTreeController_; 130 @synthesize allowedTreeController = allowedTreeController_;
112 @synthesize blockedTreeController = blockedTreeController_; 131 @synthesize blockedTreeController = blockedTreeController_;
113 132
114 - (id)initWithTabContents:(TabContents*)tabContents { 133 - (id)initWithTabContents:(TabContents*)tabContents {
115 DCHECK(tabContents); 134 DCHECK(tabContents);
135 infoBarVisible_ = NO;
136 tabContents_ = tabContents;
137
116 NSString* nibpath = 138 NSString* nibpath =
117 [mac_util::MainAppBundle() pathForResource:@"CollectedCookies" 139 [mac_util::MainAppBundle() pathForResource:@"CollectedCookies"
118 ofType:@"nib"]; 140 ofType:@"nib"];
119 if ((self = [super initWithWindowNibPath:nibpath owner:self])) { 141 if ((self = [super initWithWindowNibPath:nibpath owner:self])) {
120 tabContents_ = tabContents; 142 [self loadTreeModelFromTabContents];
121 143
122 [self loadTreeModelFromTabContents]; 144 animation_.reset([[NSViewAnimation alloc] init]);
145 [animation_ setAnimationBlockingMode:NSAnimationNonblocking];
123 } 146 }
124 return self; 147 return self;
125 } 148 }
126 149
150 - (void)awakeFromNib {
151 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
152 NSImage* infoIcon = rb.GetNSImageNamed(IDR_INFO);
153 DCHECK(infoIcon);
154 [infoBarIcon_ setImage:infoIcon];
155
156 // Initialize the banner gradient and stroke color.
157 NSColor* bannerStartingColor =
158 [NSColor colorWithCalibratedRed:kBannerGradientColorTop[0]
159 green:kBannerGradientColorTop[1]
160 blue:kBannerGradientColorTop[2]
161 alpha:1.0];
162 NSColor* bannerEndingColor =
163 [NSColor colorWithCalibratedRed:kBannerGradientColorBottom[0]
164 green:kBannerGradientColorBottom[1]
165 blue:kBannerGradientColorBottom[2]
166 alpha:1.0];
167 scoped_nsobject<NSGradient> bannerGradient(
168 [[NSGradient alloc] initWithStartingColor:bannerStartingColor
169 endingColor:bannerEndingColor]);
170 [infoBar_ setGradient:bannerGradient];
171
172 NSColor* bannerStrokeColor =
173 [NSColor colorWithCalibratedWhite:kBannerStrokeColor
174 alpha:1.0];
175 [infoBar_ setStrokeColor:bannerStrokeColor];
176
177 // Change the label of the blocked cookies part if necessary.
178 if (tabContents_->profile()->GetHostContentSettingsMap()->
179 BlockThirdPartyCookies()) {
180 [blockedCookiesText_ setStringValue:l10n_util::GetNSString(
181 IDS_COLLECTED_COOKIES_BLOCKED_THIRD_PARTY_BLOCKING_ENABLED)];
182 CGFloat textDeltaY = [GTMUILocalizerAndLayoutTweaker
183 sizeToFitFixedWidthTextField:blockedCookiesText_];
184
185 // Shrink the upper custom view.
186 NSView* upperContentView = [[splitView_ subviews] objectAtIndex:0];
187 NSRect frame = [upperContentView frame];
188 [splitView_ setPosition:(frame.size.height - textDeltaY/2.0)
189 ofDividerAtIndex:0];
190
191 // Shrink the lower outline view.
192 frame = [lowerScrollView_ frame];
193 frame.size.height -= textDeltaY;
194 [lowerScrollView_ setFrame:frame];
195
196 // Move the label down so it actually fits.
197 frame = [blockedCookiesText_ frame];
198 frame.origin.y -= textDeltaY;
199 [blockedCookiesText_ setFrame:frame];
200 }
201 }
202
127 - (void)windowWillClose:(NSNotification*)notif { 203 - (void)windowWillClose:(NSNotification*)notif {
128 [allowedOutlineView_ setDelegate:nil]; 204 [allowedOutlineView_ setDelegate:nil];
129 [blockedOutlineView_ setDelegate:nil]; 205 [blockedOutlineView_ setDelegate:nil];
206 [animation_ stopAnimation];
130 [self autorelease]; 207 [self autorelease];
131 } 208 }
132 209
133 - (IBAction)closeSheet:(id)sender { 210 - (IBAction)closeSheet:(id)sender {
134 [NSApp endSheet:[self window]]; 211 [NSApp endSheet:[self window]];
135 } 212 }
136 213
137 - (void)addException:(ContentSetting)setting 214 - (void)addException:(ContentSetting)setting
138 forTreeController:(NSTreeController*)controller { 215 forTreeController:(NSTreeController*)controller {
139 NSArray* nodes = [controller selectedNodes]; 216 NSArray* nodes = [controller selectedNodes];
217 BOOL multipleDomainsChanged = NO;
218 string16 lastDomain;
140 for (NSTreeNode* treeNode in nodes) { 219 for (NSTreeNode* treeNode in nodes) {
141 CocoaCookieTreeNode* node = [treeNode representedObject]; 220 CocoaCookieTreeNode* node = [treeNode representedObject];
142 CookieTreeNode* cookie = static_cast<CookieTreeNode*>([node treeNode]); 221 CookieTreeNode* cookie = static_cast<CookieTreeNode*>([node treeNode]);
143 if (cookie->GetDetailedInfo().node_type != 222 if (cookie->GetDetailedInfo().node_type !=
144 CookieTreeNode::DetailedInfo::TYPE_ORIGIN) { 223 CookieTreeNode::DetailedInfo::TYPE_ORIGIN) {
145 continue; 224 continue;
146 } 225 }
147 CookieTreeOriginNode* origin_node = 226 CookieTreeOriginNode* origin_node =
148 static_cast<CookieTreeOriginNode*>(cookie); 227 static_cast<CookieTreeOriginNode*>(cookie);
149 origin_node->CreateContentException( 228 origin_node->CreateContentException(
150 tabContents_->profile()->GetHostContentSettingsMap(), 229 tabContents_->profile()->GetHostContentSettingsMap(),
151 setting); 230 setting);
231 if (!lastDomain.empty())
232 multipleDomainsChanged = YES;
233 lastDomain = origin_node->GetTitleAsString16();
152 } 234 }
153 [[ContentSettingsDialogController 235 if (multipleDomainsChanged)
154 showContentSettingsForType:CONTENT_SETTINGS_TYPE_COOKIES 236 [self showInfoBarForMultipleDomainsAndSetting:setting];
155 profile:tabContents_->profile()] 237 else
156 showCookieExceptions:self]; 238 [self showInfoBarForDomain:lastDomain setting:setting];
157 } 239 }
158 240
159 - (IBAction)allowOrigin:(id)sender { 241 - (IBAction)allowOrigin:(id)sender {
160 [self addException:CONTENT_SETTING_ALLOW 242 [self addException:CONTENT_SETTING_ALLOW
161 forTreeController:blockedTreeController_]; 243 forTreeController:blockedTreeController_];
162 } 244 }
163 245
164 - (IBAction)allowForSessionFromOrigin:(id)sender { 246 - (IBAction)allowForSessionFromOrigin:(id)sender {
165 [self addException:CONTENT_SETTING_SESSION_ONLY 247 [self addException:CONTENT_SETTING_SESSION_ONLY
166 forTreeController:blockedTreeController_]; 248 forTreeController:blockedTreeController_];
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
292 static_cast<CookieTreeNode*>(allowedTreeModel_->GetRoot()); 374 static_cast<CookieTreeNode*>(allowedTreeModel_->GetRoot());
293 scoped_nsobject<CocoaCookieTreeNode> model( 375 scoped_nsobject<CocoaCookieTreeNode> model(
294 [[CocoaCookieTreeNode alloc] initWithNode:root]); 376 [[CocoaCookieTreeNode alloc] initWithNode:root]);
295 [self setCocoaAllowedTreeModel:model.get()]; // Takes ownership. 377 [self setCocoaAllowedTreeModel:model.get()]; // Takes ownership.
296 root = static_cast<CookieTreeNode*>(blockedTreeModel_->GetRoot()); 378 root = static_cast<CookieTreeNode*>(blockedTreeModel_->GetRoot());
297 model.reset( 379 model.reset(
298 [[CocoaCookieTreeNode alloc] initWithNode:root]); 380 [[CocoaCookieTreeNode alloc] initWithNode:root]);
299 [self setCocoaBlockedTreeModel:model.get()]; // Takes ownership. 381 [self setCocoaBlockedTreeModel:model.get()]; // Takes ownership.
300 } 382 }
301 383
384 -(void)showInfoBarForMultipleDomainsAndSetting:(ContentSetting)setting {
385 NSString* label;
386 switch (setting) {
387 case CONTENT_SETTING_BLOCK:
388 label = l10n_util::GetNSString(
389 IDS_COLLECTED_COOKIES_MULTIPLE_BLOCK_RULES_CREATED);
390 break;
391
392 case CONTENT_SETTING_ALLOW:
393 label = l10n_util::GetNSString(
394 IDS_COLLECTED_COOKIES_MULTIPLE_ALLOW_RULES_CREATED);
395 break;
396
397 case CONTENT_SETTING_SESSION_ONLY:
398 label = l10n_util::GetNSString(
399 IDS_COLLECTED_COOKIES_MULTIPLE_SESSION_RULES_CREATED);
400 break;
401
402 default:
403 NOTREACHED();
404 label = [[[NSString alloc] init] autorelease];
405 }
406 [infoBarText_ setStringValue:label];
407 [self animateInfoBar];
408 }
409
410 -(void)showInfoBarForDomain:(const string16&)domain
411 setting:(ContentSetting)setting {
412 NSString* label;
413 switch (setting) {
414 case CONTENT_SETTING_BLOCK:
415 label = l10n_util::GetNSStringF(
416 IDS_COLLECTED_COOKIES_BLOCK_RULE_CREATED,
417 domain);
418 break;
419
420 case CONTENT_SETTING_ALLOW:
421 label = l10n_util::GetNSStringF(
422 IDS_COLLECTED_COOKIES_ALLOW_RULE_CREATED,
423 domain);
424 break;
425
426 case CONTENT_SETTING_SESSION_ONLY:
427 label = l10n_util::GetNSStringF(
428 IDS_COLLECTED_COOKIES_SESSION_RULE_CREATED,
429 domain);
430 break;
431
432 default:
433 NOTREACHED();
434 label = [[[NSString alloc] init] autorelease];
435 }
436 [infoBarText_ setStringValue:label];
437 [self animateInfoBar];
438 }
439
440 -(void)animateInfoBar {
441 if (infoBarVisible_)
442 return;
443
444 infoBarVisible_ = YES;
445
446 NSMutableArray* animations = [NSMutableArray arrayWithCapacity:3];
447
448 NSWindow* sheet = [self window];
449 NSRect sheetFrame = [sheet frame];
450 NSRect infoBarFrame = [infoBar_ frame];
451 NSRect splitViewFrame = [splitView_ frame];
452
453 // Calculate the end position of the info bar and set it to its start
454 // position.
455 infoBarFrame.origin.y = NSHeight(sheetFrame);
456 infoBarFrame.size.width = NSWidth(sheetFrame);
457 NSRect infoBarStartFrame = infoBarFrame;
458 infoBarStartFrame.origin.y += NSHeight(infoBarFrame);
459 infoBarStartFrame.size.height = 0.0;
460 [infoBar_ setFrame:infoBarStartFrame];
461 [[[self window] contentView] addSubview:infoBar_];
462
463 // Calculate the new position of the sheet.
464 sheetFrame.origin.y -= NSHeight(infoBarFrame);
465 sheetFrame.size.height += NSHeight(infoBarFrame);
466
467 // Slide the infobar in.
468 [animations addObject:
469 [NSDictionary dictionaryWithObjectsAndKeys:
470 infoBar_, NSViewAnimationTargetKey,
471 [NSValue valueWithRect:infoBarFrame],
472 NSViewAnimationEndFrameKey,
473 [NSValue valueWithRect:infoBarStartFrame],
474 NSViewAnimationStartFrameKey,
475 nil]];
476 // Make sure the split view ends up in the right position.
477 [animations addObject:
478 [NSDictionary dictionaryWithObjectsAndKeys:
479 splitView_, NSViewAnimationTargetKey,
480 [NSValue valueWithRect:splitViewFrame],
481 NSViewAnimationEndFrameKey,
482 nil]];
483
484 // Grow the sheet.
485 [animations addObject:
486 [NSDictionary dictionaryWithObjectsAndKeys:
487 sheet, NSViewAnimationTargetKey,
488 [NSValue valueWithRect:sheetFrame],
489 NSViewAnimationEndFrameKey,
490 nil]];
491 [animation_ setViewAnimations:animations];
492 // The default duration is 0.5s, which actually feels slow in here, so speed
493 // it up a bit.
494 [animation_ gtm_setDuration:0.2
495 eventMask:NSLeftMouseUpMask];
496 [animation_ startAnimation];
497 }
498
302 @end 499 @end
OLDNEW
« no previous file with comments | « chrome/browser/cocoa/collected_cookies_mac.h ('k') | chrome/browser/gtk/collected_cookies_gtk.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698