[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 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 | |
[email protected] | 62885ab | 2013-01-23 03:55:16 | [diff] [blame] | 5 | #include "components/navigation_interception/intercept_navigation_delegate.h" |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 6 | |
| 7 | #include "base/android/jni_android.h" |
| 8 | #include "base/android/jni_string.h" |
| 9 | #include "base/callback.h" |
[email protected] | 62885ab | 2013-01-23 03:55:16 | [diff] [blame] | 10 | #include "components/navigation_interception/intercept_navigation_resource_throttle.h" |
[email protected] | c0d243a | 2013-01-31 21:20:16 | [diff] [blame] | 11 | #include "components/navigation_interception/navigation_params_android.h" |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 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 "googleurl/src/gurl.h" |
| 16 | #include "jni/InterceptNavigationDelegate_jni.h" |
| 17 | #include "net/url_request/url_request.h" |
| 18 | |
| 19 | using base::android::ConvertUTF8ToJavaString; |
| 20 | using base::android::ScopedJavaLocalRef; |
| 21 | using content::BrowserThread; |
[email protected] | 62885ab | 2013-01-23 03:55:16 | [diff] [blame] | 22 | using content::PageTransition; |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 23 | using content::RenderViewHost; |
| 24 | using content::WebContents; |
| 25 | |
[email protected] | 8812e3d0 | 2013-05-22 12:38:53 | [diff] [blame^] | 26 | namespace navigation_interception { |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 27 | |
| 28 | namespace { |
| 29 | |
| 30 | const void* kInterceptNavigationDelegateUserDataKey = |
| 31 | &kInterceptNavigationDelegateUserDataKey; |
| 32 | |
| 33 | bool CheckIfShouldIgnoreNavigationOnUIThread(RenderViewHost* source, |
[email protected] | c0d243a | 2013-01-31 21:20:16 | [diff] [blame] | 34 | const NavigationParams& params) { |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 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 | |
[email protected] | c0d243a | 2013-01-31 21:20:16 | [diff] [blame] | 46 | return intercept_navigation_delegate->ShouldIgnoreNavigation(params); |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | } // namespace |
| 50 | |
| 51 | // static |
| 52 | void InterceptNavigationDelegate::Associate( |
| 53 | WebContents* web_contents, |
| 54 | scoped_ptr<InterceptNavigationDelegate> delegate) { |
| 55 | web_contents->SetUserData(kInterceptNavigationDelegateUserDataKey, |
| 56 | delegate.release()); |
| 57 | } |
| 58 | |
| 59 | // static |
| 60 | InterceptNavigationDelegate* InterceptNavigationDelegate::Get( |
| 61 | WebContents* web_contents) { |
| 62 | return reinterpret_cast<InterceptNavigationDelegate*>( |
| 63 | web_contents->GetUserData(kInterceptNavigationDelegateUserDataKey)); |
| 64 | } |
| 65 | |
| 66 | // static |
| 67 | content::ResourceThrottle* InterceptNavigationDelegate::CreateThrottleFor( |
| 68 | net::URLRequest* request) { |
| 69 | return new InterceptNavigationResourceThrottle( |
| 70 | request, base::Bind(&CheckIfShouldIgnoreNavigationOnUIThread)); |
| 71 | } |
| 72 | |
| 73 | InterceptNavigationDelegate::InterceptNavigationDelegate( |
| 74 | JNIEnv* env, jobject jdelegate) |
| 75 | : weak_jdelegate_(env, jdelegate) { |
| 76 | } |
| 77 | |
| 78 | InterceptNavigationDelegate::~InterceptNavigationDelegate() { |
| 79 | } |
| 80 | |
| 81 | bool InterceptNavigationDelegate::ShouldIgnoreNavigation( |
[email protected] | c0d243a | 2013-01-31 21:20:16 | [diff] [blame] | 82 | const NavigationParams& navigation_params) { |
| 83 | if (!navigation_params.url().is_valid()) |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 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 | |
[email protected] | c0d243a | 2013-01-31 21:20:16 | [diff] [blame] | 92 | ScopedJavaLocalRef<jobject> jobject_params = |
| 93 | CreateJavaNavigationParams(env, navigation_params); |
| 94 | |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 95 | return Java_InterceptNavigationDelegate_shouldIgnoreNavigation( |
| 96 | env, |
| 97 | jdelegate.obj(), |
[email protected] | c0d243a | 2013-01-31 21:20:16 | [diff] [blame] | 98 | jobject_params.obj()); |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | // Register native methods. |
| 102 | |
| 103 | bool RegisterInterceptNavigationDelegate(JNIEnv* env) { |
| 104 | return RegisterNativesImpl(env); |
| 105 | } |
| 106 | |
[email protected] | 8812e3d0 | 2013-05-22 12:38:53 | [diff] [blame^] | 107 | } // namespace navigation_interception |