File: form_activity_tab_helper.mm

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (424 lines) | stat: -rw-r--r-- 15,419 bytes parent folder | download | duplicates (4)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// Copyright 2018 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/autofill/ios/form_util/form_activity_tab_helper.h"

#import <Foundation/Foundation.h>

#import <optional>
#import <variant>

#import "base/debug/crash_logging.h"
#import "base/debug/dump_without_crashing.h"
#import "base/feature_list.h"
#import "base/functional/bind.h"
#import "base/logging.h"
#import "base/metrics/histogram_functions.h"
#import "base/strings/sys_string_conversions.h"
#import "base/strings/utf_string_conversions.h"
#import "base/values.h"
#import "components/autofill/core/common/field_data_manager.h"
#import "components/autofill/core/common/form_data.h"
#import "components/autofill/core/common/unique_ids.h"
#import "components/autofill/ios/browser/autofill_util.h"
#import "components/autofill/ios/common/features.h"
#import "components/autofill/ios/common/field_data_manager_factory_ios.h"
#import "components/autofill/ios/form_util/child_frame_registrar.h"
#import "components/autofill/ios/form_util/form_activity_observer.h"
#import "components/autofill/ios/form_util/form_activity_params.h"
#import "components/autofill/ios/form_util/form_util_java_script_feature.h"
#import "ios/web/public/js_messaging/content_world.h"
#import "ios/web/public/js_messaging/script_message.h"
#import "ios/web/public/js_messaging/web_frame.h"
#import "ios/web/public/js_messaging/web_frames_manager.h"
#import "ios/web/public/ui/crw_web_view_proxy.h"

using autofill::FieldDataManager;
using autofill::FieldDataManagerFactoryIOS;
using autofill::FormData;
using autofill::LocalFrameToken;
using base::SysUTF8ToNSString;
using web::ContentWorld;
using web::WebFrame;

