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

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

Issue 2936643003: Allow cancelling the currently-presented dialog. (Closed)
Patch Set: fix compilation Created 3 years, 6 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 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 66
67 // Adds |context| and |coordinator| to the queue. If a dialog is not already 67 // Adds |context| and |coordinator| to the queue. If a dialog is not already
68 // being shown, |coordinator| will be presented. Otherwise, |coordinator| will 68 // being shown, |coordinator| will be presented. Otherwise, |coordinator| will
69 // be displayed once the previously shown dialog is dismissed. 69 // be displayed once the previously shown dialog is dismissed.
70 - (void)addDialogCoordinator:(AlertCoordinator*)coordinator 70 - (void)addDialogCoordinator:(AlertCoordinator*)coordinator
71 forWebState:(web::WebState*)webState; 71 forWebState:(web::WebState*)webState;
72 72
73 // Shows the dialog associated with the next context in |contextQueue|. 73 // Shows the dialog associated with the next context in |contextQueue|.
74 - (void)showNextDialog; 74 - (void)showNextDialog;
75 75
76 // Called when a button in |coordinator| is tapped. 76 // Called when |coordinator| is stopped.
77 - (void)buttonWasTappedForCoordinator:(AlertCoordinator*)coordinator; 77 - (void)dialogCoordinatorWasStopped:(AlertCoordinator*)coordinator;
78 78
79 // Adds buttons to |alertCoordinator|. A confirmation button with |label| as 79 // Adds buttons to |alertCoordinator|. A confirmation button with |label| as
80 // the text will be added for |confirmAction|, and a cancel button will be added 80 // the text will be added for |confirmAction|, and a cancel button will be added
81 // for |cancelAction|. 81 // for |cancelAction|.
82 - (void)setUpAlertCoordinator:(AlertCoordinator*)alertCoordinator 82 - (void)setUpAlertCoordinator:(AlertCoordinator*)alertCoordinator
83 confirmAction:(ProceduralBlock)confirmAction 83 confirmAction:(ProceduralBlock)confirmAction
84 cancelAction:(ProceduralBlock)cancelAction 84 cancelAction:(ProceduralBlock)cancelAction
85 OKLabel:(NSString*)label; 85 OKLabel:(NSString*)label;
86 86
87 // Sets up the JavaScript dialog blocking option for |alertCoordinator|. 87 // Sets up the JavaScript dialog blocking option for |alertCoordinator|.
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 [[AlertCoordinator alloc] initWithBaseViewController:self.viewController 146 [[AlertCoordinator alloc] initWithBaseViewController:self.viewController
147 title:title 147 title:title
148 message:message]; 148 message:message];
149 149
150 // Handler. 150 // Handler.
151 __weak DialogPresenter* weakSelf = self; 151 __weak DialogPresenter* weakSelf = self;
152 __weak AlertCoordinator* weakCoordinator = alertCoordinator; 152 __weak AlertCoordinator* weakCoordinator = alertCoordinator;
153 ProceduralBlock OKHandler = ^{ 153 ProceduralBlock OKHandler = ^{
154 if (completionHandler) 154 if (completionHandler)
155 completionHandler(); 155 completionHandler();
156 [weakSelf buttonWasTappedForCoordinator:weakCoordinator]; 156 [weakSelf dialogCoordinatorWasStopped:weakCoordinator];
157 }; 157 };
158 158
159 // Add button. 159 // Add button.
160 [alertCoordinator addItemWithTitle:l10n_util::GetNSString(IDS_OK) 160 [alertCoordinator addItemWithTitle:l10n_util::GetNSString(IDS_OK)
161 action:OKHandler 161 action:OKHandler
162 style:UIAlertActionStyleDefault]; 162 style:UIAlertActionStyleDefault];
163 163
164 // Add cancel handler. 164 // Add cancel handler.
165 alertCoordinator.cancelAction = completionHandler; 165 alertCoordinator.cancelAction = completionHandler;
166 alertCoordinator.noInteractionAction = completionHandler; 166 alertCoordinator.noInteractionAction = completionHandler;
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 addTextFieldWithConfigurationHandler:^(UITextField* textField) { 305 addTextFieldWithConfigurationHandler:^(UITextField* textField) {
306 textField.placeholder = l10n_util::GetNSString( 306 textField.placeholder = l10n_util::GetNSString(
307 IDS_IOS_HTTP_LOGIN_DIALOG_PASSWORD_PLACEHOLDER); 307 IDS_IOS_HTTP_LOGIN_DIALOG_PASSWORD_PLACEHOLDER);
308 textField.secureTextEntry = YES; 308 textField.secureTextEntry = YES;
309 }]; 309 }];
310 310
311 [self addDialogCoordinator:alertCoordinator forWebState:webState]; 311 [self addDialogCoordinator:alertCoordinator forWebState:webState];
312 } 312 }
313 313
314 - (void)cancelDialogForWebState:(web::WebState*)webState { 314 - (void)cancelDialogForWebState:(web::WebState*)webState {
315 DCHECK_NE(webState, self.presentedDialogWebState); 315 BOOL cancelingPresentedDialog = webState == self.presentedDialogWebState;
316 AlertCoordinator* dialogToCancel = _dialogCoordinatorsForWebStates[webState]; 316 AlertCoordinator* dialogToCancel =
317 if (dialogToCancel) { 317 cancelingPresentedDialog ? self.presentedDialogCoordinator
318 : _dialogCoordinatorsForWebStates[webState];
319 DCHECK(!cancelingPresentedDialog || dialogToCancel);
320 [dialogToCancel executeCancelHandler];
321 [dialogToCancel stop];
322
323 if (cancelingPresentedDialog) {
324 DCHECK(_dialogCoordinatorsForWebStates[webState] == nil);
325 // Simulate a button tap to trigger showing the next dialog.
326 [self dialogCoordinatorWasStopped:dialogToCancel];
327 } else if (dialogToCancel) {
328 // Clean up queued state.
318 auto it = 329 auto it =
319 std::find(_queuedWebStates.begin(), _queuedWebStates.end(), webState); 330 std::find(_queuedWebStates.begin(), _queuedWebStates.end(), webState);
320 DCHECK(it != _queuedWebStates.end()); 331 DCHECK(it != _queuedWebStates.end());
321 _queuedWebStates.erase(it); 332 _queuedWebStates.erase(it);
322 [dialogToCancel executeCancelHandler];
323 [dialogToCancel stop];
324 _dialogCoordinatorsForWebStates.erase(webState); 333 _dialogCoordinatorsForWebStates.erase(webState);
325 } 334 }
326 } 335 }
327 336
328 - (void)cancelAllDialogs { 337 - (void)cancelAllDialogs {
329 [self.presentedDialogCoordinator executeCancelHandler]; 338 [self.presentedDialogCoordinator executeCancelHandler];
330 [self.presentedDialogCoordinator stop]; 339 [self.presentedDialogCoordinator stop];
331 self.presentedDialogCoordinator = nil; 340 self.presentedDialogCoordinator = nil;
332 self.presentedDialogWebState = nil; 341 self.presentedDialogWebState = nil;
333 while (!_queuedWebStates.empty()) { 342 while (!_queuedWebStates.empty()) {
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
379 _queuedWebStates.pop_front(); 388 _queuedWebStates.pop_front();
380 self.presentedDialogCoordinator = 389 self.presentedDialogCoordinator =
381 _dialogCoordinatorsForWebStates[self.presentedDialogWebState]; 390 _dialogCoordinatorsForWebStates[self.presentedDialogWebState];
382 _dialogCoordinatorsForWebStates.erase(self.presentedDialogWebState); 391 _dialogCoordinatorsForWebStates.erase(self.presentedDialogWebState);
383 // Notify the delegate and display the dialog. 392 // Notify the delegate and display the dialog.
384 [self.delegate dialogPresenter:self 393 [self.delegate dialogPresenter:self
385 willShowDialogForWebState:self.presentedDialogWebState]; 394 willShowDialogForWebState:self.presentedDialogWebState];
386 [self.presentedDialogCoordinator start]; 395 [self.presentedDialogCoordinator start];
387 } 396 }
388 397
389 - (void)buttonWasTappedForCoordinator:(AlertCoordinator*)coordinator { 398 - (void)dialogCoordinatorWasStopped:(AlertCoordinator*)coordinator {
390 if (coordinator != self.presentedDialogCoordinator) 399 if (coordinator != self.presentedDialogCoordinator)
391 return; 400 return;
392 self.presentedDialogWebState = nil; 401 self.presentedDialogWebState = nil;
393 self.presentedDialogCoordinator = nil; 402 self.presentedDialogCoordinator = nil;
394 self.blockingConfirmationCoordinator = nil; 403 self.blockingConfirmationCoordinator = nil;
395 if (!_queuedWebStates.empty() && !self.delegate.presenting) 404 if (!_queuedWebStates.empty() && !self.delegate.presenting)
396 [self showNextDialog]; 405 [self showNextDialog];
397 } 406 }
398 407
399 - (void)setUpAlertCoordinator:(AlertCoordinator*)alertCoordinator 408 - (void)setUpAlertCoordinator:(AlertCoordinator*)alertCoordinator
400 confirmAction:(ProceduralBlock)confirmAction 409 confirmAction:(ProceduralBlock)confirmAction
401 cancelAction:(ProceduralBlock)cancelAction 410 cancelAction:(ProceduralBlock)cancelAction
402 OKLabel:(NSString*)label { 411 OKLabel:(NSString*)label {
403 // Handlers. 412 // Handlers.
404 __weak DialogPresenter* weakSelf = self; 413 __weak DialogPresenter* weakSelf = self;
405 __weak AlertCoordinator* weakCoordinator = alertCoordinator; 414 __weak AlertCoordinator* weakCoordinator = alertCoordinator;
406 415
407 ProceduralBlock confirmHandler = ^{ 416 ProceduralBlock confirmHandler = ^{
408 if (confirmAction) 417 if (confirmAction)
409 confirmAction(); 418 confirmAction();
410 [weakSelf buttonWasTappedForCoordinator:weakCoordinator]; 419 [weakSelf dialogCoordinatorWasStopped:weakCoordinator];
411 }; 420 };
412 421
413 ProceduralBlock cancelHandler = ^{ 422 ProceduralBlock cancelHandler = ^{
414 if (cancelAction) 423 if (cancelAction)
415 cancelAction(); 424 cancelAction();
416 [weakSelf buttonWasTappedForCoordinator:weakCoordinator]; 425 [weakSelf dialogCoordinatorWasStopped:weakCoordinator];
417 }; 426 };
418 427
419 // Add buttons. 428 // Add buttons.
420 [alertCoordinator addItemWithTitle:label 429 [alertCoordinator addItemWithTitle:label
421 action:confirmHandler 430 action:confirmHandler
422 style:UIAlertActionStyleDefault]; 431 style:UIAlertActionStyleDefault];
423 [alertCoordinator addItemWithTitle:l10n_util::GetNSString(IDS_CANCEL) 432 [alertCoordinator addItemWithTitle:l10n_util::GetNSString(IDS_CANCEL)
424 action:cancelHandler 433 action:cancelHandler
425 style:UIAlertActionStyleCancel]; 434 style:UIAlertActionStyleCancel];
426 435
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
478 rect:CGRectZero 487 rect:CGRectZero
479 view:nil]; 488 view:nil];
480 // Set up button actions. 489 // Set up button actions.
481 ProceduralBlock confirmHandler = ^{ 490 ProceduralBlock confirmHandler = ^{
482 if (cancelAction) 491 if (cancelAction)
483 cancelAction(); 492 cancelAction();
484 DialogPresenter* strongSelf = weakSelf; 493 DialogPresenter* strongSelf = weakSelf;
485 if (!strongSelf) 494 if (!strongSelf)
486 return; 495 return;
487 DialogBlockingOptionSelected([strongSelf presentedDialogWebState]); 496 DialogBlockingOptionSelected([strongSelf presentedDialogWebState]);
488 [strongSelf buttonWasTappedForCoordinator:weakCoordinator]; 497 [strongSelf dialogCoordinatorWasStopped:weakCoordinator];
489 }; 498 };
490 ProceduralBlock cancelHandler = ^{ 499 ProceduralBlock cancelHandler = ^{
491 if (cancelAction) 500 if (cancelAction)
492 cancelAction(); 501 cancelAction();
493 [weakSelf buttonWasTappedForCoordinator:weakCoordinator]; 502 [weakSelf dialogCoordinatorWasStopped:weakCoordinator];
494 }; 503 };
495 NSString* blockingOptionTitle = 504 NSString* blockingOptionTitle =
496 l10n_util::GetNSString(IDS_IOS_JAVA_SCRIPT_DIALOG_BLOCKING_BUTTON_TEXT); 505 l10n_util::GetNSString(IDS_IOS_JAVA_SCRIPT_DIALOG_BLOCKING_BUTTON_TEXT);
497 [confirmationCoordinator addItemWithTitle:blockingOptionTitle 506 [confirmationCoordinator addItemWithTitle:blockingOptionTitle
498 action:confirmHandler 507 action:confirmHandler
499 style:UIAlertActionStyleDestructive]; 508 style:UIAlertActionStyleDestructive];
500 [confirmationCoordinator addItemWithTitle:l10n_util::GetNSString(IDS_CANCEL) 509 [confirmationCoordinator addItemWithTitle:l10n_util::GetNSString(IDS_CANCEL)
501 action:cancelHandler 510 action:cancelHandler
502 style:UIAlertActionStyleCancel]; 511 style:UIAlertActionStyleCancel];
503 [weakSelf setBlockingConfirmationCoordinator:confirmationCoordinator]; 512 [weakSelf setBlockingConfirmationCoordinator:confirmationCoordinator];
504 [[weakSelf blockingConfirmationCoordinator] start]; 513 [[weakSelf blockingConfirmationCoordinator] start];
505 } copy]; 514 } copy];
506 } 515 }
507 516
508 @end 517 @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