| OLD | NEW |
| 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" |
| 11 #import "base/ios/weak_nsobject.h" | |
| 12 #include "base/logging.h" | 11 #include "base/logging.h" |
| 13 #import "base/mac/scoped_nsobject.h" | 12 #import "base/mac/scoped_nsobject.h" |
| 14 #include "base/strings/sys_string_conversions.h" | 13 #include "base/strings/sys_string_conversions.h" |
| 15 #include "components/strings/grit/components_strings.h" | 14 #include "components/strings/grit/components_strings.h" |
| 16 #import "ios/chrome/browser/ui/alert_coordinator/action_sheet_coordinator.h" | 15 #import "ios/chrome/browser/ui/alert_coordinator/action_sheet_coordinator.h" |
| 17 #import "ios/chrome/browser/ui/alert_coordinator/alert_coordinator.h" | 16 #import "ios/chrome/browser/ui/alert_coordinator/alert_coordinator.h" |
| 18 #import "ios/chrome/browser/ui/alert_coordinator/input_alert_coordinator.h" | 17 #import "ios/chrome/browser/ui/alert_coordinator/input_alert_coordinator.h" |
| 19 #import "ios/chrome/browser/ui/dialogs/javascript_dialog_blocking_util.h" | 18 #import "ios/chrome/browser/ui/dialogs/javascript_dialog_blocking_util.h" |
| 20 #import "ios/chrome/browser/ui/dialogs/nsurl_protection_space_util.h" | 19 #import "ios/chrome/browser/ui/dialogs/nsurl_protection_space_util.h" |
| 21 #include "ios/chrome/browser/ui/ui_util.h" | 20 #include "ios/chrome/browser/ui/ui_util.h" |
| 22 #include "ios/chrome/grit/ios_strings.h" | 21 #include "ios/chrome/grit/ios_strings.h" |
| 23 #include "ios/web/public/web_state/web_state.h" | 22 #include "ios/web/public/web_state/web_state.h" |
| 24 #include "ui/base/l10n/l10n_util.h" | 23 #include "ui/base/l10n/l10n_util.h" |
| 25 #include "url/gurl.h" | 24 #include "url/gurl.h" |
| 26 | 25 |
| 26 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 27 #error "This file requires ARC support." |
| 28 #endif |
| 29 |
| 27 // Externed accessibility identifier. | 30 // Externed accessibility identifier. |
| 28 NSString* const kJavaScriptDialogTextFieldAccessibiltyIdentifier = | 31 NSString* const kJavaScriptDialogTextFieldAccessibiltyIdentifier = |
| 29 @"JavaScriptDialogTextFieldAccessibiltyIdentifier"; | 32 @"JavaScriptDialogTextFieldAccessibiltyIdentifier"; |
| 30 | 33 |
| 31 namespace { | 34 namespace { |
| 32 // The hostname to use for JavaScript alerts when there is no valid hostname in | 35 // The hostname to use for JavaScript alerts when there is no valid hostname in |
| 33 // the URL passed to |+localizedTitleForJavaScriptAlertFromPage:type:|. | 36 // the URL passed to |+localizedTitleForJavaScriptAlertFromPage:type:|. |
| 34 const char kAboutNullHostname[] = "about:null"; | 37 const char kAboutNullHostname[] = "about:null"; |
| 35 } // namespace | 38 } // namespace |
| 36 | 39 |
| 37 @interface DialogPresenter () { | 40 @interface DialogPresenter () { |
| 38 // Backing objects for properties of the same name. | |
| 39 base::WeakNSProtocol<id<DialogPresenterDelegate>> _delegate; | |
| 40 base::WeakNSObject<UIViewController> _viewController; | |
| 41 // Queue of WebStates which correspond to the keys in | 41 // Queue of WebStates which correspond to the keys in |
| 42 // |_dialogCoordinatorsForWebStates|. | 42 // |_dialogCoordinatorsForWebStates|. |
| 43 std::deque<web::WebState*> _queuedWebStates; | 43 std::deque<web::WebState*> _queuedWebStates; |
| 44 // A map associating queued webStates with their coordinators. | 44 // A map associating queued webStates with their coordinators. |
| 45 std::map<web::WebState*, base::scoped_nsobject<AlertCoordinator>> | 45 std::map<web::WebState*, base::scoped_nsobject<AlertCoordinator>> |
| 46 _dialogCoordinatorsForWebStates; | 46 _dialogCoordinatorsForWebStates; |
| 47 web::WebState* _presentedDialogWebState; | |
| 48 base::scoped_nsobject<AlertCoordinator> _presentedDialogCoordinator; | |
| 49 base::scoped_nsobject<ActionSheetCoordinator> | |
| 50 _blockingConfirmationCoordinator; | |
| 51 } | 47 } |
| 52 | 48 |
| 53 // The delegate passed on initialization. | 49 // The delegate passed on initialization. |
| 54 @property(nonatomic, readonly) id<DialogPresenterDelegate> delegate; | 50 @property(weak, nonatomic, readonly) id<DialogPresenterDelegate> delegate; |
| 55 | 51 |
| 56 // The presenting view controller passed on initialization. | 52 // The presenting view controller passed on initialization. |
| 57 @property(nonatomic, readonly) UIViewController* viewController; | 53 @property(weak, nonatomic, readonly) UIViewController* viewController; |
| 58 | 54 |
| 59 // Whether a modal dialog is currently being shown. | 55 // Whether a modal dialog is currently being shown. |
| 60 @property(nonatomic, readonly, getter=isShowingDialog) BOOL showingDialog; | 56 @property(nonatomic, readonly, getter=isShowingDialog) BOOL showingDialog; |
| 61 | 57 |
| 62 // The webState for |presentedDialog|. | 58 // The webState for |presentedDialog|. |
| 63 @property(nonatomic) web::WebState* presentedDialogWebState; | 59 @property(nonatomic) web::WebState* presentedDialogWebState; |
| 64 | 60 |
| 65 // The dialog that's currently being shown, if any. | 61 // The dialog that's currently being shown, if any. |
| 66 @property(nonatomic, retain) AlertCoordinator* presentedDialogCoordinator; | 62 @property(nonatomic, strong) AlertCoordinator* presentedDialogCoordinator; |
| 67 | 63 |
| 68 // The JavaScript dialog blocking confirmation action sheet being shown, if any. | 64 // The JavaScript dialog blocking confirmation action sheet being shown, if any. |
| 69 @property(nonatomic, retain) AlertCoordinator* blockingConfirmationCoordinator; | 65 @property(nonatomic, strong) AlertCoordinator* blockingConfirmationCoordinator; |
| 70 | 66 |
| 71 // 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 |
| 72 // being shown, |coordinator| will be presented. Otherwise, |coordinator| will | 68 // being shown, |coordinator| will be presented. Otherwise, |coordinator| will |
| 73 // be displayed once the previously shown dialog is dismissed. | 69 // be displayed once the previously shown dialog is dismissed. |
| 74 - (void)addDialogCoordinator:(AlertCoordinator*)coordinator | 70 - (void)addDialogCoordinator:(AlertCoordinator*)coordinator |
| 75 forWebState:(web::WebState*)webState; | 71 forWebState:(web::WebState*)webState; |
| 76 | 72 |
| 77 // Shows the dialog associated with the next context in |contextQueue|. | 73 // Shows the dialog associated with the next context in |contextQueue|. |
| 78 - (void)showNextDialog; | 74 - (void)showNextDialog; |
| 79 | 75 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 99 webState:(web::WebState*)webState; | 95 webState:(web::WebState*)webState; |
| 100 | 96 |
| 101 // The block to use for the JavaScript dialog blocking option for |coordinator|. | 97 // The block to use for the JavaScript dialog blocking option for |coordinator|. |
| 102 - (ProceduralBlock)blockingActionForCoordinator:(AlertCoordinator*)coordinator; | 98 - (ProceduralBlock)blockingActionForCoordinator:(AlertCoordinator*)coordinator; |
| 103 | 99 |
| 104 @end | 100 @end |
| 105 | 101 |
| 106 @implementation DialogPresenter | 102 @implementation DialogPresenter |
| 107 | 103 |
| 108 @synthesize active = _active; | 104 @synthesize active = _active; |
| 105 @synthesize delegate = _delegate; |
| 106 @synthesize viewController = _viewController; |
| 107 @synthesize presentedDialogCoordinator = _presentedDialogCoordinator; |
| 108 @synthesize blockingConfirmationCoordinator = _blockingConfirmationCoordinator; |
| 109 @synthesize presentedDialogWebState = _presentedDialogWebState; |
| 109 | 110 |
| 110 - (instancetype)initWithDelegate:(id<DialogPresenterDelegate>)delegate | 111 - (instancetype)initWithDelegate:(id<DialogPresenterDelegate>)delegate |
| 111 presentingViewController:(UIViewController*)viewController { | 112 presentingViewController:(UIViewController*)viewController { |
| 112 if ((self = [super init])) { | 113 if ((self = [super init])) { |
| 113 DCHECK(delegate); | 114 DCHECK(delegate); |
| 114 DCHECK(viewController); | 115 DCHECK(viewController); |
| 115 _delegate.reset(delegate); | 116 _delegate = delegate; |
| 116 _viewController.reset(viewController); | 117 _viewController = viewController; |
| 117 } | 118 } |
| 118 return self; | 119 return self; |
| 119 } | 120 } |
| 120 | 121 |
| 121 #pragma mark - Accessors | 122 #pragma mark - Accessors |
| 122 | 123 |
| 123 - (void)setActive:(BOOL)active { | 124 - (void)setActive:(BOOL)active { |
| 124 if (_active != active) { | 125 if (_active != active) { |
| 125 _active = active; | 126 _active = active; |
| 126 [self tryToPresent]; | 127 [self tryToPresent]; |
| 127 } | 128 } |
| 128 } | 129 } |
| 129 | 130 |
| 130 - (id<DialogPresenterDelegate>)delegate { | |
| 131 return _delegate; | |
| 132 } | |
| 133 | |
| 134 - (UIViewController*)viewController { | |
| 135 return _viewController; | |
| 136 } | |
| 137 | |
| 138 - (BOOL)isShowingDialog { | 131 - (BOOL)isShowingDialog { |
| 139 DCHECK_EQ(self.presentedDialogWebState != nullptr, | 132 DCHECK_EQ(self.presentedDialogWebState != nullptr, |
| 140 self.presentedDialogCoordinator != nil); | 133 self.presentedDialogCoordinator != nil); |
| 141 return self.presentedDialogCoordinator != nil; | 134 return self.presentedDialogCoordinator != nil; |
| 142 } | 135 } |
| 143 | 136 |
| 144 - (web::WebState*)presentedDialogWebState { | |
| 145 return _presentedDialogWebState; | |
| 146 } | |
| 147 | |
| 148 - (void)setPresentedDialogWebState:(web::WebState*)presentedDialogWebState { | |
| 149 _presentedDialogWebState = presentedDialogWebState; | |
| 150 } | |
| 151 | |
| 152 - (AlertCoordinator*)presentedDialogCoordinator { | |
| 153 return _presentedDialogCoordinator; | |
| 154 } | |
| 155 | |
| 156 - (void)setPresentedDialogCoordinator: | |
| 157 (AlertCoordinator*)presentedDialogCoordinator { | |
| 158 _presentedDialogCoordinator.reset([presentedDialogCoordinator retain]); | |
| 159 } | |
| 160 | |
| 161 - (ActionSheetCoordinator*)blockingConfirmationCoordinator { | |
| 162 return _blockingConfirmationCoordinator; | |
| 163 } | |
| 164 | |
| 165 - (void)setBlockingConfirmationCoordinator: | |
| 166 (ActionSheetCoordinator*)blockingConfirmationActionSheetCoordinator { | |
| 167 _blockingConfirmationCoordinator.reset( | |
| 168 [blockingConfirmationActionSheetCoordinator retain]); | |
| 169 } | |
| 170 | |
| 171 #pragma mark - Public | 137 #pragma mark - Public |
| 172 | 138 |
| 173 - (void)runJavaScriptAlertPanelWithMessage:(NSString*)message | 139 - (void)runJavaScriptAlertPanelWithMessage:(NSString*)message |
| 174 requestURL:(const GURL&)requestURL | 140 requestURL:(const GURL&)requestURL |
| 175 webState:(web::WebState*)webState | 141 webState:(web::WebState*)webState |
| 176 completionHandler:(void (^)(void))completionHandler { | 142 completionHandler:(void (^)(void))completionHandler { |
| 177 NSString* title = | 143 NSString* title = |
| 178 [DialogPresenter localizedTitleForJavaScriptAlertFromPage:requestURL]; | 144 [DialogPresenter localizedTitleForJavaScriptAlertFromPage:requestURL]; |
| 179 AlertCoordinator* alertCoordinator = [[[AlertCoordinator alloc] | 145 AlertCoordinator* alertCoordinator = |
| 180 initWithBaseViewController:self.viewController | 146 [[AlertCoordinator alloc] initWithBaseViewController:self.viewController |
| 181 title:title | 147 title:title |
| 182 message:message] autorelease]; | 148 message:message]; |
| 183 | 149 |
| 184 // Handler. | 150 // Handler. |
| 185 base::WeakNSObject<DialogPresenter> weakSelf(self); | 151 __weak DialogPresenter* weakSelf = self; |
| 186 base::WeakNSObject<AlertCoordinator> weakCoordinator(alertCoordinator); | 152 __weak AlertCoordinator* weakCoordinator = alertCoordinator; |
| 187 ProceduralBlock OKHandler = ^{ | 153 ProceduralBlock OKHandler = ^{ |
| 188 if (completionHandler) | 154 if (completionHandler) |
| 189 completionHandler(); | 155 completionHandler(); |
| 190 [weakSelf buttonWasTappedForCoordinator:weakCoordinator]; | 156 [weakSelf buttonWasTappedForCoordinator:weakCoordinator]; |
| 191 }; | 157 }; |
| 192 | 158 |
| 193 // Add button. | 159 // Add button. |
| 194 [alertCoordinator addItemWithTitle:l10n_util::GetNSString(IDS_OK) | 160 [alertCoordinator addItemWithTitle:l10n_util::GetNSString(IDS_OK) |
| 195 action:OKHandler | 161 action:OKHandler |
| 196 style:UIAlertActionStyleDefault]; | 162 style:UIAlertActionStyleDefault]; |
| 197 | 163 |
| 198 // Add cancel handler. | 164 // Add cancel handler. |
| 199 alertCoordinator.cancelAction = completionHandler; | 165 alertCoordinator.cancelAction = completionHandler; |
| 200 alertCoordinator.noInteractionAction = completionHandler; | 166 alertCoordinator.noInteractionAction = completionHandler; |
| 201 | 167 |
| 202 // Blocking option setup. | 168 // Blocking option setup. |
| 203 [self setUpBlockingOptionForCoordinator:alertCoordinator webState:webState]; | 169 [self setUpBlockingOptionForCoordinator:alertCoordinator webState:webState]; |
| 204 | 170 |
| 205 [self addDialogCoordinator:alertCoordinator forWebState:webState]; | 171 [self addDialogCoordinator:alertCoordinator forWebState:webState]; |
| 206 } | 172 } |
| 207 | 173 |
| 208 - (void)runJavaScriptConfirmPanelWithMessage:(NSString*)message | 174 - (void)runJavaScriptConfirmPanelWithMessage:(NSString*)message |
| 209 requestURL:(const GURL&)requestURL | 175 requestURL:(const GURL&)requestURL |
| 210 webState:(web::WebState*)webState | 176 webState:(web::WebState*)webState |
| 211 completionHandler: | 177 completionHandler: |
| 212 (void (^)(BOOL isConfirmed))completionHandler { | 178 (void (^)(BOOL isConfirmed))completionHandler { |
| 213 NSString* title = | 179 NSString* title = |
| 214 [DialogPresenter localizedTitleForJavaScriptAlertFromPage:requestURL]; | 180 [DialogPresenter localizedTitleForJavaScriptAlertFromPage:requestURL]; |
| 215 AlertCoordinator* alertCoordinator = [[[AlertCoordinator alloc] | 181 AlertCoordinator* alertCoordinator = |
| 216 initWithBaseViewController:self.viewController | 182 [[AlertCoordinator alloc] initWithBaseViewController:self.viewController |
| 217 title:title | 183 title:title |
| 218 message:message] autorelease]; | 184 message:message]; |
| 219 | 185 |
| 220 // Actions. | 186 // Actions. |
| 221 ProceduralBlock confirmAction = ^{ | 187 ProceduralBlock confirmAction = ^{ |
| 222 if (completionHandler) | 188 if (completionHandler) |
| 223 completionHandler(YES); | 189 completionHandler(YES); |
| 224 }; | 190 }; |
| 225 | 191 |
| 226 ProceduralBlock cancelAction = ^{ | 192 ProceduralBlock cancelAction = ^{ |
| 227 if (completionHandler) | 193 if (completionHandler) |
| 228 completionHandler(NO); | 194 completionHandler(NO); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 242 } | 208 } |
| 243 | 209 |
| 244 - (void)runJavaScriptTextInputPanelWithPrompt:(NSString*)message | 210 - (void)runJavaScriptTextInputPanelWithPrompt:(NSString*)message |
| 245 defaultText:(NSString*)defaultText | 211 defaultText:(NSString*)defaultText |
| 246 requestURL:(const GURL&)requestURL | 212 requestURL:(const GURL&)requestURL |
| 247 webState:(web::WebState*)webState | 213 webState:(web::WebState*)webState |
| 248 completionHandler: | 214 completionHandler: |
| 249 (void (^)(NSString* input))completionHandler { | 215 (void (^)(NSString* input))completionHandler { |
| 250 NSString* title = | 216 NSString* title = |
| 251 [DialogPresenter localizedTitleForJavaScriptAlertFromPage:requestURL]; | 217 [DialogPresenter localizedTitleForJavaScriptAlertFromPage:requestURL]; |
| 252 InputAlertCoordinator* alertCoordinator = [[[InputAlertCoordinator alloc] | 218 InputAlertCoordinator* alertCoordinator = [[InputAlertCoordinator alloc] |
| 253 initWithBaseViewController:self.viewController | 219 initWithBaseViewController:self.viewController |
| 254 title:title | 220 title:title |
| 255 message:message] autorelease]; | 221 message:message]; |
| 256 | 222 |
| 257 // Actions. | 223 // Actions. |
| 258 base::WeakNSObject<InputAlertCoordinator> weakCoordinator(alertCoordinator); | 224 __weak InputAlertCoordinator* weakCoordinator = alertCoordinator; |
| 259 ProceduralBlock confirmAction = ^{ | 225 ProceduralBlock confirmAction = ^{ |
| 260 if (completionHandler) { | 226 if (completionHandler) { |
| 261 NSString* textInput = [weakCoordinator textFields].firstObject.text; | 227 NSString* textInput = [weakCoordinator textFields].firstObject.text; |
| 262 completionHandler(textInput ? textInput : @""); | 228 completionHandler(textInput ? textInput : @""); |
| 263 } | 229 } |
| 264 }; | 230 }; |
| 265 | 231 |
| 266 ProceduralBlock cancelAction = ^{ | 232 ProceduralBlock cancelAction = ^{ |
| 267 if (completionHandler) | 233 if (completionHandler) |
| 268 completionHandler(nil); | 234 completionHandler(nil); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 292 - (void)runAuthDialogForProtectionSpace:(NSURLProtectionSpace*)protectionSpace | 258 - (void)runAuthDialogForProtectionSpace:(NSURLProtectionSpace*)protectionSpace |
| 293 proposedCredential:(NSURLCredential*)credential | 259 proposedCredential:(NSURLCredential*)credential |
| 294 webState:(web::WebState*)webState | 260 webState:(web::WebState*)webState |
| 295 completionHandler:(void (^)(NSString* user, | 261 completionHandler:(void (^)(NSString* user, |
| 296 NSString* password))handler { | 262 NSString* password))handler { |
| 297 NSString* title = l10n_util::GetNSStringWithFixup(IDS_LOGIN_DIALOG_TITLE); | 263 NSString* title = l10n_util::GetNSStringWithFixup(IDS_LOGIN_DIALOG_TITLE); |
| 298 NSString* message = | 264 NSString* message = |
| 299 ios_internal::nsurlprotectionspace_util::MessageForHTTPAuth( | 265 ios_internal::nsurlprotectionspace_util::MessageForHTTPAuth( |
| 300 protectionSpace); | 266 protectionSpace); |
| 301 | 267 |
| 302 InputAlertCoordinator* alertCoordinator = [[[InputAlertCoordinator alloc] | 268 InputAlertCoordinator* alertCoordinator = [[InputAlertCoordinator alloc] |
| 303 initWithBaseViewController:self.viewController | 269 initWithBaseViewController:self.viewController |
| 304 title:title | 270 title:title |
| 305 message:message] autorelease]; | 271 message:message]; |
| 306 | 272 |
| 307 // Actions. | 273 // Actions. |
| 308 base::WeakNSObject<InputAlertCoordinator> weakCoordinator(alertCoordinator); | 274 __weak InputAlertCoordinator* weakCoordinator = alertCoordinator; |
| 309 ProceduralBlock confirmAction = ^{ | 275 ProceduralBlock confirmAction = ^{ |
| 310 if (handler) { | 276 if (handler) { |
| 311 NSString* username = [[weakCoordinator textFields] objectAtIndex:0].text; | 277 NSString* username = [[weakCoordinator textFields] objectAtIndex:0].text; |
| 312 NSString* password = [[weakCoordinator textFields] objectAtIndex:1].text; | 278 NSString* password = [[weakCoordinator textFields] objectAtIndex:1].text; |
| 313 handler(username, password); | 279 handler(username, password); |
| 314 } | 280 } |
| 315 }; | 281 }; |
| 316 | 282 |
| 317 ProceduralBlock cancelAction = ^{ | 283 ProceduralBlock cancelAction = ^{ |
| 318 if (handler) | 284 if (handler) |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 391 #pragma mark - Private methods. | 357 #pragma mark - Private methods. |
| 392 | 358 |
| 393 - (void)addDialogCoordinator:(AlertCoordinator*)coordinator | 359 - (void)addDialogCoordinator:(AlertCoordinator*)coordinator |
| 394 forWebState:(web::WebState*)webState { | 360 forWebState:(web::WebState*)webState { |
| 395 DCHECK(coordinator); | 361 DCHECK(coordinator); |
| 396 DCHECK(webState); | 362 DCHECK(webState); |
| 397 DCHECK_NE(webState, self.presentedDialogWebState); | 363 DCHECK_NE(webState, self.presentedDialogWebState); |
| 398 DCHECK(!_dialogCoordinatorsForWebStates[webState]); | 364 DCHECK(!_dialogCoordinatorsForWebStates[webState]); |
| 399 _queuedWebStates.push_back(webState); | 365 _queuedWebStates.push_back(webState); |
| 400 _dialogCoordinatorsForWebStates[webState] = | 366 _dialogCoordinatorsForWebStates[webState] = |
| 401 base::scoped_nsobject<AlertCoordinator>([coordinator retain]); | 367 base::scoped_nsobject<AlertCoordinator>(coordinator); |
| 402 | 368 |
| 403 if (self.active && !self.showingDialog && !self.delegate.presenting) | 369 if (self.active && !self.showingDialog && !self.delegate.presenting) |
| 404 [self showNextDialog]; | 370 [self showNextDialog]; |
| 405 } | 371 } |
| 406 | 372 |
| 407 - (void)showNextDialog { | 373 - (void)showNextDialog { |
| 408 DCHECK(self.active); | 374 DCHECK(self.active); |
| 409 DCHECK(!self.showingDialog); | 375 DCHECK(!self.showingDialog); |
| 410 DCHECK(!_queuedWebStates.empty()); | 376 DCHECK(!_queuedWebStates.empty()); |
| 411 // Update properties and remove context and the dialog from queue. | 377 // Update properties and remove context and the dialog from queue. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 428 self.blockingConfirmationCoordinator = nil; | 394 self.blockingConfirmationCoordinator = nil; |
| 429 if (!_queuedWebStates.empty() && !self.delegate.presenting) | 395 if (!_queuedWebStates.empty() && !self.delegate.presenting) |
| 430 [self showNextDialog]; | 396 [self showNextDialog]; |
| 431 } | 397 } |
| 432 | 398 |
| 433 - (void)setUpAlertCoordinator:(AlertCoordinator*)alertCoordinator | 399 - (void)setUpAlertCoordinator:(AlertCoordinator*)alertCoordinator |
| 434 confirmAction:(ProceduralBlock)confirmAction | 400 confirmAction:(ProceduralBlock)confirmAction |
| 435 cancelAction:(ProceduralBlock)cancelAction | 401 cancelAction:(ProceduralBlock)cancelAction |
| 436 OKLabel:(NSString*)label { | 402 OKLabel:(NSString*)label { |
| 437 // Handlers. | 403 // Handlers. |
| 438 base::WeakNSObject<DialogPresenter> weakSelf(self); | 404 __weak DialogPresenter* weakSelf = self; |
| 439 base::WeakNSObject<AlertCoordinator> weakCoordinator(alertCoordinator); | 405 __weak AlertCoordinator* weakCoordinator = alertCoordinator; |
| 440 | 406 |
| 441 ProceduralBlock confirmHandler = ^{ | 407 ProceduralBlock confirmHandler = ^{ |
| 442 if (confirmAction) | 408 if (confirmAction) |
| 443 confirmAction(); | 409 confirmAction(); |
| 444 [weakSelf buttonWasTappedForCoordinator:weakCoordinator]; | 410 [weakSelf buttonWasTappedForCoordinator:weakCoordinator]; |
| 445 }; | 411 }; |
| 446 | 412 |
| 447 ProceduralBlock cancelHandler = ^{ | 413 ProceduralBlock cancelHandler = ^{ |
| 448 if (cancelAction) | 414 if (cancelAction) |
| 449 cancelAction(); | 415 cancelAction(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 462 alertCoordinator.cancelAction = cancelAction; | 428 alertCoordinator.cancelAction = cancelAction; |
| 463 alertCoordinator.noInteractionAction = cancelAction; | 429 alertCoordinator.noInteractionAction = cancelAction; |
| 464 } | 430 } |
| 465 | 431 |
| 466 - (void)setUpBlockingOptionForCoordinator:(AlertCoordinator*)alertCoordinator | 432 - (void)setUpBlockingOptionForCoordinator:(AlertCoordinator*)alertCoordinator |
| 467 webState:(web::WebState*)webState { | 433 webState:(web::WebState*)webState { |
| 468 DCHECK(alertCoordinator); | 434 DCHECK(alertCoordinator); |
| 469 DCHECK(webState); | 435 DCHECK(webState); |
| 470 | 436 |
| 471 // Set up the start action. | 437 // Set up the start action. |
| 472 base::WeakNSObject<DialogPresenter> weakSelf(self); | |
| 473 base::WeakNSObject<AlertCoordinator> weakCoordinator(alertCoordinator); | |
| 474 ProceduralBlock originalStartAction = alertCoordinator.startAction; | 438 ProceduralBlock originalStartAction = alertCoordinator.startAction; |
| 475 alertCoordinator.startAction = ^{ | 439 alertCoordinator.startAction = ^{ |
| 476 if (originalStartAction) | 440 if (originalStartAction) |
| 477 originalStartAction(); | 441 originalStartAction(); |
| 478 JavaScriptDialogWasShown(webState); | 442 JavaScriptDialogWasShown(webState); |
| 479 }; | 443 }; |
| 480 | 444 |
| 481 // Early return if a blocking option should not be added. | 445 // Early return if a blocking option should not be added. |
| 482 if (!ShouldShowDialogBlockingOption(webState)) | 446 if (!ShouldShowDialogBlockingOption(webState)) |
| 483 return; | 447 return; |
| 484 | 448 |
| 485 ProceduralBlock blockingAction = | 449 ProceduralBlock blockingAction = |
| 486 [self blockingActionForCoordinator:alertCoordinator]; | 450 [self blockingActionForCoordinator:alertCoordinator]; |
| 487 NSString* blockingOptionTitle = | 451 NSString* blockingOptionTitle = |
| 488 l10n_util::GetNSString(IDS_IOS_JAVA_SCRIPT_DIALOG_BLOCKING_BUTTON_TEXT); | 452 l10n_util::GetNSString(IDS_IOS_JAVA_SCRIPT_DIALOG_BLOCKING_BUTTON_TEXT); |
| 489 [alertCoordinator addItemWithTitle:blockingOptionTitle | 453 [alertCoordinator addItemWithTitle:blockingOptionTitle |
| 490 action:blockingAction | 454 action:blockingAction |
| 491 style:UIAlertActionStyleDefault]; | 455 style:UIAlertActionStyleDefault]; |
| 492 } | 456 } |
| 493 | 457 |
| 494 - (ProceduralBlock)blockingActionForCoordinator:(AlertCoordinator*)coordinator { | 458 - (ProceduralBlock)blockingActionForCoordinator:(AlertCoordinator*)coordinator { |
| 495 base::WeakNSObject<DialogPresenter> weakSelf(self); | 459 __weak DialogPresenter* weakSelf = self; |
| 496 base::WeakNSObject<AlertCoordinator> weakCoordinator(coordinator); | 460 __weak AlertCoordinator* weakCoordinator = coordinator; |
| 497 base::WeakNSObject<UIViewController> weakBaseViewController( | 461 __weak UIViewController* weakBaseViewController = |
| 498 coordinator.baseViewController); | 462 coordinator.baseViewController; |
| 499 ProceduralBlock cancelAction = coordinator.cancelAction; | 463 ProceduralBlock cancelAction = coordinator.cancelAction; |
| 500 return [[^{ | 464 return [^{ |
| 501 // Create the confirmation coordinator. Use an action sheet on iPhone and | 465 // Create the confirmation coordinator. Use an action sheet on iPhone and |
| 502 // an alert on iPhone. | 466 // an alert on iPhone. |
| 503 NSString* confirmMessage = | 467 NSString* confirmMessage = |
| 504 l10n_util::GetNSString(IDS_JAVASCRIPT_MESSAGEBOX_SUPPRESS_OPTION); | 468 l10n_util::GetNSString(IDS_JAVASCRIPT_MESSAGEBOX_SUPPRESS_OPTION); |
| 505 AlertCoordinator* confirmationCoordinator = | 469 AlertCoordinator* confirmationCoordinator = |
| 506 IsIPadIdiom() | 470 IsIPadIdiom() ? [[AlertCoordinator alloc] |
| 507 ? [[[AlertCoordinator alloc] | 471 initWithBaseViewController:weakBaseViewController |
| 508 initWithBaseViewController:weakBaseViewController | 472 title:nil |
| 509 title:nil | 473 message:confirmMessage] |
| 510 message:confirmMessage] autorelease] | 474 : [[ActionSheetCoordinator alloc] |
| 511 : [[[ActionSheetCoordinator alloc] | 475 initWithBaseViewController:weakBaseViewController |
| 512 initWithBaseViewController:weakBaseViewController | 476 title:nil |
| 513 title:nil | 477 message:confirmMessage |
| 514 message:confirmMessage | 478 rect:CGRectZero |
| 515 rect:CGRectZero | 479 view:nil]; |
| 516 view:nil] autorelease]; | |
| 517 // Set up button actions. | 480 // Set up button actions. |
| 518 ProceduralBlock confirmHandler = ^{ | 481 ProceduralBlock confirmHandler = ^{ |
| 519 if (cancelAction) | 482 if (cancelAction) |
| 520 cancelAction(); | 483 cancelAction(); |
| 521 base::scoped_nsobject<DialogPresenter> strongSelf([weakSelf retain]); | 484 DialogPresenter* strongSelf = weakSelf; |
| 522 if (!strongSelf) | 485 if (!strongSelf) |
| 523 return; | 486 return; |
| 524 DialogBlockingOptionSelected([strongSelf presentedDialogWebState]); | 487 DialogBlockingOptionSelected([strongSelf presentedDialogWebState]); |
| 525 [strongSelf buttonWasTappedForCoordinator:weakCoordinator]; | 488 [strongSelf buttonWasTappedForCoordinator:weakCoordinator]; |
| 526 }; | 489 }; |
| 527 ProceduralBlock cancelHandler = ^{ | 490 ProceduralBlock cancelHandler = ^{ |
| 528 if (cancelAction) | 491 if (cancelAction) |
| 529 cancelAction(); | 492 cancelAction(); |
| 530 [weakSelf buttonWasTappedForCoordinator:weakCoordinator]; | 493 [weakSelf buttonWasTappedForCoordinator:weakCoordinator]; |
| 531 }; | 494 }; |
| 532 NSString* blockingOptionTitle = | 495 NSString* blockingOptionTitle = |
| 533 l10n_util::GetNSString(IDS_IOS_JAVA_SCRIPT_DIALOG_BLOCKING_BUTTON_TEXT); | 496 l10n_util::GetNSString(IDS_IOS_JAVA_SCRIPT_DIALOG_BLOCKING_BUTTON_TEXT); |
| 534 [confirmationCoordinator addItemWithTitle:blockingOptionTitle | 497 [confirmationCoordinator addItemWithTitle:blockingOptionTitle |
| 535 action:confirmHandler | 498 action:confirmHandler |
| 536 style:UIAlertActionStyleDestructive]; | 499 style:UIAlertActionStyleDestructive]; |
| 537 [confirmationCoordinator addItemWithTitle:l10n_util::GetNSString(IDS_CANCEL) | 500 [confirmationCoordinator addItemWithTitle:l10n_util::GetNSString(IDS_CANCEL) |
| 538 action:cancelHandler | 501 action:cancelHandler |
| 539 style:UIAlertActionStyleCancel]; | 502 style:UIAlertActionStyleCancel]; |
| 540 [weakSelf setBlockingConfirmationCoordinator:confirmationCoordinator]; | 503 [weakSelf setBlockingConfirmationCoordinator:confirmationCoordinator]; |
| 541 [[weakSelf blockingConfirmationCoordinator] start]; | 504 [[weakSelf blockingConfirmationCoordinator] start]; |
| 542 } copy] autorelease]; | 505 } copy]; |
| 543 } | 506 } |
| 544 | 507 |
| 545 @end | 508 @end |
| OLD | NEW |