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

Side by Side Diff: ios/chrome/browser/ui/dialogs/dialog_presenter.mm

Issue 2957423002: Check page URL origin to determine JavaScript alert titles. (Closed)
Patch Set: Small tweak Created 3 years, 5 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "ios/chrome/browser/ui/dialogs/dialog_presenter.h" 5 #import "ios/chrome/browser/ui/dialogs/dialog_presenter.h"
6 6
7 #include <deque> 7 #include <deque>
8 #include <map> 8 #include <map>
9 9
10 #import "base/ios/block_types.h" 10 #import "base/ios/block_types.h"
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 // ShouldShowDialogBlockingOption() for |webState|, optionally adds a button to 84 // ShouldShowDialogBlockingOption() for |webState|, optionally adds a button to
85 // |alertCoordinator| allowing for the blocking of future dialogs. In addition 85 // |alertCoordinator| allowing for the blocking of future dialogs. In addition
86 // to blocking dialogs for the WebState, the added button will call 86 // to blocking dialogs for the WebState, the added button will call
87 // |alertCoordinator|'s |cancelAction|. 87 // |alertCoordinator|'s |cancelAction|.
88 - (void)setUpBlockingOptionForCoordinator:(AlertCoordinator*)alertCoordinator 88 - (void)setUpBlockingOptionForCoordinator:(AlertCoordinator*)alertCoordinator
89 webState:(web::WebState*)webState; 89 webState:(web::WebState*)webState;
90 90
91 // The block to use for the JavaScript dialog blocking option for |coordinator|. 91 // The block to use for the JavaScript dialog blocking option for |coordinator|.
92 - (ProceduralBlock)blockingActionForCoordinator:(AlertCoordinator*)coordinator; 92 - (ProceduralBlock)blockingActionForCoordinator:(AlertCoordinator*)coordinator;
93 93
94 // Creates a title for the alert based on the URL (|pageURL|), and its
95 // relationship to the |mainFrameURL| (typically these are identical except for
96 // when posting alerts from an embedded iframe).
97 + (NSString*)localizedTitleForJavaScriptAlertFromPage:(const GURL&)pageURL
98 mainFrameURL:(const GURL&)mainFrameURL;
99
94 @end 100 @end
95 101
96 @implementation DialogPresenter 102 @implementation DialogPresenter
97 103
98 @synthesize active = _active; 104 @synthesize active = _active;
99 @synthesize delegate = _delegate; 105 @synthesize delegate = _delegate;
100 @synthesize viewController = _viewController; 106 @synthesize viewController = _viewController;
101 @synthesize presentedDialogCoordinator = _presentedDialogCoordinator; 107 @synthesize presentedDialogCoordinator = _presentedDialogCoordinator;
102 @synthesize blockingConfirmationCoordinator = _blockingConfirmationCoordinator; 108 @synthesize blockingConfirmationCoordinator = _blockingConfirmationCoordinator;
103 @synthesize presentedDialogWebState = _presentedDialogWebState; 109 @synthesize presentedDialogWebState = _presentedDialogWebState;
(...skipping 23 matching lines...) Expand all
127 self.presentedDialogCoordinator != nil); 133 self.presentedDialogCoordinator != nil);
128 return self.presentedDialogCoordinator != nil; 134 return self.presentedDialogCoordinator != nil;
129 } 135 }
130 136
131 #pragma mark - Public 137 #pragma mark - Public
132 138
133 - (void)runJavaScriptAlertPanelWithMessage:(NSString*)message 139 - (void)runJavaScriptAlertPanelWithMessage:(NSString*)message
134 requestURL:(const GURL&)requestURL 140 requestURL:(const GURL&)requestURL
135 webState:(web::WebState*)webState 141 webState:(web::WebState*)webState
136 completionHandler:(void (^)(void))completionHandler { 142 completionHandler:(void (^)(void))completionHandler {
137 NSString* title = 143 NSString* title = [DialogPresenter
138 [DialogPresenter localizedTitleForJavaScriptAlertFromPage:requestURL]; 144 localizedTitleForJavaScriptAlertFromPage:requestURL
145 mainFrameURL:webState->GetLastCommittedURL()];
139 AlertCoordinator* alertCoordinator = 146 AlertCoordinator* alertCoordinator =
140 [[AlertCoordinator alloc] initWithBaseViewController:self.viewController 147 [[AlertCoordinator alloc] initWithBaseViewController:self.viewController
141 title:title 148 title:title
142 message:message]; 149 message:message];
143 150
144 // Handler. 151 // Handler.
145 __weak DialogPresenter* weakSelf = self; 152 __weak DialogPresenter* weakSelf = self;
146 __weak AlertCoordinator* weakCoordinator = alertCoordinator; 153 __weak AlertCoordinator* weakCoordinator = alertCoordinator;
147 ProceduralBlock OKHandler = ^{ 154 ProceduralBlock OKHandler = ^{
148 if (completionHandler) 155 if (completionHandler)
(...skipping 14 matching lines...) Expand all
163 [self setUpBlockingOptionForCoordinator:alertCoordinator webState:webState]; 170 [self setUpBlockingOptionForCoordinator:alertCoordinator webState:webState];
164 171
165 [self addDialogCoordinator:alertCoordinator forWebState:webState]; 172 [self addDialogCoordinator:alertCoordinator forWebState:webState];
166 } 173 }
167 174
168 - (void)runJavaScriptConfirmPanelWithMessage:(NSString*)message 175 - (void)runJavaScriptConfirmPanelWithMessage:(NSString*)message
169 requestURL:(const GURL&)requestURL 176 requestURL:(const GURL&)requestURL
170 webState:(web::WebState*)webState 177 webState:(web::WebState*)webState
171 completionHandler: 178 completionHandler:
172 (void (^)(BOOL isConfirmed))completionHandler { 179 (void (^)(BOOL isConfirmed))completionHandler {
173 NSString* title = 180 NSString* title = [DialogPresenter
174 [DialogPresenter localizedTitleForJavaScriptAlertFromPage:requestURL]; 181 localizedTitleForJavaScriptAlertFromPage:requestURL
182 mainFrameURL:webState->GetLastCommittedURL()];
175 AlertCoordinator* alertCoordinator = 183 AlertCoordinator* alertCoordinator =
176 [[AlertCoordinator alloc] initWithBaseViewController:self.viewController 184 [[AlertCoordinator alloc] initWithBaseViewController:self.viewController
177 title:title 185 title:title
178 message:message]; 186 message:message];
179 187
180 // Actions. 188 // Actions.
181 ProceduralBlock confirmAction = ^{ 189 ProceduralBlock confirmAction = ^{
182 if (completionHandler) 190 if (completionHandler)
183 completionHandler(YES); 191 completionHandler(YES);
184 }; 192 };
(...skipping 15 matching lines...) Expand all
200 208
201 [self addDialogCoordinator:alertCoordinator forWebState:webState]; 209 [self addDialogCoordinator:alertCoordinator forWebState:webState];
202 } 210 }
203 211
204 - (void)runJavaScriptTextInputPanelWithPrompt:(NSString*)message 212 - (void)runJavaScriptTextInputPanelWithPrompt:(NSString*)message
205 defaultText:(NSString*)defaultText 213 defaultText:(NSString*)defaultText
206 requestURL:(const GURL&)requestURL 214 requestURL:(const GURL&)requestURL
207 webState:(web::WebState*)webState 215 webState:(web::WebState*)webState
208 completionHandler: 216 completionHandler:
209 (void (^)(NSString* input))completionHandler { 217 (void (^)(NSString* input))completionHandler {
210 NSString* title = 218 NSString* title = [DialogPresenter
211 [DialogPresenter localizedTitleForJavaScriptAlertFromPage:requestURL]; 219 localizedTitleForJavaScriptAlertFromPage:requestURL
220 mainFrameURL:webState->GetLastCommittedURL()];
212 InputAlertCoordinator* alertCoordinator = [[InputAlertCoordinator alloc] 221 InputAlertCoordinator* alertCoordinator = [[InputAlertCoordinator alloc]
213 initWithBaseViewController:self.viewController 222 initWithBaseViewController:self.viewController
214 title:title 223 title:title
215 message:message]; 224 message:message];
216 225
217 // Actions. 226 // Actions.
218 __weak InputAlertCoordinator* weakCoordinator = alertCoordinator; 227 __weak InputAlertCoordinator* weakCoordinator = alertCoordinator;
219 ProceduralBlock confirmAction = ^{ 228 ProceduralBlock confirmAction = ^{
220 if (completionHandler) { 229 if (completionHandler) {
221 NSString* textInput = [weakCoordinator textFields].firstObject.text; 230 NSString* textInput = [weakCoordinator textFields].firstObject.text;
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
342 // Don't try to present if a JavaScript dialog blocking confirmation sheet is 351 // Don't try to present if a JavaScript dialog blocking confirmation sheet is
343 // displayed. 352 // displayed.
344 if (self.blockingConfirmationCoordinator) 353 if (self.blockingConfirmationCoordinator)
345 return; 354 return;
346 // The active TabModel can't be changed while a JavaScript dialog is shown. 355 // The active TabModel can't be changed while a JavaScript dialog is shown.
347 DCHECK(!self.showingDialog); 356 DCHECK(!self.showingDialog);
348 if (_active && !_queuedWebStates.empty() && !self.delegate.presenting) 357 if (_active && !_queuedWebStates.empty() && !self.delegate.presenting)
349 [self showNextDialog]; 358 [self showNextDialog];
350 } 359 }
351 360
352 + (NSString*)localizedTitleForJavaScriptAlertFromPage:(const GURL&)pageURL { 361 + (NSString*)localizedTitleForJavaScriptAlertFromPage:(const GURL&)pageURL
362 mainFrameURL:
363 (const GURL&)mainFrameURL {
353 NSString* localizedTitle = nil; 364 NSString* localizedTitle = nil;
354 NSString* hostname = base::SysUTF8ToNSString(pageURL.host()); 365 NSString* hostname = base::SysUTF8ToNSString(pageURL.host());
355 if (!hostname.length) { 366
367 bool sameOriginAsMainFrame = pageURL.GetOrigin() == mainFrameURL.GetOrigin();
368
369 if (!sameOriginAsMainFrame) {
356 localizedTitle = l10n_util::GetNSString( 370 localizedTitle = l10n_util::GetNSString(
357 IDS_JAVASCRIPT_MESSAGEBOX_TITLE_NONSTANDARD_URL_IFRAME); 371 IDS_JAVASCRIPT_MESSAGEBOX_TITLE_NONSTANDARD_URL_IFRAME);
358 } else { 372 } else {
359 localizedTitle = l10n_util::GetNSStringF( 373 localizedTitle = l10n_util::GetNSStringF(
360 IDS_JAVASCRIPT_MESSAGEBOX_TITLE, base::SysNSStringToUTF16(hostname)); 374 IDS_JAVASCRIPT_MESSAGEBOX_TITLE, base::SysNSStringToUTF16(hostname));
361 } 375 }
362 return localizedTitle; 376 return localizedTitle;
363 } 377 }
364 378
365 #pragma mark - Private methods. 379 #pragma mark - Private methods.
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
507 style:UIAlertActionStyleDestructive]; 521 style:UIAlertActionStyleDestructive];
508 [confirmationCoordinator addItemWithTitle:l10n_util::GetNSString(IDS_CANCEL) 522 [confirmationCoordinator addItemWithTitle:l10n_util::GetNSString(IDS_CANCEL)
509 action:cancelHandler 523 action:cancelHandler
510 style:UIAlertActionStyleCancel]; 524 style:UIAlertActionStyleCancel];
511 [weakSelf setBlockingConfirmationCoordinator:confirmationCoordinator]; 525 [weakSelf setBlockingConfirmationCoordinator:confirmationCoordinator];
512 [[weakSelf blockingConfirmationCoordinator] start]; 526 [[weakSelf blockingConfirmationCoordinator] start];
513 } copy]; 527 } copy];
514 } 528 }
515 529
516 @end 530 @end
OLDNEW
« no previous file with comments | « ios/chrome/browser/ui/dialogs/dialog_presenter.h ('k') | ios/chrome/browser/ui/dialogs/dialog_presenter_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698