blob: 3939a3626923a70ff44cba63901797d225668d9a [file] [log] [blame]
[email protected]4360ae72012-10-09 22:10:461// 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]62885ab2013-01-23 03:55:165#include "components/navigation_interception/intercept_navigation_delegate.h"
[email protected]4360ae72012-10-09 22:10:466
Gyuyoung Kimcb7965e2018-01-25 00:39:017#include <memory>
8
[email protected]4360ae72012-10-09 22:10:469#include "base/android/jni_android.h"
10#include "base/android/jni_string.h"
Sebastien Marchand53801a32019-01-25 16:26:1111#include "base/bind.h"
[email protected]4360ae72012-10-09 22:10:4612#include "base/callback.h"
[email protected]c0d243a2013-01-31 21:20:1613#include "components/navigation_interception/navigation_params_android.h"
[email protected]4360ae72012-10-09 22:10:4614#include "content/public/browser/browser_thread.h"
clamy40c9e142015-09-29 11:18:4715#include "content/public/browser/navigation_throttle.h"
jaekyun038903192015-03-31 14:15:5916#include "content/public/browser/render_frame_host.h"
[email protected]4360ae72012-10-09 22:10:4617#include "content/public/browser/render_view_host.h"
jaekyun038903192015-03-31 14:15:5918#include "content/public/browser/resource_request_info.h"
[email protected]4360ae72012-10-09 22:10:4619#include "content/public/browser/web_contents.h"
[email protected]4360ae72012-10-09 22:10:4620#include "jni/InterceptNavigationDelegate_jni.h"
[email protected]e3b599e2013-07-05 07:15:1721#include "url/gurl.h"
[email protected]4360ae72012-10-09 22:10:4622
23using base::android::ConvertUTF8ToJavaString;
24using base::android::ScopedJavaLocalRef;
25using content::BrowserThread;
Sylvain Defresnec6ccc77d2014-09-19 10:19:3526using ui::PageTransition;
[email protected]4360ae72012-10-09 22:10:4627using content::RenderViewHost;
28using content::WebContents;
29
[email protected]8812e3d02013-05-22 12:38:5330namespace navigation_interception {
[email protected]4360ae72012-10-09 22:10:4631
32namespace {
33
jaekyun038903192015-03-31 14:15:5934const int kMaxValidityOfUserGestureCarryoverInSeconds = 10;
35
thestig3b6a2f12015-09-25 08:17:2036const void* const kInterceptNavigationDelegateUserDataKey =
[email protected]4360ae72012-10-09 22:10:4637 &kInterceptNavigationDelegateUserDataKey;
38
[email protected]5dcaf8e2013-12-28 01:31:4239bool CheckIfShouldIgnoreNavigationOnUIThread(WebContents* source,
[email protected]c0d243a2013-01-31 21:20:1640 const NavigationParams& params) {
mostynbad1e8c962015-03-25 21:51:1241 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]4360ae72012-10-09 22:10:4642 DCHECK(source);
43
[email protected]4360ae72012-10-09 22:10:4644 InterceptNavigationDelegate* intercept_navigation_delegate =
[email protected]5dcaf8e2013-12-28 01:31:4245 InterceptNavigationDelegate::Get(source);
[email protected]4360ae72012-10-09 22:10:4646 if (!intercept_navigation_delegate)
47 return false;
48
[email protected]c0d243a2013-01-31 21:20:1649 return intercept_navigation_delegate->ShouldIgnoreNavigation(params);
[email protected]4360ae72012-10-09 22:10:4650}
51
[email protected]a8e69a742013-10-15 10:58:5552} // namespace
[email protected]4360ae72012-10-09 22:10:4653
54// static
55void InterceptNavigationDelegate::Associate(
56 WebContents* web_contents,
dcheng84c358e2016-04-26 07:05:5357 std::unique_ptr<InterceptNavigationDelegate> delegate) {
[email protected]4360ae72012-10-09 22:10:4658 web_contents->SetUserData(kInterceptNavigationDelegateUserDataKey,
avi8945fc92017-05-02 16:03:2359 std::move(delegate));
[email protected]4360ae72012-10-09 22:10:4660}
61
62// static
63InterceptNavigationDelegate* InterceptNavigationDelegate::Get(
64 WebContents* web_contents) {
dtrainor037df0d2014-10-08 18:05:2465 return static_cast<InterceptNavigationDelegate*>(
[email protected]4360ae72012-10-09 22:10:4666 web_contents->GetUserData(kInterceptNavigationDelegateUserDataKey));
67}
68
69// static
dcheng84c358e2016-04-26 07:05:5370std::unique_ptr<content::NavigationThrottle>
clamy40c9e142015-09-29 11:18:4771InterceptNavigationDelegate::CreateThrottleFor(
Charlie Harrison3286ab72019-02-13 20:13:3072 content::NavigationHandle* handle,
73 navigation_interception::SynchronyMode mode) {
Gyuyoung Kimcb7965e2018-01-25 00:39:0174 return std::make_unique<InterceptNavigationThrottle>(
Charlie Harrison3286ab72019-02-13 20:13:3075 handle, base::Bind(&CheckIfShouldIgnoreNavigationOnUIThread), mode);
[email protected]4360ae72012-10-09 22:10:4676}
77
[email protected]4360ae72012-10-09 22:10:4678InterceptNavigationDelegate::InterceptNavigationDelegate(
79 JNIEnv* env, jobject jdelegate)
80 : weak_jdelegate_(env, jdelegate) {
81}
82
83InterceptNavigationDelegate::~InterceptNavigationDelegate() {
84}
85
86bool InterceptNavigationDelegate::ShouldIgnoreNavigation(
[email protected]c0d243a2013-01-31 21:20:1687 const NavigationParams& navigation_params) {
88 if (!navigation_params.url().is_valid())
[email protected]4360ae72012-10-09 22:10:4689 return false;
90
91 JNIEnv* env = base::android::AttachCurrentThread();
92 ScopedJavaLocalRef<jobject> jdelegate = weak_jdelegate_.get(env);
93
94 if (jdelegate.is_null())
95 return false;
96
jaekyun038903192015-03-31 14:15:5997 bool has_user_gesture_carryover =
98 !navigation_params.has_user_gesture() &&
99 base::TimeTicks::Now() - last_user_gesture_carryover_timestamp_ <=
100 base::TimeDelta::FromSeconds(
101 kMaxValidityOfUserGestureCarryoverInSeconds);
102
103 ScopedJavaLocalRef<jobject> jobject_params = CreateJavaNavigationParams(
104 env, navigation_params, has_user_gesture_carryover);
[email protected]c0d243a2013-01-31 21:20:16105
[email protected]4360ae72012-10-09 22:10:46106 return Java_InterceptNavigationDelegate_shouldIgnoreNavigation(
torne948f3662016-08-16 15:10:44107 env, jdelegate, jobject_params);
[email protected]4360ae72012-10-09 22:10:46108}
109
jaekyun038903192015-03-31 14:15:59110void InterceptNavigationDelegate::UpdateLastUserGestureCarryoverTimestamp() {
111 last_user_gesture_carryover_timestamp_ = base::TimeTicks::Now();
112}
113
[email protected]8812e3d02013-05-22 12:38:53114} // namespace navigation_interception