blob: a6163478688b409814e29d581d417c2335d07b92 [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"
clamy40c9e142015-09-29 11:18:4713#include "components/navigation_interception/intercept_navigation_throttle.h"
[email protected]c0d243a2013-01-31 21:20:1614#include "components/navigation_interception/navigation_params_android.h"
[email protected]4360ae72012-10-09 22:10:4615#include "content/public/browser/browser_thread.h"
clamy40c9e142015-09-29 11:18:4716#include "content/public/browser/navigation_throttle.h"
jaekyun038903192015-03-31 14:15:5917#include "content/public/browser/render_frame_host.h"
[email protected]4360ae72012-10-09 22:10:4618#include "content/public/browser/render_view_host.h"
jaekyun038903192015-03-31 14:15:5919#include "content/public/browser/resource_request_info.h"
[email protected]4360ae72012-10-09 22:10:4620#include "content/public/browser/web_contents.h"
[email protected]4360ae72012-10-09 22:10:4621#include "jni/InterceptNavigationDelegate_jni.h"
[email protected]e3b599e2013-07-05 07:15:1722#include "url/gurl.h"
[email protected]4360ae72012-10-09 22:10:4623
24using base::android::ConvertUTF8ToJavaString;
25using base::android::ScopedJavaLocalRef;
26using content::BrowserThread;
Sylvain Defresnec6ccc77d2014-09-19 10:19:3527using ui::PageTransition;
[email protected]4360ae72012-10-09 22:10:4628using content::RenderViewHost;
29using content::WebContents;
30
[email protected]8812e3d02013-05-22 12:38:5331namespace navigation_interception {
[email protected]4360ae72012-10-09 22:10:4632
33namespace {
34
jaekyun038903192015-03-31 14:15:5935const int kMaxValidityOfUserGestureCarryoverInSeconds = 10;
36
thestig3b6a2f12015-09-25 08:17:2037const void* const kInterceptNavigationDelegateUserDataKey =
[email protected]4360ae72012-10-09 22:10:4638 &kInterceptNavigationDelegateUserDataKey;
39
[email protected]5dcaf8e2013-12-28 01:31:4240bool CheckIfShouldIgnoreNavigationOnUIThread(WebContents* source,
[email protected]c0d243a2013-01-31 21:20:1641 const NavigationParams& params) {
mostynbad1e8c962015-03-25 21:51:1242 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]4360ae72012-10-09 22:10:4643 DCHECK(source);
44
[email protected]4360ae72012-10-09 22:10:4645 InterceptNavigationDelegate* intercept_navigation_delegate =
[email protected]5dcaf8e2013-12-28 01:31:4246 InterceptNavigationDelegate::Get(source);
[email protected]4360ae72012-10-09 22:10:4647 if (!intercept_navigation_delegate)
48 return false;
49
[email protected]c0d243a2013-01-31 21:20:1650 return intercept_navigation_delegate->ShouldIgnoreNavigation(params);
[email protected]4360ae72012-10-09 22:10:4651}
52
[email protected]a8e69a742013-10-15 10:58:5553} // namespace
[email protected]4360ae72012-10-09 22:10:4654
55// static
56void InterceptNavigationDelegate::Associate(
57 WebContents* web_contents,
dcheng84c358e2016-04-26 07:05:5358 std::unique_ptr<InterceptNavigationDelegate> delegate) {
[email protected]4360ae72012-10-09 22:10:4659 web_contents->SetUserData(kInterceptNavigationDelegateUserDataKey,
avi8945fc92017-05-02 16:03:2360 std::move(delegate));
[email protected]4360ae72012-10-09 22:10:4661}
62
63// static
64InterceptNavigationDelegate* InterceptNavigationDelegate::Get(
65 WebContents* web_contents) {
dtrainor037df0d2014-10-08 18:05:2466 return static_cast<InterceptNavigationDelegate*>(
[email protected]4360ae72012-10-09 22:10:4667 web_contents->GetUserData(kInterceptNavigationDelegateUserDataKey));
68}
69
70// static
dcheng84c358e2016-04-26 07:05:5371std::unique_ptr<content::NavigationThrottle>
clamy40c9e142015-09-29 11:18:4772InterceptNavigationDelegate::CreateThrottleFor(
73 content::NavigationHandle* handle) {
Gyuyoung Kimcb7965e2018-01-25 00:39:0174 return std::make_unique<InterceptNavigationThrottle>(
Jinsuk Kimc644aad2017-07-03 21:32:4475 handle, base::Bind(&CheckIfShouldIgnoreNavigationOnUIThread));
[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