File: js_autofill_manager.mm

package info (click to toggle)
chromium 73.0.3683.75-1~deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,792,156 kB
  • sloc: cpp: 13,473,466; ansic: 1,577,080; python: 898,539; javascript: 655,737; xml: 341,883; asm: 306,070; java: 289,969; perl: 80,911; objc: 67,198; sh: 43,184; cs: 27,853; makefile: 12,092; php: 11,064; yacc: 10,373; tcl: 8,875; ruby: 3,941; lex: 1,800; pascal: 1,473; lisp: 812; awk: 41; jsp: 39; sed: 19; sql: 3
file content (154 lines) | stat: -rw-r--r-- 6,337 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "components/autofill/ios/browser/js_autofill_manager.h"

#include <vector>

#include "base/bind.h"
#include "base/callback.h"
#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/format_macros.h"
#include "base/json/json_writer.h"
#include "base/json/string_escape.h"
#include "base/logging.h"
#include "base/mac/foundation_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/sys_string_conversions.h"
#include "components/autofill/core/common/autofill_features.h"
#include "components/autofill/ios/browser/autofill_switches.h"
#import "components/autofill/ios/browser/autofill_util.h"
#include "ios/web/public/web_state/web_frame.h"

#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif

@implementation JsAutofillManager {
  // The injection receiver used to evaluate JavaScript.
  CRWJSInjectionReceiver* _receiver;
}

- (instancetype)initWithReceiver:(CRWJSInjectionReceiver*)receiver {
  DCHECK(receiver);
  self = [super init];
  if (self) {
    _receiver = receiver;
  }
  return self;
}

- (void)addJSDelayInFrame:(web::WebFrame*)frame {
  const base::CommandLine* command_line =
      base::CommandLine::ForCurrentProcess();
  if (command_line->HasSwitch(
          autofill::switches::kAutofillIOSDelayBetweenFields)) {
    std::string delayString = command_line->GetSwitchValueASCII(
        autofill::switches::kAutofillIOSDelayBetweenFields);
    int commandLineDelay = 0;
    if (base::StringToInt(delayString, &commandLineDelay)) {
      std::vector<base::Value> parameters;
      parameters.push_back(base::Value(commandLineDelay));
      autofill::ExecuteJavaScriptFunction(
          "autofill.setDelay", parameters, frame, _receiver,
          base::OnceCallback<void(NSString*)>());
    }
  }
}

- (void)fetchFormsWithMinimumRequiredFieldsCount:(NSUInteger)requiredFieldsCount
                                         inFrame:(web::WebFrame*)frame
                               completionHandler:
                                   (void (^)(NSString*))completionHandler {
  DCHECK(completionHandler);

  bool restrictUnownedFieldsToFormlessCheckout = base::FeatureList::IsEnabled(
      autofill::features::kAutofillRestrictUnownedFieldsToFormlessCheckout);
  std::vector<base::Value> parameters;
  parameters.push_back(base::Value(static_cast<int>(requiredFieldsCount)));
  parameters.push_back(base::Value(restrictUnownedFieldsToFormlessCheckout));
  autofill::ExecuteJavaScriptFunction("autofill.extractForms", parameters,
                                      frame, _receiver,
                                      base::BindOnce(completionHandler));
}

#pragma mark -
#pragma mark ProtectedMethods

- (void)fillActiveFormField:(std::unique_ptr<base::Value>)data
                    inFrame:(web::WebFrame*)frame
          completionHandler:(ProceduralBlock)completionHandler {
  DCHECK(data);
  std::vector<base::Value> parameters;
  parameters.push_back(std::move(*data));
  autofill::ExecuteJavaScriptFunction("autofill.fillActiveFormField",
                                      parameters, frame, _receiver,
                                      base::BindOnce(^(NSString*) {
                                        completionHandler();
                                      }));
}

- (void)toggleTrackingFormMutations:(BOOL)state inFrame:(web::WebFrame*)frame {
  std::vector<base::Value> parameters;
  parameters.push_back(base::Value(state ? 200 : 0));
  autofill::ExecuteJavaScriptFunction("formHandlers.trackFormMutations",
                                      parameters, frame, _receiver,
                                      base::OnceCallback<void(NSString*)>());
}

- (void)toggleTrackingUserEditedFields:(BOOL)state
                               inFrame:(web::WebFrame*)frame {
  std::vector<base::Value> parameters;
  parameters.push_back(base::Value(static_cast<bool>(state)));
  autofill::ExecuteJavaScriptFunction(
      "formHandlers.toggleTrackingUserEditedFields", parameters, frame,
      _receiver, base::OnceCallback<void(NSString*)>());
}

- (void)fillForm:(std::unique_ptr<base::Value>)data
    forceFillFieldIdentifier:(NSString*)forceFillFieldIdentifier
                     inFrame:(web::WebFrame*)frame
           completionHandler:(ProceduralBlock)completionHandler {
  DCHECK(data);
  DCHECK(completionHandler);
  std::string fieldIdentifier =
      forceFillFieldIdentifier
          ? base::SysNSStringToUTF8(forceFillFieldIdentifier)
          : "null";
  std::vector<base::Value> parameters;
  parameters.push_back(std::move(*data));
  parameters.push_back(base::Value(fieldIdentifier));
  autofill::ExecuteJavaScriptFunction("autofill.fillForm", parameters, frame,
                                      _receiver, base::BindOnce(^(NSString*) {
                                        completionHandler();
                                      }));
}

- (void)clearAutofilledFieldsForFormName:(NSString*)formName
                         fieldIdentifier:(NSString*)fieldIdentifier
                                 inFrame:(web::WebFrame*)frame
                       completionHandler:(ProceduralBlock)completionHandler {
  DCHECK(completionHandler);
  std::vector<base::Value> parameters;
  parameters.push_back(base::Value(base::SysNSStringToUTF8(formName)));
  parameters.push_back(base::Value(base::SysNSStringToUTF8(fieldIdentifier)));
  autofill::ExecuteJavaScriptFunction("autofill.clearAutofilledFields",
                                      parameters, frame, _receiver,
                                      base::BindOnce(^(NSString*) {
                                        completionHandler();
                                      }));
}

- (void)fillPredictionData:(std::unique_ptr<base::Value>)data
                   inFrame:(web::WebFrame*)frame {
  DCHECK(data);
  std::vector<base::Value> parameters;
  parameters.push_back(std::move(*data));
  autofill::ExecuteJavaScriptFunction("autofill.fillPredictionData", parameters,
                                      frame, _receiver,
                                      base::OnceCallback<void(NSString*)>());
}

@end