Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / autofill / ios / browser / js_autofill_manager.mm
blob84d5a7235d8f927dae737e8e644cf5cd1f6be14d
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
14                                completionHandler:
15                                    (void (^)(NSString*))completionHandler {
16   DCHECK(completionHandler);
17   NSString* extractFormsJS = [NSString
18       stringWithFormat:@"__gCrWeb.autofill.extractForms(%" PRIuNS ");",
19                        requiredFieldsCount];
20   [self evaluate:extractFormsJS
21       stringResultHandler:^(NSString* result, NSError*) {
22         completionHandler(result);
23       }];
26 #pragma mark -
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*) {
50     completionHandler();
51   };
53   NSString* js =
54       [NSString stringWithFormat:@"__gCrWeb.autofill.fillActiveFormField(%@);",
55                                  dataString];
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 =
64       forceFillFieldName
65           ? base::GetQuotedJSONString([forceFillFieldName UTF8String])
66           : "null";
67   NSString* fillFormJS =
68       [NSString stringWithFormat:@"__gCrWeb.autofill.fillForm(%@, %s);",
69                                  dataString, fieldName.c_str()];
70   id stringResultHandler = ^(NSString*, NSError*) {
71     completionHandler();
72   };
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*) {
80     completionHandler();
81   };
83   NSString* js =
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:
92             [NSString
93                 stringWithFormat:@"__gCrWeb.autofill.fillPredictionData(%@);",
94                                  dataString]];
97 @end