namespace {

// Enumeration describing the possible outcomes of handling a form submission
// message.
//
// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
//
// LINT.IfChange(FormSubmissionOutcomeIOS)
enum class FormSubmissionOutcome {
  // Form submission message was valid. Observers were notified about the form
  // submission event.
  kHandled = 0,
  // Form submission message did not have a body or wasn't a dict.
  kInvalidMessageBody = 1,
  // Form submission message did not have a frame ID.
  kNoFrameID = 2,
  // There is no existing WebFrame for the submitted form.
  kNoFrame = 3,
  // The submission message indicates it originated from the main
  // frame while the matching WebFrame is not or viceversa.
  kIsMainFrameDiscrepancy = 4,
  // Form submission message is missing the href field.
  kMissingHref = 5,
  // The payload was missing the `formData` field or it wasn't a base::Dict.
  kMissingFormData = 6,
  // The formData was invalid. Inspect
  // Autofill.iOS.FormSubmission.Outcome.InvalidFormReason to investigate the
  // cause.
  kFormExtractionFailure = 7,
  // There was an error while handling the form submission event in the
  // renderer.
  kRendererError = 8,
  // There was an error while handling the form submission event in the renderer
  // but the error couldn't be parsed.
  kUnparsedRendererError = 9,
  kMaxValue = kUnparsedRendererError,
};
// LINT.ThenChange(//tools/metrics/histograms/metadata/autofill/enums.xml:FormSubmissionOutcomeIOS)

void RecordMetrics(const base::Value::Dict& message_body) {
  const base::Value::Dict* metadata = message_body.FindDict("metadata");

  if (!metadata) {
    // Don't record metrics if no metadata because all the data for calculating
    // the metrics is there.
    return;
  }

  // Extract the essential data for metrics.
  std::optional<double> drop_count = metadata->FindDouble("dropCount");
  std::optional<double> batch_size = metadata->FindDouble("size");

  // Don't record metrics if there is missing data.
  if (!drop_count || !batch_size) {
    return;
  }

  // Record the number of dropped form activities.
  base::UmaHistogramCounts100("Autofill.iOS.FormActivity.DropCount",
                              *drop_count);

  // Record the number of messages sent in the batch.
  base::UmaHistogramCounts100("Autofill.iOS.FormActivity.SendCount",
                              *batch_size);

  // Record the ratio of sent messages on total eligible messages.
  if (const int denominator = *batch_size + *drop_count; denominator > 0) {
    const int percentage = (100 * (*batch_size)) / denominator;
    base::UmaHistogramPercentage("Autofill.iOS.FormActivity.SendRatio",
                                 percentage);
  }
}

// Logs the outcome of form submissions to metrics.
void RecordFormSubmissionOutcome(FormSubmissionOutcome outcome) {
  base::UmaHistogramEnumeration(autofill::kFormSubmissionOutcomeHistogram,
                                outcome);
}

// Logs form submission failures due to invalid form data.
void RecordFormExtractionFailure(autofill::ExtractFormDataFailure failure) {
  base::UmaHistogramEnumeration(autofill::kInvalidSubmittedFormReasonHistogram,
                                failure);
}

// Finds the frame with `frame_id` in the given content world.
web::WebFrame* GetFrameInContentWorld(std::string frame_id,
                                      ContentWorld content_world,
                                      web::WebState* web_state) {
  auto* frames_manager = web_state->GetWebFramesManager(content_world);
  return frames_manager->GetFrameWithId(frame_id);
}

// Finds the local frame token associated to
// `remote_frame_token` in autofill::ChildFrameRegistrar.
std::optional<LocalFrameToken> LookupLocalFrame(std::string remote_frame_token,
                                                web::WebState* web_state) {
  if (std::optional<base::UnguessableToken> remote =
          autofill::DeserializeJavaScriptFrameId(remote_frame_token)) {
    auto* registrar =
        autofill::ChildFrameRegistrar::GetOrCreateForWebState(web_state);
    return registrar->LookupChildFrame(autofill::RemoteFrameToken(*remote));
  }

  SCOPED_CRASH_KEY_STRING32(/*category=*/"FormSubmission",
                            /*name=*/"remote_frame_token",
                            /*value=*/remote_frame_token);
  base::debug::DumpWithoutCrashing();

  return std::nullopt;
}

// Finds the WebFrame in the isolated content world corresponding to a page
// content world WebFrame with id `page_world_frame_id`. The isolated world
// frame is retrieved using `remote_frame_token`, which should be associated to
// the frame's id in autofill::ChildFrameRegistrar.
//
// Returns std::nullopt if the
// isolated frame cannot be found or it has a different origin than its
// corresponding page world frame. Otherwise it returns the isolated world
// WebFrame along with its LocalFrameToken.
std::optional<std::pair<WebFrame*, LocalFrameToken>> GetIsolatedFrame(
    const std::string& page_world_frame_id,
    const std::string& remote_frame_token,
    web::WebState* web_state) {
  if (!base::FeatureList::IsEnabled(kAutofillIsolatedWorldForJavascriptIos)) {
    return std::nullopt;
  }

  std::optional<LocalFrameToken> local_frame_token =
      LookupLocalFrame(remote_frame_token, web_state);

  if (!local_frame_token) {
    return std::nullopt;
  }

  // Verify that the sender frame in the page world has the same origin as the
  // isolated one. Avoid cross origin talk.
  web::WebFrame* isolated_world_frame = GetFrameInContentWorld(
      local_frame_token->ToString(), ContentWorld::kIsolatedWorld, web_state);
  web::WebFrame* page_world_frame = GetFrameInContentWorld(
      page_world_frame_id, ContentWorld::kPageContentWorld, web_state);

  if (!isolated_world_frame || !page_world_frame) {
    return std::nullopt;
  }

  if (isolated_world_frame->GetSecurityOrigin() !=
      page_world_frame->GetSecurityOrigin()) {
    return std::nullopt;
  }

  return std::make_pair(isolated_world_frame, *local_frame_token);
}

}  // namespace

