mkosiba@chromium.org | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 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" |
Sebastien Marchand | 53801a3 | 2019-01-25 16:26:11 | [diff] [blame] | 11 | #include "base/bind.h" |
mkosiba@chromium.org | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 12 | #include "base/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 | ca245a38 | 2022-02-21 16:11:17 | [diff] [blame] | 21 | #include "url/android/gurl_android.h" |
tfarina@chromium.org | e3b599e | 2013-07-05 07:15:17 | [diff] [blame] | 22 | #include "url/gurl.h" |
mkosiba@chromium.org | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 23 | |
| 24 | using base::android::ConvertUTF8ToJavaString; |
| 25 | using base::android::ScopedJavaLocalRef; |
| 26 | using content::BrowserThread; |
| 27 | using content::RenderViewHost; |
| 28 | using content::WebContents; |
Michael Thiessen | ca245a38 | 2022-02-21 16:11:17 | [diff] [blame] | 29 | using ui::PageTransition; |
mkosiba@chromium.org | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 30 | |
tfarina@chromium.org | 8812e3d0 | 2013-05-22 12:38:53 | [diff] [blame] | 31 | namespace navigation_interception { |
mkosiba@chromium.org | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 32 | |
| 33 | namespace { |
| 34 | |
thestig | 3b6a2f1 | 2015-09-25 08:17:20 | [diff] [blame] | 35 | const void* const kInterceptNavigationDelegateUserDataKey = |
mkosiba@chromium.org | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 36 | &kInterceptNavigationDelegateUserDataKey; |
| 37 | |
Michael Thiessen | ca245a38 | 2022-02-21 16:11:17 | [diff] [blame] | 38 | bool CheckIfShouldIgnoreNavigationOnUIThread( |
| 39 | content::NavigationHandle* navigation_handle) { |
mostynb | ad1e8c96 | 2015-03-25 21:51:12 | [diff] [blame] | 40 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Michael Thiessen | ca245a38 | 2022-02-21 16:11:17 | [diff] [blame] | 41 | DCHECK(navigation_handle); |
mkosiba@chromium.org | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 42 | |
mkosiba@chromium.org | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 43 | InterceptNavigationDelegate* intercept_navigation_delegate = |
Michael Thiessen | ca245a38 | 2022-02-21 16:11:17 | [diff] [blame] | 44 | InterceptNavigationDelegate::Get(navigation_handle->GetWebContents()); |
mkosiba@chromium.org | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 45 | if (!intercept_navigation_delegate) |
| 46 | return false; |
| 47 | |
Michael Thiessen | ca245a38 | 2022-02-21 16:11:17 | [diff] [blame] | 48 | return intercept_navigation_delegate->ShouldIgnoreNavigation( |
| 49 | navigation_handle); |
mkosiba@chromium.org | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 50 | } |
| 51 | |
thestig@chromium.org | a8e69a74 | 2013-10-15 10:58:55 | [diff] [blame] | 52 | } // namespace |
mkosiba@chromium.org | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 53 | |
| 54 | // static |
| 55 | void InterceptNavigationDelegate::Associate( |
| 56 | WebContents* web_contents, |
dcheng | 84c358e | 2016-04-26 07:05:53 | [diff] [blame] | 57 | std::unique_ptr<InterceptNavigationDelegate> delegate) { |
mkosiba@chromium.org | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 58 | web_contents->SetUserData(kInterceptNavigationDelegateUserDataKey, |
avi | 8945fc9 | 2017-05-02 16:03:23 | [diff] [blame] | 59 | std::move(delegate)); |
mkosiba@chromium.org | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | // static |
| 63 | InterceptNavigationDelegate* InterceptNavigationDelegate::Get( |
| 64 | WebContents* web_contents) { |
dtrainor | 037df0d | 2014-10-08 18:05:24 | [diff] [blame] | 65 | return static_cast<InterceptNavigationDelegate*>( |
mkosiba@chromium.org | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 66 | web_contents->GetUserData(kInterceptNavigationDelegateUserDataKey)); |
| 67 | } |
| 68 | |
| 69 | // static |
dcheng | 84c358e | 2016-04-26 07:05:53 | [diff] [blame] | 70 | std::unique_ptr<content::NavigationThrottle> |
David Bokan | 2a48f7bb | 2021-07-09 13:21:36 | [diff] [blame] | 71 | InterceptNavigationDelegate::MaybeCreateThrottleFor( |
Charlie Harrison | 3286ab7 | 2019-02-13 20:13:30 | [diff] [blame] | 72 | content::NavigationHandle* handle, |
| 73 | navigation_interception::SynchronyMode mode) { |
David Bokan | 2a48f7bb | 2021-07-09 13:21:36 | [diff] [blame] | 74 | // Navigations in a subframe or non-primary frame tree should not be |
| 75 | // intercepted. As examples of a non-primary frame tree, a navigation |
| 76 | // occurring in a Portal element or an unactivated prerendering page should |
| 77 | // not launch an app. |
| 78 | // TODO(bokan): This is a bit of a stopgap approach since we won't run |
| 79 | // throttles again when the prerender is activated which means links that are |
| 80 | // prerendered will avoid launching an app intent that a regular navigation |
| 81 | // would have. Longer term we'll want prerender activation to check for app |
| 82 | // intents, or have this throttle cancel the prerender if an intent would |
| 83 | // have been launched (without launching the intent). It's also not clear |
| 84 | // what the right behavior for <portal> elements is. |
| 85 | // https://crbug.com/1227659. |
| 86 | if (!handle->IsInPrimaryMainFrame()) |
| 87 | return nullptr; |
| 88 | |
Gyuyoung Kim | cb7965e | 2018-01-25 00:39:01 | [diff] [blame] | 89 | return std::make_unique<InterceptNavigationThrottle>( |
Ken Rockot | ae24ce9 | 2019-12-19 20:00:25 | [diff] [blame] | 90 | handle, base::BindRepeating(&CheckIfShouldIgnoreNavigationOnUIThread), |
| 91 | mode); |
mkosiba@chromium.org | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | InterceptNavigationDelegate::InterceptNavigationDelegate( |
Colin Blundell | 4695e814 | 2020-03-16 11:13:12 | [diff] [blame] | 95 | JNIEnv* env, |
| 96 | jobject jdelegate, |
| 97 | bool escape_external_handler_value) |
| 98 | : weak_jdelegate_(env, jdelegate), |
| 99 | escape_external_handler_value_(escape_external_handler_value) {} |
mkosiba@chromium.org | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 100 | |
| 101 | InterceptNavigationDelegate::~InterceptNavigationDelegate() { |
| 102 | } |
| 103 | |
| 104 | bool InterceptNavigationDelegate::ShouldIgnoreNavigation( |
Michael Thiessen | ca245a38 | 2022-02-21 16:11:17 | [diff] [blame] | 105 | content::NavigationHandle* navigation_handle) { |
| 106 | GURL escaped_url = escape_external_handler_value_ |
Ryan Hamilton | 7f3bd3d | 2022-04-23 00:07:39 | [diff] [blame^] | 107 | ? GURL(base::EscapeExternalHandlerValue( |
Michael Thiessen | ca245a38 | 2022-02-21 16:11:17 | [diff] [blame] | 108 | navigation_handle->GetURL().spec())) |
| 109 | : navigation_handle->GetURL(); |
Colin Blundell | 4695e814 | 2020-03-16 11:13:12 | [diff] [blame] | 110 | |
Michael Thiessen | ca245a38 | 2022-02-21 16:11:17 | [diff] [blame] | 111 | if (!escaped_url.is_valid()) |
mkosiba@chromium.org | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 112 | return false; |
| 113 | |
| 114 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 115 | ScopedJavaLocalRef<jobject> jdelegate = weak_jdelegate_.get(env); |
| 116 | |
| 117 | if (jdelegate.is_null()) |
| 118 | return false; |
| 119 | |
mkosiba@chromium.org | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 120 | return Java_InterceptNavigationDelegate_shouldIgnoreNavigation( |
Michael Thiessen | ca245a38 | 2022-02-21 16:11:17 | [diff] [blame] | 121 | env, jdelegate, navigation_handle->GetJavaNavigationHandle(), |
| 122 | url::GURLAndroid::FromNativeGURL(env, escaped_url)); |
| 123 | } |
| 124 | |
| 125 | void InterceptNavigationDelegate::HandleExternalProtocolDialog( |
| 126 | const GURL& url, |
| 127 | ui::PageTransition page_transition, |
| 128 | bool has_user_gesture, |
| 129 | const absl::optional<url::Origin>& initiating_origin) { |
| 130 | GURL escaped_url = escape_external_handler_value_ |
Ryan Hamilton | 7f3bd3d | 2022-04-23 00:07:39 | [diff] [blame^] | 131 | ? GURL(base::EscapeExternalHandlerValue(url.spec())) |
Michael Thiessen | ca245a38 | 2022-02-21 16:11:17 | [diff] [blame] | 132 | : url; |
| 133 | if (!escaped_url.is_valid()) |
| 134 | return; |
| 135 | |
| 136 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 137 | ScopedJavaLocalRef<jobject> jdelegate = weak_jdelegate_.get(env); |
| 138 | |
| 139 | if (jdelegate.is_null()) |
| 140 | return; |
| 141 | Java_InterceptNavigationDelegate_handleExternalProtocolDialog( |
| 142 | env, jdelegate, url::GURLAndroid::FromNativeGURL(env, escaped_url), |
| 143 | page_transition, has_user_gesture, |
| 144 | initiating_origin ? initiating_origin->CreateJavaObject() : nullptr); |
mkosiba@chromium.org | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 145 | } |
| 146 | |
tfarina@chromium.org | 8812e3d0 | 2013-05-22 12:38:53 | [diff] [blame] | 147 | } // namespace navigation_interception |