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

#ifndef CHROME_BROWSER_LOOKALIKES_SAFETY_TIP_WEB_CONTENTS_OBSERVER_H_
#define CHROME_BROWSER_LOOKALIKES_SAFETY_TIP_WEB_CONTENTS_OBSERVER_H_

#include <optional>

#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "build/build_config.h"
#include "chrome/browser/lookalikes/lookalike_url_service.h"
#include "chrome/browser/lookalikes/safety_tip_ui.h"
#include "components/security_state/core/security_state.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/visibility.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_user_data.h"
#include "services/metrics/public/cpp/ukm_source_id.h"
#include "ui/views/widget/widget.h"
#include "url/gurl.h"
#include "url/origin.h"

#if BUILDFLAG(IS_ANDROID)
#include "chrome/browser/lookalikes/safety_tip_message_delegate_android.h"
#endif

class Profile;

// Observes navigations and triggers a Safety Tip warning if a visited site is
// determined to be a lookalike.
class SafetyTipWebContentsObserver
    : public content::WebContentsObserver,
      public content::WebContentsUserData<SafetyTipWebContentsObserver> {
 public:
  ~SafetyTipWebContentsObserver() override;

  // content::WebContentsObserver:
  void DidFinishNavigation(
      content::NavigationHandle* navigation_handle) override;
  void OnVisibilityChanged(content::Visibility visibility) override;

  // Returns the info about the Safety Tip (if any) that was assigned to the
  // currently visible navigation entry. This field will be set even if the UI
  // was not actually shown because the feature was disabled.
  security_state::SafetyTipInfo GetSafetyTipInfoForVisibleNavigation() const;

  // Allows tests to register a callback to be called when the next safety tip
  // check finishes.
  void RegisterSafetyTipCheckCallbackForTesting(base::OnceClosure callback);

  // Allows tests to register a callback called when the warning closes.
  void RegisterSafetyTipCloseCallbackForTesting(base::OnceClosure callback);

  // Allows tests to see whether a safety tip check has already completed since
  // construction or last reset, and selectively register a callback if not.
  bool safety_tip_check_pending_for_testing() {
    return safety_tip_check_pending_for_testing_;
  }

  void reset_safety_tip_check_pending_for_testing() {
    safety_tip_check_pending_for_testing_ = true;
  }

 private:
  friend class content::WebContentsUserData<SafetyTipWebContentsObserver>;

  explicit SafetyTipWebContentsObserver(content::WebContents* web_contents);

  // Possibly show a Safety Tip. Called on visibility changes and page load.
  void MaybeShowSafetyTip(ukm::SourceId navigation_source_id,
                          bool called_from_visibility_check,
                          bool record_ukm_if_tip_not_shown);

  // A SafetyTipCheckCallback. Called by the safety tip service when a
  // safety tip result is available.
  void HandleSafetyTipCheckResult(ukm::SourceId navigation_source_id,
                                  bool called_from_visibility_check,
                                  bool record_ukm_if_tip_not_shown,
                                  SafetyTipCheckResult result);

  // A helper method that calls and resets
  // |safety_tip_check_callback_for_testing_| if it is set. Only flips
  // |safety_tip_check_pending_for_testing_| if |heuristics_checked| is set.
  void MaybeCallSafetyTipCheckCallback(bool heuristics_checked);

  // A helper method to handle finalizing a safety tip check. This method
  // records UKM data about triggered heuristics if |record_ukm| is true, and
  // calls MaybeCallSafetyTipCheckCallback.
  void FinalizeSafetyTipCheckWhenTipNotShown(
      bool record_ukm,
      SafetyTipCheckResult result,
      ukm::SourceId navigation_source_id);

  raw_ptr<Profile> profile_;

  // Used to cache the last safety tip info (and associated navigation entry ID)
  // so that Page Info can fetch this information without performing a
  // safety tip check. Resets type to kNone and safe_url to empty on new top
  // frame navigations. Set even if the feature to show the UI is disabled.
  security_state::SafetyTipInfo last_navigation_safety_tip_info_;
  int last_safety_tip_navigation_entry_id_ = 0;

  // The initiator origin and URL of the most recently committed navigation.
  // Presently, these are used in metrics to differentiate same-origin
  // navigations (i.e. when the user stays on a flagged page).
  std::optional<url::Origin> last_committed_initiator_origin_;
  GURL last_committed_url_;

  base::OnceClosure safety_tip_check_callback_for_testing_;
  // Whether or not heuristics have yet been checked yet.
  bool safety_tip_check_pending_for_testing_ = true;

  base::OnceClosure safety_tip_close_callback_for_testing_;

#if BUILDFLAG(IS_ANDROID)
  SafetyTipMessageDelegateAndroid delegate_;
#endif

  base::WeakPtrFactory<SafetyTipWebContentsObserver> weak_factory_{this};
  WEB_CONTENTS_USER_DATA_KEY_DECL();
};

#endif  // CHROME_BROWSER_LOOKALIKES_SAFETY_TIP_WEB_CONTENTS_OBSERVER_H_