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_view_host.h"
14 #include "content/public/browser/web_contents.h"
15 #include "jni/InterceptNavigationDelegate_jni.h"
16 #include "net/url_request/url_request.h"
19 using base::android::ConvertUTF8ToJavaString
;
20 using base::android::ScopedJavaLocalRef
;
21 using content::BrowserThread
;
22 using ui::PageTransition
;
23 using content::RenderViewHost
;
24 using content::WebContents
;
26 namespace navigation_interception
{
30 const void* kInterceptNavigationDelegateUserDataKey
=
31 &kInterceptNavigationDelegateUserDataKey
;
33 bool CheckIfShouldIgnoreNavigationOnUIThread(WebContents
* source
,
34 const NavigationParams
& params
) {
35 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
38 InterceptNavigationDelegate
* intercept_navigation_delegate
=
39 InterceptNavigationDelegate::Get(source
);
40 if (!intercept_navigation_delegate
)
43 return intercept_navigation_delegate
->ShouldIgnoreNavigation(params
);
49 void InterceptNavigationDelegate::Associate(
50 WebContents
* web_contents
,
51 scoped_ptr
<InterceptNavigationDelegate
> delegate
) {
52 web_contents
->SetUserData(kInterceptNavigationDelegateUserDataKey
,
57 InterceptNavigationDelegate
* InterceptNavigationDelegate::Get(
58 WebContents
* web_contents
) {
59 return static_cast<InterceptNavigationDelegate
*>(
60 web_contents
->GetUserData(kInterceptNavigationDelegateUserDataKey
));
64 content::ResourceThrottle
* InterceptNavigationDelegate::CreateThrottleFor(
65 net::URLRequest
* request
) {
66 return new InterceptNavigationResourceThrottle(
67 request
, base::Bind(&CheckIfShouldIgnoreNavigationOnUIThread
));
70 InterceptNavigationDelegate::InterceptNavigationDelegate(
71 JNIEnv
* env
, jobject jdelegate
)
72 : weak_jdelegate_(env
, jdelegate
) {
75 InterceptNavigationDelegate::~InterceptNavigationDelegate() {
78 bool InterceptNavigationDelegate::ShouldIgnoreNavigation(
79 const NavigationParams
& navigation_params
) {
80 if (!navigation_params
.url().is_valid())
83 JNIEnv
* env
= base::android::AttachCurrentThread();
84 ScopedJavaLocalRef
<jobject
> jdelegate
= weak_jdelegate_
.get(env
);
86 if (jdelegate
.is_null())
89 ScopedJavaLocalRef
<jobject
> jobject_params
=
90 CreateJavaNavigationParams(env
, navigation_params
);
92 return Java_InterceptNavigationDelegate_shouldIgnoreNavigation(
95 jobject_params
.obj());
98 // Register native methods.
100 bool RegisterInterceptNavigationDelegate(JNIEnv
* env
) {
101 return RegisterNativesImpl(env
);
104 } // namespace navigation_interception