blob: d58f484a42bd10620e732eee2e618c93e28eee2e [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"
11#include "base/callback.h"
clamy40c9e142015-09-29 11:18:4712#include "components/navigation_interception/intercept_navigation_throttle.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"
21#include "net/url_request/url_request.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
jaekyun038903192015-03-31 14:15:5953void 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]a8e69a742013-10-15 10:58:5571} // namespace
[email protected]4360ae72012-10-09 22:10:4672
73// static
74void InterceptNavigationDelegate::Associate(
75 WebContents* web_contents,
dcheng84c358e2016-04-26 07:05:5376 std::unique_ptr<InterceptNavigationDelegate> delegate) {
[email protected]4360ae72012-10-09 22:10:4677 web_contents->SetUserData(kInterceptNavigationDelegateUserDataKey,
avi8945fc92017-05-02 16:03:2378 std::move(delegate));
[email protected]4360ae72012-10-09 22:10:4679}
80
81// static
82InterceptNavigationDelegate* InterceptNavigationDelegate::Get(
83 WebContents* web_contents) {
dtrainor037df0d2014-10-08 18:05:2484 return static_cast<InterceptNavigationDelegate*>(
[email protected]4360ae72012-10-09 22:10:4685 web_contents->GetUserData(kInterceptNavigationDelegateUserDataKey));
86}
87
88// static
dcheng84c358e2016-04-26 07:05:5389std::unique_ptr<content::NavigationThrottle>
clamy40c9e142015-09-29 11:18:4790InterceptNavigationDelegate::CreateThrottleFor(
91 content::NavigationHandle* handle) {
Gyuyoung Kimcb7965e2018-01-25 00:39:0192 return std::make_unique<InterceptNavigationThrottle>(
Jinsuk Kimc644aad2017-07-03 21:32:4493 handle, base::Bind(&CheckIfShouldIgnoreNavigationOnUIThread));
[email protected]4360ae72012-10-09 22:10:4694}
95
jaekyun038903192015-03-31 14:15:5996// static
97void 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]4360ae72012-10-09 22:10:46113InterceptNavigationDelegate::InterceptNavigationDelegate(
114 JNIEnv* env, jobject jdelegate)
115 : weak_jdelegate_(env, jdelegate) {
116}
117
118InterceptNavigationDelegate::~InterceptNavigationDelegate() {
119}
120
121bool InterceptNavigationDelegate::ShouldIgnoreNavigation(
[email protected]c0d243a2013-01-31 21:20:16122 const NavigationParams& navigation_params) {
123 if (!navigation_params.url().is_valid())
[email protected]4360ae72012-10-09 22:10:46124 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
jaekyun038903192015-03-31 14:15:59132 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]c0d243a2013-01-31 21:20:16140
[email protected]4360ae72012-10-09 22:10:46141 return Java_InterceptNavigationDelegate_shouldIgnoreNavigation(
torne948f3662016-08-16 15:10:44142 env, jdelegate, jobject_params);
[email protected]4360ae72012-10-09 22:10:46143}
144
jaekyun038903192015-03-31 14:15:59145void InterceptNavigationDelegate::UpdateLastUserGestureCarryoverTimestamp() {
146 last_user_gesture_carryover_timestamp_ = base::TimeTicks::Now();
147}
148
[email protected]8812e3d02013-05-22 12:38:53149} // namespace navigation_interception