File: form_fetch_batcher.h

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 (76 lines) | stat: -rw-r--r-- 3,048 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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_AUTOFILL_IOS_BROWSER_FORM_FETCH_BATCHER_H_
#define COMPONENTS_AUTOFILL_IOS_BROWSER_FORM_FETCH_BATCHER_H_

#import <optional>
#import <vector>

#import "base/memory/weak_ptr.h"
#import "base/time/time.h"
#import "components/autofill/core/common/form_data.h"
#import "components/autofill/ios/browser/autofill_driver_ios_bridge.h"
#import "ios/web/public/js_messaging/web_frame.h"

namespace autofill {

// Controller for recurrently doing batches of form extraction requests on a
// given frame. Can be safely deleted while there is an ongoing batch where the
// scheduled batch will simply be no op (the equivalent of canceling a task).
// Can only do unfiltered extractions where it gives back all forms that could
// be extracted.
class FormFetchBatcher {
 public:
  FormFetchBatcher(const FormFetchBatcher&) = delete;
  FormFetchBatcher(FormFetchBatcher&&) = delete;

  FormFetchBatcher(id<AutofillDriverIOSBridge> bridge,
                   base::WeakPtr<web::WebFrame> frame,
                   base::TimeDelta batch_period);
  ~FormFetchBatcher();

  // Pushes a request into the current batch. Automatically schedules a new
  // batch if there isn't already one. Consumes the request. Applies
  // `form_name_filter` filtering which will filter forms by name when
  // specified.
  void PushRequest(
      FormFetchCompletion&& fetch_request,
      std::optional<std::u16string> form_name_filter = std::nullopt);

  // Pushes a request into the current batch, runs immediately the current
  // batch, and cancels the scheduled task if there is one. Otherwise, works the
  // same way as PushRequest().
  void PushRequestAndRun(
      FormFetchCompletion&& fetch_request,
      std::optional<std::u16string> form_name_filter = std::nullopt);

 private:
  // Runs an extraction of forms in the frame's document to complete the current
  // batch of requests. The batch isn't completed yet when calling this as there
  // is still the form extraction to be done.
  void Run();

  // Completes the current batch of pending form extraction requests after
  // receiving the results from the scheduled form extraction. Clears the
  // pending requests and makes the batcher available for a new batch.
  void Complete(std::optional<std::vector<autofill::FormData>> forms);

  // Interface for fetching forms.
  id<AutofillDriverIOSBridge> bridge_;
  // Web frame on which the batcher extract forms.
  base::WeakPtr<web::WebFrame> frame_;
  // Queued fetch requests for the next scheduled batch.
  std::vector<FormFetchCompletion> fetch_requests_;
  // Period of time between the batches.
  base::TimeDelta batch_period_;
  // True if a batch was scheduled, so no further scheduling is required.
  bool batch_scheduled_ = false;

  base::WeakPtrFactory<FormFetchBatcher> weak_factory_{this};
};

}  // namespace autofill

#endif  // COMPONENTS_AUTOFILL_IOS_BROWSER_FORM_FETCH_BATCHER_H_