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. 4 5 #include "chrome/browser/prerender/external_prerender_handler_android.h" 6 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" 18 19 #include "jni/ExternalPrerenderHandler_jni.h" 20 21 using base::android::ConvertJavaStringToUTF16; 22 23 namespace prerender { 24 25 bool ExternalPrerenderHandlerAndroid::AddPrerender(JNIEnv* env, 26 jobject obj, 27 jobject jprofile, 28 jlong web_contents_ptr, 29 jstring jurl, 30 jstring jreferrer, 31 jint width, 32 jint height) { 33 Profile* profile = ProfileAndroid::FromProfileAndroid(jprofile); 34 35 GURL url = GURL(ConvertJavaStringToUTF16(env, jurl)); 36 if (!url.is_valid()) 37 return false; 38 39 GURL referrer_url = GURL(ConvertJavaStringToUTF16(env, jreferrer)); 40 content::Referrer referrer = referrer_url.is_valid() ? 41 content::Referrer(referrer_url, blink::WebReferrerPolicyDefault) : 42 content::Referrer(); 43 PrerenderManager* prerender_manager = 44 prerender::PrerenderManagerFactory::GetForProfile(profile); 45 if (!prerender_manager) 46 return false; 47 content::WebContents* web_contents = 48 reinterpret_cast<content::WebContents*>(web_contents_ptr); 49 if (prerender_handle_.get()) { 50 prerender_handle_->OnNavigateAway(); 51 } 52 prerender_handle_.reset( 53 prerender_manager->AddPrerenderFromExternalRequest( 54 url, 55 referrer, 56 web_contents->GetController().GetDefaultSessionStorageNamespace(), 57 gfx::Size(width, height))); 58 if (!prerender_handle_) 59 return false; 60 return true; 61 } 62 63 void ExternalPrerenderHandlerAndroid::CancelCurrentPrerender(JNIEnv* env, 64 jobject object) { 65 if (!prerender_handle_) 66 return; 67 prerender_handle_->OnCancel(); 68 prerender_handle_.reset(); 69 } 70 71 static jboolean HasPrerenderedUrl(JNIEnv* env, 72 jclass clazz, 73 jobject jprofile, 74 jstring jurl, 75 jlong web_contents_ptr) { 76 if (jurl == NULL) 77 return false; 78 79 Profile* profile = ProfileAndroid::FromProfileAndroid(jprofile); 80 GURL url = GURL(ConvertJavaStringToUTF16(env, jurl)); 81 if (!url.is_valid()) 82 return false; 83 prerender::PrerenderManager* prerender_manager = 84 prerender::PrerenderManagerFactory::GetForProfile(profile); 85 if (!prerender_manager) 86 return false; 87 content::WebContents* web_contents = 88 reinterpret_cast<content::WebContents*>(web_contents_ptr); 89 return prerender_manager->HasPrerenderedUrl(url, web_contents); 90 } 91 92 ExternalPrerenderHandlerAndroid::ExternalPrerenderHandlerAndroid() {} 93 94 ExternalPrerenderHandlerAndroid::~ExternalPrerenderHandlerAndroid() {} 95 96 static jlong Init(JNIEnv* env, jclass clazz) { 97 ExternalPrerenderHandlerAndroid* external_handler = 98 new ExternalPrerenderHandlerAndroid(); 99 return reinterpret_cast<intptr_t>(external_handler); 100 } 101 102 bool ExternalPrerenderHandlerAndroid::RegisterExternalPrerenderHandlerAndroid( 103 JNIEnv* env) { 104 return RegisterNativesImpl(env); 105 } 106 107 } // namespace prerender 108