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.
5 #import "components/autofill/ios/browser/js_autofill_manager.h"
7 #include "base/format_macros.h"
8 #include "base/json/string_escape.h"
9 #include "base/logging.h"
11 @implementation JsAutofillManager
13 - (void)fetchFormsWithMinimumRequiredFieldsCount:(NSUInteger)requiredFieldsCount
15 (void (^)(NSString*))completionHandler {
16 DCHECK(completionHandler);
17 NSString* extractFormsJS = [NSString
18 stringWithFormat:@"__gCrWeb.autofill.extractForms(%" PRIuNS ");",
20 [self evaluate:extractFormsJS
21 stringResultHandler:^(NSString* result, NSError*) {
22 completionHandler(result);
27 #pragma mark ProtectedMethods
29 - (NSString*)scriptPath {
30 return @"autofill_controller";
33 - (NSString*)presenceBeacon {
34 return @"__gCrWeb.autofill";
37 - (void)storeActiveElement {
38 NSString* js = @"__gCrWeb.autofill.storeActiveElement()";
39 [self evaluate:js stringResultHandler:nil];
42 - (void)clearActiveElement {
43 NSString* js = @"__gCrWeb.autofill.clearActiveElement()";
44 [self evaluate:js stringResultHandler:nil];
47 - (void)fillActiveFormField:(NSString*)dataString
48 completionHandler:(ProceduralBlock)completionHandler {
49 web::JavaScriptCompletion resultHandler = ^void(NSString*, NSError*) {
54 [NSString stringWithFormat:@"__gCrWeb.autofill.fillActiveFormField(%@);",
56 [self evaluate:js stringResultHandler:resultHandler];
59 - (void)fillForm:(NSString*)dataString
60 forceFillFieldName:(NSString*)forceFillFieldName
61 completionHandler:(ProceduralBlock)completionHandler {
62 DCHECK(completionHandler);
63 std::string fieldName =
65 ? base::GetQuotedJSONString([forceFillFieldName UTF8String])
67 NSString* fillFormJS =
68 [NSString stringWithFormat:@"__gCrWeb.autofill.fillForm(%@, %s);",
69 dataString, fieldName.c_str()];
70 id stringResultHandler = ^(NSString*, NSError*) {
73 return [self evaluate:fillFormJS stringResultHandler:stringResultHandler];
76 - (void)clearAutofilledFieldsForFormNamed:(NSString*)formName
77 completionHandler:(ProceduralBlock)completionHandler {
78 DCHECK(completionHandler);
79 web::JavaScriptCompletion resultHandler = ^void(NSString*, NSError*) {
84 [NSString stringWithFormat:
85 @"__gCrWeb.autofill.clearAutofilledFields(%s);",
86 base::GetQuotedJSONString([formName UTF8String]).c_str()];
87 [self evaluate:js stringResultHandler:resultHandler];
90 - (void)fillPredictionData:(NSString*)dataString {
91 [self deferredEvaluate:
93 stringWithFormat:@"__gCrWeb.autofill.fillPredictionData(%@);",