File: intercept_navigation_delegate.h

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; 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 (137 lines) | stat: -rw-r--r-- 5,227 bytes parent folder | download | duplicates (3)
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
// Copyright 2012 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_NAVIGATION_INTERCEPTION_INTERCEPT_NAVIGATION_DELEGATE_H_
#define COMPONENTS_NAVIGATION_INTERCEPTION_INTERCEPT_NAVIGATION_DELEGATE_H_

#include <memory>

#include "base/android/jni_weak_ref.h"
#include "base/memory/weak_ptr.h"
#include "base/supports_user_data.h"
#include "components/navigation_interception/intercept_navigation_throttle.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/mojom/url_loader.mojom.h"
#include "services/network/public/mojom/url_loader_factory.mojom-forward.h"
#include "ui/base/page_transition_types.h"

namespace content {
class NavigationHandle;
class NavigationThrottle;
class WebContents;
}

namespace network {
struct ResourceRequest;

namespace mojom {
class URLLoader;
class URLLoaderClient;
}  // namespace mojom
}  // namespace network

namespace url {
class Origin;
}

class GURL;

namespace navigation_interception {

// Native side of the InterceptNavigationDelegate Java interface.
// This is used to create a InterceptNavigationResourceThrottle that calls the
// Java interface method to determine whether a navigation should be ignored or
// not.
// To us this class:
// 1) the Java-side interface implementation must be associated (via the
//    Associate method) with a WebContents for which URLRequests are to be
//    intercepted,
// 2) the NavigationThrottle obtained via MaybeCreateThrottleFor must be
//    associated with the NavigationHandle in the ContentBrowserClient
//    implementation.
class InterceptNavigationDelegate : public base::SupportsUserData::Data {
 public:
  // Pass true for |escape_external_handler_value| to have
  // base::EscapeExternalHandlerValue() invoked on URLs passed to
  // ShouldIgnoreNavigation() before the navigation is processed.
  InterceptNavigationDelegate(JNIEnv* env,
                              const jni_zero::JavaRef<jobject>& jdelegate,
                              bool escape_external_handler_value = false);

  InterceptNavigationDelegate(const InterceptNavigationDelegate&) = delete;
  InterceptNavigationDelegate& operator=(const InterceptNavigationDelegate&) =
      delete;

  ~InterceptNavigationDelegate() override;

  // Associates the InterceptNavigationDelegate with a WebContents using the
  // SupportsUserData mechanism.
  // As implied by the use of scoped_ptr, the WebContents will assume ownership
  // of |delegate|.
  static void Associate(content::WebContents* web_contents,
                        std::unique_ptr<InterceptNavigationDelegate> delegate);
  // Gets the InterceptNavigationDelegate associated with the WebContents,
  // can be null.
  static InterceptNavigationDelegate* Get(content::WebContents* web_contents);

  // Creates a InterceptNavigationThrottle that will direct all callbacks to
  // the InterceptNavigationDelegate.
  static void MaybeCreateAndAdd(content::NavigationThrottleRegistry& registry,
                                navigation_interception::SynchronyMode mode);

  void ShouldIgnoreNavigation(
      content::NavigationHandle* navigation_handle,
      bool should_run_async,
      InterceptNavigationThrottle::ResultCallback result_callback);

  void OnShouldIgnoreNavigationResult(bool should_ignore);

  // Requests that clients finish any pending ShouldIgnore checks synchronously.
  // If finishing the check synchronously is not possible, further
  // redirects/commits will be deferred.
  void RequestFinishPendingShouldIgnoreCheck();

  // See ContentBrowserClient::HandleExternalProtocol for the semantics around
  // |out_factory|.
  virtual void HandleSubframeExternalProtocol(
      const GURL& url,
      ui::PageTransition page_transition,
      bool has_user_gesture,
      const std::optional<url::Origin>& initiating_origin,
      mojo::PendingRemote<network::mojom::URLLoaderFactory>* out_factory);

  // To be called when a main frame requests a resource with a user gesture (eg.
  // xrh, fetch, etc.)
  void OnResourceRequestWithGesture();

  void OnSubframeAsyncActionTaken(
      JNIEnv* env,
      const base::android::JavaParamRef<jobject>& j_gurl);

 private:
  void LoaderCallback(
      const network::ResourceRequest& resource_request,
      mojo::PendingReceiver<network::mojom::URLLoader> pending_receiver,
      mojo::PendingRemote<network::mojom::URLLoaderClient> pending_client);

  void MaybeHandleSubframeAction();

  JavaObjectWeakGlobalRef weak_jdelegate_;
  bool escape_external_handler_value_ = false;

  mojo::SelfOwnedReceiverRef<network::mojom::URLLoader> url_loader_;
  // An empty URL if an async action is pending, or a URL to redirect to when
  // the URLLoader is ready.
  std::unique_ptr<GURL> subframe_redirect_url_;

  InterceptNavigationThrottle::ResultCallback should_ignore_result_callback_;

  base::WeakPtrFactory<InterceptNavigationDelegate> weak_ptr_factory_{this};
};

}  // namespace navigation_interception

#endif  // COMPONENTS_NAVIGATION_INTERCEPTION_INTERCEPT_NAVIGATION_DELEGATE_H_