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 content::PageTransition;
23 using content::RenderViewHost;
24 using content::WebContents;
26 namespace navigation_interception {
30 const void* kInterceptNavigationDelegateUserDataKey =
31 &kInterceptNavigationDelegateUserDataKey;
33 bool CheckIfShouldIgnoreNavigationOnUIThread(RenderViewHost* source,
34 const NavigationParams& params) {
35 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
38 WebContents* web_contents = WebContents::FromRenderViewHost(source);
41 InterceptNavigationDelegate* intercept_navigation_delegate =
42 InterceptNavigationDelegate::Get(web_contents);
43 if (!intercept_navigation_delegate)
46 return intercept_navigation_delegate->ShouldIgnoreNavigation(params);
52 void InterceptNavigationDelegate::Associate(
53 WebContents* web_contents,
54 scoped_ptr<InterceptNavigationDelegate> delegate) {
55 web_contents->SetUserData(kInterceptNavigationDelegateUserDataKey,
60 InterceptNavigationDelegate* InterceptNavigationDelegate::Get(
61 WebContents* web_contents) {
62 return reinterpret_cast<InterceptNavigationDelegate*>(
63 web_contents->GetUserData(kInterceptNavigationDelegateUserDataKey));
67 content::ResourceThrottle* InterceptNavigationDelegate::CreateThrottleFor(
68 net::URLRequest* request) {
69 return new InterceptNavigationResourceThrottle(
70 request, base::Bind(&CheckIfShouldIgnoreNavigationOnUIThread));
73 InterceptNavigationDelegate::InterceptNavigationDelegate(
74 JNIEnv* env, jobject jdelegate)
75 : weak_jdelegate_(env, jdelegate) {
78 InterceptNavigationDelegate::~InterceptNavigationDelegate() {
81 bool InterceptNavigationDelegate::ShouldIgnoreNavigation(
82 const NavigationParams& navigation_params) {
83 if (!navigation_params.url().is_valid())
86 JNIEnv* env = base::android::AttachCurrentThread();
87 ScopedJavaLocalRef<jobject> jdelegate = weak_jdelegate_.get(env);
89 if (jdelegate.is_null())
92 ScopedJavaLocalRef<jobject> jobject_params =
93 CreateJavaNavigationParams(env, navigation_params);
95 return Java_InterceptNavigationDelegate_shouldIgnoreNavigation(
98 jobject_params.obj());
101 // Register native methods.
103 bool RegisterInterceptNavigationDelegate(JNIEnv* env) {
104 return RegisterNativesImpl(env);
107 } // namespace navigation_interception