| OLD | NEW |
| (Empty) |
| 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 "chrome/browser/component/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 "chrome/browser/component/navigation_interception/intercept_navigation_
resource_throttle.h" | |
| 11 #include "content/public/browser/browser_thread.h" | |
| 12 #include "content/public/browser/render_view_host.h" | |
| 13 #include "content/public/browser/web_contents.h" | |
| 14 #include "googleurl/src/gurl.h" | |
| 15 #include "jni/InterceptNavigationDelegate_jni.h" | |
| 16 #include "net/url_request/url_request.h" | |
| 17 | |
| 18 using base::android::ConvertUTF8ToJavaString; | |
| 19 using base::android::ScopedJavaLocalRef; | |
| 20 using content::BrowserThread; | |
| 21 using content::RenderViewHost; | |
| 22 using content::WebContents; | |
| 23 | |
| 24 namespace navigation_interception { | |
| 25 | |
| 26 namespace { | |
| 27 | |
| 28 const void* kInterceptNavigationDelegateUserDataKey = | |
| 29 &kInterceptNavigationDelegateUserDataKey; | |
| 30 | |
| 31 bool CheckIfShouldIgnoreNavigationOnUIThread(RenderViewHost* source, | |
| 32 const GURL& url, | |
| 33 const content::Referrer& referrer, | |
| 34 bool has_user_gesture) { | |
| 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( | |
| 47 url, has_user_gesture); | |
| 48 } | |
| 49 | |
| 50 } // namespace | |
| 51 | |
| 52 // static | |
| 53 void InterceptNavigationDelegate::Associate( | |
| 54 WebContents* web_contents, | |
| 55 scoped_ptr<InterceptNavigationDelegate> delegate) { | |
| 56 web_contents->SetUserData(kInterceptNavigationDelegateUserDataKey, | |
| 57 delegate.release()); | |
| 58 } | |
| 59 | |
| 60 // static | |
| 61 InterceptNavigationDelegate* InterceptNavigationDelegate::Get( | |
| 62 WebContents* web_contents) { | |
| 63 return reinterpret_cast<InterceptNavigationDelegate*>( | |
| 64 web_contents->GetUserData(kInterceptNavigationDelegateUserDataKey)); | |
| 65 } | |
| 66 | |
| 67 // static | |
| 68 content::ResourceThrottle* InterceptNavigationDelegate::CreateThrottleFor( | |
| 69 net::URLRequest* request) { | |
| 70 return new InterceptNavigationResourceThrottle( | |
| 71 request, base::Bind(&CheckIfShouldIgnoreNavigationOnUIThread)); | |
| 72 } | |
| 73 | |
| 74 InterceptNavigationDelegate::InterceptNavigationDelegate( | |
| 75 JNIEnv* env, jobject jdelegate) | |
| 76 : weak_jdelegate_(env, jdelegate) { | |
| 77 } | |
| 78 | |
| 79 InterceptNavigationDelegate::~InterceptNavigationDelegate() { | |
| 80 } | |
| 81 | |
| 82 bool InterceptNavigationDelegate::ShouldIgnoreNavigation( | |
| 83 const GURL& url, | |
| 84 bool has_user_gesture) { | |
| 85 if (!url.is_valid()) | |
| 86 return false; | |
| 87 | |
| 88 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 89 ScopedJavaLocalRef<jobject> jdelegate = weak_jdelegate_.get(env); | |
| 90 | |
| 91 if (jdelegate.is_null()) | |
| 92 return false; | |
| 93 | |
| 94 ScopedJavaLocalRef<jstring> jstring_url = | |
| 95 ConvertUTF8ToJavaString(env, url.spec()); | |
| 96 return Java_InterceptNavigationDelegate_shouldIgnoreNavigation( | |
| 97 env, | |
| 98 jdelegate.obj(), | |
| 99 jstring_url.obj(), | |
| 100 has_user_gesture); | |
| 101 } | |
| 102 | |
| 103 // Register native methods. | |
| 104 | |
| 105 bool RegisterInterceptNavigationDelegate(JNIEnv* env) { | |
| 106 return RegisterNativesImpl(env); | |
| 107 } | |
| 108 | |
| 109 } // namespace navigation_interception | |
| OLD | NEW |