File: password_manager_java_script_feature.mm

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (179 lines) | stat: -rw-r--r-- 7,399 bytes parent folder | download | duplicates (5)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "components/password_manager/ios/password_manager_java_script_feature.h"

#import "base/no_destructor.h"
#import "base/strings/sys_string_conversions.h"
#import "base/values.h"
#import "components/autofill/core/common/password_form_fill_data.h"
#import "components/autofill/ios/browser/autofill_util.h"
#import "components/autofill/ios/common/javascript_feature_util.h"
#import "components/autofill/ios/form_util/autofill_renderer_id_java_script_feature.h"
#import "components/autofill/ios/form_util/form_util_java_script_feature.h"
#import "components/password_manager/ios/account_select_fill_data.h"
#import "components/password_manager/ios/password_manager_tab_helper.h"
#import "ios/web/public/js_messaging/java_script_feature_util.h"

using autofill::CreateBoolCallback;
using autofill::CreateStringCallback;

namespace password_manager {

namespace {
constexpr char kScriptName[] = "password_controller";
constexpr char FormSubmittedHandlerName[] = "PasswordFormSubmitButtonClick";

// The timeout for any JavaScript call in this file.
constexpr int64_t kJavaScriptExecutionTimeoutInSeconds = 5;

// Converts FormRendererId to int value that can be used in Javascript methods.
int FormRendererIdToJsParameter(autofill::FormRendererId form_id) {
  return form_id.value();
}

// Converts FieldRendererId to int value that can be used in Javascript methods.
int FieldRendererIdToJsParameter(autofill::FieldRendererId field_id) {
  return field_id.value();
}

base::Value::Dict SerializeFillData(const GURL& origin,
                                    autofill::FormRendererId form_renderer_id,
                                    autofill::FieldRendererId username_element,
                                    const std::u16string& username_value,
                                    autofill::FieldRendererId password_element,
                                    const std::u16string& password_value) {
  base::Value::Dict root_dict;
  root_dict.Set("origin", origin.spec());
  root_dict.Set("renderer_id", FormRendererIdToJsParameter(form_renderer_id));

  base::Value::List fieldList;

  base::Value::Dict usernameField;
  usernameField.Set("renderer_id",
                    FieldRendererIdToJsParameter(username_element));
  usernameField.Set("value", username_value);
  fieldList.Append(std::move(usernameField));

  base::Value::Dict passwordField;
  passwordField.Set("renderer_id", static_cast<int>(password_element.value()));
  passwordField.Set("value", password_value);
  fieldList.Append(std::move(passwordField));

  root_dict.Set("fields", std::move(fieldList));

  return root_dict;
}

// Serializes |fill_data| so it can be used by the JS side of
// PasswordController. Includes both username and password data if
// |fill_username|, and only password data otherwise.
base::Value::Dict SerializeFillData(const password_manager::FillData& fill_data,
                                    BOOL fill_username) {
  return SerializeFillData(fill_data.origin, fill_data.form_id,
                           fill_username ? fill_data.username_element_id
                                         : autofill::FieldRendererId(),
                           fill_data.username_value,
                           fill_data.password_element_id,
                           fill_data.password_value);
}

}  // namespace

// static
PasswordManagerJavaScriptFeature*
PasswordManagerJavaScriptFeature::GetInstance() {
  static base::NoDestructor<PasswordManagerJavaScriptFeature> instance;
  return instance.get();
}

PasswordManagerJavaScriptFeature::PasswordManagerJavaScriptFeature()
    : web::JavaScriptFeature(
          ContentWorldForAutofillJavascriptFeatures(),
          {FeatureScript::CreateWithFilename(
              kScriptName,
              FeatureScript::InjectionTime::kDocumentStart,
              FeatureScript::TargetFrames::kAllFrames,
              FeatureScript::ReinjectionBehavior::kInjectOncePerWindow)},
          {
              web::java_script_features::GetCommonJavaScriptFeature(),
              web::java_script_features::GetMessageJavaScriptFeature(),
              autofill::FormUtilJavaScriptFeature::GetInstance(),
              autofill::AutofillRendererIDJavaScriptFeature::GetInstance(),
          }) {}

PasswordManagerJavaScriptFeature::~PasswordManagerJavaScriptFeature() = default;

void PasswordManagerJavaScriptFeature::FindPasswordFormsInFrame(
    web::WebFrame* frame,
    base::OnceCallback<void(NSString*)> callback) {
  DCHECK(!callback.is_null());
  CallJavaScriptFunction(frame, "passwords.findPasswordForms", {},
                         CreateStringCallback(std::move(callback)),
                         base::Seconds(kJavaScriptExecutionTimeoutInSeconds));
}

void PasswordManagerJavaScriptFeature::ExtractForm(
    web::WebFrame* frame,
    autofill::FormRendererId form_identifier,
    base::OnceCallback<void(NSString*)> callback) {
  DCHECK(!callback.is_null());
  CallJavaScriptFunction(
      frame, "passwords.getPasswordFormDataAsString",
      base::Value::List().Append(FormRendererIdToJsParameter(form_identifier)),
      CreateStringCallback(std::move(callback)),
      base::Seconds(kJavaScriptExecutionTimeoutInSeconds));
}

void PasswordManagerJavaScriptFeature::FillPasswordForm(
    web::WebFrame* frame,
    const password_manager::FillData& fill_data,
    BOOL fill_username,
    const std::string& username,
    const std::string& password,
    base::OnceCallback<void(const base::Value*)> callback) {
  DCHECK(!callback.is_null());

  base::Value::Dict form_value = SerializeFillData(fill_data, fill_username);
  CallJavaScriptFunction(frame, "passwords.fillPasswordForm",
                         base::Value::List()
                             .Append(std::move(form_value))
                             .Append(username)
                             .Append(password),
                         std::move(callback),
                         base::Seconds(kJavaScriptExecutionTimeoutInSeconds));
}

std::optional<std::string>
PasswordManagerJavaScriptFeature::GetScriptMessageHandlerName() const {
  return FormSubmittedHandlerName;
}

void PasswordManagerJavaScriptFeature::ScriptMessageReceived(
    web::WebState* web_state,
    const web::ScriptMessage& message) {
  PasswordManagerTabHelper::GetOrCreateForWebState(web_state)
      ->ScriptMessageReceived(message);
}

void PasswordManagerJavaScriptFeature::FillPasswordForm(
    web::WebFrame* frame,
    autofill::FormRendererId form_identifier,
    autofill::FieldRendererId new_password_identifier,
    autofill::FieldRendererId confirm_password_identifier,
    NSString* generated_password,
    base::OnceCallback<void(BOOL)> callback) {
  DCHECK(!callback.is_null());
  CallJavaScriptFunction(
      frame, "passwords.fillPasswordFormWithGeneratedPassword",
      base::Value::List()
          .Append(FormRendererIdToJsParameter(form_identifier))
          .Append(FieldRendererIdToJsParameter(new_password_identifier))
          .Append(FieldRendererIdToJsParameter(confirm_password_identifier))
          .Append(base::SysNSStringToUTF8(generated_password)),
      CreateBoolCallback(std::move(callback)),
      base::Seconds(kJavaScriptExecutionTimeoutInSeconds));
}

}  // namespace password_manager