Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 62885ab | 2013-01-23 03:55:16 | [diff] [blame] | 5 | #include "components/navigation_interception/intercept_navigation_delegate.h" |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 6 | |
Gyuyoung Kim | cb7965e | 2018-01-25 00:39:01 | [diff] [blame] | 7 | #include <memory> |
| 8 | |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 9 | #include "base/android/jni_android.h" |
| 10 | #include "base/android/jni_string.h" |
Avi Drissman | 12be031 | 2023-01-11 09:16:09 | [diff] [blame] | 11 | #include "base/functional/bind.h" |
| 12 | #include "base/functional/callback.h" |
Ryan Hamilton | 7f3bd3d | 2022-04-23 00:07:39 | [diff] [blame] | 13 | #include "base/strings/escape.h" |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 14 | #include "content/public/browser/browser_thread.h" |
David Bokan | 2a48f7bb | 2021-07-09 13:21:36 | [diff] [blame] | 15 | #include "content/public/browser/navigation_handle.h" |
clamy | 40c9e14 | 2015-09-29 11:18:47 | [diff] [blame] | 16 | #include "content/public/browser/navigation_throttle.h" |
jaekyun | 03890319 | 2015-03-31 14:15:59 | [diff] [blame] | 17 | #include "content/public/browser/render_frame_host.h" |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 18 | #include "content/public/browser/render_view_host.h" |
| 19 | #include "content/public/browser/web_contents.h" |
Michael Thiessen | 41821c98 | 2023-08-14 21:45:54 | [diff] [blame] | 20 | #include "content/public/common/page_visibility_state.h" |
Michael Thiessen | 1a49e4d5 | 2022-12-02 21:54:40 | [diff] [blame] | 21 | #include "mojo/public/cpp/bindings/self_owned_receiver.h" |
| 22 | #include "net/http/http_status_code.h" |
| 23 | #include "net/http/http_util.h" |
| 24 | #include "net/url_request/redirect_info.h" |
| 25 | #include "net/url_request/redirect_util.h" |
| 26 | #include "services/network/public/cpp/parsed_headers.h" |
| 27 | #include "services/network/public/cpp/resource_request.h" |
| 28 | #include "services/network/public/cpp/single_request_url_loader_factory.h" |
| 29 | #include "services/network/public/mojom/url_response_head.mojom.h" |
Michael Thiessen | ca245a38 | 2022-02-21 16:11:17 | [diff] [blame] | 30 | #include "url/android/gurl_android.h" |
[email protected] | e3b599e | 2013-07-05 07:15:17 | [diff] [blame] | 31 | #include "url/gurl.h" |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 32 | |
Andrew Grieve | ecb885bb | 2024-05-29 18:14:19 | [diff] [blame] | 33 | // Must come after all headers that specialize FromJniType() / ToJniType(). |
| 34 | #include "components/navigation_interception/jni_headers/InterceptNavigationDelegate_jni.h" |
| 35 | |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 36 | using base::android::ConvertUTF8ToJavaString; |
| 37 | using base::android::ScopedJavaLocalRef; |
| 38 | using content::BrowserThread; |
| 39 | using content::RenderViewHost; |
| 40 | using content::WebContents; |
Michael Thiessen | ca245a38 | 2022-02-21 16:11:17 | [diff] [blame] | 41 | using ui::PageTransition; |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 42 | |
[email protected] | 8812e3d0 | 2013-05-22 12:38:53 | [diff] [blame] | 43 | namespace navigation_interception { |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 44 | |
| 45 | namespace { |
| 46 | |
thestig | 3b6a2f1 | 2015-09-25 08:17:20 | [diff] [blame] | 47 | const void* const kInterceptNavigationDelegateUserDataKey = |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 48 | &kInterceptNavigationDelegateUserDataKey; |
| 49 | |
Michael Thiessen | 8c2b10e | 2025-01-23 17:54:19 | [diff] [blame] | 50 | void AllowNavigationToProceed( |
| 51 | content::NavigationHandle* navigation_handle, |
| 52 | bool should_run_async, |
| 53 | InterceptNavigationThrottle::ResultCallback result_callback) { |
| 54 | std::move(result_callback).Run(false); |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 55 | } |
| 56 | |
Michael Thiessen | 1a49e4d5 | 2022-12-02 21:54:40 | [diff] [blame] | 57 | class RedirectURLLoader : public network::mojom::URLLoader { |
| 58 | public: |
Michael Thiessen | 7b53117 | 2023-01-28 05:25:59 | [diff] [blame] | 59 | RedirectURLLoader(const network::ResourceRequest& resource_request, |
Michael Thiessen | 1a49e4d5 | 2022-12-02 21:54:40 | [diff] [blame] | 60 | mojo::PendingRemote<network::mojom::URLLoaderClient> client) |
Michael Thiessen | 7b53117 | 2023-01-28 05:25:59 | [diff] [blame] | 61 | : client_(std::move(client)), request_(resource_request) {} |
| 62 | |
| 63 | void DoRedirect(std::unique_ptr<GURL> url) { |
Michael Thiessen | 1a49e4d5 | 2022-12-02 21:54:40 | [diff] [blame] | 64 | net::HttpStatusCode response_code = net::HTTP_TEMPORARY_REDIRECT; |
| 65 | auto response_head = network::mojom::URLResponseHead::New(); |
| 66 | response_head->encoded_data_length = 0; |
| 67 | response_head->headers = base::MakeRefCounted<net::HttpResponseHeaders>( |
| 68 | net::HttpUtil::AssembleRawHeaders("HTTP/1.1 307 Temporary Redirect")); |
| 69 | |
| 70 | // Avoid a round-trip to the network service by pre-parsing headers. |
| 71 | // This doesn't violate: `docs/security/rule-of-2.md`, because the input is |
| 72 | // trusted, before appending the Location: <url> header. |
| 73 | response_head->parsed_headers = |
Michael Thiessen | 7b53117 | 2023-01-28 05:25:59 | [diff] [blame] | 74 | network::PopulateParsedHeaders(response_head->headers.get(), *url); |
Michael Thiessen | 1a49e4d5 | 2022-12-02 21:54:40 | [diff] [blame] | 75 | |
Michael Thiessen | 7b53117 | 2023-01-28 05:25:59 | [diff] [blame] | 76 | response_head->headers->AddHeader("Location", url->spec()); |
Michael Thiessen | 1a49e4d5 | 2022-12-02 21:54:40 | [diff] [blame] | 77 | |
| 78 | auto first_party_url_policy = |
Michael Thiessen | 7b53117 | 2023-01-28 05:25:59 | [diff] [blame] | 79 | request_.update_first_party_url_on_redirect |
Michael Thiessen | 1a49e4d5 | 2022-12-02 21:54:40 | [diff] [blame] | 80 | ? net::RedirectInfo::FirstPartyURLPolicy::UPDATE_URL_ON_REDIRECT |
| 81 | : net::RedirectInfo::FirstPartyURLPolicy::NEVER_CHANGE_URL; |
| 82 | |
| 83 | client_->OnReceiveRedirect( |
| 84 | net::RedirectInfo::ComputeRedirectInfo( |
Michael Thiessen | 7b53117 | 2023-01-28 05:25:59 | [diff] [blame] | 85 | request_.method, request_.url, request_.site_for_cookies, |
| 86 | first_party_url_policy, request_.referrer_policy, |
Arthur Sonzogni | c571efb | 2024-01-26 20:26:18 | [diff] [blame] | 87 | request_.referrer.spec(), response_code, *url, std::nullopt, |
Michael Thiessen | 1a49e4d5 | 2022-12-02 21:54:40 | [diff] [blame] | 88 | /*insecure_scheme_was_upgraded=*/false, |
| 89 | /*copy_fragment=*/false), |
| 90 | std::move(response_head)); |
| 91 | } |
| 92 | |
Michael Thiessen | 7b53117 | 2023-01-28 05:25:59 | [diff] [blame] | 93 | void OnNonRedirectAsyncAction() { |
| 94 | client_->OnComplete(network::URLLoaderCompletionStatus(net::ERR_ABORTED)); |
| 95 | } |
| 96 | |
Michael Thiessen | 1a49e4d5 | 2022-12-02 21:54:40 | [diff] [blame] | 97 | RedirectURLLoader(const RedirectURLLoader&) = delete; |
| 98 | RedirectURLLoader& operator=(const RedirectURLLoader&) = delete; |
| 99 | |
| 100 | ~RedirectURLLoader() override = default; |
| 101 | |
| 102 | private: |
| 103 | // network::mojom::URLLoader overrides: |
| 104 | void FollowRedirect( |
| 105 | const std::vector<std::string>& removed_headers, |
| 106 | const net::HttpRequestHeaders& modified_headers, |
| 107 | const net::HttpRequestHeaders& modified_cors_exempt_headers, |
Arthur Sonzogni | c571efb | 2024-01-26 20:26:18 | [diff] [blame] | 108 | const std::optional<GURL>& new_url) override { |
Peter Boström | 77d2135 | 2024-11-13 22:26:11 | [diff] [blame] | 109 | NOTREACHED(); |
Michael Thiessen | 1a49e4d5 | 2022-12-02 21:54:40 | [diff] [blame] | 110 | } |
| 111 | void SetPriority(net::RequestPriority priority, |
| 112 | int intra_priority_value) override {} |
Michael Thiessen | 1a49e4d5 | 2022-12-02 21:54:40 | [diff] [blame] | 113 | |
| 114 | mojo::Remote<network::mojom::URLLoaderClient> client_; |
Michael Thiessen | 7b53117 | 2023-01-28 05:25:59 | [diff] [blame] | 115 | network::ResourceRequest request_; |
Michael Thiessen | 1a49e4d5 | 2022-12-02 21:54:40 | [diff] [blame] | 116 | }; |
| 117 | |
[email protected] | a8e69a74 | 2013-10-15 10:58:55 | [diff] [blame] | 118 | } // namespace |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 119 | |
| 120 | // static |
| 121 | void InterceptNavigationDelegate::Associate( |
| 122 | WebContents* web_contents, |
dcheng | 84c358e | 2016-04-26 07:05:53 | [diff] [blame] | 123 | std::unique_ptr<InterceptNavigationDelegate> delegate) { |
Michael Thiessen | 8595d474 | 2025-03-11 17:06:55 | [diff] [blame^] | 124 | if (!delegate) { |
| 125 | web_contents->RemoveUserData(kInterceptNavigationDelegateUserDataKey); |
| 126 | } else { |
| 127 | web_contents->SetUserData(kInterceptNavigationDelegateUserDataKey, |
| 128 | std::move(delegate)); |
| 129 | } |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | // static |
| 133 | InterceptNavigationDelegate* InterceptNavigationDelegate::Get( |
| 134 | WebContents* web_contents) { |
dtrainor | 037df0d | 2014-10-08 18:05:24 | [diff] [blame] | 135 | return static_cast<InterceptNavigationDelegate*>( |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 136 | web_contents->GetUserData(kInterceptNavigationDelegateUserDataKey)); |
| 137 | } |
| 138 | |
| 139 | // static |
dcheng | 84c358e | 2016-04-26 07:05:53 | [diff] [blame] | 140 | std::unique_ptr<content::NavigationThrottle> |
David Bokan | 2a48f7bb | 2021-07-09 13:21:36 | [diff] [blame] | 141 | InterceptNavigationDelegate::MaybeCreateThrottleFor( |
Charlie Harrison | 3286ab7 | 2019-02-13 20:13:30 | [diff] [blame] | 142 | content::NavigationHandle* handle, |
| 143 | navigation_interception::SynchronyMode mode) { |
David Bokan | 2a48f7bb | 2021-07-09 13:21:36 | [diff] [blame] | 144 | // Navigations in a subframe or non-primary frame tree should not be |
| 145 | // intercepted. As examples of a non-primary frame tree, a navigation |
| 146 | // occurring in a Portal element or an unactivated prerendering page should |
| 147 | // not launch an app. |
| 148 | // TODO(bokan): This is a bit of a stopgap approach since we won't run |
| 149 | // throttles again when the prerender is activated which means links that are |
| 150 | // prerendered will avoid launching an app intent that a regular navigation |
| 151 | // would have. Longer term we'll want prerender activation to check for app |
| 152 | // intents, or have this throttle cancel the prerender if an intent would |
| 153 | // have been launched (without launching the intent). It's also not clear |
| 154 | // what the right behavior for <portal> elements is. |
| 155 | // https://crbug.com/1227659. |
| 156 | if (!handle->IsInPrimaryMainFrame()) |
| 157 | return nullptr; |
| 158 | |
Michael Thiessen | 8c2b10e | 2025-01-23 17:54:19 | [diff] [blame] | 159 | InterceptNavigationDelegate* intercept_navigation_delegate = |
| 160 | InterceptNavigationDelegate::Get(handle->GetWebContents()); |
| 161 | |
| 162 | if (!intercept_navigation_delegate) { |
| 163 | return std::make_unique<InterceptNavigationThrottle>( |
| 164 | handle, base::BindRepeating(&AllowNavigationToProceed), mode, |
| 165 | base::DoNothing()); |
| 166 | } |
| 167 | |
Gyuyoung Kim | cb7965e | 2018-01-25 00:39:01 | [diff] [blame] | 168 | return std::make_unique<InterceptNavigationThrottle>( |
Michael Thiessen | 8c2b10e | 2025-01-23 17:54:19 | [diff] [blame] | 169 | handle, |
| 170 | base::BindRepeating(&InterceptNavigationDelegate::ShouldIgnoreNavigation, |
| 171 | base::Unretained(intercept_navigation_delegate)), |
| 172 | mode, |
| 173 | base::BindRepeating( |
Michael Thiessen | 729a467 | 2025-01-29 20:38:35 | [diff] [blame] | 174 | &InterceptNavigationDelegate::RequestFinishPendingShouldIgnoreCheck, |
Michael Thiessen | 8c2b10e | 2025-01-23 17:54:19 | [diff] [blame] | 175 | base::Unretained(intercept_navigation_delegate))); |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | InterceptNavigationDelegate::InterceptNavigationDelegate( |
Colin Blundell | 4695e814 | 2020-03-16 11:13:12 | [diff] [blame] | 179 | JNIEnv* env, |
Andrew Grieve | df3420a4e | 2024-08-30 21:02:48 | [diff] [blame] | 180 | const jni_zero::JavaRef<jobject>& jdelegate, |
Colin Blundell | 4695e814 | 2020-03-16 11:13:12 | [diff] [blame] | 181 | bool escape_external_handler_value) |
| 182 | : weak_jdelegate_(env, jdelegate), |
| 183 | escape_external_handler_value_(escape_external_handler_value) {} |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 184 | |
Michael Thiessen | 1a49e4d5 | 2022-12-02 21:54:40 | [diff] [blame] | 185 | InterceptNavigationDelegate::~InterceptNavigationDelegate() = default; |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 186 | |
Michael Thiessen | 8c2b10e | 2025-01-23 17:54:19 | [diff] [blame] | 187 | void InterceptNavigationDelegate::ShouldIgnoreNavigation( |
| 188 | content::NavigationHandle* navigation_handle, |
| 189 | bool should_run_async, |
| 190 | InterceptNavigationThrottle::ResultCallback result_callback) { |
| 191 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 192 | // Avoid having two outstanding checks at once for simplicity. |
| 193 | if (should_ignore_result_callback_) { |
Michael Thiessen | ec04a4a | 2025-02-24 16:01:54 | [diff] [blame] | 194 | std::move(result_callback).Run(false); |
| 195 | return; |
Michael Thiessen | 8c2b10e | 2025-01-23 17:54:19 | [diff] [blame] | 196 | } |
Michael Thiessen | ca245a38 | 2022-02-21 16:11:17 | [diff] [blame] | 197 | GURL escaped_url = escape_external_handler_value_ |
Ryan Hamilton | 7f3bd3d | 2022-04-23 00:07:39 | [diff] [blame] | 198 | ? GURL(base::EscapeExternalHandlerValue( |
Michael Thiessen | ca245a38 | 2022-02-21 16:11:17 | [diff] [blame] | 199 | navigation_handle->GetURL().spec())) |
| 200 | : navigation_handle->GetURL(); |
Colin Blundell | 4695e814 | 2020-03-16 11:13:12 | [diff] [blame] | 201 | |
Michael Thiessen | 8c2b10e | 2025-01-23 17:54:19 | [diff] [blame] | 202 | if (!escaped_url.is_valid()) { |
| 203 | std::move(result_callback).Run(false); |
| 204 | return; |
| 205 | } |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 206 | |
| 207 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 208 | ScopedJavaLocalRef<jobject> jdelegate = weak_jdelegate_.get(env); |
| 209 | |
Michael Thiessen | 8c2b10e | 2025-01-23 17:54:19 | [diff] [blame] | 210 | if (jdelegate.is_null()) { |
| 211 | std::move(result_callback).Run(false); |
| 212 | return; |
| 213 | } |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 214 | |
Michael Thiessen | 41821c98 | 2023-08-14 21:45:54 | [diff] [blame] | 215 | bool hidden_cross_frame = false; |
Michael Thiessen | 884e617 | 2023-02-13 17:50:31 | [diff] [blame] | 216 | // Only main frame navigations use this path, so we only need to check if the |
| 217 | // navigation is cross-frame to the main frame. |
Michael Thiessen | 41821c98 | 2023-08-14 21:45:54 | [diff] [blame] | 218 | if (navigation_handle->GetInitiatorFrameToken() && |
| 219 | navigation_handle->GetInitiatorFrameToken() != |
| 220 | navigation_handle->GetWebContents() |
| 221 | ->GetPrimaryMainFrame() |
| 222 | ->GetFrameToken()) { |
| 223 | content::RenderFrameHost* initiator_frame_host = |
| 224 | content::RenderFrameHost::FromFrameToken( |
Dave Tapuska | e9b7c0f7 | 2023-11-06 16:38:01 | [diff] [blame] | 225 | content::GlobalRenderFrameHostToken( |
| 226 | navigation_handle->GetInitiatorProcessId(), |
| 227 | navigation_handle->GetInitiatorFrameToken().value())); |
Michael Thiessen | 41821c98 | 2023-08-14 21:45:54 | [diff] [blame] | 228 | // If the initiator is gone treat it as not visible. |
| 229 | hidden_cross_frame = |
| 230 | !initiator_frame_host || initiator_frame_host->GetVisibilityState() != |
| 231 | content::PageVisibilityState::kVisible; |
| 232 | } |
Michael Thiessen | 884e617 | 2023-02-13 17:50:31 | [diff] [blame] | 233 | |
Michael Thiessen | 8fd3520b | 2023-03-10 20:55:47 | [diff] [blame] | 234 | // We don't care which sandbox flags are present, only that any sandbox flags |
| 235 | // are present, as we don't support persisting sandbox flags through fallback |
| 236 | // URL navigation. |
| 237 | bool is_sandboxed = navigation_handle->SandboxFlagsInherited() != |
Michael Thiessen | d80a1af1 | 2023-08-21 17:17:35 | [diff] [blame] | 238 | network::mojom::WebSandboxFlags::kNone || |
| 239 | navigation_handle->SandboxFlagsInitiator() != |
| 240 | network::mojom::WebSandboxFlags::kNone; |
Arthur Sonzogni | dc8508ab | 2023-08-17 08:42:42 | [diff] [blame] | 241 | |
Michael Thiessen | 8c2b10e | 2025-01-23 17:54:19 | [diff] [blame] | 242 | should_ignore_result_callback_ = std::move(result_callback); |
| 243 | Java_InterceptNavigationDelegate_callShouldIgnoreNavigation( |
Michael Thiessen | ca245a38 | 2022-02-21 16:11:17 | [diff] [blame] | 244 | env, jdelegate, navigation_handle->GetJavaNavigationHandle(), |
Michael Thiessen | 41821c98 | 2023-08-14 21:45:54 | [diff] [blame] | 245 | url::GURLAndroid::FromNativeGURL(env, escaped_url), hidden_cross_frame, |
Michael Thiessen | 8c2b10e | 2025-01-23 17:54:19 | [diff] [blame] | 246 | is_sandboxed, should_run_async); |
| 247 | } |
| 248 | |
| 249 | void InterceptNavigationDelegate::OnShouldIgnoreNavigationResult( |
| 250 | bool should_ignore) { |
| 251 | std::move(should_ignore_result_callback_).Run(should_ignore); |
| 252 | } |
| 253 | |
Michael Thiessen | 729a467 | 2025-01-29 20:38:35 | [diff] [blame] | 254 | void InterceptNavigationDelegate::RequestFinishPendingShouldIgnoreCheck() { |
Michael Thiessen | 8c2b10e | 2025-01-23 17:54:19 | [diff] [blame] | 255 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 256 | ScopedJavaLocalRef<jobject> jdelegate = weak_jdelegate_.get(env); |
| 257 | |
| 258 | if (jdelegate.is_null()) { |
| 259 | OnShouldIgnoreNavigationResult(false); |
| 260 | return; |
| 261 | } |
Michael Thiessen | 729a467 | 2025-01-29 20:38:35 | [diff] [blame] | 262 | Java_InterceptNavigationDelegate_requestFinishPendingShouldIgnoreCheck( |
| 263 | env, jdelegate); |
Michael Thiessen | ca245a38 | 2022-02-21 16:11:17 | [diff] [blame] | 264 | } |
| 265 | |
Michael Thiessen | 7cb129e | 2022-11-08 17:24:51 | [diff] [blame] | 266 | void InterceptNavigationDelegate::HandleSubframeExternalProtocol( |
Michael Thiessen | ca245a38 | 2022-02-21 16:11:17 | [diff] [blame] | 267 | const GURL& url, |
| 268 | ui::PageTransition page_transition, |
| 269 | bool has_user_gesture, |
Arthur Sonzogni | c571efb | 2024-01-26 20:26:18 | [diff] [blame] | 270 | const std::optional<url::Origin>& initiating_origin, |
Michael Thiessen | 1a49e4d5 | 2022-12-02 21:54:40 | [diff] [blame] | 271 | mojo::PendingRemote<network::mojom::URLLoaderFactory>* out_factory) { |
Michael Thiessen | 7b53117 | 2023-01-28 05:25:59 | [diff] [blame] | 272 | // If there's a pending async subframe action, don't consider external |
| 273 | // navigation for the current navigation. |
| 274 | if (subframe_redirect_url_ || url_loader_) { |
| 275 | return; |
| 276 | } |
| 277 | |
Michael Thiessen | ca245a38 | 2022-02-21 16:11:17 | [diff] [blame] | 278 | GURL escaped_url = escape_external_handler_value_ |
Ryan Hamilton | 7f3bd3d | 2022-04-23 00:07:39 | [diff] [blame] | 279 | ? GURL(base::EscapeExternalHandlerValue(url.spec())) |
Michael Thiessen | ca245a38 | 2022-02-21 16:11:17 | [diff] [blame] | 280 | : url; |
| 281 | if (!escaped_url.is_valid()) |
| 282 | return; |
| 283 | |
| 284 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 285 | ScopedJavaLocalRef<jobject> jdelegate = weak_jdelegate_.get(env); |
| 286 | |
| 287 | if (jdelegate.is_null()) |
| 288 | return; |
Michael Thiessen | 1a49e4d5 | 2022-12-02 21:54:40 | [diff] [blame] | 289 | ScopedJavaLocalRef<jobject> j_gurl = |
| 290 | Java_InterceptNavigationDelegate_handleSubframeExternalProtocol( |
| 291 | env, jdelegate, url::GURLAndroid::FromNativeGURL(env, escaped_url), |
| 292 | page_transition, has_user_gesture, |
Andrew Grieve | 6a37bcd | 2024-09-26 21:47:44 | [diff] [blame] | 293 | initiating_origin ? initiating_origin->ToJavaObject(env) : nullptr); |
Michael Thiessen | 1a49e4d5 | 2022-12-02 21:54:40 | [diff] [blame] | 294 | if (j_gurl.is_null()) |
| 295 | return; |
Andrew Grieve | 7912fd3 | 2024-04-15 17:23:05 | [diff] [blame] | 296 | subframe_redirect_url_ = |
| 297 | std::make_unique<GURL>(url::GURLAndroid::ToNativeGURL(env, j_gurl)); |
Michael Thiessen | 1a49e4d5 | 2022-12-02 21:54:40 | [diff] [blame] | 298 | |
| 299 | mojo::PendingReceiver<network::mojom::URLLoaderFactory> receiver = |
| 300 | out_factory->InitWithNewPipeAndPassReceiver(); |
| 301 | scoped_refptr<network::SharedURLLoaderFactory> loader_factory = |
| 302 | base::MakeRefCounted<network::SingleRequestURLLoaderFactory>( |
Michael Thiessen | 7b53117 | 2023-01-28 05:25:59 | [diff] [blame] | 303 | base::BindOnce(&InterceptNavigationDelegate::LoaderCallback, |
| 304 | weak_ptr_factory_.GetWeakPtr())); |
Michael Thiessen | 1a49e4d5 | 2022-12-02 21:54:40 | [diff] [blame] | 305 | loader_factory->Clone(std::move(receiver)); |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 306 | } |
| 307 | |
Michael Thiessen | 7b53117 | 2023-01-28 05:25:59 | [diff] [blame] | 308 | void InterceptNavigationDelegate::LoaderCallback( |
| 309 | const network::ResourceRequest& resource_request, |
| 310 | mojo::PendingReceiver<network::mojom::URLLoader> pending_receiver, |
| 311 | mojo::PendingRemote<network::mojom::URLLoaderClient> pending_client) { |
| 312 | url_loader_ = mojo::MakeSelfOwnedReceiver( |
| 313 | std::make_unique<RedirectURLLoader>(resource_request, |
| 314 | std::move(pending_client)), |
| 315 | std::move(pending_receiver)); |
| 316 | MaybeHandleSubframeAction(); |
| 317 | } |
| 318 | |
| 319 | void InterceptNavigationDelegate::MaybeHandleSubframeAction() { |
| 320 | // An empty subframe_redirect_url_ implies a pending async action. |
| 321 | if (!url_loader_ || |
| 322 | (subframe_redirect_url_ && subframe_redirect_url_->is_empty())) { |
| 323 | return; |
| 324 | } |
| 325 | RedirectURLLoader* loader = |
| 326 | static_cast<RedirectURLLoader*>(url_loader_->impl()); |
| 327 | if (!subframe_redirect_url_) { |
| 328 | loader->OnNonRedirectAsyncAction(); |
| 329 | } else { |
| 330 | loader->DoRedirect(std::move(subframe_redirect_url_)); |
| 331 | } |
| 332 | url_loader_.reset(); |
| 333 | } |
| 334 | |
Michael Thiessen | 332dadb6 | 2022-07-13 14:44:07 | [diff] [blame] | 335 | void InterceptNavigationDelegate::OnResourceRequestWithGesture() { |
| 336 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 337 | ScopedJavaLocalRef<jobject> jdelegate = weak_jdelegate_.get(env); |
| 338 | if (jdelegate.is_null()) |
| 339 | return; |
| 340 | Java_InterceptNavigationDelegate_onResourceRequestWithGesture(env, jdelegate); |
Michael Thiessen | e566352 | 2022-05-25 21:23:28 | [diff] [blame] | 341 | } |
| 342 | |
Michael Thiessen | 7b53117 | 2023-01-28 05:25:59 | [diff] [blame] | 343 | void InterceptNavigationDelegate::OnSubframeAsyncActionTaken( |
| 344 | JNIEnv* env, |
| 345 | const base::android::JavaParamRef<jobject>& j_gurl) { |
| 346 | // subframe_redirect_url_ no longer empty indicates the async action has been |
| 347 | // taken. |
| 348 | subframe_redirect_url_ = |
Andrew Grieve | 7912fd3 | 2024-04-15 17:23:05 | [diff] [blame] | 349 | j_gurl.is_null() |
| 350 | ? nullptr |
| 351 | : std::make_unique<GURL>(url::GURLAndroid::ToNativeGURL(env, j_gurl)); |
Michael Thiessen | 7b53117 | 2023-01-28 05:25:59 | [diff] [blame] | 352 | MaybeHandleSubframeAction(); |
| 353 | } |
| 354 | |
Michael Thiessen | 8c2b10e | 2025-01-23 17:54:19 | [diff] [blame] | 355 | static void JNI_InterceptNavigationDelegate_OnShouldIgnoreNavigationResult( |
| 356 | JNIEnv* env, |
| 357 | const base::android::JavaParamRef<jobject>& jweb_contents, |
| 358 | jboolean should_ignore) { |
| 359 | content::WebContents* web_contents = |
| 360 | content::WebContents::FromJavaWebContents(jweb_contents); |
| 361 | if (!web_contents) { |
|
|