OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 "ios/clean/chrome/browser/ui/dialogs/java_script_dialogs/java_script_dia
log_request.h" |
| 6 |
| 7 #import "base/strings/sys_string_conversions.h" |
| 8 #include "components/strings/grit/components_strings.h" |
| 9 #import "ios/web/public/web_state/web_state.h" |
| 10 #include "ui/base/l10n/l10n_util.h" |
| 11 #include "url/gurl.h" |
| 12 |
| 13 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 14 #error "This file requires ARC support." |
| 15 #endif |
| 16 |
| 17 namespace { |
| 18 // The hostname to use for JavaScript dialogs when the origin URL is invalid. |
| 19 const char kAboutNullHostname[] = "about:null"; |
| 20 } // namespace |
| 21 |
| 22 @interface JavaScriptDialogRequest () { |
| 23 // The callback passed upon initialization. |
| 24 web::DialogClosedCallback _callback; |
| 25 // Whether |_callback| has been run; |
| 26 BOOL _callbackHasBeenExecuted; |
| 27 } |
| 28 |
| 29 // Private initializer used by factory method. |
| 30 - (instancetype)initWithWebState:(web::WebState*)webState |
| 31 type:(web::JavaScriptDialogType)type |
| 32 originURL:(const GURL&)originURL |
| 33 message:(NSString*)message |
| 34 defaultPromptText:(NSString*)defaultPromptText |
| 35 callback:(const web::DialogClosedCallback&)callback; |
| 36 |
| 37 // Returns the title to use when displaying dialogs from |originURL|. |
| 38 + (NSString*)titleForDialogFromOrigin:(const GURL&)originURL; |
| 39 |
| 40 @end |
| 41 |
| 42 @implementation JavaScriptDialogRequest |
| 43 |
| 44 @synthesize webState = _webState; |
| 45 @synthesize type = _type; |
| 46 @synthesize title = _title; |
| 47 @synthesize message = _message; |
| 48 @synthesize defaultPromptText = _defaultPromptText; |
| 49 |
| 50 - (instancetype)initWithWebState:(web::WebState*)webState |
| 51 type:(web::JavaScriptDialogType)type |
| 52 originURL:(const GURL&)originURL |
| 53 message:(NSString*)message |
| 54 defaultPromptText:(NSString*)defaultPromptText |
| 55 callback:(const web::DialogClosedCallback&)callback { |
| 56 if ((self = [super init])) { |
| 57 _webState = webState; |
| 58 _type = type; |
| 59 _title = [[self class] titleForDialogFromOrigin:originURL]; |
| 60 _message = message; |
| 61 _defaultPromptText = defaultPromptText; |
| 62 _callback = callback; |
| 63 } |
| 64 return self; |
| 65 } |
| 66 |
| 67 - (void)dealloc { |
| 68 DCHECK(_callbackHasBeenExecuted); |
| 69 } |
| 70 |
| 71 #pragma mark - Public |
| 72 |
| 73 + (instancetype)stateWithWebState:(web::WebState*)webState |
| 74 type:(web::JavaScriptDialogType)type |
| 75 originURL:(const GURL&)originURL |
| 76 message:(NSString*)message |
| 77 defaultPromptText:(NSString*)defaultPromptText |
| 78 callback:(const web::DialogClosedCallback&)callback { |
| 79 return [[[self class] alloc] initWithWebState:webState |
| 80 type:type |
| 81 originURL:originURL |
| 82 message:message |
| 83 defaultPromptText:defaultPromptText |
| 84 callback:callback]; |
| 85 } |
| 86 |
| 87 - (void)finishRequestWithSuccess:(BOOL)success userInput:(NSString*)userInput { |
| 88 if (_callbackHasBeenExecuted) |
| 89 return; |
| 90 _callback.Run(success, userInput); |
| 91 _callbackHasBeenExecuted = YES; |
| 92 } |
| 93 |
| 94 #pragma mark - |
| 95 |
| 96 + (NSString*)titleForDialogFromOrigin:(const GURL&)originURL { |
| 97 NSString* hostname = originURL.is_valid() |
| 98 ? base::SysUTF8ToNSString(originURL.host()) |
| 99 : base::SysUTF8ToNSString(kAboutNullHostname); |
| 100 return l10n_util::GetNSStringF(IDS_JAVASCRIPT_MESSAGEBOX_TITLE, |
| 101 base::SysNSStringToUTF16(hostname)); |
| 102 } |
| 103 |
| 104 @end |
OLD | NEW |