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