namespace autofill {

// static
FormActivityTabHelper* FormActivityTabHelper::GetOrCreateForWebState(
    web::WebState* web_state) {
  FormActivityTabHelper* helper = FromWebState(web_state);
  if (!helper) {
    CreateForWebState(web_state);
    helper = FromWebState(web_state);
    DCHECK(helper);
  }
  return helper;
}

FormActivityTabHelper::FormActivityTabHelper(web::WebState* web_state) {}
FormActivityTabHelper::~FormActivityTabHelper() = default;

void FormActivityTabHelper::AddObserver(FormActivityObserver* observer) {
  observers_.AddObserver(observer);
}

void FormActivityTabHelper::RemoveObserver(FormActivityObserver* observer) {
  observers_.RemoveObserver(observer);
}

void HandleSubmissionError(const base::Value::Dict& message) {
  const std::string* error_stack = message.FindString("errorStack");
  const std::string* error_message = message.FindString("errorMessage");
  std::optional<bool> is_programmatic =
      message.FindBool("programmaticSubmission");

  if (!error_stack || !error_message || !is_programmatic) {
    RecordFormSubmissionOutcome(FormSubmissionOutcome::kUnparsedRendererError);
    return;
  }

  SCOPED_CRASH_KEY_STRING256("FormSubmissionError", "msg", *error_message);
  SCOPED_CRASH_KEY_STRING1024("FormSubmissionError", "stack", *error_stack);

  base::debug::DumpWithoutCrashing();

  RecordFormSubmissionOutcome(FormSubmissionOutcome::kRendererError);
}

void FormActivityTabHelper::OnFormMessageReceived(
    web::WebState* web_state,
    const web::ScriptMessage& message) {
  if (!message.body() || !message.body()->is_dict()) {
    // Ignore invalid message.
    return;
  }

  const auto& message_body = message.body()->GetDict();

  RecordMetrics(message_body);

  const std::string* command = message_body.FindString("command");
  if (!command) {
    DLOG(WARNING) << "JS message parameter not found: command";
  } else if (*command == "form.submit") {
    FormSubmissionHandler(web_state, message);
  } else if (*command == "form.activity") {
    HandleFormActivity(web_state, message);
  } else if (*command == "form.removal") {
    HandleFormRemoval(web_state, message);
  } else if (*command == "form.submit.error") {
    HandleSubmissionError(message_body);
  }
}

void FormActivityTabHelper::HandleFormActivity(
    web::WebState* web_state,
    const web::ScriptMessage& message) {
  FormActivityParams params;
  if (!FormActivityParams::FromMessage(message, &params)) {
    return;
  }

  web::WebFramesManager* frames_manager =
      GetWebFramesManagerForAutofill(web_state);
  web::WebFrame* sender_frame = frames_manager->GetFrameWithId(params.frame_id);
  if (!sender_frame) {
    return;
  }

  for (auto& observer : observers_)
    observer.FormActivityRegistered(web_state, sender_frame, params);
}

void FormActivityTabHelper::HandleFormRemoval(
    web::WebState* web_state,
    const web::ScriptMessage& message) {
  FormRemovalParams params;
  if (!FormRemovalParams::FromMessage(message, &params)) {
    return;
  }

  web::WebFramesManager* frames_manager =
      GetWebFramesManagerForAutofill(web_state);
  web::WebFrame* sender_frame = frames_manager->GetFrameWithId(params.frame_id);
  if (!sender_frame) {
    return;
  }

  for (auto& observer : observers_)
    observer.FormRemoved(web_state, sender_frame, params);
}

void FormActivityTabHelper::FormSubmissionHandler(
    web::WebState* web_state,
    const web::ScriptMessage& message) {
  if (!message.body() || !message.body()->is_dict()) {
    // Ignore invalid message.
    RecordFormSubmissionOutcome(FormSubmissionOutcome::kInvalidMessageBody);
    return;
  }

  const base::Value::Dict& message_body = message.body()->GetDict();
  const std::string* frame_id = message_body.FindString("frameID");
  if (!frame_id) {
    RecordFormSubmissionOutcome(FormSubmissionOutcome::kNoFrameID);
    return;
  }

  web::WebFrame* sender_frame = nullptr;

  const std::string* remote_frame_token =
      message_body.FindString("remoteFrameToken");
  LocalFrameToken local_frame_token;

  // The submit message came from a page world frame. Autofill works on the
  // isolated world space so map the page frame to the corresponding isolated
  // world one.
  if (remote_frame_token) {
    if (std::optional<std::pair<WebFrame*, LocalFrameToken>>
            isolated_frame_with_token =
                GetIsolatedFrame(*frame_id, *remote_frame_token, web_state)) {
      sender_frame = isolated_frame_with_token->first;
      local_frame_token = isolated_frame_with_token->second;
    } else {
      RecordFormSubmissionOutcome(FormSubmissionOutcome::kNoFrame);
      return;
    }
  } else {
    // Handle isolated world messages without any special consideration.
    web::WebFramesManager* frames_manager =
        GetWebFramesManagerForAutofill(web_state);
    sender_frame = frames_manager->GetFrameWithId(*frame_id);
  }

  if (!sender_frame) {
    RecordFormSubmissionOutcome(FormSubmissionOutcome::kNoFrame);
    return;
  }

  if (sender_frame->IsMainFrame() != message.is_main_frame()) {
    RecordFormSubmissionOutcome(FormSubmissionOutcome::kIsMainFrameDiscrepancy);
    return;
  }

  if (!message_body.FindString("href")) {
    DLOG(WARNING) << "JS message parameter not found: href";
    RecordFormSubmissionOutcome(FormSubmissionOutcome::kMissingHref);
    return;
  }
  const std::string* maybe_form_name = message_body.FindString("formName");

  // We decide the form is user-submitted if the user has interacted with
  // the main page (using logic from the popup blocker), or if the keyboard
  // is visible.
  BOOL submitted_by_user = message.is_user_interacting() ||
                           web_state->GetWebViewProxy().keyboardVisible;

  std::string form_name;
  if (maybe_form_name) {
    form_name = *maybe_form_name;
  }

  FieldDataManager* fieldDataManager =
      FieldDataManagerFactoryIOS::FromWebFrame(sender_frame);

  const base::Value::Dict* form_data = message_body.FindDict("formData");
  if (!form_data) {
    RecordFormSubmissionOutcome(FormSubmissionOutcome::kMissingFormData);
    return;
  }

  // We need to pass `frame_id` when extracting the form even if the frame is
  // from the page world. `ExtractFormData` checks that the frame id matches
  // the id of the frame that contains the forms. For page world forms, we set
  // FormData::host_frame with the corresponding isolated world frame in
  // `local_frame_token`.
  std::variant<FormData, ExtractFormDataFailure> form_or_failure =
      autofill::ExtractFormDataOrFailure(
          *form_data, true, base::UTF8ToUTF16(form_name),
          web_state->GetLastCommittedURL(), sender_frame->GetSecurityOrigin(),
          *fieldDataManager, *frame_id, local_frame_token);

  if (std::holds_alternative<ExtractFormDataFailure>(form_or_failure)) {
    RecordFormSubmissionOutcome(FormSubmissionOutcome::kFormExtractionFailure);
    RecordFormExtractionFailure(
        std::get<ExtractFormDataFailure>(form_or_failure));
    return;
  }

  FormData form = std::get<FormData>(form_or_failure);

  if (std::optional<bool> programmatic_submission =
          message_body.FindBool("programmaticSubmission")) {
    base::UmaHistogramBoolean(kProgrammaticFormSubmissionHistogram,
                              *programmatic_submission);
  }

  for (auto& observer : observers_) {
    observer.DocumentSubmitted(web_state, sender_frame, form,
                               submitted_by_user);
  }

  RecordFormSubmissionOutcome(FormSubmissionOutcome::kHandled);
}

}  // namespace autofill