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

Side by Side Diff: components/autofill/ios/browser/js_autofill_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
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_autofill_manager.h" 5 #import "components/autofill/ios/browser/js_autofill_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/mac/foundation_util.h"
10 11
11 @implementation JsAutofillManager 12 @implementation JsAutofillManager
12 13
13 - (void)fetchFormsWithMinimumRequiredFieldsCount:(NSUInteger)requiredFieldsCount 14 - (void)fetchFormsWithMinimumRequiredFieldsCount:(NSUInteger)requiredFieldsCount
14 completionHandler: 15 completionHandler:
15 (void (^)(NSString*))completionHandler { 16 (void (^)(NSString*))completionHandler {
16 DCHECK(completionHandler); 17 DCHECK(completionHandler);
17 NSString* extractFormsJS = [NSString 18 NSString* extractFormsJS = [NSString
18 stringWithFormat:@"__gCrWeb.autofill.extractForms(%" PRIuNS ");", 19 stringWithFormat:@"__gCrWeb.autofill.extractForms(%" PRIuNS ");",
19 requiredFieldsCount]; 20 requiredFieldsCount];
20 [self evaluate:extractFormsJS 21 [self executeJavaScript:extractFormsJS
21 stringResultHandler:^(NSString* result, NSError*) { 22 completionHandler:^(id result, NSError*) {
22 completionHandler(result); 23 completionHandler(base::mac::ObjCCastStrict<NSString>(result));
23 }]; 24 }];
24 } 25 }
25 26
26 #pragma mark - 27 #pragma mark -
27 #pragma mark ProtectedMethods 28 #pragma mark ProtectedMethods
28 29
29 - (NSString*)scriptPath { 30 - (NSString*)scriptPath {
30 return @"autofill_controller"; 31 return @"autofill_controller";
31 } 32 }
32 33
33 - (void)fillActiveFormField:(NSString*)dataString 34 - (void)fillActiveFormField:(NSString*)dataString
34 completionHandler:(ProceduralBlock)completionHandler { 35 completionHandler:(ProceduralBlock)completionHandler {
35 web::JavaScriptCompletion resultHandler = ^void(NSString*, NSError*) { 36 NSString* script =
36 completionHandler();
37 };
38
39 NSString* js =
40 [NSString stringWithFormat:@"__gCrWeb.autofill.fillActiveFormField(%@);", 37 [NSString stringWithFormat:@"__gCrWeb.autofill.fillActiveFormField(%@);",
41 dataString]; 38 dataString];
42 [self evaluate:js stringResultHandler:resultHandler]; 39 [self executeJavaScript:script completionHandler:^(id, NSError*) {
40 completionHandler();
41 }];
43 } 42 }
44 43
45 - (void)fillForm:(NSString*)dataString 44 - (void)fillForm:(NSString*)dataString
46 forceFillFieldName:(NSString*)forceFillFieldName 45 forceFillFieldName:(NSString*)forceFillFieldName
47 completionHandler:(ProceduralBlock)completionHandler { 46 completionHandler:(ProceduralBlock)completionHandler {
48 DCHECK(completionHandler); 47 DCHECK(completionHandler);
49 std::string fieldName = 48 std::string fieldName =
50 forceFillFieldName 49 forceFillFieldName
51 ? base::GetQuotedJSONString([forceFillFieldName UTF8String]) 50 ? base::GetQuotedJSONString([forceFillFieldName UTF8String])
52 : "null"; 51 : "null";
53 NSString* fillFormJS = 52 NSString* fillFormJS =
54 [NSString stringWithFormat:@"__gCrWeb.autofill.fillForm(%@, %s);", 53 [NSString stringWithFormat:@"__gCrWeb.autofill.fillForm(%@, %s);",
55 dataString, fieldName.c_str()]; 54 dataString, fieldName.c_str()];
56 id stringResultHandler = ^(NSString*, NSError*) { 55 [self executeJavaScript:fillFormJS completionHandler:^(id, NSError*) {
57 completionHandler(); 56 completionHandler();
58 }; 57 }];
59 return [self evaluate:fillFormJS stringResultHandler:stringResultHandler];
60 } 58 }
61 59
62 - (void)clearAutofilledFieldsForFormNamed:(NSString*)formName 60 - (void)clearAutofilledFieldsForFormNamed:(NSString*)formName
63 completionHandler:(ProceduralBlock)completionHandler { 61 completionHandler:(ProceduralBlock)completionHandler {
64 DCHECK(completionHandler); 62 DCHECK(completionHandler);
65 web::JavaScriptCompletion resultHandler = ^void(NSString*, NSError*) { 63 NSString* script =
66 completionHandler();
67 };
68
69 NSString* js =
70 [NSString stringWithFormat: 64 [NSString stringWithFormat:
71 @"__gCrWeb.autofill.clearAutofilledFields(%s);", 65 @"__gCrWeb.autofill.clearAutofilledFields(%s);",
72 base::GetQuotedJSONString([formName UTF8String]).c_str()]; 66 base::GetQuotedJSONString([formName UTF8String]).c_str()];
73 [self evaluate:js stringResultHandler:resultHandler]; 67 [self executeJavaScript:script completionHandler:^(id, NSError*) {
68 completionHandler();
69 }];
74 } 70 }
75 71
76 - (void)fillPredictionData:(NSString*)dataString { 72 - (void)fillPredictionData:(NSString*)dataString {
77 NSString* script = 73 NSString* script =
78 [NSString stringWithFormat:@"__gCrWeb.autofill.fillPredictionData(%@);", 74 [NSString stringWithFormat:@"__gCrWeb.autofill.fillPredictionData(%@);",
79 dataString]; 75 dataString];
80 [self executeJavaScript:script completionHandler:nil]; 76 [self executeJavaScript:script completionHandler:nil];
81 } 77 }
82 78
83 @end 79 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698