• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
AddPrerender(JNIEnv * env,jobject obj,jobject jprofile,jlong web_contents_ptr,jstring jurl,jstring jreferrer,jint width,jint height)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 
CancelCurrentPrerender(JNIEnv * env,jobject object)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 
HasPrerenderedUrl(JNIEnv * env,jclass clazz,jobject jprofile,jstring jurl,jlong web_contents_ptr)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 
HasCookieStoreLoaded(JNIEnv * env,jclass clazz,jobject jprofile)92 static jboolean HasCookieStoreLoaded(JNIEnv* env,
93                                      jclass clazz,
94                                      jobject jprofile) {
95   Profile* profile = ProfileAndroid::FromProfileAndroid(jprofile);
96   prerender::PrerenderManager* prerender_manager =
97       prerender::PrerenderManagerFactory::GetForProfile(profile);
98   if (!prerender_manager)
99     return false;
100   return prerender_manager->cookie_store_loaded();
101 }
102 
ExternalPrerenderHandlerAndroid()103 ExternalPrerenderHandlerAndroid::ExternalPrerenderHandlerAndroid() {}
104 
~ExternalPrerenderHandlerAndroid()105 ExternalPrerenderHandlerAndroid::~ExternalPrerenderHandlerAndroid() {}
106 
Init(JNIEnv * env,jclass clazz)107 static jlong Init(JNIEnv* env, jclass clazz) {
108   ExternalPrerenderHandlerAndroid* external_handler =
109       new ExternalPrerenderHandlerAndroid();
110   return reinterpret_cast<intptr_t>(external_handler);
111 }
112 
RegisterExternalPrerenderHandlerAndroid(JNIEnv * env)113 bool ExternalPrerenderHandlerAndroid::RegisterExternalPrerenderHandlerAndroid(
114     JNIEnv* env) {
115   return RegisterNativesImpl(env);
116 }
117 
118 } // namespace prerender
119