File: scoped_autofill_managers_observation.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 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 (100 lines) | stat: -rw-r--r-- 4,193 bytes parent folder | download | duplicates (6)
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
// Copyright 2023 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_CONTENT_BROWSER_SCOPED_AUTOFILL_MANAGERS_OBSERVATION_H_
#define COMPONENTS_AUTOFILL_CONTENT_BROWSER_SCOPED_AUTOFILL_MANAGERS_OBSERVATION_H_

#include "base/scoped_multi_source_observation.h"
#include "base/scoped_observation.h"
#include "components/autofill/content/browser/content_autofill_driver_factory.h"
#include "components/autofill/core/browser/foundations/autofill_manager.h"

namespace content {
class WebContents;
}

namespace autofill {

class ContentAutofillDriver;

// `ScopedAutofillManagersObservation` is a helper to reduce boilerplate when
// observing all `AutofillManagers` of a `WebContents`. Instead of having to
// implement both `ContentAutofillDriverFactory`'s and `AutofillManager`'s
// observer interfaces, the consumer now only needs to implement
// `AutofillManager::Observer` and add a member variable of this type.
//
// Example:
// class AutofillInformationConsumer : public AutofillManager::Observer {
//  public:
//   AutofillInformationConsumer(content::WebContents* contents) {
//     autofill_managers_observation.Observe(contents);
//   }
//
//   // AutofillManager::Observer:
//   void OnAfterLanguageDetermined(AutofillManager& manager) {
//     // Do something.
//   }
//
//  private:
//   ScopedAutofillManagersObservation autofill_managers_observation_{this};
// };
class ScopedAutofillManagersObservation final
    : public ContentAutofillDriverFactory::Observer {
 public:
  explicit ScopedAutofillManagersObservation(AutofillManager::Observer*);
  ~ScopedAutofillManagersObservation() override;

  // Starts observing `factory` or the `contents`' factory and the managers of
  // all of its owned drivers.
  // The `initialization_policy` defines how to handle `AutofillManager`s that
  // already exist at the time the observation starts.
  // - If the policy is `kExpectNoPreexistingManagers`, then `this` CHECKS that
  //   no managers exist at the start of the observation.
  // - If the policy is `kObservePreexistingManagers`, then `this` also starts
  //   observing those managers.
  //   WARNING: In this case, the usual contract for `AutofillManager::Observer`
  //   may no longer hold. Not every `OnAfterX` event will be preceded by an
  //   `OnBeforeX` event, since the observation may start after `OnBeforeX` has
  //   been emitted, but before `OnAfterX` is triggered.
  enum class InitializationPolicy {
    kExpectNoPreexistingManagers,
    kObservePreexistingManagers,
  };
  void Observe(ContentAutofillDriverFactory* factory,
               InitializationPolicy initialization_policy =
                   InitializationPolicy::kExpectNoPreexistingManagers);
  void Observe(content::WebContents* contents,
               InitializationPolicy initialization_policy =
                   InitializationPolicy::kExpectNoPreexistingManagers);

  // Resets all observations.
  void Reset();

  // Returns the `WebContents` whose factory it is observing.
  content::WebContents* web_contents();

 private:
  // ContentAutofillDriverFactory::Observer:
  void OnContentAutofillDriverFactoryDestroyed(
      ContentAutofillDriverFactory& factory) override;
  void OnContentAutofillDriverCreated(ContentAutofillDriverFactory& factory,
                                      ContentAutofillDriver& driver) override;
  void OnContentAutofillDriverStateChanged(
      ContentAutofillDriverFactory& factory,
      ContentAutofillDriver& driver,
      AutofillDriver::LifecycleState old_state,
      AutofillDriver::LifecycleState new_state) override;

  // The observation used to track driver creation and destruction.
  base::ScopedObservation<ContentAutofillDriverFactory,
                          ContentAutofillDriverFactory::Observer>
      factory_observation_{this};
  // The observation used to forward events to `observer_`.
  base::ScopedMultiSourceObservation<AutofillManager, AutofillManager::Observer>
      autofill_manager_observations_;
};

}  // namespace autofill

#endif  // COMPONENTS_AUTOFILL_CONTENT_BROWSER_SCOPED_AUTOFILL_MANAGERS_OBSERVATION_H_