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