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.
5 #include "components/navigation_interception/intercept_navigation_delegate.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "base/callback.h"
10 #include "components/navigation_interception/intercept_navigation_throttle.h"
11 #include "components/navigation_interception/navigation_params_android.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/navigation_throttle.h"
14 #include "content/public/browser/render_frame_host.h"
15 #include "content/public/browser/render_view_host.h"
16 #include "content/public/browser/resource_request_info.h"
17 #include "content/public/browser/web_contents.h"
18 #include "jni/InterceptNavigationDelegate_jni.h"
19 #include "net/url_request/url_request.h"
22 using base::android::ConvertUTF8ToJavaString
;
23 using base::android::ScopedJavaLocalRef
;
24 using content::BrowserThread
;
25 using ui::PageTransition
;
26 using content::RenderViewHost
;
27 using content::WebContents
;
29 namespace navigation_interception
{
33 const int kMaxValidityOfUserGestureCarryoverInSeconds
= 10;
35 const void* kInterceptNavigationDelegateUserDataKey
=
36 &kInterceptNavigationDelegateUserDataKey
;
38 bool CheckIfShouldIgnoreNavigationOnUIThread(WebContents
* source
,
39 const NavigationParams
& params
) {
40 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
43 InterceptNavigationDelegate
* intercept_navigation_delegate
=
44 InterceptNavigationDelegate::Get(source
);
45 if (!intercept_navigation_delegate
)
48 return intercept_navigation_delegate
->ShouldIgnoreNavigation(params
);
51 void UpdateUserGestureCarryoverInfoOnUIThread(int render_process_id
,
52 int render_frame_id
) {
53 content::RenderFrameHost
* render_frame_host
=
54 content::RenderFrameHost::FromID(render_process_id
, render_frame_id
);
55 if (!render_frame_host
)
58 content::WebContents
* web_contents
=
59 content::WebContents::FromRenderFrameHost(render_frame_host
);
61 InterceptNavigationDelegate
* intercept_navigation_delegate
=
62 InterceptNavigationDelegate::Get(web_contents
);
64 if (intercept_navigation_delegate
) {
65 intercept_navigation_delegate
->UpdateLastUserGestureCarryoverTimestamp();
72 void InterceptNavigationDelegate::Associate(
73 WebContents
* web_contents
,
74 scoped_ptr
<InterceptNavigationDelegate
> delegate
) {
75 web_contents
->SetUserData(kInterceptNavigationDelegateUserDataKey
,
80 InterceptNavigationDelegate
* InterceptNavigationDelegate::Get(
81 WebContents
* web_contents
) {
82 return static_cast<InterceptNavigationDelegate
*>(
83 web_contents
->GetUserData(kInterceptNavigationDelegateUserDataKey
));
87 scoped_ptr
<content::NavigationThrottle
>
88 InterceptNavigationDelegate::CreateThrottleFor(
89 content::NavigationHandle
* handle
) {
90 return scoped_ptr
<content::NavigationThrottle
>(
91 new InterceptNavigationThrottle(
92 handle
, base::Bind(&CheckIfShouldIgnoreNavigationOnUIThread
)));
96 void InterceptNavigationDelegate::UpdateUserGestureCarryoverInfo(
97 net::URLRequest
* request
) {
98 const content::ResourceRequestInfo
* info
=
99 content::ResourceRequestInfo::ForRequest(request
);
100 if (!info
|| !info
->HasUserGesture())
103 int render_process_id
, render_frame_id
;
104 if (!info
->GetAssociatedRenderFrame(&render_process_id
, &render_frame_id
))
107 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
108 base::Bind(&UpdateUserGestureCarryoverInfoOnUIThread
,
109 render_process_id
, render_frame_id
));
112 InterceptNavigationDelegate::InterceptNavigationDelegate(
113 JNIEnv
* env
, jobject jdelegate
)
114 : weak_jdelegate_(env
, jdelegate
) {
117 InterceptNavigationDelegate::~InterceptNavigationDelegate() {
120 bool InterceptNavigationDelegate::ShouldIgnoreNavigation(
121 const NavigationParams
& navigation_params
) {
122 if (!navigation_params
.url().is_valid())
125 JNIEnv
* env
= base::android::AttachCurrentThread();
126 ScopedJavaLocalRef
<jobject
> jdelegate
= weak_jdelegate_
.get(env
);
128 if (jdelegate
.is_null())
131 bool has_user_gesture_carryover
=
132 !navigation_params
.has_user_gesture() &&
133 base::TimeTicks::Now() - last_user_gesture_carryover_timestamp_
<=
134 base::TimeDelta::FromSeconds(
135 kMaxValidityOfUserGestureCarryoverInSeconds
);
137 ScopedJavaLocalRef
<jobject
> jobject_params
= CreateJavaNavigationParams(
138 env
, navigation_params
, has_user_gesture_carryover
);
140 return Java_InterceptNavigationDelegate_shouldIgnoreNavigation(
143 jobject_params
.obj());
146 void InterceptNavigationDelegate::UpdateLastUserGestureCarryoverTimestamp() {
147 last_user_gesture_carryover_timestamp_
= base::TimeTicks::Now();
150 // Register native methods.
152 bool RegisterInterceptNavigationDelegate(JNIEnv
* env
) {
153 return RegisterNativesImpl(env
);
156 } // namespace navigation_interception