1 // Copyright 2013 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.
5 #include "chrome/browser/prerender/external_prerender_handler_android.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "base/strings/string16.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/prerender/prerender_handle.h"
13 #include "chrome/browser/prerender/prerender_manager.h"
14 #include "chrome/browser/prerender/prerender_manager_factory.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/profiles/profile_android.h"
17 #include "content/public/browser/web_contents.h"
19 #include "jni/ExternalPrerenderHandler_jni.h"
21 using base::android::ConvertJavaStringToUTF16
;
25 bool ExternalPrerenderHandlerAndroid::AddPrerender(JNIEnv
* env
,
28 jobject jweb_contents
,
33 Profile
* profile
= ProfileAndroid::FromProfileAndroid(jprofile
);
35 GURL url
= GURL(ConvertJavaStringToUTF16(env
, jurl
));
39 GURL referrer_url
= GURL(ConvertJavaStringToUTF16(env
, jreferrer
));
40 content::Referrer referrer
= referrer_url
.is_valid() ?
41 content::Referrer(referrer_url
, blink::WebReferrerPolicyDefault
) :
43 PrerenderManager
* prerender_manager
=
44 prerender::PrerenderManagerFactory::GetForProfile(profile
);
45 if (!prerender_manager
)
47 content::WebContents
* web_contents
=
48 content::WebContents::FromJavaWebContents(jweb_contents
);
49 if (prerender_handle_
.get()) {
50 prerender_handle_
->OnNavigateAway();
52 prerender_handle_
.reset(
53 prerender_manager
->AddPrerenderFromExternalRequest(
56 web_contents
->GetController().GetDefaultSessionStorageNamespace(),
57 gfx::Size(width
, height
)));
58 if (!prerender_handle_
)
63 void ExternalPrerenderHandlerAndroid::CancelCurrentPrerender(JNIEnv
* env
,
65 if (!prerender_handle_
)
67 prerender_handle_
->OnCancel();
68 prerender_handle_
.reset();
71 static jboolean
HasPrerenderedUrl(JNIEnv
* env
,
72 const JavaParamRef
<jclass
>& clazz
,
73 const JavaParamRef
<jobject
>& jprofile
,
74 const JavaParamRef
<jstring
>& jurl
,
75 const JavaParamRef
<jobject
>& jweb_contents
) {
79 Profile
* profile
= ProfileAndroid::FromProfileAndroid(jprofile
);
80 GURL url
= GURL(ConvertJavaStringToUTF16(env
, jurl
));
83 prerender::PrerenderManager
* prerender_manager
=
84 prerender::PrerenderManagerFactory::GetForProfile(profile
);
85 if (!prerender_manager
)
87 content::WebContents
* web_contents
=
88 content::WebContents::FromJavaWebContents(jweb_contents
);
89 return prerender_manager
->HasPrerenderedUrl(url
, web_contents
);
92 ExternalPrerenderHandlerAndroid::ExternalPrerenderHandlerAndroid() {}
94 ExternalPrerenderHandlerAndroid::~ExternalPrerenderHandlerAndroid() {}
96 static jlong
Init(JNIEnv
* env
, const JavaParamRef
<jclass
>& clazz
) {
97 ExternalPrerenderHandlerAndroid
* external_handler
=
98 new ExternalPrerenderHandlerAndroid();
99 return reinterpret_cast<intptr_t>(external_handler
);
102 bool ExternalPrerenderHandlerAndroid::RegisterExternalPrerenderHandlerAndroid(
104 return RegisterNativesImpl(env
);
107 } // namespace prerender