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_resource_throttle.h"
11 #include "components/navigation_interception/navigation_params_android.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/render_frame_host.h"
14 #include "content/public/browser/render_view_host.h"
15 #include "content/public/browser/resource_request_info.h"
16 #include "content/public/browser/web_contents.h"
17 #include "jni/InterceptNavigationDelegate_jni.h"
18 #include "net/url_request/url_request.h"
21 using base::android::ConvertUTF8ToJavaString
;
22 using base::android::ScopedJavaLocalRef
;
23 using content::BrowserThread
;
24 using ui::PageTransition
;
25 using content::RenderViewHost
;
26 using content::WebContents
;
28 namespace navigation_interception
{
32 const int kMaxValidityOfUserGestureCarryoverInSeconds
= 10;
34 const void* kInterceptNavigationDelegateUserDataKey
=
35 &kInterceptNavigationDelegateUserDataKey
;
37 bool CheckIfShouldIgnoreNavigationOnUIThread(WebContents
* source
,
38 const NavigationParams
& params
) {
39 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
42 InterceptNavigationDelegate
* intercept_navigation_delegate
=
43 InterceptNavigationDelegate::Get(source
);
44 if (!intercept_navigation_delegate
)
47 return intercept_navigation_delegate
->ShouldIgnoreNavigation(params
);
50 void UpdateUserGestureCarryoverInfoOnUIThread(int render_process_id
,
51 int render_frame_id
) {
52 content::RenderFrameHost
* render_frame_host
=
53 content::RenderFrameHost::FromID(render_process_id
, render_frame_id
);
54 if (!render_frame_host
)
57 content::WebContents
* web_contents
=
58 content::WebContents::FromRenderFrameHost(render_frame_host
);
60 InterceptNavigationDelegate
* intercept_navigation_delegate
=
61 InterceptNavigationDelegate::Get(web_contents
);
63 if (intercept_navigation_delegate
) {
64 intercept_navigation_delegate
->UpdateLastUserGestureCarryoverTimestamp();
71 void InterceptNavigationDelegate::Associate(
72 WebContents
* web_contents
,
73 scoped_ptr
<InterceptNavigationDelegate
> delegate
) {
74 web_contents
->SetUserData(kInterceptNavigationDelegateUserDataKey
,
79 InterceptNavigationDelegate
* InterceptNavigationDelegate::Get(
80 WebContents
* web_contents
) {
81 return static_cast<InterceptNavigationDelegate
*>(
82 web_contents
->GetUserData(kInterceptNavigationDelegateUserDataKey
));
86 content::ResourceThrottle
* InterceptNavigationDelegate::CreateThrottleFor(
87 net::URLRequest
* request
) {
88 return new InterceptNavigationResourceThrottle(
89 request
, base::Bind(&CheckIfShouldIgnoreNavigationOnUIThread
));
93 void InterceptNavigationDelegate::UpdateUserGestureCarryoverInfo(
94 net::URLRequest
* request
) {
95 const content::ResourceRequestInfo
* info
=
96 content::ResourceRequestInfo::ForRequest(request
);
97 if (!info
|| !info
->HasUserGesture())
100 int render_process_id
, render_frame_id
;
101 if (!info
->GetAssociatedRenderFrame(&render_process_id
, &render_frame_id
))
104 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
105 base::Bind(&UpdateUserGestureCarryoverInfoOnUIThread
,
106 render_process_id
, render_frame_id
));
109 InterceptNavigationDelegate::InterceptNavigationDelegate(
110 JNIEnv
* env
, jobject jdelegate
)
111 : weak_jdelegate_(env
, jdelegate
) {
114 InterceptNavigationDelegate::~InterceptNavigationDelegate() {
117 bool InterceptNavigationDelegate::ShouldIgnoreNavigation(
118 const NavigationParams
& navigation_params
) {
119 if (!navigation_params
.url().is_valid())
122 JNIEnv
* env
= base::android::AttachCurrentThread();
123 ScopedJavaLocalRef
<jobject
> jdelegate
= weak_jdelegate_
.get(env
);
125 if (jdelegate
.is_null())
128 bool has_user_gesture_carryover
=
129 !navigation_params
.has_user_gesture() &&
130 base::TimeTicks::Now() - last_user_gesture_carryover_timestamp_
<=
131 base::TimeDelta::FromSeconds(
132 kMaxValidityOfUserGestureCarryoverInSeconds
);
134 ScopedJavaLocalRef
<jobject
> jobject_params
= CreateJavaNavigationParams(
135 env
, navigation_params
, has_user_gesture_carryover
);
137 return Java_InterceptNavigationDelegate_shouldIgnoreNavigation(
140 jobject_params
.obj());
143 void InterceptNavigationDelegate::UpdateLastUserGestureCarryoverTimestamp() {
144 last_user_gesture_carryover_timestamp_
= base::TimeTicks::Now();
147 // Register native methods.
149 bool RegisterInterceptNavigationDelegate(JNIEnv
* env
) {
150 return RegisterNativesImpl(env
);
153 } // namespace navigation_interception