OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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 "components/autofill/ios/browser/js_suggestion_manager.h" |
| 6 |
| 7 #include "base/format_macros.h" |
| 8 #include "base/json/string_escape.h" |
| 9 #include "base/logging.h" |
| 10 #include "base/strings/sys_string_conversions.h" |
| 11 #import "ios/web/public/web_state/js/crw_js_early_script_manager.h" |
| 12 |
| 13 namespace { |
| 14 // Santizies |str| and wraps it in quotes so it can be injected safely in |
| 15 // JavaScript. |
| 16 NSString* JSONEscape(NSString* str) { |
| 17 return base::SysUTF8ToNSString( |
| 18 base::GetQuotedJSONString(base::SysNSStringToUTF8(str))); |
| 19 } |
| 20 } // namespace |
| 21 |
| 22 @implementation JsSuggestionManager |
| 23 |
| 24 #pragma mark - |
| 25 #pragma mark ProtectedMethods |
| 26 |
| 27 - (NSString*)scriptPath { |
| 28 return @"suggestion_controller"; |
| 29 } |
| 30 |
| 31 - (NSString*)presenceBeacon { |
| 32 return @"__gCrWeb.suggestion"; |
| 33 } |
| 34 |
| 35 - (NSArray*)directDependencies { |
| 36 return @[ [CRWJSEarlyScriptManager class] ]; |
| 37 } |
| 38 |
| 39 - (void)selectNextElement { |
| 40 [self selectElementAfterForm:@"" field:@""]; |
| 41 } |
| 42 |
| 43 - (void)selectElementAfterForm:(NSString*)formName field:(NSString*)fieldName { |
| 44 NSString* selectNextElementJS = [NSString |
| 45 stringWithFormat:@"__gCrWeb.suggestion.selectNextElement(%@, %@)", |
| 46 JSONEscape(formName), JSONEscape(fieldName)]; |
| 47 [self evaluate:selectNextElementJS stringResultHandler:nil]; |
| 48 } |
| 49 |
| 50 - (void)selectPreviousElement { |
| 51 [self selectElementBeforeForm:@"" field:@""]; |
| 52 } |
| 53 |
| 54 - (void)selectElementBeforeForm:(NSString*)formName field:(NSString*)fieldName { |
| 55 NSString* selectPreviousElementJS = [NSString |
| 56 stringWithFormat:@"__gCrWeb.suggestion.selectPreviousElement(%@, %@)", |
| 57 JSONEscape(formName), JSONEscape(fieldName)]; |
| 58 [self evaluate:selectPreviousElementJS stringResultHandler:nil]; |
| 59 } |
| 60 |
| 61 - (void)fetchPreviousAndNextElementsPresenceWithCompletionHandler: |
| 62 (void (^)(BOOL, BOOL))completionHandler { |
| 63 [self fetchPreviousAndNextElementsPresenceForForm:@"" |
| 64 field:@"" |
| 65 completionHandler:completionHandler]; |
| 66 } |
| 67 |
| 68 - (void)fetchPreviousAndNextElementsPresenceForForm:(NSString*)formName |
| 69 field:(NSString*)fieldName |
| 70 completionHandler: |
| 71 (void (^)(BOOL, BOOL))completionHandler { |
| 72 DCHECK(completionHandler); |
| 73 DCHECK([self hasBeenInjected]); |
| 74 id stringResultHandler = ^(NSString* result, NSError* error) { |
| 75 // The result maybe an empty string here due to 2 reasons: |
| 76 // 1) When there is an exception running the JS |
| 77 // 2) There is a race when the page is changing due to which |
| 78 // JSSuggestionManager has not yet injected __gCrWeb.suggestion object |
| 79 // Handle this case gracefully. |
| 80 // TODO(shreyasv): Figure out / narrow down further why this occurs. |
| 81 // crbug.com/432217. |
| 82 // If a page has overridden Array.toString, the string returned may not |
| 83 // contain a ",", hence this is a defensive measure to early return. |
| 84 NSArray* components = [result componentsSeparatedByString:@","]; |
| 85 if (components.count != 2) { |
| 86 completionHandler(NO, NO); |
| 87 return; |
| 88 } |
| 89 |
| 90 DCHECK([components[0] isEqualToString:@"true"] || |
| 91 [components[0] isEqualToString:@"false"]); |
| 92 BOOL hasPreviousElement = [components[0] isEqualToString:@"true"]; |
| 93 DCHECK([components[1] isEqualToString:@"true"] || |
| 94 [components[1] isEqualToString:@"false"]); |
| 95 BOOL hasNextElement = [components[1] isEqualToString:@"true"]; |
| 96 completionHandler(hasPreviousElement, hasNextElement); |
| 97 }; |
| 98 NSString* escapedFormName = JSONEscape(formName); |
| 99 NSString* escapedFieldName = JSONEscape(fieldName); |
| 100 NSString* js = [NSString |
| 101 stringWithFormat:@"[__gCrWeb.suggestion.hasPreviousElement(%@, %@)," |
| 102 @"__gCrWeb.suggestion.hasNextElement(%@, %@)]" |
| 103 @".toString()", |
| 104 escapedFormName, escapedFieldName, escapedFormName, |
| 105 escapedFieldName]; |
| 106 [self evaluate:js stringResultHandler:stringResultHandler]; |
| 107 } |
| 108 |
| 109 - (void)closeKeyboard { |
| 110 // Deferred execution used because of a risk of crwebinvoke:// triggered |
| 111 // immediately by the loss of focus. |
| 112 [self deferredEvaluate:@"document.activeElement.blur()"]; |
| 113 } |
| 114 |
| 115 @end |
OLD | NEW |