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. 4 5#include "components/navigation_interception/intercept_navigation_delegate.h" 6 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" 17#include "url/gurl.h" 18 19using base::android::ConvertUTF8ToJavaString; 20using base::android::ScopedJavaLocalRef; 21using content::BrowserThread; 22using content::PageTransition; 23using content::RenderViewHost; 24using content::WebContents; 25 26namespace navigation_interception { 27 28namespace { 29 30const void* kInterceptNavigationDelegateUserDataKey = 31 &kInterceptNavigationDelegateUserDataKey; 32 33bool CheckIfShouldIgnoreNavigationOnUIThread(RenderViewHost* source, 34 const NavigationParams& params) { 35 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 36 DCHECK(source); 37 38 WebContents* web_contents = WebContents::FromRenderViewHost(source); 39 if (!web_contents) 40 return false; 41 InterceptNavigationDelegate* intercept_navigation_delegate = 42 InterceptNavigationDelegate::Get(web_contents); 43 if (!intercept_navigation_delegate) 44 return false; 45 46 return intercept_navigation_delegate->ShouldIgnoreNavigation(params); 47} 48 49} // namespace 50 51// static 52void InterceptNavigationDelegate::Associate( 53 WebContents* web_contents, 54 scoped_ptr<InterceptNavigationDelegate> delegate) { 55 web_contents->SetUserData(kInterceptNavigationDelegateUserDataKey, 56 delegate.release()); 57} 58 59// static 60InterceptNavigationDelegate* InterceptNavigationDelegate::Get( 61 WebContents* web_contents) { 62 return reinterpret_cast<InterceptNavigationDelegate*>( 63 web_contents->GetUserData(kInterceptNavigationDelegateUserDataKey)); 64} 65 66// static 67content::ResourceThrottle* InterceptNavigationDelegate::CreateThrottleFor( 68 net::URLRequest* request) { 69 return new InterceptNavigationResourceThrottle( 70 request, base::Bind(&CheckIfShouldIgnoreNavigationOnUIThread)); 71} 72 73InterceptNavigationDelegate::InterceptNavigationDelegate( 74 JNIEnv* env, jobject jdelegate) 75 : weak_jdelegate_(env, jdelegate) { 76} 77 78InterceptNavigationDelegate::~InterceptNavigationDelegate() { 79} 80 81bool InterceptNavigationDelegate::ShouldIgnoreNavigation( 82 const NavigationParams& navigation_params) { 83 if (!navigation_params.url().is_valid()) 84 return false; 85 86 JNIEnv* env = base::android::AttachCurrentThread(); 87 ScopedJavaLocalRef<jobject> jdelegate = weak_jdelegate_.get(env); 88 89 if (jdelegate.is_null()) 90 return false; 91 92 ScopedJavaLocalRef<jobject> jobject_params = 93 CreateJavaNavigationParams(env, navigation_params); 94 95 return Java_InterceptNavigationDelegate_shouldIgnoreNavigation( 96 env, 97 jdelegate.obj(), 98 jobject_params.obj()); 99} 100 101// Register native methods. 102 103bool RegisterInterceptNavigationDelegate(JNIEnv* env) { 104 return RegisterNativesImpl(env); 105} 106 107} // namespace navigation_interception 108