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

Side by Side Diff: components/autofill/ios/browser/js_suggestion_manager.mm

Issue 2247973002: [ios] Use new JS execution API for Autofill. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 4 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
« no previous file with comments | « components/autofill/ios/browser/js_autofill_manager.mm ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 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 "components/autofill/ios/browser/js_suggestion_manager.h" 5 #import "components/autofill/ios/browser/js_suggestion_manager.h"
6 6
7 #include "base/format_macros.h" 7 #include "base/format_macros.h"
8 #include "base/json/string_escape.h" 8 #include "base/json/string_escape.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/strings/sys_string_conversions.h" 10 #include "base/strings/sys_string_conversions.h"
(...skipping 17 matching lines...) Expand all
28 } 28 }
29 29
30 - (void)selectNextElement { 30 - (void)selectNextElement {
31 [self selectElementAfterForm:@"" field:@""]; 31 [self selectElementAfterForm:@"" field:@""];
32 } 32 }
33 33
34 - (void)selectElementAfterForm:(NSString*)formName field:(NSString*)fieldName { 34 - (void)selectElementAfterForm:(NSString*)formName field:(NSString*)fieldName {
35 NSString* selectNextElementJS = [NSString 35 NSString* selectNextElementJS = [NSString
36 stringWithFormat:@"__gCrWeb.suggestion.selectNextElement(%@, %@)", 36 stringWithFormat:@"__gCrWeb.suggestion.selectNextElement(%@, %@)",
37 JSONEscape(formName), JSONEscape(fieldName)]; 37 JSONEscape(formName), JSONEscape(fieldName)];
38 [self evaluate:selectNextElementJS stringResultHandler:nil]; 38 [self executeJavaScript:selectNextElementJS completionHandler:nil];
39 } 39 }
40 40
41 - (void)selectPreviousElement { 41 - (void)selectPreviousElement {
42 [self selectElementBeforeForm:@"" field:@""]; 42 [self selectElementBeforeForm:@"" field:@""];
43 } 43 }
44 44
45 - (void)selectElementBeforeForm:(NSString*)formName field:(NSString*)fieldName { 45 - (void)selectElementBeforeForm:(NSString*)formName field:(NSString*)fieldName {
46 NSString* selectPreviousElementJS = [NSString 46 NSString* selectPreviousElementJS = [NSString
47 stringWithFormat:@"__gCrWeb.suggestion.selectPreviousElement(%@, %@)", 47 stringWithFormat:@"__gCrWeb.suggestion.selectPreviousElement(%@, %@)",
48 JSONEscape(formName), JSONEscape(fieldName)]; 48 JSONEscape(formName), JSONEscape(fieldName)];
49 [self evaluate:selectPreviousElementJS stringResultHandler:nil]; 49 [self executeJavaScript:selectPreviousElementJS completionHandler:nil];
50 } 50 }
51 51
52 - (void)fetchPreviousAndNextElementsPresenceWithCompletionHandler: 52 - (void)fetchPreviousAndNextElementsPresenceWithCompletionHandler:
53 (void (^)(BOOL, BOOL))completionHandler { 53 (void (^)(BOOL, BOOL))completionHandler {
54 [self fetchPreviousAndNextElementsPresenceForForm:@"" 54 [self fetchPreviousAndNextElementsPresenceForForm:@""
55 field:@"" 55 field:@""
56 completionHandler:completionHandler]; 56 completionHandler:completionHandler];
57 } 57 }
58 58
59 - (void)fetchPreviousAndNextElementsPresenceForForm:(NSString*)formName 59 - (void)fetchPreviousAndNextElementsPresenceForForm:(NSString*)formName
60 field:(NSString*)fieldName 60 field:(NSString*)fieldName
61 completionHandler: 61 completionHandler:
62 (void (^)(BOOL, BOOL))completionHandler { 62 (void (^)(BOOL, BOOL))completionHandler {
63 DCHECK(completionHandler); 63 DCHECK(completionHandler);
64 DCHECK([self hasBeenInjected]); 64 DCHECK([self hasBeenInjected]);
65 id stringResultHandler = ^(NSString* result, NSError* error) { 65 NSString* escapedFormName = JSONEscape(formName);
66 NSString* escapedFieldName = JSONEscape(fieldName);
67 NSString* JS = [NSString
68 stringWithFormat:@"[__gCrWeb.suggestion.hasPreviousElement(%@, %@),"
69 @"__gCrWeb.suggestion.hasNextElement(%@, %@)]"
70 @".toString()",
71 escapedFormName, escapedFieldName, escapedFormName,
72 escapedFieldName];
73 [self executeJavaScript:JS completionHandler:^(id result, NSError* error) {
66 // The result maybe an empty string here due to 2 reasons: 74 // The result maybe an empty string here due to 2 reasons:
67 // 1) When there is an exception running the JS 75 // 1) When there is an exception running the JS
68 // 2) There is a race when the page is changing due to which 76 // 2) There is a race when the page is changing due to which
69 // JSSuggestionManager has not yet injected __gCrWeb.suggestion object 77 // JSSuggestionManager has not yet injected __gCrWeb.suggestion object
70 // Handle this case gracefully. 78 // Handle this case gracefully.
71 // TODO(shreyasv): Figure out / narrow down further why this occurs. 79 // TODO(shreyasv): Figure out / narrow down further why this occurs.
72 // crbug.com/432217. 80 // crbug.com/432217.
73 // If a page has overridden Array.toString, the string returned may not 81 // If a page has overridden Array.toString, the string returned may not
74 // contain a ",", hence this is a defensive measure to early return. 82 // contain a ",", hence this is a defensive measure to early return.
75 NSArray* components = [result componentsSeparatedByString:@","]; 83 NSArray* components = [result componentsSeparatedByString:@","];
Justin Donnelly 2016/08/17 14:29:18 Dumb Obj-C question: you don't need to cast result
Eugene But (OOO till 7-30) 2016/08/17 15:33:27 I think this code is fine as it is. .toString() sh
76 if (components.count != 2) { 84 if (components.count != 2) {
77 completionHandler(NO, NO); 85 completionHandler(NO, NO);
78 return; 86 return;
79 } 87 }
80 88
81 DCHECK([components[0] isEqualToString:@"true"] || 89 DCHECK([components[0] isEqualToString:@"true"] ||
82 [components[0] isEqualToString:@"false"]); 90 [components[0] isEqualToString:@"false"]);
83 BOOL hasPreviousElement = [components[0] isEqualToString:@"true"]; 91 BOOL hasPreviousElement = [components[0] isEqualToString:@"true"];
84 DCHECK([components[1] isEqualToString:@"true"] || 92 DCHECK([components[1] isEqualToString:@"true"] ||
85 [components[1] isEqualToString:@"false"]); 93 [components[1] isEqualToString:@"false"]);
86 BOOL hasNextElement = [components[1] isEqualToString:@"true"]; 94 BOOL hasNextElement = [components[1] isEqualToString:@"true"];
87 completionHandler(hasPreviousElement, hasNextElement); 95 completionHandler(hasPreviousElement, hasNextElement);
88 }; 96 }];
89 NSString* escapedFormName = JSONEscape(formName);
90 NSString* escapedFieldName = JSONEscape(fieldName);
91 NSString* js = [NSString
92 stringWithFormat:@"[__gCrWeb.suggestion.hasPreviousElement(%@, %@),"
93 @"__gCrWeb.suggestion.hasNextElement(%@, %@)]"
94 @".toString()",
95 escapedFormName, escapedFieldName, escapedFormName,
96 escapedFieldName];
97 [self evaluate:js stringResultHandler:stringResultHandler];
98 } 97 }
99 98
100 - (void)closeKeyboard { 99 - (void)closeKeyboard {
101 [self executeJavaScript:@"document.activeElement.blur()" 100 [self executeJavaScript:@"document.activeElement.blur()"
102 completionHandler:nil]; 101 completionHandler:nil];
103 } 102 }
104 103
105 @end 104 @end
OLDNEW
« no previous file with comments | « components/autofill/ios/browser/js_autofill_manager.mm ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698