• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 using base::android::ConvertUTF8ToJavaString;
20 using base::android::ScopedJavaLocalRef;
21 using content::BrowserThread;
22 using ui::PageTransition;
23 using content::RenderViewHost;
24 using content::WebContents;
25 
26 namespace navigation_interception {
27 
28 namespace {
29 
30 const void* kInterceptNavigationDelegateUserDataKey =
31     &kInterceptNavigationDelegateUserDataKey;
32 
CheckIfShouldIgnoreNavigationOnUIThread(WebContents * source,const NavigationParams & params)33 bool 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
Associate(WebContents * web_contents,scoped_ptr<InterceptNavigationDelegate> delegate)49 void InterceptNavigationDelegate::Associate(
50     WebContents* web_contents,
51     scoped_ptr<InterceptNavigationDelegate> delegate) {
52   web_contents->SetUserData(kInterceptNavigationDelegateUserDataKey,
53                             delegate.release());
54 }
55 
56 // static
Get(WebContents * web_contents)57 InterceptNavigationDelegate* InterceptNavigationDelegate::Get(
58     WebContents* web_contents) {
59   return reinterpret_cast<InterceptNavigationDelegate*>(
60       web_contents->GetUserData(kInterceptNavigationDelegateUserDataKey));
61 }
62 
63 // static
CreateThrottleFor(net::URLRequest * request)64 content::ResourceThrottle* InterceptNavigationDelegate::CreateThrottleFor(
65     net::URLRequest* request) {
66   return new InterceptNavigationResourceThrottle(
67       request, base::Bind(&CheckIfShouldIgnoreNavigationOnUIThread));
68 }
69 
InterceptNavigationDelegate(JNIEnv * env,jobject jdelegate)70 InterceptNavigationDelegate::InterceptNavigationDelegate(
71     JNIEnv* env, jobject jdelegate)
72     : weak_jdelegate_(env, jdelegate) {
73 }
74 
~InterceptNavigationDelegate()75 InterceptNavigationDelegate::~InterceptNavigationDelegate() {
76 }
77 
ShouldIgnoreNavigation(const NavigationParams & navigation_params)78 bool 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 
RegisterInterceptNavigationDelegate(JNIEnv * env)100 bool RegisterInterceptNavigationDelegate(JNIEnv* env) {
101   return RegisterNativesImpl(env);
102 }
103 
104 }  // namespace navigation_interception
105