Chromium Code Reviews| Index: chrome/browser/component/navigation_interception/intercept_navigation_delegate.cc |
| diff --git a/chrome/browser/component/navigation_interception/intercept_navigation_delegate.cc b/chrome/browser/component/navigation_interception/intercept_navigation_delegate.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e57bf537723e79c451051d8f044bbd6c6d5401f9 |
| --- /dev/null |
| +++ b/chrome/browser/component/navigation_interception/intercept_navigation_delegate.cc |
| @@ -0,0 +1,107 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/component/navigation_interception/intercept_navigation_delegate.h" |
| + |
| +#include "base/android/jni_android.h" |
| +#include "base/android/jni_string.h" |
| +#include "base/callback.h" |
| +#include "chrome/browser/component/navigation_interception/intercept_navigation_resource_throttle.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/render_view_host.h" |
| +#include "content/public/browser/web_contents.h" |
| +#include "googleurl/src/gurl.h" |
| +#include "jni/InterceptNavigationDelegate_jni.h" |
| +#include "net/url_request/url_request.h" |
| + |
| +using base::android::ConvertUTF8ToJavaString; |
| +using base::android::ScopedJavaLocalRef; |
| +using content::BrowserThread; |
| +using content::RenderViewHost; |
| +using content::WebContents; |
| + |
| +namespace navigation_interception { |
| + |
| +namespace { |
| + |
| +const void* kInterceptNavigationDelegateUserDataKey = |
| + &kInterceptNavigationDelegateUserDataKey; |
| + |
| +bool CheckIfShouldIgnoreNavigationOnUIThread(RenderViewHost* source, |
| + const GURL& url, |
| + const content::Referrer& referrer, |
| + bool has_user_gesture) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + DCHECK(source); |
| + |
| + WebContents* web_contents = WebContents::FromRenderViewHost(source); |
| + if (!web_contents) return false; |
|
joth
2012/10/03 22:01:32
nit: in c++ we put the non-braced statement on its
mkosiba (inactive)
2012/10/08 15:09:12
Done.
|
| + InterceptNavigationDelegate* intercept_navigation_delegate = |
| + InterceptNavigationDelegate::Get(web_contents); |
| + if (!intercept_navigation_delegate) return false; |
| + |
| + return intercept_navigation_delegate->ShouldIgnoreNavigation( |
| + url, has_user_gesture); |
| +} |
| + |
| +} // namespace |
| + |
| +// static |
| +void InterceptNavigationDelegate::Associate( |
|
joth
2012/10/03 22:01:32
note in header that it transfers ownership. |deleg
mkosiba (inactive)
2012/10/08 15:09:12
well.. the use of scoped_ptr should imply that..
joth
2012/10/09 02:13:52
Oh Yes. My eyes are still blind to the idea of pas
|
| + WebContents* web_contents, |
| + scoped_ptr<InterceptNavigationDelegate> delegate) { |
| + web_contents->SetUserData(kInterceptNavigationDelegateUserDataKey, |
| + delegate.release()); |
| +} |
| + |
| +// static |
| +InterceptNavigationDelegate* InterceptNavigationDelegate::Get( |
| + WebContents* web_contents) { |
| + return reinterpret_cast<InterceptNavigationDelegate*>( |
| + web_contents->GetUserData(kInterceptNavigationDelegateUserDataKey)); |
| +} |
| + |
| +// static |
| +content::ResourceThrottle* InterceptNavigationDelegate::CreateThrottleFor( |
| + net::URLRequest* request) { |
| + return new InterceptNavigationResourceThrottle( |
| + request, base::Bind(&CheckIfShouldIgnoreNavigationOnUIThread)); |
| +} |
| + |
| +InterceptNavigationDelegate::InterceptNavigationDelegate( |
| + JNIEnv* env, jobject jdelegate) |
| + : weak_jdelegate_(env, jdelegate) { |
| +} |
| + |
| +InterceptNavigationDelegate::~InterceptNavigationDelegate() { |
| +} |
| + |
| +bool InterceptNavigationDelegate::ShouldIgnoreNavigation( |
| + const GURL& url, |
| + bool has_user_gesture) { |
| + if (!url.is_valid()) |
| + return false; |
| + |
| + JNIEnv* env = base::android::AttachCurrentThread(); |
| + ScopedJavaLocalRef<jobject> jdelegate = weak_jdelegate_.get(env); |
| + |
| + if (jdelegate.is_null()) |
| + return false; |
| + |
| + ScopedJavaLocalRef<jstring> jstring_url = |
| + ConvertUTF8ToJavaString(env, url.spec()); |
| + return Java_InterceptNavigationDelegate_shouldIgnoreNavigation( |
| + env, |
| + jdelegate.obj(), |
| + jstring_url.obj(), |
| + has_user_gesture); |
| +} |
| + |
| +// Register native methods. |
| + |
| +bool RegisterInterceptNavigationDelegate(JNIEnv* env) { |
| + return RegisterNativesImpl(env); |
| +} |
| + |
| +} // namespace navigation_interception |