| 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 #import "chrome/browser/cocoa/cookie_prompt_window_controller.h" | |
| 6 | |
| 7 #include <string> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "app/l10n_util_mac.h" | |
| 11 #include "app/resource_bundle.h" | |
| 12 #import "base/mac_util.h" | |
| 13 #include "base/sys_string_conversions.h" | |
| 14 #import "chrome/browser/cocoa/cookie_details_view_controller.h" | |
| 15 #include "chrome/browser/cocoa/cookie_tree_node.h" | |
| 16 #include "chrome/browser/cookie_modal_dialog.h" | |
| 17 #include "chrome/browser/cookies_tree_model.h" | |
| 18 #include "grit/generated_resources.h" | |
| 19 #import "third_party/GTM/AppKit/GTMUILocalizerAndLayoutTweaker.h" | |
| 20 | |
| 21 namespace { | |
| 22 static const CGFloat kExtraMarginForDetailsView = 10; | |
| 23 } | |
| 24 | |
| 25 @implementation CookiePromptWindowController | |
| 26 | |
| 27 - (id)initWithDialog:(CookiePromptModalDialog*)dialog { | |
| 28 NSString* nibpath = [mac_util::MainAppBundle() | |
| 29 pathForResource:@"CookiePrompt" | |
| 30 ofType:@"nib"]; | |
| 31 if ((self = [super initWithWindowNibPath:nibpath owner:self])) { | |
| 32 dialog_ = dialog; | |
| 33 CocoaCookieDetails* details = [CocoaCookieDetails | |
| 34 createFromPromptModalDialog:dialog]; | |
| 35 selectionAdapterObject_.reset([[CookiePromptContentDetailsAdapter alloc] | |
| 36 initWithDetails:details]); | |
| 37 } | |
| 38 return self; | |
| 39 } | |
| 40 | |
| 41 // Ensures that all parameterized localized strings are filled in. | |
| 42 - (void)doLocalizationTweaks { | |
| 43 int descriptionStringId = 0; | |
| 44 switch (dialog_->dialog_type()) { | |
| 45 case CookiePromptModalDialog::DIALOG_TYPE_COOKIE: | |
| 46 descriptionStringId = IDS_COOKIE_ALERT_LABEL; | |
| 47 break; | |
| 48 case CookiePromptModalDialog::DIALOG_TYPE_LOCAL_STORAGE: | |
| 49 case CookiePromptModalDialog::DIALOG_TYPE_DATABASE: | |
| 50 case CookiePromptModalDialog::DIALOG_TYPE_APPCACHE: | |
| 51 descriptionStringId = IDS_DATA_ALERT_LABEL; | |
| 52 break; | |
| 53 default: | |
| 54 NOTREACHED(); | |
| 55 break; | |
| 56 } | |
| 57 | |
| 58 string16 displayHost = UTF8ToUTF16(dialog_->origin().host()); | |
| 59 NSString* description = l10n_util::GetNSStringF( | |
| 60 descriptionStringId, displayHost); | |
| 61 [description_ setStringValue:description]; | |
| 62 | |
| 63 // Add the host to the "remember for future prompts" radio button. | |
| 64 NSString* allowText = l10n_util::GetNSStringWithFixup( | |
| 65 IDS_COOKIE_ALERT_REMEMBER_RADIO); | |
| 66 NSString* replacedCellText = base::SysUTF16ToNSString( | |
| 67 ReplaceStringPlaceholders(base::SysNSStringToUTF16(allowText), | |
| 68 displayHost, NULL)); | |
| 69 [rememberChoiceCell_ setTitle:replacedCellText]; | |
| 70 } | |
| 71 | |
| 72 // Adjust the vertical layout of the views in the window so that | |
| 73 // they are spaced correctly with their fully localized contents. | |
| 74 - (void)doLayoutTweaks { | |
| 75 // Wrap the description text. | |
| 76 CGFloat sizeDelta = [GTMUILocalizerAndLayoutTweaker | |
| 77 sizeToFitFixedWidthTextField:description_]; | |
| 78 NSRect descriptionFrame = [description_ frame]; | |
| 79 descriptionFrame.origin.y -= sizeDelta; | |
| 80 [description_ setFrame:descriptionFrame]; | |
| 81 | |
| 82 // |wrapRadioGroupForWidth:| takes the font that is set on the | |
| 83 // radio group to do the wrapping. It must be set explicitly, otherwise | |
| 84 // the wrapping is based on the |NSRegularFontSize| rather than | |
| 85 // |NSSmallFontSize| | |
| 86 CGFloat fontSize = [NSFont systemFontSizeForControlSize:NSSmallControlSize]; | |
| 87 [radioGroupMatrix_ setFont:[NSFont controlContentFontOfSize:fontSize]]; | |
| 88 | |
| 89 // Wrap the radio buttons to fit if necessary. | |
| 90 [GTMUILocalizerAndLayoutTweaker | |
| 91 wrapRadioGroupForWidth:radioGroupMatrix_]; | |
| 92 sizeDelta += [GTMUILocalizerAndLayoutTweaker | |
| 93 sizeToFitView:radioGroupMatrix_].height; | |
| 94 NSRect radioGroupFrame = [radioGroupMatrix_ frame]; | |
| 95 radioGroupFrame.origin.y -= sizeDelta; | |
| 96 [radioGroupMatrix_ setFrame:radioGroupFrame]; | |
| 97 | |
| 98 // Adjust views location, they may have moved through the | |
| 99 // expansion of the radio buttons and description text. | |
| 100 NSRect disclosureViewFrame = [disclosureButtonSuperView_ frame]; | |
| 101 disclosureViewFrame.origin.y -= sizeDelta; | |
| 102 [disclosureButtonSuperView_ setFrame:disclosureViewFrame]; | |
| 103 | |
| 104 // Adjust the final window size by the size of the cookie details | |
| 105 // view, since it will be initially hidden. | |
| 106 NSRect detailsViewRect = [disclosedViewPlaceholder_ frame]; | |
| 107 sizeDelta -= detailsViewRect.size.height; | |
| 108 sizeDelta -= kExtraMarginForDetailsView; | |
| 109 | |
| 110 // Final resize the window to fit all of the adjustments | |
| 111 NSRect frame = [[self window] frame]; | |
| 112 frame.origin.y -= sizeDelta; | |
| 113 frame.size.height += sizeDelta; | |
| 114 [[self window] setFrame:frame display:NO animate:NO]; | |
| 115 } | |
| 116 | |
| 117 - (void)replaceCookieDetailsView { | |
| 118 detailsViewController_.reset([[CookieDetailsViewController alloc] init]); | |
| 119 | |
| 120 [detailsViewController_ setContentObject:selectionAdapterObject_.get()]; | |
| 121 | |
| 122 [[disclosedViewPlaceholder_ superview] | |
| 123 replaceSubview:disclosedViewPlaceholder_ | |
| 124 with:[detailsViewController_ view]]; | |
| 125 | |
| 126 [detailsViewController_ shrinkViewToFit]; | |
| 127 } | |
| 128 | |
| 129 - (void)awakeFromNib { | |
| 130 DCHECK(disclosureButton_); | |
| 131 DCHECK(radioGroupMatrix_); | |
| 132 DCHECK(disclosedViewPlaceholder_); | |
| 133 DCHECK(disclosureButtonSuperView_); | |
| 134 | |
| 135 [self doLocalizationTweaks]; | |
| 136 [self doLayoutTweaks]; | |
| 137 [self replaceCookieDetailsView]; | |
| 138 | |
| 139 [[detailsViewController_ view] setHidden:YES]; | |
| 140 } | |
| 141 | |
| 142 - (void)windowWillClose:(NSNotification*)notif { | |
| 143 [self autorelease]; | |
| 144 } | |
| 145 | |
| 146 // |contextInfo| is the bridge back to the C++ CookiePromptModalDialog. | |
| 147 - (void)processModalDialogResult:(void*)contextInfo | |
| 148 returnCode:(int)returnCode { | |
| 149 CookiePromptModalDialog* bridge = | |
| 150 reinterpret_cast<CookiePromptModalDialog*>(contextInfo); | |
| 151 bool remember = [radioGroupMatrix_ selectedRow] == 0; | |
| 152 switch (returnCode) { | |
| 153 case NSAlertFirstButtonReturn: { // OK | |
| 154 bool sessionExpire = ![detailsViewController_.get() hasExpiration]; | |
| 155 bridge->AllowSiteData(remember, sessionExpire); | |
| 156 break; | |
| 157 } | |
| 158 case NSAlertSecondButtonReturn: { // Cancel | |
| 159 bridge->BlockSiteData(remember); | |
| 160 break; | |
| 161 } | |
| 162 default: { | |
| 163 NOTREACHED(); | |
| 164 remember = false; | |
| 165 bridge->BlockSiteData(remember); | |
| 166 } | |
| 167 } | |
| 168 } | |
| 169 | |
| 170 - (void)doModalDialog:(void*)context { | |
| 171 NSInteger returnCode = [NSApp runModalForWindow:[self window]]; | |
| 172 [self processModalDialogResult:context returnCode:returnCode]; | |
| 173 } | |
| 174 | |
| 175 - (IBAction)disclosureButtonPressed:(id)sender { | |
| 176 NSWindow* window = [self window]; | |
| 177 NSRect frame = [[self window] frame]; | |
| 178 CGFloat sizeChange = [[detailsViewController_.get() view] frame].size.height + | |
| 179 kExtraMarginForDetailsView; | |
| 180 switch ([sender state]) { | |
| 181 case NSOnState: | |
| 182 frame.size.height += sizeChange; | |
| 183 frame.origin.y -= sizeChange; | |
| 184 break; | |
| 185 case NSOffState: | |
| 186 frame.size.height -= sizeChange; | |
| 187 frame.origin.y += sizeChange; | |
| 188 break; | |
| 189 default: | |
| 190 NOTREACHED(); | |
| 191 break; | |
| 192 } | |
| 193 if ([sender state] == NSOffState) { | |
| 194 [[detailsViewController_ view] setHidden:YES]; | |
| 195 } | |
| 196 [window setFrame:frame display:YES animate:YES]; | |
| 197 if ([sender state] == NSOnState) { | |
| 198 [[detailsViewController_ view] setHidden:NO]; | |
| 199 } | |
| 200 } | |
| 201 | |
| 202 // Callback for "accept" button. | |
| 203 - (IBAction)accept:(id)sender { | |
| 204 [[self window] close]; | |
| 205 [NSApp stopModalWithCode:NSAlertFirstButtonReturn]; | |
| 206 } | |
| 207 | |
| 208 // Callback for "block" button. | |
| 209 - (IBAction)block:(id)sender { | |
| 210 [[self window] close]; | |
| 211 [NSApp stopModalWithCode:NSAlertSecondButtonReturn]; | |
| 212 } | |
| 213 | |
| 214 @end | |
| OLD | NEW |