File: data_protection_navigation_observer.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 (149 lines) | stat: -rw-r--r-- 5,564 bytes parent folder | download | duplicates (2)
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
// 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 CHROME_BROWSER_ENTERPRISE_DATA_PROTECTION_DATA_PROTECTION_NAVIGATION_OBSERVER_H_
#define CHROME_BROWSER_ENTERPRISE_DATA_PROTECTION_DATA_PROTECTION_NAVIGATION_OBSERVER_H_

#include <optional>
#include <string>

#include "base/functional/callback_forward.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/enterprise/data_protection/data_protection_page_user_data.h"
#include "components/safe_browsing/core/common/proto/realtimeapi.pb.h"
#include "content/public/browser/navigation_handle_user_data.h"
#include "content/public/browser/web_contents_observer.h"

class Profile;

namespace content {

class NavigationHandle;
class WebContents;

}  // namespace content

namespace safe_browsing {
class RealTimeUrlLookupServiceBase;
}  // namespace safe_browsing

namespace enterprise_data_protection {

// Monitors a navigation in a WebContents to determine if data protection
// settings should be enabled or not.
class DataProtectionNavigationObserver
    : public content::NavigationHandleUserData<
          DataProtectionNavigationObserver>,
      public content::WebContentsObserver {
 public:
  // Callback that is meant to update data protection settings. For now,
  // it is only accepts a std::string parameter for the watermark string.
  // change this when adding new data protection settings.
  using Callback = base::OnceCallback<void(const UrlSettings&)>;

  // Log values for source of realtime URL lookup verdict. This is used to log
  // metrics as DataProtectionURLVerdictSource, so numeric values must not be
  // changed.
  enum class URLVerdictSource {
    // Verdict has been stored in the current Page's UserData.
    kPageUserData = 0,

    // Verdict has been stored by this class's lookup callback in the
    // `watermark_text_` member.
    kCachedLookupResult = 1,

    // Verdict was not cached, so we needed to perform a lookup in
    // DidFinishNavigation().
    kPostNavigationLookup = 2,

    // Verdict was stored in the WebContents' UserData.
    kWebContentsUserData = 3,

    kMaxValue = kWebContentsUserData
  };

  // Creates a DataProtectionNavigationObserver if needed.  For example, the
  // user data may not be needed for internal chrome URLs or if the required
  // enterprise policies are not set. If this is a same doc navigation, a
  // non-primary-main frame navigation, the data protection state should remain
  // unchanged.
  //
  // This function should be called in some DidStartNavigation() function
  // so that DataProtectionNavigationObserver can be created early enough to
  // monitor the whole navigation.
  //
  // The created DataProtectionNavigationObserver will delete itself when the
  // navigation completes.
  static void CreateForNavigationIfNeeded(
      Profile* profile,
      content::NavigationHandle* navigation_handle,
      Callback callback);

  // Checks the `web_contents` url for enabled data protection settings. Note
  // that `callback` is always invoked but may be called synchronously or
  // asynchronously depending on whether the state is cached in
  // RealTimeUrlLookupService or not.
  // This function is public to be called by tests and should no be called by
  // non-test code other that `DataProtectionNavigationObserver` and
  // `DataProtectionNavigationController`.
  static void ApplyDataProtectionSettings(Profile* profile,
                                          content::WebContents* web_contents,
                                          Callback callback);

  // public for testing
  DataProtectionNavigationObserver(
      content::NavigationHandle& navigation_handle,
      safe_browsing::RealTimeUrlLookupServiceBase* lookup_service,
      content::WebContents* web_contents,
      Callback callback);

  ~DataProtectionNavigationObserver() override;

  static void SetLookupServiceForTesting(
      safe_browsing::RealTimeUrlLookupServiceBase* lookup_service);

 private:
  friend class content::NavigationHandleUserData<
      DataProtectionNavigationObserver>;

  void OnLookupComplete(
      std::unique_ptr<safe_browsing::RTLookupResponse> rt_lookup_response);

  // Returns true when the "EnterpriseRealTimeUrlCheckMode" policy is enabled
  // for `browser_context`, and when a `lookup_service_` is available to make
  // URL filtering checks.
  bool ShouldPerformRealTimeUrlCheck(
      content::BrowserContext* browser_context) const;

  // content::WebContentsObserver:
  void DidRedirectNavigation(
      content::NavigationHandle* navigation_handle) override;
  void DidFinishNavigation(
      content::NavigationHandle* navigation_handle) override;

  bool is_from_cache_ = false;

  // Screenshots are allowed unless explicitly blocked.
  bool allow_screenshot_ = true;

  // The verdict indicating what watermark should be shown, if populated. Used
  // for reporting as well.
  std::unique_ptr<safe_browsing::RTLookupResponse> rt_lookup_response_;

  // Identifier string to show in the watermark if needed. This is either a user
  // email or a device ID.
  std::string identifier_;

  raw_ptr<safe_browsing::RealTimeUrlLookupServiceBase> lookup_service_ =
      nullptr;
  Callback pending_navigation_callback_;

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

  NAVIGATION_HANDLE_USER_DATA_KEY_DECL();
};

}  // namespace enterprise_data_protection

#endif  // CHROME_BROWSER_ENTERPRISE_DATA_PROTECTION_DATA_PROTECTION_NAVIGATION_OBSERVER_H_