[email protected] | 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 | |
[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" |
| 11 | #include "base/callback.h" |
clamy | 40c9e14 | 2015-09-29 11:18:47 | [diff] [blame] | 12 | #include "components/navigation_interception/intercept_navigation_throttle.h" |
[email protected] | c0d243a | 2013-01-31 21:20:16 | [diff] [blame] | 13 | #include "components/navigation_interception/navigation_params_android.h" |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 14 | #include "content/public/browser/browser_thread.h" |
clamy | 40c9e14 | 2015-09-29 11:18:47 | [diff] [blame] | 15 | #include "content/public/browser/navigation_throttle.h" |
jaekyun | 03890319 | 2015-03-31 14:15:59 | [diff] [blame] | 16 | #include "content/public/browser/render_frame_host.h" |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 17 | #include "content/public/browser/render_view_host.h" |
jaekyun | 03890319 | 2015-03-31 14:15:59 | [diff] [blame] | 18 | #include "content/public/browser/resource_request_info.h" |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 19 | #include "content/public/browser/web_contents.h" |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 20 | #include "jni/InterceptNavigationDelegate_jni.h" |
| 21 | #include "net/url_request/url_request.h" |
[email protected] | e3b599e | 2013-07-05 07:15:17 | [diff] [blame] | 22 | #include "url/gurl.h" |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 23 | |
| 24 | using base::android::ConvertUTF8ToJavaString; |
| 25 | using base::android::ScopedJavaLocalRef; |
| 26 | using content::BrowserThread; |
Sylvain Defresne | c6ccc77d | 2014-09-19 10:19:35 | [diff] [blame] | 27 | using ui::PageTransition; |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 28 | using content::RenderViewHost; |
| 29 | using content::WebContents; |
| 30 | |
[email protected] | 8812e3d0 | 2013-05-22 12:38:53 | [diff] [blame] | 31 | namespace navigation_interception { |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 32 | |
| 33 | namespace { |
| 34 | |
jaekyun | 03890319 | 2015-03-31 14:15:59 | [diff] [blame] | 35 | const int kMaxValidityOfUserGestureCarryoverInSeconds = 10; |
| 36 | |
thestig | 3b6a2f1 | 2015-09-25 08:17:20 | [diff] [blame] | 37 | const void* const kInterceptNavigationDelegateUserDataKey = |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 38 | &kInterceptNavigationDelegateUserDataKey; |
| 39 | |
[email protected] | 5dcaf8e | 2013-12-28 01:31:42 | [diff] [blame] | 40 | bool CheckIfShouldIgnoreNavigationOnUIThread(WebContents* source, |
[email protected] | c0d243a | 2013-01-31 21:20:16 | [diff] [blame] | 41 | const NavigationParams& params) { |
mostynb | ad1e8c96 | 2015-03-25 21:51:12 | [diff] [blame] | 42 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 43 | DCHECK(source); |
| 44 | |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 45 | InterceptNavigationDelegate* intercept_navigation_delegate = |
[email protected] | 5dcaf8e | 2013-12-28 01:31:42 | [diff] [blame] | 46 | InterceptNavigationDelegate::Get(source); |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 47 | if (!intercept_navigation_delegate) |
| 48 | return false; |
| 49 | |
[email protected] | c0d243a | 2013-01-31 21:20:16 | [diff] [blame] | 50 | return intercept_navigation_delegate->ShouldIgnoreNavigation(params); |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 51 | } |
| 52 | |
jaekyun | 03890319 | 2015-03-31 14:15:59 | [diff] [blame] | 53 | void UpdateUserGestureCarryoverInfoOnUIThread(int render_process_id, |
| 54 | int render_frame_id) { |
| 55 | content::RenderFrameHost* render_frame_host = |
| 56 | content::RenderFrameHost::FromID(render_process_id, render_frame_id); |
| 57 | if (!render_frame_host) |
| 58 | return; |
| 59 | |
| 60 | content::WebContents* web_contents = |
| 61 | content::WebContents::FromRenderFrameHost(render_frame_host); |
| 62 | |
| 63 | InterceptNavigationDelegate* intercept_navigation_delegate = |
| 64 | InterceptNavigationDelegate::Get(web_contents); |
| 65 | |
| 66 | if (intercept_navigation_delegate) { |
| 67 | intercept_navigation_delegate->UpdateLastUserGestureCarryoverTimestamp(); |
| 68 | } |
| 69 | } |
| 70 | |
[email protected] | a8e69a74 | 2013-10-15 10:58:55 | [diff] [blame] | 71 | } // namespace |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 72 | |
| 73 | // static |
| 74 | void InterceptNavigationDelegate::Associate( |
| 75 | WebContents* web_contents, |
dcheng | 84c358e | 2016-04-26 07:05:53 | [diff] [blame] | 76 | std::unique_ptr<InterceptNavigationDelegate> delegate) { |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 77 | web_contents->SetUserData(kInterceptNavigationDelegateUserDataKey, |
avi | 8945fc9 | 2017-05-02 16:03:23 | [diff] [blame] | 78 | std::move(delegate)); |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | // static |
| 82 | InterceptNavigationDelegate* InterceptNavigationDelegate::Get( |
| 83 | WebContents* web_contents) { |
dtrainor | 037df0d | 2014-10-08 18:05:24 | [diff] [blame] | 84 | return static_cast<InterceptNavigationDelegate*>( |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 85 | web_contents->GetUserData(kInterceptNavigationDelegateUserDataKey)); |
| 86 | } |
| 87 | |
| 88 | // static |
dcheng | 84c358e | 2016-04-26 07:05:53 | [diff] [blame] | 89 | std::unique_ptr<content::NavigationThrottle> |
clamy | 40c9e14 | 2015-09-29 11:18:47 | [diff] [blame] | 90 | InterceptNavigationDelegate::CreateThrottleFor( |
| 91 | content::NavigationHandle* handle) { |
Gyuyoung Kim | cb7965e | 2018-01-25 00:39:01 | [diff] [blame] | 92 | return std::make_unique<InterceptNavigationThrottle>( |
Jinsuk Kim | c644aad | 2017-07-03 21:32:44 | [diff] [blame] | 93 | handle, base::Bind(&CheckIfShouldIgnoreNavigationOnUIThread)); |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 94 | } |
| 95 | |
jaekyun | 03890319 | 2015-03-31 14:15:59 | [diff] [blame] | 96 | // static |
| 97 | void InterceptNavigationDelegate::UpdateUserGestureCarryoverInfo( |
| 98 | net::URLRequest* request) { |
| 99 | const content::ResourceRequestInfo* info = |
| 100 | content::ResourceRequestInfo::ForRequest(request); |
| 101 | if (!info || !info->HasUserGesture()) |
| 102 | return; |
| 103 | |
| 104 | int render_process_id, render_frame_id; |
| 105 | if (!info->GetAssociatedRenderFrame(&render_process_id, &render_frame_id)) |
| 106 | return; |
| 107 | |
| 108 | BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 109 | base::Bind(&UpdateUserGestureCarryoverInfoOnUIThread, |
| 110 | render_process_id, render_frame_id)); |
| 111 | } |
| 112 | |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 113 | InterceptNavigationDelegate::InterceptNavigationDelegate( |
| 114 | JNIEnv* env, jobject jdelegate) |
| 115 | : weak_jdelegate_(env, jdelegate) { |
| 116 | } |
| 117 | |
| 118 | InterceptNavigationDelegate::~InterceptNavigationDelegate() { |
| 119 | } |
| 120 | |
| 121 | bool InterceptNavigationDelegate::ShouldIgnoreNavigation( |
[email protected] | c0d243a | 2013-01-31 21:20:16 | [diff] [blame] | 122 | const NavigationParams& navigation_params) { |
| 123 | if (!navigation_params.url().is_valid()) |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 124 | return false; |
| 125 | |
| 126 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 127 | ScopedJavaLocalRef<jobject> jdelegate = weak_jdelegate_.get(env); |
| 128 | |
| 129 | if (jdelegate.is_null()) |
| 130 | return false; |
| 131 | |
jaekyun | 03890319 | 2015-03-31 14:15:59 | [diff] [blame] | 132 | bool has_user_gesture_carryover = |
| 133 | !navigation_params.has_user_gesture() && |
| 134 | base::TimeTicks::Now() - last_user_gesture_carryover_timestamp_ <= |
| 135 | base::TimeDelta::FromSeconds( |
| 136 | kMaxValidityOfUserGestureCarryoverInSeconds); |
| 137 | |
| 138 | ScopedJavaLocalRef<jobject> jobject_params = CreateJavaNavigationParams( |
| 139 | env, navigation_params, has_user_gesture_carryover); |
[email protected] | c0d243a | 2013-01-31 21:20:16 | [diff] [blame] | 140 | |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 141 | return Java_InterceptNavigationDelegate_shouldIgnoreNavigation( |
torne | 948f366 | 2016-08-16 15:10:44 | [diff] [blame] | 142 | env, jdelegate, jobject_params); |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 143 | } |
| 144 | |
jaekyun | 03890319 | 2015-03-31 14:15:59 | [diff] [blame] | 145 | void InterceptNavigationDelegate::UpdateLastUserGestureCarryoverTimestamp() { |
| 146 | last_user_gesture_carryover_timestamp_ = base::TimeTicks::Now(); |
| 147 | } |
| 148 | |
[email protected] | 8812e3d0 | 2013-05-22 12:38:53 | [diff] [blame] | 149 | } // namespace navigation_interception |