blob: cc3feb42d13425ac0eef8e6f31f9cfcf6f460992 [file] [log] [blame]
[email protected]4360ae72012-10-09 22:10:461// 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]62885ab2013-01-23 03:55:165#include "components/navigation_interception/intercept_navigation_delegate.h"
[email protected]4360ae72012-10-09 22:10:466
7#include "base/android/jni_android.h"
8#include "base/android/jni_string.h"
9#include "base/callback.h"
[email protected]62885ab2013-01-23 03:55:1610#include "components/navigation_interception/intercept_navigation_resource_throttle.h"
[email protected]c0d243a2013-01-31 21:20:1611#include "components/navigation_interception/navigation_params_android.h"
[email protected]4360ae72012-10-09 22:10:4612#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
19using base::android::ConvertUTF8ToJavaString;
20using base::android::ScopedJavaLocalRef;
21using content::BrowserThread;
[email protected]62885ab2013-01-23 03:55:1622using content::PageTransition;
[email protected]4360ae72012-10-09 22:10:4623using content::RenderViewHost;
24using content::WebContents;
25
[email protected]8812e3d02013-05-22 12:38:5326namespace navigation_interception {
[email protected]4360ae72012-10-09 22:10:4627
28namespace {
29
30const void* kInterceptNavigationDelegateUserDataKey =
31 &kInterceptNavigationDelegateUserDataKey;
32
33bool CheckIfShouldIgnoreNavigationOnUIThread(RenderViewHost* source,
[email protected]c0d243a2013-01-31 21:20:1634 const NavigationParams& params) {
[email protected]4360ae72012-10-09 22:10:4635 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]c0d243a2013-01-31 21:20:1646 return intercept_navigation_delegate->ShouldIgnoreNavigation(params);
[email protected]4360ae72012-10-09 22:10:4647}
48
49} // namespace
50
51// static
52void InterceptNavigationDelegate::Associate(
53 WebContents* web_contents,
54 scoped_ptr<InterceptNavigationDelegate> delegate) {
55 web_contents->SetUserData(kInterceptNavigationDelegateUserDataKey,
56 delegate.release());
57}
58
59// static
60InterceptNavigationDelegate* InterceptNavigationDelegate::Get(
61 WebContents* web_contents) {
62 return reinterpret_cast<InterceptNavigationDelegate*>(
63 web_contents->GetUserData(kInterceptNavigationDelegateUserDataKey));
64}
65
66// static
67content::ResourceThrottle* InterceptNavigationDelegate::CreateThrottleFor(
68 net::URLRequest* request) {
69 return new InterceptNavigationResourceThrottle(
70 request, base::Bind(&CheckIfShouldIgnoreNavigationOnUIThread));
71}
72
73InterceptNavigationDelegate::InterceptNavigationDelegate(
74 JNIEnv* env, jobject jdelegate)
75 : weak_jdelegate_(env, jdelegate) {
76}
77
78InterceptNavigationDelegate::~InterceptNavigationDelegate() {
79}
80
81bool InterceptNavigationDelegate::ShouldIgnoreNavigation(
[email protected]c0d243a2013-01-31 21:20:1682 const NavigationParams& navigation_params) {
83 if (!navigation_params.url().is_valid())
[email protected]4360ae72012-10-09 22:10:4684 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]c0d243a2013-01-31 21:20:1692 ScopedJavaLocalRef<jobject> jobject_params =
93 CreateJavaNavigationParams(env, navigation_params);
94
[email protected]4360ae72012-10-09 22:10:4695 return Java_InterceptNavigationDelegate_shouldIgnoreNavigation(
96 env,
97 jdelegate.obj(),
[email protected]c0d243a2013-01-31 21:20:1698 jobject_params.obj());
[email protected]4360ae72012-10-09 22:10:4699}
100
101// Register native methods.
102
103bool RegisterInterceptNavigationDelegate(JNIEnv* env) {
104 return RegisterNativesImpl(env);
105}
106
[email protected]8812e3d02013-05-22 12:38:53107} // namespace navigation_interception